tla-ts 0.0.10-dev.a4b81b6d0 → 0.0.10-dev.a5705bfbf

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 (76) hide show
  1. package/dist/deciders.cjs +31 -31
  2. package/dist/deciders.js +2 -2
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.d.cts +7 -1
  5. package/dist/index.d.ts +7 -1
  6. package/dist/index.js +1 -1
  7. package/dist/parallel-worker.cjs +1 -1
  8. package/dist/parallel-worker.js +1 -1
  9. package/dist/prove.cjs +42 -42
  10. package/dist/prove.js +30 -30
  11. package/dist/run.cjs +42 -42
  12. package/dist/run.js +42 -42
  13. package/dist/transfer.cjs +31 -29
  14. package/dist/transfer.js +43 -41
  15. package/dist/worker.cjs +4 -4
  16. package/dist/worker.js +4 -4
  17. package/package.json +1 -1
  18. package/src/ast.ts +10 -1
  19. package/src/check-failure.ts +5 -6
  20. package/src/check-trace.ts +4 -4
  21. package/src/check.ts +78 -90
  22. package/src/cli/tlc-output.ts +16 -2
  23. package/src/cli/tlc.ts +2 -2
  24. package/src/codegen.ts +10 -16
  25. package/src/coverage.ts +190 -1
  26. package/src/dependencies.ts +14 -2
  27. package/src/dfid.ts +80 -27
  28. package/src/environment.ts +30 -1
  29. package/src/evaluate.ts +223 -47
  30. package/src/exit-codes.ts +3 -4
  31. package/src/instances.ts +11 -1
  32. package/src/levels.ts +48 -7
  33. package/src/liveness-checker.ts +123 -70
  34. package/src/liveness.ts +4 -1
  35. package/src/obligations/source.ts +2 -0
  36. package/src/parser.ts +3 -0
  37. package/src/prover/smt/solver/card2bv.ts +7 -4
  38. package/src/prover/smt/solver/context.ts +20 -4
  39. package/src/prover/smt/solver/dependent-expr-state.ts +18 -12
  40. package/src/prover/smt/solver/dt2bv-tactic.ts +36 -20
  41. package/src/prover/smt/solver/fd-solver.ts +146 -39
  42. package/src/prover/smt/solver/maxcore.ts +2 -1
  43. package/src/prover/smt/solver/maxsmt-base.ts +1 -0
  44. package/src/prover/smt/solver/model-checker.ts +1 -1
  45. package/src/prover/smt/solver/muz/dl-context.ts +12 -40
  46. package/src/prover/smt/solver/muz/dl-mk-coi-filter.ts +1 -1
  47. package/src/prover/smt/solver/muz/dl-rule.ts +6 -7
  48. package/src/prover/smt/solver/muz/hnf.ts +2 -2
  49. package/src/prover/smt/solver/muz/horn-tactic.ts +14 -1
  50. package/src/prover/smt/solver/muz/rule-properties.ts +22 -0
  51. package/src/prover/smt/solver/muz/spacer-prop-solver.ts +1 -1
  52. package/src/prover/smt/solver/opt-context.ts +1 -0
  53. package/src/prover/smt/solver/opt-solver.ts +1 -1
  54. package/src/prover/smt/solver/parallel-tactic.ts +1 -1
  55. package/src/prover/smt/solver/qsat.ts +7 -2
  56. package/src/prover/smt/solver/rewriter-tpl.ts +7 -6
  57. package/src/prover/smt/solver/sat/inc-sat-solver.ts +45 -26
  58. package/src/prover/smt/solver/sat/sat-tactic.ts +4 -2
  59. package/src/prover/smt/solver/sat/sat2goal.ts +10 -7
  60. package/src/prover/smt/solver/smt-consequences.ts +1 -1
  61. package/src/prover/smt/solver/smt-kernel.ts +9 -2
  62. package/src/prover/smt/solver/smtlib-parser.ts +6 -2
  63. package/src/prover/smt/solver/smtlib-script.ts +197 -73
  64. package/src/prover/smt/solver/tactic.ts +64 -37
  65. package/src/resolve.ts +143 -39
  66. package/src/sany/generator-expressions.ts +2 -0
  67. package/src/sany/generator-proofs.ts +1 -0
  68. package/src/search.ts +109 -23
  69. package/src/simulate.ts +114 -124
  70. package/src/spec-description.ts +3 -0
  71. package/src/subexpressions.ts +168 -63
  72. package/src/successors.ts +30 -10
  73. package/src/tlc/replay.ts +45 -29
  74. package/src/trace-liveness.ts +529 -0
  75. package/src/typed-tier.ts +14 -4
  76. package/src/values.ts +15 -1
