gdp-wired 0.26.0 → 0.28.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 +24 -0
- package/dist/core/accel.js +557 -207
- package/dist/core/bmc.d.ts +4 -1
- package/dist/core/bmc.js +18 -15
- package/dist/core/cycles.d.ts +2 -0
- package/dist/core/cycles.js +1 -1
- package/dist/core/cyclic.js +17 -10
- package/dist/core/data.d.ts +1 -0
- package/dist/core/data.js +73 -18
- package/dist/core/discover.js +64 -25
- package/dist/core/dsl.js +108 -18
- package/dist/core/extract-check.d.ts +52 -2
- package/dist/core/extract-check.js +155 -104
- package/dist/core/extract-read.d.ts +25 -0
- package/dist/core/extract-read.js +55 -14
- package/dist/core/extract-shapes.js +94 -18
- package/dist/core/extract-xstate.js +16 -3
- package/dist/core/extract.d.ts +1 -0
- package/dist/core/extract.js +16 -8
- package/dist/core/fresh.d.ts +9 -0
- package/dist/core/fresh.js +91 -0
- package/dist/core/infer.d.ts +1 -0
- package/dist/core/infer.js +64 -46
- package/dist/core/live.d.ts +15 -0
- package/dist/core/live.js +317 -0
- package/dist/core/machine.js +0 -1
- package/dist/core/nonterm.d.ts +3 -1
- package/dist/core/nonterm.js +103 -25
- package/dist/core/solver.d.ts +21 -0
- package/dist/core/solver.js +8 -0
- package/dist/core/translate.js +69 -19
- package/dist/kernel/cdcl.js +212 -78
- package/dist/kernel/kernel.d.ts +17 -1
- package/dist/kernel/kernel.js +158 -2
- package/dist/kernel/prove.d.ts +30 -4
- package/dist/kernel/prove.js +119 -25
- package/dist/kernel/reader.d.ts +74 -5
- package/dist/kernel/reader.js +531 -75
- package/dist/kernel/simplex.d.ts +26 -1
- package/dist/kernel/simplex.js +148 -14
- package/dist/lean-kernel/emit.d.ts +15 -3
- package/dist/lean-kernel/emit.js +144 -12
- package/dist/lean-kernel/library.js +1 -1
- package/package.json +2 -2
package/dist/core/accel.js
CHANGED
|
@@ -7,27 +7,188 @@
|
|
|
7
7
|
// graph; its closed walks (a location may come twice: x > 0 and x < 0 taken in turn), each by its least rotation, by
|
|
8
8
|
// how many choices they make among a location's paths (at most `maxLen`; a chain of locations with one path out, as a
|
|
9
9
|
// translated program's straight-line code, costs nothing) and at most 64 turns, a walk cut where its rows (each turn's
|
|
10
|
-
// inputs left open) are refuted; a self-loop also twice over (a period of two: x := −x).
|
|
11
|
-
//
|
|
10
|
+
// inputs left open) are refuted; a self-loop also twice over (a period of two: x := −x). An inner loop (a self-loop,
|
|
11
|
+
// or a cycle of two turns, whose turn is a translation with a metering function: `accelerate`) is one more choice at
|
|
12
|
+
// its location, taken to its end at once: its count the metering function's value, linear in the state. Then the
|
|
13
|
+
// cycles the random runs take, between two visits of a location, the commonest first.
|
|
12
14
|
// - A turn's inputs as terms of the state before it: a value a row pins (an equation in it), else a bound a row puts on
|
|
13
|
-
// it (x < i0 gives i0 := x + 1, i0 >= 3 gives 3), else 0; or constants from a model of two rounds of the cycle
|
|
14
|
-
// cycle is then one linear loop: its guard G (each turn's
|
|
15
|
-
// update U (affine in the state at the cycle's start).
|
|
16
|
-
// - Its n-fold run in closed form: an atom g ≤ 0 of G holds after every number n of turns where its
|
|
17
|
-
// run, g(Uⁿx), a polynomial in n for a translation or a triangular update
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
// - The set of the loop: per
|
|
24
|
-
// are allowed and end in R (R_j, the weakest precondition: the rest composed
|
|
25
|
-
// where the cycle passes a location twice, its commonest value with no condition.
|
|
26
|
-
// -
|
|
27
|
-
//
|
|
15
|
+
// it (x < i0 gives i0 := x + 1, i0 >= 3 gives 3), else 0; or constants from a model of two rounds of the cycle; or a
|
|
16
|
+
// bound the rest of the cycle puts on it (`lookahead`). The cycle is then one linear loop: its guard G (each turn's
|
|
17
|
+
// rows, read at the state the turns before it leave) and its update U (affine in the state at the cycle's start).
|
|
18
|
+
// - Its n-fold run in closed form (the engine below): an atom g ≤ 0 of G holds after every number n of turns where its
|
|
19
|
+
// value along the run, g(Uⁿx), a polynomial in n for a translation or a triangular update, has its differences at
|
|
20
|
+
// n = 0 all ≤ 0: Δ⁰g = g, Δ^{k+1}g = Δ^k g∘U − Δ^k g, each linear in x (Σ C(n, k)·Δ^k g is g(Uⁿx)). For a geometric
|
|
21
|
+
// update (x := 2x) the differences repeat (Δ²g = Δ¹g) and the same set holds. So the set R is G with each atom's
|
|
22
|
+
// differences up to degree 3, and Houdini drops a difference that isn't kept by U (G's atoms are kept or the cycle is
|
|
23
|
+
// given up): R ∧ ¬a(Ux) refuted for each atom a, by the integer solver (core/solver.ts). A constant difference above 0
|
|
24
|
+
// gives the cycle up at once: g grows past 0.
|
|
25
|
+
// - The set of the loop: per step j of the cycle, at its location, the states from which the rest of the cycle's steps
|
|
26
|
+
// are allowed and end in R (R_j, the weakest precondition: the rest composed; an inner loop's at each of its turns);
|
|
27
|
+
// the witness by location, or by R_j where the cycle passes a location twice, its commonest value with no condition.
|
|
28
|
+
// - Products with a turn's input (x·u, which the relation leaves out): the loop's cases with x fixed (`fixedCases`), the
|
|
29
|
+
// set holding x so.
|
|
30
|
+
// - Stems (`stemsTo`): from the start's location to a part's, a path at a time or a self-loop with a closed form taken
|
|
31
|
+
// many times at once (a translation n times, any other 2 to 16), all linear, then run on the loop itself; `deepen`
|
|
32
|
+
// searches again at twice and four times the depth.
|
|
28
33
|
import { n as K, x as V } from "./machine.js";
|
|
29
34
|
import { relationOf } from "./relation.js";
|
|
30
35
|
import { canon, gcd, konst, linOfE, plus, solve, subst, times, vars } from "./solver.js";
|
|
36
|
+
import { Q0, Q1, bgcd, q, qadd, qdiv, qmul, qsub } from "./q.js";
|
|
37
|
+
const qv = (l) => new Map(Object.entries(l).filter(([, c]) => c).map(([x, c]) => [x, q(c)]));
|
|
38
|
+
const qvAdd = (a, b, f) => { const o = new Map(a); for (const [x, c] of b) {
|
|
39
|
+
const s = qadd(o.get(x) ?? Q0, qmul(c, f));
|
|
40
|
+
if (s.num)
|
|
41
|
+
o.set(x, s);
|
|
42
|
+
else
|
|
43
|
+
o.delete(x);
|
|
44
|
+
} return o; };
|
|
45
|
+
// The unknowns of A·u = B (each right-hand side a form), by elimination over the rationals; undefined where they aren't
|
|
46
|
+
// one solution.
|
|
47
|
+
function solveQ(A, B, m) {
|
|
48
|
+
const rows = A.map((r, i) => ({ a: [...r], b: B[i] })), piv = [];
|
|
49
|
+
let at = 0;
|
|
50
|
+
for (let j = 0; j < m; j++) {
|
|
51
|
+
const p = rows.findIndex((r, i) => i >= at && r.a[j].num !== 0n);
|
|
52
|
+
if (p < 0)
|
|
53
|
+
return undefined;
|
|
54
|
+
const r = rows[p];
|
|
55
|
+
rows[p] = rows[at];
|
|
56
|
+
rows[at] = r;
|
|
57
|
+
const inv = qdiv(Q1, r.a[j]);
|
|
58
|
+
r.a = r.a.map(x => qmul(x, inv));
|
|
59
|
+
r.b = qvAdd(new Map(), r.b, inv);
|
|
60
|
+
for (const [i, o] of rows.entries())
|
|
61
|
+
if (i !== at && o.a[j].num !== 0n) {
|
|
62
|
+
const f = o.a[j];
|
|
63
|
+
o.a = o.a.map((x, c) => qsub(x, qmul(f, r.a[c])));
|
|
64
|
+
o.b = qvAdd(o.b, r.b, qsub(Q0, f));
|
|
65
|
+
}
|
|
66
|
+
piv.push(at++);
|
|
67
|
+
}
|
|
68
|
+
return rows.slice(at).every(r => r.b.size === 0) ? piv.map(i => rows[i].b) : undefined;
|
|
69
|
+
}
|
|
70
|
+
const choose = (n, k) => { let c = 1n; for (let i = 0; i < k; i++)
|
|
71
|
+
c = c * BigInt(n - i) / BigInt(i + 1); return c; };
|
|
72
|
+
const basis = (r, k, n) => (r === 0 ? (n === k ? Q1 : Q0) : q(choose(n, k) * BigInt(r) ** BigInt(n)));
|
|
73
|
+
// The integer roots of Σ P[i]·tⁱ to their multiplicities, all of them, or undefined.
|
|
74
|
+
function intRoots(P) {
|
|
75
|
+
let p = [...P];
|
|
76
|
+
const out = [];
|
|
77
|
+
const take = (r) => {
|
|
78
|
+
const n = p.length - 1, b = new Array(n);
|
|
79
|
+
let acc = 0n;
|
|
80
|
+
for (let i = n; i >= 1; i--) {
|
|
81
|
+
acc = acc * r + p[i];
|
|
82
|
+
b[i - 1] = acc;
|
|
83
|
+
}
|
|
84
|
+
if (acc * r + p[0] !== 0n)
|
|
85
|
+
return false;
|
|
86
|
+
p = b;
|
|
87
|
+
const e = out.find(x => x.r === Number(r));
|
|
88
|
+
if (e)
|
|
89
|
+
e.mult++;
|
|
90
|
+
else
|
|
91
|
+
out.push({ r: Number(r), mult: 1 });
|
|
92
|
+
return true;
|
|
93
|
+
};
|
|
94
|
+
while (p.length > 1 && p[0] === 0n)
|
|
95
|
+
take(0n);
|
|
96
|
+
const c = p[0] < 0n ? -p[0] : p[0];
|
|
97
|
+
if (p.length > 1 && c > 10n ** 12n)
|
|
98
|
+
return undefined;
|
|
99
|
+
for (let d = 1n; p.length > 1 && d * d <= c; d++) {
|
|
100
|
+
if (c % d)
|
|
101
|
+
continue;
|
|
102
|
+
for (const r of [d, -d, c / d, -c / d])
|
|
103
|
+
while (p.length > 1 && take(r))
|
|
104
|
+
;
|
|
105
|
+
}
|
|
106
|
+
return p.length === 1 && out.every(x => Math.abs(x.r) <= 2 ** 20) ? out : undefined;
|
|
107
|
+
}
|
|
108
|
+
export function closedForm(terms, exact, order = 6) {
|
|
109
|
+
const vs = terms.map(qv);
|
|
110
|
+
if (vs.every(v => v.size === 0))
|
|
111
|
+
return { terms: [], den: 1 };
|
|
112
|
+
for (let m = 1; m <= order; m++) {
|
|
113
|
+
const windows = exact ? 1 : vs.length - 2 * m;
|
|
114
|
+
if (vs.length < m + 1 || windows < 1)
|
|
115
|
+
return undefined;
|
|
116
|
+
// v[n + m] = Σ a_i·v[n + i], each coordinate of each window an equation
|
|
117
|
+
const A = [], B = [];
|
|
118
|
+
for (let n = 0; n < (exact ? 1 : vs.length - m); n++)
|
|
119
|
+
for (const x of new Set(vs.slice(n, n + m + 1).flatMap(v => [...v.keys()]))) {
|
|
120
|
+
A.push(vs.slice(n, n + m).map(v => v.get(x) ?? Q0));
|
|
121
|
+
B.push(new Map([['1', vs[n + m].get(x) ?? Q0]]));
|
|
122
|
+
}
|
|
123
|
+
const a = solveQ(A, B.map(b => new Map([...b].filter(([, c]) => c.num))), m);
|
|
124
|
+
if (!a)
|
|
125
|
+
continue;
|
|
126
|
+
// tᵐ − Σ a_i·tⁱ over the integers
|
|
127
|
+
const as = a.map(f => f.get('1') ?? Q0), L = as.reduce((l, x) => l / bgcd(l, x.den) * x.den, 1n);
|
|
128
|
+
const roots = intRoots([...as.map(x => -x.num * (L / x.den)), L]);
|
|
129
|
+
if (!roots)
|
|
130
|
+
return undefined;
|
|
131
|
+
const bs = roots.flatMap(({ r, mult }) => Array.from({ length: mult }, (_, k) => ({ r, k })));
|
|
132
|
+
const c = solveQ(Array.from({ length: m }, (_, n) => bs.map(b => basis(b.r, b.k, n))), vs.slice(0, m), m);
|
|
133
|
+
if (!c)
|
|
134
|
+
return undefined;
|
|
135
|
+
let den = 1n;
|
|
136
|
+
for (const f of c)
|
|
137
|
+
for (const x of f.values())
|
|
138
|
+
den = den / bgcd(den, x.den) * x.den;
|
|
139
|
+
const out = [];
|
|
140
|
+
for (const [j, f] of c.entries()) {
|
|
141
|
+
const l = {};
|
|
142
|
+
for (const [x, y] of f) {
|
|
143
|
+
const v = Number(y.num * (den / y.den));
|
|
144
|
+
if (!Number.isSafeInteger(v))
|
|
145
|
+
return undefined;
|
|
146
|
+
l[x] = v;
|
|
147
|
+
}
|
|
148
|
+
if (Object.keys(l).length)
|
|
149
|
+
out.push({ ...bs[j], c: l });
|
|
150
|
+
}
|
|
151
|
+
return Number.isSafeInteger(Number(den)) ? { terms: out, den: Number(den) } : undefined;
|
|
152
|
+
}
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
// g, g∘U, g∘U², … (count of them)
|
|
156
|
+
export const iterates = (g, U, count) => { const out = [g]; while (out.length < count)
|
|
157
|
+
out.push(subst(out.at(-1), U)); return out; };
|
|
158
|
+
// g(Uⁿx) in closed form
|
|
159
|
+
export const nfold = (g, U, order = 6) => closedForm(iterates(g, U, order + 1), true, order);
|
|
160
|
+
// A closed form that is a polynomial in n: its coefficients by k in the binomial basis C(n, k) (over den), or undefined.
|
|
161
|
+
export const polyOf = (f) => {
|
|
162
|
+
if (f.terms.some(t => t.r !== 1))
|
|
163
|
+
return undefined;
|
|
164
|
+
const out = [];
|
|
165
|
+
for (const t of f.terms)
|
|
166
|
+
out[t.k] = t.c;
|
|
167
|
+
return Array.from({ length: out.length }, (_, k) => out[k] ?? {});
|
|
168
|
+
};
|
|
169
|
+
// A closed form at a count n: a linear form (over the rationals where den doesn't divide it; a form of integer iterates
|
|
170
|
+
// is integral).
|
|
171
|
+
export const closedAt = (f, n) => {
|
|
172
|
+
let o = {};
|
|
173
|
+
for (const t of f.terms)
|
|
174
|
+
o = plus(o, times(t.c, Number(basis(t.r, t.k, n).num)));
|
|
175
|
+
return times(o, 1 / f.den);
|
|
176
|
+
};
|
|
177
|
+
export function accelerate(G, U, S) {
|
|
178
|
+
const delta = {};
|
|
179
|
+
for (const x of S) {
|
|
180
|
+
const f = nfold({ [x]: 1 }, U, 2), p = f && polyOf(f);
|
|
181
|
+
if (!p || p.length > 2 || f.den !== 1 || vars(p[1] ?? {}).length)
|
|
182
|
+
return undefined;
|
|
183
|
+
if (p[1]?.['1'])
|
|
184
|
+
delta[x] = p[1]['1'];
|
|
185
|
+
}
|
|
186
|
+
if (!Object.keys(delta).length)
|
|
187
|
+
return undefined;
|
|
188
|
+
const rise = (a) => vars(a).reduce((s, x) => s + a[x] * (delta[x] ?? 0), 0);
|
|
189
|
+
const m = G.find(a => rise(a) === 1);
|
|
190
|
+
return { delta, ...(m ? { meter: plus(konst(1), m, -1) } : {}) };
|
|
191
|
+
}
|
|
31
192
|
const isConst = (l) => vars(l).length === 0;
|
|
32
193
|
// (a row's coefficients divided by their common divisor, its constant rounded up: an integer row as tight as it goes)
|
|
33
194
|
const tight = (l) => {
|
|
@@ -46,126 +207,144 @@ const big = (l) => Object.values(l).some(c => !Number.isSafeInteger(c) || Math.a
|
|
|
46
207
|
// rows lin <= 0 satisfiable over the integers (unknown counts as satisfiable: the kernel decides later)
|
|
47
208
|
const sat = (rows) => solve(rows.map(lin => ({ lin, label: '' })), 3000).kind !== 'refuted';
|
|
48
209
|
const implied = (rows, a) => solve([...rows, plus(times(a, -1), konst(1))].map(lin => ({ lin, label: '' })), 3000).kind === 'refuted';
|
|
49
|
-
// The candidates, one at a time (a generator: the caller tries each against the kernel and stops when one is proven)
|
|
210
|
+
// The candidates, one at a time (a generator: the caller tries each against the kernel and stops when one is proven):
|
|
211
|
+
// the relation's cycles, then those of its cases where a state value that a product reads with a turn's input is fixed
|
|
212
|
+
// (`fixedCases`), which have 30% of the time where there are any.
|
|
50
213
|
export function* accelerated(l, opts) {
|
|
51
|
-
const rel = relOf(l);
|
|
52
|
-
if (
|
|
53
|
-
return;
|
|
54
|
-
const
|
|
55
|
-
|
|
214
|
+
const rel = relOf(l), fixed = fixedCases(l), { now } = opts, t0 = now();
|
|
215
|
+
if (rel)
|
|
216
|
+
yield* cyclesOf(l, rel, fixed.length ? { ...opts, get until() { return t0 + (opts.until - t0) * 0.7; } } : opts, []);
|
|
217
|
+
for (const f of fixed) {
|
|
218
|
+
if (now() > opts.until)
|
|
219
|
+
return;
|
|
220
|
+
yield* cyclesOf(l, f.rel, { now, get until() { return opts.until; }, ...(opts.maxLen ? { maxLen: opts.maxLen } : {}) }, f.pins);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
// (`pins`: rows every part of the set keeps, the fixed values of a case)
|
|
224
|
+
function* cyclesOf(l, rel, opts, pins) {
|
|
225
|
+
const { now } = opts, maxLen = opts.maxLen ?? 6;
|
|
226
|
+
const S = rel.state, P = new Set(rel.params), Sset = new Set(S), NP = rel.paths.length;
|
|
56
227
|
const natState = S.filter(x => !rel.ints.has(x));
|
|
57
|
-
const from = (i) => (rel.loc ? rel.from[i] : 0), to = (i) => (rel.loc ? rel.to[i] : 0);
|
|
58
228
|
// Each path's inputs as terms of the state before it (a few choices each, the first the likeliest).
|
|
59
229
|
const choices = rel.paths.map(p => inputChoices(p, Sset, P));
|
|
230
|
+
const from = (i) => (i >= NP ? loops[i - NP].loc : rel.loc ? rel.from[i] : 0), to = (i) => (i >= NP ? loops[i - NP].loc : rel.loc ? rel.to[i] : 0);
|
|
231
|
+
const loops = [];
|
|
60
232
|
// The cycles: closed walks of paths (a location may come twice: two turns at one location, taken in turn), each once
|
|
61
233
|
// (its least rotation), with at most `maxLen` choices among a location's paths (a chain of locations with one path out,
|
|
62
234
|
// as a translated program's straight-line code, is free) and at most 64 turns; the shortest first.
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const costAt = (loc) => ((byFrom.get(loc) ?? []).length > 1 ? 1 : 0);
|
|
73
|
-
const back = (start) => {
|
|
74
|
-
const d = new Map([[start, 0]]), queue = [start];
|
|
75
|
-
while (queue.length) {
|
|
76
|
-
const y = queue.shift();
|
|
77
|
-
for (let i = 0; i < rel.paths.length; i++) {
|
|
78
|
-
if (to(i) !== y)
|
|
79
|
-
continue;
|
|
80
|
-
const x = from(i), c = d.get(y) + costAt(x);
|
|
81
|
-
if (c < (d.get(x) ?? Infinity)) {
|
|
82
|
-
d.set(x, c);
|
|
83
|
-
if (costAt(x))
|
|
84
|
-
queue.push(x);
|
|
85
|
-
else
|
|
86
|
-
queue.unshift(x);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
return d;
|
|
91
|
-
};
|
|
92
|
-
const backs = new Map();
|
|
93
|
-
// (a self-loop that counts a value by ±1 towards a bound its rows put on it)
|
|
94
|
-
const counters = new Map();
|
|
95
|
-
const counter = (j) => {
|
|
96
|
-
if (counters.has(j))
|
|
97
|
-
return counters.get(j);
|
|
98
|
-
const p = rel.paths[j], bounds = boundsOf(p.rows);
|
|
99
|
-
let out;
|
|
100
|
-
for (const x of S) {
|
|
101
|
-
const e = p.post[x], delta = e?.['1'] ?? 0, b = bounds.get(x);
|
|
102
|
-
if (!e || vars(e).length !== 1 || e[x] !== 1 || Math.abs(delta) !== 1 || (delta > 0 ? b?.hi : b?.lo) === undefined)
|
|
103
|
-
continue;
|
|
104
|
-
out = { x, delta, ...(b.lo !== undefined ? { lo: b.lo } : {}), ...(b.hi !== undefined ? { hi: b.hi } : {}) };
|
|
105
|
-
break;
|
|
235
|
+
// (`fewest`: the fewest choices a walk makes, so a deeper search finds only walks the first didn't)
|
|
236
|
+
const enumerate = (ids, fewest = 0, upTo = maxLen, cap = opts.maxCycles ?? 400) => {
|
|
237
|
+
let cycles = [];
|
|
238
|
+
// (those with an inner loop counted out apart, at most 60, after the rest)
|
|
239
|
+
const counted = [];
|
|
240
|
+
const byFrom = new Map();
|
|
241
|
+
for (let i = 0; i < ids; i++) {
|
|
242
|
+
const f = from(i);
|
|
243
|
+
byFrom.set(f, [...(byFrom.get(f) ?? []), i]);
|
|
106
244
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (!backs.has(s0))
|
|
118
|
-
backs.set(s0, back(s0));
|
|
119
|
-
const d = backs.get(s0);
|
|
120
|
-
// (`inner`: the self-loop just taken as many times as its rows allow, not taken again at once)
|
|
121
|
-
const walk = (seq, at, cost, sigma, rows, inner) => {
|
|
122
|
-
if (--nodes < 0 || cycles.length >= cap || now() > until)
|
|
123
|
-
return;
|
|
124
|
-
if (at === s0 && cost === budget && least(seq) && sat(rows)) {
|
|
125
|
-
if (!seq.some((x, q) => q > 0 && x === seq[q - 1] && to(x) === from(x)))
|
|
126
|
-
cycles.push(seq);
|
|
127
|
-
else if (inners.length < 60)
|
|
128
|
-
inners.push(seq);
|
|
129
|
-
}
|
|
130
|
-
if (seq.length >= 64)
|
|
131
|
-
return;
|
|
132
|
-
const c = cost + costAt(at);
|
|
133
|
-
if (c + (d.get(at) ?? Infinity) - costAt(at) > budget || c > budget)
|
|
134
|
-
return;
|
|
135
|
-
for (const j of byFrom.get(at) ?? []) {
|
|
136
|
-
if (j === inner || j < seq[0] || c + (d.get(to(j)) ?? Infinity) > budget)
|
|
137
|
-
continue;
|
|
138
|
-
const { rs, post } = open(sigma, j, seq.length), all = [...rows, ...rs];
|
|
139
|
-
if (all.some(big) || (costAt(at) && !sat(all)))
|
|
245
|
+
const least = (seq) => seq.every((_, r) => r === 0 || cmp(seq, [...seq.slice(r), ...seq.slice(0, r)]) < 0);
|
|
246
|
+
// (a turn costs 1 at a location of more than one path, 0 else; each location's least cost back to the cycle's start,
|
|
247
|
+
// so a walk that can't come back within the budget is cut)
|
|
248
|
+
const costAt = (loc) => ((byFrom.get(loc) ?? []).length > 1 ? 1 : 0);
|
|
249
|
+
const back = (start) => {
|
|
250
|
+
const d = new Map([[start, 0]]), queue = [start];
|
|
251
|
+
while (queue.length) {
|
|
252
|
+
const y = queue.shift();
|
|
253
|
+
for (let i = 0; i < NP; i++) {
|
|
254
|
+
if (to(i) !== y)
|
|
140
255
|
continue;
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
256
|
+
const x = from(i), c = d.get(y) + costAt(x);
|
|
257
|
+
if (c < (d.get(x) ?? Infinity)) {
|
|
258
|
+
d.set(x, c);
|
|
259
|
+
if (costAt(x))
|
|
260
|
+
queue.push(x);
|
|
261
|
+
else
|
|
262
|
+
queue.unshift(x);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return d;
|
|
267
|
+
};
|
|
268
|
+
const backs = new Map();
|
|
269
|
+
// (a self-loop that counts a value by ±1 towards a bound its rows put on it)
|
|
270
|
+
const counters = new Map();
|
|
271
|
+
const counter = (j) => {
|
|
272
|
+
if (counters.has(j))
|
|
273
|
+
return counters.get(j);
|
|
274
|
+
const p = rel.paths[j], bounds = boundsOf(p.rows);
|
|
275
|
+
let out;
|
|
276
|
+
for (const x of S) {
|
|
277
|
+
const e = p.post[x], delta = e?.['1'] ?? 0, b = bounds.get(x);
|
|
278
|
+
if (!e || vars(e).length !== 1 || e[x] !== 1 || Math.abs(delta) !== 1 || (delta > 0 ? b?.hi : b?.lo) === undefined)
|
|
279
|
+
continue;
|
|
280
|
+
out = { x, delta, ...(b.lo !== undefined ? { lo: b.lo } : {}), ...(b.hi !== undefined ? { hi: b.hi } : {}) };
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
counters.set(j, out);
|
|
284
|
+
return out;
|
|
285
|
+
};
|
|
286
|
+
// (each walk's turns composed with their inputs left open, u@k: a walk whose rows are refuted is cut where it chose; an
|
|
287
|
+
// inner loop with its own inputs)
|
|
288
|
+
const open = (sigma, i, k) => (i >= NP ? stepTurn(rel, natState, { inner: loops[i - NP] }, sigma) : turn(rel, natState, i, sigma, symbolic(rel.paths[i], k, Sset, P), k));
|
|
289
|
+
const id0 = Object.fromEntries(S.map(x => [x, { [x]: 1 }]));
|
|
290
|
+
let nodes = 20_000;
|
|
291
|
+
for (let budget = fewest; budget <= upTo && cycles.length < cap && nodes > 0; budget++) {
|
|
292
|
+
for (let i = 0; i < NP && cycles.length < cap && nodes > 0 && now() < opts.until; i++) {
|
|
293
|
+
const s0 = from(i);
|
|
294
|
+
if (!backs.has(s0))
|
|
295
|
+
backs.set(s0, back(s0));
|
|
296
|
+
const d = backs.get(s0);
|
|
297
|
+
// (`inner`: the self-loop just taken as many times as its rows allow, not taken again at once)
|
|
298
|
+
const walk = (seq, at, cost, sigma, rows, inner) => {
|
|
299
|
+
if (--nodes < 0 || cycles.length >= cap || now() > opts.until)
|
|
300
|
+
return;
|
|
301
|
+
if (at === s0 && cost === budget && least(seq) && sat(rows)) {
|
|
302
|
+
if (!seq.some((x, q) => q > 0 && x === seq[q - 1] && to(x) === from(x) && x < NP))
|
|
303
|
+
cycles.push(seq);
|
|
304
|
+
else if (counted.length < 60)
|
|
305
|
+
counted.push(seq);
|
|
306
|
+
}
|
|
307
|
+
if (seq.length >= 64)
|
|
308
|
+
return;
|
|
309
|
+
const c = cost + costAt(at);
|
|
310
|
+
if (c + (d.get(at) ?? Infinity) - costAt(at) > budget || c > budget)
|
|
311
|
+
return;
|
|
312
|
+
for (const j of byFrom.get(at) ?? []) {
|
|
313
|
+
if (j === inner || j < seq[0] || (j >= NP && j === seq.at(-1)) || c + (d.get(to(j)) ?? Infinity) > budget)
|
|
314
|
+
continue;
|
|
315
|
+
const { rs, post } = open(sigma, j, seq.length), all = [...rows, ...rs];
|
|
316
|
+
if (all.some(big) || (costAt(at) && !sat(all)))
|
|
317
|
+
continue;
|
|
318
|
+
walk([...seq, j], to(j), c, post, all);
|
|
319
|
+
// An inner loop: a self-loop that counts x by ±1 up or down to a bound of its rows, from a value the walk has
|
|
320
|
+
// set (x := 0, then x := x + 1 while x < 12: KnapsackDP's), taken as many times as that, as one choice, where
|
|
321
|
+
// that is 2 to 24 times; the walk goes on from there.
|
|
322
|
+
const cl = j < NP && to(j) === at && j !== seq.at(-1) && counted.length < 60 ? counter(j) : undefined, k0 = cl && sigma[cl.x];
|
|
323
|
+
if (cl && k0 && isConst(k0)) {
|
|
324
|
+
const k = k0['1'] ?? 0, r = cl.delta > 0 ? (cl.hi ?? -Infinity) - k + 1 : k - (cl.lo ?? Infinity) + 1;
|
|
325
|
+
if (r >= 2 && r <= 24 && seq.length + r < 64) {
|
|
326
|
+
let sg = sigma, rws = rows;
|
|
327
|
+
for (let q = 0; q < r; q++) {
|
|
328
|
+
const more = open(sg, j, seq.length + q);
|
|
329
|
+
sg = more.post;
|
|
330
|
+
rws = [...rws, ...more.rs];
|
|
331
|
+
}
|
|
332
|
+
nodes--;
|
|
333
|
+
if (!rws.some(big) && sat(rws))
|
|
334
|
+
walk([...seq, ...Array.from({ length: r }, () => j)], at, c, sg, rws, j);
|
|
154
335
|
}
|
|
155
|
-
nodes--;
|
|
156
|
-
if (!rws.some(big) && sat(rws))
|
|
157
|
-
walk([...seq, ...Array.from({ length: r }, () => j)], at, c, sg, rws, j);
|
|
158
336
|
}
|
|
159
337
|
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
338
|
+
};
|
|
339
|
+
const c0 = costAt(s0), first = open(id0, i, 0);
|
|
340
|
+
if (c0 + (d.get(to(i)) ?? Infinity) <= budget && sat(first.rs))
|
|
341
|
+
walk([i], to(i), c0, first.post, first.rs);
|
|
342
|
+
}
|
|
165
343
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
344
|
+
cycles = cycles.sort((x, y) => x.length - y.length).slice(0, cap);
|
|
345
|
+
return [...cycles.filter(c => c.length <= 6), ...counted.sort((x, y) => x.length - y.length), ...cycles.filter(c => c.length > 6)];
|
|
346
|
+
};
|
|
347
|
+
let cycles = enumerate(NP);
|
|
169
348
|
// the runs' cycles: between two visits of a location, the paths taken (as their least rotation), by how often; after
|
|
170
349
|
// the enumerated ones of at most 4 turns (at most 20,000 turns read)
|
|
171
350
|
if (opts.runs && opts.io) {
|
|
@@ -174,7 +353,7 @@ export function* accelerated(l, opts) {
|
|
|
174
353
|
rel.paths.forEach((_, i) => { byLoc.set(from(i), [...(byLoc.get(from(i)) ?? []), i]); });
|
|
175
354
|
let budget = 20_000;
|
|
176
355
|
for (const r of opts.runs) {
|
|
177
|
-
if (now() > until || budget <= 0)
|
|
356
|
+
if (now() > opts.until || budget <= 0)
|
|
178
357
|
break;
|
|
179
358
|
let st = opts.io.start(r.params);
|
|
180
359
|
const seq = [];
|
|
@@ -210,40 +389,113 @@ export function* accelerated(l, opts) {
|
|
|
210
389
|
cycles = [...short, ...fromRuns.filter(c => !shortKeys.has(c.join(','))), ...cycles.filter(c => c.length > 4 && !keys.has(c.join(',')))];
|
|
211
390
|
}
|
|
212
391
|
const tried = new Set();
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
392
|
+
const stepOf = (pi, ins) => (pi >= NP ? { inner: loops[pi - NP] } : { path: pi, ins });
|
|
393
|
+
// (`look`: the lookahead's choices too, or only them)
|
|
394
|
+
const tryAll = function* (cycs, look) {
|
|
395
|
+
for (const cyc0 of cycs) {
|
|
396
|
+
// (a cycle with a product: its turn first, where its product is read at the cycle's start; one such turn at most)
|
|
397
|
+
const prodAt = cyc0.flatMap((pi, j) => (pi < NP && readsPi(rel.paths[pi]) ? [j] : []));
|
|
398
|
+
if (prodAt.length > 1)
|
|
399
|
+
continue;
|
|
400
|
+
const cyc = prodAt.length ? [...cyc0.slice(prodAt[0]), ...cyc0.slice(0, prodAt[0])] : cyc0;
|
|
401
|
+
for (const reps of cyc.length === 1 && !prodAt.length ? [1, 2] : [1]) {
|
|
402
|
+
const seq = Array.from({ length: reps }, () => cyc).flat();
|
|
403
|
+
const firsts = seq.map(pi => (pi >= NP ? {} : choices[pi][0]));
|
|
404
|
+
// the choices of inputs: constants from a model of two rounds of the cycle (the constants the same in each), then
|
|
405
|
+
// each path's first choice of terms, then one path at a time its others, then one input at a time a bound the rest
|
|
406
|
+
// of the cycle puts on it (`lookahead`)
|
|
407
|
+
const sets = [];
|
|
408
|
+
if (look === 'only')
|
|
409
|
+
sets.push(...lookahead(rel, seq.map((pi, j) => stepOf(pi, firsts[j])), natState, Sset, P));
|
|
410
|
+
else {
|
|
411
|
+
const sym = seq.map((pi, j) => (pi >= NP ? {} : symbolic(rel.paths[pi], j, Sset, P)));
|
|
412
|
+
if (sym.some(x => Object.values(x).some(t => vars(t).some(v => v.includes('@'))))) {
|
|
413
|
+
const two = compose(rel, [...seq, ...seq].map((pi, j) => stepOf(pi, sym[j % seq.length])), natState);
|
|
414
|
+
const m = two && solve(two.rows.map(lin => ({ lin, label: '' })), 3000);
|
|
415
|
+
if (m && m.kind === 'sat')
|
|
416
|
+
sets.push(sym.map(x => Object.fromEntries(Object.entries(x).map(([u, t]) => [u, subst(t, Object.fromEntries(vars(t).filter(v => v.includes('@')).map(v => [v, konst(Math.round(m.witness[v] ?? 0))])))]))));
|
|
417
|
+
}
|
|
418
|
+
sets.push(firsts);
|
|
419
|
+
seq.forEach((pi, j) => { if (pi < NP)
|
|
420
|
+
for (let c = 1; c < choices[pi].length; c++)
|
|
421
|
+
sets.push(seq.map((qi, k) => (k === j ? choices[pi][c] : firsts[k]))); });
|
|
422
|
+
if (look === 'yes')
|
|
423
|
+
sets.push(...lookahead(rel, seq.map((pi, j) => stepOf(pi, firsts[j])), natState, Sset, P));
|
|
424
|
+
}
|
|
425
|
+
for (const ins of sets) {
|
|
426
|
+
if (now() > opts.until)
|
|
427
|
+
return;
|
|
428
|
+
const key = `${seq.join(',')}|${JSON.stringify(ins)}`;
|
|
429
|
+
if (tried.has(key))
|
|
430
|
+
continue;
|
|
431
|
+
tried.add(key);
|
|
432
|
+
const out = candidate(rel, seq.map((pi, j) => stepOf(pi, ins[j])), natState, l, () => now() > opts.until, pins);
|
|
433
|
+
if (out)
|
|
434
|
+
yield { ...out, cycle: seq, locs: seq.map(from) };
|
|
435
|
+
}
|
|
230
436
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
// The relation's cycles, then the cycles with an inner loop taken to its end, then the lookahead's choices on the first
|
|
440
|
+
yield* tryAll(cycles, 'no');
|
|
441
|
+
if (now() > opts.until)
|
|
442
|
+
return;
|
|
443
|
+
// The inner loops: a self-loop or a cycle of two turns at a location whose turn, with its inputs' choices, is a
|
|
444
|
+
// translation with a metering function (core/accel.ts's `accelerate`), taken to its end as one step of a walk; each
|
|
445
|
+
// named NP + k among the paths.
|
|
446
|
+
let tries = 2000;
|
|
447
|
+
for (let i = 0; i < NP && loops.length < 40 && tries > 0 && now() < opts.until; i++) {
|
|
448
|
+
const at = rel.loc ? rel.from[i] : 0;
|
|
449
|
+
if (readsPi(rel.paths[i]))
|
|
450
|
+
continue;
|
|
451
|
+
const tails = rel.loc && rel.to[i] !== at ? rel.paths.flatMap((_, j) => (rel.from[j] === rel.to[i] && rel.to[j] === at && j !== i ? [j] : [])) : [];
|
|
452
|
+
for (const ps of [...(!rel.loc || rel.to[i] === at ? [[i]] : []), ...tails.map(j => [i, j])]) {
|
|
453
|
+
if (ps.some(j => readsPi(rel.paths[j])))
|
|
454
|
+
continue;
|
|
455
|
+
const combos = ps.length === 1 ? choices[i].map(c => [c]) : choices[i].flatMap(a => choices[ps[1]].map(b => [a, b]));
|
|
456
|
+
for (const ins of combos.slice(0, 9)) {
|
|
457
|
+
if (--tries < 0)
|
|
458
|
+
break;
|
|
459
|
+
const c = compose(rel, ps.map((path, j) => ({ path, ins: ins[j] })), natState), a = c && accelerate(c.rows, c.post, S);
|
|
460
|
+
if (c && a?.meter && sat(c.rows)) {
|
|
461
|
+
loops.push({ loc: at, paths: ps, ins, rows: c.rows, acc: { delta: a.delta, meter: a.meter } });
|
|
462
|
+
break;
|
|
463
|
+
}
|
|
244
464
|
}
|
|
245
465
|
}
|
|
246
466
|
}
|
|
467
|
+
if (loops.length)
|
|
468
|
+
yield* tryAll(enumerate(NP + loops.length).filter(c => c.some(i => i >= NP)), 'yes');
|
|
469
|
+
if (now() < opts.until)
|
|
470
|
+
yield* tryAll(cycles, 'only');
|
|
471
|
+
// Deeper, in the time left: walks of up to 16 choices, at most 5,000 of them (hqr's cycle of 25 turns makes 16), each
|
|
472
|
+
// tried with its first inputs.
|
|
473
|
+
if (now() < opts.until && maxLen < DEEP)
|
|
474
|
+
yield* tryAll(enumerate(NP, maxLen + 1, DEEP, 5000), 'no');
|
|
475
|
+
}
|
|
476
|
+
const DEEP = 16;
|
|
477
|
+
// The choices one input at a time of a turn's that the rest of the cycle bounds: the cycle from that turn on (its other
|
|
478
|
+
// inputs as chosen), the bounds its rows put on the input, as terms of the state at that turn (x ≤ u gives x), the
|
|
479
|
+
// first three that leave the rows satisfiable; at most 12.
|
|
480
|
+
function lookahead(rel, steps, natState, S, P) {
|
|
481
|
+
const out = [];
|
|
482
|
+
for (const [j, s] of steps.entries()) {
|
|
483
|
+
if (!('path' in s))
|
|
484
|
+
continue;
|
|
485
|
+
for (const u of inputsOf(rel.paths[s.path], S, P)) {
|
|
486
|
+
if (out.length >= 12)
|
|
487
|
+
return out;
|
|
488
|
+
const name = `${u}@look`, rot = [{ path: s.path, ins: { ...s.ins, [u]: { [name]: 1 } } }, ...steps.slice(j + 1), ...steps.slice(0, j)];
|
|
489
|
+
const c = compose(rel, rot, natState);
|
|
490
|
+
if (!c)
|
|
491
|
+
continue;
|
|
492
|
+
const { bounds } = optionsFor(c.rows, name, x => S.has(x) || P.has(x));
|
|
493
|
+
const ok = unique(bounds).filter(t => sat(c.rows.map(f => subst(f, { [name]: t }))));
|
|
494
|
+
for (const t of ok.slice(0, 3))
|
|
495
|
+
out.push(steps.map((x, k) => (k === j ? { ...s.ins, [u]: t } : 'path' in x ? x.ins : {})));
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
return out;
|
|
247
499
|
}
|
|
248
500
|
// The path a turn takes: its pins (the location, the rule) and its rows hold at the state and the turn's inputs.
|
|
249
501
|
function pathAt(rel, among, st, input) {
|
|
@@ -259,23 +511,63 @@ function pathAt(rel, among, st, input) {
|
|
|
259
511
|
}
|
|
260
512
|
return undefined;
|
|
261
513
|
}
|
|
262
|
-
|
|
514
|
+
// (the relation as built, products and all, and as kept: each built once a loop)
|
|
515
|
+
const raws = new WeakMap(), rels = new WeakMap();
|
|
516
|
+
const rawOf = (l) => { if (!raws.has(l))
|
|
517
|
+
raws.set(l, relationOf(l)); return raws.get(l); };
|
|
518
|
+
// The loop's relation and its cases with a value fixed, built (once a loop): a caller may build them before the search,
|
|
519
|
+
// and not count the time against it.
|
|
520
|
+
export const relationBuilt = (l) => { relOf(l); fixedCases(l); return !!rels.get(l); };
|
|
263
521
|
function relOf(l) {
|
|
264
|
-
if (rels.has(l))
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
522
|
+
if (!rels.has(l))
|
|
523
|
+
rels.set(l, kept(l, rawOf(l)));
|
|
524
|
+
return rels.get(l);
|
|
525
|
+
}
|
|
526
|
+
// (a path that reads a product, which the relation stands for by a natural of its own, is kept where each product is
|
|
527
|
+
// of two values of the state or params, x := x·y: a cycle may take it as its first turn, the product read at the
|
|
528
|
+
// cycle's start (`piFacts`); a product of a turn's input is left out. The kernel checks what is found against the loop
|
|
529
|
+
// itself.)
|
|
530
|
+
function kept(l, r) {
|
|
271
531
|
if (r && !r.exact) {
|
|
272
532
|
const known = new Set([...l.params, ...Object.keys(l.state)]);
|
|
273
533
|
const ok = (x) => !isPi(x) || piParts(x)?.every(f => vars(f).every(n => known.has(n))) === true;
|
|
274
534
|
const keep = r.paths.map(p => [...p.rows.flatMap(vars), ...Object.values(p.post).flatMap(vars)].every(ok));
|
|
275
535
|
r = { ...r, paths: r.paths.filter((_, i) => keep[i]), from: r.from.filter((_, i) => keep[i]), to: r.to.filter((_, i) => keep[i]) };
|
|
276
536
|
}
|
|
277
|
-
|
|
278
|
-
|
|
537
|
+
return r && r.paths.length ? r : undefined;
|
|
538
|
+
}
|
|
539
|
+
// A product of a state value (or a param) and a turn's input (RandomHard's arg2 = arg3·x) is linear where the value is fixed:
|
|
540
|
+
// the loop's cases at x = 1, 0, −1 and 2 (an integer x, x$n or x$neg; a natural at 1, 0 and 2), each the relation of the
|
|
541
|
+
// loop whose condition fixes x first (its products then a constant times the input), the set holding x fixed (`pins`).
|
|
542
|
+
// At most two such values.
|
|
543
|
+
const fixes = new WeakMap();
|
|
544
|
+
function fixedCases(l) {
|
|
545
|
+
const hit = fixes.get(l);
|
|
546
|
+
if (hit)
|
|
547
|
+
return hit;
|
|
548
|
+
const out = [], raw = rawOf(l), S = new Set([...Object.keys(l.state), ...l.params]), I = new Set(l.inputs), xs = new Set();
|
|
549
|
+
for (const p of raw && !raw.exact ? raw.paths : [])
|
|
550
|
+
for (const n of [...p.rows.flatMap(vars), ...Object.values(p.post).flatMap(vars)]) {
|
|
551
|
+
const fs = isPi(n) ? piParts(n) : undefined;
|
|
552
|
+
if (!fs)
|
|
553
|
+
continue;
|
|
554
|
+
const [a, b] = fs.map(vars);
|
|
555
|
+
for (const [f, g] of [[a, b], [b, a]])
|
|
556
|
+
if (f.length === 1 && S.has(f[0]) && g.some(x => I.has(x)))
|
|
557
|
+
xs.add(f[0].replace(/\$(n|neg)$/, ''));
|
|
558
|
+
}
|
|
559
|
+
for (const x of [...xs].filter(x => S.has(x)).slice(0, 2)) {
|
|
560
|
+
const twin = [`${x}$n`, `${x}$neg`].find(t => S.has(t));
|
|
561
|
+
for (const c of twin ? [1, 0, -1, 2] : [1, 0, 2]) {
|
|
562
|
+
const vals = twin ? [[x, Math.max(c, 0)], [twin, Math.max(-c, 0)]] : [[x, c]];
|
|
563
|
+
const r = kept(l, relationOf({ ...l, while: { op: 'and', xs: [...vals.map(([n, v]) => ({ op: '=', a: V(n), b: K(v) })), l.while] } }));
|
|
564
|
+
if (!r)
|
|
565
|
+
continue;
|
|
566
|
+
const pins = r.ints.has(x) ? [{ [x]: 1, 1: -c }, { [x]: -1, 1: c }] : vals.flatMap(([n, v]) => [{ [n]: 1, ...konst(-v) }, { [n]: -1, ...konst(v) }]);
|
|
567
|
+
out.push({ rel: r, pins });
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
fixes.set(l, out);
|
|
279
571
|
return out;
|
|
280
572
|
}
|
|
281
573
|
// --- products ---------------------------------------------------------------------------------------------------------
|
|
@@ -501,14 +793,25 @@ function turn(rel, natState, i, sigma, ins, k) {
|
|
|
501
793
|
return { rs: [...p.rows.map(f => subst(f, sub)), ...natState.map(x => times(sigma[x], -1)), ...Object.keys(insAt).filter(u => !rel.ints.has(u)).map(u => times(insAt[u], -1))],
|
|
502
794
|
post: Object.fromEntries(rel.state.map(x => [x, subst(p.post[x] ?? { [x]: 1 }, sub)])) };
|
|
503
795
|
}
|
|
504
|
-
//
|
|
796
|
+
// A step from sigma. An inner loop's: its count m = meter(sigma) at least 1, its turn's rows at the first and the last
|
|
797
|
+
// of its turns (sigma + (m − 1)·δ: each row is linear in the turn's number, so it holds between), the state after
|
|
798
|
+
// sigma + m·δ, all linear in sigma.
|
|
799
|
+
function stepTurn(rel, natState, s, sigma) {
|
|
800
|
+
if ('path' in s)
|
|
801
|
+
return turn(rel, natState, s.path, sigma, s.ins);
|
|
802
|
+
const { rows, acc } = s.inner, m = subst(acc.meter, sigma);
|
|
803
|
+
const shift = (t) => Object.fromEntries(rel.state.map(x => [x, plus(sigma[x], times(t, acc.delta[x] ?? 0))]));
|
|
804
|
+
const last = shift(plus(m, konst(-1)));
|
|
805
|
+
return { rs: [plus(konst(1), m, -1), ...rows.map(f => subst(f, sigma)), ...rows.map(f => subst(f, last))], post: shift(m) };
|
|
806
|
+
}
|
|
807
|
+
// Steps j.. of the cycle composed from a state x (identity at j): the rows over x, and the state after them over x.
|
|
505
808
|
function compose(rel, steps, natState) {
|
|
506
809
|
let sigma = Object.fromEntries(rel.state.map(x => [x, { [x]: 1 }]));
|
|
507
810
|
const rows = [];
|
|
508
811
|
for (const [j, s] of steps.entries()) {
|
|
509
|
-
if (j > 0 && readsPi(rel.paths[s.path]))
|
|
812
|
+
if (j > 0 && 'path' in s && readsPi(rel.paths[s.path]))
|
|
510
813
|
return undefined;
|
|
511
|
-
const t =
|
|
814
|
+
const t = stepTurn(rel, natState, s, sigma);
|
|
512
815
|
rows.push(...t.rs);
|
|
513
816
|
sigma = t.post;
|
|
514
817
|
if (Object.values(sigma).some(big) || rows.some(big))
|
|
@@ -517,16 +820,17 @@ function compose(rel, steps, natState) {
|
|
|
517
820
|
return { rows: dedupe(rows.map(tight).filter(f => !(isConst(f) && (f['1'] ?? 0) <= 0))), post: sigma };
|
|
518
821
|
}
|
|
519
822
|
const dedupe = (rows) => [...new Map(rows.map(f => [canon(f), f])).values()];
|
|
520
|
-
// (`late`: the time is up; a candidate half made is given up)
|
|
521
|
-
function candidate(rel,
|
|
522
|
-
const steps = seq.map((path, j) => ({ path, ins: ins[j] }));
|
|
823
|
+
// (`late`: the time is up; a candidate half made is given up; `pins`: rows each part keeps, a case's fixed values)
|
|
824
|
+
function candidate(rel, steps, natState, l, late, pins) {
|
|
523
825
|
const whole = compose(rel, steps, natState);
|
|
524
826
|
if (!whole)
|
|
525
827
|
return undefined;
|
|
526
|
-
const G = whole.rows, U = whole.post;
|
|
828
|
+
const G = dedupe([...whole.rows, ...pins]), U = whole.post;
|
|
527
829
|
if (G.some(f => isConst(f) && (f['1'] ?? 0) > 0) || G.some(f => vars(f).some(isPi)) || !sat(G))
|
|
528
830
|
return undefined;
|
|
529
|
-
// the differences of each atom of G along U, up to degree 3 (none past a product: it is read at the cycle's start)
|
|
831
|
+
// the differences of each atom of G along U, up to degree 3 (none past a product: it is read at the cycle's start): the
|
|
832
|
+
// iterates g, g∘U, … of the closed forms' engine, differenced; a constant last difference above 0 gives the cycle up (g
|
|
833
|
+
// grows past 0)
|
|
530
834
|
const opt = [];
|
|
531
835
|
for (const g of G) {
|
|
532
836
|
let d = g;
|
|
@@ -538,7 +842,7 @@ function candidate(rel, seq, ins, natState, from, l, late) {
|
|
|
538
842
|
if ((next['1'] ?? 0) > 0)
|
|
539
843
|
return undefined;
|
|
540
844
|
break;
|
|
541
|
-
}
|
|
845
|
+
}
|
|
542
846
|
if (canon(next) === canon(d))
|
|
543
847
|
break;
|
|
544
848
|
opt.push(tight(next));
|
|
@@ -595,16 +899,37 @@ function candidate(rel, seq, ins, natState, from, l, late) {
|
|
|
595
899
|
R = irredundant(R, late);
|
|
596
900
|
if (late())
|
|
597
901
|
return undefined;
|
|
598
|
-
// per
|
|
902
|
+
// per step j: R_j, the rest of the cycle from step j allowed and ending in R (an inner loop's, at each of its turns'
|
|
903
|
+
// starts with a turn of it left: the same state after it)
|
|
904
|
+
const restOf = (rows, post, to) => irredundant(dedupe([...rows, ...pins, ...to.map(a => tight(subst(a, post)))].filter(f => !(isConst(f) && (f['1'] ?? 0) <= 0))), late);
|
|
599
905
|
const Rs = steps.map((_, j) => {
|
|
600
906
|
if (j === 0)
|
|
601
907
|
return R;
|
|
602
908
|
const rest = compose(rel, steps.slice(j), natState);
|
|
603
|
-
return
|
|
909
|
+
return restOf(rest.rows, rest.post, R);
|
|
604
910
|
});
|
|
605
911
|
if (late())
|
|
606
912
|
return undefined;
|
|
607
|
-
|
|
913
|
+
const locOf = (i) => (rel.loc ? rel.from[i] : 0);
|
|
914
|
+
const parts = [];
|
|
915
|
+
for (const [j, s] of steps.entries()) {
|
|
916
|
+
if ('path' in s) {
|
|
917
|
+
parts.push({ loc: locOf(s.path), rows: Rs[j], path: s.path, ins: s.ins });
|
|
918
|
+
continue;
|
|
919
|
+
}
|
|
920
|
+
const { paths, ins } = s.inner;
|
|
921
|
+
parts.push({ loc: s.inner.loc, rows: Rs[j], path: paths[0], ins: ins[0] });
|
|
922
|
+
for (let t = 1; t < paths.length; t++) {
|
|
923
|
+
const rest = compose(rel, paths.slice(t).map((path, k) => ({ path, ins: ins[t + k] })), natState);
|
|
924
|
+
if (!rest)
|
|
925
|
+
return undefined;
|
|
926
|
+
for (const to of [Rs[j], Rs[(j + 1) % steps.length]])
|
|
927
|
+
parts.push({ loc: locOf(paths[t]), rows: restOf(rest.rows, rest.post, to), path: paths[t], ins: ins[t] });
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
if (late())
|
|
931
|
+
return undefined;
|
|
932
|
+
if (parts.some(p => !sat(p.rows)))
|
|
608
933
|
return undefined;
|
|
609
934
|
// as the loop's condition and witness
|
|
610
935
|
const intOf = (x) => rel.ints.get(x);
|
|
@@ -630,23 +955,23 @@ function candidate(rel, seq, ins, natState, from, l, late) {
|
|
|
630
955
|
};
|
|
631
956
|
const atom = (f) => { const t = term(f); return { op: '<=', a: t.p, b: t.n }; };
|
|
632
957
|
const conj = (xs) => (xs.length === 1 ? xs[0] : { op: 'and', xs });
|
|
633
|
-
const pcIs = (j) => (rel.loc ? [{ op: '=', a: V(rel.loc), b: K(
|
|
634
|
-
const
|
|
635
|
-
// the witness: each input of the loop per
|
|
958
|
+
const pcIs = (j) => (rel.loc ? [{ op: '=', a: V(rel.loc), b: K(parts[j].loc) }] : []);
|
|
959
|
+
const full = parts.map((r, j) => conj([...pcIs(j), ...r.rows.map(atom)]));
|
|
960
|
+
// the witness: each input of the loop per part
|
|
636
961
|
// (a part is told by its location, and where the cycle comes back to a location, by an atom of its rows that each
|
|
637
962
|
// other part there refutes: a short condition, which the body's reads of the witness multiply less; its full rows
|
|
638
963
|
// where no atom tells it apart)
|
|
639
964
|
const tell = (j) => {
|
|
640
965
|
if (!rel.loc)
|
|
641
|
-
return
|
|
642
|
-
const others =
|
|
966
|
+
return full[j];
|
|
967
|
+
const others = parts.flatMap((r, k) => (k !== j && r.loc === parts[j].loc ? [r.rows] : []));
|
|
643
968
|
const atoms = [];
|
|
644
969
|
for (const r of others) {
|
|
645
970
|
if (atoms.some(a => !sat([...r, a])))
|
|
646
971
|
continue;
|
|
647
|
-
const a =
|
|
972
|
+
const a = parts[j].rows.find(f => !sat([...r, f]));
|
|
648
973
|
if (!a)
|
|
649
|
-
return
|
|
974
|
+
return full[j];
|
|
650
975
|
atoms.push(a);
|
|
651
976
|
}
|
|
652
977
|
return conj([...pcIs(j), ...atoms.map(atom)]);
|
|
@@ -657,20 +982,20 @@ function candidate(rel, seq, ins, natState, from, l, late) {
|
|
|
657
982
|
// the set after the turn costs it the next part's atoms, not every part's (TermComp's tqli: its check 10.4 s to 4.5
|
|
658
983
|
// s). Not where parts share a location: their conditions in each chain cost more than they save (s1).
|
|
659
984
|
const slotted = () => {
|
|
660
|
-
const width = Math.max(...
|
|
985
|
+
const width = Math.max(...parts.map(r => r.rows.length)), chain = (pick) => conds.reduceRight((acc, c, j) => (isZ(pick(j)) && isZ(acc) ? acc : { op: 'if', c, then: pick(j), else: acc }), K(0));
|
|
661
986
|
const slots = [];
|
|
662
987
|
for (let k = 0; k < width; k++) {
|
|
663
|
-
const ts =
|
|
988
|
+
const ts = parts.map(r => (r.rows[k] ? term(r.rows[k]) : { p: K(0), n: K(0) }));
|
|
664
989
|
slots.push({ op: '<=', a: chain(j => ts[j].p), b: chain(j => ts[j].n) });
|
|
665
990
|
}
|
|
666
991
|
return { op: 'and', xs: [{ op: 'or', xs: conds }, ...slots] };
|
|
667
992
|
};
|
|
668
|
-
const set = parts.length > 1 && rel.loc && new Set(
|
|
993
|
+
const set = parts.length > 1 && rel.loc && new Set(parts.map(p => p.loc)).size === parts.length ? slotted() : parts.length === 1 ? full[0] : { op: 'or', xs: full };
|
|
669
994
|
const valueAt = (j, n) => {
|
|
670
|
-
const p = rel.paths[
|
|
995
|
+
const p = rel.paths[parts[j].path];
|
|
671
996
|
if (n in p.pins)
|
|
672
997
|
return K(p.pins[n]);
|
|
673
|
-
const u =
|
|
998
|
+
const u = parts[j].ins;
|
|
674
999
|
for (const [x, pair] of rel.ints)
|
|
675
1000
|
if (pair[0] === n || pair[1] === n) {
|
|
676
1001
|
const t = u[x];
|
|
@@ -687,7 +1012,7 @@ function candidate(rel, seq, ins, natState, from, l, late) {
|
|
|
687
1012
|
};
|
|
688
1013
|
const wit = {};
|
|
689
1014
|
for (const n of l.inputs) {
|
|
690
|
-
const vals =
|
|
1015
|
+
const vals = parts.map((_, j) => valueAt(j, n)), keys = vals.map(v => JSON.stringify(v));
|
|
691
1016
|
if (keys.every(k => k === keys[0])) {
|
|
692
1017
|
wit[n] = vals[0];
|
|
693
1018
|
continue;
|
|
@@ -704,7 +1029,7 @@ function candidate(rel, seq, ins, natState, from, l, late) {
|
|
|
704
1029
|
e = { op: 'if', c: conds[j], then: vals[j], else: e };
|
|
705
1030
|
wit[n] = e;
|
|
706
1031
|
}
|
|
707
|
-
return { set, wit, targets:
|
|
1032
|
+
return { set, wit, targets: parts.map(p => ({ loc: p.loc, rows: p.rows })), ...(rel.loc ? { cases: parts.map((p, j) => ({ pins: [[rel.loc, p.loc]], cond: full[j] })) } : {}) };
|
|
708
1033
|
}
|
|
709
1034
|
const cmp = (a, b) => { for (let i = 0; i < a.length; i++)
|
|
710
1035
|
if (a[i] !== b[i])
|
|
@@ -796,25 +1121,26 @@ export function* stemsTo(l, targets, io, opts) {
|
|
|
796
1121
|
const from = (i) => (rel.loc ? rel.from[i] : 0), to = (i) => (rel.loc ? rel.to[i] : 0);
|
|
797
1122
|
const loops = new Map();
|
|
798
1123
|
rel.paths.forEach((p, i) => {
|
|
799
|
-
if (from(i) !== to(i))
|
|
1124
|
+
if (from(i) !== to(i) || readsPi(p))
|
|
800
1125
|
return;
|
|
801
1126
|
for (const ins of inputChoices(p, Sset, P)) {
|
|
802
1127
|
const c = compose(rel, [{ path: i, ins }], natState);
|
|
803
1128
|
if (!c)
|
|
804
1129
|
continue;
|
|
805
|
-
const
|
|
806
|
-
|
|
1130
|
+
const a = accelerate(c.rows, c.post, S);
|
|
1131
|
+
if (a) {
|
|
1132
|
+
loops.set(from(i), [...(loops.get(from(i)) ?? []), { path: i, ins, delta: a.delta }]);
|
|
1133
|
+
break;
|
|
1134
|
+
}
|
|
1135
|
+
const closed = {};
|
|
807
1136
|
for (const x of S) {
|
|
808
|
-
const
|
|
809
|
-
if (!
|
|
810
|
-
ok = false;
|
|
1137
|
+
const f = nfold({ [x]: 1 }, c.post, 4);
|
|
1138
|
+
if (!f)
|
|
811
1139
|
break;
|
|
812
|
-
|
|
813
|
-
if (d['1'])
|
|
814
|
-
delta[x] = d['1'];
|
|
1140
|
+
closed[x] = f;
|
|
815
1141
|
}
|
|
816
|
-
if (
|
|
817
|
-
loops.set(from(i), [...(loops.get(from(i)) ?? []), { path: i, ins,
|
|
1142
|
+
if (Object.keys(closed).length === S.length && S.some(x => canon(c.post[x]) !== canon({ [x]: 1 }))) {
|
|
1143
|
+
loops.set(from(i), [...(loops.get(from(i)) ?? []), { path: i, ins, closed }]);
|
|
818
1144
|
break;
|
|
819
1145
|
}
|
|
820
1146
|
}
|
|
@@ -862,7 +1188,7 @@ export function* stemsTo(l, targets, io, opts) {
|
|
|
862
1188
|
const stem = [];
|
|
863
1189
|
let left = 200_000;
|
|
864
1190
|
for (const s of segs) {
|
|
865
|
-
const times = s.n ? Math.round(w[s.n] ?? 1) : 1;
|
|
1191
|
+
const times = s.n ? Math.round(w[s.n] ?? 1) : s.times ?? 1;
|
|
866
1192
|
if (times > 100_000)
|
|
867
1193
|
return undefined;
|
|
868
1194
|
for (let k = 0; k < times; k++) {
|
|
@@ -896,7 +1222,8 @@ export function* stemsTo(l, targets, io, opts) {
|
|
|
896
1222
|
opts.report.exhausted = true;
|
|
897
1223
|
return;
|
|
898
1224
|
}
|
|
899
|
-
|
|
1225
|
+
// (`deepen`: the depth, then twice and four times it, with the calls given, 4 and 16 times them, a stem yielded once)
|
|
1226
|
+
let maxDepth = 0, shallow = -1;
|
|
900
1227
|
const outs = new Map();
|
|
901
1228
|
for (let i = 0; i < rel.paths.length; i++)
|
|
902
1229
|
if (dist.has(to(i)))
|
|
@@ -909,7 +1236,7 @@ export function* stemsTo(l, targets, io, opts) {
|
|
|
909
1236
|
for (const t of targets)
|
|
910
1237
|
if (t.loc === loc) {
|
|
911
1238
|
const m = solveRows([...rows, ...t.rows.map(f => subst(f, sigma))]);
|
|
912
|
-
if (m.kind === 'sat') {
|
|
1239
|
+
if (m.kind === 'sat' && segs.length > shallow) {
|
|
913
1240
|
const c = concrete(segs, m.witness);
|
|
914
1241
|
if (c)
|
|
915
1242
|
yield c;
|
|
@@ -930,6 +1257,21 @@ export function* stemsTo(l, targets, io, opts) {
|
|
|
930
1257
|
// a self-loop accelerated, then each path out
|
|
931
1258
|
if (!lastLoop)
|
|
932
1259
|
for (const lp of loops.get(loc) ?? []) {
|
|
1260
|
+
if (lp.closed) {
|
|
1261
|
+
// each count: its turns' rows (each turn's state the closed form at its number), the state after at the count
|
|
1262
|
+
let rs = [];
|
|
1263
|
+
for (let c = 1, at = sigma; c <= 16; c++) {
|
|
1264
|
+
rs = [...rs, ...turn(rel, natState, lp.path, at, lp.ins).rs];
|
|
1265
|
+
at = Object.fromEntries(S.map(x => [x, subst(closedAt(lp.closed[x], c), sigma)]));
|
|
1266
|
+
if (![2, 4, 8, 16].includes(c))
|
|
1267
|
+
continue;
|
|
1268
|
+
const all = [...rows, ...rs];
|
|
1269
|
+
if (all.some(big) || Object.values(at).some(big) || solveRows(all).kind === 'refuted')
|
|
1270
|
+
break;
|
|
1271
|
+
yield* dfs(loc, at, all, [...segs, { path: lp.path, ins: lp.ins, times: c }], k + 1, true);
|
|
1272
|
+
}
|
|
1273
|
+
continue;
|
|
1274
|
+
}
|
|
933
1275
|
const name = `n@${k}`, { rs, post } = step(lp.path, lp.ins, { name, delta: lp.delta });
|
|
934
1276
|
const all = [...rows, ...rs];
|
|
935
1277
|
if (solveRows(all).kind === 'refuted')
|
|
@@ -949,7 +1291,15 @@ export function* stemsTo(l, targets, io, opts) {
|
|
|
949
1291
|
yield* dfs(to(i), post, all, [...segs, { path: i, ins }], k + 1, false);
|
|
950
1292
|
}
|
|
951
1293
|
}
|
|
952
|
-
|
|
953
|
-
|
|
1294
|
+
for (const r of opts.deepen ? [1, 2, 4] : [1]) {
|
|
1295
|
+
const d = r * depth;
|
|
1296
|
+
calls = (opts.calls ?? 400) * r * r;
|
|
1297
|
+
maxDepth = Math.max(d, dist.get(startLoc) + d);
|
|
1298
|
+
yield* dfs(startLoc, sigma0, [...base], [], 0, false);
|
|
1299
|
+
if (calls <= 0 || now() > until)
|
|
1300
|
+
return;
|
|
1301
|
+
shallow = maxDepth;
|
|
1302
|
+
}
|
|
1303
|
+
if (opts.report)
|
|
954
1304
|
opts.report.exhausted = true;
|
|
955
1305
|
}
|