tla-ts 0.0.3-dev.c8c988d → 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.
Files changed (81) hide show
  1. package/dist/cli.cjs +1 -1
  2. package/dist/cli.js +1 -1
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.d.cts +163 -46
  5. package/dist/index.d.ts +163 -46
  6. package/dist/index.js +1 -1
  7. package/dist/parallel-worker.cjs +1 -1
  8. package/dist/parallel-worker.js +2 -2
  9. package/dist/parallel.cjs +1 -1
  10. package/dist/parallel.js +2 -2
  11. package/dist/run-cli.cjs +1351 -22
  12. package/dist/run-cli.js +1351 -22
  13. package/dist/transfer.cjs +83 -33
  14. package/dist/transfer.js +83 -33
  15. package/package.json +5 -1
  16. package/src/ast.ts +51 -6
  17. package/src/bags.ts +4 -7
  18. package/src/builtins.ts +29 -17
  19. package/src/bundled-modules.ts +3 -3
  20. package/src/canonical.ts +4 -3
  21. package/src/check.ts +1317 -428
  22. package/src/cli.ts +8 -1
  23. package/src/codegen.ts +68 -21
  24. package/src/config.ts +225 -206
  25. package/src/environment.ts +417 -129
  26. package/src/errors.ts +155 -18
  27. package/src/evaluate.ts +713 -259
  28. package/src/exit-codes.ts +6 -24
  29. package/src/fingerprints.ts +1 -2
  30. package/src/index.ts +1 -0
  31. package/src/instances.ts +3 -1
  32. package/src/java-overrides.ts +104 -25
  33. package/src/levels.ts +44 -28
  34. package/src/lexer.ts +32 -4
  35. package/src/live-check.ts +204 -148
  36. package/src/liveness.ts +191 -122
  37. package/src/obligation-expression.ts +2 -2
  38. package/src/obligation-source.ts +2 -0
  39. package/src/parallel-worker.ts +6 -1
  40. package/src/parallel.ts +57 -6
  41. package/src/parser.ts +405 -167
  42. package/src/pcal-ast.ts +770 -0
  43. package/src/pcal-parse.ts +1523 -0
  44. package/src/pcal-tokenize.ts +516 -0
  45. package/src/pcal-translate.ts +1230 -0
  46. package/src/pcal-validate.ts +454 -0
  47. package/src/randomization.ts +24 -7
  48. package/src/resolve.ts +1397 -457
  49. package/src/run-cli.ts +1161 -196
  50. package/src/runtime.ts +16 -0
  51. package/src/sany-builtins.ts +147 -0
  52. package/src/sany-generator.ts +3112 -0
  53. package/src/sany-levels.ts +1208 -0
  54. package/src/sany-lexer.ts +545 -0
  55. package/src/sany-linter.ts +122 -0
  56. package/src/sany-modules.ts +1398 -0
  57. package/src/sany-nodes.ts +463 -0
  58. package/src/sany-parse-error.ts +2161 -0
  59. package/src/sany-report.ts +354 -0
  60. package/src/sany-selectors.ts +738 -0
  61. package/src/sany-source.ts +248 -0
  62. package/src/sany-xml.ts +1017 -0
  63. package/src/spec-description.ts +25 -5
  64. package/src/standard-modules.ts +172 -129
  65. package/src/standard-ranges.ts +116 -0
  66. package/src/state-graph.ts +29 -6
  67. package/src/subexpressions.ts +221 -161
  68. package/src/substitution-levels.ts +5 -8
  69. package/src/successors.ts +141 -173
  70. package/src/symmetry.ts +168 -50
  71. package/src/tableau.ts +3 -1
  72. package/src/tlc-ext.ts +13 -17
  73. package/src/tlc-messages.ts +329 -0
  74. package/src/tlc-module.ts +49 -21
  75. package/src/tlc-modules.ts +0 -4
  76. package/src/tlc-order.ts +55 -9
  77. package/src/tlc-random.ts +28 -3
  78. package/src/tlc-replay.ts +655 -0
  79. package/src/tlc-version.ts +18 -0
  80. package/src/transfer.ts +15 -3
  81. package/src/values.ts +664 -317
