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/values.js
DELETED
|
@@ -1,822 +0,0 @@
|
|
|
1
|
-
// Runtime values. Booleans, integers and strings are native JS values; everything else is a class.
|
|
2
|
-
// Functions are kept in canonical form: domain 1..n => TupleV, non-empty all-string domain => RecordV,
|
|
3
|
-
// anything else => FcnV. Sets are either an explicit sorted SetV or one of the symbolic set classes.
|
|
4
|
-
import { TlaError } from "./lexer.js";
|
|
5
|
-
export class EvalError extends TlaError {
|
|
6
|
-
constructor(msg) { super(msg, undefined, undefined, 'eval'); }
|
|
7
|
-
}
|
|
8
|
-
const err = (msg) => { throw new EvalError(msg); };
|
|
9
|
-
// ---- string interning: TLC orders strings by first-seen order, not lexicographically ----
|
|
10
|
-
const strIds = new Map();
|
|
11
|
-
export function strId(s) {
|
|
12
|
-
let id = strIds.get(s);
|
|
13
|
-
if (id === undefined) {
|
|
14
|
-
id = strIds.size + 1;
|
|
15
|
-
strIds.set(s, id);
|
|
16
|
-
}
|
|
17
|
-
return id;
|
|
18
|
-
}
|
|
19
|
-
export function resetInterning() { strIds.clear(); modelValues.clear(); }
|
|
20
|
-
export class ModelValue {
|
|
21
|
-
name;
|
|
22
|
-
id;
|
|
23
|
-
type; // typed model values: "a_1" has type "a"
|
|
24
|
-
constructor(name, id) {
|
|
25
|
-
this.name = name;
|
|
26
|
-
this.id = id;
|
|
27
|
-
this.type = name.length > 2 && name[1] === '_' ? name[0] : null;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
const modelValues = new Map();
|
|
31
|
-
export function modelValue(name) {
|
|
32
|
-
let m = modelValues.get(name);
|
|
33
|
-
if (!m) {
|
|
34
|
-
m = new ModelValue(name, strId(name));
|
|
35
|
-
modelValues.set(name, m);
|
|
36
|
-
}
|
|
37
|
-
return m;
|
|
38
|
-
}
|
|
39
|
-
export function allModelValues() { return [...modelValues.values()]; }
|
|
40
|
-
export class TupleV {
|
|
41
|
-
e;
|
|
42
|
-
constructor(e) { this.e = e; }
|
|
43
|
-
}
|
|
44
|
-
export class RecordV {
|
|
45
|
-
names; // sorted by string order
|
|
46
|
-
vals;
|
|
47
|
-
constructor(names, vals) { this.names = names; this.vals = vals; }
|
|
48
|
-
get(name) {
|
|
49
|
-
const i = this.names.indexOf(name);
|
|
50
|
-
return i < 0 ? undefined : this.vals[i];
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
export class FcnV {
|
|
54
|
-
dom; // sorted, deduplicated
|
|
55
|
-
vals;
|
|
56
|
-
constructor(dom, vals) { this.dom = dom; this.vals = vals; }
|
|
57
|
-
}
|
|
58
|
-
export const EMPTY_TUPLE = new TupleV([]);
|
|
59
|
-
export class SetBase {
|
|
60
|
-
enumerable() { return true; }
|
|
61
|
-
size() { return this.elems().length; }
|
|
62
|
-
}
|
|
63
|
-
export class SetV extends SetBase {
|
|
64
|
-
e;
|
|
65
|
-
constructor(sortedElems) { super(); this.e = sortedElems; }
|
|
66
|
-
contains(v) { return bsearch(this.e, v) >= 0; }
|
|
67
|
-
elems() { return this.e; }
|
|
68
|
-
size() { return this.e.length; }
|
|
69
|
-
}
|
|
70
|
-
export const EMPTY_SET = new SetV([]);
|
|
71
|
-
export const BOOLEAN_SET = new SetV([false, true]);
|
|
72
|
-
export class IntervalV extends SetBase {
|
|
73
|
-
lo;
|
|
74
|
-
hi;
|
|
75
|
-
cache = null;
|
|
76
|
-
constructor(lo, hi) { super(); this.lo = lo; this.hi = hi; }
|
|
77
|
-
contains(v) { return typeof v === 'number' ? Number.isInteger(v) && v >= this.lo && v <= this.hi : (this.hi < this.lo || (typeof v === 'object' && v instanceof ModelValue) ? false : err(`Attempted to check if the value:\n${fmt(v)}\nis in the integer interval ${fmt(this)}`)); }
|
|
78
|
-
elems() {
|
|
79
|
-
if (!this.cache) {
|
|
80
|
-
const a = [];
|
|
81
|
-
for (let i = this.lo; i <= this.hi; i++)
|
|
82
|
-
a.push(i);
|
|
83
|
-
this.cache = a;
|
|
84
|
-
}
|
|
85
|
-
return this.cache;
|
|
86
|
-
}
|
|
87
|
-
size() { return Math.max(0, this.hi - this.lo + 1); }
|
|
88
|
-
}
|
|
89
|
-
export class SubsetV extends SetBase {
|
|
90
|
-
base;
|
|
91
|
-
cache = null;
|
|
92
|
-
constructor(base) { super(); this.base = base; }
|
|
93
|
-
contains(v) {
|
|
94
|
-
if (!(v instanceof SetBase))
|
|
95
|
-
return err(`Attempted to check if the non-set value:\n${fmt(v)}\nis in ${fmt(this)}`);
|
|
96
|
-
if (!v.enumerable())
|
|
97
|
-
return err('Attempted to check if a non-enumerable set is an element of SUBSET');
|
|
98
|
-
for (const x of v.elems())
|
|
99
|
-
if (!this.base.contains(x))
|
|
100
|
-
return false;
|
|
101
|
-
return true;
|
|
102
|
-
}
|
|
103
|
-
enumerable() { return this.base.enumerable(); }
|
|
104
|
-
size() { return 2 ** this.base.size(); }
|
|
105
|
-
elems() {
|
|
106
|
-
if (this.cache)
|
|
107
|
-
return this.cache;
|
|
108
|
-
const b = this.base.elems();
|
|
109
|
-
const n = b.length;
|
|
110
|
-
if (n > 26)
|
|
111
|
-
err(`Attempted to enumerate SUBSET of a set with ${n} elements; this is too large.`);
|
|
112
|
-
const out = [];
|
|
113
|
-
// by size, then lexicographic: this is the normalized set order
|
|
114
|
-
const idx = [];
|
|
115
|
-
const rec = (start, k) => {
|
|
116
|
-
if (k === 0) {
|
|
117
|
-
out.push(new SetV(idx.map(i => b[i])));
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
for (let i = start; i <= n - k; i++) {
|
|
121
|
-
idx.push(i);
|
|
122
|
-
rec(i + 1, k - 1);
|
|
123
|
-
idx.pop();
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
for (let k = 0; k <= n; k++)
|
|
127
|
-
rec(0, k);
|
|
128
|
-
return (this.cache = out);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
export class FcnSetV extends SetBase {
|
|
132
|
-
dom;
|
|
133
|
-
rng;
|
|
134
|
-
cache = null;
|
|
135
|
-
constructor(dom, rng) { super(); this.dom = dom; this.rng = rng; }
|
|
136
|
-
contains(v) {
|
|
137
|
-
const d = domainOf(v);
|
|
138
|
-
if (d === null) {
|
|
139
|
-
if (v instanceof ModelValue)
|
|
140
|
-
return v.type === null ? false : err(`Attempted to check if the typed model value ${v.name} is an element of\n${fmt(this)}`);
|
|
141
|
-
if (v instanceof SetBase || typeof v !== 'object')
|
|
142
|
-
return err(`Attempted to check if \n${fmt(v)}\nwhich is not a TLC function value, is in the set of functions:\n${fmt(this)}`);
|
|
143
|
-
return false;
|
|
144
|
-
}
|
|
145
|
-
if (!setEq(d, this.dom))
|
|
146
|
-
return false;
|
|
147
|
-
for (const x of fcnValues(v))
|
|
148
|
-
if (!this.rng.contains(x))
|
|
149
|
-
return false;
|
|
150
|
-
return true;
|
|
151
|
-
}
|
|
152
|
-
enumerable() { return this.dom.enumerable() && this.rng.enumerable(); }
|
|
153
|
-
size() { return this.rng.size() ** this.dom.size(); }
|
|
154
|
-
elems() {
|
|
155
|
-
if (this.cache)
|
|
156
|
-
return this.cache;
|
|
157
|
-
const d = this.dom.elems(), r = this.rng.elems();
|
|
158
|
-
const out = [];
|
|
159
|
-
const n = d.length;
|
|
160
|
-
if (r.length === 0)
|
|
161
|
-
return (this.cache = n === 0 ? [EMPTY_TUPLE] : []);
|
|
162
|
-
const total = r.length ** n;
|
|
163
|
-
if (total > 5e7)
|
|
164
|
-
err(`Attempted to enumerate a set of functions with ${total} elements; this is too large.`);
|
|
165
|
-
const idx = new Array(n).fill(0);
|
|
166
|
-
for (;;) {
|
|
167
|
-
out.push(makeFcn(d, idx.map(i => r[i])));
|
|
168
|
-
let k = n - 1;
|
|
169
|
-
while (k >= 0 && ++idx[k] === r.length) {
|
|
170
|
-
idx[k] = 0;
|
|
171
|
-
k--;
|
|
172
|
-
}
|
|
173
|
-
if (k < 0)
|
|
174
|
-
break;
|
|
175
|
-
}
|
|
176
|
-
return (this.cache = sortDedup(out));
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
export class RecSetV extends SetBase {
|
|
180
|
-
names;
|
|
181
|
-
sets; // names sorted
|
|
182
|
-
cache = null;
|
|
183
|
-
constructor(names, sets) { super(); this.names = names; this.sets = sets; }
|
|
184
|
-
contains(v) {
|
|
185
|
-
if (!(v instanceof RecordV) || v.names.length !== this.names.length)
|
|
186
|
-
return false;
|
|
187
|
-
for (let i = 0; i < this.names.length; i++) {
|
|
188
|
-
if (v.names[i] !== this.names[i] || !this.sets[i].contains(v.vals[i]))
|
|
189
|
-
return false;
|
|
190
|
-
}
|
|
191
|
-
return true;
|
|
192
|
-
}
|
|
193
|
-
enumerable() { return this.sets.every(s => s.enumerable()); }
|
|
194
|
-
size() { return this.sets.reduce((a, s) => a * s.size(), 1); }
|
|
195
|
-
elems() {
|
|
196
|
-
if (this.cache)
|
|
197
|
-
return this.cache;
|
|
198
|
-
const es = this.sets.map(s => s.elems());
|
|
199
|
-
const out = [];
|
|
200
|
-
if (es.some(e => e.length === 0))
|
|
201
|
-
return (this.cache = out);
|
|
202
|
-
const n = es.length;
|
|
203
|
-
const idx = new Array(n).fill(0);
|
|
204
|
-
for (;;) {
|
|
205
|
-
out.push(new RecordV(this.names, idx.map((i, k) => es[k][i])));
|
|
206
|
-
let k = n - 1;
|
|
207
|
-
while (k >= 0 && ++idx[k] === es[k].length) {
|
|
208
|
-
idx[k] = 0;
|
|
209
|
-
k--;
|
|
210
|
-
}
|
|
211
|
-
if (k < 0)
|
|
212
|
-
break;
|
|
213
|
-
}
|
|
214
|
-
return (this.cache = sortDedup(out));
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
export class CrossV extends SetBase {
|
|
218
|
-
sets;
|
|
219
|
-
cache = null;
|
|
220
|
-
constructor(sets) { super(); this.sets = sets; }
|
|
221
|
-
contains(v) {
|
|
222
|
-
if (!(v instanceof TupleV) || v.e.length !== this.sets.length)
|
|
223
|
-
return false;
|
|
224
|
-
for (let i = 0; i < this.sets.length; i++)
|
|
225
|
-
if (!this.sets[i].contains(v.e[i]))
|
|
226
|
-
return false;
|
|
227
|
-
return true;
|
|
228
|
-
}
|
|
229
|
-
enumerable() { return this.sets.every(s => s.enumerable()); }
|
|
230
|
-
size() { return this.sets.reduce((a, s) => a * s.size(), 1); }
|
|
231
|
-
elems() {
|
|
232
|
-
if (this.cache)
|
|
233
|
-
return this.cache;
|
|
234
|
-
const es = this.sets.map(s => s.elems());
|
|
235
|
-
const out = [];
|
|
236
|
-
if (es.some(e => e.length === 0))
|
|
237
|
-
return (this.cache = out);
|
|
238
|
-
const n = es.length;
|
|
239
|
-
const idx = new Array(n).fill(0);
|
|
240
|
-
for (;;) {
|
|
241
|
-
out.push(new TupleV(idx.map((i, k) => es[k][i])));
|
|
242
|
-
let k = n - 1;
|
|
243
|
-
while (k >= 0 && ++idx[k] === es[k].length) {
|
|
244
|
-
idx[k] = 0;
|
|
245
|
-
k--;
|
|
246
|
-
}
|
|
247
|
-
if (k < 0)
|
|
248
|
-
break;
|
|
249
|
-
}
|
|
250
|
-
return (this.cache = out);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
export class SeqSetV extends SetBase {
|
|
254
|
-
base;
|
|
255
|
-
constructor(base) { super(); this.base = base; }
|
|
256
|
-
contains(v) {
|
|
257
|
-
if (!(v instanceof TupleV))
|
|
258
|
-
return false;
|
|
259
|
-
for (const x of v.e)
|
|
260
|
-
if (!this.base.contains(x))
|
|
261
|
-
return false;
|
|
262
|
-
return true;
|
|
263
|
-
}
|
|
264
|
-
enumerable() { return false; }
|
|
265
|
-
elems() { return err(`Attempted to enumerate the non-enumerable set Seq(${fmt(this.base)}).`); }
|
|
266
|
-
}
|
|
267
|
-
export class InfSetV extends SetBase {
|
|
268
|
-
kind;
|
|
269
|
-
constructor(kind) { super(); this.kind = kind; }
|
|
270
|
-
contains(v) {
|
|
271
|
-
switch (this.kind) {
|
|
272
|
-
case 'Nat': return typeof v === 'number' ? Number.isInteger(v) && v >= 0 : v instanceof ModelValue ? false : err(`Attempted to check if the value:\n${fmt(v)}\nis in Nat.`);
|
|
273
|
-
case 'Int': return typeof v === 'number' ? Number.isInteger(v) : v instanceof ModelValue ? false : err(`Attempted to check if the value:\n${fmt(v)}\nis in Int.`);
|
|
274
|
-
case 'Real': return typeof v === 'number' ? true : v instanceof ModelValue ? false : err(`Attempted to check if the value:\n${fmt(v)}\nis in Real.`);
|
|
275
|
-
default: return typeof v === 'string' ? true : v instanceof ModelValue ? false : err(`Attempted to check if the value:\n${fmt(v)}\nis in STRING.`);
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
enumerable() { return false; }
|
|
279
|
-
elems() { return err(`Attempted to enumerate the non-enumerable set ${this.kind}.`); }
|
|
280
|
-
}
|
|
281
|
-
// Lazy union / intersection / difference / filter, used only when an operand is not enumerable.
|
|
282
|
-
export class LazySetV extends SetBase {
|
|
283
|
-
d;
|
|
284
|
-
get desc() { return typeof this.d === 'string' ? this.d : this.d(); }
|
|
285
|
-
pred;
|
|
286
|
-
constructor(desc, pred) { super(); this.d = desc; this.pred = pred; }
|
|
287
|
-
contains(v) { return this.pred(v); }
|
|
288
|
-
enumerable() { return false; }
|
|
289
|
-
elems() { return err(`Attempted to enumerate a non-enumerable set (${this.desc}).`); }
|
|
290
|
-
}
|
|
291
|
-
// A set defined by a membership predicate plus an on-demand enumeration (TLC's lazy SetPred/SetCup/...).
|
|
292
|
-
export class DerivedSetV extends SetBase {
|
|
293
|
-
d;
|
|
294
|
-
get desc() { return typeof this.d === 'string' ? this.d : this.d(); }
|
|
295
|
-
pred;
|
|
296
|
-
gen;
|
|
297
|
-
cache = null;
|
|
298
|
-
constructor(desc, pred, gen) { super(); this.d = desc; this.pred = pred; this.gen = gen; }
|
|
299
|
-
contains(v) { return this.cache ? bsearch(this.cache, v) >= 0 : this.pred(v); }
|
|
300
|
-
enumerable() { return this.gen !== null; }
|
|
301
|
-
elems() {
|
|
302
|
-
if (this.cache)
|
|
303
|
-
return this.cache;
|
|
304
|
-
if (!this.gen)
|
|
305
|
-
return err(`Attempted to enumerate a non-enumerable set (${this.desc}).`);
|
|
306
|
-
return (this.cache = sortDedup(this.gen()));
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
// ---- kinds, comparison, equality ----
|
|
310
|
-
const K_BOOL = 0, K_INT = 1, K_STR = 2, K_MODEL = 3, K_TUPLE = 4, K_REC = 5, K_FCN = 6, K_SET = 7, K_OTHER = 8;
|
|
311
|
-
function kindOf(v) {
|
|
312
|
-
switch (typeof v) {
|
|
313
|
-
case 'boolean': return K_BOOL;
|
|
314
|
-
case 'number': return K_INT;
|
|
315
|
-
case 'string': return K_STR;
|
|
316
|
-
}
|
|
317
|
-
if (v instanceof SetBase)
|
|
318
|
-
return K_SET;
|
|
319
|
-
if (v instanceof TupleV)
|
|
320
|
-
return K_TUPLE;
|
|
321
|
-
if (v instanceof RecordV)
|
|
322
|
-
return K_REC;
|
|
323
|
-
if (v instanceof FcnV)
|
|
324
|
-
return K_FCN;
|
|
325
|
-
if (v instanceof ModelValue)
|
|
326
|
-
return K_MODEL;
|
|
327
|
-
return K_OTHER;
|
|
328
|
-
}
|
|
329
|
-
const KIND_NAME = ['boolean', 'integer', 'string', 'model value', 'tuple', 'record', 'function', 'set', 'operator'];
|
|
330
|
-
function cmpFail(a, b) {
|
|
331
|
-
return err(`Attempted to compare the ${KIND_NAME[kindOf(a)]} ${fmt(a)} with the ${KIND_NAME[kindOf(b)]} ${fmt(b)}`);
|
|
332
|
-
}
|
|
333
|
-
function eqFail(a, b) {
|
|
334
|
-
return err(`Attempted to check equality of the ${KIND_NAME[kindOf(a)]} ${fmt(a)} with the ${KIND_NAME[kindOf(b)]} ${fmt(b)}`);
|
|
335
|
-
}
|
|
336
|
-
// Total order used for set normalization. Mirrors TLC's compareTo.
|
|
337
|
-
const INF_ORDER = ['Nat', 'Int', 'Real', 'STRING'];
|
|
338
|
-
export function compare(a, b) {
|
|
339
|
-
if (typeof a === 'number') {
|
|
340
|
-
if (typeof b === 'number')
|
|
341
|
-
return a < b ? -1 : a > b ? 1 : 0;
|
|
342
|
-
if (b instanceof ModelValue)
|
|
343
|
-
return modelCmp(b, a) * -1;
|
|
344
|
-
return cmpFail(a, b);
|
|
345
|
-
}
|
|
346
|
-
if (typeof a === 'string') {
|
|
347
|
-
if (typeof b === 'string')
|
|
348
|
-
return a === b ? 0 : strId(a) - strId(b);
|
|
349
|
-
if (b instanceof ModelValue)
|
|
350
|
-
return modelCmp(b, a) * -1;
|
|
351
|
-
return cmpFail(a, b);
|
|
352
|
-
}
|
|
353
|
-
if (typeof a === 'boolean') {
|
|
354
|
-
if (typeof b === 'boolean')
|
|
355
|
-
return a === b ? 0 : a ? 1 : -1;
|
|
356
|
-
if (b instanceof ModelValue)
|
|
357
|
-
return modelCmp(b, a) * -1;
|
|
358
|
-
return cmpFail(a, b);
|
|
359
|
-
}
|
|
360
|
-
if (a instanceof ModelValue)
|
|
361
|
-
return modelCmp(a, b);
|
|
362
|
-
if (b instanceof ModelValue)
|
|
363
|
-
return modelCmp(b, a) * -1;
|
|
364
|
-
if (a instanceof SetBase) {
|
|
365
|
-
if (!(b instanceof SetBase))
|
|
366
|
-
return cmpFail(a, b);
|
|
367
|
-
if (a instanceof InfSetV || b instanceof InfSetV) {
|
|
368
|
-
if (a instanceof InfSetV && b instanceof InfSetV)
|
|
369
|
-
return INF_ORDER.indexOf(a.kind) - INF_ORDER.indexOf(b.kind);
|
|
370
|
-
const [u, o] = a instanceof InfSetV ? [a, b] : [b, a];
|
|
371
|
-
return err(`Attempted to compare overridden value ${fmt(u)} with non-overridden value:\n${fmt(o)}`);
|
|
372
|
-
}
|
|
373
|
-
const x = a.elems(), y = b.elems();
|
|
374
|
-
if (x.length !== y.length)
|
|
375
|
-
return x.length - y.length;
|
|
376
|
-
for (let i = 0; i < x.length; i++) {
|
|
377
|
-
const c = compare(x[i], y[i]);
|
|
378
|
-
if (c !== 0)
|
|
379
|
-
return c;
|
|
380
|
-
}
|
|
381
|
-
return 0;
|
|
382
|
-
}
|
|
383
|
-
if (a instanceof TupleV) {
|
|
384
|
-
if (b instanceof TupleV) {
|
|
385
|
-
if (a.e.length !== b.e.length)
|
|
386
|
-
return a.e.length - b.e.length;
|
|
387
|
-
for (let i = 0; i < a.e.length; i++) {
|
|
388
|
-
const c = compare(a.e[i], b.e[i]);
|
|
389
|
-
if (c !== 0)
|
|
390
|
-
return c;
|
|
391
|
-
}
|
|
392
|
-
return 0;
|
|
393
|
-
}
|
|
394
|
-
if (b instanceof RecordV || b instanceof FcnV)
|
|
395
|
-
return fcnCmp(a, b);
|
|
396
|
-
return cmpFail(a, b);
|
|
397
|
-
}
|
|
398
|
-
if (a instanceof RecordV) {
|
|
399
|
-
if (b instanceof RecordV) {
|
|
400
|
-
const n = a.names.length;
|
|
401
|
-
if (n !== b.names.length)
|
|
402
|
-
return n - b.names.length;
|
|
403
|
-
for (let i = 0; i < n; i++) {
|
|
404
|
-
if (a.names[i] !== b.names[i])
|
|
405
|
-
return strId(a.names[i]) - strId(b.names[i]);
|
|
406
|
-
}
|
|
407
|
-
for (let i = 0; i < n; i++) {
|
|
408
|
-
const c = compare(a.vals[i], b.vals[i]);
|
|
409
|
-
if (c !== 0)
|
|
410
|
-
return c;
|
|
411
|
-
}
|
|
412
|
-
return 0;
|
|
413
|
-
}
|
|
414
|
-
if (b instanceof TupleV || b instanceof FcnV)
|
|
415
|
-
return fcnCmp(a, b);
|
|
416
|
-
return cmpFail(a, b);
|
|
417
|
-
}
|
|
418
|
-
if (a instanceof FcnV) {
|
|
419
|
-
if (b instanceof FcnV || b instanceof TupleV || b instanceof RecordV)
|
|
420
|
-
return fcnCmp(a, b);
|
|
421
|
-
return cmpFail(a, b);
|
|
422
|
-
}
|
|
423
|
-
return cmpFail(a, b);
|
|
424
|
-
}
|
|
425
|
-
function modelCmp(m, o) {
|
|
426
|
-
if (o instanceof ModelValue) {
|
|
427
|
-
if (m.type !== o.type && m.type !== null && o.type !== null)
|
|
428
|
-
return err(`Attempted to compare the differently-typed model values ${m.name} and ${o.name}`);
|
|
429
|
-
return m.id - o.id;
|
|
430
|
-
}
|
|
431
|
-
if (m.type !== null)
|
|
432
|
-
return err(`Attempted to compare the typed model value ${m.name} with the non-model value\n${fmt(o)}`);
|
|
433
|
-
return -1; // an untyped model value is smaller than every non-model value
|
|
434
|
-
}
|
|
435
|
-
function fcnCmp(a, b) {
|
|
436
|
-
const da = domainElems(a), db = domainElems(b);
|
|
437
|
-
if (da.length !== db.length)
|
|
438
|
-
return da.length - db.length;
|
|
439
|
-
const va = fcnValues(a), vb = fcnValues(b);
|
|
440
|
-
for (let i = 0; i < da.length; i++) {
|
|
441
|
-
const c = compare(da[i], db[i]);
|
|
442
|
-
if (c !== 0)
|
|
443
|
-
return c;
|
|
444
|
-
}
|
|
445
|
-
for (let i = 0; i < da.length; i++) {
|
|
446
|
-
const c = compare(va[i], vb[i]);
|
|
447
|
-
if (c !== 0)
|
|
448
|
-
return c;
|
|
449
|
-
}
|
|
450
|
-
return 0;
|
|
451
|
-
}
|
|
452
|
-
export function valEq(a, b) {
|
|
453
|
-
if (a === b)
|
|
454
|
-
return true;
|
|
455
|
-
const ta = typeof a, tb = typeof b;
|
|
456
|
-
if (ta !== 'object') {
|
|
457
|
-
if (tb !== 'object') {
|
|
458
|
-
if (ta !== tb)
|
|
459
|
-
return eqFail(a, b);
|
|
460
|
-
return false;
|
|
461
|
-
}
|
|
462
|
-
if (b instanceof ModelValue)
|
|
463
|
-
return modelEq(b, a);
|
|
464
|
-
return eqFail(a, b);
|
|
465
|
-
}
|
|
466
|
-
if (tb !== 'object') {
|
|
467
|
-
if (a instanceof ModelValue)
|
|
468
|
-
return modelEq(a, b);
|
|
469
|
-
return eqFail(a, b);
|
|
470
|
-
}
|
|
471
|
-
if (a instanceof ModelValue)
|
|
472
|
-
return modelEq(a, b);
|
|
473
|
-
if (b instanceof ModelValue)
|
|
474
|
-
return modelEq(b, a);
|
|
475
|
-
if (a instanceof SetBase) {
|
|
476
|
-
if (!(b instanceof SetBase))
|
|
477
|
-
return eqFail(a, b);
|
|
478
|
-
return setEq(a, b);
|
|
479
|
-
}
|
|
480
|
-
if (a instanceof TupleV) {
|
|
481
|
-
if (b instanceof TupleV) {
|
|
482
|
-
if (a.e.length !== b.e.length)
|
|
483
|
-
return false;
|
|
484
|
-
for (let i = 0; i < a.e.length; i++)
|
|
485
|
-
if (!valEq(a.e[i], b.e[i]))
|
|
486
|
-
return false;
|
|
487
|
-
return true;
|
|
488
|
-
}
|
|
489
|
-
if (b instanceof RecordV || b instanceof FcnV)
|
|
490
|
-
return false; // canonical forms differ
|
|
491
|
-
return eqFail(a, b);
|
|
492
|
-
}
|
|
493
|
-
if (a instanceof RecordV) {
|
|
494
|
-
if (b instanceof RecordV) {
|
|
495
|
-
const n = a.names.length;
|
|
496
|
-
if (n !== b.names.length)
|
|
497
|
-
return false;
|
|
498
|
-
for (let i = 0; i < n; i++)
|
|
499
|
-
if (a.names[i] !== b.names[i])
|
|
500
|
-
return false;
|
|
501
|
-
for (let i = 0; i < n; i++)
|
|
502
|
-
if (!valEq(a.vals[i], b.vals[i]))
|
|
503
|
-
return false;
|
|
504
|
-
return true;
|
|
505
|
-
}
|
|
506
|
-
if (b instanceof TupleV || b instanceof FcnV)
|
|
507
|
-
return false;
|
|
508
|
-
return eqFail(a, b);
|
|
509
|
-
}
|
|
510
|
-
if (a instanceof FcnV) {
|
|
511
|
-
if (b instanceof FcnV) {
|
|
512
|
-
const n = a.dom.length;
|
|
513
|
-
if (n !== b.dom.length)
|
|
514
|
-
return false;
|
|
515
|
-
for (let i = 0; i < n; i++)
|
|
516
|
-
if (!valEq(a.dom[i], b.dom[i]))
|
|
517
|
-
return false;
|
|
518
|
-
for (let i = 0; i < n; i++)
|
|
519
|
-
if (!valEq(a.vals[i], b.vals[i]))
|
|
520
|
-
return false;
|
|
521
|
-
return true;
|
|
522
|
-
}
|
|
523
|
-
if (b instanceof TupleV || b instanceof RecordV)
|
|
524
|
-
return false;
|
|
525
|
-
return eqFail(a, b);
|
|
526
|
-
}
|
|
527
|
-
return eqFail(a, b);
|
|
528
|
-
}
|
|
529
|
-
function modelEq(m, o) {
|
|
530
|
-
if (o instanceof ModelValue) {
|
|
531
|
-
if (m === o)
|
|
532
|
-
return true;
|
|
533
|
-
if (m.type !== null && o.type !== null && m.type !== o.type) {
|
|
534
|
-
return err(`Attempted to check equality of the differently-typed model values ${m.name} and ${o.name}`);
|
|
535
|
-
}
|
|
536
|
-
return false;
|
|
537
|
-
}
|
|
538
|
-
if (m.type !== null)
|
|
539
|
-
return err(`Attempted to check equality of typed model value ${m.name} and non-model value\n${fmt(o)}`);
|
|
540
|
-
return false;
|
|
541
|
-
}
|
|
542
|
-
export function setEq(a, b) {
|
|
543
|
-
if (a === b)
|
|
544
|
-
return true;
|
|
545
|
-
if (a instanceof IntervalV && b instanceof IntervalV)
|
|
546
|
-
return a.size() === b.size() && (a.size() === 0 || a.lo === b.lo);
|
|
547
|
-
if (!a.enumerable() || !b.enumerable()) {
|
|
548
|
-
if (a instanceof InfSetV && b instanceof InfSetV)
|
|
549
|
-
return a.kind === b.kind;
|
|
550
|
-
if (a.enumerable() !== b.enumerable())
|
|
551
|
-
return false;
|
|
552
|
-
return err('Attempted to check equality of two non-enumerable sets.');
|
|
553
|
-
}
|
|
554
|
-
const x = a.elems(), y = b.elems();
|
|
555
|
-
if (x.length !== y.length)
|
|
556
|
-
return false;
|
|
557
|
-
for (let i = 0; i < x.length; i++)
|
|
558
|
-
if (!valEq(x[i], y[i]))
|
|
559
|
-
return false;
|
|
560
|
-
return true;
|
|
561
|
-
}
|
|
562
|
-
export function bsearch(arr, v) {
|
|
563
|
-
let lo = 0, hi = arr.length - 1;
|
|
564
|
-
while (lo <= hi) {
|
|
565
|
-
const mid = (lo + hi) >> 1;
|
|
566
|
-
const c = compare(arr[mid], v);
|
|
567
|
-
if (c === 0)
|
|
568
|
-
return mid;
|
|
569
|
-
if (c < 0)
|
|
570
|
-
lo = mid + 1;
|
|
571
|
-
else
|
|
572
|
-
hi = mid - 1;
|
|
573
|
-
}
|
|
574
|
-
return -1;
|
|
575
|
-
}
|
|
576
|
-
export function sortDedup(a) {
|
|
577
|
-
if (a.length < 2)
|
|
578
|
-
return a;
|
|
579
|
-
let sorted = true;
|
|
580
|
-
for (let i = 1; i < a.length; i++) {
|
|
581
|
-
if (compare(a[i - 1], a[i]) >= 0) {
|
|
582
|
-
sorted = false;
|
|
583
|
-
break;
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
if (sorted)
|
|
587
|
-
return a;
|
|
588
|
-
const s = a.slice().sort(compare);
|
|
589
|
-
const out = [s[0]];
|
|
590
|
-
for (let i = 1; i < s.length; i++)
|
|
591
|
-
if (compare(s[i - 1], s[i]) !== 0)
|
|
592
|
-
out.push(s[i]);
|
|
593
|
-
return out;
|
|
594
|
-
}
|
|
595
|
-
export function mkSet(elems) { return new SetV(sortDedup(elems)); }
|
|
596
|
-
// ---- functions ----
|
|
597
|
-
// Build a function from a sorted, deduplicated domain and parallel values, in canonical form.
|
|
598
|
-
export function makeFcn(dom, vals) {
|
|
599
|
-
const n = dom.length;
|
|
600
|
-
if (n === 0)
|
|
601
|
-
return EMPTY_TUPLE;
|
|
602
|
-
const d0 = dom[0];
|
|
603
|
-
if (typeof d0 === 'number') {
|
|
604
|
-
if (d0 === 1) {
|
|
605
|
-
let ok = true;
|
|
606
|
-
for (let i = 1; i < n; i++)
|
|
607
|
-
if (dom[i] !== i + 1) {
|
|
608
|
-
ok = false;
|
|
609
|
-
break;
|
|
610
|
-
}
|
|
611
|
-
if (ok)
|
|
612
|
-
return new TupleV(vals);
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
else if (typeof d0 === 'string') {
|
|
616
|
-
let ok = true;
|
|
617
|
-
for (let i = 1; i < n; i++)
|
|
618
|
-
if (typeof dom[i] !== 'string') {
|
|
619
|
-
ok = false;
|
|
620
|
-
break;
|
|
621
|
-
}
|
|
622
|
-
if (ok)
|
|
623
|
-
return new RecordV(dom, vals);
|
|
624
|
-
}
|
|
625
|
-
return new FcnV(dom, vals);
|
|
626
|
-
}
|
|
627
|
-
// Build from unsorted pairs.
|
|
628
|
-
export function makeFcnFromPairs(pairs) {
|
|
629
|
-
pairs.sort((x, y) => compare(x[0], y[0]));
|
|
630
|
-
const dom = [], vals = [];
|
|
631
|
-
for (let i = 0; i < pairs.length; i++) {
|
|
632
|
-
if (i > 0 && compare(pairs[i - 1][0], pairs[i][0]) === 0)
|
|
633
|
-
continue;
|
|
634
|
-
dom.push(pairs[i][0]);
|
|
635
|
-
vals.push(pairs[i][1]);
|
|
636
|
-
}
|
|
637
|
-
return makeFcn(dom, vals);
|
|
638
|
-
}
|
|
639
|
-
export function makeRecord(names, vals) {
|
|
640
|
-
const idx = names.map((_, i) => i).sort((i, j) => strId(names[i]) - strId(names[j]));
|
|
641
|
-
return new RecordV(idx.map(i => names[i]), idx.map(i => vals[i]));
|
|
642
|
-
}
|
|
643
|
-
export function isFcn(v) { return v instanceof TupleV || v instanceof RecordV || v instanceof FcnV; }
|
|
644
|
-
export function domainElems(f) {
|
|
645
|
-
if (f instanceof TupleV) {
|
|
646
|
-
const a = [];
|
|
647
|
-
for (let i = 1; i <= f.e.length; i++)
|
|
648
|
-
a.push(i);
|
|
649
|
-
return a;
|
|
650
|
-
}
|
|
651
|
-
if (f instanceof RecordV)
|
|
652
|
-
return f.names;
|
|
653
|
-
if (f instanceof FcnV)
|
|
654
|
-
return f.dom;
|
|
655
|
-
return err(`Attempted to compute the domain of the non-function value:\n${fmt(f)}`);
|
|
656
|
-
}
|
|
657
|
-
export function domainOf(f) {
|
|
658
|
-
if (f instanceof TupleV)
|
|
659
|
-
return new IntervalV(1, f.e.length);
|
|
660
|
-
if (f instanceof RecordV)
|
|
661
|
-
return new SetV(f.names);
|
|
662
|
-
if (f instanceof FcnV)
|
|
663
|
-
return new SetV(f.dom);
|
|
664
|
-
return null;
|
|
665
|
-
}
|
|
666
|
-
export function fcnValues(f) {
|
|
667
|
-
if (f instanceof TupleV)
|
|
668
|
-
return f.e;
|
|
669
|
-
if (f instanceof RecordV || f instanceof FcnV)
|
|
670
|
-
return f.vals;
|
|
671
|
-
return err(`Attempted to apply a function operation to the non-function value:\n${fmt(f)}`);
|
|
672
|
-
}
|
|
673
|
-
// ---- hashing: two independent 32-bit murmur-style streams ----
|
|
674
|
-
export class Hasher {
|
|
675
|
-
h1 = 0x9747b28c | 0;
|
|
676
|
-
h2 = 0x3c6ef372 | 0;
|
|
677
|
-
reset() { this.h1 = 0x9747b28c | 0; this.h2 = 0x3c6ef372 | 0; }
|
|
678
|
-
add(x) {
|
|
679
|
-
let k = Math.imul(x, 0xcc9e2d51 | 0);
|
|
680
|
-
k = (k << 15) | (k >>> 17);
|
|
681
|
-
k = Math.imul(k, 0x1b873593);
|
|
682
|
-
let h = this.h1 ^ k;
|
|
683
|
-
h = (h << 13) | (h >>> 19);
|
|
684
|
-
this.h1 = (Math.imul(h, 5) + (0xe6546b64 | 0)) | 0;
|
|
685
|
-
k = Math.imul(x ^ 0x5bd1e995, 0x85ebca6b | 0);
|
|
686
|
-
k = (k << 13) | (k >>> 19);
|
|
687
|
-
k = Math.imul(k, 0xc2b2ae35 | 0);
|
|
688
|
-
h = this.h2 ^ k;
|
|
689
|
-
h = (h << 17) | (h >>> 15);
|
|
690
|
-
this.h2 = (Math.imul(h, 5) + 0x52dce729) | 0;
|
|
691
|
-
}
|
|
692
|
-
value(v) {
|
|
693
|
-
switch (typeof v) {
|
|
694
|
-
case 'number':
|
|
695
|
-
this.add(1);
|
|
696
|
-
this.add(v | 0);
|
|
697
|
-
if (v > 0x7fffffff || v < -0x80000000)
|
|
698
|
-
this.add((v / 4294967296) | 0);
|
|
699
|
-
return;
|
|
700
|
-
case 'boolean':
|
|
701
|
-
this.add(v ? 3 : 2);
|
|
702
|
-
return;
|
|
703
|
-
case 'string':
|
|
704
|
-
this.add(4);
|
|
705
|
-
this.add(strId(v));
|
|
706
|
-
return;
|
|
707
|
-
}
|
|
708
|
-
if (v instanceof TupleV) {
|
|
709
|
-
this.add(5);
|
|
710
|
-
this.add(v.e.length);
|
|
711
|
-
for (let i = 0; i < v.e.length; i++)
|
|
712
|
-
this.value(v.e[i]);
|
|
713
|
-
}
|
|
714
|
-
else if (v instanceof RecordV) {
|
|
715
|
-
this.add(6);
|
|
716
|
-
this.add(v.names.length);
|
|
717
|
-
for (let i = 0; i < v.names.length; i++) {
|
|
718
|
-
this.add(strId(v.names[i]));
|
|
719
|
-
this.value(v.vals[i]);
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
else if (v instanceof SetBase) {
|
|
723
|
-
const e = v.elems();
|
|
724
|
-
this.add(7);
|
|
725
|
-
this.add(e.length);
|
|
726
|
-
for (let i = 0; i < e.length; i++)
|
|
727
|
-
this.value(e[i]);
|
|
728
|
-
}
|
|
729
|
-
else if (v instanceof FcnV) {
|
|
730
|
-
this.add(8);
|
|
731
|
-
this.add(v.dom.length);
|
|
732
|
-
for (let i = 0; i < v.dom.length; i++) {
|
|
733
|
-
this.value(v.dom[i]);
|
|
734
|
-
this.value(v.vals[i]);
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
else if (v instanceof ModelValue) {
|
|
738
|
-
this.add(9);
|
|
739
|
-
this.add(v.id);
|
|
740
|
-
}
|
|
741
|
-
else
|
|
742
|
-
err('Attempted to fingerprint a non-value (operator or lazy function).');
|
|
743
|
-
}
|
|
744
|
-
finish() {
|
|
745
|
-
let h = this.h1;
|
|
746
|
-
h ^= h >>> 16;
|
|
747
|
-
h = Math.imul(h, 0x85ebca6b | 0);
|
|
748
|
-
h ^= h >>> 13;
|
|
749
|
-
h = Math.imul(h, 0xc2b2ae35 | 0);
|
|
750
|
-
h ^= h >>> 16;
|
|
751
|
-
this.h1 = h;
|
|
752
|
-
h = this.h2 ^ this.h1;
|
|
753
|
-
h ^= h >>> 15;
|
|
754
|
-
h = Math.imul(h, 0x2c1b3c6d);
|
|
755
|
-
h ^= h >>> 12;
|
|
756
|
-
h = Math.imul(h, 0x297a2d39);
|
|
757
|
-
h ^= h >>> 15;
|
|
758
|
-
this.h2 = h;
|
|
759
|
-
}
|
|
760
|
-
}
|
|
761
|
-
// ---- printing (TLC format) ----
|
|
762
|
-
function fmtStr(s) {
|
|
763
|
-
let o = '"';
|
|
764
|
-
for (const ch of s) {
|
|
765
|
-
if (ch === '\\')
|
|
766
|
-
o += '\\\\';
|
|
767
|
-
else if (ch === '"')
|
|
768
|
-
o += '\\"';
|
|
769
|
-
else if (ch === '\n')
|
|
770
|
-
o += '\\n';
|
|
771
|
-
else if (ch === '\t')
|
|
772
|
-
o += '\\t';
|
|
773
|
-
else if (ch === '\r')
|
|
774
|
-
o += '\\r';
|
|
775
|
-
else if (ch === '\f')
|
|
776
|
-
o += '\\f';
|
|
777
|
-
else
|
|
778
|
-
o += ch;
|
|
779
|
-
}
|
|
780
|
-
return o + '"';
|
|
781
|
-
}
|
|
782
|
-
const isIdent = (s) => /^[A-Za-z0-9_]*[A-Za-z][A-Za-z0-9_]*$/.test(s);
|
|
783
|
-
export function fmt(v) {
|
|
784
|
-
switch (typeof v) {
|
|
785
|
-
case 'number': return String(v);
|
|
786
|
-
case 'boolean': return v ? 'TRUE' : 'FALSE';
|
|
787
|
-
case 'string': return fmtStr(v);
|
|
788
|
-
case 'undefined': return '<undefined>';
|
|
789
|
-
}
|
|
790
|
-
if (v instanceof ModelValue)
|
|
791
|
-
return v.name;
|
|
792
|
-
if (v instanceof TupleV)
|
|
793
|
-
return '<<' + v.e.map(fmt).join(', ') + '>>';
|
|
794
|
-
if (v instanceof RecordV)
|
|
795
|
-
return '[' + v.names.map((n, i) => (isIdent(n) ? n : fmtStr(n)) + ' |-> ' + fmt(v.vals[i])).join(', ') + ']';
|
|
796
|
-
if (v instanceof FcnV)
|
|
797
|
-
return '(' + v.dom.map((d, i) => fmt(d) + ' :> ' + fmt(v.vals[i])).join(' @@ ') + ')';
|
|
798
|
-
if (v instanceof IntervalV)
|
|
799
|
-
return v.lo + '..' + v.hi;
|
|
800
|
-
if (v instanceof SetV)
|
|
801
|
-
return '{' + v.e.map(fmt).join(', ') + '}';
|
|
802
|
-
if (v instanceof SubsetV)
|
|
803
|
-
return 'SUBSET ' + fmt(v.base);
|
|
804
|
-
if (v instanceof FcnSetV)
|
|
805
|
-
return '[' + fmt(v.dom) + ' -> ' + fmt(v.rng) + ']';
|
|
806
|
-
if (v instanceof RecSetV)
|
|
807
|
-
return '[' + v.names.map((n, i) => n + ': ' + fmt(v.sets[i])).join(', ') + ']';
|
|
808
|
-
if (v instanceof CrossV)
|
|
809
|
-
return '(' + v.sets.map(fmt).join(' \\X ') + ')';
|
|
810
|
-
if (v instanceof SeqSetV)
|
|
811
|
-
return 'Seq(' + fmt(v.base) + ')';
|
|
812
|
-
if (v instanceof InfSetV)
|
|
813
|
-
return v.kind;
|
|
814
|
-
if (v instanceof LazySetV)
|
|
815
|
-
return v.desc;
|
|
816
|
-
if (v instanceof DerivedSetV)
|
|
817
|
-
return v.enumerable() ? '{' + v.elems().map(fmt).join(', ') + '}' : v.desc;
|
|
818
|
-
if (v && typeof v.describe === 'function')
|
|
819
|
-
return v.describe();
|
|
820
|
-
return '<operator>';
|
|
821
|
-
}
|
|
822
|
-
export function isSet(v) { return v instanceof SetBase; }
|