tla-ts 0.0.3-dev.5e999f2 → 0.0.3-dev.62cf3d1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.cts +24 -1
- package/dist/index.d.ts +24 -1
- package/dist/run-cli.cjs +5 -3
- package/dist/run-cli.js +20 -18
- package/dist/transfer.cjs +27 -21
- package/dist/transfer.js +27 -21
- package/package.json +1 -1
- package/src/ast.ts +3 -0
- package/src/builtins.ts +9 -3
- package/src/check.ts +234 -53
- package/src/config.ts +224 -170
- package/src/environment.ts +31 -6
- package/src/errors.ts +52 -1
- package/src/evaluate.ts +136 -15
- package/src/exit-codes.ts +22 -3
- package/src/fingerprints.ts +2 -1
- package/src/instances.ts +1 -1
- package/src/levels.ts +63 -28
- package/src/live-check.ts +3 -1
- package/src/liveness.ts +66 -6
- package/src/parser.ts +21 -10
- package/src/resolve.ts +106 -29
- package/src/run-cli.ts +56 -9
- package/src/spec-description.ts +1 -2
- package/src/standard-modules.ts +52 -38
- package/src/successors.ts +105 -27
- package/src/value-sources.ts +29 -0
- package/src/values.ts +122 -42
package/src/evaluate.ts
CHANGED
|
@@ -10,16 +10,16 @@ import {
|
|
|
10
10
|
type NumberExpression,
|
|
11
11
|
type OperatorParam,
|
|
12
12
|
type RecordField,
|
|
13
|
-
type SourceRange,
|
|
14
13
|
} from "./ast";
|
|
15
14
|
import { getKindString } from "./builtins";
|
|
16
|
-
import { TlaEvaluationError } from "./errors";
|
|
17
|
-
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
15
|
+
import { formatRange, TlaEvaluationError, TlaUnassignedError } from "./errors";
|
|
16
|
+
import { EXIT_STATUS, setExitCode, setOwnExitCode } from "./exit-codes";
|
|
18
17
|
import { generateEvaluator } from "./codegen";
|
|
19
18
|
import type { CheckerRuntime } from "./runtime";
|
|
20
19
|
import { getReadVariables } from "./dependencies";
|
|
21
20
|
import { contextualApplications, mapChildren, substitutedExpressions } from "./instances";
|
|
22
21
|
import { isEnabled } from "./successors";
|
|
22
|
+
import { getFingerprintTrace, valueSources, withSource } from "./value-sources";
|
|
23
23
|
import {
|
|
24
24
|
applyFunction,
|
|
25
25
|
combineHash,
|
|
@@ -35,6 +35,7 @@ import {
|
|
|
35
35
|
getDomain,
|
|
36
36
|
getFunctionDomain,
|
|
37
37
|
getIntervalElements,
|
|
38
|
+
getLazySubsets,
|
|
38
39
|
getSequenceElements,
|
|
39
40
|
getValueHash,
|
|
40
41
|
isFunctionValue,
|
|
@@ -49,6 +50,7 @@ import {
|
|
|
49
50
|
toFiniteSet,
|
|
50
51
|
updateFunction,
|
|
51
52
|
valuesEqual,
|
|
53
|
+
type FilteredSetValue,
|
|
52
54
|
type FunctionValue,
|
|
53
55
|
type LazyFunctionSource,
|
|
54
56
|
type LazyFunctionValue,
|
|
@@ -117,6 +119,7 @@ export interface Environment {
|
|
|
117
119
|
definitionValues: Map<Definition, Value>;
|
|
118
120
|
constantLevels: Map<Definition, boolean>;
|
|
119
121
|
evaluators: WeakMap<Expression, Evaluator>;
|
|
122
|
+
trace?: Expression[];
|
|
120
123
|
}
|
|
121
124
|
|
|
122
125
|
export interface Context {
|
|
@@ -146,9 +149,6 @@ const EXCEPT_AT = "@";
|
|
|
146
149
|
export const describeLocation = (expression: Expression) =>
|
|
147
150
|
`line ${expression.location.line}, column ${expression.location.column}`;
|
|
148
151
|
|
|
149
|
-
export const formatRange = (range: SourceRange) =>
|
|
150
|
-
`line ${range.start.line}, col ${range.start.column} to line ${range.end.line}, col ${range.end.column} of module ${range.module}`;
|
|
151
|
-
|
|
152
152
|
export const describeRange = (expression: Expression) => {
|
|
153
153
|
const range = expression.location.range;
|
|
154
154
|
return range ? formatRange(range) : describeLocation(expression);
|
|
@@ -343,9 +343,8 @@ export const getEnumeratedElements = (set: Value, expression: Expression) => {
|
|
|
343
343
|
: `TLC encountered the non-enumerable quantifier bound\n${text}\n${describeRange(expression)}\n${explanation}`,
|
|
344
344
|
);
|
|
345
345
|
}
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
: toFiniteSet(set).elements;
|
|
346
|
+
if (typeof set === "object" && set.kind === "Interval") return getIntervalElements(set);
|
|
347
|
+
return getLazySubsets(set) ?? toFiniteSet(set).elements;
|
|
349
348
|
};
|
|
350
349
|
|
|
351
350
|
export const forEachBinding = (
|
|
@@ -1023,16 +1022,80 @@ const isStateCached = (expression: Expression, environment: Environment) =>
|
|
|
1023
1022
|
getReadVariables(expression, environment) !== undefined &&
|
|
1024
1023
|
!hasFreeLocals(expression, NO_BOUND_NAMES);
|
|
1025
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
|
+
|
|
1026
1070
|
export const getEvaluator = (expression: Expression, environment: Environment): Evaluator => {
|
|
1027
1071
|
const cached = environment.evaluators.get(expression);
|
|
1028
1072
|
if (cached) return cached;
|
|
1029
1073
|
const compiled = compile(expression, environment);
|
|
1030
|
-
const
|
|
1074
|
+
const cachedEvaluator =
|
|
1031
1075
|
isWorthCaching(expression) && isClosedConstant(expression, environment)
|
|
1032
1076
|
? cacheFirstValue(compiled)
|
|
1033
1077
|
: isStateCached(expression, environment)
|
|
1034
1078
|
? cacheStateValue(compiled)
|
|
1035
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;
|
|
1036
1099
|
environment.evaluators.set(expression, evaluator);
|
|
1037
1100
|
return evaluator;
|
|
1038
1101
|
};
|
|
@@ -1040,6 +1103,7 @@ export const getEvaluator = (expression: Expression, environment: Environment):
|
|
|
1040
1103
|
const entryEvaluators = new WeakMap<Expression, Evaluator>();
|
|
1041
1104
|
|
|
1042
1105
|
export const getEntryEvaluator = (expression: Expression, environment: Environment) => {
|
|
1106
|
+
if (environment.trace) return getEvaluator(expression, environment);
|
|
1043
1107
|
const cached = entryEvaluators.get(expression);
|
|
1044
1108
|
if (cached) return cached;
|
|
1045
1109
|
const evaluator =
|
|
@@ -1148,11 +1212,32 @@ const getLazyArguments = (args: Expression[], context: Context): OperatorValue[]
|
|
|
1148
1212
|
expression: arg,
|
|
1149
1213
|
}));
|
|
1150
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
|
+
|
|
1151
1225
|
const compileBuiltinCall = (
|
|
1152
1226
|
builtin: Builtin,
|
|
1153
1227
|
args: Expression[],
|
|
1154
1228
|
environment: Environment,
|
|
1155
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
|
+
}
|
|
1156
1241
|
if (builtin.isLazy) {
|
|
1157
1242
|
return (context) => builtin.evaluate(getLazyArguments(args, context), context);
|
|
1158
1243
|
}
|
|
@@ -1312,6 +1397,13 @@ const compileApply = (
|
|
|
1312
1397
|
if (global?.kind === "Builtin" && global.name === "DOMAIN") {
|
|
1313
1398
|
const operand = getEvaluator(args[0], environment);
|
|
1314
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
|
+
}
|
|
1315
1407
|
return (context) => domain(operand(context));
|
|
1316
1408
|
}
|
|
1317
1409
|
// TLC sorts the value of a constant TLCEval argument only where no context binds a name.
|
|
@@ -1327,6 +1419,13 @@ const compileApply = (
|
|
|
1327
1419
|
const left = getEvaluator(args[0], environment);
|
|
1328
1420
|
const right = getEvaluator(args[1], environment);
|
|
1329
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
|
+
}
|
|
1330
1429
|
return (context) => subset(left(context), right(context));
|
|
1331
1430
|
}
|
|
1332
1431
|
if (global?.kind === "Builtin") return compileBuiltinCall(global, args, environment);
|
|
@@ -1532,13 +1631,21 @@ const compileSetComprehension = (
|
|
|
1532
1631
|
const pattern = expression.bound.pattern;
|
|
1533
1632
|
return (context) => {
|
|
1534
1633
|
const base = set(context);
|
|
1535
|
-
const
|
|
1634
|
+
const getTest = (evaluatePredicate: Evaluator) => (element: Value) =>
|
|
1536
1635
|
expectBoolean(
|
|
1537
|
-
|
|
1636
|
+
evaluatePredicate(withScope(context, bindPattern(context.scope, pattern, element))),
|
|
1538
1637
|
"a set filter predicate",
|
|
1539
1638
|
);
|
|
1639
|
+
const test = getTest(predicate);
|
|
1540
1640
|
if (!isReducible(base)) {
|
|
1541
|
-
if (isSetValue(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
|
+
}
|
|
1542
1649
|
throw new TlaEvaluationError(
|
|
1543
1650
|
`TLC encountered a non-enumerable quantifier bound ${formatValue(base)} at ${describeLocation(expression.bound.set)}`,
|
|
1544
1651
|
);
|
|
@@ -1591,8 +1698,12 @@ export const hasDuplicateFields = (sortedFields: RecordField[]) =>
|
|
|
1591
1698
|
sortedFields.some((field, index) => index > 0 && sortedFields[index - 1].name === field.name);
|
|
1592
1699
|
|
|
1593
1700
|
export const createUnassignedError = (name: string, expression: Expression) =>
|
|
1594
|
-
|
|
1595
|
-
|
|
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,
|
|
1596
1707
|
);
|
|
1597
1708
|
|
|
1598
1709
|
export const createPrimeOutsideActionError = (expression: Expression) =>
|
|
@@ -1782,6 +1893,16 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1782
1893
|
const evaluateFunction = child(expression.function);
|
|
1783
1894
|
const args = expression.args.map(child);
|
|
1784
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
|
+
}
|
|
1785
1906
|
if (args.length === 1) {
|
|
1786
1907
|
const argument = args[0];
|
|
1787
1908
|
return (context) =>
|
package/src/exit-codes.ts
CHANGED
|
@@ -26,7 +26,19 @@ export const setExitCode = <Thrown>(error: Thrown, code: number) => {
|
|
|
26
26
|
|
|
27
27
|
export const getExitCode = (error: Error) => exitCodes.get(error);
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
const ownExitCodes = new WeakMap<Error, number>();
|
|
30
|
+
|
|
31
|
+
// The exit status of the error code TLC gives an EvalException, which TLC uses only where it
|
|
32
|
+
// doesn't report the error again as a nested expression.
|
|
33
|
+
export const setOwnExitCode = <Thrown extends Error>(error: Thrown, code: number) => {
|
|
34
|
+
ownExitCodes.set(error, code);
|
|
35
|
+
return error;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const getOwnExitCode = (error: unknown) =>
|
|
39
|
+
error instanceof Error ? ownExitCodes.get(error) : undefined;
|
|
40
|
+
|
|
41
|
+
// TLC exits with 255 when it fails to process or evaluate a liveness property.
|
|
30
42
|
const withErrorExitCode = <Result>(run: () => Result) => {
|
|
31
43
|
try {
|
|
32
44
|
return run();
|
|
@@ -41,8 +53,15 @@ export const withLivenessExitCode = (
|
|
|
41
53
|
const checker = withErrorExitCode(create);
|
|
42
54
|
return (
|
|
43
55
|
checker && {
|
|
44
|
-
|
|
45
|
-
|
|
56
|
+
// TLC replays the initial states to report an error, but skips the liveness check of a
|
|
57
|
+
// state it has already seen, so it exits with the status of the error's own code.
|
|
58
|
+
addInitialState: (index, state) => {
|
|
59
|
+
try {
|
|
60
|
+
checker.addInitialState(index, state);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
throw setExitCode(error, getOwnExitCode(error) ?? EXIT_STATUS.error);
|
|
63
|
+
}
|
|
64
|
+
},
|
|
46
65
|
addState: (index, state, successors) =>
|
|
47
66
|
withErrorExitCode(() => checker.addState(index, state, successors)),
|
|
48
67
|
findViolation: () => withErrorExitCode(() => checker.findViolation()),
|
package/src/fingerprints.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getValueFingerprint, getValueHash, type Value } from "./values";
|
|
1
|
+
import { assertFingerprintable, getValueFingerprint, getValueHash, type Value } from "./values";
|
|
2
2
|
|
|
3
3
|
const EMPTY = 0;
|
|
4
4
|
const INITIAL_CAPACITY = 1 << 12;
|
|
@@ -20,6 +20,7 @@ export const writeStateFingerprint = (state: Value[], fingerprint: Int32Array) =
|
|
|
20
20
|
first = Math.imul(first ^ getValueHash(state[index]), 0x01000193);
|
|
21
21
|
second = Math.imul(second ^ getValueFingerprint(state[index]), 0x5bd1e995);
|
|
22
22
|
}
|
|
23
|
+
assertFingerprintable(state);
|
|
23
24
|
fingerprint[0] = mix(first) || 1;
|
|
24
25
|
fingerprint[1] = mix(second);
|
|
25
26
|
};
|
package/src/instances.ts
CHANGED
|
@@ -164,7 +164,7 @@ export const mapChildren = (
|
|
|
164
164
|
case "RecordSet":
|
|
165
165
|
return {
|
|
166
166
|
...expression,
|
|
167
|
-
fields: expression.fields.map((field) => ({
|
|
167
|
+
fields: expression.fields.map((field) => ({ ...field, value: plain(field.value) })),
|
|
168
168
|
};
|
|
169
169
|
case "Except":
|
|
170
170
|
return {
|
package/src/levels.ts
CHANGED
|
@@ -5,11 +5,10 @@ import {
|
|
|
5
5
|
type Expression,
|
|
6
6
|
type Module,
|
|
7
7
|
type OperatorParam,
|
|
8
|
-
type SourceLocation,
|
|
9
8
|
} from "./ast";
|
|
10
|
-
import { TlaSyntaxError } from "./errors";
|
|
9
|
+
import { createSemanticError, TlaSyntaxError } from "./errors";
|
|
11
10
|
import { mapChildren } from "./instances";
|
|
12
|
-
import { getBoundNames, getPatternNames } from "./resolve";
|
|
11
|
+
import { collectSemanticErrors, getBoundNames, getPatternNames } from "./resolve";
|
|
13
12
|
import { STANDARD_LEVELS } from "./standard-levels";
|
|
14
13
|
|
|
15
14
|
interface LevelData {
|
|
@@ -34,6 +33,7 @@ interface LevelContext {
|
|
|
34
33
|
constants: Set<string>;
|
|
35
34
|
variables: Set<string>;
|
|
36
35
|
getDefinition: (name: string) => Definition | undefined;
|
|
36
|
+
getQualifiedName?: (name: string) => string | undefined;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
type ScopeEntry =
|
|
@@ -176,8 +176,13 @@ export const ACTION_LEVEL = 2;
|
|
|
176
176
|
|
|
177
177
|
export const TEMPORAL_LEVEL = 3;
|
|
178
178
|
|
|
179
|
-
|
|
180
|
-
|
|
179
|
+
// SANY reports a level error at the range of the expression it rejects.
|
|
180
|
+
const createLevelError = (message: string, expression: Expression) =>
|
|
181
|
+
expression.location.range
|
|
182
|
+
? createSemanticError(`${message}.`, expression.location.range)
|
|
183
|
+
: new TlaSyntaxError(
|
|
184
|
+
`${message} at line ${expression.location.line}, column ${expression.location.column}`,
|
|
185
|
+
);
|
|
181
186
|
|
|
182
187
|
const LOGICAL_OPERATORS = new Set(["/\\", "\\/", "=>", "<=>"]);
|
|
183
188
|
|
|
@@ -201,7 +206,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
201
206
|
if (context.variables.has(name)) return { kind: "Declared", name, level: 1, arity: 0 };
|
|
202
207
|
const definition = context.getDefinition(name);
|
|
203
208
|
if (definition) {
|
|
204
|
-
|
|
209
|
+
const levels = analyzeDefinition(definition, new Map());
|
|
210
|
+
// SANY names a definition of an instance by its qualified name, as in I!Foo.
|
|
211
|
+
const qualifiedName = context.getQualifiedName?.(name);
|
|
212
|
+
return {
|
|
213
|
+
kind: "Defined",
|
|
214
|
+
levels: qualifiedName ? { ...levels, name: qualifiedName } : levels,
|
|
215
|
+
};
|
|
205
216
|
}
|
|
206
217
|
const signature = CORE_SIGNATURES.get(name) ?? STANDARD_LEVELS.get(name);
|
|
207
218
|
if (signature) return { kind: "Defined", levels: signature };
|
|
@@ -237,13 +248,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
237
248
|
if (operand.operator || operand.declared) return;
|
|
238
249
|
if (operand.data.level > getMaxLevel(operator, index)) {
|
|
239
250
|
throw new TlaSyntaxError(
|
|
240
|
-
`Level error in applying operator ${operator.name}
|
|
251
|
+
`Level error in applying operator ${operator.name}:\nThe level of argument ${index + 1} exceeds the maximum level allowed by the operator`,
|
|
241
252
|
);
|
|
242
253
|
}
|
|
243
254
|
});
|
|
244
255
|
if (ranges.some((range) => range.level > ACTION_LEVEL)) {
|
|
245
256
|
throw new TlaSyntaxError(
|
|
246
|
-
`Level error in applying operator ${operator.name}
|
|
257
|
+
`Level error in applying operator ${operator.name}:\nThe level of the range for a bounded variable exceeds the maximum level allowed by the operator`,
|
|
247
258
|
);
|
|
248
259
|
}
|
|
249
260
|
const getOperand = (formal: string) => {
|
|
@@ -444,10 +455,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
444
455
|
? applyDeclared(resolved.name, resolved.level, operands)
|
|
445
456
|
: applyDefined(resolved.levels, operands);
|
|
446
457
|
|
|
447
|
-
const checkJunction = (operands: Operand[],
|
|
458
|
+
const checkJunction = (operands: Operand[], expression: Expression) => {
|
|
448
459
|
const levels = operands.map((operand) => operand.data.level);
|
|
449
460
|
if (levels.includes(ACTION_LEVEL) && levels.includes(TEMPORAL_LEVEL)) {
|
|
450
|
-
throw createLevelError(
|
|
461
|
+
throw createLevelError(
|
|
462
|
+
"Formula has both temporal formula and action as arguments",
|
|
463
|
+
expression,
|
|
464
|
+
);
|
|
451
465
|
}
|
|
452
466
|
};
|
|
453
467
|
|
|
@@ -456,10 +470,10 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
456
470
|
if (isLeadsTo && operands.some((operand) => operand.data.level === ACTION_LEVEL)) {
|
|
457
471
|
throw createLevelError(
|
|
458
472
|
"Action used where only temporal formula or state predicate allowed",
|
|
459
|
-
expression
|
|
473
|
+
expression,
|
|
460
474
|
);
|
|
461
475
|
}
|
|
462
|
-
if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression
|
|
476
|
+
if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression);
|
|
463
477
|
};
|
|
464
478
|
|
|
465
479
|
const checkTemporal = (
|
|
@@ -473,7 +487,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
473
487
|
expression.kind === "Always"
|
|
474
488
|
? "[] followed by action not of form [A]_v"
|
|
475
489
|
: "<> followed by action not of form <<A>>_v",
|
|
476
|
-
expression
|
|
490
|
+
expression,
|
|
477
491
|
);
|
|
478
492
|
};
|
|
479
493
|
|
|
@@ -501,7 +515,17 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
501
515
|
return data;
|
|
502
516
|
};
|
|
503
517
|
|
|
518
|
+
// SANY reports a level error of an operator's arguments at the application.
|
|
504
519
|
const analyze = (expression: Expression, scope: Scope): LevelData => {
|
|
520
|
+
try {
|
|
521
|
+
return analyzeNode(expression, scope);
|
|
522
|
+
} catch (error) {
|
|
523
|
+
if (!(error instanceof TlaSyntaxError) || error.sany) throw error;
|
|
524
|
+
throw createLevelError(error.message, expression);
|
|
525
|
+
}
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
const analyzeNode = (expression: Expression, scope: Scope): LevelData => {
|
|
505
529
|
switch (expression.kind) {
|
|
506
530
|
case "Number":
|
|
507
531
|
case "Decimal":
|
|
@@ -521,7 +545,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
521
545
|
case "And":
|
|
522
546
|
case "Or": {
|
|
523
547
|
const operands = toOperands(expression.operands, scope);
|
|
524
|
-
checkJunction(operands, expression
|
|
548
|
+
checkJunction(operands, expression);
|
|
525
549
|
return applyDefined(JUNCTION_LEVELS, operands);
|
|
526
550
|
}
|
|
527
551
|
case "Prime":
|
|
@@ -561,13 +585,14 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
561
585
|
expression.bounds.map((bound) => bound.set),
|
|
562
586
|
getBoundNames(expression.bounds),
|
|
563
587
|
);
|
|
564
|
-
|
|
565
|
-
data.level === TEMPORAL_LEVEL
|
|
566
|
-
|
|
567
|
-
|
|
588
|
+
const actionBound =
|
|
589
|
+
data.level === TEMPORAL_LEVEL
|
|
590
|
+
? expression.bounds.find((bound) => analyze(bound.set, scope).level === ACTION_LEVEL)
|
|
591
|
+
: undefined;
|
|
592
|
+
if (actionBound) {
|
|
568
593
|
throw createLevelError(
|
|
569
594
|
"Action-level bound of quantified temporal formula",
|
|
570
|
-
|
|
595
|
+
actionBound.set,
|
|
571
596
|
);
|
|
572
597
|
}
|
|
573
598
|
return data;
|
|
@@ -838,13 +863,23 @@ const analyzeAssumeProve = (
|
|
|
838
863
|
// SANY checks the level of every definition, assumption, and theorem of a module.
|
|
839
864
|
export const checkModuleLevels = (module: Module, context: LevelContext) => {
|
|
840
865
|
const analyzer = createLevelAnalyzer(context);
|
|
841
|
-
|
|
842
|
-
.
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
866
|
+
collectSemanticErrors([
|
|
867
|
+
...module.definitions
|
|
868
|
+
.filter((definition) => !definition.name.includes("!"))
|
|
869
|
+
.map((definition) => ({
|
|
870
|
+
location: definition.location,
|
|
871
|
+
check: () => analyzer.analyzeDefinition(definition, new Map()),
|
|
872
|
+
})),
|
|
873
|
+
...module.assumptions.map((assumption) => ({
|
|
874
|
+
location: assumption.location,
|
|
875
|
+
check: () => analyzer.analyze(assumption.expression, new Map()),
|
|
876
|
+
})),
|
|
877
|
+
...module.theorems.map((theorem) => ({
|
|
878
|
+
location: theorem.location,
|
|
879
|
+
check: () =>
|
|
880
|
+
theorem.statement.kind === "AssumeProve"
|
|
881
|
+
? analyzeAssumeProve(theorem.statement, new Map(), analyzer)
|
|
882
|
+
: analyzer.analyze(theorem.statement, new Map()),
|
|
883
|
+
})),
|
|
884
|
+
]);
|
|
850
885
|
};
|
package/src/live-check.ts
CHANGED
|
@@ -100,7 +100,9 @@ const getNodeSlot = (table: NodeTable, state: number, tableauNode: number) => {
|
|
|
100
100
|
}
|
|
101
101
|
};
|
|
102
102
|
|
|
103
|
-
|
|
103
|
+
// Without a tableau a node is a state, so a successor the search hasn't expanded yet may lie past
|
|
104
|
+
// the per-node arrays.
|
|
105
|
+
const isWritten = (check: Check, node: number) => (check.offsets[node] ?? -1) !== -1;
|
|
104
106
|
|
|
105
107
|
const getPending = (check: Check, node: number) => (check.pending[node] ??= []);
|
|
106
108
|
|
package/src/liveness.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isLocallyResolved, type Definition, type Expression } from "./ast";
|
|
2
|
-
import { STACK_OVERFLOW, TlaEvaluationError } from "./errors";
|
|
3
|
-
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
2
|
+
import { STACK_OVERFLOW, TlaEvaluationError, TlaModuleError, UNEXPECTED_EXCEPTION } from "./errors";
|
|
3
|
+
import { EXIT_STATUS, getOwnExitCode, setExitCode } from "./exit-codes";
|
|
4
4
|
import {
|
|
5
5
|
describeLocation,
|
|
6
6
|
describeRange,
|
|
@@ -61,6 +61,23 @@ const getConstantContext = (environment: Environment, scope: Scope | undefined):
|
|
|
61
61
|
next: undefined,
|
|
62
62
|
});
|
|
63
63
|
|
|
64
|
+
// TLC evaluates the constant parts of a property while it processes the spec. Its top-level handler
|
|
65
|
+
// prints a failure without a TLC error code of its own as an unexpected exception.
|
|
66
|
+
const evaluateConstant = (expression: Expression, context: Context) => {
|
|
67
|
+
try {
|
|
68
|
+
return evaluate(expression, context);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
if (!(error instanceof TlaEvaluationError) || error instanceof TlaModuleError) throw error;
|
|
71
|
+
const code = getOwnExitCode(error);
|
|
72
|
+
throw code === undefined
|
|
73
|
+
? setExitCode(
|
|
74
|
+
new TlaEvaluationError(`${UNEXPECTED_EXCEPTION}${error.message}`, error),
|
|
75
|
+
EXIT_STATUS.error,
|
|
76
|
+
)
|
|
77
|
+
: setExitCode(error, code);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
64
81
|
const createCannotHandleError = (expression: Expression) =>
|
|
65
82
|
new TlaEvaluationError(
|
|
66
83
|
`TLC cannot handle the temporal formula at ${describeLocation(expression)}`,
|
|
@@ -193,12 +210,16 @@ export const expandTemporal = (
|
|
|
193
210
|
if (expanding.has(definition)) {
|
|
194
211
|
throw setExitCode(new TlaEvaluationError(STACK_OVERFLOW), EXIT_STATUS.error);
|
|
195
212
|
}
|
|
196
|
-
|
|
213
|
+
const expanded = expand(
|
|
197
214
|
definition.body,
|
|
198
215
|
undefined,
|
|
199
216
|
expression.name,
|
|
200
217
|
new Set([...expanding, definition]),
|
|
201
218
|
);
|
|
219
|
+
// TLC takes the body of a state-level definition in a specification as one initial predicate.
|
|
220
|
+
return !isProperty && getDefinitionLevel(definition, environment) < ACTION_LEVEL
|
|
221
|
+
? [{ expression: definition.body, scope: undefined, name: expression.name }]
|
|
222
|
+
: expanded;
|
|
202
223
|
}
|
|
203
224
|
}
|
|
204
225
|
const context = getConstantContext(environment, scope);
|
|
@@ -278,7 +299,7 @@ export const toLiveExpression = (
|
|
|
278
299
|
): LiveExpression => {
|
|
279
300
|
const atomLevel = level === 0 ? getLevel(expression, scope, environment) : level;
|
|
280
301
|
if (atomLevel === 0) {
|
|
281
|
-
const value =
|
|
302
|
+
const value = evaluateConstant(expression, getConstantContext(environment, scope));
|
|
282
303
|
if (typeof value !== "boolean") {
|
|
283
304
|
throw new TlaEvaluationError(
|
|
284
305
|
`TLC expected a boolean in the temporal formula at ${describeLocation(expression)}`,
|
|
@@ -354,9 +375,45 @@ export const toLiveExpression = (
|
|
|
354
375
|
return level > ACTION_LEVEL ? result : toAtom(expression, scope, level);
|
|
355
376
|
};
|
|
356
377
|
|
|
378
|
+
// TLC translates the operands of a connective even when the whole formula is a state or action
|
|
379
|
+
// predicate, so it fails on a constant operand before it checks any state.
|
|
380
|
+
const convertOperands = (expression: Expression, scope: Scope | undefined) => {
|
|
381
|
+
switch (expression.kind) {
|
|
382
|
+
case "Label":
|
|
383
|
+
convert(expression.body, scope);
|
|
384
|
+
return;
|
|
385
|
+
case "Let":
|
|
386
|
+
convert(expression.body, bindLetDefinitions(expression, scope, environment));
|
|
387
|
+
return;
|
|
388
|
+
case "And":
|
|
389
|
+
case "Or":
|
|
390
|
+
for (const operand of expression.operands) convert(operand, scope);
|
|
391
|
+
return;
|
|
392
|
+
case "Apply":
|
|
393
|
+
if (
|
|
394
|
+
expression.operator === "=>" ||
|
|
395
|
+
(expression.operator === "~" && !isLocallyResolved(expression))
|
|
396
|
+
) {
|
|
397
|
+
for (const operand of expression.args) convert(operand, scope);
|
|
398
|
+
}
|
|
399
|
+
return;
|
|
400
|
+
case "If": {
|
|
401
|
+
const condition = convert(expression.condition, scope);
|
|
402
|
+
const branches =
|
|
403
|
+
condition.kind === "Boolean"
|
|
404
|
+
? [condition.value ? expression.consequent : expression.alternative]
|
|
405
|
+
: [expression.consequent, expression.alternative];
|
|
406
|
+
for (const branch of branches) convert(branch, scope);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
|
|
357
411
|
const convert = (expression: Expression, scope: Scope | undefined): LiveExpression => {
|
|
358
412
|
const level = getLevel(expression, scope, environment);
|
|
359
|
-
if (level <= ACTION_LEVEL)
|
|
413
|
+
if (level <= ACTION_LEVEL) {
|
|
414
|
+
convertOperands(expression, scope);
|
|
415
|
+
return toAtom(expression, scope, level);
|
|
416
|
+
}
|
|
360
417
|
switch (expression.kind) {
|
|
361
418
|
case "Label":
|
|
362
419
|
return convert(expression.body, scope);
|
|
@@ -399,7 +456,10 @@ export const toLiveExpression = (
|
|
|
399
456
|
return convertQuantifier(expression, scope);
|
|
400
457
|
case "If": {
|
|
401
458
|
if (getLevel(expression.condition, scope, environment) === 0) {
|
|
402
|
-
const condition =
|
|
459
|
+
const condition = evaluateConstant(
|
|
460
|
+
expression.condition,
|
|
461
|
+
getConstantContext(environment, scope),
|
|
462
|
+
);
|
|
403
463
|
if (typeof condition === "boolean") {
|
|
404
464
|
return convert(condition ? expression.consequent : expression.alternative, scope);
|
|
405
465
|
}
|