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/model.js
DELETED
|
@@ -1,582 +0,0 @@
|
|
|
1
|
-
import { TlaError } from "./lexer.js";
|
|
2
|
-
import { Loader, levelOf, defLevel, LOCAL } from "./modules.js";
|
|
3
|
-
import { Evaluator, Lazy, Closure, DefRef } from "./eval.js";
|
|
4
|
-
import { buildStdlib, globalDefs } from "./stdlib.js";
|
|
5
|
-
import { OVERRIDES } from "./community.js";
|
|
6
|
-
import { EvalError, SetBase, ModelValue, isFcn, domainElems, fcnValues, fmt, strId, valEq } from "./values.js";
|
|
7
|
-
export class ConfigError extends TlaError {
|
|
8
|
-
constructor(msg) { super(msg, undefined, undefined, 'config'); }
|
|
9
|
-
}
|
|
10
|
-
export function locStr(loc, module) {
|
|
11
|
-
return `line ${loc[0]}, col ${loc[1]} to line ${loc[2]}, col ${loc[3]} of module ${module}`;
|
|
12
|
-
}
|
|
13
|
-
export function buildModel(rootName, resolve, cfg, io) {
|
|
14
|
-
const loader = new Loader(resolve, buildStdlib(io));
|
|
15
|
-
loader.globals = globalDefs();
|
|
16
|
-
const root = loader.load(rootName);
|
|
17
|
-
// native overrides for bundled modules
|
|
18
|
-
for (const [mname, ops] of Object.entries(OVERRIDES)) {
|
|
19
|
-
const m = loader.modules.get(mname);
|
|
20
|
-
if (!m)
|
|
21
|
-
continue;
|
|
22
|
-
for (const [name, [fn, opts]] of Object.entries(ops)) {
|
|
23
|
-
const d = m.syms.get(name);
|
|
24
|
-
if (d && d.k === 'def')
|
|
25
|
-
d.builtin = { fn, ...opts };
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
const ev = new Evaluator();
|
|
29
|
-
const vars = tlcVariableOrder(root);
|
|
30
|
-
vars.forEach((v, i) => { v.idx = i; });
|
|
31
|
-
ev.nvars = vars.length;
|
|
32
|
-
ev.varNames = vars.map(v => v.name);
|
|
33
|
-
const warnings = [];
|
|
34
|
-
const mkApp = (name, def) => ({ k: 'app', loc: def.loc ?? [0, 0, 0, 0], name, args: [], r: def });
|
|
35
|
-
const getDef = (name, what) => {
|
|
36
|
-
const s = root.syms.get(name);
|
|
37
|
-
if (!s)
|
|
38
|
-
throw new ConfigError(`The ${what} ${name} specified in the configuration file\nis not defined in the specification.`);
|
|
39
|
-
if (s.k === 'var')
|
|
40
|
-
throw new ConfigError(`The ${what} ${name} specified in the configuration file\nis not a defined operator.`);
|
|
41
|
-
return s;
|
|
42
|
-
};
|
|
43
|
-
// ---- constants ----
|
|
44
|
-
const opTables = new Map();
|
|
45
|
-
for (const c of cfg.constants) {
|
|
46
|
-
const scope = c.module ? loader.modules.get(c.module) : root;
|
|
47
|
-
const s = scope?.syms.get(c.name);
|
|
48
|
-
if (!s) {
|
|
49
|
-
if (c.subst !== undefined)
|
|
50
|
-
throw new ConfigError(`The constant parameter ${c.name} is not a constant or operator of the specification.`);
|
|
51
|
-
continue; // e.g. `a = a` just declares a model value
|
|
52
|
-
}
|
|
53
|
-
if (c.args) {
|
|
54
|
-
// F(v1, ..., vn) = value lines define a constant operator by a table
|
|
55
|
-
if (s.k !== 'const')
|
|
56
|
-
throw new ConfigError(`The constant parameter ${c.name} is not a constant operator of the specification.`);
|
|
57
|
-
let tbl = opTables.get(s);
|
|
58
|
-
if (!tbl) {
|
|
59
|
-
tbl = [];
|
|
60
|
-
opTables.set(s, tbl);
|
|
61
|
-
const rows = tbl, nm = c.name;
|
|
62
|
-
const params = c.args.map((_, i) => ({ name: `p${i}`, arity: 0 }));
|
|
63
|
-
s.subst = { k: 'def', name: nm, params, body: null, module: s.module, local: false, loc: [0, 0, 0, 0], builtin: { fn: (_ev, a) => {
|
|
64
|
-
for (const r of rows)
|
|
65
|
-
if (r.args.every((x, i) => valEq(x, a[i])))
|
|
66
|
-
return r.value;
|
|
67
|
-
throw new EvalError(`Attempted to apply the operator ${nm} to arguments (${a.map(x => fmt(x)).join(', ')}) for which the configuration file gives no value.`);
|
|
68
|
-
} } };
|
|
69
|
-
}
|
|
70
|
-
else if (tbl[0].args.length !== c.args.length)
|
|
71
|
-
throw new ConfigError(`The constant ${c.name} is given values with inconsistent arities in the configuration file.`);
|
|
72
|
-
tbl.push({ args: c.args, value: c.value });
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
75
|
-
if (c.subst !== undefined) {
|
|
76
|
-
const target = root.syms.get(c.subst);
|
|
77
|
-
if (!target || target.k !== 'def')
|
|
78
|
-
throw new ConfigError(`The configuration file substitutes for ${c.name} with the undefined identifier ${c.subst}.`);
|
|
79
|
-
if (s.k === 'const')
|
|
80
|
-
s.subst = target;
|
|
81
|
-
else if (s.k === 'def')
|
|
82
|
-
s.override = target;
|
|
83
|
-
}
|
|
84
|
-
else if (s.k === 'const') {
|
|
85
|
-
if (s.arity > 0)
|
|
86
|
-
throw new ConfigError(`The constant operator ${c.name} cannot be assigned a value; use <- to substitute an operator.`);
|
|
87
|
-
s.value = c.value;
|
|
88
|
-
s.hasValue = true;
|
|
89
|
-
}
|
|
90
|
-
else if (s.k === 'def') {
|
|
91
|
-
const value = c.value;
|
|
92
|
-
s.override = { k: 'def', name: s.name, params: [], body: null, module: s.module, local: false, loc: s.loc, builtin: { fn: () => value } };
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
// A constant replaced by a definition that is not constant-level (TLC evaluates the replacement at startup).
|
|
96
|
-
for (const c of cfg.constants) {
|
|
97
|
-
if (c.subst === undefined)
|
|
98
|
-
continue;
|
|
99
|
-
const s = (c.module ? loader.modules.get(c.module) : root)?.syms.get(c.name);
|
|
100
|
-
if (!s || s.k !== 'const' || !s.subst || s.subst.params.length !== 0 || s.subst.builtin)
|
|
101
|
-
continue;
|
|
102
|
-
const s0 = ev.stTouch;
|
|
103
|
-
try {
|
|
104
|
-
ev.cur = [];
|
|
105
|
-
ev.nxt = null;
|
|
106
|
-
ev.initMode = false;
|
|
107
|
-
ev.eval(mkApp(c.subst, s.subst), null);
|
|
108
|
-
}
|
|
109
|
-
catch (e) {
|
|
110
|
-
if (!(e instanceof TlaError)) {
|
|
111
|
-
if (e instanceof RangeError)
|
|
112
|
-
continue;
|
|
113
|
-
throw e;
|
|
114
|
-
}
|
|
115
|
-
if (defLevel(s.subst) > 0 || ev.stTouch !== s0) {
|
|
116
|
-
throw new ConfigError(`The configuration file substitutes constant ${c.name} with non-constant ${c.subst} - specifically: ${e.message}`);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
for (const c of root.consts) {
|
|
121
|
-
if (!c.hasValue && !c.subst) {
|
|
122
|
-
throw new ConfigError(`The constant parameter ${c.name} is not assigned a value by the configuration file.`);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
// SANY's level check: a temporal operator may not be applied to an action (no "raw TLA")
|
|
126
|
-
{
|
|
127
|
-
const errs = [];
|
|
128
|
-
const walk = (x) => {
|
|
129
|
-
if (!x || typeof x !== 'object')
|
|
130
|
-
return;
|
|
131
|
-
if (Array.isArray(x)) {
|
|
132
|
-
for (const y of x)
|
|
133
|
-
walk(y);
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
if ((x.k === 'always' || x.k === 'eventually' || x.k === 'leadsto' || x.k === 'guarantee') && x.loc) {
|
|
137
|
-
for (const a of x.a) {
|
|
138
|
-
if (a.k !== 'boxact' && a.k !== 'angleact' && levelOf(a) === 2) {
|
|
139
|
-
errs.push(`line ${a.loc[0]}, col ${a.loc[1]} to line ${a.loc[2]}, col ${a.loc[3]} of module ${a.mod ?? rootName}\n\nAction used where only temporal formula or state predicate allowed.`);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
for (const key in x) {
|
|
144
|
-
if (key === 'r' || key === 'loc' || key === 'instDefs' || key === 'ld')
|
|
145
|
-
continue;
|
|
146
|
-
const v = x[key];
|
|
147
|
-
if (v && typeof v === 'object')
|
|
148
|
-
walk(v);
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
for (const name of loader.loadOrder)
|
|
152
|
-
for (const d of loader.modules.get(name).ownDefs)
|
|
153
|
-
if (d.body)
|
|
154
|
-
walk(d.body);
|
|
155
|
-
if (errs.length)
|
|
156
|
-
throw new TlaError(`Semantic errors:\n\n*** Errors: ${errs.length}\n\n${errs.join('\n\n\n')}`, undefined, rootName, 'semantic');
|
|
157
|
-
}
|
|
158
|
-
const model = {
|
|
159
|
-
rootName, root, loader, ev, vars, inits: [], actions: [], invariants: [], impliedInits: [], impliedActions: [],
|
|
160
|
-
constraints: [], actionConstraints: [], fairness: [], liveProps: [], symmetry: null, view: null, alias: null,
|
|
161
|
-
postconditions: [], checkDeadlock: cfg.checkDeadlock ?? true, warnings,
|
|
162
|
-
};
|
|
163
|
-
// ---- invariants / constraints ----
|
|
164
|
-
for (const n of cfg.invariants) {
|
|
165
|
-
const d = getDef(n, 'invariant');
|
|
166
|
-
if (d.k === 'def' && d.params.length)
|
|
167
|
-
throw new ConfigError(`The invariant ${n} cannot take arguments.`);
|
|
168
|
-
if (d.k === 'def' && defLevel(d) >= 2)
|
|
169
|
-
throw new ConfigError(`The invariant ${n} is not a state predicate (it has primed variables or temporal operators).`);
|
|
170
|
-
model.invariants.push({ name: n, e: mkApp(n, d), ctx: null });
|
|
171
|
-
}
|
|
172
|
-
for (const n of cfg.constraints)
|
|
173
|
-
model.constraints.push({ name: n, e: mkApp(n, getDef(n, 'constraint')), ctx: null });
|
|
174
|
-
for (const n of cfg.actionConstraints)
|
|
175
|
-
model.actionConstraints.push({ name: n, e: mkApp(n, getDef(n, 'action constraint')), ctx: null });
|
|
176
|
-
if (cfg.view)
|
|
177
|
-
model.view = { name: cfg.view, e: mkApp(cfg.view, getDef(cfg.view, 'view')), ctx: null };
|
|
178
|
-
if (cfg.alias)
|
|
179
|
-
model.alias = { name: cfg.alias, e: mkApp(cfg.alias, getDef(cfg.alias, 'alias')), ctx: null };
|
|
180
|
-
const bodyLoc = (d) => {
|
|
181
|
-
let o = d.override ?? d;
|
|
182
|
-
while (o.inst)
|
|
183
|
-
o = o.inst.orig;
|
|
184
|
-
return { loc: o.body?.loc ?? o.loc, module: o.body?.mod ?? o.module };
|
|
185
|
-
};
|
|
186
|
-
for (const n of cfg.postconditions) {
|
|
187
|
-
const d = getDef(n, 'post condition');
|
|
188
|
-
model.postconditions.push({ name: n, e: mkApp(n, d), ctx: null, ...bodyLoc(d) });
|
|
189
|
-
}
|
|
190
|
-
// _POSSIBLE P: count how often P holds (state predicates: per generated state; actions: per transition) and
|
|
191
|
-
// require a non-zero count once model checking has finished.
|
|
192
|
-
if (cfg.possible.length) {
|
|
193
|
-
const pm = loader.load('_Possible');
|
|
194
|
-
const track = pm.syms.get('_Track'), check = pm.syms.get('_CheckName');
|
|
195
|
-
const str = (v) => ({ k: 'str', loc: [0, 0, 0, 0], v });
|
|
196
|
-
for (const n of cfg.possible) {
|
|
197
|
-
const s = root.syms.get(n);
|
|
198
|
-
if (!s)
|
|
199
|
-
throw new ConfigError(`The possible ${n} specified in the configuration file\nis not defined in the specification.`);
|
|
200
|
-
if (s.k !== 'def')
|
|
201
|
-
throw new ConfigError(`The possible ${n} specified in the configuration file\nis not a defined operator.`);
|
|
202
|
-
if (s.params.length)
|
|
203
|
-
throw new ConfigError(`The possible ${n} specified in the configuration file\nrequires arguments.`);
|
|
204
|
-
const lv = defLevel(s);
|
|
205
|
-
if (lv >= 3)
|
|
206
|
-
throw new ConfigError(`The possible ${n} specified in the configuration file\nis a temporal formula; only state- and action-level predicates are supported.`);
|
|
207
|
-
strId(n);
|
|
208
|
-
const e = { k: 'app', loc: s.loc, name: '_Track', args: [mkApp(n, s), str(n)], r: track };
|
|
209
|
-
(lv <= 1 ? model.constraints : model.actionConstraints).push({ name: n, e, ctx: null });
|
|
210
|
-
model.postconditions.push({ name: n, e: { k: 'app', loc: s.loc, name: '_CheckName', args: [str(n)], r: check }, ctx: null, possible: true, ...bodyLoc(s) });
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
// ---- behaviour spec ----
|
|
214
|
-
let nextPred = null;
|
|
215
|
-
const lvl = (e, ctx) => exprLevel(ev, e, ctx);
|
|
216
|
-
const processSpec = (e, ctx, name) => {
|
|
217
|
-
switch (e.k) {
|
|
218
|
-
case 'let':
|
|
219
|
-
processSpec(e.body, ev.bindLet(e.defs, ctx), name);
|
|
220
|
-
return;
|
|
221
|
-
case 'app': {
|
|
222
|
-
const x = ev.expandApp(e, ctx);
|
|
223
|
-
if (x) {
|
|
224
|
-
const nm = e.args.length === 0 && e.r !== LOCAL ? e.name : name;
|
|
225
|
-
if (lvl(x.body, x.ctx) <= 1) {
|
|
226
|
-
model.inits.push({ name: nm, e: x.body, ctx: x.ctx });
|
|
227
|
-
return;
|
|
228
|
-
}
|
|
229
|
-
processSpec(x.body, x.ctx, nm);
|
|
230
|
-
return;
|
|
231
|
-
}
|
|
232
|
-
break;
|
|
233
|
-
}
|
|
234
|
-
case 'and':
|
|
235
|
-
for (const c of e.a)
|
|
236
|
-
processSpec(c, ctx, name);
|
|
237
|
-
return;
|
|
238
|
-
case 'always': {
|
|
239
|
-
const arg = e.a[0];
|
|
240
|
-
if (arg.k === 'boxact') {
|
|
241
|
-
if (nextPred)
|
|
242
|
-
throw new ConfigError('TLC cannot handle a specification with more than one next-state relation ([][Next]_v conjunct).');
|
|
243
|
-
nextPred = { e: arg.a[0], ctx, name: '' };
|
|
244
|
-
return;
|
|
245
|
-
}
|
|
246
|
-
if (lvl(arg, ctx) <= 1)
|
|
247
|
-
throw new ConfigError('TLC cannot handle the temporal formula ' + name + ': a []P conjunct with a state predicate P in a specification is not supported (use an INVARIANT instead).');
|
|
248
|
-
model.fairness.push({ name, e, ctx });
|
|
249
|
-
return;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
const l = lvl(e, ctx);
|
|
253
|
-
if (l <= 1)
|
|
254
|
-
model.inits.push({ name, e, ctx });
|
|
255
|
-
else if (l === 3)
|
|
256
|
-
model.fairness.push({ name, e, ctx });
|
|
257
|
-
else
|
|
258
|
-
throw new ConfigError('TLC cannot handle a conjunct of the specification that is an action formula outside [][...]_v.');
|
|
259
|
-
};
|
|
260
|
-
if (cfg.spec !== undefined) {
|
|
261
|
-
if (cfg.init !== undefined || cfg.next !== undefined)
|
|
262
|
-
throw new ConfigError('The configuration file cannot specify both INIT/NEXT and SPECIFICATION fields.');
|
|
263
|
-
const d = getDef(cfg.spec, 'specification');
|
|
264
|
-
processSpec(mkApp(cfg.spec, d), null, cfg.spec);
|
|
265
|
-
}
|
|
266
|
-
else {
|
|
267
|
-
if (cfg.init !== undefined) {
|
|
268
|
-
const d = getDef(cfg.init, 'initial predicate');
|
|
269
|
-
model.inits.push({ name: cfg.init, e: mkApp(cfg.init, d), ctx: null });
|
|
270
|
-
}
|
|
271
|
-
if (cfg.next !== undefined) {
|
|
272
|
-
const d = getDef(cfg.next, 'next state action');
|
|
273
|
-
nextPred = { e: mkApp(cfg.next, d), ctx: null, name: cfg.next };
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
// a specification without variables has no behaviours to explore: TLC only checks the assumptions
|
|
277
|
-
if (vars.length === 0) {
|
|
278
|
-
model.inits.length = 0;
|
|
279
|
-
nextPred = null;
|
|
280
|
-
}
|
|
281
|
-
const noBehavior = (cfg.spec === undefined && cfg.init === undefined && cfg.next === undefined) || vars.length === 0;
|
|
282
|
-
if (model.inits.length === 0 && !noBehavior)
|
|
283
|
-
throw new ConfigError('The configuration file did not specify the initial state predicate.');
|
|
284
|
-
if (!nextPred && cfg.spec === undefined && !noBehavior)
|
|
285
|
-
throw new ConfigError('The configuration file did not specify the next state predicate.');
|
|
286
|
-
// ---- split Next into actions ----
|
|
287
|
-
const paramStr = (def, ctx) => {
|
|
288
|
-
if (!def || def.params.length === 0)
|
|
289
|
-
return '';
|
|
290
|
-
const vals = [];
|
|
291
|
-
for (const p of def.params) {
|
|
292
|
-
try {
|
|
293
|
-
const b = ev.lookup(ctx, p.name);
|
|
294
|
-
vals.push(fmt(ev.unthunk(b)));
|
|
295
|
-
}
|
|
296
|
-
catch {
|
|
297
|
-
vals.push('?');
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
return `(${vals.join(',')})`;
|
|
301
|
-
};
|
|
302
|
-
const split = (e, ctx, name, def, module) => {
|
|
303
|
-
const leaf = () => model.actions.push({ name, params: paramStr(def, ctx), loc: e.loc, module, e, ctx });
|
|
304
|
-
switch (e.k) {
|
|
305
|
-
case 'let':
|
|
306
|
-
split(e.body, ev.bindLet(e.defs, ctx), name, def, module);
|
|
307
|
-
return;
|
|
308
|
-
case 'app': {
|
|
309
|
-
const r = e.r;
|
|
310
|
-
if (r !== LOCAL && r.k === 'def' && !e.sel) {
|
|
311
|
-
let d = r.override ?? r;
|
|
312
|
-
if (!d.builtin) {
|
|
313
|
-
if (e.args.every((a) => lvl(a, ctx) === 0)) {
|
|
314
|
-
try {
|
|
315
|
-
const x = ev.expandApp(e, ctx);
|
|
316
|
-
if (x) {
|
|
317
|
-
let orig = d;
|
|
318
|
-
while (orig.inst)
|
|
319
|
-
orig = orig.inst.orig;
|
|
320
|
-
split(x.body, x.ctx, e.name, orig, orig.module);
|
|
321
|
-
return;
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
catch (err) {
|
|
325
|
-
if (!(err instanceof TlaError))
|
|
326
|
-
throw err;
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
model.actions.push({ name: e.name, params: '', loc: e.loc, module, e, ctx });
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
332
|
-
void d;
|
|
333
|
-
}
|
|
334
|
-
break;
|
|
335
|
-
}
|
|
336
|
-
case 'quant':
|
|
337
|
-
if (e.q === 'E') {
|
|
338
|
-
try {
|
|
339
|
-
const ctxs = [];
|
|
340
|
-
ev.forBindings(e.bounds, ctx, c => { ctxs.push(c); return false; });
|
|
341
|
-
if (ctxs.length > 0) {
|
|
342
|
-
for (const c of ctxs)
|
|
343
|
-
split(e.body, c, name, def, module);
|
|
344
|
-
return;
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
catch (err) {
|
|
348
|
-
if (!(err instanceof TlaError))
|
|
349
|
-
throw err;
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
break;
|
|
353
|
-
case 'or':
|
|
354
|
-
for (const d of e.a)
|
|
355
|
-
split(d, ctx, name, def, module);
|
|
356
|
-
return;
|
|
357
|
-
}
|
|
358
|
-
leaf();
|
|
359
|
-
};
|
|
360
|
-
if (nextPred) {
|
|
361
|
-
const np = nextPred;
|
|
362
|
-
split(np.e, np.ctx, np.name || 'Action', null, rootName);
|
|
363
|
-
}
|
|
364
|
-
// ---- properties ----
|
|
365
|
-
const processProp = (e, ctx, name) => {
|
|
366
|
-
switch (e.k) {
|
|
367
|
-
case 'let':
|
|
368
|
-
processProp(e.body, ev.bindLet(e.defs, ctx), name);
|
|
369
|
-
return;
|
|
370
|
-
case 'app': {
|
|
371
|
-
const x = ev.expandApp(e, ctx);
|
|
372
|
-
if (x) {
|
|
373
|
-
processProp(x.body, x.ctx, e.args.length === 0 && e.r !== LOCAL ? e.name : name);
|
|
374
|
-
return;
|
|
375
|
-
}
|
|
376
|
-
break;
|
|
377
|
-
}
|
|
378
|
-
case 'quant':
|
|
379
|
-
if (e.q === 'A' && lvl(e, ctx) === 3) {
|
|
380
|
-
try {
|
|
381
|
-
const ctxs = [];
|
|
382
|
-
ev.forBindings(e.bounds, ctx, c => { ctxs.push(c); return false; });
|
|
383
|
-
for (const c of ctxs)
|
|
384
|
-
processProp(e.body, c, name);
|
|
385
|
-
return;
|
|
386
|
-
}
|
|
387
|
-
catch (err) {
|
|
388
|
-
if (!(err instanceof TlaError))
|
|
389
|
-
throw err;
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
break;
|
|
393
|
-
case 'and':
|
|
394
|
-
for (const c of e.a)
|
|
395
|
-
processProp(c, ctx, name);
|
|
396
|
-
return;
|
|
397
|
-
case 'always': {
|
|
398
|
-
const arg = e.a[0];
|
|
399
|
-
if (arg.k === 'boxact') {
|
|
400
|
-
model.impliedActions.push({ name, e: arg, ctx });
|
|
401
|
-
return;
|
|
402
|
-
}
|
|
403
|
-
if (lvl(arg, ctx) < 2) {
|
|
404
|
-
const nm = arg.k === 'app' && arg.args.length === 0 && arg.r !== LOCAL ? arg.name : name;
|
|
405
|
-
model.invariants.push({ name: nm, e: arg, ctx });
|
|
406
|
-
return;
|
|
407
|
-
}
|
|
408
|
-
model.liveProps.push({ name, e, ctx });
|
|
409
|
-
return;
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
const l = lvl(e, ctx);
|
|
413
|
-
if (l <= 1)
|
|
414
|
-
model.impliedInits.push({ name, e, ctx });
|
|
415
|
-
else if (l === 3)
|
|
416
|
-
model.liveProps.push({ name, e, ctx });
|
|
417
|
-
else
|
|
418
|
-
throw new ConfigError(`The property ${name} is an action-level formula; TLC can only check action properties of the form [][A]_v.`);
|
|
419
|
-
};
|
|
420
|
-
for (const n of cfg.properties)
|
|
421
|
-
processProp(mkApp(n, getDef(n, 'property')), null, n);
|
|
422
|
-
// ---- symmetry ----
|
|
423
|
-
if (cfg.symmetry) {
|
|
424
|
-
const d = getDef(cfg.symmetry, 'symmetry function');
|
|
425
|
-
ev.cur = [];
|
|
426
|
-
ev.nxt = null;
|
|
427
|
-
const v = ev.eval(mkApp(cfg.symmetry, d), null);
|
|
428
|
-
if (!(v instanceof SetBase))
|
|
429
|
-
throw new ConfigError(`The symmetry operator ${cfg.symmetry} must evaluate to a set of permutations.`);
|
|
430
|
-
const perms = [];
|
|
431
|
-
for (const f of v.elems()) {
|
|
432
|
-
if (!isFcn(f))
|
|
433
|
-
throw new ConfigError('Symmetry function must have model values as domain and range.');
|
|
434
|
-
const dom = domainElems(f), rng = fcnValues(f);
|
|
435
|
-
const m = new Map();
|
|
436
|
-
let identity = true;
|
|
437
|
-
for (let i = 0; i < dom.length; i++) {
|
|
438
|
-
if (!(dom[i] instanceof ModelValue) || !(rng[i] instanceof ModelValue))
|
|
439
|
-
throw new ConfigError('Symmetry function must have model values as domain and range.');
|
|
440
|
-
m.set(dom[i], rng[i]);
|
|
441
|
-
if (dom[i] !== rng[i])
|
|
442
|
-
identity = false;
|
|
443
|
-
}
|
|
444
|
-
if (!identity)
|
|
445
|
-
perms.push(m);
|
|
446
|
-
}
|
|
447
|
-
// close the generators under composition (TLC computes the generated group)
|
|
448
|
-
const key = (m) => [...m.entries()].filter(([a, b]) => a !== b).map(([a, b]) => `${a.id}>${b.id}`).sort().join(',');
|
|
449
|
-
const seenPerms = new Set(['']);
|
|
450
|
-
const group = [];
|
|
451
|
-
for (const g of perms) {
|
|
452
|
-
const k = key(g);
|
|
453
|
-
if (!seenPerms.has(k)) {
|
|
454
|
-
seenPerms.add(k);
|
|
455
|
-
group.push(g);
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
const gens = group.slice();
|
|
459
|
-
for (let i = 0; i < group.length; i++) {
|
|
460
|
-
for (const g of gens) {
|
|
461
|
-
const c = new Map();
|
|
462
|
-
for (const [a, b] of group[i])
|
|
463
|
-
c.set(a, g.get(b) ?? b);
|
|
464
|
-
for (const [a, b] of g)
|
|
465
|
-
if (!c.has(a))
|
|
466
|
-
c.set(a, b);
|
|
467
|
-
const k = key(c);
|
|
468
|
-
if (!seenPerms.has(k)) {
|
|
469
|
-
seenPerms.add(k);
|
|
470
|
-
group.push(c);
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
model.symmetry = group.length ? group : null;
|
|
475
|
-
}
|
|
476
|
-
void EvalError;
|
|
477
|
-
void Lazy;
|
|
478
|
-
void Closure;
|
|
479
|
-
void DefRef;
|
|
480
|
-
return model;
|
|
481
|
-
}
|
|
482
|
-
// Level of an expression in a context: like levelOf, but looks through locally bound thunks.
|
|
483
|
-
export function exprLevel(ev, e, ctx) {
|
|
484
|
-
const seen = new Set();
|
|
485
|
-
const go = (n, c) => {
|
|
486
|
-
let l = levelOf(n);
|
|
487
|
-
if (l >= 3)
|
|
488
|
-
return l;
|
|
489
|
-
// local references may be bound to higher-level thunks
|
|
490
|
-
const visit = (x, cc) => {
|
|
491
|
-
if (l >= 3 || !x || typeof x !== 'object')
|
|
492
|
-
return;
|
|
493
|
-
if (x.k === 'app') {
|
|
494
|
-
if (x.r === LOCAL) {
|
|
495
|
-
let b;
|
|
496
|
-
try {
|
|
497
|
-
b = ev.lookup(cc, x.name);
|
|
498
|
-
}
|
|
499
|
-
catch {
|
|
500
|
-
b = undefined;
|
|
501
|
-
}
|
|
502
|
-
if (b instanceof Lazy && !seen.has(b.e)) {
|
|
503
|
-
seen.add(b.e);
|
|
504
|
-
l = Math.max(l, go(b.e, b.ctx));
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
else if (x.r && x.r.k !== 'def' && cc && cc.base) {
|
|
508
|
-
const b = ev.envLookup(cc.base, x.name);
|
|
509
|
-
if (b instanceof Lazy && !seen.has(b.e)) {
|
|
510
|
-
seen.add(b.e);
|
|
511
|
-
l = Math.max(l, go(b.e, b.ctx));
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
else if (x.r && x.r.k === 'def' && cc && cc.base && !x.r.builtin && x.r.body && !seen.has(x.r.body)) {
|
|
515
|
-
seen.add(x.r.body);
|
|
516
|
-
visit(x.r.body, cc.base);
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
for (const key of ['a', 'args'])
|
|
520
|
-
if (Array.isArray(x[key]))
|
|
521
|
-
for (const y of x[key])
|
|
522
|
-
visit(y, cc);
|
|
523
|
-
for (const key of ['body', 'e', 'f', 'base', 'pred', 'other'])
|
|
524
|
-
if (x[key] && typeof x[key] === 'object' && x[key].k)
|
|
525
|
-
visit(x[key], cc);
|
|
526
|
-
if (x.arms)
|
|
527
|
-
for (const arm of x.arms) {
|
|
528
|
-
visit(arm.c, cc);
|
|
529
|
-
visit(arm.e, cc);
|
|
530
|
-
}
|
|
531
|
-
if (x.fields)
|
|
532
|
-
for (const f of x.fields)
|
|
533
|
-
visit(f.e, cc);
|
|
534
|
-
if (x.bounds)
|
|
535
|
-
for (const b of x.bounds)
|
|
536
|
-
if (b.set)
|
|
537
|
-
visit(b.set, cc);
|
|
538
|
-
if (x.bound && x.bound.set)
|
|
539
|
-
visit(x.bound.set, cc);
|
|
540
|
-
if (x.defs)
|
|
541
|
-
for (const d of x.defs)
|
|
542
|
-
visit(d.body, cc);
|
|
543
|
-
if (x.updates)
|
|
544
|
-
for (const u of x.updates) {
|
|
545
|
-
for (const p of u.path)
|
|
546
|
-
visit(p, cc);
|
|
547
|
-
visit(u.val, cc);
|
|
548
|
-
}
|
|
549
|
-
};
|
|
550
|
-
visit(n, c);
|
|
551
|
-
return l;
|
|
552
|
-
};
|
|
553
|
-
return go(e, ctx);
|
|
554
|
-
}
|
|
555
|
-
// TLC lists variables in the iteration order of SANY's symbol Hashtable for the root module, so the
|
|
556
|
-
// order depends on Java's String.hashCode of each name and on the table capacity, which in turn
|
|
557
|
-
// depends on how many symbols the module context holds. Reproduced here so that printed states match.
|
|
558
|
-
const STD_SYMBOLS = {
|
|
559
|
-
Naturals: 12, Integers: 2, Reals: 3, Sequences: 8, FiniteSets: 2, TLC: 14, Bags: 12, TLCExt: 14, Randomization: 4,
|
|
560
|
-
};
|
|
561
|
-
function javaHash(s) {
|
|
562
|
-
let h = 0;
|
|
563
|
-
for (let i = 0; i < s.length; i++)
|
|
564
|
-
h = (Math.imul(h, 31) + s.charCodeAt(i)) | 0;
|
|
565
|
-
return h & 0x7fffffff;
|
|
566
|
-
}
|
|
567
|
-
export function tlcVariableOrder(root) {
|
|
568
|
-
let count = 72; // built-in operators in SANY's initial context
|
|
569
|
-
const std = new Set();
|
|
570
|
-
for (const sym of new Set(root.syms.values())) {
|
|
571
|
-
if (sym.k === 'def' && sym.builtin && STD_SYMBOLS[sym.module] !== undefined)
|
|
572
|
-
std.add(sym.module);
|
|
573
|
-
else
|
|
574
|
-
count++;
|
|
575
|
-
}
|
|
576
|
-
for (const m of std)
|
|
577
|
-
count += STD_SYMBOLS[m];
|
|
578
|
-
let cap = 11;
|
|
579
|
-
while (count > Math.floor(cap * 0.75))
|
|
580
|
-
cap = cap * 2 + 1;
|
|
581
|
-
return root.vars.map((v, i) => ({ v, b: javaHash(v.name) % cap, i })).sort((x, y) => x.b - y.b || x.i - y.i).map(x => x.v);
|
|
582
|
-
}
|
package/dist/modules.d.ts
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import type { Node, Param, ModuleNode } from './parser.ts';
|
|
2
|
-
import type { Loc } from './lexer.ts';
|
|
3
|
-
import type { Value } from './values.ts';
|
|
4
|
-
export interface Decl {
|
|
5
|
-
k: 'const' | 'var';
|
|
6
|
-
name: string;
|
|
7
|
-
arity: number;
|
|
8
|
-
idx: number;
|
|
9
|
-
module: string;
|
|
10
|
-
value?: Value;
|
|
11
|
-
subst?: Def;
|
|
12
|
-
hasValue?: boolean;
|
|
13
|
-
}
|
|
14
|
-
export interface Builtin {
|
|
15
|
-
fn: (ev: any, args: any[]) => Value;
|
|
16
|
-
ops?: number[];
|
|
17
|
-
lazy?: boolean;
|
|
18
|
-
}
|
|
19
|
-
export interface Def {
|
|
20
|
-
k: 'def';
|
|
21
|
-
name: string;
|
|
22
|
-
params: Param[];
|
|
23
|
-
body: Node | null;
|
|
24
|
-
module: string;
|
|
25
|
-
local: boolean;
|
|
26
|
-
loc: Loc;
|
|
27
|
-
builtin?: Builtin;
|
|
28
|
-
inst?: {
|
|
29
|
-
orig: Def;
|
|
30
|
-
substs: {
|
|
31
|
-
name: string;
|
|
32
|
-
e: Node;
|
|
33
|
-
arity: number;
|
|
34
|
-
}[];
|
|
35
|
-
nparams: number;
|
|
36
|
-
envCache?: any;
|
|
37
|
-
};
|
|
38
|
-
cached?: boolean;
|
|
39
|
-
cache?: Value;
|
|
40
|
-
level?: number;
|
|
41
|
-
override?: Def;
|
|
42
|
-
}
|
|
43
|
-
export type Sym = Decl | Def;
|
|
44
|
-
export declare const LOCAL: {
|
|
45
|
-
readonly k: "local";
|
|
46
|
-
};
|
|
47
|
-
export interface ModuleInfo {
|
|
48
|
-
name: string;
|
|
49
|
-
syms: Map<string, Sym>;
|
|
50
|
-
exports: Map<string, Sym>;
|
|
51
|
-
consts: Decl[];
|
|
52
|
-
vars: Decl[];
|
|
53
|
-
assumes: {
|
|
54
|
-
e: Node;
|
|
55
|
-
name: string | null;
|
|
56
|
-
module: string;
|
|
57
|
-
}[];
|
|
58
|
-
ownDefs: Def[];
|
|
59
|
-
order: string[];
|
|
60
|
-
}
|
|
61
|
-
export type SourceResolver = (name: string) => string | null;
|
|
62
|
-
interface Scope {
|
|
63
|
-
names: Map<string, {
|
|
64
|
-
used: boolean;
|
|
65
|
-
def?: Node;
|
|
66
|
-
}>;
|
|
67
|
-
parent: Scope | null;
|
|
68
|
-
}
|
|
69
|
-
export declare class Loader {
|
|
70
|
-
modules: Map<string, ModuleInfo>;
|
|
71
|
-
builtins: Map<string, ModuleInfo>;
|
|
72
|
-
resolveSource: SourceResolver;
|
|
73
|
-
loadOrder: string[];
|
|
74
|
-
loading: Set<string>;
|
|
75
|
-
globals: Map<string, Def>;
|
|
76
|
-
constructor(resolveSource: SourceResolver, builtins: Map<string, ModuleInfo>);
|
|
77
|
-
extraSources: Map<string, string>;
|
|
78
|
-
scanExtraModules(src: string): void;
|
|
79
|
-
load(name: string, from?: string): ModuleInfo;
|
|
80
|
-
process(ast: ModuleNode, outer: ModuleInfo | null): ModuleInfo;
|
|
81
|
-
lookupScope(scope: Scope | null, name: string): {
|
|
82
|
-
used: boolean;
|
|
83
|
-
def?: Node;
|
|
84
|
-
} | null;
|
|
85
|
-
resolve(n: Node, scope: Scope | null, mod: ModuleInfo): void;
|
|
86
|
-
}
|
|
87
|
-
export declare function levelOf(n: Node): number;
|
|
88
|
-
export declare function defLevel(d: Def): number;
|
|
89
|
-
export declare function declRefs(d: Def): Set<string>;
|
|
90
|
-
export declare function tailFn(n: Node): boolean;
|
|
91
|
-
export {};
|