everystate 0.1.0 → 0.3.1
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 +121 -86
- package/bin/everystate.mjs +35 -0
- package/native/darwin-arm64/everystate +0 -0
- package/native/darwin-x64/everystate +0 -0
- package/native/linux-arm64/everystate +0 -0
- package/native/linux-x64/everystate +0 -0
- package/native/win32-x64/everystate.exe +0 -0
- package/package.json +29 -24
- package/skills/formal-verification/SKILL.md +115 -0
- package/skills/formal-verification/example/Charge.cfg +7 -0
- package/skills/formal-verification/example/Charge.tla +61 -0
- package/skills/formal-verification/example/ChargeBroken.cfg +7 -0
- package/wasm/cli.mjs +26 -0
- package/wasm/everystate.d.ts +33 -0
- package/wasm/everystate.mjs +112 -0
- package/wasm/everystate.wasm +0 -0
- package/CONFORMANCE.md +0 -64
- package/dist/bundled.d.ts +0 -1
- package/dist/bundled.js +0 -2
- package/dist/cfg.d.ts +0 -24
- package/dist/cfg.js +0 -252
- package/dist/checker.d.ts +0 -96
- package/dist/checker.js +0 -520
- package/dist/cli.d.ts +0 -2
- package/dist/cli.js +0 -4
- package/dist/community.d.ts +0 -17
- package/dist/community.js +0 -1381
- package/dist/entry.d.ts +0 -2
- package/dist/entry.js +0 -39
- package/dist/eval.d.ts +0 -119
- package/dist/eval.js +0 -1533
- package/dist/index.d.ts +0 -33
- package/dist/index.js +0 -24
- package/dist/java.d.ts +0 -2
- package/dist/java.js +0 -6
- package/dist/lexer.d.ts +0 -17
- package/dist/lexer.js +0 -255
- package/dist/liveness.d.ts +0 -80
- package/dist/liveness.js +0 -855
- package/dist/main.d.ts +0 -40
- package/dist/main.js +0 -494
- package/dist/model.d.ts +0 -56
- package/dist/model.js +0 -582
- package/dist/modules.d.ts +0 -91
- package/dist/modules.js +0 -649
- package/dist/node-host.d.ts +0 -2
- package/dist/node-host.js +0 -25
- package/dist/parser.d.ts +0 -30
- package/dist/parser.js +0 -1114
- package/dist/pprint.d.ts +0 -7
- package/dist/pprint.js +0 -647
- package/dist/stdlib.d.ts +0 -14
- package/dist/stdlib.js +0 -588
- package/dist/values.d.ts +0 -150
- package/dist/values.js +0 -822
package/dist/eval.js
DELETED
|
@@ -1,1533 +0,0 @@
|
|
|
1
|
-
import { LOCAL, tailFn, defLevel } from "./modules.js";
|
|
2
|
-
import { EvalError, SetBase, SetV, IntervalV, SubsetV, DerivedSetV, FcnSetV, RecSetV, CrossV, TupleV, RecordV, FcnV, ModelValue, valEq, compare, bsearch, sortDedup, mkSet, makeFcn, makeRecord, domainOf, fmt, strId, EMPTY_SET, } from "./values.js";
|
|
3
|
-
import { TlaError } from "./lexer.js";
|
|
4
|
-
import { ppr } from "./pprint.js";
|
|
5
|
-
export class Ctx {
|
|
6
|
-
name;
|
|
7
|
-
val;
|
|
8
|
-
next;
|
|
9
|
-
base; // head of the enclosing instance environment (substitutions), or null at the root
|
|
10
|
-
constructor(name, val, next) {
|
|
11
|
-
this.name = name;
|
|
12
|
-
this.val = val;
|
|
13
|
-
this.next = next;
|
|
14
|
-
this.base = next === null ? null : next.base;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
const cons = (name, val, next) => new Ctx(name, val, next);
|
|
18
|
-
// A delayed expression (operator argument, LET definition, instance substitution).
|
|
19
|
-
export class Lazy {
|
|
20
|
-
e;
|
|
21
|
-
ctx;
|
|
22
|
-
v = undefined;
|
|
23
|
-
st = null; // state the cached value is valid for; ANY = constant
|
|
24
|
-
constructor(e, ctx) { this.e = e; this.ctx = ctx; }
|
|
25
|
-
}
|
|
26
|
-
const ANY = { any: true };
|
|
27
|
-
export class Closure {
|
|
28
|
-
params;
|
|
29
|
-
body;
|
|
30
|
-
ctx;
|
|
31
|
-
name;
|
|
32
|
-
constructor(params, body, ctx, name = 'LAMBDA') { this.params = params; this.body = body; this.ctx = ctx; this.name = name; }
|
|
33
|
-
describe() { return `<operator ${this.name}>`; }
|
|
34
|
-
}
|
|
35
|
-
export class DefRef {
|
|
36
|
-
def;
|
|
37
|
-
base;
|
|
38
|
-
constructor(def, base) { this.def = def; this.base = base; }
|
|
39
|
-
describe() { return `<operator ${this.def.name}>`; }
|
|
40
|
-
}
|
|
41
|
-
// A lazily evaluated function (recursive function definitions, functions over non-enumerable domains).
|
|
42
|
-
export class LazyFcn {
|
|
43
|
-
node;
|
|
44
|
-
ctx;
|
|
45
|
-
ev;
|
|
46
|
-
memo = new Map();
|
|
47
|
-
forced = undefined;
|
|
48
|
-
constructor(node, ctx, ev) {
|
|
49
|
-
this.node = node;
|
|
50
|
-
this.ev = ev;
|
|
51
|
-
this.ctx = node.recname ? cons(node.recname, this, ctx) : ctx;
|
|
52
|
-
}
|
|
53
|
-
describe() { return '<lazy function>'; }
|
|
54
|
-
show() {
|
|
55
|
-
try {
|
|
56
|
-
const f = this.force();
|
|
57
|
-
if (f !== this)
|
|
58
|
-
return ppr(fmt(f));
|
|
59
|
-
}
|
|
60
|
-
catch (e) {
|
|
61
|
-
if (!(e instanceof TlaError))
|
|
62
|
-
throw e;
|
|
63
|
-
}
|
|
64
|
-
return this.describe();
|
|
65
|
-
}
|
|
66
|
-
bind(arg) {
|
|
67
|
-
const ev = this.ev;
|
|
68
|
-
const bounds = this.node.bounds;
|
|
69
|
-
let c = this.ctx;
|
|
70
|
-
const one = bounds.length === 1 && (bounds[0].tuple !== null || bounds[0].names.length === 1);
|
|
71
|
-
if (one) {
|
|
72
|
-
const b = bounds[0];
|
|
73
|
-
const s = ev.evalSet(b.set, this.ctx);
|
|
74
|
-
if (!s.contains(arg))
|
|
75
|
-
throw new EvalError(`In applying the function\n${this.show()},\nthe first argument is:\n${ppr(fmt(arg))}\nwhich is not in its domain.\n`);
|
|
76
|
-
c = b.tuple ? ev.bindTuple(b.tuple, arg, c) : cons(b.names[0], arg, c);
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
if (!(arg instanceof TupleV))
|
|
80
|
-
throw new EvalError(`Attempted to apply function to argument ${fmt(arg)}, which is not in the domain of the function.`);
|
|
81
|
-
let i = 0;
|
|
82
|
-
for (const b of bounds) {
|
|
83
|
-
const s = ev.evalSet(b.set, this.ctx);
|
|
84
|
-
const cnt = b.tuple ? 1 : b.names.length;
|
|
85
|
-
for (let j = 0; j < cnt; j++) {
|
|
86
|
-
const a = arg.e[i++];
|
|
87
|
-
if (a === undefined || !s.contains(a))
|
|
88
|
-
throw new EvalError(`Attempted to apply function to argument ${fmt(arg)}, which is not in the domain of the function.`);
|
|
89
|
-
c = b.tuple ? ev.bindTuple(b.tuple, a, c) : cons(b.names[j], a, c);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
if (i !== arg.e.length)
|
|
93
|
-
throw new EvalError(`Attempted to apply function to argument ${fmt(arg)}, which is not in the domain of the function.`);
|
|
94
|
-
}
|
|
95
|
-
return c;
|
|
96
|
-
}
|
|
97
|
-
// [f EXCEPT ![k] = v] on a function that is not enumerated: later entries win
|
|
98
|
-
over = null;
|
|
99
|
-
withOverride(key, val) {
|
|
100
|
-
const g = new LazyFcn(this.node, null, this.ev);
|
|
101
|
-
g.ctx = this.ctx;
|
|
102
|
-
g.memo = this.memo;
|
|
103
|
-
g.over = this.over ? [...this.over, [key, val]] : [[key, val]];
|
|
104
|
-
return g;
|
|
105
|
-
}
|
|
106
|
-
// `lazy`: the caller keeps a function-valued result unevaluated (it is applied right away)
|
|
107
|
-
apply(arg, lazy = false) {
|
|
108
|
-
if (this.over !== null) {
|
|
109
|
-
for (let i = this.over.length - 1; i >= 0; i--)
|
|
110
|
-
if (valEq(this.over[i][0], arg))
|
|
111
|
-
return this.over[i][1];
|
|
112
|
-
}
|
|
113
|
-
const ev = this.ev;
|
|
114
|
-
const key = typeof arg === 'number' || typeof arg === 'string' ? arg : '\u0001' + fmt(arg);
|
|
115
|
-
const hit = this.memo.get(key);
|
|
116
|
-
if (hit !== undefined)
|
|
117
|
-
return hit;
|
|
118
|
-
const c = this.bind(arg);
|
|
119
|
-
const s0 = ev.stTouch, p0 = ev.prTouch;
|
|
120
|
-
// like TLC, the body of a recursive function is evaluated in keep-lazy mode
|
|
121
|
-
const v = lazy || this.node.isRec ? ev.evalLazy(this.node.body, c) : ev.eval(this.node.body, c);
|
|
122
|
-
if (ev.stTouch === s0 && ev.prTouch === p0)
|
|
123
|
-
this.memo.set(key, v);
|
|
124
|
-
return v;
|
|
125
|
-
}
|
|
126
|
-
domain() {
|
|
127
|
-
const bounds = this.node.bounds;
|
|
128
|
-
const sets = [];
|
|
129
|
-
for (const b of bounds) {
|
|
130
|
-
const s = this.ev.evalSet(b.set, this.ctx);
|
|
131
|
-
const cnt = b.tuple ? 1 : b.names.length;
|
|
132
|
-
for (let j = 0; j < cnt; j++)
|
|
133
|
-
sets.push(s);
|
|
134
|
-
}
|
|
135
|
-
return sets.length === 1 ? sets[0] : new CrossV(sets);
|
|
136
|
-
}
|
|
137
|
-
force() {
|
|
138
|
-
if (this.forced !== undefined)
|
|
139
|
-
return this.forced;
|
|
140
|
-
const ev = this.ev;
|
|
141
|
-
const s0 = ev.stTouch, p0 = ev.prTouch;
|
|
142
|
-
const ds = this.domain();
|
|
143
|
-
if (!ds.enumerable())
|
|
144
|
-
return this; // stays lazy: only application and EXCEPT are possible
|
|
145
|
-
const dom = ds.elems();
|
|
146
|
-
const vals = dom.map(d => { const v = this.apply(d); return typeof v === 'object' && v instanceof LazyFcn ? v.force() : v; });
|
|
147
|
-
const f = makeFcn(dom, vals);
|
|
148
|
-
if (ev.stTouch === s0 && ev.prTouch === p0)
|
|
149
|
-
this.forced = f;
|
|
150
|
-
return f;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
const KINDS = ['num', 'str', 'bool', 'app', 'and', 'or', 'not', 'imp', 'eqv', 'eq', 'ne', 'in', 'notin', 'subseteq',
|
|
154
|
-
'cup', 'cap', 'diff', 'powerset', 'bigunion', 'domain', 'prime', 'enabled', 'unchanged', 'cross', 'if', 'case',
|
|
155
|
-
'let', 'quant', 'choose', 'setenum', 'setfilter', 'setmap', 'fndef', 'fnset', 'rec', 'recset', 'except', 'tuple',
|
|
156
|
-
'fnapp', 'dot', 'boxact', 'angleact', 'lambda', 'at', 'always', 'eventually', 'leadsto', 'guarantee', 'wf', 'sf',
|
|
157
|
-
'tquant', 'cdot', 'stepref'];
|
|
158
|
-
const OP = {};
|
|
159
|
-
KINDS.forEach((k, i) => { OP[k] = i; });
|
|
160
|
-
const O = OP;
|
|
161
|
-
const O_num = O.num, O_str = O.str, O_bool = O.bool, O_app = O.app, O_and = O.and, O_or = O.or, O_not = O.not, O_imp = O.imp, O_eqv = O.eqv, O_eq = O.eq, O_ne = O.ne, O_in = O.in, O_notin = O.notin, O_subseteq = O.subseteq, O_cup = O.cup, O_cap = O.cap, O_diff = O.diff, O_powerset = O.powerset, O_bigunion = O.bigunion, O_domain = O.domain, O_prime = O.prime, O_enabled = O.enabled, O_unchanged = O.unchanged, O_cross = O.cross, O_if = O.if, O_case = O.case, O_let = O.let, O_quant = O.quant, O_choose = O.choose, O_setenum = O.setenum, O_setfilter = O.setfilter, O_setmap = O.setmap, O_fndef = O.fndef, O_fnset = O.fnset, O_rec = O.rec, O_recset = O.recset, O_except = O.except, O_tuple = O.tuple, O_fnapp = O.fnapp, O_dot = O.dot, O_boxact = O.boxact, O_angleact = O.angleact, O_lambda = O.lambda, O_at = O.at;
|
|
162
|
-
const ENABLED_MARK = '\u0000enabled';
|
|
163
|
-
const reducible = (s) => s instanceof SetV || s instanceof IntervalV;
|
|
164
|
-
class Found extends Error {
|
|
165
|
-
}
|
|
166
|
-
const FOUND = new Found();
|
|
167
|
-
export class Evaluator {
|
|
168
|
-
nvars = 0;
|
|
169
|
-
varNames = [];
|
|
170
|
-
cur = []; // current state (unprimed variables)
|
|
171
|
-
nxt = null; // next state (primed variables); may be partial during enumeration
|
|
172
|
-
initMode = false; // enumerating initial states: `cur` is partial and mutable
|
|
173
|
-
primeDepth = 0;
|
|
174
|
-
stTouch = 0;
|
|
175
|
-
prTouch = 0;
|
|
176
|
-
hooks = { random: Math.random };
|
|
177
|
-
callDepth = 0;
|
|
178
|
-
nAssigned = 0; // number of target variables assigned during enumeration
|
|
179
|
-
inEnabled = 0;
|
|
180
|
-
extraGenerated = 0; // intermediate states of action compositions (TLC counts them as generated)
|
|
181
|
-
scratch = new Map(); // ENABLED: primed variables of instantiated modules are existentially bound
|
|
182
|
-
fail(msg, n) {
|
|
183
|
-
const e = new EvalError(msg);
|
|
184
|
-
if (n) {
|
|
185
|
-
e.loc = n.loc;
|
|
186
|
-
e.module = n.mod;
|
|
187
|
-
}
|
|
188
|
-
throw e;
|
|
189
|
-
}
|
|
190
|
-
// ---------- context helpers ----------
|
|
191
|
-
lookup(ctx, name) {
|
|
192
|
-
for (let c = ctx; c !== null; c = c.next)
|
|
193
|
-
if (c.name === name)
|
|
194
|
-
return c.val;
|
|
195
|
-
return this.fail(`Internal error: unbound local identifier ${name}`);
|
|
196
|
-
}
|
|
197
|
-
envLookup(base, name) {
|
|
198
|
-
for (let c = base; c !== null; c = c.next)
|
|
199
|
-
if (c.name === name)
|
|
200
|
-
return c.val;
|
|
201
|
-
return undefined;
|
|
202
|
-
}
|
|
203
|
-
bindTuple(names, v, ctx) {
|
|
204
|
-
if (!(v instanceof TupleV) || v.e.length !== names.length) {
|
|
205
|
-
return this.fail(`Attempted to match the tuple pattern <<${names.join(', ')}>> against the value ${fmt(v)}`);
|
|
206
|
-
}
|
|
207
|
-
let c = ctx;
|
|
208
|
-
for (let i = 0; i < names.length; i++)
|
|
209
|
-
c = cons(names[i], v.e[i], c);
|
|
210
|
-
return c;
|
|
211
|
-
}
|
|
212
|
-
force(lz, lazy = false) {
|
|
213
|
-
if (lz.v !== undefined) {
|
|
214
|
-
if (lz.st === ANY)
|
|
215
|
-
return lz.v;
|
|
216
|
-
if (lz.st === this.cur && !this.initMode && this.primeDepth === 0) {
|
|
217
|
-
this.stTouch++;
|
|
218
|
-
return lz.v;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
const s0 = this.stTouch, p0 = this.prTouch;
|
|
222
|
-
const v = lazy ? this.evalLazy(lz.e, lz.ctx) : this.eval(lz.e, lz.ctx);
|
|
223
|
-
if (this.prTouch === p0) {
|
|
224
|
-
if (this.stTouch === s0) {
|
|
225
|
-
lz.v = v;
|
|
226
|
-
lz.st = ANY;
|
|
227
|
-
}
|
|
228
|
-
else if (!this.initMode && this.primeDepth === 0) {
|
|
229
|
-
lz.v = v;
|
|
230
|
-
lz.st = this.cur;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
return v;
|
|
234
|
-
}
|
|
235
|
-
// Turn an argument expression into a thunk / value / operator.
|
|
236
|
-
mkArg(n, ctx) {
|
|
237
|
-
switch (n.k) {
|
|
238
|
-
case 'num':
|
|
239
|
-
case 'str':
|
|
240
|
-
case 'bool': return n.v;
|
|
241
|
-
case 'lambda': return new Closure(n.params, n.body, ctx);
|
|
242
|
-
case 'app':
|
|
243
|
-
if (n.args.length === 0 && !n.sel) {
|
|
244
|
-
const r = n.r;
|
|
245
|
-
if (r === LOCAL)
|
|
246
|
-
return this.lookup(ctx, n.name);
|
|
247
|
-
if (r.k === 'def') {
|
|
248
|
-
const d = r.override ?? r;
|
|
249
|
-
if (d.params.length > 0)
|
|
250
|
-
return new DefRef(d, ctx === null ? null : ctx.base);
|
|
251
|
-
}
|
|
252
|
-
else if (r.k === 'const') {
|
|
253
|
-
const base = ctx === null ? null : ctx.base;
|
|
254
|
-
if (base !== null) {
|
|
255
|
-
const b = this.envLookup(base, n.name);
|
|
256
|
-
if (b !== undefined)
|
|
257
|
-
return b;
|
|
258
|
-
}
|
|
259
|
-
if (r.subst)
|
|
260
|
-
return r.subst.params.length > 0 ? new DefRef(r.subst, null) : new Lazy(n, ctx);
|
|
261
|
-
if (r.hasValue)
|
|
262
|
-
return r.value;
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
return new Lazy(n, ctx);
|
|
267
|
-
}
|
|
268
|
-
unthunk(t) {
|
|
269
|
-
if (t instanceof Lazy) {
|
|
270
|
-
const v = this.force(t);
|
|
271
|
-
return typeof v === 'object' && v instanceof LazyFcn ? v.force() : v;
|
|
272
|
-
}
|
|
273
|
-
return t;
|
|
274
|
-
}
|
|
275
|
-
buildEnv(def, thunks, callerBase) {
|
|
276
|
-
const inst = def.inst;
|
|
277
|
-
if (inst.nparams === 0 && callerBase === null && inst.envCache !== undefined)
|
|
278
|
-
return inst.envCache;
|
|
279
|
-
let pc = callerBase;
|
|
280
|
-
for (let i = 0; i < inst.nparams; i++)
|
|
281
|
-
pc = cons(def.params[i].name, thunks[i], pc);
|
|
282
|
-
let head = null;
|
|
283
|
-
const nodes = [];
|
|
284
|
-
for (const s of inst.substs) {
|
|
285
|
-
head = new Ctx(s.name, this.mkArg(s.e, pc), head);
|
|
286
|
-
nodes.push(head);
|
|
287
|
-
}
|
|
288
|
-
for (const nd of nodes)
|
|
289
|
-
nd.base = head;
|
|
290
|
-
if (inst.nparams === 0 && callerBase === null)
|
|
291
|
-
inst.envCache = head;
|
|
292
|
-
return head;
|
|
293
|
-
}
|
|
294
|
-
// Expand a definition application into (body, context) without evaluating it.
|
|
295
|
-
expandDef(def, thunks, base, viaInstance = false) {
|
|
296
|
-
// A config override (Op <- Other) replaces references to Op itself; an instantiated copy (I!Op) keeps the
|
|
297
|
-
// original body, as in TLC where the override is attached to the definition node, not to its body.
|
|
298
|
-
if (def.override && !(viaInstance && !def.override.builtin))
|
|
299
|
-
def = def.override;
|
|
300
|
-
if (def.builtin)
|
|
301
|
-
return null;
|
|
302
|
-
if (def.inst) {
|
|
303
|
-
const env = this.buildEnv(def, thunks, base);
|
|
304
|
-
const rest = def.inst.nparams ? thunks.slice(def.inst.nparams) : thunks;
|
|
305
|
-
const r = this.expandDef(def.inst.orig, rest, env, true);
|
|
306
|
-
return r;
|
|
307
|
-
}
|
|
308
|
-
const ps = def.params;
|
|
309
|
-
if (ps.length !== thunks.length)
|
|
310
|
-
this.fail(`The operator ${def.name} expects ${ps.length} arguments but was given ${thunks.length}.`);
|
|
311
|
-
let c = base;
|
|
312
|
-
for (let i = 0; i < ps.length; i++)
|
|
313
|
-
c = cons(ps[i].name, thunks[i], c);
|
|
314
|
-
return { body: def.body, ctx: c };
|
|
315
|
-
}
|
|
316
|
-
// Expand an `app` node one step: returns the body/context it denotes, or null if it is a plain value or builtin.
|
|
317
|
-
expandApp(n, ctx) {
|
|
318
|
-
const r = n.r;
|
|
319
|
-
let res = null;
|
|
320
|
-
if (r === LOCAL) {
|
|
321
|
-
const b = this.lookup(ctx, n.name);
|
|
322
|
-
if (b instanceof Lazy)
|
|
323
|
-
res = { body: b.e, ctx: b.ctx };
|
|
324
|
-
else if (b instanceof Closure || b instanceof DefRef)
|
|
325
|
-
res = this.expandOp(b, n.args.map((a) => this.mkArg(a, ctx)));
|
|
326
|
-
}
|
|
327
|
-
else if (r.k === 'def') {
|
|
328
|
-
res = this.expandDef(r, n.args.length ? n.args.map((a) => this.mkArg(a, ctx)) : NOARGS, ctx === null ? null : ctx.base);
|
|
329
|
-
}
|
|
330
|
-
else {
|
|
331
|
-
const base = ctx === null ? null : ctx.base;
|
|
332
|
-
const b = base !== null ? this.envLookup(base, n.name) : undefined;
|
|
333
|
-
if (b !== undefined) {
|
|
334
|
-
if (b instanceof Lazy)
|
|
335
|
-
res = { body: b.e, ctx: b.ctx };
|
|
336
|
-
else if (b instanceof Closure || b instanceof DefRef)
|
|
337
|
-
res = this.expandOp(b, n.args.map((a) => this.mkArg(a, ctx)));
|
|
338
|
-
}
|
|
339
|
-
else if (r.k === 'const' && r.subst) {
|
|
340
|
-
res = this.expandDef(r.subst, n.args.map((a) => this.mkArg(a, ctx)), null);
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
if (res && n.sel)
|
|
344
|
-
res = this.select(res, n);
|
|
345
|
-
return res;
|
|
346
|
-
}
|
|
347
|
-
expandOp(op, thunks) {
|
|
348
|
-
if (op instanceof DefRef)
|
|
349
|
-
return this.expandDef(op.def, thunks, op.base);
|
|
350
|
-
if (op.params.length !== thunks.length)
|
|
351
|
-
this.fail(`The operator ${op.name} expects ${op.params.length} arguments but was given ${thunks.length}.`);
|
|
352
|
-
let c = op.ctx;
|
|
353
|
-
for (let i = 0; i < op.params.length; i++)
|
|
354
|
-
c = cons(op.params[i].name, thunks[i], c);
|
|
355
|
-
return { body: op.body, ctx: c };
|
|
356
|
-
}
|
|
357
|
-
// Subexpression selectors: Op!1, Op!<<, Op!>> ...
|
|
358
|
-
select(res, n) {
|
|
359
|
-
let body = res.body;
|
|
360
|
-
for (const s of n.sel) {
|
|
361
|
-
const kids = body.a ?? body.args ?? (body.body ? [body.body] : undefined);
|
|
362
|
-
if (s === ':')
|
|
363
|
-
continue;
|
|
364
|
-
const i = s === '<<' ? 0 : s === '>>' ? 1 : typeof s === 'number' ? s - 1 : -1;
|
|
365
|
-
if (!kids || i < 0 || i >= kids.length)
|
|
366
|
-
this.fail(`Unsupported subexpression selector in ${n.name}`, n);
|
|
367
|
-
body = kids[i];
|
|
368
|
-
}
|
|
369
|
-
return { body, ctx: res.ctx };
|
|
370
|
-
}
|
|
371
|
-
applyOp(op, args) {
|
|
372
|
-
if (op instanceof DefRef) {
|
|
373
|
-
const d = op.def.override ?? op.def;
|
|
374
|
-
if (d.builtin)
|
|
375
|
-
return d.builtin.lazy ? d.builtin.fn(this, args) : d.builtin.fn(this, args.map(a => this.unthunk(a)));
|
|
376
|
-
}
|
|
377
|
-
else if (!(op instanceof Closure))
|
|
378
|
-
return this.fail(`Attempted to apply a non-operator value ${fmt(op)} as an operator.`);
|
|
379
|
-
const r = this.expandOp(op, args);
|
|
380
|
-
if (r === null) {
|
|
381
|
-
// instance of a builtin operator (e.g. LET T == INSTANCE TLC IN T!PrintT(x))
|
|
382
|
-
let d = op.def;
|
|
383
|
-
d = d.override ?? d;
|
|
384
|
-
while (d.inst) {
|
|
385
|
-
args = d.inst.nparams ? args.slice(d.inst.nparams) : args;
|
|
386
|
-
d = d.inst.orig;
|
|
387
|
-
d = d.override ?? d;
|
|
388
|
-
}
|
|
389
|
-
const bi = d.builtin;
|
|
390
|
-
return bi.lazy ? bi.fn(this, args) : bi.fn(this, args.map((a, i) => bi.ops && bi.ops.includes(i) ? a : this.unthunk(a)));
|
|
391
|
-
}
|
|
392
|
-
return this.eval(r.body, r.ctx);
|
|
393
|
-
}
|
|
394
|
-
applyDef(def, n, ctx) {
|
|
395
|
-
if (def.override)
|
|
396
|
-
def = def.override;
|
|
397
|
-
const bi = def.builtin;
|
|
398
|
-
if (bi) {
|
|
399
|
-
const args = new Array(n.args.length);
|
|
400
|
-
if (bi.lazy)
|
|
401
|
-
for (let i = 0; i < args.length; i++)
|
|
402
|
-
args[i] = this.mkArg(n.args[i], ctx);
|
|
403
|
-
else if (bi.ops)
|
|
404
|
-
for (let i = 0; i < args.length; i++)
|
|
405
|
-
args[i] = bi.ops.includes(i) ? this.mkArg(n.args[i], ctx) : this.eval(n.args[i], ctx);
|
|
406
|
-
else
|
|
407
|
-
for (let i = 0; i < args.length; i++)
|
|
408
|
-
args[i] = this.eval(n.args[i], ctx);
|
|
409
|
-
return bi.fn(this, args);
|
|
410
|
-
}
|
|
411
|
-
const base = ctx === null ? null : ctx.base;
|
|
412
|
-
if (def.params.length === 0 && !def.inst) {
|
|
413
|
-
if (base === null) {
|
|
414
|
-
if (def.cached)
|
|
415
|
-
return def.cache;
|
|
416
|
-
const s0 = this.stTouch, p0 = this.prTouch;
|
|
417
|
-
const v = this.eval(def.body, null);
|
|
418
|
-
if (this.stTouch === s0 && this.prTouch === p0) {
|
|
419
|
-
def.cached = true;
|
|
420
|
-
def.cache = v;
|
|
421
|
-
}
|
|
422
|
-
return v;
|
|
423
|
-
}
|
|
424
|
-
return this.eval(def.body, base);
|
|
425
|
-
}
|
|
426
|
-
const r = this.expandDef(def, n.args.length ? n.args.map((a) => this.mkArg(a, ctx)) : NOARGS, base);
|
|
427
|
-
if (r === null) {
|
|
428
|
-
// instance of a builtin operator
|
|
429
|
-
let d = def;
|
|
430
|
-
while (d.inst)
|
|
431
|
-
d = d.inst.orig;
|
|
432
|
-
return this.applyDef(d, n, ctx);
|
|
433
|
-
}
|
|
434
|
-
if (++this.callDepth > 6000) {
|
|
435
|
-
this.callDepth = 0;
|
|
436
|
-
this.fail(`Evaluation of ${def.name} exceeded the maximum recursion depth; the definition is probably non-terminating.`);
|
|
437
|
-
}
|
|
438
|
-
const v = this.eval(r.body, r.ctx);
|
|
439
|
-
this.callDepth--;
|
|
440
|
-
return v;
|
|
441
|
-
}
|
|
442
|
-
evalApp(n, ctx) {
|
|
443
|
-
const r = n.r;
|
|
444
|
-
if (n.sel) {
|
|
445
|
-
const x = this.expandApp(n, ctx);
|
|
446
|
-
if (!x)
|
|
447
|
-
return this.fail('Unsupported subexpression selector', n);
|
|
448
|
-
return this.eval(x.body, x.ctx);
|
|
449
|
-
}
|
|
450
|
-
if (r === LOCAL) {
|
|
451
|
-
const b = this.lookup(ctx, n.name);
|
|
452
|
-
if (b instanceof Lazy)
|
|
453
|
-
return this.force(b);
|
|
454
|
-
if (b instanceof Closure || b instanceof DefRef) {
|
|
455
|
-
if (n.args.length === 0 && (b instanceof Closure ? b.params.length : (b.def.override ?? b.def).params.length) > 0)
|
|
456
|
-
return b;
|
|
457
|
-
return this.applyOp(b, n.args.map((a) => this.mkArg(a, ctx)));
|
|
458
|
-
}
|
|
459
|
-
return b;
|
|
460
|
-
}
|
|
461
|
-
if (r.k === 'def')
|
|
462
|
-
return this.applyDef(r, n, ctx);
|
|
463
|
-
const base = ctx === null ? null : ctx.base;
|
|
464
|
-
if (base !== null) {
|
|
465
|
-
const b = this.envLookup(base, n.name);
|
|
466
|
-
if (b !== undefined) {
|
|
467
|
-
if (this.inEnabled > 0 && this.primeDepth > 0 && r.k === 'var' && b instanceof Lazy) {
|
|
468
|
-
const idx = this.asVar(n, ctx);
|
|
469
|
-
if (idx >= 0) {
|
|
470
|
-
const v = this.cur[idx];
|
|
471
|
-
this.prTouch++;
|
|
472
|
-
if (v === undefined)
|
|
473
|
-
return this.fail(`In evaluation, the identifier ${r.name} is either undefined or not an operator.`, n);
|
|
474
|
-
return v;
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
if (b instanceof Lazy)
|
|
478
|
-
return this.force(b);
|
|
479
|
-
if (b instanceof Closure || b instanceof DefRef)
|
|
480
|
-
return this.applyOp(b, n.args.map((a) => this.mkArg(a, ctx)));
|
|
481
|
-
return b;
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
if (r.k === 'var') {
|
|
485
|
-
const v = this.cur[r.idx];
|
|
486
|
-
if (this.primeDepth > 0)
|
|
487
|
-
this.prTouch++;
|
|
488
|
-
else
|
|
489
|
-
this.stTouch++;
|
|
490
|
-
if (v === undefined) {
|
|
491
|
-
return this.fail(this.primeDepth > 0
|
|
492
|
-
? `In evaluation, the identifier ${r.name} is either undefined or not an operator.` // primed variable not yet assigned
|
|
493
|
-
: `In evaluation, the identifier ${r.name} is either undefined or not an operator.`, n);
|
|
494
|
-
}
|
|
495
|
-
return v;
|
|
496
|
-
}
|
|
497
|
-
// constant
|
|
498
|
-
if (r.subst) {
|
|
499
|
-
const d = r.subst;
|
|
500
|
-
if (d.params.length === 0 && n.args.length === 0)
|
|
501
|
-
return this.applyDef(d, n, null);
|
|
502
|
-
const x = this.expandDef(d, n.args.map((a) => this.mkArg(a, ctx)), null);
|
|
503
|
-
if (!x)
|
|
504
|
-
return this.applyDef(d, n, ctx);
|
|
505
|
-
return this.eval(x.body, x.ctx);
|
|
506
|
-
}
|
|
507
|
-
if (r.hasValue)
|
|
508
|
-
return r.value;
|
|
509
|
-
return this.fail(`The constant parameter ${r.name} is not assigned a value by the configuration file.`, n);
|
|
510
|
-
}
|
|
511
|
-
// ---------- evaluation ----------
|
|
512
|
-
evalBool(n, ctx) {
|
|
513
|
-
const v = this.eval(n, ctx);
|
|
514
|
-
if (typeof v !== 'boolean')
|
|
515
|
-
return this.fail(`Attempted to evaluate an expression as a boolean, but it was the non-boolean value:\n${fmt(v)}`, n);
|
|
516
|
-
return v;
|
|
517
|
-
}
|
|
518
|
-
evalSet(n, ctx) {
|
|
519
|
-
const v = this.eval(n, ctx);
|
|
520
|
-
if (!(v instanceof SetBase))
|
|
521
|
-
return this.fail(`Attempted to use the non-set value:\n${fmt(v)}\nas a set.`, n);
|
|
522
|
-
return v;
|
|
523
|
-
}
|
|
524
|
-
evalElems(n, ctx) {
|
|
525
|
-
const s = this.evalSet(n, ctx);
|
|
526
|
-
try {
|
|
527
|
-
return s.elems();
|
|
528
|
-
}
|
|
529
|
-
catch (e) {
|
|
530
|
-
if (e instanceof TlaError && !e.loc) {
|
|
531
|
-
e.loc = n.loc;
|
|
532
|
-
e.module = n.mod;
|
|
533
|
-
}
|
|
534
|
-
throw e;
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
// Iterate over all bindings of a list of bounds; f returns true to stop. Returns true if stopped.
|
|
538
|
-
forBindings(bounds, ctx, f) {
|
|
539
|
-
if (bounds.length === 1) {
|
|
540
|
-
const b = bounds[0];
|
|
541
|
-
if (b.set === null)
|
|
542
|
-
return this.fail('TLC cannot handle unbounded quantification.');
|
|
543
|
-
const es = this.evalElems(b.set, ctx);
|
|
544
|
-
if (b.tuple) {
|
|
545
|
-
for (let i = 0; i < es.length; i++)
|
|
546
|
-
if (f(this.bindTuple(b.tuple, es[i], ctx)))
|
|
547
|
-
return true;
|
|
548
|
-
return false;
|
|
549
|
-
}
|
|
550
|
-
if (b.names.length === 1) {
|
|
551
|
-
const name = b.names[0];
|
|
552
|
-
for (let i = 0; i < es.length; i++)
|
|
553
|
-
if (f(new Ctx(name, es[i], ctx)))
|
|
554
|
-
return true;
|
|
555
|
-
return false;
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
const sets = bounds.map(b => {
|
|
559
|
-
if (b.set === null)
|
|
560
|
-
return this.fail('TLC cannot handle unbounded quantification.');
|
|
561
|
-
return this.evalElems(b.set, ctx);
|
|
562
|
-
});
|
|
563
|
-
// TLC varies the FIRST bound variable fastest, so the last one is the outermost loop.
|
|
564
|
-
const flat = [];
|
|
565
|
-
bounds.forEach((b, i) => {
|
|
566
|
-
if (b.tuple)
|
|
567
|
-
flat.push({ name: null, tuple: b.tuple, es: sets[i] });
|
|
568
|
-
else
|
|
569
|
-
for (const nm of b.names)
|
|
570
|
-
flat.push({ name: nm, tuple: null, es: sets[i] });
|
|
571
|
-
});
|
|
572
|
-
const rec = (i, c) => {
|
|
573
|
-
if (i < 0)
|
|
574
|
-
return f(c);
|
|
575
|
-
const b = flat[i];
|
|
576
|
-
for (const v of b.es)
|
|
577
|
-
if (rec(i - 1, b.tuple ? this.bindTuple(b.tuple, v, c) : cons(b.name, v, c)))
|
|
578
|
-
return true;
|
|
579
|
-
return false;
|
|
580
|
-
};
|
|
581
|
-
return rec(flat.length - 1, ctx);
|
|
582
|
-
}
|
|
583
|
-
bindLet(defs, ctx) {
|
|
584
|
-
let c = ctx;
|
|
585
|
-
const made = [];
|
|
586
|
-
for (const d of defs) {
|
|
587
|
-
if (d.k !== 'def') {
|
|
588
|
-
for (const def of d.instDefs) {
|
|
589
|
-
const x = new DefRef(def, null);
|
|
590
|
-
made.push(x);
|
|
591
|
-
c = cons(def.name, x, c);
|
|
592
|
-
}
|
|
593
|
-
continue;
|
|
594
|
-
}
|
|
595
|
-
const x = d.params.length === 0 ? new Lazy(d.body, null) : new Closure(d.params, d.body, null, d.name);
|
|
596
|
-
made.push(x);
|
|
597
|
-
c = cons(d.name, x, c);
|
|
598
|
-
}
|
|
599
|
-
for (const x of made) {
|
|
600
|
-
if (x instanceof DefRef)
|
|
601
|
-
x.base = c;
|
|
602
|
-
else
|
|
603
|
-
x.ctx = c;
|
|
604
|
-
}
|
|
605
|
-
return c;
|
|
606
|
-
}
|
|
607
|
-
eval(n, ctx) {
|
|
608
|
-
let o = n.o;
|
|
609
|
-
if (o === undefined)
|
|
610
|
-
o = n.o = OP[n.k];
|
|
611
|
-
switch (o) {
|
|
612
|
-
case O_num:
|
|
613
|
-
case O_str:
|
|
614
|
-
case O_bool: return n.v;
|
|
615
|
-
case O_app: {
|
|
616
|
-
const v = this.evalApp(n, ctx);
|
|
617
|
-
if (typeof v === 'object' && v instanceof LazyFcn)
|
|
618
|
-
return v.force();
|
|
619
|
-
return v;
|
|
620
|
-
}
|
|
621
|
-
case O_and: {
|
|
622
|
-
const a = n.a;
|
|
623
|
-
for (let i = 0; i < a.length; i++)
|
|
624
|
-
if (!this.evalBool(a[i], ctx))
|
|
625
|
-
return false;
|
|
626
|
-
return true;
|
|
627
|
-
}
|
|
628
|
-
case O_or: {
|
|
629
|
-
const a = n.a;
|
|
630
|
-
for (let i = 0; i < a.length; i++)
|
|
631
|
-
if (this.evalBool(a[i], ctx))
|
|
632
|
-
return true;
|
|
633
|
-
return false;
|
|
634
|
-
}
|
|
635
|
-
case O_not: return !this.evalBool(n.a[0], ctx);
|
|
636
|
-
case O_imp: return !this.evalBool(n.a[0], ctx) || this.evalBool(n.a[1], ctx);
|
|
637
|
-
case O_eqv: return this.evalBool(n.a[0], ctx) === this.evalBool(n.a[1], ctx);
|
|
638
|
-
case O_eq: return valEq(this.eval(n.a[0], ctx), this.eval(n.a[1], ctx));
|
|
639
|
-
case O_ne: return !valEq(this.eval(n.a[0], ctx), this.eval(n.a[1], ctx));
|
|
640
|
-
case O_in: {
|
|
641
|
-
const x = this.eval(n.a[0], ctx);
|
|
642
|
-
return this.evalSet(n.a[1], ctx).contains(x);
|
|
643
|
-
}
|
|
644
|
-
case O_notin: {
|
|
645
|
-
const x = this.eval(n.a[0], ctx);
|
|
646
|
-
return !this.evalSet(n.a[1], ctx).contains(x);
|
|
647
|
-
}
|
|
648
|
-
case O_subseteq: {
|
|
649
|
-
let a = this.evalSet(n.a[0], ctx), b = this.evalSet(n.a[1], ctx);
|
|
650
|
-
// SUBSET A \subseteq SUBSET B iff A \subseteq B
|
|
651
|
-
while (a instanceof SubsetV && b instanceof SubsetV) {
|
|
652
|
-
a = a.base;
|
|
653
|
-
b = b.base;
|
|
654
|
-
}
|
|
655
|
-
if (!a.enumerable())
|
|
656
|
-
return this.fail(`Attempted to check if the non-enumerable set ${fmt(a)} is a subset of ${fmt(b)}`, n);
|
|
657
|
-
if (a instanceof IntervalV && b instanceof IntervalV)
|
|
658
|
-
return a.hi < a.lo || (a.lo >= b.lo && a.hi <= b.hi);
|
|
659
|
-
for (const x of a.elems())
|
|
660
|
-
if (!b.contains(x))
|
|
661
|
-
return false;
|
|
662
|
-
return true;
|
|
663
|
-
}
|
|
664
|
-
case O_cup: return this.setUnion(this.evalSet(n.a[0], ctx), this.evalSet(n.a[1], ctx));
|
|
665
|
-
case O_cap: {
|
|
666
|
-
const a = this.evalSet(n.a[0], ctx), b = this.evalSet(n.a[1], ctx);
|
|
667
|
-
if (reducible(a) && reducible(b))
|
|
668
|
-
return new SetV(a.elems().filter(x => b.contains(x)));
|
|
669
|
-
const gen = a.enumerable() ? () => a.elems().filter(x => b.contains(x)) : b.enumerable() ? () => b.elems().filter(x => a.contains(x)) : null;
|
|
670
|
-
return new DerivedSetV(() => `${fmt(a)} \\cap ${fmt(b)}`, v => a.contains(v) && b.contains(v), gen);
|
|
671
|
-
}
|
|
672
|
-
case O_diff: {
|
|
673
|
-
const a = this.evalSet(n.a[0], ctx), b = this.evalSet(n.a[1], ctx);
|
|
674
|
-
if (reducible(a))
|
|
675
|
-
return new SetV(a.elems().filter(x => !b.contains(x)));
|
|
676
|
-
return new DerivedSetV(() => `${fmt(a)} \\ ${fmt(b)}`, v => a.contains(v) && !b.contains(v), a.enumerable() ? () => a.elems().filter(x => !b.contains(x)) : null);
|
|
677
|
-
}
|
|
678
|
-
case O_powerset: return new SubsetV(this.evalSet(n.a[0], ctx));
|
|
679
|
-
case O_bigunion: {
|
|
680
|
-
const s = this.evalElems(n.a[0], ctx);
|
|
681
|
-
const all = [];
|
|
682
|
-
let lazy = false;
|
|
683
|
-
for (const x of s) {
|
|
684
|
-
if (!(x instanceof SetBase))
|
|
685
|
-
return this.fail(`Attempted to apply UNION to a set containing the non-set value ${fmt(x)}`, n);
|
|
686
|
-
if (!(x instanceof SetV)) {
|
|
687
|
-
lazy = true;
|
|
688
|
-
break;
|
|
689
|
-
}
|
|
690
|
-
for (const y of x.elems())
|
|
691
|
-
all.push(y);
|
|
692
|
-
}
|
|
693
|
-
if (lazy) {
|
|
694
|
-
// like TLC's UnionValue: membership is tested against the member sets, enumeration is on demand
|
|
695
|
-
const sets = s;
|
|
696
|
-
return new DerivedSetV("UNION ...", v => { for (const x of sets)
|
|
697
|
-
if (x.contains(v))
|
|
698
|
-
return true; return false; }, sets.every(x => x.enumerable()) ? () => { const out = []; for (const x of sets)
|
|
699
|
-
for (const y of x.elems())
|
|
700
|
-
out.push(y); return out; } : null);
|
|
701
|
-
}
|
|
702
|
-
return mkSet(all);
|
|
703
|
-
}
|
|
704
|
-
case O_domain: {
|
|
705
|
-
const f = tailFn(n.a[0]) ? this.evalLazy(n.a[0], ctx) : n.a[0].k === 'app' ? this.evalApp(n.a[0], ctx) : this.eval(n.a[0], ctx);
|
|
706
|
-
if (f instanceof LazyFcn)
|
|
707
|
-
return f.domain();
|
|
708
|
-
const d = domainOf(f);
|
|
709
|
-
if (d === null)
|
|
710
|
-
return this.fail(`Attempted to apply the operator DOMAIN to the non-function value\n${fmt(f)}`, n);
|
|
711
|
-
return d;
|
|
712
|
-
}
|
|
713
|
-
case O_prime: {
|
|
714
|
-
if (this.nxt === null)
|
|
715
|
-
return this.fail('Attempted to evaluate a primed expression where no next state is available (the expression is not a state-level expression).', n);
|
|
716
|
-
const sc = this.cur, sn = this.nxt, im = this.initMode;
|
|
717
|
-
this.cur = sn;
|
|
718
|
-
this.nxt = null;
|
|
719
|
-
this.primeDepth++;
|
|
720
|
-
this.initMode = false;
|
|
721
|
-
try {
|
|
722
|
-
return this.eval(n.a[0], ctx);
|
|
723
|
-
}
|
|
724
|
-
finally {
|
|
725
|
-
this.cur = sc;
|
|
726
|
-
this.nxt = sn;
|
|
727
|
-
this.primeDepth--;
|
|
728
|
-
this.initMode = im;
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
case O_enabled: return this.enabled(n.a[0], ctx);
|
|
732
|
-
case O_unchanged: {
|
|
733
|
-
const e = n.a[0];
|
|
734
|
-
const v0 = this.eval(e, ctx);
|
|
735
|
-
if (this.nxt === null)
|
|
736
|
-
return this.fail('Attempted to evaluate UNCHANGED where no next state is available.', n);
|
|
737
|
-
const sc = this.cur, sn = this.nxt;
|
|
738
|
-
this.cur = sn;
|
|
739
|
-
this.nxt = null;
|
|
740
|
-
this.primeDepth++;
|
|
741
|
-
try {
|
|
742
|
-
return valEq(this.eval(e, ctx), v0);
|
|
743
|
-
}
|
|
744
|
-
finally {
|
|
745
|
-
this.cur = sc;
|
|
746
|
-
this.nxt = sn;
|
|
747
|
-
this.primeDepth--;
|
|
748
|
-
}
|
|
749
|
-
}
|
|
750
|
-
case O_cross: return new CrossV(n.a.map((x) => this.evalSet(x, ctx)));
|
|
751
|
-
case O_if: return this.evalBool(n.a[0], ctx) ? this.eval(n.a[1], ctx) : this.eval(n.a[2], ctx);
|
|
752
|
-
case O_case: {
|
|
753
|
-
for (const arm of n.arms)
|
|
754
|
-
if (this.evalBool(arm.c, ctx))
|
|
755
|
-
return this.eval(arm.e, ctx);
|
|
756
|
-
if (n.other)
|
|
757
|
-
return this.eval(n.other, ctx);
|
|
758
|
-
return this.fail('Attempted to evaluate a CASE expression in which none of the conditions were true.', n);
|
|
759
|
-
}
|
|
760
|
-
case O_let: return this.eval(n.body, this.bindLet(n.defs, ctx));
|
|
761
|
-
case O_quant: {
|
|
762
|
-
const body = n.body;
|
|
763
|
-
if (n.q === 'A')
|
|
764
|
-
return !this.forBindings(n.bounds, ctx, c => !this.evalBool(body, c));
|
|
765
|
-
return this.forBindings(n.bounds, ctx, c => this.evalBool(body, c));
|
|
766
|
-
}
|
|
767
|
-
case O_choose: {
|
|
768
|
-
const b = n.bound;
|
|
769
|
-
if (b.set === null)
|
|
770
|
-
return this.fail('TLC attempted to evaluate an unbounded CHOOSE.\nMake sure that the expression is of form CHOOSE x \\in S: P(x).', n);
|
|
771
|
-
const es = this.evalElems(b.set, ctx);
|
|
772
|
-
for (let i = 0; i < es.length; i++) {
|
|
773
|
-
const c = b.tuple ? this.bindTuple(b.tuple, es[i], ctx) : new Ctx(b.names[0], es[i], ctx);
|
|
774
|
-
if (this.evalBool(n.body, c))
|
|
775
|
-
return es[i];
|
|
776
|
-
}
|
|
777
|
-
return this.fail(`Attempted to compute the value of an expression of form\nCHOOSE x \\in S: P, but no element of S satisfied P.`, n);
|
|
778
|
-
}
|
|
779
|
-
case O_setenum: {
|
|
780
|
-
const a = n.a;
|
|
781
|
-
const out = new Array(a.length);
|
|
782
|
-
for (let i = 0; i < a.length; i++)
|
|
783
|
-
out[i] = this.eval(a[i], ctx);
|
|
784
|
-
return mkSet(out);
|
|
785
|
-
}
|
|
786
|
-
case O_setfilter: {
|
|
787
|
-
const b = n.bound;
|
|
788
|
-
const s = this.evalSet(b.set, ctx);
|
|
789
|
-
const pred = n.pred;
|
|
790
|
-
if (!reducible(s)) {
|
|
791
|
-
const test = (v) => this.evalBool(pred, b.tuple ? this.bindTuple(b.tuple, v, ctx) : cons(b.names[0], v, ctx));
|
|
792
|
-
// the predicate may depend on the state: capture it for later (lazy) evaluation
|
|
793
|
-
const sc = this.cur, sn = this.nxt;
|
|
794
|
-
const inState = (f) => {
|
|
795
|
-
const c0 = this.cur, n0 = this.nxt;
|
|
796
|
-
this.cur = sc;
|
|
797
|
-
this.nxt = sn;
|
|
798
|
-
try {
|
|
799
|
-
return f();
|
|
800
|
-
}
|
|
801
|
-
finally {
|
|
802
|
-
this.cur = c0;
|
|
803
|
-
this.nxt = n0;
|
|
804
|
-
}
|
|
805
|
-
};
|
|
806
|
-
return new DerivedSetV(() => `{x \\in ${fmt(s)} : ...}`, v => s.contains(v) && inState(() => test(v)), s.enumerable() ? () => inState(() => s.elems().filter(test)) : null);
|
|
807
|
-
}
|
|
808
|
-
const es = s.elems();
|
|
809
|
-
const out = [];
|
|
810
|
-
for (let i = 0; i < es.length; i++) {
|
|
811
|
-
const c = b.tuple ? this.bindTuple(b.tuple, es[i], ctx) : new Ctx(b.names[0], es[i], ctx);
|
|
812
|
-
if (this.evalBool(pred, c))
|
|
813
|
-
out.push(es[i]);
|
|
814
|
-
}
|
|
815
|
-
return new SetV(out);
|
|
816
|
-
}
|
|
817
|
-
case O_setmap: {
|
|
818
|
-
const out = [];
|
|
819
|
-
const e = n.e;
|
|
820
|
-
this.forBindings(n.bounds, ctx, c => { out.push(this.eval(e, c)); return false; });
|
|
821
|
-
return mkSet(out);
|
|
822
|
-
}
|
|
823
|
-
case O_fndef: return this.evalFnDef(n, ctx);
|
|
824
|
-
case O_fnset: return new FcnSetV(this.evalSet(n.a[0], ctx), this.evalSet(n.a[1], ctx));
|
|
825
|
-
case O_rec: {
|
|
826
|
-
const fs = n.fields;
|
|
827
|
-
const names = new Array(fs.length), vals = new Array(fs.length);
|
|
828
|
-
for (let i = 0; i < fs.length; i++) {
|
|
829
|
-
names[i] = fs[i].name;
|
|
830
|
-
vals[i] = this.eval(fs[i].e, ctx);
|
|
831
|
-
}
|
|
832
|
-
return this.mkRecord(n, names, vals);
|
|
833
|
-
}
|
|
834
|
-
case O_recset: {
|
|
835
|
-
const fs = n.fields;
|
|
836
|
-
const idx = fs.map((_, i) => i).sort((i, j) => strId(fs[i].name) - strId(fs[j].name));
|
|
837
|
-
return new RecSetV(idx.map(i => fs[i].name), idx.map(i => this.evalSet(fs[i].e, ctx)));
|
|
838
|
-
}
|
|
839
|
-
case O_except: {
|
|
840
|
-
let f = this.eval(n.base, ctx);
|
|
841
|
-
for (const u of n.updates) {
|
|
842
|
-
const path = u.path.map((p) => this.eval(p, ctx));
|
|
843
|
-
f = this.exceptUpdate(f, path, 0, u.val, ctx, n);
|
|
844
|
-
}
|
|
845
|
-
return f;
|
|
846
|
-
}
|
|
847
|
-
case O_tuple: {
|
|
848
|
-
const a = n.a;
|
|
849
|
-
const out = new Array(a.length);
|
|
850
|
-
for (let i = 0; i < a.length; i++)
|
|
851
|
-
out[i] = this.eval(a[i], ctx);
|
|
852
|
-
return new TupleV(out);
|
|
853
|
-
}
|
|
854
|
-
case O_fnapp: {
|
|
855
|
-
const fn = n.f;
|
|
856
|
-
let tf = fn.tf;
|
|
857
|
-
if (tf === undefined)
|
|
858
|
-
tf = tailFn(fn);
|
|
859
|
-
const f = tf ? this.evalLazy(fn, ctx) : fn.k === 'app' ? this.evalApp(fn, ctx) : this.eval(fn, ctx);
|
|
860
|
-
let arg;
|
|
861
|
-
if (n.args.length === 1)
|
|
862
|
-
arg = this.eval(n.args[0], ctx);
|
|
863
|
-
else
|
|
864
|
-
arg = new TupleV(n.args.map((a) => this.eval(a, ctx)));
|
|
865
|
-
const v = this.applyFcn(f, arg, n);
|
|
866
|
-
if (typeof v === 'object' && v instanceof LazyFcn)
|
|
867
|
-
return v.force();
|
|
868
|
-
return v;
|
|
869
|
-
}
|
|
870
|
-
case O_dot: {
|
|
871
|
-
const r = this.eval(n.e, ctx);
|
|
872
|
-
if (r instanceof RecordV) {
|
|
873
|
-
const v = r.get(n.field);
|
|
874
|
-
if (v === undefined)
|
|
875
|
-
return this.fail(`Attempted to access nonexistent field '${n.field}' of record\n${fmt(r)}`, n);
|
|
876
|
-
return v;
|
|
877
|
-
}
|
|
878
|
-
return this.fail(`Attempted to select field '${n.field}' from the non-record value ${fmt(r)}`, n);
|
|
879
|
-
}
|
|
880
|
-
case O_boxact: return this.evalBool(n.a[0], ctx) || this.unchangedVal(n.a[1], ctx);
|
|
881
|
-
case O_angleact: return this.evalBool(n.a[0], ctx) && !this.unchangedVal(n.a[1], ctx);
|
|
882
|
-
case O_lambda: return new Closure(n.params, n.body, ctx);
|
|
883
|
-
case O_at: return this.lookup(ctx, '@');
|
|
884
|
-
}
|
|
885
|
-
if (n.k === 'cdot') {
|
|
886
|
-
try {
|
|
887
|
-
this.enumerate(n, ctx, () => { throw FOUND; });
|
|
888
|
-
return false;
|
|
889
|
-
}
|
|
890
|
-
catch (e) {
|
|
891
|
-
if (e === FOUND)
|
|
892
|
-
return true;
|
|
893
|
-
throw e;
|
|
894
|
-
}
|
|
895
|
-
}
|
|
896
|
-
if (n.k === 'always' || n.k === 'eventually' || n.k === 'leadsto' || n.k === 'wf' || n.k === 'sf' || n.k === 'guarantee' || n.k === 'tquant') {
|
|
897
|
-
return this.fail('TLC attempted to evaluate a temporal formula where a state or action-level expression was expected.', n);
|
|
898
|
-
}
|
|
899
|
-
return this.fail(`TLC does not support the construct '${n.k}' here.`, n);
|
|
900
|
-
}
|
|
901
|
-
// Evaluate in TLC's "keep lazy" mode: a function constructor reached through IF/CASE/LET/definitions is
|
|
902
|
-
// returned as an unevaluated LazyFcn (the caller applies it, takes its DOMAIN or enumerates its body).
|
|
903
|
-
evalLazy(n, ctx) {
|
|
904
|
-
switch (n.k) {
|
|
905
|
-
case 'fndef': return new LazyFcn(n, ctx, this);
|
|
906
|
-
case 'if': return this.evalBool(n.a[0], ctx) ? this.evalLazy(n.a[1], ctx) : this.evalLazy(n.a[2], ctx);
|
|
907
|
-
case 'case': {
|
|
908
|
-
for (const arm of n.arms)
|
|
909
|
-
if (this.evalBool(arm.c, ctx))
|
|
910
|
-
return this.evalLazy(arm.e, ctx);
|
|
911
|
-
if (n.other)
|
|
912
|
-
return this.evalLazy(n.other, ctx);
|
|
913
|
-
return this.fail('Attempted to evaluate a CASE expression in which none of the conditions were true.', n);
|
|
914
|
-
}
|
|
915
|
-
case 'let': return this.evalLazy(n.body, this.bindLet(n.defs, ctx));
|
|
916
|
-
case 'fnapp': {
|
|
917
|
-
if (!tailFn(n))
|
|
918
|
-
break;
|
|
919
|
-
const f = this.evalLazy(n.f, ctx);
|
|
920
|
-
const arg = n.args.length === 1 ? this.eval(n.args[0], ctx) : new TupleV(n.args.map((a) => this.eval(a, ctx)));
|
|
921
|
-
return f instanceof LazyFcn ? f.apply(arg, true) : this.applyFcn(f, arg, n);
|
|
922
|
-
}
|
|
923
|
-
case 'app': {
|
|
924
|
-
if (!tailFn(n))
|
|
925
|
-
return this.evalApp(n, ctx);
|
|
926
|
-
const r = n.r;
|
|
927
|
-
if (r === LOCAL) {
|
|
928
|
-
const b = this.lookup(ctx, n.name);
|
|
929
|
-
return b instanceof Lazy ? this.force(b, true) : this.evalApp(n, ctx);
|
|
930
|
-
}
|
|
931
|
-
let def = r.override ?? r;
|
|
932
|
-
if (def.builtin)
|
|
933
|
-
break;
|
|
934
|
-
const top = def.params.length === 0 && !def.inst && (ctx === null || ctx.base === null);
|
|
935
|
-
if (top && def.cached)
|
|
936
|
-
return def.cache;
|
|
937
|
-
const x = this.expandApp(n, ctx);
|
|
938
|
-
if (!x)
|
|
939
|
-
break;
|
|
940
|
-
const s0 = this.stTouch, p0 = this.prTouch;
|
|
941
|
-
const v = this.evalLazy(x.body, x.ctx);
|
|
942
|
-
if (top && defLevel(def) === 0 && this.stTouch === s0 && this.prTouch === p0) {
|
|
943
|
-
// a constant: evaluate it completely once (TLC precomputes constant definitions), unless that fails
|
|
944
|
-
let w = v;
|
|
945
|
-
if (v instanceof LazyFcn) {
|
|
946
|
-
try {
|
|
947
|
-
w = v.force();
|
|
948
|
-
}
|
|
949
|
-
catch (e) {
|
|
950
|
-
if (!(e instanceof TlaError))
|
|
951
|
-
throw e;
|
|
952
|
-
}
|
|
953
|
-
if (this.stTouch !== s0 || this.prTouch !== p0)
|
|
954
|
-
return v;
|
|
955
|
-
}
|
|
956
|
-
def.cached = true;
|
|
957
|
-
def.cache = w;
|
|
958
|
-
return w;
|
|
959
|
-
}
|
|
960
|
-
return v;
|
|
961
|
-
}
|
|
962
|
-
}
|
|
963
|
-
return this.eval(n, ctx);
|
|
964
|
-
}
|
|
965
|
-
mkRecord(_n, names, vals) {
|
|
966
|
-
return makeRecord(names, vals);
|
|
967
|
-
}
|
|
968
|
-
unchangedVal(e, ctx) {
|
|
969
|
-
const v0 = this.eval(e, ctx);
|
|
970
|
-
if (this.nxt === null)
|
|
971
|
-
return this.fail('Attempted to evaluate an action where no next state is available.', e);
|
|
972
|
-
const sc = this.cur, sn = this.nxt;
|
|
973
|
-
this.cur = sn;
|
|
974
|
-
this.nxt = null;
|
|
975
|
-
this.primeDepth++;
|
|
976
|
-
try {
|
|
977
|
-
return valEq(this.eval(e, ctx), v0);
|
|
978
|
-
}
|
|
979
|
-
finally {
|
|
980
|
-
this.cur = sc;
|
|
981
|
-
this.nxt = sn;
|
|
982
|
-
this.primeDepth--;
|
|
983
|
-
}
|
|
984
|
-
}
|
|
985
|
-
setUnion(a, b) {
|
|
986
|
-
if (!reducible(a) || !reducible(b)) {
|
|
987
|
-
return new DerivedSetV(() => `${fmt(a)} \\cup ${fmt(b)}`, v => a.contains(v) || b.contains(v), a.enumerable() && b.enumerable() ? () => a.elems().concat(b.elems()) : null);
|
|
988
|
-
}
|
|
989
|
-
const x = a.elems(), y = b.elems();
|
|
990
|
-
if (x.length === 0)
|
|
991
|
-
return b;
|
|
992
|
-
if (y.length === 0)
|
|
993
|
-
return a;
|
|
994
|
-
const out = [];
|
|
995
|
-
let i = 0, j = 0;
|
|
996
|
-
while (i < x.length && j < y.length) {
|
|
997
|
-
const c = compare(x[i], y[j]);
|
|
998
|
-
if (c < 0)
|
|
999
|
-
out.push(x[i++]);
|
|
1000
|
-
else if (c > 0)
|
|
1001
|
-
out.push(y[j++]);
|
|
1002
|
-
else {
|
|
1003
|
-
out.push(x[i++]);
|
|
1004
|
-
j++;
|
|
1005
|
-
}
|
|
1006
|
-
}
|
|
1007
|
-
while (i < x.length)
|
|
1008
|
-
out.push(x[i++]);
|
|
1009
|
-
while (j < y.length)
|
|
1010
|
-
out.push(y[j++]);
|
|
1011
|
-
return new SetV(out);
|
|
1012
|
-
}
|
|
1013
|
-
evalFnDef(n, ctx) {
|
|
1014
|
-
const bounds = n.bounds;
|
|
1015
|
-
if (n.isRec)
|
|
1016
|
-
return new LazyFcn(n, ctx, this);
|
|
1017
|
-
if (bounds.length === 1 && (bounds[0].tuple || bounds[0].names.length === 1)) {
|
|
1018
|
-
const b = bounds[0];
|
|
1019
|
-
const s = this.evalSet(b.set, ctx);
|
|
1020
|
-
if (!s.enumerable())
|
|
1021
|
-
return new LazyFcn(n, ctx, this);
|
|
1022
|
-
const dom = s.elems();
|
|
1023
|
-
const vals = new Array(dom.length);
|
|
1024
|
-
const body = n.body;
|
|
1025
|
-
if (b.tuple)
|
|
1026
|
-
for (let i = 0; i < dom.length; i++)
|
|
1027
|
-
vals[i] = this.eval(body, this.bindTuple(b.tuple, dom[i], ctx));
|
|
1028
|
-
else {
|
|
1029
|
-
const name = b.names[0];
|
|
1030
|
-
for (let i = 0; i < dom.length; i++)
|
|
1031
|
-
vals[i] = this.eval(body, new Ctx(name, dom[i], ctx));
|
|
1032
|
-
}
|
|
1033
|
-
return makeFcn(dom, vals);
|
|
1034
|
-
}
|
|
1035
|
-
const dom = [], vals = [];
|
|
1036
|
-
const sets = [];
|
|
1037
|
-
const bsets = [];
|
|
1038
|
-
for (const b of bounds) {
|
|
1039
|
-
const bs = this.evalSet(b.set, ctx);
|
|
1040
|
-
if (!bs.enumerable())
|
|
1041
|
-
return new LazyFcn(n, ctx, this);
|
|
1042
|
-
bsets.push(bs);
|
|
1043
|
-
}
|
|
1044
|
-
for (let bi = 0; bi < bounds.length; bi++) {
|
|
1045
|
-
const b = bounds[bi];
|
|
1046
|
-
const es = bsets[bi].elems();
|
|
1047
|
-
const cnt = b.tuple ? 1 : b.names.length;
|
|
1048
|
-
for (let j = 0; j < cnt; j++)
|
|
1049
|
-
sets.push(es);
|
|
1050
|
-
}
|
|
1051
|
-
const names = [];
|
|
1052
|
-
for (const b of bounds) {
|
|
1053
|
-
if (b.tuple)
|
|
1054
|
-
names.push(b.tuple);
|
|
1055
|
-
else
|
|
1056
|
-
names.push(...b.names);
|
|
1057
|
-
}
|
|
1058
|
-
const cur = [];
|
|
1059
|
-
const rec = (i, c) => {
|
|
1060
|
-
if (i === sets.length) {
|
|
1061
|
-
dom.push(new TupleV(cur.slice()));
|
|
1062
|
-
vals.push(this.eval(n.body, c));
|
|
1063
|
-
return;
|
|
1064
|
-
}
|
|
1065
|
-
const nm = names[i];
|
|
1066
|
-
for (const v of sets[i]) {
|
|
1067
|
-
cur.push(v);
|
|
1068
|
-
rec(i + 1, typeof nm === 'string' ? cons(nm, v, c) : this.bindTuple(nm, v, c));
|
|
1069
|
-
cur.pop();
|
|
1070
|
-
}
|
|
1071
|
-
};
|
|
1072
|
-
rec(0, ctx);
|
|
1073
|
-
return makeFcn(dom, vals);
|
|
1074
|
-
}
|
|
1075
|
-
applyFcn(f, arg, n) {
|
|
1076
|
-
if (f instanceof TupleV) {
|
|
1077
|
-
if (typeof arg === 'number' && arg >= 1 && arg <= f.e.length && Number.isInteger(arg))
|
|
1078
|
-
return f.e[arg - 1];
|
|
1079
|
-
return this.fail(typeof arg === 'number' ? `Attempted to apply tuple\n${fmt(f)}\nto integer ${arg} which is out of domain.` : `Attempted to apply tuple\n${fmt(f)}\nto non-integer argument ${fmt(arg)}`, n);
|
|
1080
|
-
}
|
|
1081
|
-
if (f instanceof RecordV) {
|
|
1082
|
-
if (typeof arg === 'string') {
|
|
1083
|
-
const v = f.get(arg);
|
|
1084
|
-
if (v !== undefined)
|
|
1085
|
-
return v;
|
|
1086
|
-
}
|
|
1087
|
-
return this.fail(`Attempted to apply function:\n${fmt(f)}\nto argument ${fmt(arg)}, which is not in the domain of the function.`, n);
|
|
1088
|
-
}
|
|
1089
|
-
if (f instanceof FcnV) {
|
|
1090
|
-
const i = bsearch(f.dom, arg);
|
|
1091
|
-
if (i >= 0)
|
|
1092
|
-
return f.vals[i];
|
|
1093
|
-
return this.fail(`Attempted to apply function:\n${fmt(f)}\nto argument ${fmt(arg)}, which is not in the domain of the function.`, n);
|
|
1094
|
-
}
|
|
1095
|
-
if (f instanceof LazyFcn)
|
|
1096
|
-
return f.apply(arg);
|
|
1097
|
-
if (f instanceof Closure || f instanceof DefRef)
|
|
1098
|
-
return this.fail('Attempted to apply an operator as if it were a function (use parentheses, not brackets).', n);
|
|
1099
|
-
return this.fail(`A non-function (${fmt(f)}) was applied as a function.`, n);
|
|
1100
|
-
}
|
|
1101
|
-
exceptUpdate(f, path, i, valNode, ctx, n) {
|
|
1102
|
-
const key = path[i];
|
|
1103
|
-
const last = i === path.length - 1;
|
|
1104
|
-
const upd = (old) => last ? this.eval(valNode, cons('@', old, ctx)) : this.exceptUpdate(old, path, i + 1, valNode, ctx, n);
|
|
1105
|
-
if (f instanceof TupleV) {
|
|
1106
|
-
if (typeof key !== 'number' || key < 1 || key > f.e.length)
|
|
1107
|
-
return f;
|
|
1108
|
-
const e = f.e.slice();
|
|
1109
|
-
e[key - 1] = upd(e[key - 1]);
|
|
1110
|
-
return new TupleV(e);
|
|
1111
|
-
}
|
|
1112
|
-
if (f instanceof RecordV) {
|
|
1113
|
-
const k = typeof key === 'string' ? f.names.indexOf(key) : -1;
|
|
1114
|
-
if (k < 0)
|
|
1115
|
-
return f;
|
|
1116
|
-
const vals = f.vals.slice();
|
|
1117
|
-
vals[k] = upd(vals[k]);
|
|
1118
|
-
return new RecordV(f.names, vals);
|
|
1119
|
-
}
|
|
1120
|
-
if (f instanceof FcnV) {
|
|
1121
|
-
const k = bsearch(f.dom, key);
|
|
1122
|
-
if (k < 0)
|
|
1123
|
-
return f;
|
|
1124
|
-
const vals = f.vals.slice();
|
|
1125
|
-
vals[k] = upd(vals[k]);
|
|
1126
|
-
return new FcnV(f.dom, vals);
|
|
1127
|
-
}
|
|
1128
|
-
if (f instanceof LazyFcn) {
|
|
1129
|
-
const d = f.domain();
|
|
1130
|
-
if (d.enumerable())
|
|
1131
|
-
return this.exceptUpdate(f.force(), path, i, valNode, ctx, n);
|
|
1132
|
-
if (!d.contains(key))
|
|
1133
|
-
return f;
|
|
1134
|
-
return f.withOverride(key, upd(f.apply(key)));
|
|
1135
|
-
}
|
|
1136
|
-
return this.fail(`Attempted to apply EXCEPT to the non-function value\n${fmt(f)}`, n);
|
|
1137
|
-
}
|
|
1138
|
-
// ---------- ENABLED ----------
|
|
1139
|
-
enabled(a, ctx) {
|
|
1140
|
-
const sn = this.nxt, im = this.initMode, na = this.nAssigned, cd = this.callDepth;
|
|
1141
|
-
if (ctx !== null && ctx.base !== null && ctx.base.name !== ENABLED_MARK) {
|
|
1142
|
-
// mark the instance environment the ENABLED is evaluated in (see asVar)
|
|
1143
|
-
const env = new Ctx(ENABLED_MARK, true, ctx.base);
|
|
1144
|
-
env.base = env;
|
|
1145
|
-
ctx = new Ctx(ENABLED_MARK, true, ctx);
|
|
1146
|
-
ctx.base = env;
|
|
1147
|
-
}
|
|
1148
|
-
this.nxt = new Array(this.nvars);
|
|
1149
|
-
this.initMode = false;
|
|
1150
|
-
this.nAssigned = -1e9; // no all-assigned shortcut inside ENABLED
|
|
1151
|
-
this.inEnabled++;
|
|
1152
|
-
try {
|
|
1153
|
-
this.enumerate(a, ctx, () => { throw FOUND; });
|
|
1154
|
-
return false;
|
|
1155
|
-
}
|
|
1156
|
-
catch (e) {
|
|
1157
|
-
if (e === FOUND)
|
|
1158
|
-
return true;
|
|
1159
|
-
throw e;
|
|
1160
|
-
}
|
|
1161
|
-
finally {
|
|
1162
|
-
this.nxt = sn;
|
|
1163
|
-
this.initMode = im;
|
|
1164
|
-
this.nAssigned = na;
|
|
1165
|
-
this.callDepth = cd;
|
|
1166
|
-
this.inEnabled--;
|
|
1167
|
-
}
|
|
1168
|
-
}
|
|
1169
|
-
// ---------- action / initial-predicate enumeration ----------
|
|
1170
|
-
// Resolve an expression to a variable index if it denotes a variable (possibly through substitutions).
|
|
1171
|
-
asVar(e, ctx) {
|
|
1172
|
-
for (let guard = 0; guard < 100; guard++) {
|
|
1173
|
-
if (e.k === 'let') {
|
|
1174
|
-
ctx = this.bindLet(e.defs, ctx);
|
|
1175
|
-
e = e.body;
|
|
1176
|
-
continue;
|
|
1177
|
-
}
|
|
1178
|
-
if (e.k !== 'app' || e.args.length !== 0 || e.sel)
|
|
1179
|
-
return -1;
|
|
1180
|
-
const r = e.r;
|
|
1181
|
-
if (r === LOCAL) {
|
|
1182
|
-
const b = this.lookup(ctx, e.name);
|
|
1183
|
-
if (b instanceof Lazy) {
|
|
1184
|
-
e = b.e;
|
|
1185
|
-
ctx = b.ctx;
|
|
1186
|
-
continue;
|
|
1187
|
-
}
|
|
1188
|
-
return -1;
|
|
1189
|
-
}
|
|
1190
|
-
if (r.k === 'def') {
|
|
1191
|
-
const x = this.expandApp(e, ctx);
|
|
1192
|
-
if (!x)
|
|
1193
|
-
return -1;
|
|
1194
|
-
e = x.body;
|
|
1195
|
-
ctx = x.ctx;
|
|
1196
|
-
continue;
|
|
1197
|
-
}
|
|
1198
|
-
if (r.k !== 'var')
|
|
1199
|
-
return -1;
|
|
1200
|
-
const base = ctx === null ? null : ctx.base;
|
|
1201
|
-
if (base !== null) {
|
|
1202
|
-
const b = this.envLookup(base, e.name);
|
|
1203
|
-
if (b instanceof Lazy) {
|
|
1204
|
-
if (base.name === ENABLED_MARK) {
|
|
1205
|
-
// Like TLC's context cut-off: a variable of an instantiated module that is reached from inside an
|
|
1206
|
-
// ENABLED evaluated in that module stands for itself, not for the expression substituted for it.
|
|
1207
|
-
let si = this.scratch.get(r);
|
|
1208
|
-
if (si === undefined) {
|
|
1209
|
-
si = this.nvars + this.scratch.size;
|
|
1210
|
-
this.scratch.set(r, si);
|
|
1211
|
-
}
|
|
1212
|
-
return si;
|
|
1213
|
-
}
|
|
1214
|
-
e = b.e;
|
|
1215
|
-
ctx = b.ctx;
|
|
1216
|
-
continue;
|
|
1217
|
-
}
|
|
1218
|
-
if (b !== undefined)
|
|
1219
|
-
return -1;
|
|
1220
|
-
}
|
|
1221
|
-
return r.idx;
|
|
1222
|
-
}
|
|
1223
|
-
return -1;
|
|
1224
|
-
}
|
|
1225
|
-
// Enumerate all ways of satisfying `e`, assigning unassigned target variables (primed variables in
|
|
1226
|
-
// next-state mode, unprimed variables in init mode). Calls k for each satisfying (partial) assignment.
|
|
1227
|
-
enumerate(e, ctx, k) {
|
|
1228
|
-
// Like TLC: once every variable is assigned, the remaining conjuncts are plain predicates.
|
|
1229
|
-
if (this.nAssigned === this.nvars && e.k !== 'cdot') {
|
|
1230
|
-
if (this.evalBool(e, ctx))
|
|
1231
|
-
k();
|
|
1232
|
-
return;
|
|
1233
|
-
}
|
|
1234
|
-
switch (e.k) {
|
|
1235
|
-
case 'cdot': {
|
|
1236
|
-
// s -(A \cdot B)-> u iff there is a t with s -A-> t and t -B-> u
|
|
1237
|
-
if (this.initMode)
|
|
1238
|
-
break;
|
|
1239
|
-
const sc = this.cur, sn = this.nxt, na = this.nAssigned;
|
|
1240
|
-
const mids = [];
|
|
1241
|
-
const mid = new Array(this.nvars);
|
|
1242
|
-
this.nxt = mid;
|
|
1243
|
-
this.nAssigned = 0;
|
|
1244
|
-
try {
|
|
1245
|
-
this.enumerate(e.a[0], ctx, () => {
|
|
1246
|
-
for (let i = 0; i < this.nvars; i++)
|
|
1247
|
-
if (mid[i] === undefined)
|
|
1248
|
-
this.fail('The first action of an action composition (\\cdot) does not completely specify the intermediate state.', e);
|
|
1249
|
-
mids.push(mid.slice());
|
|
1250
|
-
});
|
|
1251
|
-
}
|
|
1252
|
-
finally {
|
|
1253
|
-
this.nxt = sn;
|
|
1254
|
-
this.nAssigned = na;
|
|
1255
|
-
}
|
|
1256
|
-
if (this.inEnabled === 0)
|
|
1257
|
-
this.extraGenerated += mids.length;
|
|
1258
|
-
for (const t of mids) {
|
|
1259
|
-
this.cur = t;
|
|
1260
|
-
try {
|
|
1261
|
-
this.enumerate(e.a[1], ctx, () => {
|
|
1262
|
-
const c = this.cur;
|
|
1263
|
-
this.cur = sc;
|
|
1264
|
-
try {
|
|
1265
|
-
k();
|
|
1266
|
-
}
|
|
1267
|
-
finally {
|
|
1268
|
-
this.cur = c;
|
|
1269
|
-
}
|
|
1270
|
-
});
|
|
1271
|
-
}
|
|
1272
|
-
finally {
|
|
1273
|
-
this.cur = sc;
|
|
1274
|
-
}
|
|
1275
|
-
}
|
|
1276
|
-
return;
|
|
1277
|
-
}
|
|
1278
|
-
case 'and':
|
|
1279
|
-
this.enumConj(e.a, 0, ctx, k);
|
|
1280
|
-
return;
|
|
1281
|
-
case 'or':
|
|
1282
|
-
for (const d of e.a)
|
|
1283
|
-
this.enumerate(d, ctx, k);
|
|
1284
|
-
return;
|
|
1285
|
-
case 'quant':
|
|
1286
|
-
if (e.q === 'E') {
|
|
1287
|
-
this.forBindings(e.bounds, ctx, c => { this.enumerate(e.body, c, k); return false; });
|
|
1288
|
-
return;
|
|
1289
|
-
}
|
|
1290
|
-
{
|
|
1291
|
-
// \A is a conjunction over all bindings
|
|
1292
|
-
const ctxs = [];
|
|
1293
|
-
this.forBindings(e.bounds, ctx, c => { ctxs.push(c); return false; });
|
|
1294
|
-
const go = (i) => {
|
|
1295
|
-
if (i === ctxs.length) {
|
|
1296
|
-
k();
|
|
1297
|
-
return;
|
|
1298
|
-
}
|
|
1299
|
-
this.enumerate(e.body, ctxs[i], () => go(i + 1));
|
|
1300
|
-
};
|
|
1301
|
-
go(0);
|
|
1302
|
-
return;
|
|
1303
|
-
}
|
|
1304
|
-
case 'imp':
|
|
1305
|
-
if (!this.evalBool(e.a[0], ctx))
|
|
1306
|
-
k();
|
|
1307
|
-
else
|
|
1308
|
-
this.enumerate(e.a[1], ctx, k);
|
|
1309
|
-
return;
|
|
1310
|
-
case 'subseteq': {
|
|
1311
|
-
const tgt = this.targetVar(e.a[0], ctx);
|
|
1312
|
-
if (tgt >= 0) {
|
|
1313
|
-
const arr = this.initMode ? this.cur : this.nxt;
|
|
1314
|
-
if (arr[tgt] === undefined) {
|
|
1315
|
-
const es = new SubsetV(this.evalSet(e.a[1], ctx)).elems();
|
|
1316
|
-
this.nAssigned++;
|
|
1317
|
-
try {
|
|
1318
|
-
for (let i = 0; i < es.length; i++) {
|
|
1319
|
-
arr[tgt] = es[i];
|
|
1320
|
-
k();
|
|
1321
|
-
}
|
|
1322
|
-
}
|
|
1323
|
-
finally {
|
|
1324
|
-
arr[tgt] = undefined;
|
|
1325
|
-
this.nAssigned--;
|
|
1326
|
-
}
|
|
1327
|
-
return;
|
|
1328
|
-
}
|
|
1329
|
-
}
|
|
1330
|
-
break;
|
|
1331
|
-
}
|
|
1332
|
-
case 'if':
|
|
1333
|
-
if (this.evalBool(e.a[0], ctx))
|
|
1334
|
-
this.enumerate(e.a[1], ctx, k);
|
|
1335
|
-
else
|
|
1336
|
-
this.enumerate(e.a[2], ctx, k);
|
|
1337
|
-
return;
|
|
1338
|
-
case 'case': {
|
|
1339
|
-
for (const arm of e.arms)
|
|
1340
|
-
if (this.evalBool(arm.c, ctx)) {
|
|
1341
|
-
this.enumerate(arm.e, ctx, k);
|
|
1342
|
-
return;
|
|
1343
|
-
}
|
|
1344
|
-
if (e.other) {
|
|
1345
|
-
this.enumerate(e.other, ctx, k);
|
|
1346
|
-
return;
|
|
1347
|
-
}
|
|
1348
|
-
this.fail('Attempted to evaluate a CASE expression in which none of the conditions were true.', e);
|
|
1349
|
-
return;
|
|
1350
|
-
}
|
|
1351
|
-
case 'let':
|
|
1352
|
-
this.enumerate(e.body, this.bindLet(e.defs, ctx), k);
|
|
1353
|
-
return;
|
|
1354
|
-
case 'app': {
|
|
1355
|
-
const x = this.expandApp(e, ctx);
|
|
1356
|
-
if (x) {
|
|
1357
|
-
this.enumerate(x.body, x.ctx, k);
|
|
1358
|
-
return;
|
|
1359
|
-
}
|
|
1360
|
-
break;
|
|
1361
|
-
}
|
|
1362
|
-
case 'eq': {
|
|
1363
|
-
const tgt = this.targetVar(e.a[0], ctx);
|
|
1364
|
-
if (tgt >= 0) {
|
|
1365
|
-
const arr = this.initMode ? this.cur : this.nxt;
|
|
1366
|
-
if (arr[tgt] === undefined) {
|
|
1367
|
-
const v = this.eval(e.a[1], ctx);
|
|
1368
|
-
arr[tgt] = v;
|
|
1369
|
-
this.nAssigned++;
|
|
1370
|
-
try {
|
|
1371
|
-
k();
|
|
1372
|
-
}
|
|
1373
|
-
finally {
|
|
1374
|
-
arr[tgt] = undefined;
|
|
1375
|
-
this.nAssigned--;
|
|
1376
|
-
}
|
|
1377
|
-
return;
|
|
1378
|
-
}
|
|
1379
|
-
}
|
|
1380
|
-
break;
|
|
1381
|
-
}
|
|
1382
|
-
case 'in': {
|
|
1383
|
-
const tgt = this.targetVar(e.a[0], ctx);
|
|
1384
|
-
if (tgt >= 0) {
|
|
1385
|
-
const arr = this.initMode ? this.cur : this.nxt;
|
|
1386
|
-
if (arr[tgt] === undefined) {
|
|
1387
|
-
const s = this.evalSet(e.a[1], ctx);
|
|
1388
|
-
const es = s.elems();
|
|
1389
|
-
this.nAssigned++;
|
|
1390
|
-
try {
|
|
1391
|
-
for (let i = 0; i < es.length; i++) {
|
|
1392
|
-
arr[tgt] = es[i];
|
|
1393
|
-
k();
|
|
1394
|
-
}
|
|
1395
|
-
}
|
|
1396
|
-
finally {
|
|
1397
|
-
arr[tgt] = undefined;
|
|
1398
|
-
this.nAssigned--;
|
|
1399
|
-
}
|
|
1400
|
-
return;
|
|
1401
|
-
}
|
|
1402
|
-
}
|
|
1403
|
-
break;
|
|
1404
|
-
}
|
|
1405
|
-
case 'unchanged':
|
|
1406
|
-
if (!this.initMode) {
|
|
1407
|
-
this.enumUnchanged(e.a[0], ctx, k);
|
|
1408
|
-
return;
|
|
1409
|
-
}
|
|
1410
|
-
break;
|
|
1411
|
-
case 'fnapp': {
|
|
1412
|
-
// TLC enters the body of a lazily evaluated (recursive) function
|
|
1413
|
-
const f = tailFn(e.f) ? this.evalLazy(e.f, ctx) : e.f.k === 'app' ? this.evalApp(e.f, ctx) : this.eval(e.f, ctx);
|
|
1414
|
-
if (f instanceof LazyFcn) {
|
|
1415
|
-
const arg = e.args.length === 1 ? this.eval(e.args[0], ctx) : new TupleV(e.args.map((a) => this.eval(a, ctx)));
|
|
1416
|
-
this.enumerate(f.node.body, f.bind(arg), k);
|
|
1417
|
-
return;
|
|
1418
|
-
}
|
|
1419
|
-
break;
|
|
1420
|
-
}
|
|
1421
|
-
case 'boxact':
|
|
1422
|
-
if (!this.initMode) {
|
|
1423
|
-
this.enumerate(e.a[0], ctx, k);
|
|
1424
|
-
this.enumUnchanged(e.a[1], ctx, k);
|
|
1425
|
-
return;
|
|
1426
|
-
}
|
|
1427
|
-
break;
|
|
1428
|
-
case 'angleact':
|
|
1429
|
-
if (!this.initMode) {
|
|
1430
|
-
this.enumerate(e.a[0], ctx, () => { if (!this.unchangedVal(e.a[1], ctx))
|
|
1431
|
-
k(); });
|
|
1432
|
-
return;
|
|
1433
|
-
}
|
|
1434
|
-
break;
|
|
1435
|
-
}
|
|
1436
|
-
if (this.evalBool(e, ctx))
|
|
1437
|
-
k();
|
|
1438
|
-
}
|
|
1439
|
-
enumConj(items, i, ctx, k) {
|
|
1440
|
-
if (i === items.length) {
|
|
1441
|
-
k();
|
|
1442
|
-
return;
|
|
1443
|
-
}
|
|
1444
|
-
if (i === items.length - 1) {
|
|
1445
|
-
this.enumerate(items[i], ctx, k);
|
|
1446
|
-
return;
|
|
1447
|
-
}
|
|
1448
|
-
this.enumerate(items[i], ctx, () => this.enumConj(items, i + 1, ctx, k));
|
|
1449
|
-
}
|
|
1450
|
-
targetVar(lhs, ctx) {
|
|
1451
|
-
if (this.initMode)
|
|
1452
|
-
return lhs.k === 'app' ? this.asVar(lhs, ctx) : -1;
|
|
1453
|
-
if (this.nxt === null || this.primeDepth > 0)
|
|
1454
|
-
return -1;
|
|
1455
|
-
// look through operator parameters bound to primed variables, e.g. Send(p, d, mem, mem')
|
|
1456
|
-
for (let guard = 0; guard < 100; guard++) {
|
|
1457
|
-
if (lhs.k === 'let') {
|
|
1458
|
-
ctx = this.bindLet(lhs.defs, ctx);
|
|
1459
|
-
lhs = lhs.body;
|
|
1460
|
-
continue;
|
|
1461
|
-
}
|
|
1462
|
-
if (lhs.k !== 'app' || lhs.args.length !== 0)
|
|
1463
|
-
break;
|
|
1464
|
-
if (lhs.r === LOCAL) {
|
|
1465
|
-
const b = this.lookup(ctx, lhs.name);
|
|
1466
|
-
if (!(b instanceof Lazy))
|
|
1467
|
-
return -1;
|
|
1468
|
-
lhs = b.e;
|
|
1469
|
-
ctx = b.ctx;
|
|
1470
|
-
}
|
|
1471
|
-
else if (lhs.r.k === 'def' && !lhs.sel) {
|
|
1472
|
-
// a definition whose body is a primed variable (a == x')
|
|
1473
|
-
const x = this.expandApp(lhs, ctx);
|
|
1474
|
-
if (!x)
|
|
1475
|
-
return -1;
|
|
1476
|
-
lhs = x.body;
|
|
1477
|
-
ctx = x.ctx;
|
|
1478
|
-
}
|
|
1479
|
-
else
|
|
1480
|
-
return -1;
|
|
1481
|
-
}
|
|
1482
|
-
if (lhs.k !== 'prime')
|
|
1483
|
-
return -1;
|
|
1484
|
-
return this.asVar(lhs.a[0], ctx);
|
|
1485
|
-
}
|
|
1486
|
-
enumUnchanged(e, ctx, k) {
|
|
1487
|
-
const nxt = this.nxt;
|
|
1488
|
-
if (e.k === 'tuple') {
|
|
1489
|
-
const items = e.a;
|
|
1490
|
-
const go = (i) => {
|
|
1491
|
-
if (i === items.length) {
|
|
1492
|
-
k();
|
|
1493
|
-
return;
|
|
1494
|
-
}
|
|
1495
|
-
this.enumUnchanged(items[i], ctx, () => go(i + 1));
|
|
1496
|
-
};
|
|
1497
|
-
go(0);
|
|
1498
|
-
return;
|
|
1499
|
-
}
|
|
1500
|
-
if (e.k === 'app') {
|
|
1501
|
-
const idx = this.asVar(e, ctx);
|
|
1502
|
-
if (idx >= 0) {
|
|
1503
|
-
if (nxt[idx] === undefined) {
|
|
1504
|
-
const v = idx >= this.nvars ? this.eval(e, ctx) : this.cur[idx];
|
|
1505
|
-
this.stTouch++;
|
|
1506
|
-
if (v === undefined)
|
|
1507
|
-
this.fail(`In evaluation, the identifier ${this.varNames[idx]} is either undefined or not an operator.`, e);
|
|
1508
|
-
nxt[idx] = v;
|
|
1509
|
-
this.nAssigned++;
|
|
1510
|
-
try {
|
|
1511
|
-
k();
|
|
1512
|
-
}
|
|
1513
|
-
finally {
|
|
1514
|
-
nxt[idx] = undefined;
|
|
1515
|
-
this.nAssigned--;
|
|
1516
|
-
}
|
|
1517
|
-
}
|
|
1518
|
-
else if (valEq(nxt[idx], idx >= this.nvars ? this.eval(e, ctx) : this.cur[idx]))
|
|
1519
|
-
k();
|
|
1520
|
-
return;
|
|
1521
|
-
}
|
|
1522
|
-
const x = this.expandApp(e, ctx);
|
|
1523
|
-
if (x) {
|
|
1524
|
-
this.enumUnchanged(x.body, x.ctx, k);
|
|
1525
|
-
return;
|
|
1526
|
-
}
|
|
1527
|
-
}
|
|
1528
|
-
if (this.unchangedVal(e, ctx))
|
|
1529
|
-
k();
|
|
1530
|
-
}
|
|
1531
|
-
}
|
|
1532
|
-
const NOARGS = [];
|
|
1533
|
-
export { cons, TlaError, EMPTY_SET, IntervalV, sortDedup, ModelValue };
|