tla-ts 0.0.3-dev.a57ca4f → 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.a57ca4f",
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
@@ -11,8 +11,13 @@ import { getReadVariables } from "./dependencies";
11
11
  import { createEnvironment } from "./environment";
12
12
  import {
13
13
  getErrorExitCode,
14
+ isStackOverflow,
15
+ formatRange,
16
+ STACK_OVERFLOW,
14
17
  TlaConfigError,
15
18
  TlaEvaluationError,
19
+ TlaIllegalStateError,
20
+ TlaIncompleteStateError,
16
21
  TlaModuleError,
17
22
  UNEXPECTED_EXCEPTION,
18
23
  } from "./errors";
@@ -21,7 +26,6 @@ import {
21
26
  describeRange,
22
27
  evaluate,
23
28
  expectBoolean,
24
- formatRange,
25
29
  getDefinition,
26
30
  isConstantLevel,
27
31
  isImpure,
@@ -59,7 +63,7 @@ import {
59
63
  type StateWriter,
60
64
  } from "./state-graph";
61
65
  import { getRepresentative, getSymmetryGroup } from "./symmetry";
62
- import { getTlcStringOrder, getTlcVariableOrder } from "./tlc-order";
66
+ import { getTlcStringOrder, getTlcDeclarationOrder } from "./tlc-order";
63
67
  import { createTlcFingerprinter, getTlcFingerprint } from "./tlc-random";
64
68
  import { createCanonicalizer } from "./canonical";
65
69
  import { getPossibleCounts, type CheckerRuntime } from "./runtime";
@@ -67,6 +71,7 @@ import { createCounterExample } from "./tlc-ext";
67
71
  import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
68
72
  import {
69
73
  combineHash,
74
+ createModelValue,
70
75
  formatState,
71
76
  formatValue,
72
77
  getValueHash,
@@ -136,6 +141,14 @@ export interface EvaluationFailure {
136
141
  trace: TraceStep[];
137
142
  initialState: Record<string, Value> | undefined;
138
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;
139
152
  }
140
153
 
141
154
  export interface CheckResult {
@@ -247,7 +260,7 @@ const getConstantValue = (name: string, definition: Definition, environment: Env
247
260
  try {
248
261
  return evaluate(createIdentifier(name), getContext(environment, undefined, undefined));
249
262
  } catch (error) {
250
- if (error instanceof TlaEvaluationError) return undefined;
263
+ if (error instanceof TlaEvaluationError || isStackOverflow(error)) return undefined;
251
264
  throw error;
252
265
  }
253
266
  };
@@ -276,8 +289,11 @@ const getInvariants = (names: string[], environment: Environment) => {
276
289
  levels.params.size === 0
277
290
  ? ""
278
291
  : "\nNote that a bug can cause TLC to incorrectly report this error.\nIf you believe your TLA+ or PlusCal specification to be correct,\nplease check if this bug described in LevelNode.java starting at line 590ff affects you.";
279
- throw new TlaConfigError(
280
- `The invariant ${name} is not a state predicate (one with no primes or temporal operators).${note}`,
292
+ throw setExitCode(
293
+ new TlaConfigError(
294
+ `The invariant ${name} is not a state predicate (one with no primes or temporal operators).${note}`,
295
+ ),
296
+ EXIT_STATUS.safetyEvaluationFailure,
281
297
  );
282
298
  }
283
299
  return [{ name, expression: createIdentifier(name), scope: undefined }];
@@ -375,8 +391,11 @@ const getSpecification = (config: ModelConfig, environment: Environment): Specif
375
391
  conjunct.kind === "Always" &&
376
392
  getLevel(conjunct.expression, item.scope, environment) < ACTION_LEVEL
377
393
  ) {
378
- throw new TlaConfigError(
379
- `TLC cannot handle []P in the specification ${config.specification} when P is a state predicate`,
394
+ throw setExitCode(
395
+ new TlaConfigError(
396
+ `TLC cannot handle the temporal formula ${describeRange(conjunct.expression)}`,
397
+ ),
398
+ EXIT_STATUS.error,
380
399
  );
381
400
  }
382
401
  const level = getLevel(conjunct, item.scope, environment);
@@ -787,11 +806,13 @@ export const createSearch = (
787
806
  if (view !== undefined || isFingerprintedLikeTlc) {
788
807
  return BigInt64Array.from(states, getTlcStateFingerprint);
789
808
  }
790
- const indices = getTlcOrder();
791
- const getFingerprint = createTlcFingerprinter();
792
- return BigInt64Array.from(fingerprintStates, (state) =>
793
- getFingerprint(indices.map((index) => state[index])),
809
+ const fingerprints = new BigInt64Array(fingerprintStates.length);
810
+ const fingerprint = createTlcFingerprinter(
811
+ getTlcOrder(),
812
+ new Int32Array(fingerprints.buffer),
794
813
  );
814
+ fingerprintStates.forEach(fingerprint);
815
+ return fingerprints;
795
816
  },
796
817
  getFingerprintState: (state) => {
797
818
  if (isFingerprintedLikeTlc) return [String(getTlcStateFingerprint(state))];
@@ -823,11 +844,17 @@ export const createSearch = (
823
844
  }
824
845
  }),
825
846
  satisfiesConstraints: (state, parent) =>
826
- constraints.every(
827
- (constraint) =>
828
- isUnchangedFor(constraint, state, parent) ||
829
- holdsLikeTlc(constraint.expression, getContext(environment, state, undefined)),
830
- ),
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
+ }),
831
858
  findFailedInitialPredicate: (state) =>
832
859
  properties.initialPredicates.find(
833
860
  (predicate) =>
@@ -860,13 +887,13 @@ export const createSearch = (
860
887
  }
861
888
  }),
862
889
  satisfiesActionConstraints: (state, next) =>
863
- actionConstraints.every((constraint) =>
864
- holdsLikeTlc(
865
- constraint.expression,
866
- getContext(environment, state, next),
867
- (expression) => `${UNEXPECTED_EXCEPTION}${getExpectedBoolean(expression, environment)}`,
868
- ),
869
- ),
890
+ actionConstraints.every((constraint) => {
891
+ try {
892
+ return holdsLikeTlc(constraint.expression, getContext(environment, state, next));
893
+ } catch (error) {
894
+ throw toNextStateError(error);
895
+ }
896
+ }),
870
897
  trackStatePossible:
871
898
  statePossible.length === 0
872
899
  ? ignorePossible
@@ -897,6 +924,39 @@ export const createSearch = (
897
924
  const withNextStateExitCode = (error: unknown) =>
898
925
  error instanceof TlaModuleError ? setExitCode(error, error.exitCode) : error;
899
926
 
927
+ // TLC reports a failure of the next-state relation as an unexpected exception, unless a standard
928
+ // module raised it as an EvalException.
929
+ const toNextStateError = (error: unknown) =>
930
+ error instanceof TlaEvaluationError &&
931
+ !(error instanceof TlaModuleError) &&
932
+ !(error instanceof TlaIncompleteStateError)
933
+ ? setExitCode(
934
+ new TlaEvaluationError(`${UNEXPECTED_EXCEPTION}${error.message}`, error),
935
+ getErrorExitCode(error),
936
+ )
937
+ : withNextStateExitCode(error);
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
+
900
960
  const withHeadline = (error: unknown, headline: string) =>
901
961
  error instanceof TlaEvaluationError
902
962
  ? setExitCode(
@@ -908,9 +968,16 @@ const withHeadline = (error: unknown, headline: string) =>
908
968
  // TLC finishes the -dump file whether or not the search finds a violation.
909
969
  export const checkSequentially = (options: CheckOptions): CheckResult => {
910
970
  const stateWriter = options.dump && createStateWriter(options.dump, options.writeFile);
911
- const result = searchStates(options, stateWriter);
912
- stateWriter?.close();
913
- return result;
971
+ try {
972
+ return searchStates(options, stateWriter);
973
+ } catch (error) {
974
+ // TLC reports a stack overflow outside the search as a failure of its own, with exit code 255.
975
+ throw isStackOverflow(error)
976
+ ? setExitCode(new TlaEvaluationError(STACK_OVERFLOW), EXIT_STATUS.error)
977
+ : error;
978
+ } finally {
979
+ stateWriter?.close();
980
+ }
914
981
  };
915
982
 
916
983
  const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefined): CheckResult => {
@@ -918,9 +985,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
918
985
  const onPrint = options.onPrint ?? console.log;
919
986
  // TLC prints again while it rebuilds a trace, between the states it prints. Printing those lines
920
987
  // before the trace would misplace them, so rebuilding a trace prints nothing.
921
- const output = { isMuted: false };
988
+ const output = { isMuted: false, replayed: undefined as string[] | undefined };
922
989
  const print = (text: string) => {
923
- if (!output.isMuted) onPrint(text);
990
+ if (output.replayed) output.replayed.push(text);
991
+ else if (!output.isMuted) onPrint(text);
924
992
  };
925
993
  const withoutOutput = <Result>(rebuild: () => Result) => {
926
994
  output.isMuted = true;
@@ -982,9 +1050,9 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
982
1050
  ),
983
1051
  });
984
1052
 
985
- const search = createSearch(config, environment, () =>
986
- getTlcVariableOrder(options.source, options.loadModule),
987
- );
1053
+ const getVariableOrder = () =>
1054
+ getTlcDeclarationOrder(options.source, options.loadModule, "variable");
1055
+ const search = createSearch(config, environment, getVariableOrder);
988
1056
  const assumptionViolation = findAssumptionViolation(specification.assumptions, environment);
989
1057
  if (assumptionViolation) return createResult("assumption-violation", assumptionViolation);
990
1058
  if (!search) return createSuccess();
@@ -1008,6 +1076,67 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1008
1076
  const spec = search.spec;
1009
1077
  const getFingerprintState = search.getFingerprintState;
1010
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
+ });
1011
1140
  const isReducing = config.view !== undefined || config.symmetry !== undefined;
