gdp-wired 0.45.0 → 0.47.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 (126) hide show
  1. package/README.md +61 -8
  2. package/dist/core/bitsets.d.ts +2 -0
  3. package/dist/core/bitsets.js +23 -0
  4. package/dist/core/bmc.js +6 -2
  5. package/dist/core/c/reader.d.ts +9 -1
  6. package/dist/core/c/reader.js +423 -31
  7. package/dist/core/calendar.d.ts +29 -0
  8. package/dist/core/calendar.js +82 -0
  9. package/dist/core/cli.d.ts +4 -0
  10. package/dist/core/cli.js +206 -40
  11. package/dist/core/compose.js +3 -1
  12. package/dist/core/counts.d.ts +4 -1
  13. package/dist/core/counts.js +314 -85
  14. package/dist/core/cycles.js +8 -1
  15. package/dist/core/cyclic.d.ts +1 -0
  16. package/dist/core/cyclic.js +13 -7
  17. package/dist/core/data-synth.d.ts +3 -0
  18. package/dist/core/data-synth.js +252 -0
  19. package/dist/core/data.d.ts +2 -1
  20. package/dist/core/data.js +223 -24
  21. package/dist/core/discover.js +3 -1
  22. package/dist/core/draft.d.ts +19 -0
  23. package/dist/core/draft.js +53 -0
  24. package/dist/core/dsl.d.ts +37 -3
  25. package/dist/core/dsl.js +167 -26
  26. package/dist/core/effect-schema.d.ts +90 -0
  27. package/dist/core/effect-schema.js +500 -0
  28. package/dist/core/effect.d.ts +4 -11
  29. package/dist/core/effect.js +3 -79
  30. package/dist/core/exists.d.ts +1 -0
  31. package/dist/core/exists.js +191 -16
  32. package/dist/core/extract-c.d.ts +2 -0
  33. package/dist/core/extract-c.js +169 -22
  34. package/dist/core/extract-check.d.ts +1 -0
  35. package/dist/core/extract-check.js +97 -6
  36. package/dist/core/extract-go-check.d.ts +1 -1
  37. package/dist/core/extract-go-check.js +26 -5
  38. package/dist/core/extract-go.d.ts +8 -0
  39. package/dist/core/extract-go.js +67 -6
  40. package/dist/core/extract-ir.d.ts +1 -0
  41. package/dist/core/extract-ir.js +15 -0
  42. package/dist/core/extract-jvm.d.ts +1 -1
  43. package/dist/core/extract-jvm.js +2 -0
  44. package/dist/core/extract-orm.d.ts +12 -0
  45. package/dist/core/extract-orm.js +244 -0
  46. package/dist/core/extract-print.js +20 -1
  47. package/dist/core/extract-py.d.ts +1 -0
  48. package/dist/core/extract-py.js +1 -1
  49. package/dist/core/extract-read.d.ts +26 -1
  50. package/dist/core/extract-read.js +14 -7
  51. package/dist/core/extract-rs.js +63 -15
  52. package/dist/core/extract-shapes.d.ts +1 -0
  53. package/dist/core/extract-shapes.js +84 -0
  54. package/dist/core/extract-sol.d.ts +10 -1
  55. package/dist/core/extract-sol.js +70 -22
  56. package/dist/core/extract-standalone.d.ts +3 -0
  57. package/dist/core/extract-standalone.js +221 -0
  58. package/dist/core/extract-step.d.ts +151 -1
  59. package/dist/core/extract-step.js +1232 -60
  60. package/dist/core/extract-swift.js +6 -2
  61. package/dist/core/extract.d.ts +9 -0
  62. package/dist/core/extract.js +77 -7
  63. package/dist/core/fiber-run.d.ts +5 -0
  64. package/dist/core/fiber-run.js +18 -0
  65. package/dist/core/infer.d.ts +4 -1
  66. package/dist/core/infer.js +918 -217
  67. package/dist/core/jvm/JavaReader.java +23 -6
  68. package/dist/core/jvm/Jvm.java +3 -0
  69. package/dist/core/jvm/KtReader.java +6 -5
  70. package/dist/core/laws.d.ts +10 -3
  71. package/dist/core/laws.js +226 -23
  72. package/dist/core/machine.d.ts +15 -1
  73. package/dist/core/machine.js +187 -15
  74. package/dist/core/nonterm.js +14 -1
  75. package/dist/core/orm/harness.js +61 -0
  76. package/dist/core/orm-fixtures/schema.prisma +5 -0
  77. package/dist/core/py/harness.py +183 -10
  78. package/dist/core/py/reader.py +234 -40
  79. package/dist/core/reader-fixtures/README.md +28 -0
  80. package/dist/core/reader-fixtures/c-693042.cpp +8 -0
  81. package/dist/core/reader-fixtures/go-685506.go +14 -0
  82. package/dist/core/reader-fixtures/jvm-724362.kt +8 -0
  83. package/dist/core/reader-fixtures/jvm-737430.kt +7 -0
  84. package/dist/core/reader-fixtures/jvm-765282.kt +3 -0
  85. package/dist/core/reader-fixtures/swift-735726.swift +15 -0
  86. package/dist/core/reader-fixtures/swift-735990.swift +15 -0
  87. package/dist/core/reader-fixtures/swift-739158.swift +4 -0
  88. package/dist/core/reader-fixtures/swift-744834.swift +15 -0
  89. package/dist/core/reader-fixtures/swift-746946.swift +31 -0
  90. package/dist/core/reader-fixtures/typescript-673062.ts.txt +2 -0
  91. package/dist/core/relation.d.ts +1 -1
  92. package/dist/core/relation.js +29 -2
  93. package/dist/core/relational.js +4 -2
  94. package/dist/core/rs/Cargo.toml +1 -1
  95. package/dist/core/rs/src/main.rs +466 -11
  96. package/dist/core/sample-cache.d.ts +8 -0
  97. package/dist/core/sample-cache.js +46 -0
  98. package/dist/core/security.d.ts +38 -0
  99. package/dist/core/security.js +88 -0
  100. package/dist/core/sol/harness.py +57 -4
  101. package/dist/core/sol-concrete.d.ts +23 -0
  102. package/dist/core/sol-concrete.js +195 -0
  103. package/dist/core/solver.js +3 -1
  104. package/dist/core/swift/Sources/gdp-swiftreader/main.swift +50 -7
  105. package/dist/core/system.js +1 -1
  106. package/dist/core/transfer.js +22 -9
  107. package/dist/core/translate.js +39 -10
  108. package/dist/core/watch.js +50 -5
  109. package/dist/interchange/format.js +11 -1
  110. package/dist/kernel/cdcl.d.ts +5 -0
  111. package/dist/kernel/cdcl.js +56 -9
  112. package/dist/kernel/epr.js +108 -26
  113. package/dist/kernel/kernel.d.ts +29 -1
  114. package/dist/kernel/kernel.js +325 -62
  115. package/dist/kernel/model-sequence.d.ts +3 -0
  116. package/dist/kernel/model-sequence.js +34 -0
  117. package/dist/kernel/prove.d.ts +10 -3
  118. package/dist/kernel/prove.js +332 -71
  119. package/dist/kernel/reader.d.ts +3 -0
  120. package/dist/kernel/reader.js +101 -14
  121. package/dist/kernel/share.d.ts +1 -0
  122. package/dist/kernel/share.js +27 -0
  123. package/dist/lean-kernel/emit.js +30 -37
  124. package/dist/lean-kernel/lean-terms.js +5 -0
  125. package/dist/lean-kernel/library.js +10 -10
  126. package/package.json +6 -1
