tla-ts 0.0.3-dev.a57ca4f → 0.0.3-dev.ab42bab
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 +39 -22
- package/dist/index.d.ts +39 -22
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +1 -1
- package/dist/run-cli.cjs +5 -3
- package/dist/run-cli.js +20 -18
- package/dist/transfer.cjs +30 -21
- package/dist/transfer.js +30 -21
- package/package.json +1 -1
- package/src/ast.ts +3 -0
- package/src/check.ts +225 -36
- package/src/codegen.ts +6 -3
- package/src/config.ts +224 -170
- package/src/environment.ts +47 -18
- package/src/errors.ts +67 -1
- package/src/evaluate.ts +81 -17
- package/src/exit-codes.ts +22 -3
- package/src/instances.ts +1 -1
- package/src/levels.ts +55 -27
- package/src/live-check.ts +131 -70
- package/src/liveness.ts +68 -10
- package/src/parallel-worker.ts +2 -2
- package/src/parallel.ts +2 -2
- package/src/parser.ts +21 -10
- package/src/resolve.ts +106 -29
- package/src/run-cli.ts +36 -8
- package/src/spec-description.ts +1 -2
- package/src/successors.ts +83 -29
- package/src/symmetry.ts +16 -6
- package/src/tableau.ts +51 -24
- package/src/tlc-module.ts +6 -6
- package/src/tlc-modules.ts +4 -0
- package/src/tlc-order.ts +8 -4
- package/src/tlc-random.ts +9 -6
- package/src/tlc-summary.ts +20 -5
- package/src/values.ts +91 -14
package/src/errors.ts
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
|
+
import type { SourceRange } from "./ast";
|
|
1
2
|
import { EXIT_STATUS, getExitCode } from "./exit-codes";
|
|
3
|
+
import type { Value } from "./values";
|
|
2
4
|
|
|
3
5
|
// How TLC reports a RuntimeException it did not expect, such as one raised while computing next
|
|
4
6
|
// states.
|
|
5
7
|
export const UNEXPECTED_EXCEPTION =
|
|
6
8
|
"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: ";
|
|
7
9
|
|
|
10
|
+
// How TLC reports a Java StackOverflowError, which V8 raises as this RangeError.
|
|
11
|
+
export const STACK_OVERFLOW =
|
|
12
|
+
"This was a Java StackOverflowError. It was probably the result\nof an incorrect recursive function definition that caused TLC to enter\nan infinite loop when trying to compute the function or its application\nto an element in its putative domain.";
|
|
13
|
+
|
|
14
|
+
export const isStackOverflow = (error: unknown) =>
|
|
15
|
+
error instanceof RangeError && error.message === "Maximum call stack size exceeded";
|
|
16
|
+
|
|
8
17
|
export class TlaError extends Error {
|
|
9
18
|
constructor(message: string, cause?: unknown) {
|
|
10
19
|
super(message, { cause });
|
|
@@ -19,16 +28,44 @@ export interface SyntaxFailure {
|
|
|
19
28
|
isEnd: boolean;
|
|
20
29
|
}
|
|
21
30
|
|
|
31
|
+
// An error SANY reports at a location, such as "In module M" or a range in a module.
|
|
32
|
+
export interface SanyError {
|
|
33
|
+
location: string;
|
|
34
|
+
message: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// The errors SANY reports for a spec. A fatal error stops it before semantic analysis.
|
|
38
|
+
export interface SanyReport {
|
|
39
|
+
errors: SanyError[];
|
|
40
|
+
isFatal: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
22
43
|
export class TlaSyntaxError extends TlaError {
|
|
23
44
|
failure: SyntaxFailure | undefined;
|
|
45
|
+
sany: SanyReport | undefined;
|
|
24
46
|
|
|
25
|
-
constructor(message: string, cause?: unknown, failure?: SyntaxFailure) {
|
|
47
|
+
constructor(message: string, cause?: unknown, failure?: SyntaxFailure, sany?: SanyReport) {
|
|
26
48
|
super(message, cause);
|
|
27
49
|
this.name = "TlaSyntaxError";
|
|
28
50
|
this.failure = failure;
|
|
51
|
+
this.sany = sany;
|
|
29
52
|
}
|
|
30
53
|
}
|
|
31
54
|
|
|
55
|
+
export const createSanyError = (errors: SanyError[], isFatal = false) =>
|
|
56
|
+
new TlaSyntaxError(
|
|
57
|
+
errors.map((error) => `${error.location}\n\n${error.message}`).join("\n\n"),
|
|
58
|
+
undefined,
|
|
59
|
+
undefined,
|
|
60
|
+
{ errors, isFatal },
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
export const formatRange = (range: SourceRange) =>
|
|
64
|
+
`line ${range.start.line}, col ${range.start.column} to line ${range.end.line}, col ${range.end.column} of module ${range.module}`;
|
|
65
|
+
|
|
66
|
+
export const createSemanticError = (message: string, range: SourceRange) =>
|
|
67
|
+
createSanyError([{ location: formatRange(range), message }]);
|
|
68
|
+
|
|
32
69
|
export class TlaConfigError extends TlaError {
|
|
33
70
|
constructor(message: string, cause?: unknown) {
|
|
34
71
|
super(message, cause);
|
|
@@ -55,6 +92,35 @@ export class TlaModuleError extends TlaEvaluationError {
|
|
|
55
92
|
}
|
|
56
93
|
}
|
|
57
94
|
|
|
95
|
+
// TLC reports a successor state that the next-state relation leaves incomplete as an error of its
|
|
96
|
+
// own, not as an exception. It keeps the name TlaEvaluationError, and index.ts doesn't export it.
|
|
97
|
+
export class TlaIncompleteStateError extends TlaEvaluationError {
|
|
98
|
+
state: (Value | undefined)[];
|
|
99
|
+
action: string | undefined;
|
|
100
|
+
missing: string[];
|
|
101
|
+
|
|
102
|
+
constructor(state: (Value | undefined)[], action: string | undefined, missing: string[]) {
|
|
103
|
+
super(
|
|
104
|
+
`Successor state is not completely specified by the next-state action. The following variable${missing.length > 1 ? "s are" : " is"} not defined: ${missing.join(", ")}.\n`,
|
|
105
|
+
);
|
|
106
|
+
this.name = "TlaEvaluationError";
|
|
107
|
+
this.state = state;
|
|
108
|
+
this.action = action;
|
|
109
|
+
this.missing = missing;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// TLC reports an initial state that the initial predicate leaves incomplete as an illegal state.
|
|
114
|
+
export class TlaIllegalStateError extends TlaEvaluationError {
|
|
115
|
+
state: (Value | undefined)[];
|
|
116
|
+
|
|
117
|
+
constructor(state: (Value | undefined)[]) {
|
|
118
|
+
super("current state is not a legal state");
|
|
119
|
+
this.name = "TlaEvaluationError";
|
|
120
|
+
this.state = state;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
58
124
|
export interface TlapmReport {
|
|
59
125
|
failure?: string;
|
|
60
126
|
exception?: string;
|
package/src/evaluate.ts
CHANGED
|
@@ -10,11 +10,10 @@ 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 } 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";
|
|
@@ -30,6 +29,7 @@ import {
|
|
|
30
29
|
createSortedSet,
|
|
31
30
|
createWrittenRecord,
|
|
32
31
|
createTuple,
|
|
32
|
+
describeMissingField,
|
|
33
33
|
formatValue,
|
|
34
34
|
getDomain,
|
|
35
35
|
getFunctionDomain,
|
|
@@ -38,6 +38,7 @@ import {
|
|
|
38
38
|
getValueHash,
|
|
39
39
|
isFunctionValue,
|
|
40
40
|
isOverriddenSet,
|
|
41
|
+
isRecordValue,
|
|
41
42
|
isSameValue,
|
|
42
43
|
markLambdaFunction,
|
|
43
44
|
isReducible,
|
|
@@ -47,6 +48,8 @@ import {
|
|
|
47
48
|
toFiniteSet,
|
|
48
49
|
updateFunction,
|
|
49
50
|
valuesEqual,
|
|
51
|
+
type FilteredSetValue,
|
|
52
|
+
type FunctionValue,
|
|
50
53
|
type LazyFunctionSource,
|
|
51
54
|
type LazyFunctionValue,
|
|
52
55
|
type Value,
|
|
@@ -114,6 +117,7 @@ export interface Environment {
|
|
|
114
117
|
definitionValues: Map<Definition, Value>;
|
|
115
118
|
constantLevels: Map<Definition, boolean>;
|
|
116
119
|
evaluators: WeakMap<Expression, Evaluator>;
|
|
120
|
+
trace?: Expression[];
|
|
117
121
|
}
|
|
118
122
|
|
|
119
123
|
export interface Context {
|
|
@@ -143,9 +147,6 @@ const EXCEPT_AT = "@";
|
|
|
143
147
|
export const describeLocation = (expression: Expression) =>
|
|
144
148
|
`line ${expression.location.line}, column ${expression.location.column}`;
|
|
145
149
|
|
|
146
|
-
export const formatRange = (range: SourceRange) =>
|
|
147
|
-
`line ${range.start.line}, col ${range.start.column} to line ${range.end.line}, col ${range.end.column} of module ${range.module}`;
|
|
148
|
-
|
|
149
150
|
export const describeRange = (expression: Expression) => {
|
|
150
151
|
const range = expression.location.range;
|
|
151
152
|
return range ? formatRange(range) : describeLocation(expression);
|
|
@@ -182,8 +183,17 @@ export const getConjunctError = (
|
|
|
182
183
|
};
|
|
183
184
|
|
|
184
185
|
export const getFieldError =
|
|
185
|
-
(expression: Expression & { kind: "FieldAccess" }) => (record:
|
|
186
|
-
|
|
186
|
+
(expression: Expression & { kind: "FieldAccess" }) => (record: FunctionValue) =>
|
|
187
|
+
isRecordValue(record)
|
|
188
|
+
? `Attempted to select nonexistent field "${expression.field}" from the record\n${formatValue(record)}\n${describeRange(expression)}`
|
|
189
|
+
: describeMissingField(record, expression.field);
|
|
190
|
+
|
|
191
|
+
export const getNonFunctionError = (expression: Expression) => (value: Value) =>
|
|
192
|
+
`A non-function (${getKindString(value)}) was applied as a function.\n${describeRange(expression)}`;
|
|
193
|
+
|
|
194
|
+
export const getNonRecordError =
|
|
195
|
+
(expression: Expression & { kind: "FieldAccess" }) => (value: Value) =>
|
|
196
|
+
`Attempted to select field "${expression.field}" from a non-record value ${formatValue(value)}\n${describeRange(expression)}`;
|
|
187
197
|
|
|
188
198
|
export const getDomainOperator = (expression: Expression) => (value: Value) => {
|
|
189
199
|
if (!isFunctionValue(value)) {
|
|
@@ -1011,23 +1021,46 @@ const isStateCached = (expression: Expression, environment: Environment) =>
|
|
|
1011
1021
|
getReadVariables(expression, environment) !== undefined &&
|
|
1012
1022
|
!hasFreeLocals(expression, NO_BOUND_NAMES);
|
|
1013
1023
|
|
|
1024
|
+
// CallStackTool also evaluates the predicate of a lazy set filter it returns, even one that TLC
|
|
1025
|
+
// made before the replay.
|
|
1026
|
+
const filteredSetRetracers = new WeakMap<
|
|
1027
|
+
FilteredSetValue,
|
|
1028
|
+
(environment: Environment) => FilteredSetValue
|
|
1029
|
+
>();
|
|
1030
|
+
|
|
1014
1031
|
export const getEvaluator = (expression: Expression, environment: Environment): Evaluator => {
|
|
1015
1032
|
const cached = environment.evaluators.get(expression);
|
|
1016
1033
|
if (cached) return cached;
|
|
1017
1034
|
const compiled = compile(expression, environment);
|
|
1018
|
-
const
|
|
1035
|
+
const cachedEvaluator =
|
|
1019
1036
|
isWorthCaching(expression) && isClosedConstant(expression, environment)
|
|
1020
1037
|
? cacheFirstValue(compiled)
|
|
1021
1038
|
: isStateCached(expression, environment)
|
|
1022
1039
|
? cacheStateValue(compiled)
|
|
1023
1040
|
: compiled;
|
|
1041
|
+
const trace = environment.trace;
|
|
1042
|
+
// TLC's CallStackTool pushes each expression it evaluates and keeps the stack when one fails.
|
|
1043
|
+
const evaluator: Evaluator = trace
|
|
1044
|
+
? (context) => {
|
|
1045
|
+
const depth = trace.length;
|
|
1046
|
+
trace.push(expression);
|
|
1047
|
+
const value = cachedEvaluator(context);
|
|
1048
|
+
trace.length = depth;
|
|
1049
|
+
const retrace =
|
|
1050
|
+
typeof value === "object" && value.kind === "FilteredSet"
|
|
1051
|
+
? filteredSetRetracers.get(value)
|
|
1052
|
+
: undefined;
|
|
1053
|
+
return retrace ? retrace(environment) : value;
|
|
1054
|
+
}
|
|
1055
|
+
: cachedEvaluator;
|
|
1024
1056
|
environment.evaluators.set(expression, evaluator);
|
|
1025
1057
|
return evaluator;
|
|
1026
1058
|
};
|
|
1027
1059
|
|
|
1028
1060
|
const entryEvaluators = new WeakMap<Expression, Evaluator>();
|
|
1029
1061
|
|
|
1030
|
-
const getEntryEvaluator = (expression: Expression, environment: Environment) => {
|
|
1062
|
+
export const getEntryEvaluator = (expression: Expression, environment: Environment) => {
|
|
1063
|
+
if (environment.trace) return getEvaluator(expression, environment);
|
|
1031
1064
|
const cached = entryEvaluators.get(expression);
|
|
1032
1065
|
if (cached) return cached;
|
|
1033
1066
|
const evaluator =
|
|
@@ -1325,6 +1358,11 @@ const compileApply = (
|
|
|
1325
1358
|
if (definition?.kind === "Function") {
|
|
1326
1359
|
throw new TlaEvaluationError(`${expression.operator} is a function; apply it with brackets`);
|
|
1327
1360
|
}
|
|
1361
|
+
if (expression.operator === "\\cdot") {
|
|
1362
|
+
throw new TlaEvaluationError(
|
|
1363
|
+
"The current version of TLC does not support action composition. An incomplete implementation can be enabled via the tlc2.tool.impl.Tool.cdot=true java property.",
|
|
1364
|
+
);
|
|
1365
|
+
}
|
|
1328
1366
|
throw new TlaEvaluationError(
|
|
1329
1367
|
`Unknown operator ${expression.operator} at ${describeLocation(expression)}`,
|
|
1330
1368
|
);
|
|
@@ -1515,13 +1553,21 @@ const compileSetComprehension = (
|
|
|
1515
1553
|
const pattern = expression.bound.pattern;
|
|
1516
1554
|
return (context) => {
|
|
1517
1555
|
const base = set(context);
|
|
1518
|
-
const
|
|
1556
|
+
const getTest = (evaluatePredicate: Evaluator) => (element: Value) =>
|
|
1519
1557
|
expectBoolean(
|
|
1520
|
-
|
|
1558
|
+
evaluatePredicate(withScope(context, bindPattern(context.scope, pattern, element))),
|
|
1521
1559
|
"a set filter predicate",
|
|
1522
1560
|
);
|
|
1561
|
+
const test = getTest(predicate);
|
|
1523
1562
|
if (!isReducible(base)) {
|
|
1524
|
-
if (isSetValue(base))
|
|
1563
|
+
if (isSetValue(base)) {
|
|
1564
|
+
const filtered: FilteredSetValue = { kind: "FilteredSet", base, test };
|
|
1565
|
+
filteredSetRetracers.set(filtered, (traced) => ({
|
|
1566
|
+
...filtered,
|
|
1567
|
+
test: getTest(getEvaluator(expression.predicate, traced)),
|
|
1568
|
+
}));
|
|
1569
|
+
return filtered;
|
|
1570
|
+
}
|
|
1525
1571
|
throw new TlaEvaluationError(
|
|
1526
1572
|
`TLC encountered a non-enumerable quantifier bound ${formatValue(base)} at ${describeLocation(expression.bound.set)}`,
|
|
1527
1573
|
);
|
|
@@ -1574,7 +1620,12 @@ export const hasDuplicateFields = (sortedFields: RecordField[]) =>
|
|
|
1574
1620
|
sortedFields.some((field, index) => index > 0 && sortedFields[index - 1].name === field.name);
|
|
1575
1621
|
|
|
1576
1622
|
export const createUnassignedError = (name: string, expression: Expression) =>
|
|
1577
|
-
|
|
1623
|
+
setOwnExitCode(
|
|
1624
|
+
new TlaEvaluationError(
|
|
1625
|
+
`In evaluation, the identifier ${name} is either undefined or not an operator.\n${describeRange(expression)}`,
|
|
1626
|
+
),
|
|
1627
|
+
EXIT_STATUS.configParseError,
|
|
1628
|
+
);
|
|
1578
1629
|
|
|
1579
1630
|
export const createPrimeOutsideActionError = (expression: Expression) =>
|
|
1580
1631
|
new TlaEvaluationError(
|
|
@@ -1762,18 +1813,31 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1762
1813
|
keepLazy(expression.function);
|
|
1763
1814
|
const evaluateFunction = child(expression.function);
|
|
1764
1815
|
const args = expression.args.map(child);
|
|
1816
|
+
const describeNonFunction = getNonFunctionError(expression);
|
|
1765
1817
|
if (args.length === 1) {
|
|
1766
1818
|
const argument = args[0];
|
|
1767
|
-
return (context) =>
|
|
1819
|
+
return (context) =>
|
|
1820
|
+
applyFunction(
|
|
1821
|
+
evaluateFunction(context),
|
|
1822
|
+
argument(context),
|
|
1823
|
+
undefined,
|
|
1824
|
+
describeNonFunction,
|
|
1825
|
+
);
|
|
1768
1826
|
}
|
|
1769
1827
|
return (context) =>
|
|
1770
|
-
applyFunction(
|
|
1828
|
+
applyFunction(
|
|
1829
|
+
evaluateFunction(context),
|
|
1830
|
+
createTuple(args.map((arg) => arg(context))),
|
|
1831
|
+
undefined,
|
|
1832
|
+
describeNonFunction,
|
|
1833
|
+
);
|
|
1771
1834
|
}
|
|
1772
1835
|
case "FieldAccess": {
|
|
1773
1836
|
const record = child(expression.record);
|
|
1774
1837
|
const field = expression.field;
|
|
1775
1838
|
const describeMissing = getFieldError(expression);
|
|
1776
|
-
|
|
1839
|
+
const describeNonRecord = getNonRecordError(expression);
|
|
1840
|
+
return (context) => applyFunction(record(context), field, describeMissing, describeNonRecord);
|
|
1777
1841
|
}
|
|
1778
1842
|
case "Product": {
|
|
1779
1843
|
const factors = expression.factors.map(child);
|
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/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 {
|
|
@@ -176,8 +175,13 @@ export const ACTION_LEVEL = 2;
|
|
|
176
175
|
|
|
177
176
|
export const TEMPORAL_LEVEL = 3;
|
|
178
177
|
|
|
179
|
-
|
|
180
|
-
|
|
178
|
+
// SANY reports a level error at the range of the expression it rejects.
|
|
179
|
+
const createLevelError = (message: string, expression: Expression) =>
|
|
180
|
+
expression.location.range
|
|
181
|
+
? createSemanticError(`${message}.`, expression.location.range)
|
|
182
|
+
: new TlaSyntaxError(
|
|
183
|
+
`${message} at line ${expression.location.line}, column ${expression.location.column}`,
|
|
184
|
+
);
|
|
181
185
|
|
|
182
186
|
const LOGICAL_OPERATORS = new Set(["/\\", "\\/", "=>", "<=>"]);
|
|
183
187
|
|
|
@@ -237,13 +241,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
237
241
|
if (operand.operator || operand.declared) return;
|
|
238
242
|
if (operand.data.level > getMaxLevel(operator, index)) {
|
|
239
243
|
throw new TlaSyntaxError(
|
|
240
|
-
`Level error in applying operator ${operator.name}
|
|
244
|
+
`Level error in applying operator ${operator.name}:\nThe level of argument ${index + 1} exceeds the maximum level allowed by the operator`,
|
|
241
245
|
);
|
|
242
246
|
}
|
|
243
247
|
});
|
|
244
248
|
if (ranges.some((range) => range.level > ACTION_LEVEL)) {
|
|
245
249
|
throw new TlaSyntaxError(
|
|
246
|
-
`Level error in applying operator ${operator.name}
|
|
250
|
+
`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
251
|
);
|
|
248
252
|
}
|
|
249
253
|
const getOperand = (formal: string) => {
|
|
@@ -444,10 +448,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
444
448
|
? applyDeclared(resolved.name, resolved.level, operands)
|
|
445
449
|
: applyDefined(resolved.levels, operands);
|
|
446
450
|
|
|
447
|
-
const checkJunction = (operands: Operand[],
|
|
451
|
+
const checkJunction = (operands: Operand[], expression: Expression) => {
|
|
448
452
|
const levels = operands.map((operand) => operand.data.level);
|
|
449
453
|
if (levels.includes(ACTION_LEVEL) && levels.includes(TEMPORAL_LEVEL)) {
|
|
450
|
-
throw createLevelError(
|
|
454
|
+
throw createLevelError(
|
|
455
|
+
"Formula has both temporal formula and action as arguments",
|
|
456
|
+
expression,
|
|
457
|
+
);
|
|
451
458
|
}
|
|
452
459
|
};
|
|
453
460
|
|
|
@@ -456,10 +463,10 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
456
463
|
if (isLeadsTo && operands.some((operand) => operand.data.level === ACTION_LEVEL)) {
|
|
457
464
|
throw createLevelError(
|
|
458
465
|
"Action used where only temporal formula or state predicate allowed",
|
|
459
|
-
expression
|
|
466
|
+
expression,
|
|
460
467
|
);
|
|
461
468
|
}
|
|
462
|
-
if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression
|
|
469
|
+
if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression);
|
|
463
470
|
};
|
|
464
471
|
|
|
465
472
|
const checkTemporal = (
|
|
@@ -473,7 +480,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
473
480
|
expression.kind === "Always"
|
|
474
481
|
? "[] followed by action not of form [A]_v"
|
|
475
482
|
: "<> followed by action not of form <<A>>_v",
|
|
476
|
-
expression
|
|
483
|
+
expression,
|
|
477
484
|
);
|
|
478
485
|
};
|
|
479
486
|
|
|
@@ -501,7 +508,17 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
501
508
|
return data;
|
|
502
509
|
};
|
|
503
510
|
|
|
511
|
+
// SANY reports a level error of an operator's arguments at the application.
|
|
504
512
|
const analyze = (expression: Expression, scope: Scope): LevelData => {
|
|
513
|
+
try {
|
|
514
|
+
return analyzeNode(expression, scope);
|
|
515
|
+
} catch (error) {
|
|
516
|
+
if (!(error instanceof TlaSyntaxError) || error.sany) throw error;
|
|
517
|
+
throw createLevelError(error.message, expression);
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
|
|
521
|
+
const analyzeNode = (expression: Expression, scope: Scope): LevelData => {
|
|
505
522
|
switch (expression.kind) {
|
|
506
523
|
case "Number":
|
|
507
524
|
case "Decimal":
|
|
@@ -521,7 +538,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
521
538
|
case "And":
|
|
522
539
|
case "Or": {
|
|
523
540
|
const operands = toOperands(expression.operands, scope);
|
|
524
|
-
checkJunction(operands, expression
|
|
541
|
+
checkJunction(operands, expression);
|
|
525
542
|
return applyDefined(JUNCTION_LEVELS, operands);
|
|
526
543
|
}
|
|
527
544
|
case "Prime":
|
|
@@ -561,13 +578,14 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
561
578
|
expression.bounds.map((bound) => bound.set),
|
|
562
579
|
getBoundNames(expression.bounds),
|
|
563
580
|
);
|
|
564
|
-
|
|
565
|
-
data.level === TEMPORAL_LEVEL
|
|
566
|
-
|
|
567
|
-
|
|
581
|
+
const actionBound =
|
|
582
|
+
data.level === TEMPORAL_LEVEL
|
|
583
|
+
? expression.bounds.find((bound) => analyze(bound.set, scope).level === ACTION_LEVEL)
|
|
584
|
+
: undefined;
|
|
585
|
+
if (actionBound) {
|
|
568
586
|
throw createLevelError(
|
|
569
587
|
"Action-level bound of quantified temporal formula",
|
|
570
|
-
|
|
588
|
+
actionBound.set,
|
|
571
589
|
);
|
|
572
590
|
}
|
|
573
591
|
return data;
|
|
@@ -838,13 +856,23 @@ const analyzeAssumeProve = (
|
|
|
838
856
|
// SANY checks the level of every definition, assumption, and theorem of a module.
|
|
839
857
|
export const checkModuleLevels = (module: Module, context: LevelContext) => {
|
|
840
858
|
const analyzer = createLevelAnalyzer(context);
|
|
841
|
-
|
|
842
|
-
.
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
859
|
+
collectSemanticErrors([
|
|
860
|
+
...module.definitions
|
|
861
|
+
.filter((definition) => !definition.name.includes("!"))
|
|
862
|
+
.map((definition) => ({
|
|
863
|
+
location: definition.location,
|
|
864
|
+
check: () => analyzer.analyzeDefinition(definition, new Map()),
|
|
865
|
+
})),
|
|
866
|
+
...module.assumptions.map((assumption) => ({
|
|
867
|
+
location: assumption.location,
|
|
868
|
+
check: () => analyzer.analyze(assumption.expression, new Map()),
|
|
869
|
+
})),
|
|
870
|
+
...module.theorems.map((theorem) => ({
|
|
871
|
+
location: theorem.location,
|
|
872
|
+
check: () =>
|
|
873
|
+
theorem.statement.kind === "AssumeProve"
|
|
874
|
+
? analyzeAssumeProve(theorem.statement, new Map(), analyzer)
|
|
875
|
+
: analyzer.analyze(theorem.statement, new Map()),
|
|
876
|
+
})),
|
|
877
|
+
]);
|
|
850
878
|
};
|