gdp-wired 0.25.0 → 0.26.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 +14 -9
- package/dist/core/accel.d.ts +1 -1
- package/dist/core/accel.js +336 -101
- package/dist/core/bmc.d.ts +8 -0
- package/dist/core/bmc.js +263 -97
- package/dist/core/cycles.d.ts +2 -3
- package/dist/core/cycles.js +35 -131
- package/dist/core/cyclic.d.ts +4 -1
- package/dist/core/cyclic.js +86 -64
- package/dist/core/data.d.ts +1 -1
- package/dist/core/data.js +89 -118
- package/dist/core/discover.js +2 -3
- package/dist/core/dsl.d.ts +3 -2
- package/dist/core/dsl.js +21 -18
- package/dist/core/exists.js +7 -14
- package/dist/core/extract-check.d.ts +3 -2
- package/dist/core/extract-check.js +137 -50
- package/dist/core/extract-print.d.ts +1 -0
- package/dist/core/extract-print.js +26 -3
- package/dist/core/extract-read.d.ts +85 -4
- package/dist/core/extract-read.js +295 -34
- package/dist/core/extract-shapes.d.ts +14 -0
- package/dist/core/extract-shapes.js +608 -78
- package/dist/core/extract-xstate.d.ts +5 -1
- package/dist/core/extract-xstate.js +618 -184
- package/dist/core/extract.js +73 -27
- package/dist/core/infer.js +72 -18
- package/dist/core/machine.d.ts +6 -0
- package/dist/core/machine.js +35 -3
- package/dist/core/nonterm.d.ts +5 -1
- package/dist/core/nonterm.js +109 -41
- package/dist/core/q.d.ts +1 -0
- package/dist/core/q.js +2 -2
- package/dist/core/relation.d.ts +1 -0
- package/dist/core/relation.js +20 -36
- package/dist/core/solver.d.ts +12 -0
- package/dist/core/solver.js +76 -30
- package/dist/core/transfer.js +22 -13
- package/dist/core/translate.d.ts +1 -1
- package/dist/core/translate.js +22 -49
- package/dist/core/xstate.d.ts +6 -1
- package/dist/core/xstate.js +336 -55
- package/dist/kernel/cdcl.js +1 -2
- package/dist/kernel/prove.d.ts +6 -1
- package/dist/kernel/prove.js +96 -82
- package/dist/kernel/reader.d.ts +1 -0
- package/dist/kernel/reader.js +4 -3
- package/dist/kernel/simplex.d.ts +1 -1
- package/dist/kernel/simplex.js +13 -1
- package/package.json +3 -2
package/dist/core/nonterm.js
CHANGED
|
@@ -1,15 +1,37 @@
|
|
|
1
|
+
// Runs that never end, found: the search behind `proveNeverEnds`. Untrusted, like prove.ts: what it finds is a closed
|
|
2
|
+
// recurrent set with its successor given (kernel.ts's Recur), checked by kernel.ts's checkRecur and, exported, by
|
|
3
|
+
// NonTerm.lean's never_ends_of.
|
|
4
|
+
//
|
|
5
|
+
// - Runs. Random runs of the loop: params small or near the constants its conditions compare with, each turn's inputs
|
|
6
|
+
// small or a value the state holds. Each run keeps its turns' inputs, so any state it reached has a stem.
|
|
7
|
+
// - Lassos. A run back at a state it was in (the params and the state; the inputs are read afresh each turn): the set is
|
|
8
|
+
// the cycle's states, each turn's inputs given at its state by an if over the states (`lassoRecur`).
|
|
9
|
+
// - Policies. From a state a run reached, a run whose inputs are terms over the state and params: in each mode (the
|
|
10
|
+
// values of the state variables the loop compares with constants, its locations), the first allowed of a few
|
|
11
|
+
// assignments, each input a term from a pool (0, 1, ±1, the constants it is compared with, each state and param value,
|
|
12
|
+
// those ± 1); an assignment is added, drawn from the pools, where the run would stop. The policy is the set's witness.
|
|
13
|
+
// A policy run that comes back is a lasso; one that goes on is the set's sample.
|
|
14
|
+
// - The set. Per mode, atoms over the integer values (a pair x, x$n or x$neg is the integer x − x$n; a natural is
|
|
15
|
+
// itself): bounds of one value, of the sum and the difference of two, equalities, each true on the policy run. Those
|
|
16
|
+
// constant along it go in first; then each counterexample the prover's model gives (a state of the set whose
|
|
17
|
+
// witnessed turn fails or leaves the set) is excluded by one more atom false there, the steadiest and simplest first
|
|
18
|
+
// (its slack along the run never shrinking), until the closure goal is proven: kernel/prove.ts's grow, which tries
|
|
19
|
+
// kernel/cdcl.ts's CDCL on a linear goal first. Linear loops' geometric runs (a value doubling, two growing apart) are
|
|
20
|
+
// among these sets: their bounds and differences grow along the run.
|
|
21
|
+
// - Accelerated cycles, between the runs and the policies (core/accel.ts): a set from a cycle of the loop's relation,
|
|
22
|
+
// the cycle's n-fold run read in closed form (an atom's differences along it), and a stem from a reached state or a
|
|
23
|
+
// run with its loops accelerated, checked as the rest are.
|
|
24
|
+
import { n as K, same as eq, seeded, walkOnce, x as V } from "./machine.js";
|
|
1
25
|
import { checkRecur, loopMachine, recurGoal, substC, trueC } from "../kernel/kernel.js";
|
|
2
|
-
import { clock, counted, grow } from "../kernel/prove.js";
|
|
26
|
+
import { clock, counted, grow, realModels } from "../kernel/prove.js";
|
|
3
27
|
import { work } from "./solver.js";
|
|
4
28
|
import { accelerated, stemsTo } from "./accel.js";
|
|
5
29
|
import { findBoundedRun } from "./bmc.js";
|
|
6
|
-
const K = (value) => ({ op: 'const', value }), V = (name) => ({ op: 'var', name });
|
|
7
30
|
const T = trueC, F = { op: '<', a: K(0), b: K(0) };
|
|
8
|
-
const isT = (c) => c.op === '<=' && c.a.op === 'const' && c.b.op === 'const' && c.a.value <= c.b.value;
|
|
9
|
-
const isF = (c) => (c.op === '<' && c.a.op === 'const' && c.b.op === 'const' && c.a.value >= c.b.value) || (c.op === '<=' && c.a.op === 'const' && c.b.op === 'const' && c.a.value > c.b.value);
|
|
31
|
+
export const isT = (c) => c.op === '<=' && c.a.op === 'const' && c.b.op === 'const' && c.a.value <= c.b.value;
|
|
32
|
+
export const isF = (c) => (c.op === '<' && c.a.op === 'const' && c.b.op === 'const' && c.a.value >= c.b.value) || (c.op === '<=' && c.a.op === 'const' && c.b.op === 'const' && c.a.value > c.b.value);
|
|
10
33
|
const and = (xs) => { const ys = xs.flatMap(x => (x.op === 'and' ? x.xs : [x])).filter(x => !isT(x)); return ys.some(isF) ? F : ys.length === 0 ? T : ys.length === 1 ? ys[0] : { op: 'and', xs: ys }; };
|
|
11
34
|
const or = (xs) => { const ys = xs.flatMap(x => (x.op === 'or' ? x.xs : [x])).filter(x => !isF(x)); return ys.some(isT) ? T : ys.length === 0 ? F : ys.length === 1 ? ys[0] : { op: 'or', xs: ys }; };
|
|
12
|
-
const eq = (a, b) => ({ op: '=', a, b });
|
|
13
35
|
const plus = (a, b) => (a.op === 'const' && a.value === 0 ? b : b.op === 'const' && b.value === 0 ? a : a.op === 'const' && b.op === 'const' ? K(a.value + b.value) : { op: 'add', a, b });
|
|
14
36
|
const minus = (a, b) => (b.op === 'const' && b.value === 0 ? a : a.op === 'const' && b.op === 'const' ? K(Math.max(0, a.value - b.value)) : { op: 'sub', a, b });
|
|
15
37
|
const times = (k, a) => (k === 0 ? K(0) : k === 1 ? a : a.op === 'const' ? K(k * a.value) : { op: 'mul', k, a });
|
|
@@ -79,10 +101,12 @@ export function goalSize(x) {
|
|
|
79
101
|
return n;
|
|
80
102
|
}
|
|
81
103
|
const GOAL_MAX = 300_000;
|
|
82
|
-
// An accelerated set's goal: the witness reads the location (and
|
|
83
|
-
// so its ifs multiply the body's less
|
|
84
|
-
//
|
|
85
|
-
|
|
104
|
+
// An accelerated set's goal: the witness reads the location (and, where the cycle comes back to one, an atom that tells
|
|
105
|
+
// the parts there apart), so its ifs multiply the body's less; and the kernel reads a term shared in the goal once per
|
|
106
|
+
// case, so its size here is its distinct terms (TermComp's s1: 23 million nodes written out, 707,064 distinct, proven in
|
|
107
|
+
// 11 s; tqli: 720,729 written out, proven in 22 s).
|
|
108
|
+
export const dagSize = (x) => { let n = 0; walkOnce(x, () => { n++; }); return n; };
|
|
109
|
+
const ACCEL_DAG_MAX = 1_500_000;
|
|
86
110
|
// --- running the loop ---------------------------------------------------------------------------------------------------
|
|
87
111
|
class Overflow extends Error {
|
|
88
112
|
}
|
|
@@ -235,8 +259,7 @@ export function findNeverEnds(l, opts = {}) {
|
|
|
235
259
|
}
|
|
236
260
|
function search(l, opts) {
|
|
237
261
|
const wall0 = performance.now(), now = clock, t0 = now(), until = t0 + (opts.ms ?? 10_000), maxTurns = opts.maxTurns ?? 400;
|
|
238
|
-
|
|
239
|
-
const rand = (k) => { seed = (seed * 1103515245 + 12345) % 2 ** 31; return Math.floor(seed / 2 ** 31 * k); };
|
|
262
|
+
const rand = seeded(opts.seed ?? 7);
|
|
240
263
|
const stats = { runs: 0, turns: 0, policies: 0, rounds: 0, ms: 0, lassoTried: 0, goalSize: 0, accel: 0, accelClosed: 0 };
|
|
241
264
|
const done = (v) => ({ ...v, stats: { ...stats, ms: Math.round(performance.now() - wall0) } });
|
|
242
265
|
const stateNames = Object.keys(l.state), kept = [...l.params, ...stateNames];
|
|
@@ -386,7 +409,7 @@ function search(l, opts) {
|
|
|
386
409
|
return done({ proven: false, checked: false, why: `no lasso in ${stats.runs} runs` });
|
|
387
410
|
// 1b. Accelerated cycles (core/accel.ts): a set from a cycle of the relation, a stem into it, its closure proven.
|
|
388
411
|
if ((opts.accel ?? 0.4) > 0) {
|
|
389
|
-
const accelUntil = Math.min(until, now() + (until - now()) * (opts.accel ?? 0.4)), m = loopMachine(l);
|
|
412
|
+
const accelUntil = Math.min(until, now() + (until - now()) * (opts.accel ?? 0.4)), m = loopMachine(l), longUntil = Math.max(until, t0 + (opts.extend ?? 0));
|
|
390
413
|
try {
|
|
391
414
|
// (the random runs, the longest 20, for the cycles they take)
|
|
392
415
|
const ends = new Map();
|
|
@@ -394,17 +417,58 @@ function search(l, opts) {
|
|
|
394
417
|
if (v.run && (ends.get(v.run)?.at ?? -1) < v.at)
|
|
395
418
|
ends.set(v.run, v);
|
|
396
419
|
const runs = [...ends.values()].sort((a, b) => b.at - a.at).slice(0, 20).map(v => ({ params: v.params, inputs: stemOf(v) }));
|
|
420
|
+
// A set's closure (`budget`: when it stops), then, where no stem is known, a bounded run (core/bmc.ts: the kernel's
|
|
421
|
+
// k-step goal's model, the set's complement as the law it breaks), then the kernel's check of each stem; `late`
|
|
422
|
+
// where the closure ran out of time.
|
|
423
|
+
const attempt = (a, goal, stems, cut, budget) => {
|
|
424
|
+
// (a model of the CDCL search whose products don't multiply out goes on to the plain search, which multiplies rows)
|
|
425
|
+
const was = realModels.on;
|
|
426
|
+
realModels.on = true;
|
|
427
|
+
let out;
|
|
428
|
+
try {
|
|
429
|
+
out = grow(goal, new Map(), { nodes: 0, until: budget });
|
|
430
|
+
}
|
|
431
|
+
finally {
|
|
432
|
+
realModels.on = was;
|
|
433
|
+
}
|
|
434
|
+
if (!('tree' in out))
|
|
435
|
+
return out.stuck === 'unknown' && out.why !== undefined ? 'late' : undefined;
|
|
436
|
+
stats.accelClosed++;
|
|
437
|
+
if (!stems.length && now() < until) {
|
|
438
|
+
const left = Math.min(until - now(), 2000);
|
|
439
|
+
const run = findBoundedRun(m, [{ name: 'outside the set', holds: { op: 'not', x: a.set } }], { ms: left, depth: 24, ...(opts.counted ? {} : { wall: performance.now() + left }) });
|
|
440
|
+
const c = run && cut({ params: run.params, stem: run.trace.map(r => Object.fromEntries(l.inputs.map(n => [n, Number(r[n] ?? 0)]))) });
|
|
441
|
+
if (c)
|
|
442
|
+
stems.push(c);
|
|
443
|
+
}
|
|
444
|
+
for (const t of stems) {
|
|
445
|
+
const recur = { start: params(t.params), stem: asStem(t.stem), set: a.set, wit: a.wit, tree: out.tree };
|
|
446
|
+
let ok = false;
|
|
447
|
+
try {
|
|
448
|
+
ok = checkRecur(l, recur);
|
|
449
|
+
}
|
|
450
|
+
catch {
|
|
451
|
+
ok = false;
|
|
452
|
+
}
|
|
453
|
+
if (ok)
|
|
454
|
+
return done({ proven: true, checked: true, recur, by: 'accelerated' });
|
|
455
|
+
}
|
|
456
|
+
return undefined;
|
|
457
|
+
};
|
|
458
|
+
// (sets with a small goal that the accelerated runs' search, run to its end, doesn't reach: after the rest, their
|
|
459
|
+
// closures and bounded runs, in the time the search has left)
|
|
460
|
+
const later = [];
|
|
461
|
+
const again = [];
|
|
397
462
|
for (const a of accelerated(l, { now, until: accelUntil, runs, io: { start, turn } })) {
|
|
398
463
|
stats.accel++;
|
|
399
464
|
const goal = recurGoal(l, a);
|
|
400
465
|
stats.goalSize = Math.max(stats.goalSize, goalSize(goal.hyps));
|
|
401
|
-
if (
|
|
466
|
+
if (dagSize(goal.hyps) > ACCEL_DAG_MAX)
|
|
402
467
|
continue;
|
|
403
468
|
const inSet = cC(a.set);
|
|
404
469
|
// A stem into the set, cut where it first enters it (every turn allowed): from a state a random run reached (the
|
|
405
470
|
// shortest), else an accelerated run (cheap: tried before the closure, which is not tried for a set no stem
|
|
406
|
-
// reaches), else, once the set is closed, a bounded run
|
|
407
|
-
// complement as the law it breaks).
|
|
471
|
+
// reaches), else, once the set is closed, a bounded run.
|
|
408
472
|
const cut = (t) => {
|
|
409
473
|
let st = start(t.params);
|
|
410
474
|
for (let j = 0; j <= t.stem.length; j++) {
|
|
@@ -429,7 +493,7 @@ function search(l, opts) {
|
|
|
429
493
|
else {
|
|
430
494
|
// (a set's stem has time of its own, past the stage's if need be, within the search's)
|
|
431
495
|
let n = 0;
|
|
432
|
-
for (const t of stemsTo(l, a.targets, { start, turn }, { now, until: Math.min(
|
|
496
|
+
for (const t of stemsTo(l, a.targets, { start, turn }, { now, until: Math.min(longUntil, now() + 1000), report })) {
|
|
433
497
|
const c = cut(t);
|
|
434
498
|
if (c) {
|
|
435
499
|
stems.push(c);
|
|
@@ -439,34 +503,38 @@ function search(l, opts) {
|
|
|
439
503
|
break;
|
|
440
504
|
}
|
|
441
505
|
}
|
|
442
|
-
// (a set the accelerated runs' search, run to its end, doesn't reach is left: the time goes to sets a run
|
|
443
|
-
|
|
506
|
+
// (a set the accelerated runs' search, run to its end, doesn't reach is left: the time goes to sets a run
|
|
507
|
+
// reaches; a small one is tried after them)
|
|
508
|
+
if (!stems.length && report.exhausted) {
|
|
509
|
+
if (dagSize(goal.hyps) <= 50_000 && later.length < 8)
|
|
510
|
+
later.push({ a, goal, cut });
|
|
444
511
|
continue;
|
|
512
|
+
}
|
|
445
513
|
// (a set with a stem is likely: its closure may take most of what is left, past the stage's share; one without,
|
|
446
514
|
// the stage's share and at most 4 s)
|
|
447
|
-
const
|
|
448
|
-
if (
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
515
|
+
const v = attempt(a, goal, stems, cut, stems.length ? Math.min(until, now() + Math.max(4000, (until - now()) * 0.6)) : Math.min(accelUntil, now() + 4000));
|
|
516
|
+
if (v && v !== 'late')
|
|
517
|
+
return v;
|
|
518
|
+
if (v === 'late' && stems.length && longUntil > until && again.length < 3)
|
|
519
|
+
again.push({ a, goal, stems, cut });
|
|
520
|
+
}
|
|
521
|
+
// (the small sets no stem reaches: a closure and a bounded run each, in what is left of the search's time)
|
|
522
|
+
for (const { a, goal, cut } of later) {
|
|
523
|
+
if (now() >= until)
|
|
524
|
+
break;
|
|
525
|
+
const v = attempt(a, goal, [], cut, Math.min(until, now() + 2000));
|
|
526
|
+
if (v && v !== 'late')
|
|
527
|
+
return v;
|
|
528
|
+
}
|
|
529
|
+
// (a set with a stem whose closure ran out of time: again, with the time `extend` gives, shared, the smaller goal
|
|
530
|
+
// first)
|
|
531
|
+
again.sort((x, y) => dagSize(x.goal.hyps) - dagSize(y.goal.hyps));
|
|
532
|
+
for (const [k, { a, goal, stems, cut }] of again.entries()) {
|
|
533
|
+
if (now() >= longUntil - 1000)
|
|
534
|
+
break;
|
|
535
|
+
const v = attempt(a, goal, stems, cut, Math.min(longUntil, now() + Math.max(4000, (longUntil - now()) * 0.9 / (again.length - k))));
|
|
536
|
+
if (v && v !== 'late')
|
|
537
|
+
return v;
|
|
470
538
|
}
|
|
471
539
|
}
|
|
472
540
|
catch (e) {
|
package/dist/core/q.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Rat } from '../kernel/kernel.js';
|
|
2
2
|
export type { Rat };
|
|
3
|
+
export declare const bgcd: (a: bigint, b: bigint) => bigint;
|
|
3
4
|
export declare const q: (num: bigint | number, den?: bigint | number) => Rat;
|
|
4
5
|
export declare const Q0: Rat, Q1: Rat;
|
|
5
6
|
export declare const qadd: (a: Rat, b: Rat) => Rat;
|
package/dist/core/q.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
export const bgcd = (a, b) => { a = a < 0n ? -a : a; b = b < 0n ? -b : b; while (b)
|
|
2
2
|
[a, b] = [b, a % b]; return a; };
|
|
3
3
|
export const q = (num, den = 1n) => {
|
|
4
4
|
let n = BigInt(num), d = BigInt(den);
|
|
@@ -8,7 +8,7 @@ export const q = (num, den = 1n) => {
|
|
|
8
8
|
n = -n;
|
|
9
9
|
d = -d;
|
|
10
10
|
}
|
|
11
|
-
const g =
|
|
11
|
+
const g = bgcd(n, d) || 1n;
|
|
12
12
|
return { num: n / g, den: d / g };
|
|
13
13
|
};
|
|
14
14
|
export const Q0 = q(0), Q1 = q(1);
|
package/dist/core/relation.d.ts
CHANGED
|
@@ -25,4 +25,5 @@ export type Generators = {
|
|
|
25
25
|
lines: number[][];
|
|
26
26
|
};
|
|
27
27
|
export declare function generators(rows: Lin[], names: string[], nats?: string[], cap?: number, until?: number): Generators | undefined;
|
|
28
|
+
export declare const tightBounds: (cs: Lin[]) => Lin[];
|
|
28
29
|
export declare function houdini(rel: Relation, cands: Lin[][], locOf: (i: number) => [number, number], until: number): Lin[][] | undefined;
|
package/dist/core/relation.js
CHANGED
|
@@ -1,19 +1,9 @@
|
|
|
1
1
|
import { clock } from "../kernel/prove.js";
|
|
2
|
-
import { solve, work } from "./solver.js";
|
|
2
|
+
import { canon, konst, plus, solve, subst, times, vars, work } from "./solver.js";
|
|
3
|
+
import { bgcd } from "./q.js";
|
|
3
4
|
class Refused extends Error {
|
|
4
5
|
}
|
|
5
6
|
const id = (t) => JSON.stringify(t);
|
|
6
|
-
const canon = (l) => id(Object.keys(l).sort().map(x => [x, l[x]]));
|
|
7
|
-
const plus = (a, b, f = 1) => { const o = { ...a }; for (const x in b) {
|
|
8
|
-
o[x] = (o[x] ?? 0) + f * b[x];
|
|
9
|
-
if (!o[x])
|
|
10
|
-
delete o[x];
|
|
11
|
-
} return o; };
|
|
12
|
-
const times = (a, f) => { const o = {}; if (f)
|
|
13
|
-
for (const x in a)
|
|
14
|
-
o[x] = a[x] * f; return o; };
|
|
15
|
-
const konst = (c) => (c ? { 1: c } : {});
|
|
16
|
-
const vars = (l) => Object.keys(l).filter(x => x !== '1');
|
|
17
7
|
const pinned = (l, pins) => {
|
|
18
8
|
let out;
|
|
19
9
|
for (const x in l)
|
|
@@ -412,10 +402,6 @@ const renameC = (c, names, f) => {
|
|
|
412
402
|
case 'all': return c;
|
|
413
403
|
}
|
|
414
404
|
};
|
|
415
|
-
const bgcd = (a, b) => { if (a < 0n)
|
|
416
|
-
a = -a; if (b < 0n)
|
|
417
|
-
b = -b; while (b)
|
|
418
|
-
[a, b] = [b, a % b]; return a; };
|
|
419
405
|
const norm = (x) => { const g = x.reduce(bgcd, 0n); return g > 1n ? x.map(y => y / g) : x; };
|
|
420
406
|
const dot = (a, b) => a.reduce((s, x, i) => s + x * b[i], 0n);
|
|
421
407
|
export function generators(rows, names, nats = [], cap = 600, until = Infinity) {
|
|
@@ -475,6 +461,21 @@ export function generators(rows, names, nats = [], cap = 600, until = Infinity)
|
|
|
475
461
|
const num = (x) => x.map(Number);
|
|
476
462
|
return { names, vertices: rays.filter(r => r.v[n] > 0n).map(r => num(r.v)), rays: rays.filter(r => r.v[n] === 0n).map(r => num(r.v)), lines: lines.map(num) };
|
|
477
463
|
}
|
|
464
|
+
// Of the bounds on one name each way, the tightest (the others follow from it), then the rows of more names.
|
|
465
|
+
export const tightBounds = (cs) => {
|
|
466
|
+
const best = new Map(), rest = [];
|
|
467
|
+
for (const c of cs) {
|
|
468
|
+
const xs = vars(c);
|
|
469
|
+
if (xs.length !== 1) {
|
|
470
|
+
rest.push(c);
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
const x = xs[0], key = `${x}${Math.sign(c[x])}`, b = (c['1'] ?? 0) / Math.abs(c[x]), was = best.get(key);
|
|
474
|
+
if (!was || b > (was['1'] ?? 0) / Math.abs(was[x]))
|
|
475
|
+
best.set(key, c);
|
|
476
|
+
}
|
|
477
|
+
return [...best.values(), ...rest];
|
|
478
|
+
};
|
|
478
479
|
// --- invariants: Houdini over the relation --------------------------------------------------------------------------
|
|
479
480
|
//
|
|
480
481
|
// Candidate atoms per location (each lin ≤ 0 over the relation's names), those that fail at a start dropped by the
|
|
@@ -483,22 +484,6 @@ export function generators(rows, names, nats = [], cap = 600, until = Infinity)
|
|
|
483
484
|
// inductive over the relation (so over the loop, where the relation is exact); the kernel proves it again as laws.
|
|
484
485
|
export function houdini(rel, cands, locOf, until) {
|
|
485
486
|
const H = cands.map(cs => [...new Map(cs.map(c => [canon(c), c])).values()]);
|
|
486
|
-
// A path's rows with the candidates at its start: of the bounds on one name, only the tightest each way (the others
|
|
487
|
-
// add no vertex).
|
|
488
|
-
const tight = (cs) => {
|
|
489
|
-
const best = new Map(), rest = [];
|
|
490
|
-
for (const c of cs) {
|
|
491
|
-
const xs = vars(c);
|
|
492
|
-
if (xs.length !== 1) {
|
|
493
|
-
rest.push(c);
|
|
494
|
-
continue;
|
|
495
|
-
}
|
|
496
|
-
const x = xs[0], s = Math.sign(c[x]), key = `${x}${s}`, b = (c['1'] ?? 0) / Math.abs(c[x]), was = best.get(key);
|
|
497
|
-
if (!was || b > (was['1'] ?? 0) / Math.abs(was[x]))
|
|
498
|
-
best.set(key, c);
|
|
499
|
-
}
|
|
500
|
-
return [...best.values(), ...rest];
|
|
501
|
-
};
|
|
502
487
|
for (let round = 0, changed = true; changed && round < 50; round++) {
|
|
503
488
|
changed = false;
|
|
504
489
|
for (const [i, p] of rel.paths.entries()) {
|
|
@@ -507,16 +492,15 @@ export function houdini(rel, cands, locOf, until) {
|
|
|
507
492
|
const [a, b] = locOf(i);
|
|
508
493
|
if (!H[b].length)
|
|
509
494
|
continue;
|
|
510
|
-
|
|
495
|
+
// (a path's rows with the candidates at its start, the tightest bounds of them: the others add no vertex)
|
|
496
|
+
const rows = [...p.rows, ...tightBounds(H[a])];
|
|
511
497
|
// (the names the candidates at the target read too: one the path reads nowhere is free there, a line)
|
|
512
498
|
const ns = [...new Set([...rows.flatMap(vars), ...Object.values(p.post).flatMap(vars), ...H[b].flatMap(vars)])].sort();
|
|
513
499
|
const g = generators(rows, ns, rel.nats, 600, until);
|
|
514
500
|
if (!g) {
|
|
515
501
|
// past the generators' cap: each candidate after the turn refuted by the integer solver, or dropped
|
|
516
502
|
const nat = rel.nats.filter(x => ns.includes(x)).map((x) => ({ [x]: -1 }));
|
|
517
|
-
const
|
|
518
|
-
o = plus(o, y !== '1' && p.post[y] ? times(p.post[y], k) : { [y]: k }); return o; };
|
|
519
|
-
const keep = H[b].filter(c => clock() < until && solve([...rows, ...nat, plus(konst(1), through(c), -1)].map(lin => ({ lin, label: '' })), 4000).kind === 'refuted');
|
|
503
|
+
const keep = H[b].filter(c => clock() < until && solve([...rows, ...nat, plus(konst(1), subst(c, p.post), -1)].map(lin => ({ lin, label: '' })), 4000).kind === 'refuted');
|
|
520
504
|
if (keep.length < H[b].length) {
|
|
521
505
|
H[b] = keep;
|
|
522
506
|
changed = true;
|
package/dist/core/solver.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { type Rat } from './q.js';
|
|
2
|
+
import type { E } from './machine.js';
|
|
2
3
|
export type Lin = Record<string, number>;
|
|
4
|
+
export declare const env: Record<string, string | undefined>;
|
|
3
5
|
export type Constraint = {
|
|
4
6
|
lin: Lin;
|
|
5
7
|
label: string;
|
|
@@ -53,8 +55,18 @@ export type Verdict = {
|
|
|
53
55
|
kind: 'unknown';
|
|
54
56
|
point?: Record<string, number>;
|
|
55
57
|
};
|
|
58
|
+
export declare const gcd: (a: number, b: number) => number;
|
|
59
|
+
export declare const vars: (lin: Lin) => string[];
|
|
60
|
+
export declare const plus: (a: Lin, b: Lin, f?: number) => Lin;
|
|
61
|
+
export declare const times: (a: Lin, f: number) => Lin;
|
|
62
|
+
export declare const konst: (c: number) => Lin;
|
|
63
|
+
export declare const linOfE: (e: E) => Lin | undefined;
|
|
64
|
+
export declare const subst: (l: Lin, m: Record<string, Lin>) => Lin;
|
|
65
|
+
export declare const canon: (l: Lin) => string;
|
|
56
66
|
export declare const work: {
|
|
57
67
|
n: number;
|
|
58
68
|
};
|
|
59
69
|
export declare function solve(constraints: Constraint[], maxRows?: number, integral?: boolean): Verdict;
|
|
70
|
+
export declare const ratioRow: (T: Float64Array[], basis: number[], enter: number, cols: number, eps: number) => [number, number];
|
|
71
|
+
export declare function pivot(T: Float64Array[], z: Float64Array | undefined, basis: number[], leave: number, enter: number, cols: number): void;
|
|
60
72
|
export declare function refuteFast(constraints: Constraint[], maxRows?: number, integral?: boolean): Verdict | undefined;
|
package/dist/core/solver.js
CHANGED
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
// row is strict), so the answer is refuted or sat, the witness exact (`rational`), the derivation one check.ts takes
|
|
12
12
|
// with no cut: the integers are this with the cut rule, and a strict row over them is lin + 1 <= 0.
|
|
13
13
|
import { Q0, Q1, q, qadd, qcmp, qdiv, qneg, qnum, qrow, qsub } from "./q.js";
|
|
14
|
+
// The process's environment (the switches and traces the searches read), empty where there is none.
|
|
15
|
+
export const env = globalThis.process?.env ?? {};
|
|
14
16
|
export const v = (name) => ({ [name]: 1 });
|
|
15
17
|
export const k = (value) => (value === 0 ? {} : { 1: value });
|
|
16
18
|
// (Plain loops, no intermediate arrays: these run millions of times in a proof. Each keeps the keys in the order they
|
|
@@ -57,8 +59,43 @@ export const eq = (a, b, label) => [...le(a, b, label ?? `${show(a)} = ${show(b)
|
|
|
57
59
|
export const nat = (...names) => names.flatMap(name => le(k(0), v(name), `${name} >= 0`));
|
|
58
60
|
// not (lin <= 0) over the integers is lin >= 1, i.e. 1 − lin <= 0.
|
|
59
61
|
export const negate = (c) => ({ lin: add(k(1), scale(c.lin, -1)), label: `not (${c.label})` });
|
|
60
|
-
const gcd = (a, b) => (b === 0 ? Math.abs(a) : gcd(b, a % b));
|
|
61
|
-
const vars = (lin) => Object.keys(lin).filter(key => key !== '1');
|
|
62
|
+
export const gcd = (a, b) => (b === 0 ? Math.abs(a) : gcd(b, a % b));
|
|
63
|
+
export const vars = (lin) => Object.keys(lin).filter(key => key !== '1');
|
|
64
|
+
// The relation's and the accelerations' arithmetic (a's keys first, then b's new ones, no zero kept).
|
|
65
|
+
export const plus = (a, b, f = 1) => { const o = { ...a }; for (const x in b) {
|
|
66
|
+
o[x] = (o[x] ?? 0) + f * b[x];
|
|
67
|
+
if (!o[x])
|
|
68
|
+
delete o[x];
|
|
69
|
+
} return o; };
|
|
70
|
+
export const times = (a, f) => { const o = {}; if (f)
|
|
71
|
+
for (const x in a)
|
|
72
|
+
o[x] = a[x] * f; return o; };
|
|
73
|
+
export const konst = (c) => (c ? { 1: c } : {});
|
|
74
|
+
// A term as a linear form where it is one (names, constants, sums and constant multiples).
|
|
75
|
+
export const linOfE = (e) => {
|
|
76
|
+
switch (e.op) {
|
|
77
|
+
case 'var': return { [e.name]: 1 };
|
|
78
|
+
case 'const': return konst(e.value);
|
|
79
|
+
case 'add': {
|
|
80
|
+
const a = linOfE(e.a), b = linOfE(e.b);
|
|
81
|
+
return a && b && plus(a, b);
|
|
82
|
+
}
|
|
83
|
+
case 'mul': {
|
|
84
|
+
const a = linOfE(e.a);
|
|
85
|
+
return a && times(a, e.k);
|
|
86
|
+
}
|
|
87
|
+
case 'sub':
|
|
88
|
+
case 'prod':
|
|
89
|
+
case 'min':
|
|
90
|
+
case 'max':
|
|
91
|
+
case 'if':
|
|
92
|
+
case 'get': return undefined;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
// l with each name of m's replaced by its form there
|
|
96
|
+
export const subst = (l, m) => { let o = {}; for (const [x, c] of Object.entries(l))
|
|
97
|
+
o = plus(o, x !== '1' && m[x] ? times(m[x], c) : { [x]: c }); return o; };
|
|
98
|
+
export const canon = (l) => JSON.stringify(Object.keys(l).sort().map(x => [x, l[x]]));
|
|
62
99
|
// maxRows: elimination can square the rows each round; past this many live rows, or a round that would make more than
|
|
63
100
|
// five times as many before merging them, it gives up with `unknown`.
|
|
64
101
|
// The rows the solver has read and made, which a counted search reads as its clock (kernel/prove.ts's `counted`).
|
|
@@ -304,6 +341,41 @@ function prune(steps, last) {
|
|
|
304
341
|
return s.kind === 'combine' ? { ...s, a: at.get(s.a), b: at.get(s.b) } : s.kind === 'cut' ? { ...s, of: at.get(s.of) } : s;
|
|
305
342
|
});
|
|
306
343
|
}
|
|
344
|
+
// A float simplex's steps (refuteFast's, cycles.ts's floatSolve): the row leaving where a column enters, the least ratio
|
|
345
|
+
// (Bland's tie-break by basis index; -1: unbounded), and its pivot, the reduced costs z with it.
|
|
346
|
+
export const ratioRow = (T, basis, enter, cols, eps) => {
|
|
347
|
+
let leave = -1, best = Infinity;
|
|
348
|
+
for (let r = 0; r < T.length; r++) {
|
|
349
|
+
const a = T[r][enter];
|
|
350
|
+
if (a > eps) {
|
|
351
|
+
const ratio = T[r][cols] / a;
|
|
352
|
+
if (ratio < best - eps || (Math.abs(ratio - best) <= eps && basis[r] < basis[leave])) {
|
|
353
|
+
best = ratio;
|
|
354
|
+
leave = r;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return [leave, best];
|
|
359
|
+
};
|
|
360
|
+
export function pivot(T, z, basis, leave, enter, cols) {
|
|
361
|
+
const p = T[leave], pv = p[enter];
|
|
362
|
+
for (let j = 0; j <= cols; j++)
|
|
363
|
+
p[j] /= pv;
|
|
364
|
+
for (let r = 0; r < T.length; r++)
|
|
365
|
+
if (r !== leave) {
|
|
366
|
+
const f = T[r][enter];
|
|
367
|
+
if (f !== 0) {
|
|
368
|
+
const row = T[r];
|
|
369
|
+
for (let j = 0; j <= cols; j++)
|
|
370
|
+
row[j] -= f * p[j];
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
const f = z ? z[enter] : 0;
|
|
374
|
+
if (f !== 0)
|
|
375
|
+
for (let j = 0; j <= cols; j++)
|
|
376
|
+
z[j] -= f * p[j];
|
|
377
|
+
basis[leave] = enter;
|
|
378
|
+
}
|
|
307
379
|
// A refutation found fast, where there is one over the rationals: Farkas' lemma says rows a·x + c <= 0 (x free) have no
|
|
308
380
|
// rational solution exactly when some λ >= 0 has Σ λ·a = 0 and Σ λ·c > 0. A floating-point simplex looks for such λ
|
|
309
381
|
// (phase one on Σ λ·a = 0, Σ λ·c = 1), and elimination on the rows it uses alone (usually a handful) gives the
|
|
@@ -354,36 +426,10 @@ export function refuteFast(constraints, maxRows = 2_000, integral = true) {
|
|
|
354
426
|
}
|
|
355
427
|
if (enter < 0)
|
|
356
428
|
break;
|
|
357
|
-
|
|
358
|
-
for (let r = 0; r < R; r++) {
|
|
359
|
-
const a = T[r][enter];
|
|
360
|
-
if (a > eps) {
|
|
361
|
-
const ratio = T[r][cols] / a;
|
|
362
|
-
if (ratio < best - eps || (Math.abs(ratio - best) <= eps && basis[r] < basis[leave])) {
|
|
363
|
-
best = ratio;
|
|
364
|
-
leave = r;
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
}
|
|
429
|
+
const [leave] = ratioRow(T, basis, enter, cols, eps);
|
|
368
430
|
if (leave < 0)
|
|
369
431
|
return undefined; // unbounded: cannot happen in phase one; give up rather than guess
|
|
370
|
-
|
|
371
|
-
for (let j = 0; j <= cols; j++)
|
|
372
|
-
p[j] /= pv;
|
|
373
|
-
for (let r = 0; r < R; r++)
|
|
374
|
-
if (r !== leave) {
|
|
375
|
-
const f = T[r][enter];
|
|
376
|
-
if (f !== 0) {
|
|
377
|
-
const row = T[r];
|
|
378
|
-
for (let j = 0; j <= cols; j++)
|
|
379
|
-
row[j] -= f * p[j];
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
const f = z[enter];
|
|
383
|
-
if (f !== 0)
|
|
384
|
-
for (let j = 0; j <= cols; j++)
|
|
385
|
-
z[j] -= f * p[j];
|
|
386
|
-
basis[leave] = enter;
|
|
432
|
+
pivot(T, z, basis, leave, enter, cols);
|
|
387
433
|
}
|
|
388
434
|
if (-z[cols] > 1e-7)
|
|
389
435
|
return undefined; // the artificials can't all leave: no λ, so a rational solution exists
|
package/dist/core/transfer.js
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
// A datatype law said another way, proven that way, and the law read back from it (core/data.ts's proveData calls this
|
|
2
|
+
// where the law alone at one unfolding isn't proven). Untrusted, as the search is: each step is a node of the one
|
|
3
|
+
// cyclic proof kernel/cyclic.ts checks, and Lean's twin reads it as any other.
|
|
4
|
+
// - Unfolding: a call to a function that isn't recursive is its body at the call's arguments, in the law and in the
|
|
5
|
+
// copies below (each again, down to the recursive functions), so the induction is chosen on what the law means. A
|
|
6
|
+
// law that grows past eight times its size so (definitions unfolded into each other's arguments) isn't unfolded.
|
|
7
|
+
// - Bridges: a recursive function of one or two naturals that computes a built-in (+, ∸, min, max, ≤, <, =, ≥, >, ≠,
|
|
8
|
+
// x + 1, x ∸ 1, 2·x, x = 0) at every argument up to 6 (12 for one) is a candidate, unless its bridge law (the function
|
|
9
|
+
// is the built-in, at every argument) is the law itself; the bridge law is proven by the search first, and only a
|
|
10
|
+
// bridge proven is used.
|
|
11
|
+
// - Copies: a recursive function that calls a bridged one, one unfolded, or a copied one is copied with the built-in,
|
|
12
|
+
// the body or the copy in its place, and its copy law (it is its copy at every argument) proven: one node made from
|
|
13
|
+
// the two bodies walked together (its recursive calls by the node's own induction, at its recursion's uses), or,
|
|
14
|
+
// where the kernel refuses that, by the search with the bridges and its callees' copy laws as lemmas.
|
|
15
|
+
// - The law through them: the law unfolded, its bridged calls the built-ins, its other calls the copies, proven by
|
|
16
|
+
// the search (lemmas and all; through the bridges and unfolded, half the time left, then through the unfolding
|
|
17
|
+
// alone); then one node, the law as stated, using that proof's node, each bridge and copy law at the calls of the
|
|
18
|
+
// law (read at their arguments), each unfolded definition's fact at its call, and the congruences between a call
|
|
19
|
+
// and the same call at the arguments said another way. Its goal is equalities; nothing is assumed.
|
|
20
|
+
// The bridges and the copies' laws are nodes like any lemma's, at levels below the law's.
|
|
21
|
+
import { n as k, walk } from "./machine.js";
|
|
1
22
|
import { Bool, Integer, Nat, Val, argsOf, bindName, define, encX, encY, evalX, factsOf, fnNamed, programOf, rankWidth, schemesOf, searchIn } from "./data.js";
|
|
2
23
|
import { proveCyclic } from "./cyclic.js";
|
|
3
24
|
import { clock, pairedKeys, withDeadline } from "../kernel/prove.js";
|
|
@@ -76,19 +97,8 @@ function isBridge(y, f, op) {
|
|
|
76
97
|
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
98
|
&& JSON.stringify(x) === JSON.stringify(op.x(c.args)));
|
|
78
99
|
}
|
|
79
|
-
const callsOf = (t) => { const out = [];
|
|
100
|
+
const callsOf = (t) => { const out = []; walk(t, o => { if (o.op === 'call')
|
|
80
101
|
out.push(o.fn); }); return out; };
|
|
81
|
-
function walkO(t, f) {
|
|
82
|
-
if (Array.isArray(t)) {
|
|
83
|
-
t.forEach(x => walkO(x, f));
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
if (!t || typeof t !== 'object')
|
|
87
|
-
return;
|
|
88
|
-
f(t);
|
|
89
|
-
for (const v of Object.values(t))
|
|
90
|
-
walkO(v, f);
|
|
91
|
-
}
|
|
92
102
|
export function transferPlan(l) {
|
|
93
103
|
const p = programOf([l]), bridges = new Map();
|
|
94
104
|
for (const f of p.fns) {
|
|
@@ -270,7 +280,6 @@ function build(l, bridged, unfold) {
|
|
|
270
280
|
return { p, lstar, bridges, equivs, copies, unfolded: unfold };
|
|
271
281
|
}
|
|
272
282
|
// --- the proof ---------------------------------------------------------------------------------------------------------
|
|
273
|
-
const k = (value) => ({ op: 'const', value });
|
|
274
283
|
const yes = { op: '<=', a: k(0), b: k(0) };
|
|
275
284
|
const top = (pr) => Math.max(0, ...pr.nodes.map(n => (n.rank[0]?.op === 'const' ? n.rank[0].value : 0)));
|
|
276
285
|
const verify = (fs, specs, until) => {
|
package/dist/core/translate.d.ts
CHANGED