tla-ts 0.0.3-dev.79f7ed7 → 0.0.3-dev.868dd4c
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 +16 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +10 -0
- package/dist/index.d.ts +10 -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 -16
- package/dist/run-cli.js +6 -5
- package/dist/transfer.cjs +27 -15
- package/dist/transfer.js +27 -15
- package/package.json +1 -1
- package/src/ast.ts +90 -0
- package/src/check.ts +259 -76
- package/src/cli-options.ts +11 -5
- package/src/codegen.ts +15 -22
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +88 -20
- package/src/errors.ts +18 -2
- package/src/evaluate.ts +117 -147
- package/src/exit-codes.ts +21 -43
- package/src/instances.ts +15 -14
- package/src/levels.ts +8 -8
- package/src/lexer.ts +5 -1
- package/src/live-check.ts +770 -0
- package/src/liveness.ts +54 -535
- package/src/parallel-worker.ts +7 -3
- package/src/parallel.ts +20 -4
- package/src/parser.ts +12 -1
- package/src/resolve.ts +16 -15
- package/src/run-cli.ts +58 -13
- package/src/runtime.ts +3 -1
- package/src/spec-description.ts +18 -6
- package/src/standard-modules.ts +2 -7
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +10 -10
- package/src/successors.ts +78 -14
- package/src/symmetry.ts +30 -8
- package/src/tableau.ts +51 -24
- package/src/tlc-ext.ts +29 -12
- package/src/tlc-module.ts +9 -28
- package/src/tlc-order.ts +25 -7
- package/src/tlc-random.ts +9 -6
- package/src/tlc-summary.ts +20 -5
- package/src/values.ts +128 -24
|
@@ -0,0 +1,770 @@
|
|
|
1
|
+
import { TlaEvaluationError } from "./errors";
|
|
2
|
+
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
3
|
+
import { hasRepeatedEffects, type Environment } from "./evaluate";
|
|
4
|
+
import {
|
|
5
|
+
evaluateLive,
|
|
6
|
+
TAUTOLOGY_MESSAGE,
|
|
7
|
+
evaluateOnLasso,
|
|
8
|
+
toLiveExpression,
|
|
9
|
+
type ScopedExpression,
|
|
10
|
+
type TemporalProperty,
|
|
11
|
+
} from "./liveness";
|
|
12
|
+
import {
|
|
13
|
+
createJunction,
|
|
14
|
+
createUnary,
|
|
15
|
+
getSolutions,
|
|
16
|
+
getTermKey,
|
|
17
|
+
isFulfilling,
|
|
18
|
+
type LiveExpression,
|
|
19
|
+
type Solution,
|
|
20
|
+
type Tableau,
|
|
21
|
+
} from "./tableau";
|
|
22
|
+
import { isSameValue, type Value } from "./values";
|
|
23
|
+
|
|
24
|
+
export interface LiveSuccessor {
|
|
25
|
+
index: number;
|
|
26
|
+
state: Value[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface Lasso {
|
|
30
|
+
prefix: number[];
|
|
31
|
+
cycle: number[];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface LivenessChecker {
|
|
35
|
+
addInitialState: (index: number, state: Value[]) => void;
|
|
36
|
+
addState: (index: number, state: Value[], successors: LiveSuccessor[]) => number[] | undefined;
|
|
37
|
+
findViolation: () => Lasso | undefined;
|
|
38
|
+
getViolatedProperties: (path: Value[][], cycleStart: number) => string[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface LiveGraph {
|
|
42
|
+
getFingerprint: (index: number) => number;
|
|
43
|
+
getSuccessors: (state: Value[]) => LiveSuccessor[];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// TLC's behavior graph for one solution: a node per state, or per state and tableau node.
|
|
47
|
+
interface Check {
|
|
48
|
+
solution: Solution;
|
|
49
|
+
// One id per distinct action check across the solutions.
|
|
50
|
+
actionIds: Int32Array;
|
|
51
|
+
nodeStates: number[];
|
|
52
|
+
nodeTableaux: number[];
|
|
53
|
+
// The successors a node gathers until it is written.
|
|
54
|
+
pending: (number[] | undefined)[];
|
|
55
|
+
// Each written node's state results, then its action results edge by edge, from the node's
|
|
56
|
+
// offset in one buffer, so a node costs no objects of its own. An unwritten node has offset -1.
|
|
57
|
+
offsets: number[];
|
|
58
|
+
results: { bytes: Uint8Array; length: number };
|
|
59
|
+
// Each written node's successor count, then its successors, from the node's edge offset.
|
|
60
|
+
edgeOffsets: number[];
|
|
61
|
+
edges: { ids: Int32Array; length: number };
|
|
62
|
+
initial: number[];
|
|
63
|
+
nodeIds: NodeTable;
|
|
64
|
+
isDone: boolean[];
|
|
65
|
+
errorNode: number | undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const isSameState = (left: Value[], right: Value[]) =>
|
|
69
|
+
left.every((value, position) => isSameValue(value, right[position]));
|
|
70
|
+
|
|
71
|
+
const removeRepeats = (path: number[]) =>
|
|
72
|
+
path.filter((node, position) => position === 0 || path[position - 1] !== node);
|
|
73
|
+
|
|
74
|
+
// A node owns one entry in each of its check's per-node arrays.
|
|
75
|
+
const pushNode = (check: Check, state: number, tableauNode: number) => {
|
|
76
|
+
check.nodeStates.push(state);
|
|
77
|
+
check.nodeTableaux.push(tableauNode);
|
|
78
|
+
check.pending.push(undefined);
|
|
79
|
+
check.offsets.push(-1);
|
|
80
|
+
check.edgeOffsets.push(-1);
|
|
81
|
+
return check.nodeStates.length - 1;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// A Map holds at most 2^24 entries, so each tableau node of a state finds its node in an
|
|
85
|
+
// open-addressing table of (state plus one, tableau node, node) triples.
|
|
86
|
+
interface NodeTable {
|
|
87
|
+
slots: Int32Array;
|
|
88
|
+
size: number;
|
|
89
|
+
shift: number;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const getNodeSlot = (table: NodeTable, state: number, tableauNode: number) => {
|
|
93
|
+
const mask = table.slots.length / 3 - 1;
|
|
94
|
+
const hash = Math.imul(Math.imul(state, 0x9e3779b1) ^ tableauNode, 0x85ebca6b) >>> table.shift;
|
|
95
|
+
for (let index = hash; ; index = (index + 1) & mask) {
|
|
96
|
+
const slot = index * 3;
|
|
97
|
+
const stored = table.slots[slot];
|
|
98
|
+
if (stored === 0 || (stored === state + 1 && table.slots[slot + 1] === tableauNode))
|
|
99
|
+
return slot;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const isWritten = (check: Check, node: number) => check.offsets[node] !== -1;
|
|
104
|
+
|
|
105
|
+
const getPending = (check: Check, node: number) => (check.pending[node] ??= []);
|
|
106
|
+
|
|
107
|
+
const getSuccessors = (check: Check, node: number) => {
|
|
108
|
+
const offset = check.edgeOffsets[node];
|
|
109
|
+
return check.edges.ids.subarray(offset + 1, offset + 1 + check.edges.ids[offset]);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const getStateResult = (check: Check, node: number, position: number) =>
|
|
113
|
+
check.results.bytes[check.offsets[node] + position] === 1;
|
|
114
|
+
|
|
115
|
+
const SUCCESSOR_CAPACITY = 16;
|
|
116
|
+
|
|
117
|
+
const SCC_MARKER = -42;
|
|
118
|
+
|
|
119
|
+
const UNVISITED = -1;
|
|
120
|
+
|
|
121
|
+
const MAXIMUM_LINK = Number.MAX_SAFE_INTEGER;
|
|
122
|
+
|
|
123
|
+
export const createLivenessChecker = (
|
|
124
|
+
properties: TemporalProperty[],
|
|
125
|
+
temporals: ScopedExpression[],
|
|
126
|
+
environment: Environment,
|
|
127
|
+
graph: LiveGraph,
|
|
128
|
+
): LivenessChecker | undefined => {
|
|
129
|
+
if (properties.length === 0) return undefined;
|
|
130
|
+
const fairness = temporals.map((item) =>
|
|
131
|
+
toLiveExpression(item.expression, item.scope, environment),
|
|
132
|
+
);
|
|
133
|
+
const negations = properties.map((property) =>
|
|
134
|
+
createUnary("Not", toLiveExpression(property.expression, property.scope, environment)),
|
|
135
|
+
);
|
|
136
|
+
const violation = negations.length === 1 ? negations[0] : createJunction("Or", negations);
|
|
137
|
+
const formula =
|
|
138
|
+
fairness.length === 0 ? violation : createJunction("And", [...fairness, violation]);
|
|
139
|
+
const solutions = getSolutions(formula);
|
|
140
|
+
if (solutions.length === 0) {
|
|
141
|
+
throw setExitCode(
|
|
142
|
+
new TlaEvaluationError(TAUTOLOGY_MESSAGE),
|
|
143
|
+
EXIT_STATUS.livenessEvaluationFailure,
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
const actionIds = new Map<string, number>();
|
|
147
|
+
const checks = solutions.map((solution): Check => ({
|
|
148
|
+
solution,
|
|
149
|
+
actionIds: Int32Array.from(solution.actionChecks, (predicate) => {
|
|
150
|
+
const key = getTermKey(predicate);
|
|
151
|
+
const id = actionIds.get(key) ?? actionIds.size;
|
|
152
|
+
actionIds.set(key, id);
|
|
153
|
+
return id;
|
|
154
|
+
}),
|
|
155
|
+
nodeStates: [],
|
|
156
|
+
nodeTableaux: [],
|
|
157
|
+
pending: [],
|
|
158
|
+
offsets: [],
|
|
159
|
+
results: { bytes: new Uint8Array(1024), length: 0 },
|
|
160
|
+
edgeOffsets: [],
|
|
161
|
+
edges: { ids: new Int32Array(1024), length: 0 },
|
|
162
|
+
initial: [],
|
|
163
|
+
nodeIds: { slots: new Int32Array(3 << 10), size: 0, shift: 22 },
|
|
164
|
+
isDone: [],
|
|
165
|
+
errorNode: undefined,
|
|
166
|
+
}));
|
|
167
|
+
const capacity = { multiplier: 1 };
|
|
168
|
+
|
|
169
|
+
// TLC evaluates every check on its own. Without side effects the order can't show, so the
|
|
170
|
+
// checks of one state share their results.
|
|
171
|
+
const isShared = !hasRepeatedEffects(environment);
|
|
172
|
+
const shared = {
|
|
173
|
+
states: new Map<Value[], Map<LiveExpression, boolean>>(),
|
|
174
|
+
actions: new Map<Value[], Map<Value[], Int8Array>>(),
|
|
175
|
+
};
|
|
176
|
+
const getStateCache = (state: Value[]) => {
|
|
177
|
+
const cache = shared.states.get(state) ?? new Map<LiveExpression, boolean>();
|
|
178
|
+
shared.states.set(state, cache);
|
|
179
|
+
return cache;
|
|
180
|
+
};
|
|
181
|
+
// TLCGet("level") reads the level of the state whose successors the checks run on.
|
|
182
|
+
const evaluateFrom = <Result>(source: number | undefined, evaluate: () => Result) => {
|
|
183
|
+
environment.runtime.setLivenessSource(source);
|
|
184
|
+
const result = evaluate();
|
|
185
|
+
environment.runtime.setLivenessSource(undefined);
|
|
186
|
+
return result;
|
|
187
|
+
};
|
|
188
|
+
const evaluateState = (predicate: LiveExpression, state: Value[]) =>
|
|
189
|
+
evaluateLive(
|
|
190
|
+
predicate,
|
|
191
|
+
state,
|
|
192
|
+
undefined,
|
|
193
|
+
environment,
|
|
194
|
+
isShared ? getStateCache(state) : new Map(),
|
|
195
|
+
);
|
|
196
|
+
const checkState = (check: Check, source: number, state: Value[]) =>
|
|
197
|
+
evaluateFrom(source, () =>
|
|
198
|
+
Uint8Array.from(check.solution.stateChecks, (predicate) =>
|
|
199
|
+
evaluateState(predicate, state) ? 1 : 0,
|
|
200
|
+
),
|
|
201
|
+
);
|
|
202
|
+
const checkAction = (check: Check, source: number, state: Value[], successor: Value[]) =>
|
|
203
|
+
evaluateFrom(source, () => {
|
|
204
|
+
const predicates = check.solution.actionChecks;
|
|
205
|
+
if (!isShared) {
|
|
206
|
+
return Uint8Array.from(predicates, (predicate) =>
|
|
207
|
+
evaluateLive(predicate, state, successor, environment, new Map()) ? 1 : 0,
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
const stateCache = getStateCache(state);
|
|
211
|
+
const results = shared.actions.get(state) ?? new Map<Value[], Int8Array>();
|
|
212
|
+
shared.actions.set(state, results);
|
|
213
|
+
const known = results.get(successor) ?? new Int8Array(actionIds.size).fill(-1);
|
|
214
|
+
results.set(successor, known);
|
|
215
|
+
return Uint8Array.from(check.actionIds, (id, position) => {
|
|
216
|
+
if (known[id] === -1) {
|
|
217
|
+
known[id] = evaluateLive(predicates[position], state, successor, environment, stateCache)
|
|
218
|
+
? 1
|
|
219
|
+
: 0;
|
|
220
|
+
}
|
|
221
|
+
return known[id];
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
const isConsistent = (
|
|
225
|
+
tableau: Tableau,
|
|
226
|
+
node: number,
|
|
227
|
+
source: number | undefined,
|
|
228
|
+
state: Value[],
|
|
229
|
+
) =>
|
|
230
|
+
evaluateFrom(source, () =>
|
|
231
|
+
tableau.nodes[node].statePredicates.every((predicate) => evaluateState(predicate, state)),
|
|
232
|
+
);
|
|
233
|
+
const isAccepting = (tableau: Tableau, node: number) => {
|
|
234
|
+
const tableauNode = tableau.nodes[node];
|
|
235
|
+
return (
|
|
236
|
+
tableauNode.particle.length === 0 &&
|
|
237
|
+
tableauNode.successors.length === 1 &&
|
|
238
|
+
tableauNode.successors[0] === node
|
|
239
|
+
);
|
|
240
|
+
};
|
|
241
|
+
const hasEmptyModel = (check: Check) =>
|
|
242
|
+
check.solution.models.some(
|
|
243
|
+
(model) =>
|
|
244
|
+
model.eventuallyAlways.length === 0 &&
|
|
245
|
+
model.infinitelyOftenStates.length === 0 &&
|
|
246
|
+
model.infinitelyOftenActions.length === 0,
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
const getNodeId = (check: Check, state: number, tableauNode: number) => {
|
|
250
|
+
if (!check.solution.tableau) return state;
|
|
251
|
+
const slots = check.nodeIds.slots;
|
|
252
|
+
const slot = getNodeSlot(check.nodeIds, state, tableauNode);
|
|
253
|
+
return slots[slot] === 0 ? undefined : slots[slot + 2];
|
|
254
|
+
};
|
|
255
|
+
const recordNode = (check: Check, state: number, tableauNode: number) => {
|
|
256
|
+
const node = pushNode(check, state, tableauNode);
|
|
257
|
+
const table = check.nodeIds;
|
|
258
|
+
if (++table.size * 2 > table.slots.length / 3) {
|
|
259
|
+
const old = table.slots;
|
|
260
|
+
table.slots = new Int32Array(old.length * 2);
|
|
261
|
+
table.shift--;
|
|
262
|
+
for (let slot = 0; slot < old.length; slot += 3) {
|
|
263
|
+
if (old[slot] !== 0)
|
|
264
|
+
table.slots.set(
|
|
265
|
+
old.subarray(slot, slot + 3),
|
|
266
|
+
getNodeSlot(table, old[slot] - 1, old[slot + 1]),
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
table.slots.set([state + 1, tableauNode, node], getNodeSlot(table, state, tableauNode));
|
|
271
|
+
return node;
|
|
272
|
+
};
|
|
273
|
+
// A state's nodes in the order they were recorded, which is the order of their ids.
|
|
274
|
+
const getStateNodes = (check: Check, tableau: Tableau, state: number) =>
|
|
275
|
+
tableau.nodes
|
|
276
|
+
.flatMap((_, tableauNode) => getNodeId(check, state, tableauNode) ?? [])
|
|
277
|
+
.sort((left, right) => left - right);
|
|
278
|
+
const writeNode = (
|
|
279
|
+
check: Check,
|
|
280
|
+
node: number,
|
|
281
|
+
actions: Uint8Array[],
|
|
282
|
+
stateResults: Uint8Array,
|
|
283
|
+
) => {
|
|
284
|
+
const actionCount = check.solution.actionChecks.length;
|
|
285
|
+
const results = check.results;
|
|
286
|
+
const offset = results.length;
|
|
287
|
+
results.length += stateResults.length + actions.length * actionCount;
|
|
288
|
+
if (results.length > results.bytes.length) {
|
|
289
|
+
const grown = new Uint8Array(Math.max(2 * results.bytes.length, results.length));
|
|
290
|
+
grown.set(results.bytes);
|
|
291
|
+
results.bytes = grown;
|
|
292
|
+
}
|
|
293
|
+
results.bytes.set(stateResults, offset);
|
|
294
|
+
actions.forEach((edge, position) =>
|
|
295
|
+
results.bytes.set(edge, offset + stateResults.length + position * actionCount),
|
|
296
|
+
);
|
|
297
|
+
check.offsets[node] = offset;
|
|
298
|
+
const successors = check.pending[node] ?? [];
|
|
299
|
+
const edges = check.edges;
|
|
300
|
+
const edgeOffset = edges.length;
|
|
301
|
+
edges.length += successors.length + 1;
|
|
302
|
+
if (edges.length > edges.ids.length) {
|
|
303
|
+
const grown = new Int32Array(Math.max(2 * edges.ids.length, edges.length));
|
|
304
|
+
grown.set(edges.ids);
|
|
305
|
+
edges.ids = grown;
|
|
306
|
+
}
|
|
307
|
+
edges.ids[edgeOffset] = successors.length;
|
|
308
|
+
edges.ids.set(successors, edgeOffset + 1);
|
|
309
|
+
check.edgeOffsets[node] = edgeOffset;
|
|
310
|
+
check.pending[node] = undefined;
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
// TLC collects a state's successors and the state itself in a SetOfStates, a hash set it
|
|
314
|
+
// iterates in slot order. Its capacity starts at 16 times a factor that grows with every set
|
|
315
|
+
// that outgrows it.
|
|
316
|
+
const getSuccessorSet = (index: number, state: Value[], successors: LiveSuccessor[]) => {
|
|
317
|
+
const table = {
|
|
318
|
+
slots: Array.from<LiveSuccessor | undefined>({
|
|
319
|
+
length: SUCCESSOR_CAPACITY * capacity.multiplier,
|
|
320
|
+
}),
|
|
321
|
+
count: 0,
|
|
322
|
+
};
|
|
323
|
+
const insert = (slots: (LiveSuccessor | undefined)[], successor: LiveSuccessor) => {
|
|
324
|
+
for (
|
|
325
|
+
let slot = (graph.getFingerprint(successor.index) & 0x7fffffff) % slots.length;
|
|
326
|
+
;
|
|
327
|
+
slot = (slot + 1) % slots.length
|
|
328
|
+
) {
|
|
329
|
+
const entry = slots[slot];
|
|
330
|
+
if (entry === undefined) {
|
|
331
|
+
slots[slot] = successor;
|
|
332
|
+
return true;
|
|
333
|
+
}
|
|
334
|
+
if (entry.index === successor.index && isSameState(entry.state, successor.state))
|
|
335
|
+
return false;
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
const put = (successor: LiveSuccessor) => {
|
|
339
|
+
if (table.count >= table.slots.length >> 1) {
|
|
340
|
+
const old = table.slots;
|
|
341
|
+
table.slots = Array.from<LiveSuccessor | undefined>({ length: 2 * old.length + 1 });
|
|
342
|
+
for (const entry of old) if (entry) insert(table.slots, entry);
|
|
343
|
+
}
|
|
344
|
+
if (insert(table.slots, successor)) table.count++;
|
|
345
|
+
};
|
|
346
|
+
successors.forEach(put);
|
|
347
|
+
put({ index, state });
|
|
348
|
+
if (table.slots.length > SUCCESSOR_CAPACITY * capacity.multiplier) capacity.multiplier++;
|
|
349
|
+
return table.slots.filter((entry): entry is LiveSuccessor => entry !== undefined);
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
// TLC's TableauLiveChecker#addNextState for a node it finds after it expanded the node's state.
|
|
353
|
+
const expandLate = (
|
|
354
|
+
check: Check,
|
|
355
|
+
tableau: Tableau,
|
|
356
|
+
state: Value[],
|
|
357
|
+
index: number,
|
|
358
|
+
node: number,
|
|
359
|
+
) => {
|
|
360
|
+
const stateResults = checkState(check, index, state);
|
|
361
|
+
const tableauNode = tableau.nodes[check.nodeTableaux[node]];
|
|
362
|
+
const actions: Uint8Array[] = [];
|
|
363
|
+
const successors = getPending(check, node);
|
|
364
|
+
const selfActions =
|
|
365
|
+
tableauNode.successors.length > 0
|
|
366
|
+
? checkAction(check, index, state, state)
|
|
367
|
+
: new Uint8Array(0);
|
|
368
|
+
for (const next of tableauNode.successors) {
|
|
369
|
+
if (!isConsistent(tableau, next, index, state)) continue;
|
|
370
|
+
if (isAccepting(tableau, next) && check.errorNode === undefined && hasEmptyModel(check)) {
|
|
371
|
+
check.errorNode = node;
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
const existing = getNodeId(check, index, next);
|
|
375
|
+
if (existing !== undefined && successors.includes(existing)) continue;
|
|
376
|
+
const target = existing ?? recordNode(check, index, next);
|
|
377
|
+
successors.push(target);
|
|
378
|
+
actions.push(selfActions);
|
|
379
|
+
if (existing === undefined) expandLate(check, tableau, state, index, target);
|
|
380
|
+
}
|
|
381
|
+
for (const successor of graph.getSuccessors(state)) {
|
|
382
|
+
const successorActions = checkAction(check, index, state, successor.state);
|
|
383
|
+
const isDone = check.isDone[successor.index] === true;
|
|
384
|
+
for (const next of tableauNode.successors) {
|
|
385
|
+
if (!isConsistent(tableau, next, index, successor.state)) continue;
|
|
386
|
+
const existing = getNodeId(check, successor.index, next);
|
|
387
|
+
if (existing !== undefined && successors.includes(existing)) continue;
|
|
388
|
+
const target = existing ?? recordNode(check, successor.index, next);
|
|
389
|
+
successors.push(target);
|
|
390
|
+
actions.push(successorActions);
|
|
391
|
+
if (existing === undefined && isDone) {
|
|
392
|
+
expandLate(check, tableau, successor.state, successor.index, target);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
if (successors.length > 0) writeNode(check, node, actions, stateResults);
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
const addTableauState = (
|
|
400
|
+
check: Check,
|
|
401
|
+
tableau: Tableau,
|
|
402
|
+
set: LiveSuccessor[],
|
|
403
|
+
actionResults: Uint8Array[],
|
|
404
|
+
stateResults: Uint8Array,
|
|
405
|
+
index: number,
|
|
406
|
+
) => {
|
|
407
|
+
const consistency = tableau.nodes.map((_, node) =>
|
|
408
|
+
set.map((successor) => isConsistent(tableau, node, index, successor.state)),
|
|
409
|
+
);
|
|
410
|
+
check.isDone[index] = true;
|
|
411
|
+
// TLC expands the nodes recorded before the loop, not the ones the loop records.
|
|
412
|
+
for (const node of getStateNodes(check, tableau, index)) {
|
|
413
|
+
const successors = getPending(check, node);
|
|
414
|
+
const actions: Uint8Array[] = [];
|
|
415
|
+
set.forEach((successor, position) => {
|
|
416
|
+
const isDone = check.isDone[successor.index] === true;
|
|
417
|
+
for (const next of tableau.nodes[check.nodeTableaux[node]].successors) {
|
|
418
|
+
if (!consistency[next][position]) continue;
|
|
419
|
+
const existing = getNodeId(check, successor.index, next);
|
|
420
|
+
if (existing !== undefined && successors.includes(existing)) continue;
|
|
421
|
+
const target = existing ?? recordNode(check, successor.index, next);
|
|
422
|
+
successors.push(target);
|
|
423
|
+
actions.push(actionResults[position]);
|
|
424
|
+
if (existing === undefined && isDone) {
|
|
425
|
+
expandLate(check, tableau, successor.state, successor.index, target);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
});
|
|
429
|
+
writeNode(check, node, actions, stateResults);
|
|
430
|
+
}
|
|
431
|
+
if (check.errorNode === undefined) return undefined;
|
|
432
|
+
return removeRepeats(
|
|
433
|
+
getPath(check, check.errorNode)
|
|
434
|
+
.reverse()
|
|
435
|
+
.map((node) => check.nodeStates[node]),
|
|
436
|
+
);
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
const addPlainState = (
|
|
440
|
+
check: Check,
|
|
441
|
+
set: LiveSuccessor[],
|
|
442
|
+
actionResults: Uint8Array[],
|
|
443
|
+
stateResults: Uint8Array,
|
|
444
|
+
index: number,
|
|
445
|
+
) => {
|
|
446
|
+
while (check.nodeStates.length <= index) pushNode(check, check.nodeStates.length, -1);
|
|
447
|
+
const successors = getPending(check, index);
|
|
448
|
+
const actions: Uint8Array[] = [];
|
|
449
|
+
set.forEach((successor, position) => {
|
|
450
|
+
if (isWritten(check, successor.index) && successors.includes(successor.index)) return;
|
|
451
|
+
successors.push(successor.index);
|
|
452
|
+
actions.push(actionResults[position]);
|
|
453
|
+
});
|
|
454
|
+
writeNode(check, index, actions, stateResults);
|
|
455
|
+
return undefined;
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
// The shortest path from an initial node to the node, last node first, as TLC's getPath finds it.
|
|
459
|
+
const getPath = (check: Check, target: number) => {
|
|
460
|
+
if (check.initial.includes(target)) return [target];
|
|
461
|
+
const parents = new Map<number, number>();
|
|
462
|
+
const queue: number[] = [];
|
|
463
|
+
for (const node of check.initial) {
|
|
464
|
+
if (!isWritten(check, node)) continue;
|
|
465
|
+
queue.push(node);
|
|
466
|
+
parents.set(node, -1);
|
|
467
|
+
}
|
|
468
|
+
for (let position = 0; position < queue.length; position++) {
|
|
469
|
+
const current = queue[position];
|
|
470
|
+
for (const next of getSuccessors(check, current)) {
|
|
471
|
+
if (next === current && check.solution.tableau) continue;
|
|
472
|
+
if (next === target) {
|
|
473
|
+
const path = [target];
|
|
474
|
+
for (let node = current; node !== -1; node = parents.get(node) ?? -1) path.push(node);
|
|
475
|
+
return path;
|
|
476
|
+
}
|
|
477
|
+
if (!isWritten(check, next) || parents.has(next)) continue;
|
|
478
|
+
parents.set(next, current);
|
|
479
|
+
queue.push(next);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
throw new TlaEvaluationError(`Couldn't re-create liveness trace (path) starting at: ${target}`);
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
// TLC's LiveWorker#checkSccs, a Tarjan search that pushes every successor of a node at once.
|
|
486
|
+
const findInModel = (check: Check, model: Solution["models"][number]): Lasso | undefined => {
|
|
487
|
+
const solution = check.solution;
|
|
488
|
+
const actionCount = solution.actionChecks.length;
|
|
489
|
+
const stateCount = solution.stateChecks.length;
|
|
490
|
+
const getAction = (node: number, edge: number, position: number) =>
|
|
491
|
+
check.results.bytes[check.offsets[node] + stateCount + edge * actionCount + position] === 1;
|
|
492
|
+
const isAllowed = (node: number, edge: number) =>
|
|
493
|
+
model.eventuallyAlways.every((position) => getAction(node, edge, position));
|
|
494
|
+
const links = new Float64Array(check.nodeStates.length).fill(UNVISITED);
|
|
495
|
+
const queue = check.initial.filter((node) => isWritten(check, node));
|
|
496
|
+
const components: number[] = [];
|
|
497
|
+
|
|
498
|
+
const isStuttering = (node: number) => {
|
|
499
|
+
const edge = getSuccessors(check, node).indexOf(node);
|
|
500
|
+
return edge !== -1 && isAllowed(node, edge);
|
|
501
|
+
};
|
|
502
|
+
const isFulfilled = (node: number, promise: Solution["promises"][number]) =>
|
|
503
|
+
solution.tableau !== undefined &&
|
|
504
|
+
isFulfilling(solution.tableau.nodes[check.nodeTableaux[node]].particle, promise);
|
|
505
|
+
|
|
506
|
+
const checkComponent = (root: number) => {
|
|
507
|
+
const top = components.pop() ?? root;
|
|
508
|
+
if (top === root && !isStuttering(top)) {
|
|
509
|
+
links[top] = MAXIMUM_LINK;
|
|
510
|
+
return undefined;
|
|
511
|
+
}
|
|
512
|
+
const members = new Set<number>();
|
|
513
|
+
const membersByState = new Map<number, number[]>();
|
|
514
|
+
for (let member = top; ; member = components.pop() ?? root) {
|
|
515
|
+
members.add(member);
|
|
516
|
+
const siblings = membersByState.get(check.nodeStates[member]);
|
|
517
|
+
if (siblings) siblings.push(member);
|
|
518
|
+
else membersByState.set(check.nodeStates[member], [member]);
|
|
519
|
+
links[member] = MAXIMUM_LINK;
|
|
520
|
+
if (member === root) break;
|
|
521
|
+
}
|
|
522
|
+
const isStateSatisfied = model.infinitelyOftenStates.every((position) =>
|
|
523
|
+
[...members].some((node) => getStateResult(check, node, position)),
|
|
524
|
+
);
|
|
525
|
+
const isActionSatisfied = model.infinitelyOftenActions.every((position) =>
|
|
526
|
+
[...members].some((node) =>
|
|
527
|
+
getSuccessors(check, node).some(
|
|
528
|
+
(next, edge) =>
|
|
529
|
+
members.has(next) && isAllowed(node, edge) && getAction(node, edge, position),
|
|
530
|
+
),
|
|
531
|
+
),
|
|
532
|
+
);
|
|
533
|
+
const isPromiseSatisfied = solution.promises.every((promise) =>
|
|
534
|
+
[...members].some((node) => isFulfilled(node, promise)),
|
|
535
|
+
);
|
|
536
|
+
if (!isStateSatisfied || !isActionSatisfied || !isPromiseSatisfied) return undefined;
|
|
537
|
+
return getLasso(root, members, membersByState);
|
|
538
|
+
};
|
|
539
|
+
|
|
540
|
+
// TLC's LiveWorker#printTrace: a prefix to the root, a greedy walk through the component
|
|
541
|
+
// until it satisfies every check, and a breadth-first path back to the root.
|
|
542
|
+
const getLasso = (
|
|
543
|
+
root: number,
|
|
544
|
+
members: Set<number>,
|
|
545
|
+
membersByState: Map<number, number[]>,
|
|
546
|
+
) => {
|
|
547
|
+
const prefix = removeRepeats(
|
|
548
|
+
getPath(check, root)
|
|
549
|
+
.reverse()
|
|
550
|
+
.map((node) => check.nodeStates[node]),
|
|
551
|
+
);
|
|
552
|
+
const cycleStack: number[] = [];
|
|
553
|
+
const seen = new Set<number>([root]);
|
|
554
|
+
const stateDone = model.infinitelyOftenStates.map(() => false);
|
|
555
|
+
const actionDone = model.infinitelyOftenActions.map(() => false);
|
|
556
|
+
const promiseDone = solution.promises.map(() => false);
|
|
557
|
+
const remaining = { count: stateDone.length + actionDone.length + promiseDone.length };
|
|
558
|
+
const current = { node: root };
|
|
559
|
+
const isMember = (node: number) => members.has(node);
|
|
560
|
+
const take = (next: number) => {
|
|
561
|
+
cycleStack.push(current.node);
|
|
562
|
+
current.node = next;
|
|
563
|
+
};
|
|
564
|
+
while (remaining.count > 0) {
|
|
565
|
+
const countAtStart = remaining.count;
|
|
566
|
+
while (true) {
|
|
567
|
+
model.infinitelyOftenStates.forEach((position, slot) => {
|
|
568
|
+
if (!stateDone[slot] && getStateResult(check, current.node, position)) {
|
|
569
|
+
stateDone[slot] = true;
|
|
570
|
+
remaining.count--;
|
|
571
|
+
}
|
|
572
|
+
});
|
|
573
|
+
solution.promises.forEach((promise, position) => {
|
|
574
|
+
if (!promiseDone[position] && isFulfilled(current.node, promise)) {
|
|
575
|
+
promiseDone[position] = true;
|
|
576
|
+
remaining.count--;
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
if (remaining.count <= 0) break;
|
|
580
|
+
const step = { last: -1, unvisited: -1 };
|
|
581
|
+
const countBeforeActions = remaining.count;
|
|
582
|
+
const successors = getSuccessors(check, current.node);
|
|
583
|
+
const taken = successors.some((next, edge) => {
|
|
584
|
+
if (membersByState.has(check.nodeStates[next])) {
|
|
585
|
+
if (!isMember(next) || !isAllowed(current.node, edge)) return false;
|
|
586
|
+
step.last = next;
|
|
587
|
+
model.infinitelyOftenActions.forEach((position, slot) => {
|
|
588
|
+
if (!actionDone[slot] && getAction(current.node, edge, position)) {
|
|
589
|
+
actionDone[slot] = true;
|
|
590
|
+
remaining.count--;
|
|
591
|
+
}
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
if (remaining.count < countBeforeActions) {
|
|
595
|
+
take(next);
|
|
596
|
+
seen.clear();
|
|
597
|
+
return true;
|
|
598
|
+
}
|
|
599
|
+
if (isMember(next) && !seen.has(next)) step.unvisited = next;
|
|
600
|
+
return false;
|
|
601
|
+
});
|
|
602
|
+
if (taken) break;
|
|
603
|
+
if (remaining.count < countAtStart) {
|
|
604
|
+
take(step.last);
|
|
605
|
+
seen.clear();
|
|
606
|
+
break;
|
|
607
|
+
}
|
|
608
|
+
while (step.unvisited === -1) {
|
|
609
|
+
const previous = cycleStack.pop();
|
|
610
|
+
if (previous === undefined) {
|
|
611
|
+
throw new TlaEvaluationError(
|
|
612
|
+
"TLC failed to build the counterexample of a liveness property",
|
|
613
|
+
);
|
|
614
|
+
}
|
|
615
|
+
current.node = previous;
|
|
616
|
+
step.unvisited =
|
|
617
|
+
getSuccessors(check, previous).find((next) => isMember(next) && !seen.has(next)) ??
|
|
618
|
+
-1;
|
|
619
|
+
}
|
|
620
|
+
take(step.unvisited);
|
|
621
|
+
seen.add(step.unvisited);
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
const postfix = getClosingPath(root, current.node, membersByState, isAllowed);
|
|
625
|
+
for (let position = cycleStack.length - 1; position >= 0; position--) {
|
|
626
|
+
const state = check.nodeStates[cycleStack[position]];
|
|
627
|
+
if (postfix[postfix.length - 1] !== state) postfix.push(state);
|
|
628
|
+
}
|
|
629
|
+
const cycleState = prefix[prefix.length - 1];
|
|
630
|
+
if (postfix[postfix.length - 1] === cycleState) postfix.pop();
|
|
631
|
+
return { prefix: prefix.slice(0, -1), cycle: [cycleState, ...postfix.reverse()] };
|
|
632
|
+
};
|
|
633
|
+
|
|
634
|
+
// TLC's LiveWorker#bfsPostFix, the states from the walk's end back to the root, last first.
|
|
635
|
+
const getClosingPath = (
|
|
636
|
+
root: number,
|
|
637
|
+
end: number,
|
|
638
|
+
membersByState: Map<number, number[]>,
|
|
639
|
+
isAllowedEdge: (node: number, edge: number) => boolean,
|
|
640
|
+
) => {
|
|
641
|
+
if (end === root) return [];
|
|
642
|
+
const start = check.nodeStates[end];
|
|
643
|
+
const parents = new Map<number, number>();
|
|
644
|
+
const seen = new Set<number>([start]);
|
|
645
|
+
const queue: [number, number][] = [];
|
|
646
|
+
for (let state = start, parent = -1, position = 0; ; position++) {
|
|
647
|
+
for (const node of membersByState.get(state) ?? []) {
|
|
648
|
+
for (const [edge, next] of getSuccessors(check, node).entries()) {
|
|
649
|
+
if (next === node || !isAllowedEdge(node, edge)) continue;
|
|
650
|
+
if (next === root) {
|
|
651
|
+
const path: number[] = [];
|
|
652
|
+
for (let item = state, itemParent = parent; item !== start;) {
|
|
653
|
+
path.push(item);
|
|
654
|
+
item = itemParent;
|
|
655
|
+
itemParent = parents.get(item) ?? -1;
|
|
656
|
+
}
|
|
657
|
+
path.push(start);
|
|
658
|
+
return path;
|
|
659
|
+
}
|
|
660
|
+
const nextState = check.nodeStates[next];
|
|
661
|
+
if (!membersByState.has(nextState) || seen.has(nextState)) continue;
|
|
662
|
+
seen.add(nextState);
|
|
663
|
+
queue.push([nextState, state]);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
parents.set(state, parent);
|
|
667
|
+
const item = queue[position];
|
|
668
|
+
if (item === undefined) {
|
|
669
|
+
throw new TlaEvaluationError(
|
|
670
|
+
"TLC failed to build the counterexample of a liveness property",
|
|
671
|
+
);
|
|
672
|
+
}
|
|
673
|
+
state = item[0];
|
|
674
|
+
parent = item[1];
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
|
|
678
|
+
for (let position = 0; position < queue.length; position++) {
|
|
679
|
+
const stack = [queue[position], UNVISITED, 0];
|
|
680
|
+
const counter = { link: 0 };
|
|
681
|
+
while (stack.length >= 3) {
|
|
682
|
+
const lowLink = stack.pop() ?? 0;
|
|
683
|
+
const location = stack.pop();
|
|
684
|
+
const node = stack.pop() ?? 0;
|
|
685
|
+
if (location === SCC_MARKER) {
|
|
686
|
+
if (links[node] === lowLink) {
|
|
687
|
+
const lasso = checkComponent(node);
|
|
688
|
+
if (lasso) return lasso;
|
|
689
|
+
}
|
|
690
|
+
stack.push(Math.min(stack.pop() ?? lowLink, lowLink));
|
|
691
|
+
continue;
|
|
692
|
+
}
|
|
693
|
+
if (links[node] !== UNVISITED) {
|
|
694
|
+
stack.push(Math.min(lowLink, links[node]));
|
|
695
|
+
continue;
|
|
696
|
+
}
|
|
697
|
+
links[node] = counter.link;
|
|
698
|
+
stack.push(lowLink, node, SCC_MARKER);
|
|
699
|
+
components.push(node);
|
|
700
|
+
const nextLowLink = { value: counter.link++ };
|
|
701
|
+
getSuccessors(check, node).forEach((next, edge) => {
|
|
702
|
+
if (!isWritten(check, next)) return;
|
|
703
|
+
if (!isAllowed(node, edge)) {
|
|
704
|
+
if (links[next] === UNVISITED) queue.push(next);
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
if (links[next] === UNVISITED) stack.push(next, UNVISITED);
|
|
708
|
+
else nextLowLink.value = Math.min(nextLowLink.value, links[next]);
|
|
709
|
+
});
|
|
710
|
+
stack.push(nextLowLink.value);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
return undefined;
|
|
714
|
+
};
|
|
715
|
+
|
|
716
|
+
return {
|
|
717
|
+
addInitialState: (index, state) => {
|
|
718
|
+
shared.states.clear();
|
|
719
|
+
shared.actions.clear();
|
|
720
|
+
for (const check of checks) {
|
|
721
|
+
const tableau = check.solution.tableau;
|
|
722
|
+
if (!tableau) {
|
|
723
|
+
check.initial.push(index);
|
|
724
|
+
continue;
|
|
725
|
+
}
|
|
726
|
+
for (let node = 0; node < tableau.initialCount; node++) {
|
|
727
|
+
if (!isConsistent(tableau, node, undefined, state)) continue;
|
|
728
|
+
check.initial.push(getNodeId(check, index, node) ?? recordNode(check, index, node));
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
},
|
|
732
|
+
addState: (index, state, successors) => {
|
|
733
|
+
shared.states.clear();
|
|
734
|
+
shared.actions.clear();
|
|
735
|
+
const set = getSuccessorSet(index, state, successors);
|
|
736
|
+
for (const check of checks) {
|
|
737
|
+
const actionResults = set.map((successor) =>
|
|
738
|
+
checkAction(check, index, state, successor.state),
|
|
739
|
+
);
|
|
740
|
+
const stateResults = checkState(check, index, state);
|
|
741
|
+
const tableau = check.solution.tableau;
|
|
742
|
+
const accepted = tableau
|
|
743
|
+
? addTableauState(check, tableau, set, actionResults, stateResults, index)
|
|
744
|
+
: addPlainState(check, set, actionResults, stateResults, index);
|
|
745
|
+
if (accepted) return accepted;
|
|
746
|
+
}
|
|
747
|
+
return undefined;
|
|
748
|
+
},
|
|
749
|
+
findViolation: () => {
|
|
750
|
+
for (const check of checks) {
|
|
751
|
+
for (const model of check.solution.models) {
|
|
752
|
+
const lasso = findInModel(check, model);
|
|
753
|
+
if (lasso) return lasso;
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
return undefined;
|
|
757
|
+
},
|
|
758
|
+
// TLC names every property that the counterexample violates, once each.
|
|
759
|
+
getViolatedProperties: (path, cycleStart) => [
|
|
760
|
+
...new Set(
|
|
761
|
+
properties
|
|
762
|
+
.filter(
|
|
763
|
+
(_, position) =>
|
|
764
|
+
!evaluateOnLasso(negations[position].body, path, cycleStart, 0, environment),
|
|
765
|
+
)
|
|
766
|
+
.map((property) => property.name),
|
|
767
|
+
),
|
|
768
|
+
],
|
|
769
|
+
};
|
|
770
|
+
};
|