tla-ts 0.0.3 → 0.0.4-dev.2be6058

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 (86) hide show
  1. package/README.md +32 -12
  2. package/dist/cli.cjs +1 -1
  3. package/dist/cli.js +1 -1
  4. package/dist/index.cjs +1 -1
  5. package/dist/index.d.cts +160 -8
  6. package/dist/index.d.ts +160 -8
  7. package/dist/index.js +1 -1
  8. package/dist/parallel-worker.cjs +2 -2
  9. package/dist/parallel-worker.js +2 -2
  10. package/dist/parallel.cjs +1 -1
  11. package/dist/parallel.js +2 -2
  12. package/dist/run-cli.cjs +45 -18
  13. package/dist/run-cli.js +45 -18
  14. package/dist/transfer.cjs +1393 -18
  15. package/dist/transfer.js +1393 -18
  16. package/package.json +5 -1
  17. package/src/ast.ts +147 -6
  18. package/src/bags.ts +4 -7
  19. package/src/builtins.ts +29 -11
  20. package/src/bundled-modules.ts +3 -3
  21. package/src/canonical.ts +4 -3
  22. package/src/check.ts +1539 -235
  23. package/src/cli-options.ts +11 -5
  24. package/src/cli.ts +8 -1
  25. package/src/codegen.ts +77 -37
  26. package/src/config.ts +254 -181
  27. package/src/dependencies.ts +2 -2
  28. package/src/environment.ts +540 -140
  29. package/src/errors.ts +224 -3
  30. package/src/evaluate.ts +797 -246
  31. package/src/exit-codes.ts +22 -43
  32. package/src/fingerprints.ts +46 -11
  33. package/src/index.ts +1 -0
  34. package/src/instances.ts +19 -16
  35. package/src/java-overrides.ts +104 -25
  36. package/src/levels.ts +191 -45
  37. package/src/lexer.ts +127 -30
  38. package/src/live-check.ts +867 -0
  39. package/src/liveness.ts +218 -572
  40. package/src/obligation-expression.ts +2 -2
  41. package/src/obligation-source.ts +2 -0
  42. package/src/parallel-worker.ts +174 -213
  43. package/src/parallel.ts +131 -39
  44. package/src/parser.ts +456 -147
  45. package/src/pcal-ast.ts +770 -0
  46. package/src/pcal-parse.ts +1523 -0
  47. package/src/pcal-tokenize.ts +516 -0
  48. package/src/pcal-translate.ts +1230 -0
  49. package/src/pcal-validate.ts +454 -0
  50. package/src/randomization.ts +24 -7
  51. package/src/resolve.ts +1481 -426
  52. package/src/run-cli.ts +1212 -143
  53. package/src/runtime.ts +19 -1
  54. package/src/sany-builtins.ts +147 -0
  55. package/src/sany-generator.ts +3128 -0
  56. package/src/sany-levels.ts +1208 -0
  57. package/src/sany-lexer.ts +545 -0
  58. package/src/sany-linter.ts +122 -0
  59. package/src/sany-modules.ts +1398 -0
  60. package/src/sany-nodes.ts +463 -0
  61. package/src/sany-parse-error.ts +2161 -0
  62. package/src/sany-report.ts +356 -0
  63. package/src/sany-selectors.ts +738 -0
  64. package/src/sany-source.ts +248 -0
  65. package/src/sany-xml.ts +1017 -0
  66. package/src/spec-description.ts +42 -11
  67. package/src/standard-modules.ts +200 -148
  68. package/src/standard-ranges.ts +116 -0
  69. package/src/state-graph.ts +180 -0
  70. package/src/subexpressions.ts +231 -171
  71. package/src/substitution-levels.ts +38 -17
  72. package/src/successors.ts +219 -107
  73. package/src/symmetry.ts +194 -54
  74. package/src/tableau.ts +54 -25
  75. package/src/tlc-ext.ts +42 -29
  76. package/src/tlc-messages.ts +329 -0
  77. package/src/tlc-module.ts +52 -43
  78. package/src/tlc-modules.ts +4 -0
  79. package/src/tlc-order.ts +78 -10
  80. package/src/tlc-random.ts +37 -9
  81. package/src/tlc-replay.ts +655 -0
  82. package/src/tlc-summary.ts +20 -5
  83. package/src/tlc-version.ts +18 -0
  84. package/src/transfer.ts +15 -3
  85. package/src/value-sources.ts +29 -0
  86. package/src/values.ts +731 -184
