gdp-wired 0.46.0 → 0.48.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 +18 -5
- package/dist/core/carry.d.ts +15 -0
- package/dist/core/carry.js +651 -0
- package/dist/core/cli.d.ts +4 -1
- package/dist/core/cli.js +31 -11
- package/dist/core/count-cases.d.ts +24 -0
- package/dist/core/count-cases.js +320 -0
- package/dist/core/count-cert-grow.d.ts +4 -0
- package/dist/core/count-cert-grow.js +37 -0
- package/dist/core/count-cert-transport.d.ts +3 -0
- package/dist/core/count-cert-transport.js +58 -0
- package/dist/core/count-completion.d.ts +9 -0
- package/dist/core/count-completion.js +27 -0
- package/dist/core/count-cubes.d.ts +8 -0
- package/dist/core/count-cubes.js +69 -0
- package/dist/core/count-demand.d.ts +37 -0
- package/dist/core/count-demand.js +361 -0
- package/dist/core/count-facts.d.ts +52 -0
- package/dist/core/count-facts.js +247 -0
- package/dist/core/count-focus-proof.d.ts +8 -0
- package/dist/core/count-focus-proof.js +140 -0
- package/dist/core/count-focused.d.ts +22 -0
- package/dist/core/count-focused.js +676 -0
- package/dist/core/count-ranges.d.ts +5 -0
- package/dist/core/count-ranges.js +65 -0
- package/dist/core/count-recovery/action-guard.d.ts +5 -0
- package/dist/core/count-recovery/action-guard.js +51 -0
- package/dist/core/count-recovery/boolean-core.d.ts +16 -0
- package/dist/core/count-recovery/boolean-core.js +258 -0
- package/dist/core/count-recovery/carried-case.d.ts +2 -0
- package/dist/core/count-recovery/carried-case.js +24 -0
- package/dist/core/count-recovery/conditional-check-cache.d.ts +23 -0
- package/dist/core/count-recovery/conditional-check-cache.js +130 -0
- package/dist/core/count-recovery/conditional-cuts.d.ts +6 -0
- package/dist/core/count-recovery/conditional-cuts.js +318 -0
- package/dist/core/count-recovery/conditional-proof.d.ts +19 -0
- package/dist/core/count-recovery/conditional-proof.js +306 -0
- package/dist/core/count-recovery/derivability-closure.d.ts +14 -0
- package/dist/core/count-recovery/derivability-closure.js +533 -0
- package/dist/core/count-recovery/empty-count-source.d.ts +2 -0
- package/dist/core/count-recovery/empty-count-source.js +40 -0
- package/dist/core/count-recovery/field-bound-lemmas.d.ts +23 -0
- package/dist/core/count-recovery/field-bound-lemmas.js +640 -0
- package/dist/core/count-recovery/field-case.d.ts +5 -0
- package/dist/core/count-recovery/field-case.js +89 -0
- package/dist/core/count-recovery/field-demand.d.ts +46 -0
- package/dist/core/count-recovery/field-demand.js +47 -0
- package/dist/core/count-recovery/field-only.d.ts +3 -0
- package/dist/core/count-recovery/field-only.js +18 -0
- package/dist/core/count-recovery/fold-path.d.ts +6 -0
- package/dist/core/count-recovery/fold-path.js +35 -0
- package/dist/core/count-recovery/frame-lemmas.d.ts +9 -0
- package/dist/core/count-recovery/frame-lemmas.js +120 -0
- package/dist/core/count-recovery/guard-cases.d.ts +12 -0
- package/dist/core/count-recovery/guard-cases.js +138 -0
- package/dist/core/count-recovery/key-closure.d.ts +14 -0
- package/dist/core/count-recovery/key-closure.js +62 -0
- package/dist/core/count-recovery/leaf-allowance.d.ts +4 -0
- package/dist/core/count-recovery/leaf-allowance.js +7 -0
- package/dist/core/count-recovery/lift-count-proxies.d.ts +6 -0
- package/dist/core/count-recovery/lift-count-proxies.js +175 -0
- package/dist/core/count-recovery/live-dependencies.d.ts +6 -0
- package/dist/core/count-recovery/live-dependencies.js +76 -0
- package/dist/core/count-recovery/member-inclusions.d.ts +11 -0
- package/dist/core/count-recovery/member-inclusions.js +168 -0
- package/dist/core/count-recovery/member-premises.d.ts +29 -0
- package/dist/core/count-recovery/member-premises.js +114 -0
- package/dist/core/count-recovery/natural-cases.d.ts +6 -0
- package/dist/core/count-recovery/natural-cases.js +59 -0
- package/dist/core/count-recovery/ops.d.ts +6 -0
- package/dist/core/count-recovery/ops.js +8 -0
- package/dist/core/count-recovery/pre-state-source.d.ts +8 -0
- package/dist/core/count-recovery/pre-state-source.js +18 -0
- package/dist/core/count-recovery/rebase-path.d.ts +9 -0
- package/dist/core/count-recovery/rebase-path.js +70 -0
- package/dist/core/count-recovery/rename-proof.d.ts +2 -0
- package/dist/core/count-recovery/rename-proof.js +102 -0
- package/dist/core/count-recovery/row-cases.d.ts +16 -0
- package/dist/core/count-recovery/row-cases.js +291 -0
- package/dist/core/count-recovery/scalar-guards.d.ts +7 -0
- package/dist/core/count-recovery/scalar-guards.js +291 -0
- package/dist/core/count-recovery/source-memo.d.ts +12 -0
- package/dist/core/count-recovery/source-memo.js +67 -0
- package/dist/core/count-select.d.ts +16 -0
- package/dist/core/count-select.js +24 -0
- package/dist/core/count-support.d.ts +598 -0
- package/dist/core/count-support.js +2080 -0
- package/dist/core/count-unions.d.ts +2 -0
- package/dist/core/count-unions.js +139 -0
- package/dist/core/count-witness-flags.d.ts +9 -0
- package/dist/core/count-witness-flags.js +37 -0
- package/dist/core/count-write-frames.d.ts +12 -0
- package/dist/core/count-write-frames.js +206 -0
- package/dist/core/counts.d.ts +20 -2
- package/dist/core/counts.js +807 -156
- package/dist/core/dsl.d.ts +50 -6
- package/dist/core/dsl.js +307 -65
- package/dist/core/effect-schema.d.ts +90 -0
- package/dist/core/effect-schema.js +500 -0
- package/dist/core/effect.d.ts +3 -7
- package/dist/core/effect.js +10 -65
- package/dist/core/exists.d.ts +1 -1
- package/dist/core/exists.js +80 -15
- package/dist/core/extract-check.d.ts +7 -1
- package/dist/core/extract-check.js +161 -19
- package/dist/core/extract-go-check.js +24 -4
- package/dist/core/extract-go.d.ts +7 -0
- package/dist/core/extract-go.js +29 -2
- package/dist/core/extract-read.d.ts +31 -9
- package/dist/core/extract-read.js +36 -11
- package/dist/core/extract-rs.js +58 -14
- package/dist/core/extract-shapes.d.ts +1 -0
- package/dist/core/extract-shapes.js +190 -27
- package/dist/core/extract-standalone.d.ts +3 -0
- package/dist/core/extract-standalone.js +265 -0
- package/dist/core/extract.d.ts +1 -0
- package/dist/core/extract.js +25 -5
- package/dist/core/field-laws.d.ts +2 -0
- package/dist/core/field-laws.js +100 -0
- package/dist/core/finite-frontiers.d.ts +5 -0
- package/dist/core/finite-frontiers.js +116 -0
- package/dist/core/finite-houdini.d.ts +52 -0
- package/dist/core/finite-houdini.js +460 -0
- package/dist/core/finite-model.d.ts +13 -0
- package/dist/core/finite-model.js +68 -0
- package/dist/core/finite-tiny.d.ts +72 -0
- package/dist/core/finite-tiny.js +1052 -0
- package/dist/core/infer.d.ts +1 -1
- package/dist/core/infer.js +32 -9
- package/dist/core/laws.d.ts +2 -1
- package/dist/core/laws.js +76 -19
- package/dist/core/lemma-cuts.d.ts +38 -0
- package/dist/core/lemma-cuts.js +365 -0
- package/dist/core/machine.d.ts +39 -2
- package/dist/core/machine.js +240 -35
- package/dist/core/mutate.d.ts +55 -0
- package/dist/core/mutate.js +210 -0
- package/dist/core/native-domain.d.ts +6 -0
- package/dist/core/native-domain.js +114 -0
- package/dist/core/native-law.d.ts +3 -0
- package/dist/core/native-law.js +174 -0
- package/dist/core/quorum-laws.d.ts +5 -0
- package/dist/core/quorum-laws.js +239 -0
- package/dist/core/relational.js +10 -2
- package/dist/core/relevance.d.ts +4 -0
- package/dist/core/relevance.js +79 -0
- package/dist/core/rs/Cargo.toml +1 -1
- package/dist/core/rs/src/main.rs +458 -9
- package/dist/core/runtime-wall.d.ts +3 -0
- package/dist/core/runtime-wall.js +4 -0
- package/dist/core/sample-step.d.ts +16 -0
- package/dist/core/sample-step.js +28 -0
- package/dist/core/source-check.d.ts +44 -0
- package/dist/core/source-check.js +126 -0
- package/dist/core/source-diagnostic.d.ts +1 -0
- package/dist/core/source-diagnostic.js +34 -0
- package/dist/core/support-budget.d.ts +2 -0
- package/dist/core/support-budget.js +6 -0
- package/dist/core/system.js +2 -0
- package/dist/core/wall.d.ts +6 -0
- package/dist/core/wall.js +23 -0
- package/dist/core/watch.js +4 -0
- package/dist/kernel/cdcl.d.ts +4 -0
- package/dist/kernel/cdcl.js +20 -5
- package/dist/kernel/kernel.d.ts +10 -0
- package/dist/kernel/kernel.js +56 -3
- package/dist/kernel/model-sequence.d.ts +2 -2
- package/dist/kernel/model-sequence.js +4 -3
- package/dist/kernel/model-values.d.ts +2 -0
- package/dist/kernel/model-values.js +60 -0
- package/dist/kernel/prove.d.ts +10 -0
- package/dist/kernel/prove.js +56 -8
- package/dist/kernel/prover-rows.d.ts +29 -0
- package/dist/kernel/prover-rows.js +114 -0
- package/dist/lean-kernel/emit.d.ts +39 -2
- package/dist/lean-kernel/emit.js +136 -24
- package/dist/lean-kernel/lean-terms.d.ts +1 -0
- package/dist/lean-kernel/lean-terms.js +13 -5
- package/dist/lean-kernel/library.js +4 -0
- package/dist/lean-kernel/qalpha.d.ts +7 -0
- package/dist/lean-kernel/qalpha.js +40 -0
- package/package.json +5 -1
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
// Untrusted proof compression. Turn repeated forced splits into independently checked conditional cuts.
|
|
2
|
+
// No search, protocol vocabulary, new facts or proof-budget changes: failed transport/check returns no proposal.
|
|
3
|
+
import { n, plus, times, x } from "./machine.js";
|
|
4
|
+
import { add, k, scale } from "./solver.js";
|
|
5
|
+
import { canon, canonFlip, checkGoalLemmas, isCell, isMonomial, leafConstraintsReusing as leafConstraints, lemmaPath } from "../kernel/kernel.js";
|
|
6
|
+
const originOf = new WeakMap();
|
|
7
|
+
// Search provenance is only an optional proposer hint. The trusted checker never reads this cache.
|
|
8
|
+
export const proofOrigins = { enabled: false };
|
|
9
|
+
export const proofOrigin = (tree) => originOf.get(tree);
|
|
10
|
+
export const setProofOrigin = (tree, origin) => { originOf.set(tree, origin); };
|
|
11
|
+
export function rememberProofOrigin(out, part, tree, path, whole) {
|
|
12
|
+
if (!out)
|
|
13
|
+
return;
|
|
14
|
+
const earlier = originOf.get(tree);
|
|
15
|
+
if (earlier && earlier.goal.hyps.length <= part.hyps.length)
|
|
16
|
+
originOf.set(out, earlier);
|
|
17
|
+
else if (part.hyps.length < whole.hyps.length)
|
|
18
|
+
originOf.set(out, { goal: part, tree, path: new Map(path) });
|
|
19
|
+
}
|
|
20
|
+
const rowIds = new WeakMap(), rowIndexes = new WeakMap();
|
|
21
|
+
const rowId = (lin) => {
|
|
22
|
+
let id = rowIds.get(lin);
|
|
23
|
+
if (id === undefined) {
|
|
24
|
+
id = canon(lin);
|
|
25
|
+
rowIds.set(lin, id);
|
|
26
|
+
}
|
|
27
|
+
return id;
|
|
28
|
+
};
|
|
29
|
+
const rowIndex = (rows) => {
|
|
30
|
+
let at = rowIndexes.get(rows);
|
|
31
|
+
if (!at) {
|
|
32
|
+
at = new Map();
|
|
33
|
+
rows.forEach((r, i) => { const id = rowId(r.lin); if (!at.has(id))
|
|
34
|
+
at.set(id, i); });
|
|
35
|
+
rowIndexes.set(rows, at);
|
|
36
|
+
}
|
|
37
|
+
return at;
|
|
38
|
+
};
|
|
39
|
+
const rowsOf = new WeakMap();
|
|
40
|
+
const pathKey = (p) => JSON.stringify([...p].map(([id, a]) => [id, canon(a.lin), a.holds]));
|
|
41
|
+
const savedProofRows = (tree, goal, path) => {
|
|
42
|
+
const old = rowsOf.get(tree);
|
|
43
|
+
return old?.goal === goal && old.path === pathKey(path) ? old : undefined;
|
|
44
|
+
};
|
|
45
|
+
const saveProofRows = (tree, goal, path, rows, atoms) => {
|
|
46
|
+
rowsOf.set(tree, { goal, path: pathKey(path), rows, atoms });
|
|
47
|
+
};
|
|
48
|
+
// Hints only: the trusted checker always rebuilds its own rows. Capturing reuses work already done by widen.
|
|
49
|
+
export function rememberProofRows(tree, goal, path, rows) {
|
|
50
|
+
if (proofOrigins.enabled && 'leaf' in tree)
|
|
51
|
+
saveProofRows(tree, goal, path, rows);
|
|
52
|
+
}
|
|
53
|
+
const decide = (lin, path) => {
|
|
54
|
+
if (!Object.values(lin).every(Number.isSafeInteger))
|
|
55
|
+
return undefined;
|
|
56
|
+
if (Object.keys(lin).every(k => k === '1'))
|
|
57
|
+
return (lin['1'] ?? 0) <= 0;
|
|
58
|
+
const direct = path.get(canon(lin))?.holds;
|
|
59
|
+
if (direct !== undefined)
|
|
60
|
+
return direct;
|
|
61
|
+
const opposite = path.get(canonFlip(lin))?.holds;
|
|
62
|
+
return opposite === undefined ? undefined : !opposite;
|
|
63
|
+
};
|
|
64
|
+
const flip = (r) => add(k(1), scale(r, -1));
|
|
65
|
+
const put = (p, lin, holds) => new Map(p).set(canon(lin), { lin, holds });
|
|
66
|
+
const proofPath = (when, cut) => {
|
|
67
|
+
const p = new Map();
|
|
68
|
+
let duplicate = 0;
|
|
69
|
+
for (const atom of [{ lin: cut, holds: false }, ...when.map(lin => ({ lin, holds: true }))]) {
|
|
70
|
+
const key = canon(atom.lin);
|
|
71
|
+
p.set(p.has(key) ? `@${duplicate++}` : key, atom);
|
|
72
|
+
}
|
|
73
|
+
return p;
|
|
74
|
+
};
|
|
75
|
+
// Unlike widen, transport never calls a solver when a row is missing.
|
|
76
|
+
function transport(from, to, tree, before, after, until, missing) {
|
|
77
|
+
if (performance.now() >= until)
|
|
78
|
+
return undefined;
|
|
79
|
+
if ('split' in tree) {
|
|
80
|
+
const known = decide(tree.split, after);
|
|
81
|
+
if (known !== undefined)
|
|
82
|
+
return transport(from, to, known ? tree.yes : tree.no, put(before, tree.split, known), after, until, missing);
|
|
83
|
+
const yes = transport(from, to, tree.yes, put(before, tree.split, true), put(after, tree.split, true), until, missing);
|
|
84
|
+
const no = yes && transport(from, to, tree.no, put(before, tree.split, false), put(after, tree.split, false), until, missing);
|
|
85
|
+
return yes && no ? { split: tree.split, yes, no } : undefined;
|
|
86
|
+
}
|
|
87
|
+
const atoms = new Set();
|
|
88
|
+
const old = savedProofRows(tree, from, before)?.rows ?? leafConstraints(from, before);
|
|
89
|
+
const rows = leafConstraints(to, new ObservedPath(after, atoms)), at = rowIndex(rows);
|
|
90
|
+
const index = (i) => {
|
|
91
|
+
if (i >= old.length)
|
|
92
|
+
return i - old.length + rows.length;
|
|
93
|
+
const row = old[i], found = row && at.get(rowId(row.lin));
|
|
94
|
+
if (row && found === undefined)
|
|
95
|
+
missing?.(row.lin);
|
|
96
|
+
return found;
|
|
97
|
+
};
|
|
98
|
+
const leaf = [], products = [];
|
|
99
|
+
for (const s of tree.leaf) {
|
|
100
|
+
if (s.kind !== 'input')
|
|
101
|
+
leaf.push(s);
|
|
102
|
+
else {
|
|
103
|
+
const i = index(s.index);
|
|
104
|
+
if (i === undefined)
|
|
105
|
+
return undefined;
|
|
106
|
+
leaf.push({ ...s, index: i });
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
for (const [a, b] of tree.products ?? []) {
|
|
110
|
+
const i = index(a), j = index(b);
|
|
111
|
+
if (i === undefined || j === undefined)
|
|
112
|
+
return undefined;
|
|
113
|
+
products.push([i, j]);
|
|
114
|
+
}
|
|
115
|
+
const out = { leaf, ...(tree.products ? { products } : {}), ...(tree.squares ? { squares: tree.squares } : {}) };
|
|
116
|
+
saveProofRows(out, to, after, rows, atoms);
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
// Observe only lookups; enumerating path rows is not itself a dependence on every path assumption.
|
|
120
|
+
class ObservedPath extends Map {
|
|
121
|
+
observed;
|
|
122
|
+
constructor(path, observed) {
|
|
123
|
+
super(path);
|
|
124
|
+
this.observed = observed;
|
|
125
|
+
}
|
|
126
|
+
get(key) { this.observed.add(key); return super.get(key); }
|
|
127
|
+
has(key) { this.observed.add(key); return super.has(key); }
|
|
128
|
+
}
|
|
129
|
+
function dependencies(g, t, path, facts, atoms, until) {
|
|
130
|
+
if (performance.now() >= until)
|
|
131
|
+
return false;
|
|
132
|
+
if ('split' in t)
|
|
133
|
+
return dependencies(g, t.yes, put(path, t.split, true), facts, atoms, until)
|
|
134
|
+
&& dependencies(g, t.no, put(path, t.split, false), facts, atoms, until);
|
|
135
|
+
const observed = new ObservedPath(path, atoms), cached = savedProofRows(t, g, path);
|
|
136
|
+
const rows = cached?.rows ?? leafConstraints(g, observed), values = [...path.values()];
|
|
137
|
+
if (cached?.atoms)
|
|
138
|
+
for (const key of cached.atoms)
|
|
139
|
+
atoms.add(key);
|
|
140
|
+
// Prover-captured tables have no new lookup trace. Conservatively retain their source conditions.
|
|
141
|
+
else if (cached)
|
|
142
|
+
for (const a of values) {
|
|
143
|
+
atoms.add(canon(a.lin));
|
|
144
|
+
atoms.add(canonFlip(a.lin));
|
|
145
|
+
}
|
|
146
|
+
for (const i of [...t.leaf.flatMap(s => s.kind === 'input' ? [s.index] : []), ...(t.products ?? []).flat()]) {
|
|
147
|
+
const src = rows[i]?.src;
|
|
148
|
+
if (src && 'fact' in src)
|
|
149
|
+
facts.add(src.fact);
|
|
150
|
+
if (src && 'atom' in src) {
|
|
151
|
+
const a = values[src.atom];
|
|
152
|
+
if (a) {
|
|
153
|
+
atoms.add(canon(a.lin));
|
|
154
|
+
atoms.add(canonFlip(a.lin));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
// Conditional replay scopes are explicit hypotheses, never silently assumed. Root scopes currently reify scalar
|
|
161
|
+
// rows; rootless production certificates also support all the original cell/term/monomial columns.
|
|
162
|
+
function scopeCondition(lin) {
|
|
163
|
+
let a = n(0), b = n(0);
|
|
164
|
+
for (const [name, z] of Object.entries(lin)) {
|
|
165
|
+
if (!Number.isSafeInteger(z))
|
|
166
|
+
return undefined;
|
|
167
|
+
if (name !== '1' && (isCell(name) || isMonomial(name) || name.startsWith('⟦')))
|
|
168
|
+
return undefined;
|
|
169
|
+
const term = name === '1' ? n(Math.abs(z)) : times(Math.abs(z), x(name));
|
|
170
|
+
if (z > 0)
|
|
171
|
+
a = plus(a, term);
|
|
172
|
+
else if (z < 0)
|
|
173
|
+
b = plus(b, term);
|
|
174
|
+
}
|
|
175
|
+
return { op: '<=', a, b };
|
|
176
|
+
}
|
|
177
|
+
// Candidate construction only. Call shareLemmaCuts or the containing machine checker before admission.
|
|
178
|
+
export function proposeLemmaCuts(goal, tree, options = {}) {
|
|
179
|
+
const started = performance.now(), root = options.path ?? new Map(), until = options.until ?? Infinity;
|
|
180
|
+
const sizes = new WeakMap(), repeats = new Map();
|
|
181
|
+
const measure = (t) => {
|
|
182
|
+
if ('leaf' in t) {
|
|
183
|
+
sizes.set(t, 1);
|
|
184
|
+
return 1;
|
|
185
|
+
}
|
|
186
|
+
const id = canon(t.split);
|
|
187
|
+
repeats.set(id, (repeats.get(id) ?? 0) + 1);
|
|
188
|
+
const size = 1 + measure(t.yes) + measure(t.no);
|
|
189
|
+
sizes.set(t, size);
|
|
190
|
+
return size;
|
|
191
|
+
};
|
|
192
|
+
const before = measure(tree);
|
|
193
|
+
if (before < (options.minNodes ?? 64))
|
|
194
|
+
return undefined;
|
|
195
|
+
const roots = [...root.values()], conditions = roots.map(a => scopeCondition(a.holds ? a.lin : flip(a.lin)));
|
|
196
|
+
if (conditions.some(c => !c))
|
|
197
|
+
return undefined;
|
|
198
|
+
const scoped = root.size ? { ...goal, hyps: [...goal.hyps, ...conditions] } : goal;
|
|
199
|
+
const lemmas = [], shared = new Map(), origins = new WeakMap();
|
|
200
|
+
const keyOf = (when, cut) => when.map(canon).sort().join(';') + '=>' + canon(cut);
|
|
201
|
+
const makeLemma = (t, original, real) => {
|
|
202
|
+
let when = [...real.values()].map(a => a.holds ? a.lin : flip(a.lin));
|
|
203
|
+
let base = proofPath(when, t.split);
|
|
204
|
+
const available = lemmas.filter(l => l.when.every(w => decide(w, real) === true));
|
|
205
|
+
const from = (g, body, path) => {
|
|
206
|
+
const required = [];
|
|
207
|
+
let proof;
|
|
208
|
+
for (;;) {
|
|
209
|
+
let absent;
|
|
210
|
+
const target = required.reduce((p, l) => put(p, l.cut, true), base);
|
|
211
|
+
proof = transport(g, goal, body, path, target, until, row => { absent = canon(row); });
|
|
212
|
+
if (proof)
|
|
213
|
+
break;
|
|
214
|
+
const dependency = available.find(l => canon(l.cut) === absent && !required.includes(l));
|
|
215
|
+
if (!dependency)
|
|
216
|
+
return undefined;
|
|
217
|
+
required.push(dependency);
|
|
218
|
+
}
|
|
219
|
+
// Inline only prerequisites whose exact rows were needed. Each is an already independent proof;
|
|
220
|
+
// no certificate lemma is permitted to cite another lemma, and there is no circular trust.
|
|
221
|
+
for (let i = required.length - 1; i >= 0; i--) {
|
|
222
|
+
const l = required[i], local = l.slice ? { ...goal, hyps: l.slice.map(j => goal.hyps[j]) } : goal;
|
|
223
|
+
const prefix = required.slice(0, i).reduce((p, prev) => put(p, prev.cut, true), base);
|
|
224
|
+
const no = transport(local, goal, l.tree, proofPath(l.when, l.cut), put(prefix, l.cut, false), until);
|
|
225
|
+
if (!no)
|
|
226
|
+
return undefined;
|
|
227
|
+
proof = { split: l.cut, yes: proof, no };
|
|
228
|
+
}
|
|
229
|
+
return proof;
|
|
230
|
+
};
|
|
231
|
+
const origin = proofOrigin(t.no);
|
|
232
|
+
let proof = origin && from(origin.goal, origin.tree, origin.path);
|
|
233
|
+
proof ??= from(goal, t.no, put(original, t.split, false));
|
|
234
|
+
if (!proof)
|
|
235
|
+
return undefined;
|
|
236
|
+
const used = new Set(), seen = new Set();
|
|
237
|
+
if (!dependencies(goal, proof, base, used, seen, until))
|
|
238
|
+
return undefined;
|
|
239
|
+
let slice = [...used].sort((a, b) => a - b), small = { ...goal, hyps: slice.map(i => goal.hyps[i]) };
|
|
240
|
+
const narrowed = transport(goal, small, proof, base, base, until);
|
|
241
|
+
if (narrowed)
|
|
242
|
+
proof = narrowed;
|
|
243
|
+
else {
|
|
244
|
+
slice = goal.hyps.map((_, i) => i);
|
|
245
|
+
small = goal;
|
|
246
|
+
}
|
|
247
|
+
// A failed dependency minimization is just a missed optimization. The old proof and full conditions remain.
|
|
248
|
+
const needed = new Set();
|
|
249
|
+
if (!dependencies(small, proof, base, new Set(), needed, until))
|
|
250
|
+
return undefined;
|
|
251
|
+
const less = when.filter(w => needed.has(canon(w)) || needed.has(canonFlip(w)));
|
|
252
|
+
if (less.length < when.length) {
|
|
253
|
+
const next = proofPath(less, t.split), reduced = transport(small, small, proof, base, next, until);
|
|
254
|
+
if (reduced) {
|
|
255
|
+
when = less;
|
|
256
|
+
base = next;
|
|
257
|
+
proof = reduced;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
// Lookup traces are conservative (a discarded alternative may read a condition). Remove each remaining
|
|
261
|
+
// unnecessary condition by exact row transport, never by asserting that an unknown condition holds.
|
|
262
|
+
for (let i = options.minimizeConditions ? when.length - 1 : -1; i >= 0; i--) {
|
|
263
|
+
const less = when.filter((_, j) => i !== j), next = proofPath(less, t.split);
|
|
264
|
+
const reduced = transport(small, small, proof, base, next, until);
|
|
265
|
+
if (reduced) {
|
|
266
|
+
when = less;
|
|
267
|
+
base = next;
|
|
268
|
+
proof = reduced;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return { when, cut: t.split, tree: proof, slice };
|
|
272
|
+
};
|
|
273
|
+
const visit = (t, original, real) => {
|
|
274
|
+
if (performance.now() >= until)
|
|
275
|
+
return undefined;
|
|
276
|
+
if ('leaf' in t) {
|
|
277
|
+
const copy = { ...t };
|
|
278
|
+
origins.set(copy, { tree: t, path: original });
|
|
279
|
+
return copy;
|
|
280
|
+
}
|
|
281
|
+
const id = canon(t.split);
|
|
282
|
+
// Prefer compact repeated derivations. Larger enclosing candidates are left as ordinary tree nodes;
|
|
283
|
+
// the final comparison still weighs each proposed proof by its actual hypothesis context.
|
|
284
|
+
if ((repeats.get(id) ?? 0) >= 2 && (sizes.get(t.no) ?? 0) >= 3 && (sizes.get(t.no) ?? 0) <= 128) {
|
|
285
|
+
const fullKey = keyOf([...real.values()].map(a => a.holds ? a.lin : flip(a.lin)), t.split);
|
|
286
|
+
let found = shared.get(fullKey);
|
|
287
|
+
if (found === undefined) {
|
|
288
|
+
const usable = lemmas.findIndex(l => canon(l.cut) === id && l.when.every(w => decide(w, real) === true));
|
|
289
|
+
if (usable >= 0)
|
|
290
|
+
found = usable;
|
|
291
|
+
}
|
|
292
|
+
if (found === undefined) {
|
|
293
|
+
const begun = performance.now(), lemma = makeLemma(t, original, real);
|
|
294
|
+
options.trace?.({ stage: 'lemma', produced: !!lemma, nodes: sizes.get(t.no) ?? 0, ms: performance.now() - begun, when: lemma?.when.length ?? -1, hyps: lemma?.slice?.length ?? -1 });
|
|
295
|
+
if (lemma) {
|
|
296
|
+
const key = keyOf(lemma.when, lemma.cut);
|
|
297
|
+
found = shared.get(key);
|
|
298
|
+
if (found === undefined) {
|
|
299
|
+
found = lemmas.length;
|
|
300
|
+
lemmas.push(lemma);
|
|
301
|
+
shared.set(key, found);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
if (found !== undefined) {
|
|
306
|
+
shared.set(fullKey, found);
|
|
307
|
+
return visit(t.yes, put(original, t.split, true), real);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
const yes = visit(t.yes, put(original, t.split, true), put(real, t.split, true));
|
|
311
|
+
const no = yes && visit(t.no, put(original, t.split, false), put(real, t.split, false));
|
|
312
|
+
return yes && no ? { split: t.split, yes, no } : undefined;
|
|
313
|
+
};
|
|
314
|
+
const skeleton = visit(tree, root, root);
|
|
315
|
+
if (!skeleton || !lemmas.length) {
|
|
316
|
+
options.trace?.({ stage: 'no-skeleton', lemmas: lemmas.length });
|
|
317
|
+
return undefined;
|
|
318
|
+
}
|
|
319
|
+
const finish = (t, path) => {
|
|
320
|
+
if ('leaf' in t) {
|
|
321
|
+
const src = origins.get(t);
|
|
322
|
+
return transport(goal, scoped, src.tree, src.path, lemmaPath(lemmas, path), until);
|
|
323
|
+
}
|
|
324
|
+
const yes = finish(t.yes, put(path, t.split, true)), no = yes && finish(t.no, put(path, t.split, false));
|
|
325
|
+
return yes && no ? { split: t.split, yes, no } : undefined;
|
|
326
|
+
};
|
|
327
|
+
let out = finish(skeleton, root);
|
|
328
|
+
if (!out)
|
|
329
|
+
return undefined;
|
|
330
|
+
// Prove the other side of each explicit scope condition by its contradictory hypothesis.
|
|
331
|
+
for (let i = roots.length - 1; i >= 0; i--) {
|
|
332
|
+
const a = roots[i], prefix = new Map([...root].slice(0, i)), bad = put(prefix, a.lin, !a.holds);
|
|
333
|
+
const rows = leafConstraints(scoped, bad), absurd = rows.findIndex(r => Object.keys(r.lin).every(k => k === '1') && (r.lin['1'] ?? 0) > 0);
|
|
334
|
+
let opposite;
|
|
335
|
+
if (absurd >= 0)
|
|
336
|
+
opposite = { leaf: [{ kind: 'input', index: absurd }] };
|
|
337
|
+
else {
|
|
338
|
+
const left = rows.findIndex(r => canon(r.lin) === canon(a.lin)), right = rows.findIndex(r => canon(r.lin) === canon(flip(a.lin)));
|
|
339
|
+
if (left < 0 || right < 0)
|
|
340
|
+
return undefined;
|
|
341
|
+
opposite = { leaf: [{ kind: 'input', index: left }, { kind: 'input', index: right }, { kind: 'combine', a: 0, b: 1, ma: 1, mb: 1 }] };
|
|
342
|
+
}
|
|
343
|
+
out = a.holds ? { split: a.lin, yes: out, no: opposite } : { split: a.lin, yes: opposite, no: out };
|
|
344
|
+
}
|
|
345
|
+
const count = (t) => 'leaf' in t ? 1 : 1 + count(t.yes) + count(t.no);
|
|
346
|
+
const after = count(out) + lemmas.reduce((n, l) => n + count(l.tree), 0);
|
|
347
|
+
const workBefore = before * Math.max(1, goal.hyps.length), workAfter = count(out) * Math.max(1, scoped.hyps.length) + lemmas.reduce((n, l) => n + count(l.tree) * Math.max(1, l.slice?.length ?? scoped.hyps.length), 0);
|
|
348
|
+
if (workAfter >= workBefore || performance.now() >= until) {
|
|
349
|
+
options.trace?.({ stage: 'not-cheaper-or-expired', before, after, workBefore, workAfter });
|
|
350
|
+
return undefined;
|
|
351
|
+
}
|
|
352
|
+
return { goal: scoped, tree: out, lemmas, stats: { before, after, cuts: lemmas.length, sourceHyps: goal.hyps.length, lemmaHyps: lemmas.reduce((n, l) => n + (l.slice?.length ?? scoped.hyps.length), 0), mainNodes: count(out), checkMs: 0, buildMs: performance.now() - started } };
|
|
353
|
+
}
|
|
354
|
+
export function shareLemmaCuts(goal, tree, options = {}) {
|
|
355
|
+
const proposed = proposeLemmaCuts(goal, tree, options);
|
|
356
|
+
if (!proposed)
|
|
357
|
+
return undefined;
|
|
358
|
+
const start = performance.now(), checked = checkGoalLemmas(proposed.goal, proposed.tree, proposed.lemmas);
|
|
359
|
+
if (!checked) {
|
|
360
|
+
options?.trace?.({ stage: 'kernel-rejected' });
|
|
361
|
+
return undefined;
|
|
362
|
+
}
|
|
363
|
+
proposed.stats.checkMs = performance.now() - start;
|
|
364
|
+
return proposed;
|
|
365
|
+
}
|
package/dist/core/machine.d.ts
CHANGED
|
@@ -101,7 +101,42 @@ export type Fn = {
|
|
|
101
101
|
recur?: Recurrence;
|
|
102
102
|
arity?: number;
|
|
103
103
|
call?: string;
|
|
104
|
+
direct?: DirectFn;
|
|
104
105
|
};
|
|
106
|
+
type DirectImplementation = {
|
|
107
|
+
kind: 'div';
|
|
108
|
+
divisor: number;
|
|
109
|
+
} | {
|
|
110
|
+
kind: 'mod';
|
|
111
|
+
divisor: number;
|
|
112
|
+
} | {
|
|
113
|
+
kind: 'const';
|
|
114
|
+
value: number;
|
|
115
|
+
} | {
|
|
116
|
+
kind: 'recur';
|
|
117
|
+
name: string;
|
|
118
|
+
args: string[];
|
|
119
|
+
zero: E;
|
|
120
|
+
next: E;
|
|
121
|
+
uses: Record<string, Fn>;
|
|
122
|
+
};
|
|
123
|
+
export type DirectFn = DirectImplementation & {
|
|
124
|
+
source: {
|
|
125
|
+
js: string;
|
|
126
|
+
arity: number;
|
|
127
|
+
call?: string;
|
|
128
|
+
descriptor: string;
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
export declare const bindDirect: (fn: Pick<Fn, "js" | "arity" | "call">, direct: DirectImplementation) => DirectFn;
|
|
132
|
+
type DirectSource = {
|
|
133
|
+
js: string | ((...args: number[]) => number);
|
|
134
|
+
arity?: number;
|
|
135
|
+
call?: string;
|
|
136
|
+
direct?: DirectFn;
|
|
137
|
+
};
|
|
138
|
+
export declare const directOf: (fn: DirectSource) => DirectFn | undefined;
|
|
139
|
+
export declare const sourceFreeFn: (fn: DirectSource) => boolean;
|
|
105
140
|
export type Recurrence = {
|
|
106
141
|
args: string[];
|
|
107
142
|
zero: E;
|
|
@@ -238,6 +273,7 @@ export declare const partsOf: (z: number, n: number) => number[];
|
|
|
238
273
|
export declare const naturalIn: (v: unknown, what: string) => number;
|
|
239
274
|
export declare function evalE(e: E, env: Env, st: State, m: Machine): number;
|
|
240
275
|
export declare const fnValue: (fn: Fn, key: number) => number;
|
|
276
|
+
export declare const fnCall: (fn: Fn, args: number[]) => number;
|
|
241
277
|
export declare const pairE: (a: E, b: E) => E;
|
|
242
278
|
export declare const argsKey: (args: E[]) => E;
|
|
243
279
|
export declare function pairedArgs(key: E, n: number): E[] | undefined;
|
|
@@ -245,9 +281,9 @@ export declare function evalC(c: C, env: Env, st: State, m: Machine): boolean;
|
|
|
245
281
|
export declare function compileE(e: E, m: Machine): (env: Env, st: State) => number;
|
|
246
282
|
export declare function compileC(c: C, m: Machine): (env: Env, st: State) => boolean;
|
|
247
283
|
export declare function leastsIn(m: Machine, env: Env, st: State): Env;
|
|
248
|
-
export declare function carriedView(m: Machine, st: State, input: Request, rename?: (map: string) => string, params?: Env): State;
|
|
284
|
+
export declare function carriedView(m: Machine, st: State, input: Request, rename?: (map: string) => string, params?: Env, checkpoint?: () => void): State;
|
|
249
285
|
export declare const numbersOf: (input: Request) => Record<string, number>;
|
|
250
|
-
export declare function interpret(m: Machine): {
|
|
286
|
+
export declare function interpret(m: Machine, checkpoint?: () => void): {
|
|
251
287
|
init: (params: Env) => State;
|
|
252
288
|
queries: (params: Env, st: State, input: Request) => Env;
|
|
253
289
|
step: (params: Env, given: State, input: Request) => State;
|
|
@@ -262,6 +298,7 @@ export type Compiled = {
|
|
|
262
298
|
step: (params: object, st: State, input: object) => State & {
|
|
263
299
|
head?: number;
|
|
264
300
|
};
|
|
301
|
+
cells?: (params: object, st: State, input: object, tick: () => void) => Record<string, Map<number, number>>;
|
|
265
302
|
};
|
|
266
303
|
export declare const loadJs: (m: Machine) => Compiled;
|
|
267
304
|
export declare function fastStep(m: Machine): ((params: Env, st: State, input: Request) => State) | undefined;
|