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

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tla-ts",
3
- "version": "0.0.4-dev.a297123",
3
+ "version": "0.0.4-dev.f48b575",
4
4
  "description": "A TLA+ interpreter and model checker in TypeScript, 2× faster than TLC",
5
5
  "keywords": [
6
6
  "formal verification",
@@ -162,8 +162,11 @@ export const CORE_OPERATORS: Builtin[] = [
162
162
  : differenceSorted;
163
163
  // The merge compares the elements, which TLC tests for membership one by one instead.
164
164
  return computeLikeTlcOnFailure(
165
- () => merge(toFiniteSet(left), toFiniteSet(right)),
166
- () => combineReducible(operator, left, right),
165
+ (finiteLeft, finiteRight) => merge(toFiniteSet(finiteLeft), toFiniteSet(finiteRight)),
166
+ (reducibleLeft, reducibleRight) =>
167
+ combineReducible(operator, reducibleLeft, reducibleRight),
168
+ left,
169
+ right,
167
170
  );
168
171
  }
169
172
  if (isReducibleSet(left)) return combineReducible(operator, left, right);
package/src/values.ts CHANGED
@@ -1025,10 +1025,7 @@ export const markNormalized = (value: Value): void => {
1025
1025
  };
1026
1026
 
1027
1027
  export const isEqualLikeTlc = (left: Value, right: Value) =>
1028
- computeLikeTlcOnFailure(
1029
- () => valuesEqual(left, right),
1030
- () => equalsLikeTlc(left, right),
1031
- );
1028
+ computeLikeTlcOnFailure(valuesEqual, equalsLikeTlc, left, right);
1032
1029
 
1033
1030
  const isStrictlySorted = (elements: Value[]) => {
1034
1031
  for (let index = 1; index < elements.length; index++) {
@@ -1052,9 +1049,11 @@ export const createSet = (elements: Value[]): SetValue => {
1052
1049
  // TLC builds a set enumeration without sorting it, so one whose elements it can't compare still
1053
1050
  // evaluates.
1054
1051
  export const createEnumeratedSet = (elements: Value[]): SetValue | MixedSetValue =>
1055
- computeLikeTlcOnFailure<SetValue | MixedSetValue>(
1056
- () => createSet(elements),
1057
- () => ({ kind: "MixedSet", elements }),
1052
+ computeLikeTlcOnFailure<Value[], undefined, SetValue | MixedSetValue>(
1053
+ createSet,
1054
+ (mixed) => ({ kind: "MixedSet", elements: mixed }),
1055
+ elements,
1056
+ undefined,
1058
1057
  );
1059
1058
 
1060
1059
  export const EMPTY_SET = createSortedSet([]);
@@ -1870,17 +1869,20 @@ export const setTrace: { sets: Value[] | undefined; callStack?: TracedCallStack
1870
1869
  };
1871
1870
 
1872
1871
  // 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,
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,
1877
1879
  ): Result => {
1878
1880
  const sets = setTrace.sets;
1879
1881
  const callStack = setTrace.callStack;
1880
1882
  const depth = sets?.length ?? 0;
1881
1883
  const trace = callStack && { ...callStack, length: callStack.expressions.length };
1882
1884
  try {
1883
- return compute();
1885
+ return compute(left, right);
1884
1886
  } catch (error) {
1885
1887
  if (!(error instanceof TlaEvaluationError)) throw error;
1886
1888
  if (sets) sets.length = depth;
@@ -1889,7 +1891,7 @@ export const computeLikeTlcOnFailure = <Result>(
1889
1891
  callStack.isFrozen = trace.isFrozen;
1890
1892
  callStack.fingerprint = trace.fingerprint;
1891
1893
  }
1892
- return computeLikeTlc(error);
1894
+ return computeLikeTlc(left, right, error);
1893
1895
  }
1894
1896
  };
1895
1897
 
@@ -1902,19 +1904,20 @@ export const isElementOf = (element: Value, set: Value): boolean => {
1902
1904
  return isElement;
1903
1905
  };
1904
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
+
1905
1914
  const isElementOfSet = (element: Value, set: Value): boolean => {
1906
1915
  if (!isSetValue(set)) throw new TlaEvaluationError(describeNonSetMembership(element, set));
1907
1916
  switch (set.kind) {
1908
1917
  case "Set":
1909
1918
  // SetEnumValue.member compares the element with the set's.
1910
1919
  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
- );
1920
+ return computeLikeTlcOnFailure(isInSortedSet, isInSortedSetLikeTlc, element, set);
1918
1921
  case "MixedSet":
1919
1922
  return isElementOfLikeTlc(element, set.elements);
1920
1923
  case "Interval":
@@ -2126,6 +2129,15 @@ export const createNonIntegerKeyError = (argument: Value) =>
2126
2129
  // binary search with compareTo.
2127
2130
  const LINEAR_SEARCH_LIMIT = 32;
2128
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
+
2129
2141
  // TLC looks a key up by the Java class of the function: a tuple or a record rejects a key of the
2130
2142
  // wrong type, and another function rejects a non-integer for a domain it keeps as an interval, which
2131
2143
  // TLC 1.7.4 doesn't. A key of another type than the domain's keys otherwise fails TLC's comparison.
@@ -2148,17 +2160,7 @@ const findKeyIndexLikeTlc = (
2148
2160
  }
2149
2161
  const identical = findIdenticalKey(domain, argument);
2150
2162
  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
- );
2163
+ return computeLikeTlcOnFailure(findIndex, failKeyLikeTlc, domain, argument);
2162
2164
  };
2163
2165
 
2164
2166
  export const applyFunction = (