gdp-wired 0.23.0 → 0.25.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 (47) hide show
  1. package/README.md +12 -2
  2. package/dist/core/accel.d.ts +44 -0
  3. package/dist/core/accel.js +720 -0
  4. package/dist/core/bmc.d.ts +13 -2
  5. package/dist/core/bmc.js +390 -18
  6. package/dist/core/cycles.d.ts +23 -0
  7. package/dist/core/cycles.js +527 -146
  8. package/dist/core/data.d.ts +17 -0
  9. package/dist/core/data.js +1177 -213
  10. package/dist/core/discover.js +8 -3
  11. package/dist/core/dsl.d.ts +7 -2
  12. package/dist/core/dsl.js +47 -9
  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.d.ts +1 -0
  28. package/dist/core/infer.js +298 -28
  29. package/dist/core/kind.d.ts +30 -0
  30. package/dist/core/kind.js +81 -0
  31. package/dist/core/nonterm.d.ts +4 -1
  32. package/dist/core/nonterm.js +98 -2
  33. package/dist/core/relation.js +82 -7
  34. package/dist/core/transfer.js +1 -1
  35. package/dist/core/xstate.d.ts +6 -0
  36. package/dist/core/xstate.js +70 -12
  37. package/dist/kernel/cdcl.d.ts +15 -0
  38. package/dist/kernel/cdcl.js +331 -244
  39. package/dist/kernel/kernel.d.ts +15 -0
  40. package/dist/kernel/kernel.js +32 -0
  41. package/dist/kernel/prove.js +20 -7
  42. package/dist/kernel/reader.d.ts +14 -0
  43. package/dist/kernel/reader.js +102 -14
  44. package/dist/lean-kernel/emit.d.ts +28 -1
  45. package/dist/lean-kernel/emit.js +56 -15
  46. package/dist/lean-kernel/library.js +2 -1
  47. 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 ?? {};
@@ -47,9 +57,21 @@ const lpFill = Number(env.GDP_CDCL_FILL ?? 8), useLp = env.GDP_CDCL_LP !== '0',
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 };
48
58
  // `opts.model`: only whether the goal has a model is wanted (core/bmc.ts): a refutation isn't unfolded into a tree, its
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.
60
+ // `opts.core`, with `model`: a refutation's outcome also names the hypotheses its leaves read (`core`, their indices in
61
+ // goal.hyps): those alone have no model (each leaf's rows are those hypotheses' facts at its decisions, or rows true of
62
+ // any value: the trail's atoms, the naturals, the term columns' definitions), as core/infer.ts's join reads it.
50
63
  export const REFUTED = 'refuted, not unfolded';
