tla-ts 0.0.3-dev.b74f28c → 0.0.3-dev.bb5b28b
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 +53 -22
- package/dist/index.d.ts +53 -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 +37 -16
- package/dist/transfer.js +37 -16
- package/package.json +1 -1
- package/src/ast.ts +93 -0
- package/src/check.ts +496 -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 +76 -3
- package/src/evaluate.ts +251 -156
- package/src/exit-codes.ts +39 -42
- package/src/instances.ts +16 -15
- package/src/levels.ts +62 -34
- package/src/lexer.ts +5 -1
- package/src/live-check.ts +772 -0
- package/src/liveness.ts +120 -543
- package/src/parallel-worker.ts +7 -3
- package/src/parallel.ts +20 -4
- package/src/parser.ts +33 -11
- package/src/resolve.ts +122 -44
- package/src/run-cli.ts +99 -19
- package/src/runtime.ts +3 -1
- package/src/spec-description.ts +19 -8
- package/src/standard-modules.ts +9 -8
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +10 -10
- package/src/successors.ts +146 -35
- 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/value-sources.ts +29 -0
- package/src/values.ts +167 -52
package/src/values.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { TlaEvaluationError, TlaModuleError } from "./errors";
|
|
2
2
|
import { prettyPrint } from "./pretty-print";
|
|
3
|
+
import { valueSources, withSource } from "./value-sources";
|
|
3
4
|
|
|
4
5
|
export interface ModelValue {
|
|
5
6
|
kind: "ModelValue";
|
|
@@ -137,6 +138,13 @@ export const isSetValue = (value: Value) =>
|
|
|
137
138
|
value.kind !== "Function" &&
|
|
138
139
|
value.kind !== "LazyFunction";
|
|
139
140
|
|
|
141
|
+
export type ReducibleSet = SetValue | MixedSetValue | IntervalValue;
|
|
142
|
+
|
|
143
|
+
// TLC reduces a set enumeration or an interval eagerly, and keeps every other set lazy.
|
|
144
|
+
export const isReducible = (value: Value): value is ReducibleSet =>
|
|
145
|
+
typeof value === "object" &&
|
|
146
|
+
(value.kind === "Set" || value.kind === "MixedSet" || value.kind === "Interval");
|
|
147
|
+
|
|
140
148
|
export const isFunctionValue = (value: Value): value is FunctionValue | LazyFunctionValue =>
|
|
141
149
|
typeof value === "object" && (value.kind === "Function" || value.kind === "LazyFunction");
|
|
142
150
|
|
|
@@ -371,7 +379,7 @@ export const compareValues = (left: Value, right: Value): number => {
|
|
|
371
379
|
`Attempted to compare ${describeKind(left)} ${formatValue(left)} with ${describeKind(right)} ${formatValue(right)}`,
|
|
372
380
|
);
|
|
373
381
|
}
|
|
374
|
-
if (isLeftSet) return compareSets(toFiniteSet(left),
|
|
382
|
+
if (isLeftSet) return compareSets(toFiniteSet(left), toComparedSet(right));
|
|
375
383
|
return compareFunctions(toFiniteFunction(left), toFiniteFunction(right));
|
|
376
384
|
}
|
|
377
385
|
if (typeof left === "number" && typeof right === "number") return left - right;
|
|
@@ -726,16 +734,17 @@ const compareSetLikeTlc = (left: ObjectValue, right: Value): number => {
|
|
|
726
734
|
);
|
|
727
735
|
};
|
|
728
736
|
|
|
729
|
-
const compareLikeTlc = (left: Value, right: Value): number =>
|
|
730
|
-
|
|
731
|
-
if (typeof
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
737
|
+
const compareLikeTlc = (left: Value, right: Value): number =>
|
|
738
|
+
withSource(left, () => {
|
|
739
|
+
if (typeof left !== "object") {
|
|
740
|
+
if (typeof right === typeof left) return compareValues(left, right);
|
|
741
|
+
if (isModelValue(right)) return compareModelValueWith(right, left);
|
|
742
|
+
throw new TlaEvaluationError(describeScalarMismatch(left, right, false));
|
|
743
|
+
}
|
|
744
|
+
if (left.kind === "ModelValue") return compareModelValue(left, right);
|
|
745
|
+
if (isFunctionValue(left)) return compareFunctionLikeTlc(left, right);
|
|
746
|
+
return compareSetLikeTlc(left, right);
|
|
747
|
+
});
|
|
739
748
|
|
|
740
749
|
const equalsElementsLikeTlc = (left: Value[], right: Value[]) =>
|
|
741
750
|
left.every((element, index) => equalsLikeTlc(element, right[index]));
|
|
@@ -823,16 +832,17 @@ const equalsSetLikeTlc = (left: ObjectValue, right: Value): boolean => {
|
|
|
823
832
|
);
|
|
824
833
|
};
|
|
825
834
|
|
|
826
|
-
const equalsLikeTlc = (left: Value, right: Value): boolean =>
|
|
827
|
-
|
|
828
|
-
if (typeof
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
835
|
+
const equalsLikeTlc = (left: Value, right: Value): boolean =>
|
|
836
|
+
withSource(left, () => {
|
|
837
|
+
if (typeof left !== "object") {
|
|
838
|
+
if (typeof right === typeof left) return left === right;
|
|
839
|
+
if (isModelValue(right)) return equalsModelValueWith(right, left);
|
|
840
|
+
throw new TlaEvaluationError(describeScalarMismatch(left, right, true));
|
|
841
|
+
}
|
|
842
|
+
if (left.kind === "ModelValue") return equalsModelValue(left, right);
|
|
843
|
+
if (isFunctionValue(left)) return equalsFunctionLikeTlc(left, right);
|
|
844
|
+
return equalsSetLikeTlc(left, right);
|
|
845
|
+
});
|
|
836
846
|
|
|
837
847
|
// TLC's ValueVec.sort, which inserts each element into the sorted prefix by binary search.
|
|
838
848
|
const sortLikeTlc = (elements: Value[]) => {
|
|
@@ -864,9 +874,11 @@ export const isEqualLikeTlc = (left: Value, right: Value) => {
|
|
|
864
874
|
}
|
|
865
875
|
};
|
|
866
876
|
|
|
877
|
+
// TLC sorts a set by comparing each element with the ones before it, so the later element converts
|
|
878
|
+
// a lazy set it compares and caches it.
|
|
867
879
|
const isStrictlySorted = (elements: Value[]) => {
|
|
868
880
|
for (let index = 1; index < elements.length; index++) {
|
|
869
|
-
if (compareValues(elements[index
|
|
881
|
+
if (compareValues(elements[index], elements[index - 1]) <= 0) return false;
|
|
870
882
|
}
|
|
871
883
|
return true;
|
|
872
884
|
};
|
|
@@ -1069,13 +1081,16 @@ const isTlcEnumerable = (value: Value) => isSetValue(value) && !isOverriddenSet(
|
|
|
1069
1081
|
const formatUnion = (value: UnionValue, isStrict = false) =>
|
|
1070
1082
|
`UNION(${formatValue(value.base, isStrict)})`;
|
|
1071
1083
|
|
|
1072
|
-
const getUnionSets = (
|
|
1084
|
+
const getUnionSets = (
|
|
1085
|
+
value: UnionValue,
|
|
1086
|
+
getElements = (set: Value) => toFiniteSet(set).elements,
|
|
1087
|
+
) => {
|
|
1073
1088
|
if (!isTlcEnumerable(value.base)) {
|
|
1074
1089
|
throw new TlaEvaluationError(
|
|
1075
1090
|
`Attempted to enumerate the nonenumerable set:\n${formatUnion(value)}`,
|
|
1076
1091
|
);
|
|
1077
1092
|
}
|
|
1078
|
-
return
|
|
1093
|
+
return getElements(value.base);
|
|
1079
1094
|
};
|
|
1080
1095
|
|
|
1081
1096
|
const getUnionSet = (set: Value, index: number, union: UnionValue) => {
|
|
@@ -1111,7 +1126,7 @@ export const isEnumerable = (value: Value): boolean => {
|
|
|
1111
1126
|
case "ProductSet":
|
|
1112
1127
|
return value.factors.every(isEnumerable) || getEmptiness(value) === true;
|
|
1113
1128
|
case "Union":
|
|
1114
|
-
return isEnumerable(value.base) &&
|
|
1129
|
+
return isEnumerable(value.base) && getStreamedElements(value.base).every(isEnumerable);
|
|
1115
1130
|
case "SetOperation":
|
|
1116
1131
|
if (value.operator === "\\cup") return isEnumerable(value.left) && isEnumerable(value.right);
|
|
1117
1132
|
if (value.operator === "\\cap") return isEnumerable(value.left) || isEnumerable(value.right);
|
|
@@ -1212,9 +1227,7 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1212
1227
|
if (isOverriddenSet(value.base)) getCardinality(value.base);
|
|
1213
1228
|
const base = toFiniteSet(value.base).elements;
|
|
1214
1229
|
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
|
-
);
|
|
1230
|
+
throw new TlaEvaluationError(describeOverflow(value));
|
|
1218
1231
|
}
|
|
1219
1232
|
return getSortedSubsets(base);
|
|
1220
1233
|
}
|
|
@@ -1318,6 +1331,7 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1318
1331
|
};
|
|
1319
1332
|
|
|
1320
1333
|
const finiteSets = new WeakMap<ObjectValue, SetValue>();
|
|
1334
|
+
const comparedSets = new WeakMap<ObjectValue, SetValue>();
|
|
1321
1335
|
|
|
1322
1336
|
export const toFiniteSet = (value: Value): SetValue => {
|
|
1323
1337
|
if (typeof value === "object" && value.kind === "Set") return value;
|
|
@@ -1326,6 +1340,16 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1326
1340
|
}
|
|
1327
1341
|
const cached = typeof value === "object" ? finiteSets.get(value) : undefined;
|
|
1328
1342
|
if (cached) return cached;
|
|
1343
|
+
const set =
|
|
1344
|
+
(typeof value === "object" ? comparedSets.get(value) : undefined) ??
|
|
1345
|
+
(valueSources.current
|
|
1346
|
+
? withSource(value, () => enumerateFiniteSet(value))
|
|
1347
|
+
: enumerateFiniteSet(value));
|
|
1348
|
+
if (typeof value === "object") finiteSets.set(value, set);
|
|
1349
|
+
return set;
|
|
1350
|
+
};
|
|
1351
|
+
|
|
1352
|
+
const enumerateFiniteSet = (value: Value) => {
|
|
1329
1353
|
const elements = enumerateSet(value);
|
|
1330
1354
|
const isSorted =
|
|
1331
1355
|
typeof value === "object" &&
|
|
@@ -1336,8 +1360,20 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1336
1360
|
value.kind === "ProductSet" ||
|
|
1337
1361
|
value.kind === "FunctionSet" ||
|
|
1338
1362
|
value.kind === "Subset");
|
|
1339
|
-
|
|
1340
|
-
|
|
1363
|
+
return isSorted ? createSortedSet(elements) : createSet(elements);
|
|
1364
|
+
};
|
|
1365
|
+
|
|
1366
|
+
// TLC converts the set a comparison receives as its argument without caching it, so a lazy set
|
|
1367
|
+
// compared that way still streams the sets it combines.
|
|
1368
|
+
const toComparedSet = (value: Value) => {
|
|
1369
|
+
const isStreamed =
|
|
1370
|
+
typeof value === "object" &&
|
|
1371
|
+
(value.kind === "Union" || value.kind === "SetOperation" || value.kind === "FilteredSet");
|
|
1372
|
+
if (!isStreamed || finiteSets.has(value)) return toFiniteSet(value);
|
|
1373
|
+
const compared = comparedSets.get(value);
|
|
1374
|
+
if (compared) return compared;
|
|
1375
|
+
const set = enumerateFiniteSet(value);
|
|
1376
|
+
comparedSets.set(value, set);
|
|
1341
1377
|
return set;
|
|
1342
1378
|
};
|
|
1343
1379
|
|
|
@@ -1345,8 +1381,10 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1345
1381
|
// so an element that two of them share comes up once for each.
|
|
1346
1382
|
export const getStreamedElements = (value: Value): Value[] => {
|
|
1347
1383
|
if (typeof value !== "object" || finiteSets.has(value)) return toFiniteSet(value).elements;
|
|
1384
|
+
// TLC streams the sets of a UNION it hasn't enumerated, so a set that appears twice there yields
|
|
1385
|
+
// its elements twice.
|
|
1348
1386
|
if (value.kind === "Union") {
|
|
1349
|
-
return getUnionSets(value).flatMap((set, index) =>
|
|
1387
|
+
return getUnionSets(value, getStreamedElements).flatMap((set, index) =>
|
|
1350
1388
|
getStreamedElements(getUnionSet(set, index, value)),
|
|
1351
1389
|
);
|
|
1352
1390
|
}
|
|
@@ -1565,6 +1603,12 @@ const describeNonSetMembership = (element: Value, value: Value) => {
|
|
|
1565
1603
|
return `${prefix} function ${formatValue(value)}`;
|
|
1566
1604
|
};
|
|
1567
1605
|
|
|
1606
|
+
// TLC checks membership in a set that another set holds through the inner set's own method.
|
|
1607
|
+
const isElementOfSet = (element: Value, set: Value) =>
|
|
1608
|
+
valueSources.current
|
|
1609
|
+
? withSource(set, () => isElementOf(element, set))
|
|
1610
|
+
: isElementOf(element, set);
|
|
1611
|
+
|
|
1568
1612
|
export const isElementOf = (element: Value, set: Value): boolean => {
|
|
1569
1613
|
if (!isSetValue(set)) throw new TlaEvaluationError(describeNonSetMembership(element, set));
|
|
1570
1614
|
switch (set.kind) {
|
|
@@ -1573,10 +1617,10 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1573
1617
|
return findIndex(set.elements, element) !== -1;
|
|
1574
1618
|
} catch (error) {
|
|
1575
1619
|
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
1576
|
-
return isElementOfLikeTlc(element, set.elements);
|
|
1620
|
+
return withSource(set, () => isElementOfLikeTlc(element, set.elements));
|
|
1577
1621
|
}
|
|
1578
1622
|
case "MixedSet":
|
|
1579
|
-
return isElementOfLikeTlc(element, set.elements);
|
|
1623
|
+
return withSource(set, () => isElementOfLikeTlc(element, set.elements));
|
|
1580
1624
|
case "Interval":
|
|
1581
1625
|
if (typeof element === "number") return element >= set.low && element <= set.high;
|
|
1582
1626
|
if (set.low <= set.high && !isModelValue(element)) {
|
|
@@ -1599,11 +1643,13 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1599
1643
|
}
|
|
1600
1644
|
case "Subset":
|
|
1601
1645
|
if (!isSetValue(element)) throw new TlaEvaluationError(describeMembership(element, set));
|
|
1602
|
-
return toFiniteSet(element).elements.every((item) =>
|
|
1646
|
+
return toFiniteSet(element).elements.every((item) => isElementOfSet(item, set.base));
|
|
1603
1647
|
case "FunctionSet": {
|
|
1604
1648
|
if (!isFunctionValue(element)) {
|
|
1605
1649
|
if (isModelValue(element)) return false;
|
|
1606
|
-
throw new TlaEvaluationError(
|
|
1650
|
+
throw new TlaEvaluationError(
|
|
1651
|
+
`Attempted to check if \n${formatValue(element)}\nwhich is not a TLC function value, is in the set of functions:\n${prettyPrint(formatValue(set))}`,
|
|
1652
|
+
);
|
|
1607
1653
|
}
|
|
1608
1654
|
const functionValue = toFiniteFunction(element);
|
|
1609
1655
|
const low = getIntervalLow(functionValue.domain);
|
|
@@ -1614,7 +1660,7 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1614
1660
|
{ kind: "Interval", low, high: low + functionValue.domain.length - 1 },
|
|
1615
1661
|
set.domain,
|
|
1616
1662
|
);
|
|
1617
|
-
return isDomain && functionValue.range.every((item) =>
|
|
1663
|
+
return isDomain && functionValue.range.every((item) => isElementOfSet(item, set.range));
|
|
1618
1664
|
}
|
|
1619
1665
|
case "RecordSet": {
|
|
1620
1666
|
const functionValue = isFunctionValue(element) ? toFiniteFunction(element) : undefined;
|
|
@@ -1628,7 +1674,7 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1628
1674
|
functionValue.domain.length === set.names.length &&
|
|
1629
1675
|
set.names.every((name, index) => {
|
|
1630
1676
|
const position = findIndex(functionValue.domain, name);
|
|
1631
|
-
return position !== -1 &&
|
|
1677
|
+
return position !== -1 && isElementOfSet(functionValue.range[position], set.sets[index]);
|
|
1632
1678
|
})
|
|
1633
1679
|
);
|
|
1634
1680
|
}
|
|
@@ -1645,24 +1691,24 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1645
1691
|
}
|
|
1646
1692
|
return (
|
|
1647
1693
|
elements.length === set.factors.length &&
|
|
1648
|
-
elements.every((item, index) =>
|
|
1694
|
+
elements.every((item, index) => isElementOfSet(item, set.factors[index]))
|
|
1649
1695
|
);
|
|
1650
1696
|
}
|
|
1651
1697
|
case "FilteredSet":
|
|
1652
|
-
return
|
|
1698
|
+
return isElementOfSet(element, set.base) && set.test(element);
|
|
1653
1699
|
case "Union":
|
|
1654
1700
|
if (!isTlcEnumerable(set.base)) {
|
|
1655
1701
|
throw new TlaEvaluationError(
|
|
1656
1702
|
`Attempted to check if:\n ${formatValue(element)}\nis an element of the non-enumerable set:\n ${formatUnion(set)}`,
|
|
1657
1703
|
);
|
|
1658
1704
|
}
|
|
1659
|
-
return toFiniteSet(set.base).elements.some((inner) =>
|
|
1705
|
+
return toFiniteSet(set.base).elements.some((inner) => isElementOfSet(element, inner));
|
|
1660
1706
|
case "SetOperation":
|
|
1661
1707
|
if (set.operator === "\\cup")
|
|
1662
|
-
return
|
|
1708
|
+
return isElementOfSet(element, set.left) || isElementOfSet(element, set.right);
|
|
1663
1709
|
return (
|
|
1664
|
-
|
|
1665
|
-
|
|
1710
|
+
isElementOfSet(element, set.left) &&
|
|
1711
|
+
isElementOfSet(element, set.right) === (set.operator === "\\cap")
|
|
1666
1712
|
);
|
|
1667
1713
|
case "SequenceSet": {
|
|
1668
1714
|
const elements = getSequenceElements(element);
|
|
@@ -1718,10 +1764,61 @@ export const markLambdaFunction = <FunctionLike extends FunctionValue | LazyFunc
|
|
|
1718
1764
|
return functionValue;
|
|
1719
1765
|
};
|
|
1720
1766
|
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1767
|
+
// TLC applies a tuple, a record and a function over an interval with their own classes, and each
|
|
1768
|
+
// reports a missing argument in its own words.
|
|
1769
|
+
const getFunctionClass = (functionValue: FunctionValue) => {
|
|
1770
|
+
const domain = functionValue.domain;
|
|
1771
|
+
if (domain.length === 0) return lambdaFunctions.has(functionValue) ? "FcnRcdValue" : "TupleValue";
|
|
1772
|
+
if (domain === intervalDomains[domain.length]) return "TupleValue";
|
|
1773
|
+
if (intervalArrays.has(domain)) return "IntervalFcnRcdValue";
|
|
1774
|
+
return domain.every((key) => typeof key === "string") ? "RecordValue" : "FcnRcdValue";
|
|
1775
|
+
};
|
|
1776
|
+
|
|
1777
|
+
const describeNotInDomain = (functionValue: FunctionValue, argument: Value) =>
|
|
1778
|
+
`Attempted to apply function:\n${prettyPrint(formatValue(functionValue))}\nto argument ${prettyPrint(formatValue(argument))}, which is not in the domain of the function.`;
|
|
1779
|
+
|
|
1780
|
+
const describeNonIntegerArgument = (argument: Value) =>
|
|
1781
|
+
`Attempted to apply function with integer domain to the non-integer argument ${prettyPrint(formatValue(argument))}`;
|
|
1782
|
+
|
|
1783
|
+
const describeMissingArgument = (
|
|
1784
|
+
functionValue: FunctionValue | LazyFunctionValue,
|
|
1785
|
+
argument: Value,
|
|
1786
|
+
) => {
|
|
1787
|
+
if (lambdaFunctions.has(functionValue) || functionValue.kind === "LazyFunction")
|
|
1788
|
+
return `In applying the function\n${prettyPrint(formatValue(functionValue))},\nthe first argument is:\n${prettyPrint(formatValue(argument))}\nwhich is not in its domain.\n`;
|
|
1789
|
+
const functionClass = getFunctionClass(functionValue);
|
|
1790
|
+
if (functionClass === "TupleValue")
|
|
1791
|
+
return typeof argument === "number"
|
|
1792
|
+
? `Attempted to access index ${argument} of tuple\n${prettyPrint(formatValue(functionValue))}\nwhich is out of bounds.`
|
|
1793
|
+
: `Attempted to access tuple at a non integral index: ${prettyPrint(formatValue(argument))}`;
|
|
1794
|
+
if (functionClass === "RecordValue")
|
|
1795
|
+
return typeof argument === "string"
|
|
1796
|
+
? `Attempted to access nonexistent field '${argument}' of record\n${prettyPrint(formatValue(functionValue))}`
|
|
1797
|
+
: `Attempted to access record by a non-string argument: ${prettyPrint(formatValue(argument))}`;
|
|
1798
|
+
return functionClass === "IntervalFcnRcdValue" && typeof argument !== "number"
|
|
1799
|
+
? describeNonIntegerArgument(argument)
|
|
1800
|
+
: describeNotInDomain(functionValue, argument);
|
|
1801
|
+
};
|
|
1802
|
+
|
|
1803
|
+
// TLC selects a field of a value that isn't a record from the function the value converts to,
|
|
1804
|
+
// which keeps a tuple's domain as an interval.
|
|
1805
|
+
export const describeMissingField = (functionValue: FunctionValue, field: string) =>
|
|
1806
|
+
getFunctionClass(functionValue) === "FcnRcdValue"
|
|
1807
|
+
? describeNotInDomain(functionValue, field)
|
|
1808
|
+
: describeNonIntegerArgument(field);
|
|
1809
|
+
|
|
1810
|
+
export const isRecordValue = (functionValue: FunctionValue) =>
|
|
1811
|
+
getFunctionClass(functionValue) === "RecordValue";
|
|
1812
|
+
|
|
1813
|
+
// TLC checks the type of an argument to a tuple or a record before it looks the argument up, so
|
|
1814
|
+
// it never compares the argument with a key of another type.
|
|
1815
|
+
const isForeignKey = (domain: Value[], argument: Value) => {
|
|
1816
|
+
const first = domain[0];
|
|
1817
|
+
return typeof first === "number"
|
|
1818
|
+
? typeof argument !== "number" &&
|
|
1819
|
+
(domain === intervalDomains[domain.length] || intervalArrays.has(domain))
|
|
1820
|
+
: typeof first === "string" && typeof argument !== "string";
|
|
1821
|
+
};
|
|
1725
1822
|
|
|
1726
1823
|
const findKeyIndex = (domain: Value[], argument: Value) => {
|
|
1727
1824
|
const first = domain[0];
|
|
@@ -1740,6 +1837,7 @@ export const applyFunction = (
|
|
|
1740
1837
|
functionValue: Value,
|
|
1741
1838
|
argument: Value,
|
|
1742
1839
|
describeMissing?: (functionValue: FunctionValue) => string,
|
|
1840
|
+
describeNonFunction?: (value: Value) => string,
|
|
1743
1841
|
): Value => {
|
|
1744
1842
|
if (typeof functionValue === "object" && functionValue.kind === "LazyFunction") {
|
|
1745
1843
|
if (!isElementOf(argument, functionValue.domain)) {
|
|
@@ -1753,10 +1851,13 @@ export const applyFunction = (
|
|
|
1753
1851
|
}
|
|
1754
1852
|
if (typeof functionValue !== "object" || functionValue.kind !== "Function") {
|
|
1755
1853
|
throw new TlaEvaluationError(
|
|
1756
|
-
|
|
1854
|
+
describeNonFunction?.(functionValue) ??
|
|
1855
|
+
`Attempted to apply ${formatValue(functionValue)}, which is not a function`,
|
|
1757
1856
|
);
|
|
1758
1857
|
}
|
|
1759
|
-
const position =
|
|
1858
|
+
const position = isForeignKey(functionValue.domain, argument)
|
|
1859
|
+
? -1
|
|
1860
|
+
: findKeyIndex(functionValue.domain, argument);
|
|
1760
1861
|
if (position === -1) {
|
|
1761
1862
|
throw new TlaEvaluationError(
|
|
1762
1863
|
describeMissing?.(functionValue) ?? describeMissingArgument(functionValue, argument),
|
|
@@ -2101,8 +2202,11 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
|
|
|
2101
2202
|
return `[${parameters} |-> <expression ${source.body}>]`;
|
|
2102
2203
|
}
|
|
2103
2204
|
const functionValue = toFiniteFunction(value);
|
|
2205
|
+
// TLC normalizes a lambda's domain to print it, but evaluates its body afterward.
|
|
2206
|
+
const formatRange = (inner: Value) =>
|
|
2207
|
+
formatValue(inner, isStrict, isNormalized && value.kind === "Function");
|
|
2104
2208
|
if (functionValue.domain.every((key, index) => key === index + 1)) {
|
|
2105
|
-
return `<<${functionValue.range.map(
|
|
2209
|
+
return `<<${functionValue.range.map(formatRange).join(", ")}>>`;
|
|
2106
2210
|
}
|
|
2107
2211
|
if (functionValue.domain.every((key) => typeof key === "string" && isFieldName(key))) {
|
|
2108
2212
|
const isSorted =
|
|
@@ -2110,9 +2214,20 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
|
|
|
2110
2214
|
const pairs = isSorted
|
|
2111
2215
|
? functionValue.domain.map((key, index) => [key, functionValue.range[index]])
|
|
2112
2216
|
: getWrittenPairs(functionValue);
|
|
2113
|
-
return `[${pairs.map((pair) => `${String(pair[0])} |-> ${
|
|
2217
|
+
return `[${pairs.map((pair) => `${String(pair[0])} |-> ${formatRange(pair[1])}`).join(", ")}]`;
|
|
2114
2218
|
}
|
|
2115
|
-
return `(${functionValue.domain.map((key, index) => `${format(key)} :> ${
|
|
2219
|
+
return `(${functionValue.domain.map((key, index) => `${format(key)} :> ${formatRange(functionValue.range[index])}`).join(" @@ ")})`;
|
|
2116
2220
|
}
|
|
2117
2221
|
}
|
|
2118
2222
|
};
|
|
2223
|
+
|
|
2224
|
+
// TLC's TLCState.toString: a line per variable, as a conjunction when there are several.
|
|
2225
|
+
export const formatState = (state: Record<string, Value>) => {
|
|
2226
|
+
const entries = Object.entries(state);
|
|
2227
|
+
return entries
|
|
2228
|
+
.map(
|
|
2229
|
+
([name, value]) =>
|
|
2230
|
+
`${entries.length === 1 ? "" : "/\\ "}${name} = ${prettyPrint(formatValue(value))}\n`,
|
|
2231
|
+
)
|
|
2232
|
+
.join("");
|
|
2233
|
+
};
|