1012
1141
  const tlcFingerprints: number[] = [];
1013
1142
  const liveness = withLivenessExitCode(() =>
@@ -1173,7 +1302,17 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1173
1302
  trace: TraceStep[],
1174
1303
  initialState?: Value[],
1175
1304
  failureOutDegrees?: number[],
1176
- ) => {
1305
+ ): CheckResult => {
1306
+ // TLC exits with 255 on a stack overflow while it computes next states, and prints the
1307
+ // behavior up to the state it was expanding.
1308
+ if (isStackOverflow(error)) {
1309
+ return current.isExpanding
1310
+ ? toFailure(
1311
+ setExitCode(new TlaEvaluationError(STACK_OVERFLOW), EXIT_STATUS.error),
1312
+ getStateTrace(current.index),
1313
+ )
1314
+ : toFailure(new TlaEvaluationError(STACK_OVERFLOW), trace, initialState);
1315
+ }
1177
1316
  if (!(error instanceof TlaEvaluationError)) throw error;
1178
1317
  return {
1179
1318
  ...createResult("error", undefined, {
@@ -1263,6 +1402,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1263
1402
  isExpanding: false,
1264
1403
  candidate: undefined as Value[] | undefined,
1265
1404
  candidateLevel: 0,
1405
+ isFingerprinting: false,
1266
1406
  livenessSource: -1,
1267
1407
  };
1268
1408
  const getPath = (index: number): Value[][] =>
@@ -1349,8 +1489,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1349
1489
  current.candidate = successor.state;
1350
1490
  current.candidateLevel = depths[index] + 1;
1351
1491
  if (isCanonicalizedFirst) canonicalizeState(successor.state);
1492
+ current.isFingerprinting = true;
1352
1493
  const fingerprintState = getFingerprintState(successor.state);
1353
1494
  const hash = getStateHash(fingerprintState);
1495
+ current.isFingerprinting = false;
1354
1496
  const found = findStateIndex(hash, fingerprintState);
1355
1497
  const isStateInModel = search.satisfiesConstraints(successor.state, state);
1356
1498
  if (isStateInModel) search.trackStatePossible(successor.state);
@@ -1422,10 +1564,22 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1422
1564
  spec.scope,
1423
1565
  );
1424
1566
  } catch (error) {
1425
- 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
+ };
1426
1580
  }
