tla-ts 0.0.2 → 0.0.3-dev.14337b3
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 +78 -12
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +85 -15
- package/dist/index.d.ts +85 -15
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +2 -2
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +22 -2
- package/dist/run-cli.js +22 -2
- package/dist/transfer.cjs +37 -16
- package/dist/transfer.js +37 -16
- package/package.json +3 -1
- package/src/ast.ts +137 -4
- package/src/check.ts +378 -100
- package/src/cli-options.ts +64 -0
- 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 +101 -3
- package/src/evaluate.ts +129 -153
- package/src/exit-codes.ts +39 -42
- package/src/fingerprints.ts +46 -11
- package/src/instances.ts +16 -15
- package/src/levels.ts +62 -34
- package/src/lexer.ts +11 -2
- package/src/live-check.ts +772 -0
- package/src/liveness.ts +116 -543
- package/src/obligation-action.ts +384 -0
- package/src/obligation-anon.ts +575 -0
- package/src/obligation-deref.ts +637 -0
- package/src/obligation-expression.ts +1060 -0
- package/src/obligation-format.ts +406 -0
- package/src/obligation-generate.ts +1074 -0
- package/src/obligation-levels.ts +329 -0
- package/src/obligation-module.ts +1042 -0
- package/src/obligation-printer.ts +889 -0
- package/src/obligation-proof.ts +76 -0
- package/src/obligation-source.ts +1084 -0
- package/src/obligation-tuples.ts +184 -0
- package/src/obligations.ts +316 -0
- package/src/parallel-worker.ts +168 -212
- package/src/parallel.ts +78 -37
- package/src/parser.ts +315 -60
- package/src/resolve.ts +122 -44
- package/src/run-cli.ts +376 -44
- package/src/runtime.ts +3 -1
- package/src/spec-description.ts +19 -8
- package/src/standard-levels.ts +3 -3
- package/src/standard-modules.ts +2 -7
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +10 -10
- package/src/successors.ts +99 -25
- package/src/symmetry.ts +30 -8
- 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/transfer.ts +22 -1
- package/src/values.ts +179 -42
package/src/values.ts
CHANGED
|
@@ -4,6 +4,8 @@ import { prettyPrint } from "./pretty-print";
|
|
|
4
4
|
export interface ModelValue {
|
|
5
5
|
kind: "ModelValue";
|
|
6
6
|
name: string;
|
|
7
|
+
type: string | undefined;
|
|
8
|
+
rank: number;
|
|
7
9
|
}
|
|
8
10
|
|
|
9
11
|
export interface SetValue {
|
|
@@ -135,6 +137,13 @@ export const isSetValue = (value: Value) =>
|
|
|
135
137
|
value.kind !== "Function" &&
|
|
136
138
|
value.kind !== "LazyFunction";
|
|
137
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
|
+
|
|
138
147
|
export const isFunctionValue = (value: Value): value is FunctionValue | LazyFunctionValue =>
|
|
139
148
|
typeof value === "object" && (value.kind === "Function" || value.kind === "LazyFunction");
|
|
140
149
|
|
|
@@ -233,8 +242,7 @@ const compareSets = (left: SetValue, right: SetValue) => {
|
|
|
233
242
|
return 0;
|
|
234
243
|
};
|
|
235
244
|
|
|
236
|
-
const getModelValueType = (value: Value) =>
|
|
237
|
-
isModelValue(value) && value.name.length > 2 && value.name[1] === "_" ? value.name[0] : undefined;
|
|
245
|
+
const getModelValueType = (value: Value) => (isModelValue(value) ? value.type : undefined);
|
|
238
246
|
|
|
239
247
|
const checkModelValueTypes = (left: Value, right: Value, isEquality: boolean) => {
|
|
240
248
|
const leftType = getModelValueType(left);
|
|
@@ -264,6 +272,7 @@ const stringOrder = { ranks: Object.create(null) as Record<string, number>, size
|
|
|
264
272
|
export const setStringOrder = (texts: Iterable<string>) => {
|
|
265
273
|
stringOrder.ranks = Object.create(null);
|
|
266
274
|
stringOrder.size = 0;
|
|
275
|
+
modelValues.clear();
|
|
267
276
|
for (const text of texts) rankString(text);
|
|
268
277
|
};
|
|
269
278
|
|
|
@@ -349,11 +358,14 @@ export const compareValues = (left: Value, right: Value): number => {
|
|
|
349
358
|
const otherSize = Math.max(0, right.high - right.low + 1);
|
|
350
359
|
return size !== otherSize || size === 0 ? size - otherSize : left.low - right.low;
|
|
351
360
|
}
|
|
361
|
+
if (leftKind === "ModelValue" && rightKind === "ModelValue") {
|
|
362
|
+
if (left.type !== undefined && right.type !== undefined) {
|
|
363
|
+
checkModelValueTypes(left, right, false);
|
|
364
|
+
}
|
|
365
|
+
return left.rank - right.rank;
|
|
366
|
+
}
|
|
352
367
|
if (leftKind === "ModelValue" || rightKind === "ModelValue") {
|
|
353
368
|
checkModelValueTypes(left, right, false);
|
|
354
|
-
if (leftKind === "ModelValue" && rightKind === "ModelValue") {
|
|
355
|
-
return compareStrings(left.name, right.name);
|
|
356
|
-
}
|
|
357
369
|
return leftKind === "ModelValue" ? -1 : 1;
|
|
358
370
|
}
|
|
359
371
|
if (left === ANY_SET || right === ANY_SET) return compareAny(left === ANY_SET ? right : left);
|
|
@@ -366,7 +378,7 @@ export const compareValues = (left: Value, right: Value): number => {
|
|
|
366
378
|
`Attempted to compare ${describeKind(left)} ${formatValue(left)} with ${describeKind(right)} ${formatValue(right)}`,
|
|
367
379
|
);
|
|
368
380
|
}
|
|
369
|
-
if (isLeftSet) return compareSets(toFiniteSet(left),
|
|
381
|
+
if (isLeftSet) return compareSets(toFiniteSet(left), toComparedSet(right));
|
|
370
382
|
return compareFunctions(toFiniteFunction(left), toFiniteFunction(right));
|
|
371
383
|
}
|
|
372
384
|
if (typeof left === "number" && typeof right === "number") return left - right;
|
|
@@ -382,11 +394,16 @@ export const compareValues = (left: Value, right: Value): number => {
|
|
|
382
394
|
|
|
383
395
|
const modelValues = new Map<string, ModelValue>();
|
|
384
396
|
|
|
397
|
+
// A check ranks every model value's name when it creates the value, and creates its own values.
|
|
385
398
|
export const createModelValue = (name: string): ModelValue => {
|
|
386
|
-
rankString(name);
|
|
387
399
|
const known = modelValues.get(name);
|
|
388
400
|
if (known) return known;
|
|
389
|
-
const value: ModelValue = {
|
|
401
|
+
const value: ModelValue = {
|
|
402
|
+
kind: "ModelValue",
|
|
403
|
+
name,
|
|
404
|
+
type: name.length > 2 && name[1] === "_" ? name[0] : undefined,
|
|
405
|
+
rank: getStringRank(name),
|
|
406
|
+
};
|
|
390
407
|
modelValues.set(name, value);
|
|
391
408
|
return value;
|
|
392
409
|
};
|
|
@@ -555,7 +572,7 @@ const equalsModelValueWith = (modelValue: ModelValue, other: Value) => {
|
|
|
555
572
|
|
|
556
573
|
const compareModelValue = (left: ModelValue, right: Value) => {
|
|
557
574
|
const type = getModelValueType(left);
|
|
558
|
-
if (type === undefined) return isModelValue(right) ?
|
|
575
|
+
if (type === undefined) return isModelValue(right) ? left.rank - right.rank : -1;
|
|
559
576
|
if (!isModelValue(right)) {
|
|
560
577
|
throw new TlaEvaluationError(
|
|
561
578
|
`Attempted to compare the typed model value ${formatValue(left)} and non-model value\n${formatValue(right)}`,
|
|
@@ -567,7 +584,7 @@ const compareModelValue = (left: ModelValue, right: Value) => {
|
|
|
567
584
|
`Attempted to compare the differently-typed model values ${formatValue(left)} and ${formatValue(right)}`,
|
|
568
585
|
);
|
|
569
586
|
}
|
|
570
|
-
return
|
|
587
|
+
return left.rank - right.rank;
|
|
571
588
|
};
|
|
572
589
|
|
|
573
590
|
const equalsModelValue = (left: ModelValue, right: Value) => {
|
|
@@ -854,9 +871,11 @@ export const isEqualLikeTlc = (left: Value, right: Value) => {
|
|
|
854
871
|
}
|
|
855
872
|
};
|
|
856
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.
|
|
857
876
|
const isStrictlySorted = (elements: Value[]) => {
|
|
858
877
|
for (let index = 1; index < elements.length; index++) {
|
|
859
|
-
if (compareValues(elements[index
|
|
878
|
+
if (compareValues(elements[index], elements[index - 1]) <= 0) return false;
|
|
860
879
|
}
|
|
861
880
|
return true;
|
|
862
881
|
};
|
|
@@ -1059,13 +1078,16 @@ const isTlcEnumerable = (value: Value) => isSetValue(value) && !isOverriddenSet(
|
|
|
1059
1078
|
const formatUnion = (value: UnionValue, isStrict = false) =>
|
|
1060
1079
|
`UNION(${formatValue(value.base, isStrict)})`;
|
|
1061
1080
|
|
|
1062
|
-
const getUnionSets = (
|
|
1081
|
+
const getUnionSets = (
|
|
1082
|
+
value: UnionValue,
|
|
1083
|
+
getElements = (set: Value) => toFiniteSet(set).elements,
|
|
1084
|
+
) => {
|
|
1063
1085
|
if (!isTlcEnumerable(value.base)) {
|
|
1064
1086
|
throw new TlaEvaluationError(
|
|
1065
1087
|
`Attempted to enumerate the nonenumerable set:\n${formatUnion(value)}`,
|
|
1066
1088
|
);
|
|
1067
1089
|
}
|
|
1068
|
-
return
|
|
1090
|
+
return getElements(value.base);
|
|
1069
1091
|
};
|
|
1070
1092
|
|
|
1071
1093
|
const getUnionSet = (set: Value, index: number, union: UnionValue) => {
|
|
@@ -1101,7 +1123,7 @@ export const isEnumerable = (value: Value): boolean => {
|
|
|
1101
1123
|
case "ProductSet":
|
|
1102
1124
|
return value.factors.every(isEnumerable) || getEmptiness(value) === true;
|
|
1103
1125
|
case "Union":
|
|
1104
|
-
return isEnumerable(value.base) &&
|
|
1126
|
+
return isEnumerable(value.base) && getStreamedElements(value.base).every(isEnumerable);
|
|
1105
1127
|
case "SetOperation":
|
|
1106
1128
|
if (value.operator === "\\cup") return isEnumerable(value.left) && isEnumerable(value.right);
|
|
1107
1129
|
if (value.operator === "\\cap") return isEnumerable(value.left) || isEnumerable(value.right);
|
|
@@ -1202,9 +1224,7 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1202
1224
|
if (isOverriddenSet(value.base)) getCardinality(value.base);
|
|
1203
1225
|
const base = toFiniteSet(value.base).elements;
|
|
1204
1226
|
if (base.length > MAXIMUM_ENUMERATED_SUBSET_BASE) {
|
|
1205
|
-
throw new TlaEvaluationError(
|
|
1206
|
-
`SUBSET of a set with ${base.length} elements is too large to enumerate`,
|
|
1207
|
-
);
|
|
1227
|
+
throw new TlaEvaluationError(describeOverflow(value));
|
|
1208
1228
|
}
|
|
1209
1229
|
return getSortedSubsets(base);
|
|
1210
1230
|
}
|
|
@@ -1308,6 +1328,7 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1308
1328
|
};
|
|
1309
1329
|
|
|
1310
1330
|
const finiteSets = new WeakMap<ObjectValue, SetValue>();
|
|
1331
|
+
const comparedSets = new WeakMap<ObjectValue, SetValue>();
|
|
1311
1332
|
|
|
1312
1333
|
export const toFiniteSet = (value: Value): SetValue => {
|
|
1313
1334
|
if (typeof value === "object" && value.kind === "Set") return value;
|
|
@@ -1316,6 +1337,13 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1316
1337
|
}
|
|
1317
1338
|
const cached = typeof value === "object" ? finiteSets.get(value) : undefined;
|
|
1318
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) => {
|
|
1319
1347
|
const elements = enumerateSet(value);
|
|
1320
1348
|
const isSorted =
|
|
1321
1349
|
typeof value === "object" &&
|
|
@@ -1326,8 +1354,20 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1326
1354
|
value.kind === "ProductSet" ||
|
|
1327
1355
|
value.kind === "FunctionSet" ||
|
|
1328
1356
|
value.kind === "Subset");
|
|
1329
|
-
|
|
1330
|
-
|
|
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);
|
|
1331
1371
|
return set;
|
|
1332
1372
|
};
|
|
1333
1373
|
|
|
@@ -1335,8 +1375,10 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1335
1375
|
// so an element that two of them share comes up once for each.
|
|
1336
1376
|
export const getStreamedElements = (value: Value): Value[] => {
|
|
1337
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.
|
|
1338
1380
|
if (value.kind === "Union") {
|
|
1339
|
-
return getUnionSets(value).flatMap((set, index) =>
|
|
1381
|
+
return getUnionSets(value, getStreamedElements).flatMap((set, index) =>
|
|
1340
1382
|
getStreamedElements(getUnionSet(set, index, value)),
|
|
1341
1383
|
);
|
|
1342
1384
|
}
|
|
@@ -1667,6 +1709,25 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1667
1709
|
}
|
|
1668
1710
|
};
|
|
1669
1711
|
|
|
1712
|
+
// A function that agrees with a member of a function set outside some entries is a member when
|
|
1713
|
+
// those entries are.
|
|
1714
|
+
export const isUpdatedElementOf = (element: Value, member: Value, set: Value): boolean => {
|
|
1715
|
+
if (element === member) return true;
|
|
1716
|
+
if (
|
|
1717
|
+
typeof element !== "object" ||
|
|
1718
|
+
typeof member !== "object" ||
|
|
1719
|
+
typeof set !== "object" ||
|
|
1720
|
+
element.kind !== "Function" ||
|
|
1721
|
+
member.kind !== "Function" ||
|
|
1722
|
+
set.kind !== "FunctionSet" ||
|
|
1723
|
+
element.domain !== member.domain
|
|
1724
|
+
)
|
|
1725
|
+
return isElementOf(element, set);
|
|
1726
|
+
return element.range.every((item, index) =>
|
|
1727
|
+
isUpdatedElementOf(item, member.range[index], set.range),
|
|
1728
|
+
);
|
|
1729
|
+
};
|
|
1730
|
+
|
|
1670
1731
|
export const toFiniteFunction = (value: Value): FunctionValue => {
|
|
1671
1732
|
if (typeof value === "object" && value.kind === "Function") return value;
|
|
1672
1733
|
if (typeof value === "object" && value.kind === "LazyFunction") {
|
|
@@ -1689,15 +1750,80 @@ export const markLambdaFunction = <FunctionLike extends FunctionValue | LazyFunc
|
|
|
1689
1750
|
return functionValue;
|
|
1690
1751
|
};
|
|
1691
1752
|
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1753
|
+
// TLC applies a tuple, a record and a function over an interval with their own classes, and each
|
|
1754
|
+
// reports a missing argument in its own words.
|
|
1755
|
+
const getFunctionClass = (functionValue: FunctionValue) => {
|
|
1756
|
+
const domain = functionValue.domain;
|
|
1757
|
+
if (domain.length === 0) return lambdaFunctions.has(functionValue) ? "FcnRcdValue" : "TupleValue";
|
|
1758
|
+
if (domain === intervalDomains[domain.length]) return "TupleValue";
|
|
1759
|
+
if (intervalArrays.has(domain)) return "IntervalFcnRcdValue";
|
|
1760
|
+
return domain.every((key) => typeof key === "string") ? "RecordValue" : "FcnRcdValue";
|
|
1761
|
+
};
|
|
1762
|
+
|
|
1763
|
+
const describeNotInDomain = (functionValue: FunctionValue, argument: Value) =>
|
|
1764
|
+
`Attempted to apply function:\n${prettyPrint(formatValue(functionValue))}\nto argument ${prettyPrint(formatValue(argument))}, which is not in the domain of the function.`;
|
|
1765
|
+
|
|
1766
|
+
const describeNonIntegerArgument = (argument: Value) =>
|
|
1767
|
+
`Attempted to apply function with integer domain to the non-integer argument ${prettyPrint(formatValue(argument))}`;
|
|
1768
|
+
|
|
1769
|
+
const describeMissingArgument = (
|
|
1770
|
+
functionValue: FunctionValue | LazyFunctionValue,
|
|
1771
|
+
argument: Value,
|
|
1772
|
+
) => {
|
|
1773
|
+
if (lambdaFunctions.has(functionValue) || functionValue.kind === "LazyFunction")
|
|
1774
|
+
return `In applying the function\n${prettyPrint(formatValue(functionValue))},\nthe first argument is:\n${prettyPrint(formatValue(argument))}\nwhich is not in its domain.\n`;
|
|
1775
|
+
const functionClass = getFunctionClass(functionValue);
|
|
1776
|
+
if (functionClass === "TupleValue")
|
|
1777
|
+
return typeof argument === "number"
|
|
1778
|
+
? `Attempted to access index ${argument} of tuple\n${prettyPrint(formatValue(functionValue))}\nwhich is out of bounds.`
|
|
1779
|
+
: `Attempted to access tuple at a non integral index: ${prettyPrint(formatValue(argument))}`;
|
|
1780
|
+
if (functionClass === "RecordValue")
|
|
1781
|
+
return typeof argument === "string"
|
|
1782
|
+
? `Attempted to access nonexistent field '${argument}' of record\n${prettyPrint(formatValue(functionValue))}`
|
|
1783
|
+
: `Attempted to access record by a non-string argument: ${prettyPrint(formatValue(argument))}`;
|
|
1784
|
+
return functionClass === "IntervalFcnRcdValue" && typeof argument !== "number"
|
|
1785
|
+
? describeNonIntegerArgument(argument)
|
|
1786
|
+
: describeNotInDomain(functionValue, argument);
|
|
1787
|
+
};
|
|
1788
|
+
|
|
1789
|
+
// TLC selects a field of a value that isn't a record from the function the value converts to,
|
|
1790
|
+
// which keeps a tuple's domain as an interval.
|
|
1791
|
+
export const describeMissingField = (functionValue: FunctionValue, field: string) =>
|
|
1792
|
+
getFunctionClass(functionValue) === "FcnRcdValue"
|
|
1793
|
+
? describeNotInDomain(functionValue, field)
|
|
1794
|
+
: describeNonIntegerArgument(field);
|
|
1795
|
+
|
|
1796
|
+
export const isRecordValue = (functionValue: FunctionValue) =>
|
|
1797
|
+
getFunctionClass(functionValue) === "RecordValue";
|
|
1798
|
+
|
|
1799
|
+
// TLC checks the type of an argument to a tuple or a record before it looks the argument up, so
|
|
1800
|
+
// it never compares the argument with a key of another type.
|
|
1801
|
+
const isForeignKey = (domain: Value[], argument: Value) => {
|
|
1802
|
+
const first = domain[0];
|
|
1803
|
+
return typeof first === "number"
|
|
1804
|
+
? typeof argument !== "number" &&
|
|
1805
|
+
(domain === intervalDomains[domain.length] || intervalArrays.has(domain))
|
|
1806
|
+
: typeof first === "string" && typeof argument !== "string";
|
|
1807
|
+
};
|
|
1808
|
+
|
|
1809
|
+
const findKeyIndex = (domain: Value[], argument: Value) => {
|
|
1810
|
+
const first = domain[0];
|
|
1811
|
+
if (typeof argument === "number" && typeof first === "number") {
|
|
1812
|
+
const offset = argument - first;
|
|
1813
|
+
if (domain[offset] === argument) return offset;
|
|
1814
|
+
}
|
|
1815
|
+
if (domain.length <= 8) {
|
|
1816
|
+
for (let index = 0; index < domain.length; index++)
|
|
1817
|
+
if (domain[index] === argument) return index;
|
|
1818
|
+
}
|
|
1819
|
+
return findIndex(domain, argument);
|
|
1820
|
+
};
|
|
1696
1821
|
|
|
1697
1822
|
export const applyFunction = (
|
|
1698
1823
|
functionValue: Value,
|
|
1699
1824
|
argument: Value,
|
|
1700
1825
|
describeMissing?: (functionValue: FunctionValue) => string,
|
|
1826
|
+
describeNonFunction?: (value: Value) => string,
|
|
1701
1827
|
): Value => {
|
|
1702
1828
|
if (typeof functionValue === "object" && functionValue.kind === "LazyFunction") {
|
|
1703
1829
|
if (!isElementOf(argument, functionValue.domain)) {
|
|
@@ -1711,20 +1837,13 @@ export const applyFunction = (
|
|
|
1711
1837
|
}
|
|
1712
1838
|
if (typeof functionValue !== "object" || functionValue.kind !== "Function") {
|
|
1713
1839
|
throw new TlaEvaluationError(
|
|
1714
|
-
|
|
1840
|
+
describeNonFunction?.(functionValue) ??
|
|
1841
|
+
`Attempted to apply ${formatValue(functionValue)}, which is not a function`,
|
|
1715
1842
|
);
|
|
1716
1843
|
}
|
|
1717
|
-
const
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
const offset = argument - first;
|
|
1721
|
-
if (domain[offset] === argument) return functionValue.range[offset];
|
|
1722
|
-
}
|
|
1723
|
-
if (domain.length <= 8) {
|
|
1724
|
-
for (let index = 0; index < domain.length; index++)
|
|
1725
|
-
if (domain[index] === argument) return functionValue.range[index];
|
|
1726
|
-
}
|
|
1727
|
-
const position = findIndex(domain, argument);
|
|
1844
|
+
const position = isForeignKey(functionValue.domain, argument)
|
|
1845
|
+
? -1
|
|
1846
|
+
: findKeyIndex(functionValue.domain, argument);
|
|
1728
1847
|
if (position === -1) {
|
|
1729
1848
|
throw new TlaEvaluationError(
|
|
1730
1849
|
describeMissing?.(functionValue) ?? describeMissingArgument(functionValue, argument),
|
|
@@ -1733,12 +1852,16 @@ export const applyFunction = (
|
|
|
1733
1852
|
return functionValue.range[position];
|
|
1734
1853
|
};
|
|
1735
1854
|
|
|
1855
|
+
export const getDomainInterval = (domain: Value[]) =>
|
|
1856
|
+
isIntervalDomain(domain)
|
|
1857
|
+
? (intervalArrays.get(domain) ?? getIntervalValue(domain.length))
|
|
1858
|
+
: undefined;
|
|
1859
|
+
|
|
1736
1860
|
export const getDomain = (functionValue: Value): Value => {
|
|
1737
1861
|
if (typeof functionValue === "object" && functionValue.kind === "LazyFunction")
|
|
1738
1862
|
return functionValue.domain;
|
|
1739
1863
|
const domain = toFiniteFunction(functionValue).domain;
|
|
1740
|
-
|
|
1741
|
-
return intervalArrays.get(domain) ?? getIntervalValue(domain.length);
|
|
1864
|
+
return getDomainInterval(domain) ?? createSortedSet(domain);
|
|
1742
1865
|
};
|
|
1743
1866
|
|
|
1744
1867
|
export const updateFunction = (
|
|
@@ -1762,7 +1885,7 @@ export const updateFunction = (
|
|
|
1762
1885
|
};
|
|
1763
1886
|
}
|
|
1764
1887
|
const finite = toFiniteFunction(functionValue);
|
|
1765
|
-
const position =
|
|
1888
|
+
const position = findKeyIndex(finite.domain, argument);
|
|
1766
1889
|
if (position === -1) return finite;
|
|
1767
1890
|
const range = [...finite.range];
|
|
1768
1891
|
const previous = finite.range[position];
|
|
@@ -2065,8 +2188,11 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
|
|
|
2065
2188
|
return `[${parameters} |-> <expression ${source.body}>]`;
|
|
2066
2189
|
}
|
|
2067
2190
|
const functionValue = toFiniteFunction(value);
|
|
2191
|
+
// TLC normalizes a lambda's domain to print it, but evaluates its body afterward.
|
|
2192
|
+
const formatRange = (inner: Value) =>
|
|
2193
|
+
formatValue(inner, isStrict, isNormalized && value.kind === "Function");
|
|
2068
2194
|
if (functionValue.domain.every((key, index) => key === index + 1)) {
|
|
2069
|
-
return `<<${functionValue.range.map(
|
|
2195
|
+
return `<<${functionValue.range.map(formatRange).join(", ")}>>`;
|
|
2070
2196
|
}
|
|
2071
2197
|
if (functionValue.domain.every((key) => typeof key === "string" && isFieldName(key))) {
|
|
2072
2198
|
const isSorted =
|
|
@@ -2074,9 +2200,20 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
|
|
|
2074
2200
|
const pairs = isSorted
|
|
2075
2201
|
? functionValue.domain.map((key, index) => [key, functionValue.range[index]])
|
|
2076
2202
|
: getWrittenPairs(functionValue);
|
|
2077
|
-
return `[${pairs.map((pair) => `${String(pair[0])} |-> ${
|
|
2203
|
+
return `[${pairs.map((pair) => `${String(pair[0])} |-> ${formatRange(pair[1])}`).join(", ")}]`;
|
|
2078
2204
|
}
|
|
2079
|
-
return `(${functionValue.domain.map((key, index) => `${format(key)} :> ${
|
|
2205
|
+
return `(${functionValue.domain.map((key, index) => `${format(key)} :> ${formatRange(functionValue.range[index])}`).join(" @@ ")})`;
|
|
2080
2206
|
}
|
|
2081
2207
|
}
|
|
2082
2208
|
};
|
|
2209
|
+
|
|
2210
|
+
// TLC's TLCState.toString: a line per variable, as a conjunction when there are several.
|
|
2211
|
+
export const formatState = (state: Record<string, Value>) => {
|
|
2212
|
+
const entries = Object.entries(state);
|
|
2213
|
+
return entries
|
|
2214
|
+
.map(
|
|
2215
|
+
([name, value]) =>
|
|
2216
|
+
`${entries.length === 1 ? "" : "/\\ "}${name} = ${prettyPrint(formatValue(value))}\n`,
|
|
2217
|
+
)
|
|
2218
|
+
.join("");
|
|
2219
|
+
};
|