gdp-wired 0.50.6 → 0.52.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 +2 -2
- package/dist/core/accel-refute.d.ts +53 -0
- package/dist/core/accel-refute.js +997 -0
- package/dist/core/cli.d.ts +2 -0
- package/dist/core/cli.js +8 -1
- package/dist/core/cycles.d.ts +2 -0
- package/dist/core/cycles.js +12 -9
- package/dist/core/dsl.d.ts +11 -2
- package/dist/core/dsl.js +194 -29
- package/dist/core/fixed-division.d.ts +9 -0
- package/dist/core/fixed-division.js +87 -0
- package/dist/core/fixed-integers.d.ts +33 -0
- package/dist/core/fixed-integers.js +279 -0
- package/dist/core/live-support.d.ts +5 -0
- package/dist/core/live-support.js +148 -0
- package/dist/core/live.js +194 -37
- package/dist/core/lockstep.d.ts +5 -0
- package/dist/core/lockstep.js +23 -0
- package/dist/core/machine.d.ts +1 -0
- package/dist/core/machine.js +33 -3
- package/dist/core/ordered-pair-seeds.d.ts +2 -0
- package/dist/core/ordered-pair-seeds.js +109 -0
- package/dist/core/proof-output.d.ts +38 -1
- package/dist/core/proof-output.js +38 -0
- package/dist/core/search-termination.d.ts +4 -0
- package/dist/core/search-termination.js +1 -1
- package/dist/core/source-check.d.ts +4 -1
- package/dist/core/source-check.js +5 -2
- package/dist/core/sparse-kind-proof.d.ts +7 -0
- package/dist/core/sparse-kind-proof.js +49 -0
- package/dist/core/step-seeds.d.ts +46 -0
- package/dist/core/step-seeds.js +316 -0
- package/dist/kernel/integer-primitives.d.ts +38 -0
- package/dist/kernel/integer-primitives.js +179 -0
- package/dist/kernel/kernel.d.ts +1 -0
- package/dist/kernel/kernel.js +44 -5
- package/dist/kernel/prove.js +13 -0
- package/dist/kernel/word-division-cuts.d.ts +6 -0
- package/dist/kernel/word-division-cuts.js +69 -0
- package/dist/kernel/word-division.d.ts +12 -0
- package/dist/kernel/word-division.js +167 -0
- package/dist/lean-kernel/Cert.lean +222 -0
- package/dist/lean-kernel/Count.lean +1568 -0
- package/dist/lean-kernel/Ghost.lean +373 -0
- package/dist/lean-kernel/GhostMap.lean +426 -0
- package/dist/lean-kernel/Kernel.lean +2881 -0
- package/dist/lean-kernel/Live.lean +1168 -0
- package/dist/lean-kernel/Progress.lean +543 -0
- package/dist/lean-kernel/Sets.lean +1975 -0
- package/dist/lean-kernel/StepLaw.lean +128 -0
- package/dist/lean-kernel/Steps.lean +477 -0
- package/dist/lean-kernel/Sums.lean +916 -0
- package/dist/lean-kernel/emit.d.ts +8 -1
- package/dist/lean-kernel/emit.js +59 -15
- package/dist/lean-kernel/integer-primitives.d.ts +17 -0
- package/dist/lean-kernel/integer-primitives.js +81 -0
- package/dist/lean-kernel/lean-aggregate/FiniteFold.lean +44 -0
- package/dist/lean-kernel/lean-aggregate/GroupedTransport.lean +308 -0
- package/dist/lean-kernel/lean-aggregate/aggregate.d.ts +13 -0
- package/dist/lean-kernel/lean-aggregate/aggregate.js +68 -0
- package/dist/lean-kernel/lean-aggregate/assets.d.ts +5 -0
- package/dist/lean-kernel/lean-aggregate/assets.js +7 -0
- package/dist/lean-kernel/lean-aggregate/audit.d.ts +3 -0
- package/dist/lean-kernel/lean-aggregate/audit.js +13 -0
- package/dist/lean-kernel/lean-aggregate/exact.d.ts +15 -0
- package/dist/lean-kernel/lean-aggregate/exact.js +51 -0
- package/dist/lean-kernel/lean-aggregate/index.d.ts +19 -0
- package/dist/lean-kernel/lean-aggregate/index.js +52 -0
- package/dist/lean-kernel/lean-aggregate/point-fold.d.ts +27 -0
- package/dist/lean-kernel/lean-aggregate/point-fold.js +79 -0
- package/dist/lean-kernel/lean-aggregate/replay-fold.d.ts +2 -0
- package/dist/lean-kernel/lean-aggregate/replay-fold.js +163 -0
- package/dist/lean-kernel/lean-aggregate/source.d.ts +2 -0
- package/dist/lean-kernel/lean-aggregate/source.js +141 -0
- package/dist/lean-kernel/lean-aggregate/weighted-flags.d.ts +2 -0
- package/dist/lean-kernel/lean-aggregate/weighted-flags.js +26 -0
- package/dist/lean-kernel/lean-aggregate/weighted-replay.d.ts +7 -0
- package/dist/lean-kernel/lean-aggregate/weighted-replay.js +53 -0
- package/dist/lean-kernel/lean-aggregate/weighted-seeded.d.ts +30 -0
- package/dist/lean-kernel/lean-aggregate/weighted-seeded.js +64 -0
- package/dist/lean-kernel/lean-aggregate/weighted.d.ts +18 -0
- package/dist/lean-kernel/lean-aggregate/weighted.js +115 -0
- package/dist/lean-kernel/lean-aggregate/worker.d.ts +10 -0
- package/dist/lean-kernel/lean-aggregate/worker.js +46 -0
- package/dist/lean-kernel/library.js +13 -0
- package/dist/lean-kernel/word-division-export.d.ts +26 -0
- package/dist/lean-kernel/word-division-export.js +172 -0
- package/dist/lean-kernel/word-division-lower.d.ts +10 -0
- package/dist/lean-kernel/word-division-lower.js +103 -0
- package/docs/lean-aggregate.md +29 -0
- package/package.json +6 -1
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { types } from 'node:util';
|
|
2
|
+
import { isSourceBound } from "../../core/source-check.js";
|
|
3
|
+
import { scope } from "../../kernel/kernel.js";
|
|
4
|
+
function refuse(detail) { throw new Error(detail); }
|
|
5
|
+
const isObject = (v) => v !== null && typeof v === 'object';
|
|
6
|
+
// Never look up a source property (including array elements) or serialize a
|
|
7
|
+
// caller-supplied object. Reject proxies before any reflective operation.
|
|
8
|
+
function snapshot(machine, law) {
|
|
9
|
+
if (Object.getPrototypeOf(Array.prototype) !== Object.prototype)
|
|
10
|
+
refuse('Unexpected array prototype chain.');
|
|
11
|
+
if (Object.getOwnPropertyDescriptor(Object.prototype, 'toJSON') ||
|
|
12
|
+
Object.getOwnPropertyDescriptor(Array.prototype, 'toJSON'))
|
|
13
|
+
refuse('Inherited toJSON is not supported.');
|
|
14
|
+
const active = new Set();
|
|
15
|
+
let nodes = 0;
|
|
16
|
+
const copy = (v, depth) => {
|
|
17
|
+
if (++nodes > 250_000 || depth > 256)
|
|
18
|
+
return refuse('Source data exceeds the snapshot bound.');
|
|
19
|
+
if (v === null || typeof v === 'string' || typeof v === 'boolean' ||
|
|
20
|
+
typeof v === 'number' && Number.isFinite(v) && !Object.is(v, -0))
|
|
21
|
+
return v;
|
|
22
|
+
if (!isObject(v) || types.isProxy(v))
|
|
23
|
+
return refuse('Source must contain only passive JSON data; omit absent optional fields.');
|
|
24
|
+
if (active.has(v))
|
|
25
|
+
return refuse('Source data must not be cyclic.');
|
|
26
|
+
const array = Array.isArray(v), proto = Object.getPrototypeOf(v);
|
|
27
|
+
if (array ? proto !== Array.prototype && proto !== null : proto !== Object.prototype && proto !== null)
|
|
28
|
+
return refuse('Unexpected source prototype.');
|
|
29
|
+
active.add(v);
|
|
30
|
+
const keys = Reflect.ownKeys(v);
|
|
31
|
+
const out = (array ? Object.setPrototypeOf([], null) : Object.create(null));
|
|
32
|
+
const length = array ? Object.getOwnPropertyDescriptor(v, 'length').value : 0;
|
|
33
|
+
// An array has exactly its length descriptor and one enumerable data
|
|
34
|
+
// descriptor per index. Reject holes, symbols and extra own properties.
|
|
35
|
+
if (array && keys.length !== length + 1)
|
|
36
|
+
return refuse('Source arrays must be dense and have no extra properties.');
|
|
37
|
+
let i = 0;
|
|
38
|
+
for (const key of keys) {
|
|
39
|
+
if (array && key === 'length')
|
|
40
|
+
continue;
|
|
41
|
+
if (typeof key !== 'string' || key === 'toJSON' || array && key !== String(i++))
|
|
42
|
+
return refuse('Unsupported source property.');
|
|
43
|
+
const d = Object.getOwnPropertyDescriptor(v, key);
|
|
44
|
+
if (!d.enumerable || !Object.hasOwn(d, 'value'))
|
|
45
|
+
return refuse('Source properties must be enumerable own data.');
|
|
46
|
+
out[key] = copy(d.value, depth + 1);
|
|
47
|
+
}
|
|
48
|
+
active.delete(v);
|
|
49
|
+
return out;
|
|
50
|
+
};
|
|
51
|
+
const pair = [copy(machine, 0), copy(law, 0)];
|
|
52
|
+
Object.setPrototypeOf(pair, null);
|
|
53
|
+
return JSON.stringify(pair);
|
|
54
|
+
}
|
|
55
|
+
const record = (v) => isObject(v) && !Array.isArray(v);
|
|
56
|
+
const fields = (v, required, optional = []) => record(v) && required.every(k => Object.hasOwn(v, k)) && Object.keys(v).every(k => required.includes(k) || optional.includes(k));
|
|
57
|
+
// These labels may be printed in comments. Do not let one supply Lean syntax.
|
|
58
|
+
function label(v) {
|
|
59
|
+
if (typeof v !== 'string' || v.includes('/-') || v.includes('-/'))
|
|
60
|
+
return false;
|
|
61
|
+
for (const c of v) {
|
|
62
|
+
const point = c.codePointAt(0);
|
|
63
|
+
if (point < 0x20 || point >= 0x7f && point <= 0x9f || point === 0x2028 || point === 0x2029)
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
const nat = (v) => typeof v === 'number' && Number.isSafeInteger(v) && v >= 0;
|
|
69
|
+
const list = (v, valid) => Array.isArray(v) && v.every(valid);
|
|
70
|
+
const table = (v, valid) => record(v) && Object.entries(v).every(([key, x]) => label(key) && valid(x));
|
|
71
|
+
const emptyList = (v) => Array.isArray(v) && v.length === 0;
|
|
72
|
+
const emptyTable = (v) => record(v) && Object.keys(v).length === 0;
|
|
73
|
+
const optional = (v, key, valid) => !Object.hasOwn(v, key) || valid(v[key]);
|
|
74
|
+
// Validate the closed natural-number source fragment, not just its opcodes.
|
|
75
|
+
// No primitive failure, carried maps, history, derived values, external facts,
|
|
76
|
+
// native functions, or alternate law binders may reach either recognizer.
|
|
77
|
+
function admissible(m, law) {
|
|
78
|
+
if (!fields(m, ['name', 'params', 'inputs', 'state', 'step'], ['maps', 'pre', 'assume', 'inputMaps', 'facts', 'fns', 'leasts', 'relations']) ||
|
|
79
|
+
!fields(law, ['name', 'holds'], ['forall', 'keys', 'some', 'stated']) ||
|
|
80
|
+
!fields(m.step, [], ['state', 'maps', 'flags']))
|
|
81
|
+
return false;
|
|
82
|
+
const maps = Object.hasOwn(m, 'maps') ? m.maps : {};
|
|
83
|
+
function term(v) {
|
|
84
|
+
if (!record(v) || !Object.hasOwn(v, 'op'))
|
|
85
|
+
return false;
|
|
86
|
+
switch (v.op) {
|
|
87
|
+
case 'var': return fields(v, ['op', 'name']) && label(v.name);
|
|
88
|
+
case 'const': return fields(v, ['op', 'value']) && nat(v.value);
|
|
89
|
+
case 'add':
|
|
90
|
+
case 'sub':
|
|
91
|
+
case 'min':
|
|
92
|
+
case 'max':
|
|
93
|
+
case 'prod':
|
|
94
|
+
case 'tup':
|
|
95
|
+
return fields(v, ['op', 'a', 'b']) && term(v.a) && term(v.b);
|
|
96
|
+
case 'mul': return fields(v, ['op', 'k', 'a']) && nat(v.k) && term(v.a);
|
|
97
|
+
case 'proj': return fields(v, ['op', 'k', 'a']) && (v.k === 0 || v.k === 1) && term(v.a);
|
|
98
|
+
case 'if': return fields(v, ['op', 'c', 'then', 'else']) && cond(v.c) && term(v.then) && term(v.else);
|
|
99
|
+
case 'get': return fields(v, ['op', 'map', 'key']) && label(v.map) && record(maps) && Object.hasOwn(maps, v.map) && term(v.key);
|
|
100
|
+
default: return false;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function cond(v) {
|
|
104
|
+
if (!record(v) || !Object.hasOwn(v, 'op'))
|
|
105
|
+
return false;
|
|
106
|
+
switch (v.op) {
|
|
107
|
+
case '<':
|
|
108
|
+
case '<=':
|
|
109
|
+
case '=': return fields(v, ['op', 'a', 'b']) && term(v.a) && term(v.b);
|
|
110
|
+
case 'and':
|
|
111
|
+
case 'or': return fields(v, ['op', 'xs']) && list(v.xs, cond);
|
|
112
|
+
case 'not': return fields(v, ['op', 'x']) && cond(v.x);
|
|
113
|
+
case 'flag': return fields(v, ['op', 'name']) && label(v.name);
|
|
114
|
+
default: return false;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const write = (v) => (fields(v, ['each', 'value']) && label(v.each) || fields(v, ['key', 'value']) && term(v.key)) && term(v.value);
|
|
118
|
+
return label(m.name) && label(law.name) && list(m.params, label) && list(m.inputs, label) && table(maps, nat) &&
|
|
119
|
+
table(m.state, term) && optional(m.step, 'state', v => table(v, term)) && optional(m.step, 'maps', v => table(v, write)) &&
|
|
120
|
+
optional(m.step, 'flags', v => table(v, cond)) && optional(m, 'pre', cond) && optional(m, 'assume', cond) &&
|
|
121
|
+
optional(m, 'inputMaps', emptyList) && optional(m, 'facts', emptyList) && optional(m, 'fns', emptyTable) &&
|
|
122
|
+
optional(m, 'leasts', emptyTable) && optional(m, 'relations', v => table(v, a => list(a, label))) &&
|
|
123
|
+
optional(law, 'forall', v => label(v) && v.length > 0) && optional(law, 'keys', emptyList) && optional(law, 'some', emptyList) &&
|
|
124
|
+
cond(law.holds) && optional(law, 'stated', v => cond(v) && JSON.stringify(v) === JSON.stringify(law.holds));
|
|
125
|
+
}
|
|
126
|
+
export function admitAggregateSource(machine, law) {
|
|
127
|
+
if (!isObject(machine) || !isObject(law) || types.isProxy(machine) || types.isProxy(law))
|
|
128
|
+
return refuse('Source machine and invariant must be passive data.');
|
|
129
|
+
// These identities matter before copying: the marker deliberately survives
|
|
130
|
+
// without a native journal. This adapter only certifies an unmarked model.
|
|
131
|
+
if (isSourceBound(machine, [law]))
|
|
132
|
+
return refuse('Source-bound machines and laws are not supported; this adapter checks models only.');
|
|
133
|
+
const input = snapshot(machine, law);
|
|
134
|
+
const [m, l] = JSON.parse(input);
|
|
135
|
+
if (!admissible(m, l))
|
|
136
|
+
return refuse('Unsupported source machine or invariant.');
|
|
137
|
+
// Scope the exact, uninstrumented data the worker will parse, before either
|
|
138
|
+
// recognizer. The retained, instrumented family is a separate obligation.
|
|
139
|
+
scope(m, [l]);
|
|
140
|
+
return input;
|
|
141
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { walkOnce } from "../../core/machine.js";
|
|
2
|
+
import { ghostedOk, substC } from "../../kernel/kernel.js";
|
|
3
|
+
export function closeWeightedFlags(c) {
|
|
4
|
+
const flags = Object.create(null), inlined = Object.create(null);
|
|
5
|
+
const src = JSON.stringify(c.machine);
|
|
6
|
+
let stem = 'internal$';
|
|
7
|
+
while (src.includes(stem))
|
|
8
|
+
stem += 'a';
|
|
9
|
+
for (const [i, [name, value]] of Object.entries(c.ghost.orig.step.flags ?? {}).entries()) {
|
|
10
|
+
if (value.op === 'all')
|
|
11
|
+
return undefined;
|
|
12
|
+
const body = substC(value, { vars: {}, maps: {}, flags: inlined });
|
|
13
|
+
let open = false;
|
|
14
|
+
walkOnce(body, e => { const t = e; if (t.op === 'flag' || t.op === 'all')
|
|
15
|
+
open = true; });
|
|
16
|
+
if (open)
|
|
17
|
+
return undefined;
|
|
18
|
+
inlined[name] = body;
|
|
19
|
+
flags[name] = { op: 'all', key: `${stem}${i}`, c: body };
|
|
20
|
+
}
|
|
21
|
+
const replace = (m) => ({ ...m, step: { ...m.step, flags } });
|
|
22
|
+
const ghost = { ...c.ghost, orig: replace(c.ghost.orig) };
|
|
23
|
+
if (!ghostedOk(ghost))
|
|
24
|
+
return undefined;
|
|
25
|
+
return { ...c, ghost, machine: replace(c.machine) };
|
|
26
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Law, Machine } from '../../core/machine.js';
|
|
2
|
+
import { type Instances, type Tree } from '../../kernel/kernel.js';
|
|
3
|
+
import type { WeightedCandidate } from './weighted.js';
|
|
4
|
+
export declare function emitWeighted(c: WeightedCandidate, source: Machine, law: Law, proof: {
|
|
5
|
+
instances: Record<string, Instances>;
|
|
6
|
+
trees: Record<string, Tree>;
|
|
7
|
+
}): string;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// Generic proof-output bridge: export only the retained weighted family,
|
|
2
|
+
// then prove source correspondence (flag views, observer, ghost map, sum).
|
|
3
|
+
import assert from 'node:assert/strict';
|
|
4
|
+
import { checkLaws, ghostedOk, sideGoals } from "../../kernel/kernel.js";
|
|
5
|
+
import { retainedSumMachineToLean } from "../../lean-kernel/emit.js";
|
|
6
|
+
export function emitWeighted(c, source, law, proof) {
|
|
7
|
+
assert(law.forall && !law.keys?.length && !law.some?.length);
|
|
8
|
+
assert(ghostedOk(c.ghost));
|
|
9
|
+
assert.equal(sideGoals(c.machine, c.laws).length, 0, 'unexpected cover search');
|
|
10
|
+
assert(checkLaws(c.machine, c.laws, proof), 'exact native weighted family rejected');
|
|
11
|
+
const [g, ...extra] = Object.keys(c.ghost.maps);
|
|
12
|
+
assert(g && extra.length === 0 && Object.keys(c.ghost.vars).length === 0);
|
|
13
|
+
const idx = c.laws.findIndex(l => l.name === law.name);
|
|
14
|
+
assert(idx >= 0);
|
|
15
|
+
const body = retainedSumMachineToLean('packet', c.machine, c.laws, proof, { free: [law.forall], after: (ix, mx, lit, cond) => {
|
|
16
|
+
const flags = Object.entries(c.ghost.orig.step.flags ?? {});
|
|
17
|
+
const binder = flags.map(([f, a]) => { assert.equal(a.op, 'all'); assert(a.op === 'all' && !a.keys?.length); return `if i = ${ix(`?${f}`)} then ${ix(a.key)} else `; }).join('') + '0';
|
|
18
|
+
const goal = cond(law.holds), specialization = cond(c.laws[idx].holds);
|
|
19
|
+
return `
|
|
20
|
+
def originalMachine : SMachine := ${lit(source)}
|
|
21
|
+
def unchangedLaw : MLaw := ⟨${goal}, some ${ix(law.forall)}⟩
|
|
22
|
+
def observer : Nat := ${ix(c.observer)}
|
|
23
|
+
def closedBinder (i : Nat) : Nat := ${binder}
|
|
24
|
+
def mapLift : SGhostedM := ⟨${lit(c.ghost.orig)}, [], [(${mx(g)}, ${c.ghost.maps[g].start})],
|
|
25
|
+
packet_sums.s.f.m.writes.filter (fun row => row.1 == ${mx(g)}), []⟩
|
|
26
|
+
theorem liftChecks : smghostOk mapLift = true := by decide +kernel
|
|
27
|
+
theorem sumShape : mapLift.full = packet_sums.s := by rfl
|
|
28
|
+
theorem sumGhostChecks : sghostOk packet_sums.ghosts = true := by decide +kernel
|
|
29
|
+
theorem promotionIdentity : mapLift.orig = GroupedSumsResearch.promote originalMachine closedBinder mapLift.orig.wits := by rfl
|
|
30
|
+
|
|
31
|
+
theorem unchanged_law (ρ : Nat → Nat) (M : Nat → Nat → Nat)
|
|
32
|
+
(hr : SReach originalMachine ρ M) : unchangedLaw.Holds ρ M := by
|
|
33
|
+
intro k
|
|
34
|
+
have ho := GroupedSumsResearch.source_rebind originalMachine observer rfl rfl rfl (by decide +kernel) hr k
|
|
35
|
+
have hc : SReach mapLift.orig (upd ρ observer k) M :=
|
|
36
|
+
(GroupedSumsResearch.candidate_reach closedBinder _ promotionIdentity rfl (by decide +kernel)).mpr ho
|
|
37
|
+
obtain ⟨ρg, Mg, hg, hs, hm⟩ := smghost_reach mapLift liftChecks hc
|
|
38
|
+
have he : packet_e${idx}.Holds ρg Mg :=
|
|
39
|
+
sum_project_laws packet_sums sumGhostChecks packet_proof packet_check packet_scheck
|
|
40
|
+
ρg Mg (sumShape ▸ hg) packet_e${idx} (by simp [packet_proof]) (by decide +kernel)
|
|
41
|
+
have hl : packet_e${idx}.Holds (upd ρ observer k) M :=
|
|
42
|
+
sentry_transfer (V := mapLift.vars) (MS := mapLift.maps) (by decide +kernel) (by decide +kernel)
|
|
43
|
+
(fun i hi => (hs i hi).symm) (fun m hmi => (hm m hmi).symm) he
|
|
44
|
+
change evalC (upd ρ observer k) M (${specialization}) = true at hl
|
|
45
|
+
change evalC (upd ρ ${ix(law.forall)} k) M (${goal}) = true
|
|
46
|
+
simpa [observer, evalC, evalE, upd] using hl
|
|
47
|
+
|
|
48
|
+
theorem aggregate_claim : ∀ ρ M, SReach originalMachine ρ M → unchangedLaw.Holds ρ M :=
|
|
49
|
+
unchanged_law
|
|
50
|
+
`;
|
|
51
|
+
} });
|
|
52
|
+
return `import GhostMap\nimport Sums\nimport GroupedTransport\nopen Kern Cert\nset_option maxRecDepth 100000\nset_option maxHeartbeats 0\n${body}`;
|
|
53
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type Instances, type Tree } from '../../kernel/kernel.js';
|
|
2
|
+
import type { WeightedCandidate } from './weighted.js';
|
|
3
|
+
export declare function proveWeightedSeeded(c: WeightedCandidate, ms: number, observe: (x: object) => void): {
|
|
4
|
+
proven: boolean;
|
|
5
|
+
checked: boolean;
|
|
6
|
+
failed: {
|
|
7
|
+
why: string;
|
|
8
|
+
goal?: undefined;
|
|
9
|
+
};
|
|
10
|
+
proof?: undefined;
|
|
11
|
+
} | {
|
|
12
|
+
proven: boolean;
|
|
13
|
+
checked: boolean;
|
|
14
|
+
failed: {
|
|
15
|
+
goal: string;
|
|
16
|
+
why: string;
|
|
17
|
+
};
|
|
18
|
+
proof?: undefined;
|
|
19
|
+
} | {
|
|
20
|
+
proven: boolean;
|
|
21
|
+
checked: boolean;
|
|
22
|
+
proof: {
|
|
23
|
+
instances: Record<string, Instances>;
|
|
24
|
+
trees: Record<string, Tree>;
|
|
25
|
+
};
|
|
26
|
+
failed: {
|
|
27
|
+
why: string;
|
|
28
|
+
goal?: undefined;
|
|
29
|
+
} | undefined;
|
|
30
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { n, same, x } from "../../core/machine.js";
|
|
2
|
+
import { checkLaws, obligations, sumParts } from "../../kernel/kernel.js";
|
|
3
|
+
import { clock, grow } from "../../kernel/prove.js";
|
|
4
|
+
import { countGuardTree } from "../../core/count-guard-tree.js";
|
|
5
|
+
// Propose ∀-instances only at the address the weighted summary actually
|
|
6
|
+
// subtracts and the preservation goal's own row. Every branch tree is an
|
|
7
|
+
// ordinary proof of the original goal (no changed hypothesis list).
|
|
8
|
+
export function proveWeightedSeeded(c, ms, observe) {
|
|
9
|
+
const { machine, laws, links } = c;
|
|
10
|
+
const p = laws.find(l => l.forall);
|
|
11
|
+
if (!p?.forall)
|
|
12
|
+
return { proven: false, checked: false, failed: { why: 'no pointwise helper' } };
|
|
13
|
+
const probe = x(`${p.forall}#`);
|
|
14
|
+
const point = links.point;
|
|
15
|
+
const touched = [point, ...(point.op === 'if' ? [point.then, point.else] : [])];
|
|
16
|
+
const sum = Object.keys(machine.sums ?? {})[0];
|
|
17
|
+
const q = `?${sumParts(sum).flag}`;
|
|
18
|
+
const closed = Object.entries(machine.step.flags ?? {}).flatMap(([name, c]) => c.op === 'all' ? [name] : []);
|
|
19
|
+
const instances = Object.fromEntries(laws.map(l => [l.name, { ...Object.fromEntries(closed.map(f => ['?' + f, [n(0)]])), [p.name]: l === p ? [...touched, probe] : [point], [q]: l === p ? [probe] : [] }]));
|
|
20
|
+
const until = clock() + ms, trees = {};
|
|
21
|
+
const goals = obligations(machine, laws, instances);
|
|
22
|
+
// Try agreement first so a large pointwise relation cannot starve it.
|
|
23
|
+
const i = laws.findIndex(({ holds: c }) => (c.op === '=' || c.op === '<=') && [c.a, c.b].some(e => e.op === 'var' && e.name === sum));
|
|
24
|
+
if (i >= 0)
|
|
25
|
+
goals.unshift(...goals.splice(2 * i, 2));
|
|
26
|
+
for (const goal of goals) {
|
|
27
|
+
if (clock() >= until)
|
|
28
|
+
return { proven: false, checked: false, failed: { goal: goal.name, why: 'budget' } };
|
|
29
|
+
const addressConditions = goal.name.endsWith('@step') ? (point.op === 'if' ? [point.c] : []) : [];
|
|
30
|
+
if (goal.name === `${p.name} @step`)
|
|
31
|
+
addressConditions.push(same(probe, point));
|
|
32
|
+
if (goal.name.endsWith('@step'))
|
|
33
|
+
for (const f of links.events.filter(f => closed.includes(f)))
|
|
34
|
+
addressConditions.push({ op: '<=', a: x('?' + f), b: n(0) });
|
|
35
|
+
const one = (g, path, ms) => {
|
|
36
|
+
const found = grow(g, path, { nodes: 0, until: Math.min(until, clock() + ms) });
|
|
37
|
+
return 'tree' in found ? found.tree : undefined;
|
|
38
|
+
};
|
|
39
|
+
const quick = one(goal, new Map(), goal.name.endsWith('@start') ? 1000 : closed.length ? 1200 : 3500);
|
|
40
|
+
const split = countGuardTree(until, { yesFirst: true });
|
|
41
|
+
const branch = (at, path) => {
|
|
42
|
+
if (at === addressConditions.length) {
|
|
43
|
+
const proof = one(goal, path, Math.min(closed.length ? 6000 : 8000, until - clock()));
|
|
44
|
+
if (!proof && closed.length)
|
|
45
|
+
observe({ goal: goal.name, phase: 'leaf', path: [...path.values()].slice(-12) });
|
|
46
|
+
return proof;
|
|
47
|
+
}
|
|
48
|
+
if (closed.length && at > 0) {
|
|
49
|
+
const dead = one(goal, path, 70);
|
|
50
|
+
if (dead)
|
|
51
|
+
return dead;
|
|
52
|
+
}
|
|
53
|
+
const next = (p) => branch(at + 1, p);
|
|
54
|
+
return split.condition(addressConditions[at], path, next, next);
|
|
55
|
+
};
|
|
56
|
+
const tree = quick ?? branch(0, new Map());
|
|
57
|
+
observe({ goal: goal.name, checkedHere: !!tree, remaining: Math.round(until - clock()) });
|
|
58
|
+
if (!tree)
|
|
59
|
+
return { proven: false, checked: false, failed: { goal: goal.name, why: 'write-key instances and guard splitting did not close' } };
|
|
60
|
+
trees[goal.name] = tree;
|
|
61
|
+
}
|
|
62
|
+
const proof = { instances, trees }, checked = checkLaws(machine, laws, proof);
|
|
63
|
+
return { proven: checked, checked, proof, failed: checked ? undefined : { why: 'whole-family kernel refused' } };
|
|
64
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type E, type Law, type Machine } from '../../core/machine.js';
|
|
2
|
+
import { type Ghosted } from '../../kernel/kernel.js';
|
|
3
|
+
export type WeightedCandidate = {
|
|
4
|
+
ghost: Ghosted;
|
|
5
|
+
machine: Machine;
|
|
6
|
+
laws: Law[];
|
|
7
|
+
links: {
|
|
8
|
+
balance: string;
|
|
9
|
+
weight: string;
|
|
10
|
+
group: string;
|
|
11
|
+
gates: [string, number][];
|
|
12
|
+
absence?: [string, number];
|
|
13
|
+
point: E;
|
|
14
|
+
events: string[];
|
|
15
|
+
};
|
|
16
|
+
observer: string;
|
|
17
|
+
};
|
|
18
|
+
export declare function weightedCandidates(m: Machine, law: Law): WeightedCandidate[];
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// Derive a filtered weighted-sum proposal from a guarded
|
|
2
|
+
// subtraction and the write cluster it refers to; never assume the proposal.
|
|
3
|
+
import { get, n, same, walkOnce, x } from "../../core/machine.js";
|
|
4
|
+
import { ghostedMachine, ghostedOk, substC, sumMachine } 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 || m.history || m.sums || m.counts || m.inputMaps?.length)
|
|
36
|
+
return [];
|
|
37
|
+
const key = law.forall, ws = m.step.maps ?? {}, names = text([m, law]);
|
|
38
|
+
let prefix = 'weight$';
|
|
39
|
+
while (names.includes(prefix))
|
|
40
|
+
prefix += 'a';
|
|
41
|
+
const obs = `${prefix}observer`, binder = `${prefix}item`, wm = `${prefix}map`, sum = `${prefix}sum`, selected = x(obs);
|
|
42
|
+
const o = { ...m, params: [...m.params, obs] }, res = [];
|
|
43
|
+
const ownFlags = Object.fromEntries(Object.keys(m.step.flags ?? {}).map(name => [name, { op: 'flag', name }]));
|
|
44
|
+
for (const aMap of new Set(termReads(law.holds).filter(r => at(r.key, key)).map(r => r.map))) {
|
|
45
|
+
const wr = ws[aMap], u = wr && 'each' in wr && wr.value;
|
|
46
|
+
if (!u || u.op !== 'if' || !isZero(u.then))
|
|
47
|
+
continue;
|
|
48
|
+
const ix = wr.each, delta = u.else;
|
|
49
|
+
if (delta.op !== 'if' || !eq(delta.else, get(aMap, x(ix))) || delta.then.op !== 'sub')
|
|
50
|
+
continue;
|
|
51
|
+
const sub = delta.then;
|
|
52
|
+
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')
|
|
53
|
+
continue;
|
|
54
|
+
const { map: weight, key: point } = sub.b.then, ww = points(m, weight);
|
|
55
|
+
if (!ww || !m.maps || m.maps[aMap] !== 0)
|
|
56
|
+
continue;
|
|
57
|
+
// Structural role match: the accumulator's addressed guard equates its
|
|
58
|
+
// own key to a foreign-key field of the same row as the removed weight.
|
|
59
|
+
const groups = new Set();
|
|
60
|
+
walkOnce(delta.c, node => {
|
|
61
|
+
const c = node;
|
|
62
|
+
if (c.op !== '=')
|
|
63
|
+
return;
|
|
64
|
+
for (const [k, t] of [[c.a, c.b], [c.b, c.a]]) {
|
|
65
|
+
if (at(k, ix) && t.op === 'get' && sameKey(t.key, point) && eq(points(m, t.map)?.key, ww.key))
|
|
66
|
+
groups.add(t.map);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
if (groups.size !== 1)
|
|
70
|
+
continue;
|
|
71
|
+
const [group] = groups;
|
|
72
|
+
const aliases = point.op === 'if' ? [point.then, point.else] : [point];
|
|
73
|
+
const guards = flat('or', sub.b.c).flatMap(c => c.op === 'flag' && m.step.flags?.[c.name] ? [m.step.flags[c.name]] : []);
|
|
74
|
+
if (!guards.length)
|
|
75
|
+
continue;
|
|
76
|
+
const byGuard = guards.map(c => labels(m, c, r => aliases.some(k => eq(k, r.key)) && !!points(m, r.map)));
|
|
77
|
+
const gates = [...new Map(byGuard[0].filter(a => byGuard.every(xs => xs.some(b => eq(a, b)))).map(t => [text(t), t])).values()];
|
|
78
|
+
if (!gates.length || gates.length > 3 || !gates.some(([name]) => eq(points(m, name)?.key, point)))
|
|
79
|
+
continue;
|
|
80
|
+
const initGuards = flat('and', u.c), creating = initGuards.find(c => c.op === 'flag');
|
|
81
|
+
const aRef = initGuards.find(c => c.op === '=' && at(c.a, ix));
|
|
82
|
+
const absent = creating?.op === 'flag' && aRef?.op === '=' && m.step.flags?.[creating.name] ?
|
|
83
|
+
labels(m, m.step.flags[creating.name], r => eq(r.key, aRef.b) && !!ws[r.map] && 'each' in ws[r.map], true) : [];
|
|
84
|
+
const absence = absent.length === 1 ? absent[0] : undefined;
|
|
85
|
+
const contribution = (read) => ite(and([...gates.map(([g, v]) => same(read(g), n(v))), same(read(group), selected)]), read(weight), zero);
|
|
86
|
+
const added = sub.a.b;
|
|
87
|
+
if (added.op !== 'if' || !isZero(added.else))
|
|
88
|
+
continue;
|
|
89
|
+
const groupSelected = substC(delta.c, { ...noSub, flags: ownFlags, vars: { [ix]: selected } });
|
|
90
|
+
// The delta must still agree with the complete post-row shape at every key.
|
|
91
|
+
const value = ite(and([groupSelected, sub.b.c]), zero, ite(and([groupSelected, added.c]), added.then, get(wm, point)));
|
|
92
|
+
const ghost = { orig: o, vars: {}, maps: { [wm]: { start: 0, write: { key: point, value } } } };
|
|
93
|
+
if (!ghostedOk(ghost))
|
|
94
|
+
continue;
|
|
95
|
+
const instrumented = ghostedMachine(ghost);
|
|
96
|
+
const machine = { ...instrumented, state: { ...instrumented.state, [sum]: zero }, sums: { [sum]: { term: get(wm, x(`${prefix}index`)), key: `${prefix}index` } } };
|
|
97
|
+
try {
|
|
98
|
+
sumMachine(machine);
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
const laws = [
|
|
104
|
+
...(absence ? [{ name: `${prefix}absent storage is zero`, holds: { op: 'or', xs: [same(get(absence[0], selected), n(absence[1])), same(get(aMap, selected), zero)] } }] : []),
|
|
105
|
+
{ name: `${prefix}point contribution`, forall: binder, holds: same(get(wm, x(binder)), contribution(name => get(name, x(binder)))) },
|
|
106
|
+
{ name: `${prefix}accumulator equals weighted live sum`, holds: same(get(aMap, selected), x(sum)) },
|
|
107
|
+
{ name: law.name, holds: substC(law.holds, { ...noSub, vars: { [key]: selected } }) },
|
|
108
|
+
];
|
|
109
|
+
const eventNames = new Set();
|
|
110
|
+
walkOnce([u.c, sub.a.b, sub.b], node => { const c = node; if (c.op === 'flag')
|
|
111
|
+
eventNames.add(c.name); });
|
|
112
|
+
res.push({ ghost, machine, laws, observer: obs, links: { balance: aMap, weight, group: group, gates, absence, point, events: [...eventNames] } });
|
|
113
|
+
}
|
|
114
|
+
return res;
|
|
115
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// This worker is used only by the opt-in API. A fresh worker-local kernel
|
|
2
|
+
// term table and emitter memo keep aggregate search separate from native calls.
|
|
3
|
+
import { parentPort, workerData } from 'node:worker_threads';
|
|
4
|
+
import { searchWall } from "../../core/wall.js";
|
|
5
|
+
import { withWall } from "../../kernel/prove.js";
|
|
6
|
+
import { foldTheory, provePointFold } from "./point-fold.js";
|
|
7
|
+
import { emitPointFold } from "./replay-fold.js";
|
|
8
|
+
import { emitWeighted } from "./weighted-replay.js";
|
|
9
|
+
import { weightedCandidates } from "./weighted.js";
|
|
10
|
+
import { closeWeightedFlags } from "./weighted-flags.js";
|
|
11
|
+
import { proveWeightedSeeded } from "./weighted-seeded.js";
|
|
12
|
+
function propose(machine, law, ms) {
|
|
13
|
+
if (foldTheory(machine, law)) {
|
|
14
|
+
const packet = provePointFold(machine, law, Math.min(4000, ms));
|
|
15
|
+
if (packet)
|
|
16
|
+
return { kind: 'candidate', mechanism: 'finite-fold', source: emitPointFold(packet) };
|
|
17
|
+
}
|
|
18
|
+
else if (law.forall) {
|
|
19
|
+
const candidates = weightedCandidates(machine, law).flatMap(p => closeWeightedFlags(p) ?? []);
|
|
20
|
+
if (candidates.length) {
|
|
21
|
+
const result = withWall(searchWall(Math.min(ms, 59_000)), () => {
|
|
22
|
+
for (const candidate of candidates) {
|
|
23
|
+
const v = proveWeightedSeeded(candidate, Math.min(ms, 58_000), () => { });
|
|
24
|
+
if (v.proven && v.checked && v.proof)
|
|
25
|
+
return emitWeighted(candidate, machine, law, v.proof);
|
|
26
|
+
}
|
|
27
|
+
return undefined;
|
|
28
|
+
});
|
|
29
|
+
if (result)
|
|
30
|
+
return { kind: 'candidate', mechanism: 'weighted', source: result };
|
|
31
|
+
return { kind: 'refused', reason: 'search', detail: 'Weighted equality did not close.' };
|
|
32
|
+
}
|
|
33
|
+
return { kind: 'refused', reason: 'unsupported', detail: 'No weighted update shape recognized.' };
|
|
34
|
+
}
|
|
35
|
+
else
|
|
36
|
+
return { kind: 'refused', reason: 'unsupported', detail: 'No finite-fold update shape recognized.' };
|
|
37
|
+
return { kind: 'refused', reason: 'search', detail: 'Finite-fold local goals did not close.' };
|
|
38
|
+
}
|
|
39
|
+
const { source, ms } = workerData;
|
|
40
|
+
try {
|
|
41
|
+
const [machine, law] = JSON.parse(source);
|
|
42
|
+
parentPort.postMessage(propose(machine, law, ms));
|
|
43
|
+
}
|
|
44
|
+
catch (e) {
|
|
45
|
+
parentPort.postMessage({ kind: 'refused', reason: 'search', detail: String(e) });
|
|
46
|
+
}
|