gdp-wired 0.22.0 → 0.24.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 (60) hide show
  1. package/README.md +27 -2
  2. package/dist/core/bmc.d.ts +49 -0
  3. package/dist/core/bmc.js +509 -0
  4. package/dist/core/check.d.ts +1 -1
  5. package/dist/core/check.js +19 -6
  6. package/dist/core/cycles.d.ts +38 -0
  7. package/dist/core/cycles.js +531 -43
  8. package/dist/core/data.d.ts +25 -1
  9. package/dist/core/data.js +984 -215
  10. package/dist/core/discover.js +8 -3
  11. package/dist/core/dsl.d.ts +17 -3
  12. package/dist/core/dsl.js +81 -15
  13. package/dist/core/extract-check.d.ts +77 -0
  14. package/dist/core/extract-check.js +785 -0
  15. package/dist/core/extract-ir.d.ts +139 -0
  16. package/dist/core/extract-ir.js +198 -0
  17. package/dist/core/extract-print.d.ts +30 -0
  18. package/dist/core/extract-print.js +426 -0
  19. package/dist/core/extract-read.d.ts +298 -0
  20. package/dist/core/extract-read.js +1298 -0
  21. package/dist/core/extract-shapes.d.ts +13 -0
  22. package/dist/core/extract-shapes.js +993 -0
  23. package/dist/core/extract-xstate.d.ts +34 -0
  24. package/dist/core/extract-xstate.js +320 -0
  25. package/dist/core/extract.d.ts +31 -0
  26. package/dist/core/extract.js +249 -0
  27. package/dist/core/infer.js +518 -5
  28. package/dist/core/kind.d.ts +30 -0
  29. package/dist/core/kind.js +81 -0
  30. package/dist/core/nonterm.d.ts +34 -0
  31. package/dist/core/nonterm.js +610 -0
  32. package/dist/core/q.d.ts +17 -0
  33. package/dist/core/q.js +52 -0
  34. package/dist/core/rationals.d.ts +112 -0
  35. package/dist/core/rationals.js +281 -0
  36. package/dist/core/relation.d.ts +28 -0
  37. package/dist/core/relation.js +483 -0
  38. package/dist/core/solver.d.ts +5 -2
  39. package/dist/core/solver.js +65 -20
  40. package/dist/core/transfer.d.ts +3 -3
  41. package/dist/core/transfer.js +69 -42
  42. package/dist/core/xstate.d.ts +6 -0
  43. package/dist/core/xstate.js +70 -12
  44. package/dist/kernel/cdcl.d.ts +13 -0
  45. package/dist/kernel/cdcl.js +307 -236
  46. package/dist/kernel/kernel.d.ts +106 -0
  47. package/dist/kernel/kernel.js +342 -0
  48. package/dist/kernel/prove.js +14 -3
  49. package/dist/kernel/qprove.d.ts +18 -0
  50. package/dist/kernel/qprove.js +157 -0
  51. package/dist/kernel/reader.d.ts +14 -0
  52. package/dist/kernel/reader.js +102 -14
  53. package/dist/kernel/simplex.d.ts +4 -0
  54. package/dist/kernel/simplex.js +38 -11
  55. package/dist/lean-kernel/emit.d.ts +46 -1
  56. package/dist/lean-kernel/emit.js +125 -16
  57. package/dist/lean-kernel/library.js +4 -1
  58. package/dist/lean-kernel/qlean.d.ts +26 -0
  59. package/dist/lean-kernel/qlean.js +105 -0
  60. package/package.json +16 -3
@@ -39,14 +39,36 @@ function localize(rows, steps) {
39
39
  });
40
40
  return { inputs, steps: out };
41
41
  }
42
+ // An atom's variable: the smaller of its canonical text and its flip's (remembered per form).
43
+ const keyMemo = new WeakMap();
44
+ const keyOf = (lin) => {
45
+ let key = keyMemo.get(lin);
46
+ if (key === undefined) {
47
+ const a = canon(lin), b = canonFlip(lin);
48
+ keyMemo.set(lin, (key = b < a ? b : a));
49
+ }
50
+ return key;
51
+ };
42
52
  // GDP_CDCL_LP=0: each node solved by elimination alone (no simplex); GDP_CDCL_FILL (8): the simplex's tableau begun
43
53
  // again past this many entries a row the node reads (research/step's comparisons).
44
54
  const env = globalThis.process?.env ?? {};
45
55
  const lpFill = Number(env.GDP_CDCL_FILL ?? 8), useLp = env.GDP_CDCL_LP !== '0', verify = env.GDP_CDCL_VERIFY === '1';
