gdp-wired 0.46.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 (182) hide show
  1. package/README.md +18 -5
  2. package/dist/core/carry.d.ts +15 -0
  3. package/dist/core/carry.js +651 -0
  4. package/dist/core/cli.d.ts +4 -1
  5. package/dist/core/cli.js +31 -11
  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-schema.d.ts +90 -0
  99. package/dist/core/effect-schema.js +500 -0
  100. package/dist/core/effect.d.ts +3 -7
  101. package/dist/core/effect.js +10 -65
  102. package/dist/core/exists.d.ts +1 -1
  103. package/dist/core/exists.js +80 -15
  104. package/dist/core/extract-check.d.ts +7 -1
  105. package/dist/core/extract-check.js +161 -19
  106. package/dist/core/extract-go-check.js +24 -4
  107. package/dist/core/extract-go.d.ts +7 -0
  108. package/dist/core/extract-go.js +29 -2
  109. package/dist/core/extract-read.d.ts +31 -9
  110. package/dist/core/extract-read.js +36 -11
  111. package/dist/core/extract-rs.js +58 -14
  112. package/dist/core/extract-shapes.d.ts +1 -0
  113. package/dist/core/extract-shapes.js +190 -27
  114. package/dist/core/extract-standalone.d.ts +3 -0
  115. package/dist/core/extract-standalone.js +265 -0
  116. package/dist/core/extract.d.ts +1 -0
  117. package/dist/core/extract.js +25 -5
  118. package/dist/core/field-laws.d.ts +2 -0
  119. package/dist/core/field-laws.js +100 -0
  120. package/dist/core/finite-frontiers.d.ts +5 -0
  121. package/dist/core/finite-frontiers.js +116 -0
  122. package/dist/core/finite-houdini.d.ts +52 -0
  123. package/dist/core/finite-houdini.js +460 -0
  124. package/dist/core/finite-model.d.ts +13 -0
  125. package/dist/core/finite-model.js +68 -0
  126. package/dist/core/finite-tiny.d.ts +72 -0
  127. package/dist/core/finite-tiny.js +1052 -0
  128. package/dist/core/infer.d.ts +1 -1
  129. package/dist/core/infer.js +32 -9
  130. package/dist/core/laws.d.ts +2 -1
  131. package/dist/core/laws.js +76 -19
  132. package/dist/core/lemma-cuts.d.ts +38 -0
  133. package/dist/core/lemma-cuts.js +365 -0
  134. package/dist/core/machine.d.ts +39 -2
  135. package/dist/core/machine.js +240 -35
  136. package/dist/core/mutate.d.ts +55 -0
  137. package/dist/core/mutate.js +210 -0
  138. package/dist/core/native-domain.d.ts +6 -0
  139. package/dist/core/native-domain.js +114 -0
  140. package/dist/core/native-law.d.ts +3 -0
  141. package/dist/core/native-law.js +174 -0
  142. package/dist/core/quorum-laws.d.ts +5 -0
  143. package/dist/core/quorum-laws.js +239 -0
  144. package/dist/core/relational.js +10 -2
  145. package/dist/core/relevance.d.ts +4 -0
  146. package/dist/core/relevance.js +79 -0
  147. package/dist/core/rs/Cargo.toml +1 -1
  148. package/dist/core/rs/src/main.rs +458 -9
  149. package/dist/core/runtime-wall.d.ts +3 -0
  150. package/dist/core/runtime-wall.js +4 -0
  151. package/dist/core/sample-step.d.ts +16 -0
  152. package/dist/core/sample-step.js +28 -0
  153. package/dist/core/source-check.d.ts +44 -0
  154. package/dist/core/source-check.js +126 -0
  155. package/dist/core/source-diagnostic.d.ts +1 -0
  156. package/dist/core/source-diagnostic.js +34 -0
  157. package/dist/core/support-budget.d.ts +2 -0
  158. package/dist/core/support-budget.js +6 -0
  159. package/dist/core/system.js +2 -0
  160. package/dist/core/wall.d.ts +6 -0
  161. package/dist/core/wall.js +23 -0
  162. package/dist/core/watch.js +4 -0
  163. package/dist/kernel/cdcl.d.ts +4 -0
  164. package/dist/kernel/cdcl.js +20 -5
  165. package/dist/kernel/kernel.d.ts +10 -0
  166. package/dist/kernel/kernel.js +56 -3
  167. package/dist/kernel/model-sequence.d.ts +2 -2
  168. package/dist/kernel/model-sequence.js +4 -3
  169. package/dist/kernel/model-values.d.ts +2 -0
  170. package/dist/kernel/model-values.js +60 -0
  171. package/dist/kernel/prove.d.ts +10 -0
  172. package/dist/kernel/prove.js +56 -8
  173. package/dist/kernel/prover-rows.d.ts +29 -0
  174. package/dist/kernel/prover-rows.js +114 -0
  175. package/dist/lean-kernel/emit.d.ts +39 -2
  176. package/dist/lean-kernel/emit.js +136 -24
  177. package/dist/lean-kernel/lean-terms.d.ts +1 -0
  178. package/dist/lean-kernel/lean-terms.js +13 -5
  179. package/dist/lean-kernel/library.js +4 -0
  180. package/dist/lean-kernel/qalpha.d.ts +7 -0
  181. package/dist/lean-kernel/qalpha.js +40 -0
  182. package/package.json +5 -1