@@ -1,6 +1,7 @@
1
- import { assumed, checkCountedLaws, countedObligations, mcGoal, scope, substC } from "../kernel/kernel.js";
2
- import { clock, counted, fullRate, grow, modelInstances, wallAt, widen, withWall } from "../kernel/prove.js";
3
- import { REFUTED, cdclStats, growCdcl } from "../kernel/cdcl.js";
1
+ import { assumed, checkCountedLaws, countedObligations, mcGoal, scope, substC, substE } from "../kernel/kernel.js";
2
+ import { brokenOf, clock, counted, fullRate, grow, modelInstances, wallAt, widen, withWall } from "../kernel/prove.js";
3
+ import { REFUTED, cdclStats } from "../kernel/cdcl.js";
4
+ import { modelSequence } from "../kernel/model-sequence.js";
4
5
  import { inferLaws } from "./infer.js";
5
6
  import { smallOf, trueSmall } from "./exists.js";
6
7
  import { env } from "./solver.js";
@@ -8,6 +9,7 @@ import { env } from "./solver.js";
8
9
  // profiles: each step goal left unknown or with a model past GDP_COUNTS_DUMP_MIN (300) hypotheses, the first GDP_COUNTS_DUMP_N (3))
9
10
  export const cprof = {};
10
11
  export const countsDump = {};
12
+ const tally = (k, n = 1) => { cprof[k] = (cprof[k] ?? 0) + n; };
11
13
  const ptime = (k, f) => { const t = performance.now(); try {
12
14
  return f();
13
15
  }
@@ -15,6 +17,9 @@ finally {
15
17
  cprof[k] = (cprof[k] ?? 0) + performance.now() - t;
16
18
  } };
17
19
  let dumped = 0;
20
+ // (GDP_COUNTS_LAZY=0: every instance and pick certFor proposes in the goal at once, as before; `wide`: the narrow
21
+ // instances at once, the widened goal's further instances and picks lazily)
22
+ const lazyMode = env.GDP_COUNTS_LAZY ?? 'wide', pool = Number(env.GDP_COUNTS_POOL ?? 1000);
18
23
  const v = (name) => ({ op: 'var', name }), k = (value) => ({ op: 'const', value });
19
24
  const json = (t) => JSON.stringify(t);
20
25
  const one = k(1), zero = k(0);
@@ -33,6 +38,8 @@ function reads(t0, map, out = new Map()) {
33
38
  return out;
34
39
  }
35
40
  const mentions = (t, name) => json(t).includes(`"name":${json(name)}`);
41
+ // Whether some object within t0 meets p (a condition nested in a term's `if` too).
42
+ const some = (t0, p) => !!t0 && typeof t0 === 'object' && (p(t0) || Object.values(t0).some(y => some(y, p)));
36
43
  const atoms = (c) => (c.op === 'and' || c.op === 'or' ? c.xs.flatMap(atoms) : c.op === 'not' ? atoms(c.x) : [c]);
37
44
  // --- candidates from the guard's shape -----------------------------------------------------------------------------------
38
45
  export function guardLaws(m) {
@@ -67,38 +74,120 @@ export function guardLaws(m) {
67
74
  }
68
75
  }
