gdp-wired 0.47.0 → 0.48.0

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 (170) hide show
  1. package/README.md +15 -4
  2. package/dist/core/carry.d.ts +15 -0
  3. package/dist/core/carry.js +651 -0
  4. package/dist/core/cli.d.ts +3 -1
  5. package/dist/core/cli.js +28 -8
  6. package/dist/core/count-cases.d.ts +24 -0
  7. package/dist/core/count-cases.js +320 -0
  8. package/dist/core/count-cert-grow.d.ts +4 -0
  9. package/dist/core/count-cert-grow.js +37 -0
  10. package/dist/core/count-cert-transport.d.ts +3 -0
  11. package/dist/core/count-cert-transport.js +58 -0
  12. package/dist/core/count-completion.d.ts +9 -0
  13. package/dist/core/count-completion.js +27 -0
  14. package/dist/core/count-cubes.d.ts +8 -0
  15. package/dist/core/count-cubes.js +69 -0
  16. package/dist/core/count-demand.d.ts +37 -0
  17. package/dist/core/count-demand.js +361 -0
  18. package/dist/core/count-facts.d.ts +52 -0
  19. package/dist/core/count-facts.js +247 -0
  20. package/dist/core/count-focus-proof.d.ts +8 -0
  21. package/dist/core/count-focus-proof.js +140 -0
  22. package/dist/core/count-focused.d.ts +22 -0
  23. package/dist/core/count-focused.js +676 -0
  24. package/dist/core/count-ranges.d.ts +5 -0
  25. package/dist/core/count-ranges.js +65 -0
  26. package/dist/core/count-recovery/action-guard.d.ts +5 -0
  27. package/dist/core/count-recovery/action-guard.js +51 -0
  28. package/dist/core/count-recovery/boolean-core.d.ts +16 -0
  29. package/dist/core/count-recovery/boolean-core.js +258 -0
  30. package/dist/core/count-recovery/carried-case.d.ts +2 -0
  31. package/dist/core/count-recovery/carried-case.js +24 -0
  32. package/dist/core/count-recovery/conditional-check-cache.d.ts +23 -0
  33. package/dist/core/count-recovery/conditional-check-cache.js +130 -0
  34. package/dist/core/count-recovery/conditional-cuts.d.ts +6 -0
  35. package/dist/core/count-recovery/conditional-cuts.js +318 -0
  36. package/dist/core/count-recovery/conditional-proof.d.ts +19 -0
  37. package/dist/core/count-recovery/conditional-proof.js +306 -0
  38. package/dist/core/count-recovery/derivability-closure.d.ts +14 -0
  39. package/dist/core/count-recovery/derivability-closure.js +533 -0
  40. package/dist/core/count-recovery/empty-count-source.d.ts +2 -0
  41. package/dist/core/count-recovery/empty-count-source.js +40 -0
  42. package/dist/core/count-recovery/field-bound-lemmas.d.ts +23 -0
  43. package/dist/core/count-recovery/field-bound-lemmas.js +640 -0
  44. package/dist/core/count-recovery/field-case.d.ts +5 -0
  45. package/dist/core/count-recovery/field-case.js +89 -0
  46. package/dist/core/count-recovery/field-demand.d.ts +46 -0
  47. package/dist/core/count-recovery/field-demand.js +47 -0
  48. package/dist/core/count-recovery/field-only.d.ts +3 -0
  49. package/dist/core/count-recovery/field-only.js +18 -0
  50. package/dist/core/count-recovery/fold-path.d.ts +6 -0
  51. package/dist/core/count-recovery/fold-path.js +35 -0
  52. package/dist/core/count-recovery/frame-lemmas.d.ts +9 -0
  53. package/dist/core/count-recovery/frame-lemmas.js +120 -0
  54. package/dist/core/count-recovery/guard-cases.d.ts +12 -0
  55. package/dist/core/count-recovery/guard-cases.js +138 -0
  56. package/dist/core/count-recovery/key-closure.d.ts +14 -0
  57. package/dist/core/count-recovery/key-closure.js +62 -0
  58. package/dist/core/count-recovery/leaf-allowance.d.ts +4 -0
  59. package/dist/core/count-recovery/leaf-allowance.js +7 -0
  60. package/dist/core/count-recovery/lift-count-proxies.d.ts +6 -0
  61. package/dist/core/count-recovery/lift-count-proxies.js +175 -0
  62. package/dist/core/count-recovery/live-dependencies.d.ts +6 -0
  63. package/dist/core/count-recovery/live-dependencies.js +76 -0
  64. package/dist/core/count-recovery/member-inclusions.d.ts +11 -0
  65. package/dist/core/count-recovery/member-inclusions.js +168 -0
  66. package/dist/core/count-recovery/member-premises.d.ts +29 -0
  67. package/dist/core/count-recovery/member-premises.js +114 -0
  68. package/dist/core/count-recovery/natural-cases.d.ts +6 -0
  69. package/dist/core/count-recovery/natural-cases.js +59 -0
  70. package/dist/core/count-recovery/ops.d.ts +6 -0
  71. package/dist/core/count-recovery/ops.js +8 -0
  72. package/dist/core/count-recovery/pre-state-source.d.ts +8 -0
  73. package/dist/core/count-recovery/pre-state-source.js +18 -0
  74. package/dist/core/count-recovery/rebase-path.d.ts +9 -0
  75. package/dist/core/count-recovery/rebase-path.js +70 -0
  76. package/dist/core/count-recovery/rename-proof.d.ts +2 -0
  77. package/dist/core/count-recovery/rename-proof.js +102 -0
  78. package/dist/core/count-recovery/row-cases.d.ts +16 -0
  79. package/dist/core/count-recovery/row-cases.js +291 -0
  80. package/dist/core/count-recovery/scalar-guards.d.ts +7 -0
  81. package/dist/core/count-recovery/scalar-guards.js +291 -0
  82. package/dist/core/count-recovery/source-memo.d.ts +12 -0
  83. package/dist/core/count-recovery/source-memo.js +67 -0
  84. package/dist/core/count-select.d.ts +16 -0
  85. package/dist/core/count-select.js +24 -0
  86. package/dist/core/count-support.d.ts +598 -0
  87. package/dist/core/count-support.js +2080 -0
  88. package/dist/core/count-unions.d.ts +2 -0
  89. package/dist/core/count-unions.js +139 -0
  90. package/dist/core/count-witness-flags.d.ts +9 -0
  91. package/dist/core/count-witness-flags.js +37 -0
  92. package/dist/core/count-write-frames.d.ts +12 -0
  93. package/dist/core/count-write-frames.js +206 -0
  94. package/dist/core/counts.d.ts +20 -2
  95. package/dist/core/counts.js +807 -156
  96. package/dist/core/dsl.d.ts +50 -6
  97. package/dist/core/dsl.js +307 -65
  98. package/dist/core/effect.js +8 -1
  99. package/dist/core/exists.d.ts +1 -1
  100. package/dist/core/exists.js +80 -15
  101. package/dist/core/extract-check.d.ts +7 -1
  102. package/dist/core/extract-check.js +130 -13
  103. package/dist/core/extract-read.d.ts +22 -8
  104. package/dist/core/extract-read.js +29 -11
  105. package/dist/core/extract-shapes.js +122 -41
  106. package/dist/core/extract-standalone.js +53 -9
  107. package/dist/core/extract.js +9 -3
  108. package/dist/core/field-laws.d.ts +2 -0
  109. package/dist/core/field-laws.js +100 -0
  110. package/dist/core/finite-frontiers.d.ts +5 -0
  111. package/dist/core/finite-frontiers.js +116 -0
  112. package/dist/core/finite-houdini.d.ts +52 -0
  113. package/dist/core/finite-houdini.js +460 -0
  114. package/dist/core/finite-model.d.ts +13 -0
  115. package/dist/core/finite-model.js +68 -0
  116. package/dist/core/finite-tiny.d.ts +72 -0
  117. package/dist/core/finite-tiny.js +1052 -0
  118. package/dist/core/infer.d.ts +1 -1
  119. package/dist/core/infer.js +32 -9
  120. package/dist/core/laws.d.ts +2 -1
  121. package/dist/core/laws.js +47 -14
  122. package/dist/core/lemma-cuts.d.ts +38 -0
  123. package/dist/core/lemma-cuts.js +365 -0
  124. package/dist/core/machine.d.ts +39 -2
  125. package/dist/core/machine.js +240 -35
  126. package/dist/core/mutate.d.ts +55 -0
  127. package/dist/core/mutate.js +210 -0
  128. package/dist/core/native-domain.d.ts +6 -0
  129. package/dist/core/native-domain.js +114 -0
  130. package/dist/core/native-law.d.ts +3 -0
  131. package/dist/core/native-law.js +174 -0
  132. package/dist/core/quorum-laws.d.ts +5 -0
  133. package/dist/core/quorum-laws.js +239 -0
  134. package/dist/core/relational.js +10 -2
  135. package/dist/core/relevance.d.ts +4 -0
  136. package/dist/core/relevance.js +79 -0
  137. package/dist/core/runtime-wall.d.ts +3 -0
  138. package/dist/core/runtime-wall.js +4 -0
  139. package/dist/core/sample-step.d.ts +16 -0
  140. package/dist/core/sample-step.js +28 -0
  141. package/dist/core/source-check.d.ts +44 -0
  142. package/dist/core/source-check.js +126 -0
  143. package/dist/core/source-diagnostic.d.ts +1 -0
  144. package/dist/core/source-diagnostic.js +34 -0
  145. package/dist/core/support-budget.d.ts +2 -0
  146. package/dist/core/support-budget.js +6 -0
  147. package/dist/core/system.js +2 -0
  148. package/dist/core/wall.d.ts +6 -0
  149. package/dist/core/wall.js +23 -0
  150. package/dist/core/watch.js +4 -0
  151. package/dist/kernel/cdcl.d.ts +4 -0
  152. package/dist/kernel/cdcl.js +20 -5
  153. package/dist/kernel/kernel.d.ts +10 -0
  154. package/dist/kernel/kernel.js +56 -3
  155. package/dist/kernel/model-sequence.d.ts +2 -2
  156. package/dist/kernel/model-sequence.js +4 -3
  157. package/dist/kernel/model-values.d.ts +2 -0
  158. package/dist/kernel/model-values.js +60 -0
  159. package/dist/kernel/prove.d.ts +10 -0
  160. package/dist/kernel/prove.js +56 -8
  161. package/dist/kernel/prover-rows.d.ts +29 -0
  162. package/dist/kernel/prover-rows.js +114 -0
  163. package/dist/lean-kernel/emit.d.ts +39 -2
  164. package/dist/lean-kernel/emit.js +136 -24
  165. package/dist/lean-kernel/lean-terms.d.ts +1 -0
  166. package/dist/lean-kernel/lean-terms.js +13 -5
  167. package/dist/lean-kernel/library.js +4 -0
  168. package/dist/lean-kernel/qalpha.d.ts +7 -0
  169. package/dist/lean-kernel/qalpha.js +40 -0
  170. package/package.json +5 -1
