tla-ts 0.0.3-dev.4b28a29 → 0.0.3-dev.4cf9a33

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.4b28a29",
3
+ "version": "0.0.3-dev.4cf9a33",
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,6 +11,9 @@ 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,
16
19
  TlaIncompleteStateError,
@@ -22,7 +25,6 @@ import {
22
25
  describeRange,
23
26
  evaluate,
24
27
  expectBoolean,
25
- formatRange,
26
28
  getDefinition,
27
29
  isConstantLevel,
28
30
  isImpure,
@@ -60,7 +62,7 @@ import {
60
62
  type StateWriter,
61
63
  } from "./state-graph";
62
64
  import { getRepresentative, getSymmetryGroup } from "./symmetry";
63
- import { getTlcStringOrder, getTlcVariableOrder } from "./tlc-order";
65
+ import { getTlcStringOrder, getTlcDeclarationOrder } from "./tlc-order";
64
66
  import { createTlcFingerprinter, getTlcFingerprint } from "./tlc-random";
65
67
  import { createCanonicalizer } from "./canonical";
66
68
  import { getPossibleCounts, type CheckerRuntime } from "./runtime";
@@ -68,6 +70,7 @@ import { createCounterExample } from "./tlc-ext";
68
70
  import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
69
71
  import {
70
72
  combineHash,
73
+ createModelValue,
71
74
  formatState,
72
75
  formatValue,
73
76
  getValueHash,
@@ -137,6 +140,13 @@ export interface EvaluationFailure {
137
140
  trace: TraceStep[];
138
141
  initialState: Record<string, Value> | undefined;
139
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;
140
150
  }
141
151
 
142
152
  export interface CheckResult {
@@ -248,7 +258,7 @@ const getConstantValue = (name: string, definition: Definition, environment: Env
248
258
  try {
249
259
  return evaluate(createIdentifier(name), getContext(environment, undefined, undefined));
250
260
  } catch (error) {
251
- if (error instanceof TlaEvaluationError) return undefined;
261
+ if (error instanceof TlaEvaluationError || isStackOverflow(error)) return undefined;
252
262
  throw error;
253
263
  }
254
264
  };
@@ -277,8 +287,11 @@ const getInvariants = (names: string[], environment: Environment) => {
277
287
  levels.params.size === 0
278
288
  ? ""
279
289
  : "\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.";
280
- throw new TlaConfigError(
281
- `The invariant ${name} is not a state predicate (one with no primes or temporal operators).${note}`,
290
+ throw setExitCode(
291
+ new TlaConfigError(
292
+ `The invariant ${name} is not a state predicate (one with no primes or temporal operators).${note}`,
293
+ ),
294
+ EXIT_STATUS.safetyEvaluationFailure,
282
295
  );
283
296
  }
284
297
  return [{ name, expression: createIdentifier(name), scope: undefined }];
@@ -376,8 +389,11 @@ const getSpecification = (config: ModelConfig, environment: Environment): Specif
376
389
  conjunct.kind === "Always" &&
377
390
  getLevel(conjunct.expression, item.scope, environment) < ACTION_LEVEL
378
391
  ) {
379
- throw new TlaConfigError(
380
- `TLC cannot handle []P in the specification ${config.specification} when P is a state predicate`,
392
+ throw setExitCode(
393
+ new TlaConfigError(
394
+ `TLC cannot handle the temporal formula ${describeRange(conjunct.expression)}`,
395
+ ),
396
+ EXIT_STATUS.error,
381
397
  );
382
398
  }
383
399
  const level = getLevel(conjunct, item.scope, environment);
@@ -826,11 +842,17 @@ export const createSearch = (
826
842
  }
827
843
  }),
828
844
  satisfiesConstraints: (state, parent) =>
829
- constraints.every(
830
- (constraint) =>
831
- isUnchangedFor(constraint, state, parent) ||
832
- holdsLikeTlc(constraint.expression, getContext(environment, state, undefined)),
833
- ),
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
+ }),
834
856
  findFailedInitialPredicate: (state) =>
835
857
  properties.initialPredicates.find(
836
858
  (predicate) =>
@@ -863,13 +885,13 @@ export const createSearch = (
863
885
  }
864
886
  }),
865
887
  satisfiesActionConstraints: (state, next) =>
