tla-ts 0.0.8 → 0.0.9-dev.4ce0657
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 +23 -0
- package/dist/compile.cjs +23 -0
- package/dist/compile.js +23 -0
- package/dist/deciders.cjs +15 -15
- package/dist/deciders.js +48 -48
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +27 -9
- package/dist/index.d.ts +27 -9
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/prove.cjs +17 -17
- package/dist/prove.js +32 -32
- package/dist/run.cjs +28 -28
- package/dist/run.js +31 -31
- package/dist/tlapm.cjs +2 -2
- package/dist/tlapm.js +1 -1
- package/dist/transfer.cjs +30 -33
- package/dist/transfer.js +30 -33
- package/dist/worker.cjs +4 -4
- package/dist/worker.js +4 -4
- package/package.json +3 -1
- package/src/check.ts +77 -46
- package/src/cli/options.ts +2 -1
- package/src/cli/tlc.ts +26 -8
- package/src/cli/typescript.ts +26 -0
- package/src/codegen.ts +0 -5
- package/src/dependencies.ts +348 -24
- package/src/environment.ts +27 -11
- package/src/errors.ts +15 -0
- package/src/evaluate.ts +188 -712
- package/src/instances.ts +67 -6
- package/src/lexer.ts +140 -51
- package/src/liveness-checker.ts +6 -3
- package/src/liveness.ts +8 -18
- package/src/parallel-worker.ts +6 -3
- package/src/parallel.ts +15 -7
- package/src/prover/smt/solver/solver.ts +31 -27
- 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 +6 -12
- 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/typescript/actions.ts +494 -0
- package/src/typescript/collections.ts +615 -0
- package/src/typescript/compile.ts +70 -0
- package/src/typescript/context.ts +106 -0
- package/src/typescript/emit.ts +122 -0
- package/src/typescript/expressions.ts +508 -0
- package/src/typescript/formula.ts +160 -0
- package/src/typescript/functions.ts +295 -0
- package/src/typescript/model.ts +164 -0
- package/src/typescript/names.ts +116 -0
- package/src/typescript/program.ts +51 -0
- package/src/typescript/properties.ts +141 -0
- package/src/typescript/scope.ts +117 -0
- package/src/typescript/steps.ts +175 -0
- package/src/typescript/types.ts +178 -0
- package/src/values.ts +126 -82
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
|
[
|
|
@@ -530,6 +531,8 @@ export const createEnvironment = (
|
|
|
530
531
|
),
|
|
531
532
|
variables: new Set(linked.variables),
|
|
532
533
|
getDefinition: (key) => definitions.get(key),
|
|
534
|
+
getOuterDefinition: (name) =>
|
|
535
|
+
module.definitions.find((definition) => definition.name === name),
|
|
533
536
|
isParameterized: (key) => parameterized.get(key) === true,
|
|
534
537
|
addDefinition: (source, name, create) => {
|
|
535
538
|
const key = createName(`${name}@${module.name}`);
|
|
@@ -741,6 +744,8 @@ export const createEnvironment = (
|
|
|
741
744
|
),
|
|
742
745
|
variables: new Set(linked.variables),
|
|
743
746
|
getDefinition: (key) => definitions.get(key),
|
|
747
|
+
// Each substitution is a local definition, which the copies read by name.
|
|
748
|
+
getOuterDefinition: () => undefined,
|
|
744
749
|
isParameterized: (key) => parameterized.get(key) === true,
|
|
745
750
|
addDefinition: (_source, name, create) => {
|
|
746
751
|
const key = `${name}${KEY_SEPARATOR}${instance.module}${suffix}.${copies.length}`;
|
|
@@ -1239,6 +1244,18 @@ export const createEnvironment = (
|
|
|
1239
1244
|
});
|
|
1240
1245
|
}
|
|
1241
1246
|
|
|
1247
|
+
const variableIndex = getVariableIndex(
|
|
1248
|
+
variables,
|
|
1249
|
+
linkOrder.flatMap((linked) => linked.module.variables.map((variable) => variable.name)),
|
|
1250
|
+
);
|
|
1251
|
+
const facts = createDefinitionFacts({
|
|
1252
|
+
definitions,
|
|
1253
|
+
substitutions,
|
|
1254
|
+
constants,
|
|
1255
|
+
variableIndex,
|
|
1256
|
+
builtins,
|
|
1257
|
+
variables,
|
|
1258
|
+
});
|
|
1242
1259
|
const environment: Environment = {
|
|
1243
1260
|
runtime,
|
|
1244
1261
|
definitions,
|
|
@@ -1247,14 +1264,13 @@ export const createEnvironment = (
|
|
|
1247
1264
|
substitutions,
|
|
1248
1265
|
instanceNames,
|
|
1249
1266
|
variables,
|
|
1250
|
-
variableIndex
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
),
|
|
1254
|
-
definitionValues: new Map(),
|
|
1255
|
-
constantLevels: new Map(),
|
|
1267
|
+
variableIndex,
|
|
1268
|
+
facts,
|
|
1269
|
+
startupValues: Array.from({ length: facts.ids.size }, (): Value | undefined => undefined),
|
|
1256
1270
|
substitutedDefinitions,
|
|
1257
1271
|
evaluators: new WeakMap(),
|
|
1272
|
+
entryEvaluators: new WeakMap(),
|
|
1273
|
+
definitionEvaluators: new WeakMap(),
|
|
1258
1274
|
};
|
|
1259
1275
|
// TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
|
|
1260
1276
|
// computes is above constant. The bound counts the operators that TLC's Java code overrides with
|
package/src/errors.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type ts from "@typescript/typescript6";
|
|
1
2
|
import type { SourceRange } from "./ast";
|
|
2
3
|
import { EXIT_STATUS, getExitCode, setExitCode } from "./exit-codes";
|
|
3
4
|
import type { PcalNode } from "./pcal/ast";
|
|
@@ -373,3 +374,17 @@ export class PcalBugError extends TlaError {
|
|
|
373
374
|
this.name = "PcalBugError";
|
|
374
375
|
}
|
|
375
376
|
}
|
|
377
|
+
|
|
378
|
+
// TypeScript that tla-ts can't compile to TLA+, at the file, line and column of the construct.
|
|
379
|
+
export class TlaTypeScriptError extends TlaError {
|
|
380
|
+
constructor(message: string, node?: ts.Node) {
|
|
381
|
+
const sourceFile = node?.getSourceFile();
|
|
382
|
+
const position = sourceFile?.getLineAndCharacterOfPosition(node?.getStart() ?? 0);
|
|
383
|
+
super(
|
|
384
|
+
sourceFile === undefined || position === undefined
|
|
385
|
+
? message
|
|
386
|
+
: `${sourceFile.fileName}:${position.line + 1}:${position.character + 1} ${message}`,
|
|
387
|
+
);
|
|
388
|
+
this.name = "TlaTypeScriptError";
|
|
389
|
+
}
|
|
390
|
+
}
|