@hviana/sema 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. package/dist/example/demo.d.ts +1 -0
  2. package/dist/example/demo.js +39 -0
  3. package/dist/example/train_base.d.ts +87 -0
  4. package/dist/example/train_base.js +2216 -0
  5. package/dist/src/alphabet.d.ts +7 -0
  6. package/dist/src/alphabet.js +33 -0
  7. package/dist/src/alu/src/alu.d.ts +185 -0
  8. package/dist/src/alu/src/alu.js +440 -0
  9. package/dist/src/alu/src/expr.d.ts +61 -0
  10. package/dist/src/alu/src/expr.js +318 -0
  11. package/dist/src/alu/src/index.d.ts +11 -0
  12. package/dist/src/alu/src/index.js +19 -0
  13. package/dist/src/alu/src/kernel-arith.d.ts +16 -0
  14. package/dist/src/alu/src/kernel-arith.js +264 -0
  15. package/dist/src/alu/src/kernel-bits.d.ts +19 -0
  16. package/dist/src/alu/src/kernel-bits.js +152 -0
  17. package/dist/src/alu/src/kernel-logic.d.ts +4 -0
  18. package/dist/src/alu/src/kernel-logic.js +60 -0
  19. package/dist/src/alu/src/kernel-nd.d.ts +3 -0
  20. package/dist/src/alu/src/kernel-nd.js +208 -0
  21. package/dist/src/alu/src/kernel-numeric.d.ts +54 -0
  22. package/dist/src/alu/src/kernel-numeric.js +366 -0
  23. package/dist/src/alu/src/operation.d.ts +168 -0
  24. package/dist/src/alu/src/operation.js +189 -0
  25. package/dist/src/alu/src/parser.d.ts +212 -0
  26. package/dist/src/alu/src/parser.js +469 -0
  27. package/dist/src/alu/src/resonance.d.ts +55 -0
  28. package/dist/src/alu/src/resonance.js +126 -0
  29. package/dist/src/alu/src/text.d.ts +31 -0
  30. package/dist/src/alu/src/text.js +73 -0
  31. package/dist/src/alu/src/value.d.ts +109 -0
  32. package/dist/src/alu/src/value.js +300 -0
  33. package/dist/src/alu/test/alu.test.d.ts +1 -0
  34. package/dist/src/alu/test/alu.test.js +764 -0
  35. package/dist/src/bytes.d.ts +14 -0
  36. package/dist/src/bytes.js +59 -0
  37. package/dist/src/config.d.ts +114 -0
  38. package/dist/src/config.js +96 -0
  39. package/dist/src/derive/src/deduction.d.ts +125 -0
  40. package/dist/src/derive/src/deduction.js +155 -0
  41. package/dist/src/derive/src/index.d.ts +7 -0
  42. package/dist/src/derive/src/index.js +11 -0
  43. package/dist/src/derive/src/priority-queue.d.ts +20 -0
  44. package/dist/src/derive/src/priority-queue.js +73 -0
  45. package/dist/src/derive/src/rewrite.d.ts +56 -0
  46. package/dist/src/derive/src/rewrite.js +100 -0
  47. package/dist/src/derive/src/trie.d.ts +90 -0
  48. package/dist/src/derive/src/trie.js +217 -0
  49. package/dist/src/derive/test/derive.test.d.ts +1 -0
  50. package/dist/src/derive/test/derive.test.js +122 -0
  51. package/dist/src/extension.d.ts +37 -0
  52. package/dist/src/extension.js +7 -0
  53. package/dist/src/geometry.d.ts +137 -0
  54. package/dist/src/geometry.js +430 -0
  55. package/dist/src/index.d.ts +15 -0
  56. package/dist/src/index.js +21 -0
  57. package/dist/src/ingest-cache.d.ts +41 -0
  58. package/dist/src/ingest-cache.js +161 -0
  59. package/dist/src/mind/articulation.d.ts +6 -0
  60. package/dist/src/mind/articulation.js +99 -0
  61. package/dist/src/mind/attention.d.ts +72 -0
  62. package/dist/src/mind/attention.js +894 -0
  63. package/dist/src/mind/canonical.d.ts +29 -0
  64. package/dist/src/mind/canonical.js +88 -0
  65. package/dist/src/mind/graph-search.d.ts +270 -0
  66. package/dist/src/mind/graph-search.js +847 -0
  67. package/dist/src/mind/index.d.ts +5 -0
  68. package/dist/src/mind/index.js +5 -0
  69. package/dist/src/mind/junction.d.ts +95 -0
  70. package/dist/src/mind/junction.js +262 -0
  71. package/dist/src/mind/learning.d.ts +47 -0
  72. package/dist/src/mind/learning.js +201 -0
  73. package/dist/src/mind/match.d.ts +111 -0
  74. package/dist/src/mind/match.js +422 -0
  75. package/dist/src/mind/mechanisms/alu.d.ts +4 -0
  76. package/dist/src/mind/mechanisms/alu.js +29 -0
  77. package/dist/src/mind/mechanisms/cast.d.ts +35 -0
  78. package/dist/src/mind/mechanisms/cast.js +447 -0
  79. package/dist/src/mind/mechanisms/confluence.d.ts +24 -0
  80. package/dist/src/mind/mechanisms/confluence.js +213 -0
  81. package/dist/src/mind/mechanisms/cover.d.ts +6 -0
  82. package/dist/src/mind/mechanisms/cover.js +179 -0
  83. package/dist/src/mind/mechanisms/extraction.d.ts +67 -0
  84. package/dist/src/mind/mechanisms/extraction.js +342 -0
  85. package/dist/src/mind/mechanisms/recall.d.ts +13 -0
  86. package/dist/src/mind/mechanisms/recall.js +151 -0
  87. package/dist/src/mind/mind.d.ts +147 -0
  88. package/dist/src/mind/mind.js +300 -0
  89. package/dist/src/mind/pipeline-mechanism.d.ts +142 -0
  90. package/dist/src/mind/pipeline-mechanism.js +213 -0
  91. package/dist/src/mind/pipeline.d.ts +20 -0
  92. package/dist/src/mind/pipeline.js +185 -0
  93. package/dist/src/mind/primitives.d.ts +43 -0
  94. package/dist/src/mind/primitives.js +162 -0
  95. package/dist/src/mind/rationale.d.ts +134 -0
  96. package/dist/src/mind/rationale.js +162 -0
  97. package/dist/src/mind/reasoning.d.ts +15 -0
  98. package/dist/src/mind/reasoning.js +162 -0
  99. package/dist/src/mind/recognition.d.ts +20 -0
  100. package/dist/src/mind/recognition.js +223 -0
  101. package/dist/src/mind/resonance.d.ts +23 -0
  102. package/dist/src/mind/resonance.js +0 -0
  103. package/dist/src/mind/trace.d.ts +15 -0
  104. package/dist/src/mind/trace.js +73 -0
  105. package/dist/src/mind/traverse.d.ts +100 -0
  106. package/dist/src/mind/traverse.js +447 -0
  107. package/dist/src/mind/types.d.ts +174 -0
  108. package/dist/src/mind/types.js +84 -0
  109. package/dist/src/rabitq-hnsw/src/database.d.ts +200 -0
  110. package/dist/src/rabitq-hnsw/src/database.js +388 -0
  111. package/dist/src/rabitq-hnsw/src/heap.d.ts +22 -0
  112. package/dist/src/rabitq-hnsw/src/heap.js +89 -0
  113. package/dist/src/rabitq-hnsw/src/hnsw.d.ts +125 -0
  114. package/dist/src/rabitq-hnsw/src/hnsw.js +474 -0
  115. package/dist/src/rabitq-hnsw/src/index.d.ts +10 -0
  116. package/dist/src/rabitq-hnsw/src/index.js +6 -0
  117. package/dist/src/rabitq-hnsw/src/prng.d.ts +19 -0
  118. package/dist/src/rabitq-hnsw/src/prng.js +36 -0
  119. package/dist/src/rabitq-hnsw/src/rabitq.d.ts +95 -0
  120. package/dist/src/rabitq-hnsw/src/rabitq.js +283 -0
  121. package/dist/src/rabitq-hnsw/src/store.d.ts +162 -0
  122. package/dist/src/rabitq-hnsw/src/store.js +825 -0
  123. package/dist/src/rabitq-hnsw/test/hnsw.test.d.ts +1 -0
  124. package/dist/src/rabitq-hnsw/test/hnsw.test.js +948 -0
  125. package/dist/src/store-sqlite.d.ts +149 -0
  126. package/dist/src/store-sqlite.js +702 -0
  127. package/dist/src/store.d.ts +638 -0
  128. package/dist/src/store.js +1618 -0
  129. package/dist/src/vec.d.ts +31 -0
  130. package/dist/src/vec.js +109 -0
  131. package/package.json +1 -1
