tla-ts 0.0.1 → 0.0.3-dev.037ac3b
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 +98 -39
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +225 -30
- package/dist/index.d.ts +225 -30
- 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 +21 -1
- package/dist/run-cli.js +21 -1
- package/dist/transfer.cjs +400 -39
- package/dist/transfer.js +400 -39
- package/package.json +6 -2
- package/src/ast.ts +213 -6
- package/src/bags.ts +161 -71
- package/src/builtins.ts +203 -23
- package/src/bundled-modules.ts +63 -32
- package/src/check.ts +1132 -258
- package/src/cli-options.ts +64 -0
- package/src/codegen.ts +58 -34
- package/src/config.ts +39 -11
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +557 -153
- package/src/errors.ts +66 -1
- package/src/evaluate.ts +553 -258
- package/src/exit-codes.ts +53 -0
- package/src/fingerprints.ts +48 -13
- package/src/index.ts +9 -4
- package/src/instances.ts +235 -79
- package/src/java-overrides.ts +241 -0
- package/src/json.ts +153 -72
- package/src/levels.ts +850 -0
- package/src/lexer.ts +108 -29
- package/src/live-check.ts +692 -0
- package/src/liveness.ts +71 -596
- 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 +186 -201
- package/src/parallel.ts +179 -84
- package/src/parser.ts +970 -206
- package/src/pretty-print.ts +259 -0
- package/src/randomization.ts +24 -113
- package/src/resolve.ts +483 -51
- package/src/run-cli.ts +473 -62
- package/src/runtime.ts +65 -28
- package/src/spec-description.ts +521 -0
- package/src/standard-levels.ts +105 -0
- package/src/standard-modules.ts +218 -92
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +31 -45
- package/src/substitution-levels.ts +184 -0
- package/src/successors.ts +336 -57
- package/src/symmetry.ts +79 -23
- package/src/tableau.ts +93 -60
- package/src/tlc-ext.ts +148 -44
- package/src/tlc-module.ts +71 -66
- package/src/tlc-modules.ts +1022 -0
- package/src/tlc-order.ts +511 -0
- package/src/tlc-random.ts +414 -0
- package/src/tlc-summary.ts +124 -0
- package/src/transfer.ts +26 -5
- package/src/values.ts +1285 -179
package/src/parallel.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Expression } from "./ast";
|
|
2
2
|
import { createCanonicalizer } from "./canonical";
|
|
3
3
|
import {
|
|
4
|
-
|
|
4
|
+
checkSequentially,
|
|
5
5
|
createSearch,
|
|
6
6
|
findAssumptionViolation,
|
|
7
7
|
type CheckOptions,
|
|
@@ -9,11 +9,21 @@ import {
|
|
|
9
9
|
} from "./check";
|
|
10
10
|
import { parseConfig, type ModelConfig } from "./config";
|
|
11
11
|
import { createEnvironment } from "./environment";
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
12
|
+
import { TlaEvaluationError } from "./errors";
|
|
13
|
+
import { isImpure } from "./evaluate";
|
|
14
|
+
import {
|
|
15
|
+
createSharedFingerprints,
|
|
16
|
+
growSharedFingerprints,
|
|
17
|
+
insertSharedFingerprint,
|
|
18
|
+
isSharedFingerprintsFull,
|
|
19
|
+
writeStateFingerprint,
|
|
20
|
+
type SharedFingerprints,
|
|
21
|
+
} from "./fingerprints";
|
|
15
22
|
import { getInitialStates } from "./successors";
|
|
23
|
+
import { getTlcStringOrder, getTlcVariableOrder } from "./tlc-order";
|
|
24
|
+
import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
|
|
16
25
|
import { createStateEncoder, type EncodedStates } from "./transfer";
|
|
26
|
+
import { getRankedStringCount, setStringOrder, type Value } from "./values";
|
|
17
27
|
|
|
18
28
|
const WORKER_THREADS_MODULE = "node:worker_threads";
|
|
19
29
|
|
|
@@ -29,11 +39,13 @@ export interface StartMessage {
|
|
|
29
39
|
source: string;
|
|
30
40
|
config: ModelConfig;
|
|
31
41
|
modules: [string, string | undefined][];
|
|
42
|
+
classes: [string, Uint8Array | undefined][];
|
|
32
43
|
index: number;
|
|
33
|
-
count: number;
|
|
34
44
|
ports: (Port | undefined)[];
|
|
35
45
|
seed: EncodedStates;
|
|
36
|
-
|
|
46
|
+
fingerprints: SharedFingerprints;
|
|
47
|
+
requests: Int32Array;
|
|
48
|
+
remaining: Int32Array;
|
|
37
49
|
}
|
|
38
50
|
|
|
39
51
|
export interface LevelMessage {
|
|
@@ -41,13 +53,19 @@ export interface LevelMessage {
|
|
|
41
53
|
level: number;
|
|
42
54
|
generated: number;
|
|
43
55
|
added: number;
|
|
56
|
+
outDegrees: number[];
|
|
57
|
+
fingerprints: BigInt64Array;
|
|
44
58
|
}
|
|
45
59
|
|
|
46
|
-
export type WorkerMessage =
|
|
60
|
+
export type WorkerMessage =
|
|
61
|
+
| { type: "ready" }
|
|
62
|
+
| { type: "abort" }
|
|
63
|
+
| { type: "paused" }
|
|
64
|
+
| LevelMessage;
|
|
47
65
|
|
|
48
|
-
export
|
|
49
|
-
|
|
50
|
-
}
|
|
66
|
+
export type CoordinatorMessage =
|
|
67
|
+
| { type: "level"; level: number }
|
|
68
|
+
| { type: "resize"; fingerprints: SharedFingerprints };
|
|
51
69
|
|
|
52
70
|
interface WorkerHandle {
|
|
53
71
|
postMessage: (message: unknown, transfer?: unknown[]) => void;
|
|
@@ -73,7 +91,7 @@ const spawnWorker = async (onMessage: (message: WorkerMessage) => void): Promise
|
|
|
73
91
|
/* @vite-ignore */ /* webpackIgnore: true */ WORKER_THREADS_MODULE
|
|
74
92
|
);
|
|
75
93
|
const worker = new threads.Worker(new URL("./parallel-worker.js", import.meta.url));
|
|
76
|
-
worker.on("message",
|
|
94
|
+
worker.on("message", onMessage);
|
|
77
95
|
worker.on("error", () => onMessage({ type: "abort" }));
|
|
78
96
|
return {
|
|
79
97
|
postMessage: (message, transfer) =>
|
|
@@ -82,93 +100,153 @@ const spawnWorker = async (onMessage: (message: WorkerMessage) => void): Promise
|
|
|
82
100
|
};
|
|
83
101
|
};
|
|
84
102
|
|
|
85
|
-
const
|
|
86
|
-
((expression.kind === "Apply" || expression.kind === "OperatorReference") &&
|
|
87
|
-
isImpureBuiltin(expression.operator, environment)) ||
|
|
88
|
-
(expression.kind === "Identifier" && isImpureBuiltin(expression.name, environment)) ||
|
|
89
|
-
getChildren(expression).some((child) => usesImpureBuiltin(child, environment));
|
|
90
|
-
|
|
91
|
-
const isImpure = (environment: Environment) =>
|
|
92
|
-
[...environment.definitions.values()].some(
|
|
93
|
-
(definition) =>
|
|
94
|
-
usesImpureBuiltin(definition.body, environment) ||
|
|
95
|
-
(definition.kind === "Function" &&
|
|
96
|
-
definition.bounds.some((bound) => usesImpureBuiltin(bound.set, environment))),
|
|
97
|
-
);
|
|
98
|
-
|
|
99
|
-
export const checkInParallel = async (options: ParallelCheckOptions): Promise<CheckResult> => {
|
|
103
|
+
export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
100
104
|
const config = typeof options.config === "string" ? parseConfig(options.config) : options.config;
|
|
101
|
-
const
|
|
105
|
+
const print = options.onPrint ?? console.log;
|
|
106
|
+
const printed = { count: 0 };
|
|
107
|
+
// The search on the calling thread prints what this one printed while it started, so it skips that.
|
|
108
|
+
const fallBack = () => {
|
|
109
|
+
const skipped = { count: printed.count };
|
|
110
|
+
return checkSequentially({
|
|
111
|
+
...options,
|
|
112
|
+
config,
|
|
113
|
+
onPrint: (text) => (skipped.count-- > 0 ? undefined : print(text)),
|
|
114
|
+
});
|
|
115
|
+
};
|
|
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.
|
|
102
119
|
const isSequential =
|
|
103
|
-
|
|
120
|
+
count < 2 ||
|
|
121
|
+
typeof SharedArrayBuffer !== "function" ||
|
|
122
|
+
options.dump !== undefined ||
|
|
104
123
|
config.view !== undefined ||
|
|
105
124
|
config.postconditions.length > 0 ||
|
|
106
125
|
config.possible.length > 0;
|
|
107
|
-
if (isSequential) return
|
|
126
|
+
if (isSequential) return fallBack();
|
|
108
127
|
const modules = new Map<string, string | undefined>();
|
|
109
|
-
const
|
|
110
|
-
loadModule
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
128
|
+
const loadModule = (name: string) => {
|
|
129
|
+
const source = options.loadModule?.(name);
|
|
130
|
+
modules.set(name, source);
|
|
131
|
+
return source;
|
|
132
|
+
};
|
|
133
|
+
const classes = new Map<string, Uint8Array | undefined>();
|
|
134
|
+
const loadClass = (name: string) => {
|
|
135
|
+
const bytes = options.loadClass?.(name);
|
|
136
|
+
classes.set(name, bytes);
|
|
137
|
+
return bytes;
|
|
138
|
+
};
|
|
139
|
+
setStringOrder(getTlcStringOrder(options.source, config, loadModule));
|
|
140
|
+
const startupStrings = getRankedStringCount();
|
|
141
|
+
const specification = createEnvironment(config, {
|
|
142
|
+
source: options.source,
|
|
143
|
+
loadModule,
|
|
144
|
+
loadClass,
|
|
145
|
+
print: (text) => {
|
|
146
|
+
printed.count++;
|
|
147
|
+
print(text);
|
|
114
148
|
},
|
|
115
|
-
print: options.onPrint ?? console.log,
|
|
116
149
|
files: { readFile: options.readFile, writeFile: options.writeFile },
|
|
117
150
|
});
|
|
118
151
|
const environment = specification.environment;
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
152
|
+
const search = createSearch(config, environment, () =>
|
|
153
|
+
getTlcVariableOrder(options.source, loadModule),
|
|
154
|
+
);
|
|
155
|
+
if (findAssumptionViolation(specification.assumptions, environment)) return fallBack();
|
|
156
|
+
if (
|
|
157
|
+
!search ||
|
|
158
|
+
search.spec.init === undefined ||
|
|
159
|
+
search.spec.next === undefined ||
|
|
160
|
+
search.properties.liveness.length > 0 ||
|
|
161
|
+
isImpure(environment)
|
|
162
|
+
) {
|
|
163
|
+
return fallBack();
|
|
123
164
|
}
|
|
124
165
|
|
|
125
|
-
const count = options.workers;
|
|
126
166
|
const canonicalize = createCanonicalizer();
|
|
127
|
-
const
|
|
167
|
+
const shared = { fingerprints: createSharedFingerprints() };
|
|
128
168
|
const fingerprint = new Int32Array(2);
|
|
129
|
-
const seeds = Array.from({ length: count },
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
169
|
+
const seeds = Array.from({ length: count }, createStateEncoder);
|
|
170
|
+
const statistics = {
|
|
171
|
+
generatedStates: 0,
|
|
172
|
+
distinctStates: 0,
|
|
173
|
+
queuedStates: 0,
|
|
174
|
+
depth: 0,
|
|
175
|
+
outDegrees: Array.from({ length: OUT_DEGREE_BUCKETS }, () => 0),
|
|
176
|
+
};
|
|
177
|
+
const tlcFingerprints: BigInt64Array[] = [];
|
|
178
|
+
const createSuccess = (): CheckResult => ({
|
|
179
|
+
status: "success",
|
|
180
|
+
violation: undefined,
|
|
181
|
+
failure: undefined,
|
|
182
|
+
...statistics,
|
|
183
|
+
fingerprintDistance: getFingerprintDistance(
|
|
184
|
+
statistics.distinctStates,
|
|
185
|
+
statistics.generatedStates,
|
|
186
|
+
() => {
|
|
187
|
+
const fingerprints = new BigInt64Array(statistics.distinctStates);
|
|
188
|
+
tlcFingerprints.reduce((offset, level) => {
|
|
189
|
+
fingerprints.set(level, offset);
|
|
190
|
+
return offset + level.length;
|
|
191
|
+
}, 0);
|
|
192
|
+
return fingerprints;
|
|
193
|
+
},
|
|
194
|
+
),
|
|
195
|
+
});
|
|
196
|
+
const seedInitialStates = (init: Expression) => {
|
|
197
|
+
const seeded: Value[][] = [];
|
|
198
|
+
for (const state of getInitialStates(init, environment, search.spec.scope)) {
|
|
199
|
+
canonicalize(state);
|
|
200
|
+
statistics.generatedStates++;
|
|
201
|
+
writeStateFingerprint(search.getFingerprintState(state), fingerprint);
|
|
202
|
+
if (isSharedFingerprintsFull(shared.fingerprints)) {
|
|
203
|
+
shared.fingerprints = growSharedFingerprints(shared.fingerprints);
|
|
204
|
+
}
|
|
205
|
+
if (!insertSharedFingerprint(shared.fingerprints, fingerprint[0], fingerprint[1])) continue;
|
|
206
|
+
if (search.findViolatedInvariant(state) || search.findFailedInitialPredicate(state)) {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
if (!search.satisfiesConstraints(state)) continue;
|
|
210
|
+
if (!seeds[seeded.length % count].encode(state)) return false;
|
|
211
|
+
seeded.push(state);
|
|
212
|
+
statistics.distinctStates++;
|
|
145
213
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
214
|
+
tlcFingerprints.push(
|
|
215
|
+
BigInt64Array.from(seeded, (state) => environment.runtime.getFingerprint(state)),
|
|
216
|
+
);
|
|
217
|
+
return true;
|
|
218
|
+
};
|
|
219
|
+
// The sequential search reports a violation or a failed evaluation in the initial states.
|
|
220
|
+
try {
|
|
221
|
+
if (!seedInitialStates(search.spec.init)) return fallBack();
|
|
222
|
+
} catch (error) {
|
|
223
|
+
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
224
|
+
return fallBack();
|
|
150
225
|
}
|
|
151
|
-
if (
|
|
152
|
-
|
|
153
|
-
statistics.
|
|
226
|
+
if (getRankedStringCount() !== startupStrings) return fallBack();
|
|
227
|
+
statistics.depth = statistics.generatedStates === 0 ? 0 : 1;
|
|
228
|
+
if (statistics.distinctStates === 0) return createSuccess();
|
|
154
229
|
|
|
155
230
|
const reports: WorkerMessage[] = [];
|
|
156
|
-
const pending = { resolve: (
|
|
231
|
+
const pending = { count, resolve: (_reports: WorkerMessage[]) => {} };
|
|
157
232
|
const onMessage = (message: WorkerMessage) => {
|
|
158
233
|
reports.push(message);
|
|
159
|
-
if (message.type === "abort" || reports.length === count) {
|
|
234
|
+
if (message.type === "abort" || reports.length === pending.count) {
|
|
160
235
|
pending.resolve(reports.splice(0));
|
|
161
236
|
}
|
|
162
237
|
};
|
|
163
|
-
const waitForReports = () =>
|
|
238
|
+
const waitForReports = (expected: number) =>
|
|
164
239
|
new Promise<WorkerMessage[]>((resolve) => {
|
|
240
|
+
pending.count = expected;
|
|
165
241
|
pending.resolve = resolve;
|
|
166
242
|
});
|
|
167
|
-
const ready = waitForReports();
|
|
243
|
+
const ready = waitForReports(count);
|
|
244
|
+
const requests = new Int32Array(new SharedArrayBuffer(count * 4));
|
|
245
|
+
const remaining = new Int32Array(new SharedArrayBuffer(count * 4));
|
|
168
246
|
const workers = await Promise.all(Array.from({ length: count }, () => spawnWorker(onMessage)));
|
|
169
247
|
const terminate = () => workers.forEach((worker) => worker.terminate());
|
|
170
248
|
const channels = new Map<string, MessageChannel>();
|
|
171
|
-
const getPort = (from: number, to: number)
|
|
249
|
+
const getPort = (from: number, to: number) => {
|
|
172
250
|
const key = from < to ? `${from}:${to}` : `${to}:${from}`;
|
|
173
251
|
const channel = channels.get(key) ?? new MessageChannel();
|
|
174
252
|
channels.set(key, channel);
|
|
@@ -178,46 +256,63 @@ export const checkInParallel = async (options: ParallelCheckOptions): Promise<Ch
|
|
|
178
256
|
const ports = Array.from({ length: count }, (_, peer) =>
|
|
179
257
|
peer === index ? undefined : getPort(index, peer),
|
|
180
258
|
);
|
|
181
|
-
const seed = seeds[index].
|
|
182
|
-
const seedFingerprints = Int32Array.from(seeds[index].fingerprints);
|
|
259
|
+
const seed = seeds[index].finish();
|
|
183
260
|
const message: StartMessage = {
|
|
184
261
|
type: "start",
|
|
185
262
|
source: options.source,
|
|
186
263
|
config,
|
|
187
264
|
modules: [...modules],
|
|
265
|
+
classes: [...classes],
|
|
188
266
|
index,
|
|
189
|
-
count,
|
|
190
267
|
ports,
|
|
191
268
|
seed,
|
|
192
|
-
|
|
269
|
+
fingerprints: shared.fingerprints,
|
|
270
|
+
requests,
|
|
271
|
+
remaining,
|
|
193
272
|
};
|
|
194
|
-
worker.postMessage(message, [
|
|
195
|
-
...ports.filter((port) => port !== undefined),
|
|
196
|
-
seed.words.buffer,
|
|
197
|
-
seedFingerprints.buffer,
|
|
198
|
-
]);
|
|
273
|
+
worker.postMessage(message, [...ports.filter((port) => port !== undefined), seed.words.buffer]);
|
|
199
274
|
});
|
|
200
275
|
const isAborted = (messages: WorkerMessage[]) =>
|
|
201
276
|
messages.some((message) => message.type === "abort");
|
|
277
|
+
const send = (message: CoordinatorMessage) =>
|
|
278
|
+
workers.forEach((worker) => worker.postMessage(message));
|
|
279
|
+
// Workers pause when the shared table needs to grow, and resume once it has.
|
|
280
|
+
const collectLevels = async (messages: WorkerMessage[]): Promise<WorkerMessage[]> => {
|
|
281
|
+
const paused = messages.filter((message) => message.type === "paused").length;
|
|
282
|
+
if (paused === 0 || isAborted(messages)) return messages;
|
|
283
|
+
shared.fingerprints = growSharedFingerprints(shared.fingerprints);
|
|
284
|
+
const resumed = waitForReports(paused);
|
|
285
|
+
send({ type: "resize", fingerprints: shared.fingerprints });
|
|
286
|
+
const reported = messages.filter((message) => message.type !== "paused");
|
|
287
|
+
return [...reported, ...(await collectLevels(await resumed))];
|
|
288
|
+
};
|
|
202
289
|
if (isAborted(await ready)) {
|
|
203
290
|
terminate();
|
|
204
|
-
return
|
|
291
|
+
return fallBack();
|
|
205
292
|
}
|
|
206
293
|
for (let level = 1; ; level++) {
|
|
207
|
-
const reported = waitForReports();
|
|
208
|
-
|
|
209
|
-
|
|
294
|
+
const reported = waitForReports(count);
|
|
295
|
+
// Every worker is idle, so peers can ask any of them for states once the level starts.
|
|
296
|
+
requests.forEach((_, index) => Atomics.store(requests, index, 0));
|
|
297
|
+
send({ type: "level", level });
|
|
298
|
+
const messages = await collectLevels(await reported);
|
|
210
299
|
if (isAborted(messages)) {
|
|
211
300
|
terminate();
|
|
212
|
-
return
|
|
301
|
+
return fallBack();
|
|
213
302
|
}
|
|
214
303
|
const levels = messages.flatMap((message) => (message.type === "level" ? [message] : []));
|
|
215
304
|
const added = levels.reduce((sum, message) => sum + message.added, 0);
|
|
216
305
|
statistics.generatedStates += levels.reduce((sum, message) => sum + message.generated, 0);
|
|
217
306
|
statistics.distinctStates += added;
|
|
307
|
+
levels.forEach((message) => {
|
|
308
|
+
message.outDegrees.forEach(
|
|
309
|
+
(degreeCount, degree) => (statistics.outDegrees[degree] += degreeCount),
|
|
310
|
+
);
|
|
311
|
+
tlcFingerprints.push(message.fingerprints);
|
|
312
|
+
});
|
|
218
313
|
if (added === 0) {
|
|
219
314
|
terminate();
|
|
220
|
-
return
|
|
315
|
+
return createSuccess();
|
|
221
316
|
}
|
|
222
317
|
statistics.depth++;
|
|
223
318
|
}
|