gdp-wired 0.45.0 → 0.47.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 +61 -8
- 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 +423 -31
- package/dist/core/calendar.d.ts +29 -0
- package/dist/core/calendar.js +82 -0
- package/dist/core/cli.d.ts +4 -0
- package/dist/core/cli.js +206 -40
- package/dist/core/compose.js +3 -1
- package/dist/core/counts.d.ts +4 -1
- package/dist/core/counts.js +314 -85
- 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 +2 -1
- package/dist/core/data.js +223 -24
- 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 +37 -3
- package/dist/core/dsl.js +167 -26
- package/dist/core/effect-schema.d.ts +90 -0
- package/dist/core/effect-schema.js +500 -0
- package/dist/core/effect.d.ts +4 -11
- package/dist/core/effect.js +3 -79
- package/dist/core/exists.d.ts +1 -0
- package/dist/core/exists.js +191 -16
- package/dist/core/extract-c.d.ts +2 -0
- package/dist/core/extract-c.js +169 -22
- package/dist/core/extract-check.d.ts +1 -0
- package/dist/core/extract-check.js +97 -6
- package/dist/core/extract-go-check.d.ts +1 -1
- package/dist/core/extract-go-check.js +26 -5
- package/dist/core/extract-go.d.ts +8 -0
- package/dist/core/extract-go.js +67 -6
- 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 +2 -0
- package/dist/core/extract-orm.d.ts +12 -0
- package/dist/core/extract-orm.js +244 -0
- package/dist/core/extract-print.js +20 -1
- package/dist/core/extract-py.d.ts +1 -0
- package/dist/core/extract-py.js +1 -1
- package/dist/core/extract-read.d.ts +26 -1
- package/dist/core/extract-read.js +14 -7
- package/dist/core/extract-rs.js +63 -15
- package/dist/core/extract-shapes.d.ts +1 -0
- package/dist/core/extract-shapes.js +84 -0
- package/dist/core/extract-sol.d.ts +10 -1
- package/dist/core/extract-sol.js +70 -22
- package/dist/core/extract-standalone.d.ts +3 -0
- package/dist/core/extract-standalone.js +221 -0
- package/dist/core/extract-step.d.ts +151 -1
- package/dist/core/extract-step.js +1232 -60
- package/dist/core/extract-swift.js +6 -2
- package/dist/core/extract.d.ts +9 -0
- package/dist/core/extract.js +77 -7
- package/dist/core/fiber-run.d.ts +5 -0
- package/dist/core/fiber-run.js +18 -0
- package/dist/core/infer.d.ts +4 -1
- package/dist/core/infer.js +918 -217
- package/dist/core/jvm/JavaReader.java +23 -6
- package/dist/core/jvm/Jvm.java +3 -0
- package/dist/core/jvm/KtReader.java +6 -5
- package/dist/core/laws.d.ts +10 -3
- package/dist/core/laws.js +226 -23
- package/dist/core/machine.d.ts +15 -1
- package/dist/core/machine.js +187 -15
- 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 +183 -10
- package/dist/core/py/reader.py +234 -40
- 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/Cargo.toml +1 -1
- package/dist/core/rs/src/main.rs +466 -11
- 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 +57 -4
- package/dist/core/sol-concrete.d.ts +23 -0
- package/dist/core/sol-concrete.js +195 -0
- package/dist/core/solver.js +3 -1
- package/dist/core/swift/Sources/gdp-swiftreader/main.swift +50 -7
- package/dist/core/system.js +1 -1
- package/dist/core/transfer.js +22 -9
- package/dist/core/translate.js +39 -10
- package/dist/core/watch.js +50 -5
- package/dist/interchange/format.js +11 -1
- package/dist/kernel/cdcl.d.ts +5 -0
- package/dist/kernel/cdcl.js +56 -9
- package/dist/kernel/epr.js +108 -26
- package/dist/kernel/kernel.d.ts +29 -1
- package/dist/kernel/kernel.js +325 -62
- package/dist/kernel/model-sequence.d.ts +3 -0
- package/dist/kernel/model-sequence.js +34 -0
- package/dist/kernel/prove.d.ts +10 -3
- package/dist/kernel/prove.js +332 -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.js +30 -37
- package/dist/lean-kernel/lean-terms.js +5 -0
- package/dist/lean-kernel/library.js +10 -10
- package/package.json +6 -1
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,11 +179,51 @@ 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
|
+
}
|
|
174
201
|
}
|
|
175
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];
|
|
223
|
+
}
|
|
224
|
+
const rest = keyParts(e.b, path);
|
|
225
|
+
return rest && [a, ...rest];
|
|
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
|
|
178
229
|
// the same for the same term). A leaf reads it through rows that hold whichever way the choice goes (termRows), so a
|
|
@@ -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,7 +677,9 @@ 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
|
}
|
|
@@ -587,7 +702,9 @@ export function substE(e, s) {
|
|
|
587
702
|
case 'add':
|
|
588
703
|
case 'sub':
|
|
589
704
|
case 'min':
|
|
590
|
-
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) };
|
|
591
708
|
default: return unreachable(e);
|
|
592
709
|
}
|
|
593
710
|
}
|
|
@@ -678,8 +795,8 @@ export const crossFree = (c, keys) => {
|
|
|
678
795
|
if (hit)
|
|
679
796
|
return hit;
|
|
680
797
|
const zero = { op: 'const', value: 0 };
|
|
681
|
-
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) }
|
|
682
|
-
: 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);
|
|
683
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)) }
|
|
684
801
|
: c.op === 'all' ? { ...c, c: crossFree(c.c, keys) } : c.op === 'flag' ? c : { ...c, a: e(c.a), b: e(c.b) };
|
|
685
802
|
if (!crossCache.has(c))
|
|
@@ -757,6 +874,7 @@ function reads(t, out = { vars: new Set(), maps: new Set(), flags: new Set() })
|
|
|
757
874
|
reads(y, out);
|
|
758
875
|
break;
|
|
759
876
|
case 'mul':
|
|
877
|
+
case 'proj':
|
|
760
878
|
reads(t.a, out);
|
|
761
879
|
break;
|
|
762
880
|
case 'if':
|
|
@@ -769,6 +887,7 @@ function reads(t, out = { vars: new Set(), maps: new Set(), flags: new Set() })
|
|
|
769
887
|
case 'min':
|
|
770
888
|
case 'max':
|
|
771
889
|
case 'prod':
|
|
890
|
+
case 'tup':
|
|
772
891
|
case '<':
|
|
773
892
|
case '<=':
|
|
774
893
|
case '=':
|
|
@@ -802,8 +921,8 @@ function inScope(what, ts, vars, maps = [], flags = []) {
|
|
|
802
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
|
|
803
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
|
|
804
923
|
// accepted, false at ?f = 0; Lean refuses it).
|
|
805
|
-
const plainName = (what, name) => { if (name === '1' || /[*[\],:#?~'
|
|
806
|
-
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 * [ ] , : # ? ~ ' ⟦ ⟧ ⟨ ⟩ |)`); };
|
|
807
926
|
const distinct = (what, names) => { const seen = new Set(); for (const name of names) {
|
|
808
927
|
if (seen.has(name))
|
|
809
928
|
throw new Error(`${what} declares ${name} twice`);
|
|
@@ -856,9 +975,24 @@ export function scope(m, laws = []) {
|
|
|
856
975
|
inScope(what, [c], names, readable, flags.slice(0, i));
|
|
857
976
|
}
|
|
858
977
|
});
|
|
978
|
+
const arity = mapArities(m, laws);
|
|
859
979
|
for (const [map, w] of Object.entries(m.step.maps ?? {})) {
|
|
860
980
|
const what = `${m.name}'s write to ${map}`;
|
|
861
|
-
|
|
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) {
|
|
862
996
|
bound(what, w.each, w.value);
|
|
863
997
|
unquantified(what, w.value);
|
|
864
998
|
inScope(what, [w.value], [...names, w.each], readable, flags);
|
|
@@ -902,11 +1036,84 @@ export function scope(m, laws = []) {
|
|
|
902
1036
|
for (const fact of facts)
|
|
903
1037
|
inScope(`fact ${fact.name}`, [fact.holds], [...m.params, ...keysOf(fact)], fns);
|
|
904
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) };
|
|
905
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,
|
|
906
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
|
|
907
1106
|
// comparison whose other side doesn't read the key. Then t is constant between finitely many keys (the runtimes'
|
|
908
1107
|
// breakpoints: each map's cells and each compared value, shifted by the offsets), so a runtime decides it on those.
|
|
909
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
|
+
};
|
|
910
1117
|
export const keyTerm = (e, key) => e.op === 'var' ? e.name === key
|
|
911
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))
|
|
912
1119
|
: e.op === 'sub' && e.a.op === 'var' && e.a.name === key && !readsKey(e.b, key);
|
|
@@ -919,7 +1126,12 @@ function keyOnly(what, t, key, fns = []) {
|
|
|
919
1126
|
bad();
|
|
920
1127
|
break;
|
|
921
1128
|
case 'get':
|
|
922
|
-
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)))
|
|
923
1135
|
walk(u.key);
|
|
924
1136
|
break;
|
|
925
1137
|
case 'const':
|
|
@@ -935,6 +1147,7 @@ function keyOnly(what, t, key, fns = []) {
|
|
|
935
1147
|
u.xs.forEach(walk);
|
|
936
1148
|
break;
|
|
937
1149
|
case 'mul':
|
|
1150
|
+
case 'proj':
|
|
938
1151
|
walk(u.a);
|
|
939
1152
|
break;
|
|
940
1153
|
case 'if':
|
|
@@ -944,21 +1157,29 @@ function keyOnly(what, t, key, fns = []) {
|
|
|
944
1157
|
break;
|
|
945
1158
|
case '<':
|
|
946
1159
|
case '<=':
|
|
947
|
-
case '=':
|
|
948
|
-
if (keyTerm(
|
|
949
|
-
walk(
|
|
950
|
-
else if (keyTerm(
|
|
951
|
-
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);
|
|
952
1165
|
else {
|
|
953
|
-
walk(
|
|
954
|
-
walk(
|
|
955
|
-
}
|
|
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);
|
|
956
1175
|
break;
|
|
1176
|
+
}
|
|
957
1177
|
case 'add':
|
|
958
1178
|
case 'sub':
|
|
959
1179
|
case 'min':
|
|
960
1180
|
case 'max':
|
|
961
1181
|
case 'prod':
|
|
1182
|
+
case 'tup':
|
|
962
1183
|
walk(u.a);
|
|
963
1184
|
walk(u.b);
|
|
964
1185
|
break;
|
|
@@ -971,9 +1192,10 @@ function keyOnly(what, t, key, fns = []) {
|
|
|
971
1192
|
// another (the comparisons between two of them, plainly, are set aside before: crossFree).
|
|
972
1193
|
function apart(what, t, key, keys) {
|
|
973
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)));
|
|
974
1196
|
const walk = (u) => {
|
|
975
|
-
if ((u.op === 'get' && keyTerm(u.key, key) && other(u.key))
|
|
976
|
-
|| ((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))))
|
|
977
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)`);
|
|
978
1200
|
for (const x of Object.values(u))
|
|
979
1201
|
if (x && typeof x === 'object')
|
|
@@ -993,13 +1215,15 @@ export const plainKey = (t, key) => {
|
|
|
993
1215
|
case 'all': return walk(u.c);
|
|
994
1216
|
case 'and':
|
|
995
1217
|
case 'or': return u.xs.every(walk);
|
|
996
|
-
case 'mul':
|
|
1218
|
+
case 'mul':
|
|
1219
|
+
case 'proj': return walk(u.a);
|
|
997
1220
|
case 'if': return walk(u.c) && walk(u.then) && walk(u.else);
|
|
998
1221
|
case 'add':
|
|
999
1222
|
case 'sub':
|
|
1000
1223
|
case 'min':
|
|
1001
1224
|
case 'max':
|
|
1002
1225
|
case 'prod':
|
|
1226
|
+
case 'tup':
|
|
1003
1227
|
case '<':
|
|
1004
1228
|
case '<=':
|
|
1005
1229
|
case '=': return walk(u.a) && walk(u.b);
|
|
@@ -1091,6 +1315,8 @@ const sumTermOk = (e, key) => {
|
|
|
1091
1315
|
case 'min':
|
|
1092
1316
|
case 'max':
|
|
1093
1317
|
case 'prod': return sumTermOk(e.a, key) && sumTermOk(e.b, key);
|
|
1318
|
+
case 'tup':
|
|
1319
|
+
case 'proj': return false;
|
|
1094
1320
|
default: return unreachable(e);
|
|
1095
1321
|
}
|
|
1096
1322
|
};
|
|
@@ -1327,6 +1553,8 @@ const natE = (e, env) => {
|
|
|
1327
1553
|
}
|
|
1328
1554
|
case 'if': return natC(e.c, env) ? natE(e.then, env) : natE(e.else, env);
|
|
1329
1555
|
case 'get': return 0n;
|
|
1556
|
+
case 'tup':
|
|
1557
|
+
case 'proj': throw new Error('a loop\'s run is read over no tuple');
|
|
1330
1558
|
default: return unreachable(e);
|
|
1331
1559
|
}
|
|
1332
1560
|
};
|
|
@@ -1414,6 +1642,8 @@ function foldTerm(e, f, fc) {
|
|
|
1414
1642
|
return isTC(c) ? f(e.then) : isFC(c) ? f(e.else) : { op: 'if', c, then: f(e.then), else: f(e.else) };
|
|
1415
1643
|
}
|
|
1416
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) };
|
|
1417
1647
|
default: return unreachable(e);
|
|
1418
1648
|
}
|
|
1419
1649
|
}
|
|
@@ -1486,8 +1716,12 @@ const substituter = (vars) => {
|
|
|
1486
1716
|
case 'min':
|
|
1487
1717
|
case 'max':
|
|
1488
1718
|
case 'prod':
|
|
1719
|
+
case 'tup':
|
|
1489
1720
|
o = { op: e.op, a: se(e.a), b: se(e.b) };
|
|
1490
1721
|
break;
|
|
1722
|
+
case 'proj':
|
|
1723
|
+
o = { op: 'proj', k: e.k, a: se(e.a) };
|
|
1724
|
+
break;
|
|
1491
1725
|
default: return unreachable(e);
|
|
1492
1726
|
}
|
|
1493
1727
|
me.set(e, o);
|
|
@@ -2049,7 +2283,9 @@ export function countedGoal(g, insts) {
|
|
|
2049
2283
|
return hs && { name: g.name, hyps: [...g.hyps, ...hs] };
|
|
2050
2284
|
}
|
|
2051
2285
|
export const postName = (map) => `${map}#`;
|
|
2052
|
-
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;
|
|
2053
2289
|
// stateOnly: the bound and body read no input and no map a request carries (Lean: avoidsE (extVars ++ flagVars) and
|
|
2054
2290
|
// avoidsMapsE carried; a count reads no flag, quantified flag or witness here, countsScope's).
|
|
2055
2291
|
const stateOnly = (m, d) => [d.below, d.body].every(t => {
|
|
@@ -2073,27 +2309,34 @@ function countsScope(m, laws) {
|
|
|
2073
2309
|
const what = `${m.name}'s count ${c}`;
|
|
2074
2310
|
if (declared.includes(postName(c)))
|
|
2075
2311
|
throw new Error(`${what}: its post name ${postName(c)} is already a name of ${m.name}`);
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
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])
|
|
2081
2321
|
if ([...declared, ...bound, ...lawNames].includes(x))
|
|
2082
2322
|
throw new Error(`${what}: ${x} is already a name of ${m.name} or a law`);
|
|
2083
2323
|
unquantified(what, d.below);
|
|
2084
2324
|
unquantified(what, d.body);
|
|
2085
|
-
inScope(`${what}'s bound`, [d.below], [...names,
|
|
2086
|
-
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]);
|
|
2087
2327
|
if (!natural(d.below) || !natural(d.body))
|
|
2088
2328
|
throw new Error(`${what}: a negative constant or factor is outside the kernel`);
|
|
2089
2329
|
}
|
|
2090
2330
|
}
|
|
2091
|
-
//
|
|
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);
|
|
2092
2335
|
const toCount = (ds, r) => {
|
|
2093
|
-
const d = ds.find(x => x.map === r[1]);
|
|
2094
|
-
if (!d)
|
|
2336
|
+
const d = ds.find(x => x.map === r[1]), p = d && countKeyParts(d, r[2]);
|
|
2337
|
+
if (!d || !p)
|
|
2095
2338
|
return undefined;
|
|
2096
|
-
const at = { vars: { [d.j]:
|
|
2339
|
+
const at = { vars: { [d.j]: p[0], [loOf(d)]: p[1] }, maps: {}, flags: {} };
|
|
2097
2340
|
return { v: r[0], key: d.key, below: substE(d.below, at), body: substE(d.body, at) };
|
|
2098
2341
|
};
|
|
2099
2342
|
function toCounts(ds, rs) {
|
|
@@ -2106,9 +2349,25 @@ function toCounts(ds, rs) {
|
|
|
2106
2349
|
}
|
|
2107
2350
|
return out;
|
|
2108
2351
|
}
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
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));
|
|
2112
2371
|
// readsOk: the variables distinct (cDistinct), each fresh, each key term reading no declaration's key.
|
|
2113
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)));
|
|
2114
2373
|
// TypeScript's own condition on the certificate, an input from outside: each read a name, a map's name and a term over
|
|
@@ -2128,6 +2387,10 @@ const safeTerm = (t) => {
|
|
|
2128
2387
|
case 'max':
|
|
2129
2388
|
case 'prod': return safeTerm(e.a) && safeTerm(e.b);
|
|
2130
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;
|
|
2131
2394
|
default: return false;
|
|
2132
2395
|
}
|
|
2133
2396
|
};
|
|
@@ -2146,21 +2409,21 @@ function checkCountedW(hyps, cs, cert) {
|
|
|
2146
2409
|
const hs = countHyps(cs, cert.insts);
|
|
2147
2410
|
return hs !== undefined && check({ name: 'counted', hyps: [...hyps, ...hs] }, cert.tree);
|
|
2148
2411
|
}
|
|
2149
|
-
// checkMC: each declaration's j and
|
|
2150
|
-
// term, and checkCountedW accepts the counted goal they make (the goal's hypotheses, each read variable equal to
|
|
2151
|
-
// 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).
|
|
2152
2415
|
export function checkMC(hyps, ds, k) {
|
|
2153
|
-
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)))
|
|
2154
2417
|
return false;
|
|
2155
2418
|
if (!k.reads.every(r => k.insts.every(i => i.wit !== r[0] && !readsVar(r[2], i.wit))))
|
|
2156
2419
|
return false;
|
|
2157
2420
|
const cs = toCounts(ds, k.reads);
|
|
2158
|
-
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 });
|
|
2159
2422
|
}
|
|
2160
2423
|
// The goal a certificate's tree refutes (what the search and the exporter read), or none.
|
|
2161
2424
|
export function mcGoal(hyps, ds, k) {
|
|
2162
2425
|
const cs = toCounts(ds, k.reads);
|
|
2163
|
-
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);
|
|
2164
2427
|
}
|
|
2165
2428
|
// The goals of a machine with counts: obligations', each law proven renamed (renLaw), with the counts each declares.
|
|
2166
2429
|
export function countedObligations(m, laws, instances = {}, only) {
|
|
@@ -2174,7 +2437,7 @@ export function countedObligations(m, laws, instances = {}, only) {
|
|
|
2174
2437
|
const ren = { vars: {}, maps: Object.fromEntries(Object.keys(m.counts ?? {}).map(c => [c, (key) => ({ op: 'get', map: postName(c), key })])), flags: {} };
|
|
2175
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; };
|
|
2176
2439
|
const all = declsOf(m), st = all.filter(d => stateOnly(m, d));
|
|
2177
|
-
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) }));
|
|
2178
2441
|
const pre = preOf(m), u = updateOf(m), assume = m.assume ? [inlineFlags(m, m.assume)] : [];
|
|
2179
2442
|
return laws.flatMap((law, i) => {
|
|
2180
2443
|
if (only !== undefined && i !== only)
|