gdp-wired 0.22.0 → 0.24.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 +27 -2
- package/dist/core/bmc.d.ts +49 -0
- package/dist/core/bmc.js +509 -0
- package/dist/core/check.d.ts +1 -1
- package/dist/core/check.js +19 -6
- package/dist/core/cycles.d.ts +38 -0
- package/dist/core/cycles.js +531 -43
- package/dist/core/data.d.ts +25 -1
- package/dist/core/data.js +984 -215
- package/dist/core/discover.js +8 -3
- package/dist/core/dsl.d.ts +17 -3
- package/dist/core/dsl.js +81 -15
- package/dist/core/extract-check.d.ts +77 -0
- package/dist/core/extract-check.js +785 -0
- package/dist/core/extract-ir.d.ts +139 -0
- package/dist/core/extract-ir.js +198 -0
- package/dist/core/extract-print.d.ts +30 -0
- package/dist/core/extract-print.js +426 -0
- package/dist/core/extract-read.d.ts +298 -0
- package/dist/core/extract-read.js +1298 -0
- package/dist/core/extract-shapes.d.ts +13 -0
- package/dist/core/extract-shapes.js +993 -0
- package/dist/core/extract-xstate.d.ts +34 -0
- package/dist/core/extract-xstate.js +320 -0
- package/dist/core/extract.d.ts +31 -0
- package/dist/core/extract.js +249 -0
- package/dist/core/infer.js +518 -5
- package/dist/core/kind.d.ts +30 -0
- package/dist/core/kind.js +81 -0
- package/dist/core/nonterm.d.ts +34 -0
- package/dist/core/nonterm.js +610 -0
- package/dist/core/q.d.ts +17 -0
- package/dist/core/q.js +52 -0
- package/dist/core/rationals.d.ts +112 -0
- package/dist/core/rationals.js +281 -0
- package/dist/core/relation.d.ts +28 -0
- package/dist/core/relation.js +483 -0
- package/dist/core/solver.d.ts +5 -2
- package/dist/core/solver.js +65 -20
- package/dist/core/transfer.d.ts +3 -3
- package/dist/core/transfer.js +69 -42
- package/dist/core/xstate.d.ts +6 -0
- package/dist/core/xstate.js +70 -12
- package/dist/kernel/cdcl.d.ts +13 -0
- package/dist/kernel/cdcl.js +307 -236
- package/dist/kernel/kernel.d.ts +106 -0
- package/dist/kernel/kernel.js +342 -0
- package/dist/kernel/prove.js +14 -3
- package/dist/kernel/qprove.d.ts +18 -0
- package/dist/kernel/qprove.js +157 -0
- package/dist/kernel/reader.d.ts +14 -0
- package/dist/kernel/reader.js +102 -14
- package/dist/kernel/simplex.d.ts +4 -0
- package/dist/kernel/simplex.js +38 -11
- package/dist/lean-kernel/emit.d.ts +46 -1
- package/dist/lean-kernel/emit.js +125 -16
- package/dist/lean-kernel/library.js +4 -1
- package/dist/lean-kernel/qlean.d.ts +26 -0
- package/dist/lean-kernel/qlean.js +105 -0
- package/package.json +16 -3
package/dist/kernel/kernel.d.ts
CHANGED
|
@@ -91,6 +91,15 @@ export type Loop = {
|
|
|
91
91
|
export declare const loopMachine: (l: Loop) => Machine;
|
|
92
92
|
export declare const loopLiveness: (l: Loop, rank: E | E[]) => Liveness;
|
|
93
93
|
export declare function loopObligations(l: Loop, laws: Law[], rank: E | E[]): Goal[];
|
|
94
|
+
export type Recur = {
|
|
95
|
+
start: Record<string, number>;
|
|
96
|
+
stem: Record<string, number>[];
|
|
97
|
+
set: C;
|
|
98
|
+
wit: Record<string, E>;
|
|
99
|
+
tree: Tree;
|
|
100
|
+
};
|
|
101
|
+
export declare function recurGoal(l: Loop, r: Pick<Recur, 'set' | 'wit'>): Goal;
|
|
102
|
+
export declare function checkRecur(l: Loop, r: Recur): boolean;
|
|
94
103
|
export declare function progressObligations(m: Machine, laws: Law[], instances: Instances, p: {
|
|
95
104
|
name: string;
|
|
96
105
|
when: C;
|
|
@@ -139,6 +148,21 @@ export declare function checkStepLaws(m: Machine, laws: Law[], stepLaws: StepLaw
|
|
|
139
148
|
trees: Record<string, Tree>;
|
|
140
149
|
}): boolean;
|
|
141
150
|
export declare const stepZone: (m: Machine, k: number) => string[];
|
|
151
|
+
export type KInduction = {
|
|
152
|
+
law: Law;
|
|
153
|
+
k: number;
|
|
154
|
+
};
|
|
155
|
+
export declare function kInductionSteps(machine: Machine, kl: KInduction): StepLaw[];
|
|
156
|
+
export declare const kInductionStart: (machine: Machine, kl: KInduction, instances: Instances) => Goal;
|
|
157
|
+
export declare function checkKInduction(m: Machine, laws: Law[], kl: KInduction, proof: {
|
|
158
|
+
laws: {
|
|
159
|
+
instances: Record<string, Instances>;
|
|
160
|
+
trees: Record<string, Tree>;
|
|
161
|
+
};
|
|
162
|
+
instances: Instances;
|
|
163
|
+
perStep?: Record<string, Instances>;
|
|
164
|
+
trees: Record<string, Tree>;
|
|
165
|
+
}): boolean;
|
|
142
166
|
export type Fold = {
|
|
143
167
|
name: string;
|
|
144
168
|
params: string[];
|
|
@@ -149,4 +173,86 @@ export type Fold = {
|
|
|
149
173
|
assume?: C;
|
|
150
174
|
};
|
|
151
175
|
export declare function foldObligations(f: Fold, laws: Law[]): Goal[];
|
|
176
|
+
export type Rat = {
|
|
177
|
+
num: bigint;
|
|
178
|
+
den: bigint;
|
|
179
|
+
};
|
|
180
|
+
export type QE = {
|
|
181
|
+
op: 'var';
|
|
182
|
+
name: string;
|
|
183
|
+
} | {
|
|
184
|
+
op: 'const';
|
|
185
|
+
q: Rat;
|
|
186
|
+
} | {
|
|
187
|
+
op: 'add' | 'sub' | 'min' | 'max';
|
|
188
|
+
a: QE;
|
|
189
|
+
b: QE;
|
|
190
|
+
} | {
|
|
191
|
+
op: 'mul';
|
|
192
|
+
k: Rat;
|
|
193
|
+
a: QE;
|
|
194
|
+
} | {
|
|
195
|
+
op: 'if';
|
|
196
|
+
c: QC;
|
|
197
|
+
then: QE;
|
|
198
|
+
else: QE;
|
|
199
|
+
};
|
|
200
|
+
export type QC = {
|
|
201
|
+
op: '<=' | '<' | '=';
|
|
202
|
+
a: QE;
|
|
203
|
+
b: QE;
|
|
204
|
+
} | {
|
|
205
|
+
op: 'and' | 'or';
|
|
206
|
+
a: QC;
|
|
207
|
+
b: QC;
|
|
208
|
+
} | {
|
|
209
|
+
op: 'not';
|
|
210
|
+
x: QC;
|
|
211
|
+
};
|
|
212
|
+
export type QGoal = {
|
|
213
|
+
name: string;
|
|
214
|
+
hyps: QC[];
|
|
215
|
+
};
|
|
216
|
+
export type QRow = {
|
|
217
|
+
cs: Map<string, bigint>;
|
|
218
|
+
c: bigint;
|
|
219
|
+
};
|
|
220
|
+
export type LF = {
|
|
221
|
+
row: QRow;
|
|
222
|
+
den: bigint;
|
|
223
|
+
};
|
|
224
|
+
export declare const ratNorm: (q: Rat) => Rat;
|
|
225
|
+
export declare const qdif: (a: LF, b: LF) => QRow;
|
|
226
|
+
export declare const qLin: (r: QRow) => Lin | undefined;
|
|
227
|
+
export declare function qdec(path: Path, r: QRow): boolean | undefined;
|
|
228
|
+
export declare function qlinear(e: QE, path: Path): LF | undefined;
|
|
229
|
+
export declare function qtruth(c: QC, path: Path): boolean | undefined;
|
|
230
|
+
export declare function qfacts(c: QC, path: Path, pos?: boolean): [QRow, boolean][];
|
|
231
|
+
export declare function qLeafRows(goal: QGoal, path: Path): Constraint[];
|
|
232
|
+
export declare function checkQTree(goal: QGoal, tree: Tree, path?: Path): boolean;
|
|
233
|
+
export type QMachine = {
|
|
234
|
+
name: string;
|
|
235
|
+
params: string[];
|
|
236
|
+
inputs: string[];
|
|
237
|
+
state: Record<string, QE>;
|
|
238
|
+
step: Record<string, QE>;
|
|
239
|
+
pre?: QC;
|
|
240
|
+
assume?: QC;
|
|
241
|
+
};
|
|
242
|
+
export type QLaw = {
|
|
243
|
+
name: string;
|
|
244
|
+
holds: QC;
|
|
245
|
+
};
|
|
246
|
+
export declare const qTrue: QC;
|
|
247
|
+
export declare function qSubstE(e: QE, s: Record<string, QE>): QE;
|
|
248
|
+
export declare function qSubstC(c: QC, s: Record<string, QE>): QC;
|
|
249
|
+
export declare function qOccurs(t: QE | QC, names: string[]): boolean;
|
|
250
|
+
export declare function qObligations(m: QMachine, laws: QLaw[], law: QLaw): {
|
|
251
|
+
start: QGoal;
|
|
252
|
+
step: QGoal;
|
|
253
|
+
};
|
|
254
|
+
export declare function checkQMachine(m: QMachine, laws: QLaw[], trees: Record<string, {
|
|
255
|
+
start: Tree;
|
|
256
|
+
step: Tree;
|
|
257
|
+
}>): boolean;
|
|
152
258
|
export {};
|
package/dist/kernel/kernel.js
CHANGED
|
@@ -1091,6 +1091,84 @@ export function loopObligations(l, laws, rank) {
|
|
|
1091
1091
|
const m = loopMachine(l), { stay, cover, helps } = livenessParts(m, loopLiveness(l, rank));
|
|
1092
1092
|
return [...obligations(m, laws, {}), ...[stay, cover, ...helps].map(sl => stepLawObligations(m, laws, {}, sl)[0])];
|
|
1093
1093
|
}
|
|
1094
|
+
// The closure goal, built as Lean's recurGoal builds it: [set, ¬(while ∧ (assume ∧ set after the body)), each read at the
|
|
1095
|
+
// witnessed request].
|
|
1096
|
+
export function recurGoal(l, r) {
|
|
1097
|
+
const w = { vars: r.wit, maps: {}, flags: {} };
|
|
1098
|
+
return { name: `${l.name} never ends: the set is closed`, hyps: [r.set, { op: 'not', x: { op: 'and', xs: [substC(l.while, w), substC(l.assume ?? trueC, w),
|
|
1099
|
+
substC(substC(r.set, { vars: l.body, maps: {}, flags: {} }), w)] } }] };
|
|
1100
|
+
}
|
|
1101
|
+
// Lean's evalE and evalC over naturals with every map cell 0 (NonTerm.lean's noMaps): exact, never rounded.
|
|
1102
|
+
const natE = (e, env) => {
|
|
1103
|
+
switch (e.op) {
|
|
1104
|
+
case 'var': return env[e.name] ?? 0n;
|
|
1105
|
+
case 'const': return BigInt(e.value);
|
|
1106
|
+
case 'add': return natE(e.a, env) + natE(e.b, env);
|
|
1107
|
+
case 'sub': {
|
|
1108
|
+
const d = natE(e.a, env) - natE(e.b, env);
|
|
1109
|
+
return d > 0n ? d : 0n;
|
|
1110
|
+
}
|
|
1111
|
+
case 'mul': return BigInt(e.k) * natE(e.a, env);
|
|
1112
|
+
case 'prod': return natE(e.a, env) * natE(e.b, env);
|
|
1113
|
+
case 'min': {
|
|
1114
|
+
const a = natE(e.a, env), b = natE(e.b, env);
|
|
1115
|
+
return a < b ? a : b;
|
|
1116
|
+
}
|
|
1117
|
+
case 'max': {
|
|
1118
|
+
const a = natE(e.a, env), b = natE(e.b, env);
|
|
1119
|
+
return a > b ? a : b;
|
|
1120
|
+
}
|
|
1121
|
+
case 'if': return natC(e.c, env) ? natE(e.then, env) : natE(e.else, env);
|
|
1122
|
+
case 'get': return 0n;
|
|
1123
|
+
default: return unreachable(e);
|
|
1124
|
+
}
|
|
1125
|
+
};
|
|
1126
|
+
const natC = (c, env) => {
|
|
1127
|
+
switch (c.op) {
|
|
1128
|
+
case '<=': return natE(c.a, env) <= natE(c.b, env);
|
|
1129
|
+
case '<': return natE(c.a, env) < natE(c.b, env);
|
|
1130
|
+
case '=': return natE(c.a, env) === natE(c.b, env);
|
|
1131
|
+
case 'and': return c.xs.every(x => natC(x, env));
|
|
1132
|
+
case 'or': return c.xs.some(x => natC(x, env));
|
|
1133
|
+
case 'not': return !natC(c.x, env);
|
|
1134
|
+
case 'flag':
|
|
1135
|
+
case 'all': throw new Error('a loop reads no flag');
|
|
1136
|
+
default: return unreachable(c);
|
|
1137
|
+
}
|
|
1138
|
+
};
|
|
1139
|
+
// Lean's checkRecur: the witness sets only inputs; the start meets the precondition; the stem's turns meet `while` and
|
|
1140
|
+
// the assumption, and the state after them is in the set; the closure goal's tree checks. A certificate outside the
|
|
1141
|
+
// language (names the loop doesn't declare, values that aren't naturals, a set reading a turn's input) is refused.
|
|
1142
|
+
export function checkRecur(l, r) {
|
|
1143
|
+
const names = [...l.params, ...l.inputs, ...Object.keys(l.state)], values = (o) => Object.values(o).every(x => Number.isSafeInteger(x) && x >= 0);
|
|
1144
|
+
distinct(l.name, names);
|
|
1145
|
+
for (const name of Object.keys(l.body))
|
|
1146
|
+
if (!(name in l.state))
|
|
1147
|
+
throw new Error(`${l.name}'s body sets ${name}, which isn't in its state`);
|
|
1148
|
+
inScope(`${l.name}'s start`, Object.values(l.state), l.params);
|
|
1149
|
+
inScope(l.name, [...Object.values(l.body), ...(l.assume ? [l.assume] : [])], names);
|
|
1150
|
+
inScope(`${l.name}'s while`, [l.while], [...l.params, ...Object.keys(l.state)]);
|
|
1151
|
+
inScope(`${l.name}'s precondition`, l.pre ? [l.pre] : [], l.params);
|
|
1152
|
+
inScope(`${l.name}'s recurrent set and witness`, [r.set, ...Object.values(r.wit)], [...l.params, ...Object.keys(l.state)]);
|
|
1153
|
+
if (![...Object.values(l.state), ...Object.values(l.body), ...Object.values(r.wit)].every(natural) || ![l.while, r.set, ...(l.pre ? [l.pre] : []), ...(l.assume ? [l.assume] : [])].every(naturalC))
|
|
1154
|
+
throw new Error('outside the kernel');
|
|
1155
|
+
if (!Object.keys(r.start).every(n => l.params.includes(n)) || !values(r.start) || !r.stem.every(t => Object.keys(t).every(n => l.inputs.includes(n)) && values(t)))
|
|
1156
|
+
throw new Error(`${l.name}: a start names params and a stem's turn inputs, each a natural`);
|
|
1157
|
+
if (!Object.keys(r.wit).every(n => l.inputs.includes(n)))
|
|
1158
|
+
return false;
|
|
1159
|
+
let env = Object.fromEntries(names.map(n => [n, BigInt(r.start[n] ?? 0)]));
|
|
1160
|
+
if (!natC(l.pre ?? trueC, env))
|
|
1161
|
+
return false;
|
|
1162
|
+
const apply = (body, at) => ({ ...at, ...Object.fromEntries(Object.entries(body).map(([n, e]) => [n, natE(e, at)])) });
|
|
1163
|
+
env = apply(l.state, env);
|
|
1164
|
+
for (const turn of r.stem) {
|
|
1165
|
+
const at = { ...env, ...Object.fromEntries(l.inputs.map(n => [n, BigInt(turn[n] ?? 0)])) };
|
|
1166
|
+
if (!natC(l.while, at) || !natC(l.assume ?? trueC, at))
|
|
1167
|
+
return false;
|
|
1168
|
+
env = apply(l.body, at);
|
|
1169
|
+
}
|
|
1170
|
+
return natC(r.set, env) && check(recurGoal(l, r), r.tree);
|
|
1171
|
+
}
|
|
1094
1172
|
// --- progress: laws with "there is a request" ----------------------------------------------------------------------
|
|
1095
1173
|
// "In every reachable state where `when` holds, some request makes `then` true of the step": proven by naming the
|
|
1096
1174
|
// request (a term per input over the state and params) and proving it allowed (the machine's assumption holds of it)
|
|
@@ -1220,6 +1298,38 @@ export const stepZone = (m, k) => [
|
|
|
1220
1298
|
...Array.from({ length: k - 1 }, (_, i) => Object.values(laterNames(m, i + 2).vars).map(e => e.name)).flat(),
|
|
1221
1299
|
...Array.from({ length: k - 1 }, (_, i) => Object.keys(m.state).map(n => stateName(n, i + 1))).flat(),
|
|
1222
1300
|
];
|
|
1301
|
+
export function kInductionSteps(machine, kl) {
|
|
1302
|
+
const m = machine.history ? historyMachine(machine).machine : machine, L = kl.law, k = kl.k;
|
|
1303
|
+
if (L.forall || L.keys?.length || L.some?.length || L.ex)
|
|
1304
|
+
throw new Error(`k-induction of ${L.name}: a law with no key`);
|
|
1305
|
+
if (!Number.isInteger(k) || k < 1 || k > 64)
|
|
1306
|
+
throw new Error(`k-induction of ${L.name}: k is a whole number from 1 to 64`);
|
|
1307
|
+
// (as the twin's checkKInd: the law reads no request's name, flag or witness; the start reads none, nor a state
|
|
1308
|
+
// variable, nor a map; scope below asks the rest)
|
|
1309
|
+
const ext = [...m.inputs, ...quantified(m).flatMap(([f, q]) => [`?${f}`, ...witnessesOf(f, q)]), ...Object.keys(m.step.flags ?? {}).map(f => `?${f}`)];
|
|
1310
|
+
if (mentions(L.holds, ext) || Object.keys(m.state).some(n => ext.includes(n)))
|
|
1311
|
+
throw new Error(`k-induction of ${L.name}: reads a request's name or a flag`);
|
|
1312
|
+
for (const e of Object.values(m.state))
|
|
1313
|
+
if (mentions({ op: '=', a: e, b: e }, [...ext, ...Object.keys(m.state)]) || JSON.stringify(e).includes('"op":"get"'))
|
|
1314
|
+
throw new Error(`k-induction of ${L.name}: a start reads the params alone`);
|
|
1315
|
+
const start = { op: 'and', xs: Object.entries(m.state).map(([n, e]) => ({ op: '=', a: { op: 'var', name: n }, b: e })) };
|
|
1316
|
+
const after = { vars: Object.fromEntries(Object.keys(m.state).map(n => [n, { op: 'var', name: `${n}'` }])), maps: Object.fromEntries(Object.keys(m.maps ?? {}).map(map => [map, (key) => ({ op: 'get', map: `${map}'`, key })])), flags: {} };
|
|
1317
|
+
const then = substC(L.holds, after);
|
|
1318
|
+
return [
|
|
1319
|
+
...Array.from({ length: k - 1 }, (_, i) => ({ name: `${L.name} @base ${i + 1}`, when: [{ op: 'and', xs: [start, L.holds] }, ...Array.from({ length: i }, () => L.holds)], then, steps: i + 1 })),
|
|
1320
|
+
{ name: `${L.name} @k-step`, when: Array.from({ length: k }, () => L.holds), then, steps: k },
|
|
1321
|
+
];
|
|
1322
|
+
}
|
|
1323
|
+
export const kInductionStart = (machine, kl, instances) => {
|
|
1324
|
+
const m = machine.history ? historyMachine(machine).machine : machine;
|
|
1325
|
+
return obligations(m, [kl.law], { [kl.law.name]: instances })[0];
|
|
1326
|
+
};
|
|
1327
|
+
export function checkKInduction(m, laws, kl, proof) {
|
|
1328
|
+
const steps = kInductionSteps(m, kl), plain = m.history ? historyMachine(m).machine : m;
|
|
1329
|
+
scope(plain, [...laws, kl.law]);
|
|
1330
|
+
const start = kInductionStart(m, kl, proof.perStep?.[`${kl.law.name} @start`] ?? proof.instances);
|
|
1331
|
+
return proof.trees[start.name] !== undefined && check(start, proof.trees[start.name]) && checkStepLaws(m, laws, steps, proof);
|
|
1332
|
+
}
|
|
1223
1333
|
export function foldObligations(f, laws) {
|
|
1224
1334
|
if (![...Object.values(f.leaf), ...Object.values(f.node)].every(natural) || !laws.every(law => !law.forall && !law.keys?.length && naturalC(law.holds) && !mentions(law.holds, f.inputs)))
|
|
1225
1335
|
throw new Error('outside the kernel');
|
|
@@ -1239,3 +1349,235 @@ export function foldObligations(f, laws) {
|
|
|
1239
1349
|
const node = step(holds(from(), [...assume, ...laws.flatMap(h => [substC(h.holds, side('l_')), substC(h.holds, side('r_'))])]), { vars: f.node, maps: {} });
|
|
1240
1350
|
return laws.flatMap(law => [goal(`${law.name} @leaf`, leaf, atEnd(leaf, law.holds)), goal(`${law.name} @node`, node, atEnd(node, law.holds))]);
|
|
1241
1351
|
}
|
|
1352
|
+
const bgcd = (a, b) => { a = a < 0n ? -a : a; b = b < 0n ? -b : b; while (b)
|
|
1353
|
+
[a, b] = [b, a % b]; return a; };
|
|
1354
|
+
// Lean's q.num and q.den: the reduced fraction (a denominator <= 0 is no rational).
|
|
1355
|
+
export const ratNorm = (q) => {
|
|
1356
|
+
if (q.den <= 0n)
|
|
1357
|
+
throw new Error('a rational has a positive denominator');
|
|
1358
|
+
const g = bgcd(q.num, q.den) || 1n;
|
|
1359
|
+
return { num: q.num / g, den: q.den / g };
|
|
1360
|
+
};
|
|
1361
|
+
const rscale = (m, r) => ({ cs: new Map(m === 0n ? [] : [...r.cs].map(([x, c]) => [x, m * c])), c: m * r.c });
|
|
1362
|
+
const radd = (a, b) => {
|
|
1363
|
+
const cs = new Map(a.cs);
|
|
1364
|
+
for (const [x, c] of b.cs) {
|
|
1365
|
+
const s = (cs.get(x) ?? 0n) + c;
|
|
1366
|
+
if (s === 0n)
|
|
1367
|
+
cs.delete(x);
|
|
1368
|
+
else
|
|
1369
|
+
cs.set(x, s);
|
|
1370
|
+
}
|
|
1371
|
+
return { cs, c: a.c + b.c };
|
|
1372
|
+
};
|
|
1373
|
+
export const qdif = (a, b) => radd(rscale(b.den, a.row), rscale(-a.den, b.row));
|
|
1374
|
+
// A row as a Lin, its coefficients safe integers (else undefined: a row past 2^53 is not read, so the check is weaker,
|
|
1375
|
+
// never wrong); and its key, as `canon` writes a Lin (zero coefficients dropped: Lean's `same` compares by value).
|
|
1376
|
+
export const qLin = (r) => {
|
|
1377
|
+
const out = {};
|
|
1378
|
+
for (const [x, c] of r.cs) {
|
|
1379
|
+
if (c < -(2n ** 53n) + 1n || c > 2n ** 53n - 1n)
|
|
1380
|
+
return undefined;
|
|
1381
|
+
out[x] = Number(c);
|
|
1382
|
+
}
|
|
1383
|
+
if (r.c !== 0n) {
|
|
1384
|
+
if (r.c < -(2n ** 53n) + 1n || r.c > 2n ** 53n - 1n)
|
|
1385
|
+
return undefined;
|
|
1386
|
+
out['1'] = Number(r.c);
|
|
1387
|
+
}
|
|
1388
|
+
return out;
|
|
1389
|
+
};
|
|
1390
|
+
const linRow = (lin) => {
|
|
1391
|
+
if (!Object.values(lin).every(c => Number.isSafeInteger(c)))
|
|
1392
|
+
return undefined;
|
|
1393
|
+
const cs = new Map();
|
|
1394
|
+
for (const [x, c] of Object.entries(lin))
|
|
1395
|
+
if (x !== '1' && c !== 0)
|
|
1396
|
+
cs.set(x, BigInt(c));
|
|
1397
|
+
return { cs, c: BigInt(lin['1'] ?? 0) };
|
|
1398
|
+
};
|
|
1399
|
+
const rowKey = (r) => { const lin = qLin(r); return lin && canon(lin); };
|
|
1400
|
+
export function qdec(path, r) {
|
|
1401
|
+
if (r.cs.size === 0)
|
|
1402
|
+
return r.c <= 0n;
|
|
1403
|
+
const key = rowKey(r);
|
|
1404
|
+
return key === undefined ? undefined : path.get(key)?.holds;
|
|
1405
|
+
}
|
|
1406
|
+
export function qlinear(e, path) {
|
|
1407
|
+
switch (e.op) {
|
|
1408
|
+
case 'var': return e.name === '1' ? undefined : { row: { cs: new Map([[e.name, 1n]]), c: 0n }, den: 1n };
|
|
1409
|
+
case 'const': {
|
|
1410
|
+
const q = ratNorm(e.q);
|
|
1411
|
+
return { row: { cs: new Map(), c: q.num }, den: q.den };
|
|
1412
|
+
}
|
|
1413
|
+
case 'add':
|
|
1414
|
+
case 'sub': {
|
|
1415
|
+
const a = qlinear(e.a, path), b = qlinear(e.b, path);
|
|
1416
|
+
if (!a || !b)
|
|
1417
|
+
return undefined;
|
|
1418
|
+
return { row: e.op === 'add' ? radd(rscale(b.den, a.row), rscale(a.den, b.row)) : qdif(a, b), den: a.den * b.den };
|
|
1419
|
+
}
|
|
1420
|
+
case 'mul': {
|
|
1421
|
+
const k = ratNorm(e.k), a = qlinear(e.a, path);
|
|
1422
|
+
return a && { row: rscale(k.num, a.row), den: k.den * a.den };
|
|
1423
|
+
}
|
|
1424
|
+
case 'min':
|
|
1425
|
+
case 'max': {
|
|
1426
|
+
const a = qlinear(e.a, path), b = qlinear(e.b, path);
|
|
1427
|
+
if (!a || !b)
|
|
1428
|
+
return undefined;
|
|
1429
|
+
const d = qdec(path, qdif(a, b));
|
|
1430
|
+
return d === undefined ? undefined : (d === (e.op === 'min')) ? a : b;
|
|
1431
|
+
}
|
|
1432
|
+
case 'if': {
|
|
1433
|
+
const t = qtruth(e.c, path);
|
|
1434
|
+
return t === undefined ? undefined : qlinear(t ? e.then : e.else, path);
|
|
1435
|
+
}
|
|
1436
|
+
default: return unreachable(e);
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
export function qtruth(c, path) {
|
|
1440
|
+
switch (c.op) {
|
|
1441
|
+
case '<=':
|
|
1442
|
+
case '<':
|
|
1443
|
+
case '=': {
|
|
1444
|
+
const a = qlinear(c.a, path), b = qlinear(c.b, path);
|
|
1445
|
+
if (!a || !b)
|
|
1446
|
+
return undefined;
|
|
1447
|
+
if (c.op === '<=')
|
|
1448
|
+
return qdec(path, qdif(a, b));
|
|
1449
|
+
if (c.op === '<') {
|
|
1450
|
+
const d = qdec(path, qdif(b, a));
|
|
1451
|
+
return d === undefined ? undefined : !d;
|
|
1452
|
+
}
|
|
1453
|
+
const d1 = qdec(path, qdif(a, b)), d2 = qdec(path, qdif(b, a));
|
|
1454
|
+
return d1 === false || d2 === false ? false : d1 === true && d2 === true ? true : undefined;
|
|
1455
|
+
}
|
|
1456
|
+
case 'and':
|
|
1457
|
+
case 'or': {
|
|
1458
|
+
const ta = qtruth(c.a, path), tb = qtruth(c.b, path), dominant = c.op === 'or';
|
|
1459
|
+
return ta === dominant || tb === dominant ? dominant : ta === !dominant && tb === !dominant ? !dominant : undefined;
|
|
1460
|
+
}
|
|
1461
|
+
case 'not': {
|
|
1462
|
+
const t = qtruth(c.x, path);
|
|
1463
|
+
return t === undefined ? undefined : !t;
|
|
1464
|
+
}
|
|
1465
|
+
default: return unreachable(c);
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
// The rows (each with its strictness) a hypothesis gives under the path when it has the truth value `pos`.
|
|
1469
|
+
export function qfacts(c, path, pos = true) {
|
|
1470
|
+
switch (c.op) {
|
|
1471
|
+
case 'not': return qfacts(c.x, path, !pos);
|
|
1472
|
+
case 'and':
|
|
1473
|
+
case 'or': {
|
|
1474
|
+
// and when true, or when false: both parts; otherwise the part the other one's truth leaves.
|
|
1475
|
+
if ((c.op === 'and') === pos)
|
|
1476
|
+
return [...qfacts(c.a, path, pos), ...qfacts(c.b, path, pos)];
|
|
1477
|
+
if (qtruth(c.a, path) === !pos)
|
|
1478
|
+
return qfacts(c.b, path, pos);
|
|
1479
|
+
if (qtruth(c.b, path) === !pos)
|
|
1480
|
+
return qfacts(c.a, path, pos);
|
|
1481
|
+
return [];
|
|
1482
|
+
}
|
|
1483
|
+
case '<=':
|
|
1484
|
+
case '<':
|
|
1485
|
+
case '=': {
|
|
1486
|
+
const a = qlinear(c.a, path), b = qlinear(c.b, path);
|
|
1487
|
+
if (!a || !b)
|
|
1488
|
+
return [];
|
|
1489
|
+
if (c.op === '<=')
|
|
1490
|
+
return pos ? [[qdif(a, b), false]] : [[qdif(b, a), true]];
|
|
1491
|
+
if (c.op === '<')
|
|
1492
|
+
return pos ? [[qdif(a, b), true]] : [[qdif(b, a), false]];
|
|
1493
|
+
if (pos)
|
|
1494
|
+
return [[qdif(a, b), false], [qdif(b, a), false]];
|
|
1495
|
+
if (qdec(path, qdif(a, b)) === true)
|
|
1496
|
+
return [[qdif(a, b), true]];
|
|
1497
|
+
if (qdec(path, qdif(b, a)) === true)
|
|
1498
|
+
return [[qdif(b, a), true]];
|
|
1499
|
+
return [];
|
|
1500
|
+
}
|
|
1501
|
+
default: return unreachable(c);
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
// A leaf's rows: the path's atoms (l <= 0, or −l < 0), then each hypothesis' facts; a row past 2^53 is left out.
|
|
1505
|
+
export function qLeafRows(goal, path) {
|
|
1506
|
+
const rows = [...[...path.values()].map(a => (a.holds ? [linRow(a.lin), false] : [rscale(-1n, linRow(a.lin)), true])),
|
|
1507
|
+
...goal.hyps.flatMap(h => qfacts(h, path))];
|
|
1508
|
+
return rows.flatMap(([r, strict]) => { const lin = qLin(r); return lin ? [{ lin, label: '', strict }] : []; });
|
|
1509
|
+
}
|
|
1510
|
+
// A split on a row the path decides already (or a constant one) is refused, as Lean's checkT refuses it.
|
|
1511
|
+
export function checkQTree(goal, tree, path = new Map()) {
|
|
1512
|
+
if ('leaf' in tree)
|
|
1513
|
+
return !tree.products?.length && !tree.squares?.length && checkRefutation(qLeafRows(goal, path), tree.leaf, false);
|
|
1514
|
+
const row = linRow(tree.split), lin = row && qLin(row);
|
|
1515
|
+
if (!row || !lin || qdec(path, row) !== undefined)
|
|
1516
|
+
return false;
|
|
1517
|
+
const key = canon(lin);
|
|
1518
|
+
return checkQTree(goal, tree.yes, new Map(path).set(key, { lin, holds: true })) && checkQTree(goal, tree.no, new Map(path).set(key, { lin, holds: false }));
|
|
1519
|
+
}
|
|
1520
|
+
export const qTrue = { op: '<=', a: { op: 'const', q: { num: 0n, den: 1n } }, b: { op: 'const', q: { num: 0n, den: 1n } } };
|
|
1521
|
+
export function qSubstE(e, s) {
|
|
1522
|
+
switch (e.op) {
|
|
1523
|
+
case 'var': return Object.hasOwn(s, e.name) ? s[e.name] : e;
|
|
1524
|
+
case 'const': return e;
|
|
1525
|
+
case 'mul': return { ...e, a: qSubstE(e.a, s) };
|
|
1526
|
+
case 'if': return { op: 'if', c: qSubstC(e.c, s), then: qSubstE(e.then, s), else: qSubstE(e.else, s) };
|
|
1527
|
+
case 'add':
|
|
1528
|
+
case 'sub':
|
|
1529
|
+
case 'min':
|
|
1530
|
+
case 'max': return { ...e, a: qSubstE(e.a, s), b: qSubstE(e.b, s) };
|
|
1531
|
+
default: return unreachable(e);
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1534
|
+
export function qSubstC(c, s) {
|
|
1535
|
+
switch (c.op) {
|
|
1536
|
+
case 'not': return { op: 'not', x: qSubstC(c.x, s) };
|
|
1537
|
+
case 'and':
|
|
1538
|
+
case 'or': return { op: c.op, a: qSubstC(c.a, s), b: qSubstC(c.b, s) };
|
|
1539
|
+
case '<=':
|
|
1540
|
+
case '<':
|
|
1541
|
+
case '=': return { op: c.op, a: qSubstE(c.a, s), b: qSubstE(c.b, s) };
|
|
1542
|
+
default: return unreachable(c);
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
export function qOccurs(t, names) {
|
|
1546
|
+
switch (t.op) {
|
|
1547
|
+
case 'var': return names.includes(t.name);
|
|
1548
|
+
case 'const': return false;
|
|
1549
|
+
case 'mul': return qOccurs(t.a, names);
|
|
1550
|
+
case 'not': return qOccurs(t.x, names);
|
|
1551
|
+
case 'if': return qOccurs(t.c, names) || qOccurs(t.then, names) || qOccurs(t.else, names);
|
|
1552
|
+
case 'and':
|
|
1553
|
+
case 'or': return qOccurs(t.a, names) || qOccurs(t.b, names);
|
|
1554
|
+
case 'add':
|
|
1555
|
+
case 'sub':
|
|
1556
|
+
case 'min':
|
|
1557
|
+
case 'max':
|
|
1558
|
+
case '<=':
|
|
1559
|
+
case '<':
|
|
1560
|
+
case '=': return qOccurs(t.a, names) || qOccurs(t.b, names);
|
|
1561
|
+
default: return unreachable(t);
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
// startGoal and stepGoal: the law refuted at the start, and after a step from where every law and the request's
|
|
1565
|
+
// assumption hold.
|
|
1566
|
+
export function qObligations(m, laws, law) {
|
|
1567
|
+
const pre = m.pre ?? qTrue, assume = m.assume ?? qTrue;
|
|
1568
|
+
return {
|
|
1569
|
+
start: { name: `${law.name} @start`, hyps: [pre, { op: 'not', x: qSubstC(law.holds, m.state) }] },
|
|
1570
|
+
step: { name: `${law.name} @step`, hyps: [pre, assume, ...laws.map(l => l.holds), { op: 'not', x: qSubstC(law.holds, m.step) }] },
|
|
1571
|
+
};
|
|
1572
|
+
}
|
|
1573
|
+
// checkMachine: the precondition reads no input and no state variable, no law reads an input, and each law's start
|
|
1574
|
+
// and step goals are refuted by its trees.
|
|
1575
|
+
export function checkQMachine(m, laws, trees) {
|
|
1576
|
+
const assigned = [...m.inputs, ...Object.keys(m.state), ...Object.keys(m.step)];
|
|
1577
|
+
if (m.pre && qOccurs(m.pre, assigned))
|
|
1578
|
+
return false;
|
|
1579
|
+
return laws.every(law => {
|
|
1580
|
+
const t = trees[law.name], goals = qObligations(m, laws, law);
|
|
1581
|
+
return !!t && !qOccurs(law.holds, m.inputs) && checkQTree(goals.start, t.start) && checkQTree(goals.step, t.step);
|
|
1582
|
+
});
|
|
1583
|
+
}
|
package/dist/kernel/prove.js
CHANGED
|
@@ -3,7 +3,7 @@ import { checkRefutation } from "../core/check.js";
|
|
|
3
3
|
import { allowNode, allowTranslated, certCyclic, certOn, certSearch, closingInsts, crossCyclic, cyclicOn, cycLog, hasQuantified, loopNodes, machineNodes, proveCyclic, reqTS, searchedStepNodes, skMachine, timedKind, withWitness, translatedProof, uncovered, uncoveredWhy } from "../core/cyclic.js";
|
|
4
4
|
import { proveSome, someLaws } from "../core/exists.js";
|
|
5
5
|
import { growCdcl } from "./cdcl.js";
|
|
6
|
-
import { assumed, atomOf, boundOf, canon, canonFlip, columnTerm, decided, factorsOf, isCell, isMonomial, stepZone, check, checkLaws, checkStepLaws, historyMachine, leafConstraints, leafConstraintsReusing, reusing, linear, livenessHelpful, livenessParts, loopMachine, obligations, plainKey, progressObligations, stepLawObligations, trueC, truth, unreachable, witnessesOf, withProducts } from "./kernel.js";
|
|
6
|
+
import { assumed, atomOf, boundOf, canon, canonFlip, columnTerm, decided, factorsOf, isCell, isMonomial, stepZone, check, checkLaws, checkStepLaws, historyMachine, substC, substE, leafConstraints, leafConstraintsReusing, reusing, linear, livenessHelpful, livenessParts, loopMachine, obligations, plainKey, progressObligations, stepLawObligations, trueC, truth, unreachable, witnessesOf, withProducts } from "./kernel.js";
|
|
7
7
|
// The atoms the path leaves open, in evaluation order: deciding any of them makes progress.
|
|
8
8
|
function openE(e, path, out) {
|
|
9
9
|
switch (e.op) {
|
|
@@ -977,7 +977,18 @@ function readKeys(m, laws) {
|
|
|
977
977
|
}
|
|
978
978
|
for (const l of laws.filter(l => !l.forall))
|
|
979
979
|
inC(l.holds);
|
|
980
|
-
return [...out.values()];
|
|
980
|
+
return keysInlined(m, [...out.values()]);
|
|
981
|
+
}
|
|
982
|
+
// Keys as the kernel's goals read them: a key that reads a flag (a map read at `when(flag, a, b)` in the step) with the
|
|
983
|
+
// flag's condition in its place (kernel.ts's updateOf inlines them so), since an instance is a term over the frame and
|
|
984
|
+
// names no flag. A quantified flag reads as its 0/1 variable ?f, as the goals read it.
|
|
985
|
+
function keysInlined(m, keys) {
|
|
986
|
+
if (!keys.some(e => JSON.stringify(e).includes('"op":"flag"')))
|
|
987
|
+
return keys;
|
|
988
|
+
const flags = {};
|
|
989
|
+
for (const [f, c] of Object.entries(m.step.flags ?? {}))
|
|
990
|
+
flags[f] = c.op === 'all' ? { op: '=', a: { op: 'var', name: `?${f}` }, b: { op: 'const', value: 1 } } : substC(c, { vars: {}, maps: {}, flags });
|
|
991
|
+
return [...new Map(keys.map(e => substE(e, { vars: {}, maps: {}, flags })).map(e => [JSON.stringify(e), e])).values()];
|
|
981
992
|
}
|
|
982
993
|
const shifts = (m) => Object.values(m.step.maps ?? {}).some(w => 'each' in w && !plainKey(w.value, w.each));
|
|
983
994
|
// The keys some goals read a map at.
|
|
@@ -1556,7 +1567,7 @@ function proveStepLawsAsGiven(m, laws, stepLaws, base) {
|
|
|
1556
1567
|
// stepZone), so a k-step law drops the keys that read one; but a flag that holds at every key holds at a later step's
|
|
1557
1568
|
// key too: where these instances don't prove it, each flag at its own witness at every step is tried (keptAcross).
|
|
1558
1569
|
const shared = [...readKeys(plain, laws), ...witnesses(plain)], named = [...laws, ...(plain.facts ?? [])], reads = shifts(plain) || laws.some(l => l.keys?.length) || pairFlags(plain);
|
|
1559
|
-
const own = (sl) => [...[sl.then, ...(sl.when === undefined ? [] : Array.isArray(sl.when) ? sl.when : [sl.when])].flatMap(readKeysC),
|
|
1570
|
+
const own = (sl) => [...keysInlined(plain, [sl.then, ...(sl.when === undefined ? [] : Array.isArray(sl.when) ? sl.when : [sl.when])].flatMap(readKeysC)),
|
|
1560
1571
|
...[sl.forall, ...(sl.keys ?? [])].flatMap(key => (key ? [{ op: 'var', name: key }] : []))];
|
|
1561
1572
|
const perStep = {};
|
|
1562
1573
|
for (const sl of stepLaws) {
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type Lin } from '../core/solver.js';
|
|
2
|
+
import type { Rat } from '../core/q.js';
|
|
3
|
+
import { type Path, type QGoal, type Tree } from './kernel.js';
|
|
4
|
+
export type QOutcome = {
|
|
5
|
+
tree: Tree;
|
|
6
|
+
nodes: number;
|
|
7
|
+
} | {
|
|
8
|
+
stuck: 'sat' | 'unknown';
|
|
9
|
+
witness?: Record<string, number>;
|
|
10
|
+
rational?: Record<string, Rat>;
|
|
11
|
+
path: Path;
|
|
12
|
+
why?: string;
|
|
13
|
+
};
|
|
14
|
+
export declare function openQ(goal: QGoal, path: Path): Lin[];
|
|
15
|
+
export declare function growQ(goal: QGoal, opts?: {
|
|
16
|
+
until?: number;
|
|
17
|
+
maxNodes?: number;
|
|
18
|
+
}): QOutcome;
|