package/src/successors.ts CHANGED
@@ -1,10 +1,19 @@
1
- import type { Bound, Definition, Expression } from "./ast";
1
+ import {
2
+ getChildren,
3
+ isLocallyResolved,
4
+ type Definition,
5
+ type Expression,
6
+ type QuantifierExpression,
7
+ } from "./ast";
8
+ import { getKindString } from "./builtins";
2
9
  import { getReadVariables } from "./dependencies";
3
- import { TlaEvaluationError } from "./errors";
10
+ import { TlaEvaluationError, TlaIllegalStateError, TlaIncompleteSuccessorError } from "./errors";
4
11
  import { EXIT_STATUS, setExitCode } from "./exit-codes";
5
12
  import {
6
13
  describeLocation,
7
- bindArgument,
14
+ describeNode,
15
+ describeRange,
16
+ bindLazyArguments,
8
17
  bindDefinitions,
9
18
  bindOperatorArguments,
10
19
  bindValue,
@@ -12,27 +21,27 @@ import {
12
21
  evaluate,
13
22
  expectBoolean,
14
23
  forEachBinding,
15
- getBoundsDomain,
16
- getChildren,
24
+ getLazyFunction,
25
+ toKeepLazyContext,
17
26
  getDefinition,
18
- getEnumeratedElements,
27
+ getContextElements,
19
28
  getReevaluatedDefinitions,
20
29
  lookupScope,
21
30
  resolveApplyOperator,
22
31
  resolveGlobalOperator,
23
- isLocallyResolved,
32
+ toUnspecifiedSubscriptError,
24
33
  type Context,
25
34
  type Environment,
26
35
  type Scope,
27
36
  type State,
28
37
  } from "./evaluate";
29
38
  import { mapChildren } from "./instances";
39
+ import { isAssignedShortcut, LITERAL_KINDS, replayEnabled } from "./tlc-replay";
30
40
  import {
31
- createTuple,
32
41
  formatValue,
33
42
  getStreamedElements,
34
- isElementOf,
35
- isEnumerable,
43
+ isFunctionValue,
44
+ isTlcEnumerable,
36
45
  valuesEqual,
37
46
  type Value,
38
47
  } from "./values";
@@ -45,6 +54,8 @@ export interface Successor {
45
54
  interface Run {
46
55
  environment: Environment;
47
56
  isInit: boolean;
57
+ // Whether the run computes ENABLED rather than the successors of the state.
58
+ isEnabled: boolean;
48
59
  current: Value[] | undefined;
49
60
  isStopped: boolean;
50
61
  }
@@ -81,15 +92,47 @@ const assign = (partial: State, index: number, value: Value) => {
81
92
  return copy;
82
93
  };
83
94
 
84
- const holds = (expression: Expression, context: Context) => {
95
+ // What TLC says it was computing when it fails.
96
+ const getComputation = (run: Run) =>
97
+ run.isInit ? "initial states" : run.isEnabled ? "ENABLED" : "next states";
98
+
99
+ // TLC fails with an unexpected exception when it fails to compute the next states or ENABLED.
100
+ const createRunError = (run: Run, message: string) => {
101
+ const error = new TlaEvaluationError(message);
102
+ return run.isInit ? error : setExitCode(error, EXIT_STATUS.error);
103
+ };
104
+
105
+ // TLC's message for a conjunct that isn't a boolean, which differs for a literal it walks to rather
106
+ // than evaluates.
107
+ const getNonBooleanMessage = (
108
+ run: Run,
109
+ value: Value,
110
+ expression: Expression,
111
+ isWalked: boolean,
112
+ ) => {
113
+ const node = describeNode(expression);
114
+ if (isWalked && LITERAL_KINDS.has(expression.kind)) {
115
+ return run.isEnabled
116
+ ? `Attempted to compute ENABLED on a non-boolean expression.\n${node}`
117
+ : `The ${run.isInit ? "init" : "next"} state relation is not a boolean expression.\n${node}`;
118
+ }
119
+ return `In computing ${getComputation(run)}, TLC expected a boolean expression,\nbut instead found ${formatValue(value)}.\n${node}`;
120
+ };
121
+
122
+ // TLC words an IF whose condition isn't a boolean differently for each computation.
123
+ const getIfConditionMessage = (run: Run, value: Value, expression: Expression) => {
124
+ const kind = getKindString(value);
125
+ const range = describeRange(expression);
126
+ if (run.isEnabled) {
127
+ return `In computing ENABLED, a non-boolean expression(${kind}) was used as the guard condition of an IF.\n${range}`;
128
+ }
129
+ return `In computing ${getComputation(run)}, a non-boolean expression (${kind}) was used as the condition of an IF.${run.isInit ? "\n" : ""}${range}`;
130
+ };
131
+
132
+ const holds = (run: Run, expression: Expression, context: Context, isWalked = true) => {
85
133
  const value = evaluate(expression, context);
86
134
  if (typeof value === "boolean") return value;
87
- try {
88
- return expectBoolean(value, `the action at ${describeLocation(expression)}`);
89
- } catch (error) {
90
- // TLC fails with an unexpected exception when the next-state relation isn't a boolean.
91
- throw context.next === undefined ? error : setExitCode(error, EXIT_STATUS.error);
92
- }
135
+ throw createRunError(run, getNonBooleanMessage(run, value, expression, isWalked));
93
136
  };
94
137
 
95
138
  const isAssignmentOperator = (operator: string) =>
@@ -287,6 +330,27 @@ const toConjunct = (
287
330
  return { kind: "Branch", expression, runner: getRunner(expression, environment, isInit) };
288
331
  };
289
332
 
333
+ // The leading guards of `\E x \in S, y \in T : P /\ Q` that read no inner bound hold for every inner
334
+ // binding alike, so they can run once for each element of the outermost bound.
335
+ const getHoistedGuards = (expression: QuantifierExpression, environment: Environment) => {
336
+ if (expression.bounds.length < 2 || expression.body.kind !== "And") return undefined;
337
+ const names = expression.bounds.flatMap((bound) =>
338
+ bound.pattern.kind === "Name" ? [bound.pattern.name] : [],
339
+ );
340
+ if (names.length !== expression.bounds.length) return undefined;
341
+ const innerNames = new Set(names.slice(0, -1));
342
+ const operands = expression.body.operands;
343
+ const index = operands.findIndex((operand) => {
344
+ if (getReadVariables(operand, environment) === undefined) return true;
345
+ if (hasBranches(operand, environment, new Set())) return true;
346
+ const readLocals = getReadLocals(operand, NO_NAMES);
347
+ return !readLocals || readLocals.some((name) => innerNames.has(name));
348
+ });
349
+ const count = index === -1 ? operands.length : index;
350
+ if (count === 0) return undefined;
351
+ return { names, guards: operands.slice(0, count), rest: operands.slice(count) };
352
+ };
353
+
290
354
  const compileConjunction = (
291
355
  operands: Expression[],
292
356
  environment: Environment,
@@ -303,11 +367,12 @@ const compileConjunction = (
303
367
  ): void => {
304
368
  let current = partial;
305
369
  let context: Context | undefined;
370
+ const isShortcut = isAssignedShortcut(run);
306
371
  for (let index = start; index < conjuncts.length; index++) {
307
- if (index > 0 && run.isInit && isComplete(current)) {
372
+ if (index > 0 && isShortcut && isComplete(current)) {
308
373
  context ??= getContext(run, scope, current);
309
374
  for (let rest = index; rest < conjuncts.length; rest++) {
310
- if (!holdsInInit(conjuncts[rest].expression, context)) return;
375
+ if (!holds(run, conjuncts[rest].expression, context, false)) return;
311
376
  }
312
377
  onComplete(current, action);
313
378
  return;
@@ -315,7 +380,7 @@ const compileConjunction = (
315
380
  const conjunct = conjuncts[index];
316
381
  if (conjunct.kind === "Guard") {
317
382
  context ??= getContext(run, scope, current);
318
- if (!holds(conjunct.expression, context)) return;
383
+ if (!holds(run, conjunct.expression, context)) return;
319
384
  continue;
320
385
  }
321
386
  if (conjunct.kind === "Assign") {
@@ -356,16 +421,10 @@ const compileConjunction = (
356
421
  runFrom(0, run, scope, partial, action, onComplete);
357
422
  };
358
423
 
359
- const holdsInInit = (expression: Expression, context: Context) =>
360
- expectBoolean(
361
- evaluate(expression, context),
362
- `the initial predicate at ${describeLocation(expression)}`,
363
- );
364
-
365
424
  const compileGuard =
366
425
  (expression: Expression): Runner =>
367
426
  (run, scope, partial, action, _isFixed, onComplete) => {
368
- if (holds(expression, getContext(run, scope, partial))) onComplete(partial, action);
427
+ if (holds(run, expression, getContext(run, scope, partial))) onComplete(partial, action);
369
428
  };
370
429
 
371
430
  const compileMembership = (
@@ -377,16 +436,17 @@ const compileMembership = (
377
436
  const index = getIndex(run, scope, partial);
378
437
  const context = getContext(run, scope, partial);
379
438
  if (partial[index] !== undefined) {
380
- if (holds(expression, context)) onComplete(partial, action);
439
+ if (holds(run, expression, context)) onComplete(partial, action);
381
440
  return;
382
441
  }
383
442
  const evaluated = evaluate(set, context);
384
443
  const value: Value =
385
444
  expression.operator === "\\subseteq" ? { kind: "Subset", base: evaluated } : evaluated;
386
- if (!isEnumerable(value)) {
387
- throw new TlaEvaluationError(
388
- `In computing next states, the right side of \\in is not enumerable: ${formatValue(value)} at ${describeLocation(expression)}`,
389
- );
445
+ if (!isTlcEnumerable(value)) {
446
+ const message = run.isEnabled
447
+ ? "The right side of \\IN is not enumerable."
448
+ : `In computing ${getComputation(run)}, the right side of \\IN is not enumerable.`;
449
+ throw createRunError(run, `${message}\n${describeRange(expression)}`);
390
450
  }
391
451
  const elements = getStreamedElements(value);
392
452
  for (let position = 0; position < elements.length && !run.isStopped; position++) {
@@ -436,65 +496,43 @@ const compileDynamicAssignment = (
436
496
  };
437
497
  };
438
498
 
439
- interface FunctionAction {
440
- bounds: Bound[];
441
- body: Expression;
442
- scope: Scope | undefined;
443
- }
444
-
445
- const getFunctionAction = (
446
- expression: Expression,
447
- scope: Scope | undefined,
448
- partial: State,
449
- run: Run,
450
- ): FunctionAction | undefined => {
451
- if (expression.kind === "FunctionConstruction") {
452
- return { bounds: expression.bounds, body: expression.body, scope };
453
- }
454
- if (expression.kind !== "Identifier") return undefined;
455
- const binding = lookupScope(scope, expression.name);
456
- if (binding?.kind === "Thunk") {
457
- return getFunctionAction(binding.expression, binding.scope, partial, run);
458
- }
459
- const definition = binding ? undefined : getDefinition(expression.name, run.environment);
460
- if (definition?.kind !== "Function") return undefined;
461
- const self = evaluate(expression, getContext(run, undefined, partial));
462
- return {
463
- bounds: definition.bounds,
464
- body: definition.body,
465
- scope: bindValue(undefined, definition.name, self),
466
- };
467
- };
468
-
499
+ // Tool.getInitStates and Tool.getNextStates walk the body of a function they apply that is still
500
+ // lazy, which they evaluate as TLC computes the initial states eagerly, and otherwise apply it.
469
501
  const compileFunctionAction = (
470
502
  expression: Expression & { kind: "FunctionApplication" },
471
503
  environment: Environment,
472
504
  isInit: boolean,
473
505
  ): Runner => {
474
- const guard = compileGuard(expression);
506
+ const node = describeRange(expression);
475
507
  return (run, scope, partial, action, isFixed, onComplete) => {
476
- const lambda = getFunctionAction(expression.function, scope, partial, run);
477
- if (!lambda) {
478
- guard(run, scope, partial, action, isFixed, onComplete);
508
+ const context = getContext(run, scope, partial);
509
+ const value = evaluate(expression.function, isInit ? context : toKeepLazyContext(context));
510
+ const lambda = getLazyFunction(value);
511
+ if (lambda !== undefined) {
512
+ getRunner(lambda.body, environment, isInit)(
513
+ run,
514
+ bindLazyArguments(lambda, expression, context),
515
+ partial,
516
+ action,
517
+ isFixed,
518
+ onComplete,
519
+ );
479
520
  return;
480
521
  }
481
- const context = getContext(run, scope, partial);
482
- const args = expression.args.map((arg) => evaluate(arg, context));
483
- const argument = args.length === 1 ? args[0] : createTuple(args);
484
- const domain = getBoundsDomain(lambda.bounds, getContext(run, lambda.scope, partial));
485
- if (!isElementOf(argument, domain)) {
486
- throw new TlaEvaluationError(
487
- `Attempted to apply a function to ${formatValue(argument)}, which is not in its domain at ${describeLocation(expression)}`,
522
+ if (!isFunctionValue(value)) {
523
+ throw createRunError(
524
+ run,
525
+ `In computing ${getComputation(run)}, a non-function (${getKindString(value)}) was applied as a function.\n${node}`,
488
526
  );
489
527
  }
490
- getRunner(lambda.body, environment, isInit)(
491
- run,
492
- bindArgument(lambda.bounds, lambda.scope, argument),
493
- partial,
494
- action,
495
- isFixed,
496
- onComplete,
497
- );
528
+ const result = evaluate(expression, context);
529
+ if (typeof result !== "boolean") {
530
+ throw createRunError(
531
+ run,
532
+ `In computing ${getComputation(run)}, TLC expected a boolean expression,\nbut didn't find one.\n${node}`,
533
+ );
534
+ }
535
+ if (result) onComplete(partial, action);
498
536
  };
499
537
  };
500
538
 
@@ -518,7 +556,7 @@ const compileUnchanged = (expression: Expression, environment: Environment): Run
518
556
  );
519
557
  return;
520
558
  }
521
- if (holds(unchanged, getContext(run, scope, partial))) onComplete(partial, action);
559
+ if (holds(run, unchanged, getContext(run, scope, partial))) onComplete(partial, action);
522
560
  };
523
561
  };
524
562
 
@@ -713,9 +751,10 @@ const compileAction = (
713
751
  if (expression.bounds.length === 1 && bound.pattern.kind === "Name") {
714
752
  const name = bound.pattern.name;
715
753
  return (run, scope, partial, action, isFixed, onComplete) => {
716
- const elements = getEnumeratedElements(
754
+ const elements = getContextElements(
717
755
  evaluate(bound.set, getContext(run, scope, partial)),
718
756
  bound.set,
757
+ environment.runtime.version,
719
758
  );
720
759
  for (let index = 0; index < elements.length && !run.isStopped; index++) {
721
760
  body(
@@ -729,6 +768,37 @@ const compileAction = (
729
768
  }
730
769
  };
731
770
  }
771
+ const hoisted = isInit ? undefined : getHoistedGuards(expression, environment);
772
+ if (hoisted) {
773
+ const rest = compileConjunction(hoisted.rest, environment, isInit);
774
+ return (run, scope, partial, action, isFixed, onComplete) => {
775
+ const context = getContext(run, scope, partial);
776
+ const sets = expression.bounds.map((innerBound) =>
777
+ getContextElements(
778
+ evaluate(innerBound.set, context),
779
+ innerBound.set,
780
+ environment.runtime.version,
781
+ ),
782
+ );
783
+ if (sets.some((elements) => elements.length === 0)) return;
784
+ const visit = (index: number, boundScope: Scope | undefined): void => {
785
+ if (index < 0) {
786
+ rest(run, boundScope, partial, action, isFixed, onComplete);
787
+ return;
788
+ }
789
+ for (const element of sets[index]) {
790
+ if (run.isStopped) return;
791
+ const elementScope = bindValue(boundScope, hoisted.names[index], element);
792
+ if (index === sets.length - 1) {
793
+ const guardContext = getContext(run, elementScope, partial);
794
+ if (!hoisted.guards.every((guard) => holds(run, guard, guardContext))) continue;
795
+ }
796
+ visit(index - 1, elementScope);
797
+ }
798
+ };
799
+ visit(sets.length - 1, scope);
800
+ };
801
+ }
732
802
  return (run, scope, partial, action, isFixed, onComplete) => {
733
803
  forEachBinding(expression.bounds, getContext(run, scope, partial), (boundScope) => {
734
804
  body(run, boundScope, partial, action, isFixed, onComplete);
@@ -756,7 +826,7 @@ const compileAction = (
756
826
  );
757
827
  if (isBoundToValues) {
758
828
  const isSatisfied = scopes.every((boundScope) =>
759
- holds(expression.body, getContext(run, boundScope, partial)),
829
+ holds(run, expression.body, getContext(run, boundScope, partial)),
760
830
  );
761
831
  if (isSatisfied) onComplete(partial, action);
762
832
  return;
@@ -770,7 +840,7 @@ const compileAction = (
770
840
  const isSatisfied = scopes
771
841
  .slice(index)
772
842
  .every((boundScope) =>
773
- holdsInInit(expression.body, getContext(run, boundScope, nextPartial)),
843
+ holds(run, expression.body, getContext(run, boundScope, nextPartial)),
774
844
  );
775
845
  if (isSatisfied) onComplete(nextPartial, nextAction);
776
846
  return;
@@ -787,24 +857,38 @@ const compileAction = (
787
857
  const alternative = child(expression.alternative);
788
858
  return (run, scope, partial, action, isFixed, onComplete) => {
789
859
  const condition = evaluate(expression.condition, getContext(run, scope, partial));
790
- const branch = expectBoolean(condition, "an IF condition") ? consequent : alternative;
860
+ if (typeof condition !== "boolean") {
861
+ throw createRunError(run, getIfConditionMessage(run, condition, expression));
862
+ }
863
+ const branch = condition ? consequent : alternative;
791
864
  branch(run, scope, partial, action, isFixed, onComplete);
792
865
  };
793
866
  }
794
867
  case "Case": {
795
868
  const arms = expression.arms.map((arm) => ({
796
869
  condition: arm.condition,
870
+ source: arm.body,
797
871
  body: child(arm.body),
798
872
  }));
799
873
  const other = expression.other ? child(expression.other) : undefined;
800
874
  return (run, scope, partial, action, isFixed, onComplete) => {
801
875
  const context = getContext(run, scope, partial);
802
- const arm = arms.find((candidate) =>
803
- expectBoolean(evaluate(candidate.condition, context), "a CASE guard"),
804
- );
876
+ const arm = arms.find((candidate) => {
877
+ const value = evaluate(candidate.condition, context);
878
+ if (typeof value === "boolean") return value;
879
+ // TLC leaves out the space before the kind under ENABLED.
880
+ throw createRunError(
881
+ run,
882
+ `In computing ${getComputation(run)}, a non-boolean expression${run.isEnabled ? "" : " "}(${getKindString(value)}) was used as a guard condition of a CASE.\n${describeNode(candidate.source)}`,
883
+ );
884
+ });
805
885
  const body = arm?.body ?? other;
806
- if (!body)
807
- throw new TlaEvaluationError(`No CASE guard is true at ${describeLocation(expression)}`);
886
+ if (!body) {
887
+ throw createRunError(
888
+ run,
889
+ `In computing ${getComputation(run)}, TLC encountered a CASE with no conditions true.\n${describeRange(expression)}`,
890
+ );
891
+ }
808
892
  body(run, scope, partial, action, isFixed, onComplete);
809
893
  };
810
894
  }
@@ -833,10 +917,18 @@ const compileAction = (
833
917
  case "AngleAction": {
834
918
  const body = child(expression.action);
835
919
  const unchanged = getUnchangedWrapper(expression.subscript);
920
+ const isUnchanged = (run: Run, scope: Scope | undefined, nextPartial: State) => {
921
+ try {
922
+ return holds(run, unchanged, getContext(run, scope, nextPartial));
923
+ } catch (error) {
924
+ throw run.isEnabled
925
+ ? toUnspecifiedSubscriptError(error, environment.runtime.version)
926
+ : error;
927
+ }
928
+ };
836
929
  return (run, scope, partial, action, isFixed, onComplete) =>
837
930
  body(run, scope, partial, action, isFixed, (nextPartial, nextAction) => {
838
- if (!holds(unchanged, getContext(run, scope, nextPartial)))
839
- onComplete(nextPartial, nextAction);
931
+ if (!isUnchanged(run, scope, nextPartial)) onComplete(nextPartial, nextAction);
840
932
  });
841
933
  }
842
934
  case "BoxAction":
@@ -913,18 +1005,27 @@ export const isEnabled = (expression: Expression, context: Context): boolean =>
913
1005
  );
914
1006
  }
915
1007
  const environment = context.environment;
916
- const found = { value: false };
1008
+ // TLC computes ENABLED again with its call stack recorded as it computes successors.
1009
+ if (environment.callStack) return replayEnabled(expression, context.scope, state, environment);
1010
+ const run: Run = {
1011
+ environment,
1012
+ isInit: false,
1013
+ isEnabled: true,
1014
+ current: state,
1015
+ isStopped: false,
1016
+ };
1017
+ // TLC stops computing ENABLED at the first successor, so it skips the rest of the action.
917
1018
  getRunner(expression, environment, false)(
918
- { environment, isInit: false, current: state, isStopped: false },
1019
+ run,
919
1020
  context.scope,
920
1021
  getEmptyState(environment),
921
1022
  undefined,
922
1023
  false,
923
1024
  () => {
924
- found.value = true;
1025
+ run.isStopped = true;
925
1026
  },
926
1027
  );
927
- return found.value;
1028
+ return run.isStopped;
928
1029
  };
929
1030
 
930
1031
  export const forEachInitialState = (
@@ -933,7 +1034,13 @@ export const forEachInitialState = (
933
1034
  onState: (state: Value[]) => boolean,
934
1035
  scope?: Scope,
935
1036
  ) => {
936
- const run: Run = { environment, isInit: true, current: undefined, isStopped: false };
1037
+ const run: Run = {
1038
+ environment,
1039
+ isInit: true,
1040
+ isEnabled: false,
1041
+ current: undefined,
1042
+ isStopped: false,
1043
+ };
937
1044
  getRunner(expression, environment, true)(
938
1045
  run,
939
1046
  scope,
@@ -945,12 +1052,7 @@ export const forEachInitialState = (
945
1052
  if (!isComplete(partial)) {
946
1053
  // TLC reports an initial state that isn't completely specified as an illegal state, not as
947
1054
  // an evaluation failure.
948
- throw setExitCode(
949
- new TlaEvaluationError(
950
- `The initial predicate does not assign ${getMissingVariables(partial, environment).join(", ")}`,
951
- ),
952
- EXIT_STATUS.error,
953
- );
1055
+ throw setExitCode(new TlaIllegalStateError([...partial]), EXIT_STATUS.error);
954
1056
  }
955
1057
  run.isStopped = onState(partial);
956
1058
  },
@@ -982,7 +1084,13 @@ export const forEachSuccessor = (
982
1084
  onSuccessor: (successor: Successor) => boolean,
983
1085
  scope?: Scope,
984
1086
  ) => {
985
- const run: Run = { environment, isInit: false, current: state, isStopped: false };
1087
+ const run: Run = {
1088
+ environment,
1089
+ isInit: false,
1090
+ isEnabled: false,
1091
+ current: state,
1092
+ isStopped: false,
1093
+ };
986
1094
  const counter = { successors: 0 };
987
1095
  getRunner(expression, environment, false)(
988
1096
  run,
@@ -993,8 +1101,12 @@ export const forEachSuccessor = (
993
1101
  (partial, action) => {
994
1102
  if (run.isStopped) return;
995
1103
  if (!isComplete(partial)) {
996
- throw new TlaEvaluationError(
997
- `Successor state is not completely specified by action ${action ?? "Next"}. The following variables are not assigned: ${getMissingVariables(partial, environment).join(", ")}`,
1104
+ const missing = getMissingVariables(partial, environment);
1105
+ throw new TlaIncompleteSuccessorError(
1106
+ `Successor state is not completely specified by action ${action ?? "Next"}. The following variables are not assigned: ${missing.join(", ")}`,
1107
+ partial,
1108
+ action,
1109
+ missing,
998
1110
  );
999
1111
  }
1000
1112
  counter.successors++;