tla-ts 0.0.7-dev.bd12468 → 0.0.8-dev.4114a01
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 +2 -0
- package/dist/deciders.cjs +16 -16
- package/dist/deciders.js +49 -49
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +26 -9
- package/dist/index.d.ts +26 -9
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/prove-worker.cjs +1 -1
- package/dist/prove-worker.js +1 -1
- package/dist/prove.cjs +31 -31
- package/dist/prove.js +32 -32
- package/dist/run.cjs +22 -22
- package/dist/run.js +26 -26
- package/dist/tlapm.cjs +1 -1
- package/dist/tlapm.js +1 -1
- package/dist/transfer.cjs +30 -33
- package/dist/transfer.js +30 -33
- package/dist/worker.cjs +4 -1
- package/dist/worker.js +4 -1
- package/package.json +1 -1
- package/src/check.ts +38 -18
- package/src/cli/options.ts +1 -0
- package/src/cli/tlc.ts +9 -0
- package/src/codegen.ts +14 -5
- package/src/dependencies.ts +348 -24
- package/src/environment.ts +30 -16
- package/src/evaluate.ts +125 -704
- package/src/lexer.ts +140 -51
- package/src/liveness.ts +1 -15
- package/src/parallel-worker.ts +24 -7
- package/src/parallel.ts +20 -10
- package/src/parser.ts +5 -2
- package/src/sany/generator.ts +9 -7
- package/src/sany/linter.ts +30 -21
- package/src/search.ts +48 -32
- package/src/spec-description.ts +1 -1
- package/src/successors.ts +3 -9
- package/src/symmetry.ts +32 -143
- package/src/tlc/fingerprint.ts +69 -116
- package/src/tlc/order.ts +118 -73
- package/src/tlc/replay.ts +1 -1
- package/src/values.ts +115 -73
- package/src/worker.ts +16 -0
package/src/tlc/fingerprint.ts
CHANGED
|
@@ -43,8 +43,48 @@ const extendByte = (byte: number) => {
|
|
|
43
43
|
high = (high >>> 8) ^ BYTE_MOD_HIGH[index];
|
|
44
44
|
};
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
// Extending is linear over GF(2), so extending by four bytes at once shifts the fingerprint by a
|
|
47
|
+
// word and adds, for each of the four, the fingerprint from zero of that byte followed by the zero
|
|
48
|
+
// bytes that come after it: SLICE_HIGH[k] and SLICE_LOW[k] hold them for k zero bytes after.
|
|
49
|
+
const createSliceTables = () => {
|
|
50
|
+
const highs = [BYTE_MOD_HIGH];
|
|
51
|
+
const lows = [BYTE_MOD_LOW];
|
|
52
|
+
for (let zeros = 1; zeros < 4; zeros++) {
|
|
53
|
+
const previousHigh = highs[zeros - 1];
|
|
54
|
+
const previousLow = lows[zeros - 1];
|
|
55
|
+
const sliceHigh = new Int32Array(256);
|
|
56
|
+
const sliceLow = new Int32Array(256);
|
|
57
|
+
for (let byte = 0; byte < 256; byte++) {
|
|
58
|
+
const index = previousLow[byte] & 0xff;
|
|
59
|
+
sliceLow[byte] =
|
|
60
|
+
((previousLow[byte] >>> 8) | (previousHigh[byte] << 24)) ^ BYTE_MOD_LOW[index];
|
|
61
|
+
sliceHigh[byte] = (previousHigh[byte] >>> 8) ^ BYTE_MOD_HIGH[index];
|
|
62
|
+
}
|
|
63
|
+
highs.push(sliceHigh);
|
|
64
|
+
lows.push(sliceLow);
|
|
65
|
+
}
|
|
66
|
+
return { highs, lows };
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const SLICES = createSliceTables();
|
|
70
|
+
const SLICE_HIGH_0 = SLICES.highs[0];
|
|
71
|
+
const SLICE_HIGH_1 = SLICES.highs[1];
|
|
72
|
+
const SLICE_HIGH_2 = SLICES.highs[2];
|
|
73
|
+
const SLICE_HIGH_3 = SLICES.highs[3];
|
|
74
|
+
const SLICE_LOW_0 = SLICES.lows[0];
|
|
75
|
+
const SLICE_LOW_1 = SLICES.lows[1];
|
|
76
|
+
const SLICE_LOW_2 = SLICES.lows[2];
|
|
77
|
+
const SLICE_LOW_3 = SLICES.lows[3];
|
|
78
|
+
|
|
79
|
+
// Extends the fingerprint by the four bytes of a word, lowest first.
|
|
80
|
+
const extendInt = (word: number) => {
|
|
81
|
+
const mixed = low ^ word;
|
|
82
|
+
const first = mixed & 0xff;
|
|
83
|
+
const second = (mixed >>> 8) & 0xff;
|
|
84
|
+
const third = (mixed >>> 16) & 0xff;
|
|
85
|
+
const fourth = mixed >>> 24;
|
|
86
|
+
low = high ^ SLICE_LOW_3[first] ^ SLICE_LOW_2[second] ^ SLICE_LOW_1[third] ^ SLICE_LOW_0[fourth];
|
|
87
|
+
high = SLICE_HIGH_3[first] ^ SLICE_HIGH_2[second] ^ SLICE_HIGH_1[third] ^ SLICE_HIGH_0[fourth];
|
|
48
88
|
};
|
|
49
89
|
|
|
50
90
|
const BOOL_VALUE = 0;
|
|
@@ -71,9 +111,16 @@ const extendScalar = (value: boolean | number | string | ModelValue) => {
|
|
|
71
111
|
if (typeof value === "string") {
|
|
72
112
|
extendByte(STRING_VALUE);
|
|
73
113
|
extendInt(value.length);
|
|
74
|
-
|
|
75
|
-
|
|
114
|
+
const whole = value.length & ~3;
|
|
115
|
+
for (let index = 0; index < whole; index += 4) {
|
|
116
|
+
extendInt(
|
|
117
|
+
(value.charCodeAt(index) & 0xff) |
|
|
118
|
+
((value.charCodeAt(index + 1) & 0xff) << 8) |
|
|
119
|
+
((value.charCodeAt(index + 2) & 0xff) << 16) |
|
|
120
|
+
((value.charCodeAt(index + 3) & 0xff) << 24),
|
|
121
|
+
);
|
|
76
122
|
}
|
|
123
|
+
for (let index = whole; index < value.length; index++) extendByte(value.charCodeAt(index));
|
|
77
124
|
return 5 + value.length;
|
|
78
125
|
}
|
|
79
126
|
extendByte(MODEL_VALUE);
|
|
@@ -114,116 +161,22 @@ export const getTlcFingerprint = (state: Value[]) => {
|
|
|
114
161
|
return fingerprint;
|
|
115
162
|
};
|
|
116
163
|
|
|
117
|
-
//
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
// The functions and sets a fingerprinter caches before it starts over, which bounds the values it
|
|
137
|
-
// keeps alive.
|
|
138
|
-
const CACHED_VALUE_LIMIT = 1 << 22;
|
|
139
|
-
|
|
140
|
-
// Extending a fingerprint by a byte is linear over GF(2), so extending it by a value's bytes
|
|
141
|
-
// XORs the value's own fingerprint from zero into the fingerprint shifted by their length.
|
|
142
|
-
// The fingerprinter caches each distinct function or set it meets, nested ones included, across
|
|
143
|
-
// every batch of states it fingerprints, as states share most of their values, even states of
|
|
144
|
-
// different levels. It reads a state's variables in `order`.
|
|
145
|
-
export const createTlcFingerprinter = (order: number[], limit = CACHED_VALUE_LIMIT) => {
|
|
146
|
-
const shiftTables = new Map<number, Int32Array>();
|
|
147
|
-
const ids = new Map<Value, number>();
|
|
148
|
-
// The byte count and fingerprint from zero of each cached value, three numbers per id, and the
|
|
149
|
-
// shift table for its byte count.
|
|
150
|
-
const contributions: number[] = [];
|
|
151
|
-
const contributionShiftTables: Int32Array[] = [];
|
|
152
|
-
const getShiftTable = (length: number) => {
|
|
153
|
-
const cached = shiftTables.get(length);
|
|
154
|
-
if (cached) return cached;
|
|
155
|
-
const table = createShiftTable(length);
|
|
156
|
-
shiftTables.set(length, table);
|
|
157
|
-
return table;
|
|
158
|
-
};
|
|
159
|
-
// Extends the fingerprint by a value's bytes and returns how many there are.
|
|
160
|
-
const extendElement = (value: Value): number => {
|
|
161
|
-
if (typeof value !== "object" || isModelValue(value)) return extendScalar(value);
|
|
162
|
-
const id = getId(value);
|
|
163
|
-
const table = contributionShiftTables[id];
|
|
164
|
-
const shiftedHigh = high;
|
|
165
|
-
const shiftedLow = low;
|
|
166
|
-
high = contributions[3 * id + 1];
|
|
167
|
-
low = contributions[3 * id + 2];
|
|
168
|
-
for (let position = 0; position < 8; position++) {
|
|
169
|
-
const word = position < 4 ? shiftedLow : shiftedHigh;
|
|
170
|
-
const entry = (position * 256 + ((word >>> ((position & 3) * 8)) & 0xff)) * 2;
|
|
171
|
-
high ^= table[entry];
|
|
172
|
-
low ^= table[entry + 1];
|
|
173
|
-
}
|
|
174
|
-
return contributions[3 * id];
|
|
175
|
-
};
|
|
176
|
-
const getId = (value: Value) => {
|
|
177
|
-
const cached = ids.get(value);
|
|
178
|
-
if (cached !== undefined) return cached;
|
|
179
|
-
const outerHigh = high;
|
|
180
|
-
const outerLow = low;
|
|
181
|
-
high = 0;
|
|
182
|
-
low = 0;
|
|
183
|
-
const length = { value: 5 };
|
|
184
|
-
if (isFunctionValue(value)) {
|
|
185
|
-
const functionValue = toFiniteFunction(value);
|
|
186
|
-
extendByte(FCN_RCD_VALUE);
|
|
187
|
-
extendInt(functionValue.domain.length);
|
|
188
|
-
for (let index = 0; index < functionValue.domain.length; index++) {
|
|
189
|
-
length.value += extendElement(functionValue.domain[index]);
|
|
190
|
-
length.value += extendElement(functionValue.range[index]);
|
|
191
|
-
}
|
|
192
|
-
} else {
|
|
193
|
-
const elements = toFiniteSet(value).elements;
|
|
194
|
-
extendByte(SET_ENUM_VALUE);
|
|
195
|
-
extendInt(elements.length);
|
|
196
|
-
for (const element of elements) length.value += extendElement(element);
|
|
197
|
-
}
|
|
198
|
-
const id = contributionShiftTables.length;
|
|
199
|
-
ids.set(value, id);
|
|
200
|
-
contributions.push(length.value, high, low);
|
|
201
|
-
contributionShiftTables.push(getShiftTable(length.value));
|
|
202
|
-
high = outerHigh;
|
|
203
|
-
low = outerLow;
|
|
204
|
-
return id;
|
|
205
|
-
};
|
|
206
|
-
return (states: Value[][]) => {
|
|
207
|
-
const fingerprints = new BigInt64Array(states.length);
|
|
208
|
-
// Each fingerprint's low and high words, as a BigInt64Array is little-endian.
|
|
209
|
-
const words = new Int32Array(fingerprints.buffer);
|
|
210
|
-
const outerHigh = high;
|
|
211
|
-
const outerLow = low;
|
|
212
|
-
states.forEach((state, position) => {
|
|
213
|
-
if (ids.size >= limit) {
|
|
214
|
-
shiftTables.clear();
|
|
215
|
-
ids.clear();
|
|
216
|
-
contributions.length = 0;
|
|
217
|
-
contributionShiftTables.length = 0;
|
|
218
|
-
}
|
|
219
|
-
high = INITIAL_HIGH;
|
|
220
|
-
low = INITIAL_LOW;
|
|
221
|
-
for (const index of order) extendElement(state[index]);
|
|
222
|
-
words[2 * position] = low;
|
|
223
|
-
words[2 * position + 1] = high;
|
|
224
|
-
});
|
|
225
|
-
high = outerHigh;
|
|
226
|
-
low = outerLow;
|
|
227
|
-
return fingerprints;
|
|
228
|
-
};
|
|
164
|
+
// TLCStateMut#fingerPrint of each state's variables in TLC's variable order, as the fingerprint
|
|
165
|
+
// store reads them.
|
|
166
|
+
export const getTlcFingerprints = (states: Value[][], order: number[]) => {
|
|
167
|
+
const fingerprints = new BigInt64Array(states.length);
|
|
168
|
+
// Each fingerprint's low and high words, as a BigInt64Array is little-endian.
|
|
169
|
+
const words = new Int32Array(fingerprints.buffer);
|
|
170
|
+
const outerHigh = high;
|
|
171
|
+
const outerLow = low;
|
|
172
|
+
states.forEach((state, position) => {
|
|
173
|
+
high = INITIAL_HIGH;
|
|
174
|
+
low = INITIAL_LOW;
|
|
175
|
+
for (const index of order) extendValue(state[index]);
|
|
176
|
+
words[2 * position] = low;
|
|
177
|
+
words[2 * position + 1] = high;
|
|
178
|
+
});
|
|
179
|
+
high = outerHigh;
|
|
180
|
+
low = outerLow;
|
|
181
|
+
return fingerprints;
|
|
229
182
|
};
|
package/src/tlc/order.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { Module, SourceLocation } from "../ast";
|
|
|
4
4
|
import type { ConfigValue, ModelConfig } from "../config";
|
|
5
5
|
import { createMissingModuleError } from "../errors";
|
|
6
6
|
import { tokenize, type Token } from "../lexer";
|
|
7
|
-
import { extractModule, parseModule } from "../parser";
|
|
7
|
+
import { extractModule, parseModule, parseModuleTokens } from "../parser";
|
|
8
8
|
import { isBefore } from "../resolve";
|
|
9
9
|
import {
|
|
10
10
|
SANY_BUILTINS,
|
|
@@ -21,6 +21,7 @@ type SymbolKind = "builtin" | "constant" | "variable" | "definition" | "theorem"
|
|
|
21
21
|
interface SymbolEntry {
|
|
22
22
|
key: string;
|
|
23
23
|
name: string;
|
|
24
|
+
hash: number;
|
|
24
25
|
kind: SymbolKind;
|
|
25
26
|
isLocal: boolean;
|
|
26
27
|
}
|
|
@@ -28,6 +29,8 @@ interface SymbolEntry {
|
|
|
28
29
|
interface SymbolTable {
|
|
29
30
|
put: (entry: SymbolEntry) => void;
|
|
30
31
|
inserted: SymbolEntry[];
|
|
32
|
+
// Where the entries the module adds itself start, after those of the modules it extends.
|
|
33
|
+
ownStart: number;
|
|
31
34
|
enumerate: () => SymbolEntry[];
|
|
32
35
|
}
|
|
33
36
|
|
|
@@ -64,38 +67,48 @@ const getJavaHash = (text: string) => {
|
|
|
64
67
|
};
|
|
65
68
|
|
|
66
69
|
// java.util.Hashtable as SANY's Context uses it: default capacity 11, load factor 0.75, new
|
|
67
|
-
// entries at the head of their bucket, and enumeration from the last bucket to the first.
|
|
70
|
+
// entries at the head of their bucket, and enumeration from the last bucket to the first. Each
|
|
71
|
+
// bucket lists its entries from tail to head, so a new entry goes at the end.
|
|
68
72
|
const createSymbolTable = (): SymbolTable => {
|
|
69
|
-
|
|
73
|
+
let buckets: SymbolEntry[][] = Array.from({ length: 11 }, () => []);
|
|
74
|
+
let threshold = 8;
|
|
70
75
|
const keys = new Set<string>();
|
|
71
76
|
const inserted: SymbolEntry[] = [];
|
|
72
|
-
const
|
|
73
|
-
(
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const buckets = Array.from({ length: capacity }, (): SymbolEntry[] => []);
|
|
77
|
-
for (let index = table.buckets.length - 1; index >= 0; index--) {
|
|
78
|
-
for (const entry of table.buckets[index]) buckets[getBucket(entry, capacity)].unshift(entry);
|
|
77
|
+
const forEachEntry = (visit: (entry: SymbolEntry) => void) => {
|
|
78
|
+
for (let index = buckets.length - 1; index >= 0; index--) {
|
|
79
|
+
const bucket = buckets[index];
|
|
80
|
+
for (let position = bucket.length - 1; position >= 0; position--) visit(bucket[position]);
|
|
79
81
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
+
};
|
|
83
|
+
const rehash = () => {
|
|
84
|
+
const capacity = buckets.length * 2 + 1;
|
|
85
|
+
const rehashed: SymbolEntry[][] = Array.from({ length: capacity }, () => []);
|
|
86
|
+
forEachEntry((entry) => rehashed[entry.hash % capacity].push(entry));
|
|
87
|
+
buckets = rehashed;
|
|
88
|
+
threshold = Math.floor(capacity * 0.75);
|
|
82
89
|
};
|
|
83
90
|
return {
|
|
84
91
|
put: (entry) => {
|
|
85
92
|
if (keys.has(entry.key)) return;
|
|
86
|
-
if (inserted.length >=
|
|
87
|
-
|
|
93
|
+
if (inserted.length >= threshold) rehash();
|
|
94
|
+
buckets[entry.hash % buckets.length].push(entry);
|
|
88
95
|
keys.add(entry.key);
|
|
89
96
|
inserted.push(entry);
|
|
90
97
|
},
|
|
91
98
|
inserted,
|
|
92
|
-
|
|
99
|
+
ownStart: 0,
|
|
100
|
+
enumerate: () => {
|
|
101
|
+
const entries: SymbolEntry[] = [];
|
|
102
|
+
forEachEntry((entry) => entries.push(entry));
|
|
103
|
+
return entries;
|
|
104
|
+
},
|
|
93
105
|
};
|
|
94
106
|
};
|
|
95
107
|
|
|
96
|
-
const createEntry = (name: string, kind: SymbolKind, isLocal: boolean) => ({
|
|
108
|
+
const createEntry = (name: string, kind: SymbolKind, isLocal: boolean): SymbolEntry => ({
|
|
97
109
|
key: kind === "module" ? `module ${name}` : name,
|
|
98
110
|
name,
|
|
111
|
+
hash: getJavaHash(name) & 0x7fffffff,
|
|
99
112
|
kind,
|
|
100
113
|
isLocal,
|
|
101
114
|
});
|
|
@@ -112,7 +125,8 @@ const getDeclaredName = (tokens: Token[], start: number) => {
|
|
|
112
125
|
return { name: first.text === "-" ? "-." : getCanonicalName(first.text), end: start + 2 };
|
|
113
126
|
}
|
|
114
127
|
if (second?.text !== "(") return { name: first.text, end: start + 1 };
|
|
115
|
-
|
|
128
|
+
let close = start + 2;
|
|
129
|
+
while (close < tokens.length && tokens[close].text !== ")") close++;
|
|
116
130
|
return { name: first.text, end: close + 1 };
|
|
117
131
|
};
|
|
118
132
|
|
|
@@ -248,12 +262,40 @@ const findModule = (modules: Module[], name: string): Module | undefined =>
|
|
|
248
262
|
modules.find((module) => module.name === name) ??
|
|
249
263
|
modules.map((module) => findModule(module.modules, name)).find(Boolean);
|
|
250
264
|
|
|
265
|
+
interface ModuleText {
|
|
266
|
+
tokens: Token[];
|
|
267
|
+
ranges: Map<string, { start: number; end: number }>;
|
|
268
|
+
getModule: () => Module;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// A module's source is read into tokens once, and parsed from them once, for every order and for
|
|
272
|
+
// the environment that evaluates it.
|
|
273
|
+
const readModuleText = (text: string | undefined): ModuleText => {
|
|
274
|
+
const tokens = text === undefined ? [] : tokenize(text);
|
|
275
|
+
const parsed = { module: undefined as Module | undefined };
|
|
276
|
+
return {
|
|
277
|
+
tokens,
|
|
278
|
+
ranges: getRanges(tokens),
|
|
279
|
+
getModule: () =>
|
|
280
|
+
(parsed.module ??= text === undefined ? parseModule("") : parseModuleTokens(text, tokens)),
|
|
281
|
+
};
|
|
282
|
+
};
|
|
283
|
+
|
|
251
284
|
// The modules TLC reads for a spec: the root module, TLC's standard modules, the files the loader
|
|
252
285
|
// returns, the modules that follow the root module, and the modules nested in any of these. A
|
|
253
286
|
// module without a source has no tokens.
|
|
254
287
|
const createModuleSet = (source: string, loadModule: ModuleLoader) => {
|
|
255
288
|
const files = new Map<string, ParsedModule>();
|
|
256
289
|
const nestedModules = new Map<string, ParsedModule>();
|
|
290
|
+
// The read of each module text, so that the environment parses a text the order read only once.
|
|
291
|
+
const texts = new Map<string, ModuleText>();
|
|
292
|
+
const readText = (text: string | undefined) => {
|
|
293
|
+
const known = text === undefined ? undefined : texts.get(text);
|
|
294
|
+
if (known) return known;
|
|
295
|
+
const read = readModuleText(text);
|
|
296
|
+
if (text !== undefined) texts.set(text, read);
|
|
297
|
+
return read;
|
|
298
|
+
};
|
|
257
299
|
const getFile = (name: string | undefined) => {
|
|
258
300
|
const cached = files.get(name ?? "");
|
|
259
301
|
if (cached) return cached;
|
|
@@ -264,15 +306,9 @@ const createModuleSet = (source: string, loadModule: ModuleLoader) => {
|
|
|
264
306
|
TLC_MODULE_SOURCES.get(name) ??
|
|
265
307
|
loadModule?.(name) ??
|
|
266
308
|
extractModule(source, name));
|
|
267
|
-
const tokens
|
|
268
|
-
const ranges = getRanges(tokens);
|
|
309
|
+
const { tokens, ranges, getModule } = readText(text);
|
|
269
310
|
const range = ranges.get(getModuleName(tokens) ?? "") ?? { start: 0, end: 0 };
|
|
270
|
-
const
|
|
271
|
-
const file = {
|
|
272
|
-
tokens,
|
|
273
|
-
...range,
|
|
274
|
-
getModule: () => (parsed.module ??= parseModule(text ?? "")),
|
|
275
|
-
};
|
|
311
|
+
const file = { tokens, ...range, getModule };
|
|
276
312
|
files.set(name ?? "", file);
|
|
277
313
|
ranges.forEach((nestedRange, nestedName) => {
|
|
278
314
|
if (nestedRange.start === range.start || nestedModules.has(nestedName)) return;
|
|
@@ -285,13 +321,17 @@ const createModuleSet = (source: string, loadModule: ModuleLoader) => {
|
|
|
285
321
|
return file;
|
|
286
322
|
};
|
|
287
323
|
getFile(undefined);
|
|
288
|
-
return
|
|
289
|
-
(name
|
|
324
|
+
return {
|
|
325
|
+
getParsed: (name: string | undefined): ParsedModule =>
|
|
326
|
+
(name === undefined ? undefined : nestedModules.get(name)) ?? getFile(name),
|
|
327
|
+
parseText: (text: string) => readText(text).getModule(),
|
|
328
|
+
};
|
|
290
329
|
};
|
|
291
330
|
|
|
292
331
|
// SANY's Context of each module, which the root module has under undefined.
|
|
293
|
-
|
|
294
|
-
|
|
332
|
+
type ModuleSet = ReturnType<typeof createModuleSet>["getParsed"];
|
|
333
|
+
|
|
334
|
+
const createContexts = (getParsed: ModuleSet) => {
|
|
295
335
|
const getUnits = (unit: ParsedModule) => {
|
|
296
336
|
const tokens = unit.tokens;
|
|
297
337
|
const module = unit.getModule();
|
|
@@ -391,8 +431,6 @@ const createContexts = (source: string, loadModule: ModuleLoader) => {
|
|
|
391
431
|
};
|
|
392
432
|
|
|
393
433
|
const contexts = new Map<string, SymbolTable>();
|
|
394
|
-
// Where the entries a module adds itself start, after those of the modules it extends.
|
|
395
|
-
const ownStarts = new Map<SymbolTable, number>();
|
|
396
434
|
const getContext = (name: string | undefined): SymbolTable => {
|
|
397
435
|
const cached = contexts.get(name ?? "");
|
|
398
436
|
if (cached) return cached;
|
|
@@ -407,7 +445,7 @@ const createContexts = (source: string, loadModule: ModuleLoader) => {
|
|
|
407
445
|
if (!entry.isLocal) table.put(entry);
|
|
408
446
|
}),
|
|
409
447
|
);
|
|
410
|
-
|
|
448
|
+
table.ownStart = table.inserted.length;
|
|
411
449
|
getUnits(unit).forEach((item) => item.apply(table));
|
|
412
450
|
return table;
|
|
413
451
|
};
|
|
@@ -415,54 +453,15 @@ const createContexts = (source: string, loadModule: ModuleLoader) => {
|
|
|
415
453
|
getContext,
|
|
416
454
|
getOwnEntries: (name: string | undefined) => {
|
|
417
455
|
const table = getContext(name);
|
|
418
|
-
return table.inserted.slice(
|
|
456
|
+
return table.inserted.slice(table.ownStart);
|
|
419
457
|
},
|
|
420
458
|
};
|
|
421
459
|
};
|
|
422
460
|
|
|
423
|
-
// The variables or constants of the root module in the reverse of its context enumeration, as
|
|
424
|
-
// ModuleNode.getVariableDecls and getConstantDecls return them. TLC compares variables in this
|
|
425
|
-
// order, and checks that the configuration assigns each constant in it.
|
|
426
|
-
export const getTlcDeclarationOrder = (
|
|
427
|
-
source: string,
|
|
428
|
-
loadModule: ModuleLoader,
|
|
429
|
-
kind: "variable" | "constant",
|
|
430
|
-
): string[] =>
|
|
431
|
-
createContexts(source, loadModule)
|
|
432
|
-
.getContext(undefined)
|
|
433
|
-
.enumerate()
|
|
434
|
-
.filter((entry) => entry.kind === kind)
|
|
435
|
-
.map((entry) => entry.name)
|
|
436
|
-
.reverse();
|
|
437
|
-
|
|
438
|
-
// The names of the definitions each module adds to its context, in the order of
|
|
439
|
-
// ModuleNode.getOpDefs, in which TLC computes the constant definitions as it starts. An INSTANCE
|
|
440
|
-
// adds the definitions of the module it instantiates in the order that module's Hashtable
|
|
441
|
-
// enumerates them.
|
|
442
|
-
export const createTlcDefinitionOrder = (source: string, loadModule: ModuleLoader) => {
|
|
443
|
-
const contexts = createContexts(source, loadModule);
|
|
444
|
-
// The root module's context reads every file of the spec, and so finds the modules nested in
|
|
445
|
-
// the files it extends or instantiates.
|
|
446
|
-
contexts.getContext(undefined);
|
|
447
|
-
return (name: string | undefined) =>
|
|
448
|
-
contexts
|
|
449
|
-
.getOwnEntries(name)
|
|
450
|
-
.filter((entry) => entry.kind === "definition")
|
|
451
|
-
.map((entry) => entry.name);
|
|
452
|
-
};
|
|
453
|
-
|
|
454
|
-
export const getTlcVariableOrder = (source: string, loadModule: ModuleLoader) =>
|
|
455
|
-
getTlcDeclarationOrder(source, loadModule, "variable");
|
|
456
|
-
|
|
457
461
|
// Every string TLC interns before model checking starts, in order: values in the config, TLC's
|
|
458
462
|
// startup strings, each module's tokens in the order SpecObj.loadSpec parses the modules, string
|
|
459
463
|
// literals in semantic analysis order, and the names TLC interns for TLCGet.
|
|
460
|
-
|
|
461
|
-
source: string,
|
|
462
|
-
config: ModelConfig,
|
|
463
|
-
loadModule: ModuleLoader,
|
|
464
|
-
): string[] => {
|
|
465
|
-
const getParsed = createModuleSet(source, loadModule);
|
|
464
|
+
const getStringOrder = (getParsed: ModuleSet, config: ModelConfig): string[] => {
|
|
466
465
|
const order = new Set<string>();
|
|
467
466
|
const intern = (text: string) => {
|
|
468
467
|
order.add(text);
|
|
@@ -563,3 +562,49 @@ export const getTlcStringOrder = (
|
|
|
563
562
|
TLC_RUNTIME_STRINGS.forEach(intern);
|
|
564
563
|
return [...order];
|
|
565
564
|
};
|
|
565
|
+
|
|
566
|
+
export interface TlcOrder {
|
|
567
|
+
// Every string TLC interns before model checking starts.
|
|
568
|
+
getStringOrder: (config: ModelConfig) => string[];
|
|
569
|
+
// The variables or constants of the root module in the reverse of its context enumeration, as
|
|
570
|
+
// ModuleNode.getVariableDecls and getConstantDecls return them. TLC compares variables in this
|
|
571
|
+
// order, and checks that the configuration assigns each constant in it.
|
|
572
|
+
getDeclarationOrder: (kind: "variable" | "constant") => string[];
|
|
573
|
+
// The names of the definitions each module adds to its context, in the order of
|
|
574
|
+
// ModuleNode.getOpDefs, in which TLC computes the constant definitions as it starts. An INSTANCE
|
|
575
|
+
// adds the definitions of the module it instantiates in the order that module's Hashtable
|
|
576
|
+
// enumerates them.
|
|
577
|
+
createDefinitionOrder: () => (name: string | undefined) => string[];
|
|
578
|
+
// Parses a module's source, once for the order and the environment.
|
|
579
|
+
parseModule: (source: string) => Module;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// One check reads the spec's modules once, and every order shares their tokens, parses and
|
|
583
|
+
// contexts.
|
|
584
|
+
export const createTlcOrder = (source: string, loadModule: ModuleLoader): TlcOrder => {
|
|
585
|
+
const modules = createModuleSet(source, loadModule);
|
|
586
|
+
const getParsed = modules.getParsed;
|
|
587
|
+
let contexts: ReturnType<typeof createContexts> | undefined;
|
|
588
|
+
const getContexts = () => (contexts ??= createContexts(getParsed));
|
|
589
|
+
return {
|
|
590
|
+
getStringOrder: (config) => getStringOrder(getParsed, config),
|
|
591
|
+
getDeclarationOrder: (kind) =>
|
|
592
|
+
getContexts()
|
|
593
|
+
.getContext(undefined)
|
|
594
|
+
.enumerate()
|
|
595
|
+
.filter((entry) => entry.kind === kind)
|
|
596
|
+
.map((entry) => entry.name)
|
|
597
|
+
.reverse(),
|
|
598
|
+
parseModule: modules.parseText,
|
|
599
|
+
createDefinitionOrder: () => {
|
|
600
|
+
const { getContext, getOwnEntries } = getContexts();
|
|
601
|
+
// The root module's context reads every file of the spec, and so finds the modules nested
|
|
602
|
+
// in the files it extends or instantiates.
|
|
603
|
+
getContext(undefined);
|
|
604
|
+
return (name) =>
|
|
605
|
+
getOwnEntries(name)
|
|
606
|
+
.filter((entry) => entry.kind === "definition")
|
|
607
|
+
.map((entry) => entry.name);
|
|
608
|
+
},
|
|
609
|
+
};
|
|
610
|
+
};
|
package/src/tlc/replay.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Expression, SourceLocation } from "../ast";
|
|
2
2
|
import { TlaEvaluationError } from "../errors";
|
|
3
|
+
import { isConstantLevel } from "../dependencies";
|
|
3
4
|
import {
|
|
4
5
|
bindDefinitions,
|
|
5
6
|
bindLazyArguments,
|
|
@@ -9,7 +10,6 @@ import {
|
|
|
9
10
|
freezeCallStack,
|
|
10
11
|
getLazyFunction,
|
|
11
12
|
toKeepLazyContext,
|
|
12
|
-
isConstantLevel,
|
|
13
13
|
lookupScope,
|
|
14
14
|
resolveApplyOperator,
|
|
15
15
|
resolveGlobalOperator,
|