gdp-wired 0.21.0 → 0.23.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 +16 -1
- package/dist/core/bmc.d.ts +38 -0
- package/dist/core/bmc.js +137 -0
- package/dist/core/check.d.ts +1 -1
- package/dist/core/check.js +19 -6
- package/dist/core/cycles.d.ts +15 -0
- package/dist/core/cycles.js +446 -8
- package/dist/core/cyclic.d.ts +6 -1
- package/dist/core/cyclic.js +13 -1
- package/dist/core/data.d.ts +17 -1
- package/dist/core/data.js +637 -97
- package/dist/core/dsl.d.ts +10 -1
- package/dist/core/dsl.js +47 -12
- package/dist/core/infer.js +518 -5
- 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 +480 -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 +77 -42
- package/dist/kernel/cdcl.d.ts +43 -0
- package/dist/kernel/cdcl.js +564 -0
- package/dist/kernel/kernel.d.ts +91 -0
- package/dist/kernel/kernel.js +310 -0
- package/dist/kernel/prove.d.ts +20 -1
- package/dist/kernel/prove.js +119 -44
- package/dist/kernel/qprove.d.ts +18 -0
- package/dist/kernel/qprove.js +157 -0
- package/dist/kernel/reader.d.ts +79 -0
- package/dist/kernel/reader.js +539 -0
- package/dist/kernel/simplex.d.ts +36 -0
- package/dist/kernel/simplex.js +259 -0
- package/dist/lean-kernel/emit.d.ts +19 -1
- package/dist/lean-kernel/emit.js +71 -3
- package/dist/lean-kernel/library.js +2 -0
- package/dist/lean-kernel/qlean.d.ts +26 -0
- package/dist/lean-kernel/qlean.js +105 -0
- package/package.json +6 -2
package/dist/core/solver.js
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
// Linear arithmetic over the integers, inside the library. A conjunction of constraints
|
|
2
|
-
// Fourier–Motzkin elimination with integer cuts, and every answer
|
|
1
|
+
// Linear arithmetic over the integers and over the rationals, inside the library. A conjunction of constraints
|
|
2
|
+
// `lin <= 0` (or `lin < 0`, strict) is decided by Fourier–Motzkin elimination with integer cuts, and every answer
|
|
3
|
+
// carries evidence that a small checker verifies:
|
|
3
4
|
// refuted a derivation: input rows, positive combinations of two rows, and cuts (divide a row's variable
|
|
4
5
|
// coefficients by their common divisor g and round the constant up), ending in `positive <= 0`
|
|
5
6
|
// sat an integer witness, checked by evaluating every input constraint at it
|
|
6
7
|
// unknown elimination was inexact for the integers and no witness was found (never a wrong answer)
|
|
7
8
|
// Elimination of x is exact for the integers when every pair it combines has a coefficient ±1 on x (the Omega test's
|
|
8
9
|
// exact shadow). Then a rational solution of the rest extends to an integer x, so back-substitution finds a witness.
|
|
10
|
+
// Over the rationals (`integral` false) the same elimination is exact with no cut at all (a combination of a strict
|
|
11
|
+
// row is strict), so the answer is refuted or sat, the witness exact (`rational`), the derivation one check.ts takes
|
|
12
|
+
// with no cut: the integers are this with the cut rule, and a strict row over them is lin + 1 <= 0.
|
|
13
|
+
import { Q0, Q1, q, qadd, qcmp, qdiv, qneg, qnum, qrow, qsub } from "./q.js";
|
|
9
14
|
export const v = (name) => ({ [name]: 1 });
|
|
10
15
|
export const k = (value) => (value === 0 ? {} : { 1: value });
|
|
11
16
|
// (Plain loops, no intermediate arrays: these run millions of times in a proof. Each keeps the keys in the order they
|
|
@@ -58,13 +63,15 @@ const vars = (lin) => Object.keys(lin).filter(key => key !== '1');
|
|
|
58
63
|
// five times as many before merging them, it gives up with `unknown`.
|
|
59
64
|
// The rows the solver has read and made, which a counted search reads as its clock (kernel/prove.ts's `counted`).
|
|
60
65
|
export const work = { n: 0 };
|
|
61
|
-
export function solve(constraints, maxRows = 20_000) {
|
|
66
|
+
export function solve(constraints, maxRows = 20_000, integral = true) {
|
|
62
67
|
work.n += constraints.length;
|
|
63
68
|
const steps = [];
|
|
64
|
-
const lins = [];
|
|
65
|
-
const push = (step, lin) => (steps.push(step), lins.push(lin), lins.length - 1);
|
|
66
|
-
// A row after its cut, if its variables share a divisor.
|
|
69
|
+
const lins = [], stricts = [];
|
|
70
|
+
const push = (step, lin, strict = false) => (steps.push(step), lins.push(lin), stricts.push(strict), lins.length - 1);
|
|
71
|
+
// A row after its cut, if its variables share a divisor (over the integers only).
|
|
67
72
|
const tighten = (index) => {
|
|
73
|
+
if (!integral)
|
|
74
|
+
return index;
|
|
68
75
|
const lin = lins[index];
|
|
69
76
|
let g = 0;
|
|
70
77
|
for (const key in lin)
|
|
@@ -82,10 +89,15 @@ export function solve(constraints, maxRows = 20_000) {
|
|
|
82
89
|
const constant = Math.ceil((lin['1'] ?? 0) / g);
|
|
83
90
|
return push({ kind: 'cut', of: index, g }, add(cut, k(constant)));
|
|
84
91
|
};
|
|
85
|
-
let rows = constraints.map((c, index) => tighten(push({ kind: 'input', index }, c.lin)));
|
|
86
|
-
// A row with no variable and a positive constant (kept per row: a row never
|
|
92
|
+
let rows = constraints.map((c, index) => tighten(push({ kind: 'input', index }, c.lin, !integral && c.strict === true)));
|
|
93
|
+
// A row with no variable and a positive constant, or a strict one with a constant >= 0 (kept per row: a row never
|
|
94
|
+
// changes once pushed).
|
|
87
95
|
const contra = [];
|
|
88
|
-
const isContra = (r) => (contra[r] ??= (lins[r]['1'] ?? 0) > 0 && Object.keys(lins[r]).every(key => key === '1'));
|
|
96
|
+
const isContra = (r) => (contra[r] ??= ((lins[r]['1'] ?? 0) > 0 || (stricts[r] && (lins[r]['1'] ?? 0) >= 0)) && Object.keys(lins[r]).every(key => key === '1'));
|
|
97
|
+
// Over the rationals every coefficient must stay a safe integer (none is rounded); past that, unknown.
|
|
98
|
+
let inexact = false;
|
|
99
|
+
const pushExact = (step, lin, strict) => { if (!integral && !Object.values(lin).every(c => Number.isSafeInteger(c)))
|
|
100
|
+
inexact = true; return push(step, lin, strict); };
|
|
89
101
|
const contradiction = () => rows.find(isContra);
|
|
90
102
|
const eliminated = [];
|
|
91
103
|
let exact = true;
|
|
@@ -101,7 +113,7 @@ export function solve(constraints, maxRows = 20_000) {
|
|
|
101
113
|
const byKey = new Map(rows.map(r => [key(r), r]));
|
|
102
114
|
for (const r of rows) {
|
|
103
115
|
const neg = byKey.get(negKey(r));
|
|
104
|
-
if (neg === undefined || neg === r)
|
|
116
|
+
if (neg === undefined || neg === r || stricts[r] || stricts[neg])
|
|
105
117
|
continue;
|
|
106
118
|
let x;
|
|
107
119
|
for (const name in lins[r])
|
|
@@ -121,7 +133,7 @@ export function solve(constraints, maxRows = 20_000) {
|
|
|
121
133
|
if (c === 0)
|
|
122
134
|
next.push(o);
|
|
123
135
|
else
|
|
124
|
-
next.push(tighten(
|
|
136
|
+
next.push(tighten(pushExact({ kind: 'combine', a: o, b: c > 0 ? down : up, ma: 1, mb: Math.abs(c) }, add(lins[o], scale(lins[c > 0 ? down : up], Math.abs(c))), stricts[o])));
|
|
125
137
|
}
|
|
126
138
|
eliminated.push({ x, rows: [up, down] });
|
|
127
139
|
rows = next;
|
|
@@ -183,27 +195,29 @@ export function solve(constraints, maxRows = 20_000) {
|
|
|
183
195
|
for (const p of choice.pos)
|
|
184
196
|
for (const n of choice.neg) {
|
|
185
197
|
const a = lins[p][choice.x], b = -lins[n][choice.x];
|
|
186
|
-
const combined =
|
|
198
|
+
const combined = pushExact({ kind: 'combine', a: p, b: n, ma: b / gcd(a, b), mb: a / gcd(a, b) }, add(scale(lins[p], b / gcd(a, b)), scale(lins[n], a / gcd(a, b))), stricts[p] || stricts[n]);
|
|
187
199
|
next.push(tighten(combined));
|
|
188
200
|
}
|
|
189
|
-
// Rows with the same variable part: only the tightest (largest constant
|
|
190
|
-
// say nothing.
|
|
201
|
+
// Rows with the same variable part: only the tightest (largest constant, strict before not at the same) matters;
|
|
202
|
+
// rows with no variables that hold say nothing.
|
|
191
203
|
const best = new Map();
|
|
192
204
|
for (const r of next) {
|
|
193
205
|
const lin = lins[r], sig = sigOf(r);
|
|
194
|
-
if (sig === '[]' && (
|
|
206
|
+
if (sig === '[]' && !isContra(r))
|
|
195
207
|
continue;
|
|
196
208
|
const seen = best.get(sig);
|
|
197
|
-
if (seen === undefined || (lins[seen]['1'] ?? 0) < (lin['1'] ?? 0))
|
|
209
|
+
if (seen === undefined || (lins[seen]['1'] ?? 0) < (lin['1'] ?? 0) || (stricts[r] && !stricts[seen] && (lins[seen]['1'] ?? 0) === (lin['1'] ?? 0)))
|
|
198
210
|
best.set(sig, r);
|
|
199
211
|
}
|
|
200
212
|
rows = [...best.values()];
|
|
201
|
-
if (rows.length > maxRows)
|
|
213
|
+
if (rows.length > maxRows || inexact)
|
|
202
214
|
return { kind: 'unknown' };
|
|
203
215
|
}
|
|
204
216
|
const found = contradiction();
|
|
205
217
|
if (found !== undefined)
|
|
206
|
-
return { kind: 'refuted', steps: prune(steps, found) };
|
|
218
|
+
return inexact ? { kind: 'unknown' } : { kind: 'refuted', steps: prune(steps, found) };
|
|
219
|
+
if (!integral)
|
|
220
|
+
return inexact ? { kind: 'unknown' } : satOverQ(constraints, eliminated, lins, stricts);
|
|
207
221
|
// A rational solution the same way (cuts only remove non-integer points, so bounds never cross): where to split.
|
|
208
222
|
const rational = {};
|
|
209
223
|
for (const { x, rows: bounds } of [...eliminated].reverse()) {
|
|
@@ -240,6 +254,35 @@ export function solve(constraints, maxRows = 20_000) {
|
|
|
240
254
|
void exact;
|
|
241
255
|
return holds ? { kind: 'sat', witness: point } : { kind: 'unknown', point: rational };
|
|
242
256
|
}
|
|
257
|
+
// Over the rationals elimination is exact: back-substitution, last eliminated first, gives each variable an exact value
|
|
258
|
+
// within its bounds (strict ones kept strict): 0 where it may be, else the integer nearest its bounds that fits, else
|
|
259
|
+
// the midpoint. The witness is checked on every input exactly; a miss (none should happen) is unknown, never sat.
|
|
260
|
+
function satOverQ(constraints, eliminated, lins, stricts) {
|
|
261
|
+
const at = {};
|
|
262
|
+
for (const { x, rows: bounds } of [...eliminated].reverse()) {
|
|
263
|
+
let lo, hi;
|
|
264
|
+
for (const r of bounds) {
|
|
265
|
+
const lin = lins[r], a = lin[x];
|
|
266
|
+
// a·x + rest (<|<=) 0: x (<|<=) −rest/a when a > 0, x (>|>=) −rest/a when a < 0.
|
|
267
|
+
const rest = qrow(Object.fromEntries(Object.entries(lin).filter(([key]) => key !== x)), at), b = qdiv(qneg(rest), q(a)), strict = stricts[r];
|
|
268
|
+
if (a > 0) {
|
|
269
|
+
if (!hi || qcmp(b, hi.v) < 0 || (qcmp(b, hi.v) === 0 && strict))
|
|
270
|
+
hi = { v: b, strict };
|
|
271
|
+
}
|
|
272
|
+
else if (!lo || qcmp(b, lo.v) > 0 || (qcmp(b, lo.v) === 0 && strict))
|
|
273
|
+
lo = { v: b, strict };
|
|
274
|
+
}
|
|
275
|
+
const fits = (v) => (!lo || qcmp(v, lo.v) > 0 || (!lo.strict && qcmp(v, lo.v) === 0)) && (!hi || qcmp(v, hi.v) < 0 || (!hi.strict && qcmp(v, hi.v) === 0));
|
|
276
|
+
const floor = (v) => q(v.num >= 0n ? v.num / v.den : -((-v.num + v.den - 1n) / v.den));
|
|
277
|
+
const tries = [Q0, ...(lo ? [floor(lo.v), qadd(floor(lo.v), Q1)] : []), ...(hi ? [floor(hi.v), qsub(floor(hi.v), Q1)] : [])];
|
|
278
|
+
at[x] = tries.find(fits) ?? (lo && hi ? qdiv(qadd(lo.v, hi.v), q(2)) : lo ? qadd(lo.v, Q1) : qsub(hi.v, Q1));
|
|
279
|
+
}
|
|
280
|
+
for (const c of constraints)
|
|
281
|
+
for (const key of vars(c.lin))
|
|
282
|
+
at[key] ??= Q0;
|
|
283
|
+
const holds = constraints.every(c => { const s = qcmp(qrow(c.lin, at), Q0); return c.strict ? s < 0 : s <= 0; });
|
|
284
|
+
return holds ? { kind: 'sat', witness: Object.fromEntries(Object.entries(at).map(([key, v]) => [key, qnum(v)])), rational: at } : { kind: 'unknown' };
|
|
285
|
+
}
|
|
243
286
|
// Only the steps the contradiction depends on, renumbered.
|
|
244
287
|
function prune(steps, last) {
|
|
245
288
|
const keep = new Set();
|
|
@@ -267,7 +310,9 @@ function prune(steps, last) {
|
|
|
267
310
|
// derivation, renumbered to the whole system's rows. Floating point only picks the rows: the derivation is exact, and
|
|
268
311
|
// the checker sees nothing else. Undefined when no λ turns up (a system with a rational solution, or one the floats
|
|
269
312
|
// misjudge): the caller goes on as without it.
|
|
270
|
-
|
|
313
|
+
// (Over the rationals, `integral` false, the elimination on the support has no cut; a refutation that needs a strict
|
|
314
|
+
// row's strictness, Σ λ·c = 0, isn't looked for here: the caller's own elimination finds it.)
|
|
315
|
+
export function refuteFast(constraints, maxRows = 2_000, integral = true) {
|
|
271
316
|
const m = constraints.length;
|
|
272
317
|
if (m === 0)
|
|
273
318
|
return undefined;
|
|
@@ -348,7 +393,7 @@ export function refuteFast(constraints, maxRows = 2_000) {
|
|
|
348
393
|
support.push(basis[r]);
|
|
349
394
|
if (!support.length)
|
|
350
395
|
return undefined;
|
|
351
|
-
const sub = solve(support.sort((a, b) => a - b).map(i => constraints[i]));
|
|
396
|
+
const sub = solve(support.sort((a, b) => a - b).map(i => constraints[i]), 20_000, integral);
|
|
352
397
|
if (sub.kind !== 'refuted')
|
|
353
398
|
return undefined;
|
|
354
399
|
return { kind: 'refuted', steps: sub.steps.map(st => (st.kind === 'input' ? { ...st, index: support[st.index] } : st)) };
|
package/dist/core/transfer.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { type DataLaw, type DataVerdict, type Options, type X } from './data.js';
|
|
2
2
|
type Op = {
|
|
3
3
|
name: string;
|
|
4
|
-
args:
|
|
4
|
+
args: number;
|
|
5
5
|
ret: 'nat' | 'bool';
|
|
6
|
-
x: (
|
|
7
|
-
at: (
|
|
6
|
+
x: (as: X[]) => X;
|
|
7
|
+
at: (ns: number[]) => number;
|
|
8
8
|
};
|
|
9
9
|
export type Plan = {
|
|
10
10
|
bridges: Map<string, Op>;
|
package/dist/core/transfer.js
CHANGED
|
@@ -1,48 +1,80 @@
|
|
|
1
|
-
import { Bool, Integer, Nat, Val, argsOf, bindName, define, encX, encY, evalX, factsOf, fnNamed, programOf, schemesOf, searchIn } from "./data.js";
|
|
1
|
+
import { Bool, Integer, Nat, Val, argsOf, bindName, define, encX, encY, evalX, factsOf, fnNamed, programOf, rankWidth, schemesOf, searchIn } from "./data.js";
|
|
2
2
|
import { proveCyclic } from "./cyclic.js";
|
|
3
3
|
import { clock, pairedKeys, withDeadline } from "../kernel/prove.js";
|
|
4
4
|
import { substE } from "../kernel/kernel.js";
|
|
5
5
|
const nat = (value) => ({ op: 'nat', value });
|
|
6
6
|
const cond = (c) => ({ op: 'bool', c });
|
|
7
7
|
const OPS = [
|
|
8
|
-
{ name: '+', args: 2, ret: 'nat', x: (a, b) => ({ op: 'add', a, b }), at: (a, b) => a + b },
|
|
9
|
-
{ name: '∸', args: 2, ret: 'nat', x: (a, b) => ({ op: 'sub', a, b }), at: (a, b) => Math.max(0, a - b) },
|
|
10
|
-
{ name: 'min', args: 2, ret: 'nat', x: (a, b) => ({ op: 'min', a, b }), at: Math.min },
|
|
11
|
-
{ name: 'max', args: 2, ret: 'nat', x: (a, b) => ({ op: 'max', a, b }), at: Math.max },
|
|
12
|
-
{ name: '≤', args: 2, ret: 'bool', x: (a, b) => cond({ op: '<=', a, b }), at: (a, b) => +(a <= b) },
|
|
13
|
-
{ name: '<', args: 2, ret: 'bool', x: (a, b) => cond({ op: '<', a, b }), at: (a, b) => +(a < b) },
|
|
14
|
-
{ name: '=', args: 2, ret: 'bool', x: (a, b) => cond({ op: '=', a, b }), at: (a, b) => +(a === b) },
|
|
15
|
-
{ name: '≥', args: 2, ret: 'bool', x: (a, b) => cond({ op: '<=', a: b, b: a }), at: (a, b) => +(a >= b) },
|
|
16
|
-
{ name: '>', args: 2, ret: 'bool', x: (a, b) => cond({ op: '<', a: b, b: a }), at: (a, b) => +(a > b) },
|
|
17
|
-
{ name: '≠', args: 2, ret: 'bool', x: (a, b) => cond({ op: 'not', x: { op: '=', a, b } }), at: (a, b) => +(a !== b) },
|
|
18
|
-
{ name: 'x + 1', args: 1, ret: 'nat', x: a => ({ op: 'add', a, b: nat(1) }), at: a => a + 1 },
|
|
19
|
-
{ name: 'x ∸ 1', args: 1, ret: 'nat', x: a => ({ op: 'sub', a, b: nat(1) }), at: a => Math.max(0, a - 1) },
|
|
20
|
-
{ name: '2·x', args: 1, ret: 'nat', x: a => ({ op: 'mul', k: 2, a }), at: a => 2 * a },
|
|
21
|
-
{ name: 'x = 0', args: 1, ret: 'bool', x: a => cond({ op: '=', a, b: nat(0) }), at: a => +(a === 0) },
|
|
8
|
+
{ name: '+', args: 2, ret: 'nat', x: ([a, b]) => ({ op: 'add', a: a, b: b }), at: ([a, b]) => a + b },
|
|
9
|
+
{ name: '∸', args: 2, ret: 'nat', x: ([a, b]) => ({ op: 'sub', a: a, b: b }), at: ([a, b]) => Math.max(0, a - b) },
|
|
10
|
+
{ name: 'min', args: 2, ret: 'nat', x: ([a, b]) => ({ op: 'min', a: a, b: b }), at: ([a, b]) => Math.min(a, b) },
|
|
11
|
+
{ name: 'max', args: 2, ret: 'nat', x: ([a, b]) => ({ op: 'max', a: a, b: b }), at: ([a, b]) => Math.max(a, b) },
|
|
12
|
+
{ name: '≤', args: 2, ret: 'bool', x: ([a, b]) => cond({ op: '<=', a: a, b: b }), at: ([a, b]) => +(a <= b) },
|
|
13
|
+
{ name: '<', args: 2, ret: 'bool', x: ([a, b]) => cond({ op: '<', a: a, b: b }), at: ([a, b]) => +(a < b) },
|
|
14
|
+
{ name: '=', args: 2, ret: 'bool', x: ([a, b]) => cond({ op: '=', a: a, b: b }), at: ([a, b]) => +(a === b) },
|
|
15
|
+
{ name: '≥', args: 2, ret: 'bool', x: ([a, b]) => cond({ op: '<=', a: b, b: a }), at: ([a, b]) => +(a >= b) },
|
|
16
|
+
{ name: '>', args: 2, ret: 'bool', x: ([a, b]) => cond({ op: '<', a: b, b: a }), at: ([a, b]) => +(a > b) },
|
|
17
|
+
{ name: '≠', args: 2, ret: 'bool', x: ([a, b]) => cond({ op: 'not', x: { op: '=', a: a, b: b } }), at: ([a, b]) => +(a !== b) },
|
|
18
|
+
{ name: 'x + 1', args: 1, ret: 'nat', x: ([a]) => ({ op: 'add', a: a, b: nat(1) }), at: ([a]) => a + 1 },
|
|
19
|
+
{ name: 'x ∸ 1', args: 1, ret: 'nat', x: ([a]) => ({ op: 'sub', a: a, b: nat(1) }), at: ([a]) => Math.max(0, a - 1) },
|
|
20
|
+
{ name: '2·x', args: 1, ret: 'nat', x: ([a]) => ({ op: 'mul', k: 2, a: a }), at: ([a]) => 2 * a },
|
|
21
|
+
{ name: 'x = 0', args: 1, ret: 'bool', x: ([a]) => cond({ op: '=', a: a, b: nat(0) }), at: ([a]) => +(a === 0) },
|
|
22
22
|
];
|
|
23
|
+
// A sum Σ cᵢ·xᵢ + c₀ of naturals (cᵢ, c₀ ≥ 0): the built-in of a function of three or four naturals (add3, add3acc), or
|
|
24
|
+
// of one or two that none of OPS is.
|
|
25
|
+
const linear = (cs, c0) => {
|
|
26
|
+
const names = ['x', 'y', 'z', 'w'], name = [...cs.flatMap((c, i) => (c ? [`${c === 1 ? '' : `${c}·`}${names[i]}`] : [])), ...(c0 || cs.every(c => !c) ? [String(c0)] : [])].join(' + ');
|
|
27
|
+
const term = (a, c) => (c === 1 ? a : { op: 'mul', k: c, a });
|
|
28
|
+
return { name, args: cs.length, ret: 'nat', at: ns => cs.reduce((t, c, i) => t + c * ns[i], c0),
|
|
29
|
+
x: as => { const parts = [...cs.flatMap((c, i) => (c ? [term(as[i], c)] : [])), ...(c0 || cs.every(c => !c) ? [nat(c0)] : [])]; return parts.slice(1).reduce((t, x) => ({ op: 'add', a: t, b: x }), parts[0]); } };
|
|
30
|
+
};
|
|
23
31
|
const fnOf = (name) => { const f = fnNamed(name); if (!f)
|
|
24
32
|
throw new Error(`no function ${name}`); return f; };
|
|
25
33
|
const recursive = (f) => f.decreases !== undefined;
|
|
26
|
-
//
|
|
34
|
+
// Recursive at all (structural, or well-founded): never unfolded at a call.
|
|
35
|
+
const taken = (f) => recursive(f) || f.measure !== undefined;
|
|
36
|
+
// The built-in a recursive function of naturals (structural, or well-founded: its termination proven) computes at every
|
|
37
|
+
// argument up to 6 (one argument: up to 12; three: up to 5; four: up to 3), if one.
|
|
27
38
|
function builtinOf(f) {
|
|
28
|
-
if (!
|
|
39
|
+
if (!taken(f) || f.paramFns.length || !(f.ret === 'nat' || f.ret === 'bool') || !f.params.every(q => q.sort === 'nat'))
|
|
29
40
|
return undefined;
|
|
30
41
|
const n = f.params.length;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
if (n < 1 || n > 4)
|
|
43
|
+
return undefined;
|
|
44
|
+
const top = [12, 6, 5, 3][n - 1], points = [[]];
|
|
45
|
+
for (let i = 0; i < n; i++)
|
|
46
|
+
points.splice(0, points.length, ...points.flatMap(p => Array.from({ length: top + 1 }, (_, a) => [...p, a])));
|
|
47
|
+
const values = new Map();
|
|
48
|
+
const at = (ns) => {
|
|
49
|
+
const key = ns.join(',');
|
|
50
|
+
if (!values.has(key)) {
|
|
51
|
+
try {
|
|
52
|
+
values.set(key, evalX({ op: 'call', fn: f.name, args: ns.map(nat) }, new Map(), {}));
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
43
57
|
}
|
|
44
|
-
return
|
|
45
|
-
}
|
|
58
|
+
return values.get(key);
|
|
59
|
+
};
|
|
60
|
+
const agrees = (op) => points.every(ns => at(ns) === op.at(ns));
|
|
61
|
+
const named = OPS.find(op => op.args === n && op.ret === f.ret && agrees(op));
|
|
62
|
+
if (named || f.ret !== 'nat')
|
|
63
|
+
return named;
|
|
64
|
+
// (its value at zero and at each unit point, then checked at every point)
|
|
65
|
+
const c0 = at(Array(n).fill(0)), cs = Array.from({ length: n }, (_, i) => { const e = Array(n).fill(0); e[i] = 1; const v = at(e); return v === undefined || c0 === undefined ? -1 : v - c0; });
|
|
66
|
+
if (c0 === undefined || cs.some(c => c < 0))
|
|
67
|
+
return undefined;
|
|
68
|
+
const op = linear(cs, c0);
|
|
69
|
+
return agrees(op) ? op : undefined;
|
|
70
|
+
}
|
|
71
|
+
// Whether `y` is f's bridge law: f at distinct variables equal (either side) to the built-in at them.
|
|
72
|
+
function isBridge(y, f, op) {
|
|
73
|
+
if (y.op !== '=')
|
|
74
|
+
return false;
|
|
75
|
+
const sides = [[y.a, y.b], [y.b, y.a]];
|
|
76
|
+
return sides.some(([c, x]) => c.op === 'call' && c.fn === f.name && c.args.every(a => a.op === 'var') && new Set(c.args.map(a => JSON.stringify(a))).size === c.args.length
|
|
77
|
+
&& JSON.stringify(x) === JSON.stringify(op.x(c.args)));
|
|
46
78
|
}
|
|
47
79
|
const callsOf = (t) => { const out = []; walkO(t, o => { if (o.op === 'call')
|
|
48
80
|
out.push(o.fn); }); return out; };
|
|
@@ -61,10 +93,10 @@ export function transferPlan(l) {
|
|
|
61
93
|
const p = programOf([l]), bridges = new Map();
|
|
62
94
|
for (const f of p.fns) {
|
|
63
95
|
const op = builtinOf(f);
|
|
64
|
-
if (op)
|
|
96
|
+
if (op && !isBridge(l.holds, f, op))
|
|
65
97
|
bridges.set(f.name, op);
|
|
66
98
|
}
|
|
67
|
-
const unfolds = p.fns.some(f => !
|
|
99
|
+
const unfolds = p.fns.some(f => !taken(f));
|
|
68
100
|
return bridges.size || unfolds ? { bridges, unfolds } : undefined;
|
|
69
101
|
}
|
|
70
102
|
// --- the law said another way ------------------------------------------------------------------------------------------
|
|
@@ -128,8 +160,8 @@ function rewriter(bridges, copies, unfold, ren = new Map()) {
|
|
|
128
160
|
case 'call': {
|
|
129
161
|
const op = bridges.get(x.fn);
|
|
130
162
|
if (op)
|
|
131
|
-
return op.x(
|
|
132
|
-
if (unfold && !
|
|
163
|
+
return op.x(x.args.map(rx));
|
|
164
|
+
if (unfold && !taken(fnOf(x.fn))) {
|
|
133
165
|
if (++unfolded > 400)
|
|
134
166
|
throw new RangeError('unfolded past 400 calls');
|
|
135
167
|
return rx(instantiate(fnOf(x.fn), x.args));
|
|
@@ -167,7 +199,7 @@ const freshName = (base) => { let n = base; for (let i = 2; fnNamed(n); i++)
|
|
|
167
199
|
function build(l, bridged, unfold) {
|
|
168
200
|
// Copied: a function that calls a bridged one, or (unfolding) one that isn't recursive, or a copied one; a function
|
|
169
201
|
// that isn't recursive is unfolded where it is called instead.
|
|
170
|
-
const p0 = programOf([l]), reach = new Set(), unfolded = (n) => unfold && !
|
|
202
|
+
const p0 = programOf([l]), reach = new Set(), unfolded = (n) => unfold && !taken(fnOf(n));
|
|
171
203
|
for (let changed = true; changed;) {
|
|
172
204
|
changed = false;
|
|
173
205
|
for (const f of p0.fns)
|
|
@@ -228,7 +260,7 @@ function build(l, bridged, unfold) {
|
|
|
228
260
|
const p1 = programOf([lstar]), back = new Map([...copies].map(([a, b]) => [b, a]));
|
|
229
261
|
const bridges = [...bridged].map(([f, op]) => {
|
|
230
262
|
const fn = fnOf(f), vs = fn.params.map(q => q.name);
|
|
231
|
-
return { fn, op, law: { name: `${f} is ${op.name}`, vars: fn.params.map(q => ({ name: q.name, sort: q.sort })), holds: { op: '=', a: { op: 'call', fn: f, args: vs.map(v) }, b: op.x(
|
|
263
|
+
return { fn, op, law: { name: `${f} is ${op.name}`, vars: fn.params.map(q => ({ name: q.name, sort: q.sort })), holds: { op: '=', a: { op: 'call', fn: f, args: vs.map(v) }, b: op.x(vs.map(v)) }, params: [] } };
|
|
232
264
|
});
|
|
233
265
|
const equivs = p1.fns.filter(f => back.has(f.name)).map(c => {
|
|
234
266
|
const fn = fnOf(back.get(c.name)), vs = fn.params.map(q => q.name), params = [...new Set(fn.paramFns)];
|
|
@@ -324,7 +356,7 @@ export function proveThrough(l, plan, opts, until) {
|
|
|
324
356
|
const v = verify(fs, specs, Math.max(until, clock() + 3000));
|
|
325
357
|
if (!v.proven || !v.checked)
|
|
326
358
|
return undefined;
|
|
327
|
-
const through = [...b.bridges.map(x => `${x.fn.name} is ${x.op.name}`), ...b.equivs.map(x => x.law.name), ...(b.unfolded ? [`unfolded: ${programOf([l]).fns.filter(f => !
|
|
359
|
+
const through = [...b.bridges.map(x => `${x.fn.name} is ${x.op.name}`), ...b.equivs.map(x => x.law.name), ...(b.unfolded ? [`unfolded: ${programOf([l]).fns.filter(f => !taken(f)).map(f => f.name).join(', ')}`] : [])];
|
|
328
360
|
return { law: l.name, proven: true, checked: true, proof: v, lemmas: [...b.bridges.map(x => x.law.name), ...b.equivs.map(x => x.law.name), ...r.verdict.lemmas], scheme: `${r.verdict.scheme ?? 'no induction'}, through the law ${b.lstar.name}`, tried, program: b.p, node: specs.length - 1, ms: Math.round(performance.now() - t0), through };
|
|
329
361
|
}
|
|
330
362
|
}
|
|
@@ -359,9 +391,9 @@ function walker(b, fs, nodeOf, own) {
|
|
|
359
391
|
if (br) {
|
|
360
392
|
if (!use(br.law.name, Object.fromEntries(f.params.map((q, i) => [q.name, as[i]]))))
|
|
361
393
|
return undefined;
|
|
362
|
-
return substE(encX(p, br.op.x(
|
|
394
|
+
return substE(encX(p, br.op.x(as.map((_, i) => ({ op: 'var', name: `a$${i}` }))), new Map()), { vars: Object.fromEntries(as.map((a, i) => [`a$${i}`, a])), maps: {}, flags: {} });
|
|
363
395
|
}
|
|
364
|
-
if (!
|
|
396
|
+
if (!taken(f) && b.unfolded)
|
|
365
397
|
return inst(`fn ${f.name}`, as) ? factBody(f, as) : undefined;
|
|
366
398
|
const copy = b.copies.get(f.name);
|
|
367
399
|
if (!copy)
|
|
@@ -439,12 +471,15 @@ function walker(b, fs, nodeOf, own) {
|
|
|
439
471
|
};
|
|
440
472
|
return { walkC, walkE, insts, uses, inst };
|
|
441
473
|
}
|
|
474
|
+
// A node's rank is (level, the measure's place, 0 or 1), the measure's place as wide as the program's widest measure
|
|
475
|
+
// (core/data.ts's attempt): the zeros after the `n` components given, so every node's rank has one length.
|
|
476
|
+
const pad = (p, n) => Array.from({ length: rankWidth(p) - n }, () => k(0));
|
|
442
477
|
// The law's node: its conclusion, using the law said through the bridges (at its own variables) and the walk's reasons.
|
|
443
478
|
function lawNode(l, b, fs, nodeOf, lnode, level) {
|
|
444
479
|
const w = walker(b, fs, nodeOf), concl = encY(b.p, l.holds, new Map());
|
|
445
480
|
if (!w.walkC(concl, encY(b.p, b.lstar.holds, new Map())))
|
|
446
481
|
return undefined;
|
|
447
|
-
return { name: `${l.name} (through ${b.lstar.name})`, hyps: [], concl, rank: [k(level),
|
|
482
|
+
return { name: `${l.name} (through ${b.lstar.name})`, hyps: [], concl, rank: [k(level), ...pad(b.p, 0), k(0)], insts: w.insts, uses: [{ node: lnode, subst: {}, guard: yes }, ...w.uses] };
|
|
448
483
|
}
|
|
449
484
|
// A copy's law, f(xs) = f'(xs), as one node at index `self`: both definitions' facts at xs, their bodies walked together
|
|
450
485
|
// (f's own recursive calls its copy's by the node itself, at the uses of f's recursion scheme), the shape of the argument
|
|
@@ -463,6 +498,6 @@ function copyNode(x, b, fs, nodeOf, self, level) {
|
|
|
463
498
|
const on = scheme?.on, size = !on ? k(0) : on.d === 'nat' ? { op: 'var', name: on.v } : { op: 'get', map: `${on.d.name}.size`, key: { op: 'var', name: on.v } };
|
|
464
499
|
if (on && on.d !== 'nat')
|
|
465
500
|
w.inst(`shape ${on.d.name}`, [{ op: 'var', name: on.v }]);
|
|
466
|
-
return { name: `${x.law.name} (its copy, made)`, hyps: [], concl: encY(b.p, x.law.holds, new Map()), rank: [k(level), size, k(0)], insts: w.insts,
|
|
501
|
+
return { name: `${x.law.name} (its copy, made)`, hyps: [], concl: encY(b.p, x.law.holds, new Map()), rank: [k(level), size, ...pad(b.p, 1), k(0)], insts: w.insts,
|
|
467
502
|
uses: [...(scheme?.uses ?? []).map(u => ({ node: self, subst: u.subst, guard: u.guard })), ...w.uses] };
|
|
468
503
|
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { type Goal, type Path } from './kernel.js';
|
|
2
|
+
import { type Outcome } from './prove.js';
|
|
3
|
+
export declare const cdclStats: {
|
|
4
|
+
on: boolean;
|
|
5
|
+
runs: number;
|
|
6
|
+
proven: number;
|
|
7
|
+
conflicts: number;
|
|
8
|
+
decisions: number;
|
|
9
|
+
solveMs: number;
|
|
10
|
+
rowsMs: number;
|
|
11
|
+
coreMs: number;
|
|
12
|
+
unfoldMs: number;
|
|
13
|
+
leaves: number;
|
|
14
|
+
resolved: number;
|
|
15
|
+
coreSize: number;
|
|
16
|
+
pathSize: number;
|
|
17
|
+
lpMisses: number;
|
|
18
|
+
resolved2: number;
|
|
19
|
+
unfoldWhy: string;
|
|
20
|
+
orderMs: number;
|
|
21
|
+
boundMs: number;
|
|
22
|
+
openMisses: number;
|
|
23
|
+
lpMs: number;
|
|
24
|
+
lpUnknown: number;
|
|
25
|
+
fmCalls: number;
|
|
26
|
+
pivots: number;
|
|
27
|
+
lpRebuilds: number;
|
|
28
|
+
lpRows: number;
|
|
29
|
+
lpNnz: number;
|
|
30
|
+
lpVars: number;
|
|
31
|
+
lpLoad: number;
|
|
32
|
+
nodes: number;
|
|
33
|
+
boundRounds: number;
|
|
34
|
+
};
|
|
35
|
+
export declare const REFUTED = "refuted, not unfolded";
|
|
36
|
+
export declare function growCdcl(goal: Goal, root: Path, budget: {
|
|
37
|
+
nodes: number;
|
|
38
|
+
until?: number;
|
|
39
|
+
}, opts?: {
|
|
40
|
+
maxLeaves?: number;
|
|
41
|
+
model?: boolean;
|
|
42
|
+
wall?: number;
|
|
43
|
+
}): Outcome;
|