gdp-wired 0.50.6 → 0.52.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 +2 -2
- package/dist/core/accel-refute.d.ts +53 -0
- package/dist/core/accel-refute.js +997 -0
- package/dist/core/cli.d.ts +2 -0
- package/dist/core/cli.js +8 -1
- package/dist/core/cycles.d.ts +2 -0
- package/dist/core/cycles.js +12 -9
- package/dist/core/dsl.d.ts +11 -2
- package/dist/core/dsl.js +194 -29
- package/dist/core/fixed-division.d.ts +9 -0
- package/dist/core/fixed-division.js +87 -0
- package/dist/core/fixed-integers.d.ts +33 -0
- package/dist/core/fixed-integers.js +279 -0
- package/dist/core/live-support.d.ts +5 -0
- package/dist/core/live-support.js +148 -0
- package/dist/core/live.js +194 -37
- package/dist/core/lockstep.d.ts +5 -0
- package/dist/core/lockstep.js +23 -0
- package/dist/core/machine.d.ts +1 -0
- package/dist/core/machine.js +33 -3
- package/dist/core/ordered-pair-seeds.d.ts +2 -0
- package/dist/core/ordered-pair-seeds.js +109 -0
- package/dist/core/proof-output.d.ts +38 -1
- package/dist/core/proof-output.js +38 -0
- package/dist/core/search-termination.d.ts +4 -0
- package/dist/core/search-termination.js +1 -1
- package/dist/core/source-check.d.ts +4 -1
- package/dist/core/source-check.js +5 -2
- package/dist/core/sparse-kind-proof.d.ts +7 -0
- package/dist/core/sparse-kind-proof.js +49 -0
- package/dist/core/step-seeds.d.ts +46 -0
- package/dist/core/step-seeds.js +316 -0
- package/dist/kernel/integer-primitives.d.ts +38 -0
- package/dist/kernel/integer-primitives.js +179 -0
- package/dist/kernel/kernel.d.ts +1 -0
- package/dist/kernel/kernel.js +44 -5
- package/dist/kernel/prove.js +13 -0
- package/dist/kernel/word-division-cuts.d.ts +6 -0
- package/dist/kernel/word-division-cuts.js +69 -0
- package/dist/kernel/word-division.d.ts +12 -0
- package/dist/kernel/word-division.js +167 -0
- package/dist/lean-kernel/Cert.lean +222 -0
- package/dist/lean-kernel/Count.lean +1568 -0
- package/dist/lean-kernel/Ghost.lean +373 -0
- package/dist/lean-kernel/GhostMap.lean +426 -0
- package/dist/lean-kernel/Kernel.lean +2881 -0
- package/dist/lean-kernel/Live.lean +1168 -0
- package/dist/lean-kernel/Progress.lean +543 -0
- package/dist/lean-kernel/Sets.lean +1975 -0
- package/dist/lean-kernel/StepLaw.lean +128 -0
- package/dist/lean-kernel/Steps.lean +477 -0
- package/dist/lean-kernel/Sums.lean +916 -0
- package/dist/lean-kernel/emit.d.ts +8 -1
- package/dist/lean-kernel/emit.js +59 -15
- package/dist/lean-kernel/integer-primitives.d.ts +17 -0
- package/dist/lean-kernel/integer-primitives.js +81 -0
- package/dist/lean-kernel/lean-aggregate/FiniteFold.lean +44 -0
- package/dist/lean-kernel/lean-aggregate/GroupedTransport.lean +308 -0
- package/dist/lean-kernel/lean-aggregate/aggregate.d.ts +13 -0
- package/dist/lean-kernel/lean-aggregate/aggregate.js +68 -0
- package/dist/lean-kernel/lean-aggregate/assets.d.ts +5 -0
- package/dist/lean-kernel/lean-aggregate/assets.js +7 -0
- package/dist/lean-kernel/lean-aggregate/audit.d.ts +3 -0
- package/dist/lean-kernel/lean-aggregate/audit.js +13 -0
- package/dist/lean-kernel/lean-aggregate/exact.d.ts +15 -0
- package/dist/lean-kernel/lean-aggregate/exact.js +51 -0
- package/dist/lean-kernel/lean-aggregate/index.d.ts +19 -0
- package/dist/lean-kernel/lean-aggregate/index.js +52 -0
- package/dist/lean-kernel/lean-aggregate/point-fold.d.ts +27 -0
- package/dist/lean-kernel/lean-aggregate/point-fold.js +79 -0
- package/dist/lean-kernel/lean-aggregate/replay-fold.d.ts +2 -0
- package/dist/lean-kernel/lean-aggregate/replay-fold.js +163 -0
- package/dist/lean-kernel/lean-aggregate/source.d.ts +2 -0
- package/dist/lean-kernel/lean-aggregate/source.js +141 -0
- package/dist/lean-kernel/lean-aggregate/weighted-flags.d.ts +2 -0
- package/dist/lean-kernel/lean-aggregate/weighted-flags.js +26 -0
- package/dist/lean-kernel/lean-aggregate/weighted-replay.d.ts +7 -0
- package/dist/lean-kernel/lean-aggregate/weighted-replay.js +53 -0
- package/dist/lean-kernel/lean-aggregate/weighted-seeded.d.ts +30 -0
- package/dist/lean-kernel/lean-aggregate/weighted-seeded.js +64 -0
- package/dist/lean-kernel/lean-aggregate/weighted.d.ts +18 -0
- package/dist/lean-kernel/lean-aggregate/weighted.js +115 -0
- package/dist/lean-kernel/lean-aggregate/worker.d.ts +10 -0
- package/dist/lean-kernel/lean-aggregate/worker.js +46 -0
- package/dist/lean-kernel/library.js +13 -0
- package/dist/lean-kernel/word-division-export.d.ts +26 -0
- package/dist/lean-kernel/word-division-export.js +172 -0
- package/dist/lean-kernel/word-division-lower.d.ts +10 -0
- package/dist/lean-kernel/word-division-lower.js +103 -0
- package/docs/lean-aggregate.md +29 -0
- package/package.json +6 -1
package/dist/core/live.js
CHANGED
|
@@ -9,15 +9,20 @@
|
|
|
9
9
|
// - a state variable alone, and the constant 0.
|
|
10
10
|
// The steps are taken from runs from the start and from random states the laws allow, where `from` holds and `to`
|
|
11
11
|
// doesn't (the kernel's goals are about every such state, reached or not): a helpful request's in its region must
|
|
12
|
-
// lower the rank
|
|
12
|
+
// lower the rank unless strong fairness permits an upward handoff. Under weak fairness a step that leaves a
|
|
13
|
+
// region must also lower; the remaining steps must not raise the rank. Components are
|
|
13
14
|
// chosen in order, each one no step raises that lowers the most of those left. The rank is then the kernel's
|
|
14
15
|
// (proveLiveness), with the invariants inferLaws finds where its goals need some. Nothing new is trusted.
|
|
15
16
|
import { evalC, evalE, interpret, seeded, walk } from "./machine.js";
|
|
16
|
-
import { falseC, livenessHelpful, livenessParts, substC, substE } from "../kernel/kernel.js";
|
|
17
|
+
import { falseC, livenessHelpful, livenessParts, plainOf, substC, substE } from "../kernel/kernel.js";
|
|
17
18
|
import { clock, proveLiveness, proveProgress, proveStepLaws, withDeadline } from "../kernel/prove.js";
|
|
18
19
|
import { allowed, watch } from "./watch.js";
|
|
19
20
|
import { showE } from "./cycles.js";
|
|
20
21
|
import { inferLaws } from "./infer.js";
|
|
22
|
+
import { isSourceBound } from "./source-check.js";
|
|
23
|
+
import { withStageDeadline } from "./dsl-deadline.js";
|
|
24
|
+
import { BudgetExpired, WallExpired } from "./wall.js";
|
|
25
|
+
import { WatchdogExpired } from "./search-watchdog.js";
|
|
21
26
|
const id = (t) => JSON.stringify(t);
|
|
22
27
|
const k = (value) => ({ op: 'const', value }), v = (name) => ({ op: 'var', name });
|
|
23
28
|
const sub = (a, b) => ({ op: 'sub', a, b }), plus1 = (a) => ({ op: 'add', a, b: k(1) });
|
|
@@ -136,7 +141,9 @@ export function leadsByTerms(m, laws, l, draws, opts = {}) {
|
|
|
136
141
|
} };
|
|
137
142
|
const pending = (ps, st) => holds(l.from, ps, st) === true && holds(l.to, ps, st) === false;
|
|
138
143
|
const regions = (ps, st) => helpful.map(h => holds(h.when, ps, st) === true);
|
|
139
|
-
const steps = [];
|
|
144
|
+
const steps = [], finishes = new Set();
|
|
145
|
+
const strong = l.fairness === 'strong';
|
|
146
|
+
const takenIn = (ps, st, input, was) => helpful.flatMap((h, i) => was[i] && Object.entries(h.request).every(([name, term]) => evalE(term, env(ps, st), st, m) === input[name]) ? [i] : []);
|
|
140
147
|
// One step from a pending state: a helpful request in its region (its named inputs at the state, the rest drawn), or
|
|
141
148
|
// any request; kept where it stays pending.
|
|
142
149
|
const take = (ps, st, bound, h) => {
|
|
@@ -149,9 +156,20 @@ export function leadsByTerms(m, laws, l, draws, opts = {}) {
|
|
|
149
156
|
if (!allowed(m, run, ps, st, input))
|
|
150
157
|
continue;
|
|
151
158
|
const next = run.step(ps, st, input);
|
|
152
|
-
if (pending(ps, st)
|
|
153
|
-
const was = regions(ps, st),
|
|
154
|
-
|
|
159
|
+
if (pending(ps, st)) {
|
|
160
|
+
const was = regions(ps, st), nextPending = pending(ps, next);
|
|
161
|
+
if (strong) {
|
|
162
|
+
const from = takenIn(ps, st, input, was);
|
|
163
|
+
if (nextPending)
|
|
164
|
+
steps.push({ ps, s: st, t: next, lower: false, handoffs: { from, to: regions(ps, next).flatMap((b, i) => b ? [i] : []) } });
|
|
165
|
+
else if (holds(l.to, ps, next))
|
|
166
|
+
for (const i of from)
|
|
167
|
+
finishes.add(i);
|
|
168
|
+
}
|
|
169
|
+
else if (nextPending) {
|
|
170
|
+
const now = regions(ps, next);
|
|
171
|
+
steps.push({ ps, s: st, t: next, lower: h !== undefined || was.some((r, i) => r && !now[i]) });
|
|
172
|
+
}
|
|
155
173
|
}
|
|
156
174
|
return next;
|
|
157
175
|
}
|
|
@@ -202,6 +220,26 @@ export function leadsByTerms(m, laws, l, draws, opts = {}) {
|
|
|
202
220
|
found++;
|
|
203
221
|
take(ps, st, bound, pick(ps, st));
|
|
204
222
|
}
|
|
223
|
+
// Strong fairness allows an ordinary step to leave a region at the same rank. A helpful step may also keep
|
|
224
|
+
// the rank if it hands off to a strictly higher-level region. Propose levels from sampled distances to a
|
|
225
|
+
// helping region observed to finish; the unchanged checker must prove each handoff, descent and enabledness.
|
|
226
|
+
const dist = helpful.map((_, i) => finishes.has(i) ? 0 : helpful.length);
|
|
227
|
+
if (strong && !l.levels)
|
|
228
|
+
for (let round = 0; round < helpful.length; round++) {
|
|
229
|
+
for (const { handoffs } of steps)
|
|
230
|
+
if (handoffs)
|
|
231
|
+
for (const i of handoffs.from)
|
|
232
|
+
for (const j of handoffs.to) {
|
|
233
|
+
dist[i] = Math.min(dist[i], dist[j] + 1);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
const levels = strong ? l.levels ?? dist.map(d => helpful.length - d) : undefined;
|
|
237
|
+
const mustLower = (from, to) => from.some(i => !to.some(j => levels[i] < levels[j]));
|
|
238
|
+
if (levels)
|
|
239
|
+
for (const s of steps)
|
|
240
|
+
if (s.handoffs)
|
|
241
|
+
s.lower = mustLower(s.handoffs.from, s.handoffs.to);
|
|
242
|
+
const goalOf = (rank) => ({ ...l, rank, ...(levels ? { levels } : {}) });
|
|
205
243
|
// A goal's counterexample as a step (the kernel names a cell by its map and its key's linear form, `M[d:1,1:2]`): the
|
|
206
244
|
// state, params and request it gives (0 where it gives none), stepped by the interpreter. The step a goal fails at
|
|
207
245
|
// is one the rank must allow; so the ranks are chosen again with it, a few rounds.
|
|
@@ -228,7 +266,9 @@ export function leadsByTerms(m, laws, l, draws, opts = {}) {
|
|
|
228
266
|
if (!pending(ps, next))
|
|
229
267
|
return false;
|
|
230
268
|
const was = regions(ps, st), now = regions(ps, next), h = /helpful request (\d+)/.exec(goal);
|
|
231
|
-
|
|
269
|
+
const lower = strong ? (h ? mustLower([Number(h[1]) - 1], now.flatMap((b, i) => b ? [i] : [])) :
|
|
270
|
+
mustLower(takenIn(ps, st, input, was), now.flatMap((b, i) => b ? [i] : []))) : !!h || was.some((r, i) => r && !now[i]);
|
|
271
|
+
steps.push({ ps, s: st, t: next, lower });
|
|
232
272
|
return true;
|
|
233
273
|
}
|
|
234
274
|
catch {
|
|
@@ -300,10 +340,10 @@ export function leadsByTerms(m, laws, l, draws, opts = {}) {
|
|
|
300
340
|
break;
|
|
301
341
|
tried.add(id(rank));
|
|
302
342
|
best ??= rank;
|
|
303
|
-
last =
|
|
304
|
-
if (last
|
|
305
|
-
return { proven: true, checked: true, rank, laws: [], found: { rank: text(rank), laws: [] } };
|
|
306
|
-
if (last
|
|
343
|
+
last = withStageDeadline(clock() + ms * (strong ? 0.9 : 1 / 3), () => proveLiveness(m, laws, goalOf(rank)), () => undefined);
|
|
344
|
+
if (last?.proven && last.checked)
|
|
345
|
+
return { proven: true, checked: true, rank, levels, laws: [], evidence: { machine: m, laws, liveness: goalOf(rank), report: last }, found: { rank: text(rank), laws: [] } };
|
|
346
|
+
if (last?.failed?.witness && fromWitness(last.failed.witness, last.failed.goal))
|
|
307
347
|
learnt = true;
|
|
308
348
|
}
|
|
309
349
|
if (!learnt)
|
|
@@ -312,23 +352,111 @@ export function leadsByTerms(m, laws, l, draws, opts = {}) {
|
|
|
312
352
|
if (!best)
|
|
313
353
|
return { proven: false, checked: false, why: `no rank of the rule's terms that the ${steps.length} steps sampled allow` };
|
|
314
354
|
// the first rank with the invariants its goals need
|
|
315
|
-
const rank = best, lv =
|
|
355
|
+
const rank = best, lv = goalOf(rank), ms = until - clock();
|
|
316
356
|
if (ms > 300) {
|
|
317
357
|
const { stay, cover, helps } = livenessParts(m, lv), inf = inferLaws(draws.rule ?? m, laws, [stay, cover, ...helps], { ms });
|
|
318
358
|
const kept = inf.found.filter(f => !f.law.some?.length), more = kept.map(f => f.law);
|
|
319
359
|
if (inf.proven && more.length) {
|
|
320
|
-
const again =
|
|
321
|
-
if (again
|
|
322
|
-
return { proven: true, checked: true, rank, laws: more, found: { rank: text(rank), laws: kept.map(f => f.text) } };
|
|
360
|
+
const again = withStageDeadline(until, () => proveLiveness(m, [...laws, ...more], lv), () => undefined);
|
|
361
|
+
if (again?.proven && again.checked)
|
|
362
|
+
return { proven: true, checked: true, rank, levels: lv.levels, laws: more, evidence: { machine: m, laws: [...laws, ...more], liveness: lv, report: again }, found: { rank: text(rank), laws: kept.map(f => f.text) } };
|
|
323
363
|
}
|
|
324
364
|
}
|
|
325
365
|
const failed = last?.failed ? { goal: last.failed.goal, stuck: last.failed.stuck, ...(last.failed.witness ? { witness: last.failed.witness } : {}) } : {};
|
|
326
366
|
return { proven: false, checked: false, rank, found: { rank: text(rank), laws: [] }, ...failed, why: `rank ${text(rank)} of the rule's terms, from ${steps.length} steps sampled; the kernel's goals not proven with it` };
|
|
327
367
|
}
|
|
368
|
+
const historyQueries = (m) => Object.keys(m.history?.queries ?? {});
|
|
369
|
+
const unsupportedRingMachine = (m) => {
|
|
370
|
+
// Facts can restrict which parameters start a run. No name or bounded
|
|
371
|
+
// sample certifies a universally quantified fact. Refuse derived inputs,
|
|
372
|
+
// external functions, primitive traps and non-scalar client requests too.
|
|
373
|
+
if (m.primitiveFailure || m.facts?.length || Object.keys(m.fns ?? {}).length || Object.keys(m.leasts ?? {}).length ||
|
|
374
|
+
m.inputMaps?.length || Object.keys(m.counts ?? {}).length || Object.keys(m.sums ?? {}).length)
|
|
375
|
+
return true;
|
|
376
|
+
if (!m.history)
|
|
377
|
+
return false;
|
|
378
|
+
// The progress check supplies placeholders for original history queries.
|
|
379
|
+
// It can show enabledness only if the real assumption cannot read them.
|
|
380
|
+
let flag = false;
|
|
381
|
+
walk(m.assume, o => { if (o.op === 'flag')
|
|
382
|
+
flag = true; });
|
|
383
|
+
return !!(m.history.prune || flag || m.assume && readsC(m.assume, historyQueries(m)) ||
|
|
384
|
+
Object.values(m.step.maps ?? {}).some(w => 'each' in w));
|
|
385
|
+
};
|
|
386
|
+
const natural = (x) => typeof x === 'number' && Number.isSafeInteger(x) && x >= 0;
|
|
387
|
+
function ringScope(m) {
|
|
388
|
+
const plain = plainOf(m), vars = new Set([...m.params, ...Object.keys(plain.state)]);
|
|
389
|
+
// Quantified/flag conditions and functions are not client policy syntax;
|
|
390
|
+
// all reads must be of explicit old state or parameters at every step.
|
|
391
|
+
function term(t, depth = 0) {
|
|
392
|
+
if (!t || depth > 64)
|
|
393
|
+
return false;
|
|
394
|
+
const sub = (x) => term(x, depth + 1);
|
|
395
|
+
switch (t.op) {
|
|
396
|
+
case 'var': return vars.has(t.name);
|
|
397
|
+
case 'const': return natural(t.value);
|
|
398
|
+
case 'get': return Object.hasOwn(plain.maps ?? {}, t.map) && sub(t.key);
|
|
399
|
+
case 'if': return condition(t.c, depth + 1) && sub(t.then) && sub(t.else);
|
|
400
|
+
case 'mul': return natural(t.k) && sub(t.a);
|
|
401
|
+
case 'proj': return (t.k === 0 || t.k === 1) && sub(t.a);
|
|
402
|
+
case 'add':
|
|
403
|
+
case 'sub':
|
|
404
|
+
case 'max':
|
|
405
|
+
case 'min':
|
|
406
|
+
case 'prod':
|
|
407
|
+
case 'tup': return sub(t.a) && sub(t.b);
|
|
408
|
+
default: return false;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
function condition(c, depth = 0) {
|
|
412
|
+
if (!c || depth > 64)
|
|
413
|
+
return false;
|
|
414
|
+
switch (c.op) {
|
|
415
|
+
case '<':
|
|
416
|
+
case '<=':
|
|
417
|
+
case '=': return term(c.a, depth + 1) && term(c.b, depth + 1);
|
|
418
|
+
case 'and':
|
|
419
|
+
case 'or': return Array.isArray(c.xs) && c.xs.every(x => condition(x, depth + 1));
|
|
420
|
+
case 'not': return condition(c.x, depth + 1);
|
|
421
|
+
case 'all':
|
|
422
|
+
case 'flag': return false;
|
|
423
|
+
default: return false;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return { term, condition };
|
|
427
|
+
}
|
|
428
|
+
const postState = (m) => {
|
|
429
|
+
const plain = plainOf(m); // log sums are state in the actual postcondition
|
|
430
|
+
return { vars: Object.fromEntries(Object.keys(plain.state).map(x => [x, v(`${x}'`)])),
|
|
431
|
+
maps: Object.fromEntries(Object.keys(plain.maps ?? {}).map(map => [map, (key) => ({ op: 'get', map: `${map}'`, key })])), flags: {} };
|
|
432
|
+
};
|
|
328
433
|
export function refutedBy(m, laws, l, ring) {
|
|
329
|
-
|
|
434
|
+
try {
|
|
435
|
+
return checkRing(m, laws, l, ring);
|
|
436
|
+
}
|
|
437
|
+
catch (e) {
|
|
438
|
+
if (e instanceof BudgetExpired || e instanceof WallExpired || e instanceof WatchdogExpired)
|
|
439
|
+
throw e;
|
|
440
|
+
return { refuted: false, why: String(e) };
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
function checkRing(m, laws, l, ring) {
|
|
444
|
+
if (isSourceBound(m, [...laws, l]))
|
|
445
|
+
return { refuted: false, why: 'a model ring is not a source counterexample' };
|
|
446
|
+
if (unsupportedRingMachine(m))
|
|
447
|
+
return { refuted: false, why: 'ring proof does not support these machine facts, functions, requests or history effects' };
|
|
448
|
+
livenessParts(m, { ...l, rank: [k(0)] });
|
|
449
|
+
const n = ring.sets.length, queries = historyQueries(m), inputs = m.inputs.filter(i => !queries.includes(i));
|
|
450
|
+
const run = interpret(m), helpful = livenessHelpful(l), scope = ringScope(m);
|
|
451
|
+
const exactNaturals = (xs, names) => Object.keys(xs).length === names.length && names.every(name => Object.hasOwn(xs, name) && natural(xs[name]));
|
|
330
452
|
if (!n)
|
|
331
453
|
return { refuted: false, why: 'no set' };
|
|
454
|
+
if (!exactNaturals(ring.params, m.params) || ring.stem.some(req => !exactNaturals(req, inputs)))
|
|
455
|
+
return { refuted: false, why: 'malformed ring parameters or stem request' };
|
|
456
|
+
if (![l.from, l.to, ...helpful.map(h => h.when), ...ring.sets.map(s => s.holds)].every(c => scope.condition(c)))
|
|
457
|
+
return { refuted: false, why: 'ring predicates must read only the state and parameters' };
|
|
458
|
+
if ([...helpful, ...ring.sets].some(s => Object.entries(s.request).some(([name, t]) => !inputs.includes(name) || !scope.term(t))))
|
|
459
|
+
return { refuted: false, why: 'ring request needs a client field and a state-relative value' };
|
|
332
460
|
if (m.pre && !evalC(m.pre, ring.params, { scalars: {}, maps: {} }, m))
|
|
333
461
|
return { refuted: false, why: 'the params break the precondition' };
|
|
334
462
|
let st = run.init(ring.params);
|
|
@@ -340,25 +468,46 @@ export function refutedBy(m, laws, l, ring) {
|
|
|
340
468
|
const at = (c) => evalC(c, { ...ring.params, ...st.scalars }, st, m);
|
|
341
469
|
if (!at(l.from) || at(l.to) || !at(ring.sets[0].holds))
|
|
342
470
|
return { refuted: false, why: 'the stem ends where `from` doesn\'t hold, `to` does, or outside the first set' };
|
|
343
|
-
const after =
|
|
344
|
-
const
|
|
471
|
+
const after = postState(m);
|
|
472
|
+
const clientRequest = (r) => Object.fromEntries(inputs.map(i => [i, r[i] ?? k(0)]));
|
|
473
|
+
// Replay a full initial lap in the actual interpreter, including request
|
|
474
|
+
// evaluation and hidden queries. Beyond that lap the certificate is about
|
|
475
|
+
// the kernel's unbounded natural model, not indefinite host execution.
|
|
476
|
+
for (const [j, set] of ring.sets.entries()) {
|
|
477
|
+
const req = Object.fromEntries(Object.entries(clientRequest(set.request)).map(([name, t]) => [name, evalE(t, { ...ring.params, ...st.scalars }, st, m)]));
|
|
478
|
+
if (!Object.values(req).every(natural) || !allowed(m, run, ring.params, st, req))
|
|
479
|
+
return { refuted: false, why: `ring ${j + 1}: its first recurring request is not allowed` };
|
|
480
|
+
st = run.step(ring.params, st, req);
|
|
481
|
+
if (!at(l.from) || at(l.to) || !at(ring.sets[(j + 1) % n].holds))
|
|
482
|
+
return { refuted: false, why: `ring ${j + 1}: interpreter did not stay pending and move into the next set` };
|
|
483
|
+
}
|
|
345
484
|
for (const [j, set] of ring.sets.entries()) {
|
|
346
485
|
const next = ring.sets[(j + 1) % n].holds, then = { op: 'and', xs: [substC(next, after), substC(l.from, after), { op: 'not', x: substC(l.to, after) }] };
|
|
347
|
-
const p = proveProgress(m, laws, { name: `${l.name}: ring ${j + 1}`, when: set.holds, then,
|
|
486
|
+
const p = proveProgress(m, laws, { name: `${l.name}: ring ${j + 1}`, when: set.holds, then,
|
|
487
|
+
request: { ...clientRequest(set.request), ...Object.fromEntries(queries.map(q => [q, k(0)])) } });
|
|
348
488
|
if (!p.proven || !p.checked)
|
|
349
489
|
return { refuted: false, why: `ring ${j + 1}: its request not proven allowed and into the next set${p.failed ? ` (${p.failed.goal})` : ''}` };
|
|
490
|
+
if (queries.length) {
|
|
491
|
+
// The interpreter computes the queries from its log. Recheck closure with every query left free, since
|
|
492
|
+
// the placeholder in progress's total request does not bind the original log.
|
|
493
|
+
const req = Object.entries(clientRequest(set.request)).map(([x, t]) => ({ op: '=', a: v(x), b: t }));
|
|
494
|
+
const allQueries = proveStepLaws(m, laws, [{ name: `${l.name}: ring ${j + 1} with query values free`,
|
|
495
|
+
when: { op: 'and', xs: [set.holds, ...req] }, then }]);
|
|
496
|
+
if (!allQueries.proven || !allQueries.checked)
|
|
497
|
+
return { refuted: false, why: `ring ${j + 1}: closure not proven without pinning history queries` };
|
|
498
|
+
}
|
|
350
499
|
}
|
|
351
500
|
// "no allowed request from a state of R_j meets these": a step law whose `then` is false
|
|
352
501
|
const never = (what, ...cs) => { const r = proveStepLaws(m, laws, [{ name: `${l.name}: ${what}`, when: { op: 'and', xs: cs }, then: falseC }]); return r.proven && r.checked; };
|
|
353
502
|
const named = (req) => Object.entries(req).map(([x, t]) => ({ op: '=', a: v(x), b: t }));
|
|
354
503
|
const fair = new Set(['taken', 'weak', 'strong']);
|
|
355
504
|
for (const [i, h] of helpful.entries()) {
|
|
356
|
-
const takes = ring.sets.map(set => Object.entries(h.request).every(([x, t]) => id(
|
|
505
|
+
const takes = ring.sets.map(set => Object.entries(h.request).every(([x, t]) => id(clientRequest(set.request)[x]) === id(t)));
|
|
357
506
|
if (!takes.some(Boolean))
|
|
358
507
|
fair.delete('taken');
|
|
359
508
|
if (!takes.some(Boolean) && !ring.sets.some(set => never(`helpful request ${i + 1} not allowed`, set.holds, ...named(h.request))))
|
|
360
509
|
fair.delete('weak');
|
|
361
|
-
if (!ring.sets.some((set, j) => takes[j] && never(`helpful request ${i + 1} taken in its region`, set.holds, { op: 'not', x: h.when }, ...named(
|
|
510
|
+
if (!ring.sets.some((set, j) => takes[j] && never(`helpful request ${i + 1} taken in its region`, set.holds, { op: 'not', x: h.when }, ...named(clientRequest(set.request))))
|
|
362
511
|
&& !ring.sets.every(set => never(`helpful request ${i + 1} not allowed in its region`, set.holds, h.when, ...named(h.request))))
|
|
363
512
|
fair.delete('strong');
|
|
364
513
|
if (!fair.size)
|
|
@@ -381,7 +530,10 @@ export function refutedBy(m, laws, l, ring) {
|
|
|
381
530
|
// literal of R_{j+1} that R_j's request isn't proven to keep is dropped (Houdini, round the cycle) until each is kept;
|
|
382
531
|
// then refutedBy checks the ring. The first ring refuted; else the first checked; undefined where no cycle is found.
|
|
383
532
|
export function findRing(m, laws, l, draws, opts = {}) {
|
|
533
|
+
if (unsupportedRingMachine(m) || isSourceBound(m, [...laws, l]))
|
|
534
|
+
return;
|
|
384
535
|
const until = clock() + (opts.ms ?? 10_000), rand = seeded(17), run = interpret(m), helpful = livenessHelpful(l), H = helpful.length;
|
|
536
|
+
const queries = new Set(historyQueries(m)), client = m.inputs.filter(i => !queries.has(i));
|
|
385
537
|
const env = (ps, st) => ({ ...ps, ...st.scalars });
|
|
386
538
|
const holds = (c, ps, st) => { try {
|
|
387
539
|
return evalC(c, env(ps, st), st, m);
|
|
@@ -397,29 +549,32 @@ export function findRing(m, laws, l, draws, opts = {}) {
|
|
|
397
549
|
} };
|
|
398
550
|
const pending = (ps, st) => holds(l.from, ps, st) === true && holds(l.to, ps, st) === false;
|
|
399
551
|
const full = (r) => Object.fromEntries(m.inputs.map(i => [i, r[i] ?? k(0)]));
|
|
400
|
-
const concrete = (r, ps, st) => Object.fromEntries(
|
|
552
|
+
const concrete = (r, ps, st) => Object.fromEntries(client.map(i => [i, evalE(r[i] ?? k(0), env(ps, st), st, m)]));
|
|
401
553
|
const atoms = new Map();
|
|
402
|
-
const add = (t) => walk(t, o => {
|
|
403
|
-
|
|
404
|
-
|
|
554
|
+
const add = (t, subst) => walk(t, o => {
|
|
555
|
+
if ((o.op !== '<' && o.op !== '<=' && o.op !== '=') || typeof o.a !== 'object' || typeof o.b !== 'object')
|
|
556
|
+
return;
|
|
557
|
+
const atom = o;
|
|
558
|
+
// Specialize only flag-free comparisons, including those in the separately collected flag definitions.
|
|
559
|
+
if (id(atom).includes('"op":"flag"'))
|
|
560
|
+
return;
|
|
561
|
+
const c = subst ? substC(atom, subst) : atom;
|
|
562
|
+
if (!readsC(c, m.inputs) && scope.condition(c) && !constant(c))
|
|
405
563
|
atoms.set(id(c), c);
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
for (const e of Object.values(m.step.state ?? {}))
|
|
413
|
-
add(substE(e, sb));
|
|
414
|
-
}
|
|
564
|
+
});
|
|
565
|
+
const scope = ringScope(m);
|
|
566
|
+
const parts = [m.assume, m.step];
|
|
567
|
+
add([l.from, l.to, helpful.map(h => h.when), parts]);
|
|
568
|
+
for (const h of helpful)
|
|
569
|
+
add(parts, { vars: full(h.request), maps: {}, flags: {} });
|
|
415
570
|
const lits = [...atoms.values()], litsAt = (ps, st) => lits.flatMap(c => { const b = holds(c, ps, st); return b === undefined ? [] : [b ? c : { op: 'not', x: c }]; });
|
|
416
571
|
const keyAt = (ps, st) => lits.map(c => { const b = holds(c, ps, st); return b === undefined ? 2 : +b; }).join('');
|
|
417
|
-
const after =
|
|
572
|
+
const after = postState(m);
|
|
418
573
|
const fair = (cyc) => helpful.every((_, h) => (l.fairness === 'strong'
|
|
419
574
|
? cyc.some(a => a.taken[h] && a.region[h]) || cyc.every(a => !(a.allows[h] && a.region[h]))
|
|
420
575
|
: cyc.some(a => a.taken[h] || !a.allows[h])));
|
|
421
576
|
// a request as terms at a state
|
|
422
|
-
const termsOf = (req, ps, st) => Object.fromEntries(
|
|
577
|
+
const termsOf = (req, ps, st) => Object.fromEntries(client.map(i => [i, helpful.map(h => h.request[i]).find(t => t && value(t, ps, st) === req[i]) ?? k(req[i])]));
|
|
423
578
|
const kept = (when, then, request) => {
|
|
424
579
|
const p = withDeadline(Math.min(until, clock() + 2000), () => proveProgress(m, laws, { name: 'kept', when: { op: 'and', xs: when }, then: { op: 'and', xs: then.map(c => substC(c, after)) }, request: full(request) }));
|
|
425
580
|
return p.proven && p.checked;
|
|
@@ -440,7 +595,7 @@ export function findRing(m, laws, l, draws, opts = {}) {
|
|
|
440
595
|
}
|
|
441
596
|
}
|
|
442
597
|
const ring = { params: ps, stem, sets: sets.map((s, j) => ({ holds: { op: 'and', xs: s }, request: reqs[j] })) };
|
|
443
|
-
return { ring, verdict: refutedBy(m, laws, l, ring) };
|
|
598
|
+
return { ring, verdict: withDeadline(until, () => refutedBy(m, laws, l, ring)) };
|
|
444
599
|
};
|
|
445
600
|
// the cycle run again from where it closed, its requests as terms, for 3,000 steps: each allowed, pending after, at
|
|
446
601
|
// the abstraction it had (so the time passes every deadline small params allow: a cycle a deadline ends is none)
|
|
@@ -448,6 +603,8 @@ export function findRing(m, laws, l, draws, opts = {}) {
|
|
|
448
603
|
try {
|
|
449
604
|
for (let n = 0; n < 3000;)
|
|
450
605
|
for (const [j, a] of cyc.entries()) {
|
|
606
|
+
if (clock() >= until)
|
|
607
|
+
return false;
|
|
451
608
|
const q = concrete(reqs[j], ps, st);
|
|
452
609
|
if (keyAt(ps, st) !== a.key || !allowed(m, run, ps, st, q))
|
|
453
610
|
return false;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { retainFamily } from "./proof-output.js";
|
|
2
|
+
import { clock, withDeadline } from "../kernel/prove.js";
|
|
3
|
+
import { kindConjuncts, sparseKindProof } from "./sparse-kind-proof.js";
|
|
4
|
+
const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
5
|
+
function renaming(e, self) {
|
|
6
|
+
return JSON.stringify(e, (_key, node) => {
|
|
7
|
+
const t = node;
|
|
8
|
+
return t?.op === 'var' && t.name === self ? { op: '$self' } : node;
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
export function tryLockstepScalars(m, law, kinds, ms) {
|
|
12
|
+
if (law.holds.op !== '=' || law.forall || law.keys?.length || law.some?.length || law.finite ||
|
|
13
|
+
law.holds.a.op !== 'var' || law.holds.b.op !== 'var')
|
|
14
|
+
return;
|
|
15
|
+
const a = law.holds.a.name, b = law.holds.b.name, u = m.step.state?.[a], v = m.step.state?.[b];
|
|
16
|
+
if (!u || !v || a === b || !m.state[a] || !m.state[b] || !same(m.state[a], m.state[b]) || renaming(u, a) !== renaming(v, b))
|
|
17
|
+
return;
|
|
18
|
+
const until = clock() + ms;
|
|
19
|
+
return withDeadline(until, () => {
|
|
20
|
+
const laws = [...kindConjuncts(kinds, [law]), law], proof = sparseKindProof(m, laws, until);
|
|
21
|
+
return proof ? retainFamily({ proven: true, checked: true }, m, laws, proof) : undefined;
|
|
22
|
+
});
|
|
23
|
+
}
|
package/dist/core/machine.d.ts
CHANGED
package/dist/core/machine.js
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import { boundOf, crossFree, eachParts, finiteBound, finiteLawBound, historyMachine, tuple, keyTerm, mapArities, plainKey, scope, sumRuntime, tupleParts } from "../kernel/kernel.js";
|
|
9
9
|
import { add, env, eq, k, le, lt, nat, scale, show, solve, sub, v } from "./solver.js";
|
|
10
10
|
import { runtimeWall } from "./runtime-wall.js";
|
|
11
|
+
import { parseWordDivisionName, wordDivisionArgs, wordDivisionValue } from "../kernel/word-division.js";
|
|
11
12
|
import { traceNow } from "./trace-clock.js";
|
|
12
13
|
import { coreMeter } from "./meter.js";
|
|
13
14
|
import { observeEval } from "./work-observe.js";
|
|
@@ -180,7 +181,7 @@ export function withArithmetic(m, more = [], ks = []) {
|
|
|
180
181
|
return { ...m, fns: { ...m.fns, ...fns }, facts: [...(m.facts ?? []), ...facts] };
|
|
181
182
|
}
|
|
182
183
|
// Every term and condition a machine holds.
|
|
183
|
-
export const machineTerms = (m) => [...Object.values(m.state), ...Object.values(m.step.flags ?? {}), ...Object.values(m.step.state ?? {}),
|
|
184
|
+
export const machineTerms = (m) => [...Object.values(m.state), ...(m.primitiveFailure ? [m.primitiveFailure] : []), ...Object.values(m.step.flags ?? {}), ...Object.values(m.step.state ?? {}),
|
|
184
185
|
...Object.values(m.step.maps ?? {}).flatMap(w => ('key' in w ? [w.key, w.value] : [w.value])), ...(m.pre ? [m.pre] : []), ...(m.assume ? [m.assume] : []),
|
|
185
186
|
...(m.facts ?? []).map(f => f.holds), ...(m.history ? [m.history.keep, ...Object.values(m.history.sums), ...Object.values(m.history.queries), ...(m.history.relations ?? []).flatMap(r => [r.when, r.holds])] : [])];
|
|
186
187
|
// A condition at every key, or a value at every key. Where the bound key is read only as a map's key, each key a map
|
|
@@ -340,6 +341,13 @@ export function evalE(e, env, st, m) {
|
|
|
340
341
|
case 'tup': return cpair(evalE(e.a, env, st, m), evalE(e.b, env, st, m));
|
|
341
342
|
case 'proj': return cunpair(evalE(e.a, env, st, m))[e.k];
|
|
342
343
|
case 'get': {
|
|
344
|
+
const intrinsic = parseWordDivisionName(e.map);
|
|
345
|
+
if (intrinsic) {
|
|
346
|
+
const args = wordDivisionArgs(e.key, intrinsic.width);
|
|
347
|
+
if (!args)
|
|
348
|
+
throw new Error(`${e.map} needs a right-nested word division tuple key`);
|
|
349
|
+
return wordDivisionValue(intrinsic.width, intrinsic.part, intrinsic.index, args.map(a => evalE(a, env, st, m)));
|
|
350
|
+
}
|
|
343
351
|
const fn = m.fns && Object.hasOwn(m.fns, e.map) ? m.fns[e.map] : undefined, args = fn?.call ? pairedArgs(e.key, fn.arity) : undefined;
|
|
344
352
|
if (args) {
|
|
345
353
|
const xs = args.map(a => evalE(a, env, st, m));
|
|
@@ -377,6 +385,13 @@ const directCall = (d) => {
|
|
|
377
385
|
case 'max': return Math.max(value(e.a, env), value(e.b, env));
|
|
378
386
|
case 'if': return condition(e.c, env) ? value(e.then, env) : value(e.else, env);
|
|
379
387
|
case 'get': {
|
|
388
|
+
const intrinsic = parseWordDivisionName(e.map);
|
|
389
|
+
if (intrinsic) {
|
|
390
|
+
const args = wordDivisionArgs(e.key, intrinsic.width);
|
|
391
|
+
if (!args)
|
|
392
|
+
throw new Error('invalid compact word-division argument tuple');
|
|
393
|
+
return wordDivisionValue(intrinsic.width, intrinsic.part, intrinsic.index, args.map(a => value(a, env)));
|
|
394
|
+
}
|
|
380
395
|
const fn = d.uses[e.map], args = pairedArgs(e.key, fn.arity ?? 1);
|
|
381
396
|
return args ? callOf(fn)(...args.map(a => value(a, env))) : fnOf(fn)(value(e.key, env));
|
|
382
397
|
}
|
|
@@ -561,6 +576,8 @@ function compileENode(e, m) {
|
|
|
561
576
|
return (env, st) => cunpair(a(env, st))[k];
|
|
562
577
|
}
|
|
563
578
|
case 'get': {
|
|
579
|
+
if (parseWordDivisionName(e.map))
|
|
580
|
+
return (env, st) => evalE(e, env, st, m);
|
|
564
581
|
const map = e.map, key = compileE(e.key, m);
|
|
565
582
|
return (env, st) => {
|
|
566
583
|
const fn = m.fns?.[map];
|
|
@@ -924,6 +941,11 @@ export function interpret(m, checkpoint) {
|
|
|
924
941
|
env[`?${name}`] = evalC(c, env, st, plain) ? 1 : 0;
|
|
925
942
|
if (m.assume && !evalC(m.assume, env, st, plain))
|
|
926
943
|
throw new RangeError(refused(m));
|
|
944
|
+
if (plain.primitiveFailure && evalC(plain.primitiveFailure, env, st, plain)) {
|
|
945
|
+
const error = new RangeError(`${m.name}: fixed-width primitive failed`);
|
|
946
|
+
error.name = 'PrimitiveFailure';
|
|
947
|
+
throw error;
|
|
948
|
+
}
|
|
927
949
|
const scalars = { ...st.scalars };
|
|
928
950
|
for (const [name, e] of Object.entries(plain.step.state ?? {}))
|
|
929
951
|
scalars[name] = evalE(e, env, st, plain);
|
|
@@ -1032,6 +1054,13 @@ function compileJsMode(m, persistent, cells) {
|
|
|
1032
1054
|
case 'tup': return `$p(${ex(e.a)}, ${ex(e.b)})`;
|
|
1033
1055
|
case 'proj': return `$u(${ex(e.a)})[${e.k}]`;
|
|
1034
1056
|
case 'get': {
|
|
1057
|
+
const intrinsic = parseWordDivisionName(e.map);
|
|
1058
|
+
if (intrinsic) {
|
|
1059
|
+
const args = wordDivisionArgs(e.key, intrinsic.width);
|
|
1060
|
+
if (!args)
|
|
1061
|
+
throw new Error(`${e.map} needs a right-nested word division tuple key`);
|
|
1062
|
+
return `$wordDivisionValue(${intrinsic.width}, ${id(intrinsic.part)}, ${intrinsic.index}, [${args.map(ex).join(', ')}])`;
|
|
1063
|
+
}
|
|
1035
1064
|
const fn = m.fns && Object.hasOwn(m.fns, e.map) ? m.fns[e.map] : undefined, args = fn?.call ? pairedArgs(e.key, fn.arity) : undefined;
|
|
1036
1065
|
if (args)
|
|
1037
1066
|
return `$n(fnc${fns.indexOf(e.map)}(${args.map(ex).join(', ')}), ${id(`${e.map}()`)})`;
|
|
@@ -1130,7 +1159,7 @@ function compileJsMode(m, persistent, cells) {
|
|
|
1130
1159
|
}
|
|
1131
1160
|
lines.push(...flags.map((name, i) => `const flag${i} = ${cx(plain.step.flags[name])};`),
|
|
1132
1161
|
// The rule's own assumption, as `allowed` (core/watch.ts) reads it (a history's machine assumes more: its relations).
|
|
1133
|
-
...(m.assume ? [`if (!${cx(m.assume)}) throw new RangeError(${id(refused(m))});`] : []), `const scalars = {...s, ${Object.entries(plain.step.state ?? {}).map(([name, e]) => `${id(name)}: ${ex(e)}`).join(', ')}};`, ...[...sharedBreaks].map(([text, bk]) => `const ${bk} = ${breaksJs(JSON.parse(text))};`), ...writes.map(([name, w], i) => ('each' in w ? `const write${i} = ${every(w, sharedOf(name))};` : `const key${i} = ${ex(w.key)}, value${i} = ${ex(w.value)};`)));
|
|
1162
|
+
...(m.assume ? [`if (!${cx(m.assume)}) throw new RangeError(${id(refused(m))});`] : []), ...(plain.primitiveFailure ? [`if (${cx(plain.primitiveFailure)}) { const e = new RangeError(${id(`${m.name}: fixed-width primitive failed`)}); e.name = "PrimitiveFailure"; throw e; }`] : []), `const scalars = {...s, ${Object.entries(plain.step.state ?? {}).map(([name, e]) => `${id(name)}: ${ex(e)}`).join(', ')}};`, ...[...sharedBreaks].map(([text, bk]) => `const ${bk} = ${breaksJs(JSON.parse(text))};`), ...writes.map(([name, w], i) => ('each' in w ? `const write${i} = ${every(w, sharedOf(name))};` : `const key${i} = ${ex(w.key)}, value${i} = ${ex(w.value)};`)));
|
|
1134
1163
|
if (h)
|
|
1135
1164
|
lines.push(`const keep = ${cx(h.keep)};`);
|
|
1136
1165
|
if (persistent)
|
|
@@ -1173,7 +1202,8 @@ export function step(params, state, input${hasFinite(plain) ? ', $finite = () =>
|
|
|
1173
1202
|
}
|
|
1174
1203
|
`;
|
|
1175
1204
|
return `// Generated from the machine ${m.name}. step consumes its state: keep the one it returns (fork to branch).
|
|
1176
|
-
${runtimeFor(fnDefs + code)}$
|
|
1205
|
+
${runtimeFor(fnDefs + code)}const $wordDivisionValue = ${wordDivisionValue.toString()};
|
|
1206
|
+
${fnDefs}${code}`;
|
|
1177
1207
|
}
|
|
1178
1208
|
// Generated code as a function. Every caller passes source this library generated from its own terms, or a function's
|
|
1179
1209
|
// `js` the rule's author wrote (code, as the rule itself is), never data; the lint's implied-eval rule is waived here
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// Untrusted strengthening proposals for an ordered pair of rows with a shared group key.
|
|
2
|
+
// A successful search must check the original law and this whole family against the same machine.
|
|
3
|
+
import { all, any, not, same, x, get, n } from "./machine.js";
|
|
4
|
+
const isVar = (t, k) => t.op === 'var' && t.name === k;
|
|
5
|
+
const rowRead = (t, k) => t.op === 'get' && isVar(t.key, k) ? t.map : undefined;
|
|
6
|
+
const conjuncts = (c) => c.op === 'and' ? c.xs.flatMap(conjuncts) : [c];
|
|
7
|
+
const forbidden = (c) => {
|
|
8
|
+
if (c.op === 'not')
|
|
9
|
+
return conjuncts(c.x);
|
|
10
|
+
if (c.op === 'or' && c.xs.every(p => p.op === 'not'))
|
|
11
|
+
return c.xs.flatMap(p => conjuncts(p.x));
|
|
12
|
+
return undefined;
|
|
13
|
+
};
|
|
14
|
+
const pairGroup = (c, i, j) => {
|
|
15
|
+
if (c.op !== '=')
|
|
16
|
+
return;
|
|
17
|
+
const lr = (a, b) => { const m = rowRead(a, i); return m && m === rowRead(b, j) ? m : undefined; };
|
|
18
|
+
return lr(c.a, c.b) ?? lr(c.b, c.a);
|
|
19
|
+
};
|
|
20
|
+
const rowTest = (c, k) => {
|
|
21
|
+
const e = c.op === 'not' ? c.x : c;
|
|
22
|
+
if (e.op !== '=')
|
|
23
|
+
return;
|
|
24
|
+
const test = (a, b) => { const map = rowRead(a, k); return map && b.op === 'const' ? { map, value: b.value, positive: c.op !== 'not' } : undefined; };
|
|
25
|
+
return test(e.a, e.b) ?? test(e.b, e.a);
|
|
26
|
+
};
|
|
27
|
+
function canAssign(value, tag) {
|
|
28
|
+
const seen = new Set(), stack = [value];
|
|
29
|
+
while (stack.length && seen.size < 2048) {
|
|
30
|
+
const e = stack.pop();
|
|
31
|
+
if (seen.has(e))
|
|
32
|
+
continue;
|
|
33
|
+
seen.add(e);
|
|
34
|
+
if (e.op === 'const' && e.value === tag)
|
|
35
|
+
return true;
|
|
36
|
+
if (e.op === 'if')
|
|
37
|
+
stack.push(e.then, e.else);
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
// A none-branch of a row selection can rule out old rows in the same group. Read only a
|
|
42
|
+
// simple one-tag absence query; maxima, grouped disjunctions and join predicates aren't tags.
|
|
43
|
+
function selectedTags(m, group, tagMap) {
|
|
44
|
+
const tags = new Set();
|
|
45
|
+
for (const flag of Object.values(m.step.flags ?? {})) {
|
|
46
|
+
if (flag.op !== 'all' || flag.keys?.length || flag.finite)
|
|
47
|
+
continue;
|
|
48
|
+
const cs = forbidden(flag.c);
|
|
49
|
+
if (!cs)
|
|
50
|
+
continue;
|
|
51
|
+
const k = flag.key;
|
|
52
|
+
const sameGroup = cs.some(c => c.op === '=' && [[c.a, c.b], [c.b, c.a]].some(([a, b]) => a && b && rowRead(a, k) === group && b.op === 'var' && m.inputs.includes(b.name)));
|
|
53
|
+
const tests = cs.map(c => rowTest(c, k)).filter(t => t?.map === tagMap && t.positive);
|
|
54
|
+
if (sameGroup && tests.length === 1)
|
|
55
|
+
tags.add(tests[0].value);
|
|
56
|
+
}
|
|
57
|
+
return [...tags];
|
|
58
|
+
}
|
|
59
|
+
// Project the later row to its tag, and project negative row filters out of the
|
|
60
|
+
// earlier row. Close the projected pattern under retagging from an exclusive
|
|
61
|
+
// selector: the old selected tag can precede neither a target nor another
|
|
62
|
+
// selected tag. Infer the coordinates and values from the law, writes and
|
|
63
|
+
// quantified row-selection guards. This deliberately small grammar can miss
|
|
64
|
+
// invariants or propose false ones; neither case can authorize a verdict.
|
|
65
|
+
export function orderedPairSeed(m, law) {
|
|
66
|
+
const i = law.forall, j = law.keys?.[0];
|
|
67
|
+
if (!i || !j || law.keys?.length !== 1 || law.some?.length || law.finite || i === j)
|
|
68
|
+
return;
|
|
69
|
+
const cs = forbidden(law.holds);
|
|
70
|
+
if (!cs || cs.length > 16)
|
|
71
|
+
return;
|
|
72
|
+
const groups = cs.map(c => pairGroup(c, i, j)).filter(g => g !== undefined);
|
|
73
|
+
if (groups.length !== 1 || !Object.hasOwn(m.maps ?? {}, groups[0]))
|
|
74
|
+
return;
|
|
75
|
+
const group = groups[0];
|
|
76
|
+
const frame = [], left = [], right = [];
|
|
77
|
+
for (const c of cs) {
|
|
78
|
+
const l = rowTest(c, i), r = rowTest(c, j);
|
|
79
|
+
if (l)
|
|
80
|
+
left.push(l);
|
|
81
|
+
else if (r)
|
|
82
|
+
right.push(r);
|
|
83
|
+
else if (pairGroup(c, i, j) === group || (c.op === '<' && isVar(c.a, i) && isVar(c.b, j)) ||
|
|
84
|
+
((c.op === '<' || c.op === '<=') && (isVar(c.a, i) || isVar(c.a, j)) && c.b.op === 'var' && Object.hasOwn(m.state, c.b.name)))
|
|
85
|
+
frame.push(c);
|
|
86
|
+
else
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (!frame.some(c => c.op === '<' && isVar(c.a, i) && isVar(c.b, j)))
|
|
90
|
+
return;
|
|
91
|
+
const ordered = (tests) => tests.map(t => JSON.stringify([t.map, t.value, t.positive])).sort().join(';');
|
|
92
|
+
if (ordered(left) !== ordered(right) || left.some(t => !Object.hasOwn(m.maps ?? {}, t.map)))
|
|
93
|
+
return;
|
|
94
|
+
const tagOf = left.flatMap(t => {
|
|
95
|
+
const write = Object.hasOwn(m.step.maps ?? {}, t.map) ? m.step.maps[t.map] : undefined;
|
|
96
|
+
if (!t.positive || !write || !canAssign(write.value, t.value))
|
|
97
|
+
return [];
|
|
98
|
+
const prior = selectedTags(m, group, t.map).filter(value => value !== t.value);
|
|
99
|
+
return prior.length === 1 ? [{ tag: t, prior: prior[0] }] : [];
|
|
100
|
+
});
|
|
101
|
+
if (tagOf.length !== 1)
|
|
102
|
+
return;
|
|
103
|
+
const { tag, prior } = tagOf[0];
|
|
104
|
+
const is = (k, map, value) => same(get(map, x(k)), n(value));
|
|
105
|
+
const earlier = all(...left.filter(t => t.positive).map(t => is(i, t.map, t.value)));
|
|
106
|
+
const target = is(j, tag.map, tag.value);
|
|
107
|
+
const blocked = any(all(earlier, target), all(is(i, tag.map, prior), any(is(j, tag.map, prior), target)));
|
|
108
|
+
return { name: `${law.name} [ordered row pairs]`, forall: i, keys: [j], holds: any(not(all(...frame)), not(blocked)) };
|
|
109
|
+
}
|