package/src/successors.ts CHANGED
@@ -1,51 +1,47 @@
1
1
  import {
2
2
  getChildren,
3
3
  isLocallyResolved,
4
- type Bound,
5
4
  type Definition,
6
5
  type Expression,
7
6
  type QuantifierExpression,
8
7
  } from "./ast";
8
+ import { getKindString } from "./builtins";
9
9
  import { getReadVariables } from "./dependencies";
10
- import {
11
- TlaEvaluationError,
12
- TlaIllegalStateError,
13
- TlaIncompleteStateError,
14
- TlaUnassignedError,
15
- } from "./errors";
10
+ import { TlaEvaluationError, TlaIllegalStateError, TlaIncompleteSuccessorError } from "./errors";
16
11
  import { EXIT_STATUS, setExitCode } from "./exit-codes";
17
12
  import {
18
13
  describeLocation,
14
+ describeNode,
19
15
  describeRange,
20
- bindArgument,
16
+ bindLazyArguments,
21
17
  bindDefinitions,
22
18
  bindOperatorArguments,
23
19
  bindValue,
24
20
  compileArgumentScope,
25
21
  evaluate,
26
- getEntryEvaluator,
27
22
  expectBoolean,
28
23
  forEachBinding,
29
- getBoundsDomain,
24
+ getLazyFunction,
25
+ toKeepLazyContext,
30
26
  getDefinition,
31
- getEnumeratedElements,
27
+ getContextElements,
32
28
  getReevaluatedDefinitions,
33
29
  lookupScope,
34
30
  resolveApplyOperator,
35
31
  resolveGlobalOperator,
32
+ toUnspecifiedSubscriptError,
36
33
  type Context,
37
34
  type Environment,
38
- type Evaluator,
39
35
  type Scope,
40
36
  type State,
41
37
  } from "./evaluate";
42
38
  import { mapChildren } from "./instances";
39
+ import { isAssignedShortcut, LITERAL_KINDS, replayEnabled } from "./tlc-replay";
43
40
  import {
44
- createTuple,
45
41
  formatValue,
46
42
  getStreamedElements,
47
- isElementOf,
48
- isEnumerable,
43
+ isFunctionValue,
44
+ isTlcEnumerable,
49
45
  valuesEqual,
50
46
  type Value,
51
47
  } from "./values";
