gdp-wired 0.24.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 +44 -0
- package/dist/core/accel.js +955 -0
- 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 +409 -174
- package/dist/core/cyclic.d.ts +4 -1
- package/dist/core/cyclic.js +86 -64
- package/dist/core/data.d.ts +9 -1
- package/dist/core/data.js +872 -168
- 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.d.ts +1 -0
- package/dist/core/infer.js +364 -40
- package/dist/core/machine.d.ts +6 -0
- package/dist/core/machine.js +35 -3
- package/dist/core/nonterm.d.ts +9 -2
- package/dist/core/nonterm.js +173 -9
- 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 +95 -39
- package/dist/core/solver.d.ts +12 -0
- package/dist/core/solver.js +76 -30
- package/dist/core/transfer.js +23 -14
- 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.d.ts +5 -0
- package/dist/kernel/cdcl.js +44 -8
- package/dist/kernel/prove.d.ts +6 -1
- package/dist/kernel/prove.js +101 -85
- 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/data.js
CHANGED
|
@@ -1,8 +1,37 @@
|
|
|
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";
|
|
5
|
-
import { rankOfPaths, ranksOfCalls } from "./cycles.js";
|
|
33
|
+
import { env, work } from "./solver.js";
|
|
34
|
+
import { rankOfPaths, ranksOfCalls, showC } from "./cycles.js";
|
|
6
35
|
import { proveThrough, transferPlan } from "./transfer.js";
|
|
7
36
|
// --- sorts and the AST ------------------------------------------------------------------------------------------------
|
|
8
37
|
export const self = { self: true };
|
|
@@ -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)
|
|
@@ -1694,7 +1703,7 @@ function unfoldAt(f) {
|
|
|
1694
1703
|
// The inductions the search tries for a law (its schemes), for tests.
|
|
1695
1704
|
export const schemesOf = (p, l) => schemes(p, l);
|
|
1696
1705
|
function schemes(p, l) {
|
|
1697
|
-
const out = [], seen = new Set();
|
|
1706
|
+
const out = [], seen = new Set(), halves = [];
|
|
1698
1707
|
const add = (s) => { const id = JSON.stringify(s.uses); if (!seen.has(id)) {
|
|
1699
1708
|
seen.add(id);
|
|
1700
1709
|
out.push(s);
|
|
@@ -1713,7 +1722,14 @@ function schemes(p, l) {
|
|
|
1713
1722
|
if (!m && (at.op !== 'var' || !l.vars.some(v => v.name === at.name)))
|
|
1714
1723
|
return;
|
|
1715
1724
|
const d = f.params[f.decreases ?? 0].sort;
|
|
1716
|
-
const
|
|
1725
|
+
const isVar = (x) => x.op === 'var' && l.vars.some(v => v.name === x.name);
|
|
1726
|
+
// (an argument that is no variable of the law read as the term it is, where its variables are the law's: its changes
|
|
1727
|
+
// at a recursive call then constrain nothing, the scheme being one half of a merged one, below)
|
|
1728
|
+
const closed = (x) => { let ok = true; walk(x, o => { if (o.op === 'var' && !l.vars.some(v => v.name === o.name))
|
|
1729
|
+
ok = false; if (o.op === 'match' || o.op === 'natmatch' || o.op === 'if')
|
|
1730
|
+
ok = false; }); return ok; };
|
|
1731
|
+
const formal = new Map(f.params.map((q, i) => [`${f.name}.${q.name}`, isVar(args[i]) ? ev(args[i].name) : closed(args[i]) ? encX(p, args[i], new Map()) : undefined]));
|
|
1732
|
+
let relaxed = false;
|
|
1717
1733
|
const uses = [];
|
|
1718
1734
|
// (`through`: the functions of f's ring whose bodies are read in place of their calls, once each: evens through
|
|
1719
1735
|
// odds, which call each other, is recursion of evens at the tail's tail)
|
|
@@ -1759,7 +1775,12 @@ function schemes(p, l) {
|
|
|
1759
1775
|
t.args.forEach((a, i) => {
|
|
1760
1776
|
const lv = args[i];
|
|
1761
1777
|
if (lv.op !== 'var' || !l.vars.some(v => v.name === lv.name)) {
|
|
1762
|
-
|
|
1778
|
+
if (JSON.stringify(a) !== JSON.stringify({ op: 'var', name: `${f.name}.${f.params[i].name}` })) {
|
|
1779
|
+
if (formal.get(`${f.name}.${f.params[i].name}`))
|
|
1780
|
+
relaxed = true;
|
|
1781
|
+
else
|
|
1782
|
+
ok = false;
|
|
1783
|
+
}
|
|
1763
1784
|
return;
|
|
1764
1785
|
}
|
|
1765
1786
|
const e = encX(p, a, env);
|
|
@@ -1777,7 +1798,11 @@ function schemes(p, l) {
|
|
|
1777
1798
|
if (!uses.length)
|
|
1778
1799
|
return;
|
|
1779
1800
|
if (!m) {
|
|
1780
|
-
|
|
1801
|
+
const sc = { name: `recursion of ${f.name} on ${at.name}`, on: { v: at.name, d }, uses };
|
|
1802
|
+
if (relaxed)
|
|
1803
|
+
halves.push(sc);
|
|
1804
|
+
else
|
|
1805
|
+
add(sc);
|
|
1781
1806
|
return;
|
|
1782
1807
|
}
|
|
1783
1808
|
// Well-founded: ranked by the measure at the law's arguments; the law's variables it reads named.
|
|
@@ -1797,6 +1822,35 @@ function schemes(p, l) {
|
|
|
1797
1822
|
}
|
|
1798
1823
|
add({ name: `structure of ${v.name}`, on: { v: v.name, d }, uses: d.ctors.flatMap((c, j) => c.fields.flatMap((f, i) => (isDataField(f.sort) && f.sort.data === d.name ? [{ subst: { [v.name]: get(fieldMap(d, j, i), x) }, guard: eqC(get(tagMap(d.name), x), k(j)) }] : []))) });
|
|
1799
1824
|
}
|
|
1825
|
+
// Two schemes merged (Boyer and Moore's): each pair of their uses that agree where both substitute a variable, the two
|
|
1826
|
+
// substitutions at once under both guards (drop n (take m xs): n, m and xs each one smaller where n and m are
|
|
1827
|
+
// successors and xs a cons), ranked by a datatype's variable both descend on, else a natural's. After the others.
|
|
1828
|
+
const plainOnes = [...out.filter(sc => sc.on && !sc.rank && !sc.companions), ...halves];
|
|
1829
|
+
let merged = 0;
|
|
1830
|
+
for (const [i, s1] of plainOnes.entries())
|
|
1831
|
+
for (const s2 of plainOnes.slice(i + 1)) {
|
|
1832
|
+
if (merged >= 4 || s1.on.v === s2.on.v)
|
|
1833
|
+
continue;
|
|
1834
|
+
const uses = [];
|
|
1835
|
+
for (const u1 of s1.uses)
|
|
1836
|
+
for (const u2 of s2.uses) {
|
|
1837
|
+
if (Object.keys(u1.subst).some(x => x in u2.subst && tid(u1.subst[x]) !== tid(u2.subst[x])))
|
|
1838
|
+
continue;
|
|
1839
|
+
const subst = { ...u1.subst, ...u2.subst };
|
|
1840
|
+
if (Object.keys(subst).length <= Math.max(Object.keys(u1.subst).length, Object.keys(u2.subst).length))
|
|
1841
|
+
continue;
|
|
1842
|
+
uses.push({ subst, guard: andC(u1.guard, u2.guard) });
|
|
1843
|
+
}
|
|
1844
|
+
if (!uses.length)
|
|
1845
|
+
continue;
|
|
1846
|
+
const on = [s2.on, s1.on].find(o => o.d !== 'nat' && uses.every(u => o.v in u.subst)) ?? [s2.on, s1.on].find(o => uses.every(u => o.v in u.subst));
|
|
1847
|
+
if (!on)
|
|
1848
|
+
continue;
|
|
1849
|
+
const before = out.length;
|
|
1850
|
+
add({ name: `${s1.name} with ${s2.name}`, on, uses, merged: true });
|
|
1851
|
+
if (out.length > before)
|
|
1852
|
+
merged++;
|
|
1853
|
+
}
|
|
1800
1854
|
return out;
|
|
1801
1855
|
}
|
|
1802
1856
|
function instancesOf(p, fs, w) {
|
|
@@ -1879,12 +1933,17 @@ function instancesOf(p, fs, w) {
|
|
|
1879
1933
|
};
|
|
1880
1934
|
ariths();
|
|
1881
1935
|
lap(`unfolded, ${insts.length} instances, ${all.length} reads`);
|
|
1882
|
-
|
|
1936
|
+
// `twins`: natural arguments of a function equal in every model of a case (its node's), one class too, with the
|
|
1937
|
+
// congruences that reads at them then meet (where the case's arguments agree by arithmetic its facts give: n ∸ len xs
|
|
1938
|
+
// and (n ∸ 1) ∸ len (tail xs), xs a cons); an explanation takes such a step only where the facts give none.
|
|
1939
|
+
const twinValues = new Map();
|
|
1940
|
+
const truncated = (t) => t.op === 'sub' || ((t.op === 'add' || t.op === 'mul') && (truncated(t.a) || (t.op === 'add' && truncated(t.b))));
|
|
1941
|
+
const closure = (model, twins = false) => {
|
|
1883
1942
|
const parent = new Map(), terms = new Map(), adj = new Map();
|
|
1884
1943
|
const find = (x) => { let r = x; while (parent.has(r))
|
|
1885
1944
|
r = parent.get(r); return r; };
|
|
1886
1945
|
const linked = new Set();
|
|
1887
|
-
const edge = (x, y, cong) => {
|
|
1946
|
+
const edge = (x, y, cong, twin) => {
|
|
1888
1947
|
const a2 = tid(x), b2 = tid(y);
|
|
1889
1948
|
terms.set(a2, x);
|
|
1890
1949
|
terms.set(b2, y);
|
|
@@ -1897,8 +1956,8 @@ function instancesOf(p, fs, w) {
|
|
|
1897
1956
|
linked.add(a2 < b2 ? `${a2}|${b2}` : `${b2}|${a2}`);
|
|
1898
1957
|
if (merged)
|
|
1899
1958
|
parent.set(find(a2), find(b2));
|
|
1900
|
-
(adj.get(a2) ?? adj.set(a2, []).get(a2)).push({ to: b2, cong });
|
|
1901
|
-
(adj.get(b2) ?? adj.set(b2, []).get(b2)).push({ to: a2, cong: cong && { map: cong.map, x: cong.y, y: cong.x } });
|
|
1959
|
+
(adj.get(a2) ?? adj.set(a2, []).get(a2)).push({ to: b2, cong, twin });
|
|
1960
|
+
(adj.get(b2) ?? adj.set(b2, []).get(b2)).push({ to: a2, cong: cong && { map: cong.map, x: cong.y, y: cong.x }, twin });
|
|
1902
1961
|
return merged;
|
|
1903
1962
|
};
|
|
1904
1963
|
const truthOf = (c) => { if (!model)
|
|
@@ -1940,6 +1999,9 @@ function instancesOf(p, fs, w) {
|
|
|
1940
1999
|
active(holdsAt(fs.facts[q.fact].name, q.at));
|
|
1941
2000
|
for (const c of w.others)
|
|
1942
2001
|
active(c);
|
|
2002
|
+
for (const c of w.hyps ?? [])
|
|
2003
|
+
if (c.op === '=')
|
|
2004
|
+
edge(c.a, c.b);
|
|
1943
2005
|
const cells = new Map();
|
|
1944
2006
|
for (const r of all) {
|
|
1945
2007
|
const n = fs.arity.get(r.map), as = n ? argsOf(r.key, n) : undefined;
|
|
@@ -1949,10 +2011,22 @@ function instancesOf(p, fs, w) {
|
|
|
1949
2011
|
// Arguments equal by arithmetic alone, one class: (n + 1) ∸ 1 and n (a successor's predecessor, where a definition
|
|
1950
2012
|
// on the naturals is unfolded at n + 1), so their reads meet by congruence. The kernel proves the equality itself.
|
|
1951
2013
|
const byForm = new Map();
|
|
2014
|
+
const at = model && ((t) => { try {
|
|
2015
|
+
return model.m.evalE(t, model.env);
|
|
2016
|
+
}
|
|
2017
|
+
catch {
|
|
2018
|
+
return undefined;
|
|
2019
|
+
} });
|
|
2020
|
+
// (a read or variable of a constant's class, that constant: n + m ∸ 1 and m ∸ 1 one form where the case has n = 0)
|
|
2021
|
+
const consts = new Map();
|
|
2022
|
+
for (const [key, t] of terms)
|
|
2023
|
+
if (t.op === 'const')
|
|
2024
|
+
consts.set(find(key), t.value);
|
|
2025
|
+
const known = (t) => consts.get(find(tid(t)));
|
|
1952
2026
|
for (const list of cells.values())
|
|
1953
2027
|
for (const x of list)
|
|
1954
2028
|
for (const t of x.as) {
|
|
1955
|
-
const f = linearForm(t);
|
|
2029
|
+
const f = linearForm(t, at, known);
|
|
1956
2030
|
if (f === undefined)
|
|
1957
2031
|
continue;
|
|
1958
2032
|
const had = byForm.get(f);
|
|
@@ -1961,6 +2035,40 @@ function instancesOf(p, fs, w) {
|
|
|
1961
2035
|
else if (tid(had) !== tid(t))
|
|
1962
2036
|
edge(had, t);
|
|
1963
2037
|
}
|
|
2038
|
+
if (twins && w.hyps && w.models.length >= 4) {
|
|
2039
|
+
const byValue = new Map();
|
|
2040
|
+
for (const [map, list] of cells) {
|
|
2041
|
+
const f = fnOfMap(map);
|
|
2042
|
+
if (!f)
|
|
2043
|
+
continue;
|
|
2044
|
+
for (const x of list)
|
|
2045
|
+
for (const [i, t] of x.as.entries()) {
|
|
2046
|
+
// (a difference, truncated, the form linear forms leave: n ∸ len xs; not a value the facts give, x for run [] x)
|
|
2047
|
+
if (f.params[i]?.sort !== 'nat' || !truncated(t))
|
|
2048
|
+
continue;
|
|
2049
|
+
const id = tid(t);
|
|
2050
|
+
let vs = twinValues.get(id);
|
|
2051
|
+
// (a value the same in every model is no witness: it is a constant's, or the models' chance)
|
|
2052
|
+
if (vs === undefined) {
|
|
2053
|
+
vs = null;
|
|
2054
|
+
try {
|
|
2055
|
+
const xs = w.models.map(({ m, env }) => m.evalE(t, env));
|
|
2056
|
+
if (new Set(xs).size > 1)
|
|
2057
|
+
vs = JSON.stringify(xs);
|
|
2058
|
+
}
|
|
2059
|
+
catch { /* past the models' bounds */ }
|
|
2060
|
+
twinValues.set(id, vs);
|
|
2061
|
+
}
|
|
2062
|
+
if (vs === null)
|
|
2063
|
+
continue;
|
|
2064
|
+
const had = byValue.get(vs);
|
|
2065
|
+
if (!had)
|
|
2066
|
+
byValue.set(vs, t);
|
|
2067
|
+
else if (tid(had) !== id)
|
|
2068
|
+
edge(had, t, undefined, true);
|
|
2069
|
+
}
|
|
2070
|
+
}
|
|
2071
|
+
}
|
|
1964
2072
|
for (let changed = true; changed && !late();) {
|
|
1965
2073
|
changed = false;
|
|
1966
2074
|
for (const [map, list] of cells) {
|
|
@@ -2108,7 +2216,7 @@ function instancesOf(p, fs, w) {
|
|
|
2108
2216
|
for (const model of w.models) {
|
|
2109
2217
|
if (late())
|
|
2110
2218
|
break;
|
|
2111
|
-
const { find, adj, truthOf } = closure(model);
|
|
2219
|
+
const { find, adj, truthOf } = closure(model, true);
|
|
2112
2220
|
const counted = new Set(), explained = new Set();
|
|
2113
2221
|
const explain = (a2, b2) => {
|
|
2114
2222
|
const key = a2 < b2 ? `${a2}|${b2}` : `${b2}|${a2}`;
|
|
@@ -2119,7 +2227,8 @@ function instancesOf(p, fs, w) {
|
|
|
2119
2227
|
// A path through no constant first: two terms the model gives one value aren't equal for that alone (each needs
|
|
2120
2228
|
// its own facts), and a congruence between them is what the proof can use; else one through a constant.
|
|
2121
2229
|
const isConst = (x) => x !== a2 && x !== b2 && x.startsWith('{"op":"const"');
|
|
2122
|
-
|
|
2230
|
+
// (and through no twins, where the facts give a path)
|
|
2231
|
+
for (const [avoid, noTwin] of [[true, true], [false, true], [false, false]]) {
|
|
2123
2232
|
if (back.has(b2))
|
|
2124
2233
|
break;
|
|
2125
2234
|
back.clear();
|
|
@@ -2131,7 +2240,7 @@ function instancesOf(p, fs, w) {
|
|
|
2131
2240
|
if (avoid && isConst(x))
|
|
2132
2241
|
continue;
|
|
2133
2242
|
for (const e of adj.get(x) ?? [])
|
|
2134
|
-
if (!back.has(e.to)) {
|
|
2243
|
+
if (!back.has(e.to) && !(noTwin && e.twin)) {
|
|
2135
2244
|
back.set(e.to, { from: x, cong: e.cong });
|
|
2136
2245
|
queue.push(e.to);
|
|
2137
2246
|
}
|
|
@@ -2280,12 +2389,18 @@ const sid = (t) => {
|
|
|
2280
2389
|
const tid = (t) => sid(t);
|
|
2281
2390
|
// A natural term's linear form (Σ cᵢ·atomᵢ + c, an atom a variable or a read, its text its id), where it is one: a
|
|
2282
2391
|
// truncated difference by a constant only where the constant is at most the term's (x + 1 ∸ 1 is x); else undefined.
|
|
2283
|
-
|
|
2392
|
+
// `at`: a model's values, where a truncated difference whose left side is at least its right there is their difference
|
|
2393
|
+
// (n + m ∸ 1 and n + (m ∸ 1) one form where m ≥ 1: the kernel proves the equality from the case's own conditions).
|
|
2394
|
+
// `known`: a term's constant value, where the facts give it one.
|
|
2395
|
+
function linearForm(e, at, known) {
|
|
2284
2396
|
const go = (t) => {
|
|
2285
2397
|
switch (t.op) {
|
|
2286
2398
|
case 'const': return { cs: new Map(), c: t.value };
|
|
2287
2399
|
case 'var':
|
|
2288
|
-
case 'get':
|
|
2400
|
+
case 'get': {
|
|
2401
|
+
const v = known?.(t);
|
|
2402
|
+
return v !== undefined ? { cs: new Map(), c: v } : { cs: new Map([[tid(t), 1]]), c: 0 };
|
|
2403
|
+
}
|
|
2289
2404
|
case 'add': {
|
|
2290
2405
|
const a = go(t.a), b = go(t.b);
|
|
2291
2406
|
if (!a || !b)
|
|
@@ -2301,7 +2416,17 @@ function linearForm(e) {
|
|
|
2301
2416
|
}
|
|
2302
2417
|
case 'sub': {
|
|
2303
2418
|
const a = go(t.a);
|
|
2304
|
-
|
|
2419
|
+
if (a && t.b.op === 'const' && a.c >= t.b.value)
|
|
2420
|
+
return { cs: a.cs, c: a.c - t.b.value };
|
|
2421
|
+
if (!a || !at)
|
|
2422
|
+
return undefined;
|
|
2423
|
+
const va = at(t.a), vb = at(t.b), b = va !== undefined && vb !== undefined && va >= vb ? go(t.b) : undefined;
|
|
2424
|
+
if (!b)
|
|
2425
|
+
return undefined;
|
|
2426
|
+
const cs = new Map(a.cs);
|
|
2427
|
+
for (const [x, k2] of b.cs)
|
|
2428
|
+
cs.set(x, (cs.get(x) ?? 0) - k2);
|
|
2429
|
+
return { cs, c: a.c - b.c };
|
|
2305
2430
|
}
|
|
2306
2431
|
case 'if':
|
|
2307
2432
|
case 'min':
|
|
@@ -2411,6 +2536,9 @@ function valuesCompared(p, l) {
|
|
|
2411
2536
|
}
|
|
2412
2537
|
// Where a lemma's terms appear in a goal: a substitution of its variables by terms of the goal, for each term of the goal
|
|
2413
2538
|
// that one of the lemma's sides matches (its variables read anywhere, the rest exactly).
|
|
2539
|
+
// A constructor in the lemma matches any value of its datatype too (`fieldMatch`), by its fields' reads (rev (y ++ [z]) at rev (y ++ x),
|
|
2540
|
+
// z the head of x: where x is no such constructor, a case the use's models then refuse).
|
|
2541
|
+
const fieldMatch = { on: false };
|
|
2414
2542
|
function matches(pattern, vars, t, s) {
|
|
2415
2543
|
if (pattern.op === 'var' && vars.has(pattern.name)) {
|
|
2416
2544
|
const had = s[pattern.name];
|
|
@@ -2418,6 +2546,19 @@ function matches(pattern, vars, t, s) {
|
|
|
2418
2546
|
return sid(had) === sid(t) ? s : undefined;
|
|
2419
2547
|
return { ...s, [pattern.name]: t };
|
|
2420
2548
|
}
|
|
2549
|
+
if (fieldMatch.on && pattern.op === 'get' && (t.op !== 'get' || t.map !== pattern.map)) {
|
|
2550
|
+
const at = pattern.map.indexOf('.'), d = at > 0 ? declared.get(pattern.map.slice(0, at)) : undefined, j = d ? d.ctors.findIndex((c, i) => c.fields.length > 0 && ctorMap(d, i) === pattern.map) : -1;
|
|
2551
|
+
if (d && j >= 0) {
|
|
2552
|
+
const fs2 = argsOf(pattern.key, d.ctors[j].fields.length);
|
|
2553
|
+
let out = s;
|
|
2554
|
+
for (const [i, f] of (fs2 ?? []).entries()) {
|
|
2555
|
+
if (!out)
|
|
2556
|
+
break;
|
|
2557
|
+
out = matches(f, vars, get(fieldMap(d, j, i), t), out);
|
|
2558
|
+
}
|
|
2559
|
+
return fs2 ? out : undefined;
|
|
2560
|
+
}
|
|
2561
|
+
}
|
|
2421
2562
|
if (pattern.op !== t.op)
|
|
2422
2563
|
return undefined;
|
|
2423
2564
|
const po = pattern, to = t;
|
|
@@ -2440,7 +2581,8 @@ function matches(pattern, vars, t, s) {
|
|
|
2440
2581
|
return out;
|
|
2441
2582
|
}
|
|
2442
2583
|
function casesOf(p, unfolded, vars, models) {
|
|
2443
|
-
|
|
2584
|
+
// (a read of an unknown function at an abstract value's code, or at its fields, is plain too: p(x) of a filter by p)
|
|
2585
|
+
const plain = (e) => (e.op === 'var' ? vars.has(e.name) : e.op === 'const' ? true : e.op === 'get' ? (p.datas.some(d => e.map.startsWith(`${d.name}.`)) && !p.datas.some(d => d.ctors.some((c, j) => c.fields.length > 0 && e.map === ctorMap(d, j))) || p.params.includes(e.map) || codecs.has(e.map)) && plain(e.key)
|
|
2444
2586
|
: e.op === 'add' || e.op === 'sub' || e.op === 'min' || e.op === 'max' ? plain(e.a) && plain(e.b) : e.op === 'mul' ? plain(e.a) : false);
|
|
2445
2587
|
const plainC = (c) => readsVar(c) && (c.op === '<' || c.op === '<=' || c.op === '=' ? plain(c.a) && plain(c.b) : c.op === 'not' ? plainC(c.x) : c.op === 'and' || c.op === 'or' ? c.xs.every(plainC) : false);
|
|
2446
2588
|
// The functions' facts the goal unfolds.
|
|
@@ -2503,9 +2645,11 @@ function casesOf(p, unfolded, vars, models) {
|
|
|
2503
2645
|
// One attempt: the law's node (and the lemmas', proven before, with their own nodes) at a scheme and a depth. With
|
|
2504
2646
|
// several cases, a main node uses a node per case (each its guards as hypotheses, its own instances), at rank
|
|
2505
2647
|
// (level, size, 1) above theirs (level, size, 0); a case node uses the main node at the children.
|
|
2506
|
-
const dataTrace = !!
|
|
2648
|
+
const dataTrace = !!env.DATA_TRACE;
|
|
2507
2649
|
// Debugging: every congruence a model's closure makes, not only those explanations use.
|
|
2508
|
-
const allCong = !!
|
|
2650
|
+
const allCong = !!env.DATA_ALLCONG;
|
|
2651
|
+
// Debugging: a failed attempt's node whose name the pattern matches, written out (hypotheses, uses, instances).
|
|
2652
|
+
const nodeTrace = env.DATA_NODE;
|
|
2509
2653
|
function attempt(p, fs, l, scheme, depth, lemmas, level, pool) {
|
|
2510
2654
|
const t0 = clock();
|
|
2511
2655
|
anchoredAt.clear();
|
|
@@ -2541,14 +2685,14 @@ function attempt(p, fs, l, scheme, depth, lemmas, level, pool) {
|
|
|
2541
2685
|
// Uses of the lemmas, where a lemma's largest read matches a read of this goal (unfolded), its key up to the
|
|
2542
2686
|
// equalities the unfolded definitions give (any branch); twice over, the uses' own conclusions read the second time.
|
|
2543
2687
|
const pre = instancesOf(p, fs, { roots: [concl], others: ihConcls, depth, shapesAt, etaAt: [], models: [], until: t0 + attemptMs.ms });
|
|
2544
|
-
const lemmaUses = lemmaUsesFor(p, fs, lemmaNodes, [concl, ihConcls], pre, models);
|
|
2688
|
+
const spare = [], lemmaUses = lemmaUsesFor(p, fs, lemmaNodes, [concl, ihConcls], pre, models, spare);
|
|
2545
2689
|
const uses = [...ih, ...lemmaUses];
|
|
2546
2690
|
if (dataTrace)
|
|
2547
2691
|
for (const u of lemmaUses)
|
|
2548
2692
|
console.error(`[data] lemma use ${specs[u.node].name.slice(0, 40)} at ${Object.entries(u.subst).map(([x, t]) => `${x} := ${brief(t, 80)}`).join('; ')}`);
|
|
2549
2693
|
// The lemmas' conclusions are unfolded like the law's (they are what the goal reads, said another way); the cycle's
|
|
2550
2694
|
// own uses only from depth 3.
|
|
2551
|
-
const instsFor = (ms, us, every = false) => instancesOf(p, fs, { roots: [concl], others: us.map(useConcl), depth, shapesAt, etaAt: [...shapesAt, ...valuesCompared(p, mb.law)], models: ms, every, until: t0 + attemptMs.ms });
|
|
2695
|
+
const instsFor = (ms, us, every = false, hyps) => instancesOf(p, fs, { roots: [concl], others: us.map(useConcl), depth, shapesAt, etaAt: [...shapesAt, ...valuesCompared(p, mb.law)], models: ms, every, until: t0 + attemptMs.ms, hyps });
|
|
2552
2696
|
const cases = scheme ? casesOf(p, pre.filter(q => fs.facts[q.fact].name.startsWith('fn ')).map(q => holdsAt(fs, q)), new Set(mb.law.vars.map(v => v.name)), mb.pool) : [];
|
|
2553
2697
|
if (cases.length <= 1) {
|
|
2554
2698
|
// One node: four uses of a lemma, twelve in all, the best first (as lemmaUsesFor ranks them).
|
|
@@ -2572,9 +2716,13 @@ function attempt(p, fs, l, scheme, depth, lemmas, level, pool) {
|
|
|
2572
2716
|
catch {
|
|
2573
2717
|
return false;
|
|
2574
2718
|
} }); };
|
|
2575
|
-
|
|
2719
|
+
// And ranked within the case: of the uses past the node's caps, those whose sides are both the goal's terms (the
|
|
2720
|
+
// step the case's rewriting joins), `perCase` more of a lemma.
|
|
2721
|
+
const fit = [...uses, ...spare].filter(u => holdsIn(u.guard) && applies(u)), kept = capUses(fit.filter(u => uses.includes(u)));
|
|
2722
|
+
const rest = fit.filter(u => u.node >= 0 && !kept.includes(u)), joins = rest.length && c.models ? caseJoins(fs, [concl, ihConcls], pre, ihConcls, few(c.models)) : undefined;
|
|
2723
|
+
const us = [...kept, ...capUses(joins ? rest.filter(u => joins(useConcl(u))) : [], lemmaCaps.joined)];
|
|
2576
2724
|
const ms = few(c.models ?? c.near ?? models);
|
|
2577
|
-
plan.push({ name: `${name}, case ${i + 1}`, hyps: c.lits, concl, sub: 0, insts: reached(p, fs, instsFor(ms, us), [c.lits, concl, us.map(u => [u.guard, useConcl(u)]), shapesAt], ms), uses: us });
|
|
2725
|
+
plan.push({ name: `${name}, case ${i + 1}`, hyps: c.lits, concl, sub: 0, insts: reached(p, fs, instsFor(ms, us, false, c.lits), [c.lits, concl, us.map(u => [u.guard, useConcl(u)]), shapesAt], ms), uses: us });
|
|
2578
2726
|
}
|
|
2579
2727
|
plans.push(plan);
|
|
2580
2728
|
}
|
|
@@ -2717,42 +2865,50 @@ function reached(p, fs, insts, from, ms) {
|
|
|
2717
2865
|
return reach.has(id(get(m, keyOf(q.at.slice(0, n))))) && reach.has(id(get(m, keyOf(q.at.slice(n)))));
|
|
2718
2866
|
});
|
|
2719
2867
|
}
|
|
2720
|
-
//
|
|
2721
|
-
//
|
|
2722
|
-
function
|
|
2723
|
-
const
|
|
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();
|
|
2724
2872
|
const find = (x) => { let r = x; while (parent.has(r))
|
|
2725
2873
|
r = parent.get(r); return r; };
|
|
2726
|
-
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)
|
|
2727
2875
|
parent.set(a, b); };
|
|
2728
|
-
const
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
for (const leaf of leaves(b))
|
|
2732
|
-
union(a, leaf);
|
|
2733
|
-
// Closed under congruence (two reads of one map at arguments of one class), by signatures.
|
|
2734
|
-
const cells = new Map(), classes = new Map();
|
|
2735
|
-
const close = (t) => {
|
|
2736
|
-
walkOnce(t, o => { if (o.op !== 'get')
|
|
2737
|
-
return; const n = fs.arity.get(o.map), as = n ? argsOf(o.key, n) : undefined; if (as)
|
|
2738
|
-
(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 = () => {
|
|
2739
2879
|
for (let changed = true, rounds = 0; changed && rounds < 8; rounds++) {
|
|
2740
2880
|
changed = false;
|
|
2741
2881
|
for (const [map, list] of cells) {
|
|
2742
2882
|
const bySig = new Map();
|
|
2743
2883
|
for (const x of list) {
|
|
2744
|
-
const sig = x.as.map(
|
|
2745
|
-
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)
|
|
2746
2886
|
bySig.set(sig, cx);
|
|
2747
|
-
|
|
2748
|
-
}
|
|
2749
|
-
if (find(id(y)) !== find(id(cx))) {
|
|
2887
|
+
else if (find(tid(y)) !== find(tid(cx))) {
|
|
2750
2888
|
union(y, cx);
|
|
2751
2889
|
changed = true;
|
|
2752
2890
|
}
|
|
2753
2891
|
}
|
|
2754
2892
|
}
|
|
2755
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();
|
|
2756
2912
|
classes.clear();
|
|
2757
2913
|
for (const [key, e] of terms) {
|
|
2758
2914
|
const r = find(key);
|
|
@@ -2780,69 +2936,89 @@ function lemmaUsesFor(p, fs, lemmas, from, insts, models = []) {
|
|
|
2780
2936
|
const callsFn = (t) => { let yes = false; walk(t, o => { if (o.op === 'get' && fs.arity.has(o.map) && p.fns.some(f => f.name === o.map))
|
|
2781
2937
|
yes = true; }); return yes; };
|
|
2782
2938
|
const tops = tops0.some(callsFn) ? tops0.filter(callsFn) : tops0;
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
const
|
|
2792
|
-
|
|
2793
|
-
|
|
2794
|
-
|
|
2795
|
-
|
|
2796
|
-
const
|
|
2797
|
-
if (
|
|
2798
|
-
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
continue;
|
|
2809
|
-
// Each argument of the read matched up to its class (a key of several arguments is their pairing).
|
|
2810
|
-
const args = argsOf(t.key, n);
|
|
2811
|
-
if (!pats || !args)
|
|
2812
|
-
continue;
|
|
2813
|
-
let subs = [{}];
|
|
2814
|
-
for (const q of pats.keys()) {
|
|
2815
|
-
const ms = members(args[q]).slice(0, 256);
|
|
2816
|
-
subs = subs.flatMap(sb => ms.flatMap(m => { const r = matchAt(q, m, sb); return r ? [r] : []; })).slice(0, 512);
|
|
2817
|
-
}
|
|
2818
|
-
for (const sub of subs) {
|
|
2819
|
-
if (Object.keys(sub).length !== vars.size)
|
|
2820
|
-
continue;
|
|
2821
|
-
// Where a model gives the lemma's arguments the goal read's (a match through every branch's classes may be none).
|
|
2822
|
-
const there = pats.map(a => substE(a, { vars: sub, maps: {}, flags: {} }));
|
|
2823
|
-
const score = models.length ? models.filter(({ m, env }) => { try {
|
|
2824
|
-
return there.every((a, q) => m.evalE(a, env) === m.evalE(args[q], env));
|
|
2939
|
+
// (a constructor matched by its fields only for a lemma no read matches as it stands: the uses would multiply)
|
|
2940
|
+
for (const byFields of [false, true]) {
|
|
2941
|
+
if (byFields && cands.some(c => c.use.node === lm.node))
|
|
2942
|
+
break;
|
|
2943
|
+
fieldMatch.on = byFields;
|
|
2944
|
+
for (const top of tops) {
|
|
2945
|
+
const n = fs.arity.get(top.map) ?? 1, pats = argsOf(top.key, n);
|
|
2946
|
+
// A pattern's match at a term alone, once (a match given others' bindings is it, where it agrees with them).
|
|
2947
|
+
const alone = pats?.map(() => new Map());
|
|
2948
|
+
const matchAt = (q, m, sb) => {
|
|
2949
|
+
const memo = alone[q];
|
|
2950
|
+
if (!memo.has(m))
|
|
2951
|
+
memo.set(m, matches(pats[q], vars, m, {}));
|
|
2952
|
+
const r = memo.get(m);
|
|
2953
|
+
if (!r)
|
|
2954
|
+
return undefined;
|
|
2955
|
+
let out = sb;
|
|
2956
|
+
for (const [x, v] of Object.entries(r)) {
|
|
2957
|
+
const had = sb[x];
|
|
2958
|
+
if (had) {
|
|
2959
|
+
if (sid(had) !== sid(v))
|
|
2960
|
+
return undefined;
|
|
2961
|
+
}
|
|
2962
|
+
else
|
|
2963
|
+
out = out === sb ? { ...sb, [x]: v } : (out[x] = v, out);
|
|
2825
2964
|
}
|
|
2826
|
-
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
if (
|
|
2965
|
+
return out;
|
|
2966
|
+
};
|
|
2967
|
+
for (const t of reads) {
|
|
2968
|
+
if (t.op !== 'get' || t.map !== top.map)
|
|
2830
2969
|
continue;
|
|
2831
|
-
|
|
2832
|
-
if (
|
|
2970
|
+
// (one use is all `hits` asks of a lemma)
|
|
2971
|
+
if (hits?.has(lm.node))
|
|
2972
|
+
break;
|
|
2973
|
+
// Each argument of the read matched up to its class (a key of several arguments is their pairing).
|
|
2974
|
+
const args = argsOf(t.key, n);
|
|
2975
|
+
if (!pats || !args)
|
|
2833
2976
|
continue;
|
|
2834
|
-
|
|
2835
|
-
const
|
|
2836
|
-
|
|
2837
|
-
|
|
2977
|
+
let subs = [{}];
|
|
2978
|
+
for (const q of pats.keys()) {
|
|
2979
|
+
const ms = members(args[q]).slice(0, 256);
|
|
2980
|
+
subs = subs.flatMap(sb => ms.flatMap(m => { const r = matchAt(q, m, sb); return r ? [r] : []; })).slice(0, 512);
|
|
2981
|
+
}
|
|
2982
|
+
for (const sub of subs) {
|
|
2983
|
+
if (Object.keys(sub).length !== vars.size)
|
|
2984
|
+
continue;
|
|
2985
|
+
// Where a model gives the lemma's arguments the goal read's (a match through every branch's classes may be none).
|
|
2986
|
+
const there = pats.map(a => substE(a, { vars: sub, maps: {}, flags: {} }));
|
|
2987
|
+
const score = models.length ? models.filter(({ m, env }) => { try {
|
|
2988
|
+
return there.every((a, q) => m.evalE(a, env) === m.evalE(args[q], env));
|
|
2838
2989
|
}
|
|
2839
2990
|
catch {
|
|
2840
|
-
return
|
|
2841
|
-
} })
|
|
2842
|
-
|
|
2991
|
+
return false;
|
|
2992
|
+
} }).length : 1;
|
|
2993
|
+
if (!score)
|
|
2994
|
+
continue;
|
|
2995
|
+
const k2 = sid([lm.node, sub]);
|
|
2996
|
+
if (seen.has(k2))
|
|
2997
|
+
continue;
|
|
2998
|
+
seen.add(k2);
|
|
2999
|
+
const names = Object.keys(sub).sort();
|
|
3000
|
+
const fp = models.length ? JSON.stringify([lm.node, models.map(({ m, env }) => names.map(x => { try {
|
|
3001
|
+
return m.evalE(sub[x], env);
|
|
3002
|
+
}
|
|
3003
|
+
catch {
|
|
3004
|
+
return -1;
|
|
3005
|
+
} }))]) : k2;
|
|
3006
|
+
const use = { node: lm.node, subst: sub, guard: yesC };
|
|
3007
|
+
if (hits) {
|
|
3008
|
+
hits.add(lm.node);
|
|
3009
|
+
break;
|
|
3010
|
+
}
|
|
3011
|
+
cands.push({ use, at: { goal: t, lemma: substE(top, { vars: sub, maps: {}, flags: {} }) }, score, fp, size: textLength([lm.node, sub]) });
|
|
3012
|
+
}
|
|
2843
3013
|
}
|
|
2844
3014
|
}
|
|
2845
3015
|
}
|
|
3016
|
+
fieldMatch.on = false;
|
|
3017
|
+
}
|
|
3018
|
+
if (hits) {
|
|
3019
|
+
for (const c of cands)
|
|
3020
|
+
hits.add(c.use.node);
|
|
3021
|
+
return [];
|
|
2846
3022
|
}
|
|
2847
3023
|
// First, as found: four of a lemma, twelve in all (the next round reads their conclusions). Then the rest, best first
|
|
2848
3024
|
// (the most models, then the smallest terms), one of each set of values, up to `perLemma` of a lemma and `total` in
|
|
@@ -2858,9 +3034,18 @@ function lemmaUsesFor(p, fs, lemmas, from, insts, models = []) {
|
|
|
2858
3034
|
fresh.push(substC(lemmas.find(lm => lm.node === c.use.node).concl, { vars: c.use.subst, maps: {}, flags: {} }));
|
|
2859
3035
|
}
|
|
2860
3036
|
const rest = cands.filter(c => !first.has(c)).sort((a, b) => b.score - a.score || a.size - b.size);
|
|
2861
|
-
for (const c of rest)
|
|
2862
|
-
if (
|
|
3037
|
+
for (const c of rest) {
|
|
3038
|
+
if (fps.has(c.fp))
|
|
3039
|
+
continue;
|
|
3040
|
+
if (out.length < lemmaCaps.total && out.filter(u => u.node === c.use.node).length < lemmaCaps.perLemma)
|
|
2863
3041
|
take(c);
|
|
3042
|
+
// (past the node's caps, kept for the cases: one case's models may meet a use the node's rank puts below them)
|
|
3043
|
+
else if (spare && spare.length < lemmaCaps.spare) {
|
|
3044
|
+
fps.add(c.fp);
|
|
3045
|
+
spare.push(c.use);
|
|
3046
|
+
matchedAt.set(c.use, c.at);
|
|
3047
|
+
}
|
|
3048
|
+
}
|
|
2864
3049
|
// The second round: the reads of the first's conclusions, and the goal's again, matched up to the classes those
|
|
2865
3050
|
// conclusions join (assoc's use makes x ++ (y ++ [z]) and (x ++ y) ++ [z] one, where rev of a snoc then matches).
|
|
2866
3051
|
for (const c of fresh)
|
|
@@ -2873,8 +3058,9 @@ function lemmaUsesFor(p, fs, lemmas, from, insts, models = []) {
|
|
|
2873
3058
|
return out;
|
|
2874
3059
|
}
|
|
2875
3060
|
// How many uses of lemmas a goal gets: the candidates kept (`perLemma` of each lemma, `total`), and of those a case node
|
|
2876
|
-
// keeps (`perCase` of each lemma, those its models meet)
|
|
2877
|
-
|
|
3061
|
+
// keeps (`perCase` of each lemma, those its models meet); past the node's, `spare` kept for its cases, of which a case
|
|
3062
|
+
// takes `joined` more (those its own rewriting passes through).
|
|
3063
|
+
export const lemmaCaps = { perLemma: 8, total: 24, perCase: 4, spare: 96, joined: 2 };
|
|
2878
3064
|
// A node's uses: the scheme's (the cycle's, read at nodes not yet placed) all, `perCase` of each lemma, `total` lemma
|
|
2879
3065
|
// uses in all, in the order given.
|
|
2880
3066
|
function capUses(uses, total = Infinity) {
|
|
@@ -2902,6 +3088,85 @@ function equalities(c) {
|
|
|
2902
3088
|
}
|
|
2903
3089
|
// A lemma use: the goal's read it matched, and the lemma's read there (equal where the use applies).
|
|
2904
3090
|
const matchedAt = new WeakMap();
|
|
3091
|
+
// A case's terms up to the equalities its models take (an if's branch, an implication's conclusion, where all of them
|
|
3092
|
+
// take it; the induction's hypotheses too), closed under congruence: `joins(c)`, whether each side of each equality of
|
|
3093
|
+
// c is one of those terms' classes (a read whose arguments' classes some read of its map has). A lemma use that joins
|
|
3094
|
+
// is a step the case's own rewriting passes through, from one of its terms to another (ripple's fertilisation), where
|
|
3095
|
+
// a use that only matches one of its terms is one its models alone may agree with (app (t, []) for t).
|
|
3096
|
+
function caseJoins(fs, from, insts, hyps, ms) {
|
|
3097
|
+
const { find, union, cell, cells, congruent } = congruence(fs);
|
|
3098
|
+
const truth = (c) => { let v; for (const { m, env } of ms) {
|
|
3099
|
+
let t;
|
|
3100
|
+
try {
|
|
3101
|
+
t = m.evalC(c, env);
|
|
3102
|
+
}
|
|
3103
|
+
catch {
|
|
3104
|
+
return undefined;
|
|
3105
|
+
}
|
|
3106
|
+
if (v === undefined)
|
|
3107
|
+
v = t;
|
|
3108
|
+
else if (v !== t)
|
|
3109
|
+
return undefined;
|
|
3110
|
+
} return v; };
|
|
3111
|
+
const leaf = (e) => { if (e.op !== 'if')
|
|
3112
|
+
return e; const t = truth(e.c); return t === undefined ? undefined : leaf(t ? e.then : e.else); };
|
|
3113
|
+
const eqs = (c) => {
|
|
3114
|
+
if (c.op === '=') {
|
|
3115
|
+
const b = leaf(c.b);
|
|
3116
|
+
if (b)
|
|
3117
|
+
union(c.a, b);
|
|
3118
|
+
return;
|
|
3119
|
+
}
|
|
3120
|
+
if (c.op === 'and') {
|
|
3121
|
+
c.xs.forEach(eqs);
|
|
3122
|
+
return;
|
|
3123
|
+
}
|
|
3124
|
+
if (c.op === 'or') {
|
|
3125
|
+
const open = c.xs.filter(x => truth(x) !== false);
|
|
3126
|
+
if (open.length === 1)
|
|
3127
|
+
eqs(open[0]);
|
|
3128
|
+
}
|
|
3129
|
+
};
|
|
3130
|
+
for (const q of insts)
|
|
3131
|
+
eqs(holdsAt(fs, q));
|
|
3132
|
+
for (const h of hyps)
|
|
3133
|
+
eqs(h);
|
|
3134
|
+
const seen = new Set();
|
|
3135
|
+
walkOnce([from, insts.map(q => holdsAt(fs, q)), hyps], o => { if (o.op !== 'get')
|
|
3136
|
+
return; seen.add(tid(o)); cell(o); });
|
|
3137
|
+
congruent();
|
|
3138
|
+
const bySig = new Map();
|
|
3139
|
+
for (const [map, list] of cells)
|
|
3140
|
+
for (const x of list)
|
|
3141
|
+
bySig.set(`${map}\u0002${x.as.map(t => find(tid(t))).join('\u0001')}`, find(tid(get(map, x.key))));
|
|
3142
|
+
// (once per object: a key's pairing reads its arguments again, so a term's objects are shared deep down)
|
|
3143
|
+
const memo = new WeakMap();
|
|
3144
|
+
const classOf = (t) => {
|
|
3145
|
+
if (t.op !== 'get')
|
|
3146
|
+
return find(tid(t));
|
|
3147
|
+
const had = memo.get(t);
|
|
3148
|
+
if (had !== undefined)
|
|
3149
|
+
return had ?? undefined;
|
|
3150
|
+
const n = fs.arity.get(t.map), as = n ? argsOf(t.key, n) : undefined;
|
|
3151
|
+
let out;
|
|
3152
|
+
if (!as)
|
|
3153
|
+
out = seen.has(tid(t)) ? find(tid(t)) : undefined;
|
|
3154
|
+
else {
|
|
3155
|
+
const cs = [];
|
|
3156
|
+
for (const a of as) {
|
|
3157
|
+
const c = classOf(a);
|
|
3158
|
+
if (c === undefined)
|
|
3159
|
+
break;
|
|
3160
|
+
cs.push(c);
|
|
3161
|
+
}
|
|
3162
|
+
out = cs.length < as.length ? undefined : bySig.get(`${t.map}\u0002${cs.join('\u0001')}`);
|
|
3163
|
+
}
|
|
3164
|
+
memo.set(t, out ?? null);
|
|
3165
|
+
return out;
|
|
3166
|
+
};
|
|
3167
|
+
const all = (c) => (c.op === '=' ? [[c.a, c.b]] : c.op === 'and' ? c.xs.flatMap(all) : []);
|
|
3168
|
+
return c => { const es = all(c); return es.length > 0 && es.every(([a, b]) => { const lb = leaf(b); return classOf(a) !== undefined && lb !== undefined && classOf(lb) !== undefined; }); };
|
|
3169
|
+
}
|
|
2905
3170
|
const holdsAt = (fs, q) => { const f = fs.facts[q.fact]; return substShared(f.holds, Object.fromEntries(f.keys.map((key, i) => [key, q.at[i]]))); };
|
|
2906
3171
|
// The models a search compares terms in: random values of the law's variables.
|
|
2907
3172
|
function modelsFor(p, l, n, seed) {
|
|
@@ -2918,7 +3183,8 @@ const settled = new WeakMap();
|
|
|
2918
3183
|
// `without`: 'plain', the law with no induction where no scheme applies; 'cases', no induction, then by the cases of
|
|
2919
3184
|
// each variable (a natural's zero and successor, a datatype's constructors), none of them using the law again; 'first',
|
|
2920
3185
|
// no induction before the schemes (a law that follows from its lemmas by rewriting).
|
|
2921
|
-
|
|
3186
|
+
// `merge`: the merged schemes too (schemes, below), at one unfolding: the law's first attempts only.
|
|
3187
|
+
function proveWith(p, fs, l, lemmas, level, opts, until, per, depths, without, merge = false) {
|
|
2922
3188
|
const models = modelsFor(p, l, 240, opts.seed ?? 3);
|
|
2923
3189
|
let tried = 0, last;
|
|
2924
3190
|
const cases = l.vars.flatMap((v) => (v.sort === 'nat' || v.sort instanceof Data ? [{ name: `cases of ${v.name}`, on: { v: v.name, d: v.sort }, uses: [] }] : []));
|
|
@@ -2929,13 +3195,25 @@ function proveWith(p, fs, l, lemmas, level, opts, until, per, depths, without) {
|
|
|
2929
3195
|
for (const s of ss) {
|
|
2930
3196
|
if (clock() > until)
|
|
2931
3197
|
return { tried, last };
|
|
3198
|
+
// (a merged scheme at one unfolding only, in the law's first attempts: its cases are each scheme's at once, many at
|
|
3199
|
+
// two, and every proof by one found so far needed no lemma)
|
|
3200
|
+
if (s?.merged && (depth > 1 || !merge))
|
|
3201
|
+
continue;
|
|
2932
3202
|
const key = JSON.stringify([l.name, s?.name, depth, level, lemmas.map(q => q.spec.name), opts.seed]);
|
|
2933
3203
|
if (done.has(key))
|
|
2934
3204
|
continue;
|
|
2935
3205
|
tried++;
|
|
2936
3206
|
const t1 = clock();
|
|
2937
|
-
|
|
3207
|
+
// (and within a third of an attempt's time: a merged scheme's proof, where there is one, is short)
|
|
3208
|
+
const { verdict, specs, main } = (attemptMs.ms = Math.min(per * (opts.patience ?? 1) / (s?.merged ? 3 : 1), until - clock()), attempt(p, fs, l, s, depth, lemmas, level, models));
|
|
2938
3209
|
last = verdict;
|
|
3210
|
+
if (nodeTrace && !verdict.proven && new RegExp(nodeTrace).test(verdict.goal)) {
|
|
3211
|
+
const n = specs.find(x => x.name === verdict.goal);
|
|
3212
|
+
if (n)
|
|
3213
|
+
console.error(`[node] ${n.name}: ${verdict.stuck}\n hyps: ${n.hyps.map(showC).join(' ; ')}\n concl: ${showC(n.concl)}\n uses:\n ${n.uses.map(u => `${specs[u.node]?.name.slice(0, 40)}: ${showC(substC(specs[u.node].concl, { vars: u.subst, maps: {}, flags: {} }))} if ${showC(u.guard)}`).join('\n ')}\n instances:\n ${n.insts.map(q => `${fs.facts[q.fact].name}: ${showC(holdsAt(fs, q))}`).join('\n ')}`);
|
|
3214
|
+
}
|
|
3215
|
+
if (dataTrace)
|
|
3216
|
+
console.error(`[data] -> ${verdict.proven ? (verdict.checked ? 'proven' : `not checked: ${verdict.why}`) : `${verdict.goal}: ${verdict.stuck}`}`);
|
|
2939
3217
|
if (!(verdict.proven && verdict.checked) && clock() <= t1 + attemptMs.ms)
|
|
2940
3218
|
done.add(key);
|
|
2941
3219
|
if (verdict.proven && verdict.checked)
|
|
@@ -3074,33 +3352,6 @@ export function conjectures(p, l, max = 24, maxSize = 6) {
|
|
|
3074
3352
|
vs.add(o.name); }); return vs.size; };
|
|
3075
3353
|
eqs.sort((a, b) => a.size - b.size || nvars(b) - nvars(a));
|
|
3076
3354
|
const rules = [];
|
|
3077
|
-
const matchX = (pat, t, vs, sub) => {
|
|
3078
|
-
if (pat.op === 'var' && vs.has(pat.name)) {
|
|
3079
|
-
const had = sub.get(pat.name);
|
|
3080
|
-
if (had)
|
|
3081
|
-
return JSON.stringify(had) === JSON.stringify(t);
|
|
3082
|
-
sub.set(pat.name, t);
|
|
3083
|
-
return true;
|
|
3084
|
-
}
|
|
3085
|
-
if (pat.op !== t.op)
|
|
3086
|
-
return false;
|
|
3087
|
-
const po = pat, to = t;
|
|
3088
|
-
for (const key of Object.keys(po)) {
|
|
3089
|
-
const a2 = po[key], b2 = to[key];
|
|
3090
|
-
if (Array.isArray(a2)) {
|
|
3091
|
-
if (!Array.isArray(b2) || a2.length !== b2.length || !a2.every((x, i) => matchX(x, b2[i], vs, sub)))
|
|
3092
|
-
return false;
|
|
3093
|
-
}
|
|
3094
|
-
else if (a2 && typeof a2 === 'object') {
|
|
3095
|
-
if (!b2 || typeof b2 !== 'object' || !matchX(a2, b2, vs, sub))
|
|
3096
|
-
return false;
|
|
3097
|
-
}
|
|
3098
|
-
else if (a2 !== b2)
|
|
3099
|
-
return false;
|
|
3100
|
-
}
|
|
3101
|
-
return true;
|
|
3102
|
-
};
|
|
3103
|
-
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));
|
|
3104
3355
|
const norm = (x, depth = 0) => {
|
|
3105
3356
|
if (depth > 30)
|
|
3106
3357
|
return x;
|
|
@@ -3118,7 +3369,7 @@ export function conjectures(p, l, max = 24, maxSize = 6) {
|
|
|
3118
3369
|
for (const r of rules) {
|
|
3119
3370
|
const sub = new Map();
|
|
3120
3371
|
if (matchX(r.l, t, r.vs, sub))
|
|
3121
|
-
return norm(
|
|
3372
|
+
return norm(substXs(r.r, sub), depth + 1);
|
|
3122
3373
|
}
|
|
3123
3374
|
return t;
|
|
3124
3375
|
};
|
|
@@ -3275,6 +3526,422 @@ export function conjectures(p, l, max = 24, maxSize = 6) {
|
|
|
3275
3526
|
}
|
|
3276
3527
|
return out;
|
|
3277
3528
|
}
|
|
3529
|
+
// Lemma calculation (Zeno's, HipSpec's): the step of an induction a law's definitions don't close, said as a lemma.
|
|
3530
|
+
// For each variable a scheme inducts on (a datatype's or a natural's) and each of its recursive constructors, the law
|
|
3531
|
+
// at that constructor of fresh fields, its definitions unfolded where they are decided (`reduceX`), the hypothesis (the
|
|
3532
|
+
// law at each recursive field) used as a rewrite where its side appears (either way, on either side); what is left
|
|
3533
|
+
// unequal is the step's goal, and with its subterms common to both sides each a fresh variable (generalised, as the
|
|
3534
|
+
// step's terms stand for any value of their sort), a conjecture. Those every random value meets are kept, smallest
|
|
3535
|
+
// first; each one's own step is calculated again (`depth`), the deeper ones first: a lemma's proof may need the next
|
|
3536
|
+
// (bind over an append needs ++ associative).
|
|
3537
|
+
export function calculated(p, l, depth = 2, max = 6) {
|
|
3538
|
+
const out = [], seen = new Set([canonText(l.holds, l.vars)]);
|
|
3539
|
+
const go = (law, d) => {
|
|
3540
|
+
if (d <= 0 || out.length >= 3 * max)
|
|
3541
|
+
return;
|
|
3542
|
+
for (const c of stepsOf(p, law)) {
|
|
3543
|
+
// (an equation and its mirror one)
|
|
3544
|
+
const text = canonText(c.holds, c.vars), y = c.holds, mirror = canonText({ op: '=', a: y.b, b: y.a }, c.vars);
|
|
3545
|
+
if (seen.has(text) || seen.has(mirror))
|
|
3546
|
+
continue;
|
|
3547
|
+
seen.add(text);
|
|
3548
|
+
seen.add(mirror);
|
|
3549
|
+
if (refute(c, 200))
|
|
3550
|
+
continue;
|
|
3551
|
+
if (general.has(c))
|
|
3552
|
+
go(c, d - 1);
|
|
3553
|
+
out.push(c);
|
|
3554
|
+
}
|
|
3555
|
+
};
|
|
3556
|
+
go(l, depth);
|
|
3557
|
+
// (the generalised steps first, in their order, then the base cases (a step's proof often needs one: nil's right
|
|
3558
|
+
// identity under length's), then the steps as they stand)
|
|
3559
|
+
return [...out.filter(c => general.has(c)), ...out.filter(c => based.has(c)), ...out.filter(c => !general.has(c) && !based.has(c))].slice(0, max);
|
|
3560
|
+
}
|
|
3561
|
+
// A law's text with its variables named by sort in order of appearance (two laws alike up to their names, one text).
|
|
3562
|
+
function canonText(y, vars) {
|
|
3563
|
+
const count = new Map(), ren = new Map();
|
|
3564
|
+
walk(y, o => { if (o.op === 'var' && !ren.has(o.name)) {
|
|
3565
|
+
const v = vars.find(w => w.name === o.name);
|
|
3566
|
+
if (!v)
|
|
3567
|
+
return;
|
|
3568
|
+
const key = v.sort instanceof Data ? v.sort.name : String(v.sort), i = (count.get(key) ?? 0) + 1;
|
|
3569
|
+
count.set(key, i);
|
|
3570
|
+
ren.set(v.name, `${key}#${i}`);
|
|
3571
|
+
} });
|
|
3572
|
+
return JSON.stringify(y, (_, v) => (v && typeof v === 'object' && v.op === 'var' && ren.has(v.name) ? { op: 'var', name: ren.get(v.name) } : v));
|
|
3573
|
+
}
|
|
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);
|
|
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
|
+
};
|
|
3595
|
+
// A term with its definitions unfolded where they are decided: a call whose body, its parameters given, decides its
|
|
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
|
|
3597
|
+
// call stays); a match of a constructor, a natmatch of a constant or of a successor (x + k), an if whose condition the
|
|
3598
|
+
// terms decide, is its case; one undecided inside a term unfolded stays as it is (a non-recursive definition unfolded
|
|
3599
|
+
// into another's, say); sums with their constants gathered. Within `fuel` unfoldings (a definition's recursion on a
|
|
3600
|
+
// value not built stays a call).
|
|
3601
|
+
export function reduceX(x, fuel = { n: 400 }) {
|
|
3602
|
+
// (`top`: the term is a body's choice, where an undecided one fails the unfolding)
|
|
3603
|
+
const red = (t, top = false) => {
|
|
3604
|
+
switch (t.op) {
|
|
3605
|
+
case 'var':
|
|
3606
|
+
case 'nat':
|
|
3607
|
+
case 'int': return t;
|
|
3608
|
+
case 'call': {
|
|
3609
|
+
const args = t.args.map(a => red(a));
|
|
3610
|
+
if (args.some(a => !a))
|
|
3611
|
+
return undefined;
|
|
3612
|
+
const call = { op: 'call', fn: t.fn, args: args };
|
|
3613
|
+
const f = fnNamed(t.fn);
|
|
3614
|
+
if (!f || fuel.n <= 0)
|
|
3615
|
+
return call;
|
|
3616
|
+
fuel.n--;
|
|
3617
|
+
const was = fuel.n;
|
|
3618
|
+
const body = red(substXs(f.body(), new Map(f.params.map((q, i) => [`${f.name}.${q.name}`, args[i]]))), true);
|
|
3619
|
+
if (!body) {
|
|
3620
|
+
fuel.n = was;
|
|
3621
|
+
return call;
|
|
3622
|
+
}
|
|
3623
|
+
return body;
|
|
3624
|
+
}
|
|
3625
|
+
case 'ctor': {
|
|
3626
|
+
const args = t.args.map(a => red(a));
|
|
3627
|
+
return args.some(a => !a) ? undefined : { ...t, args: args };
|
|
3628
|
+
}
|
|
3629
|
+
case 'match': {
|
|
3630
|
+
const of = red(t.of);
|
|
3631
|
+
if (!of)
|
|
3632
|
+
return undefined;
|
|
3633
|
+
if (of.op !== 'ctor')
|
|
3634
|
+
return top ? undefined : alpha({ ...t, of });
|
|
3635
|
+
const c = t.cases[of.ctor];
|
|
3636
|
+
return red(substXs(c.body, new Map(c.binds.map((b, i) => [b, of.args[i]]))), top);
|
|
3637
|
+
}
|
|
3638
|
+
case 'natmatch': {
|
|
3639
|
+
const of = red(t.of);
|
|
3640
|
+
if (!of)
|
|
3641
|
+
return undefined;
|
|
3642
|
+
if (of.op === 'nat')
|
|
3643
|
+
return of.value === 0 ? red(t.zero, top) : red(substXs(t.succ, new Map([[t.pred, { op: 'nat', value: of.value - 1 }]])), top);
|
|
3644
|
+
if (of.op === 'add' && of.b.op === 'nat' && of.b.value >= 1)
|
|
3645
|
+
return red(substXs(t.succ, new Map([[t.pred, of.b.value === 1 ? of.a : { op: 'add', a: of.a, b: { op: 'nat', value: of.b.value - 1 } }]])), top);
|
|
3646
|
+
return top ? undefined : alpha({ ...t, of });
|
|
3647
|
+
}
|
|
3648
|
+
case 'if': {
|
|
3649
|
+
const c = decide(t.c);
|
|
3650
|
+
return c === undefined ? (top ? undefined : alpha(t)) : red(c ? t.then : t.else, top);
|
|
3651
|
+
}
|
|
3652
|
+
case 'bool': {
|
|
3653
|
+
const c = decide(t.c);
|
|
3654
|
+
return c === undefined ? t : { op: 'bool', c: c ? { op: '<=', a: { op: 'nat', value: 0 }, b: { op: 'nat', value: 0 } } : { op: '<', a: { op: 'nat', value: 0 }, b: { op: 'nat', value: 0 } } };
|
|
3655
|
+
}
|
|
3656
|
+
case 'add': {
|
|
3657
|
+
const a = red(t.a), b = red(t.b);
|
|
3658
|
+
if (!a || !b)
|
|
3659
|
+
return undefined;
|
|
3660
|
+
if (a.op === 'nat' && b.op === 'nat')
|
|
3661
|
+
return { op: 'nat', value: a.value + b.value };
|
|
3662
|
+
if (a.op === 'nat' && a.value === 0)
|
|
3663
|
+
return b;
|
|
3664
|
+
if (b.op === 'nat' && b.value === 0)
|
|
3665
|
+
return a;
|
|
3666
|
+
// (constants last: (x + 1) + 1 is x + 2, 1 + x is x + 1)
|
|
3667
|
+
if (a.op === 'nat')
|
|
3668
|
+
return b.op === 'add' && b.b.op === 'nat' ? { op: 'add', a: b.a, b: { op: 'nat', value: b.b.value + a.value } } : { op: 'add', a: b, b: a };
|
|
3669
|
+
if (b.op === 'nat' && a.op === 'add' && a.b.op === 'nat')
|
|
3670
|
+
return { op: 'add', a: a.a, b: { op: 'nat', value: a.b.value + b.value } };
|
|
3671
|
+
return { op: 'add', a, b };
|
|
3672
|
+
}
|
|
3673
|
+
case 'sub':
|
|
3674
|
+
case 'min':
|
|
3675
|
+
case 'max':
|
|
3676
|
+
case 'prod':
|
|
3677
|
+
case 'iadd':
|
|
3678
|
+
case 'isub': {
|
|
3679
|
+
const a = red(t.a), b = red(t.b);
|
|
3680
|
+
if (!a || !b)
|
|
3681
|
+
return undefined;
|
|
3682
|
+
// (x + k ∸ j: a successor's predecessor)
|
|
3683
|
+
if (t.op === 'sub' && b.op === 'nat' && a.op === 'add' && a.b.op === 'nat' && a.b.value >= b.value)
|
|
3684
|
+
return a.b.value === b.value ? a.a : { op: 'add', a: a.a, b: { op: 'nat', value: a.b.value - b.value } };
|
|
3685
|
+
if (t.op === 'sub' && a.op === 'nat' && b.op === 'nat')
|
|
3686
|
+
return { op: 'nat', value: Math.max(0, a.value - b.value) };
|
|
3687
|
+
return { ...t, a, b };
|
|
3688
|
+
}
|
|
3689
|
+
case 'mul':
|
|
3690
|
+
case 'mod':
|
|
3691
|
+
case 'div':
|
|
3692
|
+
case 'ineg':
|
|
3693
|
+
case 'imul':
|
|
3694
|
+
case 'ofnat':
|
|
3695
|
+
case 'imod':
|
|
3696
|
+
case 'idiv': {
|
|
3697
|
+
const a = red(t.a);
|
|
3698
|
+
return a ? { ...t, a } : undefined;
|
|
3699
|
+
}
|
|
3700
|
+
case 'apply': {
|
|
3701
|
+
const arg = red(t.arg);
|
|
3702
|
+
return arg ? { ...t, arg } : undefined;
|
|
3703
|
+
}
|
|
3704
|
+
case 'size': {
|
|
3705
|
+
const of = red(t.of);
|
|
3706
|
+
return of ? { ...t, of } : undefined;
|
|
3707
|
+
}
|
|
3708
|
+
}
|
|
3709
|
+
};
|
|
3710
|
+
// An undecided choice left in a term, its binders named by their depth (two copies of one definition unfolded with
|
|
3711
|
+
// fresh names are then one term).
|
|
3712
|
+
const alpha = (t, level = 0) => {
|
|
3713
|
+
const go = (x, lv) => {
|
|
3714
|
+
if (!x || typeof x !== 'object')
|
|
3715
|
+
return x;
|
|
3716
|
+
if (Array.isArray(x))
|
|
3717
|
+
return x.map(y => go(y, lv));
|
|
3718
|
+
const o = x;
|
|
3719
|
+
if (o.op === 'match')
|
|
3720
|
+
return { ...o, of: go(o.of, lv), cases: o.cases.map(c => { const names = c.binds.map((_, i) => `$${lv}.${i}`); return { binds: names, body: go(substXs(c.body, new Map(c.binds.map((b, i) => [b, { op: 'var', name: names[i] }]))), lv + 1) }; }) };
|
|
3721
|
+
if (o.op === 'natmatch') {
|
|
3722
|
+
const name = `$${lv}.0`;
|
|
3723
|
+
return { ...o, of: go(o.of, lv), zero: go(o.zero, lv), pred: name, succ: go(substXs(o.succ, new Map([[o.pred, { op: 'var', name }]])), lv + 1) };
|
|
3724
|
+
}
|
|
3725
|
+
return Object.fromEntries(Object.entries(o).map(([key, v]) => [key, go(v, lv)]));
|
|
3726
|
+
};
|
|
3727
|
+
return go(t, level);
|
|
3728
|
+
};
|
|
3729
|
+
// A condition the terms decide: constructors told apart or alike field by field, constants compared, a test of a
|
|
3730
|
+
// constructor, a condition's value (bool) read back.
|
|
3731
|
+
const decide = (c) => {
|
|
3732
|
+
switch (c.op) {
|
|
3733
|
+
case 'and': {
|
|
3734
|
+
let all = true;
|
|
3735
|
+
for (const y of c.xs) {
|
|
3736
|
+
const v = decide(y);
|
|
3737
|
+
if (v === false)
|
|
3738
|
+
return false;
|
|
3739
|
+
if (v === undefined)
|
|
3740
|
+
all = undefined;
|
|
3741
|
+
}
|
|
3742
|
+
return all;
|
|
3743
|
+
}
|
|
3744
|
+
case 'or': {
|
|
3745
|
+
let any = false;
|
|
3746
|
+
for (const y of c.xs) {
|
|
3747
|
+
const v = decide(y);
|
|
3748
|
+
if (v === true)
|
|
3749
|
+
return true;
|
|
3750
|
+
if (v === undefined)
|
|
3751
|
+
any = undefined;
|
|
3752
|
+
}
|
|
3753
|
+
return any;
|
|
3754
|
+
}
|
|
3755
|
+
case 'not': {
|
|
3756
|
+
const v = decide(c.x);
|
|
3757
|
+
return v === undefined ? undefined : !v;
|
|
3758
|
+
}
|
|
3759
|
+
case 'is': {
|
|
3760
|
+
const of = red(c.of);
|
|
3761
|
+
return of && of.op === 'ctor' ? of.ctor === c.ctor : undefined;
|
|
3762
|
+
}
|
|
3763
|
+
case 'holds': {
|
|
3764
|
+
const x = red(c.x);
|
|
3765
|
+
return x && x.op === 'bool' ? decide(x.c) : undefined;
|
|
3766
|
+
}
|
|
3767
|
+
case '=':
|
|
3768
|
+
case '<':
|
|
3769
|
+
case '<=': {
|
|
3770
|
+
const a = red(c.a), b = red(c.b);
|
|
3771
|
+
if (!a || !b)
|
|
3772
|
+
return undefined;
|
|
3773
|
+
if (a.op === 'nat' && b.op === 'nat')
|
|
3774
|
+
return c.op === '=' ? a.value === b.value : c.op === '<' ? a.value < b.value : a.value <= b.value;
|
|
3775
|
+
if (c.op === '=' && sameX(a, b))
|
|
3776
|
+
return true;
|
|
3777
|
+
if (c.op === '=' && a.op === 'ctor' && b.op === 'ctor')
|
|
3778
|
+
return a.ctor !== b.ctor ? false : a.args.every((x, i) => sameX(x, b.args[i])) ? true : undefined;
|
|
3779
|
+
return undefined;
|
|
3780
|
+
}
|
|
3781
|
+
case 'i<':
|
|
3782
|
+
case 'i<=': return undefined;
|
|
3783
|
+
}
|
|
3784
|
+
};
|
|
3785
|
+
return red(x) ?? x;
|
|
3786
|
+
}
|
|
3787
|
+
// The steps a law's inductions leave, each as a conjecture (lemma calculation, above).
|
|
3788
|
+
const general = new WeakSet(), based = new WeakSet();
|
|
3789
|
+
function stepsOf(p, l) {
|
|
3790
|
+
if (l.holds.op !== '=')
|
|
3791
|
+
return [];
|
|
3792
|
+
const eq = l.holds, out = [];
|
|
3793
|
+
const sorts = new Map(l.vars.map(v => [v.name, v.sort]));
|
|
3794
|
+
const sortOf = (x) => (x.op === 'var' ? sorts.get(x.name) : x.op === 'call' ? fnNamed(x.fn)?.ret : x.op === 'ctor' ? dataNamed(x.data) : x.op === 'nat' || x.op === 'add' ? 'nat'
|
|
3795
|
+
: x.op === 'match' ? x.cases.map(c => sortOf(c.body)).find(s => s) : x.op === 'natmatch' ? sortOf(x.zero) ?? sortOf(x.succ) : x.op === 'if' ? sortOf(x.then) ?? sortOf(x.else) : undefined);
|
|
3796
|
+
let fresh = 0;
|
|
3797
|
+
const freshVar = (s) => { const name = `${s instanceof Data ? s.name.toLowerCase() : 'n'}_c${++fresh}`; sorts.set(name, s); return { op: 'var', name }; };
|
|
3798
|
+
const has = (x, t) => JSON.stringify(x).includes(JSON.stringify(t));
|
|
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)); };
|
|
3800
|
+
// The hypothesis at any values of the law's other variables (`free`): from's instances rewritten to to's, outermost
|
|
3801
|
+
// first (qrev (x, a :: y) to rev x ++ (a :: y), the hypothesis read at y := a :: y). Whether one was.
|
|
3802
|
+
const rewriteP = (x, from, to, free) => {
|
|
3803
|
+
let hit = false;
|
|
3804
|
+
const go = (t) => {
|
|
3805
|
+
if (!t || typeof t !== 'object')
|
|
3806
|
+
return t;
|
|
3807
|
+
if (Array.isArray(t))
|
|
3808
|
+
return t.map(go);
|
|
3809
|
+
const sub = new Map();
|
|
3810
|
+
if (t.op && matchX(from, t, free, sub)) {
|
|
3811
|
+
hit = true;
|
|
3812
|
+
return substXs(to, sub);
|
|
3813
|
+
}
|
|
3814
|
+
return Object.fromEntries(Object.entries(t).map(([key, v]) => [key, go(v)]));
|
|
3815
|
+
};
|
|
3816
|
+
return { x: go(x), hit };
|
|
3817
|
+
};
|
|
3818
|
+
// (the subterms of a term with a call in them, outermost first)
|
|
3819
|
+
const subterms = (x) => { const acc = []; const go = (t) => { if (!t || typeof t !== 'object')
|
|
3820
|
+
return; if (Array.isArray(t)) {
|
|
3821
|
+
t.forEach(go);
|
|
3822
|
+
return;
|
|
3823
|
+
} const o = t; if ((o.op === 'call' || o.op === 'ctor' || o.op === 'add') && JSON.stringify(o).includes('"op":"call"') || o.op === 'match' || o.op === 'natmatch' || o.op === 'if') {
|
|
3824
|
+
acc.push(o);
|
|
3825
|
+
if (o.op === 'match' || o.op === 'natmatch' || o.op === 'if')
|
|
3826
|
+
return;
|
|
3827
|
+
} ; for (const v of Object.values(o))
|
|
3828
|
+
go(v); }; go(x); return acc; };
|
|
3829
|
+
const lawOf = (a, b) => {
|
|
3830
|
+
// (one constructor on both sides: the fields that differ, where one does)
|
|
3831
|
+
for (;;) {
|
|
3832
|
+
if (a.op !== 'ctor' || b.op !== 'ctor' || a.ctor !== b.ctor || a.data !== b.data)
|
|
3833
|
+
break;
|
|
3834
|
+
const differ = a.args.flatMap((x, i) => (sameX(x, b.args[i]) ? [] : [i]));
|
|
3835
|
+
if (differ.length !== 1)
|
|
3836
|
+
break;
|
|
3837
|
+
[a, b] = [a.args[differ[0]], b.args[differ[0]]];
|
|
3838
|
+
}
|
|
3839
|
+
if (sameX(a, b) || !(JSON.stringify([a, b]).includes('"op":"call"')))
|
|
3840
|
+
return undefined;
|
|
3841
|
+
const y = { op: '=', a, b }, vs = [];
|
|
3842
|
+
walk(y, o => { if (o.op === 'var' && !vs.some(v => v.name === o.name)) {
|
|
3843
|
+
const s = sorts.get(o.name);
|
|
3844
|
+
if (s)
|
|
3845
|
+
vs.push({ name: o.name, sort: s });
|
|
3846
|
+
} });
|
|
3847
|
+
if (!vs.length || textLength(y) > 3000)
|
|
3848
|
+
return undefined;
|
|
3849
|
+
return { name: `${showX(a)} = ${showX(b)}`, vars: vs, holds: y, params: [...new Set(paramsIn(y))] };
|
|
3850
|
+
};
|
|
3851
|
+
// The step's goal generalised: each subterm common to both sides (those inside no other common one) a fresh variable.
|
|
3852
|
+
const generalise = (a, b) => {
|
|
3853
|
+
let ga = a, gb = b;
|
|
3854
|
+
for (const t of subterms(a)) {
|
|
3855
|
+
if (!has(ga, t) || !has(gb, t))
|
|
3856
|
+
continue;
|
|
3857
|
+
const s = sortOf(t);
|
|
3858
|
+
if (!s || s === 'bool')
|
|
3859
|
+
continue;
|
|
3860
|
+
const v = freshVar(s);
|
|
3861
|
+
ga = rewrite(ga, t, v);
|
|
3862
|
+
gb = rewrite(gb, t, v);
|
|
3863
|
+
}
|
|
3864
|
+
return [ga, gb];
|
|
3865
|
+
};
|
|
3866
|
+
const bases = [];
|
|
3867
|
+
for (const v of l.vars) {
|
|
3868
|
+
const d = v.sort;
|
|
3869
|
+
const cases = [];
|
|
3870
|
+
// (the base cases too, after every step: rev (y ++ [z]) = z :: rev y, the nil case of rev (x ++ (y ++ [z])))
|
|
3871
|
+
if (d === 'nat') {
|
|
3872
|
+
const m = freshVar('nat');
|
|
3873
|
+
cases.push({ at: { op: 'add', a: m, b: { op: 'nat', value: 1 } }, rec: [m] });
|
|
3874
|
+
bases.push({ v, at: { op: 'nat', value: 0 } });
|
|
3875
|
+
}
|
|
3876
|
+
else if (d instanceof Data && !d.abstract)
|
|
3877
|
+
for (const [j, c] of d.ctors.entries()) {
|
|
3878
|
+
const fields = c.fields.map(f => freshVar(fieldSort(f)));
|
|
3879
|
+
if (!c.fields.some(f => isDataField(f.sort) && f.sort.data === d.name)) {
|
|
3880
|
+
bases.push({ v, at: { op: 'ctor', data: d.name, ctor: j, args: fields } });
|
|
3881
|
+
continue;
|
|
3882
|
+
}
|
|
3883
|
+
cases.push({ at: { op: 'ctor', data: d.name, ctor: j, args: fields }, rec: fields.filter((_, i) => { const f = c.fields[i]; return isDataField(f.sort) && f.sort.data === d.name; }) });
|
|
3884
|
+
}
|
|
3885
|
+
for (const cs of cases) {
|
|
3886
|
+
const at = (x, to) => reduceX(substXs(x, new Map([[v.name, to]])));
|
|
3887
|
+
const a = at(eq.a, cs.at), b = at(eq.b, cs.at);
|
|
3888
|
+
if (sameX(a, b))
|
|
3889
|
+
continue;
|
|
3890
|
+
const ihs = cs.rec.map(r => [at(eq.a, r), at(eq.b, r)]), free = new Set(l.vars.filter(w => w.name !== v.name).map(w => w.name));
|
|
3891
|
+
// (`common`: a rewrite of a variable, kept only where its sides then share a term to generalise)
|
|
3892
|
+
const tries = [];
|
|
3893
|
+
for (const [ia, ib] of ihs)
|
|
3894
|
+
for (const [from, to] of [[ia, ib], [ib, ia]]) {
|
|
3895
|
+
if (from.op === 'var') {
|
|
3896
|
+
// (the hypothesis's side that is a variable, rewritten in a side that doesn't read its other side: x :: t to
|
|
3897
|
+
// x :: rev (rev t), where rev (rev t ++ [x]) = x :: rev (rev t) shares rev t)
|
|
3898
|
+
for (const s1 of [a, b])
|
|
3899
|
+
if (has(s1, from) && !has(s1, to))
|
|
3900
|
+
tries.push(s1 === a ? { x: reduceX(rewrite(a, from, to)), y: b, common: true } : { x: a, y: reduceX(rewrite(b, from, to)), common: true });
|
|
3901
|
+
continue;
|
|
3902
|
+
}
|
|
3903
|
+
if (has(a, from) || has(b, from))
|
|
3904
|
+
tries.push({ x: reduceX(rewrite(a, from, to)), y: reduceX(rewrite(b, from, to)) });
|
|
3905
|
+
// (else the hypothesis at other values of the law's other variables, where they differ: an accumulator's)
|
|
3906
|
+
else if (free.size) {
|
|
3907
|
+
const ra = rewriteP(a, from, to, free), rb = rewriteP(b, from, to, free);
|
|
3908
|
+
if (ra.hit || rb.hit)
|
|
3909
|
+
tries.push({ x: reduceX(ra.x), y: reduceX(rb.x) });
|
|
3910
|
+
}
|
|
3911
|
+
}
|
|
3912
|
+
// (and the step as unfolded, where no rewrite gave one and none closed it)
|
|
3913
|
+
if (tries.some(t => !t.common && sameX(t.x, t.y)))
|
|
3914
|
+
continue;
|
|
3915
|
+
tries.push({ x: a, y: b });
|
|
3916
|
+
const before = out.length;
|
|
3917
|
+
for (const [i, { x, y, common }] of tries.entries()) {
|
|
3918
|
+
if (sameX(x, y) || (i === tries.length - 1 && out.length > before))
|
|
3919
|
+
continue;
|
|
3920
|
+
const [ga, gb] = generalise(x, y), generalised = !sameX(ga, x) || !sameX(gb, y);
|
|
3921
|
+
if (common && !generalised)
|
|
3922
|
+
continue;
|
|
3923
|
+
// (the step as it stands where nothing is common to its sides; only a generalised one's own step is calculated)
|
|
3924
|
+
const c = lawOf(ga, gb);
|
|
3925
|
+
if (c && !out.some(o => canonText(o.holds, o.vars) === canonText(c.holds, c.vars))) {
|
|
3926
|
+
if (generalised)
|
|
3927
|
+
general.add(c);
|
|
3928
|
+
out.push(c);
|
|
3929
|
+
}
|
|
3930
|
+
}
|
|
3931
|
+
}
|
|
3932
|
+
}
|
|
3933
|
+
for (const { v, at } of bases) {
|
|
3934
|
+
const a = reduceX(substXs(eq.a, new Map([[v.name, at]]))), b = reduceX(substXs(eq.b, new Map([[v.name, at]])));
|
|
3935
|
+
if (sameX(a, b))
|
|
3936
|
+
continue;
|
|
3937
|
+
const [ga, gb] = generalise(a, b), c = lawOf(ga, gb);
|
|
3938
|
+
if (c && !out.some(o => canonText(o.holds, o.vars) === canonText(c.holds, c.vars))) {
|
|
3939
|
+
based.add(c);
|
|
3940
|
+
out.push(c);
|
|
3941
|
+
}
|
|
3942
|
+
}
|
|
3943
|
+
return out;
|
|
3944
|
+
}
|
|
3278
3945
|
export const showX = (x) => {
|
|
3279
3946
|
switch (x.op) {
|
|
3280
3947
|
case 'var': return x.name.replace(/#\d+$/, '');
|
|
@@ -3390,7 +4057,7 @@ export function searchIn(l, opts, until, ctx, phase = 'all') {
|
|
|
3390
4057
|
if (c)
|
|
3391
4058
|
return c;
|
|
3392
4059
|
}
|
|
3393
|
-
const first = phase === 'rest' || (opts.lemmasFirst && Array.isArray(opts.lemmas)) ? undefined : proveWith(p, fs, l, base, lv + 1, opts, until, 6000, [1], 'plain');
|
|
4060
|
+
const first = phase === 'rest' || (opts.lemmasFirst && Array.isArray(opts.lemmas)) ? undefined : proveWith(p, fs, l, base, lv + 1, opts, until, 6000, [1], 'plain', !ctx || ctx.merge === true);
|
|
3394
4061
|
const one = first && ok(first, base);
|
|
3395
4062
|
if (one)
|
|
3396
4063
|
return one;
|
|
@@ -3407,24 +4074,37 @@ export function searchIn(l, opts, until, ctx, phase = 'all') {
|
|
|
3407
4074
|
if (two)
|
|
3408
4075
|
return two;
|
|
3409
4076
|
}
|
|
3410
|
-
|
|
4077
|
+
// The steps the law's inductions leave (lemma calculation), then the conjectures from the values.
|
|
4078
|
+
const tc = performance.now(), calc = Array.isArray(opts.lemmas) ? [] : calculated(p, l, 2, 6);
|
|
4079
|
+
if (dataTrace)
|
|
4080
|
+
console.error(`[data] ${l.name}: ${calc.length} calculated in ${Math.round(performance.now() - tc)} ms`);
|
|
4081
|
+
const cands = Array.isArray(opts.lemmas) ? opts.lemmas : [...calc, ...conjectures(p, l)];
|
|
4082
|
+
if (dataTrace)
|
|
4083
|
+
for (const c of calc)
|
|
4084
|
+
console.error(`[data] calculated ${c.name.slice(0, 200)}`);
|
|
3411
4085
|
const p2 = ctx ? p : programOf([l, ...cands]), fs2 = ctx ? fs : factsOf(p2);
|
|
3412
4086
|
// Each scheme's goals: the law's, and each companion's (a lemma may be what only a companion's goal reads).
|
|
3413
4087
|
const goalOf = (law, uses, on, concls, shapes) => {
|
|
3414
4088
|
const concl = encY(p2, law.holds, new Map()), ihc = uses.map(u => substC(concls[u.member ?? 0] ?? concl, { vars: u.subst, maps: {}, flags: {} }));
|
|
3415
4089
|
return { from: [concl, ihc], insts: instancesOf(p2, fs2, { roots: [concl], others: ihc, depth: 2, shapesAt: shapes ?? (on && on.d !== 'nat' ? [ev(on.v)] : []), etaAt: [], models: [] }) };
|
|
3416
4090
|
};
|
|
3417
|
-
const goals = schemes(p2, l).flatMap(s => {
|
|
4091
|
+
const goals = schemes(p2, l).filter(s => !s.merged).flatMap(s => {
|
|
3418
4092
|
const all = [{ law: l, uses: s.uses, on: s.on, shapes: s.shapes }, ...(s.companions ?? [])], concls = all.map(m => encY(p2, m.law.holds, new Map()));
|
|
3419
4093
|
return all.map((m, i) => goalOf(m.law, m.uses.map(u => ({ ...u, member: u.member ?? i })), m.on, concls, 'shapes' in m ? m.shapes : undefined));
|
|
3420
4094
|
});
|
|
3421
|
-
|
|
4095
|
+
// (every conjecture against each goal at once: a lemma's uses are found apart from the others')
|
|
4096
|
+
const goalHits = new Set(), others = cands.slice(calc.length);
|
|
4097
|
+
for (const g of goals)
|
|
4098
|
+
lemmaUsesFor(p2, fs2, others.map((c, i) => ({ law: c, node: i, concl: encY(p2, c.holds, new Map()) })), g.from, g.insts, [], undefined, goalHits);
|
|
4099
|
+
const matchesGoal = (c) => goalHits.has(others.indexOf(c));
|
|
3422
4100
|
// Those the goal matches first, of those the ones of the law's shape (an implication for an implication) first.
|
|
3423
4101
|
const implication = (y) => y.op === 'or' && y.xs.length === 2 && y.xs[0].op === 'not';
|
|
3424
4102
|
const score = (c, hit) => (hit ? 2 : 0) + (implication(c.holds) === implication(l.holds) ? 1 : 0);
|
|
3425
4103
|
if (dataTrace)
|
|
3426
4104
|
console.error(`[data] ${l.name}: ${cands.length} conjectures at ${Math.round(clock())} of ${Math.round(until)}${cands.map(c => `\n[data] conjecture ${c.name.slice(0, 160)}`).join('')}`);
|
|
3427
|
-
|
|
4105
|
+
// (the calculated steps first, in their order: each before the one it was calculated from)
|
|
4106
|
+
const calcSet = new Set(calc);
|
|
4107
|
+
const ranked = [...calc.map(c => ({ c, hit: true })), ...cands.slice(calc.length).map(c => ({ c, hit: matchesGoal(c) })).sort((a, b) => score(b.c, b.hit) - score(a.c, a.hit))];
|
|
3428
4108
|
// The lemmas proven: those given (`base`) and those found, each found one a level above the last.
|
|
3429
4109
|
const proven = [...base], failed = new Set(), level = () => lv + 1 + proven.length - base.length;
|
|
3430
4110
|
// A pass of lemmas: of those the goal reads (`hits`) or the others, each proven at one unfolding (8 s of counted work
|
|
@@ -3434,19 +4114,35 @@ export function searchIn(l, opts, until, ctx, phase = 'all') {
|
|
|
3434
4114
|
// rewriting), then by its schemes: after each lemma the goal reads or of the law's shape, else at the end of the pass.
|
|
3435
4115
|
const refuted = new Set();
|
|
3436
4116
|
const lemmaPass = (which, most, end, cap, depths) => {
|
|
3437
|
-
|
|
4117
|
+
// (`extra`: the calculated steps' attempts and the second ones, at most `most`, beside the conjectures' 3·most)
|
|
4118
|
+
let n = 0, since = 0, tries = 0, extra = 0;
|
|
3438
4119
|
const again = (how) => ok(proveWith(p2, fs2, l, proven, level(), opts, until, cap, depths, how), proven, p2);
|
|
3439
|
-
|
|
4120
|
+
// A conjecture of this pass that failed is tried once more after a lemma is proven (HipSpec's loop: bind over an
|
|
4121
|
+
// append needs ++ associative, proven after it); first those, then the next of the ranking.
|
|
4122
|
+
const queue = ranked.filter(r => (which === 'hits') === r.hit), retry = [], retried = new Set();
|
|
4123
|
+
for (let i = 0; i < queue.length || retry.length;) {
|
|
3440
4124
|
if (n >= most || tries >= 3 * most || clock() > end)
|
|
3441
4125
|
break;
|
|
3442
|
-
if (
|
|
4126
|
+
if (extra >= most) {
|
|
4127
|
+
retry.length = 0;
|
|
4128
|
+
if (i >= queue.length)
|
|
4129
|
+
break;
|
|
4130
|
+
}
|
|
4131
|
+
const again2 = retry.length > 0, c = again2 ? retry.shift() : queue[i++].c;
|
|
4132
|
+
if (proven.some(q => q.law === c) || refuted.has(c))
|
|
3443
4133
|
continue;
|
|
3444
|
-
if (
|
|
4134
|
+
if (!again2 && calcSet.has(c) && extra >= most)
|
|
4135
|
+
continue;
|
|
4136
|
+
if (!again2 && refute(c, 200)) {
|
|
3445
4137
|
refuted.add(c);
|
|
3446
4138
|
continue;
|
|
3447
4139
|
}
|
|
3448
|
-
|
|
3449
|
-
|
|
4140
|
+
if (again2 || calcSet.has(c))
|
|
4141
|
+
extra++;
|
|
4142
|
+
else
|
|
4143
|
+
tries++;
|
|
4144
|
+
// (a calculated step half a conjecture's time: there are few, and one past its time is seldom the one)
|
|
4145
|
+
const r = proveWith(p2, fs2, c, proven, level(), opts, Math.min(end, clock() + (calcSet.has(c) ? 3000 : 8000) * (opts.patience ?? 1)), 5000, [1]);
|
|
3450
4146
|
tried += r.tried;
|
|
3451
4147
|
if (!r.out) {
|
|
3452
4148
|
failed.add(c);
|
|
@@ -3456,7 +4152,15 @@ export function searchIn(l, opts, until, ctx, phase = 'all') {
|
|
|
3456
4152
|
proven.push(r.out);
|
|
3457
4153
|
n++;
|
|
3458
4154
|
since++;
|
|
3459
|
-
|
|
4155
|
+
for (const q of queue.slice(0, i))
|
|
4156
|
+
if (failed.has(q.c) && !retried.has(q.c)) {
|
|
4157
|
+
retried.add(q.c);
|
|
4158
|
+
retry.push(q.c);
|
|
4159
|
+
}
|
|
4160
|
+
// (as before the passes: again after a lemma the goal reads, or one of the law's shape, an implication for one; a
|
|
4161
|
+
// calculated step's after the last of them, whose proofs read each other)
|
|
4162
|
+
if (calcSet.has(c) && !retry.length && i < queue.length && calcSet.has(queue[i].c))
|
|
4163
|
+
continue;
|
|
3460
4164
|
if (which === 'hits' || implication(c.holds) === implication(l.holds)) {
|
|
3461
4165
|
since = 0;
|
|
3462
4166
|
const r2 = again('first');
|