@@ -0,0 +1,318 @@
1
+ // Untrusted conditional proof transport. No search and no lemma references enter
2
+ // the result: each prerequisite is checked independently, then inlined as a split.
3
+ import { canon, canonFlip, check, leafConstraintsReusing as rowsAt, lemmaPath } from "../../kernel/kernel.js";
4
+ import { add, k, scale } from "../solver.js";
5
+ const alive = (until) => performance.now() < until;
6
+ const flip = (lin) => add(k(1), scale(lin, -1));
7
+ const put = (path, lin, holds) => new Map(path).set(canon(lin), { lin, holds });
8
+ const plain = (lin) => Object.keys(lin).every(name => !name.includes('⟦')) && Object.values(lin).every(Number.isSafeInteger);
9
+ const decide = (lin, path) => {
10
+ if (!Object.values(lin).every(Number.isSafeInteger))
11
+ return undefined;
12
+ if (Object.keys(lin).every(name => name === '1'))
13
+ return (lin['1'] ?? 0) <= 0;
14
+ const direct = path.get(canon(lin))?.holds;
15
+ if (direct !== undefined)
16
+ return direct;
17
+ const opposite = path.get(canonFlip(lin))?.holds;
18
+ return opposite === undefined ? undefined : !opposite;
19
+ };
20
+ // Match kernel prependAtoms exactly: duplicates remain rows, and the first
21
+ // occurrence decides lookups. In particular, negcut must precede every guard.
22
+ const proofPath = (when, cut) => {
23
+ const path = new Map();
24
+ let duplicate = 0;
25
+ for (const atom of [{ lin: cut, holds: false }, ...when.map(lin => ({ lin, holds: true }))]) {
26
+ const id = canon(atom.lin);
27
+ path.set(path.has(id) ? `@${duplicate++}` : id, atom);
28
+ }
29
+ return path;
30
+ };
31
+ function transport(source, goal, tree, from, to, until, missing) {
32
+ if (!alive(until))
33
+ return undefined;
34
+ if ('split' in tree) {
35
+ const known = decide(tree.split, to);
36
+ if (known !== undefined)
37
+ return transport(source, goal, known ? tree.yes : tree.no, put(from, tree.split, known), to, until, missing);
38
+ const yes = transport(source, goal, tree.yes, put(from, tree.split, true), put(to, tree.split, true), until, missing);
39
+ const no = yes && transport(source, goal, tree.no, put(from, tree.split, false), put(to, tree.split, false), until, missing);
40
+ return yes && no ? { split: tree.split, yes, no } : undefined;
41
+ }
42
+ const before = rowsAt(source, from), after = rowsAt(goal, to), at = new Map();
43
+ for (const [i, row] of after.entries()) {
44
+ if (!alive(until))
45
+ return undefined;
46
+ // A selected cut can make a target hypothesis fold directly to false.
47
+ // Reuse that exact input row, rather than transporting obsolete source rows.
48
+ if (Number.isSafeInteger(row.lin['1']) && (row.lin['1'] ?? 0) > 0
49
+ && Object.entries(row.lin).every(([name, value]) => name === '1' || value === 0))
50
+ return { leaf: [{ kind: 'input', index: i }] };
51
+ const id = canon(row.lin);
52
+ if (!at.has(id))
53
+ at.set(id, i);
54
+ }
55
+ const index = (i) => {
56
+ if (!Number.isSafeInteger(i) || i < 0)
57
+ return undefined;
58
+ // Squares and products follow the base rows, in the same order at both ends.
59
+ if (i >= before.length)
60
+ return i - before.length + after.length;
61
+ const row = before[i], found = at.get(canon(row.lin));
62
+ if (found === undefined)
63
+ missing?.(row.lin);
64
+ return found;
65
+ };
66
+ const leaf = [], products = [];
67
+ for (const step of tree.leaf) {
68
+ if (!alive(until))
69
+ return undefined;
70
+ if (step.kind !== 'input')
71
+ leaf.push(step);
72
+ else {
73
+ const i = index(step.index);
74
+ if (i === undefined)
75
+ return undefined;
76
+ leaf.push({ ...step, index: i });
77
+ }
78
+ }
79
+ for (const [a, b] of tree.products ?? []) {
80
+ if (!alive(until))
81
+ return undefined;
82
+ const i = index(a), j = index(b);
83
+ if (i === undefined || j === undefined)
84
+ return undefined;
85
+ products.push([i, j]);
86
+ }
87
+ return alive(until) ? { leaf, ...(tree.products ? { products } : {}), ...(tree.squares ? { squares: tree.squares } : {}) } : undefined;
88
+ }
89
+ class ObservedPath extends Map {
90
+ observed;
91
+ constructor(path, observed) {
92
+ super(path);
93
+ this.observed = observed;
94
+ }
95
+ get(key) { this.observed.add(key); return super.get(key); }
96
+ has(key) { this.observed.add(key); return super.has(key); }
97
+ }
98
+ function dependencies(goal, tree, path, facts, atoms, until) {
99
+ if (!alive(until))
100
+ return false;
101
+ if ('split' in tree)
102
+ return dependencies(goal, tree.yes, put(path, tree.split, true), facts, atoms, until)
103
+ && dependencies(goal, tree.no, put(path, tree.split, false), facts, atoms, until);
104
+ const rows = rowsAt(goal, new ObservedPath(path, atoms)), values = [...path.values()];
105
+ const observe = (i) => {
106
+ const src = rows[i]?.src;
107
+ if (src && 'fact' in src)
108
+ facts.add(src.fact);
109
+ if (src && 'atom' in src) {
110
+ const atom = values[src.atom];
111
+ if (atom) {
112
+ atoms.add(canon(atom.lin));
113
+ atoms.add(canonFlip(atom.lin));
114
+ }
115
+ }
116
+ };
117
+ for (const step of tree.leaf) {
118
+ if (!alive(until))
119
+ return false;
120
+ if (step.kind === 'input')
121
+ observe(step.index);
122
+ }
123
+ for (const [a, b] of tree.products ?? []) {
124
+ if (!alive(until))
125
+ return false;
126
+ observe(a);
127
+ observe(b);
128
+ }
129
+ return alive(until);
130
+ }
131
+ function localGoal(goal, lemma) {
132
+ if (lemma.slice === undefined)
133
+ return goal;
134
+ if (!lemma.slice.every(i => Number.isSafeInteger(i) && i >= 0 && i < goal.hyps.length))
135
+ return undefined;
136
+ return { ...goal, hyps: lemma.slice.map(i => goal.hyps[i]) };
137
+ }
138
+ function checked(goal, tree, path, until) {
139
+ if (!alive(until))
140
+ return false;
141
+ try {
142
+ return check(goal, tree, path) && alive(until);
143
+ }
144
+ catch {
145
+ return false;
146
+ }
147
+ }
148
+ // Select prerequisites only in response to missing exact rows. Eligibility is
149
+ // evaluated against the original scope, never against another lemma's cut.
150
+ function inline(goal, source, tree, from, scope, base, lemmas, until, diagnose) {
151
+ if (!alive(until))
152
+ return undefined;
153
+ const available = [];
154
+ for (const lemma of lemmas) {
155
+ if (!alive(until))
156
+ return undefined;
157
+ if (plain(lemma.cut) && lemmaPath([lemma], scope) !== scope)
158
+ available.push(lemma);
159
+ }
160
+ const required = [];
161
+ // Already selected cuts are prepended with the kernel's duplicate semantics.
162
+ // Their guards were tested above, not against this augmented proof context.
163
+ const target = (cuts) => lemmaPath(cuts.map(lemma => ({ ...lemma, when: [] })), base);
164
+ let path = base, proof;
165
+ for (;;) {
166
+ if (!alive(until))
167
+ return undefined;
168
+ let absent;
169
+ path = target(required);
170
+ proof = transport(source, goal, tree, from, path, until, row => { absent = canon(row); });
171
+ if (proof)
172
+ break;
173
+ const lemma = available.find(candidate => canon(candidate.cut) === absent && !required.includes(candidate));
174
+ if (!lemma || !alive(until)) {
175
+ diagnose?.({ reason: alive(until) ? 'missing-row' : 'deadline', row: absent, available: available.length, required: required.length });
176
+ return undefined;
177
+ }
178
+ const local = localGoal(goal, lemma);
179
+ if (!local || !checked(local, lemma.tree, proofPath(lemma.when, lemma.cut), until))
180
+ return undefined;
181
+ required.push(lemma);
182
+ required.sort((a, b) => available.indexOf(a) - available.indexOf(b));
183
+ }
184
+ for (let i = required.length - 1; i >= 0; i--) {
185
+ if (!alive(until))
186
+ return undefined;
187
+ const lemma = required[i], prefix = target(required.slice(0, i)), known = decide(lemma.cut, prefix);
188
+ const local = localGoal(goal, lemma);
189
+ if (!local)
190
+ return undefined;
191
+ if (known === true)
192
+ proof = transport(goal, goal, proof, path, prefix, until);
193
+ else {
194
+ const no = transport(local, goal, lemma.tree, proofPath(lemma.when, lemma.cut), known === false ? prefix : put(prefix, lemma.cut, false), until);
195
+ if (!no)
196
+ return undefined;
197
+ if (known === false)
198
+ proof = no;
199
+ else {
200
+ const yes = transport(goal, goal, proof, path, put(prefix, lemma.cut, true), until);
201
+ if (!yes)
202
+ return undefined;
203
+ proof = { split: lemma.cut, yes, no };
204
+ }
205
+ }
206
+ if (!proof)
207
+ return undefined;
208
+ path = prefix;
209
+ }
210
+ return alive(until) ? proof : undefined;
211
+ }
212
+ /** Transport a refutation to negcut + scope, inline required prior cuts, then
213
+ * minimize its guards and containing-goal hypothesis slice. `until` is an
214
+ * absolute performance.now() deadline, independent of the prover's clock. */
215
+ function admitCut(goal, source, tree, from, scope, cut, available, until) {
216
+ if (!alive(until) || !plain(cut))
217
+ return undefined;
218
+ let when = [];
219
+ for (const atom of scope.values()) {
220
+ if (!alive(until))
221
+ return undefined;
222
+ when.push(atom.holds ? atom.lin : flip(atom.lin));
223
+ }
224
+ let base = proofPath(when, cut), proof = inline(goal, source, tree, from, scope, base, available, until);
225
+ if (!proof)
226
+ return undefined;
227
+ const facts = new Set(), atoms = new Set();
228
+ if (!dependencies(goal, proof, base, facts, atoms, until))
229
+ return undefined;
230
+ let slice = [...facts].sort((a, b) => a - b), small = { ...goal, hyps: slice.map(i => goal.hyps[i]) };
231
+ const narrowed = transport(goal, small, proof, base, base, until);
232
+ if (narrowed)
233
+ proof = narrowed;
234
+ else {
235
+ slice = goal.hyps.map((_, i) => i);
236
+ small = goal;
237
+ }
238
+ const less = when.filter(lin => atoms.has(canon(lin)) || atoms.has(canonFlip(lin)));
239
+ if (less.length < when.length) {
240
+ const next = proofPath(less, cut), reduced = transport(small, small, proof, base, next, until);
241
+ if (reduced) {
242
+ when = less;
243
+ base = next;
244
+ proof = reduced;
245
+ }
246
+ }
247
+ // Lookup traces are conservative; each further removal must transport exactly.
248
+ for (let i = when.length - 1; i >= 0; i--) {
249
+ if (!alive(until))
250
+ return undefined;
251
+ const less = when.filter((_, j) => j !== i), next = proofPath(less, cut);
252
+ const reduced = transport(small, small, proof, base, next, until);
253
+ if (reduced) {
254
+ when = less;
255
+ base = next;
256
+ proof = reduced;
257
+ }
258
+ }
259
+ return checked(small, proof, base, until) ? { when, cut, tree: proof, slice } : undefined;
260
+ }
261
+ // Use original hypotheses only, but avoid rebuilding unrelated count-instance
262
+ // rows at every source-proof leaf. Exact transport and the final checker remain
263
+ // authoritative; a missed syntactic match falls back to the full goal.
264
+ const hypothesisIndex = new WeakMap();
265
+ function relevantHypotheses(goal, source, tree, path, available, until) {
266
+ const used = new Set();
267
+ if (!dependencies(source, tree, path, used, new Set(), until))
268
+ return undefined;
269
+ let index = hypothesisIndex.get(goal);
270
+ if (!index) {
271
+ index = new Map();
272
+ const visit = (value, owner) => {
273
+ if (!alive(until))
274
+ return false;
275
+ const key = JSON.stringify(value), owners = index.get(key) ?? new Set();
276
+ owners.add(owner);
277
+ index.set(key, owners);
278
+ if (value.op === 'and' || value.op === 'or')
279
+ return value.xs.every(c => visit(c, owner));
280
+ return value.op !== 'not' || visit(value.x, owner);
281
+ };
282
+ if (!goal.hyps.every(visit))
283
+ return undefined;
284
+ hypothesisIndex.set(goal, index);
285
+ }
286
+ const selected = new Set();
287
+ for (const i of used)
288
+ for (const j of index.get(JSON.stringify(source.hyps[i])) ?? [])
289
+ selected.add(j);
290
+ for (const lemma of available) {
291
+ if (lemma.slice === undefined || lemma.slice.some(i => !Number.isSafeInteger(i) || i < 0 || i >= goal.hyps.length))
292
+ return undefined;
293
+ for (const i of lemma.slice)
294
+ selected.add(i);
295
+ }
296
+ return [...selected].sort((a, b) => a - b);
297
+ }
298
+ export function admitConditionalCut(goal, source, tree, from, scope, cut, available, until) {
299
+ if (!alive(until) || !plain(cut))
300
+ return undefined;
301
+ const slice = relevantHypotheses(goal, source, tree, from, available, until);
302
+ if (slice && slice.length < goal.hyps.length) {
303
+ const positions = new Map(slice.map((old, index) => [old, index])), small = { ...goal, hyps: slice.map(i => goal.hyps[i]) };
304
+ const local = available.map(lemma => ({ ...lemma, slice: lemma.slice.map(i => positions.get(i)) }));
305
+ const result = admitCut(small, source, tree, from, scope, cut, local, until);
306
+ if (result)
307
+ return { ...result, slice: (result.slice ?? small.hyps.map((_, i) => i)).map(i => slice[i]) };
308
+ }
309
+ return admitCut(goal, source, tree, from, scope, cut, available, until);
310
+ }
311
+ /** Emit an ordinary tree at scope, inlining only active cuts whose exact rows
312
+ * the source proof needs. The returned tree is kernel-checked without lemmas. */
313
+ export function inlineConditionalCuts(goal, source, tree, from, scope, lemmas, until, diagnose) {
314
+ const begun = performance.now(), proof = inline(goal, source, tree, from, scope, scope, lemmas, until, diagnose);
315
+ const accepted = !!proof && checked(goal, proof, scope, until);
316
+ diagnose?.({ reason: accepted ? 'complete' : alive(until) ? proof ? 'check-failed' : 'transport-failed' : 'deadline', proposed: !!proof, checked: accepted, wallMs: performance.now() - begun });
317
+ return accepted ? proof : undefined;
318
+ }
@@ -0,0 +1,19 @@
1
+ import { type Goal, type LemmaCut, type Path, type Tree } from '../../kernel/kernel.js';
2
+ export { conditionalProofStats } from './conditional-check-cache.js';
3
+ /** A proposer hint, not an admission check. Rows in tree use lemmaPath at each
4
+ * leaf; branches still use the ordinary split path, exactly as in the kernel. */
5
+ export declare function markConditionalProof(goal: Goal, tree: Tree, lemmas: LemmaCut[]): Tree;
6
+ export declare function hasConditionalProof(tree: Tree): boolean;
7
+ /** Check each marked region with its independent cuts, and unmarked regions
8
+ * with the ordinary kernel rules. A nested mark replaces its parent's list. */
9
+ export declare function checkConditionalProof(goal: Goal, tree: Tree, path?: Path): boolean;
10
+ /** Retarget an annotated or mixed proof, without turning cuts into split trees.
11
+ * before/after are ordinary entry paths, not paths already augmented by cuts. */
12
+ export declare function transportConditionalProof(from: Goal, to: Goal, tree: Tree, before: Path, after: Path, until: number): Tree | undefined;
13
+ /** Finalize at the empty root. Each local list is guarded by its entry path so
14
+ * exporting it cannot add assumptions to unrelated ordinary siblings. Every
15
+ * leaf is then reindexed, even if it did not originally carry metadata. */
16
+ export declare function collectConditionalCertificate(goal: Goal, tree: Tree, until: number): {
17
+ tree: Tree;
18
+ lemmas: LemmaCut[];
19
+ } | undefined;
@@ -0,0 +1,306 @@
1
+ // Untrusted metadata and exact transport for conditional count proofs. The kernel
2
+ // never reads this WeakMap: only ordinary trees and independent LemmaCuts escape.
3
+ import { canon, canonFlip, check, checkGoalLemmas, facts, leafConstraintsReusing as rowsAt, lemmaPath } from "../../kernel/kernel.js";
4
+ import { add, k, scale } from "../solver.js";
5
+ import { independentHit, rememberIndependent, retargetHit, rememberRetarget, slicedCheck, targetIndex } from "./conditional-check-cache.js";
6
+ export { conditionalProofStats } from "./conditional-check-cache.js";
7
+ const marked = new WeakMap();
8
+ const alive = (until) => performance.now() < until;
9
+ const flip = (lin) => add(k(1), scale(lin, -1));
10
+ const put = (path, lin, holds) => new Map(path).set(canon(lin), { lin, holds });
11
+ const sameGoal = (a, b) => a === b || JSON.stringify(a.hyps) === JSON.stringify(b.hyps);
12
+ /** A proposer hint, not an admission check. Rows in tree use lemmaPath at each
13
+ * leaf; branches still use the ordinary split path, exactly as in the kernel. */
14
+ export function markConditionalProof(goal, tree, lemmas) {
15
+ marked.set(tree, { goal, lemmas });
16
+ return tree;
17
+ }
18
+ export function hasConditionalProof(tree) {
19
+ const seen = new WeakSet();
20
+ const visit = (t) => {
21
+ if (marked.has(t))
22
+ return true;
23
+ if (seen.has(t))
24
+ return false;
25
+ seen.add(t);
26
+ return 'split' in t && (visit(t.yes) || visit(t.no));
27
+ };
28
+ return visit(tree);
29
+ }
30
+ // Match prependAtoms, including duplicate rows and first-occurrence lookups.
31
+ // Negcut must precede guards even when a guard has the very same canonical key.
32
+ const proofPath = (when, cut) => {
33
+ const path = new Map();
34
+ let duplicate = 0;
35
+ for (const atom of [{ lin: cut, holds: false }, ...when.map(lin => ({ lin, holds: true }))]) {
36
+ const id = canon(atom.lin);
37
+ path.set(path.has(id) ? `@${duplicate++}` : id, atom);
38
+ }
39
+ return path;
40
+ };
41
+ const validLin = (lin) => !!lin && typeof lin === 'object' && !Array.isArray(lin)
42
+ && (Object.getPrototypeOf(lin) === Object.prototype || Object.getPrototypeOf(lin) === null)
43
+ && Object.values(lin).every(Number.isSafeInteger);
44
+ const localGoal = (goal, lemma) => {
45
+ if (lemma.slice === undefined)
46
+ return goal;
47
+ if (!Array.isArray(lemma.slice) || !Array.from(lemma.slice).every(i => Number.isSafeInteger(i) && i >= 0 && i < goal.hyps.length))
48
+ return undefined;
49
+ return { ...goal, hyps: lemma.slice.map(i => goal.hyps[i]) };
50
+ };
51
+ function independent(goal, lemmas, until) {
52
+ if (!alive(until) || !Array.isArray(lemmas))
53
+ return false;
54
+ for (const lemma of Array.from(lemmas)) {
55
+ if (!alive(until) || !lemma || !Array.isArray(lemma.when) || !Array.from(lemma.when).every(validLin) || !validLin(lemma.cut))
56
+ return false;
57
+ if (independentHit(goal, lemma))
58
+ continue;
59
+ const local = localGoal(goal, lemma);
60
+ if (!local || !check(local, lemma.tree, proofPath(lemma.when, lemma.cut)))
61
+ return false;
62
+ rememberIndependent(goal, lemma);
63
+ }
64
+ return alive(until);
65
+ }
66
+ const legalSplit = (tree, path) => !path.has(canon(tree.split)) && !Object.keys(tree.split).every(key => key === '1');
67
+ function checked(goal, tree, path, until) {
68
+ const visit = (t, p, context) => {
69
+ if (!alive(until))
70
+ return false;
71
+ const own = marked.get(t);
72
+ if (own) {
73
+ if (!sameGoal(goal, own.goal) || !independent(own.goal, own.lemmas, until))
74
+ return false;
75
+ context = own;
76
+ }
77
+ if ('leaf' in t)
78
+ return slicedCheck(goal, t, lemmaPath(context.lemmas, p)) && alive(until);
79
+ return legalSplit(t, p) && visit(t.yes, put(p, t.split, true), context)
80
+ && visit(t.no, put(p, t.split, false), context);
81
+ };
82
+ return visit(tree, path, { goal, lemmas: [] }) && alive(until);
83
+ }
84
+ /** Check each marked region with its independent cuts, and unmarked regions
85
+ * with the ordinary kernel rules. A nested mark replaces its parent's list. */
86
+ export function checkConditionalProof(goal, tree, path = new Map()) {
87
+ try {
88
+ return checked(goal, tree, path, Infinity);
89
+ }
90
+ catch {
91
+ return false;
92
+ }
93
+ }
94
+ const decide = (lin, path) => {
95
+ if (!Object.values(lin).every(Number.isSafeInteger))
96
+ return undefined;
97
+ if (Object.keys(lin).every(name => name === '1'))
98
+ return (lin['1'] ?? 0) <= 0;
99
+ const direct = path.get(canon(lin))?.holds;
100
+ if (direct !== undefined)
101
+ return direct;
102
+ const opposite = path.get(canonFlip(lin))?.holds;
103
+ return opposite === undefined ? undefined : !opposite;
104
+ };
105
+ // No solver fallback. Only exact base rows and identical square/product offsets
106
+ // move; a target constant-positive row is already a complete refutation.
107
+ function leaf(from, to, tree, before, after, until) {
108
+ if (!alive(until))
109
+ return undefined;
110
+ const old = rowsAt(from, before), rows = rowsAt(to, after), at = new Map();
111
+ for (const [i, row] of rows.entries()) {
112
+ if (!alive(until))
113
+ return undefined;
114
+ if (Number.isSafeInteger(row.lin['1']) && (row.lin['1'] ?? 0) > 0
115
+ && Object.entries(row.lin).every(([name, value]) => name === '1' || value === 0))
116
+ return { leaf: [{ kind: 'input', index: i }] };
117
+ const id = canon(row.lin);
118
+ if (!at.has(id))
119
+ at.set(id, i);
120
+ }
121
+ const index = (i) => {
122
+ if (!Number.isSafeInteger(i) || i < 0)
123
+ return undefined;
124
+ return i >= old.length ? i - old.length + rows.length : at.get(canon(old[i].lin));
125
+ };
126
+ const steps = [], products = [];
127
+ for (const step of tree.leaf) {
128
+ if (!alive(until))
129
+ return undefined;
130
+ if (step.kind !== 'input')
131
+ steps.push(step);
132
+ else {
133
+ const i = index(step.index);
134
+ if (i === undefined)
135
+ return undefined;
136
+ steps.push({ ...step, index: i });
137
+ }
138
+ }
139
+ for (const [a, b] of tree.products ?? []) {
140
+ if (!alive(until))
141
+ return undefined;
142
+ const i = index(a), j = index(b);
143
+ if (i === undefined || j === undefined)
144
+ return undefined;
145
+ products.push([i, j]);
146
+ }
147
+ return alive(until) ? { leaf: steps, ...(tree.products ? { products } : {}), ...(tree.squares ? { squares: tree.squares } : {}) } : undefined;
148
+ }
149
+ function ordinary(from, to, tree, before, after, until) {
150
+ if (!alive(until))
151
+ return undefined;
152
+ if ('leaf' in tree)
153
+ return leaf(from, to, tree, before, after, until);
154
+ const known = decide(tree.split, after);
155
+ if (known !== undefined)
156
+ return ordinary(from, to, known ? tree.yes : tree.no, put(before, tree.split, known), after, until);
157
+ const yes = ordinary(from, to, tree.yes, put(before, tree.split, true), put(after, tree.split, true), until);
158
+ const no = yes && ordinary(from, to, tree.no, put(before, tree.split, false), put(after, tree.split, false), until);
159
+ return yes && no ? { split: tree.split, yes, no } : undefined;
160
+ }
161
+ // Preserve mapped source hypotheses, including their order and duplicates. A
162
+ // removed hypothesis is dispensable only if exact independent-proof transport
163
+ // succeeds without it (e.g. an inserted guard is now an explicit entry atom).
164
+ // Even an unsliced source proof gets an explicit slice in an enlarged target.
165
+ function retarget(from, to, lemmas, scope, until, onlyMissing = false) {
166
+ if (!independent(from, lemmas, until))
167
+ return undefined;
168
+ const out = [], target = targetIndex(to, until);
169
+ if (!target)
170
+ return undefined;
171
+ for (const lemma of lemmas) {
172
+ if (!alive(until))
173
+ return undefined;
174
+ const source = localGoal(from, lemma), slice = [], texts = [];
175
+ const missing = [];
176
+ for (const hyp of source.hyps) {
177
+ if (!alive(until))
178
+ return undefined;
179
+ const text = JSON.stringify(hyp), i = target.get(text);
180
+ texts.push(text);
181
+ if (i === undefined)
182
+ missing.push(hyp);
183
+ else
184
+ slice.push(i);
185
+ }
186
+ // A fully mapped transport does not consume the entry scope. Include the
187
+ // matched content as well as the slice, so different goals cannot alias.
188
+ // Missing hypotheses and root collection remain exactly scope-sensitive.
189
+ const key = onlyMissing && !missing.length ? JSON.stringify([slice, texts]) : undefined;
190
+ const cached = key === undefined ? undefined : retargetHit(lemma, key);
191
+ if (cached) {
192
+ out.push(cached);
193
+ continue;
194
+ }
195
+ const when = [...lemma.when];
196
+ for (const atom of !onlyMissing || missing.length ? scope.values() : []) {
197
+ if (!alive(until))
198
+ return undefined;
199
+ when.push(atom.holds ? atom.lin : flip(atom.lin));
200
+ }
201
+ const path = proofPath(when, lemma.cut);
202
+ if (missing.length) {
203
+ const supplied = new Set([...scope.values()].map(atom => canon(atom.holds ? atom.lin : flip(atom.lin))));
204
+ for (const hyp of missing) {
205
+ if (!alive(until))
206
+ return undefined;
207
+ const rows = facts(hyp, path);
208
+ // Only explicit scope atoms may replace removed hypotheses. A missing
209
+ // unrelated hypothesis is not silently discarded, even if unused.
210
+ if (!rows.length || !rows.every(row => supplied.has(canon(row))))
211
+ return undefined;
212
+ }
213
+ }
214
+ const local = { ...to, hyps: slice.map(i => to.hyps[i]) };
215
+ const tree = ordinary(source, local, lemma.tree, proofPath(lemma.when, lemma.cut), path, until);
216
+ if (!tree || !alive(until) || !check(local, tree, proofPath(when, lemma.cut)) || !alive(until))
217
+ return undefined;
218
+ const moved = { when, cut: lemma.cut, tree, slice };
219
+ if (key !== undefined)
220
+ rememberRetarget(lemma, key, moved);
221
+ out.push(moved);
222
+ }
223
+ return alive(until) ? out : undefined;
224
+ }
225
+ /** Retarget an annotated or mixed proof, without turning cuts into split trees.
226
+ * before/after are ordinary entry paths, not paths already augmented by cuts. */
227
+ export function transportConditionalProof(from, to, tree, before, after, until) {
228
+ try {
229
+ const visit = (t, a, b, source, target) => {
230
+ if (!alive(until))
231
+ return undefined;
232
+ const own = marked.get(t);
233
+ if (own) {
234
+ const lemmas = retarget(own.goal, to, own.lemmas, b, until, true);
235
+ if (!lemmas)
236
+ return undefined;
237
+ source = own;
238
+ target = { goal: to, lemmas };
239
+ }
240
+ let out;
241
+ if ('leaf' in t)
242
+ out = leaf(source.goal, to, t, lemmaPath(source.lemmas, a), lemmaPath(target.lemmas, b), until);
243
+ else {
244
+ const known = decide(t.split, b);
245
+ if (known !== undefined)
246
+ out = visit(known ? t.yes : t.no, put(a, t.split, known), b, source, target);
247
+ else {
248
+ const yes = visit(t.yes, put(a, t.split, true), put(b, t.split, true), source, target);
249
+ const no = yes && visit(t.no, put(a, t.split, false), put(b, t.split, false), source, target);
250
+ if (yes && no)
251
+ out = { split: t.split, yes, no };
252
+ }
253
+ }
254
+ // If pruning exposed a separately marked child, do not overwrite it.
255
+ if (out && own && !marked.has(out))
256
+ markConditionalProof(to, out, target.lemmas);
257
+ return out;
258
+ };
259
+ const out = visit(tree, before, after, { goal: from, lemmas: [] }, { goal: to, lemmas: [] });
260
+ return out && checked(to, out, after, until) ? out : undefined;
261
+ }
262
+ catch {
263
+ return undefined;
264
+ }
265
+ }
266
+ /** Finalize at the empty root. Each local list is guarded by its entry path so
267
+ * exporting it cannot add assumptions to unrelated ordinary siblings. Every
268
+ * leaf is then reindexed, even if it did not originally carry metadata. */
269
+ export function collectConditionalCertificate(goal, tree, until) {
270
+ try {
271
+ const lemmas = [];
272
+ const collect = (t, path) => {
273
+ if (!alive(until))
274
+ return false;
275
+ const own = marked.get(t);
276
+ if (own) {
277
+ const cuts = retarget(own.goal, goal, own.lemmas, path, until);
278
+ if (!cuts)
279
+ return false;
280
+ for (const cut of cuts) {
281
+ if (!alive(until))
282
+ return false;
283
+ lemmas.push(cut);
284
+ }
285
+ }
286
+ return 'leaf' in t || legalSplit(t, path) && collect(t.yes, put(path, t.split, true)) && collect(t.no, put(path, t.split, false));
287
+ };
288
+ if (!collect(tree, new Map()))
289
+ return undefined;
290
+ const finish = (t, path, context) => {
291
+ if (!alive(until))
292
+ return undefined;
293
+ context = marked.get(t) ?? context;
294
+ if ('leaf' in t)
295
+ return leaf(context.goal, goal, t, lemmaPath(context.lemmas, path), lemmaPath(lemmas, path), until);
296
+ const yes = finish(t.yes, put(path, t.split, true), context);
297
+ const no = yes && finish(t.no, put(path, t.split, false), context);
298
+ return yes && no ? { split: t.split, yes, no } : undefined;
299
+ };
300
+ const out = finish(tree, new Map(), { goal, lemmas: [] });
301
+ return out && alive(until) && checkGoalLemmas(goal, out, lemmas) && alive(until) ? { tree: out, lemmas } : undefined;
302
+ }
303
+ catch {
304
+ return undefined;
305
+ }
306
+ }
@@ -0,0 +1,14 @@
1
+ import { type C, type Machine } from '../machine.js';
2
+ import { type CountCert, type CountDecl, type Goal, type Path, type Tree } from '../../kernel/kernel.js';
3
+ import { type RowPair } from './row-cases.js';
4
+ import { type FieldDemand } from './field-demand.js';
5
+ type Packet = Pick<CountCert, 'reads' | 'insts'>;
6
+ type Options = {
7
+ claim?: C;
8
+ pairs?: RowPair[];
9
+ conditional?: boolean;
10
+ fieldDemand?: FieldDemand;
11
+ fieldSource?: (g: Goal, c: Packet, claim: C, p: Path, end: number) => Tree | undefined;
12
+ };
13
+ export declare function deriveCountClosure(m: Machine, ds: CountDecl[], g: Goal, cert: Packet, root: Path, until: number, trace: object[], options?: Options): Tree | undefined;
14
+ export {};