tla-ts 0.0.3-dev.fe239f8 → 0.0.4-dev.0be9567

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.
package/src/values.ts CHANGED
@@ -14,6 +14,7 @@ export interface SetValue {
14
14
  elements: Value[];
15
15
  hash: number;
16
16
  fingerprint: number;
17
+ isFingerprinted: boolean;
17
18
  }
18
19
 
19
20
  // A set enumeration whose elements TLC can't sort, such as {1, "a"}. TLC sorts a set enumeration
@@ -31,6 +32,7 @@ export interface FunctionValue {
31
32
  fingerprint: number;
32
33
  writtenKeys: Value[] | undefined;
33
34
  functionClass: FunctionClass | undefined;
35
+ isFingerprinted: boolean;
34
36
  }
35
37
 
36
38
  export interface IntervalValue {
@@ -433,6 +435,7 @@ export const createSortedSet = (elements: Value[]): SetValue => ({
433
435
  elements,
434
436
  hash: 0,
435
437
  fingerprint: 0,
438
+ isFingerprinted: false,
436
439
  });
437
440
 
438
441
  export const createSortedFunction = (domain: Value[], range: Value[]): FunctionValue => ({
@@ -443,6 +446,7 @@ export const createSortedFunction = (domain: Value[], range: Value[]): FunctionV
443
446
  fingerprint: 0,
444
447
  writtenKeys: undefined,
445
448
  functionClass: undefined,
449
+ isFingerprinted: false,
446
450
  });
447
451
 
448
452
  // TLC words a failed function application by the Java class of the function: FcnLambdaValue for
@@ -489,21 +493,39 @@ export const markStreamedSet = <Streamed extends UnionValue | SetOperationValue>
489
493
  return value;
490
494
  };
491
495
 
496
+ // The sets that stream their elements apart from the normalized set until TLC caches them.
497
+ const STREAMING_KINDS = new Set<ObjectValue["kind"]>([
498
+ "Union",
499
+ "SetOperation",
500
+ "FilteredSet",
501
+ "ProductSet",
502
+ "RecordSet",
503
+ "FunctionSet",
504
+ ]);
505
+
492
506
  const cacheSet = (value: Value) => {
493
- if (streamedSetPresence.isPresent && typeof value === "object") cachedSets.add(value);
507
+ if (
508
+ streamedSetPresence.isPresent &&
509
+ typeof value === "object" &&
510
+ STREAMING_KINDS.has(value.kind)
511
+ ) {
512
+ cachedSets.add(value);
513
+ }
494
514
  };
495
515
 
496
516
  // FcnLambdaValue#fingerPrint caches the FcnRcdValue the function enumerates to, which applies it
497
517
  // from then on, so a function TLC has fingerprinted is no longer a lambda.
