tla-ts 0.0.3-dev.a85c0bd → 0.0.3-dev.ab42bab

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