tla-ts 0.0.3-dev.7ca58b6 → 0.0.3-dev.852fbc8

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
@@ -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[]) => {
@@ -1224,9 +1227,7 @@ const enumerateSet = (value: Value): Value[] => {
1224
1227
  if (isOverriddenSet(value.base)) getCardinality(value.base);
1225
1228
  const base = toFiniteSet(value.base).elements;
1226
1229
  if (base.length > MAXIMUM_ENUMERATED_SUBSET_BASE) {
1227
- throw new TlaEvaluationError(
1228
- `SUBSET of a set with ${base.length} elements is too large to enumerate`,
1229
- );
1230
+ throw new TlaEvaluationError(describeOverflow(value));
1230
1231
  }
1231
1232
  return getSortedSubsets(base);
1232
1233
  }
@@ -1340,7 +1341,10 @@ export const toFiniteSet = (value: Value): SetValue => {
1340
1341
  const cached = typeof value === "object" ? finiteSets.get(value) : undefined;
1341
1342
  if (cached) return cached;
1342
1343
  const set =
1343
- (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));
1344
1348
  if (typeof value === "object") finiteSets.set(value, set);
1345
1349
  return set;
1346
1350
  };
@@ -1599,6 +1603,12 @@ const describeNonSetMembership = (element: Value, value: Value) => {
1599
1603
  return `${prefix} function ${formatValue(value)}`;
1600
1604
  };
1601
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
+
1602
1612
  export const isElementOf = (element: Value, set: Value): boolean => {
1603
1613
  if (!isSetValue(set)) throw new TlaEvaluationError(describeNonSetMembership(element, set));
1604
1614
  switch (set.kind) {
@@ -1607,10 +1617,10 @@ export const isElementOf = (element: Value, set: Value): boolean => {
1607
1617
  return findIndex(set.elements, element) !== -1;
1608
1618
  } catch (error) {
1609
1619
  if (!(error instanceof TlaEvaluationError)) throw error;
1610
- return isElementOfLikeTlc(element, set.elements);
1620
+ return withSource(set, () => isElementOfLikeTlc(element, set.elements));
1611
1621
  }
1612
1622
  case "MixedSet":
1613
- return isElementOfLikeTlc(element, set.elements);
1623
+ return withSource(set, () => isElementOfLikeTlc(element, set.elements));
1614
1624
  case "Interval":
1615
1625
  if (typeof element === "number") return element >= set.low && element <= set.high;
1616
1626
  if (set.low <= set.high && !isModelValue(element)) {
@@ -1632,12 +1642,18 @@ export const isElementOf = (element: Value, set: Value): boolean => {
1632
1642
  return set.name !== "Nat" || (typeof element === "number" && element >= 0);
1633
1643
  }
1634
1644
  case "Subset":
1635
- if (!isSetValue(element)) throw new TlaEvaluationError(describeMembership(element, set));
1636
- 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));
1637
1651
  case "FunctionSet": {
1638
1652
  if (!isFunctionValue(element)) {
1639
1653
  if (isModelValue(element)) return false;
1640
- throw new TlaEvaluationError(describeMembership(element, set));
1654
+ throw new TlaEvaluationError(
1655
+ `Attempted to check if \n${formatValue(element)}\nwhich is not a TLC function value, is in the set of functions:\n${prettyPrint(formatValue(set))}`,
1656
+ );
1641
1657
  }
1642
1658
  const functionValue = toFiniteFunction(element);
1643
1659
  const low = getIntervalLow(functionValue.domain);
@@ -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);
@@ -1752,10 +1768,61 @@ export const markLambdaFunction = <FunctionLike extends FunctionValue | LazyFunc
1752
1768
  return functionValue;
1753
1769
  };
1754
1770
 
