tla-ts 0.0.10-dev.b4a856304 → 0.0.10-dev.f16792fed

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 (72) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.d.cts +34 -0
  3. package/dist/index.d.ts +34 -0
  4. package/dist/index.js +1 -1
  5. package/dist/parallel-worker.cjs +1 -1
  6. package/dist/parallel-worker.js +1 -1
  7. package/dist/prove.cjs +48 -48
  8. package/dist/prove.js +29 -29
  9. package/dist/run.cjs +40 -40
  10. package/dist/run.js +31 -31
  11. package/dist/transfer.cjs +36 -36
  12. package/dist/transfer.js +40 -40
  13. package/package.json +1 -1
  14. package/src/ast.ts +0 -5
  15. package/src/bags.ts +2 -5
  16. package/src/builtins.ts +1 -8
  17. package/src/check.ts +128 -193
  18. package/src/cli/options.ts +1 -1
  19. package/src/cli/run.ts +0 -2
  20. package/src/cli/runtime-invariants.ts +8 -7
  21. package/src/cli/shared.ts +0 -4
  22. package/src/cli/tlc-arguments.ts +24 -30
  23. package/src/cli/tlc-usage.ts +2 -3
  24. package/src/coverage.ts +3 -8
  25. package/src/evaluate.ts +26 -102
  26. package/src/java-hash.ts +7 -0
  27. package/src/java-overrides.ts +4 -9
  28. package/src/json-reader.ts +1 -2
  29. package/src/json.ts +4 -11
  30. package/src/levels.ts +7 -15
  31. package/src/lexer.ts +72 -83
  32. package/src/liveness-checker.ts +77 -106
  33. package/src/liveness.ts +10 -34
  34. package/src/parallel.ts +26 -33
  35. package/src/parser.ts +163 -226
  36. package/src/pcal/ast.ts +53 -65
  37. package/src/pcal/parse.ts +144 -191
  38. package/src/pcal/tokenize.ts +91 -115
  39. package/src/pcal/translate.ts +83 -98
  40. package/src/pcal/validate.ts +48 -50
  41. package/src/pretty-print.ts +6 -6
  42. package/src/randomization.ts +1 -4
  43. package/src/resolve.ts +12 -70
  44. package/src/runtime.ts +11 -18
  45. package/src/sany/builtins.ts +1 -3
  46. package/src/sany/generator.ts +74 -127
  47. package/src/sany/levels.ts +5 -9
  48. package/src/sany/lexer.ts +40 -44
  49. package/src/sany/linter.ts +0 -1
  50. package/src/sany/nodes.ts +4 -10
  51. package/src/search.ts +1 -1
  52. package/src/simulate.ts +50 -59
  53. package/src/spec-description.ts +23 -34
  54. package/src/standard-definitions.ts +0 -3
  55. package/src/standard-modules.ts +1 -12
  56. package/src/state-graph.ts +3 -15
  57. package/src/subexpressions.ts +38 -47
  58. package/src/successors.ts +14 -35
  59. package/src/symmetry.ts +4 -19
  60. package/src/tlc/fingerprint.ts +15 -21
  61. package/src/tlc/jar-data.ts +0 -1
  62. package/src/tlc/messages.ts +2 -21
  63. package/src/tlc/order.ts +22 -47
  64. package/src/tlc/random.ts +18 -30
  65. package/src/tlc/replay.ts +13 -33
  66. package/src/tlc/summary.ts +36 -45
  67. package/src/tlc/tlc-operators.ts +4 -14
  68. package/src/tlc/tlcext-operators.ts +11 -15
  69. package/src/tlc/trace-spec.ts +4 -9
  70. package/src/tlc/value-stream.ts +9 -13
  71. package/src/tlc/version.ts +0 -1
  72. package/src/values.ts +74 -186
