tla-ts 0.0.3-dev.a57ca4f → 0.0.3-dev.a91da46
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +148 -8
- package/dist/index.d.ts +148 -8
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +1351 -20
- package/dist/run-cli.js +1351 -20
- package/dist/transfer.cjs +86 -28
- package/dist/transfer.js +86 -28
- package/package.json +5 -1
- package/src/ast.ts +52 -6
- package/src/bags.ts +4 -7
- package/src/builtins.ts +29 -11
- package/src/bundled-modules.ts +3 -3
- package/src/canonical.ts +4 -3
- package/src/check.ts +1358 -219
- package/src/cli.ts +8 -1
- package/src/codegen.ts +68 -18
- package/src/config.ts +252 -179
- package/src/environment.ts +451 -129
- package/src/errors.ts +217 -3
- package/src/evaluate.ts +723 -116
- package/src/exit-codes.ts +5 -4
- package/src/index.ts +1 -0
- package/src/instances.ts +4 -2
- package/src/java-overrides.ts +104 -25
- package/src/levels.ts +183 -37
- package/src/lexer.ts +32 -4
- package/src/live-check.ts +220 -103
- package/src/liveness.ts +192 -65
- package/src/obligation-expression.ts +2 -2
- package/src/obligation-source.ts +2 -0
- package/src/parallel-worker.ts +8 -3
- package/src/parallel.ts +56 -5
- package/src/parser.ts +436 -128
- package/src/pcal-ast.ts +770 -0
- package/src/pcal-parse.ts +1523 -0
- package/src/pcal-tokenize.ts +516 -0
- package/src/pcal-translate.ts +1230 -0
- package/src/pcal-validate.ts +454 -0
- package/src/randomization.ts +24 -7
- package/src/resolve.ts +1364 -405
- package/src/run-cli.ts +1167 -155
- package/src/runtime.ts +16 -0
- package/src/sany-builtins.ts +147 -0
- package/src/sany-generator.ts +3112 -0
- package/src/sany-levels.ts +1208 -0
- package/src/sany-lexer.ts +545 -0
- package/src/sany-linter.ts +122 -0
- package/src/sany-modules.ts +1398 -0
- package/src/sany-nodes.ts +463 -0
- package/src/sany-parse-error.ts +2161 -0
- package/src/sany-report.ts +354 -0
- package/src/sany-selectors.ts +738 -0
- package/src/sany-source.ts +248 -0
- package/src/sany-xml.ts +1017 -0
- package/src/spec-description.ts +26 -7
- package/src/standard-modules.ts +203 -146
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +29 -6
- package/src/subexpressions.ts +221 -161
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +160 -105
- package/src/symmetry.ts +180 -52
- package/src/tableau.ts +54 -25
- package/src/tlc-ext.ts +13 -17
- package/src/tlc-messages.ts +329 -0
- package/src/tlc-module.ts +46 -18
- package/src/tlc-order.ts +56 -6
- package/src/tlc-random.ts +37 -9
- package/src/tlc-replay.ts +655 -0
- package/src/tlc-summary.ts +20 -5
- package/src/tlc-version.ts +18 -0
- package/src/transfer.ts +15 -3
- package/src/value-sources.ts +29 -0
- package/src/values.ts +680 -178
package/src/evaluate.ts
CHANGED
|
@@ -13,44 +13,62 @@ import {
|
|
|
13
13
|
type SourceRange,
|
|
14
14
|
} from "./ast";
|
|
15
15
|
import { getKindString } from "./builtins";
|
|
16
|
-
import { TlaEvaluationError } from "./errors";
|
|
16
|
+
import { isStackOverflow, TlaEvaluationError, TlaUnassignedError } from "./errors";
|
|
17
17
|
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
18
18
|
import { generateEvaluator } from "./codegen";
|
|
19
|
+
import { MESSAGE_CODE } from "./tlc-messages";
|
|
19
20
|
import type { CheckerRuntime } from "./runtime";
|
|
20
21
|
import { getReadVariables } from "./dependencies";
|
|
21
22
|
import { contextualApplications, mapChildren, substitutedExpressions } from "./instances";
|
|
23
|
+
import { prettyPrint } from "./pretty-print";
|
|
22
24
|
import { isEnabled } from "./successors";
|
|
23
25
|
import {
|
|
24
26
|
applyFunction,
|
|
27
|
+
describeMissingArgument,
|
|
25
28
|
combineHash,
|
|
26
29
|
compareValues,
|
|
27
30
|
createFunction,
|
|
31
|
+
createNonIntegerKeyError,
|
|
32
|
+
createRecord,
|
|
28
33
|
createEnumeratedSet,
|
|
29
34
|
createSortedFunction,
|
|
35
|
+
createSortedRecord,
|
|
30
36
|
createSortedSet,
|
|
31
37
|
createWrittenRecord,
|
|
32
38
|
createTuple,
|
|
33
39
|
formatValue,
|
|
40
|
+
getCardinality,
|
|
34
41
|
getDomain,
|
|
35
42
|
getFunctionDomain,
|
|
36
43
|
getIntervalElements,
|
|
44
|
+
getLazySubsets,
|
|
45
|
+
getStreamedElements,
|
|
46
|
+
getFunctionClass,
|
|
37
47
|
getSequenceElements,
|
|
38
48
|
getValueHash,
|
|
49
|
+
isElementOf,
|
|
50
|
+
isEnumerable,
|
|
39
51
|
isFunctionValue,
|
|
40
52
|
isOverriddenSet,
|
|
41
53
|
isSameValue,
|
|
42
54
|
markLambdaFunction,
|
|
43
|
-
|
|
55
|
+
isReducibleSet,
|
|
44
56
|
isSetValue,
|
|
45
57
|
MAXIMUM_INTEGER,
|
|
46
58
|
MINIMUM_INTEGER,
|
|
59
|
+
toFiniteFunction,
|
|
47
60
|
toFiniteSet,
|
|
48
61
|
updateFunction,
|
|
49
62
|
valuesEqual,
|
|
63
|
+
type FilteredSetValue,
|
|
50
64
|
type LazyFunctionSource,
|
|
51
65
|
type LazyFunctionValue,
|
|
66
|
+
type ObjectValue,
|
|
52
67
|
type Value,
|
|
68
|
+
markNormalized,
|
|
69
|
+
setTrace,
|
|
53
70
|
} from "./values";
|
|
71
|
+
import type { TlcVersion } from "./tlc-version";
|
|
54
72
|
|
|
55
73
|
export type State = (Value | undefined)[];
|
|
56
74
|
|
|
@@ -73,6 +91,8 @@ export interface Builtin {
|
|
|
73
91
|
evaluateUnary: ((value: Value) => Value) | undefined;
|
|
74
92
|
evaluateBinary: ((left: Value, right: Value) => Value) | undefined;
|
|
75
93
|
isStateLevel?: boolean;
|
|
94
|
+
// Whether TLC takes an application for a constant one although its value depends on the state.
|
|
95
|
+
isLevelConstant?: boolean;
|
|
76
96
|
isLazy?: boolean;
|
|
77
97
|
}
|
|
78
98
|
|
|
@@ -109,18 +129,89 @@ export interface Environment {
|
|
|
109
129
|
builtins: Map<string, Builtin>;
|
|
110
130
|
constants: Map<string, Value>;
|
|
111
131
|
substitutions: Map<string, string>;
|
|
132
|
+
// The name SANY gives each definition that a named instance such as `M == INSTANCE Module`
|
|
133
|
+
// copies, such as M!Op, by the key of the copy.
|
|
134
|
+
instanceNames: Map<string, string>;
|
|
112
135
|
variables: string[];
|
|
113
136
|
variableIndex: Map<string, number>;
|
|
114
137
|
definitionValues: Map<Definition, Value>;
|
|
115
138
|
constantLevels: Map<Definition, boolean>;
|
|
139
|
+
// The definitions an INSTANCE copies from a module that declares constants or variables. SANY
|
|
140
|
+
// wraps the body of each in its substitutions.
|
|
141
|
+
substitutedDefinitions: Set<Definition>;
|
|
116
142
|
evaluators: WeakMap<Expression, Evaluator>;
|
|
143
|
+
// The expressions being evaluated, outermost first, when the environment replays an error to
|
|
144
|
+
// find where it occurred, as TLC's CallStackTool does.
|
|
145
|
+
callStack?: CallStack;
|
|
117
146
|
}
|
|
118
147
|
|
|
148
|
+
export interface CallStack {
|
|
149
|
+
expressions: Expression[];
|
|
150
|
+
// TLC stops popping the stack once an evaluation fails, so it keeps where the failure occurred.
|
|
151
|
+
isFrozen: boolean;
|
|
152
|
+
// The expression that built each value of the replay that TLC's CallStackTool gives a source.
|
|
153
|
+
sources: WeakMap<ObjectValue, Expression>;
|
|
154
|
+
// The value each expression of the replay last evaluated to.
|
|
155
|
+
values: WeakMap<Expression, Value>;
|
|
156
|
+
// The expression each failure last left, which tells a failure of an operand from one of the
|
|
157
|
+
// method that computes the operator.
|
|
158
|
+
failures: WeakMap<Error, Expression>;
|
|
159
|
+
// The failure of a value with a source, which TLC raises as a FingerprintException.
|
|
160
|
+
fingerprint?: FingerprintFailure;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export interface FingerprintFailure {
|
|
164
|
+
// The sources of the values whose methods failed, outermost first.
|
|
165
|
+
sources: Expression[];
|
|
166
|
+
// The message of the error the innermost one raised.
|
|
167
|
+
reason: string;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// The operators of Tool#evalAppl whose value TLC's CallStackTool gives a source.
|
|
171
|
+
const SOURCED_OPERATORS = new Set(["SUBSET", "UNION", "DOMAIN", "\\cup", "\\cap", "\\"]);
|
|
172
|
+
|
|
173
|
+
// CallStackTool copies the SetPredValue, FcnLambdaValue and OpLambdaValue that an evaluation
|
|
174
|
+
// returns without their source, and a function TLC builds from a FcnLambdaValue has none.
|
|
175
|
+
const SOURCED_KINDS = new Set<Expression["kind"]>([
|
|
176
|
+
"SetEnumeration",
|
|
177
|
+
"SetFilter",
|
|
178
|
+
"SetMap",
|
|
179
|
+
"Tuple",
|
|
180
|
+
"FunctionSet",
|
|
181
|
+
"Record",
|
|
182
|
+
"RecordSet",
|
|
183
|
+
"Product",
|
|
184
|
+
]);
|
|
185
|
+
|
|
186
|
+
const isSourced = (expression: Expression) =>
|
|
187
|
+
SOURCED_KINDS.has(expression.kind) ||
|
|
188
|
+
(expression.kind === "Apply" && SOURCED_OPERATORS.has(expression.operator));
|
|
189
|
+
|
|
190
|
+
// The argument whose method Tool#evalAppl calls to compute an operator, as with S.member(e) for
|
|
191
|
+
// e \in S, f.apply(e) for f[e] when f is a function, or S.diff(T) for S \ T when S is Reducible.
|
|
192
|
+
const getReceiver = (expression: Expression, values: CallStack["values"]) => {
|
|
193
|
+
if (expression.kind === "FunctionApplication" || expression.kind === "Except") {
|
|
194
|
+
return expression.function;
|
|
195
|
+
}
|
|
196
|
+
if (expression.kind !== "Apply" || expression.args.length !== 2) return undefined;
|
|
197
|
+
if (["\\subseteq", "=", "#", "\\"].includes(expression.operator)) return expression.args[0];
|
|
198
|
+
if (expression.operator === "\\cap" || expression.operator === "\\cup") {
|
|
199
|
+
const left = values.get(expression.args[0]);
|
|
200
|
+
return left !== undefined && isReducibleSet(left) ? expression.args[0] : expression.args[1];
|
|
201
|
+
}
|
|
202
|
+
return expression.operator === "\\in" || expression.operator === "\\notin"
|
|
203
|
+
? expression.args[1]
|
|
204
|
+
: undefined;
|
|
205
|
+
};
|
|
206
|
+
|
|
119
207
|
export interface Context {
|
|
120
208
|
environment: Environment;
|
|
121
209
|
scope: Scope | undefined;
|
|
122
210
|
state: State | undefined;
|
|
123
211
|
next: State | undefined;
|
|
212
|
+
// EvalControl.KeepLazy, which Tool#evalAppl sets to evaluate the function it applies and which
|
|
213
|
+
// the evaluation passes on, so that a function constructor within builds a FcnLambdaValue.
|
|
214
|
+
isKeepLazy?: boolean;
|
|
124
215
|
}
|
|
125
216
|
|
|
126
217
|
export type Evaluator = (context: Context) => Value;
|
|
@@ -151,14 +242,20 @@ export const describeRange = (expression: Expression) => {
|
|
|
151
242
|
return range ? formatRange(range) : describeLocation(expression);
|
|
152
243
|
};
|
|
153
244
|
|
|
245
|
+
// SANY prints a numeral as written and any other expression by its location.
|
|
246
|
+
export const describeNode = (expression: Expression) =>
|
|
247
|
+
expression.kind === "Number"
|
|
248
|
+
? (expression.text ?? String(expression.value))
|
|
249
|
+
: describeRange(expression);
|
|
250
|
+
|
|
154
251
|
export const getConditionError = (expression: Expression, value: Value) =>
|
|
155
252
|
new TlaEvaluationError(
|
|
156
253
|
`A non-boolean expression (${getKindString(value)}) was used as the condition of an IF.\n${describeRange(expression)}`,
|
|
157
254
|
);
|
|
158
255
|
|
|
159
|
-
// TLC parses an infix chain of /\ as nested binary
|
|
160
|
-
//
|
|
161
|
-
export const
|
|
256
|
+
// TLC parses an infix chain of /\ or \/ as nested binary junctions and a bulleted list as one
|
|
257
|
+
// junction list, and it words the error differently for each.
|
|
258
|
+
export const getJunctionError = (
|
|
162
259
|
expression: Expression & { kind: "And" | "Or" },
|
|
163
260
|
index: number,
|
|
164
261
|
value: Value,
|
|
@@ -168,22 +265,48 @@ export const getConjunctError = (
|
|
|
168
265
|
range?.start.line === expression.location.line &&
|
|
169
266
|
range.start.column === expression.location.column;
|
|
170
267
|
if (isBulleted || !range) {
|
|
171
|
-
const operand = expression.operands[index];
|
|
172
|
-
// SANY prints a numeral as its value instead of its location.
|
|
173
|
-
const description = operand.kind === "Number" ? String(operand.value) : describeRange(operand);
|
|
174
268
|
return new TlaEvaluationError(
|
|
175
|
-
`A non-boolean expression (${getKindString(value)}) was used as a formula in a conjunction.\n${
|
|
269
|
+
`A non-boolean expression (${getKindString(value)}) was used as a formula in a ${expression.kind === "And" ? "conjunction" : "disjunction"}.\n${describeNode(expression.operands[index])}`,
|
|
176
270
|
);
|
|
177
271
|
}
|
|
178
|
-
const end = expression.
|
|
272
|
+
const end = expression.prefixEnds?.[Math.max(index, 1) - 1] ?? range.end;
|
|
179
273
|
return new TlaEvaluationError(
|
|
180
|
-
`Attempted to evaluate an expression of form P /\\ Q when ${index === 0 ? "P" : "Q"} was\n${getKindString(value)}.\n${describeRange({ ...expression, location: { ...expression.location, range: { ...range, end } } })}`,
|
|
274
|
+
`Attempted to evaluate an expression of form P ${expression.kind === "And" ? "/\\" : "\\/"} Q when ${index === 0 ? "P" : "Q"} was\n${getKindString(value)}.\n${describeRange({ ...expression, location: { ...expression.location, range: { ...range, end } } })}`,
|
|
181
275
|
);
|
|
182
276
|
};
|
|
183
277
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
278
|
+
// TLC selects a field of a record, and of another function from the function it converts it to,
|
|
279
|
+
// which for a tuple keeps its domain as an interval.
|
|
280
|
+
export const getFieldSelector = (expression: Expression & { kind: "FieldAccess" }) => {
|
|
281
|
+
const field = expression.field;
|
|
282
|
+
const describeMissing = (record: Value) =>
|
|
283
|
+
`Attempted to select nonexistent field "${field}" from the record\n${formatValue(record)}\n${describeRange(expression)}`;
|
|
284
|
+
const describeNotInDomain = (functionValue: Value) =>
|
|
285
|
+
`Attempted to apply function:\n${prettyPrint(formatValue(functionValue))}\nto argument "${field}", which is not in the domain of the function.`;
|
|
286
|
+
return (value: Value) => {
|
|
287
|
+
if (!isFunctionValue(value)) {
|
|
288
|
+
throw new TlaEvaluationError(
|
|
289
|
+
`Attempted to select field "${field}" from a non-record value ${prettyPrint(formatValue(value))}\n${describeRange(expression)}`,
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
const functionClass = getFunctionClass(value);
|
|
293
|
+
if (functionClass === "tuple") throw createNonIntegerKeyError(field);
|
|
294
|
+
// Only TLC's RecordValue names the field it lacks. Any other function, such as a FcnRcdValue
|
|
295
|
+
// whose keys are strings, reports an argument outside its domain.
|
|
296
|
+
return applyFunction(
|
|
297
|
+
value,
|
|
298
|
+
field,
|
|
299
|
+
functionClass === "record" ? describeMissing : describeNotInDomain,
|
|
300
|
+
);
|
|
301
|
+
};
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
// TLC 1.7.4 words what a tuple or a record lacks differently.
|
|
305
|
+
export const getMissingArgumentDescriber = (version: TlcVersion | undefined) =>
|
|
306
|
+
version === "1.7.4"
|
|
307
|
+
? (functionValue: Value, argument: Value) =>
|
|
308
|
+
describeMissingArgument(functionValue, argument, version)
|
|
309
|
+
: undefined;
|
|
187
310
|
|
|
188
311
|
export const getDomainOperator = (expression: Expression) => (value: Value) => {
|
|
189
312
|
if (!isFunctionValue(value)) {
|
|
@@ -248,11 +371,23 @@ const bindPattern = (scope: Scope | undefined, pattern: BoundPattern, value: Val
|
|
|
248
371
|
);
|
|
249
372
|
};
|
|
250
373
|
|
|
374
|
+
// Tool#evalAppl checks that it applies a function before it evaluates the argument.
|
|
375
|
+
export const expectFunction = (value: Value, application: Expression) => {
|
|
376
|
+
if (isFunctionValue(value)) return value;
|
|
377
|
+
throw new TlaEvaluationError(
|
|
378
|
+
`A non-function (${getKindString(value)}) was applied as a function.\n${describeRange(application)}`,
|
|
379
|
+
);
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
export const toKeepLazyContext = (context: Context): Context =>
|
|
383
|
+
context.isKeepLazy ? context : { ...context, isKeepLazy: true };
|
|
384
|
+
|
|
251
385
|
const withScope = (context: Context, scope: Scope | undefined): Context => ({
|
|
252
386
|
environment: context.environment,
|
|
253
387
|
scope,
|
|
254
388
|
state: context.state,
|
|
255
389
|
next: context.next,
|
|
390
|
+
isKeepLazy: context.isKeepLazy,
|
|
256
391
|
});
|
|
257
392
|
|
|
258
393
|
export const expectBoolean = (value: Value, description: string) => {
|
|
@@ -267,11 +402,26 @@ export const expectBoolean = (value: Value, description: string) => {
|
|
|
267
402
|
export const getDefinition = (name: string, environment: Environment) =>
|
|
268
403
|
environment.definitions.get(environment.substitutions.get(name) ?? name);
|
|
269
404
|
|
|
270
|
-
|
|
405
|
+
// TLC bounds the level of every action and temporal operator above constant, whatever its operands.
|
|
406
|
+
const NONCONSTANT_KINDS = new Set<Expression["kind"]>([
|
|
407
|
+
"Prime",
|
|
408
|
+
"Unchanged",
|
|
409
|
+
"Enabled",
|
|
410
|
+
"BoxAction",
|
|
411
|
+
"AngleAction",
|
|
412
|
+
"Always",
|
|
413
|
+
"Eventually",
|
|
414
|
+
"WeakFairness",
|
|
415
|
+
"StrongFairness",
|
|
416
|
+
]);
|
|
417
|
+
|
|
418
|
+
const NONCONSTANT_OPERATORS = new Set(["~>", "-+->", "\\cdot"]);
|
|
419
|
+
|
|
420
|
+
const isNonconstant = (expression: Expression, environment: Environment): boolean => {
|
|
271
421
|
if (
|
|
272
|
-
expression.kind
|
|
273
|
-
expression.kind === "
|
|
274
|
-
expression.kind === "
|
|
422
|
+
NONCONSTANT_KINDS.has(expression.kind) ||
|
|
423
|
+
(expression.kind === "UnboundedQuantifier" && expression.isTemporal === true) ||
|
|
424
|
+
(expression.kind === "Apply" && NONCONSTANT_OPERATORS.has(expression.operator))
|
|
275
425
|
) {
|
|
276
426
|
return true;
|
|
277
427
|
}
|
|
@@ -288,7 +438,7 @@ const isStateDependent = (expression: Expression, environment: Environment): boo
|
|
|
288
438
|
if (definition && !environment.constants.has(name) && !isConstantLevel(definition, environment))
|
|
289
439
|
return true;
|
|
290
440
|
}
|
|
291
|
-
return getChildren(expression).some((child) =>
|
|
441
|
+
return getChildren(expression).some((child) => isNonconstant(child, environment));
|
|
292
442
|
};
|
|
293
443
|
|
|
294
444
|
export const isConstantLevel = (definition: Definition, environment: Environment): boolean => {
|
|
@@ -296,9 +446,9 @@ export const isConstantLevel = (definition: Definition, environment: Environment
|
|
|
296
446
|
if (cached !== undefined) return cached;
|
|
297
447
|
environment.constantLevels.set(definition, true);
|
|
298
448
|
const isConstant =
|
|
299
|
-
!
|
|
449
|
+
!isNonconstant(definition.body, environment) &&
|
|
300
450
|
(definition.kind === "Operator" ||
|
|
301
|
-
!definition.bounds.some((bound) =>
|
|
451
|
+
!definition.bounds.some((bound) => isNonconstant(bound.set, environment)));
|
|
302
452
|
environment.constantLevels.set(definition, isConstant);
|
|
303
453
|
return isConstant;
|
|
304
454
|
};
|
|
@@ -321,38 +471,90 @@ const getBoundExplanation = (set: Value, text: string) => {
|
|
|
321
471
|
|
|
322
472
|
// TLC enumerates every set except the ones it implements in Java, so any other set fails as its own
|
|
323
473
|
// class does.
|
|
324
|
-
|
|
474
|
+
const checkEnumerable = (set: Value, expression: Expression, version: TlcVersion) => {
|
|
325
475
|
if (!isSetValue(set) || isOverriddenSet(set)) {
|
|
326
476
|
const text = formatValue(set);
|
|
327
|
-
|
|
477
|
+
// TLC 1.7.4 doesn't explain why it can't enumerate a set.
|
|
478
|
+
const explanation = version === "1.7.4" ? undefined : getBoundExplanation(set, text);
|
|
328
479
|
throw new TlaEvaluationError(
|
|
329
480
|
explanation === undefined
|
|
330
|
-
? `TLC encountered a non-enumerable quantifier bound\n${text}.\n${
|
|
331
|
-
: `TLC encountered the non-enumerable quantifier bound\n${text}\n${
|
|
481
|
+
? `TLC encountered a non-enumerable quantifier bound\n${text}.\n${describeNode(expression)}`
|
|
482
|
+
: `TLC encountered the non-enumerable quantifier bound\n${text}\n${describeNode(expression)}\n${explanation}`,
|
|
332
483
|
);
|
|
333
484
|
}
|
|
334
|
-
return typeof set === "object" && set.kind === "Interval"
|
|
335
|
-
? getIntervalElements(set)
|
|
336
|
-
: toFiniteSet(set).elements;
|
|
337
485
|
};
|
|
338
486
|
|
|
339
|
-
|
|
487
|
+
const getEnumeratedElements = (set: Value, expression: Expression, version: TlcVersion) => {
|
|
488
|
+
checkEnumerable(set, expression, version);
|
|
489
|
+
if (typeof set === "object" && set.kind === "Interval") return getIntervalElements(set);
|
|
490
|
+
return getLazySubsets(set) ?? toFiniteSet(set).elements;
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
// The elements Tool.contexts binds to the bound of a quantifier or a set map. TLC 1.7.4 streams a
|
|
494
|
+
// lazy set's elements, where later versions normalize the set first.
|
|
495
|
+
export const getContextElements = (set: Value, expression: Expression, version: TlcVersion) => {
|
|
496
|
+
if (version !== "1.7.4") return getEnumeratedElements(set, expression, version);
|
|
497
|
+
checkEnumerable(set, expression, version);
|
|
498
|
+
return getLazySubsets(set) ?? getStreamedElements(set);
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
// Like TLC, a quantifier keeps an interval as an interval, so that it can stop at the first witness
|
|
502
|
+
// in a large interval such as 1..2000000000 without listing it.
|
|
503
|
+
const getKeptInterval = (set: Value) => {
|
|
504
|
+
if (typeof set !== "object" || set.kind !== "Interval") return undefined;
|
|
505
|
+
getCardinality(set);
|
|
506
|
+
return set;
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
export const getQuantifierElements = (set: Value, expression: Expression, version: TlcVersion) =>
|
|
510
|
+
getKeptInterval(set) ?? getContextElements(set, expression, version);
|
|
511
|
+
|
|
512
|
+
// CHOOSE and a function constructor visit the elements of the normalized set.
|
|
513
|
+
const getNormalizedElements = (set: Value, expression: Expression, version: TlcVersion) =>
|
|
514
|
+
getKeptInterval(set) ?? getEnumeratedElements(set, expression, version);
|
|
515
|
+
|
|
516
|
+
const someElement = (
|
|
517
|
+
elements: ReturnType<typeof getQuantifierElements>,
|
|
518
|
+
visit: (element: Value) => boolean,
|
|
519
|
+
) => {
|
|
520
|
+
if (Array.isArray(elements)) return elements.some(visit);
|
|
521
|
+
for (let element = elements.low; element <= elements.high; element++) {
|
|
522
|
+
if (visit(element)) return true;
|
|
523
|
+
}
|
|
524
|
+
return false;
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
export const getBoundElements = (bounds: Bound[], context: Context) =>
|
|
528
|
+
bounds.map((bound) =>
|
|
529
|
+
getQuantifierElements(
|
|
530
|
+
evaluate(bound.set, context),
|
|
531
|
+
bound.set,
|
|
532
|
+
context.environment.runtime.version,
|
|
533
|
+
),
|
|
534
|
+
);
|
|
535
|
+
|
|
536
|
+
// Visits each binding of the bounds to the elements of their sets, until visit returns true.
|
|
537
|
+
export const forEachBoundElement = (
|
|
340
538
|
bounds: Bound[],
|
|
341
|
-
|
|
539
|
+
sets: ReturnType<typeof getBoundElements>,
|
|
540
|
+
scope: Scope | undefined,
|
|
342
541
|
visit: (scope: Scope | undefined) => boolean,
|
|
343
542
|
): boolean => {
|
|
344
|
-
const
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
if (index < 0) return visit(scope);
|
|
349
|
-
return sets[index].some((element) =>
|
|
350
|
-
visitBound(index - 1, bindPattern(scope, bounds[index].pattern, element)),
|
|
543
|
+
const visitBound = (index: number, bound: Scope | undefined): boolean => {
|
|
544
|
+
if (index < 0) return visit(bound);
|
|
545
|
+
return someElement(sets[index], (element) =>
|
|
546
|
+
visitBound(index - 1, bindPattern(bound, bounds[index].pattern, element)),
|
|
351
547
|
);
|
|
352
548
|
};
|
|
353
|
-
return visitBound(bounds.length - 1,
|
|
549
|
+
return visitBound(bounds.length - 1, scope);
|
|
354
550
|
};
|
|
355
551
|
|
|
552
|
+
export const forEachBinding = (
|
|
553
|
+
bounds: Bound[],
|
|
554
|
+
context: Context,
|
|
555
|
+
visit: (scope: Scope | undefined) => boolean,
|
|
556
|
+
) => forEachBoundElement(bounds, getBoundElements(bounds, context), context.scope, visit);
|
|
557
|
+
|
|
356
558
|
export const getBoundsDomain = (bounds: Bound[], context: Context): Value => {
|
|
357
559
|
const sets = bounds.map((bound) => evaluate(bound.set, context));
|
|
358
560
|
return sets.length === 1 ? sets[0] : { kind: "ProductSet", factors: sets };
|
|
@@ -367,6 +569,80 @@ export const bindArgument = (bounds: Bound[], scope: Scope | undefined, argument
|
|
|
367
569
|
);
|
|
368
570
|
};
|
|
369
571
|
|
|
572
|
+
// The parameters, body and scope of each function a constructor or a definition keeps lazy, whose
|
|
573
|
+
// body TLC's getInitStates and getNextStates walk for an argument, as they do a FcnLambdaValue's.
|
|
574
|
+
interface LazyFunction {
|
|
575
|
+
value: LazyFunctionValue;
|
|
576
|
+
bounds: Bound[];
|
|
577
|
+
body: Expression;
|
|
578
|
+
scope: Scope | undefined;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
const lazyFunctions = new WeakMap<LazyFunctionValue, LazyFunction>();
|
|
582
|
+
|
|
583
|
+
const keepLambda = (lambda: LazyFunction) => {
|
|
584
|
+
lazyFunctions.set(lambda.value, lambda);
|
|
585
|
+
return lambda.value;
|
|
586
|
+
};
|
|
587
|
+
|
|
588
|
+
export const getLazyFunction = (value: Value) =>
|
|
589
|
+
typeof value === "object" && value.kind === "LazyFunction" ? lazyFunctions.get(value) : undefined;
|
|
590
|
+
|
|
591
|
+
// Tool.getFcnContext: the scope of a lazy function's body for the arguments of an application. It
|
|
592
|
+
// ignores arguments beyond the parameters.
|
|
593
|
+
export const bindLazyArguments = (
|
|
594
|
+
lambda: LazyFunction,
|
|
595
|
+
expression: Expression & { kind: "FunctionApplication" },
|
|
596
|
+
context: Context,
|
|
597
|
+
) => {
|
|
598
|
+
const value = lambda.value;
|
|
599
|
+
const args = expression.args.map((arg) => evaluate(arg, context));
|
|
600
|
+
const argument = args.length === 1 ? args[0] : createTuple(args);
|
|
601
|
+
const describeArgument = (ordinal: string, element: Value, problem: string) =>
|
|
602
|
+
`In applying the function\n${prettyPrint(formatValue(value))},\nthe ${ordinal} is:\n${prettyPrint(formatValue(element))}${problem}.\n${describeRange(expression.function)}`;
|
|
603
|
+
const bounds = lambda.bounds;
|
|
604
|
+
if (bounds.length === 1) {
|
|
605
|
+
if (!isElementOf(argument, value.domain)) {
|
|
606
|
+
throw new TlaEvaluationError(
|
|
607
|
+
describeArgument("first argument", argument, "which is not in its domain"),
|
|
608
|
+
);
|
|
609
|
+
}
|
|
610
|
+
return bindArgument(bounds, lambda.scope, argument);
|
|
611
|
+
}
|
|
612
|
+
const elements = getSequenceElements(argument);
|
|
613
|
+
const domains =
|
|
614
|
+
typeof value.domain === "object" && value.domain.kind === "ProductSet"
|
|
615
|
+
? value.domain.factors
|
|
616
|
+
: [];
|
|
617
|
+
if (elements === undefined || elements.length < bounds.length) {
|
|
618
|
+
throw new TlaEvaluationError(
|
|
619
|
+
`Attempted to apply a function to an argument not in its domain.\n${describeRange(expression.function)}`,
|
|
620
|
+
);
|
|
621
|
+
}
|
|
622
|
+
bounds.forEach((bound, index) => {
|
|
623
|
+
const element = elements[index];
|
|
624
|
+
const ordinal = `argument number ${index + 1}`;
|
|
625
|
+
if (!isElementOf(element, domains[index])) {
|
|
626
|
+
throw new TlaEvaluationError(
|
|
627
|
+
describeArgument(
|
|
628
|
+
ordinal,
|
|
629
|
+
element,
|
|
630
|
+
`${bound.pattern.kind === "Name" ? "" : "\n"}which is not in its domain`,
|
|
631
|
+
),
|
|
632
|
+
);
|
|
633
|
+
}
|
|
634
|
+
if (
|
|
635
|
+
bound.pattern.kind === "Tuple" &&
|
|
636
|
+
getSequenceElements(element)?.length !== bound.pattern.names.length
|
|
637
|
+
) {
|
|
638
|
+
throw new TlaEvaluationError(
|
|
639
|
+
describeArgument(ordinal, element, "which does not match its formal parameter"),
|
|
640
|
+
);
|
|
641
|
+
}
|
|
642
|
+
});
|
|
643
|
+
return bindArgument(bounds, lambda.scope, createTuple(elements.slice(0, bounds.length)));
|
|
644
|
+
};
|
|
645
|
+
|
|
370
646
|
// TLC prints a function it can't enumerate as its parameters and the location of its body. It
|
|
371
647
|
// prints only the first name of a group such as `m, n \in S`.
|
|
372
648
|
interface LazyFunctionTemplate {
|
|
@@ -445,6 +721,22 @@ const createLazyFunction = (
|
|
|
445
721
|
return self;
|
|
446
722
|
};
|
|
447
723
|
|
|
724
|
+
const definitionSources = new WeakMap<FunctionDefinition, Expression>();
|
|
725
|
+
|
|
726
|
+
// The function constructor of a definition, which SANY locates at the whole definition.
|
|
727
|
+
const getDefinitionSource = (definition: FunctionDefinition) => {
|
|
728
|
+
const cached = definitionSources.get(definition);
|
|
729
|
+
if (cached) return cached;
|
|
730
|
+
const source: Expression = {
|
|
731
|
+
kind: "FunctionConstruction",
|
|
732
|
+
bounds: definition.bounds,
|
|
733
|
+
body: definition.body,
|
|
734
|
+
location: definition.location,
|
|
735
|
+
};
|
|
736
|
+
definitionSources.set(definition, source);
|
|
737
|
+
return source;
|
|
738
|
+
};
|
|
739
|
+
|
|
448
740
|
const createRecursiveFunction = (definition: FunctionDefinition, context: Context) => {
|
|
449
741
|
const domain = getBoundsDomain(definition.bounds, context);
|
|
450
742
|
const functionValue = createLazyFunction(
|
|
@@ -459,13 +751,44 @@ const createRecursiveFunction = (definition: FunctionDefinition, context: Contex
|
|
|
459
751
|
),
|
|
460
752
|
getLazyFunctionSource(definition.bounds, definition.body, domain),
|
|
461
753
|
);
|
|
754
|
+
keepLambda({
|
|
755
|
+
value: functionValue,
|
|
756
|
+
bounds: definition.bounds,
|
|
757
|
+
body: definition.body,
|
|
758
|
+
scope: bindValue(undefined, definition.name, functionValue),
|
|
759
|
+
});
|
|
462
760
|
return definition.bounds.length === 1 ? markLambdaFunction(functionValue) : functionValue;
|
|
463
761
|
};
|
|
464
762
|
|
|
465
|
-
|
|
466
|
-
|
|
763
|
+
// TLC computes a constant definition when it starts, without keeping a function it constructs
|
|
764
|
+
// lazy, so a function over an interval or an enumerated set is a FcnRcdValue.
|
|
765
|
+
const toStartupValue = (definition: Definition, value: Value): Value => {
|
|
766
|
+
if (typeof value !== "object" || getFunctionClass(value) !== "lambda") return value;
|
|
767
|
+
if (
|
|
768
|
+
definition.kind === "Operator" ||
|
|
769
|
+
definition.isSelfReferential ||
|
|
770
|
+
value.kind !== "LazyFunction" ||
|
|
771
|
+
!isReducibleSet(value.domain)
|
|
772
|
+
) {
|
|
773
|
+
return value;
|
|
774
|
+
}
|
|
775
|
+
try {
|
|
776
|
+
const finite = toFiniteFunction(value);
|
|
777
|
+
return createSortedFunction(finite.domain, finite.range);
|
|
778
|
+
} catch (error) {
|
|
779
|
+
// TLC leaves a definition it fails to compute to evaluate where the spec uses it.
|
|
780
|
+
if (error instanceof TlaEvaluationError) return value;
|
|
781
|
+
throw error;
|
|
782
|
+
}
|
|
783
|
+
};
|
|
784
|
+
|
|
785
|
+
// TLC evaluates a function definition that isn't recursive as it does a function constructor.
|
|
786
|
+
const evaluateDefinitionBody = (definition: Definition, context: Context) => {
|
|
787
|
+
if (definition.kind !== "Function") return evaluate(definition.body, context);
|
|
788
|
+
return definition.isSelfReferential
|
|
467
789
|
? createRecursiveFunction(definition, context)
|
|
468
|
-
: evaluate(definition
|
|
790
|
+
: evaluate(getDefinitionSource(definition), context);
|
|
791
|
+
};
|
|
469
792
|
|
|
470
793
|
const evaluateDefinitionValue = (definition: Definition, context: Context) => {
|
|
471
794
|
const isConstant = isConstantLevel(definition, context.environment);
|
|
@@ -479,12 +802,35 @@ const evaluateDefinitionValue = (definition: Definition, context: Context) => {
|
|
|
479
802
|
const topLevel = withScope(context, undefined);
|
|
480
803
|
if (!isConstant) return evaluateDefinitionBody(definition, topLevel);
|
|
481
804
|
const value = context.environment.runtime.evaluateAtStartup(() =>
|
|
482
|
-
evaluateDefinitionBody(definition, topLevel),
|
|
805
|
+
toStartupValue(definition, evaluateDefinitionBody(definition, topLevel)),
|
|
483
806
|
);
|
|
484
807
|
context.environment.definitionValues.set(definition, value);
|
|
485
808
|
return value;
|
|
486
809
|
};
|
|
487
810
|
|
|
811
|
+
// TLC computes each constant definition the configuration doesn't replace when it starts, and
|
|
812
|
+
// leaves one it fails to compute to evaluate where the spec uses it.
|
|
813
|
+
export const evaluateStartupDefinitions = (definitions: Definition[], environment: Environment) =>
|
|
814
|
+
definitions.forEach((definition) => {
|
|
815
|
+
if (definition.kind === "Operator" && definition.params.length > 0) return;
|
|
816
|
+
const name = definition.name;
|
|
817
|
+
if (environment.constants.has(name) || environment.substitutions.has(name)) return;
|
|
818
|
+
if (!isConstantLevel(definition, environment)) return;
|
|
819
|
+
try {
|
|
820
|
+
markNormalized(
|
|
821
|
+
evaluateDefinitionValue(definition, {
|
|
822
|
+
environment,
|
|
823
|
+
scope: undefined,
|
|
824
|
+
state: undefined,
|
|
825
|
+
next: undefined,
|
|
826
|
+
}),
|
|
827
|
+
);
|
|
828
|
+
} catch (error) {
|
|
829
|
+
// TLC swallows whatever the definition throws, a stack overflow included.
|
|
830
|
+
if (!(error instanceof TlaEvaluationError) && !isStackOverflow(error)) throw error;
|
|
831
|
+
}
|
|
832
|
+
});
|
|
833
|
+
|
|
488
834
|
const forceThunk = (thunk: Thunk, context: Context) => {
|
|
489
835
|
const isSameState = thunk.state === context.state && thunk.next === context.next;
|
|
490
836
|
if (isSameState && thunk.cache !== undefined) return thunk.cache;
|
|
@@ -860,10 +1206,24 @@ const isImpureDefinition = (
|
|
|
860
1206
|
return isImpure;
|
|
861
1207
|
};
|
|
862
1208
|
|
|
1209
|
+
// UNION and \cup build sets that stream the sets they combine until TLC caches them, and TLC
|
|
1210
|
+
// builds a new one at each evaluation.
|
|
1211
|
+
const STREAMED_SET: Impurity = {
|
|
1212
|
+
builtins: new Set(["UNION", "\\cup"]),
|
|
1213
|
+
definitions: new WeakMap(),
|
|
1214
|
+
};
|
|
1215
|
+
|
|
863
1216
|
// TLC's next-state generator does not bind LET definitions to values, so each reference to one
|
|
864
|
-
// that calls an impure builtin
|
|
1217
|
+
// evaluates it again: one that calls an impure builtin repeats its effects, and a set one builds
|
|
1218
|
+
// with UNION or \cup starts out uncached.
|
|
865
1219
|
export const getReevaluatedDefinitions = (definitions: Definition[], environment: Environment) =>
|
|
866
|
-
new Set(
|
|
1220
|
+
new Set(
|
|
1221
|
+
definitions.filter(
|
|
1222
|
+
(definition) =>
|
|
1223
|
+
isImpureDefinition(definition, environment) ||
|
|
1224
|
+
isImpureDefinition(definition, environment, STREAMED_SET),
|
|
1225
|
+
),
|
|
1226
|
+
);
|
|
867
1227
|
|
|
868
1228
|
export const isImpure = (environment: Environment) =>
|
|
869
1229
|
[...environment.definitions.values()].some((definition) =>
|
|
@@ -939,7 +1299,11 @@ const computeIsClosedConstant = (
|
|
|
939
1299
|
return global?.kind === "Definition" && isEvaluatedOnce(global.definition, environment);
|
|
940
1300
|
}
|
|
941
1301
|
if (expression.kind === "Apply") {
|
|
942
|
-
if (
|
|
1302
|
+
if (
|
|
1303
|
+
isImpureBuiltin(expression.operator, environment) ||
|
|
1304
|
+
STREAMED_SET.builtins.has(expression.operator) ||
|
|
1305
|
+
expression.operator === "~>"
|
|
1306
|
+
) {
|
|
943
1307
|
return false;
|
|
944
1308
|
}
|
|
945
1309
|
const operator = resolveGlobalOperator(expression.operator, environment);
|
|
@@ -1011,12 +1375,127 @@ const isStateCached = (expression: Expression, environment: Environment) =>
|
|
|
1011
1375
|
getReadVariables(expression, environment) !== undefined &&
|
|
1012
1376
|
!hasFreeLocals(expression, NO_BOUND_NAMES);
|
|
1013
1377
|
|
|
1378
|
+
// How to rebuild a set filter TLC evaluates lazily so that its predicate runs in another
|
|
1379
|
+
// environment.
|
|
1380
|
+
const filteredSetRebinders = new WeakMap<FilteredSetValue, (environment: Environment) => Value>();
|
|
1381
|
+
|
|
1382
|
+
// TLC's CallStackTool rebinds the lazy values its evaluations return, so their later evaluation
|
|
1383
|
+
// records its call stack too.
|
|
1384
|
+
const traceEvaluator =
|
|
1385
|
+
(
|
|
1386
|
+
expression: Expression,
|
|
1387
|
+
evaluator: Evaluator,
|
|
1388
|
+
environment: Environment,
|
|
1389
|
+
callStack: CallStack,
|
|
1390
|
+
): Evaluator =>
|
|
1391
|
+
(context) => {
|
|
1392
|
+
callStack.expressions.push(expression);
|
|
1393
|
+
const outerSets = setTrace.sets;
|
|
1394
|
+
const outerCallStack = setTrace.callStack;
|
|
1395
|
+
const sets = outerSets ?? [];
|
|
1396
|
+
const depth = sets.length;
|
|
1397
|
+
setTrace.sets = sets;
|
|
1398
|
+
setTrace.callStack = callStack;
|
|
1399
|
+
try {
|
|
1400
|
+
const computed = evaluator(context);
|
|
1401
|
+
const rebind =
|
|
1402
|
+
typeof computed === "object" && computed.kind === "FilteredSet"
|
|
1403
|
+
? filteredSetRebinders.get(computed)
|
|
1404
|
+
: undefined;
|
|
1405
|
+
const value = rebind ? rebind(environment) : computed;
|
|
1406
|
+
callStack.values.set(expression, value);
|
|
1407
|
+
if (typeof value === "object" && value.kind !== "FilteredSet" && isSourced(expression)) {
|
|
1408
|
+
callStack.sources.set(value, expression);
|
|
1409
|
+
}
|
|
1410
|
+
return value;
|
|
1411
|
+
} catch (error) {
|
|
1412
|
+
const failed = error instanceof Error ? callStack.failures.get(error) : undefined;
|
|
1413
|
+
if (error instanceof Error) callStack.failures.set(error, expression);
|
|
1414
|
+
const receiver = getReceiver(expression, callStack.values);
|
|
1415
|
+
const received =
|
|
1416
|
+
receiver && !getChildren(expression).some((child) => child === failed)
|
|
1417
|
+
? callStack.values.get(receiver)
|
|
1418
|
+
: undefined;
|
|
1419
|
+
const value =
|
|
1420
|
+
expression.kind === "FunctionApplication" &&
|
|
1421
|
+
received !== undefined &&
|
|
1422
|
+
!isFunctionValue(received)
|
|
1423
|
+
? undefined
|
|
1424
|
+
: received;
|
|
1425
|
+
freezeCallStack(
|
|
1426
|
+
callStack,
|
|
1427
|
+
[value, ...sets.slice(depth).filter((set) => set !== value)],
|
|
1428
|
+
error,
|
|
1429
|
+
);
|
|
1430
|
+
throw error;
|
|
1431
|
+
} finally {
|
|
1432
|
+
if (!callStack.isFrozen) callStack.expressions.pop();
|
|
1433
|
+
sets.length = depth;
|
|
1434
|
+
setTrace.sets = outerSets;
|
|
1435
|
+
setTrace.callStack = outerCallStack;
|
|
1436
|
+
}
|
|
1437
|
+
};
|
|
1438
|
+
|
|
1439
|
+
// A method of a value with a source raises a FingerprintException, which each value with a source
|
|
1440
|
+
// whose method called it heads in turn. CallStack#freeze adds their sources to the stack, outermost
|
|
1441
|
+
// first. The values are those whose methods failed, outermost first.
|
|
1442
|
+
export const freezeCallStack = (
|
|
1443
|
+
callStack: CallStack,
|
|
1444
|
+
values: (Value | undefined)[],
|
|
1445
|
+
error: unknown,
|
|
1446
|
+
) => {
|
|
1447
|
+
// A set that streams its elements enumerates itself too, so it can appear twice.
|
|
1448
|
+
const sources = [...new Set(values)].flatMap((item) => {
|
|
1449
|
+
const source = typeof item === "object" ? callStack.sources.get(item) : undefined;
|
|
1450
|
+
return source ? [source] : [];
|
|
1451
|
+
});
|
|
1452
|
+
// A value with a source raises a FingerprintException even when the stack is frozen already.
|
|
1453
|
+
if (sources.length > 0 && error instanceof TlaEvaluationError) {
|
|
1454
|
+
if (!callStack.isFrozen) callStack.expressions.push(...sources);
|
|
1455
|
+
const inner = callStack.fingerprint;
|
|
1456
|
+
callStack.fingerprint = {
|
|
1457
|
+
sources: [...sources, ...(inner?.sources ?? [])],
|
|
1458
|
+
reason: inner?.reason ?? error.message,
|
|
1459
|
+
};
|
|
1460
|
+
}
|
|
1461
|
+
callStack.isFrozen = true;
|
|
1462
|
+
};
|
|
1463
|
+
|
|
1464
|
+
// A FingerprintException that a method of a value with a source raises, or that passes through one,
|
|
1465
|
+
// takes the value's source as its new head.
|
|
1466
|
+
const headFingerprint = (callStack: CallStack, source: Expression, error: unknown) => {
|
|
1467
|
+
if (!(error instanceof TlaEvaluationError)) return;
|
|
1468
|
+
if (!callStack.isFrozen) callStack.expressions.push(source);
|
|
1469
|
+
callStack.isFrozen = true;
|
|
1470
|
+
const inner = callStack.fingerprint;
|
|
1471
|
+
callStack.fingerprint = {
|
|
1472
|
+
sources: [source, ...(inner?.sources ?? [])],
|
|
1473
|
+
reason: inner?.reason ?? error.message,
|
|
1474
|
+
};
|
|
1475
|
+
};
|
|
1476
|
+
|
|
1477
|
+
// A copy of the environment that records the call stack of each evaluation, with evaluators of its
|
|
1478
|
+
// own since the environment's evaluators record nothing.
|
|
1479
|
+
export const createTracingEnvironment = (environment: Environment): Environment => ({
|
|
1480
|
+
...environment,
|
|
1481
|
+
evaluators: new WeakMap(),
|
|
1482
|
+
callStack: {
|
|
1483
|
+
expressions: [],
|
|
1484
|
+
isFrozen: false,
|
|
1485
|
+
sources: new WeakMap(),
|
|
1486
|
+
values: new WeakMap(),
|
|
1487
|
+
failures: new WeakMap(),
|
|
1488
|
+
},
|
|
1489
|
+
});
|
|
1490
|
+
|
|
1014
1491
|
export const getEvaluator = (expression: Expression, environment: Environment): Evaluator => {
|
|
1015
1492
|
const cached = environment.evaluators.get(expression);
|
|
1016
1493
|
if (cached) return cached;
|
|
1017
1494
|
const compiled = compile(expression, environment);
|
|
1018
|
-
const
|
|
1019
|
-
|
|
1495
|
+
const callStack = environment.callStack;
|
|
1496
|
+
const evaluator = callStack
|
|
1497
|
+
? traceEvaluator(expression, compiled, environment, callStack)
|
|
1498
|
+
: isWorthCaching(expression) && isClosedConstant(expression, environment)
|
|
1020
1499
|
? cacheFirstValue(compiled)
|
|
1021
1500
|
: isStateCached(expression, environment)
|
|
1022
1501
|
? cacheStateValue(compiled)
|
|
@@ -1028,6 +1507,7 @@ export const getEvaluator = (expression: Expression, environment: Environment):
|
|
|
1028
1507
|
const entryEvaluators = new WeakMap<Expression, Evaluator>();
|
|
1029
1508
|
|
|
1030
1509
|
const getEntryEvaluator = (expression: Expression, environment: Environment) => {
|
|
1510
|
+
if (environment.callStack) return getEvaluator(expression, environment);
|
|
1031
1511
|
const cached = entryEvaluators.get(expression);
|
|
1032
1512
|
if (cached) return cached;
|
|
1033
1513
|
const evaluator =
|
|
@@ -1251,14 +1731,21 @@ const compileClosureCall = (
|
|
|
1251
1731
|
const compiled = { body: undefined as Evaluator | undefined };
|
|
1252
1732
|
const evaluateBody = (scope: Scope | undefined, context: Context) => {
|
|
1253
1733
|
compiled.body ??= getEntryEvaluator(closure.body, environment);
|
|
1254
|
-
return compiled.body({
|
|
1734
|
+
return compiled.body({
|
|
1735
|
+
environment,
|
|
1736
|
+
scope,
|
|
1737
|
+
state: context.state,
|
|
1738
|
+
next: context.next,
|
|
1739
|
+
isKeepLazy: context.isKeepLazy,
|
|
1740
|
+
});
|
|
1255
1741
|
};
|
|
1256
1742
|
if (!isMemoized) return (context) => evaluateBody(bindArguments(context), context);
|
|
1257
1743
|
const memo = getCallMemo(closure);
|
|
1258
1744
|
return (context) => {
|
|
1259
1745
|
const scope = bindArguments(context);
|
|
1260
1746
|
const values = getArgumentValues(scope, args.length);
|
|
1261
|
-
|
|
1747
|
+
// A call TLC evaluates with EvalControl.KeepLazy builds what a memoized one wouldn't.
|
|
1748
|
+
if (!values || context.isKeepLazy) return evaluateBody(scope, context);
|
|
1262
1749
|
let hash = values.length;
|
|
1263
1750
|
for (const value of values) hash = combineHash(hash, getValueHash(value));
|
|
1264
1751
|
const bucket = memo.calls.get(hash);
|
|
@@ -1295,7 +1782,9 @@ const compileApply = (
|
|
|
1295
1782
|
? undefined
|
|
1296
1783
|
: resolveGlobalOperator(expression.operator, environment);
|
|
1297
1784
|
if (global?.kind === "Closure") {
|
|
1298
|
-
|
|
1785
|
+
// The replay records the evaluation of each call, which TLC repeats.
|
|
1786
|
+
const isMemoized = !environment.callStack && isMemoizable(expression, environment);
|
|
1787
|
+
return compileClosureCall(global, args, environment, isMemoized);
|
|
1299
1788
|
}
|
|
1300
1789
|
if (global?.kind === "Builtin" && global.name === "DOMAIN") {
|
|
1301
1790
|
const operand = getEvaluator(args[0], environment);
|
|
@@ -1305,7 +1794,7 @@ const compileApply = (
|
|
|
1305
1794
|
// TLC sorts the value of a constant TLCEval argument only where no context binds a name.
|
|
1306
1795
|
if (global?.kind === "Builtin" && global.name === "TLCEval" && args.length === 1) {
|
|
1307
1796
|
const operand = getEvaluator(args[0], environment);
|
|
1308
|
-
if (contextualApplications.has(expression) ||
|
|
1797
|
+
if (contextualApplications.has(expression) || isNonconstant(args[0], environment)) {
|
|
1309
1798
|
return operand;
|
|
1310
1799
|
}
|
|
1311
1800
|
return (context) =>
|
|
@@ -1318,6 +1807,16 @@ const compileApply = (
|
|
|
1318
1807
|
return (context) => subset(left(context), right(context));
|
|
1319
1808
|
}
|
|
1320
1809
|
if (global?.kind === "Builtin") return compileBuiltinCall(global, args, environment);
|
|
1810
|
+
// TLC refuses action composition unless a Java property enables its incomplete implementation.
|
|
1811
|
+
if (expression.operator === "\\cdot" && args.length === 2) {
|
|
1812
|
+
return () => {
|
|
1813
|
+
throw new TlaEvaluationError(
|
|
1814
|
+
environment.runtime.version === "1.7.4"
|
|
1815
|
+
? "The current version of TLC does not support action composition."
|
|
1816
|
+
: "The current version of TLC does not support action composition. An incomplete implementation can be enabled via the tlc2.tool.impl.Tool.cdot=true java property.",
|
|
1817
|
+
);
|
|
1818
|
+
};
|
|
1819
|
+
}
|
|
1321
1820
|
return (context) => {
|
|
1322
1821
|
const operator = resolveOperator(expression.operator, context);
|
|
1323
1822
|
if (operator) return callOperator(operator, args, context);
|
|
@@ -1343,15 +1842,15 @@ const compileQuantifier = (
|
|
|
1343
1842
|
const name = bound.pattern.name;
|
|
1344
1843
|
const set = getEvaluator(bound.set, environment);
|
|
1345
1844
|
return (context) => {
|
|
1346
|
-
const
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
return isForall;
|
|
1845
|
+
const isFound = someElement(
|
|
1846
|
+
getQuantifierElements(set(context), bound.set, environment.runtime.version),
|
|
1847
|
+
(element) =>
|
|
1848
|
+
expectBoolean(
|
|
1849
|
+
body(withScope(context, bindValue(context.scope, name, element))),
|
|
1850
|
+
description,
|
|
1851
|
+
) !== isForall,
|
|
1852
|
+
);
|
|
1853
|
+
return isFound ? !isForall : isForall;
|
|
1355
1854
|
};
|
|
1356
1855
|
}
|
|
1357
1856
|
const bounds = expression.bounds;
|
|
@@ -1360,17 +1859,15 @@ const compileQuantifier = (
|
|
|
1360
1859
|
const sets = bounds.map((item) => getEvaluator(item.set, environment));
|
|
1361
1860
|
return (context) => {
|
|
1362
1861
|
const lists = sets.map((set, index) =>
|
|
1363
|
-
|
|
1862
|
+
getQuantifierElements(set(context), bounds[index].set, environment.runtime.version),
|
|
1364
1863
|
);
|
|
1365
1864
|
const visit = (index: number, scope: Scope | undefined): boolean => {
|
|
1366
1865
|
if (index < 0) {
|
|
1367
1866
|
return expectBoolean(body(withScope(context, scope)), description) !== isForall;
|
|
1368
1867
|
}
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
}
|
|
1373
|
-
return false;
|
|
1868
|
+
return someElement(lists[index], (element) =>
|
|
1869
|
+
visit(index - 1, bindValue(scope, names[index], element)),
|
|
1870
|
+
);
|
|
1374
1871
|
};
|
|
1375
1872
|
return visit(bounds.length - 1, context.scope) ? !isForall : isForall;
|
|
1376
1873
|
};
|
|
@@ -1418,34 +1915,26 @@ const compileFunctionConstruction = (
|
|
|
1418
1915
|
const bound = bounds[0];
|
|
1419
1916
|
const isSingleName = bounds.length === 1 && bound.pattern.kind === "Name";
|
|
1420
1917
|
const set = getEvaluator(bound.set, environment);
|
|
1421
|
-
|
|
1422
|
-
const domain = isSingleName ? set(context) : getBoundsDomain(bounds, context);
|
|
1423
|
-
// TLC builds the function only when it reduces every domain, and evaluates the body lazily
|
|
1424
|
-
// otherwise.
|
|
1425
|
-
const domains =
|
|
1426
|
-
typeof domain === "object" && domain.kind === "ProductSet" && bounds.length > 1
|
|
1427
|
-
? domain.factors
|
|
1428
|
-
: [domain];
|
|
1429
|
-
if (expression.isRecursive || isKeptLazy || !domains.every(isReducible)) {
|
|
1430
|
-
const functionValue = createLazyFunction(
|
|
1431
|
-
domain,
|
|
1432
|
-
(argument) => body(withScope(context, bindArgument(bounds, context.scope, argument))),
|
|
1433
|
-
getLazyFunctionSource(bounds, expression.body, domain),
|
|
1434
|
-
);
|
|
1435
|
-
return bounds.length === 1 ? markLambdaFunction(functionValue) : functionValue;
|
|
1436
|
-
}
|
|
1918
|
+
const construct = (domain: Value, context: Context) => {
|
|
1437
1919
|
if (isSingleName && bound.pattern.kind === "Name") {
|
|
1438
1920
|
const name = bound.pattern.name;
|
|
1439
1921
|
const keys = getFunctionDomain(domain);
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
keys.map((key) => body(withScope(context, bindValue(context.scope, name, key)))),
|
|
1444
|
-
),
|
|
1922
|
+
const functionValue = createSortedFunction(
|
|
1923
|
+
keys,
|
|
1924
|
+
keys.map((key) => body(withScope(context, bindValue(context.scope, name, key)))),
|
|
1445
1925
|
);
|
|
1926
|
+
// FcnLambdaValue#toFcnRcd builds a FcnRcdValue over an interval or an enumerated set.
|
|
1927
|
+
return isReducibleSet(domain) ? functionValue : markLambdaFunction(functionValue);
|
|
1446
1928
|
}
|
|
1447
1929
|
const pairs: [Value, Value][] = [];
|
|
1448
|
-
|
|
1930
|
+
const sets = bounds.map((item) =>
|
|
1931
|
+
getNormalizedElements(
|
|
1932
|
+
evaluate(item.set, context),
|
|
1933
|
+
item.set,
|
|
1934
|
+
context.environment.runtime.version,
|
|
1935
|
+
),
|
|
1936
|
+
);
|
|
1937
|
+
forEachBoundElement(bounds, sets, context.scope, (scope) => {
|
|
1449
1938
|
const key =
|
|
1450
1939
|
bounds.length === 1
|
|
1451
1940
|
? (lookupKey(bound.pattern, scope) ?? false)
|
|
@@ -1455,6 +1944,46 @@ const compileFunctionConstruction = (
|
|
|
1455
1944
|
});
|
|
1456
1945
|
return createFunction(pairs);
|
|
1457
1946
|
};
|
|
1947
|
+
const createLazy = (domain: Value, context: Context) => {
|
|
1948
|
+
const functionValue = keepLambda({
|
|
1949
|
+
value: createLazyFunction(
|
|
1950
|
+
domain,
|
|
1951
|
+
(argument) => body(withScope(context, bindArgument(bounds, context.scope, argument))),
|
|
1952
|
+
getLazyFunctionSource(bounds, expression.body, domain),
|
|
1953
|
+
),
|
|
1954
|
+
bounds,
|
|
1955
|
+
body: expression.body,
|
|
1956
|
+
scope: context.scope,
|
|
1957
|
+
});
|
|
1958
|
+
return bounds.length === 1 ? markLambdaFunction(functionValue) : functionValue;
|
|
1959
|
+
};
|
|
1960
|
+
const callStack = environment.callStack;
|
|
1961
|
+
return (context) => {
|
|
1962
|
+
const domain = isSingleName ? set(context) : getBoundsDomain(bounds, context);
|
|
1963
|
+
if (expression.isRecursive || isKeptLazy || context.isKeepLazy || !isEnumerable(domain)) {
|
|
1964
|
+
return createLazy(domain, context);
|
|
1965
|
+
}
|
|
1966
|
+
// TLC builds a function over a domain other than a set enumeration or an interval lazily, so
|
|
1967
|
+
// its body fails only where TLC forces it, as the replay does. Building it at once is faster
|
|
1968
|
+
// otherwise.
|
|
1969
|
+
if (!isReducibleSet(domain)) {
|
|
1970
|
+
if (callStack) return createLazy(domain, context);
|
|
1971
|
+
try {
|
|
1972
|
+
return construct(domain, context);
|
|
1973
|
+
} catch (error) {
|
|
1974
|
+
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
1975
|
+
return createLazy(domain, context);
|
|
1976
|
+
}
|
|
1977
|
+
}
|
|
1978
|
+
if (!callStack) return construct(domain, context);
|
|
1979
|
+
// TLC builds the function in a method of the FcnLambdaValue that CallStackTool gives a source.
|
|
1980
|
+
try {
|
|
1981
|
+
return construct(domain, context);
|
|
1982
|
+
} catch (error) {
|
|
1983
|
+
headFingerprint(callStack, expression, error);
|
|
1984
|
+
throw error;
|
|
1985
|
+
}
|
|
1986
|
+
};
|
|
1458
1987
|
};
|
|
1459
1988
|
|
|
1460
1989
|
const compileExcept = (
|
|
@@ -1500,7 +2029,20 @@ const compileExcept = (
|
|
|
1500
2029
|
};
|
|
1501
2030
|
return (context) => {
|
|
1502
2031
|
let current = evaluateFunction(context);
|
|
1503
|
-
for (const item of updates)
|
|
2032
|
+
for (const item of updates) {
|
|
2033
|
+
// TLC leaves the value as it is when an update's path is not in it, and warns.
|
|
2034
|
+
const found = { value: false };
|
|
2035
|
+
current = update(current, item.path, 0, context, (inner) => {
|
|
2036
|
+
found.value = true;
|
|
2037
|
+
return item.value(inner);
|
|
2038
|
+
});
|
|
2039
|
+
if (!found.value) {
|
|
2040
|
+
context.environment.runtime.warn(
|
|
2041
|
+
MESSAGE_CODE.exceptAppliedToUnknownField,
|
|
2042
|
+
`The EXCEPT was applied to non-existing fields of the value at\n${describeRange(expression.function)}`,
|
|
2043
|
+
);
|
|
2044
|
+
}
|
|
2045
|
+
}
|
|
1504
2046
|
return current;
|
|
1505
2047
|
};
|
|
1506
2048
|
};
|
|
@@ -1513,20 +2055,41 @@ const compileSetComprehension = (
|
|
|
1513
2055
|
const predicate = getEvaluator(expression.predicate, environment);
|
|
1514
2056
|
const set = getEvaluator(expression.bound.set, environment);
|
|
1515
2057
|
const pattern = expression.bound.pattern;
|
|
2058
|
+
const range = expression.predicate.location.range;
|
|
2059
|
+
const source = range && {
|
|
2060
|
+
names: pattern.kind === "Name" ? pattern.name : pattern.names.join(", "),
|
|
2061
|
+
predicate: formatRange(range),
|
|
2062
|
+
};
|
|
2063
|
+
const createTest = (evaluator: Evaluator, context: Context) => (element: Value) =>
|
|
2064
|
+
expectBoolean(
|
|
2065
|
+
evaluator(withScope(context, bindPattern(context.scope, pattern, element))),
|
|
2066
|
+
"a set filter predicate",
|
|
2067
|
+
);
|
|
2068
|
+
const createFiltered = (
|
|
2069
|
+
base: Value,
|
|
2070
|
+
context: Context,
|
|
2071
|
+
evaluator: Evaluator,
|
|
2072
|
+
): FilteredSetValue => {
|
|
2073
|
+
const filtered: FilteredSetValue = {
|
|
2074
|
+
kind: "FilteredSet",
|
|
2075
|
+
base,
|
|
2076
|
+
test: createTest(evaluator, context),
|
|
2077
|
+
source,
|
|
2078
|
+
};
|
|
2079
|
+
filteredSetRebinders.set(filtered, (tracing) =>
|
|
2080
|
+
createFiltered(
|
|
2081
|
+
base,
|
|
2082
|
+
{ ...context, environment: tracing },
|
|
2083
|
+
getEvaluator(expression.predicate, tracing),
|
|
2084
|
+
),
|
|
2085
|
+
);
|
|
2086
|
+
return filtered;
|
|
2087
|
+
};
|
|
1516
2088
|
return (context) => {
|
|
1517
2089
|
const base = set(context);
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
"a set filter predicate",
|
|
1522
|
-
);
|
|
1523
|
-
if (!isReducible(base)) {
|
|
1524
|
-
if (isSetValue(base)) return { kind: "FilteredSet", base, test };
|
|
1525
|
-
throw new TlaEvaluationError(
|
|
1526
|
-
`TLC encountered a non-enumerable quantifier bound ${formatValue(base)} at ${describeLocation(expression.bound.set)}`,
|
|
1527
|
-
);
|
|
1528
|
-
}
|
|
1529
|
-
return createSortedSet(toFiniteSet(base).elements.filter(test));
|
|
2090
|
+
// TLC filters any value it can't enumerate lazily, and fails only when it uses the result.
|
|
2091
|
+
if (!isReducibleSet(base)) return createFiltered(base, context, predicate);
|
|
2092
|
+
return createSortedSet(toFiniteSet(base).elements.filter(createTest(predicate, context)));
|
|
1530
2093
|
};
|
|
1531
2094
|
}
|
|
1532
2095
|
if (expression.kind === "SetMap") {
|
|
@@ -1549,10 +2112,18 @@ const compileSetComprehension = (
|
|
|
1549
2112
|
};
|
|
1550
2113
|
}
|
|
1551
2114
|
const body = getEvaluator(expression.body, environment);
|
|
1552
|
-
const
|
|
2115
|
+
const evaluateSet = getEvaluator(set, environment);
|
|
1553
2116
|
return (context) => {
|
|
2117
|
+
const base = evaluateSet(context);
|
|
2118
|
+
if (!isSetValue(base) || isOverriddenSet(base)) {
|
|
2119
|
+
throw new TlaEvaluationError(
|
|
2120
|
+
`Attempted to compute the value of an expression of\nform CHOOSE x \\in S: P, but S was not enumerable.\n${describeRange(expression)}`,
|
|
2121
|
+
);
|
|
2122
|
+
}
|
|
1554
2123
|
const chosen = { value: undefined as Value | undefined };
|
|
1555
|
-
|
|
2124
|
+
const elements = getNormalizedElements(base, set, context.environment.runtime.version);
|
|
2125
|
+
someElement(elements, (element) => {
|
|
2126
|
+
const scope = bindPattern(context.scope, expression.pattern, element);
|
|
1556
2127
|
if (!expectBoolean(body(withScope(context, scope)), "the body of CHOOSE")) return false;
|
|
1557
2128
|
chosen.value = lookupKey(expression.pattern, scope);
|
|
1558
2129
|
return true;
|
|
@@ -1573,8 +2144,21 @@ export const getSortedFields = (fields: RecordField[]) =>
|
|
|
1573
2144
|
export const hasDuplicateFields = (sortedFields: RecordField[]) =>
|
|
1574
2145
|
sortedFields.some((field, index) => index > 0 && sortedFields[index - 1].name === field.name);
|
|
1575
2146
|
|
|
2147
|
+
// TLC's Tool#evalAppl fails to look up a variable the state doesn't assign.
|
|
1576
2148
|
export const createUnassignedError = (name: string, expression: Expression) =>
|
|
1577
|
-
new
|
|
2149
|
+
new TlaUnassignedError(
|
|
2150
|
+
`In evaluation, the identifier ${name} is either undefined or not an operator.\n${describeRange(expression)}`,
|
|
2151
|
+
name,
|
|
2152
|
+
);
|
|
2153
|
+
|
|
2154
|
+
// TLC's Tool#enabled reads the subscript of <<A>>_v in the successor as a state of its own, and
|
|
2155
|
+
// names the fairness condition when A leaves a variable of v unassigned, unlike TLC 1.7.4's.
|
|
2156
|
+
export const toUnspecifiedSubscriptError = (error: unknown, version: TlcVersion | undefined) =>
|
|
2157
|
+
error instanceof TlaUnassignedError && version !== "1.7.4"
|
|
2158
|
+
? new TlaEvaluationError(
|
|
2159
|
+
`The action formula A appearing in a WF_v(A) or SF_v(A) operator does not specify the primed value of the variable ${error.variable} occurring in the state formula v.\n`,
|
|
2160
|
+
)
|
|
2161
|
+
: error;
|
|
1578
2162
|
|
|
1579
2163
|
export const createPrimeOutsideActionError = (expression: Expression) =>
|
|
1580
2164
|
new TlaEvaluationError(
|
|
@@ -1587,8 +2171,8 @@ const compileRecord = (recordFields: RecordField[], environment: Environment): E
|
|
|
1587
2171
|
const values = fields.map((field) => getEvaluator(field.value, environment));
|
|
1588
2172
|
if (hasDuplicates) {
|
|
1589
2173
|
return (context) =>
|
|
1590
|
-
|
|
1591
|
-
fields.map((field, index): [
|
|
2174
|
+
createRecord(
|
|
2175
|
+
fields.map((field, index): [string, Value] => [field.name, values[index](context)]),
|
|
1592
2176
|
);
|
|
1593
2177
|
}
|
|
1594
2178
|
const names = fields.map((field) => field.name);
|
|
@@ -1602,7 +2186,7 @@ const compileRecord = (recordFields: RecordField[], environment: Environment): E
|
|
|
1602
2186
|
);
|
|
1603
2187
|
}
|
|
1604
2188
|
return (context) =>
|
|
1605
|
-
|
|
2189
|
+
createSortedRecord(
|
|
1606
2190
|
names,
|
|
1607
2191
|
values.map((value) => value(context)),
|
|
1608
2192
|
);
|
|
@@ -1631,6 +2215,12 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1631
2215
|
return compileIdentifier(expression, environment);
|
|
1632
2216
|
case "Apply":
|
|
1633
2217
|
return compileApply(expression, environment);
|
|
2218
|
+
case "Bracket":
|
|
2219
|
+
return () => {
|
|
2220
|
+
throw new TlaEvaluationError(
|
|
2221
|
+
`A bracket without a function or subscript is not an expression at ${describeLocation(expression)}`,
|
|
2222
|
+
);
|
|
2223
|
+
};
|
|
1634
2224
|
case "OperatorReference":
|
|
1635
2225
|
case "Lambda":
|
|
1636
2226
|
return () => {
|
|
@@ -1643,7 +2233,7 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1643
2233
|
return (context) => {
|
|
1644
2234
|
for (let index = 0; index < operands.length; index++) {
|
|
1645
2235
|
const value = operands[index](context);
|
|
1646
|
-
if (typeof value !== "boolean") throw
|
|
2236
|
+
if (typeof value !== "boolean") throw getJunctionError(expression, index, value);
|
|
1647
2237
|
if (!value) return false;
|
|
1648
2238
|
}
|
|
1649
2239
|
return true;
|
|
@@ -1653,7 +2243,9 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1653
2243
|
const operands = expression.operands.map(child);
|
|
1654
2244
|
return (context) => {
|
|
1655
2245
|
for (let index = 0; index < operands.length; index++) {
|
|
1656
|
-
|
|
2246
|
+
const value = operands[index](context);
|
|
2247
|
+
if (typeof value !== "boolean") throw getJunctionError(expression, index, value);
|
|
2248
|
+
if (value) return true;
|
|
1657
2249
|
}
|
|
1658
2250
|
return false;
|
|
1659
2251
|
};
|
|
@@ -1667,6 +2259,7 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1667
2259
|
scope: context.scope,
|
|
1668
2260
|
state: context.next,
|
|
1669
2261
|
next: undefined,
|
|
2262
|
+
isKeepLazy: context.isKeepLazy,
|
|
1670
2263
|
});
|
|
1671
2264
|
};
|
|
1672
2265
|
}
|
|
@@ -1691,13 +2284,20 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1691
2284
|
}
|
|
1692
2285
|
case "Case": {
|
|
1693
2286
|
const arms = expression.arms.map((arm) => ({
|
|
2287
|
+
source: arm.condition,
|
|
1694
2288
|
condition: child(arm.condition),
|
|
1695
2289
|
body: child(arm.body),
|
|
1696
2290
|
}));
|
|
1697
2291
|
const other = expression.other ? child(expression.other) : undefined;
|
|
1698
2292
|
return (context) => {
|
|
1699
2293
|
for (const arm of arms) {
|
|
1700
|
-
|
|
2294
|
+
const value = arm.condition(context);
|
|
2295
|
+
if (typeof value !== "boolean") {
|
|
2296
|
+
throw new TlaEvaluationError(
|
|
2297
|
+
`A non-boolean expression (${getKindString(value)}) was used as a condition of a CASE. ${describeNode(arm.source)}`,
|
|
2298
|
+
);
|
|
2299
|
+
}
|
|
2300
|
+
if (value) return arm.body(context);
|
|
1701
2301
|
}
|
|
1702
2302
|
if (other) return other(context);
|
|
1703
2303
|
throw new TlaEvaluationError(
|
|
@@ -1760,20 +2360,27 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1760
2360
|
};
|
|
1761
2361
|
case "FunctionApplication": {
|
|
1762
2362
|
keepLazy(expression.function);
|
|
1763
|
-
const
|
|
2363
|
+
const evaluated = child(expression.function);
|
|
2364
|
+
const evaluateFunction = (context: Context) =>
|
|
2365
|
+
expectFunction(evaluated(toKeepLazyContext(context)), expression);
|
|
1764
2366
|
const args = expression.args.map(child);
|
|
2367
|
+
const describeMissing = getMissingArgumentDescriber(environment.runtime.version);
|
|
1765
2368
|
if (args.length === 1) {
|
|
1766
2369
|
const argument = args[0];
|
|
1767
|
-
return (context) =>
|
|
2370
|
+
return (context) =>
|
|
2371
|
+
applyFunction(evaluateFunction(context), argument(context), describeMissing);
|
|
1768
2372
|
}
|
|
1769
2373
|
return (context) =>
|
|
1770
|
-
applyFunction(
|
|
2374
|
+
applyFunction(
|
|
2375
|
+
evaluateFunction(context),
|
|
2376
|
+
createTuple(args.map((arg) => arg(context))),
|
|
2377
|
+
describeMissing,
|
|
2378
|
+
);
|
|
1771
2379
|
}
|
|
1772
2380
|
case "FieldAccess": {
|
|
1773
2381
|
const record = child(expression.record);
|
|
1774
|
-
const
|
|
1775
|
-
|
|
1776
|
-
return (context) => applyFunction(record(context), field, describeMissing);
|
|
2382
|
+
const select = getFieldSelector(expression);
|
|
2383
|
+
return (context) => select(record(context));
|
|
1777
2384
|
}
|
|
1778
2385
|
case "Product": {
|
|
1779
2386
|
const factors = expression.factors.map(child);
|