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/core/dsl.js
CHANGED
|
@@ -17,14 +17,14 @@
|
|
|
17
17
|
// both runtimes do.
|
|
18
18
|
import { candidates, cone, searchCounted } from "./counts.js";
|
|
19
19
|
import { env as knobs } from "./solver.js";
|
|
20
|
-
import { argsKey, machineTerms, carriedView, compileFunction, divName, divisor, divisorsIn, evalC, evalE, interpret, isArithmetic, loadJs, modName, numbersOf, pairedArgs, seeded, withArithmetic } from "./machine.js";
|
|
20
|
+
import { argsKey, machineTerms, part, tuple, carriedView, compileFunction, divName, divisor, divisorsIn, evalC, evalE, interpret, isArithmetic, loadJs, modName, numbersOf, pairedArgs, seeded, withArithmetic } from "./machine.js";
|
|
21
21
|
import { allowed, keysOf, requestEnv, watch, watchWork } from "./watch.js";
|
|
22
|
-
import { ghostOf, proveSome, skolemOf, someHolds, substVars, substVarsE } from "./exists.js";
|
|
23
|
-
import { check, checkRecur, foldObligations, ghostedMachine, loopObligations, someOfGhostsOk, substC, sumMachine } from "../kernel/kernel.js";
|
|
24
|
-
import { certLoop, clock, counted, factsFor, fullRate, grow, proveLaw, proveLaws, proveLiveness, proveSetFast, proveStepLaws, usualProof, withDeadline, withWall } from "../kernel/prove.js";
|
|
22
|
+
import { ghostOf, proveSome, skolemOf, someHolds, substVars, substVarsE, witnessSupport } from "./exists.js";
|
|
23
|
+
import { check, mapArities, checkRecur, foldObligations, ghostedMachine, loopObligations, someOfGhostsOk, substC, sumMachine } from "../kernel/kernel.js";
|
|
24
|
+
import { certLoop, clock, counted, factsFor, fullRate, grow, pastWall, proveLaw, proveLaws, proveLiveness, proveSetFast, proveStepLaws, usualProof, withDeadline, withWall } from "../kernel/prove.js";
|
|
25
25
|
import { certCyclic, certOn, crossCyclic, cyclicOn, foldNodes, proveCyclic, quietly, recurNodes, translatedProof, uncovered } from "./cyclic.js";
|
|
26
26
|
import { discover } from "./discover.js";
|
|
27
|
-
import { inferLaws } from "./infer.js";
|
|
27
|
+
import { inferLaws, inferRates } from "./infer.js";
|
|
28
28
|
import { findBoundedRun } from "./bmc.js";
|
|
29
29
|
import { conjunction, proveKInduction } from "./kind.js";
|
|
30
30
|
import { cycleLemmas, findLeadsTo, findRank } from "./cycles.js";
|
|
@@ -44,12 +44,6 @@ export const termOf = (t) => {
|
|
|
44
44
|
throw new TypeError('a natural is expected here; an integer or a decimal compares from its own side (x.atMost(n))');
|
|
45
45
|
return t.e;
|
|
46
46
|
};
|
|
47
|
-
// A natural-valued term. `minus` is truncated (a ∸ b), `times` multiplies by a constant, `mul` by another term (a
|
|
48
|
-
// product: the kernel proves with it, the GPU searches don't take it).
|
|
49
|
-
//
|
|
50
|
-
// Each term and condition may also carry its value (`val`, `ok`), computed beside its expression by plain JavaScript
|
|
51
|
-
// when every value it reads is known: what `crossCheck` compares with the expression the interpreter evaluates, so a
|
|
52
|
-
// builder that writes a different expression than it means is caught on concrete runs.
|
|
53
47
|
export class Term {
|
|
54
48
|
e;
|
|
55
49
|
val;
|
|
@@ -66,6 +60,22 @@ export class Term {
|
|
|
66
60
|
// constant has no such fact in linear arithmetic (x = y·q + r is a product of two unknowns), so it isn't offered.
|
|
67
61
|
modBy(k) { divisor(k, 'modBy'); return new Term({ op: 'get', map: modName(k), key: this.e }, this.val === undefined ? undefined : this.val % k); }
|
|
68
62
|
divBy(k) { divisor(k, 'divBy'); return new Term({ op: 'get', map: divName(k), key: this.e }, this.val === undefined ? undefined : Math.floor(this.val / k)); }
|
|
63
|
+
// On a tick of k (a constant k >= 1, a price's tick, a lot size): the multiple of k below (floorTo), above (ceilTo) or
|
|
64
|
+
// nearest (roundTo, a half by the mode: 'halfEven' Python's round, 'halfUp' JavaScript's and Java's Math.round,
|
|
65
|
+
// 'halfDown', 'halfAway' Go's and Rust's round; a natural's halfAway is its halfUp), each from divBy and modBy, so the
|
|
66
|
+
// kernel knows them by the same facts; and whether it is on one (onTick).
|
|
67
|
+
floorTo(k) { return this.divBy(k).times(k); }
|
|
68
|
+
ceilTo(k) { divisor(k, 'ceilTo'); return this.plus(k - 1).divBy(k).times(k); }
|
|
69
|
+
roundTo(k, mode = 'halfEven') {
|
|
70
|
+
divisor(k, 'roundTo');
|
|
71
|
+
if (mode === 'halfUp' || mode === 'halfAway')
|
|
72
|
+
return this.times(2).plus(k).divBy(2 * k).times(k);
|
|
73
|
+
if (mode === 'halfDown')
|
|
74
|
+
return this.times(2).plus(k - 1).divBy(2 * k).times(k);
|
|
75
|
+
const q = this.divBy(k), r2 = this.modBy(k).times(2);
|
|
76
|
+
return when(r2.lt(k), q, when(lit(k).lt(r2), q.plus(1), q.plus(q.modBy(2)))).times(k);
|
|
77
|
+
}
|
|
78
|
+
onTick(k) { return this.modBy(k).eq(0); }
|
|
69
79
|
lt(b) { return new Cond({ op: '<', a: this.e, b: termOf(b) }, both(this.val, valOf(b), (x, y) => x < y)); }
|
|
70
80
|
atMost(b) { return new Cond({ op: '<=', a: this.e, b: termOf(b) }, both(this.val, valOf(b), (x, y) => x <= y)); }
|
|
71
81
|
gt(b) { return new Cond({ op: '<', a: termOf(b), b: this.e }, both(this.val, valOf(b), (x, y) => x > y)); }
|
|
@@ -275,6 +285,24 @@ export class Int {
|
|
|
275
285
|
// the same remainder, and its quotient is x div k + neg, so both are the natural division of that one natural.
|
|
276
286
|
modBy(k) { divisor(k, 'modBy'); return add(this.pos, this.neg.times(k - 1)).modBy(k); }
|
|
277
287
|
divBy(k) { divisor(k, 'divBy'); return new Int(add(this.pos, this.neg.times(k - 1)).divBy(k), this.neg); }
|
|
288
|
+
// On a tick of k, as a natural's (Term.floorTo, ceilTo, roundTo, onTick), below 0 too: halfAway rounds a half away
|
|
289
|
+
// from 0 (up above it, down below it), halfUp toward +∞, halfDown toward −∞.
|
|
290
|
+
floorTo(k) { return this.divBy(k).times(k); }
|
|
291
|
+
ceilTo(k) { return this.negate().floorTo(k).negate(); }
|
|
292
|
+
roundTo(k, mode = 'halfEven') {
|
|
293
|
+
divisor(k, 'roundTo');
|
|
294
|
+
// (a half up: ⌊(2x + k) / 2k⌋·k; a half down: ⌈(2x − k) / 2k⌉·k, that is −⌊(k − 2x) / 2k⌋·k)
|
|
295
|
+
const up = () => this.times(2).plus(k).divBy(2 * k).times(k), down = () => Int.of(k).minus(this.times(2)).divBy(2 * k).times(k).negate();
|
|
296
|
+
if (mode === 'halfUp')
|
|
297
|
+
return up();
|
|
298
|
+
if (mode === 'halfDown')
|
|
299
|
+
return down();
|
|
300
|
+
if (mode === 'halfAway')
|
|
301
|
+
return when(this.isNegative, down(), up());
|
|
302
|
+
const q = this.divBy(k), r2 = this.modBy(k).times(2);
|
|
303
|
+
return when(r2.lt(k), q, when(lit(k).lt(r2), q.plus(1), q.plus(q.modBy(2)))).times(k);
|
|
304
|
+
}
|
|
305
|
+
onTick(k) { return this.modBy(k).eq(0); }
|
|
278
306
|
// |value|, and the value where it is not negative (0 below).
|
|
279
307
|
get abs() { return this.pos.minus(this.neg).plus(this.neg.minus(this.pos)); }
|
|
280
308
|
get clamped() { return this.pos.minus(this.neg); }
|
|
@@ -323,6 +351,13 @@ export class Decimal {
|
|
|
323
351
|
min(b) { return this.with(this.int.min(this.lift(b))); }
|
|
324
352
|
max(b) { return this.with(this.int.max(this.lift(b))); }
|
|
325
353
|
get isNegative() { return this.int.isNegative; }
|
|
354
|
+
// On a tick written in the decimal's own units (0.05 at 2 places is 5 cents): Int's floorTo, ceilTo, roundTo, onTick.
|
|
355
|
+
tick(t) { const n = scaledOf(t, this.places, 'a tick'); if (n < 1)
|
|
356
|
+
throw new RangeError(`a tick is positive, not ${t}`); return n; }
|
|
357
|
+
floorTo(t) { return this.with(this.int.floorTo(this.tick(t))); }
|
|
358
|
+
ceilTo(t) { return this.with(this.int.ceilTo(this.tick(t))); }
|
|
359
|
+
roundTo(t, mode = 'halfEven') { return this.with(this.int.roundTo(this.tick(t), mode)); }
|
|
360
|
+
onTick(t) { return this.int.onTick(this.tick(t)); }
|
|
326
361
|
}
|
|
327
362
|
const asNat = (name, raw) => {
|
|
328
363
|
if (typeof raw !== 'number' || !Number.isSafeInteger(raw) || raw < 0)
|
|
@@ -924,6 +959,37 @@ export class MapRef {
|
|
|
924
959
|
// The sum over every key of the cells, or of a term of the cell (the map starts where it is 0): exact, unbounded.
|
|
925
960
|
sum(of) { return sumOver(this.name, key => (of ? of(this.at(key)) : this.at(key)), !of); }
|
|
926
961
|
}
|
|
962
|
+
// A relation over sorts (`.relations({route: ['Node', 'Node', 'Node']})`): a set of tuples, a map keyed by tuples
|
|
963
|
+
// (core/machine.ts's tup), each cell 0 or 1. The kernel compares two tuples part by part, never by arithmetic, so the
|
|
964
|
+
// laws over relations are EPR's (the protocols' laws, as Ivy and mypyvy state them). One write per relation per step:
|
|
965
|
+
// a tuple added or removed, or the relation after the step as a set comprehension (`each`, or `union`), which writes
|
|
966
|
+
// many tuples at once.
|
|
967
|
+
export class RelRef {
|
|
968
|
+
name;
|
|
969
|
+
sorts;
|
|
970
|
+
constructor(name, sorts) {
|
|
971
|
+
this.name = name;
|
|
972
|
+
this.sorts = sorts;
|
|
973
|
+
}
|
|
974
|
+
key(xs) {
|
|
975
|
+
if (xs.length !== this.sorts.length)
|
|
976
|
+
throw new Error(`${this.name} relates ${this.sorts.length} (${this.sorts.join(', ')}), not ${xs.length}`);
|
|
977
|
+
return xs.length === 1 ? termOf(xs[0]) : tuple(xs.map(termOf));
|
|
978
|
+
}
|
|
979
|
+
// The cell at a tuple (0 or 1), and whether the tuple is in the relation.
|
|
980
|
+
at(...xs) { return new Term({ op: 'get', map: this.name, key: this.key(xs) }); }
|
|
981
|
+
has(...xs) { return this.at(...xs).atLeast(1); }
|
|
982
|
+
// The relation after the step: each tuple in it where f holds (its parts as f reads them).
|
|
983
|
+
each(f) {
|
|
984
|
+
const each = `k$${++boundCounter}`, n = this.sorts.length, k = v(each);
|
|
985
|
+
const parts = n === 1 ? [k] : Array.from({ length: n }, (_, i) => new Term(part(k.e, i, n)));
|
|
986
|
+
return { map: this.name, each, value: when(f(...parts), 1, 0).e };
|
|
987
|
+
}
|
|
988
|
+
// The relation with the tuples where f holds added; with one tuple added or removed where c holds.
|
|
989
|
+
union(f) { return this.each((...xs) => this.has(...xs).or(f(...xs))); }
|
|
990
|
+
add(xs, c = yes) { const key = this.key(xs); return { map: this.name, key, value: when(c, 1, new Term({ op: 'get', map: this.name, key })).e }; }
|
|
991
|
+
remove(xs, c = yes) { const key = this.key(xs); return { map: this.name, key, value: when(c, 0, new Term({ op: 'get', map: this.name, key })).e }; }
|
|
992
|
+
}
|
|
927
993
|
export class ListRef {
|
|
928
994
|
name;
|
|
929
995
|
len;
|
|
@@ -1289,7 +1355,9 @@ function swapE(e, from, to) {
|
|
|
1289
1355
|
case 'sub':
|
|
1290
1356
|
case 'min':
|
|
1291
1357
|
case 'max':
|
|
1292
|
-
case 'prod':
|
|
1358
|
+
case 'prod':
|
|
1359
|
+
case 'tup': return { ...e, a: swapE(e.a, from, to), b: swapE(e.b, from, to) };
|
|
1360
|
+
case 'proj': return { ...e, a: swapE(e.a, from, to) };
|
|
1293
1361
|
}
|
|
1294
1362
|
}
|
|
1295
1363
|
function perStep(c) {
|
|
@@ -1321,7 +1389,9 @@ function perStepE(e) {
|
|
|
1321
1389
|
case 'sub':
|
|
1322
1390
|
case 'min':
|
|
1323
1391
|
case 'max':
|
|
1324
|
-
case 'prod':
|
|
1392
|
+
case 'prod':
|
|
1393
|
+
case 'tup': return perStepE(e.a) ?? perStepE(e.b);
|
|
1394
|
+
case 'proj': return perStepE(e.a);
|
|
1325
1395
|
}
|
|
1326
1396
|
}
|
|
1327
1397
|
class Hoister {
|
|
@@ -1355,7 +1425,9 @@ class Hoister {
|
|
|
1355
1425
|
case 'sub':
|
|
1356
1426
|
case 'min':
|
|
1357
1427
|
case 'max':
|
|
1358
|
-
case 'prod':
|
|
1428
|
+
case 'prod':
|
|
1429
|
+
case 'tup': return { ...e, a: this.term(e.a, bound), b: this.term(e.b, bound) };
|
|
1430
|
+
case 'proj': return { ...e, a: this.term(e.a, bound) };
|
|
1359
1431
|
}
|
|
1360
1432
|
}
|
|
1361
1433
|
// A quantifier as a flag (or, with per-step truths inside, a condition over flags).
|
|
@@ -1600,6 +1672,8 @@ const recurJs = (name, args, zero, next, uses) => {
|
|
|
1600
1672
|
case 'min': return `Math.min(${js(e.a)}, ${js(e.b)})`;
|
|
1601
1673
|
case 'max': return `Math.max(${js(e.a)}, ${js(e.b)})`;
|
|
1602
1674
|
case 'if': return `(${jc(e.c)} ? ${js(e.then)} : ${js(e.else)})`;
|
|
1675
|
+
case 'tup':
|
|
1676
|
+
case 'proj': throw new Error('a recurrence takes no tuple key');
|
|
1603
1677
|
case 'get': {
|
|
1604
1678
|
const i = uses.findIndex(([u]) => u === e.map), args = uses[i][1].arity > 1 ? pairedArgs(e.key, uses[i][1].arity) : undefined;
|
|
1605
1679
|
return args ? `$g${i}(${args.map(js).join(', ')})` : `${fns.get(e.map)}(${js(e.key)})`;
|
|
@@ -1679,7 +1753,9 @@ const markedE = (e) => {
|
|
|
1679
1753
|
case 'sub':
|
|
1680
1754
|
case 'min':
|
|
1681
1755
|
case 'max':
|
|
1682
|
-
case 'prod':
|
|
1756
|
+
case 'prod':
|
|
1757
|
+
case 'tup': return { ...e, a: markedE(e.a), b: markedE(e.b) };
|
|
1758
|
+
case 'proj': return { ...e, a: markedE(e.a) };
|
|
1683
1759
|
}
|
|
1684
1760
|
};
|
|
1685
1761
|
const marked = (c) => c.op === 'flag' ? c : c.op === 'all' ? { ...c, c: marked(c.c) } : c.op === 'not' ? { op: 'not', x: marked(c.x) } : c.op === 'and' || c.op === 'or' ? { op: c.op, xs: c.xs.map(marked) } : { ...c, a: markedE(c.a), b: markedE(c.b) };
|
|
@@ -1714,9 +1790,10 @@ export class Rule {
|
|
|
1714
1790
|
const m = this.machine;
|
|
1715
1791
|
if (m.history)
|
|
1716
1792
|
return undefined;
|
|
1717
|
-
const have = new Set(Object.keys(m.sums ?? {})), more = new Set(this.spec.sumsRead ?? []);
|
|
1793
|
+
const have = new Set(Object.keys(m.sums ?? {})), more = new Set(this.spec.sumsRead ?? []), arity = mapArities(m);
|
|
1718
1794
|
for (const [map, init] of Object.entries(m.maps ?? {})) {
|
|
1719
|
-
|
|
1795
|
+
// (a map keyed by tuples has no sum read at one key: a sum's term reads its maps at the key alone)
|
|
1796
|
+
if (init !== 0 || !m.step.maps?.[map] || (m.inputMaps ?? []).includes(map) || (arity[map] ?? 1) > 1)
|
|
1720
1797
|
continue;
|
|
1721
1798
|
const name = new MapRef(map, 0).sum().e.name;
|
|
1722
1799
|
if (have.has(name))
|
|
@@ -1773,6 +1850,18 @@ export class Rule {
|
|
|
1773
1850
|
}
|
|
1774
1851
|
return this.with({ maps: [...this.spec.maps, ...maps], cells: [...(this.spec.cells ?? []), ...cells] });
|
|
1775
1852
|
}
|
|
1853
|
+
// Sorts: names for sets of identities (nodes, packets, values), unbounded, read only by equality; a relation's places
|
|
1854
|
+
// and a law's keys name theirs. Relations over them, each starting empty: `.relations({route: ['Node', 'Node']})`.
|
|
1855
|
+
sorts(...names) { return this.with({ sorts: [...(this.spec.sorts ?? []), ...names] }); }
|
|
1856
|
+
relations(x) {
|
|
1857
|
+
if (typeof x === 'function')
|
|
1858
|
+
return this.with({ relations: x });
|
|
1859
|
+
for (const [n, sorts] of Object.entries(x))
|
|
1860
|
+
for (const so of sorts)
|
|
1861
|
+
if (!(this.spec.sorts ?? []).includes(so))
|
|
1862
|
+
throw new Error(`relation ${n}: ${so} is no sort of ${this.spec.name} (.sorts(...) first)`);
|
|
1863
|
+
return this.with({ maps: [...this.spec.maps, ...Object.keys(x).map(n => [n, 0])], rels: [...(this.spec.rels ?? []), ...Object.entries(x).map(([n, so]) => [n, [...so]])] });
|
|
1864
|
+
}
|
|
1776
1865
|
// Integers (Int), each starting at an integer or at one over the params; kept as two naturals, <name> and <name>$neg.
|
|
1777
1866
|
ints(init) {
|
|
1778
1867
|
return this.state(Object.fromEntries(Object.entries(init).map(([n, x]) => [n, int.from(x)])));
|
|
@@ -1816,7 +1905,8 @@ export class Rule {
|
|
|
1816
1905
|
carries(...names) { return this.with({ carried: [...(this.spec.carried ?? []), ...names] }); }
|
|
1817
1906
|
// Counts (Machine.counts): `.counts({votes: env => ({below: env.size, body: (x, k) => env.Vote.at(k).eq(x.plus(1))})})`;
|
|
1818
1907
|
// each a map no step writes, at x the number of keys k below the bound where the body holds (or its sum), read by the
|
|
1819
|
-
// flags, the assumption and the step at the request's view, and by a law at the state.
|
|
1908
|
+
// flags, the assumption and the step at the request's view, and by a law at the state. With `base`, a count over two
|
|
1909
|
+
// parameters, read at the key base·a + b (b < base): `body: (a, k, b) => …`.
|
|
1820
1910
|
counts(cs) {
|
|
1821
1911
|
return this.with({ counts: [...(this.spec.counts ?? []), ...Object.entries(cs)] });
|
|
1822
1912
|
}
|
|
@@ -1888,7 +1978,6 @@ export class Rule {
|
|
|
1888
1978
|
return this.with({ leasts: [...(this.spec.leasts ?? []), ...Object.entries(defs)] });
|
|
1889
1979
|
}
|
|
1890
1980
|
keep(f) { return this.with({ keep: f }); }
|
|
1891
|
-
relations(f) { return this.with({ relations: f }); }
|
|
1892
1981
|
// Requests come in nondecreasing `order`; a record whose order plus `after` is at most the request's is dropped
|
|
1893
1982
|
// (the kernel proves it adds nothing to any query).
|
|
1894
1983
|
prune(order, after) { return this.with({ prune: { order, after: after } }); }
|
|
@@ -2158,6 +2247,8 @@ export class Rule {
|
|
|
2158
2247
|
// Maps and lists of a kind in place of their parts' maps (and a list's length), each part's map named by `at`.
|
|
2159
2248
|
composites(out, at, len) {
|
|
2160
2249
|
const init = new Map(this.spec.maps);
|
|
2250
|
+
for (const [n, sorts] of this.spec.rels ?? [])
|
|
2251
|
+
out[n] = new RelRef(at(n), sorts);
|
|
2161
2252
|
for (const [n, k] of this.spec.cells ?? []) {
|
|
2162
2253
|
for (const part of k.vars(n))
|
|
2163
2254
|
delete out[part];
|
|
@@ -2286,8 +2377,8 @@ export class Rule {
|
|
|
2286
2377
|
...(s.maps.length ? { maps: Object.fromEntries(s.maps) } : {}),
|
|
2287
2378
|
...(s.carried?.length ? { inputMaps: s.carried } : {}),
|
|
2288
2379
|
...(s.counts?.length ? { counts: Object.fromEntries(s.counts.map(([n, f]) => {
|
|
2289
|
-
const c = f(env), j = `${n}$x`, key = `${n}$k`, b = c.body(v(j), v(key));
|
|
2290
|
-
return [n, { j, key, below: termOf(c.below), body: b instanceof Cond ? { op: 'if', c: b.c, then: { op: 'const', value: 1 }, else: { op: 'const', value: 0 } } : termOf(b) }];
|
|
2380
|
+
const c = f(env), j = `${n}$x`, key = `${n}$k`, lo = `${n}$y`, b = c.body(v(j), v(key), v(lo));
|
|
2381
|
+
return [n, { j, key, below: termOf(c.below), body: b instanceof Cond ? { op: 'if', c: b.c, then: { op: 'const', value: 1 }, else: { op: 'const', value: 0 } } : termOf(b), ...(c.base ? { lo, base: c.base } : {}) }];
|
|
2291
2382
|
})) } : {}),
|
|
2292
2383
|
step: { ...(Object.keys(flags).length ? { flags } : {}), ...(Object.keys(state).length ? { state } : {}), ...(Object.keys(maps).length ? { maps } : {}) },
|
|
2293
2384
|
...(pre.length ? { pre: pre.length === 1 ? pre[0] : { op: 'and', xs: pre } } : {}),
|
|
@@ -2295,6 +2386,7 @@ export class Rule {
|
|
|
2295
2386
|
...(Object.keys(fns).length ? { fns } : {}),
|
|
2296
2387
|
...(facts.length ? { facts } : {}),
|
|
2297
2388
|
...(Object.keys(leasts).length ? { leasts } : {}),
|
|
2389
|
+
...(s.rels?.length ? { relations: Object.fromEntries(s.rels) } : {}),
|
|
2298
2390
|
};
|
|
2299
2391
|
if (s.record) {
|
|
2300
2392
|
const kinds = new Map(s.inputs);
|
|
@@ -2841,7 +2933,7 @@ function refuteByRuns(r, laws, stepLaws = [], budget = refuteBudget) {
|
|
|
2841
2933
|
&& facts.filter(readsParams).every(f => holds(f, params));
|
|
2842
2934
|
const cap = watchWork.n + perTrial * budget.trials;
|
|
2843
2935
|
try {
|
|
2844
|
-
const late = () => budget.until !== undefined && performance.now() > budget.until;
|
|
2936
|
+
const late = () => pastWall() || budget.until !== undefined && performance.now() > budget.until;
|
|
2845
2937
|
for (let trial = 0; trial < budget.trials && watchWork.n <= cap && !late(); trial++) {
|
|
2846
2938
|
// (`mixed`: the trials in turn at bounds 1 to 4, every fourth near)
|
|
2847
2939
|
const bound = budget.mixed ? 1 + trial % 4 : budget.bound, guided = guides, near = budget.mixed && trial % 4 ? undefined : budget.near;
|
|
@@ -2866,6 +2958,10 @@ function refuteByRuns(r, laws, stepLaws = [], budget = refuteBudget) {
|
|
|
2866
2958
|
for (const x of m.inputs)
|
|
2867
2959
|
if (near[x] !== undefined && typeof input[x] === 'number' && rand(2))
|
|
2868
2960
|
input[x] = near[x];
|
|
2961
|
+
if (near && budget.pool?.length)
|
|
2962
|
+
for (const x of m.inputs)
|
|
2963
|
+
if (typeof input[x] === 'number' && !rand(4))
|
|
2964
|
+
input[x] = budget.pool[rand(budget.pool.length)];
|
|
2869
2965
|
for (const g of guided) {
|
|
2870
2966
|
const set = g({ ...params, ...st.scalars }, st, rand, bound);
|
|
2871
2967
|
if (set && typeof input[set[0]] === 'number')
|
|
@@ -2889,7 +2985,7 @@ function refuteByRuns(r, laws, stepLaws = [], budget = refuteBudget) {
|
|
|
2889
2985
|
// variable of a kind is drawn as its kind draws (33 yes/nos drawn from 0..7 almost never make a lawful state).
|
|
2890
2986
|
const steps1 = stepLaws.filter(sl => (sl.steps ?? 1) === 1), given = withKinds(r, laws);
|
|
2891
2987
|
const wide = Object.keys(m.state).length + m.inputs.length >= 32, lawsHere = wide ? given : [], kinded = wide ? r.kindsOfState() : [];
|
|
2892
|
-
for (let trial = 0; budget.states !== false && !m.history && (steps1.length || lawsHere.length) && trial < budget.trials * 4 && watchWork.n <= cap; trial++) {
|
|
2988
|
+
for (let trial = 0; budget.states !== false && !m.history && (steps1.length || lawsHere.length) && trial < budget.trials * 4 && watchWork.n <= cap && !late(); trial++) {
|
|
2893
2989
|
const params = r.randomParams(rand, bound);
|
|
2894
2990
|
if (m.pre && !evalC(m.pre, params, { scalars: {}, maps: {} }, m) || !factsHold(params))
|
|
2895
2991
|
continue;
|
|
@@ -2944,11 +3040,21 @@ export function proveRule(r, laws, opts = {}) {
|
|
|
2944
3040
|
// step goal of no_lost_keys' laws as given took 57 s of 60 choosing a witness term)
|
|
2945
3041
|
// (an eighth where such a law binds several names: proveSome gives a ghost map to one bound name alone)
|
|
2946
3042
|
const share = laws.some(l => (l.some?.length ?? 0) > 1 && l.forall) ? 8 : laws.some(l => l.some?.length && l.forall) ? 2 : 1;
|
|
2947
|
-
|
|
3043
|
+
// Quantified witness preflight needs a counted allocation: a slow attempt must
|
|
3044
|
+
// not seed different memoized decisions. Other laws keep their existing budget;
|
|
3045
|
+
// inference's query rate is not calibrated for their whole-set preflight.
|
|
3046
|
+
const out = ms === undefined ? given() : withWall(performance.now() + ms / share, () => share === 1 ? given() : counted(inferRates.work, () => withDeadline(clock() + ms / share, given)));
|
|
2948
3047
|
const v = { ...verdictOf(out), ...(out.witnesses && out.proven ? { witnesses: out.witnesses } : {}) };
|
|
2949
3048
|
return searched(r, laws, [], v, opts);
|
|
2950
3049
|
}
|
|
2951
3050
|
export function proveSteps(r, laws, stepLaws, opts = {}) {
|
|
3051
|
+
const ms = typeof opts.search === 'object' ? opts.search.ms : undefined;
|
|
3052
|
+
// From any state, the caller's budget includes preflight runs and all initial
|
|
3053
|
+
// goals, not a fresh budget per goal. Keep reachable-state search unchanged.
|
|
3054
|
+
return opts.from !== 'any' || ms === undefined ? proveStepsIn(r, laws, stepLaws, opts)
|
|
3055
|
+
: withWall(performance.now() + ms, () => proveStepsIn(r, laws, stepLaws, opts));
|
|
3056
|
+
}
|
|
3057
|
+
function proveStepsIn(r, laws, stepLaws, opts) {
|
|
2952
3058
|
r = r.withSumsOf(laws, stepLaws);
|
|
2953
3059
|
noting(r, laws, stepLaws);
|
|
2954
3060
|
const refuted = refuteByRuns(r, laws, stepLaws);
|
|
@@ -3001,6 +3107,15 @@ function searched(r, laws, stepLaws, v, opts) {
|
|
|
3001
3107
|
if (deeper?.run)
|
|
3002
3108
|
return refutedVerdict(deeper);
|
|
3003
3109
|
}
|
|
3110
|
+
// (then, for a law of steps or one that divides by an hour or more (a clock's days), near it with its other values,
|
|
3111
|
+
// where it holds some past the small ones: a time the state keeps is one an earlier request gave)
|
|
3112
|
+
const m0 = r.machine, clocked = stepLaws.length > 0 || [...divisorsIn(laws.map(l => l.holds))].some(k => k >= 3600);
|
|
3113
|
+
const pool = clocked ? [...new Set(Object.entries(v.witness ?? {}).filter(([k, x]) => !m0.inputs.includes(k) && !m0.params.includes(k) && x > deeperBudget.bound).map(([, x]) => x))] : [];
|
|
3114
|
+
if (pool.length) {
|
|
3115
|
+
const deeper = refuteByRuns(r, laws, stepLaws, { ...deeperBudget, trials: 300, seed: 43, states: false, mixed: true, near: v.witness, pool, until: runsUntil });
|
|
3116
|
+
if (deeper?.run)
|
|
3117
|
+
return refutedVerdict(deeper);
|
|
3118
|
+
}
|
|
3004
3119
|
const until = clock() + (typeof opts.search === 'object' && opts.search.ms !== undefined ? opts.search.ms : 10_000);
|
|
3005
3120
|
// (the search's end on the wall too: kernel/prove.ts's withWall, past which every goal is unknown)
|
|
3006
3121
|
return withWall(performance.now() + Math.max(0, until - clock()), () => searchedUntil(r, laws, stepLaws, v, opts, until));
|
|
@@ -3126,16 +3241,42 @@ function searchedUntil(r, laws, stepLaws, v, opts, until) {
|
|
|
3126
3241
|
// rest assumed, the failing dropped, until none fails; then the set through `back`, the ∃ verdict as the search's)
|
|
3127
3242
|
if (sk?.cands.length) {
|
|
3128
3243
|
const tm = target.machine, hUntil = clock() + (until - clock()) / 2;
|
|
3244
|
+
const native = Object.values(sk.maps).some(g => !('each' in g.write) && g.write.key.op === 'tup');
|
|
3129
3245
|
let keep = [...sk.cands];
|
|
3130
3246
|
for (let round = 0; round < 8 && clock() < hUntil; round++) {
|
|
3131
3247
|
const ls = withKinds(target, [...fresh.map(f => f.law), ...sought, ...keep]);
|
|
3132
|
-
const
|
|
3248
|
+
const helpers = withKinds(target, [...fresh.map(f => f.law), ...keep]);
|
|
3249
|
+
const drop = keep.filter(c => {
|
|
3250
|
+
if (clock() >= hUntil)
|
|
3251
|
+
return true;
|
|
3252
|
+
const budget = Math.min(3_000, hUntil - clock());
|
|
3253
|
+
// Keep support selection deterministic on slow VMs, including native
|
|
3254
|
+
// tuple supports. Nested counted searches retain their existing clock.
|
|
3255
|
+
return counted(fullRate, () => {
|
|
3256
|
+
const end = clock() + budget;
|
|
3257
|
+
if (!native)
|
|
3258
|
+
return !withDeadline(end, () => proveLaw(tm, ls, ls.indexOf(c))).proven;
|
|
3259
|
+
// Try local provenance before instantiating every witness. All three
|
|
3260
|
+
// attempts share the same existing support-query allocation.
|
|
3261
|
+
if (round === 0 && withDeadline(Math.min(end, clock() + 500), () => proveSetFast(tm, [c])).proven)
|
|
3262
|
+
return false;
|
|
3263
|
+
const local = witnessSupport(tm, helpers, c, Object.keys(sk.maps));
|
|
3264
|
+
if (withDeadline(Math.min(end, clock() + 1_000), () => proveLaw(tm, local, local.indexOf(c))).proven)
|
|
3265
|
+
return false;
|
|
3266
|
+
return !withDeadline(end, () => proveLaw(tm, ls, ls.indexOf(c))).proven;
|
|
3267
|
+
}, true);
|
|
3268
|
+
});
|
|
3133
3269
|
if (clock() >= hUntil)
|
|
3134
3270
|
break;
|
|
3135
3271
|
if (knobs.GDP_EXISTS_LOG === '1')
|
|
3136
3272
|
console.error(`exists: ghost supports round ${round}: ${keep.length} kept, ${drop.length} dropped: ${keep.map(c => c.name).join('; ').slice(0, 1500)}`);
|
|
3137
3273
|
if (!drop.length) {
|
|
3138
|
-
const
|
|
3274
|
+
for (const l of native ? sought : []) {
|
|
3275
|
+
const local = witnessSupport(tm, ls, l);
|
|
3276
|
+
if (local.length < ls.length)
|
|
3277
|
+
withDeadline(Math.min(hUntil, clock() + 3_000), () => proveLaw(tm, local, local.indexOf(l)));
|
|
3278
|
+
}
|
|
3279
|
+
const b = back(target, keep.map(law => ({ law })), native);
|
|
3139
3280
|
if (knobs.GDP_EXISTS_LOG === '1')
|
|
3140
3281
|
console.error(`exists: ghost supports set: ${b.proven ? 'proven' : `${b.goal} ${b.stuck} ${b.why ?? ''}`}`);
|
|
3141
3282
|
if (b.proven && b.checked) {
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { Bool, Choice, Cond, Int, Kind, Maybe, Str, Term, type Init, type Raw, type TermLike, Variant } from './dsl.js';
|
|
2
|
+
import type { Rand } from './machine.js';
|
|
3
|
+
export type SchemaLike = {
|
|
4
|
+
readonly ast: unknown;
|
|
5
|
+
};
|
|
6
|
+
type Shape<S> = S extends {
|
|
7
|
+
readonly Encoded: infer A;
|
|
8
|
+
} ? A : unknown;
|
|
9
|
+
type Projected<S> = unknown extends Shape<S> ? unknown : S extends {
|
|
10
|
+
readonly fields: infer F;
|
|
11
|
+
} ? Pick<Shape<S>, Extract<keyof F, keyof Shape<S>>> : Shape<S>;
|
|
12
|
+
type IsUnion<A, Whole = A> = A extends Whole ? [Whole] extends [A] ? false : true : never;
|
|
13
|
+
type CasesOf<A extends {
|
|
14
|
+
readonly _tag: string;
|
|
15
|
+
}> = {
|
|
16
|
+
[K in A['_tag']]: {
|
|
17
|
+
[F in Exclude<Extract<keyof Extract<A, {
|
|
18
|
+
readonly _tag: K;
|
|
19
|
+
}>, string>, '_tag'>]-?: Kind<SchemaValue<Extract<A, {
|
|
20
|
+
readonly _tag: K;
|
|
21
|
+
}>[F]>>;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
/** Symbolic values, never Schema.Type cast as a Kind: numbers are signed Int, booleans Bool, strings Str/Choice. */
|
|
25
|
+
export type SchemaValue<A> = 0 extends 1 & A ? unknown : unknown extends A ? unknown : undefined extends A ? Maybe<SchemaValue<NonNullable<A>>> : null extends A ? Maybe<SchemaValue<NonNullable<A>>> : [A] extends [number] ? Int : [A] extends [boolean] ? Bool : [A] extends [string] ? string extends A ? Str : Choice<A & string> : [A] extends [{
|
|
26
|
+
readonly _tag: string;
|
|
27
|
+
}] ? IsUnion<A> extends true ? Variant<CasesOf<A & {
|
|
28
|
+
readonly _tag: string;
|
|
29
|
+
}>, '_tag'> : {
|
|
30
|
+
[K in keyof A]-?: SchemaValue<A[K]>;
|
|
31
|
+
} : [A] extends [readonly unknown[]] ? never : [A] extends [object] ? {
|
|
32
|
+
[K in keyof A]-?: SchemaValue<A[K]>;
|
|
33
|
+
} : never;
|
|
34
|
+
export type SchemaSymbolic<S> = SchemaValue<Projected<S>>;
|
|
35
|
+
export type SchemaOptions = {
|
|
36
|
+
readonly mode?: 'exact' | 'approximate';
|
|
37
|
+
};
|
|
38
|
+
export type SchemaDiagnostic = {
|
|
39
|
+
readonly path: string;
|
|
40
|
+
readonly code: string;
|
|
41
|
+
readonly message: string;
|
|
42
|
+
readonly effect: 'overapproximation' | 'normalization' | 'projection';
|
|
43
|
+
};
|
|
44
|
+
export type SchemaConstraint = {
|
|
45
|
+
readonly path: string;
|
|
46
|
+
readonly kind: 'integer' | 'literals';
|
|
47
|
+
readonly minimum?: number;
|
|
48
|
+
readonly maximum?: number;
|
|
49
|
+
readonly values?: readonly (string | number | boolean)[];
|
|
50
|
+
};
|
|
51
|
+
export type SchemaReport = {
|
|
52
|
+
readonly mode: 'exact' | 'approximate';
|
|
53
|
+
/** Equality is on safe integers (including -0 as 0), booleans, strings, null, and closed structural records. */
|
|
54
|
+
readonly domain: 'decoded-closed-values';
|
|
55
|
+
readonly runtime: 'exact-on-decoded-closed-values' | 'overapproximation' | 'normalized' | 'projection';
|
|
56
|
+
readonly model: 'exact' | 'overapproximation';
|
|
57
|
+
readonly counterexamples: 'validate-with-schema';
|
|
58
|
+
readonly losses: readonly SchemaDiagnostic[];
|
|
59
|
+
readonly constraints: readonly SchemaConstraint[];
|
|
60
|
+
};
|
|
61
|
+
export declare class SchemaError extends Error {
|
|
62
|
+
readonly path: string;
|
|
63
|
+
readonly code: string;
|
|
64
|
+
constructor(path: string, code: string, message: string);
|
|
65
|
+
}
|
|
66
|
+
/** A regular Kind, with a report that distinguishes runtime conversion from symbolic model precision. */
|
|
67
|
+
export declare class SchemaKind<V> extends Kind<V> {
|
|
68
|
+
private readonly inner;
|
|
69
|
+
readonly report: SchemaReport;
|
|
70
|
+
constructor(inner: Kind<V>, report: SchemaReport);
|
|
71
|
+
vars(name: string): string[];
|
|
72
|
+
assume(name: string): Cond[];
|
|
73
|
+
build(name: string, get: (part: string) => Term): V;
|
|
74
|
+
invariant(value: V): Cond[];
|
|
75
|
+
encode(name: string, raw: Raw): Record<string, number>;
|
|
76
|
+
decode(name: string, vars: Record<string, number>): Raw;
|
|
77
|
+
store(name: string, value: unknown): Record<string, TermLike>;
|
|
78
|
+
empty(): Raw;
|
|
79
|
+
random(name: string, rand: Rand, bound: number): Record<string, number>;
|
|
80
|
+
sample(rand: Rand, bound: number): Raw;
|
|
81
|
+
from(init: Raw | ((params: never) => unknown)): Init<V>;
|
|
82
|
+
/** Build a symbolic value through the same checked store path used by state initialization. */
|
|
83
|
+
make(value: unknown): V;
|
|
84
|
+
}
|
|
85
|
+
/** Exact by default. Approximation is opt-in and every discarded constraint/normalization has a path in report.losses. */
|
|
86
|
+
export declare function fromSchema<const S extends SchemaLike>(schema: S, options?: {
|
|
87
|
+
readonly mode?: 'exact';
|
|
88
|
+
}): SchemaKind<SchemaSymbolic<S>>;
|
|
89
|
+
export declare function fromSchema(schema: SchemaLike, options: SchemaOptions | undefined): SchemaKind<unknown>;
|
|
90
|
+
export {};
|