tla-ts 0.0.2 → 0.0.3-dev.19c763f
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 +78 -12
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +60 -1
- package/dist/index.d.ts +60 -1
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +2 -2
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +21 -2
- package/dist/run-cli.js +21 -2
- package/dist/transfer.cjs +27 -15
- package/dist/transfer.js +27 -15
- package/package.json +3 -1
- package/src/ast.ts +134 -4
- package/src/check.ts +230 -62
- package/src/cli-options.ts +64 -0
- package/src/codegen.ts +9 -19
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +75 -16
- package/src/errors.ts +46 -3
- package/src/evaluate.ts +78 -136
- package/src/exit-codes.ts +21 -43
- package/src/fingerprints.ts +46 -11
- package/src/instances.ts +15 -14
- package/src/levels.ts +8 -8
- package/src/lexer.ts +11 -2
- package/src/live-check.ts +710 -0
- package/src/liveness.ts +5 -523
- package/src/obligation-action.ts +384 -0
- package/src/obligation-anon.ts +575 -0
- package/src/obligation-deref.ts +637 -0
- package/src/obligation-expression.ts +1060 -0
- package/src/obligation-format.ts +406 -0
- package/src/obligation-generate.ts +1074 -0
- package/src/obligation-levels.ts +329 -0
- package/src/obligation-module.ts +1042 -0
- package/src/obligation-printer.ts +889 -0
- package/src/obligation-proof.ts +76 -0
- package/src/obligation-source.ts +1084 -0
- package/src/obligation-tuples.ts +184 -0
- package/src/obligations.ts +316 -0
- package/src/parallel-worker.ts +166 -210
- package/src/parallel.ts +76 -35
- package/src/parser.ts +297 -53
- package/src/resolve.ts +16 -15
- package/src/run-cli.ts +353 -40
- package/src/runtime.ts +3 -1
- package/src/spec-description.ts +18 -6
- package/src/standard-levels.ts +3 -3
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +10 -10
- package/src/successors.ts +17 -9
- package/src/symmetry.ts +14 -2
- package/src/tlc-ext.ts +29 -12
- package/src/tlc-module.ts +9 -28
- package/src/tlc-order.ts +17 -3
- package/src/transfer.ts +22 -1
- package/src/values.ts +70 -23
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,7 +45,7 @@ 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";
|
|
49
50
|
import {
|
|
50
51
|
checkInteger,
|
|
@@ -212,6 +213,8 @@ export const createEnvironment = (
|
|
|
212
213
|
const root = parseModule(options.source);
|
|
213
214
|
const definitions = new Map<string, Definition>();
|
|
214
215
|
const parameterized = new Map<string, boolean>();
|
|
216
|
+
// The definition each instance copy came from, and the instance parameters the copy adds.
|
|
217
|
+
const copySources = new Map<string, { source: string; prefix: number }>();
|
|
215
218
|
const builtins = new Map<string, Builtin>(
|
|
216
219
|
CORE_OPERATORS.map((operator) => [operator.name, operator]),
|
|
217
220
|
);
|
|
@@ -379,10 +382,11 @@ export const createEnvironment = (
|
|
|
379
382
|
variables: new Set(linked.variables),
|
|
380
383
|
getDefinition: (key) => definitions.get(key),
|
|
381
384
|
isParameterized: (key) => parameterized.get(key) === true,
|
|
382
|
-
addDefinition: (name, create) => {
|
|
385
|
+
addDefinition: (source, name, create) => {
|
|
383
386
|
const key = createName(`${name}@${module.name}`);
|
|
384
387
|
definitions.set(key, create(key));
|
|
385
388
|
parameterized.set(key, !isParameterFree);
|
|
389
|
+
copySources.set(key, { source, prefix: instance.params.length });
|
|
386
390
|
return key;
|
|
387
391
|
},
|
|
388
392
|
createName,
|
|
@@ -544,7 +548,7 @@ export const createEnvironment = (
|
|
|
544
548
|
variables: new Set(linked.variables),
|
|
545
549
|
getDefinition: (key) => definitions.get(key),
|
|
546
550
|
isParameterized: (key) => parameterized.get(key) === true,
|
|
547
|
-
addDefinition: (name, create) => {
|
|
551
|
+
addDefinition: (_source, name, create) => {
|
|
548
552
|
const key = `${name}${KEY_SEPARATOR}${instance.module}${suffix}.${copies.length}`;
|
|
549
553
|
const definition = create(key);
|
|
550
554
|
copies.push(
|
|
@@ -872,6 +876,38 @@ export const createEnvironment = (
|
|
|
872
876
|
);
|
|
873
877
|
}
|
|
874
878
|
});
|
|
879
|
+
// TLC sets a module-scoped entry on the body of the module's definition, which every instance of
|
|
880
|
+
// the module shares.
|
|
881
|
+
const scopedKeys = new Set(
|
|
882
|
+
[...config.constants, ...config.substitutions]
|
|
883
|
+
.filter((item) => item.module !== undefined)
|
|
884
|
+
.flatMap(getScopedKeys),
|
|
885
|
+
);
|
|
886
|
+
const getOverride = (
|
|
887
|
+
key: string,
|
|
888
|
+
prefix: number,
|
|
889
|
+
): { key: string; prefix: number } | undefined => {
|
|
890
|
+
if (scopedKeys.has(key)) return { key, prefix };
|
|
891
|
+
const copy = copySources.get(key);
|
|
892
|
+
return copy && getOverride(copy.source, prefix + copy.prefix);
|
|
893
|
+
};
|
|
894
|
+
copySources.forEach((copy, key) => {
|
|
895
|
+
const override = getOverride(copy.source, copy.prefix);
|
|
896
|
+
const definition = definitions.get(key);
|
|
897
|
+
if (!override || !definition) return;
|
|
898
|
+
const params = definition.kind === "Operator" ? definition.params : [];
|
|
899
|
+
const alias = createLocalAlias(
|
|
900
|
+
key,
|
|
901
|
+
override.key,
|
|
902
|
+
params,
|
|
903
|
+
params.slice(override.prefix),
|
|
904
|
+
definition.location,
|
|
905
|
+
);
|
|
906
|
+
definitions.set(key, alias);
|
|
907
|
+
linkedRoot.visible.forEach((symbol, name) => {
|
|
908
|
+
if (symbol.key === key && name !== key) definitions.set(name, alias);
|
|
909
|
+
});
|
|
910
|
+
});
|
|
875
911
|
declaredConstants.forEach((name) => {
|
|
876
912
|
if (!constants.has(name) && !substitutions.has(name) && !operatorKeys.has(name)) {
|
|
877
913
|
throw new TlaConfigError(`The constant ${name} is not assigned a value by the configuration`);
|
|
@@ -907,18 +943,41 @@ export const createEnvironment = (
|
|
|
907
943
|
getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
|
|
908
944
|
};
|
|
909
945
|
});
|
|
946
|
+
// TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
|
|
947
|
+
// computes is above constant. The bound counts the operators that TLC's Java code overrides with
|
|
948
|
+
// a minimum level as state-level, so the analyzer takes them for variables.
|
|
949
|
+
const analyzer = createLevelAnalyzer({
|
|
950
|
+
constants: new Set(constants.keys()),
|
|
951
|
+
variables: new Set([
|
|
952
|
+
...variables,
|
|
953
|
+
...["TLCGet", "Trace", "CounterExample"].filter((name) => builtins.has(name)),
|
|
954
|
+
]),
|
|
955
|
+
getDefinition: (name) => getDefinition(name, environment),
|
|
956
|
+
});
|
|
957
|
+
const isEvaluable = (body: Expression) => {
|
|
958
|
+
try {
|
|
959
|
+
evaluate(body, { environment, scope: undefined, state: undefined, next: undefined });
|
|
960
|
+
return true;
|
|
961
|
+
} catch (error) {
|
|
962
|
+
if (error instanceof TlaEvaluationError) return false;
|
|
963
|
+
throw error;
|
|
964
|
+
}
|
|
965
|
+
};
|
|
910
966
|
substitutions.forEach((target, name) => {
|
|
911
967
|
const definition = definitions.get(target);
|
|
912
968
|
if (
|
|
913
969
|
declaredConstants.includes(name) &&
|
|
914
|
-
definition &&
|
|
915
|
-
|
|
970
|
+
definition?.kind === "Operator" &&
|
|
971
|
+
definition.params.length === 0 &&
|
|
972
|
+
analyzer.analyzeDefinition(definition, new Map()).level > 0 &&
|
|
973
|
+
!isEvaluable(definition.body)
|
|
916
974
|
) {
|
|
917
975
|
throw new TlaConfigError(
|
|
918
976
|
`The configuration file substitutes constant ${name} with non-constant ${target}`,
|
|
919
977
|
);
|
|
920
978
|
}
|
|
921
979
|
});
|
|
980
|
+
runtime.isStarted = true;
|
|
922
981
|
return {
|
|
923
982
|
environment,
|
|
924
983
|
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) {
|
|
@@ -7,10 +12,20 @@ export class TlaError extends Error {
|
|
|
7
12
|
}
|
|
8
13
|
}
|
|
9
14
|
|
|
15
|
+
export interface SyntaxFailure {
|
|
16
|
+
reason: string;
|
|
17
|
+
line: number;
|
|
18
|
+
column: number;
|
|
19
|
+
isEnd: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
10
22
|
export class TlaSyntaxError extends TlaError {
|
|
11
|
-
|
|
23
|
+
failure: SyntaxFailure | undefined;
|
|
24
|
+
|
|
25
|
+
constructor(message: string, cause?: unknown, failure?: SyntaxFailure) {
|
|
12
26
|
super(message, cause);
|
|
13
27
|
this.name = "TlaSyntaxError";
|
|
28
|
+
this.failure = failure;
|
|
14
29
|
}
|
|
15
30
|
}
|
|
16
31
|
|
|
@@ -31,10 +46,38 @@ export class TlaEvaluationError extends TlaError {
|
|
|
31
46
|
// An evaluation error that TLC raises as an EvalException from a standard module's Java code. It
|
|
32
47
|
// keeps the name TlaEvaluationError, and index.ts doesn't export it.
|
|
33
48
|
export class TlaModuleError extends TlaEvaluationError {
|
|
49
|
+
exitCode: number;
|
|
50
|
+
|
|
34
51
|
constructor(message: string, exitCode: number = EXIT_STATUS.error) {
|
|
35
52
|
super(message);
|
|
36
53
|
this.name = "TlaEvaluationError";
|
|
37
|
-
|
|
54
|
+
this.exitCode = exitCode;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface TlapmReport {
|
|
59
|
+
failure?: string;
|
|
60
|
+
exception?: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export class TlaProofError extends TlaError {
|
|
64
|
+
failure: string | undefined;
|
|
65
|
+
exception: string;
|
|
66
|
+
|
|
67
|
+
constructor(message: string, report: TlapmReport = {}) {
|
|
68
|
+
super(message);
|
|
69
|
+
this.name = "TlaProofError";
|
|
70
|
+
this.failure = report.exception === undefined ? (report.failure ?? message) : undefined;
|
|
71
|
+
this.exception = report.exception ?? `Failure(${JSON.stringify(this.failure)})`;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// tlapm fails one of its own assertions, which it reports as a bug in itself. It keeps the name
|
|
76
|
+
// TlaProofError, and index.ts doesn't export it.
|
|
77
|
+
export class TlapmBugError extends TlaProofError {
|
|
78
|
+
constructor(exception = "Assert_failure") {
|
|
79
|
+
super("Oops, this seems to be a bug in TLAPM.", { exception });
|
|
80
|
+
this.name = "TlaProofError";
|
|
38
81
|
}
|
|
39
82
|
}
|
|
40
83
|
|
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";
|
|
@@ -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
|
|
|
@@ -734,7 +646,9 @@ const compileArgumentBinder = (argument: Expression, environment: Environment):
|
|
|
734
646
|
if (global === undefined) {
|
|
735
647
|
return (context) => {
|
|
736
648
|
const binding = lookupScope(context.scope, argument.name);
|
|
737
|
-
return binding?.kind === "Value"
|
|
649
|
+
return binding?.kind === "Value" || binding?.kind === "Thunk"
|
|
650
|
+
? binding
|
|
651
|
+
: createThunk(argument, context);
|
|
738
652
|
};
|
|
739
653
|
}
|
|
740
654
|
}
|
|
@@ -857,11 +771,7 @@ const evaluateLocalIdentifier = (name: string, expression: Expression, context:
|
|
|
857
771
|
const global = findGlobalName(name, context.environment);
|
|
858
772
|
if (global?.kind === "Variable") {
|
|
859
773
|
const value = context.state?.[global.index];
|
|
860
|
-
if (value === undefined)
|
|
861
|
-
throw new TlaEvaluationError(
|
|
862
|
-
`The variable ${name} is not assigned at ${describeLocation(expression)}`,
|
|
863
|
-
);
|
|
864
|
-
}
|
|
774
|
+
if (value === undefined) throw createUnassignedError(name, expression);
|
|
865
775
|
return value;
|
|
866
776
|
}
|
|
867
777
|
if (global?.kind === "Value") return global.value;
|
|
@@ -887,14 +797,29 @@ const IMPURE_BUILTINS = new Set([
|
|
|
887
797
|
export const isImpureBuiltin = (name: string, environment: Environment) =>
|
|
888
798
|
IMPURE_BUILTINS.has(name) || environment.builtins.get(name)?.isStateLevel === true;
|
|
889
799
|
|
|
890
|
-
|
|
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
|
+
};
|
|
891
812
|
|
|
892
813
|
const isEvaluatedOnce = (definition: Definition, environment: Environment) =>
|
|
893
814
|
definition.kind === "Operator" &&
|
|
894
815
|
definition.params.length === 0 &&
|
|
895
816
|
isConstantLevel(definition, environment);
|
|
896
817
|
|
|
897
|
-
const callsImpureBuiltin = (
|
|
818
|
+
const callsImpureBuiltin = (
|
|
819
|
+
expression: Expression,
|
|
820
|
+
environment: Environment,
|
|
821
|
+
impurity: Impurity,
|
|
822
|
+
): boolean => {
|
|
898
823
|
const name =
|
|
899
824
|
expression.kind === "Identifier"
|
|
900
825
|
? expression.name
|
|
@@ -903,29 +828,35 @@ const callsImpureBuiltin = (expression: Expression, environment: Environment): b
|
|
|
903
828
|
: undefined;
|
|
904
829
|
if (name !== undefined && !isLocallyResolved(expression)) {
|
|
905
830
|
const definition = getDefinition(name, environment);
|
|
906
|
-
|
|
831
|
+
const isImpureCall =
|
|
832
|
+
impurity.builtins.has(name) || environment.builtins.get(name)?.isStateLevel === true;
|
|
833
|
+
if (!definition && isImpureCall) return true;
|
|
907
834
|
if (
|
|
908
835
|
definition &&
|
|
909
836
|
!isEvaluatedOnce(definition, environment) &&
|
|
910
|
-
isImpureDefinition(definition, environment)
|
|
837
|
+
isImpureDefinition(definition, environment, impurity)
|
|
911
838
|
) {
|
|
912
839
|
return true;
|
|
913
840
|
}
|
|
914
841
|
}
|
|
915
|
-
return getChildren(expression).some((child) => callsImpureBuiltin(child, environment));
|
|
842
|
+
return getChildren(expression).some((child) => callsImpureBuiltin(child, environment, impurity));
|
|
916
843
|
};
|
|
917
844
|
|
|
918
845
|
// TLC evaluates a constant definition without parameters once, so only other definitions pass
|
|
919
846
|
// on the side effects and random choices of the builtins they call.
|
|
920
|
-
const isImpureDefinition = (
|
|
921
|
-
|
|
847
|
+
const isImpureDefinition = (
|
|
848
|
+
definition: Definition,
|
|
849
|
+
environment: Environment,
|
|
850
|
+
impurity = ANY_EFFECT,
|
|
851
|
+
): boolean => {
|
|
852
|
+
const cached = impurity.definitions.get(definition);
|
|
922
853
|
if (cached !== undefined) return cached;
|
|
923
|
-
|
|
854
|
+
impurity.definitions.set(definition, false);
|
|
924
855
|
const isImpure =
|
|
925
|
-
callsImpureBuiltin(definition.body, environment) ||
|
|
856
|
+
callsImpureBuiltin(definition.body, environment, impurity) ||
|
|
926
857
|
(definition.kind === "Function" &&
|
|
927
|
-
definition.bounds.some((bound) => callsImpureBuiltin(bound.set, environment)));
|
|
928
|
-
|
|
858
|
+
definition.bounds.some((bound) => callsImpureBuiltin(bound.set, environment, impurity)));
|
|
859
|
+
impurity.definitions.set(definition, isImpure);
|
|
929
860
|
return isImpure;
|
|
930
861
|
};
|
|
931
862
|
|
|
@@ -939,6 +870,12 @@ export const isImpure = (environment: Environment) =>
|
|
|
939
870
|
isImpureDefinition(definition, environment),
|
|
940
871
|
);
|
|
941
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
|
+
|
|
942
879
|
const closedConstants = new WeakMap<Expression, boolean>();
|
|
943
880
|
|
|
944
881
|
export const isClosedConstant = (expression: Expression, environment: Environment): boolean => {
|
|
@@ -1114,11 +1051,7 @@ const compileIdentifier = (
|
|
|
1114
1051
|
const index = global.index;
|
|
1115
1052
|
return (context) => {
|
|
1116
1053
|
const value = context.state?.[index];
|
|
1117
|
-
if (value === undefined)
|
|
1118
|
-
throw new TlaEvaluationError(
|
|
1119
|
-
`The variable ${name} is not assigned at ${describeLocation(expression)}`,
|
|
1120
|
-
);
|
|
1121
|
-
}
|
|
1054
|
+
if (value === undefined) throw createUnassignedError(name, expression);
|
|
1122
1055
|
return value;
|
|
1123
1056
|
};
|
|
1124
1057
|
}
|
|
@@ -1630,11 +1563,24 @@ const compileSetComprehension = (
|
|
|
1630
1563
|
};
|
|
1631
1564
|
};
|
|
1632
1565
|
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1566
|
+
// TLC orders a record's fields by name.
|
|
1567
|
+
export const getSortedFields = (fields: RecordField[]) =>
|
|
1568
|
+
[...fields].sort((left, right) => compareValues(left.name, right.name));
|
|
1569
|
+
|
|
1570
|
+
export const hasDuplicateFields = (sortedFields: RecordField[]) =>
|
|
1571
|
+
sortedFields.some((field, index) => index > 0 && sortedFields[index - 1].name === field.name);
|
|
1572
|
+
|
|
1573
|
+
export const createUnassignedError = (name: string, expression: Expression) =>
|
|
1574
|
+
new TlaEvaluationError(`The variable ${name} is not assigned at ${describeLocation(expression)}`);
|
|
1575
|
+
|
|
1576
|
+
export const createPrimeOutsideActionError = (expression: Expression) =>
|
|
1577
|
+
new TlaEvaluationError(
|
|
1578
|
+
`A primed expression appears outside an action at ${describeLocation(expression)}`,
|
|
1637
1579
|
);
|
|
1580
|
+
|
|
1581
|
+
const compileRecord = (recordFields: RecordField[], environment: Environment): Evaluator => {
|
|
1582
|
+
const fields = getSortedFields(recordFields);
|
|
1583
|
+
const hasDuplicates = hasDuplicateFields(fields);
|
|
1638
1584
|
const values = fields.map((field) => getEvaluator(field.value, environment));
|
|
1639
1585
|
if (hasDuplicates) {
|
|
1640
1586
|
return (context) =>
|
|
@@ -1712,11 +1658,7 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1712
1658
|
case "Prime": {
|
|
1713
1659
|
const inner = child(expression.expression);
|
|
1714
1660
|
return (context) => {
|
|
1715
|
-
if (!context.next)
|
|
1716
|
-
throw new TlaEvaluationError(
|
|
1717
|
-
`A primed expression appears outside an action at ${describeLocation(expression)}`,
|
|
1718
|
-
);
|
|
1719
|
-
}
|
|
1661
|
+
if (!context.next) throw createPrimeOutsideActionError(expression);
|
|
1720
1662
|
return inner({
|
|
1721
1663
|
environment: context.environment,
|
|
1722
1664
|
scope: context.scope,
|
|
@@ -1755,7 +1697,9 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1755
1697
|
if (expectBoolean(arm.condition(context), "a CASE guard")) return arm.body(context);
|
|
1756
1698
|
}
|
|
1757
1699
|
if (other) return other(context);
|
|
1758
|
-
throw new TlaEvaluationError(
|
|
1700
|
+
throw new TlaEvaluationError(
|
|
1701
|
+
`Attempted to evaluate a CASE with no conditions true.\n${describeRange(expression)}`,
|
|
1702
|
+
);
|
|
1759
1703
|
};
|
|
1760
1704
|
}
|
|
1761
1705
|
case "Let": {
|
|
@@ -1794,9 +1738,7 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1794
1738
|
case "Record":
|
|
1795
1739
|
return compileRecord(expression.fields, environment);
|
|
1796
1740
|
case "RecordSet": {
|
|
1797
|
-
const fields =
|
|
1798
|
-
compareValues(left.name, right.name),
|
|
1799
|
-
);
|
|
1741
|
+
const fields = getSortedFields(expression.fields);
|
|
1800
1742
|
const names = fields.map((field) => field.name);
|
|
1801
1743
|
const sets = fields.map((field) => child(field.value));
|
|
1802
1744
|
return (context) => ({ kind: "RecordSet", names, sets: sets.map((set) => set(context)) });
|
package/src/exit-codes.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { LivenessChecker } from "./
|
|
1
|
+
import type { LivenessChecker } from "./live-check";
|
|
2
2
|
|
|
3
3
|
// TLC's exit statuses, from EC.ExitStatus.
|
|
4
4
|
export const EXIT_STATUS = {
|
|
@@ -17,12 +17,6 @@ export const EXIT_STATUS = {
|
|
|
17
17
|
} as const;
|
|
18
18
|
|
|
19
19
|
const exitCodes = new WeakMap<Error, number>();
|
|
20
|
-
const moduleExitCodes = new WeakMap<Error, number>();
|
|
21
|
-
const phase = { isNextState: false };
|
|
22
|
-
|
|
23
|
-
export const setNextStatePhase = (isNextState: boolean) => {
|
|
24
|
-
phase.isNextState = isNextState;
|
|
25
|
-
};
|
|
26
20
|
|
|
27
21
|
// The innermost site that knows TLC's exit status for an error sets it first and wins.
|
|
28
22
|
export const setExitCode = <Thrown>(error: Thrown, code: number) => {
|
|
@@ -30,46 +24,30 @@ export const setExitCode = <Thrown>(error: Thrown, code: number) => {
|
|
|
30
24
|
return error;
|
|
31
25
|
};
|
|
32
26
|
|
|
33
|
-
|
|
34
|
-
// escapes the computation of next states. In every other phase, the phase decides the code.
|
|
35
|
-
export const setModuleExitCode = (error: Error, code: number) => {
|
|
36
|
-
if (phase.isNextState) moduleExitCodes.set(error, code);
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export const getExitCode = (error: Error) => exitCodes.get(error) ?? moduleExitCodes.get(error);
|
|
27
|
+
export const getExitCode = (error: Error) => exitCodes.get(error);
|
|
40
28
|
|
|
41
29
|
// TLC exits with 255 when it fails to process or evaluate a liveness property, in any phase.
|
|
42
|
-
|
|
43
|
-
create: () => LivenessChecker | undefined,
|
|
44
|
-
): LivenessChecker | undefined => {
|
|
30
|
+
const withErrorExitCode = <Result>(run: () => Result) => {
|
|
45
31
|
try {
|
|
46
|
-
|
|
47
|
-
return (
|
|
48
|
-
checker && {
|
|
49
|
-
addInitialState: (index, state) => {
|
|
50
|
-
try {
|
|
51
|
-
checker.addInitialState(index, state);
|
|
52
|
-
} catch (error) {
|
|
53
|
-
throw setExitCode(error, EXIT_STATUS.error);
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
addState: (index, state, successors) => {
|
|
57
|
-
try {
|
|
58
|
-
return checker.addState(index, state, successors);
|
|
59
|
-
} catch (error) {
|
|
60
|
-
throw setExitCode(error, EXIT_STATUS.error);
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
findViolation: () => {
|
|
64
|
-
try {
|
|
65
|
-
return checker.findViolation();
|
|
66
|
-
} catch (error) {
|
|
67
|
-
throw setExitCode(error, EXIT_STATUS.error);
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
}
|
|
71
|
-
);
|
|
32
|
+
return run();
|
|
72
33
|
} catch (error) {
|
|
73
34
|
throw setExitCode(error, EXIT_STATUS.error);
|
|
74
35
|
}
|
|
75
36
|
};
|
|
37
|
+
|
|
38
|
+
export const withLivenessExitCode = (
|
|
39
|
+
create: () => LivenessChecker | undefined,
|
|
40
|
+
): LivenessChecker | undefined => {
|
|
41
|
+
const checker = withErrorExitCode(create);
|
|
42
|
+
return (
|
|
43
|
+
checker && {
|
|
44
|
+
addInitialState: (index, state) =>
|
|
45
|
+
withErrorExitCode(() => checker.addInitialState(index, state)),
|
|
46
|
+
addState: (index, state, successors) =>
|
|
47
|
+
withErrorExitCode(() => checker.addState(index, state, successors)),
|
|
48
|
+
findViolation: () => withErrorExitCode(() => checker.findViolation()),
|
|
49
|
+
getViolatedProperties: (path, cycleStart) =>
|
|
50
|
+
withErrorExitCode(() => checker.getViolatedProperties(path, cycleStart)),
|
|
51
|
+
}
|
|
52
|
+
);
|
|
53
|
+
};
|