tla-ts 0.0.1 → 0.0.3-dev.037ac3b
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 +98 -39
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +225 -30
- package/dist/index.d.ts +225 -30
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +2 -2
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +21 -1
- package/dist/run-cli.js +21 -1
- package/dist/transfer.cjs +400 -39
- package/dist/transfer.js +400 -39
- package/package.json +6 -2
- package/src/ast.ts +213 -6
- package/src/bags.ts +161 -71
- package/src/builtins.ts +203 -23
- package/src/bundled-modules.ts +63 -32
- package/src/check.ts +1132 -258
- package/src/cli-options.ts +64 -0
- package/src/codegen.ts +58 -34
- package/src/config.ts +39 -11
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +557 -153
- package/src/errors.ts +66 -1
- package/src/evaluate.ts +553 -258
- package/src/exit-codes.ts +53 -0
- package/src/fingerprints.ts +48 -13
- package/src/index.ts +9 -4
- package/src/instances.ts +235 -79
- package/src/java-overrides.ts +241 -0
- package/src/json.ts +153 -72
- package/src/levels.ts +850 -0
- package/src/lexer.ts +108 -29
- package/src/live-check.ts +692 -0
- package/src/liveness.ts +71 -596
- package/src/obligation-action.ts +384 -0
- package/src/obligation-anon.ts +575 -0
- package/src/obligation-deref.ts +637 -0
- package/src/obligation-expression.ts +1060 -0
- package/src/obligation-format.ts +406 -0
- package/src/obligation-generate.ts +1074 -0
- package/src/obligation-levels.ts +329 -0
- package/src/obligation-module.ts +1042 -0
- package/src/obligation-printer.ts +889 -0
- package/src/obligation-proof.ts +76 -0
- package/src/obligation-source.ts +1084 -0
- package/src/obligation-tuples.ts +184 -0
- package/src/obligations.ts +316 -0
- package/src/parallel-worker.ts +186 -201
- package/src/parallel.ts +179 -84
- package/src/parser.ts +970 -206
- package/src/pretty-print.ts +259 -0
- package/src/randomization.ts +24 -113
- package/src/resolve.ts +483 -51
- package/src/run-cli.ts +473 -62
- package/src/runtime.ts +65 -28
- package/src/spec-description.ts +521 -0
- package/src/standard-levels.ts +105 -0
- package/src/standard-modules.ts +218 -92
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +31 -45
- package/src/substitution-levels.ts +184 -0
- package/src/successors.ts +336 -57
- package/src/symmetry.ts +79 -23
- package/src/tableau.ts +93 -60
- package/src/tlc-ext.ts +148 -44
- package/src/tlc-module.ts +71 -66
- package/src/tlc-modules.ts +1022 -0
- package/src/tlc-order.ts +511 -0
- package/src/tlc-random.ts +414 -0
- package/src/tlc-summary.ts +124 -0
- package/src/transfer.ts +26 -5
- package/src/values.ts +1285 -179
package/src/values.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import { TlaEvaluationError } from "./errors";
|
|
1
|
+
import { TlaEvaluationError, TlaModuleError } from "./errors";
|
|
2
|
+
import { prettyPrint } from "./pretty-print";
|
|
2
3
|
|
|
3
4
|
export interface ModelValue {
|
|
4
5
|
kind: "ModelValue";
|
|
5
6
|
name: string;
|
|
7
|
+
type: string | undefined;
|
|
8
|
+
rank: number;
|
|
6
9
|
}
|
|
7
10
|
|
|
8
11
|
export interface SetValue {
|
|
@@ -12,12 +15,20 @@ export interface SetValue {
|
|
|
12
15
|
fingerprint: number;
|
|
13
16
|
}
|
|
14
17
|
|
|
18
|
+
// A set enumeration whose elements TLC can't sort, such as {1, "a"}. TLC sorts a set enumeration
|
|
19
|
+
// only when it has to, so it still checks membership in one element by element.
|
|
20
|
+
export interface MixedSetValue {
|
|
21
|
+
kind: "MixedSet";
|
|
22
|
+
elements: Value[];
|
|
23
|
+
}
|
|
24
|
+
|
|
15
25
|
export interface FunctionValue {
|
|
16
26
|
kind: "Function";
|
|
17
27
|
domain: Value[];
|
|
18
28
|
range: Value[];
|
|
19
29
|
hash: number;
|
|
20
30
|
fingerprint: number;
|
|
31
|
+
writtenKeys: Value[] | undefined;
|
|
21
32
|
}
|
|
22
33
|
|
|
23
34
|
export interface IntervalValue {
|
|
@@ -71,10 +82,27 @@ export interface FilteredSetValue {
|
|
|
71
82
|
test: (element: Value) => boolean;
|
|
72
83
|
}
|
|
73
84
|
|
|
85
|
+
// UNION S when some element of S isn't a set enumeration. TLC enumerates it only when it has to.
|
|
86
|
+
export interface UnionValue {
|
|
87
|
+
kind: "Union";
|
|
88
|
+
base: Value;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
interface LazyFunctionParameter {
|
|
92
|
+
name: string;
|
|
93
|
+
domain: Value;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface LazyFunctionSource {
|
|
97
|
+
parameters: LazyFunctionParameter[];
|
|
98
|
+
body: string;
|
|
99
|
+
}
|
|
100
|
+
|
|
74
101
|
export interface LazyFunctionValue {
|
|
75
102
|
kind: "LazyFunction";
|
|
76
103
|
domain: Value;
|
|
77
104
|
apply: (argument: Value) => Value;
|
|
105
|
+
source?: LazyFunctionSource;
|
|
78
106
|
}
|
|
79
107
|
|
|
80
108
|
export type Value =
|
|
@@ -83,6 +111,7 @@ export type Value =
|
|
|
83
111
|
| string
|
|
84
112
|
| ModelValue
|
|
85
113
|
| SetValue
|
|
114
|
+
| MixedSetValue
|
|
86
115
|
| FunctionValue
|
|
87
116
|
| IntervalValue
|
|
88
117
|
| InfiniteSetValue
|
|
@@ -93,12 +122,13 @@ export type Value =
|
|
|
93
122
|
| SequenceSetValue
|
|
94
123
|
| SetOperationValue
|
|
95
124
|
| FilteredSetValue
|
|
125
|
+
| UnionValue
|
|
96
126
|
| LazyFunctionValue;
|
|
97
127
|
|
|
98
128
|
export type ObjectValue = Exclude<Value, boolean | number | string>;
|
|
99
129
|
|
|
100
|
-
const MAXIMUM_INTEGER = 2147483647;
|
|
101
|
-
const MINIMUM_INTEGER = -2147483648;
|
|
130
|
+
export const MAXIMUM_INTEGER = 2147483647;
|
|
131
|
+
export const MINIMUM_INTEGER = -2147483648;
|
|
102
132
|
const MAXIMUM_ENUMERATED_SUBSET_BASE = 30;
|
|
103
133
|
|
|
104
134
|
export const isSetValue = (value: Value) =>
|
|
@@ -107,13 +137,20 @@ export const isSetValue = (value: Value) =>
|
|
|
107
137
|
value.kind !== "Function" &&
|
|
108
138
|
value.kind !== "LazyFunction";
|
|
109
139
|
|
|
140
|
+
export type ReducibleSet = SetValue | MixedSetValue | IntervalValue;
|
|
141
|
+
|
|
142
|
+
// TLC reduces a set enumeration or an interval eagerly, and keeps every other set lazy.
|
|
143
|
+
export const isReducible = (value: Value): value is ReducibleSet =>
|
|
144
|
+
typeof value === "object" &&
|
|
145
|
+
(value.kind === "Set" || value.kind === "MixedSet" || value.kind === "Interval");
|
|
146
|
+
|
|
110
147
|
export const isFunctionValue = (value: Value): value is FunctionValue | LazyFunctionValue =>
|
|
111
148
|
typeof value === "object" && (value.kind === "Function" || value.kind === "LazyFunction");
|
|
112
149
|
|
|
113
150
|
export const isModelValue = (value: Value): value is ModelValue =>
|
|
114
151
|
typeof value === "object" && value.kind === "ModelValue";
|
|
115
152
|
|
|
116
|
-
const describeKind = (value: Value) => {
|
|
153
|
+
export const describeKind = (value: Value) => {
|
|
117
154
|
if (typeof value === "boolean") return "boolean";
|
|
118
155
|
if (typeof value === "number") return "integer";
|
|
119
156
|
if (typeof value === "string") return "string";
|
|
@@ -123,15 +160,59 @@ const describeKind = (value: Value) => {
|
|
|
123
160
|
|
|
124
161
|
export const checkInteger = (value: number) => {
|
|
125
162
|
if (!Number.isInteger(value) || value > MAXIMUM_INTEGER || value < MINIMUM_INTEGER) {
|
|
126
|
-
throw new
|
|
163
|
+
throw new TlaModuleError(`Integer overflow: ${value} is outside TLC's 32-bit range`);
|
|
127
164
|
}
|
|
128
165
|
return value;
|
|
129
166
|
};
|
|
130
167
|
|
|
131
|
-
|
|
168
|
+
// TLC keeps a record, or a function with string keys that @@ builds, in the order the spec writes
|
|
169
|
+
// it until it sorts the value to compare or fingerprint it. ToString, Assert, and error messages
|
|
170
|
+
// show that order.
|
|
171
|
+
const writtenOrderUse = { isUsed: false };
|
|
172
|
+
|
|
173
|
+
const setWrittenOrder = (functionValue: FunctionValue, keys: Value[]) => {
|
|
174
|
+
writtenOrderUse.isUsed = true;
|
|
175
|
+
functionValue.writtenKeys = keys;
|
|
176
|
+
return functionValue;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
export const getWrittenPairs = (functionValue: FunctionValue): [Value, Value][] => {
|
|
180
|
+
const keys = functionValue.writtenKeys ?? functionValue.domain;
|
|
181
|
+
return keys.map((key) => [key, functionValue.range[findIndex(functionValue.domain, key)]]);
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
export const createWrittenRecord = (names: Value[], range: Value[], writtenNames: Value[]) =>
|
|
185
|
+
setWrittenOrder(createSortedFunction(names, range), writtenNames);
|
|
186
|
+
|
|
187
|
+
// TLC sorts a value it compares even with itself, and every value inside it, so a value it has
|
|
188
|
+
// hashed or fingerprinted is already sorted.
|
|
189
|
+
const forgetWrittenOrder = (value: Value): void => {
|
|
190
|
+
if (typeof value !== "object" || (value.kind !== "Function" && value.kind !== "Set")) return;
|
|
191
|
+
if (value.hash !== 0 || value.fingerprint !== 0) return;
|
|
192
|
+
if (value.kind === "Set") return value.elements.forEach(forgetWrittenOrder);
|
|
193
|
+
value.writtenKeys = undefined;
|
|
194
|
+
value.range.forEach(forgetWrittenOrder);
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const isRecordDomain = (domain: Value[]) => domain.every((key) => typeof key === "string");
|
|
198
|
+
|
|
199
|
+
// TLC compares a record only with another record, so comparing one with a function of another size
|
|
200
|
+
// can fail before the sizes decide.
|
|
201
|
+
const isRecordMismatch = (left: Value[], right: Value[]) =>
|
|
202
|
+
left.length > 0 && right.length > 0 && isRecordDomain(left) !== isRecordDomain(right);
|
|
203
|
+
|
|
204
|
+
const compareFunctions = (left: FunctionValue, right: FunctionValue) => {
|
|
205
|
+
if (writtenOrderUse.isUsed) {
|
|
206
|
+
left.writtenKeys = undefined;
|
|
207
|
+
right.writtenKeys = undefined;
|
|
208
|
+
}
|
|
132
209
|
const leftDomain = left.domain;
|
|
133
210
|
const rightDomain = right.domain;
|
|
134
|
-
if (leftDomain.length !== rightDomain.length)
|
|
211
|
+
if (leftDomain.length !== rightDomain.length) {
|
|
212
|
+
return isRecordMismatch(leftDomain, rightDomain)
|
|
213
|
+
? compareLikeTlc(left, right)
|
|
214
|
+
: leftDomain.length - rightDomain.length;
|
|
215
|
+
}
|
|
135
216
|
if (leftDomain !== rightDomain) {
|
|
136
217
|
for (let index = 0; index < leftDomain.length; index++) {
|
|
137
218
|
const keyComparison = compareValues(leftDomain[index], rightDomain[index]);
|
|
@@ -148,7 +229,7 @@ const compareFunctions = (left: FunctionValue, right: FunctionValue): number =>
|
|
|
148
229
|
return 0;
|
|
149
230
|
};
|
|
150
231
|
|
|
151
|
-
const compareSets = (left: SetValue, right: SetValue)
|
|
232
|
+
const compareSets = (left: SetValue, right: SetValue) => {
|
|
152
233
|
const leftElements = left.elements;
|
|
153
234
|
const rightElements = right.elements;
|
|
154
235
|
if (leftElements.length !== rightElements.length)
|
|
@@ -161,8 +242,7 @@ const compareSets = (left: SetValue, right: SetValue): number => {
|
|
|
161
242
|
return 0;
|
|
162
243
|
};
|
|
163
244
|
|
|
164
|
-
const getModelValueType = (value: Value) =>
|
|
165
|
-
isModelValue(value) && value.name.length > 2 && value.name[1] === "_" ? value.name[0] : undefined;
|
|
245
|
+
const getModelValueType = (value: Value) => (isModelValue(value) ? value.type : undefined);
|
|
166
246
|
|
|
167
247
|
const checkModelValueTypes = (left: Value, right: Value, isEquality: boolean) => {
|
|
168
248
|
const leftType = getModelValueType(left);
|
|
@@ -185,41 +265,124 @@ const checkModelValueTypes = (left: Value, right: Value, isEquality: boolean) =>
|
|
|
185
265
|
);
|
|
186
266
|
};
|
|
187
267
|
|
|
188
|
-
|
|
268
|
+
// TLC orders strings and model value names by when it first interned the text, so every
|
|
269
|
+
// check sets the order of its spec's strings before it builds any value.
|
|
270
|
+
const stringOrder = { ranks: Object.create(null) as Record<string, number>, size: 0 };
|
|
271
|
+
|
|
272
|
+
export const setStringOrder = (texts: Iterable<string>) => {
|
|
273
|
+
stringOrder.ranks = Object.create(null);
|
|
274
|
+
stringOrder.size = 0;
|
|
275
|
+
modelValues.clear();
|
|
276
|
+
for (const text of texts) rankString(text);
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
export const rankString = (text: string) => {
|
|
280
|
+
if (stringOrder.ranks[text] === undefined) stringOrder.ranks[text] = stringOrder.size++;
|
|
281
|
+
return text;
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
export const getRankedStringCount = () => stringOrder.size;
|
|
285
|
+
|
|
286
|
+
export const getStringRank = (text: string) => stringOrder.ranks[rankString(text)];
|
|
287
|
+
|
|
288
|
+
const compareStrings = (left: string, right: string) => {
|
|
189
289
|
if (left === right) return 0;
|
|
290
|
+
const ranks = stringOrder.ranks;
|
|
291
|
+
const leftRank = ranks[left];
|
|
292
|
+
const rightRank = ranks[right];
|
|
293
|
+
if (leftRank !== undefined && rightRank !== undefined) return leftRank - rightRank;
|
|
294
|
+
if (leftRank !== undefined) return -1;
|
|
295
|
+
if (rightRank !== undefined) return 1;
|
|
296
|
+
return left < right ? -1 : 1;
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
export const isOverriddenSet = (value: Value): value is InfiniteSetValue | SequenceSetValue =>
|
|
300
|
+
typeof value === "object" && (value.kind === "InfiniteSet" || value.kind === "SequenceSet");
|
|
301
|
+
|
|
302
|
+
const isOverriddenComparison = (left: Value, right: Value) =>
|
|
303
|
+
(isOverriddenSet(left) || isOverriddenSet(right)) &&
|
|
304
|
+
!(
|
|
305
|
+
typeof left === "object" &&
|
|
306
|
+
left.kind === "SequenceSet" &&
|
|
307
|
+
typeof right === "object" &&
|
|
308
|
+
right.kind === "SequenceSet"
|
|
309
|
+
);
|
|
310
|
+
|
|
311
|
+
// TLC implements Nat, Int, STRING, Seq(S), and Any in Java classes that compare only with each
|
|
312
|
+
// other and with model values. Comparing one with any other set is an error, worded by the class of
|
|
313
|
+
// the left operand.
|
|
314
|
+
const compareOverriddenSets = (
|
|
315
|
+
left: Value,
|
|
316
|
+
right: Value,
|
|
317
|
+
verb: "compare" | "check equality of",
|
|
318
|
+
) => {
|
|
319
|
+
if (!isOverriddenSet(left)) {
|
|
320
|
+
throw new TlaEvaluationError(
|
|
321
|
+
`Attempted to ${verb} the set ${formatValue(isEnumerable(left) ? toFiniteSet(left) : left, true)} with the value:\n${formatValue(right, true)}`,
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
if (!isOverriddenSet(right)) {
|
|
325
|
+
throw new TlaEvaluationError(
|
|
326
|
+
`Attempted to compare overridden value ${formatValue(left, true)} with non-overridden value:\n${formatValue(right, true)}`,
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
const leftName = left.kind === "InfiniteSet" ? left.name : undefined;
|
|
330
|
+
const rightName = right.kind === "InfiniteSet" ? right.name : undefined;
|
|
331
|
+
if (leftName !== undefined && leftName === rightName) return 0;
|
|
332
|
+
if ((leftName === "Nat" && rightName === "Int") || (leftName === "Int" && rightName === "Nat")) {
|
|
333
|
+
return leftName === "Nat" ? -1 : 1;
|
|
334
|
+
}
|
|
335
|
+
throw new TlaEvaluationError(
|
|
336
|
+
`Attempted to compare ${formatValue(left, true)} with the value\n${formatValue(right, true)}`,
|
|
337
|
+
);
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
export const ANY_SET: InfiniteSetValue = { kind: "InfiniteSet", name: "Any" };
|
|
341
|
+
|
|
342
|
+
const compareAny = (other: Value): never => {
|
|
343
|
+
throw new TlaEvaluationError(`Attempted to compare ANY with the value\n${formatValue(other)}`);
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
export const compareValues = (left: Value, right: Value): number => {
|
|
347
|
+
if (left === right) {
|
|
348
|
+
if (writtenOrderUse.isUsed) forgetWrittenOrder(left);
|
|
349
|
+
return left === ANY_SET ? compareAny(left) : 0;
|
|
350
|
+
}
|
|
190
351
|
if (typeof left === "object" && typeof right === "object") {
|
|
191
352
|
const leftKind = left.kind;
|
|
192
353
|
const rightKind = right.kind;
|
|
193
354
|
if (leftKind === "Function" && rightKind === "Function") return compareFunctions(left, right);
|
|
194
355
|
if (leftKind === "Set" && rightKind === "Set") return compareSets(left, right);
|
|
356
|
+
if (leftKind === "Interval" && rightKind === "Interval") {
|
|
357
|
+
const size = Math.max(0, left.high - left.low + 1);
|
|
358
|
+
const otherSize = Math.max(0, right.high - right.low + 1);
|
|
359
|
+
return size !== otherSize || size === 0 ? size - otherSize : left.low - right.low;
|
|
360
|
+
}
|
|
361
|
+
if (leftKind === "ModelValue" && rightKind === "ModelValue") {
|
|
362
|
+
if (left.type !== undefined && right.type !== undefined) {
|
|
363
|
+
checkModelValueTypes(left, right, false);
|
|
364
|
+
}
|
|
365
|
+
return left.rank - right.rank;
|
|
366
|
+
}
|
|
195
367
|
if (leftKind === "ModelValue" || rightKind === "ModelValue") {
|
|
196
368
|
checkModelValueTypes(left, right, false);
|
|
197
|
-
if (leftKind === "ModelValue" && rightKind === "ModelValue") {
|
|
198
|
-
return left.name < right.name ? -1 : left.name > right.name ? 1 : 0;
|
|
199
|
-
}
|
|
200
369
|
return leftKind === "ModelValue" ? -1 : 1;
|
|
201
370
|
}
|
|
202
|
-
|
|
203
|
-
if (
|
|
204
|
-
|
|
205
|
-
`Attempted to compare ANY with ${formatValue(isAny(left) ? right : left)}`,
|
|
206
|
-
);
|
|
207
|
-
}
|
|
208
|
-
if (leftKind === "InfiniteSet" && rightKind === "InfiniteSet") {
|
|
209
|
-
if (left.name === right.name) return 0;
|
|
210
|
-
if (left.name !== "STRING" && right.name !== "STRING") return left.name === "Nat" ? -1 : 1;
|
|
371
|
+
if (left === ANY_SET || right === ANY_SET) return compareAny(left === ANY_SET ? right : left);
|
|
372
|
+
if (isSetValue(left) && isSetValue(right) && isOverriddenComparison(left, right)) {
|
|
373
|
+
return compareOverriddenSets(left, right, "compare");
|
|
211
374
|
}
|
|
212
|
-
const
|
|
213
|
-
if (
|
|
375
|
+
const isLeftSet = isSetValue(left);
|
|
376
|
+
if (isLeftSet !== isSetValue(right)) {
|
|
214
377
|
throw new TlaEvaluationError(
|
|
215
378
|
`Attempted to compare ${describeKind(left)} ${formatValue(left)} with ${describeKind(right)} ${formatValue(right)}`,
|
|
216
379
|
);
|
|
217
380
|
}
|
|
218
|
-
if (
|
|
381
|
+
if (isLeftSet) return compareSets(toFiniteSet(left), toComparedSet(right));
|
|
219
382
|
return compareFunctions(toFiniteFunction(left), toFiniteFunction(right));
|
|
220
383
|
}
|
|
221
384
|
if (typeof left === "number" && typeof right === "number") return left - right;
|
|
222
|
-
if (typeof left === "string" && typeof right === "string") return left
|
|
385
|
+
if (typeof left === "string" && typeof right === "string") return compareStrings(left, right);
|
|
223
386
|
if (typeof left === "boolean" && typeof right === "boolean") return left < right ? -1 : 1;
|
|
224
387
|
checkModelValueTypes(left, right, false);
|
|
225
388
|
if (isModelValue(left)) return -1;
|
|
@@ -231,10 +394,16 @@ export const compareValues = (left: Value, right: Value): number => {
|
|
|
231
394
|
|
|
232
395
|
const modelValues = new Map<string, ModelValue>();
|
|
233
396
|
|
|
397
|
+
// A check ranks every model value's name when it creates the value, and creates its own values.
|
|
234
398
|
export const createModelValue = (name: string): ModelValue => {
|
|
235
399
|
const known = modelValues.get(name);
|
|
236
400
|
if (known) return known;
|
|
237
|
-
const value: ModelValue = {
|
|
401
|
+
const value: ModelValue = {
|
|
402
|
+
kind: "ModelValue",
|
|
403
|
+
name,
|
|
404
|
+
type: name.length > 2 && name[1] === "_" ? name[0] : undefined,
|
|
405
|
+
rank: getStringRank(name),
|
|
406
|
+
};
|
|
238
407
|
modelValues.set(name, value);
|
|
239
408
|
return value;
|
|
240
409
|
};
|
|
@@ -252,10 +421,14 @@ export const createSortedFunction = (domain: Value[], range: Value[]): FunctionV
|
|
|
252
421
|
range,
|
|
253
422
|
hash: 0,
|
|
254
423
|
fingerprint: 0,
|
|
424
|
+
writtenKeys: undefined,
|
|
255
425
|
});
|
|
256
426
|
|
|
257
427
|
export const valuesEqual = (left: Value, right: Value): boolean => {
|
|
258
|
-
if (left === right)
|
|
428
|
+
if (left === right) {
|
|
429
|
+
if (writtenOrderUse.isUsed) forgetWrittenOrder(left);
|
|
430
|
+
return left !== ANY_SET || compareAny(left);
|
|
431
|
+
}
|
|
259
432
|
if (typeof left !== "object" || typeof right !== "object") {
|
|
260
433
|
if (typeof left === typeof right) return false;
|
|
261
434
|
if (typeof left === "object" || typeof right === "object") {
|
|
@@ -276,9 +449,52 @@ export const valuesEqual = (left: Value, right: Value): boolean => {
|
|
|
276
449
|
checkModelValueTypes(left, right, true);
|
|
277
450
|
return isModelValue(left) && isModelValue(right) && left.name === right.name;
|
|
278
451
|
}
|
|
452
|
+
if (left.kind === "Subset" && right.kind === "Subset") return valuesEqual(left.base, right.base);
|
|
453
|
+
if (left.kind === "FunctionSet" && right.kind === "FunctionSet") {
|
|
454
|
+
const isUnit = isEmptySet(left.domain);
|
|
455
|
+
const isOtherUnit = isEmptySet(right.domain);
|
|
456
|
+
if (isUnit || isOtherUnit) return isUnit === isOtherUnit;
|
|
457
|
+
const isEmpty = isEmptySet(left.range);
|
|
458
|
+
const isOtherEmpty = isEmptySet(right.range);
|
|
459
|
+
if (isEmpty || isOtherEmpty) return isEmpty === isOtherEmpty;
|
|
460
|
+
return valuesEqual(left.domain, right.domain) && valuesEqual(left.range, right.range);
|
|
461
|
+
}
|
|
462
|
+
if (left.kind === "RecordSet" && right.kind === "RecordSet") {
|
|
463
|
+
if (isEmptySet(left)) return isEmptySet(right);
|
|
464
|
+
if (isEmptySet(right)) return false;
|
|
465
|
+
const getFields = (set: RecordSetValue) =>
|
|
466
|
+
set.names
|
|
467
|
+
.map((name, index): [string, Value] => [name, set.sets[index]])
|
|
468
|
+
.sort((first, second) => (first[0] < second[0] ? -1 : 1));
|
|
469
|
+
const otherFields = getFields(right);
|
|
470
|
+
return (
|
|
471
|
+
left.names.length === right.names.length &&
|
|
472
|
+
getFields(left).every(
|
|
473
|
+
(field, index) =>
|
|
474
|
+
field[0] === otherFields[index][0] && valuesEqual(field[1], otherFields[index][1]),
|
|
475
|
+
)
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
if (left.kind === "ProductSet" && right.kind === "ProductSet") {
|
|
479
|
+
if (isEmptySet(left)) return isEmptySet(right);
|
|
480
|
+
if (isEmptySet(right)) return false;
|
|
481
|
+
return (
|
|
482
|
+
left.factors.length === right.factors.length &&
|
|
483
|
+
left.factors.every((factor, index) => valuesEqual(factor, right.factors[index]))
|
|
484
|
+
);
|
|
485
|
+
}
|
|
279
486
|
const isInfiniteComparison =
|
|
280
487
|
isSetValue(left) && isSetValue(right) && !(isEnumerable(left) && isEnumerable(right));
|
|
281
|
-
if (isInfiniteComparison
|
|
488
|
+
if (isInfiniteComparison && isOverriddenComparison(left, right)) {
|
|
489
|
+
return compareOverriddenSets(left, right, "check equality of") === 0;
|
|
490
|
+
}
|
|
491
|
+
if (isInfiniteComparison) {
|
|
492
|
+
if (!isOverriddenSet(left)) {
|
|
493
|
+
const elements = toFiniteSet(left);
|
|
494
|
+
if (!isOverriddenSet(right)) return compareSets(elements, toFiniteSet(right)) === 0;
|
|
495
|
+
}
|
|
496
|
+
return getValueKey(left) === getValueKey(right);
|
|
497
|
+
}
|
|
282
498
|
return compareValues(left, right) === 0;
|
|
283
499
|
};
|
|
284
500
|
|
|
@@ -304,9 +520,362 @@ export const isSameValue = (left: Value, right: Value): boolean => {
|
|
|
304
520
|
return getValueKey(left) === getValueKey(right);
|
|
305
521
|
};
|
|
306
522
|
|
|
523
|
+
// The comparisons above fail with their own messages. These follow the compareTo and equals
|
|
524
|
+
// methods of TLC's Java value classes, which word a failure by the class of the left operand, so a
|
|
525
|
+
// failed comparison runs again through them to fail as TLC does.
|
|
526
|
+
interface TlcFunction {
|
|
527
|
+
domain: Value[];
|
|
528
|
+
range: Value[];
|
|
529
|
+
low: number | undefined;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
const getIntervalLow = (domain: Value[]) => {
|
|
533
|
+
const first = domain.length === 0 ? 1 : domain[0];
|
|
534
|
+
return typeof first === "number" && domain.every((key, index) => key === first + index)
|
|
535
|
+
? first
|
|
536
|
+
: undefined;
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
const toTlcFunction = (value: Value) => {
|
|
540
|
+
if (!isFunctionValue(value)) return undefined;
|
|
541
|
+
const functionValue = toFiniteFunction(value);
|
|
542
|
+
return {
|
|
543
|
+
domain: functionValue.domain,
|
|
544
|
+
range: functionValue.range,
|
|
545
|
+
low: getIntervalLow(functionValue.domain),
|
|
546
|
+
};
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
const toTlcRecord = (value: Value) => {
|
|
550
|
+
if (!isFunctionValue(value)) return undefined;
|
|
551
|
+
const functionValue = toFiniteFunction(value);
|
|
552
|
+
return functionValue.domain.every((key) => typeof key === "string") ? functionValue : undefined;
|
|
553
|
+
};
|
|
554
|
+
|
|
555
|
+
const compareModelValueWith = (modelValue: ModelValue, other: Value) => {
|
|
556
|
+
if (getModelValueType(modelValue) !== undefined) {
|
|
557
|
+
throw new TlaEvaluationError(
|
|
558
|
+
`Attempted to compare the typed model value ${formatValue(modelValue)} and the non-model value\n${formatValue(other)}`,
|
|
559
|
+
);
|
|
560
|
+
}
|
|
561
|
+
return 1;
|
|
562
|
+
};
|
|
563
|
+
|
|
564
|
+
const equalsModelValueWith = (modelValue: ModelValue, other: Value) => {
|
|
565
|
+
if (getModelValueType(modelValue) !== undefined) {
|
|
566
|
+
throw new TlaEvaluationError(
|
|
567
|
+
`Attempted to check equality of the typed model value ${formatValue(modelValue)} and the non-model value\n${formatValue(other)}`,
|
|
568
|
+
);
|
|
569
|
+
}
|
|
570
|
+
return false;
|
|
571
|
+
};
|
|
572
|
+
|
|
573
|
+
const compareModelValue = (left: ModelValue, right: Value) => {
|
|
574
|
+
const type = getModelValueType(left);
|
|
575
|
+
if (type === undefined) return isModelValue(right) ? left.rank - right.rank : -1;
|
|
576
|
+
if (!isModelValue(right)) {
|
|
577
|
+
throw new TlaEvaluationError(
|
|
578
|
+
`Attempted to compare the typed model value ${formatValue(left)} and non-model value\n${formatValue(right)}`,
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
const otherType = getModelValueType(right);
|
|
582
|
+
if (otherType !== undefined && otherType !== type) {
|
|
583
|
+
throw new TlaEvaluationError(
|
|
584
|
+
`Attempted to compare the differently-typed model values ${formatValue(left)} and ${formatValue(right)}`,
|
|
585
|
+
);
|
|
586
|
+
}
|
|
587
|
+
return left.rank - right.rank;
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
const equalsModelValue = (left: ModelValue, right: Value) => {
|
|
591
|
+
const type = getModelValueType(left);
|
|
592
|
+
if (type === undefined) return isModelValue(right) && left.name === right.name;
|
|
593
|
+
if (!isModelValue(right)) {
|
|
594
|
+
throw new TlaEvaluationError(
|
|
595
|
+
`Attempted to check equality of typed model value ${formatValue(left)} and non-model value\n${formatValue(right)}`,
|
|
596
|
+
);
|
|
597
|
+
}
|
|
598
|
+
const otherType = getModelValueType(right);
|
|
599
|
+
if (otherType !== undefined && otherType !== type) {
|
|
600
|
+
throw new TlaEvaluationError(
|
|
601
|
+
`Attempted to check equality of the differently-typed model values ${formatValue(left)} and ${formatValue(right)}`,
|
|
602
|
+
);
|
|
603
|
+
}
|
|
604
|
+
return left.name === right.name;
|
|
605
|
+
};
|
|
606
|
+
|
|
607
|
+
const SCALAR_KINDS = {
|
|
608
|
+
number: { name: "integer", other: "non-integer", equality: "check equality of" },
|
|
609
|
+
string: { name: "string", other: "non-string", equality: "check equality of" },
|
|
610
|
+
boolean: { name: "boolean", other: "non-boolean", equality: "compare equality of" },
|
|
611
|
+
};
|
|
612
|
+
|
|
613
|
+
const describeScalarMismatch = (
|
|
614
|
+
left: number | string | boolean,
|
|
615
|
+
right: Value,
|
|
616
|
+
isEquality: boolean,
|
|
617
|
+
) => {
|
|
618
|
+
const kind =
|
|
619
|
+
SCALAR_KINDS[
|
|
620
|
+
typeof left === "number" ? "number" : typeof left === "string" ? "string" : "boolean"
|
|
621
|
+
];
|
|
622
|
+
return `Attempted to ${isEquality ? kind.equality : "compare"} ${kind.name} ${formatValue(left)} with ${kind.other}:\n${formatValue(right)}`;
|
|
623
|
+
};
|
|
624
|
+
|
|
625
|
+
const compareTlcFunctions = (functionValue: TlcFunction, left: Value, right: Value) => {
|
|
626
|
+
const other = toTlcFunction(right);
|
|
627
|
+
if (!other) {
|
|
628
|
+
if (isModelValue(right)) return compareModelValueWith(right, left);
|
|
629
|
+
throw new TlaEvaluationError(
|
|
630
|
+
`Attempted to compare the function ${formatValue(left)} with the value:\n${formatValue(right)}`,
|
|
631
|
+
);
|
|
632
|
+
}
|
|
633
|
+
if (functionValue.range.length !== other.range.length)
|
|
634
|
+
return functionValue.range.length - other.range.length;
|
|
635
|
+
const compareRanges = () => compareElementsLikeTlc(functionValue.range, other.range);
|
|
636
|
+
if (functionValue.low !== undefined && functionValue.range.length === 0) return 0;
|
|
637
|
+
if (functionValue.low !== undefined && other.low !== undefined) {
|
|
638
|
+
return functionValue.low === other.low ? compareRanges() : functionValue.low - other.low;
|
|
639
|
+
}
|
|
640
|
+
const intervalLow = functionValue.low ?? other.low;
|
|
641
|
+
if (intervalLow === undefined) {
|
|
642
|
+
return compareElementsLikeTlc(functionValue.domain, other.domain) || compareRanges();
|
|
643
|
+
}
|
|
644
|
+
const keys = functionValue.low === undefined ? functionValue.domain : other.domain;
|
|
645
|
+
for (let index = 0; index < keys.length; index++) {
|
|
646
|
+
const key = keys[index];
|
|
647
|
+
if (typeof key !== "number") {
|
|
648
|
+
throw new TlaEvaluationError(
|
|
649
|
+
`Attempted to compare integer with non-integer${functionValue.low === undefined ? "" : ":"}\n${formatValue(key)}.`,
|
|
650
|
+
);
|
|
651
|
+
}
|
|
652
|
+
const difference =
|
|
653
|
+
functionValue.low === undefined ? key - (intervalLow + index) : intervalLow + index - key;
|
|
654
|
+
if (difference !== 0) return difference;
|
|
655
|
+
}
|
|
656
|
+
return compareRanges();
|
|
657
|
+
};
|
|
658
|
+
|
|
659
|
+
const compareElementsLikeTlc = (left: Value[], right: Value[]) => {
|
|
660
|
+
for (let index = 0; index < left.length; index++) {
|
|
661
|
+
const comparison = compareLikeTlc(left[index], right[index]);
|
|
662
|
+
if (comparison !== 0) return comparison;
|
|
663
|
+
}
|
|
664
|
+
return 0;
|
|
665
|
+
};
|
|
666
|
+
|
|
667
|
+
const compareFunctionLikeTlc = (left: FunctionValue | LazyFunctionValue, right: Value) => {
|
|
668
|
+
const functionValue = toFiniteFunction(left);
|
|
669
|
+
const isTuple = left.kind === "Function" && getSequenceElements(functionValue) !== undefined;
|
|
670
|
+
if (isTuple) {
|
|
671
|
+
const tuple = isFunctionValue(right) ? getSequenceElements(right) : undefined;
|
|
672
|
+
if (!tuple) return compareTlcFunctions({ ...functionValue, low: 1 }, left, right);
|
|
673
|
+
return (
|
|
674
|
+
functionValue.range.length - tuple.length ||
|
|
675
|
+
compareElementsLikeTlc(functionValue.range, tuple)
|
|
676
|
+
);
|
|
677
|
+
}
|
|
678
|
+
if (left.kind === "Function" && functionValue.domain.every((key) => typeof key === "string")) {
|
|
679
|
+
const record = toTlcRecord(right);
|
|
680
|
+
if (!record) {
|
|
681
|
+
if (isModelValue(right)) return compareModelValueWith(right, left);
|
|
682
|
+
throw new TlaEvaluationError(
|
|
683
|
+
`Attempted to compare record:\n${formatValue(left)}\nwith non-record\n${formatValue(right)}`,
|
|
684
|
+
);
|
|
685
|
+
}
|
|
686
|
+
return (
|
|
687
|
+
functionValue.domain.length - record.domain.length ||
|
|
688
|
+
compareElementsLikeTlc(functionValue.domain, record.domain) ||
|
|
689
|
+
compareElementsLikeTlc(functionValue.range, record.range)
|
|
690
|
+
);
|
|
691
|
+
}
|
|
692
|
+
return compareTlcFunctions(
|
|
693
|
+
{ ...functionValue, low: getIntervalLow(functionValue.domain) },
|
|
694
|
+
left,
|
|
695
|
+
right,
|
|
696
|
+
);
|
|
697
|
+
};
|
|
698
|
+
|
|
699
|
+
export const isEnumeratedSet = (value: Value): value is SetValue | MixedSetValue =>
|
|
700
|
+
typeof value === "object" && (value.kind === "Set" || value.kind === "MixedSet");
|
|
701
|
+
|
|
702
|
+
// TLC compares a lazy set, or a set it overrides in Java, through its own class before it compares
|
|
703
|
+
// elements. The comparisons above already fail as those classes do.
|
|
704
|
+
const isComparedByClass = (left: ObjectValue, right: Value) =>
|
|
705
|
+
isOverriddenSet(left) ||
|
|
706
|
+
isOverriddenSet(right) ||
|
|
707
|
+
(typeof right === "object" &&
|
|
708
|
+
left.kind === right.kind &&
|
|
709
|
+
(left.kind === "Interval" ||
|
|
710
|
+
left.kind === "Subset" ||
|
|
711
|
+
left.kind === "FunctionSet" ||
|
|
712
|
+
left.kind === "RecordSet" ||
|
|
713
|
+
left.kind === "ProductSet"));
|
|
714
|
+
|
|
715
|
+
const getSetElementsLikeTlc = (left: SetValue | MixedSetValue, right: Value) => {
|
|
716
|
+
const other = isEnumeratedSet(right) ? right : toFiniteSet(right);
|
|
717
|
+
return { elements: toFiniteSet(left).elements, otherElements: toFiniteSet(other).elements };
|
|
718
|
+
};
|
|
719
|
+
|
|
720
|
+
const compareSetLikeTlc = (left: ObjectValue, right: Value): number => {
|
|
721
|
+
if (isComparedByClass(left, right)) return compareValues(left, right);
|
|
722
|
+
if (!isEnumeratedSet(left)) return compareSetLikeTlc(toFiniteSet(left), right);
|
|
723
|
+
if (!isSetValue(right)) {
|
|
724
|
+
if (isModelValue(right)) return compareModelValueWith(right, left);
|
|
725
|
+
throw new TlaEvaluationError(
|
|
726
|
+
`Attempted to compare the set ${formatValue(left)} with the value:\n${formatValue(right)}`,
|
|
727
|
+
);
|
|
728
|
+
}
|
|
729
|
+
const sets = getSetElementsLikeTlc(left, right);
|
|
730
|
+
return (
|
|
731
|
+
sets.elements.length - sets.otherElements.length ||
|
|
732
|
+
compareElementsLikeTlc(sets.elements, sets.otherElements)
|
|
733
|
+
);
|
|
734
|
+
};
|
|
735
|
+
|
|
736
|
+
const compareLikeTlc = (left: Value, right: Value): number => {
|
|
737
|
+
if (typeof left !== "object") {
|
|
738
|
+
if (typeof right === typeof left) return compareValues(left, right);
|
|
739
|
+
if (isModelValue(right)) return compareModelValueWith(right, left);
|
|
740
|
+
throw new TlaEvaluationError(describeScalarMismatch(left, right, false));
|
|
741
|
+
}
|
|
742
|
+
if (left.kind === "ModelValue") return compareModelValue(left, right);
|
|
743
|
+
if (isFunctionValue(left)) return compareFunctionLikeTlc(left, right);
|
|
744
|
+
return compareSetLikeTlc(left, right);
|
|
745
|
+
};
|
|
746
|
+
|
|
747
|
+
const equalsElementsLikeTlc = (left: Value[], right: Value[]) =>
|
|
748
|
+
left.every((element, index) => equalsLikeTlc(element, right[index]));
|
|
749
|
+
|
|
750
|
+
const equalsTlcFunctions = (functionValue: TlcFunction, left: Value, right: Value) => {
|
|
751
|
+
const other = toTlcFunction(right);
|
|
752
|
+
if (!other) {
|
|
753
|
+
if (isModelValue(right)) return equalsModelValueWith(right, left);
|
|
754
|
+
throw new TlaEvaluationError(
|
|
755
|
+
`Attempted to check equality of the function ${formatValue(left)} with the value:\n${formatValue(right)}`,
|
|
756
|
+
);
|
|
757
|
+
}
|
|
758
|
+
if (functionValue.range.length !== other.range.length) return false;
|
|
759
|
+
if (functionValue.low !== undefined && other.low !== undefined) {
|
|
760
|
+
return (
|
|
761
|
+
(functionValue.range.length === 0 || functionValue.low === other.low) &&
|
|
762
|
+
equalsElementsLikeTlc(functionValue.range, other.range)
|
|
763
|
+
);
|
|
764
|
+
}
|
|
765
|
+
const intervalLow = functionValue.low ?? other.low;
|
|
766
|
+
if (intervalLow === undefined) {
|
|
767
|
+
return (
|
|
768
|
+
equalsElementsLikeTlc(functionValue.domain, other.domain) &&
|
|
769
|
+
equalsElementsLikeTlc(functionValue.range, other.range)
|
|
770
|
+
);
|
|
771
|
+
}
|
|
772
|
+
const keys = functionValue.low === undefined ? functionValue.domain : other.domain;
|
|
773
|
+
return (
|
|
774
|
+
keys.every((key, index) => {
|
|
775
|
+
if (typeof key !== "number") {
|
|
776
|
+
throw new TlaEvaluationError(
|
|
777
|
+
`Attempted to compare an integer with non-integer:\n${formatValue(key)}.`,
|
|
778
|
+
);
|
|
779
|
+
}
|
|
780
|
+
return key === intervalLow + index;
|
|
781
|
+
}) && equalsElementsLikeTlc(functionValue.range, other.range)
|
|
782
|
+
);
|
|
783
|
+
};
|
|
784
|
+
|
|
785
|
+
const equalsFunctionLikeTlc = (left: FunctionValue | LazyFunctionValue, right: Value) => {
|
|
786
|
+
const functionValue = toFiniteFunction(left);
|
|
787
|
+
const isTuple = left.kind === "Function" && getSequenceElements(functionValue) !== undefined;
|
|
788
|
+
if (isTuple) {
|
|
789
|
+
const tuple = isFunctionValue(right) ? getSequenceElements(right) : undefined;
|
|
790
|
+
if (!tuple) return equalsTlcFunctions({ ...functionValue, low: 1 }, left, right);
|
|
791
|
+
return (
|
|
792
|
+
functionValue.range.length === tuple.length &&
|
|
793
|
+
equalsElementsLikeTlc(functionValue.range, tuple)
|
|
794
|
+
);
|
|
795
|
+
}
|
|
796
|
+
if (left.kind === "Function" && functionValue.domain.every((key) => typeof key === "string")) {
|
|
797
|
+
const record = toTlcRecord(right);
|
|
798
|
+
if (!record) {
|
|
799
|
+
if (isModelValue(right)) return equalsModelValueWith(right, left);
|
|
800
|
+
throw new TlaEvaluationError(
|
|
801
|
+
`Attempted to check equality of record:\n${formatValue(left)}\nwith non-record\n${formatValue(right)}`,
|
|
802
|
+
);
|
|
803
|
+
}
|
|
804
|
+
return (
|
|
805
|
+
functionValue.domain.length === record.domain.length &&
|
|
806
|
+
equalsElementsLikeTlc(functionValue.domain, record.domain) &&
|
|
807
|
+
equalsElementsLikeTlc(functionValue.range, record.range)
|
|
808
|
+
);
|
|
809
|
+
}
|
|
810
|
+
return equalsTlcFunctions(
|
|
811
|
+
{ ...functionValue, low: getIntervalLow(functionValue.domain) },
|
|
812
|
+
left,
|
|
813
|
+
right,
|
|
814
|
+
);
|
|
815
|
+
};
|
|
816
|
+
|
|
817
|
+
const equalsSetLikeTlc = (left: ObjectValue, right: Value): boolean => {
|
|
818
|
+
if (isComparedByClass(left, right)) return valuesEqual(left, right);
|
|
819
|
+
if (!isEnumeratedSet(left)) return equalsSetLikeTlc(toFiniteSet(left), right);
|
|
820
|
+
if (!isSetValue(right)) {
|
|
821
|
+
if (isModelValue(right)) return equalsModelValueWith(right, left);
|
|
822
|
+
throw new TlaEvaluationError(
|
|
823
|
+
`Attempted to check equality of the set ${formatValue(left)} with the value:\n${formatValue(right)}`,
|
|
824
|
+
);
|
|
825
|
+
}
|
|
826
|
+
const sets = getSetElementsLikeTlc(left, right);
|
|
827
|
+
return (
|
|
828
|
+
sets.elements.length === sets.otherElements.length &&
|
|
829
|
+
equalsElementsLikeTlc(sets.elements, sets.otherElements)
|
|
830
|
+
);
|
|
831
|
+
};
|
|
832
|
+
|
|
833
|
+
const equalsLikeTlc = (left: Value, right: Value): boolean => {
|
|
834
|
+
if (typeof left !== "object") {
|
|
835
|
+
if (typeof right === typeof left) return left === right;
|
|
836
|
+
if (isModelValue(right)) return equalsModelValueWith(right, left);
|
|
837
|
+
throw new TlaEvaluationError(describeScalarMismatch(left, right, true));
|
|
838
|
+
}
|
|
839
|
+
if (left.kind === "ModelValue") return equalsModelValue(left, right);
|
|
840
|
+
if (isFunctionValue(left)) return equalsFunctionLikeTlc(left, right);
|
|
841
|
+
return equalsSetLikeTlc(left, right);
|
|
842
|
+
};
|
|
843
|
+
|
|
844
|
+
// TLC's ValueVec.sort, which inserts each element into the sorted prefix by binary search.
|
|
845
|
+
const sortLikeTlc = (elements: Value[]) => {
|
|
846
|
+
const sorted: Value[] = [];
|
|
847
|
+
for (const element of elements) {
|
|
848
|
+
const bounds = { low: 0, high: sorted.length, comparison: 1 };
|
|
849
|
+
while (bounds.low < bounds.high) {
|
|
850
|
+
const middle = bounds.low + ((bounds.high - bounds.low) >> 1);
|
|
851
|
+
bounds.comparison = compareLikeTlc(element, sorted[middle]);
|
|
852
|
+
if (bounds.comparison === 0) break;
|
|
853
|
+
if (bounds.comparison < 0) bounds.high = middle;
|
|
854
|
+
else bounds.low = middle + 1;
|
|
855
|
+
}
|
|
856
|
+
if (bounds.comparison !== 0) sorted.splice(bounds.low, 0, element);
|
|
857
|
+
}
|
|
858
|
+
return sorted;
|
|
859
|
+
};
|
|
860
|
+
|
|
861
|
+
// TLC checks membership in a set enumeration it hasn't sorted element by element.
|
|
862
|
+
const isElementOfLikeTlc = (element: Value, elements: Value[]) =>
|
|
863
|
+
elements.some((member) => equalsLikeTlc(member, element));
|
|
864
|
+
|
|
865
|
+
export const isEqualLikeTlc = (left: Value, right: Value) => {
|
|
866
|
+
try {
|
|
867
|
+
return valuesEqual(left, right);
|
|
868
|
+
} catch (error) {
|
|
869
|
+
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
870
|
+
return equalsLikeTlc(left, right);
|
|
871
|
+
}
|
|
872
|
+
};
|
|
873
|
+
|
|
874
|
+
// TLC sorts a set by comparing each element with the ones before it, so the later element converts
|
|
875
|
+
// a lazy set it compares and caches it.
|
|
307
876
|
const isStrictlySorted = (elements: Value[]) => {
|
|
308
877
|
for (let index = 1; index < elements.length; index++) {
|
|
309
|
-
if (compareValues(elements[index
|
|
878
|
+
if (compareValues(elements[index], elements[index - 1]) <= 0) return false;
|
|
310
879
|
}
|
|
311
880
|
return true;
|
|
312
881
|
};
|
|
@@ -321,6 +890,17 @@ export const createSet = (elements: Value[]): SetValue => {
|
|
|
321
890
|
return createSortedSet(unique);
|
|
322
891
|
};
|
|
323
892
|
|
|
893
|
+
// TLC builds a set enumeration without sorting it, so one whose elements it can't compare still
|
|
894
|
+
// evaluates.
|
|
895
|
+
export const createEnumeratedSet = (elements: Value[]): SetValue | MixedSetValue => {
|
|
896
|
+
try {
|
|
897
|
+
return createSet(elements);
|
|
898
|
+
} catch (error) {
|
|
899
|
+
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
900
|
+
return { kind: "MixedSet", elements };
|
|
901
|
+
}
|
|
902
|
+
};
|
|
903
|
+
|
|
324
904
|
export const EMPTY_SET = createSortedSet([]);
|
|
325
905
|
|
|
326
906
|
export const differenceSorted = (left: SetValue, right: SetValue): SetValue => {
|
|
@@ -428,18 +1008,102 @@ export const createFunction = (pairs: [Value, Value][]): FunctionValue => {
|
|
|
428
1008
|
return createSortedFunction(domain, range);
|
|
429
1009
|
};
|
|
430
1010
|
|
|
1011
|
+
export const createWrittenFunction = (pairs: [Value, Value][]) => {
|
|
1012
|
+
const functionValue = createFunction(pairs);
|
|
1013
|
+
const isWrittenSorted =
|
|
1014
|
+
functionValue.domain.length !== pairs.length ||
|
|
1015
|
+
pairs.some((pair) => typeof pair[0] !== "string") ||
|
|
1016
|
+
pairs.every((pair, index) => pair[0] === functionValue.domain[index]);
|
|
1017
|
+
return isWrittenSorted
|
|
1018
|
+
? functionValue
|
|
1019
|
+
: setWrittenOrder(
|
|
1020
|
+
functionValue,
|
|
1021
|
+
pairs.map((pair) => pair[0]),
|
|
1022
|
+
);
|
|
1023
|
+
};
|
|
1024
|
+
|
|
1025
|
+
const intervalDomains: Value[][] = [];
|
|
1026
|
+
const intervalValues: IntervalValue[] = [];
|
|
1027
|
+
const intervalArrays = new WeakMap<Value[], IntervalValue>();
|
|
1028
|
+
|
|
1029
|
+
// TLC keeps the domain of a tuple, or of a function built over an interval, as an interval. Every
|
|
1030
|
+
// function over 1..n shares one domain array per length, and the elements of any other interval
|
|
1031
|
+
// map to it, so the array's identity records it. Callers must not mutate the returned array.
|
|
1032
|
+
const getIntervalDomain = (length: number): Value[] => {
|
|
1033
|
+
const cached = intervalDomains[length];
|
|
1034
|
+
if (cached) return cached;
|
|
1035
|
+
const domain = Array.from({ length }, (_, index) => index + 1);
|
|
1036
|
+
intervalDomains[length] = domain;
|
|
1037
|
+
return domain;
|
|
1038
|
+
};
|
|
1039
|
+
|
|
1040
|
+
// The elements of an interval for a caller that only iterates them, so they need not record
|
|
1041
|
+
// the interval for DOMAIN.
|
|
1042
|
+
export const getIntervalElements = (value: IntervalValue): Value[] => {
|
|
1043
|
+
getCardinality(value);
|
|
1044
|
+
if (value.low === 1 && value.high >= 1) return getIntervalDomain(value.high);
|
|
1045
|
+
const elements: number[] = [];
|
|
1046
|
+
for (let element = value.low; element <= value.high; element++) elements.push(element);
|
|
1047
|
+
return elements;
|
|
1048
|
+
};
|
|
1049
|
+
|
|
1050
|
+
const isIntervalDomain = (domain: Value[]) =>
|
|
1051
|
+
domain.length > 0 && (domain === intervalDomains[domain.length] || intervalArrays.has(domain));
|
|
1052
|
+
|
|
1053
|
+
// TLC keeps an interval domain only for a function built over an interval itself, so a set
|
|
1054
|
+
// such as (1..2) \cap S, whose elements are the shared array, gets a copy.
|
|
1055
|
+
export const getFunctionDomain = (set: Value): Value[] => {
|
|
1056
|
+
const elements = toFiniteSet(set).elements;
|
|
1057
|
+
return isIntervalDomain(elements) && !(typeof set === "object" && set.kind === "Interval")
|
|
1058
|
+
? [...elements]
|
|
1059
|
+
: elements;
|
|
1060
|
+
};
|
|
1061
|
+
|
|
1062
|
+
const getIntervalValue = (high: number): IntervalValue => {
|
|
1063
|
+
const cached = intervalValues[high];
|
|
1064
|
+
if (cached) return cached;
|
|
1065
|
+
const interval: IntervalValue = { kind: "Interval", low: 1, high };
|
|
1066
|
+
intervalValues[high] = interval;
|
|
1067
|
+
return interval;
|
|
1068
|
+
};
|
|
1069
|
+
|
|
431
1070
|
export const createTuple = (elements: Value[]): FunctionValue =>
|
|
432
|
-
createSortedFunction(
|
|
433
|
-
elements.map((_, index) => index + 1),
|
|
434
|
-
elements,
|
|
435
|
-
);
|
|
1071
|
+
createSortedFunction(getIntervalDomain(elements.length), elements);
|
|
436
1072
|
|
|
437
1073
|
export const createRecord = (fields: [string, Value][]): FunctionValue => createFunction(fields);
|
|
438
1074
|
|
|
1075
|
+
// TLC's Enumerable classes: every set except the ones it implements in Java.
|
|
1076
|
+
const isTlcEnumerable = (value: Value) => isSetValue(value) && !isOverriddenSet(value);
|
|
1077
|
+
|
|
1078
|
+
const formatUnion = (value: UnionValue, isStrict = false) =>
|
|
1079
|
+
`UNION(${formatValue(value.base, isStrict)})`;
|
|
1080
|
+
|
|
1081
|
+
const getUnionSets = (
|
|
1082
|
+
value: UnionValue,
|
|
1083
|
+
getElements = (set: Value) => toFiniteSet(set).elements,
|
|
1084
|
+
) => {
|
|
1085
|
+
if (!isTlcEnumerable(value.base)) {
|
|
1086
|
+
throw new TlaEvaluationError(
|
|
1087
|
+
`Attempted to enumerate the nonenumerable set:\n${formatUnion(value)}`,
|
|
1088
|
+
);
|
|
1089
|
+
}
|
|
1090
|
+
return getElements(value.base);
|
|
1091
|
+
};
|
|
1092
|
+
|
|
1093
|
+
const getUnionSet = (set: Value, index: number, union: UnionValue) => {
|
|
1094
|
+
if (isTlcEnumerable(set)) return set;
|
|
1095
|
+
throw new TlaEvaluationError(
|
|
1096
|
+
index === 0
|
|
1097
|
+
? "Attempted to enumerate UNION(s), but some element of s is nonenumerable."
|
|
1098
|
+
: `Attempted to enumerate the nonenumerable set:\n${formatValue(set)}\nwhen enumerating:\n${formatUnion(union)}`,
|
|
1099
|
+
);
|
|
1100
|
+
};
|
|
1101
|
+
|
|
439
1102
|
export const isEnumerable = (value: Value): boolean => {
|
|
440
1103
|
if (typeof value !== "object") return false;
|
|
441
1104
|
switch (value.kind) {
|
|
442
1105
|
case "Set":
|
|
1106
|
+
case "MixedSet":
|
|
443
1107
|
case "Interval":
|
|
444
1108
|
return true;
|
|
445
1109
|
case "InfiniteSet":
|
|
@@ -449,11 +1113,17 @@ export const isEnumerable = (value: Value): boolean => {
|
|
|
449
1113
|
case "FilteredSet":
|
|
450
1114
|
return isEnumerable(value.base);
|
|
451
1115
|
case "FunctionSet":
|
|
452
|
-
return
|
|
1116
|
+
return (
|
|
1117
|
+
(isEnumerable(value.domain) && isEnumerable(value.range)) ||
|
|
1118
|
+
getEmptiness(value.domain) === true ||
|
|
1119
|
+
getEmptiness(value) === true
|
|
1120
|
+
);
|
|
453
1121
|
case "RecordSet":
|
|
454
|
-
return value.sets.every(isEnumerable);
|
|
1122
|
+
return value.sets.every(isEnumerable) || getEmptiness(value) === true;
|
|
455
1123
|
case "ProductSet":
|
|
456
|
-
return value.factors.every(isEnumerable);
|
|
1124
|
+
return value.factors.every(isEnumerable) || getEmptiness(value) === true;
|
|
1125
|
+
case "Union":
|
|
1126
|
+
return isEnumerable(value.base) && getStreamedElements(value.base).every(isEnumerable);
|
|
457
1127
|
case "SetOperation":
|
|
458
1128
|
if (value.operator === "\\cup") return isEnumerable(value.left) && isEnumerable(value.right);
|
|
459
1129
|
if (value.operator === "\\cap") return isEnumerable(value.left) || isEnumerable(value.right);
|
|
@@ -463,7 +1133,42 @@ export const isEnumerable = (value: Value): boolean => {
|
|
|
463
1133
|
}
|
|
464
1134
|
};
|
|
465
1135
|
|
|
466
|
-
const
|
|
1136
|
+
const getEmptiness = (value: Value): boolean | undefined => {
|
|
1137
|
+
if (typeof value !== "object") return undefined;
|
|
1138
|
+
switch (value.kind) {
|
|
1139
|
+
case "Set":
|
|
1140
|
+
case "MixedSet":
|
|
1141
|
+
return value.elements.length === 0;
|
|
1142
|
+
case "Interval":
|
|
1143
|
+
return value.low > value.high;
|
|
1144
|
+
case "InfiniteSet":
|
|
1145
|
+
return value.name === "Any" ? undefined : false;
|
|
1146
|
+
case "Subset":
|
|
1147
|
+
case "SequenceSet":
|
|
1148
|
+
return false;
|
|
1149
|
+
case "FunctionSet": {
|
|
1150
|
+
const isUnit = getEmptiness(value.domain);
|
|
1151
|
+
if (isUnit === undefined) return undefined;
|
|
1152
|
+
return isUnit ? false : getEmptiness(value.range);
|
|
1153
|
+
}
|
|
1154
|
+
case "RecordSet":
|
|
1155
|
+
case "ProductSet": {
|
|
1156
|
+
for (const part of value.kind === "RecordSet" ? value.sets : value.factors) {
|
|
1157
|
+
const isEmpty = getEmptiness(part);
|
|
1158
|
+
if (isEmpty !== false) return isEmpty;
|
|
1159
|
+
}
|
|
1160
|
+
return false;
|
|
1161
|
+
}
|
|
1162
|
+
case "SetOperation":
|
|
1163
|
+
case "FilteredSet":
|
|
1164
|
+
case "Union":
|
|
1165
|
+
return isEnumerable(value) ? toFiniteSet(value).elements.length === 0 : undefined;
|
|
1166
|
+
default:
|
|
1167
|
+
return undefined;
|
|
1168
|
+
}
|
|
1169
|
+
};
|
|
1170
|
+
|
|
1171
|
+
const getCartesianProduct = (lists: Value[][]) => {
|
|
467
1172
|
const total = lists.reduce((product, list) => product * list.length, 1);
|
|
468
1173
|
const combinations: Value[][] = [];
|
|
469
1174
|
const positions = lists.map(() => 0);
|
|
@@ -491,7 +1196,7 @@ const advanceCombination = (indices: number[], length: number) => {
|
|
|
491
1196
|
return false;
|
|
492
1197
|
};
|
|
493
1198
|
|
|
494
|
-
const getSortedSubsets = (base: Value[])
|
|
1199
|
+
const getSortedSubsets = (base: Value[]) => {
|
|
495
1200
|
const subsets: Value[] = [];
|
|
496
1201
|
for (let size = 0; size <= base.length; size++) {
|
|
497
1202
|
const indices = Array.from({ length: size }, (_, index) => index);
|
|
@@ -508,12 +1213,15 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
508
1213
|
switch (value.kind) {
|
|
509
1214
|
case "Set":
|
|
510
1215
|
return value.elements;
|
|
511
|
-
case "
|
|
512
|
-
return
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
);
|
|
1216
|
+
case "MixedSet":
|
|
1217
|
+
return sortLikeTlc(value.elements);
|
|
1218
|
+
case "Interval": {
|
|
1219
|
+
const elements = getIntervalElements(value);
|
|
1220
|
+
if (elements !== intervalDomains[elements.length]) intervalArrays.set(elements, value);
|
|
1221
|
+
return elements;
|
|
1222
|
+
}
|
|
516
1223
|
case "Subset": {
|
|
1224
|
+
if (isOverriddenSet(value.base)) getCardinality(value.base);
|
|
517
1225
|
const base = toFiniteSet(value.base).elements;
|
|
518
1226
|
if (base.length > MAXIMUM_ENUMERATED_SUBSET_BASE) {
|
|
519
1227
|
throw new TlaEvaluationError(
|
|
@@ -523,29 +1231,83 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
523
1231
|
return getSortedSubsets(base);
|
|
524
1232
|
}
|
|
525
1233
|
case "FunctionSet": {
|
|
526
|
-
|
|
527
|
-
|
|
1234
|
+
if (isEmptySet(value)) return [];
|
|
1235
|
+
if (isOverriddenSet(value.domain)) {
|
|
1236
|
+
throw new TlaEvaluationError(
|
|
1237
|
+
`Attempted to enumerate a set of the form [D -> R],but the domain D:\n${formatValue(value.domain)}\ncannot be enumerated.`,
|
|
1238
|
+
);
|
|
1239
|
+
}
|
|
1240
|
+
const domain = getFunctionDomain(value.domain);
|
|
1241
|
+
if (domain.length > 0 && isOverriddenSet(value.range)) {
|
|
1242
|
+
throw new TlaEvaluationError(
|
|
1243
|
+
`Attempted to enumerate a set of the form [D -> R],but the range R:\n${formatValue(value.range)}\ncannot be enumerated.`,
|
|
1244
|
+
);
|
|
1245
|
+
}
|
|
1246
|
+
const range = domain.length === 0 ? [] : toFiniteSet(value.range).elements;
|
|
528
1247
|
return getCartesianProduct(domain.map(() => range)).map((combination): Value =>
|
|
529
1248
|
createSortedFunction(domain, combination),
|
|
530
1249
|
);
|
|
531
1250
|
}
|
|
532
|
-
case "RecordSet":
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
1251
|
+
case "RecordSet": {
|
|
1252
|
+
if (isEmptySet(value)) return [];
|
|
1253
|
+
const fields = value.sets.map((set, index) => {
|
|
1254
|
+
if (isOverriddenSet(set)) {
|
|
1255
|
+
throw new TlaEvaluationError(
|
|
1256
|
+
`Attempted to enumerate a set of the form [l1 : v1, ..., ln : vn],\nbut can't enumerate the value of the \`${value.names[index]}' field:\n${formatValue(set)}`,
|
|
1257
|
+
);
|
|
1258
|
+
}
|
|
1259
|
+
return toFiniteSet(set).elements;
|
|
1260
|
+
});
|
|
1261
|
+
return getCartesianProduct(fields).map((combination) =>
|
|
1262
|
+
createRecord(value.names.map((name, index): [string, Value] => [name, combination[index]])),
|
|
542
1263
|
);
|
|
1264
|
+
}
|
|
1265
|
+
case "ProductSet": {
|
|
1266
|
+
if (isEmptySet(value)) return [];
|
|
1267
|
+
const factors = value.factors.map((factor, index) => {
|
|
1268
|
+
if (isOverriddenSet(factor)) {
|
|
1269
|
+
throw new TlaEvaluationError(
|
|
1270
|
+
`Attempted to enumerate a set of the form s1 \\X s2 ... \\X sn,\nbut can't enumerate s${index}:\n${formatValue(factor)}`,
|
|
1271
|
+
);
|
|
1272
|
+
}
|
|
1273
|
+
return toFiniteSet(factor).elements;
|
|
1274
|
+
});
|
|
1275
|
+
return getCartesianProduct(factors).map(createTuple);
|
|
1276
|
+
}
|
|
543
1277
|
case "FilteredSet":
|
|
1278
|
+
if (isOverriddenSet(value.base)) {
|
|
1279
|
+
throw new TlaEvaluationError(
|
|
1280
|
+
`Attempted to enumerate { x \\in S : p(x) } when S:\n${formatValue(value.base)}\nis not enumerable`,
|
|
1281
|
+
);
|
|
1282
|
+
}
|
|
544
1283
|
return toFiniteSet(value.base).elements.filter(value.test);
|
|
1284
|
+
case "Union": {
|
|
1285
|
+
const elements = getUnionSets(value).flatMap(
|
|
1286
|
+
(set, index) => toFiniteSet(getUnionSet(set, index, value)).elements,
|
|
1287
|
+
);
|
|
1288
|
+
return toFiniteSet(createEnumeratedSet(elements)).elements;
|
|
1289
|
+
}
|
|
545
1290
|
case "SetOperation": {
|
|
546
|
-
if (
|
|
1291
|
+
if (
|
|
1292
|
+
value.operator === "\\cup" &&
|
|
1293
|
+
(isOverriddenSet(value.left) || isOverriddenSet(value.right))
|
|
1294
|
+
) {
|
|
1295
|
+
throw new TlaEvaluationError(
|
|
1296
|
+
`Attempted to enumerate S \\cup T when S:\n${formatValue(value.left)}\nand T:\n${formatValue(value.right)}\nare not both enumerable`,
|
|
1297
|
+
);
|
|
1298
|
+
}
|
|
1299
|
+
if (
|
|
1300
|
+
value.operator === "\\cap" &&
|
|
1301
|
+
isOverriddenSet(value.left) &&
|
|
1302
|
+
isOverriddenSet(value.right)
|
|
1303
|
+
) {
|
|
547
1304
|
throw new TlaEvaluationError(
|
|
548
|
-
`Attempted to enumerate
|
|
1305
|
+
`Attempted to enumerate S \\cap T when neither S:\n${formatValue(value.left)}\nnor T:\n${formatValue(value.right)}\nis enumerable`,
|
|
1306
|
+
);
|
|
1307
|
+
}
|
|
1308
|
+
if (value.operator === "\\" && isOverriddenSet(value.left)) {
|
|
1309
|
+
throw new TlaEvaluationError(
|
|
1310
|
+
`Attempted to enumerate S \\ T when S:\n${formatValue(value.left)}\nis not enumerable.`,
|
|
549
1311
|
);
|
|
550
1312
|
}
|
|
551
1313
|
if (value.operator === "\\cup")
|
|
@@ -568,42 +1330,93 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
568
1330
|
};
|
|
569
1331
|
|
|
570
1332
|
const finiteSets = new WeakMap<ObjectValue, SetValue>();
|
|
1333
|
+
const comparedSets = new WeakMap<ObjectValue, SetValue>();
|
|
571
1334
|
|
|
572
1335
|
export const toFiniteSet = (value: Value): SetValue => {
|
|
573
1336
|
if (typeof value === "object" && value.kind === "Set") return value;
|
|
1337
|
+
if (typeof value === "object" && value.kind === "Interval") {
|
|
1338
|
+
return createSortedSet(enumerateSet(value));
|
|
1339
|
+
}
|
|
574
1340
|
const cached = typeof value === "object" ? finiteSets.get(value) : undefined;
|
|
575
1341
|
if (cached) return cached;
|
|
1342
|
+
const set =
|
|
1343
|
+
(typeof value === "object" ? comparedSets.get(value) : undefined) ?? enumerateFiniteSet(value);
|
|
1344
|
+
if (typeof value === "object") finiteSets.set(value, set);
|
|
1345
|
+
return set;
|
|
1346
|
+
};
|
|
1347
|
+
|
|
1348
|
+
const enumerateFiniteSet = (value: Value) => {
|
|
576
1349
|
const elements = enumerateSet(value);
|
|
577
1350
|
const isSorted =
|
|
578
1351
|
typeof value === "object" &&
|
|
579
|
-
(value.kind === "
|
|
1352
|
+
(value.kind === "MixedSet" ||
|
|
1353
|
+
value.kind === "Union" ||
|
|
580
1354
|
value.kind === "SetOperation" ||
|
|
581
1355
|
value.kind === "FilteredSet" ||
|
|
582
1356
|
value.kind === "ProductSet" ||
|
|
583
1357
|
value.kind === "FunctionSet" ||
|
|
584
1358
|
value.kind === "Subset");
|
|
585
|
-
|
|
586
|
-
|
|
1359
|
+
return isSorted ? createSortedSet(elements) : createSet(elements);
|
|
1360
|
+
};
|
|
1361
|
+
|
|
1362
|
+
// TLC converts the set a comparison receives as its argument without caching it, so a lazy set
|
|
1363
|
+
// compared that way still streams the sets it combines.
|
|
1364
|
+
const toComparedSet = (value: Value) => {
|
|
1365
|
+
const isStreamed =
|
|
1366
|
+
typeof value === "object" &&
|
|
1367
|
+
(value.kind === "Union" || value.kind === "SetOperation" || value.kind === "FilteredSet");
|
|
1368
|
+
if (!isStreamed || finiteSets.has(value)) return toFiniteSet(value);
|
|
1369
|
+
const compared = comparedSets.get(value);
|
|
1370
|
+
if (compared) return compared;
|
|
1371
|
+
const set = enumerateFiniteSet(value);
|
|
1372
|
+
comparedSets.set(value, set);
|
|
587
1373
|
return set;
|
|
588
1374
|
};
|
|
589
1375
|
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
1376
|
+
// TLC's Enumerator for a lazy set streams the sets it combines until the set caches its elements,
|
|
1377
|
+
// so an element that two of them share comes up once for each.
|
|
1378
|
+
export const getStreamedElements = (value: Value): Value[] => {
|
|
1379
|
+
if (typeof value !== "object" || finiteSets.has(value)) return toFiniteSet(value).elements;
|
|
1380
|
+
// TLC streams the sets of a UNION it hasn't enumerated, so a set that appears twice there yields
|
|
1381
|
+
// its elements twice.
|
|
1382
|
+
if (value.kind === "Union") {
|
|
1383
|
+
return getUnionSets(value, getStreamedElements).flatMap((set, index) =>
|
|
1384
|
+
getStreamedElements(getUnionSet(set, index, value)),
|
|
594
1385
|
);
|
|
595
1386
|
}
|
|
596
|
-
|
|
1387
|
+
if (value.kind === "FilteredSet" && !isOverriddenSet(value.base)) {
|
|
1388
|
+
return getStreamedElements(value.base).filter(value.test);
|
|
1389
|
+
}
|
|
1390
|
+
if (value.kind !== "SetOperation") return toFiniteSet(value).elements;
|
|
1391
|
+
if (value.operator === "\\cup") {
|
|
1392
|
+
return isOverriddenSet(value.left) || isOverriddenSet(value.right)
|
|
1393
|
+
? toFiniteSet(value).elements
|
|
1394
|
+
: [...getStreamedElements(value.left), ...getStreamedElements(value.right)];
|
|
1395
|
+
}
|
|
1396
|
+
const isLeftEnumerated = value.operator === "\\" || isEnumerable(value.left);
|
|
1397
|
+
const enumerated = isLeftEnumerated ? value.left : value.right;
|
|
1398
|
+
const other = isLeftEnumerated ? value.right : value.left;
|
|
1399
|
+
if (isOverriddenSet(enumerated)) return toFiniteSet(value).elements;
|
|
1400
|
+
return getStreamedElements(enumerated).filter(
|
|
1401
|
+
(element) => isElementOf(element, other) === (value.operator === "\\cap"),
|
|
1402
|
+
);
|
|
597
1403
|
};
|
|
598
1404
|
|
|
599
1405
|
export const isEmptySet = (value: Value): boolean => {
|
|
600
|
-
if (typeof value !== "object"
|
|
1406
|
+
if (typeof value !== "object" || !isSetValue(value)) {
|
|
1407
|
+
throw new TlaEvaluationError(`Shouldn't call isEmpty() on value ${formatValue(value)}`);
|
|
1408
|
+
}
|
|
601
1409
|
switch (value.kind) {
|
|
602
1410
|
case "Set":
|
|
1411
|
+
case "MixedSet":
|
|
603
1412
|
return value.elements.length === 0;
|
|
604
1413
|
case "Interval":
|
|
605
1414
|
return value.low > value.high;
|
|
606
1415
|
case "InfiniteSet":
|
|
1416
|
+
if (value.name === "Any") {
|
|
1417
|
+
throw new TlaEvaluationError("Shouldn't call isEmpty() on value ANY");
|
|
1418
|
+
}
|
|
1419
|
+
return false;
|
|
607
1420
|
case "Subset":
|
|
608
1421
|
case "SequenceSet":
|
|
609
1422
|
return false;
|
|
@@ -612,41 +1425,143 @@ export const isEmptySet = (value: Value): boolean => {
|
|
|
612
1425
|
case "RecordSet":
|
|
613
1426
|
return value.sets.some(isEmptySet);
|
|
614
1427
|
case "FunctionSet":
|
|
615
|
-
return isEmptySet(value.
|
|
1428
|
+
return !isEmptySet(value.domain) && isEmptySet(value.range);
|
|
1429
|
+
case "Union":
|
|
1430
|
+
return getUnionSets(value).every((set, index) => isEmptySet(getUnionSet(set, index, value)));
|
|
616
1431
|
default:
|
|
617
1432
|
return toFiniteSet(value).elements.length === 0;
|
|
618
1433
|
}
|
|
619
1434
|
};
|
|
620
1435
|
|
|
621
|
-
const getProductCardinality = (sets: Value[],
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
};
|
|
1436
|
+
const getProductCardinality = (sets: Value[], describe: () => string) =>
|
|
1437
|
+
sets.reduce<number>((product, set) => {
|
|
1438
|
+
const size = product * getCardinality(set);
|
|
1439
|
+
if (size > MAXIMUM_INTEGER) throw new TlaEvaluationError(describe());
|
|
1440
|
+
return size;
|
|
1441
|
+
}, 1);
|
|
1442
|
+
|
|
1443
|
+
const describeOverflow = (value: Value) =>
|
|
1444
|
+
`Overflow when computing the number of elements in:\n${formatValue(value)}`;
|
|
627
1445
|
|
|
628
1446
|
export const getCardinality = (value: Value): number => {
|
|
629
1447
|
if (typeof value !== "object") return toFiniteSet(value).elements.length;
|
|
630
1448
|
switch (value.kind) {
|
|
631
|
-
case "Interval":
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
1449
|
+
case "Interval": {
|
|
1450
|
+
const size = Math.max(0, value.high - value.low + 1);
|
|
1451
|
+
if (size > MAXIMUM_INTEGER) {
|
|
1452
|
+
throw new TlaEvaluationError(
|
|
1453
|
+
`Size of interval value exceeds the maximum representable size (32bits): ${value.low}..${value.high}.`,
|
|
1454
|
+
);
|
|
1455
|
+
}
|
|
1456
|
+
return size;
|
|
1457
|
+
}
|
|
1458
|
+
case "InfiniteSet":
|
|
1459
|
+
case "SequenceSet":
|
|
1460
|
+
throw new TlaEvaluationError(
|
|
1461
|
+
`Attempted to compute the number of elements in the overridden value ${formatValue(value)}.`,
|
|
640
1462
|
);
|
|
1463
|
+
case "Subset": {
|
|
1464
|
+
const size = getCardinality(value.base);
|
|
1465
|
+
if (size >= 31) throw new TlaEvaluationError(describeOverflow(value));
|
|
1466
|
+
return 2 ** size;
|
|
1467
|
+
}
|
|
1468
|
+
case "FunctionSet": {
|
|
1469
|
+
if (isEmptySet(value.domain)) return 1;
|
|
1470
|
+
if (isEmptySet(value.range)) return 0;
|
|
1471
|
+
const rangeSize = getCardinality(value.range);
|
|
1472
|
+
if (rangeSize === 1) return 1;
|
|
1473
|
+
const domainSize = getCardinality(value.domain);
|
|
1474
|
+
const size = { value: 1 };
|
|
1475
|
+
for (let index = 0; index < domainSize; index++) {
|
|
1476
|
+
size.value *= rangeSize;
|
|
1477
|
+
if (size.value > MAXIMUM_INTEGER) throw new TlaEvaluationError(describeOverflow(value));
|
|
1478
|
+
}
|
|
1479
|
+
return size.value;
|
|
1480
|
+
}
|
|
641
1481
|
case "RecordSet":
|
|
642
|
-
|
|
1482
|
+
if (isEmptySet(value)) return 0;
|
|
1483
|
+
return getProductCardinality(value.sets, () => describeOverflow(value));
|
|
643
1484
|
case "ProductSet":
|
|
644
|
-
|
|
1485
|
+
if (isEmptySet(value)) return 0;
|
|
1486
|
+
return getProductCardinality(
|
|
1487
|
+
value.factors,
|
|
1488
|
+
() => `Overflow when computing the number of elements in ${formatValue(value)}`,
|
|
1489
|
+
);
|
|
645
1490
|
default:
|
|
646
1491
|
return toFiniteSet(value).elements.length;
|
|
647
1492
|
}
|
|
648
1493
|
};
|
|
649
1494
|
|
|
1495
|
+
const describeInfiniteCheck = (value: Value) =>
|
|
1496
|
+
`Attempted to check if the set ${formatValue(value)}is finite.`;
|
|
1497
|
+
|
|
1498
|
+
export const isFiniteSet = (value: Value): boolean => {
|
|
1499
|
+
if (typeof value !== "object" || value.kind === "ModelValue") {
|
|
1500
|
+
throw new TlaEvaluationError(
|
|
1501
|
+
`Attempted to check if the ${describeKind(value)} ${formatValue(value)} is a finite set.`,
|
|
1502
|
+
);
|
|
1503
|
+
}
|
|
1504
|
+
switch (value.kind) {
|
|
1505
|
+
case "Set":
|
|
1506
|
+
case "MixedSet":
|
|
1507
|
+
case "Interval":
|
|
1508
|
+
case "Function":
|
|
1509
|
+
return true;
|
|
1510
|
+
case "LazyFunction":
|
|
1511
|
+
throw new TlaEvaluationError(
|
|
1512
|
+
`Attempted to check if the function:\n${formatValue(value)}\nis a finite set.`,
|
|
1513
|
+
);
|
|
1514
|
+
case "InfiniteSet":
|
|
1515
|
+
return false;
|
|
1516
|
+
case "SequenceSet":
|
|
1517
|
+
return isFiniteSet(value.base) && isEmptySet(value.base);
|
|
1518
|
+
case "Subset":
|
|
1519
|
+
return isFiniteSet(value.base);
|
|
1520
|
+
case "FunctionSet": {
|
|
1521
|
+
const isFiniteDomain = isFiniteSet(value.domain);
|
|
1522
|
+
if (isFiniteDomain && isEmptySet(value.domain)) return true;
|
|
1523
|
+
const isFiniteRange = isFiniteSet(value.range);
|
|
1524
|
+
if (isFiniteRange && isEmptySet(value.range)) return true;
|
|
1525
|
+
return isFiniteRange && (isFiniteDomain || getCardinality(value.range) === 1);
|
|
1526
|
+
}
|
|
1527
|
+
case "RecordSet":
|
|
1528
|
+
case "ProductSet": {
|
|
1529
|
+
const parts = value.kind === "RecordSet" ? value.sets : value.factors;
|
|
1530
|
+
const finiteness = { isAllFinite: true };
|
|
1531
|
+
for (const part of parts) {
|
|
1532
|
+
if (!isFiniteSet(part)) finiteness.isAllFinite = false;
|
|
1533
|
+
else if (isEmptySet(part)) return true;
|
|
1534
|
+
}
|
|
1535
|
+
return finiteness.isAllFinite;
|
|
1536
|
+
}
|
|
1537
|
+
case "Union":
|
|
1538
|
+
if (!isTlcEnumerable(value.base)) {
|
|
1539
|
+
throw new TlaEvaluationError(
|
|
1540
|
+
`Attempted to check if the nonenumerable set:\n${formatUnion(value)}\nis a finite set.`,
|
|
1541
|
+
);
|
|
1542
|
+
}
|
|
1543
|
+
return toFiniteSet(value.base).elements.every(isFiniteSet);
|
|
1544
|
+
case "SetOperation":
|
|
1545
|
+
if (value.operator === "\\cup") return isFiniteSet(value.left) && isFiniteSet(value.right);
|
|
1546
|
+
if (value.operator === "\\cap") {
|
|
1547
|
+
if (!isFiniteSet(value.left) && !isFiniteSet(value.right)) {
|
|
1548
|
+
throw new TlaEvaluationError(describeInfiniteCheck(value));
|
|
1549
|
+
}
|
|
1550
|
+
return true;
|
|
1551
|
+
}
|
|
1552
|
+
if (isFiniteSet(value.left)) return true;
|
|
1553
|
+
if (!isFiniteSet(value.right)) throw new TlaEvaluationError(describeInfiniteCheck(value));
|
|
1554
|
+
return false;
|
|
1555
|
+
case "FilteredSet":
|
|
1556
|
+
if (!isFiniteSet(value.base)) {
|
|
1557
|
+
throw new TlaEvaluationError(
|
|
1558
|
+
`Attempted to check if expression of form {x \\in S : p(x)} is a finite set, but cannot check if S:\n${formatValue(value.base)}\nis finite.`,
|
|
1559
|
+
);
|
|
1560
|
+
}
|
|
1561
|
+
return true;
|
|
1562
|
+
}
|
|
1563
|
+
};
|
|
1564
|
+
|
|
650
1565
|
const findIndex = (sortedValues: Value[], target: Value) => {
|
|
651
1566
|
const bounds = { low: 0, high: sortedValues.length - 1 };
|
|
652
1567
|
while (bounds.low <= bounds.high) {
|
|
@@ -661,30 +1576,59 @@ const findIndex = (sortedValues: Value[], target: Value) => {
|
|
|
661
1576
|
|
|
662
1577
|
export const getSequenceElements = (value: Value): Value[] | undefined => {
|
|
663
1578
|
if (typeof value !== "object" || !isFunctionValue(value)) return undefined;
|
|
664
|
-
const
|
|
665
|
-
return
|
|
1579
|
+
const functionValue = toFiniteFunction(value);
|
|
1580
|
+
return functionValue.domain.every((key, index) => key === index + 1)
|
|
1581
|
+
? functionValue.range
|
|
1582
|
+
: undefined;
|
|
666
1583
|
};
|
|
667
1584
|
|
|
668
1585
|
const describeMembership = (element: Value, set: Value) =>
|
|
669
1586
|
`Attempted to check if the value ${formatValue(element)} is an element of ${formatValue(set)}`;
|
|
670
1587
|
|
|
1588
|
+
const describeNonSetMembership = (element: Value, value: Value) => {
|
|
1589
|
+
const prefix = `Attempted to check if the value:\n${formatValue(element)}\nis an element of the`;
|
|
1590
|
+
if (!isFunctionValue(value)) return `${prefix} ${describeKind(value)} ${formatValue(value)}`;
|
|
1591
|
+
const domain =
|
|
1592
|
+
value.kind === "Function" || isEnumerable(value.domain) ? toFiniteFunction(value).domain : [];
|
|
1593
|
+
if (domain.length > 0 && domain.every((key) => typeof key === "string")) {
|
|
1594
|
+
return `Attempted to check if element:\n${formatValue(element)}\nis in the record:\n${formatValue(value)}`;
|
|
1595
|
+
}
|
|
1596
|
+
if (domain.every((key, index) => key === index + 1)) {
|
|
1597
|
+
return "Attempted to check set membership in a tuple value.";
|
|
1598
|
+
}
|
|
1599
|
+
return `${prefix} function ${formatValue(value)}`;
|
|
1600
|
+
};
|
|
1601
|
+
|
|
671
1602
|
export const isElementOf = (element: Value, set: Value): boolean => {
|
|
672
|
-
if (
|
|
673
|
-
throw new TlaEvaluationError(`Expected a set but found ${formatValue(set)}`);
|
|
1603
|
+
if (!isSetValue(set)) throw new TlaEvaluationError(describeNonSetMembership(element, set));
|
|
674
1604
|
switch (set.kind) {
|
|
675
1605
|
case "Set":
|
|
676
|
-
|
|
1606
|
+
try {
|
|
1607
|
+
return findIndex(set.elements, element) !== -1;
|
|
1608
|
+
} catch (error) {
|
|
1609
|
+
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
1610
|
+
return isElementOfLikeTlc(element, set.elements);
|
|
1611
|
+
}
|
|
1612
|
+
case "MixedSet":
|
|
1613
|
+
return isElementOfLikeTlc(element, set.elements);
|
|
677
1614
|
case "Interval":
|
|
678
1615
|
if (typeof element === "number") return element >= set.low && element <= set.high;
|
|
679
|
-
if (set.low <= set.high && !isModelValue(element))
|
|
680
|
-
throw new TlaEvaluationError(
|
|
1616
|
+
if (set.low <= set.high && !isModelValue(element)) {
|
|
1617
|
+
throw new TlaEvaluationError(
|
|
1618
|
+
`Attempted to check if the value:\n${formatValue(element)}\nis in the integer interval ${formatValue(set)}`,
|
|
1619
|
+
);
|
|
1620
|
+
}
|
|
681
1621
|
return false;
|
|
682
1622
|
case "InfiniteSet": {
|
|
683
1623
|
if (set.name === "Any") return true;
|
|
684
1624
|
if (isModelValue(element)) return false;
|
|
685
1625
|
const isMemberType =
|
|
686
1626
|
set.name === "STRING" ? typeof element === "string" : typeof element === "number";
|
|
687
|
-
if (!isMemberType)
|
|
1627
|
+
if (!isMemberType) {
|
|
1628
|
+
throw new TlaEvaluationError(
|
|
1629
|
+
`Attempted to check if the value:\n${formatValue(element)}\nis an element of ${set.name}.`,
|
|
1630
|
+
);
|
|
1631
|
+
}
|
|
688
1632
|
return set.name !== "Nat" || (typeof element === "number" && element >= 0);
|
|
689
1633
|
}
|
|
690
1634
|
case "Subset":
|
|
@@ -695,40 +1639,58 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
695
1639
|
if (isModelValue(element)) return false;
|
|
696
1640
|
throw new TlaEvaluationError(describeMembership(element, set));
|
|
697
1641
|
}
|
|
698
|
-
const
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
1642
|
+
const functionValue = toFiniteFunction(element);
|
|
1643
|
+
const low = getIntervalLow(functionValue.domain);
|
|
1644
|
+
const isDomain =
|
|
1645
|
+
low === undefined
|
|
1646
|
+
? isEqualLikeTlc(set.domain, createSortedSet(functionValue.domain))
|
|
1647
|
+
: isEqualLikeTlc(
|
|
1648
|
+
{ kind: "Interval", low, high: low + functionValue.domain.length - 1 },
|
|
1649
|
+
set.domain,
|
|
1650
|
+
);
|
|
1651
|
+
return isDomain && functionValue.range.every((item) => isElementOf(item, set.range));
|
|
703
1652
|
}
|
|
704
1653
|
case "RecordSet": {
|
|
705
|
-
|
|
1654
|
+
const functionValue = isFunctionValue(element) ? toFiniteFunction(element) : undefined;
|
|
1655
|
+
if (!functionValue || !isRecordDomain(functionValue.domain)) {
|
|
706
1656
|
if (isModelValue(element)) return false;
|
|
707
|
-
throw new TlaEvaluationError(
|
|
1657
|
+
throw new TlaEvaluationError(
|
|
1658
|
+
`Attempted to check if non-record\n${formatValue(element)}\nis in the set of records:\n${formatValue(set)}`,
|
|
1659
|
+
);
|
|
708
1660
|
}
|
|
709
|
-
const fn = toFiniteFunction(element);
|
|
710
1661
|
return (
|
|
711
|
-
|
|
1662
|
+
functionValue.domain.length === set.names.length &&
|
|
712
1663
|
set.names.every((name, index) => {
|
|
713
|
-
const position = findIndex(
|
|
714
|
-
return position !== -1 && isElementOf(
|
|
1664
|
+
const position = findIndex(functionValue.domain, name);
|
|
1665
|
+
return position !== -1 && isElementOf(functionValue.range[position], set.sets[index]);
|
|
715
1666
|
})
|
|
716
1667
|
);
|
|
717
1668
|
}
|
|
718
1669
|
case "ProductSet": {
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
1670
|
+
const elements = isFunctionValue(element) ? getSequenceElements(element) : undefined;
|
|
1671
|
+
if (elements === undefined) {
|
|
1672
|
+
const isFunction =
|
|
1673
|
+
isFunctionValue(element) &&
|
|
1674
|
+
toFiniteFunction(element).domain.every((key) => typeof key === "number");
|
|
1675
|
+
if (isFunction || isModelValue(element)) return false;
|
|
1676
|
+
throw new TlaEvaluationError(
|
|
1677
|
+
`Attempted to check if non-tuple\n${formatValue(element)}\nis in the set of tuples:\n${formatValue(set)}`,
|
|
1678
|
+
);
|
|
722
1679
|
}
|
|
723
|
-
const elements = getSequenceElements(element);
|
|
724
1680
|
return (
|
|
725
|
-
elements !== undefined &&
|
|
726
1681
|
elements.length === set.factors.length &&
|
|
727
1682
|
elements.every((item, index) => isElementOf(item, set.factors[index]))
|
|
728
1683
|
);
|
|
729
1684
|
}
|
|
730
1685
|
case "FilteredSet":
|
|
731
1686
|
return isElementOf(element, set.base) && set.test(element);
|
|
1687
|
+
case "Union":
|
|
1688
|
+
if (!isTlcEnumerable(set.base)) {
|
|
1689
|
+
throw new TlaEvaluationError(
|
|
1690
|
+
`Attempted to check if:\n ${formatValue(element)}\nis an element of the non-enumerable set:\n ${formatUnion(set)}`,
|
|
1691
|
+
);
|
|
1692
|
+
}
|
|
1693
|
+
return toFiniteSet(set.base).elements.some((inner) => isElementOf(element, inner));
|
|
732
1694
|
case "SetOperation":
|
|
733
1695
|
if (set.operator === "\\cup")
|
|
734
1696
|
return isElementOf(element, set.left) || isElementOf(element, set.right);
|
|
@@ -745,14 +1707,34 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
745
1707
|
return elements.every((item) => isElementOf(item, set.base));
|
|
746
1708
|
}
|
|
747
1709
|
default:
|
|
748
|
-
throw new TlaEvaluationError(
|
|
1710
|
+
throw new TlaEvaluationError(describeNonSetMembership(element, set));
|
|
749
1711
|
}
|
|
750
1712
|
};
|
|
751
1713
|
|
|
1714
|
+
// A function that agrees with a member of a function set outside some entries is a member when
|
|
1715
|
+
// those entries are.
|
|
1716
|
+
export const isUpdatedElementOf = (element: Value, member: Value, set: Value): boolean => {
|
|
1717
|
+
if (element === member) return true;
|
|
1718
|
+
if (
|
|
1719
|
+
typeof element !== "object" ||
|
|
1720
|
+
typeof member !== "object" ||
|
|
1721
|
+
typeof set !== "object" ||
|
|
1722
|
+
element.kind !== "Function" ||
|
|
1723
|
+
member.kind !== "Function" ||
|
|
1724
|
+
set.kind !== "FunctionSet" ||
|
|
1725
|
+
element.domain !== member.domain
|
|
1726
|
+
)
|
|
1727
|
+
return isElementOf(element, set);
|
|
1728
|
+
return element.range.every((item, index) =>
|
|
1729
|
+
isUpdatedElementOf(item, member.range[index], set.range),
|
|
1730
|
+
);
|
|
1731
|
+
};
|
|
1732
|
+
|
|
752
1733
|
export const toFiniteFunction = (value: Value): FunctionValue => {
|
|
753
1734
|
if (typeof value === "object" && value.kind === "Function") return value;
|
|
754
1735
|
if (typeof value === "object" && value.kind === "LazyFunction") {
|
|
755
|
-
|
|
1736
|
+
if (!isEnumerable(value.domain)) getCardinality(value.domain);
|
|
1737
|
+
const domain = getFunctionDomain(value.domain);
|
|
756
1738
|
return createSortedFunction(
|
|
757
1739
|
domain,
|
|
758
1740
|
domain.map((key) => value.apply(key)),
|
|
@@ -761,64 +1743,103 @@ export const toFiniteFunction = (value: Value): FunctionValue => {
|
|
|
761
1743
|
throw new TlaEvaluationError(`Expected a function but found ${formatValue(value)}`);
|
|
762
1744
|
};
|
|
763
1745
|
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
1746
|
+
const lambdaFunctions = new WeakSet<ObjectValue>();
|
|
1747
|
+
|
|
1748
|
+
export const markLambdaFunction = <FunctionLike extends FunctionValue | LazyFunctionValue>(
|
|
1749
|
+
functionValue: FunctionLike,
|
|
1750
|
+
) => {
|
|
1751
|
+
lambdaFunctions.add(functionValue);
|
|
1752
|
+
return functionValue;
|
|
1753
|
+
};
|
|
1754
|
+
|
|
1755
|
+
const describeMissingArgument = (functionValue: Value, argument: Value) =>
|
|
1756
|
+
typeof functionValue === "object" && lambdaFunctions.has(functionValue)
|
|
1757
|
+
? `In applying the function\n${prettyPrint(formatValue(functionValue))},\nthe first argument is:\n${prettyPrint(formatValue(argument))}\nwhich is not in its domain.\n`
|
|
1758
|
+
: `Attempted to apply function ${formatValue(functionValue)} to ${formatValue(argument)}, which is not in its domain`;
|
|
1759
|
+
|
|
1760
|
+
const findKeyIndex = (domain: Value[], argument: Value) => {
|
|
777
1761
|
const first = domain[0];
|
|
778
1762
|
if (typeof argument === "number" && typeof first === "number") {
|
|
779
1763
|
const offset = argument - first;
|
|
780
|
-
if (domain[offset] === argument) return
|
|
1764
|
+
if (domain[offset] === argument) return offset;
|
|
781
1765
|
}
|
|
782
1766
|
if (domain.length <= 8) {
|
|
783
1767
|
for (let index = 0; index < domain.length; index++)
|
|
784
|
-
if (domain[index] === argument) return
|
|
1768
|
+
if (domain[index] === argument) return index;
|
|
1769
|
+
}
|
|
1770
|
+
return findIndex(domain, argument);
|
|
1771
|
+
};
|
|
1772
|
+
|
|
1773
|
+
export const applyFunction = (
|
|
1774
|
+
functionValue: Value,
|
|
1775
|
+
argument: Value,
|
|
1776
|
+
describeMissing?: (functionValue: FunctionValue) => string,
|
|
1777
|
+
): Value => {
|
|
1778
|
+
if (typeof functionValue === "object" && functionValue.kind === "LazyFunction") {
|
|
1779
|
+
if (!isElementOf(argument, functionValue.domain)) {
|
|
1780
|
+
throw new TlaEvaluationError(
|
|
1781
|
+
lambdaFunctions.has(functionValue)
|
|
1782
|
+
? describeMissingArgument(functionValue, argument)
|
|
1783
|
+
: `Attempted to apply a function to ${formatValue(argument)}, which is not in its domain`,
|
|
1784
|
+
);
|
|
1785
|
+
}
|
|
1786
|
+
return functionValue.apply(argument);
|
|
785
1787
|
}
|
|
786
|
-
|
|
1788
|
+
if (typeof functionValue !== "object" || functionValue.kind !== "Function") {
|
|
1789
|
+
throw new TlaEvaluationError(
|
|
1790
|
+
`Attempted to apply ${formatValue(functionValue)}, which is not a function`,
|
|
1791
|
+
);
|
|
1792
|
+
}
|
|
1793
|
+
const position = findKeyIndex(functionValue.domain, argument);
|
|
787
1794
|
if (position === -1) {
|
|
788
1795
|
throw new TlaEvaluationError(
|
|
789
|
-
|
|
1796
|
+
describeMissing?.(functionValue) ?? describeMissingArgument(functionValue, argument),
|
|
790
1797
|
);
|
|
791
1798
|
}
|
|
792
|
-
return
|
|
1799
|
+
return functionValue.range[position];
|
|
793
1800
|
};
|
|
794
1801
|
|
|
795
|
-
export const
|
|
796
|
-
|
|
797
|
-
|
|
1802
|
+
export const getDomainInterval = (domain: Value[]) =>
|
|
1803
|
+
isIntervalDomain(domain)
|
|
1804
|
+
? (intervalArrays.get(domain) ?? getIntervalValue(domain.length))
|
|
1805
|
+
: undefined;
|
|
1806
|
+
|
|
1807
|
+
export const getDomain = (functionValue: Value): Value => {
|
|
1808
|
+
if (typeof functionValue === "object" && functionValue.kind === "LazyFunction")
|
|
1809
|
+
return functionValue.domain;
|
|
1810
|
+
const domain = toFiniteFunction(functionValue).domain;
|
|
1811
|
+
return getDomainInterval(domain) ?? createSortedSet(domain);
|
|
798
1812
|
};
|
|
799
1813
|
|
|
800
1814
|
export const updateFunction = (
|
|
801
|
-
|
|
1815
|
+
functionValue: Value,
|
|
802
1816
|
argument: Value,
|
|
803
1817
|
update: (previous: Value) => Value,
|
|
1818
|
+
isKeptLazy = false,
|
|
804
1819
|
): Value => {
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
1820
|
+
const isLazy =
|
|
1821
|
+
typeof functionValue === "object" &&
|
|
1822
|
+
functionValue.kind === "LazyFunction" &&
|
|
1823
|
+
(isKeptLazy || !isEnumerable(functionValue.domain));
|
|
1824
|
+
if (isLazy) {
|
|
1825
|
+
if (!isElementOf(argument, functionValue.domain)) return functionValue;
|
|
1826
|
+
const updated = update(functionValue.apply(argument));
|
|
808
1827
|
return {
|
|
809
1828
|
kind: "LazyFunction",
|
|
810
|
-
domain:
|
|
811
|
-
apply: (key) => (
|
|
1829
|
+
domain: functionValue.domain,
|
|
1830
|
+
apply: (key) => (isEqualLikeTlc(argument, key) ? updated : functionValue.apply(key)),
|
|
1831
|
+
source: functionValue.source,
|
|
812
1832
|
};
|
|
813
1833
|
}
|
|
814
|
-
const finite = toFiniteFunction(
|
|
815
|
-
const position =
|
|
1834
|
+
const finite = toFiniteFunction(functionValue);
|
|
1835
|
+
const position = findKeyIndex(finite.domain, argument);
|
|
816
1836
|
if (position === -1) return finite;
|
|
817
1837
|
const range = [...finite.range];
|
|
818
1838
|
const previous = finite.range[position];
|
|
819
1839
|
const next = update(previous);
|
|
820
1840
|
range[position] = next;
|
|
821
1841
|
const updated = createSortedFunction(finite.domain, range);
|
|
1842
|
+
updated.writtenKeys = finite.writtenKeys;
|
|
822
1843
|
if (finite.hash !== 0) {
|
|
823
1844
|
const key = finite.domain[position];
|
|
824
1845
|
updated.hash =
|
|
@@ -867,7 +1888,7 @@ const getElementContribution = (element: Value) =>
|
|
|
867
1888
|
const getEntryContribution = (key: Value, value: Value) =>
|
|
868
1889
|
finalizeHash((Math.imul(getValueHash(key), 0x27d4eb2d) + getValueHash(value)) | 0);
|
|
869
1890
|
|
|
870
|
-
const computeValueHash = (value: ObjectValue)
|
|
1891
|
+
const computeValueHash = (value: ObjectValue) => {
|
|
871
1892
|
if (value.kind === "ModelValue") return finalizeHash(combineHash(hashString(value.name), 0x6d));
|
|
872
1893
|
if (isSetValue(value)) {
|
|
873
1894
|
if (!isEnumerable(value)) return hashString(getValueKey(value));
|
|
@@ -878,10 +1899,11 @@ const computeValueHash = (value: ObjectValue): number => {
|
|
|
878
1899
|
}
|
|
879
1900
|
return hash;
|
|
880
1901
|
}
|
|
881
|
-
const
|
|
1902
|
+
const functionValue = toFiniteFunction(value);
|
|
882
1903
|
let hash = FUNCTION_HASH_SEED;
|
|
883
|
-
for (let index = 0; index <
|
|
884
|
-
hash =
|
|
1904
|
+
for (let index = 0; index < functionValue.domain.length; index++) {
|
|
1905
|
+
hash =
|
|
1906
|
+
(hash + getEntryContribution(functionValue.domain[index], functionValue.range[index])) | 0;
|
|
885
1907
|
}
|
|
886
1908
|
return hash;
|
|
887
1909
|
};
|
|
@@ -924,7 +1946,7 @@ const getEntryFingerprint = (key: Value, value: Value) =>
|
|
|
924
1946
|
(Math.imul(getValueFingerprint(key), 0x165667b1) + getValueFingerprint(value)) | 0,
|
|
925
1947
|
);
|
|
926
1948
|
|
|
927
|
-
const computeValueFingerprint = (value: ObjectValue)
|
|
1949
|
+
const computeValueFingerprint = (value: ObjectValue) => {
|
|
928
1950
|
if (value.kind === "ModelValue") return finalizeFingerprint(fingerprintString(value.name) ^ 0x3c);
|
|
929
1951
|
if (isSetValue(value)) {
|
|
930
1952
|
if (!isEnumerable(value)) return fingerprintString(getValueKey(value));
|
|
@@ -935,10 +1957,12 @@ const computeValueFingerprint = (value: ObjectValue): number => {
|
|
|
935
1957
|
}
|
|
936
1958
|
return fingerprint;
|
|
937
1959
|
}
|
|
938
|
-
const
|
|
1960
|
+
const functionValue = toFiniteFunction(value);
|
|
939
1961
|
let fingerprint = 0x4cf5ad43;
|
|
940
|
-
for (let index = 0; index <
|
|
941
|
-
fingerprint =
|
|
1962
|
+
for (let index = 0; index < functionValue.domain.length; index++) {
|
|
1963
|
+
fingerprint =
|
|
1964
|
+
(fingerprint + getEntryFingerprint(functionValue.domain[index], functionValue.range[index])) |
|
|
1965
|
+
0;
|
|
942
1966
|
}
|
|
943
1967
|
return fingerprint;
|
|
944
1968
|
};
|
|
@@ -971,7 +1995,7 @@ export const getValueKey = (value: Value): string => {
|
|
|
971
1995
|
return key;
|
|
972
1996
|
};
|
|
973
1997
|
|
|
974
|
-
const computeValueKey = (value: ObjectValue)
|
|
1998
|
+
const computeValueKey = (value: ObjectValue) => {
|
|
975
1999
|
if (value.kind === "ModelValue") return `@${value.name}`;
|
|
976
2000
|
if (value.kind === "InfiniteSet") return `<${value.name}>`;
|
|
977
2001
|
if (value.kind === "SequenceSet") return `<Seq ${getValueKey(value.base)}>`;
|
|
@@ -984,42 +2008,95 @@ const computeValueKey = (value: ObjectValue): string => {
|
|
|
984
2008
|
if (!isEnumerable(value)) return `<${value.kind} ${formatValue(value)}>`;
|
|
985
2009
|
return `{${toFiniteSet(value).elements.map(getValueKey).join(",")}}`;
|
|
986
2010
|
}
|
|
987
|
-
const
|
|
988
|
-
return `(${
|
|
2011
|
+
const functionValue = toFiniteFunction(value);
|
|
2012
|
+
return `(${functionValue.domain.map((key, index) => `${getValueKey(key)}:${getValueKey(functionValue.range[index])}`).join(",")})`;
|
|
989
2013
|
};
|
|
990
2014
|
|
|
991
2015
|
const formatString = (value: string) =>
|
|
992
2016
|
`"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\t/g, "\\t")}"`;
|
|
993
2017
|
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
2018
|
+
type LazySetValue =
|
|
2019
|
+
| SubsetValue
|
|
2020
|
+
| FunctionSetValue
|
|
2021
|
+
| RecordSetValue
|
|
2022
|
+
| ProductSetValue
|
|
2023
|
+
| SetOperationValue
|
|
2024
|
+
| FilteredSetValue
|
|
2025
|
+
| UnionValue;
|
|
2026
|
+
|
|
2027
|
+
const MAXIMUM_EXPANDED_SUBSET_BASE = 6;
|
|
2028
|
+
const MAXIMUM_EXPANDED_PRODUCT = 1999;
|
|
2029
|
+
|
|
2030
|
+
const getProductSize = (sets: Value[]) =>
|
|
2031
|
+
sets.reduce<number>((size, set) => (size === 0 ? 0 : size * getCardinality(set)), 1);
|
|
2032
|
+
|
|
2033
|
+
const expandLazySet = (value: LazySetValue) => {
|
|
2034
|
+
if (value.kind === "Subset") {
|
|
2035
|
+
return getCardinality(value.base) <= MAXIMUM_EXPANDED_SUBSET_BASE
|
|
2036
|
+
? toFiniteSet(value)
|
|
2037
|
+
: undefined;
|
|
2038
|
+
}
|
|
2039
|
+
if (value.kind === "SetOperation" || value.kind === "FilteredSet" || value.kind === "Union") {
|
|
2040
|
+
return toFiniteSet(value);
|
|
2041
|
+
}
|
|
2042
|
+
if (
|
|
2043
|
+
value.kind === "FunctionSet" &&
|
|
2044
|
+
isEnumerable(value.domain) &&
|
|
2045
|
+
getCardinality(value.domain) === 0
|
|
2046
|
+
) {
|
|
2047
|
+
return createSortedSet([createSortedFunction([], [])]);
|
|
2048
|
+
}
|
|
2049
|
+
const size =
|
|
2050
|
+
value.kind === "FunctionSet"
|
|
2051
|
+
? getCardinality(value.range) ** getCardinality(value.domain)
|
|
2052
|
+
: getProductSize(value.kind === "RecordSet" ? value.sets : value.factors);
|
|
2053
|
+
if (size === 0) return EMPTY_SET;
|
|
2054
|
+
return size <= MAXIMUM_EXPANDED_PRODUCT ? toFiniteSet(value) : undefined;
|
|
2055
|
+
};
|
|
2056
|
+
|
|
2057
|
+
const getExpandedLazySet = (value: LazySetValue, isStrict: boolean) => {
|
|
2058
|
+
if (isStrict) return expandLazySet(value);
|
|
2059
|
+
// TLC prints a set it can't enumerate symbolically, except in Print, PrintT, and ToString.
|
|
2060
|
+
if (!isEnumerable(value)) return undefined;
|
|
2061
|
+
try {
|
|
2062
|
+
return expandLazySet(value);
|
|
2063
|
+
} catch (error) {
|
|
2064
|
+
if (error instanceof TlaEvaluationError) return undefined;
|
|
2065
|
+
throw error;
|
|
2066
|
+
}
|
|
2067
|
+
};
|
|
2068
|
+
|
|
2069
|
+
const formatLazySet = (value: LazySetValue, isStrict: boolean): string => {
|
|
2070
|
+
const format = (inner: Value) => formatValue(inner, isStrict);
|
|
1005
2071
|
switch (value.kind) {
|
|
1006
2072
|
case "Subset":
|
|
1007
|
-
return
|
|
2073
|
+
return typeof value.base === "object" && value.base.kind === "Interval"
|
|
2074
|
+
? `SUBSET (${format(value.base)})`
|
|
2075
|
+
: `SUBSET ${format(value.base)}`;
|
|
1008
2076
|
case "FunctionSet":
|
|
1009
2077
|
return `[${format(value.domain)} -> ${format(value.range)}]`;
|
|
1010
2078
|
case "RecordSet":
|
|
1011
|
-
return `[${value.names.map((name, index) => `${name}
|
|
2079
|
+
return `[${value.names.map((name, index) => `${name}: ${format(value.sets[index])}`).join(", ")}]`;
|
|
1012
2080
|
case "ProductSet":
|
|
1013
|
-
return value.factors.map(format).join(" \\X ")
|
|
2081
|
+
return `(${value.factors.map(format).join(" \\X ")})`;
|
|
1014
2082
|
case "SetOperation":
|
|
1015
2083
|
return `${format(value.left)} ${value.operator} ${format(value.right)}`;
|
|
1016
2084
|
case "FilteredSet":
|
|
1017
2085
|
return `{x \\in ${format(value.base)} : ...}`;
|
|
2086
|
+
case "Union":
|
|
2087
|
+
return formatUnion(value, isStrict);
|
|
1018
2088
|
}
|
|
1019
2089
|
};
|
|
1020
2090
|
|
|
1021
|
-
|
|
1022
|
-
|
|
2091
|
+
// FcnRcdValue#isName: TLC prints a function as a record when every key consists of letters,
|
|
2092
|
+
// decimal digits, and underscores, with at least one letter.
|
|
2093
|
+
const isFieldName = (name: string) =>
|
|
2094
|
+
/^[\p{L}\p{Nd}_]*$/u.test(name) &&
|
|
2095
|
+
/\p{L}/u.test(name) &&
|
|
2096
|
+
(name.length < 4 || !/^[WS]F_/.test(name));
|
|
2097
|
+
|
|
2098
|
+
export const formatValue = (value: Value, isStrict = false, isNormalized = false): string => {
|
|
2099
|
+
const format = (inner: Value) => formatValue(inner, isStrict, isNormalized);
|
|
1023
2100
|
if (typeof value === "boolean") return value ? "TRUE" : "FALSE";
|
|
1024
2101
|
if (typeof value === "number") return String(value);
|
|
1025
2102
|
if (typeof value === "string") return formatString(value);
|
|
@@ -1028,8 +2105,10 @@ export const formatValue = (value: Value, isExpanded = false): string => {
|
|
|
1028
2105
|
return value.name;
|
|
1029
2106
|
case "Set":
|
|
1030
2107
|
return `{${value.elements.map(format).join(", ")}}`;
|
|
2108
|
+
case "MixedSet":
|
|
2109
|
+
return format(toFiniteSet(value));
|
|
1031
2110
|
case "Interval":
|
|
1032
|
-
return `${value.low}..${value.high}
|
|
2111
|
+
return value.low <= value.high ? `${value.low}..${value.high}` : "{}";
|
|
1033
2112
|
case "InfiniteSet":
|
|
1034
2113
|
return value.name === "Any" ? "ANY" : value.name;
|
|
1035
2114
|
case "Subset":
|
|
@@ -1038,23 +2117,50 @@ export const formatValue = (value: Value, isExpanded = false): string => {
|
|
|
1038
2117
|
case "ProductSet":
|
|
1039
2118
|
case "SetOperation":
|
|
1040
2119
|
case "FilteredSet":
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
2120
|
+
case "Union": {
|
|
2121
|
+
const expanded = getExpandedLazySet(value, isStrict);
|
|
2122
|
+
return expanded ? format(expanded) : formatLazySet(value, isStrict);
|
|
2123
|
+
}
|
|
1044
2124
|
|
|
1045
2125
|
case "SequenceSet":
|
|
1046
2126
|
return `Seq(${format(value.base)})`;
|
|
1047
2127
|
case "Function":
|
|
1048
2128
|
case "LazyFunction": {
|
|
1049
|
-
if (value.kind === "LazyFunction" && !isEnumerable(value.domain))
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
2129
|
+
if (value.kind === "LazyFunction" && !isEnumerable(value.domain)) {
|
|
2130
|
+
const source = value.source;
|
|
2131
|
+
if (!source) return "<function>";
|
|
2132
|
+
const parameters = source.parameters
|
|
2133
|
+
.map((parameter) => `${parameter.name} \\in ${formatValue(parameter.domain)}`)
|
|
2134
|
+
.join(", ");
|
|
2135
|
+
return `[${parameters} |-> <expression ${source.body}>]`;
|
|
1053
2136
|
}
|
|
1054
|
-
|
|
1055
|
-
|
|
2137
|
+
const functionValue = toFiniteFunction(value);
|
|
2138
|
+
// TLC normalizes a lambda's domain to print it, but evaluates its body afterward.
|
|
2139
|
+
const formatRange = (inner: Value) =>
|
|
2140
|
+
formatValue(inner, isStrict, isNormalized && value.kind === "Function");
|
|
2141
|
+
if (functionValue.domain.every((key, index) => key === index + 1)) {
|
|
2142
|
+
return `<<${functionValue.range.map(formatRange).join(", ")}>>`;
|
|
1056
2143
|
}
|
|
1057
|
-
|
|
2144
|
+
if (functionValue.domain.every((key) => typeof key === "string" && isFieldName(key))) {
|
|
2145
|
+
const isSorted =
|
|
2146
|
+
isNormalized || functionValue.hash !== 0 || functionValue.fingerprint !== 0;
|
|
2147
|
+
const pairs = isSorted
|
|
2148
|
+
? functionValue.domain.map((key, index) => [key, functionValue.range[index]])
|
|
2149
|
+
: getWrittenPairs(functionValue);
|
|
2150
|
+
return `[${pairs.map((pair) => `${String(pair[0])} |-> ${formatRange(pair[1])}`).join(", ")}]`;
|
|
2151
|
+
}
|
|
2152
|
+
return `(${functionValue.domain.map((key, index) => `${format(key)} :> ${formatRange(functionValue.range[index])}`).join(" @@ ")})`;
|
|
1058
2153
|
}
|
|
1059
2154
|
}
|
|
1060
2155
|
};
|
|
2156
|
+
|
|
2157
|
+
// TLC's TLCState.toString: a line per variable, as a conjunction when there are several.
|
|
2158
|
+
export const formatState = (state: Record<string, Value>) => {
|
|
2159
|
+
const entries = Object.entries(state);
|
|
2160
|
+
return entries
|
|
2161
|
+
.map(
|
|
2162
|
+
([name, value]) =>
|
|
2163
|
+
`${entries.length === 1 ? "" : "/\\ "}${name} = ${prettyPrint(formatValue(value))}\n`,
|
|
2164
|
+
)
|
|
2165
|
+
.join("");
|
|
2166
|
+
};
|