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,640 @@
1
+ // Checked field-ceiling cuts suggested by a counted predicate and a carried
2
+ // set's per-member bound. The suggestion is untrusted; every cut is reconstructed
3
+ // and the complete result is checked against the original counted goal.
4
+ import { n, same, walkOnce, x } from "../machine.js";
5
+ import { canon, columnTerm, linear, substC, substE, truth } from "../../kernel/kernel.js";
6
+ import { countsForReads } from "../count-facts.js";
7
+ import { sub } from "../solver.js";
8
+ import { liftCountProxies } from "./lift-count-proxies.js";
9
+ import { deriveCountClosure } from "./derivability-closure.js";
10
+ import { hasConditionalProof } from "./conditional-proof.js";
11
+ import { piecesAt, sourceScalarContext, sourceWitnessContext, walkLive } from "./live-dependencies.js";
12
+ import { carriedCase } from "./carried-case.js";
13
+ import { foldPath } from "./fold-path.js";
14
+ import { fieldCase } from "./field-case.js";
15
+ import { booleanCore } from "./boolean-core.js";
16
+ import { fieldDemandAt, beginFieldAttempt, endFieldAttempt } from "./field-demand.js";
17
+ import { selectCountBounds } from "../count-select.js";
18
+ import { activeWriteRow, proveRowCases, rowPairs } from "./row-cases.js";
19
+ import { proveNaturalCases } from "./natural-cases.js";
20
+ import { activeCarried, copiesRows } from "./action-guard.js";
21
+ import { atPoint, clock, pastWall, withDeadline } from "../../kernel/prove.js";
22
+ import { REFUTED } from "../../kernel/cdcl.js";
23
+ import { wallTree, check, refuteFast, grow, rowsAt, widen, modelSequence } from "./ops.js";
24
+ // Bridge only cells used by the incoming path, including cells hidden inside
25
+ // natural-subtraction columns. Other count terms are already rewritten in g.
26
+ export function countBindingsAt(reads, root) {
27
+ const columns = new Set([...root.values()].flatMap(p => Object.keys(p.lin)));
28
+ for (const name of columns) {
29
+ const e = columnTerm(name);
30
+ if (e)
31
+ walkOnce(e, (o) => {
32
+ if (o.op === 'var')
33
+ columns.add(o.name);
34
+ if (o.op === 'get')
35
+ for (const key of Object.keys(linear(o, root) ?? {}))
36
+ columns.add(key);
37
+ });
38
+ }
39
+ return reads.flatMap(([id, map, key]) => { const cell = { op: 'get', map, key }, lin = linear(cell, root); return lin && Object.keys(lin).some(k => columns.has(k)) ? [same(x(id), cell)] : []; });
40
+ }
41
+ export function fieldBounds(m, ds, cert, options = {}) {
42
+ const key = x('$member'), bounds = [];
43
+ for (const [name, q] of Object.entries(m.step.flags ?? {}))
44
+ if (q.op === 'all')
45
+ walkOnce(substC(q.c, { vars: { [q.key]: key }, maps: {}, flags: {} }), (c) => {
46
+ const next = c.op === '<' && c.b?.op === 'add' && c.b.b.op === 'const' && c.b.b.value === 1 ? c.b.a : undefined;
47
+ const pairs = c.op === '=' ? [[c.a, c.b], [c.b, c.a]] : c.op === '<=' ? [[c.a, c.b]] : next ? [[c.a, next]] : [];
48
+ for (const [left, right] of pairs)
49
+ if (right && left?.op === 'get' && JSON.stringify(left).includes('"name":"$member"') && !JSON.stringify(right).includes('"name":"$member"'))
50
+ bounds.push({ a: left, b: right, guard: { op: '=', a: x(`?${name}`), b: { op: 'const', value: 1 } } });
51
+ });
52
+ const out = [], seen = new Set(), pre = new Set(cert.reads.filter(r => !r[1].endsWith('#')).map(r => r[0]));
53
+ for (const c of countsForReads(ds, cert.reads) ?? [])
54
+ if (pre.has(c.v))
55
+ walkOnce(substE(c.body, { vars: { [c.key]: key }, maps: {}, flags: {} }), (p) => {
56
+ if (p.op !== '<' || p.a?.op !== 'get' || JSON.stringify(p.b).includes('"name":"$member"'))
57
+ return;
58
+ for (const b of bounds)
59
+ if (JSON.stringify(b.a) === JSON.stringify(p.a)) {
60
+ const guard = p.b.op === 'get' ? { op: 'and', xs: [b.guard, { op: '<', a: p.b.key, b: c.below }] } : b.guard;
61
+ const candidate = { op: '<=', a: { op: 'if', c: guard, then: p.b, else: { op: 'const', value: 0 } }, b: b.b }, id = JSON.stringify(candidate);
62
+ if (!seen.has(id)) {
63
+ seen.add(id);
64
+ out.push(candidate);
65
+ }
66
+ }
67
+ });
68
+ const explicit = new Set();
69
+ if (options.claim)
70
+ walkLive(options.claim, options.path ?? new Map(), (o) => { if (o.op === 'get')
71
+ explicit.add(JSON.stringify(o)); });
72
+ const targets = new Set(explicit), twins = cert.reads.filter(r => r[1].endsWith('#')).map(([v, map, key]) => [v, map.slice(0, -1), key]);
73
+ for (const c of countsForReads(ds, twins) ?? [])
74
+ walkOnce(substE(c.body, { vars: { [c.key]: key }, maps: {}, flags: {} }), (p) => {
75
+ if (p.op === '<' && bounds.some(b => JSON.stringify(b.a) === JSON.stringify(p.a)))
76
+ targets.add(JSON.stringify(p.b));
77
+ });
78
+ // Old input-key values used by writes also need a ceiling: otherwise a
79
+ // post-count core can omit the actor's own pre-state version entirely.
80
+ walkOnce(m.step.maps, (o) => { if (o.op === 'get' && o.key.op === 'var' && m.inputs.includes(o.key.name))
81
+ targets.add(JSON.stringify(o)); });
82
+ const focused = out.filter(c => c.op === '<=' && c.a.op === 'if' && targets.has(JSON.stringify(c.a.then)));
83
+ return (focused.length ? focused : out).sort((a, b) => Number(b.op === '<=' && b.a.op === 'if' && explicit.has(JSON.stringify(b.a.then))) - Number(a.op === '<=' && a.a.op === 'if' && explicit.has(JSON.stringify(a.a.then)))).slice(0, 4);
84
+ }
85
+ // A candidate only: the source checker must still prove its count is empty.
86
+ export function emptyCountCandidate(m, cert, inst) {
87
+ if (inst.unit !== 0 || inst.terms.length !== 1 || inst.terms[0][0] !== 1)
88
+ return false;
89
+ const read = cert.reads.find(r => r[0] === inst.terms[0][1]);
90
+ if (!read?.[1].endsWith('#'))
91
+ return false;
92
+ const count = m.counts?.[read[1].slice(0, -1)];
93
+ if (!count)
94
+ return false;
95
+ let fields = false;
96
+ walkOnce(count.body, (o) => { if (o.op === '<' && o.a.op === 'get' && o.b.op === 'get')
97
+ fields = true; });
98
+ return fields;
99
+ }
100
+ export const fieldBoundLemmas = (...args) => wallTree('proposal-control', () => {
101
+ const [, , g, , until, trace, , path = new Map(), options = {}] = args;
102
+ if ((clock() >= until || pastWall())) {
103
+ trace.push({ stage: 'source-budget-exhausted' });
104
+ return undefined;
105
+ }
106
+ const cached = options.memo?.get(g, path, until);
107
+ if (cached && (options.deferCheck || check(g, cached, path))) {
108
+ trace.push({ stage: 'source-memo', scope: 'field' });
109
+ return cached;
110
+ }
111
+ const tree = fieldBoundLemmasImpl(...args);
112
+ if (tree && !hasConditionalProof(tree))
113
+ options.memo?.put(g, path, tree);
114
+ return tree;
115
+ });
116
+ function fieldBoundLemmasImpl(m, ds, g, cert, until, trace, onModel, root = new Map(), options = {}) {
117
+ const pointOptions = { deferCheck: !!options.deferCheck };
118
+ const seq = modelSequence(root), ctx = [], path = new Map(root), lemmas = [];
119
+ const accept = (tree, path) => tree && (options.deferCheck || check(g, tree, path)) ? tree : undefined;
120
+ const verdict = (tree) => ({ checked: !options.deferCheck && !!tree, ...(options.deferCheck ? { proposed: !!tree, deferred: true } : {}) });
121
+ const rowPlan = options.splitRows !== false ? rowPairs(m, cert, root, options.fieldClaim).slice(0, 2) : [];
122
+ let arithmetic;
123
+ const postMaps = new Set(cert.reads.filter(r => r[1].endsWith('#')).map(r => r[1])), postIds = new Set(cert.reads.filter(r => r[1].endsWith('#')).map(r => r[0]));
124
+ if (options.closure !== false) {
125
+ const closed = deriveCountClosure(m, ds, g, cert, root, until, trace, { claim: options.fieldClaim, pairs: rowPlan, conditional: options.conditional, fieldDemand: options.fieldDemand ?? fieldDemandAt(root), fieldSource: (source, proposal, claim, path, end) => fieldBoundLemmas(m, ds, source, proposal, end, trace, onModel, path, { ...options, closure: false, splitRows: false, deferCheck: true, fieldClaim: claim }) });
126
+ if (closed)
127
+ return closed;
128
+ }
129
+ // Split a packed field's guarded column domain once, outside the row cases.
130
+ // Without it each row repeats the same range reconstruction for copied keys.
131
+ if (rowPlan.length && !postIds.size && options.fieldClaim) {
132
+ const rows = new Set(rowPlan.flatMap(([key]) => key.op === 'var' ? [key.name] : [])), columns = new Set();
133
+ walkOnce(options.fieldClaim, (o) => { if (o.op === 'get' && o.key.op !== 'var')
134
+ walkOnce(o.key, (k) => { if (k.op === 'var' && !rows.has(k.name) && !m.inputs.includes(k.name) && !m.params.includes(k.name))
135
+ columns.add(k.name); }); });
136
+ const candidates = [];
137
+ walkOnce(options.fieldClaim, (c) => { if (!candidates.length && c.op === '<' && c.a.op === 'var' && columns.has(c.a.name) && truth(c, root) === undefined)
138
+ candidates.push(c); });
139
+ let domain = candidates[0];
140
+ if (domain && domain.a.op === 'var') {
141
+ const column = domain.a.name, strides = [];
142
+ walkOnce(options.fieldClaim, (o) => { if (o.op !== 'get')
143
+ return; const key = linear(o.key, root); if (!key || key[column] !== 1)
144
+ return; for (const row of rows) {
145
+ const width = Math.abs(key[row] ?? 0);
146
+ if (Number.isInteger(width) && width > 1)
147
+ strides.push(width);
148
+ } });
149
+ if (strides.length)
150
+ domain = { op: '<', a: domain.a, b: n(Math.min(...strides)) };
151
+ }
152
+ if (domain && truth(domain, root) === undefined) {
153
+ const a = linear(domain.a, root), b = linear(domain.b, root);
154
+ if (a && b) {
155
+ const split = sub(a, b);
156
+ split['1'] = (split['1'] ?? 0) + 1;
157
+ const id = canon(split), negative = new Map(root).set(id, { lin: split, holds: false }), positive = new Map(root).set(id, { lin: split, holds: true });
158
+ let no;
159
+ const meter = beginFieldAttempt(domain, root, options.fieldDemand ?? fieldDemandAt(root), 'field-column-domain');
160
+ try {
161
+ no = fieldCase(m, g, cert, options.fieldClaim, negative, Math.min(until, clock() + 50), e => trace.push(e), 0, options.memo);
162
+ }
163
+ finally {
164
+ endFieldAttempt(meter, !!no);
165
+ }
166
+ if (no) {
167
+ const yes = fieldBoundLemmas(m, ds, g, cert, until, trace, onModel, positive, options), tree = yes ? { split, yes, no } : undefined;
168
+ trace.push({ stage: 'field-column-domain', domain, proposed: !!tree });
169
+ return tree && (options.deferCheck || check(g, tree, root)) ? tree : undefined;
170
+ }
171
+ }
172
+ }
173
+ }
174
+ if (rowPlan.length && !postIds.size) {
175
+ trace.push({ stage: 'field-row-pairs', pairs: rowPlan });
176
+ return proveRowCases(g, root, rowPlan, until, (branch, end) => {
177
+ const cached = options.memo?.get(g, branch, end);
178
+ if (cached) {
179
+ trace.push({ stage: 'source-memo', scope: 'field-row' });
180
+ return cached;
181
+ }
182
+ const tree = fieldCase(m, g, cert, options.fieldClaim, branch, end, e => trace.push(e), 0, options.memo) ?? fieldBoundLemmas(m, ds, g, cert, end, trace, onModel, branch, { ...options, splitRows: false, deferCheck: true });
183
+ if (tree)
184
+ options.memo?.put(g, branch, tree);
185
+ return tree;
186
+ }, e => trace.push(e), { deferToCaller: !!options.deferCheck, splitEqual: copiesRows(m, root) });
187
+ }
188
+ const base = g.hyps.slice(0, g.hyps.length - cert.insts.length).flatMap(c => piecesAt(c, root)).filter(c => { let post = false; walkLive(c, root, (o) => { if (o.op === 'get' && postMaps.has(o.map) || o.op === 'var' && postIds.has(o.name))
189
+ post = true; }); return !post; });
190
+ const cs = new Map((countsForReads(ds, cert.reads) ?? []).map(c => [c.v, { ...c, body: substE(c.body, { vars: { [c.key]: x('$member') }, maps: {}, flags: {} }) }]));
191
+ const view = (e) => {
192
+ if (e.op !== 'if' || e.then.op !== 'const' || e.then.value !== 1 || e.else.op !== 'const' || e.else.value !== 0)
193
+ return e;
194
+ const c = e.c;
195
+ if (c.op === '=' && c.a.op === 'get' && c.b.op === 'const' && c.b.value === 1)
196
+ return c.a;
197
+ if (c.op === '=' && c.b.op === 'get' && c.a.op === 'const' && c.a.value === 1)
198
+ return c.b;
199
+ if (c.op === '<=' && c.a.op === 'const' && c.a.value === 1 && c.b.op === 'get')
200
+ return c.b;
201
+ if (c.op === '<' && c.a.op === 'const' && c.a.value === 0 && c.b.op === 'get')
202
+ return c.b;
203
+ return e;
204
+ };
205
+ const signature = (e) => {
206
+ const pred = e.op === 'get' ? { op: '=', a: e, b: { op: 'const', value: 1 } } : e.op === 'if' && e.then.op === 'const' && e.then.value === 1 && e.else.op === 'const' && e.else.value === 0 ? e.c : undefined;
207
+ const atoms = (c) => c.op === 'and' ? c.xs.flatMap(atoms) : [JSON.stringify(c)];
208
+ return pred ? JSON.stringify([...new Set(atoms(pred))].sort()) : undefined;
209
+ };
210
+ const narrow = (good) => {
211
+ const keep = cert.insts.flatMap((inst, i) => {
212
+ if (inst.unit !== 0 || inst.terms.length !== 2 || inst.terms.some(t => postIds.has(t[1])))
213
+ return [];
214
+ const positive = inst.terms.find(t => t[0] === 1), negative = inst.terms.find(t => t[0] === -1), a = positive && cs.get(positive[1]), b = negative && cs.get(negative[1]);
215
+ if (!a || !b)
216
+ return [];
217
+ const as = signature(a.body), bs = signature(b.body), same = as !== undefined && as === bs, input = view(a.body).op === 'get' && (m.inputMaps ?? []).includes(view(a.body).map);
218
+ let target = false;
219
+ walkOnce(b.body, (o) => { if (o.op === '<' && JSON.stringify(o.b) === JSON.stringify(good.a.op === 'if' ? good.a.then : good.a))
220
+ target = true; });
221
+ return same || input && target ? [i] : [];
222
+ });
223
+ const wanted = new Set(keep.map(i => cert.insts[i].wit)), all = new Set(cert.insts.map(i => i.wit));
224
+ const weaken = (c) => {
225
+ if (c.op === 'and') {
226
+ const xs = c.xs.flatMap(c => { const n = weaken(c); return n ? [n] : []; });
227
+ return xs.length ? { op: 'and', xs } : undefined;
228
+ }
229
+ let other = false, own = false;
230
+ walkLive(c, root, (o) => { if (o.op === 'var' && all.has(o.name)) {
231
+ if (wanted.has(o.name))
232
+ own = true;
233
+ else
234
+ other = true;
235
+ } });
236
+ return other && !own ? undefined : c;
237
+ };
238
+ return [...base.flatMap(c => { const n = weaken(c); return n ? [n] : []; }), ...keep.map(i => g.hyps[g.hyps.length - cert.insts.length + i])];
239
+ };
240
+ const solve = (goal, limit, finish = false) => {
241
+ const budget = limit, started = clock(), q = seq(goal, Math.min(until, clock() + budget));
242
+ trace.push({ stage: 'field-query', hyps: goal.hyps.length, ms: Math.round(clock() - started), limit: budget, outcome: 'tree' in q ? 'tree' : q.stuck, core: q.core?.length, ...(!('tree' in q) && q.stuck === 'sat' ? { model: q.witness } : {}) });
243
+ if ('tree' in q)
244
+ return q.tree;
245
+ if (q.stuck === 'sat' && q.witness)
246
+ options.onSourceModel?.(goal, q.witness, root);
247
+ if (finish && q.stuck === 'sat' && q.witness && onModel?.(goal, q.witness))
248
+ return undefined;
249
+ const reduced = q.why === REFUTED && q.core?.length ? { ...goal, hyps: q.core.map(i => goal.hyps[i]) } : finish ? goal : undefined;
250
+ if (!reduced)
251
+ return undefined;
252
+ const before = atPoint.on;
253
+ atPoint.on = !(root.size > 0 && !finish && q.core && q.core.length <= 8);
254
+ try {
255
+ const deadline = Math.min(until, clock() + (finish ? Math.max(0, until - clock()) : 250));
256
+ if (!finish && root.size > 0 && q.core && q.core.length <= 8) {
257
+ const start = clock(), folded = foldPath(reduced, root), out = grow(folded.goal, root, { nodes: 0, until: Math.min(deadline, clock() + 25), units: false });
258
+ const lifted = 'tree' in out ? withDeadline(deadline, () => widen(folded.goal, reduced, out.tree, folded.opaque, root, root)) : undefined;
259
+ trace.push({ stage: 'folded-core', aliases: Object.keys(folded.opaque).length, ms: Math.round(clock() - start), sourceChecked: 'tree' in out, lifted: !!lifted });
260
+ if (lifted)
261
+ return withDeadline(deadline, () => widen(reduced, goal, lifted, {}, root, root));
262
+ const direct = booleanCore(reduced, root, new Set(cert.insts.map(i => i.wit)), deadline, trace, pointOptions);
263
+ if (direct)
264
+ return withDeadline(deadline, () => widen(reduced, goal, direct, {}, root, root));
265
+ const contextual = sourceWitnessContext(goal, reduced, new Set(cert.insts.map(i => i.wit)), root);
266
+ if (contextual !== reduced && clock() < deadline) {
267
+ const extra = contextual.hyps.slice(reduced.hyps.length);
268
+ trace.push({ stage: 'witness-context', added: extra.length, extra: extra.flatMap(c => piecesAt(c, root)) });
269
+ const expanded = booleanCore(contextual, root, new Set(cert.insts.map(i => i.wit)), deadline, trace, pointOptions);
270
+ if (expanded)
271
+ return withDeadline(deadline, () => widen(contextual, goal, expanded, {}, root, root));
272
+ }
273
+ }
274
+ const split = !finish && root.size > 0 && q.core && q.core.length <= 8 ? carriedCase(reduced, root, m.inputMaps ?? [], new Set(cert.insts.map(i => i.wit)), deadline, trace) : undefined;
275
+ if (split)
276
+ return withDeadline(deadline, () => widen(reduced, goal, split, {}, root, root));
277
+ const out = grow(reduced, root, { nodes: 0, until: deadline, units: root.size > 0 && !!q.core && q.core.length <= 8 });
278
+ return 'tree' in out ? withDeadline(deadline, () => widen(reduced, goal, out.tree, {}, root, root)) : undefined;
279
+ }
280
+ finally {
281
+ atPoint.on = before;
282
+ }
283
+ };
284
+ for (let i = 0; i < cert.reads.length; i++)
285
+ for (let j = i + 1; j < cert.reads.length; j++) {
286
+ const a = cert.reads[i], b = cert.reads[j];
287
+ if (a[1] !== b[1] || truth({ op: '=', a: a[2], b: b[2] }, root) !== true)
288
+ continue;
289
+ for (const [left, right] of [[a[0], b[0]], [b[0], a[0]]]) {
290
+ if ((clock() >= until || pastWall()))
291
+ break;
292
+ const split = sub({ [left]: 1 }, { [right]: 1 });
293
+ if (path.get(canon(split))?.holds === true)
294
+ continue;
295
+ const noPath = new Map(path).set(canon(split), { lin: split, holds: false }), begun = clock();
296
+ trace.push({ stage: 'read-alias-start', left, right });
297
+ const meter = beginFieldAttempt({ op: '<=', a: x(left), b: x(right) }, path, options.fieldDemand ?? fieldDemandAt(root), 'read-alias');
298
+ let emitted = false;
299
+ try {
300
+ const bindings = g.hyps.filter(c => c.op === '=' && [[c.a, c.b], [c.b, c.a]].some(([v, e]) => v?.op === 'var' && [left, right].includes(v.name) && e?.op === 'get' && e.map === a[1]));
301
+ if (bindings.length !== 2)
302
+ continue;
303
+ const small = { name: 'equal-key count reads', hyps: bindings }, out = refuteFast(rowsAt(small, noPath), 256);
304
+ const local = out?.kind === 'refuted' ? { leaf: out.steps } : undefined;
305
+ const no = local && check(small, local, noPath) ? liftCountProxies(small, g, local, [], noPath, noPath, undefined, options) : undefined, accepted = !!no && (options.deferCheck || check(g, no, noPath));
306
+ emitted = accepted;
307
+ trace.push({ stage: 'read-alias', left, right, ms: Math.round(clock() - begun), checked: !options.deferCheck && accepted, ...(options.deferCheck ? { proposed: accepted, deferred: true } : {}) });
308
+ if (accepted && no) {
309
+ lemmas.push({ split, no });
310
+ ctx.push({ op: '<=', a: x(left), b: x(right) });
311
+ path.set(canon(split), { lin: split, holds: true });
312
+ }
313
+ }
314
+ finally {
315
+ endFieldAttempt(meter, emitted);
316
+ }
317
+ }
318
+ }
319
+ if ((clock() >= until || pastWall()))
320
+ return undefined;
321
+ const ceilings = fieldBounds(m, ds, cert, { claim: options.fieldClaim, path: root }), domains = [];
322
+ for (const good of ceilings)
323
+ if (good.op === '<=' && good.a.op === 'if') {
324
+ const parts = good.a.c.op === 'and' ? good.a.c.xs : [good.a.c];
325
+ for (const c of parts)
326
+ if (c.op === '<' && !domains.some(d => JSON.stringify(c) === JSON.stringify(d)))
327
+ domains.push(c);
328
+ }
329
+ for (const domain of domains) {
330
+ if (truth(domain, path) !== undefined || (clock() >= until || pastWall()))
331
+ continue;
332
+ if (domain.op !== '<')
333
+ continue;
334
+ const a = linear(domain.a, root), b = linear(domain.b, root);
335
+ if (!a || !b)
336
+ continue;
337
+ const split = sub(a, b);
338
+ split['1'] = (split['1'] ?? 0) + 1;
339
+ const noPath = new Map(path).set(canon(split), { lin: split, holds: false }), start = clock();
340
+ const meter = beginFieldAttempt(domain, path, options.fieldDemand ?? fieldDemandAt(root), 'field-domain');
341
+ let emitted = false;
342
+ try {
343
+ const part = { ...g, hyps: [...g.hyps.slice(0, g.hyps.length - cert.insts.length), ...ctx, { op: 'not', x: domain }] }, direct = refuteFast(rowsAt(part, root), 256);
344
+ const proof = direct?.kind === 'refuted' ? { leaf: direct.steps } : solve(part, 50);
345
+ const lifted = proof && widen(part, g, proof, {}, root, noPath), no = accept(lifted, noPath);
346
+ emitted = !!no;
347
+ trace.push({ stage: 'field-domain', domain, direct: direct?.kind === 'refuted', ms: Math.round(clock() - start), ...verdict(no) });
348
+ if (no) {
349
+ lemmas.push({ split, no });
350
+ ctx.push(domain);
351
+ path.set(canon(split), { lin: split, holds: true });
352
+ }
353
+ }
354
+ finally {
355
+ endFieldAttempt(meter, emitted);
356
+ }
357
+ }
358
+ const finishField = () => {
359
+ if (postIds.size || !options.fieldClaim || !lemmas.length || (clock() >= until || pastWall()))
360
+ return undefined;
361
+ const offset = g.hyps.length - cert.insts.length, part = { ...g, hyps: [...g.hyps.slice(0, offset), ...ctx, ...g.hyps.slice(offset)] };
362
+ const field = fieldCase(m, part, cert, options.fieldClaim, path, Math.min(until, clock() + 150), e => trace.push(e));
363
+ let tree = field && widen(part, g, field, {}, path, path);
364
+ trace.push({ stage: 'field-ceiling-conclusion', facts: lemmas.length, proposed: !!tree });
365
+ if (tree) {
366
+ for (const { split, no } of [...lemmas].reverse())
367
+ tree = { split, yes: tree, no };
368
+ if (options.deferCheck || check(g, tree, root))
369
+ return tree;
370
+ }
371
+ return undefined;
372
+ };
373
+ const activeCeilings = new Set(ceilings.flatMap(good => good.op === '<=' && good.a.op === 'if' && truth(good.a.c, path) === true ? [JSON.stringify([good.a.then, good.b])] : []));
374
+ for (const good of ceilings) {
375
+ if (good.op === '<=' && good.a.op === 'if' && truth(good.a.c, path) !== true && activeCeilings.has(JSON.stringify([good.a.then, good.b])))
376
+ continue;
377
+ if (good.op !== '<=' || (clock() >= until || pastWall()))
378
+ break;
379
+ if (truth(good, path) === true)
380
+ continue;
381
+ const a = linear(good.a, path), b = linear(good.b, path);
382
+ if (!a || !b)
383
+ continue;
384
+ const split = sub(a, b), noPath = new Map(path).set(canon(split), { lin: split, holds: false }), start = clock();
385
+ let no;
386
+ const meter = beginFieldAttempt(good, path, options.fieldDemand ?? fieldDemandAt(root), 'field-bound');
387
+ try {
388
+ const facts = narrow(good), part = { ...g, hyps: [...facts, ...ctx, { op: 'not', x: good }] }, proof = solve(part, 250), lifted = proof && widen(part, g, proof, {}, root, noPath);
389
+ no = accept(lifted, noPath);
390
+ }
391
+ finally {
392
+ endFieldAttempt(meter, !!no);
393
+ }
394
+ trace.push({ stage: 'field-bound', bound: good, ms: Math.round(clock() - start), proved: !!no, ...verdict(no) });
395
+ if (no) {
396
+ lemmas.push({ split, no });
397
+ ctx.push(good);
398
+ path.set(canon(split), { lin: split, holds: true });
399
+ const field = finishField();
400
+ if (field)
401
+ return field;
402
+ }
403
+ }
404
+ if ((clock() >= until || pastWall()))
405
+ return undefined;
406
+ const field = finishField();
407
+ if (field)
408
+ return field;
409
+ {
410
+ const carriedMaps = activeCarried(m, root), countMaps = new Set(cert.reads.map(r => r[1])), countIds = new Set(cert.reads.map(r => r[0])), wits = new Set(cert.insts.map(i => i.wit)), raw = g.hyps.slice(0, g.hyps.length - cert.insts.length);
411
+ const pieces = (c) => piecesAt(c, root);
412
+ const alias = (inst) => { if (inst.unit !== 0 || inst.terms.length !== 2 || inst.terms.some(([, id]) => postIds.has(id)))
413
+ return false; const a = cs.get(inst.terms[0][1]), b = cs.get(inst.terms[1][1]); return !!a && !!b && JSON.stringify(view(a.body)) === JSON.stringify(view(b.body)); };
414
+ const requestFrame = (inst) => inst.terms.length === 2 && inst.terms.some(([, id]) => postIds.has(id)) && inst.terms.some(([, id]) => { const c = cs.get(id), b = c && view(c.body); return b?.op === 'get' && (m.inputMaps ?? []).includes(b.map); });
415
+ const frame = (inst) => {
416
+ if (inst.terms.length !== 2)
417
+ return false;
418
+ const [a, b] = inst.terms.map(([, id]) => cert.reads.find(r => r[0] === id));
419
+ return !!a && !!b && a[1].replace(/#$/, '') === b[1].replace(/#$/, '') && a[1].endsWith('#') !== b[1].endsWith('#') && JSON.stringify(a[2]) === JSON.stringify(b[2]);
420
+ };
421
+ const copyFrame = (inst) => {
422
+ if (inst.terms.length !== 2)
423
+ return false;
424
+ const [a, b] = inst.terms.map(([, id]) => cert.reads.find(r => r[0] === id));
425
+ return !!a && !!b && a[1].replace(/#$/, '') === b[1].replace(/#$/, '') && a[1].endsWith('#') !== b[1].endsWith('#') && JSON.stringify(a[2]) !== JSON.stringify(b[2]);
426
+ };
427
+ const selected = (inst) => {
428
+ if (inst.unit !== 0)
429
+ return false;
430
+ if (emptyCountCandidate(m, cert, inst))
431
+ return true;
432
+ if (rowPlan.length && inst.terms.some(([, id]) => postIds.has(id)))
433
+ return false;
434
+ const carried = inst.terms.some(([k, id]) => { const c = cs.get(id), b = c && view(c.body); return k === 1 && b?.op === 'get' && carriedMaps.has(b.map); });
435
+ // Let row cases prove row-dependent carried bounds where their equalities
436
+ // are known. The complete pool remains available in every row child.
437
+ if (rowPlan.length && carried && inst.terms.length === 2) {
438
+ const localRows = new Set(rowPlan.flatMap(([key]) => key.op === 'var' ? [key.name] : [])), positive = inst.terms.find(([k]) => k === 1), negative = inst.terms.find(([k]) => k === -1);
439
+ const source = positive && cs.get(positive[1]), target = negative && cs.get(negative[1]), input = source && view(source.body);
440
+ let local = false;
441
+ if (target)
442
+ walkLive(target.body, root, (o) => { if (o.op === 'var' && localRows.has(o.name) || o.op === 'get' && (m.inputMaps ?? []).includes(o.map) && input?.op === 'get' && o.map !== input.map)
443
+ local = true; });
444
+ if (local)
445
+ return false;
446
+ }
447
+ if (inst.terms.length === 3 || inst.terms.length === 5 && carried)
448
+ return true;
449
+ if (inst.terms.length !== 2)
450
+ return false;
451
+ const [a, b] = inst.terms.map(([, id]) => cert.reads.find(r => r[0] === id));
452
+ return carried || alias(inst) || frame(inst) || copyFrame(inst) || !!a && !!b && a[1].endsWith('#') && b[1].endsWith('#') || requestFrame(inst) || !!a && !!b && a[1].endsWith('#') && !b[1].endsWith('#');
453
+ };
454
+ const pure = (c) => { let skip = false; walkLive(c, root, (o) => { if (o.op === 'get' || o.op === 'var' && wits.has(o.name))
455
+ skip = true; }); return !skip; };
456
+ const maps = Object.fromEntries([...countMaps].map(map => [map, (key) => { const r = cert.reads.find(r => r[1] === map && JSON.stringify(r[2]) === JSON.stringify(key)); return r ? x(r[0]) : { op: 'get', map, key }; }]));
457
+ const flags = Object.fromEntries(Object.keys(m.step.flags ?? {}).map(name => [name, { op: 'flag', name }]));
458
+ const numericBase = raw.map(c => substC(c, { vars: {}, maps, flags })).flatMap(c => pieces(c)).filter(pure);
459
+ // The source path can mention count cells inside a guard or natural
460
+ // subtraction. Keep their explicit bindings instead of disconnecting it
461
+ // from the rewritten numeric hypotheses. Source transport checks every
462
+ // required equality and its read-domain guard.
463
+ const numericBindings = countBindingsAt(cert.reads, root);
464
+ const finish = () => {
465
+ // The outer row pass adds only pre-state cuts. Defer arithmetic closure to
466
+ // the exhaustive row children, where post-state constraints are available.
467
+ if (rowPlan.length || arithmetic || (clock() >= until || pastWall()))
468
+ return;
469
+ const numeric = { ...g, hyps: [...numericBase, ...numericBindings, ...ctx.filter(pure)] }, begun = clock(), q = seq(numeric, Math.min(until, clock() + 200));
470
+ trace.push({ stage: 'count-arithmetic', hyps: numeric.hyps.length, ms: Math.round(clock() - begun), outcome: 'tree' in q ? 'tree' : q.stuck, core: q.core?.length, ...(!('tree' in q) && q.stuck === 'sat' ? { model: q.witness } : {}) });
471
+ let proof = 'tree' in q ? q.tree : undefined;
472
+ if (!proof && !('tree' in q) && q.why === REFUTED && q.core?.length) {
473
+ const reduced = { ...numeric, hyps: q.core.map(i => numeric.hyps[i]) }, out = grow(reduced, root, { nodes: 0, until: Math.min(until, clock() + 400), units: false });
474
+ if ('tree' in out)
475
+ proof = widen(reduced, numeric, out.tree, {}, root, root);
476
+ }
477
+ if (!proof && !('tree' in q) && q.stuck === 'unknown')
478
+ proof = proveNaturalCases(numeric, root, Math.min(until, clock() + 400), trace, pointOptions);
479
+ const lifted = proof && liftCountProxies(numeric, g, proof, cert.reads, root, path, event => trace.push({ stage: 'count-transport', ...event }), options);
480
+ if (lifted)
481
+ arithmetic = lifted; // Checked here, or deferred to the composing caller.
482
+ };
483
+ const diagonalCopy = (inst) => {
484
+ if (!copyFrame(inst))
485
+ return false;
486
+ const source = inst.terms.map(([, id]) => cert.reads.find(r => r[0] === id)).find(r => r && !r[1].endsWith('#')), key = source?.[2];
487
+ if (key?.op === 'tup')
488
+ return JSON.stringify(key.a) === JSON.stringify(key.b);
489
+ const decl = source && m.counts?.[source[1]];
490
+ return key?.op === 'add' && !!decl?.lo && key.a.op === 'mul' && key.a.k === decl.base && JSON.stringify(key.a.a) === JSON.stringify(key.b);
491
+ };
492
+ const writer = activeWriteRow(m, root);
493
+ const afterRead = (inst) => inst.terms.map(([, id]) => cert.reads.find(r => r[0] === id)).find(r => r?.[1].endsWith('#'));
494
+ const coordinates = (read) => {
495
+ const key = read[2], d = m.counts?.[read[1].replace(/#$/, '')];
496
+ if (key.op === 'tup')
497
+ return [key.a, key.b];
498
+ if (d?.lo && key.op === 'add' && key.a.op === 'mul' && key.a.k === d.base)
499
+ return [key.a.a, key.b];
500
+ return [key];
501
+ };
502
+ const rank = (inst) => {
503
+ if (emptyCountCandidate(m, cert, inst))
504
+ return -1;
505
+ const after = afterRead(inst), keys = after && coordinates(after);
506
+ if (writer && frame(inst) && keys?.every(key => truth(same(key, x(writer)), root) === false))
507
+ return -0.75;
508
+ if (copyFrame(inst)) {
509
+ if (writer && keys) {
510
+ const before = inst.terms.map(([, id]) => cert.reads.find(r => r[0] === id)).find(r => r && !r[1].endsWith('#')), oldKeys = before && coordinates(before);
511
+ let moved = false, matched = !!oldKeys && oldKeys.length === keys.length;
512
+ for (let i = 0; matched && i < keys.length; i++) {
513
+ const changed = truth(same(keys[i], x(writer)), root), old = oldKeys[i];
514
+ if (changed === false && truth(same(old, keys[i]), root) !== true)
515
+ return 0.5;
516
+ if (changed === true) {
517
+ moved = true;
518
+ matched = old.op === 'var' && m.inputs.includes(old.name) && old.name !== writer;
519
+ }
520
+ if (changed === undefined)
521
+ matched = false;
522
+ }
523
+ if (matched && moved)
524
+ return -0.5;
525
+ }
526
+ return diagonalCopy(inst) ? 0.5 : 0;
527
+ }
528
+ return requestFrame(inst) || alias(inst) ? 0 : inst.terms.length === 2 && inst.terms.every(([, id]) => postIds.has(id)) ? 1 : frame(inst) ? 2 : inst.terms.length === 3 && inst.terms.every(([, id]) => !postIds.has(id)) ? 3 : inst.terms.length === 5 ? 4 : inst.terms.length === 2 ? 5 : 6;
529
+ };
530
+ finish();
531
+ const ordered = [...cert.insts.entries()].sort((a, b) => rank(a[1]) - rank(b[1])), excluded = new Set(), confirmed = new Set();
532
+ const boundOf = (index) => { const h = g.hyps[raw.length + index]; return h?.op === 'or' && h.xs[0]?.op === '<=' ? h.xs[0] : undefined; };
533
+ let fallbackSelection = false;
534
+ const pick = () => {
535
+ const pool = ordered.filter(([index, inst]) => { const good = boundOf(index); return !excluded.has(index) && !confirmed.has(index) && selected(inst) && !!good && truth(good, path) !== true; });
536
+ // A count-sensitive guard can need shared pre-state support before its
537
+ // row children have a closing projection. Retain that ranked fallback;
538
+ // count-free row skeletons need no eager pre-state lemma chain.
539
+ if (rowPlan.length) {
540
+ fallbackSelection = true;
541
+ return numericBindings.length ? pool : [];
542
+ }
543
+ const selection = selectCountBounds({ ...g, hyps: [...numericBase, ...numericBindings, ...ctx.filter(pure)] }, pool.map(([index]) => ({ index, bound: boundOf(index) })), until, { path: root, queryMs: 200, trace: event => trace.push({ ...event, stage: 'needed-lemmas' }) });
544
+ if (!selection)
545
+ return [];
546
+ fallbackSelection = selection.fallback;
547
+ const needed = new Set(selection.indices);
548
+ // Selected row-independent identities are cheap prerequisites for harder
549
+ // copy bounds. Derive them first; the selection's rank order is unchanged.
550
+ const identity = (inst) => inst.terms.length === 3 && inst.terms.every(([, id]) => !postIds.has(id));
551
+ return pool.filter(([index]) => needed.has(index)).sort((a, b) => Number(identity(b[1])) - Number(identity(a[1])));
552
+ };
553
+ let queue = pick(), cursor = 0;
554
+ while (cursor < queue.length) {
555
+ const [index, inst] = queue[cursor++];
556
+ if (arithmetic || (clock() >= until || pastWall()))
557
+ break;
558
+ if (!selected(inst))
559
+ continue;
560
+ const h = g.hyps[raw.length + index];
561
+ if (h?.op !== 'or' || h.xs[0]?.op !== '<=')
562
+ continue;
563
+ const good = h.xs[0];
564
+ if (truth(good, path) === true)
565
+ continue;
566
+ const split = Object.fromEntries(inst.terms.map(([n, v]) => [v, n])), noPath = new Map(path).set(canon(split), { lin: split, holds: false });
567
+ let no;
568
+ const meter = beginFieldAttempt(good, path, options.fieldDemand ?? fieldDemandAt(root), 'field-count');
569
+ try {
570
+ const relevant = raw.flatMap(c => pieces(c)).filter(c => { let skip = false; walkLive(c, root, (o) => { if (o.op === 'get' && countMaps.has(o.map) || o.op === 'var' && (countIds.has(o.name) || wits.has(o.name) && o.name !== inst.wit))
571
+ skip = true; }); return !skip; });
572
+ const part = { ...g, hyps: [...relevant, ...ctx, h, { op: 'not', x: good }] }, started = clock();
573
+ let proof;
574
+ if (inst.terms.length === 3 && inst.terms.every(([, id]) => !postIds.has(id))) {
575
+ const tiny = { name: g.name, hyps: [h, { op: 'not', x: good }] }, fast = booleanCore(tiny, root, new Set([inst.wit]), Math.min(until, clock() + 30), trace, pointOptions);
576
+ if (fast)
577
+ proof = widen(tiny, part, fast, {}, root, root);
578
+ trace.push({ stage: 'direct-partition', index, proved: !!proof });
579
+ }
580
+ const directCopy = (copyFrame(inst) || frame(inst));
581
+ if (directCopy) {
582
+ // The matching diagonal gets the existing row window; reserve time for
583
+ // arithmetic closure instead of spending it on unrelated copy pairs.
584
+ const copyUntil = diagonalCopy(inst) ? Math.max(clock(), until - 200) : Math.min(until, started + 230);
585
+ const tiny = sourceScalarContext({ name: g.name, hyps: raw }, { name: g.name, hyps: [h, { op: 'not', x: good }] }, new Set([...m.params, ...m.inputs, ...Object.keys(m.state)]), root), fast = booleanCore(tiny, root, new Set([inst.wit]), copyUntil, trace, { ...pointOptions, model: false, extended: true, scalarGuards: true });
586
+ if (fast)
587
+ proof = widen(tiny, part, fast, {}, root, root);
588
+ trace.push({ stage: 'direct-copy', index, terms: inst.terms, reads: inst.terms.map(([, id]) => cert.reads.find(r => r[0] === id)), proved: !!proof });
589
+ }
590
+ // Reallocate the existing 80 + 150 ms copy window, rather than extend it.
591
+ proof ??= solve(part, directCopy ? Math.max(0, 230 - (clock() - started)) : 150);
592
+ const lifted = proof && withDeadline(Math.min(until, started + (directCopy ? 230 : 150)), () => widen(part, g, proof, {}, root, noPath));
593
+ no = accept(lifted, noPath);
594
+ trace.push({ stage: 'count-lemma', index, arity: inst.terms.length, terms: inst.terms, ms: Math.round(clock() - started), ...verdict(no) });
595
+ }
596
+ finally {
597
+ endFieldAttempt(meter, !!no);
598
+ }
599
+ if (no) {
600
+ confirmed.add(index);
601
+ lemmas.push({ split, no });
602
+ ctx.push(good);
603
+ path.set(canon(split), { lin: split, holds: true });
604
+ finish();
605
+ }
606
+ else {
607
+ excluded.add(index);
608
+ // An unknown oracle already supplied the full ranked fallback. Remove
609
+ // the failed proposal from that queue rather than repeat the same query.
610
+ if (!fallbackSelection) {
611
+ queue = pick();
612
+ cursor = 0;
613
+ }
614
+ }
615
+ }
616
+ finish();
617
+ }
618
+ if ((clock() >= until || pastWall()))
619
+ return undefined;
620
+ const part = { ...g, hyps: [...g.hyps, ...ctx] }, start = clock();
621
+ let tree = arithmetic;
622
+ if (!tree && rowPlan.length) {
623
+ trace.push({ stage: 'row-pairs', pairs: rowPlan });
624
+ tree = proveRowCases(g, path, rowPlan, until, (branch, end) => fieldBoundLemmas(m, ds, g, cert, end, trace, onModel, branch, { splitRows: false, deferCheck: true, memo: options.memo, onSourceModel: options.onSourceModel }), e => trace.push(e), { deferToCaller: true, splitEqual: copiesRows(m, root) });
625
+ }
626
+ if (!tree) {
627
+ const proof = solve(part, Math.max(150, until - clock() - 200), true);
628
+ tree = proof && widen(part, g, proof, {}, root, path);
629
+ }
630
+ trace.push({ stage: 'field-bound-conclusion', facts: lemmas.length, ms: Math.round(clock() - start), proved: !!tree });
631
+ if (!tree)
632
+ return undefined;
633
+ for (const { split, no } of [...lemmas].reverse())
634
+ tree = { split, yes: tree, no };
635
+ // The composing caller must check deferred cuts and children in its full tree.
636
+ if (options.deferCheck)
637
+ return tree;
638
+ const checked = check(g, tree, root);
639
+ return checked ? tree : undefined;
640
+ }
@@ -0,0 +1,5 @@
1
+ import { type C, type Machine } from '../machine.js';
2
+ import { type CountCert, type Goal, type Path, type Tree } from '../../kernel/kernel.js';
3
+ import { type SourceProofMemo } from './source-memo.js';
4
+ export declare function fieldDirection(claim: C | undefined, path: Path): Record<string, number> | undefined;
5
+ export declare function fieldCase(m: Machine, g: Goal, cert: Pick<CountCert, 'reads' | 'insts'>, claim: C | undefined, path: Path, until: number, trace?: (e: Record<string, unknown>) => void, naturalDepth?: number, memo?: SourceProofMemo): Tree | undefined;