46
56
  // Counters research/step reads (times only while `on`).
47
57
  export const cdclStats = { on: false, runs: 0, proven: 0, conflicts: 0, decisions: 0, solveMs: 0, rowsMs: 0, coreMs: 0, unfoldMs: 0, leaves: 0, resolved: 0, coreSize: 0, pathSize: 0, lpMisses: 0, resolved2: 0, unfoldWhy: '', orderMs: 0, boundMs: 0, openMisses: 0, lpMs: 0, lpUnknown: 0, fmCalls: 0, pivots: 0, lpRebuilds: 0, lpRows: 0, lpNnz: 0, lpVars: 0, lpLoad: 0, nodes: 0, boundRounds: 0 };
58
+ // `opts.model`: only whether the goal has a model is wanted (core/bmc.ts): a refutation isn't unfolded into a tree, its
59
+ // outcome `unknown` with `why` REFUTED; each decision's phase is the simplex's point's (phaseAtPoint); past `opts.wall` (wall time) the search stops as past its budget.
60
+ export const REFUTED = 'refuted, not unfolded';
48
61
  export function growCdcl(goal, root, budget, opts = {}) {
49
- const until = budget.until ?? Infinity, st = cdclStats, timing = st.on;
62
+ return cdclSession(goal, root, opts).run(budget, opts.wall);
63
+ }
64
+ // A search kept from one run to the next (core/bmc.ts's deeper goals): its atoms, their activities and phases, its learned
65
+ // clauses and the simplex's tableau. Between runs the caller may add hypotheses to the goal (goal.hyps grows; none is
66
+ // taken away), so every clause learned stays a consequence of the goal. A run may assume an atom (`assume`, decided
67
+ // first, at level 1): where the search learns it false the run ends refuted (REFUTED), as where nothing is assumed and
68
+ // the goal is refuted. Each run's own budget: nodes, `until` on the counted clock, `wall`.
69
+ export function cdclSession(goal, root, opts = {}) {
70
+ let until = Infinity, budget = { nodes: 0 }, wall, assumed;
71
+ const st = cdclStats, timing = st.on;
50
72
  const t = (f, add) => { if (!timing)
51
73
  return f(); const t0 = performance.now(); try {
52
74
  return f();
@@ -61,9 +83,9 @@ export function growCdcl(goal, root, budget, opts = {}) {
61
83
  let inc = 1;
62
84
  // An atom as a variable: the stored form is the one whose canonical text is the variable's name (the smaller).
63
85
  const ensure = (lin) => {
64
- const a = canon(lin), b = canonFlip(lin), flip = b < a, key = flip ? b : a;
86
+ const key = keyOf(lin);
65
87
  if (!vars.has(key)) {
66
- const stored = flip ? atomOf(lin, false).lin : lin;
88
+ const flip = key !== canon(lin), stored = flip ? atomOf(lin, false).lin : lin;
67
89
  vars.set(key, { lin: stored, terms: Object.entries(stored).filter(([x0]) => x0 !== '1'), level: -1, at: -1, act: 0, phase: !flip });
68
90
  }
69
91
  return key;
@@ -84,8 +106,9 @@ export function growCdcl(goal, root, budget, opts = {}) {
84
106
  get: name => vars.get(name), stamp: at => stamps[at], size: () => trail.length,
85
107
  lin: at => { const name = trail[at], x = vars.get(name); return { name, lin: x.lin, holds: x.val }; },
86
108
  });
87
- const last = goal.hyps.length - 1, near = nearOf(goal);
88
- const decidedLin = (lin) => vars.get(canon(lin))?.val !== undefined || vars.get(canonFlip(lin))?.val !== undefined;
109
+ let last = goal.hyps.length - 1;
110
+ const near = nearOf(goal);
111
+ const decidedLin = (lin) => vars.get(keyOf(lin))?.val !== undefined;
89
112
  const fixed = trail.length;
90
113
  const level = () => levels.length;
91
114
  const pathOf = () => new Map(trail.map(name => { const x = vars.get(name); return [name, { lin: x.lin, holds: x.val }]; }));
@@ -101,14 +124,16 @@ export function growCdcl(goal, root, budget, opts = {}) {
101
124
  else
102
125
  x.reason = reason;
103
126
  trail.push(name);
127
+ reader.touch(name);
104
128
  };
105
129
  const undoTo = (l) => {
106
130
  const keep = levels[l] ?? trail.length;
107
131
  while (trail.length > Math.max(keep, fixed)) {
108
- const x = vars.get(trail.pop());
132
+ const name = trail.pop(), x = vars.get(name);
109
133
  delete x.val;
110
134
  x.level = -1;
111
135
  delete x.reason;
136
+ reader.touch(name);
112
137
  }
113
138
  levels.length = l;
114
139
  };
@@ -220,255 +245,301 @@ export function growCdcl(goal, root, budget, opts = {}) {
220
245
  }, ms => { st.coreMs += ms; });
221
246
  const luby = (i) => { let n = 1; while ((1 << n) - 1 < i + 1)
222
247
  n++; return (1 << n) - 1 === i + 1 ? 1 << (n - 1) : luby(i + 1 - (1 << (n - 1))); };
248
+ // With a model wanted: each open atom's phase (no term column's) set to its value at the simplex's point, which meets
249
+ // every row read (over the rationals), so a decision tends to stay feasible (runs/unsat/bmc-only-phase: 320 of
250
+ // CHC-COMP's 475 unsat tasks, 280 without; each atom decided at once at the point instead, 271).
251
+ const phaseAtPoint = (cands, lp) => {
252
+ for (const lin of cands) {
253
+ const x = vars.get(ensure(lin));
254
+ if (x.val !== undefined || x.terms.some(([key]) => key.startsWith('⟦')))
255
+ continue;
256
+ const at = lp.holds(x.lin);
257
+ if (at !== undefined)
258
+ x.phase = at;
259
+ }
260
+ };
223
261
  let restartAt = 32, nRestart = 0, sinceRestart = 0;
224
262
  const stuck = (kind, why, extra = {}) => ({ stuck: kind, path: pathOf(), why, ...extra });
225
- for (;;) {
226
- if (++budget.nodes > 200_000 || clock() > until)
227
- return stuck('unknown', budget.nodes > 200_000 ? 'nodes' : 'time');
228
- let conflict;
229
- const ci = propagate();
230
- if (ci !== undefined)
231
- conflict = clauses[ci];
232
- else {
233
- reader.begin();
234
- st.nodes++;
235
- const rowsR = t(() => reader.rows(), ms => { st.rowsMs += ms; });
236
- const full = rowsR;
237
- if (verify) {
238
- const mine = new Set(rowsR.map(r => canon(r.lin))), theirs = new Set(leafConstraints(goal, pathOf()).map(r => canon(r.lin)));
239
- if (mine.size !== theirs.size || [...mine].some(x => !theirs.has(x)))
240
- throw new Error(`reader.ts's rows differ from kernel.ts's at a node of ${goal.name}`);
263
+ const run = (b, w, assume) => {
264
+ budget = b;
265
+ until = b.until ?? Infinity;
266
+ wall = w;
267
+ last = goal.hyps.length - 1;
268
+ restartAt = 32;
269
+ nRestart = 0;
270
+ sinceRestart = 0;
271
+ undoTo(0);
272
+ qhead = Math.min(qhead, trail.length); // (a run before this one may have left decisions)
273
+ if (assume) {
274
+ const name = ensure(assume);
275
+ assumed = [name, name === canon(assume)];
276
+ }
277
+ else
278
+ assumed = undefined;
279
+ return search();
280
+ };
281
+ const search = () => {
282
+ for (;;) {
283
+ if (++budget.nodes > 200_000 || clock() > until || (wall !== undefined && performance.now() > wall))
284
+ return stuck('unknown', budget.nodes > 200_000 ? 'nodes' : 'time');
285
+ let conflict;
286
+ const ci = propagate();
287
+ if (ci !== undefined)
288
+ conflict = clauses[ci];
289
+ else if (assumed && vars.get(assumed[0]).val === !assumed[1])
290
+ return { stuck: 'unknown', path: new Map(), why: REFUTED };
291
+ else if (assumed && vars.get(assumed[0]).val === undefined) {
292
+ levels.push(trail.length);
293
+ assign(assumed[0], assumed[1]);
294
+ continue;
241
295
  }
242
- work.n += 1 + full.length;
243
- // (the elimination below reads term columns' rows only once the trail has an atom about one, as growPlain does)
244
- let columns = false;
245
- for (let i = 0; i < trail.length && !columns; i++)
246
- for (const key in vars.get(trail[i]).lin)
247
- if (key.startsWith('⟦')) {
248
- columns = true;
249
- break;
250
- }
251
- const idx = columns ? undefined : full.flatMap((r, i) => (namesOf(r.lin).some(key => key.startsWith('⟦')) ? [] : [i]));
252
- const rows = idx ? idx.map(i => full[i]) : full, at = (i) => (!idx ? i : i < rows.length ? idx[i] : full.length + i - rows.length);
253
- // The candidates, in growPlain's order: the units, the conclusion's open atoms, the hypotheses' near it, the
254
- // rest, pairs of cells.
255
- const cands = t(() => {
256
- const out = [...unitAtoms(goal)];
257
- if (last >= 0)
258
- reader.open(last, out);
259
- for (let i = 0; i < last; i++)
260
- if (near[i])
261
- reader.open(i, out);
262
- for (let i = 0; i < last; i++)
263
- if (!near[i])
264
- reader.open(i, out);
265
- if (rows.some(r => namesOf(r.lin).some(isCell)))
266
- for (const x of openCells(rows, pathOf()))
267
- out.push(x);
268
- const seen = new Set();
269
- return out.filter(lin => !seen.has(lin) && (seen.add(lin), namesOf(lin).length > 0) && !decidedLin(lin));
270
- }, ms => { st.orderMs += ms; });
271
- const open = cands.length > 0;
272
- // Bound propagation: an open atom its variables' bounds (the single-variable rows) decide.
273
- const tb = timing ? performance.now() : 0;
274
- if (open) {
275
- const lo = new Map(), hi = new Map();
276
- full.forEach((r, i) => {
277
- const ks = namesOf(r.lin);
278
- if (ks.length !== 1)
279
- return;
280
- const x0 = ks[0], c = r.lin[x0], k0 = r.lin['1'] ?? 0;
281
- if (c > 0) {
282
- const b = Math.floor(-k0 / c);
283
- if (b < (hi.get(x0)?.[0] ?? Infinity))
284
- hi.set(x0, [b, i]);
296
+ else {
297
+ reader.begin();
298
+ st.nodes++;
299
+ const rowsR = t(() => reader.rows(), ms => { st.rowsMs += ms; });
300
+ const full = rowsR;
301
+ if (verify) {
302
+ const mine = new Set(rowsR.map(r => canon(r.lin))), theirs = new Set(leafConstraints(goal, pathOf()).map(r => canon(r.lin)));
303
+ if (mine.size !== theirs.size || [...mine].some(x => !theirs.has(x)))
304
+ throw new Error(`reader.ts's rows differ from kernel.ts's at a node of ${goal.name}`);
305
+ }
306
+ work.n += 1 + full.length;
307
+ // (the elimination below reads term columns' rows only once the trail has an atom about one, as growPlain does)
308
+ let columns = false;
309
+ for (let i = 0; i < trail.length && !columns; i++)
310
+ for (const key in vars.get(trail[i]).lin)
311
+ if (key.startsWith('⟦')) {
312
+ columns = true;
313
+ break;
314
+ }
315
+ const idx = columns ? undefined : full.flatMap((r, i) => (namesOf(r.lin).some(key => key.startsWith('⟦')) ? [] : [i]));
316
+ const rows = idx ? idx.map(i => full[i]) : full, at = (i) => (!idx ? i : i < rows.length ? idx[i] : full.length + i - rows.length);
317
+ // The candidates, in growPlain's order: the units, the conclusion's open atoms, the hypotheses' near it, the
318
+ // rest, pairs of cells.
319
+ const cands = t(() => {
320
+ const out = [...unitAtoms(goal)];
321
+ if (last >= 0)
322
+ reader.open(last, out);
323
+ for (let i = 0; i < last; i++)
324
+ if (near[i])
325
+ reader.open(i, out);
326
+ for (let i = 0; i < last; i++)
327
+ if (!near[i])
328
+ reader.open(i, out);
329
+ if (rows.some(r => namesOf(r.lin).some(isCell)))
330
+ for (const x of openCells(rows, pathOf()))
331
+ out.push(x);
332
+ const seen = new Set();
333
+ return out.filter(lin => !seen.has(lin) && (seen.add(lin), namesOf(lin).length > 0) && !decidedLin(lin));
334
+ }, ms => { st.orderMs += ms; });
335
+ const open = cands.length > 0;
336
+ // Bound propagation: an open atom its variables' bounds (the single-variable rows) decide.
337
+ const tb = timing ? performance.now() : 0;
338
+ if (open) {
339
+ const lo = new Map(), hi = new Map();
340
+ full.forEach((r, i) => {
341
+ const ks = namesOf(r.lin);
342
+ if (ks.length !== 1)
343
+ return;
344
+ const x0 = ks[0], c = r.lin[x0], k0 = r.lin['1'] ?? 0;
345
+ if (c > 0) {
346
+ const b = Math.floor(-k0 / c);
347
+ if (b < (hi.get(x0)?.[0] ?? Infinity))
348
+ hi.set(x0, [b, i]);
349
+ }
350
+ else if (c < 0) {
351
+ const b = Math.ceil(k0 / -c);
352
+ if (b > (lo.get(x0)?.[0] ?? -Infinity))
353
+ lo.set(x0, [b, i]);
354
+ }
355
+ });
356
+ let forced = 0;
357
+ for (const lin of cands) {
358
+ const name = ensure(lin), x = vars.get(name);
359
+ if (x.val !== undefined || namesOf(x.lin).some(key => key.startsWith('⟦')))
360
+ continue; // (a term column's rows read differently once its choice is decided)
361
+ const side = (val) => {
362
+ let sum = x.lin['1'] ?? 0;
363
+ const used = [];
364
+ for (const [x0, c] of x.terms) {
365
+ const b = (c > 0) === val ? hi.get(x0) : lo.get(x0);
366
+ if (!b)
367
+ return undefined;
368
+ sum += c * b[0];
369
+ used.push(b[1]);
370
+ }
371
+ return (val ? sum <= 0 : sum >= 1) ? used : undefined;
372
+ };
373
+ for (const val of [true, false]) {
374
+ const used = side(val);
375
+ if (!used)
376
+ continue;
377
+ const w = new Set();
378
+ for (const i of used)
379
+ for (const n of rowsR[i].w)
380
+ w.add(n);
381
+ clauses.push({ lits: new Map([[name, !val]]), node: -1 });
382
+ pending.set(clauses.length - 1, { w, used: used.map(i => full[i].lin), name, val });
383
+ assign(name, val, clauses.length - 1);
384
+ forced++;
385
+ break;
386
+ }
285
387
  }
286
- else if (c < 0) {
287
- const b = Math.ceil(k0 / -c);
288
- if (b > (lo.get(x0)?.[0] ?? -Infinity))
289
- lo.set(x0, [b, i]);
388
+ if (timing)
389
+ st.boundMs += performance.now() - tb;
390
+ if (forced) {
391
+ st.boundRounds++;
392
+ continue;
290
393
  }
291
- });
292
- let forced = 0;
293
- for (const lin of cands) {
294
- const name = ensure(lin), x = vars.get(name);
295
- if (x.val !== undefined || namesOf(x.lin).some(key => key.startsWith('⟦')))
296
- continue; // (a term column's rows read differently once its choice is decided)
297
- const side = (val) => {
298
- let sum = x.lin['1'] ?? 0;
299
- const used = [];
300
- for (const [x0, c] of x.terms) {
301
- const b = (c > 0) === val ? hi.get(x0) : lo.get(x0);
302
- if (!b)
303
- return undefined;
304
- sum += c * b[0];
305
- used.push(b[1]);
394
+ }
395
+ let steps, lpSat = false;
396
+ let verdict;
397
+ t(() => {
398
+ // The simplex first (warm from the node before): a conflict's rows, refuted exactly, are the leaf. With a case
399
+ // still open, a node it finds feasible is split; with none, the exact elimination below decides it (integers).
400
+ if (sx) {
401
+ const t0 = timing ? performance.now() : 0;
402
+ // (a tableau that has gathered many more forms than a node reads is begun again: every pivot updates them all)
403
+ if (sx.size > 4 * full.length + 500 || sx.nnz > lpFill * full.length) {
404
+ sx = new Simplex();
405
+ st.lpRebuilds++;
306
406
  }
307
- return (val ? sum <= 0 : sum >= 1) ? used : undefined;
308
- };
309
- for (const val of [true, false]) {
310
- const used = side(val);
311
- if (!used)
312
- continue;
313
- const w = new Set();
314
- for (const i of used)
315
- for (const n of rowsR[i].w)
316
- w.add(n);
317
- clauses.push({ lits: new Map([[name, !val]]), node: -1 });
318
- pending.set(clauses.length - 1, { w, used: used.map(i => full[i].lin), name, val });
319
- assign(name, val, clauses.length - 1);
320
- forced++;
321
- break;
407
+ const p0 = sx.pivots;
408
+ let lp = sx.load(full);
409
+ if (lp.kind === 'sat')
410
+ lp = sx.check();
411
+ if (timing) {
412
+ st.lpMs += performance.now() - t0;
413
+ st.pivots += sx.pivots - p0;
414
+ st.lpRows = sx.size;
415
+ st.lpNnz = sx.nnz;
416
+ st.lpVars = sx.vars;
417
+ st.lpLoad = full.length;
418
+ if (lp.kind === 'unknown')
419
+ st.lpUnknown++;
420
+ }
421
+ if (lp.kind === 'conflict') {
422
+ const support = lp.rows.sort((x, y) => x - y), out = solve(support.map(i => full[i]));
423
+ if (out.kind === 'refuted')
424
+ steps = out.steps.map(s => (s.kind === 'input' ? { ...s, index: support[s.index] } : s));
425
+ else
426
+ st.lpMisses++;
427
+ }
428
+ lpSat = lp.kind === 'sat';
429
+ if (steps || (open && lp.kind === 'sat'))
430
+ return;
322
431
  }
323
- }
324
- if (timing)
325
- st.boundMs += performance.now() - tb;
326
- if (forced) {
327
- st.boundRounds++;
328
- continue;
329
- }
330
- }
331
- let steps;
332
- let verdict;
333
- t(() => {
334
- // The simplex first (warm from the node before): a conflict's rows, refuted exactly, are the leaf. With a case
335
- // still open, a node it finds feasible is split; with none, the exact elimination below decides it (integers).
336
- if (sx) {
337
- const t0 = timing ? performance.now() : 0;
338
- // (a tableau that has gathered many more forms than a node reads is begun again: every pivot updates them all)
339
- if (sx.size > 4 * full.length + 500 || sx.nnz > lpFill * full.length) {
340
- sx = new Simplex();
341
- st.lpRebuilds++;
432
+ st.fmCalls++;
433
+ if (open && rows.length > 60) {
434
+ const fast = refuteFast(rows);
435
+ if (fast?.kind === 'refuted')
436
+ steps = fast.steps.map(s => (s.kind === 'input' ? { ...s, index: at(s.index) } : s));
342
437
  }
343
- const p0 = sx.pivots;
344
- let lp = sx.load(full);
345
- if (lp.kind === 'sat')
346
- lp = sx.check();
347
- if (timing) {
348
- st.lpMs += performance.now() - t0;
349
- st.pivots += sx.pivots - p0;
350
- st.lpRows = sx.size;
351
- st.lpNnz = sx.nnz;
352
- st.lpVars = sx.vars;
353
- st.lpLoad = full.length;
354
- if (lp.kind === 'unknown')
355
- st.lpUnknown++;
438
+ if (!steps) {
439
+ verdict = open && rows.length > 60 ? solve(rows, 2 * rows.length) : solve(rows);
440
+ if (verdict.kind === 'refuted')
441
+ steps = verdict.steps.map(s => (s.kind === 'input' ? { ...s, index: at(s.index) } : s));
356
442
  }
357
- if (lp.kind === 'conflict') {
358
- const support = lp.rows.sort((x, y) => x - y), out = solve(support.map(i => full[i]));
359
- if (out.kind === 'refuted')
360
- steps = out.steps.map(s => (s.kind === 'input' ? { ...s, index: support[s.index] } : s));
361
- else
362
- st.lpMisses++;
443
+ if (!steps && open && !columns && full.length > rows.length) {
444
+ const fast = refuteFast(full);
445
+ if (fast?.kind === 'refuted')
446
+ steps = fast.steps;
363
447
  }
364
- if (steps || (open && lp.kind === 'sat'))
365
- return;
366
- }
367
- st.fmCalls++;
368
- if (open && rows.length > 60) {
369
- const fast = refuteFast(rows);
370
- if (fast?.kind === 'refuted')
371
- steps = fast.steps.map(s => (s.kind === 'input' ? { ...s, index: at(s.index) } : s));
372
- }
373
- if (!steps) {
374
- verdict = open && rows.length > 60 ? solve(rows, 2 * rows.length) : solve(rows);
375
- if (verdict.kind === 'refuted')
376
- steps = verdict.steps.map(s => (s.kind === 'input' ? { ...s, index: at(s.index) } : s));
377
- }
378
- if (!steps && open && !columns && full.length > rows.length) {
379
- const fast = refuteFast(full);
380
- if (fast?.kind === 'refuted')
381
- steps = fast.steps;
382
- }
383
- }, ms => { st.solveMs += ms; });
384
- if (steps)
385
- conflict = leafAt(rowsR, steps);
386
- else {
387
- // A decision: the open atom of highest activity (the first candidate among equals).
388
- let pick, best = -1;
389
- for (const lin of cands) {
390
- const name = ensure(lin), x = vars.get(name);
391
- if (x.val === undefined && x.act > best) {
392
- best = x.act;
393
- pick = name;
448
+ }, ms => { st.solveMs += ms; });
449
+ if (steps)
450
+ conflict = leafAt(rowsR, steps);
451
+ else {
452
+ if (opts.model && lpSat && sx && open)
453
+ phaseAtPoint(cands, sx);
454
+ // A decision: the open atom of highest activity (the first candidate among equals).
455
+ let pick, best = -1;
456
+ for (const lin of cands) {
457
+ const name = ensure(lin), x = vars.get(name);
458
+ if (x.val === undefined && x.act > best) {
459
+ best = x.act;
460
+ pick = name;
461
+ }
394
462
  }
395
- }
396
- if (!pick && verdict?.kind === 'unknown') {
397
- for (const [x0, value] of Object.entries(verdict.point ?? {})) {
398
- if (Math.abs(value - Math.round(value)) < 1e-9 || isMonomial(x0))
399
- continue;
400
- const c = Math.floor(value), lin = c === 0 ? { [x0]: 1 } : { [x0]: 1, 1: -c };
401
- if (!decidedLin(lin)) {
402
- pick = ensure(lin);
403
- break;
463
+ if (!pick && verdict?.kind === 'unknown') {
464
+ for (const [x0, value] of Object.entries(verdict.point ?? {})) {
465
+ if (Math.abs(value - Math.round(value)) < 1e-9 || isMonomial(x0))
466
+ continue;
467
+ const c = Math.floor(value), lin = c === 0 ? { [x0]: 1 } : { [x0]: 1, 1: -c };
468
+ if (!decidedLin(lin)) {
469
+ pick = ensure(lin);
470
+ break;
471
+ }
404
472
  }
405
473
  }
406
- }
407
- // Nothing open by this reading: the kernel's own reading of the trail (prove.ts's) has the last word.
408
- if (!pick) {
409
- const p = pathOf(), more = openAt(goal, p, rowsAt(goal, p)).find(lin => Object.keys(lin).some(key => key !== '1') && !decidedLin(lin));
410
- if (more) {
411
- st.openMisses++;
412
- pick = ensure(more);
474
+ // Nothing open by this reading: the kernel's own reading of the trail (prove.ts's) has the last word.
475
+ if (!pick) {
476
+ const p = pathOf(), more = openAt(goal, p, rowsAt(goal, p)).find(lin => Object.keys(lin).some(key => key !== '1') && !decidedLin(lin));
477
+ if (more) {
478
+ st.openMisses++;
479
+ pick = ensure(more);
480
+ }
413
481
  }
482
+ if (!pick)
483
+ return verdict?.kind === 'sat' ? stuck('sat', 'a model', { witness: verdict.witness }) : stuck('unknown', 'no split');
484
+ levels.push(trail.length);
485
+ st.decisions++;
486
+ assign(pick, vars.get(pick).phase);
487
+ continue;
414
488
  }
415
- if (!pick)
416
- return verdict?.kind === 'sat' ? stuck('sat', 'a model', { witness: verdict.witness }) : stuck('unknown', 'no split');
417
- levels.push(trail.length);
418
- st.decisions++;
419
- assign(pick, vars.get(pick).phase);
420
- continue;
421
489
  }
422
- }
423
- // Conflict analysis.
424
- st.conflicts++;
425
- sinceRestart++;
426
- let c = conflict;
427
- for (;;) {
428
- if (!c.lits.size) {
429
- const tree = t(() => unfold(goal, nodes, c.node, root, opts.maxLeaves ?? 20_000, until), ms => { st.unfoldMs += ms; });
430
- if (!tree)
431
- return stuck('unknown', 'unfold');
432
- st.proven++;
433
- return { tree, nodes: budget.nodes };
490
+ // Conflict analysis.
491
+ st.conflicts++;
492
+ sinceRestart++;
493
+ let c = conflict;
494
+ for (;;) {
495
+ if (!c.lits.size) {
496
+ if (opts.model)
497
+ return { stuck: 'unknown', path: new Map(), why: REFUTED };
498
+ const tree = t(() => unfold(goal, nodes, c.node, root, opts.maxLeaves ?? 20_000, until), ms => { st.unfoldMs += ms; });
499
+ if (!tree)
500
+ return stuck('unknown', 'unfold');
501
+ st.proven++;
502
+ return { tree, nodes: budget.nodes };
503
+ }
504
+ const top = Math.max(...[...c.lits.keys()].map(name => vars.get(name).level));
505
+ if (top < level()) {
506
+ undoTo(top);
507
+ qhead = trail.length;
508
+ }
509
+ const here = [...c.lits.keys()].filter(name => vars.get(name).level === top);
510
+ if (top === 0) {
511
+ c = resolveOn(c, here.reduce((a, b) => (vars.get(a).at > vars.get(b).at ? a : b)));
512
+ continue;
513
+ }
514
+ if (here.length === 1)
515
+ break;
516
+ const latest = here.reduce((a, b) => (vars.get(a).at > vars.get(b).at ? a : b));
517
+ if (vars.get(latest).reason === undefined)
518
+ break;
519
+ c = resolveOn(c, latest);
434
520
  }
435
521
  const top = Math.max(...[...c.lits.keys()].map(name => vars.get(name).level));
436
- if (top < level()) {
437
- undoTo(top);
438
- qhead = trail.length;
439
- }
440
522
  const here = [...c.lits.keys()].filter(name => vars.get(name).level === top);
441
- if (top === 0) {
442
- c = resolveOn(c, here.reduce((a, b) => (vars.get(a).at > vars.get(b).at ? a : b)));
523
+ bump(c.lits);
524
+ const learned = addClause(c.lits, c.node);
525
+ if (here.length !== 1) {
526
+ undoTo(Math.max(0, top - 1));
527
+ qhead = Math.min(qhead, trail.length);
443
528
  continue;
444
529
  }
445
- if (here.length === 1)
446
- break;
447
- const latest = here.reduce((a, b) => (vars.get(a).at > vars.get(b).at ? a : b));
448
- if (vars.get(latest).reason === undefined)
449
- break;
450
- c = resolveOn(c, latest);
451
- }
452
- const top = Math.max(...[...c.lits.keys()].map(name => vars.get(name).level));
453
- const here = [...c.lits.keys()].filter(name => vars.get(name).level === top);
454
- bump(c.lits);
455
- const learned = addClause(c.lits, c.node);
456
- if (here.length !== 1) {
457
- undoTo(Math.max(0, top - 1));
458
- qhead = Math.min(qhead, trail.length);
459
- continue;
460
- }
461
- const uip = here[0];
462
- undoTo(Math.max(0, ...[...c.lits.keys()].filter(name => name !== uip).map(name => vars.get(name).level)));
463
- qhead = trail.length;
464
- assign(uip, !c.lits.get(uip), learned);
465
- if (sinceRestart >= restartAt) {
466
- sinceRestart = 0;
467
- restartAt = 32 * luby(++nRestart);
468
- undoTo(0);
469
- qhead = Math.min(qhead, trail.length);
530
+ const uip = here[0];
531
+ undoTo(Math.max(0, ...[...c.lits.keys()].filter(name => name !== uip).map(name => vars.get(name).level)));
532
+ qhead = trail.length;
533
+ assign(uip, !c.lits.get(uip), learned);
534
+ if (sinceRestart >= restartAt) {
535
+ sinceRestart = 0;
536
+ restartAt = 32 * luby(++nRestart);
537
+ undoTo(0);
538
+ qhead = Math.min(qhead, trail.length);
539
+ }
470
540
  }
471
- }
541
+ };
542
+ return { run };
472
543
  }
473
544
  // The DAG as a kernel tree below `root`, walked depth first with a reader of its own (the tree's path is a stack, as
474
545
  // the search's trail is): each leaf's derivation found again among the rows at its tree path, in kernel.ts's order (the
@@ -477,11 +548,11 @@ export function growCdcl(goal, root, budget, opts = {}) {
477
548
  function unfold(goal, nodes, top, root, maxLeaves, until) {
478
549
  const stack = [], on = new Map(), stamps = [];
479
550
  let stampN = 0, leaves = 0;
480
- const push = (name, lin, holds) => { on.set(name, { val: holds, at: stack.length }); stamps[stack.length] = ++stampN; stack.push({ name, lin, holds }); };
481
- const pop = () => { on.delete(stack.pop().name); };
551
+ const reader = new Reader(goal, { get: name => on.get(name), stamp: at => stamps[at], size: () => stack.length, lin: at => stack[at] });
552
+ const push = (name, lin, holds) => { on.set(name, { val: holds, at: stack.length }); stamps[stack.length] = ++stampN; stack.push({ name, lin, holds }); reader.touch(name); };
553
+ const pop = () => { const name = stack.pop().name; on.delete(name); reader.touch(name); };
482
554
  for (const [key, a] of root)
483
555
  push(key, a.lin, a.holds);
484
- const reader = new Reader(goal, { get: name => on.get(name), stamp: at => stamps[at], size: () => stack.length, lin: at => stack[at] });
485
556
  const go = (i) => {
486
557
  const n = nodes[i];
487
558
  if ('res' in n) {