tla-ts 0.0.8-dev.d9bffda → 0.0.9-dev.0dd11f695

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.
Files changed (119) hide show
  1. package/README.md +30 -0
  2. package/dist/compile.cjs +23 -0
  3. package/dist/compile.js +23 -0
  4. package/dist/deciders.cjs +15 -15
  5. package/dist/deciders.js +40 -40
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.d.cts +80 -10
  8. package/dist/index.d.ts +80 -10
  9. package/dist/index.js +1 -1
  10. package/dist/parallel-worker.cjs +1 -1
  11. package/dist/parallel-worker.js +1 -1
  12. package/dist/prove-worker.cjs +1 -1
  13. package/dist/prove-worker.js +1 -1
  14. package/dist/prove.cjs +116 -36
  15. package/dist/prove.js +116 -36
  16. package/dist/run.cjs +143 -31
  17. package/dist/run.js +143 -31
  18. package/dist/tlapm.cjs +2 -2
  19. package/dist/tlapm.js +1 -1
  20. package/dist/transfer.cjs +128 -103
  21. package/dist/transfer.js +128 -103
  22. package/dist/worker.cjs +7 -7
  23. package/dist/worker.js +7 -7
  24. package/package.json +3 -1
  25. package/src/ast.ts +7 -21
  26. package/src/bags.ts +99 -99
  27. package/src/builtins.ts +44 -75
  28. package/src/bundled-modules.ts +1 -8
  29. package/src/check.ts +605 -311
  30. package/src/cli/options.ts +12 -3
  31. package/src/cli/run.ts +2 -2
  32. package/src/cli/runtime-invariants.ts +135 -0
  33. package/src/cli/shared.ts +20 -9
  34. package/src/cli/tlc-arguments.ts +646 -0
  35. package/src/cli/tlc-usage.ts +362 -0
  36. package/src/cli/tlc.ts +571 -185
  37. package/src/cli/typescript.ts +26 -0
  38. package/src/cli/xml-exporter.ts +4 -10
  39. package/src/codegen.ts +190 -46
  40. package/src/config.ts +101 -68
  41. package/src/coverage.ts +855 -0
  42. package/src/dependencies.ts +20 -28
  43. package/src/dfid.ts +636 -0
  44. package/src/environment.ts +266 -98
  45. package/src/errors.ts +142 -45
  46. package/src/evaluate.ts +632 -298
  47. package/src/exit-codes.ts +37 -1
  48. package/src/index.ts +3 -0
  49. package/src/instances.ts +85 -9
  50. package/src/java-overrides.ts +2 -12
  51. package/src/json-reader.ts +456 -0
  52. package/src/json.ts +262 -139
  53. package/src/levels.ts +2 -3
  54. package/src/lexer.ts +17 -26
  55. package/src/liveness-checker.ts +10 -18
  56. package/src/liveness.ts +121 -49
  57. package/src/parallel-worker.ts +14 -10
  58. package/src/parallel.ts +41 -17
  59. package/src/parser.ts +436 -90
  60. package/src/pcal/ast.ts +7 -28
  61. package/src/pcal/parse.ts +11 -39
  62. package/src/pcal/tokenize.ts +4 -20
  63. package/src/pcal/translate.ts +10 -42
  64. package/src/pcal/validate.ts +3 -21
  65. package/src/pretty-print.ts +1 -2
  66. package/src/prover/smt/solver/solver.ts +31 -27
  67. package/src/randomization.ts +84 -35
  68. package/src/resolve.ts +18 -60
  69. package/src/runtime.ts +11 -4
  70. package/src/sany/builtins.ts +3 -4
  71. package/src/sany/generator.ts +128 -176
  72. package/src/sany/levels.ts +6 -30
  73. package/src/sany/lexer.ts +7 -23
  74. package/src/sany/linter.ts +31 -29
  75. package/src/sany/modules.ts +1 -5
  76. package/src/sany/nodes.ts +4 -28
  77. package/src/sany/parse-error.ts +44 -100
  78. package/src/sany/report.ts +25 -40
  79. package/src/sany/selectors.ts +8 -22
  80. package/src/sany/source.ts +8 -15
  81. package/src/sany/xml.ts +11 -42
  82. package/src/search.ts +547 -144
  83. package/src/simulate.ts +694 -0
  84. package/src/spec-description.ts +166 -48
  85. package/src/standard-definitions.ts +2 -3
  86. package/src/standard-modules.ts +264 -68
  87. package/src/state-graph.ts +51 -18
  88. package/src/subexpressions.ts +31 -5
  89. package/src/successors.ts +267 -77
  90. package/src/symmetry.ts +31 -8
  91. package/src/tableau.ts +1 -2
  92. package/src/tlc/fingerprint.ts +183 -61
  93. package/src/tlc/jar-data.ts +113 -6
  94. package/src/tlc/messages.ts +122 -37
  95. package/src/tlc/order.ts +33 -38
  96. package/src/tlc/random.ts +16 -16
  97. package/src/tlc/replay.ts +563 -211
  98. package/src/tlc/summary.ts +2 -4
  99. package/src/tlc/tlc-operators.ts +81 -36
  100. package/src/tlc/tlcext-operators.ts +138 -117
  101. package/src/tlc/trace-spec.ts +273 -0
  102. package/src/tlc/value-stream.ts +143 -0
  103. package/src/tlc/version.ts +2 -4
  104. package/src/typescript/actions.ts +494 -0
  105. package/src/typescript/collections.ts +615 -0
  106. package/src/typescript/compile.ts +70 -0
  107. package/src/typescript/context.ts +106 -0
  108. package/src/typescript/emit.ts +122 -0
  109. package/src/typescript/expressions.ts +508 -0
  110. package/src/typescript/formula.ts +160 -0
  111. package/src/typescript/functions.ts +295 -0
  112. package/src/typescript/model.ts +164 -0
  113. package/src/typescript/names.ts +116 -0
  114. package/src/typescript/program.ts +51 -0
  115. package/src/typescript/properties.ts +148 -0
  116. package/src/typescript/scope.ts +117 -0
  117. package/src/typescript/steps.ts +175 -0
  118. package/src/typescript/types.ts +178 -0
  119. package/src/values.ts +832 -255
package/src/tlc/replay.ts CHANGED
@@ -1,14 +1,24 @@
1
1
  import type { Expression, SourceLocation } from "../ast";
2
2
  import { TlaEvaluationError } from "../errors";
3
3
  import { isConstantLevel } from "../dependencies";
