tla-ts 0.0.4-dev.a297123 → 0.0.4-dev.e9adbb1

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
@@ -234,7 +234,13 @@ const compareFunctions = (left: FunctionValue, right: FunctionValue) => {
234
234
  const rightRange = right.range;
235
235
  if (leftRange === rightRange) return 0;
236
236
  for (let index = 0; index < leftRange.length; index++) {
237
- const valueComparison = compareValues(leftRange[index], rightRange[index]);
237
+ const leftValue = leftRange[index];
238
+ const rightValue = rightRange[index];
239
+ // V8 can't inline the recursive compareValues here, so two numbers skip the call.
240
+ const valueComparison =
241
+ typeof leftValue === "number" && typeof rightValue === "number"
242
+ ? leftValue - rightValue
243
+ : compareValues(leftValue, rightValue);
238
244
  if (valueComparison !== 0) return valueComparison;
239
245
  }
240
246
  return 0;
@@ -619,9 +625,14 @@ export const valuesEqual = (left: Value, right: Value): boolean => {
619
625
  // TLC prints and words errors about an equal function by its Java class, so a value can stand for
620
626
  // another in a state only if each function in it has the same class.
621
627
  const createSameValueCheck = (isClassSensitive: boolean) => {
622
- const areSameValues = (left: Value[], right: Value[]) =>
623
- left === right ||
624
- (left.length === right.length && left.every((element, index) => isSame(element, right[index])));
628
+ const areSameValues = (left: Value[], right: Value[]) => {
629
+ if (left === right) return true;
630
+ if (left.length !== right.length) return false;
631
+ for (let index = 0; index < left.length; index++) {
632
+ if (!isSame(left[index], right[index])) return false;
633
+ }
634
+ return true;
635
+ };
625
636
  const isSame = (left: Value, right: Value): boolean => {
626
637
  if (left === right) return true;
627
638
  if (typeof left !== "object" || typeof right !== "object") return false;
@@ -1025,10 +1036,7 @@ export const markNormalized = (value: Value): void => {
1025
1036
  };
1026
1037
 
1027
1038
  export const isEqualLikeTlc = (left: Value, right: Value) =>
1028
- computeLikeTlcOnFailure(
1029
- () => valuesEqual(left, right),
1030
- () => equalsLikeTlc(left, right),
1031
- );
1039
+ computeLikeTlcOnFailure(valuesEqual, equalsLikeTlc, left, right);
1032
1040
 
1033
1041
  const isStrictlySorted = (elements: Value[]) => {
1034
1042
  for (let index = 1; index < elements.length; index++) {
@@ -1052,9 +1060,11 @@ export const createSet = (elements: Value[]): SetValue => {
1052
1060
  // TLC builds a set enumeration without sorting it, so one whose elements it can't compare still
1053
1061
  // evaluates.
1054
1062
  export const createEnumeratedSet = (elements: Value[]): SetValue | MixedSetValue =>
1055
- computeLikeTlcOnFailure<SetValue | MixedSetValue>(
1056
- () => createSet(elements),
1057
- () => ({ kind: "MixedSet", elements }),
1063
+ computeLikeTlcOnFailure<Value[], undefined, SetValue | MixedSetValue>(
1064
+ createSet,
1065
+ (mixed) => ({ kind: "MixedSet", elements: mixed }),
1066
+ elements,
1067
+ undefined,
1058
1068
  );
1059
1069
 
1060
1070
  export const EMPTY_SET = createSortedSet([]);
@@ -1596,6 +1606,38 @@ const getCombinedSets = (value: ObjectValue) =>
1596
1606
  ? [value.range]
1597
1607
  : undefined;
1598
1608
 
1609
+ // Whether a value TLC builds alike at each evaluation and that enumerating, comparing or
1610
+ // fingerprinting it leaves alike, so that one evaluation can stand for the next. It holds no UNION or
1611
+ // \cup, which streams until TLC caches it, no lambda, which fingerprinting turns into a record, and
1612
+ // no record a comparison reorders.
1613
+ export const isSettledValue = (value: Value): boolean => {
1614
+ if (typeof value !== "object") return true;
1615
+ switch (value.kind) {
1616
+ case "ModelValue":
1617
+ case "Interval":
1618
+ return true;
1619
+ case "Set":
1620
+ return value.elements.every(isSettledValue);
1621
+ case "Function":
1622
+ return (
1623
+ value.functionClass !== "lambda" &&
1624
+ value.writtenKeys === undefined &&
1625
+ value.domain.every(isSettledValue) &&
1626
+ value.range.every(isSettledValue)
1627
+ );
1628
+ case "SetOperation":
1629
+ return (
1630
+ value.operator !== "\\cup" && isSettledValue(value.left) && isSettledValue(value.right)
1631
+ );
1632
+ case "FilteredSet":
1633
+ return isSettledValue(value.base);
1634
+ case "ProductSet":
1635
+ return value.factors.every(isSettledValue);
1636
+ default:
1637
+ return false;
1638
+ }
1639
+ };
1640
+
1599
1641
  // Whether a set streams its elements in another order than the normalized set, so that a set of
1600
1642
  // tuples, records, or functions it is a component of has to stream too.
1601
1643
  const isStreamedApart = (value: Value): boolean => {
@@ -1870,17 +1912,20 @@ export const setTrace: { sets: Value[] | undefined; callStack?: TracedCallStack
1870
1912
  };
1871
1913
 
1872
1914
  // Takes a fast path, and computes as TLC does where it fails. TLC never takes the fast path, so
1873
- // the failure's leftovers in the replay's trace go first.
1874
- export const computeLikeTlcOnFailure = <Result>(
1875
- compute: () => Result,
1876
- computeLikeTlc: (error: TlaEvaluationError) => Result,
1915
+ // the failure's leftovers in the replay's trace go first. Both paths take their operands as
1916
+ // arguments, so a hot caller passes functions it doesn't allocate.
1917
+ export const computeLikeTlcOnFailure = <Left, Right, Result>(
1918
+ compute: (left: Left, right: Right) => Result,
1919
+ computeLikeTlc: (left: Left, right: Right, error: TlaEvaluationError) => Result,
1920
+ left: Left,
1921
+ right: Right,
1877
1922
  ): Result => {
1878
1923
  const sets = setTrace.sets;
1879
1924
  const callStack = setTrace.callStack;
1880
1925
  const depth = sets?.length ?? 0;
1881
1926
  const trace = callStack && { ...callStack, length: callStack.expressions.length };
1882
1927
  try {
1883
- return compute();
1928
+ return compute(left, right);
1884
1929
  } catch (error) {
1885
1930
  if (!(error instanceof TlaEvaluationError)) throw error;
1886
1931
  if (sets) sets.length = depth;
@@ -1889,7 +1934,7 @@ export const computeLikeTlcOnFailure = <Result>(
1889
1934
  callStack.isFrozen = trace.isFrozen;
1890
1935
  callStack.fingerprint = trace.fingerprint;
1891
1936
  }
1892
- return computeLikeTlc(error);
1937
+ return computeLikeTlc(left, right, error);
1893
1938
  }
1894
1939
  };
1895
1940
 
@@ -1902,19 +1947,20 @@ export const isElementOf = (element: Value, set: Value): boolean => {
1902
1947
  return isElement;
1903
1948
  };
1904
1949
 
1950
+ const isInSortedSet = (element: Value, set: SetValue) => findIndex(set.elements, element) !== -1;
1951
+
1952
+ const isInSortedSetLikeTlc = (element: Value, set: SetValue) =>
1953
+ normalizedSets.has(set)
1954
+ ? searchLikeTlc(element, set.elements).isFound
1955
+ : isElementOfLikeTlc(element, set.elements);
1956
+
1905
1957
  const isElementOfSet = (element: Value, set: Value): boolean => {
1906
1958
  if (!isSetValue(set)) throw new TlaEvaluationError(describeNonSetMembership(element, set));
1907
1959
  switch (set.kind) {
1908
1960
  case "Set":
1909
1961
  // SetEnumValue.member compares the element with the set's.
1910
1962
  cacheSet(element);
1911
- return computeLikeTlcOnFailure(
1912
- () => findIndex(set.elements, element) !== -1,
1913
- () =>
1914
- normalizedSets.has(set)
1915
- ? searchLikeTlc(element, set.elements).isFound
1916
- : isElementOfLikeTlc(element, set.elements),
1917
- );
1963
+ return computeLikeTlcOnFailure(isInSortedSet, isInSortedSetLikeTlc, element, set);
1918
1964
  case "MixedSet":
1919
1965
  return isElementOfLikeTlc(element, set.elements);
1920
1966
  case "Interval":
@@ -2126,6 +2172,15 @@ export const createNonIntegerKeyError = (argument: Value) =>
2126
2172
  // binary search with compareTo.
2127
2173
  const LINEAR_SEARCH_LIMIT = 32;
2128
2174
 
2175
+ const failKeyLikeTlc = (domain: Value[], argument: Value, error: TlaEvaluationError): never => {
2176
+ if (valueVersion.is174 || domain.length < LINEAR_SEARCH_LIMIT) {
2177
+ domain.forEach((key) => equalsLikeTlc(key, argument));
2178
+ } else {
2179
+ findIndex(domain, argument, compareLikeTlc);
2180
+ }
2181
+ throw error;
2182
+ };
2183
+
2129
2184
  // TLC looks a key up by the Java class of the function: a tuple or a record rejects a key of the
2130
2185
  // wrong type, and another function rejects a non-integer for a domain it keeps as an interval, which
2131
2186
  // TLC 1.7.4 doesn't. A key of another type than the domain's keys otherwise fails TLC's comparison.
@@ -2148,17 +2203,7 @@ const findKeyIndexLikeTlc = (
2148
2203
  }
2149
2204
  const identical = findIdenticalKey(domain, argument);
2150
2205
  if (identical !== -1) return identical;
2151
- return computeLikeTlcOnFailure(
2152
- () => findIndex(domain, argument),
2153
- (error) => {
2154
- if (valueVersion.is174 || domain.length < LINEAR_SEARCH_LIMIT) {
2155
- domain.forEach((key) => equalsLikeTlc(key, argument));
2156
- } else {
2157
- findIndex(domain, argument, compareLikeTlc);
2158
- }
2159
- throw error;
2160
- },
2161
- );
2206
+ return computeLikeTlcOnFailure(findIndex, failKeyLikeTlc, domain, argument);
2162
2207
  };
2163
2208
 
2164
2209
  export const applyFunction = (