tla-ts 0.0.3-dev.bcfa494 → 0.0.3-dev.bfd303f

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/tableau.ts CHANGED
@@ -289,7 +289,7 @@ const toDisjunctiveNormalForm = (expression: LiveExpression): LiveExpression =>
289
289
  total.value *= normal.operands.length;
290
290
  if (total.value > MAXIMUM_DISJUNCTS) {
291
291
  throw new TlaEvaluationError(
292
- "TLC cannot handle the temporal formula because it exceeds the maximum supported size in disjunctive normal form",
292
+ "TLC cannot handle the temporal formula because it exceeds the maximum supported size in disjunctive normal form.",
293
293
  );
294
294
  }
295
295
  }
package/src/tlc-module.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { TlaModuleError } from "./errors";
1
+ import { TlaEvaluationError, TlaModuleError } from "./errors";
2
2
  import { EXIT_STATUS } from "./exit-codes";
3
3
  import type { Builtin } from "./evaluate";
4
4
  import { prettyPrint } from "./pretty-print";
@@ -48,7 +48,7 @@ const getRuntimeValue = (
48
48
  state: (Value | undefined)[] | undefined,
49
49
  ) => {
50
50
  if (!runtime.isStarted && CHECKER_KEYS.has(key)) {
51
- throw new TlaModuleError(`TLCGet(${key}) was undefined.`);
51
+ throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
52
52
  }
53
53
  const duration = Math.floor((Date.now() - runtime.startedAt) / 1000);
54
54
  switch (key) {
@@ -104,11 +104,11 @@ const getRuntimeValue = (
104
104
  }
105
105
  if (key.startsWith(NAMED_REGISTER_PREFIX)) {
106
106
  const value = getNamedRegisters(runtime).get(key);
107
- if (value === undefined) throw new TlaModuleError(`TLCGet(${key}) was undefined.`);
107
+ if (value === undefined) throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
108
108
  return value;
109
109
  }
110
110
  if (key.startsWith("-D")) return rankString(runtime.properties.get(key.slice(2)) ?? key);
111
- throw new TlaModuleError(`TLCGet(${key}) was undefined.`);
111
+ throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
112
112
  };
113
113
 
114
114
  const getPermutations = (elements: Value[]): Value[][] =>
@@ -197,11 +197,11 @@ export const createTlcOperators = (
197
197
  const key = getValue(args, 0, "TLCGet");
198
198
  if (typeof key === "number" && key >= 0) {
199
199
  const value = runtime.registers.get(key);
200
- if (value === undefined) throw new TlaModuleError(`TLCGet(${key}) was undefined.`);
200
+ if (value === undefined) throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
201
201
  return value;
202
202
  }
203
203
  if (typeof key !== "string") {
204
- throw new TlaModuleError(
204
+ throw new TlaEvaluationError(
205
205
  `The argument of TLCGet should be a nonnegative integer, but instead it is:\n${formatValue(key)}`,
206
206
  );
207
207
  }
@@ -1013,6 +1013,10 @@ export const TLC_JAVA_METHODS = new Map([
1013
1013
  "RandomElement",
1014
1014
  "public static tlc2.value.impl.Value tlc2.module.TLC.RandomElement(tlc2.value.impl.Value)",
1015
1015
  ],
1016
+ [
1017
+ "TLCGet",
1018
+ "public static tlc2.value.impl.Value tlc2.module.TLCGetSet.TLCGetEval(tlc2.tool.impl.Tool,tla2sany.semantic.ExprOrOpArgNode[],tlc2.util.Context,tlc2.tool.TLCState,tlc2.tool.TLCState,int,tlc2.tool.coverage.CostModel)",
1019
+ ],
1016
1020
  [
1017
1021
  "TLCEval",
1018
1022
  "public static tlc2.value.impl.Value tlc2.module.TLCEval.tlcEval(tlc2.tool.impl.Tool,tla2sany.semantic.ExprOrOpArgNode[],tlc2.util.Context,tlc2.tool.TLCState,tlc2.tool.TLCState,int,tlc2.tool.coverage.CostModel)",
package/src/values.ts CHANGED
@@ -1224,9 +1224,7 @@ const enumerateSet = (value: Value): Value[] => {
1224
1224
  if (isOverriddenSet(value.base)) getCardinality(value.base);
1225
1225
  const base = toFiniteSet(value.base).elements;
1226
1226
  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
- );
1227
+ throw new TlaEvaluationError(describeOverflow(value));
1230
1228
  }
1231
1229
  return getSortedSubsets(base);
1232
1230
  }
@@ -1752,10 +1750,61 @@ export const markLambdaFunction = <FunctionLike extends FunctionValue | LazyFunc
1752
1750
  return functionValue;
1753
1751
  };
1754
1752
 
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`;
1753
+ // TLC applies a tuple, a record and a function over an interval with their own classes, and each
1754
+ // reports a missing argument in its own words.
1755
+ const getFunctionClass = (functionValue: FunctionValue) => {
1756
+ const domain = functionValue.domain;
1757
+ if (domain.length === 0) return lambdaFunctions.has(functionValue) ? "FcnRcdValue" : "TupleValue";
1758
+ if (domain === intervalDomains[domain.length]) return "TupleValue";
1759
+ if (intervalArrays.has(domain)) return "IntervalFcnRcdValue";
1760
+ return domain.every((key) => typeof key === "string") ? "RecordValue" : "FcnRcdValue";
1761
+ };
1762
+
1763
+ const describeNotInDomain = (functionValue: FunctionValue, argument: Value) =>
1764
+ `Attempted to apply function:\n${prettyPrint(formatValue(functionValue))}\nto argument ${prettyPrint(formatValue(argument))}, which is not in the domain of the function.`;
1765
+
1766
+ const describeNonIntegerArgument = (argument: Value) =>
1767
+ `Attempted to apply function with integer domain to the non-integer argument ${prettyPrint(formatValue(argument))}`;
1768
+
1769
+ const describeMissingArgument = (
1770
+ functionValue: FunctionValue | LazyFunctionValue,
1771
+ argument: Value,
1772
+ ) => {
1773
+ if (lambdaFunctions.has(functionValue) || functionValue.kind === "LazyFunction")
1774
+ return `In applying the function\n${prettyPrint(formatValue(functionValue))},\nthe first argument is:\n${prettyPrint(formatValue(argument))}\nwhich is not in its domain.\n`;
1775
+ const functionClass = getFunctionClass(functionValue);
1776
+ if (functionClass === "TupleValue")
1777
+ return typeof argument === "number"
1778
+ ? `Attempted to access index ${argument} of tuple\n${prettyPrint(formatValue(functionValue))}\nwhich is out of bounds.`
1779
+ : `Attempted to access tuple at a non integral index: ${prettyPrint(formatValue(argument))}`;
1780
+ if (functionClass === "RecordValue")
1781
+ return typeof argument === "string"
1782
+ ? `Attempted to access nonexistent field '${argument}' of record\n${prettyPrint(formatValue(functionValue))}`
1783
+ : `Attempted to access record by a non-string argument: ${prettyPrint(formatValue(argument))}`;
1784
+ return functionClass === "IntervalFcnRcdValue" && typeof argument !== "number"
1785
+ ? describeNonIntegerArgument(argument)
1786
+ : describeNotInDomain(functionValue, argument);
1787
+ };
1788
+
1789
+ // TLC selects a field of a value that isn't a record from the function the value converts to,
1790
+ // which keeps a tuple's domain as an interval.
1791
+ export const describeMissingField = (functionValue: FunctionValue, field: string) =>
1792
+ getFunctionClass(functionValue) === "FcnRcdValue"
1793
+ ? describeNotInDomain(functionValue, field)
1794
+ : describeNonIntegerArgument(field);
1795
+
1796
+ export const isRecordValue = (functionValue: FunctionValue) =>
1797
+ getFunctionClass(functionValue) === "RecordValue";
1798
+
1799
+ // TLC checks the type of an argument to a tuple or a record before it looks the argument up, so
1800
+ // it never compares the argument with a key of another type.
1801
+ const isForeignKey = (domain: Value[], argument: Value) => {
1802
+ const first = domain[0];
1803
+ return typeof first === "number"
1804
+ ? typeof argument !== "number" &&
1805
+ (domain === intervalDomains[domain.length] || intervalArrays.has(domain))
1806
+ : typeof first === "string" && typeof argument !== "string";
1807
+ };
1759
1808
 
1760
1809
  const findKeyIndex = (domain: Value[], argument: Value) => {
1761
1810
  const first = domain[0];
@@ -1774,6 +1823,7 @@ export const applyFunction = (
1774
1823
  functionValue: Value,
1775
1824
  argument: Value,
1776
1825
  describeMissing?: (functionValue: FunctionValue) => string,
1826
+ describeNonFunction?: (value: Value) => string,
1777
1827
  ): Value => {
1778
1828
  if (typeof functionValue === "object" && functionValue.kind === "LazyFunction") {
1779
1829
  if (!isElementOf(argument, functionValue.domain)) {
@@ -1787,10 +1837,13 @@ export const applyFunction = (
1787
1837
  }
1788
1838
  if (typeof functionValue !== "object" || functionValue.kind !== "Function") {
1789
1839
  throw new TlaEvaluationError(
1790
- `Attempted to apply ${formatValue(functionValue)}, which is not a function`,
1840
+ describeNonFunction?.(functionValue) ??
1841
+ `Attempted to apply ${formatValue(functionValue)}, which is not a function`,
1791
1842
  );
1792
1843
  }
1793
- const position = findKeyIndex(functionValue.domain, argument);
1844
+ const position = isForeignKey(functionValue.domain, argument)
1845
+ ? -1
1846
+ : findKeyIndex(functionValue.domain, argument);
1794
1847
  if (position === -1) {
1795
1848
  throw new TlaEvaluationError(
1796
1849
  describeMissing?.(functionValue) ?? describeMissingArgument(functionValue, argument),