51
64
  export function growCdcl(goal, root, budget, opts = {}) {
52
- const until = budget.until ?? Infinity, st = cdclStats, timing = st.on;
65
+ return cdclSession(goal, root, opts).run(budget, opts.wall);
66
+ }
67
+ // A search kept from one run to the next (core/bmc.ts's deeper goals): its atoms, their activities and phases, its learned
68
+ // clauses and the simplex's tableau. Between runs the caller may add hypotheses to the goal (goal.hyps grows; none is
69
+ // taken away), so every clause learned stays a consequence of the goal. A run may assume an atom (`assume`, decided
70
+ // first, at level 1): where the search learns it false the run ends refuted (REFUTED), as where nothing is assumed and
71
+ // the goal is refuted. Each run's own budget: nodes, `until` on the counted clock, `wall`.
72
+ export function cdclSession(goal, root, opts = {}) {
73
+ let until = Infinity, budget = { nodes: 0 }, wall, assumed;
74
+ const st = cdclStats, timing = st.on;
53
75
  const t = (f, add) => { if (!timing)
54
76
  return f(); const t0 = performance.now(); try {
55
77
  return f();
@@ -61,12 +83,21 @@ export function growCdcl(goal, root, budget, opts = {}) {
61
83
  const vars = new Map(), trail = [], levels = [];
62
84
  let sx = useLp ? new Simplex() : undefined;
63
85
  const nodes = [], clauses = [], occ = new Map();
86
+ // (with opts.core: each leaf's hypotheses, by node; the hypothesis each row at this node came from)
87
+ const leafHyps = new Map(), wantCore = !!(opts.core && opts.model);
88
+ const hypsOf = (rs) => {
89
+ const at = new Map();
90
+ for (let i = 0; i < goal.hyps.length; i++)
91
+ for (const r of reader.facts(i).rows)
92
+ at.set(r, i);
93
+ return [...new Set(rs.flatMap(r => { const i = at.get(r); return i === undefined ? [] : [i]; }))];
94
+ };
64
95
  let inc = 1;
65
96
  // An atom as a variable: the stored form is the one whose canonical text is the variable's name (the smaller).
66
97
  const ensure = (lin) => {
67
- const a = canon(lin), b = canonFlip(lin), flip = b < a, key = flip ? b : a;
98
+ const key = keyOf(lin);
68
99
  if (!vars.has(key)) {
69
- const stored = flip ? atomOf(lin, false).lin : lin;
100
+ const flip = key !== canon(lin), stored = flip ? atomOf(lin, false).lin : lin;
70
101
  vars.set(key, { lin: stored, terms: Object.entries(stored).filter(([x0]) => x0 !== '1'), level: -1, at: -1, act: 0, phase: !flip });
71
102
  }
72
103
  return key;
@@ -87,8 +118,9 @@ export function growCdcl(goal, root, budget, opts = {}) {
87
118
  get: name => vars.get(name), stamp: at => stamps[at], size: () => trail.length,
88
119
  lin: at => { const name = trail[at], x = vars.get(name); return { name, lin: x.lin, holds: x.val }; },
89
120
  });
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;
121
+ let last = goal.hyps.length - 1;
122
+ const near = nearOf(goal);
123
+ const decidedLin = (lin) => vars.get(keyOf(lin))?.val !== undefined;
92
124
  const fixed = trail.length;
93
125
  const level = () => levels.length;
94
126
  const pathOf = () => new Map(trail.map(name => { const x = vars.get(name); return [name, { lin: x.lin, holds: x.val }]; }));
@@ -104,14 +136,16 @@ export function growCdcl(goal, root, budget, opts = {}) {
104
136
  else
105
137
  x.reason = reason;
106
138
  trail.push(name);
139
+ reader.touch(name);
107
140
  };
108
141
  const undoTo = (l) => {
109
142
  const keep = levels[l] ?? trail.length;
110
143
  while (trail.length > Math.max(keep, fixed)) {
111
- const x = vars.get(trail.pop());
144
+ const name = trail.pop(), x = vars.get(name);
112
145
  delete x.val;
113
146
  x.level = -1;
114
147
  delete x.reason;
148
+ reader.touch(name);
115
149
  }
116
150
  levels.length = l;
117
151
  };
@@ -184,8 +218,10 @@ export function growCdcl(goal, root, budget, opts = {}) {
184
218
  };
185
219
  // A leaf: the decisions of a reason (the root's are every leaf's, below which the tree starts: not literals of its
186
220
  // clause) and a derivation over the rows it reads.
187
- const leafNode = (w, inputs, steps, extra) => {
221
+ const leafNode = (w, inputs, steps, extra, hyps) => {
188
222
  nodes.push({ inputs, steps });
223
+ if (hyps)
224
+ leafHyps.set(nodes.length - 1, hyps);
189
225
  st.leaves++;
190
226
  const lits = new Map();
191
227
  for (const name of w)
@@ -205,7 +241,7 @@ export function growCdcl(goal, root, budget, opts = {}) {
205
241
  w.add(name);
206
242
  st.pathSize += trail.length - fixed;
207
243
  const local = localize(rows, steps);
208
- return leafNode(w, local.inputs, local.steps);
244
+ return leafNode(w, local.inputs, local.steps, undefined, wantCore ? hypsOf(steps.flatMap(s => (s.kind === 'input' ? [rows[s.index]] : []))) : undefined);
209
245
  }, ms => { st.coreMs += ms; });
210
246
  // Bound propagation's reasons, made when conflict analysis reads them: the atom's bounds (single-variable rows at the
211
247
  // trail then) and its other side refute each other, a leaf at the bounds' reasons with that side decided.
@@ -217,7 +253,7 @@ export function growCdcl(goal, root, budget, opts = {}) {
217
253
  const s = solve(local);
218
254
  if (s.kind !== 'refuted')
219
255
  throw new Error('a bound propagation its bounds do not refute');
220
- const loc = localize(local, s.steps), made = leafNode(b.w, loc.inputs, loc.steps, [b.name, !b.val]);
256
+ const loc = localize(local, s.steps), made = leafNode(b.w, loc.inputs, loc.steps, [b.name, !b.val], b.hyps);
221
257
  c.lits = made.lits;
222
258
  c.node = made.node;
223
259
  }, ms => { st.coreMs += ms; });
@@ -238,258 +274,309 @@ export function growCdcl(goal, root, budget, opts = {}) {
238
274
  };
239
275
  let restartAt = 32, nRestart = 0, sinceRestart = 0;
240
276
  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}`);
277
+ const run = (b, w, assume) => {
278
+ budget = b;
279
+ until = b.until ?? Infinity;
280
+ wall = w;
281
+ last = goal.hyps.length - 1;
282
+ restartAt = 32;
283
+ nRestart = 0;
284
+ sinceRestart = 0;
285
+ undoTo(0);
286
+ qhead = Math.min(qhead, trail.length); // (a run before this one may have left decisions)
287
+ if (assume) {
288
+ const name = ensure(assume);
289
+ assumed = [name, name === canon(assume)];
290
+ }
291
+ else
292
+ assumed = undefined;
293
+ return search();
294
+ };
295
+ const search = () => {
296
+ for (;;) {
297
+ if (++budget.nodes > 200_000 || clock() > until || (wall !== undefined && performance.now() > wall))
298
+ return stuck('unknown', budget.nodes > 200_000 ? 'nodes' : 'time');
299
+ let conflict;
300
+ const ci = propagate();
301
+ if (ci !== undefined)
302
+ conflict = clauses[ci];
303
+ else if (assumed && vars.get(assumed[0]).val === !assumed[1])
304
+ return { stuck: 'unknown', path: new Map(), why: REFUTED };
305
+ else if (assumed && vars.get(assumed[0]).val === undefined) {
306
+ levels.push(trail.length);
307
+ assign(assumed[0], assumed[1]);
308
+ continue;
257
309
  }
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]);
310
+ else {
311
+ reader.begin();
312
+ st.nodes++;
313
+ const rowsR = t(() => reader.rows(), ms => { st.rowsMs += ms; });
314
+ const full = rowsR;
315
+ if (verify) {
316
+ const mine = new Set(rowsR.map(r => canon(r.lin))), theirs = new Set(leafConstraints(goal, pathOf()).map(r => canon(r.lin)));
317
+ if (mine.size !== theirs.size || [...mine].some(x => !theirs.has(x)))
318
+ throw new Error(`reader.ts's rows differ from kernel.ts's at a node of ${goal.name}`);
319
+ }
320
+ work.n += 1 + full.length;
321
+ // (the elimination below reads term columns' rows only once the trail has an atom about one, as growPlain does)
322
+ let columns = false;
323
+ for (let i = 0; i < trail.length && !columns; i++)
324
+ for (const key in vars.get(trail[i]).lin)
325
+ if (key.startsWith('⟦')) {
326
+ columns = true;
327
+ break;
328
+ }
329
+ const idx = columns ? undefined : full.flatMap((r, i) => (namesOf(r.lin).some(key => key.startsWith('⟦')) ? [] : [i]));
330
+ const rows = idx ? idx.map(i => full[i]) : full, at = (i) => (!idx ? i : i < rows.length ? idx[i] : full.length + i - rows.length);
331
+ // The candidates, in growPlain's order: the units, the conclusion's open atoms, the hypotheses' near it, the
332
+ // rest, pairs of cells.
333
+ const cands = t(() => {
334
+ const out = [...unitAtoms(goal)];
335
+ if (last >= 0)
336
+ reader.open(last, out);
337
+ for (let i = 0; i < last; i++)
338
+ if (near[i])
339
+ reader.open(i, out);
340
+ for (let i = 0; i < last; i++)
341
+ if (!near[i])
342
+ reader.open(i, out);
343
+ if (rows.some(r => namesOf(r.lin).some(isCell)))
344
+ for (const x of openCells(rows, pathOf()))
345
+ out.push(x);
346
+ const seen = new Set();
347
+ return out.filter(lin => !seen.has(lin) && (seen.add(lin), namesOf(lin).length > 0) && !decidedLin(lin));
348
+ }, ms => { st.orderMs += ms; });
349
+ const open = cands.length > 0;
350
+ // Bound propagation: an open atom its variables' bounds (the single-variable rows) decide.
351
+ const tb = timing ? performance.now() : 0;
352
+ if (open) {
353
+ const lo = new Map(), hi = new Map();
354
+ full.forEach((r, i) => {
355
+ const ks = namesOf(r.lin);
356
+ if (ks.length !== 1)
357
+ return;
358
+ const x0 = ks[0], c = r.lin[x0], k0 = r.lin['1'] ?? 0;
359
+ if (c > 0) {
360
+ const b = Math.floor(-k0 / c);
361
+ if (b < (hi.get(x0)?.[0] ?? Infinity))
362
+ hi.set(x0, [b, i]);
363
+ }
364
+ else if (c < 0) {
365
+ const b = Math.ceil(k0 / -c);
366
+ if (b > (lo.get(x0)?.[0] ?? -Infinity))
367
+ lo.set(x0, [b, i]);
368
+ }
369
+ });
370
+ let forced = 0;
371
+ for (const lin of cands) {
372
+ const name = ensure(lin), x = vars.get(name);
373
+ if (x.val !== undefined || namesOf(x.lin).some(key => key.startsWith('⟦')))
374
+ continue; // (a term column's rows read differently once its choice is decided)
375
+ const side = (val) => {
376
+ let sum = x.lin['1'] ?? 0;
377
+ const used = [];
378
+ for (const [x0, c] of x.terms) {
379
+ const b = (c > 0) === val ? hi.get(x0) : lo.get(x0);
380
+ if (!b)
381
+ return undefined;
382
+ sum += c * b[0];
383
+ used.push(b[1]);
384
+ }
385
+ return (val ? sum <= 0 : sum >= 1) ? used : undefined;
386
+ };
387
+ for (const val of [true, false]) {
388
+ const used = side(val);
389
+ if (!used)
390
+ continue;
391
+ const w = new Set();
392
+ for (const i of used)
393
+ for (const n of rowsR[i].w)
394
+ w.add(n);
395
+ clauses.push({ lits: new Map([[name, !val]]), node: -1 });
396
+ pending.set(clauses.length - 1, { w, used: used.map(i => full[i].lin), name, val, ...(wantCore ? { hyps: hypsOf(used.map(i => rowsR[i])) } : {}) });
397
+ assign(name, val, clauses.length - 1);
398
+ forced++;
399
+ break;
400
+ }
301
401
  }
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]);
402
+ if (timing)
403
+ st.boundMs += performance.now() - tb;
404
+ if (forced) {
405
+ st.boundRounds++;
406
+ continue;
306
407
  }
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]);
408
+ }
409
+ let steps, lpSat = false;
410
+ let verdict;
411
+ t(() => {
412
+ // The simplex first (warm from the node before): a conflict's rows, refuted exactly, are the leaf. With a case
413
+ // still open, a node it finds feasible is split; with none, the exact elimination below decides it (integers).
414
+ if (sx) {
415
+ const t0 = timing ? performance.now() : 0;
416
+ // (a tableau that has gathered many more forms than a node reads is begun again: every pivot updates them all)
417
+ if (sx.size > 4 * full.length + 500 || sx.nnz > lpFill * full.length) {
418
+ sx = new Simplex();
419
+ st.lpRebuilds++;
322
420
  }
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;
421
+ const p0 = sx.pivots;
422
+ let lp = sx.load(full);
423
+ if (lp.kind === 'sat')
424
+ lp = sx.check();
425
+ if (timing) {
426
+ st.lpMs += performance.now() - t0;
427
+ st.pivots += sx.pivots - p0;
428
+ st.lpRows = sx.size;
429
+ st.lpNnz = sx.nnz;
430
+ st.lpVars = sx.vars;
431
+ st.lpLoad = full.length;
432
+ if (lp.kind === 'unknown')
433
+ st.lpUnknown++;
434
+ }
435
+ if (lp.kind === 'conflict') {
436
+ const support = lp.rows.sort((x, y) => x - y), out = solve(support.map(i => full[i]));
437
+ if (out.kind === 'refuted')
438
+ steps = out.steps.map(s => (s.kind === 'input' ? { ...s, index: support[s.index] } : s));
439
+ else
440
+ st.lpMisses++;
441
+ }
442
+ lpSat = lp.kind === 'sat';
443
+ if (steps || (open && lp.kind === 'sat'))
444
+ return;
338
445
  }
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++;
446
+ st.fmCalls++;
447
+ if (open && rows.length > 60) {
448
+ const fast = refuteFast(rows);
449
+ if (fast?.kind === 'refuted')
450
+ steps = fast.steps.map(s => (s.kind === 'input' ? { ...s, index: at(s.index) } : s));
358
451
  }
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++;
452
+ if (!steps) {
453
+ verdict = open && rows.length > 60 ? solve(rows, 2 * rows.length) : solve(rows);
454
+ if (verdict.kind === 'refuted')
455
+ steps = verdict.steps.map(s => (s.kind === 'input' ? { ...s, index: at(s.index) } : s));
372
456
  }
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++;
457
+ if (!steps && open && !columns && full.length > rows.length) {
458
+ const fast = refuteFast(full);
459
+ if (fast?.kind === 'refuted')
460
+ steps = fast.steps;
379
461
  }
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;
462
+ }, ms => { st.solveMs += ms; });
463
+ if (steps)
464
+ conflict = leafAt(rowsR, steps);
465
+ else {
466
+ if (opts.model && lpSat && sx && open)
467
+ phaseAtPoint(cands, sx);
468
+ // A decision: the open atom of highest activity (the first candidate among equals).
469
+ let pick, best = -1;
470
+ for (const lin of cands) {
471
+ const name = ensure(lin), x = vars.get(name);
472
+ if (x.val === undefined && x.act > best) {
473
+ best = x.act;
474
+ pick = name;
475
+ }
413
476
  }
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;
477
+ if (!pick && verdict?.kind === 'unknown') {
478
+ for (const [x0, value] of Object.entries(verdict.point ?? {})) {
479
+ if (Math.abs(value - Math.round(value)) < 1e-9 || isMonomial(x0))
480
+ continue;
481
+ const c = Math.floor(value), lin = c === 0 ? { [x0]: 1 } : { [x0]: 1, 1: -c };
482
+ if (!decidedLin(lin)) {
483
+ pick = ensure(lin);
484
+ break;
485
+ }
423
486
  }
424
487
  }
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);
488
+ // Nothing open by this reading: the kernel's own reading of the trail (prove.ts's) has the last word.
489
+ if (!pick) {
490
+ const p = pathOf(), more = openAt(goal, p, rowsAt(goal, p)).find(lin => Object.keys(lin).some(key => key !== '1') && !decidedLin(lin));
491
+ if (more) {
492
+ st.openMisses++;
493
+ pick = ensure(more);
494
+ }
432
495
  }
496
+ if (!pick)
497
+ return verdict?.kind === 'sat' ? stuck('sat', 'a model', { witness: verdict.witness }) : stuck('unknown', 'no split');
498
+ levels.push(trail.length);
499
+ st.decisions++;
500
+ assign(pick, vars.get(pick).phase);
501
+ continue;
433
502
  }
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
503
  }
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 };
504
+ // Conflict analysis.
505
+ st.conflicts++;
506
+ sinceRestart++;
507
+ let c = conflict;
508
+ for (;;) {
509
+ if (!c.lits.size) {
510
+ if (opts.model) {
511
+ if (!wantCore)
512
+ return { stuck: 'unknown', path: new Map(), why: REFUTED };
513
+ // the hypotheses the leaves below the empty clause read (a clause never made into a node, a bound's not yet
514
+ // materialized, leaves the core unknown)
515
+ const core = new Set(), seen = new Set(), todo = [c.node];
516
+ let whole = true;
517
+ while (todo.length) {
518
+ const i = todo.pop();
519
+ if (i < 0) {
520
+ whole = false;
521
+ break;
522
+ }
523
+ if (seen.has(i))
524
+ continue;
525
+ seen.add(i);
526
+ const nd = nodes[i];
527
+ if ('res' in nd)
528
+ todo.push(...nd.res);
529
+ else
530
+ for (const h of leafHyps.get(i) ?? [])
531
+ core.add(h);
532
+ }
533
+ return { stuck: 'unknown', path: new Map(), why: REFUTED, ...(whole ? { core: [...core].sort((a, b) => a - b) } : {}) };
534
+ }
535
+ const tree = t(() => unfold(goal, nodes, c.node, root, opts.maxLeaves ?? 20_000, until), ms => { st.unfoldMs += ms; });
536
+ if (!tree)
537
+ return stuck('unknown', 'unfold');
538
+ st.proven++;
539
+ return { tree, nodes: budget.nodes };
540
+ }
541
+ const top = Math.max(...[...c.lits.keys()].map(name => vars.get(name).level));
542
+ if (top < level()) {
543
+ undoTo(top);
544
+ qhead = trail.length;
545
+ }
546
+ const here = [...c.lits.keys()].filter(name => vars.get(name).level === top);
547
+ if (top === 0) {
548
+ c = resolveOn(c, here.reduce((a, b) => (vars.get(a).at > vars.get(b).at ? a : b)));
549
+ continue;
550
+ }
551
+ if (here.length === 1)
552
+ break;
553
+ const latest = here.reduce((a, b) => (vars.get(a).at > vars.get(b).at ? a : b));
554
+ if (vars.get(latest).reason === undefined)
555
+ break;
556
+ c = resolveOn(c, latest);
455
557
  }
456
558
  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
559
  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)));
560
+ bump(c.lits);
561
+ const learned = addClause(c.lits, c.node);
562
+ if (here.length !== 1) {
563
+ undoTo(Math.max(0, top - 1));
564
+ qhead = Math.min(qhead, trail.length);
464
565
  continue;
465
566
  }
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);
567
+ const uip = here[0];
568
+ undoTo(Math.max(0, ...[...c.lits.keys()].filter(name => name !== uip).map(name => vars.get(name).level)));
569
+ qhead = trail.length;
570
+ assign(uip, !c.lits.get(uip), learned);
571
+ if (sinceRestart >= restartAt) {
572
+ sinceRestart = 0;
573
+ restartAt = 32 * luby(++nRestart);
574
+ undoTo(0);
575
+ qhead = Math.min(qhead, trail.length);
576
+ }
491
577
  }
492
- }
578
+ };
579
+ return { run };
493
580
  }
494
581
  // 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
582
  // 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 +585,11 @@ export function growCdcl(goal, root, budget, opts = {}) {
498
585
  function unfold(goal, nodes, top, root, maxLeaves, until) {
499
586
  const stack = [], on = new Map(), stamps = [];
500
587
  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); };
588
+ const reader = new Reader(goal, { get: name => on.get(name), stamp: at => stamps[at], size: () => stack.length, lin: at => stack[at] });
589
+ 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); };
590
+ const pop = () => { const name = stack.pop().name; on.delete(name); reader.touch(name); };
503
591
  for (const [key, a] of root)
504
592
  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
593
  const go = (i) => {
507
594
  const n = nodes[i];
508
595
  if ('res' in n) {