tla-ts 0.0.1 → 0.0.3-dev.037ac3b

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 (74) hide show
  1. package/README.md +98 -39
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +225 -30
  4. package/dist/index.d.ts +225 -30
  5. package/dist/index.js +1 -1
  6. package/dist/parallel-worker.cjs +2 -2
  7. package/dist/parallel-worker.js +2 -2
  8. package/dist/parallel.cjs +1 -1
  9. package/dist/parallel.js +2 -2
  10. package/dist/run-cli.cjs +21 -1
  11. package/dist/run-cli.js +21 -1
  12. package/dist/transfer.cjs +400 -39
  13. package/dist/transfer.js +400 -39
  14. package/package.json +6 -2
  15. package/src/ast.ts +213 -6
  16. package/src/bags.ts +161 -71
  17. package/src/builtins.ts +203 -23
  18. package/src/bundled-modules.ts +63 -32
  19. package/src/check.ts +1132 -258
  20. package/src/cli-options.ts +64 -0
  21. package/src/codegen.ts +58 -34
  22. package/src/config.ts +39 -11
  23. package/src/dependencies.ts +2 -2
  24. package/src/environment.ts +557 -153
  25. package/src/errors.ts +66 -1
  26. package/src/evaluate.ts +553 -258
  27. package/src/exit-codes.ts +53 -0
  28. package/src/fingerprints.ts +48 -13
  29. package/src/index.ts +9 -4
  30. package/src/instances.ts +235 -79
  31. package/src/java-overrides.ts +241 -0
  32. package/src/json.ts +153 -72
  33. package/src/levels.ts +850 -0
  34. package/src/lexer.ts +108 -29
  35. package/src/live-check.ts +692 -0
  36. package/src/liveness.ts +71 -596
  37. package/src/obligation-action.ts +384 -0
  38. package/src/obligation-anon.ts +575 -0
  39. package/src/obligation-deref.ts +637 -0
  40. package/src/obligation-expression.ts +1060 -0
  41. package/src/obligation-format.ts +406 -0
  42. package/src/obligation-generate.ts +1074 -0
  43. package/src/obligation-levels.ts +329 -0
  44. package/src/obligation-module.ts +1042 -0
  45. package/src/obligation-printer.ts +889 -0
  46. package/src/obligation-proof.ts +76 -0
  47. package/src/obligation-source.ts +1084 -0
  48. package/src/obligation-tuples.ts +184 -0
  49. package/src/obligations.ts +316 -0
  50. package/src/parallel-worker.ts +186 -201
  51. package/src/parallel.ts +179 -84
  52. package/src/parser.ts +970 -206
  53. package/src/pretty-print.ts +259 -0
  54. package/src/randomization.ts +24 -113
  55. package/src/resolve.ts +483 -51
  56. package/src/run-cli.ts +473 -62
  57. package/src/runtime.ts +65 -28
  58. package/src/spec-description.ts +521 -0
  59. package/src/standard-levels.ts +105 -0
  60. package/src/standard-modules.ts +218 -92
  61. package/src/state-graph.ts +157 -0
  62. package/src/subexpressions.ts +31 -45
  63. package/src/substitution-levels.ts +184 -0
  64. package/src/successors.ts +336 -57
  65. package/src/symmetry.ts +79 -23
  66. package/src/tableau.ts +93 -60
  67. package/src/tlc-ext.ts +148 -44
  68. package/src/tlc-module.ts +71 -66
  69. package/src/tlc-modules.ts +1022 -0
  70. package/src/tlc-order.ts +511 -0
  71. package/src/tlc-random.ts +414 -0
  72. package/src/tlc-summary.ts +124 -0
  73. package/src/transfer.ts +26 -5
  74. package/src/values.ts +1285 -179
