tla-ts 0.0.3-dev.3082068 → 0.0.3-dev.33ae522

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/successors.ts CHANGED
@@ -7,7 +7,12 @@ import {
7
7
  type QuantifierExpression,
8
8
  } from "./ast";
9
9
  import { getReadVariables } from "./dependencies";
10
- import { TlaEvaluationError, TlaIllegalStateError, TlaIncompleteStateError } from "./errors";
10
+ import {
11
+ TlaEvaluationError,
12
+ TlaIllegalStateError,
13
+ TlaIncompleteStateError,
14
+ TlaUnassignedError,
15
+ } from "./errors";
11
16
  import { EXIT_STATUS, setExitCode } from "./exit-codes";
12
17
  import {
13
18
  describeLocation,
@@ -53,6 +58,7 @@ export interface Successor {
53
58
  interface Run {
54
59
  environment: Environment;
55
60
  isInit: boolean;
61
+ isEnabled: boolean;
56
62
  current: Value[] | undefined;
57
63
  isStopped: boolean;
58
64
  }
@@ -950,8 +956,17 @@ const compileAction = (
950
956
  const unchanged = getUnchangedWrapper(expression.subscript);
951
957
  return (run, scope, partial, action, isFixed, onComplete) =>
952
958
  body(run, scope, partial, action, isFixed, (nextPartial, nextAction) => {
953
- if (!holds(unchanged, getContext(run, scope, nextPartial)))
954
- onComplete(nextPartial, nextAction);
959
+ // ENABLED evaluates the subscript on the successor that the action specifies. TLC names
960
+ // a variable that the action leaves without a value.
961
+ try {
962
+ if (!holds(unchanged, getContext(run, scope, nextPartial)))
963
+ onComplete(nextPartial, nextAction);
964
+ } catch (error) {
965
+ if (!run.isEnabled || !(error instanceof TlaUnassignedError)) throw error;
966
+ throw new TlaEvaluationError(
967
+ `The action formula A appearing in a WF_v(A) or SF_v(A) operator does not specify the primed value of the variable ${error.variable} occurring in the state formula v.\n`,
968
+ );
969
+ }
955
970
  });
956
971
  }
957
972
  case "BoxAction":
@@ -1028,7 +1043,13 @@ export const isEnabled = (expression: Expression, context: Context): boolean =>
1028
1043
  );
1029
1044
  }
1030
1045
  const environment = context.environment;
1031
- const run: Run = { environment, isInit: false, current: state, isStopped: false };
1046
+ const run: Run = {
1047
+ environment,
1048
+ isInit: false,
1049
+ isEnabled: true,
1050
+ current: state,
1051
+ isStopped: false,
1052
+ };
1032
1053
  // TLC stops computing ENABLED at the first successor, so it skips the rest of the action.