1427
1581
  }
1428
- if (outcome.result) return outcome.result;
1582
+ if (outcome.result) return replayInitialStates(outcome.result);
1429
1583
 
1430
1584
  const next = spec.next;
1431
1585
  if (next === undefined) {
@@ -1481,7 +1635,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1481
1635
  try {
1482
1636
  outcome.result = visitSuccessor(index, successor, liveSuccessors, source, action);
1483
1637
  } catch (error) {
1484
- 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);
1485
1642
  }
1486
1643
  return outcome.result !== undefined;
1487
1644
  });
@@ -1507,6 +1664,31 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1507
1664
  return toFailure(error, getStateTrace(index), undefined, getPartialOutDegrees());
1508
1665
  }
1509
1666
  };
1667
+ // TLC names the action when the next-state relation has more than one, and prints the
1668
+ // incomplete successor with null for each variable it leaves undefined.
1669
+ const reportIncompleteSuccessor = (error: TlaIncompleteStateError, index: number) => {
1670
+ nextActions.value ??= getNextActions(config, environment, specification.instanceActions);
1671
+ const actions = nextActions.value;
1672
+ const action =
1673
+ actions.length === 1 ? actions[0] : actions.find((item) => item.name === error.action);
1674
+ const variables = `variable${error.missing.length > 1 ? "s are" : " is"} not defined: ${error.missing.join(", ")}.\n`;
1675
+ runtime.generatedStates++;
1676
+ return toFailure(
1677
+ actions.length === 1
1678
+ ? error
1679
+ : new TlaEvaluationError(
1680
+ `Successor state is not completely specified by action ${error.action} of the next-state relation. The following ${variables}`,
1681
+ ),
1682
+ [
1683
+ ...getStateTrace(index),
1684
+ {
1685
+ action: error.action,
1686
+ label: action && getActionLabel(action),
1687
+ state: toRecord(error.state.map((value) => value ?? createModelValue("null"))),
1688
+ },
1689
+ ],
1690
+ );
1691
+ };
1510
1692
  // TLC names the properties and the step back into the cycle from the states it prints.