@@ -27,16 +27,11 @@ export interface LiveSuccessor {
27
27
  export interface Lasso {
28
28
  prefix: number[];
29
29
  cycle: number[];
30
- // TLC warns that a stuttering counterexample to a liveness property may come from a missing
31
- // fairness constraint.
32
30
  isCycleConstrained: boolean;
33
31
  }
34
32
 
35
33
  export interface LivenessChecker {
36
- // How many solutions of the negated properties TLC checks, which it calls branches.
37
34
  branches: number;
38
- // With a tracing environment, the checks record their call stack as TLC's replay of a failed
39
- // check does.
40
35
  addInitialState: (index: number, state: Value[], tracing?: Environment) => void;
41
36
  addState: (
42
37
  index: number,
@@ -53,8 +48,6 @@ interface SearchGraph {
53
48
  getSuccessors: (index: number, state: Value[]) => LiveSuccessor[];
54
49
  }
55
50
 
56
- // Numbers in a typed array that doubles as it fills, so a graph of millions of nodes takes a few
57
- // bytes for each.
58
51
  interface Pool<Data extends Int32Array | Uint8Array> {
59
52
  data: Data;
60
53
  length: number;
@@ -66,17 +59,12 @@ interface BehaviorGraph {
66
59
  actionIds: Int32Array;
67
60
  nodeStates: number[];
68
61
  nodeTableaux: number[];
69
- // Where a node's edges start in edgeTargets, or UNWRITTEN until TLC writes the node with its
70
- // successors, and how many it has.
71
62
  edgeStarts: number[];
72
63
  edgeCounts: number[];
73
64
  edgeTargets: Pool<Int32Array>;
74
- // The results of the action checks on each edge, and of the state checks on each state, which
75
- // are the same for every node of the state.
76
65
  edgeActions: Pool<Uint8Array>;
77
66
  stateResults: Pool<Uint8Array>;
78
67
  initial: number[];
79
- // The nodes of each state in the order they are recorded, a list through nextSiblings.
80
68
  firstNodes: Pool<Int32Array>;
81
69
  lastNodes: Pool<Int32Array>;
82
70
  nextSiblings: number[];
@@ -137,8 +125,7 @@ const writeToPool = <Data extends Int32Array | Uint8Array>(
137
125
 
138
126
  const NO_NODE = -1;
139
127
 
140
- // A Map holds at most 2^24 entries, so each tableau node of a state finds its node in an
141
- // open-addressing table of (state plus one, tableau node, node) triples.
128
+ // A Map holds at most 2^24 entries, so an open-addressing table of (state plus one, tableau node, node) triples finds each node.
142
129
  interface NodeTable {
143
130
  slots: Int32Array;
144
131
  size: number;
@@ -225,9 +212,9 @@ export const createLivenessChecker = (
225
212
  solution,
226
213
  actionIds: Int32Array.from(solution.actionChecks, (predicate) => {
227
214
  const key = getTermKey(predicate);
228
- const id = actionIds.get(key) ?? actionIds.size;
229
- actionIds.set(key, id);
230
- return id;
215
+ const actionId = actionIds.get(key) ?? actionIds.size;
216
+ actionIds.set(key, actionId);
217
+ return actionId;
231
218
  }),
232
219
  nodeStates: [],
233
220
  nodeTableaux: [],
@@ -244,23 +231,17 @@ export const createLivenessChecker = (
244
231
  isDone: [],
245
232
  errorNode: undefined,
246
233
  }));
247
- const capacity = { multiplier: 1 };
234
+ let capacityMultiplier = 1;
248
235
 
249
- // TLC evaluates every check on its own. Without side effects the order can't show, so the
250
- // checks of one state share their results.
251
236
  const isShared = !hasRepeatedEffects(environment);
252
- const evaluation = { environment };
253
- const shared = {
254
- states: new Map<Value[], Map<LiveExpression, boolean>>(),
255
- actions: new Map<Value[], Map<Value[], Int8Array>>(),
256
- };
237
+ let evaluationEnvironment = environment;
238
+ const sharedStates = new Map<Value[], Map<LiveExpression, boolean>>();
239
+ const sharedActions = new Map<Value[], Map<Value[], Int8Array>>();
257
240
  const getStateCache = (state: Value[]) => {
258
- const cache = shared.states.get(state) ?? new Map<LiveExpression, boolean>();
259
- shared.states.set(state, cache);
241
+ const cache = sharedStates.get(state) ?? new Map<LiveExpression, boolean>();
242
+ sharedStates.set(state, cache);
260
243
  return cache;
261
244
  };
262
- // TLCGet("level") reads the level of the state whose successors the checks run on.
263
- // A check that fails leaves no source, so the failure's trace reads the levels of the search.
264
245
  const evaluateFrom = <Result>(source: number | undefined, evaluate: () => Result) => {
265
246
  environment.runtime.setLivenessSource(source);
266
247
  try {
@@ -274,7 +255,7 @@ export const createLivenessChecker = (
274
255
  predicate,
275
256
  state,
276
257
  undefined,
277
- evaluation.environment,
258
+ evaluationEnvironment,
278
259
  isShared ? getStateCache(state) : new Map(),
279
260
  );
280
261
  const checkState = (behavior: BehaviorGraph, source: number, state: Value[]) =>
@@ -293,27 +274,27 @@ export const createLivenessChecker = (
293
274
  const predicates = behavior.solution.actionChecks;
294
275
  if (!isShared) {
295
276
  return Uint8Array.from(predicates, (predicate) =>
296
- evaluateLive(predicate, state, successor, evaluation.environment, new Map()) ? 1 : 0,
277
+ evaluateLive(predicate, state, successor, evaluationEnvironment, new Map()) ? 1 : 0,
297
278
  );
298
279
  }
299
280
  const stateCache = getStateCache(state);
300
- const results = shared.actions.get(state) ?? new Map<Value[], Int8Array>();
301
- shared.actions.set(state, results);
281
+ const results = sharedActions.get(state) ?? new Map<Value[], Int8Array>();
282
+ sharedActions.set(state, results);
302
283
  const known = results.get(successor) ?? new Int8Array(actionIds.size).fill(-1);
303
284
  results.set(successor, known);
304
- return Uint8Array.from(behavior.actionIds, (id, position) => {
305
- if (known[id] === -1) {
306
- known[id] = evaluateLive(
285
+ return Uint8Array.from(behavior.actionIds, (actionId, position) => {
286
+ if (known[actionId] === -1) {
287
+ known[actionId] = evaluateLive(
307
288
  predicates[position],
308
289
  state,
309
290
  successor,
310
- evaluation.environment,
291
+ evaluationEnvironment,
311
292
  stateCache,
312
293
  )
313
294
  ? 1
314
295
  : 0;
315
296
  }
316
- return known[id];
297
+ return known[actionId];
317
298
  });
318
299
  });
319
300
  const isConsistent = (
@@ -325,8 +306,6 @@ export const createLivenessChecker = (
325
306
  evaluateFrom(source, () =>
326
307
  tableau.nodes[node].statePredicates.every((predicate) => evaluateState(predicate, state)),
327
308
  );
328
- // TLC 1.7.4 doesn't stop at a tableau node that accepts every behavior, so it finds a violation
329
- // of a property like a safety property only in its liveness checks.
330
309
  const isAccepting = (tableau: Tableau, node: number) => {
331
310
  if (environment.runtime.version === "1.7.4") return false;
332
311
  const tableauNode = tableau.nodes[node];
@@ -395,16 +374,12 @@ export const createLivenessChecker = (
395
374
  behavior.edgeCounts[node] = targets.length;
396
375
  };
397
376
 
398
- // TLC collects a state's successors and the state itself in a SetOfStates, a hash set it
399
- // iterates in slot order. Its capacity starts at 16 times a factor that grows with every set
400
- // that outgrows it.
377
+ // TLC iterates a state's successors and the state itself in the slot order of a SetOfStates, a hash set whose capacity grows with every set that outgrows it.
401
378
  const getSuccessorSet = (index: number, state: Value[], successors: LiveSuccessor[]) => {
402
- const table = {
403
- slots: Array.from<LiveSuccessor | undefined>({
404
- length: SUCCESSOR_CAPACITY * capacity.multiplier,
405
- }),
406
- count: 0,
407
- };
379
+ let successorSlots = Array.from<LiveSuccessor | undefined>({
380
+ length: SUCCESSOR_CAPACITY * capacityMultiplier,
381
+ });
382
+ let successorCount = 0;
408
383
  const insertSuccessor = (slots: (LiveSuccessor | undefined)[], successor: LiveSuccessor) => {
409
384
  for (
410
385
  let slot = (search.getFingerprint(successor) & 0x7fffffff) % slots.length;
@@ -421,17 +396,17 @@ export const createLivenessChecker = (
421
396
  }
422
397
  };
423
398
  const addSuccessor = (successor: LiveSuccessor) => {
424
- if (table.count >= table.slots.length >> 1) {
425
- const old = table.slots;
426
- table.slots = Array.from<LiveSuccessor | undefined>({ length: 2 * old.length + 1 });
427
- for (const entry of old) if (entry) insertSuccessor(table.slots, entry);
399
+ if (successorCount >= successorSlots.length >> 1) {
400
+ const old = successorSlots;
401
+ successorSlots = Array.from<LiveSuccessor | undefined>({ length: 2 * old.length + 1 });
402
+ for (const entry of old) if (entry) insertSuccessor(successorSlots, entry);
428
403
  }
429
- if (insertSuccessor(table.slots, successor)) table.count++;
404
+ if (insertSuccessor(successorSlots, successor)) successorCount++;
430
405
  };
431
406
  successors.forEach(addSuccessor);
432
407
  addSuccessor({ index, state });
433
- if (table.slots.length > SUCCESSOR_CAPACITY * capacity.multiplier) capacity.multiplier++;
434
- return table.slots.filter((entry): entry is LiveSuccessor => entry !== undefined);
408
+ if (successorSlots.length > SUCCESSOR_CAPACITY * capacityMultiplier) capacityMultiplier++;
409
+ return successorSlots.filter((entry): entry is LiveSuccessor => entry !== undefined);
435
410
  };
436
411
 
437
412
  const expandLate = (
@@ -471,8 +446,7 @@ export const createLivenessChecker = (
471
446
  const isDone = behavior.isDone[successor.index] === true;
472
447
  for (const next of tableauNode.successors) {
473
448
  if (!isConsistent(tableau, next, index, successor.state)) continue;
474
- // A successor the search never stored follows a violation or an error, after which nothing
475
- // checks the graph. TLC records its node, which it never expands.
449
+ // Deviation from TLC: TLC records the node of a successor the search never stored, which it never expands.
476
450
  if (successor.index === -1) continue;
477
451
  const existing = getNodeId(behavior, successor.index, next);
478
452
  if (existing !== undefined && successors.includes(existing)) continue;
@@ -495,7 +469,7 @@ export const createLivenessChecker = (
495
469
  stateResults: Uint8Array,
496
470
  index: number,
497
471
  ) => {
498
- const consistency = tableau.nodes.map((_, node) =>
472
+ const consistency = tableau.nodes.map((_tableauNode, node) =>
499
473
  set.map((successor) => isConsistent(tableau, node, index, successor.state)),
500
474
  );
501
475
  behavior.isDone[index] = true;
@@ -571,7 +545,6 @@ export const createLivenessChecker = (
571
545
  throw new TlaEvaluationError(`Couldn't re-create liveness trace (path) starting at: ${target}`);
572
546
  };
573
547
 
574
- // TLC's LiveWorker#checkSccs, a Tarjan search that pushes every successor of a node at once.
575
548
  const findLassoInModel = (
576
549
  behavior: BehaviorGraph,
577
550
  model: Solution["models"][number],
@@ -628,8 +601,6 @@ export const createLivenessChecker = (
628
601
  return getLasso(root, members, membersByState);
629
602
  };
630
603
 
631
- // TLC's LiveWorker#printTrace: a prefix to the root, a greedy walk through the component until it
632
- // satisfies every check, and a breadth-first path back to the root.
633
604
  const getLasso = (
634
605
  root: number,
635
606
  members: Set<number>,
@@ -645,74 +616,75 @@ export const createLivenessChecker = (
645
616
  const stateDone = model.infinitelyOftenStates.map(() => false);
646
617
  const actionDone = model.infinitelyOftenActions.map(() => false);
647
618
  const promiseDone = solution.promises.map(() => false);
648
- const remaining = { count: stateDone.length + actionDone.length + promiseDone.length };
649
- const current = { node: root };
619
+ let remainingCount = stateDone.length + actionDone.length + promiseDone.length;
620
+ let currentNode = root;
650
621
  const isMember = (node: number) => members.has(node);
651
622
  const moveTo = (next: number) => {
652
- cycleStack.push(current.node);
653
- current.node = next;
623
+ cycleStack.push(currentNode);
624
+ currentNode = next;
654
625
  };
655
- while (remaining.count > 0) {
656
- const countAtStart = remaining.count;
626
+ while (remainingCount > 0) {
627
+ const countAtStart = remainingCount;
657
628
  while (true) {
658
629
  model.infinitelyOftenStates.forEach((position, slot) => {
659
- if (!stateDone[slot] && getStateResult(behavior, current.node, position)) {
630
+ if (!stateDone[slot] && getStateResult(behavior, currentNode, position)) {
660
631
  stateDone[slot] = true;
661
- remaining.count--;
632
+ remainingCount--;
662
633
  }
663
634
  });
664
635
  solution.promises.forEach((promise, position) => {
665
- if (!promiseDone[position] && isFulfilled(current.node, promise)) {
636
+ if (!promiseDone[position] && isFulfilled(currentNode, promise)) {
666
637
  promiseDone[position] = true;
667
- remaining.count--;
638
+ remainingCount--;
668
639
  }
669
640
  });
670
- if (remaining.count <= 0) break;
671
- const step = { last: -1, unvisited: -1 };
672
- const countBeforeActions = remaining.count;
673
- const taken = getNodeSuccessors(behavior, current.node).some((next, edge) => {
641
+ if (remainingCount <= 0) break;
642
+ let lastNext = -1;
643
+ let unvisitedNext = -1;
644
+ const countBeforeActions = remainingCount;
645
+ const isTaken = getNodeSuccessors(behavior, currentNode).some((next, edge) => {
674
646
  if (membersByState.has(behavior.nodeStates[next])) {
675
- if (!isMember(next) || !isAllowed(current.node, edge)) return false;
676
- step.last = next;
647
+ if (!isMember(next) || !isAllowed(currentNode, edge)) return false;
648
+ lastNext = next;
677
649
  model.infinitelyOftenActions.forEach((position, slot) => {
678
- if (!actionDone[slot] && getAction(current.node, edge, position)) {
650
+ if (!actionDone[slot] && getAction(currentNode, edge, position)) {
679
651
  actionDone[slot] = true;
680
- remaining.count--;
652
+ remainingCount--;
681
653
  }
682
654
  });
683
655
  }
684
- if (remaining.count < countBeforeActions) {
656
+ if (remainingCount < countBeforeActions) {
685
657
  moveTo(next);
686
658
  seen.clear();
687
659
  return true;
688
660
  }
689
- if (isMember(next) && !seen.has(next)) step.unvisited = next;
661
+ if (isMember(next) && !seen.has(next)) unvisitedNext = next;
690
662
  return false;
691
663
  });
692
- if (taken) break;
693
- if (remaining.count < countAtStart) {
694
- moveTo(step.last);
664
+ if (isTaken) break;
665
+ if (remainingCount < countAtStart) {
666
+ moveTo(lastNext);
695
667
  seen.clear();
696
668
  break;
697
669
  }
698
- while (step.unvisited === -1) {
670
+ while (unvisitedNext === -1) {
699
671
  const previous = cycleStack.pop();
700
672
  if (previous === undefined) {
701
673
  throw new TlaEvaluationError(
702
674
  "TLC failed to build the counterexample of a liveness property",
703
675
  );
704
676
  }
705
- current.node = previous;
706
- step.unvisited =
677
+ currentNode = previous;
678
+ unvisitedNext =
707
679
  getNodeSuccessors(behavior, previous).find(
708
680
  (next) => isMember(next) && !seen.has(next),
709
681
  ) ?? -1;
710
682
  }
711
- moveTo(step.unvisited);
712
- seen.add(step.unvisited);
683
+ moveTo(unvisitedNext);
684
+ seen.add(unvisitedNext);
713
685
  }
714
686
  }
715
- const postfix = getClosingPath(root, current.node, membersByState, isAllowed);
687
+ const postfix = getClosingPath(root, currentNode, membersByState, isAllowed);
716
688
  for (let position = cycleStack.length - 1; position >= 0; position--) {
717
689
  const state = behavior.nodeStates[cycleStack[position]];
718
690
  if (postfix[postfix.length - 1] !== state) postfix.push(state);
@@ -771,7 +743,7 @@ export const createLivenessChecker = (
771
743
 
772
744
  for (let position = 0; position < queue.length; position++) {
773
745
  const stack = [queue[position], UNVISITED, 0];
774
- const counter = { link: 0 };
746
+ let nextLink = 0;
775
747
  while (stack.length >= 3) {
776
748
  const lowLink = stack.pop() ?? 0;
777
749
  const location = stack.pop();
@@ -788,10 +760,10 @@ export const createLivenessChecker = (
788
760
  stack.push(Math.min(lowLink, links[node]));
789
761
  continue;
790
762
  }
791
- links[node] = counter.link;
763
+ links[node] = nextLink;
792
764
  stack.push(lowLink, node, SCC_MARKER);
793
765
  components.push(node);
794
- const nextLowLink = { value: counter.link++ };
766
+ let nextLowLink = nextLink++;
795
767
  getNodeSuccessors(behavior, node).forEach((next, edge) => {
796
768
  if (!isWritten(behavior, next)) return;
797
769
  if (!isAllowed(node, edge)) {
@@ -799,9 +771,9 @@ export const createLivenessChecker = (
799
771
  return;
800
772
  }
801
773
  if (links[next] === UNVISITED) stack.push(next, UNVISITED);
802
- else nextLowLink.value = Math.min(nextLowLink.value, links[next]);
774
+ else nextLowLink = Math.min(nextLowLink, links[next]);
803
775
  });
804
- stack.push(nextLowLink.value);
776
+ stack.push(nextLowLink);
805
777
  }
806
778
  }
807
779
  return undefined;
@@ -810,9 +782,9 @@ export const createLivenessChecker = (
810
782
  return {
811
783
  branches: behaviors.length,
812
784
  addInitialState: (index, state, tracing) => {
813
- evaluation.environment = tracing ?? environment;
814
- shared.states.clear();
815
- shared.actions.clear();
785
+ evaluationEnvironment = tracing ?? environment;
786
+ sharedStates.clear();
787
+ sharedActions.clear();
816
788
  try {
817
789
  for (const behavior of behaviors) {
818
790
  const tableau = behavior.solution.tableau;
@@ -828,13 +800,13 @@ export const createLivenessChecker = (
828
800
  }
829
801
  }
830
802
  } finally {
831
- evaluation.environment = environment;
803
+ evaluationEnvironment = environment;
832
804
  }
833
805
  },
834
806
  addState: (index, state, successors, tracing) => {
835
- evaluation.environment = tracing ?? environment;
836
- shared.states.clear();
837
- shared.actions.clear();
807
+ evaluationEnvironment = tracing ?? environment;
808
+ sharedStates.clear();
809
+ sharedActions.clear();
838
810
  try {
839
811
  const set = getSuccessorSet(index, state, successors);
840
812
  for (const behavior of behaviors) {
@@ -859,7 +831,7 @@ export const createLivenessChecker = (
859
831
  }
860
832
  return undefined;
861
833
  } finally {
862
- evaluation.environment = environment;
834
+ evaluationEnvironment = environment;
863
835
  }
864
836
  },
865
837
  findViolation: () => {
@@ -871,12 +843,11 @@ export const createLivenessChecker = (
871
843
  }
872
844
  return undefined;
873
845
  },
874
- // TLC names every property that the counterexample violates, once each.
875
846
  getViolatedProperties: (path, cycleStart) => [
876
847
  ...new Set(
877
848
  properties
878
849
  .filter(
879
- (_, position) =>
850
+ (_property, position) =>
880
851
  !evaluateOnLasso(negations[position].body, path, cycleStart, 0, environment),
881
852
  )
882
853
  .map((property) => property.name),
package/src/liveness.ts CHANGED
@@ -47,7 +47,6 @@ export interface ScopedExpression {
47
47
  scope: Scope | undefined;
48
48
  }
49
49
 
50
- // The name TLC gives a part of a property: a conjunct's location or the enclosing definition.
51
50
  interface ExpandedExpression extends ScopedExpression {
52
51
  name: string | undefined;
53
52
  path: string[];
@@ -81,7 +80,6 @@ const getConstantContext = (environment: Environment, scope: Scope | undefined):
81
80
  next: undefined,
82
81
  });
83
82
 
84
- // TLC adds the message of the error that stopped it, when it has one.
85
83
  const createCannotHandleError = (expression: Expression, inner?: string) =>
86
84
  new TlaEvaluationError(
87
85
  `TLC cannot handle the temporal formula ${describeRange(expression)}${inner === undefined ? "" : `:\n${inner}`}`,
@@ -95,7 +93,6 @@ export const getLevel = (
95
93
  visiting = new Set<object>(),
96
94
  ): number => {
97
95
  const getBodyLevel = (binding: object, body: Expression, bodyScope: Scope | undefined) => {
98
- // A recursive call adds nothing, as SANY's level never decreases while it iterates.
99
96
  if (visiting.has(binding)) return CONSTANT_LEVEL;
100
97
  visiting.add(binding);
101
98
  const level = getLevel(body, bodyScope, environment, new Set(), visiting);
@@ -134,10 +131,10 @@ export const getLevel = (
134
131
  return getGlobalLevel(name) ?? getBindingLevel(name) ?? CONSTANT_LEVEL;
135
132
  };
136
133
  const getChildrenLevel = (node: Expression) => {
137
- const level = { value: CONSTANT_LEVEL };
134
+ let level = CONSTANT_LEVEL;
138
135
  mapChildren(node, (child, bound) => {
139
- level.value = Math.max(
140
- level.value,
136
+ level = Math.max(
137
+ level,
141
138
  getLevel(
142
139
  child,
143
140
  scope,
@@ -148,7 +145,7 @@ export const getLevel = (
148
145
  );
149
146
  return child;
150
147
  });
151
- return level.value;
148
+ return level;
152
149
  };
153
150
  switch (expression.kind) {
154
151
  case "Prime":
@@ -184,8 +181,6 @@ export const getLevel = (
184
181
  ) {
185
182
  return level;
186
183
  }
187
- // SANY counts an argument only where the definition's level depends on its parameter, so
188
- // ENABLED of an operator argument stays a state predicate.
189
184
  return getBodyLevel(
190
185
  operator,
191
186
  operator.body,
@@ -197,7 +192,6 @@ export const getLevel = (
197
192
  }
198
193
  };
199
194
 
200
- // TLC leaves a formula whose sets it fails to enumerate, and doesn't expect an EvalException.
201
195
  const getUnrolledElements = (bounds: Bound[], context: Context) => {
202
196
  try {
203
197
  return getBoundElements(bounds, context);
@@ -215,10 +209,8 @@ export const expandTemporal = (
215
209
  expression: Expression,
216
210
  scope: Scope | undefined,
217
211
  environment: Environment,
218
- // Whether TLC expands the formula as a property, which unrolls each \A over a constant set.
219
212
  isProperty: boolean,
220
213
  expanding = new Set<Definition>(),
221
- // Whether the expansion entered an instance's definition, after which TLC expands no call.
222
214
  isSubstituted = false,
223
215
  name?: string,
224
216
  path: string[] = [],
@@ -262,7 +254,6 @@ export const expandTemporal = (
262
254
  const definition = environment.constants.has(expression.name)
263
255
  ? undefined
264
256
  : getDefinition(expression.name, environment);
265
- // TLC expands a definition it didn't compute at startup, even a constant one.
266
257
  if (
267
258
  !binding &&
268
259
  definition?.kind === "Operator" &&
@@ -273,7 +264,6 @@ export const expandTemporal = (
273
264
  if (expanding.has(definition)) {
274
265
  throw createStackOverflowError();
275
266
  }
276
- // TLC names a part after the operator as the spec writes it, as I!Op outside the instance.
277
267
  return expand(
278
268
  definition.body,
279
269
  undefined,
@@ -284,7 +274,6 @@ export const expandTemporal = (
284
274
  }
285
275
  }
286
276
  const context = getConstantContext(environment, scope);
287
- // TLC 1.7.4 expands no call, a property every one, and a specification only temporal ones.
288
277
  if (
289
278
  expression.kind === "Apply" &&
290
279
  environment.runtime.version !== "1.7.4" &&
@@ -294,8 +283,6 @@ export const expandTemporal = (
294
283
  const definition = isLocallyResolved(expression)
295
284
  ? undefined
296
285
  : getDefinition(expression.operator, environment);
297
- // An instance's definition that takes only the instance's parameters takes no arguments in
298
- // TLC, which expands it even within the instance.
299
286
  const isOwnArgumentFree =
300
287
  operator?.kind === "Closure" &&
301
288
  operator.params.every((param) => environment.instanceParams.has(param));
@@ -354,7 +341,6 @@ export const toLiveExpression = (
354
341
  scope: Scope | undefined,
355
342
  level: number,
356
343
  ): LiveExpression => {
357
- // TLC after 1.7.4 bounds the level of a formula whose parts are constant by the formula's own.
358
344
  const atomLevel =
359
345
  level === CONSTANT_LEVEL && !is174 ? getLevel(expression, scope, environment) : level;
360
346
  if (atomLevel === CONSTANT_LEVEL) {
@@ -367,7 +353,6 @@ export const toLiveExpression = (
367
353
  return createBoolean(value);
368
354
  }
369
355
  if (atomLevel === VARIABLE_LEVEL) return { kind: "State", expression, scope };
370
- // TLC checks <<A>>_v's subscript before it evaluates A.
371
356
  if (expression.kind === "AngleAction") {
372
357
  return {
373
358
  kind: "Action",
@@ -379,7 +364,7 @@ export const toLiveExpression = (
379
364
  return { kind: "Action", expression, subscript: undefined, scope };
380
365
  };
381
366
 
382
- const expansion = { depth: 0 };
367
+ let expansionDepth = 0;
383
368
 
384
369
  const expand = (
385
370
  body: Expression,
@@ -387,15 +372,14 @@ export const toLiveExpression = (
387
372
  expression: Expression,
388
373
  scope: Scope | undefined,
389
374
  ) => {
390
- if (expansion.depth >= MAXIMUM_EXPANSION_DEPTH) {
375
+ if (expansionDepth >= MAXIMUM_EXPANSION_DEPTH) {
391
376
  throw createStackOverflowError();
392
377
  }
393
- expansion.depth++;
378
+ expansionDepth++;
394
379
  const result = convert(body, bodyScope);
395
- expansion.depth--;
380
+ expansionDepth--;
396
381
  const level = getLiveLevel(result);
397
- // For a constant body, Liveness#astToLive bounds the level of a temporal call by its own and
398
- // takes the call for an action, which classifyExpr rejects. tla-ts keeps the constant.
382
+ // Deviation from TLC: Liveness#astToLive takes a temporal call with a constant body for an action, which classifyExpr rejects.
399
383
  const isConstantTemporal =
400
384
  level === CONSTANT_LEVEL && getLevel(expression, scope, environment) > ACTION_LEVEL;
401
385
  return level > ACTION_LEVEL || isConstantTemporal ? result : toAtom(expression, scope, level);
@@ -425,8 +409,6 @@ export const toLiveExpression = (
425
409
  ? resolveOperator(name, context)
426
410
  : resolveGlobalOperator(name, environment);
427
411
  const args = expression.kind === "Apply" ? expression.args : [];
428
- // An operator constant can be substituted by a conjunction or disjunction, whose arguments
429
- // TLC translates as it translates its operands.
430
412
  const junction =
431
413
  operator?.kind === "Builtin" && args.length === 2 ? JUNCTIONS.get(operator.name) : undefined;
432
414
  if (junction) {
@@ -456,8 +438,6 @@ export const toLiveExpression = (
456
438
  );
457
439
  };
458
440
 
459
- // TLC can't handle a call whose expansion fails, except that after 1.7.4 it fails as a
460
- // recursive operator's expansion does.
461
441
  const expandCall = (
462
442
  expression: Expression,
463
443
  expandBody: () => LiveExpression,
@@ -479,7 +459,6 @@ export const toLiveExpression = (
479
459
  const level = getLevel(expression, scope, environment);
480
460
  const operands: LiveExpression[] = [];
481
461
  const isForall = expression.kind === "Forall";
482
- // TLC can't handle a quantifier whose body fails, and under \A adds another failure's message.
483
462
  try {
484
463
  forEachBinding(expression.bounds, getConstantContext(environment, scope), (boundScope) => {
485
464
  operands.push(convert(expression.body, boundScope));
@@ -499,13 +478,11 @@ export const toLiveExpression = (
499
478
  return toWhole(expression, scope, createJunction(isForall ? "And" : "Or", operands));
500
479
  };
501
480
 
502
- // TLC takes a formula whose translated parts are below temporal level whole, at their level.
503
481
  const toWhole = (expression: Expression, scope: Scope | undefined, parts: LiveExpression) => {
504
482
  const level = getLiveLevel(parts);
505
483
  return level > ACTION_LEVEL ? parts : toAtom(expression, scope, level);
506
484
  };
507
485
 
508
- // A constant translated part, such as a quantifier over {}, can make the whole formula constant.
509
486
  const isTranslatedWhole = (expression: Expression) =>
510
487
  expression.kind === "Label" ||
511
488
  expression.kind === "Let" ||
@@ -568,7 +545,6 @@ export const toLiveExpression = (
568
545
  case "Exists":
569
546
  return convertQuantifier(expression, scope);
570
547
  case "If": {
571
- // TLC 1.7.4 translates both branches whatever the guard.
572
548
  if (!is174 && getLevel(expression.condition, scope, environment) === CONSTANT_LEVEL) {
573
549
  const condition = evaluate(expression.condition, getConstantContext(environment, scope));
574
550
  if (typeof condition === "boolean") {
@@ -698,7 +674,7 @@ export const evaluateOnLasso = (
698
674
  ): boolean => {
699
675
  const getNext = (index: number) => (index + 1 < path.length ? index + 1 : cycleStart);
700
676
  const getFuture = (index: number) =>
701
- path.map((_, offset) => offset).filter((offset) => offset >= Math.min(index, cycleStart));
677
+ path.map((_state, offset) => offset).filter((offset) => offset >= Math.min(index, cycleStart));
702
678
  const recurse = (operand: LiveExpression, index = position) =>
703
679
  evaluateOnLasso(operand, path, cycleStart, index, environment);
704
680
  switch (expression.kind) {