tla-ts 0.0.3-dev.b74f28c → 0.0.3-dev.bcfa494

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 (46) hide show
  1. package/README.md +16 -2
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +9 -0
  4. package/dist/index.d.ts +9 -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 +254 -74
  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 +88 -20
  21. package/src/errors.ts +18 -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 +766 -0
  28. package/src/liveness.ts +54 -535
  29. package/src/parallel-worker.ts +7 -3
  30. package/src/parallel.ts +20 -4
  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 +78 -14
  40. package/src/tableau.ts +50 -23
  41. package/src/tlc-ext.ts +29 -12
  42. package/src/tlc-module.ts +9 -28
  43. package/src/tlc-order.ts +25 -7
  44. package/src/tlc-random.ts +9 -6
  45. package/src/tlc-summary.ts +20 -5
  46. package/src/values.ts +59 -11
package/src/check.ts CHANGED
@@ -1,32 +1,42 @@
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
+ TlaIncompleteStateError,
17
+ TlaModuleError,
18
+ UNEXPECTED_EXCEPTION,
19
+ } from "./errors";
20
+ import { EXIT_STATUS, setExitCode, withLivenessExitCode } from "./exit-codes";
7
21
  import {
8
22
  describeRange,
9
23
  evaluate,
10
24
  expectBoolean,
11
25
  formatRange,
12
- getChildren,
13
26
  getDefinition,
14
27
  isConstantLevel,
15
28
  isImpure,
16
- isLocallyResolved,
17
29
  type Context,
18
30
  type Environment,
19
31
  type Scope,
20
32
  } from "./evaluate";
21
33
  import {
22
34
  createLivenessChecker,
23
- expandTemporal,
24
- getLevel,
25
35
  type Lasso,
36
+ type LivenessChecker,
26
37
  type LiveSuccessor,
27
- type ScopedExpression,
28
- type TemporalProperty,
29
- } from "./liveness";
38
+ } from "./live-check";
39
+ import { expandTemporal, getLevel, type ScopedExpression, type TemporalProperty } from "./liveness";
30
40
  import { ACTION_LEVEL, createLevelAnalyzer, TEMPORAL_LEVEL } from "./levels";
31
41
  import { prettyPrint } from "./pretty-print";
