gdp-wired 0.47.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 +15 -4
- package/dist/core/carry.d.ts +15 -0
- package/dist/core/carry.js +651 -0
- package/dist/core/cli.d.ts +3 -1
- package/dist/core/cli.js +28 -8
- 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.js +8 -1
- 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 +130 -13
- package/dist/core/extract-read.d.ts +22 -8
- package/dist/core/extract-read.js +29 -11
- package/dist/core/extract-shapes.js +122 -41
- package/dist/core/extract-standalone.js +53 -9
- package/dist/core/extract.js +9 -3
- 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 +47 -14
- 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/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,175 @@
|
|
|
1
|
+
// Untrusted linear recipe transport through explicit count-read equalities.
|
|
2
|
+
// Unlike widen's fallback this never launches a fresh whole-goal search.
|
|
3
|
+
// A deferred result is only a proposal: its composing caller must check the
|
|
4
|
+
// complete tree against the original goal, including these transported leaves.
|
|
5
|
+
import { walkOnce } from "../machine.js";
|
|
6
|
+
import { add, scale, sub } from "../solver.js";
|
|
7
|
+
import { canon, columnTerm, linear, substE } from "../../kernel/kernel.js";
|
|
8
|
+
import { wallTree, solve, check, rowsAt } from "./ops.js";
|
|
9
|
+
export const liftCountProxies = (...args) => wallTree('transport', () => liftCountProxiesImpl(...args));
|
|
10
|
+
function liftCountProxiesImpl(part, whole, tree, reads, pp = new Map(), wp = new Map(), diagnose, options = {}) {
|
|
11
|
+
const aliases = new Map(reads.map(([id, map, key]) => [id, { op: 'get', map, key }]));
|
|
12
|
+
const substitutions = Object.fromEntries(aliases), terms = new Map();
|
|
13
|
+
const remapTerm = (name) => {
|
|
14
|
+
if (terms.has(name))
|
|
15
|
+
return terms.get(name);
|
|
16
|
+
const term = columnTerm(name);
|
|
17
|
+
let uses = false;
|
|
18
|
+
if (term)
|
|
19
|
+
walkOnce(term, (o) => { if (o.op === 'var' && aliases.has(o.name))
|
|
20
|
+
uses = true; });
|
|
21
|
+
const mapped = term && uses ? substE(term, { vars: substitutions, maps: {}, flags: {} }) : undefined;
|
|
22
|
+
terms.set(name, mapped);
|
|
23
|
+
return mapped;
|
|
24
|
+
};
|
|
25
|
+
const replace = (lin, path) => {
|
|
26
|
+
let out = {};
|
|
27
|
+
for (const [name, coefficient] of Object.entries(lin)) {
|
|
28
|
+
const e = aliases.get(name) ?? remapTerm(name), value = e ? linear(e, path) : { [name]: 1 };
|
|
29
|
+
if (!value || !e && [...aliases.keys()].some(id => name.includes(id)))
|
|
30
|
+
return undefined;
|
|
31
|
+
out = add(out, scale(value, coefficient));
|
|
32
|
+
}
|
|
33
|
+
return out;
|
|
34
|
+
};
|
|
35
|
+
const go = (t, source, target) => {
|
|
36
|
+
if (!('leaf' in t)) {
|
|
37
|
+
const split = replace(t.split, target);
|
|
38
|
+
if (!split)
|
|
39
|
+
return undefined;
|
|
40
|
+
const id = canon(split), known = target.get(id)?.holds;
|
|
41
|
+
if (known !== undefined)
|
|
42
|
+
return go(known ? t.yes : t.no, new Map(source).set(canon(t.split), { lin: t.split, holds: known }), target);
|
|
43
|
+
const yes = go(t.yes, new Map(source).set(canon(t.split), { lin: t.split, holds: true }), new Map(target).set(id, { lin: split, holds: true }));
|
|
44
|
+
const no = yes && go(t.no, new Map(source).set(canon(t.split), { lin: t.split, holds: false }), new Map(target).set(id, { lin: split, holds: false }));
|
|
45
|
+
return yes && no ? { split, yes, no } : undefined;
|
|
46
|
+
}
|
|
47
|
+
if (t.products?.length || t.squares?.length)
|
|
48
|
+
return undefined;
|
|
49
|
+
const rp = rowsAt(part, source), rw = rowsAt(whole, target), exact = new Map(), normalized = new Map(), columns = new Map();
|
|
50
|
+
for (const [id, e] of aliases) {
|
|
51
|
+
const l = linear(e, target);
|
|
52
|
+
if (!l)
|
|
53
|
+
return undefined;
|
|
54
|
+
const entries = Object.entries(l);
|
|
55
|
+
if (entries.length !== 1 || entries[0][1] !== 1)
|
|
56
|
+
return undefined;
|
|
57
|
+
columns.set(entries[0][0], id);
|
|
58
|
+
}
|
|
59
|
+
const normalize = (lin) => { let out = {}; for (const [name, c] of Object.entries(lin))
|
|
60
|
+
out = add(out, { [columns.get(name) ?? name]: c }); return out; };
|
|
61
|
+
rw.forEach((r, i) => { const id = canon(r.lin), norm = canon(normalize(r.lin)); if (!exact.has(id))
|
|
62
|
+
exact.set(id, i); if (!normalized.has(norm))
|
|
63
|
+
normalized.set(norm, i); });
|
|
64
|
+
const steps = [], indices = [];
|
|
65
|
+
const input = (index) => { steps.push({ kind: 'input', index }); return steps.length - 1; };
|
|
66
|
+
for (const s of t.leaf) {
|
|
67
|
+
if (s.kind === 'input') {
|
|
68
|
+
const row = rp[s.index];
|
|
69
|
+
if (!row)
|
|
70
|
+
return undefined;
|
|
71
|
+
const desired = replace(row.lin, target);
|
|
72
|
+
if (!desired)
|
|
73
|
+
return undefined;
|
|
74
|
+
const direct = exact.get(canon(desired));
|
|
75
|
+
if (direct !== undefined) {
|
|
76
|
+
indices.push(input(direct));
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
// A retained read binding becomes 0 <= 0 under the substitution. The
|
|
80
|
+
// recipe language requires positive weights, so use opposite source
|
|
81
|
+
// equality rows rather than a zero-weight combine or a new axiom.
|
|
82
|
+
if (!Object.keys(desired).length) {
|
|
83
|
+
const a = rw.findIndex(r => exact.has(canon(scale(r.lin, -1))));
|
|
84
|
+
if (a < 0) {
|
|
85
|
+
diagnose?.({ reason: 'missing-zero-row' });
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
const b = exact.get(canon(scale(rw[a].lin, -1))), left = input(a), right = input(b);
|
|
89
|
+
steps.push({ kind: 'combine', a: left, b: right, ma: 1, mb: 1 });
|
|
90
|
+
indices.push(steps.length - 1);
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
const base = normalized.get(canon(row.lin));
|
|
94
|
+
if (base === undefined) {
|
|
95
|
+
diagnose?.({ reason: 'missing-row', row: row.lin, desired });
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
let at = input(base), value = rw[base].lin;
|
|
99
|
+
for (const [id, e] of aliases) {
|
|
100
|
+
const c = value[id] ?? 0;
|
|
101
|
+
if (!c)
|
|
102
|
+
continue;
|
|
103
|
+
const l = linear(e, target);
|
|
104
|
+
if (!l)
|
|
105
|
+
return undefined;
|
|
106
|
+
const bridge = sub(l, { [id]: 1 }), signed = c > 0 ? bridge : scale(bridge, -1), r = exact.get(canon(signed));
|
|
107
|
+
if (r === undefined) {
|
|
108
|
+
diagnose?.({ reason: 'missing-binding', id, row: signed });
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
const b = input(r);
|
|
112
|
+
steps.push({ kind: 'combine', a: at, b, ma: 1, mb: Math.abs(c) });
|
|
113
|
+
at = steps.length - 1;
|
|
114
|
+
value = add(value, scale(bridge, c));
|
|
115
|
+
}
|
|
116
|
+
if (canon(value) !== canon(desired))
|
|
117
|
+
return undefined;
|
|
118
|
+
indices.push(at);
|
|
119
|
+
}
|
|
120
|
+
else if (s.kind === 'combine') {
|
|
121
|
+
const a = indices[s.a], b = indices[s.b];
|
|
122
|
+
if (a === undefined || b === undefined)
|
|
123
|
+
return undefined;
|
|
124
|
+
steps.push({ ...s, a, b });
|
|
125
|
+
indices.push(steps.length - 1);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
const of = indices[s.of];
|
|
129
|
+
if (of === undefined)
|
|
130
|
+
return undefined;
|
|
131
|
+
steps.push({ ...s, of });
|
|
132
|
+
indices.push(steps.length - 1);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return { leaf: steps };
|
|
136
|
+
};
|
|
137
|
+
const guards = new Map();
|
|
138
|
+
walkOnce(whole.hyps, (c) => {
|
|
139
|
+
if (c.op !== 'or' || c.xs.length !== 2)
|
|
140
|
+
return;
|
|
141
|
+
const eq = c.xs.find((x) => x.op === '=' && x.a.op === 'var' && aliases.has(x.a.name)), guard = c.xs.find((x) => x !== eq);
|
|
142
|
+
if (!eq || guard?.op !== '<=')
|
|
143
|
+
return;
|
|
144
|
+
const a = linear(guard.a, wp), b = linear(guard.b, wp);
|
|
145
|
+
if (!a || !b)
|
|
146
|
+
return;
|
|
147
|
+
const lin = sub(a, b);
|
|
148
|
+
if (Object.keys(lin).every(k => k === '1') && (lin['1'] ?? 0) > 0)
|
|
149
|
+
return;
|
|
150
|
+
guards.set(canon(lin), lin);
|
|
151
|
+
});
|
|
152
|
+
const all = [...guards.values()];
|
|
153
|
+
const open = (i, path) => {
|
|
154
|
+
if (i === all.length)
|
|
155
|
+
return go(tree, pp, path);
|
|
156
|
+
const split = all[i], id = canon(split), known = path.get(id)?.holds;
|
|
157
|
+
if (known === false)
|
|
158
|
+
return open(i + 1, path);
|
|
159
|
+
const badPath = new Map(path).set(id, { lin: split, holds: true }), out = solve(rowsAt(whole, badPath), 2000);
|
|
160
|
+
if (out.kind !== 'refuted') {
|
|
161
|
+
diagnose?.({ reason: 'domain-not-refuted', split });
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
const yes = { leaf: out.steps };
|
|
165
|
+
if (!options.deferCheck && !check(whole, yes, badPath))
|
|
166
|
+
return undefined;
|
|
167
|
+
if (known === true)
|
|
168
|
+
return yes;
|
|
169
|
+
const no = open(i + 1, new Map(path).set(id, { lin: split, holds: false }));
|
|
170
|
+
return no ? { split, yes, no } : undefined;
|
|
171
|
+
};
|
|
172
|
+
const out = open(0, wp), accepted = !!out && (options.deferCheck || check(whole, out, wp));
|
|
173
|
+
diagnose?.({ reason: 'complete', guards: all.length, checked: !options.deferCheck && accepted, ...(options.deferCheck ? { proposed: !!out, deferred: true } : {}) });
|
|
174
|
+
return accepted ? out : undefined;
|
|
175
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type C } from '../machine.js';
|
|
2
|
+
import { type Goal, type Path } from '../../kernel/kernel.js';
|
|
3
|
+
export declare function piecesAt(c: C, path: Path, neg?: boolean): C[];
|
|
4
|
+
export declare function sourceWitnessContext(source: Goal, core: Goal, wits: ReadonlySet<string>, path: Path): Goal;
|
|
5
|
+
export declare function sourceScalarContext(source: Goal, core: Goal, names: ReadonlySet<string>, path: Path): Goal;
|
|
6
|
+
export declare function walkLive(root: unknown, path: Path, visit: (value: any) => void): void;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { truth } from "../../kernel/kernel.js";
|
|
2
|
+
// Expose conjunctions hidden behind phase implications before dependency
|
|
3
|
+
// filtering. The actual terms are retained, so row transport need not invent
|
|
4
|
+
// identities between rewritten conditional-expression columns.
|
|
5
|
+
export function piecesAt(c, path, neg = false) {
|
|
6
|
+
const t = truth(c, path);
|
|
7
|
+
if (t !== undefined)
|
|
8
|
+
return t !== neg ? [] : [{ op: 'or', xs: [] }];
|
|
9
|
+
if (c.op === 'not')
|
|
10
|
+
return piecesAt(c.x, path, !neg);
|
|
11
|
+
if (c.op === 'and' && !neg || c.op === 'or' && neg)
|
|
12
|
+
return c.xs.flatMap(x => piecesAt(x, path, neg));
|
|
13
|
+
if (c.op === 'and' || c.op === 'or') {
|
|
14
|
+
const open = c.xs.filter(x => truth(x, path) === undefined);
|
|
15
|
+
if (open.length === 1)
|
|
16
|
+
return piecesAt(open[0], path, neg);
|
|
17
|
+
}
|
|
18
|
+
return [neg ? { op: 'not', x: c } : c];
|
|
19
|
+
}
|
|
20
|
+
// A proposed small core can omit a witness's source-side guard instance.
|
|
21
|
+
// Re-add only existing hypotheses about witnesses used by that core; never
|
|
22
|
+
// synthesize a guard premise or import an unrelated witness's condition.
|
|
23
|
+
export function sourceWitnessContext(source, core, wits, path) {
|
|
24
|
+
const active = new Set();
|
|
25
|
+
walkLive(core.hyps, path, o => { if (o.op === 'var' && wits.has(o.name))
|
|
26
|
+
active.add(o.name); });
|
|
27
|
+
if (!active.size)
|
|
28
|
+
return core;
|
|
29
|
+
const extra = source.hyps.filter(c => {
|
|
30
|
+
if (core.hyps.includes(c))
|
|
31
|
+
return false;
|
|
32
|
+
let wanted = false, foreign = false;
|
|
33
|
+
walkLive(c, path, o => { if (o.op === 'var' && wits.has(o.name)) {
|
|
34
|
+
if (active.has(o.name))
|
|
35
|
+
wanted = true;
|
|
36
|
+
else
|
|
37
|
+
foreign = true;
|
|
38
|
+
} });
|
|
39
|
+
return wanted && !foreign;
|
|
40
|
+
});
|
|
41
|
+
return extra.length ? { ...core, hyps: [...core.hyps, ...extra] } : core;
|
|
42
|
+
}
|
|
43
|
+
// Preserve source scalar domains in a small point core. A packed-row proof can
|
|
44
|
+
// need the original size/stride bound even when a numeric core omits it.
|
|
45
|
+
export function sourceScalarContext(source, core, names, path) {
|
|
46
|
+
const extra = source.hyps.flatMap(c => piecesAt(c, path)).filter(c => {
|
|
47
|
+
if (core.hyps.includes(c))
|
|
48
|
+
return false;
|
|
49
|
+
let scalar = true;
|
|
50
|
+
walkLive(c, path, (o) => { if (o.op === 'get' || o.op === 'flag' || o.op === 'all' || o.op === 'var' && !names.has(o.name))
|
|
51
|
+
scalar = false; });
|
|
52
|
+
return scalar;
|
|
53
|
+
});
|
|
54
|
+
return extra.length ? { ...core, hyps: [...core.hyps, ...extra] } : core;
|
|
55
|
+
}
|
|
56
|
+
export function walkLive(root, path, visit) {
|
|
57
|
+
const seen = new WeakSet();
|
|
58
|
+
const go = (value) => {
|
|
59
|
+
if (!value || typeof value !== 'object' || seen.has(value))
|
|
60
|
+
return;
|
|
61
|
+
seen.add(value);
|
|
62
|
+
if (['not', 'and', 'or', '<', '<=', '='].includes(value.op) && truth(value, path) !== undefined)
|
|
63
|
+
return;
|
|
64
|
+
if (value.op === 'if') {
|
|
65
|
+
const chosen = truth(value.c, path);
|
|
66
|
+
if (chosen !== undefined) {
|
|
67
|
+
go(chosen ? value.then : value.else);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
visit(value);
|
|
72
|
+
for (const child of Array.isArray(value) ? value : Object.values(value))
|
|
73
|
+
go(child);
|
|
74
|
+
};
|
|
75
|
+
go(root);
|
|
76
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type C, type E, type Machine } from '../machine.js';
|
|
2
|
+
import { type CountCert, type CountDecl } from '../../kernel/kernel.js';
|
|
3
|
+
export type MemberInclusion = {
|
|
4
|
+
positive: string;
|
|
5
|
+
negative: string;
|
|
6
|
+
carried: string;
|
|
7
|
+
below: E;
|
|
8
|
+
guards: C[];
|
|
9
|
+
};
|
|
10
|
+
export declare function preferMemberInclusions<T extends Pick<CountCert, 'reads' | 'insts'> & Partial<Pick<CountCert, 'picks'>>>(m: Machine, ds: CountDecl[], full: T, picked: T, cap?: number, more?: () => boolean): T;
|
|
11
|
+
export declare function memberInclusions(m: Machine, ds: CountDecl[], reads: CountCert['reads'], more?: () => boolean): MemberInclusion[];
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// Untrusted carried-set inclusions. Flag equalities suggest substitutions;
|
|
2
|
+
// upper bounds suggest sufficient scalar guards, not substitutions. Ordinary
|
|
3
|
+
// count witnesses and independent conditional cuts prove every result.
|
|
4
|
+
import { n, same, walkOnce, x } from "../machine.js";
|
|
5
|
+
import { substC, substE } from "../../kernel/kernel.js";
|
|
6
|
+
import { countsForReads } from "../count-facts.js";
|
|
7
|
+
import { bounded } from "./scalar-guards.js";
|
|
8
|
+
// The ordinary pool already contains many pairs, but model-driven selection can
|
|
9
|
+
// miss a useful inclusion when its target count is otherwise unconstrained.
|
|
10
|
+
// Prefer only existing witness instances suggested by the flag syntax. Their
|
|
11
|
+
// scalar guards are not asserted: mcGoal must still prove every witness branch.
|
|
12
|
+
export function preferMemberInclusions(m, ds, full, picked, cap = 10, more) {
|
|
13
|
+
const room = cap - (picked.picks?.length ?? 0);
|
|
14
|
+
if (room <= 0 || picked.insts.length > room || !m.inputMaps?.length || !full.insts.length)
|
|
15
|
+
return picked;
|
|
16
|
+
const vector = (q) => text([q.unit, q.below, [...q.terms].sort((a, b) => a[1].localeCompare(b[1]) || a[0] - b[0])]);
|
|
17
|
+
const available = new Map(full.insts.map(q => [vector(q), q])), preferred = [];
|
|
18
|
+
for (const p of memberInclusions(m, ds, full.reads, more)) {
|
|
19
|
+
const q = available.get(vector({ unit: 0, below: p.below, terms: [[1, p.positive], [-1, p.negative]] }));
|
|
20
|
+
if (q)
|
|
21
|
+
preferred.push(q);
|
|
22
|
+
}
|
|
23
|
+
if (!preferred.length)
|
|
24
|
+
return picked;
|
|
25
|
+
const seen = new Set(), insts = [...preferred, ...picked.insts].filter(q => { const k = vector(q); if (seen.has(k))
|
|
26
|
+
return false; seen.add(k); return true; }).slice(0, room);
|
|
27
|
+
return { ...picked, insts };
|
|
28
|
+
}
|
|
29
|
+
const text = (v) => JSON.stringify(v);
|
|
30
|
+
const member = (c) => {
|
|
31
|
+
if (c.op === '=' && c.a.op === 'get' && c.b.op === 'const' && c.b.value === 1)
|
|
32
|
+
return c.a;
|
|
33
|
+
if (c.op === '=' && c.b.op === 'get' && c.a.op === 'const' && c.a.value === 1)
|
|
34
|
+
return c.b;
|
|
35
|
+
if (c.op === '<' && c.a.op === 'const' && c.a.value === 0 && c.b.op === 'get')
|
|
36
|
+
return c.b;
|
|
37
|
+
if (c.op === '<=' && c.a.op === 'const' && c.a.value === 1 && c.b.op === 'get')
|
|
38
|
+
return c.b;
|
|
39
|
+
return undefined;
|
|
40
|
+
};
|
|
41
|
+
const predicate = (e) => e.op === 'if' && e.then.op === 'const' && e.then.value === 1 && e.else.op === 'const' && e.else.value === 0 ? e.c : e.op === 'get' ? same(e, n(1)) : undefined;
|
|
42
|
+
const parts = (c) => c.op === 'and' ? c.xs.flatMap(parts) : [c];
|
|
43
|
+
export function memberInclusions(m, ds, reads, more) {
|
|
44
|
+
if (more && !more())
|
|
45
|
+
return [];
|
|
46
|
+
const picked = reads.filter(([, map]) => !map.endsWith('#')), names = new Set(picked.map(r => r[1])), decls = ds.filter(d => names.has(d.map));
|
|
47
|
+
if (!m.inputMaps?.length || !bounded([m.step.flags, decls, picked]))
|
|
48
|
+
return [];
|
|
49
|
+
const counts = countsForReads(decls, picked);
|
|
50
|
+
if (!counts)
|
|
51
|
+
return [];
|
|
52
|
+
const used = new Set();
|
|
53
|
+
walkOnce([m.step.flags, decls, picked], (o) => { if (o.op === 'var')
|
|
54
|
+
used.add(o.name); });
|
|
55
|
+
let key = 'inclusion$member';
|
|
56
|
+
while (used.has(key))
|
|
57
|
+
key += '$';
|
|
58
|
+
const mentions = (v) => { let found = false; walkOnce(v, (o) => { if (o.op === 'var' && o.name === key)
|
|
59
|
+
found = true; }); return found; };
|
|
60
|
+
const inputs = new Set(m.inputMaps), facts = new Map();
|
|
61
|
+
for (const q of Object.values(m.step.flags ?? {})) {
|
|
62
|
+
if (more && !more())
|
|
63
|
+
return [];
|
|
64
|
+
if (q.op !== 'all' || q.keys?.length)
|
|
65
|
+
continue;
|
|
66
|
+
const c = substC(q.c, { vars: { [q.key]: x(key) }, maps: {}, flags: {} });
|
|
67
|
+
if (c.op !== 'or' || c.xs.length !== 2)
|
|
68
|
+
continue;
|
|
69
|
+
for (let i = 0; i < 2; i++) {
|
|
70
|
+
const a = c.xs[i], source = a.op === 'not' ? member(a.x) : undefined;
|
|
71
|
+
if (!source || !inputs.has(source.map) || source.key.op !== 'var' || source.key.name !== key)
|
|
72
|
+
continue;
|
|
73
|
+
const entry = facts.get(source.map) ?? { known: new Set(), conditions: new Set(), anchors: new Map(), upper: new Map() };
|
|
74
|
+
for (const consequent of parts(c.xs[1 - i])) {
|
|
75
|
+
entry.conditions.add(text(consequent));
|
|
76
|
+
const membership = member(consequent);
|
|
77
|
+
if (membership)
|
|
78
|
+
entry.known.add(text(membership));
|
|
79
|
+
if ((consequent.op === '<' || consequent.op === '<=') && consequent.a.op === 'get' && mentions(consequent.a.key) && !mentions(consequent.b) && !entry.upper.has(text(consequent.a)))
|
|
80
|
+
entry.upper.set(text(consequent.a), { bound: consequent.b, strict: consequent.op === '<' });
|
|
81
|
+
if (consequent.op !== '=')
|
|
82
|
+
continue;
|
|
83
|
+
for (const [from, to] of [[consequent.a, consequent.b], [consequent.b, consequent.a]]) {
|
|
84
|
+
if (from.op === 'get' && to.op === 'get' && mentions(from.key) && !mentions(to) && !entry.anchors.has(text(from)))
|
|
85
|
+
entry.anchors.set(text(from), to);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
facts.set(source.map, entry);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const out = [], seen = new Set();
|
|
92
|
+
let work = 20000;
|
|
93
|
+
for (const a of counts) {
|
|
94
|
+
const body = substE(a.body, { vars: { [a.key]: x(key) }, maps: {}, flags: {} }), p = predicate(body), source = p && member(p);
|
|
95
|
+
if (!source || !inputs.has(source.map) || source.key.op !== 'var' || source.key.name !== key)
|
|
96
|
+
continue;
|
|
97
|
+
const entry = facts.get(source.map);
|
|
98
|
+
if (!entry || !entry.anchors.size && !entry.upper.size)
|
|
99
|
+
continue;
|
|
100
|
+
const known = new Set([...entry.known, text(source)]);
|
|
101
|
+
for (const b of counts) {
|
|
102
|
+
if (--work < 0 || out.length >= 128 || more && !more())
|
|
103
|
+
return out;
|
|
104
|
+
if (a.v === b.v || text(a.below) !== text(b.below))
|
|
105
|
+
continue;
|
|
106
|
+
const target = predicate(substE(b.body, { vars: { [b.key]: x(key) }, maps: {}, flags: {} }));
|
|
107
|
+
if (!target)
|
|
108
|
+
continue;
|
|
109
|
+
let changed = false;
|
|
110
|
+
const maps = Object.fromEntries([...new Set([...entry.anchors.keys()].map(k => JSON.parse(k).map))].map(map => [map, (at) => {
|
|
111
|
+
const from = { op: 'get', map, key: at }, to = entry.anchors.get(text(from));
|
|
112
|
+
if (to) {
|
|
113
|
+
changed = true;
|
|
114
|
+
return to;
|
|
115
|
+
}
|
|
116
|
+
return from;
|
|
117
|
+
}]));
|
|
118
|
+
const rewritten = substC(target, { vars: {}, maps, flags: {} });
|
|
119
|
+
const simplify = (c, positive = true) => {
|
|
120
|
+
if (entry.conditions.has(text(c)))
|
|
121
|
+
return true;
|
|
122
|
+
const view = member(c);
|
|
123
|
+
if (view && known.has(text(view)))
|
|
124
|
+
return true;
|
|
125
|
+
if (c.op === 'and' || c.op === 'or') {
|
|
126
|
+
const xs = c.xs.map(q => simplify(q, positive)), and = c.op === 'and';
|
|
127
|
+
if (xs.includes(!and))
|
|
128
|
+
return !and;
|
|
129
|
+
const rest = xs.filter(v => typeof v !== 'boolean');
|
|
130
|
+
return !rest.length ? and : rest.length === 1 ? rest[0] : { op: c.op, xs: rest };
|
|
131
|
+
}
|
|
132
|
+
if (c.op === 'not') {
|
|
133
|
+
const v = simplify(c.x, !positive);
|
|
134
|
+
return typeof v === 'boolean' ? !v : { op: 'not', x: v };
|
|
135
|
+
}
|
|
136
|
+
// An upper bound is not an equality substitution. In a positive
|
|
137
|
+
// comparison only, its scalar bound gives a sufficient guard. A strict
|
|
138
|
+
// source bound u < U only needs U <= B to prove the strict target u < B.
|
|
139
|
+
if (positive && (c.op === '<' || c.op === '<=')) {
|
|
140
|
+
const upper = entry.upper.get(text(c.a));
|
|
141
|
+
if (upper) {
|
|
142
|
+
c = { ...c, op: upper.strict ? '<=' : c.op, a: upper.bound };
|
|
143
|
+
changed = true;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (c.op === '=' || c.op === '<' || c.op === '<=') {
|
|
147
|
+
if (text(c.a) === text(c.b))
|
|
148
|
+
return c.op !== '<';
|
|
149
|
+
if (c.a.op === 'const' && c.b.op === 'const')
|
|
150
|
+
return c.op === '=' ? c.a.value === c.b.value : c.op === '<' ? c.a.value < c.b.value : c.a.value <= c.b.value;
|
|
151
|
+
}
|
|
152
|
+
return c;
|
|
153
|
+
};
|
|
154
|
+
const reduced = simplify(rewritten);
|
|
155
|
+
if (!changed || reduced === false || typeof reduced !== 'boolean' && mentions(reduced))
|
|
156
|
+
continue;
|
|
157
|
+
const guards = reduced === true ? [] : parts(reduced);
|
|
158
|
+
if (guards.some(c => !['=', '<', '<='].includes(c.op)))
|
|
159
|
+
continue;
|
|
160
|
+
const id = text([a.v, b.v, guards]);
|
|
161
|
+
if (seen.has(id))
|
|
162
|
+
continue;
|
|
163
|
+
seen.add(id);
|
|
164
|
+
out.push({ positive: a.v, negative: b.v, carried: source.map, below: a.below, guards });
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return out;
|
|
168
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type E, type Law, type Machine } from '../machine.js';
|
|
2
|
+
import { type CountCert, type CountDecl, type CountInst, type Instances } from '../../kernel/kernel.js';
|
|
3
|
+
import { type MemberInclusion } from './member-inclusions.js';
|
|
4
|
+
type Packet = Pick<CountCert, 'reads' | 'insts'>;
|
|
5
|
+
export type MemberHint = MemberInclusion & {
|
|
6
|
+
instance: CountInst;
|
|
7
|
+
reads: CountCert['reads'];
|
|
8
|
+
};
|
|
9
|
+
export declare function memberPremiseState(source: unknown): {
|
|
10
|
+
used: Set<string>;
|
|
11
|
+
witnesses: Map<string, string>;
|
|
12
|
+
admissions: Map<string, number>;
|
|
13
|
+
serial: number;
|
|
14
|
+
};
|
|
15
|
+
type State = ReturnType<typeof memberPremiseState>;
|
|
16
|
+
export declare function memberHints(m: Machine, decls: CountDecl[], cert: Packet, more?: () => boolean): MemberHint[];
|
|
17
|
+
export declare function admitMemberWitnesses<T extends Packet>(m: Machine, decls: CountDecl[], cert: T, state: State, cap: number, more?: () => boolean): {
|
|
18
|
+
cert: T;
|
|
19
|
+
hints: MemberHint[];
|
|
20
|
+
};
|
|
21
|
+
export declare function instantiateMemberPremises(m: Machine, laws: Law[], at: Instances, decls: CountDecl[], hints: MemberHint[], state: State, cap: number, more?: () => boolean): {
|
|
22
|
+
at: Instances;
|
|
23
|
+
added: {
|
|
24
|
+
law: string;
|
|
25
|
+
tuple: E[];
|
|
26
|
+
kind: "flag" | "law";
|
|
27
|
+
}[];
|
|
28
|
+
};
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// Ordinary member-count witnesses and instances of existing universal premises.
|
|
2
|
+
// Nothing here asserts a scalar guard or a flag body. The machine checker
|
|
3
|
+
// reconstructs every added tuple from the original law/flag declaration.
|
|
4
|
+
import { walkOnce } from "../machine.js";
|
|
5
|
+
import { countsForReads } from "../count-facts.js";
|
|
6
|
+
import { fieldLawBridges } from "../count-focused.js";
|
|
7
|
+
import { seedCountWitnessFlags } from "../count-witness-flags.js";
|
|
8
|
+
import { memberInclusions } from "./member-inclusions.js";
|
|
9
|
+
export function memberPremiseState(source) {
|
|
10
|
+
const used = new Set();
|
|
11
|
+
walkOnce(source, (o) => { for (const [key, value] of Object.entries(o)) {
|
|
12
|
+
used.add(key);
|
|
13
|
+
if (typeof value === 'string')
|
|
14
|
+
used.add(value);
|
|
15
|
+
} });
|
|
16
|
+
return { used, witnesses: new Map(), admissions: new Map(), serial: 0 };
|
|
17
|
+
}
|
|
18
|
+
const text = (value) => JSON.stringify(value);
|
|
19
|
+
const vector = (q) => text([q.unit, q.below, [...q.terms].sort((a, b) => a[1].localeCompare(b[1]) || a[0] - b[0])]);
|
|
20
|
+
const nodeCount = (body) => { let n = 0; walkOnce(body, (o) => { if (o.op)
|
|
21
|
+
n++; }); return n; };
|
|
22
|
+
// Put the strongest-shaped target bodies first; this is ordering, not a proof
|
|
23
|
+
// of implication. All entries remain ordinary CInsts under the existing cap.
|
|
24
|
+
export function memberHints(m, decls, cert, more) {
|
|
25
|
+
const bodies = new Map((countsForReads(decls, cert.reads) ?? []).map(c => [c.v, nodeCount(c.body)])), available = new Map(cert.insts.map(q => [vector(q), q]));
|
|
26
|
+
return memberInclusions(m, decls, cert.reads, more).sort((a, b) => (bodies.get(b.negative) ?? 0) - (bodies.get(a.negative) ?? 0)).flatMap(p => {
|
|
27
|
+
const instance = available.get(vector({ unit: 0, below: p.below, terms: [[1, p.positive], [-1, p.negative]] }));
|
|
28
|
+
return instance ? [{ ...p, instance, reads: cert.reads.filter(r => r[0] === p.positive || r[0] === p.negative) }] : [];
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
export function admitMemberWitnesses(m, decls, cert, state, cap, more = () => true) {
|
|
32
|
+
const candidates = memberInclusions(m, decls, cert.reads, more);
|
|
33
|
+
if (!candidates.length || cap <= 0)
|
|
34
|
+
return { cert, hints: [] };
|
|
35
|
+
const bodies = new Map((countsForReads(decls, cert.reads) ?? []).map(c => [c.v, nodeCount(c.body)])), reads = new Map(cert.reads.map(r => [r[0], r])), ds = new Map(decls.map(d => [d.map, d]));
|
|
36
|
+
candidates.sort((a, b) => (bodies.get(b.negative) ?? 0) - (bodies.get(a.negative) ?? 0));
|
|
37
|
+
walkOnce([decls, cert], (o) => { if (o.op === 'var')
|
|
38
|
+
state.used.add(o.name); });
|
|
39
|
+
for (const r of cert.reads)
|
|
40
|
+
state.used.add(r[0]);
|
|
41
|
+
for (const q of cert.insts)
|
|
42
|
+
state.used.add(q.wit);
|
|
43
|
+
const hints = [];
|
|
44
|
+
for (const p of candidates) {
|
|
45
|
+
if (hints.length >= Math.min(cap, 10) || !more())
|
|
46
|
+
break;
|
|
47
|
+
const a = reads.get(p.positive), b = reads.get(p.negative), key = text([a.slice(1), b.slice(1), p.below, ds.get(a[1]), ds.get(b[1])]);
|
|
48
|
+
let wit = state.witnesses.get(key);
|
|
49
|
+
if (!wit) {
|
|
50
|
+
do {
|
|
51
|
+
wit = `member$${state.serial++}`;
|
|
52
|
+
} while (state.used.has(wit));
|
|
53
|
+
state.used.add(wit);
|
|
54
|
+
state.witnesses.set(key, wit);
|
|
55
|
+
}
|
|
56
|
+
hints.push({ ...p, reads: [a, b], instance: { wit, unit: 0, below: p.below, terms: [[1, p.positive], [-1, p.negative]] } });
|
|
57
|
+
}
|
|
58
|
+
const seen = new Set(), insts = [...hints.map(h => h.instance), ...cert.insts].filter(q => { const k = vector(q); if (seen.has(k))
|
|
59
|
+
return false; seen.add(k); return true; }).slice(0, cap);
|
|
60
|
+
return { cert: { ...cert, insts, ...('wits' in cert ? { wits: insts.map(q => q.wit) } : {}) }, hints: hints.filter(h => insts.includes(h.instance)) };
|
|
61
|
+
}
|
|
62
|
+
// An empty scalar-law proposal list seeds only member flags and preserves
|
|
63
|
+
// every previously learned tuple; model-driven field instantiation is separate.
|
|
64
|
+
export function instantiateMemberPremises(m, laws, at, decls, hints, state, cap, more = () => true) {
|
|
65
|
+
let next = at;
|
|
66
|
+
const added = [];
|
|
67
|
+
const add = (name, tuple, kind) => {
|
|
68
|
+
if (!tuple.length || added.length >= cap || (state.admissions.get(name) ?? 0) >= 4 || !more())
|
|
69
|
+
return;
|
|
70
|
+
const old = next[name] ?? [], width = tuple.length;
|
|
71
|
+
if (old.length % width || Array.from({ length: old.length / width }, (_, i) => text(old.slice(i * width, (i + 1) * width))).includes(text(tuple)))
|
|
72
|
+
return;
|
|
73
|
+
if (next === at)
|
|
74
|
+
next = { ...at };
|
|
75
|
+
next[name] = [...old, ...tuple];
|
|
76
|
+
state.admissions.set(name, (state.admissions.get(name) ?? 0) + 1);
|
|
77
|
+
added.push({ law: name, tuple, kind });
|
|
78
|
+
};
|
|
79
|
+
for (const hint of hints) {
|
|
80
|
+
if (added.length >= cap || !more())
|
|
81
|
+
break;
|
|
82
|
+
// Supplying the declaration-backed reads preserves packed bounds and source
|
|
83
|
+
// membership. seedCountWitnessFlags excludes recursive count-bearing flags.
|
|
84
|
+
const packetReads = hint.reads;
|
|
85
|
+
if (packetReads.length !== 2)
|
|
86
|
+
continue;
|
|
87
|
+
const seeded = seedCountWitnessFlags(m, next, [{ decls, cert: { reads: packetReads, insts: [hint.instance] } }], true, { carriedOnly: true });
|
|
88
|
+
for (const [name, terms] of Object.entries(seeded))
|
|
89
|
+
for (const term of terms.slice((next[name] ?? []).length))
|
|
90
|
+
add(name, [term], 'flag');
|
|
91
|
+
const maps = new Set(), keys = new Map();
|
|
92
|
+
walkOnce(hint.guards, (o) => { if (o.op === 'get') {
|
|
93
|
+
maps.add(o.map);
|
|
94
|
+
keys.set(text(o.key), o.key);
|
|
95
|
+
} });
|
|
96
|
+
const sourceLaws = laws.filter(l => { if (!l.forall || l.some?.length || (l.keys?.length ?? 0) > 1)
|
|
97
|
+
return false; let counted = false, overlap = 0; const seen = new Set(); walkOnce(l.holds, (o) => { if (o.op === 'get') {
|
|
98
|
+
if (m.counts?.[o.map])
|
|
99
|
+
counted = true;
|
|
100
|
+
if (maps.has(o.map) && !seen.has(o.map)) {
|
|
101
|
+
seen.add(o.map);
|
|
102
|
+
overlap++;
|
|
103
|
+
}
|
|
104
|
+
} }); return !counted && overlap > 0; }).sort((a, b) => { const score = (l) => { const seen = new Set(); walkOnce(l.holds, (o) => { if (o.op === 'get' && maps.has(o.map))
|
|
105
|
+
seen.add(o.map); }); return seen.size; }; return score(b) - score(a); });
|
|
106
|
+
const terms = [...keys.values()].slice(0, 4), bridged = fieldLawBridges(m, sourceLaws, next, terms, terms);
|
|
107
|
+
for (const law of sourceLaws) {
|
|
108
|
+
const width = 1 + (law.keys?.length ?? 0), old = next[law.name] ?? [], all = bridged[law.name] ?? [];
|
|
109
|
+
for (let i = old.length; i < all.length; i += width)
|
|
110
|
+
add(law.name, all.slice(i, i + width), 'law');
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return { at: next, added };
|
|
114
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type Lin } from '../solver.js';
|
|
2
|
+
import { type Goal, type Path, type Tree } from '../../kernel/kernel.js';
|
|
3
|
+
export declare function naturalSplits(g: Goal, root: Path): Lin[];
|
|
4
|
+
export declare function proveNaturalCases(g: Goal, root: Path, until: number, trace?: object[], options?: {
|
|
5
|
+
deferCheck?: boolean;
|
|
6
|
+
}): Tree | undefined;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Untrusted arithmetic case proposals for natural subtraction in a source path.
|
|
2
|
+
// The ordinary kernel still checks all leaves and the combined tree.
|
|
3
|
+
import { walkOnce } from "../machine.js";
|
|
4
|
+
import { sub } from "../solver.js";
|
|
5
|
+
import { canon, columnTerm, linear, truth } from "../../kernel/kernel.js";
|
|
6
|
+
import { clock } from "../../kernel/prove.js";
|
|
7
|
+
import { check, grow } from "./ops.js";
|
|
8
|
+
export function naturalSplits(g, root) {
|
|
9
|
+
const expressions = [g.hyps], seen = new Set(), splits = new Map();
|
|
10
|
+
const columns = [...root.values()].flatMap(p => Object.keys(p.lin));
|
|
11
|
+
for (const name of columns) {
|
|
12
|
+
if (seen.has(name))
|
|
13
|
+
continue;
|
|
14
|
+
seen.add(name);
|
|
15
|
+
const term = columnTerm(name);
|
|
16
|
+
if (term) {
|
|
17
|
+
expressions.push(term);
|
|
18
|
+
walkOnce(term, (o) => { if (o.op === 'var')
|
|
19
|
+
columns.push(o.name); });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
walkOnce(expressions, (o) => {
|
|
23
|
+
if (o.op !== 'sub' || truth({ op: '<=', a: o.a, b: o.b }, root) !== undefined)
|
|
24
|
+
return;
|
|
25
|
+
const a = linear(o.a, root), b = linear(o.b, root);
|
|
26
|
+
if (!a || !b)
|
|
27
|
+
return;
|
|
28
|
+
const split = sub(a, b);
|
|
29
|
+
splits.set(canon(split), split);
|
|
30
|
+
});
|
|
31
|
+
return [...splits.values()].slice(0, 2);
|
|
32
|
+
}
|
|
33
|
+
export function proveNaturalCases(g, root, until, trace = [], options = {}) {
|
|
34
|
+
const splits = naturalSplits(g, root);
|
|
35
|
+
if (!splits.length)
|
|
36
|
+
return undefined;
|
|
37
|
+
const started = clock();
|
|
38
|
+
const go = (index, path) => {
|
|
39
|
+
if (clock() >= until)
|
|
40
|
+
return undefined;
|
|
41
|
+
const split = splits[index];
|
|
42
|
+
if (!split) {
|
|
43
|
+
const out = grow(g, path, { nodes: 0, until: Math.min(until, clock() + 100), units: false });
|
|
44
|
+
trace.push({ stage: 'natural-case-leaf', proposed: 'tree' in out, ...(!('tree' in out) ? { outcome: out.stuck, ...('witness' in out ? { model: out.witness } : {}) } : {}) });
|
|
45
|
+
return 'tree' in out ? out.tree : undefined;
|
|
46
|
+
}
|
|
47
|
+
const id = canon(split);
|
|
48
|
+
if (path.has(id))
|
|
49
|
+
return go(index + 1, path);
|
|
50
|
+
const yes = go(index + 1, new Map(path).set(id, { lin: split, holds: true }));
|
|
51
|
+
if (!yes)
|
|
52
|
+
return undefined;
|
|
53
|
+
const no = go(index + 1, new Map(path).set(id, { lin: split, holds: false }));
|
|
54
|
+
return no ? { split, yes, no } : undefined;
|
|
55
|
+
};
|
|
56
|
+
const tree = go(0, root), accepted = !!tree && (options.deferCheck || check(g, tree, root));
|
|
57
|
+
trace.push({ stage: 'natural-cases', cases: splits.length, ms: Math.round(clock() - started), checked: accepted && !options.deferCheck, proposed: !!tree, deferred: !!options.deferCheck });
|
|
58
|
+
return accepted ? tree : undefined;
|
|
59
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { check } from '../../kernel/kernel.js';
|
|
2
|
+
export { grow, widen, rowsAt } from '../../kernel/prove.js';
|
|
3
|
+
export { modelSequence } from '../../kernel/model-sequence.js';
|
|
4
|
+
export { solve, refuteFast } from '../solver.js';
|
|
5
|
+
export declare const wallSpan: <T>(_phase: string, run: () => T) => T;
|
|
6
|
+
export declare const wallTree: <T>(_phase: string, run: () => T) => T;
|