package/src/successors.ts CHANGED
@@ -1,31 +1,46 @@
1
- import type { Bound, Definition, Expression } from "./ast";
2
- import { TlaEvaluationError } from "./errors";
3
1
  import {
2
+ getChildren,
3
+ isLocallyResolved,
4
+ type Bound,
5
+ type Definition,
6
+ type Expression,
7
+ type QuantifierExpression,
8
+ } from "./ast";
9
+ import { getReadVariables } from "./dependencies";
10
+ import { TlaEvaluationError, UNEXPECTED_EXCEPTION } from "./errors";
11
+ import { EXIT_STATUS, setExitCode } from "./exit-codes";
12
+ import {
13
+ describeLocation,
14
+ describeRange,
4
15
  bindArgument,
5
16
  bindDefinitions,
6
17
  bindOperatorArguments,
7
18
  bindValue,
8
19
  compileArgumentScope,
9
20
  evaluate,
21
+ getEntryEvaluator,
10
22
  expectBoolean,
11
23
  forEachBinding,
12
24
  getBoundsDomain,
13
25
  getDefinition,
26
+ getEnumeratedElements,
27
+ getReevaluatedDefinitions,
14
28
  lookupScope,
15
29
  resolveApplyOperator,
16
30
  resolveGlobalOperator,
17
- isLocallyResolved,
18
31
  type Context,
19
32
  type Environment,
33
+ type Evaluator,
20
34
  type Scope,
21
35
  type State,
22
36
  } from "./evaluate";
37
+ import { mapChildren } from "./instances";
23
38
  import {
24
39
  createTuple,
25
40
  formatValue,
41
+ getStreamedElements,
26
42
  isElementOf,
27
43
  isEnumerable,
28
- toFiniteSet,
29
44
  valuesEqual,
30
45
  type Value,
31
46
  } from "./values";
@@ -39,6 +54,7 @@ interface Run {
39
54
  environment: Environment;
40
55
  isInit: boolean;
41
56
  current: Value[] | undefined;
57
+ isStopped: boolean;
42
58
  }
43
59
 
44
60
  type Continuation = (partial: State, action: string | undefined) => void;
