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,247 @@
1
+ // Untrusted, opt-in counting proposals. No hypothesis here is an axiom: callers
2
+ // pass insts to countedGoal/mcGoal, prove that goal, then checkCounted/checkMC.
3
+ // Venn regions are a search representation, NOT new declared count variables.
4
+ import { x } from "./machine.js";
5
+ import { substE } from "../kernel/kernel.js";
6
+ const stable = (t) => JSON.stringify(t, (_, v) => v && typeof v === 'object' && !Array.isArray(v)
7
+ ? Object.fromEntries(Object.entries(v).sort(([a], [b]) => a.localeCompare(b))) : v);
8
+ const namesIn = (t, out = new Set()) => {
9
+ if (typeof t === 'string')
10
+ out.add(t);
11
+ else if (t && typeof t === 'object')
12
+ for (const [key, v] of Object.entries(t)) {
13
+ out.add(key);
14
+ namesIn(v, out);
15
+ }
16
+ return out;
17
+ };
18
+ const fresh = (used, prefix) => {
19
+ let i = 0;
20
+ while (used.has(`${prefix}${i}`))
21
+ i++;
22
+ const name = `${prefix}${i}`;
23
+ used.add(name);
24
+ return name;
25
+ };
26
+ const limit = (n, fallback, cap) => n === undefined ? fallback : Number.isSafeInteger(n) && n >= 0 ? Math.min(n, cap) : 0;
27
+ function condition(c, atoms) {
28
+ if (c.op === 'flag' || c.op === 'all')
29
+ return undefined;
30
+ if (c.op === 'not') {
31
+ const a = condition(c.x, atoms);
32
+ return a === undefined ? undefined : { op: 'not', a };
33
+ }
34
+ if (c.op === 'and' || c.op === 'or') {
35
+ const xs = c.xs.map(v => condition(v, atoms));
36
+ return xs.some(v => v === undefined) ? undefined : { op: c.op, xs: xs };
37
+ }
38
+ const key = stable(c);
39
+ atoms.set(key, c);
40
+ return { atom: key };
41
+ }
42
+ function booleanBody(e, atoms) {
43
+ if (e.op === 'const')
44
+ return e.value === 0 ? false : e.value === 1 ? true : undefined;
45
+ if (e.op !== 'if')
46
+ return undefined;
47
+ const c = condition(e.c, atoms), a = booleanBody(e.then, atoms), b = booleanBody(e.else, atoms);
48
+ if (c === undefined || a === undefined || b === undefined)
49
+ return undefined;
50
+ return { op: 'or', xs: [{ op: 'and', xs: [c, a] }, { op: 'and', xs: [{ op: 'not', a: c }, b] }] };
51
+ }
52
+ const evaluate = (f, values) => typeof f === 'boolean' ? f
53
+ : 'atom' in f ? values.get(f.atom)
54
+ : f.op === 'not' ? !evaluate(f.a, values)
55
+ : f.op === 'and' ? f.xs.every(x => evaluate(x, values)) : f.xs.some(x => evaluate(x, values));
56
+ const abs = (n) => n < 0n ? -n : n;
57
+ const gcd = (a, b) => { while (b) {
58
+ const r = a % b;
59
+ a = b;
60
+ b = r;
61
+ } return abs(a); };
62
+ const primitive = (a, b) => {
63
+ const d = [...a, ...b].reduce(gcd, 0n);
64
+ if (d > 1n) {
65
+ for (let i = 0; i < a.length; i++)
66
+ a[i] = a[i] / d;
67
+ for (let i = 0; i < b.length; i++)
68
+ b[i] = b[i] / d;
69
+ }
70
+ };
71
+ /** Propose Boolean identities, subset bounds and quorum intersection instances.
72
+ * At most 2^maxAtoms rows per same-bound group. Non-Boolean counts and over-wide
73
+ * groups are reported, never silently treated as sets. Atom valuations deliberately
74
+ * overapproximate arithmetic: the kernel, not this table, decides their feasibility.
75
+ */
76
+ export function proposeCountFacts(counts, context = [], options = {}) {
77
+ const out = { insts: [], regions: [], skipped: [] };
78
+ const used = namesIn([counts, context]);
79
+ for (const s of options.reserved ?? [])
80
+ used.add(s);
81
+ const key = fresh(used, 'region$key$'), atomCap = limit(options.maxAtoms, 8, 12);
82
+ const countCap = limit(options.maxCounts, 64, 128), factCap = limit(options.maxFacts, 256, 4096);
83
+ const groups = new Map(), seen = new Set();
84
+ for (const c of counts) {
85
+ if (seen.has(c.v)) {
86
+ out.skipped.push({ count: c.v, reason: 'duplicate count name' });
87
+ continue;
88
+ }
89
+ seen.add(c.v);
90
+ const bound = stable(c.below), group = groups.get(bound) ?? [];
91
+ group.push(c);
92
+ groups.set(bound, group);
93
+ }
94
+ const emitted = new Set();
95
+ const emit = (cs, coeff, below) => {
96
+ if (out.insts.length >= factCap || coeff.some(c => abs(c) > BigInt(Number.MAX_SAFE_INTEGER)))
97
+ return;
98
+ const terms = cs.flatMap((c, i) => coeff[i] ? [[Number(coeff[i]), c.v]] : []);
99
+ const unit = Number(coeff[cs.length] ?? 0n);
100
+ if (!terms.length && unit === 0)
101
+ return;
102
+ const signature = stable([below, terms, unit]);
103
+ if (emitted.has(signature))
104
+ return;
105
+ emitted.add(signature);
106
+ out.insts.push({ wit: fresh(used, 'region$w$'), below, terms, unit });
107
+ };
108
+ for (const group of groups.values()) {
109
+ const atoms = new Map(), cs = [], fs = [];
110
+ for (const c of group) {
111
+ if (cs.length >= countCap) {
112
+ out.skipped.push({ count: c.v, reason: 'count cap' });
113
+ continue;
114
+ }
115
+ const local = new Map(), f = booleanBody(substE(c.body, { vars: { [c.key]: x(key) }, maps: {}, flags: {} }), local);
116
+ if (f === undefined) {
117
+ out.skipped.push({ count: c.v, reason: 'non-Boolean body' });
118
+ continue;
119
+ }
120
+ if (new Set([...atoms.keys(), ...local.keys()]).size > atomCap) {
121
+ out.skipped.push({ count: c.v, reason: 'atom cap' });
122
+ continue;
123
+ }
124
+ for (const [a, c] of local)
125
+ atoms.set(a, c);
126
+ cs.push(c);
127
+ fs.push(f);
128
+ }
129
+ if (!cs.length)
130
+ continue;
131
+ const below = cs[0].below, keys = [...atoms.keys()];
132
+ const rows = Array.from({ length: 2 ** keys.length }, (_, bits) => {
133
+ const values = new Map(keys.map((k, i) => [k, !!(bits & (1 << i))]));
134
+ return fs.map(f => Number(evaluate(f, values)));
135
+ });
136
+ out.regions.push({ below, atoms: [...atoms.values()], counts: cs.map(c => c.v), rows });
137
+ // Integer column elimination gives every independent identity among declared
138
+ // counts and the universe. Track exact coefficients; no floating-point rank.
139
+ const basis = [];
140
+ for (let j = 0; j <= cs.length; j++) {
141
+ const col = rows.map(row => BigInt(j === cs.length ? 1 : row[j]));
142
+ const coeff = Array.from({ length: cs.length + 1 }, (_, i) => i === j ? 1n : 0n);
143
+ for (const b of basis) {
144
+ const a = col[b.pivot], d = b.col[b.pivot];
145
+ if (!a)
146
+ continue;
147
+ for (let i = 0; i < col.length; i++)
148
+ col[i] = d * col[i] - a * b.col[i];
149
+ for (let i = 0; i < coeff.length; i++)
150
+ coeff[i] = d * coeff[i] - a * b.coeff[i];
151
+ primitive(col, coeff);
152
+ }
153
+ const pivot = col.findIndex(x => x !== 0n);
154
+ if (pivot < 0) {
155
+ emit(cs, coeff, below);
156
+ emit(cs, coeff.map(c => -c), below);
157
+ }
158
+ else
159
+ basis.push({ pivot, col, coeff });
160
+ }
161
+ for (let a = 0; a < cs.length; a++) {
162
+ const coeff = new Array(cs.length + 1).fill(0n);
163
+ coeff[a] = 1n;
164
+ coeff[cs.length] = -1n;
165
+ emit(cs, coeff, below);
166
+ for (let b = 0; b < cs.length; b++)
167
+ if (a !== b) {
168
+ if (rows.every(row => row[a] <= row[b])) {
169
+ const sub = new Array(cs.length + 1).fill(0n);
170
+ sub[a] = 1n;
171
+ sub[b] = -1n;
172
+ emit(cs, sub, below);
173
+ }
174
+ // The alternative is a common-member witness, not an unconditional bound.
175
+ if (a < b) {
176
+ const meet = new Array(cs.length + 1).fill(0n);
177
+ meet[a] = 1n;
178
+ meet[b] = 1n;
179
+ meet[cs.length] = -1n;
180
+ emit(cs, meet, below);
181
+ }
182
+ }
183
+ }
184
+ }
185
+ return out;
186
+ }
187
+ /** Ask for the existing distinct-key lower-bound rule at concrete member keys.
188
+ * The evaluator is supplied by the caller's model. It is only a heuristic: even
189
+ * a lying evaluator cannot bypass checkMC's pickHyp, which re-reads each body.
190
+ * Both scanning and certificate size are bounded; large universes are reported.
191
+ */
192
+ export function proposeCountPicks(counts, valueOf, options = {}) {
193
+ const picks = [], audit = [];
194
+ const maxBelow = limit(options.maxBelow, 256, 4096), maxKeys = limit(options.maxKeys, 32, 256);
195
+ for (const c of counts) {
196
+ const below = valueOf(c.below), abstract = valueOf(x(c.v));
197
+ const row = { count: c.v, below, abstract };
198
+ audit.push(row);
199
+ if (!Number.isSafeInteger(below) || below < 0 || below > maxBelow || !Number.isSafeInteger(abstract) || abstract < 0) {
200
+ row.reason = 'bound/value outside scan budget';
201
+ continue;
202
+ }
203
+ let actual = 0, selected = 0;
204
+ const keys = [];
205
+ for (let k = 0; k < below; k++) {
206
+ const term = { op: 'const', value: k };
207
+ const value = valueOf(substE(c.body, { vars: { [c.key]: term }, maps: {}, flags: {} }));
208
+ if (!Number.isSafeInteger(value) || value < 0 || !Number.isSafeInteger(actual + value)) {
209
+ row.reason = 'non-natural/unsafe body sum';
210
+ break;
211
+ }
212
+ actual += value;
213
+ if (value > 0 && keys.length < maxKeys && selected <= abstract) {
214
+ keys.push(term);
215
+ selected += value;
216
+ }
217
+ }
218
+ if (row.reason)
219
+ continue;
220
+ row.actual = actual;
221
+ if (selected > abstract)
222
+ picks.push([c.v, keys]);
223
+ else if (actual > abstract)
224
+ row.reason = 'pick key cap';
225
+ }
226
+ return { picks, audit };
227
+ }
228
+ /** Instantiate existing machine reads, including packed two-parameter keys.
229
+ * Mirrors the untrusted side of toCount; mcGoal/checkMC remain the authority.
230
+ * Returns undefined rather than guessing a decomposition the checker won't use.
231
+ */
232
+ export function countsForReads(decls, reads) {
233
+ const out = [];
234
+ for (const [v, map, term] of reads) {
235
+ const d = decls.find(d => d.map === map);
236
+ if (!d)
237
+ return undefined;
238
+ const base = d.base ?? 0, lo = d.lo ?? d.j;
239
+ if (base && !(term.op === 'add' && term.a.op === 'mul' && term.a.k === base))
240
+ return undefined;
241
+ const hi = base && term.op === 'add' && term.a.op === 'mul' ? term.a.a : { op: 'const', value: 0 };
242
+ const low = base && term.op === 'add' ? term.b : term;
243
+ const at = { vars: { [d.j]: hi, [lo]: low }, maps: {}, flags: {} };
244
+ out.push({ v, key: d.key, below: substE(d.below, at), body: substE(d.body, at) });
245
+ }
246
+ return out;
247
+ }
@@ -0,0 +1,8 @@
1
+ import { type Law, type Machine } from './machine.js';
2
+ import { type CountCert, type CountDecl, type Goal, type Instances } from '../kernel/kernel.js';
3
+ export type FocusedCountProof = {
4
+ instances: Instances;
5
+ certs: Record<string, CountCert>;
6
+ };
7
+ export declare const focusedCountRecoveryEnabled: (m: Machine, setting?: string | undefined) => boolean;
8
+ export declare function proveFocusedCounts(m: Machine, laws: Law[], law: Law, at: Instances, until: number, propose: (goal: Goal, decls: CountDecl[]) => Pick<CountCert, 'reads' | 'insts'>, trace?: (event: Record<string, unknown>) => void): FocusedCountProof | undefined;
@@ -0,0 +1,140 @@
1
+ // Untrusted recovery from an oversized counted goal. Each returned certificate
2
+ // is an ordinary counted proof, checked by the caller's existing kernel gate.
3
+ import { walkOnce, x } from "./machine.js";
4
+ import { countedObligations, mcGoal } from "../kernel/kernel.js";
5
+ import { clock, grow, pastWall } from "../kernel/prove.js";
6
+ import { env } from "./solver.js";
7
+ import { countLawSeeds, countRowBridges, fieldGuardSeeds, fieldLawBridges, focusCountCert, rowKeyNames } from "./count-focused.js";
8
+ import { seedCountWitnessFlags } from "./count-witness-flags.js";
9
+ import { finiteCountChoice, proveCountCases } from "./count-cases.js";
10
+ // Normal finite-input machines use the bounded recovery; 0 remains an explicit
11
+ // opt-out, while 1 also enables the legacy non-finite recovery experiment.
12
+ export const focusedCountRecoveryEnabled = (m, setting = env.GDP_COUNTS_FOCUS) => setting === '1' || setting !== '0' && !!finiteCountChoice(m);
13
+ export function proveFocusedCounts(m, laws, law, at, until, propose, trace) {
14
+ if (clock() >= until || pastWall() || !m.inputMaps?.length)
15
+ return undefined;
16
+ let clean = {};
17
+ for (const [name, terms] of Object.entries(at)) {
18
+ const l = laws.find(l => l.name === name), arity = l ? 1 + (l.keys?.length ?? 0) : 1, keep = [];
19
+ for (let i = 0; i < terms.length; i += arity) {
20
+ const tuple = terms.slice(i, i + arity);
21
+ let witness = false;
22
+ walkOnce(tuple, (o) => { if (o.op === 'var' && /^w(?:\$\d+|\?)/.test(o.name))
23
+ witness = true; });
24
+ if (!witness)
25
+ keep.push(...tuple);
26
+ }
27
+ clean[name] = keep;
28
+ }
29
+ if (law.forall) {
30
+ const tuple = [x(`${law.forall}#`), ...(law.keys ?? []).map(x)], had = clean[law.name] ?? [], arity = tuple.length;
31
+ if (!Array.from({ length: Math.floor(had.length / arity) }, (_, i) => JSON.stringify(had.slice(i * arity, (i + 1) * arity))).includes(JSON.stringify(tuple)))
32
+ clean[law.name] = [...had, ...tuple];
33
+ }
34
+ const names = [law.forall, ...law.keys ?? []], own = names.filter((n) => !!n).map((n, i) => x(i === 0 ? `${n}#` : n)), scalar = rowKeyNames(m, law);
35
+ let targetCounts = false;
36
+ walkOnce(law.holds, (o) => { if (o.op === 'get') {
37
+ if (o.key.op === 'var')
38
+ scalar.add(o.key.name);
39
+ if (m.counts?.[o.map])
40
+ targetCounts = true;
41
+ } });
42
+ const rows = own.filter((_, i) => scalar.has(names[i])), request = m.inputs.filter(name => Object.values(m.counts ?? {}).some(d => ![d.j, d.lo, d.key].includes(name) && JSON.stringify([d.below, d.body]).includes(`"name":${JSON.stringify(name)}`))).map(x);
43
+ const bridgeLaws = targetCounts ? laws.filter(l => (l.keys?.length ?? 0) === 0) : laws;
44
+ clean = countLawSeeds(m, laws, clean, rows.length ? rows : own, request, !targetCounts, { requestDiagonals: true });
45
+ clean = fieldLawBridges(m, bridgeLaws, clean, rows.length ? rows : own, request, own.length === 3 ? own[2] : undefined);
46
+ clean = countRowBridges(m, law, clean, own, request);
47
+ if (!targetCounts)
48
+ clean = fieldGuardSeeds(m, clean, [...rows, ...request, { op: 'const', value: 0 }]);
49
+ const index = laws.findIndex(l => l.name === law.name);
50
+ for (const carried of [undefined, ...m.inputMaps]) {
51
+ if (clock() >= until || pastWall())
52
+ return undefined;
53
+ let instances = clean;
54
+ const retained = new Map();
55
+ const proposal = (goal, decls) => focusCountCert(m, decls, propose(goal, decls), { carried, structural: false, frames: true, algebraic: true, requestFrames: true, copyFrames: true, targets: !targetCounts ? rows : undefined, claim: goal.hyps.at(-1) });
56
+ const packetsByInstances = new Map();
57
+ const build = () => {
58
+ const key = JSON.stringify(instances), had = packetsByInstances.get(key);
59
+ if (had)
60
+ return had;
61
+ const packets = countedObligations(m, laws, { [law.name]: instances }, index).map(({ goal, decls }) => ({ goal, decls, cert: retained.get(goal.name) ?? proposal(goal, decls) }));
62
+ packetsByInstances.set(key, packets);
63
+ return packets;
64
+ };
65
+ let packets = build();
66
+ const seeded = seedCountWitnessFlags(m, instances, packets, true);
67
+ if (seeded !== instances) {
68
+ instances = seeded;
69
+ packets = build();
70
+ }
71
+ const rowGoal = !targetCounts && own.length === 3;
72
+ const certs = {};
73
+ let complete = !rowGoal && !finiteCountChoice(m);
74
+ const quickUntil = Math.min(until, clock() + 1000);
75
+ for (const { goal, decls, cert } of rowGoal || finiteCountChoice(m) ? [] : packets) {
76
+ const g = mcGoal(goal.hyps, decls, cert);
77
+ if (!g) {
78
+ complete = false;
79
+ break;
80
+ }
81
+ const result = grow({ ...g, name: goal.name }, new Map(), { nodes: 0, until: quickUntil });
82
+ if (!('tree' in result)) {
83
+ complete = false;
84
+ break;
85
+ }
86
+ certs[goal.name] = { ...cert, tree: result.tree };
87
+ }
88
+ if (complete)
89
+ return { instances, certs };
90
+ if (clock() >= until || pastWall())
91
+ continue;
92
+ let cold = {};
93
+ cold = countLawSeeds(m, laws, cold, rows.length ? rows : own, request, !targetCounts, { requestDiagonals: true });
94
+ cold = fieldLawBridges(m, bridgeLaws, cold, rows.length ? rows : own, request, own.length === 3 ? own[2] : undefined);
95
+ if (law.forall)
96
+ cold[law.name] = [...cold[law.name] ?? [], ...own];
97
+ cold = countRowBridges(m, law, cold, own, request);
98
+ if (!targetCounts)
99
+ cold = fieldGuardSeeds(m, cold, [...rows, ...request, { op: 'const', value: 0 }]);
100
+ instances = cold;
101
+ packets = build();
102
+ const flags = seedCountWitnessFlags(m, instances, packets, true);
103
+ if (flags !== instances) {
104
+ instances = flags;
105
+ packets = build();
106
+ }
107
+ const step = packets.find(p => p.goal.name.endsWith('@step'));
108
+ if (!step)
109
+ continue;
110
+ trace?.({ stage: 'focused-packet', carried: carried ?? null, hyps: step.goal.hyps.length, insts: step.cert.insts.length, reads: step.cert.reads.length, instances: Object.fromEntries(Object.entries(instances).map(([name, terms]) => [name, terms.length])) });
111
+ // Still an untrusted proposal: the caller checks the final, transported
112
+ // certificate with checkMC/checkCountedLaws.
113
+ const result = proveCountCases(m, laws, instances, step.cert, until, next => {
114
+ instances = next;
115
+ return build().find(p => p.goal.name.endsWith('@step'));
116
+ }, { deferCheck: true, propose: proposal, saturate: true, queryMs: 200, trace, ...(!targetCounts && own.length === 3 ? { localModels: true, coreLimit: 16, units: false, corePointOnly: true } : {}) });
117
+ if (!result)
118
+ continue;
119
+ instances = result.instances;
120
+ retained.set(step.goal.name, result.cert);
121
+ packetsByInstances.clear();
122
+ packets = build();
123
+ trace?.({ stage: 'focused-step-certificate', before: step.cert.insts.length, after: result.cert.insts.length });
124
+ const combined = { [step.goal.name]: result.cert };
125
+ let closed = true;
126
+ for (const p of packets) {
127
+ if (p.goal.name === step.goal.name)
128
+ continue;
129
+ const g = mcGoal(p.goal.hyps, p.decls, p.cert), out = g && grow({ ...g, name: p.goal.name }, new Map(), { nodes: 0, until });
130
+ if (!out || !('tree' in out)) {
131
+ closed = false;
132
+ break;
133
+ }
134
+ combined[p.goal.name] = { ...p.cert, tree: out.tree };
135
+ }
136
+ if (closed)
137
+ return { instances, certs: combined };
138
+ }
139
+ return undefined;
140
+ }
@@ -0,0 +1,22 @@
1
+ export declare function countLawSeeds(m: Machine, laws: Law[], at: Instances, own: E[], request: E[], fromRequestOnly?: boolean, options?: {
2
+ requestDiagonals?: boolean;
3
+ }): Instances;
4
+ export declare function fieldGuardSeeds(m: Machine, at: Instances, keys: E[]): Instances;
5
+ export declare function rowKeyNames(m: Machine, law: Law): Set<string>;
6
+ export declare function countRowBridges(m: Machine, law: Law, at: Instances, own: E[], request: E[]): Instances;
7
+ export declare function independentCountInstances(m: Machine, laws: Law[], before: Instances, next: Instances, witnesses: string[]): Instances | undefined;
8
+ export declare function fieldLawBridges(m: Machine, laws: Law[], at: Instances, own: E[], request: E[], column?: E): Instances;
9
+ import { type C, type E, type Law, type Machine } from './machine.js';
10
+ import { type CountCert, type CountDecl, type Instances } from '../kernel/kernel.js';
11
+ export declare function copyCountSubset(m: Machine, cert: Pick<CountCert, 'reads' | 'insts'>): Pick<CountCert, 'reads' | 'insts'>;
12
+ export declare function demandCountCert(m: Machine, decls: CountDecl[], full: Pick<CountCert, 'reads' | 'insts'>, claim: C): Pick<CountCert, 'reads' | 'insts'>;
13
+ export declare function focusCountCert(m: Machine, decls: CountDecl[], full: Pick<CountCert, 'reads' | 'insts'>, options?: {
14
+ carried?: string;
15
+ structural?: boolean;
16
+ frames?: boolean;
17
+ algebraic?: boolean;
18
+ requestFrames?: boolean;
19
+ copyFrames?: boolean;
20
+ targets?: E[];
21
+ claim?: C;
22
+ }): Pick<CountCert, 'reads' | 'insts'>;