tla-ts 0.0.3-dev.b1578d0 → 0.0.3-dev.bffbba3
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 +45 -15
- package/src/environment.ts +65 -6
- package/src/evaluate.ts +10 -7
- package/src/instances.ts +3 -3
- package/src/live-check.ts +4 -22
- package/src/liveness.ts +51 -14
- package/src/runtime.ts +2 -0
- package/src/standard-modules.ts +2 -7
- package/src/successors.ts +60 -4
- package/src/tableau.ts +50 -23
- package/src/tlc-module.ts +6 -0
- package/src/values.ts +48 -11
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-module.ts
CHANGED
|
@@ -39,11 +39,17 @@ const formatCalendarVersion = (date: Date) =>
|
|
|
39
39
|
const getRegisterFunction = <Key extends Value>(registers: Map<Key, Value>) =>
|
|
40
40
|
createFunction([...registers].map(([key, value]): [Value, Value] => [key, createTuple([value])]));
|
|
41
41
|
|
|
42
|
+
// TLC creates its model checker after it processes the spec, so these are undefined before then.
|
|
43
|
+
const CHECKER_KEYS = new Set(["diameter", "generated", "distinct", "queue", "stats", "config"]);
|
|
44
|
+
|
|
42
45
|
const getRuntimeValue = (
|
|
43
46
|
runtime: CheckerRuntime,
|
|
44
47
|
key: string,
|
|
45
48
|
state: (Value | undefined)[] | undefined,
|
|
46
49
|
) => {
|
|
50
|
+
if (!runtime.isStarted && CHECKER_KEYS.has(key)) {
|
|
51
|
+
throw new TlaModuleError(`TLCGet(${key}) was undefined.`);
|
|
52
|
+
}
|
|
47
53
|
const duration = Math.floor((Date.now() - runtime.startedAt) / 1000);
|
|
48
54
|
switch (key) {
|
|
49
55
|
case "level":
|
package/src/values.ts
CHANGED
|
@@ -137,6 +137,13 @@ export const isSetValue = (value: Value) =>
|
|
|
137
137
|
value.kind !== "Function" &&
|
|
138
138
|
value.kind !== "LazyFunction";
|
|
139
139
|
|
|
140
|
+
export type ReducibleSet = SetValue | MixedSetValue | IntervalValue;
|
|
141
|
+
|
|
142
|
+
// TLC reduces a set enumeration or an interval eagerly, and keeps every other set lazy.
|
|
143
|
+
export const isReducible = (value: Value): value is ReducibleSet =>
|
|
144
|
+
typeof value === "object" &&
|
|
145
|
+
(value.kind === "Set" || value.kind === "MixedSet" || value.kind === "Interval");
|
|
146
|
+
|
|
140
147
|
export const isFunctionValue = (value: Value): value is FunctionValue | LazyFunctionValue =>
|
|
141
148
|
typeof value === "object" && (value.kind === "Function" || value.kind === "LazyFunction");
|
|
142
149
|
|
|
@@ -371,7 +378,7 @@ export const compareValues = (left: Value, right: Value): number => {
|
|
|
371
378
|
`Attempted to compare ${describeKind(left)} ${formatValue(left)} with ${describeKind(right)} ${formatValue(right)}`,
|
|
372
379
|
);
|
|
373
380
|
}
|
|
374
|
-
if (isLeftSet) return compareSets(toFiniteSet(left),
|
|
381
|
+
if (isLeftSet) return compareSets(toFiniteSet(left), toComparedSet(right));
|
|
375
382
|
return compareFunctions(toFiniteFunction(left), toFiniteFunction(right));
|
|
376
383
|
}
|
|
377
384
|
if (typeof left === "number" && typeof right === "number") return left - right;
|
|
@@ -864,9 +871,11 @@ export const isEqualLikeTlc = (left: Value, right: Value) => {
|
|
|
864
871
|
}
|
|
865
872
|
};
|
|
866
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.
|
|
867
876
|
const isStrictlySorted = (elements: Value[]) => {
|
|
868
877
|
for (let index = 1; index < elements.length; index++) {
|
|
869
|
-
if (compareValues(elements[index
|
|
878
|
+
if (compareValues(elements[index], elements[index - 1]) <= 0) return false;
|
|
870
879
|
}
|
|
871
880
|
return true;
|
|
872
881
|
};
|
|
@@ -1069,13 +1078,16 @@ const isTlcEnumerable = (value: Value) => isSetValue(value) && !isOverriddenSet(
|
|
|
1069
1078
|
const formatUnion = (value: UnionValue, isStrict = false) =>
|
|
1070
1079
|
`UNION(${formatValue(value.base, isStrict)})`;
|
|
1071
1080
|
|
|
1072
|
-
const getUnionSets = (
|
|
1081
|
+
const getUnionSets = (
|
|
1082
|
+
value: UnionValue,
|
|
1083
|
+
getElements = (set: Value) => toFiniteSet(set).elements,
|
|
1084
|
+
) => {
|
|
1073
1085
|
if (!isTlcEnumerable(value.base)) {
|
|
1074
1086
|
throw new TlaEvaluationError(
|
|
1075
1087
|
`Attempted to enumerate the nonenumerable set:\n${formatUnion(value)}`,
|
|
1076
1088
|
);
|
|
1077
1089
|
}
|
|
1078
|
-
return
|
|
1090
|
+
return getElements(value.base);
|
|
1079
1091
|
};
|
|
1080
1092
|
|
|
1081
1093
|
const getUnionSet = (set: Value, index: number, union: UnionValue) => {
|
|
@@ -1111,7 +1123,7 @@ export const isEnumerable = (value: Value): boolean => {
|
|
|
1111
1123
|
case "ProductSet":
|
|
1112
1124
|
return value.factors.every(isEnumerable) || getEmptiness(value) === true;
|
|
1113
1125
|
case "Union":
|
|
1114
|
-
return isEnumerable(value.base) &&
|
|
1126
|
+
return isEnumerable(value.base) && getStreamedElements(value.base).every(isEnumerable);
|
|
1115
1127
|
case "SetOperation":
|
|
1116
1128
|
if (value.operator === "\\cup") return isEnumerable(value.left) && isEnumerable(value.right);
|
|
1117
1129
|
if (value.operator === "\\cap") return isEnumerable(value.left) || isEnumerable(value.right);
|
|
@@ -1318,6 +1330,7 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1318
1330
|
};
|
|
1319
1331
|
|
|
1320
1332
|
const finiteSets = new WeakMap<ObjectValue, SetValue>();
|
|
1333
|
+
const comparedSets = new WeakMap<ObjectValue, SetValue>();
|
|
1321
1334
|
|
|
1322
1335
|
export const toFiniteSet = (value: Value): SetValue => {
|
|
1323
1336
|
if (typeof value === "object" && value.kind === "Set") return value;
|
|
@@ -1326,6 +1339,13 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1326
1339
|
}
|
|
1327
1340
|
const cached = typeof value === "object" ? finiteSets.get(value) : undefined;
|
|
1328
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) => {
|
|
1329
1349
|
const elements = enumerateSet(value);
|
|
1330
1350
|
const isSorted =
|
|
1331
1351
|
typeof value === "object" &&
|
|
@@ -1336,8 +1356,20 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1336
1356
|
value.kind === "ProductSet" ||
|
|
1337
1357
|
value.kind === "FunctionSet" ||
|
|
1338
1358
|
value.kind === "Subset");
|
|
1339
|
-
|
|
1340
|
-
|
|
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);
|
|
1341
1373
|
return set;
|
|
1342
1374
|
};
|
|
1343
1375
|
|
|
@@ -1345,8 +1377,10 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1345
1377
|
// so an element that two of them share comes up once for each.
|
|
1346
1378
|
export const getStreamedElements = (value: Value): Value[] => {
|
|
1347
1379
|
if (typeof value !== "object" || finiteSets.has(value)) return toFiniteSet(value).elements;
|
|
1380
|
+
// TLC streams the sets of a UNION it hasn't enumerated, so a set that appears twice there yields
|
|
1381
|
+
// its elements twice.
|
|
1348
1382
|
if (value.kind === "Union") {
|
|
1349
|
-
return getUnionSets(value).flatMap((set, index) =>
|
|
1383
|
+
return getUnionSets(value, getStreamedElements).flatMap((set, index) =>
|
|
1350
1384
|
getStreamedElements(getUnionSet(set, index, value)),
|
|
1351
1385
|
);
|
|
1352
1386
|
}
|
|
@@ -2101,8 +2135,11 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
|
|
|
2101
2135
|
return `[${parameters} |-> <expression ${source.body}>]`;
|
|
2102
2136
|
}
|
|
2103
2137
|
const functionValue = toFiniteFunction(value);
|
|
2138
|
+
// TLC normalizes a lambda's domain to print it, but evaluates its body afterward.
|
|
2139
|
+
const formatRange = (inner: Value) =>
|
|
2140
|
+
formatValue(inner, isStrict, isNormalized && value.kind === "Function");
|
|
2104
2141
|
if (functionValue.domain.every((key, index) => key === index + 1)) {
|
|
2105
|
-
return `<<${functionValue.range.map(
|
|
2142
|
+
return `<<${functionValue.range.map(formatRange).join(", ")}>>`;
|
|
2106
2143
|
}
|
|
2107
2144
|
if (functionValue.domain.every((key) => typeof key === "string" && isFieldName(key))) {
|
|
2108
2145
|
const isSorted =
|
|
@@ -2110,9 +2147,9 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
|
|
|
2110
2147
|
const pairs = isSorted
|
|
2111
2148
|
? functionValue.domain.map((key, index) => [key, functionValue.range[index]])
|
|
2112
2149
|
: getWrittenPairs(functionValue);
|
|
2113
|
-
return `[${pairs.map((pair) => `${String(pair[0])} |-> ${
|
|
2150
|
+
return `[${pairs.map((pair) => `${String(pair[0])} |-> ${formatRange(pair[1])}`).join(", ")}]`;
|
|
2114
2151
|
}
|
|
2115
|
-
return `(${functionValue.domain.map((key, index) => `${format(key)} :> ${
|
|
2152
|
+
return `(${functionValue.domain.map((key, index) => `${format(key)} :> ${formatRange(functionValue.range[index])}`).join(" @@ ")})`;
|
|
2116
2153
|
}
|
|
2117
2154
|
}
|
|
2118
2155
|
};
|