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