tla-ts 0.0.4-dev.2be6058 → 0.0.4-dev.5c7c024

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([]);
@@ -1590,6 +1595,38 @@ const getCombinedSets = (value: ObjectValue) =>
1590
1595
  ? [value.range]
1591
1596
  : undefined;
1592
1597
 
1598
+ // Whether a value TLC builds alike at each evaluation and that enumerating, comparing or
1599
+ // fingerprinting it leaves alike, so that one evaluation can stand for the next. It holds no UNION or
1600
+ // \cup, which streams until TLC caches it, no lambda, which fingerprinting turns into a record, and
1601
+ // no record a comparison reorders.
1602
+ export const isSettledValue = (value: Value): boolean => {
1603
+ if (typeof value !== "object") return true;
1604
+ switch (value.kind) {
1605
+ case "ModelValue":
1606
+ case "Interval":
1607
+ return true;
1608
+ case "Set":
1609
+ return value.elements.every(isSettledValue);
1610
+ case "Function":
1611
+ return (
1612
+ value.functionClass !== "lambda" &&
1613
+ value.writtenKeys === undefined &&
1614
+ value.domain.every(isSettledValue) &&
1615
+ value.range.every(isSettledValue)
1616
+ );
1617
+ case "SetOperation":
1618
+ return (
1619
+ value.operator !== "\\cup" && isSettledValue(value.left) && isSettledValue(value.right)
1620
+ );
1621
+ case "FilteredSet":
1622
+ return isSettledValue(value.base);
1623
+ case "ProductSet":
1624
+ return value.factors.every(isSettledValue);
1625
+ default:
1626
+ return false;
1627
+ }
1628
+ };
1629
+
1593
1630
  // Whether a set streams its elements in another order than the normalized set, so that a set of
1594
1631
  // tuples, records, or functions it is a component of has to stream too.
1595
1632
  const isStreamedApart = (value: Value): boolean => {
@@ -1864,17 +1901,20 @@ export const setTrace: { sets: Value[] | undefined; callStack?: TracedCallStack
1864
1901
  };
1865
1902
 
1866
1903
  // 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,
1904
+ // the failure's leftovers in the replay's trace go first. Both paths take their operands as
1905
+ // arguments, so a hot caller passes functions it doesn't allocate.
1906
+ export const computeLikeTlcOnFailure = <Left, Right, Result>(
1907
+ compute: (left: Left, right: Right) => Result,
1908
+ computeLikeTlc: (left: Left, right: Right, error: TlaEvaluationError) => Result,
1909
+ left: Left,
1910
+ right: Right,
1871
1911
  ): Result => {
1872
1912
  const sets = setTrace.sets;
1873
1913
  const callStack = setTrace.callStack;
1874
1914
  const depth = sets?.length ?? 0;
1875
1915
  const trace = callStack && { ...callStack, length: callStack.expressions.length };
1876
1916
  try {
1877
- return compute();
1917
+ return compute(left, right);
1878
1918
  } catch (error) {
1879
1919
  if (!(error instanceof TlaEvaluationError)) throw error;
1880
1920
  if (sets) sets.length = depth;
@@ -1883,7 +1923,7 @@ export const computeLikeTlcOnFailure = <Result>(
1883
1923
  callStack.isFrozen = trace.isFrozen;
1884
1924
  callStack.fingerprint = trace.fingerprint;
1885
1925
  }
1886
- return computeLikeTlc(error);
1926
+ return computeLikeTlc(left, right, error);
1887
1927
  }
1888
1928
  };
1889
1929
 
@@ -1896,19 +1936,20 @@ export const isElementOf = (element: Value, set: Value): boolean => {
1896
1936
  return isElement;
1897
1937
  };
1898
1938
 
1939
+ const isInSortedSet = (element: Value, set: SetValue) => findIndex(set.elements, element) !== -1;
1940
+
1941
+ const isInSortedSetLikeTlc = (element: Value, set: SetValue) =>
1942
+ normalizedSets.has(set)
1943
+ ? searchLikeTlc(element, set.elements).isFound
1944
+ : isElementOfLikeTlc(element, set.elements);
1945
+
1899
1946
  const isElementOfSet = (element: Value, set: Value): boolean => {
1900
1947
  if (!isSetValue(set)) throw new TlaEvaluationError(describeNonSetMembership(element, set));
1901
1948
  switch (set.kind) {
1902
1949
  case "Set":
1903
1950
  // SetEnumValue.member compares the element with the set's.
1904
1951
  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
- );
1952
+ return computeLikeTlcOnFailure(isInSortedSet, isInSortedSetLikeTlc, element, set);
1912
1953
  case "MixedSet":
1913
1954
  return isElementOfLikeTlc(element, set.elements);
1914
1955
  case "Interval":
@@ -2101,17 +2142,14 @@ const describeExceptKey = (functionValue: FunctionValue, argument: Value) => {
2101
2142
  : `Attempted to apply record to a non-string argument ${shown}.`;
2102
2143
  };
2103
2144
 
2104
- const findKeyIndex = (domain: Value[], argument: Value) => {
2145
+ // A key the domain holds itself, which TLC finds without a comparison that can fail.
2146
+ const findIdenticalKey = (domain: Value[], argument: Value) => {
2105
2147
  const first = domain[0];
2106
2148
  if (typeof argument === "number" && typeof first === "number") {
2107
2149
  const offset = argument - first;
2108
2150
  if (domain[offset] === argument) return offset;
2109
2151
  }
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);
2152
+ return domain.length <= 8 ? domain.indexOf(argument) : -1;
2115
2153
  };
2116
2154
 
2117
2155
  export const createNonIntegerKeyError = (argument: Value) =>
@@ -2123,6 +2161,15 @@ export const createNonIntegerKeyError = (argument: Value) =>
2123
2161
  // binary search with compareTo.
2124
2162
  const LINEAR_SEARCH_LIMIT = 32;
2125
2163
 
2164
+ const failKeyLikeTlc = (domain: Value[], argument: Value, error: TlaEvaluationError): never => {
2165
+ if (valueVersion.is174 || domain.length < LINEAR_SEARCH_LIMIT) {
2166
+ domain.forEach((key) => equalsLikeTlc(key, argument));
2167
+ } else {
2168
+ findIndex(domain, argument, compareLikeTlc);
2169
+ }
2170
+ throw error;
2171
+ };
2172
+
2126
2173
  // TLC looks a key up by the Java class of the function: a tuple or a record rejects a key of the
2127
2174
  // wrong type, and another function rejects a non-integer for a domain it keeps as an interval, which
2128
2175
  // TLC 1.7.4 doesn't. A key of another type than the domain's keys otherwise fails TLC's comparison.
@@ -2143,17 +2190,9 @@ const findKeyIndexLikeTlc = (
2143
2190
  throw createNonIntegerKeyError(argument);
2144
2191
  }
2145
2192
  }
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
- );
2193
+ const identical = findIdenticalKey(domain, argument);
2194
+ if (identical !== -1) return identical;
2195
+ return computeLikeTlcOnFailure(findIndex, failKeyLikeTlc, domain, argument);
2157
2196
  };
2158
2197
 
2159
2198
  export const applyFunction = (