tla-ts 0.0.8 → 0.0.9-dev.0dd11f695
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 +30 -0
- package/dist/compile.cjs +23 -0
- package/dist/compile.js +23 -0
- package/dist/deciders.cjs +15 -15
- package/dist/deciders.js +40 -40
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +98 -19
- package/dist/index.d.ts +98 -19
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/prove-worker.cjs +1 -1
- package/dist/prove-worker.js +1 -1
- package/dist/prove.cjs +116 -36
- package/dist/prove.js +116 -36
- package/dist/run.cjs +143 -31
- package/dist/run.js +143 -31
- package/dist/tlapm.cjs +2 -2
- package/dist/tlapm.js +1 -1
- package/dist/transfer.cjs +128 -106
- package/dist/transfer.js +128 -106
- package/dist/worker.cjs +7 -7
- package/dist/worker.js +7 -7
- package/package.json +3 -1
- package/src/ast.ts +7 -21
- package/src/bags.ts +99 -99
- package/src/builtins.ts +44 -75
- package/src/bundled-modules.ts +1 -8
- package/src/check.ts +605 -311
- package/src/cli/options.ts +12 -3
- package/src/cli/run.ts +2 -2
- package/src/cli/runtime-invariants.ts +135 -0
- package/src/cli/shared.ts +20 -9
- package/src/cli/tlc-arguments.ts +646 -0
- package/src/cli/tlc-usage.ts +362 -0
- package/src/cli/tlc.ts +571 -185
- package/src/cli/typescript.ts +26 -0
- package/src/cli/xml-exporter.ts +4 -10
- package/src/codegen.ts +190 -51
- package/src/config.ts +101 -68
- package/src/coverage.ts +855 -0
- package/src/dependencies.ts +340 -24
- package/src/dfid.ts +636 -0
- package/src/environment.ts +289 -109
- package/src/errors.ts +142 -45
- package/src/evaluate.ts +734 -980
- package/src/exit-codes.ts +37 -1
- package/src/index.ts +3 -0
- package/src/instances.ts +85 -9
- package/src/java-overrides.ts +2 -12
- package/src/json-reader.ts +456 -0
- package/src/json.ts +262 -139
- package/src/levels.ts +2 -3
- package/src/lexer.ts +150 -70
- package/src/liveness-checker.ts +10 -18
- package/src/liveness.ts +122 -64
- package/src/parallel-worker.ts +14 -10
- package/src/parallel.ts +41 -17
- package/src/parser.ts +436 -90
- package/src/pcal/ast.ts +7 -28
- package/src/pcal/parse.ts +11 -39
- package/src/pcal/tokenize.ts +4 -20
- package/src/pcal/translate.ts +10 -42
- package/src/pcal/validate.ts +3 -21
- package/src/pretty-print.ts +1 -2
- package/src/prover/smt/solver/solver.ts +31 -27
- package/src/randomization.ts +84 -35
- package/src/resolve.ts +18 -60
- package/src/runtime.ts +11 -4
- package/src/sany/builtins.ts +3 -4
- package/src/sany/generator.ts +128 -176
- package/src/sany/levels.ts +6 -30
- package/src/sany/lexer.ts +7 -23
- package/src/sany/linter.ts +31 -29
- package/src/sany/modules.ts +1 -5
- package/src/sany/nodes.ts +4 -28
- package/src/sany/parse-error.ts +44 -100
- package/src/sany/report.ts +25 -40
- package/src/sany/selectors.ts +8 -22
- package/src/sany/source.ts +8 -15
- package/src/sany/xml.ts +11 -42
- package/src/search.ts +550 -150
- package/src/simulate.ts +694 -0
- package/src/spec-description.ts +167 -49
- package/src/standard-definitions.ts +2 -3
- package/src/standard-modules.ts +264 -68
- package/src/state-graph.ts +51 -18
- package/src/subexpressions.ts +31 -5
- package/src/successors.ts +269 -85
- package/src/symmetry.ts +59 -147
- package/src/tableau.ts +1 -2
- package/src/tlc/fingerprint.ts +226 -151
- package/src/tlc/jar-data.ts +113 -6
- package/src/tlc/messages.ts +122 -37
- package/src/tlc/order.ts +56 -82
- package/src/tlc/random.ts +16 -16
- package/src/tlc/replay.ts +564 -212
- package/src/tlc/summary.ts +2 -4
- package/src/tlc/tlc-operators.ts +81 -36
- package/src/tlc/tlcext-operators.ts +138 -117
- package/src/tlc/trace-spec.ts +273 -0
- package/src/tlc/value-stream.ts +143 -0
- package/src/tlc/version.ts +2 -4
- package/src/typescript/actions.ts +494 -0
- package/src/typescript/collections.ts +615 -0
- package/src/typescript/compile.ts +70 -0
- package/src/typescript/context.ts +106 -0
- package/src/typescript/emit.ts +122 -0
- package/src/typescript/expressions.ts +508 -0
- package/src/typescript/formula.ts +160 -0
- package/src/typescript/functions.ts +295 -0
- package/src/typescript/model.ts +164 -0
- package/src/typescript/names.ts +116 -0
- package/src/typescript/program.ts +51 -0
- package/src/typescript/properties.ts +148 -0
- package/src/typescript/scope.ts +117 -0
- package/src/typescript/steps.ts +175 -0
- package/src/typescript/types.ts +178 -0
- package/src/values.ts +910 -318
package/src/values.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
isStackOverflow,
|
|
3
|
+
TlaAliasFormatError,
|
|
4
|
+
TlaEvaluationError,
|
|
5
|
+
TlaFeatureUnsupportedError,
|
|
6
|
+
TlaModuleError,
|
|
7
|
+
TlaSetBoundError,
|
|
8
|
+
} from "./errors";
|
|
2
9
|
import { prettyPrint } from "./pretty-print";
|
|
10
|
+
import { isEvalException } from "./tlc/messages";
|
|
3
11
|
import type { TlcVersion } from "./tlc/version";
|
|
4
12
|
|
|
5
13
|
export interface ModelValue {
|
|
@@ -7,19 +15,30 @@ export interface ModelValue {
|
|
|
7
15
|
name: string;
|
|
8
16
|
type: string | undefined;
|
|
9
17
|
rank: number;
|
|
18
|
+
hash: number;
|
|
19
|
+
fingerprint: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface LazySet {
|
|
23
|
+
enumerated?: SetValue;
|
|
24
|
+
isCached?: boolean;
|
|
25
|
+
isFingerprinted?: boolean;
|
|
10
26
|
}
|
|
11
27
|
|
|
12
28
|
export interface SetValue {
|
|
13
29
|
kind: "Set";
|
|
14
30
|
elements: Value[];
|
|
31
|
+
// A set enumeration's unsorted elements, duplicates included, until TLC normalizes it.
|
|
32
|
+
writtenElements: Value[] | undefined;
|
|
33
|
+
// Once normalized, TLC checks membership by binary search with compareTo, not with equals.
|
|
34
|
+
isNormalized: boolean;
|
|
15
35
|
hash: number;
|
|
16
36
|
fingerprint: number;
|
|
17
37
|
isFingerprinted: boolean;
|
|
18
38
|
}
|
|
19
39
|
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
export interface MixedSetValue {
|
|
40
|
+
// TLC sorts a set enumeration only when it has to, so it checks membership element by element.
|
|
41
|
+
export interface MixedSetValue extends LazySet {
|
|
23
42
|
kind: "MixedSet";
|
|
24
43
|
elements: Value[];
|
|
25
44
|
}
|
|
@@ -32,6 +51,8 @@ export interface FunctionValue {
|
|
|
32
51
|
fingerprint: number;
|
|
33
52
|
writtenKeys: Value[] | undefined;
|
|
34
53
|
functionClass: FunctionClass | undefined;
|
|
54
|
+
// The set a lambda ranges over, which TLC's DOMAIN returns until it builds the function.
|
|
55
|
+
lambdaDomain: Value | undefined;
|
|
35
56
|
isFingerprinted: boolean;
|
|
36
57
|
}
|
|
37
58
|
|
|
@@ -46,34 +67,34 @@ export interface InfiniteSetValue {
|
|
|
46
67
|
name: "Nat" | "Int" | "STRING" | "Any";
|
|
47
68
|
}
|
|
48
69
|
|
|
49
|
-
export interface SubsetValue {
|
|
70
|
+
export interface SubsetValue extends LazySet {
|
|
50
71
|
kind: "Subset";
|
|
51
72
|
base: Value;
|
|
52
73
|
}
|
|
53
74
|
|
|
54
|
-
export interface FunctionSetValue {
|
|
75
|
+
export interface FunctionSetValue extends LazySet {
|
|
55
76
|
kind: "FunctionSet";
|
|
56
77
|
domain: Value;
|
|
57
78
|
range: Value;
|
|
58
79
|
}
|
|
59
80
|
|
|
60
|
-
export interface RecordSetValue {
|
|
81
|
+
export interface RecordSetValue extends LazySet {
|
|
61
82
|
kind: "RecordSet";
|
|
62
83
|
names: string[];
|
|
63
84
|
sets: Value[];
|
|
64
85
|
}
|
|
65
86
|
|
|
66
|
-
export interface ProductSetValue {
|
|
87
|
+
export interface ProductSetValue extends LazySet {
|
|
67
88
|
kind: "ProductSet";
|
|
68
89
|
factors: Value[];
|
|
69
90
|
}
|
|
70
91
|
|
|
71
|
-
export interface SequenceSetValue {
|
|
92
|
+
export interface SequenceSetValue extends LazySet {
|
|
72
93
|
kind: "SequenceSet";
|
|
73
94
|
base: Value;
|
|
74
95
|
}
|
|
75
96
|
|
|
76
|
-
export interface SetOperationValue {
|
|
97
|
+
export interface SetOperationValue extends LazySet {
|
|
77
98
|
kind: "SetOperation";
|
|
78
99
|
operator: "\\cup" | "\\cap" | "\\";
|
|
79
100
|
left: Value;
|
|
@@ -86,15 +107,15 @@ interface FilteredSetSource {
|
|
|
86
107
|
predicate: string;
|
|
87
108
|
}
|
|
88
109
|
|
|
89
|
-
export interface FilteredSetValue {
|
|
110
|
+
export interface FilteredSetValue extends LazySet {
|
|
90
111
|
kind: "FilteredSet";
|
|
91
112
|
base: Value;
|
|
92
|
-
test: (element: Value) => boolean;
|
|
113
|
+
test: (element: Value, isMember: boolean) => boolean;
|
|
93
114
|
source?: FilteredSetSource;
|
|
94
115
|
}
|
|
95
116
|
|
|
96
117
|
// UNION S when some element of S isn't a set enumeration. TLC enumerates it only when it has to.
|
|
97
|
-
export interface UnionValue {
|
|
118
|
+
export interface UnionValue extends LazySet {
|
|
98
119
|
kind: "Union";
|
|
99
120
|
base: Value;
|
|
100
121
|
}
|
|
@@ -109,12 +130,17 @@ export interface LazyFunctionSource {
|
|
|
109
130
|
body: string;
|
|
110
131
|
}
|
|
111
132
|
|
|
133
|
+
// enumerated is the FcnRcdValue TLC converts the function to once it compares or fingerprints it.
|
|
112
134
|
export interface LazyFunctionValue {
|
|
113
135
|
kind: "LazyFunction";
|
|
114
136
|
domain: Value;
|
|
115
137
|
apply: (argument: Value) => Value;
|
|
138
|
+
// The length of each bound's tuple pattern, or undefined for a name.
|
|
139
|
+
boundTupleLengths?: (number | undefined)[];
|
|
116
140
|
source?: LazyFunctionSource;
|
|
117
141
|
functionClass?: FunctionClass;
|
|
142
|
+
enumerated?: FunctionValue;
|
|
143
|
+
isFingerprinted?: boolean;
|
|
118
144
|
}
|
|
119
145
|
|
|
120
146
|
export type Value =
|
|
@@ -139,9 +165,62 @@ export type Value =
|
|
|
139
165
|
|
|
140
166
|
export type ObjectValue = Exclude<Value, boolean | number | string>;
|
|
141
167
|
|
|
168
|
+
type ConvertibleSetValue =
|
|
169
|
+
| MixedSetValue
|
|
170
|
+
| SubsetValue
|
|
171
|
+
| FunctionSetValue
|
|
172
|
+
| RecordSetValue
|
|
173
|
+
| ProductSetValue
|
|
174
|
+
| SequenceSetValue
|
|
175
|
+
| SetOperationValue
|
|
176
|
+
| FilteredSetValue
|
|
177
|
+
| UnionValue;
|
|
178
|
+
|
|
179
|
+
const isLazySet = (value: ObjectValue): value is ConvertibleSetValue =>
|
|
180
|
+
value.kind !== "ModelValue" &&
|
|
181
|
+
value.kind !== "Set" &&
|
|
182
|
+
value.kind !== "Function" &&
|
|
183
|
+
value.kind !== "Interval" &&
|
|
184
|
+
value.kind !== "InfiniteSet" &&
|
|
185
|
+
value.kind !== "LazyFunction";
|
|
186
|
+
|
|
142
187
|
export const MAXIMUM_INTEGER = 2147483647;
|
|
188
|
+
|
|
189
|
+
const DEFAULT_SET_BOUND = 1000000;
|
|
190
|
+
const VALUE_VEC_CAPACITY = 10;
|
|
191
|
+
|
|
192
|
+
const setBound = { size: DEFAULT_SET_BOUND };
|
|
193
|
+
|
|
194
|
+
export const getSetBound = () => setBound.size;
|
|
195
|
+
|
|
196
|
+
// A ValueVec that TLC grows one element at a time starts with room for 10, and fails to grow once
|
|
197
|
+
// its room reaches the bound.
|
|
198
|
+
export const getMaximumSetSize = () => Math.max(setBound.size, VALUE_VEC_CAPACITY);
|
|
199
|
+
|
|
200
|
+
export const getSetBoundMessage = () =>
|
|
201
|
+
`Attempted to construct a set with too many elements (>${setBound.size}).`;
|
|
202
|
+
|
|
203
|
+
export const addBoundedElement = (elements: Value[], element: Value) => {
|
|
204
|
+
if (elements.length === getMaximumSetSize()) throw new TlaSetBoundError(getSetBoundMessage());
|
|
205
|
+
elements.push(element);
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
export const isBeyondSetBound = (value: Value): value is IntervalValue =>
|
|
209
|
+
typeof value === "object" &&
|
|
210
|
+
value.kind === "Interval" &&
|
|
211
|
+
value.high - value.low >= getMaximumSetSize();
|
|
212
|
+
|
|
213
|
+
// TLC steps through an interval without building its elements.
|
|
214
|
+
export const forEachElement = (
|
|
215
|
+
elements: IntervalValue | Value[],
|
|
216
|
+
visit: (element: Value) => void,
|
|
217
|
+
) => {
|
|
218
|
+
if (Array.isArray(elements)) return elements.forEach(visit);
|
|
219
|
+
for (let element = elements.low; element <= elements.high; element++) visit(element);
|
|
220
|
+
};
|
|
143
221
|
export const MINIMUM_INTEGER = -2147483648;
|
|
144
222
|
const MAXIMUM_ENUMERATED_SUBSET_BASE = 30;
|
|
223
|
+
const MAXIMUM_BUILT_SUBSET_BASE = 20;
|
|
145
224
|
|
|
146
225
|
export const isSetValue = (value: Value) =>
|
|
147
226
|
typeof value === "object" &&
|
|
@@ -149,8 +228,6 @@ export const isSetValue = (value: Value) =>
|
|
|
149
228
|
value.kind !== "Function" &&
|
|
150
229
|
value.kind !== "LazyFunction";
|
|
151
230
|
|
|
152
|
-
// TLC's Reducible values, SetEnumValue and IntervalValue, which combine with another set element by
|
|
153
|
-
// element.
|
|
154
231
|
export const isReducibleSet = (value: Value): value is SetValue | MixedSetValue | IntervalValue =>
|
|
155
232
|
typeof value === "object" &&
|
|
156
233
|
(value.kind === "Set" || value.kind === "MixedSet" || value.kind === "Interval");
|
|
@@ -175,9 +252,9 @@ export const describeKind = (value: Value) => {
|
|
|
175
252
|
export const isTlcInteger = (value: number) =>
|
|
176
253
|
Number.isInteger(value) && value <= MAXIMUM_INTEGER && value >= MINIMUM_INTEGER;
|
|
177
254
|
|
|
178
|
-
// TLC keeps a record, or a function
|
|
179
|
-
// it
|
|
180
|
-
// show
|
|
255
|
+
// TLC keeps a record, or a function @@ builds, in written order until it sorts it to compare or
|
|
256
|
+
// fingerprint it. It searches the function in that order, and ToString, Assert, and error
|
|
257
|
+
// messages show it, unless every key is an integer.
|
|
181
258
|
const writtenOrderUse = { isUsed: false };
|
|
182
259
|
|
|
183
260
|
const setWrittenOrder = (functionValue: FunctionValue, keys: Value[]) => {
|
|
@@ -186,16 +263,27 @@ const setWrittenOrder = (functionValue: FunctionValue, keys: Value[]) => {
|
|
|
186
263
|
return functionValue;
|
|
187
264
|
};
|
|
188
265
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
266
|
+
const getWrittenKeys = (functionValue: FunctionValue) =>
|
|
267
|
+
(functionValue.hash !== 0 || functionValue.fingerprint !== 0
|
|
268
|
+
? undefined
|
|
269
|
+
: functionValue.writtenKeys) ?? functionValue.domain;
|
|
270
|
+
|
|
271
|
+
export const getWrittenPairs = (functionValue: FunctionValue): [Value, Value][] =>
|
|
272
|
+
getWrittenKeys(functionValue).map((key) => [
|
|
273
|
+
key,
|
|
274
|
+
functionValue.range[functionValue.domain.indexOf(key)],
|
|
275
|
+
]);
|
|
276
|
+
|
|
277
|
+
// TLC sorts a function whose keys are all integers to check whether it is a tuple.
|
|
278
|
+
export const getPrintedPairs = (functionValue: FunctionValue): [Value, Value][] =>
|
|
279
|
+
functionValue.domain.every((key) => typeof key === "number")
|
|
280
|
+
? functionValue.domain.map((key, index) => [key, functionValue.range[index]])
|
|
281
|
+
: getWrittenPairs(functionValue);
|
|
193
282
|
|
|
194
283
|
export const createWrittenRecord = (names: Value[], range: Value[], writtenNames: Value[]) =>
|
|
195
284
|
setWrittenOrder(createSortedRecord(names, range), writtenNames);
|
|
196
285
|
|
|
197
|
-
// TLC sorts a value it compares even with itself,
|
|
198
|
-
// hashed or fingerprinted is already sorted.
|
|
286
|
+
// TLC sorts a value it compares, even with itself, so a hashed or fingerprinted value is sorted.
|
|
199
287
|
const forgetWrittenOrder = (value: Value): void => {
|
|
200
288
|
if (typeof value !== "object" || (value.kind !== "Function" && value.kind !== "Set")) return;
|
|
201
289
|
if (value.hash !== 0 || value.fingerprint !== 0) return;
|
|
@@ -206,8 +294,7 @@ const forgetWrittenOrder = (value: Value): void => {
|
|
|
206
294
|
|
|
207
295
|
const isRecordDomain = (domain: Value[]) => domain.every((key) => typeof key === "string");
|
|
208
296
|
|
|
209
|
-
// TLC compares a record only with
|
|
210
|
-
// can fail before the sizes decide.
|
|
297
|
+
// TLC compares a record only with a record, so this can fail before the sizes decide.
|
|
211
298
|
const isRecordMismatch = (left: Value[], right: Value[]) =>
|
|
212
299
|
left.length > 0 && right.length > 0 && isRecordDomain(left) !== isRecordDomain(right);
|
|
213
300
|
|
|
@@ -281,26 +368,71 @@ const checkModelValueTypes = (left: Value, right: Value, isEquality: boolean) =>
|
|
|
281
368
|
);
|
|
282
369
|
};
|
|
283
370
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
371
|
+
interface StringOrder {
|
|
372
|
+
ranks: Record<string, number>;
|
|
373
|
+
size: number;
|
|
374
|
+
// A parallel search's workers rank the strings they create in different orders, so the search
|
|
375
|
+
// notes the first string it orders against another string ranked from this rank on.
|
|
376
|
+
searchRank: number | undefined;
|
|
377
|
+
unorderedString: string | undefined;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// TLC orders strings and model values by first interning, so each check sets its order first.
|
|
381
|
+
const stringOrder: StringOrder = {
|
|
287
382
|
ranks: Object.create(null),
|
|
288
383
|
size: 0,
|
|
384
|
+
searchRank: undefined,
|
|
385
|
+
unorderedString: undefined,
|
|
289
386
|
};
|
|
290
387
|
|
|
291
388
|
export const setStringOrder = (texts: Iterable<string>) => {
|
|
292
389
|
stringOrder.ranks = Object.create(null);
|
|
293
390
|
stringOrder.size = 0;
|
|
391
|
+
stringOrder.searchRank = undefined;
|
|
392
|
+
stringOrder.unorderedString = undefined;
|
|
294
393
|
modelValues.clear();
|
|
295
394
|
for (const text of texts) rankString(text);
|
|
296
395
|
};
|
|
297
396
|
|
|
298
|
-
|
|
299
|
-
|
|
397
|
+
export const freezeStringOrder = () => {
|
|
398
|
+
stringOrder.searchRank = stringOrder.size;
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
export const getUnorderedString = () => stringOrder.unorderedString;
|
|
402
|
+
|
|
403
|
+
// TLC 1.7.4 prints and looks keys up in functions differently, so each check sets the version.
|
|
300
404
|
const valueVersion = { is174: false };
|
|
301
405
|
|
|
302
|
-
|
|
303
|
-
|
|
406
|
+
const printing = { isExpanded: true, isViewPrinted: false };
|
|
407
|
+
|
|
408
|
+
export interface ValueOptions {
|
|
409
|
+
version?: TlcVersion;
|
|
410
|
+
maxSetSize?: number;
|
|
411
|
+
isLazyValueExpanded?: boolean;
|
|
412
|
+
isViewPrinted?: boolean;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
export const setValueOptions = (options: ValueOptions) => {
|
|
416
|
+
valueVersion.is174 = options.version === "1.7.4";
|
|
417
|
+
setBound.size = options.maxSetSize ?? DEFAULT_SET_BOUND;
|
|
418
|
+
printing.isExpanded = options.isLazyValueExpanded ?? true;
|
|
419
|
+
printing.isViewPrinted = options.isViewPrinted ?? false;
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
// TLC evaluates the VIEW of a state it prints as it prints it.
|
|
423
|
+
const viewTexts = new WeakMap<Record<string, Value>, () => string>();
|
|
424
|
+
|
|
425
|
+
export const setViewText = (state: Record<string, Value>, getText: () => string) => {
|
|
426
|
+
if (printing.isViewPrinted) viewTexts.set(state, getText);
|
|
427
|
+
return state;
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
// TLC prints by its _format only the state an ALIAS gives.
|
|
431
|
+
const aliasNames = new WeakMap<Record<string, Value>, string>();
|
|
432
|
+
|
|
433
|
+
export const setAliasName = (state: Record<string, Value>, alias: string) => {
|
|
434
|
+
aliasNames.set(state, alias);
|
|
435
|
+
return state;
|
|
304
436
|
};
|
|
305
437
|
|
|
306
438
|
export const rankString = (text: string) => {
|
|
@@ -308,8 +440,6 @@ export const rankString = (text: string) => {
|
|
|
308
440
|
return text;
|
|
309
441
|
};
|
|
310
442
|
|
|
311
|
-
export const getRankedStringCount = () => stringOrder.size;
|
|
312
|
-
|
|
313
443
|
export const getStringRank = (text: string) => stringOrder.ranks[rankString(text)];
|
|
314
444
|
|
|
315
445
|
const compareStrings = (left: string, right: string) => {
|
|
@@ -317,6 +447,14 @@ const compareStrings = (left: string, right: string) => {
|
|
|
317
447
|
const ranks = stringOrder.ranks;
|
|
318
448
|
const leftRank = ranks[left];
|
|
319
449
|
const rightRank = ranks[right];
|
|
450
|
+
const searchRank = stringOrder.searchRank;
|
|
451
|
+
if (
|
|
452
|
+
searchRank !== undefined &&
|
|
453
|
+
(leftRank === undefined || leftRank >= searchRank) &&
|
|
454
|
+
(rightRank === undefined || rightRank >= searchRank)
|
|
455
|
+
) {
|
|
456
|
+
stringOrder.unorderedString ??= left;
|
|
457
|
+
}
|
|
320
458
|
if (leftRank !== undefined && rightRank !== undefined) return leftRank - rightRank;
|
|
321
459
|
if (leftRank !== undefined) return -1;
|
|
322
460
|
if (rightRank !== undefined) return 1;
|
|
@@ -335,9 +473,15 @@ const isOverriddenComparison = (left: Value, right: Value) =>
|
|
|
335
473
|
right.kind === "SequenceSet"
|
|
336
474
|
);
|
|
337
475
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
476
|
+
const createComparisonError = (left: Value, right: Value) =>
|
|
477
|
+
new TlaEvaluationError(
|
|
478
|
+
isOverriddenSet(left)
|
|
479
|
+
? `Attempted to compare overridden value ${formatValue(left, true)} with non-overridden value:\n${formatValue(right)}`
|
|
480
|
+
: `Attempted to compare ${describeKind(left)} ${formatValue(left)} with ${describeKind(right)} ${formatValue(right)}`,
|
|
481
|
+
);
|
|
482
|
+
|
|
483
|
+
// TLC's Nat, Int, STRING, Seq(S), and Any compare only with each other and with model values,
|
|
484
|
+
// and fail with any other set, worded by the class of the left operand.
|
|
341
485
|
const compareOverriddenSets = (
|
|
342
486
|
left: Value,
|
|
343
487
|
right: Value,
|
|
@@ -348,18 +492,14 @@ const compareOverriddenSets = (
|
|
|
348
492
|
`Attempted to ${verb} the set ${formatValue(isEnumerable(left) ? toFiniteSet(left) : left, true)} with the value:\n${formatValue(right, true)}`,
|
|
349
493
|
);
|
|
350
494
|
}
|
|
351
|
-
if (!isOverriddenSet(right))
|
|
352
|
-
throw new TlaEvaluationError(
|
|
353
|
-
`Attempted to compare overridden value ${formatValue(left, true)} with non-overridden value:\n${formatValue(right, true)}`,
|
|
354
|
-
);
|
|
355
|
-
}
|
|
495
|
+
if (!isOverriddenSet(right)) throw createComparisonError(left, right);
|
|
356
496
|
const leftName = left.kind === "InfiniteSet" ? left.name : undefined;
|
|
357
497
|
const rightName = right.kind === "InfiniteSet" ? right.name : undefined;
|
|
358
498
|
if (leftName !== undefined && leftName === rightName) return 0;
|
|
359
499
|
if ((leftName === "Nat" && rightName === "Int") || (leftName === "Int" && rightName === "Nat")) {
|
|
360
500
|
return leftName === "Nat" ? -1 : 1;
|
|
361
501
|
}
|
|
362
|
-
throw new
|
|
502
|
+
throw new TlaModuleError(
|
|
363
503
|
`Attempted to compare ${formatValue(left, true)} with the value\n${formatValue(right, true)}`,
|
|
364
504
|
);
|
|
365
505
|
};
|
|
@@ -367,13 +507,20 @@ const compareOverriddenSets = (
|
|
|
367
507
|
export const ANY_SET: InfiniteSetValue = { kind: "InfiniteSet", name: "Any" };
|
|
368
508
|
|
|
369
509
|
const compareAny = (other: Value): never => {
|
|
370
|
-
throw new
|
|
510
|
+
throw new TlaModuleError(`Attempted to compare ANY with the value\n${formatValue(other)}`);
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
// TLC enumerates a lambda to compare it, even with itself.
|
|
514
|
+
const compareWithItself = (value: Value) => {
|
|
515
|
+
if (writtenOrderUse.isUsed) forgetWrittenOrder(value);
|
|
516
|
+
if (value === ANY_SET) compareAny(value);
|
|
517
|
+
if (typeof value === "object" && value.kind === "LazyFunction") toFiniteFunction(value);
|
|
371
518
|
};
|
|
372
519
|
|
|
373
520
|
export const compareValues = (left: Value, right: Value): number => {
|
|
374
521
|
if (left === right) {
|
|
375
|
-
|
|
376
|
-
return
|
|
522
|
+
compareWithItself(left);
|
|
523
|
+
return 0;
|
|
377
524
|
}
|
|
378
525
|
if (typeof left === "object" && typeof right === "object") {
|
|
379
526
|
const leftKind = left.kind;
|
|
@@ -401,9 +548,16 @@ export const compareValues = (left: Value, right: Value): number => {
|
|
|
401
548
|
}
|
|
402
549
|
const isLeftSet = isSetValue(left);
|
|
403
550
|
if (isLeftSet !== isSetValue(right)) {
|
|
404
|
-
throw
|
|
405
|
-
|
|
406
|
-
|
|
551
|
+
throw createComparisonError(left, right);
|
|
552
|
+
}
|
|
553
|
+
// The sizes decide first, so an interval of another size never needs its elements.
|
|
554
|
+
if (leftKind === "Interval" && rightKind === "Set") {
|
|
555
|
+
const difference = getCardinality(left) - right.elements.length;
|
|
556
|
+
if (difference !== 0) return difference;
|
|
557
|
+
}
|
|
558
|
+
if (leftKind === "Set" && rightKind === "Interval") {
|
|
559
|
+
const difference = left.elements.length - getCardinality(right);
|
|
560
|
+
if (difference !== 0) return difference;
|
|
407
561
|
}
|
|
408
562
|
if (isLeftSet) return compareSets(toFiniteSet(left), toFiniteSet(right));
|
|
409
563
|
return compareFunctions(toFiniteFunction(left), toFiniteFunction(right));
|
|
@@ -414,35 +568,53 @@ export const compareValues = (left: Value, right: Value): number => {
|
|
|
414
568
|
checkModelValueTypes(left, right, false);
|
|
415
569
|
if (isModelValue(left)) return -1;
|
|
416
570
|
if (isModelValue(right)) return 1;
|
|
417
|
-
throw
|
|
418
|
-
`Attempted to compare ${describeKind(left)} ${formatValue(left)} with ${describeKind(right)} ${formatValue(right)}`,
|
|
419
|
-
);
|
|
571
|
+
throw createComparisonError(left, right);
|
|
420
572
|
};
|
|
421
573
|
|
|
422
574
|
const modelValues = new Map<string, ModelValue>();
|
|
423
575
|
|
|
576
|
+
// ModelValue's index is a short.
|
|
577
|
+
const MAXIMUM_MODEL_VALUES = 32768;
|
|
578
|
+
|
|
424
579
|
// setStringOrder clears the model values, so each check ranks and creates its own.
|
|
425
580
|
export const createModelValue = (name: string): ModelValue => {
|
|
426
581
|
const known = modelValues.get(name);
|
|
427
582
|
if (known) return known;
|
|
583
|
+
if (modelValues.size === MAXIMUM_MODEL_VALUES) {
|
|
584
|
+
throw new TlaFeatureUnsupportedError(
|
|
585
|
+
`TLC supports at most ${MAXIMUM_MODEL_VALUES} model values, but model value ${name} exceeds this limit.`,
|
|
586
|
+
);
|
|
587
|
+
}
|
|
428
588
|
const value: ModelValue = {
|
|
429
589
|
kind: "ModelValue",
|
|
430
590
|
name,
|
|
431
591
|
type: name.length > 2 && name[1] === "_" ? name[0] : undefined,
|
|
432
592
|
rank: getStringRank(name),
|
|
593
|
+
hash: finalizeHash(combineHash(hashString(name), 0x6d)),
|
|
594
|
+
fingerprint: finalizeFingerprint(fingerprintString(name) ^ 0x3c),
|
|
433
595
|
};
|
|
434
596
|
modelValues.set(name, value);
|
|
435
597
|
return value;
|
|
436
598
|
};
|
|
437
599
|
|
|
600
|
+
export const getModelValues = () => [...modelValues.values()];
|
|
601
|
+
|
|
438
602
|
export const createSortedSet = (elements: Value[]): SetValue => ({
|
|
439
603
|
kind: "Set",
|
|
440
604
|
elements,
|
|
605
|
+
writtenElements: undefined,
|
|
606
|
+
isNormalized: false,
|
|
441
607
|
hash: 0,
|
|
442
608
|
fingerprint: 0,
|
|
443
609
|
isFingerprinted: false,
|
|
444
610
|
});
|
|
445
611
|
|
|
612
|
+
export const createNormalizedSet = (elements: Value[]): SetValue => {
|
|
613
|
+
const set = createSortedSet(elements);
|
|
614
|
+
set.isNormalized = true;
|
|
615
|
+
return set;
|
|
616
|
+
};
|
|
617
|
+
|
|
446
618
|
export const createSortedFunction = (domain: Value[], range: Value[]): FunctionValue => ({
|
|
447
619
|
kind: "Function",
|
|
448
620
|
domain,
|
|
@@ -451,12 +623,12 @@ export const createSortedFunction = (domain: Value[], range: Value[]): FunctionV
|
|
|
451
623
|
fingerprint: 0,
|
|
452
624
|
writtenKeys: undefined,
|
|
453
625
|
functionClass: undefined,
|
|
626
|
+
lambdaDomain: undefined,
|
|
454
627
|
isFingerprinted: false,
|
|
455
628
|
});
|
|
456
629
|
|
|
457
|
-
// TLC words a failed
|
|
458
|
-
//
|
|
459
|
-
// [a |-> e], and FcnRcdValue for any other function.
|
|
630
|
+
// TLC words a failed application by the function's Java class: FcnLambdaValue, TupleValue,
|
|
631
|
+
// RecordValue, or FcnRcdValue.
|
|
460
632
|
export type FunctionClass = "lambda" | "tuple" | "record";
|
|
461
633
|
|
|
462
634
|
// Whether a lambda exists for markFingerprinted to find, so that a spec without one skips the walk.
|
|
@@ -480,15 +652,11 @@ export const markLambdaFunction = <FunctionLike extends FunctionValue | LazyFunc
|
|
|
480
652
|
functionValue: FunctionLike,
|
|
481
653
|
) => setFunctionClass(functionValue, "lambda");
|
|
482
654
|
|
|
483
|
-
|
|
655
|
+
// TLC caches a lazy set as the receiver of compareTo, equals, size, and fingerPrint, not as
|
|
656
|
+
// their argument, and enumerates the sets an uncached one combines, duplicates and all.
|
|
657
|
+
const isCachedSet = (value: ObjectValue) => isLazySet(value) && value.isCached === true;
|
|
484
658
|
|
|
485
|
-
//
|
|
486
|
-
// equals, size, and fingerPrint, not as their argument, and enumerates the sets an uncached one
|
|
487
|
-
// combines, duplicates and all.
|
|
488
|
-
const cachedSets = new WeakSet<ObjectValue>();
|
|
489
|
-
|
|
490
|
-
// Whether the spec has built a UNION or \cup set, the sets that stream duplicates, so that a spec
|
|
491
|
-
// without one skips tracking what TLC caches.
|
|
659
|
+
// Lets a spec without a UNION or \cup set, which stream duplicates, skip tracking TLC's caches.
|
|
492
660
|
const streamedSetPresence = { isPresent: false };
|
|
493
661
|
|
|
494
662
|
export const markStreamedSet = <Streamed extends UnionValue | SetOperationValue>(
|
|
@@ -498,7 +666,6 @@ export const markStreamedSet = <Streamed extends UnionValue | SetOperationValue>
|
|
|
498
666
|
return value;
|
|
499
667
|
};
|
|
500
668
|
|
|
501
|
-
// The sets that stream their elements apart from the normalized set until TLC caches them.
|
|
502
669
|
const STREAMING_KINDS = new Set<ObjectValue["kind"]>([
|
|
503
670
|
"Union",
|
|
504
671
|
"SetOperation",
|
|
@@ -512,25 +679,23 @@ const cacheSet = (value: Value) => {
|
|
|
512
679
|
if (
|
|
513
680
|
streamedSetPresence.isPresent &&
|
|
514
681
|
typeof value === "object" &&
|
|
682
|
+
isLazySet(value) &&
|
|
515
683
|
STREAMING_KINDS.has(value.kind)
|
|
516
684
|
) {
|
|
517
|
-
|
|
685
|
+
value.isCached = true;
|
|
518
686
|
}
|
|
519
687
|
};
|
|
520
688
|
|
|
521
|
-
//
|
|
522
|
-
// from then on, so a function TLC has fingerprinted is no longer a lambda.
|
|
689
|
+
// Fingerprinting caches the FcnRcdValue a lambda enumerates to, so it is no longer a lambda.
|
|
523
690
|
export const markFingerprinted = (value: Value): void => {
|
|
524
691
|
if ((!lambdaPresence.isPresent && !streamedSetPresence.isPresent) || typeof value !== "object") {
|
|
525
692
|
return;
|
|
526
693
|
}
|
|
527
|
-
if (value.kind === "
|
|
528
|
-
|
|
529
|
-
value.isFingerprinted = true;
|
|
530
|
-
} else {
|
|
531
|
-
if (fingerprintedValues.has(value)) return;
|
|
532
|
-
fingerprintedValues.add(value);
|
|
694
|
+
if (value.kind === "ModelValue" || value.kind === "Interval" || value.kind === "InfiniteSet") {
|
|
695
|
+
return;
|
|
533
696
|
}
|
|
697
|
+
if (value.isFingerprinted) return;
|
|
698
|
+
value.isFingerprinted = true;
|
|
534
699
|
if (value.kind === "Function") {
|
|
535
700
|
if (value.functionClass === "lambda") value.functionClass = undefined;
|
|
536
701
|
value.domain.forEach(markFingerprinted);
|
|
@@ -546,8 +711,8 @@ export const markFingerprinted = (value: Value): void => {
|
|
|
546
711
|
export const valuesEqual = (left: Value, right: Value): boolean => {
|
|
547
712
|
cacheSet(left);
|
|
548
713
|
if (left === right) {
|
|
549
|
-
|
|
550
|
-
return
|
|
714
|
+
compareWithItself(left);
|
|
715
|
+
return true;
|
|
551
716
|
}
|
|
552
717
|
if (typeof left !== "object" || typeof right !== "object") {
|
|
553
718
|
if (typeof left === typeof right) return false;
|
|
@@ -556,16 +721,19 @@ export const valuesEqual = (left: Value, right: Value): boolean => {
|
|
|
556
721
|
}
|
|
557
722
|
return compareValues(left, right) === 0;
|
|
558
723
|
}
|
|
559
|
-
const isHashed =
|
|
560
|
-
(left.kind === "Set" || left.kind === "Function") &&
|
|
561
|
-
(right.kind === "Set" || right.kind === "Function") &&
|
|
562
|
-
left.hash !== 0 &&
|
|
563
|
-
right.hash !== 0;
|
|
564
|
-
if (isHashed && left.hash !== right.hash) return false;
|
|
565
724
|
if (left.kind === "Function" && right.kind === "Function")
|
|
566
725
|
return compareFunctions(left, right) === 0;
|
|
567
726
|
if (left.kind === "Set" && right.kind === "Set") return compareSets(left, right) === 0;
|
|
727
|
+
// The sizes decide first, so an interval of another size never needs its elements.
|
|
728
|
+
if (left.kind === "Interval" && right.kind === "Set") {
|
|
729
|
+
if (getCardinality(left) !== right.elements.length) return false;
|
|
730
|
+
}
|
|
731
|
+
if (left.kind === "Set" && right.kind === "Interval") {
|
|
732
|
+
if (left.elements.length !== getCardinality(right)) return false;
|
|
733
|
+
}
|
|
568
734
|
if (isModelValue(left) || isModelValue(right)) {
|
|
735
|
+
// TLC enumerates a lambda, which can fail, before it compares it.
|
|
736
|
+
if (left.kind === "LazyFunction") toFiniteFunction(left);
|
|
569
737
|
checkModelValueTypes(left, right, true);
|
|
570
738
|
return isModelValue(left) && isModelValue(right) && left.name === right.name;
|
|
571
739
|
}
|
|
@@ -618,8 +786,7 @@ export const valuesEqual = (left: Value, right: Value): boolean => {
|
|
|
618
786
|
return compareValues(left, right) === 0;
|
|
619
787
|
};
|
|
620
788
|
|
|
621
|
-
// TLC
|
|
622
|
-
// another in a state only if each function in it has the same class.
|
|
789
|
+
// TLC words errors by each function's Java class, so a stand-in must match each function's class.
|
|
623
790
|
const createSameValueCheck = (isClassSensitive: boolean) => {
|
|
624
791
|
const areSameValues = (left: Value[], right: Value[]) => {
|
|
625
792
|
if (left === right) return true;
|
|
@@ -659,8 +826,7 @@ const canonicalizeElements = (elements: Value[], canonicalize: (value: Value) =>
|
|
|
659
826
|
}
|
|
660
827
|
};
|
|
661
828
|
|
|
662
|
-
//
|
|
663
|
-
// the states it keeps share their values.
|
|
829
|
+
// So that the states it keeps share their values.
|
|
664
830
|
export const createCanonicalizer = () => {
|
|
665
831
|
const canonicalValues = new Map<number, Value | Value[]>();
|
|
666
832
|
const canonicalize = (value: Value): Value => {
|
|
@@ -694,21 +860,17 @@ export const createCanonicalizer = () => {
|
|
|
694
860
|
};
|
|
695
861
|
};
|
|
696
862
|
|
|
697
|
-
//
|
|
698
|
-
//
|
|
699
|
-
// failed comparison runs again through them to fail as TLC does.
|
|
863
|
+
// These follow TLC's compareTo and equals, which word a failure by the left operand's class,
|
|
864
|
+
// so a failed comparison above runs again through them to fail as TLC does.
|
|
700
865
|
interface TlcFunction {
|
|
701
866
|
domain: Value[];
|
|
702
867
|
range: Value[];
|
|
703
868
|
low: number | undefined;
|
|
704
869
|
}
|
|
705
870
|
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
? first
|
|
710
|
-
: undefined;
|
|
711
|
-
};
|
|
871
|
+
// TLC compares a domain it keeps as an interval apart from one it keeps as an array.
|
|
872
|
+
const getIntervalLow = (value: Value, domain: Value[]) =>
|
|
873
|
+
domain.length === 0 || getFunctionClass(value) === "tuple" ? 1 : getDomainInterval(domain)?.low;
|
|
712
874
|
|
|
713
875
|
const toTlcFunction = (value: Value) => {
|
|
714
876
|
if (!isFunctionValue(value)) return undefined;
|
|
@@ -716,7 +878,7 @@ const toTlcFunction = (value: Value) => {
|
|
|
716
878
|
return {
|
|
717
879
|
domain: functionValue.domain,
|
|
718
880
|
range: functionValue.range,
|
|
719
|
-
low: getIntervalLow(functionValue.domain),
|
|
881
|
+
low: getIntervalLow(value, functionValue.domain),
|
|
720
882
|
};
|
|
721
883
|
};
|
|
722
884
|
|
|
@@ -809,8 +971,7 @@ const compareElementsLikeTlc = (left: Value[], right: Value[]) => {
|
|
|
809
971
|
|
|
810
972
|
const compareFunctionLikeTlc = (left: FunctionValue | LazyFunctionValue, right: Value) => {
|
|
811
973
|
const functionValue = toFiniteFunction(left);
|
|
812
|
-
|
|
813
|
-
if (isTuple) {
|
|
974
|
+
if (getFunctionClass(left) === "tuple") {
|
|
814
975
|
const tuple = isFunctionValue(right) ? getSequenceElements(right) : undefined;
|
|
815
976
|
if (!tuple) return compareTlcFunctions({ ...functionValue, low: 1 }, left, right);
|
|
816
977
|
return (
|
|
@@ -833,14 +994,13 @@ const compareFunctionLikeTlc = (left: FunctionValue | LazyFunctionValue, right:
|
|
|
833
994
|
);
|
|
834
995
|
}
|
|
835
996
|
return compareTlcFunctions(
|
|
836
|
-
{ ...functionValue, low: getIntervalLow(functionValue.domain) },
|
|
997
|
+
{ ...functionValue, low: getIntervalLow(left, functionValue.domain) },
|
|
837
998
|
left,
|
|
838
999
|
right,
|
|
839
1000
|
);
|
|
840
1001
|
};
|
|
841
1002
|
|
|
842
|
-
// TLC compares a lazy
|
|
843
|
-
// elements. The comparisons above already fail as those classes do.
|
|
1003
|
+
// TLC compares a lazy or Java-overridden set through its own class before it compares elements.
|
|
844
1004
|
const isComparedByClass = (left: ObjectValue, right: Value) =>
|
|
845
1005
|
isOverriddenSet(left) ||
|
|
846
1006
|
isOverriddenSet(right) ||
|
|
@@ -924,8 +1084,7 @@ const equalsTlcFunctions = (functionValue: TlcFunction, left: Value, right: Valu
|
|
|
924
1084
|
|
|
925
1085
|
const equalsFunctionLikeTlc = (left: FunctionValue | LazyFunctionValue, right: Value) => {
|
|
926
1086
|
const functionValue = toFiniteFunction(left);
|
|
927
|
-
|
|
928
|
-
if (isTuple) {
|
|
1087
|
+
if (getFunctionClass(left) === "tuple") {
|
|
929
1088
|
const tuple = isFunctionValue(right) ? getSequenceElements(right) : undefined;
|
|
930
1089
|
if (!tuple) return equalsTlcFunctions({ ...functionValue, low: 1 }, left, right);
|
|
931
1090
|
return (
|
|
@@ -948,7 +1107,7 @@ const equalsFunctionLikeTlc = (left: FunctionValue | LazyFunctionValue, right: V
|
|
|
948
1107
|
);
|
|
949
1108
|
}
|
|
950
1109
|
return equalsTlcFunctions(
|
|
951
|
-
{ ...functionValue, low: getIntervalLow(functionValue.domain) },
|
|
1110
|
+
{ ...functionValue, low: getIntervalLow(left, functionValue.domain) },
|
|
952
1111
|
left,
|
|
953
1112
|
right,
|
|
954
1113
|
);
|
|
@@ -991,7 +1150,6 @@ const sortLikeTlc = (elements: Value[]) => {
|
|
|
991
1150
|
return sorted;
|
|
992
1151
|
};
|
|
993
1152
|
|
|
994
|
-
// Calls a method of a value as TLC does, whose failure the value heads when it has a source.
|
|
995
1153
|
const callTracedMethod = <Result>(receiver: Value, method: () => Result) => {
|
|
996
1154
|
const sets = setTrace.sets;
|
|
997
1155
|
if (sets === undefined) return method();
|
|
@@ -1001,8 +1159,7 @@ const callTracedMethod = <Result>(receiver: Value, method: () => Result) => {
|
|
|
1001
1159
|
return result;
|
|
1002
1160
|
};
|
|
1003
1161
|
|
|
1004
|
-
//
|
|
1005
|
-
// is or would go.
|
|
1162
|
+
// A binary search with compareTo, which ends where the element is or would go.
|
|
1006
1163
|
const searchLikeTlc = (element: Value, sorted: Value[]) => {
|
|
1007
1164
|
const bounds = { low: 0, high: sorted.length };
|
|
1008
1165
|
while (bounds.low < bounds.high) {
|
|
@@ -1015,21 +1172,18 @@ const searchLikeTlc = (element: Value, sorted: Value[]) => {
|
|
|
1015
1172
|
return { index: bounds.low, isFound: false };
|
|
1016
1173
|
};
|
|
1017
1174
|
|
|
1018
|
-
// TLC checks membership in a set enumeration it hasn't sorted element by element, with the equals
|
|
1019
|
-
// method of each member.
|
|
1020
1175
|
const isElementOfLikeTlc = (element: Value, elements: Value[]) =>
|
|
1021
1176
|
elements.some((member) => callTracedMethod(member, () => equalsLikeTlc(member, element)));
|
|
1022
1177
|
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1178
|
+
export const getWrittenElements = (set: SetValue | MixedSetValue) =>
|
|
1179
|
+
set.kind === "MixedSet" || set.isNormalized
|
|
1180
|
+
? set.elements
|
|
1181
|
+
: (set.writtenElements ?? set.elements);
|
|
1026
1182
|
|
|
1027
|
-
// IValue.deepNormalize, which TLC applies to the constants of the config and the constant
|
|
1028
|
-
// definitions it computes as it starts.
|
|
1029
1183
|
export const markNormalized = (value: Value): void => {
|
|
1030
1184
|
if (typeof value !== "object") return;
|
|
1031
1185
|
if (value.kind === "Set") {
|
|
1032
|
-
|
|
1186
|
+
value.isNormalized = true;
|
|
1033
1187
|
value.elements.forEach(markNormalized);
|
|
1034
1188
|
}
|
|
1035
1189
|
if (value.kind === "Function") {
|
|
@@ -1038,6 +1192,33 @@ export const markNormalized = (value: Value): void => {
|
|
|
1038
1192
|
}
|
|
1039
1193
|
};
|
|
1040
1194
|
|
|
1195
|
+
// TLC's Value.normalize sorts a set enumeration, and the sets a lazy set it hasn't cached combines.
|
|
1196
|
+
export const normalizeShallowly = (value: Value): void => {
|
|
1197
|
+
if (typeof value !== "object" || isCachedSet(value)) return;
|
|
1198
|
+
switch (value.kind) {
|
|
1199
|
+
case "Set":
|
|
1200
|
+
value.isNormalized = true;
|
|
1201
|
+
return;
|
|
1202
|
+
case "MixedSet":
|
|
1203
|
+
toFiniteSet(value);
|
|
1204
|
+
return;
|
|
1205
|
+
case "FilteredSet":
|
|
1206
|
+
case "Subset":
|
|
1207
|
+
return normalizeShallowly(value.base);
|
|
1208
|
+
case "SetOperation":
|
|
1209
|
+
if (value.operator === "\\cup") return;
|
|
1210
|
+
normalizeShallowly(value.left);
|
|
1211
|
+
return normalizeShallowly(value.right);
|
|
1212
|
+
case "FunctionSet":
|
|
1213
|
+
normalizeShallowly(value.domain);
|
|
1214
|
+
return normalizeShallowly(value.range);
|
|
1215
|
+
case "RecordSet":
|
|
1216
|
+
return value.sets.forEach(normalizeShallowly);
|
|
1217
|
+
case "ProductSet":
|
|
1218
|
+
return value.factors.forEach(normalizeShallowly);
|
|
1219
|
+
}
|
|
1220
|
+
};
|
|
1221
|
+
|
|
1041
1222
|
export const isEqualLikeTlc = (left: Value, right: Value) =>
|
|
1042
1223
|
computeLikeTlcOnFailure(valuesEqual, equalsLikeTlc, left, right);
|
|
1043
1224
|
|
|
@@ -1048,8 +1229,19 @@ const isStrictlySorted = (elements: Value[]) => {
|
|
|
1048
1229
|
return true;
|
|
1049
1230
|
};
|
|
1050
1231
|
|
|
1232
|
+
const isAscendingIntegers = (elements: Value[]) => {
|
|
1233
|
+
for (let index = 0; index < elements.length; index++) {
|
|
1234
|
+
const element = elements[index];
|
|
1235
|
+
const previous = index > 0 ? elements[index - 1] : undefined;
|
|
1236
|
+
if (typeof element !== "number" || (typeof previous === "number" && previous >= element)) {
|
|
1237
|
+
return false;
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
return true;
|
|
1241
|
+
};
|
|
1242
|
+
|
|
1051
1243
|
export const createSet = (elements: Value[]): SetValue => {
|
|
1052
|
-
//
|
|
1244
|
+
// TLC's sort compares each element after the first with those before it.
|
|
1053
1245
|
for (let index = 1; index < elements.length; index++) cacheSet(elements[index]);
|
|
1054
1246
|
if (elements.length < 2 || isStrictlySorted(elements)) return createSortedSet(elements);
|
|
1055
1247
|
const sorted = [...elements].sort(compareValues);
|
|
@@ -1060,15 +1252,18 @@ export const createSet = (elements: Value[]): SetValue => {
|
|
|
1060
1252
|
return createSortedSet(unique);
|
|
1061
1253
|
};
|
|
1062
1254
|
|
|
1063
|
-
// TLC builds a set enumeration
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
computeLikeTlcOnFailure<Value[], undefined, SetValue | MixedSetValue>(
|
|
1255
|
+
// TLC builds a set enumeration unsorted, so one whose elements it can't compare still evaluates.
|
|
1256
|
+
export const createEnumeratedSet = (elements: Value[]): SetValue | MixedSetValue => {
|
|
1257
|
+
if (isAscendingIntegers(elements)) return createSortedSet(elements);
|
|
1258
|
+
const set = computeLikeTlcOnFailure<Value[], undefined, SetValue | MixedSetValue>(
|
|
1067
1259
|
createSet,
|
|
1068
1260
|
(mixed) => ({ kind: "MixedSet", elements: mixed }),
|
|
1069
1261
|
elements,
|
|
1070
1262
|
undefined,
|
|
1071
1263
|
);
|
|
1264
|
+
if (set.kind === "Set" && set.elements !== elements) set.writtenElements = elements;
|
|
1265
|
+
return set;
|
|
1266
|
+
};
|
|
1072
1267
|
|
|
1073
1268
|
export const EMPTY_SET = createSortedSet([]);
|
|
1074
1269
|
|
|
@@ -1184,7 +1379,6 @@ export const createWrittenFunction = (pairs: [Value, Value][]) => {
|
|
|
1184
1379
|
const functionValue = createFunction(pairs);
|
|
1185
1380
|
const isWrittenSorted =
|
|
1186
1381
|
functionValue.domain.length !== pairs.length ||
|
|
1187
|
-
pairs.some((pair) => typeof pair[0] !== "string") ||
|
|
1188
1382
|
pairs.every((pair, index) => pair[0] === functionValue.domain[index]);
|
|
1189
1383
|
return isWrittenSorted
|
|
1190
1384
|
? functionValue
|
|
@@ -1194,23 +1388,26 @@ export const createWrittenFunction = (pairs: [Value, Value][]) => {
|
|
|
1194
1388
|
);
|
|
1195
1389
|
};
|
|
1196
1390
|
|
|
1391
|
+
const MAX_SHARED_INTERVAL_LENGTH = 1024;
|
|
1197
1392
|
const intervalDomains: Value[][] = [];
|
|
1393
|
+
const longIntervalDomains = new Map<number, WeakRef<Value[]>>();
|
|
1198
1394
|
const intervalValues: IntervalValue[] = [];
|
|
1199
1395
|
const intervalArrays = new WeakMap<Value[], IntervalValue>();
|
|
1200
1396
|
|
|
1201
|
-
//
|
|
1202
|
-
//
|
|
1203
|
-
//
|
|
1397
|
+
// Every function over 1..n shares one domain array per length, so the array's identity records
|
|
1398
|
+
// the interval. Callers must not mutate the returned array. Longer domains are held weakly: a
|
|
1399
|
+
// sequence that grows one element at a time would otherwise keep an array of every length it
|
|
1400
|
+
// passed through, n * n / 2 numbers in all.
|
|
1204
1401
|
const getIntervalDomain = (length: number): Value[] => {
|
|
1205
|
-
const cached = intervalDomains[length];
|
|
1402
|
+
const cached = intervalDomains[length] ?? longIntervalDomains.get(length)?.deref();
|
|
1206
1403
|
if (cached) return cached;
|
|
1207
1404
|
const domain = Array.from({ length }, (_, index) => index + 1);
|
|
1208
|
-
|
|
1405
|
+
if (length > MAX_SHARED_INTERVAL_LENGTH) longIntervalDomains.set(length, new WeakRef(domain));
|
|
1406
|
+
else intervalDomains[length] = domain;
|
|
1209
1407
|
return domain;
|
|
1210
1408
|
};
|
|
1211
1409
|
|
|
1212
|
-
// The
|
|
1213
|
-
// the interval for DOMAIN.
|
|
1410
|
+
// The caller only iterates the elements, so they need not record the interval for DOMAIN.
|
|
1214
1411
|
export const getIntervalElements = (value: IntervalValue): Value[] => {
|
|
1215
1412
|
getCardinality(value);
|
|
1216
1413
|
if (value.low === 1 && value.high >= 1) return getIntervalDomain(value.high);
|
|
@@ -1220,10 +1417,12 @@ export const getIntervalElements = (value: IntervalValue): Value[] => {
|
|
|
1220
1417
|
};
|
|
1221
1418
|
|
|
1222
1419
|
const isIntervalDomain = (domain: Value[]) =>
|
|
1223
|
-
domain.length > 0 &&
|
|
1420
|
+
(domain.length > 0 &&
|
|
1421
|
+
(domain === intervalDomains[domain.length] ||
|
|
1422
|
+
domain === longIntervalDomains.get(domain.length)?.deref())) ||
|
|
1423
|
+
intervalArrays.has(domain);
|
|
1224
1424
|
|
|
1225
|
-
//
|
|
1226
|
-
// such as (1..2) \cap S, whose elements are the shared array, gets a copy.
|
|
1425
|
+
// A set such as (1..2) \cap S, whose elements are the shared array, gets a copy.
|
|
1227
1426
|
export const getFunctionDomain = (set: Value): Value[] => {
|
|
1228
1427
|
const elements = toFiniteSet(set).elements;
|
|
1229
1428
|
return isIntervalDomain(elements) && !(typeof set === "object" && set.kind === "Interval")
|
|
@@ -1248,7 +1447,6 @@ export const createRecord = (fields: [string, Value][]): FunctionValue =>
|
|
|
1248
1447
|
export const createSortedRecord = (names: Value[], range: Value[]) =>
|
|
1249
1448
|
setFunctionClass(createSortedFunction(names, range), "record");
|
|
1250
1449
|
|
|
1251
|
-
// TLC's Enumerable classes: every set except the ones it implements in Java.
|
|
1252
1450
|
export const isTlcEnumerable = (value: Value) => isSetValue(value) && !isOverriddenSet(value);
|
|
1253
1451
|
|
|
1254
1452
|
const formatUnion = (value: UnionValue, isStrict = false) =>
|
|
@@ -1299,7 +1497,13 @@ export const isEnumerable = (value: Value): boolean => {
|
|
|
1299
1497
|
case "ProductSet":
|
|
1300
1498
|
return value.factors.every(isEnumerable) || getEmptiness(value) === true;
|
|
1301
1499
|
case "Union":
|
|
1302
|
-
return
|
|
1500
|
+
return (
|
|
1501
|
+
isEnumerable(value.base) &&
|
|
1502
|
+
(isEnumeratedSet(value.base)
|
|
1503
|
+
? getWrittenElements(value.base)
|
|
1504
|
+
: getStreamedElements(value.base)
|
|
1505
|
+
).every(isEnumerable)
|
|
1506
|
+
);
|
|
1303
1507
|
case "SetOperation":
|
|
1304
1508
|
if (value.operator === "\\cup") return isEnumerable(value.left) && isEnumerable(value.right);
|
|
1305
1509
|
if (value.operator === "\\cap") return isEnumerable(value.left) || isEnumerable(value.right);
|
|
@@ -1372,15 +1576,14 @@ const advanceCombination = (indices: number[], length: number) => {
|
|
|
1372
1576
|
return false;
|
|
1373
1577
|
};
|
|
1374
1578
|
|
|
1375
|
-
// TLC enumerates SUBSET S lazily, by size and then in the order of S, so a quantifier that
|
|
1376
|
-
// its witness early never builds the rest.
|
|
1377
|
-
// computed in that order as a loop reads them.
|
|
1579
|
+
// TLC enumerates SUBSET S lazily, by size and then in the order of S, so a quantifier that
|
|
1580
|
+
// finds its witness early never builds the rest.
|
|
1378
1581
|
export const getLazySubsets = (value: Value): Value[] | undefined => {
|
|
1379
1582
|
if (typeof value !== "object" || value.kind !== "Subset" || isOverriddenSet(value.base)) {
|
|
1380
1583
|
return undefined;
|
|
1381
1584
|
}
|
|
1382
1585
|
const base = toFiniteSet(value.base).elements;
|
|
1383
|
-
if (base.length <=
|
|
1586
|
+
if (base.length <= MAXIMUM_BUILT_SUBSET_BASE) return undefined;
|
|
1384
1587
|
const cursor = { index: -1, indices: [0] };
|
|
1385
1588
|
const getSubset = (index: number) => {
|
|
1386
1589
|
if (index <= cursor.index) cursor.index = -1;
|
|
@@ -1418,9 +1621,8 @@ const getSortedSubsets = (base: Value[]) => {
|
|
|
1418
1621
|
return subsets;
|
|
1419
1622
|
};
|
|
1420
1623
|
|
|
1421
|
-
//
|
|
1422
|
-
//
|
|
1423
|
-
// before they enumerate it.
|
|
1624
|
+
// TLC enumerates the sets in turn and stops at the first empty one. Later versions decide
|
|
1625
|
+
// that the set is empty before they enumerate it.
|
|
1424
1626
|
const getComponentElements = (
|
|
1425
1627
|
sets: Value[],
|
|
1426
1628
|
getElements: (set: Value) => Value[],
|
|
@@ -1437,7 +1639,6 @@ const getComponentElements = (
|
|
|
1437
1639
|
return lists;
|
|
1438
1640
|
};
|
|
1439
1641
|
|
|
1440
|
-
// getElements enumerates the sets a set of functions, records, or tuples combines.
|
|
1441
1642
|
const enumerateSet = (
|
|
1442
1643
|
value: Value,
|
|
1443
1644
|
getElements = (set: Value) => toFiniteSet(set).elements,
|
|
@@ -1455,8 +1656,7 @@ const enumerateSet = (
|
|
|
1455
1656
|
return elements;
|
|
1456
1657
|
}
|
|
1457
1658
|
case "Subset": {
|
|
1458
|
-
//
|
|
1459
|
-
// set of a value of size 0, a function with an empty domain among them.
|
|
1659
|
+
// TLC sizes the set before it enumerates it, and enumerates a set of size 0 as {{}}.
|
|
1460
1660
|
if (getCardinality(value.base) === 0) return getSortedSubsets([]);
|
|
1461
1661
|
if (!isSetValue(value.base)) {
|
|
1462
1662
|
throw new TlaEvaluationError(
|
|
@@ -1516,22 +1716,21 @@ const enumerateSet = (
|
|
|
1516
1716
|
`Attempted to enumerate { x \\in S : p(x) } when S:\n${formatValue(value.base)}\nis not enumerable`,
|
|
1517
1717
|
);
|
|
1518
1718
|
}
|
|
1519
|
-
return toFiniteSet(
|
|
1719
|
+
return toFiniteSet(
|
|
1720
|
+
createEnumeratedSet(
|
|
1721
|
+
getStreamedElements(value.base).filter((element) => value.test(element, false)),
|
|
1722
|
+
),
|
|
1723
|
+
).elements;
|
|
1520
1724
|
case "Union": {
|
|
1521
|
-
const elements =
|
|
1522
|
-
|
|
1523
|
-
|
|
1725
|
+
const elements: Value[] = [];
|
|
1726
|
+
getUnionSets(value).forEach((set, index) => {
|
|
1727
|
+
const unionSet = getUnionSet(set, index, value);
|
|
1728
|
+
if (isBeyondSetBound(unionSet)) throw new TlaSetBoundError(getSetBoundMessage());
|
|
1729
|
+
getStreamedElements(unionSet).forEach((element) => addBoundedElement(elements, element));
|
|
1730
|
+
});
|
|
1524
1731
|
return toFiniteSet(createEnumeratedSet(elements)).elements;
|
|
1525
1732
|
}
|
|
1526
1733
|
case "SetOperation": {
|
|
1527
|
-
if (
|
|
1528
|
-
value.operator === "\\cup" &&
|
|
1529
|
-
(!isTlcEnumerable(value.left) || !isTlcEnumerable(value.right))
|
|
1530
|
-
) {
|
|
1531
|
-
throw new TlaEvaluationError(
|
|
1532
|
-
`Attempted to enumerate S \\cup T when S:\n${formatValue(value.left)}\nand T:\n${formatValue(value.right)}\nare not both enumerable`,
|
|
1533
|
-
);
|
|
1534
|
-
}
|
|
1535
1734
|
if (
|
|
1536
1735
|
value.operator === "\\cap" &&
|
|
1537
1736
|
!isTlcEnumerable(value.left) &&
|
|
@@ -1546,14 +1745,24 @@ const enumerateSet = (
|
|
|
1546
1745
|
`Attempted to enumerate S \\ T when S:\n${formatValue(value.left)}\nis not enumerable.`,
|
|
1547
1746
|
);
|
|
1548
1747
|
}
|
|
1549
|
-
if (value.operator === "\\cup")
|
|
1550
|
-
return
|
|
1748
|
+
if (value.operator === "\\cup") {
|
|
1749
|
+
return toFiniteSet(createEnumeratedSet(listUnionLikeTlc(value))).elements;
|
|
1750
|
+
}
|
|
1551
1751
|
const isLeftEnumerated = value.operator === "\\" || isTlcEnumerable(value.left);
|
|
1552
1752
|
const enumerated = isLeftEnumerated ? value.left : value.right;
|
|
1553
1753
|
const other = isLeftEnumerated ? value.right : value.left;
|
|
1554
|
-
|
|
1555
|
-
|
|
1754
|
+
const kept: Value[] = [];
|
|
1755
|
+
forEachElement(
|
|
1756
|
+
typeof enumerated === "object" && enumerated.kind === "Interval"
|
|
1757
|
+
? enumerated
|
|
1758
|
+
: getStreamedElements(enumerated),
|
|
1759
|
+
(element) => {
|
|
1760
|
+
if (isElementOf(element, other) === (value.operator === "\\cap")) {
|
|
1761
|
+
addBoundedElement(kept, element);
|
|
1762
|
+
}
|
|
1763
|
+
},
|
|
1556
1764
|
);
|
|
1765
|
+
return toFiniteSet(createEnumeratedSet(kept)).elements;
|
|
1557
1766
|
}
|
|
1558
1767
|
case "InfiniteSet":
|
|
1559
1768
|
case "SequenceSet":
|
|
@@ -1565,15 +1774,13 @@ const enumerateSet = (
|
|
|
1565
1774
|
}
|
|
1566
1775
|
};
|
|
1567
1776
|
|
|
1568
|
-
const finiteSets = new WeakMap<ObjectValue, SetValue>();
|
|
1569
|
-
|
|
1570
1777
|
export const toFiniteSet = (value: Value): SetValue => {
|
|
1571
1778
|
if (typeof value === "object" && value.kind === "Set") return value;
|
|
1572
1779
|
if (typeof value === "object" && value.kind === "Interval") {
|
|
1573
1780
|
return createSortedSet(enumerateSet(value));
|
|
1574
1781
|
}
|
|
1575
|
-
const
|
|
1576
|
-
if (
|
|
1782
|
+
const lazy = typeof value === "object" && isLazySet(value) ? value : undefined;
|
|
1783
|
+
if (lazy?.enumerated) return lazy.enumerated;
|
|
1577
1784
|
const sets = setTrace.sets;
|
|
1578
1785
|
sets?.push(value);
|
|
1579
1786
|
const elements = enumerateSet(value);
|
|
@@ -1588,18 +1795,103 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1588
1795
|
value.kind === "FunctionSet" ||
|
|
1589
1796
|
value.kind === "Subset");
|
|
1590
1797
|
const set = isSorted ? createSortedSet(elements) : createSet(elements);
|
|
1591
|
-
if (
|
|
1798
|
+
if (lazy) lazy.enumerated = set;
|
|
1592
1799
|
return set;
|
|
1593
1800
|
};
|
|
1594
1801
|
|
|
1595
|
-
// TLC's
|
|
1596
|
-
//
|
|
1597
|
-
|
|
1598
|
-
if (typeof value !== "object" ||
|
|
1802
|
+
// TLC's enumerator of a set sorts the set enumerations it streams, and fails on a set it can't
|
|
1803
|
+
// enumerate, before it yields an element, so S \cup T fails on T before it streams S.
|
|
1804
|
+
const openStream = (value: Value): void => {
|
|
1805
|
+
if (typeof value !== "object" || isCachedSet(value)) return;
|
|
1806
|
+
switch (value.kind) {
|
|
1807
|
+
case "MixedSet":
|
|
1808
|
+
toFiniteSet(value);
|
|
1809
|
+
return;
|
|
1810
|
+
case "FilteredSet":
|
|
1811
|
+
if (!isTlcEnumerable(value.base)) toFiniteSet(value);
|
|
1812
|
+
return openStream(value.base);
|
|
1813
|
+
case "Union":
|
|
1814
|
+
if (!isEnumeratedSet(value.base) && isTlcEnumerable(value.base)) {
|
|
1815
|
+
return openStream(value.base);
|
|
1816
|
+
}
|
|
1817
|
+
getUnionSets(value)
|
|
1818
|
+
.slice(0, 1)
|
|
1819
|
+
.forEach((set) => openStream(getUnionSet(set, 0, value)));
|
|
1820
|
+
return;
|
|
1821
|
+
case "SetOperation":
|
|
1822
|
+
if (value.operator === "\\" && !isTlcEnumerable(value.left)) toFiniteSet(value);
|
|
1823
|
+
if (
|
|
1824
|
+
value.operator === "\\cup" &&
|
|
1825
|
+
(!isTlcEnumerable(value.left) || !isTlcEnumerable(value.right))
|
|
1826
|
+
) {
|
|
1827
|
+
toFiniteSet(value);
|
|
1828
|
+
}
|
|
1829
|
+
if (value.operator !== "\\cap" || isTlcEnumerable(value.left)) openStream(value.left);
|
|
1830
|
+
else openStream(value.right);
|
|
1831
|
+
if (value.operator === "\\cup") openStream(value.right);
|
|
1832
|
+
}
|
|
1833
|
+
};
|
|
1834
|
+
|
|
1835
|
+
const listUnionLikeTlc = (union: SetOperationValue) => {
|
|
1836
|
+
if (!isTlcEnumerable(union.left) || !isTlcEnumerable(union.right)) {
|
|
1837
|
+
throw new TlaEvaluationError(
|
|
1838
|
+
`Attempted to enumerate S \\cup T when S:\n${formatValue(union.left)}\nand T:\n${formatValue(union.right)}\nare not both enumerable`,
|
|
1839
|
+
);
|
|
1840
|
+
}
|
|
1841
|
+
openStream(union.left);
|
|
1842
|
+
openStream(union.right);
|
|
1843
|
+
if (isBeyondSetBound(union.left)) throw new TlaSetBoundError(getSetBoundMessage());
|
|
1844
|
+
const left = getStreamedElements(union.left);
|
|
1845
|
+
const right = isBeyondSetBound(union.right) ? undefined : getStreamedElements(union.right);
|
|
1846
|
+
if (!right || left.length + right.length > getMaximumSetSize()) {
|
|
1847
|
+
throw new TlaSetBoundError(getSetBoundMessage());
|
|
1848
|
+
}
|
|
1849
|
+
return [...left, ...right];
|
|
1850
|
+
};
|
|
1851
|
+
|
|
1852
|
+
// EnumerableValue#elements(NORMALIZED) sorts a union outside its methods, so a failed comparison
|
|
1853
|
+
// isn't a fingerprint failure.
|
|
1854
|
+
export const getNormalizedSetElements = (value: Value) => {
|
|
1855
|
+
if (
|
|
1856
|
+
typeof value !== "object" ||
|
|
1857
|
+
value.kind !== "SetOperation" ||
|
|
1858
|
+
value.operator !== "\\cup" ||
|
|
1859
|
+
value.enumerated
|
|
1860
|
+
) {
|
|
1861
|
+
return toFiniteSet(value).elements;
|
|
1862
|
+
}
|
|
1863
|
+
const listed = callTracedMethod(value, () => listUnionLikeTlc(value));
|
|
1864
|
+
const elements = toFiniteSet(createEnumeratedSet(listed)).elements;
|
|
1865
|
+
value.enumerated = createSortedSet(elements);
|
|
1866
|
+
return elements;
|
|
1867
|
+
};
|
|
1868
|
+
|
|
1869
|
+
// TLC streams the sets a lazy set combines until it caches it, so a shared element repeats. Like
|
|
1870
|
+
// its enumerators, the stream stops at the first element visit accepts.
|
|
1871
|
+
export const someStreamedElement = (value: Value, visit: (element: Value) => boolean): boolean => {
|
|
1872
|
+
if (typeof value !== "object" || isCachedSet(value)) {
|
|
1873
|
+
return toFiniteSet(value).elements.some(visit);
|
|
1874
|
+
}
|
|
1599
1875
|
const sets = setTrace.sets;
|
|
1600
1876
|
sets?.push(value);
|
|
1601
|
-
|
|
1877
|
+
openStream(value);
|
|
1878
|
+
// The set is enumerated only while it computes an element, not while visit looks at one.
|
|
1879
|
+
const isFound = someElementOfStream(value, (element) => {
|
|
1880
|
+
sets?.pop();
|
|
1881
|
+
const isAccepted = visit(element);
|
|
1882
|
+
sets?.push(value);
|
|
1883
|
+
return isAccepted;
|
|
1884
|
+
});
|
|
1602
1885
|
sets?.pop();
|
|
1886
|
+
return isFound;
|
|
1887
|
+
};
|
|
1888
|
+
|
|
1889
|
+
export const getStreamedElements = (value: Value) => {
|
|
1890
|
+
const elements: Value[] = [];
|
|
1891
|
+
someStreamedElement(value, (element) => {
|
|
1892
|
+
elements.push(element);
|
|
1893
|
+
return false;
|
|
1894
|
+
});
|
|
1603
1895
|
return elements;
|
|
1604
1896
|
};
|
|
1605
1897
|
|
|
@@ -1612,10 +1904,9 @@ const getCombinedSets = (value: ObjectValue) =>
|
|
|
1612
1904
|
? [value.range]
|
|
1613
1905
|
: undefined;
|
|
1614
1906
|
|
|
1615
|
-
// Whether
|
|
1616
|
-
//
|
|
1617
|
-
//
|
|
1618
|
-
// no record a comparison reorders.
|
|
1907
|
+
// Whether one evaluation can stand for the next: no UNION or \cup, which streams until TLC
|
|
1908
|
+
// caches it, no lambda, which fingerprinting turns into a record, and no record a
|
|
1909
|
+
// comparison reorders.
|
|
1619
1910
|
export const isSettledValue = (value: Value): boolean => {
|
|
1620
1911
|
if (typeof value !== "object") return true;
|
|
1621
1912
|
switch (value.kind) {
|
|
@@ -1644,10 +1935,9 @@ export const isSettledValue = (value: Value): boolean => {
|
|
|
1644
1935
|
}
|
|
1645
1936
|
};
|
|
1646
1937
|
|
|
1647
|
-
//
|
|
1648
|
-
// tuples, records, or functions it is a component of has to stream too.
|
|
1938
|
+
// A set of tuples, records, or functions with such a component has to stream too.
|
|
1649
1939
|
const isStreamedApart = (value: Value): boolean => {
|
|
1650
|
-
if (typeof value !== "object" ||
|
|
1940
|
+
if (typeof value !== "object" || isCachedSet(value)) return false;
|
|
1651
1941
|
if (value.kind === "Union") return true;
|
|
1652
1942
|
if (value.kind === "FilteredSet") return isStreamedApart(value.base);
|
|
1653
1943
|
if (value.kind === "SetOperation") {
|
|
@@ -1658,31 +1948,36 @@ const isStreamedApart = (value: Value): boolean => {
|
|
|
1658
1948
|
return getCombinedSets(value)?.some(isStreamedApart) ?? false;
|
|
1659
1949
|
};
|
|
1660
1950
|
|
|
1661
|
-
const
|
|
1662
|
-
// TLC streams the sets of a UNION it hasn't enumerated, so a set
|
|
1663
|
-
// its elements twice.
|
|
1951
|
+
const someElementOfStream = (value: ObjectValue, visit: (element: Value) => boolean): boolean => {
|
|
1952
|
+
// TLC streams the sets of a UNION it hasn't enumerated, so a repeated set yields them twice.
|
|
1664
1953
|
if (value.kind === "Union") {
|
|
1665
|
-
return getUnionSets(value, getStreamedElements).
|
|
1666
|
-
|
|
1954
|
+
return getUnionSets(value, getStreamedElements).some((set, index) =>
|
|
1955
|
+
someStreamedElement(getUnionSet(set, index, value), visit),
|
|
1667
1956
|
);
|
|
1668
1957
|
}
|
|
1669
|
-
if (value.kind === "FilteredSet" &&
|
|
1670
|
-
return
|
|
1958
|
+
if (value.kind === "FilteredSet" && isTlcEnumerable(value.base)) {
|
|
1959
|
+
return someStreamedElement(
|
|
1960
|
+
value.base,
|
|
1961
|
+
(element) => value.test(element, false) && visit(element),
|
|
1962
|
+
);
|
|
1671
1963
|
}
|
|
1672
1964
|
const components = getCombinedSets(value);
|
|
1673
|
-
if (components?.some(isStreamedApart))
|
|
1674
|
-
|
|
1965
|
+
if (components?.some(isStreamedApart)) {
|
|
1966
|
+
return enumerateSet(value, getStreamedElements).some(visit);
|
|
1967
|
+
}
|
|
1968
|
+
if (value.kind !== "SetOperation") return toFiniteSet(value).elements.some(visit);
|
|
1675
1969
|
if (value.operator === "\\cup") {
|
|
1676
1970
|
return !isTlcEnumerable(value.left) || !isTlcEnumerable(value.right)
|
|
1677
|
-
? toFiniteSet(value).elements
|
|
1678
|
-
:
|
|
1971
|
+
? toFiniteSet(value).elements.some(visit)
|
|
1972
|
+
: someStreamedElement(value.left, visit) || someStreamedElement(value.right, visit);
|
|
1679
1973
|
}
|
|
1680
1974
|
const isLeftEnumerated = value.operator === "\\" || isTlcEnumerable(value.left);
|
|
1681
1975
|
const enumerated = isLeftEnumerated ? value.left : value.right;
|
|
1682
1976
|
const other = isLeftEnumerated ? value.right : value.left;
|
|
1683
|
-
if (!isTlcEnumerable(enumerated)) return toFiniteSet(value).elements;
|
|
1684
|
-
return
|
|
1685
|
-
|
|
1977
|
+
if (!isTlcEnumerable(enumerated)) return toFiniteSet(value).elements.some(visit);
|
|
1978
|
+
return someStreamedElement(
|
|
1979
|
+
enumerated,
|
|
1980
|
+
(element) => isElementOf(element, other) === (value.operator === "\\cap") && visit(element),
|
|
1686
1981
|
);
|
|
1687
1982
|
};
|
|
1688
1983
|
|
|
@@ -1731,8 +2026,7 @@ const getProductCardinality = (
|
|
|
1731
2026
|
const describeOverflow = (value: Value) =>
|
|
1732
2027
|
`Overflow when computing the number of elements in:\n${formatValue(value)}`;
|
|
1733
2028
|
|
|
1734
|
-
// TLC 1.7.4 sizes every component
|
|
1735
|
-
// checking whether one is empty, so an infinite component fails even beside an empty one.
|
|
2029
|
+
// TLC 1.7.4 sizes every component without checking for an empty one, so an infinite one fails.
|
|
1736
2030
|
export const getCardinality = (value: Value, version?: TlcVersion): number => {
|
|
1737
2031
|
if (typeof value !== "object") {
|
|
1738
2032
|
throw new TlaEvaluationError(
|
|
@@ -1881,8 +2175,34 @@ const findSortedIndex = (sortedValues: Value[], target: Value, compare = compare
|
|
|
1881
2175
|
return -1;
|
|
1882
2176
|
};
|
|
1883
2177
|
|
|
2178
|
+
// A sorted set as TLC builds it unsorted, by adding each element it doesn't hold yet.
|
|
2179
|
+
export const createUnsortedCopy = (set: SetValue, added: Value[]): SetValue => {
|
|
2180
|
+
const elements = set.elements;
|
|
2181
|
+
if (elements.length > getMaximumSetSize()) throw new TlaSetBoundError(getSetBoundMessage());
|
|
2182
|
+
const isAdded = Array.from({ length: elements.length }, () => false);
|
|
2183
|
+
const written = added.filter((element) => {
|
|
2184
|
+
const index = findSortedIndex(elements, element);
|
|
2185
|
+
if (isAdded[index]) return false;
|
|
2186
|
+
isAdded[index] = true;
|
|
2187
|
+
return true;
|
|
2188
|
+
});
|
|
2189
|
+
const copy = createSortedSet(elements);
|
|
2190
|
+
if (written.some((element, index) => element !== elements[index])) {
|
|
2191
|
+
copy.writtenElements = written;
|
|
2192
|
+
}
|
|
2193
|
+
return copy;
|
|
2194
|
+
};
|
|
2195
|
+
|
|
1884
2196
|
export const getSequenceElements = (value: Value): Value[] | undefined => {
|
|
1885
2197
|
if (typeof value !== "object" || !isFunctionValue(value)) return undefined;
|
|
2198
|
+
if (
|
|
2199
|
+
value.kind === "LazyFunction" &&
|
|
2200
|
+
(!isSetValue(value.domain) || isOverriddenSet(value.domain))
|
|
2201
|
+
) {
|
|
2202
|
+
throw new TlaEvaluationError(
|
|
2203
|
+
"To convert a function of form [x \\in S |-> f(x)] to a tuple, the set S must be enumerable.",
|
|
2204
|
+
);
|
|
2205
|
+
}
|
|
1886
2206
|
const functionValue = toFiniteFunction(value);
|
|
1887
2207
|
return functionValue.domain.every((key, index) => key === index + 1)
|
|
1888
2208
|
? functionValue.range
|
|
@@ -1900,24 +2220,21 @@ const describeNonSetMembership = (element: Value, value: Value) => {
|
|
|
1900
2220
|
return `${prefix} function ${formatValue(value)}`;
|
|
1901
2221
|
};
|
|
1902
2222
|
|
|
1903
|
-
// The call stack of the replay, which a failed fast path leaves frozen.
|
|
1904
2223
|
interface TracedCallStack {
|
|
1905
2224
|
expressions: unknown[];
|
|
1906
2225
|
isFrozen: boolean;
|
|
2226
|
+
sources: WeakMap<ObjectValue, unknown>;
|
|
1907
2227
|
fingerprint?: unknown;
|
|
1908
2228
|
}
|
|
1909
2229
|
|
|
1910
|
-
// The
|
|
1911
|
-
//
|
|
1912
|
-
// check or enumeration leaves the values it was in, each of which TLC adds to the
|
|
1913
|
-
// FingerprintException when it has a source.
|
|
2230
|
+
// The values being checked or enumerated, outermost first, as TLC's CallStackTool replays a
|
|
2231
|
+
// computation. TLC adds each one with a source to the FingerprintException.
|
|
1914
2232
|
export const setTrace: { sets: Value[] | undefined; callStack?: TracedCallStack } = {
|
|
1915
2233
|
sets: undefined,
|
|
1916
2234
|
};
|
|
1917
2235
|
|
|
1918
|
-
//
|
|
1919
|
-
//
|
|
1920
|
-
// arguments, so a hot caller passes functions it doesn't allocate.
|
|
2236
|
+
// TLC never takes the fast path, so the failure's leftovers in the replay's trace go first.
|
|
2237
|
+
// The operands are arguments so that a hot caller passes functions it doesn't allocate.
|
|
1921
2238
|
export const computeLikeTlcOnFailure = <Left, Right, Result>(
|
|
1922
2239
|
compute: (left: Left, right: Right) => Result,
|
|
1923
2240
|
computeLikeTlc: (left: Left, right: Right, error: TlaEvaluationError) => Result,
|
|
@@ -1942,6 +2259,11 @@ export const computeLikeTlcOnFailure = <Left, Right, Result>(
|
|
|
1942
2259
|
}
|
|
1943
2260
|
};
|
|
1944
2261
|
|
|
2262
|
+
export const isFingerprintedSince = (depth: number) =>
|
|
2263
|
+
(setTrace.sets ?? [])
|
|
2264
|
+
.slice(depth)
|
|
2265
|
+
.some((set) => typeof set === "object" && setTrace.callStack?.sources.has(set) === true);
|
|
2266
|
+
|
|
1945
2267
|
export const isElementOf = (element: Value, set: Value): boolean => {
|
|
1946
2268
|
const sets = setTrace.sets;
|
|
1947
2269
|
if (sets === undefined) return isElementOfSet(element, set);
|
|
@@ -1951,26 +2273,60 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1951
2273
|
return isElement;
|
|
1952
2274
|
};
|
|
1953
2275
|
|
|
2276
|
+
// Integers compare by subtraction, so a search that meets only integers needs no other comparison.
|
|
2277
|
+
const isIntegerInSortedSet = (element: number, set: SetValue) => {
|
|
2278
|
+
const elements = set.elements;
|
|
2279
|
+
const bounds = { low: 0, high: elements.length - 1 };
|
|
2280
|
+
while (bounds.low <= bounds.high) {
|
|
2281
|
+
const middle = (bounds.low + bounds.high) >> 1;
|
|
2282
|
+
const probe = elements[middle];
|
|
2283
|
+
if (typeof probe !== "number") return undefined;
|
|
2284
|
+
if (probe === element) return true;
|
|
2285
|
+
if (probe < element) bounds.low = middle + 1;
|
|
2286
|
+
else bounds.high = middle - 1;
|
|
2287
|
+
}
|
|
2288
|
+
return false;
|
|
2289
|
+
};
|
|
2290
|
+
|
|
1954
2291
|
const isInSortedSet = (element: Value, set: SetValue) =>
|
|
1955
2292
|
findSortedIndex(set.elements, element) !== -1;
|
|
1956
2293
|
|
|
1957
2294
|
const isInSortedSetLikeTlc = (element: Value, set: SetValue) =>
|
|
1958
|
-
|
|
2295
|
+
set.isNormalized
|
|
1959
2296
|
? searchLikeTlc(element, set.elements).isFound
|
|
1960
|
-
: isElementOfLikeTlc(element, set
|
|
2297
|
+
: isElementOfLikeTlc(element, getWrittenElements(set));
|
|
2298
|
+
|
|
2299
|
+
const isModelValueMember = (modelValue: ModelValue, set: Value) => {
|
|
2300
|
+
if (modelValue.type !== undefined) {
|
|
2301
|
+
throw new TlaEvaluationError(
|
|
2302
|
+
`Attempted to check if the typed model value ${prettyPrint(formatValue(modelValue))} is an element of\n${prettyPrint(formatValue(set))}`,
|
|
2303
|
+
);
|
|
2304
|
+
}
|
|
2305
|
+
return false;
|
|
2306
|
+
};
|
|
1961
2307
|
|
|
2308
|
+
// The common cases stay small enough for V8 to inline.
|
|
1962
2309
|
const isElementOfSet = (element: Value, set: Value): boolean => {
|
|
2310
|
+
if (typeof element === "number" && typeof set === "object") {
|
|
2311
|
+
if (set.kind === "Set")
|
|
2312
|
+
return isIntegerInSortedSet(element, set) ?? isElementOfAnySet(element, set);
|
|
2313
|
+
if (set.kind === "Interval") return element >= set.low && element <= set.high;
|
|
2314
|
+
}
|
|
2315
|
+
return isElementOfAnySet(element, set);
|
|
2316
|
+
};
|
|
2317
|
+
|
|
2318
|
+
const isElementOfAnySet = (element: Value, set: Value): boolean => {
|
|
1963
2319
|
if (!isSetValue(set)) throw new TlaEvaluationError(describeNonSetMembership(element, set));
|
|
1964
2320
|
switch (set.kind) {
|
|
1965
2321
|
case "Set":
|
|
1966
|
-
//
|
|
2322
|
+
// TLC compares the element with the set's.
|
|
1967
2323
|
cacheSet(element);
|
|
1968
2324
|
return computeLikeTlcOnFailure(isInSortedSet, isInSortedSetLikeTlc, element, set);
|
|
1969
2325
|
case "MixedSet":
|
|
1970
2326
|
return isElementOfLikeTlc(element, set.elements);
|
|
1971
2327
|
case "Interval":
|
|
1972
2328
|
if (typeof element === "number") return element >= set.low && element <= set.high;
|
|
1973
|
-
if (set.low <= set.high && !isModelValue(element)) {
|
|
2329
|
+
if (set.low <= set.high && (!isModelValue(element) || element.type !== undefined)) {
|
|
1974
2330
|
throw new TlaEvaluationError(
|
|
1975
2331
|
`Attempted to check if the value:\n${formatValue(element)}\nis in the integer interval ${formatValue(set)}`,
|
|
1976
2332
|
);
|
|
@@ -1978,19 +2334,18 @@ const isElementOfSet = (element: Value, set: Value): boolean => {
|
|
|
1978
2334
|
return false;
|
|
1979
2335
|
case "InfiniteSet": {
|
|
1980
2336
|
if (set.name === "Any") return true;
|
|
1981
|
-
if (isModelValue(element)) return
|
|
2337
|
+
if (isModelValue(element)) return isModelValueMember(element, set);
|
|
1982
2338
|
const isMemberType =
|
|
1983
2339
|
set.name === "STRING" ? typeof element === "string" : typeof element === "number";
|
|
1984
2340
|
if (!isMemberType) {
|
|
1985
|
-
throw new
|
|
2341
|
+
throw new TlaModuleError(
|
|
1986
2342
|
`Attempted to check if the value:\n${formatValue(element)}\nis an element of ${set.name}.`,
|
|
1987
2343
|
);
|
|
1988
2344
|
}
|
|
1989
2345
|
return set.name !== "Nat" || (typeof element === "number" && element >= 0);
|
|
1990
2346
|
}
|
|
1991
2347
|
case "Subset":
|
|
1992
|
-
//
|
|
1993
|
-
// Java class overrides included.
|
|
2348
|
+
// TLC fails on a value that isn't Enumerable, model values and Java-overridden sets included.
|
|
1994
2349
|
if (!isSetValue(element) || isOverriddenSet(element)) {
|
|
1995
2350
|
throw new TlaEvaluationError(
|
|
1996
2351
|
`Attempted to check if the non-enumerable value\n${prettyPrint(formatValue(element))}\nis element of\n${prettyPrint(formatValue(set))}`,
|
|
@@ -1999,14 +2354,14 @@ const isElementOfSet = (element: Value, set: Value): boolean => {
|
|
|
1999
2354
|
return toFiniteSet(element).elements.every((item) => isElementOf(item, set.base));
|
|
2000
2355
|
case "FunctionSet": {
|
|
2001
2356
|
if (!isFunctionValue(element)) {
|
|
2002
|
-
if (isModelValue(element)) return
|
|
2003
|
-
//
|
|
2357
|
+
if (isModelValue(element)) return isModelValueMember(element, set);
|
|
2358
|
+
// TLC prints the element without Values.ppr.
|
|
2004
2359
|
throw new TlaEvaluationError(
|
|
2005
2360
|
`Attempted to check if \n${formatValue(element)}\nwhich is not a TLC function value, is in the set of functions:\n${prettyPrint(formatValue(set))}`,
|
|
2006
2361
|
);
|
|
2007
2362
|
}
|
|
2008
2363
|
const functionValue = toFiniteFunction(element);
|
|
2009
|
-
const low = getIntervalLow(functionValue.domain);
|
|
2364
|
+
const low = getIntervalLow(element, functionValue.domain);
|
|
2010
2365
|
const isDomain =
|
|
2011
2366
|
low === undefined
|
|
2012
2367
|
? isEqualLikeTlc(set.domain, createSortedSet(functionValue.domain))
|
|
@@ -2019,7 +2374,7 @@ const isElementOfSet = (element: Value, set: Value): boolean => {
|
|
|
2019
2374
|
case "RecordSet": {
|
|
2020
2375
|
const functionValue = isFunctionValue(element) ? toFiniteFunction(element) : undefined;
|
|
2021
2376
|
if (!functionValue || !isRecordDomain(functionValue.domain)) {
|
|
2022
|
-
if (isModelValue(element)) return
|
|
2377
|
+
if (isModelValue(element)) return isModelValueMember(element, set);
|
|
2023
2378
|
throw new TlaEvaluationError(
|
|
2024
2379
|
`Attempted to check if non-record\n${formatValue(element)}\nis in the set of records:\n${formatValue(set)}`,
|
|
2025
2380
|
);
|
|
@@ -2038,7 +2393,8 @@ const isElementOfSet = (element: Value, set: Value): boolean => {
|
|
|
2038
2393
|
const isFunction =
|
|
2039
2394
|
isFunctionValue(element) &&
|
|
2040
2395
|
toFiniteFunction(element).domain.every((key) => typeof key === "number");
|
|
2041
|
-
if (
|
|
2396
|
+
if (isModelValue(element)) return isModelValueMember(element, set);
|
|
2397
|
+
if (isFunction) return false;
|
|
2042
2398
|
throw new TlaEvaluationError(
|
|
2043
2399
|
`Attempted to check if non-tuple\n${formatValue(element)}\nis in the set of tuples:\n${formatValue(set)}`,
|
|
2044
2400
|
);
|
|
@@ -2049,14 +2405,29 @@ const isElementOfSet = (element: Value, set: Value): boolean => {
|
|
|
2049
2405
|
);
|
|
2050
2406
|
}
|
|
2051
2407
|
case "FilteredSet":
|
|
2052
|
-
|
|
2053
|
-
|
|
2408
|
+
try {
|
|
2409
|
+
return isElementOf(element, set.base) && set.test(element, true);
|
|
2410
|
+
} catch (error) {
|
|
2411
|
+
if (!(error instanceof TlaEvaluationError) || !isEvalException(error)) throw error;
|
|
2412
|
+
throw new TlaEvaluationError(
|
|
2413
|
+
`Cannot decide if element:\n${prettyPrint(formatValue(element))}\n is element of:\n${prettyPrint(formatValue(set.base))}\nand satisfies the predicate ${set.source?.predicate}`,
|
|
2414
|
+
);
|
|
2415
|
+
}
|
|
2416
|
+
case "Union": {
|
|
2054
2417
|
if (!isTlcEnumerable(set.base)) {
|
|
2055
2418
|
throw new TlaEvaluationError(
|
|
2056
2419
|
`Attempted to check if:\n ${formatValue(element)}\nis an element of the non-enumerable set:\n ${formatUnion(set)}`,
|
|
2057
2420
|
);
|
|
2058
2421
|
}
|
|
2059
|
-
|
|
2422
|
+
const sets = toFiniteSet(set.base).elements;
|
|
2423
|
+
// TLC sorts the sets to enumerate them, and comparing two sets normalizes both.
|
|
2424
|
+
if (sets.length > 1) {
|
|
2425
|
+
sets.forEach((inner) => {
|
|
2426
|
+
if (typeof inner === "object" && inner.kind === "Set") inner.isNormalized = true;
|
|
2427
|
+
});
|
|
2428
|
+
}
|
|
2429
|
+
return sets.some((inner) => isElementOf(element, inner));
|
|
2430
|
+
}
|
|
2060
2431
|
case "SetOperation":
|
|
2061
2432
|
if (set.operator === "\\cup")
|
|
2062
2433
|
return isElementOf(element, set.left) || isElementOf(element, set.right);
|
|
@@ -2067,9 +2438,9 @@ const isElementOfSet = (element: Value, set: Value): boolean => {
|
|
|
2067
2438
|
case "SequenceSet": {
|
|
2068
2439
|
const elements = getSequenceElements(element);
|
|
2069
2440
|
if (elements === undefined) {
|
|
2070
|
-
if (isModelValue(element)) return
|
|
2071
|
-
//
|
|
2072
|
-
throw new
|
|
2441
|
+
if (isModelValue(element)) return isModelValueMember(element, set);
|
|
2442
|
+
// TLC raises TLC_MODULE_CHECK_MEMBER_OF.
|
|
2443
|
+
throw new TlaModuleError(
|
|
2073
2444
|
`Attempted to check if the value:\n${prettyPrint(formatValue(element))}\nis an element of ${prettyPrint(formatValue(set))}.`,
|
|
2074
2445
|
);
|
|
2075
2446
|
}
|
|
@@ -2080,8 +2451,6 @@ const isElementOfSet = (element: Value, set: Value): boolean => {
|
|
|
2080
2451
|
}
|
|
2081
2452
|
};
|
|
2082
2453
|
|
|
2083
|
-
// A function that agrees with a member of a function set outside some entries is a member when
|
|
2084
|
-
// those entries are.
|
|
2085
2454
|
export const isUpdatedElementOf = (element: Value, member: Value, set: Value): boolean => {
|
|
2086
2455
|
if (element === member) return true;
|
|
2087
2456
|
if (
|
|
@@ -2102,15 +2471,20 @@ export const isUpdatedElementOf = (element: Value, member: Value, set: Value): b
|
|
|
2102
2471
|
export const toFiniteFunction = (value: Value): FunctionValue => {
|
|
2103
2472
|
if (typeof value === "object" && value.kind === "Function") return value;
|
|
2104
2473
|
if (typeof value === "object" && value.kind === "LazyFunction") {
|
|
2474
|
+
if (value.enumerated) return value.enumerated;
|
|
2105
2475
|
if (!isEnumerable(value.domain)) getCardinality(value.domain);
|
|
2476
|
+
if (!isSetValue(value.domain)) {
|
|
2477
|
+
throw new TlaEvaluationError("The domains of formal parameters must be enumerable.");
|
|
2478
|
+
}
|
|
2106
2479
|
const domain = getFunctionDomain(value.domain);
|
|
2107
|
-
|
|
2480
|
+
value.enumerated = setFunctionClass(
|
|
2108
2481
|
createSortedFunction(
|
|
2109
2482
|
domain,
|
|
2110
2483
|
domain.map((key) => value.apply(key)),
|
|
2111
2484
|
),
|
|
2112
2485
|
getFunctionClass(value),
|
|
2113
2486
|
);
|
|
2487
|
+
return value.enumerated;
|
|
2114
2488
|
}
|
|
2115
2489
|
throw new TlaEvaluationError(`Expected a function but found ${formatValue(value)}`);
|
|
2116
2490
|
};
|
|
@@ -2173,22 +2547,29 @@ export const createNonIntegerKeyError = (argument: Value) =>
|
|
|
2173
2547
|
`Attempted to apply function with integer domain to the non-integer argument ${prettyPrint(formatValue(argument))}`,
|
|
2174
2548
|
);
|
|
2175
2549
|
|
|
2176
|
-
// TLC
|
|
2177
|
-
// binary search with compareTo.
|
|
2550
|
+
// TLC searches a domain this small linearly with equals, and a larger one with compareTo.
|
|
2178
2551
|
const LINEAR_SEARCH_LIMIT = 32;
|
|
2179
2552
|
|
|
2180
|
-
const
|
|
2181
|
-
|
|
2182
|
-
|
|
2553
|
+
const findDomainIndex = (functionValue: FunctionValue, argument: Value) =>
|
|
2554
|
+
findSortedIndex(functionValue.domain, argument);
|
|
2555
|
+
|
|
2556
|
+
// TLC searches a function it hasn't normalized linearly, in the order its keys were written.
|
|
2557
|
+
const failKeyLikeTlc = (
|
|
2558
|
+
functionValue: FunctionValue,
|
|
2559
|
+
argument: Value,
|
|
2560
|
+
error: TlaEvaluationError,
|
|
2561
|
+
): never => {
|
|
2562
|
+
const keys = getWrittenKeys(functionValue);
|
|
2563
|
+
if (valueVersion.is174 || keys !== functionValue.domain || keys.length < LINEAR_SEARCH_LIMIT) {
|
|
2564
|
+
keys.forEach((key) => equalsLikeTlc(key, argument));
|
|
2183
2565
|
} else {
|
|
2184
|
-
findSortedIndex(
|
|
2566
|
+
findSortedIndex(keys, argument, compareLikeTlc);
|
|
2185
2567
|
}
|
|
2186
2568
|
throw error;
|
|
2187
2569
|
};
|
|
2188
2570
|
|
|
2189
|
-
//
|
|
2190
|
-
//
|
|
2191
|
-
// TLC 1.7.4 doesn't. A key of another type than the domain's keys otherwise fails TLC's comparison.
|
|
2571
|
+
// A tuple or a record rejects a key of the wrong type, and another function a non-integer for
|
|
2572
|
+
// an interval domain, which TLC 1.7.4 doesn't. Any other key type fails TLC's comparison.
|
|
2192
2573
|
const findKeyIndexLikeTlc = (
|
|
2193
2574
|
functionValue: FunctionValue,
|
|
2194
2575
|
argument: Value,
|
|
@@ -2202,21 +2583,87 @@ const findKeyIndexLikeTlc = (
|
|
|
2202
2583
|
if (keyType !== undefined && typeof argument !== keyType) {
|
|
2203
2584
|
throw new TlaEvaluationError(describeWrongKey(functionValue, argument));
|
|
2204
2585
|
}
|
|
2205
|
-
if (
|
|
2586
|
+
if (
|
|
2587
|
+
keyType === undefined &&
|
|
2588
|
+
!valueVersion.is174 &&
|
|
2589
|
+
typeof argument !== "number" &&
|
|
2590
|
+
isIntervalDomain(domain)
|
|
2591
|
+
) {
|
|
2206
2592
|
throw createNonIntegerKeyError(argument);
|
|
2207
2593
|
}
|
|
2208
2594
|
}
|
|
2209
2595
|
const identical = findIdenticalKey(domain, argument);
|
|
2210
2596
|
if (identical !== -1) return identical;
|
|
2211
|
-
return computeLikeTlcOnFailure(
|
|
2597
|
+
return computeLikeTlcOnFailure(findDomainIndex, failKeyLikeTlc, functionValue, argument);
|
|
2598
|
+
};
|
|
2599
|
+
|
|
2600
|
+
// TLC checks each argument against its bound in turn. FcnLambdaValue#apply then indexes past an
|
|
2601
|
+
// argument tuple shorter than the bounds and ignores one longer, where this rejects the list.
|
|
2602
|
+
const checkBoundArguments = (
|
|
2603
|
+
functionValue: LazyFunctionValue,
|
|
2604
|
+
tupleLengths: (number | undefined)[],
|
|
2605
|
+
argument: Value,
|
|
2606
|
+
) => {
|
|
2607
|
+
const domain = functionValue.domain;
|
|
2608
|
+
const elements = getSequenceElements(argument);
|
|
2609
|
+
const describe = (position: string, shown: Value, problem: string) =>
|
|
2610
|
+
`In applying the function\n${prettyPrint(formatValue(functionValue))},\nthe ${position} is:\n${prettyPrint(formatValue(shown))}\nwhich ${problem}.\n`;
|
|
2611
|
+
const createListError = () =>
|
|
2612
|
+
new TlaEvaluationError(
|
|
2613
|
+
describe("argument list", argument, "does not match its formal parameter"),
|
|
2614
|
+
);
|
|
2615
|
+
if (elements === undefined) throw createListError();
|
|
2616
|
+
if (typeof domain !== "object" || domain.kind !== "ProductSet") {
|
|
2617
|
+
throw new TlaEvaluationError(
|
|
2618
|
+
`Attempted to apply a function to ${formatValue(argument)}, which is not in its domain`,
|
|
2619
|
+
);
|
|
2620
|
+
}
|
|
2621
|
+
tupleLengths.forEach((tupleLength, index) => {
|
|
2622
|
+
if (index >= elements.length) throw createListError();
|
|
2623
|
+
const element = elements[index];
|
|
2624
|
+
const position = `argument number ${index + 1}`;
|
|
2625
|
+
if (!isElementOf(element, domain.factors[index])) {
|
|
2626
|
+
throw new TlaEvaluationError(
|
|
2627
|
+
describe(
|
|
2628
|
+
position,
|
|
2629
|
+
element,
|
|
2630
|
+
tupleLength === undefined
|
|
2631
|
+
? `is not in the function's domain ${formatValue(domain)}`
|
|
2632
|
+
: "is not in its domain",
|
|
2633
|
+
),
|
|
2634
|
+
);
|
|
2635
|
+
}
|
|
2636
|
+
if (tupleLength !== undefined && getSequenceElements(element)?.length !== tupleLength) {
|
|
2637
|
+
throw new TlaEvaluationError(
|
|
2638
|
+
describe(position, element, "does not match its formal parameter"),
|
|
2639
|
+
);
|
|
2640
|
+
}
|
|
2641
|
+
});
|
|
2642
|
+
if (elements.length > tupleLengths.length) throw createListError();
|
|
2212
2643
|
};
|
|
2213
2644
|
|
|
2645
|
+
// TLC checks that a FcnLambdaValue's domain holds the argument, and searches only the FcnRcdValue
|
|
2646
|
+
// it converts the lambda to when it fingerprints it.
|
|
2647
|
+
const isOutsideLambdaDomain = (functionValue: FunctionValue, argument: Value) =>
|
|
2648
|
+
functionValue.lambdaDomain !== undefined &&
|
|
2649
|
+
functionValue.hash === 0 &&
|
|
2650
|
+
functionValue.fingerprint === 0 &&
|
|
2651
|
+
!isElementOf(argument, functionValue.lambdaDomain);
|
|
2652
|
+
|
|
2214
2653
|
export const applyFunction = (
|
|
2215
2654
|
functionValue: Value,
|
|
2216
2655
|
argument: Value,
|
|
2217
2656
|
describeMissing?: (functionValue: FunctionValue, argument: Value) => string,
|
|
2218
2657
|
): Value => {
|
|
2219
2658
|
if (typeof functionValue === "object" && functionValue.kind === "LazyFunction") {
|
|
2659
|
+
if (functionValue.enumerated) {
|
|
2660
|
+
return applyFunction(functionValue.enumerated, argument, describeMissing);
|
|
2661
|
+
}
|
|
2662
|
+
const tupleLengths = functionValue.boundTupleLengths;
|
|
2663
|
+
if (tupleLengths !== undefined) {
|
|
2664
|
+
checkBoundArguments(functionValue, tupleLengths, argument);
|
|
2665
|
+
return functionValue.apply(argument);
|
|
2666
|
+
}
|
|
2220
2667
|
if (!isElementOf(argument, functionValue.domain)) {
|
|
2221
2668
|
throw new TlaEvaluationError(
|
|
2222
2669
|
getFunctionClass(functionValue) === "lambda"
|
|
@@ -2232,6 +2679,9 @@ export const applyFunction = (
|
|
|
2232
2679
|
);
|
|
2233
2680
|
}
|
|
2234
2681
|
const describe = describeMissing ?? describeMissingArgument;
|
|
2682
|
+
if (isOutsideLambdaDomain(functionValue, argument)) {
|
|
2683
|
+
throw new TlaEvaluationError(describe(functionValue, argument));
|
|
2684
|
+
}
|
|
2235
2685
|
const position = findKeyIndexLikeTlc(functionValue, argument, describe);
|
|
2236
2686
|
if (position === -1) throw new TlaEvaluationError(describe(functionValue, argument));
|
|
2237
2687
|
return functionValue.range[position];
|
|
@@ -2245,8 +2695,18 @@ export const getDomainInterval = (domain: Value[]) =>
|
|
|
2245
2695
|
export const getDomain = (functionValue: Value): Value => {
|
|
2246
2696
|
if (typeof functionValue === "object" && functionValue.kind === "LazyFunction")
|
|
2247
2697
|
return functionValue.domain;
|
|
2248
|
-
const
|
|
2249
|
-
|
|
2698
|
+
const finite = toFiniteFunction(functionValue);
|
|
2699
|
+
if (finite.functionClass === "lambda" && finite.lambdaDomain) return finite.lambdaDomain;
|
|
2700
|
+
const interval = getDomainInterval(finite.domain);
|
|
2701
|
+
if (interval) return interval;
|
|
2702
|
+
const set = createSortedSet(finite.domain);
|
|
2703
|
+
// TLC keeps a record's domain in written order and sorts any other function's.
|
|
2704
|
+
if (finite.functionClass === "record") {
|
|
2705
|
+
if (finite.writtenKeys) set.writtenElements = finite.writtenKeys;
|
|
2706
|
+
return set;
|
|
2707
|
+
}
|
|
2708
|
+
set.isNormalized = true;
|
|
2709
|
+
return set;
|
|
2250
2710
|
};
|
|
2251
2711
|
|
|
2252
2712
|
export const updateFunction = (
|
|
@@ -2258,7 +2718,7 @@ export const updateFunction = (
|
|
|
2258
2718
|
const isLazy =
|
|
2259
2719
|
typeof functionValue === "object" &&
|
|
2260
2720
|
functionValue.kind === "LazyFunction" &&
|
|
2261
|
-
(isKeptLazy ||
|
|
2721
|
+
(isKeptLazy || functionValue.enumerated === undefined);
|
|
2262
2722
|
if (isLazy) {
|
|
2263
2723
|
if (!isElementOf(argument, functionValue.domain)) return functionValue;
|
|
2264
2724
|
const updated = update(functionValue.apply(argument));
|
|
@@ -2268,6 +2728,7 @@ export const updateFunction = (
|
|
|
2268
2728
|
domain: functionValue.domain,
|
|
2269
2729
|
apply: (key) => (isEqualLikeTlc(argument, key) ? updated : functionValue.apply(key)),
|
|
2270
2730
|
source: functionValue.source,
|
|
2731
|
+
boundTupleLengths: functionValue.boundTupleLengths,
|
|
2271
2732
|
},
|
|
2272
2733
|
getFunctionClass(functionValue),
|
|
2273
2734
|
);
|
|
@@ -2278,6 +2739,7 @@ export const updateFunction = (
|
|
|
2278
2739
|
);
|
|
2279
2740
|
}
|
|
2280
2741
|
const finite = toFiniteFunction(functionValue);
|
|
2742
|
+
if (isOutsideLambdaDomain(finite, argument)) return finite;
|
|
2281
2743
|
const position = findKeyIndexLikeTlc(finite, argument, describeExceptKey);
|
|
2282
2744
|
if (position === -1) return finite;
|
|
2283
2745
|
const range = [...finite.range];
|
|
@@ -2289,6 +2751,7 @@ export const updateFunction = (
|
|
|
2289
2751
|
getFunctionClass(finite),
|
|
2290
2752
|
);
|
|
2291
2753
|
updated.writtenKeys = finite.writtenKeys;
|
|
2754
|
+
updated.lambdaDomain = finite.lambdaDomain;
|
|
2292
2755
|
if (finite.hash !== 0 && finite.hash !== UNFINGERPRINTABLE_HASH) {
|
|
2293
2756
|
const nextHash = getValueHash(next);
|
|
2294
2757
|
if (nextHash !== UNFINGERPRINTABLE_HASH) {
|
|
@@ -2315,8 +2778,6 @@ export const updateFunction = (
|
|
|
2315
2778
|
return updated;
|
|
2316
2779
|
};
|
|
2317
2780
|
|
|
2318
|
-
const valueHashes = new WeakMap<ObjectValue, number>();
|
|
2319
|
-
|
|
2320
2781
|
const HASH_SEED = 0x811c9dc5;
|
|
2321
2782
|
|
|
2322
2783
|
const finalizeHash = (hash: number) => {
|
|
@@ -2343,16 +2804,13 @@ const getElementContribution = (elementHash: number) =>
|
|
|
2343
2804
|
const getEntryContribution = (keyHash: number, valueHash: number) =>
|
|
2344
2805
|
finalizeHash((Math.imul(keyHash, 0x27d4eb2d) + valueHash) | 0);
|
|
2345
2806
|
|
|
2346
|
-
// The hash of
|
|
2347
|
-
//
|
|
2348
|
-
// when TLC fingerprints it.
|
|
2807
|
+
// The hash of a value TLC fails to fingerprint, such as Nat. Calls with one as an argument still
|
|
2808
|
+
// share their result, and a state that holds one fails when TLC fingerprints it.
|
|
2349
2809
|
const UNFINGERPRINTABLE_HASH = 0x2f6b1c4d;
|
|
2350
2810
|
|
|
2351
|
-
const computeValueHash = (value:
|
|
2352
|
-
if (value.kind === "
|
|
2353
|
-
|
|
2354
|
-
if (!isEnumerable(value)) return UNFINGERPRINTABLE_HASH;
|
|
2355
|
-
const elements = toFiniteSet(value).elements;
|
|
2811
|
+
const computeValueHash = (value: SetValue | FunctionValue) => {
|
|
2812
|
+
if (value.kind === "Set") {
|
|
2813
|
+
const elements = value.elements;
|
|
2356
2814
|
let hash = SET_HASH_SEED;
|
|
2357
2815
|
for (let index = 0; index < elements.length; index++) {
|
|
2358
2816
|
const elementHash = getValueHash(elements[index]);
|
|
@@ -2361,12 +2819,10 @@ const computeValueHash = (value: ObjectValue) => {
|
|
|
2361
2819
|
}
|
|
2362
2820
|
return hash;
|
|
2363
2821
|
}
|
|
2364
|
-
if (value.kind === "LazyFunction" && !isEnumerable(value.domain)) return UNFINGERPRINTABLE_HASH;
|
|
2365
|
-
const functionValue = toFiniteFunction(value);
|
|
2366
2822
|
let hash = FUNCTION_HASH_SEED;
|
|
2367
|
-
for (let index = 0; index <
|
|
2368
|
-
const keyHash = getValueHash(
|
|
2369
|
-
const itemHash = getValueHash(
|
|
2823
|
+
for (let index = 0; index < value.domain.length; index++) {
|
|
2824
|
+
const keyHash = getValueHash(value.domain[index]);
|
|
2825
|
+
const itemHash = getValueHash(value.range[index]);
|
|
2370
2826
|
if (keyHash === UNFINGERPRINTABLE_HASH || itemHash === UNFINGERPRINTABLE_HASH) {
|
|
2371
2827
|
return UNFINGERPRINTABLE_HASH;
|
|
2372
2828
|
}
|
|
@@ -2375,8 +2831,7 @@ const computeValueHash = (value: ObjectValue) => {
|
|
|
2375
2831
|
return hash;
|
|
2376
2832
|
};
|
|
2377
2833
|
|
|
2378
|
-
// TLC
|
|
2379
|
-
// the like, which it never enumerates.
|
|
2834
|
+
// TLC enumerates each set and function, and fails on Nat, Seq(S) and the like.
|
|
2380
2835
|
const fingerprintValue = (value: Value): void => {
|
|
2381
2836
|
if (typeof value !== "object" || value.kind === "ModelValue") return;
|
|
2382
2837
|
if (value.kind === "InfiniteSet" || value.kind === "SequenceSet") {
|
|
@@ -2393,8 +2848,7 @@ const fingerprintValue = (value: Value): void => {
|
|
|
2393
2848
|
functionValue.range.forEach(fingerprintValue);
|
|
2394
2849
|
};
|
|
2395
2850
|
|
|
2396
|
-
//
|
|
2397
|
-
// too large to enumerate, or one of a value with the hash of the values TLC fails on.
|
|
2851
|
+
// Hashing can fail, as for a set too large to enumerate, and so can a value TLC fails on.
|
|
2398
2852
|
export const getFingerprintError = (value: Value) => {
|
|
2399
2853
|
if (typeof value !== "object" || value.kind === "ModelValue") return undefined;
|
|
2400
2854
|
try {
|
|
@@ -2406,7 +2860,6 @@ export const getFingerprintError = (value: Value) => {
|
|
|
2406
2860
|
}
|
|
2407
2861
|
};
|
|
2408
2862
|
|
|
2409
|
-
// The values a value holds, which TLC fingerprints as it fingerprints the value.
|
|
2410
2863
|
export const getHeldValues = (value: ObjectValue): Value[] => {
|
|
2411
2864
|
switch (value.kind) {
|
|
2412
2865
|
case "Set":
|
|
@@ -2442,15 +2895,15 @@ export const getValueHash = (value: Value): number => {
|
|
|
2442
2895
|
if (value.hash === 0) value.hash = computeValueHash(value);
|
|
2443
2896
|
return value.hash;
|
|
2444
2897
|
}
|
|
2445
|
-
|
|
2446
|
-
if (
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2898
|
+
if (value.kind === "ModelValue") return value.hash;
|
|
2899
|
+
if (value.kind === "LazyFunction") {
|
|
2900
|
+
return isEnumerable(value.domain)
|
|
2901
|
+
? getValueHash(toFiniteFunction(value))
|
|
2902
|
+
: UNFINGERPRINTABLE_HASH;
|
|
2903
|
+
}
|
|
2904
|
+
return isEnumerable(value) ? getValueHash(toFiniteSet(value)) : UNFINGERPRINTABLE_HASH;
|
|
2450
2905
|
};
|
|
2451
2906
|
|
|
2452
|
-
const valueFingerprints = new WeakMap<ObjectValue, number>();
|
|
2453
|
-
|
|
2454
2907
|
const finalizeFingerprint = (hash: number) => {
|
|
2455
2908
|
const first = Math.imul(hash ^ (hash >>> 15), 0x2c1b3c6d);
|
|
2456
2909
|
const second = Math.imul(first ^ (first >>> 12), 0x297a2d39);
|
|
@@ -2472,23 +2925,18 @@ const getEntryFingerprint = (key: Value, value: Value) =>
|
|
|
2472
2925
|
(Math.imul(getValueFingerprint(key), 0x165667b1) + getValueFingerprint(value)) | 0,
|
|
2473
2926
|
);
|
|
2474
2927
|
|
|
2475
|
-
const computeValueFingerprint = (value:
|
|
2476
|
-
if (value.kind === "
|
|
2477
|
-
|
|
2478
|
-
if (!isEnumerable(value)) return fingerprintString(getValueKey(value));
|
|
2479
|
-
const elements = toFiniteSet(value).elements;
|
|
2928
|
+
const computeValueFingerprint = (value: SetValue | FunctionValue) => {
|
|
2929
|
+
if (value.kind === "Set") {
|
|
2930
|
+
const elements = value.elements;
|
|
2480
2931
|
let fingerprint = 0x1b873593;
|
|
2481
2932
|
for (let index = 0; index < elements.length; index++) {
|
|
2482
2933
|
fingerprint = (fingerprint + getElementFingerprint(elements[index])) | 0;
|
|
2483
2934
|
}
|
|
2484
2935
|
return fingerprint;
|
|
2485
2936
|
}
|
|
2486
|
-
const functionValue = toFiniteFunction(value);
|
|
2487
2937
|
let fingerprint = 0x4cf5ad43;
|
|
2488
|
-
for (let index = 0; index <
|
|
2489
|
-
fingerprint =
|
|
2490
|
-
(fingerprint + getEntryFingerprint(functionValue.domain[index], functionValue.range[index])) |
|
|
2491
|
-
0;
|
|
2938
|
+
for (let index = 0; index < value.domain.length; index++) {
|
|
2939
|
+
fingerprint = (fingerprint + getEntryFingerprint(value.domain[index], value.range[index])) | 0;
|
|
2492
2940
|
}
|
|
2493
2941
|
return fingerprint;
|
|
2494
2942
|
};
|
|
@@ -2501,24 +2949,18 @@ export const getValueFingerprint = (value: Value): number => {
|
|
|
2501
2949
|
if (value.fingerprint === 0) value.fingerprint = computeValueFingerprint(value);
|
|
2502
2950
|
return value.fingerprint;
|
|
2503
2951
|
}
|
|
2504
|
-
|
|
2505
|
-
if (
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2952
|
+
if (value.kind === "ModelValue") return value.fingerprint;
|
|
2953
|
+
if (value.kind === "LazyFunction") return getValueFingerprint(toFiniteFunction(value));
|
|
2954
|
+
return isEnumerable(value)
|
|
2955
|
+
? getValueFingerprint(toFiniteSet(value))
|
|
2956
|
+
: fingerprintString(getValueKey(value));
|
|
2509
2957
|
};
|
|
2510
2958
|
|
|
2511
|
-
const valueKeys = new WeakMap<ObjectValue, string>();
|
|
2512
|
-
|
|
2513
2959
|
export const getValueKey = (value: Value): string => {
|
|
2514
2960
|
if (typeof value === "boolean") return value ? "T" : "F";
|
|
2515
2961
|
if (typeof value === "number") return String(value);
|
|
2516
2962
|
if (typeof value === "string") return JSON.stringify(value);
|
|
2517
|
-
|
|
2518
|
-
if (cached !== undefined) return cached;
|
|
2519
|
-
const key = computeValueKey(value);
|
|
2520
|
-
valueKeys.set(value, key);
|
|
2521
|
-
return key;
|
|
2963
|
+
return computeValueKey(value);
|
|
2522
2964
|
};
|
|
2523
2965
|
|
|
2524
2966
|
const computeValueKey = (value: ObjectValue) => {
|
|
@@ -2538,8 +2980,17 @@ const computeValueKey = (value: ObjectValue) => {
|
|
|
2538
2980
|
return `(${functionValue.domain.map((key, index) => `${getValueKey(key)}:${getValueKey(functionValue.range[index])}`).join(",")})`;
|
|
2539
2981
|
};
|
|
2540
2982
|
|
|
2983
|
+
const PRINTED_ESCAPES: Record<string, string> = {
|
|
2984
|
+
'"': '\\"',
|
|
2985
|
+
"\\": "\\\\",
|
|
2986
|
+
"\t": "\\t",
|
|
2987
|
+
"\n": "\\n",
|
|
2988
|
+
"\f": "\\f",
|
|
2989
|
+
"\r": "\\r",
|
|
2990
|
+
};
|
|
2991
|
+
|
|
2541
2992
|
const formatString = (value: string) =>
|
|
2542
|
-
`"${value.replace(
|
|
2993
|
+
`"${value.replace(/["\\\t\n\f\r]/g, (character) => PRINTED_ESCAPES[character])}"`;
|
|
2543
2994
|
|
|
2544
2995
|
type LazySetValue =
|
|
2545
2996
|
| SubsetValue
|
|
@@ -2565,23 +3016,27 @@ const expandLazySet = (value: LazySetValue) => {
|
|
|
2565
3016
|
if (value.kind === "SetOperation" || value.kind === "FilteredSet" || value.kind === "Union") {
|
|
2566
3017
|
return toFiniteSet(value);
|
|
2567
3018
|
}
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
getCardinality(value.
|
|
2572
|
-
|
|
2573
|
-
return createSortedSet([createSortedFunction([], [])]);
|
|
3019
|
+
// TLC asks whether the domain is empty before it sizes it.
|
|
3020
|
+
if (value.kind === "FunctionSet") {
|
|
3021
|
+
const domainSize = isEmptySet(value.domain) ? 0 : getCardinality(value.domain);
|
|
3022
|
+
const size = domainSize === 0 ? 1 : getCardinality(value.range) ** domainSize;
|
|
3023
|
+
return size <= MAXIMUM_EXPANDED_PRODUCT ? toFiniteSet(value) : undefined;
|
|
2574
3024
|
}
|
|
2575
|
-
const size =
|
|
2576
|
-
value.kind === "FunctionSet"
|
|
2577
|
-
? getCardinality(value.range) ** getCardinality(value.domain)
|
|
2578
|
-
: getProductSize(value.kind === "RecordSet" ? value.sets : value.factors);
|
|
2579
|
-
if (size === 0) return EMPTY_SET;
|
|
3025
|
+
const size = getProductSize(value.kind === "RecordSet" ? value.sets : value.factors);
|
|
2580
3026
|
return size <= MAXIMUM_EXPANDED_PRODUCT ? toFiniteSet(value) : undefined;
|
|
2581
3027
|
};
|
|
2582
3028
|
|
|
2583
3029
|
const getExpandedLazySet = (value: LazySetValue, isStrict: boolean) => {
|
|
2584
|
-
|
|
3030
|
+
// UnionValue alone of TLC's lazy sets doesn't swallow the failure to enumerate itself.
|
|
3031
|
+
if (isStrict || value.kind === "Union") return expandLazySet(value);
|
|
3032
|
+
// TLC sizes the base of SUBSET before it enumerates it, and only a failure to size it is quiet.
|
|
3033
|
+
if (
|
|
3034
|
+
value.kind === "Subset" &&
|
|
3035
|
+
isFunctionValue(value.base) &&
|
|
3036
|
+
(value.base.kind === "Function" || isEnumerable(value.base.domain))
|
|
3037
|
+
) {
|
|
3038
|
+
return expandLazySet(value);
|
|
3039
|
+
}
|
|
2585
3040
|
// TLC prints a set it can't enumerate symbolically, except in Print, PrintT, and ToString.
|
|
2586
3041
|
if (!isEnumerable(value)) return undefined;
|
|
2587
3042
|
try {
|
|
@@ -2616,24 +3071,78 @@ const formatLazySet = (value: LazySetValue, isStrict: boolean): string => {
|
|
|
2616
3071
|
}
|
|
2617
3072
|
};
|
|
2618
3073
|
|
|
2619
|
-
//
|
|
2620
|
-
// decimal digits, and underscores, with at least one letter.
|
|
3074
|
+
// TLC prints a record when every key is letters, digits, and underscores, with a letter.
|
|
2621
3075
|
const isFieldName = (name: string) =>
|
|
2622
3076
|
/^[\p{L}\p{Nd}_]*$/u.test(name) &&
|
|
2623
3077
|
/\p{L}/u.test(name) &&
|
|
2624
3078
|
(name.length < 4 || !/^[WS]F_/.test(name));
|
|
2625
3079
|
|
|
2626
|
-
// FcnLambdaValue#toString prints a function it fails to enumerate as its parameters and body.
|
|
2627
3080
|
const toPrintedFunction = (value: LazyFunctionValue) => {
|
|
2628
3081
|
if (!isEnumerable(value.domain)) return undefined;
|
|
2629
3082
|
try {
|
|
2630
3083
|
return toFiniteFunction(value);
|
|
2631
3084
|
} catch (error) {
|
|
2632
|
-
if (error instanceof TlaEvaluationError) return undefined;
|
|
3085
|
+
if (error instanceof TlaEvaluationError || isStackOverflow(error)) return undefined;
|
|
2633
3086
|
throw error;
|
|
2634
3087
|
}
|
|
2635
3088
|
};
|
|
2636
3089
|
|
|
3090
|
+
// TLC prints normalized copies, so the printed values themselves stay unnormalized.
|
|
3091
|
+
export const formatNormalizedCopies = (values: Value[]) => {
|
|
3092
|
+
const markedSets: SetValue[] = [];
|
|
3093
|
+
const normalize = (value: Value): void => {
|
|
3094
|
+
if (typeof value !== "object") return;
|
|
3095
|
+
switch (value.kind) {
|
|
3096
|
+
case "Set":
|
|
3097
|
+
value.elements.forEach(normalize);
|
|
3098
|
+
if (value.isNormalized) return;
|
|
3099
|
+
value.isNormalized = true;
|
|
3100
|
+
markedSets.push(value);
|
|
3101
|
+
return;
|
|
3102
|
+
case "MixedSet":
|
|
3103
|
+
value.elements.forEach(normalize);
|
|
3104
|
+
toFiniteSet(value);
|
|
3105
|
+
return;
|
|
3106
|
+
case "Function":
|
|
3107
|
+
value.range.forEach(normalize);
|
|
3108
|
+
return;
|
|
3109
|
+
case "LazyFunction":
|
|
3110
|
+
normalize(value.enumerated ?? value.domain);
|
|
3111
|
+
return;
|
|
3112
|
+
case "SetOperation":
|
|
3113
|
+
normalize(value.left);
|
|
3114
|
+
normalize(value.right);
|
|
3115
|
+
return;
|
|
3116
|
+
case "FunctionSet":
|
|
3117
|
+
normalize(value.domain);
|
|
3118
|
+
normalize(value.range);
|
|
3119
|
+
return;
|
|
3120
|
+
case "RecordSet":
|
|
3121
|
+
value.sets.forEach(normalize);
|
|
3122
|
+
return;
|
|
3123
|
+
case "ProductSet":
|
|
3124
|
+
value.factors.forEach(normalize);
|
|
3125
|
+
return;
|
|
3126
|
+
case "Subset":
|
|
3127
|
+
case "Union":
|
|
3128
|
+
case "FilteredSet":
|
|
3129
|
+
case "SequenceSet":
|
|
3130
|
+
normalize(value.base);
|
|
3131
|
+
return;
|
|
3132
|
+
default:
|
|
3133
|
+
return;
|
|
3134
|
+
}
|
|
3135
|
+
};
|
|
3136
|
+
try {
|
|
3137
|
+
values.forEach(normalize);
|
|
3138
|
+
return values.map((value) => formatValue(value, true, true));
|
|
3139
|
+
} finally {
|
|
3140
|
+
markedSets.forEach((set) => {
|
|
3141
|
+
set.isNormalized = false;
|
|
3142
|
+
});
|
|
3143
|
+
}
|
|
3144
|
+
};
|
|
3145
|
+
|
|
2637
3146
|
export const formatValue = (value: Value, isStrict = false, isNormalized = false): string => {
|
|
2638
3147
|
const format = (inner: Value) => formatValue(inner, isStrict, isNormalized);
|
|
2639
3148
|
if (typeof value === "boolean") return value ? "TRUE" : "FALSE";
|
|
@@ -2657,7 +3166,7 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
|
|
|
2657
3166
|
case "SetOperation":
|
|
2658
3167
|
case "FilteredSet":
|
|
2659
3168
|
case "Union": {
|
|
2660
|
-
const expanded = getExpandedLazySet(value, isStrict);
|
|
3169
|
+
const expanded = printing.isExpanded ? getExpandedLazySet(value, isStrict) : undefined;
|
|
2661
3170
|
return expanded ? format(expanded) : formatLazySet(value, isStrict);
|
|
2662
3171
|
}
|
|
2663
3172
|
|
|
@@ -2665,7 +3174,12 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
|
|
|
2665
3174
|
return `Seq(${format(value.base)})`;
|
|
2666
3175
|
case "Function":
|
|
2667
3176
|
case "LazyFunction": {
|
|
2668
|
-
const functionValue =
|
|
3177
|
+
const functionValue =
|
|
3178
|
+
value.kind !== "LazyFunction"
|
|
3179
|
+
? value
|
|
3180
|
+
: printing.isExpanded || !value.source
|
|
3181
|
+
? toPrintedFunction(value)
|
|
3182
|
+
: undefined;
|
|
2669
3183
|
if (functionValue === undefined) {
|
|
2670
3184
|
const source = value.kind === "LazyFunction" ? value.source : undefined;
|
|
2671
3185
|
if (!source) return "<function>";
|
|
@@ -2684,34 +3198,112 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
|
|
|
2684
3198
|
// TLC normalizes a lambda's domain to print it, but evaluates its body afterward.
|
|
2685
3199
|
const formatRange = (inner: Value) =>
|
|
2686
3200
|
formatValue(inner, isStrict, isNormalized && getFunctionClass(value) !== "lambda");
|
|
3201
|
+
if (functionValue.domain.length === 0 && getFunctionClass(value) === "record") return "[]";
|
|
2687
3202
|
if (functionValue.domain.every((key, index) => key === index + 1)) {
|
|
2688
3203
|
return `<<${functionValue.range.map(formatRange).join(", ")}>>`;
|
|
2689
3204
|
}
|
|
2690
|
-
|
|
2691
|
-
|
|
3205
|
+
const pairs = isNormalized
|
|
3206
|
+
? functionValue.domain.map((key, index) => [key, functionValue.range[index]])
|
|
3207
|
+
: getPrintedPairs(functionValue);
|
|
3208
|
+
// A RecordValue always prints as a record, a FcnRcdValue only if its keys are names.
|
|
2692
3209
|
if (
|
|
2693
3210
|
getFunctionClass(value) === "record" ||
|
|
2694
3211
|
functionValue.domain.every((key) => typeof key === "string" && isFieldName(key))
|
|
2695
3212
|
) {
|
|
2696
|
-
const isSorted =
|
|
2697
|
-
isNormalized || functionValue.hash !== 0 || functionValue.fingerprint !== 0;
|
|
2698
|
-
const pairs = isSorted
|
|
2699
|
-
? functionValue.domain.map((key, index) => [key, functionValue.range[index]])
|
|
2700
|
-
: getWrittenPairs(functionValue);
|
|
2701
3213
|
return `[${pairs.map((pair) => `${String(pair[0])} |-> ${formatRange(pair[1])}`).join(", ")}]`;
|
|
2702
3214
|
}
|
|
2703
|
-
return `(${
|
|
3215
|
+
return `(${pairs.map((pair) => `${format(pair[0])} :> ${formatRange(pair[1])}`).join(" @@ ")})`;
|
|
2704
3216
|
}
|
|
2705
3217
|
}
|
|
2706
3218
|
};
|
|
2707
3219
|
|
|
2708
|
-
// TLC
|
|
2709
|
-
|
|
3220
|
+
// TLC counts a value it can't compare with the previous one as changed.
|
|
3221
|
+
const isUnchanged = (previous: Value | undefined, value: Value) => {
|
|
3222
|
+
if (previous === undefined) return false;
|
|
3223
|
+
try {
|
|
3224
|
+
return valuesEqual(previous, value);
|
|
3225
|
+
} catch (error) {
|
|
3226
|
+
if (error instanceof TlaEvaluationError) return false;
|
|
3227
|
+
throw error;
|
|
3228
|
+
}
|
|
3229
|
+
};
|
|
3230
|
+
|
|
3231
|
+
const JAVA_FORMAT_SPECIFIER = /%(\d+\$)?([-#+ 0,(<]*)(\d+)?(\.\d+)?(.?)/gs;
|
|
3232
|
+
|
|
3233
|
+
const getJavaHashCode = (text: string) =>
|
|
3234
|
+
Array.from({ length: text.length }, (_, index) => text.charCodeAt(index)).reduce(
|
|
3235
|
+
(hash, unit) => (Math.imul(hash, 31) + unit) | 0,
|
|
3236
|
+
0,
|
|
3237
|
+
);
|
|
3238
|
+
|
|
3239
|
+
const formatJava = (format: string, args: string[]) => {
|
|
3240
|
+
const indices = { next: 0, last: -1 };
|
|
3241
|
+
const isFitting = { value: true };
|
|
3242
|
+
const text = format.replace(
|
|
3243
|
+
JAVA_FORMAT_SPECIFIER,
|
|
3244
|
+
(match, explicit?: string, flags = "", width?: string, precision?: string, conversion = "") => {
|
|
3245
|
+
const pad = (value: string) =>
|
|
3246
|
+
flags.includes("-") ? value.padEnd(Number(width)) : value.padStart(Number(width ?? 0));
|
|
3247
|
+
const isRelative = flags.includes("<");
|
|
3248
|
+
const otherFlags = flags.replace("<", "");
|
|
3249
|
+
if (conversion === "n" && match === "%n") return "\n";
|
|
3250
|
+
if (conversion === "%" && precision === undefined && /^-?$/.test(otherFlags)) {
|
|
3251
|
+
if (otherFlags === "-" && width === undefined) isFitting.value = false;
|
|
3252
|
+
return pad("%");
|
|
3253
|
+
}
|
|
3254
|
+
const index = isRelative
|
|
3255
|
+
? indices.last
|
|
3256
|
+
: explicit === undefined
|
|
3257
|
+
? indices.next++
|
|
3258
|
+
: Number(explicit.slice(0, -1)) - 1;
|
|
3259
|
+
indices.last = index;
|
|
3260
|
+
const arg = args[index];
|
|
3261
|
+
if (
|
|
3262
|
+
!"sSbBhH".includes(conversion) ||
|
|
3263
|
+
conversion === "" ||
|
|
3264
|
+
!/^-?$/.test(otherFlags) ||
|
|
3265
|
+
(otherFlags === "-" && width === undefined) ||
|
|
3266
|
+
arg === undefined
|
|
3267
|
+
) {
|
|
3268
|
+
isFitting.value = false;
|
|
3269
|
+
return match;
|
|
3270
|
+
}
|
|
3271
|
+
const lower = conversion.toLowerCase();
|
|
3272
|
+
const value =
|
|
3273
|
+
lower === "s" ? arg : lower === "b" ? "true" : (getJavaHashCode(arg) >>> 0).toString(16);
|
|
3274
|
+
const shown = pad(
|
|
3275
|
+
precision === undefined ? value : value.slice(0, Number(precision.slice(1))),
|
|
3276
|
+
);
|
|
3277
|
+
return conversion === lower ? shown : shown.toUpperCase();
|
|
3278
|
+
},
|
|
3279
|
+
);
|
|
3280
|
+
return isFitting.value ? text : undefined;
|
|
3281
|
+
};
|
|
3282
|
+
|
|
3283
|
+
// TLC prints the VIEW of a state, but not of one it prints as a difference from the previous.
|
|
3284
|
+
export const formatState = (state: Record<string, Value>, previous?: Record<string, Value>) => {
|
|
3285
|
+
const getViewText = previous ? undefined : viewTexts.get(state);
|
|
3286
|
+
if (getViewText) return getViewText();
|
|
2710
3287
|
const entries = Object.entries(state);
|
|
3288
|
+
const alias = aliasNames.get(state);
|
|
3289
|
+
const isFormatted = alias !== undefined && "_format" in state;
|
|
3290
|
+
const format = isFormatted ? state._format : entries.length === 1 ? "%s = %s\n" : "/\\ %s = %s\n";
|
|
3291
|
+
if (typeof format !== "string") {
|
|
3292
|
+
throw new TlaAliasFormatError(
|
|
3293
|
+
`The _format field of the ALIAS ${alias} is ${formatValue(format)}, but it must be a string.`,
|
|
3294
|
+
);
|
|
3295
|
+
}
|
|
2711
3296
|
return entries
|
|
2712
|
-
.
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
3297
|
+
.filter(([name]) => !(isFormatted && name === "_format"))
|
|
3298
|
+
.filter(([name, value]) => !previous || !isUnchanged(previous[name], value))
|
|
3299
|
+
.map(([name, value]) => {
|
|
3300
|
+
const text = formatJava(format, [name, prettyPrint(formatValue(value))]);
|
|
3301
|
+
if (text === undefined) {
|
|
3302
|
+
throw new TlaAliasFormatError(
|
|
3303
|
+
`The _format field of the ALIAS ${alias} is ${formatValue(format)}, but it must format two strings, the name and the value of a field.`,
|
|
3304
|
+
);
|
|
3305
|
+
}
|
|
3306
|
+
return text;
|
|
3307
|
+
})
|
|
2716
3308
|
.join("");
|
|
2717
3309
|
};
|