tla-ts 0.0.3-dev.14337b3 → 0.0.3-dev.19a7038

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.3-dev.14337b3",
3
+ "version": "0.0.3-dev.19a7038",
4
4
  "description": "A TLA+ interpreter and model checker in TypeScript, 2× faster than TLC",
5
5
  "keywords": [
6
6
  "formal verification",
package/src/builtins.ts CHANGED
@@ -297,15 +297,21 @@ export const getJavaInteger = (value: Value, signature: string) => {
297
297
  return value;
298
298
  };
299
299
 
300
- export const getIntegersSignature = (method: string, result: string, arity: number) =>
301
- `public static tlc2.value.impl.${result} tlc2.module.Integers.${method}(${Array.from({ length: arity }, () => "tlc2.value.impl.IntValue").join(",")})`;
300
+ export const getArithmeticSignature = (
301
+ javaClass: "Naturals" | "Integers",
302
+ method: string,
303
+ result: string,
304
+ arity: number,
305
+ ) =>
306
+ `public static tlc2.value.impl.${result} tlc2.module.${javaClass}.${method}(${Array.from({ length: arity }, () => "tlc2.value.impl.IntValue").join(",")})`;
302
307
 
303
308
  export const defineArithmetic = (
309
+ javaClass: "Naturals" | "Integers",
304
310
  name: string,
305
311
  method: string,
306
312
  compute: (left: number, right: number) => number,
307
313
  ) => {
308
- const signature = getIntegersSignature(method, "IntValue", 2);
314
+ const signature = getArithmeticSignature(javaClass, method, "IntValue", 2);
309
315
  return defineBinary(name, (left, right) => {
310
316
  const leftNumber = getJavaInteger(left, signature);
311
317
  const rightNumber = getJavaInteger(right, signature);
package/src/check.ts CHANGED
@@ -16,6 +16,7 @@ import {
16
16
  STACK_OVERFLOW,
17
17
  TlaConfigError,
18
18
  TlaEvaluationError,
19
+ TlaIllegalStateError,
19
20
  TlaIncompleteStateError,
20
21
  TlaModuleError,
21
22
  UNEXPECTED_EXCEPTION,
@@ -35,6 +36,7 @@ import {
35
36
  import {
36
37
  createLivenessChecker,
37
38
  type Lasso,
39
+ type LiveGraph,
38
40
  type LivenessChecker,
39
41
  type LiveSuccessor,
40
42
  } from "./live-check";
@@ -67,8 +69,10 @@ import { createTlcFingerprinter, getTlcFingerprint } from "./tlc-random";
67
69
  import { createCanonicalizer } from "./canonical";
68
70
  import { getPossibleCounts, type CheckerRuntime } from "./runtime";
69
71
  import { createCounterExample } from "./tlc-ext";
72
+ import { getFingerprintTrace, valueSources } from "./value-sources";
70
73
  import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
71
74
  import {
75
+ assertFingerprintable,
72
76
  combineHash,
73
77
  createModelValue,
74
78
  formatState,
@@ -140,6 +144,25 @@ export interface EvaluationFailure {
140
144
  trace: TraceStep[];
141
145
  initialState: Record<string, Value> | undefined;
142
146
  exitCode: number;
147
+ replay?: Replay;
148
+ isWithoutStatistics?: boolean;
149
+ }
150
+
151
+ // TLC replays a failed step to print the expressions it was evaluating when the replay failed.
152
+ export interface Replay {
153
+ output: string[];
154
+ callStack: string[] | undefined;
155
+ fingerprint?: Fingerprint;
156
+ // The worker that fails to check liveness replays the step at once, so TLC prints the stack
157
+ // before the error, and only when it holds a frame.
158
+ isBeforeError?: boolean;
159
+ }
160
+
161
+ // TLC reports an error from a method of a value that has a source as one it was unable to
162
+ // fingerprint. With an empty stack, it lists the sources of the values instead.
163
+ export interface Fingerprint {
164
+ sources: string[];
165
+ reason: string;
143
166
  }
144
167
 
145
168
  export interface CheckResult {
@@ -915,8 +938,8 @@ export const createSearch = (
915
938
  const withNextStateExitCode = (error: unknown) =>
916
939
  error instanceof TlaModuleError ? setExitCode(error, error.exitCode) : error;
917
940
 
918
- // TLC reports a failure of the next-state relation as an unexpected exception, unless a standard
919
- // module raised it as an EvalException.
941
+ // TLC reports a failure of the next-state relation, or of the liveness check of a step, as an
942
+ // unexpected exception, unless a standard module raised it as an EvalException.
920
943
  const toNextStateError = (error: unknown) =>
921
944
  error instanceof TlaEvaluationError &&
922
945
  !(error instanceof TlaModuleError) &&
@@ -927,6 +950,47 @@ const toNextStateError = (error: unknown) =>
927
950
  )
928
951
  : withNextStateExitCode(error);
929
952
 
953
+ const isRepeated = (frame: string, index: number, frames: string[]) =>
954
+ index > 0 && frames[index - 1] === frame;
955
+
956
+ // FingerprintException#getTrace numbers the sources from the outermost value and lists the innermost
957
+ // first.
958
+ const getSourceTrace = (sources: Expression[]) =>
959
+ sources
960
+ .flatMap((source) => (source.location.range ? [formatRange(source.location.range)] : []))
961
+ .filter((source, index, all) => !isRepeated(source, index, all))
962
+ .map((source, index) => `${index}) ${source}`)
963
+ .reverse();
964
+
965
+ // TLC keeps the stack only when an evaluation fails, and prints each expression on it once, however
966
+ // many times in a row it pushed it.
967
+ const getReplayedCallStack = (
968
+ replay: () => void,
969
+ trace: Expression[],
970
+ ): Pick<Replay, "callStack" | "fingerprint"> => {
971
+ try {
972
+ replay();
973
+ return { callStack: undefined };
974
+ } catch (error) {
975
+ if (!(error instanceof TlaEvaluationError)) return { callStack: [] };
976
+ const callStack = trace
977
+ .flatMap((expression) => {
978
+ const range = expression.location.range;
979
+ return range
980
+ ? [
981
+ `Line ${range.start.line}, column ${range.start.column} to line ${range.end.line}, column ${range.end.column} in ${range.module}`,
982
+ ]
983
+ : [];
984
+ })
985
+ .filter((frame, index, frames) => !isRepeated(frame, index, frames));
986
+ const sources = getFingerprintTrace(error);
987
+ return {
988
+ callStack,
989
+ fingerprint: sources && { sources: getSourceTrace(sources), reason: error.message },
990
+ };
991
+ }
992
+ };
993
+
930
994
  const withHeadline = (error: unknown, headline: string) =>
931
995
  error instanceof TlaEvaluationError
932
996
  ? setExitCode(
@@ -955,9 +1019,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
955
1019
  const onPrint = options.onPrint ?? console.log;
956
1020
  // TLC prints again while it rebuilds a trace, between the states it prints. Printing those lines
957
1021
  // before the trace would misplace them, so rebuilding a trace prints nothing.
958
- const output = { isMuted: false };
1022
+ const output = { isMuted: false, replayed: undefined as string[] | undefined };
959
1023
  const print = (text: string) => {
960
- if (!output.isMuted) onPrint(text);
1024
+ if (output.replayed) output.replayed.push(text);
1025
+ else if (!output.isMuted) onPrint(text);
961
1026
  };
962
1027
  const withoutOutput = <Result>(rebuild: () => Result) => {
963
1028
  output.isMuted = true;
@@ -1019,9 +1084,9 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1019
1084
  ),
1020
1085
  });
1021
1086
 
1022
- const search = createSearch(config, environment, () =>
1023
- getTlcDeclarationOrder(options.source, options.loadModule, "variable"),
1024
- );
1087
+ const getVariableOrder = () =>
1088
+ getTlcDeclarationOrder(options.source, options.loadModule, "variable");
1089
+ const search = createSearch(config, environment, getVariableOrder);
1025
1090
  const assumptionViolation = findAssumptionViolation(specification.assumptions, environment);
1026
1091
  if (assumptionViolation) return createResult("assumption-violation", assumptionViolation);
1027
1092
  if (!search) return createSuccess();
@@ -1045,37 +1110,114 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1045
1110
  const spec = search.spec;
1046
1111
  const getFingerprintState = search.getFingerprintState;
1047
1112
  const nextActions = { value: undefined as SpecAction[] | undefined };
1113
+ const replayFailure = (
1114
+ result: CheckResult,
1115
+ replay: (tracedSearch: Search, traced: Environment) => void,
1116
+ isBeforeError = false,
1117
+ ): CheckResult => {
1118
+ if (!result.failure) return result;
1119
+ const trace: Expression[] = [];
1120
+ const traced: Environment = { ...environment, evaluators: new WeakMap(), trace };
1121
+ const tracedSearch = createSearch(config, traced, getVariableOrder);
1122
+ if (!tracedSearch) return result;
1123
+ const replayed: string[] = [];
1124
+ output.replayed = replayed;
1125
+ valueSources.current = new WeakMap();
1126
+ const replayedStack = getReplayedCallStack(() => replay(tracedSearch, traced), trace);
1127
+ valueSources.current = undefined;
1128
+ output.replayed = undefined;
1129
+ return {
1130
+ ...result,
1131
+ failure: { ...result.failure, replay: { output: replayed, ...replayedStack, isBeforeError } },
1132
+ };
1133
+ };
1134
+ // TLC fingerprints a state outside CallStackTool, so a failure of a value with a source leaves
1135
+ // the stack empty. Any other failure keeps the stack of the initial predicate.
1136
+ const fingerprintReplayed = (state: Value[], traced: Environment) => {
1137
+ try {
1138
+ getStateHash(getFingerprintState(state));
1139
+ } catch (error) {
1140
+ if (traced.trace && getFingerprintTrace(error)) traced.trace.length = 0;
1141
+ throw error;
1142
+ }
1143
+ };
1144
+ // TLC checks every initial state again, as when it first generated them.
1145
+ const replayInitialStates = (result: CheckResult) =>
1146
+ replayFailure(result, (tracedSearch, traced) => {
1147
+ if (spec.init === undefined) return;
1148
+ forEachInitialState(
1149
+ spec.init,
1150
+ traced,
1151
+ (state) => {
1152
+ if (tracedSearch.satisfiesConstraints(state)) fingerprintReplayed(state, traced);
1153
+ return (
1154
+ tracedSearch.findViolatedInvariant(state) !== undefined ||
1155
+ tracedSearch.findFailedInitialPredicate(state) !== undefined
1156
+ );
1157
+ },
1158
+ spec.scope,
1159
+ );
1160
+ });
1161
+ // TLC rebuilds the behavior up to the state it expanded before it replays the step.
1162
+ const rebuildBehavior = (index: number): void => {
1163
+ const parent = parents[index];
1164
+ if (parent === -1) {
1165
+ getSpecInitialStates();
1166
+ return;
1167
+ }
1168
+ rebuildBehavior(parent);
1169
+ getLabel(parent, states[parent], states[index]);
1170
+ };
1171
+ // TLC generates the successors of each action before it checks them. It checks the invariants
1172
+ // only of a successor outside the model, since it has seen the others.
1173
+ const replaySuccessors = (result: CheckResult, index: number) =>
1174
+ replayFailure(result, (tracedSearch, traced) => {
1175
+ rebuildBehavior(index);
1176
+ const state = states[index];
1177
+ nextActions.value ??= getNextActions(config, environment, specification.instanceActions);
1178
+ nextActions.value.find((action) =>
1179
+ getSuccessors(action.expression, state, traced, action.scope).some((successor) => {
1180
+ const isInModel =
1181
+ tracedSearch.satisfiesConstraints(successor.state) &&
1182
+ tracedSearch.satisfiesActionConstraints(state, successor.state);
1183
+ if (isInModel) fingerprintReplayed(successor.state, traced);
1184
+ return (
1185
+ (!isInModel && tracedSearch.findViolatedInvariant(successor.state) !== undefined) ||
1186
+ tracedSearch.findViolatedActionProperty(state, successor.state) !== undefined
1187
+ );
1188
+ }),
1189
+ );
1190
+ });
1048
1191
  const isReducing = config.view !== undefined || config.symmetry !== undefined;
1049
1192
  const tlcFingerprints: number[] = [];
1193
+ const liveGraph: LiveGraph = {
1194
+ getFingerprint: (index) =>
1195
+ (tlcFingerprints[index] ??= Number(BigInt.asIntN(32, runtime.getFingerprint(states[index])))),
1196
+ getSuccessors: (state) => {
1197
+ const successors: LiveSuccessor[] = [];
1198
+ if (spec.next === undefined) return successors;
1199
+ forEachSuccessor(
1200
+ spec.next,
1201
+ state,
1202
+ environment,
1203
+ (successor) => {
1204
+ canonicalizeState(successor.state);
1205
+ const isInModel =
1206
+ search.satisfiesConstraints(successor.state, state) &&
1207
+ search.satisfiesActionConstraints(state, successor.state);
1208
+ if (!isInModel) return false;
1209
+ const fingerprintState = getFingerprintState(successor.state);
1210
+ const index = findStateIndex(getStateHash(fingerprintState), fingerprintState);
1211
+ successors.push({ index, state: successor.state });
1212
+ return false;
1213
+ },
1214
+ spec.scope,
1215
+ );
1216
+ return successors;
1217
+ },
1218
+ };
1050
1219
  const liveness = withLivenessExitCode(() =>
1051
- createLivenessChecker(search.properties.liveness, spec.temporals, environment, {
1052
- getFingerprint: (index) =>
1053
- (tlcFingerprints[index] ??= Number(
1054
- BigInt.asIntN(32, runtime.getFingerprint(states[index])),
1055
- )),
1056
- getSuccessors: (state) => {
1057
- const successors: LiveSuccessor[] = [];
1058
- if (spec.next === undefined) return successors;
1059
- forEachSuccessor(
1060
- spec.next,
1061
- state,
1062
- environment,
1063
- (successor) => {
1064
- canonicalizeState(successor.state);
1065
- const isInModel =
1066
- search.satisfiesConstraints(successor.state, state) &&
1067
- search.satisfiesActionConstraints(state, successor.state);
1068
- if (!isInModel) return false;
1069
- const fingerprintState = getFingerprintState(successor.state);
1070
- const index = findStateIndex(getStateHash(fingerprintState), fingerprintState);
1071
- successors.push({ index, state: successor.state });
1072
- return false;
1073
- },
1074
- spec.scope,
1075
- );
1076
- return successors;
1077
- },
1078
- }),
1220
+ createLivenessChecker(search.properties.liveness, spec.temporals, environment, liveGraph),
1079
1221
  );
1080
1222
  const isCanonicalizedFirst = search.isSymmetric || liveness !== undefined;
1081
1223
 
@@ -1248,6 +1390,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1248
1390
  let hash = 0x811c9dc5;
1249
1391
  for (let index = 0; index < state.length; index++)
1250
1392
  hash = combineHash(hash, getValueHash(state[index]));
1393
+ assertFingerprintable(state);
1251
1394
  return hash & 0x3fffffff;
1252
1395
  };
1253
1396
  const isSameState = (stored: Value[], state: Value[]) => {
@@ -1472,10 +1615,22 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1472
1615
  spec.scope,
1473
1616
  );
1474
1617
  } catch (error) {
1475
- return toFailure(error, []);
1618
+ if (!(error instanceof TlaIllegalStateError))
1619
+ return replayInitialStates(toFailure(error, []));
1620
+ // TLC prints an illegal initial state with null for each variable it leaves undefined, and
1621
+ // stops before it prints the statistics.
1622
+ const illegal = toFailure(
1623
+ error,
1624
+ [],
1625
+ error.state.map((value) => value ?? createModelValue("null")),
1626
+ );
1627
+ return {
1628
+ ...illegal,
1629
+ failure: illegal.failure && { ...illegal.failure, isWithoutStatistics: true },
1630
+ };
1476
1631
  }
1477
1632
  }
1478
- if (outcome.result) return outcome.result;
1633
+ if (outcome.result) return replayInitialStates(outcome.result);
1479
1634
 
1480
1635
  const next = spec.next;
1481
1636
  if (next === undefined) {
@@ -1533,7 +1688,8 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1533
1688
  } catch (error) {
1534
1689
  // TLC reports a successor it cannot fingerprint as a failure of the state it expands.
1535
1690
  if (current.isFingerprinting) throw error;
1536
- outcome.result = toFailure(error, getTrace(index, successor));
1691
+ const failure = toFailure(error, getTrace(index, successor));
1692
+ outcome.result = isStackOverflow(error) ? failure : replaySuccessors(failure, index);
1537
1693
  }
1538
1694
  return outcome.result !== undefined;
1539
1695
  });
@@ -1556,7 +1712,17 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1556
1712
  trace: toTrace(steps, accepted),
1557
1713
  });
