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,179 @@
|
|
|
1
|
+
const fail = (reason) => ({ ok: false, reason });
|
|
2
|
+
const own = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
|
|
3
|
+
const binary = (op) => op === 'add' || op === 'sub' || op === 'mul' || op === 'div' || op === 'rem';
|
|
4
|
+
const dividing = (op) => op === 'div' || op === 'rem';
|
|
5
|
+
function snapshot(spec) {
|
|
6
|
+
if (spec === null || typeof spec !== 'object')
|
|
7
|
+
return { ok: false, reason: 'invalid spec' };
|
|
8
|
+
let s;
|
|
9
|
+
try {
|
|
10
|
+
const proto = Object.getPrototypeOf(spec);
|
|
11
|
+
if (proto !== Object.prototype && proto !== null)
|
|
12
|
+
return { ok: false, reason: 'invalid spec' };
|
|
13
|
+
const ds = Object.getOwnPropertyDescriptors(spec);
|
|
14
|
+
const keys = Reflect.ownKeys(ds);
|
|
15
|
+
if (keys.some(k => typeof k !== 'string'))
|
|
16
|
+
return { ok: false, reason: 'invalid spec' };
|
|
17
|
+
s = Object.create(null);
|
|
18
|
+
for (const k of keys) {
|
|
19
|
+
if (!['width', 'signed', 'operation', 'overflow', 'division'].includes(k))
|
|
20
|
+
return { ok: false, reason: 'unknown spec field' };
|
|
21
|
+
const d = ds[k];
|
|
22
|
+
if (!own(d, 'value'))
|
|
23
|
+
return { ok: false, reason: 'invalid spec' };
|
|
24
|
+
s[k] = d.value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return { ok: false, reason: 'invalid spec' };
|
|
29
|
+
}
|
|
30
|
+
if (!own(s, 'width') || ![8, 16, 32, 64].includes(s.width))
|
|
31
|
+
return { ok: false, reason: 'invalid width' };
|
|
32
|
+
if (!own(s, 'signed') || typeof s.signed !== 'boolean')
|
|
33
|
+
return { ok: false, reason: 'invalid signed flag' };
|
|
34
|
+
if (!own(s, 'operation') || !['add', 'sub', 'mul', 'neg', 'cast', 'div', 'rem'].includes(s.operation))
|
|
35
|
+
return { ok: false, reason: 'invalid operation' };
|
|
36
|
+
if (!own(s, 'overflow') || !['WRAP', 'SATURATE', 'TRAP'].includes(s.overflow))
|
|
37
|
+
return { ok: false, reason: 'invalid overflow mode' };
|
|
38
|
+
const hasDivision = own(s, 'division');
|
|
39
|
+
if (hasDivision !== dividing(s.operation) ||
|
|
40
|
+
(hasDivision && !['FLOOR', 'TRUNCATING', 'EUCLIDEAN'].includes(s.division)))
|
|
41
|
+
return { ok: false, reason: 'invalid division mode' };
|
|
42
|
+
const clean = { width: s.width, signed: s.signed, operation: s.operation, overflow: s.overflow };
|
|
43
|
+
if (hasDivision)
|
|
44
|
+
clean.division = s.division;
|
|
45
|
+
return { ok: true, spec: clean };
|
|
46
|
+
}
|
|
47
|
+
export function evaluatePrimitive(spec, a, b) {
|
|
48
|
+
const parsed = snapshot(spec);
|
|
49
|
+
return parsed.ok ? evaluateSnapshot(parsed.spec, a, b) : fail(parsed.reason);
|
|
50
|
+
}
|
|
51
|
+
function evaluateSnapshot(spec, a, b) {
|
|
52
|
+
if (typeof a !== 'bigint')
|
|
53
|
+
return fail('a must be bigint');
|
|
54
|
+
const needsB = binary(spec.operation);
|
|
55
|
+
if (needsB ? typeof b !== 'bigint' : b !== undefined)
|
|
56
|
+
return fail(needsB ? 'b must be bigint' : 'unexpected b');
|
|
57
|
+
const bits = BigInt(spec.width);
|
|
58
|
+
const modulus = 1n << bits;
|
|
59
|
+
const min = spec.signed ? -(modulus >> 1n) : 0n;
|
|
60
|
+
const max = spec.signed ? (modulus >> 1n) - 1n : modulus - 1n;
|
|
61
|
+
// Cast is conversion from an unbounded exact integer; other operands are values of this type.
|
|
62
|
+
if (spec.operation !== 'cast' && (a < min || a > max))
|
|
63
|
+
return fail('a out of bounds');
|
|
64
|
+
if (needsB && (b < min || b > max))
|
|
65
|
+
return fail('b out of bounds');
|
|
66
|
+
let raw;
|
|
67
|
+
switch (spec.operation) {
|
|
68
|
+
case 'add':
|
|
69
|
+
raw = a + b;
|
|
70
|
+
break;
|
|
71
|
+
case 'sub':
|
|
72
|
+
raw = a - b;
|
|
73
|
+
break;
|
|
74
|
+
case 'mul':
|
|
75
|
+
raw = a * b;
|
|
76
|
+
break;
|
|
77
|
+
case 'neg':
|
|
78
|
+
raw = -a;
|
|
79
|
+
break;
|
|
80
|
+
case 'cast':
|
|
81
|
+
raw = a;
|
|
82
|
+
break;
|
|
83
|
+
case 'div':
|
|
84
|
+
case 'rem': {
|
|
85
|
+
if (b === 0n)
|
|
86
|
+
return fail('division by zero');
|
|
87
|
+
let q = a / b; // BigInt division truncates towards zero.
|
|
88
|
+
let r = a % b;
|
|
89
|
+
if (spec.division === 'FLOOR' && r !== 0n && ((r < 0n) !== (b < 0n))) {
|
|
90
|
+
q -= 1n;
|
|
91
|
+
r += b;
|
|
92
|
+
}
|
|
93
|
+
else if (spec.division === 'EUCLIDEAN' && r < 0n) {
|
|
94
|
+
const sign = b < 0n ? -1n : 1n;
|
|
95
|
+
q -= sign;
|
|
96
|
+
r += b * sign;
|
|
97
|
+
}
|
|
98
|
+
// Division-pair trap: even rem(MIN, -1) fails when its quotient overflows.
|
|
99
|
+
// WRAP/SATURATE still return the exact remainder (zero in that case).
|
|
100
|
+
if (spec.operation === 'rem' && spec.overflow === 'TRAP' && (q < min || q > max))
|
|
101
|
+
return fail('overflow');
|
|
102
|
+
raw = spec.operation === 'div' ? q : r;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (raw >= min && raw <= max)
|
|
107
|
+
return { ok: true, value: raw };
|
|
108
|
+
if (spec.overflow === 'TRAP')
|
|
109
|
+
return fail('overflow');
|
|
110
|
+
if (spec.overflow === 'SATURATE')
|
|
111
|
+
return { ok: true, value: raw < min ? min : max };
|
|
112
|
+
const unsigned = ((raw % modulus) + modulus) % modulus;
|
|
113
|
+
return { ok: true, value: spec.signed && unsigned >= (modulus >> 1n) ? unsigned - modulus : unsigned };
|
|
114
|
+
}
|
|
115
|
+
export function checkWrapCase(spec, a, b, c) {
|
|
116
|
+
const parsed = snapshot(spec);
|
|
117
|
+
if (!parsed.ok)
|
|
118
|
+
return false;
|
|
119
|
+
const s = parsed.spec;
|
|
120
|
+
if (s.overflow !== 'WRAP' || !['add', 'sub', 'mul', 'neg', 'cast'].includes(s.operation))
|
|
121
|
+
return false;
|
|
122
|
+
if (typeof a !== 'bigint' || c === null || typeof c !== 'object')
|
|
123
|
+
return false;
|
|
124
|
+
let q, u, y;
|
|
125
|
+
try {
|
|
126
|
+
if (Object.getPrototypeOf(c) !== Object.prototype && Object.getPrototypeOf(c) !== null)
|
|
127
|
+
return false;
|
|
128
|
+
const ds = Object.getOwnPropertyDescriptors(c);
|
|
129
|
+
if (Reflect.ownKeys(ds).length !== 3 || !['quotient', 'residue', 'value'].every(k => own(ds, k) && own(ds[k], 'value') && typeof ds[k].value === 'bigint'))
|
|
130
|
+
return false;
|
|
131
|
+
q = ds.quotient.value;
|
|
132
|
+
u = ds.residue.value;
|
|
133
|
+
y = ds.value.value;
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
const m = 1n << BigInt(s.width), half = m >> 1n;
|
|
139
|
+
const lo = s.signed ? -half : 0n, hi = s.signed ? half - 1n : m - 1n;
|
|
140
|
+
if (s.operation !== 'cast' && (a < lo || a > hi))
|
|
141
|
+
return false;
|
|
142
|
+
if (binary(s.operation) ? typeof b !== 'bigint' || b < lo || b > hi : b !== undefined)
|
|
143
|
+
return false;
|
|
144
|
+
let raw;
|
|
145
|
+
switch (s.operation) {
|
|
146
|
+
case 'add':
|
|
147
|
+
raw = a + b;
|
|
148
|
+
break;
|
|
149
|
+
case 'sub':
|
|
150
|
+
raw = a - b;
|
|
151
|
+
break;
|
|
152
|
+
case 'mul':
|
|
153
|
+
raw = a * b;
|
|
154
|
+
break;
|
|
155
|
+
case 'neg':
|
|
156
|
+
raw = -a;
|
|
157
|
+
break;
|
|
158
|
+
case 'cast':
|
|
159
|
+
raw = a;
|
|
160
|
+
break;
|
|
161
|
+
case 'div':
|
|
162
|
+
case 'rem': return false;
|
|
163
|
+
}
|
|
164
|
+
return u >= 0n && u < m && raw === q * m + u &&
|
|
165
|
+
y === (s.signed && u >= half ? u - m : u) && y >= lo && y <= hi &&
|
|
166
|
+
raw - y === (q + (s.signed && u >= half ? 1n : 0n)) * m;
|
|
167
|
+
}
|
|
168
|
+
// Valid operands/results cross JSON as exact decimal strings. An invalid supplied
|
|
169
|
+
// operand is explicitly marked <invalid>, never silently changed into an absent one.
|
|
170
|
+
// Diagnostic receipts are not proof certificates and must not be accepted as such.
|
|
171
|
+
export function evaluatePrimitiveReceipt(spec, a, b) {
|
|
172
|
+
const parsed = snapshot(spec);
|
|
173
|
+
const result = parsed.ok ? evaluateSnapshot(parsed.spec, a, b) : fail(parsed.reason);
|
|
174
|
+
return {
|
|
175
|
+
spec: parsed.ok ? parsed.spec : null, a: typeof a === 'bigint' ? a.toString(10) : '<invalid>',
|
|
176
|
+
...(b === undefined ? {} : { b: typeof b === 'bigint' ? b.toString(10) : '<invalid>' }),
|
|
177
|
+
result: result.ok ? { ok: true, value: result.value.toString(10) } : result,
|
|
178
|
+
};
|
|
179
|
+
}
|
package/dist/kernel/kernel.d.ts
CHANGED
package/dist/kernel/kernel.js
CHANGED
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
import { checkRefutation } from "../core/check.js";
|
|
21
21
|
import { observeCheckStep, observeTableCells } from "../core/meter.js";
|
|
22
22
|
import { admittedLinearSub as rowSub, admittedLinearAdd as rowAdd, admittedLinearScale as rowScale, admittedLinearCopy as rowCopy, admittedLinearVariable as rowVariable, visitLinearCells } from "../core/linear-admission.js";
|
|
23
|
+
import { parseWordDivisionName, wordDivisionArgs, wordDivisionFacts } from "./word-division.js";
|
|
23
24
|
import { fundedAdd as add, fundedConstant as k, fundedScale as scale, fundedSub as sub, fundedVariable as v, admittedLinearProduct, everyLinearCell } from "../core/linear-admission.js";
|
|
24
25
|
import { workMap, workFilter, workConcat, workStrings, workJoin, workSlice, workSetArray, workEntries, workJSON, workFlatMap, workArraySlice } from "../core/work-collections.js";
|
|
25
26
|
import { completeWorkProfile } from "../core/work-profile.js";
|
|
@@ -1123,7 +1124,33 @@ const across = (r, c, state, maps, first) => substC(c, {
|
|
|
1123
1124
|
maps: Object.fromEntries(maps.map(map => [`${map}'`, (key) => { const f = own(r.at.maps, map); return f ? f(key) : { op: 'get', map, key }; }])),
|
|
1124
1125
|
flags: first.flags,
|
|
1125
1126
|
});
|
|
1126
|
-
|
|
1127
|
+
// A plain range/comparison goal does not need convolution hypotheses. Keeping
|
|
1128
|
+
// irrelevant products out avoids eagerly solving nonlinear constraints merely
|
|
1129
|
+
// to prove a digit bound. Omitting valid lemmas can only lose proofs, not invent
|
|
1130
|
+
// one. A reserved helper read is opaque here, even when its arguments compute.
|
|
1131
|
+
const divisionArithmetic = (x, seen = new Set()) => {
|
|
1132
|
+
if (!x || typeof x !== 'object' || seen.has(x))
|
|
1133
|
+
return false;
|
|
1134
|
+
seen.add(x);
|
|
1135
|
+
const t = x;
|
|
1136
|
+
if (t.op === 'get' && typeof t.map === 'string' && parseWordDivisionName(t.map))
|
|
1137
|
+
return false;
|
|
1138
|
+
if (t.op === 'prod' || t.op === 'add' || t.op === 'sub' || t.op === 'mul')
|
|
1139
|
+
return true;
|
|
1140
|
+
return Object.values(x).some(value => divisionArithmetic(value, seen));
|
|
1141
|
+
};
|
|
1142
|
+
const goal = (name, r, conclusion) => {
|
|
1143
|
+
const hyps = [...r.hyps, { op: 'not', x: conclusion }];
|
|
1144
|
+
// These are kernel-generated consequences of the reserved total Nat helpers,
|
|
1145
|
+
// never caller-supplied assumptions. Domain guards preserve invalid-input cases.
|
|
1146
|
+
const facts = wordDivisionFacts(hyps);
|
|
1147
|
+
if (!facts.length)
|
|
1148
|
+
return { name, hyps };
|
|
1149
|
+
const arithmetic = divisionArithmetic(conclusion);
|
|
1150
|
+
const theory = facts.filter(c => arithmetic || !divisionArithmetic(c));
|
|
1151
|
+
// Search treats the final hypothesis as the negated conclusion.
|
|
1152
|
+
return { name, hyps: [...r.hyps, ...theory, { op: 'not', x: conclusion }], wordDivision: true };
|
|
1153
|
+
};
|
|
1127
1154
|
// A machine's flags as its terms read them (decided before the step, over the frame it starts from): a plain flag its
|
|
1128
1155
|
// condition (through the flags before it), a quantified flag (one whose condition holds at every key) its 0/1 variable
|
|
1129
1156
|
// ?f, which `closing` ties to the condition.
|
|
@@ -1202,10 +1229,17 @@ function reads(t, out = { vars: new Set(), maps: new Set(), flags: new Set() })
|
|
|
1202
1229
|
out.vars.add(t.name);
|
|
1203
1230
|
break;
|
|
1204
1231
|
case 'const': break;
|
|
1205
|
-
case 'get':
|
|
1206
|
-
|
|
1232
|
+
case 'get': {
|
|
1233
|
+
const builtin = parseWordDivisionName(t.map);
|
|
1234
|
+
if (builtin) {
|
|
1235
|
+
if (!wordDivisionArgs(t.key, builtin.width))
|
|
1236
|
+
throw new Error('invalid compact word-division argument tuple');
|
|
1237
|
+
}
|
|
1238
|
+
else
|
|
1239
|
+
out.maps.add(t.map);
|
|
1207
1240
|
reads(t.key, out);
|
|
1208
1241
|
break;
|
|
1242
|
+
}
|
|
1209
1243
|
case 'flag':
|
|
1210
1244
|
out.flags.add(t.name);
|
|
1211
1245
|
break;
|
|
@@ -1289,6 +1323,11 @@ export function scope(m, laws = []) {
|
|
|
1289
1323
|
// (a count map is read as a map the request carries; a law reads the counts of the state, countsScope says which)
|
|
1290
1324
|
const counts = Object.keys(m.counts ?? {}), carried = [...(m.inputMaps ?? []), ...counts];
|
|
1291
1325
|
const names = [...m.params, ...m.inputs, ...state];
|
|
1326
|
+
// Intrinsic reads are interpreted before ordinary maps/functions in BOTH
|
|
1327
|
+
// runtimes. A declaration/write in this namespace must never impersonate one.
|
|
1328
|
+
for (const name of [...maps, ...carried, ...fns])
|
|
1329
|
+
if (name.startsWith('worddiv$'))
|
|
1330
|
+
throw new Error('worddiv$ is reserved for compact integer-division helpers');
|
|
1292
1331
|
distinct(m.name, [...names, ...maps, ...carried, ...fns]);
|
|
1293
1332
|
for (const name of [...names, ...maps, ...carried, ...fns, ...flags])
|
|
1294
1333
|
plainName(m.name, name);
|
|
@@ -2272,10 +2311,10 @@ export function stepLawObligations(machine, laws, instances, sl) {
|
|
|
2272
2311
|
if (sl.keys?.length && (!sl.forall || new Set([sl.forall, ...sl.keys]).size !== sl.keys.length + 1))
|
|
2273
2312
|
throw new Error(`step law ${sl.name}: further keys come with a first, each a different name`);
|
|
2274
2313
|
unquantified(`step law ${sl.name}`, sl.then);
|
|
2275
|
-
inScope(`step law ${sl.name}`, [sl.then], [...taken, ...state.map(n => `${n}'`), ...later, ...(sl.forall ? [sl.forall] : []), ...(sl.keys ?? [])], [...maps, ...carried, ...maps.map(n => `${n}'`), ...laterMaps], flagNames);
|
|
2314
|
+
inScope(`step law ${sl.name}`, [sl.then], [...taken, ...state.map(n => `${n}'`), ...later, ...(sl.forall ? [sl.forall] : []), ...(sl.keys ?? [])], [...maps, ...carried, ...maps.map(n => `${n}'`), ...laterMaps, ...Object.keys(m.fns ?? {})], flagNames);
|
|
2276
2315
|
for (const w of whens) {
|
|
2277
2316
|
unquantified(`step law ${sl.name}'s when`, w);
|
|
2278
|
-
inScope(`step law ${sl.name}'s when`, [w], taken, [...maps, ...carried], flagNames);
|
|
2317
|
+
inScope(`step law ${sl.name}'s when`, [w], taken, [...maps, ...carried, ...Object.keys(m.fns ?? {})], flagNames);
|
|
2279
2318
|
}
|
|
2280
2319
|
if (!naturalC(sl.then) || !whens.every(naturalC))
|
|
2281
2320
|
throw new Error(`step law ${sl.name}: a negative constant or factor is outside the kernel`);
|
package/dist/kernel/prove.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { retainFamily, retainFrom } from "../core/proof-output.js";
|
|
2
|
+
import { wordDivisionCuts } from "./word-division-cuts.js";
|
|
2
3
|
import { checkWatchdog } from "../core/search-watchdog.js";
|
|
3
4
|
// An untrusted prover for the kernel: grows a split tree until every leaf's constraints are refuted by solve(), splitting
|
|
4
5
|
// on whatever atom the hypotheses leave open (an if's condition, a min's or ∸'s comparison, a disjunction's atoms, two
|
|
@@ -926,6 +927,18 @@ const tupledScope = () => !!eprScope.patterns && Object.values(eprScope.patterns
|
|
|
926
927
|
// (a part of a wide goal by the EPR path first, its core widened to the part, else the usual search)
|
|
927
928
|
const eprPart = (gp, until) => eprFirst(gp, { nodes: 0, until }) ?? grow(gp, new Map(), { nodes: 0, until });
|
|
928
929
|
function growRoot(goal, path, budget) {
|
|
930
|
+
// Finite-model preflight can spend its whole share enumerating 16-bit digit
|
|
931
|
+
// ranges before noticing an immediate linear contradiction. Try the existing
|
|
932
|
+
// bounded plain search first for compact word goals. Only accept a proof tree
|
|
933
|
+
// (never a relaxed model); the same kernel checks it below as every other tree.
|
|
934
|
+
if (!path.size && !budget.nodes && goal.wordDivision) {
|
|
935
|
+
const local = wordDivisionCuts(goal, (g, p) => { const r = grow(g, p, budget); return 'tree' in r ? r.tree : undefined; }, widen);
|
|
936
|
+
if (local)
|
|
937
|
+
return { tree: local, nodes: budget.nodes };
|
|
938
|
+
const quick = growPlain(goal, path, { nodes: 0, until: budget.until, cap: 64 });
|
|
939
|
+
if ('tree' in quick)
|
|
940
|
+
return quick;
|
|
941
|
+
}
|
|
929
942
|
const wide = !path.size && !budget.nodes && wideFor(goal), epr = !!eprScope.patterns && !eprScope.busy;
|
|
930
943
|
// (a wide goal's parts by the EPR path too, where the goal is EPR-shaped)
|
|
931
944
|
if (wide) {
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type E } from '../core/machine.js';
|
|
2
|
+
import { type Goal, type Path, type Tree } from './kernel.js';
|
|
3
|
+
type Prove = (goal: Goal, path: Path) => Tree | undefined;
|
|
4
|
+
type Widen = (part: Goal, whole: Goal, tree: Tree, opaque: Record<string, E>, pp: Path, wp: Path) => Tree | undefined;
|
|
5
|
+
export declare function wordDivisionCuts(goal: Goal, prove: Prove, widen: Widen): Tree | undefined;
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// Untrusted local lemmas: every branch and the final tree use the ordinary checker.
|
|
2
|
+
// Avoid combining four radix columns into an unsafe 2^64 intermediate by proving
|
|
3
|
+
// c_i = multiplicationCarry_i + additionCarry_i one column at a time.
|
|
4
|
+
import { walkOnce } from "../core/machine.js";
|
|
5
|
+
import { add, scale } from "../core/solver.js";
|
|
6
|
+
import { canon, check, linear } from "./kernel.js";
|
|
7
|
+
import { parseWordDivisionName, wordDivisionArgs, wordDivisionName } from "./word-division.js";
|
|
8
|
+
export function wordDivisionCuts(goal, prove, widen) {
|
|
9
|
+
let found, modular = false;
|
|
10
|
+
walkOnce(goal.hyps.at(-1), o => {
|
|
11
|
+
const e = o;
|
|
12
|
+
if (e.op !== 'get')
|
|
13
|
+
return;
|
|
14
|
+
if (e.map === 'mod$65536')
|
|
15
|
+
modular = true;
|
|
16
|
+
if (parseWordDivisionName(e.map)?.width === 64)
|
|
17
|
+
found ??= e;
|
|
18
|
+
});
|
|
19
|
+
if (!found || !modular)
|
|
20
|
+
return;
|
|
21
|
+
const key = found.key, args = wordDivisionArgs(key, 64);
|
|
22
|
+
if (!args)
|
|
23
|
+
return;
|
|
24
|
+
const zero = { op: 'const', value: 0 };
|
|
25
|
+
const sum = (xs) => xs.reduce((a, b) => ({ op: 'add', a, b }), zero);
|
|
26
|
+
const get = (part, i) => ({ op: 'get', map: wordDivisionName(64, part, i), key });
|
|
27
|
+
const call = (map, key) => ({ op: 'get', map, key });
|
|
28
|
+
let mp = zero, ap = zero;
|
|
29
|
+
const cuts = [];
|
|
30
|
+
for (let i = 0; i < 4; i++) {
|
|
31
|
+
const col = sum([mp, ...Array.from({ length: i + 1 }, (_, j) => ({ op: 'prod', a: get('q', j), b: args[4 + i - j] }))]);
|
|
32
|
+
const digit = call('mod$65536', col);
|
|
33
|
+
mp = call('div$65536', col);
|
|
34
|
+
ap = call('div$65536', sum([digit, get('r', i), ap]));
|
|
35
|
+
const a = get('c', i + 1), b = sum([mp, ap]), la = linear(a, new Map()), lb = linear(b, new Map());
|
|
36
|
+
if (!la || !lb)
|
|
37
|
+
return;
|
|
38
|
+
cuts.push({ a, b, lin: add(la, scale(lb, -1)) });
|
|
39
|
+
}
|
|
40
|
+
const branch = (p, lin, holds) => new Map(p).set(canon(lin), { lin, holds });
|
|
41
|
+
const ordinary = (g, p) => {
|
|
42
|
+
const tree = prove(g, p);
|
|
43
|
+
return tree && check(g, tree, p) ? tree : undefined;
|
|
44
|
+
};
|
|
45
|
+
const walk = (i, p) => {
|
|
46
|
+
if (i === cuts.length)
|
|
47
|
+
return ordinary(goal, p);
|
|
48
|
+
const cut = cuts[i], l = cut.lin, neg = scale(l, -1);
|
|
49
|
+
if (p.has(canon(l)) || p.has(canon(neg)) || Object.keys(l).every(k => k === '1'))
|
|
50
|
+
return;
|
|
51
|
+
const part = { name: `word carry ${i + 1}`, hyps: [...goal.hyps.slice(0, -1), { op: 'not', x: { op: '=', a: cut.a, b: cut.b } }] };
|
|
52
|
+
const noP = branch(p, l, false), yesP = branch(p, l, true), lowP = branch(yesP, neg, false), eqP = branch(yesP, neg, true);
|
|
53
|
+
const no = ordinary(part, noP), low = ordinary(part, lowP);
|
|
54
|
+
if (!no || !low)
|
|
55
|
+
return;
|
|
56
|
+
const noWide = widen(part, goal, no, {}, noP, noP), lowWide = widen(part, goal, low, {}, lowP, lowP);
|
|
57
|
+
if (!noWide || !lowWide)
|
|
58
|
+
return;
|
|
59
|
+
const rest = walk(i + 1, eqP);
|
|
60
|
+
return rest ? { split: l, yes: { split: neg, yes: rest, no: lowWide }, no: noWide } : undefined;
|
|
61
|
+
};
|
|
62
|
+
const d = linear(sum(args.slice(4)), new Map());
|
|
63
|
+
if (!d)
|
|
64
|
+
return;
|
|
65
|
+
const nonzero = add({ '1': 1 }, scale(d, -1)), yesP = branch(new Map(), nonzero, true), noP = branch(new Map(), nonzero, false);
|
|
66
|
+
const yes = walk(0, yesP), no = ordinary(goal, noP);
|
|
67
|
+
const tree = yes && no ? { split: nonzero, yes, no } : undefined;
|
|
68
|
+
return tree && check(goal, tree) ? tree : undefined;
|
|
69
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { C, E } from '../core/machine.js';
|
|
2
|
+
export type WordDivisionSpec = {
|
|
3
|
+
width: 8 | 16 | 32 | 64;
|
|
4
|
+
part: 'q' | 'r' | 'c';
|
|
5
|
+
index: number;
|
|
6
|
+
};
|
|
7
|
+
export declare function wordDivisionName(width: WordDivisionSpec['width'], part: WordDivisionSpec['part'], index: number): string;
|
|
8
|
+
export declare function parseWordDivisionName(name: string): WordDivisionSpec | undefined;
|
|
9
|
+
export declare function wordDivisionKey(args: E[]): E;
|
|
10
|
+
export declare function wordDivisionArgs(key: E, width: WordDivisionSpec['width']): E[] | undefined;
|
|
11
|
+
export declare function wordDivisionValue(width: 8 | 16 | 32 | 64, part: 'q' | 'r' | 'c', index: number, values: number[]): number;
|
|
12
|
+
export declare function wordDivisionFacts(terms: unknown[]): C[];
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
export function wordDivisionName(width, part, index) {
|
|
2
|
+
if (![8, 16, 32, 64].includes(width) || !['q', 'r', 'c'].includes(part) || !Number.isSafeInteger(index) ||
|
|
3
|
+
index < (part === 'c' ? 1 : 0) || index > (part === 'c' ? 2 * (width === 8 ? 1 : width / 16) : (width === 8 ? 1 : width / 16) - 1))
|
|
4
|
+
throw new Error('invalid word division name');
|
|
5
|
+
return `worddiv$${width}$${part}$${index}`;
|
|
6
|
+
}
|
|
7
|
+
export function parseWordDivisionName(name) {
|
|
8
|
+
if (typeof name !== 'string')
|
|
9
|
+
return undefined;
|
|
10
|
+
const m = /^worddiv\$(8|16|32|64)\$(q|r|c)\$(0|[1-9][0-9]*)$/.exec(name);
|
|
11
|
+
if (!m)
|
|
12
|
+
return undefined;
|
|
13
|
+
const width = Number(m[1]);
|
|
14
|
+
const part = m[2];
|
|
15
|
+
const index = Number(m[3]);
|
|
16
|
+
const n = width === 8 ? 1 : width / 16;
|
|
17
|
+
return Number.isSafeInteger(index) && (part === 'c' ? index >= 1 && index <= 2 * n : index < n)
|
|
18
|
+
? { width, part, index } : undefined;
|
|
19
|
+
}
|
|
20
|
+
export function wordDivisionKey(args) {
|
|
21
|
+
if (args.length < 2)
|
|
22
|
+
throw new Error('word division key needs at least two arguments');
|
|
23
|
+
return args.slice(0, -1).reduceRight((b, a) => ({ op: 'tup', a, b }), args[args.length - 1]);
|
|
24
|
+
}
|
|
25
|
+
export function wordDivisionArgs(key, width) {
|
|
26
|
+
if (![8, 16, 32, 64].includes(width))
|
|
27
|
+
return undefined;
|
|
28
|
+
const count = 2 * (width === 8 ? 1 : width / 16);
|
|
29
|
+
const args = [];
|
|
30
|
+
let node = key;
|
|
31
|
+
for (let i = 1; i < count; i++) {
|
|
32
|
+
if (node.op !== 'tup')
|
|
33
|
+
return undefined;
|
|
34
|
+
args.push(node.a);
|
|
35
|
+
node = node.b;
|
|
36
|
+
}
|
|
37
|
+
if (node.op === 'tup')
|
|
38
|
+
return undefined;
|
|
39
|
+
args.push(node);
|
|
40
|
+
return args;
|
|
41
|
+
}
|
|
42
|
+
// Deliberately self-contained: compiled JavaScript embeds this function's source.
|
|
43
|
+
// Inputs are safe natural Numbers; every arithmetic step on word data is BigInt.
|
|
44
|
+
export function wordDivisionValue(width, part, index, values) {
|
|
45
|
+
if (width !== 8 && width !== 16 && width !== 32 && width !== 64)
|
|
46
|
+
throw new Error('invalid word division width');
|
|
47
|
+
const n = width === 8 ? 1 : width / 16;
|
|
48
|
+
if (part !== 'q' && part !== 'r' && part !== 'c')
|
|
49
|
+
throw new Error('invalid word division part');
|
|
50
|
+
if (!Number.isSafeInteger(index) || (part === 'c' ? index < 1 || index > 2 * n : index < 0 || index >= n))
|
|
51
|
+
throw new Error('invalid word division index');
|
|
52
|
+
if (!Array.isArray(values) || values.length !== 2 * n)
|
|
53
|
+
throw new Error('invalid word division arity');
|
|
54
|
+
const base = width === 8 ? 256n : 65536n;
|
|
55
|
+
const digits = [];
|
|
56
|
+
for (const value of values) {
|
|
57
|
+
if (!Number.isSafeInteger(value) || value < 0)
|
|
58
|
+
throw new Error('invalid word division operand');
|
|
59
|
+
digits.push(BigInt(value) % base);
|
|
60
|
+
}
|
|
61
|
+
let a = 0n, b = 0n, power = 1n;
|
|
62
|
+
for (let i = 0; i < n; i++) {
|
|
63
|
+
a += digits[i] * power;
|
|
64
|
+
b += digits[n + i] * power;
|
|
65
|
+
power *= base;
|
|
66
|
+
}
|
|
67
|
+
const q = b === 0n ? 0n : a / b;
|
|
68
|
+
const r = b === 0n ? 0n : a % b;
|
|
69
|
+
if (part === 'q' || part === 'r')
|
|
70
|
+
return Number(((part === 'q' ? q : r) / (base ** BigInt(index))) % base);
|
|
71
|
+
let carry = 0n;
|
|
72
|
+
for (let col = 0; col < index; col++) {
|
|
73
|
+
let total = carry + (col < n ? (r / (base ** BigInt(col))) % base : 0n);
|
|
74
|
+
for (let j = 0; j < n; j++) {
|
|
75
|
+
const k = col - j;
|
|
76
|
+
if (k >= 0 && k < n)
|
|
77
|
+
total += ((q / (base ** BigInt(j))) % base) * digits[n + k];
|
|
78
|
+
}
|
|
79
|
+
carry = total / base;
|
|
80
|
+
}
|
|
81
|
+
return Number(carry);
|
|
82
|
+
}
|
|
83
|
+
// Negative-only memo: skipping a subsequently mutated subtree can only omit
|
|
84
|
+
// optional consequences, never add a stale trusted assumption. Positive facts
|
|
85
|
+
// are always rebuilt from the current syntax. Legacy shared ASTs need no rescan.
|
|
86
|
+
const wordFree = new WeakSet();
|
|
87
|
+
export function wordDivisionFacts(terms) {
|
|
88
|
+
const out = [];
|
|
89
|
+
const seen = new Set();
|
|
90
|
+
const keys = new Set();
|
|
91
|
+
const constant = (value) => ({ op: 'const', value });
|
|
92
|
+
const add = (xs) => xs.reduce((a, b) => ({ op: 'add', a, b }), constant(0));
|
|
93
|
+
const eq = (a, b) => ({ op: '=', a, b });
|
|
94
|
+
const lt = (a, b) => ({ op: '<', a, b });
|
|
95
|
+
const le = (a, b) => ({ op: '<=', a, b });
|
|
96
|
+
const both = (xs) => ({ op: 'and', xs });
|
|
97
|
+
const either = (xs) => ({ op: 'or', xs });
|
|
98
|
+
const digitLex = (a, b, strict) => {
|
|
99
|
+
const alternatives = [];
|
|
100
|
+
for (let i = a.length - 1; i >= 0; i--)
|
|
101
|
+
alternatives.push(both([...a.slice(i + 1).map((x, j) => eq(x, b[i + 1 + j])), lt(a[i], b[i])]));
|
|
102
|
+
if (!strict)
|
|
103
|
+
alternatives.push(both(a.map((x, i) => eq(x, b[i]))));
|
|
104
|
+
return either(alternatives);
|
|
105
|
+
};
|
|
106
|
+
const visit = (value) => {
|
|
107
|
+
if (!value || typeof value !== 'object' || wordFree.has(value))
|
|
108
|
+
return false;
|
|
109
|
+
if (seen.has(value))
|
|
110
|
+
return true; // conservative for shared/cyclic syntax
|
|
111
|
+
seen.add(value);
|
|
112
|
+
let hasWord = false;
|
|
113
|
+
if (!Array.isArray(value)) {
|
|
114
|
+
const node = value;
|
|
115
|
+
if (node.op === 'get' && typeof node.map === 'string') {
|
|
116
|
+
hasWord = node.map.startsWith('worddiv$');
|
|
117
|
+
const spec = parseWordDivisionName(node.map);
|
|
118
|
+
if (spec && node.key && typeof node.key === 'object') {
|
|
119
|
+
const key = node.key;
|
|
120
|
+
const args = wordDivisionArgs(key, spec.width);
|
|
121
|
+
if (args) {
|
|
122
|
+
const tag = `${spec.width}:${JSON.stringify(key)}`;
|
|
123
|
+
if (!keys.has(tag)) {
|
|
124
|
+
keys.add(tag);
|
|
125
|
+
const n = spec.width === 8 ? 1 : spec.width / 16, base = spec.width === 8 ? 256 : 65536;
|
|
126
|
+
const a = args.slice(0, n), b = args.slice(n);
|
|
127
|
+
const get = (part, i) => ({ op: 'get', map: wordDivisionName(spec.width, part, i), key });
|
|
128
|
+
const q = Array.from({ length: n }, (_, i) => get('q', i));
|
|
129
|
+
const r = Array.from({ length: n }, (_, i) => get('r', i));
|
|
130
|
+
const c = Array.from({ length: 2 * n + 1 }, (_, i) => i === 0 ? constant(0) : get('c', i));
|
|
131
|
+
for (const x of [...q, ...r])
|
|
132
|
+
out.push(lt(x, constant(base)));
|
|
133
|
+
for (let i = 1; i <= 2 * n; i++)
|
|
134
|
+
out.push(le(c[i], constant(n * base)));
|
|
135
|
+
const domain = both([...args.map(x => lt(x, constant(base))), either(b.map(x => lt(constant(0), x)))]);
|
|
136
|
+
const guarded = (fact) => { out.push(either([{ op: 'not', x: domain }, fact])); };
|
|
137
|
+
for (let i = 0; i < 2 * n; i++) {
|
|
138
|
+
const columns = [c[i]];
|
|
139
|
+
if (i < n)
|
|
140
|
+
columns.push(r[i]);
|
|
141
|
+
for (let j = 0; j < n; j++)
|
|
142
|
+
if (i - j >= 0 && i - j < n)
|
|
143
|
+
columns.push({ op: 'prod', a: q[j], b: b[i - j] });
|
|
144
|
+
guarded(eq(add(columns), add([i < n ? a[i] : constant(0), { op: 'mul', k: base, a: c[i + 1] }])));
|
|
145
|
+
}
|
|
146
|
+
// Q*B+R=A fits in n digits, so the carry after the LOW n
|
|
147
|
+
// columns is already zero. Waiting until 2n forces elimination
|
|
148
|
+
// through upper columns with unsafe 2^64 certificate products.
|
|
149
|
+
guarded(eq(c[n], constant(0)));
|
|
150
|
+
guarded(digitLex(r, b, true));
|
|
151
|
+
guarded(digitLex(q, a, false));
|
|
152
|
+
const bOne = both([eq(b[0], constant(1)), ...b.slice(1).map(x => eq(x, constant(0)))]);
|
|
153
|
+
guarded(either([{ op: 'not', x: bOne }, both([...q.map((x, i) => eq(x, a[i])), ...r.map(x => eq(x, constant(0)))])]));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
for (const child of Object.values(value))
|
|
160
|
+
hasWord = visit(child) || hasWord;
|
|
161
|
+
if (!hasWord)
|
|
162
|
+
wordFree.add(value);
|
|
163
|
+
return hasWord;
|
|
164
|
+
};
|
|
165
|
+
visit(terms);
|
|
166
|
+
return out;
|
|
167
|
+
}
|