gdp-wired 0.31.1 → 0.33.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 +95 -6
- package/dist/core/c/reader.d.ts +86 -0
- package/dist/core/c/reader.js +860 -0
- package/dist/core/check.js +20 -6
- package/dist/core/cli.d.ts +76 -0
- package/dist/core/cli.js +636 -0
- package/dist/core/dsl.d.ts +6 -4
- package/dist/core/dsl.js +30 -16
- package/dist/core/effect.d.ts +6 -4
- package/dist/core/effect.js +48 -10
- package/dist/core/ex/harness.exs +177 -0
- package/dist/core/ex/json.exs +46 -0
- package/dist/core/ex/reader.exs +462 -0
- package/dist/core/exists.d.ts +3 -0
- package/dist/core/exists.js +19 -5
- package/dist/core/extract-c.d.ts +35 -0
- package/dist/core/extract-c.js +424 -0
- package/dist/core/extract-check.js +27 -6
- package/dist/core/extract-ex.d.ts +16 -0
- package/dist/core/extract-ex.js +33 -0
- package/dist/core/extract-go-check.d.ts +28 -0
- package/dist/core/extract-go-check.js +639 -0
- package/dist/core/extract-go.d.ts +231 -0
- package/dist/core/extract-go.js +1166 -0
- package/dist/core/extract-jvm.d.ts +23 -0
- package/dist/core/extract-jvm.js +220 -0
- package/dist/core/extract-print.js +17 -7
- package/dist/core/extract-py.d.ts +15 -0
- package/dist/core/extract-py.js +31 -0
- package/dist/core/extract-rb.d.ts +17 -0
- package/dist/core/extract-rb.js +31 -0
- package/dist/core/extract-read.d.ts +20 -0
- package/dist/core/extract-read.js +46 -6
- package/dist/core/extract-rs.d.ts +12 -0
- package/dist/core/extract-rs.js +306 -0
- package/dist/core/extract-shapes.d.ts +2 -0
- package/dist/core/extract-shapes.js +6 -5
- package/dist/core/extract-step.d.ts +806 -0
- package/dist/core/extract-step.js +4141 -0
- package/dist/core/extract-swift.d.ts +27 -0
- package/dist/core/extract-swift.js +484 -0
- package/dist/core/extract-xstate.d.ts +10 -3
- package/dist/core/extract-xstate.js +50 -4
- package/dist/core/extract.d.ts +22 -3
- package/dist/core/extract.js +107 -18
- package/dist/core/go/go.mod +3 -0
- package/dist/core/go/reader.go +901 -0
- package/dist/core/infer.d.ts +4 -0
- package/dist/core/infer.js +1838 -69
- package/dist/core/jvm/Harness.java +361 -0
- package/dist/core/jvm/JavaReader.java +825 -0
- package/dist/core/jvm/Jvm.java +332 -0
- package/dist/core/jvm/KtReader.java +960 -0
- package/dist/core/jvm/Rt.java +10 -0
- package/dist/core/laws.d.ts +83 -0
- package/dist/core/laws.js +623 -0
- package/dist/core/machine.d.ts +2 -0
- package/dist/core/machine.js +87 -0
- package/dist/core/py/harness.py +328 -0
- package/dist/core/py/reader.py +447 -0
- package/dist/core/rb/harness.rb +233 -0
- package/dist/core/rb/reader.rb +375 -0
- package/dist/core/relational.d.ts +49 -0
- package/dist/core/relational.js +345 -0
- package/dist/core/rs/Cargo.lock +108 -0
- package/dist/core/rs/Cargo.toml +14 -0
- package/dist/core/rs/src/main.rs +681 -0
- package/dist/core/swift/Harness.swift +124 -0
- package/dist/core/swift/Main.swift +34 -0
- package/dist/core/swift/Package.swift +13 -0
- package/dist/core/swift/Sources/gdp-swiftreader/main.swift +1341 -0
- package/dist/interchange/check.d.ts +16 -0
- package/dist/interchange/check.js +111 -0
- package/dist/interchange/format.d.ts +150 -0
- package/dist/interchange/format.js +678 -0
- package/dist/kernel/epr.d.ts +40 -0
- package/dist/kernel/epr.js +1882 -0
- package/dist/kernel/kernel.d.ts +45 -1
- package/dist/kernel/kernel.js +138 -13
- package/dist/kernel/prove.d.ts +45 -0
- package/dist/kernel/prove.js +658 -51
- package/dist/lean-kernel/emit.d.ts +17 -2
- package/dist/lean-kernel/emit.js +87 -7
- package/package.json +57 -2
package/dist/core/infer.js
CHANGED
|
@@ -32,15 +32,16 @@
|
|
|
32
32
|
// rules it out (and the most of those seen) is added; a chosen candidate that nothing supports, or that makes a proof slow, is dropped; the set proven as a whole
|
|
33
33
|
// is the answer, after each chosen candidate the proof doesn't need is taken out again
|
|
34
34
|
import { quietly } from "./cyclic.js";
|
|
35
|
-
import { all, any, atMost, divisorsIn, evalC, evalE, get, interpret, machineTerms, modName, n, not, plus, same, seeded, times, withArithmetic, x } from "./machine.js";
|
|
35
|
+
import { all, any, atMost, carriedView, compileC, compileE, loadJs, divisorsIn, evalC, evalE, get, interpret, machineTerms, fnValue, modName, n, not, plus, same, seeded, times, walk, withArithmetic, x } from "./machine.js";
|
|
36
36
|
import { bool, proveRule, proveSteps, verdictOf } from "./dsl.js";
|
|
37
37
|
import { allowed, keysOf, linKey, watch } from "./watch.js";
|
|
38
|
-
import { clock, counted, grow, keyValue, lawInstances, proveLaw, proveLaws, proveStepLaws, stepLawInstances, usedHyps, withDeadline, withModels, withTrees } from "../kernel/prove.js";
|
|
38
|
+
import { clock, counted, fullWork, decideLaw, decideNote, machineKey, memoHits, withStrict, fullRate, grow, keyValue, lawInstances, proveLaw, proveLaws, proveSetFast, proveStepLaws, stepLawInstances, usedHyps, withDeadline, withModels, withTrees, withWall, withWork } from "../kernel/prove.js";
|
|
39
39
|
import { assumed, obligations, plainOf, stepLawObligations, substC } from "../kernel/kernel.js";
|
|
40
40
|
import { env, gcd, work } from "./solver.js";
|
|
41
41
|
import { bgcd } from "./q.js";
|
|
42
42
|
import { showC, showE } from "./exists.js";
|
|
43
43
|
import { REFUTED, growCdcl } from "../kernel/cdcl.js";
|
|
44
|
+
import { eprMeter, withEprCap } from "../kernel/epr.js";
|
|
44
45
|
// a value outside a guard's (or a control value's) values
|
|
45
46
|
const off = (g, v) => v < g.value || v > (g.hi ?? g.value);
|
|
46
47
|
const KEY = 'key$found', KEY2 = 'key2$found', AT = '@', K = '#', SOME = 'k$found';
|
|
@@ -51,8 +52,10 @@ const KEY = 'key$found', KEY2 = 'key2$found', AT = '@', K = '#', SOME = 'k$found
|
|
|
51
52
|
// frontier corpus's then keeps 3,000 samples, at least as many as 500 ms of wall time gave on 2 vCPU (`runs/modal/infer/
|
|
52
53
|
// before`'s cal2 jobs: 29 to 11,625 trials, 745 to 3,000 samples). Of 100 to 3,000 trials (`runs/modal/infer/grid`),
|
|
53
54
|
// 100 lost a corpus item (metered blocks) and 200 to 3,000 none: 400 is a step above the least that lost none.
|
|
54
|
-
export const inferRates = { tries: 100, rows: 10_000, evals: 10_000, watched: 100, work: 40 };
|
|
55
|
+
export const inferRates = { tries: 100, rows: 10_000, evals: 10_000, watched: 100, work: 40, etries: 80, epr: 500, erows: 100, efull: 220 };
|
|
55
56
|
export const inferRuns = 16_000;
|
|
57
|
+
// (the share of what is left of the search's budget that the engine's clock may spend: GDP_ENGINE_SHARE, 0.95: ring needs 52.2 of its 55)
|
|
58
|
+
const engineShare = Number(env.GDP_ENGINE_SHARE ?? 0.95);
|
|
56
59
|
// GDP_INFER_TRACE=1: each phase's wall time beside what it counted, to stderr (the calibration's numbers).
|
|
57
60
|
const inferTrace = !!env.GDP_INFER_TRACE;
|
|
58
61
|
// The join (below, after the candidates): GDP_JOIN=0 turns it off, for comparing. Its share of what is left of the
|
|
@@ -62,7 +65,7 @@ const inferTrace = !!env.GDP_INFER_TRACE;
|
|
|
62
65
|
// the control state up to 7, and 128 of its values; a goal tried with 6 parts of the set before all of it, 2 forms added
|
|
63
66
|
// at a time.
|
|
64
67
|
const joinOn = env.GDP_JOIN !== '0';
|
|
65
|
-
const joinShare = 0.6, joinQueryMs = 8000, joinRequestMs = 150, joinWalk = 8, joinPairs = 10, joinRaises = 2, joinSparse = 4, joinCoef = 1000;
|
|
68
|
+
const joinShare = 0.6, joinQueryMs = 8000, joinGrowEpr = 300_000, joinGrowWork = 400_000, joinRequestMs = 150, joinWalk = 8, joinPairs = 10, joinRaises = 2, joinSparse = 4, joinCoef = 1000;
|
|
66
69
|
const joinExtras = 24, joinTries = 40, joinBits = 7, joinKeys = 128, joinAttempts = 6, joinAdd = 2;
|
|
67
70
|
// GDP_JOIN_CORE=0: the join's goals by the prover's trees alone, as before (else a model, or a refutation's core, of the
|
|
68
71
|
// CDCL search first, at a quarter of the goal's budget).
|
|
@@ -222,23 +225,53 @@ function keySorts(m, laws, pairing) {
|
|
|
222
225
|
const a = hi && name(hi[0]), b = lo && name(lo[0]);
|
|
223
226
|
return a && b ? [a, b] : undefined;
|
|
224
227
|
};
|
|
228
|
+
// (a key of three or more parts, N²·a + N·b + c: its parts, each a place of its own, `${k}:${i}`)
|
|
229
|
+
const arity = new Map();
|
|
230
|
+
const parts = (key, name) => {
|
|
231
|
+
if (!pairing?.n)
|
|
232
|
+
return undefined;
|
|
233
|
+
const lin = new Map();
|
|
234
|
+
if (!linKey(key, () => true, 1, lin) || (lin.get('1') ?? 0) !== pairing.c)
|
|
235
|
+
return undefined;
|
|
236
|
+
const vs = [...lin].filter(([v, c]) => v !== '1' && c).sort((a, b) => b[1] - a[1]), ns = vs.map(([v]) => name(v));
|
|
237
|
+
return vs.length >= 3 && vs.every(([, c], i) => c === pairing.n ** (vs.length - 1 - i)) && ns.every(Boolean) ? ns : undefined;
|
|
238
|
+
};
|
|
225
239
|
const kids = (t, f) => { for (const v of Object.values(t))
|
|
226
240
|
if (v && typeof v === 'object')
|
|
227
241
|
(Array.isArray(v) ? v : [v]).forEach(y => { if (y && typeof y === 'object' && 'op' in y)
|
|
228
242
|
f(y); }); };
|
|
229
243
|
const walk = (t, name) => {
|
|
230
244
|
if (t.op === 'get') {
|
|
231
|
-
|
|
245
|
+
// (a carried map's key has its cells' sort, as a map's: the quorum a request brings, read where the stored one is)
|
|
246
|
+
const carried = (m.inputMaps ?? []).includes(t.map);
|
|
247
|
+
// (a function of a key: its argument's sort, `F:f`)
|
|
248
|
+
if (m.fns?.[t.map] && t.key.op === 'var' && name(t.key.name)) {
|
|
249
|
+
union(name(t.key.name), `F:${t.map}`);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
if (!carried && !(t.map in (m.maps ?? {}))) {
|
|
232
253
|
walk(t.key, name);
|
|
233
254
|
return;
|
|
234
255
|
}
|
|
235
|
-
|
|
256
|
+
if (carried) {
|
|
257
|
+
if (t.key.op === 'var' && name(t.key.name))
|
|
258
|
+
union(name(t.key.name), `M:${t.map}`);
|
|
259
|
+
else
|
|
260
|
+
walk(t.key, name);
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
const sp = split(t.key, name), ps = parts(t.key, name);
|
|
236
264
|
if (sp) {
|
|
237
265
|
composed.add(t.map);
|
|
238
266
|
union(sp[0], 'hi');
|
|
239
267
|
union(sp[1], 'lo');
|
|
240
268
|
return;
|
|
241
269
|
}
|
|
270
|
+
if (ps) {
|
|
271
|
+
arity.set(t.map, ps.length);
|
|
272
|
+
ps.forEach((q, i) => union(q, `${t.map}:${i}`));
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
242
275
|
if (t.key.op === 'var' && name(t.key.name)) {
|
|
243
276
|
plainRead.add(t.map);
|
|
244
277
|
union(name(t.key.name), `M:${t.map}`);
|
|
@@ -258,6 +291,14 @@ function keySorts(m, laws, pairing) {
|
|
|
258
291
|
union(a, b);
|
|
259
292
|
return;
|
|
260
293
|
}
|
|
294
|
+
// (a write at every key of k parts at one: z = N²·n + N·k + v)
|
|
295
|
+
if (t.op === '=' && t.a.op === 'var' && name(t.a.name)?.startsWith('M:')) {
|
|
296
|
+
const mp = name(t.a.name).slice(2), ps = parts(t.b, name);
|
|
297
|
+
if (ps) {
|
|
298
|
+
arity.set(mp, ps.length);
|
|
299
|
+
ps.forEach((q, i) => union(q, `${mp}:${i}`));
|
|
300
|
+
}
|
|
301
|
+
}
|
|
261
302
|
kids(t, y => walk(y, name));
|
|
262
303
|
};
|
|
263
304
|
const inputs = new Set(m.inputs), rule = (v) => (inputs.has(v) ? v : undefined);
|
|
@@ -278,6 +319,12 @@ function keySorts(m, laws, pairing) {
|
|
|
278
319
|
const lawName = (l) => { const own = new Set([l.forall, ...(l.keys ?? [])]); return (v) => (own.has(v) ? `L:${l.name}:${v}` : undefined); };
|
|
279
320
|
for (const l of laws)
|
|
280
321
|
walk(l.holds, lawName(l));
|
|
322
|
+
// (a map read at a composed key is no plain read where a write at every key reads it at its own key)
|
|
323
|
+
for (const mp of composed)
|
|
324
|
+
plainRead.delete(mp);
|
|
325
|
+
// (a map of keys of k parts read at a write at every key's own key is no plain read)
|
|
326
|
+
for (const mp of arity.keys())
|
|
327
|
+
plainRead.delete(mp);
|
|
281
328
|
return {
|
|
282
329
|
find,
|
|
283
330
|
sortOf: (l, v) => find(`L:${l.name}:${v}`),
|
|
@@ -292,15 +339,19 @@ function keySorts(m, laws, pairing) {
|
|
|
292
339
|
kids(t, see);
|
|
293
340
|
};
|
|
294
341
|
see(l.holds);
|
|
295
|
-
// (the antecedent's conditions on the keys
|
|
296
|
-
const onKeys = (c) => { const s2 = JSON.stringify(c)
|
|
342
|
+
// (the antecedent's conditions on the keys, with the state and params beside: k < 64, a ≠ b, k < size, j < next)
|
|
343
|
+
const onKeys = (c) => { const s2 = JSON.stringify(c), ns = [...s2.matchAll(/"name":"([^"]+)"/g)].map(q => q[1]); return !s2.includes('"op":"get"') && ns.some(v => own.has(v)) && ns.every(v => own.has(v) || v in m.state || m.params.includes(v)); };
|
|
297
344
|
if (l.holds.op === 'or')
|
|
298
345
|
for (const y of l.holds.xs)
|
|
299
346
|
if (y.op === 'not')
|
|
300
347
|
guards.push(...conj(y.x).filter(onKeys));
|
|
301
348
|
return { composites, guards };
|
|
302
349
|
},
|
|
303
|
-
|
|
350
|
+
// (a guard that reads the state or params beside the keys: a domain that moves)
|
|
351
|
+
moving: (g, bound) => [...JSON.stringify(g).matchAll(/"name":"([^"]+)"/g)].some(q => !bound.includes(q[1])),
|
|
352
|
+
composedMaps: [...composed], plainRead: [...plainRead], arity,
|
|
353
|
+
// (a key of k parts at these key terms)
|
|
354
|
+
composeAll: (ts) => { const k = ts.reduce((acc, t) => plus(times(pairing.n, acc), t)); return pairing.c ? plus(k, n(pairing.c)) : k; },
|
|
304
355
|
compose: (a, b) => { const k = plus(times(pairing.n, a), b); return pairing.c ? plus(k, n(pairing.c)) : k; },
|
|
305
356
|
plainMaps: (sort) => [...plainRead].filter(mp => find(`M:${mp}`) === sort),
|
|
306
357
|
valuesAt: (st, sort) => {
|
|
@@ -315,6 +366,11 @@ function keySorts(m, laws, pairing) {
|
|
|
315
366
|
if (find('lo') === sort)
|
|
316
367
|
ks.add((k - pairing.c) % pairing.n);
|
|
317
368
|
}
|
|
369
|
+
const ar = arity.get(mp);
|
|
370
|
+
if (ar && pairing?.n && k >= pairing.c)
|
|
371
|
+
for (let i = ar - 1, r = k - pairing.c; i >= 0; i--, r = Math.floor(r / pairing.n))
|
|
372
|
+
if (find(`${mp}:${i}`) === sort)
|
|
373
|
+
ks.add(i ? r % pairing.n : r);
|
|
318
374
|
}
|
|
319
375
|
const xs = [...ks].sort((a, b) => a - b);
|
|
320
376
|
return [...xs, xs.length ? xs.at(-1) + 1 : 0];
|
|
@@ -647,11 +703,21 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
647
703
|
opts = { ...opts, progress: (phase, info) => globalThis.process.stderr.write(`[infer ${phase}] ${JSON.stringify(info)}\n`) };
|
|
648
704
|
const t0 = performance.now(), ms = opts.ms ?? 20000, wallUntil = t0 + ms, rule = isRule(target) ? target.withSumsOf(laws, stepLaws) : undefined;
|
|
649
705
|
// The clock the budgets read (counted milliseconds since the start, or wall time where `timed`), and the stop.
|
|
650
|
-
const timed = !!opts.timed, w0 = work.n, used = { tries: 0, rows: 0, evals: 0, watched: 0 };
|
|
706
|
+
const timed = !!opts.timed, w0 = work.n, used = { tries: 0, rows: 0, evals: 0, watched: 0, erows: 0, etries: 0 };
|
|
651
707
|
const now = () => (timed ? performance.now() - t0
|
|
652
708
|
: used.tries / inferRates.tries + used.rows / inferRates.rows + used.evals / inferRates.evals + used.watched / inferRates.watched + (work.n - w0) / inferRates.work);
|
|
653
709
|
let stopped = false;
|
|
654
710
|
const stop = () => stopped || (stopped = performance.now() > wallUntil);
|
|
711
|
+
// The engine's clock (the stage below, from the small instance on): counted as `now` is, with the EPR path's work
|
|
712
|
+
// (kernel/epr.ts's eprMeter) and the views' rows beside it, so that every choice it makes by its budget (a BFS cut, the
|
|
713
|
+
// filter, a goal's cap, a round's growth) is the same on any machine; the wall (`stop`, `until` past `ms`) only ends it.
|
|
714
|
+
// It starts at the engine's start (`eS`), and its budget, `eUntil`, is fixed there from what the join counted: 0.9
|
|
715
|
+
// of what is left of `ms` once the join's count is taken at 0.3 a millisecond (the join's rates count its work
|
|
716
|
+
// 2 to 14 times faster than a Modal container does it), so the engine ends within the wall on a container somewhat
|
|
717
|
+
// slower than Modal's usual; past `eUntil` it stops as it would past the wall, at the same place on any machine.
|
|
718
|
+
let eS = { now: 0, epr: eprMeter.n, erows: 0, etries: 0, full: fullWork() }, eUntil = ms;
|
|
719
|
+
const vnow = () => (timed ? now() : (used.etries - eS.etries) / inferRates.etries + (eprMeter.n - eS.epr) / inferRates.epr + (used.erows - eS.erows) / inferRates.erows + (fullWork() - eS.full) / inferRates.efull);
|
|
720
|
+
const meters = () => ({ real: Math.round(performance.now() - t0), v: Math.round(vnow()), now: Math.round(now()), full: Math.round(fullWork()), epr: eprMeter.n - eS.epr, erows: used.erows, etries: used.etries, tries: used.tries, rows: used.rows, evals: used.evals, work: work.n - w0 });
|
|
655
721
|
const until = ms;
|
|
656
722
|
let tp = performance.now(), cp = 0, wp = w0;
|
|
657
723
|
const trace = (what, o = {}) => {
|
|
@@ -684,12 +750,13 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
684
750
|
}
|
|
685
751
|
return out;
|
|
686
752
|
};
|
|
687
|
-
|
|
753
|
+
// (`fast`: the set's proof asked again after a drop, kernel/prove.ts's proveSetFast: what was settled kept where it stands)
|
|
754
|
+
const prove = (ls, fast = false) => {
|
|
688
755
|
proofs++;
|
|
689
756
|
const { r: searchRule, m: mm } = forLaws(ls);
|
|
690
757
|
if (searchRule)
|
|
691
|
-
return stepLaws.length ? proveSteps(searchRule, ls, stepLaws, { search: false }) : proveRule(searchRule, ls, { search: false });
|
|
692
|
-
const out = stepLaws.length ? proveStepLaws(mm, ls, stepLaws) : proveLaws(mm, ls);
|
|
758
|
+
return stepLaws.length ? proveSteps(searchRule, ls, stepLaws, { search: false }) : proveRule(searchRule, ls, { search: false, fast });
|
|
759
|
+
const out = stepLaws.length ? proveStepLaws(mm, ls, stepLaws) : (fast ? proveSetFast : proveLaws)(mm, ls);
|
|
693
760
|
return verdictOf(out);
|
|
694
761
|
};
|
|
695
762
|
const stats = (samples, candidates, alive) => ({ samples, candidates, alive, proofs, ms: Math.round(performance.now() - t0), stopped: stop() });
|
|
@@ -699,9 +766,9 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
699
766
|
// --- runs ------------------------------------------------------------------------------------------------------------
|
|
700
767
|
const run = interpret(m), rand = seeded(17), kinds = rule ? rule.kindsOfState() : [];
|
|
701
768
|
const given = rule ? [...rule.kindLaws().filter(k => !laws.some(l => l.name === k.name)), ...laws] : laws;
|
|
702
|
-
const randomParams = (bound) => (rule ? rule.randomParams(
|
|
703
|
-
const randomInput = (params, bound) => (rule ? rule.randomInput(
|
|
704
|
-
: { ...Object.fromEntries(m.inputs.map(i => [i,
|
|
769
|
+
const randomParams = (bound, r = rand) => (rule ? rule.randomParams(r, bound) : Object.fromEntries(m.params.map(p => [p, r(bound + 1)])));
|
|
770
|
+
const randomInput = (params, bound, r = rand) => (rule ? rule.randomInput(r, bound, params)
|
|
771
|
+
: { ...Object.fromEntries(m.inputs.map(i => [i, r(bound + 1)])), ...Object.fromEntries((m.inputMaps ?? []).map(i => [i, new Map()])) });
|
|
705
772
|
const samples = [], cap = 3000;
|
|
706
773
|
const keep = (s) => { if (samples.length < cap)
|
|
707
774
|
samples.push(s);
|
|
@@ -714,7 +781,7 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
714
781
|
const mapsText = (st) => JSON.stringify(Object.values(st.maps).map(c => [...c]));
|
|
715
782
|
// Counted: `runs` requests tried at least, and on until the samples are full (a trial keeps its start at least, so as
|
|
716
783
|
// many trials as they hold fill them), the trials at most `runs` or that many. Timed: `runsMs` of wall time.
|
|
717
|
-
const runsTimed = timed || opts.runsMs !== undefined, runsUntil = Math.min(wallUntil, performance.now() + (opts.runsMs ?? 500)), least = opts.runs ?? inferRuns;
|
|
784
|
+
const runsTimed = timed || opts.runsMs !== undefined, runsUntil = Math.min(wallUntil, performance.now() + (opts.runsMs ?? 500)), least = opts.runs ?? Math.min(inferRuns, Math.max(2000, 4 * ms));
|
|
718
785
|
const moreRuns = (trial) => (runsTimed ? performance.now() < runsUntil : trial < Math.max(cap, least) && (used.tries < least || samples.length < cap) && !stop());
|
|
719
786
|
let trials = 0;
|
|
720
787
|
// (an assumption with "there exists", a value the request brings, may admit no request at a small bound, a multiple
|
|
@@ -833,17 +900,16 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
833
900
|
}
|
|
834
901
|
const termAtoms = atoms.filter(a => a.term);
|
|
835
902
|
// a row's term atoms' values, at its state (a term whose value the interpreter can't take: 0)
|
|
903
|
+
const termFns = termAtoms.map(a => compileE(a.term, m));
|
|
836
904
|
const withTerms = (env, st) => {
|
|
837
905
|
if (!termAtoms.length)
|
|
838
906
|
return env;
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
env[a.parts[0][0]] = evalE(a.term, env, st, m);
|
|
842
|
-
}
|
|
843
|
-
catch {
|
|
844
|
-
env[a.parts[0][0]] = 0;
|
|
845
|
-
}
|
|
907
|
+
termAtoms.forEach((a, i) => { try {
|
|
908
|
+
env[a.parts[0][0]] = termFns[i](env, st);
|
|
846
909
|
}
|
|
910
|
+
catch {
|
|
911
|
+
env[a.parts[0][0]] = 0;
|
|
912
|
+
} });
|
|
847
913
|
return env;
|
|
848
914
|
};
|
|
849
915
|
const A = atoms.length, cellAtoms = atoms.flatMap((a, i) => (a.cell ? [i] : []));
|
|
@@ -931,8 +997,12 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
931
997
|
pick(3, mags, terms, scalarOk, false);
|
|
932
998
|
else if (choose(3, P) * 8 <= maxCandidates / 2)
|
|
933
999
|
pick(3, [1], terms, scalarOk, false);
|
|
1000
|
+
// (a map at a composed key and a map at a plain one read at one key are of two sorts: Leader[k] against
|
|
1001
|
+
// VoteRequestMsg[k], a pair relation's raw key, is no law of the rule's)
|
|
1002
|
+
const pairMaps = new Set(pairingOf(m)?.n ? keySorts(m, laws, pairingOf(m)).composedMaps : []), pairOf = (i) => pairMaps.has(atoms[i].parts[0][0]);
|
|
1003
|
+
const sortedCells = (acc) => { const cs = acc.filter(([i]) => atoms[i].cell); return cs.every(([i]) => pairOf(i)) || cs.every(([i]) => !pairOf(i)); };
|
|
934
1004
|
if (cellAtoms.length) {
|
|
935
|
-
const all = atoms.flatMap((_, i) => (modeOf(i)?.choice ? [] : [i])), cellOk = (acc) => acc.some(([i]) => atoms[i].cell) && acc.filter(([i]) => !atoms[i].cell).length <= 1;
|
|
1005
|
+
const all = atoms.flatMap((_, i) => (modeOf(i)?.choice ? [] : [i])), cellOk = (acc) => acc.some(([i]) => atoms[i].cell) && acc.filter(([i]) => !atoms[i].cell).length <= 1 && sortedCells(acc);
|
|
936
1006
|
pick(1, [1], cellAtoms, () => true, true);
|
|
937
1007
|
pick(2, [1], all, cellOk, true);
|
|
938
1008
|
if (choose(3, A) * 8 <= maxCandidates / 4)
|
|
@@ -965,7 +1035,7 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
965
1035
|
forms.push({ coef, d: b, cells, size: sizeOf(coef) + 1 + (cells ? 10 : 0) });
|
|
966
1036
|
if (coef.length > 2)
|
|
967
1037
|
continue;
|
|
968
|
-
const here = guards.filter(mode => !coef.some(([i]) => i === mode.atom) && (cells || !atoms[mode.atom].cell));
|
|
1038
|
+
const here = guards.filter(mode => !coef.some(([i]) => i === mode.atom) && (cells ? !atoms[mode.atom].cell || sortedCells([...coef, [mode.atom, 1]]) : !atoms[mode.atom].cell));
|
|
969
1039
|
const most = here.map(() => new Map()), away = cells ? keyNots.map(() => -Infinity) : [];
|
|
970
1040
|
for (let j = 0; j < N; j += cells ? strideK : strideS) {
|
|
971
1041
|
const value = at(coef, vals, j);
|
|
@@ -1148,6 +1218,28 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
1148
1218
|
const body = f.when ? any(condOf({ coef: f.when.coef.map(([i, c]) => [i, -c]), d: -f.when.d - 1, cells: false, size: 0 }), atMost(lhs, rhs)) : atMost(lhs, rhs);
|
|
1149
1219
|
return !f.guard ? body : 'keyNot' in f.guard ? any(same(x(KEY), x(atoms[f.guard.keyNot].parts[0][0])), body) : any(notAt(f.guard.atom, f.guard.value, f.guard.hi), body);
|
|
1150
1220
|
};
|
|
1221
|
+
// A view's frame as builder code (its keys by their names; comparisons of a name with a name or a constant), and the
|
|
1222
|
+
// state and params it reads.
|
|
1223
|
+
const frameSource = (v) => {
|
|
1224
|
+
const ren = new Map(v.vars.map((k2, i) => [k2, v.names[i]])), reads = [];
|
|
1225
|
+
const name = (t) => (t.op !== 'var' ? undefined : ren.get(t.name) ?? (reads.push(t.name), t.name));
|
|
1226
|
+
const c = (t) => {
|
|
1227
|
+
if (t.op === 'not') {
|
|
1228
|
+
const y = c(t.x);
|
|
1229
|
+
return y && `${y}.not()`;
|
|
1230
|
+
}
|
|
1231
|
+
if (t.op === 'and') {
|
|
1232
|
+
const xs = t.xs.map(c);
|
|
1233
|
+
return xs.length && xs.every(Boolean) ? xs.slice(1).reduce((a, b) => `${a}.and(${b})`, xs[0]) : undefined;
|
|
1234
|
+
}
|
|
1235
|
+
if (t.op !== '<' && t.op !== '<=' && t.op !== '=')
|
|
1236
|
+
return undefined;
|
|
1237
|
+
const a = name(t.a), b = t.b.op === 'const' ? String(t.b.value) : name(t.b);
|
|
1238
|
+
return a && b ? `${a}.${t.op === '<' ? 'lt' : t.op === '<=' ? 'atMost' : 'eq'}(${b})` : undefined;
|
|
1239
|
+
};
|
|
1240
|
+
const src = c(v.frame);
|
|
1241
|
+
return src ? { binder: v.names.length === 1 ? v.names[0] : `(${v.names.join(', ')})`, src, reads } : undefined;
|
|
1242
|
+
};
|
|
1151
1243
|
// The builder call, for a rule whose atoms are its own variables (naturals, or integers alone).
|
|
1152
1244
|
const sourceOf = (f, text) => {
|
|
1153
1245
|
if (!rule || f.when || f.mod || f.also || f.never || f.nevers || f.cube || [...f.coef.map(([i]) => i), ...(f.guard && 'atom' in f.guard ? [f.guard.atom] : [])].some(i => atoms[i].term))
|
|
@@ -1156,18 +1248,24 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
1156
1248
|
const ints = f.coef.filter(([i]) => atoms[i].int).length;
|
|
1157
1249
|
if (ints && ints !== f.coef.length)
|
|
1158
1250
|
return undefined;
|
|
1159
|
-
// (a view's cell: its map at x or at y
|
|
1160
|
-
|
|
1251
|
+
// (a view's cell: its map at x or at y, a view of one or two plain keys whose frame compares names and constants)
|
|
1252
|
+
const fr = f.view !== undefined ? frameSource(views[f.view]) : undefined;
|
|
1253
|
+
if (f.view !== undefined && (!views[f.view].printable || !fr))
|
|
1161
1254
|
return undefined;
|
|
1162
1255
|
const mapOf = (i) => { const a = atoms[i], p = a.parts[0][0]; return a.place !== undefined ? p.slice(2, p.lastIndexOf(AT)) : p; };
|
|
1163
1256
|
const ref = (i) => (atoms[i].place !== undefined ? `${mapOf(i)}.at(${atoms[i].place === 0 ? 'x' : 'y'})` : atoms[i].cell ? `${atoms[i].parts[0][0]}.at(k)` : nameOf(i));
|
|
1164
1257
|
// (a cell of a map of yes/nos, at k or at x or y, reads as its 0/1 term too)
|
|
1165
1258
|
const boolCell = (i) => (atoms[i].cell || atoms[i].place !== undefined) && rule.kindsOfCells().some(([mp, kd]) => mp === mapOf(i) && kd === bool);
|
|
1166
1259
|
const val = (i) => { const mo = modeOf(i); return mo?.choice && !atoms[i].cell ? `${ref(i)}.index` : (mo?.bool && !atoms[i].cell) || boolCell(i) ? `${ref(i)}.n` : ref(i); };
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1260
|
+
// (a yes/no, a cell of yes/nos too, as itself or its negation; a level of any value by its number)
|
|
1261
|
+
const cond = (i, value, hi) => {
|
|
1262
|
+
const mo = modeOf(i), v = val(i);
|
|
1263
|
+
if (hi !== undefined)
|
|
1264
|
+
return hi === Infinity ? `${v}.atLeast(${value})` : `${v}.atLeast(${value}).and(${v}.atMost(${hi}))`;
|
|
1265
|
+
return mo?.choice && !atoms[i].cell ? `${ref(i)}.is('${mo.choice[value]}')` : (mo?.bool && !atoms[i].cell) || boolCell(i) ? (value === 1 ? ref(i) : `${ref(i)}.not()`) : `${v}.eq(${value})`;
|
|
1266
|
+
};
|
|
1267
|
+
const args = `({${[...new Set([...used.map(i => (atoms[i].cell || atoms[i].place !== undefined ? mapOf(i) : nameOf(i))), ...(fr?.reads ?? [])])].join(', ')}})`;
|
|
1268
|
+
const wrap = (body) => (f.cells ? `forall(k => ${body})` : fr ? `forall(${fr.binder} => ${fr.src}.implies(${body}))` : body);
|
|
1171
1269
|
if (f.unreachable)
|
|
1172
1270
|
return `r.law('${text}', ${args} => ${cond(f.unreachable.atom, f.unreachable.value, f.unreachable.hi)}.not())`;
|
|
1173
1271
|
if (f.cong) {
|
|
@@ -1279,6 +1377,48 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
1279
1377
|
}
|
|
1280
1378
|
return out;
|
|
1281
1379
|
};
|
|
1380
|
+
// A kernel witness's function values (a function the kernel knows by its facts alone: the model's own), the steps and
|
|
1381
|
+
// the views reading them where it names them and the interpreter's elsewhere, within f.
|
|
1382
|
+
const withFnsOf = (w, f) => {
|
|
1383
|
+
const fns = m.fns, saved = [], named = { ...Object.fromEntries([...scalars, ...m.params, ...m.inputs].map(v => [v, 0])), ...w };
|
|
1384
|
+
for (const [name, fn] of Object.entries(fns ?? {})) {
|
|
1385
|
+
if (fn.call || fn.recur)
|
|
1386
|
+
continue;
|
|
1387
|
+
const table = {};
|
|
1388
|
+
for (const [c, v] of Object.entries(w))
|
|
1389
|
+
if (c.startsWith(`${name}[`) && c.endsWith(']')) {
|
|
1390
|
+
const key = keyValue(c, named);
|
|
1391
|
+
if (key !== undefined)
|
|
1392
|
+
table[key] = v;
|
|
1393
|
+
}
|
|
1394
|
+
if (Object.keys(table).length) {
|
|
1395
|
+
saved.push([name, fn]);
|
|
1396
|
+
fns[name] = { ...fn, js: `(k) => (${JSON.stringify(table)})[k] ?? (${fn.js})(k)` };
|
|
1397
|
+
}
|
|
1398
|
+
}
|
|
1399
|
+
try {
|
|
1400
|
+
return f();
|
|
1401
|
+
}
|
|
1402
|
+
finally {
|
|
1403
|
+
for (const [name, fn] of saved)
|
|
1404
|
+
fns[name] = fn;
|
|
1405
|
+
}
|
|
1406
|
+
};
|
|
1407
|
+
// A kernel witness's request: its inputs, and each map it carries at the cells the model names (a quorum).
|
|
1408
|
+
const requestOf = (w) => {
|
|
1409
|
+
const out = Object.fromEntries(m.inputs.map(i => [i, w[i] ?? 0])), named = { ...Object.fromEntries([...scalars, ...m.params, ...m.inputs].map(v => [v, 0])), ...w };
|
|
1410
|
+
for (const q of m.inputMaps ?? []) {
|
|
1411
|
+
const cells = new Map();
|
|
1412
|
+
for (const [name, value] of Object.entries(w))
|
|
1413
|
+
if (name.startsWith(`${q}[`) && name.endsWith(']')) {
|
|
1414
|
+
const key = keyValue(name, named);
|
|
1415
|
+
if (key !== undefined)
|
|
1416
|
+
cells.set(key, value);
|
|
1417
|
+
}
|
|
1418
|
+
out[q] = cells;
|
|
1419
|
+
}
|
|
1420
|
+
return out;
|
|
1421
|
+
};
|
|
1282
1422
|
// --- Houdini on the runs' neighbors ----------------------------------------------------------------------------------
|
|
1283
1423
|
let alive = forms.slice().sort((p, q) => p.size - q.size).slice(0, maxCandidates);
|
|
1284
1424
|
const ctis = [];
|
|
@@ -1355,6 +1495,11 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
1355
1495
|
const names = new Set(given.map(l => l.name));
|
|
1356
1496
|
alive = alive.filter(f => !names.has(textOf(f)));
|
|
1357
1497
|
let pool = alive.slice(), chosen = [];
|
|
1498
|
+
// (the enumeration's set, proven where the join found nothing: the answer, the search below not run)
|
|
1499
|
+
let enumProof;
|
|
1500
|
+
// (the ∃ law the engine's set proved with, its ghost's: reported in `found` with the cubes, so the verdict's laws prove
|
|
1501
|
+
// again as they are, and export to Lean)
|
|
1502
|
+
let enumSome = [];
|
|
1358
1503
|
const goalName = (g) => g.replace(/ @[^@]*$/, '');
|
|
1359
1504
|
let last = first;
|
|
1360
1505
|
const slow = Math.max(3000, ms / 10);
|
|
@@ -1520,8 +1665,23 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
1520
1665
|
// A view's atoms (each map at each place it is read at there) and rows: each assignment of its keys to keys a state
|
|
1521
1666
|
// holds (by sort: the keys of the maps read at the key's place, the parts of composed keys there, and a fresh one;
|
|
1522
1667
|
// per key at most `per`), the frame met, each map's cell at each of its places.
|
|
1523
|
-
|
|
1668
|
+
// The join's rows of a space of keys (the cells, a view): rows alike but for their keys once, up to 6,000. The hull
|
|
1669
|
+
// and the levels read only which rows there are, so the cap holds as many of the samples' kinds of rows as it can.
|
|
1670
|
+
const distinctRows = (rowsOf) => {
|
|
1671
|
+
const rows = [], seen = new Set(), fresh = (r) => { let t = ''; for (const q in r)
|
|
1672
|
+
if (q !== K)
|
|
1673
|
+
t += `${r[q]},`; return !seen.has(t) && !!seen.add(t); };
|
|
1674
|
+
for (const s2 of samples) {
|
|
1675
|
+
if (rows.length > 6000)
|
|
1676
|
+
break;
|
|
1677
|
+
rows.push(...rowsOf(s2.params, s2.st).filter(fresh));
|
|
1678
|
+
}
|
|
1679
|
+
return rows;
|
|
1680
|
+
};
|
|
1681
|
+
const addView = (v, mapsAt, valuesOf2, per, extra = [], product = false) => {
|
|
1524
1682
|
const vi = views.length, va = [];
|
|
1683
|
+
// (`extra`: values of its keys that are atoms too, a key or a function of one: e ≤ Ep[a], idn(x) < idn(y))
|
|
1684
|
+
extra.forEach((t, j) => { va.push(atoms.length); atoms.push({ name: t.name, parts: [[`$x${vi}:${j}`, 1]], param: false, int: false, cell: false, term: t.term, view: vi }); });
|
|
1525
1685
|
mapsAt.forEach((mps, place) => {
|
|
1526
1686
|
for (const i of cellAtoms) {
|
|
1527
1687
|
const a = atoms[i];
|
|
@@ -1531,39 +1691,54 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
1531
1691
|
atoms.push({ name: `${a.parts[0][0]}[${viewText(v, showE(v.places[place]))}]`, parts: a.parts.map(([q, sg]) => [`${AT}${AT}${q}${AT}${vi}:${place}`, sg]), param: false, int: a.int, cell: false, place, view: vi });
|
|
1532
1692
|
}
|
|
1533
1693
|
});
|
|
1534
|
-
const
|
|
1535
|
-
|
|
1694
|
+
const compiled = { frame: compileC(v.frame, m), places: v.places.map(p => compileE(p, m)), extra: extra.map(t => compileE(t.term, m)),
|
|
1695
|
+
xs: extra.map((_, j) => `$x${vi}:${j}`), qs: mapsAt.map((mps, place) => mps.map(mp => `${AT}${AT}${mp}${AT}${vi}:${place}`)) };
|
|
1696
|
+
const rowsOf = (params, st, full = false) => {
|
|
1697
|
+
// (`per` values a key; the engine's views (`product`) at most per^keys rows a state, 64 at least, the fewer-valued
|
|
1698
|
+
// sorts' keys taking all of theirs and the cut on the most-valued: a sort's values past a few are still read where
|
|
1699
|
+
// another sort has few; a model's state, every row up to 1024, so that its diagram is read at the keys it names)
|
|
1700
|
+
const all2 = valuesOf2(st).map(vs => (named.has(st) ? vs.slice(0, -1) : vs)), lens = all2.map(vs => (product ? vs.length : Math.min(per, vs.length))), most = !product || full ? Infinity : named.has(st) ? 1024 : Math.max(64, per ** all2.length);
|
|
1701
|
+
for (let size = lens.reduce((a, b) => a * b, 1); size > most;) {
|
|
1702
|
+
const i = lens.indexOf(Math.max(...lens));
|
|
1703
|
+
if (lens[i] <= 1)
|
|
1704
|
+
break;
|
|
1705
|
+
size = size / lens[i] * (lens[i] - 1);
|
|
1706
|
+
lens[i]--;
|
|
1707
|
+
}
|
|
1708
|
+
const cand = all2.map((vs, i) => vs.slice(0, lens[i])), out = [];
|
|
1709
|
+
// (the frame, places and extra terms compiled once a view (`compiled`); one env the keys are set in, as each
|
|
1710
|
+
// assignment's own copy was: a level reads only the keys above it)
|
|
1536
1711
|
const go = (i, env) => {
|
|
1537
1712
|
if (i === v.vars.length) {
|
|
1538
1713
|
try {
|
|
1539
|
-
if (!
|
|
1714
|
+
if (!compiled.frame(env, st))
|
|
1540
1715
|
return;
|
|
1541
1716
|
}
|
|
1542
1717
|
catch {
|
|
1543
1718
|
return;
|
|
1544
1719
|
}
|
|
1545
|
-
const row = { ...params, ...st.scalars
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
row[
|
|
1549
|
-
|
|
1720
|
+
const row = { ...params, ...st.scalars }, cells = [];
|
|
1721
|
+
row[K] = env[v.vars[0]];
|
|
1722
|
+
compiled.extra.forEach((f, j) => { try {
|
|
1723
|
+
row[compiled.xs[j]] = f(env, st);
|
|
1724
|
+
}
|
|
1725
|
+
catch {
|
|
1726
|
+
row[compiled.xs[j]] = 0;
|
|
1550
1727
|
} });
|
|
1728
|
+
mapsAt.forEach((mps, place) => { const key = compiled.places[place](env, st); mps.forEach((mp, h) => { const q = compiled.qs[place][h]; row[q] = cellOf(st, mp, key); cells.push([q, mp, key]); }); });
|
|
1729
|
+
used.erows++;
|
|
1551
1730
|
out.push(mark(withTerms(row, st), st, cells));
|
|
1552
1731
|
return;
|
|
1553
1732
|
}
|
|
1554
|
-
for (const k2 of cand[i])
|
|
1555
|
-
|
|
1733
|
+
for (const k2 of cand[i]) {
|
|
1734
|
+
env[v.vars[i]] = k2;
|
|
1735
|
+
go(i + 1, env);
|
|
1736
|
+
}
|
|
1556
1737
|
};
|
|
1557
1738
|
go(0, { ...params, ...st.scalars });
|
|
1558
1739
|
return out;
|
|
1559
1740
|
};
|
|
1560
|
-
|
|
1561
|
-
for (const s2 of samples) {
|
|
1562
|
-
if (rows.length > 6000)
|
|
1563
|
-
break;
|
|
1564
|
-
rows.push(...rowsOf(s2.params, s2.st));
|
|
1565
|
-
}
|
|
1566
|
-
views.push({ ...v, atoms: va, rows, rowsOf });
|
|
1741
|
+
views.push({ ...v, atoms: va, rows: distinctRows(rowsOf), rowsOf });
|
|
1567
1742
|
};
|
|
1568
1743
|
// The pairs: where the rule composes two keys into one (Link[N·a + b] beside Known[a], Known[b]), the cells at two
|
|
1569
1744
|
// keys x and y and at N·x + y, a row per two keys a state holds (or the parts of a composed key it holds, or a fresh
|
|
@@ -1600,11 +1775,28 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
1600
1775
|
const seen = new Set();
|
|
1601
1776
|
for (const l of callerLaws) {
|
|
1602
1777
|
const bound = l.forall ? [l.forall, ...(l.keys ?? [])] : [];
|
|
1603
|
-
if (bound.length
|
|
1778
|
+
if (!bound.length || bound.length > 4)
|
|
1604
1779
|
continue;
|
|
1605
1780
|
const shape = sorts.shape(l, bound);
|
|
1606
|
-
|
|
1607
|
-
|
|
1781
|
+
if (!shape)
|
|
1782
|
+
continue;
|
|
1783
|
+
const reads = (g) => bound.filter(b2 => JSON.stringify(g).includes(`"name":"${b2}"`)), moving = shape.guards.some((g) => sorts.moving(g, bound));
|
|
1784
|
+
// A key's domain that moves (its guards that read no other key but the state or params: k < size, j < next): a
|
|
1785
|
+
// view of that key alone, the cells of its sort at it where the domain holds (the cells' space reads every key a
|
|
1786
|
+
// state holds, a map written at every key holding its value past the domain too).
|
|
1787
|
+
for (const b2 of bound) {
|
|
1788
|
+
const dom = shape.guards.filter((g) => reads(g).length === 1 && reads(g)[0] === b2 && sorts.moving(g, bound)), mps = sorts.plainMaps(sorts.sortOf(l, b2));
|
|
1789
|
+
if (!dom.length || !mps.length)
|
|
1790
|
+
continue;
|
|
1791
|
+
const frame = all(...dom.map((g) => substC(g, { vars: { [b2]: x(KEY) }, maps: {}, flags: {} }))), sig = JSON.stringify([sorts.sortOf(l, b2), frame]);
|
|
1792
|
+
if (seen.has(sig))
|
|
1793
|
+
continue;
|
|
1794
|
+
seen.add(sig);
|
|
1795
|
+
addView({ vars: [KEY], names: ['x'], places: [x(KEY)], covers: [[0]], frame, need: 1, printable: true }, [mps], st => [sorts.valuesAt(st, sorts.sortOf(l, b2))], 64);
|
|
1796
|
+
}
|
|
1797
|
+
// (two keys the pairs already relate: a row and a column of the composed key, or two plain keys where no domain
|
|
1798
|
+
// of the law's moves)
|
|
1799
|
+
if (bound.length < 2 || (bound.length === 2 && pairing && ((!pairing.n && !moving) || (shape.composites.length === 1 && sorts.sortOf(l, bound[0]) !== sorts.sortOf(l, bound[1])))))
|
|
1608
1800
|
continue;
|
|
1609
1801
|
const vars = bound.map((_, i) => (i ? `key${i + 1}$found` : KEY)), ren = Object.fromEntries(bound.map((b2, i) => [b2, x(vars[i])]));
|
|
1610
1802
|
const places = [], mapsAt = [], covers = [];
|
|
@@ -1618,14 +1810,16 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
1618
1810
|
mapsAt.push(mps);
|
|
1619
1811
|
covers.push([i]);
|
|
1620
1812
|
} });
|
|
1621
|
-
const
|
|
1813
|
+
const distinct = bound.flatMap((b2, i) => bound.slice(i + 1).flatMap((c2, j) => (sorts.sortOf(l, b2) === sorts.sortOf(l, c2) ? [not(same(x(vars[i]), x(vars[i + 1 + j])))] : [])));
|
|
1814
|
+
const frame = all(...shape.guards.map((g) => substC(g, { vars: ren, maps: {}, flags: {} })), ...distinct);
|
|
1815
|
+
const sig = JSON.stringify([bound.map(b2 => sorts.sortOf(l, b2)), covers, mapsAt, frame]);
|
|
1622
1816
|
if (seen.has(sig) || !places.length)
|
|
1623
1817
|
continue;
|
|
1624
1818
|
seen.add(sig);
|
|
1625
|
-
const distinct = bound.flatMap((b2, i) => bound.slice(i + 1).flatMap((c2, j) => (sorts.sortOf(l, b2) === sorts.sortOf(l, c2) ? [not(same(x(vars[i]), x(vars[i + 1 + j])))] : [])));
|
|
1626
|
-
const frame = all(...shape.guards.map((g) => substC(g, { vars: ren, maps: {}, flags: {} })), ...distinct);
|
|
1627
1819
|
const per = Math.max(2, Math.floor(64 ** (1 / bound.length)));
|
|
1628
|
-
|
|
1820
|
+
// (printed where its places are its two keys, x and y)
|
|
1821
|
+
const printable = bound.length === 2 && places.length === 2 && places.every((p2, i) => p2.op === 'var' && p2.name === vars[i]);
|
|
1822
|
+
addView({ vars, names: ['x', 'y', 'z', 'w'].slice(0, bound.length), places, covers, frame, need: bound.length, printable }, mapsAt, st => bound.map(b2 => sorts.valuesAt(st, sorts.sortOf(l, b2))), per);
|
|
1629
1823
|
}
|
|
1630
1824
|
}
|
|
1631
1825
|
// The directions over some values: each value up and down, pairs of them where there are few, the rule's own
|
|
@@ -1724,7 +1918,7 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
1724
1918
|
const vecs = (p) => (cells ? p.cells.map(valuesOf) : view ? (p.views[kind] ?? []).map(valuesOf) : [valuesOf(p.scalar)]);
|
|
1725
1919
|
const add = (p, widen) => { for (const v of vecs(p))
|
|
1726
1920
|
join(keyOf(v), v, widen); };
|
|
1727
|
-
const rows = (cells ?
|
|
1921
|
+
const rows = (cells ? distinctRows(cellRows) : view ? view.rows : rowsS).map(valuesOf);
|
|
1728
1922
|
for (const v of rows)
|
|
1729
1923
|
join(keyOf(v), v, false);
|
|
1730
1924
|
opts.progress?.('join start', { space: kind, control: ctl.map(c => `${nameOf(c.atom)}:${c.cuts.join('/')}`).join(' '), groups: groups.size, dirs: dirs.length, coords: CN });
|
|
@@ -1978,6 +2172,9 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
1978
2172
|
};
|
|
1979
2173
|
// (a step law's goal: its step from a state the set allows)
|
|
1980
2174
|
const stepLawOf = new Map(stepLaws.map(sl => [sl.name, sl]));
|
|
2175
|
+
// (the engine runs after a join that finds nothing, on a rule whose scalars the runs held at few values: below)
|
|
2176
|
+
let follows;
|
|
2177
|
+
const engineFollows = () => (follows ??= !!sorts && callerLaws.some(l => l.keys?.length) && atoms.every((a, i) => a.cell || a.param || !!a.term || a.view !== undefined || i >= valsS.length || new Set(valsS[i]).size <= 4));
|
|
1981
2178
|
const stepWith = (name, form, ctx) => {
|
|
1982
2179
|
const ctxLaws = [...ctx, ...(form ? [form] : [])].map(lawFor), ls = [...given, ...ctxLaws], mm = forLaws(ctxLaws).m, sl = form ? undefined : stepLawOf.get(name);
|
|
1983
2180
|
const idx = form ? ls.length - 1 : ls.findIndex(l => l.name === name);
|
|
@@ -2002,7 +2199,12 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
2002
2199
|
if (!('tree' in quick) && quick.stuck === 'sat' && quick.witness)
|
|
2003
2200
|
return { stuck: 'sat', witness: quick.witness, g: g };
|
|
2004
2201
|
}
|
|
2005
|
-
|
|
2202
|
+
// (where the engine follows, the usual search cut at joinGrowWork of the cost model's units and its EPR decisions at
|
|
2203
|
+
// joinGrowEpr units: the proving joins' searches take under a second (decentralized lock's sums pass, 0.8 s, 217k
|
|
2204
|
+
// units and 1.6k), and an unknown one ran 13 s (wo's every k: Leader[k] <= VoteRequestMsg[k], 1.2M and 647k), the
|
|
2205
|
+
// engine's time; counted, where a 2 s wall used to end it, so the join ends where it ends on any machine)
|
|
2206
|
+
const follow = engineFollows();
|
|
2207
|
+
const out = withEprCap(follow ? joinGrowEpr : Infinity, () => withWork(follow ? fullWork() + joinGrowWork : Infinity, () => budgeted(joinQueryMs, until2 => grow(g, new Map(), { nodes: 0, until: until2 }))));
|
|
2006
2208
|
if (!('tree' in out))
|
|
2007
2209
|
return { stuck: out.stuck, witness: out.stuck === 'sat' ? out.witness : undefined, g: g };
|
|
2008
2210
|
for (const h of usedHyps(g, out.tree))
|
|
@@ -2135,8 +2337,30 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
2135
2337
|
return true;
|
|
2136
2338
|
};
|
|
2137
2339
|
const formOf = (c, view) => ({ coef: [], d: 0, cells: false, cube: c, size: 1, ...(view !== undefined ? { view } : {}) });
|
|
2340
|
+
// (a view's key read only by order literals bounding it from below (Ep[x] + 1 <= y, y nowhere else): a value past the
|
|
2341
|
+
// small instance's always has that literal, so the cube is a stronger one in disguise, and false where that is: no such
|
|
2342
|
+
// cube. One bounded from above alone is 0 there, the cube a true one (started is 0 and y + 1 <= Ep[x]: Ep is 0 before
|
|
2343
|
+
// init))
|
|
2344
|
+
const oneSided = (c, vi) => {
|
|
2345
|
+
const v = views[vi], side = new Map(), held = new Set();
|
|
2346
|
+
for (const l of c)
|
|
2347
|
+
for (const [a, k] of 'at' in l ? [[l.at.atom, 0]] : l.lin.coef) {
|
|
2348
|
+
const at = atoms[a];
|
|
2349
|
+
if (at.place !== undefined)
|
|
2350
|
+
for (const ki of v.covers[at.place] ?? [])
|
|
2351
|
+
held.add(v.vars[ki]);
|
|
2352
|
+
else if (at.term?.op === 'var' && k) {
|
|
2353
|
+
const s0 = side.get(at.term.name), dir = Math.sign(k);
|
|
2354
|
+
side.set(at.term.name, s0 === undefined || s0 === dir ? dir : 2);
|
|
2355
|
+
}
|
|
2356
|
+
else if (at.term)
|
|
2357
|
+
walk(at.term, o => { if (o.op === 'var')
|
|
2358
|
+
held.add(String(o.name)); });
|
|
2359
|
+
}
|
|
2360
|
+
return [...side].some(([nm, d]) => d === -1 && !held.has(nm));
|
|
2361
|
+
};
|
|
2138
2362
|
const witnessStep = (w) => {
|
|
2139
|
-
const pre = stateAt(w), input =
|
|
2363
|
+
const pre = stateAt(w), input = requestOf(w);
|
|
2140
2364
|
try {
|
|
2141
2365
|
return { pre, post: run.step({}, pre, input) };
|
|
2142
2366
|
}
|
|
@@ -2310,6 +2534,598 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
2310
2534
|
}
|
|
2311
2535
|
return [];
|
|
2312
2536
|
};
|
|
2537
|
+
// The template (DistAI's): per sort, as many keys as the most places of that sort one map or a caller's law reads; a
|
|
2538
|
+
// view of each sub-template of one to four keys (each plain map at each key of its sort, each composed map at each
|
|
2539
|
+
// row and column key, a column's keys below N, keys of one sort distinct). Its cubes: each set of at most three
|
|
2540
|
+
// literals (a few-valued cell or scalar at a value, its largest as "at least") no row of the samples or the start is
|
|
2541
|
+
// in, minimal, reading every key, one per renaming of a sort's keys, the fewest literals first.
|
|
2542
|
+
const known = new Map();
|
|
2543
|
+
// (each enumerated view's keys' sorts: minimal implication's renamings)
|
|
2544
|
+
const viewSorts = new Map();
|
|
2545
|
+
// (order literals that can't hold together, p ≤ q + d around a cycle summing below 0: a cube nothing is in)
|
|
2546
|
+
const contradicts = (c) => {
|
|
2547
|
+
const e = c.flatMap(l => ('lin' in l && l.lin.coef.length === 2 && l.lin.coef[0][1] === 1 && l.lin.coef[1][1] === -1 ? [[l.lin.coef[0][0], l.lin.coef[1][0], l.lin.d]] : []));
|
|
2548
|
+
const ns = [...new Set(e.flatMap(([p, q]) => [p, q]))], dist = new Map(ns.map(q => [q, 0]));
|
|
2549
|
+
for (let k = 0; k <= ns.length; k++)
|
|
2550
|
+
for (const [p, q, d] of e)
|
|
2551
|
+
if (dist.get(q) + d < dist.get(p)) {
|
|
2552
|
+
if (k === ns.length)
|
|
2553
|
+
return true;
|
|
2554
|
+
dist.set(p, dist.get(q) + d);
|
|
2555
|
+
}
|
|
2556
|
+
return false;
|
|
2557
|
+
};
|
|
2558
|
+
const smallReq = [];
|
|
2559
|
+
// (each pool cube's near misses: rows of the filter's larger instances where every literal but one holds)
|
|
2560
|
+
const nearMiss = new Map();
|
|
2561
|
+
let smallAll = [];
|
|
2562
|
+
// (a small state run under another table of a function: its params' table, the rule's functions swapped while it is read)
|
|
2563
|
+
const tableOf = new WeakMap();
|
|
2564
|
+
const withTable = (params, f) => {
|
|
2565
|
+
const tb = tableOf.get(params);
|
|
2566
|
+
if (!tb || !m.fns)
|
|
2567
|
+
return f();
|
|
2568
|
+
const saved = Object.entries(tb).map(([k]) => [k, m.fns[k]]);
|
|
2569
|
+
Object.assign(m.fns, tb);
|
|
2570
|
+
try {
|
|
2571
|
+
return f();
|
|
2572
|
+
}
|
|
2573
|
+
finally {
|
|
2574
|
+
for (const [k, fn] of saved)
|
|
2575
|
+
m.fns[k] = fn;
|
|
2576
|
+
}
|
|
2577
|
+
};
|
|
2578
|
+
const enumerated = () => {
|
|
2579
|
+
const ks = sorts, comp = ks.composedMaps.length && pairing?.n ? [ks.find('hi'), ks.find('lo')] : undefined;
|
|
2580
|
+
const multi = [...ks.arity].map(([mp, k]) => ({ mp, slots: Array.from({ length: k }, (_, i) => ks.find(`${mp}:${i}`)) }));
|
|
2581
|
+
const ss = [...new Set([...ks.plainRead.map(mp => ks.find(`M:${mp}`)), ...(comp ?? []), ...multi.flatMap(q => q.slots)])];
|
|
2582
|
+
// (a map's values of a key's sort, and the sorts an order compares: e < Ep[a] makes Ep's values epochs, ordered)
|
|
2583
|
+
const vp = new Map(), vf = (a) => { const q = vp.get(a); return q === undefined || q === a ? a : vf(q); };
|
|
2584
|
+
const node = (e) => (e.op === 'var' ? (m.inputs.includes(e.name) ? ks.find(e.name) : m.params.includes(e.name) ? `P:${e.name}` : undefined) : e.op === 'get' && (mapSet.has(e.map) || m.fns?.[e.map]) ? `V:${e.map}` : undefined);
|
|
2585
|
+
const ord = new Set(), leaves = (e) => (e.op === 'if' ? [...leaves(e.then), ...leaves(e.else)] : [e]);
|
|
2586
|
+
const relate = (a, b, less = false) => { if (a && b) {
|
|
2587
|
+
const ra = vf(a), rb = vf(b);
|
|
2588
|
+
if (ra !== rb)
|
|
2589
|
+
vp.set(ra, rb);
|
|
2590
|
+
if (less)
|
|
2591
|
+
ord.add(vf(b));
|
|
2592
|
+
} };
|
|
2593
|
+
walk([m.step, m.assume], o => { const c = o; if (c.op === '<' || c.op === '<=' || c.op === '=')
|
|
2594
|
+
relate(node(c.a), node(c.b), c.op !== '='); });
|
|
2595
|
+
// (a param that bounds a sort's keys, k < p, is that sort's size in the small instance; a param a key equals, a write
|
|
2596
|
+
// at every key at it (k = first) or a read at it, is an element of that sort, which pins it against renaming)
|
|
2597
|
+
const bounding = new Map(), elementOf = new Map();
|
|
2598
|
+
walk([m.step, m.assume], o => { const c = o; if ((c.op === '<' || c.op === '<=') && c.b.op === 'var' && m.params.includes(c.b.name) && c.a.op === 'var' && m.inputs.includes(c.a.name))
|
|
2599
|
+
bounding.set(c.b.name, new Set([...(bounding.get(c.b.name) ?? []), ks.find(c.a.name)])); });
|
|
2600
|
+
for (const [mp, w] of Object.entries(m.step.maps ?? {})) {
|
|
2601
|
+
const srt = ks.plainRead.includes(mp) ? ks.find(`M:${mp}`) : undefined;
|
|
2602
|
+
if (srt && 'each' in w)
|
|
2603
|
+
walk(w.value, o => { const c = o; if (c.op === '=' && c.a.op === 'var' && c.b.op === 'var') {
|
|
2604
|
+
const q = c.a.name === w.each ? c.b.name : c.b.name === w.each ? c.a.name : undefined;
|
|
2605
|
+
if (q && m.params.includes(q))
|
|
2606
|
+
elementOf.set(q, srt);
|
|
2607
|
+
} });
|
|
2608
|
+
}
|
|
2609
|
+
walk([m.step, m.assume], o => { const e = o; if (e.op === 'get' && e.key.op === 'var' && m.params.includes(e.key.name) && ks.plainRead.includes(e.map))
|
|
2610
|
+
elementOf.set(e.key.name, ks.find(`M:${e.map}`)); });
|
|
2611
|
+
for (const [mp, w] of Object.entries(m.step.maps ?? {}))
|
|
2612
|
+
for (const l of leaves(w.value))
|
|
2613
|
+
relate(`V:${mp}`, node(l));
|
|
2614
|
+
const valueSort = (mp) => ss.find(q => vf(q) === vf(`V:${mp}`)), ordered = (q) => [...ord].some(o => vf(o) === vf(q));
|
|
2615
|
+
// (a function of a key whose values are a composed map's rows: Pending[idn(x)·N + y])
|
|
2616
|
+
const fnHi = Object.keys(m.fns ?? {}).filter(f => comp && ss.includes(ks.find(`F:${f}`)) && vf(`V:${f}`) === vf(comp[0]));
|
|
2617
|
+
const placesOf = (s) => (comp ? comp.filter(c => c === s).length + fnHi.filter(f => ks.find(`F:${f}`) === s).length : 0);
|
|
2618
|
+
const width = ss.map(s => Math.min(3, Math.max(ordered(s) ? 2 : 0, ks.plainMaps(s).length ? 1 : 0, placesOf(s) + (placesOf(s) >= 2 ? 1 : 0), ...multi.map(q => q.slots.filter(c => c === s).length), ...callerLaws.map(l => [l.forall, ...(l.keys ?? [])].filter(b => b && ks.sortOf(l, b) === s).length))));
|
|
2619
|
+
// (a key's own bounds and domain: a caller's law's guards on one key of the sort, k < 64, k < size)
|
|
2620
|
+
const bounds = new Map();
|
|
2621
|
+
for (const l of callerLaws) {
|
|
2622
|
+
const b = [l.forall, ...(l.keys ?? [])].filter((q) => !!q);
|
|
2623
|
+
for (const g of b.length ? ks.shape(l, b).guards : []) {
|
|
2624
|
+
const one = b.filter(q => JSON.stringify(g).includes(`"name":"${q}"`)), s = one.length === 1 ? ks.sortOf(l, one[0]) : undefined;
|
|
2625
|
+
if (s)
|
|
2626
|
+
bounds.set(s, [...(bounds.get(s) ?? []), substC(g, { vars: { [one[0]]: x(KEY) }, maps: {}, flags: {} })]);
|
|
2627
|
+
}
|
|
2628
|
+
}
|
|
2629
|
+
opts.progress?.('join template', { sorts: ss.map((s, i) => `${s}:${width[i]}`).join(' '), multi: multi.map(q => `${q.mp}:${q.slots.join(',')}`).join(' ') });
|
|
2630
|
+
// The small instance (BRAB's, DuoAI's simulation made complete): each key sort its first 2 to 3 elements (its width),
|
|
2631
|
+
// an ordered one 0..3, a function's values over its argument's, params small, each input over its sort's elements
|
|
2632
|
+
// (a carried set over their subsets, any other over the values a few draws give), every reachable state by BFS up
|
|
2633
|
+
// to 20,000 states and a seventh of what is left; its states the views' rows first.
|
|
2634
|
+
// (an instance: the small one (`grow` none, the samples), or `grow` more elements of a sort within `share` ms, its
|
|
2635
|
+
// BFS uncut and its own draws (the pool's data filter))
|
|
2636
|
+
// (`visit`: each new state as the BFS finds it, which ends the BFS by answering false: the filter reads as it goes)
|
|
2637
|
+
const instance = (grow, share, rnd, cap = 2_000_000, visit) => {
|
|
2638
|
+
let halt = false;
|
|
2639
|
+
const base = share === undefined, most = base ? 20_000 : cap, combosMax = base ? 2048 : 16_384, reqs = [];
|
|
2640
|
+
const size = (q) => Math.max(2, Math.min(3, width[ss.indexOf(q)] ?? 2)) + grow(q), keyDom = (q) => Array.from({ length: ordered(q) ? 4 + grow(q) : size(q) }, (_, i) => i);
|
|
2641
|
+
const fnDom = (q) => Object.keys(m.fns ?? {}).filter(f => vf(`V:${f}`) === vf(q) && ss.includes(ks.find(`F:${f}`))).flatMap(f => keyDom(ks.find(`F:${f}`)).map(k => { try {
|
|
2642
|
+
return fnValue(m.fns[f], k);
|
|
2643
|
+
}
|
|
2644
|
+
catch {
|
|
2645
|
+
return 0;
|
|
2646
|
+
} }));
|
|
2647
|
+
const domOf = (v, drawn) => { const q = ks.find(v); return ss.includes(q) ? (fnDom(q).length ? fnDom(q) : keyDom(q)) : [...drawn].sort((a, b) => a - b); };
|
|
2648
|
+
const pvs = new Map();
|
|
2649
|
+
for (let t = 0; t < 200 && pvs.size < (base ? 4 : 8); t++) {
|
|
2650
|
+
// (a param a sort's keys are compared with, n < size, is that sort's size in the instance)
|
|
2651
|
+
const p = randomParams(2, rnd);
|
|
2652
|
+
for (const q of m.params) {
|
|
2653
|
+
const el = elementOf.get(q), srts = ss.filter(r => vf(r) === vf(`P:${q}`));
|
|
2654
|
+
if (el && ss.includes(el))
|
|
2655
|
+
p[q] = keyDom(el)[rnd(keyDom(el).length)];
|
|
2656
|
+
else if (bounding.has(q) && [...bounding.get(q)].some(r => ss.includes(r)))
|
|
2657
|
+
p[q] = Math.min(...[...bounding.get(q)].filter(r => ss.includes(r)).map(r => keyDom(r).length));
|
|
2658
|
+
else if (srts.length) {
|
|
2659
|
+
const d = keyDom(srts[0]);
|
|
2660
|
+
p[q] = d[rnd(d.length)];
|
|
2661
|
+
}
|
|
2662
|
+
}
|
|
2663
|
+
if ((m.pre && !evalC(m.pre, p, { scalars: {}, maps: {} }, m)) || m.params.some(q => ss.includes(ks.find(q)) && !domOf(q, new Set()).includes(p[q])))
|
|
2664
|
+
continue;
|
|
2665
|
+
pvs.set(JSON.stringify(p), p);
|
|
2666
|
+
}
|
|
2667
|
+
const t3 = vnow(), seen = new Set(), small = [];
|
|
2668
|
+
let end2 = vnow() + (share ?? Math.min(4000, (eUntil - vnow()) / 7));
|
|
2669
|
+
let cut = false;
|
|
2670
|
+
// (a state's key: its cells off the map's default, in key order, the least under the renamings of each symmetric
|
|
2671
|
+
// sort's elements: a sort the step neither orders, nor computes on (a function's argument, a param's bound), nor
|
|
2672
|
+
// stores in a cell or a scalar; every map's key read as its parts' sorts, else no renaming)
|
|
2673
|
+
const layout = new Map();
|
|
2674
|
+
for (const mp of Object.keys(m.maps ?? {})) {
|
|
2675
|
+
const ar = ks.arity.get(mp);
|
|
2676
|
+
if (ks.plainRead.includes(mp))
|
|
2677
|
+
layout.set(mp, [ks.find(`M:${mp}`)]);
|
|
2678
|
+
else if (ks.composedMaps.includes(mp) && pairing?.n)
|
|
2679
|
+
layout.set(mp, [ks.find('hi'), ks.find('lo')]);
|
|
2680
|
+
else if (ar && pairing?.n)
|
|
2681
|
+
layout.set(mp, Array.from({ length: ar }, (_, i) => ks.find(`${mp}:${i}`)));
|
|
2682
|
+
}
|
|
2683
|
+
const stored = new Set(m.inputs.filter(i => JSON.stringify(m.step.state ?? {}).includes(`"name":"${i}"`)).map(i => ks.find(i)));
|
|
2684
|
+
const pinned = new Set(elementOf.values()), sym = layout.size === Object.keys(m.maps ?? {}).length ? ss.filter(q => !pinned.has(q) && !ordered(q) && !valueSort2(q) && !stored.has(q) && !Object.keys(m.fns ?? {}).some(f => ks.find(`F:${f}`) === q) && !m.params.some(p2 => vf(`P:${p2}`) === vf(q))) : [];
|
|
2685
|
+
function valueSort2(q) { return Object.keys(m.maps ?? {}).some(mp => valueSort(mp) === q); }
|
|
2686
|
+
const perms = (k) => (k <= 1 ? [[0]] : perms(k - 1).flatMap(p2 => Array.from({ length: k }, (_, i) => [...p2.slice(0, i), k - 1, ...p2.slice(i)])));
|
|
2687
|
+
let rens = [new Map()];
|
|
2688
|
+
for (const q of sym) {
|
|
2689
|
+
const next = rens.flatMap(r2 => perms(size(q)).map(pm => new Map([...r2, [q, pm]])));
|
|
2690
|
+
if (next.length > 36)
|
|
2691
|
+
break;
|
|
2692
|
+
rens = next;
|
|
2693
|
+
}
|
|
2694
|
+
const renKey = (mp, k, ren) => {
|
|
2695
|
+
const lay = layout.get(mp);
|
|
2696
|
+
if (!lay || !ren.size)
|
|
2697
|
+
return k;
|
|
2698
|
+
if (lay.length === 1) {
|
|
2699
|
+
const pm = ren.get(lay[0]);
|
|
2700
|
+
return pm && k < pm.length ? pm[k] : k;
|
|
2701
|
+
}
|
|
2702
|
+
if (k < pairing.c)
|
|
2703
|
+
return k;
|
|
2704
|
+
const digits = [];
|
|
2705
|
+
for (let i = 0, r = k - pairing.c; i < lay.length; i++, r = Math.floor(r / pairing.n))
|
|
2706
|
+
digits.unshift(i === lay.length - 1 ? r : r % pairing.n);
|
|
2707
|
+
return pairing.c + digits.reduce((acc, d, i) => { const pm = ren.get(lay[i]); return acc * pairing.n + (pm && d < pm.length ? pm[d] : d); }, 0);
|
|
2708
|
+
};
|
|
2709
|
+
const textAt = (q, ren) => JSON.stringify([q.params, q.st.scalars, Object.entries(q.st.maps).map(([mp, cells]) => { const d = q.st.defaults?.[mp] ?? m.maps?.[mp] ?? 0; return [...cells].filter(([, v]) => v !== d).map(([k, v]) => [renKey(mp, k, ren), v]).sort((a, b) => a[0] - b[0]); }), q.st.defaults ?? null]);
|
|
2710
|
+
// (the state's own text first, its renamings' only for a text not seen)
|
|
2711
|
+
const canon = new Map();
|
|
2712
|
+
const keyOf3 = (q) => {
|
|
2713
|
+
const own = textAt(q, new Map());
|
|
2714
|
+
let best = canon.get(own);
|
|
2715
|
+
if (best === undefined) {
|
|
2716
|
+
best = own;
|
|
2717
|
+
for (const ren of rens) {
|
|
2718
|
+
const t = textAt(q, ren);
|
|
2719
|
+
if (t < best)
|
|
2720
|
+
best = t;
|
|
2721
|
+
}
|
|
2722
|
+
canon.set(own, best);
|
|
2723
|
+
}
|
|
2724
|
+
return best;
|
|
2725
|
+
};
|
|
2726
|
+
// (the step compiled once: core/machine.ts's JavaScript, its assumption refusing a request as `allowed` does)
|
|
2727
|
+
// (a function the kernel knows by its facts alone (no recurrence, no call) on a template sort: its own table and a
|
|
2728
|
+
// few others that keep its facts on the sort's keys (its values reversed, and each turned by one), each run as its
|
|
2729
|
+
// own machine; their states' rows read under their table. One table alone is a fact of it, not of the rule: ring's
|
|
2730
|
+
// ids, every node past another one's below it)
|
|
2731
|
+
const tables = [{ m, run, js: m.history ? undefined : loadJs(m), fns: {} }];
|
|
2732
|
+
for (const [f, fn] of Object.entries(m.fns ?? {})) {
|
|
2733
|
+
const arg = ks.find(`F:${f}`);
|
|
2734
|
+
if (fn.call || fn.recur || (fn.arity ?? 1) !== 1 || !ss.includes(arg) || m.history)
|
|
2735
|
+
continue;
|
|
2736
|
+
const dom = keyDom(arg), vals = dom.map(k => { try {
|
|
2737
|
+
return fnValue(fn, k);
|
|
2738
|
+
}
|
|
2739
|
+
catch {
|
|
2740
|
+
return 0;
|
|
2741
|
+
} }), turn = (vs) => [...vs.slice(1), vs[0]];
|
|
2742
|
+
for (const vs of [[...vals].reverse(), turn(vals), turn([...vals].reverse())]) {
|
|
2743
|
+
const fn2 = { ...fn, js: `(k) => (${JSON.stringify(Object.fromEntries(dom.map((k, i) => [k, vs[i]])))})[k] ?? (${fn.js})(k)` }, m2 = { ...m, fns: { ...m.fns, [f]: fn2 } };
|
|
2744
|
+
const keeps = (m.facts ?? []).every(fc => { const b = [fc.forall, ...(fc.keys ?? [])].filter((q) => !!q); return b.reduce(acc => acc.flatMap(t2 => dom.map(k => [...t2, k])), [[]]).every(t => { try {
|
|
2745
|
+
return evalC(fc.holds, Object.fromEntries(b.map((q, k) => [q, t[k]])), { scalars: {}, maps: {} }, m2);
|
|
2746
|
+
}
|
|
2747
|
+
catch {
|
|
2748
|
+
return false;
|
|
2749
|
+
} }); });
|
|
2750
|
+
if (keeps && tables.length < 4)
|
|
2751
|
+
tables.push({ m: m2, run: interpret(m2), js: loadJs(m2), fns: { [f]: fn2 } });
|
|
2752
|
+
}
|
|
2753
|
+
}
|
|
2754
|
+
const stepIn = (tb, params, st, input) => {
|
|
2755
|
+
try {
|
|
2756
|
+
if (tb.js)
|
|
2757
|
+
return tb.js.step(params, tb.js.fork(st), input);
|
|
2758
|
+
return allowed(tb.m, tb.run, params, st, input) ? tb.run.step(params, st, input) : undefined;
|
|
2759
|
+
}
|
|
2760
|
+
catch {
|
|
2761
|
+
return undefined;
|
|
2762
|
+
}
|
|
2763
|
+
};
|
|
2764
|
+
const stepOf = (params, st, input) => stepIn(tables[0], params, st, input);
|
|
2765
|
+
const others = [];
|
|
2766
|
+
for (const params of pvs.values()) {
|
|
2767
|
+
const drawn = new Map(m.inputs.map(i => [i, new Set()]));
|
|
2768
|
+
for (let t = 0; t < 200; t++) {
|
|
2769
|
+
const inp = randomInput(params, 2, rnd);
|
|
2770
|
+
for (const i of m.inputs)
|
|
2771
|
+
if (typeof inp[i] === 'number')
|
|
2772
|
+
drawn.get(i).add(inp[i]);
|
|
2773
|
+
}
|
|
2774
|
+
// (an input no template sort holds, bounded by a param (n1 < size): every value below it)
|
|
2775
|
+
const boundBy = (i) => [...bounding].find(([, v2]) => v2.has(ks.find(i)))?.[0];
|
|
2776
|
+
const doms = m.inputs.map(i => [i, !ss.includes(ks.find(i)) && boundBy(i) !== undefined && (params[boundBy(i)] ?? 0) <= 8 ? Array.from({ length: params[boundBy(i)] ?? 0 }, (_, k) => k) : domOf(i, drawn.get(i))]);
|
|
2777
|
+
for (const q of m.inputMaps ?? []) {
|
|
2778
|
+
const ks2 = keyDom(ks.find(`M:${q}`));
|
|
2779
|
+
doms.push([q, Array.from({ length: 2 ** ks2.length }, (_, b) => new Map(ks2.filter((_, j) => b >> j & 1).map(k => [k, 1])))]);
|
|
2780
|
+
}
|
|
2781
|
+
let combos = [{}];
|
|
2782
|
+
for (const [i, d] of doms)
|
|
2783
|
+
combos = combos.flatMap(c => d.map(v2 => ({ ...c, [i]: v2 })));
|
|
2784
|
+
if (combos.length > combosMax)
|
|
2785
|
+
combos = Array.from({ length: combosMax }, () => combos[rnd(combos.length)]);
|
|
2786
|
+
reqs.push({ params, combos });
|
|
2787
|
+
if (base)
|
|
2788
|
+
smallReq.push({ params, combos });
|
|
2789
|
+
if (base)
|
|
2790
|
+
opts.progress?.('join small domains', { params: JSON.stringify(params), doms: doms.map(([i, d]) => `${i}:${d.length > 8 ? d.length : d.map(v2 => (typeof v2 === 'number' ? v2 : 'map')).join('/')}`).join(' '), combos: combos.length });
|
|
2791
|
+
let st0;
|
|
2792
|
+
try {
|
|
2793
|
+
st0 = run.init(params);
|
|
2794
|
+
}
|
|
2795
|
+
catch {
|
|
2796
|
+
continue;
|
|
2797
|
+
}
|
|
2798
|
+
const tB = vnow(), queue = [{ params, st: st0 }], qi = { at: 0 }, bfsEnd = vnow() + (end2 - vnow()) * 0.75 / (pvs.size - reqs.length + 1);
|
|
2799
|
+
seen.add(keyOf3(queue[0]));
|
|
2800
|
+
small.push(queue[0]);
|
|
2801
|
+
if (visit && !visit(queue[0]))
|
|
2802
|
+
halt = true;
|
|
2803
|
+
// (a BFS that won't finish (a quarter of its share gone, 1 s at least, and its queue still a quarter of what it
|
|
2804
|
+
// found): cut there, the walks half a second after; an instance it does finish is what it was)
|
|
2805
|
+
const cutAt = base ? tB + Math.max(1000, (bfsEnd - tB) / 4) : Infinity;
|
|
2806
|
+
while (!halt && qi.at < queue.length && small.length < most && vnow() < bfsEnd) {
|
|
2807
|
+
if (vnow() > cutAt && (queue.length - qi.at) * 4 >= small.length) {
|
|
2808
|
+
end2 = Math.min(end2, vnow() + 500);
|
|
2809
|
+
break;
|
|
2810
|
+
}
|
|
2811
|
+
const q0 = queue[qi.at++];
|
|
2812
|
+
for (const input of combos) {
|
|
2813
|
+
used.etries++;
|
|
2814
|
+
const next = stepOf(params, q0.st, input);
|
|
2815
|
+
if (!next)
|
|
2816
|
+
continue;
|
|
2817
|
+
const q1 = { params, st: next }, k1 = keyOf3(q1);
|
|
2818
|
+
if (!seen.has(k1)) {
|
|
2819
|
+
seen.add(k1);
|
|
2820
|
+
small.push(q1);
|
|
2821
|
+
queue.push(q1);
|
|
2822
|
+
if (visit && !visit(q1)) {
|
|
2823
|
+
halt = true;
|
|
2824
|
+
break;
|
|
2825
|
+
}
|
|
2826
|
+
}
|
|
2827
|
+
}
|
|
2828
|
+
}
|
|
2829
|
+
if (qi.at < queue.length)
|
|
2830
|
+
cut = true;
|
|
2831
|
+
queue.length = 0;
|
|
2832
|
+
// (the other tables: a BFS each from the same start, within the same share)
|
|
2833
|
+
for (const tb of tables.slice(1)) {
|
|
2834
|
+
let s0;
|
|
2835
|
+
try {
|
|
2836
|
+
s0 = tb.run.init(params);
|
|
2837
|
+
}
|
|
2838
|
+
catch {
|
|
2839
|
+
continue;
|
|
2840
|
+
}
|
|
2841
|
+
const p2 = { ...params }, q2 = [{ params: p2, st: s0 }], q2i = { at: 0 }, seen2 = new Set([keyOf3(q2[0])]), end3 = Math.min(end2, vnow() + (bfsEnd - tB));
|
|
2842
|
+
tableOf.set(p2, tb.fns);
|
|
2843
|
+
others.push(q2[0]);
|
|
2844
|
+
while (!halt && q2i.at < q2.length && others.length < most && vnow() < end3) {
|
|
2845
|
+
const q0 = q2[q2i.at++];
|
|
2846
|
+
for (const input of combos) {
|
|
2847
|
+
used.etries++;
|
|
2848
|
+
const next = stepIn(tb, p2, q0.st, input);
|
|
2849
|
+
if (!next)
|
|
2850
|
+
continue;
|
|
2851
|
+
const q1 = { params: p2, st: next }, k1 = keyOf3(q1);
|
|
2852
|
+
if (!seen2.has(k1)) {
|
|
2853
|
+
seen2.add(k1);
|
|
2854
|
+
others.push(q1);
|
|
2855
|
+
q2.push(q1);
|
|
2856
|
+
if (visit && !visit(q1)) {
|
|
2857
|
+
halt = true;
|
|
2858
|
+
break;
|
|
2859
|
+
}
|
|
2860
|
+
}
|
|
2861
|
+
}
|
|
2862
|
+
}
|
|
2863
|
+
}
|
|
2864
|
+
}
|
|
2865
|
+
// (then random walks from its last states, deeper than the BFS's levels reach: each step the first of a few
|
|
2866
|
+
// requests that changes the state, to the end of its share)
|
|
2867
|
+
const bfs = small.length;
|
|
2868
|
+
for (let t = 0; !halt && cut && reqs.length && small.length < most && vnow() < end2; t++) {
|
|
2869
|
+
const from = small[Math.max(0, small.length - 1 - rnd(Math.min(small.length, 64)))], req = reqs.find(q => q.params === from.params);
|
|
2870
|
+
if (!req)
|
|
2871
|
+
break;
|
|
2872
|
+
let at = from.st;
|
|
2873
|
+
for (let d = 0; d < 40 && vnow() < end2; d++) {
|
|
2874
|
+
let next;
|
|
2875
|
+
for (let k = 0; k < 16 && !next; k++) {
|
|
2876
|
+
const input = req.combos[rnd(req.combos.length)];
|
|
2877
|
+
used.etries++;
|
|
2878
|
+
const s2 = stepOf(from.params, at, input);
|
|
2879
|
+
if (s2 && keyOf3({ params: from.params, st: s2 }) !== keyOf3({ params: from.params, st: at }))
|
|
2880
|
+
next = s2;
|
|
2881
|
+
}
|
|
2882
|
+
if (!next)
|
|
2883
|
+
break;
|
|
2884
|
+
at = next;
|
|
2885
|
+
const q1 = { params: from.params, st: at }, k1 = keyOf3(q1);
|
|
2886
|
+
if (!seen.has(k1)) {
|
|
2887
|
+
seen.add(k1);
|
|
2888
|
+
small.push(q1);
|
|
2889
|
+
}
|
|
2890
|
+
}
|
|
2891
|
+
}
|
|
2892
|
+
opts.progress?.('join meters', { at: base ? 'small' : 'filter bfs', ...meters() });
|
|
2893
|
+
if (base)
|
|
2894
|
+
opts.progress?.('join small instance', { states: small.length, bfs, params: pvs.size, symmetric: sym.slice(0, rens.length > 1 ? sym.length : 0).join(), ms: Math.round(vnow()), took: Math.round(vnow() - t3) });
|
|
2895
|
+
return { small, others, bfs, cut, params: pvs.size, ms: Math.round(vnow() - t3) };
|
|
2896
|
+
};
|
|
2897
|
+
{
|
|
2898
|
+
const r0 = instance(() => 0, undefined, rand);
|
|
2899
|
+
samples.unshift(...r0.small);
|
|
2900
|
+
smallAll = [...r0.small, ...r0.others];
|
|
2901
|
+
}
|
|
2902
|
+
const out = [];
|
|
2903
|
+
for (const counts of width.reduce((acc, w) => acc.flatMap(c => Array.from({ length: w + 1 }, (_, k) => [...c, k])), [[]])) {
|
|
2904
|
+
const keys = ss.flatMap((s, i) => Array.from({ length: counts[i] }, () => s)), vars = keys.map((_, i) => `key${i + 1}$enum`);
|
|
2905
|
+
if (!keys.length || keys.length > 5 || stop())
|
|
2906
|
+
continue;
|
|
2907
|
+
const places = [], mapsAt = [], covers = [];
|
|
2908
|
+
keys.forEach((s, i) => { if (ks.plainMaps(s).length) {
|
|
2909
|
+
places.push(x(vars[i]));
|
|
2910
|
+
mapsAt.push(ks.plainMaps(s));
|
|
2911
|
+
covers.push([i]);
|
|
2912
|
+
} });
|
|
2913
|
+
if (comp)
|
|
2914
|
+
keys.forEach((a, i) => keys.forEach((b, j) => { if (a === comp[0] && b === comp[1]) {
|
|
2915
|
+
places.push(ks.compose(x(vars[i]), x(vars[j])));
|
|
2916
|
+
mapsAt.push(ks.composedMaps);
|
|
2917
|
+
covers.push(i === j ? [i] : [i, j]);
|
|
2918
|
+
} }));
|
|
2919
|
+
for (const q of multi)
|
|
2920
|
+
for (const t of q.slots.reduce((acc, s) => acc.flatMap(p => keys.flatMap((k2, j) => (k2 === s ? [[...p, j]] : []))), [[]])) {
|
|
2921
|
+
places.push(ks.composeAll(t.map(j => x(vars[j]))));
|
|
2922
|
+
mapsAt.push([q.mp]);
|
|
2923
|
+
covers.push([...new Set(t)]);
|
|
2924
|
+
}
|
|
2925
|
+
for (const f of fnHi)
|
|
2926
|
+
keys.forEach((a, i) => keys.forEach((b, j) => { if (a === ks.find(`F:${f}`) && b === comp[1]) {
|
|
2927
|
+
places.push(ks.compose(get(f, x(vars[i])), x(vars[j])));
|
|
2928
|
+
mapsAt.push(ks.composedMaps);
|
|
2929
|
+
covers.push(i === j ? [i] : [i, j]);
|
|
2930
|
+
} }));
|
|
2931
|
+
const names = ['x', 'y', 'z', 'w', 'u'].slice(0, keys.length), fns = Object.keys(m.fns ?? {}).filter(f => ordered(`V:${f}`) && ss.includes(ks.find(`F:${f}`)));
|
|
2932
|
+
const extra = [...keys.flatMap((q, i) => (ordered(q) ? [{ name: names[i], term: x(vars[i]) }] : [])), ...fns.flatMap(f => keys.flatMap((q, i) => (q === ks.find(`F:${f}`) ? [{ name: `${f}(${names[i]})`, term: get(f, x(vars[i])) }] : [])))];
|
|
2933
|
+
const frame = all(...keys.flatMap((s, i) => [...(comp && s === comp[1] && !bounds.has(s) ? [atMost(x(vars[i]), n(pairing.n - 1))] : []), ...[...new Set((bounds.get(s) ?? []).map(g => JSON.stringify(substC(g, { vars: { [KEY]: x(vars[i]) }, maps: {}, flags: {} }))))].map(t => JSON.parse(t)), ...keys.slice(0, i).flatMap((t, j) => (t === s ? [not(same(x(vars[j]), x(vars[i])))] : []))]));
|
|
2934
|
+
const vi = views.length;
|
|
2935
|
+
addView({ vars, names, places, covers, frame, need: keys.length, printable: false }, mapsAt, st => keys.map(s => ks.valuesAt(st, s)), Math.max(2, Math.floor(64 ** (1 / keys.length))), extra, true);
|
|
2936
|
+
// (the rows: every small state's, distinct, at most 20,000; the view's own rows stop at its first states, the shallow
|
|
2937
|
+
// ones, so a deep state's (a leader's) would be left out)
|
|
2938
|
+
viewSorts.set(vi, keys);
|
|
2939
|
+
const v = views[vi], R = [], seenRows = new Set();
|
|
2940
|
+
for (const q of smallAll.length ? smallAll : []) {
|
|
2941
|
+
if (R.length >= 20_000)
|
|
2942
|
+
break;
|
|
2943
|
+
for (const row of withTable(q.params, () => v.rowsOf(q.params, q.st))) {
|
|
2944
|
+
const r = valuesOf(row), k2 = vecKey(r);
|
|
2945
|
+
if (!seenRows.has(k2)) {
|
|
2946
|
+
seenRows.add(k2);
|
|
2947
|
+
R.push(r);
|
|
2948
|
+
}
|
|
2949
|
+
}
|
|
2950
|
+
}
|
|
2951
|
+
if (!R.length)
|
|
2952
|
+
R.push(...v.rows.slice(0, 3000).map(valuesOf));
|
|
2953
|
+
try {
|
|
2954
|
+
if (!m.params.length)
|
|
2955
|
+
R.push(...v.rowsOf({}, run.init({})).map(valuesOf));
|
|
2956
|
+
}
|
|
2957
|
+
catch { /* (the samples hold the start) */ }
|
|
2958
|
+
// (each literal with the rows it is true at, as bits; `id` its atoms, `name` its text under a renaming of the keys)
|
|
2959
|
+
const W = Math.ceil(R.length / 32), lits = [];
|
|
2960
|
+
const where = (i) => { const pl = atoms[i].place; return pl === undefined ? [] : covers[pl]; };
|
|
2961
|
+
// (an extra atom's key, and its function where it is one)
|
|
2962
|
+
const keyOf2 = (i) => { const t = atoms[i].term; return t?.op === 'var' ? v.vars.indexOf(t.name) : t?.op === 'get' && t.key.op === 'var' ? v.vars.indexOf(t.key.name) : -1; };
|
|
2963
|
+
const fnOf = (i) => { const t = atoms[i].term; return t?.op === 'get' ? t.map : undefined; };
|
|
2964
|
+
const term = (i, ren) => (atoms[i].place !== undefined ? `${atoms[i].parts[0][0].split(AT)[2]}[${where(i).map(k => ren[k]).join()}]` : keyOf2(i) >= 0 ? `${fnOf(i) ?? ''}#${ren[keyOf2(i)]}` : `s${i}`);
|
|
2965
|
+
const reads = (i) => (keyOf2(i) >= 0 ? [keyOf2(i)] : where(i));
|
|
2966
|
+
const add = (lit, id, name, rd, holds, before) => {
|
|
2967
|
+
const bits = new Uint32Array(W);
|
|
2968
|
+
R.forEach((r, k) => { if (holds(r))
|
|
2969
|
+
bits[k >> 5] |= 1 << (k & 31); });
|
|
2970
|
+
lits.push({ lit, id, name, reads: rd, bits, ...(before ? { before } : {}), state: ('at' in lit ? [lit.at.atom] : lit.lin.coef.map(([a]) => a)).some(a => atoms[a].place !== undefined || (!atoms[a].term && !atoms[a].param)) });
|
|
2971
|
+
};
|
|
2972
|
+
const valued = (i) => atoms[i].place !== undefined && !!valueSort(atoms[i].parts[0][0].split(AT)[2]);
|
|
2973
|
+
for (const i of [...v.atoms.filter(j => !atoms[j].int && !atoms[j].term), ...modes.filter(mo => !atoms[mo.atom].cell && !atoms[mo.atom].param).map(mo => mo.atom)]) {
|
|
2974
|
+
const vals = [...new Set(R.map(r => r[i]))].sort((a, b) => a - b);
|
|
2975
|
+
// (a value of a key's sort: 0 alone, an order literal below)
|
|
2976
|
+
for (const value of valued(i) ? vals.filter(q => q === 0) : vals.length <= 3 ? vals : []) {
|
|
2977
|
+
const top = value > 0 && value === vals.at(-1);
|
|
2978
|
+
add(top ? { lin: { coef: [[i, -1]], d: -value } } : { at: { atom: i, value } }, `${i}`, ren => `${term(i, ren)}=${value}`, reads(i), r => (top ? r[i] >= value : r[i] === value));
|
|
2979
|
+
}
|
|
2980
|
+
}
|
|
2981
|
+
// (an ordered sort's keys and values compared: t ≤ u and u < t, of a key and a value or two values)
|
|
2982
|
+
const ts = v.atoms.filter(i => keyOf2(i) >= 0 || valued(i));
|
|
2983
|
+
const sortOf2 = (i) => vf(fnOf(i) ? `V:${fnOf(i)}` : keyOf2(i) >= 0 ? keys[keyOf2(i)] : valueSort(atoms[i].parts[0][0].split(AT)[2]));
|
|
2984
|
+
for (const [a2, i] of ts.entries())
|
|
2985
|
+
for (const j of ts.slice(a2 + 1)) {
|
|
2986
|
+
if (sortOf2(i) !== sortOf2(j))
|
|
2987
|
+
continue;
|
|
2988
|
+
// (two keys of one sort are distinct: one before the other)
|
|
2989
|
+
const keysOnly = !fnOf(i) && !fnOf(j) && atoms[i].place === undefined && atoms[j].place === undefined;
|
|
2990
|
+
for (const [p, q] of [[i, j], [j, i]])
|
|
2991
|
+
for (const d of keysOnly ? [-1] : [0, -1])
|
|
2992
|
+
add({ lin: { coef: [[p, 1], [q, -1]], d } }, `${i}~${j}`, ren => `${term(p, ren)}<=${d}+${term(q, ren)}`, [...reads(p), ...reads(q)], r => r[p] - r[q] <= d, keysOnly ? [keyOf2(p), keyOf2(q)] : undefined);
|
|
2993
|
+
}
|
|
2994
|
+
used.rows += R.length * lits.length;
|
|
2995
|
+
opts.progress?.('join view', { keys: keys.join(), places: places.length, lits: lits.length, rows: R.length, names: lits.map(l => l.name(keys.map((_, i) => i))).join(' ').slice(0, 900) });
|
|
2996
|
+
if (lits.length > 96)
|
|
2997
|
+
continue;
|
|
2998
|
+
const empty = (b) => b.every(w => w === 0), and = (a, b) => a.map((w, k) => w & b[k]);
|
|
2999
|
+
const none = (ls) => empty(ls.slice(1).reduce((b, l) => and(b, lits[l].bits), lits[ls[0]].bits));
|
|
3000
|
+
// (a renaming of a sort's keys moves each literal to the place its keys go to)
|
|
3001
|
+
const perms = keys.reduce((acc, s) => acc.flatMap(p => keys.flatMap((t, j) => (t === s && !p.includes(j) ? [[...p, j]] : []))), [[]]);
|
|
3002
|
+
const canon = (ls) => perms.map(ren => ls.map(l => lits[l].name(ren)).sort().join('&')).sort()[0];
|
|
3003
|
+
const seen = new Set(), found = [];
|
|
3004
|
+
const dfs = (from, chosen, size, bits, most = 400) => {
|
|
3005
|
+
for (let j = from; j < lits.length && found.length < most; j++) {
|
|
3006
|
+
if (chosen.some(c => lits[c].id === lits[j].id))
|
|
3007
|
+
continue;
|
|
3008
|
+
const now2 = [...chosen, j], b = bits ? and(bits, lits[j].bits) : lits[j].bits;
|
|
3009
|
+
if (now2.length < size) {
|
|
3010
|
+
if (!empty(b))
|
|
3011
|
+
dfs(j + 1, now2, size, b, most);
|
|
3012
|
+
continue;
|
|
3013
|
+
}
|
|
3014
|
+
if (!empty(b) || now2.some((_, k) => now2.length > 1 && none(now2.filter((__, q) => q !== k))))
|
|
3015
|
+
continue;
|
|
3016
|
+
// (a cube reading no state (keys and functions alone) is a fact of the functions' tables, or false: none)
|
|
3017
|
+
if (contradicts(now2.map(l => lits[l].lit)) || !now2.some(l => lits[l].state) || oneSided(now2.map(l => lits[l].lit), vi))
|
|
3018
|
+
continue;
|
|
3019
|
+
const read = new Set(now2.flatMap(l => lits[l].reads)), k2 = canon(now2);
|
|
3020
|
+
if (read.size === keys.length && !seen.has(k2)) {
|
|
3021
|
+
seen.add(k2);
|
|
3022
|
+
found.push(now2);
|
|
3023
|
+
}
|
|
3024
|
+
}
|
|
3025
|
+
};
|
|
3026
|
+
// (four literals where a view's literals are few: a ring's order of three keys with what holds there)
|
|
3027
|
+
for (let size = 1; size <= (lits.length <= 40 ? 4 : 3); size++)
|
|
3028
|
+
dfs(0, [], size);
|
|
3029
|
+
// (where they are more: the four-literal cubes with a chain of three keys of one sort, a < b < c (betweenness, and a
|
|
3030
|
+
// cycle's turns of it), the two order literals and any two others, 200 at most; and the doubled patterns below)
|
|
3031
|
+
if (lits.length > 40) {
|
|
3032
|
+
const at = found.length;
|
|
3033
|
+
lits.forEach((a, i) => lits.forEach((b, j) => { if (a.before && b.before && a.before[1] === b.before[0] && a.before[0] !== b.before[1] && found.length < at + 200) {
|
|
3034
|
+
const ab = and(a.bits, b.bits);
|
|
3035
|
+
if (!empty(ab))
|
|
3036
|
+
dfs(0, [i, j], 4, ab, at + 200);
|
|
3037
|
+
} }));
|
|
3038
|
+
// (and "two of a pattern are one", DuoAI's: a two-literal pattern some row has, beside its image under a renaming of
|
|
3039
|
+
// the view's keys within their sorts (Transfer[z, x] and Ep[x] < z, twice: two live transfers), 200 at most)
|
|
3040
|
+
const byName = new Map(lits.map((l, i) => [l.name(keys.map((_, k) => k)), i])), at2 = found.length;
|
|
3041
|
+
for (const ren of perms.filter(r => r.some((k, i) => k !== i)))
|
|
3042
|
+
for (let i = 0; i < lits.length && found.length < at2 + 200; i++)
|
|
3043
|
+
for (let j = i + 1; j < lits.length && found.length < at2 + 200; j++) {
|
|
3044
|
+
const i2 = byName.get(lits[i].name(ren)), j2 = byName.get(lits[j].name(ren));
|
|
3045
|
+
if (i2 === undefined || j2 === undefined || new Set([i, j, i2, j2]).size < 4 || lits[i].id === lits[j].id || empty(and(lits[i].bits, lits[j].bits)))
|
|
3046
|
+
continue;
|
|
3047
|
+
const ls2 = [i, j, i2, j2], b2 = ls2.slice(1).reduce((b, l) => and(b, lits[l].bits), lits[i].bits);
|
|
3048
|
+
if (!empty(b2) || ls2.some((_, k) => none(ls2.filter((__, q) => q !== k))) || contradicts(ls2.map(l => lits[l].lit)) || oneSided(ls2.map(l => lits[l].lit), vi))
|
|
3049
|
+
continue;
|
|
3050
|
+
const read = new Set(ls2.flatMap(l => lits[l].reads)), k2 = canon(ls2);
|
|
3051
|
+
if (read.size === keys.length && !seen.has(k2)) {
|
|
3052
|
+
seen.add(k2);
|
|
3053
|
+
found.push(ls2);
|
|
3054
|
+
}
|
|
3055
|
+
}
|
|
3056
|
+
}
|
|
3057
|
+
known.set(vi, { R, lits: lits.map(l => l.lit) });
|
|
3058
|
+
out.push(...found.map(ls => formOf(ls.map(l => lits[l].lit), vi)));
|
|
3059
|
+
}
|
|
3060
|
+
const made0 = out.filter((f, i) => !set.some(g => textOf(g) === textOf(f)) && out.findIndex(g => textOf(g) === textOf(f)) === i);
|
|
3061
|
+
return env.GDP_POOL !== '0' ? dataFilter(made0) : made0;
|
|
3062
|
+
// The pool's data filter (DuoAI's junk killed by data before any solver call; research/protocols/engine/DESIGN.md,
|
|
3063
|
+
// gen/pool): each cube asked at every reachable state of larger instances, one more element of every sort and then
|
|
3064
|
+
// of each sort alone, within a budget of its own (3 s, at most a tenth of what is left: half an instance's share its
|
|
3065
|
+
// BFS, uncut, the rest reading its states); a cube some row of such a state is in is no law, and goes
|
|
3066
|
+
function dataFilter(pool) {
|
|
3067
|
+
// (in states, not time, so that a slow machine reads what a fast one does: each instance's states read as its BFS
|
|
3068
|
+
// finds them, until they stop killing, `quiet` (150) states in a row that kill no cube (ring's kills come up to its
|
|
3069
|
+
// 529th state, wo's to its 271st, distributed's to its 152nd), `most5` (1,000) at most;
|
|
3070
|
+
// its BFS ends there, a share of the engine's clock (30 s, a third of what is left) its outer bound. A slow
|
|
3071
|
+
// container that read fewer states left ring's pool at 47–61 cubes, not 44, and its first round past the wall:
|
|
3072
|
+
// research/protocols/engine/DESIGN.md, gen/laneA)
|
|
3073
|
+
const t5 = vnow(), share = Math.min(Number(env.GDP_POOL_MS ?? 30_000), (eUntil - t5) / 3);
|
|
3074
|
+
const quiet = Number(env.GDP_POOL_QUIET ?? 150), least5 = Number(env.GDP_POOL_LEAST ?? 0), most5 = Number(env.GDP_POOL_STATES ?? 1_000);
|
|
3075
|
+
const litAt = (l, r) => ('at' in l ? valueOf(atoms[l.at.atom], r) === l.at.value : l.lin.coef.reduce((s2, [i, c]) => s2 + c * valueOf(atoms[i], r), 0) <= l.lin.d);
|
|
3076
|
+
const plans = [() => 1, ...(ss.length > 1 ? ss.map(s2 => (q) => (q === s2 ? 1 : 0)) : [])];
|
|
3077
|
+
let live = pool, states = 0;
|
|
3078
|
+
for (const [k, grow] of plans.entries()) {
|
|
3079
|
+
if (!live.length || stop())
|
|
3080
|
+
break;
|
|
3081
|
+
const killed = new Set(), byView = new Map();
|
|
3082
|
+
for (const f of live)
|
|
3083
|
+
byView.set(f.view, [...(byView.get(f.view) ?? []), f]);
|
|
3084
|
+
let read = 0, still = 0, lastKill = 0;
|
|
3085
|
+
const r = instance(grow, share, seeded(29 + k), most5, q => {
|
|
3086
|
+
if (read >= most5 || (read >= (k ? 0 : least5) && still >= quiet) || stop())
|
|
3087
|
+
return false;
|
|
3088
|
+
states++;
|
|
3089
|
+
read++;
|
|
3090
|
+
const before = killed.size;
|
|
3091
|
+
for (const [vi, fs] of byView) {
|
|
3092
|
+
const alive = fs.filter(f => !killed.has(f));
|
|
3093
|
+
if (!alive.length)
|
|
3094
|
+
continue;
|
|
3095
|
+
const rows = withTable(q.params, () => views[vi].rowsOf(q.params, q.st, true));
|
|
3096
|
+
for (const f of alive) {
|
|
3097
|
+
let near = 0;
|
|
3098
|
+
for (const row of rows) {
|
|
3099
|
+
let off = 0;
|
|
3100
|
+
for (const l of f.cube)
|
|
3101
|
+
if (!litAt(l, row) && ++off > 1)
|
|
3102
|
+
break;
|
|
3103
|
+
if (off === 0) {
|
|
3104
|
+
killed.add(f);
|
|
3105
|
+
break;
|
|
3106
|
+
}
|
|
3107
|
+
if (off === 1)
|
|
3108
|
+
near++;
|
|
3109
|
+
}
|
|
3110
|
+
if (near)
|
|
3111
|
+
nearMiss.set(f, (nearMiss.get(f) ?? 0) + near);
|
|
3112
|
+
}
|
|
3113
|
+
}
|
|
3114
|
+
if (killed.size > before)
|
|
3115
|
+
lastKill = read;
|
|
3116
|
+
still = killed.size > before ? 0 : still + 1;
|
|
3117
|
+
return read < most5 && (read < (k ? 0 : least5) || still < quiet);
|
|
3118
|
+
});
|
|
3119
|
+
opts.progress?.('join meters', { at: `filter read ${k}`, read, lastKill, bfs: r.small.length + r.others.length, ...meters() });
|
|
3120
|
+
live = live.filter(f => !killed.has(f));
|
|
3121
|
+
// (no instance after one that kills nothing; GDP_POOL_STOP=0 runs them all)
|
|
3122
|
+
if (env.GDP_POOL_STOP !== '0' && k > 0 && !killed.size)
|
|
3123
|
+
break;
|
|
3124
|
+
}
|
|
3125
|
+
opts.progress?.('join pool', { made: pool.length, left: live.length, states, ms: Math.round(vnow() - t5), real: Math.round(performance.now() - t0) });
|
|
3126
|
+
return live;
|
|
3127
|
+
}
|
|
3128
|
+
};
|
|
2313
3129
|
// The control refined on demand (as CEGAR refines its predicates, here read off the rule): a counterexample to a
|
|
2314
3130
|
// caller's law that the set allows and nothing rules out lies among the samples of its control state. A level of a
|
|
2315
3131
|
// value the rule compares with a constant (the first, of the values the laws read first, whose levels keep the control
|
|
@@ -2319,6 +3135,952 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
2319
3135
|
stood.clear();
|
|
2320
3136
|
reached.length = 0;
|
|
2321
3137
|
} return ok; };
|
|
3138
|
+
// The engine, after a join that found nothing, on a rule of keyed maps whose scalars take few values (no counters;
|
|
3139
|
+
// research/protocols/engine/DESIGN.md): the template's cubes (DuoAI's ∀ candidates, research/protocols/enum/
|
|
3140
|
+
// DESIGN.md) read off the small instance, those the caller's and the kinds' laws don't imply; then Houdini, the
|
|
3141
|
+
// caller's laws first, every cube assumed: the CDCL search's refutation (its core settles the goal while the laws it
|
|
3142
|
+
// read stay) or model, else the proof path's. A model the interpreter reads as a real step out of the goal drops its
|
|
3143
|
+
// cube; one out of the caller's law from a state no small state is has its diagrams blocked as cubes, the caller asked
|
|
3144
|
+
// again first (IC3's order); the caller's law unproven otherwise ends it. A round with nothing real drops the
|
|
3145
|
+
// unsettled. The set is then proven, its trees kept. Later rounds leave a sums pass its reserve (core/dsl.ts's:
|
|
3146
|
+
// decentralized lock's) while the caller's law fails from the set.
|
|
3147
|
+
const engine = () => {
|
|
3148
|
+
if (sorts && callerLaws.some(l => l.keys?.length) && now() < until && !stop()) {
|
|
3149
|
+
if (!timed) {
|
|
3150
|
+
eS = { now: now(), epr: eprMeter.n, erows: used.erows, etries: used.etries, full: fullWork() };
|
|
3151
|
+
eUntil = engineShare * (ms - 0.3 * eS.now);
|
|
3152
|
+
}
|
|
3153
|
+
opts.progress?.('join engine', { wall: Math.round(vnow()), eUntil: Math.round(eUntil) });
|
|
3154
|
+
opts.progress?.('join meters', { at: 'engine', ...meters() });
|
|
3155
|
+
// (a sums pass's reserve where one follows: 10 s, or a quarter of what is left where less is)
|
|
3156
|
+
const v0 = views.length, reserve = rule?.withMapSums() ? Math.min(10_000, (eUntil - vnow()) / 4) : 0;
|
|
3157
|
+
const implied = (f) => {
|
|
3158
|
+
const v = views[f.view], sk = { vars: Object.fromEntries(v.vars.map(k => [k, x(`${k}#`)])), maps: {}, flags: {} }, hyps = [substC(v.frame, sk), not(substC(condOf(f), sk))];
|
|
3159
|
+
for (const l of given) {
|
|
3160
|
+
const b = [l.forall, ...(l.keys ?? [])].filter((q) => !!q);
|
|
3161
|
+
if (v.vars.length ** b.length > 64)
|
|
3162
|
+
return false;
|
|
3163
|
+
for (const t of b.reduce(acc => acc.flatMap(t2 => v.vars.map(k => [...t2, k])), [[]]))
|
|
3164
|
+
hyps.push(substC(l.holds, { vars: Object.fromEntries(b.map((q, k) => [q, sk.vars[t[k]]])), maps: {}, flags: {} }));
|
|
3165
|
+
}
|
|
3166
|
+
return 'tree' in withWall(wallUntil, () => budgeted(joinQueryMs / 8, until2 => grow({ name: 'implied', hyps }, new Map(), { nodes: 0, until: until2 })));
|
|
3167
|
+
};
|
|
3168
|
+
// (the cubes that read a map the caller's laws read first)
|
|
3169
|
+
const callerMaps = new Set(laws.flatMap(l => [...JSON.stringify(l.holds).matchAll(/"map":"([^"]+)"/g)].map(q => q[1])));
|
|
3170
|
+
const near = (f) => (f.cube.some(l => atomsIn(formOf([l])).some(i => callerMaps.has(atoms[i].parts[0][0].split(AT)[2] ?? ''))) ? 0 : 1);
|
|
3171
|
+
// (the rounds over the data-filtered pool cut to its minimal cubes)
|
|
3172
|
+
const minPool = env.GDP_POOL !== '0' ? minimal(enumerated()) : { kept: enumerated(), by: new Map() }, made = minPool.kept;
|
|
3173
|
+
const tEngine = vnow();
|
|
3174
|
+
let cubes = [];
|
|
3175
|
+
// (in turn by the maps a cube reads, those reading the caller's first: one of each kind before a second of any; in
|
|
3176
|
+
// a kind, the fewer literals first (DuoAI's minimal clauses), then the cube over more distinct keys (the law its
|
|
3177
|
+
// diagonal case is a case of))
|
|
3178
|
+
const mapsOfCube = (f) => [...new Set(f.cube.flatMap(l => atomsIn(formOf([l])).map(i => atoms[i].parts[0][0].split(AT)[2] ?? '')))].sort().join();
|
|
3179
|
+
const byMaps = new Map();
|
|
3180
|
+
const width2 = (f) => views[f.view].vars.length;
|
|
3181
|
+
for (const f of [...made].sort((a, b) => near(a) - near(b) || a.cube.length - b.cube.length || width2(b) - width2(a))) {
|
|
3182
|
+
const k2 = mapsOfCube(f);
|
|
3183
|
+
byMaps.set(k2, [...(byMaps.get(k2) ?? []), f]);
|
|
3184
|
+
}
|
|
3185
|
+
// (each turn the kinds in order of their own first cube: its fewer literals, then whether it reads the caller's maps)
|
|
3186
|
+
const kinds2 = [...byMaps.values()].sort((a, b) => a[0].cube.length - b[0].cube.length || near(a[0]) - near(b[0])), turns = [];
|
|
3187
|
+
for (let i = 0; turns.length < made.length; i++)
|
|
3188
|
+
for (const fs of kinds2)
|
|
3189
|
+
if (fs[i])
|
|
3190
|
+
turns.push(fs[i]);
|
|
3191
|
+
// (every cube of at most two literals first, DuoAI's minimal clauses, the turns' order kept within each part)
|
|
3192
|
+
turns.sort((a, b) => Math.min(a.cube.length, 3) - Math.min(b.cube.length, 3));
|
|
3193
|
+
// (round 0 the minimal cubes, at most 24, or the whole enumeration where it is 16 or fewer; the rest by relevance to
|
|
3194
|
+
// the counterexamples, IC3's order)
|
|
3195
|
+
const whole = made.length <= 16;
|
|
3196
|
+
for (const f of turns) {
|
|
3197
|
+
if (cubes.length >= 24 || stop())
|
|
3198
|
+
break;
|
|
3199
|
+
if ((whole || f.cube.length <= 2) && !names.has(textOf(f)) && !implied(f))
|
|
3200
|
+
cubes.push(f);
|
|
3201
|
+
}
|
|
3202
|
+
opts.progress?.('join enumerated', { views: views.length - v0, cubes: cubes.length, ms: Math.round(vnow()) });
|
|
3203
|
+
opts.progress?.('join meters', { at: 'enumerated', ...meters() });
|
|
3204
|
+
opts.progress?.('join cubes', { cubes: cubes.map(textOf).join(' | ') });
|
|
3205
|
+
opts.progress?.('join made', { cubes: made.map(textOf).join(' | ') });
|
|
3206
|
+
// (a goal's proof within the search's own end, on the cost model's clock: a counted search inside it keeps the end)
|
|
3207
|
+
// (a goal's cap, on the engine's clock: the kernel's counted deadline and the EPR path's counted cap, so that the
|
|
3208
|
+
// goal ends at the same place on any machine; the search's wall, only its outer end)
|
|
3209
|
+
const within = (end, f) => { const left = Math.max(0, end - vnow()); return withWall(wallUntil, () => withEprCap(left * inferRates.epr, () => counted(fullRate, () => withDeadline(clock() + left, f), true))); };
|
|
3210
|
+
// (a model's values past the rule's constants renamed onto small ones in order, a gap kept up to 64: the step then
|
|
3211
|
+
// runs at every key, and the model's equalities, order, +1 and comparisons with the constants are kept)
|
|
3212
|
+
const top = Math.max(1024, ...[...JSON.stringify([m.step, m.assume ?? null, m.pre ?? null]).matchAll(/"value":(\d+)/g)].map(q => Number(q[1])));
|
|
3213
|
+
const smallOf = (w) => {
|
|
3214
|
+
const big = [...new Set(Object.values(w).filter(v => v > top))].sort((a, b) => a - b);
|
|
3215
|
+
if (!big.length)
|
|
3216
|
+
return w;
|
|
3217
|
+
const to = new Map();
|
|
3218
|
+
big.forEach((v, i) => to.set(v, i ? to.get(big[i - 1]) + Math.min(64, v - big[i - 1]) : top + 1));
|
|
3219
|
+
return Object.fromEntries(Object.entries(w).map(([c, v]) => [c, to.get(v) ?? v]));
|
|
3220
|
+
};
|
|
3221
|
+
const realCti = (w0, f, others) => {
|
|
3222
|
+
const w = smallOf(w0);
|
|
3223
|
+
return withFnsOf(w, () => {
|
|
3224
|
+
const { params, st } = stateOf(w), read = new Set([...others, f].map(g => g?.view));
|
|
3225
|
+
// (a model's cells past the keys the interpreter steps at every key, a million, left out)
|
|
3226
|
+
for (const cells of Object.values(st.maps))
|
|
3227
|
+
for (const k of [...cells.keys()])
|
|
3228
|
+
if (k > 1_000_000)
|
|
3229
|
+
cells.delete(k);
|
|
3230
|
+
const at = (s2) => ({ scalar: withTerms({ ...params, ...s2.scalars }, s2), cells: [], views: views.map((v, i) => (read.has(i) ? v.rowsOf(params, s2) : [])) });
|
|
3231
|
+
let post;
|
|
3232
|
+
if (vnow() > eUntil)
|
|
3233
|
+
return false;
|
|
3234
|
+
try {
|
|
3235
|
+
post = run.step(params, st, requestOf(w));
|
|
3236
|
+
stepNamed(st, post);
|
|
3237
|
+
}
|
|
3238
|
+
catch {
|
|
3239
|
+
return false;
|
|
3240
|
+
}
|
|
3241
|
+
if (vnow() > eUntil)
|
|
3242
|
+
return false;
|
|
3243
|
+
const before = at(st);
|
|
3244
|
+
if (!others.every(g => holdsAt(g, before)))
|
|
3245
|
+
return false;
|
|
3246
|
+
lastPre = { params, st, w };
|
|
3247
|
+
if (!f)
|
|
3248
|
+
return breaks(params, post, st, requestOf(w));
|
|
3249
|
+
return (at(post).views[f.view] ?? []).some(r => judged(f, r) && !holdsRow(f, r));
|
|
3250
|
+
});
|
|
3251
|
+
};
|
|
3252
|
+
// (a cube false at a start: its start goal's model, judged at the interpreter's own start at the model's params (a
|
|
3253
|
+
// start is reachable, so it is a real counterexample); such a cube is not asked, nor kept)
|
|
3254
|
+
const startBad = new Map();
|
|
3255
|
+
// (the law at the model's keys, at the interpreter's start for the model's params, with the model's function values
|
|
3256
|
+
// where they keep the facts at those keys)
|
|
3257
|
+
const badAtStart = (f, w0) => {
|
|
3258
|
+
const w = smallOf(w0), l = lawFor(f);
|
|
3259
|
+
return withFnsOf(w, () => {
|
|
3260
|
+
const { params } = stateOf(w);
|
|
3261
|
+
try {
|
|
3262
|
+
const st = run.init(params), env2 = { ...params, ...st.scalars };
|
|
3263
|
+
for (const k of [l.forall, ...(l.keys ?? [])])
|
|
3264
|
+
if (k)
|
|
3265
|
+
env2[k] = w[k] ?? w[`${k}#`] ?? 0;
|
|
3266
|
+
const vals = [...new Set([l.forall, ...(l.keys ?? [])].filter((k) => !!k).map(k => env2[k]))];
|
|
3267
|
+
for (const fct of m.facts ?? []) {
|
|
3268
|
+
const b = [fct.forall, ...(fct.keys ?? [])].filter((q) => !!q);
|
|
3269
|
+
if (vals.length ** b.length > 4096)
|
|
3270
|
+
return false;
|
|
3271
|
+
for (const t of b.reduce(acc => acc.flatMap(t2 => vals.map(v => [...t2, v])), [[]]))
|
|
3272
|
+
if (!evalC(fct.holds, { ...env2, ...Object.fromEntries(b.map((q, k) => [q, t[k]])) }, st, m))
|
|
3273
|
+
return false;
|
|
3274
|
+
}
|
|
3275
|
+
return !evalC(l.holds, env2, st, m);
|
|
3276
|
+
}
|
|
3277
|
+
catch {
|
|
3278
|
+
return false;
|
|
3279
|
+
}
|
|
3280
|
+
});
|
|
3281
|
+
};
|
|
3282
|
+
const atStart = (f) => {
|
|
3283
|
+
let b = startBad.get(f);
|
|
3284
|
+
if (b === undefined) {
|
|
3285
|
+
const ls2 = [...given, lawFor(f)], g = obligations(forLaws(ls2).m, ls2, {}, given.length)[0];
|
|
3286
|
+
const q = budgeted(joinQueryMs / 32, until2 => growCdcl(g, new Map(), { nodes: 0, until: until2 }, { model: true, wall: wallUntil }));
|
|
3287
|
+
b = !('tree' in q) && q.stuck === 'sat' && !!q.witness && badAtStart(f, q.witness);
|
|
3288
|
+
startBad.set(f, b);
|
|
3289
|
+
}
|
|
3290
|
+
return b;
|
|
3291
|
+
};
|
|
3292
|
+
// (a counterexample's diagram (UPDR's, IC3PO's): in each template view, a row of its state no reachable small state's
|
|
3293
|
+
// row is, as the literals true there, generalized by taking out each literal while no small state's row is inside;
|
|
3294
|
+
// blocked as a law ¬diagram, at most four a counterexample)
|
|
3295
|
+
let lastPre;
|
|
3296
|
+
const reachedRows = new Map();
|
|
3297
|
+
const diagrams = (pre) => {
|
|
3298
|
+
const out2 = [];
|
|
3299
|
+
for (const [vi, k] of known) {
|
|
3300
|
+
const seenR = reachedRows.get(vi) ?? new Set(k.R.map(vecKey));
|
|
3301
|
+
reachedRows.set(vi, seenR);
|
|
3302
|
+
// (a model names some cells: the literals over those alone)
|
|
3303
|
+
for (const row of views[vi].rowsOf(pre.params, pre.st)) {
|
|
3304
|
+
const r = valuesOf(row);
|
|
3305
|
+
if (!unnamed.get(row) && seenR.has(vecKey(r)))
|
|
3306
|
+
continue;
|
|
3307
|
+
let c = k.lits.filter(l => litHolds(l, r) && judged(formOf([l], vi), row));
|
|
3308
|
+
if (!c.length || k.R.some(y => inCube(c, y)))
|
|
3309
|
+
continue;
|
|
3310
|
+
for (const l of [...c]) {
|
|
3311
|
+
const c2 = c.filter(x2 => x2 !== l);
|
|
3312
|
+
if (c2.length && !k.R.some(y => inCube(c2, y)))
|
|
3313
|
+
c = c2;
|
|
3314
|
+
}
|
|
3315
|
+
if (c.length <= 5 && !k.R.some(y => inCube(c, y)) && !oneSided(c, vi))
|
|
3316
|
+
out2.push(formOf(c, vi));
|
|
3317
|
+
if (out2.length >= 4)
|
|
3318
|
+
return out2;
|
|
3319
|
+
}
|
|
3320
|
+
}
|
|
3321
|
+
return out2;
|
|
3322
|
+
};
|
|
3323
|
+
let ghost, ghosts;
|
|
3324
|
+
const ghosted = new WeakMap();
|
|
3325
|
+
const withGhost = (mm) => {
|
|
3326
|
+
if (!ghost)
|
|
3327
|
+
return mm;
|
|
3328
|
+
let g = ghosted.get(mm);
|
|
3329
|
+
if (!g) {
|
|
3330
|
+
g = { ...mm, state: { ...mm.state, [ghost.name]: ghost.start }, step: { ...mm.step, state: { ...mm.step.state, [ghost.name]: ghost.step } } };
|
|
3331
|
+
ghosted.set(mm, g);
|
|
3332
|
+
}
|
|
3333
|
+
return g;
|
|
3334
|
+
};
|
|
3335
|
+
const ghostsOf = () => {
|
|
3336
|
+
const out3 = [];
|
|
3337
|
+
const states = samples.slice(0, 3000), conds = new Map();
|
|
3338
|
+
for (const w of Object.values(m.step.maps ?? {}))
|
|
3339
|
+
walk(w.value, o => { if (o.op === 'if' && conds.size < 16)
|
|
3340
|
+
conds.set(JSON.stringify(o.c), o.c); });
|
|
3341
|
+
for (const [vi, k] of known) {
|
|
3342
|
+
const v = views[vi];
|
|
3343
|
+
if (v.vars.length !== 1 || stop())
|
|
3344
|
+
continue;
|
|
3345
|
+
const keyed = (l) => atomsIn(formOf([l])).some(i => atoms[i].place !== undefined || !!atoms[i].term);
|
|
3346
|
+
const As = [[], ...k.lits.filter(l => !keyed(l)).map(l => [l])], Bs0 = k.lits.filter(keyed);
|
|
3347
|
+
const off = (l) => !('at' in l) || l.at.value !== 0;
|
|
3348
|
+
const Bs = [...Bs0.map(l => [l]), ...Bs0.flatMap((l, i) => Bs0.slice(i + 1).map(l2 => [l, l2]))].filter(b => !contradicts(b) && b.some(off));
|
|
3349
|
+
const rowsAt = states.map(q => v.rowsOf(q.params, q.st).map(valuesOf));
|
|
3350
|
+
for (const A of As)
|
|
3351
|
+
for (const B of Bs) {
|
|
3352
|
+
if (out3.length >= 6)
|
|
3353
|
+
return out3;
|
|
3354
|
+
let some2 = false, notAll = false, ok = true;
|
|
3355
|
+
for (const rows of rowsAt) {
|
|
3356
|
+
if (!rows.length || !inCube(A, rows[0]))
|
|
3357
|
+
continue;
|
|
3358
|
+
some2 = true;
|
|
3359
|
+
if (!rows.some(r => inCube(B, r))) {
|
|
3360
|
+
ok = false;
|
|
3361
|
+
break;
|
|
3362
|
+
}
|
|
3363
|
+
if (!rows.every(r => inCube(B, r)))
|
|
3364
|
+
notAll = true;
|
|
3365
|
+
}
|
|
3366
|
+
if (!ok || !some2 || !notAll)
|
|
3367
|
+
continue;
|
|
3368
|
+
const y = `some$enum${vi}`, sub = { vars: { [v.vars[0]]: x(y) }, maps: {}, flags: {} };
|
|
3369
|
+
const holds = any(...(A.length ? [condOf(formOf(A, vi))] : []), all(substC(v.frame, sub), substC(not(condOf(formOf(B, vi))), sub)));
|
|
3370
|
+
const name = `${A.length ? textOf(formOf(A, vi)) + ' else ' : ''}some ${textOf(formOf(B, vi))}`;
|
|
3371
|
+
// (the witness: along random runs of the small instance, the first ghost the law holds at)
|
|
3372
|
+
witness: for (const c of [all(), ...conds.values()])
|
|
3373
|
+
for (const i of m.inputs) {
|
|
3374
|
+
const step = { op: 'if', c, then: x(i), else: x(y) };
|
|
3375
|
+
let held2 = 0, bad = false;
|
|
3376
|
+
for (const { params, combos } of smallReq)
|
|
3377
|
+
for (let walk2 = 0; walk2 < 40 && !bad; walk2++) {
|
|
3378
|
+
let st, g = 0;
|
|
3379
|
+
try {
|
|
3380
|
+
st = run.init(params);
|
|
3381
|
+
}
|
|
3382
|
+
catch {
|
|
3383
|
+
break;
|
|
3384
|
+
}
|
|
3385
|
+
for (let d = 0; d < 30 && !bad; d++) {
|
|
3386
|
+
const input = combos[rand(combos.length)];
|
|
3387
|
+
try {
|
|
3388
|
+
if (!allowed(m, run, params, st, input))
|
|
3389
|
+
continue;
|
|
3390
|
+
const env2 = { ...params, ...st.scalars, ...Object.fromEntries(m.inputs.map(i2 => [i2, input[i2]])), [y]: g }, seen2 = carriedView(m, st, input);
|
|
3391
|
+
for (const [fl, c2] of Object.entries(m.step.flags ?? {}))
|
|
3392
|
+
env2[`?${fl}`] = evalC(c2, env2, seen2, m) ? 1 : 0;
|
|
3393
|
+
const next = run.step(params, st, input);
|
|
3394
|
+
g = evalE(step, env2, seen2, m);
|
|
3395
|
+
st = next;
|
|
3396
|
+
if (evalC(holds, { ...params, ...st.scalars, [y]: g }, st, m))
|
|
3397
|
+
held2++;
|
|
3398
|
+
else
|
|
3399
|
+
bad = true;
|
|
3400
|
+
}
|
|
3401
|
+
catch {
|
|
3402
|
+
continue;
|
|
3403
|
+
}
|
|
3404
|
+
}
|
|
3405
|
+
}
|
|
3406
|
+
if (bad || held2 < 50)
|
|
3407
|
+
continue;
|
|
3408
|
+
out3.push({ name: y, start: n(0), step, plain: { name, holds }, some: { name, holds, some: [y], witness: { ghost: { [y]: { start: n(0), step } } } } });
|
|
3409
|
+
break witness;
|
|
3410
|
+
}
|
|
3411
|
+
}
|
|
3412
|
+
}
|
|
3413
|
+
return out3;
|
|
3414
|
+
};
|
|
3415
|
+
// (a goal the CDCL search refuted, by the laws its core read: settled while each is still in the set, Houdini's
|
|
3416
|
+
// cores)
|
|
3417
|
+
// (`stands`: the goals proven since the set last lost a law; a set that only grows keeps their proofs)
|
|
3418
|
+
// (a goal's proof had every cube in the set as it stood: a drop takes back the proofs since the dropped cube came in)
|
|
3419
|
+
const settled = new Map(), kept2 = new Set(), stands = new Set(), provenAt = new Map(), addedAt = new Map();
|
|
3420
|
+
const unstand = (gs) => { const r = Math.min(...[...gs].map(g => addedAt.get(g) ?? 0)); for (const [nm, at] of provenAt)
|
|
3421
|
+
if (at >= r) {
|
|
3422
|
+
stands.delete(nm);
|
|
3423
|
+
provenAt.delete(nm);
|
|
3424
|
+
} };
|
|
3425
|
+
let held = false;
|
|
3426
|
+
const pres = [], subs = [], preAt = new WeakMap();
|
|
3427
|
+
let perGoal = 0, houdinied = false;
|
|
3428
|
+
const engineStart = vnow();
|
|
3429
|
+
const ptOf = (q) => { let pt = preAt.get(q); if (!pt) {
|
|
3430
|
+
pt = withFnsOf(q.w, () => pointOf(q.params, q.st));
|
|
3431
|
+
preAt.set(q, pt);
|
|
3432
|
+
} return pt; };
|
|
3433
|
+
// (a kernel model's state, its values past the rule's constants renamed small, its cells past a million left out)
|
|
3434
|
+
const preOf = (wit) => { const w2 = smallOf(wit); return withFnsOf(w2, () => { const { params, st } = stateOf(w2); for (const cells of Object.values(st.maps))
|
|
3435
|
+
for (const k of [...cells.keys()])
|
|
3436
|
+
if (k > 1_000_000)
|
|
3437
|
+
cells.delete(k); return { params, st, w: w2 }; }); };
|
|
3438
|
+
// (the cubes dropped, never asked again: false at a start)
|
|
3439
|
+
const dead = new Set();
|
|
3440
|
+
// (IC3's frames: a cube whose relative induction fails without a counterexample from a start is not dropped but
|
|
3441
|
+
// pending: out of the set the others assume, asked again (pushed) with the set and itself once the set has changed
|
|
3442
|
+
// since its last try, back in the set where that proves; twice at most)
|
|
3443
|
+
const pending = new Set(), tried = new Map(), backs = new Map();
|
|
3444
|
+
const versionOf = () => cubes.map(textOf).sort().join('|');
|
|
3445
|
+
// (what blocks a counterexample's state: the enumeration's minimal cubes false there (the antichain is the
|
|
3446
|
+
// generalization), or that may be (cells the model leaves unnamed), four at most (the frames' pushes prune the wrong
|
|
3447
|
+
// ones; distributed's needed blockers ranked 2nd to 4th): the false first, then those false at
|
|
3448
|
+
// the most of the counterexamples' states so far, then the fewest literals; else its diagrams)
|
|
3449
|
+
const seenCtis = [];
|
|
3450
|
+
// (every cube ranked in the top eight at some counterexample: the pool a fixpoint's Houdini runs over)
|
|
3451
|
+
const pool = new Set();
|
|
3452
|
+
const blockers = (pre) => {
|
|
3453
|
+
const pt = ptOf(pre), pts = (seenCtis.push(pre), seenCtis.slice(-16).map(ptOf)), bs = [];
|
|
3454
|
+
const scored = made.filter(g => !dead.has(textOf(g)) && !pending.has(g)).map(g => ({ g, f: !holdsAt(g, pt) ? 2 : mayFail(g, pt) ? 1 : 0 })).filter(q => q.f).map(({ g, f }) => ({ g, k: f * 1024 + pts.filter(p2 => !holdsAt(g, p2)).length }));
|
|
3455
|
+
const ranked = scored.sort((a, b) => b.k - a.k || a.g.cube.length - b.g.cube.length || width2(b.g) - width2(a.g));
|
|
3456
|
+
for (const { g } of ranked.slice(0, 8))
|
|
3457
|
+
pool.add(g);
|
|
3458
|
+
for (const { g } of ranked) {
|
|
3459
|
+
if (bs.length >= 4)
|
|
3460
|
+
break;
|
|
3461
|
+
if (!bs.some(h => textOf(h) === textOf(g)) && !atStart(g))
|
|
3462
|
+
bs.push(g);
|
|
3463
|
+
}
|
|
3464
|
+
return bs.length ? bs : withFnsOf(pre.w, () => diagrams(pre)).filter(g => !dead.has(textOf(g)) && !atStart(g));
|
|
3465
|
+
};
|
|
3466
|
+
// (a cube that may be false at a counterexample: at a row where it reads cells the model doesn't name, every literal
|
|
3467
|
+
// the model does name in its pattern)
|
|
3468
|
+
const mayFail = (g, pt) => g.view !== undefined && (pt.views[g.view] ?? []).some(r => !judged(g, r) && g.cube.every(l => { const h = formOf([l], g.view); return !judged(h, r) || !holdsRow(h, r); }));
|
|
3469
|
+
const relevant = (room, kept) => {
|
|
3470
|
+
if (room <= 0 || !pres.length && !subs.length)
|
|
3471
|
+
return [];
|
|
3472
|
+
const have = new Set([...kept.map(textOf), ...dead, ...[...pending].map(textOf)]), pts = pres.map(ptOf), sts = subs.slice(-32).map(ptOf);
|
|
3473
|
+
const scored = made.filter(g => !have.has(textOf(g)) && !names.has(textOf(g))).map(g => ({ g, k: pts.filter(pt => !holdsAt(g, pt)).length * 4096 + pts.filter(pt => mayFail(g, pt)).length * 64 + sts.filter(pt => !holdsAt(g, pt)).length })).filter(q => q.k > 0);
|
|
3474
|
+
// (ties: the fewer literals, then the fewer keys, the more particular law)
|
|
3475
|
+
scored.sort((a, b) => b.k - a.k || a.g.cube.length - b.g.cube.length || width2(a.g) - width2(b.g));
|
|
3476
|
+
const out3 = [];
|
|
3477
|
+
for (const { g } of scored) {
|
|
3478
|
+
if (out3.length >= room || stop())
|
|
3479
|
+
break;
|
|
3480
|
+
if (!implied(g) && !atStart(g))
|
|
3481
|
+
out3.push(g);
|
|
3482
|
+
}
|
|
3483
|
+
return out3;
|
|
3484
|
+
};
|
|
3485
|
+
// (minimal implication, DuoAI's: a cube B goes where a kept cube A's law implies B's law: A's keys sent into B's one
|
|
3486
|
+
// to one within each sort, each literal of A entailed by one of B's (the same; the same sum at a bound no larger; a
|
|
3487
|
+
// value at least A's bound); `by`, B's kept impliers)
|
|
3488
|
+
function minimal(pool) {
|
|
3489
|
+
const letters = ['x', 'y', 'z', 'w', 'u'], tok = /\b([xyzwu])\b/g;
|
|
3490
|
+
const litsOf = (f, ren) => f.cube.map(l => {
|
|
3491
|
+
const nm = (i) => atoms[i].name.replace(tok, (_, c) => letters[ren[letters.indexOf(c)]]);
|
|
3492
|
+
return 'at' in l ? { sum: nm(l.at.atom), d: 0, at: l.at.value } : { sum: l.lin.coef.map(([i, c]) => `${c}*${nm(i)}`).sort().join('+'), d: l.lin.d };
|
|
3493
|
+
});
|
|
3494
|
+
const entails = (b, a) => (a.at !== undefined ? b.at !== undefined && b.sum === a.sum && b.at === a.at
|
|
3495
|
+
: b.at !== undefined ? a.sum === `-1*${b.sum}` && -b.at <= a.d : b.sum === a.sum && b.d <= a.d);
|
|
3496
|
+
const sortsOf = (f) => viewSorts.get(f.view) ?? [];
|
|
3497
|
+
const maps2 = (sa, sb) => sa.reduce((acc, s2) => acc.flatMap(r => sb.flatMap((t2, j) => (t2 === s2 && !r.includes(j) ? [[...r, j]] : []))), [[]]);
|
|
3498
|
+
const own = new Map(pool.map(f => [f, litsOf(f, sortsOf(f).map((_, i) => i))]));
|
|
3499
|
+
const implies = (a, b) => a !== b && maps2(sortsOf(a), sortsOf(b)).some(ren => { const la = litsOf(a, ren), lb = own.get(b); return la.every(x2 => lb.some(y2 => entails(y2, x2))); });
|
|
3500
|
+
const order = [...pool].sort((p2, q2) => p2.cube.length - q2.cube.length || sortsOf(p2).length - sortsOf(q2).length);
|
|
3501
|
+
const kept = [], by = new Map();
|
|
3502
|
+
for (const b of order)
|
|
3503
|
+
if (!kept.some(a => implies(a, b)))
|
|
3504
|
+
kept.push(b);
|
|
3505
|
+
for (const b of pool)
|
|
3506
|
+
if (!kept.includes(b))
|
|
3507
|
+
by.set(b, kept.filter(a => implies(a, b)));
|
|
3508
|
+
return { kept: pool.filter(f => kept.includes(f)), by };
|
|
3509
|
+
}
|
|
3510
|
+
// The top-down phase (DuoAI's, research/protocols/engine/DESIGN.md gen/ring2, gen/ring3): the filtered minimal
|
|
3511
|
+
// pool, not given, not implied by the caller's laws, not false at a start; Houdini, each round every cube's goal with
|
|
3512
|
+
// the others assumed (decideLaw, a short cap first, the undecided again at a longer one only where the round would
|
|
3513
|
+
// end with no drop), every failure dropped at once, a cube a dropped one implied back where nothing kept implies it,
|
|
3514
|
+
// and only the goals whose proofs read a dropped cube asked again; then the caller's goals, the cone (their support,
|
|
3515
|
+
// closed under each cube's; a cube with no known support asked with the cone alone, a model's pre-state bringing in
|
|
3516
|
+
// the fixpoint's cubes false there), and prove() of the cone. A fixpoint that doesn't prove leaves the rounds below.
|
|
3517
|
+
// (by default where the pool is past what the rounds' first round takes, 24, and Houdini can afford it, 64: GDP_TD=1
|
|
3518
|
+
// always, GDP_TD=0 never)
|
|
3519
|
+
if ((env.GDP_TD === '1' || (env.GDP_TD !== '0' && made.length > 24 && made.length <= 64)) && !stop())
|
|
3520
|
+
withTrees(() => withStrict(() => {
|
|
3521
|
+
const T0 = vnow(), ph = (o) => opts.progress?.('join td', { ...o, td: Math.round(vnow() - T0), ...meters() });
|
|
3522
|
+
const by = minPool.by, q1 = Number(env.GDP_TD_Q1 ?? joinQueryMs / 8), q2 = Number(env.GDP_TD_Q2 ?? q1);
|
|
3523
|
+
ph({ phase: 'start', made: made.length, before: Math.round(tEngine - t0) });
|
|
3524
|
+
const fresh = (f) => !names.has(textOf(f)) && !dead.has(textOf(f)) && !implied(f) && !atStart(f);
|
|
3525
|
+
let hs = made.filter(fresh);
|
|
3526
|
+
ph({ phase: 'pool', cubes: hs.length });
|
|
3527
|
+
const uses = new Map(), undecided = new Set(), reasked = new Set();
|
|
3528
|
+
let asked = new Set(hs);
|
|
3529
|
+
for (let r = 0; hs.length && vnow() < eUntil && !stop(); r++) {
|
|
3530
|
+
const ls = [...given, ...hs.map(lawFor)], mm = forLaws(ls).m, failed = new Set(), tr = vnow(), at = new Map(hs.map((f, i) => [f, given.length + i]));
|
|
3531
|
+
const ask = (f, ms2) => {
|
|
3532
|
+
if (vnow() > eUntil) {
|
|
3533
|
+
undecided.add(f);
|
|
3534
|
+
return;
|
|
3535
|
+
}
|
|
3536
|
+
const dl = within(Math.min(eUntil, vnow() + ms2), () => decideLaw(mm, ls, at.get(f)));
|
|
3537
|
+
if (!dl) {
|
|
3538
|
+
undecided.add(f);
|
|
3539
|
+
return;
|
|
3540
|
+
}
|
|
3541
|
+
undecided.delete(f);
|
|
3542
|
+
if (dl.proven)
|
|
3543
|
+
uses.set(textOf(f), dl.uses);
|
|
3544
|
+
else {
|
|
3545
|
+
failed.add(f);
|
|
3546
|
+
uses.delete(textOf(f));
|
|
3547
|
+
}
|
|
3548
|
+
};
|
|
3549
|
+
// (a goal left undecided is kept, as optimistic Houdini keeps it, and asked again only once a round has dropped
|
|
3550
|
+
// nothing: a drop's round would ask it again at the same cap for nothing most rounds)
|
|
3551
|
+
let todo = r === 0 ? hs : hs.filter(f => asked.has(f) && !undecided.has(f));
|
|
3552
|
+
// (the goals with the most near misses in the larger instances first, and a failure dropped at
|
|
3553
|
+
// once, the round's later goals asked with the smaller set)
|
|
3554
|
+
if (env.GDP_TD_ORDER !== '0') {
|
|
3555
|
+
todo = [...todo].sort((a2, b2) => (nearMiss.get(b2) ?? 0) - (nearMiss.get(a2) ?? 0));
|
|
3556
|
+
let cur = hs.filter(f => !failed.has(f));
|
|
3557
|
+
for (const f of todo) {
|
|
3558
|
+
if (vnow() > eUntil) {
|
|
3559
|
+
undecided.add(f);
|
|
3560
|
+
continue;
|
|
3561
|
+
}
|
|
3562
|
+
const ls2 = [...given, ...cur.map(lawFor)], dl = within(Math.min(eUntil, vnow() + q1), () => decideLaw(mm, ls2, given.length + cur.indexOf(f)));
|
|
3563
|
+
if (!dl) {
|
|
3564
|
+
undecided.add(f);
|
|
3565
|
+
continue;
|
|
3566
|
+
}
|
|
3567
|
+
undecided.delete(f);
|
|
3568
|
+
if (dl.proven)
|
|
3569
|
+
uses.set(textOf(f), dl.uses);
|
|
3570
|
+
else {
|
|
3571
|
+
failed.add(f);
|
|
3572
|
+
uses.delete(textOf(f));
|
|
3573
|
+
cur = cur.filter(g => g !== f);
|
|
3574
|
+
}
|
|
3575
|
+
}
|
|
3576
|
+
}
|
|
3577
|
+
else
|
|
3578
|
+
for (const f of todo)
|
|
3579
|
+
ask(f, q1);
|
|
3580
|
+
let second = 0;
|
|
3581
|
+
// (each once: one still undecided then is kept, the cone's proof deciding)
|
|
3582
|
+
if (!failed.size && undecided.size)
|
|
3583
|
+
for (const f of hs.filter(g => undecided.has(g) && !reasked.has(g))) {
|
|
3584
|
+
second++;
|
|
3585
|
+
reasked.add(f);
|
|
3586
|
+
ask(f, Math.max(q1, q2));
|
|
3587
|
+
}
|
|
3588
|
+
ph({ phase: 'round', round: r, cubes: hs.length, asked: todo.length, second, failed: failed.size, undecided: undecided.size, ms: Math.round(vnow() - tr) });
|
|
3589
|
+
if (!failed.size)
|
|
3590
|
+
break;
|
|
3591
|
+
for (const f of failed)
|
|
3592
|
+
dead.add(textOf(f));
|
|
3593
|
+
const gone = new Set([...failed].map(textOf)), inSet = new Set(hs.filter(f => !failed.has(f)));
|
|
3594
|
+
const back = [...by].filter(([b, as]) => !inSet.has(b) && !dead.has(textOf(b)) && as.some(a => failed.has(a)) && !as.some(a => inSet.has(a))).map(([b]) => b).filter(fresh);
|
|
3595
|
+
hs = [...inSet, ...back];
|
|
3596
|
+
// (only the goals whose proofs read a dropped cube, the new ones, and the undecided are asked again)
|
|
3597
|
+
asked = new Set([...back, ...hs.filter(f => !uses.has(textOf(f)) || uses.get(textOf(f)).some(u => gone.has(u)))]);
|
|
3598
|
+
}
|
|
3599
|
+
if (stop() || vnow() > eUntil) {
|
|
3600
|
+
ph({ phase: 'out of time' });
|
|
3601
|
+
return;
|
|
3602
|
+
}
|
|
3603
|
+
// (GDP_TD_DECIDED=1: the cubes Houdini left undecided out before the caller's goals: the cone of decided cubes)
|
|
3604
|
+
if (env.GDP_TD_DECIDED === '1')
|
|
3605
|
+
hs = hs.filter(f => !undecided.has(f));
|
|
3606
|
+
const tc = vnow(), need = new Set();
|
|
3607
|
+
const callerFrom = () => {
|
|
3608
|
+
need.clear();
|
|
3609
|
+
const ls = [...given, ...hs.map(lawFor), ...(ghost ? [ghost.plain] : [])], mm = withGhost(forLaws(ls).m);
|
|
3610
|
+
for (const l of [...laws, ...(ghost ? [ghost.plain] : [])]) {
|
|
3611
|
+
const dl = within(Math.min(eUntil, vnow() + joinQueryMs), () => decideLaw(mm, ls, ls.findIndex(k => k.name === l.name)));
|
|
3612
|
+
if (!dl?.proven)
|
|
3613
|
+
return false;
|
|
3614
|
+
for (const u of dl.uses)
|
|
3615
|
+
need.add(u);
|
|
3616
|
+
}
|
|
3617
|
+
return true;
|
|
3618
|
+
};
|
|
3619
|
+
let callerOk = callerFrom();
|
|
3620
|
+
if (!callerOk && !ghosts) {
|
|
3621
|
+
ghosts = ghostsOf();
|
|
3622
|
+
for (ghost = ghosts.shift(); ghost && !(callerOk = callerFrom()); ghost = ghosts.shift())
|
|
3623
|
+
;
|
|
3624
|
+
}
|
|
3625
|
+
const close = () => { for (const todo = [...need]; todo.length;)
|
|
3626
|
+
for (const u of uses.get(todo.pop()) ?? [])
|
|
3627
|
+
if (!need.has(u)) {
|
|
3628
|
+
need.add(u);
|
|
3629
|
+
todo.push(u);
|
|
3630
|
+
} };
|
|
3631
|
+
close();
|
|
3632
|
+
for (let it = 0; callerOk && it < 8 && vnow() < eUntil && !stop(); it++) {
|
|
3633
|
+
const inCone = hs.filter(f => need.has(textOf(f))), open = inCone.filter(f => !uses.has(textOf(f)));
|
|
3634
|
+
if (!open.length)
|
|
3635
|
+
break;
|
|
3636
|
+
const ls = [...given, ...inCone.map(lawFor), ...(ghost ? [ghost.plain] : [])], mm = withGhost(forLaws(ls).m);
|
|
3637
|
+
let grew = 0, settledN = 0;
|
|
3638
|
+
for (const f of open) {
|
|
3639
|
+
if (vnow() > eUntil)
|
|
3640
|
+
break;
|
|
3641
|
+
const dl = within(Math.min(eUntil, vnow() + q2), () => decideLaw(mm, ls, ls.findIndex(k => k.name === textOf(f))));
|
|
3642
|
+
if (dl?.proven) {
|
|
3643
|
+
uses.set(textOf(f), dl.uses);
|
|
3644
|
+
settledN++;
|
|
3645
|
+
continue;
|
|
3646
|
+
}
|
|
3647
|
+
if (!dl || !dl.goal.endsWith('@step'))
|
|
3648
|
+
continue;
|
|
3649
|
+
const pt = ptOf(preOf(dl.witness));
|
|
3650
|
+
for (const g of hs)
|
|
3651
|
+
if (!need.has(textOf(g)) && !holdsAt(g, pt)) {
|
|
3652
|
+
need.add(textOf(g));
|
|
3653
|
+
grew++;
|
|
3654
|
+
}
|
|
3655
|
+
}
|
|
3656
|
+
close();
|
|
3657
|
+
if (!grew && !settledN)
|
|
3658
|
+
break;
|
|
3659
|
+
}
|
|
3660
|
+
let cut = hs.filter(f => need.has(textOf(f)));
|
|
3661
|
+
ph({ phase: 'cone', fixpoint: hs.length, undecided: undecided.size, caller: callerOk ? 1 : 0, cone: cut.length, undecidedInCone: cut.filter(f => undecided.has(f)).length, ms: Math.round(vnow() - tc) });
|
|
3662
|
+
if (callerOk)
|
|
3663
|
+
for (let k = 0; k < 24 && vnow() < eUntil && !stop(); k++) {
|
|
3664
|
+
const t2 = vnow(), setLaws = [...laws, ...cut.map(lawFor), ...(ghost ? [ghost.some] : [])];
|
|
3665
|
+
const h0 = memoHits.n, m0 = memoHits.miss;
|
|
3666
|
+
let v = within(t0 + ms, () => prove(setLaws, true));
|
|
3667
|
+
const hits = memoHits.n - h0, misses = memoHits.miss - m0, fastMs = Math.round(vnow() - t2), fast = v.proven && v.checked ? 1 : 0, fastAt = String(v.goal ?? '').slice(0, 100);
|
|
3668
|
+
if (!(v.proven && v.checked))
|
|
3669
|
+
v = within(t0 + ms, () => prove(setLaws));
|
|
3670
|
+
const at = String(v.goal ?? ''), g = v.proven && v.checked ? undefined : cut.find(c => at.startsWith(`${textOf(c)} @`));
|
|
3671
|
+
ph({ phase: 'prove', cubes: cut.length, proven: v.proven && v.checked ? 1 : 0, ms: Math.round(vnow() - t2), fastMs, fast, hits, misses, setKey: memoHits.setKey, tdKey: env.GDP_MEMO_TRACE ? machineKey(forLaws([...given, ...cut.map(lawFor)]).m) : '', diff: env.GDP_MEMO_TRACE ? ((x, y) => { let i = 0; while (i < x.length && x[i] === y[i])
|
|
3672
|
+
i++; return `${i}: ${x.slice(Math.max(0, i - 60), i + 60)} || ${y.slice(Math.max(0, i - 60), i + 60)}`; })(JSON.stringify(forLaws([...given, ...cut.map(lawFor)]).m), memoHits.setJson) : '', fastAt, at: at.slice(0, 100) });
|
|
3673
|
+
if (v.proven && v.checked) {
|
|
3674
|
+
why = 'inductive';
|
|
3675
|
+
cone = cut;
|
|
3676
|
+
enumProof = v;
|
|
3677
|
+
enumSome = ghost ? [ghost.some] : [];
|
|
3678
|
+
cubes = cut;
|
|
3679
|
+
break;
|
|
3680
|
+
}
|
|
3681
|
+
if (!g)
|
|
3682
|
+
break;
|
|
3683
|
+
cut = cut.filter(c => c !== g);
|
|
3684
|
+
}
|
|
3685
|
+
}));
|
|
3686
|
+
for (let round = 0, over = false; !over && cubes.length && !stop(); round++)
|
|
3687
|
+
withTrees(() => {
|
|
3688
|
+
// (one cube once: a cube in the set is not pending, and a text in the set once)
|
|
3689
|
+
cubes = cubes.filter((g, i) => !atStart(g) && cubes.findIndex(h => textOf(h) === textOf(g)) === i);
|
|
3690
|
+
for (const g of cubes)
|
|
3691
|
+
if (pending.has(g) || [...pending].some(h => textOf(h) === textOf(g)))
|
|
3692
|
+
for (const h of [...pending])
|
|
3693
|
+
if (textOf(h) === textOf(g))
|
|
3694
|
+
pending.delete(h);
|
|
3695
|
+
for (const g of cubes)
|
|
3696
|
+
if (!addedAt.has(g))
|
|
3697
|
+
addedAt.set(g, round);
|
|
3698
|
+
const stand = (nm) => { stands.add(nm); provenAt.set(nm, round); };
|
|
3699
|
+
// (DuoAI's: the Houdini fixpoint of the relevant candidates, once, with 40% of the engine's time left: the set, the pending cubes and every cube a
|
|
3700
|
+
// counterexample ranked in its top eight, a cube the fast proof fails at dropped (the usual proof where it fails at
|
|
3701
|
+
// a cube the rounds proved), until the whole proves or the time is out)
|
|
3702
|
+
if (!houdinied && !enumProof && vnow() > eUntil - 0.4 * (eUntil - engineStart)) {
|
|
3703
|
+
houdinied = true;
|
|
3704
|
+
let hs = [...new Map([...cubes, ...pending, ...pool].filter(g => !dead.has(textOf(g)) && !atStart(g)).map(g => [textOf(g), g])).values()];
|
|
3705
|
+
for (let k = 0; k < 200 && hs.length && vnow() < eUntil && !stop(); k++) {
|
|
3706
|
+
const t2 = vnow(), hl = [...laws, ...hs.map(lawFor), ...(ghost ? [ghost.some] : [])];
|
|
3707
|
+
let v = within(t0 + ms, () => prove(hl, true));
|
|
3708
|
+
const at0 = String(v.goal ?? ''), g0 = v.proven && v.checked ? undefined : hs.find(c => at0.startsWith(`${textOf(c)} @`));
|
|
3709
|
+
// (or at a law the caller gave, a kind's: no cube to blame, so the usual proof's verdict)
|
|
3710
|
+
if (!(v.proven && v.checked) && (g0 ? at0.endsWith('@step') && stands.has(lawFor(g0).name) : true))
|
|
3711
|
+
v = within(t0 + ms, () => prove(hl));
|
|
3712
|
+
const at = String(v.goal ?? ''), g = v.proven && v.checked ? undefined : hs.find(c => at.startsWith(`${textOf(c)} @`));
|
|
3713
|
+
opts.progress?.('join houdini', { cubes: hs.length, proven: v.proven && v.checked ? 1 : 0, ms: Math.round(vnow() - t2), at: at.slice(0, 100) });
|
|
3714
|
+
if (v.proven && v.checked) {
|
|
3715
|
+
why = 'inductive';
|
|
3716
|
+
cone = hs;
|
|
3717
|
+
enumProof = v;
|
|
3718
|
+
enumSome = ghost ? [ghost.some] : [];
|
|
3719
|
+
cubes = hs;
|
|
3720
|
+
break;
|
|
3721
|
+
}
|
|
3722
|
+
if (!g)
|
|
3723
|
+
break;
|
|
3724
|
+
hs = hs.filter(c => c !== g);
|
|
3725
|
+
}
|
|
3726
|
+
}
|
|
3727
|
+
if (enumProof) {
|
|
3728
|
+
over = true;
|
|
3729
|
+
return;
|
|
3730
|
+
}
|
|
3731
|
+
let asked = 0, cubeMs = 0, tGoal = 0;
|
|
3732
|
+
// (later rounds less the sums pass's reserve, unless the caller's law holds from the set: Houdini's part, then)
|
|
3733
|
+
const kUntil = () => eUntil - (round && !held ? reserve : 0);
|
|
3734
|
+
const real = new Set(), unsure = new Set(), lsAll = [...given, ...cubes.map(lawFor), ...(ghost ? [ghost.plain] : [])], blocked2 = [], mmRound = withGhost(forLaws(lsAll).m), at2 = new Map(cubes.map((g, i) => [g, i]));
|
|
3735
|
+
const version = versionOf(), startReal = new Set(), promoted = [];
|
|
3736
|
+
let caller = false, complete = true, again = false, coneTried = false;
|
|
3737
|
+
// (relative induction of the caller's cone first, IC3's: once the caller's goals (and the ∃ law's) are proven, the
|
|
3738
|
+
// cubes their proofs read, closed under each cube's own proof's, each asked (one undecided stays, its support
|
|
3739
|
+
// unknown), and the cone proven whole; a proof ends the rounds. A cone cube with a model, or the one the cone's
|
|
3740
|
+
// proof fails at, is left out and the caller asked again (Houdini over the cone), a few times. The memo keeps every
|
|
3741
|
+
// decision for the round's goals after it. GDP_IC3_CONE=0 off)
|
|
3742
|
+
const tryCone = () => {
|
|
3743
|
+
const t1 = vnow(), out2 = new Set();
|
|
3744
|
+
for (let it = 0; it < 6; it++) {
|
|
3745
|
+
const cs = cubes.filter(c => !out2.has(c)), ls2 = it ? [...given, ...cs.map(lawFor), ...(ghost ? [ghost.plain] : [])] : lsAll, mm2 = it ? withGhost(forLaws(ls2).m) : mmRound;
|
|
3746
|
+
const byName = new Map(cs.map(c => [textOf(c), c])), at3 = new Map(cs.map((c, i) => [c, given.length + i])), need = new Set(), todo = [];
|
|
3747
|
+
const ask = (i) => (vnow() > kUntil() || stop() ? undefined : within(Math.min(kUntil(), vnow() + joinQueryMs / 8), () => decideLaw(mm2, ls2, i)));
|
|
3748
|
+
const add = (us) => { for (const u of us)
|
|
3749
|
+
if (!need.has(u)) {
|
|
3750
|
+
need.add(u);
|
|
3751
|
+
todo.push(u);
|
|
3752
|
+
} };
|
|
3753
|
+
for (const l of [...laws, ...(ghost ? [ghost.plain] : [])]) {
|
|
3754
|
+
const dl = ask(ls2.findIndex(k => k.name === l.name));
|
|
3755
|
+
if (!dl?.proven)
|
|
3756
|
+
return false;
|
|
3757
|
+
add(dl.uses);
|
|
3758
|
+
}
|
|
3759
|
+
let open2 = 0, failed;
|
|
3760
|
+
while (todo.length && !failed) {
|
|
3761
|
+
const c = byName.get(todo.pop());
|
|
3762
|
+
if (!c)
|
|
3763
|
+
continue;
|
|
3764
|
+
if (vnow() > kUntil() || stop())
|
|
3765
|
+
return false;
|
|
3766
|
+
const dl = ask(at3.get(c));
|
|
3767
|
+
if (!dl) {
|
|
3768
|
+
open2++;
|
|
3769
|
+
continue;
|
|
3770
|
+
}
|
|
3771
|
+
if (dl.proven)
|
|
3772
|
+
add(dl.uses);
|
|
3773
|
+
else
|
|
3774
|
+
failed = c;
|
|
3775
|
+
}
|
|
3776
|
+
if (failed) {
|
|
3777
|
+
out2.add(failed);
|
|
3778
|
+
opts.progress?.('join cone', { round, it, failed: textOf(failed).slice(0, 100), need: need.size, ms: Math.round(vnow() - t1) });
|
|
3779
|
+
continue;
|
|
3780
|
+
}
|
|
3781
|
+
const cut = cs.filter(c => need.has(textOf(c)));
|
|
3782
|
+
const v = within(t0 + ms, () => prove([...laws, ...cut.map(lawFor), ...(ghost ? [ghost.some] : [])], true));
|
|
3783
|
+
const at = String(v.goal ?? ''), g = v.proven && v.checked ? undefined : cut.find(c => at.startsWith(`${textOf(c)} @`));
|
|
3784
|
+
opts.progress?.('join cone', { round, it, cubes: cubes.length, cone: cut.length, undecided: open2, proven: v.proven && v.checked ? 1 : 0, ms: Math.round(vnow() - t1), at: at.slice(0, 100) });
|
|
3785
|
+
if (v.proven && v.checked) {
|
|
3786
|
+
why = 'inductive';
|
|
3787
|
+
cone = cut;
|
|
3788
|
+
enumProof = v;
|
|
3789
|
+
enumSome = ghost ? [ghost.some] : [];
|
|
3790
|
+
cubes = cut;
|
|
3791
|
+
return true;
|
|
3792
|
+
}
|
|
3793
|
+
if (!g)
|
|
3794
|
+
return false;
|
|
3795
|
+
out2.add(g);
|
|
3796
|
+
}
|
|
3797
|
+
return false;
|
|
3798
|
+
};
|
|
3799
|
+
{
|
|
3800
|
+
for (const goal of [...laws.map(l => l.name), ...(ghost ? [ghost.plain.name] : []), ...cubes]) {
|
|
3801
|
+
if (vnow() > kUntil() || stop()) {
|
|
3802
|
+
complete = false;
|
|
3803
|
+
break;
|
|
3804
|
+
}
|
|
3805
|
+
const f = typeof goal === 'string' ? undefined : goal, ls = lsAll, others = cubes.filter(g => g !== f);
|
|
3806
|
+
const idx = f ? given.length + at2.get(f) : ls.findIndex(l => l.name === goal), mm = mmRound;
|
|
3807
|
+
if (f && !coneTried && env.GDP_IC3_CONE !== '0') {
|
|
3808
|
+
coneTried = true;
|
|
3809
|
+
if (tryCone()) {
|
|
3810
|
+
over = true;
|
|
3811
|
+
return;
|
|
3812
|
+
}
|
|
3813
|
+
}
|
|
3814
|
+
if (f)
|
|
3815
|
+
held = true;
|
|
3816
|
+
if ((f && kept2.has(f)) || stands.has(ls[idx].name))
|
|
3817
|
+
continue;
|
|
3818
|
+
const was = settled.get(ls[idx].name);
|
|
3819
|
+
if (was && was.every(n2 => ls.some(l => l.name === n2)))
|
|
3820
|
+
continue;
|
|
3821
|
+
settled.delete(ls[idx].name);
|
|
3822
|
+
// (a goal decided by the EPR path alone first (a proof or a model, a cube's within a quarter of a join query, the
|
|
3823
|
+
// caller's within one); else a model or a refutation of the CDCL search alone, then the proof path's)
|
|
3824
|
+
if (f) {
|
|
3825
|
+
if (tGoal)
|
|
3826
|
+
cubeMs += vnow() - tGoal;
|
|
3827
|
+
tGoal = vnow();
|
|
3828
|
+
asked++;
|
|
3829
|
+
}
|
|
3830
|
+
else
|
|
3831
|
+
tGoal = 0;
|
|
3832
|
+
const tDl = vnow(), dlEnd = Math.min(kUntil(), vnow() + (f ? joinQueryMs / 8 : joinQueryMs)), dl = within(dlEnd, () => decideLaw(mm, ls, idx));
|
|
3833
|
+
// (the EPR path out of time on the caller's goal, or refuting it with a core the kernel's search didn't finish:
|
|
3834
|
+
// the proof path would be no quicker)
|
|
3835
|
+
const outOfTime = !dl && (vnow() - tDl >= 0.9 * (dlEnd - tDl) || decideNote.refuted);
|
|
3836
|
+
if (dl?.proven) {
|
|
3837
|
+
stand(ls[idx].name);
|
|
3838
|
+
continue;
|
|
3839
|
+
}
|
|
3840
|
+
let isReal = !!dl && (f && dl.goal.endsWith('@start') ? badAtStart(f, dl.witness) : realCti(dl.witness, f, others)), wit = dl?.witness;
|
|
3841
|
+
if (f && isReal && dl.goal.endsWith('@start'))
|
|
3842
|
+
startReal.add(f);
|
|
3843
|
+
// (a cube the EPR path ran out of time on: unsure at once, kept as optimistic Houdini keeps it; the CDCL search
|
|
3844
|
+
// after it would be no quicker)
|
|
3845
|
+
if (!dl && !(f && outOfTime)) {
|
|
3846
|
+
const inst = mapNames.length ? lawInstances(mm, ls, idx) : undefined, g = obligations(mm, ls, inst ? { [ls[idx].name]: inst } : {}, idx)[1];
|
|
3847
|
+
// (the engine's wall too: a counted budget alone runs on past it, wo's 6 s)
|
|
3848
|
+
// (its share on the wall too: a counted budget of 2 s ran 8.8 s on wo's 3-key cubes)
|
|
3849
|
+
const quick = budgeted(joinQueryMs / 4, until2 => growCdcl(g, new Map(), { nodes: 0, until: until2 }, { model: true, core: true, wall: wallUntil }));
|
|
3850
|
+
if (!('tree' in quick) && quick.why === REFUTED && quick.core) {
|
|
3851
|
+
const lawAt = lawsFrom(mm, ls, false, inst ?? {});
|
|
3852
|
+
settled.set(ls[idx].name, [...new Set([...quick.core].map(h => lawAt.get(h)).filter((k) => k !== undefined && k >= given.length && k !== idx).map(k => ls[k].name))]);
|
|
3853
|
+
}
|
|
3854
|
+
if ('tree' in quick || quick.why === REFUTED) {
|
|
3855
|
+
stand(ls[idx].name);
|
|
3856
|
+
continue;
|
|
3857
|
+
}
|
|
3858
|
+
// (the caller's law's model from the proof path alone: EPR's, real or with the instances that refute it)
|
|
3859
|
+
isReal = !!f && quick.stuck === 'sat' && !!quick.witness && realCti(quick.witness, f, others);
|
|
3860
|
+
if (quick.stuck === 'sat')
|
|
3861
|
+
wit = quick.witness;
|
|
3862
|
+
// (a cube the EPR path doesn't decide: the CDCL search's answer alone; the caller's, the proof path's too)
|
|
3863
|
+
if (!isReal && !f && !outOfTime) {
|
|
3864
|
+
proofs++;
|
|
3865
|
+
const r2 = within(Math.min(kUntil() - 2000, vnow() + Math.max(joinQueryMs * 1.25, (kUntil() - vnow()) / 2)), () => proveLaw(mm, ls, idx));
|
|
3866
|
+
if (r2.proven) {
|
|
3867
|
+
stand(ls[idx].name);
|
|
3868
|
+
continue;
|
|
3869
|
+
}
|
|
3870
|
+
isReal = !!r2.witness && realCti(r2.witness, f, others);
|
|
3871
|
+
wit = r2.witness ?? wit;
|
|
3872
|
+
}
|
|
3873
|
+
}
|
|
3874
|
+
// (a real counterexample from a state no small state is: its diagram blocked, the goal's laws kept)
|
|
3875
|
+
// (a blocker blocked already this round blocks as well: the goal kept, nothing new blocked; one in the set doesn't
|
|
3876
|
+
// (a model the set rules out is no counterexample to learn from))
|
|
3877
|
+
const fresh = (g) => !blocked2.some(h => textOf(h) === textOf(g)) && !!blocked2.push(g);
|
|
3878
|
+
const outside = (g) => !ls.some(l => l.name === textOf(g));
|
|
3879
|
+
const ds = isReal && lastPre ? blockers(lastPre).filter(outside) : [];
|
|
3880
|
+
ds.filter(fresh);
|
|
3881
|
+
// (the caller's: blocked at once, IC3's order, the caller asked again before the cubes)
|
|
3882
|
+
if (ds.length && !f && lastPre)
|
|
3883
|
+
pres.push(lastPre);
|
|
3884
|
+
if (ds.length) {
|
|
3885
|
+
if (!f) {
|
|
3886
|
+
held = false;
|
|
3887
|
+
if (!ghosts) {
|
|
3888
|
+
ghosts = ghostsOf();
|
|
3889
|
+
ghost = ghosts.shift();
|
|
3890
|
+
if (ghost)
|
|
3891
|
+
opts.progress?.('join exists', { law: ghost.plain.name.slice(0, 160) });
|
|
3892
|
+
}
|
|
3893
|
+
break;
|
|
3894
|
+
}
|
|
3895
|
+
continue;
|
|
3896
|
+
}
|
|
3897
|
+
// (a model the interpreter doesn't confirm: PDR's obligation at its state, its diagrams blocked and the goal kept
|
|
3898
|
+
// (the caller's asked again first) where a row of it no small state has; else unsure)
|
|
3899
|
+
if (!isReal && wit) {
|
|
3900
|
+
const pre = preOf(wit);
|
|
3901
|
+
const ds2 = blockers(pre).filter(outside);
|
|
3902
|
+
ds2.filter(fresh);
|
|
3903
|
+
if (ds2.length && !f)
|
|
3904
|
+
pres.push(pre);
|
|
3905
|
+
if (ds2.length) {
|
|
3906
|
+
if (!f) {
|
|
3907
|
+
held = false;
|
|
3908
|
+
break;
|
|
3909
|
+
}
|
|
3910
|
+
continue;
|
|
3911
|
+
}
|
|
3912
|
+
;
|
|
3913
|
+
(f ? subs : pres).push(pre);
|
|
3914
|
+
}
|
|
3915
|
+
else if (!f && isReal && lastPre)
|
|
3916
|
+
pres.push(lastPre);
|
|
3917
|
+
// (the ∃ law's own goal unproven: it goes; the caller's unproven with no diagram (every view's rows reached, or no
|
|
3918
|
+
// model to read): the ∀∃ laws, once)
|
|
3919
|
+
if (!f && ghost && goal === ghost.plain.name) {
|
|
3920
|
+
opts.progress?.('join exists', { dropped: ghost.plain.name.slice(0, 120) });
|
|
3921
|
+
ghost = ghosts?.shift();
|
|
3922
|
+
stands.clear();
|
|
3923
|
+
again = true;
|
|
3924
|
+
break;
|
|
3925
|
+
}
|
|
3926
|
+
if (!f && !ghosts) {
|
|
3927
|
+
const t1 = vnow();
|
|
3928
|
+
ghosts = ghostsOf();
|
|
3929
|
+
ghost = ghosts.shift();
|
|
3930
|
+
opts.progress?.('join exists', { law: ghost ? `${ghost.plain.name.slice(0, 160)}; ${ghost.name} := ${JSON.stringify(ghost.step).slice(0, 160)}` : 'none', ms: Math.round(vnow() - t1) });
|
|
3931
|
+
if (ghost) {
|
|
3932
|
+
again = true;
|
|
3933
|
+
held = false;
|
|
3934
|
+
break;
|
|
3935
|
+
}
|
|
3936
|
+
}
|
|
3937
|
+
if (!f) {
|
|
3938
|
+
caller = true;
|
|
3939
|
+
held = false;
|
|
3940
|
+
break;
|
|
3941
|
+
}
|
|
3942
|
+
;
|
|
3943
|
+
(isReal ? real : unsure).add(f);
|
|
3944
|
+
}
|
|
3945
|
+
}
|
|
3946
|
+
opts.progress?.('join enumerated', { round, cubes: cubes.length, real: real.size, unsure: unsure.size, caller: caller ? 1 : 0, diagrams: blocked2.length, ms: Math.round(vnow()) });
|
|
3947
|
+
// (optimistic Houdini: an unsure cube holds at every small state and every step from one (they are its rows), so
|
|
3948
|
+
// it stays; the set's proof decides, a cube it fails at dropped and the rounds go on)
|
|
3949
|
+
// (IC3's push: the pending cubes asked together, each with the set and every other pending one assumed (one frame),
|
|
3950
|
+
// those with a model out (an undecided one stays, as Houdini keeps it) and the rest asked again, until none has
|
|
3951
|
+
// one; what is left comes back into the set together.
|
|
3952
|
+
// Once the set has changed since the last push; each cube back twice at most)
|
|
3953
|
+
let frame = complete && !caller ? [...pending].filter(g => tried.get(g) !== version && (backs.get(g) ?? 0) < 2) : [];
|
|
3954
|
+
const pushed = frame.length;
|
|
3955
|
+
for (const g of frame)
|
|
3956
|
+
tried.set(g, version);
|
|
3957
|
+
for (let pass = 0; frame.length && pass < 4 && vnow() < kUntil() && !stop(); pass++) {
|
|
3958
|
+
frame = frame.filter((g, i) => !lsAll.some(l => l.name === textOf(g)) && frame.findIndex(h => textOf(h) === textOf(g)) === i);
|
|
3959
|
+
const lsP = [...lsAll, ...frame.map(lawFor)], mmP = withGhost(forLaws(lsP).m), failed = new Set();
|
|
3960
|
+
frame.forEach((g, i) => {
|
|
3961
|
+
if (vnow() > kUntil()) {
|
|
3962
|
+
failed.add(g);
|
|
3963
|
+
return;
|
|
3964
|
+
}
|
|
3965
|
+
const dl = within(Math.min(kUntil(), vnow() + joinQueryMs / 8), () => decideLaw(mmP, lsP, lsAll.length + i));
|
|
3966
|
+
if (!dl || dl.proven)
|
|
3967
|
+
return;
|
|
3968
|
+
failed.add(g);
|
|
3969
|
+
// (the push's counterexample an obligation, IC3's: blocked as a round's are, by the enumeration's cubes false
|
|
3970
|
+
// there, so that the next push has what it lacked)
|
|
3971
|
+
if (dl.goal.endsWith('@step'))
|
|
3972
|
+
for (const b of blockers(preOf(dl.witness)))
|
|
3973
|
+
if (!lsP.some(l => l.name === textOf(b)) && !pending.has(b) && !blocked2.some(h => textOf(h) === textOf(b)))
|
|
3974
|
+
blocked2.push(b);
|
|
3975
|
+
});
|
|
3976
|
+
if (!failed.size) {
|
|
3977
|
+
promoted.push(...frame);
|
|
3978
|
+
break;
|
|
3979
|
+
}
|
|
3980
|
+
frame = frame.filter(g => !failed.has(g));
|
|
3981
|
+
}
|
|
3982
|
+
if (pending.size)
|
|
3983
|
+
opts.progress?.('join frames', { round, pending: pending.size, pushed, promoted: promoted.length });
|
|
3984
|
+
// (IC3's frames: a failing cube pending, not dropped, unless a start breaks it)
|
|
3985
|
+
const drops = new Set(real);
|
|
3986
|
+
for (const g of unsure)
|
|
3987
|
+
kept2.add(g);
|
|
3988
|
+
const demote = (gs) => { for (const g of gs) {
|
|
3989
|
+
if (startReal.has(g))
|
|
3990
|
+
dead.add(textOf(g));
|
|
3991
|
+
else
|
|
3992
|
+
pending.add(g);
|
|
3993
|
+
addedAt.delete(g);
|
|
3994
|
+
tried.delete(g);
|
|
3995
|
+
} };
|
|
3996
|
+
const back = () => { for (const g of promoted) {
|
|
3997
|
+
pending.delete(g);
|
|
3998
|
+
backs.set(g, (backs.get(g) ?? 0) + 1);
|
|
3999
|
+
addedAt.delete(g);
|
|
4000
|
+
} return promoted; };
|
|
4001
|
+
// (the costliest round's mean a goal, 250 ms at least: round 0's minimal cubes are the cheapest)
|
|
4002
|
+
if (asked > 1)
|
|
4003
|
+
perGoal = Math.max(perGoal, 250, cubeMs / (asked - 1));
|
|
4004
|
+
// (the next round's new cubes: what a quarter of what is left affords past the goals it asks again, 8 at least)
|
|
4005
|
+
const grow = (kept) => { const ask = kept.filter(g => !kept2.has(g) && !stands.has(lawFor(g).name)).length, afford = perGoal ? Math.floor((eUntil - vnow()) / (4 * perGoal)) : 16; return relevant(Math.max(8, Math.min(256 - kept.length, afford - ask)), kept); };
|
|
4006
|
+
if (blocked2.length) {
|
|
4007
|
+
if (drops.size)
|
|
4008
|
+
unstand(drops);
|
|
4009
|
+
demote(drops);
|
|
4010
|
+
const kept = [...cubes.filter(g => !drops.has(g)), ...back(), ...blocked2];
|
|
4011
|
+
cubes = [...kept, ...grow(kept)];
|
|
4012
|
+
return;
|
|
4013
|
+
}
|
|
4014
|
+
if (again)
|
|
4015
|
+
return;
|
|
4016
|
+
// (the caller's law unproven with no diagram: the cubes its counterexamples make relevant, while there are some)
|
|
4017
|
+
if (caller && complete) {
|
|
4018
|
+
if (drops.size)
|
|
4019
|
+
unstand(drops);
|
|
4020
|
+
demote(drops);
|
|
4021
|
+
const kept = [...cubes.filter(g => !drops.has(g)), ...back()], more = grow(kept);
|
|
4022
|
+
if (more.length) {
|
|
4023
|
+
cubes = [...kept, ...more];
|
|
4024
|
+
return;
|
|
4025
|
+
}
|
|
4026
|
+
}
|
|
4027
|
+
// (the set's proof, relative induction over the set: a cube it fails at dropped and the proof asked again at once, the
|
|
4028
|
+
// rounds again only where the caller's law fails or a cube's goal can't be named)
|
|
4029
|
+
const gone = [];
|
|
4030
|
+
let grown = false;
|
|
4031
|
+
if (complete && !caller && !drops.size)
|
|
4032
|
+
for (let k = 0; k < 24 && !stop() && vnow() < eUntil; k++) {
|
|
4033
|
+
const t2 = vnow(), setLaws = [...laws, ...cubes.map(lawFor), ...(ghost ? [ghost.some] : [])];
|
|
4034
|
+
let v = within(t0 + ms, () => prove(setLaws, true));
|
|
4035
|
+
// (the fast proof failing at a cube whose own goal the rounds proved from this set: the two disagree, and the
|
|
4036
|
+
// usual proof decides (proveSetFast has a spurious model on ring's own-id law))
|
|
4037
|
+
const at0 = String(v.goal ?? ''), g0 = v.proven && v.checked ? undefined : cubes.find(c => at0.startsWith(`${textOf(c)} @`));
|
|
4038
|
+
if (g0 && at0.endsWith('@step') && stands.has(lawFor(g0).name))
|
|
4039
|
+
v = within(t0 + ms, () => prove(setLaws));
|
|
4040
|
+
const at = String(v.goal ?? ''), g = v.proven && v.checked ? undefined : cubes.find(c => at.startsWith(`${textOf(c)} @`));
|
|
4041
|
+
opts.progress?.('join set', { cubes: cubes.length, exists: ghost ? 1 : 0, proven: v.proven && v.checked ? 1 : 0, ms: Math.round(vnow() - t2), at: at.slice(0, 120) });
|
|
4042
|
+
if (v.proven && v.checked) {
|
|
4043
|
+
why = 'inductive';
|
|
4044
|
+
cone = cubes;
|
|
4045
|
+
enumProof = v;
|
|
4046
|
+
enumSome = ghost ? [ghost.some] : [];
|
|
4047
|
+
break;
|
|
4048
|
+
}
|
|
4049
|
+
if (!g)
|
|
4050
|
+
break;
|
|
4051
|
+
// (IC3's: the failing cube's model at its step, blocked by the enumeration's cubes false at its state where there
|
|
4052
|
+
// are some new ones, the cube kept and the rounds again; else the cube dropped)
|
|
4053
|
+
const wit2 = v.witness, bs = wit2 && at.endsWith('@step') ? blockers(preOf(wit2)).filter(b => !cubes.some(c => textOf(c) === textOf(b)) && ![...pending].some(c => textOf(c) === textOf(b)) && !dead.has(textOf(b)) && !atStart(b)) : [];
|
|
4054
|
+
if (bs.length) {
|
|
4055
|
+
cubes = [...cubes, ...bs];
|
|
4056
|
+
grown = true;
|
|
4057
|
+
break;
|
|
4058
|
+
}
|
|
4059
|
+
if (wit2 && at.endsWith('@step'))
|
|
4060
|
+
subs.push(preOf(wit2));
|
|
4061
|
+
cubes = cubes.filter(c => c !== g);
|
|
4062
|
+
gone.push(g);
|
|
4063
|
+
}
|
|
4064
|
+
if (!complete || caller || (!drops.size && !gone.length && !grown && !promoted.length && !pending.size) || enumProof)
|
|
4065
|
+
over = true;
|
|
4066
|
+
if (drops.size || gone.length)
|
|
4067
|
+
unstand([...drops, ...gone]);
|
|
4068
|
+
demote([...drops, ...gone]);
|
|
4069
|
+
cubes = [...cubes.filter(g => !drops.has(g)), ...back()];
|
|
4070
|
+
// (nothing changed and nothing left to push: the frames at their fixpoint, the set grown by relevance to the
|
|
4071
|
+
// counterexamples while there is something relevant)
|
|
4072
|
+
if (!enumProof && !drops.size && !gone.length && !grown && !promoted.length && [...pending].every(g => tried.get(g) === versionOf() || (backs.get(g) ?? 0) >= 2)) {
|
|
4073
|
+
const more = complete && !enumProof ? grow(cubes) : [];
|
|
4074
|
+
if (more.length) {
|
|
4075
|
+
cubes = [...cubes, ...more];
|
|
4076
|
+
over = false;
|
|
4077
|
+
}
|
|
4078
|
+
else
|
|
4079
|
+
over = true;
|
|
4080
|
+
}
|
|
4081
|
+
});
|
|
4082
|
+
}
|
|
4083
|
+
};
|
|
2322
4084
|
for (;;) {
|
|
2323
4085
|
if (now() > jUntil || stop()) {
|
|
2324
4086
|
why = 'time';
|
|
@@ -2361,8 +4123,10 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
2361
4123
|
// (before the whole set, the forms proven so far beside the context: a proof there is a proof with the whole set,
|
|
2362
4124
|
// at a fraction of its hypotheses; a model there, the whole set asked as before)
|
|
2363
4125
|
const provenFirst = () => { const ps = whole.filter(f => !ctx.includes(f) && stood.has(textOf(f))); return ps.length && ctx.length + ps.length < whole.length / 2 ? [...ctx, ...ps] : undefined; };
|
|
2364
|
-
|
|
2365
|
-
|
|
4126
|
+
// (a few tries, more where the set is large: until the context would hold a sixteenth of it)
|
|
4127
|
+
const tries = Math.max(joinAttempts, Math.ceil(whole.length / 16 / joinAdd));
|
|
4128
|
+
for (let attempt = 0; attempt <= tries; attempt++) {
|
|
4129
|
+
if (attempt === tries && ctx !== whole) {
|
|
2366
4130
|
const pf = provenFirst();
|
|
2367
4131
|
if (pf) {
|
|
2368
4132
|
const out = stepWith(todo, todoForm, pf);
|
|
@@ -2388,7 +4152,7 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
2388
4152
|
if (ctx === whole)
|
|
2389
4153
|
break;
|
|
2390
4154
|
ctx = whole;
|
|
2391
|
-
attempt =
|
|
4155
|
+
attempt = tries - 1;
|
|
2392
4156
|
continue;
|
|
2393
4157
|
}
|
|
2394
4158
|
const w = out.witness, { params: pp0, st: st0 } = stateOf(w), pre0 = pointOf(pp0, st0);
|
|
@@ -2417,7 +4181,7 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
2417
4181
|
const w = witness, { params, st } = stateOf(w);
|
|
2418
4182
|
if (todoForm?.nevers)
|
|
2419
4183
|
neversBroken++;
|
|
2420
|
-
const input =
|
|
4184
|
+
const input = requestOf(w);
|
|
2421
4185
|
let post;
|
|
2422
4186
|
try {
|
|
2423
4187
|
post = run.step(params, st, input);
|
|
@@ -2563,7 +4327,10 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
2563
4327
|
}
|
|
2564
4328
|
opts.progress?.('join', { round: rounds, set: set.length, ms: qms, goal: todo.slice(0, 60), joins: joins - j0, walked, dropped: dropped - d0 });
|
|
2565
4329
|
}
|
|
2566
|
-
|
|
4330
|
+
// (a rule whose scalars the runs held at a few values each: no counter, the engine's shape)
|
|
4331
|
+
if (engineFollows() && why !== 'inductive')
|
|
4332
|
+
engine();
|
|
4333
|
+
opts.progress?.('join end', { rounds, set: set.length, why, cone: cone.length, ms: Math.round(performance.now() - jT0), ...meters() });
|
|
2567
4334
|
// (unproven, the search below begins as it would have: the candidates and the counterexamples as they were)
|
|
2568
4335
|
if (why === 'inductive')
|
|
2569
4336
|
chosen = cone;
|
|
@@ -2618,7 +4385,9 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
2618
4385
|
};
|
|
2619
4386
|
// A cell form's mirror, −Σ ≤ −d beside Σ ≤ d under the same guard (its text).
|
|
2620
4387
|
const mirrorText = (f) => (!f.cells || f.mod || f.cong || f.never || f.nevers || f.cube || f.unreachable || f.also || !f.coef.length ? undefined : textOf({ ...f, coef: f.coef.map(([i, c]) => [i, -c]), d: -f.d }));
|
|
2621
|
-
|
|
4388
|
+
if (enumProof)
|
|
4389
|
+
last = enumProof;
|
|
4390
|
+
for (let iter = 0; iter < 300 && !enumProof && now() < until && !stop(); iter++) {
|
|
2622
4391
|
const began = now(), wall = performance.now();
|
|
2623
4392
|
// (each law proven alone, then the set as a whole: the same goals, grown once)
|
|
2624
4393
|
last = withTrees(() => search([...given, ...chosen.map(lawFor)]));
|
|
@@ -2691,8 +4460,8 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
2691
4460
|
trace('end', { proven: false, stopped: stop() });
|
|
2692
4461
|
return { ...last, found: [], stats: stats(samples.length, forms.length, alive.length) };
|
|
2693
4462
|
}
|
|
2694
|
-
// Take out what the proof doesn't need, the last added first.
|
|
2695
|
-
for (const f of opts.minimize === false ? [] : [...chosen].reverse()) {
|
|
4463
|
+
// Take out what the proof doesn't need, the last added first (not the engine's set: Houdini's, each cube minimal).
|
|
4464
|
+
for (const f of opts.minimize === false || enumProof ? [] : [...chosen].reverse()) {
|
|
2696
4465
|
if (now() > until || stop())
|
|
2697
4466
|
break;
|
|
2698
4467
|
const without = chosen.filter(g => g !== f), v = prove([...laws, ...without.map(lawFor)]);
|
|
@@ -2703,5 +4472,5 @@ function inferLawsSearch(target, laws, stepLaws = [], opts = {}) {
|
|
|
2703
4472
|
if (kept.length)
|
|
2704
4473
|
rule?.dividesBy(...kept);
|
|
2705
4474
|
trace('end', { proven: true, stopped: stop(), found: chosen.map(f => textOf(f)).join('; ') });
|
|
2706
|
-
return { ...last, found: chosen.map(f => { const law = lawFor(f), text = law.name, source = sourceOf(f, text); return { law, text, ...(source ? { source } : {}) }; }), stats: stats(samples.length, forms.length, alive.length) };
|
|
4475
|
+
return { ...last, found: [...chosen.map(f => { const law = lawFor(f), text = law.name, source = sourceOf(f, text); return { law, text, ...(source ? { source } : {}) }; }), ...(enumProof ? enumSome : []).map(law => ({ law, text: law.name }))], stats: stats(samples.length, forms.length, alive.length) };
|
|
2707
4476
|
}
|