everystate 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +111 -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 +23 -24
- 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/stdlib.js
DELETED
|
@@ -1,588 +0,0 @@
|
|
|
1
|
-
import { Closure, DefRef, Lazy } from "./eval.js";
|
|
2
|
-
import { EvalError, SetBase, SetV, IntervalV, InfSetV, SeqSetV, LazySetV, CrossV, RecSetV, FcnSetV, TupleV, RecordV, FcnV, ModelValue, modelValue, valEq, compare, mkSet, BOOLEAN_SET, makeFcn, makeFcnFromPairs, domainElems, fcnValues, isFcn, fmt, EMPTY_TUPLE, bsearch, } from "./values.js";
|
|
3
|
-
export const fail = (msg) => { throw new EvalError(msg); };
|
|
4
|
-
const ORD = ['first', 'second', 'third', 'fourth'];
|
|
5
|
-
export function int(v, op, i) {
|
|
6
|
-
if (typeof v !== 'number')
|
|
7
|
-
fail(`The ${ORD[i]} argument of ${op} should be an integer, but instead it is:\n${fmt(v)}`);
|
|
8
|
-
return v;
|
|
9
|
-
}
|
|
10
|
-
function nat(v, op, i) {
|
|
11
|
-
if (typeof v !== 'number' || v < 0)
|
|
12
|
-
fail(`The ${ORD[i]} argument of ${op} should be a natural number, but instead it is:\n${fmt(v)}`);
|
|
13
|
-
return v;
|
|
14
|
-
}
|
|
15
|
-
export function seq(v, op, i) {
|
|
16
|
-
if (v instanceof TupleV)
|
|
17
|
-
return v.e;
|
|
18
|
-
return fail(`The ${ORD[i]} argument of ${op} should be a sequence, but instead it is:\n${fmt(v)}`);
|
|
19
|
-
}
|
|
20
|
-
export function set(v, op, i) {
|
|
21
|
-
if (v instanceof SetBase)
|
|
22
|
-
return v;
|
|
23
|
-
return fail(`The ${ORD[i]} argument of ${op} should be a set, but instead it is:\n${fmt(v)}`);
|
|
24
|
-
}
|
|
25
|
-
function checkInt(n, what) {
|
|
26
|
-
if (n > 2147483647 || n < -2147483648)
|
|
27
|
-
fail(`Overflow when computing ${what}`);
|
|
28
|
-
return n;
|
|
29
|
-
}
|
|
30
|
-
// A product / function set / record set that an empty component makes empty although another one is infinite.
|
|
31
|
-
function emptyProduct(x) {
|
|
32
|
-
const empty = (y) => y instanceof IntervalV ? y.hi < y.lo : y instanceof SetV ? y.e.length === 0 : emptyProduct(y);
|
|
33
|
-
if (x instanceof CrossV || x instanceof RecSetV)
|
|
34
|
-
return x.sets.some(empty);
|
|
35
|
-
if (x instanceof FcnSetV)
|
|
36
|
-
return empty(x.rng) && !empty(x.dom);
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
function module(name, exts, defs) {
|
|
40
|
-
const info = { name, syms: new Map(), exports: new Map(), consts: [], vars: [], assumes: [], ownDefs: [], order: [] };
|
|
41
|
-
for (const e of exts)
|
|
42
|
-
for (const [k, v] of e.exports) {
|
|
43
|
-
info.syms.set(k, v);
|
|
44
|
-
info.exports.set(k, v);
|
|
45
|
-
}
|
|
46
|
-
for (const [n, spec] of Object.entries(defs)) {
|
|
47
|
-
let def;
|
|
48
|
-
if (Array.isArray(spec) && typeof spec[1] === 'function') {
|
|
49
|
-
const [arity, fn, opts] = spec;
|
|
50
|
-
const params = [];
|
|
51
|
-
for (let i = 0; i < arity; i++)
|
|
52
|
-
params.push({ name: `p${i}`, arity: opts?.ops?.includes(i) ? 1 : 0 });
|
|
53
|
-
def = { k: 'def', name: n, params, body: null, module: name, local: false, loc: [0, 0, 0, 0], builtin: { fn, ...opts } };
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
const value = spec;
|
|
57
|
-
def = { k: 'def', name: n, params: [], body: null, module: name, local: false, loc: [0, 0, 0, 0], builtin: { fn: () => value } };
|
|
58
|
-
}
|
|
59
|
-
info.syms.set(n, def);
|
|
60
|
-
info.exports.set(n, def);
|
|
61
|
-
info.ownDefs.push(def);
|
|
62
|
-
}
|
|
63
|
-
return info;
|
|
64
|
-
}
|
|
65
|
-
export function toStr(v) { return typeof v === 'string' ? v : fmt(v); }
|
|
66
|
-
export function globalDefs() {
|
|
67
|
-
const g = module('<builtin>', [], { BOOLEAN: BOOLEAN_SET, STRING: new InfSetV('STRING') });
|
|
68
|
-
return g.syms;
|
|
69
|
-
}
|
|
70
|
-
export function buildStdlib(io) {
|
|
71
|
-
const Naturals = module('Naturals', [], {
|
|
72
|
-
'+': [2, (_, a) => checkInt(int(a[0], '+', 0) + int(a[1], '+', 1), `${a[0]}+${a[1]}`)],
|
|
73
|
-
'-': [2, (_, a) => checkInt(int(a[0], '-', 0) - int(a[1], '-', 1), `${a[0]}-${a[1]}`)],
|
|
74
|
-
'*': [2, (_, a) => checkInt(int(a[0], '*', 0) * int(a[1], '*', 1), `${a[0]}*${a[1]}`)],
|
|
75
|
-
'^': [2, (_, a) => {
|
|
76
|
-
const b = int(a[0], '^', 0), e = int(a[1], '^', 1);
|
|
77
|
-
if (e < 0)
|
|
78
|
-
fail(`The second argument of ^ should be a natural number, but instead it is:\n${e}`);
|
|
79
|
-
return checkInt(b ** e, `${b}^${e}`);
|
|
80
|
-
}],
|
|
81
|
-
'<': [2, (_, a) => int(a[0], '<', 0) < int(a[1], '<', 1)],
|
|
82
|
-
'>': [2, (_, a) => int(a[0], '>', 0) > int(a[1], '>', 1)],
|
|
83
|
-
'<=': [2, (_, a) => int(a[0], '<=', 0) <= int(a[1], '<=', 1)],
|
|
84
|
-
'>=': [2, (_, a) => int(a[0], '>=', 0) >= int(a[1], '>=', 1)],
|
|
85
|
-
'..': [2, (_, a) => new IntervalV(int(a[0], '..', 0), int(a[1], '..', 1))],
|
|
86
|
-
'\\div': [2, (_, a) => {
|
|
87
|
-
const x = int(a[0], '\\div', 0), y = int(a[1], '\\div', 1);
|
|
88
|
-
if (y === 0)
|
|
89
|
-
fail('The second argument of \\div is 0.');
|
|
90
|
-
return Math.floor(x / y);
|
|
91
|
-
}],
|
|
92
|
-
'%': [2, (_, a) => {
|
|
93
|
-
const x = int(a[0], '%', 0), y = int(a[1], '%', 1);
|
|
94
|
-
if (y <= 0)
|
|
95
|
-
fail(`The second argument of % should be a positive number, but instead it is:\n${y}`);
|
|
96
|
-
const r = x % y;
|
|
97
|
-
return r < 0 ? r + y : r;
|
|
98
|
-
}],
|
|
99
|
-
'Nat': new InfSetV('Nat'),
|
|
100
|
-
});
|
|
101
|
-
const Integers = module('Integers', [Naturals], {
|
|
102
|
-
'Int': new InfSetV('Int'),
|
|
103
|
-
'-.': [1, (_, a) => checkInt(-int(a[0], '-.', 0), `-${a[0]}`)],
|
|
104
|
-
});
|
|
105
|
-
const Reals = module('Reals', [Integers], {
|
|
106
|
-
'Real': new InfSetV('Real'),
|
|
107
|
-
'/': [2, (_, a) => {
|
|
108
|
-
const x = int(a[0], '/', 0), y = int(a[1], '/', 1);
|
|
109
|
-
if (y === 0)
|
|
110
|
-
fail('The second argument of / is 0.');
|
|
111
|
-
if (x % y !== 0)
|
|
112
|
-
fail('TLC cannot handle real numbers that are not integers.');
|
|
113
|
-
return x / y;
|
|
114
|
-
}],
|
|
115
|
-
'Infinity': [0, () => fail('TLC cannot evaluate Infinity.')],
|
|
116
|
-
});
|
|
117
|
-
const Sequences = module('Sequences', [], {
|
|
118
|
-
'Seq': [1, (_, a) => new SeqSetV(set(a[0], 'Seq', 0))],
|
|
119
|
-
'Len': [1, (_, a) => typeof a[0] === 'string' ? a[0].length : seq(a[0], 'Len', 0).length],
|
|
120
|
-
'\\o': [2, (_, a) => {
|
|
121
|
-
if (typeof a[0] === 'string' && typeof a[1] === 'string')
|
|
122
|
-
return a[0] + a[1];
|
|
123
|
-
const x = seq(a[0], '\\o', 0), y = seq(a[1], '\\o', 1);
|
|
124
|
-
return x.length === 0 ? a[1] : y.length === 0 ? a[0] : new TupleV(x.concat(y));
|
|
125
|
-
}],
|
|
126
|
-
'Append': [2, (_, a) => { const s = seq(a[0], 'Append', 0).slice(); s.push(a[1]); return new TupleV(s); }],
|
|
127
|
-
'Head': [1, (_, a) => {
|
|
128
|
-
const s = seq(a[0], 'Head', 0);
|
|
129
|
-
if (s.length === 0)
|
|
130
|
-
fail('Attempted to apply Head to the empty sequence.');
|
|
131
|
-
return s[0];
|
|
132
|
-
}],
|
|
133
|
-
'Tail': [1, (_, a) => {
|
|
134
|
-
const s = seq(a[0], 'Tail', 0);
|
|
135
|
-
if (s.length === 0)
|
|
136
|
-
fail('Attempted to apply Tail to the empty sequence.');
|
|
137
|
-
return new TupleV(s.slice(1));
|
|
138
|
-
}],
|
|
139
|
-
'SubSeq': [3, (_, a) => {
|
|
140
|
-
if (typeof a[0] === 'string')
|
|
141
|
-
return a[0].slice(int(a[1], 'SubSeq', 1) - 1, int(a[2], 'SubSeq', 2));
|
|
142
|
-
const s = seq(a[0], 'SubSeq', 0), m = int(a[1], 'SubSeq', 1), n = int(a[2], 'SubSeq', 2);
|
|
143
|
-
if (m > n)
|
|
144
|
-
return EMPTY_TUPLE;
|
|
145
|
-
if (m < 1 || n > s.length)
|
|
146
|
-
fail(`The ${m < 1 ? 'second' : 'third'} argument of SubSeq must be in the domain of its first argument:\n${fmt(a[0])}\n, but instead it is\n${m < 1 ? m : n}`);
|
|
147
|
-
return new TupleV(s.slice(m - 1, n));
|
|
148
|
-
}],
|
|
149
|
-
'SelectSeq': [2, (ev, a) => {
|
|
150
|
-
const s = seq(a[0], 'SelectSeq', 0);
|
|
151
|
-
return new TupleV(s.filter(x => {
|
|
152
|
-
const r = ev.applyOp(a[1], [x]);
|
|
153
|
-
if (typeof r !== 'boolean')
|
|
154
|
-
fail(`The second argument of SelectSeq must return a boolean, but it returned:\n${fmt(r)}`);
|
|
155
|
-
return r;
|
|
156
|
-
}));
|
|
157
|
-
}, { ops: [1] }],
|
|
158
|
-
});
|
|
159
|
-
const FiniteSets = module('FiniteSets', [], {
|
|
160
|
-
'IsFiniteSet': [1, (_, a) => set(a[0], 'IsFiniteSet', 0).enumerable()],
|
|
161
|
-
'Cardinality': [1, (_, a) => {
|
|
162
|
-
const s = set(a[0], 'Cardinality', 0);
|
|
163
|
-
if (!s.enumerable()) {
|
|
164
|
-
if (emptyProduct(s))
|
|
165
|
-
return 0;
|
|
166
|
-
fail(`Attempted to compute the cardinality of the infinite set ${fmt(s)}`);
|
|
167
|
-
}
|
|
168
|
-
const n = s.size();
|
|
169
|
-
if (n > 2147483647)
|
|
170
|
-
fail(`Attempted to apply the operator overridden by the Java method\npublic static tlc2.value.impl.IntValue tlc2.module.FiniteSets.Cardinality(tlc2.value.impl.Value),\nbut it produced the following error:\nOverflow when computing the number of elements in:\n${fmt(s)}`);
|
|
171
|
-
return n;
|
|
172
|
-
}],
|
|
173
|
-
});
|
|
174
|
-
const registers = new Map();
|
|
175
|
-
const tlcCache = new Map();
|
|
176
|
-
const TLC = module('TLC', [], {
|
|
177
|
-
'Print': [2, (_, a) => { io.print(`${fmt(a[0])} ${fmt(a[1])}`); return a[1]; }],
|
|
178
|
-
'PrintT': [1, (_, a) => { io.print(fmt(a[0])); return true; }],
|
|
179
|
-
'Assert': [2, (_, a) => {
|
|
180
|
-
if (typeof a[0] !== 'boolean')
|
|
181
|
-
fail(`The first argument of Assert should be a boolean, but instead it is:\n${fmt(a[0])}`);
|
|
182
|
-
if (!a[0]) {
|
|
183
|
-
const e = new EvalError(`The first argument of Assert evaluated to FALSE; the second argument was:\n${fmt(a[1])}`);
|
|
184
|
-
e.kind = 'assert';
|
|
185
|
-
throw e;
|
|
186
|
-
}
|
|
187
|
-
return true;
|
|
188
|
-
}],
|
|
189
|
-
'JavaTime': [0, () => Math.floor(Date.now() / 1000) & 0x7fffffff],
|
|
190
|
-
'TLCGet': [1, (ev, a) => {
|
|
191
|
-
ev.stTouch++; // never a constant: definitions using it must not be cached
|
|
192
|
-
if (typeof a[0] === 'string') {
|
|
193
|
-
const key = a[0];
|
|
194
|
-
if (key.startsWith('s:')) {
|
|
195
|
-
const v = registers.get(key);
|
|
196
|
-
if (v === undefined)
|
|
197
|
-
fail(`TLCGet(${key}) was undefined.`);
|
|
198
|
-
return v;
|
|
199
|
-
}
|
|
200
|
-
if (key === 'all' || key === 'all:named') {
|
|
201
|
-
const pairs = [];
|
|
202
|
-
for (const [k, v] of registers)
|
|
203
|
-
if ((typeof k === 'string') === (key === 'all:named'))
|
|
204
|
-
pairs.push([k, new TupleV([v])]);
|
|
205
|
-
return makeFcnFromPairs(pairs);
|
|
206
|
-
}
|
|
207
|
-
const h = ev.hooks.tlcGet?.(a[0]);
|
|
208
|
-
if (h === undefined)
|
|
209
|
-
fail(`TLCGet(${fmt(a[0])}) is not supported.`);
|
|
210
|
-
return h;
|
|
211
|
-
}
|
|
212
|
-
const v = registers.get(int(a[0], 'TLCGet', 0));
|
|
213
|
-
if (v === undefined)
|
|
214
|
-
fail(`TLCGet(${a[0]}) was undefined.`);
|
|
215
|
-
return v;
|
|
216
|
-
}],
|
|
217
|
-
'TLCSet': [2, (ev, a) => {
|
|
218
|
-
if (typeof a[0] === 'string') {
|
|
219
|
-
if (a[0].startsWith('s:'))
|
|
220
|
-
registers.set(a[0], a[1]);
|
|
221
|
-
else
|
|
222
|
-
ev.hooks.tlcSet?.(a[0], a[1]);
|
|
223
|
-
return true;
|
|
224
|
-
}
|
|
225
|
-
registers.set(int(a[0], 'TLCSet', 0), a[1]);
|
|
226
|
-
return true;
|
|
227
|
-
}],
|
|
228
|
-
':>': [2, (_, a) => makeFcn([a[0]], [a[1]])],
|
|
229
|
-
'@@': [2, (_, a) => {
|
|
230
|
-
if (!isFcn(a[0]))
|
|
231
|
-
fail(`The first argument of @@ should be a function, but instead it is:\n${fmt(a[0])}`);
|
|
232
|
-
if (!isFcn(a[1]))
|
|
233
|
-
fail(`The second argument of @@ should be a function, but instead it is:\n${fmt(a[1])}`);
|
|
234
|
-
const d0 = domainElems(a[0]), v0 = fcnValues(a[0]), d1 = domainElems(a[1]), v1 = fcnValues(a[1]);
|
|
235
|
-
if (d1.length === 0)
|
|
236
|
-
return a[0];
|
|
237
|
-
if (d0.length === 0)
|
|
238
|
-
return a[1];
|
|
239
|
-
const pairs = d0.map((d, i) => [d, v0[i]]);
|
|
240
|
-
// first function wins: list its pairs first; makeFcnFromPairs keeps the first of equal keys (stable sort)
|
|
241
|
-
for (let i = 0; i < d1.length; i++)
|
|
242
|
-
pairs.push([d1[i], v1[i]]);
|
|
243
|
-
return makeFcnFromPairs(pairs);
|
|
244
|
-
}],
|
|
245
|
-
'Permutations': [1, (_, a) => {
|
|
246
|
-
const s = set(a[0], 'Permutations', 0).elems();
|
|
247
|
-
const out = [];
|
|
248
|
-
const used = new Array(s.length).fill(false);
|
|
249
|
-
const cur = [];
|
|
250
|
-
const rec = () => {
|
|
251
|
-
if (cur.length === s.length) {
|
|
252
|
-
out.push(makeFcn(s, cur.slice()));
|
|
253
|
-
return;
|
|
254
|
-
}
|
|
255
|
-
for (let i = 0; i < s.length; i++) {
|
|
256
|
-
if (used[i])
|
|
257
|
-
continue;
|
|
258
|
-
used[i] = true;
|
|
259
|
-
cur.push(s[i]);
|
|
260
|
-
rec();
|
|
261
|
-
cur.pop();
|
|
262
|
-
used[i] = false;
|
|
263
|
-
}
|
|
264
|
-
};
|
|
265
|
-
rec();
|
|
266
|
-
return mkSet(out);
|
|
267
|
-
}],
|
|
268
|
-
'SortSeq': [2, (ev, a) => {
|
|
269
|
-
const s = seq(a[0], 'SortSeq', 0).slice();
|
|
270
|
-
// stable insertion via Array.sort with the user's strict comparison
|
|
271
|
-
s.sort((x, y) => (ev.applyOp(a[1], [x, y]) === true ? -1 : ev.applyOp(a[1], [y, x]) === true ? 1 : 0));
|
|
272
|
-
return new TupleV(s);
|
|
273
|
-
}, { ops: [1] }],
|
|
274
|
-
'RandomElement': [1, (ev, a) => {
|
|
275
|
-
const s = set(a[0], 'RandomElement', 0).elems();
|
|
276
|
-
if (s.length === 0)
|
|
277
|
-
fail('Attempted to apply RandomElement to the empty set.');
|
|
278
|
-
return s[Math.floor(ev.hooks.random() * s.length)];
|
|
279
|
-
}],
|
|
280
|
-
'Any': new LazySetV('Any', () => true),
|
|
281
|
-
'ToString': [1, (_, a) => fmt(a[0])],
|
|
282
|
-
'TLCEval': [1, (_, a) => a[0]],
|
|
283
|
-
});
|
|
284
|
-
const TLCExt = module('TLCExt', [], {
|
|
285
|
-
'AssertEq': [2, (_, a) => {
|
|
286
|
-
const eq = valEq(a[0], a[1]);
|
|
287
|
-
if (!eq)
|
|
288
|
-
io.print(`\nAssertion failed:\n${fmt(a[0])}\n!=\n${fmt(a[1])}\n`);
|
|
289
|
-
return eq;
|
|
290
|
-
}],
|
|
291
|
-
'AssertError': [2, (ev, a) => {
|
|
292
|
-
const expected = ev.unthunk(a[0]);
|
|
293
|
-
try {
|
|
294
|
-
ev.unthunk(a[1]);
|
|
295
|
-
}
|
|
296
|
-
catch (e) {
|
|
297
|
-
if (e instanceof EvalError) {
|
|
298
|
-
if (e.message === expected)
|
|
299
|
-
return true;
|
|
300
|
-
io.print(`\nAssertion failed:\n${fmt(expected)}\n!=\n${fmt(e.message)}\n`);
|
|
301
|
-
return false;
|
|
302
|
-
}
|
|
303
|
-
throw e;
|
|
304
|
-
}
|
|
305
|
-
return false;
|
|
306
|
-
}, { lazy: true }],
|
|
307
|
-
'TLCModelValue': [1, (_, a) => modelValue(String(a[0]))],
|
|
308
|
-
'TLCDefer': [1, () => true],
|
|
309
|
-
'PickSuccessor': [1, () => true],
|
|
310
|
-
'TLCNoOp': [1, (_, a) => a[0]],
|
|
311
|
-
// TLCCache(expr, closure): expr is evaluated once per (occurrence, closure value)
|
|
312
|
-
'TLCCache': [2, (ev, a) => {
|
|
313
|
-
const node = a[0] instanceof Lazy ? a[0].e : null;
|
|
314
|
-
if (!node)
|
|
315
|
-
return ev.unthunk(a[0]);
|
|
316
|
-
const key = fmt(ev.unthunk(a[1]));
|
|
317
|
-
let m = tlcCache.get(node);
|
|
318
|
-
if (!m) {
|
|
319
|
-
m = new Map();
|
|
320
|
-
tlcCache.set(node, m);
|
|
321
|
-
}
|
|
322
|
-
let v = m.get(key);
|
|
323
|
-
if (v === undefined) {
|
|
324
|
-
v = ev.unthunk(a[0]);
|
|
325
|
-
m.set(key, v);
|
|
326
|
-
}
|
|
327
|
-
return v;
|
|
328
|
-
}, { lazy: true }],
|
|
329
|
-
'TLCGetOrDefault': [2, (_, a) => registers.get(int(a[0], 'TLCGetOrDefault', 0)) ?? a[1]],
|
|
330
|
-
'TLCGetAndSet': [4, (ev, a) => {
|
|
331
|
-
const old = registers.get(int(a[0], 'TLCGetAndSet', 0)) ?? a[3];
|
|
332
|
-
registers.set(a[0], ev.applyOp(a[1], [old, a[2]]));
|
|
333
|
-
return old;
|
|
334
|
-
}, { ops: [1] }],
|
|
335
|
-
'TLCFP': [1, (ev, a) => ev.hooks.fp?.(a[0]) ?? 0],
|
|
336
|
-
'Trace': [0, ev => (ev.stTouch++, ev.hooks.trace?.()) ?? fail('Trace is not available in this context.')],
|
|
337
|
-
'CounterExample': [0, ev => (ev.stTouch++, ev.hooks.counterExample?.()) ?? fail('CounterExample is not available in this context.')],
|
|
338
|
-
'ToTrace': [1, () => fail('ToTrace is not supported.')],
|
|
339
|
-
});
|
|
340
|
-
// ---- Bags ----
|
|
341
|
-
const bagPairs = (b, op) => {
|
|
342
|
-
if (!isFcn(b))
|
|
343
|
-
fail(`Attempted to apply ${op} to the non-bag value:\n${fmt(b)}`);
|
|
344
|
-
return [domainElems(b), fcnValues(b)];
|
|
345
|
-
};
|
|
346
|
-
const mkBag = (m) => makeFcnFromPairs(m.filter(p => p[1] > 0));
|
|
347
|
-
const Bags = module('Bags', [], {
|
|
348
|
-
'IsABag': [1, (_, a) => isFcn(a[0]) && fcnValues(a[0]).every(x => typeof x === 'number' && x > 0)],
|
|
349
|
-
'BagToSet': [1, (_, a) => new SetV(domainElems(a[0]).slice())],
|
|
350
|
-
'SetToBag': [1, (_, a) => { const e = set(a[0], 'SetToBag', 0).elems(); return makeFcn(e, e.map(() => 1)); }],
|
|
351
|
-
'BagIn': [2, (_, a) => bsearch(bagPairs(a[1], 'BagIn')[0], a[0]) >= 0],
|
|
352
|
-
'EmptyBag': EMPTY_TUPLE,
|
|
353
|
-
'\\oplus': [2, (_, a) => {
|
|
354
|
-
const [d0, v0] = bagPairs(a[0], '(+)'), [d1, v1] = bagPairs(a[1], '(+)');
|
|
355
|
-
const out = d0.map((d, i) => [d, v0[i]]);
|
|
356
|
-
for (let i = 0; i < d1.length; i++) {
|
|
357
|
-
const k = bsearch(d0, d1[i]);
|
|
358
|
-
if (k >= 0)
|
|
359
|
-
out[k][1] = checkInt(out[k][1] + v1[i], `${out[k][1]}+${v1[i]}`);
|
|
360
|
-
else
|
|
361
|
-
out.push([d1[i], v1[i]]);
|
|
362
|
-
}
|
|
363
|
-
return mkBag(out);
|
|
364
|
-
}],
|
|
365
|
-
'\\ominus': [2, (_, a) => {
|
|
366
|
-
const [d0, v0] = bagPairs(a[0], '(-)'), [d1, v1] = bagPairs(a[1], '(-)');
|
|
367
|
-
return mkBag(d0.map((d, i) => { const k = bsearch(d1, d); return [d, k >= 0 ? v0[i] - v1[k] : v0[i]]; }));
|
|
368
|
-
}],
|
|
369
|
-
'BagUnion': [1, (_, a) => {
|
|
370
|
-
const out = [];
|
|
371
|
-
for (const b of set(a[0], 'BagUnion', 0).elems()) {
|
|
372
|
-
const [d, v] = bagPairs(b, 'BagUnion');
|
|
373
|
-
for (let i = 0; i < d.length; i++) {
|
|
374
|
-
const hit = out.find(p => valEq(p[0], d[i]));
|
|
375
|
-
if (hit)
|
|
376
|
-
hit[1] = checkInt(hit[1] + v[i], `${hit[1]}+${v[i]}`);
|
|
377
|
-
else
|
|
378
|
-
out.push([d[i], v[i]]);
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
return mkBag(out);
|
|
382
|
-
}],
|
|
383
|
-
'\\sqsubseteq': [2, (_, a) => {
|
|
384
|
-
const [d0, v0] = bagPairs(a[0], '\\sqsubseteq'), [d1, v1] = bagPairs(a[1], '\\sqsubseteq');
|
|
385
|
-
return d0.every((d, i) => { const k = bsearch(d1, d); return k >= 0 && v0[i] <= v1[k]; });
|
|
386
|
-
}],
|
|
387
|
-
'SubBag': [1, (_, a) => {
|
|
388
|
-
const [d, v] = bagPairs(a[0], 'SubBag');
|
|
389
|
-
const out = [];
|
|
390
|
-
const cur = [];
|
|
391
|
-
const rec = (i) => {
|
|
392
|
-
if (i === d.length) {
|
|
393
|
-
out.push(mkBag(cur.slice()));
|
|
394
|
-
return;
|
|
395
|
-
}
|
|
396
|
-
for (let c = 0; c <= v[i]; c++) {
|
|
397
|
-
cur.push([d[i], c]);
|
|
398
|
-
rec(i + 1);
|
|
399
|
-
cur.pop();
|
|
400
|
-
}
|
|
401
|
-
};
|
|
402
|
-
rec(0);
|
|
403
|
-
return mkSet(out);
|
|
404
|
-
}],
|
|
405
|
-
'BagOfAll': [2, (ev, a) => {
|
|
406
|
-
const [d, v] = bagPairs(a[1], 'BagOfAll');
|
|
407
|
-
const out = [];
|
|
408
|
-
for (let i = 0; i < d.length; i++) {
|
|
409
|
-
const x = ev.applyOp(a[0], [d[i]]);
|
|
410
|
-
const hit = out.find(p => valEq(p[0], x));
|
|
411
|
-
if (hit)
|
|
412
|
-
hit[1] += v[i];
|
|
413
|
-
else
|
|
414
|
-
out.push([x, v[i]]);
|
|
415
|
-
}
|
|
416
|
-
return mkBag(out);
|
|
417
|
-
}, { ops: [0] }],
|
|
418
|
-
'BagCardinality': [1, (_, a) => bagPairs(a[0], 'BagCardinality')[1].reduce((x, y) => checkInt(x + y, `${x}+${y}`), 0)],
|
|
419
|
-
'CopiesIn': [2, (_, a) => { const [d, v] = bagPairs(a[1], 'CopiesIn'); const k = bsearch(d, a[0]); return k >= 0 ? v[k] : 0; }],
|
|
420
|
-
});
|
|
421
|
-
// ---- _Possible (support module of the _POSSIBLE config keyword) ----
|
|
422
|
-
const POSSIBLE_KEY = 's:_possible';
|
|
423
|
-
const possibleCounts = () => registers.get(POSSIBLE_KEY) ?? EMPTY_TUPLE;
|
|
424
|
-
const Possible = module('_Possible', [], {
|
|
425
|
-
'_Track': [2, (_, a) => {
|
|
426
|
-
if (typeof a[0] !== 'boolean')
|
|
427
|
-
fail(`Attempted to evaluate an expression of form IF P THEN ... ELSE ..., but P was the non-boolean value:\n${fmt(a[0])}`);
|
|
428
|
-
const old = possibleCounts();
|
|
429
|
-
const d = domainElems(old), v = fcnValues(old);
|
|
430
|
-
const pairs = d.map((x, i) => [x, v[i]]);
|
|
431
|
-
const hit = pairs.find(p => p[0] === a[1]);
|
|
432
|
-
if (hit) {
|
|
433
|
-
if (a[0])
|
|
434
|
-
hit[1] = hit[1] + 1;
|
|
435
|
-
}
|
|
436
|
-
else
|
|
437
|
-
pairs.push([a[1], a[0] ? 1 : 0]);
|
|
438
|
-
registers.set(POSSIBLE_KEY, makeFcnFromPairs(pairs));
|
|
439
|
-
return true;
|
|
440
|
-
}],
|
|
441
|
-
'_Counts': [0, ev => { ev.stTouch++; return possibleCounts(); }],
|
|
442
|
-
'_CheckName': [1, (_, a) => {
|
|
443
|
-
const c = possibleCounts();
|
|
444
|
-
const d = domainElems(c), v = fcnValues(c);
|
|
445
|
-
const i = d.indexOf(a[0]);
|
|
446
|
-
if (i >= 0 && v[i] !== 0)
|
|
447
|
-
return true;
|
|
448
|
-
io.print(fmt(c));
|
|
449
|
-
return false;
|
|
450
|
-
}],
|
|
451
|
-
'_PrintCounts': [0, () => { io.print(fmt(possibleCounts())); return true; }],
|
|
452
|
-
});
|
|
453
|
-
const subsetsOfSize = (b, k) => {
|
|
454
|
-
const out = [];
|
|
455
|
-
const idx = [];
|
|
456
|
-
const rec = (start, left) => {
|
|
457
|
-
if (left === 0) {
|
|
458
|
-
out.push(new SetV(idx.map(i => b[i])));
|
|
459
|
-
return;
|
|
460
|
-
}
|
|
461
|
-
for (let i = start; i <= b.length - left; i++) {
|
|
462
|
-
idx.push(i);
|
|
463
|
-
rec(i + 1, left - 1);
|
|
464
|
-
idx.pop();
|
|
465
|
-
}
|
|
466
|
-
};
|
|
467
|
-
rec(0, k);
|
|
468
|
-
return out;
|
|
469
|
-
};
|
|
470
|
-
// A set known to be empty without enumerating it (components are asked in order, like TLC).
|
|
471
|
-
const knownEmpty = (x) => {
|
|
472
|
-
if (x instanceof CrossV)
|
|
473
|
-
return x.sets.some(knownEmpty);
|
|
474
|
-
if (x instanceof RecSetV)
|
|
475
|
-
return x.sets.some(knownEmpty);
|
|
476
|
-
if (x instanceof FcnSetV)
|
|
477
|
-
return knownEmpty(x.rng) && !knownEmpty(x.dom);
|
|
478
|
-
if (x instanceof IntervalV)
|
|
479
|
-
return x.hi < x.lo;
|
|
480
|
-
if (x instanceof SetV)
|
|
481
|
-
return x.e.length === 0;
|
|
482
|
-
if (x instanceof InfSetV)
|
|
483
|
-
return false;
|
|
484
|
-
return x.enumerable() && x.size() === 0;
|
|
485
|
-
};
|
|
486
|
-
// A uniformly drawn element of a non-empty finite set, without enumerating products and function sets.
|
|
487
|
-
const randomElem = (ev, x) => {
|
|
488
|
-
const rnd = (n) => Math.min(n - 1, Math.floor(ev.hooks.random() * n));
|
|
489
|
-
if (x instanceof IntervalV)
|
|
490
|
-
return x.lo + rnd(x.hi - x.lo + 1);
|
|
491
|
-
if (x instanceof CrossV)
|
|
492
|
-
return new TupleV(x.sets.map(c => randomElem(ev, c)));
|
|
493
|
-
if (x instanceof RecSetV)
|
|
494
|
-
return new RecordV(x.names, x.sets.map(c => randomElem(ev, c)));
|
|
495
|
-
if (x instanceof FcnSetV) {
|
|
496
|
-
const d = x.dom.elems();
|
|
497
|
-
return makeFcn(d, d.map(() => randomElem(ev, x.rng)));
|
|
498
|
-
}
|
|
499
|
-
const e = x.elems();
|
|
500
|
-
return e[rnd(e.length)];
|
|
501
|
-
};
|
|
502
|
-
const finiteSet = (v, op, i, what) => {
|
|
503
|
-
if (v instanceof SetBase && (knownEmpty(v) || v.enumerable()))
|
|
504
|
-
return v;
|
|
505
|
-
return fail(`The ${ORD[i]} argument of ${op} should be a ${what}, but instead it is:\n${fmt(v)}`);
|
|
506
|
-
};
|
|
507
|
-
const cardOf = (x) => knownEmpty(x) ? 0 : x.size();
|
|
508
|
-
const randomSubset = (ev, k, x) => {
|
|
509
|
-
const n = cardOf(x);
|
|
510
|
-
if (n === 0)
|
|
511
|
-
return mkSet([]);
|
|
512
|
-
if (k >= n)
|
|
513
|
-
return mkSet(x.elems().slice());
|
|
514
|
-
if (n <= 4 * k || n <= 64) {
|
|
515
|
-
const e = x.elems().slice();
|
|
516
|
-
for (let i = e.length - 1; i > 0; i--) {
|
|
517
|
-
const j = Math.floor(ev.hooks.random() * (i + 1));
|
|
518
|
-
[e[i], e[j]] = [e[j], e[i]];
|
|
519
|
-
}
|
|
520
|
-
return mkSet(e.slice(0, k));
|
|
521
|
-
}
|
|
522
|
-
let out = [];
|
|
523
|
-
while (out.length < k) {
|
|
524
|
-
const more = [];
|
|
525
|
-
for (let i = out.length; i < k; i++)
|
|
526
|
-
more.push(randomElem(ev, x));
|
|
527
|
-
out = mkSet(out.concat(more)).elems();
|
|
528
|
-
}
|
|
529
|
-
return new SetV(out);
|
|
530
|
-
};
|
|
531
|
-
const randomSetOfSubsets = (ev, picks, p, x) => {
|
|
532
|
-
const e = x.elems();
|
|
533
|
-
const out = [];
|
|
534
|
-
for (let i = 0; i < picks; i++)
|
|
535
|
-
out.push(new SetV(e.filter(() => ev.hooks.random() < p)));
|
|
536
|
-
return mkSet(out);
|
|
537
|
-
};
|
|
538
|
-
const tooManyPicks = (picks, n, op) => {
|
|
539
|
-
if (32 - Math.clz32(picks) > n && picks > 2 ** n)
|
|
540
|
-
fail(`The first argument of ${op} should be a nonnegative integer that is smaller than the subset's size of 2^${n}, but instead it is:\n${picks}`);
|
|
541
|
-
};
|
|
542
|
-
const Randomization = module('Randomization', [], {
|
|
543
|
-
'RandomSubset': [2, (ev, a) => {
|
|
544
|
-
if (typeof a[0] !== 'number')
|
|
545
|
-
fail(`The first argument of RandomSubset should be a nonnegative integer, but instead it is:\n${fmt(a[0])}`);
|
|
546
|
-
const x = finiteSet(a[1], 'RandomSubset', 1, 'a finite set');
|
|
547
|
-
if (a[0] < 0)
|
|
548
|
-
fail(`Attempted to apply the operator overridden by the Java method\npublic static tlc2.value.impl.Value tlc2.module.Randomization.RandomSubset(tlc2.value.impl.Value,tlc2.value.impl.Value),\nbut it produced the following error:\n${a[0]}`);
|
|
549
|
-
return randomSubset(ev, a[0], x);
|
|
550
|
-
}],
|
|
551
|
-
'RandomSetOfSubsets': [3, (ev, a) => {
|
|
552
|
-
if (typeof a[0] !== 'number' || a[0] < 0)
|
|
553
|
-
fail(`The first argument of RandomSetOfSubsets should be a nonnegative integer, but instead it is:\n${fmt(a[0])}`);
|
|
554
|
-
if (typeof a[1] !== 'number' || a[1] < 0)
|
|
555
|
-
fail(`The second argument of RandomSetOfSubsets should be a nonnegative integer, but instead it is:\n${fmt(a[1])}`);
|
|
556
|
-
const x = finiteSet(a[2], 'RandomSetOfSubsets', 2, 'finite set');
|
|
557
|
-
const n = cardOf(x);
|
|
558
|
-
tooManyPicks(a[0], n, 'RandomSetOfSubsets');
|
|
559
|
-
if (n < a[1])
|
|
560
|
-
fail(`The second argument of RandomSetOfSubsets should be a nonnegative integer in range 0..Cardinality(S), but instead it is:\n${fmt(a[1])}`);
|
|
561
|
-
return randomSetOfSubsets(ev, a[0], n === 0 ? 0 : a[1] / n, x);
|
|
562
|
-
}],
|
|
563
|
-
'RandomSubsetSet': [3, (ev, a) => {
|
|
564
|
-
if (typeof a[0] !== 'number' || a[0] < 0)
|
|
565
|
-
fail(`The first argument of RandomSubsetSetProbability should be a nonnegative integer, but instead it is:\n${fmt(a[0])}`);
|
|
566
|
-
const p = typeof a[1] === 'string' && a[1].trim() !== '' ? Number(a[1]) : NaN;
|
|
567
|
-
if (typeof a[1] !== 'string')
|
|
568
|
-
fail(`The second argument of RandomSubsetSetProbability should be a string literal representing a probability, but instead it is:\n${fmt(a[1])}`);
|
|
569
|
-
if (Number.isNaN(p) || p < 0 || p > 1)
|
|
570
|
-
fail(`The second argument of RandomSubsetSetProbability should be a string literal does not represent a parsable probability, but instead it is:\n${fmt(a[1])}`);
|
|
571
|
-
const x = finiteSet(a[2], 'RandomSubsetSetProbability', 2, 'finite set');
|
|
572
|
-
tooManyPicks(a[0], cardOf(x), 'RandomSubsetSetProbability');
|
|
573
|
-
return randomSetOfSubsets(ev, a[0], p, x);
|
|
574
|
-
}],
|
|
575
|
-
'TestRandomSetOfSubsets': [3, () => true],
|
|
576
|
-
});
|
|
577
|
-
void subsetsOfSize;
|
|
578
|
-
void compare;
|
|
579
|
-
void Closure;
|
|
580
|
-
void DefRef;
|
|
581
|
-
void RecordV;
|
|
582
|
-
void FcnV;
|
|
583
|
-
void ModelValue;
|
|
584
|
-
const m = new Map();
|
|
585
|
-
for (const x of [Naturals, Integers, Reals, Sequences, FiniteSets, TLC, TLCExt, Bags, Randomization, Possible])
|
|
586
|
-
m.set(x.name, x);
|
|
587
|
-
return m;
|
|
588
|
-
}
|