tla-ts 0.0.3-dev.79f7ed7 → 0.0.3-dev.949cce6

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.
Files changed (47) hide show
  1. package/README.md +16 -2
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +10 -0
  4. package/dist/index.d.ts +10 -0
  5. package/dist/index.js +1 -1
  6. package/dist/parallel-worker.cjs +1 -1
  7. package/dist/parallel-worker.js +2 -2
  8. package/dist/parallel.cjs +1 -1
  9. package/dist/parallel.js +2 -2
  10. package/dist/run-cli.cjs +17 -16
  11. package/dist/run-cli.js +6 -5
  12. package/dist/transfer.cjs +27 -15
  13. package/dist/transfer.js +27 -15
  14. package/package.json +1 -1
  15. package/src/ast.ts +90 -0
  16. package/src/check.ts +239 -72
  17. package/src/cli-options.ts +11 -5
  18. package/src/codegen.ts +9 -19
  19. package/src/dependencies.ts +2 -2
  20. package/src/environment.ts +75 -16
  21. package/src/errors.ts +9 -2
  22. package/src/evaluate.ts +85 -142
  23. package/src/exit-codes.ts +21 -43
  24. package/src/instances.ts +15 -14
  25. package/src/levels.ts +8 -8
  26. package/src/lexer.ts +5 -1
  27. package/src/live-check.ts +706 -0
  28. package/src/liveness.ts +54 -535
  29. package/src/parallel-worker.ts +5 -1
  30. package/src/parallel.ts +18 -2
  31. package/src/parser.ts +12 -1
  32. package/src/resolve.ts +16 -15
  33. package/src/run-cli.ts +58 -13
  34. package/src/runtime.ts +3 -1
  35. package/src/spec-description.ts +18 -6
  36. package/src/standard-modules.ts +2 -7
  37. package/src/state-graph.ts +157 -0
  38. package/src/subexpressions.ts +10 -10
  39. package/src/successors.ts +77 -13
  40. package/src/symmetry.ts +14 -2
  41. package/src/tableau.ts +50 -23
  42. package/src/tlc-ext.ts +29 -12
  43. package/src/tlc-module.ts +9 -28
  44. package/src/tlc-order.ts +17 -3
  45. package/src/tlc-random.ts +9 -6
  46. package/src/tlc-summary.ts +20 -5
  47. package/src/values.ts +66 -15
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tla-ts",
3
- "version": "0.0.3-dev.79f7ed7",
3
+ "version": "0.0.3-dev.949cce6",
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
@@ -486,3 +486,93 @@ export interface Module {
486
486
  uses: UseOrHide[];
487
487
  units: ModuleUnit[];
488
488
  }
