tla-ts 0.0.8-dev.aa7b562 → 0.0.8-dev.d9bffda
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/dist/index.d.cts +18 -9
- package/dist/index.d.ts +18 -9
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/prove.cjs +2 -2
- package/dist/prove.js +2 -2
- package/dist/run.cjs +1 -1
- package/dist/run.js +1 -1
- package/dist/transfer.cjs +26 -26
- package/dist/transfer.js +30 -30
- package/package.json +1 -1
- package/src/codegen.ts +0 -5
- package/src/dependencies.ts +348 -24
- package/src/environment.ts +23 -11
- package/src/evaluate.ts +123 -703
- package/src/liveness.ts +1 -15
- package/src/search.ts +5 -8
- 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 +27 -48
- package/src/tlc/replay.ts +1 -1
- package/src/values.ts +86 -71
package/package.json
CHANGED
package/src/codegen.ts
CHANGED
|
@@ -11,9 +11,7 @@ import {
|
|
|
11
11
|
getFieldSelector,
|
|
12
12
|
getMissingArgumentDescriber,
|
|
13
13
|
getQuantifierElements,
|
|
14
|
-
isClosedConstant,
|
|
15
14
|
isValueArgument,
|
|
16
|
-
isWorthCaching,
|
|
17
15
|
keepLazy,
|
|
18
16
|
resolveGlobalOperator,
|
|
19
17
|
expectFunction,
|
|
@@ -281,9 +279,6 @@ const isReadOnly = (expression: Expression): boolean => {
|
|
|
281
279
|
const generateExpression = (generator: Generator, expression: Expression, frame: Frame): string => {
|
|
282
280
|
const generate = (node: Expression, nodeFrame = frame) =>
|
|
283
281
|
generateExpression(generator, node, nodeFrame);
|
|
284
|
-
if (isWorthCaching(expression) && isClosedConstant(expression, generator.environment)) {
|
|
285
|
-
return generateDelegate(generator, expression, frame);
|
|
286
|
-
}
|
|
287
282
|
switch (expression.kind) {
|
|
288
283
|
case "Number":
|
|
289
284
|
if (isTlcInteger(expression.value)) {
|
package/src/dependencies.ts
CHANGED
|
@@ -1,18 +1,79 @@
|
|
|
1
1
|
import { getChildren, isLocallyResolved, type Definition, type Expression } from "./ast";
|
|
2
|
-
import {
|
|
2
|
+
import type { Environment } from "./evaluate";
|
|
3
|
+
import { ACTION_LEVEL, CONSTANT_LEVEL, TEMPORAL_LEVEL, VARIABLE_LEVEL } from "./levels";
|
|
3
4
|
|
|
4
|
-
|
|
5
|
+
// The names of a spec that the facts depend on, which are final before the environment is built.
|
|
6
|
+
interface DefinitionNames extends Pick<
|
|
7
|
+
Environment,
|
|
8
|
+
"definitions" | "substitutions" | "constants" | "variableIndex" | "builtins" | "variables"
|
|
9
|
+
> {}
|
|
10
|
+
|
|
11
|
+
// What TLC's evaluation needs to know about each definition of a spec: whether it is constant-level,
|
|
12
|
+
// which effects evaluating it has, which variables it reads and its liveness level. Each fact is
|
|
13
|
+
// reachability over the definitions it calls, so one pass over the spec's call graph computes them
|
|
14
|
+
// all, once, when the environment is created. A row of words holds a definition's facts: the first
|
|
15
|
+
// word holds the flags below, and the rest a bitset of the variables it reads.
|
|
16
|
+
export interface DefinitionFacts {
|
|
17
|
+
ids: Map<Definition, number>;
|
|
18
|
+
rows: Uint32Array;
|
|
19
|
+
// The words of each row: one of flags, then one per 32 variables.
|
|
20
|
+
words: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// The definition reads a variable, a state-level builtin, or an action or temporal operator.
|
|
24
|
+
const NONCONSTANT = 1;
|
|
25
|
+
// Evaluating the definition again can print, assert, read the clock or choose at random.
|
|
26
|
+
const IMPURE = 2;
|
|
27
|
+
// Like IMPURE, but an Assert that held holds again, so repeating it looks the same.
|
|
28
|
+
const REPEATED_EFFECT = 4;
|
|
29
|
+
// The definition builds a set with UNION or \cup, which streams until TLC caches it.
|
|
30
|
+
const STREAMED = 8;
|
|
31
|
+
// The variables the definition reads don't decide its value: it has an effect, or it is an action.
|
|
32
|
+
const READS_IMPURE = 16;
|
|
33
|
+
// The liveness level of the definition, as thresholds so that reachability is a bitwise or.
|
|
34
|
+
const VARIABLE_LEVEL_FLAGS = 32;
|
|
35
|
+
const ACTION_LEVEL_FLAGS = VARIABLE_LEVEL_FLAGS | 64;
|
|
36
|
+
const TEMPORAL_LEVEL_FLAGS = ACTION_LEVEL_FLAGS | 128;
|
|
37
|
+
|
|
38
|
+
const ACTION_KINDS = new Set<Expression["kind"]>([
|
|
5
39
|
"Prime",
|
|
6
40
|
"Unchanged",
|
|
7
|
-
"Enabled",
|
|
8
|
-
"Always",
|
|
9
|
-
"Eventually",
|
|
10
41
|
"BoxAction",
|
|
11
42
|
"AngleAction",
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
const TEMPORAL_KINDS = new Set<Expression["kind"]>([
|
|
46
|
+
"Always",
|
|
47
|
+
"Eventually",
|
|
12
48
|
"WeakFairness",
|
|
13
49
|
"StrongFairness",
|
|
14
50
|
]);
|
|
15
51
|
|
|
52
|
+
// TLC bounds the level of every action and temporal operator above constant, whatever its operands.
|
|
53
|
+
const NONCONSTANT_KINDS = new Set<Expression["kind"]>([
|
|
54
|
+
...ACTION_KINDS,
|
|
55
|
+
...TEMPORAL_KINDS,
|
|
56
|
+
"Enabled",
|
|
57
|
+
]);
|
|
58
|
+
|
|
59
|
+
const TEMPORAL_OPERATORS = new Set(["~>", "-+->"]);
|
|
60
|
+
|
|
61
|
+
const NONCONSTANT_OPERATORS = new Set([...TEMPORAL_OPERATORS, "\\cdot"]);
|
|
62
|
+
|
|
63
|
+
const IMPURE_BUILTINS = new Set([
|
|
64
|
+
"Print",
|
|
65
|
+
"PrintT",
|
|
66
|
+
"Assert",
|
|
67
|
+
"JavaTime",
|
|
68
|
+
"RandomElement",
|
|
69
|
+
"TLCGet",
|
|
70
|
+
"TLCSet",
|
|
71
|
+
]);
|
|
72
|
+
|
|
73
|
+
// UNION and \cup build sets that stream the sets they combine until TLC caches them, and TLC
|
|
74
|
+
// builds a new one at each evaluation.
|
|
75
|
+
const STREAMED_BUILTINS = new Set(["UNION", "\\cup"]);
|
|
76
|
+
|
|
16
77
|
const getReferencedName = (expression: Expression) => {
|
|
17
78
|
if (expression.kind === "Identifier") return expression.name;
|
|
18
79
|
if (expression.kind === "Apply" || expression.kind === "OperatorReference")
|
|
@@ -20,30 +81,293 @@ const getReferencedName = (expression: Expression) => {
|
|
|
20
81
|
return undefined;
|
|
21
82
|
};
|
|
22
83
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
84
|
+
const isImpureBuiltin = (name: string, environment: DefinitionNames) =>
|
|
85
|
+
IMPURE_BUILTINS.has(name) || environment.builtins.get(name)?.isStateLevel === true;
|
|
86
|
+
|
|
87
|
+
interface Edge {
|
|
88
|
+
callee: Definition;
|
|
89
|
+
// The configuration replaces the name by a constant value, which TLC's level and variable
|
|
90
|
+
// analyses take for a constant and its effect analysis evaluates.
|
|
91
|
+
isOverridden: boolean;
|
|
92
|
+
// Whether the reference is outside every action and temporal operator, where the level of the
|
|
93
|
+
// callee counts. An operator such as ENABLED fixes the level of what it holds.
|
|
94
|
+
isLevelCounted: boolean;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// The facts an expression's own nodes establish, with the definitions it refers to.
|
|
98
|
+
const collectLocalFacts = (
|
|
99
|
+
expressions: Expression[],
|
|
100
|
+
environment: DefinitionNames,
|
|
101
|
+
words: number,
|
|
102
|
+
) => {
|
|
103
|
+
const row = new Uint32Array(words);
|
|
104
|
+
const edges: Edge[] = [];
|
|
105
|
+
// The level flags of a node, or undefined for a node whose level the nodes it holds decide. An
|
|
106
|
+
// action or temporal operator has its own level whatever it holds, as does ENABLED, so nothing
|
|
107
|
+
// below one counts towards the level, variables and callees included.
|
|
108
|
+
const getOwnLevelFlags = (node: Expression) => {
|
|
109
|
+
const kind = node.kind;
|
|
110
|
+
if (ACTION_KINDS.has(kind) || (kind === "Apply" && node.operator === "\\cdot")) {
|
|
111
|
+
return ACTION_LEVEL_FLAGS;
|
|
112
|
+
}
|
|
113
|
+
if (kind === "Enabled") return VARIABLE_LEVEL_FLAGS;
|
|
114
|
+
if (
|
|
115
|
+
TEMPORAL_KINDS.has(kind) ||
|
|
116
|
+
(kind === "UnboundedQuantifier" && node.isTemporal === true) ||
|
|
117
|
+
(kind === "Apply" && TEMPORAL_OPERATORS.has(node.operator))
|
|
118
|
+
) {
|
|
119
|
+
return TEMPORAL_LEVEL_FLAGS;
|
|
120
|
+
}
|
|
121
|
+
return undefined;
|
|
122
|
+
};
|
|
123
|
+
const visit = (node: Expression, isLevelCounted: boolean) => {
|
|
124
|
+
const kind = node.kind;
|
|
125
|
+
if (
|
|
126
|
+
NONCONSTANT_KINDS.has(kind) ||
|
|
127
|
+
(kind === "UnboundedQuantifier" && node.isTemporal === true) ||
|
|
128
|
+
(kind === "Apply" && NONCONSTANT_OPERATORS.has(node.operator))
|
|
129
|
+
) {
|
|
130
|
+
row[0] |= NONCONSTANT;
|
|
131
|
+
}
|
|
132
|
+
if (NONCONSTANT_KINDS.has(kind)) row[0] |= READS_IMPURE;
|
|
133
|
+
const ownLevelFlags = getOwnLevelFlags(node);
|
|
134
|
+
if (isLevelCounted && ownLevelFlags !== undefined) row[0] |= ownLevelFlags;
|
|
135
|
+
const isInnerLevelCounted = isLevelCounted && ownLevelFlags === undefined;
|
|
31
136
|
const name = getReferencedName(node);
|
|
32
|
-
if (name !== undefined && !isLocallyResolved(node)
|
|
137
|
+
if (name !== undefined && !isLocallyResolved(node)) {
|
|
33
138
|
const target = environment.substitutions.get(name) ?? name;
|
|
34
139
|
const definition = environment.definitions.get(target);
|
|
35
|
-
|
|
140
|
+
const isOverridden = environment.constants.has(name);
|
|
36
141
|
const variableIndex = environment.variableIndex.get(name);
|
|
37
|
-
if (variableIndex !== undefined
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
142
|
+
if (variableIndex !== undefined || environment.builtins.get(name)?.isStateLevel) {
|
|
143
|
+
row[0] |= NONCONSTANT;
|
|
144
|
+
}
|
|
145
|
+
if (variableIndex !== undefined) {
|
|
146
|
+
if (isInnerLevelCounted) row[0] |= VARIABLE_LEVEL_FLAGS;
|
|
147
|
+
if (!isOverridden) row[1 + (variableIndex >>> 5)] |= 1 << (variableIndex & 31);
|
|
148
|
+
}
|
|
149
|
+
if (definition) {
|
|
150
|
+
edges.push({ callee: definition, isOverridden, isLevelCounted: isInnerLevelCounted });
|
|
151
|
+
} else {
|
|
152
|
+
if (isImpureBuiltin(name, environment)) {
|
|
153
|
+
row[0] |= IMPURE | (name === "Assert" ? 0 : REPEATED_EFFECT);
|
|
154
|
+
}
|
|
155
|
+
if (STREAMED_BUILTINS.has(name)) row[0] |= STREAMED;
|
|
156
|
+
if (!isOverridden && isImpureBuiltin(target, environment)) row[0] |= READS_IMPURE;
|
|
157
|
+
const builtin = environment.builtins.get(target);
|
|
158
|
+
if (
|
|
159
|
+
isInnerLevelCounted &&
|
|
160
|
+
!isOverridden &&
|
|
161
|
+
variableIndex === undefined &&
|
|
162
|
+
builtin?.isStateLevel &&
|
|
163
|
+
!builtin.isLevelConstant
|
|
164
|
+
) {
|
|
165
|
+
row[0] |= VARIABLE_LEVEL_FLAGS;
|
|
166
|
+
}
|
|
44
167
|
}
|
|
45
168
|
}
|
|
46
|
-
|
|
169
|
+
getChildren(node).forEach((child) => visit(child, isInnerLevelCounted));
|
|
47
170
|
};
|
|
48
|
-
|
|
171
|
+
expressions.forEach((expression) => visit(expression, true));
|
|
172
|
+
return { row, edges };
|
|
49
173
|
};
|
|
174
|
+
|
|
175
|
+
const getDefinitionExpressions = (definition: Definition) =>
|
|
176
|
+
definition.kind === "Function"
|
|
177
|
+
? [...definition.bounds.map((bound) => bound.set), definition.body]
|
|
178
|
+
: [definition.body];
|
|
179
|
+
|
|
180
|
+
// Closes each row under bitwise or along the edges: a definition's facts are its own and those of
|
|
181
|
+
// every definition it reaches. Tarjan's algorithm lists the strongly connected components with the
|
|
182
|
+
// callees first, so each component ors in components that are already final.
|
|
183
|
+
const closeRows = (local: Uint32Array, words: number, edges: Int32Array[], mask: number) => {
|
|
184
|
+
const count = edges.length;
|
|
185
|
+
const closed = local.slice();
|
|
186
|
+
const index = new Int32Array(count).fill(-1);
|
|
187
|
+
const lowLink = new Int32Array(count);
|
|
188
|
+
const component = new Int32Array(count).fill(-1);
|
|
189
|
+
const stack: number[] = [];
|
|
190
|
+
// The frame of a depth-first visit: the node and the edge it resumes at.
|
|
191
|
+
const frames: number[] = [];
|
|
192
|
+
let nextIndex = 0;
|
|
193
|
+
let componentCount = 0;
|
|
194
|
+
const orRow = (into: number, from: number) => {
|
|
195
|
+
for (let word = 0; word < words; word++) {
|
|
196
|
+
const bits = closed[from * words + word];
|
|
197
|
+
closed[into * words + word] |= word === 0 ? bits & mask : bits;
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
const enter = (node: number) => {
|
|
201
|
+
frames.push(node, 0);
|
|
202
|
+
index[node] = lowLink[node] = nextIndex++;
|
|
203
|
+
stack.push(node);
|
|
204
|
+
};
|
|
205
|
+
for (let root = 0; root < count; root++) {
|
|
206
|
+
if (index[root] !== -1) continue;
|
|
207
|
+
enter(root);
|
|
208
|
+
while (frames.length > 0) {
|
|
209
|
+
const node = frames[frames.length - 2];
|
|
210
|
+
const position = frames[frames.length - 1];
|
|
211
|
+
const nodeEdges = edges[node];
|
|
212
|
+
if (position < nodeEdges.length) {
|
|
213
|
+
frames[frames.length - 1] = position + 1;
|
|
214
|
+
const callee = nodeEdges[position];
|
|
215
|
+
if (index[callee] === -1) {
|
|
216
|
+
enter(callee);
|
|
217
|
+
} else if (component[callee] === -1) {
|
|
218
|
+
// A visited node without a component is still on the stack.
|
|
219
|
+
lowLink[node] = Math.min(lowLink[node], index[callee]);
|
|
220
|
+
}
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
frames.length -= 2;
|
|
224
|
+
if (frames.length > 0) {
|
|
225
|
+
const caller = frames[frames.length - 2];
|
|
226
|
+
lowLink[caller] = Math.min(lowLink[caller], lowLink[node]);
|
|
227
|
+
}
|
|
228
|
+
if (lowLink[node] !== index[node]) continue;
|
|
229
|
+
// The component is complete: its members share one row, which ors in their callees' rows.
|
|
230
|
+
const members: number[] = [];
|
|
231
|
+
for (;;) {
|
|
232
|
+
const member = stack.pop() ?? node;
|
|
233
|
+
component[member] = componentCount;
|
|
234
|
+
members.push(member);
|
|
235
|
+
if (member === node) break;
|
|
236
|
+
}
|
|
237
|
+
componentCount++;
|
|
238
|
+
const first = members[0];
|
|
239
|
+
members.forEach((member) => {
|
|
240
|
+
if (member !== first) orRow(first, member);
|
|
241
|
+
edges[member].forEach((callee) => {
|
|
242
|
+
if (component[callee] !== component[first]) orRow(first, callee);
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
members.forEach((member) => {
|
|
246
|
+
if (member !== first) orRow(member, first);
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return closed;
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const PURITY_MASK = IMPURE | REPEATED_EFFECT | STREAMED;
|
|
254
|
+
const READ_MASK = NONCONSTANT | READS_IMPURE;
|
|
255
|
+
|
|
256
|
+
// TLC evaluates a constant definition without parameters once, so only other definitions pass
|
|
257
|
+
// on the side effects and random choices of the builtins they call.
|
|
258
|
+
const isEvaluatedOnce = (definition: Definition, flags: number) =>
|
|
259
|
+
definition.kind === "Operator" && definition.params.length === 0 && (flags & NONCONSTANT) === 0;
|
|
260
|
+
|
|
261
|
+
export const createDefinitionFacts = (environment: DefinitionNames): DefinitionFacts => {
|
|
262
|
+
const definitions = [...new Set(environment.definitions.values())];
|
|
263
|
+
const ids = new Map(definitions.map((definition, id) => [definition, id]));
|
|
264
|
+
const words = 1 + ((environment.variables.length + 31) >>> 5);
|
|
265
|
+
const local = new Uint32Array(definitions.length * words);
|
|
266
|
+
const localImpurity = new Uint32Array(definitions.length * words);
|
|
267
|
+
const edges = definitions.map((definition, id) => {
|
|
268
|
+
const facts = collectLocalFacts(getDefinitionExpressions(definition), environment, words);
|
|
269
|
+
local.set(facts.row, id * words);
|
|
270
|
+
localImpurity[id * words] = facts.row[0] & PURITY_MASK;
|
|
271
|
+
return facts.edges;
|
|
272
|
+
});
|
|
273
|
+
const getId = (edge: Edge) => ids.get(edge.callee) ?? -1;
|
|
274
|
+
const readEdges = edges.map(
|
|
275
|
+
(own) => new Int32Array(own.filter((edge) => !edge.isOverridden).map(getId)),
|
|
276
|
+
);
|
|
277
|
+
const reads = closeRows(local, words, readEdges, READ_MASK);
|
|
278
|
+
const levelEdges = edges.map(
|
|
279
|
+
(own) =>
|
|
280
|
+
new Int32Array(own.filter((edge) => !edge.isOverridden && edge.isLevelCounted).map(getId)),
|
|
281
|
+
);
|
|
282
|
+
const levels = closeRows(local, words, levelEdges, TEMPORAL_LEVEL_FLAGS);
|
|
283
|
+
const purityEdges = edges.map(
|
|
284
|
+
(own) =>
|
|
285
|
+
new Int32Array(
|
|
286
|
+
own
|
|
287
|
+
.map(getId)
|
|
288
|
+
.filter((callee) => !isEvaluatedOnce(definitions[callee], reads[callee * words])),
|
|
289
|
+
),
|
|
290
|
+
);
|
|
291
|
+
const purity = closeRows(localImpurity, words, purityEdges, PURITY_MASK);
|
|
292
|
+
const rows = reads.map((word, position) =>
|
|
293
|
+
position % words === 0
|
|
294
|
+
? (word & READ_MASK) |
|
|
295
|
+
(levels[position] & TEMPORAL_LEVEL_FLAGS) |
|
|
296
|
+
(purity[position] & PURITY_MASK)
|
|
297
|
+
: word,
|
|
298
|
+
);
|
|
299
|
+
return { ids, rows, words };
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
// The facts of a definition: from the table, or for a LET definition from its own body and the
|
|
303
|
+
// table's rows of the definitions it calls.
|
|
304
|
+
const getRow = (definition: Definition, environment: Environment): Uint32Array => {
|
|
305
|
+
const facts = environment.facts;
|
|
306
|
+
const id = facts.ids.get(definition);
|
|
307
|
+
return id === undefined
|
|
308
|
+
? getExpressionsRow(getDefinitionExpressions(definition), environment)
|
|
309
|
+
: facts.rows.subarray(id * facts.words, (id + 1) * facts.words);
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
// The facts of expressions: their own, with the rows of the definitions they call ored in.
|
|
313
|
+
const getExpressionsRow = (expressions: Expression[], environment: Environment) => {
|
|
314
|
+
const words = environment.facts.words;
|
|
315
|
+
const local = collectLocalFacts(expressions, environment, words);
|
|
316
|
+
const row = local.row;
|
|
317
|
+
local.edges.forEach((edge) => {
|
|
318
|
+
const callee = getRow(edge.callee, environment);
|
|
319
|
+
if (!edge.isOverridden) {
|
|
320
|
+
row[0] |= callee[0] & READ_MASK;
|
|
321
|
+
if (edge.isLevelCounted) row[0] |= callee[0] & TEMPORAL_LEVEL_FLAGS;
|
|
322
|
+
for (let word = 1; word < words; word++) row[word] |= callee[word];
|
|
323
|
+
}
|
|
324
|
+
if (!isEvaluatedOnce(edge.callee, callee[0])) row[0] |= callee[0] & PURITY_MASK;
|
|
325
|
+
});
|
|
326
|
+
return row;
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
export const isConstantLevel = (definition: Definition, environment: Environment) =>
|
|
330
|
+
(getRow(definition, environment)[0] & NONCONSTANT) === 0;
|
|
331
|
+
|
|
332
|
+
const EFFECT_FLAGS = { any: IMPURE, repeated: REPEATED_EFFECT, streamed: STREAMED };
|
|
333
|
+
|
|
334
|
+
export const isImpureDefinition = (
|
|
335
|
+
definition: Definition,
|
|
336
|
+
environment: Environment,
|
|
337
|
+
effect: keyof typeof EFFECT_FLAGS = "any",
|
|
338
|
+
) => (getRow(definition, environment)[0] & EFFECT_FLAGS[effect]) !== 0;
|
|
339
|
+
|
|
340
|
+
// The liveness level of a definition: constant, variable, action or temporal.
|
|
341
|
+
export const getDefinitionLevel = (definition: Definition, environment: Environment) => {
|
|
342
|
+
const flags = getRow(definition, environment)[0] & TEMPORAL_LEVEL_FLAGS;
|
|
343
|
+
return flags === TEMPORAL_LEVEL_FLAGS
|
|
344
|
+
? TEMPORAL_LEVEL
|
|
345
|
+
: flags === ACTION_LEVEL_FLAGS
|
|
346
|
+
? ACTION_LEVEL
|
|
347
|
+
: flags === VARIABLE_LEVEL_FLAGS
|
|
348
|
+
? VARIABLE_LEVEL
|
|
349
|
+
: CONSTANT_LEVEL;
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
const toVariables = (row: Uint32Array) => {
|
|
353
|
+
const variables: number[] = [];
|
|
354
|
+
for (let word = 1; word < row.length; word++) {
|
|
355
|
+
for (let bits = row[word]; bits !== 0; bits &= bits - 1) {
|
|
356
|
+
variables.push(((word - 1) << 5) + (31 - Math.clz32(bits & -bits)));
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return variables;
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
// The variables an expression reads, through the definitions it calls, when they and its scope
|
|
363
|
+
// decide its value, or undefined when it has an effect or is an action.
|
|
364
|
+
export const getReadVariables = (
|
|
365
|
+
expression: Expression,
|
|
366
|
+
environment: Environment,
|
|
367
|
+
): number[] | undefined => {
|
|
368
|
+
const row = getExpressionsRow([expression], environment);
|
|
369
|
+
return row[0] & READS_IMPURE ? undefined : toVariables(row);
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
export const isNonconstant = (expression: Expression, environment: Environment) =>
|
|
373
|
+
(getExpressionsRow([expression], environment)[0] & NONCONSTANT) !== 0;
|
package/src/environment.ts
CHANGED
|
@@ -41,7 +41,7 @@ import {
|
|
|
41
41
|
withLetInstances,
|
|
42
42
|
} from "./instances";
|
|
43
43
|
import { overrideDefinitions, type JavaOverride } from "./java-overrides";
|
|
44
|
-
import { extractModule
|
|
44
|
+
import { extractModule } from "./parser";
|
|
45
45
|
import {
|
|
46
46
|
getArity,
|
|
47
47
|
getParamArities,
|
|
@@ -73,6 +73,7 @@ import { resolveInstanceSubexpressions, resolveModuleSubexpressions } from "./su
|
|
|
73
73
|
import { checkModuleLevels, createLevelAnalyzer } from "./levels";
|
|
74
74
|
import { createInstanceChecker, type InstanceCheck } from "./substitution-levels";
|
|
75
75
|
import { isEvalException } from "./tlc/messages";
|
|
76
|
+
import { createDefinitionFacts } from "./dependencies";
|
|
76
77
|
import { createTlcOrder, type TlcOrder } from "./tlc/order";
|
|
77
78
|
import {
|
|
78
79
|
isTlcInteger,
|
|
@@ -179,9 +180,9 @@ const getDeclarations = (linked: LinkedModule): Declaration[] => [
|
|
|
179
180
|
|
|
180
181
|
// Parses the module SANY reads from the file name.tla. A module SANY's parser rejects fails as it
|
|
181
182
|
// does in TLC: SANY's message, then a fatal error that names the module.
|
|
182
|
-
const parseModuleFile = (source: string, name: string) => {
|
|
183
|
+
const parseModuleFile = (source: string, name: string, order: TlcOrder) => {
|
|
183
184
|
try {
|
|
184
|
-
return parseModule(source);
|
|
185
|
+
return order.parseModule(source);
|
|
185
186
|
} catch (error) {
|
|
186
187
|
if (!(error instanceof TlaSyntaxError)) throw error;
|
|
187
188
|
const parseError = getSanyParseError(source);
|
|
@@ -302,7 +303,7 @@ export const createEnvironment = (
|
|
|
302
303
|
options: EnvironmentOptions,
|
|
303
304
|
): Specification => {
|
|
304
305
|
const order = options.order ?? createTlcOrder(options.source, options.loadModule);
|
|
305
|
-
const root = order.
|
|
306
|
+
const root = order.parseModule(options.source);
|
|
306
307
|
const definitions = new Map<string, Definition>();
|
|
307
308
|
const parameterized = new Map<string, boolean>();
|
|
308
309
|
// The definition each instance copy came from, and the instance parameters the copy adds.
|
|
@@ -420,7 +421,7 @@ export const createEnvironment = (
|
|
|
420
421
|
BUNDLED_MODULES.get(name) ??
|
|
421
422
|
extractModule(options.source, name);
|
|
422
423
|
if (source === undefined) throw createMissingModuleError(name, importer);
|
|
423
|
-
const module = parseModuleFile(source, name);
|
|
424
|
+
const module = parseModuleFile(source, name, order);
|
|
424
425
|
if (module.name !== name) {
|
|
425
426
|
throw createSanyError(
|
|
426
427
|
[
|
|
@@ -1239,6 +1240,18 @@ export const createEnvironment = (
|
|
|
1239
1240
|
});
|
|
1240
1241
|
}
|
|
1241
1242
|
|
|
1243
|
+
const variableIndex = getVariableIndex(
|
|
1244
|
+
variables,
|
|
1245
|
+
linkOrder.flatMap((linked) => linked.module.variables.map((variable) => variable.name)),
|
|
1246
|
+
);
|
|
1247
|
+
const facts = createDefinitionFacts({
|
|
1248
|
+
definitions,
|
|
1249
|
+
substitutions,
|
|
1250
|
+
constants,
|
|
1251
|
+
variableIndex,
|
|
1252
|
+
builtins,
|
|
1253
|
+
variables,
|
|
1254
|
+
});
|
|
1242
1255
|
const environment: Environment = {
|
|
1243
1256
|
runtime,
|
|
1244
1257
|
definitions,
|
|
@@ -1247,14 +1260,13 @@ export const createEnvironment = (
|
|
|
1247
1260
|
substitutions,
|
|
1248
1261
|
instanceNames,
|
|
1249
1262
|
variables,
|
|
1250
|
-
variableIndex
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
),
|
|
1254
|
-
definitionValues: new Map(),
|
|
1255
|
-
constantLevels: new Map(),
|
|
1263
|
+
variableIndex,
|
|
1264
|
+
facts,
|
|
1265
|
+
startupValues: Array.from({ length: facts.ids.size }, (): Value | undefined => undefined),
|
|
1256
1266
|
substitutedDefinitions,
|
|
1257
1267
|
evaluators: new WeakMap(),
|
|
1268
|
+
entryEvaluators: new WeakMap(),
|
|
1269
|
+
definitionEvaluators: new WeakMap(),
|
|
1258
1270
|
};
|
|
1259
1271
|
// TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
|
|
1260
1272
|
// computes is above constant. The bound counts the operators that TLC's Java code overrides with
|