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/src/tableau.ts
CHANGED
|
@@ -162,22 +162,39 @@ const containsAction = (expression: LiveExpression): boolean => {
|
|
|
162
162
|
}
|
|
163
163
|
};
|
|
164
164
|
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
165
|
+
const termKeys = new WeakMap<LiveExpression, string>();
|
|
166
|
+
const atoms = { count: 0 };
|
|
167
|
+
|
|
168
|
+
// A term's structure as a string in which each atom stands for itself, so equal terms share a key.
|
|
169
|
+
export const getTermKey = (expression: LiveExpression): string => {
|
|
170
|
+
const cached = termKeys.get(expression);
|
|
171
|
+
if (cached !== undefined) return cached;
|
|
172
|
+
const key = (() => {
|
|
173
|
+
switch (expression.kind) {
|
|
174
|
+
case "Boolean":
|
|
175
|
+
return String(expression.value);
|
|
176
|
+
case "State":
|
|
177
|
+
case "Enabled":
|
|
178
|
+
case "Action":
|
|
179
|
+
return `#${atoms.count++}`;
|
|
180
|
+
case "And":
|
|
181
|
+
case "Or":
|
|
182
|
+
return `${expression.kind}(${expression.operands.map(getTermKey).join(",")})`;
|
|
183
|
+
default:
|
|
184
|
+
return `${expression.kind}(${getTermKey(expression.body)})`;
|
|
185
|
+
}
|
|
186
|
+
})();
|
|
187
|
+
termKeys.set(expression, key);
|
|
188
|
+
return key;
|
|
177
189
|
};
|
|
178
190
|
|
|
179
|
-
const
|
|
180
|
-
|
|
191
|
+
const isEqual = (left: LiveExpression, right: LiveExpression) =>
|
|
192
|
+
left === right || getTermKey(left) === getTermKey(right);
|
|
193
|
+
|
|
194
|
+
const isMember = (items: LiveExpression[], expression: LiveExpression) => {
|
|
195
|
+
const key = getTermKey(expression);
|
|
196
|
+
return items.some((item) => item === expression || getTermKey(item) === key);
|
|
197
|
+
};
|
|
181
198
|
|
|
182
199
|
const getDual = (kind: LiveJunction["kind"]) => (kind === "And" ? "Or" : "And");
|
|
183
200
|
|
|
@@ -446,13 +463,14 @@ const getTriples = (
|
|
|
446
463
|
return parts ? [{ whole: term, first: parts.first, second: parts.second }] : [];
|
|
447
464
|
});
|
|
448
465
|
|
|
449
|
-
|
|
450
|
-
|
|
466
|
+
// A particle is a set of terms, so its key ignores order and repeats.
|
|
467
|
+
const getParticleKey = (particle: LiveExpression[]) =>
|
|
468
|
+
[...new Set(particle.map(getTermKey))].sort().join("|");
|
|
451
469
|
|
|
452
|
-
const unionParticles = (left: LiveExpression[][], right: LiveExpression[][]) =>
|
|
453
|
-
|
|
454
|
-
...right
|
|
455
|
-
|
|
470
|
+
const unionParticles = (left: LiveExpression[][], right: LiveExpression[][]) => {
|
|
471
|
+
const rightKeys = new Set(right.map(getParticleKey));
|
|
472
|
+
return [...left.filter((particle) => !rightKeys.has(getParticleKey(particle))), ...right];
|
|
473
|
+
};
|
|
456
474
|
|
|
457
475
|
const getParticleClosure = (terms: LiveExpression[]) => {
|
|
458
476
|
const closure = getPositiveClosure(terms);
|
|
@@ -516,14 +534,23 @@ const createTableauNode = (particle: LiveExpression[]): TableauNode => ({
|
|
|
516
534
|
const createTableau = (formula: LiveExpression) => {
|
|
517
535
|
const nodes = getParticleClosure([formula]).map(createTableauNode);
|
|
518
536
|
const initialCount = nodes.length;
|
|
537
|
+
const indices = new Map<string, number>();
|
|
538
|
+
nodes.forEach((node, index) => {
|
|
539
|
+
const key = getParticleKey(node.particle);
|
|
540
|
+
if (!indices.has(key)) indices.set(key, index);
|
|
541
|
+
});
|
|
519
542
|
for (let index = 0; index < nodes.length; index++) {
|
|
520
543
|
const implied = nodes[index].particle.flatMap((term) =>
|
|
521
544
|
term.kind === "Next" ? [term.body] : [],
|
|
522
545
|
);
|
|
523
546
|
for (const particle of getParticleClosure(implied)) {
|
|
524
|
-
const
|
|
525
|
-
|
|
526
|
-
|
|
547
|
+
const key = getParticleKey(particle);
|
|
548
|
+
const existing = indices.get(key);
|
|
549
|
+
if (existing === undefined) {
|
|
550
|
+
indices.set(key, nodes.length);
|
|
551
|
+
nodes.push(createTableauNode(particle));
|
|
552
|
+
}
|
|
553
|
+
nodes[index].successors.push(existing ?? nodes.length - 1);
|
|
527
554
|
}
|
|
528
555
|
}
|
|
529
556
|
return { nodes, initialCount };
|
package/src/tlc-random.ts
CHANGED
|
@@ -181,8 +181,10 @@ const createShiftTable = (length: number) => {
|
|
|
181
181
|
|
|
182
182
|
// Extending a fingerprint by a byte is linear over GF(2), so extending it by a value's bytes
|
|
183
183
|
// XORs the value's own fingerprint from zero into the fingerprint shifted by their length.
|
|
184
|
-
// The fingerprinter caches each distinct variable value for one pass over the stored states.
|
|
185
|
-
|
|
184
|
+
// The fingerprinter caches each distinct variable value for one pass over the stored states. It
|
|
185
|
+
// reads a state's variables in `order` and writes each fingerprint's low and high words into
|
|
186
|
+
// `words`, an Int32Array over a little-endian BigInt64Array.
|
|
187
|
+
export const createTlcFingerprinter = (order: number[], words: Int32Array) => {
|
|
186
188
|
const shiftTables = new Map<number, Int32Array>();
|
|
187
189
|
const indices = new Map<Value, number>();
|
|
188
190
|
const contributions: number[] = [];
|
|
@@ -204,11 +206,11 @@ export const createTlcFingerprinter = () => {
|
|
|
204
206
|
shiftTables.set(length, table);
|
|
205
207
|
return table;
|
|
206
208
|
};
|
|
207
|
-
return (state: Value[]) => {
|
|
209
|
+
return (state: Value[], position: number) => {
|
|
208
210
|
const outer = { high, low };
|
|
209
211
|
const fingerprint = { high: INITIAL_HIGH, low: INITIAL_LOW };
|
|
210
|
-
for (const
|
|
211
|
-
const contribution = getContribution(
|
|
212
|
+
for (const index of order) {
|
|
213
|
+
const contribution = getContribution(state[index]);
|
|
212
214
|
const table = getShiftTable(contributions[contribution]);
|
|
213
215
|
const extended = {
|
|
214
216
|
high: contributions[contribution + 1],
|
|
@@ -225,7 +227,8 @@ export const createTlcFingerprinter = () => {
|
|
|
225
227
|
}
|
|
226
228
|
high = outer.high;
|
|
227
229
|
low = outer.low;
|
|
228
|
-
|
|
230
|
+
words[2 * position] = fingerprint.low;
|
|
231
|
+
words[2 * position + 1] = fingerprint.high;
|
|
229
232
|
};
|
|
230
233
|
};
|
|
231
234
|
|
package/src/tlc-summary.ts
CHANGED
|
@@ -87,11 +87,26 @@ export const getFingerprintDistance = (
|
|
|
87
87
|
const probability = getOptimisticProbability(distinctStates, generatedStates);
|
|
88
88
|
if (probability < ACTUAL_PROBABILITY_THRESHOLD) return undefined;
|
|
89
89
|
const fingerprints = getFingerprints().sort();
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
90
|
+
// The low and high words of each fingerprint. A gap whose high words differ by at most 2^20 is
|
|
91
|
+
// below 2^53, so a number holds it exactly and only wider gaps need a BigInt.
|
|
92
|
+
const words = new Int32Array(fingerprints.buffer);
|
|
93
|
+
const nearest = { narrow: Number.POSITIVE_INFINITY, wide: 0x7fff_ffff_ffff_ffffn };
|
|
94
|
+
for (let index = 1; index < fingerprints.length; index++) {
|
|
95
|
+
const high = words[2 * index + 1];
|
|
96
|
+
const previousHigh = words[2 * index - 1];
|
|
97
|
+
if (high < 0 !== previousHigh < 0) continue;
|
|
98
|
+
const highGap = high - previousHigh;
|
|
99
|
+
if (highGap <= 0x10_0000) {
|
|
100
|
+
const gap = highGap * 0x1_0000_0000 + (words[2 * index] >>> 0) - (words[2 * index - 2] >>> 0);
|
|
101
|
+
nearest.narrow = Math.min(nearest.narrow, gap);
|
|
102
|
+
} else {
|
|
103
|
+
const gap = fingerprints[index] - fingerprints[index - 1];
|
|
104
|
+
if (gap < nearest.wide) nearest.wide = gap;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (nearest.narrow === Number.POSITIVE_INFINITY) return nearest.wide;
|
|
108
|
+
const narrow = BigInt(nearest.narrow);
|
|
109
|
+
return narrow < nearest.wide ? narrow : nearest.wide;
|
|
95
110
|
};
|
|
96
111
|
|
|
97
112
|
export const getOutDegreeSummary = (buckets: number[]) => {
|
package/src/values.ts
CHANGED
|
@@ -378,7 +378,7 @@ export const compareValues = (left: Value, right: Value): number => {
|
|
|
378
378
|
`Attempted to compare ${describeKind(left)} ${formatValue(left)} with ${describeKind(right)} ${formatValue(right)}`,
|
|
379
379
|
);
|
|
380
380
|
}
|
|
381
|
-
if (isLeftSet) return compareSets(toFiniteSet(left),
|
|
381
|
+
if (isLeftSet) return compareSets(toFiniteSet(left), toComparedSet(right));
|
|
382
382
|
return compareFunctions(toFiniteFunction(left), toFiniteFunction(right));
|
|
383
383
|
}
|
|
384
384
|
if (typeof left === "number" && typeof right === "number") return left - right;
|
|
@@ -871,9 +871,11 @@ export const isEqualLikeTlc = (left: Value, right: Value) => {
|
|
|
871
871
|
}
|
|
872
872
|
};
|
|
873
873
|
|
|
874
|
+
// TLC sorts a set by comparing each element with the ones before it, so the later element converts
|
|
875
|
+
// a lazy set it compares and caches it.
|
|
874
876
|
const isStrictlySorted = (elements: Value[]) => {
|
|
875
877
|
for (let index = 1; index < elements.length; index++) {
|
|
876
|
-
if (compareValues(elements[index
|
|
878
|
+
if (compareValues(elements[index], elements[index - 1]) <= 0) return false;
|
|
877
879
|
}
|
|
878
880
|
return true;
|
|
879
881
|
};
|
|
@@ -1328,6 +1330,7 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1328
1330
|
};
|
|
1329
1331
|
|
|
1330
1332
|
const finiteSets = new WeakMap<ObjectValue, SetValue>();
|
|
1333
|
+
const comparedSets = new WeakMap<ObjectValue, SetValue>();
|
|
1331
1334
|
|
|
1332
1335
|
export const toFiniteSet = (value: Value): SetValue => {
|
|
1333
1336
|
if (typeof value === "object" && value.kind === "Set") return value;
|
|
@@ -1336,6 +1339,13 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1336
1339
|
}
|
|
1337
1340
|
const cached = typeof value === "object" ? finiteSets.get(value) : undefined;
|
|
1338
1341
|
if (cached) return cached;
|
|
1342
|
+
const set =
|
|
1343
|
+
(typeof value === "object" ? comparedSets.get(value) : undefined) ?? enumerateFiniteSet(value);
|
|
1344
|
+
if (typeof value === "object") finiteSets.set(value, set);
|
|
1345
|
+
return set;
|
|
1346
|
+
};
|
|
1347
|
+
|
|
1348
|
+
const enumerateFiniteSet = (value: Value) => {
|
|
1339
1349
|
const elements = enumerateSet(value);
|
|
1340
1350
|
const isSorted =
|
|
1341
1351
|
typeof value === "object" &&
|
|
@@ -1346,8 +1356,20 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1346
1356
|
value.kind === "ProductSet" ||
|
|
1347
1357
|
value.kind === "FunctionSet" ||
|
|
1348
1358
|
value.kind === "Subset");
|
|
1349
|
-
|
|
1350
|
-
|
|
1359
|
+
return isSorted ? createSortedSet(elements) : createSet(elements);
|
|
1360
|
+
};
|
|
1361
|
+
|
|
1362
|
+
// TLC converts the set a comparison receives as its argument without caching it, so a lazy set
|
|
1363
|
+
// compared that way still streams the sets it combines.
|
|
1364
|
+
const toComparedSet = (value: Value) => {
|
|
1365
|
+
const isStreamed =
|
|
1366
|
+
typeof value === "object" &&
|
|
1367
|
+
(value.kind === "Union" || value.kind === "SetOperation" || value.kind === "FilteredSet");
|
|
1368
|
+
if (!isStreamed || finiteSets.has(value)) return toFiniteSet(value);
|
|
1369
|
+
const compared = comparedSets.get(value);
|
|
1370
|
+
if (compared) return compared;
|
|
1371
|
+
const set = enumerateFiniteSet(value);
|
|
1372
|
+
comparedSets.set(value, set);
|
|
1351
1373
|
return set;
|
|
1352
1374
|
};
|
|
1353
1375
|
|