tla-ts 0.0.3-dev.79f7ed7 → 0.0.3-dev.852fbc8
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 +55 -22
- package/dist/index.d.ts +55 -22
- 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 +19 -16
- package/dist/run-cli.js +20 -17
- package/dist/transfer.cjs +37 -16
- package/dist/transfer.js +37 -16
- package/package.json +1 -1
- package/src/ast.ts +93 -0
- package/src/check.ts +511 -99
- 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 +116 -28
- package/src/errors.ts +87 -3
- package/src/evaluate.ts +252 -156
- package/src/exit-codes.ts +39 -42
- package/src/instances.ts +16 -15
- package/src/levels.ts +62 -34
- package/src/lexer.ts +5 -1
- package/src/live-check.ts +772 -0
- package/src/liveness.ts +120 -543
- package/src/parallel-worker.ts +7 -3
- package/src/parallel.ts +20 -4
- package/src/parser.ts +33 -11
- package/src/resolve.ts +122 -44
- package/src/run-cli.ts +112 -20
- package/src/runtime.ts +3 -1
- package/src/spec-description.ts +19 -8
- package/src/standard-modules.ts +10 -9
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +10 -10
- package/src/successors.ts +183 -39
- package/src/symmetry.ts +30 -8
- 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 +179 -57
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
21
|
import { isEnabled } from "./successors";
|
|
22
|
+
import { getFingerprintTrace, valueSources, withSource } from "./value-sources";
|
|
21
23
|
import {
|
|
22
24
|
applyFunction,
|
|
23
25
|
combineHash,
|
|
@@ -28,23 +30,27 @@ import {
|
|
|
28
30
|
createSortedSet,
|
|
29
31
|
createWrittenRecord,
|
|
30
32
|
createTuple,
|
|
33
|
+
describeMissingField,
|
|
31
34
|
formatValue,
|
|
32
35
|
getDomain,
|
|
33
36
|
getFunctionDomain,
|
|
34
37
|
getIntervalElements,
|
|
35
38
|
getSequenceElements,
|
|
36
39
|
getValueHash,
|
|
37
|
-
isEnumerable,
|
|
38
40
|
isFunctionValue,
|
|
39
41
|
isOverriddenSet,
|
|
42
|
+
isRecordValue,
|
|
40
43
|
isSameValue,
|
|
41
44
|
markLambdaFunction,
|
|
45
|
+
isReducible,
|
|
42
46
|
isSetValue,
|
|
43
47
|
MAXIMUM_INTEGER,
|
|
44
48
|
MINIMUM_INTEGER,
|
|
45
49
|
toFiniteSet,
|
|
46
50
|
updateFunction,
|
|
47
51
|
valuesEqual,
|
|
52
|
+
type FilteredSetValue,
|
|
53
|
+
type FunctionValue,
|
|
48
54
|
type LazyFunctionSource,
|
|
49
55
|
type LazyFunctionValue,
|
|
50
56
|
type Value,
|
|
@@ -112,6 +118,7 @@ export interface Environment {
|
|
|
112
118
|
definitionValues: Map<Definition, Value>;
|
|
113
119
|
constantLevels: Map<Definition, boolean>;
|
|
114
120
|
evaluators: WeakMap<Expression, Evaluator>;
|
|
121
|
+
trace?: Expression[];
|
|
115
122
|
}
|
|
116
123
|
|
|
117
124
|
export interface Context {
|
|
@@ -141,9 +148,6 @@ const EXCEPT_AT = "@";
|
|
|
141
148
|
export const describeLocation = (expression: Expression) =>
|
|
142
149
|
`line ${expression.location.line}, column ${expression.location.column}`;
|
|
143
150
|
|
|
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
151
|
export const describeRange = (expression: Expression) => {
|
|
148
152
|
const range = expression.location.range;
|
|
149
153
|
return range ? formatRange(range) : describeLocation(expression);
|
|
@@ -180,8 +184,17 @@ export const getConjunctError = (
|
|
|
180
184
|
};
|
|
181
185
|
|
|
182
186
|
export const getFieldError =
|
|
183
|
-
(expression: Expression & { kind: "FieldAccess" }) => (record:
|
|
184
|
-
|
|
187
|
+
(expression: Expression & { kind: "FieldAccess" }) => (record: FunctionValue) =>
|
|
188
|
+
isRecordValue(record)
|
|
189
|
+
? `Attempted to select nonexistent field "${expression.field}" from the record\n${formatValue(record)}\n${describeRange(expression)}`
|
|
190
|
+
: describeMissingField(record, expression.field);
|
|
191
|
+
|
|
192
|
+
export const getNonFunctionError = (expression: Expression) => (value: Value) =>
|
|
193
|
+
`A non-function (${getKindString(value)}) was applied as a function.\n${describeRange(expression)}`;
|
|
194
|
+
|
|
195
|
+
export const getNonRecordError =
|
|
196
|
+
(expression: Expression & { kind: "FieldAccess" }) => (value: Value) =>
|
|
197
|
+
`Attempted to select field "${expression.field}" from a non-record value ${formatValue(value)}\n${describeRange(expression)}`;
|
|
185
198
|
|
|
186
199
|
export const getDomainOperator = (expression: Expression) => (value: Value) => {
|
|
187
200
|
if (!isFunctionValue(value)) {
|
|
@@ -262,96 +275,6 @@ export const expectBoolean = (value: Value, description: string) => {
|
|
|
262
275
|
return value;
|
|
263
276
|
};
|
|
264
277
|
|
|
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
278
|
export const getDefinition = (name: string, environment: Environment) =>
|
|
356
279
|
environment.definitions.get(environment.substitutions.get(name) ?? name);
|
|
357
280
|
|
|
@@ -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
|
}
|
|
@@ -1369,6 +1397,13 @@ const compileApply = (
|
|
|
1369
1397
|
if (global?.kind === "Builtin" && global.name === "DOMAIN") {
|
|
1370
1398
|
const operand = getEvaluator(args[0], environment);
|
|
1371
1399
|
const domain = getDomainOperator(expression);
|
|
1400
|
+
// TLC builds a new interval for the domain of a tuple, so the source it gives one stays its own.
|
|
1401
|
+
if (environment.trace) {
|
|
1402
|
+
return (context) => {
|
|
1403
|
+
const value = domain(operand(context));
|
|
1404
|
+
return typeof value === "object" && value.kind === "Interval" ? { ...value } : value;
|
|
1405
|
+
};
|
|
1406
|
+
}
|
|
1372
1407
|
return (context) => domain(operand(context));
|
|
1373
1408
|
}
|
|
1374
1409
|
// TLC sorts the value of a constant TLCEval argument only where no context binds a name.
|
|
@@ -1384,6 +1419,13 @@ const compileApply = (
|
|
|
1384
1419
|
const left = getEvaluator(args[0], environment);
|
|
1385
1420
|
const right = getEvaluator(args[1], environment);
|
|
1386
1421
|
const subset = getSubsetOperator(expression, global);
|
|
1422
|
+
if (environment.trace) {
|
|
1423
|
+
return (context) => {
|
|
1424
|
+
const subsetValue = left(context);
|
|
1425
|
+
const superset = right(context);
|
|
1426
|
+
return withSource(subsetValue, () => subset(subsetValue, superset));
|
|
1427
|
+
};
|
|
1428
|
+
}
|
|
1387
1429
|
return (context) => subset(left(context), right(context));
|
|
1388
1430
|
}
|
|
1389
1431
|
if (global?.kind === "Builtin") return compileBuiltinCall(global, args, environment);
|
|
@@ -1394,6 +1436,11 @@ const compileApply = (
|
|
|
1394
1436
|
if (definition?.kind === "Function") {
|
|
1395
1437
|
throw new TlaEvaluationError(`${expression.operator} is a function; apply it with brackets`);
|
|
1396
1438
|
}
|
|
1439
|
+
if (expression.operator === "\\cdot") {
|
|
1440
|
+
throw new TlaEvaluationError(
|
|
1441
|
+
"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.",
|
|
1442
|
+
);
|
|
1443
|
+
}
|
|
1397
1444
|
throw new TlaEvaluationError(
|
|
1398
1445
|
`Unknown operator ${expression.operator} at ${describeLocation(expression)}`,
|
|
1399
1446
|
);
|
|
@@ -1489,7 +1536,13 @@ const compileFunctionConstruction = (
|
|
|
1489
1536
|
const set = getEvaluator(bound.set, environment);
|
|
1490
1537
|
return (context) => {
|
|
1491
1538
|
const domain = isSingleName ? set(context) : getBoundsDomain(bounds, context);
|
|
1492
|
-
|
|
1539
|
+
// TLC builds the function only when it reduces every domain, and evaluates the body lazily
|
|
1540
|
+
// otherwise.
|
|
1541
|
+
const domains =
|
|
1542
|
+
typeof domain === "object" && domain.kind === "ProductSet" && bounds.length > 1
|
|
1543
|
+
? domain.factors
|
|
1544
|
+
: [domain];
|
|
1545
|
+
if (expression.isRecursive || isKeptLazy || !domains.every(isReducible)) {
|
|
1493
1546
|
const functionValue = createLazyFunction(
|
|
1494
1547
|
domain,
|
|
1495
1548
|
(argument) => body(withScope(context, bindArgument(bounds, context.scope, argument))),
|
|
@@ -1578,16 +1631,21 @@ const compileSetComprehension = (
|
|
|
1578
1631
|
const pattern = expression.bound.pattern;
|
|
1579
1632
|
return (context) => {
|
|
1580
1633
|
const base = set(context);
|
|
1581
|
-
const
|
|
1634
|
+
const getTest = (evaluatePredicate: Evaluator) => (element: Value) =>
|
|
1582
1635
|
expectBoolean(
|
|
1583
|
-
|
|
1636
|
+
evaluatePredicate(withScope(context, bindPattern(context.scope, pattern, element))),
|
|
1584
1637
|
"a set filter predicate",
|
|
1585
1638
|
);
|
|
1586
|
-
const
|
|
1587
|
-
|
|
1588
|
-
(base
|
|
1589
|
-
|
|
1590
|
-
|
|
1639
|
+
const test = getTest(predicate);
|
|
1640
|
+
if (!isReducible(base)) {
|
|
1641
|
+
if (isSetValue(base)) {
|
|
1642
|
+
const filtered: FilteredSetValue = { kind: "FilteredSet", base, test };
|
|
1643
|
+
filteredSetRetracers.set(filtered, (traced) => ({
|
|
1644
|
+
...filtered,
|
|
1645
|
+
test: getTest(getEvaluator(expression.predicate, traced)),
|
|
1646
|
+
}));
|
|
1647
|
+
return filtered;
|
|
1648
|
+
}
|
|
1591
1649
|
throw new TlaEvaluationError(
|
|
1592
1650
|
`TLC encountered a non-enumerable quantifier bound ${formatValue(base)} at ${describeLocation(expression.bound.set)}`,
|
|
1593
1651
|
);
|
|
@@ -1632,11 +1690,30 @@ const compileSetComprehension = (
|
|
|
1632
1690
|
};
|
|
1633
1691
|
};
|
|
1634
1692
|
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1693
|
+
// TLC orders a record's fields by name.
|
|
1694
|
+
export const getSortedFields = (fields: RecordField[]) =>
|
|
1695
|
+
[...fields].sort((left, right) => compareValues(left.name, right.name));
|
|
1696
|
+
|
|
1697
|
+
export const hasDuplicateFields = (sortedFields: RecordField[]) =>
|
|
1698
|
+
sortedFields.some((field, index) => index > 0 && sortedFields[index - 1].name === field.name);
|
|
1699
|
+
|
|
1700
|
+
export const createUnassignedError = (name: string, expression: Expression) =>
|
|
1701
|
+
setOwnExitCode(
|
|
1702
|
+
new TlaUnassignedError(
|
|
1703
|
+
`In evaluation, the identifier ${name} is either undefined or not an operator.\n${describeRange(expression)}`,
|
|
1704
|
+
name,
|
|
1705
|
+
),
|
|
1706
|
+
EXIT_STATUS.configParseError,
|
|
1707
|
+
);
|
|
1708
|
+
|
|
1709
|
+
export const createPrimeOutsideActionError = (expression: Expression) =>
|
|
1710
|
+
new TlaEvaluationError(
|
|
1711
|
+
`A primed expression appears outside an action at ${describeLocation(expression)}`,
|
|
1639
1712
|
);
|
|
1713
|
+
|
|
1714
|
+
const compileRecord = (recordFields: RecordField[], environment: Environment): Evaluator => {
|
|
1715
|
+
const fields = getSortedFields(recordFields);
|
|
1716
|
+
const hasDuplicates = hasDuplicateFields(fields);
|
|
1640
1717
|
const values = fields.map((field) => getEvaluator(field.value, environment));
|
|
1641
1718
|
if (hasDuplicates) {
|
|
1642
1719
|
return (context) =>
|
|
@@ -1714,11 +1791,7 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1714
1791
|
case "Prime": {
|
|
1715
1792
|
const inner = child(expression.expression);
|
|
1716
1793
|
return (context) => {
|
|
1717
|
-
if (!context.next)
|
|
1718
|
-
throw new TlaEvaluationError(
|
|
1719
|
-
`A primed expression appears outside an action at ${describeLocation(expression)}`,
|
|
1720
|
-
);
|
|
1721
|
-
}
|
|
1794
|
+
if (!context.next) throw createPrimeOutsideActionError(expression);
|
|
1722
1795
|
return inner({
|
|
1723
1796
|
environment: context.environment,
|
|
1724
1797
|
scope: context.scope,
|
|
@@ -1757,7 +1830,9 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1757
1830
|
if (expectBoolean(arm.condition(context), "a CASE guard")) return arm.body(context);
|
|
1758
1831
|
}
|
|
1759
1832
|
if (other) return other(context);
|
|
1760
|
-
throw new TlaEvaluationError(
|
|
1833
|
+
throw new TlaEvaluationError(
|
|
1834
|
+
`Attempted to evaluate a CASE with no conditions true.\n${describeRange(expression)}`,
|
|
1835
|
+
);
|
|
1761
1836
|
};
|
|
1762
1837
|
}
|
|
1763
1838
|
case "Let": {
|
|
@@ -1796,9 +1871,7 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1796
1871
|
case "Record":
|
|
1797
1872
|
return compileRecord(expression.fields, environment);
|
|
1798
1873
|
case "RecordSet": {
|
|
1799
|
-
const fields =
|
|
1800
|
-
compareValues(left.name, right.name),
|
|
1801
|
-
);
|
|
1874
|
+
const fields = getSortedFields(expression.fields);
|
|
1802
1875
|
const names = fields.map((field) => field.name);
|
|
1803
1876
|
const sets = fields.map((field) => child(field.value));
|
|
1804
1877
|
return (context) => ({ kind: "RecordSet", names, sets: sets.map((set) => set(context)) });
|
|
@@ -1819,18 +1892,41 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1819
1892
|
keepLazy(expression.function);
|
|
1820
1893
|
const evaluateFunction = child(expression.function);
|
|
1821
1894
|
const args = expression.args.map(child);
|
|
1895
|
+
const describeNonFunction = getNonFunctionError(expression);
|
|
1896
|
+
if (args.length === 1 && environment.trace) {
|
|
1897
|
+
const argument = args[0];
|
|
1898
|
+
return (context) => {
|
|
1899
|
+
const functionValue = evaluateFunction(context);
|
|
1900
|
+
const argumentValue = argument(context);
|
|
1901
|
+
const apply = () =>
|
|
1902
|
+
applyFunction(functionValue, argumentValue, undefined, describeNonFunction);
|
|
1903
|
+
return isFunctionValue(functionValue) ? withSource(functionValue, apply) : apply();
|
|
1904
|
+
};
|
|
1905
|
+
}
|
|
1822
1906
|
if (args.length === 1) {
|
|
1823
1907
|
const argument = args[0];
|
|
1824
|
-
return (context) =>
|
|
1908
|
+
return (context) =>
|
|
1909
|
+
applyFunction(
|
|
1910
|
+
evaluateFunction(context),
|
|
1911
|
+
argument(context),
|
|
1912
|
+
undefined,
|
|
1913
|
+
describeNonFunction,
|
|
1914
|
+
);
|
|
1825
1915
|
}
|
|
1826
1916
|
return (context) =>
|
|
1827
|
-
applyFunction(
|
|
1917
|
+
applyFunction(
|
|
1918
|
+
evaluateFunction(context),
|
|
1919
|
+
createTuple(args.map((arg) => arg(context))),
|
|
1920
|
+
undefined,
|
|
1921
|
+
describeNonFunction,
|
|
1922
|
+
);
|
|
1828
1923
|
}
|
|
1829
1924
|
case "FieldAccess": {
|
|
1830
1925
|
const record = child(expression.record);
|
|
1831
1926
|
const field = expression.field;
|
|
1832
1927
|
const describeMissing = getFieldError(expression);
|
|
1833
|
-
|
|
1928
|
+
const describeNonRecord = getNonRecordError(expression);
|
|
1929
|
+
return (context) => applyFunction(record(context), field, describeMissing, describeNonRecord);
|
|
1834
1930
|
}
|
|
1835
1931
|
case "Product": {
|
|
1836
1932
|
const factors = expression.factors.map(child);
|