69
76
  for (const [mp, w] of Object.entries(m.step.maps ?? {})) {
70
- if ('each' in w || w.key.op !== 'var' || !m.inputs.includes(w.key.name))
77
+ // (the written key: a request's value t, or two, t and u, composed N·t + u (a relation's cell): a count of two
78
+ // parameters, read at the law's N·a + b with b < N)
79
+ if ('each' in w)
71
80
  continue;
72
- const t = w.key.name, written = inline({ op: '<=', a: one, b: w.value });
73
- const law = (name, cmp) => when(name, { op: '<=', a: one, b: { op: 'get', map: mp, key: v('a$c') } }, cmp);
81
+ const k0 = w.key, ps = k0.op === 'var' ? [k0.name] : k0.op === 'add' && k0.a.op === 'mul' && k0.a.k > 1 && k0.a.a.op === 'var' && k0.b.op === 'var' && k0.a.a.name !== k0.b.name ? [k0.a.a.name, k0.b.name] : [];
82
+ if (!ps.length || !ps.every(i => m.inputs.includes(i)))
83
+ continue;
84
+ const t = ps[0], base = ps.length === 2 && k0.op === 'add' && k0.a.op === 'mul' ? k0.a.k : 0, written = inline({ op: '<=', a: one, b: w.value });
85
+ const at = base ? { op: 'add', a: { op: 'mul', k: base, a: v('a$c') }, b: v('b$c') } : v('a$c');
86
+ const law = (name, cmp) => (base
87
+ ? { name, forall: 'a$c', keys: ['b$c'], holds: { op: 'or', xs: [{ op: 'not', x: { op: 'and', xs: [{ op: '<=', a: one, b: { op: 'get', map: mp, key: at } }, { op: '<', a: v('b$c'), b: k(base) }] } }, cmp] } }
88
+ : when(name, { op: '<=', a: one, b: { op: 'get', map: mp, key: v('a$c') } }, cmp));
74
89
  // A comparison with a state count at the written key.
75
- for (const g of guards)
76
- for (const [, [c, key]] of reads(g, isCount)) {
77
- if (json(key) !== json(v(t)) || mentions(m.counts[c].body, t))
78
- continue;
79
- if (m.inputs.some(i => i !== t && mentions(g, i)))
80
- continue;
81
- laws.push(law(`${mp} keeps ${c}'s bound`, substC(g, { vars: { [t]: v('a$c') }, maps: {}, flags: {} })));
82
- }
83
- // A quantified flag the write reads: every member of a counted set satisfies R(member, t).
90
+ if (!base)
91
+ for (const g of guards)
92
+ for (const [, [c, key]] of reads(g, isCount)) {
93
+ if (json(key) !== json(v(t)) || mentions(m.counts[c].body, t))
94
+ continue;
95
+ if (m.inputs.some(i => i !== t && mentions(g, i)))
96
+ continue;
97
+ laws.push(law(`${mp} keeps ${c}'s bound`, substC(g, { vars: { [t]: v('a$c') }, maps: {}, flags: {} })));
98
+ }
99
+ // What the write reads of every member of a counted set: R(member, t) by a quantified flag (∀ q. h(q) → R(q, t)),
100
+ // or by a request's count of the members without it read 0 (|{k: h(k) ∧ ¬R(k, t)}| = 0).
101
+ const said = [];
84
102
  for (const [f, q] of Object.entries(flags)) {
85
103
  if (q.op !== 'all' || q.keys?.length || !mentions(written, `?${f}`) || q.c.op !== 'or' || q.c.xs.length !== 2)
86
104
  continue;
87
105
  const [h, r] = q.c.xs[0].op === 'not' ? [q.c.xs[0].x, q.c.xs[1]] : [q.c.xs[1].op === 'not' ? q.c.xs[1].x : undefined, q.c.xs[0]];
88
- if (!h || !r)
106
+ if (h && r)
107
+ said.push({ key: q.key, h, r });
108
+ }
109
+ for (const [c0, d0] of Object.entries(m.counts ?? {})) {
110
+ const zero0 = some(written, x => x.op === '=' && x.a?.op === 'get' && x.a.map === c0 && x.b?.op === 'const' && x.b.value === 0);
111
+ const b0 = d0.body.op === 'if' && d0.body.c.op === 'and' && d0.body.c.xs.length === 2 ? d0.body.c.xs : undefined;
112
+ if (!zero0 || !b0 || mentions([d0.body, d0.below], d0.j))
89
113
  continue;
114
+ const [h, r] = b0[1].op === 'not' ? [b0[0], b0[1].x] : b0[0].op === 'not' ? [b0[1], b0[0].x] : [];
115
+ if (h && r)
116
+ said.push({ key: d0.key, h, r });
117
+ }
118
+ for (const { key: qk, h, r } of said) {
90
119
  for (const [c, d] of Object.entries(all)) {
91
- // the set: the count's body is the flag's hypothesis at its key
120
+ // the set: the count's body is the hypothesis at its key
92
121
  const body = d.body.op === 'if' ? d.body.c : undefined;
93
- if (!body || json(substC(body, { vars: { [d.key]: v(q.key) }, maps: {}, flags: {} })) !== json(h))
122
+ if (!body || json(substC(body, { vars: { [d.key]: v(qk) }, maps: {}, flags: {} })) !== json(h))
94
123
  continue;
95
- if (m.inputs.some(i => i !== t && mentions(r, i)))
124
+ if (m.inputs.some(i => !ps.includes(i) && mentions(r, i)))
96
125
  continue;
97
- const name = `${c}$${mp}`, j = `${name}$x`, key = `${name}$k`;
98
- counts[name] = { j, key, below: d.below, body: { op: 'if', c: substC(r, { vars: { [q.key]: v(key), [t]: v(j) }, maps: {}, flags: {} }), then: one, else: zero } };
126
+ const name = `${c}$${mp}`, j = `${name}$x`, key = `${name}$k`, lo = `${name}$y`;
127
+ counts[name] = { j, key, below: d.below, body: { op: 'if', c: substC(r, { vars: { [qk]: v(key), [t]: v(j), ...(base ? { [ps[1]]: v(lo) } : {}) }, maps: {}, flags: {} }), then: one, else: zero }, ...(base ? { lo, base } : {}) };
99
128
  for (const g of [...guards, ...more])
100
129
  if (reads(g, x => x === c).size)
101
- laws.push(law(`${mp} keeps ${c}'s bound through ${name}`, substC(g, { vars: {}, maps: { [c]: () => ({ op: 'get', map: name, key: v('a$c') }) }, flags: {} })));
130
+ laws.push(law(`${mp} keeps ${c}'s bound through ${name}`, substC(g, { vars: {}, maps: { [c]: () => ({ op: 'get', map: name, key: at }) }, flags: {} })));
131
+ }
132
+ }
133
+ }
134
+ // Who did or still can write a relation: a write W[N²·n + N·t + u] := 1 by a request's node n (below the bound of the
135
+ // set the guard counts) under a flag whose literals reading n are G(n, t, u). Its count over two parameters,
136
+ // c(t, u) = |{k: G(k, t, u) ∨ W[N²·k + N·t + u]}|, the keys that wrote it at (t, u) or may yet; a set the guard counts
137
+ // and every such k meet unless c is at most what the guard's comparison forbids. Proposed under each map P the flag
138
+ // reads at N·t + u alone (a cell the writes at (t, u) need), at another (c, d): P[N·c + d] ∧ a < c (∧ b ≠ d, or a ≤ c ∧
139
+ // b ≠ d) ⇒ ¬(the guard's comparison with c at (a, b)): no majority could write W at (a, b) once P holds further on.
140
+ const lin = (e) => {
141
+ if (e.op === 'var')
142
+ return new Map([[e.name, 1]]);
143
+ if (e.op === 'mul' && e.a.op === 'var')
144
+ return new Map([[e.a.name, e.k]]);
145
+ if (e.op !== 'add')
146
+ return undefined;
147
+ const x = lin(e.a), y = lin(e.b);
148
+ if (!x || !y || [...x.keys()].some(q => y.has(q)))
149
+ return undefined;
150
+ return new Map([...x, ...y]);
151
+ };
152
+ const conj = (c) => (c.op === 'and' ? c.xs.flatMap(conj) : [c]);
153
+ for (const [mp, w] of Object.entries(m.step.maps ?? {})) {
154
+ if ('each' in w)
155
+ continue;
156
+ const l = lin(w.key), es = l && [...l.entries()].sort((x, y) => y[1] - x[1]);
157
+ if (!es || es.length !== 3 || !es.every(([q]) => m.inputs.includes(q)))
158
+ continue;
159
+ const [[nd, c0], [t, c1], [u, c2]] = es;
160
+ if (c2 !== 1 || c1 < 2 || c0 !== c1 * c1)
161
+ continue;
162
+ const base = c1;
163
+ for (const [f, fc] of Object.entries(flags)) {
164
+ if (fc.op === 'all' || !json(w.value).includes(`"op":"flag","name":${json(f)}`))
165
+ continue;
166
+ const parts = conj(inline(fc)), dep = parts.filter(x => mentions(x, nd));
167
+ if (!dep.length || dep.some(x => m.inputs.some(i => ![nd, t, u].includes(i) && mentions(x, i))))
168
+ continue;
169
+ const ps = parts.flatMap(x => (x.op === '=' && x.a.op === 'get' && x.b.op === 'const' && x.b.value === 1 ? [x.a] : x.op === '<=' && x.a.op === 'const' && x.a.value === 1 && x.b.op === 'get' ? [x.b] : []))
170
+ .filter(g => { const kl = lin(g.key); return !!kl && kl.size === 2 && kl.get(t) === base && kl.get(u) === 1; });
171
+ if (!ps.length)
172
+ continue;
173
+ for (const [c, d] of Object.entries(all)) {
174
+ if (d.body.op !== 'if' || !json(m.assume).includes(json({ op: '<', a: v(nd), b: d.below })))
175
+ continue;
176
+ const gs = guards.filter(g => reads(g, x => x === c).size);
177
+ if (!gs.length || mentions(d.body, d.j))
178
+ continue;
179
+ const name = `can$${mp}$${c}`, j = `${name}$x`, key = `${name}$k`, lo = `${name}$y`, sub = { vars: { [nd]: v(key), [t]: v(j), [u]: v(lo) }, maps: {}, flags: {} };
180
+ const body = { op: 'or', xs: [{ op: 'and', xs: dep.map(x => substC(x, sub)) }, { op: '<=', a: one, b: { op: 'get', map: mp, key: substE(w.key, sub) } }] };
181
+ counts[name] = { j, key, lo, base, below: d.below, body: { op: 'if', c: body, then: one, else: zero } };
182
+ const pk2 = (a, b) => ({ op: 'add', a: { op: 'mul', k: base, a: v(a) }, b: v(b) });
183
+ for (const g of gs)
184
+ for (const pg of ps)
185
+ for (const [r1, r2] of [['<', true], ['<', false], ['<=', true]]) {
186
+ const hyp = { op: 'and', xs: [{ op: '<=', a: one, b: { op: 'get', map: pg.map, key: pk2('c$c', 'd$c') } }, { op: r1, a: v('a$c'), b: v('c$c') },
187
+ ...(r2 ? [{ op: 'not', x: { op: '=', a: v('b$c'), b: v('d$c') } }] : []), { op: '<', a: v('b$c'), b: k(base) }, { op: '<', a: v('d$c'), b: k(base) }] };
188
+ laws.push({ name: `${pg.map} at (c, d) bounds ${name} at (a, b): a ${r1} c${r2 ? ', b ≠ d' : ''}`, forall: 'a$c', keys: ['b$c', 'c$c', 'd$c'],
189
+ holds: { op: 'or', xs: [{ op: 'not', x: hyp }, { op: 'not', x: substC(g, { vars: {}, maps: { [c]: () => ({ op: 'get', map: name, key: pk2('a$c', 'b$c') }) }, flags: {} }) }] } });
190
+ }
102
191
  }
103
192
  }
104
193
  }