@@ -0,0 +1,651 @@
1
+ // Syntax-derived proposals only. Neither a copied guard nor a co-write is a trusted
2
+ // transition fact: counts.ts applies its ordinary scope/sample filter and kernel proof.
3
+ import { all, any, not, same, less, n, x, walk } from "./machine.js";
4
+ import { scope, substC, substE } from "../kernel/kernel.js";
5
+ // Shared with comparisons(): keep its existing IF-leaf / truncated-subtraction behavior.
6
+ export function storedInputs(m) {
7
+ const out = new Map();
8
+ const leaves = (e, to) => { if (e.op === 'var') {
9
+ if (m.inputs.includes(e.name) && !out.has(e.name))
10
+ out.set(e.name, to);
11
+ }
12
+ else if (e.op === 'if') {
13
+ leaves(e.then, to);
14
+ leaves(e.else, to);
15
+ } };
16
+ for (const [v, e] of Object.entries(m.step.state ?? {}))
17
+ leaves(e.op === 'sub' ? e.a : e, v);
18
+ return out;
19
+ }
20
+ const json = (v) => JSON.stringify(v);
21
+ const eq = (a, b) => json(a) === json(b);
22
+ const yes = same(n(0), n(0)), no = not(yes);
23
+ const vars = (t) => { const out = new Set(); walk(t, o => { if (o.op === 'var' && typeof o.name === 'string')
24
+ out.add(o.name); }); return [...out]; };
25
+ const flat = (c, op) => c.op === op ? c.xs.flatMap(q => flat(q, op)) : [c];
26
+ // Partial evaluation at an entry guard also recognizes the DSL's Boolean-map encoding.
27
+ function simplify(c, at = yes, value = true) {
28
+ if (eq(c, at))
29
+ return value ? yes : no;
30
+ if (c.op === 'not') {
31
+ const q = simplify(c.x, at, value);
32
+ return eq(q, yes) ? no : eq(q, no) ? yes : q.op === 'not' ? q.x : not(q);
33
+ }
34
+ if (c.op === 'and' || c.op === 'or') {
35
+ const unit = c.op === 'and' ? yes : no, zero = c.op === 'and' ? no : yes;
36
+ const qs = flat(c, c.op).map(q => simplify(q, at, value));
37
+ if (qs.some(q => eq(q, zero)) || qs.some(q => qs.some(r => eq(q, not(r)))))
38
+ return zero;
39
+ const xs = [...new Map(qs.filter(q => !eq(q, unit)).map(q => [json(q), q])).values()];
40
+ return xs.length === 0 ? unit : xs.length === 1 ? xs[0] : { op: c.op, xs };
41
+ }
42
+ if ((c.op === '=' || c.op === '<' || c.op === '<=') && c.a.op === 'const' && c.b.op === 'const')
43
+ return (c.op === '=' ? c.a.value === c.b.value : c.op === '<' ? c.a.value < c.b.value : c.a.value <= c.b.value) ? yes : no;
44
+ return c;
45
+ }
46
+ function atGuard(e, guard) {
47
+ if (e.op !== 'if')
48
+ return e;
49
+ const c = simplify(e.c, guard), a = atGuard(e.then, guard), b = atGuard(e.else, guard);
50
+ return eq(c, yes) ? a : eq(c, no) ? b : { op: 'if', c, then: a, else: b };
51
+ }
52
+ function positive(c) {
53
+ // The cube engine writes 0 + 1 <= 0 + cell; compare that same atom, not its spelling.
54
+ const bare = (e) => e.op === 'add' && eq(e.a, n(0)) ? bare(e.b) : e.op === 'add' && eq(e.b, n(0)) ? bare(e.a) : e;
55
+ if (c.op === '<=') {
56
+ const a = bare(c.a), b = bare(c.b);
57
+ if (eq(a, n(1)) && b.op === 'get')
58
+ return b;
59
+ }
60
+ if (c.op === '=') {
61
+ const a = bare(c.a), b = bare(c.b);
62
+ if (eq(b, n(1)) && a.op === 'get')
63
+ return a;
64
+ }
65
+ if (c.op === 'not' && c.x.op === '=') {
66
+ const a = bare(c.x.a), b = bare(c.x.b);
67
+ if (eq(b, n(0)) && a.op === 'get')
68
+ return a;
69
+ }
70
+ return undefined;
71
+ }
72
+ // Small CNF expansion, bounded independently of proof/search budgets; incomplete is fine.
73
+ function clauses(c, flags, bound, depth = 0, fuel = { left: 4096 }) {
74
+ if (depth > 24 || --fuel.left < 0)
75
+ return [];
76
+ if (c.op === 'flag') {
77
+ const q = flags[c.name];
78
+ return q ? clauses(q, flags, bound, depth + 1, fuel) : [[c]];
79
+ }
80
+ if (c.op === 'all') {
81
+ for (const k of [c.key, ...(c.keys ?? [])])
82
+ bound.add(k);
83
+ return clauses(c.c, flags, bound, depth + 1, fuel);
84
+ }
85
+ if (c.op === 'not' && c.x.op === 'flag') {
86
+ const q = flags[c.x.name];
87
+ return q && q.op !== 'all' ? clauses(not(q), flags, bound, depth + 1, fuel) : [[c]];
88
+ }
89
+ if (c.op === 'not' && c.x.op === 'not')
90
+ return clauses(c.x.x, flags, bound, depth + 1, fuel);
91
+ if (c.op === 'not' && (c.x.op === 'and' || c.x.op === 'or'))
92
+ return clauses({ op: c.x.op === 'and' ? 'or' : 'and', xs: c.x.xs.map(not) }, flags, bound, depth + 1, fuel);
93
+ if (c.op === 'and') {
94
+ const out = [];
95
+ for (const q of c.xs) {
96
+ if (out.length >= 64 || fuel.left <= 0)
97
+ break;
98
+ out.push(...clauses(q, flags, bound, depth + 1, fuel).slice(0, 64 - out.length));
99
+ }
100
+ return out;
101
+ }
102
+ if (c.op === 'or') {
103
+ let out = [[]];
104
+ for (const q of c.xs) {
105
+ const cs = clauses(q, flags, bound, depth + 1, fuel);
106
+ if (!cs.length || out.length * cs.length > 64)
107
+ return [];
108
+ out = out.flatMap(a => cs.map(b => [...a, ...b]));
109
+ }
110
+ return out;
111
+ }
112
+ return [[c]];
113
+ }
114
+ // Linear packed-key matching: match by coefficient, not association or variable names.
115
+ function linear(e, k = 1, out = new Map()) {
116
+ if (e.op === 'var') {
117
+ out.set(e.name, (out.get(e.name) ?? 0) + k);
118
+ return out;
119
+ }
120
+ if (e.op === 'const')
121
+ return e.value === 0 ? out : undefined;
122
+ if (e.op === 'mul')
123
+ return linear(e.a, k * e.k, out);
124
+ return e.op === 'add' && linear(e.a, k, out) ? linear(e.b, k, out) : undefined;
125
+ }
126
+ function match(pattern, actual) {
127
+ const p = linear(pattern), a = linear(actual);
128
+ if (!p || !a || p.size !== a.size || new Set(p.values()).size !== p.size || new Set(a.values()).size !== a.size)
129
+ return;
130
+ const out = {};
131
+ for (const [v, k] of p) {
132
+ const w = [...a].find(([, j]) => j === k);
133
+ if (!w)
134
+ return;
135
+ out[v] = x(w[0]);
136
+ }
137
+ return out;
138
+ }
139
+ export function carryCandidates(m, occupied = [], audit = { rows: [], work: 0, exhausted: false }) {
140
+ const flags = m.step.flags ?? {}, copies = storedInputs(m), out = new Map();
141
+ // Bound unsuccessful generation as well as output; local to this invocation.
142
+ let work = 0;
143
+ const more = () => { if (work >= 8192) {
144
+ audit.exhausted = true;
145
+ return false;
146
+ } audit.work = ++work; return out.size < 128; };
147
+ const taken = new Set([...m.params, ...m.inputs, ...Object.keys(m.state), ...Object.keys(m.maps ?? {}), ...(m.inputMaps ?? [])]);
148
+ walk(m, o => { if (typeof o.name === 'string')
149
+ taken.add(o.name); if (typeof o.key === 'string')
150
+ taken.add(o.key); });
151
+ const fresh = () => { let s = 'carryKey'; while (taken.has(s))
152
+ s += 'X'; taken.add(s); return s; };
153
+ const add = (holds, bound, family) => {
154
+ if (out.size >= 128)
155
+ return; // proposal cap only; never a proof/search budget
156
+ const ks = vars(holds).filter(v => bound.has(v)), signature = json([ks, holds]);
157
+ let h = 14695981039346656037n;
158
+ for (let i = 0; i < signature.length; i++)
159
+ h = BigInt.asUintN(64, (h ^ BigInt(signature.charCodeAt(i))) * 1099511628211n);
160
+ const name = `carry:${family}:${h.toString(16)}`;
161
+ const l = { name, holds, ...(ks.length ? { forall: ks[0], ...(ks.length > 1 ? { keys: ks.slice(1) } : {}) } : {}) };
162
+ if (occupied.some(l => l.name === name))
163
+ return;
164
+ try {
165
+ scope(m, [l]);
166
+ }
167
+ catch {
168
+ return;
169
+ }
170
+ const prev = out.get(name);
171
+ if (prev && !eq(prev, l)) {
172
+ out.delete(name);
173
+ return;
174
+ }
175
+ out.set(name, l);
176
+ };
177
+ const bridges = [];
178
+ bridges: for (const law of occupied)
179
+ for (const row of clauses(law.holds, {}, new Set())) {
180
+ if (!more())
181
+ return [...out.values()];
182
+ for (const a of row)
183
+ if (a.op === 'not') {
184
+ const from = positive(a.x);
185
+ if (from)
186
+ for (const b of row) {
187
+ const to = positive(b);
188
+ if (to && to.map !== from.map && bridges.length < 64) {
189
+ bridges.push({ from, to });
190
+ if (bridges.length === 64)
191
+ break bridges;
192
+ }
193
+ }
194
+ }
195
+ }
196
+ for (const [mode, w] of Object.entries(m.step.state ?? {})) {
197
+ if (!more())
198
+ return [...out.values()];
199
+ if (w.op !== 'if' || w.then.op !== 'const')
200
+ continue;
201
+ const sub = { vars: {}, maps: {}, flags: {} };
202
+ for (const [input, target] of copies) {
203
+ const e = m.step.state?.[target];
204
+ if (e?.op === 'if' && eq(e.c, w.c) && eq(e.then, x(input)))
205
+ sub.vars[input] = x(target);
206
+ }
207
+ if (!Object.keys(sub.vars).length)
208
+ continue;
209
+ for (const [target, update] of Object.entries(m.step.maps ?? {}))
210
+ if ('each' in update) {
211
+ const e = atGuard(update.value, w.c), cell = e.op === 'get' ? e : e.op === 'if' && eq(e.then, n(1)) && eq(e.else, n(0)) ? positive(e.c) : undefined;
212
+ if (cell && m.inputMaps?.includes(cell.map) && eq(cell.key, x(update.each)))
213
+ sub.maps[cell.map] = key => ({ op: 'get', map: target, key });
214
+ }
215
+ const bound = new Set(), active = same(x(mode), w.then);
216
+ const source = clauses(all(w.c, ...(m.assume ? [m.assume] : [])), flags, bound);
217
+ for (const row of source) {
218
+ if (!more())
219
+ return [...out.values()];
220
+ const variants = [row];
221
+ for (let i = 0; i < row.length; i++) {
222
+ const c = positive(row[i]);
223
+ if (!c)
224
+ continue;
225
+ for (const b of bridges)
226
+ if (c.map === b.from.map) {
227
+ if (!more())
228
+ return [...out.values()];
229
+ const vs = match(b.from.key, c.key);
230
+ if (vs)
231
+ variants.push(row.map((q, j) => j === i ? { op: '<=', a: n(1), b: substE(b.to, { vars: vs, maps: {}, flags: {} }) } : q));
232
+ }
233
+ }
234
+ for (const rs of variants) {
235
+ if (!more())
236
+ return [...out.values()];
237
+ const raw = any(...rs), names = vars(raw);
238
+ // The entry's old control state is not the new stored state. Unretained inputs
239
+ // make this whole CNF clause unavailable; never erase a disjunct to strengthen it.
240
+ if (names.some(v => v in m.state) || !names.some(v => v in sub.vars) && !Object.keys(sub.maps).some(mp => json(raw).includes(`"map":${json(mp)}`)))
241
+ continue;
242
+ if (rs.some(q => q.op === 'flag' || q.op === 'not' && q.x.op === 'flag'))
243
+ continue;
244
+ const c = substC(raw, sub);
245
+ add(any(not(active), c), bound, 'guard');
246
+ }
247
+ }
248
+ }
249
+ // Co-written monotone point and interval maps. Keep the source interval and input
250
+ // bounds; no protocol names, guessed domain constants, or invariant seeds.
251
+ const inserts = (map, key, value) => {
252
+ if (value.op !== 'if' || !eq(value.then, n(1)) || !eq(value.else, n(0)))
253
+ return [];
254
+ const c = simplify(value.c), branches = clauses(not(c), {}, new Set()).map(row => row.map(q => simplify(not(q))));
255
+ return branches.filter(row => !row.some(q => json(q).includes(json({ op: 'get', map, key }))));
256
+ };
257
+ for (const [mp, point] of Object.entries(m.step.maps ?? {})) {
258
+ if (!more())
259
+ return [...out.values()];
260
+ if ('each' in point)
261
+ continue;
262
+ const ps = vars(point.key);
263
+ if (!ps.length || ps.some(v => !m.inputs.includes(v)))
264
+ continue;
265
+ for (const g of inserts(mp, point.key, point.value))
266
+ for (const [other, interval] of Object.entries(m.step.maps ?? {})) {
267
+ if (!more())
268
+ return [...out.values()];
269
+ if (!('each' in interval))
270
+ continue;
271
+ for (const row of inserts(other, x(interval.each), interval.value)) {
272
+ if (!more())
273
+ return [...out.values()];
274
+ if (!g.every(q => row.some(r => eq(q, r))))
275
+ continue;
276
+ const rest = row.filter(q => !g.some(r => eq(q, r)));
277
+ const lo = rest.find((q) => q.op === '<=' && eq(q.b, x(interval.each)));
278
+ const hi = rest.find((q) => q.op === '<' && eq(q.a, x(interval.each)));
279
+ if (!lo || !hi || rest.length !== 2 || hi.b.op !== 'add' || !eq(hi.b.a, lo.a))
280
+ continue;
281
+ const z = fresh(), names = ps.map(() => fresh()), vs = Object.fromEntries(ps.map((p, i) => [p, x(names[i])])), bound = new Set([...names, z]);
282
+ const s = { vars: { ...vs, [interval.each]: { op: 'add', a: lo.a, b: x(z) } }, maps: {}, flags: {} };
283
+ const domains = m.assume ? clauses(m.assume, {}, new Set()).filter(r => vars(r).every(v => ps.includes(v) || m.params.includes(v))).map(r => any(...r)) : [];
284
+ const hyp = all(...domains, { op: '<', a: x(z), b: hi.b.b }, { op: '<=', a: n(1), b: { op: 'get', map: mp, key: point.key } });
285
+ // Substitute the interval key before the point variables (substitution is simultaneous).
286
+ const cell = substE(substE({ op: 'get', map: other, key: x(interval.each) }, s), { vars: vs, maps: {}, flags: {} });
287
+ add(any(not(substC(hyp, { vars: vs, maps: {}, flags: {} })), { op: '<=', a: n(1), b: cell }), bound, 'co-write');
288
+ }
289
+ }
290
+ }
291
+ // New rules are appended; existing guarded/co-write candidates and their order survive.
292
+ // Only unconditional transition input bounds are used. m.pre is param-only by scope.
293
+ const pre = flat(m.pre ?? yes, 'and'), transition = flat(m.assume ?? yes, 'and');
294
+ const paramOnly = (e) => vars(e).every(v => m.params.includes(v)) && !json(e).includes('"op":"get"') && !json(e).includes('"op":"flag"');
295
+ const bounds = transition.filter((c) => c.op === '<' && c.a.op === 'var' && m.inputs.includes(c.a.name) && paramOnly(c.b));
296
+ for (const [target, update] of Object.entries(m.step.state ?? {}))
297
+ for (const b of bounds) {
298
+ if (!more())
299
+ return [...out.values()];
300
+ const input = b.a.name, bound = b.b, allowedInputs = new Set(bounds.filter(q => eq(q.b, bound)).map(q => q.a.name));
301
+ const safe = (e) => boundKnown(e, bound, pre);
302
+ let copied = false;
303
+ const leaves = (e, depth = 0) => { if (!more() || depth > 48)
304
+ return false; if (e.op === 'if')
305
+ return leaves(e.then, depth + 1) && leaves(e.else, depth + 1); if (e.op === 'var' && e.name === target)
306
+ return true; if (e.op === 'var' && allowedInputs.has(e.name)) {
307
+ copied = true;
308
+ return true;
309
+ } return safe(e); };
310
+ if (!safe(m.state[target])) {
311
+ audit.rows.push({ rule: 'range', target, input, accepted: false, reason: 'initial bound not established' });
312
+ continue;
313
+ }
314
+ const accepted = leaves(update) && copied;
315
+ audit.rows.push({ rule: 'range', target, input, accepted, reason: accepted ? 'all leaves preserve exact source bound' : 'unsafe or unbounded update leaf' });
316
+ if (accepted)
317
+ add({ op: '<', a: x(target), b: bound }, new Set(), 'global-range');
318
+ }
319
+ const guarded = [...out.values()];
320
+ for (const l of guarded) {
321
+ if (!more())
322
+ return [...out.values()];
323
+ if (l.holds.op !== 'or' || l.holds.xs.length !== 2)
324
+ continue;
325
+ const [g, body] = l.holds.xs;
326
+ if (g?.op !== 'not' || g.x.op !== '=' || g.x.a.op !== 'var' || g.x.b.op !== 'const' || !body)
327
+ continue;
328
+ const mode = g.x.a.name, entry = m.step.state?.[mode];
329
+ if (entry?.op !== 'if' || !eq(entry.then, g.x.b))
330
+ continue;
331
+ const qs = flat(body, 'or'), negative = qs.flatMap(q => { const atom = q.op === 'not' ? q.x : not(q), cell = positiveExact(atom); return cell ? [{ atom, cell, clause: q }] : []; }), positive = qs.flatMap(q => positiveExact(q) ? [{ atom: q, cell: positiveExact(q) }] : []);
332
+ if (negative.length !== 1 || positive.length !== 1)
333
+ continue;
334
+ const domain = negative[0], range = positive[0];
335
+ if (domain.cell.map === range.cell.map || qs.some(q => q !== domain.clause && q !== range.atom && json(q).includes('"op":"get"')))
336
+ continue;
337
+ const update = m.step.maps?.[domain.cell.map], persistent = m.step.maps?.[range.cell.map];
338
+ if (!update || !('each' in update) || !persistent || m.maps?.[domain.cell.map] !== 0 || domain.cell.key.op !== 'var')
339
+ continue;
340
+ const entryFacts = factsWith(new Map(), entry.c, true), entryValue = foldE(update.value, entryFacts, more);
341
+ let sourceCell;
342
+ if (entryValue?.op === 'get' && m.inputMaps?.includes(entryValue.map) && eq(entryValue.key, x(update.each)))
343
+ sourceCell = entryValue;
344
+ else if (entryValue?.op === 'if' && eq(entryValue.then, n(1)) && eq(entryValue.else, n(0))) {
345
+ const p = positiveExact(entryValue.c);
346
+ if (p && m.inputMaps?.includes(p.map) && eq(p.key, x(update.each))) {
347
+ const inverse = substC(domain.atom, { vars: { [domain.cell.key.name]: x(update.each) }, maps: { [domain.cell.map]: key => ({ op: 'get', map: p.map, key }) }, flags: {} });
348
+ // No Eq1/>=1/nonzero normalization is permitted across the copy.
349
+ if (eq(inverse, entryValue.c))
350
+ sourceCell = p;
351
+ }
352
+ }
353
+ if (!sourceCell) {
354
+ audit.rows.push({ rule: 'frame', target: l.name, accepted: false, reason: 'copy predicate is not exact' });
355
+ continue;
356
+ }
357
+ const oldCell = { op: 'get', map: range.cell.map, key: 'each' in persistent ? x(persistent.each) : persistent.key }, atOld = replaceCell(range.atom, range.cell, oldCell), post = replaceCell(range.atom, range.cell, persistent.value);
358
+ const retained = foldC(post, factsWith(new Map(), atOld, true), more);
359
+ if (!retained || !eq(retained, yes)) {
360
+ audit.rows.push({ rule: 'frame', target: l.name, accepted: false, reason: 'consequent positivity not preserved' });
361
+ continue;
362
+ }
363
+ const fields = vars(body).filter(v => v in m.state), oldDomain = { op: 'get', map: domain.cell.map, key: x(update.each) }, oldAtom = substC(domain.atom, { vars: { [domain.cell.key.name]: x(update.each) }, maps: {}, flags: {} });
364
+ const outside = factsWith(new Map(), entry.c, false);
365
+ const framed = (e, fs, depth = 0) => {
366
+ if (!more() || depth > 48)
367
+ return false;
368
+ const value = foldE(e, fs, more);
369
+ if (!value)
370
+ return false;
371
+ if (value.op === 'if')
372
+ return framed(value.then, factsWith(fs, value.c, true), depth + 1) && framed(value.else, factsWith(fs, value.c, false), depth + 1);
373
+ const after = foldC(replaceCell(oldAtom, oldDomain, value), fs, more);
374
+ if (after && eq(after, no))
375
+ return true;
376
+ const prior = foldC(oldAtom, fs, more);
377
+ if (!eq(value, oldDomain) && (!prior || !eq(prior, yes)))
378
+ return false;
379
+ return fields.every(v => { const post = foldE(m.step.state?.[v] ?? x(v), fs, more); return !!post && eq(post, x(v)); });
380
+ };
381
+ const accepted = framed(update.value, outside);
382
+ audit.rows.push({ rule: 'frame', target: l.name, accepted, reason: accepted ? 'empty init, exact entry copy, clear-or-frame, persistent positive target' : 'outside-entry path unframed' });
383
+ if (accepted)
384
+ add(body, new Set([l.forall, ...(l.keys ?? [])].filter((v) => !!v)), 'global-frame');
385
+ }
386
+ // Appended point/point proposals: syntax is eligibility, never a transition fact.
387
+ // Accept only the two exact Boolean retention shapes; no arbitrary self read.
388
+ const insertion = (map, key, value) => {
389
+ if (!more() || value.op !== 'if' || !eq(value.then, n(1)) || !eq(value.else, n(0)))
390
+ return;
391
+ const branches = flat(simplify(value.c), 'or'), prior = same({ op: 'get', map, key }, n(1));
392
+ if (branches.length !== 2)
393
+ return;
394
+ for (let i = 0; i < 2; i++) {
395
+ const guard = branches[i], retained = branches[1 - i];
396
+ if (eq(retained, prior) || eq(retained, simplify(all(not(guard), prior))))
397
+ return guard;
398
+ }
399
+ return;
400
+ };
401
+ // Complete, bounded expansion of plain flags only. In particular an OR keeps
402
+ // every arm, and quantified/opaque/cyclic flags cannot supply a free binder.
403
+ const plain = (c, depth = 0) => {
404
+ if (!more() || depth > 24 || c.op === 'all')
405
+ return;
406
+ if (c.op === 'flag') {
407
+ const q = flags[c.name];
408
+ return q ? plain(q, depth + 1) : undefined;
409
+ }
410
+ if (c.op === 'not') {
411
+ const q = plain(c.x, depth + 1);
412
+ return q ? not(q) : undefined;
413
+ }
414
+ if (c.op === 'and' || c.op === 'or') {
415
+ const xs = [];
416
+ for (const x of c.xs) {
417
+ const q = plain(x, depth + 1);
418
+ if (!q)
419
+ return;
420
+ xs.push(q);
421
+ }
422
+ return { op: c.op, xs };
423
+ }
424
+ return c;
425
+ };
426
+ for (const [source, point] of Object.entries(m.step.maps ?? {})) {
427
+ if (!more())
428
+ return [...out.values()];
429
+ if ('each' in point || m.maps?.[source] !== 0)
430
+ continue;
431
+ const ps = vars(point.key), affine = linear(point.key);
432
+ if (!ps.length || !affine || [...affine.values()].some(k => !Number.isSafeInteger(k) || k <= 0) || ps.some(p => !m.inputs.includes(p)))
433
+ continue;
434
+ const domains = bounds.filter(b => ps.includes(b.a.name));
435
+ if (ps.some(p => !domains.some(b => eq(b.a, x(p)))))
436
+ continue;
437
+ const guard = insertion(source, point.key, point.value), expanded = guard && plain(guard);
438
+ if (!expanded)
439
+ continue;
440
+ const names = ps.map(() => fresh()), at = { vars: Object.fromEntries(ps.map((p, i) => [p, x(names[i])])), maps: {}, flags: {} }, bound = new Set(names);
441
+ // Only necessary unit conjuncts are eligible, never one selected OR arm.
442
+ for (const atom of flat(expanded, 'and')) {
443
+ if (!more())
444
+ return [...out.values()];
445
+ if (atom.op !== '=')
446
+ continue;
447
+ const dest = eq(atom.b, n(1)) && atom.a.op === 'get' ? atom.a : eq(atom.a, n(1)) && atom.b.op === 'get' ? atom.b : undefined;
448
+ if (!dest || dest.map === source || !Object.hasOwn(m.maps ?? {}, dest.map) || !linear(dest.key) || vars(dest.key).some(p => !ps.includes(p) && !m.params.includes(p)))
449
+ continue;
450
+ const update = m.step.maps?.[dest.map];
451
+ if (!update || 'each' in update || !insertion(dest.map, update.key, update.value))
452
+ continue;
453
+ const hyp = all(...domains, { op: '<=', a: n(1), b: { op: 'get', map: source, key: point.key } }), size = out.size;
454
+ add(substC(any(not(hyp), same(dest, n(1))), at), bound, 'point-guard');
455
+ if (out.size > size)
456
+ audit.rows.push({ rule: 'point-guard', target: source, accepted: true, reason: 'exact insertion guard, persistent-one destination, source bounds; proposal only' });
457
+ }
458
+ }
459
+ return [...out.values()];
460
+ }
461
+ function factsWith(fs, c, value) {
462
+ const out = new Map(fs);
463
+ const add = (q, v, depth = 0) => { if (depth > 48)
464
+ return; out.set(json(q), { c: q, value: v }); if (q.op === 'not')
465
+ add(q.x, !v, depth + 1);
466
+ else if (q.op === 'and' && v || q.op === 'or' && !v)
467
+ for (const x of q.xs)
468
+ add(x, v, depth + 1); };
469
+ add(c, value);
470
+ return out;
471
+ }
472
+ function positiveExact(c) {
473
+ if (c.op === '=' && eq(c.b, n(1)) && c.a.op === 'get')
474
+ return c.a;
475
+ if (c.op === '=' && eq(c.a, n(1)) && c.b.op === 'get')
476
+ return c.b;
477
+ if (c.op === '<=' && eq(c.a, n(1)) && c.b.op === 'get')
478
+ return c.b;
479
+ if (c.op === 'not' && c.x.op === '=' && eq(c.x.b, n(0)) && c.x.a.op === 'get')
480
+ return c.x.a;
481
+ if (c.op === 'not' && c.x.op === '=' && eq(c.x.a, n(0)) && c.x.b.op === 'get')
482
+ return c.x.b;
483
+ return undefined;
484
+ }
485
+ function replaceCell(c, old, value) { return substC(c, { vars: {}, maps: { [old.map]: key => eq(key, old.key) ? value : { op: 'get', map: old.map, key } }, flags: {} }); }
486
+ function boundKnown(e, b, pre) {
487
+ if (pre.some(c => c.op === '<' && eq(c.a, e) && eq(c.b, b)))
488
+ return true;
489
+ if (e.op !== 'const' || e.value < 0)
490
+ return false;
491
+ if (b.op === 'const')
492
+ return e.value < b.value;
493
+ return pre.some(c => (c.op === '<' || c.op === '<=') && c.a.op === 'const' && eq(c.b, b) && e.value < (c.op === '<' ? c.a.value + 1 : c.a.value));
494
+ }
495
+ function foldE(e, fs, tick, depth = 0) {
496
+ if (!tick() || depth > 48)
497
+ return;
498
+ for (const { c, value } of fs.values())
499
+ if (value && c.op === '=') {
500
+ if (eq(c.a, e) && c.b.op === 'const')
501
+ return c.b;
502
+ if (eq(c.b, e) && c.a.op === 'const')
503
+ return c.a;
504
+ }
505
+ if (e.op === 'if') {
506
+ const c = foldC(e.c, fs, tick, depth + 1);
507
+ if (!c)
508
+ return;
509
+ if (eq(c, yes))
510
+ return foldE(e.then, fs, tick, depth + 1);
511
+ if (eq(c, no))
512
+ return foldE(e.else, fs, tick, depth + 1);
513
+ const a = foldE(e.then, fs, tick, depth + 1), b = foldE(e.else, fs, tick, depth + 1);
514
+ if (!a || !b)
515
+ return;
516
+ return eq(a, b) ? a : { op: 'if', c, then: a, else: b };
517
+ }
518
+ if (e.op === 'get') {
519
+ const key = foldE(e.key, fs, tick, depth + 1);
520
+ return key ? { ...e, key } : undefined;
521
+ }
522
+ if (e.op === 'add' || e.op === 'sub' || e.op === 'min' || e.op === 'max' || e.op === 'prod') {
523
+ const a = foldE(e.a, fs, tick, depth + 1), b = foldE(e.b, fs, tick, depth + 1);
524
+ if (!a || !b)
525
+ return;
526
+ if (a.op === 'const' && b.op === 'const')
527
+ return n(e.op === 'add' ? a.value + b.value : e.op === 'sub' ? Math.max(0, a.value - b.value) : e.op === 'min' ? Math.min(a.value, b.value) : e.op === 'max' ? Math.max(a.value, b.value) : a.value * b.value);
528
+ return { ...e, a, b };
529
+ }
530
+ if (e.op === 'mul') {
531
+ const a = foldE(e.a, fs, tick, depth + 1);
532
+ return a ? (a.op === 'const' ? n(e.k * a.value) : { ...e, a }) : undefined;
533
+ }
534
+ return e;
535
+ }
536
+ function foldC(c, fs, tick, depth = 0) {
537
+ if (!tick() || depth > 48)
538
+ return;
539
+ const f = fs.get(json(c));
540
+ if (f)
541
+ return f.value ? yes : no;
542
+ if (c.op === 'not') {
543
+ const q = foldC(c.x, fs, tick, depth + 1);
544
+ return q ? (eq(q, yes) ? no : eq(q, no) ? yes : q.op === 'not' ? q.x : not(q)) : undefined;
545
+ }
546
+ if (c.op === 'and' || c.op === 'or') {
547
+ const unit = c.op === 'and' ? yes : no, zero = c.op === 'and' ? no : yes, qs = [];
548
+ for (const x of flat(c, c.op)) {
549
+ const q = foldC(x, fs, tick, depth + 1);
550
+ if (!q)
551
+ return;
552
+ if (eq(q, zero))
553
+ return zero;
554
+ if (!eq(q, unit))
555
+ qs.push(q);
556
+ }
557
+ if (qs.some(q => qs.some(r => eq(q, not(r)) || eq(r, not(q)))))
558
+ return zero;
559
+ const unique = [...new Map(qs.map(q => [json(q), q])).values()];
560
+ return !unique.length ? unit : unique.length === 1 ? unique[0] : { op: c.op, xs: unique };
561
+ }
562
+ if (c.op === '=' || c.op === '<' || c.op === '<=') {
563
+ const a = foldE(c.a, fs, tick, depth + 1), b = foldE(c.b, fs, tick, depth + 1);
564
+ if (!a || !b)
565
+ return;
566
+ if (eq(a, b))
567
+ return c.op === '<' ? no : yes;
568
+ if (a.op === 'const' && b.op === 'const')
569
+ return (c.op === '=' ? a.value === b.value : c.op === '<' ? a.value < b.value : a.value <= b.value) ? yes : no;
570
+ return { ...c, a, b };
571
+ }
572
+ return c;
573
+ }
574
+ export function scanPrefixes(m, occupied = []) {
575
+ const flags = m.step.flags ?? {}, packets = [];
576
+ let work = 0;
577
+ const more = () => ++work <= 8192 && packets.length < 64;
578
+ const eq = (a, b) => JSON.stringify(a) === JSON.stringify(b);
579
+ const sub = (c, from, to) => substC(c, { vars: { [from]: to }, maps: {}, flags: {} });
580
+ const parts = (c) => c.op === 'and' ? c.xs.flatMap(parts) : [c];
581
+ const closure = (c, seen = new Set()) => {
582
+ const rs = [c];
583
+ walk(c, o => {
584
+ if (o.op === 'flag' && typeof o.name === 'string' && !seen.has(o.name)) {
585
+ seen.add(o.name);
586
+ const f = flags[o.name];
587
+ if (f && f.op !== 'all')
588
+ rs.push(...closure(f, seen));
589
+ }
590
+ });
591
+ return rs;
592
+ };
593
+ // Stable structural discriminator; collisions are detected below, not trusted away.
594
+ const tag = (s) => {
595
+ let h = 14695981039346656037n;
596
+ for (let i = 0; i < s.length; i++)
597
+ h = BigInt.asUintN(64, (h ^ BigInt(s.charCodeAt(i))) * 1099511628211n);
598
+ return h.toString(16);
599
+ };
600
+ scan: for (const [counter, w] of Object.entries(m.step.state ?? {})) {
601
+ if (!more())
602
+ break;
603
+ if (w.op !== 'if' || !eq(w.then, { op: 'add', a: x(counter), b: n(1) }) || w.else.op !== 'if' || !eq(w.else.then, n(0)) || !eq(w.else.else, x(counter)))
604
+ continue;
605
+ const related = closure(w.c), atoms = related.flatMap(parts);
606
+ const activities = atoms.filter((c) => c.op === '=' && c.a.op === 'var' && c.a.name !== counter && c.a.name in m.state && eq(m.state[c.a.name], n(0)) && eq(c.b, n(1)));
607
+ for (const c of related)
608
+ for (const node of parts(c)) {
609
+ if (node.op !== 'not' || node.x.op !== 'flag')
610
+ continue;
611
+ const rowName = node.x.name, row = flags[rowName];
612
+ if (row?.op !== 'all' || row.keys?.length)
613
+ continue;
614
+ const members = parts(c).filter(q => q.op === '=' && q.a.op === 'get' && eq(q.a.key, x(counter)) && q.a.map in (m.maps ?? {}) && eq(q.b, n(1)));
615
+ for (const member of members)
616
+ for (const activity of activities) {
617
+ if (!more())
618
+ break scan;
619
+ const taken = new Set([...m.params, ...m.inputs, ...Object.keys(m.state), ...Object.keys(m.maps ?? {}), ...Object.keys(flags)]);
620
+ walk([m], o => { if (typeof o.name === 'string')
621
+ taken.add(o.name); if (typeof o.key === 'string')
622
+ taken.add(o.key); });
623
+ let fresh = 'scanKey';
624
+ while (taken.has(fresh) || fresh === row.key)
625
+ fresh += 'X';
626
+ const base = `scan-prefix:${counter}:${rowName}:${tag(JSON.stringify({ member, activity, row }))}`;
627
+ packets.push([
628
+ { name: base + ':entry', holds: any(not(same(activity.a, n(0))), same(x(counter), n(0))) },
629
+ { name: base, forall: fresh, keys: [row.key], holds: any(not(all(activity, less(x(fresh), x(counter)), sub(member, counter, x(fresh)))), sub(row.c, counter, x(fresh))) },
630
+ ]);
631
+ }
632
+ }
633
+ }
634
+ const blocked = new Set(occupied.map(l => l.name)), byName = new Map();
635
+ for (const packet of packets)
636
+ for (const l of packet) {
637
+ const signature = JSON.stringify(l), previous = byName.get(l.name);
638
+ if (previous !== undefined && previous !== signature)
639
+ blocked.add(l.name);
640
+ byName.set(l.name, signature);
641
+ }
642
+ const unique = new Map();
643
+ for (const packet of packets) {
644
+ // Skip the whole family if either name conflicts, including with a caller.
645
+ if (packet.some(l => blocked.has(l.name)))
646
+ continue;
647
+ for (const l of packet)
648
+ unique.set(l.name, l);
649
+ }
650
+ return [...unique.values()];
651
+ }
@@ -2,6 +2,7 @@
2
2
  import type { Extraction, Name } from './extract.js';
3
3
  import { type SecurityCandidate } from './security.js';
4
4
  import { type Verdict } from './dsl.js';
5
+ import type { TranslationFields } from './source-check.js';
5
6
  import { type LawText } from './laws.js';
6
7
  export declare const colors: (env: Record<string, string | undefined>, tty: boolean, json?: boolean) => boolean;
7
8
  export type Opts = {
@@ -34,7 +35,8 @@ type Config = {
34
35
  };
35
36
  export declare function configFor(from: string): Config;
36
37
  export declare function toml(text: string): Record<string, unknown>;
37
- type LawOut = {
38
+ type LawOut = Omit<TranslationFields, 'translationWitness'> & {
39
+ translationWitness?: unknown;
38
40
  name: string;
39
41
  expr: string;
40
42
  where?: string;