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

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.bfd303f",
3
+ "version": "0.0.3-dev.c8c988d",
4
4
  "description": "A TLA+ interpreter and model checker in TypeScript, 2× faster than TLC",
5
5
  "keywords": [
6
6
  "formal verification",
package/src/ast.ts CHANGED
@@ -47,6 +47,8 @@ export interface ApplyExpression {
47
47
  // The number of args each part of a name like F(x)!l(y) takes, when a part before the last takes
48
48
  // some.
49
49
  argumentCounts?: number[];
50
+ // The range of the parenthesized arguments of a named operator, where SANY reports their count.
51
+ argumentsRange?: SourceRange;
50
52
  location: SourceLocation;
51
53
  resolution?: LocalResolution;
52
54
  }
@@ -192,6 +194,7 @@ export interface FunctionSetExpression {
192
194
  export interface RecordField {
193
195
  name: string;
194
196
  value: Expression;
197
+ location: SourceLocation;
195
198
  }
196
199
 
197
200
  export interface RecordExpression {
@@ -442,12 +445,14 @@ export type StepBody =
442
445
  | { kind: "Assert"; isSuffices: boolean; statement: Expression | AssumeProve }
443
446
  | { kind: "Qed" };
444
447
 
445
- // head is the range of the step number and its statement, without the proof.
448
+ // head is the range of the step number and its statement, without the proof, and statement is the
449
+ // range of the statement alone.
446
450
  export interface ProofStep {
447
451
  name: string | undefined;
448
452
  token: string;
449
453
  body: StepBody;
450
454
  head: SourceRange;
455
+ statement: SourceRange;
451
456
  proof: Proof | undefined;
452
457
  location: SourceLocation;
453
458
  }
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
@@ -12,9 +12,11 @@ import { createEnvironment } from "./environment";
12
12
  import {
13
13
  getErrorExitCode,
14
14
  isStackOverflow,
15
+ formatRange,
15
16
  STACK_OVERFLOW,
16
17
  TlaConfigError,
17
18
  TlaEvaluationError,
19
+ TlaIllegalStateError,
18
20
  TlaIncompleteStateError,
19
21
  TlaModuleError,
20
22
  UNEXPECTED_EXCEPTION,
@@ -24,7 +26,6 @@ import {
24
26
  describeRange,
25
27
  evaluate,
26
28
  expectBoolean,
27
- formatRange,
28
29
  getDefinition,
29
30
  isConstantLevel,
30
31
  isImpure,
@@ -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 {
@@ -835,11 +858,17 @@ export const createSearch = (
835
858
  }
836
859
  }),
837
860
  satisfiesConstraints: (state, parent) =>
838
- constraints.every(
839
- (constraint) =>
840
- isUnchangedFor(constraint, state, parent) ||
841
- holdsLikeTlc(constraint.expression, getContext(environment, state, undefined)),
842
- ),
861
+ constraints.every((constraint) => {
862
+ try {
863
+ return (
864
+ isUnchangedFor(constraint, state, parent) ||
865
+ holdsLikeTlc(constraint.expression, getContext(environment, state, undefined))
866
+ );
867
+ } catch (error) {
868
+ // TLC reports a failing constraint on a successor like a failure of the next-state relation.
869
+ throw parent ? toNextStateError(error) : error;
870
+ }
871
+ }),
843
872
  findFailedInitialPredicate: (state) =>
844
873
  properties.initialPredicates.find(
845
874
  (predicate) =>
@@ -872,13 +901,13 @@ export const createSearch = (
872
901
  }
873
902
  }),
874
903
  satisfiesActionConstraints: (state, next) =>
875
- actionConstraints.every((constraint) =>
876
- holdsLikeTlc(
877
- constraint.expression,
878
- getContext(environment, state, next),
879
- (expression) => `${UNEXPECTED_EXCEPTION}${getExpectedBoolean(expression, environment)}`,
880
- ),
881
- ),
904
+ actionConstraints.every((constraint) => {
905
+ try {
906
+ return holdsLikeTlc(constraint.expression, getContext(environment, state, next));
907
+ } catch (error) {
908
+ throw toNextStateError(error);
909
+ }
910
+ }),
882
911
  trackStatePossible:
883
912
  statePossible.length === 0
884
913
  ? ignorePossible
@@ -909,8 +938,8 @@ export const createSearch = (
909
938
  const withNextStateExitCode = (error: unknown) =>
910
939
  error instanceof TlaModuleError ? setExitCode(error, error.exitCode) : error;
911
940
 
912
- // TLC reports a failure of the next-state relation as an unexpected exception, unless a standard
913
- // 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.
914
943
  const toNextStateError = (error: unknown) =>
915
944
  error instanceof TlaEvaluationError &&
916
945
  !(error instanceof TlaModuleError) &&
@@ -921,6 +950,47 @@ const toNextStateError = (error: unknown) =>
921
950
  )
922
951
  : withNextStateExitCode(error);
923
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
+
924
994
  const withHeadline = (error: unknown, headline: string) =>
925
995
  error instanceof TlaEvaluationError
926
996
  ? setExitCode(
@@ -949,9 +1019,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
949
1019
  const onPrint = options.onPrint ?? console.log;
950
1020
  // TLC prints again while it rebuilds a trace, between the states it prints. Printing those lines
951
1021
  // before the trace would misplace them, so rebuilding a trace prints nothing.
952
- const output = { isMuted: false };
1022
+ const output = { isMuted: false, replayed: undefined as string[] | undefined };
953
1023
  const print = (text: string) => {
954
- if (!output.isMuted) onPrint(text);
1024
+ if (output.replayed) output.replayed.push(text);
1025
+ else if (!output.isMuted) onPrint(text);
955
1026
  };
956
1027
  const withoutOutput = <Result>(rebuild: () => Result) => {
957
1028
  output.isMuted = true;
@@ -1013,9 +1084,9 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1013
1084
  ),
1014
1085
  });
1015
1086
 
1016
- const search = createSearch(config, environment, () =>
1017
- getTlcDeclarationOrder(options.source, options.loadModule, "variable"),
1018
- );
1087
+ const getVariableOrder = () =>
1088
+ getTlcDeclarationOrder(options.source, options.loadModule, "variable");
1089
+ const search = createSearch(config, environment, getVariableOrder);
1019
1090
  const assumptionViolation = findAssumptionViolation(specification.assumptions, environment);
1020
1091
  if (assumptionViolation) return createResult("assumption-violation", assumptionViolation);
1021
1092
  if (!search) return createSuccess();
@@ -1039,37 +1110,114 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1039
1110
  const spec = search.spec;
1040
1111
  const getFingerprintState = search.getFingerprintState;
1041
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
+ });
1042
1191
  const isReducing = config.view !== undefined || config.symmetry !== undefined;
