tla-ts 0.0.3-dev.91b9cf4 → 0.0.3-dev.974cfcb
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/dist/transfer.cjs +11 -11
- package/dist/transfer.js +11 -11
- package/package.json +1 -1
- package/src/check.ts +48 -18
- package/src/evaluate.ts +1 -1
- package/src/live-check.ts +87 -57
- package/src/liveness.ts +51 -14
- package/src/successors.ts +11 -4
- package/src/tableau.ts +50 -23
- package/src/tlc-random.ts +9 -6
- package/src/tlc-summary.ts +20 -5
- package/src/values.ts +26 -4
package/package.json
CHANGED
package/src/check.ts
CHANGED
|
@@ -446,12 +446,12 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
|
|
|
446
446
|
config.properties.forEach((name) => {
|
|
447
447
|
const definition = getConfigPredicate("property", name, environment);
|
|
448
448
|
if (!definition) return;
|
|
449
|
-
expandTemporal(definition.body, undefined, environment, true).forEach((item) => {
|
|
449
|
+
expandTemporal(definition.body, undefined, environment, true, name).forEach((item) => {
|
|
450
450
|
const conjunct = item.expression;
|
|
451
451
|
if (isConstantTrue(item, environment, "property")) return;
|
|
452
452
|
if (conjunct.kind === "Always" && conjunct.expression.kind === "BoxAction") {
|
|
453
453
|
properties.actionProperties.push({
|
|
454
|
-
name,
|
|
454
|
+
name: item.name,
|
|
455
455
|
box: conjunct.expression,
|
|
456
456
|
action: conjunct.expression.action,
|
|
457
457
|
subscript: conjunct.expression.subscript,
|
|
@@ -463,12 +463,22 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
|
|
|
463
463
|
conjunct.kind === "Always" &&
|
|
464
464
|
getLevel(conjunct.expression, item.scope, environment) <= 1
|
|
465
465
|
) {
|
|
466
|
-
|
|
466
|
+
// TLC names the invariant []P after P when P is an operator name.
|
|
467
|
+
const expression = conjunct.expression;
|
|
468
|
+
properties.invariants.push({
|
|
469
|
+
name: expression.kind === "Identifier" ? expression.name : item.name,
|
|
470
|
+
expression,
|
|
471
|
+
scope: item.scope,
|
|
472
|
+
});
|
|
467
473
|
return;
|
|
468
474
|
}
|
|
469
475
|
const level = getLevel(conjunct, item.scope, environment);
|
|
470
476
|
if (level < ACTION_LEVEL) {
|
|
471
|
-
properties.initialPredicates.push({
|
|
477
|
+
properties.initialPredicates.push({
|
|
478
|
+
name: item.name,
|
|
479
|
+
expression: conjunct,
|
|
480
|
+
scope: item.scope,
|
|
481
|
+
});
|
|
472
482
|
return;
|
|
473
483
|
}
|
|
474
484
|
if (level === ACTION_LEVEL) {
|
|
@@ -777,11 +787,13 @@ export const createSearch = (
|
|
|
777
787
|
if (view !== undefined || isFingerprintedLikeTlc) {
|
|
778
788
|
return BigInt64Array.from(states, getTlcStateFingerprint);
|
|
779
789
|
}
|
|
780
|
-
const
|
|
781
|
-
const
|
|
782
|
-
|
|
783
|
-
|
|
790
|
+
const fingerprints = new BigInt64Array(fingerprintStates.length);
|
|
791
|
+
const fingerprint = createTlcFingerprinter(
|
|
792
|
+
getTlcOrder(),
|
|
793
|
+
new Int32Array(fingerprints.buffer),
|
|
784
794
|
);
|
|
795
|
+
fingerprintStates.forEach(fingerprint);
|
|
796
|
+
return fingerprints;
|
|
785
797
|
},
|
|
786
798
|
getFingerprintState: (state) => {
|
|
787
799
|
if (isFingerprintedLikeTlc) return [String(getTlcStateFingerprint(state))];
|
|
@@ -1037,7 +1049,9 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
1037
1049
|
const parents: number[] = [];
|
|
1038
1050
|
const actions: (string | undefined)[] = [];
|
|
1039
1051
|
const depths: number[] = [];
|
|
1040
|
-
|
|
1052
|
+
// A Map holds at most 2^24 entries, so an open-addressing table keeps each seen state's hash
|
|
1053
|
+
// and its index plus one side by side, probed by the hash.
|
|
1054
|
+
const seen = { slots: new Int32Array(1 << 17), size: 0, shift: 16 };
|
|
1041
1055
|
const canonicalizeState = createCanonicalizer();
|
|
1042
1056
|
|
|
1043
1057
|
const toRecord = (state: Value[]): Record<string, Value> =>
|
|
@@ -1197,19 +1211,35 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
1197
1211
|
}
|
|
1198
1212
|
return true;
|
|
1199
1213
|
};
|
|
1214
|
+
const getSlot = (hash: number) => (Math.imul(hash, 0x9e3779b1) >>> seen.shift) << 1;
|
|
1200
1215
|
const findStateIndex = (hash: number, state: Value[]) => {
|
|
1201
|
-
const
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1216
|
+
const mask = seen.slots.length - 1;
|
|
1217
|
+
for (let slot = getSlot(hash); ; slot = (slot + 2) & mask) {
|
|
1218
|
+
const entry = seen.slots[slot + 1];
|
|
1219
|
+
if (entry === 0) return -1;
|
|
1220
|
+
if (seen.slots[slot] === hash && isSameState(fingerprintStates[entry - 1], state)) {
|
|
1221
|
+
return entry - 1;
|
|
1222
|
+
}
|
|
1205
1223
|
}
|
|
1206
|
-
|
|
1224
|
+
};
|
|
1225
|
+
const insertSeen = (hash: number, entry: number) => {
|
|
1226
|
+
const mask = seen.slots.length - 1;
|
|
1227
|
+
let slot = getSlot(hash);
|
|
1228
|
+
while (seen.slots[slot + 1] !== 0) slot = (slot + 2) & mask;
|
|
1229
|
+
seen.slots[slot] = hash;
|
|
1230
|
+
seen.slots[slot + 1] = entry;
|
|
1207
1231
|
};
|
|
1208
1232
|
const markSeen = (hash: number, index: number) => {
|
|
1209
|
-
|
|
1210
|
-
if (
|
|
1211
|
-
|
|
1212
|
-
|
|
1233
|
+
seen.size++;
|
|
1234
|
+
if (seen.size * 4 > seen.slots.length) {
|
|
1235
|
+
const slots = seen.slots;
|
|
1236
|
+
seen.slots = new Int32Array(slots.length * 2);
|
|
1237
|
+
seen.shift--;
|
|
1238
|
+
for (let slot = 0; slot < slots.length; slot += 2) {
|
|
1239
|
+
if (slots[slot + 1] !== 0) insertSeen(slots[slot], slots[slot + 1]);
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
insertSeen(hash, index + 1);
|
|
1213
1243
|
};
|
|
1214
1244
|
const countDistinctState = (depth: number) => {
|
|
1215
1245
|
runtime.distinctStates = states.length + 1;
|
package/src/evaluate.ts
CHANGED
|
@@ -1027,7 +1027,7 @@ export const getEvaluator = (expression: Expression, environment: Environment):
|
|
|
1027
1027
|
|
|
1028
1028
|
const entryEvaluators = new WeakMap<Expression, Evaluator>();
|
|
1029
1029
|
|
|
1030
|
-
const getEntryEvaluator = (expression: Expression, environment: Environment) => {
|
|
1030
|
+
export const getEntryEvaluator = (expression: Expression, environment: Environment) => {
|
|
1031
1031
|
const cached = entryEvaluators.get(expression);
|
|
1032
1032
|
if (cached) return cached;
|
|
1033
1033
|
const evaluator =
|
package/src/live-check.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { TlaEvaluationError } from "./errors";
|
|
|
2
2
|
import { hasRepeatedEffects, type Environment } from "./evaluate";
|
|
3
3
|
import {
|
|
4
4
|
evaluateLive,
|
|
5
|
+
TAUTOLOGY_MESSAGE,
|
|
5
6
|
evaluateOnLasso,
|
|
6
7
|
toLiveExpression,
|
|
7
8
|
type ScopedExpression,
|
|
@@ -11,6 +12,7 @@ import {
|
|
|
11
12
|
createJunction,
|
|
12
13
|
createUnary,
|
|
13
14
|
getSolutions,
|
|
15
|
+
getTermKey,
|
|
14
16
|
isFulfilling,
|
|
15
17
|
type LiveExpression,
|
|
16
18
|
type Solution,
|
|
@@ -48,36 +50,16 @@ interface Check {
|
|
|
48
50
|
nodeStates: number[];
|
|
49
51
|
nodeTableaux: number[];
|
|
50
52
|
successors: number[][];
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
53
|
+
// Each written node's state results, then its action results edge by edge, from the node's
|
|
54
|
+
// offset in one buffer, so a node costs no objects of its own. An unwritten node has offset -1.
|
|
55
|
+
offsets: number[];
|
|
56
|
+
results: { bytes: Uint8Array; length: number };
|
|
54
57
|
initial: number[];
|
|
55
|
-
nodeIds:
|
|
56
|
-
nodesByState: Map<number, number[]>;
|
|
58
|
+
nodeIds: NodeTable;
|
|
57
59
|
isDone: boolean[];
|
|
58
60
|
errorNode: number | undefined;
|
|
59
61
|
}
|
|
60
62
|
|
|
61
|
-
const liveKeys = new WeakMap<LiveExpression, string>();
|
|
62
|
-
|
|
63
|
-
const atomCount = { value: 0 };
|
|
64
|
-
|
|
65
|
-
// Each solution builds its own junctions over shared atoms, so equal checks get equal keys.
|
|
66
|
-
const getLiveKey = (expression: LiveExpression): string => {
|
|
67
|
-
const cached = liveKeys.get(expression);
|
|
68
|
-
if (cached !== undefined) return cached;
|
|
69
|
-
const key =
|
|
70
|
-
expression.kind === "Boolean"
|
|
71
|
-
? String(expression.value)
|
|
72
|
-
: "body" in expression
|
|
73
|
-
? `${expression.kind}(${getLiveKey(expression.body)})`
|
|
74
|
-
: "operands" in expression
|
|
75
|
-
? `${expression.kind}(${expression.operands.map(getLiveKey).join(",")})`
|
|
76
|
-
: `${expression.kind}#${atomCount.value++}`;
|
|
77
|
-
liveKeys.set(expression, key);
|
|
78
|
-
return key;
|
|
79
|
-
};
|
|
80
|
-
|
|
81
63
|
const isSameState = (left: Value[], right: Value[]) =>
|
|
82
64
|
left.every((value, position) => isSameValue(value, right[position]));
|
|
83
65
|
|
|
@@ -89,12 +71,34 @@ const pushNode = (check: Check, state: number, tableauNode: number) => {
|
|
|
89
71
|
check.nodeStates.push(state);
|
|
90
72
|
check.nodeTableaux.push(tableauNode);
|
|
91
73
|
check.successors.push([]);
|
|
92
|
-
check.
|
|
93
|
-
check.stateResults.push(new Uint8Array(0));
|
|
94
|
-
check.isWritten.push(false);
|
|
74
|
+
check.offsets.push(-1);
|
|
95
75
|
return check.nodeStates.length - 1;
|
|
96
76
|
};
|
|
97
77
|
|
|
78
|
+
// A Map holds at most 2^24 entries, so each tableau node of a state finds its node in an
|
|
79
|
+
// open-addressing table of (state plus one, tableau node, node) triples.
|
|
80
|
+
interface NodeTable {
|
|
81
|
+
slots: Int32Array;
|
|
82
|
+
size: number;
|
|
83
|
+
shift: number;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const getNodeSlot = (table: NodeTable, state: number, tableauNode: number) => {
|
|
87
|
+
const mask = table.slots.length / 3 - 1;
|
|
88
|
+
const hash = Math.imul(Math.imul(state, 0x9e3779b1) ^ tableauNode, 0x85ebca6b) >>> table.shift;
|
|
89
|
+
for (let index = hash; ; index = (index + 1) & mask) {
|
|
90
|
+
const slot = index * 3;
|
|
91
|
+
const stored = table.slots[slot];
|
|
92
|
+
if (stored === 0 || (stored === state + 1 && table.slots[slot + 1] === tableauNode))
|
|
93
|
+
return slot;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const isWritten = (check: Check, node: number) => check.offsets[node] !== -1;
|
|
98
|
+
|
|
99
|
+
const getStateResult = (check: Check, node: number, position: number) =>
|
|
100
|
+
check.results.bytes[check.offsets[node] + position] === 1;
|
|
101
|
+
|
|
98
102
|
const SUCCESSOR_CAPACITY = 16;
|
|
99
103
|
|
|
100
104
|
const SCC_MARKER = -42;
|
|
@@ -121,13 +125,13 @@ export const createLivenessChecker = (
|
|
|
121
125
|
fairness.length === 0 ? violation : createJunction("And", [...fairness, violation]);
|
|
122
126
|
const solutions = getSolutions(formula);
|
|
123
127
|
if (solutions.length === 0) {
|
|
124
|
-
throw new TlaEvaluationError(
|
|
128
|
+
throw new TlaEvaluationError(TAUTOLOGY_MESSAGE);
|
|
125
129
|
}
|
|
126
130
|
const actionIds = new Map<string, number>();
|
|
127
131
|
const checks = solutions.map((solution): Check => ({
|
|
128
132
|
solution,
|
|
129
133
|
actionIds: Int32Array.from(solution.actionChecks, (predicate) => {
|
|
130
|
-
const key =
|
|
134
|
+
const key = getTermKey(predicate);
|
|
131
135
|
const id = actionIds.get(key) ?? actionIds.size;
|
|
132
136
|
actionIds.set(key, id);
|
|
133
137
|
return id;
|
|
@@ -135,12 +139,10 @@ export const createLivenessChecker = (
|
|
|
135
139
|
nodeStates: [],
|
|
136
140
|
nodeTableaux: [],
|
|
137
141
|
successors: [],
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
isWritten: [],
|
|
142
|
+
offsets: [],
|
|
143
|
+
results: { bytes: new Uint8Array(1024), length: 0 },
|
|
141
144
|
initial: [],
|
|
142
|
-
nodeIds: new
|
|
143
|
-
nodesByState: new Map(),
|
|
145
|
+
nodeIds: { slots: new Int32Array(3 << 10), size: 0, shift: 22 },
|
|
144
146
|
isDone: [],
|
|
145
147
|
errorNode: undefined,
|
|
146
148
|
}));
|
|
@@ -226,18 +228,35 @@ export const createLivenessChecker = (
|
|
|
226
228
|
model.infinitelyOftenActions.length === 0,
|
|
227
229
|
);
|
|
228
230
|
|
|
229
|
-
const getNodeId = (check: Check, state: number, tableauNode: number) =>
|
|
230
|
-
check.solution.tableau
|
|
231
|
-
|
|
232
|
-
|
|
231
|
+
const getNodeId = (check: Check, state: number, tableauNode: number) => {
|
|
232
|
+
if (!check.solution.tableau) return state;
|
|
233
|
+
const slots = check.nodeIds.slots;
|
|
234
|
+
const slot = getNodeSlot(check.nodeIds, state, tableauNode);
|
|
235
|
+
return slots[slot] === 0 ? undefined : slots[slot + 2];
|
|
236
|
+
};
|
|
233
237
|
const recordNode = (check: Check, state: number, tableauNode: number) => {
|
|
234
238
|
const node = pushNode(check, state, tableauNode);
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
+
const table = check.nodeIds;
|
|
240
|
+
if (++table.size * 2 > table.slots.length / 3) {
|
|
241
|
+
const old = table.slots;
|
|
242
|
+
table.slots = new Int32Array(old.length * 2);
|
|
243
|
+
table.shift--;
|
|
244
|
+
for (let slot = 0; slot < old.length; slot += 3) {
|
|
245
|
+
if (old[slot] !== 0)
|
|
246
|
+
table.slots.set(
|
|
247
|
+
old.subarray(slot, slot + 3),
|
|
248
|
+
getNodeSlot(table, old[slot] - 1, old[slot + 1]),
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
table.slots.set([state + 1, tableauNode, node], getNodeSlot(table, state, tableauNode));
|
|
239
253
|
return node;
|
|
240
254
|
};
|
|
255
|
+
// A state's nodes in the order they were recorded, which is the order of their ids.
|
|
256
|
+
const getStateNodes = (check: Check, tableau: Tableau, state: number) =>
|
|
257
|
+
tableau.nodes
|
|
258
|
+
.flatMap((_, tableauNode) => getNodeId(check, state, tableauNode) ?? [])
|
|
259
|
+
.sort((left, right) => left - right);
|
|
241
260
|
const writeNode = (
|
|
242
261
|
check: Check,
|
|
243
262
|
node: number,
|
|
@@ -245,11 +264,21 @@ export const createLivenessChecker = (
|
|
|
245
264
|
stateResults: Uint8Array,
|
|
246
265
|
) => {
|
|
247
266
|
const actionCount = check.solution.actionChecks.length;
|
|
248
|
-
const
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
267
|
+
const results = check.results;
|
|
268
|
+
const offset = results.length;
|
|
269
|
+
results.length += stateResults.length + actions.length * actionCount;
|
|
270
|
+
if (results.length > results.bytes.length) {
|
|
271
|
+
const grown = new Uint8Array(Math.max(2 * results.bytes.length, results.length));
|
|
272
|
+
grown.set(results.bytes);
|
|
273
|
+
results.bytes = grown;
|
|
274
|
+
}
|
|
275
|
+
results.bytes.set(stateResults, offset);
|
|
276
|
+
actions.forEach((edge, position) =>
|
|
277
|
+
results.bytes.set(edge, offset + stateResults.length + position * actionCount),
|
|
278
|
+
);
|
|
279
|
+
check.offsets[node] = offset;
|
|
280
|
+
// A node gets no successors once written, so it keeps them without spare capacity.
|
|
281
|
+
check.successors[node] = check.successors[node].slice();
|
|
253
282
|
};
|
|
254
283
|
|
|
255
284
|
// TLC collects a state's successors and the state itself in a SetOfStates, a hash set it
|
|
@@ -351,7 +380,7 @@ export const createLivenessChecker = (
|
|
|
351
380
|
);
|
|
352
381
|
check.isDone[index] = true;
|
|
353
382
|
// TLC expands the nodes recorded before the loop, not the ones the loop records.
|
|
354
|
-
for (const node of (check
|
|
383
|
+
for (const node of getStateNodes(check, tableau, index)) {
|
|
355
384
|
const successors = check.successors[node];
|
|
356
385
|
const actions: Uint8Array[] = [];
|
|
357
386
|
set.forEach((successor, position) => {
|
|
@@ -389,7 +418,7 @@ export const createLivenessChecker = (
|
|
|
389
418
|
const successors = check.successors[index];
|
|
390
419
|
const actions: Uint8Array[] = [];
|
|
391
420
|
set.forEach((successor, position) => {
|
|
392
|
-
if (check
|
|
421
|
+
if (isWritten(check, successor.index) && successors.includes(successor.index)) return;
|
|
393
422
|
successors.push(successor.index);
|
|
394
423
|
actions.push(actionResults[position]);
|
|
395
424
|
});
|
|
@@ -403,7 +432,7 @@ export const createLivenessChecker = (
|
|
|
403
432
|
const parents = new Map<number, number>();
|
|
404
433
|
const queue: number[] = [];
|
|
405
434
|
for (const node of check.initial) {
|
|
406
|
-
if (!check
|
|
435
|
+
if (!isWritten(check, node)) continue;
|
|
407
436
|
queue.push(node);
|
|
408
437
|
parents.set(node, -1);
|
|
409
438
|
}
|
|
@@ -416,7 +445,7 @@ export const createLivenessChecker = (
|
|
|
416
445
|
for (let node = current; node !== -1; node = parents.get(node) ?? -1) path.push(node);
|
|
417
446
|
return path;
|
|
418
447
|
}
|
|
419
|
-
if (!check
|
|
448
|
+
if (!isWritten(check, next) || parents.has(next)) continue;
|
|
420
449
|
parents.set(next, current);
|
|
421
450
|
queue.push(next);
|
|
422
451
|
}
|
|
@@ -428,12 +457,13 @@ export const createLivenessChecker = (
|
|
|
428
457
|
const findInModel = (check: Check, model: Solution["models"][number]): Lasso | undefined => {
|
|
429
458
|
const solution = check.solution;
|
|
430
459
|
const actionCount = solution.actionChecks.length;
|
|
460
|
+
const stateCount = solution.stateChecks.length;
|
|
431
461
|
const getAction = (node: number, edge: number, position: number) =>
|
|
432
|
-
check.
|
|
462
|
+
check.results.bytes[check.offsets[node] + stateCount + edge * actionCount + position] === 1;
|
|
433
463
|
const isAllowed = (node: number, edge: number) =>
|
|
434
464
|
model.eventuallyAlways.every((position) => getAction(node, edge, position));
|
|
435
465
|
const links = new Float64Array(check.nodeStates.length).fill(UNVISITED);
|
|
436
|
-
const queue = check.initial.filter((node) => check
|
|
466
|
+
const queue = check.initial.filter((node) => isWritten(check, node));
|
|
437
467
|
const components: number[] = [];
|
|
438
468
|
|
|
439
469
|
const isStuttering = (node: number) => {
|
|
@@ -461,7 +491,7 @@ export const createLivenessChecker = (
|
|
|
461
491
|
if (member === root) break;
|
|
462
492
|
}
|
|
463
493
|
const isStateSatisfied = model.infinitelyOftenStates.every((position) =>
|
|
464
|
-
[...members].some((node) => check
|
|
494
|
+
[...members].some((node) => getStateResult(check, node, position)),
|
|
465
495
|
);
|
|
466
496
|
const isActionSatisfied = model.infinitelyOftenActions.every((position) =>
|
|
467
497
|
[...members].some((node) =>
|
|
@@ -506,7 +536,7 @@ export const createLivenessChecker = (
|
|
|
506
536
|
const countAtStart = remaining.count;
|
|
507
537
|
while (true) {
|
|
508
538
|
model.infinitelyOftenStates.forEach((position, slot) => {
|
|
509
|
-
if (!stateDone[slot] && check
|
|
539
|
+
if (!stateDone[slot] && getStateResult(check, current.node, position)) {
|
|
510
540
|
stateDone[slot] = true;
|
|
511
541
|
remaining.count--;
|
|
512
542
|
}
|
|
@@ -639,7 +669,7 @@ export const createLivenessChecker = (
|
|
|
639
669
|
components.push(node);
|
|
640
670
|
const nextLowLink = { value: counter.link++ };
|
|
641
671
|
check.successors[node].forEach((next, edge) => {
|
|
642
|
-
if (!check
|
|
672
|
+
if (!isWritten(check, next)) return;
|
|
643
673
|
if (!isAllowed(node, edge)) {
|
|
644
674
|
if (links[next] === UNVISITED) queue.push(next);
|
|
645
675
|
return;
|
package/src/liveness.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { isLocallyResolved, type Definition, type Expression } from "./ast";
|
|
2
2
|
import { TlaConfigError, TlaEvaluationError } from "./errors";
|
|
3
|
+
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
3
4
|
import {
|
|
4
5
|
describeLocation,
|
|
6
|
+
describeRange,
|
|
5
7
|
bindDefinitions,
|
|
6
8
|
bindOperatorArguments,
|
|
7
9
|
evaluate,
|
|
@@ -36,6 +38,10 @@ export interface ScopedExpression {
|
|
|
36
38
|
scope: Scope | undefined;
|
|
37
39
|
}
|
|
38
40
|
|
|
41
|
+
export interface NamedExpression extends ScopedExpression {
|
|
42
|
+
name: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
39
45
|
export interface TemporalProperty extends ScopedExpression {
|
|
40
46
|
name: string;
|
|
41
47
|
}
|
|
@@ -144,21 +150,27 @@ const getDefinitionLevel = (definition: Definition, environment: Environment) =>
|
|
|
144
150
|
return level;
|
|
145
151
|
};
|
|
146
152
|
|
|
153
|
+
export const TAUTOLOGY_MESSAGE = "Temporal formula is a tautology (its negation is unsatisfiable).";
|
|
154
|
+
|
|
155
|
+
// TLC names each part of a property after the operator it expands, or after the location of the
|
|
156
|
+
// conjunct or quantifier body it splits off.
|
|
147
157
|
export const expandTemporal = (
|
|
148
158
|
expression: Expression,
|
|
149
159
|
scope: Scope | undefined,
|
|
150
160
|
environment: Environment,
|
|
151
|
-
|
|
161
|
+
isProperty: boolean,
|
|
162
|
+
name = "",
|
|
152
163
|
expanding = new Set<Definition>(),
|
|
153
|
-
):
|
|
164
|
+
): NamedExpression[] => {
|
|
154
165
|
const expand = (
|
|
155
166
|
child: Expression,
|
|
156
167
|
childScope: Scope | undefined,
|
|
168
|
+
childName = name,
|
|
157
169
|
childExpanding = expanding,
|
|
158
|
-
):
|
|
159
|
-
expandTemporal(child, childScope, environment,
|
|
170
|
+
): NamedExpression[] =>
|
|
171
|
+
expandTemporal(child, childScope, environment, isProperty, childName, childExpanding);
|
|
160
172
|
if (expression.kind === "And") {
|
|
161
|
-
return expression.operands.flatMap((operand) => expand(operand, scope));
|
|
173
|
+
return expression.operands.flatMap((operand) => expand(operand, scope, describeRange(operand)));
|
|
162
174
|
}
|
|
163
175
|
if (expression.kind === "Label") return expand(expression.body, scope);
|
|
164
176
|
if (expression.kind === "Let") {
|
|
@@ -181,33 +193,58 @@ export const expandTemporal = (
|
|
|
181
193
|
if (expanding.has(definition)) {
|
|
182
194
|
throw new TlaConfigError(`The definition of ${expression.name} refers to itself`);
|
|
183
195
|
}
|
|
184
|
-
return expand(
|
|
196
|
+
return expand(
|
|
197
|
+
definition.body,
|
|
198
|
+
undefined,
|
|
199
|
+
expression.name,
|
|
200
|
+
new Set([...expanding, definition]),
|
|
201
|
+
);
|
|
185
202
|
}
|
|
186
203
|
}
|
|
187
204
|
const context = getConstantContext(environment, scope);
|
|
188
|
-
|
|
205
|
+
// A property expands every operator it applies and unrolls every \A over constant sets, but a
|
|
206
|
+
// specification only expands the temporal ones.
|
|
207
|
+
const isExpanded = (child: Expression) =>
|
|
208
|
+
isProperty || getLevel(child, scope, environment) === TEMPORAL_LEVEL;
|
|
209
|
+
if (expression.kind === "Apply" && isExpanded(expression)) {
|
|
189
210
|
const operator = resolveApplyOperator(expression, context);
|
|
190
211
|
const definition = isLocallyResolved(expression)
|
|
191
212
|
? undefined
|
|
192
213
|
: getDefinition(expression.operator, environment);
|
|
193
|
-
|
|
194
|
-
|
|
214
|
+
const isExpandable =
|
|
215
|
+
operator?.kind === "Closure" &&
|
|
216
|
+
operator.params.length === expression.args.length &&
|
|
217
|
+
!definition?.isRecursive;
|
|
218
|
+
if (isExpandable) {
|
|
219
|
+
return expand(
|
|
220
|
+
operator.body,
|
|
221
|
+
bindOperatorArguments(operator, expression.args, context),
|
|
222
|
+
expression.operator,
|
|
223
|
+
);
|
|
195
224
|
}
|
|
196
225
|
}
|
|
197
226
|
const isUnrolled =
|
|
198
|
-
|
|
227
|
+
isProperty &&
|
|
199
228
|
expression.kind === "Forall" &&
|
|
200
|
-
getLevel(expression.body, scope, environment) === TEMPORAL_LEVEL &&
|
|
201
229
|
expression.bounds.every((bound) => getLevel(bound.set, scope, environment) === 0);
|
|
202
230
|
if (isUnrolled) {
|
|
203
|
-
const expanded:
|
|
231
|
+
const expanded: NamedExpression[] = [];
|
|
232
|
+
const bindings = { count: 0 };
|
|
204
233
|
forEachBinding(expression.bounds, context, (boundScope) => {
|
|
205
|
-
|
|
234
|
+
bindings.count++;
|
|
235
|
+
expanded.push(...expand(expression.body, boundScope, describeRange(expression.body)));
|
|
206
236
|
return false;
|
|
207
237
|
});
|
|
238
|
+
// TLC rejects a property that quantifies over no elements before it checks anything.
|
|
239
|
+
if (bindings.count === 0) {
|
|
240
|
+
throw setExitCode(
|
|
241
|
+
new TlaEvaluationError(TAUTOLOGY_MESSAGE),
|
|
242
|
+
EXIT_STATUS.livenessEvaluationFailure,
|
|
243
|
+
);
|
|
244
|
+
}
|
|
208
245
|
return expanded;
|
|
209
246
|
}
|
|
210
|
-
return [{ expression, scope }];
|
|
247
|
+
return [{ expression, scope, name }];
|
|
211
248
|
};
|
|
212
249
|
|
|
213
250
|
const bindLetDefinitions = (
|
package/src/successors.ts
CHANGED
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
bindValue,
|
|
19
19
|
compileArgumentScope,
|
|
20
20
|
evaluate,
|
|
21
|
+
getEntryEvaluator,
|
|
21
22
|
expectBoolean,
|
|
22
23
|
forEachBinding,
|
|
23
24
|
getBoundsDomain,
|
|
@@ -29,6 +30,7 @@ import {
|
|
|
29
30
|
resolveGlobalOperator,
|
|
30
31
|
type Context,
|
|
31
32
|
type Environment,
|
|
33
|
+
type Evaluator,
|
|
32
34
|
type Scope,
|
|
33
35
|
type State,
|
|
34
36
|
} from "./evaluate";
|
|
@@ -87,8 +89,7 @@ const assign = (partial: State, index: number, value: Value) => {
|
|
|
87
89
|
return copy;
|
|
88
90
|
};
|
|
89
91
|
|
|
90
|
-
const holds = (expression: Expression, context: Context) => {
|
|
91
|
-
const value = evaluate(expression, context);
|
|
92
|
+
const holds = (expression: Expression, context: Context, value = evaluate(expression, context)) => {
|
|
92
93
|
if (typeof value === "boolean") return value;
|
|
93
94
|
try {
|
|
94
95
|
return expectBoolean(value, `the action at ${describeLocation(expression)}`);
|
|
@@ -320,6 +321,7 @@ const compileConjunction = (
|
|
|
320
321
|
isInit: boolean,
|
|
321
322
|
): Runner => {
|
|
322
323
|
const conjuncts = operands.map((operand) => toConjunct(operand, environment, isInit));
|
|
324
|
+
const evaluators: (Evaluator | undefined)[] = conjuncts.map(() => undefined);
|
|
323
325
|
const runFrom = (
|
|
324
326
|
start: number,
|
|
325
327
|
run: Run,
|
|
@@ -342,12 +344,17 @@ const compileConjunction = (
|
|
|
342
344
|
const conjunct = conjuncts[index];
|
|
343
345
|
if (conjunct.kind === "Guard") {
|
|
344
346
|
context ??= getContext(run, scope, current);
|
|
345
|
-
|
|
347
|
+
const evaluator = (evaluators[index] ??= getEntryEvaluator(
|
|
348
|
+
conjunct.expression,
|
|
349
|
+
environment,
|
|
350
|
+
));
|
|
351
|
+
if (!holds(conjunct.expression, context, evaluator(context))) return;
|
|
346
352
|
continue;
|
|
347
353
|
}
|
|
348
354
|
if (conjunct.kind === "Assign") {
|
|
349
355
|
context ??= getContext(run, scope, current);
|
|
350
|
-
const
|
|
356
|
+
const evaluator = (evaluators[index] ??= getEntryEvaluator(conjunct.value, environment));
|
|
357
|
+
const value = evaluator(context);
|
|
351
358
|
const assigned = current[conjunct.index];
|
|
352
359
|
if (assigned === undefined) {
|
|
353
360
|
current = assign(current, conjunct.index, value);
|