1558
1714
  } catch (error) {
1559
- return toFailure(error, getStateTrace(index), undefined, getPartialOutDegrees());
1715
+ return replayFailure(
1716
+ toFailure(toNextStateError(error), getStateTrace(index), undefined, getPartialOutDegrees()),
1717
+ (_, traced) =>
1718
+ createLivenessChecker(
1719
+ search.properties.liveness,
1720
+ spec.temporals,
1721
+ traced,
1722
+ liveGraph,
1723
+ )?.addState(index, state, liveSuccessors),
1724
+ true,
1725
+ );
1560
1726
  }
1561
1727
  };
1562
1728
  // TLC names the action when the next-state relation has more than one, and prints the
@@ -1629,10 +1795,14 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1629
1795
  try {
1630
1796
  outcome.result = expandState(index);
1631
1797
  } catch (error) {
1632
- outcome.result =
1798
+ const failure =
1633
1799
  error instanceof TlaIncompleteStateError
1634
1800
  ? reportIncompleteSuccessor(error, index)
1635
1801
  : toFailure(toNextStateError(error), getStateTrace(index));
1802
+ outcome.result =
1803
+ error instanceof TlaIncompleteStateError || isStackOverflow(error)
1804
+ ? failure
1805
+ : replaySuccessors(failure, index);
1636
1806
  }