1043
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
+ };
1044
1219
  const liveness = withLivenessExitCode(() =>
1045
- createLivenessChecker(search.properties.liveness, spec.temporals, environment, {
1046
- getFingerprint: (index) =>
1047
- (tlcFingerprints[index] ??= Number(
1048
- BigInt.asIntN(32, runtime.getFingerprint(states[index])),
1049
- )),
1050
- getSuccessors: (state) => {
1051
- const successors: LiveSuccessor[] = [];
1052
- if (spec.next === undefined) return successors;
1053
- forEachSuccessor(
1054
- spec.next,
1055
- state,
1056
- environment,
1057
- (successor) => {
1058
- canonicalizeState(successor.state);
1059
- const isInModel =
1060
- search.satisfiesConstraints(successor.state, state) &&
1061
- search.satisfiesActionConstraints(state, successor.state);
1062
- if (!isInModel) return false;
1063
- const fingerprintState = getFingerprintState(successor.state);
1064
- const index = findStateIndex(getStateHash(fingerprintState), fingerprintState);
1065
- successors.push({ index, state: successor.state });
1066
- return false;
1067
- },
1068
- spec.scope,
1069
- );
1070
- return successors;
1071
- },
1072
- }),
1220
+ createLivenessChecker(search.properties.liveness, spec.temporals, environment, liveGraph),
1073
1221
  );