@@ -0,0 +1,14 @@
1
+ /** True when two byte spans are equal in length and content. */
2
+ export declare function bytesEqual(a: Uint8Array, b: Uint8Array): boolean;
3
+ /** Concatenate byte arrays. Takes an array rather than rest params so
4
+ * a large segment list can never overflow the call stack via spread. */
5
+ export declare function concatBytes(parts: Uint8Array[]): Uint8Array;
6
+ /** Join two byte spans — the hot two-operand case of {@link concatBytes},
7
+ * fused without the array wrapper for the search's inner fuse loop. */
8
+ export declare function concat2(a: Uint8Array, b: Uint8Array): Uint8Array;
9
+ /** Latin-1 view of a byte span — a stable, lossless string key for chart
10
+ * memoization (every byte 0–255 maps to one code unit). */
11
+ export declare function latin1(b: Uint8Array): string;
12
+ /** First index ≥ `from` at which `needle` occurs in `hay`, or -1. A short naive
13
+ * scan — used only to locate a result span inside a learnt framing form. */
14
+ export declare function indexOf(hay: Uint8Array, needle: Uint8Array, from: number): number;
@@ -0,0 +1,59 @@
1
+ // bytes.ts — small, pure byte-span utilities.
2
+ //
3
+ // Nothing here knows about Sema, the store, or the search; these are the
4
+ // mechanical operations on Uint8Arrays that the rest of the code leans on, kept
5
+ // together so a reader meets them once and never wonders whether a given helper
6
+ // hides a side effect.
7
+ /** True when two byte spans are equal in length and content. */
8
+ export function bytesEqual(a, b) {
9
+ if (a.length !== b.length)
10
+ return false;
11
+ for (let i = 0; i < a.length; i++)
12
+ if (a[i] !== b[i])
13
+ return false;
14
+ return true;
15
+ }
16
+ /** Concatenate byte arrays. Takes an array rather than rest params so
17
+ * a large segment list can never overflow the call stack via spread. */
18
+ export function concatBytes(parts) {
19
+ let total = 0;
20
+ for (const p of parts)
21
+ total += p.length;
22
+ const out = new Uint8Array(total);
23
+ let off = 0;
24
+ for (const p of parts) {
25
+ out.set(p, off);
26
+ off += p.length;
27
+ }
28
+ return out;
29
+ }
30
+ /** Join two byte spans — the hot two-operand case of {@link concatBytes},
31
+ * fused without the array wrapper for the search's inner fuse loop. */
32
+ export function concat2(a, b) {
33
+ const out = new Uint8Array(a.length + b.length);
34
+ out.set(a, 0);
35
+ out.set(b, a.length);
36
+ return out;
37
+ }
38
+ /** Latin-1 view of a byte span — a stable, lossless string key for chart
39
+ * memoization (every byte 0–255 maps to one code unit). */
40
+ export function latin1(b) {
41
+ let s = "";
42
+ for (let k = 0; k < b.length; k++)
43
+ s += String.fromCharCode(b[k]);
44
+ return s;
45
+ }
46
+ /** First index ≥ `from` at which `needle` occurs in `hay`, or -1. A short naive
47
+ * scan — used only to locate a result span inside a learnt framing form. */
48
+ export function indexOf(hay, needle, from) {
49
+ if (needle.length === 0)
50
+ return from;
51
+ outer: for (let i = Math.max(0, from); i + needle.length <= hay.length; i++) {
52
+ for (let j = 0; j < needle.length; j++) {
53
+ if (hay[i + j] !== needle[j])
54
+ continue outer;
55
+ }
56
+ return i;
57
+ }
58
+ return -1;
59
+ }
@@ -0,0 +1,114 @@
1
+ export interface AluConfig {
2
+ /** Whether the ALU sub-lib contributes computation rules to the graph search.
3
+ * When false, no operator/operand pre-resolution runs and no ALU rule fires —
4
+ * thinking behaves exactly as it did before ALU existed. */
5
+ enabled: boolean;
6
+ /** Convergence tolerance ε for the numerical limit layer (diff/solve/exp/…). */
7
+ tol: number;
8
+ /** Hard iteration ceiling for any convergence loop, so a non-converging
9
+ * refinement still terminates. */
10
+ maxIter: number;
11
+ /** Decimal places a real result is rounded to before it is encoded to bytes.
12
+ * Determinism here is load-bearing: the search keys an output span by its
13
+ * bytes, so two derivations of the same value must spell it identically. */
14
+ precision: number;
15
+ }
16
+ export interface GeometryConfig {
17
+ /** Maximum siblings per tree fold. */
18
+ maxGroup: number;
19
+ }
20
+ export interface AlphabetConfig {
21
+ /** How different neighbouring byte vectors are from their coarse ancestors
22
+ * (0 = identical, 1 = independent). */
23
+ roughness: number;
24
+ /** Seed XOR mask for the alphabet's PRNG derivation. */
25
+ seedMask: number;
26
+ }
27
+ export interface StoreConfig {
28
+ minHaloMass: number;
29
+ m: number;
30
+ efConstruction: number;
31
+ /** Construction budget for REACH-ONLY interior gists — the ~90% of content
32
+ * index inserts that exist so a partial query can resonate a sub-region
33
+ * and climb, never as dedup targets. Deposit roots and halo-bearing
34
+ * targets always build with the full `efConstruction`. A smaller budget
35
+ * here is the one deliberate speed-for-quality trade in ingestion (an
36
+ * interior's layer-0 wiring is built from a narrower candidate beam);
37
+ * the recall suite (partial recall, multi-topic attention, counterfactual
38
+ * anchoring) is the gate for its value. Set equal to `efConstruction`
39
+ * to disable the trade. */
40
+ efConstructionInterior: number;
41
+ efSearch: number;
42
+ /** Compact the in-memory vector indices after this many vectors are written.
43
+ * Compaction rebuilds an index from its live codes to reclaim the slots left
44
+ * by tombstoned (updated/deleted) halo entries; pacing it on write VOLUME
45
+ * (not on a flush count that goes quiet during repeat-heavy training) keeps
46
+ * the index dense and query cost bounded. */
47
+ compactEveryNWrites: number;
48
+ /** Over-fetch factor for HNSW queries (ANN recall cushion). */
49
+ overfetch: number;
50
+ /** Combined buffered-write ceiling before a flush of both vector indices
51
+ * (content + halo). Higher ⇒ fewer, larger flushes into the in-memory
52
+ * indices and fewer write-transaction commits. */
53
+ batchSize: number;
54
+ /** Max entries in the store's exact-content dedup map (bounds RAM on huge
55
+ * corpora; a miss only risks a duplicate node, never incorrectness). */
56
+ dedupCacheMax: number;
57
+ /** Max bytes of reconstructed content cached in memory (regenerable).
58
+ * Large branch nodes cost more budget than small leaves, so the cache
59
+ * naturally favours cheap, frequently-hit entries. */
60
+ bytesCacheMax: number;
61
+ /** Max bytes of node-record cache (avoids repeated SQLite lookups for
62
+ * shared DAG nodes). Each record is ~30-50 bytes. */
63
+ recCacheBytes: number;
64
+ /** Max bytes of ingest-result cache used by {@link CachedIngest}. */
65
+ ingestCacheBytes: number;
66
+ /** Max bytes of captured-but-not-yet-indexed node gists (D·4 each). A node's
67
+ * gist enters the content index lazily, only when it first becomes a
68
+ * resonance target (gains a continuation edge or a halo); until then its gist
69
+ * waits here. A deposit links/pours a node right after interning it, so the
70
+ * working set is one deposit's nodes — a modest budget captures ~all of it.
71
+ * An eviction only means a node is reached by the structural DAG climb instead
72
+ * of by direct resonance — a little recall reach, never correctness. */
73
+ pendingGistBytes: number;
74
+ /** Max bytes of EXACT halo accumulators kept in memory (D·4 each). The
75
+ * durable halo row is 2-bit quantized; this cache keeps the accumulators a
76
+ * session is actively pouring into at full precision, so within-session
77
+ * accumulate-then-compare (concept formation as it happens) never
78
+ * round-trips through the quantizer. An eviction or a reopen reads the
79
+ * 2-bit row — the fidelity every cross-session consumer already gets. */
80
+ haloCacheBytes: number;
81
+ /** Size, in MiB, of each `rabitq-hnsw` `VectorDatabase`'s memory budget
82
+ * (forwarded as its `cacheSizeMb`). It is the index's SINGLE memory knob: it
83
+ * sizes both SQLite's page cache and the derived immutable-code LRU. A PURE
84
+ * latency optimisation — the index reads codes from SQLite on demand, so its
85
+ * correctness and its asymptotic per-operation storage-read count are identical
86
+ * with the budget at 0. Exposed so a scaling test can set it to 0 and measure
87
+ * the honest, cache-independent cost. */
88
+ vectorCacheMb: number;
89
+ /** Max entries in the skipped-interior LRU set. Interiors that
90
+ * {@link Store.indexSubtree} has already visited (indexed or skipped) are
91
+ * remembered here so subsequent calls prune their subtrees. Session-local
92
+ * (regenerable). */
93
+ coveredIdsMax: number;
94
+ /** Max bytes of transparent-chain runs ({@link Store.chainRun}) cached for
95
+ * the store's lifetime (~4 bytes per chain node). Valid until any write
96
+ * could break a node's transparency (a new structural parent or a new
97
+ * continuation edge), when the whole cache is dropped — writes happen in
98
+ * training bursts, reads in read-only query phases, so the cache pays for
99
+ * itself exactly where it matters. Regenerable; a miss re-walks. */
100
+ chainCacheBytes: number;
101
+ }
102
+ export interface MindConfig {
103
+ seed: number;
104
+ recallQueryK: number;
105
+ haloQueryK: number;
106
+ normalizeEpsilon: number;
107
+ cosineEpsilon: number;
108
+ alu: AluConfig;
109
+ geometry: GeometryConfig;
110
+ alphabet: AlphabetConfig;
111
+ store: StoreConfig;
112
+ }
113
+ export declare const DEFAULT_CONFIG: MindConfig;
114
+ export declare function resolveConfig(opts?: Partial<MindConfig>): MindConfig;
@@ -0,0 +1,96 @@
1
+ // config.ts — the single configuration interface for Sema.
2
+ // Every tunable parameter lives here. Subsystems receive their subset.
3
+ // ── Defaults ──
4
+ export const DEFAULT_CONFIG = {
5
+ seed: 42,
6
+ recallQueryK: 12,
7
+ haloQueryK: 12,
8
+ normalizeEpsilon: 1e-12,
9
+ cosineEpsilon: 1e-12,
10
+ alu: {
11
+ enabled: true,
12
+ tol: 1e-10,
13
+ maxIter: 1000,
14
+ precision: 6,
15
+ },
16
+ geometry: {
17
+ maxGroup: 4,
18
+ },
19
+ alphabet: {
20
+ roughness: 0.65,
21
+ seedMask: 0xa1fa17,
22
+ },
23
+ store: {
24
+ minHaloMass: 1,
25
+ m: 8,
26
+ efConstruction: 64,
27
+ efConstructionInterior: 16,
28
+ efSearch: 64,
29
+ compactEveryNWrites: 50_000,
30
+ overfetch: 4,
31
+ batchSize: 256,
32
+ dedupCacheMax: 1_000_000,
33
+ bytesCacheMax: 20_000_000,
34
+ recCacheBytes: 10_000_000,
35
+ ingestCacheBytes: 50_000_000,
36
+ pendingGistBytes: 16_000_000,
37
+ haloCacheBytes: 16_000_000,
38
+ vectorCacheMb: 64,
39
+ coveredIdsMax: 100_000,
40
+ chainCacheBytes: 16_000_000,
41
+ },
42
+ };
43
+ // ── Config resolver: partial input + defaults = full config ──
44
+ export function resolveConfig(opts = {}) {
45
+ return {
46
+ seed: opts.seed ?? DEFAULT_CONFIG.seed,
47
+ recallQueryK: opts.recallQueryK ?? DEFAULT_CONFIG.recallQueryK,
48
+ haloQueryK: opts.haloQueryK ?? DEFAULT_CONFIG.haloQueryK,
49
+ normalizeEpsilon: opts.normalizeEpsilon ?? DEFAULT_CONFIG.normalizeEpsilon,
50
+ cosineEpsilon: opts.cosineEpsilon ?? DEFAULT_CONFIG.cosineEpsilon,
51
+ alu: {
52
+ enabled: opts.alu?.enabled ?? DEFAULT_CONFIG.alu.enabled,
53
+ tol: opts.alu?.tol ?? DEFAULT_CONFIG.alu.tol,
54
+ maxIter: opts.alu?.maxIter ?? DEFAULT_CONFIG.alu.maxIter,
55
+ precision: opts.alu?.precision ?? DEFAULT_CONFIG.alu.precision,
56
+ },
57
+ geometry: {
58
+ maxGroup: opts.geometry?.maxGroup ?? DEFAULT_CONFIG.geometry.maxGroup,
59
+ },
60
+ alphabet: {
61
+ roughness: opts.alphabet?.roughness ?? DEFAULT_CONFIG.alphabet.roughness,
62
+ seedMask: opts.alphabet?.seedMask ?? DEFAULT_CONFIG.alphabet.seedMask,
63
+ },
64
+ store: {
65
+ minHaloMass: opts.store?.minHaloMass ?? DEFAULT_CONFIG.store.minHaloMass,
66
+ m: opts.store?.m ?? DEFAULT_CONFIG.store.m,
67
+ efConstruction: opts.store?.efConstruction ??
68
+ DEFAULT_CONFIG.store.efConstruction,
69
+ efConstructionInterior: opts.store?.efConstructionInterior ??
70
+ DEFAULT_CONFIG.store.efConstructionInterior,
71
+ efSearch: opts.store?.efSearch ?? DEFAULT_CONFIG.store.efSearch,
72
+ compactEveryNWrites: opts.store?.compactEveryNWrites ??
73
+ DEFAULT_CONFIG.store.compactEveryNWrites,
74
+ overfetch: opts.store?.overfetch ?? DEFAULT_CONFIG.store.overfetch,
75
+ batchSize: opts.store?.batchSize ?? DEFAULT_CONFIG.store.batchSize,
76
+ dedupCacheMax: opts.store?.dedupCacheMax ??
77
+ DEFAULT_CONFIG.store.dedupCacheMax,
78
+ bytesCacheMax: opts.store?.bytesCacheMax ??
79
+ DEFAULT_CONFIG.store.bytesCacheMax,
80
+ recCacheBytes: opts.store?.recCacheBytes ??
81
+ DEFAULT_CONFIG.store.recCacheBytes,
82
+ ingestCacheBytes: opts.store?.ingestCacheBytes ??
83
+ DEFAULT_CONFIG.store.ingestCacheBytes,
84
+ pendingGistBytes: opts.store?.pendingGistBytes ??
85
+ DEFAULT_CONFIG.store.pendingGistBytes,
86
+ haloCacheBytes: opts.store?.haloCacheBytes ??
87
+ DEFAULT_CONFIG.store.haloCacheBytes,
88
+ vectorCacheMb: opts.store?.vectorCacheMb ??
89
+ DEFAULT_CONFIG.store.vectorCacheMb,
90
+ coveredIdsMax: opts.store?.coveredIdsMax ??
91
+ DEFAULT_CONFIG.store.coveredIdsMax,
92
+ chainCacheBytes: opts.store?.chainCacheBytes ??
93
+ DEFAULT_CONFIG.store.chainCacheBytes,
94
+ },
95
+ };
96
+ }
@@ -0,0 +1,125 @@
1
+ /**
2
+ * Knuth's lightest-derivation algorithm with an A* outside bound.
3
+ *
4
+ * A *weighted deduction system* (equivalently an implicit AND-OR hypergraph) is
5
+ * a set of items combined by inference rules
6
+ *
7
+ * premise₁ ∧ … ∧ premiseₖ --localCost--> conclusion
8
+ *
9
+ * where a derivation's cost is the sum of the local costs of the rules used.
10
+ * {@link lightestDerivation} finds a minimum-cost derivation of a goal item.
11
+ * The engine is the Dijkstra-like core of Knuth (1977) — an item's cost is
12
+ * final the moment it is popped — extended with an admissible heuristic so that
13
+ * partial derivations which cannot lead cheaply to the goal are never expanded
14
+ * (A* parsing). It is completely generic: it knows nothing of what items are,
15
+ * only how to canonicalise them, enumerate their rules, score them, and test
16
+ * the goal.
17
+ *
18
+ * The four reductions the search relies on:
19
+ * 1. **Canonical chart memoization** — items are keyed by {@link
20
+ * DeductionSystem.key}; equivalent partial derivations collapse to one
21
+ * chart entry, the cheapest.
22
+ * 2. **Backward demand filtering** — {@link DeductionSystem.rules} only emits
23
+ * rules whose conclusion can still reach the goal, so work unrelated to the
24
+ * goal is never generated.
25
+ * 3. **A* lower-bound pruning** — {@link DeductionSystem.heuristic} keeps the
26
+ * agenda ordered by g + h, so only competitive items are expanded.
27
+ * 4. **Lazy hyperedge generation** — rules (including bridges) are produced by
28
+ * `rules` only when one of their premises is finalised, never up front.
29
+ *
30
+ * Correctness conditions (the caller must uphold these):
31
+ * - Local costs are non-negative (more generally, monotone / superior).
32
+ * - The heuristic never overestimates the remaining cost to a goal
33
+ * (admissible) and is hyperedge-consistent:
34
+ * h(conclusion) ≤ ruleCost + Σ h(premiseᵢ)
35
+ * i.e. relaxing a rule cannot decrease f. The default heuristic (0) is
36
+ * trivially consistent and turns the search into plain Knuth/Dijkstra.
37
+ * - {@link DeductionSystem.key} preserves every part of an item that can
38
+ * affect how it later combines (its "boundary signature"); anything the key
39
+ * drops is asserted to be irrelevant to future composition.
40
+ */
41
+ /**
42
+ * A weighted deduction rule (a hyperedge): the conjunction of `premises`
43
+ * derives `conclusion` at an additional `cost`.
44
+ */
45
+ export interface Rule<I> {
46
+ premises: readonly I[];
47
+ conclusion: I;
48
+ /** Local (edge) cost added on top of the premises' costs. Non-negative. */
49
+ cost: number;
50
+ /** The combinator this rule's firing uses at its conclusion:
51
+ * • `"min"` (default, omitted) — Knuth/A* proper: the conclusion's cost is
52
+ * the CHEAPEST of any rule that reaches it, every other route discarded.
53
+ * The shortest-path monoid (min, +) that makes the search admissible and
54
+ * output-sensitive.
55
+ * • `"sum"` — evidence pooling: EVERY firing of a sum rule contributes its
56
+ * cost to the SAME conclusion (accumulated in {@link
57
+ * DeductionSystem.pool}), instead of competing to be the one cheapest
58
+ * route. The (+, +) monoid a consensus vote needs — several
59
+ * independent premises corroborating one conclusion — kept deliberately
60
+ * OUT of the min-cost chart: a pooled conclusion is never relaxed into
61
+ * `g`, never enters the agenda, and is never itself a premise — it is a
62
+ * terminal aggregate the caller reads out of `pool` once the search is
63
+ * done. */
64
+ combine?: "min" | "sum";
65
+ }
66
+ /** One rule's contribution to a pooled (`combine: "sum"`) conclusion — the
67
+ * firing rule and the already-finalised derivations of its premises, so a
68
+ * caller can render each contribution exactly as it would a min-cost step. */
69
+ export interface PooledContribution<I> {
70
+ rule: Rule<I>;
71
+ premises: Array<Derivation<I>>;
72
+ }
73
+ /** The running aggregate at one pooled conclusion: every sum-mode rule that
74
+ * has fired for it, accumulated. */
75
+ export interface PooledConclusion<I> {
76
+ item: I;
77
+ cost: number;
78
+ contributions: Array<PooledContribution<I>>;
79
+ }
80
+ /** The problem the solver is given: items, rules, a goal, and a heuristic. */
81
+ export interface DeductionSystem<I> {
82
+ /** Canonical key for chart memoization (the item's boundary signature). */
83
+ key(item: I): string;
84
+ /** Axioms: the atomic items and their base costs (the search's seeds). */
85
+ axioms(): Iterable<{
86
+ item: I;
87
+ cost: number;
88
+ }>;
89
+ /**
90
+ * Lazily generate the demanded rules that have `item` among their premises.
91
+ * Called once, when `item` is finalised. `costOf` returns the finalised cost
92
+ * of any item (Infinity if not yet known) — use it for backward-demand /
93
+ * boundary filtering, e.g. drop a rule whose other premises are still open or
94
+ * whose conclusion can no longer beat the best goal.
95
+ */
96
+ rules(item: I, costOf: (other: I) => number): Iterable<Rule<I>>;
97
+ /** Whether `item` satisfies the goal. The first finalised goal wins. */
98
+ isGoal(item: I): boolean;
99
+ /** Admissible, consistent lower bound on the cost from `item` to a goal. */
100
+ heuristic?(item: I): number;
101
+ /** Present only on a system that fires `combine: "sum"` rules — supplied
102
+ * empty, populated in place as the search runs, read back once it returns
103
+ * (typically `null`: a pooling system has no goal to reach, it exhausts its
104
+ * axioms instead — see {@link lightestDerivation}). Absent on every
105
+ * ordinary min-cost system, which is what keeps pooling a zero-cost opt-in:
106
+ * `relax` only takes the pooling branch when a rule declares `combine:
107
+ * "sum"` AND this map is present. */
108
+ pool?: Map<string, PooledConclusion<I>>;
109
+ }
110
+ /** A node of the reconstructed derivation tree. */
111
+ export interface Derivation<I> {
112
+ /** The derived item. */
113
+ item: I;
114
+ /** This item's minimum derivation cost (its g value). */
115
+ cost: number;
116
+ /** The rule that produced it, or null if it is an axiom. */
117
+ rule: Rule<I> | null;
118
+ /** Derivations of the rule's premises (empty for an axiom). */
119
+ premises: Array<Derivation<I>>;
120
+ }
121
+ /**
122
+ * Find a lightest derivation of a goal item, or `null` if none exists.
123
+ * `cost` on the returned root is the total derivation cost.
124
+ */
125
+ export declare function lightestDerivation<I>(system: DeductionSystem<I>): Derivation<I> | null;
@@ -0,0 +1,155 @@
1
+ /**
2
+ * Knuth's lightest-derivation algorithm with an A* outside bound.
3
+ *
4
+ * A *weighted deduction system* (equivalently an implicit AND-OR hypergraph) is
5
+ * a set of items combined by inference rules
6
+ *
7
+ * premise₁ ∧ … ∧ premiseₖ --localCost--> conclusion
8
+ *
9
+ * where a derivation's cost is the sum of the local costs of the rules used.
10
+ * {@link lightestDerivation} finds a minimum-cost derivation of a goal item.
11
+ * The engine is the Dijkstra-like core of Knuth (1977) — an item's cost is
12
+ * final the moment it is popped — extended with an admissible heuristic so that
13
+ * partial derivations which cannot lead cheaply to the goal are never expanded
14
+ * (A* parsing). It is completely generic: it knows nothing of what items are,
15
+ * only how to canonicalise them, enumerate their rules, score them, and test
16
+ * the goal.
17
+ *
18
+ * The four reductions the search relies on:
19
+ * 1. **Canonical chart memoization** — items are keyed by {@link
20
+ * DeductionSystem.key}; equivalent partial derivations collapse to one
21
+ * chart entry, the cheapest.
22
+ * 2. **Backward demand filtering** — {@link DeductionSystem.rules} only emits
23
+ * rules whose conclusion can still reach the goal, so work unrelated to the
24
+ * goal is never generated.
25
+ * 3. **A* lower-bound pruning** — {@link DeductionSystem.heuristic} keeps the
26
+ * agenda ordered by g + h, so only competitive items are expanded.
27
+ * 4. **Lazy hyperedge generation** — rules (including bridges) are produced by
28
+ * `rules` only when one of their premises is finalised, never up front.
29
+ *
30
+ * Correctness conditions (the caller must uphold these):
31
+ * - Local costs are non-negative (more generally, monotone / superior).
32
+ * - The heuristic never overestimates the remaining cost to a goal
33
+ * (admissible) and is hyperedge-consistent:
34
+ * h(conclusion) ≤ ruleCost + Σ h(premiseᵢ)
35
+ * i.e. relaxing a rule cannot decrease f. The default heuristic (0) is
36
+ * trivially consistent and turns the search into plain Knuth/Dijkstra.
37
+ * - {@link DeductionSystem.key} preserves every part of an item that can
38
+ * affect how it later combines (its "boundary signature"); anything the key
39
+ * drops is asserted to be irrelevant to future composition.
40
+ */
41
+ import { MinHeap } from "./priority-queue.js";
42
+ /**
43
+ * Find a lightest derivation of a goal item, or `null` if none exists.
44
+ * `cost` on the returned root is the total derivation cost.
45
+ */
46
+ export function lightestDerivation(system) {
47
+ const g = new Map(); // best known cost per item
48
+ const proof = new Map(); // producing rule per item
49
+ const items = new Map(); // key → the item it stands for
50
+ const hCache = new Map();
51
+ const agenda = new MinHeap();
52
+ const heuristic = system.heuristic;
53
+ const h = (item, key) => {
54
+ if (!heuristic)
55
+ return 0;
56
+ let v = hCache.get(key);
57
+ if (v === undefined) {
58
+ v = heuristic(item);
59
+ hCache.set(key, v);
60
+ }
61
+ return v;
62
+ };
63
+ const costOf = (item) => g.get(system.key(item)) ?? Infinity;
64
+ const relax = (item, cost, rule) => {
65
+ const key = system.key(item);
66
+ if (rule?.combine === "sum" && system.pool) {
67
+ // Evidence pooling: accumulate this firing rather than compete for the
68
+ // cheapest — see {@link Rule.combine}. The premises are already
69
+ // finalised (the caller only relaxes a rule once every premise's cost
70
+ // is known), so their derivations can be read back immediately.
71
+ const premises = rule.premises.map((p) => reconstruct(p, system, g, proof));
72
+ const prior = system.pool.get(key);
73
+ system.pool.set(key, {
74
+ item,
75
+ cost: (prior?.cost ?? 0) + cost,
76
+ contributions: [...(prior?.contributions ?? []), { rule, premises }],
77
+ });
78
+ return;
79
+ }
80
+ const current = g.get(key);
81
+ if (current === undefined || cost < current) {
82
+ g.set(key, cost);
83
+ proof.set(key, rule);
84
+ items.set(key, item);
85
+ agenda.push(cost + h(item, key), { key, g: cost });
86
+ }
87
+ };
88
+ for (const { item, cost } of system.axioms())
89
+ relax(item, cost, null);
90
+ while (agenda.size > 0) {
91
+ const { value } = agenda.pop();
92
+ const key = value.key;
93
+ // Lazy deletion: an entry is stale if a cheaper derivation has since been
94
+ // recorded for the same item.
95
+ if (value.g !== g.get(key))
96
+ continue;
97
+ const item = items.get(key);
98
+ if (system.isGoal(item)) {
99
+ return reconstruct(item, system, g, proof);
100
+ }
101
+ for (const rule of system.rules(item, costOf)) {
102
+ let sum = rule.cost;
103
+ let ready = true;
104
+ for (const p of rule.premises) {
105
+ const pc = g.get(system.key(p));
106
+ if (pc === undefined) {
107
+ ready = false;
108
+ break;
109
+ }
110
+ sum += pc;
111
+ }
112
+ if (ready)
113
+ relax(rule.conclusion, sum, rule);
114
+ }
115
+ }
116
+ return null;
117
+ }
118
+ function reconstruct(item, system, g, proof) {
119
+ // Iterative post-order over the derivation hypergraph. In the rewrite
120
+ // search every rule has one premise, so the derivation is a chain whose
121
+ // length equals the number of frontier edges — which, with long inputs,
122
+ // can exceed the call stack. Multi-premise rules (the test-suite bridge
123
+ // case) are handled by the same explicit stack.
124
+ const done = new Map();
125
+ const stack = [item];
126
+ while (stack.length > 0) {
127
+ const cur = stack[stack.length - 1]; // peek
128
+ const key = system.key(cur);
129
+ if (done.has(key)) {
130
+ stack.pop();
131
+ continue;
132
+ }
133
+ const rule = proof.get(key) ?? null;
134
+ const premises = rule?.premises ?? [];
135
+ // Push any unresolved premises (rightmost first → leftmost resolves first).
136
+ let pending = false;
137
+ for (let i = premises.length - 1; i >= 0; i--) {
138
+ if (!done.has(system.key(premises[i]))) {
139
+ stack.push(premises[i]);
140
+ pending = true;
141
+ }
142
+ }
143
+ if (!pending) {
144
+ stack.pop(); // this item
145
+ const kids = premises.map((p) => done.get(system.key(p)));
146
+ done.set(key, {
147
+ item: cur,
148
+ cost: g.get(key),
149
+ rule,
150
+ premises: kids,
151
+ });
152
+ }
153
+ }
154
+ return done.get(system.key(item));
155
+ }
@@ -0,0 +1,7 @@
1
+ export { lightestDerivation } from "./deduction.js";
2
+ export type { DeductionSystem, Derivation, PooledConclusion, PooledContribution, Rule, } from "./deduction.js";
3
+ export { coverSequence } from "./rewrite.js";
4
+ export type { CandidateSpan, Cover } from "./rewrite.js";
5
+ export { Trie } from "./trie.js";
6
+ export type { Match } from "./trie.js";
7
+ export { MinHeap } from "./priority-queue.js";
@@ -0,0 +1,11 @@
1
+ // derive — A* lightest-derivation rewrite search.
2
+ //
3
+ // A small, self-contained library for finding minimum-cost derivations in a
4
+ // weighted deduction system (an implicit AND-OR hypergraph), with on-demand
5
+ // (lazy) rule generation and an admissible A* outside bound. It has no
6
+ // dependency on the rest of the codebase and is reusable for any
7
+ // symbolic-rewriting mechanism. See ./../README.md for the design.
8
+ export { lightestDerivation } from "./deduction.js";
9
+ export { coverSequence } from "./rewrite.js";
10
+ export { Trie } from "./trie.js";
11
+ export { MinHeap } from "./priority-queue.js";
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Binary min-heap keyed by a numeric priority, carrying an arbitrary payload.
3
+ *
4
+ * It is the agenda of the lightest-derivation search, where the priority is the
5
+ * estimate f = g + h. Stale entries are tolerated by the consumer (lazy
6
+ * deletion), so there is no decrease-key: when an item's cost improves it is
7
+ * simply pushed again, and the older, higher-priority copy is recognised as
8
+ * stale and discarded when it surfaces. Parallel arrays (rather than an array
9
+ * of objects) keep the hot path allocation-free.
10
+ */
11
+ export declare class MinHeap<T> {
12
+ private readonly keys;
13
+ private readonly vals;
14
+ get size(): number;
15
+ push(priority: number, value: T): void;
16
+ pop(): {
17
+ priority: number;
18
+ value: T;
19
+ } | undefined;
20
+ }