@@ -108,7 +197,7 @@ export function guardLaws(m) {
108
197
  // The count cells the goal reads (each before the step with its post name's twin), instances over pairs of them with one
109
198
  // bound (a count that doesn't drop, +1 and −1; two that meet, +1, +1 and −1), each with a fresh witness; and the laws and
110
199
  // quantified flags at the witnesses (returned, for the goal's law's instances).
111
- export function certFor(goal, decls) {
200
+ export function certFor(goal, decls, wide = false, most = wide ? 24 : 10) {
112
201
  const byMap = new Map(decls.map(d => [d.map, d])), last = goal.hyps.at(-1);
113
202
  // (the conclusion's reads first, then those at a constant or the law's own names, then the rest)
114
203
  const own = (t) => t.op === 'const' || (t.op === 'var' && json(last).includes(`"name":${json(t.name)}`));
@@ -142,7 +231,59 @@ export function certFor(goal, decls) {
142
231
  for (const [j, c] of named.entries())
143
232
  if (i < j && !(ranked(a) || ranked(c)) && bound(a) === bound(c) && !post(a) && !post(c))
144
233
  add([[1, a[0]], [1, c[0]]], -1, byMap.get(a[1]).below);
145
- return { reads: named, insts: insts.slice(0, 10), wits: wits.slice(0, 10) };
234
+ // (a count after the step, the conclusion's, at least a request's count less another: a set's members all in it but
235
+ // those a guard's count of the rest finds, |Q| ≤ missing + votes)
236
+ for (const c of named)
237
+ if (post(c) && rank([c[1], c[2]]) === 0)
238
+ for (const a of named)
239
+ for (const d of named)
240
+ if (a !== d && !post(a) && !post(d) && keyless(a[1]) && keyless(d[1]) && bound(a) === bound(c) && bound(d) === bound(c))
241
+ add([[1, a[0]], [-1, d[0]], [-1, c[0]]], 0, byMap.get(c[1]).below);
242
+ // (wide, asked by a model the instances above leave: every count at most each other one of its bound, either way, and
243
+ // each positive only where a key is in it; a count read with no partner is then tied to its cells at a witness)
244
+ // (`most`: the instances kept, 24 widened; a lazy goal's pool takes many more, as only those a model breaks are asked)
245
+ const cap = most;
246
+ if (wide) {
247
+ insts.splice(10), wits.splice(10);
248
+ for (const a of named)
249
+ for (const c of named)
250
+ if (a !== c && bound(a) === bound(c) && !(post(c) && !post(a)))
251
+ add([[1, a[0]], [-1, c[0]]], 0, byMap.get(a[1]).below);
252
+ for (const a of named)
253
+ add([[1, a[0]]], 0, byMap.get(a[1]).below);
254
+ // (a lazy pool's further instances: each count at most its bound, and every two of a bound that meet)
255
+ if (cap > 24) {
256
+ for (const a of named)
257
+ add([[1, a[0]]], -1, byMap.get(a[1]).below);
258
+ const had = new Set(insts.map(i => json([i.terms, i.unit])));
259
+ for (const [i, a] of named.entries())
260
+ for (const [j, c] of named.entries())
261
+ if (i < j && bound(a) === bound(c) && post(a) === post(c) && !had.has(json([[[1, a[0]], [1, c[0]]], -1])))
262
+ add([[1, a[0]], [1, c[0]]], -1, byMap.get(a[1]).below);
263
+ }
264
+ }
265
+ // (wide: each count picked at the keys the goal reads the maps its body reads at its key and its instances' witnesses:
266
+ // at least its body there, at every two of them and at all of them)
267
+ const picks = [];
268
+ if (wide)
269
+ for (const r of named) {
270
+ const d = byMap.get(r[1]), at = [...reads(d.body, () => true).values()].filter(([, key]) => json(key) === json(v(d.key))).map(([mp]) => mp);
271
+ if (!at.length)
272
+ continue;
273
+ // (the key terms the goal reads those maps at, and the witnesses of the instances that name this count: a member the
274
+ // counting lemma found is a key too; three of each first, six at most)
275
+ const gs = [...new Map([...reads(goal.hyps, mp => at.includes(mp)).values()].map(([, t]) => [json(t), t])).values()].filter(t => !mentions(t, d.key));
276
+ const ws = insts.slice(0, cap).filter(i => i.terms.some(([, x]) => x === r[0])).map(i => v(i.wit)), ts0 = [...gs.slice(0, 3), ...ws.slice(0, 3), ...gs.slice(3), ...ws.slice(3)];
277
+ const ts = [...new Map(ts0.map(t => [json(t), t])).values()].slice(0, 6);
278
+ for (let i = 0; i < ts.length; i++)
279
+ for (let j = i + 1; j < ts.length; j++)
280
+ picks.push([r[0], [ts[i], ts[j]]]);
281
+ if (ts.length > 2)
282
+ picks.push([r[0], ts]);
283
+ }
284
+ // (loose: a read no instance among the first ten names, which only widening ties to its cells)
285
+ const loose = named.some(r => !insts.slice(0, 10).some(i => i.terms.some(([, x]) => x === r[0])));
286
+ return { reads: named, insts: insts.slice(0, cap), wits: wits.slice(0, cap), picks, loose };
146
287
  }
147
288
  // --- the candidates --------------------------------------------------------------------------------------------------
148
289
  // One list from the machine's shapes, each kept only if true on every small state: each map's cells at most what the
@@ -255,7 +396,7 @@ export function cone(m, set, attempt, until, houdini = false) {
255
396
  if ('proof' in r) {
256
397
  kept.set(law.name, r.proof);
257
398
  grew = any = true;
258
- if (env.GDP_COUNTS_TRACE === '3')
399
+ if (env.GDP_COUNTS_TRACE?.includes('3'))
259
400
  console.error(`counts: keep ${law.name.slice(0, 50)} tier ${t}`);
260
401
  }
261
402
  }
@@ -284,7 +425,7 @@ export function searchCounted(m0, caller, ms = 60_000) {
284
425
  return run();
285
426
  cdclStats.on = true;
286
427
  const t = performance.now(), out = run();
287
- console.error(`counts: prof total ${Math.round(performance.now() - t)} ms; ${Object.entries(cprof).map(([k2, x]) => `${k2} ${Math.round(x)}`).join(', ')}; cdcl runs ${cdclStats.runs} conflicts ${cdclStats.conflicts} decisions ${cdclStats.decisions} solveMs ${Math.round(cdclStats.solveMs)} rowsMs ${Math.round(cdclStats.rowsMs)} coreMs ${Math.round(cdclStats.coreMs)} unfoldMs ${Math.round(cdclStats.unfoldMs)} lpMs ${Math.round(cdclStats.lpMs)} boundMs ${Math.round(cdclStats.boundMs)} orderMs ${Math.round(cdclStats.orderMs)}`);
428
+ console.error(`counts: prof total ${Math.round(performance.now() - t)} ms; ${Object.entries(cprof).map(([k2, x]) => `${k2} ${Math.round(x)}`).join(', ')}; cdcl runs ${cdclStats.runs} conflicts ${cdclStats.conflicts} decisions ${cdclStats.decisions} solveMs ${Math.round(cdclStats.solveMs)} rowsMs ${Math.round(cdclStats.rowsMs)} coreMs ${Math.round(cdclStats.coreMs)} unfoldMs ${Math.round(cdclStats.unfoldMs)} lpMs ${Math.round(cdclStats.lpMs)} boundMs ${Math.round(cdclStats.boundMs)} orderMs ${Math.round(cdclStats.orderMs)} fmCalls ${cdclStats.fmCalls} lpMisses ${cdclStats.lpMisses} lpUnknown ${cdclStats.lpUnknown} missMs ${Math.round(cdclStats.missMs)} fastMs ${Math.round(cdclStats.fastMs)} fmOpenMs ${Math.round(cdclStats.fmOpenMs)} fmLeafMs ${Math.round(cdclStats.fmLeafMs)} nodes ${cdclStats.nodes}`);
288
429
  return out;
289
430
  }
290
431
  function searchCounted0(m0, g, m, caller, ms) {
@@ -292,15 +433,17 @@ function searchCounted0(m0, g, m, caller, ms) {
292
433
  if (!caller.every(l => clock() < until && trueSmall(m, [l])))
293
434
  return { proven: false, laws: caller, m, why: clock() < until ? 'a law fails on the small instances' : 'time' };
294
435
  const t1 = clock();
295
- let set = [...caller, ...candidates(m0, m, caller, caller, g.laws, ms, until)];
436
+ // (GDP_COUNTS_GIVEN=1, research's: the callers alone, no candidate: whether a set written whole goes through)
437
+ let set = env.GDP_COUNTS_GIVEN === '1' ? [...caller] : [...caller, ...candidates(m0, m, caller, caller, g.laws, ms, until)];
296
438
  if (env.GDP_COUNTS_TRACE)
297
439
  console.error(`counts: candidates at ${Math.round(clock() - t1)} ms: ${set.map(l => l.name).join('; ')}`);
298
440
  // (the law's own names and the request's values that key a map: an action's value keys none)
299
441
  const keys0 = [...[...reads([m.step, m.assume ?? null], () => true).values()].map(([, key]) => key), ...Object.values(m.step.maps ?? {}).flatMap(w => ('each' in w ? [] : [w.key]))];
300
442
  const keyed = m.inputs.filter(i => keys0.some(key => mentions(key, i))).map(v);
301
443
  const terms = (l) => [...(l.forall ? [v(`${l.forall}#`)] : []), ...(l.keys ?? []).map(v), ...keyed];
302
- let counted = true;
303
- const none = { reads: [], insts: [] };
444
+ const widened = new Set();
445
+ let counted = true, houdini = false; // (houdini: the rounds over the whole set; only they widen)
446
+ const none = { reads: [], insts: [], wits: [], picks: [], loose: false };
304
447
  // One law's goals and their certificates, the laws `ls` assumed (a subsequence of the set): pass 0 with no instances of
305
448
  // the lemma, pass 1 with the cells' (one set of witnesses a law); a tree each within `ms`. The instances: the
306
449
  // quantified flags at the law's own terms, then what the goals' models ask.
@@ -309,12 +452,21 @@ function searchCounted0(m0, g, m, caller, ms) {
309
452
  // broken at a tuple of the goal's keys); none broken, it is still a model, and the law is refuted again without a
310
453
  // solve. (Not where a law added reads a count: its goal's count instances aren't read here.)
311
454
  const models = new Map(), isCount = (mp) => !!m.counts?.[mp];
455
+ // Cone tiers and Houdini may ask the same law again with more instances. Keep a
456
+ // bounded set of sessions; each independently resets unless its hypotheses are
457
+ // still present verbatim. This reuses work, never a verdict from a weaker goal.
458
+ const decisions = new Map();
312
459
  const attempt = (law, ls, pass, ms) => {
460
+ // A cached model can return before the no-count optimization below. A helper
461
+ // without count reads must not suppress the next law's counting-lemma pass.
462
+ if (!pass)
463
+ counted = true;
313
464
  const ts = terms(law), id = `${pass} ${law.name}`, cti = models.get(id);
314
465
  // (the model broken: the attempt starts at the instances it asks for, its last attempt's and the tuples the new laws
315
466
  // break it at, where the rounds of models would lead it again)
316
467
  let seed;
317
- if (cti && !ls.some(l => !cti.ls.has(l.name) && reads(l.holds, isCount).size)) {
468
+ // (not where the attempt may widen: its narrow model is no model of the widened goal)
469
+ if (cti && !(houdini && caller.some(l => l.name === law.name) && !widened.has(law.name)) && !ls.some(l => !cti.ls.has(l.name) && reads(l.holds, isCount).size)) {
318
470
  const at0 = Object.fromEntries(Object.entries(cti.at).filter(([n]) => n.startsWith('?') || ls.some(l => l.name === n)));
319
471
  if (!(seed = ptime('mbqi', () => modelInstances(m, ls, at0, cti.gg, cti.w))))
320
472
  return { stuck: 'sat' };
@@ -322,8 +474,61 @@ function searchCounted0(m0, g, m, caller, ms) {
322
474
  const refuted = (gg, w) => { models.set(id, { gg, w, at, ls: new Set(ls.map(l => l.name)) }); return { stuck: 'sat' }; };
323
475
  let at = seed ?? Object.fromEntries(Object.entries(m.step.flags ?? {}).flatMap(([f, q]) => (q.op === 'all' && !q.keys?.length ? [[`?${f}`, ts]] : [])));
324
476
  const obligations = (i) => ptime('encode', () => countedObligations(m, ls, { [law.name]: i }, ls.indexOf(law)));
325
- const build = () => obligations(at).map(({ goal, decls }) => { const c = pass ? certFor(goal, decls) : none; return { goal, decls, c, gg: ptime('encode', () => mcGoal(goal.hyps, decls, c)) }; });
477
+ // (`wide`: the count instances widened once, where a model is left that the narrow ones and the laws don't refute, for
478
+ // a caller's law or where a read is in none of them: the cone's many refuted candidates keep the narrow ones)
479
+ let wide = false;
480
+ const isCaller = houdini && caller.some(l => l.name === law.name);
481
+ // (lazy (CEGAR): the goal carries the counting lemma's instances and picks some model of it broke, of all certFor
482
+ // proposes: each model read against the rest, as a state, and those it breaks added, until a model breaks none; an
483
+ // instance by the reads it names, a pick by its read and terms, so the choice outlives the goal's rebuilds)
484
+ const chosen = new Set();
485
+ const build = () => obligations(at).map(({ goal, decls }) => {
486
+ const full = pass ? certFor(goal, decls, wide, wide && lazyMode !== '0' ? pool : undefined) : none;
487
+ if (!pass || lazyMode === '0' || (lazyMode === 'wide' && !wide))
488
+ return { goal, decls, c: full, gg: ptime('encode', () => mcGoal(goal.hyps, decls, full)), pool: undefined };
489
+ const rd = new Map(full.reads.map(r => [r[0], json([r[1], r[2]])]));
490
+ const keys = [...full.picks.map(q => `p${json([rd.get(q[0]), q[1]])}`), ...full.insts.map(i => `i${json([i.terms.map(([z, x]) => [z, rd.get(x)]), i.unit, i.below])}`)];
491
+ if (lazyMode === 'wide')
492
+ for (const q of keys.slice(full.picks.length, full.picks.length + 10))
493
+ chosen.add(q);
494
+ const c = { ...full, picks: full.picks.filter((_, j) => chosen.has(keys[j])), insts: full.insts.filter((_, j) => chosen.has(keys[j + full.picks.length])) };
495
+ const fg = ptime('encode', () => mcGoal(goal.hyps, decls, full));
496
+ return { goal, decls, c, gg: ptime('encode', () => mcGoal(goal.hyps, decls, c)), pool: fg && keys.length ? { fg, keys, wits: [...full.picks.map(() => undefined), ...full.insts.map(i => i.wit)] } : undefined };
497
+ });
498
+ // (the pool's hypotheses a model breaks, chosen; whether there were any)
499
+ // (`strict`, where nothing else is broken or asked: the cells it leaves out at their start alone, as a model whose
500
+ // completion meets an instance may still meet it only at a witness some law then breaks)
501
+ const breaks = (b, w, strict = false) => ptime('lazy', () => {
502
+ if (!b?.pool)
503
+ return false;
504
+ // (broken where no completion of the model meets it: the cells it leaves out at their start, at 1, or one map's
505
+ // flipped from either, and an instance's witness at each value below 16)
506
+ const { fg, keys, wits } = b.pool, base = fg.hyps.length - keys.length, ids = keys.flatMap((q, j) => (chosen.has(q) ? [] : [base + j])), judge = brokenOf(fg);
507
+ let bad = ids.length ? judge(w, ids) : [];
508
+ const mps = [...new Set(bad.flatMap(i => [...reads(fg.hyps[i], () => true).values()].map(([mp]) => mp)))], one1 = Object.fromEntries(mps.map(mp => [mp, 1]));
509
+ for (const d of strict ? [undefined] : [undefined, one1, ...mps.map(mp => ({ [mp]: 1 })), ...mps.map(mp => ({ ...one1, [mp]: 0 }))])
510
+ for (let x = 0; x < 16 && bad.length; x++) {
511
+ if (x && !bad.some(i => wits[i - base]))
512
+ break;
513
+ const w2 = { ...w };
514
+ for (const i of bad)
515
+ if (wits[i - base])
516
+ w2[wits[i - base]] = x;
517
+ const still = new Set(judge(w2, bad, false, d));
518
+ bad = bad.filter(i => still.has(i));
519
+ }
520
+ for (const i of bad)
521
+ chosen.add(keys[i - base]);
522
+ tally('#lazy', bad.length);
523
+ return bad.length > 0;
524
+ });
326
525
  let built = build();
526
+ let decideStep = decisions.get(id);
527
+ if (!decideStep) {
528
+ if (decisions.size >= 64)
529
+ decisions.delete(decisions.keys().next().value);
530
+ decisions.set(id, (decideStep = modelSequence()));
531
+ }
327
532
  const t0 = clock(), more = () => clock() - t0 <= Math.max(ms, 1_000);
328
533
  // The step goal decided first by the CDCL search for a model (its phases the simplex's point: a model is met where the
329
534
  // tree search would split on), within 2·ms (5 s at most):
@@ -340,7 +545,7 @@ function searchCounted0(m0, g, m, caller, ms) {
340
545
  const step = built.at(-1);
341
546
  if (!step?.gg)
342
547
  break;
343
- const q = ptime('decide', () => growCdcl(step.gg, new Map(), { nodes: 0, until: Math.min(until, clock() + Math.min(2 * ms, 5_000)) }, { model: true, core: true }));
548
+ const q = ptime('decide', () => decideStep(step.gg, Math.min(until, clock() + Math.min(2 * ms, 5_000))));
344
549
  if (!('tree' in q) && q.why === REFUTED && q.core) {
345
550
  const gg = step.gg, last = gg.hyps.length - 1, used = new Set(q.core.map(h => json(gg.hyps[h]))), cut = {};
346
551
  for (const [name, ts2] of Object.entries(at)) {
@@ -348,7 +553,7 @@ function searchCounted0(m0, g, m, caller, ms) {
348
553
  cut[name] = !l ? ts2 : Array.from({ length: ts2.length / n }, (_, t) => ts2.slice(t * n, t * n + n)).filter(t => used.has(json(assumed([l], { [name]: t })[0]))).flat();
349
554
  }
350
555
  at = cut;
351
- built = obligations(at).map(({ goal, decls }, j) => ({ goal, decls, c: built[j].c, gg: mcGoal(goal.hyps, decls, built[j].c) }));
556
+ built = obligations(at).map(({ goal, decls }, j) => ({ goal, decls, c: built[j].c, gg: mcGoal(goal.hyps, decls, built[j].c), pool: built[j].pool }));
352
557
  part = { gg: built.at(-1).gg, core: { name: gg.name, hyps: (q.core.includes(last) ? q.core : [...q.core, last]).map(h => gg.hyps[h]) } };
353
558
  break;
354
559
  }
@@ -356,12 +561,24 @@ function searchCounted0(m0, g, m, caller, ms) {
356
561
  break;
357
562
  if (!more())
358
563
  return { stuck: 'unknown' };
359
- const asked = ptime('mbqi', () => modelInstances(m, ls, at, step.gg, q.witness));
564
+ const asked = ptime('mbqi', () => modelInstances(m, ls, at, step.gg, q.witness)), lz = breaks(step, q.witness) || (!asked && breaks(step, q.witness, true));
565
+ tally('#rounds');
566
+ tally('#hyps', step.gg.hyps.length);
567
+ if (lz && !asked) {
568
+ built = build();
569
+ continue;
570
+ }
360
571
  if (!asked) {
361
572
  if (countsDump.on && pass && dumped < Number(env.GDP_COUNTS_DUMP_N ?? 3)) {
362
573
  dumped++;
363
574
  countsDump.on({ law: law.name, goal: step.goal.name, ls: ls.map(l => l.name), stuck: 'sat', witness: q.witness, reads: step.c.reads, insts: step.c.insts, hyps: step.gg.hyps });
364
575
  }
576
+ if (pass && !wide && houdini && 'loose' in step.c && (step.c.loose || isCaller) && more()) {
577
+ wide = true;
578
+ widened.add(law.name);
579
+ built = build();
580
+ continue;
581
+ }
365
582
  return refuted(step.gg, q.witness);
366
583
  }
367
584
  at = asked;
@@ -382,77 +599,89 @@ function searchCounted0(m0, g, m, caller, ms) {
382
599
  dumped++;
383
600
  countsDump.on({ law: law.name, goal: goal.name, ls: ls.map(l => l.name), stuck: o.stuck, hyps: gg.hyps });
384
601
  }
385
- if (env.GDP_COUNTS_TRACE === '2')
386
- console.error(`counts: ${ls.length === 1 ? 'alone' : `with ${ls.length}`} ${pass} ${goal.name}: ${o && 'tree' in o ? 'proven' : gg ? o.stuck : 'no read'} ${gg?.hyps.length ?? 0} hyps ${Math.round(clock() - t0)} ms`);
602
+ if (env.GDP_COUNTS_TRACE?.includes('2'))
603
+ console.error(`counts: ${ls.length === 1 ? 'alone' : `with ${ls.length}`} ${pass} ${goal.name}: ${o && 'tree' in o ? 'proven' : gg ? o.stuck : 'no read'} ${gg?.hyps.length ?? 0} hyps (insts ${'insts' in c ? c.insts.length : 0} picks ${'picks' in c ? c.picks.length : 0}${wide ? ' wide' : ''}${built.find(b => b.gg === gg)?.pool ? ` of ${built.find(b => b.gg === gg).pool.keys.length}` : ''}) ${Math.round(clock() - t0)} ms`);
387
604
  // (the tree search's model of the step goal: the instances it asks for, as the CDCL search's, and the goals again)
388
605
  if (gg && o && !('tree' in o) && o.stuck === 'sat' && o.witness && gg === built.at(-1).gg && more()) {
389
- const asked = ptime('mbqi', () => modelInstances(m, ls, at, gg, o.witness));
390
- if (asked) {
391
- at = asked;
606
+ const asked = ptime('mbqi', () => modelInstances(m, ls, at, gg, o.witness)), lz = breaks(built.at(-1), o.witness) || (!asked && breaks(built.at(-1), o.witness, true));
607
+ if (asked || lz) {
608
+ if (asked)
609
+ at = asked;
392
610
  built = build();
393
611
  again = true;
394
612
  break;
395
613
  }
396
614
  }
615
+ if (gg && o && !('tree' in o) && o.stuck === 'sat' && o.witness && gg === built.at(-1).gg && pass && !wide && houdini && 'loose' in c && (c.loose || isCaller) && more()) {
616
+ wide = true;
617
+ widened.add(law.name);
618
+ built = build();
619
+ again = true;
620
+ break;
621
+ }
397
622
  if (gg && o && !('tree' in o) && o.stuck === 'sat' && o.witness && gg === built.at(-1).gg)
398
623
  return refuted(gg, o.witness);
399
624
  if (!o || !('tree' in o))
400
625
  return { stuck: gg ? o.stuck ?? '' : '' };
401
- certs[goal.name] = { reads: c.reads, insts: c.insts, tree: o.tree };
626
+ certs[goal.name] = { reads: c.reads, insts: c.insts, tree: o.tree, ...('picks' in c && c.picks.length ? { picks: c.picks } : {}) };
402
627
  }
403
628
  if (!again)
404
629
  return { proof: { instances: at, certs } };
405
630
  }
406
631
  };
407
632
  const traced = (law, ls, pass, ms) => {
408
- const t0 = clock(), r = attempt(law, ls, pass, ms);
409
- if (env.GDP_COUNTS_TRACE === '3')
410
- console.error(`counts: try ${law.name.slice(0, 50)} | ls ${ls.length} pass ${pass} ms ${ms} took ${Math.round(clock() - t0)} -> ${'proof' in r ? 'kept' : r.stuck}`);
633
+ const t0 = clock(), w0 = performance.now(), p0 = { ...cprof }, r = attempt(law, ls, pass, ms);
634
+ if (env.GDP_COUNTS_TRACE?.includes('3'))
635
+ console.error(`counts: try ${law.name.slice(0, 50)} | ls ${ls.length} pass ${pass} ms ${Math.round(ms)} took ${Math.round(clock() - t0)} wall ${Math.round(performance.now() - w0)} [${Object.entries(cprof).filter(([k2, x]) => x - (p0[k2] ?? 0) >= 1).map(([k2, x]) => `${k2} ${Math.round(x - (p0[k2] ?? 0))}`).join(' ')}] -> ${'proof' in r ? 'kept' : r.stuck}`);
411
636
  return r;
412
637
  };
413
- // The cone (a law about no key is assumed in every goal, so with one in the set nothing is kept); with every caller
414
- // kept, the kept laws are the proof; else Houdini over the whole set, the kept laws' proofs reused.
415
- const kept = set.every(l => l.forall) ? cone(m, set, (law, ls, ms2) => traced(law, ls, 1, ms2), until, true) : new Map();
416
- if (env.GDP_COUNTS_TRACE)
417
- console.error(`counts: kept ${kept.size} of ${set.length} in ${Math.round(clock() - t1)} ms`);
638
+ // Prove the proposed set together. A preliminary cone reproved many helpers in
639
+ // several smaller contexts before asking these same induction goals. Houdini
640
+ // drops failed helpers and carries proofs whose instantiated support survives.
641
+ // Every returned set still passes checkCountedLaws, including all caller laws.
418
642
  const finish = (ls, proof) => (ptime('kernel', () => checkCountedLaws(m, ls, proof)) ? { proven: true, laws: ls, m, proof } : { proven: false, laws: ls, m, why: 'the kernel refused the set' });
419
- const keptProof = () => ({ instances: Object.fromEntries([...kept].map(([n, pr]) => [n, pr.instances])), certs: Object.assign({}, ...[...kept.values()].map(pr => pr.certs)) });
420
- if (caller.every(l => kept.has(l.name)))
421
- return finish(set.filter(l => kept.has(l.name)), keptProof());
422
- // (each law not kept an even share of what is left a round)
423
- let carried = new Map();
424
- for (let round = 0; clock() < until; round++) {
425
- const { instances, certs } = keptProof(), failed = new Set(), proofs = new Map(), share = (until - clock()) / Math.max(1, set.filter(l => !kept.has(l.name) && !carried.has(l.name)).length);
426
- // (the callers last, so they meet every support kept so far)
427
- for (const law of [...set.filter(l => !caller.includes(l)), ...set.filter(l => caller.includes(l))]) {
428
- if (clock() > until)
429
- return { proven: false, laws: set, m, why: 'time' };
430
- if (kept.has(law.name))
431
- continue;
432
- const was = carried.get(law.name);
433
- let r = was ? { proof: was } : traced(law, set, 0, share);
434
- if (!('proof' in r) && counted)
435
- r = traced(law, set, 1, share);
436
- if (!('proof' in r)) {
437
- failed.add(law.name);
438
- continue;
643
+ const join = (kept = new Map()) => {
644
+ const keptProof = () => ({ instances: Object.fromEntries([...kept].map(([n, pr]) => [n, pr.instances])), certs: Object.assign({}, ...[...kept.values()].map(pr => pr.certs)) });
645
+ if (caller.every(l => kept.has(l.name)))
646
+ return finish(set.filter(l => kept.has(l.name)), keptProof());
647
+ // (each law without a carried proof an even share of what is left a round)
648
+ let carried = new Map();
649
+ houdini = true;
650
+ for (let round = 0; clock() < until; round++) {
651
+ const { instances, certs } = keptProof();
652
+ const failed = new Set(), proofs = new Map(), share = (until - clock()) / Math.max(1, set.filter(l => !kept.has(l.name) && !carried.has(l.name)).length);
653
+ // (the callers last, so they meet every support kept so far)
654
+ for (const law of [...set.filter(l => !caller.includes(l)), ...set.filter(l => caller.includes(l))]) {
655
+ if (clock() > until)
656
+ return { proven: false, laws: set, m, why: 'time' };
657
+ if (kept.has(law.name))
658
+ continue;
659
+ const was = carried.get(law.name);
660
+ let r = was ? { proof: was } : traced(law, set, 0, share);
661
+ if (!('proof' in r) && counted)
662
+ r = traced(law, set, 1, share);
663
+ if (!('proof' in r)) {
664
+ failed.add(law.name);
665
+ continue;
666
+ }
667
+ proofs.set(law.name, r.proof);
668
+ instances[law.name] = r.proof.instances;
669
+ Object.assign(certs, r.proof.certs);
439
670
  }
440
- proofs.set(law.name, r.proof);
441
- instances[law.name] = r.proof.instances;
442
- Object.assign(certs, r.proof.certs);
671
+ if (env.GDP_COUNTS_TRACE)
672
+ console.error(`counts: round ${round}: ${set.length} laws, ${kept.size} kept, ${carried.size} carried, failed ${[...failed].join('; ').slice(0, 600)}`);
673
+ if (!failed.size)
674
+ return finish(set, { instances, certs });
675
+ const next = set.filter(l => !failed.has(l.name) || caller.includes(l));
676
+ // (only callers fail: drop nothing more, give up)
677
+ if (next.length === set.length)
678
+ return { proven: false, laws: set, m, why: `not proven: ${[...failed].join(', ')}` };
679
+ // (a proof that assumes no law dropped at any key is one over the rest: its goals are the same; the kernel checks all)
680
+ const dropped = set.filter(l => !next.includes(l));
681
+ carried = new Map([...proofs].filter(([, pr]) => dropped.every(d => d.forall && !pr.instances[d.name]?.length)));
682
+ set = next;
443
683
  }
444
- if (env.GDP_COUNTS_TRACE)
445
- console.error(`counts: round ${round}: ${set.length} laws, ${kept.size} kept, failed ${[...failed].join('; ').slice(0, 600)}`);
446
- if (!failed.size)
447
- return finish(set, { instances, certs });
448
- const next = set.filter(l => !failed.has(l.name) || caller.includes(l));
449
- // (only callers fail: drop nothing more, give up)
450
- if (next.length === set.length)
451
- return { proven: false, laws: set, m, why: `not proven: ${[...failed].join(', ')}` };
452
- // (a proof that assumes no law dropped at any key is one over the rest: its goals are the same; the kernel checks all)
453
- const dropped = set.filter(l => !next.includes(l));
454
- carried = new Map([...proofs].filter(([, pr]) => dropped.every(d => d.forall && !pr.instances[d.name]?.length)));
455
- set = next;
456
- }
457
- return { proven: false, laws: set, m, why: 'time' };
684
+ return { proven: false, laws: set, m, why: 'time' };
685
+ };
686
+ return join();
458
687
  }
@@ -1,4 +1,4 @@
1
- import { argsKey, atMost as le, compileFunction, evalC as evalMC, evalE as evalME, fnValue, get, interpret, minus as sub, n as k, pairedArgs, plus as add, same as eqC, seeded, walk, x as v } from "./machine.js";
1
+ import { argsKey, atMost as le, compileFunction, cpair, cunpair, evalC as evalMC, evalE as evalME, fnValue, get, interpret, minus as sub, n as k, pairedArgs, plus as add, same as eqC, seeded, tupleParts, walk, x as v } from "./machine.js";
2
2
  import { check, livenessHelpful, livenessParts, loopLiveness, loopMachine, loopObligations, strongly, substC, substE } from "../kernel/kernel.js";
3
3
  import { checkCyclic, factsAvoid, lexLt } from "../kernel/cyclic.js";
4
4
  import { and, proveCyclic, vars as vs } from "./cyclic.js";
@@ -42,6 +42,8 @@ export const showE = (e) => {
42
42
  const as = pairedArgs(e.key, 2), as3 = as && pairedArgs(e.key, 3);
43
43
  return `${e.map}(${(as3 ?? as ?? [e.key]).map(showE).join(', ')})`;
44
44
  }
45
+ case 'tup': return `⟨${tupleParts(e).map(showE).join(', ')}⟩`;
46
+ case 'proj': return `${e.k === 0 ? 'fst' : 'snd'}(${showE(e.a)})`;
45
47
  }
46
48
  };
47
49
  export const showC = (c) => {
@@ -154,6 +156,11 @@ function ev(cx, e, env, rec) {
154
156
  case 'min': return two(e.a, e.b, Math.min);
155
157
  case 'max': return two(e.a, e.b, Math.max);
156
158
  case 'prod': return two(e.a, e.b, (x, y) => x * y);
159
+ case 'tup': return two(e.a, e.b, cpair);
160
+ case 'proj': {
161
+ const x = ev(cx, e.a, env, rec);
162
+ return x === undefined ? undefined : cunpair(x)[e.k];
163
+ }
157
164
  case 'mul': {
158
165
  const x = ev(cx, e.a, env, rec);
159
166
  return x === undefined ? undefined : e.k * x;
@@ -10,6 +10,7 @@ export type NodeSpec = Omit<CNode, 'uses' | 'main'> & {
10
10
  slice?: boolean;
11
11
  main?: Tree;
12
12
  carry?: boolean | 'sliced';
13
+ within?: number;
13
14
  };
14
15
  export type CyclicVerdict = {
15
16
  proven: true;