tla-ts 0.0.3-dev.c949d82 → 0.0.3-dev.ccbff8c
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/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +167 -36
- package/dist/index.d.ts +167 -36
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +1351 -21
- package/dist/run-cli.js +1351 -21
- package/dist/transfer.cjs +83 -34
- package/dist/transfer.js +83 -34
- package/package.json +5 -1
- package/src/ast.ts +55 -7
- package/src/bags.ts +4 -7
- package/src/builtins.ts +29 -11
- package/src/bundled-modules.ts +3 -3
- package/src/canonical.ts +4 -3
- package/src/check.ts +1344 -285
- package/src/cli.ts +8 -1
- package/src/codegen.ts +68 -21
- package/src/config.ts +225 -206
- package/src/environment.ts +423 -127
- package/src/errors.ts +168 -9
- package/src/evaluate.ts +722 -146
- package/src/exit-codes.ts +6 -24
- package/src/index.ts +1 -0
- package/src/instances.ts +4 -2
- package/src/java-overrides.ts +104 -25
- package/src/levels.ts +183 -37
- package/src/lexer.ts +32 -4
- package/src/live-check.ts +204 -148
- package/src/liveness.ts +190 -117
- package/src/obligation-expression.ts +2 -2
- package/src/obligation-source.ts +2 -0
- package/src/parallel-worker.ts +6 -1
- package/src/parallel.ts +57 -6
- package/src/parser.ts +412 -139
- package/src/pcal-ast.ts +770 -0
- package/src/pcal-parse.ts +1523 -0
- package/src/pcal-tokenize.ts +516 -0
- package/src/pcal-translate.ts +1230 -0
- package/src/pcal-validate.ts +454 -0
- package/src/randomization.ts +24 -7
- package/src/resolve.ts +1405 -416
- package/src/run-cli.ts +1167 -174
- package/src/runtime.ts +16 -0
- package/src/sany-builtins.ts +147 -0
- package/src/sany-generator.ts +3112 -0
- package/src/sany-levels.ts +1208 -0
- package/src/sany-lexer.ts +545 -0
- package/src/sany-linter.ts +122 -0
- package/src/sany-modules.ts +1398 -0
- package/src/sany-nodes.ts +463 -0
- package/src/sany-parse-error.ts +2161 -0
- package/src/sany-report.ts +354 -0
- package/src/sany-selectors.ts +738 -0
- package/src/sany-source.ts +248 -0
- package/src/sany-xml.ts +1017 -0
- package/src/spec-description.ts +25 -5
- package/src/standard-modules.ts +203 -146
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +29 -6
- package/src/subexpressions.ts +221 -161
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +158 -120
- package/src/symmetry.ts +168 -50
- package/src/tableau.ts +3 -1
- package/src/tlc-ext.ts +13 -17
- package/src/tlc-messages.ts +329 -0
- package/src/tlc-module.ts +49 -21
- package/src/tlc-modules.ts +0 -4
- package/src/tlc-order.ts +55 -9
- package/src/tlc-random.ts +28 -3
- package/src/tlc-replay.ts +655 -0
- package/src/tlc-version.ts +18 -0
- package/src/transfer.ts +15 -3
- package/src/value-sources.ts +29 -0
- package/src/values.ts +683 -256
|
@@ -0,0 +1,655 @@
|
|
|
1
|
+
import type { Expression, SourceLocation } from "./ast";
|
|
2
|
+
import { TlaEvaluationError } from "./errors";
|
|
3
|
+
import {
|
|
4
|
+
bindDefinitions,
|
|
5
|
+
bindLazyArguments,
|
|
6
|
+
bindOperatorArguments,
|
|
7
|
+
evaluate,
|
|
8
|
+
forEachBinding,
|
|
9
|
+
freezeCallStack,
|
|
10
|
+
getLazyFunction,
|
|
11
|
+
toKeepLazyContext,
|
|
12
|
+
isConstantLevel,
|
|
13
|
+
lookupScope,
|
|
14
|
+
resolveApplyOperator,
|
|
15
|
+
resolveGlobalOperator,
|
|
16
|
+
type CallStack,
|
|
17
|
+
type Context,
|
|
18
|
+
type Environment,
|
|
19
|
+
type Scope,
|
|
20
|
+
type State,
|
|
21
|
+
toUnspecifiedSubscriptError,
|
|
22
|
+
} from "./evaluate";
|
|
23
|
+
import { getStreamedElements, isTlcEnumerable, setTrace, valuesEqual, type Value } from "./values";
|
|
24
|
+
|
|
25
|
+
// When TLC fails to compute the initial states or the successors of a state, it computes them
|
|
26
|
+
// again with its CallStackTool, which records each node it visits, and prints where the failure
|
|
27
|
+
// occurred. The replay below walks the predicates as TLC's Tool.getInitStates and
|
|
28
|
+
// Tool.getNextStates do, in their order and with their nesting, so that it records the nodes TLC
|
|
29
|
+
// records. It evaluates the other expressions with an environment that records the nodes too.
|
|
30
|
+
|
|
31
|
+
export interface ReplayAction {
|
|
32
|
+
expression: Expression;
|
|
33
|
+
scope: Scope | undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// TLC's ActionItemList: the conjuncts left to process, the first at the head.
|
|
37
|
+
interface Item {
|
|
38
|
+
kind: "Predicate" | "Unchanged" | "Changed";
|
|
39
|
+
expression: Expression;
|
|
40
|
+
scope: Scope | undefined;
|
|
41
|
+
rest: Item | undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface Replay {
|
|
45
|
+
environment: Environment;
|
|
46
|
+
callStack: CallStack;
|
|
47
|
+
isInit: boolean;
|
|
48
|
+
// Whether the replay computes ENABLED rather than the successors of the state.
|
|
49
|
+
isEnabled: boolean;
|
|
50
|
+
// The state whose successors the replay computes.
|
|
51
|
+
current: Value[] | undefined;
|
|
52
|
+
onState: (state: Value[]) => void;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const withFrame = <Result>(replay: Replay, expression: Expression, run: () => Result): Result => {
|
|
56
|
+
const callStack = replay.callStack;
|
|
57
|
+
callStack.expressions.push(expression);
|
|
58
|
+
const outerSets = setTrace.sets;
|
|
59
|
+
const sets = outerSets ?? [];
|
|
60
|
+
const depth = sets.length;
|
|
61
|
+
setTrace.sets = sets;
|
|
62
|
+
try {
|
|
63
|
+
return run();
|
|
64
|
+
} catch (error) {
|
|
65
|
+
// A quantifier enumerates its bounds outside any evaluation.
|
|
66
|
+
freezeCallStack(callStack, sets.slice(depth), error);
|
|
67
|
+
throw error;
|
|
68
|
+
} finally {
|
|
69
|
+
if (!callStack.isFrozen) callStack.expressions.pop();
|
|
70
|
+
sets.length = depth;
|
|
71
|
+
setTrace.sets = outerSets;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const getContext = (replay: Replay, scope: Scope | undefined, partial: State): Context =>
|
|
76
|
+
replay.isInit
|
|
77
|
+
? { environment: replay.environment, scope, state: partial, next: undefined }
|
|
78
|
+
: { environment: replay.environment, scope, state: replay.current, next: partial };
|
|
79
|
+
|
|
80
|
+
const isComplete = (partial: State): partial is Value[] => !partial.includes(undefined);
|
|
81
|
+
|
|
82
|
+
const getEmptyState = (environment: Environment) => environment.variables.map(() => undefined);
|
|
83
|
+
|
|
84
|
+
const assign = (partial: State, index: number, value: Value) => {
|
|
85
|
+
const copy = [...partial];
|
|
86
|
+
copy[index] = value;
|
|
87
|
+
return copy;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// SANY parses an infix chain such as A /\ B /\ C as (A /\ B) /\ C, where tla-ts keeps one
|
|
91
|
+
// junction, so the replay recreates the node of each prefix.
|
|
92
|
+
const getPrefix = (expression: Expression & { kind: "And" | "Or" }, count: number): Expression => {
|
|
93
|
+
const first = expression.operands[0].location;
|
|
94
|
+
const range = expression.location.range;
|
|
95
|
+
const end = expression.prefixEnds?.[count - 2];
|
|
96
|
+
const location: SourceLocation =
|
|
97
|
+
range && end ? { ...first, range: { ...range, end } } : expression.location;
|
|
98
|
+
return { ...expression, operands: expression.operands.slice(0, count), location };
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const getOperatorName = (expression: Expression) =>
|
|
102
|
+
expression.kind === "Identifier" ? expression.name : undefined;
|
|
103
|
+
|
|
104
|
+
// The state variable an expression names, as TLC's getVar finds it through definitions and
|
|
105
|
+
// operator arguments.
|
|
106
|
+
const getVariable = (
|
|
107
|
+
replay: Replay,
|
|
108
|
+
expression: Expression,
|
|
109
|
+
scope: Scope | undefined,
|
|
110
|
+
): number | undefined => {
|
|
111
|
+
if (expression.kind === "Label") return getVariable(replay, expression.body, scope);
|
|
112
|
+
const name = getOperatorName(expression);
|
|
113
|
+
if (name === undefined) return undefined;
|
|
114
|
+
const binding = lookupScope(scope, name);
|
|
115
|
+
if (binding?.kind === "Thunk") return getVariable(replay, binding.expression, binding.scope);
|
|
116
|
+
if (binding) return undefined;
|
|
117
|
+
const index = replay.environment.variableIndex.get(name);
|
|
118
|
+
if (index !== undefined) return index;
|
|
119
|
+
const operator = resolveGlobalOperator(name, replay.environment);
|
|
120
|
+
return operator?.kind === "Closure" && operator.params.length === 0
|
|
121
|
+
? getVariable(replay, operator.body, operator.scope)
|
|
122
|
+
: undefined;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
// The variable a primed expression such as x' assigns, as TLC's getPrimedVar finds it.
|
|
126
|
+
const getPrimedVariable = (
|
|
127
|
+
replay: Replay,
|
|
128
|
+
expression: Expression,
|
|
129
|
+
scope: Scope | undefined,
|
|
130
|
+
): number | undefined => {
|
|
131
|
+
if (expression.kind === "Prime") return getVariable(replay, expression.expression, scope);
|
|
132
|
+
if (expression.kind === "Label") return getPrimedVariable(replay, expression.body, scope);
|
|
133
|
+
const name = getOperatorName(expression);
|
|
134
|
+
if (name === undefined) return undefined;
|
|
135
|
+
const binding = lookupScope(scope, name);
|
|
136
|
+
if (binding?.kind === "Thunk") {
|
|
137
|
+
return getPrimedVariable(replay, binding.expression, binding.scope);
|
|
138
|
+
}
|
|
139
|
+
if (binding) return undefined;
|
|
140
|
+
const operator = resolveGlobalOperator(name, replay.environment);
|
|
141
|
+
return operator?.kind === "Closure" && operator.params.length === 0
|
|
142
|
+
? getPrimedVariable(replay, operator.body, operator.scope)
|
|
143
|
+
: undefined;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const isTrue = (value: Value) => {
|
|
147
|
+
if (typeof value !== "boolean")
|
|
148
|
+
throw new TlaEvaluationError("TLC expected a boolean expression.");
|
|
149
|
+
return value;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
// Once every variable has a value, TLC's getInitStates and getNextStatesAllAssigned evaluate the
|
|
153
|
+
// conjuncts left instead of walking them, so a disjunction among them takes one step. Its enabled
|
|
154
|
+
// doesn't, and Tool#getNextStates0 takes this shortcut only with warnings on.
|
|
155
|
+
export const isAssignedShortcut = (replay: Pick<Replay, "environment" | "isInit" | "isEnabled">) =>
|
|
156
|
+
!replay.isEnabled && (replay.isInit || replay.environment.runtime.isWarningEnabled);
|
|
157
|
+
|
|
158
|
+
// TLC's getInitStates(ActionItemList) and getNextStates(ActionItemList): processes the conjuncts
|
|
159
|
+
// left once a conjunct holds.
|
|
160
|
+
const proceed = (replay: Replay, items: Item | undefined, partial: State): void => {
|
|
161
|
+
if (items === undefined) {
|
|
162
|
+
if (isComplete(partial)) replay.onState(partial);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (isComplete(partial) && isAssignedShortcut(replay)) {
|
|
166
|
+
proceedAssigned(replay, items, partial);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (items.kind === "Predicate") {
|
|
170
|
+
visit(replay, items.expression, items.rest, items.scope, partial);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
if (items.kind === "Unchanged") {
|
|
174
|
+
visitUnchanged(replay, items.expression, items.rest, items.scope, partial);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (isChanged(replay, items, partial)) proceed(replay, items.rest, partial);
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const isChanged = (replay: Replay, item: Item, partial: State) => {
|
|
181
|
+
const environment = replay.environment;
|
|
182
|
+
const before = evaluate(item.expression, {
|
|
183
|
+
environment,
|
|
184
|
+
scope: item.scope,
|
|
185
|
+
state: replay.current,
|
|
186
|
+
next: undefined,
|
|
187
|
+
});
|
|
188
|
+
const after = (() => {
|
|
189
|
+
try {
|
|
190
|
+
return evaluate(item.expression, {
|
|
191
|
+
environment,
|
|
192
|
+
scope: item.scope,
|
|
193
|
+
state: partial,
|
|
194
|
+
next: undefined,
|
|
195
|
+
});
|
|
196
|
+
} catch (error) {
|
|
197
|
+
throw replay.isEnabled
|
|
198
|
+
? toUnspecifiedSubscriptError(error, environment.runtime.version)
|
|
199
|
+
: error;
|
|
200
|
+
}
|
|
201
|
+
})();
|
|
202
|
+
return !valuesEqual(before, after);
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
// Once every variable has a value, TLC evaluates the conjuncts left instead of walking them.
|
|
206
|
+
const proceedAssigned = (replay: Replay, items: Item | undefined, partial: Value[]): void => {
|
|
207
|
+
for (let item = items; item !== undefined; item = item.rest) {
|
|
208
|
+
if (item.kind === "Unchanged") {
|
|
209
|
+
visitUnchanged(replay, item.expression, item.rest, item.scope, partial);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (
|
|
213
|
+
item.kind === "Changed"
|
|
214
|
+
? !isChanged(replay, item, partial)
|
|
215
|
+
: !isTrue(evaluate(item.expression, getContext(replay, item.scope, partial)))
|
|
216
|
+
) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
replay.onState(partial);
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
const visit = (
|
|
224
|
+
replay: Replay,
|
|
225
|
+
expression: Expression,
|
|
226
|
+
items: Item | undefined,
|
|
227
|
+
scope: Scope | undefined,
|
|
228
|
+
partial: State,
|
|
229
|
+
): void =>
|
|
230
|
+
withFrame(replay, expression, () => {
|
|
231
|
+
switch (expression.kind) {
|
|
232
|
+
case "Let":
|
|
233
|
+
visit(
|
|
234
|
+
replay,
|
|
235
|
+
expression.body,
|
|
236
|
+
items,
|
|
237
|
+
bindDefinitions(expression.definitions, getContext(replay, scope, partial)),
|
|
238
|
+
partial,
|
|
239
|
+
);
|
|
240
|
+
return;
|
|
241
|
+
case "Label":
|
|
242
|
+
visit(replay, expression.body, items, scope, partial);
|
|
243
|
+
return;
|
|
244
|
+
default:
|
|
245
|
+
if (LITERAL_KINDS.has(expression.kind))
|
|
246
|
+
throw new TlaEvaluationError("The state relation is not a boolean expression.");
|
|
247
|
+
withFrame(replay, expression, () =>
|
|
248
|
+
visitApplication(replay, expression, items, scope, partial),
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
// The value of a user-defined operator TLC evaluates instead of walking its body.
|
|
254
|
+
const visitValue = (replay: Replay, value: Value, items: Item | undefined, partial: State) => {
|
|
255
|
+
if (isTrue(value)) proceed(replay, items, partial);
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
const visitDefined = (
|
|
259
|
+
replay: Replay,
|
|
260
|
+
expression: Expression & { kind: "Identifier" | "Apply" },
|
|
261
|
+
items: Item | undefined,
|
|
262
|
+
scope: Scope | undefined,
|
|
263
|
+
partial: State,
|
|
264
|
+
) => {
|
|
265
|
+
const context = getContext(replay, scope, partial);
|
|
266
|
+
if (expression.kind === "Identifier") {
|
|
267
|
+
const binding = lookupScope(scope, expression.name);
|
|
268
|
+
if (binding?.kind === "Thunk" && binding.cache === undefined) {
|
|
269
|
+
visit(replay, binding.expression, items, binding.scope, partial);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
if (
|
|
273
|
+
binding?.kind === "Operator" &&
|
|
274
|
+
binding.operator.kind === "Closure" &&
|
|
275
|
+
binding.operator.params.length === 0
|
|
276
|
+
) {
|
|
277
|
+
visit(replay, binding.operator.body, items, binding.operator.scope, partial);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
const definition = binding ? undefined : replay.environment.definitions.get(expression.name);
|
|
281
|
+
const operator = binding
|
|
282
|
+
? undefined
|
|
283
|
+
: resolveGlobalOperator(expression.name, replay.environment);
|
|
284
|
+
if (
|
|
285
|
+
operator?.kind === "Closure" &&
|
|
286
|
+
operator.params.length === 0 &&
|
|
287
|
+
!replay.environment.variableIndex.has(expression.name) &&
|
|
288
|
+
!(definition && isConstantLevel(definition, replay.environment))
|
|
289
|
+
) {
|
|
290
|
+
visit(replay, operator.body, items, operator.scope, partial);
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
visitValue(replay, evaluate(expression, context), items, partial);
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
const operator = resolveApplyOperator(expression, context);
|
|
297
|
+
if (operator?.kind === "Closure" && operator.params.length === expression.args.length) {
|
|
298
|
+
visit(
|
|
299
|
+
replay,
|
|
300
|
+
operator.body,
|
|
301
|
+
items,
|
|
302
|
+
bindOperatorArguments(operator, expression.args, context),
|
|
303
|
+
partial,
|
|
304
|
+
);
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
visitValue(replay, evaluate(expression, context), items, partial);
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
// A variable takes the value when it has none yet, and otherwise must already have it.
|
|
311
|
+
const proceedWithValue = (
|
|
312
|
+
replay: Replay,
|
|
313
|
+
items: Item | undefined,
|
|
314
|
+
partial: State,
|
|
315
|
+
index: number,
|
|
316
|
+
value: Value,
|
|
317
|
+
) => {
|
|
318
|
+
const assigned = partial[index];
|
|
319
|
+
if (assigned === undefined) {
|
|
320
|
+
proceed(replay, items, assign(partial, index, value));
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
if (valuesEqual(assigned, value)) proceed(replay, items, partial);
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
const cons = (
|
|
327
|
+
kind: Item["kind"],
|
|
328
|
+
expression: Expression,
|
|
329
|
+
scope: Scope | undefined,
|
|
330
|
+
rest: Item | undefined,
|
|
331
|
+
): Item => ({ kind, expression, scope, rest });
|
|
332
|
+
|
|
333
|
+
const visitAssignment = (
|
|
334
|
+
replay: Replay,
|
|
335
|
+
expression: Expression & { kind: "Apply" },
|
|
336
|
+
items: Item | undefined,
|
|
337
|
+
scope: Scope | undefined,
|
|
338
|
+
partial: State,
|
|
339
|
+
) => {
|
|
340
|
+
const [target, source] = expression.args;
|
|
341
|
+
const index = replay.isInit
|
|
342
|
+
? getVariable(replay, target, scope)
|
|
343
|
+
: getPrimedVariable(replay, target, scope);
|
|
344
|
+
const context = getContext(replay, scope, partial);
|
|
345
|
+
if (index === undefined) {
|
|
346
|
+
if (evaluate(expression, context) === true) proceed(replay, items, partial);
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
const value = evaluate(source, context);
|
|
350
|
+
const assigned = partial[index];
|
|
351
|
+
if (expression.operator === "=") {
|
|
352
|
+
proceedWithValue(replay, items, partial, index, value);
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
const set: Value = expression.operator === "\\subseteq" ? { kind: "Subset", base: value } : value;
|
|
356
|
+
if (assigned !== undefined) {
|
|
357
|
+
if (evaluate(expression, context) === true) proceed(replay, items, partial);
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
if (!isTlcEnumerable(set))
|
|
361
|
+
throw new TlaEvaluationError("The right side of \\IN is not enumerable.");
|
|
362
|
+
for (const element of getStreamedElements(set)) {
|
|
363
|
+
proceed(replay, items, assign(partial, index, element));
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
const ASSIGNMENT_OPERATORS = new Set(["=", "\\in", "\\subseteq"]);
|
|
368
|
+
|
|
369
|
+
export const LITERAL_KINDS = new Set<Expression["kind"]>(["Number", "Decimal", "String"]);
|
|
370
|
+
|
|
371
|
+
const visitApplication = (
|
|
372
|
+
replay: Replay,
|
|
373
|
+
expression: Expression,
|
|
374
|
+
items: Item | undefined,
|
|
375
|
+
scope: Scope | undefined,
|
|
376
|
+
partial: State,
|
|
377
|
+
): void => {
|
|
378
|
+
const context = getContext(replay, scope, partial);
|
|
379
|
+
switch (expression.kind) {
|
|
380
|
+
case "Identifier":
|
|
381
|
+
visitDefined(replay, expression, items, scope, partial);
|
|
382
|
+
return;
|
|
383
|
+
case "Apply": {
|
|
384
|
+
if (ASSIGNMENT_OPERATORS.has(expression.operator) && expression.args.length === 2) {
|
|
385
|
+
visitAssignment(replay, expression, items, scope, partial);
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
if (expression.operator === "=>" && expression.args.length === 2) {
|
|
389
|
+
if (isTrue(evaluate(expression.args[0], context))) {
|
|
390
|
+
visit(replay, expression.args[1], items, scope, partial);
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
proceed(replay, items, partial);
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
visitDefined(replay, expression, items, scope, partial);
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
case "And": {
|
|
400
|
+
const operands = expression.operands;
|
|
401
|
+
if (expression.isBulleted || operands.length === 2) {
|
|
402
|
+
const rest = operands
|
|
403
|
+
.slice(1)
|
|
404
|
+
.reduceRight<Item | undefined>(
|
|
405
|
+
(tail, operand) => cons("Predicate", operand, scope, tail),
|
|
406
|
+
items,
|
|
407
|
+
);
|
|
408
|
+
visit(replay, operands[0], rest, scope, partial);
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
const last = operands[operands.length - 1];
|
|
412
|
+
visit(
|
|
413
|
+
replay,
|
|
414
|
+
getPrefix(expression, operands.length - 1),
|
|
415
|
+
cons("Predicate", last, scope, items),
|
|
416
|
+
scope,
|
|
417
|
+
partial,
|
|
418
|
+
);
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
case "Or": {
|
|
422
|
+
const operands = expression.operands;
|
|
423
|
+
if (expression.isBulleted || operands.length === 2) {
|
|
424
|
+
operands.forEach((operand) => visit(replay, operand, items, scope, partial));
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
visit(replay, getPrefix(expression, operands.length - 1), items, scope, partial);
|
|
428
|
+
visit(replay, operands[operands.length - 1], items, scope, partial);
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
case "Exists":
|
|
432
|
+
forEachBinding(expression.bounds, context, (bound) => {
|
|
433
|
+
visit(replay, expression.body, items, bound, partial);
|
|
434
|
+
return false;
|
|
435
|
+
});
|
|
436
|
+
return;
|
|
437
|
+
case "Forall": {
|
|
438
|
+
const bounds: (Scope | undefined)[] = [];
|
|
439
|
+
forEachBinding(expression.bounds, context, (bound) => {
|
|
440
|
+
bounds.push(bound);
|
|
441
|
+
return false;
|
|
442
|
+
});
|
|
443
|
+
if (bounds.length === 0) {
|
|
444
|
+
proceed(replay, items, partial);
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
// TLC adds each binding after the first to the front of the conjuncts left.
|
|
448
|
+
const rest = bounds
|
|
449
|
+
.slice(1)
|
|
450
|
+
.reduce<Item | undefined>(
|
|
451
|
+
(tail, bound) => cons("Predicate", expression.body, bound, tail),
|
|
452
|
+
items,
|
|
453
|
+
);
|
|
454
|
+
visit(replay, expression.body, rest, bounds[0], partial);
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
case "If":
|
|
458
|
+
visit(
|
|
459
|
+
replay,
|
|
460
|
+
isTrue(evaluate(expression.condition, context))
|
|
461
|
+
? expression.consequent
|
|
462
|
+
: expression.alternative,
|
|
463
|
+
items,
|
|
464
|
+
scope,
|
|
465
|
+
partial,
|
|
466
|
+
);
|
|
467
|
+
return;
|
|
468
|
+
case "Case": {
|
|
469
|
+
const arm = expression.arms.find((candidate) =>
|
|
470
|
+
isTrue(evaluate(candidate.condition, context)),
|
|
471
|
+
);
|
|
472
|
+
const body = arm?.body ?? expression.other;
|
|
473
|
+
if (body === undefined) {
|
|
474
|
+
throw new TlaEvaluationError("TLC encountered a CASE with no conditions true.");
|
|
475
|
+
}
|
|
476
|
+
visit(replay, body, items, scope, partial);
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
case "FunctionApplication": {
|
|
480
|
+
const lambda = getLazyFunction(
|
|
481
|
+
evaluate(expression.function, replay.isInit ? context : toKeepLazyContext(context)),
|
|
482
|
+
);
|
|
483
|
+
if (lambda) {
|
|
484
|
+
visit(replay, lambda.body, items, bindLazyArguments(lambda, expression, context), partial);
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
break;
|
|
488
|
+
}
|
|
489
|
+
case "Unchanged":
|
|
490
|
+
if (!replay.isInit) {
|
|
491
|
+
visitUnchanged(replay, expression.expression, items, scope, partial);
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
break;
|
|
495
|
+
case "BoxAction":
|
|
496
|
+
if (!replay.isInit) {
|
|
497
|
+
visit(replay, expression.action, items, scope, partial);
|
|
498
|
+
visitUnchanged(replay, expression.subscript, items, scope, partial);
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
break;
|
|
502
|
+
case "AngleAction":
|
|
503
|
+
if (!replay.isInit) {
|
|
504
|
+
visit(
|
|
505
|
+
replay,
|
|
506
|
+
expression.action,
|
|
507
|
+
cons("Changed", expression.subscript, scope, items),
|
|
508
|
+
scope,
|
|
509
|
+
partial,
|
|
510
|
+
);
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
break;
|
|
514
|
+
}
|
|
515
|
+
visitValue(replay, evaluate(expression, context), items, partial);
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
// TLC's processUnchanged.
|
|
519
|
+
const visitUnchanged = (
|
|
520
|
+
replay: Replay,
|
|
521
|
+
expression: Expression,
|
|
522
|
+
items: Item | undefined,
|
|
523
|
+
scope: Scope | undefined,
|
|
524
|
+
partial: State,
|
|
525
|
+
): void =>
|
|
526
|
+
withFrame(replay, expression, () => {
|
|
527
|
+
const index = getVariable(replay, expression, scope);
|
|
528
|
+
if (index !== undefined) {
|
|
529
|
+
const value = replay.current?.[index];
|
|
530
|
+
if (value !== undefined) proceedWithValue(replay, items, partial, index, value);
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
if (expression.kind === "Tuple") {
|
|
534
|
+
const [first, ...others] = expression.elements;
|
|
535
|
+
if (first === undefined) {
|
|
536
|
+
proceed(replay, items, partial);
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
const rest = others.reduceRight<Item | undefined>(
|
|
540
|
+
(tail, element) => cons("Unchanged", element, scope, tail),
|
|
541
|
+
items,
|
|
542
|
+
);
|
|
543
|
+
visitUnchanged(replay, first, rest, scope, partial);
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
if (expression.kind === "Identifier") {
|
|
547
|
+
const binding = lookupScope(scope, expression.name);
|
|
548
|
+
if (binding?.kind === "Thunk") {
|
|
549
|
+
visitUnchanged(replay, binding.expression, items, binding.scope, partial);
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
const operator = binding
|
|
553
|
+
? undefined
|
|
554
|
+
: resolveGlobalOperator(expression.name, replay.environment);
|
|
555
|
+
if (operator?.kind === "Closure" && operator.params.length === 0) {
|
|
556
|
+
visitUnchanged(replay, operator.body, items, operator.scope, partial);
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
const environment = replay.environment;
|
|
561
|
+
const before = evaluate(expression, {
|
|
562
|
+
environment,
|
|
563
|
+
scope,
|
|
564
|
+
state: replay.current,
|
|
565
|
+
next: undefined,
|
|
566
|
+
});
|
|
567
|
+
const after = evaluate(expression, { environment, scope, state: partial, next: undefined });
|
|
568
|
+
if (valuesEqual(before, after)) proceed(replay, items, partial);
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
const createReplay = (
|
|
572
|
+
environment: Environment,
|
|
573
|
+
isInit: boolean,
|
|
574
|
+
current: Value[] | undefined,
|
|
575
|
+
onState: (state: Value[]) => void,
|
|
576
|
+
isEnabled = false,
|
|
577
|
+
): Replay => {
|
|
578
|
+
const callStack = environment.callStack;
|
|
579
|
+
if (!callStack) throw new Error("The replay needs an environment that records its call stack");
|
|
580
|
+
return { environment, callStack, isInit, isEnabled, current, onState };
|
|
581
|
+
};
|
|
582
|
+
|
|
583
|
+
const ENABLED_FOUND = new Error("The replay found a successor");
|
|
584
|
+
|
|
585
|
+
// TLC's Tool#enabled, which walks the action as getNextStates does and stops at the first
|
|
586
|
+
// successor.
|
|
587
|
+
export const replayEnabled = (
|
|
588
|
+
expression: Expression,
|
|
589
|
+
scope: Scope | undefined,
|
|
590
|
+
state: Value[],
|
|
591
|
+
environment: Environment,
|
|
592
|
+
) => {
|
|
593
|
+
const replay = createReplay(
|
|
594
|
+
environment,
|
|
595
|
+
false,
|
|
596
|
+
state,
|
|
597
|
+
() => {
|
|
598
|
+
throw ENABLED_FOUND;
|
|
599
|
+
},
|
|
600
|
+
true,
|
|
601
|
+
);
|
|
602
|
+
const depth = replay.callStack.expressions.length;
|
|
603
|
+
try {
|
|
604
|
+
// The ENABLED of a fairness condition WF_v(A) passes A to TLC's enabled with v to change, where
|
|
605
|
+
// liveness.ts builds <<A>>_v at the location of A.
|
|
606
|
+
if (expression.kind === "AngleAction" && expression.location === expression.action.location) {
|
|
607
|
+
const changed = cons("Changed", expression.subscript, scope, undefined);
|
|
608
|
+
visit(replay, expression.action, changed, scope, getEmptyState(environment));
|
|
609
|
+
} else {
|
|
610
|
+
visit(replay, expression, undefined, scope, getEmptyState(environment));
|
|
611
|
+
}
|
|
612
|
+
return false;
|
|
613
|
+
} catch (error) {
|
|
614
|
+
if (error !== ENABLED_FOUND) throw error;
|
|
615
|
+
// The frames the walk left are those of the successor, not of a failure.
|
|
616
|
+
replay.callStack.expressions.length = depth;
|
|
617
|
+
replay.callStack.isFrozen = false;
|
|
618
|
+
return true;
|
|
619
|
+
}
|
|
620
|
+
};
|
|
621
|
+
|
|
622
|
+
// TLC's getInitStates, which conjoins the initial predicates of the specification.
|
|
623
|
+
export const replayInitialStates = (
|
|
624
|
+
inits: ReplayAction[],
|
|
625
|
+
environment: Environment,
|
|
626
|
+
onState: (state: Value[]) => void,
|
|
627
|
+
) => {
|
|
628
|
+
const [first, ...others] = inits;
|
|
629
|
+
if (first === undefined) return;
|
|
630
|
+
const replay = createReplay(environment, true, undefined, onState);
|
|
631
|
+
const rest = others.reduceRight<Item | undefined>(
|
|
632
|
+
(tail, init) => cons("Predicate", init.expression, init.scope, tail),
|
|
633
|
+
undefined,
|
|
634
|
+
);
|
|
635
|
+
visit(replay, first.expression, rest, first.scope, getEmptyState(environment));
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
// TLC's getNextStates for each action TLC splits the next-state relation into. TLC checks the
|
|
639
|
+
// successors of an action once it has computed them all.
|
|
640
|
+
export const replaySuccessors = <Action extends ReplayAction>(
|
|
641
|
+
actions: Action[],
|
|
642
|
+
state: Value[],
|
|
643
|
+
environment: Environment,
|
|
644
|
+
onState: (state: Value[], action: Action) => void,
|
|
645
|
+
) => {
|
|
646
|
+
// TLC fails to split a next-state relation that is not an operator application before it checks
|
|
647
|
+
// the spec, so it replays nothing.
|
|
648
|
+
if (actions.some((action) => LITERAL_KINDS.has(action.expression.kind))) return;
|
|
649
|
+
actions.forEach((action) => {
|
|
650
|
+
const successors: Value[][] = [];
|
|
651
|
+
const replay = createReplay(environment, false, state, (next) => successors.push(next));
|
|
652
|
+
visit(replay, action.expression, undefined, action.scope, getEmptyState(environment));
|
|
653
|
+
successors.forEach((successor) => onState(successor, action));
|
|
654
|
+
});
|
|
655
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { strFromU8, unzipSync } from "fflate";
|
|
3
|
+
|
|
4
|
+
// The TLC whose output tla-ts reproduces: the 1.7.4 release, or the master builds that tla-ts
|
|
5
|
+
// follows for any other tla2tools.jar.
|
|
6
|
+
export type TlcVersion = "1.7.4" | "master";
|
|
7
|
+
|
|
8
|
+
const MANIFEST = "META-INF/MANIFEST.MF";
|
|
9
|
+
|
|
10
|
+
// The version of a tla2tools.jar, from the Git tag that its manifest records.
|
|
11
|
+
export const getTlcVersion = (jar: string | undefined): TlcVersion => {
|
|
12
|
+
if (jar === undefined || !existsSync(jar)) return "master";
|
|
13
|
+
const manifest = unzipSync(readFileSync(jar), { filter: (file) => file.name === MANIFEST })[
|
|
14
|
+
MANIFEST
|
|
15
|
+
];
|
|
16
|
+
const tag = manifest && /^X-Git-Tag: *(\S*)/m.exec(strFromU8(manifest))?.[1];
|
|
17
|
+
return tag === "v1.7.4" ? "1.7.4" : "master";
|
|
18
|
+
};
|