gdp-wired 0.44.0 → 0.46.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 +58 -7
- package/dist/core/bitsets.d.ts +2 -0
- package/dist/core/bitsets.js +23 -0
- package/dist/core/bmc.js +6 -2
- package/dist/core/c/reader.d.ts +9 -1
- package/dist/core/c/reader.js +445 -38
- package/dist/core/calendar.d.ts +29 -0
- package/dist/core/calendar.js +82 -0
- package/dist/core/check.js +3 -2
- package/dist/core/cli.d.ts +4 -0
- package/dist/core/cli.js +335 -39
- package/dist/core/compose.js +3 -1
- package/dist/core/counts.d.ts +5 -2
- package/dist/core/counts.js +335 -88
- package/dist/core/cycles.js +8 -1
- package/dist/core/cyclic.d.ts +1 -0
- package/dist/core/cyclic.js +13 -7
- package/dist/core/data-synth.d.ts +3 -0
- package/dist/core/data-synth.js +252 -0
- package/dist/core/data.d.ts +8 -2
- package/dist/core/data.js +1113 -172
- package/dist/core/discover.js +3 -1
- package/dist/core/draft.d.ts +19 -0
- package/dist/core/draft.js +53 -0
- package/dist/core/dsl.d.ts +39 -3
- package/dist/core/dsl.js +250 -31
- package/dist/core/effect-prims.js +2 -2
- package/dist/core/effect.d.ts +1 -4
- package/dist/core/effect.js +1 -15
- package/dist/core/ex/reader.exs +17 -1
- package/dist/core/exists.d.ts +16 -1
- package/dist/core/exists.js +447 -50
- package/dist/core/extract-c.d.ts +2 -0
- package/dist/core/extract-c.js +185 -31
- package/dist/core/extract-check.d.ts +7 -0
- package/dist/core/extract-check.js +115 -3
- package/dist/core/extract-ex.js +3 -3
- package/dist/core/extract-go-check.d.ts +1 -1
- package/dist/core/extract-go-check.js +21 -13
- package/dist/core/extract-go.d.ts +1 -0
- package/dist/core/extract-go.js +58 -9
- package/dist/core/extract-ir.d.ts +1 -0
- package/dist/core/extract-ir.js +15 -0
- package/dist/core/extract-jvm.d.ts +1 -1
- package/dist/core/extract-jvm.js +65 -14
- package/dist/core/extract-orm.d.ts +12 -0
- package/dist/core/extract-orm.js +244 -0
- package/dist/core/extract-print.js +23 -1
- package/dist/core/extract-py.d.ts +1 -0
- package/dist/core/extract-py.js +4 -4
- package/dist/core/extract-rb.js +3 -3
- package/dist/core/extract-read.d.ts +25 -0
- package/dist/core/extract-read.js +60 -5
- package/dist/core/extract-rs.js +22 -20
- package/dist/core/extract-shapes.js +22 -0
- package/dist/core/extract-sol.d.ts +10 -1
- package/dist/core/extract-sol.js +89 -30
- package/dist/core/extract-step.d.ts +201 -4
- package/dist/core/extract-step.js +1514 -111
- package/dist/core/extract-swift.js +24 -13
- package/dist/core/extract.d.ts +9 -0
- package/dist/core/extract.js +88 -7
- package/dist/core/fiber-run.d.ts +5 -0
- package/dist/core/fiber-run.js +18 -0
- package/dist/core/fresh.d.ts +2 -2
- package/dist/core/fresh.js +7 -3
- package/dist/core/go/reader.go +7 -1
- package/dist/core/infer.d.ts +4 -1
- package/dist/core/infer.js +1020 -246
- package/dist/core/jvm/JavaReader.java +33 -8
- package/dist/core/jvm/Jvm.java +20 -1
- package/dist/core/jvm/KtReader.java +59 -6
- package/dist/core/laws.d.ts +10 -3
- package/dist/core/laws.js +193 -14
- package/dist/core/machine.d.ts +16 -3
- package/dist/core/machine.js +212 -28
- package/dist/core/nonterm.js +14 -1
- package/dist/core/orm/harness.js +61 -0
- package/dist/core/orm-fixtures/schema.prisma +5 -0
- package/dist/core/py/harness.py +260 -21
- package/dist/core/py/reader.py +278 -39
- package/dist/core/rb/reader.rb +52 -5
- package/dist/core/reader-fixtures/README.md +28 -0
- package/dist/core/reader-fixtures/c-693042.cpp +8 -0
- package/dist/core/reader-fixtures/go-685506.go +14 -0
- package/dist/core/reader-fixtures/jvm-724362.kt +8 -0
- package/dist/core/reader-fixtures/jvm-737430.kt +7 -0
- package/dist/core/reader-fixtures/jvm-765282.kt +3 -0
- package/dist/core/reader-fixtures/swift-735726.swift +15 -0
- package/dist/core/reader-fixtures/swift-735990.swift +15 -0
- package/dist/core/reader-fixtures/swift-739158.swift +4 -0
- package/dist/core/reader-fixtures/swift-744834.swift +15 -0
- package/dist/core/reader-fixtures/swift-746946.swift +31 -0
- package/dist/core/reader-fixtures/typescript-673062.ts.txt +2 -0
- package/dist/core/relation.d.ts +1 -1
- package/dist/core/relation.js +29 -2
- package/dist/core/relational.js +4 -2
- package/dist/core/rs/src/main.rs +18 -2
- package/dist/core/sample-cache.d.ts +8 -0
- package/dist/core/sample-cache.js +46 -0
- package/dist/core/security.d.ts +38 -0
- package/dist/core/security.js +88 -0
- package/dist/core/sol/harness.py +63 -7
- package/dist/core/sol-concrete.d.ts +23 -0
- package/dist/core/sol-concrete.js +195 -0
- package/dist/core/solver.js +8 -5
- package/dist/core/swift/Sources/gdp-swiftreader/main.swift +50 -7
- package/dist/core/system.d.ts +62 -1
- package/dist/core/system.js +263 -4
- package/dist/core/transfer.js +39 -9
- package/dist/core/translate.js +39 -10
- package/dist/core/watch.js +51 -6
- package/dist/interchange/check.js +10 -10
- package/dist/interchange/format.d.ts +4 -0
- package/dist/interchange/format.js +24 -6
- package/dist/kernel/cdcl.d.ts +5 -0
- package/dist/kernel/cdcl.js +88 -10
- package/dist/kernel/epr.js +141 -29
- package/dist/kernel/kernel.d.ts +31 -1
- package/dist/kernel/kernel.js +391 -101
- package/dist/kernel/model-sequence.d.ts +3 -0
- package/dist/kernel/model-sequence.js +34 -0
- package/dist/kernel/prove.d.ts +30 -1
- package/dist/kernel/prove.js +481 -71
- package/dist/kernel/reader.d.ts +3 -0
- package/dist/kernel/reader.js +101 -14
- package/dist/kernel/share.d.ts +1 -0
- package/dist/kernel/share.js +27 -0
- package/dist/lean-kernel/emit.d.ts +9 -3
- package/dist/lean-kernel/emit.js +200 -66
- package/dist/lean-kernel/lean-terms.js +5 -0
- package/dist/lean-kernel/library.js +10 -10
- package/package.json +7 -2
package/dist/kernel/prove.js
CHANGED
|
@@ -8,14 +8,15 @@
|
|
|
8
8
|
// - a refutation of a split's yes side that never reads the split refutes its parent (lift), and the no side is skipped;
|
|
9
9
|
// - a hypothesis' rows and open atoms, and every term inside them, are reused from an earlier path that decided the
|
|
10
10
|
// same atoms they read (kernel.ts's reusing), so a new decision reads again only what it touches.
|
|
11
|
-
import { evalC, evalE, walkOnce } from "../core/machine.js";
|
|
11
|
+
import { cpair, evalC, evalE, walkOnce } from "../core/machine.js";
|
|
12
12
|
import { add, costs, env, k, refuteFast, scale, solve, sub, v, work } from "../core/solver.js";
|
|
13
13
|
import { checkRefutation } from "../core/check.js";
|
|
14
14
|
import { allowNode, allowTranslated, certCyclic, certOn, certSearch, stepCertFloor, translateMs, closingInsts, crossCyclic, cyclicOn, cycLog, hasQuantified, loopNodes, machineNodes, proveCyclic, reqTS, searchedStepNodes, skMachine, timedKind, withWitness, translatedProof, uncovered, uncoveredWhy } from "../core/cyclic.js";
|
|
15
15
|
import { proveSome, someLaws } from "../core/exists.js";
|
|
16
|
-
import { growCdcl } from "./cdcl.js";
|
|
17
|
-
import { decide, pastEprCap, patternsOf } from "./epr.js";
|
|
18
|
-
import {
|
|
16
|
+
import { REFUTED, growCdcl } from "./cdcl.js";
|
|
17
|
+
import { decide, eprMeter, pastEprCap, patternsOf, withEprCap } from "./epr.js";
|
|
18
|
+
import { projected, tupleParts } from "./kernel.js";
|
|
19
|
+
import { assumed, atomOf, boundOf, canon, canonFlip, cellParts, columnTerm, decided, factorsOf, isCell, isMonomial, stepZone, check, checkLaws, checkStepLaws, historyMachine, plainOf, substC, substE, leafConstraints, leafConstraintsReusing, reusing, linear, livenessHelpful, livenessParts, strongly, loopMachine, obligations, plainKey, progressObligations, stepLawObligations, trueC, truth, unreachable, witnessesOf, withProducts } from "./kernel.js";
|
|
19
20
|
// The atoms the path leaves open, in evaluation order: deciding any of them makes progress.
|
|
20
21
|
function openE(e, path, out) {
|
|
21
22
|
switch (e.op) {
|
|
@@ -24,6 +25,8 @@ function openE(e, path, out) {
|
|
|
24
25
|
case 'mul': return openE(e.a, path, out);
|
|
25
26
|
case 'prod': return (openE(e.a, path, out), openE(e.b, path, out));
|
|
26
27
|
case 'get': return openE(e.key, path, out);
|
|
28
|
+
case 'tup': return (openE(e.a, path, out), openE(e.b, path, out));
|
|
29
|
+
case 'proj': return openE(e.a.op === 'tup' ? (e.k === 0 ? e.a.a : e.a.b) : e.a, path, out);
|
|
27
30
|
case 'if': {
|
|
28
31
|
const t = truth(e.c, path);
|
|
29
32
|
if (t === undefined)
|
|
@@ -54,6 +57,9 @@ function openC(c, path, out) {
|
|
|
54
57
|
return openC(c.x, path, out);
|
|
55
58
|
if (c.op === 'and' || c.op === 'or')
|
|
56
59
|
return c.xs.forEach(y => openC(y, path, out));
|
|
60
|
+
// (two tuples equal: their parts' equalities, the kernel's reading)
|
|
61
|
+
if (c.op === '=' && c.a.op === 'tup' && c.b.op === 'tup')
|
|
62
|
+
return [c.a.a, c.a.b].forEach((x, i) => openC({ op: '=', a: x, b: i ? c.b.b : c.b.a }, path, out));
|
|
57
63
|
const a = linear(c.a, path), b = linear(c.b, path), own = [];
|
|
58
64
|
if (a && b) {
|
|
59
65
|
const d = sub(a, b);
|
|
@@ -87,6 +93,10 @@ function openH(c, path, out, positive = true) {
|
|
|
87
93
|
}
|
|
88
94
|
if (c.op === 'flag' || c.op === 'all')
|
|
89
95
|
return;
|
|
96
|
+
if (c.op === '=' && c.a.op === 'tup' && c.b.op === 'tup') {
|
|
97
|
+
const parts = [{ op: '=', a: c.a.a, b: c.b.a }, { op: '=', a: c.a.b, b: c.b.b }];
|
|
98
|
+
return openH(positive ? { op: 'and', xs: parts } : { op: 'or', xs: parts.map((x) => ({ op: 'not', x })) }, path, out, true);
|
|
99
|
+
}
|
|
90
100
|
const before = out.length;
|
|
91
101
|
openE(c.a, path, out), openE(c.b, path, out);
|
|
92
102
|
if (out.length === before && !positive && c.op === '=' && truth(c, path) === undefined)
|
|
@@ -112,7 +122,7 @@ export const cellPair = (a, b) => {
|
|
|
112
122
|
cellPairsN = 1;
|
|
113
123
|
cellPairs.set(a, (row = new Map()));
|
|
114
124
|
}
|
|
115
|
-
const d = sub(cellKey(a).key, cellKey(b).key);
|
|
125
|
+
const d = sub(cellKey(a).key ?? {}, cellKey(b).key ?? {});
|
|
116
126
|
row.set(b, (pair = { d, neg: scale(d, -1) }));
|
|
117
127
|
}
|
|
118
128
|
return pair;
|
|
@@ -120,8 +130,39 @@ export const cellPair = (a, b) => {
|
|
|
120
130
|
const cellKey = (n) => { let c = cellKeys.get(n); if (!c) {
|
|
121
131
|
if (cellKeys.size > 100_000)
|
|
122
132
|
cellKeys.clear();
|
|
123
|
-
cellKeys.set(n, (c =
|
|
133
|
+
cellKeys.set(n, (c = cellParts(n)));
|
|
124
134
|
} return c; };
|
|
135
|
+
// Two tuple cells of one map: their parts compared one at a time, the first the path leaves open (each way), none
|
|
136
|
+
// once a part is decided apart. (Each part's difference made once per pair, as a plain key's.)
|
|
137
|
+
const tuplePairs = new Map();
|
|
138
|
+
// Two cells' parts as pairs of differences: [] for two keys of different shapes (never equal), undefined for plain keys.
|
|
139
|
+
export const tupleCellPairs = (a, b) => {
|
|
140
|
+
const ka = cellKey(a).keys, kb = cellKey(b).keys;
|
|
141
|
+
if (!ka && !kb)
|
|
142
|
+
return undefined;
|
|
143
|
+
if (!ka || !kb || ka.length !== kb.length)
|
|
144
|
+
return [];
|
|
145
|
+
const id = `${a} ${b}`;
|
|
146
|
+
let ds = tuplePairs.get(id);
|
|
147
|
+
if (!ds) {
|
|
148
|
+
if (tuplePairs.size > 200_000)
|
|
149
|
+
tuplePairs.clear();
|
|
150
|
+
tuplePairs.set(id, (ds = ka.map((k, i) => { const d = sub(k, kb[i]); return [d, scale(d, -1)]; })));
|
|
151
|
+
}
|
|
152
|
+
return ds;
|
|
153
|
+
};
|
|
154
|
+
const tupleOpen = (a, b, _ka, _kb, dec) => {
|
|
155
|
+
for (const [d, neg] of tupleCellPairs(a, b)) {
|
|
156
|
+
const x = dec(d), y = dec(neg);
|
|
157
|
+
if (x === undefined)
|
|
158
|
+
return d;
|
|
159
|
+
if (x === true && y === undefined)
|
|
160
|
+
return neg;
|
|
161
|
+
if (!(x && y))
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
return undefined;
|
|
165
|
+
};
|
|
125
166
|
function openCells(rows, path) {
|
|
126
167
|
const seen = new Set(), names = [];
|
|
127
168
|
for (const r of rows)
|
|
@@ -138,6 +179,12 @@ function openCells(rows, path) {
|
|
|
138
179
|
for (let j = i + 1; j < names.length; j++) {
|
|
139
180
|
if (cs[i].map !== cs[j].map)
|
|
140
181
|
continue;
|
|
182
|
+
if (cs[i].keys || cs[j].keys) {
|
|
183
|
+
const t = cs[i].keys && cs[j].keys ? tupleOpen(names[i], names[j], cs[i].keys, cs[j].keys, dec) : undefined;
|
|
184
|
+
if (t)
|
|
185
|
+
out.push(t);
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
141
188
|
const pair = cellPair(names[i], names[j]), d = pair.d;
|
|
142
189
|
if (pairedKeys.skip && Object.keys(d).some(isMonomial))
|
|
143
190
|
continue;
|
|
@@ -241,7 +288,10 @@ export const cellProducts = { on: false };
|
|
|
241
288
|
// clock inside `counted` and in the cost model's work outside it (`withWork`); past either the goal is left `unknown`. A goal with many instances (a law about two keys at its pairs) has that many more rows per leaf.
|
|
242
289
|
// A deadline every goal grown inside `withDeadline` keeps, beside its own budget: a search that proves many things (core/
|
|
243
290
|
// infer.ts, core/discover.ts, the search proveRule falls back on) bounds them all; a goal past it is left `unknown`.
|
|
244
|
-
let deadline = Infinity;
|
|
291
|
+
let deadline = Infinity, deadlineEpr = 0;
|
|
292
|
+
// EPR has its own work counter. Debit it from a counted deadline, not from clock(): the protocol engine already
|
|
293
|
+
// accounts for EPR in its own clock. A scope's anchor charges all its queries, including nested and failed ones.
|
|
294
|
+
const eprTimeSince = (start) => rate ? (eprMeter.n - start) / rate : 0;
|
|
245
295
|
// The clock every budget here reads (grow's `until`, withDeadline's; core/cyclic.ts's and core/data.ts's searches): wall
|
|
246
296
|
// time, or within `counted` the work the prover has done (the solver's rows read and made, each node's rows), `perMs`
|
|
247
297
|
// units a millisecond, so that what a search's budget lets it try, and so its verdict, doesn't depend on the machine's
|
|
@@ -311,15 +361,17 @@ const ranOut = () => pastWall() || withinDeadline(Infinity) <= clock();
|
|
|
311
361
|
export const wallAt = () => wallEnd;
|
|
312
362
|
export const late = (until) => clock() > until || (workCap !== Infinity && fullWork() > workCap) || pastWall();
|
|
313
363
|
// A budget's end, within the deadline (a translation's own bound, core/cyclic.ts's, kept by the export's GDP_CERT_MS).
|
|
314
|
-
export const withinDeadline = (until) => Math.min(until, deadline);
|
|
364
|
+
export const withinDeadline = (until) => Math.min(until, deadline - eprTimeSince(deadlineEpr));
|
|
315
365
|
export function withDeadline(until, f) {
|
|
316
|
-
const outer = deadline;
|
|
317
|
-
deadline =
|
|
366
|
+
const outer = deadline, outerEpr = deadlineEpr;
|
|
367
|
+
deadline = withinDeadline(until);
|
|
368
|
+
deadlineEpr = eprMeter.n;
|
|
318
369
|
try {
|
|
319
370
|
return f();
|
|
320
371
|
}
|
|
321
372
|
finally {
|
|
322
373
|
deadline = outer;
|
|
374
|
+
deadlineEpr = outerEpr;
|
|
323
375
|
}
|
|
324
376
|
}
|
|
325
377
|
// `units: false`: the goal's own order, no units first (a caller after a model, not a proof: a run guided by the
|
|
@@ -333,8 +385,9 @@ export function grow(goal, path = new Map(), budget) {
|
|
|
333
385
|
return withWork(fullWork() + groundRate * ms, () => grow(goal, path, { nodes: 0 }));
|
|
334
386
|
budget = { nodes: 0, until: clock() + ms };
|
|
335
387
|
}
|
|
336
|
-
|
|
337
|
-
|
|
388
|
+
const end = withinDeadline(budget.until ?? Infinity);
|
|
389
|
+
if (end < (budget.until ?? Infinity))
|
|
390
|
+
budget.until = end;
|
|
338
391
|
if (budget.units === false && unitsNow) {
|
|
339
392
|
unitsNow = false;
|
|
340
393
|
try {
|
|
@@ -441,9 +494,13 @@ const withPatterns = (m, laws, f) => {
|
|
|
441
494
|
// (`refuted`: the last decision refuted the goal and its core's proof didn't finish: a model from fewer instances, asked
|
|
442
495
|
// before, is then no answer, as the abstraction refutes it at these)
|
|
443
496
|
const eprLast = { refuted: false };
|
|
444
|
-
function eprFirst(goal, budget, whole = true,
|
|
497
|
+
function eprFirst(goal, budget, whole = true, stop = () => pastWall() || pastEprCap()) {
|
|
445
498
|
eprLast.refuted = false;
|
|
446
|
-
const
|
|
499
|
+
const start = eprMeter.n, until = budget.until ?? Infinity;
|
|
500
|
+
const d = decide(goal, eprScope.patterns, { exact: true, late: () => stop() || late(withinDeadline(until - eprTimeSince(start))) });
|
|
501
|
+
// The local goal's remaining allowance also reaches the core proof or fallback search.
|
|
502
|
+
if (budget.until !== undefined)
|
|
503
|
+
budget.until = until - eprTimeSince(start);
|
|
447
504
|
if ('unknown' in d)
|
|
448
505
|
return undefined;
|
|
449
506
|
if (d.sat)
|
|
@@ -452,7 +509,8 @@ function eprFirst(goal, budget, whole = true, late = () => pastWall() || pastEpr
|
|
|
452
509
|
const now = clock(), end = withinDeadline(budget.until ?? Infinity), w = fullWork();
|
|
453
510
|
eprScope.busy = true;
|
|
454
511
|
try {
|
|
455
|
-
const
|
|
512
|
+
const share = whole ? 0.5 : 1;
|
|
513
|
+
const out = withWork(workCap === Infinity ? Infinity : w + share * (workCap - w), () => grow(part, new Map(), { nodes: 0, until: end === Infinity ? end : now + share * (end - now) }));
|
|
456
514
|
if (!('tree' in out)) {
|
|
457
515
|
eprLast.refuted = true;
|
|
458
516
|
return undefined;
|
|
@@ -466,9 +524,168 @@ function eprFirst(goal, budget, whole = true, late = () => pastWall() || pastEpr
|
|
|
466
524
|
eprScope.busy = false;
|
|
467
525
|
}
|
|
468
526
|
}
|
|
527
|
+
// Wide goals (GDP_WIDE hypotheses or more, a law at every tuple of a goal's keys, a step's every write, and
|
|
528
|
+
// GDP_WIDE_TERMS terms): each node of the search reads every hypothesis' rows, so a goal of a thousand reaches neither a
|
|
529
|
+
// tree nor a model within its budget, though its refutation reads one or two and a model of a few is often a model of
|
|
530
|
+
// all. Grown first over a part: the conclusion, then the hypotheses a model of the part breaks (the model read as a
|
|
531
|
+
// state: each cell at its key's value, any other name 0), those sharing a cell with the part first, the part doubled a
|
|
532
|
+
// round. A model that breaks none is the goal's (every hypothesis holds at it); a tree is widened to the goal (`widen`),
|
|
533
|
+
// which the kernel checks as any; a model-only query's refutation is the part's, its core the part's hypotheses. Within
|
|
534
|
+
// a share of the goal's budget, each round within what the whole's search would spend on as many nodes; anything else
|
|
535
|
+
// leaves the goal as before. Untrusted.
|
|
536
|
+
const wideAt = Number(env.GDP_WIDE ?? 256), wideTerms = Number(env.GDP_WIDE_TERMS ?? 40_000), wideShare = Number(env.GDP_WIDE_SHARE ?? 0.25), wideTrace = !!env.GDP_WIDE_TRACE;
|
|
537
|
+
let wideBusy = false;
|
|
538
|
+
// (while a part is grown: whether a point holds every hypothesis of the part, read as a state; a node's point that does is
|
|
539
|
+
// the part's model, whatever atoms are still open, and the whole's if it breaks none of the rest)
|
|
540
|
+
export const wideScope = {};
|
|
541
|
+
export const wideStats = { goals: 0, trees: 0, models: 0, rounds: 0 };
|
|
542
|
+
export const wideHook = {};
|
|
543
|
+
const cellsIn = (c) => { const out = new Set(); walkOnce(c, o => { const x = o; if (x.op === 'get')
|
|
544
|
+
out.add(`${x.map}${JSON.stringify(x.key)}`); }); return out; };
|
|
545
|
+
// (the hypotheses a model breaks among `ids` (all where none are given), the model read as a state, the conclusion first;
|
|
546
|
+
// `first`: the first one broken alone)
|
|
547
|
+
// (`defaults`: a map's cells the model leaves out read at that value, not the map's start)
|
|
548
|
+
export const brokenOf = (goal) => {
|
|
549
|
+
const maps = new Set();
|
|
550
|
+
walkOnce(goal.hyps, o => { const x = o; if (x.op === 'get' && typeof x.map === 'string')
|
|
551
|
+
maps.add(x.map); });
|
|
552
|
+
const mm = { maps: Object.fromEntries([...maps].map(mp => [mp, 0])) };
|
|
553
|
+
return (w0, ids = goal.hyps.map((_, i) => i), first = false, defaults) => {
|
|
554
|
+
// (a term column's value is its term's at the point, not the point's own where its choice is open: each cell is put
|
|
555
|
+
// at its key's value with the columns so read, twice, as a column's term may read a cell)
|
|
556
|
+
const w = { ...w0 }, at = new Proxy(w, { get: (t, q) => t[q] ?? 0 }), out = [];
|
|
557
|
+
let st = { scalars: {}, maps: {}, ...(defaults ? { defaults } : {}) };
|
|
558
|
+
for (let pass = 0; pass < 3; pass++) {
|
|
559
|
+
if (pass)
|
|
560
|
+
for (const n in w) {
|
|
561
|
+
const e = n.startsWith('⟦') ? columnTerm(n) : undefined;
|
|
562
|
+
if (e)
|
|
563
|
+
try {
|
|
564
|
+
w[n] = evalE(e, at, st, mm);
|
|
565
|
+
}
|
|
566
|
+
catch { /* left */ }
|
|
567
|
+
}
|
|
568
|
+
st = { scalars: {}, maps: Object.fromEntries([...maps].map(mp => [mp, new Map()])), ...(defaults ? { defaults } : {}) };
|
|
569
|
+
for (const [cell, value] of Object.entries(w)) {
|
|
570
|
+
const br = cell.indexOf('['), key = br > 0 && cell.endsWith(']') && st.maps[cell.slice(0, br)] ? keyValue(cell, w) : undefined;
|
|
571
|
+
if (key !== undefined)
|
|
572
|
+
st.maps[cell.slice(0, br)].set(key, value);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
for (let q = ids.length - 1; q >= 0 && !(first && out.length); q--) {
|
|
576
|
+
const i = ids[q];
|
|
577
|
+
let ok = false;
|
|
578
|
+
try {
|
|
579
|
+
ok = evalC(goal.hyps[i], at, st, mm);
|
|
580
|
+
}
|
|
581
|
+
catch { /* broken */ }
|
|
582
|
+
if (!ok)
|
|
583
|
+
out.push(i);
|
|
584
|
+
}
|
|
585
|
+
return out.reverse();
|
|
586
|
+
};
|
|
587
|
+
};
|
|
588
|
+
// (`ask`: how a part is asked, a tree wanted by default; kernel/cdcl.ts's model-only queries ask for a model or a
|
|
589
|
+
// refutation, whose core is the part's hypotheses, the whole's by index)
|
|
590
|
+
// (and its terms, each shared part once, past GDP_WIDE_TERMS: what each node of the whole goal's search reads, where the
|
|
591
|
+
// search is costly; under it the whole is asked as before, its model the search's own, which the engine's join reads)
|
|
592
|
+
const termsSeen = new WeakMap();
|
|
593
|
+
const termsOf = (goal) => { let n = termsSeen.get(goal); if (n === undefined) {
|
|
594
|
+
n = 0;
|
|
595
|
+
walkOnce(goal.hyps, () => { n++; });
|
|
596
|
+
termsSeen.set(goal, n);
|
|
597
|
+
} return n; };
|
|
598
|
+
export const wideFor = (goal) => !wideBusy && goal.hyps.length >= wideAt && termsOf(goal) >= wideTerms;
|
|
599
|
+
// (`keep`: a part's tree kept as the part's, with the part and its hypotheses' indices in the whole as its core, not
|
|
600
|
+
// widened: settle's, whose final proof is made over the instances its proofs read)
|
|
601
|
+
export function growWide(goal, budget, ask = (g, until) => grow(g, new Map(), { nodes: 0, until }), keep = false) {
|
|
602
|
+
const hyps = goal.hyps, last = hyps.length - 1, broken = brokenOf(goal), now = clock(), w = fullWork();
|
|
603
|
+
const end = withinDeadline(budget.until ?? Infinity), span = wideShare * (end - now), room = wideShare * (workCap - w), until = now + span;
|
|
604
|
+
const part = new Set([last]), near = new Set(cellsIn(hyps[last])), cells = new Map();
|
|
605
|
+
const cellsAt = (i) => { let c = cells.get(i); if (!c)
|
|
606
|
+
cells.set(i, (c = cellsIn(hyps[i]))); return c; };
|
|
607
|
+
const take = (i) => { part.add(i); for (const x of cellsAt(i))
|
|
608
|
+
near.add(x); };
|
|
609
|
+
wideBusy = true;
|
|
610
|
+
wideStats.goals++;
|
|
611
|
+
try {
|
|
612
|
+
while (!late(until)) {
|
|
613
|
+
wideStats.rounds++;
|
|
614
|
+
// (a part's own budget: what the whole's search spends on as many nodes, four times over (a node of the part reads
|
|
615
|
+
// that much less), at least a sixteenth of the share: a part that a small budget doesn't settle leaves the goal)
|
|
616
|
+
const ids = [...part].sort((a, b) => a - b), g = { name: goal.name, hyps: ids.map(i => hyps[i]) }, f = Math.max(1 / 16, Math.min(1, 4 * part.size / hyps.length));
|
|
617
|
+
const t0 = performance.now(), at = clock();
|
|
618
|
+
wideScope.judge = w => !broken(w, ids, true).length;
|
|
619
|
+
let out;
|
|
620
|
+
try {
|
|
621
|
+
out = withWork(Math.min(w + room, fullWork() + f * room), () => ask(g, Math.min(until, at + f * span)));
|
|
622
|
+
}
|
|
623
|
+
finally {
|
|
624
|
+
wideScope.judge = undefined;
|
|
625
|
+
}
|
|
626
|
+
wideHook.on?.(g, out);
|
|
627
|
+
if (wideTrace)
|
|
628
|
+
console.error(`[wide] ${goal.name.slice(0, 40)}: part ${g.hyps.length} of ${hyps.length}, ${'tree' in out ? 'tree' : out.stuck} in ${Math.round(performance.now() - t0)} ms${'tree' in out ? '' : ` ${out.why?.slice(0, 50) ?? ''}`}`);
|
|
629
|
+
if ('tree' in out && keep) {
|
|
630
|
+
wideStats.trees++;
|
|
631
|
+
return { tree: out.tree, nodes: out.nodes, part: g, core: ids };
|
|
632
|
+
}
|
|
633
|
+
if ('tree' in out) {
|
|
634
|
+
const tree = withWork(w + room, () => withDeadline(until, () => widen(g, goal, out.tree)));
|
|
635
|
+
if (tree)
|
|
636
|
+
wideStats.trees++;
|
|
637
|
+
return tree && { tree, nodes: out.nodes };
|
|
638
|
+
}
|
|
639
|
+
if (out.why === REFUTED)
|
|
640
|
+
return { ...out, ...(out.core ? { core: out.core.map(i => ids[i]) } : {}) };
|
|
641
|
+
if (out.stuck !== 'sat' || !out.witness)
|
|
642
|
+
return undefined;
|
|
643
|
+
const bad = broken(out.witness);
|
|
644
|
+
if (wideTrace)
|
|
645
|
+
console.error(`[wide] breaks ${bad.length}${bad.length ? `: ${bad.slice(0, 12).join(' ')}` : ''}`);
|
|
646
|
+
if (!bad.length) {
|
|
647
|
+
wideStats.models++;
|
|
648
|
+
return { ...out, path: new Map() };
|
|
649
|
+
}
|
|
650
|
+
const more = bad.filter(i => !part.has(i)), size = part.size;
|
|
651
|
+
if (!more.length || 2 * size > hyps.length)
|
|
652
|
+
return undefined;
|
|
653
|
+
// (what the model breaks, those sharing a cell with the part first; then, to twice the part, the hypotheses that share
|
|
654
|
+
// one, then any, in the goal's order: the rounds are as many as the doublings to the part that settles the goal)
|
|
655
|
+
const shared = (i) => [...cellsAt(i)].some(x => near.has(x));
|
|
656
|
+
more.sort((a, b) => Number(shared(b)) - Number(shared(a)));
|
|
657
|
+
for (const i of more.slice(0, Math.max(16, size)))
|
|
658
|
+
take(i);
|
|
659
|
+
for (const any of [false, true])
|
|
660
|
+
for (let i = 0; i < last && part.size < 2 * size; i++)
|
|
661
|
+
if (!part.has(i) && (any || shared(i)))
|
|
662
|
+
take(i);
|
|
663
|
+
}
|
|
664
|
+
return undefined;
|
|
665
|
+
}
|
|
666
|
+
finally {
|
|
667
|
+
wideBusy = false;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
// (the abstraction of a whole wide goal, quadratic in its cells, within GDP_WIDE_EPR units: i4's learning switch's
|
|
671
|
+
// largest decision otherwise 0.47M, its engine set's step goal of 7,590 hypotheses 19M, past its caller's end, in 10 GB)
|
|
672
|
+
const wideEprUnits = Number(env.GDP_WIDE_EPR ?? 2_000_000), wideParts = env.GDP_WIDE_PARTS !== '0';
|
|
673
|
+
// (only where the rule has tuple keys, its patterns n zeros: radix-keyed rules keep main's path, consensus_wo_decide's
|
|
674
|
+
// and ring_leader_election's proofs among them, lost to it at 60 s)
|
|
675
|
+
const tupledScope = () => !!eprScope.patterns && Object.values(eprScope.patterns).some(c => c.length > 1 && c[0] === 0);
|
|
676
|
+
// (a part of a wide goal by the EPR path first, its core widened to the part, else the usual search)
|
|
677
|
+
const eprPart = (gp, until) => eprFirst(gp, { nodes: 0, until }) ?? grow(gp, new Map(), { nodes: 0, until });
|
|
469
678
|
function growRoot(goal, path, budget) {
|
|
470
|
-
|
|
471
|
-
|
|
679
|
+
const wide = !path.size && !budget.nodes && wideFor(goal), epr = !!eprScope.patterns && !eprScope.busy;
|
|
680
|
+
// (a wide goal's parts by the EPR path too, where the goal is EPR-shaped)
|
|
681
|
+
if (wide) {
|
|
682
|
+
const out = epr && wideParts && tupledScope() ? growWide(goal, budget, eprPart) : growWide(goal, budget);
|
|
683
|
+
if (out)
|
|
684
|
+
return out;
|
|
685
|
+
}
|
|
686
|
+
// (not for a wide goal's part: its abstraction's search reads no judge, and the whole goal is decided by it after)
|
|
687
|
+
if (epr && !wideBusy && !path.size && !budget.nodes) {
|
|
688
|
+
const out = wide && tupledScope() ? withEprCap(wideEprUnits, () => eprFirst(goal, budget)) : eprFirst(goal, budget);
|
|
472
689
|
if (out)
|
|
473
690
|
return out;
|
|
474
691
|
}
|
|
@@ -484,7 +701,7 @@ function growRoot(goal, path, budget) {
|
|
|
484
701
|
const cdcl = !!root && cdclShare > 0 && !cellProducts.on && Number(/@(\d+) steps/.exec(goal.name)?.[1] ?? 1) <= 1 && !root.some(r => Object.keys(r.lin).some(isMonomial));
|
|
485
702
|
// At the root: plainly first; where the cases multiply past a budget, through cuts. The plain budget halves with each
|
|
486
703
|
// two open choices (what a plain search's cases grow by), so a long run's steps reach the cuts early.
|
|
487
|
-
if (root && unitFirst && unitsNow) {
|
|
704
|
+
if (root && unitFirst && unitsNow && !(eprScope.busy && tupledScope())) {
|
|
488
705
|
// A goal that a few cases settle needs no units (finding them costs a Farkas check per atom); the rest get them.
|
|
489
706
|
unitsNow = false;
|
|
490
707
|
try {
|
|
@@ -555,6 +772,8 @@ function growPlain(goal, path, budget) {
|
|
|
555
772
|
const verdict = next && rows.length > 60 ? solveP(rows, 2 * rows.length) : solveP(rows);
|
|
556
773
|
if (verdict.kind === 'refuted')
|
|
557
774
|
return { tree: leafOf(verdict.steps), nodes: budget.nodes };
|
|
775
|
+
if (next && wideScope.judge && verdict.kind === 'sat' && wideScope.judge(verdict.witness))
|
|
776
|
+
return { stuck: 'sat', witness: verdict.witness, path };
|
|
558
777
|
// Before a split, the rows the plain search sets aside (those reading a term column) by their Farkas rows: an open
|
|
559
778
|
// choice's bounds (min(a, c) ≤ c) often refute the case whichever way the choice goes, so its cases need no split.
|
|
560
779
|
if (next && !columns && full.length > rows.length) {
|
|
@@ -1088,7 +1307,7 @@ function withCuts(goal, path, budget) {
|
|
|
1088
1307
|
const chain = [];
|
|
1089
1308
|
let at = new Map(path);
|
|
1090
1309
|
for (const { n, e } of columns) {
|
|
1091
|
-
if (
|
|
1310
|
+
if (late(budget.until ?? Infinity))
|
|
1092
1311
|
return undefined;
|
|
1093
1312
|
if (e.op !== 'if')
|
|
1094
1313
|
continue;
|
|
@@ -1099,7 +1318,7 @@ function withCuts(goal, path, budget) {
|
|
|
1099
1318
|
const id = canon(lemma);
|
|
1100
1319
|
if (at.has(id) || Object.keys(lemma).every(key => key === '1'))
|
|
1101
1320
|
continue;
|
|
1102
|
-
const no = growFocused(goal, new Map(at).set(id, { lin: lemma, holds: false }), focus, { nodes: 0 });
|
|
1321
|
+
const no = growFocused(goal, new Map(at).set(id, { lin: lemma, holds: false }), focus, { nodes: 0, until: budget.until });
|
|
1103
1322
|
if ('tree' in no) {
|
|
1104
1323
|
chain.push({ split: lemma, no: no.tree });
|
|
1105
1324
|
at = new Map(at).set(id, { lin: lemma, holds: true });
|
|
@@ -1127,11 +1346,16 @@ function topAtoms(c, path) {
|
|
|
1127
1346
|
const atoms = c.op === '<=' ? [sub(a, b)] : c.op === '<' ? [add(sub(a, b), k(1))] : [sub(a, b), sub(b, a)];
|
|
1128
1347
|
return atoms.filter(l => Object.keys(l).some(key => key !== '1') && !path.has(canon(l)) && !path.has(canonFlip(l)));
|
|
1129
1348
|
}
|
|
1130
|
-
// A refutation that splits only on these atoms (a cut's failing side), within a small budget
|
|
1349
|
+
// A refutation that splits only on these atoms (a cut's failing side), within a small budget and the goal's end (past
|
|
1350
|
+
// it the goal is left unknown whatever the cuts find: the search after them stops at its first node).
|
|
1131
1351
|
function growFocused(goal, path, focus, budget, near = focus) {
|
|
1132
|
-
if (++budget.nodes > 64 ||
|
|
1352
|
+
if (++budget.nodes > 64 || late(budget.until ?? Infinity))
|
|
1133
1353
|
return { stuck: 'unknown', path };
|
|
1134
|
-
|
|
1354
|
+
// (each node reads its whole leaf, counted as growPlain counts a node's rows)
|
|
1355
|
+
const leaf = leafConstraints(goal, path);
|
|
1356
|
+
work.n += 1 + leaf.length;
|
|
1357
|
+
costs.plainRows += leaf.length;
|
|
1358
|
+
const verdict = solveNear(leaf, [...near, ...[...path.values()].slice(-1).map(a => a.lin)]);
|
|
1135
1359
|
if (verdict.kind === 'refuted')
|
|
1136
1360
|
return { tree: { leaf: verdict.steps }, nodes: budget.nodes };
|
|
1137
1361
|
const split = focus.find(l => !path.has(canon(l)) && !path.has(canonFlip(l)));
|
|
@@ -1221,9 +1445,9 @@ const eachRead = (f) => {
|
|
|
1221
1445
|
}
|
|
1222
1446
|
else if (e.op === 'if')
|
|
1223
1447
|
(inC(e.c), inE(e.then), inE(e.else));
|
|
1224
|
-
else if (e.op === 'mul')
|
|
1448
|
+
else if (e.op === 'mul' || e.op === 'proj')
|
|
1225
1449
|
inE(e.a);
|
|
1226
|
-
else if ('
|
|
1450
|
+
else if ('b' in e)
|
|
1227
1451
|
(inE(e.a), inE(e.b)); };
|
|
1228
1452
|
const inC = (c) => (c.op === 'flag' ? undefined : c.op === 'all' ? inC(c.c) : c.op === 'not' ? inC(c.x) : c.op === 'and' || c.op === 'or' ? c.xs.forEach(inC) : (inE(c.a), inE(c.b)));
|
|
1229
1453
|
return { inE, inC };
|
|
@@ -1233,13 +1457,17 @@ function readKeys(m, laws) {
|
|
|
1233
1457
|
const out = new Map();
|
|
1234
1458
|
const bound = new Set([...Object.values(m.step.flags ?? {}).flatMap(c => (c.op === 'all' ? boundOf(c) : [])), ...Object.values(m.step.maps ?? {}).flatMap(w => ('each' in w ? [w.each] : []))]);
|
|
1235
1459
|
const free = (e) => !JSON.stringify(e).match(/"name":"([^"]+)"/g)?.some(n => bound.has(n.slice(8, -1)));
|
|
1236
|
-
|
|
1237
|
-
|
|
1460
|
+
// (a tuple key's parts are the keys: a law about every tuple of keys is assumed at them)
|
|
1461
|
+
const put = (key) => { for (const p of tupleParts(key))
|
|
1462
|
+
if (free(p))
|
|
1463
|
+
out.set(JSON.stringify(p), p); };
|
|
1464
|
+
const { inE, inC } = eachRead(e => put(e.key));
|
|
1238
1465
|
Object.values(m.step.state ?? {}).forEach(inE);
|
|
1239
1466
|
Object.values(m.step.flags ?? {}).forEach(inC);
|
|
1240
1467
|
for (const w of Object.values(m.step.maps ?? {})) {
|
|
1241
1468
|
if (!('each' in w)) {
|
|
1242
|
-
|
|
1469
|
+
for (const p of tupleParts(w.key))
|
|
1470
|
+
out.set(JSON.stringify(p), p);
|
|
1243
1471
|
inE(w.key);
|
|
1244
1472
|
}
|
|
1245
1473
|
inE(w.value);
|
|
@@ -1263,8 +1491,15 @@ const shifts = (m) => Object.values(m.step.maps ?? {}).some(w => 'each' in w &&
|
|
|
1263
1491
|
// The keys some goals read a map at, and at each map, in one walk.
|
|
1264
1492
|
function mapReads(goals) {
|
|
1265
1493
|
const all = new Map(), out = new Map();
|
|
1266
|
-
|
|
1267
|
-
|
|
1494
|
+
// (a tuple key: its parts are the keys, each part read back from a tuple as the part; the map's key the tuple of them)
|
|
1495
|
+
const { inC } = eachRead(e0 => {
|
|
1496
|
+
const key = e0.key.op === 'tup' || e0.key.op === 'proj' ? projected(e0.key) : e0.key, id = JSON.stringify(key);
|
|
1497
|
+
for (const p of tupleParts(key))
|
|
1498
|
+
all.set(JSON.stringify(p), p);
|
|
1499
|
+
if (!out.has(e0.map))
|
|
1500
|
+
out.set(e0.map, new Map());
|
|
1501
|
+
out.get(e0.map).set(id, key);
|
|
1502
|
+
});
|
|
1268
1503
|
for (const g of goals)
|
|
1269
1504
|
g.hyps.forEach(inC);
|
|
1270
1505
|
return { keys: [...all.values()], byMap: new Map([...out].map(([mp, ks]) => [mp, [...ks.values()]])) };
|
|
@@ -1273,6 +1508,18 @@ function mapReads(goals) {
|
|
|
1273
1508
|
// where the names have these (a name left out is 0, as a model leaves it).
|
|
1274
1509
|
export const keyValue = (cell, w) => {
|
|
1275
1510
|
const inner = cell.slice(cell.indexOf('[') + 1, -1);
|
|
1511
|
+
// (a tuple's parts each valued so, the key their pairing)
|
|
1512
|
+
if (inner.startsWith('⟨')) {
|
|
1513
|
+
const parts = cellParts(cell).keys.map(k => keyValue(`_[${canon(k)}]`, w));
|
|
1514
|
+
if (parts.some(x => x === undefined))
|
|
1515
|
+
return undefined;
|
|
1516
|
+
try {
|
|
1517
|
+
return parts.slice(0, -1).reduceRight((b, a) => cpair(a, b), parts.at(-1));
|
|
1518
|
+
}
|
|
1519
|
+
catch {
|
|
1520
|
+
return undefined;
|
|
1521
|
+
}
|
|
1522
|
+
}
|
|
1276
1523
|
// (its parts split at the commas outside brackets: a part may be a cell itself, Pending[idn[n:1]:64,n:1], valued as the
|
|
1277
1524
|
// model values it)
|
|
1278
1525
|
const parts = [];
|
|
@@ -1309,7 +1556,11 @@ export const keyValue = (cell, w) => {
|
|
|
1309
1556
|
// the values the goal's keys take, and each tuple where one fails added, as the goal's terms with those values, to its
|
|
1310
1557
|
// instances. None found: the model is one on every tuple of the goal's keys. Untrusted: it chooses instances only.
|
|
1311
1558
|
export const modelRounds = 4;
|
|
1312
|
-
|
|
1559
|
+
// A condition as the disjuncts it is the or of (¬(a ∧ b) as ¬a, ¬b).
|
|
1560
|
+
// (GDP_MODEL_CUTS=0: every tuple evaluated whole, for comparing)
|
|
1561
|
+
const modelCuts = env.GDP_MODEL_CUTS !== '0';
|
|
1562
|
+
const disjuncts = (c) => (c.op === 'or' ? c.xs.flatMap(disjuncts) : c.op === 'not' && c.x.op === 'and' ? c.x.xs.flatMap(y => disjuncts({ op: 'not', x: y })) : c.op === 'not' && c.x.op === 'not' ? disjuncts(c.x.x) : [c]);
|
|
1563
|
+
export function modelInstances(m, laws, inst, goal, w, most = 64, sorted = false) {
|
|
1313
1564
|
const pm = plainOf(m), qs = Object.entries(pm.step.flags ?? {}).filter((e) => e[1].op === 'all' && w[`?${e[0]}`] === 1);
|
|
1314
1565
|
const keyed = laws.filter(l => l.forall);
|
|
1315
1566
|
if (!keyed.length && !qs.length)
|
|
@@ -1326,6 +1577,21 @@ export function modelInstances(m, laws, inst, goal, w, most = 64) {
|
|
|
1326
1577
|
maps[cell.slice(0, br)].set(key, value);
|
|
1327
1578
|
}
|
|
1328
1579
|
const st = { scalars: {}, maps }, read = (at) => new Proxy(at, { get: (t, p) => (p in t ? t[p] : w[p] ?? 0), has: (t, p) => p in t || p in w });
|
|
1580
|
+
// (a count's cell the model has not: its definition, its body summed over the model's cells below its bound, so a law
|
|
1581
|
+
// reading it is read as the model's state reads it, not at 0)
|
|
1582
|
+
for (const [c, d] of Object.entries(pm.counts ?? {})) {
|
|
1583
|
+
const had = maps[c], sum = (x) => {
|
|
1584
|
+
const e1 = read(d.base ? { [d.j]: Math.floor(x / d.base), [d.lo]: x % d.base } : { [d.j]: x }), n = Math.min(256, evalE(d.below, e1, st, mm));
|
|
1585
|
+
let t = 0;
|
|
1586
|
+
for (let k = 0; k < n; k++)
|
|
1587
|
+
t += evalE(d.body, read({ ...e1, [d.key]: k }), st, mm);
|
|
1588
|
+
return t;
|
|
1589
|
+
};
|
|
1590
|
+
maps[c] = new (class extends Map {
|
|
1591
|
+
get(x) { if (!super.has(x))
|
|
1592
|
+
super.set(x, 0), super.set(x, sum(x)); return super.get(x); }
|
|
1593
|
+
})(had);
|
|
1594
|
+
}
|
|
1329
1595
|
const ev = (f) => { try {
|
|
1330
1596
|
return f();
|
|
1331
1597
|
}
|
|
@@ -1341,7 +1607,9 @@ export function modelInstances(m, laws, inst, goal, w, most = 64) {
|
|
|
1341
1607
|
// Each bound key over its own sort as the model fills it: the terms its body's map reads bind matched against the
|
|
1342
1608
|
// goal's keys (M[x·N + y] against M[a·N + b] binds x to a), one term a value; a key no read binds (or only a
|
|
1343
1609
|
// function's), every key of the goal.
|
|
1344
|
-
const keys = mapReads([goal])
|
|
1610
|
+
const { keys, byMap } = mapReads([goal]), all = new Map();
|
|
1611
|
+
// (a law's key read at a tuple's place is matched against that map's tuples: its sort is the place's)
|
|
1612
|
+
const tupled = [...byMap.values()].some(ks => ks.some(k => k.op === 'tup'));
|
|
1345
1613
|
for (const e of keys) {
|
|
1346
1614
|
const v2 = valueOf(e);
|
|
1347
1615
|
if (v2 !== undefined && !all.has(v2))
|
|
@@ -1360,20 +1628,22 @@ export function modelInstances(m, laws, inst, goal, w, most = 64) {
|
|
|
1360
1628
|
// (a key no read binds is a value, an epoch say, over the naturals: 0, their least, is one of its terms too, as Ivy's
|
|
1361
1629
|
// ordered sorts have their zero; a law before the start, nothing above 0, is assumed there)
|
|
1362
1630
|
const zero = { op: 'const', value: 0 };
|
|
1363
|
-
const
|
|
1631
|
+
const accepts = instanceSortGuard(m, laws);
|
|
1632
|
+
const sortsOf = (body, names, atoms) => sortTerms(body, names, fns, mp => (tupled ? [...keys, ...(byMap.get(mp) ?? [])] : keys)).map(ts => {
|
|
1364
1633
|
const p = new Map();
|
|
1365
1634
|
for (const e of ts ?? []) {
|
|
1366
1635
|
const v2 = valueOf(e);
|
|
1367
1636
|
if (v2 !== undefined && !p.has(v2) && (!atoms || e.op === 'var'))
|
|
1368
1637
|
p.set(v2, e);
|
|
1369
1638
|
}
|
|
1370
|
-
const
|
|
1639
|
+
const fallback = new Map([...all].filter(([, e]) => !atoms || e.op === 'var'));
|
|
1640
|
+
const q = p.size ? p : fallback, r = witnessFirst(q);
|
|
1371
1641
|
return ts || r.has(0) ? r : r.set(0, zero);
|
|
1372
1642
|
});
|
|
1373
1643
|
const out = { ...inst };
|
|
1374
1644
|
// (the 20,000 evaluations shared: each law and flag at most an even share of what is left, so the laws about five keys
|
|
1375
1645
|
// first don't leave none to those after: sharded KV's "a key in transfer has no owner", broken at the goal's own keys)
|
|
1376
|
-
let added = 0, evals = 0, left = 0;
|
|
1646
|
+
let added = 0, evals = 0, left = 0, ineligible = false;
|
|
1377
1647
|
const each = (id, names, body, atoms) => {
|
|
1378
1648
|
if (pastWall())
|
|
1379
1649
|
return;
|
|
@@ -1387,9 +1657,23 @@ export function modelInstances(m, laws, inst, goal, w, most = 64) {
|
|
|
1387
1657
|
}
|
|
1388
1658
|
for (let t = 0; t + n <= had.length; t += n)
|
|
1389
1659
|
have.add(JSON.stringify(had.slice(t, t + n)));
|
|
1660
|
+
// (the body's disjuncts, each checked as soon as the keys it reads are bound: one that holds there holds the body at
|
|
1661
|
+
// every tuple below, so transitivity's T(t, x, y) false cuts its z; a law about four keys over ten values each, 10,000
|
|
1662
|
+
// tuples, took the whole share before, its broken tuples never reached)
|
|
1663
|
+
const ready = Array.from({ length: n + 1 }, () => []);
|
|
1664
|
+
if (modelCuts)
|
|
1665
|
+
for (const d of disjuncts(body)) {
|
|
1666
|
+
const t = JSON.stringify(d);
|
|
1667
|
+
ready[names.reduce((a, x, j) => (t.includes(`"name":${JSON.stringify(x)}`) ? j + 1 : a), 0)].push(d);
|
|
1668
|
+
}
|
|
1390
1669
|
const go = (i, at, terms) => {
|
|
1391
1670
|
if (added >= most || evals > until || (evals % 512 === 0 && pastWall()))
|
|
1392
1671
|
return;
|
|
1672
|
+
for (const d of ready[i]) {
|
|
1673
|
+
evals++;
|
|
1674
|
+
if (ev(() => evalC(d, read(at), st, mm)) === true)
|
|
1675
|
+
return;
|
|
1676
|
+
}
|
|
1393
1677
|
if (i === n) {
|
|
1394
1678
|
evals++;
|
|
1395
1679
|
if (ev(() => evalC(body, read(at), st, mm)) !== false)
|
|
@@ -1397,6 +1681,11 @@ export function modelInstances(m, laws, inst, goal, w, most = 64) {
|
|
|
1397
1681
|
const id2 = JSON.stringify(terms);
|
|
1398
1682
|
if (have.has(id2))
|
|
1399
1683
|
return;
|
|
1684
|
+
// Sort-aware consumers must not count tuples they would discard after this pass.
|
|
1685
|
+
if (sorted && terms.some((e, j) => !accepts(id, j, e))) {
|
|
1686
|
+
ineligible = true;
|
|
1687
|
+
return;
|
|
1688
|
+
}
|
|
1400
1689
|
have.add(id2);
|
|
1401
1690
|
out[id] = [...(out[id] ?? []), ...terms];
|
|
1402
1691
|
added++;
|
|
@@ -1406,6 +1695,8 @@ export function modelInstances(m, laws, inst, goal, w, most = 64) {
|
|
|
1406
1695
|
go(i + 1, { ...at, [names[i]]: v2 }, [...terms, e]);
|
|
1407
1696
|
};
|
|
1408
1697
|
go(0, {}, []);
|
|
1698
|
+
if (modelsTrace)
|
|
1699
|
+
console.error(`[models] ${atoms ? 'atoms' : 'all'} ${id.slice(0, 30)}: sorts ${sorts.map(x => x.size).join('x')}, evals ${evals} of ${until}, added ${added}`);
|
|
1409
1700
|
};
|
|
1410
1701
|
// the goal's own names first; every key it reads where they show nothing
|
|
1411
1702
|
for (const atoms of [true, false]) {
|
|
@@ -1418,7 +1709,8 @@ export function modelInstances(m, laws, inst, goal, w, most = 64) {
|
|
|
1418
1709
|
for (const [f, q] of qs)
|
|
1419
1710
|
each(`?${f}`, boundOf(q), q.c, atoms);
|
|
1420
1711
|
}
|
|
1421
|
-
|
|
1712
|
+
// A rejected-only refinement is not evidence that the model satisfies every assumed law.
|
|
1713
|
+
return added || ineligible ? out : undefined;
|
|
1422
1714
|
}
|
|
1423
1715
|
// A goal (or a law's goals) asked at some instances, and again at those its model asks for (modelInstances), up to
|
|
1424
1716
|
// modelRounds times. `ask` is the caller's own question; `modelOf` reads a model from its answer, `settled` says it is
|
|
@@ -1427,6 +1719,8 @@ export function modelInstances(m, laws, inst, goal, w, most = 64) {
|
|
|
1427
1719
|
// not the goal's usual budget again; a later round left unsettled gives back the first round's answer, so the verdict is
|
|
1428
1720
|
// never one the model's tuples made worse. Untrusted.
|
|
1429
1721
|
export const modelShare = 3;
|
|
1722
|
+
// (GDP_MODELS_TRACE=1: each model round, its goal and the tuples a model adds, on stderr)
|
|
1723
|
+
const modelsTrace = !!env.GDP_MODELS_TRACE, modelFloor = Number(env.GDP_MODEL_FLOOR ?? 1_000);
|
|
1430
1724
|
const costNow = () => (rate ? clock() : fullWork() / groundRate);
|
|
1431
1725
|
const within = (ms, f) => (rate ? withDeadline(clock() + ms, f) : withWork(fullWork() + groundRate * ms, f));
|
|
1432
1726
|
export function withModels(m, laws, inst, goalOf, ask, modelOf, settled) {
|
|
@@ -1439,13 +1733,18 @@ function modelsLoop(m, laws, inst, goalOf, ask, modelOf, settled) {
|
|
|
1439
1733
|
if (round)
|
|
1440
1734
|
cap -= costNow() - c0;
|
|
1441
1735
|
else
|
|
1442
|
-
cap = Math.max(
|
|
1443
|
-
if (round && !settled(result))
|
|
1736
|
+
cap = Math.max(modelFloor, modelShare * (costNow() - c0));
|
|
1737
|
+
if (round && !settled(result)) {
|
|
1738
|
+
if (modelsTrace)
|
|
1739
|
+
console.error(`[models] round ${round} unsettled in ${Math.round(costNow() - c0)} cost`);
|
|
1444
1740
|
return first;
|
|
1741
|
+
}
|
|
1445
1742
|
if (pastWall())
|
|
1446
1743
|
return first ?? { result, inst: at };
|
|
1447
1744
|
first ??= { result, inst: at };
|
|
1448
1745
|
const mw = modelOf(result), more = mw && round < modelRounds && cap > 0 ? modelInstances(m, laws, at, mw.goal, mw.w) : undefined;
|
|
1746
|
+
if (modelsTrace)
|
|
1747
|
+
console.error(`[models] round ${round} ${mw ? `model at ${mw.goal.name} (${mw.goal.hyps.length} hyps)` : settled(result) ? 'settled' : 'unsettled'} ${Math.round(costNow() - c0)} cost, cap ${Math.round(cap)}; ${more ? `+${Object.entries(more).map(([n, ks]) => `${n.slice(0, 24)}:${ks.length - (at[n]?.length ?? 0)}`).filter(x => !x.endsWith(':0')).join(' ')}` : 'no more'}`);
|
|
1449
1748
|
if (!more)
|
|
1450
1749
|
return { result, inst: at };
|
|
1451
1750
|
at = more;
|
|
@@ -1769,10 +2068,14 @@ const triggers = (body, key) => {
|
|
|
1769
2068
|
const substKey = (e, key, by) => JSON.parse(JSON.stringify(e).replaceAll(`{"op":"var","name":${JSON.stringify(key)}}`, JSON.stringify(by)));
|
|
1770
2069
|
// GDP_TRIGGER=0 keeps every instance (for comparing).
|
|
1771
2070
|
const triggerOn = env.GDP_TRIGGER !== '0';
|
|
1772
|
-
function triggered(m, laws, inst, build, focus = []) {
|
|
2071
|
+
export function triggered(m, laws, inst, build, focus = []) {
|
|
1773
2072
|
if (!triggerOn)
|
|
1774
2073
|
return inst;
|
|
1775
2074
|
// Per name: its bound keys, and for each the triggers that read it alone (a key with none takes any value).
|
|
2075
|
+
// Tuple-keyed function images (including generated witnesses) introduce key
|
|
2076
|
+
// terms outside relational reads. Keep legacy focus seeds for that shape.
|
|
2077
|
+
const tupleFunctions = Object.entries(m.step.maps ?? {}).some(([name, w]) => !m.relations?.[name] && !('each' in w) && w.key.op === 'tup');
|
|
2078
|
+
const tupled = !!Object.keys(m.relations ?? {}).length && !tupleFunctions;
|
|
1776
2079
|
const bodies = new Map();
|
|
1777
2080
|
for (const [n, ks] of Object.entries(inst)) {
|
|
1778
2081
|
if (!ks.length)
|
|
@@ -1781,15 +2084,26 @@ function triggered(m, laws, inst, build, focus = []) {
|
|
|
1781
2084
|
const body = q?.op === 'all' ? q.c : l?.forall ? l.holds : undefined, keys = q?.op === 'all' ? boundOf(q) : l?.forall ? [l.forall, ...(l.keys ?? [])] : [];
|
|
1782
2085
|
if (!body)
|
|
1783
2086
|
continue;
|
|
1784
|
-
const per = keys.map(key =>
|
|
2087
|
+
const per = keys.map(key => {
|
|
2088
|
+
const ts = triggers(body, key).filter(t => tupled || keys.every(o => o === key || !JSON.stringify(t.at).includes(`"name":${JSON.stringify(o)}`)));
|
|
2089
|
+
// A joint read constrains the tuple; independent field reads would admit
|
|
2090
|
+
// its Cartesian product again. Missing useful tuples can still enter by MBQI.
|
|
2091
|
+
const joint = tupled ? ts.filter(t => keys.filter(k => JSON.stringify(t.at).includes(`"name":${JSON.stringify(k)}`)).length > 1) : [];
|
|
2092
|
+
return joint.length ? joint : ts;
|
|
2093
|
+
});
|
|
1785
2094
|
if (per.some(t => t.length))
|
|
1786
2095
|
bodies.set(n, { keys, per });
|
|
1787
2096
|
}
|
|
1788
2097
|
if (!bodies.size)
|
|
1789
2098
|
return inst;
|
|
1790
2099
|
const tuplesOf = (n, ks) => { const a = bodies.get(n).keys.length; return Array.from({ length: Math.floor(ks.length / a) }, (_, t) => ks.slice(t * a, (t + 1) * a)); };
|
|
1791
|
-
const kept = (tuple) => tuple.some(e => e.op === 'var' && focus.includes(e.name));
|
|
2100
|
+
const kept = (tuple) => !tupled && tuple.some(e => e.op === 'var' && focus.includes(e.name));
|
|
1792
2101
|
const out = Object.fromEntries(Object.entries(inst).map(([n, ks]) => [n, bodies.has(n) ? tuplesOf(n, ks).filter(kept).flat() : ks]));
|
|
2102
|
+
// Tuple substitution is independent of the growing read set. Cache each
|
|
2103
|
+
// candidate's trigger text once, rather than rebuilding it every round.
|
|
2104
|
+
const candidates = new Map();
|
|
2105
|
+
for (const n of bodies.keys())
|
|
2106
|
+
candidates.set(n, tuplesOf(n, inst[n]).map(tuple => ({ tuple, id: JSON.stringify(tuple), tests: [] })));
|
|
1793
2107
|
for (let round = 0; round < 8 && !pastWall(); round++) {
|
|
1794
2108
|
const reads = new Set();
|
|
1795
2109
|
const { inC } = eachRead(e => { reads.add(`${e.map}|${JSON.stringify(e.key)}`); });
|
|
@@ -1798,9 +2112,11 @@ function triggered(m, laws, inst, build, focus = []) {
|
|
|
1798
2112
|
let grew = false;
|
|
1799
2113
|
for (const [n, { keys, per }] of bodies) {
|
|
1800
2114
|
const have = new Set(tuplesOf(n, out[n]).map(t => JSON.stringify(t)));
|
|
1801
|
-
for (const tuple of
|
|
1802
|
-
|
|
1803
|
-
|
|
2115
|
+
for (const { tuple, id, tests } of candidates.get(n)) {
|
|
2116
|
+
if (have.has(id) || !keys.every((key, i) => {
|
|
2117
|
+
const at = tests[i] ??= per[i].map(({ map, at }) => `${map}|${JSON.stringify(tupled ? substE(at, { vars: Object.fromEntries(keys.map((k, j) => [k, tuple[j]])), maps: {}, flags: {} }) : substKey(at, key, tuple[i]))}`);
|
|
2118
|
+
return !at.length || at.some(t => reads.has(t));
|
|
2119
|
+
}))
|
|
1804
2120
|
continue;
|
|
1805
2121
|
out[n].push(...tuple);
|
|
1806
2122
|
have.add(id);
|
|
@@ -1914,7 +2230,9 @@ function substVar(c, name, by) {
|
|
|
1914
2230
|
case 'sub':
|
|
1915
2231
|
case 'min':
|
|
1916
2232
|
case 'max':
|
|
1917
|
-
case 'prod':
|
|
2233
|
+
case 'prod':
|
|
2234
|
+
case 'tup': return { ...t, a: e(t.a), b: e(t.b) };
|
|
2235
|
+
case 'proj': return { ...t, a: e(t.a) };
|
|
1918
2236
|
}
|
|
1919
2237
|
};
|
|
1920
2238
|
return c.op === 'flag' ? c : c.op === 'all' ? { ...c, c: substVar(c.c, name, by) } : c.op === 'not' ? { op: 'not', x: substVar(c.x, name, by) } : c.op === 'and' || c.op === 'or' ? { op: c.op, xs: c.xs.map(y => substVar(y, name, by)) } : { ...c, a: e(c.a), b: e(c.b) };
|
|
@@ -1988,6 +2306,7 @@ export function decideLaw(m, laws, i, rounds = modelRounds) {
|
|
|
1988
2306
|
export const decideNote = { refuted: false };
|
|
1989
2307
|
function decideLaw0(m, laws, i, rounds) {
|
|
1990
2308
|
decideNote.refuted = false;
|
|
2309
|
+
delete decideNote.hint;
|
|
1991
2310
|
if (!fastShape(m, laws))
|
|
1992
2311
|
return undefined;
|
|
1993
2312
|
return withPatterns(m, laws, () => {
|
|
@@ -2036,7 +2355,7 @@ function settled(m, laws, i, rounds, usual) {
|
|
|
2036
2355
|
}
|
|
2037
2356
|
memoHits.miss++;
|
|
2038
2357
|
const mo = rounds === modelRounds ? memo.models.get(law.name) : undefined;
|
|
2039
|
-
if (mo && mo.text === text && laws.every(l => mo.set.get(l.name) === textOfLaw(l)))
|
|
2358
|
+
if (mo && (!strict(usual) || mo.strict) && mo.text === text && laws.every(l => mo.set.get(l.name) === textOfLaw(l)))
|
|
2040
2359
|
return mo.model;
|
|
2041
2360
|
const out = settle(m, laws, i, rounds, usual, prev);
|
|
2042
2361
|
const cap = (x) => { if (x.size >= 4096)
|
|
@@ -2044,7 +2363,7 @@ function settled(m, laws, i, rounds, usual) {
|
|
|
2044
2363
|
if (out && 'proofs' in out)
|
|
2045
2364
|
cap(memo.proven).set(law.name, out);
|
|
2046
2365
|
else if (out?.stuck === 'sat' && rounds === modelRounds)
|
|
2047
|
-
cap(memo.models).set(law.name, { text, model: out, set: new Map(laws.map(l => [l.name, textOfLaw(l)])) });
|
|
2366
|
+
cap(memo.models).set(law.name, { text, model: out, strict: strict(usual), set: new Map(laws.map(l => [l.name, textOfLaw(l)])) });
|
|
2048
2367
|
return out;
|
|
2049
2368
|
}
|
|
2050
2369
|
// The fast path's machines: no history, sums or "there exists" (their goals are other machines' or need witnesses).
|
|
@@ -2089,10 +2408,11 @@ function settle(m, laws, i, rounds, usual, prev) {
|
|
|
2089
2408
|
const law = laws[i], byName = new Map(laws.map(l => [l.name, l]));
|
|
2090
2409
|
// (decideLaw's and the fast set proof's: each law's keys of its sort alone, `bySort`; proveLaw's and the join's as they
|
|
2091
2410
|
// were. The fast set proof is checked over the tuples its proofs read, and the usual proof follows where it fails)
|
|
2092
|
-
|
|
2411
|
+
const reuse = prev && laws.every(l => !l.forall || l.name in prev.inst);
|
|
2412
|
+
let inst = reuse ? prev.inst : bySort(m, laws, lawInstances(m, laws, i));
|
|
2093
2413
|
// (GDP_DLTRIG=1: decideLaw's first instances only those a goal's reads trigger (proveOne's `triggered`), the model
|
|
2094
2414
|
// rounds adding the instances a model breaks (MBQI's))
|
|
2095
|
-
if (!
|
|
2415
|
+
if (!reuse && (Object.keys(m.relations ?? {}).length ? env.GDP_DLTRIG !== '0' : !usual && env.GDP_DLTRIG === '1'))
|
|
2096
2416
|
inst = triggered(plainOf(m), laws, inst, i2 => obligations(m, laws, { [law.name]: i2 }, i), law.forall ? [`${law.forall}#`] : []);
|
|
2097
2417
|
const proofs = Object.fromEntries(Object.entries(prev?.proofs ?? {}).filter(([, p]) => stands(p, byName)));
|
|
2098
2418
|
const unknown = (g, why = 'past the search\'s end') => (usual ? { goal: g.name, stuck: 'unknown', why } : undefined);
|
|
@@ -2108,7 +2428,13 @@ function settle(m, laws, i, rounds, usual, prev) {
|
|
|
2108
2428
|
continue;
|
|
2109
2429
|
if (late())
|
|
2110
2430
|
return strict(usual) ? unknown(g) : last ?? unknown(g);
|
|
2111
|
-
|
|
2431
|
+
// (a wide goal decided over parts (growWide), each part by the EPR path: the abstraction of the whole is quadratic in
|
|
2432
|
+
// its cells, i4's engine set's step goal of 7,590 hypotheses ran 19M units past its caller's end and 10 GB)
|
|
2433
|
+
let o = !eprScope.patterns ? undefined : wideFor(g) && tupledScope() ? eprWide(g, late) ?? withEprCap(wideEprUnits, () => eprFirst(g, { nodes: 0, until: clock() + 4_000 }, false, late)) : eprFirst(g, { nodes: 0, until: clock() + 4_000 }, false, late);
|
|
2434
|
+
// Initial states often erase every map read, leaving a small arithmetic goal
|
|
2435
|
+
// outside EPR. Do not require a whole-set proof just to seed its start tree.
|
|
2436
|
+
if (!o && g.name.endsWith(' @start') && !late())
|
|
2437
|
+
o = grow(g, new Map(), { nodes: 0, until: end });
|
|
2112
2438
|
// (a refutation whose core's proof didn't finish: a model from fewer instances, which these refute, is no answer;
|
|
2113
2439
|
// decideLaw's unknown, the usual search's next)
|
|
2114
2440
|
if (!o && eprLast.refuted) {
|
|
@@ -2141,13 +2467,15 @@ function settle(m, laws, i, rounds, usual, prev) {
|
|
|
2141
2467
|
last = { goal: g.name, stuck: 'sat', witness: o.witness, ...(o.diagram ? { diagram: o.diagram } : {}), why: 'a model' };
|
|
2142
2468
|
// (strict, decideLaw's: a model is returned only where confirmed, no assumed law broken at it over its own domain;
|
|
2143
2469
|
// one that breaks only instances of another sort, or that the rounds or the wall cut off, is no answer)
|
|
2144
|
-
const capped = !(round < rounds || (round < 4 * rounds && end !== Infinity && clock() < end)), raw = capped ? undefined : modelInstances(m, laws, inst, g, o.witness);
|
|
2470
|
+
const capped = !(round < rounds || (round < 4 * rounds && end !== Infinity && clock() < end)), raw = capped ? undefined : modelInstances(m, laws, inst, g, o.witness, undefined, true);
|
|
2145
2471
|
if (!capped && !raw)
|
|
2146
2472
|
return last;
|
|
2147
2473
|
more = raw && bySort(m, laws, raw);
|
|
2148
2474
|
const count = (x) => Object.values(x).reduce((s2, ks) => s2 + ks.length, 0);
|
|
2149
|
-
if (strict(usual) && (capped || pastWall() || !more || count(more) <= count(inst)))
|
|
2475
|
+
if (strict(usual) && (capped || pastWall() || !more || count(more) <= count(inst))) {
|
|
2476
|
+
decideNote.hint = { goal: g.name, witness: o.witness };
|
|
2150
2477
|
return undefined;
|
|
2478
|
+
}
|
|
2151
2479
|
// (the model's only broken instances another sort's: the next round would find it again; decideLaw's no answer)
|
|
2152
2480
|
if (more && count(more) <= count(inst))
|
|
2153
2481
|
return usual ? last : undefined;
|
|
@@ -2160,6 +2488,14 @@ function settle(m, laws, i, rounds, usual, prev) {
|
|
|
2160
2488
|
inst = more;
|
|
2161
2489
|
}
|
|
2162
2490
|
}
|
|
2491
|
+
// A wide goal over parts, each asked by the EPR path (its core widened to the part) or else the usual search: a tree is
|
|
2492
|
+
// the part's, a model one of the whole. Parts for four times eprFirst's window (growWide's share of it), each part
|
|
2493
|
+
// within that window.
|
|
2494
|
+
function eprWide(g, late) {
|
|
2495
|
+
const out = growWide(g, { nodes: 0, until: clock() + 16_000 }, (gp, until) => { const u = Math.max(until, clock() + 4_000); return eprFirst(gp, { nodes: 0, until: u }, true, late) ?? grow(gp, new Map(), { nodes: 0, until: u }); }, true);
|
|
2496
|
+
eprLast.refuted = false;
|
|
2497
|
+
return out && ('tree' in out || (out.stuck === 'sat' && out.witness)) ? out : undefined;
|
|
2498
|
+
}
|
|
2163
2499
|
// A set of laws proven as proveLaws proves it, for a search that drops a law the proof fails at and asks again (the
|
|
2164
2500
|
// enumeration's set proof): each law's goals settled in order (kept from this call or decideLaw's where they stand), the
|
|
2165
2501
|
// first law not proven returned with its goal's model; when every law is, the proof made once: each law's instances only
|
|
@@ -2230,24 +2566,50 @@ export function proveLaw(m, laws, i) {
|
|
|
2230
2566
|
}
|
|
2231
2567
|
function proveOne(m, laws, i) {
|
|
2232
2568
|
const law = laws[i], pm = plainOf(m), inst = lawInstances(m, laws, i);
|
|
2569
|
+
// Keep the ordinary fallback's actual trees too. A local proof can use a
|
|
2570
|
+
// ghost value as a key where EPR's sorted instances were insufficient. The
|
|
2571
|
+
// complete-set path widens these trees and checks every obligation afresh.
|
|
2572
|
+
const remember = (at, trees) => {
|
|
2573
|
+
if (!Object.values(m.step.maps ?? {}).some(w => !('each' in w) && w.key.op === 'tup') || !fastShape(m, laws))
|
|
2574
|
+
return;
|
|
2575
|
+
const byName = new Map(laws.map(l => [l.name, l])), proofs = {};
|
|
2576
|
+
for (const g of obligations(m, laws, { [law.name]: at }, i)) {
|
|
2577
|
+
const tree = trees[g.name];
|
|
2578
|
+
if (!tree)
|
|
2579
|
+
return;
|
|
2580
|
+
const src = sourcesOf(m, laws, at, g, usedHyps(g, tree));
|
|
2581
|
+
proofs[g.name] = { part: g, tree, uses: new Map((src ? [...src.keys()] : laws.map(l => l.name)).map(n => [n, textOfLaw(byName.get(n))])), ...(src ? { tuples: src } : {}) };
|
|
2582
|
+
}
|
|
2583
|
+
const memo = memoOf(m);
|
|
2584
|
+
if (memo.proven.size >= 4096)
|
|
2585
|
+
memo.proven.clear();
|
|
2586
|
+
memo.proven.set(law.name, { text: textOfLaw(law), inst: at, proofs });
|
|
2587
|
+
};
|
|
2233
2588
|
const few = triggered(pm, laws, inst, i2 => obligations(m, laws, { [law.name]: i2 }, i), law.forall ? [`${law.forall}#`] : []);
|
|
2234
2589
|
if (!sameInstances(few, inst)) {
|
|
2235
2590
|
let proven = true;
|
|
2591
|
+
const done = {};
|
|
2236
2592
|
for (const goal of obligations(m, laws, { [law.name]: few }, i)) {
|
|
2237
2593
|
const o = grow(goal);
|
|
2238
|
-
if ('tree' in o)
|
|
2594
|
+
if ('tree' in o) {
|
|
2595
|
+
done[goal.name] = o.tree;
|
|
2239
2596
|
continue;
|
|
2597
|
+
}
|
|
2240
2598
|
proven = false;
|
|
2241
2599
|
// (a goal that ran out past the deadline: the next instances can't be tried, and it is the verdict's goal)
|
|
2242
2600
|
if (o.stuck === 'unknown' && ranOut())
|
|
2243
2601
|
return { proven: false, goal: goal.name, stuck: 'unknown' };
|
|
2244
2602
|
break;
|
|
2245
2603
|
}
|
|
2246
|
-
if (proven)
|
|
2604
|
+
if (proven) {
|
|
2605
|
+
remember(few, done);
|
|
2247
2606
|
return { proven: true };
|
|
2607
|
+
}
|
|
2248
2608
|
}
|
|
2249
2609
|
// (a model breaking a law at a tuple of the goal's keys: those added, the law asked again)
|
|
2250
|
-
const { result: a } = withModels(m, laws, inst, i2 => obligations(m, laws, { [law.name]: i2 }, i), growAll, grownModel, grownSettled);
|
|
2610
|
+
const { result: a, inst: at } = withModels(m, laws, inst, i2 => obligations(m, laws, { [law.name]: i2 }, i), growAll, grownModel, grownSettled);
|
|
2611
|
+
if (!a.out)
|
|
2612
|
+
remember(at, a.done);
|
|
2251
2613
|
return a.out ? { proven: false, goal: a.goal.name, stuck: a.out.stuck, ...(a.out.witness ? { witness: a.out.witness } : {}), ...(a.out.diagram ? { diagram: a.out.diagram } : {}) } : { proven: true };
|
|
2252
2614
|
}
|
|
2253
2615
|
function growAll(gs) {
|
|
@@ -2290,12 +2652,20 @@ finally {
|
|
|
2290
2652
|
} };
|
|
2291
2653
|
// (by machine and by the law set's names: one machine is asked with many sets)
|
|
2292
2654
|
const sortsMemo = new WeakMap();
|
|
2655
|
+
const sortPlacesOn = env.GDP_SORT_PLACES !== '0';
|
|
2293
2656
|
function sortsOf(m, laws) {
|
|
2294
2657
|
const par = new Map(), find = (a) => { const q = par.get(a); if (q === undefined || q === a)
|
|
2295
2658
|
return a; const r = find(q); par.set(a, r); return r; };
|
|
2296
2659
|
const named = new Set(), union = (a, b) => { named.add(a); named.add(b); const ra = find(a), rb = find(b); if (ra !== rb)
|
|
2297
2660
|
par.set(ra, rb); };
|
|
2298
2661
|
const pats = patternsOf(m, laws), whole = new Map();
|
|
2662
|
+
// (only on machines with tuple keys, whose patterns are n zeros: elsewhere the sorts are as before, distributed_lock's
|
|
2663
|
+
// instances among them)
|
|
2664
|
+
const sortPlaces = sortPlacesOn && Object.values(pats).some(c => c.length > 1 && c[0] === 0);
|
|
2665
|
+
// (a map of one part read at a name, Src[p]: its value a sort too, F:Src, as a function's, what it is compared with and
|
|
2666
|
+
// the places it is read at: Src[Src[p]] is no key of Src where Src's values are nodes and its keys packets)
|
|
2667
|
+
// (GDP_SORT_PLACES=0: neither, for comparing)
|
|
2668
|
+
const valued = (e) => sortPlaces && !((pats[e.map]?.length ?? 0) > 1) && e.key.op !== 'tup' && (e.key.op === 'var' || e.key.op === 'get');
|
|
2299
2669
|
const leaves = (e, k, out) => { if (e.op === 'add') {
|
|
2300
2670
|
leaves(e.a, k, out);
|
|
2301
2671
|
leaves(e.b, k, out);
|
|
@@ -2305,7 +2675,7 @@ function sortsOf(m, laws) {
|
|
|
2305
2675
|
else if (e.op !== 'const')
|
|
2306
2676
|
out.push([e, k]); };
|
|
2307
2677
|
const walkC = (c, name) => {
|
|
2308
|
-
const node = (e) => (e.op === 'var' ? name(e.name) : e.op === 'get' && m.fns?.[e.map] ? `F:${e.map}` : undefined);
|
|
2678
|
+
const node = (e) => (e.op === 'var' ? name(e.name) : e.op === 'get' && (m.fns?.[e.map] || valued(e)) ? `F:${e.map}` : undefined);
|
|
2309
2679
|
const visit = (o) => {
|
|
2310
2680
|
if (!o || typeof o !== 'object')
|
|
2311
2681
|
return;
|
|
@@ -2316,6 +2686,10 @@ function sortsOf(m, laws) {
|
|
|
2316
2686
|
if (n)
|
|
2317
2687
|
union(n, `A:${x.map}`);
|
|
2318
2688
|
}
|
|
2689
|
+
// (a tuple key: each part of its place's sort, M:map:i)
|
|
2690
|
+
else if (x.key.op === 'tup' && sortPlaces)
|
|
2691
|
+
tupleParts(x.key).forEach((e, i) => { const n = node(e); if (n)
|
|
2692
|
+
union(n, `M:${x.map}:${i}`); });
|
|
2319
2693
|
else {
|
|
2320
2694
|
const ls = [];
|
|
2321
2695
|
leaves(x.key, 1, ls);
|
|
@@ -2407,13 +2781,13 @@ function sortsOf(m, laws) {
|
|
|
2407
2781
|
return mp ? `K:${mp}` : `K:?${cs}`;
|
|
2408
2782
|
}
|
|
2409
2783
|
const x = ls[0][0];
|
|
2410
|
-
return x.op === 'var' ? (x.name.endsWith('#') ? undefined : `V:${x.name}`) : x.op === 'get' && m.fns?.[x.map] ? `F:${x.map}` : undefined;
|
|
2784
|
+
return x.op === 'var' ? (x.name.endsWith('#') ? undefined : `V:${x.name}`) : x.op === 'get' && (m.fns?.[x.map] || valued(x)) ? `F:${x.map}` : undefined;
|
|
2411
2785
|
};
|
|
2412
2786
|
return { key: (law, k) => `L:${law}:${k}`, of, find, anchored: (a) => /^K:/.test(a) || !!anchorOf.get(find(a)) };
|
|
2413
2787
|
}
|
|
2414
|
-
function
|
|
2788
|
+
function instanceSortGuard(m, laws) {
|
|
2415
2789
|
if (!sortedOn)
|
|
2416
|
-
return
|
|
2790
|
+
return () => true;
|
|
2417
2791
|
let byLaws = sortsMemo.get(m);
|
|
2418
2792
|
if (!byLaws)
|
|
2419
2793
|
sortsMemo.set(m, (byLaws = new Map()));
|
|
@@ -2433,14 +2807,28 @@ function bySort(m, laws, inst) {
|
|
|
2433
2807
|
const l = laws.find(x => x.name === n);
|
|
2434
2808
|
return l?.forall ? [l.forall, ...(l.keys ?? [])].map(k => S.key(n, k)) : undefined;
|
|
2435
2809
|
};
|
|
2810
|
+
const wanted = new Map();
|
|
2811
|
+
return (n, j, e) => {
|
|
2812
|
+
if (!wanted.has(n))
|
|
2813
|
+
wanted.set(n, keysOf(n));
|
|
2814
|
+
const want = wanted.get(n)?.[j], se = S.of(e);
|
|
2815
|
+
return !want || !se || !S.anchored(se) || !S.anchored(want) || S.find(se) === S.find(want);
|
|
2816
|
+
};
|
|
2817
|
+
}
|
|
2818
|
+
function bySort(m, laws, inst) {
|
|
2819
|
+
if (!sortedOn)
|
|
2820
|
+
return inst;
|
|
2821
|
+
const accepts = instanceSortGuard(m, laws);
|
|
2436
2822
|
return Object.fromEntries(Object.entries(inst).map(([n, ks]) => {
|
|
2437
|
-
const
|
|
2823
|
+
const q = n.startsWith('?') ? m.step.flags?.[n.slice(1)] : undefined;
|
|
2824
|
+
const l = laws.find(l => l.name === n);
|
|
2825
|
+
const want = q?.op === 'all' ? boundOf(q) : l?.forall ? [l.forall, ...(l.keys ?? [])] : undefined;
|
|
2438
2826
|
if (!want || !ks.length)
|
|
2439
2827
|
return [n, ks];
|
|
2440
2828
|
const a = want.length, out = [];
|
|
2441
2829
|
for (let t = 0; t + a <= ks.length; t += a) {
|
|
2442
2830
|
const tuple = ks.slice(t, t + a);
|
|
2443
|
-
const ok = tuple.every((e, j) =>
|
|
2831
|
+
const ok = tuple.every((e, j) => accepts(n, j, e));
|
|
2444
2832
|
if (ok)
|
|
2445
2833
|
out.push(...tuple);
|
|
2446
2834
|
}
|
|
@@ -2456,6 +2844,18 @@ export function lawInstances(m, laws, i) {
|
|
|
2456
2844
|
focus: [...(law.forall ? [`${law.forall}#`] : []), ...(law.keys ?? [])],
|
|
2457
2845
|
});
|
|
2458
2846
|
}
|
|
2847
|
+
// (GDP_SET_FIRST=0: the usual way alone, for comparing)
|
|
2848
|
+
const setFirstOn = env.GDP_SET_FIRST !== '0';
|
|
2849
|
+
const setFirst = (m, laws) => {
|
|
2850
|
+
// (machines with tuple keys alone, patterns of zeros: on radix keys the usual way is the one tuned, and a set proof
|
|
2851
|
+
// first cost sharded KV's no-lost-keys, whose witness passes ask the whole set again and again, 2.8M of work to 45M)
|
|
2852
|
+
if (!eprOn || !setFirstOn || pastWall() || !fastShape(m, laws) || !Object.values(patternsOf(plainOf(m), laws)).some(c => c.length > 1 && c.every(x => x === 0)))
|
|
2853
|
+
return undefined;
|
|
2854
|
+
const out = proveSetFast(m, laws);
|
|
2855
|
+
if (modelsTrace)
|
|
2856
|
+
console.error(`[models] the set first: ${out.proven ? out.checked ? 'proven' : 'proven, not checked' : `not proven at ${out.failed?.goal}`}`);
|
|
2857
|
+
return out.proven && out.checked && out.proof ? out : undefined;
|
|
2858
|
+
};
|
|
2459
2859
|
// `defer`: the usual trees now, their certificate and check given to it as `finish`, which proveStepLaws runs once every
|
|
2460
2860
|
// step law is proven too: a verdict that fails needs neither (TigerBeetle's laws spent 50 s of 67 on the laws' certificate
|
|
2461
2861
|
// and check before a step law failed). Deferred, a certificate not made leaves the check to checkStepLaws, which reads
|
|
@@ -2464,12 +2864,18 @@ function proveLawsAsGiven(m, laws, defer) {
|
|
|
2464
2864
|
// Each law's two goals (start and step) get their own instances, seeded with its own keys.
|
|
2465
2865
|
// (a machine with sums: its instances are its plain machine's, whose flags hold each sum's facts at every key)
|
|
2466
2866
|
const pm = plainOf(m), keys = [...readKeys(pm, laws), ...witnesses(pm)], instances = {};
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2867
|
+
// (an EPR-shaped machine's set over tuple keys proved first as proveSetFast proves it: each law settled by the EPR
|
|
2868
|
+
// path, its instances sorted and only the tuples its proofs read, its cores widened to goals that small; i4's learning
|
|
2869
|
+
// switch over relations 6 s there against 80 s here, whose check alone read goals of 1,200 hypotheses for 55 s. Not
|
|
2870
|
+
// proven, the usual way)
|
|
2871
|
+
const fast = defer ? undefined : setFirst(m, laws);
|
|
2872
|
+
if (!fast)
|
|
2873
|
+
laws.forEach((law, i) => {
|
|
2874
|
+
instances[law.name] = instancesFor(pm, laws, inst => obligations(m, laws, { ...instances, [law.name]: inst }, i), {
|
|
2875
|
+
seed: [...keys, ...(law.forall ? [{ op: 'var', name: `${law.forall}#` }] : []), ...(law.keys ?? []).map((name) => ({ op: 'var', name }))],
|
|
2876
|
+
focus: [...(law.forall ? [`${law.forall}#`] : []), ...(law.keys ?? [])],
|
|
2877
|
+
});
|
|
2471
2878
|
});
|
|
2472
|
-
});
|
|
2473
2879
|
// (by default a covered machine's certificate is its cyclic proof, core/cyclic.ts's certCyclic, the usual trees its
|
|
2474
2880
|
// input, not checked again; where none is made, the usual trees are checked and the proof counted uncovered)
|
|
2475
2881
|
// (what no instance covers, read only where a cyclic mode is on: the usual route's own proofs, inferLaws' trial ones
|
|
@@ -2478,7 +2884,7 @@ function proveLawsAsGiven(m, laws, defer) {
|
|
|
2478
2884
|
const modes = cyclicOn() || certOn(), hist = modes && m.history ? historyMachine(m) : undefined, mc = hist ? hist.machine : m;
|
|
2479
2885
|
const why = modes ? (sumsWhy(m) ?? uncoveredWhy(mc, laws)) : undefined, cert = certOn() && !why;
|
|
2480
2886
|
usualCheck.on = !cert && !defer;
|
|
2481
|
-
const t0 = performance.now(), out = (() => { try {
|
|
2887
|
+
const t0 = performance.now(), out = fast ?? (() => { try {
|
|
2482
2888
|
return proveThinned(m, laws, { ...instances });
|
|
2483
2889
|
}
|
|
2484
2890
|
finally {
|
|
@@ -2543,6 +2949,8 @@ function thinned(m, laws, instances) {
|
|
|
2543
2949
|
for (const [i, law] of laws.entries()) {
|
|
2544
2950
|
const full = instances[law.name], focus = law.forall ? [`${law.forall}#`] : [];
|
|
2545
2951
|
const few = triggered(plainOf(m), laws, full, inst => obligations(m, laws, { ...instances, [law.name]: inst }, i), focus);
|
|
2952
|
+
if (modelsTrace)
|
|
2953
|
+
console.error(`[models] law ${law.name}: ${Object.values(full).reduce((a, ks) => a + ks.length, 0)} keys, ${Object.values(few).reduce((a, ks) => a + ks.length, 0)} triggered`);
|
|
2546
2954
|
// (thinned instances first, then the full ones; each asked again at the tuples its models ask for)
|
|
2547
2955
|
for (const inst of sameInstances(few, full) ? [full] : [few, full]) {
|
|
2548
2956
|
const { result: a, inst: at } = withModels(m, laws, inst, i2 => obligations(m, laws, { ...instances, [law.name]: i2 }, i), growAll, grownModel, grownSettled);
|
|
@@ -2923,12 +3331,12 @@ export function certLoop(l, laws, rank, trees, usualMs) {
|
|
|
2923
3331
|
function readKeysC(c) {
|
|
2924
3332
|
const out = [];
|
|
2925
3333
|
const inE = (e) => { if (e.op === 'get')
|
|
2926
|
-
(out.push(e.key), inE(e.key));
|
|
3334
|
+
(out.push(...tupleParts(e.key)), inE(e.key));
|
|
2927
3335
|
else if (e.op === 'if')
|
|
2928
3336
|
(inC(e.c), inE(e.then), inE(e.else));
|
|
2929
|
-
else if (e.op === 'mul')
|
|
3337
|
+
else if (e.op === 'mul' || e.op === 'proj')
|
|
2930
3338
|
inE(e.a);
|
|
2931
|
-
else if ('
|
|
3339
|
+
else if ('b' in e)
|
|
2932
3340
|
(inE(e.a), inE(e.b)); };
|
|
2933
3341
|
const inC = (x) => (x.op === 'flag' || x.op === 'all' ? undefined : x.op === 'not' ? inC(x.x) : x.op === 'and' || x.op === 'or' ? x.xs.forEach(inC) : (inE(x.a), inE(x.b)));
|
|
2934
3342
|
inC(c);
|
|
@@ -2964,7 +3372,9 @@ export function concretize(goal, bound = 12) {
|
|
|
2964
3372
|
case 'mul': return e.k * ev(e.a, env);
|
|
2965
3373
|
case 'prod': return ev(e.a, env) * ev(e.b, env);
|
|
2966
3374
|
case 'if': return holds(e.c, env) ? ev(e.then, env) : ev(e.else, env);
|
|
2967
|
-
case 'get':
|
|
3375
|
+
case 'get':
|
|
3376
|
+
case 'tup':
|
|
3377
|
+
case 'proj': return NaN;
|
|
2968
3378
|
}
|
|
2969
3379
|
};
|
|
2970
3380
|
const holds = (c, env) => c.op === 'not' ? !holds(c.x, env) : c.op === 'and' ? c.xs.every(y => holds(y, env)) : c.op === 'or' ? c.xs.some(y => holds(y, env))
|