tla-ts 0.0.3 → 0.0.4-dev.4819fb7
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 +77 -37
- 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 +200 -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 +741 -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,65 @@ 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(
|
|
1029
|
+
() => valuesEqual(left, right),
|
|
1030
|
+
() => equalsLikeTlc(left, right),
|
|
1031
|
+
);
|
|
1032
|
+
|
|
864
1033
|
const isStrictlySorted = (elements: Value[]) => {
|
|
865
1034
|
for (let index = 1; index < elements.length; index++) {
|
|
866
1035
|
if (compareValues(elements[index - 1], elements[index]) >= 0) return false;
|
|
@@ -869,6 +1038,8 @@ const isStrictlySorted = (elements: Value[]) => {
|
|
|
869
1038
|
};
|
|
870
1039
|
|
|
871
1040
|
export const createSet = (elements: Value[]): SetValue => {
|
|
1041
|
+
// ValueVec.sort compares each element after the first with those before it.
|
|
1042
|
+
for (let index = 1; index < elements.length; index++) cacheSet(elements[index]);
|
|
872
1043
|
if (elements.length < 2 || isStrictlySorted(elements)) return createSortedSet(elements);
|
|
873
1044
|
const sorted = [...elements].sort(compareValues);
|
|
874
1045
|
const unique = [sorted[0]];
|
|
@@ -880,14 +1051,11 @@ export const createSet = (elements: Value[]): SetValue => {
|
|
|
880
1051
|
|
|
881
1052
|
// TLC builds a set enumeration without sorting it, so one whose elements it can't compare still
|
|
882
1053
|
// evaluates.
|
|
883
|
-
export const createEnumeratedSet = (elements: Value[]): SetValue | MixedSetValue =>
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
return { kind: "MixedSet", elements };
|
|
889
|
-
}
|
|
890
|
-
};
|
|
1054
|
+
export const createEnumeratedSet = (elements: Value[]): SetValue | MixedSetValue =>
|
|
1055
|
+
computeLikeTlcOnFailure<SetValue | MixedSetValue>(
|
|
1056
|
+
() => createSet(elements),
|
|
1057
|
+
() => ({ kind: "MixedSet", elements }),
|
|
1058
|
+
);
|
|
891
1059
|
|
|
892
1060
|
export const EMPTY_SET = createSortedSet([]);
|
|
893
1061
|
|
|
@@ -966,12 +1134,15 @@ export const unionSorted = (left: SetValue, right: SetValue): SetValue => {
|
|
|
966
1134
|
}
|
|
967
1135
|
if (added.length === 0) return left;
|
|
968
1136
|
const union = createSortedSet(merged);
|
|
969
|
-
if (left.hash !== 0) {
|
|
1137
|
+
if (left.hash !== 0 && left.hash !== UNFINGERPRINTABLE_HASH) {
|
|
970
1138
|
let hash = left.hash;
|
|
971
|
-
|
|
972
|
-
|
|
1139
|
+
let index = 0;
|
|
1140
|
+
for (; index < added.length; index++) {
|
|
1141
|
+
const elementHash = getValueHash(added[index]);
|
|
1142
|
+
if (elementHash === UNFINGERPRINTABLE_HASH) break;
|
|
1143
|
+
hash = (hash + getElementContribution(elementHash)) | 0;
|
|
973
1144
|
}
|
|
974
|
-
union.hash = hash;
|
|
1145
|
+
if (index === added.length) union.hash = hash;
|
|
975
1146
|
}
|
|
976
1147
|
if (left.fingerprint !== 0) {
|
|
977
1148
|
let fingerprint = left.fingerprint;
|
|
@@ -1056,23 +1227,27 @@ const getIntervalValue = (high: number): IntervalValue => {
|
|
|
1056
1227
|
};
|
|
1057
1228
|
|
|
1058
1229
|
export const createTuple = (elements: Value[]): FunctionValue =>
|
|
1059
|
-
createSortedFunction(getIntervalDomain(elements.length), elements);
|
|
1230
|
+
setFunctionClass(createSortedFunction(getIntervalDomain(elements.length), elements), "tuple");
|
|
1060
1231
|
|
|
1061
|
-
export const createRecord = (fields: [string, Value][]): FunctionValue =>
|
|
1232
|
+
export const createRecord = (fields: [string, Value][]): FunctionValue =>
|
|
1233
|
+
setFunctionClass(createFunction(fields), "record");
|
|
1062
1234
|
|
|
1063
1235
|
// TLC's Enumerable classes: every set except the ones it implements in Java.
|
|
1064
|
-
const isTlcEnumerable = (value: Value) => isSetValue(value) && !isOverriddenSet(value);
|
|
1236
|
+
export const isTlcEnumerable = (value: Value) => isSetValue(value) && !isOverriddenSet(value);
|
|
1065
1237
|
|
|
1066
1238
|
const formatUnion = (value: UnionValue, isStrict = false) =>
|
|
1067
1239
|
`UNION(${formatValue(value.base, isStrict)})`;
|
|
1068
1240
|
|
|
1069
|
-
const getUnionSets = (
|
|
1241
|
+
const getUnionSets = (
|
|
1242
|
+
value: UnionValue,
|
|
1243
|
+
getElements = (set: Value) => toFiniteSet(set).elements,
|
|
1244
|
+
) => {
|
|
1070
1245
|
if (!isTlcEnumerable(value.base)) {
|
|
1071
1246
|
throw new TlaEvaluationError(
|
|
1072
1247
|
`Attempted to enumerate the nonenumerable set:\n${formatUnion(value)}`,
|
|
1073
1248
|
);
|
|
1074
1249
|
}
|
|
1075
|
-
return
|
|
1250
|
+
return getElements(value.base);
|
|
1076
1251
|
};
|
|
1077
1252
|
|
|
1078
1253
|
const getUnionSet = (set: Value, index: number, union: UnionValue) => {
|
|
@@ -1108,7 +1283,7 @@ export const isEnumerable = (value: Value): boolean => {
|
|
|
1108
1283
|
case "ProductSet":
|
|
1109
1284
|
return value.factors.every(isEnumerable) || getEmptiness(value) === true;
|
|
1110
1285
|
case "Union":
|
|
1111
|
-
return isEnumerable(value.base) &&
|
|
1286
|
+
return isEnumerable(value.base) && getStreamedElements(value.base).every(isEnumerable);
|
|
1112
1287
|
case "SetOperation":
|
|
1113
1288
|
if (value.operator === "\\cup") return isEnumerable(value.left) && isEnumerable(value.right);
|
|
1114
1289
|
if (value.operator === "\\cap") return isEnumerable(value.left) || isEnumerable(value.right);
|
|
@@ -1181,6 +1356,41 @@ const advanceCombination = (indices: number[], length: number) => {
|
|
|
1181
1356
|
return false;
|
|
1182
1357
|
};
|
|
1183
1358
|
|
|
1359
|
+
// TLC enumerates SUBSET S lazily, by size and then in the order of S, so a quantifier that finds
|
|
1360
|
+
// its witness early never builds the rest. For a power set too large to build, the elements are
|
|
1361
|
+
// computed in that order as a loop reads them.
|
|
1362
|
+
export const getLazySubsets = (value: Value): Value[] | undefined => {
|
|
1363
|
+
if (typeof value !== "object" || value.kind !== "Subset" || isOverriddenSet(value.base)) {
|
|
1364
|
+
return undefined;
|
|
1365
|
+
}
|
|
1366
|
+
const base = toFiniteSet(value.base).elements;
|
|
1367
|
+
if (base.length <= MAXIMUM_ENUMERATED_SUBSET_BASE) return undefined;
|
|
1368
|
+
const cursor = { index: -1, indices: [0] };
|
|
1369
|
+
const getSubset = (index: number) => {
|
|
1370
|
+
if (index <= cursor.index) cursor.index = -1;
|
|
1371
|
+
while (cursor.index < index) {
|
|
1372
|
+
if (cursor.index === -1) cursor.indices = [];
|
|
1373
|
+
else if (!advanceCombination(cursor.indices, base.length)) {
|
|
1374
|
+
cursor.indices = Array.from({ length: cursor.indices.length + 1 }, (_, item) => item);
|
|
1375
|
+
}
|
|
1376
|
+
cursor.index++;
|
|
1377
|
+
}
|
|
1378
|
+
return createSortedSet(cursor.indices.map((item) => base[item]));
|
|
1379
|
+
};
|
|
1380
|
+
const length = 2 ** base.length;
|
|
1381
|
+
const isIndex = (property: string | symbol) =>
|
|
1382
|
+
typeof property === "string" && /^\d+$/.test(property) && Number(property) < length;
|
|
1383
|
+
return new Proxy<Value[]>([], {
|
|
1384
|
+
get: (target, property, receiver) => {
|
|
1385
|
+
if (property === "length") return length;
|
|
1386
|
+
return isIndex(property)
|
|
1387
|
+
? getSubset(Number(property))
|
|
1388
|
+
: Reflect.get(target, property, receiver);
|
|
1389
|
+
},
|
|
1390
|
+
has: (target, property) => isIndex(property) || Reflect.has(target, property),
|
|
1391
|
+
});
|
|
1392
|
+
};
|
|
1393
|
+
|
|
1184
1394
|
const getSortedSubsets = (base: Value[]) => {
|
|
1185
1395
|
const subsets: Value[] = [];
|
|
1186
1396
|
for (let size = 0; size <= base.length; size++) {
|
|
@@ -1192,7 +1402,30 @@ const getSortedSubsets = (base: Value[]) => {
|
|
|
1192
1402
|
return subsets;
|
|
1193
1403
|
};
|
|
1194
1404
|
|
|
1195
|
-
|
|
1405
|
+
// The Enumerator of a set of records or tuples enumerates its sets in turn and stops at the first
|
|
1406
|
+
// empty one, which leaves the product empty. Later versions of TLC decide that the set is empty
|
|
1407
|
+
// before they enumerate it.
|
|
1408
|
+
const getComponentElements = (
|
|
1409
|
+
sets: Value[],
|
|
1410
|
+
getElements: (set: Value) => Value[],
|
|
1411
|
+
describeUnenumerable: (set: Value, index: number) => string,
|
|
1412
|
+
) => {
|
|
1413
|
+
const lists: Value[][] = [];
|
|
1414
|
+
for (let index = 0; index < sets.length; index++) {
|
|
1415
|
+
const set = sets[index];
|
|
1416
|
+
if (isOverriddenSet(set)) throw new TlaEvaluationError(describeUnenumerable(set, index));
|
|
1417
|
+
const elements = getElements(set);
|
|
1418
|
+
if (elements.length === 0) return [[]];
|
|
1419
|
+
lists.push(elements);
|
|
1420
|
+
}
|
|
1421
|
+
return lists;
|
|
1422
|
+
};
|
|
1423
|
+
|
|
1424
|
+
// getElements enumerates the sets a set of functions, records, or tuples combines.
|
|
1425
|
+
const enumerateSet = (
|
|
1426
|
+
value: Value,
|
|
1427
|
+
getElements = (set: Value) => toFiniteSet(set).elements,
|
|
1428
|
+
): Value[] => {
|
|
1196
1429
|
if (typeof value !== "object")
|
|
1197
1430
|
throw new TlaEvaluationError(`Expected a set but found ${formatValue(value)}`);
|
|
1198
1431
|
switch (value.kind) {
|
|
@@ -1206,61 +1439,63 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1206
1439
|
return elements;
|
|
1207
1440
|
}
|
|
1208
1441
|
case "Subset": {
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
if (base
|
|
1442
|
+
// SubsetValue.toSetEnum sizes the set before it enumerates it, and enumerates only the empty
|
|
1443
|
+
// set of a value of size 0, a function with an empty domain among them.
|
|
1444
|
+
if (getCardinality(value.base) === 0) return getSortedSubsets([]);
|
|
1445
|
+
if (!isSetValue(value.base)) {
|
|
1212
1446
|
throw new TlaEvaluationError(
|
|
1213
|
-
`
|
|
1447
|
+
`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
1448
|
);
|
|
1215
1449
|
}
|
|
1450
|
+
const base = toFiniteSet(value.base).elements;
|
|
1451
|
+
if (base.length > MAXIMUM_ENUMERATED_SUBSET_BASE) {
|
|
1452
|
+
throw new TlaEvaluationError(describeOverflow(value));
|
|
1453
|
+
}
|
|
1216
1454
|
return getSortedSubsets(base);
|
|
1217
1455
|
}
|
|
1218
1456
|
case "FunctionSet": {
|
|
1219
|
-
if (isEmptySet(value)) return [];
|
|
1457
|
+
if (!valueVersion.is174 && isEmptySet(value)) return [];
|
|
1220
1458
|
if (isOverriddenSet(value.domain)) {
|
|
1221
1459
|
throw new TlaEvaluationError(
|
|
1222
1460
|
`Attempted to enumerate a set of the form [D -> R],but the domain D:\n${formatValue(value.domain)}\ncannot be enumerated.`,
|
|
1223
1461
|
);
|
|
1224
1462
|
}
|
|
1225
1463
|
const domain = getFunctionDomain(value.domain);
|
|
1226
|
-
|
|
1464
|
+
// TLC 1.7.4 requires an enumerable range even of an empty domain.
|
|
1465
|
+
if ((domain.length > 0 || valueVersion.is174) && isOverriddenSet(value.range)) {
|
|
1227
1466
|
throw new TlaEvaluationError(
|
|
1228
1467
|
`Attempted to enumerate a set of the form [D -> R],but the range R:\n${formatValue(value.range)}\ncannot be enumerated.`,
|
|
1229
1468
|
);
|
|
1230
1469
|
}
|
|
1231
|
-
const range = domain.length === 0 ? [] :
|
|
1470
|
+
const range = domain.length === 0 ? [] : getElements(value.range);
|
|
1232
1471
|
return getCartesianProduct(domain.map(() => range)).map((combination): Value =>
|
|
1233
1472
|
createSortedFunction(domain, combination),
|
|
1234
1473
|
);
|
|
1235
1474
|
}
|
|
1236
1475
|
case "RecordSet": {
|
|
1237
|
-
if (isEmptySet(value)) return [];
|
|
1238
|
-
const fields =
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
)
|
|
1243
|
-
|
|
1244
|
-
return toFiniteSet(set).elements;
|
|
1245
|
-
});
|
|
1476
|
+
if (!valueVersion.is174 && isEmptySet(value)) return [];
|
|
1477
|
+
const fields = getComponentElements(
|
|
1478
|
+
value.sets,
|
|
1479
|
+
getElements,
|
|
1480
|
+
(set, index) =>
|
|
1481
|
+
`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)}`,
|
|
1482
|
+
);
|
|
1246
1483
|
return getCartesianProduct(fields).map((combination) =>
|
|
1247
1484
|
createRecord(value.names.map((name, index): [string, Value] => [name, combination[index]])),
|
|
1248
1485
|
);
|
|
1249
1486
|
}
|
|
1250
1487
|
case "ProductSet": {
|
|
1251
|
-
if (isEmptySet(value)) return [];
|
|
1252
|
-
const factors =
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
)
|
|
1257
|
-
|
|
1258
|
-
return toFiniteSet(factor).elements;
|
|
1259
|
-
});
|
|
1488
|
+
if (!valueVersion.is174 && isEmptySet(value)) return [];
|
|
1489
|
+
const factors = getComponentElements(
|
|
1490
|
+
value.factors,
|
|
1491
|
+
getElements,
|
|
1492
|
+
(factor, index) =>
|
|
1493
|
+
`Attempted to enumerate a set of the form s1 \\X s2 ... \\X sn,\nbut can't enumerate s${index}:\n${formatValue(factor)}`,
|
|
1494
|
+
);
|
|
1260
1495
|
return getCartesianProduct(factors).map(createTuple);
|
|
1261
1496
|
}
|
|
1262
1497
|
case "FilteredSet":
|
|
1263
|
-
if (isOverriddenSet(value.base)) {
|
|
1498
|
+
if (!isSetValue(value.base) || isOverriddenSet(value.base)) {
|
|
1264
1499
|
throw new TlaEvaluationError(
|
|
1265
1500
|
`Attempted to enumerate { x \\in S : p(x) } when S:\n${formatValue(value.base)}\nis not enumerable`,
|
|
1266
1501
|
);
|
|
@@ -1275,7 +1510,7 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1275
1510
|
case "SetOperation": {
|
|
1276
1511
|
if (
|
|
1277
1512
|
value.operator === "\\cup" &&
|
|
1278
|
-
(
|
|
1513
|
+
(!isTlcEnumerable(value.left) || !isTlcEnumerable(value.right))
|
|
1279
1514
|
) {
|
|
1280
1515
|
throw new TlaEvaluationError(
|
|
1281
1516
|
`Attempted to enumerate S \\cup T when S:\n${formatValue(value.left)}\nand T:\n${formatValue(value.right)}\nare not both enumerable`,
|
|
@@ -1283,21 +1518,21 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1283
1518
|
}
|
|
1284
1519
|
if (
|
|
1285
1520
|
value.operator === "\\cap" &&
|
|
1286
|
-
|
|
1287
|
-
|
|
1521
|
+
!isTlcEnumerable(value.left) &&
|
|
1522
|
+
!isTlcEnumerable(value.right)
|
|
1288
1523
|
) {
|
|
1289
1524
|
throw new TlaEvaluationError(
|
|
1290
1525
|
`Attempted to enumerate S \\cap T when neither S:\n${formatValue(value.left)}\nnor T:\n${formatValue(value.right)}\nis enumerable`,
|
|
1291
1526
|
);
|
|
1292
1527
|
}
|
|
1293
|
-
if (value.operator === "\\" &&
|
|
1528
|
+
if (value.operator === "\\" && !isTlcEnumerable(value.left)) {
|
|
1294
1529
|
throw new TlaEvaluationError(
|
|
1295
1530
|
`Attempted to enumerate S \\ T when S:\n${formatValue(value.left)}\nis not enumerable.`,
|
|
1296
1531
|
);
|
|
1297
1532
|
}
|
|
1298
1533
|
if (value.operator === "\\cup")
|
|
1299
1534
|
return unionSorted(toFiniteSet(value.left), toFiniteSet(value.right)).elements;
|
|
1300
|
-
const isLeftEnumerated = value.operator === "\\" ||
|
|
1535
|
+
const isLeftEnumerated = value.operator === "\\" || isTlcEnumerable(value.left);
|
|
1301
1536
|
const enumerated = isLeftEnumerated ? value.left : value.right;
|
|
1302
1537
|
const other = isLeftEnumerated ? value.right : value.left;
|
|
1303
1538
|
return toFiniteSet(enumerated).elements.filter(
|
|
@@ -1323,7 +1558,10 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1323
1558
|
}
|
|
1324
1559
|
const cached = typeof value === "object" ? finiteSets.get(value) : undefined;
|
|
1325
1560
|
if (cached) return cached;
|
|
1561
|
+
const sets = setTrace.sets;
|
|
1562
|
+
sets?.push(value);
|
|
1326
1563
|
const elements = enumerateSet(value);
|
|
1564
|
+
sets?.pop();
|
|
1327
1565
|
const isSorted =
|
|
1328
1566
|
typeof value === "object" &&
|
|
1329
1567
|
(value.kind === "MixedSet" ||
|
|
@@ -1341,25 +1579,60 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1341
1579
|
// TLC's Enumerator for a lazy set streams the sets it combines until the set caches its elements,
|
|
1342
1580
|
// so an element that two of them share comes up once for each.
|
|
1343
1581
|
export const getStreamedElements = (value: Value): Value[] => {
|
|
1344
|
-
if (typeof value !== "object" ||
|
|
1582
|
+
if (typeof value !== "object" || cachedSets.has(value)) return toFiniteSet(value).elements;
|
|
1583
|
+
const sets = setTrace.sets;
|
|
1584
|
+
sets?.push(value);
|
|
1585
|
+
const elements = streamElements(value);
|
|
1586
|
+
sets?.pop();
|
|
1587
|
+
return elements;
|
|
1588
|
+
};
|
|
1589
|
+
|
|
1590
|
+
const getCombinedSets = (value: ObjectValue) =>
|
|
1591
|
+
value.kind === "ProductSet"
|
|
1592
|
+
? value.factors
|
|
1593
|
+
: value.kind === "RecordSet"
|
|
1594
|
+
? value.sets
|
|
1595
|
+
: value.kind === "FunctionSet"
|
|
1596
|
+
? [value.range]
|
|
1597
|
+
: undefined;
|
|
1598
|
+
|
|
1599
|
+
// Whether a set streams its elements in another order than the normalized set, so that a set of
|
|
1600
|
+
// tuples, records, or functions it is a component of has to stream too.
|
|
1601
|
+
const isStreamedApart = (value: Value): boolean => {
|
|
1602
|
+
if (typeof value !== "object" || cachedSets.has(value)) return false;
|
|
1603
|
+
if (value.kind === "Union") return true;
|
|
1604
|
+
if (value.kind === "FilteredSet") return isStreamedApart(value.base);
|
|
1605
|
+
if (value.kind === "SetOperation") {
|
|
1606
|
+
return (
|
|
1607
|
+
value.operator === "\\cup" || isStreamedApart(value.left) || isStreamedApart(value.right)
|
|
1608
|
+
);
|
|
1609
|
+
}
|
|
1610
|
+
return getCombinedSets(value)?.some(isStreamedApart) ?? false;
|
|
1611
|
+
};
|
|
1612
|
+
|
|
1613
|
+
const streamElements = (value: ObjectValue): Value[] => {
|
|
1614
|
+
// TLC streams the sets of a UNION it hasn't enumerated, so a set that appears twice there yields
|
|
1615
|
+
// its elements twice.
|
|
1345
1616
|
if (value.kind === "Union") {
|
|
1346
|
-
return getUnionSets(value).flatMap((set, index) =>
|
|
1617
|
+
return getUnionSets(value, getStreamedElements).flatMap((set, index) =>
|
|
1347
1618
|
getStreamedElements(getUnionSet(set, index, value)),
|
|
1348
1619
|
);
|
|
1349
1620
|
}
|
|
1350
1621
|
if (value.kind === "FilteredSet" && !isOverriddenSet(value.base)) {
|
|
1351
1622
|
return getStreamedElements(value.base).filter(value.test);
|
|
1352
1623
|
}
|
|
1624
|
+
const components = getCombinedSets(value);
|
|
1625
|
+
if (components?.some(isStreamedApart)) return enumerateSet(value, getStreamedElements);
|
|
1353
1626
|
if (value.kind !== "SetOperation") return toFiniteSet(value).elements;
|
|
1354
1627
|
if (value.operator === "\\cup") {
|
|
1355
|
-
return
|
|
1628
|
+
return !isTlcEnumerable(value.left) || !isTlcEnumerable(value.right)
|
|
1356
1629
|
? toFiniteSet(value).elements
|
|
1357
1630
|
: [...getStreamedElements(value.left), ...getStreamedElements(value.right)];
|
|
1358
1631
|
}
|
|
1359
|
-
const isLeftEnumerated = value.operator === "\\" ||
|
|
1632
|
+
const isLeftEnumerated = value.operator === "\\" || isTlcEnumerable(value.left);
|
|
1360
1633
|
const enumerated = isLeftEnumerated ? value.left : value.right;
|
|
1361
1634
|
const other = isLeftEnumerated ? value.right : value.left;
|
|
1362
|
-
if (
|
|
1635
|
+
if (!isTlcEnumerable(enumerated)) return toFiniteSet(value).elements;
|
|
1363
1636
|
return getStreamedElements(enumerated).filter(
|
|
1364
1637
|
(element) => isElementOf(element, other) === (value.operator === "\\cap"),
|
|
1365
1638
|
);
|
|
@@ -1396,9 +1669,13 @@ export const isEmptySet = (value: Value): boolean => {
|
|
|
1396
1669
|
}
|
|
1397
1670
|
};
|
|
1398
1671
|
|
|
1399
|
-
const getProductCardinality = (
|
|
1672
|
+
const getProductCardinality = (
|
|
1673
|
+
sets: Value[],
|
|
1674
|
+
version: TlcVersion | undefined,
|
|
1675
|
+
describe: () => string,
|
|
1676
|
+
) =>
|
|
1400
1677
|
sets.reduce<number>((product, set) => {
|
|
1401
|
-
const size = product * getCardinality(set);
|
|
1678
|
+
const size = product * getCardinality(set, version);
|
|
1402
1679
|
if (size > MAXIMUM_INTEGER) throw new TlaEvaluationError(describe());
|
|
1403
1680
|
return size;
|
|
1404
1681
|
}, 1);
|
|
@@ -1406,9 +1683,25 @@ const getProductCardinality = (sets: Value[], describe: () => string) =>
|
|
|
1406
1683
|
const describeOverflow = (value: Value) =>
|
|
1407
1684
|
`Overflow when computing the number of elements in:\n${formatValue(value)}`;
|
|
1408
1685
|
|
|
1409
|
-
|
|
1410
|
-
|
|
1686
|
+
// TLC 1.7.4 sizes every component of a set of functions, records, or tuples without first
|
|
1687
|
+
// checking whether one is empty, so an infinite component fails even beside an empty one.
|
|
1688
|
+
export const getCardinality = (value: Value, version?: TlcVersion): number => {
|
|
1689
|
+
if (typeof value !== "object") {
|
|
1690
|
+
const kind =
|
|
1691
|
+
typeof value === "number" ? "integer" : typeof value === "string" ? "string" : "boolean";
|
|
1692
|
+
throw new TlaEvaluationError(
|
|
1693
|
+
`Attempted to compute the number of elements in the ${kind} ${formatValue(value)}.`,
|
|
1694
|
+
);
|
|
1695
|
+
}
|
|
1411
1696
|
switch (value.kind) {
|
|
1697
|
+
case "ModelValue":
|
|
1698
|
+
throw new TlaEvaluationError(
|
|
1699
|
+
`Attempted to compute the number of elements in the model value ${formatValue(value)}.`,
|
|
1700
|
+
);
|
|
1701
|
+
case "Function":
|
|
1702
|
+
return value.domain.length;
|
|
1703
|
+
case "LazyFunction":
|
|
1704
|
+
return getCardinality(value.domain, version);
|
|
1412
1705
|
case "Interval": {
|
|
1413
1706
|
const size = Math.max(0, value.high - value.low + 1);
|
|
1414
1707
|
if (size > MAXIMUM_INTEGER) {
|
|
@@ -1424,16 +1717,19 @@ export const getCardinality = (value: Value): number => {
|
|
|
1424
1717
|
`Attempted to compute the number of elements in the overridden value ${formatValue(value)}.`,
|
|
1425
1718
|
);
|
|
1426
1719
|
case "Subset": {
|
|
1427
|
-
const size = getCardinality(value.base);
|
|
1720
|
+
const size = getCardinality(value.base, version);
|
|
1428
1721
|
if (size >= 31) throw new TlaEvaluationError(describeOverflow(value));
|
|
1429
1722
|
return 2 ** size;
|
|
1430
1723
|
}
|
|
1431
1724
|
case "FunctionSet": {
|
|
1432
|
-
if (
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1725
|
+
if (version !== "1.7.4") {
|
|
1726
|
+
if (isEmptySet(value.domain)) return 1;
|
|
1727
|
+
if (isEmptySet(value.range)) return 0;
|
|
1728
|
+
if (getCardinality(value.range, version) === 1) return 1;
|
|
1729
|
+
}
|
|
1730
|
+
const domainSize = getCardinality(value.domain, version);
|
|
1731
|
+
const rangeSize = getCardinality(value.range, version);
|
|
1732
|
+
if (rangeSize <= 1) return domainSize === 0 ? 1 : rangeSize;
|
|
1437
1733
|
const size = { value: 1 };
|
|
1438
1734
|
for (let index = 0; index < domainSize; index++) {
|
|
1439
1735
|
size.value *= rangeSize;
|
|
@@ -1442,15 +1738,17 @@ export const getCardinality = (value: Value): number => {
|
|
|
1442
1738
|
return size.value;
|
|
1443
1739
|
}
|
|
1444
1740
|
case "RecordSet":
|
|
1445
|
-
if (isEmptySet(value)) return 0;
|
|
1446
|
-
return getProductCardinality(value.sets, () => describeOverflow(value));
|
|
1741
|
+
if (version !== "1.7.4" && isEmptySet(value)) return 0;
|
|
1742
|
+
return getProductCardinality(value.sets, version, () => describeOverflow(value));
|
|
1447
1743
|
case "ProductSet":
|
|
1448
|
-
if (isEmptySet(value)) return 0;
|
|
1744
|
+
if (version !== "1.7.4" && isEmptySet(value)) return 0;
|
|
1449
1745
|
return getProductCardinality(
|
|
1450
1746
|
value.factors,
|
|
1747
|
+
version,
|
|
1451
1748
|
() => `Overflow when computing the number of elements in ${formatValue(value)}`,
|
|
1452
1749
|
);
|
|
1453
1750
|
default:
|
|
1751
|
+
cacheSet(value);
|
|
1454
1752
|
return toFiniteSet(value).elements.length;
|
|
1455
1753
|
}
|
|
1456
1754
|
};
|
|
@@ -1525,11 +1823,11 @@ export const isFiniteSet = (value: Value): boolean => {
|
|
|
1525
1823
|
}
|
|
1526
1824
|
};
|
|
1527
1825
|
|
|
1528
|
-
const findIndex = (sortedValues: Value[], target: Value) => {
|
|
1826
|
+
const findIndex = (sortedValues: Value[], target: Value, compare = compareValues) => {
|
|
1529
1827
|
const bounds = { low: 0, high: sortedValues.length - 1 };
|
|
1530
1828
|
while (bounds.low <= bounds.high) {
|
|
1531
1829
|
const middle = (bounds.low + bounds.high) >> 1;
|
|
1532
|
-
const comparison =
|
|
1830
|
+
const comparison = compare(sortedValues[middle], target);
|
|
1533
1831
|
if (comparison === 0) return middle;
|
|
1534
1832
|
if (comparison < 0) bounds.low = middle + 1;
|
|
1535
1833
|
else bounds.high = middle - 1;
|
|
@@ -1545,33 +1843,78 @@ export const getSequenceElements = (value: Value): Value[] | undefined => {
|
|
|
1545
1843
|
: undefined;
|
|
1546
1844
|
};
|
|
1547
1845
|
|
|
1548
|
-
const describeMembership = (element: Value, set: Value) =>
|
|
1549
|
-
`Attempted to check if the value ${formatValue(element)} is an element of ${formatValue(set)}`;
|
|
1550
|
-
|
|
1551
1846
|
const describeNonSetMembership = (element: Value, value: Value) => {
|
|
1552
1847
|
const prefix = `Attempted to check if the value:\n${formatValue(element)}\nis an element of the`;
|
|
1553
1848
|
if (!isFunctionValue(value)) return `${prefix} ${describeKind(value)} ${formatValue(value)}`;
|
|
1554
|
-
const
|
|
1555
|
-
|
|
1556
|
-
if (domain.length > 0 && domain.every((key) => typeof key === "string")) {
|
|
1849
|
+
const functionClass = getFunctionClass(value);
|
|
1850
|
+
if (functionClass === "record") {
|
|
1557
1851
|
return `Attempted to check if element:\n${formatValue(element)}\nis in the record:\n${formatValue(value)}`;
|
|
1558
1852
|
}
|
|
1559
|
-
if (
|
|
1560
|
-
return "Attempted to check set membership in a tuple value.";
|
|
1561
|
-
}
|
|
1853
|
+
if (functionClass === "tuple") return "Attempted to check set membership in a tuple value.";
|
|
1562
1854
|
return `${prefix} function ${formatValue(value)}`;
|
|
1563
1855
|
};
|
|
1564
1856
|
|
|
1857
|
+
// The call stack of the replay, which a failed fast path leaves frozen.
|
|
1858
|
+
interface TracedCallStack {
|
|
1859
|
+
expressions: unknown[];
|
|
1860
|
+
isFrozen: boolean;
|
|
1861
|
+
fingerprint?: unknown;
|
|
1862
|
+
}
|
|
1863
|
+
|
|
1864
|
+
// The sets whose membership is being checked or whose elements are being enumerated, and the
|
|
1865
|
+
// members being compared, outermost first, while TLC's CallStackTool replays a computation. A failed
|
|
1866
|
+
// check or enumeration leaves the values it was in, each of which TLC adds to the
|
|
1867
|
+
// FingerprintException when it has a source.
|
|
1868
|
+
export const setTrace: { sets: Value[] | undefined; callStack?: TracedCallStack } = {
|
|
1869
|
+
sets: undefined,
|
|
1870
|
+
};
|
|
1871
|
+
|
|
1872
|
+
// Takes a fast path, and computes as TLC does where it fails. TLC never takes the fast path, so
|
|
1873
|
+
// the failure's leftovers in the replay's trace go first.
|
|
1874
|
+
export const computeLikeTlcOnFailure = <Result>(
|
|
1875
|
+
compute: () => Result,
|
|
1876
|
+
computeLikeTlc: (error: TlaEvaluationError) => Result,
|
|
1877
|
+
): Result => {
|
|
1878
|
+
const sets = setTrace.sets;
|
|
1879
|
+
const callStack = setTrace.callStack;
|
|
1880
|
+
const depth = sets?.length ?? 0;
|
|
1881
|
+
const trace = callStack && { ...callStack, length: callStack.expressions.length };
|
|
1882
|
+
try {
|
|
1883
|
+
return compute();
|
|
1884
|
+
} catch (error) {
|
|
1885
|
+
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
1886
|
+
if (sets) sets.length = depth;
|
|
1887
|
+
if (callStack && trace) {
|
|
1888
|
+
callStack.expressions.length = trace.length;
|
|
1889
|
+
callStack.isFrozen = trace.isFrozen;
|
|
1890
|
+
callStack.fingerprint = trace.fingerprint;
|
|
1891
|
+
}
|
|
1892
|
+
return computeLikeTlc(error);
|
|
1893
|
+
}
|
|
1894
|
+
};
|
|
1895
|
+
|
|
1565
1896
|
export const isElementOf = (element: Value, set: Value): boolean => {
|
|
1897
|
+
const sets = setTrace.sets;
|
|
1898
|
+
if (sets === undefined) return isElementOfSet(element, set);
|
|
1899
|
+
sets.push(set);
|
|
1900
|
+
const isElement = isElementOfSet(element, set);
|
|
1901
|
+
sets.pop();
|
|
1902
|
+
return isElement;
|
|
1903
|
+
};
|
|
1904
|
+
|
|
1905
|
+
const isElementOfSet = (element: Value, set: Value): boolean => {
|
|
1566
1906
|
if (!isSetValue(set)) throw new TlaEvaluationError(describeNonSetMembership(element, set));
|
|
1567
1907
|
switch (set.kind) {
|
|
1568
1908
|
case "Set":
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1909
|
+
// SetEnumValue.member compares the element with the set's.
|
|
1910
|
+
cacheSet(element);
|
|
1911
|
+
return computeLikeTlcOnFailure(
|
|
1912
|
+
() => findIndex(set.elements, element) !== -1,
|
|
1913
|
+
() =>
|
|
1914
|
+
normalizedSets.has(set)
|
|
1915
|
+
? searchLikeTlc(element, set.elements).isFound
|
|
1916
|
+
: isElementOfLikeTlc(element, set.elements),
|
|
1917
|
+
);
|
|
1575
1918
|
case "MixedSet":
|
|
1576
1919
|
return isElementOfLikeTlc(element, set.elements);
|
|
1577
1920
|
case "Interval":
|
|
@@ -1595,12 +1938,21 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1595
1938
|
return set.name !== "Nat" || (typeof element === "number" && element >= 0);
|
|
1596
1939
|
}
|
|
1597
1940
|
case "Subset":
|
|
1598
|
-
|
|
1941
|
+
// SubsetValue.member fails on any value that is not Enumerable, model values and the sets a
|
|
1942
|
+
// Java class overrides included.
|
|
1943
|
+
if (!isSetValue(element) || isOverriddenSet(element)) {
|
|
1944
|
+
throw new TlaEvaluationError(
|
|
1945
|
+
`Attempted to check if the non-enumerable value\n${prettyPrint(formatValue(element))}\nis element of\n${prettyPrint(formatValue(set))}`,
|
|
1946
|
+
);
|
|
1947
|
+
}
|
|
1599
1948
|
return toFiniteSet(element).elements.every((item) => isElementOf(item, set.base));
|
|
1600
1949
|
case "FunctionSet": {
|
|
1601
1950
|
if (!isFunctionValue(element)) {
|
|
1602
1951
|
if (isModelValue(element)) return false;
|
|
1603
|
-
|
|
1952
|
+
// SetOfFcnsValue.member prints the element without Values.ppr.
|
|
1953
|
+
throw new TlaEvaluationError(
|
|
1954
|
+
`Attempted to check if \n${formatValue(element)}\nwhich is not a TLC function value, is in the set of functions:\n${prettyPrint(formatValue(set))}`,
|
|
1955
|
+
);
|
|
1604
1956
|
}
|
|
1605
1957
|
const functionValue = toFiniteFunction(element);
|
|
1606
1958
|
const low = getIntervalLow(functionValue.domain);
|
|
@@ -1665,7 +2017,10 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1665
2017
|
const elements = getSequenceElements(element);
|
|
1666
2018
|
if (elements === undefined) {
|
|
1667
2019
|
if (isModelValue(element)) return false;
|
|
1668
|
-
|
|
2020
|
+
// Sequences.member raises TLC_MODULE_CHECK_MEMBER_OF.
|
|
2021
|
+
throw new TlaEvaluationError(
|
|
2022
|
+
`Attempted to check if the value:\n${prettyPrint(formatValue(element))}\nis an element of ${prettyPrint(formatValue(set))}.`,
|
|
2023
|
+
);
|
|
1669
2024
|
}
|
|
1670
2025
|
return elements.every((item) => isElementOf(item, set.base));
|
|
1671
2026
|
}
|
|
@@ -1698,50 +2053,123 @@ export const toFiniteFunction = (value: Value): FunctionValue => {
|
|
|
1698
2053
|
if (typeof value === "object" && value.kind === "LazyFunction") {
|
|
1699
2054
|
if (!isEnumerable(value.domain)) getCardinality(value.domain);
|
|
1700
2055
|
const domain = getFunctionDomain(value.domain);
|
|
1701
|
-
return
|
|
1702
|
-
|
|
1703
|
-
|
|
2056
|
+
return setFunctionClass(
|
|
2057
|
+
createSortedFunction(
|
|
2058
|
+
domain,
|
|
2059
|
+
domain.map((key) => value.apply(key)),
|
|
2060
|
+
),
|
|
2061
|
+
getFunctionClass(value),
|
|
1704
2062
|
);
|
|
1705
2063
|
}
|
|
1706
2064
|
throw new TlaEvaluationError(`Expected a function but found ${formatValue(value)}`);
|
|
1707
2065
|
};
|
|
1708
2066
|
|
|
1709
|
-
const
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
2067
|
+
export const describeMissingArgument = (
|
|
2068
|
+
functionValue: Value,
|
|
2069
|
+
argument: Value,
|
|
2070
|
+
version?: TlcVersion,
|
|
1713
2071
|
) => {
|
|
1714
|
-
|
|
1715
|
-
|
|
2072
|
+
const shown = prettyPrint(formatValue(functionValue));
|
|
2073
|
+
const shownArgument = prettyPrint(formatValue(argument));
|
|
2074
|
+
const is174 = version === "1.7.4";
|
|
2075
|
+
switch (getFunctionClass(functionValue)) {
|
|
2076
|
+
case "lambda":
|
|
2077
|
+
return `In applying the function\n${shown},\nthe first argument is:\n${shownArgument}\nwhich is not in its domain.\n`;
|
|
2078
|
+
case "tuple":
|
|
2079
|
+
if (typeof argument !== "number") {
|
|
2080
|
+
return is174
|
|
2081
|
+
? "Attempted to apply tuple to a non-integer argument."
|
|
2082
|
+
: `Attempted to access tuple at a non integral index: ${shownArgument}`;
|
|
2083
|
+
}
|
|
2084
|
+
return is174
|
|
2085
|
+
? `Attempted to apply tuple\n${shown}\nto integer ${argument} which is out of domain.`
|
|
2086
|
+
: `Attempted to access index ${argument} of tuple\n${shown}\nwhich is out of bounds.`;
|
|
2087
|
+
case "record":
|
|
2088
|
+
if (typeof argument !== "string") {
|
|
2089
|
+
return is174
|
|
2090
|
+
? `Attempted to apply record to a non-string value ${shownArgument}.`
|
|
2091
|
+
: `Attempted to access record by a non-string argument: ${shownArgument}`;
|
|
2092
|
+
}
|
|
2093
|
+
return is174
|
|
2094
|
+
? `Attempted to apply the record\n${shown}\nto nonexistent record field ${argument}.`
|
|
2095
|
+
: `Attempted to access nonexistent field '${argument}' of record\n${shown}`;
|
|
2096
|
+
default:
|
|
2097
|
+
return `Attempted to apply function:\n${shown}\nto argument ${shownArgument}, which is not in the domain of the function.`;
|
|
2098
|
+
}
|
|
1716
2099
|
};
|
|
1717
2100
|
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
2101
|
+
// TLC 1.7.4 words a key of the wrong type in an EXCEPT, which selects rather than applies, apart.
|
|
2102
|
+
const describeExceptKey = (functionValue: FunctionValue, argument: Value) => {
|
|
2103
|
+
if (!valueVersion.is174) return describeMissingArgument(functionValue, argument);
|
|
2104
|
+
const shown = prettyPrint(formatValue(argument));
|
|
2105
|
+
return getFunctionClass(functionValue) === "tuple"
|
|
2106
|
+
? `Attempted to apply tuple to a non-integer argument ${shown}.`
|
|
2107
|
+
: `Attempted to apply record to a non-string argument ${shown}.`;
|
|
2108
|
+
};
|
|
1722
2109
|
|
|
1723
|
-
|
|
2110
|
+
// A key the domain holds itself, which TLC finds without a comparison that can fail.
|
|
2111
|
+
const findIdenticalKey = (domain: Value[], argument: Value) => {
|
|
1724
2112
|
const first = domain[0];
|
|
1725
2113
|
if (typeof argument === "number" && typeof first === "number") {
|
|
1726
2114
|
const offset = argument - first;
|
|
1727
2115
|
if (domain[offset] === argument) return offset;
|
|
1728
2116
|
}
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
2117
|
+
return domain.length <= 8 ? domain.indexOf(argument) : -1;
|
|
2118
|
+
};
|
|
2119
|
+
|
|
2120
|
+
export const createNonIntegerKeyError = (argument: Value) =>
|
|
2121
|
+
new TlaEvaluationError(
|
|
2122
|
+
`Attempted to apply function with integer domain to the non-integer argument ${prettyPrint(formatValue(argument))}`,
|
|
2123
|
+
);
|
|
2124
|
+
|
|
2125
|
+
// TLC's FcnRcdValue searches a domain this small linearly with equals, and a larger normalized one by
|
|
2126
|
+
// binary search with compareTo.
|
|
2127
|
+
const LINEAR_SEARCH_LIMIT = 32;
|
|
2128
|
+
|
|
2129
|
+
// TLC looks a key up by the Java class of the function: a tuple or a record rejects a key of the
|
|
2130
|
+
// wrong type, and another function rejects a non-integer for a domain it keeps as an interval, which
|
|
2131
|
+
// TLC 1.7.4 doesn't. A key of another type than the domain's keys otherwise fails TLC's comparison.
|
|
2132
|
+
const findKeyIndexLikeTlc = (
|
|
2133
|
+
functionValue: FunctionValue,
|
|
2134
|
+
argument: Value,
|
|
2135
|
+
describeWrongKey: (functionValue: FunctionValue, argument: Value) => string,
|
|
2136
|
+
) => {
|
|
2137
|
+
const domain = functionValue.domain;
|
|
2138
|
+
if (typeof argument !== typeof domain[0]) {
|
|
2139
|
+
const functionClass = getFunctionClass(functionValue);
|
|
2140
|
+
const keyType =
|
|
2141
|
+
functionClass === "tuple" ? "number" : functionClass === "record" ? "string" : undefined;
|
|
2142
|
+
if (keyType !== undefined && typeof argument !== keyType) {
|
|
2143
|
+
throw new TlaEvaluationError(describeWrongKey(functionValue, argument));
|
|
2144
|
+
}
|
|
2145
|
+
if (keyType === undefined && !valueVersion.is174 && isIntervalDomain(domain)) {
|
|
2146
|
+
throw createNonIntegerKeyError(argument);
|
|
2147
|
+
}
|
|
1732
2148
|
}
|
|
1733
|
-
|
|
2149
|
+
const identical = findIdenticalKey(domain, argument);
|
|
2150
|
+
if (identical !== -1) return identical;
|
|
2151
|
+
return computeLikeTlcOnFailure(
|
|
2152
|
+
() => findIndex(domain, argument),
|
|
2153
|
+
(error) => {
|
|
2154
|
+
if (valueVersion.is174 || domain.length < LINEAR_SEARCH_LIMIT) {
|
|
2155
|
+
domain.forEach((key) => equalsLikeTlc(key, argument));
|
|
2156
|
+
} else {
|
|
2157
|
+
findIndex(domain, argument, compareLikeTlc);
|
|
2158
|
+
}
|
|
2159
|
+
throw error;
|
|
2160
|
+
},
|
|
2161
|
+
);
|
|
1734
2162
|
};
|
|
1735
2163
|
|
|
1736
2164
|
export const applyFunction = (
|
|
1737
2165
|
functionValue: Value,
|
|
1738
2166
|
argument: Value,
|
|
1739
|
-
describeMissing?: (functionValue: FunctionValue) => string,
|
|
2167
|
+
describeMissing?: (functionValue: FunctionValue, argument: Value) => string,
|
|
1740
2168
|
): Value => {
|
|
1741
2169
|
if (typeof functionValue === "object" && functionValue.kind === "LazyFunction") {
|
|
1742
2170
|
if (!isElementOf(argument, functionValue.domain)) {
|
|
1743
2171
|
throw new TlaEvaluationError(
|
|
1744
|
-
|
|
2172
|
+
getFunctionClass(functionValue) === "lambda"
|
|
1745
2173
|
? describeMissingArgument(functionValue, argument)
|
|
1746
2174
|
: `Attempted to apply a function to ${formatValue(argument)}, which is not in its domain`,
|
|
1747
2175
|
);
|
|
@@ -1753,12 +2181,9 @@ export const applyFunction = (
|
|
|
1753
2181
|
`Attempted to apply ${formatValue(functionValue)}, which is not a function`,
|
|
1754
2182
|
);
|
|
1755
2183
|
}
|
|
1756
|
-
const
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
describeMissing?.(functionValue) ?? describeMissingArgument(functionValue, argument),
|
|
1760
|
-
);
|
|
1761
|
-
}
|
|
2184
|
+
const describe = describeMissing ?? describeMissingArgument;
|
|
2185
|
+
const position = findKeyIndexLikeTlc(functionValue, argument, describe);
|
|
2186
|
+
if (position === -1) throw new TlaEvaluationError(describe(functionValue, argument));
|
|
1762
2187
|
return functionValue.range[position];
|
|
1763
2188
|
};
|
|
1764
2189
|
|
|
@@ -1787,28 +2212,45 @@ export const updateFunction = (
|
|
|
1787
2212
|
if (isLazy) {
|
|
1788
2213
|
if (!isElementOf(argument, functionValue.domain)) return functionValue;
|
|
1789
2214
|
const updated = update(functionValue.apply(argument));
|
|
1790
|
-
return
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
2215
|
+
return setFunctionClass<LazyFunctionValue>(
|
|
2216
|
+
{
|
|
2217
|
+
kind: "LazyFunction",
|
|
2218
|
+
domain: functionValue.domain,
|
|
2219
|
+
apply: (key) => (isEqualLikeTlc(argument, key) ? updated : functionValue.apply(key)),
|
|
2220
|
+
source: functionValue.source,
|
|
2221
|
+
},
|
|
2222
|
+
getFunctionClass(functionValue),
|
|
2223
|
+
);
|
|
2224
|
+
}
|
|
2225
|
+
if (!isFunctionValue(functionValue)) {
|
|
2226
|
+
throw new TlaEvaluationError(
|
|
2227
|
+
`Attempted to apply EXCEPT construct to the value ${prettyPrint(formatValue(functionValue))}.`,
|
|
2228
|
+
);
|
|
1796
2229
|
}
|
|
1797
2230
|
const finite = toFiniteFunction(functionValue);
|
|
1798
|
-
const position =
|
|
2231
|
+
const position = findKeyIndexLikeTlc(finite, argument, describeExceptKey);
|
|
1799
2232
|
if (position === -1) return finite;
|
|
1800
2233
|
const range = [...finite.range];
|
|
1801
2234
|
const previous = finite.range[position];
|
|
1802
2235
|
const next = update(previous);
|
|
1803
2236
|
range[position] = next;
|
|
1804
|
-
const updated =
|
|
2237
|
+
const updated = setFunctionClass(
|
|
2238
|
+
createSortedFunction(finite.domain, range),
|
|
2239
|
+
getFunctionClass(finite),
|
|
2240
|
+
);
|
|
1805
2241
|
updated.writtenKeys = finite.writtenKeys;
|
|
1806
|
-
if (finite.hash !== 0) {
|
|
1807
|
-
const
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
2242
|
+
if (finite.hash !== 0 && finite.hash !== UNFINGERPRINTABLE_HASH) {
|
|
2243
|
+
const nextHash = getValueHash(next);
|
|
2244
|
+
if (nextHash !== UNFINGERPRINTABLE_HASH) {
|
|
2245
|
+
const keyHash = getValueHash(finite.domain[position]);
|
|
2246
|
+
updated.hash =
|
|
2247
|
+
previous === next
|
|
2248
|
+
? finite.hash
|
|
2249
|
+
: (finite.hash -
|
|
2250
|
+
getEntryContribution(keyHash, getValueHash(previous)) +
|
|
2251
|
+
getEntryContribution(keyHash, nextHash)) |
|
|
2252
|
+
0;
|
|
2253
|
+
}
|
|
1812
2254
|
}
|
|
1813
2255
|
if (finite.fingerprint !== 0) {
|
|
1814
2256
|
const key = finite.domain[position];
|
|
@@ -1845,32 +2287,103 @@ const hashString = (value: string) => {
|
|
|
1845
2287
|
const SET_HASH_SEED = 0x73a5c3e1;
|
|
1846
2288
|
const FUNCTION_HASH_SEED = 0x66d1b4f7;
|
|
1847
2289
|
|
|
1848
|
-
const getElementContribution = (
|
|
1849
|
-
finalizeHash((
|
|
2290
|
+
const getElementContribution = (elementHash: number) =>
|
|
2291
|
+
finalizeHash((elementHash + 0x9e3779b9) | 0);
|
|
1850
2292
|
|
|
1851
|
-
const getEntryContribution = (
|
|
1852
|
-
finalizeHash((Math.imul(
|
|
2293
|
+
const getEntryContribution = (keyHash: number, valueHash: number) =>
|
|
2294
|
+
finalizeHash((Math.imul(keyHash, 0x27d4eb2d) + valueHash) | 0);
|
|
2295
|
+
|
|
2296
|
+
// The hash of each value that TLC fails to fingerprint, such as Nat or a set that holds it. Calls
|
|
2297
|
+
// with such a value as an argument still share their result, and a state that holds one fails
|
|
2298
|
+
// when TLC fingerprints it.
|
|
2299
|
+
const UNFINGERPRINTABLE_HASH = 0x2f6b1c4d;
|
|
1853
2300
|
|
|
1854
2301
|
const computeValueHash = (value: ObjectValue) => {
|
|
1855
2302
|
if (value.kind === "ModelValue") return finalizeHash(combineHash(hashString(value.name), 0x6d));
|
|
1856
2303
|
if (isSetValue(value)) {
|
|
1857
|
-
if (!isEnumerable(value)) return
|
|
2304
|
+
if (!isEnumerable(value)) return UNFINGERPRINTABLE_HASH;
|
|
1858
2305
|
const elements = toFiniteSet(value).elements;
|
|
1859
2306
|
let hash = SET_HASH_SEED;
|
|
1860
2307
|
for (let index = 0; index < elements.length; index++) {
|
|
1861
|
-
|
|
2308
|
+
const elementHash = getValueHash(elements[index]);
|
|
2309
|
+
if (elementHash === UNFINGERPRINTABLE_HASH) return UNFINGERPRINTABLE_HASH;
|
|
2310
|
+
hash = (hash + getElementContribution(elementHash)) | 0;
|
|
1862
2311
|
}
|
|
1863
2312
|
return hash;
|
|
1864
2313
|
}
|
|
2314
|
+
if (value.kind === "LazyFunction" && !isEnumerable(value.domain)) return UNFINGERPRINTABLE_HASH;
|
|
1865
2315
|
const functionValue = toFiniteFunction(value);
|
|
1866
2316
|
let hash = FUNCTION_HASH_SEED;
|
|
1867
2317
|
for (let index = 0; index < functionValue.domain.length; index++) {
|
|
1868
|
-
|
|
1869
|
-
|
|
2318
|
+
const keyHash = getValueHash(functionValue.domain[index]);
|
|
2319
|
+
const itemHash = getValueHash(functionValue.range[index]);
|
|
2320
|
+
if (keyHash === UNFINGERPRINTABLE_HASH || itemHash === UNFINGERPRINTABLE_HASH) {
|
|
2321
|
+
return UNFINGERPRINTABLE_HASH;
|
|
2322
|
+
}
|
|
2323
|
+
hash = (hash + getEntryContribution(keyHash, itemHash)) | 0;
|
|
1870
2324
|
}
|
|
1871
2325
|
return hash;
|
|
1872
2326
|
};
|
|
1873
2327
|
|
|
2328
|
+
// TLC's fingerPrint enumerates each set and function a value holds, and fails on Nat, Seq(S) and
|
|
2329
|
+
// the like, which it never enumerates.
|
|
2330
|
+
const fingerprintValue = (value: Value): void => {
|
|
2331
|
+
if (typeof value !== "object" || value.kind === "ModelValue") return;
|
|
2332
|
+
if (value.kind === "InfiniteSet" || value.kind === "SequenceSet") {
|
|
2333
|
+
throw new TlaEvaluationError(
|
|
2334
|
+
`TLC has found a state in which the value of a variable contains ${formatValue(value)}`,
|
|
2335
|
+
);
|
|
2336
|
+
}
|
|
2337
|
+
if (isSetValue(value)) {
|
|
2338
|
+
toFiniteSet(value).elements.forEach(fingerprintValue);
|
|
2339
|
+
return;
|
|
2340
|
+
}
|
|
2341
|
+
const functionValue = toFiniteFunction(value);
|
|
2342
|
+
functionValue.domain.forEach(fingerprintValue);
|
|
2343
|
+
functionValue.range.forEach(fingerprintValue);
|
|
2344
|
+
};
|
|
2345
|
+
|
|
2346
|
+
// The error TLC raises as it fingerprints a value: one that hashing the value raises, as for a set
|
|
2347
|
+
// too large to enumerate, or one of a value with the hash of the values TLC fails on.
|
|
2348
|
+
export const getFingerprintError = (value: Value) => {
|
|
2349
|
+
if (typeof value !== "object" || value.kind === "ModelValue") return undefined;
|
|
2350
|
+
try {
|
|
2351
|
+
if (getValueHash(value) === UNFINGERPRINTABLE_HASH) fingerprintValue(value);
|
|
2352
|
+
return undefined;
|
|
2353
|
+
} catch (error) {
|
|
2354
|
+
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
2355
|
+
return error;
|
|
2356
|
+
}
|
|
2357
|
+
};
|
|
2358
|
+
|
|
2359
|
+
// The values a value holds, which TLC fingerprints as it fingerprints the value.
|
|
2360
|
+
export const getHeldValues = (value: ObjectValue): Value[] => {
|
|
2361
|
+
switch (value.kind) {
|
|
2362
|
+
case "Set":
|
|
2363
|
+
case "MixedSet":
|
|
2364
|
+
return value.elements;
|
|
2365
|
+
case "Function":
|
|
2366
|
+
return [...value.domain, ...value.range];
|
|
2367
|
+
case "Subset":
|
|
2368
|
+
case "SequenceSet":
|
|
2369
|
+
case "FilteredSet":
|
|
2370
|
+
case "Union":
|
|
2371
|
+
return [value.base];
|
|
2372
|
+
case "FunctionSet":
|
|
2373
|
+
return [value.domain, value.range];
|
|
2374
|
+
case "RecordSet":
|
|
2375
|
+
return value.sets;
|
|
2376
|
+
case "ProductSet":
|
|
2377
|
+
return value.factors;
|
|
2378
|
+
case "SetOperation":
|
|
2379
|
+
return [value.left, value.right];
|
|
2380
|
+
case "LazyFunction":
|
|
2381
|
+
return [value.domain];
|
|
2382
|
+
default:
|
|
2383
|
+
return [];
|
|
2384
|
+
}
|
|
2385
|
+
};
|
|
2386
|
+
|
|
1874
2387
|
export const getValueHash = (value: Value): number => {
|
|
1875
2388
|
if (typeof value === "number") return finalizeHash(value ^ 0x5bd1e995);
|
|
1876
2389
|
if (typeof value === "boolean") return value ? 0x2b7e1516 : 0x28aed2a6;
|
|
@@ -2045,7 +2558,9 @@ const formatLazySet = (value: LazySetValue, isStrict: boolean): string => {
|
|
|
2045
2558
|
case "SetOperation":
|
|
2046
2559
|
return `${format(value.left)} ${value.operator} ${format(value.right)}`;
|
|
2047
2560
|
case "FilteredSet":
|
|
2048
|
-
return
|
|
2561
|
+
return value.source
|
|
2562
|
+
? `{${value.source.names} \\in ${format(value.base)} : <expression ${value.source.predicate}> }`
|
|
2563
|
+
: `{x \\in ${format(value.base)} : ...}`;
|
|
2049
2564
|
case "Union":
|
|
2050
2565
|
return formatUnion(value, isStrict);
|
|
2051
2566
|
}
|
|
@@ -2058,6 +2573,17 @@ const isFieldName = (name: string) =>
|
|
|
2058
2573
|
/\p{L}/u.test(name) &&
|
|
2059
2574
|
(name.length < 4 || !/^[WS]F_/.test(name));
|
|
2060
2575
|
|
|
2576
|
+
// FcnLambdaValue#toString prints a function it fails to enumerate as its parameters and body.
|
|
2577
|
+
const toPrintedFunction = (value: LazyFunctionValue) => {
|
|
2578
|
+
if (!isEnumerable(value.domain)) return undefined;
|
|
2579
|
+
try {
|
|
2580
|
+
return toFiniteFunction(value);
|
|
2581
|
+
} catch (error) {
|
|
2582
|
+
if (error instanceof TlaEvaluationError) return undefined;
|
|
2583
|
+
throw error;
|
|
2584
|
+
}
|
|
2585
|
+
};
|
|
2586
|
+
|
|
2061
2587
|
export const formatValue = (value: Value, isStrict = false, isNormalized = false): string => {
|
|
2062
2588
|
const format = (inner: Value) => formatValue(inner, isStrict, isNormalized);
|
|
2063
2589
|
if (typeof value === "boolean") return value ? "TRUE" : "FALSE";
|
|
@@ -2089,27 +2615,53 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
|
|
|
2089
2615
|
return `Seq(${format(value.base)})`;
|
|
2090
2616
|
case "Function":
|
|
2091
2617
|
case "LazyFunction": {
|
|
2092
|
-
|
|
2093
|
-
|
|
2618
|
+
const functionValue = value.kind === "LazyFunction" ? toPrintedFunction(value) : value;
|
|
2619
|
+
if (functionValue === undefined) {
|
|
2620
|
+
const source = value.kind === "LazyFunction" ? value.source : undefined;
|
|
2094
2621
|
if (!source) return "<function>";
|
|
2095
2622
|
const parameters = source.parameters
|
|
2096
2623
|
.map((parameter) => `${parameter.name} \\in ${formatValue(parameter.domain)}`)
|
|
2097
2624
|
.join(", ");
|
|
2098
2625
|
return `[${parameters} |-> <expression ${source.body}>]`;
|
|
2099
2626
|
}
|
|
2100
|
-
|
|
2627
|
+
if (
|
|
2628
|
+
functionValue.domain.length === 0 &&
|
|
2629
|
+
valueVersion.is174 &&
|
|
2630
|
+
getFunctionClass(value) !== "tuple"
|
|
2631
|
+
) {
|
|
2632
|
+
return "<< >>";
|
|
2633
|
+
}
|
|
2634
|
+
// TLC normalizes a lambda's domain to print it, but evaluates its body afterward.
|
|
2635
|
+
const formatRange = (inner: Value) =>
|
|
2636
|
+
formatValue(inner, isStrict, isNormalized && getFunctionClass(value) !== "lambda");
|
|
2101
2637
|
if (functionValue.domain.every((key, index) => key === index + 1)) {
|
|
2102
|
-
return `<<${functionValue.range.map(
|
|
2638
|
+
return `<<${functionValue.range.map(formatRange).join(", ")}>>`;
|
|
2103
2639
|
}
|
|
2104
|
-
|
|
2640
|
+
// A RecordValue prints as a record whatever its fields, and a FcnRcdValue only if each key
|
|
2641
|
+
// is a name.
|
|
2642
|
+
if (
|
|
2643
|
+
getFunctionClass(value) === "record" ||
|
|
2644
|
+
functionValue.domain.every((key) => typeof key === "string" && isFieldName(key))
|
|
2645
|
+
) {
|
|
2105
2646
|
const isSorted =
|
|
2106
2647
|
isNormalized || functionValue.hash !== 0 || functionValue.fingerprint !== 0;
|
|
2107
2648
|
const pairs = isSorted
|
|
2108
2649
|
? functionValue.domain.map((key, index) => [key, functionValue.range[index]])
|
|
2109
2650
|
: getWrittenPairs(functionValue);
|
|
2110
|
-
return `[${pairs.map((pair) => `${String(pair[0])} |-> ${
|
|
2651
|
+
return `[${pairs.map((pair) => `${String(pair[0])} |-> ${formatRange(pair[1])}`).join(", ")}]`;
|
|
2111
2652
|
}
|
|
2112
|
-
return `(${functionValue.domain.map((key, index) => `${format(key)} :> ${
|
|
2653
|
+
return `(${functionValue.domain.map((key, index) => `${format(key)} :> ${formatRange(functionValue.range[index])}`).join(" @@ ")})`;
|
|
2113
2654
|
}
|
|
2114
2655
|
}
|
|
2115
2656
|
};
|
|
2657
|
+
|
|
2658
|
+
// TLC's TLCState.toString: a line per variable, as a conjunction when there are several.
|
|
2659
|
+
export const formatState = (state: Record<string, Value>) => {
|
|
2660
|
+
const entries = Object.entries(state);
|
|
2661
|
+
return entries
|
|
2662
|
+
.map(
|
|
2663
|
+
([name, value]) =>
|
|
2664
|
+
`${entries.length === 1 ? "" : "/\\ "}${name} = ${prettyPrint(formatValue(value))}\n`,
|
|
2665
|
+
)
|
|
2666
|
+
.join("");
|
|
2667
|
+
};
|