tla-ts 0.0.3-dev.cf3b82c → 0.0.3-dev.d9ff6f2

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.cf3b82c",
3
+ "version": "0.0.3-dev.d9ff6f2",
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 {
package/src/check.ts CHANGED
@@ -12,6 +12,7 @@ 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,
@@ -24,7 +25,6 @@ import {
24
25
  describeRange,
25
26
  evaluate,
26
27
  expectBoolean,
27
- formatRange,
28
28
  getDefinition,
29
29
  isConstantLevel,
30
30
  isImpure,
@@ -140,6 +140,13 @@ export interface EvaluationFailure {
140
140
  trace: TraceStep[];
141
141
  initialState: Record<string, Value> | undefined;
142
142
  exitCode: number;
143
+ replay?: Replay;
144
+ }
145
+
146
+ // TLC replays a failed step to print the expressions it was evaluating when the replay failed.
147
+ export interface Replay {
148
+ output: string[];
149
+ callStack: string[] | undefined;
143
150
  }
144
151
 
145
152
  export interface CheckResult {
@@ -835,11 +842,17 @@ export const createSearch = (
835
842
  }
836
843
  }),
837
844
  satisfiesConstraints: (state, parent) =>
838
- constraints.every(
839
- (constraint) =>
840
- isUnchangedFor(constraint, state, parent) ||
841
- holdsLikeTlc(constraint.expression, getContext(environment, state, undefined)),
842
- ),
845
+ constraints.every((constraint) => {
846
+ try {
847
+ return (
848
+ isUnchangedFor(constraint, state, parent) ||
849
+ holdsLikeTlc(constraint.expression, getContext(environment, state, undefined))
850
+ );
851
+ } catch (error) {
852
+ // TLC reports a failing constraint on a successor like a failure of the next-state relation.
853
+ throw parent ? toNextStateError(error) : error;
854
+ }
855
+ }),
843
856
  findFailedInitialPredicate: (state) =>
844
857
  properties.initialPredicates.find(
845
858
  (predicate) =>
@@ -872,13 +885,13 @@ export const createSearch = (
872
885
  }
873
886
  }),
874
887
  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
- ),
888
+ actionConstraints.every((constraint) => {
889
+ try {
890
+ return holdsLikeTlc(constraint.expression, getContext(environment, state, next));
891
+ } catch (error) {
892
+ throw toNextStateError(error);
893
+ }
894
+ }),
882
895
  trackStatePossible:
883
896
  statePossible.length === 0
884
897
  ? ignorePossible
@@ -921,6 +934,27 @@ const toNextStateError = (error: unknown) =>
921
934
  )
922
935
  : withNextStateExitCode(error);
923
936
 
937
+ // TLC keeps the stack only when an evaluation fails, and prints each expression on it once, however
938
+ // many times in a row it pushed it.
939
+ const getReplayedCallStack = (replay: () => void, trace: Expression[]) => {
940
+ try {
941
+ replay();
942
+ return undefined;
943
+ } catch (error) {
944
+ if (!(error instanceof TlaEvaluationError)) return [];
945
+ return trace
946
+ .flatMap((expression) => {
947
+ const range = expression.location.range;
948
+ return range
949
+ ? [
950
+ `Line ${range.start.line}, column ${range.start.column} to line ${range.end.line}, column ${range.end.column} in ${range.module}`,
951
+ ]
952
+ : [];
953
+ })
954
+ .filter((frame, index, frames) => index === 0 || frames[index - 1] !== frame);
955
+ }
956
+ };
957
+
924
958
  const withHeadline = (error: unknown, headline: string) =>
925
959
  error instanceof TlaEvaluationError
926
960
  ? setExitCode(
@@ -949,9 +983,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
949
983
  const onPrint = options.onPrint ?? console.log;
950
984
  // TLC prints again while it rebuilds a trace, between the states it prints. Printing those lines
951
985
  // before the trace would misplace them, so rebuilding a trace prints nothing.
952
- const output = { isMuted: false };
986
+ const output = { isMuted: false, replayed: undefined as string[] | undefined };
953
987
  const print = (text: string) => {
954
- if (!output.isMuted) onPrint(text);
988
+ if (output.replayed) output.replayed.push(text);
989
+ else if (!output.isMuted) onPrint(text);
955
990
  };
956
991
  const withoutOutput = <Result>(rebuild: () => Result) => {
957
992
  output.isMuted = true;
@@ -1013,9 +1048,9 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1013
1048
  ),
1014
1049
  });
1015
1050
 
1016
- const search = createSearch(config, environment, () =>
1017
- getTlcDeclarationOrder(options.source, options.loadModule, "variable"),
1018
- );
1051
+ const getVariableOrder = () =>
1052
+ getTlcDeclarationOrder(options.source, options.loadModule, "variable");
1053
+ const search = createSearch(config, environment, getVariableOrder);
1019
1054
  const assumptionViolation = findAssumptionViolation(specification.assumptions, environment);
1020
1055
  if (assumptionViolation) return createResult("assumption-violation", assumptionViolation);
1021
1056
  if (!search) return createSuccess();
@@ -1039,6 +1074,67 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1039
1074
  const spec = search.spec;
1040
1075
  const getFingerprintState = search.getFingerprintState;
1041
1076
  const nextActions = { value: undefined as SpecAction[] | undefined };