1074
1222
  const isCanonicalizedFirst = search.isSymmetric || liveness !== undefined;
1075
1223
 
@@ -1242,6 +1390,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1242
1390
  let hash = 0x811c9dc5;
1243
1391
  for (let index = 0; index < state.length; index++)
1244
1392
  hash = combineHash(hash, getValueHash(state[index]));
1393
+ assertFingerprintable(state);
1245
1394
  return hash & 0x3fffffff;
1246
1395
  };
1247
1396
  const isSameState = (stored: Value[], state: Value[]) => {
@@ -1304,6 +1453,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1304
1453
  isExpanding: false,
1305
1454
  candidate: undefined as Value[] | undefined,
1306
1455
  candidateLevel: 0,
1456
+ isFingerprinting: false,
1307
1457
  livenessSource: -1,
1308
1458
  };
1309
1459
  const getPath = (index: number): Value[][] =>
@@ -1390,8 +1540,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1390
1540
  current.candidate = successor.state;
1391
1541
  current.candidateLevel = depths[index] + 1;
1392
1542
  if (isCanonicalizedFirst) canonicalizeState(successor.state);
1543
+ current.isFingerprinting = true;
1393
1544
  const fingerprintState = getFingerprintState(successor.state);
1394
1545
  const hash = getStateHash(fingerprintState);
1546
+ current.isFingerprinting = false;
1395
1547
  const found = findStateIndex(hash, fingerprintState);
1396
1548
  const isStateInModel = search.satisfiesConstraints(successor.state, state);
1397
1549
  if (isStateInModel) search.trackStatePossible(successor.state);
@@ -1463,10 +1615,22 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1463
1615
  spec.scope,
1464
1616
  );
1465
1617
  } catch (error) {
1466
- 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
+ };
1467
1631
  }
1468
1632
  }
1469
- if (outcome.result) return outcome.result;
1633
+ if (outcome.result) return replayInitialStates(outcome.result);
1470
1634
 
1471
1635
  const next = spec.next;
1472
1636
  if (next === undefined) {
@@ -1522,7 +1686,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1522
1686
  try {
1523
1687
  outcome.result = visitSuccessor(index, successor, liveSuccessors, source, action);
1524
1688
  } catch (error) {
1525
- outcome.result = toFailure(error, getTrace(index, successor));
1689
+ // TLC reports a successor it cannot fingerprint as a failure of the state it expands.
1690
+ if (current.isFingerprinting) throw error;
1691
+ const failure = toFailure(error, getTrace(index, successor));
1692
+ outcome.result = isStackOverflow(error) ? failure : replaySuccessors(failure, index);
1526
1693
  }
1527
1694
  return outcome.result !== undefined;
1528
1695
  });
@@ -1545,7 +1712,17 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1545
1712
  trace: toTrace(steps, accepted),
1546
1713
  });
1547
1714
  } catch (error) {
1548
- 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
+ );
1549
1726
  }
1550
1727
  };
1551
1728
  // TLC names the action when the next-state relation has more than one, and prints the
@@ -1618,10 +1795,14 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1618
1795
  try {
1619
1796
  outcome.result = expandState(index);
1620
1797
  } catch (error) {
1621
- outcome.result =
1798
+ const failure =
1622
1799
  error instanceof TlaIncompleteStateError
1623
1800
  ? reportIncompleteSuccessor(error, index)
1624
1801
  : toFailure(toNextStateError(error), getStateTrace(index));
1802
+ outcome.result =
1803
+ error instanceof TlaIncompleteStateError || isStackOverflow(error)
1804
+ ? failure
1805
+ : replaySuccessors(failure, index);
1625
1806
  }
1626
1807
  if (outcome.result) return outcome.result;
1627
1808
  outDegrees[getOutDegreeBucket()]++;