@@ -58,6 +54,7 @@ export interface Successor {
58
54
  interface Run {
59
55
  environment: Environment;
60
56
  isInit: boolean;
57
+ // Whether the run computes ENABLED rather than the successors of the state.
61
58
  isEnabled: boolean;
62
59
  current: Value[] | undefined;
63
60
  isStopped: boolean;
@@ -95,54 +92,57 @@ const assign = (partial: State, index: number, value: Value) => {
95
92
  return copy;
96
93
  };
97
94
 
98
- // TLC's Tool.getInitStates and Tool.getNextStates report a value where they expect a formula.
99
- const expectFormula = (expression: Expression, value: Value, isInit: boolean) => {
100
- if (typeof value === "boolean") return value;
101
- throw new TlaEvaluationError(
102
- `In computing ${isInit ? "initial" : "next"} states, TLC expected a boolean expression,\nbut instead found ${formatValue(value)}.\n${describeRange(expression)}`,
103
- );
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);
104
103
  };
105
104
 
106
- const holds = (expression: Expression, context: Context, value = evaluate(expression, context)) => {
107
- // TLC rejects a number in the next-state relation when it splits the relation into actions.
108
- if (context.next !== undefined && expression.kind === "Number") {
109
- throw setExitCode(
110
- new TlaEvaluationError(
111
- `The next state relation is not a boolean expression.\n${expression.text ?? expression.value}`,
112
- ),
113
- EXIT_STATUS.error,
114
- );
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}`;
115
118
  }
116
- return expectFormula(expression, value, context.next === undefined);
119
+ return `In computing ${getComputation(run)}, TLC expected a boolean expression,\nbut instead found ${formatValue(value)}.\n${node}`;
117
120
  };
118
121
 
119
- const isAssignmentOperator = (operator: string) =>
120
- operator === "=" || operator === "\\in" || operator === "\\subseteq";
121
-
122
- const tracedRunners = new WeakMap<Environment, WeakMap<Expression, Runner>[]>();
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
+ };
123
131
 
124
- const getRunners = (environment: Environment, isInit: boolean) => {
125
- if (!environment.trace) return isInit ? initRunners : nextRunners;
126
- const runners = tracedRunners.get(environment) ?? [new WeakMap(), new WeakMap()];
127
- tracedRunners.set(environment, runners);
128
- return runners[isInit ? 0 : 1];
132
+ const holds = (run: Run, expression: Expression, context: Context, isWalked = true) => {
133
+ const value = evaluate(expression, context);
134
+ if (typeof value === "boolean") return value;
135
+ throw createRunError(run, getNonBooleanMessage(run, value, expression, isWalked));
129
136
  };
130
137
 
138
+ const isAssignmentOperator = (operator: string) =>
139
+ operator === "=" || operator === "\\in" || operator === "\\subseteq";
140
+
131
141
  const getRunner = (expression: Expression, environment: Environment, isInit: boolean): Runner => {
132
- const runners = getRunners(environment, isInit);
142
+ const runners = isInit ? initRunners : nextRunners;
133
143
  const cached = runners.get(expression);
134
144
  if (cached) return cached;
135
- const compiled = compileAction(expression, environment, isInit);
136
- const trace = environment.trace;
137
- // TLC's CallStackTool keeps an action's expression on the stack while the rest of the action runs.
138
- const runner: Runner = trace
139
- ? (run, scope, partial, action, isFixed, onComplete) => {
140
- const depth = trace.length;
141
- trace.push(expression);
142
- compiled(run, scope, partial, action, isFixed, onComplete);
143
- trace.length = depth;
144
- }
145
- : compiled;
145
+ const runner = compileAction(expression, environment, isInit);
146
146
  runners.set(expression, runner);
147
147
  return runner;
148
148
  };
@@ -226,23 +226,6 @@ const getUnchangedIndices = (
226
226
  return getUnchangedIndices(definition.body, environment, visited);
227
227
  };
228
228
 
229
- // TLC's CallStackTool keeps each expression that UNCHANGED takes apart on the stack.
230
- const getUnchangedFrames = (expression: Expression, environment: Environment): Expression[] => {
231
- if (expression.kind === "Tuple") {
232
- return [
233
- expression,
234
- ...expression.elements.flatMap((element) => getUnchangedFrames(element, environment)),
235
- ];
236
- }
237
- const definition =
238
- expression.kind === "Identifier" && !environment.variableIndex.has(expression.name)
239
- ? getZeroArityBody(expression.name, environment)
240
- : undefined;
241
- return definition
242
- ? [expression, ...getUnchangedFrames(definition.body, environment)]
243
- : [expression];
244
- };
245
-
246
229
  const ACTION_KINDS = new Set([
247
230
  "Label",
248
231
  "And",
@@ -374,8 +357,6 @@ const compileConjunction = (
374
357
  isInit: boolean,
375
358
  ): Runner => {
376
359
  const conjuncts = operands.map((operand) => toConjunct(operand, environment, isInit));
377
- const evaluators: (Evaluator | undefined)[] = conjuncts.map(() => undefined);
378
- const trace = environment.trace;
379
360
  const runFrom = (
380
361
  start: number,
381
362
  run: Run,
@@ -386,35 +367,25 @@ const compileConjunction = (
386
367
  ): void => {
387
368
  let current = partial;
388
369
  let context: Context | undefined;
370
+ const isShortcut = isAssignedShortcut(run);
389
371
  for (let index = start; index < conjuncts.length; index++) {
390
- if (index > 0 && run.isInit && isComplete(current)) {
372
+ if (index > 0 && isShortcut && isComplete(current)) {
391
373
  context ??= getContext(run, scope, current);
392
374
  for (let rest = index; rest < conjuncts.length; rest++) {
393
- if (!holdsInInit(conjuncts[rest].expression, context)) return;
375
+ if (!holds(run, conjuncts[rest].expression, context, false)) return;
394
376
  }
395
377
  onComplete(current, action);
396
378
  return;
397
379
  }
398
380
  const conjunct = conjuncts[index];
399
- if (trace) {
400
- trace.push(conjunct.expression);
401
- if (conjunct.expression.kind === "Unchanged") {
402
- trace.push(...getUnchangedFrames(conjunct.expression.expression, environment));
403
- }
404
- }
405
381
  if (conjunct.kind === "Guard") {
406
382
  context ??= getContext(run, scope, current);
407
- const evaluator = (evaluators[index] ??= getEntryEvaluator(
408
- conjunct.expression,
409
- environment,
410
- ));
411
- if (!holds(conjunct.expression, context, evaluator(context))) return;
383
+ if (!holds(run, conjunct.expression, context)) return;
412
384
  continue;
413
385
  }
414
386
  if (conjunct.kind === "Assign") {
415
387
  context ??= getContext(run, scope, current);
416
- const evaluator = (evaluators[index] ??= getEntryEvaluator(conjunct.value, environment));
417
- const value = evaluator(context);
388
+ const value = evaluate(conjunct.value, context);
418
389
  const assigned = current[conjunct.index];
419
390
  if (assigned === undefined) {
420
391
  current = assign(current, conjunct.index, value);
@@ -450,13 +421,10 @@ const compileConjunction = (
450
421
  runFrom(0, run, scope, partial, action, onComplete);
451
422
  };
452
423
 
453
- const holdsInInit = (expression: Expression, context: Context) =>
454
- expectFormula(expression, evaluate(expression, context), true);
455
-
456
424
  const compileGuard =
457
425
  (expression: Expression): Runner =>
458
426
  (run, scope, partial, action, _isFixed, onComplete) => {
459
- if (holds(expression, getContext(run, scope, partial))) onComplete(partial, action);
427
+ if (holds(run, expression, getContext(run, scope, partial))) onComplete(partial, action);
460
428
  };
461
429
 
462
430
  const compileMembership = (
@@ -468,16 +436,17 @@ const compileMembership = (
468
436
  const index = getIndex(run, scope, partial);
469
437
  const context = getContext(run, scope, partial);
470
438
  if (partial[index] !== undefined) {
471
- if (holds(expression, context)) onComplete(partial, action);
439
+ if (holds(run, expression, context)) onComplete(partial, action);
472
440
  return;
473
441
  }
474
442
  const evaluated = evaluate(set, context);
475
443
  const value: Value =
476
444
  expression.operator === "\\subseteq" ? { kind: "Subset", base: evaluated } : evaluated;
477
- if (!isEnumerable(value)) {
478
- throw new TlaEvaluationError(
479
- `In computing ${run.isInit ? "initial" : "next"} states, the right side of \\IN is not enumerable.\n${describeRange(expression)}`,
480
- );
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)}`);
481
450
  }