1637
1807
  if (outcome.result) return outcome.result;
1638
1808
  outDegrees[getOutDegreeBucket()]++;
@@ -266,7 +266,11 @@ export const createEnvironment = (
266
266
 
267
267
  const getStandardSymbols = (name: string) => {
268
268
  const symbols = addStandardModule(name);
269
- if (symbols) useModule(name, STANDARD_DEPENDENCIES.get(name) ?? []);
269
+ const dependencies = STANDARD_DEPENDENCIES.get(name) ?? [];
270
+ if (symbols) useModule(name, dependencies);
271
+ // TLC loads the Java class of each module a standard module uses, so one that uses Integers
272
+ // overrides the arithmetic of Naturals.
273
+ if (symbols && dependencies.includes("Integers")) addStandardModule("Integers");
270
274
  return symbols;
271
275
  };
272
276
 
package/src/errors.ts CHANGED
@@ -92,6 +92,17 @@ export class TlaModuleError extends TlaEvaluationError {
92
92
  }
93
93
  }
94
94
 
95
+ // A variable that a partial state leaves without a value.
96
+ export class TlaUnassignedError extends TlaEvaluationError {
97
+ variable: string;
98
+
99
+ constructor(message: string, variable: string) {
100
+ super(message);
101
+ this.name = "TlaEvaluationError";
102
+ this.variable = variable;
103
+ }
104
+ }
105
+
95
106
  // TLC reports a successor state that the next-state relation leaves incomplete as an error of its
96
107
  // own, not as an exception. It keeps the name TlaEvaluationError, and index.ts doesn't export it.
97
108
  export class TlaIncompleteStateError extends TlaEvaluationError {
@@ -110,6 +121,17 @@ export class TlaIncompleteStateError extends TlaEvaluationError {
110
121
  }
111
122
  }
112
123
 
124
+ // TLC reports an initial state that the initial predicate leaves incomplete as an illegal state.
125
+ export class TlaIllegalStateError extends TlaEvaluationError {
126
+ state: (Value | undefined)[];
127
+
128
+ constructor(state: (Value | undefined)[]) {
129
+ super("current state is not a legal state");
130
+ this.name = "TlaEvaluationError";
131
+ this.state = state;
132
+ }
133
+ }
134
+
113
135
  export interface TlapmReport {
114
136
  failure?: string;
115
137
  exception?: string;