tla-ts 0.0.3-dev.e927a09 → 0.0.3-dev.eba78ea
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 +8 -0
- package/dist/index.d.ts +8 -0
- 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 +24 -16
- package/dist/transfer.js +24 -16
- package/package.json +1 -1
- package/src/check.ts +141 -33
- package/src/cli-options.ts +11 -5
- package/src/environment.ts +65 -6
- package/src/errors.ts +4 -2
- package/src/evaluate.ts +9 -6
- package/src/exit-codes.ts +1 -13
- package/src/instances.ts +3 -3
- package/src/parallel.ts +3 -0
- 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 +49 -0
- package/src/tlc-ext.ts +27 -2
- package/src/tlc-module.ts +9 -28
- package/src/values.ts +33 -7
package/package.json
CHANGED
package/src/check.ts
CHANGED
|
@@ -16,7 +16,7 @@ import {
|
|
|
16
16
|
TlaModuleError,
|
|
17
17
|
UNEXPECTED_EXCEPTION,
|
|
18
18
|
} from "./errors";
|
|
19
|
-
import { EXIT_STATUS, setExitCode,
|
|
19
|
+
import { EXIT_STATUS, setExitCode, withLivenessExitCode } from "./exit-codes";
|
|
20
20
|
import {
|
|
21
21
|
describeRange,
|
|
22
22
|
evaluate,
|
|
@@ -47,10 +47,17 @@ import {
|
|
|
47
47
|
} from "./successors";
|
|
48
48
|
import {
|
|
49
49
|
getActionLabel,
|
|
50
|
+
getActionSignature,
|
|
50
51
|
getNextActions,
|
|
51
52
|
getSpecificationValue,
|
|
52
53
|
type SpecAction,
|
|
53
54
|
} from "./spec-description";
|
|
55
|
+
import {
|
|
56
|
+
createStateWriter,
|
|
57
|
+
type GraphState,
|
|
58
|
+
type StateDump,
|
|
59
|
+
type StateWriter,
|
|
60
|
+
} from "./state-graph";
|
|
54
61
|
import { getRepresentative, getSymmetryGroup } from "./symmetry";
|
|
55
62
|
import { getTlcStringOrder, getTlcVariableOrder } from "./tlc-order";
|
|
56
63
|
import { createTlcFingerprinter, getTlcFingerprint } from "./tlc-random";
|
|
@@ -60,6 +67,7 @@ import { createCounterExample } from "./tlc-ext";
|
|
|
60
67
|
import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
|
|
61
68
|
import {
|
|
62
69
|
combineHash,
|
|
70
|
+
formatState,
|
|
63
71
|
formatValue,
|
|
64
72
|
getValueHash,
|
|
65
73
|
isFunctionValue,
|
|
@@ -83,6 +91,8 @@ export interface CheckOptions {
|
|
|
83
91
|
workers?: number;
|
|
84
92
|
// TLC's -lncheck: "final" checks liveness only on the complete state graph.
|
|
85
93
|
livenessCheck?: "default" | "final";
|
|
94
|
+
// TLC's -dump: writes the reachable states, or with `dot` the state graph, through writeFile.
|
|
95
|
+
dump?: StateDump;
|
|
86
96
|
}
|
|
87
97
|
|
|
88
98
|
export type CheckStatus =
|
|
@@ -518,7 +528,9 @@ const getMembership = (
|
|
|
518
528
|
const getStateChecks = (named: NamedExpression[], environment: Environment): StateCheck[] =>
|
|
519
529
|
named.flatMap((item) =>
|
|
520
530
|
getConjuncts(item.expression, environment).map((expression) => {
|
|
521
|
-
|
|
531
|
+
// A LET or an argument in the scope can read variables the expression doesn't name.
|
|
532
|
+
const variables =
|
|
533
|
+
item.scope === undefined ? getReadVariables(expression, environment) : undefined;
|
|
522
534
|
return {
|
|
523
535
|
name: item.name,
|
|
524
536
|
expression,
|
|
@@ -831,7 +843,10 @@ export const createSearch = (
|
|
|
831
843
|
)
|
|
832
844
|
);
|
|
833
845
|
} catch (error) {
|
|
834
|
-
throw withHeadline(
|
|
846
|
+
throw withHeadline(
|
|
847
|
+
withNextStateExitCode(error),
|
|
848
|
+
`Evaluating action property ${property.name} failed.`,
|
|
849
|
+
);
|
|
835
850
|
}
|
|
836
851
|
}),
|
|
837
852
|
satisfiesActionConstraints: (state, next) =>
|
|
@@ -867,6 +882,11 @@ export const createSearch = (
|
|
|
867
882
|
};
|
|
868
883
|
};
|
|
869
884
|
|
|
885
|
+
// TLC exits with the code of a standard module's failure only when the failure escapes the
|
|
886
|
+
// computation of next states. In every other phase, the phase decides the code.
|
|
887
|
+
const withNextStateExitCode = (error: unknown) =>
|
|
888
|
+
error instanceof TlaModuleError ? setExitCode(error, error.exitCode) : error;
|
|
889
|
+
|
|
870
890
|
const withHeadline = (error: unknown, headline: string) =>
|
|
871
891
|
error instanceof TlaEvaluationError
|
|
872
892
|
? setExitCode(
|
|
@@ -875,8 +895,15 @@ const withHeadline = (error: unknown, headline: string) =>
|
|
|
875
895
|
)
|
|
876
896
|
: error;
|
|
877
897
|
|
|
898
|
+
// TLC finishes the -dump file whether or not the search finds a violation.
|
|
878
899
|
export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
879
|
-
|
|
900
|
+
const stateWriter = options.dump && createStateWriter(options.dump, options.writeFile);
|
|
901
|
+
const result = searchStates(options, stateWriter);
|
|
902
|
+
stateWriter?.close();
|
|
903
|
+
return result;
|
|
904
|
+
};
|
|
905
|
+
|
|
906
|
+
const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefined): CheckResult => {
|
|
880
907
|
const config = typeof options.config === "string" ? parseConfig(options.config) : options.config;
|
|
881
908
|
const onPrint = options.onPrint ?? console.log;
|
|
882
909
|
// TLC prints again while it rebuilds a trace, between the states it prints. Printing those lines
|
|
@@ -1010,7 +1037,9 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1010
1037
|
const parents: number[] = [];
|
|
1011
1038
|
const actions: (string | undefined)[] = [];
|
|
1012
1039
|
const depths: number[] = [];
|
|
1013
|
-
|
|
1040
|
+
// A Map holds at most 2^24 entries, so an open-addressing table keeps each seen state's hash
|
|
1041
|
+
// and its index plus one side by side, probed by the hash.
|
|
1042
|
+
const seen = { slots: new Int32Array(1 << 17), size: 0, shift: 16 };
|
|
1014
1043
|
const canonicalizeState = createCanonicalizer();
|
|
1015
1044
|
|
|
1016
1045
|
const toRecord = (state: Value[]): Record<string, Value> =>
|
|
@@ -1023,11 +1052,16 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1023
1052
|
position: number,
|
|
1024
1053
|
loopStart: number | undefined,
|
|
1025
1054
|
) => {
|
|
1026
|
-
const state = steps[position].state;
|
|
1027
|
-
const next = (steps[position + 1] ?? steps[loopStart ?? position]).state;
|
|
1028
1055
|
// TLC evaluates TLCGet("level") and Trace in an alias at the state's position in the trace.
|
|
1029
1056
|
runtime.getLevel = () => position + 1;
|
|
1030
1057
|
runtime.getBehavior = () => steps.slice(0, position + 1).map((step) => step.state);
|
|
1058
|
+
return evaluateAlias(
|
|
1059
|
+
alias,
|
|
1060
|
+
steps[position].state,
|
|
1061
|
+
(steps[position + 1] ?? steps[loopStart ?? position]).state,
|
|
1062
|
+
);
|
|
1063
|
+
};
|
|
1064
|
+
const evaluateAlias = (alias: string, state: Value[], next: Value[] | undefined) => {
|
|
1031
1065
|
try {
|
|
1032
1066
|
const body = getConfigDefinition("alias", alias, environment).body;
|
|
1033
1067
|
const value = evaluate(createIdentifier(alias), getContext(environment, state, next));
|
|
@@ -1165,19 +1199,35 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1165
1199
|
}
|
|
1166
1200
|
return true;
|
|
1167
1201
|
};
|
|
1202
|
+
const getSlot = (hash: number) => (Math.imul(hash, 0x9e3779b1) >>> seen.shift) << 1;
|
|
1168
1203
|
const findStateIndex = (hash: number, state: Value[]) => {
|
|
1169
|
-
const
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1204
|
+
const mask = seen.slots.length - 1;
|
|
1205
|
+
for (let slot = getSlot(hash); ; slot = (slot + 2) & mask) {
|
|
1206
|
+
const entry = seen.slots[slot + 1];
|
|
1207
|
+
if (entry === 0) return -1;
|
|
1208
|
+
if (seen.slots[slot] === hash && isSameState(fingerprintStates[entry - 1], state)) {
|
|
1209
|
+
return entry - 1;
|
|
1210
|
+
}
|
|
1173
1211
|
}
|
|
1174
|
-
|
|
1212
|
+
};
|
|
1213
|
+
const insertSeen = (hash: number, entry: number) => {
|
|
1214
|
+
const mask = seen.slots.length - 1;
|
|
1215
|
+
let slot = getSlot(hash);
|
|
1216
|
+
while (seen.slots[slot + 1] !== 0) slot = (slot + 2) & mask;
|
|
1217
|
+
seen.slots[slot] = hash;
|
|
1218
|
+
seen.slots[slot + 1] = entry;
|
|
1175
1219
|
};
|
|
1176
1220
|
const markSeen = (hash: number, index: number) => {
|
|
1177
|
-
|
|
1178
|
-
if (
|
|
1179
|
-
|
|
1180
|
-
|
|
1221
|
+
seen.size++;
|
|
1222
|
+
if (seen.size * 4 > seen.slots.length) {
|
|
1223
|
+
const slots = seen.slots;
|
|
1224
|
+
seen.slots = new Int32Array(slots.length * 2);
|
|
1225
|
+
seen.shift--;
|
|
1226
|
+
for (let slot = 0; slot < slots.length; slot += 2) {
|
|
1227
|
+
if (slots[slot + 1] !== 0) insertSeen(slots[slot], slots[slot + 1]);
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
insertSeen(hash, index + 1);
|
|
1181
1231
|
};
|
|
1182
1232
|
const countDistinctState = (depth: number) => {
|
|
1183
1233
|
runtime.distinctStates = states.length + 1;
|
|
@@ -1228,6 +1278,18 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1228
1278
|
return current.isExpanding ? depths[current.index] + 1 : 0;
|
|
1229
1279
|
};
|
|
1230
1280
|
|
|
1281
|
+
const getGraphState = (state: Value[], level: number): GraphState => ({
|
|
1282
|
+
fingerprint: BigInt.asIntN(64, runtime.getFingerprint(state)),
|
|
1283
|
+
level,
|
|
1284
|
+
getLabel: () =>
|
|
1285
|
+
formatState(
|
|
1286
|
+
config.alias === undefined
|
|
1287
|
+
? toRecord(state)
|
|
1288
|
+
: evaluateAlias(config.alias, state, undefined),
|
|
1289
|
+
),
|
|
1290
|
+
getText: () => formatState(toRecord(state)),
|
|
1291
|
+
});
|
|
1292
|
+
|
|
1231
1293
|
const visitInitialState = (state: Value[]) => {
|
|
1232
1294
|
canonicalizeState(state);
|
|
1233
1295
|
runtime.generatedStates++;
|
|
@@ -1244,6 +1306,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1244
1306
|
countDistinctState(1);
|
|
1245
1307
|
runtime.queueSize++;
|
|
1246
1308
|
liveness?.addInitialState(states.length, state);
|
|
1309
|
+
stateWriter?.writeInitialState(getGraphState(state, 1));
|
|
1247
1310
|
}
|
|
1248
1311
|
const violated = search.findViolatedInvariant(state);
|
|
1249
1312
|
if (violated) {
|
|
@@ -1264,7 +1327,13 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1264
1327
|
addState(state, fingerprintState, -1, undefined);
|
|
1265
1328
|
return undefined;
|
|
1266
1329
|
};
|
|
1267
|
-
const visitSuccessor = (
|
|
1330
|
+
const visitSuccessor = (
|
|
1331
|
+
index: number,
|
|
1332
|
+
successor: Successor,
|
|
1333
|
+
liveSuccessors: LiveSuccessor[],
|
|
1334
|
+
source: GraphState | undefined,
|
|
1335
|
+
action: SpecAction | undefined,
|
|
1336
|
+
) => {
|
|
1268
1337
|
const state = states[index];
|
|
1269
1338
|
runtime.generatedStates++;
|
|
1270
1339
|
current.candidate = successor.state;
|
|
@@ -1278,6 +1347,14 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1278
1347
|
const isInModel = isStateInModel && search.satisfiesActionConstraints(state, successor.state);
|
|
1279
1348
|
if (isInModel) search.trackActionPossible(state, successor.state);
|
|
1280
1349
|
const existing = isInModel ? found : -1;
|
|
1350
|
+
if (source && isInModel) {
|
|
1351
|
+
stateWriter?.writeSuccessor(
|
|
1352
|
+
source,
|
|
1353
|
+
getGraphState(successor.state, depths[index] + 1),
|
|
1354
|
+
existing !== -1,
|
|
1355
|
+
action && { name: action.name, signature: getActionSignature(action) },
|
|
1356
|
+
);
|
|
1357
|
+
}
|
|
1281
1358
|
if (existing !== -1 && liveness) {
|
|
1282
1359
|
const stored = states[existing];
|
|
1283
1360
|
const isStored = stored.every((value, position) => value === successor.state[position]);
|
|
@@ -1349,23 +1426,55 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1349
1426
|
}
|
|
1350
1427
|
return createSuccess();
|
|
1351
1428
|
}
|
|
1429
|
+
// TLC labels and colors an edge of the -dump state graph by the action that generated it. When
|
|
1430
|
+
// the graph names its edges, the search generates successors action by action, as TLC does.
|
|
1431
|
+
const isNamingEdges = Boolean(
|
|
1432
|
+
options.dump?.dot && (options.dump.actionLabels || options.dump.colorize),
|
|
1433
|
+
);
|
|
1434
|
+
const forEachActionSuccessor = (
|
|
1435
|
+
state: Value[],
|
|
1436
|
+
onSuccessor: (successor: Successor, action: SpecAction | undefined) => boolean,
|
|
1437
|
+
) => {
|
|
1438
|
+
if (!isNamingEdges) {
|
|
1439
|
+
return forEachSuccessor(
|
|
1440
|
+
next,
|
|
1441
|
+
state,
|
|
1442
|
+
environment,
|
|
1443
|
+
(successor) => onSuccessor(successor, undefined),
|
|
1444
|
+
spec.scope,
|
|
1445
|
+
);
|
|
1446
|
+
}
|
|
1447
|
+
nextActions.value ??= getNextActions(config, environment, specification.instanceActions);
|
|
1448
|
+
const progress = { isStopped: false };
|
|
1449
|
+
return nextActions.value.reduce((count, action) => {
|
|
1450
|
+
if (progress.isStopped) return count;
|
|
1451
|
+
return (
|
|
1452
|
+
count +
|
|
1453
|
+
forEachSuccessor(
|
|
1454
|
+
action.expression,
|
|
1455
|
+
state,
|
|
1456
|
+
environment,
|
|
1457
|
+
(successor) => {
|
|
1458
|
+
progress.isStopped = onSuccessor(successor, action);
|
|
1459
|
+
return progress.isStopped;
|
|
1460
|
+
},
|
|
1461
|
+
action.scope,
|
|
1462
|
+
)
|
|
1463
|
+
);
|
|
1464
|
+
}, 0);
|
|
1465
|
+
};
|
|
1352
1466
|
const expandState = (index: number) => {
|
|
1353
1467
|
const state = states[index];
|
|
1354
1468
|
const liveSuccessors: LiveSuccessor[] = [];
|
|
1355
|
-
const
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
(
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
}
|
|
1365
|
-
return outcome.result !== undefined;
|
|
1366
|
-
},
|
|
1367
|
-
spec.scope,
|
|
1368
|
-
);
|
|
1469
|
+
const source = stateWriter && getGraphState(state, depths[index]);
|
|
1470
|
+
const successorCount = forEachActionSuccessor(state, (successor, action) => {
|
|
1471
|
+
try {
|
|
1472
|
+
outcome.result = visitSuccessor(index, successor, liveSuccessors, source, action);
|
|
1473
|
+
} catch (error) {
|
|
1474
|
+
outcome.result = toFailure(error, getTrace(index, successor));
|
|
1475
|
+
}
|
|
1476
|
+
return outcome.result !== undefined;
|
|
1477
|
+
});
|
|
1369
1478
|
current.isExpanding = false;
|
|
1370
1479
|
if (outcome.result) return outcome.result;
|
|
1371
1480
|
if (successorCount === 0 && config.checkDeadlock) {
|
|
@@ -1425,7 +1534,6 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1425
1534
|
periodic.time += performance.now() - now;
|
|
1426
1535
|
return lasso;
|
|
1427
1536
|
};
|
|
1428
|
-
setNextStatePhase(true);
|
|
1429
1537
|
for (let index = 0; index < states.length && !runtime.isStopped; index++) {
|
|
1430
1538
|
current.index = index;
|
|
1431
1539
|
current.candidate = undefined;
|
|
@@ -1434,7 +1542,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1434
1542
|
try {
|
|
1435
1543
|
outcome.result = expandState(index);
|
|
1436
1544
|
} catch (error) {
|
|
1437
|
-
outcome.result = toFailure(error, getStateTrace(index));
|
|
1545
|
+
outcome.result = toFailure(withNextStateExitCode(error), getStateTrace(index));
|
|
1438
1546
|
}
|
|
1439
1547
|
if (outcome.result) return outcome.result;
|
|
1440
1548
|
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
|
};
|
package/src/environment.ts
CHANGED
|
@@ -17,7 +17,8 @@ import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
|
17
17
|
import {
|
|
18
18
|
createBigNumberError,
|
|
19
19
|
createRealNumberError,
|
|
20
|
-
|
|
20
|
+
evaluate,
|
|
21
|
+
getDefinition,
|
|
21
22
|
isTlcInteger,
|
|
22
23
|
type Builtin,
|
|
23
24
|
type Environment,
|
|
@@ -44,7 +45,7 @@ import {
|
|
|
44
45
|
import { createRuntime } from "./runtime";
|
|
45
46
|
import { CORE_OPERATORS, getStandardModule, STANDARD_DEPENDENCIES } from "./standard-modules";
|
|
46
47
|
import { resolveInstanceSubexpressions, resolveModuleSubexpressions } from "./subexpressions";
|
|
47
|
-
import { checkModuleLevels } from "./levels";
|
|
48
|
+
import { checkModuleLevels, createLevelAnalyzer } from "./levels";
|
|
48
49
|
import { checkInstances, type InstanceCheck } from "./substitution-levels";
|
|
49
50
|
import {
|
|
50
51
|
checkInteger,
|
|
@@ -212,6 +213,8 @@ export const createEnvironment = (
|
|
|
212
213
|
const root = parseModule(options.source);
|
|
213
214
|
const definitions = new Map<string, Definition>();
|
|
214
215
|
const parameterized = new Map<string, boolean>();
|
|
216
|
+
// The definition each instance copy came from, and the instance parameters the copy adds.
|
|
217
|
+
const copySources = new Map<string, { source: string; prefix: number }>();
|
|
215
218
|
const builtins = new Map<string, Builtin>(
|
|
216
219
|
CORE_OPERATORS.map((operator) => [operator.name, operator]),
|
|
217
220
|
);
|
|
@@ -379,10 +382,11 @@ export const createEnvironment = (
|
|
|
379
382
|
variables: new Set(linked.variables),
|
|
380
383
|
getDefinition: (key) => definitions.get(key),
|
|
381
384
|
isParameterized: (key) => parameterized.get(key) === true,
|
|
382
|
-
addDefinition: (name, create) => {
|
|
385
|
+
addDefinition: (source, name, create) => {
|
|
383
386
|
const key = createName(`${name}@${module.name}`);
|
|
384
387
|
definitions.set(key, create(key));
|
|
385
388
|
parameterized.set(key, !isParameterFree);
|
|
389
|
+
copySources.set(key, { source, prefix: instance.params.length });
|
|
386
390
|
return key;
|
|
387
391
|
},
|
|
388
392
|
createName,
|
|
@@ -544,7 +548,7 @@ export const createEnvironment = (
|
|
|
544
548
|
variables: new Set(linked.variables),
|
|
545
549
|
getDefinition: (key) => definitions.get(key),
|
|
546
550
|
isParameterized: (key) => parameterized.get(key) === true,
|
|
547
|
-
addDefinition: (name, create) => {
|
|
551
|
+
addDefinition: (_source, name, create) => {
|
|
548
552
|
const key = `${name}${KEY_SEPARATOR}${instance.module}${suffix}.${copies.length}`;
|
|
549
553
|
const definition = create(key);
|
|
550
554
|
copies.push(
|
|
@@ -872,6 +876,38 @@ export const createEnvironment = (
|
|
|
872
876
|
);
|
|
873
877
|
}
|
|
874
878
|
});
|
|
879
|
+
// TLC sets a module-scoped entry on the body of the module's definition, which every instance of
|
|
880
|
+
// the module shares.
|
|
881
|
+
const scopedKeys = new Set(
|
|
882
|
+
[...config.constants, ...config.substitutions]
|
|
883
|
+
.filter((item) => item.module !== undefined)
|
|
884
|
+
.flatMap(getScopedKeys),
|
|
885
|
+
);
|
|
886
|
+
const getOverride = (
|
|
887
|
+
key: string,
|
|
888
|
+
prefix: number,
|
|
889
|
+
): { key: string; prefix: number } | undefined => {
|
|
890
|
+
if (scopedKeys.has(key)) return { key, prefix };
|
|
891
|
+
const copy = copySources.get(key);
|
|
892
|
+
return copy && getOverride(copy.source, prefix + copy.prefix);
|
|
893
|
+
};
|
|
894
|
+
copySources.forEach((copy, key) => {
|
|
895
|
+
const override = getOverride(copy.source, copy.prefix);
|
|
896
|
+
const definition = definitions.get(key);
|
|
897
|
+
if (!override || !definition) return;
|
|
898
|
+
const params = definition.kind === "Operator" ? definition.params : [];
|
|
899
|
+
const alias = createLocalAlias(
|
|
900
|
+
key,
|
|
901
|
+
override.key,
|
|
902
|
+
params,
|
|
903
|
+
params.slice(override.prefix),
|
|
904
|
+
definition.location,
|
|
905
|
+
);
|
|
906
|
+
definitions.set(key, alias);
|
|
907
|
+
linkedRoot.visible.forEach((symbol, name) => {
|
|
908
|
+
if (symbol.key === key && name !== key) definitions.set(name, alias);
|
|
909
|
+
});
|
|
910
|
+
});
|
|
875
911
|
declaredConstants.forEach((name) => {
|
|
876
912
|
if (!constants.has(name) && !substitutions.has(name) && !operatorKeys.has(name)) {
|
|
877
913
|
throw new TlaConfigError(`The constant ${name} is not assigned a value by the configuration`);
|
|
@@ -907,18 +943,41 @@ export const createEnvironment = (
|
|
|
907
943
|
getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
|
|
908
944
|
};
|
|
909
945
|
});
|
|
946
|
+
// TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
|
|
947
|
+
// computes is above constant. The bound counts the operators that TLC's Java code overrides with
|
|
948
|
+
// a minimum level as state-level, so the analyzer takes them for variables.
|
|
949
|
+
const analyzer = createLevelAnalyzer({
|
|
950
|
+
constants: new Set(constants.keys()),
|
|
951
|
+
variables: new Set([
|
|
952
|
+
...variables,
|
|
953
|
+
...["TLCGet", "Trace", "CounterExample"].filter((name) => builtins.has(name)),
|
|
954
|
+
]),
|
|
955
|
+
getDefinition: (name) => getDefinition(name, environment),
|
|
956
|
+
});
|
|
957
|
+
const isEvaluable = (body: Expression) => {
|
|
958
|
+
try {
|
|
959
|
+
evaluate(body, { environment, scope: undefined, state: undefined, next: undefined });
|
|
960
|
+
return true;
|
|
961
|
+
} catch (error) {
|
|
962
|
+
if (error instanceof TlaEvaluationError) return false;
|
|
963
|
+
throw error;
|
|
964
|
+
}
|
|
965
|
+
};
|
|
910
966
|
substitutions.forEach((target, name) => {
|
|
911
967
|
const definition = definitions.get(target);
|
|
912
968
|
if (
|
|
913
969
|
declaredConstants.includes(name) &&
|
|
914
|
-
definition &&
|
|
915
|
-
|
|
970
|
+
definition?.kind === "Operator" &&
|
|
971
|
+
definition.params.length === 0 &&
|
|
972
|
+
analyzer.analyzeDefinition(definition, new Map()).level > 0 &&
|
|
973
|
+
!isEvaluable(definition.body)
|
|
916
974
|
) {
|
|
917
975
|
throw new TlaConfigError(
|
|
918
976
|
`The configuration file substitutes constant ${name} with non-constant ${target}`,
|
|
919
977
|
);
|
|
920
978
|
}
|
|
921
979
|
});
|
|
980
|
+
runtime.isStarted = true;
|
|
922
981
|
return {
|
|
923
982
|
environment,
|
|
924
983
|
assumptions,
|
package/src/errors.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EXIT_STATUS, getExitCode
|
|
1
|
+
import { EXIT_STATUS, getExitCode } from "./exit-codes";
|
|
2
2
|
|
|
3
3
|
// How TLC reports a RuntimeException it did not expect, such as one raised while computing next
|
|
4
4
|
// states.
|
|
@@ -46,10 +46,12 @@ export class TlaEvaluationError extends TlaError {
|
|
|
46
46
|
// An evaluation error that TLC raises as an EvalException from a standard module's Java code. It
|
|
47
47
|
// keeps the name TlaEvaluationError, and index.ts doesn't export it.
|
|
48
48
|
export class TlaModuleError extends TlaEvaluationError {
|
|
49
|
+
exitCode: number;
|
|
50
|
+
|
|
49
51
|
constructor(message: string, exitCode: number = EXIT_STATUS.error) {
|
|
50
52
|
super(message);
|
|
51
53
|
this.name = "TlaEvaluationError";
|
|
52
|
-
|
|
54
|
+
this.exitCode = exitCode;
|
|
53
55
|
}
|
|
54
56
|
}
|
|
55
57
|
|
package/src/evaluate.ts
CHANGED
|
@@ -36,11 +36,11 @@ import {
|
|
|
36
36
|
getIntervalElements,
|
|
37
37
|
getSequenceElements,
|
|
38
38
|
getValueHash,
|
|
39
|
-
isEnumerable,
|
|
40
39
|
isFunctionValue,
|
|
41
40
|
isOverriddenSet,
|
|
42
41
|
isSameValue,
|
|
43
42
|
markLambdaFunction,
|
|
43
|
+
isReducible,
|
|
44
44
|
isSetValue,
|
|
45
45
|
MAXIMUM_INTEGER,
|
|
46
46
|
MINIMUM_INTEGER,
|
|
@@ -1420,7 +1420,13 @@ const compileFunctionConstruction = (
|
|
|
1420
1420
|
const set = getEvaluator(bound.set, environment);
|
|
1421
1421
|
return (context) => {
|
|
1422
1422
|
const domain = isSingleName ? set(context) : getBoundsDomain(bounds, context);
|
|
1423
|
-
|
|
1423
|
+
// TLC builds the function only when it reduces every domain, and evaluates the body lazily
|
|
1424
|
+
// otherwise.
|
|
1425
|
+
const domains =
|
|
1426
|
+
typeof domain === "object" && domain.kind === "ProductSet" && bounds.length > 1
|
|
1427
|
+
? domain.factors
|
|
1428
|
+
: [domain];
|
|
1429
|
+
if (expression.isRecursive || isKeptLazy || !domains.every(isReducible)) {
|
|
1424
1430
|
const functionValue = createLazyFunction(
|
|
1425
1431
|
domain,
|
|
1426
1432
|
(argument) => body(withScope(context, bindArgument(bounds, context.scope, argument))),
|
|
@@ -1514,10 +1520,7 @@ const compileSetComprehension = (
|
|
|
1514
1520
|
predicate(withScope(context, bindPattern(context.scope, pattern, element))),
|
|
1515
1521
|
"a set filter predicate",
|
|
1516
1522
|
);
|
|
1517
|
-
|
|
1518
|
-
typeof base === "object" &&
|
|
1519
|
-
(base.kind === "Set" || base.kind === "MixedSet" || base.kind === "Interval");
|
|
1520
|
-
if (!isReducible) {
|
|
1523
|
+
if (!isReducible(base)) {
|
|
1521
1524
|
if (isSetValue(base)) return { kind: "FilteredSet", base, test };
|
|
1522
1525
|
throw new TlaEvaluationError(
|
|
1523
1526
|
`TLC encountered a non-enumerable quantifier bound ${formatValue(base)} at ${describeLocation(expression.bound.set)}`,
|
package/src/exit-codes.ts
CHANGED
|
@@ -17,12 +17,6 @@ export const EXIT_STATUS = {
|
|
|
17
17
|
} as const;
|
|
18
18
|
|
|
19
19
|
const exitCodes = new WeakMap<Error, number>();
|
|
20
|
-
const moduleExitCodes = new WeakMap<Error, number>();
|
|
21
|
-
const phase = { isNextState: false };
|
|
22
|
-
|
|
23
|
-
export const setNextStatePhase = (isNextState: boolean) => {
|
|
24
|
-
phase.isNextState = isNextState;
|
|
25
|
-
};
|
|
26
20
|
|
|
27
21
|
// The innermost site that knows TLC's exit status for an error sets it first and wins.
|
|
28
22
|
export const setExitCode = <Thrown>(error: Thrown, code: number) => {
|
|
@@ -30,13 +24,7 @@ export const setExitCode = <Thrown>(error: Thrown, code: number) => {
|
|
|
30
24
|
return error;
|
|
31
25
|
};
|
|
32
26
|
|
|
33
|
-
|
|
34
|
-
// escapes the computation of next states. In every other phase, the phase decides the code.
|
|
35
|
-
export const setModuleExitCode = (error: Error, code: number) => {
|
|
36
|
-
if (phase.isNextState) moduleExitCodes.set(error, code);
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export const getExitCode = (error: Error) => exitCodes.get(error) ?? moduleExitCodes.get(error);
|
|
27
|
+
export const getExitCode = (error: Error) => exitCodes.get(error);
|
|
40
28
|
|
|
41
29
|
// TLC exits with 255 when it fails to process or evaluate a liveness property, in any phase.
|
|
42
30
|
const withErrorExitCode = <Result>(run: () => Result) => {
|
package/src/instances.ts
CHANGED
|
@@ -219,7 +219,7 @@ interface Instantiation {
|
|
|
219
219
|
variables: Set<string>;
|
|
220
220
|
getDefinition: (key: string) => Definition | undefined;
|
|
221
221
|
isParameterized: (key: string) => boolean;
|
|
222
|
-
addDefinition: (name: string, create: (key: string) => Definition) => string;
|
|
222
|
+
addDefinition: (source: string, name: string, create: (key: string) => Definition) => string;
|
|
223
223
|
createName: (name: string) => string;
|
|
224
224
|
}
|
|
225
225
|
|
|
@@ -640,7 +640,7 @@ const copyDefinition = (rewrite: Rewrite, key: string): string => {
|
|
|
640
640
|
const instantiation = rewrite.instantiation;
|
|
641
641
|
const definition = instantiation.getDefinition(key);
|
|
642
642
|
if (!definition) return key;
|
|
643
|
-
return instantiation.addDefinition(definition.name, (copyKey) => {
|
|
643
|
+
return instantiation.addDefinition(key, definition.name, (copyKey) => {
|
|
644
644
|
rewrite.copies.set(key, copyKey);
|
|
645
645
|
const rename = createRenamer(rewrite);
|
|
646
646
|
const mapped = renameParams(
|
|
@@ -679,7 +679,7 @@ export const createInstantiation = (instantiation: Instantiation) => {
|
|
|
679
679
|
if (copy !== undefined) return copy;
|
|
680
680
|
const definition = instantiation.getDefinition(key);
|
|
681
681
|
if (!definition) return key;
|
|
682
|
-
return instantiation.addDefinition(definition.name, () =>
|
|
682
|
+
return instantiation.addDefinition(key, definition.name, () =>
|
|
683
683
|
withInstanceParams(definition, instantiation.params, definition.location),
|
|
684
684
|
);
|
|
685
685
|
},
|
package/src/parallel.ts
CHANGED
|
@@ -114,9 +114,12 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
|
114
114
|
});
|
|
115
115
|
};
|
|
116
116
|
const count = options.workers ?? 1;
|
|
117
|
+
// TLC's workers write the -dump state graph in whichever order they find it, so one worker
|
|
118
|
+
// writes the graph a single TLC worker does.
|
|
117
119
|
const isSequential =
|
|
118
120
|
count < 2 ||
|
|
119
121
|
typeof SharedArrayBuffer !== "function" ||
|
|
122
|
+
options.dump !== undefined ||
|
|
120
123
|
config.view !== undefined ||
|
|
121
124
|
config.postconditions.length > 0 ||
|
|
122
125
|
config.possible.length > 0;
|