@@ -62,10 +78,7 @@ type Conjunct = { expression: Expression } & (
62
78
  const initRunners = new WeakMap<Expression, Runner>();
63
79
  const nextRunners = new WeakMap<Expression, Runner>();
64
80
 
65
- const describeLocation = (expression: Expression) =>
66
- `line ${expression.location.line}, column ${expression.location.column}`;
67
-
68
- const getContext = (run: Run, scope: Scope | undefined, partial: State): Context =>
81
+ const getContext = (run: Run, scope: Scope | undefined, partial: State) =>
69
82
  run.isInit
70
83
  ? { environment: run.environment, scope, state: partial, next: undefined }
71
84
  : { environment: run.environment, scope, state: run.current, next: partial };
@@ -76,10 +89,14 @@ const assign = (partial: State, index: number, value: Value) => {
76
89
  return copy;
77
90
  };
78
91
 
79
- const holds = (expression: Expression, context: Context) => {
80
- const value = evaluate(expression, context);
92
+ const holds = (expression: Expression, context: Context, value = evaluate(expression, context)) => {
81
93
  if (typeof value === "boolean") return value;
82
- return expectBoolean(value, `the action at ${describeLocation(expression)}`);
94
+ try {
95
+ return expectBoolean(value, `the action at ${describeLocation(expression)}`);
96
+ } catch (error) {
97
+ // TLC fails with an unexpected exception when the next-state relation isn't a boolean.
98
+ throw context.next === undefined ? error : setExitCode(error, EXIT_STATUS.error);
99
+ }
83
100
  };
84
101
 
85
102
  const isAssignmentOperator = (operator: string) =>
@@ -188,16 +205,12 @@ const ACTION_KINDS = new Set([
188
205
  "FunctionApplication",
189
206
  ]);
190
207
 
191
- const isGuard = (expression: Expression, environment: Environment, isInit: boolean): boolean => {
208
+ const isGuard = (expression: Expression, environment: Environment, isInit: boolean) => {
192
209
  if (ACTION_KINDS.has(expression.kind)) return false;
193
210
  if (expression.kind === "Identifier") {
194
- if (
195
- environment.variableIndex.has(expression.name) ||
196
- environment.constants.has(expression.name)
197
- ) {
198
- return true;
199
- }
200
- return false;
211
+ return (
212
+ environment.variableIndex.has(expression.name) || environment.constants.has(expression.name)
213
+ );
201
214
  }
202
215
  if (expression.kind !== "Apply") return true;
203
216
  if (expression.operator === "=>") return false;
@@ -212,6 +225,56 @@ const isGuard = (expression: Expression, environment: Environment, isInit: boole
212
225
  return definition === undefined && environment.builtins.has(expression.operator);
213
226
  };
214
227
 
228
+ const NO_NAMES: ReadonlySet<string> = new Set();
229
+
230
+ // The local names an expression reads from enclosing scopes, or undefined when it applies a local
231
+ // operator, whose body may assign variables.
232
+ const getReadLocals = (
233
+ expression: Expression,
234
+ bound: ReadonlySet<string>,
235
+ ): string[] | undefined => {
236
+ if (isLocallyResolved(expression)) {
237
+ if (expression.kind !== "Identifier") return undefined;
238
+ return bound.has(expression.name) ? [] : [expression.name];
239
+ }
240
+ const names: string[] = [];
241
+ const result = { isReadOnly: true };
242
+ mapChildren(expression, (child, childNames) => {
243
+ const childLocals = result.isReadOnly
244
+ ? getReadLocals(child, childNames.length === 0 ? bound : new Set([...bound, ...childNames]))
245
+ : [];
246
+ if (childLocals) names.push(...childLocals);
247
+ else result.isReadOnly = false;
248
+ return child;
249
+ });
250
+ return result.isReadOnly ? names : undefined;
251
+ };
252
+
253
+ // TLC generates a successor for every disjunct and witness that holds, so an expression only
254
+ // checks like a guard when it has neither.
255
+ const hasBranches = (
256
+ expression: Expression,
257
+ environment: Environment,
258
+ visited: Set<Definition>,
259
+ ): boolean => {
260
+ if (expression.kind === "Or" || expression.kind === "Exists") return true;
261
+ const name =
262
+ expression.kind === "Identifier"
263
+ ? expression.name
264
+ : expression.kind === "Apply"
265
+ ? expression.operator
266
+ : undefined;
267
+ const definition =
268
+ name === undefined || isLocallyResolved(expression)
269
+ ? undefined
270
+ : getDefinition(name, environment);
271
+ if (definition && !visited.has(definition)) {
272
+ visited.add(definition);
273
+ if (hasBranches(definition.body, environment, visited)) return true;
274
+ }
275
+ return getChildren(expression).some((child) => hasBranches(child, environment, visited));
276
+ };
277
+
215
278
  const toConjunct = (
216
279
  expression: Expression,
217
280
  environment: Environment,
@@ -231,12 +294,34 @@ const toConjunct = (
231
294
  return { kind: "Branch", expression, runner: getRunner(expression, environment, isInit) };
232
295
  };
233
296
 
297
+ // The leading guards of `\E x \in S, y \in T : P /\ Q` that read no inner bound hold for every inner
298
+ // binding alike, so they can run once for each element of the outermost bound.
299
+ const getHoistedGuards = (expression: QuantifierExpression, environment: Environment) => {
300
+ if (expression.bounds.length < 2 || expression.body.kind !== "And") return undefined;
301
+ const names = expression.bounds.flatMap((bound) =>
302
+ bound.pattern.kind === "Name" ? [bound.pattern.name] : [],
303
+ );
304
+ if (names.length !== expression.bounds.length) return undefined;
305
+ const innerNames = new Set(names.slice(0, -1));
306
+ const operands = expression.body.operands;
307
+ const index = operands.findIndex((operand) => {
308
+ if (getReadVariables(operand, environment) === undefined) return true;
309
+ if (hasBranches(operand, environment, new Set())) return true;
310
+ const readLocals = getReadLocals(operand, NO_NAMES);
311
+ return !readLocals || readLocals.some((name) => innerNames.has(name));
312
+ });
313
+ const count = index === -1 ? operands.length : index;
314
+ if (count === 0) return undefined;
315
+ return { names, guards: operands.slice(0, count), rest: operands.slice(count) };
316
+ };
317
+
234
318
  const compileConjunction = (
235
319
  operands: Expression[],
236
320
  environment: Environment,
237
321
  isInit: boolean,
238
322
  ): Runner => {
239
323
  const conjuncts = operands.map((operand) => toConjunct(operand, environment, isInit));
324
+ const evaluators: (Evaluator | undefined)[] = conjuncts.map(() => undefined);
240
325
  const runFrom = (
241
326
  start: number,
242
327
  run: Run,
@@ -259,12 +344,17 @@ const compileConjunction = (
259
344
  const conjunct = conjuncts[index];
260
345
  if (conjunct.kind === "Guard") {
261
346
  context ??= getContext(run, scope, current);
262
- if (!holds(conjunct.expression, context)) return;
347
+ const evaluator = (evaluators[index] ??= getEntryEvaluator(
348
+ conjunct.expression,
349
+ environment,
350
+ ));
351
+ if (!holds(conjunct.expression, context, evaluator(context))) return;
263
352
  continue;
264
353
  }
265
354
  if (conjunct.kind === "Assign") {
266
355
  context ??= getContext(run, scope, current);
267
- const value = evaluate(conjunct.value, context);
356
+ const evaluator = (evaluators[index] ??= getEntryEvaluator(conjunct.value, environment));
357
+ const value = evaluator(context);
268
358
  const assigned = current[conjunct.index];
269
359
  if (assigned === undefined) {
270
360
  current = assign(current, conjunct.index, value);
@@ -332,8 +422,8 @@ const compileMembership = (
332
422
  `In computing next states, the right side of \\in is not enumerable: ${formatValue(value)} at ${describeLocation(expression)}`,
333
423
  );
334
424
  }
335
- const elements = toFiniteSet(value).elements;
336
- for (let position = 0; position < elements.length; position++) {
425
+ const elements = getStreamedElements(value);
426
+ for (let position = 0; position < elements.length && !run.isStopped; position++) {
337
427
  onComplete(assign(partial, index, elements[position]), action);
338
428
  }
339
429
  };
@@ -542,6 +632,104 @@ const compileIdentifier = (
542
632
  };
543
633
  };
544
634
 
635
+ // A PlusCal action starts with a guard such as pc[self] = "L1", and a process is a disjunction of
636
+ // its actions. The labels an expression requires of one variable at one bound name let a
637
+ // disjunction skip the operands whose guard is false without evaluating them.
638
+ interface LabelGuard {
639
+ variable: number;
640
+ argument: string;
641
+ labels: Set<string>;
642
+ }
643
+
644
+ const getLabelGuard = (
645
+ expression: Expression,
646
+ environment: Environment,
647
+ visited: Set<Expression>,
648
+ ): LabelGuard | undefined => {
649
+ if (visited.has(expression)) return undefined;
650
+ visited.add(expression);
651
+ if (expression.kind === "And") {
652
+ const first = expression.operands[0];
653
+ if (first.kind !== "Apply" || first.operator !== "=" || isLocallyResolved(first)) {
654
+ return undefined;
655
+ }
656
+ const target = first.args[0];
657
+ const label = first.args[1];
658
+ if (target.kind !== "FunctionApplication" || label.kind !== "String") return undefined;
659
+ const argument = target.args.length === 1 ? target.args[0] : undefined;
660
+ if (argument?.kind !== "Identifier" || !isLocallyResolved(argument)) return undefined;
661
+ const variable = getStaticVariableIndex(target.function, environment, false);
662
+ if (variable === undefined) return undefined;
663
+ return { variable, argument: argument.name, labels: new Set([label.value]) };
664
+ }
665
+ if (expression.kind === "Or") {
666
+ const guards = expression.operands.map((operand) =>
667
+ getLabelGuard(operand, environment, visited),
668
+ );
669
+ const first = guards[0];
670
+ const isShared = guards.every(
671
+ (guard) =>
672
+ guard !== undefined &&
673
+ guard.variable === first?.variable &&
674
+ guard.argument === first.argument,
675
+ );
676
+ if (!first || !isShared) return undefined;
677
+ const labels = new Set(guards.flatMap((guard) => [...(guard?.labels ?? [])]));
678
+ return { variable: first.variable, argument: first.argument, labels };
679
+ }
680
+ if (expression.kind !== "Apply" || isLocallyResolved(expression)) return undefined;
681
+ const closure = resolveGlobalOperator(expression.operator, environment);
682
+ if (closure?.kind !== "Closure" || closure.scope !== undefined) return undefined;
683
+ const guard = getLabelGuard(closure.body, environment, visited);
684
+ const position = closure.params.findIndex(
685
+ (param) => param.arity === 0 && param.name === guard?.argument,
686
+ );
687
+ const argument = expression.args[position];
688
+ if (!guard || argument?.kind !== "Identifier" || !isLocallyResolved(argument)) {
689
+ return undefined;
690
+ }
691
+ return { variable: guard.variable, argument: argument.name, labels: guard.labels };
692
+ };
693
+
694
+ const getLabel = (run: Run, scope: Scope | undefined, guard: LabelGuard) => {
695
+ const binding = lookupScope(scope, guard.argument);
696
+ const functionValue = run.current?.[guard.variable];
697
+ if (
698
+ binding?.kind !== "Value" ||
699
+ typeof functionValue !== "object" ||
700
+ functionValue.kind !== "Function"
701
+ ) {
702
+ return undefined;
703
+ }
704
+ const position = functionValue.domain.indexOf(binding.value);
705
+ const label = position === -1 ? undefined : functionValue.range[position];
706
+ return typeof label === "string" ? label : undefined;
707
+ };
708
+
709
+ const compileDisjunction = (
710
+ operands: Expression[],
711
+ environment: Environment,
712
+ isInit: boolean,
713
+ ): Runner => {
714
+ const runners = operands.map((operand) => getRunner(operand, environment, isInit));
715
+ const guards = isInit
716
+ ? []
717
+ : operands.map((operand) => getLabelGuard(operand, environment, new Set()));
718
+ const dispatch = guards.find((guard) => guard !== undefined);
719
+ const labels = guards.map((guard) =>
720
+ guard?.variable === dispatch?.variable && guard?.argument === dispatch?.argument
721
+ ? guard?.labels
722
+ : undefined,
723
+ );
724
+ return (run, scope, partial, action, isFixed, onComplete) => {
725
+ const label = dispatch && getLabel(run, scope, dispatch);
726
+ for (let index = 0; index < runners.length && !run.isStopped; index++) {
727
+ if (label !== undefined && labels[index]?.has(label) === false) continue;
728
+ runners[index](run, scope, partial, action, isFixed, onComplete);
729
+ }
730
+ };
731
+ };
732
+
545
733
  const compileAction = (
546
734
  expression: Expression,
547
735
  environment: Environment,
@@ -551,26 +739,19 @@ const compileAction = (
551
739
  switch (expression.kind) {
552
740
  case "And":
553
741
  return compileConjunction(expression.operands, environment, isInit);
554
- case "Or": {
555
- const operands = expression.operands.map(child);
556
- return (run, scope, partial, action, isFixed, onComplete) => {
557
- for (const operand of operands) operand(run, scope, partial, action, isFixed, onComplete);
558
- };
559
- }
742
+ case "Or":
743
+ return compileDisjunction(expression.operands, environment, isInit);
560
744
  case "Exists": {
561
745
  const body = child(expression.body);
562
746
  const bound = expression.bounds[0];
563
747
  if (expression.bounds.length === 1 && bound.pattern.kind === "Name") {
564
748
  const name = bound.pattern.name;
565
749
  return (run, scope, partial, action, isFixed, onComplete) => {
566
- const set = evaluate(bound.set, getContext(run, scope, partial));
567
- if (!isEnumerable(set)) {
568
- throw new TlaEvaluationError(
569
- `TLC encountered a non-enumerable quantifier bound ${formatValue(set)} at ${describeLocation(bound.set)}`,
570
- );
571
- }
572
- const elements = toFiniteSet(set).elements;
573
- for (let index = 0; index < elements.length; index++) {
750
+ const elements = getEnumeratedElements(
751
+ evaluate(bound.set, getContext(run, scope, partial)),
752
+ bound.set,
753
+ );
754
+ for (let index = 0; index < elements.length && !run.isStopped; index++) {
574
755
  body(
575
756
  run,
576
757
  bindValue(scope, name, elements[index]),
@@ -582,15 +763,48 @@ const compileAction = (
582
763
  }
583
764
  };
584
765
  }
766
+ const hoisted = isInit ? undefined : getHoistedGuards(expression, environment);
767
+ if (hoisted) {
768
+ const rest = compileConjunction(hoisted.rest, environment, isInit);
769
+ return (run, scope, partial, action, isFixed, onComplete) => {
770
+ const context = getContext(run, scope, partial);
771
+ const sets = expression.bounds.map((innerBound) =>
772
+ getEnumeratedElements(evaluate(innerBound.set, context), innerBound.set),
773
+ );
774
+ if (sets.some((elements) => elements.length === 0)) return;
775
+ const visit = (index: number, boundScope: Scope | undefined): void => {
776
+ if (index < 0) {
777
+ rest(run, boundScope, partial, action, isFixed, onComplete);
778
+ return;
779
+ }
780
+ for (const element of sets[index]) {
781
+ if (run.isStopped) return;
782
+ const elementScope = bindValue(boundScope, hoisted.names[index], element);
783
+ if (index === sets.length - 1) {
784
+ const guardContext = getContext(run, elementScope, partial);
785
+ if (!hoisted.guards.every((guard) => holds(guard, guardContext))) continue;
786
+ }
787
+ visit(index - 1, elementScope);
788
+ }
789
+ };
790
+ visit(sets.length - 1, scope);
791
+ };
792
+ }
585
793
  return (run, scope, partial, action, isFixed, onComplete) => {
586
794
  forEachBinding(expression.bounds, getContext(run, scope, partial), (boundScope) => {
587
795
  body(run, boundScope, partial, action, isFixed, onComplete);
588
- return false;
796
+ return run.isStopped;
589
797
  });
590
798
  };
591
799
  }
592
800
  case "Forall": {
593
801
  const body = child(expression.body);
802
+ const readLocals =
803
+ !isInit &&
804
+ getReadVariables(expression, environment) !== undefined &&
805
+ !hasBranches(expression.body, environment, new Set())
806
+ ? getReadLocals(expression, NO_NAMES)
807
+ : undefined;
594
808
  return (run, scope, partial, action, _isFixed, onComplete) => {
595
809
  const boundScopes: (Scope | undefined)[] = [];
596
810
  forEachBinding(expression.bounds, getContext(run, scope, partial), (boundScope) => {
@@ -598,6 +812,16 @@ const compileAction = (
598
812
  return false;
599
813
  });
600
814
  const scopes = [...boundScopes.slice(0, 1), ...boundScopes.slice(1).reverse()];
815
+ const isBoundToValues = readLocals?.every(
816
+ (name) => lookupScope(scope, name)?.kind === "Value",
817
+ );
818
+ if (isBoundToValues) {
819
+ const isSatisfied = scopes.every((boundScope) =>
820
+ holds(expression.body, getContext(run, boundScope, partial)),
821
+ );
822
+ if (isSatisfied) onComplete(partial, action);
823
+ return;
824
+ }
601
825
  const visit = (index: number, nextPartial: State, nextAction: string | undefined): void => {
602
826
  if (index === scopes.length) {
603
827
  onComplete(nextPartial, nextAction);
@@ -641,16 +865,19 @@ const compileAction = (
641
865
  );
642
866
  const body = arm?.body ?? other;
643
867
  if (!body)
644
- throw new TlaEvaluationError(`No CASE guard is true at ${describeLocation(expression)}`);
868
+ throw new TlaEvaluationError(
869
+ `${UNEXPECTED_EXCEPTION}In computing next states, TLC encountered a CASE with no conditions true.\n${describeRange(expression)}`,
870
+ );
645
871
  body(run, scope, partial, action, isFixed, onComplete);
646
872
  };
647
873
  }
648
874
  case "Let": {
649
875
  const body = child(expression.body);
876
+ const reevaluated = getReevaluatedDefinitions(expression.definitions, environment);
650
877
  return (run, scope, partial, action, isFixed, onComplete) =>
651
878
  body(
652
879
  run,
653
- bindDefinitions(expression.definitions, getContext(run, scope, partial)),
880
+ bindDefinitions(expression.definitions, getContext(run, scope, partial), reevaluated),
654
881
  partial,
655
882
  action,
656
883
  isFixed,
@@ -749,62 +976,114 @@ export const isEnabled = (expression: Expression, context: Context): boolean =>
749
976
  );
750
977
  }
751
978
  const environment = context.environment;
752
- const found = { value: false };
979
+ const run: Run = { environment, isInit: false, current: state, isStopped: false };
980
+ // TLC stops computing ENABLED at the first successor, so it skips the rest of the action.
753
981
  getRunner(expression, environment, false)(
754
- { environment, isInit: false, current: state },
982
+ run,
755
983
  context.scope,
756
984
  getEmptyState(environment),
757
985
  undefined,
758
986
  false,
759
987
  () => {
760
- found.value = true;
988
+ run.isStopped = true;
761
989
  },
762
990
  );
763
- return found.value;
991
+ return run.isStopped;
764
992
  };
765
993
 
766
- export const getInitialStates = (expression: Expression, environment: Environment): Value[][] => {
767
- const states: Value[][] = [];
768
- const run: Run = { environment, isInit: true, current: undefined };
994
+ export const forEachInitialState = (
995
+ expression: Expression,
996
+ environment: Environment,
997
+ onState: (state: Value[]) => boolean,
998
+ scope?: Scope,
999
+ ) => {
1000
+ const run: Run = { environment, isInit: true, current: undefined, isStopped: false };
769
1001
  getRunner(expression, environment, true)(
770
1002
  run,
771
- undefined,
1003
+ scope,
772
1004
  getEmptyState(environment),
773
1005
  undefined,
774
1006
  false,
775
1007
  (partial) => {
1008
+ if (run.isStopped) return;
776
1009
  if (!isComplete(partial)) {
777
- throw new TlaEvaluationError(
778
- `The initial predicate does not assign ${getMissingVariables(partial, environment).join(", ")}`,
1010
+ // TLC reports an initial state that isn't completely specified as an illegal state, not as
1011
+ // an evaluation failure.
1012
+ throw setExitCode(
1013
+ new TlaEvaluationError(
1014
+ `The initial predicate does not assign ${getMissingVariables(partial, environment).join(", ")}`,
1015
+ ),
1016
+ EXIT_STATUS.error,
779
1017
  );
780
1018
  }
781
- states.push(partial);
1019
+ run.isStopped = onState(partial);
782
1020
  },
783
1021
  );
1022
+ };
1023
+
1024
+ export const getInitialStates = (
1025
+ expression: Expression,
1026
+ environment: Environment,
1027
+ scope?: Scope,
1028
+ ): Value[][] => {
1029
+ const states: Value[][] = [];
1030
+ forEachInitialState(
1031
+ expression,
1032
+ environment,
1033
+ (state) => {
1034
+ states.push(state);
1035
+ return false;
1036
+ },
1037
+ scope,
1038
+ );
784
1039
  return states;
785
1040
  };
786
1041
 
787
- export const getSuccessors = (
1042
+ export const forEachSuccessor = (
788
1043
  expression: Expression,
789
1044
  state: Value[],
790
1045
  environment: Environment,
791
- ): Successor[] => {
792
- const successors: Successor[] = [];
793
- const run: Run = { environment, isInit: false, current: state };
1046
+ onSuccessor: (successor: Successor) => boolean,
1047
+ scope?: Scope,
1048
+ ) => {
1049
+ const run: Run = { environment, isInit: false, current: state, isStopped: false };
1050
+ const counter = { successors: 0 };
794
1051
  getRunner(expression, environment, false)(
795
1052
  run,
796
- undefined,
1053
+ scope,
797
1054
  getEmptyState(environment),
798
1055
  undefined,
799
1056
  false,
800
1057
  (partial, action) => {
1058
+ if (run.isStopped) return;
801
1059
  if (!isComplete(partial)) {
802
1060
  throw new TlaEvaluationError(
803
1061
  `Successor state is not completely specified by action ${action ?? "Next"}. The following variables are not assigned: ${getMissingVariables(partial, environment).join(", ")}`,
804
1062
  );
805
1063
  }
806
- successors.push({ state: partial, action });
1064
+ counter.successors++;
1065
+ run.isStopped = onSuccessor({ state: partial, action });
1066
+ },
1067
+ );
1068
+ return counter.successors;
1069
+ };
1070
+
1071
+ export const getSuccessors = (
1072
+ expression: Expression,
1073
+ state: Value[],
1074
+ environment: Environment,
1075
+ scope?: Scope,
1076
+ ): Successor[] => {
1077
+ const successors: Successor[] = [];
1078
+ forEachSuccessor(
1079
+ expression,
1080
+ state,
1081
+ environment,
1082
+ (successor) => {
1083
+ successors.push(successor);
1084
+ return false;
807
1085
  },
1086
+ scope,
808
1087
  );
809
1088
  return successors;
810
1089
  };