32
42
  import {
@@ -38,12 +48,19 @@ import {
38
48
  } from "./successors";
39
49
  import {
40
50
  getActionLabel,
51
+ getActionSignature,
41
52
  getNextActions,
42
53
  getSpecificationValue,
43
54
  type SpecAction,
44
55
  } from "./spec-description";
56
+ import {
57
+ createStateWriter,
58
+ type GraphState,
59
+ type StateDump,
60
+ type StateWriter,
61
+ } from "./state-graph";
45
62
  import { getRepresentative, getSymmetryGroup } from "./symmetry";
46
- import { getTlcStringOrder, getTlcVariableOrder } from "./tlc-order";
63
+ import { getTlcStringOrder, getTlcDeclarationOrder } from "./tlc-order";
47
64
  import { createTlcFingerprinter, getTlcFingerprint } from "./tlc-random";
48
65
  import { createCanonicalizer } from "./canonical";
49
66
  import { getPossibleCounts, type CheckerRuntime } from "./runtime";
@@ -51,6 +68,7 @@ import { createCounterExample } from "./tlc-ext";
51
68
  import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
52
69
  import {
53
70
  combineHash,
71
+ formatState,
54
72
  formatValue,
55
73
  getValueHash,
56
74
  isFunctionValue,
@@ -74,6 +92,8 @@ export interface CheckOptions {
74
92
  workers?: number;
75
93
  // TLC's -lncheck: "final" checks liveness only on the complete state graph.
76
94
  livenessCheck?: "default" | "final";
95
+ // TLC's -dump: writes the reachable states, or with `dot` the state graph, through writeFile.
96
+ dump?: StateDump;
77
97
  }
78
98
 
79
99
  export type CheckStatus =
@@ -95,6 +115,8 @@ export interface TraceStep {
95
115
 
96
116
  export interface Violation {
97
117
  name: string;
118
+ // Every temporal property that a liveness counterexample violates, in the configuration's order.
119
+ properties?: string[];
98
120
  trace: TraceStep[];
99
121
  loopStart: number | undefined;
100
122
  loopLabel?: string;
@@ -425,12 +447,12 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
425
447
  config.properties.forEach((name) => {
426
448
  const definition = getConfigPredicate("property", name, environment);
427
449
  if (!definition) return;
428
- expandTemporal(definition.body, undefined, environment, true).forEach((item) => {
450
+ expandTemporal(definition.body, undefined, environment, true, name).forEach((item) => {
429
451
  const conjunct = item.expression;
430
452
  if (isConstantTrue(item, environment, "property")) return;
431
453
  if (conjunct.kind === "Always" && conjunct.expression.kind === "BoxAction") {
432
454
  properties.actionProperties.push({
433
- name,
455
+ name: item.name,
434
456
  box: conjunct.expression,
435
457
  action: conjunct.expression.action,
436
458
  subscript: conjunct.expression.subscript,
@@ -442,12 +464,22 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
442
464
  conjunct.kind === "Always" &&
443
465
  getLevel(conjunct.expression, item.scope, environment) <= 1
444
466
  ) {
445
- properties.invariants.push({ name, expression: conjunct.expression, scope: item.scope });
467
+ // TLC names the invariant []P after P when P is an operator name.
468
+ const expression = conjunct.expression;
469
+ properties.invariants.push({
470
+ name: expression.kind === "Identifier" ? expression.name : item.name,
471
+ expression,
472
+ scope: item.scope,
473
+ });
446
474
  return;
447
475
  }
448
476
  const level = getLevel(conjunct, item.scope, environment);
449
477
  if (level < ACTION_LEVEL) {
450
- properties.initialPredicates.push({ name, expression: conjunct, scope: item.scope });
478
+ properties.initialPredicates.push({
479
+ name: item.name,
480
+ expression: conjunct,
481
+ scope: item.scope,
482
+ });
451
483
  return;
452
484
  }
453
485
  if (level === ACTION_LEVEL) {
@@ -507,7 +539,9 @@ const getMembership = (
507
539
  const getStateChecks = (named: NamedExpression[], environment: Environment): StateCheck[] =>
508
540
  named.flatMap((item) =>
509
541
  getConjuncts(item.expression, environment).map((expression) => {
510
- const variables = getReadVariables(expression, environment);
542
+ // A LET or an argument in the scope can read variables the expression doesn't name.
543
+ const variables =
544
+ item.scope === undefined ? getReadVariables(expression, environment) : undefined;
511
545
  return {
512
546
  name: item.name,
513
547
  expression,
@@ -754,11 +788,13 @@ export const createSearch = (
754
788
  if (view !== undefined || isFingerprintedLikeTlc) {
755
789
  return BigInt64Array.from(states, getTlcStateFingerprint);
756
790
  }
757
- const indices = getTlcOrder();
758
- const getFingerprint = createTlcFingerprinter();
759
- return BigInt64Array.from(fingerprintStates, (state) =>
760
- getFingerprint(indices.map((index) => state[index])),
791
+ const fingerprints = new BigInt64Array(fingerprintStates.length);
792
+ const fingerprint = createTlcFingerprinter(
793
+ getTlcOrder(),
794
+ new Int32Array(fingerprints.buffer),
761
795
  );
796
+ fingerprintStates.forEach(fingerprint);
797
+ return fingerprints;
762
798
  },
763
799
  getFingerprintState: (state) => {
764
800
  if (isFingerprintedLikeTlc) return [String(getTlcStateFingerprint(state))];
@@ -820,7 +856,10 @@ export const createSearch = (
820
856
  )
821
857
  );
822
858
  } catch (error) {
823
- throw withHeadline(error, `Evaluating action property ${property.name} failed.`);
859
+ throw withHeadline(
860
+ withNextStateExitCode(error),
861
+ `Evaluating action property ${property.name} failed.`,
862
+ );
824
863
  }
825
864
  }),
826
865
  satisfiesActionConstraints: (state, next) =>
@@ -828,8 +867,7 @@ export const createSearch = (
828
867
  holdsLikeTlc(
829
868
  constraint.expression,
830
869
  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)}`,
870
+ (expression) => `${UNEXPECTED_EXCEPTION}${getExpectedBoolean(expression, environment)}`,
833
871
  ),
834
872
  ),
835
873
  trackStatePossible:
@@ -857,6 +895,23 @@ export const createSearch = (
857
895
  };
858
896
  };
859
897
 
898
+ // TLC exits with the code of a standard module's failure only when the failure escapes the
899
+ // computation of next states. In every other phase, the phase decides the code.
900
+ const withNextStateExitCode = (error: unknown) =>
901
+ error instanceof TlaModuleError ? setExitCode(error, error.exitCode) : error;
902
+
903
+ // TLC reports a failure of the next-state relation as an unexpected exception, unless a standard
904
+ // module raised it as an EvalException.
905
+ const toNextStateError = (error: unknown) =>
906
+ error instanceof TlaEvaluationError &&
907
+ !(error instanceof TlaModuleError) &&
908
+ !(error instanceof TlaIncompleteStateError)
909
+ ? setExitCode(
910
+ new TlaEvaluationError(`${UNEXPECTED_EXCEPTION}${error.message}`, error),
911
+ getErrorExitCode(error),
912
+ )
913
+ : withNextStateExitCode(error);
914
+
860
915
  const withHeadline = (error: unknown, headline: string) =>
861
916
  error instanceof TlaEvaluationError
862
917
  ? setExitCode(
@@ -865,8 +920,15 @@ const withHeadline = (error: unknown, headline: string) =>
865
920
  )
866
921
  : error;
867
922
 
923
+ // TLC finishes the -dump file whether or not the search finds a violation.
868
924
  export const checkSequentially = (options: CheckOptions): CheckResult => {
869
- setNextStatePhase(false);
925
+ const stateWriter = options.dump && createStateWriter(options.dump, options.writeFile);
926
+ const result = searchStates(options, stateWriter);
927
+ stateWriter?.close();
928
+ return result;
929
+ };
930
+
931
+ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefined): CheckResult => {
870
932
  const config = typeof options.config === "string" ? parseConfig(options.config) : options.config;
871
933
  const onPrint = options.onPrint ?? console.log;
872
934
  // TLC prints again while it rebuilds a trace, between the states it prints. Printing those lines
@@ -936,7 +998,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
936
998
  });
937
999
 
938
1000
  const search = createSearch(config, environment, () =>
939
- getTlcVariableOrder(options.source, options.loadModule),
1001
+ getTlcDeclarationOrder(options.source, options.loadModule, "variable"),
940
1002
  );
941
1003
  const assumptionViolation = findAssumptionViolation(specification.assumptions, environment);
942
1004
  if (assumptionViolation) return createResult("assumption-violation", assumptionViolation);
@@ -962,8 +1024,36 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
962
1024
  const getFingerprintState = search.getFingerprintState;
963
1025
  const nextActions = { value: undefined as SpecAction[] | undefined };
964
1026
  const isReducing = config.view !== undefined || config.symmetry !== undefined;
1027
+ const tlcFingerprints: number[] = [];
965
1028
  const liveness = withLivenessExitCode(() =>
966
- createLivenessChecker(search.properties.liveness, spec.temporals, environment),
1029
+ createLivenessChecker(search.properties.liveness, spec.temporals, environment, {
1030
+ getFingerprint: (index) =>
1031
+ (tlcFingerprints[index] ??= Number(
1032
+ BigInt.asIntN(32, runtime.getFingerprint(states[index])),
1033
+ )),
1034
+ getSuccessors: (state) => {
1035
+ const successors: LiveSuccessor[] = [];
1036
+ if (spec.next === undefined) return successors;
1037
+ forEachSuccessor(
1038
+ spec.next,
1039
+ state,
1040
+ environment,
1041
+ (successor) => {
1042
+ canonicalizeState(successor.state);
1043
+ const isInModel =
1044
+ search.satisfiesConstraints(successor.state, state) &&
1045
+ search.satisfiesActionConstraints(state, successor.state);
1046
+ if (!isInModel) return false;
1047
+ const fingerprintState = getFingerprintState(successor.state);
1048
+ const index = findStateIndex(getStateHash(fingerprintState), fingerprintState);
1049
+ successors.push({ index, state: successor.state });
1050
+ return false;
1051
+ },
1052
+ spec.scope,
1053
+ );
1054
+ return successors;
1055
+ },
1056
+ }),
967
1057
  );
968
1058
  const isCanonicalizedFirst = search.isSymmetric || liveness !== undefined;
969
1059
 
@@ -972,7 +1062,9 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
972
1062
  const parents: number[] = [];
973
1063
  const actions: (string | undefined)[] = [];
974
1064
  const depths: number[] = [];
975
- const buckets = new Map<number, number | number[]>();
1065
+ // A Map holds at most 2^24 entries, so an open-addressing table keeps each seen state's hash
1066
+ // and its index plus one side by side, probed by the hash.
1067
+ const seen = { slots: new Int32Array(1 << 17), size: 0, shift: 16 };
976
1068
  const canonicalizeState = createCanonicalizer();
977
1069
 
978
1070
  const toRecord = (state: Value[]): Record<string, Value> =>
@@ -985,11 +1077,16 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
985
1077
  position: number,
986
1078
  loopStart: number | undefined,
987
1079
  ) => {
988
- const state = steps[position].state;
989
- const next = (steps[position + 1] ?? steps[loopStart ?? position]).state;
990
1080
  // TLC evaluates TLCGet("level") and Trace in an alias at the state's position in the trace.
991
1081
  runtime.getLevel = () => position + 1;
992
1082
  runtime.getBehavior = () => steps.slice(0, position + 1).map((step) => step.state);
1083
+ return evaluateAlias(
1084
+ alias,
1085
+ steps[position].state,
1086
+ (steps[position + 1] ?? steps[loopStart ?? position]).state,
1087
+ );
1088
+ };
1089
+ const evaluateAlias = (alias: string, state: Value[], next: Value[] | undefined) => {
993
1090
  try {
994
1091
  const body = getConfigDefinition("alias", alias, environment).body;
995
1092
  const value = evaluate(createIdentifier(alias), getContext(environment, state, next));
@@ -1104,7 +1201,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1104
1201
  outDegrees: failureOutDegrees,
1105
1202
  };
1106
1203
  };
1107
- const getPathTrace = (path: number[], loopStart?: number) => {
1204
+ const getPathSteps = (path: number[], loopStart?: number) => {
1108
1205
  const steps = getPathStates(path, NEXT_RECOVERY_FAILURE);
1109
1206
  const last = steps[steps.length - 1].state;
1110
1207
  const isClosed =
@@ -1113,7 +1210,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1113
1210
  loopStart === path.length - 1 ||
1114
1211
  findSuccessorWithFingerprint(last, path[loopStart]) !== undefined;
1115
1212
  if (!isClosed) throw new TlaEvaluationError(NEXT_RECOVERY_FAILURE);
1116
- return toTrace(steps, path, loopStart);
1213
+ return steps;
1117
1214
  };
1118
1215
  const getStateHash = (state: Value[]) => {
1119
1216
  let hash = 0x811c9dc5;
@@ -1127,19 +1224,35 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1127
1224
  }
1128
1225
  return true;
1129
1226
  };
1227
+ const getSlot = (hash: number) => (Math.imul(hash, 0x9e3779b1) >>> seen.shift) << 1;
1130
1228
  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;
1229
+ const mask = seen.slots.length - 1;
1230
+ for (let slot = getSlot(hash); ; slot = (slot + 2) & mask) {
1231
+ const entry = seen.slots[slot + 1];
1232
+ if (entry === 0) return -1;
1233
+ if (seen.slots[slot] === hash && isSameState(fingerprintStates[entry - 1], state)) {
1234
+ return entry - 1;
1235
+ }
1135
1236
  }
1136
- return bucket.find((index) => isSameState(fingerprintStates[index], state)) ?? -1;
1237
+ };
1238
+ const insertSeen = (hash: number, entry: number) => {
1239
+ const mask = seen.slots.length - 1;
1240
+ let slot = getSlot(hash);
1241
+ while (seen.slots[slot + 1] !== 0) slot = (slot + 2) & mask;
1242
+ seen.slots[slot] = hash;
1243
+ seen.slots[slot + 1] = entry;
1137
1244
  };
1138
1245
  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);
1246
+ seen.size++;
1247
+ if (seen.size * 4 > seen.slots.length) {
1248
+ const slots = seen.slots;
1249
+ seen.slots = new Int32Array(slots.length * 2);
1250
+ seen.shift--;
1251
+ for (let slot = 0; slot < slots.length; slot += 2) {
1252
+ if (slots[slot + 1] !== 0) insertSeen(slots[slot], slots[slot + 1]);
1253
+ }
1254
+ }
1255
+ insertSeen(hash, index + 1);
1143
1256
  };
1144
1257
  const countDistinctState = (depth: number) => {
1145
1258
  runtime.distinctStates = states.length + 1;
@@ -1179,7 +1292,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1179
1292
  };
1180
1293
  runtime.getCurrentState = () => (current.index === -1 ? undefined : states[current.index]);
1181
1294
  runtime.setLivenessSource = (index) => {
1182
- current.livenessSource = index;
1295
+ current.livenessSource = index ?? -1;
1183
1296
  };
1184
1297
  runtime.getLevel = (state) => {
1185
1298
  const source = current.livenessSource;
@@ -1190,6 +1303,18 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1190
1303
  return current.isExpanding ? depths[current.index] + 1 : 0;
1191
1304
  };
1192
1305
 
1306
+ const getGraphState = (state: Value[], level: number): GraphState => ({
1307
+ fingerprint: BigInt.asIntN(64, runtime.getFingerprint(state)),
1308
+ level,
1309
+ getLabel: () =>
1310
+ formatState(
1311
+ config.alias === undefined
1312
+ ? toRecord(state)
1313
+ : evaluateAlias(config.alias, state, undefined),
1314
+ ),
1315
+ getText: () => formatState(toRecord(state)),
1316
+ });
1317
+
1193
1318
  const visitInitialState = (state: Value[]) => {
1194
1319
  canonicalizeState(state);
1195
1320
  runtime.generatedStates++;
@@ -1206,6 +1331,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1206
1331
  countDistinctState(1);
1207
1332
  runtime.queueSize++;
1208
1333
  liveness?.addInitialState(states.length, state);
1334
+ stateWriter?.writeInitialState(getGraphState(state, 1));
1209
1335
  }
1210
1336
  const violated = search.findViolatedInvariant(state);
1211
1337
  if (violated) {
@@ -1226,7 +1352,13 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1226
1352
  addState(state, fingerprintState, -1, undefined);
1227
1353
  return undefined;
1228
1354
  };
1229
- const visitSuccessor = (index: number, successor: Successor, liveSuccessors: LiveSuccessor[]) => {
1355
+ const visitSuccessor = (
1356
+ index: number,
1357
+ successor: Successor,
1358
+ liveSuccessors: LiveSuccessor[],
1359
+ source: GraphState | undefined,
1360
+ action: SpecAction | undefined,
1361
+ ) => {
1230
1362
  const state = states[index];
1231
1363
  runtime.generatedStates++;
1232
1364
  current.candidate = successor.state;
@@ -1240,6 +1372,14 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1240
1372
  const isInModel = isStateInModel && search.satisfiesActionConstraints(state, successor.state);
1241
1373
  if (isInModel) search.trackActionPossible(state, successor.state);
1242
1374
  const existing = isInModel ? found : -1;
1375
+ if (source && isInModel) {
1376
+ stateWriter?.writeSuccessor(
1377
+ source,
1378
+ getGraphState(successor.state, depths[index] + 1),
1379
+ existing !== -1,
1380
+ action && { name: action.name, signature: getActionSignature(action) },
1381
+ );
1382
+ }
1243
1383
  if (existing !== -1 && liveness) {
1244
1384
  const stored = states[existing];
1245
1385
  const isStored = stored.every((value, position) => value === successor.state[position]);
@@ -1311,23 +1451,55 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1311
1451
  }
1312
1452
  return createSuccess();
1313
1453
  }
1454
+ // TLC labels and colors an edge of the -dump state graph by the action that generated it. When
1455
+ // the graph names its edges, the search generates successors action by action, as TLC does.
1456
+ const isNamingEdges = Boolean(
1457
+ options.dump?.dot && (options.dump.actionLabels || options.dump.colorize),
1458
+ );
1459
+ const forEachActionSuccessor = (
1460
+ state: Value[],
1461
+ onSuccessor: (successor: Successor, action: SpecAction | undefined) => boolean,
1462
+ ) => {
1463
+ if (!isNamingEdges) {
1464
+ return forEachSuccessor(
1465
+ next,
1466
+ state,
1467
+ environment,
1468
+ (successor) => onSuccessor(successor, undefined),
1469
+ spec.scope,
1470
+ );
1471
+ }
1472
+ nextActions.value ??= getNextActions(config, environment, specification.instanceActions);
1473
+ const progress = { isStopped: false };
1474
+ return nextActions.value.reduce((count, action) => {
1475
+ if (progress.isStopped) return count;
1476
+ return (
1477
+ count +
1478
+ forEachSuccessor(
1479
+ action.expression,
1480
+ state,
1481
+ environment,
1482
+ (successor) => {
1483
+ progress.isStopped = onSuccessor(successor, action);
1484
+ return progress.isStopped;
1485
+ },
1486
+ action.scope,
1487
+ )
1488
+ );
1489
+ }, 0);
1490
+ };
1314
1491
  const expandState = (index: number) => {
1315
1492
  const state = states[index];
1316
1493
  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
- );
1494
+ const source = stateWriter && getGraphState(state, depths[index]);
1495
+ const successorCount = forEachActionSuccessor(state, (successor, action) => {
1496
+ try {
1497
+ outcome.result = visitSuccessor(index, successor, liveSuccessors, source, action);
1498
+ } catch (error) {
1499
+ outcome.result = toFailure(error, getTrace(index, successor));
1500
+ }
1501
+ return outcome.result !== undefined;
1502
+ });
1331
1503
  current.isExpanding = false;
1332
1504
  if (outcome.result) return outcome.result;
1333
1505
  if (successorCount === 0 && config.checkDeadlock) {
@@ -1335,32 +1507,43 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1335
1507
  }
1336
1508
  try {
1337
1509
  const accepted = liveness?.addState(index, state, liveSuccessors);
1338
- current.livenessSource = -1;
1339
- if (!accepted) return undefined;
1510
+ if (!liveness || !accepted) return undefined;
1511
+ const steps = getPathSteps(accepted);
1512
+ const properties = liveness.getViolatedProperties(
1513
+ steps.map((step) => step.state),
1514
+ steps.length - 1,
1515
+ );
1340
1516
  return reportViolation("property-violation", {
1341
- name: accepted.name,
1342
- trace: getPathTrace(accepted.path),
1517
+ name: properties[0],
1518
+ properties,
1519
+ trace: toTrace(steps, accepted),
1343
1520
  });
1344
1521
  } catch (error) {
1345
1522
  return toFailure(error, getStateTrace(index), undefined, getPartialOutDegrees());
1346
1523
  }
1347
1524
  };
1348
- const reportLasso = (lasso: Lasso) => {
1525
+ // TLC names the properties and the step back into the cycle from the states it prints.
1526
+ const reportLasso = (checker: LivenessChecker, lasso: Lasso) => {
1349
1527
  const path = [...lasso.prefix, ...lasso.cycle];
1350
- const trace = getPathTrace(path, lasso.prefix.length);
1351
- const back = path[lasso.prefix.length];
1528
+ const loopStart = lasso.prefix.length;
1529
+ const steps = getPathSteps(path, loopStart);
1530
+ const trace = toTrace(steps, path, loopStart);
1531
+ const properties = checker.getViolatedProperties(
1532
+ steps.map((step) => step.state),
1533
+ loopStart,
1534
+ );
1535
+ const back = path[loopStart];
1352
1536
  const last = path[path.length - 1];
1537
+ const lastState = steps[steps.length - 1].state;
1353
1538
  const loopLabel =
1354
- back === last ? undefined : withoutOutput(() => getLabel(last, states[last], states[back]));
1539
+ back === last ? undefined : withoutOutput(() => getLabel(last, lastState, states[back]));
1355
1540
  return reportViolation(
1356
1541
  "liveness-violation",
1357
- { name: lasso.name, trace, loopStart: lasso.prefix.length, loopLabel },
1542
+ { name: properties[0], properties, trace, loopStart, loopLabel },
1358
1543
  () =>
1359
1544
  back === last
1360
1545
  ? trace[trace.length - 1].action
1361
- : getTraceSuccessors(states[last]).find((successor) =>
1362
- isSameState(successor.state, states[back]),
1363
- )?.action,
1546
+ : findSuccessorWithFingerprint(lastState, back)?.action,
1364
1547
  );
1365
1548
  };
1366
1549
  const isPeriodic = liveness !== undefined && options.livenessCheck !== "final";
@@ -1373,11 +1556,9 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1373
1556
  if (!isGrown || periodic.time > (now - startedAt) * LIVENESS_RATIO) return undefined;
1374
1557
  periodic.size = expanded;
1375
1558
  const lasso = liveness?.findViolation();
1376
- current.livenessSource = -1;
1377
1559
  periodic.time += performance.now() - now;
1378
1560
  return lasso;
1379
1561
  };
1380
- setNextStatePhase(true);
1381
1562
  for (let index = 0; index < states.length && !runtime.isStopped; index++) {
1382
1563
  current.index = index;
1383
1564
  current.candidate = undefined;
@@ -1386,22 +1567,21 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1386
1567
  try {
1387
1568
  outcome.result = expandState(index);
1388
1569
  } catch (error) {
1389
- outcome.result = toFailure(error, getStateTrace(index));
1570
+ outcome.result = toFailure(toNextStateError(error), getStateTrace(index));
1390
1571
  }
1391
1572
  if (outcome.result) return outcome.result;
1392
1573
  outDegrees[getOutDegreeBucket()]++;
1393
1574
  expansion.newStates = 0;
1394
1575
  if (isPeriodic && performance.now() >= periodic.at) {
1395
1576
  const lasso = findPeriodicViolation(index + 1);
1396
- if (lasso) return reportLasso(lasso);
1577
+ if (liveness && lasso) return reportLasso(liveness, lasso);
1397
1578
  }
1398
1579
  }
1399
1580
 
1400
1581
  current.index = -1;
1401
1582
  current.candidate = undefined;
1402
1583
  const lasso = liveness?.findViolation();
1403
- current.livenessSource = -1;
1404
- if (lasso) return reportLasso(lasso);
1584
+ if (liveness && lasso) return reportLasso(liveness, lasso);
1405
1585
 
1406
1586
  const postconditionViolation = search.findPostconditionViolation();
1407
1587
  if (postconditionViolation) {
@@ -2,6 +2,8 @@ export interface OptionSpec {
2
2
  flag: string;
3
3
  isRequired?: boolean;
4
4
  isRepeatable?: boolean;
5
+ // How many of the arguments after the flag the option takes, when that depends on them.
6
+ getArity?: (rest: string[]) => number;
5
7
  }
6
8
 
7
9
  export interface ParsedOption {
@@ -29,27 +31,31 @@ export const defineCommand = (usage: string, specs: OptionSpec[]) => {
29
31
  const arities = new Map(
30
32
  specs.map((spec) => {
31
33
  const [name, ...values] = spec.flag.split(" ");
32
- return [name, values.length];
34
+ return [name, spec.getArity ?? (() => values.length)];
33
35
  }),
34
36
  );
35
37
  const parse = (args: string[]): ParsedArgs => {
36
38
  const options: ParsedOption[] = [];
37
39
  const positionals: string[] = [];
38
40
  const unknown: string[] = [];
41
+ const incomplete: ParsedOption[] = [];
39
42
  for (let index = 0; index < args.length; index++) {
40
- const arity = arities.get(args[index]);
41
- if (arity === undefined) {
43
+ const getArity = arities.get(args[index]);
44
+ if (getArity === undefined) {
42
45
  (args[index].startsWith("-") ? unknown : positionals).push(args[index]);
43
46
  continue;
44
47
  }
45
- options.push({ name: args[index], values: args.slice(index + 1, index + 1 + arity) });
48
+ const arity = getArity(args.slice(index + 1));
49
+ const option = { name: args[index], values: args.slice(index + 1, index + 1 + arity) };
50
+ options.push(option);
51
+ if (option.values.length < arity) incomplete.push(option);
46
52
  index += arity;
47
53
  }
48
54
  return {
49
55
  options,
50
56
  positionals,
51
57
  unknown,
52
- incomplete: options.find((option) => option.values.length < (arities.get(option.name) ?? 0)),
58
+ incomplete: incomplete[0],
53
59
  has: (name) => options.some((option) => option.name === name),
54
60
  get: (name) => options.findLast((option) => option.name === name)?.values,
55
61
  };