tla-ts 0.0.3-dev.6979965 → 0.0.3-dev.6a30eee
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 +11 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- 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 +3 -2
- package/dist/run-cli.js +3 -2
- package/dist/transfer.cjs +19 -15
- package/dist/transfer.js +19 -15
- package/package.json +1 -1
- package/src/check.ts +70 -28
- package/src/errors.ts +5 -0
- package/src/evaluate.ts +41 -12
- package/src/exit-codes.ts +20 -30
- package/src/lexer.ts +5 -1
- package/src/live-check.ts +710 -0
- package/src/liveness.ts +4 -521
- package/src/parallel-worker.ts +100 -113
- package/src/parallel.ts +29 -21
- package/src/parser.ts +12 -1
- package/src/run-cli.ts +9 -2
- package/src/runtime.ts +1 -1
- package/src/successors.ts +10 -6
- package/src/symmetry.ts +14 -2
- package/src/tlc-order.ts +17 -3
- package/src/values.ts +7 -4
package/src/parallel-worker.ts
CHANGED
|
@@ -21,56 +21,21 @@ import { createStateDecoder, createStateEncoder, type EncodedStates } from "./tr
|
|
|
21
21
|
import { getRankedStringCount, setStringOrder, type Value } from "./values";
|
|
22
22
|
|
|
23
23
|
const WORKER_THREADS_MODULE = "node:worker_threads";
|
|
24
|
-
|
|
25
|
-
interface PeerMessage {
|
|
26
|
-
phase: "balance";
|
|
27
|
-
level: number;
|
|
28
|
-
from: number;
|
|
29
|
-
states: EncodedStates;
|
|
30
|
-
}
|
|
24
|
+
const LOCKED = -1;
|
|
31
25
|
|
|
32
26
|
interface Expansion {
|
|
27
|
+
states: Value[][];
|
|
33
28
|
origin: number;
|
|
29
|
+
end: number;
|
|
34
30
|
successors: Successor[] | undefined;
|
|
35
31
|
index: number;
|
|
32
|
+
added: number;
|
|
36
33
|
generated: number;
|
|
37
|
-
|
|
34
|
+
outDegrees: number[];
|
|
38
35
|
next: Value[][];
|
|
39
36
|
fingerprintStates: Value[][];
|
|
40
37
|
}
|
|
41
38
|
|
|
42
|
-
interface Transfer {
|
|
43
|
-
from: number;
|
|
44
|
-
to: number;
|
|
45
|
-
count: number;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const getBalancePlan = (sizes: number[]) => {
|
|
49
|
-
const total = sizes.reduce((sum, size) => sum + size, 0);
|
|
50
|
-
const average = total / sizes.length;
|
|
51
|
-
if (Math.max(...sizes) - Math.min(...sizes) <= Math.max(64, average / 4)) return [];
|
|
52
|
-
const targets = sizes.map(
|
|
53
|
-
(_, index) => Math.floor(average) + (index < total % sizes.length ? 1 : 0),
|
|
54
|
-
);
|
|
55
|
-
const donors = sizes.flatMap((size, index) =>
|
|
56
|
-
size > targets[index] ? [{ index, amount: size - targets[index] }] : [],
|
|
57
|
-
);
|
|
58
|
-
const receivers = sizes.flatMap((size, index) =>
|
|
59
|
-
size < targets[index] ? [{ index, amount: targets[index] - size }] : [],
|
|
60
|
-
);
|
|
61
|
-
const transfers: Transfer[] = [];
|
|
62
|
-
for (const donor of donors) {
|
|
63
|
-
for (const receiver of receivers) {
|
|
64
|
-
const moved = Math.min(donor.amount, receiver.amount);
|
|
65
|
-
if (moved === 0) continue;
|
|
66
|
-
transfers.push({ from: donor.index, to: receiver.index, count: moved });
|
|
67
|
-
donor.amount -= moved;
|
|
68
|
-
receiver.amount -= moved;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
return transfers;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
39
|
const getParentPort = async () => {
|
|
75
40
|
if (typeof (globalThis as { postMessage?: unknown }).postMessage === "function") {
|
|
76
41
|
return globalThis as unknown as Port;
|
|
@@ -81,33 +46,6 @@ const getParentPort = async () => {
|
|
|
81
46
|
return threads.parentPort as unknown as Port;
|
|
82
47
|
};
|
|
83
48
|
|
|
84
|
-
const createInbox = () => {
|
|
85
|
-
const messages = new Map<string, PeerMessage[]>();
|
|
86
|
-
const waiters = new Map<string, { count: number; resolve: (messages: PeerMessage[]) => void }>();
|
|
87
|
-
const getKey = (phase: PeerMessage["phase"], level: number) => `${phase}:${level}`;
|
|
88
|
-
const settle = (key: string) => {
|
|
89
|
-
const waiter = waiters.get(key);
|
|
90
|
-
const received = messages.get(key) ?? [];
|
|
91
|
-
if (!waiter || received.length < waiter.count) return;
|
|
92
|
-
waiters.delete(key);
|
|
93
|
-
messages.delete(key);
|
|
94
|
-
waiter.resolve(received);
|
|
95
|
-
};
|
|
96
|
-
return {
|
|
97
|
-
deliver: (message: PeerMessage) => {
|
|
98
|
-
const key = getKey(message.phase, message.level);
|
|
99
|
-
messages.set(key, [...(messages.get(key) ?? []), message]);
|
|
100
|
-
settle(key);
|
|
101
|
-
},
|
|
102
|
-
receive: (phase: PeerMessage["phase"], level: number, count: number) =>
|
|
103
|
-
new Promise<PeerMessage[]>((resolve) => {
|
|
104
|
-
const key = getKey(phase, level);
|
|
105
|
-
waiters.set(key, { count, resolve });
|
|
106
|
-
settle(key);
|
|
107
|
-
}),
|
|
108
|
-
};
|
|
109
|
-
};
|
|
110
|
-
|
|
111
49
|
const run = (start: StartMessage, parent: Port) => {
|
|
112
50
|
const modules = new Map(start.modules);
|
|
113
51
|
const loadModule = (name: string) => modules.get(name);
|
|
@@ -132,26 +70,55 @@ const run = (start: StartMessage, parent: Port) => {
|
|
|
132
70
|
const canonicalize = createCanonicalizer();
|
|
133
71
|
const decode = createStateDecoder(environment.variables.length);
|
|
134
72
|
const shared = { fingerprints: start.fingerprints };
|
|
73
|
+
const requests = start.requests;
|
|
74
|
+
const remaining = start.remaining;
|
|
135
75
|
// Under symmetry, a successor the worker generated before has the representative it had then,
|
|
136
76
|
// so the worker skips the search for one.
|
|
137
77
|
const generatedStates = search.isSymmetric ? createFingerprintTable() : undefined;
|
|
138
78
|
const fingerprint = new Int32Array(2);
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
peer.port.start?.();
|
|
79
|
+
const waiters = { resume: () => {}, loot: (_states: EncodedStates) => {} };
|
|
80
|
+
start.ports.forEach((port) => {
|
|
81
|
+
port?.addEventListener("message", (event) => waiters.loot(event.data as EncodedStates));
|
|
82
|
+
port?.start?.();
|
|
144
83
|
});
|
|
145
84
|
const frontier = { states: decode(start.seed).map(canonicalize) };
|
|
146
|
-
|
|
147
|
-
|
|
85
|
+
|
|
86
|
+
// A worker that runs out of states asks a peer for some by writing its index + 1 to the peer's
|
|
87
|
+
// request word. The peer sends half of the states it hasn't started, keeps expanding, and wakes
|
|
88
|
+
// the workers waiting to ask it next.
|
|
89
|
+
const answer = (expansion: Expansion, request: number) => {
|
|
90
|
+
const thief = Atomics.load(requests, self) - 1;
|
|
91
|
+
const given = Math.floor((expansion.end - expansion.origin - 1) / 2);
|
|
92
|
+
const encoder = createStateEncoder();
|
|
93
|
+
const states = expansion.states.slice(expansion.end - given, expansion.end);
|
|
94
|
+
const isEncoded = given > 0 && states.every(encoder.encode);
|
|
95
|
+
if (isEncoded) expansion.end -= given;
|
|
96
|
+
const loot = isEncoded ? encoder.finish() : createStateEncoder().finish();
|
|
97
|
+
start.ports[thief]?.postMessage(loot, [loot.words.buffer]);
|
|
98
|
+
Atomics.store(requests, self, request);
|
|
99
|
+
Atomics.notify(requests, self);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// A worker answers any request before it pauses or runs out of states, and takes no more until
|
|
103
|
+
// it resumes.
|
|
104
|
+
const lock = (expansion: Expansion) => {
|
|
105
|
+
Atomics.store(remaining, self, 0);
|
|
106
|
+
if (Atomics.compareExchange(requests, self, 0, LOCKED) === 0) return;
|
|
107
|
+
answer(expansion, LOCKED);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const unlock = (expansion: Expansion) => {
|
|
111
|
+
Atomics.store(remaining, self, expansion.end - expansion.origin);
|
|
112
|
+
Atomics.store(requests, self, 0);
|
|
113
|
+
};
|
|
148
114
|
|
|
149
115
|
// The worker pauses before a successor when the shared table needs to grow, and resumes there.
|
|
150
116
|
const expand = (expansion: Expansion) => {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const state = states[expansion.origin];
|
|
117
|
+
for (; expansion.origin < expansion.end; expansion.origin++) {
|
|
118
|
+
const state = expansion.states[expansion.origin];
|
|
154
119
|
if (!expansion.successors) {
|
|
120
|
+
if (Atomics.load(requests, self) > 0) answer(expansion, 0);
|
|
121
|
+
Atomics.store(remaining, self, expansion.end - expansion.origin - 1);
|
|
155
122
|
expansion.successors = getSuccessors(nextAction, state, environment, search.spec.scope);
|
|
156
123
|
expansion.generated += expansion.successors.length;
|
|
157
124
|
if (expansion.successors.length === 0 && start.config.checkDeadlock) return undefined;
|
|
@@ -161,7 +128,11 @@ const run = (start: StartMessage, parent: Port) => {
|
|
|
161
128
|
if (isSharedFingerprintsFull(shared.fingerprints)) return "paused";
|
|
162
129
|
const successor = successors[expansion.index];
|
|
163
130
|
if (search.findViolatedActionProperty(state, successor.state)) return undefined;
|
|
164
|
-
if (!search.satisfiesActionConstraints(state, successor.state))
|
|
131
|
+
if (!search.satisfiesActionConstraints(state, successor.state)) {
|
|
132
|
+
// TLC checks the invariants of every successor it has not seen, even outside the model.
|
|
133
|
+
if (search.findViolatedInvariant(successor.state, state)) return undefined;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
165
136
|
if (generatedStates) {
|
|
166
137
|
writeStateFingerprint(successor.state, fingerprint);
|
|
167
138
|
if (!generatedStates.insert(fingerprint[0], fingerprint[1])) continue;
|
|
@@ -174,69 +145,85 @@ const run = (start: StartMessage, parent: Port) => {
|
|
|
174
145
|
if (!search.satisfiesConstraints(successor.state, state)) continue;
|
|
175
146
|
expansion.next.push(successor.state);
|
|
176
147
|
expansion.fingerprintStates.push(fingerprintState);
|
|
177
|
-
expansion.
|
|
148
|
+
expansion.added++;
|
|
178
149
|
}
|
|
150
|
+
expansion.outDegrees[Math.min(expansion.added, OUT_DEGREE_BUCKETS - 1)]++;
|
|
179
151
|
expansion.successors = undefined;
|
|
180
152
|
expansion.index = 0;
|
|
153
|
+
expansion.added = 0;
|
|
181
154
|
}
|
|
182
155
|
return "done";
|
|
183
156
|
};
|
|
184
157
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
const
|
|
192
|
-
|
|
158
|
+
// Each attempt asks the peer with the most states left. While another worker's request to that
|
|
159
|
+
// peer is open, the worker waits for the peer to answer it. The worker stops once no peer that
|
|
160
|
+
// takes requests has two states left.
|
|
161
|
+
const steal = async (expansion: Expansion) => {
|
|
162
|
+
const skipped = new Set<number>();
|
|
163
|
+
for (;;) {
|
|
164
|
+
const victim = start.ports.reduce<number | undefined>((best, port, index) => {
|
|
165
|
+
if (!port || skipped.has(index) || Atomics.load(remaining, index) < 2) return best;
|
|
166
|
+
return best === undefined || Atomics.load(remaining, index) > Atomics.load(remaining, best)
|
|
167
|
+
? index
|
|
168
|
+
: best;
|
|
169
|
+
}, undefined);
|
|
170
|
+
if (victim === undefined) return false;
|
|
171
|
+
const request = Atomics.compareExchange(requests, victim, 0, self + 1);
|
|
172
|
+
if (request === LOCKED) skipped.add(victim);
|
|
173
|
+
if (request > 0) Atomics.wait(requests, victim, request);
|
|
174
|
+
if (request !== 0) continue;
|
|
175
|
+
const loot = await new Promise<EncodedStates>((resolve) => (waiters.loot = resolve));
|
|
176
|
+
if (loot.count === 0) {
|
|
177
|
+
skipped.add(victim);
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
expansion.states = decode(loot).map(canonicalize);
|
|
181
|
+
expansion.origin = 0;
|
|
182
|
+
expansion.end = expansion.states.length;
|
|
183
|
+
unlock(expansion);
|
|
184
|
+
return true;
|
|
193
185
|
}
|
|
194
|
-
outgoing.forEach((encoder, index) => {
|
|
195
|
-
const states = encoder.finish();
|
|
196
|
-
send(index, { phase: "balance", level, from: self, states }, [states.words.buffer]);
|
|
197
|
-
});
|
|
198
|
-
const incoming = await inbox.receive("balance", level, peers.length);
|
|
199
|
-
incoming.forEach((message) => next.push(...decode(message.states).map(canonicalize)));
|
|
200
|
-
return true;
|
|
201
186
|
};
|
|
202
187
|
|
|
203
|
-
const waiters = { resume: () => {}, balance: (_sizes: number[]) => {} };
|
|
204
188
|
const report = (message: WorkerMessage) =>
|
|
205
189
|
parent.postMessage(message, message.type === "level" ? [message.fingerprints.buffer] : []);
|
|
206
190
|
|
|
207
191
|
const runLevel = async (level: number): Promise<LevelMessage | undefined> => {
|
|
208
192
|
const expansion: Expansion = {
|
|
193
|
+
states: frontier.states,
|
|
209
194
|
origin: 0,
|
|
195
|
+
end: frontier.states.length,
|
|
210
196
|
successors: undefined,
|
|
211
197
|
index: 0,
|
|
198
|
+
added: 0,
|
|
212
199
|
generated: 0,
|
|
213
|
-
|
|
200
|
+
outDegrees: Array.from({ length: OUT_DEGREE_BUCKETS }, () => 0),
|
|
214
201
|
next: [],
|
|
215
202
|
fingerprintStates: [],
|
|
216
203
|
};
|
|
217
|
-
for (
|
|
204
|
+
for (;;) {
|
|
205
|
+
const status = expand(expansion);
|
|
218
206
|
if (status === undefined) return undefined;
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
207
|
+
lock(expansion);
|
|
208
|
+
if (status === "paused") {
|
|
209
|
+
const resumed = new Promise<void>((resolve) => (waiters.resume = resolve));
|
|
210
|
+
report({ type: "paused" });
|
|
211
|
+
await resumed;
|
|
212
|
+
unlock(expansion);
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (!(await steal(expansion))) break;
|
|
222
216
|
}
|
|
223
217
|
if (getRankedStringCount() !== startupStrings) return undefined;
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
const outDegrees = Array.from({ length: OUT_DEGREE_BUCKETS }, () => 0);
|
|
227
|
-
expansion.newStates.forEach((degree) => outDegrees[Math.min(degree, OUT_DEGREE_BUCKETS - 1)]++);
|
|
228
|
-
const fingerprints = search.getStoredFingerprints(next, expansion.fingerprintStates);
|
|
229
|
-
const sizes = new Promise<number[]>((resolve) => (waiters.balance = resolve));
|
|
230
|
-
report({ type: "expanded", from: self, added });
|
|
231
|
-
if (!(await balance(level, next, await sizes))) return undefined;
|
|
232
|
-
frontier.states = next;
|
|
218
|
+
frontier.states = expansion.next;
|
|
219
|
+
Atomics.store(remaining, self, frontier.states.length);
|
|
233
220
|
return {
|
|
234
221
|
type: "level",
|
|
235
222
|
level,
|
|
236
223
|
generated: expansion.generated,
|
|
237
|
-
added,
|
|
238
|
-
outDegrees,
|
|
239
|
-
fingerprints,
|
|
224
|
+
added: expansion.next.length,
|
|
225
|
+
outDegrees: expansion.outDegrees,
|
|
226
|
+
fingerprints: search.getStoredFingerprints(expansion.next, expansion.fingerprintStates),
|
|
240
227
|
};
|
|
241
228
|
};
|
|
242
229
|
|
|
@@ -253,8 +240,8 @@ const run = (start: StartMessage, parent: Port) => {
|
|
|
253
240
|
shared.fingerprints = message.fingerprints;
|
|
254
241
|
waiters.resume();
|
|
255
242
|
}
|
|
256
|
-
if (message.type === "balance") waiters.balance(message.sizes);
|
|
257
243
|
});
|
|
244
|
+
Atomics.store(remaining, self, frontier.states.length);
|
|
258
245
|
report({ type: "ready" });
|
|
259
246
|
};
|
|
260
247
|
|
package/src/parallel.ts
CHANGED
|
@@ -44,6 +44,8 @@ export interface StartMessage {
|
|
|
44
44
|
ports: (Port | undefined)[];
|
|
45
45
|
seed: EncodedStates;
|
|
46
46
|
fingerprints: SharedFingerprints;
|
|
47
|
+
requests: Int32Array;
|
|
48
|
+
remaining: Int32Array;
|
|
47
49
|
}
|
|
48
50
|
|
|
49
51
|
export interface LevelMessage {
|
|
@@ -59,13 +61,11 @@ export type WorkerMessage =
|
|
|
59
61
|
| { type: "ready" }
|
|
60
62
|
| { type: "abort" }
|
|
61
63
|
| { type: "paused" }
|
|
62
|
-
| { type: "expanded"; from: number; added: number }
|
|
63
64
|
| LevelMessage;
|
|
64
65
|
|
|
65
66
|
export type CoordinatorMessage =
|
|
66
67
|
| { type: "level"; level: number }
|
|
67
|
-
| { type: "resize"; fingerprints: SharedFingerprints }
|
|
68
|
-
| { type: "balance"; sizes: number[] };
|
|
68
|
+
| { type: "resize"; fingerprints: SharedFingerprints };
|
|
69
69
|
|
|
70
70
|
interface WorkerHandle {
|
|
71
71
|
postMessage: (message: unknown, transfer?: unknown[]) => void;
|
|
@@ -102,7 +102,17 @@ const spawnWorker = async (onMessage: (message: WorkerMessage) => void): Promise
|
|
|
102
102
|
|
|
103
103
|
export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
104
104
|
const config = typeof options.config === "string" ? parseConfig(options.config) : options.config;
|
|
105
|
-
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
|
+
};
|
|
106
116
|
const count = options.workers ?? 1;
|
|
107
117
|
const isSequential =
|
|
108
118
|
count < 2 ||
|
|
@@ -129,7 +139,10 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
|
129
139
|
source: options.source,
|
|
130
140
|
loadModule,
|
|
131
141
|
loadClass,
|
|
132
|
-
print:
|
|
142
|
+
print: (text) => {
|
|
143
|
+
printed.count++;
|
|
144
|
+
print(text);
|
|
145
|
+
},
|
|
133
146
|
files: { readFile: options.readFile, writeFile: options.writeFile },
|
|
134
147
|
});
|
|
135
148
|
const environment = specification.environment;
|
|
@@ -225,6 +238,8 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
|
225
238
|
pending.resolve = resolve;
|
|
226
239
|
});
|
|
227
240
|
const ready = waitForReports(count);
|
|
241
|
+
const requests = new Int32Array(new SharedArrayBuffer(count * 4));
|
|
242
|
+
const remaining = new Int32Array(new SharedArrayBuffer(count * 4));
|
|
228
243
|
const workers = await Promise.all(Array.from({ length: count }, () => spawnWorker(onMessage)));
|
|
229
244
|
const terminate = () => workers.forEach((worker) => worker.terminate());
|
|
230
245
|
const channels = new Map<string, MessageChannel>();
|
|
@@ -249,6 +264,8 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
|
249
264
|
ports,
|
|
250
265
|
seed,
|
|
251
266
|
fingerprints: shared.fingerprints,
|
|
267
|
+
requests,
|
|
268
|
+
remaining,
|
|
252
269
|
};
|
|
253
270
|
worker.postMessage(message, [...ports.filter((port) => port !== undefined), seed.words.buffer]);
|
|
254
271
|
});
|
|
@@ -257,34 +274,25 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
|
257
274
|
const send = (message: CoordinatorMessage) =>
|
|
258
275
|
workers.forEach((worker) => worker.postMessage(message));
|
|
259
276
|
// Workers pause when the shared table needs to grow, and resume once it has.
|
|
260
|
-
const
|
|
277
|
+
const collectLevels = async (messages: WorkerMessage[]): Promise<WorkerMessage[]> => {
|
|
261
278
|
const paused = messages.filter((message) => message.type === "paused").length;
|
|
262
279
|
if (paused === 0 || isAborted(messages)) return messages;
|
|
263
280
|
shared.fingerprints = growSharedFingerprints(shared.fingerprints);
|
|
264
281
|
const resumed = waitForReports(paused);
|
|
265
282
|
send({ type: "resize", fingerprints: shared.fingerprints });
|
|
266
|
-
const
|
|
267
|
-
return [...
|
|
283
|
+
const reported = messages.filter((message) => message.type !== "paused");
|
|
284
|
+
return [...reported, ...(await collectLevels(await resumed))];
|
|
268
285
|
};
|
|
269
286
|
if (isAborted(await ready)) {
|
|
270
287
|
terminate();
|
|
271
288
|
return fallBack();
|
|
272
289
|
}
|
|
273
290
|
for (let level = 1; ; level++) {
|
|
274
|
-
const expanding = waitForReports(count);
|
|
275
|
-
send({ type: "level", level });
|
|
276
|
-
const expansions = await collectExpansions(await expanding);
|
|
277
|
-
if (isAborted(expansions)) {
|
|
278
|
-
terminate();
|
|
279
|
-
return fallBack();
|
|
280
|
-
}
|
|
281
|
-
const sizes = Array.from({ length: count }, () => 0);
|
|
282
|
-
expansions.forEach((message) => {
|
|
283
|
-
if (message.type === "expanded") sizes[message.from] = message.added;
|
|
284
|
-
});
|
|
285
291
|
const reported = waitForReports(count);
|
|
286
|
-
|
|
287
|
-
|
|
292
|
+
// Every worker is idle, so peers can ask any of them for states once the level starts.
|
|
293
|
+
requests.forEach((_, index) => Atomics.store(requests, index, 0));
|
|
294
|
+
send({ type: "level", level });
|
|
295
|
+
const messages = await collectLevels(await reported);
|
|
288
296
|
if (isAborted(messages)) {
|
|
289
297
|
terminate();
|
|
290
298
|
return fallBack();
|
package/src/parser.ts
CHANGED
|
@@ -135,6 +135,8 @@ const INFIX_OPERATORS = new Map<string, InfixOperator>([
|
|
|
135
135
|
["^^", createInfix(14, 14, false)],
|
|
136
136
|
]);
|
|
137
137
|
|
|
138
|
+
const NEGATION_PRECEDENCE = 12;
|
|
139
|
+
|
|
138
140
|
const POSTFIX_OPERATORS = new Set(["^+", "^*", "^#"]);
|
|
139
141
|
|
|
140
142
|
const PREFIX_OPERATOR_REFERENCES = new Set(["-", "~", "-."]);
|
|
@@ -977,7 +979,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
977
979
|
return { kind: "Apply", operator: "~", args: [parseExpression(5)], location };
|
|
978
980
|
case "-":
|
|
979
981
|
advance();
|
|
980
|
-
return {
|
|
982
|
+
return {
|
|
983
|
+
kind: "Apply",
|
|
984
|
+
operator: "-.",
|
|
985
|
+
args: [parseExpression(NEGATION_PRECEDENCE + 1)],
|
|
986
|
+
location,
|
|
987
|
+
};
|
|
981
988
|
case "[]":
|
|
982
989
|
advance();
|
|
983
990
|
return { kind: "Always", expression: parseExpression(16), location };
|
|
@@ -1054,6 +1061,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1054
1061
|
}
|
|
1055
1062
|
}
|
|
1056
1063
|
advance();
|
|
1064
|
+
// SANY reads - after an infix operator as prefix -, and rejects it after one that binds tighter.
|
|
1065
|
+
if (infix.low > NEGATION_PRECEDENCE && isAt("-")) {
|
|
1066
|
+
throw createError(`Precedence conflict between ${token.text} and -`, peek());
|
|
1067
|
+
}
|
|
1057
1068
|
const right = parseExpression(infix.high + 1);
|
|
1058
1069
|
const left = state.left;
|
|
1059
1070
|
if (token.text === "/\\" || token.text === "\\/") {
|
package/src/run-cli.ts
CHANGED
|
@@ -113,6 +113,11 @@ const printTrace = (trace: TraceStep[], heading = "Error: The behavior up to thi
|
|
|
113
113
|
});
|
|
114
114
|
};
|
|
115
115
|
|
|
116
|
+
const getTemporalHeadline = (names: string[]) =>
|
|
117
|
+
names.length === 1
|
|
118
|
+
? `Error: Temporal property ${names[0]} was violated.\n`
|
|
119
|
+
: `Error: Temporal properties ${names.slice(0, -1).join(", ")}${names.length > 2 ? "," : ""} and ${names[names.length - 1]} were violated.\n`;
|
|
120
|
+
|
|
116
121
|
const printResult = (result: CheckResult) => {
|
|
117
122
|
if (result.failure) {
|
|
118
123
|
console.log(`Error: ${result.failure.message}`);
|
|
@@ -140,10 +145,10 @@ const printResult = (result: CheckResult) => {
|
|
|
140
145
|
"invariant-violation": isInitial
|
|
141
146
|
? `Error: Invariant ${name} is violated by the initial state:`
|
|
142
147
|
: `Error: Invariant ${name} is violated.`,
|
|
143
|
-
"property-violation":
|
|
148
|
+
"property-violation": getTemporalHeadline(violation.properties ?? [name]),
|
|
144
149
|
"liveness-violation":
|
|
145
150
|
loopStart !== undefined
|
|
146
|
-
?
|
|
151
|
+
? getTemporalHeadline(violation.properties ?? [name])
|
|
147
152
|
: isInitial
|
|
148
153
|
? `Error: Property ${name} is violated by the initial state:`
|
|
149
154
|
: `Error: Action property ${name} is violated.`,
|
|
@@ -152,6 +157,8 @@ const printResult = (result: CheckResult) => {
|
|
|
152
157
|
if (violation.counts) console.log(violation.counts);
|
|
153
158
|
console.log(headline);
|
|
154
159
|
if (isInitial) printState(violation.trace[0].state);
|
|
160
|
+
else if (result.status === "property-violation")
|
|
161
|
+
printTrace(violation.trace, "Error: The following behavior constitutes a counter-example:\n");
|
|
155
162
|
else if (loopStart === undefined) printTrace(violation.trace);
|
|
156
163
|
else {
|
|
157
164
|
printTrace(violation.trace, "Error: The following behavior constitutes a counter-example:\n");
|
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;
|
package/src/successors.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { Bound, Definition, Expression } from "./ast";
|
|
2
2
|
import { getReadVariables } from "./dependencies";
|
|
3
|
-
import { TlaEvaluationError } from "./errors";
|
|
3
|
+
import { TlaEvaluationError, UNEXPECTED_EXCEPTION } from "./errors";
|
|
4
4
|
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
5
5
|
import {
|
|
6
6
|
describeLocation,
|
|
7
|
+
describeRange,
|
|
7
8
|
bindArgument,
|
|
8
9
|
bindDefinitions,
|
|
9
10
|
bindOperatorArguments,
|
|
@@ -804,7 +805,9 @@ const compileAction = (
|
|
|
804
805
|
);
|
|
805
806
|
const body = arm?.body ?? other;
|
|
806
807
|
if (!body)
|
|
807
|
-
throw new TlaEvaluationError(
|
|
808
|
+
throw new TlaEvaluationError(
|
|
809
|
+
`${UNEXPECTED_EXCEPTION}In computing next states, TLC encountered a CASE with no conditions true.\n${describeRange(expression)}`,
|
|
810
|
+
);
|
|
808
811
|
body(run, scope, partial, action, isFixed, onComplete);
|
|
809
812
|
};
|
|
810
813
|
}
|
|
@@ -913,18 +916,19 @@ export const isEnabled = (expression: Expression, context: Context): boolean =>
|
|
|
913
916
|
);
|
|
914
917
|
}
|
|
915
918
|
const environment = context.environment;
|
|
916
|
-
const
|
|
919
|
+
const run: Run = { environment, isInit: false, current: state, isStopped: false };
|
|
920
|
+
// TLC stops computing ENABLED at the first successor, so it skips the rest of the action.
|
|
917
921
|
getRunner(expression, environment, false)(
|
|
918
|
-
|
|
922
|
+
run,
|
|
919
923
|
context.scope,
|
|
920
924
|
getEmptyState(environment),
|
|
921
925
|
undefined,
|
|
922
926
|
false,
|
|
923
927
|
() => {
|
|
924
|
-
|
|
928
|
+
run.isStopped = true;
|
|
925
929
|
},
|
|
926
930
|
);
|
|
927
|
-
return
|
|
931
|
+
return run.isStopped;
|
|
928
932
|
};
|
|
929
933
|
|
|
930
934
|
export const forEachInitialState = (
|
package/src/symmetry.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
isEnumerable,
|
|
10
10
|
isFunctionValue,
|
|
11
11
|
isModelValue,
|
|
12
|
+
isSameValue,
|
|
12
13
|
isSetValue,
|
|
13
14
|
toFiniteFunction,
|
|
14
15
|
toFiniteSet,
|
|
@@ -163,7 +164,10 @@ const permuteValue = (value: Value, permutation: Permutation): Value => {
|
|
|
163
164
|
if (size === 0 || size > CACHED_SIZE_LIMIT) return computePermutedValue(value, permutation);
|
|
164
165
|
const cached = permutation.cache.get(value);
|
|
165
166
|
if (cached !== undefined) return cached;
|
|
166
|
-
const
|
|
167
|
+
const computed = computePermutedValue(value, permutation);
|
|
168
|
+
// A permutation often maps a value to an equal one. Keeping the original lets the comparison
|
|
169
|
+
// with the smallest state so far stop at identity.
|
|
170
|
+
const permuted = computed !== value && isSameValue(computed, value) ? value : computed;
|
|
167
171
|
if (permutation.cacheSize >= permutation.cacheLimit) {
|
|
168
172
|
permutation.cache = new WeakMap();
|
|
169
173
|
permutation.cacheSize = 0;
|
|
@@ -184,7 +188,15 @@ const computePermutedValue = (value: ObjectValue, permutation: Permutation) => {
|
|
|
184
188
|
const domain = permuteElements(functionValue.domain, permutation);
|
|
185
189
|
const range = permuteElements(functionValue.range, permutation) ?? functionValue.range;
|
|
186
190
|
if (domain) {
|
|
187
|
-
|
|
191
|
+
const permuted = createFunction(
|
|
192
|
+
domain.map((key, index): [Value, Value] => [key, range[index]]),
|
|
193
|
+
);
|
|
194
|
+
// A permutation often maps the domain to itself, such as a function over the symmetric set.
|
|
195
|
+
// Sharing the original domain array lets comparisons with other images skip the domain.
|
|
196
|
+
const isSameDomain = permuted.domain.every(
|
|
197
|
+
(key, index) => key === functionValue.domain[index],
|
|
198
|
+
);
|
|
199
|
+
return isSameDomain ? createSortedFunction(functionValue.domain, permuted.range) : permuted;
|
|
188
200
|
}
|
|
189
201
|
return range === functionValue.range
|
|
190
202
|
? functionValue
|
package/src/tlc-order.ts
CHANGED
|
@@ -184,9 +184,18 @@ const isAdjacent = (left: Token, right: Token | undefined) =>
|
|
|
184
184
|
right.line === left.line &&
|
|
185
185
|
right.column === left.column + left.text.length;
|
|
186
186
|
|
|
187
|
+
const STRING_ESCAPES: Record<string, string> = {
|
|
188
|
+
"\\": "\\\\",
|
|
189
|
+
'"': '\\"',
|
|
190
|
+
"\n": "\\n",
|
|
191
|
+
"\t": "\\t",
|
|
192
|
+
"\r": "\\r",
|
|
193
|
+
"\f": "\\f",
|
|
194
|
+
};
|
|
195
|
+
|
|
187
196
|
// The texts SANY interns while parsing, in order. SANY reads a proof step label such as <1>2 and
|
|
188
|
-
// a module header such as ---- MODULE as one token
|
|
189
|
-
// during semantic analysis.
|
|
197
|
+
// a module header such as ---- MODULE as one token. It interns a string literal as written, quotes
|
|
198
|
+
// included, and interns the literal's contents only during semantic analysis.
|
|
190
199
|
const getParsedTexts = (tokens: Token[]) => {
|
|
191
200
|
const texts: string[] = [];
|
|
192
201
|
for (let index = 0; index < tokens.length; index++) {
|
|
@@ -202,7 +211,12 @@ const getParsedTexts = (tokens: Token[]) => {
|
|
|
202
211
|
continue;
|
|
203
212
|
}
|
|
204
213
|
const isHeader = token.text === "MODULE" && tokens[index - 1]?.kind === "Separator";
|
|
205
|
-
if (
|
|
214
|
+
if (isHeader) continue;
|
|
215
|
+
texts.push(
|
|
216
|
+
token.kind === "String"
|
|
217
|
+
? `"${token.text.replace(/[\\"\n\t\r\f]/g, (character) => STRING_ESCAPES[character])}"`
|
|
218
|
+
: token.text,
|
|
219
|
+
);
|
|
206
220
|
}
|
|
207
221
|
return texts;
|
|
208
222
|
};
|