1033
1054
  getRunner(expression, environment, false)(
1034
1055
  run,
@@ -1049,7 +1070,13 @@ export const forEachInitialState = (
1049
1070
  onState: (state: Value[]) => boolean,
1050
1071
  scope?: Scope,
1051
1072
  ) => {
1052
- const run: Run = { environment, isInit: true, current: undefined, isStopped: false };
1073
+ const run: Run = {
1074
+ environment,
1075
+ isInit: true,
1076
+ isEnabled: false,
1077
+ current: undefined,
1078
+ isStopped: false,
1079
+ };
1053
1080
  getRunner(expression, environment, true)(
1054
1081
  run,
1055
1082
  scope,
@@ -1091,7 +1118,13 @@ export const forEachSuccessor = (
1091
1118
  onSuccessor: (successor: Successor) => boolean,
1092
1119
  scope?: Scope,
1093
1120
  ) => {
1094
- const run: Run = { environment, isInit: false, current: state, isStopped: false };
1121
+ const run: Run = {
1122
+ environment,
1123
+ isInit: false,
1124
+ isEnabled: false,
1125
+ current: state,
1126
+ isStopped: false,
1127
+ };
1095
1128
  const counter = { successors: 0 };
1096
1129
  getRunner(expression, environment, false)(
1097
1130
  run,
@@ -0,0 +1,29 @@
1
+ import type { Expression } from "./ast";
2
+ import { TlaEvaluationError } from "./errors";
3
+ import type { Value } from "./values";
4
+
5
+ // While TLC replays a failure, it gives each set, tuple or record it builds the expression that
6
+ // built it. A method of such a value that fails prepends that expression to the error's trace, and
7
+ // TLC reports the error as one it was unable to fingerprint.
8
+ export const valueSources: { current: WeakMap<object, Expression> | undefined } = {
9
+ current: undefined,
10
+ };
11
+
12
+ const fingerprintTraces = new WeakMap<TlaEvaluationError, Expression[]>();
13
+
14
+ export const getFingerprintTrace = (error: unknown) =>
15
+ error instanceof TlaEvaluationError ? fingerprintTraces.get(error) : undefined;
16
+
17
+ export const withSource = <T>(value: Value, run: () => T): T => {
18
+ const sources = valueSources.current;
19
+ if (!sources || typeof value !== "object") return run();
20
+ try {
21
+ return run();
22
+ } catch (error) {
23
+ const source = sources.get(value);
24
+ if (source && error instanceof TlaEvaluationError) {
25
+ fingerprintTraces.set(error, [source, ...(fingerprintTraces.get(error) ?? [])]);
26
+ }
27
+ throw error;
28
+ }
29
+ };
package/src/values.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { TlaEvaluationError, TlaModuleError } from "./errors";
2
2
  import { prettyPrint } from "./pretty-print";
3
+ import { valueSources, withSource } from "./value-sources";
3
4
 
4
5
  export interface ModelValue {
5
6
  kind: "ModelValue";
@@ -733,16 +734,17 @@ const compareSetLikeTlc = (left: ObjectValue, right: Value): number => {
733
734
  );
734
735
  };
735
736
 
736
- const compareLikeTlc = (left: Value, right: Value): number => {
737
- if (typeof left !== "object") {
738
- if (typeof right === typeof left) return compareValues(left, right);
739
- if (isModelValue(right)) return compareModelValueWith(right, left);
740
- throw new TlaEvaluationError(describeScalarMismatch(left, right, false));
741
- }
742
- if (left.kind === "ModelValue") return compareModelValue(left, right);
743
- if (isFunctionValue(left)) return compareFunctionLikeTlc(left, right);
744
- return compareSetLikeTlc(left, right);
745
- };
737
+ const compareLikeTlc = (left: Value, right: Value): number =>
738
+ withSource(left, () => {
739
+ if (typeof left !== "object") {
740
+ if (typeof right === typeof left) return compareValues(left, right);
741
+ if (isModelValue(right)) return compareModelValueWith(right, left);
742
+ throw new TlaEvaluationError(describeScalarMismatch(left, right, false));
743
+ }
744
+ if (left.kind === "ModelValue") return compareModelValue(left, right);
745
+ if (isFunctionValue(left)) return compareFunctionLikeTlc(left, right);
746
+ return compareSetLikeTlc(left, right);
747
+ });
746
748
 
747
749
  const equalsElementsLikeTlc = (left: Value[], right: Value[]) =>
748
750
  left.every((element, index) => equalsLikeTlc(element, right[index]));
@@ -830,16 +832,17 @@ const equalsSetLikeTlc = (left: ObjectValue, right: Value): boolean => {
830
832
  );
831
833
  };
832
834
 
833
- const equalsLikeTlc = (left: Value, right: Value): boolean => {
834
- if (typeof left !== "object") {
835
- if (typeof right === typeof left) return left === right;
836
- if (isModelValue(right)) return equalsModelValueWith(right, left);
837
- throw new TlaEvaluationError(describeScalarMismatch(left, right, true));
838
- }
839
- if (left.kind === "ModelValue") return equalsModelValue(left, right);
840
- if (isFunctionValue(left)) return equalsFunctionLikeTlc(left, right);
841
- return equalsSetLikeTlc(left, right);
842
- };
835
+ const equalsLikeTlc = (left: Value, right: Value): boolean =>
836
+ withSource(left, () => {
837
+ if (typeof left !== "object") {
838
+ if (typeof right === typeof left) return left === right;
839
+ if (isModelValue(right)) return equalsModelValueWith(right, left);
840
+ throw new TlaEvaluationError(describeScalarMismatch(left, right, true));
841
+ }
842
+ if (left.kind === "ModelValue") return equalsModelValue(left, right);
843
+ if (isFunctionValue(left)) return equalsFunctionLikeTlc(left, right);
844
+ return equalsSetLikeTlc(left, right);
845
+ });
843
846
 
844
847
  // TLC's ValueVec.sort, which inserts each element into the sorted prefix by binary search.
845
848
  const sortLikeTlc = (elements: Value[]) => {
@@ -1338,7 +1341,10 @@ export const toFiniteSet = (value: Value): SetValue => {
1338
1341
  const cached = typeof value === "object" ? finiteSets.get(value) : undefined;
1339
1342
  if (cached) return cached;
1340
1343
  const set =
1341
- (typeof value === "object" ? comparedSets.get(value) : undefined) ?? enumerateFiniteSet(value);
1344
+ (typeof value === "object" ? comparedSets.get(value) : undefined) ??
1345
+ (valueSources.current
1346
+ ? withSource(value, () => enumerateFiniteSet(value))
1347
+ : enumerateFiniteSet(value));
1342
1348
  if (typeof value === "object") finiteSets.set(value, set);
1343
1349
  return set;
1344
1350
  };
@@ -1597,6 +1603,12 @@ const describeNonSetMembership = (element: Value, value: Value) => {
1597
1603
  return `${prefix} function ${formatValue(value)}`;
1598
1604
  };
1599
1605
 
1606
+ // TLC checks membership in a set that another set holds through the inner set's own method.
1607
+ const isElementOfSet = (element: Value, set: Value) =>
1608
+ valueSources.current
1609
+ ? withSource(set, () => isElementOf(element, set))
1610
+ : isElementOf(element, set);
1611
+
1600
1612
  export const isElementOf = (element: Value, set: Value): boolean => {
1601
1613
  if (!isSetValue(set)) throw new TlaEvaluationError(describeNonSetMembership(element, set));
1602
1614
  switch (set.kind) {
@@ -1605,10 +1617,10 @@ export const isElementOf = (element: Value, set: Value): boolean => {
1605
1617
  return findIndex(set.elements, element) !== -1;
1606
1618
  } catch (error) {
1607
1619
  if (!(error instanceof TlaEvaluationError)) throw error;
1608
- return isElementOfLikeTlc(element, set.elements);
1620
+ return withSource(set, () => isElementOfLikeTlc(element, set.elements));
1609
1621
  }
1610
1622
  case "MixedSet":
1611
- return isElementOfLikeTlc(element, set.elements);
1623
+ return withSource(set, () => isElementOfLikeTlc(element, set.elements));
1612
1624
  case "Interval":
1613
1625
  if (typeof element === "number") return element >= set.low && element <= set.high;
1614
1626
  if (set.low <= set.high && !isModelValue(element)) {
@@ -1630,8 +1642,12 @@ export const isElementOf = (element: Value, set: Value): boolean => {
1630
1642
  return set.name !== "Nat" || (typeof element === "number" && element >= 0);
1631
1643
  }
1632
1644
  case "Subset":
1633
- if (!isSetValue(element)) throw new TlaEvaluationError(describeMembership(element, set));
1634
- return toFiniteSet(element).elements.every((item) => isElementOf(item, set.base));
1645
+ if (!isTlcEnumerable(element)) {
1646
+ throw new TlaEvaluationError(
1647
+ `Attempted to check if the non-enumerable value\n${prettyPrint(formatValue(element))}\nis element of\n${prettyPrint(formatValue(set))}`,
1648
+ );
1649
+ }
1650
+ return toFiniteSet(element).elements.every((item) => isElementOfSet(item, set.base));
1635
1651
  case "FunctionSet": {
1636
1652
  if (!isFunctionValue(element)) {
1637
1653
  if (isModelValue(element)) return false;
@@ -1648,7 +1664,7 @@ export const isElementOf = (element: Value, set: Value): boolean => {
1648
1664
  { kind: "Interval", low, high: low + functionValue.domain.length - 1 },
1649
1665
  set.domain,
1650
1666
  );
1651
- return isDomain && functionValue.range.every((item) => isElementOf(item, set.range));
1667
+ return isDomain && functionValue.range.every((item) => isElementOfSet(item, set.range));
1652
1668
  }
1653
1669
  case "RecordSet": {
1654
1670
  const functionValue = isFunctionValue(element) ? toFiniteFunction(element) : undefined;
@@ -1662,7 +1678,7 @@ export const isElementOf = (element: Value, set: Value): boolean => {
1662
1678
  functionValue.domain.length === set.names.length &&
1663
1679
  set.names.every((name, index) => {
1664
1680
  const position = findIndex(functionValue.domain, name);
1665
- return position !== -1 && isElementOf(functionValue.range[position], set.sets[index]);
1681
+ return position !== -1 && isElementOfSet(functionValue.range[position], set.sets[index]);
1666
1682
  })
1667
1683
  );
1668
1684
  }
@@ -1679,24 +1695,24 @@ export const isElementOf = (element: Value, set: Value): boolean => {
1679
1695
  }
1680
1696
  return (
1681
1697
  elements.length === set.factors.length &&
1682
- elements.every((item, index) => isElementOf(item, set.factors[index]))
1698
+ elements.every((item, index) => isElementOfSet(item, set.factors[index]))
1683
1699
  );
1684
1700
  }
1685
1701
  case "FilteredSet":
1686
- return isElementOf(element, set.base) && set.test(element);
1702
+ return isElementOfSet(element, set.base) && set.test(element);
1687
1703
  case "Union":
1688
1704
  if (!isTlcEnumerable(set.base)) {
1689
1705
  throw new TlaEvaluationError(
1690
1706
  `Attempted to check if:\n ${formatValue(element)}\nis an element of the non-enumerable set:\n ${formatUnion(set)}`,
1691
1707
  );
1692
1708
  }
1693
- return toFiniteSet(set.base).elements.some((inner) => isElementOf(element, inner));
1709
+ return toFiniteSet(set.base).elements.some((inner) => isElementOfSet(element, inner));
1694
1710
  case "SetOperation":
1695
1711
  if (set.operator === "\\cup")
1696
- return isElementOf(element, set.left) || isElementOf(element, set.right);
1712
+ return isElementOfSet(element, set.left) || isElementOfSet(element, set.right);
1697
1713
  return (
1698
- isElementOf(element, set.left) &&
1699
- isElementOf(element, set.right) === (set.operator === "\\cap")
1714
+ isElementOfSet(element, set.left) &&
1715
+ isElementOfSet(element, set.right) === (set.operator === "\\cap")
1700
1716
  );
1701
1717
  case "SequenceSet": {
1702
1718
  const elements = getSequenceElements(element);