tla-ts 0.0.4-dev.66582d4 → 0.0.4-dev.7c70fed

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
@@ -512,14 +516,16 @@ const cacheSet = (value: Value) => {
512
516
  // FcnLambdaValue#fingerPrint caches the FcnRcdValue the function enumerates to, which applies it
513
517
  // from then on, so a function TLC has fingerprinted is no longer a lambda.
514
518
  export const markFingerprinted = (value: Value): void => {
515
- if (
516
- (!lambdaPresence.isPresent && !streamedSetPresence.isPresent) ||
517
- typeof value !== "object" ||
518
- fingerprintedValues.has(value)
519
- ) {
519
+ if ((!lambdaPresence.isPresent && !streamedSetPresence.isPresent) || typeof value !== "object") {
520
520
  return;
521
521
  }
522
- 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
+ }
523
529
  if (value.kind === "Function") {
524
530
  if (value.functionClass === "lambda") value.functionClass = undefined;
525
531
  value.domain.forEach(markFingerprinted);
@@ -1019,10 +1025,7 @@ export const markNormalized = (value: Value): void => {
1019
1025
  };
1020
1026
 
1021
1027
  export const isEqualLikeTlc = (left: Value, right: Value) =>
1022
- computeLikeTlcOnFailure(
1023
- () => valuesEqual(left, right),
1024
- () => equalsLikeTlc(left, right),
1025
- );
1028
+ computeLikeTlcOnFailure(valuesEqual, equalsLikeTlc, left, right);
1026
1029
 
1027
1030
  const isStrictlySorted = (elements: Value[]) => {
1028
1031
  for (let index = 1; index < elements.length; index++) {
@@ -1046,9 +1049,11 @@ export const createSet = (elements: Value[]): SetValue => {
1046
1049
  // TLC builds a set enumeration without sorting it, so one whose elements it can't compare still
1047
1050
  // evaluates.
1048
1051
  export const createEnumeratedSet = (elements: Value[]): SetValue | MixedSetValue =>
1049
- computeLikeTlcOnFailure<SetValue | MixedSetValue>(
1050
- () => createSet(elements),
1051
- () => ({ kind: "MixedSet", elements }),
1052
+ computeLikeTlcOnFailure<Value[], undefined, SetValue | MixedSetValue>(
1053
+ createSet,
1054
+ (mixed) => ({ kind: "MixedSet", elements: mixed }),
1055
+ elements,
1056
+ undefined,
1052
1057
  );
1053
1058
 
1054
1059
  export const EMPTY_SET = createSortedSet([]);
@@ -1864,17 +1869,20 @@ export const setTrace: { sets: Value[] | undefined; callStack?: TracedCallStack
1864
1869
  };
1865
1870
 
1866
1871
  // Takes a fast path, and computes as TLC does where it fails. TLC never takes the fast path, so
1867
- // the failure's leftovers in the replay's trace go first.
1868
- export const computeLikeTlcOnFailure = <Result>(
1869
- compute: () => Result,
1870
- 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,
1871
1879
  ): Result => {
1872
1880
  const sets = setTrace.sets;
1873
1881
  const callStack = setTrace.callStack;
1874
1882
  const depth = sets?.length ?? 0;
1875
1883
  const trace = callStack && { ...callStack, length: callStack.expressions.length };
1876
1884
  try {
1877
- return compute();
1885
+ return compute(left, right);
1878
1886
  } catch (error) {
1879
1887
  if (!(error instanceof TlaEvaluationError)) throw error;
1880
1888
  if (sets) sets.length = depth;
@@ -1883,7 +1891,7 @@ export const computeLikeTlcOnFailure = <Result>(
1883
1891
  callStack.isFrozen = trace.isFrozen;
1884
1892
  callStack.fingerprint = trace.fingerprint;
1885
1893
  }
1886
- return computeLikeTlc(error);
1894
+ return computeLikeTlc(left, right, error);
1887
1895
  }
1888
1896
  };
1889
1897
 
@@ -1896,19 +1904,20 @@ export const isElementOf = (element: Value, set: Value): boolean => {
1896
1904
  return isElement;
1897
1905
  };
1898
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
+
1899
1914
  const isElementOfSet = (element: Value, set: Value): boolean => {
1900
1915
  if (!isSetValue(set)) throw new TlaEvaluationError(describeNonSetMembership(element, set));
1901
1916
  switch (set.kind) {
1902
1917
  case "Set":
1903
1918
  // SetEnumValue.member compares the element with the set's.
1904
1919
  cacheSet(element);
1905
- return computeLikeTlcOnFailure(
1906
- () => findIndex(set.elements, element) !== -1,
1907
- () =>
1908
- normalizedSets.has(set)
1909
- ? searchLikeTlc(element, set.elements).isFound
1910
- : isElementOfLikeTlc(element, set.elements),
1911
- );
1920
+ return computeLikeTlcOnFailure(isInSortedSet, isInSortedSetLikeTlc, element, set);
1912
1921
  case "MixedSet":
1913
1922
  return isElementOfLikeTlc(element, set.elements);
1914
1923
  case "Interval":
@@ -2101,17 +2110,14 @@ const describeExceptKey = (functionValue: FunctionValue, argument: Value) => {
2101
2110
  : `Attempted to apply record to a non-string argument ${shown}.`;
2102
2111
  };
2103
2112
 
2104
- 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) => {
2105
2115
  const first = domain[0];
2106
2116
  if (typeof argument === "number" && typeof first === "number") {
2107
2117
  const offset = argument - first;
2108
2118
  if (domain[offset] === argument) return offset;
2109
2119
  }
2110
- if (domain.length <= 8) {
2111
- for (let index = 0; index < domain.length; index++)
2112
- if (domain[index] === argument) return index;
2113
- }
2114
- return findIndex(domain, argument);
2120
+ return domain.length <= 8 ? domain.indexOf(argument) : -1;
2115
2121
  };
2116
2122
 
2117
2123
  export const createNonIntegerKeyError = (argument: Value) =>
@@ -2123,6 +2129,15 @@ export const createNonIntegerKeyError = (argument: Value) =>
2123
2129
  // binary search with compareTo.
2124
2130
  const LINEAR_SEARCH_LIMIT = 32;
2125
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
+
2126
2141
  // TLC looks a key up by the Java class of the function: a tuple or a record rejects a key of the
2127
2142
  // wrong type, and another function rejects a non-integer for a domain it keeps as an interval, which
2128
2143
  // TLC 1.7.4 doesn't. A key of another type than the domain's keys otherwise fails TLC's comparison.
@@ -2143,17 +2158,9 @@ const findKeyIndexLikeTlc = (
2143
2158
  throw createNonIntegerKeyError(argument);
2144
2159
  }
2145
2160
  }
2146
- return computeLikeTlcOnFailure(
2147
- () => findKeyIndex(domain, argument),
2148
- (error) => {
2149
- if (valueVersion.is174 || domain.length < LINEAR_SEARCH_LIMIT) {
2150
- domain.forEach((key) => equalsLikeTlc(key, argument));
2151
- } else {
2152
- findIndex(domain, argument, compareLikeTlc);
2153
- }
2154
- throw error;
2155
- },
2156
- );
2161
+ const identical = findIdenticalKey(domain, argument);
2162
+ if (identical !== -1) return identical;
2163
+ return computeLikeTlcOnFailure(findIndex, failKeyLikeTlc, domain, argument);
2157
2164
  };
2158
2165
 
2159
2166
  export const applyFunction = (