tla-ts 0.0.3-dev.252cf06 → 0.0.3-dev.2d84a88

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.
Files changed (49) hide show
  1. package/README.md +5 -1
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +53 -22
  4. package/dist/index.d.ts +53 -22
  5. package/dist/index.js +1 -1
  6. package/dist/parallel-worker.cjs +1 -1
  7. package/dist/parallel-worker.js +2 -2
  8. package/dist/parallel.cjs +1 -1
  9. package/dist/parallel.js +2 -2
  10. package/dist/run-cli.cjs +19 -17
  11. package/dist/run-cli.js +20 -18
  12. package/dist/transfer.cjs +32 -15
  13. package/dist/transfer.js +32 -15
  14. package/package.json +1 -1
  15. package/src/ast.ts +3 -0
  16. package/src/builtins.ts +9 -3
  17. package/src/check.ts +465 -97
  18. package/src/cli-options.ts +11 -5
  19. package/src/codegen.ts +14 -19
  20. package/src/config.ts +224 -170
  21. package/src/environment.ts +111 -19
  22. package/src/errors.ts +82 -3
  23. package/src/evaluate.ts +202 -47
  24. package/src/exit-codes.ts +21 -14
  25. package/src/fingerprints.ts +2 -1
  26. package/src/instances.ts +4 -4
  27. package/src/levels.ts +55 -27
  28. package/src/live-check.ts +132 -70
  29. package/src/liveness.ts +117 -22
  30. package/src/parallel-worker.ts +2 -2
  31. package/src/parallel.ts +5 -2
  32. package/src/parser.ts +21 -10
  33. package/src/resolve.ts +106 -29
  34. package/src/run-cli.ts +103 -18
  35. package/src/runtime.ts +2 -0
  36. package/src/spec-description.ts +12 -6
  37. package/src/standard-modules.ts +52 -43
  38. package/src/state-graph.ts +157 -0
  39. package/src/successors.ts +169 -33
  40. package/src/symmetry.ts +16 -6
  41. package/src/tableau.ts +51 -24
  42. package/src/tlc-ext.ts +27 -2
  43. package/src/tlc-module.ts +14 -33
  44. package/src/tlc-modules.ts +4 -0
  45. package/src/tlc-order.ts +8 -4
  46. package/src/tlc-random.ts +9 -6
  47. package/src/tlc-summary.ts +20 -5
  48. package/src/value-sources.ts +29 -0
  49. package/src/values.ts +236 -55
package/src/evaluate.ts CHANGED
@@ -10,16 +10,16 @@ import {
10
10
  type NumberExpression,
11
11
  type OperatorParam,
12
12
  type RecordField,
13
- type SourceRange,
14
13
  } from "./ast";
15
14
  import { getKindString } from "./builtins";
16
- import { TlaEvaluationError } from "./errors";
17
- import { EXIT_STATUS, setExitCode } from "./exit-codes";
15
+ import { formatRange, TlaEvaluationError, TlaUnassignedError } from "./errors";
16
+ import { EXIT_STATUS, setExitCode, setOwnExitCode } from "./exit-codes";
18
17
  import { generateEvaluator } from "./codegen";
19
18
  import type { CheckerRuntime } from "./runtime";
20
19
  import { getReadVariables } from "./dependencies";
21
20
  import { contextualApplications, mapChildren, substitutedExpressions } from "./instances";
22
21
  import { isEnabled } from "./successors";