482
451
  const elements = getStreamedElements(value);
483
452
  for (let position = 0; position < elements.length && !run.isStopped; position++) {
@@ -527,65 +496,43 @@ const compileDynamicAssignment = (
527
496
  };
528
497
  };
529
498
 
530
- interface FunctionAction {
531
- bounds: Bound[];
532
- body: Expression;
533
- scope: Scope | undefined;
534
- }
535
-
536
- const getFunctionAction = (
537
- expression: Expression,
538
- scope: Scope | undefined,
539
- partial: State,
540
- run: Run,
541
- ): FunctionAction | undefined => {
542
- if (expression.kind === "FunctionConstruction") {
543
- return { bounds: expression.bounds, body: expression.body, scope };
544
- }
545
- if (expression.kind !== "Identifier") return undefined;
546
- const binding = lookupScope(scope, expression.name);
547
- if (binding?.kind === "Thunk") {
548
- return getFunctionAction(binding.expression, binding.scope, partial, run);
549
- }
550
- const definition = binding ? undefined : getDefinition(expression.name, run.environment);
551
- if (definition?.kind !== "Function") return undefined;
552
- const self = evaluate(expression, getContext(run, undefined, partial));
553
- return {
554
- bounds: definition.bounds,
555
- body: definition.body,
556
- scope: bindValue(undefined, definition.name, self),
557
- };
558
- };
559
-
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.
560
501
  const compileFunctionAction = (
561
502
  expression: Expression & { kind: "FunctionApplication" },
562
503
  environment: Environment,
563
504
  isInit: boolean,
564
505
  ): Runner => {
565
- const guard = compileGuard(expression);
506
+ const node = describeRange(expression);
566
507
  return (run, scope, partial, action, isFixed, onComplete) => {
567
- const lambda = getFunctionAction(expression.function, scope, partial, run);
568
- if (!lambda) {
569
- 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
+ );
570
520
  return;
571
521
  }
572
- const context = getContext(run, scope, partial);
573
- const args = expression.args.map((arg) => evaluate(arg, context));
574
- const argument = args.length === 1 ? args[0] : createTuple(args);
575
- const domain = getBoundsDomain(lambda.bounds, getContext(run, lambda.scope, partial));
576
- if (!isElementOf(argument, domain)) {
577
- throw new TlaEvaluationError(
578
- `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}`,
579
526
  );
580
527
  }
581
- getRunner(lambda.body, environment, isInit)(
582
- run,
583
- bindArgument(lambda.bounds, lambda.scope, argument),
584
- partial,
585
- action,
586
- isFixed,
587
- onComplete,
588
- );
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);
589
536
  };
