tla-ts 0.0.3-dev.52c5b61 → 0.0.3-dev.596cd91
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 +12 -12
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +150 -8
- package/dist/index.d.ts +150 -8
- 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 +45 -19
- package/dist/run-cli.js +45 -19
- package/dist/transfer.cjs +1391 -28
- package/dist/transfer.js +1391 -28
- package/package.json +5 -1
- package/src/ast.ts +57 -6
- package/src/bags.ts +4 -7
- package/src/builtins.ts +29 -11
- package/src/bundled-modules.ts +3 -3
- package/src/canonical.ts +4 -3
- package/src/check.ts +1396 -227
- package/src/cli.ts +8 -1
- package/src/codegen.ts +68 -18
- package/src/config.ts +254 -181
- package/src/environment.ts +494 -129
- package/src/errors.ts +217 -3
- package/src/evaluate.ts +722 -112
- package/src/exit-codes.ts +5 -4
- package/src/index.ts +1 -0
- package/src/instances.ts +4 -2
- package/src/java-overrides.ts +104 -25
- package/src/levels.ts +183 -37
- package/src/lexer.ts +122 -29
- package/src/live-check.ts +220 -102
- package/src/liveness.ts +216 -52
- package/src/obligation-expression.ts +2 -2
- package/src/obligation-source.ts +2 -0
- package/src/parallel-worker.ts +8 -3
- package/src/parallel.ts +56 -5
- package/src/parser.ts +445 -147
- package/src/pcal-ast.ts +770 -0
- package/src/pcal-parse.ts +1523 -0
- package/src/pcal-tokenize.ts +516 -0
- package/src/pcal-translate.ts +1230 -0
- package/src/pcal-validate.ts +454 -0
- package/src/randomization.ts +24 -7
- package/src/resolve.ts +1465 -411
- package/src/run-cli.ts +1179 -155
- package/src/runtime.ts +18 -0
- package/src/sany-builtins.ts +147 -0
- package/src/sany-generator.ts +3128 -0
- package/src/sany-levels.ts +1208 -0
- package/src/sany-lexer.ts +545 -0
- package/src/sany-linter.ts +122 -0
- package/src/sany-modules.ts +1398 -0
- package/src/sany-nodes.ts +463 -0
- package/src/sany-parse-error.ts +2161 -0
- package/src/sany-report.ts +356 -0
- package/src/sany-selectors.ts +738 -0
- package/src/sany-source.ts +248 -0
- package/src/sany-xml.ts +1017 -0
- package/src/spec-description.ts +26 -7
- package/src/standard-modules.ts +200 -148
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +29 -6
- package/src/subexpressions.ts +221 -161
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +207 -103
- package/src/symmetry.ts +180 -52
- package/src/tableau.ts +54 -25
- package/src/tlc-ext.ts +13 -17
- package/src/tlc-messages.ts +329 -0
- package/src/tlc-module.ts +49 -15
- package/src/tlc-modules.ts +4 -0
- package/src/tlc-order.ts +61 -7
- package/src/tlc-random.ts +37 -9
- package/src/tlc-replay.ts +655 -0
- package/src/tlc-summary.ts +20 -5
- package/src/tlc-version.ts +18 -0
- package/src/transfer.ts +15 -3
- package/src/value-sources.ts +29 -0
- package/src/values.ts +713 -180
package/src/exit-codes.ts
CHANGED
|
@@ -41,10 +41,11 @@ export const withLivenessExitCode = (
|
|
|
41
41
|
const checker = withErrorExitCode(create);
|
|
42
42
|
return (
|
|
43
43
|
checker && {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
branches: checker.branches,
|
|
45
|
+
addInitialState: (index, state, tracing) =>
|
|
46
|
+
withErrorExitCode(() => checker.addInitialState(index, state, tracing)),
|
|
47
|
+
addState: (index, state, successors, tracing) =>
|
|
48
|
+
withErrorExitCode(() => checker.addState(index, state, successors, tracing)),
|
|
48
49
|
findViolation: () => withErrorExitCode(() => checker.findViolation()),
|
|
49
50
|
getViolatedProperties: (path, cycleStart) =>
|
|
50
51
|
withErrorExitCode(() => checker.getViolatedProperties(path, cycleStart)),
|
package/src/index.ts
CHANGED
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 {
|
|
@@ -183,6 +183,8 @@ export const mapChildren = (
|
|
|
183
183
|
function: plain(expression.function),
|
|
184
184
|
args: expression.args.map(plain),
|
|
185
185
|
};
|
|
186
|
+
case "Bracket":
|
|
187
|
+
return { ...expression, args: expression.args.map(plain) };
|
|
186
188
|
case "FieldAccess":
|
|
187
189
|
return { ...expression, record: plain(expression.record) };
|
|
188
190
|
case "Product":
|
|
@@ -705,7 +707,7 @@ const expandLetInstances = (
|
|
|
705
707
|
const index = position === -1 ? current.length : position;
|
|
706
708
|
return [...current.slice(0, index), ...instantiate(instance, inner), ...current.slice(index)];
|
|
707
709
|
}, mapped.definitions);
|
|
708
|
-
return {
|
|
710
|
+
return { ...mapped, definitions };
|
|
709
711
|
};
|
|
710
712
|
|
|
711
713
|
// A named theorem's statement is linked as its definition.
|
package/src/java-overrides.ts
CHANGED
|
@@ -12,9 +12,20 @@ interface PoolEntry {
|
|
|
12
12
|
interface JavaMethod {
|
|
13
13
|
name: string;
|
|
14
14
|
arity: number;
|
|
15
|
+
// Method#toString, which TLC prints as the signature of an override.
|
|
16
|
+
signature: string;
|
|
15
17
|
code: Uint8Array | undefined;
|
|
16
18
|
}
|
|
17
19
|
|
|
20
|
+
// A public static method of the class named after a module, which TLC reports as it loads the
|
|
21
|
+
// class, with whether it overrides an operator.
|
|
22
|
+
export interface JavaOverride {
|
|
23
|
+
module: string;
|
|
24
|
+
operator: string;
|
|
25
|
+
signature: string;
|
|
26
|
+
isMatched: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
18
29
|
interface ObjectReference {
|
|
19
30
|
className: string;
|
|
20
31
|
value: Expression | undefined;
|
|
@@ -44,12 +55,44 @@ const createReader = (bytes: Uint8Array) => {
|
|
|
44
55
|
};
|
|
45
56
|
};
|
|
46
57
|
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
58
|
+
const PRIMITIVE_TYPES = new Map([
|
|
59
|
+
["B", "byte"],
|
|
60
|
+
["C", "char"],
|
|
61
|
+
["D", "double"],
|
|
62
|
+
["F", "float"],
|
|
63
|
+
["I", "int"],
|
|
64
|
+
["J", "long"],
|
|
65
|
+
["S", "short"],
|
|
66
|
+
["Z", "boolean"],
|
|
67
|
+
["V", "void"],
|
|
68
|
+
]);
|
|
69
|
+
|
|
70
|
+
// The modifiers Method#toString prints, in the order of Modifier#toString.
|
|
71
|
+
const METHOD_MODIFIERS: [number, string][] = [
|
|
72
|
+
[0x0001, "public"],
|
|
73
|
+
[0x0004, "protected"],
|
|
74
|
+
[0x0002, "private"],
|
|
75
|
+
[0x0400, "abstract"],
|
|
76
|
+
[0x0008, "static"],
|
|
77
|
+
[0x0010, "final"],
|
|
78
|
+
[0x0020, "synchronized"],
|
|
79
|
+
[0x0100, "native"],
|
|
80
|
+
[0x0800, "strictfp"],
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
const getTypeNames = (descriptor: string) =>
|
|
84
|
+
[...descriptor.matchAll(/(\[*)(?:L([^;]*);|([BCDFIJSZV]))/g)].map(
|
|
85
|
+
(match) =>
|
|
86
|
+
`${match[2]?.replaceAll("/", ".") ?? PRIMITIVE_TYPES.get(match[3])}${"[]".repeat(match[1].length)}`,
|
|
87
|
+
);
|
|
51
88
|
|
|
52
|
-
|
|
89
|
+
const getClassName = (pool: PoolEntry[], index: number) => pool[pool[index].first].text;
|
|
90
|
+
|
|
91
|
+
const getJavaClassName = (pool: PoolEntry[], index: number) =>
|
|
92
|
+
getClassName(pool, index).replaceAll("/", ".");
|
|
93
|
+
|
|
94
|
+
// The public static methods of a class file and their bytecode, in the order of the class file.
|
|
95
|
+
// Class#getDeclaredMethods orders them by where HotSpot allocated the symbols of their names.
|
|
53
96
|
const readMethods = (bytes: Uint8Array) => {
|
|
54
97
|
const reader = createReader(bytes);
|
|
55
98
|
if (reader.readUint32() !== CLASS_MAGIC)
|
|
@@ -80,7 +123,7 @@ const readMethods = (bytes: Uint8Array) => {
|
|
|
80
123
|
}
|
|
81
124
|
}
|
|
82
125
|
reader.readUint16();
|
|
83
|
-
reader.readUint16();
|
|
126
|
+
const className = getJavaClassName(pool, reader.readUint16());
|
|
84
127
|
reader.readUint16();
|
|
85
128
|
reader.readBytes(reader.readUint16() * 2);
|
|
86
129
|
for (let fields = reader.readUint16(); fields > 0; fields--) {
|
|
@@ -95,23 +138,38 @@ const readMethods = (bytes: Uint8Array) => {
|
|
|
95
138
|
const access = reader.readUint16();
|
|
96
139
|
const name = pool[reader.readUint16()].text;
|
|
97
140
|
const descriptor = pool[reader.readUint16()].text;
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
method.code = attribute.subarray(8, 8 + length);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
141
|
+
const parametersEnd = descriptor.indexOf(")");
|
|
142
|
+
const parameters = getTypeNames(descriptor.slice(1, parametersEnd));
|
|
143
|
+
const attributes = Array.from({ length: reader.readUint16() }, () => ({
|
|
144
|
+
name: pool[reader.readUint16()].text,
|
|
145
|
+
reader: createReader(reader.readBytes(reader.readUint32())),
|
|
146
|
+
}));
|
|
107
147
|
const isOverride = (access & ACCESS_PUBLIC) !== 0 && (access & ACCESS_STATIC) !== 0;
|
|
108
|
-
if (isOverride)
|
|
148
|
+
if (!isOverride) continue;
|
|
149
|
+
const code = attributes.find((attribute) => attribute.name === "Code")?.reader;
|
|
150
|
+
// A Code attribute holds max_stack and max_locals before the length of the bytecode.
|
|
151
|
+
code?.readBytes(4);
|
|
152
|
+
const thrown = attributes.find((attribute) => attribute.name === "Exceptions")?.reader;
|
|
153
|
+
const exceptions = thrown
|
|
154
|
+
? Array.from({ length: thrown.readUint16() }, () =>
|
|
155
|
+
getJavaClassName(pool, thrown.readUint16()),
|
|
156
|
+
)
|
|
157
|
+
: [];
|
|
158
|
+
const modifiers = METHOD_MODIFIERS.filter(([flag]) => (access & flag) !== 0).map(
|
|
159
|
+
([, modifier]) => modifier,
|
|
160
|
+
);
|
|
161
|
+
const returnType = getTypeNames(descriptor.slice(parametersEnd + 1))[0];
|
|
162
|
+
const throwsClause = exceptions.length === 0 ? "" : ` throws ${exceptions.join(",")}`;
|
|
163
|
+
methods.push({
|
|
164
|
+
name,
|
|
165
|
+
arity: parameters.length,
|
|
166
|
+
code: code?.readBytes(code.readUint32()),
|
|
167
|
+
signature: `${modifiers.join(" ")} ${returnType} ${className}.${name}(${parameters.join(",")})${throwsClause}`,
|
|
168
|
+
});
|
|
109
169
|
}
|
|
110
170
|
return { pool, methods };
|
|
111
171
|
};
|
|
112
172
|
|
|
113
|
-
const getClassName = (pool: PoolEntry[], index: number) => pool[pool[index].first].text;
|
|
114
|
-
|
|
115
173
|
const getMember = (pool: PoolEntry[], index: number) => {
|
|
116
174
|
const reference = pool[index];
|
|
117
175
|
const nameAndType = pool[reference.second];
|
|
@@ -220,16 +278,29 @@ const getReturnedLiteral = (
|
|
|
220
278
|
throw unsupported();
|
|
221
279
|
};
|
|
222
280
|
|
|
223
|
-
// TLC loads a class named after each module and replaces
|
|
224
|
-
// with the public static method of the same name and arity.
|
|
225
|
-
|
|
226
|
-
|
|
281
|
+
// TLC loads a class named after each module and replaces each operator of the module that the
|
|
282
|
+
// root module sees with the public static method of the same name and arity. It reports whether
|
|
283
|
+
// each method matched an operator.
|
|
284
|
+
export const overrideDefinitions = (
|
|
285
|
+
module: Module,
|
|
286
|
+
bytes: Uint8Array | undefined,
|
|
287
|
+
isRoot: boolean,
|
|
288
|
+
): { module: Module; overrides: JavaOverride[] } => {
|
|
289
|
+
if (!bytes) return { module, overrides: [] };
|
|
227
290
|
const classFile = readMethods(bytes);
|
|
291
|
+
const arities = new Map(
|
|
292
|
+
module.definitions
|
|
293
|
+
.filter((definition) => isRoot || !definition.isLocal)
|
|
294
|
+
.map((definition) => [
|
|
295
|
+
definition.name,
|
|
296
|
+
definition.kind === "Operator" ? definition.params.length : 0,
|
|
297
|
+
]),
|
|
298
|
+
);
|
|
299
|
+
const isMatched = (method: JavaMethod) => arities.get(method.name) === method.arity;
|
|
228
300
|
const definitions = module.definitions.map((definition): Definition => {
|
|
229
301
|
if (definition.kind !== "Operator") return definition;
|
|
230
302
|
const method = classFile.methods.find(
|
|
231
|
-
(candidate) =>
|
|
232
|
-
candidate.name === definition.name && candidate.arity === definition.params.length,
|
|
303
|
+
(candidate) => candidate.name === definition.name && isMatched(candidate),
|
|
233
304
|
);
|
|
234
305
|
if (!method) return definition;
|
|
235
306
|
return {
|
|
@@ -237,5 +308,13 @@ export const overrideDefinitions = (module: Module, bytes: Uint8Array | undefine
|
|
|
237
308
|
body: getReturnedLiteral(method, classFile.pool, module.name, definition.location),
|
|
238
309
|
};
|
|
239
310
|
});
|
|
240
|
-
return {
|
|
311
|
+
return {
|
|
312
|
+
module: { ...module, definitions },
|
|
313
|
+
overrides: classFile.methods.map((method) => ({
|
|
314
|
+
module: module.name,
|
|
315
|
+
operator: method.name,
|
|
316
|
+
signature: method.signature,
|
|
317
|
+
isMatched: isMatched(method),
|
|
318
|
+
})),
|
|
319
|
+
};
|
|
241
320
|
};
|
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 =
|
|
@@ -172,12 +181,19 @@ const bindFormals = (scope: Scope, formals: OperatorParam[]) =>
|
|
|
172
181
|
|
|
173
182
|
const toFormals = (names: string[]) => names.map((name) => ({ name, arity: 0 }));
|
|
174
183
|
|
|
184
|
+
// The levels of tla2sany.semantic.LevelConstants.
|
|
185
|
+
export const CONSTANT_LEVEL = 0;
|
|
186
|
+
export const VARIABLE_LEVEL = 1;
|
|
175
187
|
export const ACTION_LEVEL = 2;
|
|
176
|
-
|
|
177
188
|
export const TEMPORAL_LEVEL = 3;
|
|
178
189
|
|
|
179
|
-
|
|
180
|
-
|
|
190
|
+
// SANY reports a level error at the range of the expression it rejects.
|
|
191
|
+
const createLevelError = (message: string, expression: Expression) =>
|
|
192
|
+
expression.location.range
|
|
193
|
+
? createSemanticError(`${message}.`, expression.location.range)
|
|
194
|
+
: new TlaSyntaxError(
|
|
195
|
+
`${message} at line ${expression.location.line}, column ${expression.location.column}`,
|
|
196
|
+
);
|
|
181
197
|
|
|
182
198
|
const LOGICAL_OPERATORS = new Set(["/\\", "\\/", "=>", "<=>"]);
|
|
183
199
|
|
|
@@ -201,7 +217,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
201
217
|
if (context.variables.has(name)) return { kind: "Declared", name, level: 1, arity: 0 };
|
|
202
218
|
const definition = context.getDefinition(name);
|
|
203
219
|
if (definition) {
|
|
204
|
-
|
|
220
|
+
const levels = analyzeDefinition(definition, new Map());
|
|
221
|
+
// SANY names a definition of an instance by its qualified name, as in I!Foo.
|
|
222
|
+
const qualifiedName = context.getQualifiedName?.(name);
|
|
223
|
+
return {
|
|
224
|
+
kind: "Defined",
|
|
225
|
+
levels: qualifiedName ? { ...levels, name: qualifiedName } : levels,
|
|
226
|
+
};
|
|
205
227
|
}
|
|
206
228
|
const signature = CORE_SIGNATURES.get(name) ?? STANDARD_LEVELS.get(name);
|
|
207
229
|
if (signature) return { kind: "Defined", levels: signature };
|
|
@@ -237,13 +259,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
237
259
|
if (operand.operator || operand.declared) return;
|
|
238
260
|
if (operand.data.level > getMaxLevel(operator, index)) {
|
|
239
261
|
throw new TlaSyntaxError(
|
|
240
|
-
`Level error in applying operator ${operator.name}
|
|
262
|
+
`Level error in applying operator ${operator.name}:\nThe level of argument ${index + 1} exceeds the maximum level allowed by the operator`,
|
|
241
263
|
);
|
|
242
264
|
}
|
|
243
265
|
});
|
|
244
266
|
if (ranges.some((range) => range.level > ACTION_LEVEL)) {
|
|
245
267
|
throw new TlaSyntaxError(
|
|
246
|
-
`Level error in applying operator ${operator.name}
|
|
268
|
+
`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
269
|
);
|
|
248
270
|
}
|
|
249
271
|
const getOperand = (formal: string) => {
|
|
@@ -444,10 +466,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
444
466
|
? applyDeclared(resolved.name, resolved.level, operands)
|
|
445
467
|
: applyDefined(resolved.levels, operands);
|
|
446
468
|
|
|
447
|
-
const checkJunction = (operands: Operand[],
|
|
469
|
+
const checkJunction = (operands: Operand[], expression: Expression) => {
|
|
448
470
|
const levels = operands.map((operand) => operand.data.level);
|
|
449
471
|
if (levels.includes(ACTION_LEVEL) && levels.includes(TEMPORAL_LEVEL)) {
|
|
450
|
-
throw createLevelError(
|
|
472
|
+
throw createLevelError(
|
|
473
|
+
"Formula has both temporal formula and action as arguments",
|
|
474
|
+
expression,
|
|
475
|
+
);
|
|
451
476
|
}
|
|
452
477
|
};
|
|
453
478
|
|
|
@@ -456,10 +481,10 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
456
481
|
if (isLeadsTo && operands.some((operand) => operand.data.level === ACTION_LEVEL)) {
|
|
457
482
|
throw createLevelError(
|
|
458
483
|
"Action used where only temporal formula or state predicate allowed",
|
|
459
|
-
expression
|
|
484
|
+
expression,
|
|
460
485
|
);
|
|
461
486
|
}
|
|
462
|
-
if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression
|
|
487
|
+
if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression);
|
|
463
488
|
};
|
|
464
489
|
|
|
465
490
|
const checkTemporal = (
|
|
@@ -473,7 +498,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
473
498
|
expression.kind === "Always"
|
|
474
499
|
? "[] followed by action not of form [A]_v"
|
|
475
500
|
: "<> followed by action not of form <<A>>_v",
|
|
476
|
-
expression
|
|
501
|
+
expression,
|
|
477
502
|
);
|
|
478
503
|
};
|
|
479
504
|
|
|
@@ -501,7 +526,17 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
501
526
|
return data;
|
|
502
527
|
};
|
|
503
528
|
|
|
529
|
+
// SANY reports a level error of an operator's arguments at the application.
|
|
504
530
|
const analyze = (expression: Expression, scope: Scope): LevelData => {
|
|
531
|
+
try {
|
|
532
|
+
return analyzeNode(expression, scope);
|
|
533
|
+
} catch (error) {
|
|
534
|
+
if (!(error instanceof TlaSyntaxError) || error.sany) throw error;
|
|
535
|
+
throw createLevelError(error.message, expression);
|
|
536
|
+
}
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
const analyzeNode = (expression: Expression, scope: Scope): LevelData => {
|
|
505
540
|
switch (expression.kind) {
|
|
506
541
|
case "Number":
|
|
507
542
|
case "Decimal":
|
|
@@ -521,7 +556,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
521
556
|
case "And":
|
|
522
557
|
case "Or": {
|
|
523
558
|
const operands = toOperands(expression.operands, scope);
|
|
524
|
-
checkJunction(operands, expression
|
|
559
|
+
checkJunction(operands, expression);
|
|
525
560
|
return applyDefined(JUNCTION_LEVELS, operands);
|
|
526
561
|
}
|
|
527
562
|
case "Prime":
|
|
@@ -561,13 +596,14 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
561
596
|
expression.bounds.map((bound) => bound.set),
|
|
562
597
|
getBoundNames(expression.bounds),
|
|
563
598
|
);
|
|
564
|
-
|
|
565
|
-
data.level === TEMPORAL_LEVEL
|
|
566
|
-
|
|
567
|
-
|
|
599
|
+
const actionBound =
|
|
600
|
+
data.level === TEMPORAL_LEVEL
|
|
601
|
+
? expression.bounds.find((bound) => analyze(bound.set, scope).level === ACTION_LEVEL)
|
|
602
|
+
: undefined;
|
|
603
|
+
if (actionBound) {
|
|
568
604
|
throw createLevelError(
|
|
569
605
|
"Action-level bound of quantified temporal formula",
|
|
570
|
-
|
|
606
|
+
actionBound.set,
|
|
571
607
|
);
|
|
572
608
|
}
|
|
573
609
|
return data;
|
|
@@ -656,6 +692,9 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
656
692
|
? toOperand(expression.args[0], scope)
|
|
657
693
|
: { data: applyBuiltin(SET_LEVELS, expression.args, scope) },
|
|
658
694
|
]);
|
|
695
|
+
// SANY's semantic checker rejects the expression without computing its level.
|
|
696
|
+
case "Bracket":
|
|
697
|
+
return createData();
|
|
659
698
|
case "FieldAccess":
|
|
660
699
|
return applyDefined(SELECT_LEVELS, [
|
|
661
700
|
toOperand(expression.record, scope),
|
|
@@ -817,34 +856,141 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
817
856
|
return { analyze, analyzeDefinition, toOperand };
|
|
818
857
|
};
|
|
819
858
|
|
|
820
|
-
const analyzeAssumeProve = (
|
|
859
|
+
export const analyzeAssumeProve = (
|
|
821
860
|
assumeProve: AssumeProve,
|
|
822
861
|
scope: Scope,
|
|
823
862
|
analyzer: ReturnType<typeof createLevelAnalyzer>,
|
|
824
|
-
) => {
|
|
863
|
+
): LevelData => {
|
|
825
864
|
const inner = new Map(scope);
|
|
826
|
-
assumeProve.assumptions.
|
|
865
|
+
const parts = assumeProve.assumptions.flatMap((assumption) => {
|
|
827
866
|
if (assumption.kind === "NewSymbol") {
|
|
828
|
-
|
|
867
|
+
const set = assumption.set && analyzer.analyze(assumption.set, inner);
|
|
829
868
|
inner.set(assumption.name, { kind: "Formal", arity: assumption.arity });
|
|
830
|
-
return;
|
|
869
|
+
return set ? [set] : [];
|
|
831
870
|
}
|
|
832
|
-
|
|
833
|
-
|
|
871
|
+
return [
|
|
872
|
+
assumption.kind === "AssumeProve"
|
|
873
|
+
? analyzeAssumeProve(assumption, inner, analyzer)
|
|
874
|
+
: analyzer.analyze(assumption, inner),
|
|
875
|
+
];
|
|
876
|
+
});
|
|
877
|
+
parts.push(analyzer.analyze(assumeProve.conclusion, inner));
|
|
878
|
+
const data = createData(Math.max(...parts.map((part) => part.level)));
|
|
879
|
+
parts.forEach((part) => {
|
|
880
|
+
part.params.forEach((param) => data.params.add(param));
|
|
881
|
+
part.constraints.forEach((value, key) => putConstraint(data.constraints, key, value));
|
|
882
|
+
part.argConstraints.forEach((value, key) => putArgConstraint(data.argConstraints, key, value));
|
|
883
|
+
part.argParams.forEach((key) => data.argParams.add(key));
|
|
834
884
|
});
|
|
835
|
-
|
|
885
|
+
return data;
|
|
836
886
|
};
|
|
837
887
|
|
|
838
|
-
// SANY checks the level of every definition, assumption, and
|
|
839
|
-
export const checkModuleLevels = (
|
|
888
|
+
// SANY checks the level of every definition, assumption, theorem, and instance of a module.
|
|
889
|
+
export const checkModuleLevels = (
|
|
890
|
+
module: Module,
|
|
891
|
+
context: LevelContext,
|
|
892
|
+
instances: { location: SourceLocation; check: () => void }[],
|
|
893
|
+
warnings: SanyError[],
|
|
894
|
+
) => {
|
|
840
895
|
const analyzer = createLevelAnalyzer(context);
|
|
841
|
-
|
|
842
|
-
.
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
896
|
+
const getLevel = (expressions: Expression[]) =>
|
|
897
|
+
Math.max(0, ...expressions.map((expression) => analyzer.analyze(expression, new Map()).level));
|
|
898
|
+
const getStepLevel = (step: ProofStep) => {
|
|
899
|
+
const body = step.body;
|
|
900
|
+
if (body.kind === "Have" || body.kind === "Case") return getLevel([body.expression]);
|
|
901
|
+
if (body.kind === "Witness") return getLevel(body.expressions);
|
|
902
|
+
if (body.kind === "Take") return getLevel(body.bounds.map((bound) => bound.set));
|
|
903
|
+
if (body.kind === "Pick") {
|
|
904
|
+
return getLevel([body.body, ...body.bounds.map((bound) => bound.set)]);
|
|
905
|
+
}
|
|
906
|
+
if (body.kind !== "Assert") return 0;
|
|
907
|
+
return body.statement.kind === "AssumeProve"
|
|
908
|
+
? analyzeAssumeProve(body.statement, new Map(), analyzer).level
|
|
909
|
+
: getLevel([body.statement]);
|
|
910
|
+
};
|
|
911
|
+
// SANY's TheoremNode.LevelCheckTemporal: the proof of a temporal goal may only assume constants.
|
|
912
|
+
const checkTemporalGoal = (proof: Proof | undefined): SanyError[] =>
|
|
913
|
+
proof?.kind !== "Steps"
|
|
914
|
+
? []
|
|
915
|
+
: proof.steps.flatMap((step) => {
|
|
916
|
+
const location = formatRange(step.statement);
|
|
917
|
+
const isConstant = () => getStepLevel(step) === 0;
|
|
918
|
+
if (
|
|
919
|
+
step.body.kind === "Have" ||
|
|
920
|
+
step.body.kind === "Take" ||
|
|
921
|
+
step.body.kind === "Witness"
|
|
922
|
+
) {
|
|
923
|
+
return isConstant()
|
|
924
|
+
? []
|
|
925
|
+
: [{ location, message: "Non-constant TAKE, WITNESS, or HAVE for temporal goal." }];
|
|
926
|
+
}
|
|
927
|
+
if (step.body.kind === "Case") {
|
|
928
|
+
return [
|
|
929
|
+
...(isConstant()
|
|
930
|
+
? []
|
|
931
|
+
: [{ location, message: "Non-constant CASE for temporal goal." }]),
|
|
932
|
+
...checkTemporalGoal(step.proof),
|
|
933
|
+
];
|
|
934
|
+
}
|
|
935
|
+
return step.body.kind === "Qed" ? checkTemporalGoal(step.proof) : [];
|
|
936
|
+
});
|
|
937
|
+
// SANY checks each step as a theorem of its own after the steps of its proof.
|
|
938
|
+
const checkProof = (proof: Proof | undefined): SanyError[] =>
|
|
939
|
+
proof?.kind !== "Steps"
|
|
940
|
+
? []
|
|
941
|
+
: proof.steps.flatMap((step) => {
|
|
942
|
+
const body = step.body;
|
|
943
|
+
const isTemporal = () => getStepLevel(step) === TEMPORAL_LEVEL;
|
|
944
|
+
const pickErrors =
|
|
945
|
+
body.kind === "Pick" && body.bounds.length > 0 && isTemporal()
|
|
946
|
+
? body.bounds
|
|
947
|
+
.filter((bound) => getLevel([bound.set]) !== 0)
|
|
948
|
+
.flatMap((bound) =>
|
|
949
|
+
bound.set.location.range
|
|
950
|
+
? [
|
|
951
|
+
{
|
|
952
|
+
location: formatRange(bound.set.location.range),
|
|
953
|
+
message: "Non-constant bound of temporal PICK.",
|
|
954
|
+
},
|
|
955
|
+
]
|
|
956
|
+
: [],
|
|
957
|
+
)
|
|
958
|
+
: [];
|
|
959
|
+
return [
|
|
960
|
+
...checkProof(step.proof),
|
|
961
|
+
...pickErrors,
|
|
962
|
+
...(step.proof?.kind === "Steps" && isTemporal() ? checkTemporalGoal(step.proof) : []),
|
|
963
|
+
];
|
|
964
|
+
});
|
|
965
|
+
const errors = collectSemanticErrors(
|
|
966
|
+
[
|
|
967
|
+
...module.definitions
|
|
968
|
+
.filter((definition) => !definition.name.includes("!"))
|
|
969
|
+
.map((definition) => ({
|
|
970
|
+
location: definition.location,
|
|
971
|
+
check: () => analyzer.analyzeDefinition(definition, new Map()),
|
|
972
|
+
})),
|
|
973
|
+
...module.assumptions.map((assumption) => ({
|
|
974
|
+
location: assumption.location,
|
|
975
|
+
check: () => analyzer.analyze(assumption.expression, new Map()),
|
|
976
|
+
})),
|
|
977
|
+
...module.theorems.map((theorem) => ({
|
|
978
|
+
location: theorem.location,
|
|
979
|
+
check: () => {
|
|
980
|
+
const data =
|
|
981
|
+
theorem.statement.kind === "AssumeProve"
|
|
982
|
+
? analyzeAssumeProve(theorem.statement, new Map(), analyzer)
|
|
983
|
+
: analyzer.analyze(theorem.statement, new Map());
|
|
984
|
+
const errors = [
|
|
985
|
+
...checkProof(theorem.proof),
|
|
986
|
+
...(data.level === TEMPORAL_LEVEL ? checkTemporalGoal(theorem.proof) : []),
|
|
987
|
+
];
|
|
988
|
+
if (errors.length > 0) throw createSanyError(errors);
|
|
989
|
+
},
|
|
990
|
+
})),
|
|
991
|
+
...instances,
|
|
992
|
+
],
|
|
993
|
+
warnings,
|
|
849
994
|
);
|
|
995
|
+
if (errors.length > 0) throw createSanyError(errors, false, undefined, warnings);
|
|
850
996
|
};
|