4
+ import {
5
+ countCoverage,
6
+ costCoverage,
7
+ getCoverageNode,
8
+ runAtCoverageNode,
9
+ type Coverage,
10
+ } from "../coverage";
4
11
  import {
5
12
  bindDefinitions,
6
13
  bindLazyArguments,
7
14
  bindOperatorArguments,
15
+ describeRange,
8
16
  evaluate,
9
- forEachBinding,
17
+ forEachBoundElement,
10
18
  freezeCallStack,
11
19
  getLazyFunction,
20
+ getQuantifierElements,
21
+ mapBoundSets,
12
22
  toKeepLazyContext,
13
23
  lookupScope,
14
24
  resolveApplyOperator,
@@ -20,13 +30,19 @@ import {
20
30
  type State,
21
31
  toUnspecifiedSubscriptError,
22
32
  } 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.
33
+ import {
34
+ getStreamedElements,
35
+ isEqualLikeTlc,
36
+ isTlcEnumerable,
37
+ setTrace,
38
+ type Value,
39
+ } from "../values";
40
+ import { substitutionFrames } from "../instances";
41
+ import { MESSAGE_CODE } from "./messages";
42
+
43
+ // Replays a failed computation as TLC's CallStackTool does, walking the predicates in the order
44
+ // and nesting of Tool.getInitStates and getNextStates, so it records the nodes TLC records, and
45
+ // counts the coverage TLC counts.
30
46
 
31
47
  interface ReplayAction {
32
48
  expression: Expression;
@@ -38,23 +54,27 @@ interface ActionItem {
38
54
  kind: "Predicate" | "Unchanged" | "Changed";
39
55
  expression: Expression;
40
56
  scope: Scope | undefined;
57
+ node: number;
41
58
  rest: ActionItem | undefined;
42
59
  }
43
60
 
44
61
  interface Replay {
45
62
  environment: Environment;
46
- callStack: CallStack;
63
+ callStack: CallStack | undefined;
64
+ coverage: Coverage | undefined;
47
65
  isInit: boolean;
48
- // Whether the replay computes ENABLED rather than the successors of the state.
49
66
  isEnabled: boolean;
50
- // The state whose successors the replay computes.
51
67
  current: Value[] | undefined;
52
68
  onState: (state: Value[]) => void;
69
+ onIncomplete: ((state: State) => void) | undefined;
53
70
  }
54
71
 
55
72
  const withFrame = <Result>(replay: Replay, expression: Expression, run: () => Result): Result => {
56
73
  const callStack = replay.callStack;
57
- callStack.expressions.push(expression);
74
+ if (!callStack) return run();
75
+ const isEntered = callStack.expressions.at(-1) === expression;
76
+ const frames = (!isEntered && substitutionFrames.get(expression)) || [];
77
+ callStack.expressions.push(...frames, expression);
58
78
  const outerSets = setTrace.sets;
59
79
  const sets = outerSets ?? [];
60
80
  const depth = sets.length;
@@ -66,12 +86,37 @@ const withFrame = <Result>(replay: Replay, expression: Expression, run: () => Re
66
86
  freezeCallStack(callStack, sets.slice(depth), error);
67
87
  throw error;
68
88
  } finally {
69
- if (!callStack.isFrozen) callStack.expressions.pop();
89
+ if (!callStack.isFrozen) callStack.expressions.length -= frames.length + 1;
70
90
  sets.length = depth;
71
91
  setTrace.sets = outerSets;
72
92
  }
73
93
  };
74
94
 
95
+ const getNode = (replay: Replay, node: number, expression: Expression) =>
96
+ replay.coverage ? getCoverageNode(replay.coverage, node, expression) : -1;
97
+
98
+ const runAt = <Result>(replay: Replay, node: number, run: () => Result) =>
99
+ replay.coverage ? runAtCoverageNode(replay.coverage, node, run) : run();
100
+
101
+ const evaluateAt = (replay: Replay, node: number, expression: Expression, context: Context) =>
102
+ runAt(replay, node, () => evaluate(expression, context));
103
+
104
+ // TLC evaluates a user-defined or overridden operator's arguments under its node, but doesn't
105
+ // count the node itself.
106
+ const evaluateOperatorAt = (
107
+ replay: Replay,
108
+ node: number,
109
+ expression: Expression,
110
+ context: Context,
111
+ ) => {
112
+ const value = evaluateAt(replay, node, expression, context);
113
+ const coverage = replay.coverage;
114
+ if (coverage?.covered.has(expression)) {
115
+ for (let item = node; item !== -1; item = coverage.prefixes[item]) coverage.primary[item]--;
116
+ }
117
+ return value;
118
+ };
119
+
75
120
  const getContext = (replay: Replay, scope: Scope | undefined, partial: State): Context =>
76
121
  replay.isInit
77
122
  ? { environment: replay.environment, scope, state: partial, next: undefined }
@@ -87,8 +132,7 @@ const assign = (partial: State, index: number, value: Value) => {
87
132
  return copy;
88
133
  };
89
134
 
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.
135
+ // SANY parses A /\ B /\ C as (A /\ B) /\ C, so the replay recreates the node of each prefix.
92
136
  const getPrefix = (expression: Expression & { kind: "And" | "Or" }, count: number): Expression => {
93
137
  const first = expression.operands[0].location;
94
138
  const range = expression.location.range;
@@ -98,48 +142,48 @@ const getPrefix = (expression: Expression & { kind: "And" | "Or" }, count: numbe
98
142
  return { ...expression, operands: expression.operands.slice(0, count), location };
99
143
  };
100
144
 
145
+ const getPrefixNode = (replay: Replay, node: number) =>
146
+ replay.coverage ? replay.coverage.prefixes[node] : -1;
147
+
101
148
  const getIdentifierName = (expression: Expression) =>
102
149
  expression.kind === "Identifier" ? expression.name : undefined;
103
150
 
104
- // The state variable an expression names, as TLC's getVar finds it through definitions and
105
- // operator arguments.
106
151
  const getVariable = (
107
- replay: Replay,
152
+ environment: Environment,
108
153
  expression: Expression,
109
154
  scope: Scope | undefined,
110
155
  ): number | undefined => {
111
- if (expression.kind === "Label") return getVariable(replay, expression.body, scope);
156
+ if (expression.kind === "Label") return getVariable(environment, expression.body, scope);
112
157
  const name = getIdentifierName(expression);
113
158
  if (name === undefined) return undefined;
114
159
  const binding = lookupScope(scope, name);
115
- if (binding?.kind === "Thunk") return getVariable(replay, binding.expression, binding.scope);
160
+ if (binding?.kind === "Thunk") return getVariable(environment, binding.expression, binding.scope);
116
161
  if (binding) return undefined;
117
- const index = replay.environment.variableIndex.get(name);
162
+ const index = environment.variableIndex.get(name);
118
163
  if (index !== undefined) return index;
119
- const operator = resolveGlobalOperator(name, replay.environment);
164
+ const operator = resolveGlobalOperator(name, environment);
120
165
  return operator?.kind === "Closure" && operator.params.length === 0
121
- ? getVariable(replay, operator.body, operator.scope)
166
+ ? getVariable(environment, operator.body, operator.scope)
122
167
  : undefined;
123
168
  };
124
169
 
125
- // The variable a primed expression such as x' assigns, as TLC's getPrimedVar finds it.
126
170
  const getPrimedVariable = (
127
- replay: Replay,
171
+ environment: Environment,
128
172
  expression: Expression,
129
173
  scope: Scope | undefined,
130
174
  ): number | undefined => {
131
- if (expression.kind === "Prime") return getVariable(replay, expression.expression, scope);
132
- if (expression.kind === "Label") return getPrimedVariable(replay, expression.body, scope);
175
+ if (expression.kind === "Prime") return getVariable(environment, expression.expression, scope);
176
+ if (expression.kind === "Label") return getPrimedVariable(environment, expression.body, scope);
133
177
  const name = getIdentifierName(expression);
134
178
  if (name === undefined) return undefined;
135
179
  const binding = lookupScope(scope, name);
136
180
  if (binding?.kind === "Thunk") {
137
- return getPrimedVariable(replay, binding.expression, binding.scope);
181
+ return getPrimedVariable(environment, binding.expression, binding.scope);
138
182
  }
139
183
  if (binding) return undefined;
140
- const operator = resolveGlobalOperator(name, replay.environment);
184
+ const operator = resolveGlobalOperator(name, environment);
141
185
  return operator?.kind === "Closure" && operator.params.length === 0
142
- ? getPrimedVariable(replay, operator.body, operator.scope)
186
+ ? getPrimedVariable(environment, operator.body, operator.scope)
143
187
  : undefined;
144
188
  };
145
189
 
@@ -149,37 +193,60 @@ const isTrue = (value: Value) => {
149
193
  return value;
150
194
  };
151
195
 
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.
196
+ // Once every variable has a value, TLC evaluates the conjuncts left instead of walking them,
197
+ // though not under ENABLED, and the next states take this shortcut only with warnings on.
155
198
  export const isAssignedShortcut = (replay: Pick<Replay, "environment" | "isInit" | "isEnabled">) =>
156
199
  !replay.isEnabled && (replay.isInit || replay.environment.runtime.isWarningEnabled);
157
200
 
158
- // TLC's getInitStates(ActionItemList) and getNextStates(ActionItemList): processes the conjuncts
159
- // left once a conjunct holds.
160
- const proceed = (replay: Replay, items: ActionItem | undefined, partial: State): void => {
201
+ // TLC counts the node that goes on to the conjuncts left for each next state they produce, but
202
+ // counts an initial state only as it adds it.
203
+ const proceed = (
204
+ replay: Replay,
205
+ items: ActionItem | undefined,
206
+ partial: State,
207
+ node: number,
208
+ ): boolean => {
209
+ const isProduced = proceedItems(replay, items, partial, node);
210
+ if (isProduced && replay.coverage && !replay.isInit) countCoverage(replay.coverage, node);
211
+ return isProduced;
212
+ };
213
+
214
+ const addState = (replay: Replay, state: Value[], node: number) => {
215
+ const coverage = replay.coverage;
216
+ if (coverage && replay.isInit) {
217
+ countCoverage(coverage, node);
218
+ countCoverage(coverage, coverage.roots[node]);
219
+ }
220
+ replay.onState(state);
221
+ return true;
222
+ };
223
+
224
+ const proceedItems = (
225
+ replay: Replay,
226
+ items: ActionItem | undefined,
227
+ partial: State,
228
+ node: number,
229
+ ): boolean => {
161
230
  if (items === undefined) {
162
- if (isComplete(partial)) replay.onState(partial);
163
- return;
231
+ if (isComplete(partial)) return addState(replay, partial, node);
232
+ replay.onIncomplete?.(partial);
233
+ return false;
164
234
  }
165
235
  if (isComplete(partial) && isAssignedShortcut(replay)) {
166
- proceedAssigned(replay, items, partial);
167
- return;
236
+ return proceedAssigned(replay, items, partial, node);
168
237
  }
169
238
  if (items.kind === "Predicate") {
170
- visit(replay, items.expression, items.rest, items.scope, partial);
171
- return;
239
+ return visit(replay, items.expression, items.rest, items.scope, partial, items.node);
172
240
  }
173
241
  if (items.kind === "Unchanged") {
174
- visitUnchanged(replay, items.expression, items.rest, items.scope, partial);
175
- return;
242
+ return visitUnchanged(replay, items.expression, items.rest, items.scope, partial, items.node);
176
243
  }
177
- if (isChanged(replay, items, partial)) proceed(replay, items.rest, partial);
244
+ return isChanged(replay, items, partial) && proceed(replay, items.rest, partial, items.node);
178
245
  };
179
246
 
180
247
  const isChanged = (replay: Replay, item: ActionItem, partial: State) => {
181
248
  const environment = replay.environment;
182
- const before = evaluate(item.expression, {
249
+ const before = evaluateAt(replay, item.node, item.expression, {
183
250
  environment,
184
251
  scope: item.scope,
185
252
  state: replay.current,
@@ -187,7 +254,7 @@ const isChanged = (replay: Replay, item: ActionItem, partial: State) => {
187
254
  });
188
255
  const after = (() => {
189
256
  try {
190
- return evaluate(item.expression, {
257
+ return evaluateAt(replay, item.node, item.expression, {
191
258
  environment,
192
259
  scope: item.scope,
193
260
  state: partial,
@@ -199,24 +266,38 @@ const isChanged = (replay: Replay, item: ActionItem, partial: State) => {
199
266
  : error;
200
267
  }
201
268
  })();
202
- return !valuesEqual(before, after);
269
+ try {
270
+ return !isEqualLikeTlc(before, after);
271
+ } catch (error) {
272
+ // TLC's v0.equals(v1) raises a FingerprintException when v0 has a source.
273
+ if (replay.callStack) freezeCallStack(replay.callStack, [before], error);
274
+ throw error;
275
+ }
203
276
  };
204
277
 
205
- const proceedAssigned = (replay: Replay, items: ActionItem | undefined, partial: Value[]): void => {
278
+ const proceedAssigned = (
279
+ replay: Replay,
280
+ items: ActionItem | undefined,
281
+ partial: Value[],
282
+ node: number,
283
+ ): boolean => {
206
284
  for (let item = items; item !== undefined; item = item.rest) {
207
285
  if (item.kind === "Unchanged") {
208
- visitUnchanged(replay, item.expression, item.rest, item.scope, partial);
209
- return;
286
+ return visitUnchanged(replay, item.expression, item.rest, item.scope, partial, item.node);
210
287
  }
211
- if (
288
+ const isHeld =
212
289
  item.kind === "Changed"
213
- ? !isChanged(replay, item, partial)
214
- : !isTrue(evaluate(item.expression, getContext(replay, item.scope, partial)))
215
- ) {
216
- return;
290
+ ? isChanged(replay, item, partial)
291
+ : isTrue(
292
+ evaluateAt(replay, item.node, item.expression, getContext(replay, item.scope, partial)),
293
+ );
294
+ if (!isHeld) {
295
+ const coverage = replay.coverage;
296
+ if (coverage && replay.isInit) coverage.secondary[coverage.roots[node]]++;
297
+ return false;
217
298
  }
218
299
  }
219
- replay.onState(partial);
300
+ return addState(replay, partial, node);
220
301
  };
221
302
 
222
303
  const visit = (
@@ -225,26 +306,39 @@ const visit = (
225
306
  items: ActionItem | undefined,
226
307
  scope: Scope | undefined,
227
308
  partial: State,
228
- ): void =>
309
+ parent: number,
310
+ ): boolean =>
311
+ visitAt(replay, expression, items, scope, partial, getNode(replay, parent, expression));
312
+
313
+ const visitAt = (
314
+ replay: Replay,
315
+ expression: Expression,
316
+ items: ActionItem | undefined,
317
+ scope: Scope | undefined,
318
+ partial: State,
319
+ node: number,
320
+ ): boolean =>
229
321
  withFrame(replay, expression, () => {
230
322
  switch (expression.kind) {
231
- case "Let":
232
- visit(
233
- replay,
234
- expression.body,
235
- items,
236
- bindDefinitions(expression.definitions, getContext(replay, scope, partial)),
237
- partial,
238
- );
239
- return;
323
+ case "Let": {
324
+ const context = getContext(replay, scope, partial);
325
+ // TLC walks a LET definition's body again at each reference, under the reference.
326
+ const definitions = expression.definitions;
327
+ const bound = replay.coverage
328
+ ? bindDefinitions(definitions, context, {
329
+ impure: new Set(definitions),
330
+ streamed: new Set(),
331
+ })
332
+ : bindDefinitions(definitions, context);
333
+ return visit(replay, expression.body, items, bound, partial, node);
334
+ }
240
335
  case "Label":
241
- visit(replay, expression.body, items, scope, partial);
242
- return;
336
+ return visit(replay, expression.body, items, scope, partial, node);
243
337
  default:
244
338
  if (LITERAL_KINDS.has(expression.kind))
245
339
  throw new TlaEvaluationError("The state relation is not a boolean expression.");
246
- withFrame(replay, expression, () =>
247
- visitApplication(replay, expression, items, scope, partial),
340
+ return withFrame(replay, expression, () =>
341
+ visitApplication(replay, expression, items, scope, partial, node),
248
342
  );
249
343
  }
250
344
  });
@@ -255,9 +349,30 @@ const visitValue = (
255
349
  value: Value,
256
350
  items: ActionItem | undefined,
257
351
  partial: State,
258
- ) => {
259
- if (isTrue(value)) proceed(replay, items, partial);
260
- };
352
+ node: number,
353
+ ) => isTrue(value) && proceed(replay, items, partial, node);
354
+
355
+ const BUILT_IN_OPERATORS = new Set([
356
+ "=",
357
+ "#",
358
+ "~",
359
+ "<=>",
360
+ "/\\",
361
+ "\\/",
362
+ "=>",
363
+ "\\in",
364
+ "\\notin",
365
+ "\\subseteq",
366
+ "\\cup",
367
+ "\\cap",
368
+ "\\",
369
+ "SUBSET",
370
+ "UNION",
371
+ "DOMAIN",
372
+ "~>",
373
+ "-+->",
374
+ "\\cdot",
375
+ ]);
261
376
 
262
377
  const visitDefined = (
263
378
  replay: Replay,
@@ -265,21 +380,21 @@ const visitDefined = (
265
380
  items: ActionItem | undefined,
266
381
  scope: Scope | undefined,
267
382
  partial: State,
268
- ) => {
383
+ node: number,
384
+ ): boolean => {
269
385
  const context = getContext(replay, scope, partial);
270
386
  if (expression.kind === "Identifier") {
271
387
  const binding = lookupScope(scope, expression.name);
272
388
  if (binding?.kind === "Thunk" && binding.cache === undefined) {
273
- visit(replay, binding.expression, items, binding.scope, partial);
274
- return;
389
+ const thunkNode = replay.coverage?.values.get(binding) ?? node;
390
+ return visit(replay, binding.expression, items, binding.scope, partial, thunkNode);
275
391
  }
276
392
  if (
277
393
  binding?.kind === "Operator" &&
278
394
  binding.operator.kind === "Closure" &&
279
395
  binding.operator.params.length === 0
280
396
  ) {
281
- visit(replay, binding.operator.body, items, binding.operator.scope, partial);
282
- return;
397
+ return visit(replay, binding.operator.body, items, binding.operator.scope, partial, node);
283
398
  }
284
399
  const definition = binding ? undefined : replay.environment.definitions.get(expression.name);
285
400
  const operator = binding
@@ -291,24 +406,27 @@ const visitDefined = (
291
406
  !replay.environment.variableIndex.has(expression.name) &&
292
407
  !(definition && isConstantLevel(definition, replay.environment))
293
408
  ) {
294
- visit(replay, operator.body, items, operator.scope, partial);
295
- return;
409
+ return visit(replay, operator.body, items, operator.scope, partial, node);
296
410
  }
297
- visitValue(replay, evaluate(expression, context), items, partial);
298
- return;
411
+ return visitValue(
412
+ replay,
413
+ evaluateOperatorAt(replay, node, expression, context),
414
+ items,
415
+ partial,
416
+ node,
417
+ );
299
418
  }
300
419
  const operator = resolveApplyOperator(expression, context);
301
420
  if (operator?.kind === "Closure" && operator.params.length === expression.args.length) {
302
- visit(
303
- replay,
304
- operator.body,
305
- items,
421
+ const bound = runAt(replay, node, () =>
306
422
  bindOperatorArguments(operator, expression.args, context),
307
- partial,
308
423
  );
309
- return;
424
+ return visit(replay, operator.body, items, bound, partial, node);
310
425
  }
311
- visitValue(replay, evaluate(expression, context), items, partial);
426
+ const value = BUILT_IN_OPERATORS.has(expression.operator)
427
+ ? evaluateAt(replay, node, expression, context)
428
+ : evaluateOperatorAt(replay, node, expression, context);
429
+ return visitValue(replay, value, items, partial, node);
312
430
  };
313
431
 
314
432
  // A variable takes the value when it has none yet, and otherwise must already have it.
@@ -318,21 +436,26 @@ const proceedWithValue = (
318
436
  partial: State,
319
437
  index: number,
320
438
  value: Value,
439
+ node: number,
321
440
  ) => {
322
441
  const assigned = partial[index];
323
- if (assigned === undefined) {
324
- proceed(replay, items, assign(partial, index, value));
325
- return;
326
- }
327
- if (valuesEqual(assigned, value)) proceed(replay, items, partial);
442
+ if (assigned === undefined) return proceed(replay, items, assign(partial, index, value), node);
443
+ return isEqualLikeTlc(assigned, value) && proceed(replay, items, partial, node);
328
444
  };
329
445
 
330
446
  const createItem = (
331
447
  kind: ActionItem["kind"],
332
448
  expression: Expression,
333
449
  scope: Scope | undefined,
450
+ node: number,
334
451
  rest: ActionItem | undefined,
335
- ): ActionItem => ({ kind, expression, scope, rest });
452
+ ): ActionItem => ({ kind, expression, scope, node, rest });
453
+
454
+ // TLC costs a set it enumerates for each element, and for the end of the enumeration.
455
+ const getEnumerated = (replay: Replay, set: Value, elements: Value[]) => {
456
+ if (replay.coverage) costCoverage(replay.coverage, set, elements.length + 1);
457
+ return elements;
458
+ };
336
459
 
337
460
  const visitAssignment = (
338
461
  replay: Replay,
@@ -340,65 +463,121 @@ const visitAssignment = (
340
463
  items: ActionItem | undefined,
341
464
  scope: Scope | undefined,
342
465
  partial: State,
466
+ node: number,
343
467
  ) => {
344
468
  const [target, source] = expression.args;
345
469
  const index = replay.isInit
346
- ? getVariable(replay, target, scope)
347
- : getPrimedVariable(replay, target, scope);
470
+ ? getVariable(replay.environment, target, scope)
471
+ : getPrimedVariable(replay.environment, target, scope);
348
472
  const context = getContext(replay, scope, partial);
349
473
  if (index === undefined) {
350
- if (evaluate(expression, context) === true) proceed(replay, items, partial);
351
- return;
474
+ return (
475
+ evaluateAt(replay, node, expression, context) === true &&
476
+ proceed(replay, items, partial, node)
477
+ );
352
478
  }
353
- const value = evaluate(source, context);
479
+ const value = evaluateAt(replay, node, source, context);
354
480
  const assigned = partial[index];
355
481
  if (expression.operator === "=") {
356
- proceedWithValue(replay, items, partial, index, value);
357
- return;
482
+ return proceedWithValue(replay, items, partial, index, value, node);
358
483
  }
359
484
  const set: Value = expression.operator === "\\subseteq" ? { kind: "Subset", base: value } : value;
485
+ // TLC gives the power set the source of its base, so a failure to enumerate it names both.
486
+ if (typeof set === "object" && set !== value) replay.callStack?.sources.set(set, source);
360
487
  if (assigned !== undefined) {
361
- if (evaluate(expression, context) === true) proceed(replay, items, partial);
362
- return;
488
+ return (
489
+ evaluateAt(replay, node, expression, context) === true &&
490
+ proceed(replay, items, partial, node)
491
+ );
363
492
  }
364
493
  if (!isTlcEnumerable(set))
365
494
  throw new TlaEvaluationError("The right side of \\IN is not enumerable.");
366
- for (const element of getStreamedElements(set)) {
367
- proceed(replay, items, assign(partial, index, element));
368
- }
495
+ return getEnumerated(replay, set, getStreamedElements(set)).reduce<boolean>(
496
+ (isProduced, element) =>
497
+ proceed(replay, items, assign(partial, index, element), node) || isProduced,
498
+ false,
499
+ );
369
500
  };
370
501
 
371
502
  const ASSIGNMENT_OPERATORS = new Set(["=", "\\in", "\\subseteq"]);
372
503
 
373
504
  export const LITERAL_KINDS = new Set<Expression["kind"]>(["Number", "Decimal", "String"]);
374
505
 
506
+ const ENABLED_TEMPORAL_FORMS: Partial<Record<Expression["kind"], string>> = {
507
+ Always: "[]",
508
+ Eventually: "<>",
509
+ WeakFairness: "WF",
510
+ StrongFairness: "SF",
511
+ };
512
+
513
+ // TLC rejects a temporal formula, and a quantifier or CHOOSE without a bound, under ENABLED.
514
+ export const getEnabledFailure = (expression: Expression) => {
515
+ const form =
516
+ expression.kind === "Apply" && (expression.operator === "~>" || expression.operator === "-+->")
517
+ ? expression.operator === "~>"
518
+ ? "a ~> b"
519
+ : "a -+-> formula"
520
+ : ENABLED_TEMPORAL_FORMS[expression.kind];
521
+ if (form !== undefined) {
522
+ return `In computing ENABLED, TLC encountered a temporal formula (${form}).\n${describeRange(expression)}`;
523
+ }
524
+ if (expression.kind === "UnboundedQuantifier") {
525
+ return expression.isTemporal
526
+ ? `In computing ENABLED, TLC encountered temporal quantifier.\n${describeRange(expression)}`
527
+ : `In computing ENABLED, TLC encountered unbounded quantifier. Make sure that the expression is of form ${expression.quantifier === "Forall" ? "\\A" : "\\E"} x \\in S: P(x).\n${describeRange(expression)}`;
528
+ }
529
+ if (expression.kind === "Choose" && expression.set === undefined) {
530
+ return `In computing ENABLED, TLC encountered unbounded CHOOSE. Make sure that the expression is of form CHOOSE x \\in S: P(x).\n${describeRange(expression)}`;
531
+ }
532
+ return undefined;
533
+ };
534
+
535
+ const getBindings = (
536
+ replay: Replay,
537
+ expression: Expression & { kind: "Exists" | "Forall" },
538
+ context: Context,
539
+ node: number,
540
+ ) => {
541
+ const bindings: (Scope | undefined)[] = [];
542
+ const version = replay.environment.runtime.version;
543
+ const sets = runAt(replay, node, () =>
544
+ mapBoundSets(expression.bounds, (set) => {
545
+ const value = evaluate(set, context);
546
+ const elements = getQuantifierElements(value, set, version);
547
+ if (Array.isArray(elements)) getEnumerated(replay, value, elements);
548
+ return elements;
549
+ }),
550
+ );
551
+ forEachBoundElement(expression.bounds, sets, context.scope, (bound) => {
552
+ bindings.push(bound);
553
+ return false;
554
+ });
555
+ return bindings;
556
+ };
557
+
375
558
  const visitApplication = (
376
559
  replay: Replay,
377
560
  expression: Expression,
378
561
  items: ActionItem | undefined,
379
562
  scope: Scope | undefined,
380
563
  partial: State,
381
- ): void => {
564
+ node: number,
565
+ ): boolean => {
382
566
  const context = getContext(replay, scope, partial);
383
567
  switch (expression.kind) {
384
568
  case "Identifier":
385
- visitDefined(replay, expression, items, scope, partial);
386
- return;
569
+ return visitDefined(replay, expression, items, scope, partial, node);
387
570
  case "Apply": {
388
571
  if (ASSIGNMENT_OPERATORS.has(expression.operator) && expression.args.length === 2) {
389
- visitAssignment(replay, expression, items, scope, partial);
390
- return;
572
+ return visitAssignment(replay, expression, items, scope, partial, node);
391
573
  }
392
574
  if (expression.operator === "=>" && expression.args.length === 2) {
393
- if (isTrue(evaluate(expression.args[0], context))) {
394
- visit(replay, expression.args[1], items, scope, partial);
395
- return;
575
+ if (isTrue(evaluateAt(replay, node, expression.args[0], context))) {
576
+ return visit(replay, expression.args[1], items, scope, partial, node);
396
577
  }
397
- proceed(replay, items, partial);
398
- return;
578
+ return proceed(replay, items, partial, node);
399
579
  }
400
- visitDefined(replay, expression, items, scope, partial);
401
- return;
580
+ return visitDefined(replay, expression, items, scope, partial, node);
402
581
  }
403
582
  case "And": {
404
583
  const operands = expression.operands;
@@ -406,170 +585,216 @@ const visitApplication = (
406
585
  const rest = operands
407
586
  .slice(1)
408
587
  .reduceRight<ActionItem | undefined>(
409
- (tail, operand) => createItem("Predicate", operand, scope, tail),
588
+ (tail, operand) => createItem("Predicate", operand, scope, node, tail),
410
589
  items,
411
590
  );
412
- visit(replay, operands[0], rest, scope, partial);
413
- return;
591
+ return visit(replay, operands[0], rest, scope, partial, node);
414
592
  }
415
593
  const last = operands[operands.length - 1];
416
- visit(
594
+ return visitAt(
417
595
  replay,
418
596
  getPrefix(expression, operands.length - 1),
419
- createItem("Predicate", last, scope, items),
597
+ createItem("Predicate", last, scope, node, items),
420
598
  scope,
421
599
  partial,
600
+ getPrefixNode(replay, node),
422
601
  );
423
- return;
424
602
  }
425
603
  case "Or": {
426
604
  const operands = expression.operands;
427
605
  if (expression.isBulleted || operands.length === 2) {
428
- operands.forEach((operand) => visit(replay, operand, items, scope, partial));
429
- return;
606
+ return operands.reduce(
607
+ (isProduced, operand) =>
608
+ visit(replay, operand, items, scope, partial, node) || isProduced,
609
+ false,
610
+ );
430
611
  }
431
- visit(replay, getPrefix(expression, operands.length - 1), items, scope, partial);
432
- visit(replay, operands[operands.length - 1], items, scope, partial);
433
- return;
612
+ const isPrefixProduced = visitAt(
613
+ replay,
614
+ getPrefix(expression, operands.length - 1),
615
+ items,
616
+ scope,
617
+ partial,
618
+ getPrefixNode(replay, node),
619
+ );
620
+ const isLastProduced = visit(
621
+ replay,
622
+ operands[operands.length - 1],
623
+ items,
624
+ scope,
625
+ partial,
626
+ node,
627
+ );
628
+ return isPrefixProduced || isLastProduced;
434
629
  }
435
630
  case "Exists":
436
- forEachBinding(expression.bounds, context, (bound) => {
437
- visit(replay, expression.body, items, bound, partial);
438
- return false;
439
- });
440
- return;
631
+ return getBindings(replay, expression, context, node).reduce(
632
+ (isProduced, bound) =>
633
+ visit(replay, expression.body, items, bound, partial, node) || isProduced,
634
+ false,
635
+ );
441
636
  case "Forall": {
442
- const bounds: (Scope | undefined)[] = [];
443
- forEachBinding(expression.bounds, context, (bound) => {
444
- bounds.push(bound);
445
- return false;
446
- });
447
- if (bounds.length === 0) {
448
- proceed(replay, items, partial);
449
- return;
450
- }
637
+ const bounds = getBindings(replay, expression, context, node);
638
+ if (bounds.length === 0) return proceed(replay, items, partial, node);
451
639
  // TLC adds each binding after the first to the front of the conjuncts left.
452
640
  const rest = bounds
453
641
  .slice(1)
454
642
  .reduce<ActionItem | undefined>(
455
- (tail, bound) => createItem("Predicate", expression.body, bound, tail),
643
+ (tail, bound) => createItem("Predicate", expression.body, bound, node, tail),
456
644
  items,
457
645
  );
458
- visit(replay, expression.body, rest, bounds[0], partial);
459
- return;
646
+ return visit(replay, expression.body, rest, bounds[0], partial, node);
460
647
  }
461
648
  case "If":
462
- visit(
649
+ return visit(
463
650
  replay,
464
- isTrue(evaluate(expression.condition, context))
651
+ isTrue(evaluateAt(replay, node, expression.condition, context))
465
652
  ? expression.consequent
466
653
  : expression.alternative,
467
654
  items,
468
655
  scope,
469
656
  partial,
657
+ node,
470
658
  );
471
- return;
472
659
  case "Case": {
473
660
  const arm = expression.arms.find((candidate) =>
474
- isTrue(evaluate(candidate.condition, context)),
661
+ isTrue(evaluateAt(replay, node, candidate.condition, context)),
475
662
  );
476
663
  const body = arm?.body ?? expression.other;
477
664
  if (body === undefined) {
478
665
  throw new TlaEvaluationError("TLC encountered a CASE with no conditions true.");
479
666
  }
480
- visit(replay, body, items, scope, partial);
481
- return;
667
+ return visit(replay, body, items, scope, partial, node);
482
668
  }
483
669
  case "FunctionApplication": {
484
670
  const lambda = getLazyFunction(
485
- evaluate(expression.function, replay.isInit ? context : toKeepLazyContext(context)),
671
+ evaluateAt(
672
+ replay,
673
+ node,
674
+ expression.function,
675
+ replay.isInit ? context : toKeepLazyContext(context),
676
+ ),
486
677
  );
487
678
  if (lambda) {
488
- visit(replay, lambda.body, items, bindLazyArguments(lambda, expression, context), partial);
489
- return;
679
+ const bound = runAt(replay, node, () => bindLazyArguments(lambda, expression, context));
680
+ return visit(replay, lambda.body, items, bound, partial, node);
490
681
  }
491
682
  break;
492
683
  }
493
684
  case "Unchanged":
494
685
  if (!replay.isInit) {
495
- visitUnchanged(replay, expression.expression, items, scope, partial);
496
- return;
686
+ return visitUnchanged(replay, expression.expression, items, scope, partial, node);
497
687
  }
498
688
  break;
499
689
  case "BoxAction":
500
690
  if (!replay.isInit) {
501
- visit(replay, expression.action, items, scope, partial);
502
- visitUnchanged(replay, expression.subscript, items, scope, partial);
503
- return;
691
+ const isProduced = visit(replay, expression.action, items, scope, partial, node);
692
+ return (
693
+ visitUnchanged(replay, expression.subscript, items, scope, partial, node) || isProduced
694
+ );
504
695
  }
505
696
  break;
506
697
  case "AngleAction":
507
698
  if (!replay.isInit) {
508
- visit(
699
+ return visit(
509
700
  replay,
510
701
  expression.action,
511
- createItem("Changed", expression.subscript, scope, items),
702
+ createItem("Changed", expression.subscript, scope, node, items),
512
703
  scope,
513
704
  partial,
705
+ node,
514
706
  );
515
- return;
516
707
  }
517
708
  break;
518
709
  }
519
- visitValue(replay, evaluate(expression, context), items, partial);
710
+ const failure = replay.isEnabled ? getEnabledFailure(expression) : undefined;
711
+ if (failure !== undefined) throw new TlaEvaluationError(failure);
712
+ return visitValue(replay, evaluateAt(replay, node, expression, context), items, partial, node);
520
713
  };
521
714
 
522
- // TLC's processUnchanged.
715
+ export const warnChangedUnchanged = (
716
+ environment: Environment,
717
+ index: number,
718
+ expression: Expression,
719
+ ) =>
720
+ environment.runtime.warn(
721
+ MESSAGE_CODE.unchangedVariableChanged,
722
+ `The variable ${environment.variables[index]} was changed while it is specified as UNCHANGED at\n${describeRange(expression)}`,
723
+ );
724
+
523
725
  const visitUnchanged = (
524
726
  replay: Replay,
525
727
  expression: Expression,
526
728
  items: ActionItem | undefined,
527
729
  scope: Scope | undefined,
528
730
  partial: State,
529
- ): void =>
731
+ parent: number,
732
+ ): boolean =>
530
733
  withFrame(replay, expression, () => {
531
- const index = getVariable(replay, expression, scope);
734
+ const node = getNode(replay, parent, expression);
735
+ const index = getVariable(replay.environment, expression, scope);
532
736
  if (index !== undefined) {
533
737
  const value = replay.current?.[index];
534
- if (value !== undefined) proceedWithValue(replay, items, partial, index, value);
535
- return;
738
+ if (value === undefined) return false;
739
+ const assigned = partial[index];
740
+ if (assigned !== undefined && !isEqualLikeTlc(assigned, value)) {
741
+ warnChangedUnchanged(replay.environment, index, expression);
742
+ return false;
743
+ }
744
+ return proceedWithValue(replay, items, partial, index, value, node);
536
745
  }
537
746
  if (expression.kind === "Tuple") {
538
747
  const [first, ...others] = expression.elements;
539
- if (first === undefined) {
540
- proceed(replay, items, partial);
541
- return;
542
- }
748
+ if (first === undefined) return proceed(replay, items, partial, node);
543
749
  const rest = others.reduceRight<ActionItem | undefined>(
544
- (tail, element) => createItem("Unchanged", element, scope, tail),
750
+ (tail, element) => createItem("Unchanged", element, scope, node, tail),
545
751
  items,
546
752
  );
547
- visitUnchanged(replay, first, rest, scope, partial);
548
- return;
753
+ return visitUnchanged(replay, first, rest, scope, partial, node);
549
754
  }
550
755
  if (expression.kind === "Identifier") {
551
756
  const binding = lookupScope(scope, expression.name);
552
757
  if (binding?.kind === "Thunk") {
553
- visitUnchanged(replay, binding.expression, items, binding.scope, partial);
554
- return;
758
+ return visitUnchanged(replay, binding.expression, items, binding.scope, partial, node);
555
759
  }
556
760
  const operator = binding
557
761
  ? undefined
558
762
  : resolveGlobalOperator(expression.name, replay.environment);
559
763
  if (operator?.kind === "Closure" && operator.params.length === 0) {
560
- visitUnchanged(replay, operator.body, items, operator.scope, partial);
561
- return;
764
+ return visitUnchanged(replay, operator.body, items, operator.scope, partial, node);
765
+ }
766
+ }
767
+ // Tool#enabledUnchanged evaluates an operator application in the partial next state, which
768
+ // fails on a variable it hasn't assigned, as its comment says. tla-ts walks the operator's
769
+ // body as it walks a definition without parameters.
770
+ if (replay.isEnabled && expression.kind === "Apply") {
771
+ const context = getContext(replay, scope, partial);
772
+ const operator = resolveApplyOperator(expression, context);
773
+ if (operator?.kind === "Closure" && operator.params.length === expression.args.length) {
774
+ return visitUnchanged(
775
+ replay,
776
+ operator.body,
777
+ items,
778
+ bindOperatorArguments(operator, expression.args, context),
779
+ partial,
780
+ node,
781
+ );
562
782
  }
563
783
  }
564
784
  const environment = replay.environment;
565
- const before = evaluate(expression, {
785
+ const before = evaluateAt(replay, node, expression, {
566
786
  environment,
567
787
  scope,
568
788
  state: replay.current,
569
789
  next: undefined,
570
790
  });
571
- const after = evaluate(expression, { environment, scope, state: partial, next: undefined });
572
- if (valuesEqual(before, after)) proceed(replay, items, partial);
791
+ const after = evaluateAt(replay, node, expression, {
792
+ environment,
793
+ scope,
794
+ state: partial,
795
+ next: undefined,
796
+ });
797
+ return isEqualLikeTlc(before, after) && proceed(replay, items, partial, node);
573
798
  });
574
799
 
575
800
  const createReplay = (
@@ -578,16 +803,20 @@ const createReplay = (
578
803
  current: Value[] | undefined,
579
804
  onState: (state: Value[]) => void,
580
805
  isEnabled = false,
581
- ): Replay => {
582
- const callStack = environment.callStack;
583
- if (!callStack) throw new Error("The replay needs an environment that records its call stack");
584
- return { environment, callStack, isInit, isEnabled, current, onState };
585
- };
806
+ onIncomplete?: (state: State) => void,
807
+ ): Replay => ({
808
+ environment,
809
+ callStack: environment.callStack,
810
+ coverage: environment.coverage,
811
+ isInit,
812
+ isEnabled,
813
+ current,
814
+ onState,
815
+ onIncomplete,
816
+ });
586
817
 
587
818
  const ENABLED_FOUND = new Error("The replay found a successor");
588
819
 
589
- // TLC's Tool#enabled, which walks the action as getNextStates does and stops at the first
590
- // successor.
591
820
  export const replayEnabled = (
592
821
  expression: Expression,
593
822
  scope: Scope | undefined,
@@ -603,57 +832,180 @@ export const replayEnabled = (
603
832
  },
604
833
  true,
605
834
  );
606
- const depth = replay.callStack.expressions.length;
835
+ const callStack = replay.callStack;
836
+ const depth = callStack?.expressions.length ?? 0;
607
837
  try {
608
- // The ENABLED of a fairness condition WF_v(A) passes A to TLC's enabled with v to change, where
609
- // liveness.ts builds <<A>>_v at the location of A.
838
+ // liveness.ts builds the <<A>>_v of WF_v(A) at the location of A.
610
839
  if (expression.kind === "AngleAction" && expression.location === expression.action.location) {
611
- const changed = createItem("Changed", expression.subscript, scope, undefined);
612
- visit(replay, expression.action, changed, scope, getEmptyState(environment));
840
+ const changed = createItem("Changed", expression.subscript, scope, -1, undefined);
841
+ visit(replay, expression.action, changed, scope, getEmptyState(environment), -1);
613
842
  } else {
614
- visit(replay, expression, undefined, scope, getEmptyState(environment));
843
+ visit(replay, expression, undefined, scope, getEmptyState(environment), -1);
615
844
  }
616
845
  return false;
617
846
  } catch (error) {
618
847
  if (error !== ENABLED_FOUND) throw error;
619
848
  // The frames the walk left are those of the successor, not of a failure.
620
- replay.callStack.expressions.length = depth;
621
- replay.callStack.isFrozen = false;
849
+ if (callStack) {
850
+ callStack.expressions.length = depth;
851
+ callStack.isFrozen = false;
852
+ }
622
853
  return true;
623
854
  }
624
855
  };
625
856
 
626
- // TLC's getInitStates, which conjoins the initial predicates of the specification.
627
857
  export const replayInitialStates = (
628
- inits: ReplayAction[],
858
+ inits: (ReplayAction & { node?: number })[],
629
859
  environment: Environment,
630
860
  onState: (state: Value[]) => void,
861
+ onIncomplete?: (state: State) => void,
631
862
  ) => {
632
863
  const [first, ...others] = inits;
633
864
  if (first === undefined) return;
634
- const replay = createReplay(environment, true, undefined, onState);
865
+ const replay = createReplay(environment, true, undefined, onState, false, onIncomplete);
635
866
  const rest = others.reduceRight<ActionItem | undefined>(
636
- (tail, init) => createItem("Predicate", init.expression, init.scope, tail),
867
+ (tail, init) => createItem("Predicate", init.expression, init.scope, init.node ?? -1, tail),
637
868
  undefined,
638
869
  );
639
- visit(replay, first.expression, rest, first.scope, getEmptyState(environment));
870
+ visit(replay, first.expression, rest, first.scope, getEmptyState(environment), first.node ?? -1);
640
871
  };
641
872
 
642
- // TLC's getNextStates for each action TLC splits the next-state relation into. TLC checks the
643
- // successors of an action once it has computed them all.
873
+ // TLC checks the successors of an action once it has computed them all.
644
874
  export const replaySuccessors = <Action extends ReplayAction>(
645
875
  actions: Action[],
646
876
  state: Value[],
647
877
  environment: Environment,
648
878
  onState: (state: Value[], action: Action) => void,
649
879
  ) => {
650
- // TLC fails to split a next-state relation that is not an operator application before it checks
651
- // the spec, so it replays nothing.
880
+ // TLC fails to split a next-state relation that isn't an operator application, so replays none.
652
881
  if (actions.some((action) => LITERAL_KINDS.has(action.expression.kind))) return;
653
882
  actions.forEach((action) => {
654
883
  const successors: Value[][] = [];
655
- const replay = createReplay(environment, false, state, (next) => successors.push(next));
656
- visit(replay, action.expression, undefined, action.scope, getEmptyState(environment));
884
+ walkSuccessors(action, -1, state, environment, (next) => successors.push(next));
657
885
  successors.forEach((successor) => onState(successor, action));
658
886
  });
659
887
  };
888
+
889
+ export const walkSuccessors = (
890
+ action: ReplayAction,
891
+ node: number,
892
+ state: Value[],
893
+ environment: Environment,
894
+ onState: (state: Value[]) => void,
895
+ onIncomplete?: (state: State) => void,
896
+ ) => {
897
+ const replay = createReplay(environment, false, state, onState, false, onIncomplete);
898
+ visit(replay, action.expression, undefined, action.scope, getEmptyState(environment), node);
899
+ };
900
+
901
+ // TLC's Spec.getPrimedLocs, whose nodes TLC reports even when they never produce a state.
902
+ export const getPrimedRanges = (actions: ReplayAction[], environment: Environment) => {
903
+ const ranges: Expression[] = [];
904
+ const context = (scope: Scope | undefined): Context => ({
905
+ environment,
906
+ scope,
907
+ state: undefined,
908
+ next: undefined,
909
+ });
910
+ const collectUnchanged = (expression: Expression, scope: Scope | undefined): void => {
911
+ if (
912
+ getVariable(environment, expression, undefined) !== undefined &&
913
+ expression.kind === "Identifier"
914
+ ) {
915
+ ranges.push(expression);
916
+ return;
917
+ }
918
+ if (expression.kind === "Tuple") {
919
+ expression.elements.forEach((element) => collectUnchanged(element, scope));
920
+ return;
921
+ }
922
+ if (expression.kind !== "Identifier") return;
923
+ const binding = lookupScope(scope, expression.name);
924
+ const operator = binding ? undefined : resolveGlobalOperator(expression.name, environment);
925
+ if (operator?.kind === "Closure" && operator.params.length === 0) {
926
+ collectUnchanged(operator.body, operator.scope);
927
+ }
928
+ };
929
+ const collect = (
930
+ expression: Expression,
931
+ scope: Scope | undefined,
932
+ visited: Set<Expression>,
933
+ ): void => {
934
+ switch (expression.kind) {
935
+ case "Let":
936
+ collect(expression.body, bindDefinitions(expression.definitions, context(scope)), visited);
937
+ return;
938
+ case "Label":
939
+ collect(expression.body, scope, visited);
940
+ return;
941
+ case "FunctionApplication":
942
+ collect(expression.function, scope, visited);
943
+ return;
944
+ case "If":
945
+ collect(expression.consequent, scope, visited);
946
+ collect(expression.alternative, scope, visited);
947
+ return;
948
+ case "Case":
949
+ expression.arms.forEach((arm) => collect(arm.body, scope, visited));
950
+ if (expression.other) collect(expression.other, scope, visited);
951
+ return;
952
+ case "And":
953
+ case "Or":
954
+ expression.operands.forEach((operand) => collect(operand, scope, visited));
955
+ return;
956
+ case "Exists":
957
+ case "Forall":
958
+ collect(expression.body, scope, visited);
959
+ return;
960
+ case "Unchanged":
961
+ collectUnchanged(expression.expression, scope);
962
+ return;
963
+ case "AngleAction":
964
+ collect(expression.action, scope, visited);
965
+ return;
966
+ case "BoxAction":
967
+ collect(expression.action, scope, visited);
968
+ ranges.push(expression.subscript);
969
+ return;
970
+ case "Identifier":
971
+ case "Apply": {
972
+ if (expression.kind === "Apply") {
973
+ if (expression.operator === "=>") {
974
+ expression.args.forEach((arg) => collect(arg, scope, visited));
975
+ return;
976
+ }
977
+ if (expression.operator === "=" || expression.operator === "\\in") {
978
+ if (getPrimedVariable(environment, expression.args[0], scope) !== undefined) {
979
+ ranges.push(expression);
980
+ }
981
+ return;
982
+ }
983
+ }
984
+ const name = expression.kind === "Identifier" ? expression.name : expression.operator;
985
+ const binding = lookupScope(scope, name);
986
+ if (binding?.kind === "Thunk") {
987
+ collect(binding.expression, binding.scope, visited);
988
+ return;
989
+ }
990
+ const operator =
991
+ expression.kind === "Apply"
992
+ ? resolveApplyOperator(expression, context(scope))
993
+ : binding?.kind === "Operator"
994
+ ? binding.operator
995
+ : binding
996
+ ? undefined
997
+ : resolveGlobalOperator(name, environment);
998
+ if (operator?.kind !== "Closure" || visited.has(operator.body)) return;
999
+ const definition = environment.definitions.get(name);
1000
+ if (definition?.isRecursive) return;
1001
+ const args = expression.kind === "Apply" ? expression.args : [];
1002
+ if (operator.params.length !== args.length) return;
1003
+ visited.add(operator.body);
1004
+ collect(operator.body, bindOperatorArguments(operator, args, context(scope)), visited);
1005
+ visited.delete(operator.body);
1006
+ }
1007
+ }
1008
+ };
1009
+ actions.forEach((action) => collect(action.expression, action.scope, new Set()));
1010
+ return ranges.flatMap((expression) => expression.location.range ?? []);
1011
+ };