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/kernel.js
CHANGED
|
@@ -22,6 +22,13 @@ import { add, k, scale, sub, v } from "../core/solver.js";
|
|
|
22
22
|
// An exhaustive switch's default: TypeScript proves it unreachable, and if it is reached anyway (a value from outside
|
|
23
23
|
// the types), it fails loudly instead of returning undefined.
|
|
24
24
|
export function unreachable(value) { throw new Error(`unhandled case: ${JSON.stringify(value)}`); }
|
|
25
|
+
// Tuple keys (core/machine.ts's tup and proj): an n-tuple nests to the right, tup(x1, tup(x2, … xn)); one part is the
|
|
26
|
+
// part itself. `tupleParts` reads one back ([x1, …, xn]; anything else one part); `part` is the i-th of n parts of a
|
|
27
|
+
// natural read as an n-tuple (proj 0, proj 0 ∘ proj 1, …, the last proj 1 ∘ … ∘ proj 1).
|
|
28
|
+
export const tuple = (parts) => parts.slice(0, -1).reduceRight((b, a) => ({ op: 'tup', a, b }), parts.at(-1));
|
|
29
|
+
export const tupleParts = (e) => (e.op === 'tup' ? [e.a, ...tupleParts(e.b)] : [e]);
|
|
30
|
+
export const part = (key, i, n) => { let e = key; for (let j = 0; j < i; j++)
|
|
31
|
+
e = { op: 'proj', k: 1, a: e }; return i < n - 1 ? { op: 'proj', k: 0, a: e } : e; };
|
|
25
32
|
// (Remembered per linear form: a form is never changed once built, and the same forms are looked up at every node.)
|
|
26
33
|
const canonMemo = new WeakMap(), flipMemo = new WeakMap();
|
|
27
34
|
export const canon = (lin) => {
|
|
@@ -96,8 +103,12 @@ const choiceFree = (e) => {
|
|
|
96
103
|
break;
|
|
97
104
|
case 'add':
|
|
98
105
|
case 'prod':
|
|
106
|
+
case 'tup':
|
|
99
107
|
out = choiceFree(e.a) && choiceFree(e.b);
|
|
100
108
|
break;
|
|
109
|
+
case 'proj':
|
|
110
|
+
out = choiceFree(e.a);
|
|
111
|
+
break;
|
|
101
112
|
case 'sub':
|
|
102
113
|
case 'min':
|
|
103
114
|
case 'max':
|
|
@@ -136,7 +147,7 @@ function linearOf(e, path) {
|
|
|
136
147
|
const safe = (lin) => Object.values(lin).every(c => Number.isSafeInteger(c));
|
|
137
148
|
function linearOp(e, path) {
|
|
138
149
|
switch (e.op) {
|
|
139
|
-
case 'var': return e.name === '1' || /[*[\]
|
|
150
|
+
case 'var': return e.name === '1' || /[*[\],:⟦⟨⟩|]/.test(e.name) ? undefined : v(e.name); // those characters build cells, monomials, term columns and tuple keys
|
|
140
151
|
case 'const': return k(e.value);
|
|
141
152
|
case 'mul': {
|
|
142
153
|
const a = linear(e.a, path);
|
|
@@ -168,10 +179,50 @@ function linearOp(e, path) {
|
|
|
168
179
|
return c === undefined ? v(termColumn(e)) : linear(c ? e.then : e.else, path);
|
|
169
180
|
}
|
|
170
181
|
case 'get': {
|
|
182
|
+
// (a map's name builds its cells' columns: one with the characters of a column could spell another read's)
|
|
183
|
+
if (!plainMap(e.map))
|
|
184
|
+
return undefined;
|
|
185
|
+
if (e.key.op === 'tup' || e.key.op === 'proj') {
|
|
186
|
+
const keys = keyParts(e.key, path);
|
|
187
|
+
if (keys)
|
|
188
|
+
return v(`${e.map}[⟨${keys.map(canon).join('|')}⟩]`);
|
|
189
|
+
if (e.key.op === 'tup')
|
|
190
|
+
return undefined;
|
|
191
|
+
}
|
|
171
192
|
const key = linear(e.key, path);
|
|
172
193
|
return key && v(`${e.map}[${canon(key)}]`);
|
|
173
194
|
}
|
|
195
|
+
// A tuple is no linear form (its pairing is not linear); a part of one read back is the part (Lean's linear).
|
|
196
|
+
case 'tup': return undefined;
|
|
197
|
+
case 'proj': {
|
|
198
|
+
const ps = keyParts(e.a, path);
|
|
199
|
+
return !ps ? undefined : e.k === 0 ? ps[0] : ps.length === 2 ? ps[1] : undefined;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
// A map's name that builds no other column (kept per name: read at every cell of every path).
|
|
204
|
+
const plainMaps = new Map();
|
|
205
|
+
export const plainMap = (name) => { let ok = plainMaps.get(name); if (ok === undefined)
|
|
206
|
+
plainMaps.set(name, (ok = name !== '1' && !/[*[\],:⟦⟧⟨⟩|]/.test(name))); return ok; };
|
|
207
|
+
// A tuple key's parts as linear forms (Lean's `parts`): tup(a, b) is a's form, then b's parts if b is a tuple, else b's
|
|
208
|
+
// form; so tup(tup(a, b), c) has none (its first part is a tuple). A read at it is the cell M[⟨k1|k2|…⟩].
|
|
209
|
+
// A tuple's rest (proj 1 of it) is its parts after the first, while two or more are left (Lean's `parts`).
|
|
210
|
+
function keyParts(e, path) {
|
|
211
|
+
if (e.op === 'proj') {
|
|
212
|
+
const ps = e.k === 0 ? undefined : keyParts(e.a, path);
|
|
213
|
+
return ps && ps.length >= 3 ? ps.slice(1) : undefined;
|
|
214
|
+
}
|
|
215
|
+
if (e.op !== 'tup')
|
|
216
|
+
return undefined;
|
|
217
|
+
const a = linear(e.a, path);
|
|
218
|
+
if (!a)
|
|
219
|
+
return undefined;
|
|
220
|
+
if (e.b.op !== 'tup') {
|
|
221
|
+
const b = linear(e.b, path);
|
|
222
|
+
return b && [a, b];
|
|
174
223
|
}
|
|
224
|
+
const rest = keyParts(e.b, path);
|
|
225
|
+
return rest && [a, ...rest];
|
|
175
226
|
}
|
|
176
227
|
// --- term columns -------------------------------------------------------------------------------------------------------
|
|
177
228
|
// A choice the path leaves open is a column ⟦n⟧ for the n-th term met (one table per process: the name only has to be
|
|
@@ -244,7 +295,7 @@ export function termRows(name, path) {
|
|
|
244
295
|
// Every coefficient, product and partial sum a safe integer, else no product: a rounded product is past 2^53, but a
|
|
245
296
|
// monomial's other products could bring its sum back under it (and a square's form is the prover's, any float).
|
|
246
297
|
function times(a, b) {
|
|
247
|
-
const out =
|
|
298
|
+
const out = Object.create(null);
|
|
248
299
|
for (const [p, cp] of Object.entries(a))
|
|
249
300
|
for (const [q, cq] of Object.entries(b)) {
|
|
250
301
|
if (p.includes('⟦') || q.includes('⟦'))
|
|
@@ -348,6 +399,11 @@ function truthOf(c, path) {
|
|
|
348
399
|
case '<':
|
|
349
400
|
case '<=':
|
|
350
401
|
case '=': {
|
|
402
|
+
const parts = tupleEq(c);
|
|
403
|
+
if (parts) {
|
|
404
|
+
const a = truth(parts[0], path), b = truth(parts[1], path);
|
|
405
|
+
return a === false || b === false ? false : a === true && b === true ? true : undefined;
|
|
406
|
+
}
|
|
351
407
|
const cmp = comparison(c, path);
|
|
352
408
|
if (!cmp)
|
|
353
409
|
return undefined;
|
|
@@ -357,6 +413,26 @@ function truthOf(c, path) {
|
|
|
357
413
|
default: return unreachable(c);
|
|
358
414
|
}
|
|
359
415
|
}
|
|
416
|
+
// An equality of two tuples as its two parts' equalities (tup(a1, a2) = tup(b1, b2): a1 = b1, a2 = b2), kept per condition.
|
|
417
|
+
const tupleEqMemo = new WeakMap(), tupleAndMemo = new WeakMap();
|
|
418
|
+
// (the same as one condition, `and` of the two: kernel/reader.ts reads it so)
|
|
419
|
+
export function tupleAnd(c) {
|
|
420
|
+
const parts = tupleEq(c);
|
|
421
|
+
if (!parts)
|
|
422
|
+
return undefined;
|
|
423
|
+
let out = tupleAndMemo.get(c);
|
|
424
|
+
if (!out)
|
|
425
|
+
tupleAndMemo.set(c, (out = { op: 'and', xs: parts }));
|
|
426
|
+
return out;
|
|
427
|
+
}
|
|
428
|
+
export function tupleEq(c) {
|
|
429
|
+
if (c.op !== '=' || c.a.op !== 'tup' || c.b.op !== 'tup')
|
|
430
|
+
return undefined;
|
|
431
|
+
let out = tupleEqMemo.get(c);
|
|
432
|
+
if (out === undefined)
|
|
433
|
+
tupleEqMemo.set(c, (out = [{ op: '=', a: c.a.a, b: c.b.a }, { op: '=', a: c.a.b, b: c.b.b }]));
|
|
434
|
+
return out ?? undefined;
|
|
435
|
+
}
|
|
360
436
|
// The linear facts a hypothesis gives under the path (each lin <= 0).
|
|
361
437
|
export function facts(c, path, positive = true) {
|
|
362
438
|
switch (c.op) {
|
|
@@ -373,6 +449,14 @@ export function facts(c, path, positive = true) {
|
|
|
373
449
|
case '<':
|
|
374
450
|
case '<=':
|
|
375
451
|
case '=': {
|
|
452
|
+
// Two tuples equal is their parts equal (the pairing is a bijection): Lean's facts of `and (eq a1 b1) (eq a2 b2)`.
|
|
453
|
+
const parts = tupleEq(c);
|
|
454
|
+
if (parts) {
|
|
455
|
+
if (positive)
|
|
456
|
+
return [...facts(parts[0], path, true), ...facts(parts[1], path, true)];
|
|
457
|
+
const open = parts.filter(y => truth(y, path) !== true);
|
|
458
|
+
return open.length === 0 ? [k(1)] : open.length === 1 ? facts(open[0], path, false) : [];
|
|
459
|
+
}
|
|
376
460
|
const cmp = comparison(c, path);
|
|
377
461
|
if (!cmp)
|
|
378
462
|
return [];
|
|
@@ -472,14 +556,14 @@ function leafRows(goal, path, factsOf) {
|
|
|
472
556
|
rows.push({ lin, label: '', src: { fact: i } });
|
|
473
557
|
const names = new Set(rows.flatMap(r => Object.keys(r.lin).filter(key => key !== '1')));
|
|
474
558
|
// Two reads of one map whose keys the path proves equal are equal.
|
|
475
|
-
const cells = [...names].filter(isCell).map(name => ({ name,
|
|
559
|
+
const cells = [...names].filter(isCell).map(name => ({ name, ...cellParts(name) }));
|
|
476
560
|
for (const [i, p] of cells.entries())
|
|
477
561
|
for (const q of cells.slice(i + 1)) {
|
|
478
562
|
if (p.map !== q.map)
|
|
479
563
|
continue;
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
564
|
+
if (!sameKey(p, q, path))
|
|
565
|
+
continue;
|
|
566
|
+
rows.push({ lin: sub(v(p.name), v(q.name)), label: '', src: { pair: [p.name, q.name] } }, { lin: sub(v(q.name), v(p.name)), label: '', src: { pair: [p.name, q.name] } });
|
|
483
567
|
}
|
|
484
568
|
// Each term column's rows, and the columns those rows bring in turn.
|
|
485
569
|
const seen = new Set(), todo = [...names].filter(n => n.startsWith('⟦'));
|
|
@@ -502,30 +586,59 @@ function leafRows(goal, path, factsOf) {
|
|
|
502
586
|
rows.push({ lin: { [name]: -1 }, label: '', src: { nat: name } });
|
|
503
587
|
return rows;
|
|
504
588
|
}
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
589
|
+
// A cell's map and key: M[k] a key, M[⟨k1|k2|…⟩] a tuple's parts (each a canonical text). Split at the outer level only:
|
|
590
|
+
// a name in a key may itself be a cell, with brackets, commas and bars of its own.
|
|
591
|
+
// (kept per name: every leaf reads its cells' keys again; a form is never changed once built)
|
|
592
|
+
const cellMemo = new Map();
|
|
593
|
+
export function cellParts(cell) {
|
|
594
|
+
let out = cellMemo.get(cell);
|
|
595
|
+
if (!out) {
|
|
596
|
+
if (cellMemo.size > 200_000)
|
|
597
|
+
cellMemo.clear();
|
|
598
|
+
cellMemo.set(cell, (out = cellParts0(cell)));
|
|
599
|
+
}
|
|
600
|
+
return out;
|
|
601
|
+
}
|
|
602
|
+
function cellParts0(cell) {
|
|
603
|
+
const map = cell.slice(0, cell.indexOf('[')), inner = cell.slice(cell.indexOf('[') + 1, -1);
|
|
604
|
+
if (inner.startsWith('⟨') && inner.endsWith('⟩'))
|
|
605
|
+
return { map, keys: splitOuter(inner.slice(1, -1), '|').map(linOf) };
|
|
606
|
+
return { map, key: linOf(inner) };
|
|
607
|
+
}
|
|
608
|
+
const splitOuter = (text, sep) => {
|
|
510
609
|
const parts = [];
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
610
|
+
let depth = 0, start = 0;
|
|
611
|
+
for (let i = 0; i <= text.length; i++) {
|
|
612
|
+
const ch = text[i];
|
|
613
|
+
if (ch === '[' || ch === '⟨')
|
|
514
614
|
depth++;
|
|
515
|
-
else if (ch === ']')
|
|
615
|
+
else if (ch === ']' || ch === '⟩')
|
|
516
616
|
depth--;
|
|
517
|
-
else if ((ch ===
|
|
518
|
-
|
|
519
|
-
parts.push(inner.slice(start, i));
|
|
617
|
+
else if ((ch === sep && depth === 0) || i === text.length) {
|
|
618
|
+
parts.push(text.slice(start, i));
|
|
520
619
|
start = i + 1;
|
|
521
620
|
}
|
|
522
621
|
}
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
622
|
+
return parts;
|
|
623
|
+
};
|
|
624
|
+
// A canonical text read back: `name:coef` joined by ',' at the outer level.
|
|
625
|
+
const linOf = (text) => {
|
|
626
|
+
const out = Object.create(null);
|
|
627
|
+
for (const part of splitOuter(text, ','))
|
|
628
|
+
if (part) {
|
|
629
|
+
const at = part.lastIndexOf(':');
|
|
630
|
+
out[part.slice(0, at)] = Number(part.slice(at + 1));
|
|
631
|
+
}
|
|
527
632
|
return out;
|
|
528
633
|
};
|
|
634
|
+
// Two keys the path proves equal: two plain keys whose difference it decides 0 both ways, or two tuples of as many parts,
|
|
635
|
+
// each pair of parts so (Lean's cellEq). A plain key and a tuple are never claimed equal.
|
|
636
|
+
function sameKey(p, q, path) {
|
|
637
|
+
const zero = (a, b) => { const d = sub(a, b); return decide(d, path) === true && decide(scale(d, -1), path) === true; };
|
|
638
|
+
if (p.key && q.key)
|
|
639
|
+
return zero(p.key, q.key);
|
|
640
|
+
return !!p.keys && !!q.keys && p.keys.length === q.keys.length && p.keys.every((a, i) => zero(a, q.keys[i]));
|
|
641
|
+
}
|
|
529
642
|
// A leaf may also use the product of two of its rows: p <= 0 and q <= 0 give p·q >= 0, as nlinarith does; and the
|
|
530
643
|
// square of any linear form t, which holds whatever t's sign: t·t >= 0 (a sum of squares, as a Positivstellensatz
|
|
531
644
|
// certificate has). The squares follow the leaf's rows, then the products; a pair may name a square or an earlier
|
|
@@ -564,32 +677,40 @@ function natural(e) {
|
|
|
564
677
|
case 'add':
|
|
565
678
|
case 'sub':
|
|
566
679
|
case 'min':
|
|
567
|
-
case 'max':
|
|
680
|
+
case 'max':
|
|
681
|
+
case 'tup': return natural(e.a) && natural(e.b);
|
|
682
|
+
case 'proj': return (e.k === 0 || e.k === 1) && natural(e.a);
|
|
568
683
|
default: return unreachable(e);
|
|
569
684
|
}
|
|
570
685
|
}
|
|
571
686
|
const naturalC = (c) => c.op === 'flag' ? true : c.op === 'all' ? naturalC(c.c) : c.op === 'not' ? naturalC(c.x) : c.op === 'and' || c.op === 'or' ? c.xs.every(naturalC) : natural(c.a) && natural(c.b);
|
|
687
|
+
// (a substitution's own entries only: a name like `constructor` or `toString` is no entry of {}; terra's review. Every
|
|
688
|
+
// record the kernel reads by a name of the rule or the certificate reads it so, and every Lin is built on no prototype,
|
|
689
|
+
// where a name __proto__ is a key like any other.)
|
|
690
|
+
export const own = (r, k) => (Object.hasOwn(r, k) ? r[k] : undefined);
|
|
572
691
|
export function substE(e, s) {
|
|
573
692
|
switch (e.op) {
|
|
574
|
-
case 'var': return s.vars
|
|
693
|
+
case 'var': return own(s.vars, e.name) ?? e;
|
|
575
694
|
case 'const': return e;
|
|
576
695
|
case 'mul': return { ...e, a: substE(e.a, s) };
|
|
577
696
|
case 'prod': return { ...e, a: substE(e.a, s), b: substE(e.b, s) };
|
|
578
697
|
case 'if': return { op: 'if', c: substC(e.c, s), then: substE(e.then, s), else: substE(e.else, s) };
|
|
579
698
|
case 'get': {
|
|
580
|
-
const key = substE(e.key, s);
|
|
581
|
-
return
|
|
699
|
+
const key = substE(e.key, s), f = own(s.maps, e.map);
|
|
700
|
+
return f ? f(key) : { op: 'get', map: e.map, key };
|
|
582
701
|
}
|
|
583
702
|
case 'add':
|
|
584
703
|
case 'sub':
|
|
585
704
|
case 'min':
|
|
586
|
-
case 'max':
|
|
705
|
+
case 'max':
|
|
706
|
+
case 'tup': return { ...e, a: substE(e.a, s), b: substE(e.b, s) };
|
|
707
|
+
case 'proj': return { ...e, a: substE(e.a, s) };
|
|
587
708
|
default: return unreachable(e);
|
|
588
709
|
}
|
|
589
710
|
}
|
|
590
711
|
export function substC(c, s) {
|
|
591
712
|
if (c.op === 'flag') {
|
|
592
|
-
const f = s.flags
|
|
713
|
+
const f = own(s.flags, c.name);
|
|
593
714
|
if (!f)
|
|
594
715
|
throw new Error(`unknown flag ${c.name}`);
|
|
595
716
|
return f;
|
|
@@ -623,7 +744,7 @@ const reading = (r, vars, maps = {}) => ({ hyps: r.hyps, at: { ...r.at, vars: {
|
|
|
623
744
|
// One step: each name the update sets is its term read where the run is; a map read after it is the written value at
|
|
624
745
|
// the written key, and what the map held before elsewhere (or, written at every key, its value at the key read).
|
|
625
746
|
const step = (r, u) => {
|
|
626
|
-
const at = r.at, before = (map, key) => (at.maps
|
|
747
|
+
const at = r.at, before = (map, key) => { const f = own(at.maps, map); return f ? f(key) : { op: 'get', map, key }; };
|
|
627
748
|
return { hyps: r.hyps, at: {
|
|
628
749
|
vars: { ...at.vars, ...Object.fromEntries(Object.entries(u.vars).map(([n, e]) => [n, substE(e, at)])) },
|
|
629
750
|
maps: { ...at.maps, ...Object.fromEntries(Object.entries(u.maps).map(([map, w]) => [map, 'each' in w
|
|
@@ -636,7 +757,7 @@ const step = (r, u) => {
|
|
|
636
757
|
// so a run of k steps is k small frames rather than a term nesting each state in the next (which grows as 3^k when a
|
|
637
758
|
// step reads its state thrice). The names are new to the goal; each equation holds of the run that took the steps.
|
|
638
759
|
const nameState = (r, state, j) => ({
|
|
639
|
-
hyps: [...r.hyps, ...state.map(n => ({ op: '=', a: { op: 'var', name: stateName(n, j) }, b: r.at.vars
|
|
760
|
+
hyps: [...r.hyps, ...state.map(n => ({ op: '=', a: { op: 'var', name: stateName(n, j) }, b: own(r.at.vars, n) ?? { op: 'var', name: n } }))],
|
|
640
761
|
at: { ...r.at, vars: { ...r.at.vars, ...Object.fromEntries(state.map(n => [n, { op: 'var', name: stateName(n, j) }])) } },
|
|
641
762
|
});
|
|
642
763
|
export const stateName = (n, j) => `${n}~${j}`;
|
|
@@ -645,8 +766,8 @@ const atEnd = (r, c) => substC(c, r.at);
|
|
|
645
766
|
// The conclusion read across the run: plain names at the first frame, primed names (s', and M'[k] as a read of the map
|
|
646
767
|
// named M') at the last; the first step's inputs as it read them, and its flags, decided where it was taken.
|
|
647
768
|
const across = (r, c, state, maps, first) => substC(c, {
|
|
648
|
-
vars: { ...first.inputs, ...Object.fromEntries(state.map(n => [`${n}'`, r.at.vars
|
|
649
|
-
maps: Object.fromEntries(maps.map(map => [`${map}'`, (key) => (r.at.maps
|
|
769
|
+
vars: { ...first.inputs, ...Object.fromEntries(state.map(n => [`${n}'`, own(r.at.vars, n) ?? { op: 'var', name: n }])) },
|
|
770
|
+
maps: Object.fromEntries(maps.map(map => [`${map}'`, (key) => { const f = own(r.at.maps, map); return f ? f(key) : { op: 'get', map, key }; }])),
|
|
650
771
|
flags: first.flags,
|
|
651
772
|
});
|
|
652
773
|
const goal = (name, r, conclusion) => ({ name, hyps: [...r.hyps, { op: 'not', x: conclusion }] });
|
|
@@ -654,7 +775,7 @@ const goal = (name, r, conclusion) => ({ name, hyps: [...r.hyps, { op: 'not', x:
|
|
|
654
775
|
// condition (through the flags before it), a quantified flag (one whose condition holds at every key) its 0/1 variable
|
|
655
776
|
// ?f, which `closing` ties to the condition.
|
|
656
777
|
const flagsOf = (m) => {
|
|
657
|
-
const out =
|
|
778
|
+
const out = Object.create(null);
|
|
658
779
|
for (const [f, c] of Object.entries(m.step.flags ?? {}))
|
|
659
780
|
out[f] = c.op === 'all' ? { op: '=', a: { op: 'var', name: `?${f}` }, b: { op: 'const', value: 1 } } : substC(c, { vars: {}, maps: {}, flags: out });
|
|
660
781
|
return out;
|
|
@@ -674,8 +795,8 @@ export const crossFree = (c, keys) => {
|
|
|
674
795
|
if (hit)
|
|
675
796
|
return hit;
|
|
676
797
|
const zero = { op: 'const', value: 0 };
|
|
677
|
-
const e = (t) => (t.op === 'if' ? { ...t, c: crossFree(t.c, keys), then: e(t.then), else: e(t.else) } : t.op === 'get' ? { ...t, key: e(t.key) } : t.op === 'mul' ? { ...t, a: e(t.a) }
|
|
678
|
-
: t.op === 'add' || t.op === 'sub' || t.op === 'min' || t.op === 'max' || t.op === 'prod' ? { ...t, a: e(t.a), b: e(t.b) } : t);
|
|
798
|
+
const e = (t) => (t.op === 'if' ? { ...t, c: crossFree(t.c, keys), then: e(t.then), else: e(t.else) } : t.op === 'get' ? { ...t, key: e(t.key) } : t.op === 'mul' || t.op === 'proj' ? { ...t, a: e(t.a) }
|
|
799
|
+
: t.op === 'add' || t.op === 'sub' || t.op === 'min' || t.op === 'max' || t.op === 'prod' || t.op === 'tup' ? { ...t, a: e(t.a), b: e(t.b) } : t);
|
|
679
800
|
const out = crossing(c, keys) ? { op: '<=', a: zero, b: zero } : c.op === 'not' ? { op: 'not', x: crossFree(c.x, keys) } : c.op === 'and' || c.op === 'or' ? { op: c.op, xs: c.xs.map(y => crossFree(y, keys)) }
|
|
680
801
|
: c.op === 'all' ? { ...c, c: crossFree(c.c, keys) } : c.op === 'flag' ? c : { ...c, a: e(c.a), b: e(c.b) };
|
|
681
802
|
if (!crossCache.has(c))
|
|
@@ -710,7 +831,7 @@ const laterNames = (m, j) => ({
|
|
|
710
831
|
});
|
|
711
832
|
// The laws assumed where the run is: each plain law, and each every-key law at the keys the proof names.
|
|
712
833
|
export const assumed = (laws, instances) => laws.flatMap(l => {
|
|
713
|
-
const keys = instances
|
|
834
|
+
const keys = own(instances, l.name) ?? [];
|
|
714
835
|
if (l.keys?.length) {
|
|
715
836
|
const names = [l.forall, ...l.keys], n = names.length;
|
|
716
837
|
return Array.from({ length: Math.floor(keys.length / n) }, (_, t) => substC(l.holds, { vars: Object.fromEntries(names.map((name, i) => [name, keys[t * n + i]])), maps: {}, flags: {} }));
|
|
@@ -753,6 +874,7 @@ function reads(t, out = { vars: new Set(), maps: new Set(), flags: new Set() })
|
|
|
753
874
|
reads(y, out);
|
|
754
875
|
break;
|
|
755
876
|
case 'mul':
|
|
877
|
+
case 'proj':
|
|
756
878
|
reads(t.a, out);
|
|
757
879
|
break;
|
|
758
880
|
case 'if':
|
|
@@ -765,6 +887,7 @@ function reads(t, out = { vars: new Set(), maps: new Set(), flags: new Set() })
|
|
|
765
887
|
case 'min':
|
|
766
888
|
case 'max':
|
|
767
889
|
case 'prod':
|
|
890
|
+
case 'tup':
|
|
768
891
|
case '<':
|
|
769
892
|
case '<=':
|
|
770
893
|
case '=':
|
|
@@ -798,8 +921,8 @@ function inScope(what, ts, vars, maps = [], flags = []) {
|
|
|
798
921
|
// keeps a later step's names apart from every name of the machine instead.) A name of the machine, a law's or step
|
|
799
922
|
// law's key with one of them would be read as the kernel's own (a param ?f as the flag f's 0/1 variable: "s <= ?f" was
|
|
800
923
|
// accepted, false at ?f = 0; Lean refuses it).
|
|
801
|
-
const plainName = (what, name) => { if (name === '1' || /[*[\],:#?~'
|
|
802
|
-
throw new Error(`${what}: the name ${name} is reserved (1, or * [ ] , : # ? ~ ' ⟦ ⟧)`); };
|
|
924
|
+
const plainName = (what, name) => { if (name === '1' || /[*[\],:#?~'⟦⟧⟨⟩|]/.test(name))
|
|
925
|
+
throw new Error(`${what}: the name ${name} is reserved (1, or * [ ] , : # ? ~ ' ⟦ ⟧ ⟨ ⟩ |)`); };
|
|
803
926
|
const distinct = (what, names) => { const seen = new Set(); for (const name of names) {
|
|
804
927
|
if (seen.has(name))
|
|
805
928
|
throw new Error(`${what} declares ${name} twice`);
|
|
@@ -852,9 +975,24 @@ export function scope(m, laws = []) {
|
|
|
852
975
|
inScope(what, [c], names, readable, flags.slice(0, i));
|
|
853
976
|
}
|
|
854
977
|
});
|
|
978
|
+
const arity = mapArities(m, laws);
|
|
855
979
|
for (const [map, w] of Object.entries(m.step.maps ?? {})) {
|
|
856
980
|
const what = `${m.name}'s write to ${map}`;
|
|
857
|
-
|
|
981
|
+
// At every key of a tuple-keyed map, the key is read as its parts (proj k, or the key whole at a read of the map):
|
|
982
|
+
// the runtimes compute the write at every tuple of parts, each part read as a bound key is.
|
|
983
|
+
if ('each' in w && (own(arity, map) ?? 1) > 1) {
|
|
984
|
+
const t = eachParts(w, arity[map]);
|
|
985
|
+
plainName(what, w.each);
|
|
986
|
+
if (readsKey(t.value, w.each))
|
|
987
|
+
throw new Error(`${what}: the key ${w.each} is read as its ${arity[map]} parts (or whole, as the key of a map of ${arity[map]}), nothing else`);
|
|
988
|
+
for (const key of t.names) {
|
|
989
|
+
bound(what, key, t.value);
|
|
990
|
+
apart(what, crossFree({ op: '=', a: t.value, b: { op: 'const', value: 0 } }, t.names), key, t.names);
|
|
991
|
+
}
|
|
992
|
+
unquantified(what, w.value);
|
|
993
|
+
inScope(what, [t.value], [...names, ...t.names], readable, flags);
|
|
994
|
+
}
|
|
995
|
+
else if ('each' in w) {
|
|
858
996
|
bound(what, w.each, w.value);
|
|
859
997
|
unquantified(what, w.value);
|
|
860
998
|
inScope(what, [w.value], [...names, w.each], readable, flags);
|
|
@@ -898,11 +1036,84 @@ export function scope(m, laws = []) {
|
|
|
898
1036
|
for (const fact of facts)
|
|
899
1037
|
inScope(`fact ${fact.name}`, [fact.holds], [...m.params, ...keysOf(fact)], fns);
|
|
900
1038
|
}
|
|
1039
|
+
// Each map's number of parts (1 a plain key): every read and every write of a map is at keys of one shape, so a tuple
|
|
1040
|
+
// map's cells are its tuples alone (the runtimes keep them apart from a plain key's). A function, a count and a carried
|
|
1041
|
+
// map take plain keys.
|
|
1042
|
+
// Lean keyArity at an empty path: projected tuple tails have the arity of their linear parts.
|
|
1043
|
+
const keyArity = (key) => keyParts(key, new Map())?.length ?? tupleParts(key).length;
|
|
1044
|
+
export function mapArities(m, laws = []) {
|
|
1045
|
+
const out = Object.create(null);
|
|
1046
|
+
const plainOnly = new Set([...Object.keys(m.fns ?? {}), ...Object.keys(m.counts ?? {}), ...(m.inputMaps ?? [])]);
|
|
1047
|
+
const note = (map, key) => {
|
|
1048
|
+
const n = keyArity(key), had = own(out, map);
|
|
1049
|
+
if (had !== undefined && had !== n)
|
|
1050
|
+
throw new Error(`${m.name}: the map ${map} is read at keys of ${had} and of ${n} parts`);
|
|
1051
|
+
if (n > 1 && plainOnly.has(map))
|
|
1052
|
+
throw new Error(`${m.name}: ${map} is a function, a count or a carried map, read at plain keys only`);
|
|
1053
|
+
out[map] = n;
|
|
1054
|
+
};
|
|
1055
|
+
walkTerms([...machineTermsOf(m), ...laws.map(l => l.holds)], o => { if (o.op === 'get' && typeof o.map === 'string' && o.key)
|
|
1056
|
+
note(o.map, o.key); });
|
|
1057
|
+
for (const [map, w] of Object.entries(m.step.maps ?? {}))
|
|
1058
|
+
if (!('each' in w))
|
|
1059
|
+
note(map, w.key);
|
|
1060
|
+
return out;
|
|
1061
|
+
}
|
|
1062
|
+
const walkTerms = (ts, f) => {
|
|
1063
|
+
const go = (t) => { if (Array.isArray(t)) {
|
|
1064
|
+
t.forEach(go);
|
|
1065
|
+
return;
|
|
1066
|
+
} if (!t || typeof t !== 'object')
|
|
1067
|
+
return; f(t); for (const x of Object.values(t))
|
|
1068
|
+
go(x); };
|
|
1069
|
+
ts.forEach(go);
|
|
1070
|
+
};
|
|
1071
|
+
const machineTermsOf = (m) => [m.state, m.step, m.pre, m.assume, m.facts, m.history, m.sums, m.counts, m.leasts];
|
|
1072
|
+
// A write at every key of an n-part map, read at its parts: the key named each, at k.1 … k.n (each$1 …), the value with
|
|
1073
|
+
// the key the tuple of them and each part read back (proj of a tuple its part), so it reads the parts as bound keys.
|
|
1074
|
+
export function eachParts(w, n) {
|
|
1075
|
+
const names = Array.from({ length: n }, (_, i) => `${w.each}$${i + 1}`);
|
|
1076
|
+
// (the flags it reads left as they are)
|
|
1077
|
+
const flags = Object.fromEntries([...JSON.stringify(w.value).matchAll(/"op":"flag","name":"((?:[^"\\]|\\.)*)"/g)].map(m => [JSON.parse(`"${m[1]}"`), { op: 'flag', name: JSON.parse(`"${m[1]}"`) }]));
|
|
1078
|
+
const at = substE(w.value, { vars: { [w.each]: tuple(names.map((name) => ({ op: 'var', name }))) }, maps: {}, flags });
|
|
1079
|
+
return { names, value: projected(at) };
|
|
1080
|
+
}
|
|
1081
|
+
// proj of a tuple read as its part, throughout.
|
|
1082
|
+
export function projected(e) {
|
|
1083
|
+
switch (e.op) {
|
|
1084
|
+
case 'var':
|
|
1085
|
+
case 'const': return e;
|
|
1086
|
+
case 'proj': {
|
|
1087
|
+
const a = projected(e.a);
|
|
1088
|
+
return a.op === 'tup' ? (e.k === 0 ? a.a : a.b) : { op: 'proj', k: e.k, a };
|
|
1089
|
+
}
|
|
1090
|
+
case 'mul': return { ...e, a: projected(e.a) };
|
|
1091
|
+
case 'get': return { ...e, key: projected(e.key) };
|
|
1092
|
+
case 'if': return { op: 'if', c: projectedC(e.c), then: projected(e.then), else: projected(e.else) };
|
|
1093
|
+
case 'add':
|
|
1094
|
+
case 'sub':
|
|
1095
|
+
case 'min':
|
|
1096
|
+
case 'max':
|
|
1097
|
+
case 'prod':
|
|
1098
|
+
case 'tup': return { ...e, a: projected(e.a), b: projected(e.b) };
|
|
1099
|
+
default: return unreachable(e);
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
export const projectedC = (c) => c.op === 'not' ? { op: 'not', x: projectedC(c.x) } : c.op === 'and' || c.op === 'or' ? { op: c.op, xs: c.xs.map(projectedC) }
|
|
1103
|
+
: c.op === 'all' ? { ...c, c: projectedC(c.c) } : c.op === 'flag' ? c : { ...c, a: projected(c.a), b: projected(c.b) };
|
|
901
1104
|
// A key bound in t is read in key terms only: the key, or the key plus or less a term that doesn't read it (k + e, e + k,
|
|
902
1105
|
// k ∸ e), each a map's key (no function's or count's: their values don't stop where a map's cells do) or a side of a
|
|
903
1106
|
// comparison whose other side doesn't read the key. Then t is constant between finitely many keys (the runtimes'
|
|
904
1107
|
// breakpoints: each map's cells and each compared value, shifted by the offsets), so a runtime decides it on those.
|
|
905
1108
|
const readsKey = (t, key) => JSON.stringify(t).includes(`"op":"var","name":${JSON.stringify(key)}`);
|
|
1109
|
+
// A tuple key's parts each read the same way: the key alone in a part, or plus or less a term that doesn't read it; a
|
|
1110
|
+
// comparison of two tuples of as many parts compares them part by part.
|
|
1111
|
+
const tupleSides = (u) => {
|
|
1112
|
+
if (u.op !== '=' || u.a.op !== 'tup' || u.b.op !== 'tup')
|
|
1113
|
+
return undefined;
|
|
1114
|
+
const as = tupleParts(u.a), bs = tupleParts(u.b);
|
|
1115
|
+
return as.length === bs.length ? as.map((a, i) => [a, bs[i]]) : undefined;
|
|
1116
|
+
};
|
|
906
1117
|
export const keyTerm = (e, key) => e.op === 'var' ? e.name === key
|
|
907
1118
|
: e.op === 'add' ? (e.a.op === 'var' && e.a.name === key && !readsKey(e.b, key)) || (e.b.op === 'var' && e.b.name === key && !readsKey(e.a, key))
|
|
908
1119
|
: e.op === 'sub' && e.a.op === 'var' && e.a.name === key && !readsKey(e.b, key);
|
|
@@ -915,7 +1126,12 @@ function keyOnly(what, t, key, fns = []) {
|
|
|
915
1126
|
bad();
|
|
916
1127
|
break;
|
|
917
1128
|
case 'get':
|
|
918
|
-
if (
|
|
1129
|
+
if (u.key.op === 'tup' && !fns.includes(u.map)) {
|
|
1130
|
+
for (const part of tupleParts(u.key))
|
|
1131
|
+
if (!keyTerm(part, key))
|
|
1132
|
+
walk(part);
|
|
1133
|
+
}
|
|
1134
|
+
else if (!(keyTerm(u.key, key) && !fns.includes(u.map)))
|
|
919
1135
|
walk(u.key);
|
|
920
1136
|
break;
|
|
921
1137
|
case 'const':
|
|
@@ -931,6 +1147,7 @@ function keyOnly(what, t, key, fns = []) {
|
|
|
931
1147
|
u.xs.forEach(walk);
|
|
932
1148
|
break;
|
|
933
1149
|
case 'mul':
|
|
1150
|
+
case 'proj':
|
|
934
1151
|
walk(u.a);
|
|
935
1152
|
break;
|
|
936
1153
|
case 'if':
|
|
@@ -940,21 +1157,29 @@ function keyOnly(what, t, key, fns = []) {
|
|
|
940
1157
|
break;
|
|
941
1158
|
case '<':
|
|
942
1159
|
case '<=':
|
|
943
|
-
case '=':
|
|
944
|
-
if (keyTerm(
|
|
945
|
-
walk(
|
|
946
|
-
else if (keyTerm(
|
|
947
|
-
walk(
|
|
1160
|
+
case '=': {
|
|
1161
|
+
const side = (a, b) => { if (keyTerm(a, key) && !readsKey(b, key))
|
|
1162
|
+
walk(b);
|
|
1163
|
+
else if (keyTerm(b, key) && !readsKey(a, key))
|
|
1164
|
+
walk(a);
|
|
948
1165
|
else {
|
|
949
|
-
walk(
|
|
950
|
-
walk(
|
|
951
|
-
}
|
|
1166
|
+
walk(a);
|
|
1167
|
+
walk(b);
|
|
1168
|
+
} };
|
|
1169
|
+
const pairs = tupleSides(u);
|
|
1170
|
+
if (pairs)
|
|
1171
|
+
for (const [a, b] of pairs)
|
|
1172
|
+
side(a, b);
|
|
1173
|
+
else
|
|
1174
|
+
side(u.a, u.b);
|
|
952
1175
|
break;
|
|
1176
|
+
}
|
|
953
1177
|
case 'add':
|
|
954
1178
|
case 'sub':
|
|
955
1179
|
case 'min':
|
|
956
1180
|
case 'max':
|
|
957
1181
|
case 'prod':
|
|
1182
|
+
case 'tup':
|
|
958
1183
|
walk(u.a);
|
|
959
1184
|
walk(u.b);
|
|
960
1185
|
break;
|
|
@@ -967,9 +1192,10 @@ function keyOnly(what, t, key, fns = []) {
|
|
|
967
1192
|
// another (the comparisons between two of them, plainly, are set aside before: crossFree).
|
|
968
1193
|
function apart(what, t, key, keys) {
|
|
969
1194
|
const others = keys.filter(k => k !== key), other = (e) => others.some(k => readsKey(e, k));
|
|
1195
|
+
const cmp = (a, b) => (keyTerm(a, key) && (other(a) || other(b))) || (keyTerm(b, key) && (other(a) || other(b)));
|
|
970
1196
|
const walk = (u) => {
|
|
971
|
-
if ((u.op === 'get' && keyTerm(u.key, key) && other(u.key))
|
|
972
|
-
|| ((u.op === '<' || u.op === '<=' || u.op === '=') && ((
|
|
1197
|
+
if ((u.op === 'get' && (u.key.op === 'tup' ? tupleParts(u.key).some(p => keyTerm(p, key) && other(p)) : keyTerm(u.key, key) && other(u.key)))
|
|
1198
|
+
|| ((u.op === '<' || u.op === '<=' || u.op === '=') && (tupleSides(u)?.some(([a, b]) => cmp(a, b)) ?? cmp(u.a, u.b))))
|
|
973
1199
|
throw new Error(`${what}: the keys ${keys.join(', ')} are each read on their own, and compared with one another only plainly (i < j, i <= j, i = j)`);
|
|
974
1200
|
for (const x of Object.values(u))
|
|
975
1201
|
if (x && typeof x === 'object')
|
|
@@ -989,13 +1215,15 @@ export const plainKey = (t, key) => {
|
|
|
989
1215
|
case 'all': return walk(u.c);
|
|
990
1216
|
case 'and':
|
|
991
1217
|
case 'or': return u.xs.every(walk);
|
|
992
|
-
case 'mul':
|
|
1218
|
+
case 'mul':
|
|
1219
|
+
case 'proj': return walk(u.a);
|
|
993
1220
|
case 'if': return walk(u.c) && walk(u.then) && walk(u.else);
|
|
994
1221
|
case 'add':
|
|
995
1222
|
case 'sub':
|
|
996
1223
|
case 'min':
|
|
997
1224
|
case 'max':
|
|
998
1225
|
case 'prod':
|
|
1226
|
+
case 'tup':
|
|
999
1227
|
case '<':
|
|
1000
1228
|
case '<=':
|
|
1001
1229
|
case '=': return walk(u.a) && walk(u.b);
|
|
@@ -1087,6 +1315,8 @@ const sumTermOk = (e, key) => {
|
|
|
1087
1315
|
case 'min':
|
|
1088
1316
|
case 'max':
|
|
1089
1317
|
case 'prod': return sumTermOk(e.a, key) && sumTermOk(e.b, key);
|
|
1318
|
+
case 'tup':
|
|
1319
|
+
case 'proj': return false;
|
|
1090
1320
|
default: return unreachable(e);
|
|
1091
1321
|
}
|
|
1092
1322
|
};
|
|
@@ -1109,7 +1339,7 @@ export function sumCovers(m) {
|
|
|
1109
1339
|
return Object.fromEntries(Object.entries(m.sums ?? {}).map(([s, sum]) => {
|
|
1110
1340
|
const out = new Map();
|
|
1111
1341
|
for (const map of [...new Set([...JSON.stringify(sum.term).matchAll(/"op":"get","map":"([^"]+)"/g)].map(x2 => x2[1]))]) {
|
|
1112
|
-
const w = m.step.maps
|
|
1342
|
+
const w = own(m.step.maps ?? {}, map);
|
|
1113
1343
|
if (!w)
|
|
1114
1344
|
continue;
|
|
1115
1345
|
if ('each' in w)
|
|
@@ -1138,21 +1368,21 @@ export function sumMachine(m, given, laws = []) {
|
|
|
1138
1368
|
const same = Object.fromEntries(Object.keys(flags).map(f => [f, { op: 'flag', name: f }]));
|
|
1139
1369
|
// (a sum may be declared in the state, at 0, which no step sets: a state variable the plain machine moves)
|
|
1140
1370
|
for (const [s] of sums)
|
|
1141
|
-
if (
|
|
1371
|
+
if (Object.hasOwn(m.state, s) && JSON.stringify(m.state[s]) !== JSON.stringify(ZERO) || Object.hasOwn(m.step.state ?? {}, s))
|
|
1142
1372
|
throw new Error(`${m.name}: the sum ${s} starts at 0 and no step sets it`);
|
|
1143
1373
|
// (every name the machine has, as the Lean twin numbers them: a flag f's variable ?f, a quantified flag's witnesses,
|
|
1144
1374
|
// the keys writes at every key and quantified flags bind)
|
|
1145
|
-
const names = [...m.params, ...m.inputs, ...Object.keys(m.state).filter(n => !(
|
|
1375
|
+
const names = [...m.params, ...m.inputs, ...Object.keys(m.state).filter(n => !Object.hasOwn(m.sums, n)), ...Object.keys(m.maps ?? {}), ...(m.inputMaps ?? []), ...Object.keys(m.fns ?? {}),
|
|
1146
1376
|
...Object.keys(flags), ...Object.keys(flags).map(f => `?${f}`), ...Object.entries(flags).flatMap(([f, c]) => (c.op === 'all' ? [...witnessesOf(f, c), ...boundOf(c)] : [])),
|
|
1147
1377
|
...Object.values(m.step.maps ?? {}).flatMap(w => ('each' in w ? [w.each] : []))];
|
|
1148
|
-
const steps =
|
|
1378
|
+
const steps = Object.create(null), qflags = Object.create(null), facts = [], goals = [];
|
|
1149
1379
|
for (const [s, sum] of sums) {
|
|
1150
|
-
const { term, key } = sum, { flag, x } = sumParts(s), cs = covers
|
|
1380
|
+
const { term, key } = sum, { flag, x } = sumParts(s), cs = own(covers, s);
|
|
1151
1381
|
const read = [...new Set([...JSON.stringify(term).matchAll(/"op":"get","map":"([^"]+)"/g)].map(x2 => x2[1]))];
|
|
1152
1382
|
const bad = (why) => { throw new Error(`${m.name}: the sum ${s}: ${why}`); };
|
|
1153
1383
|
for (const name of [s, key, flag, x]) {
|
|
1154
1384
|
plainName(m.name, name);
|
|
1155
|
-
if (names.includes(name) ||
|
|
1385
|
+
if (names.includes(name) || Object.hasOwn(m.sums ?? {}, name) && name !== s)
|
|
1156
1386
|
bad(`${name} is already a name of the machine`);
|
|
1157
1387
|
}
|
|
1158
1388
|
if (new Set([s, key, flag, x, `?${flag}`, `w?${flag}`]).size !== 6 || names.includes(`?${flag}`) || names.includes(`w?${flag}`))
|
|
@@ -1160,7 +1390,7 @@ export function sumMachine(m, given, laws = []) {
|
|
|
1160
1390
|
if (!natural(term) || !sumTermOk(term, key))
|
|
1161
1391
|
bad(`its term reads the maps at ${key} alone, and no other name`);
|
|
1162
1392
|
for (const map of read) {
|
|
1163
|
-
if ((m.inputMaps ?? []).includes(map) || (m.fns ?? {})
|
|
1393
|
+
if ((m.inputMaps ?? []).includes(map) || Object.hasOwn(m.fns ?? {}, map) || !Object.hasOwn(m.maps ?? {}, map))
|
|
1164
1394
|
bad(`${map} is not a map of the machine's state`);
|
|
1165
1395
|
}
|
|
1166
1396
|
// 0 where every map holds its start value (Sums.lean's startVal: the term with each read its map's start).
|
|
@@ -1172,14 +1402,14 @@ export function sumMachine(m, given, laws = []) {
|
|
|
1172
1402
|
bad('a key a step writes it at reads a flag');
|
|
1173
1403
|
// (Sums.lean's sumOk1: a map the term reads written at one key has that key among the cover keys)
|
|
1174
1404
|
for (const map of read) {
|
|
1175
|
-
const w = m.step.maps
|
|
1405
|
+
const w = own(m.step.maps ?? {}, map);
|
|
1176
1406
|
if (w && !('each' in w) && !cs.some(c => JSON.stringify(c) === JSON.stringify(w.key)))
|
|
1177
1407
|
bad(`the key ${map} is written at is no cover key`);
|
|
1178
1408
|
}
|
|
1179
1409
|
const here = (t) => substE(term, { vars: { [key]: t }, maps: {}, flags: same });
|
|
1180
1410
|
// A map read after the step, over the step's view (Sums.lean's postRd).
|
|
1181
1411
|
const post = (map) => (k2) => {
|
|
1182
|
-
const w = m.step.maps
|
|
1412
|
+
const w = own(m.step.maps ?? {}, map);
|
|
1183
1413
|
if (!w)
|
|
1184
1414
|
return { op: 'get', map, key: k2 };
|
|
1185
1415
|
if ('each' in w)
|
|
@@ -1250,14 +1480,15 @@ export function obligations(m, laws, instances = {}, only) {
|
|
|
1250
1480
|
return [];
|
|
1251
1481
|
// At the start: the precondition, and no request. One step: from a state where the laws hold, a request allowed.
|
|
1252
1482
|
// The facts hold throughout, read where the run begins (they read no state).
|
|
1253
|
-
const
|
|
1254
|
-
const
|
|
1483
|
+
const inst = own(instances, law.name) ?? {};
|
|
1484
|
+
const start = given(m, holds(from(startOf(m)), pre), inst);
|
|
1485
|
+
const turn = step(holds(given(m, from(), inst), [...pre, ...assume, ...assumed(laws, inst), ...closing(m, inst)]), u);
|
|
1255
1486
|
return [goal(`${law.name} @start`, start, atEnd(start, proven(law))), goal(`${law.name} @step`, turn, atEnd(turn, proven(law)))];
|
|
1256
1487
|
});
|
|
1257
1488
|
}
|
|
1258
1489
|
// The whole set at once: every goal has a tree, and every tree checks.
|
|
1259
1490
|
export function checkLaws(m, laws, proof) {
|
|
1260
|
-
return obligations(m, laws, proof.instances).every(g => proof.trees
|
|
1491
|
+
return obligations(m, laws, proof.instances).every(g => { const t = own(proof.trees, g.name); return t !== undefined && check(g, t); });
|
|
1261
1492
|
}
|
|
1262
1493
|
// A loop is a machine: its state, a turn's inputs as a request, its body taken where `while` holds and the state kept
|
|
1263
1494
|
// where it fails. Its invariants are the machine's laws, and that it halts is a liveness: from anywhere, `while` fails
|
|
@@ -1279,7 +1510,7 @@ export function loopObligations(l, laws, rank) {
|
|
|
1279
1510
|
const names = [...l.params, ...l.inputs, ...Object.keys(l.state)];
|
|
1280
1511
|
distinct(l.name, names);
|
|
1281
1512
|
for (const name of Object.keys(l.body))
|
|
1282
|
-
if (!(
|
|
1513
|
+
if (!Object.hasOwn(l.state, name))
|
|
1283
1514
|
throw new Error(`${l.name}'s body sets ${name}, which isn't in its state`);
|
|
1284
1515
|
inScope(`${l.name}'s start`, Object.values(l.state), l.params);
|
|
1285
1516
|
inScope(l.name, [...Object.values(l.body), ...(l.assume ? [l.assume] : [])], names);
|
|
@@ -1303,7 +1534,7 @@ export function recurGoal(l, r) {
|
|
|
1303
1534
|
// Lean's evalE and evalC over naturals with every map cell 0 (NonTerm.lean's noMaps): exact, never rounded.
|
|
1304
1535
|
const natE = (e, env) => {
|
|
1305
1536
|
switch (e.op) {
|
|
1306
|
-
case 'var': return env
|
|
1537
|
+
case 'var': return own(env, e.name) ?? 0n;
|
|
1307
1538
|
case 'const': return BigInt(e.value);
|
|
1308
1539
|
case 'add': return natE(e.a, env) + natE(e.b, env);
|
|
1309
1540
|
case 'sub': {
|
|
@@ -1322,6 +1553,8 @@ const natE = (e, env) => {
|
|
|
1322
1553
|
}
|
|
1323
1554
|
case 'if': return natC(e.c, env) ? natE(e.then, env) : natE(e.else, env);
|
|
1324
1555
|
case 'get': return 0n;
|
|
1556
|
+
case 'tup':
|
|
1557
|
+
case 'proj': throw new Error('a loop\'s run is read over no tuple');
|
|
1325
1558
|
default: return unreachable(e);
|
|
1326
1559
|
}
|
|
1327
1560
|
};
|
|
@@ -1347,7 +1580,7 @@ function stemOk(l, r) {
|
|
|
1347
1580
|
const names = [...l.params, ...l.inputs, ...Object.keys(l.state)], values = (o) => Object.values(o).every(x => Number.isSafeInteger(x) && x >= 0);
|
|
1348
1581
|
distinct(l.name, names);
|
|
1349
1582
|
for (const name of Object.keys(l.body))
|
|
1350
|
-
if (!(
|
|
1583
|
+
if (!Object.hasOwn(l.state, name))
|
|
1351
1584
|
throw new Error(`${l.name}'s body sets ${name}, which isn't in its state`);
|
|
1352
1585
|
inScope(`${l.name}'s start`, Object.values(l.state), l.params);
|
|
1353
1586
|
inScope(l.name, [...Object.values(l.body), ...(l.assume ? [l.assume] : [])], names);
|
|
@@ -1360,13 +1593,13 @@ function stemOk(l, r) {
|
|
|
1360
1593
|
throw new Error(`${l.name}: a start names params and a stem's turn inputs, each a natural`);
|
|
1361
1594
|
if (!Object.keys(r.wit).every(n => l.inputs.includes(n)))
|
|
1362
1595
|
return false;
|
|
1363
|
-
let env = Object.fromEntries(names.map(n => [n, BigInt(r.start
|
|
1596
|
+
let env = Object.fromEntries(names.map(n => [n, BigInt(own(r.start, n) ?? 0)]));
|
|
1364
1597
|
if (!natC(l.pre ?? trueC, env))
|
|
1365
1598
|
return false;
|
|
1366
1599
|
const apply = (body, at) => ({ ...at, ...Object.fromEntries(Object.entries(body).map(([n, e]) => [n, natE(e, at)])) });
|
|
1367
1600
|
env = apply(l.state, env);
|
|
1368
1601
|
for (const turn of r.stem) {
|
|
1369
|
-
const at = { ...env, ...Object.fromEntries(l.inputs.map(n => [n, BigInt(turn
|
|
1602
|
+
const at = { ...env, ...Object.fromEntries(l.inputs.map(n => [n, BigInt(own(turn, n) ?? 0)])) };
|
|
1370
1603
|
if (!natC(l.while, at) || !natC(l.assume ?? trueC, at))
|
|
1371
1604
|
return false;
|
|
1372
1605
|
env = apply(l.body, at);
|
|
@@ -1409,6 +1642,8 @@ function foldTerm(e, f, fc) {
|
|
|
1409
1642
|
return isTC(c) ? f(e.then) : isFC(c) ? f(e.else) : { op: 'if', c, then: f(e.then), else: f(e.else) };
|
|
1410
1643
|
}
|
|
1411
1644
|
case 'get': return { op: 'get', map: e.map, key: f(e.key) };
|
|
1645
|
+
case 'tup': return { op: 'tup', a: f(e.a), b: f(e.b) };
|
|
1646
|
+
case 'proj': return { op: 'proj', k: e.k, a: f(e.a) };
|
|
1412
1647
|
default: return unreachable(e);
|
|
1413
1648
|
}
|
|
1414
1649
|
}
|
|
@@ -1462,7 +1697,7 @@ const substituter = (vars) => {
|
|
|
1462
1697
|
let o;
|
|
1463
1698
|
switch (e.op) {
|
|
1464
1699
|
case 'var':
|
|
1465
|
-
o = vars
|
|
1700
|
+
o = own(vars, e.name) ?? e;
|
|
1466
1701
|
break;
|
|
1467
1702
|
case 'const':
|
|
1468
1703
|
o = e;
|
|
@@ -1481,8 +1716,12 @@ const substituter = (vars) => {
|
|
|
1481
1716
|
case 'min':
|
|
1482
1717
|
case 'max':
|
|
1483
1718
|
case 'prod':
|
|
1719
|
+
case 'tup':
|
|
1484
1720
|
o = { op: e.op, a: se(e.a), b: se(e.b) };
|
|
1485
1721
|
break;
|
|
1722
|
+
case 'proj':
|
|
1723
|
+
o = { op: 'proj', k: e.k, a: se(e.a) };
|
|
1724
|
+
break;
|
|
1486
1725
|
default: return unreachable(e);
|
|
1487
1726
|
}
|
|
1488
1727
|
me.set(e, o);
|
|
@@ -1540,7 +1779,7 @@ export function checkRecurCases(l, r) {
|
|
|
1540
1779
|
export function progressObligations(m0, laws, instances, p) {
|
|
1541
1780
|
const m = plainOf(m0), own = m0.sums && Object.keys(m0.sums).length ? m0.assume : m.assume;
|
|
1542
1781
|
scope(m, laws); // (as obligations scopes it: a param ?f would be read as the flag f's variable here too)
|
|
1543
|
-
if (!m.inputs.every(i =>
|
|
1782
|
+
if (!m.inputs.every(i => Object.hasOwn(p.request, i)) || !Object.values(p.request).every(natural) || mentions(p.when, m.inputs))
|
|
1544
1783
|
throw new Error('outside the kernel');
|
|
1545
1784
|
for (const e of Object.values(p.request))
|
|
1546
1785
|
if (mentions({ op: '=', a: e, b: e }, m.inputs))
|
|
@@ -1671,10 +1910,10 @@ export function stepLawObligations(machine, laws, instances, sl) {
|
|
|
1671
1910
|
const lawsOnly = (inst) => Object.fromEntries(Object.entries(inst).filter(([n]) => !n.startsWith('?')));
|
|
1672
1911
|
export function checkStepLaws(m, laws, stepLaws, proof) {
|
|
1673
1912
|
return checkLaws(m, laws, proof.laws) && stepLaws.every(sl => {
|
|
1674
|
-
const inst = proof.perStep
|
|
1913
|
+
const inst = own(proof.perStep ?? {}, sl.name) ?? proof.instances, plain = plainOf(m);
|
|
1675
1914
|
const zone = stepZone(plain, sl.steps ?? 1);
|
|
1676
1915
|
return [...assumed(laws, inst), ...assumed(plain.facts ?? [], inst), ...closing(plain, lawsOnly(inst))].every(c => !mentions(c, zone))
|
|
1677
|
-
&& stepLawObligations(m, laws, inst, sl).every(g => proof.trees
|
|
1916
|
+
&& stepLawObligations(m, laws, inst, sl).every(g => { const t = own(proof.trees, g.name); return t !== undefined && check(g, t); });
|
|
1678
1917
|
});
|
|
1679
1918
|
}
|
|
1680
1919
|
// The names a k-step law's run introduces past its first frame: each later step's inputs, quantified flags and their
|
|
@@ -1712,8 +1951,9 @@ export const kInductionStart = (machine, kl, instances) => {
|
|
|
1712
1951
|
export function checkKInduction(m, laws, kl, proof) {
|
|
1713
1952
|
const steps = kInductionSteps(m, kl), plain = plainOf(m);
|
|
1714
1953
|
scope(plain, [...laws, kl.law]);
|
|
1715
|
-
const start = kInductionStart(m, kl, proof.perStep
|
|
1716
|
-
|
|
1954
|
+
const start = kInductionStart(m, kl, own(proof.perStep ?? {}, `${kl.law.name} @start`) ?? proof.instances);
|
|
1955
|
+
const t = own(proof.trees, start.name);
|
|
1956
|
+
return t !== undefined && check(start, t) && checkStepLaws(m, laws, steps, proof);
|
|
1717
1957
|
}
|
|
1718
1958
|
export function foldObligations(f, laws) {
|
|
1719
1959
|
if (![...Object.values(f.leaf), ...Object.values(f.node)].every(natural) || !laws.every(law => !law.forall && !law.keys?.length && naturalC(law.holds) && !mentions(law.holds, f.inputs)))
|
|
@@ -1765,7 +2005,7 @@ export const qdif = (a, b) => radd(rscale(b.den, a.row), rscale(-a.den, b.row));
|
|
|
1765
2005
|
// A row as a Lin, its coefficients safe integers (else undefined: a row past 2^53 is not read, so the check is weaker,
|
|
1766
2006
|
// never wrong); and its key, as `canon` writes a Lin (zero coefficients dropped: Lean's `same` compares by value).
|
|
1767
2007
|
export const qLin = (r) => {
|
|
1768
|
-
const out =
|
|
2008
|
+
const out = Object.create(null);
|
|
1769
2009
|
for (const [x, c] of r.cs) {
|
|
1770
2010
|
if (c < -(2n ** 53n) + 1n || c > 2n ** 53n - 1n)
|
|
1771
2011
|
return undefined;
|
|
@@ -1968,7 +2208,7 @@ export function checkQMachine(m, laws, trees) {
|
|
|
1968
2208
|
if (m.pre && qOccurs(m.pre, assigned))
|
|
1969
2209
|
return false;
|
|
1970
2210
|
return laws.every(law => {
|
|
1971
|
-
const t = trees
|
|
2211
|
+
const t = own(trees, law.name), goals = qObligations(m, laws, law);
|
|
1972
2212
|
return !!t && !qOccurs(law.holds, m.inputs) && checkQTree(goals.start, t.start) && checkQTree(goals.step, t.step);
|
|
1973
2213
|
});
|
|
1974
2214
|
}
|
|
@@ -2043,7 +2283,9 @@ export function countedGoal(g, insts) {
|
|
|
2043
2283
|
return hs && { name: g.name, hyps: [...g.hyps, ...hs] };
|
|
2044
2284
|
}
|
|
2045
2285
|
export const postName = (map) => `${map}#`;
|
|
2046
|
-
const declsOf = (m) => Object.entries(m.counts ?? {}).map(([map, c]) => ({ map, j: c.j, key: c.key, below: c.below, body: c.body }));
|
|
2286
|
+
const declsOf = (m) => Object.entries(m.counts ?? {}).map(([map, c]) => ({ map, j: c.j, key: c.key, below: c.below, body: c.body, ...(c.base ? { lo: c.lo, base: c.base } : {}) }));
|
|
2287
|
+
// A declaration's second parameter (Lean's CDecl.lo, base): one parameter is lo = j, base 0.
|
|
2288
|
+
const loOf = (d) => d.lo ?? d.j, baseOf = (d) => d.base ?? 0;
|
|
2047
2289
|
// stateOnly: the bound and body read no input and no map a request carries (Lean: avoidsE (extVars ++ flagVars) and
|
|
2048
2290
|
// avoidsMapsE carried; a count reads no flag, quantified flag or witness here, countsScope's).
|
|
2049
2291
|
const stateOnly = (m, d) => [d.below, d.body].every(t => {
|
|
@@ -2067,27 +2309,34 @@ function countsScope(m, laws) {
|
|
|
2067
2309
|
const what = `${m.name}'s count ${c}`;
|
|
2068
2310
|
if (declared.includes(postName(c)))
|
|
2069
2311
|
throw new Error(`${what}: its post name ${postName(c)} is already a name of ${m.name}`);
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
for (const x of [
|
|
2312
|
+
// (two parameters: lo a plain name apart from j and key, base a positive integer; one: neither)
|
|
2313
|
+
if ((d.lo === undefined) !== (d.base === undefined) || (d.base !== undefined && !(Number.isSafeInteger(d.base) && d.base > 0)))
|
|
2314
|
+
throw new Error(`${what}: lo and base go together, base a positive integer`);
|
|
2315
|
+
const ps = d.lo === undefined ? [d.j] : [d.j, d.lo];
|
|
2316
|
+
for (const x of [...ps, d.key])
|
|
2317
|
+
plainName(what, x);
|
|
2318
|
+
if (new Set([...ps, d.key]).size !== ps.length + 1)
|
|
2319
|
+
throw new Error(`${what}: j, lo and key are one name`);
|
|
2320
|
+
for (const x of [...ps, d.key])
|
|
2075
2321
|
if ([...declared, ...bound, ...lawNames].includes(x))
|
|
2076
2322
|
throw new Error(`${what}: ${x} is already a name of ${m.name} or a law`);
|
|
2077
2323
|
unquantified(what, d.below);
|
|
2078
2324
|
unquantified(what, d.body);
|
|
2079
|
-
inScope(`${what}'s bound`, [d.below], [...names,
|
|
2080
|
-
inScope(`${what}'s body`, [d.body], [...names,
|
|
2325
|
+
inScope(`${what}'s bound`, [d.below], [...names, ...ps], [...maps, ...carried, ...fns]);
|
|
2326
|
+
inScope(`${what}'s body`, [d.body], [...names, ...ps, d.key], [...maps, ...carried, ...fns]);
|
|
2081
2327
|
if (!natural(d.below) || !natural(d.body))
|
|
2082
2328
|
throw new Error(`${what}: a negative constant or factor is outside the kernel`);
|
|
2083
2329
|
}
|
|
2084
2330
|
}
|
|
2085
|
-
//
|
|
2331
|
+
// countKeyParts (CountMachine.lean): the terms a key term gives the parameters: one, the term (j a constant 0); two, the term written base·a + b
|
|
2332
|
+
// (j at a, lo at b). toCount: a read cell as Count.lean's count (its variable, the declaration with its parameters at
|
|
2333
|
+
// the parts); toCounts: each.
|
|
2334
|
+
const countKeyParts = (d, e) => (!baseOf(d) ? [{ op: 'const', value: 0 }, e] : e.op === 'add' && e.a.op === 'mul' && e.a.k === baseOf(d) ? [e.a.a, e.b] : undefined);
|
|
2086
2335
|
const toCount = (ds, r) => {
|
|
2087
|
-
const d = ds.find(x => x.map === r[1]);
|
|
2088
|
-
if (!d)
|
|
2336
|
+
const d = ds.find(x => x.map === r[1]), p = d && countKeyParts(d, r[2]);
|
|
2337
|
+
if (!d || !p)
|
|
2089
2338
|
return undefined;
|
|
2090
|
-
const at = { vars: { [d.j]:
|
|
2339
|
+
const at = { vars: { [d.j]: p[0], [loOf(d)]: p[1] }, maps: {}, flags: {} };
|
|
2091
2340
|
return { v: r[0], key: d.key, below: substE(d.below, at), body: substE(d.body, at) };
|
|
2092
2341
|
};
|
|
2093
2342
|
function toCounts(ds, rs) {
|
|
@@ -2100,9 +2349,25 @@ function toCounts(ds, rs) {
|
|
|
2100
2349
|
}
|
|
2101
2350
|
return out;
|
|
2102
2351
|
}
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
const
|
|
2352
|
+
// readHyp: the read's variable is its cell; for two parameters, where the second part is below the base.
|
|
2353
|
+
const readHyp = (ds, r) => {
|
|
2354
|
+
const eq = { op: '=', a: { op: 'var', name: r[0] }, b: { op: 'get', map: r[1], key: r[2] } }, d = ds.find(x => x.map === r[1]), p = d && baseOf(d) ? countKeyParts(d, r[2]) : undefined;
|
|
2355
|
+
return p ? { op: 'or', xs: [{ op: '<=', a: { op: 'const', value: baseOf(d) }, b: p[1] }, eq] } : eq;
|
|
2356
|
+
};
|
|
2357
|
+
// A count at least its body at distinct keys (Lean's pickHyp, pickHyps): a pick names a read and key terms; where the
|
|
2358
|
+
// terms are distinct and below the count's bound, the count is at least the sum of its body at them. It holds wherever
|
|
2359
|
+
// the count is its sum, so a certificate may name any terms; a pick of no read adds nothing.
|
|
2360
|
+
const allNe = (k, ks) => (ks.length ? { op: 'and', xs: [{ op: 'not', x: { op: '=', a: k, b: ks[0] } }, allNe(k, ks.slice(1))] } : trueC);
|
|
2361
|
+
const distinctBelow = (b, ks) => (ks.length ? { op: 'and', xs: [{ op: '<', a: ks[0], b }, { op: 'and', xs: [allNe(ks[0], ks.slice(1)), distinctBelow(b, ks.slice(1))] }] } : trueC);
|
|
2362
|
+
const pickHyp = (c, ks) => ({ op: 'or', xs: [{ op: 'not', x: distinctBelow(c.below, ks) },
|
|
2363
|
+
{ op: '<=', a: sumE(ks.map(k => substE(c.body, { vars: { [c.key]: k }, maps: {}, flags: {} }))), b: { op: 'var', name: c.v } }] });
|
|
2364
|
+
const pickHyps = (ds, rs, ps) => ps.flatMap(([v, ks]) => {
|
|
2365
|
+
const r = rs.find(x => x[0] === v), c = r && toCount(ds, r);
|
|
2366
|
+
return c ? [pickHyp(c, ks)] : [];
|
|
2367
|
+
});
|
|
2368
|
+
const picksTyped = (ps) => ps === undefined || (Array.isArray(ps) && ps.every(p => Array.isArray(p) && p.length === 2 && typeof p[0] === 'string' && Array.isArray(p[1]) && p[1].every(safeTerm)));
|
|
2369
|
+
// readFresh: no hypothesis, declaration or key term reads v, and v is no declaration's j, lo or key.
|
|
2370
|
+
const readFresh = (hyps, ds, rs, v) => hyps.every(h => !readsVar(h, v)) && ds.every(d => !readsVar(d.below, v) && !readsVar(d.body, v) && d.j !== v && loOf(d) !== v && d.key !== v) && rs.every(r => !readsVar(r[2], v));
|
|
2106
2371
|
// readsOk: the variables distinct (cDistinct), each fresh, each key term reading no declaration's key.
|
|
2107
2372
|
const readsOk = (hyps, ds, rs) => new Set(rs.map(r => r[0])).size === rs.length && rs.every(r => readFresh(hyps, ds, rs, r[0]) && ds.every(d => !readsVar(r[2], d.key)));
|
|
2108
2373
|
// TypeScript's own condition on the certificate, an input from outside: each read a name, a map's name and a term over
|
|
@@ -2122,6 +2387,10 @@ const safeTerm = (t) => {
|
|
|
2122
2387
|
case 'max':
|
|
2123
2388
|
case 'prod': return safeTerm(e.a) && safeTerm(e.b);
|
|
2124
2389
|
case 'if': return safeCond(e.c) && safeTerm(e.then) && safeTerm(e.else);
|
|
2390
|
+
// Count certificates still use the scalar/packed-key language, not native tuple terms.
|
|
2391
|
+
// These were refused by the default too; make that unchanged boundary explicit.
|
|
2392
|
+
case 'tup':
|
|
2393
|
+
case 'proj': return false;
|
|
2125
2394
|
default: return false;
|
|
2126
2395
|
}
|
|
2127
2396
|
};
|
|
@@ -2140,21 +2409,21 @@ function checkCountedW(hyps, cs, cert) {
|
|
|
2140
2409
|
const hs = countHyps(cs, cert.insts);
|
|
2141
2410
|
return hs !== undefined && check({ name: 'counted', hyps: [...hyps, ...hs] }, cert.tree);
|
|
2142
2411
|
}
|
|
2143
|
-
// checkMC: each declaration's j and
|
|
2144
|
-
// term, and checkCountedW accepts the counted goal they make (the goal's hypotheses, each read variable equal to
|
|
2145
|
-
// cell; Lean's names the hypotheses it uses, each the goal's).
|
|
2412
|
+
// checkMC: each declaration's j and lo differ from its key, the reads are fresh, no read is a witness or reads one in
|
|
2413
|
+
// its key term, and checkCountedW accepts the counted goal they make (the goal's hypotheses, each read variable equal to
|
|
2414
|
+
// its cell; Lean's names the hypotheses it uses, each the goal's).
|
|
2146
2415
|
export function checkMC(hyps, ds, k) {
|
|
2147
|
-
if (!(ds.every(d => d.j !== d.key) && readsTyped(k.reads) && Array.isArray(k.insts) && k.insts.every(i => !!i && safeTerm(i.below)) && readsOk(hyps, ds, k.reads)))
|
|
2416
|
+
if (!(ds.every(d => d.j !== d.key && loOf(d) !== d.key) && picksTyped(k.picks) && readsTyped(k.reads) && Array.isArray(k.insts) && k.insts.every(i => !!i && safeTerm(i.below)) && readsOk(hyps, ds, k.reads)))
|
|
2148
2417
|
return false;
|
|
2149
2418
|
if (!k.reads.every(r => k.insts.every(i => i.wit !== r[0] && !readsVar(r[2], i.wit))))
|
|
2150
2419
|
return false;
|
|
2151
2420
|
const cs = toCounts(ds, k.reads);
|
|
2152
|
-
return cs !== undefined && checkCountedW([...hyps, ...k.reads.map(readHyp)], cs, { insts: k.insts, tree: k.tree });
|
|
2421
|
+
return cs !== undefined && checkCountedW([...hyps, ...k.reads.map(r => readHyp(ds, r)), ...pickHyps(ds, k.reads, k.picks ?? [])], cs, { insts: k.insts, tree: k.tree });
|
|
2153
2422
|
}
|
|
2154
2423
|
// The goal a certificate's tree refutes (what the search and the exporter read), or none.
|
|
2155
2424
|
export function mcGoal(hyps, ds, k) {
|
|
2156
2425
|
const cs = toCounts(ds, k.reads);
|
|
2157
|
-
return cs && countedGoal({ name: 'counted', hyps: [...hyps, ...k.reads.map(readHyp)], counts: cs }, k.insts);
|
|
2426
|
+
return cs && countedGoal({ name: 'counted', hyps: [...hyps, ...k.reads.map(r => readHyp(ds, r)), ...pickHyps(ds, k.reads, k.picks ?? [])], counts: cs }, k.insts);
|
|
2158
2427
|
}
|
|
2159
2428
|
// The goals of a machine with counts: obligations', each law proven renamed (renLaw), with the counts each declares.
|
|
2160
2429
|
export function countedObligations(m, laws, instances = {}, only) {
|
|
@@ -2168,13 +2437,14 @@ export function countedObligations(m, laws, instances = {}, only) {
|
|
|
2168
2437
|
const ren = { vars: {}, maps: Object.fromEntries(Object.keys(m.counts ?? {}).map(c => [c, (key) => ({ op: 'get', map: postName(c), key })])), flags: {} };
|
|
2169
2438
|
const proven = (law) => { const h = substC(law.holds, ren); return law.forall ? substC(h, { vars: { [law.forall]: { op: 'var', name: `${law.forall}#` } }, maps: {}, flags: {} }) : h; };
|
|
2170
2439
|
const all = declsOf(m), st = all.filter(d => stateOnly(m, d));
|
|
2171
|
-
const after = (at) => st.map(d => ({ map: postName(d.map),
|
|
2440
|
+
const after = (at) => st.map(d => ({ ...d, map: postName(d.map), below: substE(d.below, at), body: substE(d.body, at) }));
|
|
2172
2441
|
const pre = preOf(m), u = updateOf(m), assume = m.assume ? [inlineFlags(m, m.assume)] : [];
|
|
2173
2442
|
return laws.flatMap((law, i) => {
|
|
2174
2443
|
if (only !== undefined && i !== only)
|
|
2175
2444
|
return [];
|
|
2176
|
-
const
|
|
2177
|
-
const
|
|
2445
|
+
const inst = own(instances, law.name) ?? {};
|
|
2446
|
+
const start = given(m, holds(from(startOf(m)), pre), inst);
|
|
2447
|
+
const turn = step(holds(given(m, from(), inst), [...pre, ...assume, ...assumed(laws, inst), ...closing(m, inst)]), u);
|
|
2178
2448
|
return [{ goal: goal(`${law.name} @start`, start, atEnd(start, proven(law))), decls: after(start.at) },
|
|
2179
2449
|
{ goal: goal(`${law.name} @step`, turn, atEnd(turn, proven(law))), decls: [...all, ...after(turn.at)] }];
|
|
2180
2450
|
});
|
|
@@ -2191,7 +2461,7 @@ function witsOk(m, laws, k) {
|
|
|
2191
2461
|
}
|
|
2192
2462
|
// Lean's checkCMachine: every goal has a certificate, its witnesses fresh, and every certificate checks.
|
|
2193
2463
|
export function checkCountedLaws(m, laws, proof) {
|
|
2194
|
-
return countedObligations(m, laws, proof.instances).every(({ goal: g, decls }) => { const k = proof.certs
|
|
2464
|
+
return countedObligations(m, laws, proof.instances).every(({ goal: g, decls }) => { const k = own(proof.certs, g.name); return k !== undefined && witsOk(m, laws, k) && checkMC(g.hyps, decls, k); });
|
|
2195
2465
|
}
|
|
2196
2466
|
// SGhostedM.full: the original with each ghost's start and step after its own (Lean: lists appended; here records, whose
|
|
2197
2467
|
// names smghostOk keeps apart from the original's).
|
|
@@ -2225,3 +2495,23 @@ export function ghostedOk(g) {
|
|
|
2225
2495
|
// The laws of a ghosted machine, checked: the ghosts as smghostOk says, and the laws of the ghosted machine as checkLaws
|
|
2226
2496
|
// says (Lean: smghostOk and checkSMachine of SGhostedM.full).
|
|
2227
2497
|
export const checkGhostedLaws = (g, laws, proof) => ghostedOk(g) && checkLaws(ghostedMachine(g), laws, proof);
|
|
2498
|
+
// GhostMap.lean's forall_some_of_ghostsubst, the ∃ reading: a law with "there exists" (`some`, its body c) holds of the
|
|
2499
|
+
// original (Lean's ExHolds: for every value of its keys, some natural for each bound name where c holds) where the
|
|
2500
|
+
// ghosted machine's law `plain` is c with each bound name x replaced by a term sub[x] (Lean's substC σ c: a term over
|
|
2501
|
+
// anything of the ghosted machine, a ghost map at a key, several, the law's keys). The caller has the plain law proven
|
|
2502
|
+
// of ghostedMachine(g) (checkLaws). What Lean takes: the ghosts smghostOk's (ghostedOk); the plain law exactly c with sub
|
|
2503
|
+
// in place, about the law's keys (e.law.key, e.keys2); c reading no ghost variable and no ghost map (avoids, avoidsMaps);
|
|
2504
|
+
// the bound names sub's (σ's names). And where the law's reading and Lean's statement would part: c in Lean's terms (no
|
|
2505
|
+
// quantified condition, no flag), each bound name once, none a key of the law (ExHolds writes the bound names over the
|
|
2506
|
+
// keys' values) nor a name of the original or of a ghost.
|
|
2507
|
+
export function someOfGhostsOk(g, law, plain, sub) {
|
|
2508
|
+
const xs = law.some ?? [], keys = [...(law.forall ? [law.forall] : []), ...(law.keys ?? [])], o = g.orig, r = reads(law.holds), text = JSON.stringify(law.holds);
|
|
2509
|
+
const vs = Object.keys(g.vars), ms = Object.keys(g.maps);
|
|
2510
|
+
const names = [...o.params, ...o.inputs, ...Object.keys(o.state), ...Object.keys(o.maps ?? {}), ...(o.inputMaps ?? []), ...Object.keys(o.fns ?? {}), ...vs, ...ms];
|
|
2511
|
+
return ghostedOk(g) && xs.length > 0 && new Set(xs).size === xs.length && Object.keys(sub).length === xs.length && xs.every(x => Object.hasOwn(sub, x))
|
|
2512
|
+
&& !text.includes('"op":"all"') && !text.includes('"op":"flag"') && !plain.some?.length
|
|
2513
|
+
&& plain.forall === law.forall && JSON.stringify(plain.keys ?? []) === JSON.stringify(law.keys ?? [])
|
|
2514
|
+
&& JSON.stringify(plain.holds) === JSON.stringify(substC(law.holds, { vars: Object.assign(Object.create(null), sub), maps: {}, flags: {} }))
|
|
2515
|
+
&& vs.every(v => !r.vars.has(v)) && ms.every(mp => !r.maps.has(mp))
|
|
2516
|
+
&& xs.every(x => !keys.includes(x) && !names.includes(x));
|
|
2517
|
+
}
|