590
537
  };
591
538
 
@@ -609,7 +556,7 @@ const compileUnchanged = (expression: Expression, environment: Environment): Run
609
556
  );
610
557
  return;
611
558
  }
612
- if (holds(unchanged, getContext(run, scope, partial))) onComplete(partial, action);
559
+ if (holds(run, unchanged, getContext(run, scope, partial))) onComplete(partial, action);
613
560
  };
614
561
  };
615
562
 
@@ -804,9 +751,10 @@ const compileAction = (
804
751
  if (expression.bounds.length === 1 && bound.pattern.kind === "Name") {
805
752
  const name = bound.pattern.name;
806
753
  return (run, scope, partial, action, isFixed, onComplete) => {
807
- const elements = getEnumeratedElements(
754
+ const elements = getContextElements(
808
755
  evaluate(bound.set, getContext(run, scope, partial)),
809
756
  bound.set,
757
+ environment.runtime.version,
810
758
  );
811
759
  for (let index = 0; index < elements.length && !run.isStopped; index++) {
812
760
  body(
@@ -820,14 +768,17 @@ const compileAction = (
820
768
  }
821
769
  };
822
770
  }
823
- const hoisted =
824
- isInit || environment.trace ? undefined : getHoistedGuards(expression, environment);
771
+ const hoisted = isInit ? undefined : getHoistedGuards(expression, environment);
825
772
  if (hoisted) {
826
773
  const rest = compileConjunction(hoisted.rest, environment, isInit);
827
774
  return (run, scope, partial, action, isFixed, onComplete) => {
828
775
  const context = getContext(run, scope, partial);
829
776
  const sets = expression.bounds.map((innerBound) =>
830
- getEnumeratedElements(evaluate(innerBound.set, context), innerBound.set),
777
+ getContextElements(
778
+ evaluate(innerBound.set, context),
779
+ innerBound.set,
780
+ environment.runtime.version,
781
+ ),
831
782
  );
832
783
  if (sets.some((elements) => elements.length === 0)) return;
833
784
  const visit = (index: number, boundScope: Scope | undefined): void => {
@@ -840,7 +791,7 @@ const compileAction = (
840
791
  const elementScope = bindValue(boundScope, hoisted.names[index], element);
841
792
  if (index === sets.length - 1) {
842
793
  const guardContext = getContext(run, elementScope, partial);
843
- if (!hoisted.guards.every((guard) => holds(guard, guardContext))) continue;
794
+ if (!hoisted.guards.every((guard) => holds(run, guard, guardContext))) continue;
844
795
  }
845
796
  visit(index - 1, elementScope);
846
797
  }
@@ -875,7 +826,7 @@ const compileAction = (
875
826
  );
876
827
  if (isBoundToValues) {
877
828
  const isSatisfied = scopes.every((boundScope) =>
878
- holds(expression.body, getContext(run, boundScope, partial)),
829
+ holds(run, expression.body, getContext(run, boundScope, partial)),
879
830
  );
880
831
  if (isSatisfied) onComplete(partial, action);
881
832
  return;
@@ -889,7 +840,7 @@ const compileAction = (
889
840
  const isSatisfied = scopes
890
841
  .slice(index)
891
842
  .every((boundScope) =>
892
- holdsInInit(expression.body, getContext(run, boundScope, nextPartial)),
843
+ holds(run, expression.body, getContext(run, boundScope, nextPartial)),
893
844
  );
894
845
  if (isSatisfied) onComplete(nextPartial, nextAction);
895
846
  return;
@@ -906,26 +857,38 @@ const compileAction = (
906
857
  const alternative = child(expression.alternative);
907
858
  return (run, scope, partial, action, isFixed, onComplete) => {
908
859
  const condition = evaluate(expression.condition, getContext(run, scope, partial));
909
- 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;
910
864
  branch(run, scope, partial, action, isFixed, onComplete);
911
865
  };
912
866
  }
913
867
  case "Case": {
914
868
  const arms = expression.arms.map((arm) => ({
915
869
  condition: arm.condition,
870
+ source: arm.body,
916
871
  body: child(arm.body),
917
872
  }));
918
873
  const other = expression.other ? child(expression.other) : undefined;
919
874
  return (run, scope, partial, action, isFixed, onComplete) => {
920
875
  const context = getContext(run, scope, partial);
921
- const arm = arms.find((candidate) =>
922
- expectBoolean(evaluate(candidate.condition, context), "a CASE guard"),
923
- );
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
+ });
924
885
  const body = arm?.body ?? other;
925
- if (!body)
926
- throw new TlaEvaluationError(
927
- `In computing ${run.isInit ? "initial" : "next"} states, TLC encountered a CASE with no conditions true.\n${describeRange(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)}`,
928
890
  );
891
+ }
929
892
  body(run, scope, partial, action, isFixed, onComplete);
930
893
  };
931
894
  }
@@ -954,19 +917,18 @@ const compileAction = (
954
917
  case "AngleAction": {
955
918
  const body = child(expression.action);
956
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
+ };
957
929
  return (run, scope, partial, action, isFixed, onComplete) =>
958
930
  body(run, scope, partial, action, isFixed, (nextPartial, nextAction) => {
959
- // ENABLED evaluates the subscript on the successor that the action specifies. TLC names
960
- // a variable that the action leaves without a value.
961
- try {
962
- if (!holds(unchanged, getContext(run, scope, nextPartial)))
963
- onComplete(nextPartial, nextAction);
964
- } catch (error) {
965
- if (!run.isEnabled || !(error instanceof TlaUnassignedError)) throw error;
966
- throw new TlaEvaluationError(
967
- `The action formula A appearing in a WF_v(A) or SF_v(A) operator does not specify the primed value of the variable ${error.variable} occurring in the state formula v.\n`,
968
- );
969
- }
931
+ if (!isUnchanged(run, scope, nextPartial)) onComplete(nextPartial, nextAction);
970
932
  });
971
933
  }
972
934
  case "BoxAction":
@@ -1043,6 +1005,8 @@ export const isEnabled = (expression: Expression, context: Context): boolean =>
1043
1005
  );
1044
1006
  }
1045
1007
  const environment = context.environment;
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);
1046
1010
  const run: Run = {
1047
1011
  environment,
1048
1012
  isInit: false,
@@ -1086,7 +1050,9 @@ export const forEachInitialState = (
1086
1050
  (partial) => {
1087
1051
  if (run.isStopped) return;
1088
1052
  if (!isComplete(partial)) {
1089
- throw setExitCode(new TlaIllegalStateError(partial), EXIT_STATUS.error);
1053
+ // TLC reports an initial state that isn't completely specified as an illegal state, not as
1054
+ // an evaluation failure.
1055
+ throw setExitCode(new TlaIllegalStateError([...partial]), EXIT_STATUS.error);
1090
1056
  }
1091
1057
  run.isStopped = onState(partial);
1092
1058
  },
@@ -1135,10 +1101,12 @@ export const forEachSuccessor = (
1135
1101
  (partial, action) => {
1136
1102
  if (run.isStopped) return;
1137
1103
  if (!isComplete(partial)) {
1138
- throw new TlaIncompleteStateError(
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(", ")}`,
1139
1107
  partial,
1140
1108
  action,
1141
- getMissingVariables(partial, environment).sort(),
1109
+ missing,
1142
1110
  );
1143
1111
  }
1144
1112
  counter.successors++;