22
+ import { getFingerprintTrace, valueSources, withSource } from "./value-sources";
23
23
  import {
24
24
  applyFunction,
25
25
  combineHash,
@@ -30,23 +30,28 @@ import {
30
30
  createSortedSet,
31
31
  createWrittenRecord,
32
32
  createTuple,
33
+ describeMissingField,
33
34
  formatValue,
34
35
  getDomain,
35
36
  getFunctionDomain,
36
37
  getIntervalElements,
38
+ getLazySubsets,
37
39
  getSequenceElements,
38
40
  getValueHash,
39
- isEnumerable,
40
41
  isFunctionValue,
41
42
  isOverriddenSet,
43
+ isRecordValue,
42
44
  isSameValue,
43
45
  markLambdaFunction,
46
+ isReducible,
44
47
  isSetValue,
45
48
  MAXIMUM_INTEGER,
46
49
  MINIMUM_INTEGER,
47
50
  toFiniteSet,
48
51
  updateFunction,
49
52
  valuesEqual,
53
+ type FilteredSetValue,
54
+ type FunctionValue,
50
55
  type LazyFunctionSource,
51
56
  type LazyFunctionValue,
52
57
  type Value,
@@ -114,6 +119,7 @@ export interface Environment {
114
119
  definitionValues: Map<Definition, Value>;
115
120
  constantLevels: Map<Definition, boolean>;
116
121
  evaluators: WeakMap<Expression, Evaluator>;
122
+ trace?: Expression[];
117
123
  }
118
124
 
119
125
  export interface Context {
@@ -143,9 +149,6 @@ const EXCEPT_AT = "@";
143
149
  export const describeLocation = (expression: Expression) =>
144
150
  `line ${expression.location.line}, column ${expression.location.column}`;
145
151
 
146
- export const formatRange = (range: SourceRange) =>
147
- `line ${range.start.line}, col ${range.start.column} to line ${range.end.line}, col ${range.end.column} of module ${range.module}`;
148
-
149
152
  export const describeRange = (expression: Expression) => {
150
153
  const range = expression.location.range;
151
154
  return range ? formatRange(range) : describeLocation(expression);
@@ -182,8 +185,17 @@ export const getConjunctError = (
182
185
  };
183
186
 
184
187
  export const getFieldError =
185
- (expression: Expression & { kind: "FieldAccess" }) => (record: Value) =>
186
- `Attempted to select nonexistent field "${expression.field}" from the record\n${formatValue(record)}\n${describeRange(expression)}`;
188
+ (expression: Expression & { kind: "FieldAccess" }) => (record: FunctionValue) =>
189
+ isRecordValue(record)
190
+ ? `Attempted to select nonexistent field "${expression.field}" from the record\n${formatValue(record)}\n${describeRange(expression)}`
191
+ : describeMissingField(record, expression.field);
192
+
193
+ export const getNonFunctionError = (expression: Expression) => (value: Value) =>
194
+ `A non-function (${getKindString(value)}) was applied as a function.\n${describeRange(expression)}`;
195
+
196
+ export const getNonRecordError =
197
+ (expression: Expression & { kind: "FieldAccess" }) => (value: Value) =>
198
+ `Attempted to select field "${expression.field}" from a non-record value ${formatValue(value)}\n${describeRange(expression)}`;
187
199
 
188
200
  export const getDomainOperator = (expression: Expression) => (value: Value) => {
189
201
  if (!isFunctionValue(value)) {
@@ -331,9 +343,8 @@ export const getEnumeratedElements = (set: Value, expression: Expression) => {
331
343
  : `TLC encountered the non-enumerable quantifier bound\n${text}\n${describeRange(expression)}\n${explanation}`,
332
344
  );
333
345
  }
334
- return typeof set === "object" && set.kind === "Interval"
335
- ? getIntervalElements(set)
336
- : toFiniteSet(set).elements;
346
+ if (typeof set === "object" && set.kind === "Interval") return getIntervalElements(set);
347
+ return getLazySubsets(set) ?? toFiniteSet(set).elements;
337
348
  };
338
349
 
339
350
  export const forEachBinding = (
@@ -771,11 +782,7 @@ const evaluateLocalIdentifier = (name: string, expression: Expression, context:
771
782
  const global = findGlobalName(name, context.environment);
772
783
  if (global?.kind === "Variable") {
773
784
  const value = context.state?.[global.index];
774
- if (value === undefined) {
775
- throw new TlaEvaluationError(
776
- `The variable ${name} is not assigned at ${describeLocation(expression)}`,
777
- );
778
- }
785
+ if (value === undefined) throw createUnassignedError(name, expression);
779
786
  return value;
780
787
  }
781
788
  if (global?.kind === "Value") return global.value;
@@ -1015,23 +1022,88 @@ const isStateCached = (expression: Expression, environment: Environment) =>
1015
1022
  getReadVariables(expression, environment) !== undefined &&
1016
1023
  !hasFreeLocals(expression, NO_BOUND_NAMES);
1017
1024
 
1025
+ // CallStackTool also evaluates the predicate of a lazy set filter it returns, even one that TLC
1026
+ // made before the replay.
1027
+ const filteredSetRetracers = new WeakMap<
1028
+ FilteredSetValue,
1029
+ (environment: Environment) => FilteredSetValue
1030
+ >();
1031
+
1032
+ const SOURCED_KINDS = new Set<Expression["kind"]>([
1033
+ "SetEnumeration",
1034
+ "SetMap",
1035
+ "SetFilter",
1036
+ "Tuple",
1037
+ "Record",
1038
+ "RecordSet",
1039
+ "FunctionSet",
1040
+ "Product",
1041
+ ]);
1042
+
1043
+ const SOURCED_OPERATORS = new Set(["SUBSET", "UNION", "DOMAIN", "\\cup", "\\cap", "\\"]);
1044
+
1045
+ // CallStackTool gives a set, tuple or record it builds the expression as its source, but not a lazy
1046
+ // set filter, which it returns as a copy.
1047
+ const isSourced = (expression: Expression, environment: Environment) =>
1048
+ SOURCED_KINDS.has(expression.kind) ||
1049
+ (expression.kind === "Apply" &&
1050
+ SOURCED_OPERATORS.has(expression.operator) &&
1051
+ !isLocallyResolved(expression) &&
1052
+ resolveGlobalOperator(expression.operator, environment)?.kind === "Builtin");
1053
+
1054
+ // When an evaluation fails, CallStackTool freezes the stack at the innermost expression and pushes
1055
+ // the source of each value whose method failed.
1056
+ const evaluateTraced = (
1057
+ evaluator: Evaluator,
1058
+ context: Context,
1059
+ trace: Expression[],
1060
+ depth: number,
1061
+ ) => {
1062
+ try {
1063
+ return evaluator(context);
1064
+ } catch (error) {
1065
+ if (trace.length === depth + 1) trace.push(...(getFingerprintTrace(error) ?? []));
1066
+ throw error;
1067
+ }
1068
+ };
1069
+
1018
1070
  export const getEvaluator = (expression: Expression, environment: Environment): Evaluator => {
1019
1071
  const cached = environment.evaluators.get(expression);
1020
1072
  if (cached) return cached;
1021
1073
  const compiled = compile(expression, environment);
1022
- const evaluator =
1074
+ const cachedEvaluator =
1023
1075
  isWorthCaching(expression) && isClosedConstant(expression, environment)
1024
1076
  ? cacheFirstValue(compiled)
1025
1077
  : isStateCached(expression, environment)
1026
1078
  ? cacheStateValue(compiled)
1027
1079
  : compiled;
1080
+ const trace = environment.trace;
1081
+ const isSourcedExpression = trace !== undefined && isSourced(expression, environment);
1082
+ // TLC's CallStackTool pushes each expression it evaluates and keeps the stack when one fails.
1083
+ const evaluator: Evaluator = trace
1084
+ ? (context) => {
1085
+ const depth = trace.length;
1086
+ trace.push(expression);
1087
+ const value = evaluateTraced(cachedEvaluator, context, trace, depth);
1088
+ trace.length = depth;
1089
+ if (isSourcedExpression && typeof value === "object" && value.kind !== "FilteredSet") {
1090
+ valueSources.current?.set(value, expression);
1091
+ }
1092
+ const retrace =
1093
+ typeof value === "object" && value.kind === "FilteredSet"
1094
+ ? filteredSetRetracers.get(value)
1095
+ : undefined;
1096
+ return retrace ? retrace(environment) : value;
1097
+ }
1098
+ : cachedEvaluator;
1028
1099
  environment.evaluators.set(expression, evaluator);
1029
1100
  return evaluator;
1030
1101
  };
1031
1102
 
1032
1103
  const entryEvaluators = new WeakMap<Expression, Evaluator>();
1033
1104
 
1034
- const getEntryEvaluator = (expression: Expression, environment: Environment) => {
1105
+ export const getEntryEvaluator = (expression: Expression, environment: Environment) => {
1106
+ if (environment.trace) return getEvaluator(expression, environment);
1035
1107
  const cached = entryEvaluators.get(expression);
1036
1108
  if (cached) return cached;
1037
1109
  const evaluator =
@@ -1055,11 +1127,7 @@ const compileIdentifier = (
1055
1127
  const index = global.index;
1056
1128
  return (context) => {
1057
1129
  const value = context.state?.[index];
1058
- if (value === undefined) {
1059
- throw new TlaEvaluationError(
1060
- `The variable ${name} is not assigned at ${describeLocation(expression)}`,
1061
- );
1062
- }
1130
+ if (value === undefined) throw createUnassignedError(name, expression);
1063
1131
  return value;
1064
1132
  };
1065
1133
  }
@@ -1144,11 +1212,32 @@ const getLazyArguments = (args: Expression[], context: Context): OperatorValue[]
1144
1212
  expression: arg,
1145
1213
  }));
1146
1214
 
1215
+ const RECEIVING_OPERATORS = new Set(["=", "#", "\\in", "\\notin", "\\cup", "\\cap", "\\"]);
1216
+
1217
+ // The argument whose method TLC calls to apply a built-in operator.
1218
+ const getReceiver = (name: string, left: Value, right: Value) =>
1219
+ name === "\\in" ||
1220
+ name === "\\notin" ||
1221
+ ((name === "\\cup" || name === "\\cap") && !isReducible(left))
1222
+ ? right
1223
+ : left;
1224
+
1147
1225
  const compileBuiltinCall = (
1148
1226
  builtin: Builtin,
1149
1227
  args: Expression[],
1150
1228
  environment: Environment,
1151
1229
  ): Evaluator => {
1230
+ if (environment.trace && RECEIVING_OPERATORS.has(builtin.name) && args.length === 2) {
1231
+ const evaluateLeft = getEvaluator(args[0], environment);
1232
+ const evaluateRight = getEvaluator(args[1], environment);
1233
+ return (context) => {
1234
+ const left = evaluateLeft(context);
1235
+ const right = evaluateRight(context);
1236
+ return withSource(getReceiver(builtin.name, left, right), () =>
1237
+ builtin.evaluate([left, right], context),
1238
+ );
1239
+ };
1240
+ }
1152
1241
  if (builtin.isLazy) {
1153
1242
  return (context) => builtin.evaluate(getLazyArguments(args, context), context);
1154
1243
  }
@@ -1308,6 +1397,13 @@ const compileApply = (
1308
1397
  if (global?.kind === "Builtin" && global.name === "DOMAIN") {
1309
1398
  const operand = getEvaluator(args[0], environment);
1310
1399
  const domain = getDomainOperator(expression);
1400
+ // TLC builds a new interval for the domain of a tuple, so the source it gives one stays its own.
1401
+ if (environment.trace) {
1402
+ return (context) => {
1403
+ const value = domain(operand(context));
1404
+ return typeof value === "object" && value.kind === "Interval" ? { ...value } : value;
1405
+ };
1406
+ }
1311
1407
  return (context) => domain(operand(context));
1312
1408
  }
1313
1409
  // TLC sorts the value of a constant TLCEval argument only where no context binds a name.
@@ -1323,6 +1419,13 @@ const compileApply = (
1323
1419
  const left = getEvaluator(args[0], environment);
1324
1420
  const right = getEvaluator(args[1], environment);
1325
1421
  const subset = getSubsetOperator(expression, global);
1422
+ if (environment.trace) {
1423
+ return (context) => {
1424
+ const subsetValue = left(context);
1425
+ const superset = right(context);
1426
+ return withSource(subsetValue, () => subset(subsetValue, superset));
1427
+ };
1428
+ }
1326
1429
  return (context) => subset(left(context), right(context));
1327
1430
  }
1328
1431
  if (global?.kind === "Builtin") return compileBuiltinCall(global, args, environment);
@@ -1333,6 +1436,11 @@ const compileApply = (
1333
1436
  if (definition?.kind === "Function") {
1334
1437
  throw new TlaEvaluationError(`${expression.operator} is a function; apply it with brackets`);
1335
1438
  }
1439
+ if (expression.operator === "\\cdot") {
1440
+ throw new TlaEvaluationError(
1441
+ "The current version of TLC does not support action composition. An incomplete implementation can be enabled via the tlc2.tool.impl.Tool.cdot=true java property.",
1442
+ );
1443
+ }
1336
1444
  throw new TlaEvaluationError(
1337
1445
  `Unknown operator ${expression.operator} at ${describeLocation(expression)}`,
1338
1446
  );
@@ -1428,7 +1536,13 @@ const compileFunctionConstruction = (
1428
1536
  const set = getEvaluator(bound.set, environment);
1429
1537
  return (context) => {
1430
1538
  const domain = isSingleName ? set(context) : getBoundsDomain(bounds, context);
1431
- if (expression.isRecursive || isKeptLazy || !isEnumerable(domain)) {
1539
+ // TLC builds the function only when it reduces every domain, and evaluates the body lazily
1540
+ // otherwise.
1541
+ const domains =
1542
+ typeof domain === "object" && domain.kind === "ProductSet" && bounds.length > 1
1543
+ ? domain.factors
1544
+ : [domain];
1545
+ if (expression.isRecursive || isKeptLazy || !domains.every(isReducible)) {
1432
1546
  const functionValue = createLazyFunction(
1433
1547
  domain,
1434
1548
  (argument) => body(withScope(context, bindArgument(bounds, context.scope, argument))),
@@ -1517,16 +1631,21 @@ const compileSetComprehension = (
1517
1631
  const pattern = expression.bound.pattern;
1518
1632
  return (context) => {
1519
1633
  const base = set(context);
1520
- const test = (element: Value) =>
1634
+ const getTest = (evaluatePredicate: Evaluator) => (element: Value) =>
1521
1635
  expectBoolean(
1522
- predicate(withScope(context, bindPattern(context.scope, pattern, element))),
1636
+ evaluatePredicate(withScope(context, bindPattern(context.scope, pattern, element))),
1523
1637
  "a set filter predicate",
1524
1638
  );
1525
- const isReducible =
1526
- typeof base === "object" &&
1527
- (base.kind === "Set" || base.kind === "MixedSet" || base.kind === "Interval");
1528
- if (!isReducible) {
1529
- if (isSetValue(base)) return { kind: "FilteredSet", base, test };
1639
+ const test = getTest(predicate);
1640
+ if (!isReducible(base)) {
1641
+ if (isSetValue(base)) {
1642
+ const filtered: FilteredSetValue = { kind: "FilteredSet", base, test };
1643
+ filteredSetRetracers.set(filtered, (traced) => ({
1644
+ ...filtered,
1645
+ test: getTest(getEvaluator(expression.predicate, traced)),
1646
+ }));
1647
+ return filtered;
1648
+ }
1530
1649
  throw new TlaEvaluationError(
1531
1650
  `TLC encountered a non-enumerable quantifier bound ${formatValue(base)} at ${describeLocation(expression.bound.set)}`,
1532
1651
  );
@@ -1571,11 +1690,30 @@ const compileSetComprehension = (
1571
1690
  };
1572
1691
  };
1573
1692
 
1574
- const compileRecord = (recordFields: RecordField[], environment: Environment): Evaluator => {
1575
- const fields = [...recordFields].sort((left, right) => compareValues(left.name, right.name));
1576
- const hasDuplicates = fields.some(
1577
- (field, index) => index > 0 && fields[index - 1].name === field.name,
1693
+ // TLC orders a record's fields by name.
1694
+ export const getSortedFields = (fields: RecordField[]) =>
1695
+ [...fields].sort((left, right) => compareValues(left.name, right.name));
1696
+
1697
+ export const hasDuplicateFields = (sortedFields: RecordField[]) =>
1698
+ sortedFields.some((field, index) => index > 0 && sortedFields[index - 1].name === field.name);
1699
+
1700
+ export const createUnassignedError = (name: string, expression: Expression) =>
1701
+ setOwnExitCode(
1702
+ new TlaUnassignedError(
1703
+ `In evaluation, the identifier ${name} is either undefined or not an operator.\n${describeRange(expression)}`,
1704
+ name,
1705
+ ),
1706
+ EXIT_STATUS.configParseError,
1707
+ );
1708
+
1709
+ export const createPrimeOutsideActionError = (expression: Expression) =>
1710
+ new TlaEvaluationError(
1711
+ `A primed expression appears outside an action at ${describeLocation(expression)}`,
1578
1712
  );
1713
+
1714
+ const compileRecord = (recordFields: RecordField[], environment: Environment): Evaluator => {
1715
+ const fields = getSortedFields(recordFields);
1716
+ const hasDuplicates = hasDuplicateFields(fields);
1579
1717
  const values = fields.map((field) => getEvaluator(field.value, environment));
1580
1718
  if (hasDuplicates) {
1581
1719
  return (context) =>
@@ -1653,11 +1791,7 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
1653
1791
  case "Prime": {
1654
1792
  const inner = child(expression.expression);
1655
1793
  return (context) => {
1656
- if (!context.next) {
1657
- throw new TlaEvaluationError(
1658
- `A primed expression appears outside an action at ${describeLocation(expression)}`,
1659
- );
1660
- }
1794
+ if (!context.next) throw createPrimeOutsideActionError(expression);
1661
1795
  return inner({
1662
1796
  environment: context.environment,
1663
1797
  scope: context.scope,
@@ -1737,9 +1871,7 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
1737
1871
  case "Record":
1738
1872
  return compileRecord(expression.fields, environment);
1739
1873
  case "RecordSet": {
1740
- const fields = [...expression.fields].sort((left, right) =>
1741
- compareValues(left.name, right.name),
1742
- );
1874
+ const fields = getSortedFields(expression.fields);
1743
1875
  const names = fields.map((field) => field.name);
1744
1876
  const sets = fields.map((field) => child(field.value));
1745
1877
  return (context) => ({ kind: "RecordSet", names, sets: sets.map((set) => set(context)) });
@@ -1760,18 +1892,41 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
1760
1892
  keepLazy(expression.function);
1761
1893
  const evaluateFunction = child(expression.function);
1762
1894
  const args = expression.args.map(child);
1895
+ const describeNonFunction = getNonFunctionError(expression);
1896
+ if (args.length === 1 && environment.trace) {
1897
+ const argument = args[0];
1898
+ return (context) => {
1899
+ const functionValue = evaluateFunction(context);
1900
+ const argumentValue = argument(context);
1901
+ const apply = () =>
1902
+ applyFunction(functionValue, argumentValue, undefined, describeNonFunction);
1903
+ return isFunctionValue(functionValue) ? withSource(functionValue, apply) : apply();
1904
+ };
1905
+ }
1763
1906
  if (args.length === 1) {
1764
1907
  const argument = args[0];
1765
- return (context) => applyFunction(evaluateFunction(context), argument(context));
1908
+ return (context) =>
1909
+ applyFunction(
1910
+ evaluateFunction(context),
1911
+ argument(context),
1912
+ undefined,
1913
+ describeNonFunction,
1914
+ );
1766
1915
  }
1767
1916
  return (context) =>
1768
- applyFunction(evaluateFunction(context), createTuple(args.map((arg) => arg(context))));
1917
+ applyFunction(
1918
+ evaluateFunction(context),
1919
+ createTuple(args.map((arg) => arg(context))),
1920
+ undefined,
1921
+ describeNonFunction,
1922
+ );
1769
1923
  }
1770
1924
  case "FieldAccess": {
1771
1925
  const record = child(expression.record);
1772
1926
  const field = expression.field;
1773
1927
  const describeMissing = getFieldError(expression);
1774
- return (context) => applyFunction(record(context), field, describeMissing);
1928
+ const describeNonRecord = getNonRecordError(expression);
1929
+ return (context) => applyFunction(record(context), field, describeMissing, describeNonRecord);
1775
1930
  }
1776
1931
  case "Product": {
1777
1932
  const factors = expression.factors.map(child);
package/src/exit-codes.ts CHANGED
@@ -17,12 +17,6 @@ export const EXIT_STATUS = {
17
17
  } as const;
18
18
 
19
19
  const exitCodes = new WeakMap<Error, number>();
20
- const moduleExitCodes = new WeakMap<Error, number>();
21
- const phase = { isNextState: false };
22
-
23
- export const setNextStatePhase = (isNextState: boolean) => {
24
- phase.isNextState = isNextState;
25
- };
26
20
 
27
21
  // The innermost site that knows TLC's exit status for an error sets it first and wins.
28
22
  export const setExitCode = <Thrown>(error: Thrown, code: number) => {
@@ -30,15 +24,21 @@ export const setExitCode = <Thrown>(error: Thrown, code: number) => {
30
24
  return error;
31
25
  };
32
26
 
33
- // TLC exits with the code of an exception from a standard module's Java code only when the exception
34
- // escapes the computation of next states. In every other phase, the phase decides the code.
35
- export const setModuleExitCode = (error: Error, code: number) => {
36
- if (phase.isNextState) moduleExitCodes.set(error, code);
27
+ export const getExitCode = (error: Error) => exitCodes.get(error);
28
+
29
+ const ownExitCodes = new WeakMap<Error, number>();
30
+
31
+ // The exit status of the error code TLC gives an EvalException, which TLC uses only where it
32
+ // doesn't report the error again as a nested expression.
33
+ export const setOwnExitCode = <Thrown extends Error>(error: Thrown, code: number) => {
34
+ ownExitCodes.set(error, code);
35
+ return error;
37
36
  };
38
37
 
39
- export const getExitCode = (error: Error) => exitCodes.get(error) ?? moduleExitCodes.get(error);
38
+ export const getOwnExitCode = (error: unknown) =>
39
+ error instanceof Error ? ownExitCodes.get(error) : undefined;
40
40
 
41
- // TLC exits with 255 when it fails to process or evaluate a liveness property, in any phase.
41
+ // TLC exits with 255 when it fails to process or evaluate a liveness property.
42
42
  const withErrorExitCode = <Result>(run: () => Result) => {
43
43
  try {
44
44
  return run();
@@ -53,8 +53,15 @@ export const withLivenessExitCode = (
53
53
  const checker = withErrorExitCode(create);
54
54
  return (
55
55
  checker && {
56
- addInitialState: (index, state) =>
57
- withErrorExitCode(() => checker.addInitialState(index, state)),
56
+ // TLC replays the initial states to report an error, but skips the liveness check of a
57
+ // state it has already seen, so it exits with the status of the error's own code.
58
+ addInitialState: (index, state) => {
59
+ try {
60
+ checker.addInitialState(index, state);
61
+ } catch (error) {
62
+ throw setExitCode(error, getOwnExitCode(error) ?? EXIT_STATUS.error);
63
+ }
64
+ },
58
65
  addState: (index, state, successors) =>
59
66
  withErrorExitCode(() => checker.addState(index, state, successors)),
60
67
  findViolation: () => withErrorExitCode(() => checker.findViolation()),
@@ -1,4 +1,4 @@
1
- import { getValueFingerprint, getValueHash, type Value } from "./values";
1
+ import { assertFingerprintable, getValueFingerprint, getValueHash, type Value } from "./values";
2
2
 
3
3
  const EMPTY = 0;
4
4
  const INITIAL_CAPACITY = 1 << 12;
@@ -20,6 +20,7 @@ export const writeStateFingerprint = (state: Value[], fingerprint: Int32Array) =
20
20
  first = Math.imul(first ^ getValueHash(state[index]), 0x01000193);
21
21
  second = Math.imul(second ^ getValueFingerprint(state[index]), 0x5bd1e995);
22
22
  }
23
+ assertFingerprintable(state);
23
24
  fingerprint[0] = mix(first) || 1;
24
25
  fingerprint[1] = mix(second);
25
26
  };
package/src/instances.ts CHANGED
@@ -164,7 +164,7 @@ export const mapChildren = (
164
164
  case "RecordSet":
165
165
  return {
166
166
  ...expression,
167
- fields: expression.fields.map((field) => ({ name: field.name, value: plain(field.value) })),
167
+ fields: expression.fields.map((field) => ({ ...field, value: plain(field.value) })),
168
168
  };
169
169
  case "Except":
170
170
  return {
@@ -219,7 +219,7 @@ interface Instantiation {
219
219
  variables: Set<string>;
220
220
  getDefinition: (key: string) => Definition | undefined;
221
221
  isParameterized: (key: string) => boolean;
222
- addDefinition: (name: string, create: (key: string) => Definition) => string;
222
+ addDefinition: (source: string, name: string, create: (key: string) => Definition) => string;
223
223
  createName: (name: string) => string;
224
224
  }
225
225
 
@@ -640,7 +640,7 @@ const copyDefinition = (rewrite: Rewrite, key: string): string => {
640
640
  const instantiation = rewrite.instantiation;
641
641
  const definition = instantiation.getDefinition(key);
642
642
  if (!definition) return key;
643
- return instantiation.addDefinition(definition.name, (copyKey) => {
643
+ return instantiation.addDefinition(key, definition.name, (copyKey) => {
644
644
  rewrite.copies.set(key, copyKey);
645
645
  const rename = createRenamer(rewrite);
646
646
  const mapped = renameParams(
@@ -679,7 +679,7 @@ export const createInstantiation = (instantiation: Instantiation) => {
679
679
  if (copy !== undefined) return copy;
680
680
  const definition = instantiation.getDefinition(key);
681
681
  if (!definition) return key;
682
- return instantiation.addDefinition(definition.name, () =>
682
+ return instantiation.addDefinition(key, definition.name, () =>
683
683
  withInstanceParams(definition, instantiation.params, definition.location),
684
684
  );
685
685
  },