866
- actionConstraints.every((constraint) =>
867
- holdsLikeTlc(
868
- constraint.expression,
869
- getContext(environment, state, next),
870
- (expression) => `${UNEXPECTED_EXCEPTION}${getExpectedBoolean(expression, environment)}`,
871
- ),
872
- ),
888
+ actionConstraints.every((constraint) => {
889
+ try {
890
+ return holdsLikeTlc(constraint.expression, getContext(environment, state, next));
891
+ } catch (error) {
892
+ throw toNextStateError(error);
893
+ }
894
+ }),
873
895
  trackStatePossible:
874
896
  statePossible.length === 0
875
897
  ? ignorePossible
@@ -912,6 +934,27 @@ const toNextStateError = (error: unknown) =>
912
934
  )
913
935
  : withNextStateExitCode(error);
914
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
+
915
958
  const withHeadline = (error: unknown, headline: string) =>
916
959
  error instanceof TlaEvaluationError
917
960
  ? setExitCode(
@@ -923,9 +966,16 @@ const withHeadline = (error: unknown, headline: string) =>
923
966
  // TLC finishes the -dump file whether or not the search finds a violation.
924
967
  export const checkSequentially = (options: CheckOptions): CheckResult => {
925
968
  const stateWriter = options.dump && createStateWriter(options.dump, options.writeFile);
926
- const result = searchStates(options, stateWriter);
927
- stateWriter?.close();
928
- return result;
969
+ try {
970
+ return searchStates(options, stateWriter);
971
+ } catch (error) {
972
+ // TLC reports a stack overflow outside the search as a failure of its own, with exit code 255.
973
+ throw isStackOverflow(error)
974
+ ? setExitCode(new TlaEvaluationError(STACK_OVERFLOW), EXIT_STATUS.error)
975
+ : error;
976
+ } finally {
977
+ stateWriter?.close();
978
+ }
929
979
  };
930
980
 
931
981
  const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefined): CheckResult => {
@@ -933,9 +983,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
933
983
  const onPrint = options.onPrint ?? console.log;
934
984
  // TLC prints again while it rebuilds a trace, between the states it prints. Printing those lines
935
985
  // before the trace would misplace them, so rebuilding a trace prints nothing.
936
- const output = { isMuted: false };
986
+ const output = { isMuted: false, replayed: undefined as string[] | undefined };
937
987
  const print = (text: string) => {
938
- if (!output.isMuted) onPrint(text);
988
+ if (output.replayed) output.replayed.push(text);
989
+ else if (!output.isMuted) onPrint(text);
939
990
  };
940
991
  const withoutOutput = <Result>(rebuild: () => Result) => {
941
992
  output.isMuted = true;
@@ -997,9 +1048,9 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
997
1048
  ),
998
1049
  });
999
1050
 
1000
- const search = createSearch(config, environment, () =>
1001
- getTlcVariableOrder(options.source, options.loadModule),
1002
- );
1051
+ const getVariableOrder = () =>
1052
+ getTlcDeclarationOrder(options.source, options.loadModule, "variable");
1053
+ const search = createSearch(config, environment, getVariableOrder);
1003
1054
  const assumptionViolation = findAssumptionViolation(specification.assumptions, environment);
1004
1055
  if (assumptionViolation) return createResult("assumption-violation", assumptionViolation);
1005
1056
  if (!search) return createSuccess();
@@ -1023,6 +1074,67 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1023
1074
  const spec = search.spec;
1024
1075
  const getFingerprintState = search.getFingerprintState;
1025
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
+ });
1026
1138
  const isReducing = config.view !== undefined || config.symmetry !== undefined;
1027
1139
  const tlcFingerprints: number[] = [];
