tla-ts 0.0.3-dev.b74f28c → 0.0.3-dev.bcfa494
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 +9 -0
- package/dist/index.d.ts +9 -0
- 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 +17 -16
- package/dist/run-cli.js +6 -5
- package/dist/transfer.cjs +27 -15
- package/dist/transfer.js +27 -15
- package/package.json +1 -1
- package/src/ast.ts +90 -0
- package/src/check.ts +254 -74
- package/src/cli-options.ts +11 -5
- package/src/codegen.ts +9 -19
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +88 -20
- package/src/errors.ts +18 -2
- package/src/evaluate.ts +85 -142
- package/src/exit-codes.ts +21 -43
- package/src/instances.ts +15 -14
- package/src/levels.ts +8 -8
- package/src/lexer.ts +5 -1
- package/src/live-check.ts +766 -0
- package/src/liveness.ts +54 -535
- package/src/parallel-worker.ts +7 -3
- package/src/parallel.ts +20 -4
- package/src/parser.ts +12 -1
- package/src/resolve.ts +16 -15
- package/src/run-cli.ts +58 -13
- package/src/runtime.ts +3 -1
- package/src/spec-description.ts +18 -6
- package/src/standard-modules.ts +2 -7
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +10 -10
- package/src/successors.ts +78 -14
- package/src/tableau.ts +50 -23
- package/src/tlc-ext.ts +29 -12
- package/src/tlc-module.ts +9 -28
- package/src/tlc-order.ts +25 -7
- package/src/tlc-random.ts +9 -6
- package/src/tlc-summary.ts +20 -5
- package/src/values.ts +59 -11
package/src/codegen.ts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import { TlaEvaluationError } from "./errors";
|
|
1
|
+
import { getChildren, isLocallyResolved, type Expression } from "./ast";
|
|
3
2
|
import {
|
|
4
|
-
describeLocation,
|
|
5
3
|
bindValue,
|
|
6
4
|
expectBoolean,
|
|
7
5
|
findGlobalName,
|
|
8
|
-
getChildren,
|
|
9
6
|
getEnumeratedElements,
|
|
10
7
|
getConditionError,
|
|
11
8
|
getConjunctError,
|
|
@@ -14,19 +11,21 @@ import {
|
|
|
14
11
|
getEvaluator,
|
|
15
12
|
getFieldError,
|
|
16
13
|
isClosedConstant,
|
|
17
|
-
isLocallyResolved,
|
|
18
14
|
isTlcInteger,
|
|
19
15
|
isValueArgument,
|
|
20
16
|
isWorthCaching,
|
|
21
17
|
keepLazy,
|
|
22
18
|
resolveGlobalOperator,
|
|
19
|
+
createPrimeOutsideActionError,
|
|
20
|
+
createUnassignedError,
|
|
21
|
+
getSortedFields,
|
|
22
|
+
hasDuplicateFields,
|
|
23
23
|
type Context,
|
|
24
24
|
type Environment,
|
|
25
25
|
type Evaluator,
|
|
26
26
|
} from "./evaluate";
|
|
27
27
|
import {
|
|
28
28
|
applyFunction,
|
|
29
|
-
compareValues,
|
|
30
29
|
createEnumeratedSet,
|
|
31
30
|
createSortedFunction,
|
|
32
31
|
createTuple,
|
|
@@ -84,14 +83,10 @@ const createRuntime = () => ({
|
|
|
84
83
|
equal: valuesEqual,
|
|
85
84
|
enumerate: getEnumeratedElements,
|
|
86
85
|
unassigned: (name: string, expression: Expression): never => {
|
|
87
|
-
throw
|
|
88
|
-
`The variable ${name} is not assigned at ${describeLocation(expression)}`,
|
|
89
|
-
);
|
|
86
|
+
throw createUnassignedError(name, expression);
|
|
90
87
|
},
|
|
91
88
|
primeOutsideAction: (expression: Expression): never => {
|
|
92
|
-
throw
|
|
93
|
-
`A primed expression appears outside an action at ${describeLocation(expression)}`,
|
|
94
|
-
);
|
|
89
|
+
throw createPrimeOutsideActionError(expression);
|
|
95
90
|
},
|
|
96
91
|
prime: (context: Context): Context => ({
|
|
97
92
|
environment: context.environment,
|
|
@@ -262,13 +257,8 @@ const generateExpression = (generator: Generator, expression: Expression, frame:
|
|
|
262
257
|
case "FieldAccess":
|
|
263
258
|
return `apply(${generate(expression.record)}, ${JSON.stringify(expression.field)}, ${addReference(generator, getFieldError(expression))})`;
|
|
264
259
|
case "Record": {
|
|
265
|
-
const fields =
|
|
266
|
-
|
|
267
|
-
);
|
|
268
|
-
const hasDuplicates = fields.some(
|
|
269
|
-
(field, index) => index > 0 && fields[index - 1].name === field.name,
|
|
270
|
-
);
|
|
271
|
-
if (hasDuplicates) return generateDelegate(generator, expression, frame);
|
|
260
|
+
const fields = getSortedFields(expression.fields);
|
|
261
|
+
if (hasDuplicateFields(fields)) return generateDelegate(generator, expression, frame);
|
|
272
262
|
const names = addReference(
|
|
273
263
|
generator,
|
|
274
264
|
fields.map((field) => field.name),
|
package/src/dependencies.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import {
|
|
1
|
+
import { getChildren, type Definition, type Expression } from "./ast";
|
|
2
|
+
import { isImpureBuiltin, type Environment } from "./evaluate";
|
|
3
3
|
|
|
4
4
|
const STATE_INDEPENDENT_KINDS = new Set([
|
|
5
5
|
"Prime",
|
package/src/environment.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
type Assumption,
|
|
4
|
+
type Declaration,
|
|
5
|
+
type Definition,
|
|
6
|
+
type Expression,
|
|
7
|
+
type Instance,
|
|
8
|
+
type Module,
|
|
9
|
+
type OperatorParam,
|
|
10
|
+
type SourceLocation,
|
|
10
11
|
} from "./ast";
|
|
11
12
|
import { BUNDLED_MODULES } from "./bundled-modules";
|
|
12
13
|
import type { ConfigValue, ModelConfig } from "./config";
|
|
@@ -16,8 +17,8 @@ import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
|
16
17
|
import {
|
|
17
18
|
createBigNumberError,
|
|
18
19
|
createRealNumberError,
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
evaluate,
|
|
21
|
+
getDefinition,
|
|
21
22
|
isTlcInteger,
|
|
22
23
|
type Builtin,
|
|
23
24
|
type Environment,
|
|
@@ -44,8 +45,9 @@ import {
|
|
|
44
45
|
import { createRuntime } from "./runtime";
|
|
45
46
|
import { CORE_OPERATORS, getStandardModule, STANDARD_DEPENDENCIES } from "./standard-modules";
|
|
46
47
|
import { resolveInstanceSubexpressions, resolveModuleSubexpressions } from "./subexpressions";
|
|
47
|
-
import { checkModuleLevels } from "./levels";
|
|
48
|
+
import { checkModuleLevels, createLevelAnalyzer } from "./levels";
|
|
48
49
|
import { checkInstances, type InstanceCheck } from "./substitution-levels";
|
|
50
|
+
import { getTlcDeclarationOrder } from "./tlc-order";
|
|
49
51
|
import {
|
|
50
52
|
checkInteger,
|
|
51
53
|
createModelValue,
|
|
@@ -212,6 +214,8 @@ export const createEnvironment = (
|
|
|
212
214
|
const root = parseModule(options.source);
|
|
213
215
|
const definitions = new Map<string, Definition>();
|
|
214
216
|
const parameterized = new Map<string, boolean>();
|
|
217
|
+
// The definition each instance copy came from, and the instance parameters the copy adds.
|
|
218
|
+
const copySources = new Map<string, { source: string; prefix: number }>();
|
|
215
219
|
const builtins = new Map<string, Builtin>(
|
|
216
220
|
CORE_OPERATORS.map((operator) => [operator.name, operator]),
|
|
217
221
|
);
|
|
@@ -379,10 +383,11 @@ export const createEnvironment = (
|
|
|
379
383
|
variables: new Set(linked.variables),
|
|
380
384
|
getDefinition: (key) => definitions.get(key),
|
|
381
385
|
isParameterized: (key) => parameterized.get(key) === true,
|
|
382
|
-
addDefinition: (name, create) => {
|
|
386
|
+
addDefinition: (source, name, create) => {
|
|
383
387
|
const key = createName(`${name}@${module.name}`);
|
|
384
388
|
definitions.set(key, create(key));
|
|
385
389
|
parameterized.set(key, !isParameterFree);
|
|
390
|
+
copySources.set(key, { source, prefix: instance.params.length });
|
|
386
391
|
return key;
|
|
387
392
|
},
|
|
388
393
|
createName,
|
|
@@ -544,7 +549,7 @@ export const createEnvironment = (
|
|
|
544
549
|
variables: new Set(linked.variables),
|
|
545
550
|
getDefinition: (key) => definitions.get(key),
|
|
546
551
|
isParameterized: (key) => parameterized.get(key) === true,
|
|
547
|
-
addDefinition: (name, create) => {
|
|
552
|
+
addDefinition: (_source, name, create) => {
|
|
548
553
|
const key = `${name}${KEY_SEPARATOR}${instance.module}${suffix}.${copies.length}`;
|
|
549
554
|
const definition = create(key);
|
|
550
555
|
copies.push(
|
|
@@ -872,11 +877,51 @@ export const createEnvironment = (
|
|
|
872
877
|
);
|
|
873
878
|
}
|
|
874
879
|
});
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
880
|
+
// TLC sets a module-scoped entry on the body of the module's definition, which every instance of
|
|
881
|
+
// the module shares.
|
|
882
|
+
const scopedKeys = new Set(
|
|
883
|
+
[...config.constants, ...config.substitutions]
|
|
884
|
+
.filter((item) => item.module !== undefined)
|
|
885
|
+
.flatMap(getScopedKeys),
|
|
886
|
+
);
|
|
887
|
+
const getOverride = (
|
|
888
|
+
key: string,
|
|
889
|
+
prefix: number,
|
|
890
|
+
): { key: string; prefix: number } | undefined => {
|
|
891
|
+
if (scopedKeys.has(key)) return { key, prefix };
|
|
892
|
+
const copy = copySources.get(key);
|
|
893
|
+
return copy && getOverride(copy.source, prefix + copy.prefix);
|
|
894
|
+
};
|
|
895
|
+
copySources.forEach((copy, key) => {
|
|
896
|
+
const override = getOverride(copy.source, copy.prefix);
|
|
897
|
+
const definition = definitions.get(key);
|
|
898
|
+
if (!override || !definition) return;
|
|
899
|
+
const params = definition.kind === "Operator" ? definition.params : [];
|
|
900
|
+
const alias = createLocalAlias(
|
|
901
|
+
key,
|
|
902
|
+
override.key,
|
|
903
|
+
params,
|
|
904
|
+
params.slice(override.prefix),
|
|
905
|
+
definition.location,
|
|
906
|
+
);
|
|
907
|
+
definitions.set(key, alias);
|
|
908
|
+
linkedRoot.visible.forEach((symbol, name) => {
|
|
909
|
+
if (symbol.key === key && name !== key) definitions.set(name, alias);
|
|
910
|
+
});
|
|
879
911
|
});
|
|
912
|
+
const unassigned = declaredConstants.filter(
|
|
913
|
+
(name) => !constants.has(name) && !substitutions.has(name) && !operatorKeys.has(name),
|
|
914
|
+
);
|
|
915
|
+
if (unassigned.length > 0) {
|
|
916
|
+
// TLC reports the first constant it finds unassigned in SANY's order of declarations.
|
|
917
|
+
const name =
|
|
918
|
+
getTlcDeclarationOrder(options.source, options.loadModule, "constant").find((constant) =>
|
|
919
|
+
unassigned.includes(constant),
|
|
920
|
+
) ?? unassigned[0];
|
|
921
|
+
throw new TlaConfigError(
|
|
922
|
+
`The constant parameter ${name} is not assigned a value by the configuration file.`,
|
|
923
|
+
);
|
|
924
|
+
}
|
|
880
925
|
|
|
881
926
|
const environment: Environment = {
|
|
882
927
|
runtime,
|
|
@@ -907,18 +952,41 @@ export const createEnvironment = (
|
|
|
907
952
|
getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
|
|
908
953
|
};
|
|
909
954
|
});
|
|
955
|
+
// TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
|
|
956
|
+
// computes is above constant. The bound counts the operators that TLC's Java code overrides with
|
|
957
|
+
// a minimum level as state-level, so the analyzer takes them for variables.
|
|
958
|
+
const analyzer = createLevelAnalyzer({
|
|
959
|
+
constants: new Set(constants.keys()),
|
|
960
|
+
variables: new Set([
|
|
961
|
+
...variables,
|
|
962
|
+
...["TLCGet", "Trace", "CounterExample"].filter((name) => builtins.has(name)),
|
|
963
|
+
]),
|
|
964
|
+
getDefinition: (name) => getDefinition(name, environment),
|
|
965
|
+
});
|
|
966
|
+
const isEvaluable = (body: Expression) => {
|
|
967
|
+
try {
|
|
968
|
+
evaluate(body, { environment, scope: undefined, state: undefined, next: undefined });
|
|
969
|
+
return true;
|
|
970
|
+
} catch (error) {
|
|
971
|
+
if (error instanceof TlaEvaluationError) return false;
|
|
972
|
+
throw error;
|
|
973
|
+
}
|
|
974
|
+
};
|
|
910
975
|
substitutions.forEach((target, name) => {
|
|
911
976
|
const definition = definitions.get(target);
|
|
912
977
|
if (
|
|
913
978
|
declaredConstants.includes(name) &&
|
|
914
|
-
definition &&
|
|
915
|
-
|
|
979
|
+
definition?.kind === "Operator" &&
|
|
980
|
+
definition.params.length === 0 &&
|
|
981
|
+
analyzer.analyzeDefinition(definition, new Map()).level > 0 &&
|
|
982
|
+
!isEvaluable(definition.body)
|
|
916
983
|
) {
|
|
917
984
|
throw new TlaConfigError(
|
|
918
985
|
`The configuration file substitutes constant ${name} with non-constant ${target}`,
|
|
919
986
|
);
|
|
920
987
|
}
|
|
921
988
|
});
|
|
989
|
+
runtime.isStarted = true;
|
|
922
990
|
return {
|
|
923
991
|
environment,
|
|
924
992
|
assumptions,
|
package/src/errors.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import { EXIT_STATUS, getExitCode
|
|
1
|
+
import { EXIT_STATUS, getExitCode } from "./exit-codes";
|
|
2
|
+
|
|
3
|
+
// How TLC reports a RuntimeException it did not expect, such as one raised while computing next
|
|
4
|
+
// states.
|
|
5
|
+
export const UNEXPECTED_EXCEPTION =
|
|
6
|
+
"TLC threw an unexpected exception.\nThis was probably caused by an error in the spec or model.\nSee the User Output or TLC Console for clues to what happened.\nThe exception was a java.lang.RuntimeException\n: ";
|
|
2
7
|
|
|
3
8
|
export class TlaError extends Error {
|
|
4
9
|
constructor(message: string, cause?: unknown) {
|
|
@@ -41,10 +46,21 @@ export class TlaEvaluationError extends TlaError {
|
|
|
41
46
|
// An evaluation error that TLC raises as an EvalException from a standard module's Java code. It
|
|
42
47
|
// keeps the name TlaEvaluationError, and index.ts doesn't export it.
|
|
43
48
|
export class TlaModuleError extends TlaEvaluationError {
|
|
49
|
+
exitCode: number;
|
|
50
|
+
|
|
44
51
|
constructor(message: string, exitCode: number = EXIT_STATUS.error) {
|
|
45
52
|
super(message);
|
|
46
53
|
this.name = "TlaEvaluationError";
|
|
47
|
-
|
|
54
|
+
this.exitCode = exitCode;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// TLC reports a successor state that the next-state relation leaves incomplete as an error of its
|
|
59
|
+
// own, not as an exception. It keeps the name TlaEvaluationError, and index.ts doesn't export it.
|
|
60
|
+
export class TlaIncompleteStateError extends TlaEvaluationError {
|
|
61
|
+
constructor(message: string) {
|
|
62
|
+
super(message);
|
|
63
|
+
this.name = "TlaEvaluationError";
|
|
48
64
|
}
|
|
49
65
|
}
|
|
50
66
|
|
package/src/evaluate.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
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,
|
|
13
|
+
type SourceRange,
|
|
12
14
|
} from "./ast";
|
|
13
15
|
import { getKindString } from "./builtins";
|
|
14
16
|
import { TlaEvaluationError } from "./errors";
|
|
@@ -34,11 +36,11 @@ import {
|
|
|
34
36
|
getIntervalElements,
|
|
35
37
|
getSequenceElements,
|
|
36
38
|
getValueHash,
|
|
37
|
-
isEnumerable,
|
|
38
39
|
isFunctionValue,
|
|
39
40
|
isOverriddenSet,
|
|
40
41
|
isSameValue,
|
|
41
42
|
markLambdaFunction,
|
|
43
|
+
isReducible,
|
|
42
44
|
isSetValue,
|
|
43
45
|
MAXIMUM_INTEGER,
|
|
44
46
|
MINIMUM_INTEGER,
|
|
@@ -262,96 +264,6 @@ export const expectBoolean = (value: Value, description: string) => {
|
|
|
262
264
|
return value;
|
|
263
265
|
};
|
|
264
266
|
|
|
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
267
|
export const getDefinition = (name: string, environment: Environment) =>
|
|
356
268
|
environment.definitions.get(environment.substitutions.get(name) ?? name);
|
|
357
269
|
|
|
@@ -859,11 +771,7 @@ const evaluateLocalIdentifier = (name: string, expression: Expression, context:
|
|
|
859
771
|
const global = findGlobalName(name, context.environment);
|
|
860
772
|
if (global?.kind === "Variable") {
|
|
861
773
|
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
|
-
}
|
|
774
|
+
if (value === undefined) throw createUnassignedError(name, expression);
|
|
867
775
|
return value;
|
|
868
776
|
}
|
|
869
777
|
if (global?.kind === "Value") return global.value;
|
|
@@ -889,14 +797,29 @@ const IMPURE_BUILTINS = new Set([
|
|
|
889
797
|
export const isImpureBuiltin = (name: string, environment: Environment) =>
|
|
890
798
|
IMPURE_BUILTINS.has(name) || environment.builtins.get(name)?.isStateLevel === true;
|
|
891
799
|
|
|
892
|
-
|
|
800
|
+
interface Impurity {
|
|
801
|
+
builtins: Set<string>;
|
|
802
|
+
definitions: WeakMap<Definition, boolean>;
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
const ANY_EFFECT: Impurity = { builtins: IMPURE_BUILTINS, definitions: new WeakMap() };
|
|
806
|
+
|
|
807
|
+
// An Assert that held holds again, so evaluating it twice looks the same as evaluating it once.
|
|
808
|
+
const REPEATED_EFFECT: Impurity = {
|
|
809
|
+
builtins: new Set([...IMPURE_BUILTINS].filter((name) => name !== "Assert")),
|
|
810
|
+
definitions: new WeakMap(),
|
|
811
|
+
};
|
|
893
812
|
|
|
894
813
|
const isEvaluatedOnce = (definition: Definition, environment: Environment) =>
|
|
895
814
|
definition.kind === "Operator" &&
|
|
896
815
|
definition.params.length === 0 &&
|
|
897
816
|
isConstantLevel(definition, environment);
|
|
898
817
|
|
|
899
|
-
const callsImpureBuiltin = (
|
|
818
|
+
const callsImpureBuiltin = (
|
|
819
|
+
expression: Expression,
|
|
820
|
+
environment: Environment,
|
|
821
|
+
impurity: Impurity,
|
|
822
|
+
): boolean => {
|
|
900
823
|
const name =
|
|
901
824
|
expression.kind === "Identifier"
|
|
902
825
|
? expression.name
|
|
@@ -905,29 +828,35 @@ const callsImpureBuiltin = (expression: Expression, environment: Environment): b
|
|
|
905
828
|
: undefined;
|
|
906
829
|
if (name !== undefined && !isLocallyResolved(expression)) {
|
|
907
830
|
const definition = getDefinition(name, environment);
|
|
908
|
-
|
|
831
|
+
const isImpureCall =
|
|
832
|
+
impurity.builtins.has(name) || environment.builtins.get(name)?.isStateLevel === true;
|
|
833
|
+
if (!definition && isImpureCall) return true;
|
|
909
834
|
if (
|
|
910
835
|
definition &&
|
|
911
836
|
!isEvaluatedOnce(definition, environment) &&
|
|
912
|
-
isImpureDefinition(definition, environment)
|
|
837
|
+
isImpureDefinition(definition, environment, impurity)
|
|
913
838
|
) {
|
|
914
839
|
return true;
|
|
915
840
|
}
|
|
916
841
|
}
|
|
917
|
-
return getChildren(expression).some((child) => callsImpureBuiltin(child, environment));
|
|
842
|
+
return getChildren(expression).some((child) => callsImpureBuiltin(child, environment, impurity));
|
|
918
843
|
};
|
|
919
844
|
|
|
920
845
|
// TLC evaluates a constant definition without parameters once, so only other definitions pass
|
|
921
846
|
// on the side effects and random choices of the builtins they call.
|
|
922
|
-
const isImpureDefinition = (
|
|
923
|
-
|
|
847
|
+
const isImpureDefinition = (
|
|
848
|
+
definition: Definition,
|
|
849
|
+
environment: Environment,
|
|
850
|
+
impurity = ANY_EFFECT,
|
|
851
|
+
): boolean => {
|
|
852
|
+
const cached = impurity.definitions.get(definition);
|
|
924
853
|
if (cached !== undefined) return cached;
|
|
925
|
-
|
|
854
|
+
impurity.definitions.set(definition, false);
|
|
926
855
|
const isImpure =
|
|
927
|
-
callsImpureBuiltin(definition.body, environment) ||
|
|
856
|
+
callsImpureBuiltin(definition.body, environment, impurity) ||
|
|
928
857
|
(definition.kind === "Function" &&
|
|
929
|
-
definition.bounds.some((bound) => callsImpureBuiltin(bound.set, environment)));
|
|
930
|
-
|
|
858
|
+
definition.bounds.some((bound) => callsImpureBuiltin(bound.set, environment, impurity)));
|
|
859
|
+
impurity.definitions.set(definition, isImpure);
|
|
931
860
|
return isImpure;
|
|
932
861
|
};
|
|
933
862
|
|
|
@@ -941,6 +870,12 @@ export const isImpure = (environment: Environment) =>
|
|
|
941
870
|
isImpureDefinition(definition, environment),
|
|
942
871
|
);
|
|
943
872
|
|
|
873
|
+
// Whether evaluating a definition again can show, such as by printing twice.
|
|
874
|
+
export const hasRepeatedEffects = (environment: Environment) =>
|
|
875
|
+
[...environment.definitions.values()].some((definition) =>
|
|
876
|
+
isImpureDefinition(definition, environment, REPEATED_EFFECT),
|
|
877
|
+
);
|
|
878
|
+
|
|
944
879
|
const closedConstants = new WeakMap<Expression, boolean>();
|
|
945
880
|
|
|
946
881
|
export const isClosedConstant = (expression: Expression, environment: Environment): boolean => {
|
|
@@ -1092,7 +1027,7 @@ export const getEvaluator = (expression: Expression, environment: Environment):
|
|
|
1092
1027
|
|
|
1093
1028
|
const entryEvaluators = new WeakMap<Expression, Evaluator>();
|
|
1094
1029
|
|
|
1095
|
-
const getEntryEvaluator = (expression: Expression, environment: Environment) => {
|
|
1030
|
+
export const getEntryEvaluator = (expression: Expression, environment: Environment) => {
|
|
1096
1031
|
const cached = entryEvaluators.get(expression);
|
|
1097
1032
|
if (cached) return cached;
|
|
1098
1033
|
const evaluator =
|
|
@@ -1116,11 +1051,7 @@ const compileIdentifier = (
|
|
|
1116
1051
|
const index = global.index;
|
|
1117
1052
|
return (context) => {
|
|
1118
1053
|
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
|
-
}
|
|
1054
|
+
if (value === undefined) throw createUnassignedError(name, expression);
|
|
1124
1055
|
return value;
|
|
1125
1056
|
};
|
|
1126
1057
|
}
|
|
@@ -1489,7 +1420,13 @@ const compileFunctionConstruction = (
|
|
|
1489
1420
|
const set = getEvaluator(bound.set, environment);
|
|
1490
1421
|
return (context) => {
|
|
1491
1422
|
const domain = isSingleName ? set(context) : getBoundsDomain(bounds, context);
|
|
1492
|
-
|
|
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)) {
|
|
1493
1430
|
const functionValue = createLazyFunction(
|
|
1494
1431
|
domain,
|
|
1495
1432
|
(argument) => body(withScope(context, bindArgument(bounds, context.scope, argument))),
|
|
@@ -1583,10 +1520,7 @@ const compileSetComprehension = (
|
|
|
1583
1520
|
predicate(withScope(context, bindPattern(context.scope, pattern, element))),
|
|
1584
1521
|
"a set filter predicate",
|
|
1585
1522
|
);
|
|
1586
|
-
|
|
1587
|
-
typeof base === "object" &&
|
|
1588
|
-
(base.kind === "Set" || base.kind === "MixedSet" || base.kind === "Interval");
|
|
1589
|
-
if (!isReducible) {
|
|
1523
|
+
if (!isReducible(base)) {
|
|
1590
1524
|
if (isSetValue(base)) return { kind: "FilteredSet", base, test };
|
|
1591
1525
|
throw new TlaEvaluationError(
|
|
1592
1526
|
`TLC encountered a non-enumerable quantifier bound ${formatValue(base)} at ${describeLocation(expression.bound.set)}`,
|
|
@@ -1632,11 +1566,24 @@ const compileSetComprehension = (
|
|
|
1632
1566
|
};
|
|
1633
1567
|
};
|
|
1634
1568
|
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1569
|
+
// TLC orders a record's fields by name.
|
|
1570
|
+
export const getSortedFields = (fields: RecordField[]) =>
|
|
1571
|
+
[...fields].sort((left, right) => compareValues(left.name, right.name));
|
|
1572
|
+
|
|
1573
|
+
export const hasDuplicateFields = (sortedFields: RecordField[]) =>
|
|
1574
|
+
sortedFields.some((field, index) => index > 0 && sortedFields[index - 1].name === field.name);
|
|
1575
|
+
|
|
1576
|
+
export const createUnassignedError = (name: string, expression: Expression) =>
|
|
1577
|
+
new TlaEvaluationError(`The variable ${name} is not assigned at ${describeLocation(expression)}`);
|
|
1578
|
+
|
|
1579
|
+
export const createPrimeOutsideActionError = (expression: Expression) =>
|
|
1580
|
+
new TlaEvaluationError(
|
|
1581
|
+
`A primed expression appears outside an action at ${describeLocation(expression)}`,
|
|
1639
1582
|
);
|
|
1583
|
+
|
|
1584
|
+
const compileRecord = (recordFields: RecordField[], environment: Environment): Evaluator => {
|
|
1585
|
+
const fields = getSortedFields(recordFields);
|
|
1586
|
+
const hasDuplicates = hasDuplicateFields(fields);
|
|
1640
1587
|
const values = fields.map((field) => getEvaluator(field.value, environment));
|
|
1641
1588
|
if (hasDuplicates) {
|
|
1642
1589
|
return (context) =>
|
|
@@ -1714,11 +1661,7 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1714
1661
|
case "Prime": {
|
|
1715
1662
|
const inner = child(expression.expression);
|
|
1716
1663
|
return (context) => {
|
|
1717
|
-
if (!context.next)
|
|
1718
|
-
throw new TlaEvaluationError(
|
|
1719
|
-
`A primed expression appears outside an action at ${describeLocation(expression)}`,
|
|
1720
|
-
);
|
|
1721
|
-
}
|
|
1664
|
+
if (!context.next) throw createPrimeOutsideActionError(expression);
|
|
1722
1665
|
return inner({
|
|
1723
1666
|
environment: context.environment,
|
|
1724
1667
|
scope: context.scope,
|
|
@@ -1757,7 +1700,9 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1757
1700
|
if (expectBoolean(arm.condition(context), "a CASE guard")) return arm.body(context);
|
|
1758
1701
|
}
|
|
1759
1702
|
if (other) return other(context);
|
|
1760
|
-
throw new TlaEvaluationError(
|
|
1703
|
+
throw new TlaEvaluationError(
|
|
1704
|
+
`Attempted to evaluate a CASE with no conditions true.\n${describeRange(expression)}`,
|
|
1705
|
+
);
|
|
1761
1706
|
};
|
|
1762
1707
|
}
|
|
1763
1708
|
case "Let": {
|
|
@@ -1796,9 +1741,7 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1796
1741
|
case "Record":
|
|
1797
1742
|
return compileRecord(expression.fields, environment);
|
|
1798
1743
|
case "RecordSet": {
|
|
1799
|
-
const fields =
|
|
1800
|
-
compareValues(left.name, right.name),
|
|
1801
|
-
);
|
|
1744
|
+
const fields = getSortedFields(expression.fields);
|
|
1802
1745
|
const names = fields.map((field) => field.name);
|
|
1803
1746
|
const sets = fields.map((field) => child(field.value));
|
|
1804
1747
|
return (context) => ({ kind: "RecordSet", names, sets: sets.map((set) => set(context)) });
|