1511
1693
  const reportLasso = (checker: LivenessChecker, lasso: Lasso) => {
1512
1694
  const path = [...lasso.prefix, ...lasso.cycle];
@@ -1552,7 +1734,14 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1552
1734
  try {
1553
1735
  outcome.result = expandState(index);
1554
1736
  } catch (error) {
1555
- outcome.result = toFailure(withNextStateExitCode(error), getStateTrace(index));
1737
+ const failure =
1738
+ error instanceof TlaIncompleteStateError
1739
+ ? reportIncompleteSuccessor(error, index)
1740
+ : toFailure(toNextStateError(error), getStateTrace(index));
1741
+ outcome.result =
1742
+ error instanceof TlaIncompleteStateError || isStackOverflow(error)
1743
+ ? failure
1744
+ : replaySuccessors(failure, index);
1556
1745
  }
1557
1746
  if (outcome.result) return outcome.result;
1558
1747
  outDegrees[getOutDegreeBucket()]++;
package/src/codegen.ts CHANGED
@@ -10,6 +10,8 @@ import {
10
10
  getSubsetOperator,
11
11
  getEvaluator,
12
12
  getFieldError,
13
+ getNonFunctionError,
14
+ getNonRecordError,
13
15
  isClosedConstant,
14
16
  isTlcInteger,
15
17
  isValueArgument,
@@ -250,12 +252,13 @@ const generateExpression = (generator: Generator, expression: Expression, frame:
250
252
  case "FunctionApplication": {
251
253
  keepLazy(expression.function);
252
254
  const functionCode = generate(expression.function);
255
+ const describeNonFunction = addReference(generator, getNonFunctionError(expression));
253
256
  if (expression.args.length === 1)
254
- return `apply(${functionCode}, ${generate(expression.args[0])})`;
255
- return `apply(${functionCode}, tuple([${expression.args.map((arg) => generate(arg)).join(", ")}]))`;
257
+ return `apply(${functionCode}, ${generate(expression.args[0])}, undefined, ${describeNonFunction})`;
258
+ return `apply(${functionCode}, tuple([${expression.args.map((arg) => generate(arg)).join(", ")}]), undefined, ${describeNonFunction})`;
256
259
  }
257
260
  case "FieldAccess":
258
- return `apply(${generate(expression.record)}, ${JSON.stringify(expression.field)}, ${addReference(generator, getFieldError(expression))})`;
261
+ return `apply(${generate(expression.record)}, ${JSON.stringify(expression.field)}, ${addReference(generator, getFieldError(expression))}, ${addReference(generator, getNonRecordError(expression))})`;
259
262
  case "Record": {
260
263
  const fields = getSortedFields(expression.fields);
261
264
  if (hasDuplicateFields(fields)) return generateDelegate(generator, expression, frame);