tla-ts 0.0.3-dev.252cf06 → 0.0.3-dev.27a0c3a
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/README.md +5 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +30 -22
- package/dist/index.d.ts +30 -22
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +17 -17
- package/dist/run-cli.js +6 -6
- package/dist/transfer.cjs +28 -14
- package/dist/transfer.js +28 -14
- package/package.json +1 -1
- package/src/check.ts +259 -61
- package/src/cli-options.ts +11 -5
- package/src/codegen.ts +14 -19
- package/src/config.ts +224 -170
- package/src/environment.ts +84 -14
- package/src/errors.ts +30 -2
- package/src/evaluate.ts +72 -35
- package/src/exit-codes.ts +21 -14
- package/src/instances.ts +3 -3
- package/src/live-check.ts +132 -70
- package/src/liveness.ts +113 -22
- package/src/parallel-worker.ts +2 -2
- package/src/parallel.ts +5 -2
- package/src/run-cli.ts +49 -11
- package/src/runtime.ts +2 -0
- package/src/spec-description.ts +11 -4
- package/src/standard-modules.ts +2 -7
- package/src/state-graph.ts +157 -0
- package/src/successors.ts +84 -18
- package/src/symmetry.ts +16 -6
- package/src/tableau.ts +51 -24
- package/src/tlc-ext.ts +27 -2
- package/src/tlc-module.ts +14 -33
- package/src/tlc-modules.ts +4 -0
- package/src/tlc-order.ts +8 -4
- package/src/tlc-random.ts +9 -6
- package/src/tlc-summary.ts +20 -5
- package/src/values.ts +121 -20
package/package.json
CHANGED
package/src/check.ts
CHANGED
|
@@ -11,12 +11,15 @@ import { getReadVariables } from "./dependencies";
|
|
|
11
11
|
import { createEnvironment } from "./environment";
|
|
12
12
|
import {
|
|
13
13
|
getErrorExitCode,
|
|
14
|
+
isStackOverflow,
|
|
15
|
+
STACK_OVERFLOW,
|
|
14
16
|
TlaConfigError,
|
|
15
17
|
TlaEvaluationError,
|
|
18
|
+
TlaIncompleteStateError,
|
|
16
19
|
TlaModuleError,
|
|
17
20
|
UNEXPECTED_EXCEPTION,
|
|
18
21
|
} from "./errors";
|
|
19
|
-
import { EXIT_STATUS, setExitCode,
|
|
22
|
+
import { EXIT_STATUS, setExitCode, withLivenessExitCode } from "./exit-codes";
|
|
20
23
|
import {
|
|
21
24
|
describeRange,
|
|
22
25
|
evaluate,
|
|
@@ -47,12 +50,19 @@ import {
|
|
|
47
50
|
} from "./successors";
|
|
48
51
|
import {
|
|
49
52
|
getActionLabel,
|
|
53
|
+
getActionSignature,
|
|
50
54
|
getNextActions,
|
|
51
55
|
getSpecificationValue,
|
|
52
56
|
type SpecAction,
|
|
53
57
|
} from "./spec-description";
|
|
58
|
+
import {
|
|
59
|
+
createStateWriter,
|
|
60
|
+
type GraphState,
|
|
61
|
+
type StateDump,
|
|
62
|
+
type StateWriter,
|
|
63
|
+
} from "./state-graph";
|
|
54
64
|
import { getRepresentative, getSymmetryGroup } from "./symmetry";
|
|
55
|
-
import { getTlcStringOrder,
|
|
65
|
+
import { getTlcStringOrder, getTlcDeclarationOrder } from "./tlc-order";
|
|
56
66
|
import { createTlcFingerprinter, getTlcFingerprint } from "./tlc-random";
|
|
57
67
|
import { createCanonicalizer } from "./canonical";
|
|
58
68
|
import { getPossibleCounts, type CheckerRuntime } from "./runtime";
|
|
@@ -60,6 +70,8 @@ import { createCounterExample } from "./tlc-ext";
|
|
|
60
70
|
import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
|
|
61
71
|
import {
|
|
62
72
|
combineHash,
|
|
73
|
+
createModelValue,
|
|
74
|
+
formatState,
|
|
63
75
|
formatValue,
|
|
64
76
|
getValueHash,
|
|
65
77
|
isFunctionValue,
|
|
@@ -83,6 +95,8 @@ export interface CheckOptions {
|
|
|
83
95
|
workers?: number;
|
|
84
96
|
// TLC's -lncheck: "final" checks liveness only on the complete state graph.
|
|
85
97
|
livenessCheck?: "default" | "final";
|
|
98
|
+
// TLC's -dump: writes the reachable states, or with `dot` the state graph, through writeFile.
|
|
99
|
+
dump?: StateDump;
|
|
86
100
|
}
|
|
87
101
|
|
|
88
102
|
export type CheckStatus =
|
|
@@ -237,7 +251,7 @@ const getConstantValue = (name: string, definition: Definition, environment: Env
|
|
|
237
251
|
try {
|
|
238
252
|
return evaluate(createIdentifier(name), getContext(environment, undefined, undefined));
|
|
239
253
|
} catch (error) {
|
|
240
|
-
if (error instanceof TlaEvaluationError) return undefined;
|
|
254
|
+
if (error instanceof TlaEvaluationError || isStackOverflow(error)) return undefined;
|
|
241
255
|
throw error;
|
|
242
256
|
}
|
|
243
257
|
};
|
|
@@ -266,8 +280,11 @@ const getInvariants = (names: string[], environment: Environment) => {
|
|
|
266
280
|
levels.params.size === 0
|
|
267
281
|
? ""
|
|
268
282
|
: "\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.";
|
|
269
|
-
throw
|
|
270
|
-
|
|
283
|
+
throw setExitCode(
|
|
284
|
+
new TlaConfigError(
|
|
285
|
+
`The invariant ${name} is not a state predicate (one with no primes or temporal operators).${note}`,
|
|
286
|
+
),
|
|
287
|
+
EXIT_STATUS.safetyEvaluationFailure,
|
|
271
288
|
);
|
|
272
289
|
}
|
|
273
290
|
return [{ name, expression: createIdentifier(name), scope: undefined }];
|
|
@@ -365,8 +382,11 @@ const getSpecification = (config: ModelConfig, environment: Environment): Specif
|
|
|
365
382
|
conjunct.kind === "Always" &&
|
|
366
383
|
getLevel(conjunct.expression, item.scope, environment) < ACTION_LEVEL
|
|
367
384
|
) {
|
|
368
|
-
throw
|
|
369
|
-
|
|
385
|
+
throw setExitCode(
|
|
386
|
+
new TlaConfigError(
|
|
387
|
+
`TLC cannot handle the temporal formula ${describeRange(conjunct.expression)}`,
|
|
388
|
+
),
|
|
389
|
+
EXIT_STATUS.error,
|
|
370
390
|
);
|
|
371
391
|
}
|
|
372
392
|
const level = getLevel(conjunct, item.scope, environment);
|
|
@@ -436,12 +456,12 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
|
|
|
436
456
|
config.properties.forEach((name) => {
|
|
437
457
|
const definition = getConfigPredicate("property", name, environment);
|
|
438
458
|
if (!definition) return;
|
|
439
|
-
expandTemporal(definition.body, undefined, environment, true).forEach((item) => {
|
|
459
|
+
expandTemporal(definition.body, undefined, environment, true, name).forEach((item) => {
|
|
440
460
|
const conjunct = item.expression;
|
|
441
461
|
if (isConstantTrue(item, environment, "property")) return;
|
|
442
462
|
if (conjunct.kind === "Always" && conjunct.expression.kind === "BoxAction") {
|
|
443
463
|
properties.actionProperties.push({
|
|
444
|
-
name,
|
|
464
|
+
name: item.name,
|
|
445
465
|
box: conjunct.expression,
|
|
446
466
|
action: conjunct.expression.action,
|
|
447
467
|
subscript: conjunct.expression.subscript,
|
|
@@ -453,12 +473,22 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
|
|
|
453
473
|
conjunct.kind === "Always" &&
|
|
454
474
|
getLevel(conjunct.expression, item.scope, environment) <= 1
|
|
455
475
|
) {
|
|
456
|
-
|
|
476
|
+
// TLC names the invariant []P after P when P is an operator name.
|
|
477
|
+
const expression = conjunct.expression;
|
|
478
|
+
properties.invariants.push({
|
|
479
|
+
name: expression.kind === "Identifier" ? expression.name : item.name,
|
|
480
|
+
expression,
|
|
481
|
+
scope: item.scope,
|
|
482
|
+
});
|
|
457
483
|
return;
|
|
458
484
|
}
|
|
459
485
|
const level = getLevel(conjunct, item.scope, environment);
|
|
460
486
|
if (level < ACTION_LEVEL) {
|
|
461
|
-
properties.initialPredicates.push({
|
|
487
|
+
properties.initialPredicates.push({
|
|
488
|
+
name: item.name,
|
|
489
|
+
expression: conjunct,
|
|
490
|
+
scope: item.scope,
|
|
491
|
+
});
|
|
462
492
|
return;
|
|
463
493
|
}
|
|
464
494
|
if (level === ACTION_LEVEL) {
|
|
@@ -518,7 +548,9 @@ const getMembership = (
|
|
|
518
548
|
const getStateChecks = (named: NamedExpression[], environment: Environment): StateCheck[] =>
|
|
519
549
|
named.flatMap((item) =>
|
|
520
550
|
getConjuncts(item.expression, environment).map((expression) => {
|
|
521
|
-
|
|
551
|
+
// A LET or an argument in the scope can read variables the expression doesn't name.
|
|
552
|
+
const variables =
|
|
553
|
+
item.scope === undefined ? getReadVariables(expression, environment) : undefined;
|
|
522
554
|
return {
|
|
523
555
|
name: item.name,
|
|
524
556
|
expression,
|
|
@@ -765,11 +797,13 @@ export const createSearch = (
|
|
|
765
797
|
if (view !== undefined || isFingerprintedLikeTlc) {
|
|
766
798
|
return BigInt64Array.from(states, getTlcStateFingerprint);
|
|
767
799
|
}
|
|
768
|
-
const
|
|
769
|
-
const
|
|
770
|
-
|
|
771
|
-
|
|
800
|
+
const fingerprints = new BigInt64Array(fingerprintStates.length);
|
|
801
|
+
const fingerprint = createTlcFingerprinter(
|
|
802
|
+
getTlcOrder(),
|
|
803
|
+
new Int32Array(fingerprints.buffer),
|
|
772
804
|
);
|
|
805
|
+
fingerprintStates.forEach(fingerprint);
|
|
806
|
+
return fingerprints;
|
|
773
807
|
},
|
|
774
808
|
getFingerprintState: (state) => {
|
|
775
809
|
if (isFingerprintedLikeTlc) return [String(getTlcStateFingerprint(state))];
|
|
@@ -801,11 +835,17 @@ export const createSearch = (
|
|
|
801
835
|
}
|
|
802
836
|
}),
|
|
803
837
|
satisfiesConstraints: (state, parent) =>
|
|
804
|
-
constraints.every(
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
838
|
+
constraints.every((constraint) => {
|
|
839
|
+
try {
|
|
840
|
+
return (
|
|
841
|
+
isUnchangedFor(constraint, state, parent) ||
|
|
842
|
+
holdsLikeTlc(constraint.expression, getContext(environment, state, undefined))
|
|
843
|
+
);
|
|
844
|
+
} catch (error) {
|
|
845
|
+
// TLC reports a failing constraint on a successor like a failure of the next-state relation.
|
|
846
|
+
throw parent ? toNextStateError(error) : error;
|
|
847
|
+
}
|
|
848
|
+
}),
|
|
809
849
|
findFailedInitialPredicate: (state) =>
|
|
810
850
|
properties.initialPredicates.find(
|
|
811
851
|
(predicate) =>
|
|
@@ -831,17 +871,20 @@ export const createSearch = (
|
|
|
831
871
|
)
|
|
832
872
|
);
|
|
833
873
|
} catch (error) {
|
|
834
|
-
throw withHeadline(
|
|
874
|
+
throw withHeadline(
|
|
875
|
+
withNextStateExitCode(error),
|
|
876
|
+
`Evaluating action property ${property.name} failed.`,
|
|
877
|
+
);
|
|
835
878
|
}
|
|
836
879
|
}),
|
|
837
880
|
satisfiesActionConstraints: (state, next) =>
|
|
838
|
-
actionConstraints.every((constraint) =>
|
|
839
|
-
|
|
840
|
-
constraint.expression,
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
),
|
|
881
|
+
actionConstraints.every((constraint) => {
|
|
882
|
+
try {
|
|
883
|
+
return holdsLikeTlc(constraint.expression, getContext(environment, state, next));
|
|
884
|
+
} catch (error) {
|
|
885
|
+
throw toNextStateError(error);
|
|
886
|
+
}
|
|
887
|
+
}),
|
|
845
888
|
trackStatePossible:
|
|
846
889
|
statePossible.length === 0
|
|
847
890
|
? ignorePossible
|
|
@@ -867,6 +910,23 @@ export const createSearch = (
|
|
|
867
910
|
};
|
|
868
911
|
};
|
|
869
912
|
|
|
913
|
+
// TLC exits with the code of a standard module's failure only when the failure escapes the
|
|
914
|
+
// computation of next states. In every other phase, the phase decides the code.
|
|
915
|
+
const withNextStateExitCode = (error: unknown) =>
|
|
916
|
+
error instanceof TlaModuleError ? setExitCode(error, error.exitCode) : error;
|
|
917
|
+
|
|
918
|
+
// TLC reports a failure of the next-state relation as an unexpected exception, unless a standard
|
|
919
|
+
// module raised it as an EvalException.
|
|
920
|
+
const toNextStateError = (error: unknown) =>
|
|
921
|
+
error instanceof TlaEvaluationError &&
|
|
922
|
+
!(error instanceof TlaModuleError) &&
|
|
923
|
+
!(error instanceof TlaIncompleteStateError)
|
|
924
|
+
? setExitCode(
|
|
925
|
+
new TlaEvaluationError(`${UNEXPECTED_EXCEPTION}${error.message}`, error),
|
|
926
|
+
getErrorExitCode(error),
|
|
927
|
+
)
|
|
928
|
+
: withNextStateExitCode(error);
|
|
929
|
+
|
|
870
930
|
const withHeadline = (error: unknown, headline: string) =>
|
|
871
931
|
error instanceof TlaEvaluationError
|
|
872
932
|
? setExitCode(
|
|
@@ -875,8 +935,22 @@ const withHeadline = (error: unknown, headline: string) =>
|
|
|
875
935
|
)
|
|
876
936
|
: error;
|
|
877
937
|
|
|
938
|
+
// TLC finishes the -dump file whether or not the search finds a violation.
|
|
878
939
|
export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
879
|
-
|
|
940
|
+
const stateWriter = options.dump && createStateWriter(options.dump, options.writeFile);
|
|
941
|
+
try {
|
|
942
|
+
return searchStates(options, stateWriter);
|
|
943
|
+
} catch (error) {
|
|
944
|
+
// TLC reports a stack overflow outside the search as a failure of its own, with exit code 255.
|
|
945
|
+
throw isStackOverflow(error)
|
|
946
|
+
? setExitCode(new TlaEvaluationError(STACK_OVERFLOW), EXIT_STATUS.error)
|
|
947
|
+
: error;
|
|
948
|
+
} finally {
|
|
949
|
+
stateWriter?.close();
|
|
950
|
+
}
|
|
951
|
+
};
|
|
952
|
+
|
|
953
|
+
const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefined): CheckResult => {
|
|
880
954
|
const config = typeof options.config === "string" ? parseConfig(options.config) : options.config;
|
|
881
955
|
const onPrint = options.onPrint ?? console.log;
|
|
882
956
|
// TLC prints again while it rebuilds a trace, between the states it prints. Printing those lines
|
|
@@ -946,7 +1020,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
946
1020
|
});
|
|
947
1021
|
|
|
948
1022
|
const search = createSearch(config, environment, () =>
|
|
949
|
-
|
|
1023
|
+
getTlcDeclarationOrder(options.source, options.loadModule, "variable"),
|
|
950
1024
|
);
|
|
951
1025
|
const assumptionViolation = findAssumptionViolation(specification.assumptions, environment);
|
|
952
1026
|
if (assumptionViolation) return createResult("assumption-violation", assumptionViolation);
|
|
@@ -1010,7 +1084,9 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1010
1084
|
const parents: number[] = [];
|
|
1011
1085
|
const actions: (string | undefined)[] = [];
|
|
1012
1086
|
const depths: number[] = [];
|
|
1013
|
-
|
|
1087
|
+
// A Map holds at most 2^24 entries, so an open-addressing table keeps each seen state's hash
|
|
1088
|
+
// and its index plus one side by side, probed by the hash.
|
|
1089
|
+
const seen = { slots: new Int32Array(1 << 17), size: 0, shift: 16 };
|
|
1014
1090
|
const canonicalizeState = createCanonicalizer();
|
|
1015
1091
|
|
|
1016
1092
|
const toRecord = (state: Value[]): Record<string, Value> =>
|
|
@@ -1023,11 +1099,16 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1023
1099
|
position: number,
|
|
1024
1100
|
loopStart: number | undefined,
|
|
1025
1101
|
) => {
|
|
1026
|
-
const state = steps[position].state;
|
|
1027
|
-
const next = (steps[position + 1] ?? steps[loopStart ?? position]).state;
|
|
1028
1102
|
// TLC evaluates TLCGet("level") and Trace in an alias at the state's position in the trace.
|
|
1029
1103
|
runtime.getLevel = () => position + 1;
|
|
1030
1104
|
runtime.getBehavior = () => steps.slice(0, position + 1).map((step) => step.state);
|
|
1105
|
+
return evaluateAlias(
|
|
1106
|
+
alias,
|
|
1107
|
+
steps[position].state,
|
|
1108
|
+
(steps[position + 1] ?? steps[loopStart ?? position]).state,
|
|
1109
|
+
);
|
|
1110
|
+
};
|
|
1111
|
+
const evaluateAlias = (alias: string, state: Value[], next: Value[] | undefined) => {
|
|
1031
1112
|
try {
|
|
1032
1113
|
const body = getConfigDefinition("alias", alias, environment).body;
|
|
1033
1114
|
const value = evaluate(createIdentifier(alias), getContext(environment, state, next));
|
|
@@ -1129,7 +1210,17 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1129
1210
|
trace: TraceStep[],
|
|
1130
1211
|
initialState?: Value[],
|
|
1131
1212
|
failureOutDegrees?: number[],
|
|
1132
|
-
) => {
|
|
1213
|
+
): CheckResult => {
|
|
1214
|
+
// TLC exits with 255 on a stack overflow while it computes next states, and prints the
|
|
1215
|
+
// behavior up to the state it was expanding.
|
|
1216
|
+
if (isStackOverflow(error)) {
|
|
1217
|
+
return current.isExpanding
|
|
1218
|
+
? toFailure(
|
|
1219
|
+
setExitCode(new TlaEvaluationError(STACK_OVERFLOW), EXIT_STATUS.error),
|
|
1220
|
+
getStateTrace(current.index),
|
|
1221
|
+
)
|
|
1222
|
+
: toFailure(new TlaEvaluationError(STACK_OVERFLOW), trace, initialState);
|
|
1223
|
+
}
|
|
1133
1224
|
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
1134
1225
|
return {
|
|
1135
1226
|
...createResult("error", undefined, {
|
|
@@ -1165,19 +1256,35 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1165
1256
|
}
|
|
1166
1257
|
return true;
|
|
1167
1258
|
};
|
|
1259
|
+
const getSlot = (hash: number) => (Math.imul(hash, 0x9e3779b1) >>> seen.shift) << 1;
|
|
1168
1260
|
const findStateIndex = (hash: number, state: Value[]) => {
|
|
1169
|
-
const
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1261
|
+
const mask = seen.slots.length - 1;
|
|
1262
|
+
for (let slot = getSlot(hash); ; slot = (slot + 2) & mask) {
|
|
1263
|
+
const entry = seen.slots[slot + 1];
|
|
1264
|
+
if (entry === 0) return -1;
|
|
1265
|
+
if (seen.slots[slot] === hash && isSameState(fingerprintStates[entry - 1], state)) {
|
|
1266
|
+
return entry - 1;
|
|
1267
|
+
}
|
|
1173
1268
|
}
|
|
1174
|
-
|
|
1269
|
+
};
|
|
1270
|
+
const insertSeen = (hash: number, entry: number) => {
|
|
1271
|
+
const mask = seen.slots.length - 1;
|
|
1272
|
+
let slot = getSlot(hash);
|
|
1273
|
+
while (seen.slots[slot + 1] !== 0) slot = (slot + 2) & mask;
|
|
1274
|
+
seen.slots[slot] = hash;
|
|
1275
|
+
seen.slots[slot + 1] = entry;
|
|
1175
1276
|
};
|
|
1176
1277
|
const markSeen = (hash: number, index: number) => {
|
|
1177
|
-
|
|
1178
|
-
if (
|
|
1179
|
-
|
|
1180
|
-
|
|
1278
|
+
seen.size++;
|
|
1279
|
+
if (seen.size * 4 > seen.slots.length) {
|
|
1280
|
+
const slots = seen.slots;
|
|
1281
|
+
seen.slots = new Int32Array(slots.length * 2);
|
|
1282
|
+
seen.shift--;
|
|
1283
|
+
for (let slot = 0; slot < slots.length; slot += 2) {
|
|
1284
|
+
if (slots[slot + 1] !== 0) insertSeen(slots[slot], slots[slot + 1]);
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
insertSeen(hash, index + 1);
|
|
1181
1288
|
};
|
|
1182
1289
|
const countDistinctState = (depth: number) => {
|
|
1183
1290
|
runtime.distinctStates = states.length + 1;
|
|
@@ -1203,6 +1310,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1203
1310
|
isExpanding: false,
|
|
1204
1311
|
candidate: undefined as Value[] | undefined,
|
|
1205
1312
|
candidateLevel: 0,
|
|
1313
|
+
isFingerprinting: false,
|
|
1206
1314
|
livenessSource: -1,
|
|
1207
1315
|
};
|
|
1208
1316
|
const getPath = (index: number): Value[][] =>
|
|
@@ -1228,6 +1336,18 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1228
1336
|
return current.isExpanding ? depths[current.index] + 1 : 0;
|
|
1229
1337
|
};
|
|
1230
1338
|
|
|
1339
|
+
const getGraphState = (state: Value[], level: number): GraphState => ({
|
|
1340
|
+
fingerprint: BigInt.asIntN(64, runtime.getFingerprint(state)),
|
|
1341
|
+
level,
|
|
1342
|
+
getLabel: () =>
|
|
1343
|
+
formatState(
|
|
1344
|
+
config.alias === undefined
|
|
1345
|
+
? toRecord(state)
|
|
1346
|
+
: evaluateAlias(config.alias, state, undefined),
|
|
1347
|
+
),
|
|
1348
|
+
getText: () => formatState(toRecord(state)),
|
|
1349
|
+
});
|
|
1350
|
+
|
|
1231
1351
|
const visitInitialState = (state: Value[]) => {
|
|
1232
1352
|
canonicalizeState(state);
|
|
1233
1353
|
runtime.generatedStates++;
|
|
@@ -1244,6 +1364,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1244
1364
|
countDistinctState(1);
|
|
1245
1365
|
runtime.queueSize++;
|
|
1246
1366
|
liveness?.addInitialState(states.length, state);
|
|
1367
|
+
stateWriter?.writeInitialState(getGraphState(state, 1));
|
|
1247
1368
|
}
|
|
1248
1369
|
const violated = search.findViolatedInvariant(state);
|
|
1249
1370
|
if (violated) {
|
|
@@ -1264,20 +1385,36 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1264
1385
|
addState(state, fingerprintState, -1, undefined);
|
|
1265
1386
|
return undefined;
|
|
1266
1387
|
};
|
|
1267
|
-
const visitSuccessor = (
|
|
1388
|
+
const visitSuccessor = (
|
|
1389
|
+
index: number,
|
|
1390
|
+
successor: Successor,
|
|
1391
|
+
liveSuccessors: LiveSuccessor[],
|
|
1392
|
+
source: GraphState | undefined,
|
|
1393
|
+
action: SpecAction | undefined,
|
|
1394
|
+
) => {
|
|
1268
1395
|
const state = states[index];
|
|
1269
1396
|
runtime.generatedStates++;
|
|
1270
1397
|
current.candidate = successor.state;
|
|
1271
1398
|
current.candidateLevel = depths[index] + 1;
|
|
1272
1399
|
if (isCanonicalizedFirst) canonicalizeState(successor.state);
|
|
1400
|
+
current.isFingerprinting = true;
|
|
1273
1401
|
const fingerprintState = getFingerprintState(successor.state);
|
|
1274
1402
|
const hash = getStateHash(fingerprintState);
|
|
1403
|
+
current.isFingerprinting = false;
|
|
1275
1404
|
const found = findStateIndex(hash, fingerprintState);
|
|
1276
1405
|
const isStateInModel = search.satisfiesConstraints(successor.state, state);
|
|
1277
1406
|
if (isStateInModel) search.trackStatePossible(successor.state);
|
|
1278
1407
|
const isInModel = isStateInModel && search.satisfiesActionConstraints(state, successor.state);
|
|
1279
1408
|
if (isInModel) search.trackActionPossible(state, successor.state);
|
|
1280
1409
|
const existing = isInModel ? found : -1;
|
|
1410
|
+
if (source && isInModel) {
|
|
1411
|
+
stateWriter?.writeSuccessor(
|
|
1412
|
+
source,
|
|
1413
|
+
getGraphState(successor.state, depths[index] + 1),
|
|
1414
|
+
existing !== -1,
|
|
1415
|
+
action && { name: action.name, signature: getActionSignature(action) },
|
|
1416
|
+
);
|
|
1417
|
+
}
|
|
1281
1418
|
if (existing !== -1 && liveness) {
|
|
1282
1419
|
const stored = states[existing];
|
|
1283
1420
|
const isStored = stored.every((value, position) => value === successor.state[position]);
|
|
@@ -1349,23 +1486,57 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1349
1486
|
}
|
|
1350
1487
|
return createSuccess();
|
|
1351
1488
|
}
|
|
1489
|
+
// TLC labels and colors an edge of the -dump state graph by the action that generated it. When
|
|
1490
|
+
// the graph names its edges, the search generates successors action by action, as TLC does.
|
|
1491
|
+
const isNamingEdges = Boolean(
|
|
1492
|
+
options.dump?.dot && (options.dump.actionLabels || options.dump.colorize),
|
|
1493
|
+
);
|
|
1494
|
+
const forEachActionSuccessor = (
|
|
1495
|
+
state: Value[],
|
|
1496
|
+
onSuccessor: (successor: Successor, action: SpecAction | undefined) => boolean,
|
|
1497
|
+
) => {
|
|
1498
|
+
if (!isNamingEdges) {
|
|
1499
|
+
return forEachSuccessor(
|
|
1500
|
+
next,
|
|
1501
|
+
state,
|
|
1502
|
+
environment,
|
|
1503
|
+
(successor) => onSuccessor(successor, undefined),
|
|
1504
|
+
spec.scope,
|
|
1505
|
+
);
|
|
1506
|
+
}
|
|
1507
|
+
nextActions.value ??= getNextActions(config, environment, specification.instanceActions);
|
|
1508
|
+
const progress = { isStopped: false };
|
|
1509
|
+
return nextActions.value.reduce((count, action) => {
|
|
1510
|
+
if (progress.isStopped) return count;
|
|
1511
|
+
return (
|
|
1512
|
+
count +
|
|
1513
|
+
forEachSuccessor(
|
|
1514
|
+
action.expression,
|
|
1515
|
+
state,
|
|
1516
|
+
environment,
|
|
1517
|
+
(successor) => {
|
|
1518
|
+
progress.isStopped = onSuccessor(successor, action);
|
|
1519
|
+
return progress.isStopped;
|
|
1520
|
+
},
|
|
1521
|
+
action.scope,
|
|
1522
|
+
)
|
|
1523
|
+
);
|
|
1524
|
+
}, 0);
|
|
1525
|
+
};
|
|
1352
1526
|
const expandState = (index: number) => {
|
|
1353
1527
|
const state = states[index];
|
|
1354
1528
|
const liveSuccessors: LiveSuccessor[] = [];
|
|
1355
|
-
const
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
(
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
},
|
|
1367
|
-
spec.scope,
|
|
1368
|
-
);
|
|
1529
|
+
const source = stateWriter && getGraphState(state, depths[index]);
|
|
1530
|
+
const successorCount = forEachActionSuccessor(state, (successor, action) => {
|
|
1531
|
+
try {
|
|
1532
|
+
outcome.result = visitSuccessor(index, successor, liveSuccessors, source, action);
|
|
1533
|
+
} catch (error) {
|
|
1534
|
+
// TLC reports a successor it cannot fingerprint as a failure of the state it expands.
|
|
1535
|
+
if (current.isFingerprinting) throw error;
|
|
1536
|
+
outcome.result = toFailure(error, getTrace(index, successor));
|
|
1537
|
+
}
|
|
1538
|
+
return outcome.result !== undefined;
|
|
1539
|
+
});
|
|
1369
1540
|
current.isExpanding = false;
|
|
1370
1541
|
if (outcome.result) return outcome.result;
|
|
1371
1542
|
if (successorCount === 0 && config.checkDeadlock) {
|
|
@@ -1388,6 +1559,31 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1388
1559
|
return toFailure(error, getStateTrace(index), undefined, getPartialOutDegrees());
|
|
1389
1560
|
}
|
|
1390
1561
|
};
|
|
1562
|
+
// TLC names the action when the next-state relation has more than one, and prints the
|
|
1563
|
+
// incomplete successor with null for each variable it leaves undefined.
|
|
1564
|
+
const reportIncompleteSuccessor = (error: TlaIncompleteStateError, index: number) => {
|
|
1565
|
+
nextActions.value ??= getNextActions(config, environment, specification.instanceActions);
|
|
1566
|
+
const actions = nextActions.value;
|
|
1567
|
+
const action =
|
|
1568
|
+
actions.length === 1 ? actions[0] : actions.find((item) => item.name === error.action);
|
|
1569
|
+
const variables = `variable${error.missing.length > 1 ? "s are" : " is"} not defined: ${error.missing.join(", ")}.\n`;
|
|
1570
|
+
runtime.generatedStates++;
|
|
1571
|
+
return toFailure(
|
|
1572
|
+
actions.length === 1
|
|
1573
|
+
? error
|
|
1574
|
+
: new TlaEvaluationError(
|
|
1575
|
+
`Successor state is not completely specified by action ${error.action} of the next-state relation. The following ${variables}`,
|
|
1576
|
+
),
|
|
1577
|
+
[
|
|
1578
|
+
...getStateTrace(index),
|
|
1579
|
+
{
|
|
1580
|
+
action: error.action,
|
|
1581
|
+
label: action && getActionLabel(action),
|
|
1582
|
+
state: toRecord(error.state.map((value) => value ?? createModelValue("null"))),
|
|
1583
|
+
},
|
|
1584
|
+
],
|
|
1585
|
+
);
|
|
1586
|
+
};
|
|
1391
1587
|
// TLC names the properties and the step back into the cycle from the states it prints.
|
|
1392
1588
|
const reportLasso = (checker: LivenessChecker, lasso: Lasso) => {
|
|
1393
1589
|
const path = [...lasso.prefix, ...lasso.cycle];
|
|
@@ -1425,7 +1621,6 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1425
1621
|
periodic.time += performance.now() - now;
|
|
1426
1622
|
return lasso;
|
|
1427
1623
|
};
|
|
1428
|
-
setNextStatePhase(true);
|
|
1429
1624
|
for (let index = 0; index < states.length && !runtime.isStopped; index++) {
|
|
1430
1625
|
current.index = index;
|
|
1431
1626
|
current.candidate = undefined;
|
|
@@ -1434,7 +1629,10 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1434
1629
|
try {
|
|
1435
1630
|
outcome.result = expandState(index);
|
|
1436
1631
|
} catch (error) {
|
|
1437
|
-
outcome.result =
|
|
1632
|
+
outcome.result =
|
|
1633
|
+
error instanceof TlaIncompleteStateError
|
|
1634
|
+
? reportIncompleteSuccessor(error, index)
|
|
1635
|
+
: toFailure(toNextStateError(error), getStateTrace(index));
|
|
1438
1636
|
}
|
|
1439
1637
|
if (outcome.result) return outcome.result;
|
|
1440
1638
|
outDegrees[getOutDegreeBucket()]++;
|
package/src/cli-options.ts
CHANGED
|
@@ -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
|
|
41
|
-
if (
|
|
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
|
-
|
|
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:
|
|
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
|
};
|