tla-ts 0.0.3-dev.6979965 → 0.0.3-dev.6ac5593
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 +16 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +72 -22
- package/dist/index.d.ts +72 -22
- 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 +20 -17
- package/dist/run-cli.js +22 -19
- package/dist/transfer.cjs +46 -16
- package/dist/transfer.js +46 -16
- package/package.json +1 -1
- package/src/ast.ts +121 -4
- package/src/builtins.ts +9 -3
- package/src/check.ts +521 -105
- package/src/cli-options.ts +11 -5
- package/src/codegen.ts +15 -22
- package/src/config.ts +224 -170
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +410 -130
- package/src/errors.ts +109 -3
- package/src/evaluate.ts +443 -194
- package/src/exit-codes.ts +39 -42
- package/src/fingerprints.ts +2 -1
- package/src/instances.ts +17 -16
- package/src/levels.ts +185 -44
- package/src/lexer.ts +5 -1
- package/src/live-check.ts +772 -0
- package/src/liveness.ts +120 -543
- package/src/parallel-worker.ts +102 -115
- package/src/parallel.ts +34 -23
- package/src/parser.ts +184 -54
- package/src/resolve.ts +1378 -420
- package/src/run-cli.ts +115 -20
- package/src/runtime.ts +3 -1
- package/src/sany-lexer.ts +615 -0
- package/src/sany-parse-error.ts +3143 -0
- package/src/spec-description.ts +19 -8
- package/src/standard-modules.ts +54 -45
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +231 -171
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +184 -40
- package/src/symmetry.ts +123 -43
- package/src/tableau.ts +51 -24
- package/src/tlc-ext.ts +29 -12
- package/src/tlc-module.ts +14 -33
- package/src/tlc-modules.ts +4 -0
- package/src/tlc-order.ts +25 -7
- package/src/tlc-random.ts +9 -6
- package/src/tlc-summary.ts +20 -5
- package/src/value-sources.ts +29 -0
- package/src/values.ts +249 -65
package/src/evaluate.ts
CHANGED
|
@@ -1,23 +1,25 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
isLocallyResolved,
|
|
4
|
+
type Bound,
|
|
5
|
+
type BoundPattern,
|
|
6
|
+
type DecimalExpression,
|
|
7
|
+
type Definition,
|
|
8
|
+
type Expression,
|
|
9
|
+
type FunctionDefinition,
|
|
10
|
+
type NumberExpression,
|
|
11
|
+
type OperatorParam,
|
|
12
|
+
type RecordField,
|
|
12
13
|
} from "./ast";
|
|
13
14
|
import { getKindString } from "./builtins";
|
|
14
|
-
import { TlaEvaluationError } from "./errors";
|
|
15
|
-
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
15
|
+
import { formatRange, TlaEvaluationError, TlaUnassignedError } from "./errors";
|
|
16
|
+
import { EXIT_STATUS, setExitCode, setOwnExitCode } from "./exit-codes";
|
|
16
17
|
import { generateEvaluator } from "./codegen";
|
|
17
18
|
import type { CheckerRuntime } from "./runtime";
|
|
18
19
|
import { getReadVariables } from "./dependencies";
|
|
19
20
|
import { contextualApplications, mapChildren, substitutedExpressions } from "./instances";
|
|
20
|
-
import { isEnabled } from "./successors";
|
|
21
|
+
import { getReadLocals, isEnabled } from "./successors";
|
|
22
|
+
import { getFingerprintTrace, valueSources, withSource } from "./value-sources";
|
|
21
23
|
import {
|
|
22
24
|
applyFunction,
|
|
23
25
|
combineHash,
|
|
@@ -28,23 +30,28 @@ import {
|
|
|
28
30
|
createSortedSet,
|
|
29
31
|
createWrittenRecord,
|
|
30
32
|
createTuple,
|
|
33
|
+
describeMissingField,
|
|
31
34
|
formatValue,
|
|
32
35
|
getDomain,
|
|
33
36
|
getFunctionDomain,
|
|
34
37
|
getIntervalElements,
|
|
38
|
+
getLazySubsets,
|
|
35
39
|
getSequenceElements,
|
|
36
40
|
getValueHash,
|
|
37
|
-
isEnumerable,
|
|
38
41
|
isFunctionValue,
|
|
39
42
|
isOverriddenSet,
|
|
43
|
+
isRecordValue,
|
|
40
44
|
isSameValue,
|
|
41
45
|
markLambdaFunction,
|
|
46
|
+
isReducible,
|
|
42
47
|
isSetValue,
|
|
43
48
|
MAXIMUM_INTEGER,
|
|
44
49
|
MINIMUM_INTEGER,
|
|
45
50
|
toFiniteSet,
|
|
46
51
|
updateFunction,
|
|
47
52
|
valuesEqual,
|
|
53
|
+
type FilteredSetValue,
|
|
54
|
+
type FunctionValue,
|
|
48
55
|
type LazyFunctionSource,
|
|
49
56
|
type LazyFunctionValue,
|
|
50
57
|
type Value,
|
|
@@ -112,6 +119,7 @@ export interface Environment {
|
|
|
112
119
|
definitionValues: Map<Definition, Value>;
|
|
113
120
|
constantLevels: Map<Definition, boolean>;
|
|
114
121
|
evaluators: WeakMap<Expression, Evaluator>;
|
|
122
|
+
trace?: Expression[];
|
|
115
123
|
}
|
|
116
124
|
|
|
117
125
|
export interface Context {
|
|
@@ -141,9 +149,6 @@ const EXCEPT_AT = "@";
|
|
|
141
149
|
export const describeLocation = (expression: Expression) =>
|
|
142
150
|
`line ${expression.location.line}, column ${expression.location.column}`;
|
|
143
151
|
|
|
144
|
-
export const formatRange = (range: SourceRange) =>
|
|
145
|
-
`line ${range.start.line}, col ${range.start.column} to line ${range.end.line}, col ${range.end.column} of module ${range.module}`;
|
|
146
|
-
|
|
147
152
|
export const describeRange = (expression: Expression) => {
|
|
148
153
|
const range = expression.location.range;
|
|
149
154
|
return range ? formatRange(range) : describeLocation(expression);
|
|
@@ -180,8 +185,17 @@ export const getConjunctError = (
|
|
|
180
185
|
};
|
|
181
186
|
|
|
182
187
|
export const getFieldError =
|
|
183
|
-
(expression: Expression & { kind: "FieldAccess" }) => (record:
|
|
184
|
-
|
|
188
|
+
(expression: Expression & { kind: "FieldAccess" }) => (record: FunctionValue) =>
|
|
189
|
+
isRecordValue(record)
|
|
190
|
+
? `Attempted to select nonexistent field "${expression.field}" from the record\n${formatValue(record)}\n${describeRange(expression)}`
|
|
191
|
+
: describeMissingField(record, expression.field);
|
|
192
|
+
|
|
193
|
+
export const getNonFunctionError = (expression: Expression) => (value: Value) =>
|
|
194
|
+
`A non-function (${getKindString(value)}) was applied as a function.\n${describeRange(expression)}`;
|
|
195
|
+
|
|
196
|
+
export const getNonRecordError =
|
|
197
|
+
(expression: Expression & { kind: "FieldAccess" }) => (value: Value) =>
|
|
198
|
+
`Attempted to select field "${expression.field}" from a non-record value ${formatValue(value)}\n${describeRange(expression)}`;
|
|
185
199
|
|
|
186
200
|
export const getDomainOperator = (expression: Expression) => (value: Value) => {
|
|
187
201
|
if (!isFunctionValue(value)) {
|
|
@@ -262,96 +276,6 @@ export const expectBoolean = (value: Value, description: string) => {
|
|
|
262
276
|
return value;
|
|
263
277
|
};
|
|
264
278
|
|
|
265
|
-
export const getChildren = (expression: Expression): Expression[] => {
|
|
266
|
-
switch (expression.kind) {
|
|
267
|
-
case "Number":
|
|
268
|
-
case "Decimal":
|
|
269
|
-
case "String":
|
|
270
|
-
case "Identifier":
|
|
271
|
-
case "OperatorReference":
|
|
272
|
-
case "ExceptAt":
|
|
273
|
-
return [];
|
|
274
|
-
case "Apply":
|
|
275
|
-
return expression.args;
|
|
276
|
-
case "Lambda":
|
|
277
|
-
return [expression.body];
|
|
278
|
-
case "And":
|
|
279
|
-
case "Or":
|
|
280
|
-
return expression.operands;
|
|
281
|
-
case "Prime":
|
|
282
|
-
case "Unchanged":
|
|
283
|
-
case "Enabled":
|
|
284
|
-
case "Always":
|
|
285
|
-
case "Eventually":
|
|
286
|
-
return [expression.expression];
|
|
287
|
-
case "If":
|
|
288
|
-
return [expression.condition, expression.consequent, expression.alternative];
|
|
289
|
-
case "Case":
|
|
290
|
-
return [
|
|
291
|
-
...expression.arms.flatMap((arm) => [arm.condition, arm.body]),
|
|
292
|
-
...(expression.other ? [expression.other] : []),
|
|
293
|
-
];
|
|
294
|
-
case "Let":
|
|
295
|
-
return [
|
|
296
|
-
...expression.definitions.flatMap((definition) =>
|
|
297
|
-
definition.kind === "Function"
|
|
298
|
-
? [...definition.bounds.map((bound) => bound.set), definition.body]
|
|
299
|
-
: [definition.body],
|
|
300
|
-
),
|
|
301
|
-
expression.body,
|
|
302
|
-
];
|
|
303
|
-
case "Forall":
|
|
304
|
-
case "Exists":
|
|
305
|
-
return [...expression.bounds.map((bound) => bound.set), expression.body];
|
|
306
|
-
case "UnboundedQuantifier":
|
|
307
|
-
return [expression.body];
|
|
308
|
-
case "Choose":
|
|
309
|
-
return [...(expression.set ? [expression.set] : []), expression.body];
|
|
310
|
-
case "SetEnumeration":
|
|
311
|
-
case "Tuple":
|
|
312
|
-
return expression.elements;
|
|
313
|
-
case "SetFilter":
|
|
314
|
-
return [expression.bound.set, expression.predicate];
|
|
315
|
-
case "SetMap":
|
|
316
|
-
return [expression.expression, ...expression.bounds.map((bound) => bound.set)];
|
|
317
|
-
case "FunctionConstruction":
|
|
318
|
-
return [...expression.bounds.map((bound) => bound.set), expression.body];
|
|
319
|
-
case "FunctionSet":
|
|
320
|
-
return [expression.domain, expression.range];
|
|
321
|
-
case "Record":
|
|
322
|
-
case "RecordSet":
|
|
323
|
-
return expression.fields.map((field) => field.value);
|
|
324
|
-
case "Except":
|
|
325
|
-
return [
|
|
326
|
-
expression.function,
|
|
327
|
-
...expression.updates.flatMap((update) => [
|
|
328
|
-
...update.path.flatMap((step) => (step.kind === "Index" ? step.args : [])),
|
|
329
|
-
update.value,
|
|
330
|
-
]),
|
|
331
|
-
];
|
|
332
|
-
case "FunctionApplication":
|
|
333
|
-
return [expression.function, ...expression.args];
|
|
334
|
-
case "FieldAccess":
|
|
335
|
-
return [expression.record];
|
|
336
|
-
case "Product":
|
|
337
|
-
return expression.factors;
|
|
338
|
-
case "BoxAction":
|
|
339
|
-
case "AngleAction":
|
|
340
|
-
return [expression.action, expression.subscript];
|
|
341
|
-
case "WeakFairness":
|
|
342
|
-
case "StrongFairness":
|
|
343
|
-
return [expression.subscript, expression.action];
|
|
344
|
-
case "Label":
|
|
345
|
-
return [expression.body];
|
|
346
|
-
case "Subexpression":
|
|
347
|
-
return expression.selectors.flatMap((selector) => ("args" in selector ? selector.args : []));
|
|
348
|
-
}
|
|
349
|
-
};
|
|
350
|
-
|
|
351
|
-
export const isLocallyResolved = (expression: Expression) =>
|
|
352
|
-
(expression.kind === "Identifier" || expression.kind === "Apply") &&
|
|
353
|
-
expression.resolution?.kind === "Local";
|
|
354
|
-
|
|
355
279
|
export const getDefinition = (name: string, environment: Environment) =>
|
|
356
280
|
environment.definitions.get(environment.substitutions.get(name) ?? name);
|
|
357
281
|
|
|
@@ -419,9 +343,8 @@ export const getEnumeratedElements = (set: Value, expression: Expression) => {
|
|
|
419
343
|
: `TLC encountered the non-enumerable quantifier bound\n${text}\n${describeRange(expression)}\n${explanation}`,
|
|
420
344
|
);
|
|
421
345
|
}
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
: toFiniteSet(set).elements;
|
|
346
|
+
if (typeof set === "object" && set.kind === "Interval") return getIntervalElements(set);
|
|
347
|
+
return getLazySubsets(set) ?? toFiniteSet(set).elements;
|
|
425
348
|
};
|
|
426
349
|
|
|
427
350
|
export const forEachBinding = (
|
|
@@ -859,11 +782,7 @@ const evaluateLocalIdentifier = (name: string, expression: Expression, context:
|
|
|
859
782
|
const global = findGlobalName(name, context.environment);
|
|
860
783
|
if (global?.kind === "Variable") {
|
|
861
784
|
const value = context.state?.[global.index];
|
|
862
|
-
if (value === undefined)
|
|
863
|
-
throw new TlaEvaluationError(
|
|
864
|
-
`The variable ${name} is not assigned at ${describeLocation(expression)}`,
|
|
865
|
-
);
|
|
866
|
-
}
|
|
785
|
+
if (value === undefined) throw createUnassignedError(name, expression);
|
|
867
786
|
return value;
|
|
868
787
|
}
|
|
869
788
|
if (global?.kind === "Value") return global.value;
|
|
@@ -889,14 +808,29 @@ const IMPURE_BUILTINS = new Set([
|
|
|
889
808
|
export const isImpureBuiltin = (name: string, environment: Environment) =>
|
|
890
809
|
IMPURE_BUILTINS.has(name) || environment.builtins.get(name)?.isStateLevel === true;
|
|
891
810
|
|
|
892
|
-
|
|
811
|
+
interface Impurity {
|
|
812
|
+
builtins: Set<string>;
|
|
813
|
+
definitions: WeakMap<Definition, boolean>;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
const ANY_EFFECT: Impurity = { builtins: IMPURE_BUILTINS, definitions: new WeakMap() };
|
|
817
|
+
|
|
818
|
+
// An Assert that held holds again, so evaluating it twice looks the same as evaluating it once.
|
|
819
|
+
const REPEATED_EFFECT: Impurity = {
|
|
820
|
+
builtins: new Set([...IMPURE_BUILTINS].filter((name) => name !== "Assert")),
|
|
821
|
+
definitions: new WeakMap(),
|
|
822
|
+
};
|
|
893
823
|
|
|
894
824
|
const isEvaluatedOnce = (definition: Definition, environment: Environment) =>
|
|
895
825
|
definition.kind === "Operator" &&
|
|
896
826
|
definition.params.length === 0 &&
|
|
897
827
|
isConstantLevel(definition, environment);
|
|
898
828
|
|
|
899
|
-
const callsImpureBuiltin = (
|
|
829
|
+
const callsImpureBuiltin = (
|
|
830
|
+
expression: Expression,
|
|
831
|
+
environment: Environment,
|
|
832
|
+
impurity: Impurity,
|
|
833
|
+
): boolean => {
|
|
900
834
|
const name =
|
|
901
835
|
expression.kind === "Identifier"
|
|
902
836
|
? expression.name
|
|
@@ -905,29 +839,35 @@ const callsImpureBuiltin = (expression: Expression, environment: Environment): b
|
|
|
905
839
|
: undefined;
|
|
906
840
|
if (name !== undefined && !isLocallyResolved(expression)) {
|
|
907
841
|
const definition = getDefinition(name, environment);
|
|
908
|
-
|
|
842
|
+
const isImpureCall =
|
|
843
|
+
impurity.builtins.has(name) || environment.builtins.get(name)?.isStateLevel === true;
|
|
844
|
+
if (!definition && isImpureCall) return true;
|
|
909
845
|
if (
|
|
910
846
|
definition &&
|
|
911
847
|
!isEvaluatedOnce(definition, environment) &&
|
|
912
|
-
isImpureDefinition(definition, environment)
|
|
848
|
+
isImpureDefinition(definition, environment, impurity)
|
|
913
849
|
) {
|
|
914
850
|
return true;
|
|
915
851
|
}
|
|
916
852
|
}
|
|
917
|
-
return getChildren(expression).some((child) => callsImpureBuiltin(child, environment));
|
|
853
|
+
return getChildren(expression).some((child) => callsImpureBuiltin(child, environment, impurity));
|
|
918
854
|
};
|
|
919
855
|
|
|
920
856
|
// TLC evaluates a constant definition without parameters once, so only other definitions pass
|
|
921
857
|
// on the side effects and random choices of the builtins they call.
|
|
922
|
-
const isImpureDefinition = (
|
|
923
|
-
|
|
858
|
+
const isImpureDefinition = (
|
|
859
|
+
definition: Definition,
|
|
860
|
+
environment: Environment,
|
|
861
|
+
impurity = ANY_EFFECT,
|
|
862
|
+
): boolean => {
|
|
863
|
+
const cached = impurity.definitions.get(definition);
|
|
924
864
|
if (cached !== undefined) return cached;
|
|
925
|
-
|
|
865
|
+
impurity.definitions.set(definition, false);
|
|
926
866
|
const isImpure =
|
|
927
|
-
callsImpureBuiltin(definition.body, environment) ||
|
|
867
|
+
callsImpureBuiltin(definition.body, environment, impurity) ||
|
|
928
868
|
(definition.kind === "Function" &&
|
|
929
|
-
definition.bounds.some((bound) => callsImpureBuiltin(bound.set, environment)));
|
|
930
|
-
|
|
869
|
+
definition.bounds.some((bound) => callsImpureBuiltin(bound.set, environment, impurity)));
|
|
870
|
+
impurity.definitions.set(definition, isImpure);
|
|
931
871
|
return isImpure;
|
|
932
872
|
};
|
|
933
873
|
|
|
@@ -941,6 +881,12 @@ export const isImpure = (environment: Environment) =>
|
|
|
941
881
|
isImpureDefinition(definition, environment),
|
|
942
882
|
);
|
|
943
883
|
|
|
884
|
+
// Whether evaluating a definition again can show, such as by printing twice.
|
|
885
|
+
export const hasRepeatedEffects = (environment: Environment) =>
|
|
886
|
+
[...environment.definitions.values()].some((definition) =>
|
|
887
|
+
isImpureDefinition(definition, environment, REPEATED_EFFECT),
|
|
888
|
+
);
|
|
889
|
+
|
|
944
890
|
const closedConstants = new WeakMap<Expression, boolean>();
|
|
945
891
|
|
|
946
892
|
export const isClosedConstant = (expression: Expression, environment: Environment): boolean => {
|
|
@@ -1076,23 +1022,88 @@ const isStateCached = (expression: Expression, environment: Environment) =>
|
|
|
1076
1022
|
getReadVariables(expression, environment) !== undefined &&
|
|
1077
1023
|
!hasFreeLocals(expression, NO_BOUND_NAMES);
|
|
1078
1024
|
|
|
1025
|
+
// CallStackTool also evaluates the predicate of a lazy set filter it returns, even one that TLC
|
|
1026
|
+
// made before the replay.
|
|
1027
|
+
const filteredSetRetracers = new WeakMap<
|
|
1028
|
+
FilteredSetValue,
|
|
1029
|
+
(environment: Environment) => FilteredSetValue
|
|
1030
|
+
>();
|
|
1031
|
+
|
|
1032
|
+
const SOURCED_KINDS = new Set<Expression["kind"]>([
|
|
1033
|
+
"SetEnumeration",
|
|
1034
|
+
"SetMap",
|
|
1035
|
+
"SetFilter",
|
|
1036
|
+
"Tuple",
|
|
1037
|
+
"Record",
|
|
1038
|
+
"RecordSet",
|
|
1039
|
+
"FunctionSet",
|
|
1040
|
+
"Product",
|
|
1041
|
+
]);
|
|
1042
|
+
|
|
1043
|
+
const SOURCED_OPERATORS = new Set(["SUBSET", "UNION", "DOMAIN", "\\cup", "\\cap", "\\"]);
|
|
1044
|
+
|
|
1045
|
+
// CallStackTool gives a set, tuple or record it builds the expression as its source, but not a lazy
|
|
1046
|
+
// set filter, which it returns as a copy.
|
|
1047
|
+
const isSourced = (expression: Expression, environment: Environment) =>
|
|
1048
|
+
SOURCED_KINDS.has(expression.kind) ||
|
|
1049
|
+
(expression.kind === "Apply" &&
|
|
1050
|
+
SOURCED_OPERATORS.has(expression.operator) &&
|
|
1051
|
+
!isLocallyResolved(expression) &&
|
|
1052
|
+
resolveGlobalOperator(expression.operator, environment)?.kind === "Builtin");
|
|
1053
|
+
|
|
1054
|
+
// When an evaluation fails, CallStackTool freezes the stack at the innermost expression and pushes
|
|
1055
|
+
// the source of each value whose method failed.
|
|
1056
|
+
const evaluateTraced = (
|
|
1057
|
+
evaluator: Evaluator,
|
|
1058
|
+
context: Context,
|
|
1059
|
+
trace: Expression[],
|
|
1060
|
+
depth: number,
|
|
1061
|
+
) => {
|
|
1062
|
+
try {
|
|
1063
|
+
return evaluator(context);
|
|
1064
|
+
} catch (error) {
|
|
1065
|
+
if (trace.length === depth + 1) trace.push(...(getFingerprintTrace(error) ?? []));
|
|
1066
|
+
throw error;
|
|
1067
|
+
}
|
|
1068
|
+
};
|
|
1069
|
+
|
|
1079
1070
|
export const getEvaluator = (expression: Expression, environment: Environment): Evaluator => {
|
|
1080
1071
|
const cached = environment.evaluators.get(expression);
|
|
1081
1072
|
if (cached) return cached;
|
|
1082
1073
|
const compiled = compile(expression, environment);
|
|
1083
|
-
const
|
|
1074
|
+
const cachedEvaluator =
|
|
1084
1075
|
isWorthCaching(expression) && isClosedConstant(expression, environment)
|
|
1085
1076
|
? cacheFirstValue(compiled)
|
|
1086
1077
|
: isStateCached(expression, environment)
|
|
1087
1078
|
? cacheStateValue(compiled)
|
|
1088
1079
|
: compiled;
|
|
1080
|
+
const trace = environment.trace;
|
|
1081
|
+
const isSourcedExpression = trace !== undefined && isSourced(expression, environment);
|
|
1082
|
+
// TLC's CallStackTool pushes each expression it evaluates and keeps the stack when one fails.
|
|
1083
|
+
const evaluator: Evaluator = trace
|
|
1084
|
+
? (context) => {
|
|
1085
|
+
const depth = trace.length;
|
|
1086
|
+
trace.push(expression);
|
|
1087
|
+
const value = evaluateTraced(cachedEvaluator, context, trace, depth);
|
|
1088
|
+
trace.length = depth;
|
|
1089
|
+
if (isSourcedExpression && typeof value === "object" && value.kind !== "FilteredSet") {
|
|
1090
|
+
valueSources.current?.set(value, expression);
|
|
1091
|
+
}
|
|
1092
|
+
const retrace =
|
|
1093
|
+
typeof value === "object" && value.kind === "FilteredSet"
|
|
1094
|
+
? filteredSetRetracers.get(value)
|
|
1095
|
+
: undefined;
|
|
1096
|
+
return retrace ? retrace(environment) : value;
|
|
1097
|
+
}
|
|
1098
|
+
: cachedEvaluator;
|
|
1089
1099
|
environment.evaluators.set(expression, evaluator);
|
|
1090
1100
|
return evaluator;
|
|
1091
1101
|
};
|
|
1092
1102
|
|
|
1093
1103
|
const entryEvaluators = new WeakMap<Expression, Evaluator>();
|
|
1094
1104
|
|
|
1095
|
-
const getEntryEvaluator = (expression: Expression, environment: Environment) => {
|
|
1105
|
+
export const getEntryEvaluator = (expression: Expression, environment: Environment) => {
|
|
1106
|
+
if (environment.trace) return getEvaluator(expression, environment);
|
|
1096
1107
|
const cached = entryEvaluators.get(expression);
|
|
1097
1108
|
if (cached) return cached;
|
|
1098
1109
|
const evaluator =
|
|
@@ -1116,11 +1127,7 @@ const compileIdentifier = (
|
|
|
1116
1127
|
const index = global.index;
|
|
1117
1128
|
return (context) => {
|
|
1118
1129
|
const value = context.state?.[index];
|
|
1119
|
-
if (value === undefined)
|
|
1120
|
-
throw new TlaEvaluationError(
|
|
1121
|
-
`The variable ${name} is not assigned at ${describeLocation(expression)}`,
|
|
1122
|
-
);
|
|
1123
|
-
}
|
|
1130
|
+
if (value === undefined) throw createUnassignedError(name, expression);
|
|
1124
1131
|
return value;
|
|
1125
1132
|
};
|
|
1126
1133
|
}
|
|
@@ -1205,11 +1212,32 @@ const getLazyArguments = (args: Expression[], context: Context): OperatorValue[]
|
|
|
1205
1212
|
expression: arg,
|
|
1206
1213
|
}));
|
|
1207
1214
|
|
|
1215
|
+
const RECEIVING_OPERATORS = new Set(["=", "#", "\\in", "\\notin", "\\cup", "\\cap", "\\"]);
|
|
1216
|
+
|
|
1217
|
+
// The argument whose method TLC calls to apply a built-in operator.
|
|
1218
|
+
const getReceiver = (name: string, left: Value, right: Value) =>
|
|
1219
|
+
name === "\\in" ||
|
|
1220
|
+
name === "\\notin" ||
|
|
1221
|
+
((name === "\\cup" || name === "\\cap") && !isReducible(left))
|
|
1222
|
+
? right
|
|
1223
|
+
: left;
|
|
1224
|
+
|
|
1208
1225
|
const compileBuiltinCall = (
|
|
1209
1226
|
builtin: Builtin,
|
|
1210
1227
|
args: Expression[],
|
|
1211
1228
|
environment: Environment,
|
|
1212
1229
|
): Evaluator => {
|
|
1230
|
+
if (environment.trace && RECEIVING_OPERATORS.has(builtin.name) && args.length === 2) {
|
|
1231
|
+
const evaluateLeft = getEvaluator(args[0], environment);
|
|
1232
|
+
const evaluateRight = getEvaluator(args[1], environment);
|
|
1233
|
+
return (context) => {
|
|
1234
|
+
const left = evaluateLeft(context);
|
|
1235
|
+
const right = evaluateRight(context);
|
|
1236
|
+
return withSource(getReceiver(builtin.name, left, right), () =>
|
|
1237
|
+
builtin.evaluate([left, right], context),
|
|
1238
|
+
);
|
|
1239
|
+
};
|
|
1240
|
+
}
|
|
1213
1241
|
if (builtin.isLazy) {
|
|
1214
1242
|
return (context) => builtin.evaluate(getLazyArguments(args, context), context);
|
|
1215
1243
|
}
|
|
@@ -1269,52 +1297,180 @@ export const compileArgumentScope = (
|
|
|
1269
1297
|
};
|
|
1270
1298
|
|
|
1271
1299
|
interface MemoizedCall {
|
|
1300
|
+
sources: (Expression | undefined)[];
|
|
1272
1301
|
args: Value[];
|
|
1273
1302
|
value: Value;
|
|
1274
1303
|
}
|
|
1275
1304
|
|
|
1276
1305
|
const MEMOIZED_CALL_LIMIT = 1 << 16;
|
|
1277
1306
|
|
|
1278
|
-
|
|
1307
|
+
// A call site that finds fewer than half its calls in a state's memo after this many new ones
|
|
1308
|
+
// stops paying for the lookups.
|
|
1309
|
+
const MEMOIZED_TRIAL_CALLS = 1 << 10;
|
|
1310
|
+
|
|
1311
|
+
interface CallMemo {
|
|
1312
|
+
calls: Map<number, MemoizedCall[]>;
|
|
1313
|
+
size: number;
|
|
1314
|
+
state: Context["state"];
|
|
1315
|
+
}
|
|
1279
1316
|
|
|
1280
|
-
|
|
1317
|
+
interface CallMemos {
|
|
1318
|
+
constant: CallMemo;
|
|
1319
|
+
state: CallMemo;
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
const createCallMemo = (): CallMemo => ({ calls: new Map(), size: 0, state: undefined });
|
|
1323
|
+
|
|
1324
|
+
const callMemos = new WeakMap<Closure, CallMemos>();
|
|
1325
|
+
|
|
1326
|
+
const getCallMemos = (closure: Closure) => {
|
|
1281
1327
|
const cached = callMemos.get(closure);
|
|
1282
1328
|
if (cached) return cached;
|
|
1283
|
-
const
|
|
1284
|
-
callMemos.set(closure,
|
|
1285
|
-
return
|
|
1329
|
+
const memos = { constant: createCallMemo(), state: createCallMemo() };
|
|
1330
|
+
callMemos.set(closure, memos);
|
|
1331
|
+
return memos;
|
|
1332
|
+
};
|
|
1333
|
+
|
|
1334
|
+
const readsExceptAt = (expression: Expression): boolean =>
|
|
1335
|
+
expression.kind === "ExceptAt" || getChildren(expression).some(readsExceptAt);
|
|
1336
|
+
|
|
1337
|
+
// The locals an argument expression reads, when the state and their values decide its value. An @
|
|
1338
|
+
// reads the value its EXCEPT binds.
|
|
1339
|
+
const argumentLocals = new WeakMap<Expression, string[] | false>();
|
|
1340
|
+
|
|
1341
|
+
const getArgumentLocals = (expression: Expression, environment: Environment) => {
|
|
1342
|
+
const cached = argumentLocals.get(expression);
|
|
1343
|
+
if (cached !== undefined) return cached;
|
|
1344
|
+
const locals =
|
|
1345
|
+
getReadVariables(expression, environment) !== undefined &&
|
|
1346
|
+
getReadLocals(expression, NO_BOUND_NAMES);
|
|
1347
|
+
const unique = locals && [
|
|
1348
|
+
...new Set(readsExceptAt(expression) ? [...locals, EXCEPT_AT] : locals),
|
|
1349
|
+
];
|
|
1350
|
+
argumentLocals.set(expression, unique ?? false);
|
|
1351
|
+
return unique ?? false;
|
|
1352
|
+
};
|
|
1353
|
+
|
|
1354
|
+
// Hashing a lazy set or function would enumerate it, which TLC does only when it has to.
|
|
1355
|
+
const isEagerValue = (value: Value) =>
|
|
1356
|
+
typeof value !== "object" ||
|
|
1357
|
+
value.kind === "ModelValue" ||
|
|
1358
|
+
value.kind === "Set" ||
|
|
1359
|
+
value.kind === "Function" ||
|
|
1360
|
+
value.kind === "Interval";
|
|
1361
|
+
|
|
1362
|
+
// The key of the call being looked up, copied into the memo only when the call is new. Counts
|
|
1363
|
+
// mark its ends, since shortening an array is slow.
|
|
1364
|
+
const callKey = {
|
|
1365
|
+
sources: [] as (Expression | undefined)[],
|
|
1366
|
+
sourceCount: 0,
|
|
1367
|
+
args: [] as Value[],
|
|
1368
|
+
argCount: 0,
|
|
1369
|
+
hash: 0,
|
|
1370
|
+
isStateKeyed: false,
|
|
1371
|
+
};
|
|
1372
|
+
|
|
1373
|
+
// A model value's rank tells it apart without hashing its name.
|
|
1374
|
+
const getKeyHash = (value: Value) =>
|
|
1375
|
+
typeof value === "object" && value.kind === "ModelValue" ? value.rank : getValueHash(value);
|
|
1376
|
+
|
|
1377
|
+
const pushKeyValue = (value: Value) => {
|
|
1378
|
+
if (!isEagerValue(value)) return false;
|
|
1379
|
+
callKey.sources[callKey.sourceCount++] = undefined;
|
|
1380
|
+
callKey.args[callKey.argCount++] = value;
|
|
1381
|
+
callKey.hash = combineHash(callKey.hash, getKeyHash(value));
|
|
1382
|
+
return true;
|
|
1383
|
+
};
|
|
1384
|
+
|
|
1385
|
+
// An argument TLC has not evaluated yet stands for its expression and the values of the locals it
|
|
1386
|
+
// reads, so a call keeps the laziness of its arguments.
|
|
1387
|
+
const pushKeyExpression = (
|
|
1388
|
+
expression: Expression,
|
|
1389
|
+
locals: string[] | false,
|
|
1390
|
+
scope: Scope | undefined,
|
|
1391
|
+
) => {
|
|
1392
|
+
if (!locals) return false;
|
|
1393
|
+
callKey.sources[callKey.sourceCount++] = expression;
|
|
1394
|
+
callKey.isStateKeyed = true;
|
|
1395
|
+
for (let index = 0; index < locals.length; index++) {
|
|
1396
|
+
const local = lookupScope(scope, locals[index]);
|
|
1397
|
+
if (local?.kind !== "Value" || !isEagerValue(local.value)) return false;
|
|
1398
|
+
callKey.args[callKey.argCount++] = local.value;
|
|
1399
|
+
callKey.hash = combineHash(callKey.hash, getKeyHash(local.value));
|
|
1400
|
+
}
|
|
1401
|
+
return true;
|
|
1286
1402
|
};
|
|
1287
1403
|
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1404
|
+
// Each argument adds to the key what compileArgumentBinder would bind its parameter to.
|
|
1405
|
+
const compileArgumentKey = (
|
|
1406
|
+
argument: Expression,
|
|
1407
|
+
environment: Environment,
|
|
1408
|
+
): ((context: Context) => boolean) => {
|
|
1409
|
+
const isLiteral =
|
|
1410
|
+
argument.kind === "String" || (argument.kind === "Number" && isTlcInteger(argument.value));
|
|
1411
|
+
if (isLiteral) return () => pushKeyValue(argument.value);
|
|
1412
|
+
if (argument.kind === "Identifier") {
|
|
1413
|
+
const global = isLocallyResolved(argument)
|
|
1414
|
+
? undefined
|
|
1415
|
+
: findGlobalName(argument.name, environment);
|
|
1416
|
+
if (global?.kind === "Value") return () => pushKeyValue(global.value);
|
|
1417
|
+
if (global === undefined) {
|
|
1418
|
+
return (context) => {
|
|
1419
|
+
const binding = lookupScope(context.scope, argument.name);
|
|
1420
|
+
if (binding?.kind === "Value") return pushKeyValue(binding.value);
|
|
1421
|
+
if (binding?.kind !== "Thunk" || binding.state !== context.state) return false;
|
|
1422
|
+
return pushKeyExpression(
|
|
1423
|
+
binding.expression,
|
|
1424
|
+
getArgumentLocals(binding.expression, environment),
|
|
1425
|
+
binding.scope,
|
|
1426
|
+
);
|
|
1427
|
+
};
|
|
1428
|
+
}
|
|
1293
1429
|
}
|
|
1294
|
-
|
|
1430
|
+
const locals = getArgumentLocals(argument, environment);
|
|
1431
|
+
return (context) => pushKeyExpression(argument, locals, context.scope);
|
|
1295
1432
|
};
|
|
1296
1433
|
|
|
1297
|
-
const
|
|
1298
|
-
|
|
1434
|
+
const isKnownCall = (call: MemoizedCall) => {
|
|
1435
|
+
if (call.args.length !== callKey.argCount || call.sources.length !== callKey.sourceCount) {
|
|
1436
|
+
return false;
|
|
1437
|
+
}
|
|
1438
|
+
for (let index = 0; index < callKey.sourceCount; index++) {
|
|
1439
|
+
if (call.sources[index] !== callKey.sources[index]) return false;
|
|
1440
|
+
}
|
|
1441
|
+
for (let index = 0; index < callKey.argCount; index++) {
|
|
1442
|
+
if (!isSameValue(call.args[index], callKey.args[index])) return false;
|
|
1443
|
+
}
|
|
1444
|
+
return true;
|
|
1445
|
+
};
|
|
1446
|
+
|
|
1447
|
+
const findCall = (bucket: MemoizedCall[] | undefined) => {
|
|
1448
|
+
if (bucket === undefined) return undefined;
|
|
1449
|
+
for (let index = 0; index < bucket.length; index++) {
|
|
1450
|
+
if (isKnownCall(bucket[index])) return bucket[index];
|
|
1451
|
+
}
|
|
1452
|
+
return undefined;
|
|
1453
|
+
};
|
|
1299
1454
|
|
|
1300
|
-
//
|
|
1301
|
-
// so its calls
|
|
1302
|
-
const
|
|
1455
|
+
// An operator without side effects yields the same value for the same arguments, a state-level one
|
|
1456
|
+
// in the same state, so its calls share one result.
|
|
1457
|
+
const getMemoizedLevel = (expression: Expression & { kind: "Apply" }, environment: Environment) => {
|
|
1303
1458
|
const definition = getDefinition(expression.operator, environment);
|
|
1304
|
-
|
|
1459
|
+
const isMemoizable =
|
|
1305
1460
|
definition?.kind === "Operator" &&
|
|
1306
1461
|
definition.params.length > 0 &&
|
|
1307
1462
|
definition.params.every((param) => param.arity === 0) &&
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
);
|
|
1463
|
+
!isImpureDefinition(definition, environment);
|
|
1464
|
+
if (!isMemoizable) return undefined;
|
|
1465
|
+
if (isConstantLevel(definition, environment)) return "constant";
|
|
1466
|
+
return isStatePure(definition, environment) ? "state" : undefined;
|
|
1311
1467
|
};
|
|
1312
1468
|
|
|
1313
1469
|
const compileClosureCall = (
|
|
1314
1470
|
closure: Closure,
|
|
1315
1471
|
args: Expression[],
|
|
1316
1472
|
environment: Environment,
|
|
1317
|
-
|
|
1473
|
+
memoizedLevel: "constant" | "state" | undefined,
|
|
1318
1474
|
): Evaluator => {
|
|
1319
1475
|
const bindArguments = compileArgumentScope(closure, args, environment);
|
|
1320
1476
|
const compiled = { body: undefined as Evaluator | undefined };
|
|
@@ -1322,22 +1478,47 @@ const compileClosureCall = (
|
|
|
1322
1478
|
compiled.body ??= getEntryEvaluator(closure.body, environment);
|
|
1323
1479
|
return compiled.body({ environment, scope, state: context.state, next: context.next });
|
|
1324
1480
|
};
|
|
1325
|
-
if (!
|
|
1326
|
-
|
|
1481
|
+
if (!memoizedLevel || args.length !== closure.params.length) {
|
|
1482
|
+
return (context) => evaluateBody(bindArguments(context), context);
|
|
1483
|
+
}
|
|
1484
|
+
const memos = getCallMemos(closure);
|
|
1485
|
+
const keys = args.map((arg) => compileArgumentKey(arg, environment));
|
|
1486
|
+
const usage = { hits: 0, misses: 0 };
|
|
1327
1487
|
return (context) => {
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
for (
|
|
1488
|
+
callKey.sourceCount = 0;
|
|
1489
|
+
callKey.argCount = 0;
|
|
1490
|
+
callKey.hash = args.length;
|
|
1491
|
+
callKey.isStateKeyed = false;
|
|
1492
|
+
for (let index = 0; index < keys.length; index++) {
|
|
1493
|
+
if (!keys[index](context)) return evaluateBody(bindArguments(context), context);
|
|
1494
|
+
}
|
|
1495
|
+
const isStateKeyed = memoizedLevel === "state" || callKey.isStateKeyed;
|
|
1496
|
+
if (isStateKeyed && usage.misses > MEMOIZED_TRIAL_CALLS && usage.hits < usage.misses) {
|
|
1497
|
+
return evaluateBody(bindArguments(context), context);
|
|
1498
|
+
}
|
|
1499
|
+
const memo = isStateKeyed ? memos.state : memos.constant;
|
|
1500
|
+
if (isStateKeyed && memo.state !== context.state) {
|
|
1501
|
+
memo.state = context.state;
|
|
1502
|
+
memo.calls.clear();
|
|
1503
|
+
memo.size = 0;
|
|
1504
|
+
}
|
|
1505
|
+
// A small integer key keeps the Map from boxing it.
|
|
1506
|
+
const hash = callKey.hash & 0x3fffffff;
|
|
1333
1507
|
const bucket = memo.calls.get(hash);
|
|
1334
|
-
const known = bucket
|
|
1335
|
-
if (known)
|
|
1336
|
-
|
|
1508
|
+
const known = findCall(bucket);
|
|
1509
|
+
if (known) {
|
|
1510
|
+
if (isStateKeyed) usage.hits++;
|
|
1511
|
+
return known.value;
|
|
1512
|
+
}
|
|
1513
|
+
if (isStateKeyed) usage.misses++;
|
|
1514
|
+
const sources = callKey.sources.slice(0, callKey.sourceCount);
|
|
1515
|
+
const keyArgs = callKey.args.slice(0, callKey.argCount);
|
|
1516
|
+
const value = evaluateBody(bindArguments(context), context);
|
|
1337
1517
|
if (memo.size < MEMOIZED_CALL_LIMIT) {
|
|
1338
1518
|
memo.size++;
|
|
1339
|
-
|
|
1340
|
-
|
|
1519
|
+
const call = { sources, args: keyArgs, value };
|
|
1520
|
+
if (bucket) bucket.push(call);
|
|
1521
|
+
else memo.calls.set(hash, [call]);
|
|
1341
1522
|
}
|
|
1342
1523
|
return value;
|
|
1343
1524
|
};
|
|
@@ -1364,11 +1545,18 @@ const compileApply = (
|
|
|
1364
1545
|
? undefined
|
|
1365
1546
|
: resolveGlobalOperator(expression.operator, environment);
|
|
1366
1547
|
if (global?.kind === "Closure") {
|
|
1367
|
-
return compileClosureCall(global, args, environment,
|
|
1548
|
+
return compileClosureCall(global, args, environment, getMemoizedLevel(expression, environment));
|
|
1368
1549
|
}
|
|
1369
1550
|
if (global?.kind === "Builtin" && global.name === "DOMAIN") {
|
|
1370
1551
|
const operand = getEvaluator(args[0], environment);
|
|
1371
1552
|
const domain = getDomainOperator(expression);
|
|
1553
|
+
// TLC builds a new interval for the domain of a tuple, so the source it gives one stays its own.
|
|
1554
|
+
if (environment.trace) {
|
|
1555
|
+
return (context) => {
|
|
1556
|
+
const value = domain(operand(context));
|
|
1557
|
+
return typeof value === "object" && value.kind === "Interval" ? { ...value } : value;
|
|
1558
|
+
};
|
|
1559
|
+
}
|
|
1372
1560
|
return (context) => domain(operand(context));
|
|
1373
1561
|
}
|
|
1374
1562
|
// TLC sorts the value of a constant TLCEval argument only where no context binds a name.
|
|
@@ -1384,6 +1572,13 @@ const compileApply = (
|
|
|
1384
1572
|
const left = getEvaluator(args[0], environment);
|
|
1385
1573
|
const right = getEvaluator(args[1], environment);
|
|
1386
1574
|
const subset = getSubsetOperator(expression, global);
|
|
1575
|
+
if (environment.trace) {
|
|
1576
|
+
return (context) => {
|
|
1577
|
+
const subsetValue = left(context);
|
|
1578
|
+
const superset = right(context);
|
|
1579
|
+
return withSource(subsetValue, () => subset(subsetValue, superset));
|
|
1580
|
+
};
|
|
1581
|
+
}
|
|
1387
1582
|
return (context) => subset(left(context), right(context));
|
|
1388
1583
|
}
|
|
1389
1584
|
if (global?.kind === "Builtin") return compileBuiltinCall(global, args, environment);
|
|
@@ -1394,6 +1589,11 @@ const compileApply = (
|
|
|
1394
1589
|
if (definition?.kind === "Function") {
|
|
1395
1590
|
throw new TlaEvaluationError(`${expression.operator} is a function; apply it with brackets`);
|
|
1396
1591
|
}
|
|
1592
|
+
if (expression.operator === "\\cdot") {
|
|
1593
|
+
throw new TlaEvaluationError(
|
|
1594
|
+
"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.",
|
|
1595
|
+
);
|
|
1596
|
+
}
|
|
1397
1597
|
throw new TlaEvaluationError(
|
|
1398
1598
|
`Unknown operator ${expression.operator} at ${describeLocation(expression)}`,
|
|
1399
1599
|
);
|
|
@@ -1489,7 +1689,13 @@ const compileFunctionConstruction = (
|
|
|
1489
1689
|
const set = getEvaluator(bound.set, environment);
|
|
1490
1690
|
return (context) => {
|
|
1491
1691
|
const domain = isSingleName ? set(context) : getBoundsDomain(bounds, context);
|
|
1492
|
-
|
|
1692
|
+
// TLC builds the function only when it reduces every domain, and evaluates the body lazily
|
|
1693
|
+
// otherwise.
|
|
1694
|
+
const domains =
|
|
1695
|
+
typeof domain === "object" && domain.kind === "ProductSet" && bounds.length > 1
|
|
1696
|
+
? domain.factors
|
|
1697
|
+
: [domain];
|
|
1698
|
+
if (expression.isRecursive || isKeptLazy || !domains.every(isReducible)) {
|
|
1493
1699
|
const functionValue = createLazyFunction(
|
|
1494
1700
|
domain,
|
|
1495
1701
|
(argument) => body(withScope(context, bindArgument(bounds, context.scope, argument))),
|
|
@@ -1578,16 +1784,21 @@ const compileSetComprehension = (
|
|
|
1578
1784
|
const pattern = expression.bound.pattern;
|
|
1579
1785
|
return (context) => {
|
|
1580
1786
|
const base = set(context);
|
|
1581
|
-
const
|
|
1787
|
+
const getTest = (evaluatePredicate: Evaluator) => (element: Value) =>
|
|
1582
1788
|
expectBoolean(
|
|
1583
|
-
|
|
1789
|
+
evaluatePredicate(withScope(context, bindPattern(context.scope, pattern, element))),
|
|
1584
1790
|
"a set filter predicate",
|
|
1585
1791
|
);
|
|
1586
|
-
const
|
|
1587
|
-
|
|
1588
|
-
(base
|
|
1589
|
-
|
|
1590
|
-
|
|
1792
|
+
const test = getTest(predicate);
|
|
1793
|
+
if (!isReducible(base)) {
|
|
1794
|
+
if (isSetValue(base)) {
|
|
1795
|
+
const filtered: FilteredSetValue = { kind: "FilteredSet", base, test };
|
|
1796
|
+
filteredSetRetracers.set(filtered, (traced) => ({
|
|
1797
|
+
...filtered,
|
|
1798
|
+
test: getTest(getEvaluator(expression.predicate, traced)),
|
|
1799
|
+
}));
|
|
1800
|
+
return filtered;
|
|
1801
|
+
}
|
|
1591
1802
|
throw new TlaEvaluationError(
|
|
1592
1803
|
`TLC encountered a non-enumerable quantifier bound ${formatValue(base)} at ${describeLocation(expression.bound.set)}`,
|
|
1593
1804
|
);
|
|
@@ -1632,11 +1843,30 @@ const compileSetComprehension = (
|
|
|
1632
1843
|
};
|
|
1633
1844
|
};
|
|
1634
1845
|
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1846
|
+
// TLC orders a record's fields by name.
|
|
1847
|
+
export const getSortedFields = (fields: RecordField[]) =>
|
|
1848
|
+
[...fields].sort((left, right) => compareValues(left.name, right.name));
|
|
1849
|
+
|
|
1850
|
+
export const hasDuplicateFields = (sortedFields: RecordField[]) =>
|
|
1851
|
+
sortedFields.some((field, index) => index > 0 && sortedFields[index - 1].name === field.name);
|
|
1852
|
+
|
|
1853
|
+
export const createUnassignedError = (name: string, expression: Expression) =>
|
|
1854
|
+
setOwnExitCode(
|
|
1855
|
+
new TlaUnassignedError(
|
|
1856
|
+
`In evaluation, the identifier ${name} is either undefined or not an operator.\n${describeRange(expression)}`,
|
|
1857
|
+
name,
|
|
1858
|
+
),
|
|
1859
|
+
EXIT_STATUS.configParseError,
|
|
1860
|
+
);
|
|
1861
|
+
|
|
1862
|
+
export const createPrimeOutsideActionError = (expression: Expression) =>
|
|
1863
|
+
new TlaEvaluationError(
|
|
1864
|
+
`A primed expression appears outside an action at ${describeLocation(expression)}`,
|
|
1639
1865
|
);
|
|
1866
|
+
|
|
1867
|
+
const compileRecord = (recordFields: RecordField[], environment: Environment): Evaluator => {
|
|
1868
|
+
const fields = getSortedFields(recordFields);
|
|
1869
|
+
const hasDuplicates = hasDuplicateFields(fields);
|
|
1640
1870
|
const values = fields.map((field) => getEvaluator(field.value, environment));
|
|
1641
1871
|
if (hasDuplicates) {
|
|
1642
1872
|
return (context) =>
|
|
@@ -1714,11 +1944,7 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1714
1944
|
case "Prime": {
|
|
1715
1945
|
const inner = child(expression.expression);
|
|
1716
1946
|
return (context) => {
|
|
1717
|
-
if (!context.next)
|
|
1718
|
-
throw new TlaEvaluationError(
|
|
1719
|
-
`A primed expression appears outside an action at ${describeLocation(expression)}`,
|
|
1720
|
-
);
|
|
1721
|
-
}
|
|
1947
|
+
if (!context.next) throw createPrimeOutsideActionError(expression);
|
|
1722
1948
|
return inner({
|
|
1723
1949
|
environment: context.environment,
|
|
1724
1950
|
scope: context.scope,
|
|
@@ -1757,7 +1983,9 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1757
1983
|
if (expectBoolean(arm.condition(context), "a CASE guard")) return arm.body(context);
|
|
1758
1984
|
}
|
|
1759
1985
|
if (other) return other(context);
|
|
1760
|
-
throw new TlaEvaluationError(
|
|
1986
|
+
throw new TlaEvaluationError(
|
|
1987
|
+
`Attempted to evaluate a CASE with no conditions true.\n${describeRange(expression)}`,
|
|
1988
|
+
);
|
|
1761
1989
|
};
|
|
1762
1990
|
}
|
|
1763
1991
|
case "Let": {
|
|
@@ -1796,9 +2024,7 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1796
2024
|
case "Record":
|
|
1797
2025
|
return compileRecord(expression.fields, environment);
|
|
1798
2026
|
case "RecordSet": {
|
|
1799
|
-
const fields =
|
|
1800
|
-
compareValues(left.name, right.name),
|
|
1801
|
-
);
|
|
2027
|
+
const fields = getSortedFields(expression.fields);
|
|
1802
2028
|
const names = fields.map((field) => field.name);
|
|
1803
2029
|
const sets = fields.map((field) => child(field.value));
|
|
1804
2030
|
return (context) => ({ kind: "RecordSet", names, sets: sets.map((set) => set(context)) });
|
|
@@ -1819,18 +2045,41 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1819
2045
|
keepLazy(expression.function);
|
|
1820
2046
|
const evaluateFunction = child(expression.function);
|
|
1821
2047
|
const args = expression.args.map(child);
|
|
2048
|
+
const describeNonFunction = getNonFunctionError(expression);
|
|
2049
|
+
if (args.length === 1 && environment.trace) {
|
|
2050
|
+
const argument = args[0];
|
|
2051
|
+
return (context) => {
|
|
2052
|
+
const functionValue = evaluateFunction(context);
|
|
2053
|
+
const argumentValue = argument(context);
|
|
2054
|
+
const apply = () =>
|
|
2055
|
+
applyFunction(functionValue, argumentValue, undefined, describeNonFunction);
|
|
2056
|
+
return isFunctionValue(functionValue) ? withSource(functionValue, apply) : apply();
|
|
2057
|
+
};
|
|
2058
|
+
}
|
|
1822
2059
|
if (args.length === 1) {
|
|
1823
2060
|
const argument = args[0];
|
|
1824
|
-
return (context) =>
|
|
2061
|
+
return (context) =>
|
|
2062
|
+
applyFunction(
|
|
2063
|
+
evaluateFunction(context),
|
|
2064
|
+
argument(context),
|
|
2065
|
+
undefined,
|
|
2066
|
+
describeNonFunction,
|
|
2067
|
+
);
|
|
1825
2068
|
}
|
|
1826
2069
|
return (context) =>
|
|
1827
|
-
applyFunction(
|
|
2070
|
+
applyFunction(
|
|
2071
|
+
evaluateFunction(context),
|
|
2072
|
+
createTuple(args.map((arg) => arg(context))),
|
|
2073
|
+
undefined,
|
|
2074
|
+
describeNonFunction,
|
|
2075
|
+
);
|
|
1828
2076
|
}
|
|
1829
2077
|
case "FieldAccess": {
|
|
1830
2078
|
const record = child(expression.record);
|
|
1831
2079
|
const field = expression.field;
|
|
1832
2080
|
const describeMissing = getFieldError(expression);
|
|
1833
|
-
|
|
2081
|
+
const describeNonRecord = getNonRecordError(expression);
|
|
2082
|
+
return (context) => applyFunction(record(context), field, describeMissing, describeNonRecord);
|
|
1834
2083
|
}
|
|
1835
2084
|
case "Product": {
|
|
1836
2085
|
const factors = expression.factors.map(child);
|