tla-ts 0.0.3-dev.d34b19a → 0.0.3-dev.d9ff6f2
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 +5 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +47 -22
- package/dist/index.d.ts +47 -22
- 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 +19 -16
- package/dist/run-cli.js +20 -17
- package/dist/transfer.cjs +36 -15
- package/dist/transfer.js +36 -15
- package/package.json +1 -1
- package/src/ast.ts +93 -0
- package/src/check.ts +438 -98
- package/src/cli-options.ts +11 -5
- package/src/codegen.ts +15 -22
- package/src/config.ts +224 -170
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +116 -28
- package/src/errors.ts +65 -3
- package/src/evaluate.ts +163 -156
- package/src/exit-codes.ts +39 -42
- package/src/instances.ts +16 -15
- package/src/levels.ts +62 -34
- package/src/live-check.ts +772 -0
- package/src/liveness.ts +120 -543
- package/src/parallel-worker.ts +2 -2
- package/src/parallel.ts +20 -4
- package/src/parser.ts +33 -11
- package/src/resolve.ts +122 -44
- package/src/run-cli.ts +91 -19
- package/src/runtime.ts +3 -1
- package/src/spec-description.ts +19 -8
- package/src/standard-modules.ts +2 -7
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +10 -10
- package/src/successors.ts +145 -27
- package/src/symmetry.ts +16 -6
- package/src/tableau.ts +51 -24
- package/src/tlc-ext.ts +29 -12
- package/src/tlc-module.ts +14 -33
- package/src/tlc-modules.ts +4 -0
- 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 +124 -21
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);
|
|
@@ -1212,9 +1224,7 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1212
1224
|
if (isOverriddenSet(value.base)) getCardinality(value.base);
|
|
1213
1225
|
const base = toFiniteSet(value.base).elements;
|
|
1214
1226
|
if (base.length > MAXIMUM_ENUMERATED_SUBSET_BASE) {
|
|
1215
|
-
throw new TlaEvaluationError(
|
|
1216
|
-
`SUBSET of a set with ${base.length} elements is too large to enumerate`,
|
|
1217
|
-
);
|
|
1227
|
+
throw new TlaEvaluationError(describeOverflow(value));
|
|
1218
1228
|
}
|
|
1219
1229
|
return getSortedSubsets(base);
|
|
1220
1230
|
}
|
|
@@ -1318,6 +1328,7 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1318
1328
|
};
|
|
1319
1329
|
|
|
1320
1330
|
const finiteSets = new WeakMap<ObjectValue, SetValue>();
|
|
1331
|
+
const comparedSets = new WeakMap<ObjectValue, SetValue>();
|
|
1321
1332
|
|
|
1322
1333
|
export const toFiniteSet = (value: Value): SetValue => {
|
|
1323
1334
|
if (typeof value === "object" && value.kind === "Set") return value;
|
|
@@ -1326,6 +1337,13 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1326
1337
|
}
|
|
1327
1338
|
const cached = typeof value === "object" ? finiteSets.get(value) : undefined;
|
|
1328
1339
|
if (cached) return cached;
|
|
1340
|
+
const set =
|
|
1341
|
+
(typeof value === "object" ? comparedSets.get(value) : undefined) ?? enumerateFiniteSet(value);
|
|
1342
|
+
if (typeof value === "object") finiteSets.set(value, set);
|
|
1343
|
+
return set;
|
|
1344
|
+
};
|
|
1345
|
+
|
|
1346
|
+
const enumerateFiniteSet = (value: Value) => {
|
|
1329
1347
|
const elements = enumerateSet(value);
|
|
1330
1348
|
const isSorted =
|
|
1331
1349
|
typeof value === "object" &&
|
|
@@ -1336,8 +1354,20 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1336
1354
|
value.kind === "ProductSet" ||
|
|
1337
1355
|
value.kind === "FunctionSet" ||
|
|
1338
1356
|
value.kind === "Subset");
|
|
1339
|
-
|
|
1340
|
-
|
|
1357
|
+
return isSorted ? createSortedSet(elements) : createSet(elements);
|
|
1358
|
+
};
|
|
1359
|
+
|
|
1360
|
+
// TLC converts the set a comparison receives as its argument without caching it, so a lazy set
|
|
1361
|
+
// compared that way still streams the sets it combines.
|
|
1362
|
+
const toComparedSet = (value: Value) => {
|
|
1363
|
+
const isStreamed =
|
|
1364
|
+
typeof value === "object" &&
|
|
1365
|
+
(value.kind === "Union" || value.kind === "SetOperation" || value.kind === "FilteredSet");
|
|
1366
|
+
if (!isStreamed || finiteSets.has(value)) return toFiniteSet(value);
|
|
1367
|
+
const compared = comparedSets.get(value);
|
|
1368
|
+
if (compared) return compared;
|
|
1369
|
+
const set = enumerateFiniteSet(value);
|
|
1370
|
+
comparedSets.set(value, set);
|
|
1341
1371
|
return set;
|
|
1342
1372
|
};
|
|
1343
1373
|
|
|
@@ -1345,8 +1375,10 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1345
1375
|
// so an element that two of them share comes up once for each.
|
|
1346
1376
|
export const getStreamedElements = (value: Value): Value[] => {
|
|
1347
1377
|
if (typeof value !== "object" || finiteSets.has(value)) return toFiniteSet(value).elements;
|
|
1378
|
+
// TLC streams the sets of a UNION it hasn't enumerated, so a set that appears twice there yields
|
|
1379
|
+
// its elements twice.
|
|
1348
1380
|
if (value.kind === "Union") {
|
|
1349
|
-
return getUnionSets(value).flatMap((set, index) =>
|
|
1381
|
+
return getUnionSets(value, getStreamedElements).flatMap((set, index) =>
|
|
1350
1382
|
getStreamedElements(getUnionSet(set, index, value)),
|
|
1351
1383
|
);
|
|
1352
1384
|
}
|
|
@@ -1603,7 +1635,9 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1603
1635
|
case "FunctionSet": {
|
|
1604
1636
|
if (!isFunctionValue(element)) {
|
|
1605
1637
|
if (isModelValue(element)) return false;
|
|
1606
|
-
throw new TlaEvaluationError(
|
|
1638
|
+
throw new TlaEvaluationError(
|
|
1639
|
+
`Attempted to check if \n${formatValue(element)}\nwhich is not a TLC function value, is in the set of functions:\n${prettyPrint(formatValue(set))}`,
|
|
1640
|
+
);
|
|
1607
1641
|
}
|
|
1608
1642
|
const functionValue = toFiniteFunction(element);
|
|
1609
1643
|
const low = getIntervalLow(functionValue.domain);
|
|
@@ -1718,10 +1752,61 @@ export const markLambdaFunction = <FunctionLike extends FunctionValue | LazyFunc
|
|
|
1718
1752
|
return functionValue;
|
|
1719
1753
|
};
|
|
1720
1754
|
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1755
|
+
// TLC applies a tuple, a record and a function over an interval with their own classes, and each
|
|
1756
|
+
// reports a missing argument in its own words.
|
|
1757
|
+
const getFunctionClass = (functionValue: FunctionValue) => {
|
|
1758
|
+
const domain = functionValue.domain;
|
|
1759
|
+
if (domain.length === 0) return lambdaFunctions.has(functionValue) ? "FcnRcdValue" : "TupleValue";
|
|
1760
|
+
if (domain === intervalDomains[domain.length]) return "TupleValue";
|
|
1761
|
+
if (intervalArrays.has(domain)) return "IntervalFcnRcdValue";
|
|
1762
|
+
return domain.every((key) => typeof key === "string") ? "RecordValue" : "FcnRcdValue";
|
|
1763
|
+
};
|
|
1764
|
+
|
|
1765
|
+
const describeNotInDomain = (functionValue: FunctionValue, argument: Value) =>
|
|
1766
|
+
`Attempted to apply function:\n${prettyPrint(formatValue(functionValue))}\nto argument ${prettyPrint(formatValue(argument))}, which is not in the domain of the function.`;
|
|
1767
|
+
|
|
1768
|
+
const describeNonIntegerArgument = (argument: Value) =>
|
|
1769
|
+
`Attempted to apply function with integer domain to the non-integer argument ${prettyPrint(formatValue(argument))}`;
|
|
1770
|
+
|
|
1771
|
+
const describeMissingArgument = (
|
|
1772
|
+
functionValue: FunctionValue | LazyFunctionValue,
|
|
1773
|
+
argument: Value,
|
|
1774
|
+
) => {
|
|
1775
|
+
if (lambdaFunctions.has(functionValue) || functionValue.kind === "LazyFunction")
|
|
1776
|
+
return `In applying the function\n${prettyPrint(formatValue(functionValue))},\nthe first argument is:\n${prettyPrint(formatValue(argument))}\nwhich is not in its domain.\n`;
|
|
1777
|
+
const functionClass = getFunctionClass(functionValue);
|
|
1778
|
+
if (functionClass === "TupleValue")
|
|
1779
|
+
return typeof argument === "number"
|
|
1780
|
+
? `Attempted to access index ${argument} of tuple\n${prettyPrint(formatValue(functionValue))}\nwhich is out of bounds.`
|
|
1781
|
+
: `Attempted to access tuple at a non integral index: ${prettyPrint(formatValue(argument))}`;
|
|
1782
|
+
if (functionClass === "RecordValue")
|
|
1783
|
+
return typeof argument === "string"
|
|
1784
|
+
? `Attempted to access nonexistent field '${argument}' of record\n${prettyPrint(formatValue(functionValue))}`
|
|
1785
|
+
: `Attempted to access record by a non-string argument: ${prettyPrint(formatValue(argument))}`;
|
|
1786
|
+
return functionClass === "IntervalFcnRcdValue" && typeof argument !== "number"
|
|
1787
|
+
? describeNonIntegerArgument(argument)
|
|
1788
|
+
: describeNotInDomain(functionValue, argument);
|
|
1789
|
+
};
|
|
1790
|
+
|
|
1791
|
+
// TLC selects a field of a value that isn't a record from the function the value converts to,
|
|
1792
|
+
// which keeps a tuple's domain as an interval.
|
|
1793
|
+
export const describeMissingField = (functionValue: FunctionValue, field: string) =>
|
|
1794
|
+
getFunctionClass(functionValue) === "FcnRcdValue"
|
|
1795
|
+
? describeNotInDomain(functionValue, field)
|
|
1796
|
+
: describeNonIntegerArgument(field);
|
|
1797
|
+
|
|
1798
|
+
export const isRecordValue = (functionValue: FunctionValue) =>
|
|
1799
|
+
getFunctionClass(functionValue) === "RecordValue";
|
|
1800
|
+
|
|
1801
|
+
// TLC checks the type of an argument to a tuple or a record before it looks the argument up, so
|
|
1802
|
+
// it never compares the argument with a key of another type.
|
|
1803
|
+
const isForeignKey = (domain: Value[], argument: Value) => {
|
|
1804
|
+
const first = domain[0];
|
|
1805
|
+
return typeof first === "number"
|
|
1806
|
+
? typeof argument !== "number" &&
|
|
1807
|
+
(domain === intervalDomains[domain.length] || intervalArrays.has(domain))
|
|
1808
|
+
: typeof first === "string" && typeof argument !== "string";
|
|
1809
|
+
};
|
|
1725
1810
|
|
|
1726
1811
|
const findKeyIndex = (domain: Value[], argument: Value) => {
|
|
1727
1812
|
const first = domain[0];
|
|
@@ -1740,6 +1825,7 @@ export const applyFunction = (
|
|
|
1740
1825
|
functionValue: Value,
|
|
1741
1826
|
argument: Value,
|
|
1742
1827
|
describeMissing?: (functionValue: FunctionValue) => string,
|
|
1828
|
+
describeNonFunction?: (value: Value) => string,
|
|
1743
1829
|
): Value => {
|
|
1744
1830
|
if (typeof functionValue === "object" && functionValue.kind === "LazyFunction") {
|
|
1745
1831
|
if (!isElementOf(argument, functionValue.domain)) {
|
|
@@ -1753,10 +1839,13 @@ export const applyFunction = (
|
|
|
1753
1839
|
}
|
|
1754
1840
|
if (typeof functionValue !== "object" || functionValue.kind !== "Function") {
|
|
1755
1841
|
throw new TlaEvaluationError(
|
|
1756
|
-
|
|
1842
|
+
describeNonFunction?.(functionValue) ??
|
|
1843
|
+
`Attempted to apply ${formatValue(functionValue)}, which is not a function`,
|
|
1757
1844
|
);
|
|
1758
1845
|
}
|
|
1759
|
-
const position =
|
|
1846
|
+
const position = isForeignKey(functionValue.domain, argument)
|
|
1847
|
+
? -1
|
|
1848
|
+
: findKeyIndex(functionValue.domain, argument);
|
|
1760
1849
|
if (position === -1) {
|
|
1761
1850
|
throw new TlaEvaluationError(
|
|
1762
1851
|
describeMissing?.(functionValue) ?? describeMissingArgument(functionValue, argument),
|
|
@@ -2101,8 +2190,11 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
|
|
|
2101
2190
|
return `[${parameters} |-> <expression ${source.body}>]`;
|
|
2102
2191
|
}
|
|
2103
2192
|
const functionValue = toFiniteFunction(value);
|
|
2193
|
+
// TLC normalizes a lambda's domain to print it, but evaluates its body afterward.
|
|
2194
|
+
const formatRange = (inner: Value) =>
|
|
2195
|
+
formatValue(inner, isStrict, isNormalized && value.kind === "Function");
|
|
2104
2196
|
if (functionValue.domain.every((key, index) => key === index + 1)) {
|
|
2105
|
-
return `<<${functionValue.range.map(
|
|
2197
|
+
return `<<${functionValue.range.map(formatRange).join(", ")}>>`;
|
|
2106
2198
|
}
|
|
2107
2199
|
if (functionValue.domain.every((key) => typeof key === "string" && isFieldName(key))) {
|
|
2108
2200
|
const isSorted =
|
|
@@ -2110,9 +2202,20 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
|
|
|
2110
2202
|
const pairs = isSorted
|
|
2111
2203
|
? functionValue.domain.map((key, index) => [key, functionValue.range[index]])
|
|
2112
2204
|
: getWrittenPairs(functionValue);
|
|
2113
|
-
return `[${pairs.map((pair) => `${String(pair[0])} |-> ${
|
|
2205
|
+
return `[${pairs.map((pair) => `${String(pair[0])} |-> ${formatRange(pair[1])}`).join(", ")}]`;
|
|
2114
2206
|
}
|
|
2115
|
-
return `(${functionValue.domain.map((key, index) => `${format(key)} :> ${
|
|
2207
|
+
return `(${functionValue.domain.map((key, index) => `${format(key)} :> ${formatRange(functionValue.range[index])}`).join(" @@ ")})`;
|
|
2116
2208
|
}
|
|
2117
2209
|
}
|
|
2118
2210
|
};
|
|
2211
|
+
|
|
2212
|
+
// TLC's TLCState.toString: a line per variable, as a conjunction when there are several.
|
|
2213
|
+
export const formatState = (state: Record<string, Value>) => {
|
|
2214
|
+
const entries = Object.entries(state);
|
|
2215
|
+
return entries
|
|
2216
|
+
.map(
|
|
2217
|
+
([name, value]) =>
|
|
2218
|
+
`${entries.length === 1 ? "" : "/\\ "}${name} = ${prettyPrint(formatValue(value))}\n`,
|
|
2219
|
+
)
|
|
2220
|
+
.join("");
|
|
2221
|
+
};
|