gdp-wired 0.24.0 → 0.25.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/dist/core/accel.d.ts +44 -0
- package/dist/core/accel.js +720 -0
- package/dist/core/cycles.js +428 -97
- package/dist/core/data.d.ts +8 -0
- package/dist/core/data.js +828 -95
- package/dist/core/infer.d.ts +1 -0
- package/dist/core/infer.js +298 -28
- package/dist/core/nonterm.d.ts +4 -1
- package/dist/core/nonterm.js +98 -2
- package/dist/core/relation.js +75 -3
- package/dist/core/transfer.js +1 -1
- package/dist/kernel/cdcl.d.ts +5 -0
- package/dist/kernel/cdcl.js +43 -6
- package/dist/kernel/prove.js +6 -4
- package/package.json +1 -1
package/dist/core/cycles.js
CHANGED
|
@@ -2,7 +2,7 @@ import { compileFunction, evalC as evalMC, evalE as evalME, fnValue, interpret,
|
|
|
2
2
|
import { check, livenessHelpful, livenessParts, loopLiveness, loopMachine, loopObligations, substC, substE } from "../kernel/kernel.js";
|
|
3
3
|
import { checkCyclic, factsAvoid, lexLt } from "../kernel/cyclic.js";
|
|
4
4
|
import { proveCyclic } from "./cyclic.js";
|
|
5
|
-
import { solve, work } from "./solver.js";
|
|
5
|
+
import { refuteFast, solve, work } from "./solver.js";
|
|
6
6
|
import { clock, grow, proveLaws, proveLiveness, proveStepLaws, withDeadline } from "../kernel/prove.js";
|
|
7
7
|
import { inferLaws } from "./infer.js";
|
|
8
8
|
import { generators, houdini, relationOf } from "./relation.js";
|
|
@@ -415,6 +415,9 @@ function lp(must, pool, until, scale = 1, signs = [true, false], whole = false)
|
|
|
415
415
|
};
|
|
416
416
|
// The widest box first: where it has no solution, none of the smaller has. Past it, a rank's rows (homogeneous in the
|
|
417
417
|
// unknowns, the drop's constant aside) are solved exactly over the rationals, the solution scaled to whole numbers.
|
|
418
|
+
// (a path's generators, `whole`: the LP over the rationals alone, its least Σ|u| as small a rank as the boxes find)
|
|
419
|
+
if (whole && !fit)
|
|
420
|
+
return exactLp(must, pool, until, whole);
|
|
418
421
|
if (!stage(signs.at(-1), bounds.at(-1), true))
|
|
419
422
|
return fit ? undefined : exactLp(must, pool, until, whole);
|
|
420
423
|
for (const natural of signs)
|
|
@@ -432,52 +435,67 @@ const bgcd = (a, b) => { if (a < 0n)
|
|
|
432
435
|
[a, b] = [b, a % b]; return a; };
|
|
433
436
|
const q = (n, d = 1n) => { if (d < 0n)
|
|
434
437
|
(n = -n, d = -d); const g = bgcd(n, d) || 1n; return g === 1n ? { n, d } : { n: n / g, d: d / g }; };
|
|
435
|
-
const Q0 = q(0n)
|
|
438
|
+
const Q0 = q(0n);
|
|
436
439
|
const qadd = (a, b) => (a.d === b.d ? q(a.n + b.n, a.d) : q(a.n * b.d + b.n * a.d, a.d * b.d));
|
|
437
440
|
const qsub = (a, b) => qadd(a, { n: -b.n, d: b.d });
|
|
438
|
-
const
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
441
|
+
const reduce = (r) => { let g = r.d; for (const x of r.n) {
|
|
442
|
+
if (g === 1n)
|
|
443
|
+
break;
|
|
444
|
+
if (x)
|
|
445
|
+
g = bgcd(g, x);
|
|
446
|
+
} return g > 1n ? { n: r.n.map(x => x / g), d: r.d / g } : r; };
|
|
447
|
+
// row r minus f times the pivot row (its pivot entry pv > 0 at column e): r's entry at e becomes 0
|
|
448
|
+
const eliminate = (r, piv, e) => {
|
|
449
|
+
const f = r.n[e], pv = piv.n[e];
|
|
450
|
+
if (!f)
|
|
451
|
+
return r;
|
|
452
|
+
return reduce({ n: r.n.map((x, j) => x * pv - f * piv.n[j]), d: r.d * pv });
|
|
453
|
+
};
|
|
454
|
+
function pivotOn(T, z, leave, e) {
|
|
455
|
+
let L = T[leave];
|
|
456
|
+
if (L.n[e] < 0n)
|
|
457
|
+
L = { n: L.n.map(x => -x), d: L.d };
|
|
458
|
+
T[leave] = reduce({ n: L.n, d: L.n[e] });
|
|
459
|
+
const piv = { n: L.n, d: 1n };
|
|
460
|
+
for (let r = 0; r < T.length; r++)
|
|
461
|
+
if (r !== leave && T[r].n[e])
|
|
462
|
+
T[r] = eliminate(T[r], piv, e);
|
|
463
|
+
return z && eliminate(z, piv, e);
|
|
464
|
+
}
|
|
443
465
|
function simplex(T, basis, cost, open, until) {
|
|
444
466
|
const cols = cost.length;
|
|
467
|
+
let z = { n: [...cost, 0n], d: 1n };
|
|
468
|
+
for (const [i, b] of basis.entries())
|
|
469
|
+
if (cost[b])
|
|
470
|
+
z = eliminate(z, { n: T[i].n, d: 1n }, b);
|
|
445
471
|
for (let it = 0; it < 5000; it++) {
|
|
446
472
|
// (each pivot counted as work, as the solver's rows are: a counted search's clock runs while it solves)
|
|
447
473
|
work.n += 1 + ((T.length * cols) >> 6);
|
|
448
474
|
if (clock() > until)
|
|
449
475
|
return false;
|
|
450
476
|
let enter = -1;
|
|
451
|
-
for (let j = 0; j < cols && enter < 0; j++)
|
|
452
|
-
if (
|
|
453
|
-
continue;
|
|
454
|
-
let r = cost[j];
|
|
455
|
-
for (const [i, b] of basis.entries())
|
|
456
|
-
if (T[i][j].n && cost[b].n)
|
|
457
|
-
r = qsub(r, qmul(cost[b], T[i][j]));
|
|
458
|
-
if (r.n < 0n)
|
|
477
|
+
for (let j = 0; j < cols && enter < 0; j++)
|
|
478
|
+
if (z.n[j] < 0n && open(j))
|
|
459
479
|
enter = j;
|
|
460
|
-
}
|
|
461
480
|
if (enter < 0)
|
|
462
481
|
return true;
|
|
463
|
-
let leave = -1
|
|
482
|
+
let leave = -1;
|
|
464
483
|
for (const [i, row] of T.entries()) {
|
|
465
|
-
|
|
484
|
+
const a = row.n[enter];
|
|
485
|
+
if (a <= 0n)
|
|
486
|
+
continue;
|
|
487
|
+
if (leave < 0) {
|
|
488
|
+
leave = i;
|
|
466
489
|
continue;
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
490
|
+
}
|
|
491
|
+
// row's rhs / a against the best's (the denominators cancel)
|
|
492
|
+
const L = T[leave], lhs = row.n[cols] * L.n[enter], rhs = L.n[cols] * a;
|
|
493
|
+
if (lhs < rhs || (lhs === rhs && basis[i] < basis[leave]))
|
|
494
|
+
leave = i;
|
|
470
495
|
}
|
|
471
496
|
if (leave < 0)
|
|
472
497
|
return false; // unbounded: not for an objective bounded below
|
|
473
|
-
|
|
474
|
-
T[leave] = T[leave].map(x => qdiv(x, p));
|
|
475
|
-
for (const [i, row] of T.entries()) {
|
|
476
|
-
if (i === leave || !row[enter].n)
|
|
477
|
-
continue;
|
|
478
|
-
const f = row[enter];
|
|
479
|
-
T[i] = row.map((x, j) => qsub(x, qmul(f, T[leave][j])));
|
|
480
|
-
}
|
|
498
|
+
z = pivotOn(T, z, leave, enter);
|
|
481
499
|
basis[leave] = enter;
|
|
482
500
|
}
|
|
483
501
|
return false;
|
|
@@ -486,7 +504,211 @@ function simplex(T, basis, cost, open, until) {
|
|
|
486
504
|
// rational solution scaled by its denominators' least common multiple and divided by its numerators' common divisor;
|
|
487
505
|
// kept only if every row holds of it exactly (a row with a positive constant, a drop of at least 1, holds of any
|
|
488
506
|
// multiple at least 1 of a solution; one with a negative constant may not).
|
|
507
|
+
// The same LP in floating point first (the floats only pick the point): each coefficient read back as the nearest
|
|
508
|
+
// fraction of small denominator, scaled to whole numbers and kept only where every row holds of it exactly; else the
|
|
509
|
+
// exact simplex below (where the floats find no point, none is looked for again). Over hundreds of rows the floats are what makes the search affordable (the exact tableau's
|
|
510
|
+
// reductions dominated it).
|
|
511
|
+
const nearFraction = (x, most = 10_000) => {
|
|
512
|
+
// continued fractions: the last convergent within the denominator bound
|
|
513
|
+
let [h0, h1, k0, k1, y] = [0, 1, 1, 0, x];
|
|
514
|
+
for (let i = 0; i < 40; i++) {
|
|
515
|
+
const a = Math.floor(y), h2 = a * h1 + h0, k2 = a * k1 + k0;
|
|
516
|
+
if (k2 > most)
|
|
517
|
+
break;
|
|
518
|
+
[h0, h1, k0, k1] = [h1, h2, k1, k2];
|
|
519
|
+
if (Math.abs(x - h1 / k1) < 1e-12 || Math.abs(y - a) < 1e-12)
|
|
520
|
+
break;
|
|
521
|
+
y = 1 / (y - a);
|
|
522
|
+
}
|
|
523
|
+
return [h1, k1];
|
|
524
|
+
};
|
|
525
|
+
const lpTrace = !!globalThis.process?.env?.LP_TRACE;
|
|
526
|
+
function floatSolve(rows, until, equil = true, again = true) {
|
|
527
|
+
const xs = [...new Set(rows.flatMap(l => Object.keys(l)).filter(x => x !== '1'))].sort(), n = xs.length, m = rows.length;
|
|
528
|
+
const cols = 2 * n + 2 * m, T = [], basis = [], eps = 1e-9;
|
|
529
|
+
for (const [i, l] of rows.entries()) {
|
|
530
|
+
let rhs = -(l['1'] ?? 0);
|
|
531
|
+
const sign = rhs < 0 ? -1 : 1;
|
|
532
|
+
rhs *= sign;
|
|
533
|
+
// (each row scaled to its largest entry 1: a vertex's coordinates reach 2^31, the drop's constant is 1)
|
|
534
|
+
const row = new Float64Array(cols + 1), big = (equil && Math.max(Math.abs(rhs), ...xs.map(x => Math.abs(l[x] ?? 0)))) || 1;
|
|
535
|
+
for (const [j, x] of xs.entries()) {
|
|
536
|
+
const c = sign * (l[x] ?? 0) / big;
|
|
537
|
+
if (c)
|
|
538
|
+
(row[j] = c, row[n + j] = -c);
|
|
539
|
+
}
|
|
540
|
+
row[2 * n + i] = sign;
|
|
541
|
+
if (sign < 0) {
|
|
542
|
+
row[2 * n + m + i] = 1;
|
|
543
|
+
basis.push(2 * n + m + i);
|
|
544
|
+
}
|
|
545
|
+
else
|
|
546
|
+
basis.push(2 * n + i);
|
|
547
|
+
row[cols] = rhs / big;
|
|
548
|
+
T.push(row);
|
|
549
|
+
}
|
|
550
|
+
const art = (j) => j >= 2 * n + m;
|
|
551
|
+
// minimize cost·y, the reduced costs kept as a row: the most negative enters (Dantzig's rule), Bland's after a run of
|
|
552
|
+
// degenerate pivots (no cycling)
|
|
553
|
+
const run = (cost, open) => {
|
|
554
|
+
const z = new Float64Array(cols + 1);
|
|
555
|
+
for (let j = 0; j < cols; j++)
|
|
556
|
+
z[j] = cost(j);
|
|
557
|
+
for (const [i, b] of basis.entries()) {
|
|
558
|
+
const c = cost(b);
|
|
559
|
+
if (c)
|
|
560
|
+
for (let j = 0; j <= cols; j++)
|
|
561
|
+
z[j] -= c * T[i][j];
|
|
562
|
+
}
|
|
563
|
+
let stalled = 0;
|
|
564
|
+
for (let it = 0; it < 10 * (m + cols); it++) {
|
|
565
|
+
// (each pivot counted as work, an eighth of the exact simplex's: a counted search's clock runs while it solves)
|
|
566
|
+
work.n += 1 + ((T.length * cols) >> 9);
|
|
567
|
+
if ((it & 63) === 0 && clock() > until)
|
|
568
|
+
return false;
|
|
569
|
+
let enter = -1;
|
|
570
|
+
if (stalled < 50) {
|
|
571
|
+
let low = -eps;
|
|
572
|
+
for (let j = 0; j < cols; j++)
|
|
573
|
+
if (z[j] < low && open(j)) {
|
|
574
|
+
low = z[j];
|
|
575
|
+
enter = j;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
else
|
|
579
|
+
for (let j = 0; j < cols; j++)
|
|
580
|
+
if (z[j] < -eps && open(j)) {
|
|
581
|
+
enter = j;
|
|
582
|
+
break;
|
|
583
|
+
}
|
|
584
|
+
if (enter < 0)
|
|
585
|
+
return true;
|
|
586
|
+
let leave = -1, best = Infinity;
|
|
587
|
+
for (let r = 0; r < T.length; r++) {
|
|
588
|
+
const a = T[r][enter];
|
|
589
|
+
if (a > eps) {
|
|
590
|
+
const ratio = T[r][cols] / a;
|
|
591
|
+
if (ratio < best - eps || (Math.abs(ratio - best) <= eps && basis[r] < basis[leave])) {
|
|
592
|
+
best = ratio;
|
|
593
|
+
leave = r;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
if (leave < 0)
|
|
598
|
+
return false;
|
|
599
|
+
stalled = best <= eps ? stalled + 1 : 0;
|
|
600
|
+
const p = T[leave], pv = p[enter];
|
|
601
|
+
for (let j = 0; j <= cols; j++)
|
|
602
|
+
p[j] /= pv;
|
|
603
|
+
for (let r = 0; r < T.length; r++)
|
|
604
|
+
if (r !== leave) {
|
|
605
|
+
const f = T[r][enter];
|
|
606
|
+
if (f !== 0) {
|
|
607
|
+
const row = T[r];
|
|
608
|
+
for (let j = 0; j <= cols; j++)
|
|
609
|
+
row[j] -= f * p[j];
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
const f = z[enter];
|
|
613
|
+
if (f !== 0)
|
|
614
|
+
for (let j = 0; j <= cols; j++)
|
|
615
|
+
z[j] -= f * p[j];
|
|
616
|
+
basis[leave] = enter;
|
|
617
|
+
}
|
|
618
|
+
return false;
|
|
619
|
+
};
|
|
620
|
+
if (!run(j => (art(j) ? 1 : 0), () => true))
|
|
621
|
+
return undefined;
|
|
622
|
+
// (no point: the artificials' least sum clearly above zero, which the exact simplex is not asked again)
|
|
623
|
+
if (T.some((row, i) => art(basis[i]) && row[cols] > 1e-6))
|
|
624
|
+
return 'infeasible';
|
|
625
|
+
if (T.some((row, i) => art(basis[i]) && row[cols] > 1e-9))
|
|
626
|
+
return undefined;
|
|
627
|
+
for (let i = T.length - 1; i >= 0; i--) {
|
|
628
|
+
if (!art(basis[i]))
|
|
629
|
+
continue;
|
|
630
|
+
let j = -1;
|
|
631
|
+
for (let c = 0; c < 2 * n + m; c++)
|
|
632
|
+
if (Math.abs(T[i][c]) > eps) {
|
|
633
|
+
j = c;
|
|
634
|
+
break;
|
|
635
|
+
}
|
|
636
|
+
if (j < 0) {
|
|
637
|
+
T.splice(i, 1);
|
|
638
|
+
basis.splice(i, 1);
|
|
639
|
+
continue;
|
|
640
|
+
}
|
|
641
|
+
const p = T[i], pv = p[j];
|
|
642
|
+
for (let c = 0; c <= cols; c++)
|
|
643
|
+
p[c] /= pv;
|
|
644
|
+
for (let r = 0; r < T.length; r++)
|
|
645
|
+
if (r !== i) {
|
|
646
|
+
const f = T[r][j];
|
|
647
|
+
if (f !== 0) {
|
|
648
|
+
const row = T[r];
|
|
649
|
+
for (let c = 0; c <= cols; c++)
|
|
650
|
+
row[c] -= f * p[c];
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
basis[i] = j;
|
|
654
|
+
}
|
|
655
|
+
if (!run(j => (j < 2 * n ? 1 : 0), j => !art(j)))
|
|
656
|
+
return undefined;
|
|
657
|
+
const val = (j) => { const i = basis.indexOf(j); return i < 0 ? 0 : T[i][cols]; };
|
|
658
|
+
const u = xs.map((_, j) => val(j) - val(n + j));
|
|
659
|
+
// read back: tiny values kept or as 0 (a coefficient 1/2^31 against a bound of the int range is one, or the floats'
|
|
660
|
+
// error), each the nearest fraction, the whole scaled to
|
|
661
|
+
// integers, with and without their common divisor; the first that every row holds of exactly
|
|
662
|
+
for (const [snap, most] of [[0, 10_000], [1e-7, 10_000], [1e-7, 1_000_000], [0, 2 ** 32]]) {
|
|
663
|
+
const fr = u.map(x => (Math.abs(x) <= snap ? [0, 1] : nearFraction(Math.abs(x), most)));
|
|
664
|
+
let lcm = 1n;
|
|
665
|
+
for (const [, d] of fr)
|
|
666
|
+
lcm = lcm / bgcd(lcm, BigInt(d)) * BigInt(d);
|
|
667
|
+
const ints = u.map((x, j) => BigInt(Math.sign(x) * fr[j][0]) * (lcm / BigInt(fr[j][1]))), g = ints.reduce((a, b) => bgcd(a, b), 0n) || 1n;
|
|
668
|
+
for (const div of g > 1n ? [g, 1n] : [1n]) {
|
|
669
|
+
const w = {};
|
|
670
|
+
let safe = true;
|
|
671
|
+
for (const [j, x] of xs.entries()) {
|
|
672
|
+
const c = ints[j] / div;
|
|
673
|
+
if (c > BigInt(Number.MAX_SAFE_INTEGER) || -c > BigInt(Number.MAX_SAFE_INTEGER))
|
|
674
|
+
safe = false;
|
|
675
|
+
else if (c)
|
|
676
|
+
w[x] = Number(c);
|
|
677
|
+
}
|
|
678
|
+
if (safe && holdsExactly(rows, w))
|
|
679
|
+
return w;
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
// Else the tiny values as 0 exactly, the LP solved again over the rest: within the tolerance a coefficient of 2^−31
|
|
683
|
+
// against a vertex at 2^31 is a drop of 1 the exact rows don't allow.
|
|
684
|
+
const zero = new Set(xs.filter((_, j) => u[j] !== 0 && Math.abs(u[j]) <= 1e-7));
|
|
685
|
+
if (!again || !zero.size)
|
|
686
|
+
return undefined;
|
|
687
|
+
const fewer = floatSolve(rows.map(r => Object.fromEntries(Object.entries(r).filter(([x]) => !zero.has(x)))), until, equil, false);
|
|
688
|
+
return fewer && fewer !== 'infeasible' && holdsExactly(rows, fewer) ? fewer : undefined;
|
|
689
|
+
}
|
|
690
|
+
// Every row (lin <= 0) at w, in BigInt: products past 2^53 read exactly.
|
|
691
|
+
const holdsExactly = (rows, w) => rows.every(l => {
|
|
692
|
+
let s = 0n;
|
|
693
|
+
for (const x in l)
|
|
694
|
+
s += BigInt(l[x]) * (x === '1' ? 1n : BigInt(w[x] ?? 0));
|
|
695
|
+
return s <= 0n;
|
|
696
|
+
});
|
|
489
697
|
function exactSolve(rows, until) {
|
|
698
|
+
// (the rows scaled to their largest entry first, then as they are: either way the tolerance may hide what a row of a
|
|
699
|
+
// vertex at 2^31 says, a coefficient 2^−31 read as 0 or 0 as 2^−31; infeasible where both say so)
|
|
700
|
+
const t0 = performance.now(), first = floatSolve(rows, until), second = first && first !== 'infeasible' ? first : floatSolve(rows, until, false);
|
|
701
|
+
const fast = second && second !== 'infeasible' ? second : first === 'infeasible' && second === 'infeasible' ? 'infeasible' : undefined;
|
|
702
|
+
if (lpTrace)
|
|
703
|
+
console.error(`[lp] ${rows.length} rows ${new Set(rows.flatMap(l => Object.keys(l))).size} unknowns: ${fast === undefined ? 'undefined' : fast === 'infeasible' ? 'infeasible' : 'found'} ${Math.round(performance.now() - t0)} ms`);
|
|
704
|
+
if (fast === 'infeasible')
|
|
705
|
+
return undefined;
|
|
706
|
+
if (fast)
|
|
707
|
+
return fast;
|
|
708
|
+
// (no point read back, or the two disagree: a refutation over the rationals, Farkas' rows picked by the floats and
|
|
709
|
+
// checked by elimination on them, which the exact simplex below would take far longer to give; else the exact simplex)
|
|
710
|
+
if (refuteFast(rows.map(lin => ({ lin, label: '' })), 2_000, false)?.kind === 'refuted')
|
|
711
|
+
return undefined;
|
|
490
712
|
const xs = [...new Set(rows.flatMap(l => Object.keys(l)).filter(x => x !== '1'))].sort(), n = xs.length, m = rows.length;
|
|
491
713
|
// Columns: p (n), q (n), a slack per row (m), an artificial per row (m); the right-hand side last.
|
|
492
714
|
const cols = 2 * n + 2 * m, T = [], basis = [];
|
|
@@ -494,51 +716,45 @@ function exactSolve(rows, until) {
|
|
|
494
716
|
let rhs = -(l['1'] ?? 0);
|
|
495
717
|
const sign = rhs < 0 ? -1 : 1;
|
|
496
718
|
rhs *= sign;
|
|
497
|
-
const row = Array.from({ length: cols + 1 }, () =>
|
|
719
|
+
const row = Array.from({ length: cols + 1 }, () => 0n);
|
|
498
720
|
for (const [j, x] of xs.entries()) {
|
|
499
721
|
const c = sign * (l[x] ?? 0);
|
|
500
722
|
if (c)
|
|
501
|
-
(row[j] =
|
|
723
|
+
(row[j] = BigInt(c), row[n + j] = BigInt(-c));
|
|
502
724
|
}
|
|
503
|
-
row[2 * n + i] =
|
|
725
|
+
row[2 * n + i] = BigInt(sign);
|
|
504
726
|
if (sign < 0) {
|
|
505
|
-
row[2 * n + m + i] =
|
|
727
|
+
row[2 * n + m + i] = 1n;
|
|
506
728
|
basis.push(2 * n + m + i);
|
|
507
729
|
}
|
|
508
730
|
else
|
|
509
731
|
basis.push(2 * n + i);
|
|
510
|
-
row[cols] =
|
|
511
|
-
T.push(row);
|
|
732
|
+
row[cols] = BigInt(rhs);
|
|
733
|
+
T.push({ n: row, d: 1n });
|
|
512
734
|
}
|
|
513
735
|
const art = (j) => j >= 2 * n + m;
|
|
514
|
-
const phase1 = Array.from({ length: cols }, (_, j) => (art(j) ?
|
|
736
|
+
const phase1 = Array.from({ length: cols }, (_, j) => (art(j) ? 1n : 0n));
|
|
515
737
|
if (!simplex(T, basis, phase1, () => true, until))
|
|
516
738
|
return undefined;
|
|
517
|
-
if (T.some((row, i) => art(basis[i]) && row[cols]
|
|
739
|
+
if (T.some((row, i) => art(basis[i]) && row.n[cols]))
|
|
518
740
|
return undefined; // infeasible
|
|
519
741
|
// An artificial left in the basis at zero: pivoted out on any other column, or its row dropped (redundant).
|
|
520
742
|
for (let i = T.length - 1; i >= 0; i--) {
|
|
521
743
|
if (!art(basis[i]))
|
|
522
744
|
continue;
|
|
523
|
-
const j = T[i].findIndex((x, c) => c < 2 * n + m && x
|
|
745
|
+
const j = T[i].n.findIndex((x, c) => c < 2 * n + m && x !== 0n);
|
|
524
746
|
if (j < 0) {
|
|
525
747
|
T.splice(i, 1);
|
|
526
748
|
basis.splice(i, 1);
|
|
527
749
|
continue;
|
|
528
750
|
}
|
|
529
|
-
|
|
530
|
-
T[i] = T[i].map(x => qdiv(x, p));
|
|
531
|
-
for (const [r, row] of T.entries())
|
|
532
|
-
if (r !== i && row[j].n) {
|
|
533
|
-
const f = row[j];
|
|
534
|
-
T[r] = row.map((x, c) => qsub(x, qmul(f, T[i][c])));
|
|
535
|
-
}
|
|
751
|
+
pivotOn(T, undefined, i, j);
|
|
536
752
|
basis[i] = j;
|
|
537
753
|
}
|
|
538
|
-
const phase2 = Array.from({ length: cols }, (_, j) => (j < 2 * n ?
|
|
754
|
+
const phase2 = Array.from({ length: cols }, (_, j) => (j < 2 * n ? 1n : 0n));
|
|
539
755
|
if (!simplex(T, basis, phase2, j => !art(j), until))
|
|
540
756
|
return undefined;
|
|
541
|
-
const val = (j) => { const i = basis.indexOf(j); return i < 0 ? Q0 : T[i][cols]; };
|
|
757
|
+
const val = (j) => { const i = basis.indexOf(j); return i < 0 ? Q0 : q(T[i].n[cols], T[i].d); };
|
|
542
758
|
const u = xs.map((_, j) => qsub(val(j), val(n + j)));
|
|
543
759
|
let lcm = 1n;
|
|
544
760
|
for (const r of u)
|
|
@@ -552,7 +768,7 @@ function exactSolve(rows, until) {
|
|
|
552
768
|
if (c)
|
|
553
769
|
w[x] = c;
|
|
554
770
|
}
|
|
555
|
-
return rows
|
|
771
|
+
return holdsExactly(rows, w) ? w : undefined;
|
|
556
772
|
}
|
|
557
773
|
// exactSolve over `must` and the pool's rows, the pool's added as they are violated (the most violated first). The
|
|
558
774
|
// solution is found on the smaller half of the runs (each row's size its largest coefficient, a sample's value or a
|
|
@@ -596,7 +812,13 @@ function lexRanks(idx, nodes, samples, edges, name, until, onEdges = false, weak
|
|
|
596
812
|
const ends = onEdges ? edges.flatMap(e => [[e.from, e.s], [e.to, e.t]]) : idx.flatMap(n => samples[n].map(s => [n, s.env]));
|
|
597
813
|
const points = [...new Map(ends.map(([n, env]) => { const l = rankRow(n, nodes, env, name, -1); return [id(l), l]; })).values()];
|
|
598
814
|
const delta = (e) => plus(rankRow(e.to, nodes, e.t, name), rankRow(e.from, nodes, e.s, name, -1));
|
|
599
|
-
|
|
815
|
+
// (a loop, not a spread: tens of thousands of samples' values overflow the stack as arguments)
|
|
816
|
+
let scale = 1;
|
|
817
|
+
for (const n of idx)
|
|
818
|
+
for (const s of samples[n])
|
|
819
|
+
for (const x of Object.values(s.env))
|
|
820
|
+
if (x + 1 > scale)
|
|
821
|
+
scale = x + 1;
|
|
600
822
|
let left = [...new Map(edges.map(e => { const l = delta(e); return [id(l), l]; })).values()];
|
|
601
823
|
let keep = [...new Map(weak.map(e => { const l = delta(e); return [id(l), l]; })).values()].filter(l => !left.some(x => id(x) === id(l)));
|
|
602
824
|
const comps = [];
|
|
@@ -1038,7 +1260,7 @@ export function rankOfPaths(name, rel, until) {
|
|
|
1038
1260
|
return r;
|
|
1039
1261
|
return r.forms ? { forms: r.forms, values: locations(rel).values } : { why: 'a rank by regions' };
|
|
1040
1262
|
}
|
|
1041
|
-
function relationRank(l, rel, until, extra = []) {
|
|
1263
|
+
function relationRank(l, rel, until, extra = [], inv) {
|
|
1042
1264
|
const { values, fromOf, toOf, names, at } = locations(rel);
|
|
1043
1265
|
const every = [...new Set(at.flat())].sort();
|
|
1044
1266
|
const gpaths = [];
|
|
@@ -1057,7 +1279,7 @@ function relationRank(l, rel, until, extra = []) {
|
|
|
1057
1279
|
const unknown = (tp, j, n, x) => (x === 1 ? `b:${tp.tag}${j}:${n}` : `a:${tp.tag}${j}:${tp.own ? n : '*'}:${x}`);
|
|
1058
1280
|
// a node: a location, or one side of an atom at a location (regions, below)
|
|
1059
1281
|
const nodeLoc = values.map((_, n) => n);
|
|
1060
|
-
const varsAt = (tp, n) => (tp.own ? at[nodeLoc[n]] :
|
|
1282
|
+
const varsAt = (tp, n) => (tp.own ? at[nodeLoc[n]] : tp.names);
|
|
1061
1283
|
const fAt = (tp, j, n, x, t) => {
|
|
1062
1284
|
const out = {};
|
|
1063
1285
|
for (const v of varsAt(tp, n))
|
|
@@ -1082,11 +1304,16 @@ function relationRank(l, rel, until, extra = []) {
|
|
|
1082
1304
|
const holds = (p, h, c) => {
|
|
1083
1305
|
const env = (g) => Object.fromEntries(p.gens.names.map((x, i) => [x, g[i]]));
|
|
1084
1306
|
const tOf = (g) => g[p.gens.names.length];
|
|
1085
|
-
|
|
1307
|
+
// (each row once; one that reads no unknown and holds, along a name the template doesn't read, left out)
|
|
1308
|
+
const out = new Map();
|
|
1309
|
+
for (const r of [
|
|
1086
1310
|
...p.gens.vertices.map(g => plusL(scaleL(h(env(g), tOf(g)), -1), { 1: c * tOf(g) })),
|
|
1087
1311
|
...p.gens.rays.map(g => scaleL(h(env(g), 0), -1)),
|
|
1088
1312
|
...p.gens.lines.flatMap(g => [h(env(g), 0), scaleL(h(env(g), 0), -1)]),
|
|
1089
|
-
]
|
|
1313
|
+
])
|
|
1314
|
+
if (Object.keys(r).some(x => x !== '1') || (r['1'] ?? 0) > 0)
|
|
1315
|
+
out.set(id(Object.keys(r).sort().map(x => [x, r[x]])), r);
|
|
1316
|
+
return [...out.values()];
|
|
1090
1317
|
};
|
|
1091
1318
|
const delta = (tp, j, p) => (x, t) => plusL(fAt(tp, j, p.from, x, t), fAt(tp, j, p.to, postAt(p, x, t), t), -1);
|
|
1092
1319
|
const strictRows = (tp, p) => [
|
|
@@ -1095,7 +1322,12 @@ function relationRank(l, rel, until, extra = []) {
|
|
|
1095
1322
|
...holds(p, (x, t) => fAt(tp, tp.depth - 1, p.from, x, t), tp.depth === 1 ? 1 : 0),
|
|
1096
1323
|
];
|
|
1097
1324
|
const weakRows = (tp, p) => Array.from({ length: tp.depth }, (_, j) => holds(p, delta(tp, j, p), 0)).flat();
|
|
1098
|
-
|
|
1325
|
+
let scale = 1;
|
|
1326
|
+
for (const p of gpaths)
|
|
1327
|
+
for (const g of p.gens.vertices)
|
|
1328
|
+
for (const x of g)
|
|
1329
|
+
if (Math.abs(x) + 1 > scale)
|
|
1330
|
+
scale = Math.abs(x) + 1;
|
|
1099
1331
|
const sat = (w, rows) => rows.every(r => valueAt(r, w) <= 0);
|
|
1100
1332
|
// One block over the paths left: strict on all of them at once, else on as many as one after another allows.
|
|
1101
1333
|
const block = (tp, left, greedy, until) => {
|
|
@@ -1134,10 +1366,12 @@ function relationRank(l, rel, until, extra = []) {
|
|
|
1134
1366
|
const ranked = (paths, tag0, until) => {
|
|
1135
1367
|
let left = paths;
|
|
1136
1368
|
const mine = [], sizes = [];
|
|
1137
|
-
|
|
1369
|
+
// a shared template's names: those live where the component's paths begin or end
|
|
1370
|
+
const names = [...new Set(paths.flatMap(p => [...at[nodeLoc[p.from]], ...at[nodeLoc[p.to]]]))].sort();
|
|
1371
|
+
while (left.length && mine.length < 16 && clock() < until) {
|
|
1138
1372
|
const tag = `${tag0}${mine.length}.`;
|
|
1139
1373
|
// depth 1 first, all at once then one path after another; then deeper
|
|
1140
|
-
const order = [1, 2, 3].flatMap(depth => [false, true].flatMap(greedy => [false, true].map((own) => [{ depth, own, tag }, greedy])));
|
|
1374
|
+
const order = [1, 2, 3].flatMap(depth => [false, true].flatMap(greedy => [false, true].map((own) => [{ depth, own, tag, names }, greedy])));
|
|
1141
1375
|
let found;
|
|
1142
1376
|
for (const [tp, greedy] of order) {
|
|
1143
1377
|
if (found || clock() > until)
|
|
@@ -1165,50 +1399,124 @@ function relationRank(l, rel, until, extra = []) {
|
|
|
1165
1399
|
left = left.filter(p => !strict.includes(p));
|
|
1166
1400
|
}
|
|
1167
1401
|
if (left.length)
|
|
1168
|
-
return { why: clock() >= until ? 'out of time on the relation' : 'past
|
|
1402
|
+
return { why: clock() >= until ? 'out of time on the relation' : 'past 16 components' };
|
|
1169
1403
|
return { parts: mine, sizes };
|
|
1170
1404
|
};
|
|
1171
|
-
// Regions: a component with no rank, its locations split by
|
|
1172
|
-
//
|
|
1173
|
-
//
|
|
1405
|
+
// Regions: a component with no rank, its locations split by atoms (one, then two at once): the atoms its paths read
|
|
1406
|
+
// (q < p: min(p, q); y ≥ 2: a sign) and the signs of what a path adds to a value (y := y + x: x ≥ 1). Each sign vector
|
|
1407
|
+
// at a location is a node of its own, the paths split by the signs before and after; the kernel's term reads the atoms
|
|
1408
|
+
// (if atom then f else g). With invariants (`inv`), each node's own first, by Houdini over every path of the relation
|
|
1409
|
+
// split the same way: disjunctive invariants (x ≤ −1 where x ≥ 1 fails, after x := 1 or x := −1), each a law
|
|
1410
|
+
// (pc = ℓ ∧ the signs → atom) the kernel proves inductive with the rest.
|
|
1174
1411
|
const after1 = (p, a) => { let o = {}; for (const [x, c] of Object.entries(a))
|
|
1175
1412
|
o = plusL(o, x !== '1' && p.post[x] ? scaleL(p.post[x], c) : { [x]: c }); return o; };
|
|
1176
1413
|
const side = (a, yes) => (yes ? a : plusL({ 1: 1 }, a, -1));
|
|
1414
|
+
const regionLaws = [], regionTexts = [];
|
|
1177
1415
|
const regions = (c, paths) => {
|
|
1178
|
-
const seen = new Set(),
|
|
1179
|
-
|
|
1180
|
-
if (!Object.keys(r).every(x => x === '1' || names.has(x)) || Object.keys(r).length < 2)
|
|
1181
|
-
continue;
|
|
1416
|
+
const seen = new Set(), guards = [], deltas = [];
|
|
1417
|
+
const add = (r, to) => {
|
|
1182
1418
|
const key = [id(Object.keys(r).sort().map(x => [x, r[x]])), id(Object.keys(side(r, false)).sort().map(x => [x, side(r, false)[x]]))].sort().join();
|
|
1183
1419
|
if (!seen.has(key)) {
|
|
1184
1420
|
seen.add(key);
|
|
1185
|
-
|
|
1421
|
+
to.push(r);
|
|
1186
1422
|
}
|
|
1187
|
-
}
|
|
1188
|
-
|
|
1423
|
+
};
|
|
1424
|
+
for (const r of paths.flatMap(p => p.rows))
|
|
1425
|
+
if (Object.keys(r).every(x => x === '1' || names.has(x)) && Object.keys(r).length >= 2)
|
|
1426
|
+
add(r, guards);
|
|
1427
|
+
for (const p of paths)
|
|
1428
|
+
for (const [x, f] of Object.entries(p.post)) {
|
|
1429
|
+
const d = plusL(f, { [x]: 1 }, -1);
|
|
1430
|
+
if (Object.keys(d).some(y => y !== '1') && Object.keys(d).every(y => y === '1' || every.includes(y)))
|
|
1431
|
+
add(plusL({ 1: 1 }, d, -1), deltas); // d ≥ 1, over live names
|
|
1432
|
+
}
|
|
1433
|
+
guards.sort((a, b) => Object.keys(b).length - Object.keys(a).length);
|
|
1434
|
+
// (the signs of what paths add first: fewer, and each a direction of progress; then the guards' atoms)
|
|
1435
|
+
const singles = [...deltas.slice(0, 4), ...guards.slice(0, 8)], few = singles.slice(0, 4);
|
|
1436
|
+
// (two atoms at once only with the regions' own invariants: without them, the relation's search is the first of
|
|
1437
|
+
// several, and one atom's regions are what it had)
|
|
1438
|
+
const tries = [...singles.map(a => [a]), ...(inv ? few.flatMap((a, i) => few.slice(i + 1).map(b => [a, b])) : [])];
|
|
1439
|
+
if (trace)
|
|
1440
|
+
console.error(`[cycles] ${l.name}: component ${c}: ${tries.length} splits to try`);
|
|
1189
1441
|
const locs = values.flatMap((_, n) => (comp[n] === c ? [n] : []));
|
|
1190
|
-
for (const [ai,
|
|
1442
|
+
for (const [ai, as] of tries.entries()) {
|
|
1191
1443
|
if (clock() > until)
|
|
1192
1444
|
return undefined;
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1445
|
+
// (each try its share of the time left)
|
|
1446
|
+
const mine = clock() + (until - clock()) / Math.max(2, tries.length - ai);
|
|
1447
|
+
// (every location split, not only the component's: a disjunction an invariant needs is often made before it, x := 1
|
|
1448
|
+
// or x := −1 on the way in)
|
|
1449
|
+
const K = as.length, base = nodeLoc.length, node = (n, bits) => base + (n << K) + bits;
|
|
1450
|
+
for (const n of values.keys())
|
|
1451
|
+
for (let b = 0; b < 1 << K; b++)
|
|
1452
|
+
nodeLoc.push(n);
|
|
1453
|
+
const sides = (p, bits, after) => as.map((a, i) => side(after ? after1(p, a) : a, ((bits >> i) & 1) === 0));
|
|
1454
|
+
// (undefined where a path's generators aren't found, past their cap or the time: a path left out is no path ranked)
|
|
1455
|
+
const splitOf = (ps, extra = () => []) => {
|
|
1456
|
+
const out = [];
|
|
1457
|
+
for (const p of ps)
|
|
1458
|
+
for (let b1 = 0; b1 < 1 << K; b1++)
|
|
1459
|
+
for (let b2 = 0; b2 < 1 << K; b2++) {
|
|
1460
|
+
const rows = [...p.rows, ...sides(p, b1, false), ...sides(p, b2, true), ...extra(node(p.from, b1))];
|
|
1461
|
+
// (over the path's names and any an atom reads beside them: a name missing would drop its coefficient)
|
|
1462
|
+
const ns = rows.some(r => Object.keys(r).some(x => x !== '1' && !p.gens.names.includes(x))) ? [...new Set([...p.gens.names, ...rows.flatMap(r => Object.keys(r).filter(x => x !== '1'))])].sort() : p.gens.names;
|
|
1463
|
+
const gens = generators(rows, ns, rel.nats, 600, mine);
|
|
1464
|
+
if (!gens)
|
|
1465
|
+
return undefined;
|
|
1466
|
+
if (gens.vertices.length)
|
|
1467
|
+
out.push({ from: node(p.from, b1), to: node(p.to, b2), gens, post: p.post, rows });
|
|
1468
|
+
}
|
|
1469
|
+
return out;
|
|
1470
|
+
};
|
|
1471
|
+
const conds = as.map(a => leC(rel, a));
|
|
1472
|
+
const term = (fs, n, i = 0, bits = 0) => (i === K ? termOf(rel, fs[node(n, bits)]) : { op: 'if', c: conds[i], then: term(fs, n, i + 1, bits), else: term(fs, n, i + 1, bits | (1 << i)) });
|
|
1473
|
+
const answer = (out) => ({ parts: out.parts.map(fs => values.map((_, n) => (locs.includes(n) ? term(fs, n) : k(0)))), sizes: out.sizes });
|
|
1474
|
+
// (with invariants to find, the split is ranked once, with its regions' own: more rows, no fewer ranks)
|
|
1475
|
+
if (!inv) {
|
|
1476
|
+
const split = splitOf(paths);
|
|
1477
|
+
if (!split)
|
|
1478
|
+
continue;
|
|
1479
|
+
const out = ranked(split, `${c}.r${ai}.`, mine);
|
|
1480
|
+
if (trace)
|
|
1481
|
+
console.error(`[cycles] ${l.name}: component ${c} split by ${conds.map(showC).join(' and ')}: ${'parts' in out ? out.parts.length + ' parts' : out.why}`);
|
|
1482
|
+
if ('parts' in out)
|
|
1483
|
+
return answer(out);
|
|
1484
|
+
continue;
|
|
1211
1485
|
}
|
|
1486
|
+
// the nodes' invariants: Houdini over every path split, each node's candidates its location's, those false at a
|
|
1487
|
+
// start in its region dropped
|
|
1488
|
+
const N = values.length << K;
|
|
1489
|
+
const split0 = splitOf(gpaths);
|
|
1490
|
+
if (!split0)
|
|
1491
|
+
continue;
|
|
1492
|
+
const cands = Array.from({ length: N }, (_, j) => {
|
|
1493
|
+
const n = j >> K, bits = j & ((1 << K) - 1);
|
|
1494
|
+
const here = inv.starts.filter(([m, env]) => m === n && as.every((a, i) => valueAt(side(a, ((bits >> i) & 1) === 0), env) <= 0));
|
|
1495
|
+
return (inv.cands[n] ?? []).filter(h => here.every(([, env]) => valueAt(h, env) <= 0));
|
|
1496
|
+
});
|
|
1497
|
+
const relSplit = { ...rel, paths: split0.map(p => ({ rows: p.rows, pins: {}, post: p.post })) };
|
|
1498
|
+
const H = houdini(relSplit, cands, i => [split0[i].from - base, split0[i].to - base], mine);
|
|
1499
|
+
if (trace && !H)
|
|
1500
|
+
console.error(`[cycles] ${l.name}: component ${c} split by ${conds.map(showC).join(' and ')}: Houdini out of time`);
|
|
1501
|
+
if (!H)
|
|
1502
|
+
continue;
|
|
1503
|
+
const Ht = H.map(inv.tightest);
|
|
1504
|
+
const split = splitOf(paths, nd => Ht[nd - base] ?? []);
|
|
1505
|
+
if (!split)
|
|
1506
|
+
continue;
|
|
1507
|
+
const out = ranked(split, `${c}.h${ai}.`, mine);
|
|
1508
|
+
if (trace)
|
|
1509
|
+
console.error(`[cycles] ${l.name}: component ${c} split by ${conds.map(showC).join(' and ')}, with ${Ht.flat().length} invariants of the regions: ${'parts' in out ? out.parts.length + ' parts' : out.why}`);
|
|
1510
|
+
if (!('parts' in out))
|
|
1511
|
+
continue;
|
|
1512
|
+
for (let j = 0; j < N; j++)
|
|
1513
|
+
for (const h of Ht[j]) {
|
|
1514
|
+
const n = j >> K, bits = j & ((1 << K) - 1), atom = leC(rel, h);
|
|
1515
|
+
const not = [...(rel.loc ? [eqC(v(rel.loc), k(values[n]))] : []), ...as.map((a, i) => leC(rel, side(a, ((bits >> i) & 1) === 0)))];
|
|
1516
|
+
regionLaws.push({ name: `region invariant ${c}.${j}.${regionLaws.length}`, holds: { op: 'or', xs: [...not.map((x) => ({ op: 'not', x })), atom] } });
|
|
1517
|
+
regionTexts.push(`${not.map(showC).join(' ∧ ')} → ${showC(atom)}`);
|
|
1518
|
+
}
|
|
1519
|
+
return answer(out);
|
|
1212
1520
|
}
|
|
1213
1521
|
return undefined;
|
|
1214
1522
|
};
|
|
@@ -1247,7 +1555,7 @@ function relationRank(l, rel, until, extra = []) {
|
|
|
1247
1555
|
const linear = [...new Set(comp)].every(c => lins[c] || !parts[c]);
|
|
1248
1556
|
const forms = linear ? [...(gpaths.some(p => comp[p.from] !== comp[p.to]) ? [values.map((_, n) => konstL(heightOf(comp[n])))] : []),
|
|
1249
1557
|
...Array.from({ length: width }, (_, j) => values.map((_, n) => lins[comp[n]]?.[j]?.[n] ?? {}))] : undefined;
|
|
1250
|
-
return { rank, text: `(${rank.map(showE).join(', ')})`, blocks: sizes, ...(forms ? { forms } : {}) };
|
|
1558
|
+
return { rank, text: `(${rank.map(showE).join(', ')})`, blocks: sizes, laws: regionLaws, texts: regionTexts, ...(forms ? { forms } : {}) };
|
|
1251
1559
|
}
|
|
1252
1560
|
// Invariants first, so a rank may assume them: atoms per location (each name's bounds at the constants the paths
|
|
1253
1561
|
// read, and the paths' own atoms over the state and params), those false at a start dropped (the loop's starts, run
|
|
@@ -1297,8 +1605,20 @@ export function rankWithInvariants(l, laws, until) {
|
|
|
1297
1605
|
const K = [...consts].sort((a, b) => Math.abs(a) - Math.abs(b) || a - b).slice(0, 24);
|
|
1298
1606
|
const atoms = [...new Map(rel.paths.flatMap(p => p.rows).filter(r => Object.keys(r).filter(x => x !== '1').length > 1 && Object.keys(r).every(x => x === '1' || names.has(x))).map(r => [id(Object.keys(r).sort().map(x => [x, r[x]])), r])).values()];
|
|
1299
1607
|
// per location, over the names live there: their bounds at the constants, then (a second try) the paths' atoms too
|
|
1300
|
-
|
|
1301
|
-
const
|
|
1608
|
+
// (and 1 ≤ 0: the location unreachable, where every path into it leaves from states the rest rule out)
|
|
1609
|
+
const bounds = values.map((_, n) => [{ 1: 1 }, ...at[n].flatMap(x => K.flatMap((c) => [plusL({ [x]: 1 }, konstL(-c)), plusL({ [x]: -1 }, konstL(c))]))]);
|
|
1610
|
+
// and what a path's assignments leave where it goes: x := y + c with y kept, x − y = c there (KoAT2's 2 + X₄ = X₁)
|
|
1611
|
+
const copies = values.map(() => []);
|
|
1612
|
+
for (const [i, p] of rel.paths.entries())
|
|
1613
|
+
for (const [x, f] of Object.entries(p.post)) {
|
|
1614
|
+
const ys = Object.keys(f).filter(y => y !== '1');
|
|
1615
|
+
if (ys.length !== 1 || f[ys[0]] !== 1 || ys[0] === x || !names.has(ys[0]))
|
|
1616
|
+
continue;
|
|
1617
|
+
const y = ys[0], kept = p.post[y] ? id(p.post[y]) === id({ [y]: 1 }) : true;
|
|
1618
|
+
if (kept)
|
|
1619
|
+
copies[toOf(i)].push(plusL({ [x]: 1, [y]: -1 }, konstL(-(f['1'] ?? 0))), plusL({ [x]: -1, [y]: 1 }, konstL(f['1'] ?? 0)));
|
|
1620
|
+
}
|
|
1621
|
+
const withAtoms = values.map((_, n) => [...bounds[n], ...atoms.filter(a => Object.keys(a).every(x => x === '1' || at[n].includes(x))), ...copies[n].filter(a => Object.keys(a).every(x => x === '1' || at[n].includes(x)))]);
|
|
1302
1622
|
// the starts: random params (the precondition's), the state they begin in
|
|
1303
1623
|
const cx = { eqs: new Map(), arity: new Map(), values: {}, memo: new Map(), fuel: Infinity, depth: 0 }, rand = seeded(31);
|
|
1304
1624
|
const starts = [];
|
|
@@ -1327,6 +1647,10 @@ export function rankWithInvariants(l, laws, until) {
|
|
|
1327
1647
|
};
|
|
1328
1648
|
// of the bounds on one name each way, the tightest (the others follow from it: the same set)
|
|
1329
1649
|
const tightest = (hs) => {
|
|
1650
|
+
// (a location no run reaches: 1 ≤ 0 alone)
|
|
1651
|
+
const never = hs.find(c => Object.keys(c).every(x => x === '1') && (c['1'] ?? 0) > 0);
|
|
1652
|
+
if (never)
|
|
1653
|
+
return [never];
|
|
1330
1654
|
const best = new Map(), rest = [];
|
|
1331
1655
|
for (const c of hs) {
|
|
1332
1656
|
const xs = Object.keys(c).filter(x => x !== '1');
|
|
@@ -1346,10 +1670,15 @@ export function rankWithInvariants(l, laws, until) {
|
|
|
1346
1670
|
if (clock() > until)
|
|
1347
1671
|
break;
|
|
1348
1672
|
const closed = houdini(rel, atStarts(cands), locOf, i ? until : clock() + (until - clock()) / 2);
|
|
1349
|
-
if (
|
|
1673
|
+
if (trace)
|
|
1674
|
+
console.error(`[cycles] ${l.name}: houdini round ${i}: ${closed ? closed.map(h => h.map(x => showC(leC(rel, x))).join(" ∧ ")).join(" | ") : "none"}`);
|
|
1675
|
+
if (!closed)
|
|
1676
|
+
continue;
|
|
1677
|
+
// (with none at any location, the regions' own may still be found: the second try only)
|
|
1678
|
+
if (!closed.some(h => h.length) && !i)
|
|
1350
1679
|
continue;
|
|
1351
1680
|
H = closed.map(tightest);
|
|
1352
|
-
found = relationRank(l, rel, until, H);
|
|
1681
|
+
found = relationRank(l, rel, i ? until : clock() + (until - clock()) / 2, H, { cands: atStarts(cands), starts, tightest });
|
|
1353
1682
|
if ('rank' in found)
|
|
1354
1683
|
break;
|
|
1355
1684
|
found = { why: `with ${H.flat().length} invariants: ${found.why}` };
|
|
@@ -1362,6 +1691,7 @@ export function rankWithInvariants(l, laws, until) {
|
|
|
1362
1691
|
const fewer = houdini(rel, H.map((g, n) => g.filter(h => !drop(n, h))), locOf, stop);
|
|
1363
1692
|
if (!fewer || fewer.flat().length === H.flat().length)
|
|
1364
1693
|
return false;
|
|
1694
|
+
// (no regions' own invariants here: they would only find again the ones dropped)
|
|
1365
1695
|
const r = relationRank(l, rel, stop, fewer);
|
|
1366
1696
|
if ('rank' in r) {
|
|
1367
1697
|
H = fewer;
|
|
@@ -1383,7 +1713,8 @@ export function rankWithInvariants(l, laws, until) {
|
|
|
1383
1713
|
if (H[n].includes(h))
|
|
1384
1714
|
without((m, g) => m === n && g === h);
|
|
1385
1715
|
}
|
|
1386
|
-
|
|
1716
|
+
const at0 = asLaws(H);
|
|
1717
|
+
return { rank: found.rank, text: found.text, laws: [...at0.laws, ...found.laws], texts: [...at0.texts, ...found.texts] };
|
|
1387
1718
|
}
|
|
1388
1719
|
const seeded = (seed) => { let x = seed; return (n) => { x = (x * 1103515245 + 12345) % 2 ** 31; return Math.floor(x / 2 ** 31 * n); }; };
|
|
1389
1720
|
const goalsProven = (goals) => {
|
|
@@ -1484,7 +1815,7 @@ export function findRank(l, laws = [], opts = {}) {
|
|
|
1484
1815
|
if ('trees' in tried[0].verdict)
|
|
1485
1816
|
return done(linear, tried[0].verdict.trees);
|
|
1486
1817
|
}
|
|
1487
|
-
// The relation's rank (per location, in phases), within
|
|
1818
|
+
// The relation's rank (per location, in phases), within a third of the time left; then degree 2 over the products the loop
|
|
1488
1819
|
// reads; then the relation's rank with invariants. (A search that throws, a stack too deep for a large loop say,
|
|
1489
1820
|
// gives way to the rest.)
|
|
1490
1821
|
const safely = (f) => { try {
|
|
@@ -1493,16 +1824,16 @@ export function findRank(l, laws = [], opts = {}) {
|
|
|
1493
1824
|
catch (e) {
|
|
1494
1825
|
return { why: `the relation's search threw: ${e.message.slice(0, 120)}` };
|
|
1495
1826
|
} };
|
|
1496
|
-
const relT0 = clock(), rel = safely(() => rankOfRelation(l, laws, clock() + (until - clock()) /
|
|
1827
|
+
const relT0 = clock(), rel = safely(() => rankOfRelation(l, laws, clock() + (until - clock()) / 3));
|
|
1497
1828
|
let relWhy = 'rank' in rel ? `the kernel refused ${rel.text}` : rel.why;
|
|
1498
1829
|
if (trace)
|
|
1499
1830
|
console.error(`[cycles] ${l.name}: the relation's rank ${'rank' in rel ? rel.text : rel.why} (from ${Math.round(relT0 - t0)} ms to ${Math.round(clock() - t0)} ms)`);
|
|
1500
1831
|
if ('rank' in rel) {
|
|
1501
|
-
const verdict = withDeadline(until, () => goalsProven(loopObligations(l, laws, rel.rank)));
|
|
1832
|
+
const k0 = performance.now(), verdict = withDeadline(until, () => goalsProven(loopObligations(l, laws, rel.rank)));
|
|
1833
|
+
if (trace)
|
|
1834
|
+
console.error(`[cycles] ${l.name}: the kernel on the relation's rank, ${Math.round(performance.now() - k0)} ms: ${'trees' in verdict ? 'proven' : `${verdict.goal} ${verdict.stuck}`}`);
|
|
1502
1835
|
if ('trees' in verdict)
|
|
1503
1836
|
return done(rel.rank, verdict.trees);
|
|
1504
|
-
if (trace)
|
|
1505
|
-
console.error(`[cycles] ${l.name}: the kernel refused ${rel.text} at ${verdict.goal}`);
|
|
1506
1837
|
relWhy = `the kernel refused ${rel.text.slice(0, 300)} at ${verdict.goal}`;
|
|
1507
1838
|
}
|
|
1508
1839
|
tried.push(...quadratic(read, until));
|