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
package/src/values.ts CHANGED
@@ -6,6 +6,7 @@ import {
6
6
  TlaModuleError,
7
7
  TlaSetBoundError,
8
8
  } from "./errors";
9
+ import { getJavaHashCode } from "./java-hash";
9
10
  import { prettyPrint } from "./pretty-print";
10
11
  import { isEvalException } from "./tlc/messages";
11
12
  import type { TlcVersion } from "./tlc/version";
@@ -28,16 +29,13 @@ interface LazySet {
28
29
  export interface SetValue {
29
30
  kind: "Set";
30
31
  elements: Value[];
31
- // A set enumeration's unsorted elements, duplicates included, until TLC normalizes it.
32
32
  writtenElements: Value[] | undefined;
33
- // Once normalized, TLC checks membership by binary search with compareTo, not with equals.
34
33
  isNormalized: boolean;
35
34
  hash: number;
36
35
  fingerprint: number;
37
36
  isFingerprinted: boolean;
38
37
  }
39
38
 
40
- // TLC sorts a set enumeration only when it has to, so it checks membership element by element.
41
39
  export interface MixedSetValue extends LazySet {
42
40
  kind: "MixedSet";
43
41
  elements: Value[];
@@ -51,7 +49,6 @@ export interface FunctionValue {
51
49
  fingerprint: number;
52
50
  writtenKeys: Value[] | undefined;
53
51
  functionClass: FunctionClass | undefined;
54
- // The set a lambda ranges over, which TLC's DOMAIN returns until it builds the function.
55
52
  lambdaDomain: Value | undefined;
56
53
  isFingerprinted: boolean;
57
54
  }
@@ -101,7 +98,6 @@ export interface SetOperationValue extends LazySet {
101
98
  right: Value;
102
99
  }
103
100
 
104
- // TLC prints a set filter as its bound names and the location of its predicate.
105
101
  interface FilteredSetSource {
106
102
  names: string;
107
103
  predicate: string;
@@ -114,7 +110,6 @@ export interface FilteredSetValue extends LazySet {
114
110
  source?: FilteredSetSource;
115
111
  }
116
112
 
117
- // UNION S when some element of S isn't a set enumeration. TLC enumerates it only when it has to.
118
113
  export interface UnionValue extends LazySet {
119
114
  kind: "Union";
120
115
  base: Value;
@@ -130,12 +125,10 @@ export interface LazyFunctionSource {
130
125
  body: string;
131
126
  }
132
127
 
133
- // enumerated is the FcnRcdValue TLC converts the function to once it compares or fingerprints it.
134
128
  export interface LazyFunctionValue {
135
129
  kind: "LazyFunction";
136
130
  domain: Value;
137
131
  apply: (argument: Value) => Value;
138
- // The length of each bound's tuple pattern, or undefined for a name.
139
132
  boundTupleLengths?: (number | undefined)[];
140
133
  source?: LazyFunctionSource;
141
134
  functionClass?: FunctionClass;
@@ -189,16 +182,14 @@ export const MAXIMUM_INTEGER = 2147483647;
189
182
  const DEFAULT_SET_BOUND = 1000000;
190
183
  const VALUE_VEC_CAPACITY = 10;
191
184
 
192
- const setBound = { size: DEFAULT_SET_BOUND };
185
+ let setBound = DEFAULT_SET_BOUND;
193
186
 
194
- export const getSetBound = () => setBound.size;
187
+ export const getSetBound = () => setBound;
195
188
 
196
- // A ValueVec that TLC grows one element at a time starts with room for 10, and fails to grow once
197
- // its room reaches the bound.
198
- export const getMaximumSetSize = () => Math.max(setBound.size, VALUE_VEC_CAPACITY);
189
+ export const getMaximumSetSize = () => Math.max(setBound, VALUE_VEC_CAPACITY);
199
190
 
200
191
  export const getSetBoundMessage = () =>
201
- `Attempted to construct a set with too many elements (>${setBound.size}).`;
192
+ `Attempted to construct a set with too many elements (>${setBound}).`;
202
193
 
203
194
  export const addBoundedElement = (elements: Value[], element: Value) => {
204
195
  if (elements.length === getMaximumSetSize()) throw new TlaSetBoundError(getSetBoundMessage());
@@ -210,7 +201,6 @@ export const isBeyondSetBound = (value: Value): value is IntervalValue =>
210
201
  value.kind === "Interval" &&
211
202
  value.high - value.low >= getMaximumSetSize();
212
203
 
213
- // TLC steps through an interval without building its elements.
214
204
  export const forEachElement = (
215
205
  elements: IntervalValue | Value[],
216
206
  visit: (element: Value) => void,
@@ -252,13 +242,10 @@ export const describeKind = (value: Value) => {
252
242
  export const isTlcInteger = (value: number) =>
253
243
  Number.isInteger(value) && value <= MAXIMUM_INTEGER && value >= MINIMUM_INTEGER;
254
244
 
255
- // TLC keeps a record, or a function @@ builds, in written order until it sorts it to compare or
256
- // fingerprint it. It searches the function in that order, and ToString, Assert, and error
257
- // messages show it, unless every key is an integer.
258
- const writtenOrderUse = { isUsed: false };
245
+ let isWrittenOrderUsed = false;
259
246
 
260
247
  const setWrittenOrder = (functionValue: FunctionValue, keys: Value[]) => {
261
- writtenOrderUse.isUsed = true;
248
+ isWrittenOrderUsed = true;
262
249
  functionValue.writtenKeys = keys;
263
250
  return functionValue;
264
251
  };
@@ -274,7 +261,6 @@ export const getWrittenPairs = (functionValue: FunctionValue): [Value, Value][]
274
261
  functionValue.range[functionValue.domain.indexOf(key)],
275
262
  ]);
276
263
 
277
- // TLC sorts a function whose keys are all integers to check whether it is a tuple.
278
264
  export const getPrintedPairs = (functionValue: FunctionValue): [Value, Value][] =>
279
265
  functionValue.domain.every((key) => typeof key === "number")
280
266
  ? functionValue.domain.map((key, index) => [key, functionValue.range[index]])
@@ -283,7 +269,6 @@ export const getPrintedPairs = (functionValue: FunctionValue): [Value, Value][]
283
269
  export const createWrittenRecord = (names: Value[], range: Value[], writtenNames: Value[]) =>
284
270
  setWrittenOrder(createSortedRecord(names, range), writtenNames);
285
271
 
286
- // TLC sorts a value it compares, even with itself, so a hashed or fingerprinted value is sorted.
287
272
  const forgetWrittenOrder = (value: Value): void => {
288
273
  if (typeof value !== "object" || (value.kind !== "Function" && value.kind !== "Set")) return;
289
274
  if (value.hash !== 0 || value.fingerprint !== 0) return;
@@ -294,12 +279,11 @@ const forgetWrittenOrder = (value: Value): void => {
294
279
 
295
280
  const isRecordDomain = (domain: Value[]) => domain.every((key) => typeof key === "string");
296
281
 
297
- // TLC compares a record only with a record, so this can fail before the sizes decide.
298
282
  const isRecordMismatch = (left: Value[], right: Value[]) =>
299
283
  left.length > 0 && right.length > 0 && isRecordDomain(left) !== isRecordDomain(right);
300
284
 
301
285
  const compareFunctions = (left: FunctionValue, right: FunctionValue) => {
302
- if (writtenOrderUse.isUsed) {
286
+ if (isWrittenOrderUsed) {
303
287
  left.writtenKeys = undefined;
304
288
  right.writtenKeys = undefined;
305
289
  }
@@ -371,13 +355,10 @@ const checkModelValueTypes = (left: Value, right: Value, isEquality: boolean) =>
371
355
  interface StringOrder {
372
356
  ranks: Record<string, number>;
373
357
  size: number;
374
- // A parallel search's workers rank the strings they create in different orders, so the search
375
- // notes the first string it orders against another string ranked from this rank on.
376
358
  searchRank: number | undefined;
377
359
  unorderedString: string | undefined;
378
360
  }
379
361
 
380
- // TLC orders strings and model values by first interning, so each check sets its order first.
381
362
  const stringOrder: StringOrder = {
382
363
  ranks: Object.create(null),
383
364
  size: 0,
@@ -400,8 +381,7 @@ export const freezeStringOrder = () => {
400
381
 
401
382
  export const getUnorderedString = () => stringOrder.unorderedString;
402
383
 
403
- // TLC 1.7.4 prints and looks keys up in functions differently, so each check sets the version.
404
- const valueVersion = { is174: false };
384
+ let isVersion174 = false;
405
385
 
406
386
  const printing = { isExpanded: true, isViewPrinted: false };
407
387
 
@@ -413,13 +393,12 @@ export interface ValueOptions {
413
393
  }
414
394
 
415
395
  export const setValueOptions = (options: ValueOptions) => {
416
- valueVersion.is174 = options.version === "1.7.4";
417
- setBound.size = options.maxSetSize ?? DEFAULT_SET_BOUND;
396
+ isVersion174 = options.version === "1.7.4";
397
+ setBound = options.maxSetSize ?? DEFAULT_SET_BOUND;
418
398
  printing.isExpanded = options.isLazyValueExpanded ?? true;
419
399
  printing.isViewPrinted = options.isViewPrinted ?? false;
420
400
  };
421
401
 
422
- // TLC evaluates the VIEW of a state it prints as it prints it.
423
402
  const viewTexts = new WeakMap<Record<string, Value>, () => string>();
424
403
 
425
404
  export const setViewText = (state: Record<string, Value>, getText: () => string) => {
@@ -427,7 +406,6 @@ export const setViewText = (state: Record<string, Value>, getText: () => string)
427
406
  return state;
428
407
  };
429
408
 
430
- // TLC prints by its _format only the state an ALIAS gives.
431
409
  const aliasNames = new WeakMap<Record<string, Value>, string>();
432
410
 
433
411
  export const setAliasName = (state: Record<string, Value>, alias: string) => {
@@ -480,8 +458,6 @@ const createComparisonError = (left: Value, right: Value) =>
480
458
  : `Attempted to compare ${describeKind(left)} ${formatValue(left)} with ${describeKind(right)} ${formatValue(right)}`,
481
459
  );
482
460
 
483
- // TLC's Nat, Int, STRING, Seq(S), and Any compare only with each other and with model values,
484
- // and fail with any other set, worded by the class of the left operand.
485
461
  const compareOverriddenSets = (
486
462
  left: Value,
487
463
  right: Value,
@@ -510,9 +486,8 @@ const compareAny = (other: Value): never => {
510
486
  throw new TlaModuleError(`Attempted to compare ANY with the value\n${formatValue(other)}`);
511
487
  };
512
488
 
513
- // TLC enumerates a lambda to compare it, even with itself.
514
489
  const compareWithItself = (value: Value) => {
515
- if (writtenOrderUse.isUsed) forgetWrittenOrder(value);
490
+ if (isWrittenOrderUsed) forgetWrittenOrder(value);
516
491
  if (value === ANY_SET) compareAny(value);
517
492
  if (typeof value === "object" && value.kind === "LazyFunction") toFiniteFunction(value);
518
493
  };
@@ -550,7 +525,6 @@ export const compareValues = (left: Value, right: Value): number => {
550
525
  if (isLeftSet !== isSetValue(right)) {
551
526
  throw createComparisonError(left, right);
552
527
  }
553
- // The sizes decide first, so an interval of another size never needs its elements.
554
528
  if (leftKind === "Interval" && rightKind === "Set") {
555
529
  const difference = getCardinality(left) - right.elements.length;
556
530
  if (difference !== 0) return difference;
@@ -573,10 +547,8 @@ export const compareValues = (left: Value, right: Value): number => {
573
547
 
574
548
  const modelValues = new Map<string, ModelValue>();
575
549
 
576
- // ModelValue's index is a short.
577
550
  const MAXIMUM_MODEL_VALUES = 32768;
578
551
 
579
- // setStringOrder clears the model values, so each check ranks and creates its own.
580
552
  export const createModelValue = (name: string): ModelValue => {
581
553
  const known = modelValues.get(name);
582
554
  if (known) return known;
@@ -627,18 +599,15 @@ export const createSortedFunction = (domain: Value[], range: Value[]): FunctionV
627
599
  isFingerprinted: false,
628
600
  });
629
601
 
630
- // TLC words a failed application by the function's Java class: FcnLambdaValue, TupleValue,
631
- // RecordValue, or FcnRcdValue.
632
602
  export type FunctionClass = "lambda" | "tuple" | "record";
633
603
 
634
- // Whether a lambda exists for markFingerprinted to find, so that a spec without one skips the walk.
635
- const lambdaPresence = { isPresent: false };
604
+ let hasLambda = false;
636
605
 
637
606
  export const setFunctionClass = <FunctionLike extends FunctionValue | LazyFunctionValue>(
638
607
  functionValue: FunctionLike,
639
608
  functionClass: FunctionClass | undefined,
640
609
  ) => {
641
- if (functionClass === "lambda") lambdaPresence.isPresent = true;
610
+ if (functionClass === "lambda") hasLambda = true;
642
611
  if (functionClass !== undefined) functionValue.functionClass = functionClass;
643
612
  return functionValue;
644
613
  };
@@ -652,17 +621,14 @@ export const markLambdaFunction = <FunctionLike extends FunctionValue | LazyFunc
652
621
  functionValue: FunctionLike,
653
622
  ) => setFunctionClass(functionValue, "lambda");
654
623
 
655
- // TLC caches a lazy set as the receiver of compareTo, equals, size, and fingerPrint, not as
656
- // their argument, and enumerates the sets an uncached one combines, duplicates and all.
657
624
  const isCachedSet = (value: ObjectValue) => isLazySet(value) && value.isCached === true;
658
625
 
659
- // Lets a spec without a UNION or \cup set, which stream duplicates, skip tracking TLC's caches.
660
- const streamedSetPresence = { isPresent: false };
626
+ let hasStreamedSet = false;
661
627
 
662
628
  export const markStreamedSet = <Streamed extends UnionValue | SetOperationValue>(
663
629
  value: Streamed,
664
630
  ) => {
665
- streamedSetPresence.isPresent = true;
631
+ hasStreamedSet = true;
666
632
  return value;
667
633
  };
668
634
 
@@ -677,7 +643,7 @@ const STREAMING_KINDS = new Set<ObjectValue["kind"]>([
677
643
 
678
644
  const cacheSet = (value: Value) => {
679
645
  if (
680
- streamedSetPresence.isPresent &&
646
+ hasStreamedSet &&
681
647
  typeof value === "object" &&
682
648
  isLazySet(value) &&
683
649
  STREAMING_KINDS.has(value.kind)
@@ -686,11 +652,8 @@ const cacheSet = (value: Value) => {
686
652
  }
687
653
  };
688
654
 
689
- // Fingerprinting caches the FcnRcdValue a lambda enumerates to, so it is no longer a lambda.
690
655
  export const markFingerprinted = (value: Value): void => {
691
- if ((!lambdaPresence.isPresent && !streamedSetPresence.isPresent) || typeof value !== "object") {
692
- return;
693
- }
656
+ if ((!hasLambda && !hasStreamedSet) || typeof value !== "object") return;
694
657
  if (value.kind === "ModelValue" || value.kind === "Interval" || value.kind === "InfiniteSet") {
695
658
  return;
696
659
  }
@@ -724,7 +687,6 @@ export const valuesEqual = (left: Value, right: Value): boolean => {
724
687
  if (left.kind === "Function" && right.kind === "Function")
725
688
  return compareFunctions(left, right) === 0;
726
689
  if (left.kind === "Set" && right.kind === "Set") return compareSets(left, right) === 0;
727
- // The sizes decide first, so an interval of another size never needs its elements.
728
690
  if (left.kind === "Interval" && right.kind === "Set") {
729
691
  if (getCardinality(left) !== right.elements.length) return false;
730
692
  }
@@ -732,7 +694,6 @@ export const valuesEqual = (left: Value, right: Value): boolean => {
732
694
  if (left.elements.length !== getCardinality(right)) return false;
733
695
  }
734
696
  if (isModelValue(left) || isModelValue(right)) {
735
- // TLC enumerates a lambda, which can fail, before it compares it.
736
697
  if (left.kind === "LazyFunction") toFiniteFunction(left);
737
698
  checkModelValueTypes(left, right, true);
738
699
  return isModelValue(left) && isModelValue(right) && left.name === right.name;
@@ -786,7 +747,6 @@ export const valuesEqual = (left: Value, right: Value): boolean => {
786
747
  return compareValues(left, right) === 0;
787
748
  };
788
749
 
789
- // TLC words errors by each function's Java class, so a stand-in must match each function's class.
790
750
  const createSameValueCheck = (isClassSensitive: boolean) => {
791
751
  const areSameValues = (left: Value[], right: Value[]) => {
792
752
  if (left === right) return true;
@@ -826,7 +786,6 @@ const canonicalizeElements = (elements: Value[], canonicalize: (value: Value) =>
826
786
  }
827
787
  };
828
788
 
829
- // So that the states it keeps share their values.
830
789
  export const createCanonicalizer = () => {
831
790
  const canonicalValues = new Map<number, Value | Value[]>();
832
791
  const canonicalize = (value: Value): Value => {
@@ -860,15 +819,12 @@ export const createCanonicalizer = () => {
860
819
  };
861
820
  };
862
821
 
863
- // These follow TLC's compareTo and equals, which word a failure by the left operand's class,
864
- // so a failed comparison above runs again through them to fail as TLC does.
865
822
  interface TlcFunction {
866
823
  domain: Value[];
867
824
  range: Value[];
868
825
  low: number | undefined;
869
826
  }
870
827
 
871
- // TLC compares a domain it keeps as an interval apart from one it keeps as an array.
872
828
  const getIntervalLow = (value: Value, domain: Value[]) =>
873
829
  domain.length === 0 || getFunctionClass(value) === "tuple" ? 1 : getDomainInterval(domain)?.low;
874
830
 
@@ -885,10 +841,9 @@ const toTlcFunction = (value: Value) => {
885
841
  const toTlcRecord = (value: Value) => {
886
842
  if (!isFunctionValue(value)) return undefined;
887
843
  const functionValue = toFiniteFunction(value);
888
- return functionValue.domain.every((key) => typeof key === "string") ? functionValue : undefined;
844
+ return isRecordDomain(functionValue.domain) ? functionValue : undefined;
889
845
  };
890
846
 
891
- // A model value compared with a value of another kind. The other value is never a model value.
892
847
  const compareModelValueWith = (modelValue: ModelValue, other: Value) => {
893
848
  checkModelValueTypes(other, modelValue, false);
894
849
  return 1;
@@ -1000,7 +955,6 @@ const compareFunctionLikeTlc = (left: FunctionValue | LazyFunctionValue, right:
1000
955
  );
1001
956
  };
1002
957
 
1003
- // TLC compares a lazy or Java-overridden set through its own class before it compares elements.
1004
958
  const isComparedByClass = (left: ObjectValue, right: Value) =>
1005
959
  isOverriddenSet(left) ||
1006
960
  isOverriddenSet(right) ||
@@ -1140,7 +1094,6 @@ const equalsLikeTlc = (left: Value, right: Value): boolean => {
1140
1094
  return equalsSetLikeTlc(left, right);
1141
1095
  };
1142
1096
 
1143
- // TLC's ValueVec.sort, which inserts each element into the sorted prefix by binary search.
1144
1097
  const sortLikeTlc = (elements: Value[]) => {
1145
1098
  const sorted: Value[] = [];
1146
1099
  for (const element of elements) {
@@ -1159,17 +1112,17 @@ const callTracedMethod = <Result>(receiver: Value, method: () => Result) => {
1159
1112
  return result;
1160
1113
  };
1161
1114
 
1162
- // A binary search with compareTo, which ends where the element is or would go.
1163
1115
  const searchLikeTlc = (element: Value, sorted: Value[]) => {
1164
- const bounds = { low: 0, high: sorted.length };
1165
- while (bounds.low < bounds.high) {
1166
- const middle = bounds.low + ((bounds.high - bounds.low) >> 1);
1116
+ let low = 0;
1117
+ let high = sorted.length;
1118
+ while (low < high) {
1119
+ const middle = low + ((high - low) >> 1);
1167
1120
  const comparison = callTracedMethod(element, () => compareLikeTlc(element, sorted[middle]));
1168
1121
  if (comparison === 0) return { index: middle, isFound: true };
1169
- if (comparison < 0) bounds.high = middle;
1170
- else bounds.low = middle + 1;
1122
+ if (comparison < 0) high = middle;
1123
+ else low = middle + 1;
1171
1124
  }
1172
- return { index: bounds.low, isFound: false };
1125
+ return { index: low, isFound: false };
1173
1126
  };
1174
1127
 
1175
1128
  const isElementOfLikeTlc = (element: Value, elements: Value[]) =>
@@ -1192,7 +1145,6 @@ export const markNormalized = (value: Value): void => {
1192
1145
  }
1193
1146
  };
1194
1147
 
1195
- // TLC's Value.normalize sorts a set enumeration, and the sets a lazy set it hasn't cached combines.
1196
1148
  export const normalizeShallowly = (value: Value): void => {
1197
1149
  if (typeof value !== "object" || isCachedSet(value)) return;
1198
1150
  switch (value.kind) {
@@ -1241,7 +1193,6 @@ const isAscendingIntegers = (elements: Value[]) => {
1241
1193
  };
1242
1194
 
1243
1195
  export const createSet = (elements: Value[]): SetValue => {
1244
- // TLC's sort compares each element after the first with those before it.
1245
1196
  for (let index = 1; index < elements.length; index++) cacheSet(elements[index]);
1246
1197
  if (elements.length < 2 || isStrictlySorted(elements)) return createSortedSet(elements);
1247
1198
  const sorted = [...elements].sort(compareValues);
@@ -1252,7 +1203,6 @@ export const createSet = (elements: Value[]): SetValue => {
1252
1203
  return createSortedSet(unique);
1253
1204
  };
1254
1205
 
1255
- // TLC builds a set enumeration unsorted, so one whose elements it can't compare still evaluates.
1256
1206
  export const createEnumeratedSet = (elements: Value[]): SetValue | MixedSetValue => {
1257
1207
  if (isAscendingIntegers(elements)) return createSortedSet(elements);
1258
1208
  const set = computeLikeTlcOnFailure<Value[], undefined, SetValue | MixedSetValue>(
@@ -1394,20 +1344,15 @@ const longIntervalDomains = new Map<number, WeakRef<Value[]>>();
1394
1344
  const intervalValues: IntervalValue[] = [];
1395
1345
  const intervalArrays = new WeakMap<Value[], IntervalValue>();
1396
1346
 
1397
- // Every function over 1..n shares one domain array per length, so the array's identity records
1398
- // the interval. Callers must not mutate the returned array. Longer domains are held weakly: a
1399
- // sequence that grows one element at a time would otherwise keep an array of every length it
1400
- // passed through, n * n / 2 numbers in all.
1401
1347
  const getIntervalDomain = (length: number): Value[] => {
1402
1348
  const cached = intervalDomains[length] ?? longIntervalDomains.get(length)?.deref();
1403
1349
  if (cached) return cached;
1404
- const domain = Array.from({ length }, (_, index) => index + 1);
1350
+ const domain = Array.from({ length }, (_value, index) => index + 1);
1405
1351
  if (length > MAX_SHARED_INTERVAL_LENGTH) longIntervalDomains.set(length, new WeakRef(domain));
1406
1352
  else intervalDomains[length] = domain;
1407
1353
  return domain;
1408
1354
  };
1409
1355
 
1410
- // The caller only iterates the elements, so they need not record the interval for DOMAIN.
1411
1356
  export const getIntervalElements = (value: IntervalValue): Value[] => {
1412
1357
  getCardinality(value);
1413
1358
  if (value.low === 1 && value.high >= 1) return getIntervalDomain(value.high);
@@ -1422,7 +1367,6 @@ const isIntervalDomain = (domain: Value[]) =>
1422
1367
  domain === longIntervalDomains.get(domain.length)?.deref())) ||
1423
1368
  intervalArrays.has(domain);
1424
1369
 
1425
- // A set such as (1..2) \cap S, whose elements are the shared array, gets a copy.
1426
1370
  export const getFunctionDomain = (set: Value): Value[] => {
1427
1371
  const elements = toFiniteSet(set).elements;
1428
1372
  return isIntervalDomain(elements) && !(typeof set === "object" && set.kind === "Interval")
@@ -1576,25 +1520,24 @@ const advanceCombination = (indices: number[], length: number) => {
1576
1520
  return false;
1577
1521
  };
1578
1522
 
1579
- // TLC enumerates SUBSET S lazily, by size and then in the order of S, so a quantifier that
1580
- // finds its witness early never builds the rest.
1581
1523
  export const getLazySubsets = (value: Value): Value[] | undefined => {
1582
1524
  if (typeof value !== "object" || value.kind !== "Subset" || isOverriddenSet(value.base)) {
1583
1525
  return undefined;
1584
1526
  }
1585
1527
  const base = toFiniteSet(value.base).elements;
1586
1528
  if (base.length <= MAXIMUM_BUILT_SUBSET_BASE) return undefined;
1587
- const cursor = { index: -1, indices: [0] };
1529
+ let cursorIndex = -1;
1530
+ let indices = [0];
1588
1531
  const getSubset = (index: number) => {
1589
- if (index <= cursor.index) cursor.index = -1;
1590
- while (cursor.index < index) {
1591
- if (cursor.index === -1) cursor.indices = [];
1592
- else if (!advanceCombination(cursor.indices, base.length)) {
1593
- cursor.indices = Array.from({ length: cursor.indices.length + 1 }, (_, item) => item);
1532
+ if (index <= cursorIndex) cursorIndex = -1;
1533
+ while (cursorIndex < index) {
1534
+ if (cursorIndex === -1) indices = [];
1535
+ else if (!advanceCombination(indices, base.length)) {
1536
+ indices = Array.from({ length: indices.length + 1 }, (_value, position) => position);
1594
1537
  }
1595
- cursor.index++;
1538
+ cursorIndex++;
1596
1539
  }
1597
- return createSortedSet(cursor.indices.map((item) => base[item]));
1540
+ return createSortedSet(indices.map((baseIndex) => base[baseIndex]));
1598
1541
  };
1599
1542
  const length = 2 ** base.length;
1600
1543
  const isIndex = (property: string | symbol) =>
@@ -1613,7 +1556,7 @@ export const getLazySubsets = (value: Value): Value[] | undefined => {
1613
1556
  const getSortedSubsets = (base: Value[]) => {
1614
1557
  const subsets: Value[] = [];
1615
1558
  for (let size = 0; size <= base.length; size++) {
1616
- const indices = Array.from({ length: size }, (_, index) => index);
1559
+ const indices = Array.from({ length: size }, (_value, index) => index);
1617
1560
  do {
1618
1561
  subsets.push(createSortedSet(indices.map((index) => base[index])));
1619
1562
  } while (advanceCombination(indices, base.length));
@@ -1621,8 +1564,6 @@ const getSortedSubsets = (base: Value[]) => {
1621
1564
  return subsets;
1622
1565
  };
1623
1566
 
1624
- // TLC enumerates the sets in turn and stops at the first empty one. Later versions decide
1625
- // that the set is empty before they enumerate it.
1626
1567
  const getComponentElements = (
1627
1568
  sets: Value[],
1628
1569
  getElements: (set: Value) => Value[],
@@ -1656,7 +1597,6 @@ const enumerateSet = (
1656
1597
  return elements;
1657
1598
  }
1658
1599
  case "Subset": {
1659
- // TLC sizes the set before it enumerates it, and enumerates a set of size 0 as {{}}.
1660
1600
  if (getCardinality(value.base) === 0) return getSortedSubsets([]);
1661
1601
  if (!isSetValue(value.base)) {
1662
1602
  throw new TlaEvaluationError(
@@ -1670,15 +1610,14 @@ const enumerateSet = (
1670
1610
  return getSortedSubsets(base);
1671
1611
  }
1672
1612
  case "FunctionSet": {
1673
- if (!valueVersion.is174 && isEmptySet(value)) return [];
1613
+ if (!isVersion174 && isEmptySet(value)) return [];
1674
1614
  if (isOverriddenSet(value.domain)) {
1675
1615
  throw new TlaEvaluationError(
1676
1616
  `Attempted to enumerate a set of the form [D -> R],but the domain D:\n${formatValue(value.domain)}\ncannot be enumerated.`,
1677
1617
  );
1678
1618
  }
1679
1619
  const domain = getFunctionDomain(value.domain);
1680
- // TLC 1.7.4 requires an enumerable range even of an empty domain.
1681
- if ((domain.length > 0 || valueVersion.is174) && isOverriddenSet(value.range)) {
1620
+ if ((domain.length > 0 || isVersion174) && isOverriddenSet(value.range)) {
1682
1621
  throw new TlaEvaluationError(
1683
1622
  `Attempted to enumerate a set of the form [D -> R],but the range R:\n${formatValue(value.range)}\ncannot be enumerated.`,
1684
1623
  );
@@ -1689,7 +1628,7 @@ const enumerateSet = (
1689
1628
  );
1690
1629
  }
1691
1630
  case "RecordSet": {
1692
- if (!valueVersion.is174 && isEmptySet(value)) return [];
1631
+ if (!isVersion174 && isEmptySet(value)) return [];
1693
1632
  const fields = getComponentElements(
1694
1633
  value.sets,
1695
1634
  getElements,
@@ -1701,7 +1640,7 @@ const enumerateSet = (
1701
1640
  );
1702
1641
  }
1703
1642
  case "ProductSet": {
1704
- if (!valueVersion.is174 && isEmptySet(value)) return [];
1643
+ if (!isVersion174 && isEmptySet(value)) return [];
1705
1644
  const factors = getComponentElements(
1706
1645
  value.factors,
1707
1646
  getElements,
@@ -1799,8 +1738,6 @@ export const toFiniteSet = (value: Value): SetValue => {
1799
1738
  return set;
1800
1739
  };
1801
1740
 
1802
- // TLC's enumerator of a set sorts the set enumerations it streams, and fails on a set it can't
1803
- // enumerate, before it yields an element, so S \cup T fails on T before it streams S.
1804
1741
  const openStream = (value: Value): void => {
1805
1742
  if (typeof value !== "object" || isCachedSet(value)) return;
1806
1743
  switch (value.kind) {
@@ -1849,8 +1786,6 @@ const listUnionLikeTlc = (union: SetOperationValue) => {
1849
1786
  return [...left, ...right];
1850
1787
  };
1851
1788
 
1852
- // EnumerableValue#elements(NORMALIZED) sorts a union outside its methods, so a failed comparison
1853
- // isn't a fingerprint failure.
1854
1789
  export const getNormalizedSetElements = (value: Value) => {
1855
1790
  if (
1856
1791
  typeof value !== "object" ||
@@ -1866,8 +1801,6 @@ export const getNormalizedSetElements = (value: Value) => {
1866
1801
  return elements;
1867
1802
  };
1868
1803
 
1869
- // TLC streams the sets a lazy set combines until it caches it, so a shared element repeats. Like
1870
- // its enumerators, the stream stops at the first element visit accepts.
1871
1804
  export const someStreamedElement = (value: Value, visit: (element: Value) => boolean): boolean => {
1872
1805
  if (typeof value !== "object" || isCachedSet(value)) {
1873
1806
  return toFiniteSet(value).elements.some(visit);
@@ -1875,7 +1808,6 @@ export const someStreamedElement = (value: Value, visit: (element: Value) => boo
1875
1808
  const sets = setTrace.sets;
1876
1809
  sets?.push(value);
1877
1810
  openStream(value);
1878
- // The set is enumerated only while it computes an element, not while visit looks at one.
1879
1811
  const isFound = someElementOfStream(value, (element) => {
1880
1812
  sets?.pop();
1881
1813
  const isAccepted = visit(element);
@@ -1904,9 +1836,6 @@ const getCombinedSets = (value: ObjectValue) =>
1904
1836
  ? [value.range]
1905
1837
  : undefined;
1906
1838
 
1907
- // Whether one evaluation can stand for the next: no UNION or \cup, which streams until TLC
1908
- // caches it, no lambda, which fingerprinting turns into a record, and no record a
1909
- // comparison reorders.
1910
1839
  export const isSettledValue = (value: Value): boolean => {
1911
1840
  if (typeof value !== "object") return true;
1912
1841
  switch (value.kind) {
@@ -1935,7 +1864,6 @@ export const isSettledValue = (value: Value): boolean => {
1935
1864
  }
1936
1865
  };
1937
1866
 
1938
- // A set of tuples, records, or functions with such a component has to stream too.
1939
1867
  const isStreamedApart = (value: Value): boolean => {
1940
1868
  if (typeof value !== "object" || isCachedSet(value)) return false;
1941
1869
  if (value.kind === "Union") return true;
@@ -1949,7 +1877,6 @@ const isStreamedApart = (value: Value): boolean => {
1949
1877
  };
1950
1878
 
1951
1879
  const someElementOfStream = (value: ObjectValue, visit: (element: Value) => boolean): boolean => {
1952
- // TLC streams the sets of a UNION it hasn't enumerated, so a repeated set yields them twice.
1953
1880
  if (value.kind === "Union") {
1954
1881
  return getUnionSets(value, getStreamedElements).some((set, index) =>
1955
1882
  someStreamedElement(getUnionSet(set, index, value), visit),
@@ -2026,7 +1953,6 @@ const getProductCardinality = (
2026
1953
  const describeOverflow = (value: Value) =>
2027
1954
  `Overflow when computing the number of elements in:\n${formatValue(value)}`;
2028
1955
 
2029
- // TLC 1.7.4 sizes every component without checking for an empty one, so an infinite one fails.
2030
1956
  export const getCardinality = (value: Value, version?: TlcVersion): number => {
2031
1957
  if (typeof value !== "object") {
2032
1958
  throw new TlaEvaluationError(
@@ -2070,12 +1996,12 @@ export const getCardinality = (value: Value, version?: TlcVersion): number => {
2070
1996
  const domainSize = getCardinality(value.domain, version);
2071
1997
  const rangeSize = getCardinality(value.range, version);
2072
1998
  if (rangeSize <= 1) return domainSize === 0 ? 1 : rangeSize;
2073
- const size = { value: 1 };
1999
+ let size = 1;
2074
2000
  for (let index = 0; index < domainSize; index++) {
2075
- size.value *= rangeSize;
2076
- if (size.value > MAXIMUM_INTEGER) throw new TlaEvaluationError(describeOverflow(value));
2001
+ size *= rangeSize;
2002
+ if (size > MAXIMUM_INTEGER) throw new TlaEvaluationError(describeOverflow(value));
2077
2003
  }
2078
- return size.value;
2004
+ return size;
2079
2005
  }
2080
2006
  case "RecordSet":
2081
2007
  if (version !== "1.7.4" && isEmptySet(value)) return 0;
@@ -2128,12 +2054,12 @@ export const isFiniteSet = (value: Value): boolean => {
2128
2054
  case "RecordSet":
2129
2055
  case "ProductSet": {
2130
2056
  const parts = value.kind === "RecordSet" ? value.sets : value.factors;
2131
- const finiteness = { isAllFinite: true };
2057
+ let isAllFinite = true;
2132
2058
  for (const part of parts) {
2133
- if (!isFiniteSet(part)) finiteness.isAllFinite = false;
2059
+ if (!isFiniteSet(part)) isAllFinite = false;
2134
2060
  else if (isEmptySet(part)) return true;
2135
2061
  }
2136
- return finiteness.isAllFinite;
2062
+ return isAllFinite;
2137
2063
  }
2138
2064
  case "Union":
2139
2065
  if (!isTlcEnumerable(value.base)) {
@@ -2164,18 +2090,18 @@ export const isFiniteSet = (value: Value): boolean => {
2164
2090
  };
2165
2091
 
2166
2092
  const findSortedIndex = (sortedValues: Value[], target: Value, compare = compareValues) => {
2167
- const bounds = { low: 0, high: sortedValues.length - 1 };
2168
- while (bounds.low <= bounds.high) {
2169
- const middle = (bounds.low + bounds.high) >> 1;
2093
+ let low = 0;
2094
+ let high = sortedValues.length - 1;
2095
+ while (low <= high) {
2096
+ const middle = (low + high) >> 1;
2170
2097
  const comparison = compare(sortedValues[middle], target);
2171
2098
  if (comparison === 0) return middle;
2172
- if (comparison < 0) bounds.low = middle + 1;
2173
- else bounds.high = middle - 1;
2099
+ if (comparison < 0) low = middle + 1;
2100
+ else high = middle - 1;
2174
2101
  }
2175
2102
  return -1;
2176
2103
  };
2177
2104
 
2178
- // A sorted set as TLC builds it unsorted, by adding each element it doesn't hold yet.
2179
2105
  export const createUnsortedCopy = (set: SetValue, added: Value[]): SetValue => {
2180
2106
  const elements = set.elements;
2181
2107
  if (elements.length > getMaximumSetSize()) throw new TlaSetBoundError(getSetBoundMessage());
@@ -2227,14 +2153,10 @@ interface TracedCallStack {
2227
2153
  fingerprint?: unknown;
2228
2154
  }
2229
2155
 
2230
- // The values being checked or enumerated, outermost first, as TLC's CallStackTool replays a
2231
- // computation. TLC adds each one with a source to the FingerprintException.
2232
2156
  export const setTrace: { sets: Value[] | undefined; callStack?: TracedCallStack } = {
2233
2157
  sets: undefined,
2234
2158
  };
2235
2159
 
2236
- // TLC never takes the fast path, so the failure's leftovers in the replay's trace go first.
2237
- // The operands are arguments so that a hot caller passes functions it doesn't allocate.
2238
2160
  export const computeLikeTlcOnFailure = <Left, Right, Result>(
2239
2161
  compute: (left: Left, right: Right) => Result,
2240
2162
  computeLikeTlc: (left: Left, right: Right, error: TlaEvaluationError) => Result,
@@ -2273,17 +2195,17 @@ export const isElementOf = (element: Value, set: Value): boolean => {
2273
2195
  return isElement;
2274
2196
  };
2275
2197
 
2276
- // Integers compare by subtraction, so a search that meets only integers needs no other comparison.
2277
2198
  const isIntegerInSortedSet = (element: number, set: SetValue) => {
2278
2199
  const elements = set.elements;
2279
- const bounds = { low: 0, high: elements.length - 1 };
2280
- while (bounds.low <= bounds.high) {
2281
- const middle = (bounds.low + bounds.high) >> 1;
2200
+ let low = 0;
2201
+ let high = elements.length - 1;
2202
+ while (low <= high) {
2203
+ const middle = (low + high) >> 1;
2282
2204
  const probe = elements[middle];
2283
2205
  if (typeof probe !== "number") return undefined;
2284
2206
  if (probe === element) return true;
2285
- if (probe < element) bounds.low = middle + 1;
2286
- else bounds.high = middle - 1;
2207
+ if (probe < element) low = middle + 1;
2208
+ else high = middle - 1;
2287
2209
  }
2288
2210
  return false;
2289
2211
  };
@@ -2319,7 +2241,6 @@ const isElementOfAnySet = (element: Value, set: Value): boolean => {
2319
2241
  if (!isSetValue(set)) throw new TlaEvaluationError(describeNonSetMembership(element, set));
2320
2242
  switch (set.kind) {
2321
2243
  case "Set":
2322
- // TLC compares the element with the set's.
2323
2244
  cacheSet(element);
2324
2245
  return computeLikeTlcOnFailure(isInSortedSet, isInSortedSetLikeTlc, element, set);
2325
2246
  case "MixedSet":
@@ -2345,7 +2266,6 @@ const isElementOfAnySet = (element: Value, set: Value): boolean => {
2345
2266
  return set.name !== "Nat" || (typeof element === "number" && element >= 0);
2346
2267
  }
2347
2268
  case "Subset":
2348
- // TLC fails on a value that isn't Enumerable, model values and Java-overridden sets included.
2349
2269
  if (!isSetValue(element) || isOverriddenSet(element)) {
2350
2270
  throw new TlaEvaluationError(
2351
2271
  `Attempted to check if the non-enumerable value\n${prettyPrint(formatValue(element))}\nis element of\n${prettyPrint(formatValue(set))}`,
@@ -2355,7 +2275,6 @@ const isElementOfAnySet = (element: Value, set: Value): boolean => {
2355
2275
  case "FunctionSet": {
2356
2276
  if (!isFunctionValue(element)) {
2357
2277
  if (isModelValue(element)) return isModelValueMember(element, set);
2358
- // TLC prints the element without Values.ppr.
2359
2278
  throw new TlaEvaluationError(
2360
2279
  `Attempted to check if \n${formatValue(element)}\nwhich is not a TLC function value, is in the set of functions:\n${prettyPrint(formatValue(set))}`,
2361
2280
  );
@@ -2420,7 +2339,6 @@ const isElementOfAnySet = (element: Value, set: Value): boolean => {
2420
2339
  );
2421
2340
  }
2422
2341
  const sets = toFiniteSet(set.base).elements;
2423
- // TLC sorts the sets to enumerate them, and comparing two sets normalizes both.
2424
2342
  if (sets.length > 1) {
2425
2343
  sets.forEach((inner) => {
2426
2344
  if (typeof inner === "object" && inner.kind === "Set") inner.isNormalized = true;
@@ -2439,7 +2357,6 @@ const isElementOfAnySet = (element: Value, set: Value): boolean => {
2439
2357
  const elements = getSequenceElements(element);
2440
2358
  if (elements === undefined) {
2441
2359
  if (isModelValue(element)) return isModelValueMember(element, set);
2442
- // TLC raises TLC_MODULE_CHECK_MEMBER_OF.
2443
2360
  throw new TlaModuleError(
2444
2361
  `Attempted to check if the value:\n${prettyPrint(formatValue(element))}\nis an element of ${prettyPrint(formatValue(set))}.`,
2445
2362
  );
@@ -2523,16 +2440,14 @@ export const describeMissingArgument = (
2523
2440
  }
2524
2441
  };
2525
2442
 
2526
- // TLC 1.7.4 words a key of the wrong type in an EXCEPT, which selects rather than applies, apart.
2527
2443
  const describeExceptKey = (functionValue: FunctionValue, argument: Value) => {
2528
- if (!valueVersion.is174) return describeMissingArgument(functionValue, argument);
2444
+ if (!isVersion174) return describeMissingArgument(functionValue, argument);
2529
2445
  const shown = prettyPrint(formatValue(argument));
2530
2446
  return getFunctionClass(functionValue) === "tuple"
2531
2447
  ? `Attempted to apply tuple to a non-integer argument ${shown}.`
2532
2448
  : `Attempted to apply record to a non-string argument ${shown}.`;
2533
2449
  };
2534
2450
 
2535
- // A key the domain holds itself, which TLC finds without a comparison that can fail.
2536
2451
  const findIdenticalKey = (domain: Value[], argument: Value) => {
2537
2452
  const first = domain[0];
2538
2453
  if (typeof argument === "number" && typeof first === "number") {
@@ -2547,20 +2462,18 @@ export const createNonIntegerKeyError = (argument: Value) =>
2547
2462
  `Attempted to apply function with integer domain to the non-integer argument ${prettyPrint(formatValue(argument))}`,
2548
2463
  );
2549
2464
 
2550
- // TLC searches a domain this small linearly with equals, and a larger one with compareTo.
2551
2465
  const LINEAR_SEARCH_LIMIT = 32;
2552
2466
 
2553
2467
  const findDomainIndex = (functionValue: FunctionValue, argument: Value) =>
2554
2468
  findSortedIndex(functionValue.domain, argument);
2555
2469
 
2556
- // TLC searches a function it hasn't normalized linearly, in the order its keys were written.
2557
2470
  const failKeyLikeTlc = (
2558
2471
  functionValue: FunctionValue,
2559
2472
  argument: Value,
2560
2473
  error: TlaEvaluationError,
2561
2474
  ): never => {
2562
2475
  const keys = getWrittenKeys(functionValue);
2563
- if (valueVersion.is174 || keys !== functionValue.domain || keys.length < LINEAR_SEARCH_LIMIT) {
2476
+ if (isVersion174 || keys !== functionValue.domain || keys.length < LINEAR_SEARCH_LIMIT) {
2564
2477
  keys.forEach((key) => equalsLikeTlc(key, argument));
2565
2478
  } else {
2566
2479
  findSortedIndex(keys, argument, compareLikeTlc);
@@ -2568,8 +2481,6 @@ const failKeyLikeTlc = (
2568
2481
  throw error;
2569
2482
  };
2570
2483
 
2571
- // A tuple or a record rejects a key of the wrong type, and another function a non-integer for
2572
- // an interval domain, which TLC 1.7.4 doesn't. Any other key type fails TLC's comparison.
2573
2484
  const findKeyIndexLikeTlc = (
2574
2485
  functionValue: FunctionValue,
2575
2486
  argument: Value,
@@ -2585,7 +2496,7 @@ const findKeyIndexLikeTlc = (
2585
2496
  }
2586
2497
  if (
2587
2498
  keyType === undefined &&
2588
- !valueVersion.is174 &&
2499
+ !isVersion174 &&
2589
2500
  typeof argument !== "number" &&
2590
2501
  isIntervalDomain(domain)
2591
2502
  ) {
@@ -2597,8 +2508,7 @@ const findKeyIndexLikeTlc = (
2597
2508
  return computeLikeTlcOnFailure(findDomainIndex, failKeyLikeTlc, functionValue, argument);
2598
2509
  };
2599
2510
 
2600
- // TLC checks each argument against its bound in turn. FcnLambdaValue#apply then indexes past an
2601
- // argument tuple shorter than the bounds and ignores one longer, where this rejects the list.
2511
+ // Deviation from TLC: FcnLambdaValue#apply indexes past an argument tuple shorter than the bounds and ignores a longer one, where this rejects the list.
2602
2512
  const checkBoundArguments = (
2603
2513
  functionValue: LazyFunctionValue,
2604
2514
  tupleLengths: (number | undefined)[],
@@ -2642,8 +2552,6 @@ const checkBoundArguments = (
2642
2552
  if (elements.length > tupleLengths.length) throw createListError();
2643
2553
  };
2644
2554
 
2645
- // TLC checks that a FcnLambdaValue's domain holds the argument, and searches only the FcnRcdValue
2646
- // it converts the lambda to when it fingerprints it.
2647
2555
  const isOutsideLambdaDomain = (functionValue: FunctionValue, argument: Value) =>
2648
2556
  functionValue.lambdaDomain !== undefined &&
2649
2557
  functionValue.hash === 0 &&
@@ -2700,7 +2608,6 @@ export const getDomain = (functionValue: Value): Value => {
2700
2608
  const interval = getDomainInterval(finite.domain);
2701
2609
  if (interval) return interval;
2702
2610
  const set = createSortedSet(finite.domain);
2703
- // TLC keeps a record's domain in written order and sorts any other function's.
2704
2611
  if (finite.functionClass === "record") {
2705
2612
  if (finite.writtenKeys) set.writtenElements = finite.writtenKeys;
2706
2613
  return set;
@@ -2804,8 +2711,6 @@ const getElementContribution = (elementHash: number) =>
2804
2711
  const getEntryContribution = (keyHash: number, valueHash: number) =>
2805
2712
  finalizeHash((Math.imul(keyHash, 0x27d4eb2d) + valueHash) | 0);
2806
2713
 
2807
- // The hash of a value TLC fails to fingerprint, such as Nat. Calls with one as an argument still
2808
- // share their result, and a state that holds one fails when TLC fingerprints it.
2809
2714
  const UNFINGERPRINTABLE_HASH = 0x2f6b1c4d;
2810
2715
 
2811
2716
  const computeValueHash = (value: SetValue | FunctionValue) => {
@@ -2831,7 +2736,6 @@ const computeValueHash = (value: SetValue | FunctionValue) => {
2831
2736
  return hash;
2832
2737
  };
2833
2738
 
2834
- // TLC enumerates each set and function, and fails on Nat, Seq(S) and the like.
2835
2739
  const fingerprintValue = (value: Value): void => {
2836
2740
  if (typeof value !== "object" || value.kind === "ModelValue") return;
2837
2741
  if (value.kind === "InfiniteSet" || value.kind === "SequenceSet") {
@@ -2848,7 +2752,6 @@ const fingerprintValue = (value: Value): void => {
2848
2752
  functionValue.range.forEach(fingerprintValue);
2849
2753
  };
2850
2754
 
2851
- // Hashing can fail, as for a set too large to enumerate, and so can a value TLC fails on.
2852
2755
  export const getFingerprintError = (value: Value) => {
2853
2756
  if (typeof value !== "object" || value.kind === "ModelValue") return undefined;
2854
2757
  try {
@@ -3016,7 +2919,6 @@ const expandLazySet = (value: LazySetValue) => {
3016
2919
  if (value.kind === "SetOperation" || value.kind === "FilteredSet" || value.kind === "Union") {
3017
2920
  return toFiniteSet(value);
3018
2921
  }
3019
- // TLC asks whether the domain is empty before it sizes it.
3020
2922
  if (value.kind === "FunctionSet") {
3021
2923
  const domainSize = isEmptySet(value.domain) ? 0 : getCardinality(value.domain);
3022
2924
  const size = domainSize === 0 ? 1 : getCardinality(value.range) ** domainSize;
@@ -3027,9 +2929,7 @@ const expandLazySet = (value: LazySetValue) => {
3027
2929
  };
3028
2930
 
3029
2931
  const getExpandedLazySet = (value: LazySetValue, isStrict: boolean) => {
3030
- // UnionValue alone of TLC's lazy sets doesn't swallow the failure to enumerate itself.
3031
2932
  if (isStrict || value.kind === "Union") return expandLazySet(value);
3032
- // TLC sizes the base of SUBSET before it enumerates it, and only a failure to size it is quiet.
3033
2933
  if (
3034
2934
  value.kind === "Subset" &&
3035
2935
  isFunctionValue(value.base) &&
@@ -3037,7 +2937,6 @@ const getExpandedLazySet = (value: LazySetValue, isStrict: boolean) => {
3037
2937
  ) {
3038
2938
  return expandLazySet(value);
3039
2939
  }
3040
- // TLC prints a set it can't enumerate symbolically, except in Print, PrintT, and ToString.
3041
2940
  if (!isEnumerable(value)) return undefined;
3042
2941
  try {
3043
2942
  return expandLazySet(value);
@@ -3071,7 +2970,6 @@ const formatLazySet = (value: LazySetValue, isStrict: boolean): string => {
3071
2970
  }
3072
2971
  };
3073
2972
 
3074
- // TLC prints a record when every key is letters, digits, and underscores, with a letter.
3075
2973
  const isFieldName = (name: string) =>
3076
2974
  /^[\p{L}\p{Nd}_]*$/u.test(name) &&
3077
2975
  /\p{L}/u.test(name) &&
@@ -3087,7 +2985,6 @@ const toPrintedFunction = (value: LazyFunctionValue) => {
3087
2985
  }
3088
2986
  };
3089
2987
 
3090
- // TLC prints normalized copies, so the printed values themselves stay unnormalized.
3091
2988
  export const formatNormalizedCopies = (values: Value[]) => {
3092
2989
  const markedSets: SetValue[] = [];
3093
2990
  const normalize = (value: Value): void => {
@@ -3190,12 +3087,11 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
3190
3087
  }
3191
3088
  if (
3192
3089
  functionValue.domain.length === 0 &&
3193
- valueVersion.is174 &&
3090
+ isVersion174 &&
3194
3091
  getFunctionClass(value) !== "tuple"
3195
3092
  ) {
3196
3093
  return "<< >>";
3197
3094
  }
3198
- // TLC normalizes a lambda's domain to print it, but evaluates its body afterward.
3199
3095
  const formatRange = (inner: Value) =>
3200
3096
  formatValue(inner, isStrict, isNormalized && getFunctionClass(value) !== "lambda");
3201
3097
  if (functionValue.domain.length === 0 && getFunctionClass(value) === "record") return "[]";
@@ -3205,7 +3101,6 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
3205
3101
  const pairs = isNormalized
3206
3102
  ? functionValue.domain.map((key, index) => [key, functionValue.range[index]])
3207
3103
  : getPrintedPairs(functionValue);
3208
- // A RecordValue always prints as a record, a FcnRcdValue only if its keys are names.
3209
3104
  if (
3210
3105
  getFunctionClass(value) === "record" ||
3211
3106
  functionValue.domain.every((key) => typeof key === "string" && isFieldName(key))
@@ -3217,7 +3112,6 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
3217
3112
  }
3218
3113
  };
3219
3114
 
3220
- // TLC counts a value it can't compare with the previous one as changed.
3221
3115
  const isUnchanged = (previous: Value | undefined, value: Value) => {
3222
3116
  if (previous === undefined) return false;
3223
3117
  try {
@@ -3230,15 +3124,10 @@ const isUnchanged = (previous: Value | undefined, value: Value) => {
3230
3124
 
3231
3125
  const JAVA_FORMAT_SPECIFIER = /%(\d+\$)?([-#+ 0,(<]*)(\d+)?(\.\d+)?(.?)/gs;
3232
3126
 
3233
- const getJavaHashCode = (text: string) =>
3234
- Array.from({ length: text.length }, (_, index) => text.charCodeAt(index)).reduce(
3235
- (hash, unit) => (Math.imul(hash, 31) + unit) | 0,
3236
- 0,
3237
- );
3238
-
3239
3127
  const formatJava = (format: string, args: string[]) => {
3240
- const indices = { next: 0, last: -1 };
3241
- const isFitting = { value: true };
3128
+ let nextIndex = 0;
3129
+ let lastIndex = -1;
3130
+ let isFitting = true;
3242
3131
  const text = format.replace(
3243
3132
  JAVA_FORMAT_SPECIFIER,
3244
3133
  (match, explicit?: string, flags = "", width?: string, precision?: string, conversion = "") => {
@@ -3248,15 +3137,15 @@ const formatJava = (format: string, args: string[]) => {
3248
3137
  const otherFlags = flags.replace("<", "");
3249
3138
  if (conversion === "n" && match === "%n") return "\n";
3250
3139
  if (conversion === "%" && precision === undefined && /^-?$/.test(otherFlags)) {
3251
- if (otherFlags === "-" && width === undefined) isFitting.value = false;
3140
+ if (otherFlags === "-" && width === undefined) isFitting = false;
3252
3141
  return pad("%");
3253
3142
  }
3254
3143
  const index = isRelative
3255
- ? indices.last
3144
+ ? lastIndex
3256
3145
  : explicit === undefined
3257
- ? indices.next++
3146
+ ? nextIndex++
3258
3147
  : Number(explicit.slice(0, -1)) - 1;
3259
- indices.last = index;
3148
+ lastIndex = index;
3260
3149
  const arg = args[index];
3261
3150
  if (
3262
3151
  !"sSbBhH".includes(conversion) ||
@@ -3265,7 +3154,7 @@ const formatJava = (format: string, args: string[]) => {
3265
3154
  (otherFlags === "-" && width === undefined) ||
3266
3155
  arg === undefined
3267
3156
  ) {
3268
- isFitting.value = false;
3157
+ isFitting = false;
3269
3158
  return match;
3270
3159
  }
3271
3160
  const lower = conversion.toLowerCase();
@@ -3277,10 +3166,9 @@ const formatJava = (format: string, args: string[]) => {
3277
3166
  return conversion === lower ? shown : shown.toUpperCase();
3278
3167
  },
3279
3168
  );
3280
- return isFitting.value ? text : undefined;
3169
+ return isFitting ? text : undefined;
3281
3170
  };
3282
3171
 
3283
- // TLC prints the VIEW of a state, but not of one it prints as a difference from the previous.
3284
3172
  export const formatState = (state: Record<string, Value>, previous?: Record<string, Value>) => {
3285
3173
  const getViewText = previous ? undefined : viewTexts.get(state);
3286
3174
  if (getViewText) return getViewText();