tla-ts 0.0.3-dev.07eab97 → 0.0.3-dev.0ad0249
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/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +160 -46
- package/dist/index.d.ts +160 -46
- 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 +1351 -22
- package/dist/run-cli.js +1351 -22
- package/dist/transfer.cjs +83 -33
- package/dist/transfer.js +83 -33
- package/package.json +4 -1
- package/src/ast.ts +42 -5
- package/src/bags.ts +4 -7
- package/src/builtins.ts +29 -17
- package/src/bundled-modules.ts +3 -3
- package/src/canonical.ts +4 -3
- package/src/check.ts +1296 -429
- package/src/cli.ts +8 -1
- package/src/codegen.ts +68 -21
- package/src/config.ts +225 -206
- package/src/environment.ts +388 -115
- package/src/errors.ts +155 -18
- package/src/evaluate.ts +713 -259
- package/src/exit-codes.ts +6 -24
- package/src/fingerprints.ts +1 -2
- package/src/index.ts +1 -0
- package/src/instances.ts +3 -1
- package/src/java-overrides.ts +104 -25
- package/src/levels.ts +37 -27
- package/src/lexer.ts +14 -1
- package/src/live-check.ts +204 -148
- package/src/liveness.ts +191 -122
- package/src/obligation-expression.ts +2 -2
- package/src/obligation-source.ts +2 -0
- package/src/parallel-worker.ts +6 -1
- package/src/parallel.ts +57 -6
- package/src/parser.ts +369 -115
- package/src/pcal-ast.ts +770 -0
- package/src/pcal-parse.ts +1523 -0
- package/src/pcal-tokenize.ts +516 -0
- package/src/pcal-translate.ts +1230 -0
- package/src/pcal-validate.ts +454 -0
- package/src/randomization.ts +24 -7
- package/src/resolve.ts +1332 -453
- package/src/run-cli.ts +1161 -196
- package/src/runtime.ts +16 -0
- package/src/sany-builtins.ts +147 -0
- package/src/sany-generator.ts +3112 -0
- package/src/sany-levels.ts +1208 -0
- package/src/sany-lexer.ts +545 -0
- package/src/sany-linter.ts +122 -0
- package/src/sany-modules.ts +1398 -0
- package/src/sany-nodes.ts +463 -0
- package/src/sany-parse-error.ts +2161 -0
- package/src/sany-report.ts +354 -0
- package/src/sany-selectors.ts +738 -0
- package/src/sany-source.ts +248 -0
- package/src/sany-xml.ts +1017 -0
- package/src/spec-description.ts +25 -5
- package/src/standard-modules.ts +172 -129
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +29 -6
- package/src/subexpressions.ts +221 -161
- package/src/successors.ts +141 -173
- package/src/symmetry.ts +168 -50
- package/src/tableau.ts +3 -1
- package/src/tlc-ext.ts +13 -17
- package/src/tlc-messages.ts +329 -0
- package/src/tlc-module.ts +49 -21
- package/src/tlc-modules.ts +0 -4
- package/src/tlc-order.ts +55 -9
- package/src/tlc-random.ts +28 -3
- package/src/tlc-replay.ts +655 -0
- package/src/tlc-version.ts +18 -0
- package/src/transfer.ts +15 -3
- package/src/values.ts +664 -317
package/src/values.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { TlaEvaluationError, TlaModuleError } from "./errors";
|
|
2
2
|
import { prettyPrint } from "./pretty-print";
|
|
3
|
-
import {
|
|
3
|
+
import type { TlcVersion } from "./tlc-version";
|
|
4
4
|
|
|
5
5
|
export interface ModelValue {
|
|
6
6
|
kind: "ModelValue";
|
|
@@ -30,6 +30,7 @@ export interface FunctionValue {
|
|
|
30
30
|
hash: number;
|
|
31
31
|
fingerprint: number;
|
|
32
32
|
writtenKeys: Value[] | undefined;
|
|
33
|
+
functionClass: FunctionClass | undefined;
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
export interface IntervalValue {
|
|
@@ -77,10 +78,17 @@ export interface SetOperationValue {
|
|
|
77
78
|
right: Value;
|
|
78
79
|
}
|
|
79
80
|
|
|
81
|
+
// TLC prints a set filter as its bound names and the location of its predicate.
|
|
82
|
+
interface FilteredSetSource {
|
|
83
|
+
names: string;
|
|
84
|
+
predicate: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
80
87
|
export interface FilteredSetValue {
|
|
81
88
|
kind: "FilteredSet";
|
|
82
89
|
base: Value;
|
|
83
90
|
test: (element: Value) => boolean;
|
|
91
|
+
source?: FilteredSetSource;
|
|
84
92
|
}
|
|
85
93
|
|
|
86
94
|
// UNION S when some element of S isn't a set enumeration. TLC enumerates it only when it has to.
|
|
@@ -104,6 +112,7 @@ export interface LazyFunctionValue {
|
|
|
104
112
|
domain: Value;
|
|
105
113
|
apply: (argument: Value) => Value;
|
|
106
114
|
source?: LazyFunctionSource;
|
|
115
|
+
functionClass?: FunctionClass;
|
|
107
116
|
}
|
|
108
117
|
|
|
109
118
|
export type Value =
|
|
@@ -138,10 +147,9 @@ export const isSetValue = (value: Value) =>
|
|
|
138
147
|
value.kind !== "Function" &&
|
|
139
148
|
value.kind !== "LazyFunction";
|
|
140
149
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
export const isReducible = (value: Value): value is ReducibleSet =>
|
|
150
|
+
// TLC's Reducible values, SetEnumValue and IntervalValue, which combine with another set element by
|
|
151
|
+
// element.
|
|
152
|
+
export const isReducibleSet = (value: Value): value is SetValue | MixedSetValue | IntervalValue =>
|
|
145
153
|
typeof value === "object" &&
|
|
146
154
|
(value.kind === "Set" || value.kind === "MixedSet" || value.kind === "Interval");
|
|
147
155
|
|
|
@@ -183,7 +191,7 @@ export const getWrittenPairs = (functionValue: FunctionValue): [Value, Value][]
|
|
|
183
191
|
};
|
|
184
192
|
|
|
185
193
|
export const createWrittenRecord = (names: Value[], range: Value[], writtenNames: Value[]) =>
|
|
186
|
-
setWrittenOrder(
|
|
194
|
+
setWrittenOrder(createSortedRecord(names, range), writtenNames);
|
|
187
195
|
|
|
188
196
|
// TLC sorts a value it compares even with itself, and every value inside it, so a value it has
|
|
189
197
|
// hashed or fingerprinted is already sorted.
|
|
@@ -268,7 +276,10 @@ const checkModelValueTypes = (left: Value, right: Value, isEquality: boolean) =>
|
|
|
268
276
|
|
|
269
277
|
// TLC orders strings and model value names by when it first interned the text, so every
|
|
270
278
|
// check sets the order of its spec's strings before it builds any value.
|
|
271
|
-
const stringOrder
|
|
279
|
+
const stringOrder: { ranks: Record<string, number>; size: number } = {
|
|
280
|
+
ranks: Object.create(null),
|
|
281
|
+
size: 0,
|
|
282
|
+
};
|
|
272
283
|
|
|
273
284
|
export const setStringOrder = (texts: Iterable<string>) => {
|
|
274
285
|
stringOrder.ranks = Object.create(null);
|
|
@@ -277,6 +288,14 @@ export const setStringOrder = (texts: Iterable<string>) => {
|
|
|
277
288
|
for (const text of texts) rankString(text);
|
|
278
289
|
};
|
|
279
290
|
|
|
291
|
+
// TLC 1.7.4 prints an empty function that isn't a tuple with a space between its brackets, and
|
|
292
|
+
// looks a key up in a function differently, so every check also sets the version its values follow.
|
|
293
|
+
const valueVersion = { is174: false };
|
|
294
|
+
|
|
295
|
+
export const setValueVersion = (version: TlcVersion | undefined) => {
|
|
296
|
+
valueVersion.is174 = version === "1.7.4";
|
|
297
|
+
};
|
|
298
|
+
|
|
280
299
|
export const rankString = (text: string) => {
|
|
281
300
|
if (stringOrder.ranks[text] === undefined) stringOrder.ranks[text] = stringOrder.size++;
|
|
282
301
|
return text;
|
|
@@ -379,7 +398,7 @@ export const compareValues = (left: Value, right: Value): number => {
|
|
|
379
398
|
`Attempted to compare ${describeKind(left)} ${formatValue(left)} with ${describeKind(right)} ${formatValue(right)}`,
|
|
380
399
|
);
|
|
381
400
|
}
|
|
382
|
-
if (isLeftSet) return compareSets(toFiniteSet(left),
|
|
401
|
+
if (isLeftSet) return compareSets(toFiniteSet(left), toFiniteSet(right));
|
|
383
402
|
return compareFunctions(toFiniteFunction(left), toFiniteFunction(right));
|
|
384
403
|
}
|
|
385
404
|
if (typeof left === "number" && typeof right === "number") return left - right;
|
|
@@ -423,9 +442,85 @@ export const createSortedFunction = (domain: Value[], range: Value[]): FunctionV
|
|
|
423
442
|
hash: 0,
|
|
424
443
|
fingerprint: 0,
|
|
425
444
|
writtenKeys: undefined,
|
|
445
|
+
functionClass: undefined,
|
|
426
446
|
});
|
|
427
447
|
|
|
448
|
+
// TLC words a failed function application by the Java class of the function: FcnLambdaValue for
|
|
449
|
+
// [x \in S |-> e], TupleValue for <<...>> and what the sequence operators build, RecordValue for
|
|
450
|
+
// [a |-> e], and FcnRcdValue for any other function.
|
|
451
|
+
export type FunctionClass = "lambda" | "tuple" | "record";
|
|
452
|
+
|
|
453
|
+
// Whether a lambda exists for markFingerprinted to find, so that a spec without one skips the walk.
|
|
454
|
+
const lambdaPresence = { isPresent: false };
|
|
455
|
+
|
|
456
|
+
export const setFunctionClass = <FunctionLike extends FunctionValue | LazyFunctionValue>(
|
|
457
|
+
functionValue: FunctionLike,
|
|
458
|
+
functionClass: FunctionClass | undefined,
|
|
459
|
+
) => {
|
|
460
|
+
if (functionClass === "lambda") lambdaPresence.isPresent = true;
|
|
461
|
+
if (functionClass !== undefined) functionValue.functionClass = functionClass;
|
|
462
|
+
return functionValue;
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
export const getFunctionClass = (value: Value) =>
|
|
466
|
+
typeof value === "object" && (value.kind === "Function" || value.kind === "LazyFunction")
|
|
467
|
+
? value.functionClass
|
|
468
|
+
: undefined;
|
|
469
|
+
|
|
470
|
+
export const markLambdaFunction = <FunctionLike extends FunctionValue | LazyFunctionValue>(
|
|
471
|
+
functionValue: FunctionLike,
|
|
472
|
+
) => setFunctionClass(functionValue, "lambda");
|
|
473
|
+
|
|
474
|
+
const fingerprintedValues = new WeakSet<ObjectValue>();
|
|
475
|
+
|
|
476
|
+
// The lazy sets TLC has cached the elements of. TLC caches a set as the receiver of compareTo,
|
|
477
|
+
// equals, size, and fingerPrint, not as their argument, and enumerates the sets an uncached one
|
|
478
|
+
// combines, duplicates and all.
|
|
479
|
+
const cachedSets = new WeakSet<ObjectValue>();
|
|
480
|
+
|
|
481
|
+
// Whether the spec has built a UNION or \cup set, the sets that stream duplicates, so that a spec
|
|
482
|
+
// without one skips tracking what TLC caches.
|
|
483
|
+
const streamedSetPresence = { isPresent: false };
|
|
484
|
+
|
|
485
|
+
export const markStreamedSet = <Streamed extends UnionValue | SetOperationValue>(
|
|
486
|
+
value: Streamed,
|
|
487
|
+
) => {
|
|
488
|
+
streamedSetPresence.isPresent = true;
|
|
489
|
+
return value;
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
const cacheSet = (value: Value) => {
|
|
493
|
+
if (streamedSetPresence.isPresent && typeof value === "object") cachedSets.add(value);
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
// FcnLambdaValue#fingerPrint caches the FcnRcdValue the function enumerates to, which applies it
|
|
497
|
+
// from then on, so a function TLC has fingerprinted is no longer a lambda.
|
|
498
|
+
export const markFingerprinted = (value: Value): void => {
|
|
499
|
+
if (
|
|
500
|
+
(!lambdaPresence.isPresent && !streamedSetPresence.isPresent) ||
|
|
501
|
+
typeof value !== "object" ||
|
|
502
|
+
fingerprintedValues.has(value)
|
|
503
|
+
) {
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
fingerprintedValues.add(value);
|
|
507
|
+
if (value.kind === "Function") {
|
|
508
|
+
if (value.functionClass === "lambda") value.functionClass = undefined;
|
|
509
|
+
value.domain.forEach(markFingerprinted);
|
|
510
|
+
value.range.forEach(markFingerprinted);
|
|
511
|
+
} else if (value.kind === "Set" || value.kind === "MixedSet") {
|
|
512
|
+
value.elements.forEach(markFingerprinted);
|
|
513
|
+
} else if (isSetValue(value) && isEnumerable(value)) {
|
|
514
|
+
cacheSet(value);
|
|
515
|
+
toFiniteSet(value).elements.forEach(markFingerprinted);
|
|
516
|
+
}
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
export const createSortedRecord = (names: Value[], range: Value[]) =>
|
|
520
|
+
setFunctionClass(createSortedFunction(names, range), "record");
|
|
521
|
+
|
|
428
522
|
export const valuesEqual = (left: Value, right: Value): boolean => {
|
|
523
|
+
cacheSet(left);
|
|
429
524
|
if (left === right) {
|
|
430
525
|
if (writtenOrderUse.isUsed) forgetWrittenOrder(left);
|
|
431
526
|
return left !== ANY_SET || compareAny(left);
|
|
@@ -499,28 +594,36 @@ export const valuesEqual = (left: Value, right: Value): boolean => {
|
|
|
499
594
|
return compareValues(left, right) === 0;
|
|
500
595
|
};
|
|
501
596
|
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
(
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
(
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
597
|
+
// TLC prints and words errors about an equal function by its Java class, so a value can stand for
|
|
598
|
+
// another in a state only if each function in it has the same class.
|
|
599
|
+
const createSameValueCheck = (isClassSensitive: boolean) => {
|
|
600
|
+
const areSameValues = (left: Value[], right: Value[]) =>
|
|
601
|
+
left === right ||
|
|
602
|
+
(left.length === right.length && left.every((element, index) => isSame(element, right[index])));
|
|
603
|
+
const isSame = (left: Value, right: Value): boolean => {
|
|
604
|
+
if (left === right) return true;
|
|
605
|
+
if (typeof left !== "object" || typeof right !== "object") return false;
|
|
606
|
+
if (left.kind === "Set" && right.kind === "Set")
|
|
607
|
+
return (
|
|
608
|
+
(left.hash === 0 || right.hash === 0 || left.hash === right.hash) &&
|
|
609
|
+
areSameValues(left.elements, right.elements)
|
|
610
|
+
);
|
|
611
|
+
if (left.kind === "Function" && right.kind === "Function")
|
|
612
|
+
return (
|
|
613
|
+
(!isClassSensitive || left.functionClass === right.functionClass) &&
|
|
614
|
+
(left.hash === 0 || right.hash === 0 || left.hash === right.hash) &&
|
|
615
|
+
areSameValues(left.domain, right.domain) &&
|
|
616
|
+
areSameValues(left.range, right.range)
|
|
617
|
+
);
|
|
618
|
+
return getValueKey(left) === getValueKey(right);
|
|
619
|
+
};
|
|
620
|
+
return isSame;
|
|
522
621
|
};
|
|
523
622
|
|
|
623
|
+
export const isSameValue = createSameValueCheck(false);
|
|
624
|
+
|
|
625
|
+
export const isInterchangeableValue = createSameValueCheck(true);
|
|
626
|
+
|
|
524
627
|
// The comparisons above fail with their own messages. These follow the compareTo and equals
|
|
525
628
|
// methods of TLC's Java value classes, which word a failure by the class of the left operand, so a
|
|
526
629
|
// failed comparison runs again through them to fail as TLC does.
|
|
@@ -676,7 +779,7 @@ const compareFunctionLikeTlc = (left: FunctionValue | LazyFunctionValue, right:
|
|
|
676
779
|
compareElementsLikeTlc(functionValue.range, tuple)
|
|
677
780
|
);
|
|
678
781
|
}
|
|
679
|
-
if (left
|
|
782
|
+
if (getFunctionClass(left) === "record") {
|
|
680
783
|
const record = toTlcRecord(right);
|
|
681
784
|
if (!record) {
|
|
682
785
|
if (isModelValue(right)) return compareModelValueWith(right, left);
|
|
@@ -734,17 +837,16 @@ const compareSetLikeTlc = (left: ObjectValue, right: Value): number => {
|
|
|
734
837
|
);
|
|
735
838
|
};
|
|
736
839
|
|
|
737
|
-
const compareLikeTlc = (left: Value, right: Value): number =>
|
|
738
|
-
|
|
739
|
-
if (typeof left
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
});
|
|
840
|
+
const compareLikeTlc = (left: Value, right: Value): number => {
|
|
841
|
+
if (typeof left !== "object") {
|
|
842
|
+
if (typeof right === typeof left) return compareValues(left, right);
|
|
843
|
+
if (isModelValue(right)) return compareModelValueWith(right, left);
|
|
844
|
+
throw new TlaEvaluationError(describeScalarMismatch(left, right, false));
|
|
845
|
+
}
|
|
846
|
+
if (left.kind === "ModelValue") return compareModelValue(left, right);
|
|
847
|
+
if (isFunctionValue(left)) return compareFunctionLikeTlc(left, right);
|
|
848
|
+
return compareSetLikeTlc(left, right);
|
|
849
|
+
};
|
|
748
850
|
|
|
749
851
|
const equalsElementsLikeTlc = (left: Value[], right: Value[]) =>
|
|
750
852
|
left.every((element, index) => equalsLikeTlc(element, right[index]));
|
|
@@ -795,7 +897,7 @@ const equalsFunctionLikeTlc = (left: FunctionValue | LazyFunctionValue, right: V
|
|
|
795
897
|
equalsElementsLikeTlc(functionValue.range, tuple)
|
|
796
898
|
);
|
|
797
899
|
}
|
|
798
|
-
if (left
|
|
900
|
+
if (getFunctionClass(left) === "record") {
|
|
799
901
|
const record = toTlcRecord(right);
|
|
800
902
|
if (!record) {
|
|
801
903
|
if (isModelValue(right)) return equalsModelValueWith(right, left);
|
|
@@ -832,58 +934,90 @@ const equalsSetLikeTlc = (left: ObjectValue, right: Value): boolean => {
|
|
|
832
934
|
);
|
|
833
935
|
};
|
|
834
936
|
|
|
835
|
-
const equalsLikeTlc = (left: Value, right: Value): boolean =>
|
|
836
|
-
|
|
837
|
-
if (typeof
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
});
|
|
937
|
+
const equalsLikeTlc = (left: Value, right: Value): boolean => {
|
|
938
|
+
if (typeof left !== "object") {
|
|
939
|
+
if (typeof right === typeof left) return left === right;
|
|
940
|
+
if (isModelValue(right)) return equalsModelValueWith(right, left);
|
|
941
|
+
throw new TlaEvaluationError(describeScalarMismatch(left, right, true));
|
|
942
|
+
}
|
|
943
|
+
if (left.kind === "ModelValue") return equalsModelValue(left, right);
|
|
944
|
+
if (isFunctionValue(left)) return equalsFunctionLikeTlc(left, right);
|
|
945
|
+
return equalsSetLikeTlc(left, right);
|
|
946
|
+
};
|
|
846
947
|
|
|
847
948
|
// TLC's ValueVec.sort, which inserts each element into the sorted prefix by binary search.
|
|
848
949
|
const sortLikeTlc = (elements: Value[]) => {
|
|
849
950
|
const sorted: Value[] = [];
|
|
850
951
|
for (const element of elements) {
|
|
851
|
-
const
|
|
852
|
-
|
|
853
|
-
const middle = bounds.low + ((bounds.high - bounds.low) >> 1);
|
|
854
|
-
bounds.comparison = compareLikeTlc(element, sorted[middle]);
|
|
855
|
-
if (bounds.comparison === 0) break;
|
|
856
|
-
if (bounds.comparison < 0) bounds.high = middle;
|
|
857
|
-
else bounds.low = middle + 1;
|
|
858
|
-
}
|
|
859
|
-
if (bounds.comparison !== 0) sorted.splice(bounds.low, 0, element);
|
|
952
|
+
const found = searchLikeTlc(element, sorted);
|
|
953
|
+
if (!found.isFound) sorted.splice(found.index, 0, element);
|
|
860
954
|
}
|
|
861
955
|
return sorted;
|
|
862
956
|
};
|
|
863
957
|
|
|
864
|
-
//
|
|
958
|
+
// Calls a method of a value as TLC does, whose failure the value heads when it has a source.
|
|
959
|
+
const callTracedMethod = <Result>(receiver: Value, method: () => Result) => {
|
|
960
|
+
const sets = setTrace.sets;
|
|
961
|
+
if (sets === undefined) return method();
|
|
962
|
+
sets.push(receiver);
|
|
963
|
+
const result = method();
|
|
964
|
+
sets.pop();
|
|
965
|
+
return result;
|
|
966
|
+
};
|
|
967
|
+
|
|
968
|
+
// ValueVec.search on sorted elements: a binary search with compareTo, which ends where the element
|
|
969
|
+
// is or would go.
|
|
970
|
+
const searchLikeTlc = (element: Value, sorted: Value[]) => {
|
|
971
|
+
const bounds = { low: 0, high: sorted.length };
|
|
972
|
+
while (bounds.low < bounds.high) {
|
|
973
|
+
const middle = bounds.low + ((bounds.high - bounds.low) >> 1);
|
|
974
|
+
const comparison = callTracedMethod(element, () => compareLikeTlc(element, sorted[middle]));
|
|
975
|
+
if (comparison === 0) return { index: middle, isFound: true };
|
|
976
|
+
if (comparison < 0) bounds.high = middle;
|
|
977
|
+
else bounds.low = middle + 1;
|
|
978
|
+
}
|
|
979
|
+
return { index: bounds.low, isFound: false };
|
|
980
|
+
};
|
|
981
|
+
|
|
982
|
+
// TLC checks membership in a set enumeration it hasn't sorted element by element, with the equals
|
|
983
|
+
// method of each member.
|
|
865
984
|
const isElementOfLikeTlc = (element: Value, elements: Value[]) =>
|
|
866
|
-
elements.some((member) => equalsLikeTlc(member, element));
|
|
985
|
+
elements.some((member) => callTracedMethod(member, () => equalsLikeTlc(member, element)));
|
|
867
986
|
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
987
|
+
// The sets TLC has normalized, whose membership ValueVec.search checks by binary search with
|
|
988
|
+
// compareTo instead of element by element.
|
|
989
|
+
const normalizedSets = new WeakSet<SetValue>();
|
|
990
|
+
|
|
991
|
+
// IValue.deepNormalize, which TLC applies to the constants of the config and the constant
|
|
992
|
+
// definitions it computes as it starts.
|
|
993
|
+
export const markNormalized = (value: Value): void => {
|
|
994
|
+
if (typeof value !== "object") return;
|
|
995
|
+
if (value.kind === "Set") {
|
|
996
|
+
normalizedSets.add(value);
|
|
997
|
+
value.elements.forEach(markNormalized);
|
|
998
|
+
}
|
|
999
|
+
if (value.kind === "Function") {
|
|
1000
|
+
value.domain.forEach(markNormalized);
|
|
1001
|
+
value.range.forEach(markNormalized);
|
|
874
1002
|
}
|
|
875
1003
|
};
|
|
876
1004
|
|
|
877
|
-
|
|
878
|
-
|
|
1005
|
+
export const isEqualLikeTlc = (left: Value, right: Value) =>
|
|
1006
|
+
computeLikeTlcOnFailure(
|
|
1007
|
+
() => valuesEqual(left, right),
|
|
1008
|
+
() => equalsLikeTlc(left, right),
|
|
1009
|
+
);
|
|
1010
|
+
|
|
879
1011
|
const isStrictlySorted = (elements: Value[]) => {
|
|
880
1012
|
for (let index = 1; index < elements.length; index++) {
|
|
881
|
-
if (compareValues(elements[index], elements[index
|
|
1013
|
+
if (compareValues(elements[index - 1], elements[index]) >= 0) return false;
|
|
882
1014
|
}
|
|
883
1015
|
return true;
|
|
884
1016
|
};
|
|
885
1017
|
|
|
886
1018
|
export const createSet = (elements: Value[]): SetValue => {
|
|
1019
|
+
// ValueVec.sort compares each element after the first with those before it.
|
|
1020
|
+
for (let index = 1; index < elements.length; index++) cacheSet(elements[index]);
|
|
887
1021
|
if (elements.length < 2 || isStrictlySorted(elements)) return createSortedSet(elements);
|
|
888
1022
|
const sorted = [...elements].sort(compareValues);
|
|
889
1023
|
const unique = [sorted[0]];
|
|
@@ -895,14 +1029,11 @@ export const createSet = (elements: Value[]): SetValue => {
|
|
|
895
1029
|
|
|
896
1030
|
// TLC builds a set enumeration without sorting it, so one whose elements it can't compare still
|
|
897
1031
|
// evaluates.
|
|
898
|
-
export const createEnumeratedSet = (elements: Value[]): SetValue | MixedSetValue =>
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
return { kind: "MixedSet", elements };
|
|
904
|
-
}
|
|
905
|
-
};
|
|
1032
|
+
export const createEnumeratedSet = (elements: Value[]): SetValue | MixedSetValue =>
|
|
1033
|
+
computeLikeTlcOnFailure<SetValue | MixedSetValue>(
|
|
1034
|
+
() => createSet(elements),
|
|
1035
|
+
() => ({ kind: "MixedSet", elements }),
|
|
1036
|
+
);
|
|
906
1037
|
|
|
907
1038
|
export const EMPTY_SET = createSortedSet([]);
|
|
908
1039
|
|
|
@@ -981,12 +1112,15 @@ export const unionSorted = (left: SetValue, right: SetValue): SetValue => {
|
|
|
981
1112
|
}
|
|
982
1113
|
if (added.length === 0) return left;
|
|
983
1114
|
const union = createSortedSet(merged);
|
|
984
|
-
if (left.hash !== 0) {
|
|
1115
|
+
if (left.hash !== 0 && left.hash !== UNFINGERPRINTABLE_HASH) {
|
|
985
1116
|
let hash = left.hash;
|
|
986
|
-
|
|
987
|
-
|
|
1117
|
+
let index = 0;
|
|
1118
|
+
for (; index < added.length; index++) {
|
|
1119
|
+
const elementHash = getValueHash(added[index]);
|
|
1120
|
+
if (elementHash === UNFINGERPRINTABLE_HASH) break;
|
|
1121
|
+
hash = (hash + getElementContribution(elementHash)) | 0;
|
|
988
1122
|
}
|
|
989
|
-
union.hash = hash;
|
|
1123
|
+
if (index === added.length) union.hash = hash;
|
|
990
1124
|
}
|
|
991
1125
|
if (left.fingerprint !== 0) {
|
|
992
1126
|
let fingerprint = left.fingerprint;
|
|
@@ -1071,12 +1205,13 @@ const getIntervalValue = (high: number): IntervalValue => {
|
|
|
1071
1205
|
};
|
|
1072
1206
|
|
|
1073
1207
|
export const createTuple = (elements: Value[]): FunctionValue =>
|
|
1074
|
-
createSortedFunction(getIntervalDomain(elements.length), elements);
|
|
1208
|
+
setFunctionClass(createSortedFunction(getIntervalDomain(elements.length), elements), "tuple");
|
|
1075
1209
|
|
|
1076
|
-
export const createRecord = (fields: [string, Value][]): FunctionValue =>
|
|
1210
|
+
export const createRecord = (fields: [string, Value][]): FunctionValue =>
|
|
1211
|
+
setFunctionClass(createFunction(fields), "record");
|
|
1077
1212
|
|
|
1078
1213
|
// TLC's Enumerable classes: every set except the ones it implements in Java.
|
|
1079
|
-
const isTlcEnumerable = (value: Value) => isSetValue(value) && !isOverriddenSet(value);
|
|
1214
|
+
export const isTlcEnumerable = (value: Value) => isSetValue(value) && !isOverriddenSet(value);
|
|
1080
1215
|
|
|
1081
1216
|
const formatUnion = (value: UnionValue, isStrict = false) =>
|
|
1082
1217
|
`UNION(${formatValue(value.base, isStrict)})`;
|
|
@@ -1245,7 +1380,30 @@ const getSortedSubsets = (base: Value[]) => {
|
|
|
1245
1380
|
return subsets;
|
|
1246
1381
|
};
|
|
1247
1382
|
|
|
1248
|
-
|
|
1383
|
+
// The Enumerator of a set of records or tuples enumerates its sets in turn and stops at the first
|
|
1384
|
+
// empty one, which leaves the product empty. Later versions of TLC decide that the set is empty
|
|
1385
|
+
// before they enumerate it.
|
|
1386
|
+
const getComponentElements = (
|
|
1387
|
+
sets: Value[],
|
|
1388
|
+
getElements: (set: Value) => Value[],
|
|
1389
|
+
describeUnenumerable: (set: Value, index: number) => string,
|
|
1390
|
+
) => {
|
|
1391
|
+
const lists: Value[][] = [];
|
|
1392
|
+
for (let index = 0; index < sets.length; index++) {
|
|
1393
|
+
const set = sets[index];
|
|
1394
|
+
if (isOverriddenSet(set)) throw new TlaEvaluationError(describeUnenumerable(set, index));
|
|
1395
|
+
const elements = getElements(set);
|
|
1396
|
+
if (elements.length === 0) return [[]];
|
|
1397
|
+
lists.push(elements);
|
|
1398
|
+
}
|
|
1399
|
+
return lists;
|
|
1400
|
+
};
|
|
1401
|
+
|
|
1402
|
+
// getElements enumerates the sets a set of functions, records, or tuples combines.
|
|
1403
|
+
const enumerateSet = (
|
|
1404
|
+
value: Value,
|
|
1405
|
+
getElements = (set: Value) => toFiniteSet(set).elements,
|
|
1406
|
+
): Value[] => {
|
|
1249
1407
|
if (typeof value !== "object")
|
|
1250
1408
|
throw new TlaEvaluationError(`Expected a set but found ${formatValue(value)}`);
|
|
1251
1409
|
switch (value.kind) {
|
|
@@ -1259,7 +1417,14 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1259
1417
|
return elements;
|
|
1260
1418
|
}
|
|
1261
1419
|
case "Subset": {
|
|
1262
|
-
|
|
1420
|
+
// SubsetValue.toSetEnum sizes the set before it enumerates it, and enumerates only the empty
|
|
1421
|
+
// set of a value of size 0, a function with an empty domain among them.
|
|
1422
|
+
if (getCardinality(value.base) === 0) return getSortedSubsets([]);
|
|
1423
|
+
if (!isSetValue(value.base)) {
|
|
1424
|
+
throw new TlaEvaluationError(
|
|
1425
|
+
`Attempted to compute the value of an expression of form\nSUBSET S, but S is a non-enumerable value:\n${prettyPrint(formatValue(value.base))}`,
|
|
1426
|
+
);
|
|
1427
|
+
}
|
|
1263
1428
|
const base = toFiniteSet(value.base).elements;
|
|
1264
1429
|
if (base.length > MAXIMUM_ENUMERATED_SUBSET_BASE) {
|
|
1265
1430
|
throw new TlaEvaluationError(describeOverflow(value));
|
|
@@ -1267,51 +1432,48 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1267
1432
|
return getSortedSubsets(base);
|
|
1268
1433
|
}
|
|
1269
1434
|
case "FunctionSet": {
|
|
1270
|
-
if (isEmptySet(value)) return [];
|
|
1435
|
+
if (!valueVersion.is174 && isEmptySet(value)) return [];
|
|
1271
1436
|
if (isOverriddenSet(value.domain)) {
|
|
1272
1437
|
throw new TlaEvaluationError(
|
|
1273
1438
|
`Attempted to enumerate a set of the form [D -> R],but the domain D:\n${formatValue(value.domain)}\ncannot be enumerated.`,
|
|
1274
1439
|
);
|
|
1275
1440
|
}
|
|
1276
1441
|
const domain = getFunctionDomain(value.domain);
|
|
1277
|
-
|
|
1442
|
+
// TLC 1.7.4 requires an enumerable range even of an empty domain.
|
|
1443
|
+
if ((domain.length > 0 || valueVersion.is174) && isOverriddenSet(value.range)) {
|
|
1278
1444
|
throw new TlaEvaluationError(
|
|
1279
1445
|
`Attempted to enumerate a set of the form [D -> R],but the range R:\n${formatValue(value.range)}\ncannot be enumerated.`,
|
|
1280
1446
|
);
|
|
1281
1447
|
}
|
|
1282
|
-
const range = domain.length === 0 ? [] :
|
|
1448
|
+
const range = domain.length === 0 ? [] : getElements(value.range);
|
|
1283
1449
|
return getCartesianProduct(domain.map(() => range)).map((combination): Value =>
|
|
1284
1450
|
createSortedFunction(domain, combination),
|
|
1285
1451
|
);
|
|
1286
1452
|
}
|
|
1287
1453
|
case "RecordSet": {
|
|
1288
|
-
if (isEmptySet(value)) return [];
|
|
1289
|
-
const fields =
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
)
|
|
1294
|
-
|
|
1295
|
-
return toFiniteSet(set).elements;
|
|
1296
|
-
});
|
|
1454
|
+
if (!valueVersion.is174 && isEmptySet(value)) return [];
|
|
1455
|
+
const fields = getComponentElements(
|
|
1456
|
+
value.sets,
|
|
1457
|
+
getElements,
|
|
1458
|
+
(set, index) =>
|
|
1459
|
+
`Attempted to enumerate a set of the form [l1 : v1, ..., ln : vn],\nbut can't enumerate the value of the \`${value.names[index]}' field:\n${formatValue(set)}`,
|
|
1460
|
+
);
|
|
1297
1461
|
return getCartesianProduct(fields).map((combination) =>
|
|
1298
1462
|
createRecord(value.names.map((name, index): [string, Value] => [name, combination[index]])),
|
|
1299
1463
|
);
|
|
1300
1464
|
}
|
|
1301
1465
|
case "ProductSet": {
|
|
1302
|
-
if (isEmptySet(value)) return [];
|
|
1303
|
-
const factors =
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
)
|
|
1308
|
-
|
|
1309
|
-
return toFiniteSet(factor).elements;
|
|
1310
|
-
});
|
|
1466
|
+
if (!valueVersion.is174 && isEmptySet(value)) return [];
|
|
1467
|
+
const factors = getComponentElements(
|
|
1468
|
+
value.factors,
|
|
1469
|
+
getElements,
|
|
1470
|
+
(factor, index) =>
|
|
1471
|
+
`Attempted to enumerate a set of the form s1 \\X s2 ... \\X sn,\nbut can't enumerate s${index}:\n${formatValue(factor)}`,
|
|
1472
|
+
);
|
|
1311
1473
|
return getCartesianProduct(factors).map(createTuple);
|
|
1312
1474
|
}
|
|
1313
1475
|
case "FilteredSet":
|
|
1314
|
-
if (isOverriddenSet(value.base)) {
|
|
1476
|
+
if (!isSetValue(value.base) || isOverriddenSet(value.base)) {
|
|
1315
1477
|
throw new TlaEvaluationError(
|
|
1316
1478
|
`Attempted to enumerate { x \\in S : p(x) } when S:\n${formatValue(value.base)}\nis not enumerable`,
|
|
1317
1479
|
);
|
|
@@ -1348,7 +1510,7 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1348
1510
|
}
|
|
1349
1511
|
if (value.operator === "\\cup")
|
|
1350
1512
|
return unionSorted(toFiniteSet(value.left), toFiniteSet(value.right)).elements;
|
|
1351
|
-
const isLeftEnumerated = value.operator === "\\" ||
|
|
1513
|
+
const isLeftEnumerated = value.operator === "\\" || isTlcEnumerable(value.left);
|
|
1352
1514
|
const enumerated = isLeftEnumerated ? value.left : value.right;
|
|
1353
1515
|
const other = isLeftEnumerated ? value.right : value.left;
|
|
1354
1516
|
return toFiniteSet(enumerated).elements.filter(
|
|
@@ -1366,7 +1528,6 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1366
1528
|
};
|
|
1367
1529
|
|
|
1368
1530
|
const finiteSets = new WeakMap<ObjectValue, SetValue>();
|
|
1369
|
-
const comparedSets = new WeakMap<ObjectValue, SetValue>();
|
|
1370
1531
|
|
|
1371
1532
|
export const toFiniteSet = (value: Value): SetValue => {
|
|
1372
1533
|
if (typeof value === "object" && value.kind === "Set") return value;
|
|
@@ -1375,17 +1536,10 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1375
1536
|
}
|
|
1376
1537
|
const cached = typeof value === "object" ? finiteSets.get(value) : undefined;
|
|
1377
1538
|
if (cached) return cached;
|
|
1378
|
-
const
|
|
1379
|
-
|
|
1380
|
-
(valueSources.current
|
|
1381
|
-
? withSource(value, () => enumerateFiniteSet(value))
|
|
1382
|
-
: enumerateFiniteSet(value));
|
|
1383
|
-
if (typeof value === "object") finiteSets.set(value, set);
|
|
1384
|
-
return set;
|
|
1385
|
-
};
|
|
1386
|
-
|
|
1387
|
-
const enumerateFiniteSet = (value: Value) => {
|
|
1539
|
+
const sets = setTrace.sets;
|
|
1540
|
+
sets?.push(value);
|
|
1388
1541
|
const elements = enumerateSet(value);
|
|
1542
|
+
sets?.pop();
|
|
1389
1543
|
const isSorted =
|
|
1390
1544
|
typeof value === "object" &&
|
|
1391
1545
|
(value.kind === "MixedSet" ||
|
|
@@ -1395,27 +1549,46 @@ const enumerateFiniteSet = (value: Value) => {
|
|
|
1395
1549
|
value.kind === "ProductSet" ||
|
|
1396
1550
|
value.kind === "FunctionSet" ||
|
|
1397
1551
|
value.kind === "Subset");
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
// TLC converts the set a comparison receives as its argument without caching it, so a lazy set
|
|
1402
|
-
// compared that way still streams the sets it combines.
|
|
1403
|
-
const toComparedSet = (value: Value) => {
|
|
1404
|
-
const isStreamed =
|
|
1405
|
-
typeof value === "object" &&
|
|
1406
|
-
(value.kind === "Union" || value.kind === "SetOperation" || value.kind === "FilteredSet");
|
|
1407
|
-
if (!isStreamed || finiteSets.has(value)) return toFiniteSet(value);
|
|
1408
|
-
const compared = comparedSets.get(value);
|
|
1409
|
-
if (compared) return compared;
|
|
1410
|
-
const set = enumerateFiniteSet(value);
|
|
1411
|
-
comparedSets.set(value, set);
|
|
1552
|
+
const set = isSorted ? createSortedSet(elements) : createSet(elements);
|
|
1553
|
+
if (typeof value === "object") finiteSets.set(value, set);
|
|
1412
1554
|
return set;
|
|
1413
1555
|
};
|
|
1414
1556
|
|
|
1415
1557
|
// TLC's Enumerator for a lazy set streams the sets it combines until the set caches its elements,
|
|
1416
1558
|
// so an element that two of them share comes up once for each.
|
|
1417
1559
|
export const getStreamedElements = (value: Value): Value[] => {
|
|
1418
|
-
if (typeof value !== "object" ||
|
|
1560
|
+
if (typeof value !== "object" || cachedSets.has(value)) return toFiniteSet(value).elements;
|
|
1561
|
+
const sets = setTrace.sets;
|
|
1562
|
+
sets?.push(value);
|
|
1563
|
+
const elements = streamElements(value);
|
|
1564
|
+
sets?.pop();
|
|
1565
|
+
return elements;
|
|
1566
|
+
};
|
|
1567
|
+
|
|
1568
|
+
const getCombinedSets = (value: ObjectValue) =>
|
|
1569
|
+
value.kind === "ProductSet"
|
|
1570
|
+
? value.factors
|
|
1571
|
+
: value.kind === "RecordSet"
|
|
1572
|
+
? value.sets
|
|
1573
|
+
: value.kind === "FunctionSet"
|
|
1574
|
+
? [value.range]
|
|
1575
|
+
: undefined;
|
|
1576
|
+
|
|
1577
|
+
// Whether a set streams its elements in another order than the normalized set, so that a set of
|
|
1578
|
+
// tuples, records, or functions it is a component of has to stream too.
|
|
1579
|
+
const isStreamedApart = (value: Value): boolean => {
|
|
1580
|
+
if (typeof value !== "object" || cachedSets.has(value)) return false;
|
|
1581
|
+
if (value.kind === "Union") return true;
|
|
1582
|
+
if (value.kind === "FilteredSet") return isStreamedApart(value.base);
|
|
1583
|
+
if (value.kind === "SetOperation") {
|
|
1584
|
+
return (
|
|
1585
|
+
value.operator === "\\cup" || isStreamedApart(value.left) || isStreamedApart(value.right)
|
|
1586
|
+
);
|
|
1587
|
+
}
|
|
1588
|
+
return getCombinedSets(value)?.some(isStreamedApart) ?? false;
|
|
1589
|
+
};
|
|
1590
|
+
|
|
1591
|
+
const streamElements = (value: ObjectValue): Value[] => {
|
|
1419
1592
|
// TLC streams the sets of a UNION it hasn't enumerated, so a set that appears twice there yields
|
|
1420
1593
|
// its elements twice.
|
|
1421
1594
|
if (value.kind === "Union") {
|
|
@@ -1426,13 +1599,15 @@ export const getStreamedElements = (value: Value): Value[] => {
|
|
|
1426
1599
|
if (value.kind === "FilteredSet" && !isOverriddenSet(value.base)) {
|
|
1427
1600
|
return getStreamedElements(value.base).filter(value.test);
|
|
1428
1601
|
}
|
|
1602
|
+
const components = getCombinedSets(value);
|
|
1603
|
+
if (components?.some(isStreamedApart)) return enumerateSet(value, getStreamedElements);
|
|
1429
1604
|
if (value.kind !== "SetOperation") return toFiniteSet(value).elements;
|
|
1430
1605
|
if (value.operator === "\\cup") {
|
|
1431
1606
|
return !isTlcEnumerable(value.left) || !isTlcEnumerable(value.right)
|
|
1432
1607
|
? toFiniteSet(value).elements
|
|
1433
1608
|
: [...getStreamedElements(value.left), ...getStreamedElements(value.right)];
|
|
1434
1609
|
}
|
|
1435
|
-
const isLeftEnumerated = value.operator === "\\" ||
|
|
1610
|
+
const isLeftEnumerated = value.operator === "\\" || isTlcEnumerable(value.left);
|
|
1436
1611
|
const enumerated = isLeftEnumerated ? value.left : value.right;
|
|
1437
1612
|
const other = isLeftEnumerated ? value.right : value.left;
|
|
1438
1613
|
if (!isTlcEnumerable(enumerated)) return toFiniteSet(value).elements;
|
|
@@ -1472,9 +1647,13 @@ export const isEmptySet = (value: Value): boolean => {
|
|
|
1472
1647
|
}
|
|
1473
1648
|
};
|
|
1474
1649
|
|
|
1475
|
-
const getProductCardinality = (
|
|
1650
|
+
const getProductCardinality = (
|
|
1651
|
+
sets: Value[],
|
|
1652
|
+
version: TlcVersion | undefined,
|
|
1653
|
+
describe: () => string,
|
|
1654
|
+
) =>
|
|
1476
1655
|
sets.reduce<number>((product, set) => {
|
|
1477
|
-
const size = product * getCardinality(set);
|
|
1656
|
+
const size = product * getCardinality(set, version);
|
|
1478
1657
|
if (size > MAXIMUM_INTEGER) throw new TlaEvaluationError(describe());
|
|
1479
1658
|
return size;
|
|
1480
1659
|
}, 1);
|
|
@@ -1482,9 +1661,25 @@ const getProductCardinality = (sets: Value[], describe: () => string) =>
|
|
|
1482
1661
|
const describeOverflow = (value: Value) =>
|
|
1483
1662
|
`Overflow when computing the number of elements in:\n${formatValue(value)}`;
|
|
1484
1663
|
|
|
1485
|
-
|
|
1486
|
-
|
|
1664
|
+
// TLC 1.7.4 sizes every component of a set of functions, records, or tuples without first
|
|
1665
|
+
// checking whether one is empty, so an infinite component fails even beside an empty one.
|
|
1666
|
+
export const getCardinality = (value: Value, version?: TlcVersion): number => {
|
|
1667
|
+
if (typeof value !== "object") {
|
|
1668
|
+
const kind =
|
|
1669
|
+
typeof value === "number" ? "integer" : typeof value === "string" ? "string" : "boolean";
|
|
1670
|
+
throw new TlaEvaluationError(
|
|
1671
|
+
`Attempted to compute the number of elements in the ${kind} ${formatValue(value)}.`,
|
|
1672
|
+
);
|
|
1673
|
+
}
|
|
1487
1674
|
switch (value.kind) {
|
|
1675
|
+
case "ModelValue":
|
|
1676
|
+
throw new TlaEvaluationError(
|
|
1677
|
+
`Attempted to compute the number of elements in the model value ${formatValue(value)}.`,
|
|
1678
|
+
);
|
|
1679
|
+
case "Function":
|
|
1680
|
+
return value.domain.length;
|
|
1681
|
+
case "LazyFunction":
|
|
1682
|
+
return getCardinality(value.domain, version);
|
|
1488
1683
|
case "Interval": {
|
|
1489
1684
|
const size = Math.max(0, value.high - value.low + 1);
|
|
1490
1685
|
if (size > MAXIMUM_INTEGER) {
|
|
@@ -1500,16 +1695,19 @@ export const getCardinality = (value: Value): number => {
|
|
|
1500
1695
|
`Attempted to compute the number of elements in the overridden value ${formatValue(value)}.`,
|
|
1501
1696
|
);
|
|
1502
1697
|
case "Subset": {
|
|
1503
|
-
const size = getCardinality(value.base);
|
|
1698
|
+
const size = getCardinality(value.base, version);
|
|
1504
1699
|
if (size >= 31) throw new TlaEvaluationError(describeOverflow(value));
|
|
1505
1700
|
return 2 ** size;
|
|
1506
1701
|
}
|
|
1507
1702
|
case "FunctionSet": {
|
|
1508
|
-
if (
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1703
|
+
if (version !== "1.7.4") {
|
|
1704
|
+
if (isEmptySet(value.domain)) return 1;
|
|
1705
|
+
if (isEmptySet(value.range)) return 0;
|
|
1706
|
+
if (getCardinality(value.range, version) === 1) return 1;
|
|
1707
|
+
}
|
|
1708
|
+
const domainSize = getCardinality(value.domain, version);
|
|
1709
|
+
const rangeSize = getCardinality(value.range, version);
|
|
1710
|
+
if (rangeSize <= 1) return domainSize === 0 ? 1 : rangeSize;
|
|
1513
1711
|
const size = { value: 1 };
|
|
1514
1712
|
for (let index = 0; index < domainSize; index++) {
|
|
1515
1713
|
size.value *= rangeSize;
|
|
@@ -1518,15 +1716,17 @@ export const getCardinality = (value: Value): number => {
|
|
|
1518
1716
|
return size.value;
|
|
1519
1717
|
}
|
|
1520
1718
|
case "RecordSet":
|
|
1521
|
-
if (isEmptySet(value)) return 0;
|
|
1522
|
-
return getProductCardinality(value.sets, () => describeOverflow(value));
|
|
1719
|
+
if (version !== "1.7.4" && isEmptySet(value)) return 0;
|
|
1720
|
+
return getProductCardinality(value.sets, version, () => describeOverflow(value));
|
|
1523
1721
|
case "ProductSet":
|
|
1524
|
-
if (isEmptySet(value)) return 0;
|
|
1722
|
+
if (version !== "1.7.4" && isEmptySet(value)) return 0;
|
|
1525
1723
|
return getProductCardinality(
|
|
1526
1724
|
value.factors,
|
|
1725
|
+
version,
|
|
1527
1726
|
() => `Overflow when computing the number of elements in ${formatValue(value)}`,
|
|
1528
1727
|
);
|
|
1529
1728
|
default:
|
|
1729
|
+
cacheSet(value);
|
|
1530
1730
|
return toFiniteSet(value).elements.length;
|
|
1531
1731
|
}
|
|
1532
1732
|
};
|
|
@@ -1601,11 +1801,11 @@ export const isFiniteSet = (value: Value): boolean => {
|
|
|
1601
1801
|
}
|
|
1602
1802
|
};
|
|
1603
1803
|
|
|
1604
|
-
const findIndex = (sortedValues: Value[], target: Value) => {
|
|
1804
|
+
const findIndex = (sortedValues: Value[], target: Value, compare = compareValues) => {
|
|
1605
1805
|
const bounds = { low: 0, high: sortedValues.length - 1 };
|
|
1606
1806
|
while (bounds.low <= bounds.high) {
|
|
1607
1807
|
const middle = (bounds.low + bounds.high) >> 1;
|
|
1608
|
-
const comparison =
|
|
1808
|
+
const comparison = compare(sortedValues[middle], target);
|
|
1609
1809
|
if (comparison === 0) return middle;
|
|
1610
1810
|
if (comparison < 0) bounds.low = middle + 1;
|
|
1611
1811
|
else bounds.high = middle - 1;
|
|
@@ -1621,41 +1821,80 @@ export const getSequenceElements = (value: Value): Value[] | undefined => {
|
|
|
1621
1821
|
: undefined;
|
|
1622
1822
|
};
|
|
1623
1823
|
|
|
1624
|
-
const describeMembership = (element: Value, set: Value) =>
|
|
1625
|
-
`Attempted to check if the value ${formatValue(element)} is an element of ${formatValue(set)}`;
|
|
1626
|
-
|
|
1627
1824
|
const describeNonSetMembership = (element: Value, value: Value) => {
|
|
1628
1825
|
const prefix = `Attempted to check if the value:\n${formatValue(element)}\nis an element of the`;
|
|
1629
1826
|
if (!isFunctionValue(value)) return `${prefix} ${describeKind(value)} ${formatValue(value)}`;
|
|
1630
|
-
const
|
|
1631
|
-
|
|
1632
|
-
if (domain.length > 0 && domain.every((key) => typeof key === "string")) {
|
|
1827
|
+
const functionClass = getFunctionClass(value);
|
|
1828
|
+
if (functionClass === "record") {
|
|
1633
1829
|
return `Attempted to check if element:\n${formatValue(element)}\nis in the record:\n${formatValue(value)}`;
|
|
1634
1830
|
}
|
|
1635
|
-
if (
|
|
1636
|
-
return "Attempted to check set membership in a tuple value.";
|
|
1637
|
-
}
|
|
1831
|
+
if (functionClass === "tuple") return "Attempted to check set membership in a tuple value.";
|
|
1638
1832
|
return `${prefix} function ${formatValue(value)}`;
|
|
1639
1833
|
};
|
|
1640
1834
|
|
|
1641
|
-
//
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1835
|
+
// The call stack of the replay, which a failed fast path leaves frozen.
|
|
1836
|
+
interface TracedCallStack {
|
|
1837
|
+
expressions: unknown[];
|
|
1838
|
+
isFrozen: boolean;
|
|
1839
|
+
fingerprint?: unknown;
|
|
1840
|
+
}
|
|
1841
|
+
|
|
1842
|
+
// The sets whose membership is being checked or whose elements are being enumerated, and the
|
|
1843
|
+
// members being compared, outermost first, while TLC's CallStackTool replays a computation. A failed
|
|
1844
|
+
// check or enumeration leaves the values it was in, each of which TLC adds to the
|
|
1845
|
+
// FingerprintException when it has a source.
|
|
1846
|
+
export const setTrace: { sets: Value[] | undefined; callStack?: TracedCallStack } = {
|
|
1847
|
+
sets: undefined,
|
|
1848
|
+
};
|
|
1849
|
+
|
|
1850
|
+
// Takes a fast path, and computes as TLC does where it fails. TLC never takes the fast path, so
|
|
1851
|
+
// the failure's leftovers in the replay's trace go first.
|
|
1852
|
+
export const computeLikeTlcOnFailure = <Result>(
|
|
1853
|
+
compute: () => Result,
|
|
1854
|
+
computeLikeTlc: (error: TlaEvaluationError) => Result,
|
|
1855
|
+
): Result => {
|
|
1856
|
+
const sets = setTrace.sets;
|
|
1857
|
+
const callStack = setTrace.callStack;
|
|
1858
|
+
const depth = sets?.length ?? 0;
|
|
1859
|
+
const trace = callStack && { ...callStack, length: callStack.expressions.length };
|
|
1860
|
+
try {
|
|
1861
|
+
return compute();
|
|
1862
|
+
} catch (error) {
|
|
1863
|
+
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
1864
|
+
if (sets) sets.length = depth;
|
|
1865
|
+
if (callStack && trace) {
|
|
1866
|
+
callStack.expressions.length = trace.length;
|
|
1867
|
+
callStack.isFrozen = trace.isFrozen;
|
|
1868
|
+
callStack.fingerprint = trace.fingerprint;
|
|
1869
|
+
}
|
|
1870
|
+
return computeLikeTlc(error);
|
|
1871
|
+
}
|
|
1872
|
+
};
|
|
1646
1873
|
|
|
1647
1874
|
export const isElementOf = (element: Value, set: Value): boolean => {
|
|
1875
|
+
const sets = setTrace.sets;
|
|
1876
|
+
if (sets === undefined) return isElementOfSet(element, set);
|
|
1877
|
+
sets.push(set);
|
|
1878
|
+
const isElement = isElementOfSet(element, set);
|
|
1879
|
+
sets.pop();
|
|
1880
|
+
return isElement;
|
|
1881
|
+
};
|
|
1882
|
+
|
|
1883
|
+
const isElementOfSet = (element: Value, set: Value): boolean => {
|
|
1648
1884
|
if (!isSetValue(set)) throw new TlaEvaluationError(describeNonSetMembership(element, set));
|
|
1649
1885
|
switch (set.kind) {
|
|
1650
1886
|
case "Set":
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1887
|
+
// SetEnumValue.member compares the element with the set's.
|
|
1888
|
+
cacheSet(element);
|
|
1889
|
+
return computeLikeTlcOnFailure(
|
|
1890
|
+
() => findIndex(set.elements, element) !== -1,
|
|
1891
|
+
() =>
|
|
1892
|
+
normalizedSets.has(set)
|
|
1893
|
+
? searchLikeTlc(element, set.elements).isFound
|
|
1894
|
+
: isElementOfLikeTlc(element, set.elements),
|
|
1895
|
+
);
|
|
1657
1896
|
case "MixedSet":
|
|
1658
|
-
return
|
|
1897
|
+
return isElementOfLikeTlc(element, set.elements);
|
|
1659
1898
|
case "Interval":
|
|
1660
1899
|
if (typeof element === "number") return element >= set.low && element <= set.high;
|
|
1661
1900
|
if (set.low <= set.high && !isModelValue(element)) {
|
|
@@ -1677,15 +1916,18 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1677
1916
|
return set.name !== "Nat" || (typeof element === "number" && element >= 0);
|
|
1678
1917
|
}
|
|
1679
1918
|
case "Subset":
|
|
1680
|
-
|
|
1919
|
+
// SubsetValue.member fails on any value that is not Enumerable, model values and the sets a
|
|
1920
|
+
// Java class overrides included.
|
|
1921
|
+
if (!isSetValue(element) || isOverriddenSet(element)) {
|
|
1681
1922
|
throw new TlaEvaluationError(
|
|
1682
1923
|
`Attempted to check if the non-enumerable value\n${prettyPrint(formatValue(element))}\nis element of\n${prettyPrint(formatValue(set))}`,
|
|
1683
1924
|
);
|
|
1684
1925
|
}
|
|
1685
|
-
return toFiniteSet(element).elements.every((item) =>
|
|
1926
|
+
return toFiniteSet(element).elements.every((item) => isElementOf(item, set.base));
|
|
1686
1927
|
case "FunctionSet": {
|
|
1687
1928
|
if (!isFunctionValue(element)) {
|
|
1688
1929
|
if (isModelValue(element)) return false;
|
|
1930
|
+
// SetOfFcnsValue.member prints the element without Values.ppr.
|
|
1689
1931
|
throw new TlaEvaluationError(
|
|
1690
1932
|
`Attempted to check if \n${formatValue(element)}\nwhich is not a TLC function value, is in the set of functions:\n${prettyPrint(formatValue(set))}`,
|
|
1691
1933
|
);
|
|
@@ -1699,7 +1941,7 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1699
1941
|
{ kind: "Interval", low, high: low + functionValue.domain.length - 1 },
|
|
1700
1942
|
set.domain,
|
|
1701
1943
|
);
|
|
1702
|
-
return isDomain && functionValue.range.every((item) =>
|
|
1944
|
+
return isDomain && functionValue.range.every((item) => isElementOf(item, set.range));
|
|
1703
1945
|
}
|
|
1704
1946
|
case "RecordSet": {
|
|
1705
1947
|
const functionValue = isFunctionValue(element) ? toFiniteFunction(element) : undefined;
|
|
@@ -1713,7 +1955,7 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1713
1955
|
functionValue.domain.length === set.names.length &&
|
|
1714
1956
|
set.names.every((name, index) => {
|
|
1715
1957
|
const position = findIndex(functionValue.domain, name);
|
|
1716
|
-
return position !== -1 &&
|
|
1958
|
+
return position !== -1 && isElementOf(functionValue.range[position], set.sets[index]);
|
|
1717
1959
|
})
|
|
1718
1960
|
);
|
|
1719
1961
|
}
|
|
@@ -1730,30 +1972,33 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1730
1972
|
}
|
|
1731
1973
|
return (
|
|
1732
1974
|
elements.length === set.factors.length &&
|
|
1733
|
-
elements.every((item, index) =>
|
|
1975
|
+
elements.every((item, index) => isElementOf(item, set.factors[index]))
|
|
1734
1976
|
);
|
|
1735
1977
|
}
|
|
1736
1978
|
case "FilteredSet":
|
|
1737
|
-
return
|
|
1979
|
+
return isElementOf(element, set.base) && set.test(element);
|
|
1738
1980
|
case "Union":
|
|
1739
1981
|
if (!isTlcEnumerable(set.base)) {
|
|
1740
1982
|
throw new TlaEvaluationError(
|
|
1741
1983
|
`Attempted to check if:\n ${formatValue(element)}\nis an element of the non-enumerable set:\n ${formatUnion(set)}`,
|
|
1742
1984
|
);
|
|
1743
1985
|
}
|
|
1744
|
-
return toFiniteSet(set.base).elements.some((inner) =>
|
|
1986
|
+
return toFiniteSet(set.base).elements.some((inner) => isElementOf(element, inner));
|
|
1745
1987
|
case "SetOperation":
|
|
1746
1988
|
if (set.operator === "\\cup")
|
|
1747
|
-
return
|
|
1989
|
+
return isElementOf(element, set.left) || isElementOf(element, set.right);
|
|
1748
1990
|
return (
|
|
1749
|
-
|
|
1750
|
-
|
|
1991
|
+
isElementOf(element, set.left) &&
|
|
1992
|
+
isElementOf(element, set.right) === (set.operator === "\\cap")
|
|
1751
1993
|
);
|
|
1752
1994
|
case "SequenceSet": {
|
|
1753
1995
|
const elements = getSequenceElements(element);
|
|
1754
1996
|
if (elements === undefined) {
|
|
1755
1997
|
if (isModelValue(element)) return false;
|
|
1756
|
-
|
|
1998
|
+
// Sequences.member raises TLC_MODULE_CHECK_MEMBER_OF.
|
|
1999
|
+
throw new TlaEvaluationError(
|
|
2000
|
+
`Attempted to check if the value:\n${prettyPrint(formatValue(element))}\nis an element of ${prettyPrint(formatValue(set))}.`,
|
|
2001
|
+
);
|
|
1757
2002
|
}
|
|
1758
2003
|
return elements.every((item) => isElementOf(item, set.base));
|
|
1759
2004
|
}
|
|
@@ -1786,77 +2031,58 @@ export const toFiniteFunction = (value: Value): FunctionValue => {
|
|
|
1786
2031
|
if (typeof value === "object" && value.kind === "LazyFunction") {
|
|
1787
2032
|
if (!isEnumerable(value.domain)) getCardinality(value.domain);
|
|
1788
2033
|
const domain = getFunctionDomain(value.domain);
|
|
1789
|
-
return
|
|
1790
|
-
|
|
1791
|
-
|
|
2034
|
+
return setFunctionClass(
|
|
2035
|
+
createSortedFunction(
|
|
2036
|
+
domain,
|
|
2037
|
+
domain.map((key) => value.apply(key)),
|
|
2038
|
+
),
|
|
2039
|
+
getFunctionClass(value),
|
|
1792
2040
|
);
|
|
1793
2041
|
}
|
|
1794
2042
|
throw new TlaEvaluationError(`Expected a function but found ${formatValue(value)}`);
|
|
1795
2043
|
};
|
|
1796
2044
|
|
|
1797
|
-
const
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
2045
|
+
export const describeMissingArgument = (
|
|
2046
|
+
functionValue: Value,
|
|
2047
|
+
argument: Value,
|
|
2048
|
+
version?: TlcVersion,
|
|
1801
2049
|
) => {
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
2050
|
+
const shown = prettyPrint(formatValue(functionValue));
|
|
2051
|
+
const shownArgument = prettyPrint(formatValue(argument));
|
|
2052
|
+
const is174 = version === "1.7.4";
|
|
2053
|
+
switch (getFunctionClass(functionValue)) {
|
|
2054
|
+
case "lambda":
|
|
2055
|
+
return `In applying the function\n${shown},\nthe first argument is:\n${shownArgument}\nwhich is not in its domain.\n`;
|
|
2056
|
+
case "tuple":
|
|
2057
|
+
if (typeof argument !== "number") {
|
|
2058
|
+
return is174
|
|
2059
|
+
? "Attempted to apply tuple to a non-integer argument."
|
|
2060
|
+
: `Attempted to access tuple at a non integral index: ${shownArgument}`;
|
|
2061
|
+
}
|
|
2062
|
+
return is174
|
|
2063
|
+
? `Attempted to apply tuple\n${shown}\nto integer ${argument} which is out of domain.`
|
|
2064
|
+
: `Attempted to access index ${argument} of tuple\n${shown}\nwhich is out of bounds.`;
|
|
2065
|
+
case "record":
|
|
2066
|
+
if (typeof argument !== "string") {
|
|
2067
|
+
return is174
|
|
2068
|
+
? `Attempted to apply record to a non-string value ${shownArgument}.`
|
|
2069
|
+
: `Attempted to access record by a non-string argument: ${shownArgument}`;
|
|
2070
|
+
}
|
|
2071
|
+
return is174
|
|
2072
|
+
? `Attempted to apply the record\n${shown}\nto nonexistent record field ${argument}.`
|
|
2073
|
+
: `Attempted to access nonexistent field '${argument}' of record\n${shown}`;
|
|
2074
|
+
default:
|
|
2075
|
+
return `Attempted to apply function:\n${shown}\nto argument ${shownArgument}, which is not in the domain of the function.`;
|
|
2076
|
+
}
|
|
1814
2077
|
};
|
|
1815
2078
|
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
const
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
functionValue: FunctionValue | LazyFunctionValue,
|
|
1824
|
-
argument: Value,
|
|
1825
|
-
) => {
|
|
1826
|
-
if (lambdaFunctions.has(functionValue) || functionValue.kind === "LazyFunction")
|
|
1827
|
-
return `In applying the function\n${prettyPrint(formatValue(functionValue))},\nthe first argument is:\n${prettyPrint(formatValue(argument))}\nwhich is not in its domain.\n`;
|
|
1828
|
-
const functionClass = getFunctionClass(functionValue);
|
|
1829
|
-
if (functionClass === "TupleValue")
|
|
1830
|
-
return typeof argument === "number"
|
|
1831
|
-
? `Attempted to access index ${argument} of tuple\n${prettyPrint(formatValue(functionValue))}\nwhich is out of bounds.`
|
|
1832
|
-
: `Attempted to access tuple at a non integral index: ${prettyPrint(formatValue(argument))}`;
|
|
1833
|
-
if (functionClass === "RecordValue")
|
|
1834
|
-
return typeof argument === "string"
|
|
1835
|
-
? `Attempted to access nonexistent field '${argument}' of record\n${prettyPrint(formatValue(functionValue))}`
|
|
1836
|
-
: `Attempted to access record by a non-string argument: ${prettyPrint(formatValue(argument))}`;
|
|
1837
|
-
return functionClass === "IntervalFcnRcdValue" && typeof argument !== "number"
|
|
1838
|
-
? describeNonIntegerArgument(argument)
|
|
1839
|
-
: describeNotInDomain(functionValue, argument);
|
|
1840
|
-
};
|
|
1841
|
-
|
|
1842
|
-
// TLC selects a field of a value that isn't a record from the function the value converts to,
|
|
1843
|
-
// which keeps a tuple's domain as an interval.
|
|
1844
|
-
export const describeMissingField = (functionValue: FunctionValue, field: string) =>
|
|
1845
|
-
getFunctionClass(functionValue) === "FcnRcdValue"
|
|
1846
|
-
? describeNotInDomain(functionValue, field)
|
|
1847
|
-
: describeNonIntegerArgument(field);
|
|
1848
|
-
|
|
1849
|
-
export const isRecordValue = (functionValue: FunctionValue) =>
|
|
1850
|
-
getFunctionClass(functionValue) === "RecordValue";
|
|
1851
|
-
|
|
1852
|
-
// TLC checks the type of an argument to a tuple or a record before it looks the argument up, so
|
|
1853
|
-
// it never compares the argument with a key of another type.
|
|
1854
|
-
const isForeignKey = (domain: Value[], argument: Value) => {
|
|
1855
|
-
const first = domain[0];
|
|
1856
|
-
return typeof first === "number"
|
|
1857
|
-
? typeof argument !== "number" &&
|
|
1858
|
-
(domain === intervalDomains[domain.length] || intervalArrays.has(domain))
|
|
1859
|
-
: typeof first === "string" && typeof argument !== "string";
|
|
2079
|
+
// TLC 1.7.4 words a key of the wrong type in an EXCEPT, which selects rather than applies, apart.
|
|
2080
|
+
const describeExceptKey = (functionValue: FunctionValue, argument: Value) => {
|
|
2081
|
+
if (!valueVersion.is174) return describeMissingArgument(functionValue, argument);
|
|
2082
|
+
const shown = prettyPrint(formatValue(argument));
|
|
2083
|
+
return getFunctionClass(functionValue) === "tuple"
|
|
2084
|
+
? `Attempted to apply tuple to a non-integer argument ${shown}.`
|
|
2085
|
+
: `Attempted to apply record to a non-string argument ${shown}.`;
|
|
1860
2086
|
};
|
|
1861
2087
|
|
|
1862
2088
|
const findKeyIndex = (domain: Value[], argument: Value) => {
|
|
@@ -1872,16 +2098,57 @@ const findKeyIndex = (domain: Value[], argument: Value) => {
|
|
|
1872
2098
|
return findIndex(domain, argument);
|
|
1873
2099
|
};
|
|
1874
2100
|
|
|
2101
|
+
export const createNonIntegerKeyError = (argument: Value) =>
|
|
2102
|
+
new TlaEvaluationError(
|
|
2103
|
+
`Attempted to apply function with integer domain to the non-integer argument ${prettyPrint(formatValue(argument))}`,
|
|
2104
|
+
);
|
|
2105
|
+
|
|
2106
|
+
// TLC's FcnRcdValue searches a domain this small linearly with equals, and a larger normalized one by
|
|
2107
|
+
// binary search with compareTo.
|
|
2108
|
+
const LINEAR_SEARCH_LIMIT = 32;
|
|
2109
|
+
|
|
2110
|
+
// TLC looks a key up by the Java class of the function: a tuple or a record rejects a key of the
|
|
2111
|
+
// wrong type, and another function rejects a non-integer for a domain it keeps as an interval, which
|
|
2112
|
+
// TLC 1.7.4 doesn't. A key of another type than the domain's keys otherwise fails TLC's comparison.
|
|
2113
|
+
const findKeyIndexLikeTlc = (
|
|
2114
|
+
functionValue: FunctionValue,
|
|
2115
|
+
argument: Value,
|
|
2116
|
+
describeWrongKey: (functionValue: FunctionValue, argument: Value) => string,
|
|
2117
|
+
) => {
|
|
2118
|
+
const domain = functionValue.domain;
|
|
2119
|
+
if (typeof argument !== typeof domain[0]) {
|
|
2120
|
+
const functionClass = getFunctionClass(functionValue);
|
|
2121
|
+
const keyType =
|
|
2122
|
+
functionClass === "tuple" ? "number" : functionClass === "record" ? "string" : undefined;
|
|
2123
|
+
if (keyType !== undefined && typeof argument !== keyType) {
|
|
2124
|
+
throw new TlaEvaluationError(describeWrongKey(functionValue, argument));
|
|
2125
|
+
}
|
|
2126
|
+
if (keyType === undefined && !valueVersion.is174 && isIntervalDomain(domain)) {
|
|
2127
|
+
throw createNonIntegerKeyError(argument);
|
|
2128
|
+
}
|
|
2129
|
+
}
|
|
2130
|
+
return computeLikeTlcOnFailure(
|
|
2131
|
+
() => findKeyIndex(domain, argument),
|
|
2132
|
+
(error) => {
|
|
2133
|
+
if (valueVersion.is174 || domain.length < LINEAR_SEARCH_LIMIT) {
|
|
2134
|
+
domain.forEach((key) => equalsLikeTlc(key, argument));
|
|
2135
|
+
} else {
|
|
2136
|
+
findIndex(domain, argument, compareLikeTlc);
|
|
2137
|
+
}
|
|
2138
|
+
throw error;
|
|
2139
|
+
},
|
|
2140
|
+
);
|
|
2141
|
+
};
|
|
2142
|
+
|
|
1875
2143
|
export const applyFunction = (
|
|
1876
2144
|
functionValue: Value,
|
|
1877
2145
|
argument: Value,
|
|
1878
|
-
describeMissing?: (functionValue: FunctionValue) => string,
|
|
1879
|
-
describeNonFunction?: (value: Value) => string,
|
|
2146
|
+
describeMissing?: (functionValue: FunctionValue, argument: Value) => string,
|
|
1880
2147
|
): Value => {
|
|
1881
2148
|
if (typeof functionValue === "object" && functionValue.kind === "LazyFunction") {
|
|
1882
2149
|
if (!isElementOf(argument, functionValue.domain)) {
|
|
1883
2150
|
throw new TlaEvaluationError(
|
|
1884
|
-
|
|
2151
|
+
getFunctionClass(functionValue) === "lambda"
|
|
1885
2152
|
? describeMissingArgument(functionValue, argument)
|
|
1886
2153
|
: `Attempted to apply a function to ${formatValue(argument)}, which is not in its domain`,
|
|
1887
2154
|
);
|
|
@@ -1890,18 +2157,12 @@ export const applyFunction = (
|
|
|
1890
2157
|
}
|
|
1891
2158
|
if (typeof functionValue !== "object" || functionValue.kind !== "Function") {
|
|
1892
2159
|
throw new TlaEvaluationError(
|
|
1893
|
-
|
|
1894
|
-
`Attempted to apply ${formatValue(functionValue)}, which is not a function`,
|
|
1895
|
-
);
|
|
1896
|
-
}
|
|
1897
|
-
const position = isForeignKey(functionValue.domain, argument)
|
|
1898
|
-
? -1
|
|
1899
|
-
: findKeyIndex(functionValue.domain, argument);
|
|
1900
|
-
if (position === -1) {
|
|
1901
|
-
throw new TlaEvaluationError(
|
|
1902
|
-
describeMissing?.(functionValue) ?? describeMissingArgument(functionValue, argument),
|
|
2160
|
+
`Attempted to apply ${formatValue(functionValue)}, which is not a function`,
|
|
1903
2161
|
);
|
|
1904
2162
|
}
|
|
2163
|
+
const describe = describeMissing ?? describeMissingArgument;
|
|
2164
|
+
const position = findKeyIndexLikeTlc(functionValue, argument, describe);
|
|
2165
|
+
if (position === -1) throw new TlaEvaluationError(describe(functionValue, argument));
|
|
1905
2166
|
return functionValue.range[position];
|
|
1906
2167
|
};
|
|
1907
2168
|
|
|
@@ -1930,28 +2191,45 @@ export const updateFunction = (
|
|
|
1930
2191
|
if (isLazy) {
|
|
1931
2192
|
if (!isElementOf(argument, functionValue.domain)) return functionValue;
|
|
1932
2193
|
const updated = update(functionValue.apply(argument));
|
|
1933
|
-
return
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
2194
|
+
return setFunctionClass<LazyFunctionValue>(
|
|
2195
|
+
{
|
|
2196
|
+
kind: "LazyFunction",
|
|
2197
|
+
domain: functionValue.domain,
|
|
2198
|
+
apply: (key) => (isEqualLikeTlc(argument, key) ? updated : functionValue.apply(key)),
|
|
2199
|
+
source: functionValue.source,
|
|
2200
|
+
},
|
|
2201
|
+
getFunctionClass(functionValue),
|
|
2202
|
+
);
|
|
2203
|
+
}
|
|
2204
|
+
if (!isFunctionValue(functionValue)) {
|
|
2205
|
+
throw new TlaEvaluationError(
|
|
2206
|
+
`Attempted to apply EXCEPT construct to the value ${prettyPrint(formatValue(functionValue))}.`,
|
|
2207
|
+
);
|
|
1939
2208
|
}
|
|
1940
2209
|
const finite = toFiniteFunction(functionValue);
|
|
1941
|
-
const position =
|
|
2210
|
+
const position = findKeyIndexLikeTlc(finite, argument, describeExceptKey);
|
|
1942
2211
|
if (position === -1) return finite;
|
|
1943
2212
|
const range = [...finite.range];
|
|
1944
2213
|
const previous = finite.range[position];
|
|
1945
2214
|
const next = update(previous);
|
|
1946
2215
|
range[position] = next;
|
|
1947
|
-
const updated =
|
|
2216
|
+
const updated = setFunctionClass(
|
|
2217
|
+
createSortedFunction(finite.domain, range),
|
|
2218
|
+
getFunctionClass(finite),
|
|
2219
|
+
);
|
|
1948
2220
|
updated.writtenKeys = finite.writtenKeys;
|
|
1949
|
-
if (finite.hash !== 0) {
|
|
1950
|
-
const
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
2221
|
+
if (finite.hash !== 0 && finite.hash !== UNFINGERPRINTABLE_HASH) {
|
|
2222
|
+
const nextHash = getValueHash(next);
|
|
2223
|
+
if (nextHash !== UNFINGERPRINTABLE_HASH) {
|
|
2224
|
+
const keyHash = getValueHash(finite.domain[position]);
|
|
2225
|
+
updated.hash =
|
|
2226
|
+
previous === next
|
|
2227
|
+
? finite.hash
|
|
2228
|
+
: (finite.hash -
|
|
2229
|
+
getEntryContribution(keyHash, getValueHash(previous)) +
|
|
2230
|
+
getEntryContribution(keyHash, nextHash)) |
|
|
2231
|
+
0;
|
|
2232
|
+
}
|
|
1955
2233
|
}
|
|
1956
2234
|
if (finite.fingerprint !== 0) {
|
|
1957
2235
|
const key = finite.domain[position];
|
|
@@ -1988,37 +2266,103 @@ const hashString = (value: string) => {
|
|
|
1988
2266
|
const SET_HASH_SEED = 0x73a5c3e1;
|
|
1989
2267
|
const FUNCTION_HASH_SEED = 0x66d1b4f7;
|
|
1990
2268
|
|
|
1991
|
-
const getElementContribution = (
|
|
1992
|
-
finalizeHash((
|
|
2269
|
+
const getElementContribution = (elementHash: number) =>
|
|
2270
|
+
finalizeHash((elementHash + 0x9e3779b9) | 0);
|
|
1993
2271
|
|
|
1994
|
-
const getEntryContribution = (
|
|
1995
|
-
finalizeHash((Math.imul(
|
|
2272
|
+
const getEntryContribution = (keyHash: number, valueHash: number) =>
|
|
2273
|
+
finalizeHash((Math.imul(keyHash, 0x27d4eb2d) + valueHash) | 0);
|
|
1996
2274
|
|
|
1997
|
-
|
|
2275
|
+
// The hash of each value that TLC fails to fingerprint, such as Nat or a set that holds it. Calls
|
|
2276
|
+
// with such a value as an argument still share their result, and a state that holds one fails
|
|
2277
|
+
// when TLC fingerprints it.
|
|
2278
|
+
const UNFINGERPRINTABLE_HASH = 0x2f6b1c4d;
|
|
1998
2279
|
|
|
1999
2280
|
const computeValueHash = (value: ObjectValue) => {
|
|
2000
2281
|
if (value.kind === "ModelValue") return finalizeHash(combineHash(hashString(value.name), 0x6d));
|
|
2001
2282
|
if (isSetValue(value)) {
|
|
2002
|
-
if (!isEnumerable(value))
|
|
2003
|
-
unenumeratedSets.isHashed = true;
|
|
2004
|
-
return hashString(getValueKey(value));
|
|
2005
|
-
}
|
|
2283
|
+
if (!isEnumerable(value)) return UNFINGERPRINTABLE_HASH;
|
|
2006
2284
|
const elements = toFiniteSet(value).elements;
|
|
2007
2285
|
let hash = SET_HASH_SEED;
|
|
2008
2286
|
for (let index = 0; index < elements.length; index++) {
|
|
2009
|
-
|
|
2287
|
+
const elementHash = getValueHash(elements[index]);
|
|
2288
|
+
if (elementHash === UNFINGERPRINTABLE_HASH) return UNFINGERPRINTABLE_HASH;
|
|
2289
|
+
hash = (hash + getElementContribution(elementHash)) | 0;
|
|
2010
2290
|
}
|
|
2011
2291
|
return hash;
|
|
2012
2292
|
}
|
|
2293
|
+
if (value.kind === "LazyFunction" && !isEnumerable(value.domain)) return UNFINGERPRINTABLE_HASH;
|
|
2013
2294
|
const functionValue = toFiniteFunction(value);
|
|
2014
2295
|
let hash = FUNCTION_HASH_SEED;
|
|
2015
2296
|
for (let index = 0; index < functionValue.domain.length; index++) {
|
|
2016
|
-
|
|
2017
|
-
|
|
2297
|
+
const keyHash = getValueHash(functionValue.domain[index]);
|
|
2298
|
+
const itemHash = getValueHash(functionValue.range[index]);
|
|
2299
|
+
if (keyHash === UNFINGERPRINTABLE_HASH || itemHash === UNFINGERPRINTABLE_HASH) {
|
|
2300
|
+
return UNFINGERPRINTABLE_HASH;
|
|
2301
|
+
}
|
|
2302
|
+
hash = (hash + getEntryContribution(keyHash, itemHash)) | 0;
|
|
2018
2303
|
}
|
|
2019
2304
|
return hash;
|
|
2020
2305
|
};
|
|
2021
2306
|
|
|
2307
|
+
// TLC's fingerPrint enumerates each set and function a value holds, and fails on Nat, Seq(S) and
|
|
2308
|
+
// the like, which it never enumerates.
|
|
2309
|
+
const fingerprintValue = (value: Value): void => {
|
|
2310
|
+
if (typeof value !== "object" || value.kind === "ModelValue") return;
|
|
2311
|
+
if (value.kind === "InfiniteSet" || value.kind === "SequenceSet") {
|
|
2312
|
+
throw new TlaEvaluationError(
|
|
2313
|
+
`TLC has found a state in which the value of a variable contains ${formatValue(value)}`,
|
|
2314
|
+
);
|
|
2315
|
+
}
|
|
2316
|
+
if (isSetValue(value)) {
|
|
2317
|
+
toFiniteSet(value).elements.forEach(fingerprintValue);
|
|
2318
|
+
return;
|
|
2319
|
+
}
|
|
2320
|
+
const functionValue = toFiniteFunction(value);
|
|
2321
|
+
functionValue.domain.forEach(fingerprintValue);
|
|
2322
|
+
functionValue.range.forEach(fingerprintValue);
|
|
2323
|
+
};
|
|
2324
|
+
|
|
2325
|
+
// The error TLC raises as it fingerprints a value: one that hashing the value raises, as for a set
|
|
2326
|
+
// too large to enumerate, or one of a value with the hash of the values TLC fails on.
|
|
2327
|
+
export const getFingerprintError = (value: Value) => {
|
|
2328
|
+
if (typeof value !== "object" || value.kind === "ModelValue") return undefined;
|
|
2329
|
+
try {
|
|
2330
|
+
if (getValueHash(value) === UNFINGERPRINTABLE_HASH) fingerprintValue(value);
|
|
2331
|
+
return undefined;
|
|
2332
|
+
} catch (error) {
|
|
2333
|
+
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
2334
|
+
return error;
|
|
2335
|
+
}
|
|
2336
|
+
};
|
|
2337
|
+
|
|
2338
|
+
// The values a value holds, which TLC fingerprints as it fingerprints the value.
|
|
2339
|
+
export const getHeldValues = (value: ObjectValue): Value[] => {
|
|
2340
|
+
switch (value.kind) {
|
|
2341
|
+
case "Set":
|
|
2342
|
+
case "MixedSet":
|
|
2343
|
+
return value.elements;
|
|
2344
|
+
case "Function":
|
|
2345
|
+
return [...value.domain, ...value.range];
|
|
2346
|
+
case "Subset":
|
|
2347
|
+
case "SequenceSet":
|
|
2348
|
+
case "FilteredSet":
|
|
2349
|
+
case "Union":
|
|
2350
|
+
return [value.base];
|
|
2351
|
+
case "FunctionSet":
|
|
2352
|
+
return [value.domain, value.range];
|
|
2353
|
+
case "RecordSet":
|
|
2354
|
+
return value.sets;
|
|
2355
|
+
case "ProductSet":
|
|
2356
|
+
return value.factors;
|
|
2357
|
+
case "SetOperation":
|
|
2358
|
+
return [value.left, value.right];
|
|
2359
|
+
case "LazyFunction":
|
|
2360
|
+
return [value.domain];
|
|
2361
|
+
default:
|
|
2362
|
+
return [];
|
|
2363
|
+
}
|
|
2364
|
+
};
|
|
2365
|
+
|
|
2022
2366
|
export const getValueHash = (value: Value): number => {
|
|
2023
2367
|
if (typeof value === "number") return finalizeHash(value ^ 0x5bd1e995);
|
|
2024
2368
|
if (typeof value === "boolean") return value ? 0x2b7e1516 : 0x28aed2a6;
|
|
@@ -2034,25 +2378,6 @@ export const getValueHash = (value: Value): number => {
|
|
|
2034
2378
|
return hash;
|
|
2035
2379
|
};
|
|
2036
2380
|
|
|
2037
|
-
const fingerprintLikeTlc = (value: Value): void => {
|
|
2038
|
-
if (typeof value !== "object" || value.kind === "ModelValue") return;
|
|
2039
|
-
if (isOverriddenSet(value)) {
|
|
2040
|
-
throw new TlaEvaluationError(
|
|
2041
|
-
`TLC has found a state in which the value of a variable contains ${formatValue(value)}`,
|
|
2042
|
-
);
|
|
2043
|
-
}
|
|
2044
|
-
if (isSetValue(value)) return toFiniteSet(value).elements.forEach(fingerprintLikeTlc);
|
|
2045
|
-
const functionValue = toFiniteFunction(value);
|
|
2046
|
-
functionValue.domain.forEach(fingerprintLikeTlc);
|
|
2047
|
-
functionValue.range.forEach(fingerprintLikeTlc);
|
|
2048
|
-
};
|
|
2049
|
-
|
|
2050
|
-
// TLC enumerates every set in a state to fingerprint it, so it fails on a set it can't enumerate.
|
|
2051
|
-
// A state holds one only if hashing it met one, so states go unchecked until then.
|
|
2052
|
-
export const assertFingerprintable = (state: Value[]) => {
|
|
2053
|
-
if (unenumeratedSets.isHashed) state.forEach(fingerprintLikeTlc);
|
|
2054
|
-
};
|
|
2055
|
-
|
|
2056
2381
|
const valueFingerprints = new WeakMap<ObjectValue, number>();
|
|
2057
2382
|
|
|
2058
2383
|
const finalizeFingerprint = (hash: number) => {
|
|
@@ -2079,10 +2404,7 @@ const getEntryFingerprint = (key: Value, value: Value) =>
|
|
|
2079
2404
|
const computeValueFingerprint = (value: ObjectValue) => {
|
|
2080
2405
|
if (value.kind === "ModelValue") return finalizeFingerprint(fingerprintString(value.name) ^ 0x3c);
|
|
2081
2406
|
if (isSetValue(value)) {
|
|
2082
|
-
if (!isEnumerable(value))
|
|
2083
|
-
unenumeratedSets.isHashed = true;
|
|
2084
|
-
return fingerprintString(getValueKey(value));
|
|
2085
|
-
}
|
|
2407
|
+
if (!isEnumerable(value)) return fingerprintString(getValueKey(value));
|
|
2086
2408
|
const elements = toFiniteSet(value).elements;
|
|
2087
2409
|
let fingerprint = 0x1b873593;
|
|
2088
2410
|
for (let index = 0; index < elements.length; index++) {
|
|
@@ -2215,7 +2537,9 @@ const formatLazySet = (value: LazySetValue, isStrict: boolean): string => {
|
|
|
2215
2537
|
case "SetOperation":
|
|
2216
2538
|
return `${format(value.left)} ${value.operator} ${format(value.right)}`;
|
|
2217
2539
|
case "FilteredSet":
|
|
2218
|
-
return
|
|
2540
|
+
return value.source
|
|
2541
|
+
? `{${value.source.names} \\in ${format(value.base)} : <expression ${value.source.predicate}> }`
|
|
2542
|
+
: `{x \\in ${format(value.base)} : ...}`;
|
|
2219
2543
|
case "Union":
|
|
2220
2544
|
return formatUnion(value, isStrict);
|
|
2221
2545
|
}
|
|
@@ -2228,6 +2552,17 @@ const isFieldName = (name: string) =>
|
|
|
2228
2552
|
/\p{L}/u.test(name) &&
|
|
2229
2553
|
(name.length < 4 || !/^[WS]F_/.test(name));
|
|
2230
2554
|
|
|
2555
|
+
// FcnLambdaValue#toString prints a function it fails to enumerate as its parameters and body.
|
|
2556
|
+
const toPrintedFunction = (value: LazyFunctionValue) => {
|
|
2557
|
+
if (!isEnumerable(value.domain)) return undefined;
|
|
2558
|
+
try {
|
|
2559
|
+
return toFiniteFunction(value);
|
|
2560
|
+
} catch (error) {
|
|
2561
|
+
if (error instanceof TlaEvaluationError) return undefined;
|
|
2562
|
+
throw error;
|
|
2563
|
+
}
|
|
2564
|
+
};
|
|
2565
|
+
|
|
2231
2566
|
export const formatValue = (value: Value, isStrict = false, isNormalized = false): string => {
|
|
2232
2567
|
const format = (inner: Value) => formatValue(inner, isStrict, isNormalized);
|
|
2233
2568
|
if (typeof value === "boolean") return value ? "TRUE" : "FALSE";
|
|
@@ -2259,22 +2594,34 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
|
|
|
2259
2594
|
return `Seq(${format(value.base)})`;
|
|
2260
2595
|
case "Function":
|
|
2261
2596
|
case "LazyFunction": {
|
|
2262
|
-
|
|
2263
|
-
|
|
2597
|
+
const functionValue = value.kind === "LazyFunction" ? toPrintedFunction(value) : value;
|
|
2598
|
+
if (functionValue === undefined) {
|
|
2599
|
+
const source = value.kind === "LazyFunction" ? value.source : undefined;
|
|
2264
2600
|
if (!source) return "<function>";
|
|
2265
2601
|
const parameters = source.parameters
|
|
2266
2602
|
.map((parameter) => `${parameter.name} \\in ${formatValue(parameter.domain)}`)
|
|
2267
2603
|
.join(", ");
|
|
2268
2604
|
return `[${parameters} |-> <expression ${source.body}>]`;
|
|
2269
2605
|
}
|
|
2270
|
-
|
|
2606
|
+
if (
|
|
2607
|
+
functionValue.domain.length === 0 &&
|
|
2608
|
+
valueVersion.is174 &&
|
|
2609
|
+
getFunctionClass(value) !== "tuple"
|
|
2610
|
+
) {
|
|
2611
|
+
return "<< >>";
|
|
2612
|
+
}
|
|
2271
2613
|
// TLC normalizes a lambda's domain to print it, but evaluates its body afterward.
|
|
2272
2614
|
const formatRange = (inner: Value) =>
|
|
2273
|
-
formatValue(inner, isStrict, isNormalized && value
|
|
2615
|
+
formatValue(inner, isStrict, isNormalized && getFunctionClass(value) !== "lambda");
|
|
2274
2616
|
if (functionValue.domain.every((key, index) => key === index + 1)) {
|
|
2275
2617
|
return `<<${functionValue.range.map(formatRange).join(", ")}>>`;
|
|
2276
2618
|
}
|
|
2277
|
-
|
|
2619
|
+
// A RecordValue prints as a record whatever its fields, and a FcnRcdValue only if each key
|
|
2620
|
+
// is a name.
|
|
2621
|
+
if (
|
|
2622
|
+
getFunctionClass(value) === "record" ||
|
|
2623
|
+
functionValue.domain.every((key) => typeof key === "string" && isFieldName(key))
|
|
2624
|
+
) {
|
|
2278
2625
|
const isSorted =
|
|
2279
2626
|
isNormalized || functionValue.hash !== 0 || functionValue.fingerprint !== 0;
|
|
2280
2627
|
const pairs = isSorted
|