tla-ts 0.0.2 → 0.0.3-dev.14337b3
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 +78 -12
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +85 -15
- package/dist/index.d.ts +85 -15
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +2 -2
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +22 -2
- package/dist/run-cli.js +22 -2
- package/dist/transfer.cjs +37 -16
- package/dist/transfer.js +37 -16
- package/package.json +3 -1
- package/src/ast.ts +137 -4
- package/src/check.ts +378 -100
- package/src/cli-options.ts +64 -0
- package/src/codegen.ts +15 -22
- package/src/config.ts +224 -170
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +116 -28
- package/src/errors.ts +101 -3
- package/src/evaluate.ts +129 -153
- package/src/exit-codes.ts +39 -42
- package/src/fingerprints.ts +46 -11
- package/src/instances.ts +16 -15
- package/src/levels.ts +62 -34
- package/src/lexer.ts +11 -2
- package/src/live-check.ts +772 -0
- package/src/liveness.ts +116 -543
- package/src/obligation-action.ts +384 -0
- package/src/obligation-anon.ts +575 -0
- package/src/obligation-deref.ts +637 -0
- package/src/obligation-expression.ts +1060 -0
- package/src/obligation-format.ts +406 -0
- package/src/obligation-generate.ts +1074 -0
- package/src/obligation-levels.ts +329 -0
- package/src/obligation-module.ts +1042 -0
- package/src/obligation-printer.ts +889 -0
- package/src/obligation-proof.ts +76 -0
- package/src/obligation-source.ts +1084 -0
- package/src/obligation-tuples.ts +184 -0
- package/src/obligations.ts +316 -0
- package/src/parallel-worker.ts +168 -212
- package/src/parallel.ts +78 -37
- package/src/parser.ts +315 -60
- package/src/resolve.ts +122 -44
- package/src/run-cli.ts +376 -44
- package/src/runtime.ts +3 -1
- package/src/spec-description.ts +19 -8
- package/src/standard-levels.ts +3 -3
- package/src/standard-modules.ts +2 -7
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +10 -10
- package/src/successors.ts +99 -25
- package/src/symmetry.ts +30 -8
- package/src/tableau.ts +51 -24
- package/src/tlc-ext.ts +29 -12
- package/src/tlc-module.ts +14 -33
- package/src/tlc-modules.ts +4 -0
- package/src/tlc-order.ts +25 -7
- package/src/tlc-random.ts +9 -6
- package/src/tlc-summary.ts +20 -5
- package/src/transfer.ts +22 -1
- package/src/values.ts +179 -42
package/src/run-cli.ts
CHANGED
|
@@ -1,19 +1,42 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
existsSync,
|
|
3
|
+
globSync,
|
|
4
|
+
mkdirSync,
|
|
5
|
+
readFileSync,
|
|
6
|
+
readSync,
|
|
7
|
+
realpathSync,
|
|
8
|
+
writeFileSync,
|
|
9
|
+
} from "node:fs";
|
|
10
|
+
import { spawnSync } from "node:child_process";
|
|
11
|
+
import { basename, delimiter, dirname, join, resolve } from "node:path";
|
|
3
12
|
import { availableParallelism } from "node:os";
|
|
4
13
|
import type { CheckResult, CheckStatus, TraceStep } from "./check";
|
|
14
|
+
import { defineCommand, type ParsedArgs } from "./cli-options";
|
|
5
15
|
import { parseConfig } from "./config";
|
|
6
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
createSanyError,
|
|
18
|
+
getErrorExitCode,
|
|
19
|
+
TlaConfigError,
|
|
20
|
+
TlaError,
|
|
21
|
+
TlaEvaluationError,
|
|
22
|
+
TlaProofError,
|
|
23
|
+
TlaSyntaxError,
|
|
24
|
+
type SanyReport,
|
|
25
|
+
} from "./errors";
|
|
7
26
|
import { EXIT_STATUS } from "./exit-codes";
|
|
27
|
+
import type { LineRange } from "./obligation-generate";
|
|
28
|
+
import { getAtPrefix, getLocusText } from "./obligation-expression";
|
|
29
|
+
import { formatSummary, getObligations, WHOLE_MODULE, type ModuleObligations } from "./obligations";
|
|
8
30
|
import { check } from "./parallel";
|
|
9
|
-
import {
|
|
31
|
+
import { parseModule } from "./parser";
|
|
32
|
+
import type { StateDump } from "./state-graph";
|
|
10
33
|
import {
|
|
11
34
|
formatProbability,
|
|
12
35
|
getActualProbability,
|
|
13
36
|
getOptimisticProbability,
|
|
14
37
|
getOutDegreeSummary,
|
|
15
38
|
} from "./tlc-summary";
|
|
16
|
-
import {
|
|
39
|
+
import { formatState, type Value } from "./values";
|
|
17
40
|
|
|
18
41
|
const EXIT_CODES: Record<CheckStatus, number> = {
|
|
19
42
|
success: EXIT_STATUS.success,
|
|
@@ -26,27 +49,79 @@ const EXIT_CODES: Record<CheckStatus, number> = {
|
|
|
26
49
|
error: EXIT_STATUS.error,
|
|
27
50
|
};
|
|
28
51
|
|
|
29
|
-
const
|
|
30
|
-
|
|
52
|
+
const CHECK_COMMAND = defineCommand("tla-ts <Spec.tla|Spec.cfg|glob>... {options}", [
|
|
53
|
+
{ flag: "-config <Spec.cfg>" },
|
|
54
|
+
{ flag: "-deadlock" },
|
|
55
|
+
// Like TLC, -dump takes a format only when the next argument starts with dot and a file follows.
|
|
56
|
+
{
|
|
57
|
+
flag: "-dump [<format>] <file>",
|
|
58
|
+
getArity: (rest) => (rest.length > 1 && rest[0].startsWith("dot") ? 2 : 1),
|
|
59
|
+
},
|
|
60
|
+
{ flag: "-lncheck <final|default>" },
|
|
61
|
+
{ flag: "-seed <num>" },
|
|
62
|
+
{ flag: "-workers <count|auto>" },
|
|
63
|
+
]);
|
|
64
|
+
|
|
65
|
+
const PROVE_COMMAND = defineCommand("tla-ts prove {options} <Module.tla|glob>...", [
|
|
66
|
+
{ flag: "-N", isRequired: true },
|
|
67
|
+
{ flag: "--toolbox <start> <end>" },
|
|
68
|
+
{ flag: "--line <num>" },
|
|
69
|
+
{ flag: "--printallobs" },
|
|
70
|
+
{ flag: "--summary" },
|
|
71
|
+
{ flag: "--strict" },
|
|
72
|
+
{ flag: "-I <directory>", isRepeatable: true },
|
|
73
|
+
]);
|
|
74
|
+
|
|
75
|
+
const USAGE = [
|
|
76
|
+
`Usage: ${CHECK_COMMAND.usage}`,
|
|
77
|
+
` ${PROVE_COMMAND.usage}`,
|
|
78
|
+
" tla-ts prove [<tlapm option>]... <Module.tla|glob>...",
|
|
79
|
+
].join("\n");
|
|
80
|
+
|
|
81
|
+
const GLOB_CHARACTERS = /[*?[{]/;
|
|
82
|
+
|
|
83
|
+
interface InputPath {
|
|
84
|
+
path: string;
|
|
85
|
+
isGlobbed: boolean;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const getInputPaths = (paths: string[]): InputPath[] =>
|
|
89
|
+
paths.flatMap((arg): InputPath[] =>
|
|
90
|
+
GLOB_CHARACTERS.test(arg)
|
|
91
|
+
? globSync(arg)
|
|
92
|
+
.sort()
|
|
93
|
+
.map((path) => ({ path, isGlobbed: true }))
|
|
94
|
+
: [{ path: arg, isGlobbed: false }],
|
|
95
|
+
);
|
|
31
96
|
|
|
32
97
|
const isJavaLong = (text: string) =>
|
|
33
98
|
/^[+-]?\d+$/.test(text) && BigInt.asIntN(64, BigInt(text)) === BigInt(text);
|
|
34
99
|
|
|
35
|
-
const getWorkerCount = (
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
100
|
+
const getWorkerCount = (values: string[] | undefined) => {
|
|
101
|
+
if (!values) return 1;
|
|
102
|
+
return values[0] === "auto"
|
|
103
|
+
? availableParallelism()
|
|
104
|
+
: Math.max(1, Number.parseInt(values[0], 10) || 1);
|
|
40
105
|
};
|
|
41
106
|
|
|
42
|
-
const printState = (state: Record<string, Value>) =>
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
)
|
|
49
|
-
|
|
107
|
+
const printState = (state: Record<string, Value>) => console.log(formatState(state));
|
|
108
|
+
|
|
109
|
+
const UNSUPPORTED_DUMP_FORMATS = ["constrained", "stuttering", "strict"];
|
|
110
|
+
|
|
111
|
+
// TLC names the file of a -dump by its format unless the name already ends that way.
|
|
112
|
+
const getDumpPath = (path: string, extension: string) =>
|
|
113
|
+
path.endsWith(extension) ? path : `${path}${extension}`;
|
|
114
|
+
|
|
115
|
+
const getDump = (values: string[]): StateDump => {
|
|
116
|
+
if (values.length === 1) return { path: getDumpPath(values[0], ".dump") };
|
|
117
|
+
const format = values[0].toLowerCase();
|
|
118
|
+
return {
|
|
119
|
+
path: getDumpPath(values[1], ".dot"),
|
|
120
|
+
dot: true,
|
|
121
|
+
colorize: format.includes("colorize"),
|
|
122
|
+
actionLabels: format.includes("actionlabels"),
|
|
123
|
+
snapshot: format.includes("snapshot"),
|
|
124
|
+
};
|
|
50
125
|
};
|
|
51
126
|
|
|
52
127
|
const printTrace = (trace: TraceStep[], heading = "Error: The behavior up to this point is:") => {
|
|
@@ -57,6 +132,11 @@ const printTrace = (trace: TraceStep[], heading = "Error: The behavior up to thi
|
|
|
57
132
|
});
|
|
58
133
|
};
|
|
59
134
|
|
|
135
|
+
const getTemporalHeadline = (names: string[]) =>
|
|
136
|
+
names.length === 1
|
|
137
|
+
? `Error: Temporal property ${names[0]} was violated.\n`
|
|
138
|
+
: `Error: Temporal properties ${names.slice(0, -1).join(", ")}${names.length > 2 ? "," : ""} and ${names[names.length - 1]} were violated.\n`;
|
|
139
|
+
|
|
60
140
|
const printResult = (result: CheckResult) => {
|
|
61
141
|
if (result.failure) {
|
|
62
142
|
console.log(`Error: ${result.failure.message}`);
|
|
@@ -84,10 +164,10 @@ const printResult = (result: CheckResult) => {
|
|
|
84
164
|
"invariant-violation": isInitial
|
|
85
165
|
? `Error: Invariant ${name} is violated by the initial state:`
|
|
86
166
|
: `Error: Invariant ${name} is violated.`,
|
|
87
|
-
"property-violation":
|
|
167
|
+
"property-violation": getTemporalHeadline(violation.properties ?? [name]),
|
|
88
168
|
"liveness-violation":
|
|
89
169
|
loopStart !== undefined
|
|
90
|
-
?
|
|
170
|
+
? getTemporalHeadline(violation.properties ?? [name])
|
|
91
171
|
: isInitial
|
|
92
172
|
? `Error: Property ${name} is violated by the initial state:`
|
|
93
173
|
: `Error: Action property ${name} is violated.`,
|
|
@@ -96,6 +176,8 @@ const printResult = (result: CheckResult) => {
|
|
|
96
176
|
if (violation.counts) console.log(violation.counts);
|
|
97
177
|
console.log(headline);
|
|
98
178
|
if (isInitial) printState(violation.trace[0].state);
|
|
179
|
+
else if (result.status === "property-violation")
|
|
180
|
+
printTrace(violation.trace, "Error: The following behavior constitutes a counter-example:\n");
|
|
99
181
|
else if (loopStart === undefined) printTrace(violation.trace);
|
|
100
182
|
else {
|
|
101
183
|
printTrace(violation.trace, "Error: The following behavior constitutes a counter-example:\n");
|
|
@@ -130,26 +212,35 @@ const printResult = (result: CheckResult) => {
|
|
|
130
212
|
if (outDegree) console.log(outDegree);
|
|
131
213
|
};
|
|
132
214
|
|
|
133
|
-
const
|
|
134
|
-
const specPath = args.find((arg, index) => arg.endsWith(".tla") && args[index - 1] !== "-config");
|
|
135
|
-
if (!specPath) {
|
|
136
|
-
console.error(USAGE);
|
|
137
|
-
return 1;
|
|
138
|
-
}
|
|
215
|
+
const checkSpec = async (specPath: string, args: ParsedArgs) => {
|
|
139
216
|
if (!existsSync(specPath)) {
|
|
140
217
|
console.error(`Cannot find the specification file ${specPath}`);
|
|
141
218
|
return EXIT_STATUS.configParseError;
|
|
142
219
|
}
|
|
143
|
-
const
|
|
144
|
-
const
|
|
220
|
+
const source = readFileSync(specPath, "utf8");
|
|
221
|
+
const fileName = basename(specPath, ".tla");
|
|
222
|
+
const moduleName = parseModule(source).name;
|
|
223
|
+
if (moduleName !== fileName) {
|
|
224
|
+
throw createSanyError(
|
|
225
|
+
[
|
|
226
|
+
{
|
|
227
|
+
location: "Unknown location",
|
|
228
|
+
message: `File name '${fileName}' does not match the name '${moduleName}' of the top level module it contains.`,
|
|
229
|
+
},
|
|
230
|
+
],
|
|
231
|
+
true,
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
const configPath =
|
|
235
|
+
args.get("-config")?.[0] ??
|
|
236
|
+
(args.has("-config") ? undefined : specPath.replace(/\.tla$/, ".cfg"));
|
|
145
237
|
if (!configPath || !existsSync(configPath)) {
|
|
146
238
|
console.error(`Cannot find the configuration file ${configPath ?? ""}`);
|
|
147
239
|
return EXIT_STATUS.configParseError;
|
|
148
240
|
}
|
|
149
241
|
const config = parseConfig(readFileSync(configPath, "utf8"));
|
|
150
|
-
if (args.
|
|
151
|
-
const
|
|
152
|
-
const seed = seedFlag === -1 ? "0" : args[seedFlag + 1];
|
|
242
|
+
if (args.has("-deadlock")) config.checkDeadlock = false;
|
|
243
|
+
const seed = args.has("-seed") ? args.get("-seed")?.[0] : "0";
|
|
153
244
|
if (seed === undefined) {
|
|
154
245
|
console.error("Error: seed required.");
|
|
155
246
|
return 1;
|
|
@@ -158,19 +249,31 @@ const run = async (args: string[]) => {
|
|
|
158
249
|
console.error(`Error: An integer for seed required. But encountered ${seed}`);
|
|
159
250
|
return 1;
|
|
160
251
|
}
|
|
161
|
-
const
|
|
162
|
-
const livenessCheck = livenessFlag === -1 ? "default" : args[livenessFlag + 1]?.toLowerCase();
|
|
252
|
+
const livenessCheck = args.has("-lncheck") ? args.get("-lncheck")?.[0]?.toLowerCase() : "default";
|
|
163
253
|
if (livenessCheck === undefined) {
|
|
164
254
|
console.error("Error: expect a strategy such as final for -lncheck option.");
|
|
165
255
|
return 1;
|
|
166
256
|
}
|
|
257
|
+
const dumpValues = args.get("-dump");
|
|
258
|
+
if (dumpValues?.length === 0) {
|
|
259
|
+
console.error("Error: A file name for dumping states required.");
|
|
260
|
+
return 1;
|
|
261
|
+
}
|
|
262
|
+
const unsupportedFormat =
|
|
263
|
+
dumpValues?.length === 2 &&
|
|
264
|
+
UNSUPPORTED_DUMP_FORMATS.find((format) => dumpValues[0].toLowerCase().includes(format));
|
|
265
|
+
if (unsupportedFormat) {
|
|
266
|
+
console.error(`Error: tla-ts does not support the -dump format ${unsupportedFormat}.`);
|
|
267
|
+
return 1;
|
|
268
|
+
}
|
|
167
269
|
const directory = dirname(specPath);
|
|
168
270
|
console.log(`Checking ${basename(specPath)} with ${basename(configPath)}`);
|
|
169
271
|
const result = await check({
|
|
170
|
-
workers: getWorkerCount(args),
|
|
272
|
+
workers: getWorkerCount(args.get("-workers")),
|
|
171
273
|
livenessCheck: ["final", "seqfinal", "off"].includes(livenessCheck) ? "final" : "default",
|
|
172
274
|
seed: BigInt(seed),
|
|
173
|
-
|
|
275
|
+
dump: dumpValues && getDump(dumpValues),
|
|
276
|
+
source,
|
|
174
277
|
config,
|
|
175
278
|
onPause: () => readSync(0, Buffer.alloc(1)),
|
|
176
279
|
loadModule: (name) => {
|
|
@@ -191,20 +294,249 @@ const run = async (args: string[]) => {
|
|
|
191
294
|
return result.failure?.exitCode ?? EXIT_CODES[result.status];
|
|
192
295
|
};
|
|
193
296
|
|
|
194
|
-
|
|
195
|
-
|
|
297
|
+
const formatSanyErrors = (report: SanyReport) =>
|
|
298
|
+
`*** Errors: ${report.errors.length}\n\n${report.errors.map((error) => `${error.location}\n\n${error.message}\n\n`).join("\n")}`;
|
|
299
|
+
|
|
300
|
+
// TLC prints the errors it raises itself, and SANY's report of a spec it rejects. tla-ts names the
|
|
301
|
+
// others, such as a module it cannot parse.
|
|
302
|
+
const printError = (error: TlaError, path?: string) => {
|
|
196
303
|
if (error instanceof TlaConfigError || error instanceof TlaEvaluationError) {
|
|
197
304
|
return console.log(`Error: ${error.message}`);
|
|
198
305
|
}
|
|
199
|
-
|
|
306
|
+
const report = error instanceof TlaSyntaxError ? error.sany : undefined;
|
|
307
|
+
if (!report || path === undefined) return console.error(`${error.name}: ${error.message}`);
|
|
308
|
+
console.log(
|
|
309
|
+
report.isFatal
|
|
310
|
+
? `\nFatal errors while parsing TLA+ spec in file ${basename(path, ".tla")}\n\n${report.errors.map((item) => `${item.location}\n\n${item.message}\n`).join("")}${formatSanyErrors(report)}\n`
|
|
311
|
+
: `Semantic errors:\n\n${formatSanyErrors(report)}`,
|
|
312
|
+
);
|
|
313
|
+
console.log("Error: Parsing or semantic analysis failed.");
|
|
200
314
|
};
|
|
201
315
|
|
|
202
|
-
|
|
316
|
+
// Prints a TlaError and returns its exit code.
|
|
317
|
+
const reportErrors = async (run: () => number | Promise<number>, path?: string) => {
|
|
203
318
|
try {
|
|
204
|
-
|
|
319
|
+
return await run();
|
|
205
320
|
} catch (error) {
|
|
206
321
|
if (!(error instanceof TlaError)) throw error;
|
|
207
|
-
printError(error);
|
|
208
|
-
|
|
322
|
+
printError(error, path);
|
|
323
|
+
return getErrorExitCode(error);
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
// Runs each path in turn, so an error in one leaves the others to report. The exit code is the
|
|
328
|
+
// first that is not success.
|
|
329
|
+
const runEach = async (paths: string[], runPath: (path: string) => number | Promise<number>) => {
|
|
330
|
+
const exitCodes: number[] = [];
|
|
331
|
+
for (const [index, path] of paths.entries()) {
|
|
332
|
+
if (index > 0) console.log("");
|
|
333
|
+
exitCodes.push(await reportErrors(() => runPath(path), path));
|
|
334
|
+
}
|
|
335
|
+
return exitCodes.find((exitCode) => exitCode !== EXIT_STATUS.success) ?? EXIT_STATUS.success;
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
// tlapm installs its standard library, with modules such as TLAPS, at lib/tlapm/stdlib beside
|
|
339
|
+
// bin/tlapm.
|
|
340
|
+
const getTlapmPaths = () =>
|
|
341
|
+
(process.env.PATH ?? "")
|
|
342
|
+
.split(delimiter)
|
|
343
|
+
.map((directory) => join(directory, "tlapm"))
|
|
344
|
+
.filter((path) => existsSync(path));
|
|
345
|
+
|
|
346
|
+
const getTlapmLibrary = () =>
|
|
347
|
+
getTlapmPaths()
|
|
348
|
+
.map((path) => join(dirname(dirname(realpathSync(path))), "lib/tlapm/stdlib"))
|
|
349
|
+
.find((path) => existsSync(path));
|
|
350
|
+
|
|
351
|
+
const expandGlobs = (paths: string[]) =>
|
|
352
|
+
paths.flatMap((path) => (GLOB_CHARACTERS.test(path) ? globSync(path).sort() : [path]));
|
|
353
|
+
|
|
354
|
+
const proveWithTlapm = (args: string[]) => {
|
|
355
|
+
const tlapm = getTlapmPaths()[0];
|
|
356
|
+
if (tlapm === undefined) {
|
|
357
|
+
console.error(
|
|
358
|
+
"Error: tla-ts prove runs tlapm to prove obligations, but tlapm isn't on your PATH. Install tlapm, or pass -N to only generate the obligations.",
|
|
359
|
+
);
|
|
360
|
+
return 1;
|
|
361
|
+
}
|
|
362
|
+
return spawnSync(tlapm, expandGlobs(args), { stdio: "inherit" }).status ?? 1;
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
const readModule = (path: string) =>
|
|
366
|
+
existsSync(path) ? { file: path, source: readFileSync(path, "utf8") } : undefined;
|
|
367
|
+
|
|
368
|
+
// A message of tlapm's toolbox protocol, which it writes to stderr.
|
|
369
|
+
const printToolboxMessage = (fields: string[]) =>
|
|
370
|
+
console.error(`@!!BEGIN\n${fields.map((field) => `@!!${field}\n`).join("")}@!!END\n`);
|
|
371
|
+
|
|
372
|
+
const TLAPM_VERSION = "5f5037f";
|
|
373
|
+
|
|
374
|
+
const quoteArgument = (arg: string) =>
|
|
375
|
+
/^[+,\-./0-9:=@A-Z_a-z]+$/.test(arg) ? arg : `'${arg.replaceAll("'", "'\\''")}'`;
|
|
376
|
+
|
|
377
|
+
const printToolboxHeader = (args: string[]) => {
|
|
378
|
+
const now = new Date();
|
|
379
|
+
const pad = (value: number) => String(value).padStart(2, "0");
|
|
380
|
+
const date = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`;
|
|
381
|
+
const time = `${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`;
|
|
382
|
+
console.log(
|
|
383
|
+
`\n\\* TLAPM version ${TLAPM_VERSION}\n\\* launched at ${date} ${time} with command line:\n\\* ${["tla-ts", "prove", ...args].map(quoteArgument).join(" ")}\n`,
|
|
384
|
+
);
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
const STRICT_EXIT_STATUS = { incomplete: 11, emptyTarget: 12 };
|
|
388
|
+
|
|
389
|
+
const checkStrictly = (result: ModuleObligations, isTargeted: boolean, isSummary: boolean) => {
|
|
390
|
+
const summary = result.summary;
|
|
391
|
+
if (!isSummary) {
|
|
392
|
+
summary.absent.forEach((locus) => console.error(`Missing proof at ${getLocusText(locus)}`));
|
|
393
|
+
summary.omitted.forEach((locus) => console.error(`Omitted proof at ${getLocusText(locus)}`));
|
|
394
|
+
}
|
|
395
|
+
const isIncomplete = summary.absent.length + summary.omitted.length > 0;
|
|
396
|
+
if (isIncomplete) {
|
|
397
|
+
console.error(
|
|
398
|
+
`${getAtPrefix(result.location)}[ERROR]: Proof incomplete in module ${JSON.stringify(result.name)}: ${summary.absent.length} missing, ${summary.omitted.length} omitted proof step(s).`,
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
const isEmpty = isTargeted && result.obligations.length === 0;
|
|
402
|
+
if (isEmpty) {
|
|
403
|
+
console.error(
|
|
404
|
+
`${getAtPrefix(result.location)}[ERROR]: No proof obligation found for the selected target.`,
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
return [
|
|
408
|
+
...(isIncomplete ? [STRICT_EXIT_STATUS.incomplete] : []),
|
|
409
|
+
...(isEmpty ? [STRICT_EXIT_STATUS.emptyTarget] : []),
|
|
410
|
+
];
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
const TLAPM_FAILURE_STATUS = 3;
|
|
414
|
+
|
|
415
|
+
const generateObligations = (generate: () => ModuleObligations[]) => {
|
|
416
|
+
try {
|
|
417
|
+
return generate();
|
|
418
|
+
} catch (error) {
|
|
419
|
+
if (!(error instanceof TlaProofError)) throw error;
|
|
420
|
+
console.error(` tlapm ending abnormally with ${error.exception}\n`);
|
|
421
|
+
return undefined;
|
|
209
422
|
}
|
|
210
423
|
};
|
|
424
|
+
|
|
425
|
+
const prove = (args: string[]) => {
|
|
426
|
+
if (!args.includes("-N") && !args.includes("--summary")) return proveWithTlapm(args);
|
|
427
|
+
const parsed = PROVE_COMMAND.parse(args);
|
|
428
|
+
const unsupported = parsed.unknown[0];
|
|
429
|
+
if (unsupported !== undefined) {
|
|
430
|
+
console.error(`Error: tla-ts prove does not support the tlapm option ${unsupported}.`);
|
|
431
|
+
return 1;
|
|
432
|
+
}
|
|
433
|
+
if (parsed.incomplete) {
|
|
434
|
+
console.error(`Error: option '${parsed.incomplete.name}' needs an argument.`);
|
|
435
|
+
return 1;
|
|
436
|
+
}
|
|
437
|
+
const nonInteger = parsed.options.find(
|
|
438
|
+
(option) => option.name !== "-I" && option.values.some((value) => !/^-?\d+$/.test(value)),
|
|
439
|
+
);
|
|
440
|
+
if (nonInteger) {
|
|
441
|
+
console.error(`Error: option '${nonInteger.name}' expects an integer.`);
|
|
442
|
+
return 1;
|
|
443
|
+
}
|
|
444
|
+
const files = expandGlobs(parsed.positionals);
|
|
445
|
+
const modulePaths = files.map((path) => (path.endsWith(".tla") ? path : `${path}.tla`));
|
|
446
|
+
if (modulePaths.length === 0) {
|
|
447
|
+
console.error(USAGE);
|
|
448
|
+
return 1;
|
|
449
|
+
}
|
|
450
|
+
const isToolbox = parsed.has("--toolbox") || parsed.has("--line");
|
|
451
|
+
if (isToolbox) printToolboxHeader(args);
|
|
452
|
+
// Like tlapm, the last of --toolbox and --line wins, and a 0 means the whole module.
|
|
453
|
+
const lines = parsed.options.reduce<LineRange>((range, option) => {
|
|
454
|
+
const [start, end] = option.values.map(Number);
|
|
455
|
+
if (option.name === "--toolbox") return { start, end: end === 0 ? Infinity : end };
|
|
456
|
+
if (option.name === "--line") return start === 0 ? WHOLE_MODULE : { start, end: start };
|
|
457
|
+
return range;
|
|
458
|
+
}, WHOLE_MODULE);
|
|
459
|
+
// tlapm searches the current directory and each module's, then its library, then each -I
|
|
460
|
+
// directory, the last given first.
|
|
461
|
+
const tlapmLibrary = getTlapmLibrary();
|
|
462
|
+
const includes = parsed.options.flatMap((option) => (option.name === "-I" ? option.values : []));
|
|
463
|
+
const directories = modulePaths.reduce(
|
|
464
|
+
(found, path) => (found.includes(dirname(path)) ? found : [dirname(path), ...found]),
|
|
465
|
+
[".", ...(tlapmLibrary ? [tlapmLibrary] : []), ...includes.toReversed()],
|
|
466
|
+
);
|
|
467
|
+
// tlapm names each file it reads by joining a search directory and the name, as in ./Module.tla.
|
|
468
|
+
const results = generateObligations(() => {
|
|
469
|
+
const missingIndex = modulePaths.findIndex((path) => !existsSync(path));
|
|
470
|
+
if (missingIndex !== -1) {
|
|
471
|
+
const missing = files[missingIndex];
|
|
472
|
+
console.error(`File "<unknown>":\nError: File ${JSON.stringify(missing)} not found`);
|
|
473
|
+
throw new TlaProofError(`File ${missing} not found`, { failure: "Module.Parser.parse_file" });
|
|
474
|
+
}
|
|
475
|
+
return getObligations(
|
|
476
|
+
modulePaths.map((path) => ({ file: `./${path}`, source: readFileSync(path, "utf8") })),
|
|
477
|
+
(name) =>
|
|
478
|
+
directories.map((directory) => readModule(`${directory}/${name}.tla`)).find(Boolean),
|
|
479
|
+
{
|
|
480
|
+
lines,
|
|
481
|
+
onWarning: (warning) => {
|
|
482
|
+
if (warning.channel === "stdout") return console.log(warning.text);
|
|
483
|
+
const isToolboxMessage =
|
|
484
|
+
warning.channel === "toolbox" || warning.channel === "toolboxOnly";
|
|
485
|
+
if (isToolbox && isToolboxMessage)
|
|
486
|
+
return printToolboxMessage(["type:warning", `msg:${warning.text}`]);
|
|
487
|
+
if (warning.channel !== "toolboxOnly") console.error(warning.text);
|
|
488
|
+
},
|
|
489
|
+
},
|
|
490
|
+
);
|
|
491
|
+
});
|
|
492
|
+
if (!results) return TLAPM_FAILURE_STATUS;
|
|
493
|
+
const failures = results.flatMap((result) => {
|
|
494
|
+
if (parsed.has("--summary")) console.log(formatSummary(result));
|
|
495
|
+
if (isToolbox) {
|
|
496
|
+
result.obligations.forEach((obligation) =>
|
|
497
|
+
printToolboxMessage([
|
|
498
|
+
"type:obligation",
|
|
499
|
+
`id:${obligation.id}`,
|
|
500
|
+
`loc:${obligation.location}`,
|
|
501
|
+
"status:to be proved",
|
|
502
|
+
...(parsed.has("--printallobs") ? [`obl:${obligation.text}`] : []),
|
|
503
|
+
]),
|
|
504
|
+
);
|
|
505
|
+
printToolboxMessage(["type:obligationsnumber", `count:${result.obligations.length}`]);
|
|
506
|
+
}
|
|
507
|
+
if (!parsed.has("--strict")) return [];
|
|
508
|
+
return checkStrictly(result, lines.start > 0 || lines.end < Infinity, parsed.has("--summary"));
|
|
509
|
+
});
|
|
510
|
+
return failures.includes(STRICT_EXIT_STATUS.incomplete)
|
|
511
|
+
? STRICT_EXIT_STATUS.incomplete
|
|
512
|
+
: (failures[0] ?? EXIT_STATUS.success);
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
const run = async (args: string[]) => {
|
|
516
|
+
if (args[0] === "prove") return reportErrors(() => prove(args.slice(1)));
|
|
517
|
+
const parsed = CHECK_COMMAND.parse(args);
|
|
518
|
+
const inputs = getInputPaths(parsed.positionals);
|
|
519
|
+
const isConfigured = parsed.has("-config");
|
|
520
|
+
// A glob also matches modules that only other specs import, which have no configuration.
|
|
521
|
+
const specPaths = inputs
|
|
522
|
+
.map((input) => ({ ...input, path: input.path.replace(/\.cfg$/, ".tla") }))
|
|
523
|
+
.filter(
|
|
524
|
+
(input) =>
|
|
525
|
+
input.path.endsWith(".tla") &&
|
|
526
|
+
(!input.isGlobbed || isConfigured || existsSync(input.path.replace(/\.tla$/, ".cfg"))),
|
|
527
|
+
)
|
|
528
|
+
.map((input) => input.path);
|
|
529
|
+
if (specPaths.length === 0) {
|
|
530
|
+
console.error(USAGE);
|
|
531
|
+
return 1;
|
|
532
|
+
}
|
|
533
|
+
if (isConfigured && specPaths.length > 1) {
|
|
534
|
+
console.error("Error: -config applies to one specification, but several were given.");
|
|
535
|
+
return 1;
|
|
536
|
+
}
|
|
537
|
+
return runEach([...new Set(specPaths)], (path) => checkSpec(path, parsed));
|
|
538
|
+
};
|
|
539
|
+
|
|
540
|
+
export const runCli = async () => {
|
|
541
|
+
process.exitCode = await run(process.argv.slice(2));
|
|
542
|
+
};
|
package/src/runtime.ts
CHANGED
|
@@ -6,7 +6,7 @@ export interface CheckerRuntime {
|
|
|
6
6
|
getBehavior: (state: Value[]) => Value[][];
|
|
7
7
|
getSpecification: () => Value;
|
|
8
8
|
counterExample: Value | undefined;
|
|
9
|
-
setLivenessSource: (index: number) => void;
|
|
9
|
+
setLivenessSource: (index: number | undefined) => void;
|
|
10
10
|
getCurrentState: () => Value[] | undefined;
|
|
11
11
|
getFingerprint: (state: Value[]) => bigint;
|
|
12
12
|
getRandom: () => JavaRandom;
|
|
@@ -24,6 +24,7 @@ export interface CheckerRuntime {
|
|
|
24
24
|
properties: Map<string, string>;
|
|
25
25
|
possibleCounts: Map<string, number>;
|
|
26
26
|
isStopped: boolean;
|
|
27
|
+
isStarted: boolean;
|
|
27
28
|
checkDeadlock: boolean;
|
|
28
29
|
startedAt: number;
|
|
29
30
|
}
|
|
@@ -75,6 +76,7 @@ export const createRuntime = (checkDeadlock: boolean, seed = 0n): CheckerRuntime
|
|
|
75
76
|
properties: new Map(),
|
|
76
77
|
possibleCounts: new Map(),
|
|
77
78
|
isStopped: false,
|
|
79
|
+
isStarted: false,
|
|
78
80
|
checkDeadlock,
|
|
79
81
|
startedAt: Date.now(),
|
|
80
82
|
};
|
package/src/spec-description.ts
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
isLocallyResolved,
|
|
3
|
+
type Definition,
|
|
4
|
+
type Expression,
|
|
5
|
+
type SourceLocation,
|
|
6
|
+
type SourceRange,
|
|
7
|
+
} from "./ast";
|
|
2
8
|
import type { ModelConfig } from "./config";
|
|
3
9
|
import type { InstanceAction } from "./environment";
|
|
4
|
-
import { TlaError } from "./errors";
|
|
10
|
+
import { formatRange, TlaError } from "./errors";
|
|
5
11
|
import {
|
|
6
12
|
bindDefinitions,
|
|
7
13
|
bindOperatorArguments,
|
|
8
14
|
evaluate,
|
|
9
15
|
forEachBinding,
|
|
10
|
-
formatRange,
|
|
11
16
|
getDefinition,
|
|
12
17
|
isConstantLevel,
|
|
13
|
-
isLocallyResolved,
|
|
14
18
|
lookupScope,
|
|
15
19
|
resolveGlobalOperator,
|
|
16
20
|
type Closure,
|
|
@@ -464,10 +468,17 @@ export const getNextActions = (
|
|
|
464
468
|
instanceActions: Map<string, InstanceAction>,
|
|
465
469
|
) => describe(config, environment, instanceActions).actions;
|
|
466
470
|
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
}
|
|
471
|
+
const formatParameters = (action: SpecAction) =>
|
|
472
|
+
action.parameters.length === 0
|
|
473
|
+
? ""
|
|
474
|
+
: `(${action.parameters.map((parameter) => formatValue(parameter[1])).join(",")})`;
|
|
475
|
+
|
|
476
|
+
export const getActionLabel = (action: SpecAction) =>
|
|
477
|
+
`${action.name === UNNAMED_ACTION ? "Action" : action.name}${formatParameters(action)} ${formatRange(action.location.range ?? BUILTIN_RANGE)}`;
|
|
478
|
+
|
|
479
|
+
// TLC's Action.getInvocationSignature, which names the action and the values of its parameters.
|
|
480
|
+
export const getActionSignature = (action: SpecAction) =>
|
|
481
|
+
`${action.name}${formatParameters(action)}`;
|
|
471
482
|
|
|
472
483
|
export const getSpecificationValue = (
|
|
473
484
|
config: ModelConfig,
|
package/src/standard-levels.ts
CHANGED
|
@@ -28,7 +28,7 @@ const SIGNATURES: Signature[] = [
|
|
|
28
28
|
["Head", 0, [0], [2], [1]],
|
|
29
29
|
["Tail", 0, [0], [2], [1]],
|
|
30
30
|
["SubSeq", 0, [0, 0, 0], [2, 2, 2], [1, 1, 1]],
|
|
31
|
-
["SelectSeq", 0, [0, 1], [2, 2], [1, 1], [], ["1,
|
|
31
|
+
["SelectSeq", 0, [0, 1], [2, 2], [1, 1], [], ["1,0,0"]],
|
|
32
32
|
["IsFiniteSet", 0, [0], [2], [1]],
|
|
33
33
|
["Cardinality", 0, [0], [2], [1]],
|
|
34
34
|
["IsABag", 0, [0], [2], [1]],
|
|
@@ -53,7 +53,7 @@ const SIGNATURES: Signature[] = [
|
|
|
53
53
|
[":>", 0, [0, 0], [2, 3], [1, 1]],
|
|
54
54
|
["@@", 0, [0, 0], [2, 2], [1, 1]],
|
|
55
55
|
["Permutations", 0, [0], [2], [1]],
|
|
56
|
-
["SortSeq", 0, [0, 2], [2, 2], [1, 1], [], ["1,
|
|
56
|
+
["SortSeq", 0, [0, 2], [2, 2], [1, 1], [], ["1,0,0", "1,0,1"]],
|
|
57
57
|
["RandomElement", 0, [0], [3], [1]],
|
|
58
58
|
["Any", 0, [], [], []],
|
|
59
59
|
["ToString", 0, [0], [2], [1]],
|
|
@@ -61,7 +61,7 @@ const SIGNATURES: Signature[] = [
|
|
|
61
61
|
["AssertEq", 0, [0, 0], [2, 2], [1, 1]],
|
|
62
62
|
["AssertError", 0, [0, 0], [2, 3], [1, 0]],
|
|
63
63
|
["TLCGetOrDefault", 0, [0, 0], [3, 3], [0, 0]],
|
|
64
|
-
["TLCGetAndSet", 0, [0, 2, 0, 0], [3, 3, 3, 3], [0, 0, 0, 0], [], ["1,
|
|
64
|
+
["TLCGetAndSet", 0, [0, 2, 0, 0], [3, 3, 3, 3], [0, 0, 0, 0], [], ["1,2,1"]],
|
|
65
65
|
["Trace", 0, [], [], []],
|
|
66
66
|
["CounterExample", 0, [], [], []],
|
|
67
67
|
["ToTrace", 0, [0], [3], [0]],
|
package/src/standard-modules.ts
CHANGED
|
@@ -40,6 +40,7 @@ import {
|
|
|
40
40
|
isEnumerable,
|
|
41
41
|
isFiniteSet,
|
|
42
42
|
isFunctionValue,
|
|
43
|
+
isReducible,
|
|
43
44
|
isSetValue,
|
|
44
45
|
toFiniteSet,
|
|
45
46
|
unionSorted,
|
|
@@ -47,7 +48,7 @@ import {
|
|
|
47
48
|
createEnumeratedSet,
|
|
48
49
|
EMPTY_SET,
|
|
49
50
|
isEnumeratedSet,
|
|
50
|
-
type
|
|
51
|
+
type ReducibleSet,
|
|
51
52
|
type MixedSetValue,
|
|
52
53
|
type SetOperationValue,
|
|
53
54
|
type SetValue,
|
|
@@ -75,12 +76,6 @@ const unionInTree = (sets: SetValue[], start: number, end: number): SetValue =>
|
|
|
75
76
|
|
|
76
77
|
const SET_OPERATORS: SetOperationValue["operator"][] = ["\\cup", "\\cap", "\\"];
|
|
77
78
|
|
|
78
|
-
type ReducibleSet = SetValue | MixedSetValue | IntervalValue;
|
|
79
|
-
|
|
80
|
-
const isReducible = (value: Value): value is ReducibleSet =>
|
|
81
|
-
typeof value === "object" &&
|
|
82
|
-
(value.kind === "Set" || value.kind === "MixedSet" || value.kind === "Interval");
|
|
83
|
-
|
|
84
79
|
const isMixedSet = (value: Value) => typeof value === "object" && value.kind === "MixedSet";
|
|
85
80
|
|
|
86
81
|
const getReducibleElements = (set: ReducibleSet) =>
|