tla-ts 0.0.3-dev.c949d82 → 0.0.3-dev.cfd469d
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.cjs +1 -1
- package/dist/index.d.cts +27 -0
- package/dist/index.d.ts +27 -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 +14 -13
- package/dist/run-cli.js +22 -21
- package/dist/transfer.cjs +27 -18
- package/dist/transfer.js +27 -18
- package/package.json +1 -1
- package/src/ast.ts +25 -4
- package/src/builtins.ts +9 -3
- package/src/check.ts +210 -40
- package/src/environment.ts +208 -69
- package/src/errors.ts +47 -3
- package/src/evaluate.ts +131 -9
- package/src/fingerprints.ts +2 -1
- package/src/instances.ts +1 -1
- package/src/levels.ts +177 -36
- package/src/liveness.ts +5 -1
- package/src/parser.ts +140 -42
- package/src/resolve.ts +962 -391
- package/src/run-cli.ts +34 -3
- package/src/sany-lexer.ts +615 -0
- package/src/sany-parse-error.ts +3143 -0
- package/src/standard-modules.ts +52 -38
- package/src/standard-ranges.ts +116 -0
- package/src/subexpressions.ts +26 -14
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +87 -17
- package/src/value-sources.ts +29 -0
- package/src/values.ts +122 -42
package/src/evaluate.ts
CHANGED
|
@@ -12,13 +12,14 @@ import {
|
|
|
12
12
|
type RecordField,
|
|
13
13
|
} from "./ast";
|
|
14
14
|
import { getKindString } from "./builtins";
|
|
15
|
-
import { formatRange, TlaEvaluationError } from "./errors";
|
|
15
|
+
import { formatRange, TlaEvaluationError, TlaUnassignedError } from "./errors";
|
|
16
16
|
import { EXIT_STATUS, setExitCode, setOwnExitCode } from "./exit-codes";
|
|
17
17
|
import { generateEvaluator } from "./codegen";
|
|
18
18
|
import type { CheckerRuntime } from "./runtime";
|
|
19
19
|
import { getReadVariables } from "./dependencies";
|
|
20
20
|
import { contextualApplications, mapChildren, substitutedExpressions } from "./instances";
|
|
21
21
|
import { isEnabled } from "./successors";
|
|
22
|
+
import { getFingerprintTrace, valueSources, withSource } from "./value-sources";
|
|
22
23
|
import {
|
|
23
24
|
applyFunction,
|
|
24
25
|
combineHash,
|
|
@@ -34,6 +35,7 @@ import {
|
|
|
34
35
|
getDomain,
|
|
35
36
|
getFunctionDomain,
|
|
36
37
|
getIntervalElements,
|
|
38
|
+
getLazySubsets,
|
|
37
39
|
getSequenceElements,
|
|
38
40
|
getValueHash,
|
|
39
41
|
isFunctionValue,
|
|
@@ -48,6 +50,7 @@ import {
|
|
|
48
50
|
toFiniteSet,
|
|
49
51
|
updateFunction,
|
|
50
52
|
valuesEqual,
|
|
53
|
+
type FilteredSetValue,
|
|
51
54
|
type FunctionValue,
|
|
52
55
|
type LazyFunctionSource,
|
|
53
56
|
type LazyFunctionValue,
|
|
@@ -116,6 +119,7 @@ export interface Environment {
|
|
|
116
119
|
definitionValues: Map<Definition, Value>;
|
|
117
120
|
constantLevels: Map<Definition, boolean>;
|
|
118
121
|
evaluators: WeakMap<Expression, Evaluator>;
|
|
122
|
+
trace?: Expression[];
|
|
119
123
|
}
|
|
120
124
|
|
|
121
125
|
export interface Context {
|
|
@@ -339,9 +343,8 @@ export const getEnumeratedElements = (set: Value, expression: Expression) => {
|
|
|
339
343
|
: `TLC encountered the non-enumerable quantifier bound\n${text}\n${describeRange(expression)}\n${explanation}`,
|
|
340
344
|
);
|
|
341
345
|
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
: toFiniteSet(set).elements;
|
|
346
|
+
if (typeof set === "object" && set.kind === "Interval") return getIntervalElements(set);
|
|
347
|
+
return getLazySubsets(set) ?? toFiniteSet(set).elements;
|
|
345
348
|
};
|
|
346
349
|
|
|
347
350
|
export const forEachBinding = (
|
|
@@ -1019,16 +1022,80 @@ const isStateCached = (expression: Expression, environment: Environment) =>
|
|
|
1019
1022
|
getReadVariables(expression, environment) !== undefined &&
|
|
1020
1023
|
!hasFreeLocals(expression, NO_BOUND_NAMES);
|
|
1021
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
|
+
|
|
1022
1070
|
export const getEvaluator = (expression: Expression, environment: Environment): Evaluator => {
|
|
1023
1071
|
const cached = environment.evaluators.get(expression);
|
|
1024
1072
|
if (cached) return cached;
|
|
1025
1073
|
const compiled = compile(expression, environment);
|
|
1026
|
-
const
|
|
1074
|
+
const cachedEvaluator =
|
|
1027
1075
|
isWorthCaching(expression) && isClosedConstant(expression, environment)
|
|
1028
1076
|
? cacheFirstValue(compiled)
|
|
1029
1077
|
: isStateCached(expression, environment)
|
|
1030
1078
|
? cacheStateValue(compiled)
|
|
1031
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;
|
|
1032
1099
|
environment.evaluators.set(expression, evaluator);
|
|
1033
1100
|
return evaluator;
|
|
1034
1101
|
};
|
|
@@ -1036,6 +1103,7 @@ export const getEvaluator = (expression: Expression, environment: Environment):
|
|
|
1036
1103
|
const entryEvaluators = new WeakMap<Expression, Evaluator>();
|
|
1037
1104
|
|
|
1038
1105
|
export const getEntryEvaluator = (expression: Expression, environment: Environment) => {
|
|
1106
|
+
if (environment.trace) return getEvaluator(expression, environment);
|
|
1039
1107
|
const cached = entryEvaluators.get(expression);
|
|
1040
1108
|
if (cached) return cached;
|
|
1041
1109
|
const evaluator =
|
|
@@ -1144,11 +1212,32 @@ const getLazyArguments = (args: Expression[], context: Context): OperatorValue[]
|
|
|
1144
1212
|
expression: arg,
|
|
1145
1213
|
}));
|
|
1146
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
|
+
|
|
1147
1225
|
const compileBuiltinCall = (
|
|
1148
1226
|
builtin: Builtin,
|
|
1149
1227
|
args: Expression[],
|
|
1150
1228
|
environment: Environment,
|
|
1151
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
|
+
}
|
|
1152
1241
|
if (builtin.isLazy) {
|
|
1153
1242
|
return (context) => builtin.evaluate(getLazyArguments(args, context), context);
|
|
1154
1243
|
}
|
|
@@ -1308,6 +1397,13 @@ const compileApply = (
|
|
|
1308
1397
|
if (global?.kind === "Builtin" && global.name === "DOMAIN") {
|
|
1309
1398
|
const operand = getEvaluator(args[0], environment);
|
|
1310
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
|
+
}
|
|
1311
1407
|
return (context) => domain(operand(context));
|
|
1312
1408
|
}
|
|
1313
1409
|
// TLC sorts the value of a constant TLCEval argument only where no context binds a name.
|
|
@@ -1323,6 +1419,13 @@ const compileApply = (
|
|
|
1323
1419
|
const left = getEvaluator(args[0], environment);
|
|
1324
1420
|
const right = getEvaluator(args[1], environment);
|
|
1325
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
|
+
}
|
|
1326
1429
|
return (context) => subset(left(context), right(context));
|
|
1327
1430
|
}
|
|
1328
1431
|
if (global?.kind === "Builtin") return compileBuiltinCall(global, args, environment);
|
|
@@ -1528,13 +1631,21 @@ const compileSetComprehension = (
|
|
|
1528
1631
|
const pattern = expression.bound.pattern;
|
|
1529
1632
|
return (context) => {
|
|
1530
1633
|
const base = set(context);
|
|
1531
|
-
const
|
|
1634
|
+
const getTest = (evaluatePredicate: Evaluator) => (element: Value) =>
|
|
1532
1635
|
expectBoolean(
|
|
1533
|
-
|
|
1636
|
+
evaluatePredicate(withScope(context, bindPattern(context.scope, pattern, element))),
|
|
1534
1637
|
"a set filter predicate",
|
|
1535
1638
|
);
|
|
1639
|
+
const test = getTest(predicate);
|
|
1536
1640
|
if (!isReducible(base)) {
|
|
1537
|
-
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
|
+
}
|
|
1538
1649
|
throw new TlaEvaluationError(
|
|
1539
1650
|
`TLC encountered a non-enumerable quantifier bound ${formatValue(base)} at ${describeLocation(expression.bound.set)}`,
|
|
1540
1651
|
);
|
|
@@ -1588,8 +1699,9 @@ export const hasDuplicateFields = (sortedFields: RecordField[]) =>
|
|
|
1588
1699
|
|
|
1589
1700
|
export const createUnassignedError = (name: string, expression: Expression) =>
|
|
1590
1701
|
setOwnExitCode(
|
|
1591
|
-
new
|
|
1702
|
+
new TlaUnassignedError(
|
|
1592
1703
|
`In evaluation, the identifier ${name} is either undefined or not an operator.\n${describeRange(expression)}`,
|
|
1704
|
+
name,
|
|
1593
1705
|
),
|
|
1594
1706
|
EXIT_STATUS.configParseError,
|
|
1595
1707
|
);
|
|
@@ -1781,6 +1893,16 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1781
1893
|
const evaluateFunction = child(expression.function);
|
|
1782
1894
|
const args = expression.args.map(child);
|
|
1783
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
|
+
}
|
|
1784
1906
|
if (args.length === 1) {
|
|
1785
1907
|
const argument = args[0];
|
|
1786
1908
|
return (context) =>
|
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,19 @@ import {
|
|
|
5
5
|
type Expression,
|
|
6
6
|
type Module,
|
|
7
7
|
type OperatorParam,
|
|
8
|
+
type Proof,
|
|
9
|
+
type ProofStep,
|
|
8
10
|
type SourceLocation,
|
|
9
11
|
} from "./ast";
|
|
10
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
createSanyError,
|
|
14
|
+
createSemanticError,
|
|
15
|
+
formatRange,
|
|
16
|
+
TlaSyntaxError,
|
|
17
|
+
type SanyError,
|
|
18
|
+
} from "./errors";
|
|
11
19
|
import { mapChildren } from "./instances";
|
|
12
|
-
import { getBoundNames, getPatternNames } from "./resolve";
|
|
20
|
+
import { collectSemanticErrors, getBoundNames, getPatternNames } from "./resolve";
|
|
13
21
|
import { STANDARD_LEVELS } from "./standard-levels";
|
|
14
22
|
|
|
15
23
|
interface LevelData {
|
|
@@ -34,6 +42,7 @@ interface LevelContext {
|
|
|
34
42
|
constants: Set<string>;
|
|
35
43
|
variables: Set<string>;
|
|
36
44
|
getDefinition: (name: string) => Definition | undefined;
|
|
45
|
+
getQualifiedName?: (name: string) => string | undefined;
|
|
37
46
|
}
|
|
38
47
|
|
|
39
48
|
type ScopeEntry =
|
|
@@ -176,8 +185,13 @@ export const ACTION_LEVEL = 2;
|
|
|
176
185
|
|
|
177
186
|
export const TEMPORAL_LEVEL = 3;
|
|
178
187
|
|
|
179
|
-
|
|
180
|
-
|
|
188
|
+
// SANY reports a level error at the range of the expression it rejects.
|
|
189
|
+
const createLevelError = (message: string, expression: Expression) =>
|
|
190
|
+
expression.location.range
|
|
191
|
+
? createSemanticError(`${message}.`, expression.location.range)
|
|
192
|
+
: new TlaSyntaxError(
|
|
193
|
+
`${message} at line ${expression.location.line}, column ${expression.location.column}`,
|
|
194
|
+
);
|
|
181
195
|
|
|
182
196
|
const LOGICAL_OPERATORS = new Set(["/\\", "\\/", "=>", "<=>"]);
|
|
183
197
|
|
|
@@ -201,7 +215,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
201
215
|
if (context.variables.has(name)) return { kind: "Declared", name, level: 1, arity: 0 };
|
|
202
216
|
const definition = context.getDefinition(name);
|
|
203
217
|
if (definition) {
|
|
204
|
-
|
|
218
|
+
const levels = analyzeDefinition(definition, new Map());
|
|
219
|
+
// SANY names a definition of an instance by its qualified name, as in I!Foo.
|
|
220
|
+
const qualifiedName = context.getQualifiedName?.(name);
|
|
221
|
+
return {
|
|
222
|
+
kind: "Defined",
|
|
223
|
+
levels: qualifiedName ? { ...levels, name: qualifiedName } : levels,
|
|
224
|
+
};
|
|
205
225
|
}
|
|
206
226
|
const signature = CORE_SIGNATURES.get(name) ?? STANDARD_LEVELS.get(name);
|
|
207
227
|
if (signature) return { kind: "Defined", levels: signature };
|
|
@@ -237,13 +257,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
237
257
|
if (operand.operator || operand.declared) return;
|
|
238
258
|
if (operand.data.level > getMaxLevel(operator, index)) {
|
|
239
259
|
throw new TlaSyntaxError(
|
|
240
|
-
`Level error in applying operator ${operator.name}
|
|
260
|
+
`Level error in applying operator ${operator.name}:\nThe level of argument ${index + 1} exceeds the maximum level allowed by the operator`,
|
|
241
261
|
);
|
|
242
262
|
}
|
|
243
263
|
});
|
|
244
264
|
if (ranges.some((range) => range.level > ACTION_LEVEL)) {
|
|
245
265
|
throw new TlaSyntaxError(
|
|
246
|
-
`Level error in applying operator ${operator.name}
|
|
266
|
+
`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
267
|
);
|
|
248
268
|
}
|
|
249
269
|
const getOperand = (formal: string) => {
|
|
@@ -444,10 +464,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
444
464
|
? applyDeclared(resolved.name, resolved.level, operands)
|
|
445
465
|
: applyDefined(resolved.levels, operands);
|
|
446
466
|
|
|
447
|
-
const checkJunction = (operands: Operand[],
|
|
467
|
+
const checkJunction = (operands: Operand[], expression: Expression) => {
|
|
448
468
|
const levels = operands.map((operand) => operand.data.level);
|
|
449
469
|
if (levels.includes(ACTION_LEVEL) && levels.includes(TEMPORAL_LEVEL)) {
|
|
450
|
-
throw createLevelError(
|
|
470
|
+
throw createLevelError(
|
|
471
|
+
"Formula has both temporal formula and action as arguments",
|
|
472
|
+
expression,
|
|
473
|
+
);
|
|
451
474
|
}
|
|
452
475
|
};
|
|
453
476
|
|
|
@@ -456,10 +479,10 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
456
479
|
if (isLeadsTo && operands.some((operand) => operand.data.level === ACTION_LEVEL)) {
|
|
457
480
|
throw createLevelError(
|
|
458
481
|
"Action used where only temporal formula or state predicate allowed",
|
|
459
|
-
expression
|
|
482
|
+
expression,
|
|
460
483
|
);
|
|
461
484
|
}
|
|
462
|
-
if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression
|
|
485
|
+
if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression);
|
|
463
486
|
};
|
|
464
487
|
|
|
465
488
|
const checkTemporal = (
|
|
@@ -473,7 +496,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
473
496
|
expression.kind === "Always"
|
|
474
497
|
? "[] followed by action not of form [A]_v"
|
|
475
498
|
: "<> followed by action not of form <<A>>_v",
|
|
476
|
-
expression
|
|
499
|
+
expression,
|
|
477
500
|
);
|
|
478
501
|
};
|
|
479
502
|
|
|
@@ -501,7 +524,17 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
501
524
|
return data;
|
|
502
525
|
};
|
|
503
526
|
|
|
527
|
+
// SANY reports a level error of an operator's arguments at the application.
|
|
504
528
|
const analyze = (expression: Expression, scope: Scope): LevelData => {
|
|
529
|
+
try {
|
|
530
|
+
return analyzeNode(expression, scope);
|
|
531
|
+
} catch (error) {
|
|
532
|
+
if (!(error instanceof TlaSyntaxError) || error.sany) throw error;
|
|
533
|
+
throw createLevelError(error.message, expression);
|
|
534
|
+
}
|
|
535
|
+
};
|
|
536
|
+
|
|
537
|
+
const analyzeNode = (expression: Expression, scope: Scope): LevelData => {
|
|
505
538
|
switch (expression.kind) {
|
|
506
539
|
case "Number":
|
|
507
540
|
case "Decimal":
|
|
@@ -521,7 +554,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
521
554
|
case "And":
|
|
522
555
|
case "Or": {
|
|
523
556
|
const operands = toOperands(expression.operands, scope);
|
|
524
|
-
checkJunction(operands, expression
|
|
557
|
+
checkJunction(operands, expression);
|
|
525
558
|
return applyDefined(JUNCTION_LEVELS, operands);
|
|
526
559
|
}
|
|
527
560
|
case "Prime":
|
|
@@ -561,13 +594,14 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
561
594
|
expression.bounds.map((bound) => bound.set),
|
|
562
595
|
getBoundNames(expression.bounds),
|
|
563
596
|
);
|
|
564
|
-
|
|
565
|
-
data.level === TEMPORAL_LEVEL
|
|
566
|
-
|
|
567
|
-
|
|
597
|
+
const actionBound =
|
|
598
|
+
data.level === TEMPORAL_LEVEL
|
|
599
|
+
? expression.bounds.find((bound) => analyze(bound.set, scope).level === ACTION_LEVEL)
|
|
600
|
+
: undefined;
|
|
601
|
+
if (actionBound) {
|
|
568
602
|
throw createLevelError(
|
|
569
603
|
"Action-level bound of quantified temporal formula",
|
|
570
|
-
|
|
604
|
+
actionBound.set,
|
|
571
605
|
);
|
|
572
606
|
}
|
|
573
607
|
return data;
|
|
@@ -817,34 +851,141 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
817
851
|
return { analyze, analyzeDefinition, toOperand };
|
|
818
852
|
};
|
|
819
853
|
|
|
820
|
-
const analyzeAssumeProve = (
|
|
854
|
+
export const analyzeAssumeProve = (
|
|
821
855
|
assumeProve: AssumeProve,
|
|
822
856
|
scope: Scope,
|
|
823
857
|
analyzer: ReturnType<typeof createLevelAnalyzer>,
|
|
824
|
-
) => {
|
|
858
|
+
): LevelData => {
|
|
825
859
|
const inner = new Map(scope);
|
|
826
|
-
assumeProve.assumptions.
|
|
860
|
+
const parts = assumeProve.assumptions.flatMap((assumption) => {
|
|
827
861
|
if (assumption.kind === "NewSymbol") {
|
|
828
|
-
|
|
862
|
+
const set = assumption.set && analyzer.analyze(assumption.set, inner);
|
|
829
863
|
inner.set(assumption.name, { kind: "Formal", arity: assumption.arity });
|
|
830
|
-
return;
|
|
864
|
+
return set ? [set] : [];
|
|
831
865
|
}
|
|
832
|
-
|
|
833
|
-
|
|
866
|
+
return [
|
|
867
|
+
assumption.kind === "AssumeProve"
|
|
868
|
+
? analyzeAssumeProve(assumption, inner, analyzer)
|
|
869
|
+
: analyzer.analyze(assumption, inner),
|
|
870
|
+
];
|
|
834
871
|
});
|
|
835
|
-
analyzer.analyze(assumeProve.conclusion, inner);
|
|
872
|
+
parts.push(analyzer.analyze(assumeProve.conclusion, inner));
|
|
873
|
+
const data = createData(Math.max(...parts.map((part) => part.level)));
|
|
874
|
+
parts.forEach((part) => {
|
|
875
|
+
part.params.forEach((param) => data.params.add(param));
|
|
876
|
+
part.constraints.forEach((value, key) => putConstraint(data.constraints, key, value));
|
|
877
|
+
part.argConstraints.forEach((value, key) => putArgConstraint(data.argConstraints, key, value));
|
|
878
|
+
part.argParams.forEach((key) => data.argParams.add(key));
|
|
879
|
+
});
|
|
880
|
+
return data;
|
|
836
881
|
};
|
|
837
882
|
|
|
838
|
-
// SANY checks the level of every definition, assumption, and
|
|
839
|
-
export const checkModuleLevels = (
|
|
883
|
+
// SANY checks the level of every definition, assumption, theorem, and instance of a module.
|
|
884
|
+
export const checkModuleLevels = (
|
|
885
|
+
module: Module,
|
|
886
|
+
context: LevelContext,
|
|
887
|
+
instances: { location: SourceLocation; check: () => void }[],
|
|
888
|
+
warnings: SanyError[],
|
|
889
|
+
) => {
|
|
840
890
|
const analyzer = createLevelAnalyzer(context);
|
|
841
|
-
|
|
842
|
-
.
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
891
|
+
const getLevel = (expressions: Expression[]) =>
|
|
892
|
+
Math.max(0, ...expressions.map((expression) => analyzer.analyze(expression, new Map()).level));
|
|
893
|
+
const getStepLevel = (step: ProofStep) => {
|
|
894
|
+
const body = step.body;
|
|
895
|
+
if (body.kind === "Have" || body.kind === "Case") return getLevel([body.expression]);
|
|
896
|
+
if (body.kind === "Witness") return getLevel(body.expressions);
|
|
897
|
+
if (body.kind === "Take") return getLevel(body.bounds.map((bound) => bound.set));
|
|
898
|
+
if (body.kind === "Pick") {
|
|
899
|
+
return getLevel([body.body, ...body.bounds.map((bound) => bound.set)]);
|
|
900
|
+
}
|
|
901
|
+
if (body.kind !== "Assert") return 0;
|
|
902
|
+
return body.statement.kind === "AssumeProve"
|
|
903
|
+
? analyzeAssumeProve(body.statement, new Map(), analyzer).level
|
|
904
|
+
: getLevel([body.statement]);
|
|
905
|
+
};
|
|
906
|
+
// SANY's TheoremNode.LevelCheckTemporal: the proof of a temporal goal may only assume constants.
|
|
907
|
+
const checkTemporalGoal = (proof: Proof | undefined): SanyError[] =>
|
|
908
|
+
proof?.kind !== "Steps"
|
|
909
|
+
? []
|
|
910
|
+
: proof.steps.flatMap((step) => {
|
|
911
|
+
const location = formatRange(step.statement);
|
|
912
|
+
const isConstant = () => getStepLevel(step) === 0;
|
|
913
|
+
if (
|
|
914
|
+
step.body.kind === "Have" ||
|
|
915
|
+
step.body.kind === "Take" ||
|
|
916
|
+
step.body.kind === "Witness"
|
|
917
|
+
) {
|
|
918
|
+
return isConstant()
|
|
919
|
+
? []
|
|
920
|
+
: [{ location, message: "Non-constant TAKE, WITNESS, or HAVE for temporal goal." }];
|
|
921
|
+
}
|
|
922
|
+
if (step.body.kind === "Case") {
|
|
923
|
+
return [
|
|
924
|
+
...(isConstant()
|
|
925
|
+
? []
|
|
926
|
+
: [{ location, message: "Non-constant CASE for temporal goal." }]),
|
|
927
|
+
...checkTemporalGoal(step.proof),
|
|
928
|
+
];
|
|
929
|
+
}
|
|
930
|
+
return step.body.kind === "Qed" ? checkTemporalGoal(step.proof) : [];
|
|
931
|
+
});
|
|
932
|
+
// SANY checks each step as a theorem of its own after the steps of its proof.
|
|
933
|
+
const checkProof = (proof: Proof | undefined): SanyError[] =>
|
|
934
|
+
proof?.kind !== "Steps"
|
|
935
|
+
? []
|
|
936
|
+
: proof.steps.flatMap((step) => {
|
|
937
|
+
const body = step.body;
|
|
938
|
+
const isTemporal = () => getStepLevel(step) === TEMPORAL_LEVEL;
|
|
939
|
+
const pickErrors =
|
|
940
|
+
body.kind === "Pick" && body.bounds.length > 0 && isTemporal()
|
|
941
|
+
? body.bounds
|
|
942
|
+
.filter((bound) => getLevel([bound.set]) !== 0)
|
|
943
|
+
.flatMap((bound) =>
|
|
944
|
+
bound.set.location.range
|
|
945
|
+
? [
|
|
946
|
+
{
|
|
947
|
+
location: formatRange(bound.set.location.range),
|
|
948
|
+
message: "Non-constant bound of temporal PICK.",
|
|
949
|
+
},
|
|
950
|
+
]
|
|
951
|
+
: [],
|
|
952
|
+
)
|
|
953
|
+
: [];
|
|
954
|
+
return [
|
|
955
|
+
...checkProof(step.proof),
|
|
956
|
+
...pickErrors,
|
|
957
|
+
...(step.proof?.kind === "Steps" && isTemporal() ? checkTemporalGoal(step.proof) : []),
|
|
958
|
+
];
|
|
959
|
+
});
|
|
960
|
+
const errors = collectSemanticErrors(
|
|
961
|
+
[
|
|
962
|
+
...module.definitions
|
|
963
|
+
.filter((definition) => !definition.name.includes("!"))
|
|
964
|
+
.map((definition) => ({
|
|
965
|
+
location: definition.location,
|
|
966
|
+
check: () => analyzer.analyzeDefinition(definition, new Map()),
|
|
967
|
+
})),
|
|
968
|
+
...module.assumptions.map((assumption) => ({
|
|
969
|
+
location: assumption.location,
|
|
970
|
+
check: () => analyzer.analyze(assumption.expression, new Map()),
|
|
971
|
+
})),
|
|
972
|
+
...module.theorems.map((theorem) => ({
|
|
973
|
+
location: theorem.location,
|
|
974
|
+
check: () => {
|
|
975
|
+
const data =
|
|
976
|
+
theorem.statement.kind === "AssumeProve"
|
|
977
|
+
? analyzeAssumeProve(theorem.statement, new Map(), analyzer)
|
|
978
|
+
: analyzer.analyze(theorem.statement, new Map());
|
|
979
|
+
const errors = [
|
|
980
|
+
...checkProof(theorem.proof),
|
|
981
|
+
...(data.level === TEMPORAL_LEVEL ? checkTemporalGoal(theorem.proof) : []),
|
|
982
|
+
];
|
|
983
|
+
if (errors.length > 0) throw createSanyError(errors);
|
|
984
|
+
},
|
|
985
|
+
})),
|
|
986
|
+
...instances,
|
|
987
|
+
],
|
|
988
|
+
warnings,
|
|
849
989
|
);
|
|
990
|
+
if (errors.length > 0) throw createSanyError(errors, false, undefined, warnings);
|
|
850
991
|
};
|
package/src/liveness.ts
CHANGED
|
@@ -210,12 +210,16 @@ export const expandTemporal = (
|
|
|
210
210
|
if (expanding.has(definition)) {
|
|
211
211
|
throw setExitCode(new TlaEvaluationError(STACK_OVERFLOW), EXIT_STATUS.error);
|
|
212
212
|
}
|
|
213
|
-
|
|
213
|
+
const expanded = expand(
|
|
214
214
|
definition.body,
|
|
215
215
|
undefined,
|
|
216
216
|
expression.name,
|
|
217
217
|
new Set([...expanding, definition]),
|
|
218
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;
|
|
219
223
|
}
|
|
220
224
|
}
|
|
221
225
|
const context = getConstantContext(environment, scope);
|