gdp-wired 0.25.0 → 0.26.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 +14 -9
- package/dist/core/accel.d.ts +1 -1
- package/dist/core/accel.js +336 -101
- package/dist/core/bmc.d.ts +8 -0
- package/dist/core/bmc.js +263 -97
- package/dist/core/cycles.d.ts +2 -3
- package/dist/core/cycles.js +35 -131
- package/dist/core/cyclic.d.ts +4 -1
- package/dist/core/cyclic.js +86 -64
- package/dist/core/data.d.ts +1 -1
- package/dist/core/data.js +89 -118
- package/dist/core/discover.js +2 -3
- package/dist/core/dsl.d.ts +3 -2
- package/dist/core/dsl.js +21 -18
- package/dist/core/exists.js +7 -14
- package/dist/core/extract-check.d.ts +3 -2
- package/dist/core/extract-check.js +137 -50
- package/dist/core/extract-print.d.ts +1 -0
- package/dist/core/extract-print.js +26 -3
- package/dist/core/extract-read.d.ts +85 -4
- package/dist/core/extract-read.js +295 -34
- package/dist/core/extract-shapes.d.ts +14 -0
- package/dist/core/extract-shapes.js +608 -78
- package/dist/core/extract-xstate.d.ts +5 -1
- package/dist/core/extract-xstate.js +618 -184
- package/dist/core/extract.js +73 -27
- package/dist/core/infer.js +72 -18
- package/dist/core/machine.d.ts +6 -0
- package/dist/core/machine.js +35 -3
- package/dist/core/nonterm.d.ts +5 -1
- package/dist/core/nonterm.js +109 -41
- package/dist/core/q.d.ts +1 -0
- package/dist/core/q.js +2 -2
- package/dist/core/relation.d.ts +1 -0
- package/dist/core/relation.js +20 -36
- package/dist/core/solver.d.ts +12 -0
- package/dist/core/solver.js +76 -30
- package/dist/core/transfer.js +22 -13
- package/dist/core/translate.d.ts +1 -1
- package/dist/core/translate.js +22 -49
- package/dist/core/xstate.d.ts +6 -1
- package/dist/core/xstate.js +336 -55
- package/dist/kernel/cdcl.js +1 -2
- package/dist/kernel/prove.d.ts +6 -1
- package/dist/kernel/prove.js +96 -82
- package/dist/kernel/reader.d.ts +1 -0
- package/dist/kernel/reader.js +4 -3
- package/dist/kernel/simplex.d.ts +1 -1
- package/dist/kernel/simplex.js +13 -1
- package/package.json +3 -2
package/dist/core/cyclic.js
CHANGED
|
@@ -1,18 +1,69 @@
|
|
|
1
|
+
// Cyclic proofs found. Untrusted: an instance states the nodes and their uses, kernel/prove.ts grows a tree for every
|
|
2
|
+
// goal, and kernel/cyclic.ts checks the whole. The instances are today's induction rules said as one rule, each a
|
|
3
|
+
// graph of sequents whose back-edges lower a rank:
|
|
4
|
+
// - a recurrence's lemmas (`recur`): f a map read at x, its two equations facts, each lemma a node at rank (0, x)
|
|
5
|
+
// using every lemma at x − 1; each law between two points a node at rank (1, y), using itself at y − 1 and the lemmas
|
|
6
|
+
// (whose rank, (0, ·), is below)
|
|
7
|
+
// - a machine's laws: the run as maps over time, each state variable s read at time t as s@[t], each input as i@[t],
|
|
8
|
+
// each map M at key k as M@[t + k·Ω] (Ω above the run's length T); the start and every step are facts (each at the
|
|
9
|
+
// time it gives, a step only up to T); each
|
|
10
|
+
// law is a node at rank (t), using every law at t − 1 (a law about every key at the keys the machine's proof names)
|
|
11
|
+
// - a fold over trees: the tree as a table of nodes, a node's children numbered below it (any tree can be), each law a
|
|
12
|
+
// node at rank (n) using every law at both children
|
|
13
|
+
// Beyond those, a node may use any node at any terms (nested, mutual, two ranks in order): `proveCyclic` takes the nodes
|
|
14
|
+
// as they are given (examples/cyclic.ts).
|
|
15
|
+
//
|
|
16
|
+
// Translated (machineNodes, recurNodes, foldNodes given the usual proof; core/translate.ts): what the usual route proved,
|
|
17
|
+
// made a cyclic proof from its trees rather than searched. The facts are the instance's as above (Bridge.lean's
|
|
18
|
+
// cycCanon, recFacts, the fold's table); the nodes, in this order:
|
|
19
|
+
// - machines, laws l_0..l_{L-1}: node i < L the law at t, rank (t, 1), hyps the precondition, t <= T, T < Ω (cycMOk's
|
|
20
|
+
// node), facts the start and the step at t and each map's at the keys it reads, uses node L + i (guard 1 <= t) and
|
|
21
|
+
// node 2L + i (guard t <= 0), its tree made (`carry`). Node L + i the law after a step from t − 1, rank (t, 0):
|
|
22
|
+
// hyps the precondition, 1 <= t, t <= T, T < Ω and the request's assumption at t − 1, facts the functions' at the
|
|
23
|
+
// keys the usual proof names, uses the laws at t − 1 at those keys (guard 0 <= 0), its conclusion the law at the
|
|
24
|
+
// state the step gives over the cells at t − 1 (a write's reads at its key read at the key read), its tree the usual
|
|
25
|
+
// step tree. Node 2L + i the law at the start, rank (0, 0): the usual start goal, k# as k, its tree the usual one.
|
|
26
|
+
// Only the facts and uses the usual tree reads are kept.
|
|
27
|
+
// - then each step law s_j (proveStepLaws): node 3L + 2j after its k steps, rank (t, 0), the usual step law's goal over
|
|
28
|
+
// the cells at t − k..t − 1 (hyps the precondition, k <= t, t <= T, T < Ω, the assumption and `when` at each step's
|
|
29
|
+
// time, the state's equations after each step but the last that the usual tree reads; uses the laws at each of those
|
|
30
|
+
// times; the times t − n at once, over a map one step back at a time, t − 1 − 1), and node
|
|
31
|
+
// 3L + 2j + 1 the step law itself, rank (t, 2): hyps the precondition, k <= t, t <= T, T < Ω and `when` at each
|
|
32
|
+
// step's time, its conclusion `then` with plain names at t − k, primed ones at t and a later step's request (i@j) at
|
|
33
|
+
// its time, facts the step at t − k + 1..t (and the maps' at t), its one use node 3L + 2j, its tree made (`carry`).
|
|
34
|
+
// Over several steps a later step reads a map as the usual goal's term over the cells at t − k (its laws there a
|
|
35
|
+
// use only where they read no map), the step law's node reading each cell back to t − k through the maps' steps (its
|
|
36
|
+
// tree carried on its goal sliced first).
|
|
37
|
+
// - a machine with flags that hold at every key is read as its sk machine, the usual goals' closing hypotheses the
|
|
38
|
+
// closing facts at their time (`qflagFacts`); a request allowed (`allowTranslated`) two nodes after the laws'.
|
|
39
|
+
// - a loop's halting (`loopNodes`): its machine's laws and its liveness's step laws as above (kernel.ts's
|
|
40
|
+
// loopObligations); a liveness's certificate is its step laws' proof and its helpful requests' allowed nodes, the
|
|
41
|
+
// claim about runs that never end read from them as from the usual trees (kernel/prove.ts's proveLiveness).
|
|
42
|
+
// - recurrences, lemmas n, laws between b: nodes 0..n+b−1 as above (their trees made), using also node 2n + 2b + i
|
|
43
|
+
// (the lemma at the start, guard x <= 0; a law between at y = x, guard y <= x); nodes n + b.. the lemma (law between)
|
|
44
|
+
// after a step, the usual step goal with x as x − 1, fx as f(x − 1) (y, fy likewise, fx as f(x)); then the start
|
|
45
|
+
// nodes, rank (0, 0, 0) and (1, 0, 0).
|
|
46
|
+
// - folds, laws L: node i the law at a node n, rank (n, 1), using node L + i (guard Kind[n] = 1) and node 2L + i (guard
|
|
47
|
+
// Kind[n] = 0); node L + i from its children, rank (n, 0), hyps n < N, Kind[n] = 1, both children below n and the
|
|
48
|
+
// assumption at n, uses the laws at both children; node 2L + i at a leaf, rank (n, 0).
|
|
49
|
+
//
|
|
50
|
+
// The switches (the environment; a user needs none): GDP_CERT=usual, the usual trees as every certificate (the fallback
|
|
51
|
+
// and the baseline); GDP_CERT_SEARCH=1, a search where a translation fails, within GDP_CERT_MS (10,000 ms); GDP_CYCLIC=1,
|
|
52
|
+
// the cross-check (every proof made again as a cyclic proof, a differing verdict throwing; GDP_CYCLIC_LAX=1 only logs it;
|
|
53
|
+
// GDP_CYCLIC_ROUTE=translate|search|both, its route). Diagnostics: GDP_CYCLIC_LOG=1, the `cyclic: {...}` lines
|
|
54
|
+
// research/cyclic/ratios.ts reads; GDP_CYCLIC_PROF=1, where each proof's time went; GDP_CYCLIC_SLICE=0, every goal grown
|
|
55
|
+
// whole; GDP_CYCLIC_DEBUG=1, each goal grown.
|
|
56
|
+
import { atMost as le, get, n as k, plus as add, same as eq, walk, x as v } from "./machine.js";
|
|
1
57
|
import { assumed, boundOf, foldObligations, livenessParts, loopLiveness, loopMachine, loopObligations, obligations, stepLawObligations, substC, substE, witnessesOf } from "../kernel/kernel.js";
|
|
2
58
|
import { baseHyps, descentGoal, instHyps, mainGoal, premiseGoal, refuseCyclic } from "../kernel/cyclic.js";
|
|
3
59
|
import { cellProducts, clock, grow, profile, usedHyps, widen, withDeadline, withinDeadline } from "../kernel/prove.js";
|
|
4
60
|
import { carry, decide, readsFirst, renamer, translate, tstats } from "./translate.js";
|
|
5
|
-
|
|
6
|
-
const k = (value) => ({ op: 'const', value });
|
|
7
|
-
const add = (a, b) => ({ op: 'add', a, b });
|
|
61
|
+
import { env } from "./solver.js";
|
|
8
62
|
const less1 = (a) => ({ op: 'sub', a, b: k(1) });
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const eq = (a, b) => ({ op: '=', a, b });
|
|
12
|
-
const and = (...xs) => (xs.length === 1 ? xs[0] : { op: 'and', xs });
|
|
13
|
-
const implies = (a, b) => ({ op: 'or', xs: [{ op: 'not', x: a }, b] });
|
|
63
|
+
export const and = (...xs) => (xs.length === 1 ? xs[0] : { op: 'and', xs });
|
|
64
|
+
export const implies = (a, b) => ({ op: 'or', xs: [{ op: 'not', x: a }, b] });
|
|
14
65
|
const yes = le(k(0), k(0));
|
|
15
|
-
const vars = (s) => ({ vars: s, maps: {}, flags: {} });
|
|
66
|
+
export const vars = (s) => ({ vars: s, maps: {}, flags: {} });
|
|
16
67
|
// A map's cell at time u and key k, in the run's one map over both: u + k·Ω, where Ω is above the run's length T, so
|
|
17
68
|
// that no two (u, k) with u <= T meet (u < Ω). Linear in u, and two keys' cells differ by (k − k')·Ω.
|
|
18
69
|
const omega = v('O$');
|
|
@@ -21,7 +72,7 @@ export const pair = (u, key) => add(u, { op: 'prod', a: key, b: omega });
|
|
|
21
72
|
// GDP_CYCLIC_PROF=1: each comparison's line also has where the cyclic proof's time went, by goal kind (main, premise,
|
|
22
73
|
// descent, the kernel's check): ms, goals, hypotheses, solver and row ms.
|
|
23
74
|
export const cyclicProf = {};
|
|
24
|
-
const profOn = () => env
|
|
75
|
+
const profOn = () => env.GDP_CYCLIC_PROF === '1';
|
|
25
76
|
export function timedKind(kind, hyps, f) {
|
|
26
77
|
if (!profOn())
|
|
27
78
|
return f();
|
|
@@ -50,15 +101,13 @@ export function timedKind(kind, hyps, f) {
|
|
|
50
101
|
// A cell by what it may be equal to: a run's map read at time u and any key (two keys the prover may prove equal) is
|
|
51
102
|
// one, by its map and u; a function's read at any key is one, by the function; a run's other reads are themselves.
|
|
52
103
|
const cellsIn = (t, out = new Set()) => {
|
|
53
|
-
|
|
54
|
-
const
|
|
104
|
+
walk(t, o => {
|
|
105
|
+
const map = o.map;
|
|
55
106
|
if (o.op === 'get') {
|
|
56
107
|
const key = o.key;
|
|
57
108
|
out.add(key.op === 'add' && key.b.op === 'prod' && JSON.stringify(key.b.b) === JSON.stringify(omega) ? `${map}@${JSON.stringify(key.a)}` : map.endsWith('@') ? JSON.stringify(o) : map);
|
|
58
109
|
}
|
|
59
|
-
|
|
60
|
-
cellsIn(val, out);
|
|
61
|
-
}
|
|
110
|
+
});
|
|
62
111
|
return out;
|
|
63
112
|
};
|
|
64
113
|
function partsOf(c, guards = [], out = []) {
|
|
@@ -149,7 +198,7 @@ finally {
|
|
|
149
198
|
cellProducts.on = was;
|
|
150
199
|
} };
|
|
151
200
|
let sliceNow = false;
|
|
152
|
-
const sliceOn = () => sliceNow && env
|
|
201
|
+
const sliceOn = () => sliceNow && env.GDP_CYCLIC_SLICE !== '0';
|
|
153
202
|
const searched = new Map();
|
|
154
203
|
export const translatedCache = new Map();
|
|
155
204
|
// `order`: the order the nodes are grown in, by default those with a weak variant first (a variant that uses a weak node
|
|
@@ -168,7 +217,7 @@ export function proveCyclic(facts, specs, order0, grownBefore = searched, st = t
|
|
|
168
217
|
if (part) {
|
|
169
218
|
// (within two seconds: a weaker goal no easier than the whole is left to the whole)
|
|
170
219
|
const t0 = performance.now(), p = timedKind(`${kind} sliced`, part.hyps.length, () => grow(part, new Map(), { nodes: 0, until: clock() + Math.min(2_000, later ?? 2_000) }));
|
|
171
|
-
if (env
|
|
220
|
+
if (env.GDP_CYCLIC_DEBUG)
|
|
172
221
|
console.error(`[cyc] ${g.name}: ${part.hyps.length} hyps${part !== sliced ? ' (opaque)' : ''}, ${'tree' in p ? `${p.nodes} nodes` : p.stuck}, ${Math.round(performance.now() - t0)} ms`);
|
|
173
222
|
if ('tree' in p)
|
|
174
223
|
return { tree: p.tree, from: part, used: () => new Set([...usedHyps(part, p.tree)].flatMap(i => part.from[i])) };
|
|
@@ -229,7 +278,7 @@ export function proveCyclic(facts, specs, order0, grownBefore = searched, st = t
|
|
|
229
278
|
if (part) {
|
|
230
279
|
const nf = part.from.filter(f => f[0] >= h0 && f[0] < base).length, nu = part.from.filter(f => f[0] >= base && f[0] < last).length;
|
|
231
280
|
const tree = carry(part, nf, nu, st, clock() + 2_000), wide = tree && timedKind('widen', 0, () => widen(part, g, tree));
|
|
232
|
-
if (env
|
|
281
|
+
if (env.GDP_CYCLIC_DEBUG)
|
|
233
282
|
console.error(`[carry] ${n1.name}: ${part.hyps.length} of ${g.hyps.length} hyps, ${tree ? (wide ? 'carried, widened' : 'carried, not widened') : 'not carried'}`);
|
|
234
283
|
if (wide)
|
|
235
284
|
return wide;
|
|
@@ -266,7 +315,7 @@ export function proveCyclic(facts, specs, order0, grownBefore = searched, st = t
|
|
|
266
315
|
if (n2 && (n2.insts.length < n1.insts.length || n2.uses.length < n1.uses.length)) {
|
|
267
316
|
const g2 = mainGoal(facts, shapes, n2), part = hide(g2);
|
|
268
317
|
const p = timedKind('main selected', g2.hyps.length, () => withProducts(() => grow(part, new Map(), { nodes: 0, until: clock() + Math.min(2_000, later ?? 2_000) })));
|
|
269
|
-
if (env
|
|
318
|
+
if (env.GDP_CYCLIC_DEBUG)
|
|
270
319
|
console.error(`[cyc] ${g.name}: ${g2.hyps.length} of ${g.hyps.length} hyps selected, ${'tree' in p ? `${p.nodes} nodes` : p.stuck}`);
|
|
271
320
|
const t = 'tree' in p ? own({ tree: p.tree, from: part }, g2) : undefined;
|
|
272
321
|
if (t && 'tree' in p) {
|
|
@@ -306,7 +355,7 @@ export function proveCyclic(facts, specs, order0, grownBefore = searched, st = t
|
|
|
306
355
|
const premises = [];
|
|
307
356
|
for (const [i, c] of m.hyps.entries()) {
|
|
308
357
|
const t = whole(premiseGoal(facts, n, u, c, i), n.name, mine2);
|
|
309
|
-
if ('proven' in t && env
|
|
358
|
+
if ('proven' in t && env.GDP_CYCLIC_DEBUG)
|
|
310
359
|
console.error(`[premise] ${n.name}: use of node ${u.node}, its hypothesis ${i} (${t.stuck}): ${JSON.stringify(substC(c, vars(u.subst)))}`);
|
|
311
360
|
if ('proven' in t)
|
|
312
361
|
return t;
|
|
@@ -533,13 +582,8 @@ function withReads(facts, specs, nodes = specs) {
|
|
|
533
582
|
});
|
|
534
583
|
if (!simple.length)
|
|
535
584
|
return specs;
|
|
536
|
-
const readsIn = (t, map) => { const out = [];
|
|
537
|
-
|
|
538
|
-
if (o.op === 'get' && o.map === map)
|
|
539
|
-
out.push(o.key);
|
|
540
|
-
for (const val of Object.values(o))
|
|
541
|
-
walk(val);
|
|
542
|
-
} }; walk(t); return out; };
|
|
585
|
+
const readsIn = (t, map) => { const out = []; walk(t, o => { if (o.op === 'get' && o.map === map)
|
|
586
|
+
out.push(o.key); }); return out; };
|
|
543
587
|
return specs.map(n => {
|
|
544
588
|
const insts = [...n.insts], seen = new Set(insts.map(p => JSON.stringify(p)));
|
|
545
589
|
for (let round = 0; round < 2; round++) {
|
|
@@ -727,12 +771,8 @@ export function machineNodes(given, laws, instances, usual, st = tstats()) {
|
|
|
727
771
|
// A function's fact also at its keys with the terms the step gives a state variable (the usual proof reads the state
|
|
728
772
|
// after the step so) as that variable's cell at t, where it then reads the function where the law at t does: at the
|
|
729
773
|
// key its own reads are at, not one the prover would have to prove equal to it.
|
|
730
|
-
const fnReads = (x) => { const out = [];
|
|
731
|
-
|
|
732
|
-
if (o.op === 'get' && !o.map.endsWith('@'))
|
|
733
|
-
out.push(JSON.stringify(o));
|
|
734
|
-
Object.values(o).forEach(walk);
|
|
735
|
-
} }; walk(x); return out; };
|
|
774
|
+
const fnReads = (x) => { const out = []; walk(x, o => { if (o.op === 'get' && !o.map.endsWith('@'))
|
|
775
|
+
out.push(JSON.stringify(o)); }); return out; };
|
|
736
776
|
const concl = substC(l.holds, at(t)), reads = new Set(fnReads(concl));
|
|
737
777
|
const after = (e) => substE(afterStep(substE(e, vars(fresh))), pre);
|
|
738
778
|
const both = (tuple, make, read) => {
|
|
@@ -784,9 +824,7 @@ export function machineNodes(given, laws, instances, usual, st = tstats()) {
|
|
|
784
824
|
const wkeys = new Set(mapNames.flatMap(map => { const w = m.step.maps?.[map]; return w && !('each' in w) ? [JSON.stringify(inlineE(w.key))] : []; }));
|
|
785
825
|
const readsOf = (c) => {
|
|
786
826
|
const out = [], seen = new Set();
|
|
787
|
-
|
|
788
|
-
if (!x || typeof x !== 'object')
|
|
789
|
-
return;
|
|
827
|
+
walk(c, x => {
|
|
790
828
|
const o = x;
|
|
791
829
|
if (o.op === 'if' && o.c.op === '=' && wkeys.has(JSON.stringify(o.c.b))) {
|
|
792
830
|
const id = JSON.stringify([o.c.a, o.c.b]);
|
|
@@ -795,10 +833,7 @@ export function machineNodes(given, laws, instances, usual, st = tstats()) {
|
|
|
795
833
|
out.push({ k: o.c.a, w: o.c.b });
|
|
796
834
|
}
|
|
797
835
|
}
|
|
798
|
-
|
|
799
|
-
walk(val);
|
|
800
|
-
};
|
|
801
|
-
walk(c);
|
|
836
|
+
});
|
|
802
837
|
return out;
|
|
803
838
|
};
|
|
804
839
|
for (const [i, l] of laws.entries()) {
|
|
@@ -1033,10 +1068,7 @@ export function stepLawNodes(m, laws, steps, facts, lawNodes, time = tBack) {
|
|
|
1033
1068
|
function cellsAt(m, facts, x) {
|
|
1034
1069
|
const mapNames = Object.keys(m.maps ?? {}), t = JSON.stringify(v('t$')), fi = (name) => facts.findIndex(f => f.name === name);
|
|
1035
1070
|
const out = [], seen = new Set();
|
|
1036
|
-
|
|
1037
|
-
if (!y || typeof y !== 'object')
|
|
1038
|
-
return;
|
|
1039
|
-
const o = y;
|
|
1071
|
+
walk(x, o => {
|
|
1040
1072
|
if (o.op === 'get' && typeof o.map === 'string' && o.map.endsWith('@') && mapNames.includes(o.map.slice(0, -1))) {
|
|
1041
1073
|
const key = o.key;
|
|
1042
1074
|
if (key.op === 'add' && JSON.stringify(key.a) === t && key.b.op === 'prod') {
|
|
@@ -1047,10 +1079,7 @@ function cellsAt(m, facts, x) {
|
|
|
1047
1079
|
}
|
|
1048
1080
|
}
|
|
1049
1081
|
}
|
|
1050
|
-
|
|
1051
|
-
walk(val);
|
|
1052
|
-
};
|
|
1053
|
-
walk(x);
|
|
1082
|
+
});
|
|
1054
1083
|
return out;
|
|
1055
1084
|
}
|
|
1056
1085
|
// Each map cell a node reads at one of `times` (the first the earliest), pushed to `insts`: that map's step there (at a
|
|
@@ -1101,10 +1130,7 @@ export function allowNode(m, laws, inst, w, facts, lawNodes, name, extra = []) {
|
|
|
1101
1130
|
// Each carried cell the conclusion and the facts given at t read there (a flag's body at its witness, say): 0 (the
|
|
1102
1131
|
// request carries empty sets).
|
|
1103
1132
|
const zeros = [], seen = new Set();
|
|
1104
|
-
|
|
1105
|
-
if (!x || typeof x !== 'object')
|
|
1106
|
-
return;
|
|
1107
|
-
const o = x;
|
|
1133
|
+
walk([concl, instHyps(facts, extra)], o => {
|
|
1108
1134
|
if (o.op === 'get' && typeof o.map === 'string' && o.map.endsWith('@') && carried.includes(o.map.slice(0, -1))) {
|
|
1109
1135
|
const id = JSON.stringify(o);
|
|
1110
1136
|
if (!seen.has(id)) {
|
|
@@ -1112,10 +1138,7 @@ export function allowNode(m, laws, inst, w, facts, lawNodes, name, extra = []) {
|
|
|
1112
1138
|
zeros.push(eq(o, k(0)));
|
|
1113
1139
|
}
|
|
1114
1140
|
}
|
|
1115
|
-
|
|
1116
|
-
walk(val);
|
|
1117
|
-
};
|
|
1118
|
-
walk([concl, instHyps(facts, extra)]);
|
|
1141
|
+
});
|
|
1119
1142
|
const hyps = [...(m.pre ? [m.pre] : []), le(t, T), ...(traced.length ? [{ op: '<', a: T, b: omega }] : []), substC(w, now), ...zeros];
|
|
1120
1143
|
const uses = laws.flatMap((l, i) => tuplesOf(l).map(ts => ({ node: i, subst: Object.fromEntries(keysOf(l).map((kn, q) => [kn, substE(ts[q], now)])), guard: yes })));
|
|
1121
1144
|
const insts = [{ fact: fi(`${m.name}: the start`), at: [t] }, { fact: fi(`${m.name}: a step`), at: [t] },
|
|
@@ -1301,12 +1324,11 @@ export function foldNodes(f, laws, usual, st = tstats()) {
|
|
|
1301
1324
|
// --- the cyclic route beside the usual one (GDP_CYCLIC=1) -------------------------------------------------------------
|
|
1302
1325
|
// With GDP_CYCLIC=1, every proof the instances above cover is made again as a cyclic proof, and a verdict that differs
|
|
1303
1326
|
// throws (GDP_CYCLIC_LAX=1 only prints it, to measure a whole run): the suite run so shows the rules are instances. Each comparison is printed to stderr as a line `cyclic: {...}`.
|
|
1304
|
-
const env = () => globalThis.process?.env;
|
|
1305
1327
|
let quiet = 0;
|
|
1306
|
-
export const cyclicOn = () => quiet === 0 && env
|
|
1328
|
+
export const cyclicOn = () => quiet === 0 && env.GDP_CYCLIC === '1';
|
|
1307
1329
|
// The `cyclic: {...}` lines research/cyclic/ratios.ts reads, printed only with GDP_CYCLIC=1 (the cross-check) or
|
|
1308
1330
|
// GDP_CYCLIC_LOG=1 (modal/run.py sets it): a user's proofs print nothing.
|
|
1309
|
-
export const cycLog = (o) => { if (env
|
|
1331
|
+
export const cycLog = (o) => { if (env.GDP_CYCLIC === '1' || env.GDP_CYCLIC_LOG === '1')
|
|
1310
1332
|
console.error(`cyclic: ${JSON.stringify(o)}`); };
|
|
1311
1333
|
// A search's many trial proofs (core/discover.ts, core/infer.ts) aren't compared: what it finds is proven again.
|
|
1312
1334
|
export const quietly = (f) => { quiet++; try {
|
|
@@ -1341,7 +1363,7 @@ export function translatedProof(key, build, st, base) {
|
|
|
1341
1363
|
}
|
|
1342
1364
|
return { verdict: v };
|
|
1343
1365
|
}
|
|
1344
|
-
export const routeOf = () => env
|
|
1366
|
+
export const routeOf = () => env.GDP_CYCLIC_ROUTE ?? 'translate';
|
|
1345
1367
|
const none = () => ({ proven: false, checked: false, node: '', goal: 'no instance searched', stuck: 'unknown' });
|
|
1346
1368
|
// The cyclic proof of what the usual route proved: translated, else (`fallback`, or `searched` where nothing is
|
|
1347
1369
|
// translated) searched, within `until` where it is given; with why the translation wasn't proven.
|
|
@@ -1381,7 +1403,7 @@ export function crossCyclic(kind, name, usual, run0, usualMs, translated) {
|
|
|
1381
1403
|
search = { searchMs: Math.round(performance.now() - s0), searchCyclic: o.proven && o.checked };
|
|
1382
1404
|
}
|
|
1383
1405
|
cycLog({ kind, name, usual, cyclic, ms, ...(usualMs === undefined ? {} : { usualMs: Math.round(usualMs) }), route: how, ...(how !== 'searched' ? st : {}), ...(why ? { fellBack: why } : {}), ...search, ...prof, ...(out.proven ? { nodes: out.proof.nodes.length, ...(out.why ? { why: out.why } : {}) } : { goal: out.goal, stuck: out.stuck }) });
|
|
1384
|
-
const lax = env
|
|
1406
|
+
const lax = env.GDP_CYCLIC_LAX === '1';
|
|
1385
1407
|
if (cyclic !== usual && !lax)
|
|
1386
1408
|
throw new Error(`cyclic route: ${kind} ${name} is ${cyclic ? 'proven' : 'not proven'} as a cyclic proof, ${usual ? 'proven' : 'not proven'} the usual way${out.proven ? (out.why ? ` (${out.why})` : '') : ` (${out.goal}: ${out.stuck})`}`);
|
|
1387
1409
|
if (search && search.searchCyclic !== usual && !lax)
|
|
@@ -1393,10 +1415,10 @@ export function crossCyclic(kind, name, usual, run0, usualMs, translated) {
|
|
|
1393
1415
|
// checks the usual trees and counts the proof uncovered (`why`). With GDP_CERT_SEARCH=1 (off by default: a search costs
|
|
1394
1416
|
// seconds a proof) a proof the translation doesn't cover is searched first, within GDP_CERT_MS (10 s by default). Each
|
|
1395
1417
|
// logs a line `cyclic: {..., cert: true}` (cycLog).
|
|
1396
|
-
export const certMode = () => env
|
|
1418
|
+
export const certMode = () => env.GDP_CERT !== 'usual';
|
|
1397
1419
|
export const certOn = () => quiet === 0 && certMode();
|
|
1398
|
-
export const certSearch = () => env
|
|
1399
|
-
export const certMs = () => Number(env
|
|
1420
|
+
export const certSearch = () => env.GDP_CERT_SEARCH === '1';
|
|
1421
|
+
export const certMs = () => Number(env.GDP_CERT_MS ?? 10_000);
|
|
1400
1422
|
// A proof of a kind no cyclic instance covers, or whose cyclic proof wasn't made, counted (`cyclic: {..., uncovered:
|
|
1401
1423
|
// true, why}`): its certificate the usual trees.
|
|
1402
1424
|
export const uncovered = (kind, name, why) => cycLog({ kind, name, uncovered: true, why });
|
package/dist/core/data.d.ts
CHANGED
package/dist/core/data.js
CHANGED
|
@@ -1,7 +1,36 @@
|
|
|
1
|
+
// Recursive datatypes as values, proven by cyclic proofs. Untrusted: this file states facts and nodes, kernel/prove.ts
|
|
2
|
+
// grows a tree for every goal, and kernel/cyclic.ts checks the whole, as for every cyclic proof (nothing here is new to
|
|
3
|
+
// the kernel). Lean's twin of a proof (lean-kernel/Data.lean and the export's own definitions) is about the datatype as
|
|
4
|
+
// declared: a Lean `inductive`, the functions by pattern matching, the law over them.
|
|
5
|
+
//
|
|
6
|
+
// The encoding: every natural is a value of every datatype (a bijection between the naturals and the datatype's values,
|
|
7
|
+
// which Lean's twin defines and proves), so a value is a natural and two values are equal exactly when they are the same
|
|
8
|
+
// natural. A datatype D is read through maps: `D.tag[x]` (which constructor), `D.<ctor>.<field>[x]` (its fields, 0 under
|
|
9
|
+
// another constructor), `D.size[x]` (1 plus the sizes of its fields of D's group: the rank). A field is a natural, a value
|
|
10
|
+
// of D, of another datatype of D's group (declared together: a rose tree and its forest), or of one declared before (a
|
|
11
|
+
// list of lists); every datatype has its own codes. A constructor of no field is
|
|
12
|
+
// the constant of its tag; one with fields is a map read at its fields (`D.<ctor>[key]`); a function is a map read at
|
|
13
|
+
// its arguments; several arguments are read at their pairing, left-nested (a + b)² + b, as the library reads a function
|
|
14
|
+
// of several arguments (core/dsl.ts's argsKey). The facts, each at every value of its keys:
|
|
15
|
+
// - the shape: D.tag[x] is a constructor's tag, and under tag j, D.size[x] = 1 + the sizes of x's fields of type D,
|
|
16
|
+
// and the fields of every other constructor 0
|
|
17
|
+
// - eta: under tag j, x is constructor j at its own fields (every natural is a value)
|
|
18
|
+
// - each constructor: D.tag[C[key]] = j and each field of C[key] is its argument
|
|
19
|
+
// - each function: f[key] = its body, a match read as ifs on the tag, a field bound by a case read from its map
|
|
20
|
+
// - congruence of each map read at a pairing: equal arguments, equal cells (the kernel compares a pairing's
|
|
21
|
+
// monomials, not its arguments)
|
|
22
|
+
// A law is a node at rank (level, D.size[x]) for an argument x; its uses read it at x's fields (structural induction),
|
|
23
|
+
// or at the arguments of a function's own recursive calls (recursion induction, other arguments changing with them; on a
|
|
24
|
+
// natural n, at n − 1, its rank n), each where the tag is the case's. On a datatype of a group, the law has a companion
|
|
25
|
+
// for each other datatype x's fields reach (the law lifted through each function's own call at such a field), each node
|
|
26
|
+
// using the others at the fields of their datatypes (mutual induction). Lemmas the law needs are found by the values
|
|
27
|
+
// (theory exploration: equations between terms the random values agree on, then the shapes of a predicate kept), proven
|
|
28
|
+
// the same way at lower levels, and used at the terms of the law they match.
|
|
29
|
+
import { argsKey, atMost as leC, get, n as k, same as eqC, walk, x as ev } from "./machine.js";
|
|
1
30
|
import { substC, substE } from "../kernel/kernel.js";
|
|
2
|
-
import { proveCyclic } from "./cyclic.js";
|
|
31
|
+
import { and as andC, implies, proveCyclic } from "./cyclic.js";
|
|
3
32
|
import { clock, counted, pairedKeys, withDeadline } from "../kernel/prove.js";
|
|
4
|
-
import { work } from "./solver.js";
|
|
33
|
+
import { env, work } from "./solver.js";
|
|
5
34
|
import { rankOfPaths, ranksOfCalls, showC } from "./cycles.js";
|
|
6
35
|
import { proveThrough, transferPlan } from "./transfer.js";
|
|
7
36
|
// --- sorts and the AST ------------------------------------------------------------------------------------------------
|
|
@@ -302,18 +331,6 @@ export function define(name, params, ret, body, opts = {}) {
|
|
|
302
331
|
export const fnParam = (name) => Object.assign((x) => new Nat({ op: 'apply', param: name, arg: natX(x) }), { param: name });
|
|
303
332
|
const paramsIn = (t) => { const out = []; walk(t, o => { if (o.op === 'apply')
|
|
304
333
|
out.push(o.param); }); return out; };
|
|
305
|
-
function walk(t, f) {
|
|
306
|
-
if (Array.isArray(t)) {
|
|
307
|
-
t.forEach(x => walk(x, f));
|
|
308
|
-
return;
|
|
309
|
-
}
|
|
310
|
-
if (!t || typeof t !== 'object')
|
|
311
|
-
return;
|
|
312
|
-
const o = t;
|
|
313
|
-
f(o);
|
|
314
|
-
for (const v of Object.values(o))
|
|
315
|
-
walk(v, f);
|
|
316
|
-
}
|
|
317
334
|
// Each object once, in walk's order (a term shared within another is visited where walk first reaches it).
|
|
318
335
|
function walkOnce(t, f, seen = new WeakSet()) {
|
|
319
336
|
if (!t || typeof t !== 'object' || seen.has(t))
|
|
@@ -1062,16 +1079,8 @@ export function samples(vars, paramNames, n, seed = 1) {
|
|
|
1062
1079
|
return out;
|
|
1063
1080
|
}
|
|
1064
1081
|
// --- the encoding -----------------------------------------------------------------------------------------------------
|
|
1065
|
-
const ev = (name) => ({ op: 'var', name });
|
|
1066
|
-
const k = (value) => ({ op: 'const', value });
|
|
1067
|
-
const get = (map, key) => ({ op: 'get', map, key });
|
|
1068
|
-
const eqC = (a, b) => ({ op: '=', a, b });
|
|
1069
|
-
const leC = (a, b) => ({ op: '<=', a, b });
|
|
1070
|
-
const andC = (...xs) => (xs.length === 1 ? xs[0] : { op: 'and', xs });
|
|
1071
|
-
const implies = (a, b) => ({ op: 'or', xs: [{ op: 'not', x: a }, b] });
|
|
1072
1082
|
const yesC = leC(k(0), k(0));
|
|
1073
|
-
|
|
1074
|
-
export const keyOf = (args) => args.slice(1).reduce(pairE, args[0]);
|
|
1083
|
+
export const keyOf = argsKey;
|
|
1075
1084
|
// The arguments of a key built by keyOf (n of them), read back from its shape.
|
|
1076
1085
|
export function argsOf(key, n) {
|
|
1077
1086
|
if (n === 1)
|
|
@@ -2636,11 +2645,11 @@ function casesOf(p, unfolded, vars, models) {
|
|
|
2636
2645
|
// One attempt: the law's node (and the lemmas', proven before, with their own nodes) at a scheme and a depth. With
|
|
2637
2646
|
// several cases, a main node uses a node per case (each its guards as hypotheses, its own instances), at rank
|
|
2638
2647
|
// (level, size, 1) above theirs (level, size, 0); a case node uses the main node at the children.
|
|
2639
|
-
const dataTrace = !!
|
|
2648
|
+
const dataTrace = !!env.DATA_TRACE;
|
|
2640
2649
|
// Debugging: every congruence a model's closure makes, not only those explanations use.
|
|
2641
|
-
const allCong = !!
|
|
2650
|
+
const allCong = !!env.DATA_ALLCONG;
|
|
2642
2651
|
// Debugging: a failed attempt's node whose name the pattern matches, written out (hypotheses, uses, instances).
|
|
2643
|
-
const nodeTrace =
|
|
2652
|
+
const nodeTrace = env.DATA_NODE;
|
|
2644
2653
|
function attempt(p, fs, l, scheme, depth, lemmas, level, pool) {
|
|
2645
2654
|
const t0 = clock();
|
|
2646
2655
|
anchoredAt.clear();
|
|
@@ -2856,43 +2865,50 @@ function reached(p, fs, insts, from, ms) {
|
|
|
2856
2865
|
return reach.has(id(get(m, keyOf(q.at.slice(0, n))))) && reach.has(id(get(m, keyOf(q.at.slice(n)))));
|
|
2857
2866
|
});
|
|
2858
2867
|
}
|
|
2859
|
-
//
|
|
2860
|
-
//
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
const id = tid, parent = new Map(), terms = new Map();
|
|
2868
|
+
// Classes of terms by text (`union`, each term given to `onUnion`), closed under congruence (`congruent`: two reads of a
|
|
2869
|
+
// map, each given to `cell`, at arguments of one class are one), by signatures.
|
|
2870
|
+
function congruence(fs, onUnion) {
|
|
2871
|
+
const parent = new Map(), cells = new Map();
|
|
2864
2872
|
const find = (x) => { let r = x; while (parent.has(r))
|
|
2865
2873
|
r = parent.get(r); return r; };
|
|
2866
|
-
const union = (x, y) => {
|
|
2874
|
+
const union = (x, y) => { onUnion?.(x); onUnion?.(y); const a = find(tid(x)), b = find(tid(y)); if (a !== b)
|
|
2867
2875
|
parent.set(a, b); };
|
|
2868
|
-
const
|
|
2869
|
-
|
|
2870
|
-
|
|
2871
|
-
for (const leaf of leaves(b))
|
|
2872
|
-
union(a, leaf);
|
|
2873
|
-
// Closed under congruence (two reads of one map at arguments of one class), by signatures.
|
|
2874
|
-
const cells = new Map(), classes = new Map();
|
|
2875
|
-
const close = (t) => {
|
|
2876
|
-
walkOnce(t, o => { if (o.op !== 'get')
|
|
2877
|
-
return; const n = fs.arity.get(o.map), as = n ? argsOf(o.key, n) : undefined; if (as)
|
|
2878
|
-
(cells.get(o.map) ?? cells.set(o.map, []).get(o.map)).push({ key: o.key, as }); });
|
|
2876
|
+
const cell = (o) => { const n = fs.arity.get(o.map), as = n ? argsOf(o.key, n) : undefined; if (as)
|
|
2877
|
+
(cells.get(o.map) ?? cells.set(o.map, []).get(o.map)).push({ key: o.key, as }); };
|
|
2878
|
+
const congruent = () => {
|
|
2879
2879
|
for (let changed = true, rounds = 0; changed && rounds < 8; rounds++) {
|
|
2880
2880
|
changed = false;
|
|
2881
2881
|
for (const [map, list] of cells) {
|
|
2882
2882
|
const bySig = new Map();
|
|
2883
2883
|
for (const x of list) {
|
|
2884
|
-
const sig = x.as.map(
|
|
2885
|
-
if (!y)
|
|
2884
|
+
const sig = x.as.map(t => find(tid(t))).join('\u0001'), y = bySig.get(sig), cx = get(map, x.key);
|
|
2885
|
+
if (!y)
|
|
2886
2886
|
bySig.set(sig, cx);
|
|
2887
|
-
|
|
2888
|
-
}
|
|
2889
|
-
if (find(id(y)) !== find(id(cx))) {
|
|
2887
|
+
else if (find(tid(y)) !== find(tid(cx))) {
|
|
2890
2888
|
union(y, cx);
|
|
2891
2889
|
changed = true;
|
|
2892
2890
|
}
|
|
2893
2891
|
}
|
|
2894
2892
|
}
|
|
2895
2893
|
}
|
|
2894
|
+
};
|
|
2895
|
+
return { find, union, cell, cells, congruent };
|
|
2896
|
+
}
|
|
2897
|
+
// The uses of lemmas a goal's reads match: each lemma's largest reads (those inside no other) matched against the goal's
|
|
2898
|
+
// reads of the same map at keys equal, by the instances' equations (every branch), to the read's key.
|
|
2899
|
+
// `hits`: the lemmas (their nodes) with any use at all, gathered there (and no uses made): whether a goal reads each.
|
|
2900
|
+
function lemmaUsesFor(p, fs, lemmas, from, insts, models = [], spare, hits) {
|
|
2901
|
+
const terms = new Map(), { find, union, cell, congruent } = congruence(fs, x => terms.set(tid(x), x));
|
|
2902
|
+
const leaves = (e) => (e.op === 'if' ? [...leaves(e.then), ...leaves(e.else)] : [e]);
|
|
2903
|
+
for (const q of insts)
|
|
2904
|
+
for (const [a, b] of equalities(holdsAt(fs, q)))
|
|
2905
|
+
for (const leaf of leaves(b))
|
|
2906
|
+
union(a, leaf);
|
|
2907
|
+
const classes = new Map();
|
|
2908
|
+
const close = (t) => {
|
|
2909
|
+
walkOnce(t, o => { if (o.op === 'get')
|
|
2910
|
+
cell(o); });
|
|
2911
|
+
congruent();
|
|
2896
2912
|
classes.clear();
|
|
2897
2913
|
for (const [key, e] of terms) {
|
|
2898
2914
|
const r = find(key);
|
|
@@ -3078,11 +3094,7 @@ const matchedAt = new WeakMap();
|
|
|
3078
3094
|
// is a step the case's own rewriting passes through, from one of its terms to another (ripple's fertilisation), where
|
|
3079
3095
|
// a use that only matches one of its terms is one its models alone may agree with (app (t, []) for t).
|
|
3080
3096
|
function caseJoins(fs, from, insts, hyps, ms) {
|
|
3081
|
-
const
|
|
3082
|
-
const find = (x) => { let r = x; while (parent.has(r))
|
|
3083
|
-
r = parent.get(r); return r; };
|
|
3084
|
-
const union = (x, y) => { const a = find(tid(x)), b = find(tid(y)); if (a !== b)
|
|
3085
|
-
parent.set(a, b); };
|
|
3097
|
+
const { find, union, cell, cells, congruent } = congruence(fs);
|
|
3086
3098
|
const truth = (c) => { let v; for (const { m, env } of ms) {
|
|
3087
3099
|
let t;
|
|
3088
3100
|
try {
|
|
@@ -3119,25 +3131,10 @@ function caseJoins(fs, from, insts, hyps, ms) {
|
|
|
3119
3131
|
eqs(holdsAt(fs, q));
|
|
3120
3132
|
for (const h of hyps)
|
|
3121
3133
|
eqs(h);
|
|
3122
|
-
const
|
|
3134
|
+
const seen = new Set();
|
|
3123
3135
|
walkOnce([from, insts.map(q => holdsAt(fs, q)), hyps], o => { if (o.op !== 'get')
|
|
3124
|
-
return; seen.add(tid(o));
|
|
3125
|
-
|
|
3126
|
-
for (let changed = true, rounds = 0; changed && rounds < 8; rounds++) {
|
|
3127
|
-
changed = false;
|
|
3128
|
-
for (const [map, list] of cells) {
|
|
3129
|
-
const bySig = new Map();
|
|
3130
|
-
for (const x of list) {
|
|
3131
|
-
const sig = x.as.map(t => find(tid(t))).join('\u0001'), y = bySig.get(sig), cx = get(map, x.key);
|
|
3132
|
-
if (!y)
|
|
3133
|
-
bySig.set(sig, cx);
|
|
3134
|
-
else if (find(tid(y)) !== find(tid(cx))) {
|
|
3135
|
-
union(y, cx);
|
|
3136
|
-
changed = true;
|
|
3137
|
-
}
|
|
3138
|
-
}
|
|
3139
|
-
}
|
|
3140
|
-
}
|
|
3136
|
+
return; seen.add(tid(o)); cell(o); });
|
|
3137
|
+
congruent();
|
|
3141
3138
|
const bySig = new Map();
|
|
3142
3139
|
for (const [map, list] of cells)
|
|
3143
3140
|
for (const x of list)
|
|
@@ -3355,33 +3352,6 @@ export function conjectures(p, l, max = 24, maxSize = 6) {
|
|
|
3355
3352
|
vs.add(o.name); }); return vs.size; };
|
|
3356
3353
|
eqs.sort((a, b) => a.size - b.size || nvars(b) - nvars(a));
|
|
3357
3354
|
const rules = [];
|
|
3358
|
-
const matchX = (pat, t, vs, sub) => {
|
|
3359
|
-
if (pat.op === 'var' && vs.has(pat.name)) {
|
|
3360
|
-
const had = sub.get(pat.name);
|
|
3361
|
-
if (had)
|
|
3362
|
-
return JSON.stringify(had) === JSON.stringify(t);
|
|
3363
|
-
sub.set(pat.name, t);
|
|
3364
|
-
return true;
|
|
3365
|
-
}
|
|
3366
|
-
if (pat.op !== t.op)
|
|
3367
|
-
return false;
|
|
3368
|
-
const po = pat, to = t;
|
|
3369
|
-
for (const key of Object.keys(po)) {
|
|
3370
|
-
const a2 = po[key], b2 = to[key];
|
|
3371
|
-
if (Array.isArray(a2)) {
|
|
3372
|
-
if (!Array.isArray(b2) || a2.length !== b2.length || !a2.every((x, i) => matchX(x, b2[i], vs, sub)))
|
|
3373
|
-
return false;
|
|
3374
|
-
}
|
|
3375
|
-
else if (a2 && typeof a2 === 'object') {
|
|
3376
|
-
if (!b2 || typeof b2 !== 'object' || !matchX(a2, b2, vs, sub))
|
|
3377
|
-
return false;
|
|
3378
|
-
}
|
|
3379
|
-
else if (a2 !== b2)
|
|
3380
|
-
return false;
|
|
3381
|
-
}
|
|
3382
|
-
return true;
|
|
3383
|
-
};
|
|
3384
|
-
const substX = (x, sub) => JSON.parse(JSON.stringify(x), (_, v) => (v && typeof v === 'object' && v.op === 'var' && sub.has(v.name) ? sub.get(v.name) : v));
|
|
3385
3355
|
const norm = (x, depth = 0) => {
|
|
3386
3356
|
if (depth > 30)
|
|
3387
3357
|
return x;
|
|
@@ -3399,7 +3369,7 @@ export function conjectures(p, l, max = 24, maxSize = 6) {
|
|
|
3399
3369
|
for (const r of rules) {
|
|
3400
3370
|
const sub = new Map();
|
|
3401
3371
|
if (matchX(r.l, t, r.vs, sub))
|
|
3402
|
-
return norm(
|
|
3372
|
+
return norm(substXs(r.r, sub), depth + 1);
|
|
3403
3373
|
}
|
|
3404
3374
|
return t;
|
|
3405
3375
|
};
|
|
@@ -3603,6 +3573,25 @@ function canonText(y, vars) {
|
|
|
3603
3573
|
}
|
|
3604
3574
|
const substXs = (x, sub) => (sub.size ? JSON.parse(JSON.stringify(x), (_, v) => (v && typeof v === 'object' && v.op === 'var' && sub.has(v.name) ? sub.get(v.name) : v)) : x);
|
|
3605
3575
|
const sameX = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
3576
|
+
// Whether t is pat with each of `free` given a term (in `sub`, one term a name).
|
|
3577
|
+
const matchX = (pat, t, free, sub) => {
|
|
3578
|
+
if (pat && typeof pat === 'object' && pat.op === 'var' && free.has(pat.name)) {
|
|
3579
|
+
const n = pat.name, had = sub.get(n);
|
|
3580
|
+
if (had)
|
|
3581
|
+
return sameX(had, t);
|
|
3582
|
+
sub.set(n, t);
|
|
3583
|
+
return true;
|
|
3584
|
+
}
|
|
3585
|
+
if (Array.isArray(pat))
|
|
3586
|
+
return Array.isArray(t) && pat.length === t.length && pat.every((x, i) => matchX(x, t[i], free, sub));
|
|
3587
|
+
if (pat && typeof pat === 'object') {
|
|
3588
|
+
if (!t || typeof t !== 'object' || Array.isArray(t))
|
|
3589
|
+
return false;
|
|
3590
|
+
const po = pat, to = t;
|
|
3591
|
+
return Object.keys(po).length === Object.keys(to).length && Object.keys(po).every(key => matchX(po[key], to[key], free, sub));
|
|
3592
|
+
}
|
|
3593
|
+
return pat === t;
|
|
3594
|
+
};
|
|
3606
3595
|
// A term with its definitions unfolded where they are decided: a call whose body, its parameters given, decides its
|
|
3607
3596
|
// first choice (the match, natmatch or if at its top, and the ones in the case taken) is that case's term (else the
|
|
3608
3597
|
// call stays); a match of a constructor, a natmatch of a constant or of a successor (x + k), an if whose condition the
|
|
@@ -3810,24 +3799,6 @@ function stepsOf(p, l) {
|
|
|
3810
3799
|
const rewrite = (x, from, to) => { const f = JSON.stringify(from); return JSON.parse(JSON.stringify(x), (_, v) => (v && typeof v === 'object' && v.op && JSON.stringify(v) === f ? to : v)); };
|
|
3811
3800
|
// The hypothesis at any values of the law's other variables (`free`): from's instances rewritten to to's, outermost
|
|
3812
3801
|
// first (qrev (x, a :: y) to rev x ++ (a :: y), the hypothesis read at y := a :: y). Whether one was.
|
|
3813
|
-
const matchP = (pat, t, free, sub) => {
|
|
3814
|
-
if (pat && typeof pat === 'object' && pat.op === 'var' && free.has(pat.name)) {
|
|
3815
|
-
const n = pat.name, had = sub.get(n);
|
|
3816
|
-
if (had)
|
|
3817
|
-
return sameX(had, t);
|
|
3818
|
-
sub.set(n, t);
|
|
3819
|
-
return true;
|
|
3820
|
-
}
|
|
3821
|
-
if (Array.isArray(pat))
|
|
3822
|
-
return Array.isArray(t) && pat.length === t.length && pat.every((x, i) => matchP(x, t[i], free, sub));
|
|
3823
|
-
if (pat && typeof pat === 'object') {
|
|
3824
|
-
if (!t || typeof t !== 'object' || Array.isArray(t))
|
|
3825
|
-
return false;
|
|
3826
|
-
const po = pat, to = t;
|
|
3827
|
-
return Object.keys(po).length === Object.keys(to).length && Object.keys(po).every(key => matchP(po[key], to[key], free, sub));
|
|
3828
|
-
}
|
|
3829
|
-
return pat === t;
|
|
3830
|
-
};
|
|
3831
3802
|
const rewriteP = (x, from, to, free) => {
|
|
3832
3803
|
let hit = false;
|
|
3833
3804
|
const go = (t) => {
|
|
@@ -3836,7 +3807,7 @@ function stepsOf(p, l) {
|
|
|
3836
3807
|
if (Array.isArray(t))
|
|
3837
3808
|
return t.map(go);
|
|
3838
3809
|
const sub = new Map();
|
|
3839
|
-
if (t.op &&
|
|
3810
|
+
if (t.op && matchX(from, t, free, sub)) {
|
|
3840
3811
|
hit = true;
|
|
3841
3812
|
return substXs(to, sub);
|
|
3842
3813
|
}
|
package/dist/core/discover.js
CHANGED
|
@@ -11,9 +11,9 @@
|
|
|
11
11
|
// out.verdict // the laws, proven of out.rule: r with its functions and lists made again with the lemmas
|
|
12
12
|
// out.found // per function or list, the lemmas found, as source to paste into recur(...) or carriesList(...)
|
|
13
13
|
import { quietly } from "./cyclic.js";
|
|
14
|
-
import { any, atMost, compileFunction, evalC, evalE, n, plus, prod, same, times, x } from "./machine.js";
|
|
14
|
+
import { any, atMost, compileFunction, evalC, evalE, n, plus, prod, same, seeded, times, x } from "./machine.js";
|
|
15
15
|
import { Unproven, lit, proveFold, proveSteps, recur, recurOf } from "./dsl.js";
|
|
16
|
-
|
|
16
|
+
import { gcd } from "./solver.js";
|
|
17
17
|
const valueOf = (t) => (typeof t === 'number' ? t : t.val);
|
|
18
18
|
const evalLin = (l, at) => Object.entries(l).reduce((s, [a, c]) => s + c * (a === '1' ? 1 : at[a]), 0);
|
|
19
19
|
const holds = (c, at) => { const v = evalLin(c.lin, at); return c.rel === '=' ? v === 0 : v <= 0; };
|
|
@@ -117,7 +117,6 @@ function closedForm(samples, show) {
|
|
|
117
117
|
// The law a goal is of (the kernel names each goal after its law: '<law> @step').
|
|
118
118
|
const lawOfGoal = (goal) => goal.replace(/ @(start|step|step law|\d+ steps|leaf|node)$/, '');
|
|
119
119
|
const isGoalOf = (goal) => (c) => { const law = lawOfGoal(goal); return law === c.name || law === `${c.name} at y`; };
|
|
120
|
-
const gcd = (a, b) => (b === 0 ? Math.abs(a) : gcd(b, a % b));
|
|
121
120
|
// --- recurrences ---------------------------------------------------------------------------------------------------
|
|
122
121
|
const showRecur = (a) => (a === 'f' ? 'f(x)' : a === 'g' ? 'f(y)' : a === 'xx' ? 'x·x' : a === 'xxx' ? 'x·x·x' : a);
|
|
123
122
|
const srcRecur = (a) => (a === 'xx' ? 'x.mul(x)' : a === 'xxx' ? 'x.mul(x).mul(x)' : a);
|