tla-ts 0.0.3 → 0.0.4-dev.0be9567
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 +32 -12
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +162 -8
- package/dist/index.d.ts +162 -8
- 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 +45 -18
- package/dist/run-cli.js +45 -18
- package/dist/transfer.cjs +1393 -18
- package/dist/transfer.js +1393 -18
- package/package.json +5 -1
- package/src/ast.ts +147 -6
- package/src/bags.ts +4 -7
- package/src/builtins.ts +29 -11
- package/src/bundled-modules.ts +3 -3
- package/src/canonical.ts +4 -3
- package/src/check.ts +1539 -235
- package/src/cli-options.ts +11 -5
- package/src/cli.ts +8 -1
- package/src/codegen.ts +98 -39
- package/src/config.ts +254 -181
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +540 -140
- package/src/errors.ts +224 -3
- package/src/evaluate.ts +994 -279
- package/src/exit-codes.ts +22 -43
- package/src/fingerprints.ts +46 -11
- package/src/index.ts +1 -0
- package/src/instances.ts +19 -16
- package/src/java-overrides.ts +104 -25
- package/src/levels.ts +191 -45
- package/src/lexer.ts +127 -30
- package/src/live-check.ts +867 -0
- package/src/liveness.ts +218 -572
- package/src/obligation-expression.ts +2 -2
- package/src/obligation-source.ts +2 -0
- package/src/parallel-worker.ts +174 -213
- package/src/parallel.ts +131 -39
- package/src/parser.ts +456 -147
- 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 +1481 -426
- package/src/run-cli.ts +1212 -143
- package/src/runtime.ts +19 -1
- package/src/sany-builtins.ts +147 -0
- package/src/sany-generator.ts +3128 -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 +356 -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 +42 -11
- package/src/standard-modules.ts +203 -148
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +180 -0
- package/src/subexpressions.ts +231 -171
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +220 -108
- package/src/symmetry.ts +194 -54
- package/src/tableau.ts +54 -25
- package/src/tlc-ext.ts +42 -29
- package/src/tlc-messages.ts +329 -0
- package/src/tlc-module.ts +52 -43
- package/src/tlc-modules.ts +4 -0
- package/src/tlc-order.ts +78 -10
- package/src/tlc-random.ts +37 -9
- package/src/tlc-replay.ts +655 -0
- package/src/tlc-summary.ts +20 -5
- package/src/tlc-version.ts +18 -0
- package/src/transfer.ts +15 -3
- package/src/value-sources.ts +29 -0
- package/src/values.ts +743 -189
package/src/values.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { TlaEvaluationError, TlaModuleError } from "./errors";
|
|
2
2
|
import { prettyPrint } from "./pretty-print";
|
|
3
|
+
import type { TlcVersion } from "./tlc-version";
|
|
3
4
|
|
|
4
5
|
export interface ModelValue {
|
|
5
6
|
kind: "ModelValue";
|
|
6
7
|
name: string;
|
|
7
8
|
type: string | undefined;
|
|
9
|
+
rank: number;
|
|
8
10
|
}
|
|
9
11
|
|
|
10
12
|
export interface SetValue {
|
|
@@ -12,6 +14,7 @@ export interface SetValue {
|
|
|
12
14
|
elements: Value[];
|
|
13
15
|
hash: number;
|
|
14
16
|
fingerprint: number;
|
|
17
|
+
isFingerprinted: boolean;
|
|
15
18
|
}
|
|
16
19
|
|
|
17
20
|
// A set enumeration whose elements TLC can't sort, such as {1, "a"}. TLC sorts a set enumeration
|
|
@@ -28,6 +31,8 @@ export interface FunctionValue {
|
|
|
28
31
|
hash: number;
|
|
29
32
|
fingerprint: number;
|
|
30
33
|
writtenKeys: Value[] | undefined;
|
|
34
|
+
functionClass: FunctionClass | undefined;
|
|
35
|
+
isFingerprinted: boolean;
|
|
31
36
|
}
|
|
32
37
|
|
|
33
38
|
export interface IntervalValue {
|
|
@@ -75,10 +80,17 @@ export interface SetOperationValue {
|
|
|
75
80
|
right: Value;
|
|
76
81
|
}
|
|
77
82
|
|
|
83
|
+
// TLC prints a set filter as its bound names and the location of its predicate.
|
|
84
|
+
interface FilteredSetSource {
|
|
85
|
+
names: string;
|
|
86
|
+
predicate: string;
|
|
87
|
+
}
|
|
88
|
+
|
|
78
89
|
export interface FilteredSetValue {
|
|
79
90
|
kind: "FilteredSet";
|
|
80
91
|
base: Value;
|
|
81
92
|
test: (element: Value) => boolean;
|
|
93
|
+
source?: FilteredSetSource;
|
|
82
94
|
}
|
|
83
95
|
|
|
84
96
|
// UNION S when some element of S isn't a set enumeration. TLC enumerates it only when it has to.
|
|
@@ -102,6 +114,7 @@ export interface LazyFunctionValue {
|
|
|
102
114
|
domain: Value;
|
|
103
115
|
apply: (argument: Value) => Value;
|
|
104
116
|
source?: LazyFunctionSource;
|
|
117
|
+
functionClass?: FunctionClass;
|
|
105
118
|
}
|
|
106
119
|
|
|
107
120
|
export type Value =
|
|
@@ -136,6 +149,12 @@ export const isSetValue = (value: Value) =>
|
|
|
136
149
|
value.kind !== "Function" &&
|
|
137
150
|
value.kind !== "LazyFunction";
|
|
138
151
|
|
|
152
|
+
// TLC's Reducible values, SetEnumValue and IntervalValue, which combine with another set element by
|
|
153
|
+
// element.
|
|
154
|
+
export const isReducibleSet = (value: Value): value is SetValue | MixedSetValue | IntervalValue =>
|
|
155
|
+
typeof value === "object" &&
|
|
156
|
+
(value.kind === "Set" || value.kind === "MixedSet" || value.kind === "Interval");
|
|
157
|
+
|
|
139
158
|
export const isFunctionValue = (value: Value): value is FunctionValue | LazyFunctionValue =>
|
|
140
159
|
typeof value === "object" && (value.kind === "Function" || value.kind === "LazyFunction");
|
|
141
160
|
|
|
@@ -174,7 +193,7 @@ export const getWrittenPairs = (functionValue: FunctionValue): [Value, Value][]
|
|
|
174
193
|
};
|
|
175
194
|
|
|
176
195
|
export const createWrittenRecord = (names: Value[], range: Value[], writtenNames: Value[]) =>
|
|
177
|
-
setWrittenOrder(
|
|
196
|
+
setWrittenOrder(createSortedRecord(names, range), writtenNames);
|
|
178
197
|
|
|
179
198
|
// TLC sorts a value it compares even with itself, and every value inside it, so a value it has
|
|
180
199
|
// hashed or fingerprinted is already sorted.
|
|
@@ -259,14 +278,26 @@ const checkModelValueTypes = (left: Value, right: Value, isEquality: boolean) =>
|
|
|
259
278
|
|
|
260
279
|
// TLC orders strings and model value names by when it first interned the text, so every
|
|
261
280
|
// check sets the order of its spec's strings before it builds any value.
|
|
262
|
-
const stringOrder
|
|
281
|
+
const stringOrder: { ranks: Record<string, number>; size: number } = {
|
|
282
|
+
ranks: Object.create(null),
|
|
283
|
+
size: 0,
|
|
284
|
+
};
|
|
263
285
|
|
|
264
286
|
export const setStringOrder = (texts: Iterable<string>) => {
|
|
265
287
|
stringOrder.ranks = Object.create(null);
|
|
266
288
|
stringOrder.size = 0;
|
|
289
|
+
modelValues.clear();
|
|
267
290
|
for (const text of texts) rankString(text);
|
|
268
291
|
};
|
|
269
292
|
|
|
293
|
+
// TLC 1.7.4 prints an empty function that isn't a tuple with a space between its brackets, and
|
|
294
|
+
// looks a key up in a function differently, so every check also sets the version its values follow.
|
|
295
|
+
const valueVersion = { is174: false };
|
|
296
|
+
|
|
297
|
+
export const setValueVersion = (version: TlcVersion | undefined) => {
|
|
298
|
+
valueVersion.is174 = version === "1.7.4";
|
|
299
|
+
};
|
|
300
|
+
|
|
270
301
|
export const rankString = (text: string) => {
|
|
271
302
|
if (stringOrder.ranks[text] === undefined) stringOrder.ranks[text] = stringOrder.size++;
|
|
272
303
|
return text;
|
|
@@ -353,7 +384,7 @@ export const compareValues = (left: Value, right: Value): number => {
|
|
|
353
384
|
if (left.type !== undefined && right.type !== undefined) {
|
|
354
385
|
checkModelValueTypes(left, right, false);
|
|
355
386
|
}
|
|
356
|
-
return
|
|
387
|
+
return left.rank - right.rank;
|
|
357
388
|
}
|
|
358
389
|
if (leftKind === "ModelValue" || rightKind === "ModelValue") {
|
|
359
390
|
checkModelValueTypes(left, right, false);
|
|
@@ -385,14 +416,15 @@ export const compareValues = (left: Value, right: Value): number => {
|
|
|
385
416
|
|
|
386
417
|
const modelValues = new Map<string, ModelValue>();
|
|
387
418
|
|
|
419
|
+
// A check ranks every model value's name when it creates the value, and creates its own values.
|
|
388
420
|
export const createModelValue = (name: string): ModelValue => {
|
|
389
|
-
rankString(name);
|
|
390
421
|
const known = modelValues.get(name);
|
|
391
422
|
if (known) return known;
|
|
392
423
|
const value: ModelValue = {
|
|
393
424
|
kind: "ModelValue",
|
|
394
425
|
name,
|
|
395
426
|
type: name.length > 2 && name[1] === "_" ? name[0] : undefined,
|
|
427
|
+
rank: getStringRank(name),
|
|
396
428
|
};
|
|
397
429
|
modelValues.set(name, value);
|
|
398
430
|
return value;
|
|
@@ -403,6 +435,7 @@ export const createSortedSet = (elements: Value[]): SetValue => ({
|
|
|
403
435
|
elements,
|
|
404
436
|
hash: 0,
|
|
405
437
|
fingerprint: 0,
|
|
438
|
+
isFingerprinted: false,
|
|
406
439
|
});
|
|
407
440
|
|
|
408
441
|
export const createSortedFunction = (domain: Value[], range: Value[]): FunctionValue => ({
|
|
@@ -412,9 +445,104 @@ export const createSortedFunction = (domain: Value[], range: Value[]): FunctionV
|
|
|
412
445
|
hash: 0,
|
|
413
446
|
fingerprint: 0,
|
|
414
447
|
writtenKeys: undefined,
|
|
448
|
+
functionClass: undefined,
|
|
449
|
+
isFingerprinted: false,
|
|
415
450
|
});
|
|
416
451
|
|
|
452
|
+
// TLC words a failed function application by the Java class of the function: FcnLambdaValue for
|
|
453
|
+
// [x \in S |-> e], TupleValue for <<...>> and what the sequence operators build, RecordValue for
|
|
454
|
+
// [a |-> e], and FcnRcdValue for any other function.
|
|
455
|
+
export type FunctionClass = "lambda" | "tuple" | "record";
|
|
456
|
+
|
|
457
|
+
// Whether a lambda exists for markFingerprinted to find, so that a spec without one skips the walk.
|
|
458
|
+
const lambdaPresence = { isPresent: false };
|
|
459
|
+
|
|
460
|
+
export const setFunctionClass = <FunctionLike extends FunctionValue | LazyFunctionValue>(
|
|
461
|
+
functionValue: FunctionLike,
|
|
462
|
+
functionClass: FunctionClass | undefined,
|
|
463
|
+
) => {
|
|
464
|
+
if (functionClass === "lambda") lambdaPresence.isPresent = true;
|
|
465
|
+
if (functionClass !== undefined) functionValue.functionClass = functionClass;
|
|
466
|
+
return functionValue;
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
export const getFunctionClass = (value: Value) =>
|
|
470
|
+
typeof value === "object" && (value.kind === "Function" || value.kind === "LazyFunction")
|
|
471
|
+
? value.functionClass
|
|
472
|
+
: undefined;
|
|
473
|
+
|
|
474
|
+
export const markLambdaFunction = <FunctionLike extends FunctionValue | LazyFunctionValue>(
|
|
475
|
+
functionValue: FunctionLike,
|
|
476
|
+
) => setFunctionClass(functionValue, "lambda");
|
|
477
|
+
|
|
478
|
+
const fingerprintedValues = new WeakSet<ObjectValue>();
|
|
479
|
+
|
|
480
|
+
// The lazy sets TLC has cached the elements of. TLC caches a set as the receiver of compareTo,
|
|
481
|
+
// equals, size, and fingerPrint, not as their argument, and enumerates the sets an uncached one
|
|
482
|
+
// combines, duplicates and all.
|
|
483
|
+
const cachedSets = new WeakSet<ObjectValue>();
|
|
484
|
+
|
|
485
|
+
// Whether the spec has built a UNION or \cup set, the sets that stream duplicates, so that a spec
|
|
486
|
+
// without one skips tracking what TLC caches.
|
|
487
|
+
const streamedSetPresence = { isPresent: false };
|
|
488
|
+
|
|
489
|
+
export const markStreamedSet = <Streamed extends UnionValue | SetOperationValue>(
|
|
490
|
+
value: Streamed,
|
|
491
|
+
) => {
|
|
492
|
+
streamedSetPresence.isPresent = true;
|
|
493
|
+
return value;
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
// The sets that stream their elements apart from the normalized set until TLC caches them.
|
|
497
|
+
const STREAMING_KINDS = new Set<ObjectValue["kind"]>([
|
|
498
|
+
"Union",
|
|
499
|
+
"SetOperation",
|
|
500
|
+
"FilteredSet",
|
|
501
|
+
"ProductSet",
|
|
502
|
+
"RecordSet",
|
|
503
|
+
"FunctionSet",
|
|
504
|
+
]);
|
|
505
|
+
|
|
506
|
+
const cacheSet = (value: Value) => {
|
|
507
|
+
if (
|
|
508
|
+
streamedSetPresence.isPresent &&
|
|
509
|
+
typeof value === "object" &&
|
|
510
|
+
STREAMING_KINDS.has(value.kind)
|
|
511
|
+
) {
|
|
512
|
+
cachedSets.add(value);
|
|
513
|
+
}
|
|
514
|
+
};
|
|
515
|
+
|
|
516
|
+
// FcnLambdaValue#fingerPrint caches the FcnRcdValue the function enumerates to, which applies it
|
|
517
|
+
// from then on, so a function TLC has fingerprinted is no longer a lambda.
|
|
518
|
+
export const markFingerprinted = (value: Value): void => {
|
|
519
|
+
if ((!lambdaPresence.isPresent && !streamedSetPresence.isPresent) || typeof value !== "object") {
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
if (value.kind === "Function" || value.kind === "Set") {
|
|
523
|
+
if (value.isFingerprinted) return;
|
|
524
|
+
value.isFingerprinted = true;
|
|
525
|
+
} else {
|
|
526
|
+
if (fingerprintedValues.has(value)) return;
|
|
527
|
+
fingerprintedValues.add(value);
|
|
528
|
+
}
|
|
529
|
+
if (value.kind === "Function") {
|
|
530
|
+
if (value.functionClass === "lambda") value.functionClass = undefined;
|
|
531
|
+
value.domain.forEach(markFingerprinted);
|
|
532
|
+
value.range.forEach(markFingerprinted);
|
|
533
|
+
} else if (value.kind === "Set" || value.kind === "MixedSet") {
|
|
534
|
+
value.elements.forEach(markFingerprinted);
|
|
535
|
+
} else if (isSetValue(value) && isEnumerable(value)) {
|
|
536
|
+
cacheSet(value);
|
|
537
|
+
toFiniteSet(value).elements.forEach(markFingerprinted);
|
|
538
|
+
}
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
export const createSortedRecord = (names: Value[], range: Value[]) =>
|
|
542
|
+
setFunctionClass(createSortedFunction(names, range), "record");
|
|
543
|
+
|
|
417
544
|
export const valuesEqual = (left: Value, right: Value): boolean => {
|
|
545
|
+
cacheSet(left);
|
|
418
546
|
if (left === right) {
|
|
419
547
|
if (writtenOrderUse.isUsed) forgetWrittenOrder(left);
|
|
420
548
|
return left !== ANY_SET || compareAny(left);
|
|
@@ -488,28 +616,36 @@ export const valuesEqual = (left: Value, right: Value): boolean => {
|
|
|
488
616
|
return compareValues(left, right) === 0;
|
|
489
617
|
};
|
|
490
618
|
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
(
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
(
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
619
|
+
// TLC prints and words errors about an equal function by its Java class, so a value can stand for
|
|
620
|
+
// another in a state only if each function in it has the same class.
|
|
621
|
+
const createSameValueCheck = (isClassSensitive: boolean) => {
|
|
622
|
+
const areSameValues = (left: Value[], right: Value[]) =>
|
|
623
|
+
left === right ||
|
|
624
|
+
(left.length === right.length && left.every((element, index) => isSame(element, right[index])));
|
|
625
|
+
const isSame = (left: Value, right: Value): boolean => {
|
|
626
|
+
if (left === right) return true;
|
|
627
|
+
if (typeof left !== "object" || typeof right !== "object") return false;
|
|
628
|
+
if (left.kind === "Set" && right.kind === "Set")
|
|
629
|
+
return (
|
|
630
|
+
(left.hash === 0 || right.hash === 0 || left.hash === right.hash) &&
|
|
631
|
+
areSameValues(left.elements, right.elements)
|
|
632
|
+
);
|
|
633
|
+
if (left.kind === "Function" && right.kind === "Function")
|
|
634
|
+
return (
|
|
635
|
+
(!isClassSensitive || left.functionClass === right.functionClass) &&
|
|
636
|
+
(left.hash === 0 || right.hash === 0 || left.hash === right.hash) &&
|
|
637
|
+
areSameValues(left.domain, right.domain) &&
|
|
638
|
+
areSameValues(left.range, right.range)
|
|
639
|
+
);
|
|
640
|
+
return getValueKey(left) === getValueKey(right);
|
|
641
|
+
};
|
|
642
|
+
return isSame;
|
|
511
643
|
};
|
|
512
644
|
|
|
645
|
+
export const isSameValue = createSameValueCheck(false);
|
|
646
|
+
|
|
647
|
+
export const isInterchangeableValue = createSameValueCheck(true);
|
|
648
|
+
|
|
513
649
|
// The comparisons above fail with their own messages. These follow the compareTo and equals
|
|
514
650
|
// methods of TLC's Java value classes, which word a failure by the class of the left operand, so a
|
|
515
651
|
// failed comparison runs again through them to fail as TLC does.
|
|
@@ -562,7 +698,7 @@ const equalsModelValueWith = (modelValue: ModelValue, other: Value) => {
|
|
|
562
698
|
|
|
563
699
|
const compareModelValue = (left: ModelValue, right: Value) => {
|
|
564
700
|
const type = getModelValueType(left);
|
|
565
|
-
if (type === undefined) return isModelValue(right) ?
|
|
701
|
+
if (type === undefined) return isModelValue(right) ? left.rank - right.rank : -1;
|
|
566
702
|
if (!isModelValue(right)) {
|
|
567
703
|
throw new TlaEvaluationError(
|
|
568
704
|
`Attempted to compare the typed model value ${formatValue(left)} and non-model value\n${formatValue(right)}`,
|
|
@@ -574,7 +710,7 @@ const compareModelValue = (left: ModelValue, right: Value) => {
|
|
|
574
710
|
`Attempted to compare the differently-typed model values ${formatValue(left)} and ${formatValue(right)}`,
|
|
575
711
|
);
|
|
576
712
|
}
|
|
577
|
-
return
|
|
713
|
+
return left.rank - right.rank;
|
|
578
714
|
};
|
|
579
715
|
|
|
580
716
|
const equalsModelValue = (left: ModelValue, right: Value) => {
|
|
@@ -665,7 +801,7 @@ const compareFunctionLikeTlc = (left: FunctionValue | LazyFunctionValue, right:
|
|
|
665
801
|
compareElementsLikeTlc(functionValue.range, tuple)
|
|
666
802
|
);
|
|
667
803
|
}
|
|
668
|
-
if (left
|
|
804
|
+
if (getFunctionClass(left) === "record") {
|
|
669
805
|
const record = toTlcRecord(right);
|
|
670
806
|
if (!record) {
|
|
671
807
|
if (isModelValue(right)) return compareModelValueWith(right, left);
|
|
@@ -783,7 +919,7 @@ const equalsFunctionLikeTlc = (left: FunctionValue | LazyFunctionValue, right: V
|
|
|
783
919
|
equalsElementsLikeTlc(functionValue.range, tuple)
|
|
784
920
|
);
|
|
785
921
|
}
|
|
786
|
-
if (left
|
|
922
|
+
if (getFunctionClass(left) === "record") {
|
|
787
923
|
const record = toTlcRecord(right);
|
|
788
924
|
if (!record) {
|
|
789
925
|
if (isModelValue(right)) return equalsModelValueWith(right, left);
|
|
@@ -835,32 +971,62 @@ const equalsLikeTlc = (left: Value, right: Value): boolean => {
|
|
|
835
971
|
const sortLikeTlc = (elements: Value[]) => {
|
|
836
972
|
const sorted: Value[] = [];
|
|
837
973
|
for (const element of elements) {
|
|
838
|
-
const
|
|
839
|
-
|
|
840
|
-
const middle = bounds.low + ((bounds.high - bounds.low) >> 1);
|
|
841
|
-
bounds.comparison = compareLikeTlc(element, sorted[middle]);
|
|
842
|
-
if (bounds.comparison === 0) break;
|
|
843
|
-
if (bounds.comparison < 0) bounds.high = middle;
|
|
844
|
-
else bounds.low = middle + 1;
|
|
845
|
-
}
|
|
846
|
-
if (bounds.comparison !== 0) sorted.splice(bounds.low, 0, element);
|
|
974
|
+
const found = searchLikeTlc(element, sorted);
|
|
975
|
+
if (!found.isFound) sorted.splice(found.index, 0, element);
|
|
847
976
|
}
|
|
848
977
|
return sorted;
|
|
849
978
|
};
|
|
850
979
|
|
|
851
|
-
//
|
|
980
|
+
// Calls a method of a value as TLC does, whose failure the value heads when it has a source.
|
|
981
|
+
const callTracedMethod = <Result>(receiver: Value, method: () => Result) => {
|
|
982
|
+
const sets = setTrace.sets;
|
|
983
|
+
if (sets === undefined) return method();
|
|
984
|
+
sets.push(receiver);
|
|
985
|
+
const result = method();
|
|
986
|
+
sets.pop();
|
|
987
|
+
return result;
|
|
988
|
+
};
|
|
989
|
+
|
|
990
|
+
// ValueVec.search on sorted elements: a binary search with compareTo, which ends where the element
|
|
991
|
+
// is or would go.
|
|
992
|
+
const searchLikeTlc = (element: Value, sorted: Value[]) => {
|
|
993
|
+
const bounds = { low: 0, high: sorted.length };
|
|
994
|
+
while (bounds.low < bounds.high) {
|
|
995
|
+
const middle = bounds.low + ((bounds.high - bounds.low) >> 1);
|
|
996
|
+
const comparison = callTracedMethod(element, () => compareLikeTlc(element, sorted[middle]));
|
|
997
|
+
if (comparison === 0) return { index: middle, isFound: true };
|
|
998
|
+
if (comparison < 0) bounds.high = middle;
|
|
999
|
+
else bounds.low = middle + 1;
|
|
1000
|
+
}
|
|
1001
|
+
return { index: bounds.low, isFound: false };
|
|
1002
|
+
};
|
|
1003
|
+
|
|
1004
|
+
// TLC checks membership in a set enumeration it hasn't sorted element by element, with the equals
|
|
1005
|
+
// method of each member.
|
|
852
1006
|
const isElementOfLikeTlc = (element: Value, elements: Value[]) =>
|
|
853
|
-
elements.some((member) => equalsLikeTlc(member, element));
|
|
1007
|
+
elements.some((member) => callTracedMethod(member, () => equalsLikeTlc(member, element)));
|
|
854
1008
|
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
1009
|
+
// The sets TLC has normalized, whose membership ValueVec.search checks by binary search with
|
|
1010
|
+
// compareTo instead of element by element.
|
|
1011
|
+
const normalizedSets = new WeakSet<SetValue>();
|
|
1012
|
+
|
|
1013
|
+
// IValue.deepNormalize, which TLC applies to the constants of the config and the constant
|
|
1014
|
+
// definitions it computes as it starts.
|
|
1015
|
+
export const markNormalized = (value: Value): void => {
|
|
1016
|
+
if (typeof value !== "object") return;
|
|
1017
|
+
if (value.kind === "Set") {
|
|
1018
|
+
normalizedSets.add(value);
|
|
1019
|
+
value.elements.forEach(markNormalized);
|
|
1020
|
+
}
|
|
1021
|
+
if (value.kind === "Function") {
|
|
1022
|
+
value.domain.forEach(markNormalized);
|
|
1023
|
+
value.range.forEach(markNormalized);
|
|
861
1024
|
}
|
|
862
1025
|
};
|
|
863
1026
|
|
|
1027
|
+
export const isEqualLikeTlc = (left: Value, right: Value) =>
|
|
1028
|
+
computeLikeTlcOnFailure(valuesEqual, equalsLikeTlc, left, right);
|
|
1029
|
+
|
|
864
1030
|
const isStrictlySorted = (elements: Value[]) => {
|
|
865
1031
|
for (let index = 1; index < elements.length; index++) {
|
|
866
1032
|
if (compareValues(elements[index - 1], elements[index]) >= 0) return false;
|
|
@@ -869,6 +1035,8 @@ const isStrictlySorted = (elements: Value[]) => {
|
|
|
869
1035
|
};
|
|
870
1036
|
|
|
871
1037
|
export const createSet = (elements: Value[]): SetValue => {
|
|
1038
|
+
// ValueVec.sort compares each element after the first with those before it.
|
|
1039
|
+
for (let index = 1; index < elements.length; index++) cacheSet(elements[index]);
|
|
872
1040
|
if (elements.length < 2 || isStrictlySorted(elements)) return createSortedSet(elements);
|
|
873
1041
|
const sorted = [...elements].sort(compareValues);
|
|
874
1042
|
const unique = [sorted[0]];
|
|
@@ -880,14 +1048,13 @@ export const createSet = (elements: Value[]): SetValue => {
|
|
|
880
1048
|
|
|
881
1049
|
// TLC builds a set enumeration without sorting it, so one whose elements it can't compare still
|
|
882
1050
|
// evaluates.
|
|
883
|
-
export const createEnumeratedSet = (elements: Value[]): SetValue | MixedSetValue =>
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
};
|
|
1051
|
+
export const createEnumeratedSet = (elements: Value[]): SetValue | MixedSetValue =>
|
|
1052
|
+
computeLikeTlcOnFailure<Value[], undefined, SetValue | MixedSetValue>(
|
|
1053
|
+
createSet,
|
|
1054
|
+
(mixed) => ({ kind: "MixedSet", elements: mixed }),
|
|
1055
|
+
elements,
|
|
1056
|
+
undefined,
|
|
1057
|
+
);
|
|
891
1058
|
|
|
892
1059
|
export const EMPTY_SET = createSortedSet([]);
|
|
893
1060
|
|
|
@@ -966,12 +1133,15 @@ export const unionSorted = (left: SetValue, right: SetValue): SetValue => {
|
|
|
966
1133
|
}
|
|
967
1134
|
if (added.length === 0) return left;
|
|
968
1135
|
const union = createSortedSet(merged);
|
|
969
|
-
if (left.hash !== 0) {
|
|
1136
|
+
if (left.hash !== 0 && left.hash !== UNFINGERPRINTABLE_HASH) {
|
|
970
1137
|
let hash = left.hash;
|
|
971
|
-
|
|
972
|
-
|
|
1138
|
+
let index = 0;
|
|
1139
|
+
for (; index < added.length; index++) {
|
|
1140
|
+
const elementHash = getValueHash(added[index]);
|
|
1141
|
+
if (elementHash === UNFINGERPRINTABLE_HASH) break;
|
|
1142
|
+
hash = (hash + getElementContribution(elementHash)) | 0;
|
|
973
1143
|
}
|
|
974
|
-
union.hash = hash;
|
|
1144
|
+
if (index === added.length) union.hash = hash;
|
|
975
1145
|
}
|
|
976
1146
|
if (left.fingerprint !== 0) {
|
|
977
1147
|
let fingerprint = left.fingerprint;
|
|
@@ -1056,23 +1226,27 @@ const getIntervalValue = (high: number): IntervalValue => {
|
|
|
1056
1226
|
};
|
|
1057
1227
|
|
|
1058
1228
|
export const createTuple = (elements: Value[]): FunctionValue =>
|
|
1059
|
-
createSortedFunction(getIntervalDomain(elements.length), elements);
|
|
1229
|
+
setFunctionClass(createSortedFunction(getIntervalDomain(elements.length), elements), "tuple");
|
|
1060
1230
|
|
|
1061
|
-
export const createRecord = (fields: [string, Value][]): FunctionValue =>
|
|
1231
|
+
export const createRecord = (fields: [string, Value][]): FunctionValue =>
|
|
1232
|
+
setFunctionClass(createFunction(fields), "record");
|
|
1062
1233
|
|
|
1063
1234
|
// TLC's Enumerable classes: every set except the ones it implements in Java.
|
|
1064
|
-
const isTlcEnumerable = (value: Value) => isSetValue(value) && !isOverriddenSet(value);
|
|
1235
|
+
export const isTlcEnumerable = (value: Value) => isSetValue(value) && !isOverriddenSet(value);
|
|
1065
1236
|
|
|
1066
1237
|
const formatUnion = (value: UnionValue, isStrict = false) =>
|
|
1067
1238
|
`UNION(${formatValue(value.base, isStrict)})`;
|
|
1068
1239
|
|
|
1069
|
-
const getUnionSets = (
|
|
1240
|
+
const getUnionSets = (
|
|
1241
|
+
value: UnionValue,
|
|
1242
|
+
getElements = (set: Value) => toFiniteSet(set).elements,
|
|
1243
|
+
) => {
|
|
1070
1244
|
if (!isTlcEnumerable(value.base)) {
|
|
1071
1245
|
throw new TlaEvaluationError(
|
|
1072
1246
|
`Attempted to enumerate the nonenumerable set:\n${formatUnion(value)}`,
|
|
1073
1247
|
);
|
|
1074
1248
|
}
|
|
1075
|
-
return
|
|
1249
|
+
return getElements(value.base);
|
|
1076
1250
|
};
|
|
1077
1251
|
|
|
1078
1252
|
const getUnionSet = (set: Value, index: number, union: UnionValue) => {
|
|
@@ -1108,7 +1282,7 @@ export const isEnumerable = (value: Value): boolean => {
|
|
|
1108
1282
|
case "ProductSet":
|
|
1109
1283
|
return value.factors.every(isEnumerable) || getEmptiness(value) === true;
|
|
1110
1284
|
case "Union":
|
|
1111
|
-
return isEnumerable(value.base) &&
|
|
1285
|
+
return isEnumerable(value.base) && getStreamedElements(value.base).every(isEnumerable);
|
|
1112
1286
|
case "SetOperation":
|
|
1113
1287
|
if (value.operator === "\\cup") return isEnumerable(value.left) && isEnumerable(value.right);
|
|
1114
1288
|
if (value.operator === "\\cap") return isEnumerable(value.left) || isEnumerable(value.right);
|
|
@@ -1181,6 +1355,41 @@ const advanceCombination = (indices: number[], length: number) => {
|
|
|
1181
1355
|
return false;
|
|
1182
1356
|
};
|
|
1183
1357
|
|
|
1358
|
+
// TLC enumerates SUBSET S lazily, by size and then in the order of S, so a quantifier that finds
|
|
1359
|
+
// its witness early never builds the rest. For a power set too large to build, the elements are
|
|
1360
|
+
// computed in that order as a loop reads them.
|
|
1361
|
+
export const getLazySubsets = (value: Value): Value[] | undefined => {
|
|
1362
|
+
if (typeof value !== "object" || value.kind !== "Subset" || isOverriddenSet(value.base)) {
|
|
1363
|
+
return undefined;
|
|
1364
|
+
}
|
|
1365
|
+
const base = toFiniteSet(value.base).elements;
|
|
1366
|
+
if (base.length <= MAXIMUM_ENUMERATED_SUBSET_BASE) return undefined;
|
|
1367
|
+
const cursor = { index: -1, indices: [0] };
|
|
1368
|
+
const getSubset = (index: number) => {
|
|
1369
|
+
if (index <= cursor.index) cursor.index = -1;
|
|
1370
|
+
while (cursor.index < index) {
|
|
1371
|
+
if (cursor.index === -1) cursor.indices = [];
|
|
1372
|
+
else if (!advanceCombination(cursor.indices, base.length)) {
|
|
1373
|
+
cursor.indices = Array.from({ length: cursor.indices.length + 1 }, (_, item) => item);
|
|
1374
|
+
}
|
|
1375
|
+
cursor.index++;
|
|
1376
|
+
}
|
|
1377
|
+
return createSortedSet(cursor.indices.map((item) => base[item]));
|
|
1378
|
+
};
|
|
1379
|
+
const length = 2 ** base.length;
|
|
1380
|
+
const isIndex = (property: string | symbol) =>
|
|
1381
|
+
typeof property === "string" && /^\d+$/.test(property) && Number(property) < length;
|
|
1382
|
+
return new Proxy<Value[]>([], {
|
|
1383
|
+
get: (target, property, receiver) => {
|
|
1384
|
+
if (property === "length") return length;
|
|
1385
|
+
return isIndex(property)
|
|
1386
|
+
? getSubset(Number(property))
|
|
1387
|
+
: Reflect.get(target, property, receiver);
|
|
1388
|
+
},
|
|
1389
|
+
has: (target, property) => isIndex(property) || Reflect.has(target, property),
|
|
1390
|
+
});
|
|
1391
|
+
};
|
|
1392
|
+
|
|
1184
1393
|
const getSortedSubsets = (base: Value[]) => {
|
|
1185
1394
|
const subsets: Value[] = [];
|
|
1186
1395
|
for (let size = 0; size <= base.length; size++) {
|
|
@@ -1192,7 +1401,30 @@ const getSortedSubsets = (base: Value[]) => {
|
|
|
1192
1401
|
return subsets;
|
|
1193
1402
|
};
|
|
1194
1403
|
|
|
1195
|
-
|
|
1404
|
+
// The Enumerator of a set of records or tuples enumerates its sets in turn and stops at the first
|
|
1405
|
+
// empty one, which leaves the product empty. Later versions of TLC decide that the set is empty
|
|
1406
|
+
// before they enumerate it.
|
|
1407
|
+
const getComponentElements = (
|
|
1408
|
+
sets: Value[],
|
|
1409
|
+
getElements: (set: Value) => Value[],
|
|
1410
|
+
describeUnenumerable: (set: Value, index: number) => string,
|
|
1411
|
+
) => {
|
|
1412
|
+
const lists: Value[][] = [];
|
|
1413
|
+
for (let index = 0; index < sets.length; index++) {
|
|
1414
|
+
const set = sets[index];
|
|
1415
|
+
if (isOverriddenSet(set)) throw new TlaEvaluationError(describeUnenumerable(set, index));
|
|
1416
|
+
const elements = getElements(set);
|
|
1417
|
+
if (elements.length === 0) return [[]];
|
|
1418
|
+
lists.push(elements);
|
|
1419
|
+
}
|
|
1420
|
+
return lists;
|
|
1421
|
+
};
|
|
1422
|
+
|
|
1423
|
+
// getElements enumerates the sets a set of functions, records, or tuples combines.
|
|
1424
|
+
const enumerateSet = (
|
|
1425
|
+
value: Value,
|
|
1426
|
+
getElements = (set: Value) => toFiniteSet(set).elements,
|
|
1427
|
+
): Value[] => {
|
|
1196
1428
|
if (typeof value !== "object")
|
|
1197
1429
|
throw new TlaEvaluationError(`Expected a set but found ${formatValue(value)}`);
|
|
1198
1430
|
switch (value.kind) {
|
|
@@ -1206,61 +1438,63 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1206
1438
|
return elements;
|
|
1207
1439
|
}
|
|
1208
1440
|
case "Subset": {
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
if (base
|
|
1441
|
+
// SubsetValue.toSetEnum sizes the set before it enumerates it, and enumerates only the empty
|
|
1442
|
+
// set of a value of size 0, a function with an empty domain among them.
|
|
1443
|
+
if (getCardinality(value.base) === 0) return getSortedSubsets([]);
|
|
1444
|
+
if (!isSetValue(value.base)) {
|
|
1212
1445
|
throw new TlaEvaluationError(
|
|
1213
|
-
`
|
|
1446
|
+
`Attempted to compute the value of an expression of form\nSUBSET S, but S is a non-enumerable value:\n${prettyPrint(formatValue(value.base))}`,
|
|
1214
1447
|
);
|
|
1215
1448
|
}
|
|
1449
|
+
const base = toFiniteSet(value.base).elements;
|
|
1450
|
+
if (base.length > MAXIMUM_ENUMERATED_SUBSET_BASE) {
|
|
1451
|
+
throw new TlaEvaluationError(describeOverflow(value));
|
|
1452
|
+
}
|
|
1216
1453
|
return getSortedSubsets(base);
|
|
1217
1454
|
}
|
|
1218
1455
|
case "FunctionSet": {
|
|
1219
|
-
if (isEmptySet(value)) return [];
|
|
1456
|
+
if (!valueVersion.is174 && isEmptySet(value)) return [];
|
|
1220
1457
|
if (isOverriddenSet(value.domain)) {
|
|
1221
1458
|
throw new TlaEvaluationError(
|
|
1222
1459
|
`Attempted to enumerate a set of the form [D -> R],but the domain D:\n${formatValue(value.domain)}\ncannot be enumerated.`,
|
|
1223
1460
|
);
|
|
1224
1461
|
}
|
|
1225
1462
|
const domain = getFunctionDomain(value.domain);
|
|
1226
|
-
|
|
1463
|
+
// TLC 1.7.4 requires an enumerable range even of an empty domain.
|
|
1464
|
+
if ((domain.length > 0 || valueVersion.is174) && isOverriddenSet(value.range)) {
|
|
1227
1465
|
throw new TlaEvaluationError(
|
|
1228
1466
|
`Attempted to enumerate a set of the form [D -> R],but the range R:\n${formatValue(value.range)}\ncannot be enumerated.`,
|
|
1229
1467
|
);
|
|
1230
1468
|
}
|
|
1231
|
-
const range = domain.length === 0 ? [] :
|
|
1469
|
+
const range = domain.length === 0 ? [] : getElements(value.range);
|
|
1232
1470
|
return getCartesianProduct(domain.map(() => range)).map((combination): Value =>
|
|
1233
1471
|
createSortedFunction(domain, combination),
|
|
1234
1472
|
);
|
|
1235
1473
|
}
|
|
1236
1474
|
case "RecordSet": {
|
|
1237
|
-
if (isEmptySet(value)) return [];
|
|
1238
|
-
const fields =
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
)
|
|
1243
|
-
|
|
1244
|
-
return toFiniteSet(set).elements;
|
|
1245
|
-
});
|
|
1475
|
+
if (!valueVersion.is174 && isEmptySet(value)) return [];
|
|
1476
|
+
const fields = getComponentElements(
|
|
1477
|
+
value.sets,
|
|
1478
|
+
getElements,
|
|
1479
|
+
(set, index) =>
|
|
1480
|
+
`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)}`,
|
|
1481
|
+
);
|
|
1246
1482
|
return getCartesianProduct(fields).map((combination) =>
|
|
1247
1483
|
createRecord(value.names.map((name, index): [string, Value] => [name, combination[index]])),
|
|
1248
1484
|
);
|
|
1249
1485
|
}
|
|
1250
1486
|
case "ProductSet": {
|
|
1251
|
-
if (isEmptySet(value)) return [];
|
|
1252
|
-
const factors =
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
)
|
|
1257
|
-
|
|
1258
|
-
return toFiniteSet(factor).elements;
|
|
1259
|
-
});
|
|
1487
|
+
if (!valueVersion.is174 && isEmptySet(value)) return [];
|
|
1488
|
+
const factors = getComponentElements(
|
|
1489
|
+
value.factors,
|
|
1490
|
+
getElements,
|
|
1491
|
+
(factor, index) =>
|
|
1492
|
+
`Attempted to enumerate a set of the form s1 \\X s2 ... \\X sn,\nbut can't enumerate s${index}:\n${formatValue(factor)}`,
|
|
1493
|
+
);
|
|
1260
1494
|
return getCartesianProduct(factors).map(createTuple);
|
|
1261
1495
|
}
|
|
1262
1496
|
case "FilteredSet":
|
|
1263
|
-
if (isOverriddenSet(value.base)) {
|
|
1497
|
+
if (!isSetValue(value.base) || isOverriddenSet(value.base)) {
|
|
1264
1498
|
throw new TlaEvaluationError(
|
|
1265
1499
|
`Attempted to enumerate { x \\in S : p(x) } when S:\n${formatValue(value.base)}\nis not enumerable`,
|
|
1266
1500
|
);
|
|
@@ -1275,7 +1509,7 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1275
1509
|
case "SetOperation": {
|
|
1276
1510
|
if (
|
|
1277
1511
|
value.operator === "\\cup" &&
|
|
1278
|
-
(
|
|
1512
|
+
(!isTlcEnumerable(value.left) || !isTlcEnumerable(value.right))
|
|
1279
1513
|
) {
|
|
1280
1514
|
throw new TlaEvaluationError(
|
|
1281
1515
|
`Attempted to enumerate S \\cup T when S:\n${formatValue(value.left)}\nand T:\n${formatValue(value.right)}\nare not both enumerable`,
|
|
@@ -1283,21 +1517,21 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1283
1517
|
}
|
|
1284
1518
|
if (
|
|
1285
1519
|
value.operator === "\\cap" &&
|
|
1286
|
-
|
|
1287
|
-
|
|
1520
|
+
!isTlcEnumerable(value.left) &&
|
|
1521
|
+
!isTlcEnumerable(value.right)
|
|
1288
1522
|
) {
|
|
1289
1523
|
throw new TlaEvaluationError(
|
|
1290
1524
|
`Attempted to enumerate S \\cap T when neither S:\n${formatValue(value.left)}\nnor T:\n${formatValue(value.right)}\nis enumerable`,
|
|
1291
1525
|
);
|
|
1292
1526
|
}
|
|
1293
|
-
if (value.operator === "\\" &&
|
|
1527
|
+
if (value.operator === "\\" && !isTlcEnumerable(value.left)) {
|
|
1294
1528
|
throw new TlaEvaluationError(
|
|
1295
1529
|
`Attempted to enumerate S \\ T when S:\n${formatValue(value.left)}\nis not enumerable.`,
|
|
1296
1530
|
);
|
|
1297
1531
|
}
|
|
1298
1532
|
if (value.operator === "\\cup")
|
|
1299
1533
|
return unionSorted(toFiniteSet(value.left), toFiniteSet(value.right)).elements;
|
|
1300
|
-
const isLeftEnumerated = value.operator === "\\" ||
|
|
1534
|
+
const isLeftEnumerated = value.operator === "\\" || isTlcEnumerable(value.left);
|
|
1301
1535
|
const enumerated = isLeftEnumerated ? value.left : value.right;
|
|
1302
1536
|
const other = isLeftEnumerated ? value.right : value.left;
|
|
1303
1537
|
return toFiniteSet(enumerated).elements.filter(
|
|
@@ -1323,7 +1557,10 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1323
1557
|
}
|
|
1324
1558
|
const cached = typeof value === "object" ? finiteSets.get(value) : undefined;
|
|
1325
1559
|
if (cached) return cached;
|
|
1560
|
+
const sets = setTrace.sets;
|
|
1561
|
+
sets?.push(value);
|
|
1326
1562
|
const elements = enumerateSet(value);
|
|
1563
|
+
sets?.pop();
|
|
1327
1564
|
const isSorted =
|
|
1328
1565
|
typeof value === "object" &&
|
|
1329
1566
|
(value.kind === "MixedSet" ||
|
|
@@ -1341,25 +1578,60 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1341
1578
|
// TLC's Enumerator for a lazy set streams the sets it combines until the set caches its elements,
|
|
1342
1579
|
// so an element that two of them share comes up once for each.
|
|
1343
1580
|
export const getStreamedElements = (value: Value): Value[] => {
|
|
1344
|
-
if (typeof value !== "object" ||
|
|
1581
|
+
if (typeof value !== "object" || cachedSets.has(value)) return toFiniteSet(value).elements;
|
|
1582
|
+
const sets = setTrace.sets;
|
|
1583
|
+
sets?.push(value);
|
|
1584
|
+
const elements = streamElements(value);
|
|
1585
|
+
sets?.pop();
|
|
1586
|
+
return elements;
|
|
1587
|
+
};
|
|
1588
|
+
|
|
1589
|
+
const getCombinedSets = (value: ObjectValue) =>
|
|
1590
|
+
value.kind === "ProductSet"
|
|
1591
|
+
? value.factors
|
|
1592
|
+
: value.kind === "RecordSet"
|
|
1593
|
+
? value.sets
|
|
1594
|
+
: value.kind === "FunctionSet"
|
|
1595
|
+
? [value.range]
|
|
1596
|
+
: undefined;
|
|
1597
|
+
|
|
1598
|
+
// Whether a set streams its elements in another order than the normalized set, so that a set of
|
|
1599
|
+
// tuples, records, or functions it is a component of has to stream too.
|
|
1600
|
+
const isStreamedApart = (value: Value): boolean => {
|
|
1601
|
+
if (typeof value !== "object" || cachedSets.has(value)) return false;
|
|
1602
|
+
if (value.kind === "Union") return true;
|
|
1603
|
+
if (value.kind === "FilteredSet") return isStreamedApart(value.base);
|
|
1604
|
+
if (value.kind === "SetOperation") {
|
|
1605
|
+
return (
|
|
1606
|
+
value.operator === "\\cup" || isStreamedApart(value.left) || isStreamedApart(value.right)
|
|
1607
|
+
);
|
|
1608
|
+
}
|
|
1609
|
+
return getCombinedSets(value)?.some(isStreamedApart) ?? false;
|
|
1610
|
+
};
|
|
1611
|
+
|
|
1612
|
+
const streamElements = (value: ObjectValue): Value[] => {
|
|
1613
|
+
// TLC streams the sets of a UNION it hasn't enumerated, so a set that appears twice there yields
|
|
1614
|
+
// its elements twice.
|
|
1345
1615
|
if (value.kind === "Union") {
|
|
1346
|
-
return getUnionSets(value).flatMap((set, index) =>
|
|
1616
|
+
return getUnionSets(value, getStreamedElements).flatMap((set, index) =>
|
|
1347
1617
|
getStreamedElements(getUnionSet(set, index, value)),
|
|
1348
1618
|
);
|
|
1349
1619
|
}
|
|
1350
1620
|
if (value.kind === "FilteredSet" && !isOverriddenSet(value.base)) {
|
|
1351
1621
|
return getStreamedElements(value.base).filter(value.test);
|
|
1352
1622
|
}
|
|
1623
|
+
const components = getCombinedSets(value);
|
|
1624
|
+
if (components?.some(isStreamedApart)) return enumerateSet(value, getStreamedElements);
|
|
1353
1625
|
if (value.kind !== "SetOperation") return toFiniteSet(value).elements;
|
|
1354
1626
|
if (value.operator === "\\cup") {
|
|
1355
|
-
return
|
|
1627
|
+
return !isTlcEnumerable(value.left) || !isTlcEnumerable(value.right)
|
|
1356
1628
|
? toFiniteSet(value).elements
|
|
1357
1629
|
: [...getStreamedElements(value.left), ...getStreamedElements(value.right)];
|
|
1358
1630
|
}
|
|
1359
|
-
const isLeftEnumerated = value.operator === "\\" ||
|
|
1631
|
+
const isLeftEnumerated = value.operator === "\\" || isTlcEnumerable(value.left);
|
|
1360
1632
|
const enumerated = isLeftEnumerated ? value.left : value.right;
|
|
1361
1633
|
const other = isLeftEnumerated ? value.right : value.left;
|
|
1362
|
-
if (
|
|
1634
|
+
if (!isTlcEnumerable(enumerated)) return toFiniteSet(value).elements;
|
|
1363
1635
|
return getStreamedElements(enumerated).filter(
|
|
1364
1636
|
(element) => isElementOf(element, other) === (value.operator === "\\cap"),
|
|
1365
1637
|
);
|
|
@@ -1396,9 +1668,13 @@ export const isEmptySet = (value: Value): boolean => {
|
|
|
1396
1668
|
}
|
|
1397
1669
|
};
|
|
1398
1670
|
|
|
1399
|
-
const getProductCardinality = (
|
|
1671
|
+
const getProductCardinality = (
|
|
1672
|
+
sets: Value[],
|
|
1673
|
+
version: TlcVersion | undefined,
|
|
1674
|
+
describe: () => string,
|
|
1675
|
+
) =>
|
|
1400
1676
|
sets.reduce<number>((product, set) => {
|
|
1401
|
-
const size = product * getCardinality(set);
|
|
1677
|
+
const size = product * getCardinality(set, version);
|
|
1402
1678
|
if (size > MAXIMUM_INTEGER) throw new TlaEvaluationError(describe());
|
|
1403
1679
|
return size;
|
|
1404
1680
|
}, 1);
|
|
@@ -1406,9 +1682,25 @@ const getProductCardinality = (sets: Value[], describe: () => string) =>
|
|
|
1406
1682
|
const describeOverflow = (value: Value) =>
|
|
1407
1683
|
`Overflow when computing the number of elements in:\n${formatValue(value)}`;
|
|
1408
1684
|
|
|
1409
|
-
|
|
1410
|
-
|
|
1685
|
+
// TLC 1.7.4 sizes every component of a set of functions, records, or tuples without first
|
|
1686
|
+
// checking whether one is empty, so an infinite component fails even beside an empty one.
|
|
1687
|
+
export const getCardinality = (value: Value, version?: TlcVersion): number => {
|
|
1688
|
+
if (typeof value !== "object") {
|
|
1689
|
+
const kind =
|
|
1690
|
+
typeof value === "number" ? "integer" : typeof value === "string" ? "string" : "boolean";
|
|
1691
|
+
throw new TlaEvaluationError(
|
|
1692
|
+
`Attempted to compute the number of elements in the ${kind} ${formatValue(value)}.`,
|
|
1693
|
+
);
|
|
1694
|
+
}
|
|
1411
1695
|
switch (value.kind) {
|
|
1696
|
+
case "ModelValue":
|
|
1697
|
+
throw new TlaEvaluationError(
|
|
1698
|
+
`Attempted to compute the number of elements in the model value ${formatValue(value)}.`,
|
|
1699
|
+
);
|
|
1700
|
+
case "Function":
|
|
1701
|
+
return value.domain.length;
|
|
1702
|
+
case "LazyFunction":
|
|
1703
|
+
return getCardinality(value.domain, version);
|
|
1412
1704
|
case "Interval": {
|
|
1413
1705
|
const size = Math.max(0, value.high - value.low + 1);
|
|
1414
1706
|
if (size > MAXIMUM_INTEGER) {
|
|
@@ -1424,16 +1716,19 @@ export const getCardinality = (value: Value): number => {
|
|
|
1424
1716
|
`Attempted to compute the number of elements in the overridden value ${formatValue(value)}.`,
|
|
1425
1717
|
);
|
|
1426
1718
|
case "Subset": {
|
|
1427
|
-
const size = getCardinality(value.base);
|
|
1719
|
+
const size = getCardinality(value.base, version);
|
|
1428
1720
|
if (size >= 31) throw new TlaEvaluationError(describeOverflow(value));
|
|
1429
1721
|
return 2 ** size;
|
|
1430
1722
|
}
|
|
1431
1723
|
case "FunctionSet": {
|
|
1432
|
-
if (
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1724
|
+
if (version !== "1.7.4") {
|
|
1725
|
+
if (isEmptySet(value.domain)) return 1;
|
|
1726
|
+
if (isEmptySet(value.range)) return 0;
|
|
1727
|
+
if (getCardinality(value.range, version) === 1) return 1;
|
|
1728
|
+
}
|
|
1729
|
+
const domainSize = getCardinality(value.domain, version);
|
|
1730
|
+
const rangeSize = getCardinality(value.range, version);
|
|
1731
|
+
if (rangeSize <= 1) return domainSize === 0 ? 1 : rangeSize;
|
|
1437
1732
|
const size = { value: 1 };
|
|
1438
1733
|
for (let index = 0; index < domainSize; index++) {
|
|
1439
1734
|
size.value *= rangeSize;
|
|
@@ -1442,15 +1737,17 @@ export const getCardinality = (value: Value): number => {
|
|
|
1442
1737
|
return size.value;
|
|
1443
1738
|
}
|
|
1444
1739
|
case "RecordSet":
|
|
1445
|
-
if (isEmptySet(value)) return 0;
|
|
1446
|
-
return getProductCardinality(value.sets, () => describeOverflow(value));
|
|
1740
|
+
if (version !== "1.7.4" && isEmptySet(value)) return 0;
|
|
1741
|
+
return getProductCardinality(value.sets, version, () => describeOverflow(value));
|
|
1447
1742
|
case "ProductSet":
|
|
1448
|
-
if (isEmptySet(value)) return 0;
|
|
1743
|
+
if (version !== "1.7.4" && isEmptySet(value)) return 0;
|
|
1449
1744
|
return getProductCardinality(
|
|
1450
1745
|
value.factors,
|
|
1746
|
+
version,
|
|
1451
1747
|
() => `Overflow when computing the number of elements in ${formatValue(value)}`,
|
|
1452
1748
|
);
|
|
1453
1749
|
default:
|
|
1750
|
+
cacheSet(value);
|
|
1454
1751
|
return toFiniteSet(value).elements.length;
|
|
1455
1752
|
}
|
|
1456
1753
|
};
|
|
@@ -1525,11 +1822,11 @@ export const isFiniteSet = (value: Value): boolean => {
|
|
|
1525
1822
|
}
|
|
1526
1823
|
};
|
|
1527
1824
|
|
|
1528
|
-
const findIndex = (sortedValues: Value[], target: Value) => {
|
|
1825
|
+
const findIndex = (sortedValues: Value[], target: Value, compare = compareValues) => {
|
|
1529
1826
|
const bounds = { low: 0, high: sortedValues.length - 1 };
|
|
1530
1827
|
while (bounds.low <= bounds.high) {
|
|
1531
1828
|
const middle = (bounds.low + bounds.high) >> 1;
|
|
1532
|
-
const comparison =
|
|
1829
|
+
const comparison = compare(sortedValues[middle], target);
|
|
1533
1830
|
if (comparison === 0) return middle;
|
|
1534
1831
|
if (comparison < 0) bounds.low = middle + 1;
|
|
1535
1832
|
else bounds.high = middle - 1;
|
|
@@ -1545,33 +1842,82 @@ export const getSequenceElements = (value: Value): Value[] | undefined => {
|
|
|
1545
1842
|
: undefined;
|
|
1546
1843
|
};
|
|
1547
1844
|
|
|
1548
|
-
const describeMembership = (element: Value, set: Value) =>
|
|
1549
|
-
`Attempted to check if the value ${formatValue(element)} is an element of ${formatValue(set)}`;
|
|
1550
|
-
|
|
1551
1845
|
const describeNonSetMembership = (element: Value, value: Value) => {
|
|
1552
1846
|
const prefix = `Attempted to check if the value:\n${formatValue(element)}\nis an element of the`;
|
|
1553
1847
|
if (!isFunctionValue(value)) return `${prefix} ${describeKind(value)} ${formatValue(value)}`;
|
|
1554
|
-
const
|
|
1555
|
-
|
|
1556
|
-
if (domain.length > 0 && domain.every((key) => typeof key === "string")) {
|
|
1848
|
+
const functionClass = getFunctionClass(value);
|
|
1849
|
+
if (functionClass === "record") {
|
|
1557
1850
|
return `Attempted to check if element:\n${formatValue(element)}\nis in the record:\n${formatValue(value)}`;
|
|
1558
1851
|
}
|
|
1559
|
-
if (
|
|
1560
|
-
return "Attempted to check set membership in a tuple value.";
|
|
1561
|
-
}
|
|
1852
|
+
if (functionClass === "tuple") return "Attempted to check set membership in a tuple value.";
|
|
1562
1853
|
return `${prefix} function ${formatValue(value)}`;
|
|
1563
1854
|
};
|
|
1564
1855
|
|
|
1856
|
+
// The call stack of the replay, which a failed fast path leaves frozen.
|
|
1857
|
+
interface TracedCallStack {
|
|
1858
|
+
expressions: unknown[];
|
|
1859
|
+
isFrozen: boolean;
|
|
1860
|
+
fingerprint?: unknown;
|
|
1861
|
+
}
|
|
1862
|
+
|
|
1863
|
+
// The sets whose membership is being checked or whose elements are being enumerated, and the
|
|
1864
|
+
// members being compared, outermost first, while TLC's CallStackTool replays a computation. A failed
|
|
1865
|
+
// check or enumeration leaves the values it was in, each of which TLC adds to the
|
|
1866
|
+
// FingerprintException when it has a source.
|
|
1867
|
+
export const setTrace: { sets: Value[] | undefined; callStack?: TracedCallStack } = {
|
|
1868
|
+
sets: undefined,
|
|
1869
|
+
};
|
|
1870
|
+
|
|
1871
|
+
// Takes a fast path, and computes as TLC does where it fails. TLC never takes the fast path, so
|
|
1872
|
+
// the failure's leftovers in the replay's trace go first. Both paths take their operands as
|
|
1873
|
+
// arguments, so a hot caller passes functions it doesn't allocate.
|
|
1874
|
+
export const computeLikeTlcOnFailure = <Left, Right, Result>(
|
|
1875
|
+
compute: (left: Left, right: Right) => Result,
|
|
1876
|
+
computeLikeTlc: (left: Left, right: Right, error: TlaEvaluationError) => Result,
|
|
1877
|
+
left: Left,
|
|
1878
|
+
right: Right,
|
|
1879
|
+
): Result => {
|
|
1880
|
+
const sets = setTrace.sets;
|
|
1881
|
+
const callStack = setTrace.callStack;
|
|
1882
|
+
const depth = sets?.length ?? 0;
|
|
1883
|
+
const trace = callStack && { ...callStack, length: callStack.expressions.length };
|
|
1884
|
+
try {
|
|
1885
|
+
return compute(left, right);
|
|
1886
|
+
} catch (error) {
|
|
1887
|
+
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
1888
|
+
if (sets) sets.length = depth;
|
|
1889
|
+
if (callStack && trace) {
|
|
1890
|
+
callStack.expressions.length = trace.length;
|
|
1891
|
+
callStack.isFrozen = trace.isFrozen;
|
|
1892
|
+
callStack.fingerprint = trace.fingerprint;
|
|
1893
|
+
}
|
|
1894
|
+
return computeLikeTlc(left, right, error);
|
|
1895
|
+
}
|
|
1896
|
+
};
|
|
1897
|
+
|
|
1565
1898
|
export const isElementOf = (element: Value, set: Value): boolean => {
|
|
1899
|
+
const sets = setTrace.sets;
|
|
1900
|
+
if (sets === undefined) return isElementOfSet(element, set);
|
|
1901
|
+
sets.push(set);
|
|
1902
|
+
const isElement = isElementOfSet(element, set);
|
|
1903
|
+
sets.pop();
|
|
1904
|
+
return isElement;
|
|
1905
|
+
};
|
|
1906
|
+
|
|
1907
|
+
const isInSortedSet = (element: Value, set: SetValue) => findIndex(set.elements, element) !== -1;
|
|
1908
|
+
|
|
1909
|
+
const isInSortedSetLikeTlc = (element: Value, set: SetValue) =>
|
|
1910
|
+
normalizedSets.has(set)
|
|
1911
|
+
? searchLikeTlc(element, set.elements).isFound
|
|
1912
|
+
: isElementOfLikeTlc(element, set.elements);
|
|
1913
|
+
|
|
1914
|
+
const isElementOfSet = (element: Value, set: Value): boolean => {
|
|
1566
1915
|
if (!isSetValue(set)) throw new TlaEvaluationError(describeNonSetMembership(element, set));
|
|
1567
1916
|
switch (set.kind) {
|
|
1568
1917
|
case "Set":
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
1573
|
-
return isElementOfLikeTlc(element, set.elements);
|
|
1574
|
-
}
|
|
1918
|
+
// SetEnumValue.member compares the element with the set's.
|
|
1919
|
+
cacheSet(element);
|
|
1920
|
+
return computeLikeTlcOnFailure(isInSortedSet, isInSortedSetLikeTlc, element, set);
|
|
1575
1921
|
case "MixedSet":
|
|
1576
1922
|
return isElementOfLikeTlc(element, set.elements);
|
|
1577
1923
|
case "Interval":
|
|
@@ -1595,12 +1941,21 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1595
1941
|
return set.name !== "Nat" || (typeof element === "number" && element >= 0);
|
|
1596
1942
|
}
|
|
1597
1943
|
case "Subset":
|
|
1598
|
-
|
|
1944
|
+
// SubsetValue.member fails on any value that is not Enumerable, model values and the sets a
|
|
1945
|
+
// Java class overrides included.
|
|
1946
|
+
if (!isSetValue(element) || isOverriddenSet(element)) {
|
|
1947
|
+
throw new TlaEvaluationError(
|
|
1948
|
+
`Attempted to check if the non-enumerable value\n${prettyPrint(formatValue(element))}\nis element of\n${prettyPrint(formatValue(set))}`,
|
|
1949
|
+
);
|
|
1950
|
+
}
|
|
1599
1951
|
return toFiniteSet(element).elements.every((item) => isElementOf(item, set.base));
|
|
1600
1952
|
case "FunctionSet": {
|
|
1601
1953
|
if (!isFunctionValue(element)) {
|
|
1602
1954
|
if (isModelValue(element)) return false;
|
|
1603
|
-
|
|
1955
|
+
// SetOfFcnsValue.member prints the element without Values.ppr.
|
|
1956
|
+
throw new TlaEvaluationError(
|
|
1957
|
+
`Attempted to check if \n${formatValue(element)}\nwhich is not a TLC function value, is in the set of functions:\n${prettyPrint(formatValue(set))}`,
|
|
1958
|
+
);
|
|
1604
1959
|
}
|
|
1605
1960
|
const functionValue = toFiniteFunction(element);
|
|
1606
1961
|
const low = getIntervalLow(functionValue.domain);
|
|
@@ -1665,7 +2020,10 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1665
2020
|
const elements = getSequenceElements(element);
|
|
1666
2021
|
if (elements === undefined) {
|
|
1667
2022
|
if (isModelValue(element)) return false;
|
|
1668
|
-
|
|
2023
|
+
// Sequences.member raises TLC_MODULE_CHECK_MEMBER_OF.
|
|
2024
|
+
throw new TlaEvaluationError(
|
|
2025
|
+
`Attempted to check if the value:\n${prettyPrint(formatValue(element))}\nis an element of ${prettyPrint(formatValue(set))}.`,
|
|
2026
|
+
);
|
|
1669
2027
|
}
|
|
1670
2028
|
return elements.every((item) => isElementOf(item, set.base));
|
|
1671
2029
|
}
|
|
@@ -1698,50 +2056,122 @@ export const toFiniteFunction = (value: Value): FunctionValue => {
|
|
|
1698
2056
|
if (typeof value === "object" && value.kind === "LazyFunction") {
|
|
1699
2057
|
if (!isEnumerable(value.domain)) getCardinality(value.domain);
|
|
1700
2058
|
const domain = getFunctionDomain(value.domain);
|
|
1701
|
-
return
|
|
1702
|
-
|
|
1703
|
-
|
|
2059
|
+
return setFunctionClass(
|
|
2060
|
+
createSortedFunction(
|
|
2061
|
+
domain,
|
|
2062
|
+
domain.map((key) => value.apply(key)),
|
|
2063
|
+
),
|
|
2064
|
+
getFunctionClass(value),
|
|
1704
2065
|
);
|
|
1705
2066
|
}
|
|
1706
2067
|
throw new TlaEvaluationError(`Expected a function but found ${formatValue(value)}`);
|
|
1707
2068
|
};
|
|
1708
2069
|
|
|
1709
|
-
const
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
2070
|
+
export const describeMissingArgument = (
|
|
2071
|
+
functionValue: Value,
|
|
2072
|
+
argument: Value,
|
|
2073
|
+
version?: TlcVersion,
|
|
1713
2074
|
) => {
|
|
1714
|
-
|
|
1715
|
-
|
|
2075
|
+
const shown = prettyPrint(formatValue(functionValue));
|
|
2076
|
+
const shownArgument = prettyPrint(formatValue(argument));
|
|
2077
|
+
const is174 = version === "1.7.4";
|
|
2078
|
+
switch (getFunctionClass(functionValue)) {
|
|
2079
|
+
case "lambda":
|
|
2080
|
+
return `In applying the function\n${shown},\nthe first argument is:\n${shownArgument}\nwhich is not in its domain.\n`;
|
|
2081
|
+
case "tuple":
|
|
2082
|
+
if (typeof argument !== "number") {
|
|
2083
|
+
return is174
|
|
2084
|
+
? "Attempted to apply tuple to a non-integer argument."
|
|
2085
|
+
: `Attempted to access tuple at a non integral index: ${shownArgument}`;
|
|
2086
|
+
}
|
|
2087
|
+
return is174
|
|
2088
|
+
? `Attempted to apply tuple\n${shown}\nto integer ${argument} which is out of domain.`
|
|
2089
|
+
: `Attempted to access index ${argument} of tuple\n${shown}\nwhich is out of bounds.`;
|
|
2090
|
+
case "record":
|
|
2091
|
+
if (typeof argument !== "string") {
|
|
2092
|
+
return is174
|
|
2093
|
+
? `Attempted to apply record to a non-string value ${shownArgument}.`
|
|
2094
|
+
: `Attempted to access record by a non-string argument: ${shownArgument}`;
|
|
2095
|
+
}
|
|
2096
|
+
return is174
|
|
2097
|
+
? `Attempted to apply the record\n${shown}\nto nonexistent record field ${argument}.`
|
|
2098
|
+
: `Attempted to access nonexistent field '${argument}' of record\n${shown}`;
|
|
2099
|
+
default:
|
|
2100
|
+
return `Attempted to apply function:\n${shown}\nto argument ${shownArgument}, which is not in the domain of the function.`;
|
|
2101
|
+
}
|
|
1716
2102
|
};
|
|
1717
2103
|
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
2104
|
+
// TLC 1.7.4 words a key of the wrong type in an EXCEPT, which selects rather than applies, apart.
|
|
2105
|
+
const describeExceptKey = (functionValue: FunctionValue, argument: Value) => {
|
|
2106
|
+
if (!valueVersion.is174) return describeMissingArgument(functionValue, argument);
|
|
2107
|
+
const shown = prettyPrint(formatValue(argument));
|
|
2108
|
+
return getFunctionClass(functionValue) === "tuple"
|
|
2109
|
+
? `Attempted to apply tuple to a non-integer argument ${shown}.`
|
|
2110
|
+
: `Attempted to apply record to a non-string argument ${shown}.`;
|
|
2111
|
+
};
|
|
1722
2112
|
|
|
1723
|
-
|
|
2113
|
+
// A key the domain holds itself, which TLC finds without a comparison that can fail.
|
|
2114
|
+
const findIdenticalKey = (domain: Value[], argument: Value) => {
|
|
1724
2115
|
const first = domain[0];
|
|
1725
2116
|
if (typeof argument === "number" && typeof first === "number") {
|
|
1726
2117
|
const offset = argument - first;
|
|
1727
2118
|
if (domain[offset] === argument) return offset;
|
|
1728
2119
|
}
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
2120
|
+
return domain.length <= 8 ? domain.indexOf(argument) : -1;
|
|
2121
|
+
};
|
|
2122
|
+
|
|
2123
|
+
export const createNonIntegerKeyError = (argument: Value) =>
|
|
2124
|
+
new TlaEvaluationError(
|
|
2125
|
+
`Attempted to apply function with integer domain to the non-integer argument ${prettyPrint(formatValue(argument))}`,
|
|
2126
|
+
);
|
|
2127
|
+
|
|
2128
|
+
// TLC's FcnRcdValue searches a domain this small linearly with equals, and a larger normalized one by
|
|
2129
|
+
// binary search with compareTo.
|
|
2130
|
+
const LINEAR_SEARCH_LIMIT = 32;
|
|
2131
|
+
|
|
2132
|
+
const failKeyLikeTlc = (domain: Value[], argument: Value, error: TlaEvaluationError): never => {
|
|
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
|
+
// TLC looks a key up by the Java class of the function: a tuple or a record rejects a key of the
|
|
2142
|
+
// wrong type, and another function rejects a non-integer for a domain it keeps as an interval, which
|
|
2143
|
+
// TLC 1.7.4 doesn't. A key of another type than the domain's keys otherwise fails TLC's comparison.
|
|
2144
|
+
const findKeyIndexLikeTlc = (
|
|
2145
|
+
functionValue: FunctionValue,
|
|
2146
|
+
argument: Value,
|
|
2147
|
+
describeWrongKey: (functionValue: FunctionValue, argument: Value) => string,
|
|
2148
|
+
) => {
|
|
2149
|
+
const domain = functionValue.domain;
|
|
2150
|
+
if (typeof argument !== typeof domain[0]) {
|
|
2151
|
+
const functionClass = getFunctionClass(functionValue);
|
|
2152
|
+
const keyType =
|
|
2153
|
+
functionClass === "tuple" ? "number" : functionClass === "record" ? "string" : undefined;
|
|
2154
|
+
if (keyType !== undefined && typeof argument !== keyType) {
|
|
2155
|
+
throw new TlaEvaluationError(describeWrongKey(functionValue, argument));
|
|
2156
|
+
}
|
|
2157
|
+
if (keyType === undefined && !valueVersion.is174 && isIntervalDomain(domain)) {
|
|
2158
|
+
throw createNonIntegerKeyError(argument);
|
|
2159
|
+
}
|
|
1732
2160
|
}
|
|
1733
|
-
|
|
2161
|
+
const identical = findIdenticalKey(domain, argument);
|
|
2162
|
+
if (identical !== -1) return identical;
|
|
2163
|
+
return computeLikeTlcOnFailure(findIndex, failKeyLikeTlc, domain, argument);
|
|
1734
2164
|
};
|
|
1735
2165
|
|
|
1736
2166
|
export const applyFunction = (
|
|
1737
2167
|
functionValue: Value,
|
|
1738
2168
|
argument: Value,
|
|
1739
|
-
describeMissing?: (functionValue: FunctionValue) => string,
|
|
2169
|
+
describeMissing?: (functionValue: FunctionValue, argument: Value) => string,
|
|
1740
2170
|
): Value => {
|
|
1741
2171
|
if (typeof functionValue === "object" && functionValue.kind === "LazyFunction") {
|
|
1742
2172
|
if (!isElementOf(argument, functionValue.domain)) {
|
|
1743
2173
|
throw new TlaEvaluationError(
|
|
1744
|
-
|
|
2174
|
+
getFunctionClass(functionValue) === "lambda"
|
|
1745
2175
|
? describeMissingArgument(functionValue, argument)
|
|
1746
2176
|
: `Attempted to apply a function to ${formatValue(argument)}, which is not in its domain`,
|
|
1747
2177
|
);
|
|
@@ -1753,12 +2183,9 @@ export const applyFunction = (
|
|
|
1753
2183
|
`Attempted to apply ${formatValue(functionValue)}, which is not a function`,
|
|
1754
2184
|
);
|
|
1755
2185
|
}
|
|
1756
|
-
const
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
describeMissing?.(functionValue) ?? describeMissingArgument(functionValue, argument),
|
|
1760
|
-
);
|
|
1761
|
-
}
|
|
2186
|
+
const describe = describeMissing ?? describeMissingArgument;
|
|
2187
|
+
const position = findKeyIndexLikeTlc(functionValue, argument, describe);
|
|
2188
|
+
if (position === -1) throw new TlaEvaluationError(describe(functionValue, argument));
|
|
1762
2189
|
return functionValue.range[position];
|
|
1763
2190
|
};
|
|
1764
2191
|
|
|
@@ -1787,28 +2214,45 @@ export const updateFunction = (
|
|
|
1787
2214
|
if (isLazy) {
|
|
1788
2215
|
if (!isElementOf(argument, functionValue.domain)) return functionValue;
|
|
1789
2216
|
const updated = update(functionValue.apply(argument));
|
|
1790
|
-
return
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
2217
|
+
return setFunctionClass<LazyFunctionValue>(
|
|
2218
|
+
{
|
|
2219
|
+
kind: "LazyFunction",
|
|
2220
|
+
domain: functionValue.domain,
|
|
2221
|
+
apply: (key) => (isEqualLikeTlc(argument, key) ? updated : functionValue.apply(key)),
|
|
2222
|
+
source: functionValue.source,
|
|
2223
|
+
},
|
|
2224
|
+
getFunctionClass(functionValue),
|
|
2225
|
+
);
|
|
2226
|
+
}
|
|
2227
|
+
if (!isFunctionValue(functionValue)) {
|
|
2228
|
+
throw new TlaEvaluationError(
|
|
2229
|
+
`Attempted to apply EXCEPT construct to the value ${prettyPrint(formatValue(functionValue))}.`,
|
|
2230
|
+
);
|
|
1796
2231
|
}
|
|
1797
2232
|
const finite = toFiniteFunction(functionValue);
|
|
1798
|
-
const position =
|
|
2233
|
+
const position = findKeyIndexLikeTlc(finite, argument, describeExceptKey);
|
|
1799
2234
|
if (position === -1) return finite;
|
|
1800
2235
|
const range = [...finite.range];
|
|
1801
2236
|
const previous = finite.range[position];
|
|
1802
2237
|
const next = update(previous);
|
|
1803
2238
|
range[position] = next;
|
|
1804
|
-
const updated =
|
|
2239
|
+
const updated = setFunctionClass(
|
|
2240
|
+
createSortedFunction(finite.domain, range),
|
|
2241
|
+
getFunctionClass(finite),
|
|
2242
|
+
);
|
|
1805
2243
|
updated.writtenKeys = finite.writtenKeys;
|
|
1806
|
-
if (finite.hash !== 0) {
|
|
1807
|
-
const
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
2244
|
+
if (finite.hash !== 0 && finite.hash !== UNFINGERPRINTABLE_HASH) {
|
|
2245
|
+
const nextHash = getValueHash(next);
|
|
2246
|
+
if (nextHash !== UNFINGERPRINTABLE_HASH) {
|
|
2247
|
+
const keyHash = getValueHash(finite.domain[position]);
|
|
2248
|
+
updated.hash =
|
|
2249
|
+
previous === next
|
|
2250
|
+
? finite.hash
|
|
2251
|
+
: (finite.hash -
|
|
2252
|
+
getEntryContribution(keyHash, getValueHash(previous)) +
|
|
2253
|
+
getEntryContribution(keyHash, nextHash)) |
|
|
2254
|
+
0;
|
|
2255
|
+
}
|
|
1812
2256
|
}
|
|
1813
2257
|
if (finite.fingerprint !== 0) {
|
|
1814
2258
|
const key = finite.domain[position];
|
|
@@ -1845,32 +2289,103 @@ const hashString = (value: string) => {
|
|
|
1845
2289
|
const SET_HASH_SEED = 0x73a5c3e1;
|
|
1846
2290
|
const FUNCTION_HASH_SEED = 0x66d1b4f7;
|
|
1847
2291
|
|
|
1848
|
-
const getElementContribution = (
|
|
1849
|
-
finalizeHash((
|
|
2292
|
+
const getElementContribution = (elementHash: number) =>
|
|
2293
|
+
finalizeHash((elementHash + 0x9e3779b9) | 0);
|
|
2294
|
+
|
|
2295
|
+
const getEntryContribution = (keyHash: number, valueHash: number) =>
|
|
2296
|
+
finalizeHash((Math.imul(keyHash, 0x27d4eb2d) + valueHash) | 0);
|
|
1850
2297
|
|
|
1851
|
-
|
|
1852
|
-
|
|
2298
|
+
// The hash of each value that TLC fails to fingerprint, such as Nat or a set that holds it. Calls
|
|
2299
|
+
// with such a value as an argument still share their result, and a state that holds one fails
|
|
2300
|
+
// when TLC fingerprints it.
|
|
2301
|
+
const UNFINGERPRINTABLE_HASH = 0x2f6b1c4d;
|
|
1853
2302
|
|
|
1854
2303
|
const computeValueHash = (value: ObjectValue) => {
|
|
1855
2304
|
if (value.kind === "ModelValue") return finalizeHash(combineHash(hashString(value.name), 0x6d));
|
|
1856
2305
|
if (isSetValue(value)) {
|
|
1857
|
-
if (!isEnumerable(value)) return
|
|
2306
|
+
if (!isEnumerable(value)) return UNFINGERPRINTABLE_HASH;
|
|
1858
2307
|
const elements = toFiniteSet(value).elements;
|
|
1859
2308
|
let hash = SET_HASH_SEED;
|
|
1860
2309
|
for (let index = 0; index < elements.length; index++) {
|
|
1861
|
-
|
|
2310
|
+
const elementHash = getValueHash(elements[index]);
|
|
2311
|
+
if (elementHash === UNFINGERPRINTABLE_HASH) return UNFINGERPRINTABLE_HASH;
|
|
2312
|
+
hash = (hash + getElementContribution(elementHash)) | 0;
|
|
1862
2313
|
}
|
|
1863
2314
|
return hash;
|
|
1864
2315
|
}
|
|
2316
|
+
if (value.kind === "LazyFunction" && !isEnumerable(value.domain)) return UNFINGERPRINTABLE_HASH;
|
|
1865
2317
|
const functionValue = toFiniteFunction(value);
|
|
1866
2318
|
let hash = FUNCTION_HASH_SEED;
|
|
1867
2319
|
for (let index = 0; index < functionValue.domain.length; index++) {
|
|
1868
|
-
|
|
1869
|
-
|
|
2320
|
+
const keyHash = getValueHash(functionValue.domain[index]);
|
|
2321
|
+
const itemHash = getValueHash(functionValue.range[index]);
|
|
2322
|
+
if (keyHash === UNFINGERPRINTABLE_HASH || itemHash === UNFINGERPRINTABLE_HASH) {
|
|
2323
|
+
return UNFINGERPRINTABLE_HASH;
|
|
2324
|
+
}
|
|
2325
|
+
hash = (hash + getEntryContribution(keyHash, itemHash)) | 0;
|
|
1870
2326
|
}
|
|
1871
2327
|
return hash;
|
|
1872
2328
|
};
|
|
1873
2329
|
|
|
2330
|
+
// TLC's fingerPrint enumerates each set and function a value holds, and fails on Nat, Seq(S) and
|
|
2331
|
+
// the like, which it never enumerates.
|
|
2332
|
+
const fingerprintValue = (value: Value): void => {
|
|
2333
|
+
if (typeof value !== "object" || value.kind === "ModelValue") return;
|
|
2334
|
+
if (value.kind === "InfiniteSet" || value.kind === "SequenceSet") {
|
|
2335
|
+
throw new TlaEvaluationError(
|
|
2336
|
+
`TLC has found a state in which the value of a variable contains ${formatValue(value)}`,
|
|
2337
|
+
);
|
|
2338
|
+
}
|
|
2339
|
+
if (isSetValue(value)) {
|
|
2340
|
+
toFiniteSet(value).elements.forEach(fingerprintValue);
|
|
2341
|
+
return;
|
|
2342
|
+
}
|
|
2343
|
+
const functionValue = toFiniteFunction(value);
|
|
2344
|
+
functionValue.domain.forEach(fingerprintValue);
|
|
2345
|
+
functionValue.range.forEach(fingerprintValue);
|
|
2346
|
+
};
|
|
2347
|
+
|
|
2348
|
+
// The error TLC raises as it fingerprints a value: one that hashing the value raises, as for a set
|
|
2349
|
+
// too large to enumerate, or one of a value with the hash of the values TLC fails on.
|
|
2350
|
+
export const getFingerprintError = (value: Value) => {
|
|
2351
|
+
if (typeof value !== "object" || value.kind === "ModelValue") return undefined;
|
|
2352
|
+
try {
|
|
2353
|
+
if (getValueHash(value) === UNFINGERPRINTABLE_HASH) fingerprintValue(value);
|
|
2354
|
+
return undefined;
|
|
2355
|
+
} catch (error) {
|
|
2356
|
+
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
2357
|
+
return error;
|
|
2358
|
+
}
|
|
2359
|
+
};
|
|
2360
|
+
|
|
2361
|
+
// The values a value holds, which TLC fingerprints as it fingerprints the value.
|
|
2362
|
+
export const getHeldValues = (value: ObjectValue): Value[] => {
|
|
2363
|
+
switch (value.kind) {
|
|
2364
|
+
case "Set":
|
|
2365
|
+
case "MixedSet":
|
|
2366
|
+
return value.elements;
|
|
2367
|
+
case "Function":
|
|
2368
|
+
return [...value.domain, ...value.range];
|
|
2369
|
+
case "Subset":
|
|
2370
|
+
case "SequenceSet":
|
|
2371
|
+
case "FilteredSet":
|
|
2372
|
+
case "Union":
|
|
2373
|
+
return [value.base];
|
|
2374
|
+
case "FunctionSet":
|
|
2375
|
+
return [value.domain, value.range];
|
|
2376
|
+
case "RecordSet":
|
|
2377
|
+
return value.sets;
|
|
2378
|
+
case "ProductSet":
|
|
2379
|
+
return value.factors;
|
|
2380
|
+
case "SetOperation":
|
|
2381
|
+
return [value.left, value.right];
|
|
2382
|
+
case "LazyFunction":
|
|
2383
|
+
return [value.domain];
|
|
2384
|
+
default:
|
|
2385
|
+
return [];
|
|
2386
|
+
}
|
|
2387
|
+
};
|
|
2388
|
+
|
|
1874
2389
|
export const getValueHash = (value: Value): number => {
|
|
1875
2390
|
if (typeof value === "number") return finalizeHash(value ^ 0x5bd1e995);
|
|
1876
2391
|
if (typeof value === "boolean") return value ? 0x2b7e1516 : 0x28aed2a6;
|
|
@@ -2045,7 +2560,9 @@ const formatLazySet = (value: LazySetValue, isStrict: boolean): string => {
|
|
|
2045
2560
|
case "SetOperation":
|
|
2046
2561
|
return `${format(value.left)} ${value.operator} ${format(value.right)}`;
|
|
2047
2562
|
case "FilteredSet":
|
|
2048
|
-
return
|
|
2563
|
+
return value.source
|
|
2564
|
+
? `{${value.source.names} \\in ${format(value.base)} : <expression ${value.source.predicate}> }`
|
|
2565
|
+
: `{x \\in ${format(value.base)} : ...}`;
|
|
2049
2566
|
case "Union":
|
|
2050
2567
|
return formatUnion(value, isStrict);
|
|
2051
2568
|
}
|
|
@@ -2058,6 +2575,17 @@ const isFieldName = (name: string) =>
|
|
|
2058
2575
|
/\p{L}/u.test(name) &&
|
|
2059
2576
|
(name.length < 4 || !/^[WS]F_/.test(name));
|
|
2060
2577
|
|
|
2578
|
+
// FcnLambdaValue#toString prints a function it fails to enumerate as its parameters and body.
|
|
2579
|
+
const toPrintedFunction = (value: LazyFunctionValue) => {
|
|
2580
|
+
if (!isEnumerable(value.domain)) return undefined;
|
|
2581
|
+
try {
|
|
2582
|
+
return toFiniteFunction(value);
|
|
2583
|
+
} catch (error) {
|
|
2584
|
+
if (error instanceof TlaEvaluationError) return undefined;
|
|
2585
|
+
throw error;
|
|
2586
|
+
}
|
|
2587
|
+
};
|
|
2588
|
+
|
|
2061
2589
|
export const formatValue = (value: Value, isStrict = false, isNormalized = false): string => {
|
|
2062
2590
|
const format = (inner: Value) => formatValue(inner, isStrict, isNormalized);
|
|
2063
2591
|
if (typeof value === "boolean") return value ? "TRUE" : "FALSE";
|
|
@@ -2089,27 +2617,53 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
|
|
|
2089
2617
|
return `Seq(${format(value.base)})`;
|
|
2090
2618
|
case "Function":
|
|
2091
2619
|
case "LazyFunction": {
|
|
2092
|
-
|
|
2093
|
-
|
|
2620
|
+
const functionValue = value.kind === "LazyFunction" ? toPrintedFunction(value) : value;
|
|
2621
|
+
if (functionValue === undefined) {
|
|
2622
|
+
const source = value.kind === "LazyFunction" ? value.source : undefined;
|
|
2094
2623
|
if (!source) return "<function>";
|
|
2095
2624
|
const parameters = source.parameters
|
|
2096
2625
|
.map((parameter) => `${parameter.name} \\in ${formatValue(parameter.domain)}`)
|
|
2097
2626
|
.join(", ");
|
|
2098
2627
|
return `[${parameters} |-> <expression ${source.body}>]`;
|
|
2099
2628
|
}
|
|
2100
|
-
|
|
2629
|
+
if (
|
|
2630
|
+
functionValue.domain.length === 0 &&
|
|
2631
|
+
valueVersion.is174 &&
|
|
2632
|
+
getFunctionClass(value) !== "tuple"
|
|
2633
|
+
) {
|
|
2634
|
+
return "<< >>";
|
|
2635
|
+
}
|
|
2636
|
+
// TLC normalizes a lambda's domain to print it, but evaluates its body afterward.
|
|
2637
|
+
const formatRange = (inner: Value) =>
|
|
2638
|
+
formatValue(inner, isStrict, isNormalized && getFunctionClass(value) !== "lambda");
|
|
2101
2639
|
if (functionValue.domain.every((key, index) => key === index + 1)) {
|
|
2102
|
-
return `<<${functionValue.range.map(
|
|
2640
|
+
return `<<${functionValue.range.map(formatRange).join(", ")}>>`;
|
|
2103
2641
|
}
|
|
2104
|
-
|
|
2642
|
+
// A RecordValue prints as a record whatever its fields, and a FcnRcdValue only if each key
|
|
2643
|
+
// is a name.
|
|
2644
|
+
if (
|
|
2645
|
+
getFunctionClass(value) === "record" ||
|
|
2646
|
+
functionValue.domain.every((key) => typeof key === "string" && isFieldName(key))
|
|
2647
|
+
) {
|
|
2105
2648
|
const isSorted =
|
|
2106
2649
|
isNormalized || functionValue.hash !== 0 || functionValue.fingerprint !== 0;
|
|
2107
2650
|
const pairs = isSorted
|
|
2108
2651
|
? functionValue.domain.map((key, index) => [key, functionValue.range[index]])
|
|
2109
2652
|
: getWrittenPairs(functionValue);
|
|
2110
|
-
return `[${pairs.map((pair) => `${String(pair[0])} |-> ${
|
|
2653
|
+
return `[${pairs.map((pair) => `${String(pair[0])} |-> ${formatRange(pair[1])}`).join(", ")}]`;
|
|
2111
2654
|
}
|
|
2112
|
-
return `(${functionValue.domain.map((key, index) => `${format(key)} :> ${
|
|
2655
|
+
return `(${functionValue.domain.map((key, index) => `${format(key)} :> ${formatRange(functionValue.range[index])}`).join(" @@ ")})`;
|
|
2113
2656
|
}
|
|
2114
2657
|
}
|
|
2115
2658
|
};
|
|
2659
|
+
|
|
2660
|
+
// TLC's TLCState.toString: a line per variable, as a conjunction when there are several.
|
|
2661
|
+
export const formatState = (state: Record<string, Value>) => {
|
|
2662
|
+
const entries = Object.entries(state);
|
|
2663
|
+
return entries
|
|
2664
|
+
.map(
|
|
2665
|
+
([name, value]) =>
|
|
2666
|
+
`${entries.length === 1 ? "" : "/\\ "}${name} = ${prettyPrint(formatValue(value))}\n`,
|
|
2667
|
+
)
|
|
2668
|
+
.join("");
|
|
2669
|
+
};
|