1755
- const describeMissingArgument = (functionValue: Value, argument: Value) =>
1756
- typeof functionValue === "object" && lambdaFunctions.has(functionValue)
1757
- ? `In applying the function\n${prettyPrint(formatValue(functionValue))},\nthe first argument is:\n${prettyPrint(formatValue(argument))}\nwhich is not in its domain.\n`
1758
- : `Attempted to apply function ${formatValue(functionValue)} to ${formatValue(argument)}, which is not in its domain`;
1771
+ // TLC applies a tuple, a record and a function over an interval with their own classes, and each
1772
+ // reports a missing argument in its own words.
1773
+ const getFunctionClass = (functionValue: FunctionValue) => {
1774
+ const domain = functionValue.domain;
1775
+ if (domain.length === 0) return lambdaFunctions.has(functionValue) ? "FcnRcdValue" : "TupleValue";
1776
+ if (domain === intervalDomains[domain.length]) return "TupleValue";
1777
+ if (intervalArrays.has(domain)) return "IntervalFcnRcdValue";
1778
+ return domain.every((key) => typeof key === "string") ? "RecordValue" : "FcnRcdValue";
1779
+ };
1780
+
1781
+ const describeNotInDomain = (functionValue: FunctionValue, argument: Value) =>
1782
+ `Attempted to apply function:\n${prettyPrint(formatValue(functionValue))}\nto argument ${prettyPrint(formatValue(argument))}, which is not in the domain of the function.`;
1783
+
1784
+ const describeNonIntegerArgument = (argument: Value) =>
1785
+ `Attempted to apply function with integer domain to the non-integer argument ${prettyPrint(formatValue(argument))}`;
1786
+
1787
+ const describeMissingArgument = (
1788
+ functionValue: FunctionValue | LazyFunctionValue,
1789
+ argument: Value,
1790
+ ) => {
1791
+ if (lambdaFunctions.has(functionValue) || functionValue.kind === "LazyFunction")
1792
+ return `In applying the function\n${prettyPrint(formatValue(functionValue))},\nthe first argument is:\n${prettyPrint(formatValue(argument))}\nwhich is not in its domain.\n`;
1793
+ const functionClass = getFunctionClass(functionValue);
1794
+ if (functionClass === "TupleValue")
1795
+ return typeof argument === "number"
1796
+ ? `Attempted to access index ${argument} of tuple\n${prettyPrint(formatValue(functionValue))}\nwhich is out of bounds.`
1797
+ : `Attempted to access tuple at a non integral index: ${prettyPrint(formatValue(argument))}`;
1798
+ if (functionClass === "RecordValue")
1799
+ return typeof argument === "string"
1800
+ ? `Attempted to access nonexistent field '${argument}' of record\n${prettyPrint(formatValue(functionValue))}`
1801
+ : `Attempted to access record by a non-string argument: ${prettyPrint(formatValue(argument))}`;
1802
+ return functionClass === "IntervalFcnRcdValue" && typeof argument !== "number"
1803
+ ? describeNonIntegerArgument(argument)
1804
+ : describeNotInDomain(functionValue, argument);
1805
+ };
1806
+
1807
+ // TLC selects a field of a value that isn't a record from the function the value converts to,
1808
+ // which keeps a tuple's domain as an interval.
1809
+ export const describeMissingField = (functionValue: FunctionValue, field: string) =>
1810
+ getFunctionClass(functionValue) === "FcnRcdValue"
1811
+ ? describeNotInDomain(functionValue, field)
1812
+ : describeNonIntegerArgument(field);
1813
+
1814
+ export const isRecordValue = (functionValue: FunctionValue) =>
1815
+ getFunctionClass(functionValue) === "RecordValue";
1816
+
1817
+ // TLC checks the type of an argument to a tuple or a record before it looks the argument up, so
1818
+ // it never compares the argument with a key of another type.
1819
+ const isForeignKey = (domain: Value[], argument: Value) => {
1820
+ const first = domain[0];
1821
+ return typeof first === "number"
1822
+ ? typeof argument !== "number" &&
1823
+ (domain === intervalDomains[domain.length] || intervalArrays.has(domain))
1824
+ : typeof first === "string" && typeof argument !== "string";
1825
+ };
1759
1826
 
1760
1827
  const findKeyIndex = (domain: Value[], argument: Value) => {
1761
1828
  const first = domain[0];
@@ -1774,6 +1841,7 @@ export const applyFunction = (
1774
1841
  functionValue: Value,
1775
1842
  argument: Value,
1776
1843
  describeMissing?: (functionValue: FunctionValue) => string,
1844
+ describeNonFunction?: (value: Value) => string,
1777
1845
  ): Value => {
1778
1846
  if (typeof functionValue === "object" && functionValue.kind === "LazyFunction") {
1779
1847
  if (!isElementOf(argument, functionValue.domain)) {
@@ -1787,10 +1855,13 @@ export const applyFunction = (
1787
1855
  }
1788
1856
  if (typeof functionValue !== "object" || functionValue.kind !== "Function") {
1789
1857
  throw new TlaEvaluationError(
1790
- `Attempted to apply ${formatValue(functionValue)}, which is not a function`,
1858
+ describeNonFunction?.(functionValue) ??
1859
+ `Attempted to apply ${formatValue(functionValue)}, which is not a function`,
1791
1860
  );
1792
1861
  }
1793
- const position = findKeyIndex(functionValue.domain, argument);
1862
+ const position = isForeignKey(functionValue.domain, argument)
1863
+ ? -1
1864
+ : findKeyIndex(functionValue.domain, argument);
1794
1865
  if (position === -1) {
1795
1866
  throw new TlaEvaluationError(
1796
1867
  describeMissing?.(functionValue) ?? describeMissingArgument(functionValue, argument),