gdp-wired 0.23.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/README.md +12 -2
- package/dist/core/accel.d.ts +44 -0
- package/dist/core/accel.js +720 -0
- package/dist/core/bmc.d.ts +13 -2
- package/dist/core/bmc.js +390 -18
- package/dist/core/cycles.d.ts +23 -0
- package/dist/core/cycles.js +527 -146
- package/dist/core/data.d.ts +17 -0
- package/dist/core/data.js +1177 -213
- package/dist/core/discover.js +8 -3
- package/dist/core/dsl.d.ts +7 -2
- package/dist/core/dsl.js +47 -9
- package/dist/core/extract-check.d.ts +77 -0
- package/dist/core/extract-check.js +785 -0
- package/dist/core/extract-ir.d.ts +139 -0
- package/dist/core/extract-ir.js +198 -0
- package/dist/core/extract-print.d.ts +30 -0
- package/dist/core/extract-print.js +426 -0
- package/dist/core/extract-read.d.ts +298 -0
- package/dist/core/extract-read.js +1298 -0
- package/dist/core/extract-shapes.d.ts +13 -0
- package/dist/core/extract-shapes.js +993 -0
- package/dist/core/extract-xstate.d.ts +34 -0
- package/dist/core/extract-xstate.js +320 -0
- package/dist/core/extract.d.ts +31 -0
- package/dist/core/extract.js +249 -0
- package/dist/core/infer.d.ts +1 -0
- package/dist/core/infer.js +298 -28
- package/dist/core/kind.d.ts +30 -0
- package/dist/core/kind.js +81 -0
- package/dist/core/nonterm.d.ts +4 -1
- package/dist/core/nonterm.js +98 -2
- package/dist/core/relation.js +82 -7
- package/dist/core/transfer.js +1 -1
- package/dist/core/xstate.d.ts +6 -0
- package/dist/core/xstate.js +70 -12
- package/dist/kernel/cdcl.d.ts +15 -0
- package/dist/kernel/cdcl.js +331 -244
- package/dist/kernel/kernel.d.ts +15 -0
- package/dist/kernel/kernel.js +32 -0
- package/dist/kernel/prove.js +20 -7
- package/dist/kernel/reader.d.ts +14 -0
- package/dist/kernel/reader.js +102 -14
- package/dist/lean-kernel/emit.d.ts +28 -1
- package/dist/lean-kernel/emit.js +56 -15
- package/dist/lean-kernel/library.js +2 -1
- package/package.json +12 -3
package/dist/core/nonterm.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { checkRecur, recurGoal, substC, trueC } from "../kernel/kernel.js";
|
|
1
|
+
import { checkRecur, loopMachine, recurGoal, substC, trueC } from "../kernel/kernel.js";
|
|
2
2
|
import { clock, counted, grow } from "../kernel/prove.js";
|
|
3
3
|
import { work } from "./solver.js";
|
|
4
|
+
import { accelerated, stemsTo } from "./accel.js";
|
|
5
|
+
import { findBoundedRun } from "./bmc.js";
|
|
4
6
|
const K = (value) => ({ op: 'const', value }), V = (name) => ({ op: 'var', name });
|
|
5
7
|
const T = trueC, F = { op: '<', a: K(0), b: K(0) };
|
|
6
8
|
const isT = (c) => c.op === '<=' && c.a.op === 'const' && c.b.op === 'const' && c.a.value <= c.b.value;
|
|
@@ -77,6 +79,10 @@ export function goalSize(x) {
|
|
|
77
79
|
return n;
|
|
78
80
|
}
|
|
79
81
|
const GOAL_MAX = 300_000;
|
|
82
|
+
// An accelerated set's goal: the witness reads the location (and the set's parts only where the cycle comes back to one),
|
|
83
|
+
// so its ifs multiply the body's less, and such goals of millions of nodes have closed (TermComp's tqli: 720,729 nodes,
|
|
84
|
+
// proven in 22 s).
|
|
85
|
+
const ACCEL_GOAL_MAX = 5_000_000;
|
|
80
86
|
// --- running the loop ---------------------------------------------------------------------------------------------------
|
|
81
87
|
class Overflow extends Error {
|
|
82
88
|
}
|
|
@@ -231,7 +237,7 @@ function search(l, opts) {
|
|
|
231
237
|
const wall0 = performance.now(), now = clock, t0 = now(), until = t0 + (opts.ms ?? 10_000), maxTurns = opts.maxTurns ?? 400;
|
|
232
238
|
let seed = opts.seed ?? 7;
|
|
233
239
|
const rand = (k) => { seed = (seed * 1103515245 + 12345) % 2 ** 31; return Math.floor(seed / 2 ** 31 * k); };
|
|
234
|
-
const stats = { runs: 0, turns: 0, policies: 0, rounds: 0, ms: 0, lassoTried: 0, goalSize: 0 };
|
|
240
|
+
const stats = { runs: 0, turns: 0, policies: 0, rounds: 0, ms: 0, lassoTried: 0, goalSize: 0, accel: 0, accelClosed: 0 };
|
|
235
241
|
const done = (v) => ({ ...v, stats: { ...stats, ms: Math.round(performance.now() - wall0) } });
|
|
236
242
|
const stateNames = Object.keys(l.state), kept = [...l.params, ...stateNames];
|
|
237
243
|
const allObs = observables(kept), inObs = observables(l.inputs);
|
|
@@ -378,6 +384,96 @@ function search(l, opts) {
|
|
|
378
384
|
}
|
|
379
385
|
if (opts.lassos)
|
|
380
386
|
return done({ proven: false, checked: false, why: `no lasso in ${stats.runs} runs` });
|
|
387
|
+
// 1b. Accelerated cycles (core/accel.ts): a set from a cycle of the relation, a stem into it, its closure proven.
|
|
388
|
+
if ((opts.accel ?? 0.4) > 0) {
|
|
389
|
+
const accelUntil = Math.min(until, now() + (until - now()) * (opts.accel ?? 0.4)), m = loopMachine(l);
|
|
390
|
+
try {
|
|
391
|
+
// (the random runs, the longest 20, for the cycles they take)
|
|
392
|
+
const ends = new Map();
|
|
393
|
+
for (const v of visits)
|
|
394
|
+
if (v.run && (ends.get(v.run)?.at ?? -1) < v.at)
|
|
395
|
+
ends.set(v.run, v);
|
|
396
|
+
const runs = [...ends.values()].sort((a, b) => b.at - a.at).slice(0, 20).map(v => ({ params: v.params, inputs: stemOf(v) }));
|
|
397
|
+
for (const a of accelerated(l, { now, until: accelUntil, runs, io: { start, turn } })) {
|
|
398
|
+
stats.accel++;
|
|
399
|
+
const goal = recurGoal(l, a);
|
|
400
|
+
stats.goalSize = Math.max(stats.goalSize, goalSize(goal.hyps));
|
|
401
|
+
if (goalSize(goal.hyps) > ACCEL_GOAL_MAX)
|
|
402
|
+
continue;
|
|
403
|
+
const inSet = cC(a.set);
|
|
404
|
+
// A stem into the set, cut where it first enters it (every turn allowed): from a state a random run reached (the
|
|
405
|
+
// shortest), else an accelerated run (cheap: tried before the closure, which is not tried for a set no stem
|
|
406
|
+
// reaches), else, once the set is closed, a bounded run (core/bmc.ts: the kernel's k-step goal's model, the set's
|
|
407
|
+
// complement as the law it breaks).
|
|
408
|
+
const cut = (t) => {
|
|
409
|
+
let st = start(t.params);
|
|
410
|
+
for (let j = 0; j <= t.stem.length; j++) {
|
|
411
|
+
if (inSet(st))
|
|
412
|
+
return { params: t.params, stem: t.stem.slice(0, j) };
|
|
413
|
+
if (j === t.stem.length)
|
|
414
|
+
return undefined;
|
|
415
|
+
const nx = turn(st, t.stem[j]);
|
|
416
|
+
if (!nx)
|
|
417
|
+
return undefined;
|
|
418
|
+
st = nx;
|
|
419
|
+
}
|
|
420
|
+
return undefined;
|
|
421
|
+
};
|
|
422
|
+
const stems = [], report = { exhausted: false };
|
|
423
|
+
let best;
|
|
424
|
+
for (const v of visits)
|
|
425
|
+
if ((!best || v.at < best.at) && inSet(v.st))
|
|
426
|
+
best = v;
|
|
427
|
+
if (best)
|
|
428
|
+
stems.push({ params: best.params, stem: stemOf(best) });
|
|
429
|
+
else {
|
|
430
|
+
// (a set's stem has time of its own, past the stage's if need be, within the search's)
|
|
431
|
+
let n = 0;
|
|
432
|
+
for (const t of stemsTo(l, a.targets, { start, turn }, { now, until: Math.min(until, now() + 1000), report })) {
|
|
433
|
+
const c = cut(t);
|
|
434
|
+
if (c) {
|
|
435
|
+
stems.push(c);
|
|
436
|
+
break;
|
|
437
|
+
}
|
|
438
|
+
if (++n >= 4)
|
|
439
|
+
break;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
// (a set the accelerated runs' search, run to its end, doesn't reach is left: the time goes to sets a run reaches)
|
|
443
|
+
if (!stems.length && report.exhausted)
|
|
444
|
+
continue;
|
|
445
|
+
// (a set with a stem is likely: its closure may take most of what is left, past the stage's share; one without,
|
|
446
|
+
// the stage's share and at most 4 s)
|
|
447
|
+
const out = grow(goal, new Map(), { nodes: 0, until: stems.length ? Math.min(until, now() + Math.max(4000, (until - now()) * 0.6)) : Math.min(accelUntil, now() + 4000) });
|
|
448
|
+
if (!('tree' in out))
|
|
449
|
+
continue;
|
|
450
|
+
stats.accelClosed++;
|
|
451
|
+
if (!stems.length && now() < until) {
|
|
452
|
+
const left = Math.min(until - now(), 2000);
|
|
453
|
+
const run = findBoundedRun(m, [{ name: 'outside the set', holds: { op: 'not', x: a.set } }], { ms: left, depth: 24, ...(opts.counted ? {} : { wall: performance.now() + left }) });
|
|
454
|
+
const c = run && cut({ params: run.params, stem: run.trace.map(r => Object.fromEntries(l.inputs.map(n => [n, Number(r[n] ?? 0)]))) });
|
|
455
|
+
if (c)
|
|
456
|
+
stems.push(c);
|
|
457
|
+
}
|
|
458
|
+
for (const t of stems) {
|
|
459
|
+
const recur = { start: params(t.params), stem: asStem(t.stem), set: a.set, wit: a.wit, tree: out.tree };
|
|
460
|
+
let ok = false;
|
|
461
|
+
try {
|
|
462
|
+
ok = checkRecur(l, recur);
|
|
463
|
+
}
|
|
464
|
+
catch {
|
|
465
|
+
ok = false;
|
|
466
|
+
}
|
|
467
|
+
if (ok)
|
|
468
|
+
return done({ proven: true, checked: true, recur, by: 'accelerated' });
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
catch (e) {
|
|
473
|
+
if (!(e instanceof Overflow))
|
|
474
|
+
throw e;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
381
477
|
// 2. Policies from the states reached.
|
|
382
478
|
const pools = inObs.map(o => {
|
|
383
479
|
const terms = [iConst(0), iConst(1)];
|
package/dist/core/relation.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { clock } from "../kernel/prove.js";
|
|
2
|
+
import { solve, work } from "./solver.js";
|
|
2
3
|
class Refused extends Error {
|
|
3
4
|
}
|
|
4
5
|
const id = (t) => JSON.stringify(t);
|
|
@@ -110,21 +111,90 @@ function assert(c, p, positive, n) {
|
|
|
110
111
|
// The body's value of a state variable on a path: its ifs decided or split, a truncated difference at the top kept
|
|
111
112
|
// whole (an integer's part, if it is one).
|
|
112
113
|
// An if and the ifs of its else branches, one after another (a long chain of rules is no deeper a recursion).
|
|
114
|
+
// (A condition the pins decide alone, pc = 3 and t = 1 on a path that pins both, is read at them first: a chain of a
|
|
115
|
+
// rule per case, for every path, is then a walk, not a split of each case.)
|
|
113
116
|
function chain(e, p, n, leaf) {
|
|
114
117
|
const out = [];
|
|
115
118
|
let cur = e, ps = [p];
|
|
116
119
|
while (cur.op === 'if' && ps.length) {
|
|
117
|
-
const c = cur.c, then = cur.then;
|
|
118
|
-
for (const q of ps)
|
|
120
|
+
const c = cur.c, then = cur.then, next = [];
|
|
121
|
+
for (const q of ps) {
|
|
122
|
+
const t = truthAt(c, q.pins);
|
|
123
|
+
if (t === true) {
|
|
124
|
+
out.push(...leaf(then, q, n));
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (t === false) {
|
|
128
|
+
next.push(q);
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
119
131
|
for (const r of assert(c, q, true, n))
|
|
120
132
|
out.push(...leaf(then, r, n));
|
|
121
|
-
|
|
133
|
+
next.push(...assert(c, q, false, n));
|
|
134
|
+
}
|
|
135
|
+
ps = next;
|
|
122
136
|
cur = cur.else;
|
|
123
137
|
}
|
|
124
138
|
for (const q of ps)
|
|
125
139
|
out.push(...leaf(cur, q, n));
|
|
126
140
|
return out;
|
|
127
141
|
}
|
|
142
|
+
// A term's value, and a condition's truth, where the pins decide them (undefined where a name isn't pinned).
|
|
143
|
+
function valueAt(e, pins) {
|
|
144
|
+
switch (e.op) {
|
|
145
|
+
case 'var': return pins[e.name];
|
|
146
|
+
case 'const': return e.value;
|
|
147
|
+
case 'mul': {
|
|
148
|
+
const a = valueAt(e.a, pins);
|
|
149
|
+
return a === undefined ? undefined : e.k * a;
|
|
150
|
+
}
|
|
151
|
+
case 'if': {
|
|
152
|
+
const c = truthAt(e.c, pins);
|
|
153
|
+
return c === undefined ? undefined : valueAt(c ? e.then : e.else, pins);
|
|
154
|
+
}
|
|
155
|
+
case 'add':
|
|
156
|
+
case 'sub':
|
|
157
|
+
case 'min':
|
|
158
|
+
case 'max':
|
|
159
|
+
case 'prod': {
|
|
160
|
+
const a = valueAt(e.a, pins), b = a === undefined ? undefined : valueAt(e.b, pins);
|
|
161
|
+
if (a === undefined || b === undefined)
|
|
162
|
+
return undefined;
|
|
163
|
+
return e.op === 'add' ? a + b : e.op === 'sub' ? Math.max(0, a - b) : e.op === 'min' ? Math.min(a, b) : e.op === 'max' ? Math.max(a, b) : a * b;
|
|
164
|
+
}
|
|
165
|
+
case 'get': return undefined;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
function truthAt(c, pins) {
|
|
169
|
+
switch (c.op) {
|
|
170
|
+
case '<':
|
|
171
|
+
case '<=':
|
|
172
|
+
case '=': {
|
|
173
|
+
const a = valueAt(c.a, pins), b = a === undefined ? undefined : valueAt(c.b, pins);
|
|
174
|
+
if (a === undefined || b === undefined)
|
|
175
|
+
return undefined;
|
|
176
|
+
return c.op === '<' ? a < b : c.op === '<=' ? a <= b : a === b;
|
|
177
|
+
}
|
|
178
|
+
case 'and':
|
|
179
|
+
case 'or': {
|
|
180
|
+
let open = false;
|
|
181
|
+
for (const x of c.xs) {
|
|
182
|
+
const t = truthAt(x, pins);
|
|
183
|
+
if (t === undefined)
|
|
184
|
+
open = true;
|
|
185
|
+
else if (t === (c.op === 'or'))
|
|
186
|
+
return t;
|
|
187
|
+
}
|
|
188
|
+
return open ? undefined : c.op === 'and';
|
|
189
|
+
}
|
|
190
|
+
case 'not': {
|
|
191
|
+
const t = truthAt(c.x, pins);
|
|
192
|
+
return t === undefined ? undefined : !t;
|
|
193
|
+
}
|
|
194
|
+
case 'flag':
|
|
195
|
+
case 'all': return undefined;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
128
198
|
function valueOf(e, p, n) {
|
|
129
199
|
if (e.op === 'if')
|
|
130
200
|
return chain(e, p, n, valueOf);
|
|
@@ -350,6 +420,9 @@ const norm = (x) => { const g = x.reduce(bgcd, 0n); return g > 1n ? x.map(y => y
|
|
|
350
420
|
const dot = (a, b) => a.reduce((s, x, i) => s + x * b[i], 0n);
|
|
351
421
|
export function generators(rows, names, nats = [], cap = 600, until = Infinity) {
|
|
352
422
|
const n = names.length, at = new Map(names.map((x, i) => [x, i]));
|
|
423
|
+
// (a row over a name not given has no place: no generators, rather than its coefficient dropped)
|
|
424
|
+
if (rows.some(r => Object.keys(r).some(x => x !== '1' && !at.has(x))))
|
|
425
|
+
return undefined;
|
|
353
426
|
const hs = [...rows.map(r => { const h = Array.from({ length: n + 1 }, () => 0n); for (const [x, c] of Object.entries(r)) {
|
|
354
427
|
if (x === '1')
|
|
355
428
|
h[n] = BigInt(c);
|
|
@@ -377,7 +450,9 @@ export function generators(rows, names, nats = [], cap = 600, until = Infinity)
|
|
|
377
450
|
const pos = rays.filter((_, i) => val[i] > 0n), neg = rays.filter((_, i) => val[i] < 0n), zero = rays.filter((_, i) => val[i] === 0n);
|
|
378
451
|
const next = [...zero.map(r => ({ v: r.v, zero: new Set([...r.zero, hi]) })), ...neg];
|
|
379
452
|
const vp = new Map(pos.map(r => [r, dot(h, r.v)])), vn = new Map(neg.map(r => [r, dot(h, r.v)]));
|
|
380
|
-
|
|
453
|
+
// (counted as work: a counted search's clock runs while the generators are made)
|
|
454
|
+
work.n += 1 + ((rays.length + pos.length * neg.length) * (n + 1) >> 4);
|
|
455
|
+
if (clock() > until)
|
|
381
456
|
return undefined;
|
|
382
457
|
for (const p of pos)
|
|
383
458
|
for (const q of neg) {
|
|
@@ -427,7 +502,7 @@ export function houdini(rel, cands, locOf, until) {
|
|
|
427
502
|
for (let round = 0, changed = true; changed && round < 50; round++) {
|
|
428
503
|
changed = false;
|
|
429
504
|
for (const [i, p] of rel.paths.entries()) {
|
|
430
|
-
if (
|
|
505
|
+
if (clock() > until)
|
|
431
506
|
return undefined;
|
|
432
507
|
const [a, b] = locOf(i);
|
|
433
508
|
if (!H[b].length)
|
|
@@ -441,7 +516,7 @@ export function houdini(rel, cands, locOf, until) {
|
|
|
441
516
|
const nat = rel.nats.filter(x => ns.includes(x)).map((x) => ({ [x]: -1 }));
|
|
442
517
|
const through = (c) => { let o = {}; for (const [y, k] of Object.entries(c))
|
|
443
518
|
o = plus(o, y !== '1' && p.post[y] ? times(p.post[y], k) : { [y]: k }); return o; };
|
|
444
|
-
const keep = H[b].filter(c =>
|
|
519
|
+
const keep = H[b].filter(c => clock() < until && solve([...rows, ...nat, plus(konst(1), through(c), -1)].map(lin => ({ lin, label: '' })), 4000).kind === 'refuted');
|
|
445
520
|
if (keep.length < H[b].length) {
|
|
446
521
|
H[b] = keep;
|
|
447
522
|
changed = true;
|
package/dist/core/transfer.js
CHANGED
|
@@ -331,7 +331,7 @@ export function proveThrough(l, plan, opts, until) {
|
|
|
331
331
|
// (through the bridges and unfolded: half the time left, the other half through the unfolding alone, the functions
|
|
332
332
|
// as written)
|
|
333
333
|
const again = b.unfolded && bridged.size > 0;
|
|
334
|
-
const r = searchIn(b.lstar, opts, again ? clock() + (until - clock()) / 2 : until, { p: b.p, fs, base: [], level: 0 });
|
|
334
|
+
const r = searchIn(b.lstar, opts, again ? clock() + (until - clock()) / 2 : until, { p: b.p, fs, base: [], level: 0, merge: true });
|
|
335
335
|
tried += r.verdict.tried;
|
|
336
336
|
if (!r.out && again && clock() < until) {
|
|
337
337
|
bridged = new Map();
|
package/dist/core/xstate.d.ts
CHANGED
|
@@ -8,9 +8,11 @@ type XAction = string | {
|
|
|
8
8
|
type XTransition = string | {
|
|
9
9
|
target?: string | string[];
|
|
10
10
|
guard?: unknown;
|
|
11
|
+
cond?: unknown;
|
|
11
12
|
actions?: XAction | XAction[];
|
|
12
13
|
reenter?: boolean;
|
|
13
14
|
description?: string;
|
|
15
|
+
[k: string]: unknown;
|
|
14
16
|
};
|
|
15
17
|
export type XNode = {
|
|
16
18
|
id?: string;
|
|
@@ -41,6 +43,10 @@ export type Bridge = {
|
|
|
41
43
|
guards?: Record<string, Guard>;
|
|
42
44
|
actions?: Record<string, Action>;
|
|
43
45
|
events?: string[];
|
|
46
|
+
inline?: {
|
|
47
|
+
guards?: ReadonlyMap<unknown, Guard>;
|
|
48
|
+
actions?: ReadonlyMap<unknown, Action>;
|
|
49
|
+
};
|
|
44
50
|
};
|
|
45
51
|
export type MachineLike = object;
|
|
46
52
|
export declare function fromXState(machine: MachineLike, opts?: Bridge): Chart<Obj, Obj, Obj>;
|
package/dist/core/xstate.js
CHANGED
|
@@ -18,22 +18,57 @@
|
|
|
18
18
|
// root, eventless (`always`) and delayed (`after`) transitions, invoked actors and done events, wildcard events, several
|
|
19
19
|
// targets at once, inline guards and actions, entry and exit actions that write the context, two regions writing the
|
|
20
20
|
// context on one event (XState runs their actions one after another; a chart's regions write in one step), and a
|
|
21
|
-
// shallow history over nested states.
|
|
21
|
+
// shallow history over nested states. So is every key it doesn't read, rather than dropped: a node or transition key
|
|
22
|
+
// outside the lists below (KEYS), an initial transition with actions, an event XState raises itself (done, error, init).
|
|
23
|
+
// An XState v4 machine reads the same, its `cond` the guard v5 calls `guard` (a name, or {type, ...params}); v4's
|
|
24
|
+
// `in`, `strict`, `activities` and eventless `''` are refused with the rest. Each XState ignores the other's guard key,
|
|
25
|
+
// so a v5 machine with `cond` or a v4 one with `guard` is refused (a plain config is read by the key it uses).
|
|
22
26
|
import { Choice, bool, int } from "./dsl.js";
|
|
23
27
|
import { chart } from "./chart.js";
|
|
28
|
+
// The keys read, and those that change nothing a run shows (descriptions, metadata, types, a final state's output, v4's
|
|
29
|
+
// action-order flags, which order no write to the context: those a transition makes run in order either way; v5's
|
|
30
|
+
// `reenter` and v4's `internal`, which change only which entry and exit actions run, and those write no context here).
|
|
31
|
+
// Any other is refused, by name, rather than dropped; some with the reason.
|
|
32
|
+
const KEYS = {
|
|
33
|
+
node: new Set(['id', 'type', 'history', 'initial', 'target', 'states', 'on', 'entry', 'exit', 'description', 'meta', 'tags', 'output', 'data']),
|
|
34
|
+
root: new Set(['context', 'version', 'types', 'schemas', 'schema', 'tsTypes', 'predictableActionArguments', 'preserveActionOrder']),
|
|
35
|
+
transition: new Set(['target', 'guard', 'cond', 'actions', 'reenter', 'internal', 'description', 'meta']),
|
|
36
|
+
};
|
|
37
|
+
const WHY = {
|
|
38
|
+
always: '`always` has no chart counterpart', after: '`after` has no chart counterpart', invoke: '`invoke` has no chart counterpart', onDone: '`onDone` has no chart counterpart',
|
|
39
|
+
onError: '`onError` has no chart counterpart', activities: "v4's `activities` have no chart counterpart", strict: "v4's `strict` (an event no state takes throws) has no chart counterpart",
|
|
40
|
+
delimiter: 'a state path delimiter other than "."', onEntry: "v4's `onEntry`: write it as `entry`", onExit: "v4's `onExit`: write it as `exit`",
|
|
41
|
+
in: "v4's `in` guard: write it as a named guard reading env.in(...)",
|
|
42
|
+
};
|
|
43
|
+
const keyCheck = (o, allowed, refuse, also) => {
|
|
44
|
+
for (const k of Object.keys(o))
|
|
45
|
+
if (!allowed.has(k) && !also?.has(k) && o[k] !== undefined)
|
|
46
|
+
refuse(WHY[k] ?? `\`${k}\`, a key fromXState doesn't read (it would be dropped)`);
|
|
47
|
+
};
|
|
48
|
+
// Events XState raises itself (a done state or actor, an error, the start): a chart's events are only those sent.
|
|
49
|
+
const raised = (e) => e === '' || /^(xstate\.|done\.(state|invoke)\.|error\.(platform|execution|communication))/.test(e);
|
|
24
50
|
const list = (x) => (x === undefined ? [] : Array.isArray(x) ? x : [x]);
|
|
25
51
|
const configOf = (machine) => ('config' in machine && machine.config && typeof machine.config === 'object' ? machine.config : machine);
|
|
52
|
+
// The XState a machine is of: v5's machines have getInitialSnapshot, v4's withConfig; a plain config says neither.
|
|
53
|
+
const versionOf = (machine) => {
|
|
54
|
+
const m = machine;
|
|
55
|
+
return typeof m.getInitialSnapshot === 'function' ? 5 : typeof m.withConfig === 'function' ? 4 : undefined;
|
|
56
|
+
};
|
|
26
57
|
export function fromXState(machine, opts = {}) {
|
|
27
|
-
const root = configOf(machine), name = root.id ?? 'machine', parallel = root.type === 'parallel';
|
|
58
|
+
const root = configOf(machine), name = root.id ?? 'machine', parallel = root.type === 'parallel', version = versionOf(machine);
|
|
28
59
|
const refuse = (where, what) => { throw new Error(`${name}: ${where || 'the machine'}: ${what}`); };
|
|
29
60
|
const infos = [], ids = new Map();
|
|
30
61
|
const walk = (node, segs, parent) => {
|
|
31
62
|
const info = { node, segs, parent }, where = segs.join('.');
|
|
32
63
|
infos.push(info);
|
|
33
64
|
ids.set(node.id ?? (segs.length ? `${name}.${where}` : name), info);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
65
|
+
keyCheck(node, KEYS.node, what => refuse(where, what), segs.length ? undefined : KEYS.root);
|
|
66
|
+
if (node.type !== undefined && !['atomic', 'compound', 'parallel', 'final', 'history'].includes(node.type))
|
|
67
|
+
refuse(where, `the state type ${JSON.stringify(node.type)}`);
|
|
68
|
+
if (node.initial !== undefined && typeof node.initial !== 'string')
|
|
69
|
+
refuse(where, 'an initial transition given as an object (its actions have no chart counterpart): name the initial state');
|
|
70
|
+
if (node.type !== 'history' && (node.history !== undefined || node.target !== undefined))
|
|
71
|
+
refuse(where, '`history` or `target` on a state that is not a history node');
|
|
37
72
|
if (node.type === 'parallel' && segs.length)
|
|
38
73
|
refuse(where, 'a parallel state below the root (a chart runs regions only at the top)');
|
|
39
74
|
for (const [k, kid] of Object.entries(node.states ?? {}))
|
|
@@ -75,16 +110,25 @@ export function fromXState(machine, opts = {}) {
|
|
|
75
110
|
return at.node.type === 'history' ? { to: at.parent, viaHistory: true } : { to: at, viaHistory: false };
|
|
76
111
|
};
|
|
77
112
|
// Guards and actions, named and given again.
|
|
78
|
-
|
|
113
|
+
// v5's guard: a name or {type, params}; v4's cond: a name or {type, ...params}, its params the object's other keys.
|
|
114
|
+
const guardOf = (where, g, v4 = false) => {
|
|
79
115
|
if (g === undefined)
|
|
80
116
|
return undefined;
|
|
81
|
-
const
|
|
117
|
+
const given = opts.inline?.guards?.get(g);
|
|
118
|
+
if (given)
|
|
119
|
+
return env => given(env);
|
|
120
|
+
const v4params = (o) => { const { type: _t, ...rest } = o; void _t; return Object.keys(rest).length ? rest : undefined; };
|
|
121
|
+
const [n, params] = typeof g === 'string' ? [g, undefined] : typeof g === 'object' && g && typeof g.type === 'string' ? [g.type, v4 ? v4params(g) : g.params] : [undefined, undefined];
|
|
82
122
|
if (n === undefined)
|
|
83
123
|
return refuse(where, `an inline guard${typeof g === 'function' && g.type ? ` (${g.type})` : ''}: name it in setup({guards}) and give its condition in guards`);
|
|
84
124
|
const f = opts.guards?.[n] ?? refuse(where, `the guard ${n}: give its condition in guards`);
|
|
85
125
|
return env => f(env, params);
|
|
86
126
|
};
|
|
87
127
|
const actionsOf = (where, as) => as.flatMap(a => {
|
|
128
|
+
if (opts.inline?.actions?.has(a)) {
|
|
129
|
+
const f = opts.inline.actions.get(a);
|
|
130
|
+
return f ? [(env) => f(env)] : [];
|
|
131
|
+
}
|
|
88
132
|
const [n, params] = typeof a === 'string' ? [a, undefined] : typeof a === 'object' && a && typeof a.type === 'string' ? [a.type, a.params] : [undefined, undefined];
|
|
89
133
|
if (n === undefined)
|
|
90
134
|
return refuse(where, `an inline action${typeof a === 'function' && a.type ? ` (${a.type})` : ''}: name it in setup({actions}) and give the context it writes in actions`);
|
|
@@ -124,7 +168,7 @@ export function fromXState(machine, opts = {}) {
|
|
|
124
168
|
c = c.regions(Object.fromEntries(top.map(r => [r.segs[0], nodeOf(r)])));
|
|
125
169
|
}
|
|
126
170
|
else
|
|
127
|
-
c = c.states(nodeOf(infos[0]).states ??
|
|
171
|
+
c = c.states(nodeOf(infos[0]).states ?? { [STATELESS]: {} }, root.initial ? { initial: root.initial } : {});
|
|
128
172
|
if (opts.events)
|
|
129
173
|
c = c.events(...opts.events);
|
|
130
174
|
// The transitions, in document order (the chart takes the most specific source first, then the first declared, as
|
|
@@ -132,20 +176,32 @@ export function fromXState(machine, opts = {}) {
|
|
|
132
176
|
const writers = new Map();
|
|
133
177
|
for (const i of infos)
|
|
134
178
|
for (const [event, spec] of Object.entries(i.node.on ?? {})) {
|
|
135
|
-
const where = `${pathOf(i) || 'the root'} on ${event}`;
|
|
179
|
+
const where = `${pathOf(i) || 'the root'} on ${event || '""'}`;
|
|
136
180
|
if (event.includes('*'))
|
|
137
181
|
refuse(where, 'a wildcard event');
|
|
182
|
+
if (raised(event))
|
|
183
|
+
refuse(where, event ? 'an event XState raises itself (a done, error or init event)' : "v4's eventless transition (`''`)");
|
|
138
184
|
if (spec === undefined)
|
|
139
185
|
refuse(where, 'a forbidden event (an undefined transition)');
|
|
140
186
|
for (const t of list(spec)) {
|
|
187
|
+
if (t === null || (typeof t !== 'string' && typeof t !== 'object'))
|
|
188
|
+
refuse(where, `a transition given as ${typeof t === 'function' ? 'a function' : JSON.stringify(t)}`);
|
|
141
189
|
const o = typeof t === 'string' ? { target: t } : t, targets = list(o.target);
|
|
190
|
+
keyCheck(o, KEYS.transition, what => refuse(where, what));
|
|
191
|
+
if (o.guard !== undefined && o.cond !== undefined)
|
|
192
|
+
refuse(where, 'both `guard` and v4\'s `cond`');
|
|
193
|
+
// (each XState reads its own and takes a transition with the other's unguarded: refused, not taken so)
|
|
194
|
+
if (o.cond !== undefined && version === 5)
|
|
195
|
+
refuse(where, 'v4\'s `cond` in an XState v5 machine, which doesn\'t read it (it takes this transition unguarded): name the guard in `guard`');
|
|
196
|
+
if (o.guard !== undefined && version === 4)
|
|
197
|
+
refuse(where, 'v5\'s `guard` in an XState v4 machine, which doesn\'t read it (it takes this transition unguarded): name the guard in `cond`');
|
|
142
198
|
if (targets.length > 1)
|
|
143
199
|
refuse(where, 'several targets at once');
|
|
144
200
|
const to = targets[0] === undefined ? undefined : resolve(i, targets[0]);
|
|
145
|
-
const set = composed(actionsOf(where, list(o.actions))), when = guardOf(where, o.guard);
|
|
201
|
+
const set = composed(actionsOf(where, list(o.actions))), when = o.cond !== undefined ? guardOf(where, o.cond, true) : guardOf(where, o.guard);
|
|
146
202
|
// Where it applies: its state; for the root, every top state but a final one (XState stops there), or in a
|
|
147
203
|
// parallel machine the region its target is in (the first region, for one with no target).
|
|
148
|
-
const from = i.segs.length ? pathOf(i) : parallel ? (to ? to.to.segs[0] : top[0].segs[0]) : top.filter(x => x.node.type !== 'final' && x.node.type !== 'history').map(pathOf);
|
|
204
|
+
const from = i.segs.length ? pathOf(i) : parallel ? (to ? to.to.segs[0] : top[0].segs[0]) : top.length ? top.filter(x => x.node.type !== 'final' && x.node.type !== 'history').map(pathOf) : STATELESS;
|
|
149
205
|
if (set) {
|
|
150
206
|
const region = parallel ? String(list(from)[0]).split('.')[0] : 'state';
|
|
151
207
|
writers.set(event, (writers.get(event) ?? new Set()).add(region));
|
|
@@ -158,6 +214,8 @@ export function fromXState(machine, opts = {}) {
|
|
|
158
214
|
refuse(`on ${event}`, `regions ${[...regions].join(' and ')} both write the context (XState runs their actions one after another)`);
|
|
159
215
|
return c;
|
|
160
216
|
}
|
|
217
|
+
// A machine with no states (its root's transitions only): one state of the chart's, XState's value {} there.
|
|
218
|
+
const STATELESS = '(machine)';
|
|
161
219
|
function contextOf(root, given, refuse) {
|
|
162
220
|
const out = {};
|
|
163
221
|
if (root.context !== undefined && typeof root.context !== 'object') {
|
|
@@ -192,7 +250,7 @@ export function conforms(c, machine, createActor, opts = {}) {
|
|
|
192
250
|
const nest = (path) => path.split('.').reduceRight((acc, seg) => (acc === undefined ? seg : { [seg]: acc }), undefined);
|
|
193
251
|
if (root.type === 'parallel')
|
|
194
252
|
return Object.fromEntries(Object.keys(root.states ?? {}).filter(k => root.states[k].type !== 'history').map(k => [k, nest(String(d[k]))]));
|
|
195
|
-
return nest(String(d.state));
|
|
253
|
+
return d.state === STATELESS ? {} : nest(String(d.state));
|
|
196
254
|
};
|
|
197
255
|
const contextKeys = Object.keys((root.context && typeof root.context === 'object' ? root.context : {}));
|
|
198
256
|
let checked = 0;
|
package/dist/kernel/cdcl.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type Lin } from '../core/solver.js';
|
|
1
2
|
import { type Goal, type Path } from './kernel.js';
|
|
2
3
|
import { type Outcome } from './prove.js';
|
|
3
4
|
export declare const cdclStats: {
|
|
@@ -33,6 +34,9 @@ export declare const cdclStats: {
|
|
|
33
34
|
boundRounds: number;
|
|
34
35
|
};
|
|
35
36
|
export declare const REFUTED = "refuted, not unfolded";
|
|
37
|
+
export type Refuted = Outcome & {
|
|
38
|
+
core?: number[];
|
|
39
|
+
};
|
|
36
40
|
export declare function growCdcl(goal: Goal, root: Path, budget: {
|
|
37
41
|
nodes: number;
|
|
38
42
|
until?: number;
|
|
@@ -40,4 +44,15 @@ export declare function growCdcl(goal: Goal, root: Path, budget: {
|
|
|
40
44
|
maxLeaves?: number;
|
|
41
45
|
model?: boolean;
|
|
42
46
|
wall?: number;
|
|
47
|
+
core?: boolean;
|
|
43
48
|
}): Outcome;
|
|
49
|
+
export declare function cdclSession(goal: Goal, root: Path, opts?: {
|
|
50
|
+
maxLeaves?: number;
|
|
51
|
+
model?: boolean;
|
|
52
|
+
core?: boolean;
|
|
53
|
+
}): {
|
|
54
|
+
run: (b: {
|
|
55
|
+
nodes: number;
|
|
56
|
+
until?: number;
|
|
57
|
+
}, w?: number, assume?: Lin) => Outcome;
|
|
58
|
+
};
|