489
+
490
+ export const getChildren = (expression: Expression): Expression[] => {
491
+ switch (expression.kind) {
492
+ case "Number":
493
+ case "Decimal":
494
+ case "String":
495
+ case "Identifier":
496
+ case "OperatorReference":
497
+ case "ExceptAt":
498
+ return [];
499
+ case "Apply":
500
+ return expression.args;
501
+ case "Lambda":
502
+ return [expression.body];
503
+ case "And":
504
+ case "Or":
505
+ return expression.operands;
506
+ case "Prime":
507
+ case "Unchanged":
508
+ case "Enabled":
509
+ case "Always":
510
+ case "Eventually":
511
+ return [expression.expression];
512
+ case "If":
513
+ return [expression.condition, expression.consequent, expression.alternative];
514
+ case "Case":
515
+ return [
516
+ ...expression.arms.flatMap((arm) => [arm.condition, arm.body]),
517
+ ...(expression.other ? [expression.other] : []),
518
+ ];
519
+ case "Let":
520
+ return [
521
+ ...expression.definitions.flatMap((definition) =>
522
+ definition.kind === "Function"
523
+ ? [...definition.bounds.map((bound) => bound.set), definition.body]
524
+ : [definition.body],
525
+ ),
526
+ expression.body,
527
+ ];
528
+ case "Forall":
529
+ case "Exists":
530
+ return [...expression.bounds.map((bound) => bound.set), expression.body];
531
+ case "UnboundedQuantifier":
532
+ return [expression.body];
533
+ case "Choose":
534
+ return [...(expression.set ? [expression.set] : []), expression.body];
535
+ case "SetEnumeration":
536
+ case "Tuple":
537
+ return expression.elements;
538
+ case "SetFilter":
539
+ return [expression.bound.set, expression.predicate];
540
+ case "SetMap":
541
+ return [expression.expression, ...expression.bounds.map((bound) => bound.set)];
542
+ case "FunctionConstruction":
543
+ return [...expression.bounds.map((bound) => bound.set), expression.body];
544
+ case "FunctionSet":
545
+ return [expression.domain, expression.range];
546
+ case "Record":
547
+ case "RecordSet":
548
+ return expression.fields.map((field) => field.value);
549
+ case "Except":
550
+ return [
551
+ expression.function,
552
+ ...expression.updates.flatMap((update) => [
553
+ ...update.path.flatMap((step) => (step.kind === "Index" ? step.args : [])),
554
+ update.value,
555
+ ]),
556
+ ];
557
+ case "FunctionApplication":
558
+ return [expression.function, ...expression.args];
559
+ case "FieldAccess":
560
+ return [expression.record];
561
+ case "Product":
562
+ return expression.factors;
563
+ case "BoxAction":
564
+ case "AngleAction":
565
+ return [expression.action, expression.subscript];
566
+ case "WeakFairness":
567
+ case "StrongFairness":
568
+ return [expression.subscript, expression.action];
569
+ case "Label":
570
+ return [expression.body];
571
+ case "Subexpression":
572
+ return expression.selectors.flatMap((selector) => ("args" in selector ? selector.args : []));
573
+ }
574
+ };
575
+
576
+ export const isLocallyResolved = (expression: Expression) =>
577
+ (expression.kind === "Identifier" || expression.kind === "Apply") &&
578
+ expression.resolution?.kind === "Local";
package/src/check.ts CHANGED
@@ -1,32 +1,41 @@
1
- import type { Assumption, Definition, Expression, SourceLocation } from "./ast";
1
+ import {
2
+ getChildren,
3
+ isLocallyResolved,
4
+ type Assumption,
5
+ type Definition,
6
+ type Expression,
7
+ type SourceLocation,
8
+ } from "./ast";
2
9
  import { parseConfig, type ConfigValue, type ModelConfig } from "./config";
3
10
  import { getReadVariables } from "./dependencies";
4
11
  import { createEnvironment } from "./environment";
5
- import { getErrorExitCode, TlaConfigError, TlaEvaluationError, TlaModuleError } from "./errors";
6
- import { EXIT_STATUS, setExitCode, setNextStatePhase, withLivenessExitCode } from "./exit-codes";
12
+ import {
13
+ getErrorExitCode,
14
+ TlaConfigError,
15
+ TlaEvaluationError,
16
+ TlaModuleError,
17
+ UNEXPECTED_EXCEPTION,
18
+ } from "./errors";
19
+ import { EXIT_STATUS, setExitCode, withLivenessExitCode } from "./exit-codes";
7
20
  import {
8
21
  describeRange,
9
22
  evaluate,
10
23
  expectBoolean,
11
24
  formatRange,
12
- getChildren,
13
25
  getDefinition,
14
26
  isConstantLevel,
15
27
  isImpure,
16
- isLocallyResolved,
17
28
  type Context,
18
29
  type Environment,
19
30
  type Scope,
20
31
  } from "./evaluate";
21
32
  import {
22
33
  createLivenessChecker,
23
- expandTemporal,
24
- getLevel,
25
34
  type Lasso,
35
+ type LivenessChecker,
26
36
  type LiveSuccessor,
27
- type ScopedExpression,
28
- type TemporalProperty,
29
- } from "./liveness";
37
+ } from "./live-check";
38
+ import { expandTemporal, getLevel, type ScopedExpression, type TemporalProperty } from "./liveness";
30
39
  import { ACTION_LEVEL, createLevelAnalyzer, TEMPORAL_LEVEL } from "./levels";
31
40
  import { prettyPrint } from "./pretty-print";