@@ -17,8 +17,11 @@ import {
17
17
  } from "./ast";
18
18
  import { formatRange, TlaSyntaxError, type SanyError } from "./errors";
19
19
  import { getSourceName, mapChildren, mapDefinition } from "./instances";
20
+ import { parenthesizedRanges } from "./parser";
21
+ import { STANDARD_LEVELS } from "./standard-definitions";
20
22
  import { resolveSynonym } from "./tlc/jar-data";
21
23
  import {
24
+ compareLocations,
22
25
  getArity,
23
26
  getBinder,
24
27
  getNameSelectors,
@@ -32,7 +35,6 @@ import {
32
35
  selectionErrors,
33
36
  selectionArguments,
34
37
  selections,
35
- selectionSources,
36
38
  SELECTION_NAME,
37
39
  factReferences,
38
40
  type NullSelection,
@@ -60,6 +62,17 @@ interface SelectingName {
60
62
  }
61
63
 
62
64
  const declaredArities = new WeakMap<Definition, number>();
65
+ const generatedArgumentCounts = new WeakMap<Expression, number>();
66
+
67
+ const resolveDefinition = (
68
+ definition: Definition,
69
+ resolve: (expression: Expression) => Expression,
70
+ ) => {
71
+ const resolved = mapDefinition(definition, resolve);
72
+ const arity = declaredArities.get(definition);
73
+ if (arity !== undefined) declaredArities.set(resolved, arity);
74
+ return resolved;
75
+ };
63
76
 
64
77
  const setDeclaredArities = (declarations: Declaration[], definitions: Definition[]) =>
65
78
  declarations.forEach((declaration) => {
@@ -105,29 +118,33 @@ const findLabel = (
105
118
  }
106
119
  if (expression.kind === "Let")
107
120
  return findLabel(expression.body, name, [...lets, expression.definitions]);
108
- if (expression.kind === "Lambda") return undefined;
109
- for (const child of getChildren(expression)) {
121
+ if (expression.kind === "Lambda" || expression.kind === "Selection") return undefined;
122
+ for (const child of getChildren(expression).slice(0, generatedArgumentCounts.get(expression))) {
110
123
  const found = findLabel(child, name, lets);
111
124
  if (found) return found;
112
125
  }
113
126
  return undefined;
114
127
  };
115
128
 
129
+ const getHiddenLocation = (location: SourceLocation) => ({
130
+ line: location.line,
131
+ column: location.column,
132
+ });
133
+
116
134
  const wrapLets = (body: Expression, lets: Definition[][], location: SourceLocation) =>
117
135
  lets.reduceRight<Expression>(
118
- (inner, definitions) => ({ kind: "Let", definitions, body: inner, location }),
136
+ (inner, definitions) => ({
137
+ kind: "Let",
138
+ definitions,
139
+ body: inner,
140
+ location: getHiddenLocation(location),
141
+ }),
119
142
  body,
120
143
  );
121
144
 
122
145
  const createApplication = (selection: Selection, body: Expression): Expression => {
123
146
  const location = selection.location;
124
- const wrapped = wrapLets(body, selection.lets, location);
125
- if (selection.params.length === 0 && wrapped !== body) return wrapped;
126
- if (selection.params.length === 0) {
127
- const copy = { ...body };
128
- selectionSources.set(copy, selectionSources.get(body) ?? body);
129
- return copy;
130
- }
147
+ if (selection.params.length === 0) return body;
131
148
  if (selection.args.length === 0) {
132
149
  if (selection.params.some((param) => param.arity > 0)) {
133
150
  throw createError("Selected an operator with operator parameters as an argument", location);
@@ -135,7 +152,7 @@ const createApplication = (selection: Selection, body: Expression): Expression =
135
152
  return {
136
153
  kind: "Lambda",
137
154
  params: selection.params.map((param) => param.name),
138
- body: wrapped,
155
+ body,
139
156
  location,
140
157
  };
141
158
  }
@@ -152,7 +169,7 @@ const createApplication = (selection: Selection, body: Expression): Expression =
152
169
  kind: "Operator",
153
170
  name: SELECTION_NAME,
154
171
  params: selection.params,
155
- body: wrapped,
172
+ body,
156
173
  isLocal: true,
157
174
  isRecursive: false,
158
175
  location,
@@ -165,7 +182,7 @@ const createApplication = (selection: Selection, body: Expression): Expression =
165
182
  location,
166
183
  resolution: LOCAL_RESOLUTION,
167
184
  },
168
- location,
185
+ location: getHiddenLocation(location),
169
186
  };
170
187
  };
171
188
 
@@ -194,21 +211,26 @@ const applyDefinition = (definition: Definition, selection: Selection) => {
194
211
  location,
195
212
  resolution: LOCAL_RESOLUTION,
196
213
  };
197
- return createApplication({ ...selection, params: [...selection.params, ...params] }, reference);
214
+ return createApplication(
215
+ { ...selection, params: [...selection.params, ...params] },
216
+ wrapLets(reference, selection.lets, location),
217
+ );
198
218
  };
199
219
 
200
220
  const selectSubexpression = (
201
221
  selectors: Selector[],
202
222
  lookup: Lookup,
203
- isGoal: (name: string) => boolean,
204
223
  reference: Expression & SelectingName,
205
224
  isOperator: boolean,
206
225
  ): Expression => {
207
226
  const location = reference.location;
208
227
  const selection: Selection = { params: [], args: [], lets: [], location };
209
228
  let selectorIndex = 0;
229
+ let operatorIndex = -1;
210
230
  let current: Expression | undefined;
231
+ let unwrappedSelection: Expression | undefined;
211
232
  let isFailed = false;
233
+ let generatedArguments = 0;
212
234
  const nulls: NullSelection[] = [];
213
235
  const applications: SelectionArguments[] = [];
214
236
  let expressionError: SanyError | undefined;
@@ -229,6 +251,7 @@ const selectSubexpression = (
229
251
  nulls,
230
252
  error: { location: formatRange(range), message },
231
253
  });
254
+ generatedArgumentCounts.set(reference, generatedArguments);
232
255
  isFailed = true;
233
256
  return undefined;
234
257
  };
@@ -265,9 +288,13 @@ const selectSubexpression = (
265
288
  if (found.args.length !== arity) {
266
289
  const range = reference.argumentRanges?.[selectorIndex] ?? location.range;
267
290
  const message = `The operator ${resolveSynonym(found.name)} requires ${arity - found.earlier} arguments.`;
268
- if (!isOperator || found.args.length !== 0) return report(range, message);
291
+ if (!isOperator || found.args.length !== 0) {
292
+ generatedArguments += found.earlier;
293
+ return report(range, message);
294
+ }
269
295
  deferToExpression(range, message);
270
296
  }
297
+ generatedArguments += found.args.length;
271
298
  found.args.forEach((arg, index) =>
272
299
  selectionArguments.set(arg, {
273
300
  operator: found.name,
@@ -304,6 +331,7 @@ const selectSubexpression = (
304
331
  selection.params.push(...definition.params);
305
332
  selection.args.push(...found.args);
306
333
  current = definition.body;
334
+ operatorIndex = selectorIndex;
307
335
  selectorIndex++;
308
336
  return undefined;
309
337
  };
@@ -321,6 +349,7 @@ const selectSubexpression = (
321
349
  if (!isOperator) return report(range, message);
322
350
  deferToExpression(range, message);
323
351
  }
352
+ generatedArguments += selector.args.length;
324
353
  selection.lets.push(...found.lets);
325
354
  selection.params.push(...params.map((name) => ({ name, arity: 0 })));
326
355
  selection.args.push(...selector.args);
@@ -334,31 +363,23 @@ const selectSubexpression = (
334
363
  if (!current) throw createError("Subexpression selection failed", location);
335
364
  if (selector.kind === "Colon") return;
336
365
  const range = reference.selectorRanges?.[selectorIndex];
337
- const currentRange = current.location.range;
338
- if (
339
- (current.kind === "Subexpression" ||
340
- (current.kind === "Identifier" && current.name.includes("!")) ||
341
- (current.kind === "Apply" && current.operator.includes("!"))) &&
342
- (currentRange?.module !== location.range?.module || isBefore(current.location, location))
343
- ) {
344
- const selected = selectionErrors.has(current)
345
- ? current
346
- : resolveSubexpressions(current, lookup, isGoal);
347
- const noOperands = `Choosing operand \`${getSelectorName(selector)}' of subexpression with no operands.`;
348
- if (selectionErrors.has(selected)) {
349
- report(range, noOperands);
350
- return;
351
- }
352
- if (selections.has(selected) && selected.kind !== "Lambda") {
353
- checkNull(selected, noOperands, true);
354
- current =
355
- selected.kind === "Let" &&
356
- selected.body.kind === "Apply" &&
357
- selected.body.operator === SELECTION_NAME
358
- ? selected.body
366
+ const noOperands = `Choosing operand \`${getSelectorName(selector)}' of subexpression with no operands.`;
367
+ if (selectionErrors.has(current)) {
368
+ report(range, noOperands);
369
+ return;
370
+ }
371
+ if (selections.has(current) && current.kind !== "Lambda" && current !== unwrappedSelection) {
372
+ checkNull(current, noOperands, true);
373
+ unwrappedSelection = current;
374
+ current =
375
+ current.kind === "Let" &&
376
+ current.body.kind === "Apply" &&
377
+ current.body.operator === SELECTION_NAME
378
+ ? current.body
379
+ : current.kind === "Selection"
380
+ ? current
359
381
  : // SANY wraps a selection without parameters in $Nop, whose only operand is the selection.
360
- { kind: "Tuple", elements: [selected], location: selected.location };
361
- }
382
+ { kind: "Tuple", elements: [current], location: current.location };
362
383
  }
363
384
  if (current.kind === "Label") {
364
385
  current = current.body;
@@ -386,10 +407,14 @@ const selectSubexpression = (
386
407
  }
387
408
  selection.params.push(...binder.names.map((name) => ({ name, arity: 0 })));
388
409
  if (selector.kind === "Bound") {
410
+ generatedArguments += selector.args.length;
389
411
  selection.args.push(...selector.args);
390
412
  applications.push(getExpressionArguments("", range, selector.args));
391
413
  }
392
- current = binder.body;
414
+ current =
415
+ selections.has(current) && binder.body.kind === "Selection"
416
+ ? binder.body.expression
417
+ : binder.body;
393
418
  return;
394
419
  }
395
420
  if (current.kind === "Case") {
@@ -480,6 +505,24 @@ const selectSubexpression = (
480
505
  followLabels();
481
506
  continue;
482
507
  }
508
+ if (selector.kind === "Colon" && selectors[selectorIndex - 1].kind !== "Name") {
509
+ report(
510
+ reference.selectorRanges?.[selectorIndex - 1],
511
+ "!: should not follow an operand selector.",
512
+ );
513
+ return reference;
514
+ }
515
+ if (
516
+ selector.kind === "Colon" &&
517
+ selectors[selectorIndex + 1]?.kind !== "Name" &&
518
+ (operatorIndex !== selectorIndex - 1 || selectorIndex !== selectors.length - 1)
519
+ ) {
520
+ report(
521
+ reference.selectorRanges?.[selectorIndex],
522
+ "`!:' can be used only after a name and either at the end after an\noperator name or before an operator name.",
523
+ );
524
+ return reference;
525
+ }
483
526
  if (selector.kind !== "Colon") {
484
527
  checkNull(
485
528
  current,
@@ -510,7 +553,23 @@ const selectSubexpression = (
510
553
  }
511
554
  deferToExpression(location.range, message);
512
555
  }
513
- const selected = createApplication(selection, current);
556
+ const body = wrapLets(current, selection.lets, location);
557
+ const share = (expression: Expression): Expression => ({
558
+ kind: "Selection",
559
+ expression,
560
+ // SANY's $Nop, which TLC's call stack shows at the reference, wraps only a selection without parameters.
561
+ location:
562
+ selection.params.length === 0 && body.kind !== "Lambda"
563
+ ? location
564
+ : getHiddenLocation(location),
565
+ });
566
+ // SANY selects a LAMBDA itself rather than wrapping it in $Nop, so the selection keeps its arity.
567
+ const selected = createApplication(
568
+ selection,
569
+ body.kind !== "Lambda"
570
+ ? share(body)
571
+ : { ...body, body: selections.has(body) ? body.body : share(body.body), location },
572
+ );
514
573
  nullSelections.set(selected, nulls);
515
574
  if (expressionError) operatorSelectionErrors.set(selected, expressionError);
516
575
  return selected;
@@ -543,14 +602,24 @@ const resolveSubexpressions = (
543
602
  if (!containsReference(expression)) return expression;
544
603
  if (expression.kind === "Let") {
545
604
  if (expression.recursive) setDeclaredArities(expression.recursive, expression.definitions);
605
+ const resolved: Definition[] = [];
546
606
  const local: Lookup = (name) =>
547
- expression.definitions.find((definition) => definition.name === name) ?? lookup(name);
607
+ resolved.find((definition) => definition.name === name) ??
608
+ expression.definitions.find((definition) => definition.name === name) ??
609
+ lookup(name);
548
610
  const isLocalGoal = (name: string) =>
549
611
  !expression.definitions.some((definition) => definition.name === name) && isGoal(name);
550
- const mapped = mapChildren(expression, (child) =>
551
- resolveSubexpressions(child, local, isLocalGoal),
612
+ expression.definitions.forEach((definition) =>
613
+ resolved.push(
614
+ resolveDefinition(definition, (child) => resolveSubexpressions(child, local, isLocalGoal)),
615
+ ),
552
616
  );
553
- return mapped.kind === "Let" && mapped.instances
617
+ const mapped = {
618
+ ...expression,
619
+ definitions: resolved,
620
+ body: resolveSubexpressions(expression.body, local, isLocalGoal),
621
+ };
622
+ return mapped.instances
554
623
  ? {
555
624
  ...mapped,
556
625
  instances: mapped.instances.map((instance) =>
@@ -560,6 +629,14 @@ const resolveSubexpressions = (
560
629
  : mapped;
561
630
  }
562
631
  const callee = expression.kind === "Apply" ? lookup(expression.operator) : undefined;
632
+ // SANY expects an operator argument only of a user-defined operator, which a standard module's built-in operators stand for.
633
+ const calleeParams =
634
+ callee?.kind === "Operator"
635
+ ? callee.params
636
+ : expression.kind === "Apply"
637
+ ? STANDARD_LEVELS.get(expression.operator.slice(expression.operator.lastIndexOf("!") + 1))
638
+ ?.formals
639
+ : undefined;
563
640
  const rebuilt =
564
641
  expression.kind === "Apply"
565
642
  ? {
@@ -569,7 +646,7 @@ const resolveSubexpressions = (
569
646
  arg,
570
647
  lookup,
571
648
  isGoal,
572
- callee?.kind !== "Operator" || callee.params[index]?.arity !== 0,
649
+ calleeParams !== undefined && calleeParams[index]?.arity !== 0,
573
650
  ),
574
651
  ),
575
652
  }
@@ -587,7 +664,16 @@ const resolveSubexpressions = (
587
664
  ? getQualifiedSelectors(mapped.operator, mapped.args, mapped.argumentCounts, lookup)
588
665
  : undefined;
589
666
  const first = selectors?.[0];
590
- if (!selectors) return mapped;
667
+ const setGeneratedArguments = (count: number) => {
668
+ generatedArgumentCounts.set(expression, count);
669
+ generatedArgumentCounts.set(mapped, count);
670
+ };
671
+ if (!selectors) {
672
+ if (mapped.kind === "Apply" && mapped.operator.includes("!") && !lookup(mapped.operator)) {
673
+ setGeneratedArguments(0);
674
+ }
675
+ return mapped;
676
+ }
591
677
  const nameCount = selectors.findIndex((selector) => selector.kind !== "Name");
592
678
  const names = selectors.slice(0, nameCount === -1 ? selectors.length : nameCount);
593
679
  const isQualified = names.some(
@@ -601,9 +687,16 @@ const resolveSubexpressions = (
601
687
  ),
602
688
  );
603
689
  if (first?.kind === "Name" && isGoal(first.name) && !isQualified) return mapped;
604
- if (first?.kind === "Name" && selectors[1].kind !== "Name" && !lookup(first.name)) return mapped;
605
- const selected = selectSubexpression(selectors, lookup, isGoal, mapped, isOperator);
606
- if (selected !== mapped) selections.add(selected);
690
+ if (first?.kind === "Name" && selectors[1].kind !== "Name" && !lookup(first.name)) {
691
+ setGeneratedArguments(0);
692
+ return mapped;
693
+ }
694
+ const selected = selectSubexpression(selectors, lookup, mapped, isOperator);
695
+ const generated = generatedArgumentCounts.get(mapped);
696
+ if (generated !== undefined) setGeneratedArguments(generated);
697
+ const parenthesized = parenthesizedRanges.get(expression);
698
+ if (parenthesized && selected !== mapped) parenthesizedRanges.set(selected, parenthesized);
699
+ if (selected !== mapped) selections.set(selected, mapped);
607
700
  return selected;
608
701
  };
609
702
 
@@ -675,13 +768,15 @@ export const resolveModuleSubexpressions = (module: Module, available: Definitio
675
768
  (definition.isRecursive && declared !== undefined && isBefore(declared, location))
676
769
  );
677
770
  };
771
+ const resolvedDefinitions = new Map<Definition, Definition>();
678
772
  const getVisibleLookup =
679
773
  (lookup: Lookup, location: SourceLocation): Lookup =>
680
774
  (name) => {
681
775
  const definition = lookup(name);
682
- return definition && (!own.has(definition) || isDeclaredBefore(definition, location))
683
- ? definition
684
- : undefined;
776
+ if (!definition || (own.has(definition) && !isDeclaredBefore(definition, location))) {
777
+ return undefined;
778
+ }
779
+ return resolvedDefinitions.get(definition) ?? definition;
685
780
  };
686
781
  const proofLookup = createLookup([...available, ...module.definitions]);
687
782
  const isGoal = (name: string) => goals.has(name);
@@ -730,7 +825,7 @@ export const resolveModuleSubexpressions = (module: Module, available: Definitio
730
825
  definitions: body.definitions.map((definition) =>
731
826
  definition.kind === "Instance"
732
827
  ? resolveInstance(definition, lookup, isProofGoal)
733
- : mapDefinition(definition, resolveInProof),
828
+ : resolveDefinition(definition, resolveInProof),
734
829
  ),
735
830
  };
736
831
  case "Have":
@@ -760,8 +855,8 @@ export const resolveModuleSubexpressions = (module: Module, available: Definitio
760
855
  body: resolver.resolveStep(step.body),
761
856
  proof: resolver.resolveProof(step.proof),
762
857
  };
763
- if (step.body.kind === "Define") {
764
- const definitions = step.body.definitions.flatMap((definition) =>
858
+ if (resolved.body.kind === "Define") {
859
+ const definitions = resolved.body.definitions.flatMap((definition) =>
765
860
  definition.kind === "Instance" ? [] : [definition],
766
861
  );
767
862
  const outer = resolver.lookup;
@@ -775,6 +870,19 @@ export const resolveModuleSubexpressions = (module: Module, available: Definitio
775
870
  };
776
871
  return { lookup, resolveFacts, resolveStatement, resolveStep, resolveProof };
777
872
  };
873
+ const hasReferences =
874
+ module.definitions.some((definition) => containsReference(definition.body)) ||
875
+ module.assumptions.some((assumption) => containsReference(assumption.expression));
876
+ if (hasReferences) {
877
+ [...module.definitions, ...clauses]
878
+ .toSorted((left, right) => compareLocations(left.location, right.location))
879
+ .forEach((definition) =>
880
+ resolvedDefinitions.set(
881
+ definition,
882
+ resolveDefinition(definition, (expression) => resolve(expression, definition.location)),
883
+ ),
884
+ );
885
+ }
778
886
  const resolved = {
779
887
  ...module,
780
888
  theorems: module.theorems.map((theorem) => {
@@ -799,14 +907,11 @@ export const resolveModuleSubexpressions = (module: Module, available: Definitio
799
907
  ),
800
908
  })),
801
909
  };
802
- const hasReferences =
803
- module.definitions.some((definition) => containsReference(definition.body)) ||
804
- module.assumptions.some((assumption) => containsReference(assumption.expression));
805
910
  if (!hasReferences) return resolved;
806
911
  return {
807
912
  ...resolved,
808
- definitions: module.definitions.map((definition) =>
809
- mapDefinition(definition, (expression) => resolve(expression, definition.location)),
913
+ definitions: module.definitions.map(
914
+ (definition) => resolvedDefinitions.get(definition) ?? definition,
810
915
  ),
811
916
  assumptions: module.assumptions.map((assumption) => ({
812
917
  ...assumption,
package/src/successors.ts CHANGED
@@ -274,6 +274,7 @@ const getUnchangedVariables = (
274
274
 
275
275
  const ACTION_KINDS = new Set([
276
276
  "Label",
277
+ "Selection",
277
278
  "And",
278
279
  "Or",
279
280
  "Exists",
@@ -1092,10 +1093,16 @@ const compileAction = (
1092
1093
  case "Let": {
1093
1094
  const body = getChildRunner(expression.body);
1094
1095
  const reevaluated = getReevaluatedDefinitions(expression.definitions, environment);
1096
+ // TLC's Tool#enabled binds a LET's definitions to lazy values it caches, where Tool#getNextStates evaluates them at every use.
1097
+ const cached = { impure: new Set<Definition>(), streamed: reevaluated.streamed };
1095
1098
  return (run, scope, partial, action, isFixed, onComplete) =>
1096
1099
  body(
1097
1100
  run,
1098
- bindDefinitions(expression.definitions, getContext(run, scope, partial), reevaluated),
1101
+ bindDefinitions(
1102
+ expression.definitions,
1103
+ getContext(run, scope, partial),
1104
+ run.isEnabled ? cached : reevaluated,
1105
+ ),
1099
1106
  partial,
1100
1107
  action,
1101
1108
  isFixed,
@@ -1140,6 +1147,8 @@ const compileAction = (
1140
1147
  );
1141
1148
  case "Label":
1142
1149
  return getChildRunner(expression.body);
1150
+ case "Selection":
1151
+ return getChildRunner(expression.expression);
1143
1152
  case "Identifier":
1144
1153
  return compileIdentifier(expression, environment, isInit);
1145
1154
  case "FunctionApplication":
@@ -1283,6 +1292,20 @@ export const getInitialStates = (
1283
1292
  return states;
1284
1293
  };
1285
1294
 
1295
+ export const createIncompleteSuccessorError = (
1296
+ partial: State,
1297
+ action: string | undefined,
1298
+ environment: Environment,
1299
+ ) => {
1300
+ const missing = environment.variables.filter((_name, index) => partial[index] === undefined);
1301
+ return new TlaIncompleteSuccessorError(
1302
+ `Successor state is not completely specified by action ${action ?? "Next"}. The following variables are not assigned: ${missing.join(", ")}`,
1303
+ partial,
1304
+ action,
1305
+ missing,
1306
+ );
1307
+ };
1308
+
1286
1309
  export const forEachSuccessor = (
1287
1310
  expression: Expression,
1288
1311
  state: Value[],
@@ -1291,6 +1314,7 @@ export const forEachSuccessor = (
1291
1314
  scope?: Scope,
1292
1315
  generator?: JavaRandom,
1293
1316
  onUnsatisfied?: Run["onUnsatisfied"],
1317
+ onIncomplete?: (state: State, action: string | undefined) => void,
1294
1318
  ) => {
1295
1319
  const run: Run = {
1296
1320
  environment,
@@ -1311,15 +1335,11 @@ export const forEachSuccessor = (
1311
1335
  (partial, action) => {
1312
1336
  if (run.isStopped) return;
1313
1337
  if (!isComplete(partial)) {
1314
- const missing = environment.variables.filter(
1315
- (_name, index) => partial[index] === undefined,
1316
- );
1317
- throw new TlaIncompleteSuccessorError(
1318
- `Successor state is not completely specified by action ${action ?? "Next"}. The following variables are not assigned: ${missing.join(", ")}`,
1319
- partial,
1320
- action,
1321
- missing,
1322
- );
1338
+ if (onIncomplete) {
1339
+ onIncomplete(partial, action);
1340
+ return;
1341
+ }
1342
+ throw createIncompleteSuccessorError(partial, action, environment);
1323
1343
  }
1324
1344
  run.successors++;
1325
1345
  run.isStopped = onSuccessor({ state: partial, action });
package/src/tlc/replay.ts CHANGED
@@ -2,8 +2,9 @@ import type { Expression, SourceLocation } from "../ast";
2
2
  import { TlaEvaluationError } from "../errors";
3
3
  import { isConstantLevel } from "../dependencies";
4
4
  import {
5
+ chargeContexts,
5
6
  countCoverage,
6
- costCoverage,
7
+ createChargedEnumerator,
7
8
  getCoverageNode,
8
9
  runAtCoverageNode,
9
10
  type Coverage,
@@ -308,16 +309,19 @@ const visitAt = (
308
309
  case "Let": {
309
310
  const context = getContext(replay, scope, partial);
310
311
  const definitions = expression.definitions;
311
- const bound = replay.coverage
312
- ? bindDefinitions(definitions, context, {
313
- impure: new Set(definitions),
314
- streamed: new Set(),
315
- })
316
- : bindDefinitions(definitions, context);
312
+ const bound =
313
+ replay.coverage && !replay.isEnabled
314
+ ? bindDefinitions(definitions, context, {
315
+ impure: new Set(definitions),
316
+ streamed: new Set(),
317
+ })
318
+ : bindDefinitions(definitions, context);
317
319
  return visit(replay, expression.body, items, bound, partial, node);
318
320
  }
319
321
  case "Label":
320
322
  return visit(replay, expression.body, items, scope, partial, node);
323
+ case "Selection":
324
+ return visit(replay, expression.expression, items, scope, partial, node);
321
325
  default:
322
326
  if (LITERAL_KINDS.has(expression.kind))
323
327
  throw new TlaEvaluationError("The state relation is not a boolean expression.");
@@ -433,11 +437,6 @@ const createItem = (
433
437
  rest: ActionItem | undefined,
434
438
  ): ActionItem => ({ kind, expression, scope, node, rest });
435
439
 
436
- const getEnumerated = (replay: Replay, set: Value, elements: Value[]) => {
437
- if (replay.coverage) costCoverage(replay.coverage, set, elements.length + 1);
438
- return elements;
439
- };
440
-
441
440
  const visitAssignment = (
442
441
  replay: Replay,
443
442
  expression: Expression & { kind: "Apply" },
@@ -472,11 +471,15 @@ const visitAssignment = (
472
471
  }
473
472
  if (!isTlcEnumerable(set))
474
473
  throw new TlaEvaluationError("The right side of \\IN is not enumerable.");
475
- return getEnumerated(replay, set, getStreamedElements(set)).reduce<boolean>(
476
- (isProduced, element) =>
477
- proceed(replay, items, assign(partial, index, element), node) || isProduced,
478
- false,
479
- );
474
+ const elements = getStreamedElements(set);
475
+ const enumerator =
476
+ replay.coverage && createChargedEnumerator(replay.coverage, set, elements, false);
477
+ const isProduced = elements.reduce<boolean>((isProduced, element) => {
478
+ enumerator?.next();
479
+ return proceed(replay, items, assign(partial, index, element), node) || isProduced;
480
+ }, false);
481
+ enumerator?.next();
482
+ return isProduced;
480
483
  };
481
484
 
482
485
  const ASSIGNMENT_OPERATORS = new Set(["=", "\\in", "\\subseteq"]);
@@ -519,19 +522,26 @@ const getBindings = (
519
522
  ) => {
520
523
  const bindings: (Scope | undefined)[] = [];
521
524
  const version = replay.environment.runtime.version;
522
- const sets = runAt(replay, node, () =>
525
+ const bounded = runAt(replay, node, () =>
523
526
  mapBoundSets(expression.bounds, (set) => {
524
527
  const value = evaluate(set, context);
525
- const elements = getQuantifierElements(value, set, version);
526
- if (Array.isArray(elements)) getEnumerated(replay, value, elements);
527
- return elements;
528
+ return { value, elements: getQuantifierElements(value, set, version) };
528
529
  }),
529
530
  );
531
+ const sets = bounded.map((item) => item.elements);
530
532
  forEachBoundElement(expression.bounds, sets, context.scope, (bound) => {
531
533
  bindings.push(bound);
532
534
  return false;
533
535
  });
534
- return bindings;
536
+ const coverage = replay.coverage;
537
+ const charge =
538
+ coverage &&
539
+ chargeContexts(
540
+ coverage,
541
+ bounded.map((item) => item.value),
542
+ sets,
543
+ );
544
+ return { bindings, charge };
535
545
  };
536
546
 
537
547
  const visitApplication = (
@@ -606,14 +616,17 @@ const visitApplication = (
606
616
  );
607
617
  return isPrefixProduced || isLastProduced;
608
618
  }
609
- case "Exists":
610
- return getBindings(replay, expression, context, node).reduce(
611
- (isProduced, bound) =>
612
- visit(replay, expression.body, items, bound, partial, node) || isProduced,
613
- false,
614
- );
619
+ case "Exists": {
620
+ const bound = getBindings(replay, expression, context, node);
621
+ return bound.bindings.reduce((isProduced, scope) => {
622
+ bound.charge?.();
623
+ return visit(replay, expression.body, items, scope, partial, node) || isProduced;
624
+ }, false);
625
+ }
615
626
  case "Forall": {
616
- const bounds = getBindings(replay, expression, context, node);
627
+ const bound = getBindings(replay, expression, context, node);
628
+ const bounds = bound.bindings;
629
+ bounds.forEach(() => bound.charge?.());
617
630
  if (bounds.length === 0) return proceed(replay, items, partial, node);
618
631
  const rest = bounds
619
632
  .slice(1)
@@ -915,6 +928,9 @@ export const getPrimedRanges = (actions: ReplayAction[], environment: Environmen
915
928
  case "Label":
916
929
  collect(expression.body, scope, visited);
917
930
  return;
931
+ case "Selection":
932
+ collect(expression.expression, scope, visited);
933
+ return;
918
934
  case "FunctionApplication":
919
935
  collect(expression.function, scope, visited);
920
936
  return;