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.
- package/README.md +12 -2
- package/dist/core/bmc.d.ts +13 -2
- package/dist/core/bmc.js +390 -18
- package/dist/core/cycles.d.ts +23 -0
- package/dist/core/cycles.js +104 -54
- package/dist/core/data.d.ts +9 -0
- package/dist/core/data.js +352 -121
- package/dist/core/discover.js +8 -3
- package/dist/core/dsl.d.ts +7 -2
- package/dist/core/dsl.js +47 -9
- package/dist/core/extract-check.d.ts +77 -0
- package/dist/core/extract-check.js +785 -0
- package/dist/core/extract-ir.d.ts +139 -0
- package/dist/core/extract-ir.js +198 -0
- package/dist/core/extract-print.d.ts +30 -0
- package/dist/core/extract-print.js +426 -0
- package/dist/core/extract-read.d.ts +298 -0
- package/dist/core/extract-read.js +1298 -0
- package/dist/core/extract-shapes.d.ts +13 -0
- package/dist/core/extract-shapes.js +993 -0
- package/dist/core/extract-xstate.d.ts +34 -0
- package/dist/core/extract-xstate.js +320 -0
- package/dist/core/extract.d.ts +31 -0
- package/dist/core/extract.js +249 -0
- package/dist/core/kind.d.ts +30 -0
- package/dist/core/kind.js +81 -0
- package/dist/core/relation.js +7 -4
- package/dist/core/xstate.d.ts +6 -0
- package/dist/core/xstate.js +70 -12
- package/dist/kernel/cdcl.d.ts +10 -0
- package/dist/kernel/cdcl.js +291 -241
- package/dist/kernel/kernel.d.ts +15 -0
- package/dist/kernel/kernel.js +32 -0
- package/dist/kernel/prove.js +14 -3
- package/dist/kernel/reader.d.ts +14 -0
- package/dist/kernel/reader.js +102 -14
- package/dist/lean-kernel/emit.d.ts +28 -1
- package/dist/lean-kernel/emit.js +56 -15
- package/dist/lean-kernel/library.js +2 -1
- package/package.json +12 -3
package/dist/kernel/cdcl.js
CHANGED
|
@@ -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
|
-
|
|
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
|
|
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
|
-
|
|
91
|
-
const
|
|
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(
|
|
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
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
const
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
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
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
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
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
388
|
+
if (timing)
|
|
389
|
+
st.boundMs += performance.now() - tb;
|
|
390
|
+
if (forced) {
|
|
391
|
+
st.boundRounds++;
|
|
392
|
+
continue;
|
|
306
393
|
}
|
|
307
|
-
}
|
|
308
|
-
let
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
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
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
if (
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
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
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
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
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
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 (
|
|
374
|
-
const
|
|
375
|
-
if (
|
|
376
|
-
steps =
|
|
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
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
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
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
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
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
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
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
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
|
-
|
|
463
|
-
|
|
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
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
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
|
|
502
|
-
const
|
|
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) {
|