32
41
  import {
@@ -38,10 +47,17 @@ import {
38
47
  } from "./successors";
39
48
  import {
40
49
  getActionLabel,
50
+ getActionSignature,
41
51
  getNextActions,
42
52
  getSpecificationValue,
43
53
  type SpecAction,
44
54
  } from "./spec-description";
55
+ import {
56
+ createStateWriter,
57
+ type GraphState,
58
+ type StateDump,
59
+ type StateWriter,
60
+ } from "./state-graph";
45
61
  import { getRepresentative, getSymmetryGroup } from "./symmetry";
46
62
  import { getTlcStringOrder, getTlcVariableOrder } from "./tlc-order";
47
63
  import { createTlcFingerprinter, getTlcFingerprint } from "./tlc-random";
@@ -51,6 +67,7 @@ import { createCounterExample } from "./tlc-ext";
51
67
  import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
52
68
  import {
53
69
  combineHash,
70
+ formatState,
54
71
  formatValue,
55
72
  getValueHash,
56
73
  isFunctionValue,
@@ -74,6 +91,8 @@ export interface CheckOptions {
74
91
  workers?: number;
75
92
  // TLC's -lncheck: "final" checks liveness only on the complete state graph.
76
93
  livenessCheck?: "default" | "final";
94
+ // TLC's -dump: writes the reachable states, or with `dot` the state graph, through writeFile.
95
+ dump?: StateDump;
77
96
  }
78
97
 
79
98
  export type CheckStatus =
@@ -95,6 +114,8 @@ export interface TraceStep {
95
114
 
96
115
  export interface Violation {
97
116
  name: string;
117
+ // Every temporal property that a liveness counterexample violates, in the configuration's order.
118
+ properties?: string[];
98
119
  trace: TraceStep[];
99
120
  loopStart: number | undefined;
100
121
  loopLabel?: string;
@@ -425,12 +446,12 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
425
446
  config.properties.forEach((name) => {
426
447
  const definition = getConfigPredicate("property", name, environment);
427
448
  if (!definition) return;
428
- expandTemporal(definition.body, undefined, environment, true).forEach((item) => {
449
+ expandTemporal(definition.body, undefined, environment, true, name).forEach((item) => {
429
450
  const conjunct = item.expression;
430
451
  if (isConstantTrue(item, environment, "property")) return;
431
452
  if (conjunct.kind === "Always" && conjunct.expression.kind === "BoxAction") {
432
453
  properties.actionProperties.push({
433
- name,
454
+ name: item.name,
434
455
  box: conjunct.expression,
435
456
  action: conjunct.expression.action,
436
457
  subscript: conjunct.expression.subscript,
@@ -442,12 +463,22 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
442
463
  conjunct.kind === "Always" &&
443
464
  getLevel(conjunct.expression, item.scope, environment) <= 1
444
465
  ) {
445
- properties.invariants.push({ name, expression: conjunct.expression, scope: item.scope });
466
+ // TLC names the invariant []P after P when P is an operator name.
467
+ const expression = conjunct.expression;
468
+ properties.invariants.push({
469
+ name: expression.kind === "Identifier" ? expression.name : item.name,
470
+ expression,
471
+ scope: item.scope,
472
+ });
446
473
  return;
447
474
  }
448
475
  const level = getLevel(conjunct, item.scope, environment);
449
476
  if (level < ACTION_LEVEL) {
450
- properties.initialPredicates.push({ name, expression: conjunct, scope: item.scope });
477
+ properties.initialPredicates.push({
478
+ name: item.name,
479
+ expression: conjunct,
480
+ scope: item.scope,
481
+ });
451
482
  return;
452
483
  }
453
484
  if (level === ACTION_LEVEL) {
@@ -507,7 +538,9 @@ const getMembership = (
507
538
  const getStateChecks = (named: NamedExpression[], environment: Environment): StateCheck[] =>
508
539
  named.flatMap((item) =>
509
540
  getConjuncts(item.expression, environment).map((expression) => {
510
- const variables = getReadVariables(expression, environment);
541
+ // A LET or an argument in the scope can read variables the expression doesn't name.
542
+ const variables =
543
+ item.scope === undefined ? getReadVariables(expression, environment) : undefined;
511
544
  return {
512
545
  name: item.name,
513
546
  expression,
@@ -754,11 +787,13 @@ export const createSearch = (
754
787
  if (view !== undefined || isFingerprintedLikeTlc) {
755
788
  return BigInt64Array.from(states, getTlcStateFingerprint);
756
789
  }
757
- const indices = getTlcOrder();
758
- const getFingerprint = createTlcFingerprinter();
759
- return BigInt64Array.from(fingerprintStates, (state) =>
760
- getFingerprint(indices.map((index) => state[index])),
790
+ const fingerprints = new BigInt64Array(fingerprintStates.length);
791
+ const fingerprint = createTlcFingerprinter(
792
+ getTlcOrder(),
793
+ new Int32Array(fingerprints.buffer),
761
794
  );
795
+ fingerprintStates.forEach(fingerprint);
796
+ return fingerprints;
762
797
  },
763
798
  getFingerprintState: (state) => {
764
799
  if (isFingerprintedLikeTlc) return [String(getTlcStateFingerprint(state))];
@@ -820,7 +855,10 @@ export const createSearch = (
820
855
  )
821
856
  );
822
857
  } catch (error) {
823
- throw withHeadline(error, `Evaluating action property ${property.name} failed.`);
858
+ throw withHeadline(
859
+ withNextStateExitCode(error),
860
+ `Evaluating action property ${property.name} failed.`,
861
+ );
824
862
  }
825
863
  }),
826
864
  satisfiesActionConstraints: (state, next) =>
@@ -828,8 +866,7 @@ export const createSearch = (
828
866
  holdsLikeTlc(
829
867
  constraint.expression,
830
868
  getContext(environment, state, next),
831
- (expression) =>
832
- `TLC threw an unexpected exception.\nThis was probably caused by an error in the spec or model.\nSee the User Output or TLC Console for clues to what happened.\nThe exception was a java.lang.RuntimeException\n: ${getExpectedBoolean(expression, environment)}`,
869
+ (expression) => `${UNEXPECTED_EXCEPTION}${getExpectedBoolean(expression, environment)}`,
833
870
  ),
834
871
  ),
835
872
  trackStatePossible:
@@ -857,6 +894,11 @@ export const createSearch = (
857
894
  };
858
895
  };
859
896
 
897
+ // TLC exits with the code of a standard module's failure only when the failure escapes the
898
+ // computation of next states. In every other phase, the phase decides the code.
899
+ const withNextStateExitCode = (error: unknown) =>
900
+ error instanceof TlaModuleError ? setExitCode(error, error.exitCode) : error;
901
+
860
902
  const withHeadline = (error: unknown, headline: string) =>
861
903
  error instanceof TlaEvaluationError
862
904
  ? setExitCode(
@@ -865,8 +907,15 @@ const withHeadline = (error: unknown, headline: string) =>
865
907
  )
866
908
  : error;
867
909
 
910
+ // TLC finishes the -dump file whether or not the search finds a violation.
868
911
  export const checkSequentially = (options: CheckOptions): CheckResult => {
869
- setNextStatePhase(false);
912
+ const stateWriter = options.dump && createStateWriter(options.dump, options.writeFile);
913
+ const result = searchStates(options, stateWriter);
914
+ stateWriter?.close();
915
+ return result;
916
+ };
917
+
918
+ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefined): CheckResult => {
870
919
  const config = typeof options.config === "string" ? parseConfig(options.config) : options.config;
871
920
  const onPrint = options.onPrint ?? console.log;
872
921
  // TLC prints again while it rebuilds a trace, between the states it prints. Printing those lines
@@ -962,8 +1011,36 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
962
1011
  const getFingerprintState = search.getFingerprintState;
963
1012
  const nextActions = { value: undefined as SpecAction[] | undefined };
964
1013
  const isReducing = config.view !== undefined || config.symmetry !== undefined;
1014
+ const tlcFingerprints: number[] = [];
965
1015
  const liveness = withLivenessExitCode(() =>
966
- createLivenessChecker(search.properties.liveness, spec.temporals, environment),
1016
+ createLivenessChecker(search.properties.liveness, spec.temporals, environment, {
1017
+ getFingerprint: (index) =>
1018
+ (tlcFingerprints[index] ??= Number(
1019
+ BigInt.asIntN(32, runtime.getFingerprint(states[index])),
1020
+ )),
1021
+ getSuccessors: (state) => {
1022
+ const successors: LiveSuccessor[] = [];
1023
+ if (spec.next === undefined) return successors;
1024
+ forEachSuccessor(
1025
+ spec.next,
1026
+ state,
1027
+ environment,
1028
+ (successor) => {
1029
+ canonicalizeState(successor.state);
1030
+ const isInModel =
1031
+ search.satisfiesConstraints(successor.state, state) &&
1032
+ search.satisfiesActionConstraints(state, successor.state);
1033
+ if (!isInModel) return false;
1034
+ const fingerprintState = getFingerprintState(successor.state);
1035
+ const index = findStateIndex(getStateHash(fingerprintState), fingerprintState);
1036
+ successors.push({ index, state: successor.state });
1037
+ return false;
1038
+ },
1039
+ spec.scope,
1040
+ );
1041
+ return successors;
1042
+ },
1043
+ }),
967
1044
  );
968
1045
  const isCanonicalizedFirst = search.isSymmetric || liveness !== undefined;
969
1046
 
@@ -972,7 +1049,9 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
972
1049
  const parents: number[] = [];
973
1050
  const actions: (string | undefined)[] = [];
974
1051
  const depths: number[] = [];
975
- const buckets = new Map<number, number | number[]>();
1052
+ // A Map holds at most 2^24 entries, so an open-addressing table keeps each seen state's hash
1053
+ // and its index plus one side by side, probed by the hash.
1054
+ const seen = { slots: new Int32Array(1 << 17), size: 0, shift: 16 };
976
1055
  const canonicalizeState = createCanonicalizer();
977
1056
 
978
1057
  const toRecord = (state: Value[]): Record<string, Value> =>
@@ -985,11 +1064,16 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
985
1064
  position: number,
986
1065
  loopStart: number | undefined,
987
1066
  ) => {
988
- const state = steps[position].state;
989
- const next = (steps[position + 1] ?? steps[loopStart ?? position]).state;
990
1067
  // TLC evaluates TLCGet("level") and Trace in an alias at the state's position in the trace.
991
1068
  runtime.getLevel = () => position + 1;
992
1069
  runtime.getBehavior = () => steps.slice(0, position + 1).map((step) => step.state);
1070
+ return evaluateAlias(
1071
+ alias,
1072
+ steps[position].state,
1073
+ (steps[position + 1] ?? steps[loopStart ?? position]).state,
1074
+ );
1075
+ };
1076
+ const evaluateAlias = (alias: string, state: Value[], next: Value[] | undefined) => {
993
1077
  try {
994
1078
  const body = getConfigDefinition("alias", alias, environment).body;
995
1079
  const value = evaluate(createIdentifier(alias), getContext(environment, state, next));
@@ -1104,7 +1188,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1104
1188
  outDegrees: failureOutDegrees,
1105
1189
  };
1106
1190
  };
1107
- const getPathTrace = (path: number[], loopStart?: number) => {
1191
+ const getPathSteps = (path: number[], loopStart?: number) => {
1108
1192
  const steps = getPathStates(path, NEXT_RECOVERY_FAILURE);
1109
1193
  const last = steps[steps.length - 1].state;
1110
1194
  const isClosed =
@@ -1113,7 +1197,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1113
1197
  loopStart === path.length - 1 ||
1114
1198
  findSuccessorWithFingerprint(last, path[loopStart]) !== undefined;
1115
1199
  if (!isClosed) throw new TlaEvaluationError(NEXT_RECOVERY_FAILURE);
1116
- return toTrace(steps, path, loopStart);
1200
+ return steps;
1117
1201
  };
1118
1202
  const getStateHash = (state: Value[]) => {
1119
1203
  let hash = 0x811c9dc5;
@@ -1127,19 +1211,35 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1127
1211
  }
1128
1212
  return true;
1129
1213
  };
1214
+ const getSlot = (hash: number) => (Math.imul(hash, 0x9e3779b1) >>> seen.shift) << 1;
1130
1215
  const findStateIndex = (hash: number, state: Value[]) => {
1131
- const bucket = buckets.get(hash);
1132
- if (bucket === undefined) return -1;
1133
- if (typeof bucket === "number") {
1134
- return isSameState(fingerprintStates[bucket], state) ? bucket : -1;
1216
+ const mask = seen.slots.length - 1;
1217
+ for (let slot = getSlot(hash); ; slot = (slot + 2) & mask) {
1218
+ const entry = seen.slots[slot + 1];
1219
+ if (entry === 0) return -1;
1220
+ if (seen.slots[slot] === hash && isSameState(fingerprintStates[entry - 1], state)) {
1221
+ return entry - 1;
1222
+ }
1135
1223
  }
1136
- return bucket.find((index) => isSameState(fingerprintStates[index], state)) ?? -1;
1224
+ };
1225
+ const insertSeen = (hash: number, entry: number) => {
1226
+ const mask = seen.slots.length - 1;
1227
+ let slot = getSlot(hash);
1228
+ while (seen.slots[slot + 1] !== 0) slot = (slot + 2) & mask;
1229
+ seen.slots[slot] = hash;
1230
+ seen.slots[slot + 1] = entry;
1137
1231
  };
1138
1232
  const markSeen = (hash: number, index: number) => {
1139
- const bucket = buckets.get(hash);
1140
- if (bucket === undefined) buckets.set(hash, index);
1141
- else if (typeof bucket === "number") buckets.set(hash, [bucket, index]);
1142
- else bucket.push(index);
1233
+ seen.size++;
1234
+ if (seen.size * 4 > seen.slots.length) {
1235
+ const slots = seen.slots;
1236
+ seen.slots = new Int32Array(slots.length * 2);
1237
+ seen.shift--;
1238
+ for (let slot = 0; slot < slots.length; slot += 2) {
1239
+ if (slots[slot + 1] !== 0) insertSeen(slots[slot], slots[slot + 1]);
1240
+ }
1241
+ }
1242
+ insertSeen(hash, index + 1);
1143
1243
  };
1144
1244
  const countDistinctState = (depth: number) => {
1145
1245
  runtime.distinctStates = states.length + 1;
@@ -1179,7 +1279,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1179
1279
  };
1180
1280
  runtime.getCurrentState = () => (current.index === -1 ? undefined : states[current.index]);
1181
1281
  runtime.setLivenessSource = (index) => {
1182
- current.livenessSource = index;
1282
+ current.livenessSource = index ?? -1;
1183
1283
  };
1184
1284
  runtime.getLevel = (state) => {
1185
1285
  const source = current.livenessSource;
@@ -1190,6 +1290,18 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1190
1290
  return current.isExpanding ? depths[current.index] + 1 : 0;
1191
1291
  };
1192
1292
 
1293
+ const getGraphState = (state: Value[], level: number): GraphState => ({
1294
+ fingerprint: BigInt.asIntN(64, runtime.getFingerprint(state)),
1295
+ level,
1296
+ getLabel: () =>
1297
+ formatState(
1298
+ config.alias === undefined
1299
+ ? toRecord(state)
1300
+ : evaluateAlias(config.alias, state, undefined),
1301
+ ),
1302
+ getText: () => formatState(toRecord(state)),
1303
+ });
1304
+
1193
1305
  const visitInitialState = (state: Value[]) => {
1194
1306
  canonicalizeState(state);
1195
1307
  runtime.generatedStates++;
@@ -1206,6 +1318,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1206
1318
  countDistinctState(1);
1207
1319
  runtime.queueSize++;
1208
1320
  liveness?.addInitialState(states.length, state);
1321
+ stateWriter?.writeInitialState(getGraphState(state, 1));
1209
1322
  }
1210
1323
  const violated = search.findViolatedInvariant(state);
1211
1324
  if (violated) {
@@ -1226,7 +1339,13 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1226
1339
  addState(state, fingerprintState, -1, undefined);
1227
1340
  return undefined;
1228
1341
  };
1229
- const visitSuccessor = (index: number, successor: Successor, liveSuccessors: LiveSuccessor[]) => {
1342
+ const visitSuccessor = (
1343
+ index: number,
1344
+ successor: Successor,
1345
+ liveSuccessors: LiveSuccessor[],
1346
+ source: GraphState | undefined,
1347
+ action: SpecAction | undefined,
1348
+ ) => {
1230
1349
  const state = states[index];
1231
1350
  runtime.generatedStates++;
1232
1351
  current.candidate = successor.state;
@@ -1240,6 +1359,14 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1240
1359
  const isInModel = isStateInModel && search.satisfiesActionConstraints(state, successor.state);
1241
1360
  if (isInModel) search.trackActionPossible(state, successor.state);
1242
1361
  const existing = isInModel ? found : -1;
1362
+ if (source && isInModel) {
1363
+ stateWriter?.writeSuccessor(
1364
+ source,
1365
+ getGraphState(successor.state, depths[index] + 1),
1366
+ existing !== -1,
1367
+ action && { name: action.name, signature: getActionSignature(action) },
1368
+ );
1369
+ }
1243
1370
  if (existing !== -1 && liveness) {
1244
1371
  const stored = states[existing];
1245
1372
  const isStored = stored.every((value, position) => value === successor.state[position]);
@@ -1311,23 +1438,55 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1311
1438
  }
1312
1439
  return createSuccess();
1313
1440
  }
1441
+ // TLC labels and colors an edge of the -dump state graph by the action that generated it. When
1442
+ // the graph names its edges, the search generates successors action by action, as TLC does.
1443
+ const isNamingEdges = Boolean(
1444
+ options.dump?.dot && (options.dump.actionLabels || options.dump.colorize),
1445
+ );
1446
+ const forEachActionSuccessor = (
1447
+ state: Value[],
1448
+ onSuccessor: (successor: Successor, action: SpecAction | undefined) => boolean,
1449
+ ) => {
1450
+ if (!isNamingEdges) {
1451
+ return forEachSuccessor(
1452
+ next,
1453
+ state,
1454
+ environment,
1455
+ (successor) => onSuccessor(successor, undefined),
1456
+ spec.scope,
1457
+ );
1458
+ }
1459
+ nextActions.value ??= getNextActions(config, environment, specification.instanceActions);
1460
+ const progress = { isStopped: false };
1461
+ return nextActions.value.reduce((count, action) => {
1462
+ if (progress.isStopped) return count;
1463
+ return (
1464
+ count +
1465
+ forEachSuccessor(
1466
+ action.expression,
1467
+ state,
1468
+ environment,
1469
+ (successor) => {
1470
+ progress.isStopped = onSuccessor(successor, action);
1471
+ return progress.isStopped;
1472
+ },
1473
+ action.scope,
1474
+ )
1475
+ );
1476
+ }, 0);
1477
+ };
1314
1478
  const expandState = (index: number) => {
1315
1479
  const state = states[index];
1316
1480
  const liveSuccessors: LiveSuccessor[] = [];
1317
- const successorCount = forEachSuccessor(
1318
- next,
1319
- state,
1320
- environment,
1321
- (successor) => {
1322
- try {
1323
- outcome.result = visitSuccessor(index, successor, liveSuccessors);
1324
- } catch (error) {
1325
- outcome.result = toFailure(error, getTrace(index, successor));
1326
- }
1327
- return outcome.result !== undefined;
1328
- },
1329
- spec.scope,
1330
- );
1481
+ const source = stateWriter && getGraphState(state, depths[index]);
1482
+ const successorCount = forEachActionSuccessor(state, (successor, action) => {
1483
+ try {
1484
+ outcome.result = visitSuccessor(index, successor, liveSuccessors, source, action);
1485
+ } catch (error) {
1486
+ outcome.result = toFailure(error, getTrace(index, successor));
1487
+ }
1488
+ return outcome.result !== undefined;
1489
+ });
1331
1490
  current.isExpanding = false;
1332
1491
  if (outcome.result) return outcome.result;
1333
1492
  if (successorCount === 0 && config.checkDeadlock) {
@@ -1335,32 +1494,43 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1335
1494
  }
1336
1495
  try {
1337
1496
  const accepted = liveness?.addState(index, state, liveSuccessors);
1338
- current.livenessSource = -1;
1339
- if (!accepted) return undefined;
1497
+ if (!liveness || !accepted) return undefined;
1498
+ const steps = getPathSteps(accepted);
1499
+ const properties = liveness.getViolatedProperties(
1500
+ steps.map((step) => step.state),
1501
+ steps.length - 1,
1502
+ );
1340
1503
  return reportViolation("property-violation", {
1341
- name: accepted.name,
1342
- trace: getPathTrace(accepted.path),
1504
+ name: properties[0],
1505
+ properties,
1506
+ trace: toTrace(steps, accepted),
1343
1507
  });
1344
1508
  } catch (error) {
1345
1509
  return toFailure(error, getStateTrace(index), undefined, getPartialOutDegrees());
1346
1510
  }
1347
1511
  };
1348
- const reportLasso = (lasso: Lasso) => {
1512
+ // TLC names the properties and the step back into the cycle from the states it prints.
1513
+ const reportLasso = (checker: LivenessChecker, lasso: Lasso) => {
1349
1514
  const path = [...lasso.prefix, ...lasso.cycle];
1350
- const trace = getPathTrace(path, lasso.prefix.length);
1351
- const back = path[lasso.prefix.length];
1515
+ const loopStart = lasso.prefix.length;
1516
+ const steps = getPathSteps(path, loopStart);
1517
+ const trace = toTrace(steps, path, loopStart);
1518
+ const properties = checker.getViolatedProperties(
1519
+ steps.map((step) => step.state),
1520
+ loopStart,
1521
+ );
1522
+ const back = path[loopStart];
1352
1523
  const last = path[path.length - 1];
1524
+ const lastState = steps[steps.length - 1].state;
1353
1525
  const loopLabel =
1354
- back === last ? undefined : withoutOutput(() => getLabel(last, states[last], states[back]));
1526
+ back === last ? undefined : withoutOutput(() => getLabel(last, lastState, states[back]));
1355
1527
  return reportViolation(
1356
1528
  "liveness-violation",
1357
- { name: lasso.name, trace, loopStart: lasso.prefix.length, loopLabel },
1529
+ { name: properties[0], properties, trace, loopStart, loopLabel },
1358
1530
  () =>
1359
1531
  back === last
1360
1532
  ? trace[trace.length - 1].action
1361
- : getTraceSuccessors(states[last]).find((successor) =>
1362
- isSameState(successor.state, states[back]),
1363
- )?.action,
1533
+ : findSuccessorWithFingerprint(lastState, back)?.action,
1364
1534
  );
1365
1535
  };
1366
1536
  const isPeriodic = liveness !== undefined && options.livenessCheck !== "final";
@@ -1373,11 +1543,9 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1373
1543
  if (!isGrown || periodic.time > (now - startedAt) * LIVENESS_RATIO) return undefined;
1374
1544
  periodic.size = expanded;
1375
1545
  const lasso = liveness?.findViolation();
1376
- current.livenessSource = -1;
1377
1546
  periodic.time += performance.now() - now;
1378
1547
  return lasso;
1379
1548
  };
1380
- setNextStatePhase(true);
1381
1549
  for (let index = 0; index < states.length && !runtime.isStopped; index++) {
1382
1550
  current.index = index;
1383
1551
  current.candidate = undefined;
@@ -1386,22 +1554,21 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1386
1554
  try {
1387
1555
  outcome.result = expandState(index);
1388
1556
  } catch (error) {
1389
- outcome.result = toFailure(error, getStateTrace(index));
1557
+ outcome.result = toFailure(withNextStateExitCode(error), getStateTrace(index));
1390
1558
  }
1391
1559
  if (outcome.result) return outcome.result;
1392
1560
  outDegrees[getOutDegreeBucket()]++;
1393
1561
  expansion.newStates = 0;
1394
1562
  if (isPeriodic && performance.now() >= periodic.at) {
1395
1563
  const lasso = findPeriodicViolation(index + 1);
1396
- if (lasso) return reportLasso(lasso);
1564
+ if (liveness && lasso) return reportLasso(liveness, lasso);
1397
1565
  }
1398
1566
  }
1399
1567
 
1400
1568
  current.index = -1;
1401
1569
  current.candidate = undefined;
1402
1570
  const lasso = liveness?.findViolation();
1403
- current.livenessSource = -1;
1404
- if (lasso) return reportLasso(lasso);
1571
+ if (liveness && lasso) return reportLasso(liveness, lasso);
1405
1572
 
1406
1573
  const postconditionViolation = search.findPostconditionViolation();
1407
1574
  if (postconditionViolation) {