1077
+ const replayFailure = (
1078
+ result: CheckResult,
1079
+ replay: (tracedSearch: Search, traced: Environment) => void,
1080
+ ): CheckResult => {
1081
+ if (!result.failure) return result;
1082
+ const trace: Expression[] = [];
1083
+ const traced: Environment = { ...environment, evaluators: new WeakMap(), trace };
1084
+ const tracedSearch = createSearch(config, traced, getVariableOrder);
1085
+ if (!tracedSearch) return result;
1086
+ const replayed: string[] = [];
1087
+ output.replayed = replayed;
1088
+ const callStack = getReplayedCallStack(() => replay(tracedSearch, traced), trace);
1089
+ output.replayed = undefined;
1090
+ return { ...result, failure: { ...result.failure, replay: { output: replayed, callStack } } };
1091
+ };
1092
+ // TLC checks every initial state again, as when it first generated them.
1093
+ const replayInitialStates = (result: CheckResult) =>
1094
+ replayFailure(result, (tracedSearch, traced) => {
1095
+ if (spec.init === undefined) return;
1096
+ forEachInitialState(
1097
+ spec.init,
1098
+ traced,
1099
+ (state) => {
1100
+ tracedSearch.satisfiesConstraints(state);
1101
+ return (
1102
+ tracedSearch.findViolatedInvariant(state) !== undefined ||
1103
+ tracedSearch.findFailedInitialPredicate(state) !== undefined
1104
+ );
1105
+ },
1106
+ spec.scope,
1107
+ );
1108
+ });
1109
+ // TLC rebuilds the behavior up to the state it expanded before it replays the step.
1110
+ const rebuildBehavior = (index: number): void => {
1111
+ const parent = parents[index];
1112
+ if (parent === -1) {
1113
+ getSpecInitialStates();
1114
+ return;
1115
+ }
1116
+ rebuildBehavior(parent);
1117
+ getLabel(parent, states[parent], states[index]);
1118
+ };
1119
+ // TLC generates the successors of each action before it checks them. It checks the invariants
1120
+ // only of a successor outside the model, since it has seen the others.
1121
+ const replaySuccessors = (result: CheckResult, index: number) =>
1122
+ replayFailure(result, (tracedSearch, traced) => {
1123
+ rebuildBehavior(index);
1124
+ const state = states[index];
1125
+ nextActions.value ??= getNextActions(config, environment, specification.instanceActions);
1126
+ nextActions.value.find((action) =>
1127
+ getSuccessors(action.expression, state, traced, action.scope).some((successor) => {
1128
+ const isInModel =
1129
+ tracedSearch.satisfiesConstraints(successor.state) &&
1130
+ tracedSearch.satisfiesActionConstraints(state, successor.state);
1131
+ return (
1132
+ (!isInModel && tracedSearch.findViolatedInvariant(successor.state) !== undefined) ||
1133
+ tracedSearch.findViolatedActionProperty(state, successor.state) !== undefined
1134
+ );
1135
+ }),
1136
+ );
1137
+ });
1042
1138
  const isReducing = config.view !== undefined || config.symmetry !== undefined;
1043
1139
  const tlcFingerprints: number[] = [];
1044
1140
  const liveness = withLivenessExitCode(() =>
@@ -1304,6 +1400,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1304
1400
  isExpanding: false,
1305
1401
  candidate: undefined as Value[] | undefined,
1306
1402
  candidateLevel: 0,
1403
+ isFingerprinting: false,
1307
1404
  livenessSource: -1,
1308
1405
  };
1309
1406
  const getPath = (index: number): Value[][] =>
@@ -1390,8 +1487,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1390
1487
  current.candidate = successor.state;
1391
1488
  current.candidateLevel = depths[index] + 1;
1392
1489
  if (isCanonicalizedFirst) canonicalizeState(successor.state);
1490
+ current.isFingerprinting = true;
1393
1491
  const fingerprintState = getFingerprintState(successor.state);
1394
1492
  const hash = getStateHash(fingerprintState);
1493
+ current.isFingerprinting = false;
1395
1494
  const found = findStateIndex(hash, fingerprintState);
1396
1495
  const isStateInModel = search.satisfiesConstraints(successor.state, state);
1397
1496
  if (isStateInModel) search.trackStatePossible(successor.state);
@@ -1463,10 +1562,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1463
1562
  spec.scope,
1464
1563
  );
1465
1564
  } catch (error) {
1466
- return toFailure(error, []);
1565
+ return replayInitialStates(toFailure(error, []));
1467
1566
  }
1468
1567
  }
1469
- if (outcome.result) return outcome.result;
1568
+ if (outcome.result) return replayInitialStates(outcome.result);
1470
1569
 
1471
1570
  const next = spec.next;
1472
1571
  if (next === undefined) {
@@ -1522,7 +1621,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1522
1621
  try {
1523
1622
  outcome.result = visitSuccessor(index, successor, liveSuccessors, source, action);
1524
1623
  } catch (error) {
1525
- outcome.result = toFailure(error, getTrace(index, successor));
1624
+ // TLC reports a successor it cannot fingerprint as a failure of the state it expands.
1625
+ if (current.isFingerprinting) throw error;
1626
+ const failure = toFailure(error, getTrace(index, successor));
1627
+ outcome.result = isStackOverflow(error) ? failure : replaySuccessors(failure, index);
1526
1628
  }
1527
1629
  return outcome.result !== undefined;
1528
1630
  });
@@ -1618,10 +1720,14 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1618
1720
  try {
1619
1721
  outcome.result = expandState(index);
1620
1722
  } catch (error) {
1621
- outcome.result =
1723
+ const failure =
1622
1724
  error instanceof TlaIncompleteStateError
1623
1725
  ? reportIncompleteSuccessor(error, index)
1624
1726
  : toFailure(toNextStateError(error), getStateTrace(index));
1727
+ outcome.result =
1728
+ error instanceof TlaIncompleteStateError || isStackOverflow(error)
1729
+ ? failure
1730
+ : replaySuccessors(failure, index);
1625
1731
  }
1626
1732
  if (outcome.result) return outcome.result;
1627
1733
  outDegrees[getOutDegreeBucket()]++;