1028
1140
  const liveness = withLivenessExitCode(() =>
@@ -1188,7 +1300,17 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1188
1300
  trace: TraceStep[],
1189
1301
  initialState?: Value[],
1190
1302
  failureOutDegrees?: number[],
1191
- ) => {
1303
+ ): CheckResult => {
1304
+ // TLC exits with 255 on a stack overflow while it computes next states, and prints the
1305
+ // behavior up to the state it was expanding.
1306
+ if (isStackOverflow(error)) {
1307
+ return current.isExpanding
1308
+ ? toFailure(
1309
+ setExitCode(new TlaEvaluationError(STACK_OVERFLOW), EXIT_STATUS.error),
1310
+ getStateTrace(current.index),
1311
+ )
1312
+ : toFailure(new TlaEvaluationError(STACK_OVERFLOW), trace, initialState);
1313
+ }
1192
1314
  if (!(error instanceof TlaEvaluationError)) throw error;
1193
1315
  return {
1194
1316
  ...createResult("error", undefined, {
@@ -1278,6 +1400,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1278
1400
  isExpanding: false,
1279
1401
  candidate: undefined as Value[] | undefined,
1280
1402
  candidateLevel: 0,
1403
+ isFingerprinting: false,
1281
1404
  livenessSource: -1,
1282
1405
  };
1283
1406
  const getPath = (index: number): Value[][] =>
@@ -1364,8 +1487,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1364
1487
  current.candidate = successor.state;
1365
1488
  current.candidateLevel = depths[index] + 1;
1366
1489
  if (isCanonicalizedFirst) canonicalizeState(successor.state);
1490
+ current.isFingerprinting = true;
1367
1491
  const fingerprintState = getFingerprintState(successor.state);
1368
1492
  const hash = getStateHash(fingerprintState);
1493
+ current.isFingerprinting = false;
1369
1494
  const found = findStateIndex(hash, fingerprintState);
1370
1495
  const isStateInModel = search.satisfiesConstraints(successor.state, state);
1371
1496
  if (isStateInModel) search.trackStatePossible(successor.state);
@@ -1437,10 +1562,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1437
1562
  spec.scope,
1438
1563
  );
1439
1564
  } catch (error) {
1440
- return toFailure(error, []);
1565
+ return replayInitialStates(toFailure(error, []));
1441
1566
  }
1442
1567
  }
1443
- if (outcome.result) return outcome.result;
1568
+ if (outcome.result) return replayInitialStates(outcome.result);
1444
1569
 
1445
1570
  const next = spec.next;
1446
1571
  if (next === undefined) {
@@ -1496,7 +1621,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1496
1621
  try {
1497
1622
  outcome.result = visitSuccessor(index, successor, liveSuccessors, source, action);
1498
1623
  } catch (error) {
1499
- 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);
1500
1628
  }
1501
1629
  return outcome.result !== undefined;
1502
1630
  });
@@ -1522,6 +1650,31 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1522
1650
  return toFailure(error, getStateTrace(index), undefined, getPartialOutDegrees());
1523
1651
  }
1524
1652
  };
1653
+ // TLC names the action when the next-state relation has more than one, and prints the
1654
+ // incomplete successor with null for each variable it leaves undefined.
1655
+ const reportIncompleteSuccessor = (error: TlaIncompleteStateError, index: number) => {
1656
+ nextActions.value ??= getNextActions(config, environment, specification.instanceActions);
1657
+ const actions = nextActions.value;
1658
+ const action =
1659
+ actions.length === 1 ? actions[0] : actions.find((item) => item.name === error.action);
1660
+ const variables = `variable${error.missing.length > 1 ? "s are" : " is"} not defined: ${error.missing.join(", ")}.\n`;
1661
+ runtime.generatedStates++;
1662
+ return toFailure(
1663
+ actions.length === 1
1664
+ ? error
1665
+ : new TlaEvaluationError(
1666
+ `Successor state is not completely specified by action ${error.action} of the next-state relation. The following ${variables}`,
1667
+ ),
1668
+ [
1669
+ ...getStateTrace(index),
1670
+ {
1671
+ action: error.action,
1672
+ label: action && getActionLabel(action),
1673
+ state: toRecord(error.state.map((value) => value ?? createModelValue("null"))),
1674
+ },
1675
+ ],
1676
+ );
1677
+ };
1525
1678
  // TLC names the properties and the step back into the cycle from the states it prints.
1526
1679
  const reportLasso = (checker: LivenessChecker, lasso: Lasso) => {
1527
1680
  const path = [...lasso.prefix, ...lasso.cycle];
@@ -1567,7 +1720,14 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1567
1720
  try {
1568
1721
  outcome.result = expandState(index);
1569
1722
  } catch (error) {
1570
- outcome.result = toFailure(toNextStateError(error), getStateTrace(index));
1723
+ const failure =
1724
+ error instanceof TlaIncompleteStateError
1725
+ ? reportIncompleteSuccessor(error, index)
1726
+ : toFailure(toNextStateError(error), getStateTrace(index));
1727
+ outcome.result =
1728
+ error instanceof TlaIncompleteStateError || isStackOverflow(error)
1729
+ ? failure
1730
+ : replaySuccessors(failure, index);
1571
1731
  }
1572
1732
  if (outcome.result) return outcome.result;
1573
1733
  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);