gdp-wired 0.52.0 → 0.53.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/dist/core/aggregate-kinds.d.ts +10 -0
- package/dist/core/aggregate-kinds.js +44 -0
- package/dist/core/aggregate-search.d.ts +9 -0
- package/dist/core/aggregate-search.js +74 -0
- package/dist/core/aggregate-weighted/candidates.d.ts +18 -0
- package/dist/core/aggregate-weighted/candidates.js +128 -0
- package/dist/core/aggregate-weighted/search.d.ts +17 -0
- package/dist/core/aggregate-weighted/search.js +68 -0
- package/dist/core/dsl.js +12 -1
- package/dist/core/filtered-sum-candidates.d.ts +17 -0
- package/dist/core/filtered-sum-candidates.js +77 -0
- package/dist/core/proof-output.d.ts +11 -1
- package/dist/core/proof-output.js +6 -0
- package/dist/kernel/finite-sum.d.ts +31 -0
- package/dist/kernel/finite-sum.js +74 -0
- package/dist/kernel/ghost-write.d.ts +117 -0
- package/dist/kernel/ghost-write.js +89 -0
- package/dist/lean-kernel/emit.d.ts +11 -0
- package/dist/lean-kernel/emit.js +24 -2
- package/dist/lean-kernel/finite-sum-export.d.ts +3 -0
- package/dist/lean-kernel/finite-sum-export.js +163 -0
- package/dist/lean-kernel/ghost-write-export.d.ts +3 -0
- package/dist/lean-kernel/ghost-write-export.js +81 -0
- package/dist/lean-kernel/library.js +3 -0
- package/dist/lean-kernel/lift-simulation/shape.js +5 -5
- package/docs/lean-aggregate.md +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Law, type Machine } from './machine.js';
|
|
2
|
+
import { type Instances, type Tree } from '../kernel/kernel.js';
|
|
3
|
+
export type AggregateKinds = {
|
|
4
|
+
laws: Law[];
|
|
5
|
+
proof: {
|
|
6
|
+
instances: Record<string, Instances>;
|
|
7
|
+
trees: Record<string, Tree>;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
export declare function checkAggregateKinds(m: Machine, kinds: Law[], until: number): AggregateKinds | undefined;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// The aggregate law does not need any kind as a premise. Prove the kinds
|
|
2
|
+
// independently on the same source machine; ordinary checking is mandatory.
|
|
3
|
+
import { walkOnce } from "./machine.js";
|
|
4
|
+
import { checkLaws, obligations, own } from "../kernel/kernel.js";
|
|
5
|
+
import { clock, grow, pastWall } from "../kernel/prove.js";
|
|
6
|
+
import { growCdcl } from "../kernel/cdcl.js";
|
|
7
|
+
export function checkAggregateKinds(m, kinds, until) {
|
|
8
|
+
if (kinds.some(l => l.some?.length || l.keys?.length || l.finite !== undefined || l.ex))
|
|
9
|
+
return;
|
|
10
|
+
// A conjunction at any key holds iff each conjunct holds at that same key.
|
|
11
|
+
const parts = (c) => c.op === 'and' && c.xs.length ? c.xs.flatMap(parts) : [c];
|
|
12
|
+
const laws = kinds.flatMap(k => parts(k.holds).map((holds, i) => ({ ...k, name: `${k.name} [kind ${i + 1}]`, holds })));
|
|
13
|
+
const instances = Object.fromEntries(laws.filter(l => l.forall).map(l => {
|
|
14
|
+
const reads = new Set(), probe = { op: 'var', name: `${l.forall}#` };
|
|
15
|
+
walkOnce(l.holds, t => { const e = t; if (e.op === 'get')
|
|
16
|
+
reads.add(e.map); });
|
|
17
|
+
const addresses = [...reads].flatMap(map => {
|
|
18
|
+
const w = own(m.step.maps ?? {}, map);
|
|
19
|
+
return w && 'key' in w ? [w.key] : [];
|
|
20
|
+
});
|
|
21
|
+
const terms = [...new Map([probe, ...addresses].map(t => [JSON.stringify(t), t])).values()];
|
|
22
|
+
return [l.name, { [l.name]: terms }];
|
|
23
|
+
}));
|
|
24
|
+
let goals;
|
|
25
|
+
try {
|
|
26
|
+
goals = obligations(m, laws, instances);
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
if (pastWall())
|
|
30
|
+
return;
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const trees = Object.create(null);
|
|
34
|
+
for (const goal of goals) {
|
|
35
|
+
const quick = grow(goal, new Map(), { nodes: 0, until: Math.min(until, clock() + 75) });
|
|
36
|
+
const short = 'tree' in quick ? quick : growCdcl(goal, new Map(), { nodes: 0, until: Math.min(until, clock() + 3500) });
|
|
37
|
+
const got = 'tree' in short ? short : grow(goal, new Map(), { nodes: 0, until });
|
|
38
|
+
if (!('tree' in got))
|
|
39
|
+
return;
|
|
40
|
+
trees[goal.name] = got.tree;
|
|
41
|
+
}
|
|
42
|
+
const proof = { instances, trees };
|
|
43
|
+
return checkLaws(m, laws, proof) ? { laws, proof } : undefined;
|
|
44
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Law, Machine } from './machine.js';
|
|
2
|
+
import { type FiniteSumCertificate } from '../kernel/finite-sum.js';
|
|
3
|
+
import { type GhostWriteCertificate } from '../kernel/ghost-write.js';
|
|
4
|
+
import { type AggregateKinds } from './aggregate-kinds.js';
|
|
5
|
+
export type AggregateCertificate = FiniteSumCertificate | GhostWriteCertificate;
|
|
6
|
+
export declare function searchAggregate(m: Machine, law: Law, getKinds: () => Law[], ms: number): {
|
|
7
|
+
certificate: AggregateCertificate;
|
|
8
|
+
kinds?: AggregateKinds;
|
|
9
|
+
} | undefined;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { searchWall } from "./wall.js";
|
|
2
|
+
import { proposeFilteredCounters } from "./filtered-sum-candidates.js";
|
|
3
|
+
import { weightedSearch } from "./aggregate-weighted/search.js";
|
|
4
|
+
import { checkFiniteSum, finiteSumTheory, sumLocalGoal } from "../kernel/finite-sum.js";
|
|
5
|
+
import { checkGhostWrite } from "../kernel/ghost-write.js";
|
|
6
|
+
import { clock, grow, pastWall, withWall } from "../kernel/prove.js";
|
|
7
|
+
import { checkAggregateKinds } from "./aggregate-kinds.js";
|
|
8
|
+
export function searchAggregate(m, law, getKinds, ms) {
|
|
9
|
+
// Reserve at least half the requested wall for ordinary proof/search.
|
|
10
|
+
const allowance = Math.min(30_000, ms / 2);
|
|
11
|
+
if (!(allowance > 0))
|
|
12
|
+
return;
|
|
13
|
+
return withWall(searchWall(allowance), () => {
|
|
14
|
+
const until = clock() + allowance;
|
|
15
|
+
const certificate = proposeAggregate(m, law, allowance);
|
|
16
|
+
if (!certificate || pastWall())
|
|
17
|
+
return undefined;
|
|
18
|
+
const kinds = getKinds();
|
|
19
|
+
if (!kinds.length)
|
|
20
|
+
return { certificate };
|
|
21
|
+
const checked = checkAggregateKinds(m, kinds, until);
|
|
22
|
+
return checked && !pastWall() ? { certificate, kinds: checked } : undefined;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function proposeAggregate(m, law, ms) {
|
|
26
|
+
if (ms <= 0 || m.history || m.primitiveFailure || Object.keys(m.sums ?? {}).length || Object.keys(m.counts ?? {}).length ||
|
|
27
|
+
Object.keys(m.fns ?? {}).length || m.facts?.length || m.inputMaps?.length || law.keys?.length || law.some?.length || law.ex)
|
|
28
|
+
return;
|
|
29
|
+
const maps = Object.values(m.step.maps ?? {});
|
|
30
|
+
if (!law.forall) {
|
|
31
|
+
if (maps.length !== 1 || !('each' in maps[0]))
|
|
32
|
+
return;
|
|
33
|
+
const allowance = Math.min(1_000, ms);
|
|
34
|
+
return withWall(searchWall(allowance), () => {
|
|
35
|
+
const until = clock() + allowance;
|
|
36
|
+
for (const p of proposeFilteredCounters(m, law)) {
|
|
37
|
+
if (p.bound.op !== 'var')
|
|
38
|
+
continue;
|
|
39
|
+
const layout = { map: p.map, bound: p.bound.name, key: p.key, offset: p.offset, fields: p.fields.map(({ field, value }) => ({ field, value })) };
|
|
40
|
+
// The public snapshot can lack automatically inferred sums or other
|
|
41
|
+
// features. Scope refusal is not a proof failure on the ordinary path.
|
|
42
|
+
let t;
|
|
43
|
+
try {
|
|
44
|
+
t = finiteSumTheory(m, law, layout);
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
if (pastWall())
|
|
48
|
+
return undefined;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (!t)
|
|
52
|
+
continue;
|
|
53
|
+
const trees = Object.create(null);
|
|
54
|
+
for (const local of t.locals) {
|
|
55
|
+
const result = grow(sumLocalGoal(local), new Map(), { nodes: 0, until });
|
|
56
|
+
if (!('tree' in result))
|
|
57
|
+
break;
|
|
58
|
+
trees[local.name] = result.tree;
|
|
59
|
+
}
|
|
60
|
+
const cert = { kind: 'finite-sum', layout, trees };
|
|
61
|
+
if (checkFiniteSum(m, law, cert))
|
|
62
|
+
return cert;
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
if (!maps.some(write => 'each' in write) || !maps.some(write => !('each' in write)))
|
|
68
|
+
return;
|
|
69
|
+
const p = weightedSearch(m, law, ms);
|
|
70
|
+
if (!p)
|
|
71
|
+
return;
|
|
72
|
+
const cert = { kind: 'ghost-write', recipe: p.recipe, projection: { omitMap: p.recipe.ghostMap }, target: p.machine, laws: p.laws, pointLaw: p.pointLaw, proof: p.proof };
|
|
73
|
+
return checkGhostWrite(m, law, cert) ? cert : undefined;
|
|
74
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type E, type Law, type Machine } from '../machine.js';
|
|
2
|
+
export type WeightedRecipe = {
|
|
3
|
+
ghostMap: string;
|
|
4
|
+
point: E;
|
|
5
|
+
gates: [column: string, value: number][];
|
|
6
|
+
group: string;
|
|
7
|
+
weight: string;
|
|
8
|
+
observer: string;
|
|
9
|
+
};
|
|
10
|
+
export type WeightedCandidate = {
|
|
11
|
+
recipe: WeightedRecipe;
|
|
12
|
+
machine: Machine;
|
|
13
|
+
laws: Law[];
|
|
14
|
+
pointLaw: string;
|
|
15
|
+
agreementLaw: string;
|
|
16
|
+
sum: string;
|
|
17
|
+
};
|
|
18
|
+
export declare function weightedCandidates(m: Machine, law: Law): WeightedCandidate[];
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// Untrusted recognition: derive a weighted-map recipe from a guarded subtraction.
|
|
2
|
+
// Acceptance and public-law transport belong to the caller’s trusted checker.
|
|
3
|
+
import { get, n, same, walkOnce, x } from "../machine.js";
|
|
4
|
+
import { ghostedMachine, ghostedOk, substC } from "../../kernel/kernel.js";
|
|
5
|
+
const text = (u) => JSON.stringify(u);
|
|
6
|
+
const eq = (a, b) => text(a) === text(b);
|
|
7
|
+
const flat = (op, c) => c.op === op ? c.xs.flatMap(t => flat(op, t)) : [c];
|
|
8
|
+
const zero = n(0);
|
|
9
|
+
const sameKey = (t, k) => eq(t, k);
|
|
10
|
+
const at = (t, nm) => t.op === 'var' && t.name === nm;
|
|
11
|
+
const isZero = (t, o) => t.op === 'const' && t.value === (o ?? 0);
|
|
12
|
+
const and = (xs) => ({ op: 'and', xs });
|
|
13
|
+
const ite = (c, then, otherwise) => ({ op: 'if', c, then, else: otherwise });
|
|
14
|
+
const noSub = { vars: {}, maps: {}, flags: {} };
|
|
15
|
+
const termReads = (t) => { const a = []; walkOnce(t, o => { const e = o; if (e.op === 'get')
|
|
16
|
+
a.push(e); }); return a; };
|
|
17
|
+
const points = (m, map) => { const w = m.step.maps?.[map]; return w && !('each' in w) ? w : undefined; };
|
|
18
|
+
const labels = (m, c, matching, negated = false) => {
|
|
19
|
+
const r = [];
|
|
20
|
+
const pattern = (node) => {
|
|
21
|
+
if (node.op !== '=')
|
|
22
|
+
return;
|
|
23
|
+
const t = node.a.op === 'get' ? node.a : node.b.op === 'get' ? node.b : undefined, v = t === node.a ? node.b : node.a;
|
|
24
|
+
if (t && v.op === 'const' && matching(t) && Object.hasOwn(m.maps ?? {}, t.map) && m.maps[t.map] !== v.value)
|
|
25
|
+
r.push([t.map, v.value]);
|
|
26
|
+
};
|
|
27
|
+
if (!negated)
|
|
28
|
+
walkOnce(c, o => pattern(o));
|
|
29
|
+
else
|
|
30
|
+
walkOnce(c, o => { const q = o; if (q.op === 'not')
|
|
31
|
+
pattern(q.x); });
|
|
32
|
+
return r;
|
|
33
|
+
};
|
|
34
|
+
export function weightedCandidates(m, law) {
|
|
35
|
+
if (!law.forall || law.keys?.length || law.some?.length || law.ex || law.finite !== undefined ||
|
|
36
|
+
m.history || m.primitiveFailure || m.sums || Object.keys(m.counts ?? {}).length || m.inputMaps?.length ||
|
|
37
|
+
Object.keys(m.fns ?? {}).length || m.facts?.length || Object.keys(m.leasts ?? {}).length)
|
|
38
|
+
return [];
|
|
39
|
+
const key = law.forall, ws = m.step.maps ?? {}, names = text([m, law]);
|
|
40
|
+
let prefix = 'weight$';
|
|
41
|
+
while (names.includes(prefix))
|
|
42
|
+
prefix += 'a';
|
|
43
|
+
const obs = `${prefix}observer`, binder = `${prefix}item`, wm = `${prefix}map`, sum = `${prefix}sum`, selected = x(obs);
|
|
44
|
+
// The lane’s canonical family uses its exact closed-flag view: each source
|
|
45
|
+
// flag is inlined in order, then bound over an unused fresh key. Do not
|
|
46
|
+
// substitute a different ghost assignment or rewrite any source writes.
|
|
47
|
+
const sumKey = `${prefix}index`, fresh = [obs, wm, sum, sumKey];
|
|
48
|
+
let stem = 'internal$';
|
|
49
|
+
while (text([m, law, fresh]).includes(stem))
|
|
50
|
+
stem += 'a';
|
|
51
|
+
const closed = Object.create(null), inlined = Object.create(null);
|
|
52
|
+
for (const [i, [name, condition]] of Object.entries(m.step.flags ?? {}).entries()) {
|
|
53
|
+
if (condition.op === 'all')
|
|
54
|
+
return [];
|
|
55
|
+
const c = substC(condition, { ...noSub, flags: inlined });
|
|
56
|
+
inlined[name] = c;
|
|
57
|
+
closed[name] = { op: 'all', key: `${stem}${i}`, c };
|
|
58
|
+
}
|
|
59
|
+
const o = { ...m, params: [...m.params, obs], step: { ...m.step, flags: closed } }, res = [];
|
|
60
|
+
for (const aMap of new Set(termReads(law.holds).filter(r => at(r.key, key)).map(r => r.map))) {
|
|
61
|
+
const wr = ws[aMap], u = wr && 'each' in wr && wr.value;
|
|
62
|
+
if (!u || u.op !== 'if' || !isZero(u.then))
|
|
63
|
+
continue;
|
|
64
|
+
const ix = wr.each, delta = u.else;
|
|
65
|
+
if (delta.op !== 'if' || !eq(delta.else, get(aMap, x(ix))) || delta.then.op !== 'sub')
|
|
66
|
+
continue;
|
|
67
|
+
const sub = delta.then;
|
|
68
|
+
if (sub.a.op !== 'add' || !eq(sub.a.a, get(aMap, x(ix))) || sub.b.op !== 'if' || !isZero(sub.b.else) || sub.b.then.op !== 'get')
|
|
69
|
+
continue;
|
|
70
|
+
const { map: weight, key: point } = sub.b.then, ww = points(m, weight);
|
|
71
|
+
if (!ww || !m.maps || m.maps[aMap] !== 0)
|
|
72
|
+
continue;
|
|
73
|
+
// Structural role match: the accumulator's addressed guard equates its
|
|
74
|
+
// own key to a foreign-key field of the same row as the removed weight.
|
|
75
|
+
const groups = new Set();
|
|
76
|
+
walkOnce(delta.c, node => {
|
|
77
|
+
const c = node;
|
|
78
|
+
if (c.op !== '=')
|
|
79
|
+
return;
|
|
80
|
+
for (const [k, t] of [[c.a, c.b], [c.b, c.a]]) {
|
|
81
|
+
if (at(k, ix) && t.op === 'get' && sameKey(t.key, point) && eq(points(m, t.map)?.key, ww.key))
|
|
82
|
+
groups.add(t.map);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
if (groups.size !== 1)
|
|
86
|
+
continue;
|
|
87
|
+
const [group] = groups;
|
|
88
|
+
const aliases = point.op === 'if' ? [point.then, point.else] : [point];
|
|
89
|
+
const guards = flat('or', sub.b.c).flatMap(c => c.op === 'flag' && m.step.flags?.[c.name] ? [m.step.flags[c.name]] : []);
|
|
90
|
+
if (!guards.length)
|
|
91
|
+
continue;
|
|
92
|
+
const byGuard = guards.map(c => labels(m, c, r => aliases.some(k => eq(k, r.key)) && !!points(m, r.map)));
|
|
93
|
+
const gates = [...new Map(byGuard[0].filter(a => byGuard.every(xs => xs.some(b => eq(a, b)))).map(t => [text(t), t])).values()];
|
|
94
|
+
if (!gates.length || gates.length > 3 || !gates.some(([name]) => eq(points(m, name)?.key, point)))
|
|
95
|
+
continue;
|
|
96
|
+
const initGuards = flat('and', u.c), creating = initGuards.find(c => c.op === 'flag');
|
|
97
|
+
const aRef = initGuards.find(c => c.op === '=' && at(c.a, ix));
|
|
98
|
+
const absent = creating?.op === 'flag' && aRef?.op === '=' && m.step.flags?.[creating.name] ?
|
|
99
|
+
labels(m, m.step.flags[creating.name], r => eq(r.key, aRef.b) && !!ws[r.map] && 'each' in ws[r.map], true) : [];
|
|
100
|
+
const absence = absent.length === 1 ? absent[0] : undefined;
|
|
101
|
+
const post = (map, k) => {
|
|
102
|
+
const w = points(m, map);
|
|
103
|
+
if (!w)
|
|
104
|
+
throw Error('non-point in row');
|
|
105
|
+
if (eq(k, w.key))
|
|
106
|
+
return w.value;
|
|
107
|
+
return ite(same(k, w.key), w.value, get(map, k));
|
|
108
|
+
};
|
|
109
|
+
const contribution = (read) => ite(gates.reduceRight((tail, [g, v]) => and([same(read(g), n(v)), tail]), same(read(group), selected)), read(weight), zero);
|
|
110
|
+
// GhostWriteTransport.completePoint: the full post-row, including conditional
|
|
111
|
+
// aliasing against every column write; no read from the old ghost.
|
|
112
|
+
const value = contribution(name => post(name, point));
|
|
113
|
+
const ghost = { orig: o, vars: {}, maps: { [wm]: { start: 0, write: { key: point, value } } } };
|
|
114
|
+
if (!ghostedOk(ghost))
|
|
115
|
+
continue;
|
|
116
|
+
const instrumented = ghostedMachine(ghost);
|
|
117
|
+
const machine = { ...instrumented, state: { ...instrumented.state, [sum]: zero }, sums: { [sum]: { term: get(wm, x(sumKey)), key: sumKey } } };
|
|
118
|
+
const pointLaw = `${prefix}point contribution`, agreementLaw = `${prefix}accumulator equals weighted live sum`;
|
|
119
|
+
const laws = [
|
|
120
|
+
...(absence ? [{ name: `${prefix}absent storage is zero`, holds: { op: 'or', xs: [same(get(absence[0], selected), n(absence[1])), same(get(aMap, selected), zero)] } }] : []),
|
|
121
|
+
{ name: pointLaw, forall: binder, holds: same(get(wm, x(binder)), contribution(name => get(name, x(binder)))) },
|
|
122
|
+
{ name: agreementLaw, holds: same(get(aMap, selected), x(sum)) },
|
|
123
|
+
{ name: law.name, holds: substC(law.holds, { ...noSub, vars: { [key]: selected } }) },
|
|
124
|
+
];
|
|
125
|
+
res.push({ recipe: { ghostMap: wm, point, gates, group: group, weight, observer: obs }, machine, laws, pointLaw, agreementLaw, sum });
|
|
126
|
+
}
|
|
127
|
+
return res;
|
|
128
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type Law, type Machine } from '../machine.js';
|
|
2
|
+
import { type Instances, type Tree } from '../../kernel/kernel.js';
|
|
3
|
+
import { type WeightedRecipe } from './candidates.js';
|
|
4
|
+
export type WeightedProof = {
|
|
5
|
+
instances: Record<string, Instances>;
|
|
6
|
+
trees: Record<string, Tree>;
|
|
7
|
+
};
|
|
8
|
+
export type WeightedProposal = {
|
|
9
|
+
kind: 'weighted';
|
|
10
|
+
recipe: WeightedRecipe;
|
|
11
|
+
machine: Machine;
|
|
12
|
+
laws: Law[];
|
|
13
|
+
pointLaw: string;
|
|
14
|
+
proof: WeightedProof;
|
|
15
|
+
};
|
|
16
|
+
export type WeightedObserver = (event: Record<string, unknown>) => void;
|
|
17
|
+
export declare function weightedSearch(source: Machine, law: Law, ms: number, observe?: WeightedObserver): WeightedProposal | undefined;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { n, x } from "../machine.js";
|
|
2
|
+
import { searchWall } from "../wall.js";
|
|
3
|
+
import { obligations, sumParts } from "../../kernel/kernel.js";
|
|
4
|
+
import { clock, grow, pastWall, withWall } from "../../kernel/prove.js";
|
|
5
|
+
import { growCdcl } from "../../kernel/cdcl.js";
|
|
6
|
+
import { weightedCandidates } from "./candidates.js";
|
|
7
|
+
// Every seed is an ordinary instance of a premise of the original obligation.
|
|
8
|
+
// The output is a proposal only, even after its instrumented family checks.
|
|
9
|
+
function proveSeeded(candidate, until, observe) {
|
|
10
|
+
const { machine, laws, recipe: { point }, pointLaw, agreementLaw, sum } = candidate;
|
|
11
|
+
const law = laws.find(l => l.name === pointLaw);
|
|
12
|
+
if (!law?.forall)
|
|
13
|
+
return;
|
|
14
|
+
const probe = x(`${law.forall}#`);
|
|
15
|
+
const touched = [point, ...(point.op === 'if' ? [point.then, point.else] : [])];
|
|
16
|
+
const sumFlag = `?${sumParts(sum).flag}`;
|
|
17
|
+
const closedInstances = Object.fromEntries(Object.entries(machine.step.flags ?? {})
|
|
18
|
+
.flatMap(([name, condition]) => condition.op === 'all' ? [[`?${name}`, [n(0)]]] : []));
|
|
19
|
+
const instances = Object.fromEntries(laws.map(l => [l.name, {
|
|
20
|
+
...closedInstances,
|
|
21
|
+
[pointLaw]: l === law ? [...touched, probe] : [point],
|
|
22
|
+
[sumFlag]: l === law ? [probe] : [],
|
|
23
|
+
}]));
|
|
24
|
+
const trees = Object.create(null);
|
|
25
|
+
const goals = obligations(machine, laws, instances);
|
|
26
|
+
// Give the agreement obligation first refusal before the pointwise invariant.
|
|
27
|
+
const agreement = 2 * laws.findIndex(l => l.name === agreementLaw);
|
|
28
|
+
goals.unshift(...goals.splice(agreement, 2));
|
|
29
|
+
for (const goal of goals) {
|
|
30
|
+
if (clock() >= until || pastWall())
|
|
31
|
+
return;
|
|
32
|
+
const step = goal.name.endsWith(' @step');
|
|
33
|
+
const end = Math.min(until, clock() + (step ? 30_000 : 2_000));
|
|
34
|
+
const quick = grow(goal, new Map(), { nodes: 0, until: Math.min(end, clock() + (step ? 750 : 2_000)) });
|
|
35
|
+
const found = step && !('tree' in quick) && clock() < end && !pastWall()
|
|
36
|
+
? growCdcl(goal, new Map(), { nodes: 0, until: Math.min(end, clock() + 28_000) }) : quick;
|
|
37
|
+
observe({ phase: 'goal', goal: goal.name, closed: 'tree' in found, remaining: Math.round(until - clock()),
|
|
38
|
+
...('tree' in found ? {} : { stuck: found.stuck, why: found.why }) });
|
|
39
|
+
if (!('tree' in found))
|
|
40
|
+
return;
|
|
41
|
+
trees[goal.name] = found.tree;
|
|
42
|
+
}
|
|
43
|
+
// checkGhostWrite reconstructs this family from the source and checks it
|
|
44
|
+
// before production can accept it. Avoid doing the same full check twice.
|
|
45
|
+
return clock() < until && !pastWall() ? { instances, trees } : undefined;
|
|
46
|
+
}
|
|
47
|
+
export function weightedSearch(source, law, ms, observe = () => { }) {
|
|
48
|
+
if (!Number.isFinite(ms) || ms < 1000)
|
|
49
|
+
return;
|
|
50
|
+
const capped = Math.min(ms, 60_000), until = clock() + capped;
|
|
51
|
+
return withWall(searchWall(capped), () => {
|
|
52
|
+
try {
|
|
53
|
+
for (const candidate of weightedCandidates(source, law)) {
|
|
54
|
+
if (clock() >= until || pastWall())
|
|
55
|
+
break;
|
|
56
|
+
observe({ phase: 'candidate', recipe: candidate.recipe });
|
|
57
|
+
const proof = proveSeeded(candidate, until, observe);
|
|
58
|
+
if (proof)
|
|
59
|
+
return { kind: 'weighted', recipe: candidate.recipe, machine: candidate.machine,
|
|
60
|
+
laws: candidate.laws, pointLaw: candidate.pointLaw, proof };
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
observe({ phase: 'error', error: String(error) });
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
|
+
});
|
|
68
|
+
}
|
package/dist/core/dsl.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { retainFrom, retainCounted, publishProof, withPublicRule, retainStepProof, publishStepProof, retainLiveProof, publishLiveProof } from "./proof-output.js";
|
|
1
|
+
import { retainFrom, retainCounted, retainAggregate, publishProof, withPublicRule, retainStepProof, publishStepProof, retainLiveProof, publishLiveProof } from "./proof-output.js";
|
|
2
|
+
import { searchAggregate } from "./aggregate-search.js";
|
|
2
3
|
import { searchNow as countedSearchNow } from "./wall.js";
|
|
3
4
|
// Typed builders for machines, decisions and loops: names are declared once and come back as typed values, so a
|
|
4
5
|
// misspelled name, a law that reads a request, or a step that sets an undeclared variable is a TypeScript error, and
|
|
@@ -3492,6 +3493,7 @@ function proveRuleWall(r, laws, opts, captureMachine) {
|
|
|
3492
3493
|
return retainFrom(translationVerdict(r, laws, result), [result]);
|
|
3493
3494
|
}
|
|
3494
3495
|
function proveRuleIn(r, laws, opts, captureMachine) {
|
|
3496
|
+
const originalRule = r;
|
|
3495
3497
|
r = r.withSumsOf(laws, opts.candidateSeeds?.laws ?? []);
|
|
3496
3498
|
noting(r, laws);
|
|
3497
3499
|
if (opts.candidateSeeds) {
|
|
@@ -3561,6 +3563,15 @@ function proveRuleIn(r, laws, opts, captureMachine) {
|
|
|
3561
3563
|
}
|
|
3562
3564
|
if (refuted)
|
|
3563
3565
|
return searched(r, laws, [], refutedVerdict(refuted), opts);
|
|
3566
|
+
if (r === originalRule && !opts.candidateSeeds && opts.from !== 'any' && opts.search !== false &&
|
|
3567
|
+
!completeWorkProfile.enabled && !(typeof opts.search === 'object' && opts.search.work !== undefined) && laws.length === 1) {
|
|
3568
|
+
const ms = typeof opts.search === 'object' ? opts.search.ms ?? 60_000 : 60_000;
|
|
3569
|
+
// Rule.machine can allocate fresh write binders on each access. Check and
|
|
3570
|
+
// retain the same public snapshot, not a later alpha-renamed copy.
|
|
3571
|
+
const aggregate = searchAggregate(captureMachine, laws[0], () => withKinds(r, laws).slice(0, -1), ms);
|
|
3572
|
+
if (aggregate)
|
|
3573
|
+
return retainAggregate({ proven: true, checked: true }, captureMachine, laws[0], aggregate.certificate, aggregate.kinds);
|
|
3574
|
+
}
|
|
3564
3575
|
// A rule with counts: its goals are the counted ones (core/counts.ts searches them; the kernel checks the set).
|
|
3565
3576
|
if (r.machine.counts && Object.keys(r.machine.counts).length) {
|
|
3566
3577
|
const c = searchCounted(r.machine, withKinds(r, laws), typeof opts.search === 'object' ? opts.search.ms ?? 60_000 : 60_000);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type C, type Counted, type E, type Law, type Machine } from './machine.js';
|
|
2
|
+
export type FilteredCounterProposal = {
|
|
3
|
+
map: string;
|
|
4
|
+
key: string;
|
|
5
|
+
reset: C;
|
|
6
|
+
bound: E;
|
|
7
|
+
offset: number;
|
|
8
|
+
fields: {
|
|
9
|
+
field: string;
|
|
10
|
+
value: number;
|
|
11
|
+
guard: C;
|
|
12
|
+
count: string;
|
|
13
|
+
}[];
|
|
14
|
+
counts: Record<string, Counted>;
|
|
15
|
+
laws: Law[];
|
|
16
|
+
};
|
|
17
|
+
export declare function proposeFilteredCounters(m: Machine, caller: Law): FilteredCounterProposal[];
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// Untrusted, intentionally small syntax recognizers. Their products are
|
|
2
|
+
// candidates for existing checked induction, never extra source facts.
|
|
3
|
+
import { get, n, x, plus, same } from "./machine.js";
|
|
4
|
+
import { substC } from "../kernel/kernel.js";
|
|
5
|
+
const equal = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
6
|
+
const atoms = (c) => !c ? [] : c.op === 'and' ? c.xs.flatMap(atoms) : [c];
|
|
7
|
+
const subKey = (c, name, term) => substC(c, { vars: { [name]: term }, maps: {}, flags: {} });
|
|
8
|
+
const rootReset = (u, start) => u.op === 'if' && equal(u.then, start) ? u : undefined;
|
|
9
|
+
const increase = (u, field) => u.op === 'if' && equal(u.else, x(field)) && equal(u.then, plus(x(field), n(1))) ? u.c : undefined;
|
|
10
|
+
const guardIncludes = (full, sub) => atoms(sub).every(p => atoms(full).some(q => equal(p, q)));
|
|
11
|
+
const onlyVariable = (e, name) => e.op === 'var' && e.name === name;
|
|
12
|
+
export function proposeFilteredCounters(m, caller) {
|
|
13
|
+
if (m.history || Object.keys(m.counts ?? {}).length || Object.keys(m.sums ?? {}).length || caller.forall || caller.keys?.length)
|
|
14
|
+
return [];
|
|
15
|
+
const src = JSON.stringify([m, caller]), uses = JSON.stringify(caller.holds);
|
|
16
|
+
let stem = 'agg$';
|
|
17
|
+
while (src.includes(stem))
|
|
18
|
+
stem += 'a';
|
|
19
|
+
const out = [];
|
|
20
|
+
for (const [map, wr] of Object.entries(m.step.maps ?? {})) {
|
|
21
|
+
// Cover singleton point writes and broadcast-reset-plus-point writes. A
|
|
22
|
+
// spatial guard is not accepted as a scalar guard; it is first specialized.
|
|
23
|
+
if (!('each' in wr) || m.maps?.[map] === undefined)
|
|
24
|
+
continue;
|
|
25
|
+
const init = n(m.maps[map]);
|
|
26
|
+
const outer = rootReset(wr.value, init), first = outer?.else;
|
|
27
|
+
if (!outer || first?.op !== 'if')
|
|
28
|
+
continue;
|
|
29
|
+
const keyAtom = atoms(first.c).find(c => c.op === '=' && onlyVariable(c.a, wr.each) && c.b.op === 'var' && m.inputs.includes(c.b.name));
|
|
30
|
+
if (keyAtom?.op !== '=' || keyAtom.b.op !== 'var')
|
|
31
|
+
continue;
|
|
32
|
+
const key = keyAtom.b.name;
|
|
33
|
+
const below = atoms(m.assume).find(c => (c.op === '<=' || c.op === '<') && onlyVariable(c.a, key) && c.b.op === 'var' && m.params.includes(c.b.name));
|
|
34
|
+
if (!below || (below.op !== '<=' && below.op !== '<'))
|
|
35
|
+
continue;
|
|
36
|
+
const lower = atoms(m.assume).filter(c => c.op === '<=' && c.a.op === 'const' && onlyVariable(c.b, key)).map(c => c.op === '<=' && c.a.op === 'const' ? c.a.value : 0);
|
|
37
|
+
const offset = Math.max(0, ...lower);
|
|
38
|
+
const bound0 = below.op === '<=' ? plus(below.b, n(1)) : below.b;
|
|
39
|
+
const bound = below.op === '<=' && offset === 1 ? below.b : offset ? { op: 'sub', a: bound0, b: n(offset) } : bound0;
|
|
40
|
+
const options = [];
|
|
41
|
+
let walk = first;
|
|
42
|
+
for (let i = 0; i < 8 && walk.op === 'if'; i++, walk = walk.else) {
|
|
43
|
+
const value = walk.then;
|
|
44
|
+
const parts = atoms(walk.c);
|
|
45
|
+
if (!parts.some(p => equal(p, keyAtom)) || value.op !== 'const' || equal(value, init))
|
|
46
|
+
break;
|
|
47
|
+
const [one, ...more] = parts.filter(p => !equal(p, keyAtom));
|
|
48
|
+
if (!one)
|
|
49
|
+
break;
|
|
50
|
+
options.push({ value: value.value, guard: more.length ? { op: 'and', xs: [one, ...more] } : one });
|
|
51
|
+
}
|
|
52
|
+
if (!equal(walk, get(map, x(wr.each))) || !options.length)
|
|
53
|
+
continue;
|
|
54
|
+
const counters = Object.entries(m.step.state ?? {}).flatMap(([field, u]) => {
|
|
55
|
+
if (!uses.includes(`"name":${JSON.stringify(field)}`) || !equal(m.state[field], n(0)))
|
|
56
|
+
return [];
|
|
57
|
+
const reset = rootReset(u, n(0)), guard = reset && increase(reset.else, field);
|
|
58
|
+
return reset && guard && equal(reset.c, outer.c) ? [{ field, guard }] : [];
|
|
59
|
+
});
|
|
60
|
+
const fields = counters.flatMap(({ field, guard }) => {
|
|
61
|
+
// Prefer the first branch whose guard is covered by the increment. Later
|
|
62
|
+
// alternatives are fallback writes in the source's own branch order.
|
|
63
|
+
const opt = options.find(o => guardIncludes(guard, o.guard));
|
|
64
|
+
return opt ? [{ field, guard, value: opt.value, count: `${stem}${out.length}$${field}` }] : [];
|
|
65
|
+
});
|
|
66
|
+
if (!fields.length || new Set(fields.map(f => f.value)).size !== fields.length)
|
|
67
|
+
continue;
|
|
68
|
+
const binder = `${stem}member`, arg = `${stem}argument`, coord = offset ? plus(x(binder), n(offset)) : x(binder);
|
|
69
|
+
const counts = Object.fromEntries(fields.map(f => [f.count, { j: arg, key: binder, below: bound, body: { op: 'if', c: same(get(map, coord), n(f.value)), then: n(1), else: n(0) } }]));
|
|
70
|
+
const laws = fields.map(f => ({ name: `${stem}${f.field} equals its filtered count`, holds: same(x(f.field), get(f.count, n(0))) }));
|
|
71
|
+
// Emit a disjoint-capacity candidate, not a theorem about an assumed tally.
|
|
72
|
+
if (fields.length > 1)
|
|
73
|
+
laws.push({ name: `${stem}disjoint filtered capacity`, holds: { op: '<=', a: fields.map(f => x(f.field)).reduce(plus), b: bound } });
|
|
74
|
+
out.push({ map, key, reset: subKey(outer.c, wr.each, x(key)), bound, offset, fields, counts, laws });
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
import type { Machine, Law } from './machine.js';
|
|
1
|
+
import type { E, Machine, Law } from './machine.js';
|
|
2
|
+
import type { AggregateCertificate } from './aggregate-search.js';
|
|
3
|
+
import type { AggregateKinds } from './aggregate-kinds.js';
|
|
2
4
|
import type { Instances, Liveness, StepLaw, Tree } from '../kernel/kernel.js';
|
|
3
5
|
import type { proveLaws, proveLiveness, proveProgress, proveStepLaws } from '../kernel/prove.js';
|
|
4
6
|
export type ProofOutputNode = {
|
|
7
|
+
kind: 'aggregate';
|
|
8
|
+
machine: Machine;
|
|
9
|
+
law: Law;
|
|
10
|
+
certificate: AggregateCertificate;
|
|
11
|
+
kinds?: AggregateKinds;
|
|
12
|
+
terms: E[];
|
|
13
|
+
} | {
|
|
5
14
|
kind: 'counted';
|
|
6
15
|
machine: Machine;
|
|
7
16
|
laws: Law[];
|
|
@@ -85,6 +94,7 @@ export declare function retainCounted<T extends object>(result: T, family: {
|
|
|
85
94
|
certs: Record<string, import('../kernel/kernel.ts').CountCert>;
|
|
86
95
|
};
|
|
87
96
|
}): T;
|
|
97
|
+
export declare function retainAggregate<T extends object>(result: T, machine: Machine, law: Law, certificate: AggregateCertificate, kinds?: AggregateKinds): T;
|
|
88
98
|
export declare function retainFrom<T extends object>(result: T, parents: object[], context?: unknown): T;
|
|
89
99
|
export declare function publishProof<T extends object>(result: T, machine: Machine, laws: Law[]): T;
|
|
90
100
|
export {};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { snapshotKernelTermState } from "../kernel/kernel.js";
|
|
1
2
|
// Synchronous public rule invocations can recursively prove a Skolem family.
|
|
2
3
|
let publicRuleDepth = 0;
|
|
3
4
|
export function withPublicRule(run) {
|
|
@@ -65,6 +66,11 @@ export function retainCounted(result, family) {
|
|
|
65
66
|
records.set(result, { kind: 'counted', machine: family.m, laws: family.laws, proof: family.proof });
|
|
66
67
|
return result;
|
|
67
68
|
}
|
|
69
|
+
export function retainAggregate(result, machine, law, certificate, kinds) {
|
|
70
|
+
if (proofOutput.on)
|
|
71
|
+
records.set(result, { kind: 'aggregate', machine, law, certificate, ...(kinds ? { kinds } : {}), terms: snapshotKernelTermState().terms });
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
68
74
|
export function retainFrom(result, parents, context = { kind: 'projection' }) {
|
|
69
75
|
if (proofOutput.on)
|
|
70
76
|
records.set(result, { kind: 'derived', parents: parents.map(parent => records.get(parent)), context });
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type C, type E, type Law, type Machine } from '../core/machine.js';
|
|
2
|
+
import { type Goal, type Tree } from './kernel.js';
|
|
3
|
+
export type FiniteSumLayout = {
|
|
4
|
+
map: string;
|
|
5
|
+
bound: string;
|
|
6
|
+
key: string;
|
|
7
|
+
offset: number;
|
|
8
|
+
fields: {
|
|
9
|
+
field: string;
|
|
10
|
+
value: number;
|
|
11
|
+
}[];
|
|
12
|
+
};
|
|
13
|
+
export type FiniteSumCertificate = {
|
|
14
|
+
kind: 'finite-sum';
|
|
15
|
+
layout: FiniteSumLayout;
|
|
16
|
+
trees: Record<string, Tree>;
|
|
17
|
+
};
|
|
18
|
+
export type SumLocal = {
|
|
19
|
+
name: string;
|
|
20
|
+
assumptions: C[];
|
|
21
|
+
conclusion: C;
|
|
22
|
+
};
|
|
23
|
+
export declare const sumLocalGoal: (l: SumLocal) => Goal;
|
|
24
|
+
export declare function finiteSumTheory(m: Machine, law: Law, p: FiniteSumLayout): {
|
|
25
|
+
locals: SumLocal[];
|
|
26
|
+
coordinate: string;
|
|
27
|
+
point: E;
|
|
28
|
+
bound: E;
|
|
29
|
+
reset: C;
|
|
30
|
+
} | undefined;
|
|
31
|
+
export declare function checkFiniteSum(m: Machine, law: Law, p: FiniteSumCertificate): boolean;
|