498
518
  export const markFingerprinted = (value: Value): void => {
499
- if (
500
- (!lambdaPresence.isPresent && !streamedSetPresence.isPresent) ||
501
- typeof value !== "object" ||
502
- fingerprintedValues.has(value)
503
- ) {
519
+ if ((!lambdaPresence.isPresent && !streamedSetPresence.isPresent) || typeof value !== "object") {
504
520
  return;
505
521
  }
506
- fingerprintedValues.add(value);
522
+ if (value.kind === "Function" || value.kind === "Set") {
523
+ if (value.isFingerprinted) return;
524
+ value.isFingerprinted = true;
525
+ } else {
526
+ if (fingerprintedValues.has(value)) return;
527
+ fingerprintedValues.add(value);
528
+ }
507
529
  if (value.kind === "Function") {
508
530
  if (value.functionClass === "lambda") value.functionClass = undefined;
509
531
  value.domain.forEach(markFingerprinted);
@@ -1003,10 +1025,7 @@ export const markNormalized = (value: Value): void => {
1003
1025
  };
1004
1026
 
1005
1027
  export const isEqualLikeTlc = (left: Value, right: Value) =>
1006
- computeLikeTlcOnFailure(
1007
- () => valuesEqual(left, right),
1008
- () => equalsLikeTlc(left, right),
1009
- );
1028
+ computeLikeTlcOnFailure(valuesEqual, equalsLikeTlc, left, right);
1010
1029
 
1011
1030
  const isStrictlySorted = (elements: Value[]) => {
1012
1031
  for (let index = 1; index < elements.length; index++) {
@@ -1030,9 +1049,11 @@ export const createSet = (elements: Value[]): SetValue => {
1030
1049
  // TLC builds a set enumeration without sorting it, so one whose elements it can't compare still
1031
1050
  // evaluates.
1032
1051
  export const createEnumeratedSet = (elements: Value[]): SetValue | MixedSetValue =>
1033
- computeLikeTlcOnFailure<SetValue | MixedSetValue>(
1034
- () => createSet(elements),
1035
- () => ({ kind: "MixedSet", elements }),
1052
+ computeLikeTlcOnFailure<Value[], undefined, SetValue | MixedSetValue>(
1053
+ createSet,
1054
+ (mixed) => ({ kind: "MixedSet", elements: mixed }),
1055
+ elements,
1056
+ undefined,
1036
1057
  );
1037
1058
 
1038
1059
  export const EMPTY_SET = createSortedSet([]);
@@ -1848,17 +1869,20 @@ export const setTrace: { sets: Value[] | undefined; callStack?: TracedCallStack
1848
1869
  };
1849
1870
 
1850
1871
  // Takes a fast path, and computes as TLC does where it fails. TLC never takes the fast path, so
1851
- // the failure's leftovers in the replay's trace go first.
1852
- export const computeLikeTlcOnFailure = <Result>(
1853
- compute: () => Result,
1854
- computeLikeTlc: (error: TlaEvaluationError) => Result,
1872
+ // the failure's leftovers in the replay's trace go first. Both paths take their operands as
1873
+ // arguments, so a hot caller passes functions it doesn't allocate.
1874
+ export const computeLikeTlcOnFailure = <Left, Right, Result>(
1875
+ compute: (left: Left, right: Right) => Result,
1876
+ computeLikeTlc: (left: Left, right: Right, error: TlaEvaluationError) => Result,
1877
+ left: Left,
1878
+ right: Right,
1855
1879
  ): Result => {
1856
1880
  const sets = setTrace.sets;
1857
1881
  const callStack = setTrace.callStack;
1858
1882
  const depth = sets?.length ?? 0;
1859
1883
  const trace = callStack && { ...callStack, length: callStack.expressions.length };
1860
1884
  try {
1861
- return compute();
1885
+ return compute(left, right);
1862
1886
  } catch (error) {
1863
1887
  if (!(error instanceof TlaEvaluationError)) throw error;
1864
1888
  if (sets) sets.length = depth;
@@ -1867,7 +1891,7 @@ export const computeLikeTlcOnFailure = <Result>(
1867
1891
  callStack.isFrozen = trace.isFrozen;
1868
1892
  callStack.fingerprint = trace.fingerprint;
1869
1893
  }
1870
- return computeLikeTlc(error);
1894
+ return computeLikeTlc(left, right, error);
1871
1895
  }
1872
1896
  };
1873
1897
 
@@ -1880,19 +1904,20 @@ export const isElementOf = (element: Value, set: Value): boolean => {
1880
1904
  return isElement;
1881
1905
  };
1882
1906
 
1907
+ const isInSortedSet = (element: Value, set: SetValue) => findIndex(set.elements, element) !== -1;
1908
+
1909
+ const isInSortedSetLikeTlc = (element: Value, set: SetValue) =>
1910
+ normalizedSets.has(set)
1911
+ ? searchLikeTlc(element, set.elements).isFound
1912
+ : isElementOfLikeTlc(element, set.elements);
1913
+
1883
1914
  const isElementOfSet = (element: Value, set: Value): boolean => {
1884
1915
  if (!isSetValue(set)) throw new TlaEvaluationError(describeNonSetMembership(element, set));
1885
1916
  switch (set.kind) {
1886
1917
  case "Set":
1887
1918
  // SetEnumValue.member compares the element with the set's.
1888
1919
  cacheSet(element);
1889
- return computeLikeTlcOnFailure(
1890
- () => findIndex(set.elements, element) !== -1,
1891
- () =>
1892
- normalizedSets.has(set)
1893
- ? searchLikeTlc(element, set.elements).isFound
1894
- : isElementOfLikeTlc(element, set.elements),
1895
- );
1920
+ return computeLikeTlcOnFailure(isInSortedSet, isInSortedSetLikeTlc, element, set);
1896
1921
  case "MixedSet":
1897
1922
  return isElementOfLikeTlc(element, set.elements);
1898
1923
  case "Interval":
@@ -2085,17 +2110,14 @@ const describeExceptKey = (functionValue: FunctionValue, argument: Value) => {
2085
2110
  : `Attempted to apply record to a non-string argument ${shown}.`;
2086
2111
  };
2087
2112
 
2088
- const findKeyIndex = (domain: Value[], argument: Value) => {
2113
+ // A key the domain holds itself, which TLC finds without a comparison that can fail.
2114
+ const findIdenticalKey = (domain: Value[], argument: Value) => {
2089
2115
  const first = domain[0];
2090
2116
  if (typeof argument === "number" && typeof first === "number") {
2091
2117
  const offset = argument - first;
2092
2118
  if (domain[offset] === argument) return offset;
2093
2119
  }
2094
- if (domain.length <= 8) {
2095
- for (let index = 0; index < domain.length; index++)
2096
- if (domain[index] === argument) return index;
2097
- }
2098
- return findIndex(domain, argument);
2120
+ return domain.length <= 8 ? domain.indexOf(argument) : -1;
2099
2121
  };
2100
2122
 
2101
2123
  export const createNonIntegerKeyError = (argument: Value) =>
@@ -2107,6 +2129,15 @@ export const createNonIntegerKeyError = (argument: Value) =>
2107
2129
  // binary search with compareTo.
2108
2130
  const LINEAR_SEARCH_LIMIT = 32;
2109
2131
 
2132
+ const failKeyLikeTlc = (domain: Value[], argument: Value, error: TlaEvaluationError): never => {
2133
+ if (valueVersion.is174 || domain.length < LINEAR_SEARCH_LIMIT) {
2134
+ domain.forEach((key) => equalsLikeTlc(key, argument));
2135
+ } else {
2136
+ findIndex(domain, argument, compareLikeTlc);
2137
+ }
2138
+ throw error;
2139
+ };
2140
+
2110
2141
  // TLC looks a key up by the Java class of the function: a tuple or a record rejects a key of the
2111
2142
  // wrong type, and another function rejects a non-integer for a domain it keeps as an interval, which
2112
2143
  // TLC 1.7.4 doesn't. A key of another type than the domain's keys otherwise fails TLC's comparison.
@@ -2127,17 +2158,9 @@ const findKeyIndexLikeTlc = (
2127
2158
  throw createNonIntegerKeyError(argument);
2128
2159
  }
2129
2160
  }
2130
- return computeLikeTlcOnFailure(
2131
- () => findKeyIndex(domain, argument),
2132
- (error) => {
2133
- if (valueVersion.is174 || domain.length < LINEAR_SEARCH_LIMIT) {
2134
- domain.forEach((key) => equalsLikeTlc(key, argument));
2135
- } else {
2136
- findIndex(domain, argument, compareLikeTlc);
2137
- }
2138
- throw error;
2139
- },
2140
- );
2161
+ const identical = findIdenticalKey(domain, argument);
2162
+ if (identical !== -1) return identical;
2163
+ return computeLikeTlcOnFailure(findIndex, failKeyLikeTlc, domain, argument);
2141
2164
  };
2142
2165
 
2143
2166
  export const applyFunction = (