tla-ts 0.0.3-dev.5ea4534 → 0.0.3-dev.5fd6731
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 +59 -22
- package/dist/index.d.ts +59 -22
- 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 -12
- package/dist/run-cli.js +22 -20
- package/dist/transfer.cjs +39 -21
- package/dist/transfer.js +39 -21
- package/package.json +1 -1
- package/src/ast.ts +27 -4
- package/src/builtins.ts +9 -3
- package/src/check.ts +357 -79
- package/src/codegen.ts +6 -3
- package/src/config.ts +224 -170
- package/src/environment.ts +249 -87
- package/src/errors.ts +100 -1
- package/src/evaluate.ts +182 -26
- package/src/exit-codes.ts +22 -3
- package/src/fingerprints.ts +2 -1
- package/src/instances.ts +1 -1
- package/src/levels.ts +177 -36
- package/src/live-check.ts +132 -70
- package/src/liveness.ts +117 -22
- package/src/parallel-worker.ts +2 -2
- package/src/parallel.ts +2 -2
- package/src/parser.ts +155 -49
- package/src/resolve.ts +911 -342
- package/src/run-cli.ts +61 -11
- package/src/sany-lexer.ts +615 -0
- package/src/sany-parse-error.ts +3143 -0
- package/src/spec-description.ts +1 -2
- package/src/standard-modules.ts +54 -45
- package/src/standard-ranges.ts +116 -0
- package/src/subexpressions.ts +26 -14
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +169 -33
- 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/value-sources.ts +29 -0
- package/src/values.ts +231 -61
package/src/resolve.ts
CHANGED
|
@@ -4,33 +4,75 @@ import {
|
|
|
4
4
|
type AssumeProve,
|
|
5
5
|
type Bound,
|
|
6
6
|
type BoundPattern,
|
|
7
|
+
type Declaration,
|
|
7
8
|
type Definition,
|
|
8
9
|
type Expression,
|
|
10
|
+
type Fact,
|
|
9
11
|
type Instance,
|
|
12
|
+
type LabelExpression,
|
|
10
13
|
type Module,
|
|
11
14
|
type NewSymbol,
|
|
12
15
|
type OperatorParam,
|
|
13
16
|
type Proof,
|
|
14
17
|
type ProofStep,
|
|
18
|
+
type Selector,
|
|
15
19
|
type SourceLocation,
|
|
20
|
+
type SourceRange,
|
|
16
21
|
type Theorem,
|
|
17
22
|
} from "./ast";
|
|
18
|
-
import { TlaSyntaxError } from "./errors";
|
|
23
|
+
import { formatRange, TlaSyntaxError, type SanyError } from "./errors";
|
|
19
24
|
|
|
20
|
-
|
|
21
|
-
|
|
25
|
+
// The kinds of symbol SANY tells apart when it binds a name twice. A parameter is also a bound name,
|
|
26
|
+
// and a NEW symbol has a kind for each level.
|
|
27
|
+
export type SymbolKind =
|
|
28
|
+
| "Builtin"
|
|
29
|
+
| "Constant"
|
|
30
|
+
| "Variable"
|
|
31
|
+
| "Definition"
|
|
32
|
+
| "Instance"
|
|
33
|
+
| "Theorem"
|
|
34
|
+
| "Parameter"
|
|
35
|
+
| `New${NewSymbol["level"]}`;
|
|
36
|
+
|
|
37
|
+
// range is where SANY reports the symbol, and two symbols at one range are one node to SANY.
|
|
38
|
+
export interface Binding {
|
|
39
|
+
kind: SymbolKind;
|
|
22
40
|
arity: number;
|
|
23
|
-
|
|
41
|
+
range?: SourceRange;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface ModuleSymbol extends Binding {
|
|
45
|
+
key: string;
|
|
24
46
|
}
|
|
25
47
|
|
|
26
48
|
export type Symbols = Map<string, ModuleSymbol>;
|
|
27
49
|
|
|
28
|
-
|
|
50
|
+
// A named theorem or proof step whose parts a name such as foo!lab or <1>2!1 selects. isInProof
|
|
51
|
+
// holds while SANY analyzes the proof of the theorem or step.
|
|
52
|
+
interface Goal {
|
|
53
|
+
statement: Expression | AssumeProve;
|
|
54
|
+
isSuffices: boolean;
|
|
55
|
+
isInProof: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// isPending holds for an operator a RECURSIVE declaration names until its definition.
|
|
59
|
+
interface LocalName extends Binding {
|
|
29
60
|
name: string;
|
|
30
|
-
arity: number;
|
|
31
61
|
parent: LocalName | undefined;
|
|
62
|
+
goal?: Goal;
|
|
63
|
+
isPending?: boolean;
|
|
32
64
|
}
|
|
33
65
|
|
|
66
|
+
// A name the module binds, from position on.
|
|
67
|
+
interface ModuleBinding extends Binding {
|
|
68
|
+
position: SourceLocation;
|
|
69
|
+
isPending?: boolean;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// errors and warnings collect what SANY reports while it goes on analyzing the unit, and bindings
|
|
73
|
+
// holds the names the module binds. A label may appear where hasLabelScope holds, in a definition, a
|
|
74
|
+
// named theorem, or a numbered step, except where isLabelForbidden holds, in the scope of a NEW in a
|
|
75
|
+
// nested ASSUME. isGenerated holds in a definition tla-ts generates for a LET INSTANCE.
|
|
34
76
|
interface Unit {
|
|
35
77
|
imported: Symbols;
|
|
36
78
|
definitions: Map<string, Definition>;
|
|
@@ -38,8 +80,23 @@ interface Unit {
|
|
|
38
80
|
location: SourceLocation;
|
|
39
81
|
self: Definition | undefined;
|
|
40
82
|
facts: Set<string>;
|
|
83
|
+
goals: Map<string, Goal>;
|
|
84
|
+
bindings: Map<string, ModuleBinding>;
|
|
85
|
+
errors: SanyError[];
|
|
86
|
+
warnings: SanyError[];
|
|
87
|
+
hasLabelScope: boolean;
|
|
88
|
+
isLabelForbidden: boolean;
|
|
89
|
+
isGenerated: boolean;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
interface SelectionState {
|
|
93
|
+
node: Expression | NewSymbol | AssumeProve;
|
|
94
|
+
isGoal: boolean;
|
|
95
|
+
isSufficesOpen: boolean;
|
|
41
96
|
}
|
|
42
97
|
|
|
98
|
+
type Reference = Expression & { kind: "Identifier" | "Apply" | "Subexpression" };
|
|
99
|
+
|
|
43
100
|
export const LOCAL_RESOLUTION = { kind: "Local" } as const;
|
|
44
101
|
|
|
45
102
|
const renamedArities = new WeakMap<Expression, number>();
|
|
@@ -53,7 +110,7 @@ const isKey = (name: string) => {
|
|
|
53
110
|
};
|
|
54
111
|
|
|
55
112
|
const CORE_SYMBOLS: Symbols = new Map(
|
|
56
|
-
["~>", "-+->", "\\cdot"].map((name) => [name, { key: name, arity: 2 }]),
|
|
113
|
+
["~>", "-+->", "\\cdot"].map((name) => [name, { key: name, arity: 2, kind: "Builtin" }]),
|
|
57
114
|
);
|
|
58
115
|
|
|
59
116
|
export const isBefore = (left: SourceLocation, right: SourceLocation) =>
|
|
@@ -71,12 +128,99 @@ const lookupLocal = (scope: LocalName | undefined, name: string) => {
|
|
|
71
128
|
return undefined;
|
|
72
129
|
};
|
|
73
130
|
|
|
74
|
-
|
|
75
|
-
|
|
131
|
+
// A name the module binds before the unit and before the range, which a copy of a subexpression
|
|
132
|
+
// that a reference selects keeps from its definition.
|
|
133
|
+
const getModuleBinding = (name: string, unit: Unit, range: SourceRange | undefined) => {
|
|
134
|
+
const binding = unit.bindings.get(name);
|
|
135
|
+
return binding &&
|
|
136
|
+
isBefore(binding.position, unit.location) &&
|
|
137
|
+
(!range || isBefore(binding.position, range.start))
|
|
138
|
+
? binding
|
|
139
|
+
: undefined;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const isSameRange = (left: SourceRange | undefined, right: SourceRange | undefined) =>
|
|
143
|
+
left !== undefined && right !== undefined && formatRange(left) === formatRange(right);
|
|
144
|
+
|
|
145
|
+
// SymbolTable.addSymbol binds a name nothing binds yet. It rejects a built-in operator, a parameter
|
|
146
|
+
// or bound name already in use, and a symbol of another kind or arity than the one bound, and warns
|
|
147
|
+
// of any other duplicate. tla-ts binds a symbol SANY never sees, such as one it generates. Tells
|
|
148
|
+
// whether to bind the added symbol.
|
|
149
|
+
const isBindable = (name: string, current: Binding | undefined, added: Binding, unit: Unit) => {
|
|
150
|
+
if (current === undefined) return true;
|
|
151
|
+
if (isSameRange(current.range, added.range)) return false;
|
|
152
|
+
const range = added.range;
|
|
153
|
+
if (range === undefined || unit.isGenerated || (current.kind !== "Builtin" && !current.range)) {
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
const location = formatRange(range);
|
|
157
|
+
const existing = current.range && formatRange(current.range);
|
|
158
|
+
if (current.kind === "Builtin") {
|
|
159
|
+
unit.errors.push({
|
|
160
|
+
location,
|
|
161
|
+
message: `Symbol ${name} is a built-in operator, and cannot be redefined.`,
|
|
162
|
+
});
|
|
163
|
+
} else if (
|
|
164
|
+
added.kind === "Parameter" ||
|
|
165
|
+
current.kind !== added.kind ||
|
|
166
|
+
current.arity !== added.arity
|
|
167
|
+
) {
|
|
168
|
+
unit.errors.push({
|
|
169
|
+
location,
|
|
170
|
+
message: `Multiply-defined symbol '${name}': this definition or declaration conflicts \nwith the one at ${existing}.`,
|
|
171
|
+
});
|
|
172
|
+
} else {
|
|
173
|
+
unit.warnings.push({
|
|
174
|
+
location,
|
|
175
|
+
message: `Multiple declarations or definitions for symbol ${name}. \nThis duplicates the one at ${existing}.`,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
return false;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const bindName = (
|
|
182
|
+
scope: LocalName | undefined,
|
|
183
|
+
name: string,
|
|
184
|
+
added: Binding,
|
|
185
|
+
unit: Unit,
|
|
186
|
+
): LocalName | undefined =>
|
|
187
|
+
isBindable(
|
|
188
|
+
name,
|
|
189
|
+
lookupLocal(scope, name) ?? getModuleBinding(name, unit, added.range),
|
|
190
|
+
added,
|
|
191
|
+
unit,
|
|
192
|
+
)
|
|
193
|
+
? { ...added, name, parent: scope }
|
|
194
|
+
: scope;
|
|
195
|
+
|
|
196
|
+
// Binds the names of a module from the unit on.
|
|
197
|
+
const bindModuleName = (name: string, added: Binding, unit: Unit) => {
|
|
198
|
+
if (isBindable(name, unit.bindings.get(name), added, unit)) {
|
|
199
|
+
unit.bindings.set(name, { ...added, position: unit.location });
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const bindNames = (
|
|
204
|
+
scope: LocalName | undefined,
|
|
205
|
+
names: string[],
|
|
206
|
+
ranges: SourceRange[] | undefined,
|
|
207
|
+
unit: Unit,
|
|
208
|
+
) =>
|
|
209
|
+
names.reduce(
|
|
210
|
+
(parent, name, index) =>
|
|
211
|
+
bindName(parent, name, { kind: "Parameter", arity: 0, range: ranges?.[index] }, unit),
|
|
212
|
+
scope,
|
|
213
|
+
);
|
|
76
214
|
|
|
77
|
-
const bindParams = (scope: LocalName | undefined, params: OperatorParam[]) =>
|
|
78
|
-
params.reduce
|
|
79
|
-
(parent, param) =>
|
|
215
|
+
const bindParams = (scope: LocalName | undefined, params: OperatorParam[], unit: Unit) =>
|
|
216
|
+
params.reduce(
|
|
217
|
+
(parent, param) =>
|
|
218
|
+
bindName(
|
|
219
|
+
parent,
|
|
220
|
+
param.name,
|
|
221
|
+
{ kind: "Parameter", arity: param.arity, range: param.location?.range },
|
|
222
|
+
unit,
|
|
223
|
+
),
|
|
80
224
|
scope,
|
|
81
225
|
);
|
|
82
226
|
|
|
@@ -99,10 +243,25 @@ const getVisibleDefinition = (name: string, unit: Unit) => {
|
|
|
99
243
|
const findSymbol = (name: string, unit: Unit): ModuleSymbol | undefined => {
|
|
100
244
|
const definition = getVisibleDefinition(name, unit);
|
|
101
245
|
return definition
|
|
102
|
-
? { key: unit.getKey(definition), arity: getArity(definition) }
|
|
246
|
+
? { key: unit.getKey(definition), arity: getArity(definition), kind: "Definition" }
|
|
103
247
|
: unit.imported.get(name);
|
|
104
248
|
};
|
|
105
249
|
|
|
250
|
+
// SANY reports a name at the range of its text.
|
|
251
|
+
const getNameRange = (location: SourceLocation, name: string): SourceRange => ({
|
|
252
|
+
module: location.range?.module ?? "",
|
|
253
|
+
start: location,
|
|
254
|
+
end: { line: location.line, column: location.column + name.length - 1 },
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
// SANY reports an operator symbol applied to operands as one it cannot resolve.
|
|
258
|
+
const getFixity = (expression: Expression) => {
|
|
259
|
+
if (expression.kind !== "Apply") return undefined;
|
|
260
|
+
if (expression.args.length === 2) return "infix";
|
|
261
|
+
if (expression.args.length !== 1) return undefined;
|
|
262
|
+
return isBefore(expression.location, expression.args[0].location) ? "prefix" : "postfix";
|
|
263
|
+
};
|
|
264
|
+
|
|
106
265
|
// SANY names an undefined operator symbol such as + differently from an undefined identifier.
|
|
107
266
|
const isOperatorSymbol = (name: string) =>
|
|
108
267
|
name[0].toLowerCase() === name[0].toUpperCase() &&
|
|
@@ -112,6 +271,187 @@ const isOperatorSymbol = (name: string) =>
|
|
|
112
271
|
const isStepName = (name: string) =>
|
|
113
272
|
name.startsWith("<") && ((name[1] >= "0" && name[1] <= "9") || name[1] === "*");
|
|
114
273
|
|
|
274
|
+
export const getOperandIndex = (operand: number | "<<" | ">>", count: number) => {
|
|
275
|
+
if (typeof operand === "number") return operand >= 1 && operand <= count ? operand - 1 : -1;
|
|
276
|
+
if (operand === "<<") return count > 0 ? 0 : -1;
|
|
277
|
+
return count === 2 ? 1 : -1;
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
const getSelectors = (reference: Reference): Selector[] =>
|
|
281
|
+
reference.kind === "Subexpression"
|
|
282
|
+
? reference.selectors
|
|
283
|
+
: (reference.kind === "Identifier" ? reference.name : reference.operator)
|
|
284
|
+
.split("!")
|
|
285
|
+
.map((name) => ({ kind: "Name", name, args: [] }));
|
|
286
|
+
|
|
287
|
+
const isReference = (expression: Expression): expression is Reference =>
|
|
288
|
+
expression.kind === "Identifier" ||
|
|
289
|
+
expression.kind === "Apply" ||
|
|
290
|
+
expression.kind === "Subexpression";
|
|
291
|
+
|
|
292
|
+
const isInScopeOfDeclaration = (assumeProve: AssumeProve, clause: number) =>
|
|
293
|
+
assumeProve.assumptions.slice(0, clause).some((assumption) => assumption.kind === "NewSymbol");
|
|
294
|
+
|
|
295
|
+
const findLabel = (expression: Expression, name: string): LabelExpression | undefined => {
|
|
296
|
+
if (expression.kind === "Label") return expression.name === name ? expression : undefined;
|
|
297
|
+
if (expression.kind === "Lambda") return undefined;
|
|
298
|
+
if (expression.kind === "Let") return findLabel(expression.body, name);
|
|
299
|
+
return getChildren(expression).reduce<LabelExpression | undefined>(
|
|
300
|
+
(found, child) => found ?? findLabel(child, name),
|
|
301
|
+
undefined,
|
|
302
|
+
);
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
// SANY drops a label in the scope of a NEW in a nested ASSUME, after it reports the label.
|
|
306
|
+
const findClauseLabel = (
|
|
307
|
+
clause: Expression | NewSymbol | AssumeProve,
|
|
308
|
+
name: string,
|
|
309
|
+
isForbidden: boolean,
|
|
310
|
+
): LabelExpression | undefined => {
|
|
311
|
+
if (clause.kind === "AssumeProve") {
|
|
312
|
+
return [...clause.assumptions, clause.conclusion].reduce<LabelExpression | undefined>(
|
|
313
|
+
(found, inner, index) =>
|
|
314
|
+
found ?? findClauseLabel(inner, name, isForbidden || isInScopeOfDeclaration(clause, index)),
|
|
315
|
+
undefined,
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
if (isForbidden) return undefined;
|
|
319
|
+
if (clause.kind === "NewSymbol") return clause.set && findLabel(clause.set, name);
|
|
320
|
+
return findLabel(clause, name);
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
const getOperandName = (operand: number | "<<" | ">>") =>
|
|
324
|
+
typeof operand === "number" ? `argument selector ${operand}` : operand;
|
|
325
|
+
|
|
326
|
+
const SCOPE_MESSAGE = "within the scope of a declaration\n from outside that declaration's scope.";
|
|
327
|
+
|
|
328
|
+
// Generator.selectorToNode for a name that selects part of a theorem or step. A part in the scope of
|
|
329
|
+
// a NEW in its ASSUME may be selected only within the proof of the theorem, or only after the proof
|
|
330
|
+
// of a SUFFICES step. A fact may select an ASSUME/PROVE or a NEW clause.
|
|
331
|
+
const checkSelection = (
|
|
332
|
+
reference: Reference,
|
|
333
|
+
selectors: Selector[],
|
|
334
|
+
goal: Goal,
|
|
335
|
+
unit: Unit,
|
|
336
|
+
isFact: boolean,
|
|
337
|
+
) => {
|
|
338
|
+
const report = (index: number | undefined, message: string) => {
|
|
339
|
+
const range =
|
|
340
|
+
(index === undefined ? undefined : reference.selectorRanges?.[index]) ??
|
|
341
|
+
reference.location.range;
|
|
342
|
+
if (range) unit.errors.push({ location: formatRange(range), message });
|
|
343
|
+
};
|
|
344
|
+
const isOutOfScope = goal.isSuffices === goal.isInProof;
|
|
345
|
+
const statement = goal.statement;
|
|
346
|
+
const second = selectors[1];
|
|
347
|
+
if (second.kind === "Name") {
|
|
348
|
+
const clauses =
|
|
349
|
+
statement.kind === "AssumeProve"
|
|
350
|
+
? [...statement.assumptions, statement.conclusion]
|
|
351
|
+
: [statement];
|
|
352
|
+
const found = clauses
|
|
353
|
+
.map((clause, index) => ({ index, label: findClauseLabel(clause, second.name, false) }))
|
|
354
|
+
.find((item) => item.label !== undefined);
|
|
355
|
+
if (!found) return report(1, `Cannot find label \`${second.name}'.`);
|
|
356
|
+
if (
|
|
357
|
+
statement.kind === "AssumeProve" &&
|
|
358
|
+
isInScopeOfDeclaration(statement, found.index) &&
|
|
359
|
+
isOutOfScope
|
|
360
|
+
) {
|
|
361
|
+
return report(
|
|
362
|
+
1,
|
|
363
|
+
`Accessing subexpression labeled \`${second.name}' of ASSUME/PROVE clause ${SCOPE_MESSAGE}`,
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
selectors.slice(2).reduce((label, selector, offset) => {
|
|
367
|
+
if (!label || selector.kind !== "Name") return undefined;
|
|
368
|
+
const inner = findLabel(label.body, selector.name);
|
|
369
|
+
if (!inner) report(offset + 2, `Cannot find label \`${selector.name}'.`);
|
|
370
|
+
return inner;
|
|
371
|
+
}, found.label);
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
const state: SelectionState = {
|
|
375
|
+
node: statement,
|
|
376
|
+
isGoal: true,
|
|
377
|
+
isSufficesOpen: goal.isSuffices,
|
|
378
|
+
};
|
|
379
|
+
for (const [offset, selector] of selectors.slice(1).entries()) {
|
|
380
|
+
const index = offset + 1;
|
|
381
|
+
const node = state.node;
|
|
382
|
+
if (selector.kind !== "Operand") return;
|
|
383
|
+
if (state.isSufficesOpen) {
|
|
384
|
+
if (getOperandIndex(selector.operand, 1) !== 0) {
|
|
385
|
+
return report(
|
|
386
|
+
index,
|
|
387
|
+
node.kind === "AssumeProve"
|
|
388
|
+
? "Accessing non-existent subexpression of a SUFFICES"
|
|
389
|
+
: `Nonexistent operand specified by \`${getOperandName(selector.operand)}'.`,
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
state.isSufficesOpen = false;
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
if (node.kind !== "AssumeProve") return;
|
|
396
|
+
const clauses = [...node.assumptions, node.conclusion];
|
|
397
|
+
const position = getOperandIndex(selector.operand, clauses.length);
|
|
398
|
+
if (position === -1) {
|
|
399
|
+
return report(
|
|
400
|
+
index,
|
|
401
|
+
`Nonexistent operand specified by \`${getOperandName(selector.operand)}'.`,
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
if (isInScopeOfDeclaration(node, position) && (!state.isGoal || isOutOfScope)) {
|
|
405
|
+
return report(index, `Accessing ASSUME/PROVE clause ${SCOPE_MESSAGE}`);
|
|
406
|
+
}
|
|
407
|
+
const clause = clauses[position];
|
|
408
|
+
if (clause.kind === "NewSymbol" && index !== selectors.length - 1) {
|
|
409
|
+
return report(index, "Selected a subexpression of a NEW clause of an ASSUME.");
|
|
410
|
+
}
|
|
411
|
+
state.node = clause;
|
|
412
|
+
state.isGoal = false;
|
|
413
|
+
}
|
|
414
|
+
if (isFact) return;
|
|
415
|
+
if (state.node.kind === "AssumeProve") {
|
|
416
|
+
report(undefined, "Selected ASSUME/PROVE instead of expression.");
|
|
417
|
+
}
|
|
418
|
+
if (state.node.kind === "NewSymbol") {
|
|
419
|
+
report(undefined, "Selected a NEW declaration as an expression or operator.");
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
// Generator.generateLabel reports a label where it may not appear, and skips the label's body.
|
|
424
|
+
const isLabelReported = (label: LabelExpression, unit: Unit) => {
|
|
425
|
+
const message = !unit.hasLabelScope
|
|
426
|
+
? "Label not in definition or proof step."
|
|
427
|
+
: unit.isLabelForbidden
|
|
428
|
+
? "Label not allowed within scope of declaration in nested ASSUME/PROVE."
|
|
429
|
+
: undefined;
|
|
430
|
+
if (message === undefined || !label.location.range) return false;
|
|
431
|
+
unit.errors.push({ location: formatRange(label.location.range), message });
|
|
432
|
+
return true;
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
// Checks a name that selects part of a theorem or step, and tells whether it is one.
|
|
436
|
+
const checkReference = (
|
|
437
|
+
expression: Expression,
|
|
438
|
+
scope: LocalName | undefined,
|
|
439
|
+
unit: Unit,
|
|
440
|
+
isFact: boolean,
|
|
441
|
+
) => {
|
|
442
|
+
if (!isReference(expression)) return false;
|
|
443
|
+
const selectors = getSelectors(expression);
|
|
444
|
+
const first = selectors[0];
|
|
445
|
+
const goal =
|
|
446
|
+
selectors.length > 1 && first.kind === "Name"
|
|
447
|
+
? (lookupLocal(scope, first.name)?.goal ?? unit.goals.get(first.name))
|
|
448
|
+
: undefined;
|
|
449
|
+
if (goal) checkSelection(expression, selectors, goal, unit, isFact);
|
|
450
|
+
return goal !== undefined;
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
// Generator.selectorToNode reports an unknown operator and goes on without analyzing its arguments,
|
|
454
|
+
// so its arity is undefined. It does the same for an operator given the wrong number of arguments.
|
|
115
455
|
const getSymbolArity = (
|
|
116
456
|
expression: Expression & { kind: "Identifier" | "Apply" | "OperatorReference" },
|
|
117
457
|
name: string,
|
|
@@ -122,7 +462,9 @@ const getSymbolArity = (
|
|
|
122
462
|
const renamed = renamedArities.get(expression);
|
|
123
463
|
if (renamed !== undefined) return renamed;
|
|
124
464
|
if (isKey(name)) return expression.kind === "Apply" ? expression.args.length : 0;
|
|
125
|
-
if (isStepName(name))
|
|
465
|
+
if (checkReference(expression, scope, unit, false) || isStepName(name)) {
|
|
466
|
+
return expression.kind === "Apply" ? expression.args.length : 0;
|
|
467
|
+
}
|
|
126
468
|
const local = lookupLocal(scope, name);
|
|
127
469
|
if (local && expression.kind !== "OperatorReference") expression.resolution = LOCAL_RESOLUTION;
|
|
128
470
|
if (local) return local.arity;
|
|
@@ -135,7 +477,7 @@ const getSymbolArity = (
|
|
|
135
477
|
}
|
|
136
478
|
// A name such as Inv!P1 selects the label P1 of a definition or theorem, which takes its own
|
|
137
479
|
// arguments.
|
|
138
|
-
const prefix = name.slice(0, name.indexOf("!"));
|
|
480
|
+
const prefix = name.includes("!") ? name.slice(0, name.indexOf("!")) : "";
|
|
139
481
|
const isSelected =
|
|
140
482
|
prefix !== "" &&
|
|
141
483
|
(lookupLocal(scope, prefix) !== undefined ||
|
|
@@ -144,18 +486,30 @@ const getSymbolArity = (
|
|
|
144
486
|
if (isSelected) {
|
|
145
487
|
return expression.kind === "Apply" ? expression.args.length : 0;
|
|
146
488
|
}
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
489
|
+
const range = formatRange(getNameRange(location, name === "-." ? "-" : name));
|
|
490
|
+
if (!isOperatorSymbol(name)) {
|
|
491
|
+
unit.errors.push({ location: range, message: `Unknown operator: \`${name}'.` });
|
|
492
|
+
return undefined;
|
|
493
|
+
}
|
|
494
|
+
unit.errors.push({
|
|
495
|
+
location: range,
|
|
496
|
+
message: `Could not find declaration or definition of symbol '${name}'.`,
|
|
497
|
+
});
|
|
498
|
+
const fixity = getFixity(expression);
|
|
499
|
+
if (fixity && location.range) {
|
|
500
|
+
unit.errors.push({
|
|
501
|
+
location: formatRange(location.range),
|
|
502
|
+
message: `Couldn't resolve ${fixity} operator symbol \`${name}'.`,
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
return undefined;
|
|
153
506
|
};
|
|
154
507
|
|
|
155
508
|
const resolveBounds = (bounds: Bound[], scope: LocalName | undefined, unit: Unit) => {
|
|
156
509
|
bounds.forEach((bound) => resolveExpression(bound.set, scope, unit));
|
|
157
510
|
return bounds.reduce(
|
|
158
|
-
(current, bound) =>
|
|
511
|
+
(current, bound) =>
|
|
512
|
+
bindNames(current, getPatternNames(bound.pattern), bound.pattern.ranges, unit),
|
|
159
513
|
scope,
|
|
160
514
|
);
|
|
161
515
|
};
|
|
@@ -171,14 +525,60 @@ const markRecursiveConstructions = (expression: Expression, name: string): void
|
|
|
171
525
|
getChildren(expression).forEach((child) => markRecursiveConstructions(child, name));
|
|
172
526
|
};
|
|
173
527
|
|
|
174
|
-
|
|
175
|
-
|
|
528
|
+
// Generator.processOperator binds the parameters, reports a name already bound unless a RECURSIVE
|
|
529
|
+
// declaration of this level awaits the definition, analyzes the body, and then binds the name.
|
|
530
|
+
// processFunction binds the bound names before it looks up the name, and binds the name only where
|
|
531
|
+
// nothing else does. recursive holds the declarations of this level. Tells whether to bind the name.
|
|
532
|
+
const resolveDefinition = (
|
|
533
|
+
definition: Definition,
|
|
534
|
+
scope: LocalName | undefined,
|
|
535
|
+
outer: Unit,
|
|
536
|
+
recursive: Set<Binding>,
|
|
537
|
+
) => {
|
|
538
|
+
const unit = outer.hasLabelScope ? outer : { ...outer, hasLabelScope: true };
|
|
539
|
+
const name = definition.name;
|
|
540
|
+
const range = definition.location.range;
|
|
541
|
+
const report = (message: string) => {
|
|
542
|
+
if (range && !unit.isGenerated) unit.errors.push({ location: formatRange(range), message });
|
|
543
|
+
};
|
|
544
|
+
const inner =
|
|
545
|
+
definition.kind === "Function"
|
|
546
|
+
? resolveBounds(definition.bounds, scope, unit)
|
|
547
|
+
: bindParams(scope, definition.params, unit);
|
|
548
|
+
const current = lookupLocal(inner, name) ?? getModuleBinding(name, unit, range);
|
|
549
|
+
const isDeclared = current?.isPending === true && recursive.has(current);
|
|
550
|
+
if (current?.isPending && !isDeclared) {
|
|
551
|
+
report(
|
|
552
|
+
`Recursive ${definition.kind === "Function" ? "function" : "operator"} ${name} defined at wrong LET/IN level.`,
|
|
553
|
+
);
|
|
554
|
+
} else if (current && !isDeclared) {
|
|
555
|
+
report(
|
|
556
|
+
definition.kind === "Function"
|
|
557
|
+
? `Function name \`${name}' already defined or declared.`
|
|
558
|
+
: `Operator ${name} already defined or declared.`,
|
|
559
|
+
);
|
|
560
|
+
}
|
|
176
561
|
if (definition.kind === "Function") {
|
|
177
|
-
resolveExpression(
|
|
178
|
-
|
|
179
|
-
|
|
562
|
+
resolveExpression(
|
|
563
|
+
definition.body,
|
|
564
|
+
{ name, kind: "Parameter", arity: 0, range, parent: inner },
|
|
565
|
+
unit,
|
|
566
|
+
);
|
|
567
|
+
markRecursiveConstructions(definition.body, name);
|
|
568
|
+
} else resolveExpression(definition.body, inner, unit);
|
|
569
|
+
if (current && isDeclared) {
|
|
570
|
+
current.isPending = false;
|
|
571
|
+
current.range = range;
|
|
572
|
+
return false;
|
|
180
573
|
}
|
|
181
|
-
|
|
574
|
+
return definition.kind === "Function"
|
|
575
|
+
? current === undefined
|
|
576
|
+
: isBindable(
|
|
577
|
+
name,
|
|
578
|
+
lookupLocal(scope, name) ?? getModuleBinding(name, unit, range),
|
|
579
|
+
{ kind: "Definition", arity: getArity(definition), range },
|
|
580
|
+
unit,
|
|
581
|
+
);
|
|
182
582
|
};
|
|
183
583
|
|
|
184
584
|
const resolveDefinitions = (
|
|
@@ -186,16 +586,40 @@ const resolveDefinitions = (
|
|
|
186
586
|
scope: LocalName | undefined,
|
|
187
587
|
unit: Unit,
|
|
188
588
|
) => {
|
|
189
|
-
const
|
|
589
|
+
const recursive = new Set<Binding>();
|
|
590
|
+
const declared = definitions
|
|
190
591
|
.filter((definition) => definition.isRecursive)
|
|
191
|
-
.reduce<LocalName | undefined>(
|
|
192
|
-
(parent, definition
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
592
|
+
.reduce<LocalName | undefined>((parent, definition) => {
|
|
593
|
+
if (lookupLocal(parent, definition.name)?.isPending) return parent;
|
|
594
|
+
const local: LocalName = {
|
|
595
|
+
name: definition.name,
|
|
596
|
+
arity: getArity(definition),
|
|
597
|
+
kind: "Definition",
|
|
598
|
+
parent,
|
|
599
|
+
isPending: true,
|
|
600
|
+
};
|
|
601
|
+
recursive.add(local);
|
|
602
|
+
return local;
|
|
603
|
+
}, scope);
|
|
604
|
+
return definitions.reduce<LocalName | undefined>((current, definition) => {
|
|
605
|
+
const local: LocalName = {
|
|
606
|
+
name: definition.name,
|
|
607
|
+
arity: getArity(definition),
|
|
608
|
+
kind: "Definition",
|
|
609
|
+
range: definition.location.range,
|
|
610
|
+
parent: current,
|
|
611
|
+
};
|
|
612
|
+
if (isInstantiated(definition)) return local;
|
|
613
|
+
const isGenerated = unit.isGenerated || definition.isLocal || isKey(definition.name);
|
|
614
|
+
return resolveDefinition(
|
|
615
|
+
definition,
|
|
616
|
+
current,
|
|
617
|
+
isGenerated ? { ...unit, isGenerated } : unit,
|
|
618
|
+
recursive,
|
|
619
|
+
)
|
|
620
|
+
? local
|
|
621
|
+
: current;
|
|
622
|
+
}, declared);
|
|
199
623
|
};
|
|
200
624
|
|
|
201
625
|
// OpDefNode#match: a parameter of arity 0 takes an expression, and a parameter of arity n takes an
|
|
@@ -235,21 +659,37 @@ const resolveApply = (
|
|
|
235
659
|
scope: LocalName | undefined,
|
|
236
660
|
unit: Unit,
|
|
237
661
|
) => {
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
662
|
+
const operator = expression.operator;
|
|
663
|
+
const callee = lookupLocal(scope, operator) ? undefined : getVisibleDefinition(operator, unit);
|
|
664
|
+
const arity = getSymbolArity(expression, operator, scope, unit);
|
|
665
|
+
if (arity === undefined) return;
|
|
242
666
|
if (arity !== expression.args.length) {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
667
|
+
unit.errors.push({
|
|
668
|
+
location: formatRange(
|
|
669
|
+
expression.argumentsRange ?? getNameRange(expression.location, operator),
|
|
670
|
+
),
|
|
671
|
+
message: `The operator ${operator} requires ${arity} arguments.`,
|
|
672
|
+
});
|
|
673
|
+
return;
|
|
246
674
|
}
|
|
675
|
+
// Generator.generateExprOrOpArg analyzes a name in a position that takes an expression as an
|
|
676
|
+
// expression.
|
|
247
677
|
expression.args.forEach((arg, index) => {
|
|
678
|
+
if (
|
|
679
|
+
arg.kind === "Identifier" &&
|
|
680
|
+
callee?.kind === "Operator" &&
|
|
681
|
+
callee.params[index].arity === 0
|
|
682
|
+
) {
|
|
683
|
+
resolveExpression(arg, scope, unit);
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
248
686
|
const name = getArgumentName(arg);
|
|
249
687
|
const argDefinition =
|
|
250
688
|
name === undefined || lookupLocal(scope, name) ? undefined : getVisibleDefinition(name, unit);
|
|
251
689
|
const argArity = resolveArgument(arg, scope, unit);
|
|
252
|
-
if (callee
|
|
690
|
+
if (callee && argArity !== undefined) {
|
|
691
|
+
checkArgument(callee, index, arg, argArity, argDefinition);
|
|
692
|
+
}
|
|
253
693
|
});
|
|
254
694
|
};
|
|
255
695
|
|
|
@@ -275,11 +715,13 @@ const resolveExpression = (
|
|
|
275
715
|
case "ExceptAt":
|
|
276
716
|
return;
|
|
277
717
|
case "Identifier": {
|
|
278
|
-
const
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
718
|
+
const name = expression.name;
|
|
719
|
+
const arity = getSymbolArity(expression, name, scope, unit);
|
|
720
|
+
if (arity !== undefined && arity > 0) {
|
|
721
|
+
unit.errors.push({
|
|
722
|
+
location: formatRange(getNameRange(expression.location, name)),
|
|
723
|
+
message: `The operator ${name} requires ${arity} arguments.`,
|
|
724
|
+
});
|
|
283
725
|
}
|
|
284
726
|
return;
|
|
285
727
|
}
|
|
@@ -290,7 +732,11 @@ const resolveExpression = (
|
|
|
290
732
|
resolveApply(expression, scope, unit);
|
|
291
733
|
return;
|
|
292
734
|
case "Lambda":
|
|
293
|
-
resolveExpression(
|
|
735
|
+
resolveExpression(
|
|
736
|
+
expression.body,
|
|
737
|
+
bindNames(scope, expression.params, expression.paramRanges, unit),
|
|
738
|
+
unit,
|
|
739
|
+
);
|
|
294
740
|
return;
|
|
295
741
|
case "And":
|
|
296
742
|
case "Or":
|
|
@@ -323,13 +769,17 @@ const resolveExpression = (
|
|
|
323
769
|
resolveExpression(expression.body, resolveBounds(expression.bounds, scope, unit), unit);
|
|
324
770
|
return;
|
|
325
771
|
case "UnboundedQuantifier":
|
|
326
|
-
resolveExpression(
|
|
772
|
+
resolveExpression(
|
|
773
|
+
expression.body,
|
|
774
|
+
bindNames(scope, expression.names, expression.nameRanges, unit),
|
|
775
|
+
unit,
|
|
776
|
+
);
|
|
327
777
|
return;
|
|
328
778
|
case "Choose":
|
|
329
779
|
if (expression.set) resolveExpression(expression.set, scope, unit);
|
|
330
780
|
resolveExpression(
|
|
331
781
|
expression.body,
|
|
332
|
-
bindNames(scope, getPatternNames(expression.pattern)),
|
|
782
|
+
bindNames(scope, getPatternNames(expression.pattern), expression.pattern.ranges, unit),
|
|
333
783
|
unit,
|
|
334
784
|
);
|
|
335
785
|
return;
|
|
@@ -348,12 +798,15 @@ const resolveExpression = (
|
|
|
348
798
|
return;
|
|
349
799
|
case "Record":
|
|
350
800
|
case "RecordSet":
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
801
|
+
expression.fields.forEach((field, index) => {
|
|
802
|
+
if (expression.fields.slice(0, index).some((earlier) => earlier.name === field.name)) {
|
|
803
|
+
unit.errors.push({
|
|
804
|
+
location: formatRange(field.location.range ?? getNameRange(field.location, field.name)),
|
|
805
|
+
message: "Non-unique fields in constructor.",
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
resolveExpression(field.value, scope, unit);
|
|
809
|
+
});
|
|
357
810
|
return;
|
|
358
811
|
case "Except":
|
|
359
812
|
resolveAll([
|
|
@@ -382,34 +835,72 @@ const resolveExpression = (
|
|
|
382
835
|
resolveAll([expression.subscript, expression.action]);
|
|
383
836
|
return;
|
|
384
837
|
case "Label":
|
|
385
|
-
resolveExpression(expression.body, scope, unit);
|
|
838
|
+
if (!isLabelReported(expression, unit)) resolveExpression(expression.body, scope, unit);
|
|
386
839
|
return;
|
|
387
|
-
case "Subexpression":
|
|
840
|
+
case "Subexpression": {
|
|
388
841
|
expression.selectors
|
|
389
842
|
.flatMap((selector) => ("args" in selector ? selector.args : []))
|
|
390
843
|
.forEach((arg) => resolveArgument(arg, scope, unit));
|
|
844
|
+
checkReference(expression, scope, unit, false);
|
|
845
|
+
}
|
|
391
846
|
}
|
|
392
847
|
};
|
|
393
848
|
|
|
849
|
+
// SANY analyzes the facts of a proof apart from their names, which may name theorems and modules.
|
|
850
|
+
const checkFacts = (facts: Fact[], scope: LocalName | undefined, unit: Unit) => {
|
|
851
|
+
const check = (expression: Expression, isFact: boolean, inner: Unit): void => {
|
|
852
|
+
if (expression.kind === "Label" && isLabelReported(expression, inner)) return;
|
|
853
|
+
checkReference(expression, scope, inner, isFact);
|
|
854
|
+
getChildren(expression).forEach((child) =>
|
|
855
|
+
check(
|
|
856
|
+
child,
|
|
857
|
+
false,
|
|
858
|
+
expression.kind === "Let" && child !== expression.body
|
|
859
|
+
? { ...inner, hasLabelScope: true }
|
|
860
|
+
: inner,
|
|
861
|
+
),
|
|
862
|
+
);
|
|
863
|
+
};
|
|
864
|
+
facts.forEach((fact) => {
|
|
865
|
+
if (fact.kind !== "ModuleFact") check(fact, true, { ...unit, hasLabelScope: false });
|
|
866
|
+
});
|
|
867
|
+
};
|
|
868
|
+
|
|
394
869
|
const resolveNewSymbol = (symbol: NewSymbol, scope: LocalName | undefined, unit: Unit) => {
|
|
395
870
|
if (symbol.set) resolveExpression(symbol.set, scope, unit);
|
|
396
|
-
return
|
|
871
|
+
return bindName(
|
|
872
|
+
scope,
|
|
873
|
+
symbol.name,
|
|
874
|
+
{ kind: `New${symbol.level}`, arity: symbol.arity, range: symbol.nameRange },
|
|
875
|
+
unit,
|
|
876
|
+
);
|
|
397
877
|
};
|
|
398
878
|
|
|
399
|
-
// The NEW symbols of an ASSUME stay in scope for its conclusion and the proof that follows.
|
|
879
|
+
// The NEW symbols of an ASSUME stay in scope for its conclusion and the proof that follows. SANY
|
|
880
|
+
// forbids a label in the scope of a NEW in a nested ASSUME.
|
|
400
881
|
const resolveAssumeProve = (
|
|
401
882
|
assumeProve: AssumeProve,
|
|
402
883
|
scope: LocalName | undefined,
|
|
403
884
|
unit: Unit,
|
|
885
|
+
isNested = false,
|
|
404
886
|
): LocalName | undefined => {
|
|
405
|
-
const assumed = assumeProve.assumptions.reduce(
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
887
|
+
const assumed = assumeProve.assumptions.reduce(
|
|
888
|
+
(current, assumption) => {
|
|
889
|
+
if (assumption.kind === "NewSymbol") {
|
|
890
|
+
return {
|
|
891
|
+
scope: resolveNewSymbol(assumption, current.scope, current.unit),
|
|
892
|
+
unit: isNested ? { ...current.unit, isLabelForbidden: true } : current.unit,
|
|
893
|
+
};
|
|
894
|
+
}
|
|
895
|
+
if (assumption.kind === "AssumeProve") {
|
|
896
|
+
resolveAssumeProve(assumption, current.scope, current.unit, true);
|
|
897
|
+
} else resolveExpression(assumption, current.scope, current.unit);
|
|
898
|
+
return current;
|
|
899
|
+
},
|
|
900
|
+
{ scope, unit },
|
|
901
|
+
);
|
|
902
|
+
resolveExpression(assumeProve.conclusion, assumed.scope, assumed.unit);
|
|
903
|
+
return assumed.scope;
|
|
413
904
|
};
|
|
414
905
|
|
|
415
906
|
const resolveStatement = (
|
|
@@ -424,7 +915,9 @@ const resolveStatement = (
|
|
|
424
915
|
|
|
425
916
|
// SANY resolves the expressions of a proof, while its facts and DEF clauses may also name
|
|
426
917
|
// theorems, assumptions, and modules.
|
|
427
|
-
const resolveProof = (proof: Proof | undefined, scope: LocalName | undefined,
|
|
918
|
+
const resolveProof = (proof: Proof | undefined, scope: LocalName | undefined, outer: Unit) => {
|
|
919
|
+
const unit = outer.hasLabelScope ? { ...outer, hasLabelScope: false } : outer;
|
|
920
|
+
if (proof?.kind === "By") checkFacts(proof.facts, scope, unit);
|
|
428
921
|
if (proof?.kind !== "Steps") return;
|
|
429
922
|
proof.steps.reduce((current, step) => resolveStep(step, current, unit), scope);
|
|
430
923
|
};
|
|
@@ -435,12 +928,27 @@ const resolveStep = (
|
|
|
435
928
|
unit: Unit,
|
|
436
929
|
): LocalName | undefined => {
|
|
437
930
|
const body = step.body;
|
|
931
|
+
const stepUnit = step.name === undefined ? unit : { ...unit, hasLabelScope: true };
|
|
438
932
|
switch (body.kind) {
|
|
933
|
+
// The NEW symbols of a SUFFICES ASSUME are in scope after its proof, and those of any other
|
|
934
|
+
// ASSUME in its proof.
|
|
439
935
|
case "Assert": {
|
|
440
|
-
const assumed = resolveStatement(body.statement, scope,
|
|
441
|
-
|
|
442
|
-
|
|
936
|
+
const assumed = resolveStatement(body.statement, scope, stepUnit);
|
|
937
|
+
const proofScope = body.isSuffices ? scope : assumed;
|
|
938
|
+
const name = step.name;
|
|
939
|
+
if (name === undefined) {
|
|
940
|
+
resolveProof(step.proof, proofScope, unit);
|
|
941
|
+
return body.isSuffices ? assumed : scope;
|
|
942
|
+
}
|
|
943
|
+
const goal = { statement: body.statement, isSuffices: body.isSuffices, isInProof: true };
|
|
944
|
+
resolveProof(step.proof, { name, arity: 0, kind: "Theorem", parent: proofScope, goal }, unit);
|
|
945
|
+
goal.isInProof = false;
|
|
946
|
+
return { name, arity: 0, kind: "Theorem", parent: body.isSuffices ? assumed : scope, goal };
|
|
443
947
|
}
|
|
948
|
+
case "Use":
|
|
949
|
+
case "Hide":
|
|
950
|
+
checkFacts(body.facts, scope, unit);
|
|
951
|
+
return scope;
|
|
444
952
|
case "Define":
|
|
445
953
|
return resolveDefinitions(
|
|
446
954
|
body.definitions.flatMap((definition) =>
|
|
@@ -451,17 +959,27 @@ const resolveStep = (
|
|
|
451
959
|
);
|
|
452
960
|
case "Have":
|
|
453
961
|
case "Case":
|
|
454
|
-
resolveExpression(body.expression, scope,
|
|
962
|
+
resolveExpression(body.expression, scope, stepUnit);
|
|
455
963
|
resolveProof(step.proof, scope, unit);
|
|
456
964
|
return scope;
|
|
457
965
|
case "Witness":
|
|
458
|
-
body.expressions.forEach((expression) => resolveExpression(expression, scope,
|
|
966
|
+
body.expressions.forEach((expression) => resolveExpression(expression, scope, stepUnit));
|
|
459
967
|
return scope;
|
|
460
968
|
case "Take":
|
|
461
|
-
return bindNames(
|
|
969
|
+
return bindNames(
|
|
970
|
+
resolveBounds(body.bounds, scope, stepUnit),
|
|
971
|
+
body.names,
|
|
972
|
+
body.nameRanges,
|
|
973
|
+
stepUnit,
|
|
974
|
+
);
|
|
462
975
|
case "Pick": {
|
|
463
|
-
const picked = bindNames(
|
|
464
|
-
|
|
976
|
+
const picked = bindNames(
|
|
977
|
+
resolveBounds(body.bounds, scope, stepUnit),
|
|
978
|
+
body.names,
|
|
979
|
+
body.nameRanges,
|
|
980
|
+
stepUnit,
|
|
981
|
+
);
|
|
982
|
+
resolveExpression(body.body, picked, stepUnit);
|
|
465
983
|
resolveProof(step.proof, scope, unit);
|
|
466
984
|
return picked;
|
|
467
985
|
}
|
|
@@ -471,324 +989,375 @@ const resolveStep = (
|
|
|
471
989
|
}
|
|
472
990
|
};
|
|
473
991
|
|
|
992
|
+
// SANY binds the name of a theorem after its statement and before its proof.
|
|
474
993
|
const resolveTheorem = (theorem: Theorem, unit: Unit) => {
|
|
475
994
|
const isDefinition = theorem.name !== undefined && theorem.statement.kind !== "AssumeProve";
|
|
476
995
|
const scope = isDefinition ? undefined : resolveStatement(theorem.statement, undefined, unit);
|
|
996
|
+
if (theorem.name !== undefined) {
|
|
997
|
+
bindModuleName(
|
|
998
|
+
theorem.name,
|
|
999
|
+
{ kind: "Theorem", arity: 0, range: theorem.location.range },
|
|
1000
|
+
unit,
|
|
1001
|
+
);
|
|
1002
|
+
}
|
|
1003
|
+
const goal = unit.goals.get(theorem.name ?? "");
|
|
1004
|
+
if (goal) goal.isInProof = true;
|
|
477
1005
|
resolveProof(theorem.proof, scope, unit);
|
|
1006
|
+
if (goal) goal.isInProof = false;
|
|
478
1007
|
};
|
|
479
1008
|
|
|
480
|
-
|
|
1009
|
+
// What SANY reports at a location as it links a module, such as a conflict between the modules it
|
|
1010
|
+
// extends.
|
|
1011
|
+
export interface ModuleReport {
|
|
1012
|
+
location: SourceLocation;
|
|
1013
|
+
errors: SanyError[];
|
|
1014
|
+
warnings: SanyError[];
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
// bound holds the names in scope where the module starts: the built-in operators, the names of the
|
|
1018
|
+
// module that encloses it, and those of the modules it extends. instanced holds the names each
|
|
1019
|
+
// INSTANCE adds, and warnings the warnings SANY reports for the spec.
|
|
1020
|
+
export interface ModuleUnit {
|
|
481
1021
|
imported: Symbols;
|
|
482
|
-
|
|
1022
|
+
bound: Symbols;
|
|
483
1023
|
definitions: Map<string, Definition>;
|
|
484
1024
|
getKey: (definition: Definition) => string;
|
|
1025
|
+
instanced: Map<Instance, Symbols>;
|
|
1026
|
+
reports: ModuleReport[];
|
|
1027
|
+
warnings: SanyError[];
|
|
485
1028
|
}
|
|
486
1029
|
|
|
487
|
-
|
|
488
|
-
name: string;
|
|
489
|
-
kind: "Definition" | "Constant" | "Variable";
|
|
490
|
-
location: SourceLocation;
|
|
491
|
-
isRecursive: boolean;
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
type IsDefined = (name: string) => boolean;
|
|
495
|
-
|
|
496
|
-
const createRedefinitionError = (name: string, location: SourceLocation) =>
|
|
497
|
-
new TlaSyntaxError(
|
|
498
|
-
`Multiply-defined symbol '${name}' at line ${location.line}, column ${location.column}`,
|
|
499
|
-
);
|
|
500
|
-
|
|
501
|
-
const bindUnique = (
|
|
502
|
-
scope: LocalName | undefined,
|
|
503
|
-
names: string[],
|
|
504
|
-
isDefined: IsDefined,
|
|
505
|
-
location: SourceLocation,
|
|
506
|
-
) =>
|
|
507
|
-
names.reduce<LocalName | undefined>((parent, name) => {
|
|
508
|
-
if (lookupLocal(parent, name) || isDefined(name)) throw createRedefinitionError(name, location);
|
|
509
|
-
return { name, arity: 0, parent };
|
|
510
|
-
}, scope);
|
|
511
|
-
|
|
512
|
-
const checkBoundNames = (
|
|
513
|
-
bounds: Bound[],
|
|
514
|
-
scope: LocalName | undefined,
|
|
515
|
-
isDefined: IsDefined,
|
|
516
|
-
location: SourceLocation,
|
|
517
|
-
) => {
|
|
518
|
-
bounds.forEach((bound) => checkNames(bound.set, scope, isDefined));
|
|
519
|
-
return bounds.reduce(
|
|
520
|
-
(current, bound) => bindUnique(current, getPatternNames(bound.pattern), isDefined, location),
|
|
521
|
-
scope,
|
|
522
|
-
);
|
|
523
|
-
};
|
|
1030
|
+
export const MODULE_START: SourceLocation = { line: 0, column: 0 };
|
|
524
1031
|
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
const bound = checkBoundNames(definition.bounds, self, isDefined, definition.location);
|
|
533
|
-
checkNames(definition.body, bound, isDefined);
|
|
534
|
-
return;
|
|
535
|
-
}
|
|
536
|
-
const params = bindUnique(
|
|
537
|
-
scope,
|
|
538
|
-
definition.params.map((param) => param.name),
|
|
539
|
-
isDefined,
|
|
540
|
-
definition.location,
|
|
1032
|
+
// A named theorem or assumption is linked as a definition too.
|
|
1033
|
+
export const isTheoremDefinition = (module: Module, definition: Definition) =>
|
|
1034
|
+
[...module.theorems, ...module.assumptions].some(
|
|
1035
|
+
(item) =>
|
|
1036
|
+
item.name === definition.name &&
|
|
1037
|
+
item.location.line === definition.location.line &&
|
|
1038
|
+
item.location.column === definition.location.column,
|
|
541
1039
|
);
|
|
542
|
-
checkNames(definition.body, params, isDefined);
|
|
543
|
-
};
|
|
544
|
-
|
|
545
|
-
const checkNames = (
|
|
546
|
-
expression: Expression,
|
|
547
|
-
scope: LocalName | undefined,
|
|
548
|
-
isDefined: IsDefined,
|
|
549
|
-
): void => {
|
|
550
|
-
const location = expression.location;
|
|
551
|
-
switch (expression.kind) {
|
|
552
|
-
case "Lambda":
|
|
553
|
-
checkNames(
|
|
554
|
-
expression.body,
|
|
555
|
-
bindUnique(scope, expression.params, isDefined, location),
|
|
556
|
-
isDefined,
|
|
557
|
-
);
|
|
558
|
-
return;
|
|
559
|
-
case "Let": {
|
|
560
|
-
const recursive = expression.definitions
|
|
561
|
-
.filter((definition) => definition.isRecursive)
|
|
562
|
-
.map((definition) => definition.name);
|
|
563
|
-
const inner = expression.definitions.reduce((current, definition) => {
|
|
564
|
-
if (lookupLocal(current, definition.name) || isDefined(definition.name)) {
|
|
565
|
-
throw createRedefinitionError(definition.name, definition.location);
|
|
566
|
-
}
|
|
567
|
-
checkDefinitionNames(definition, bindNames(current, recursive), isDefined);
|
|
568
|
-
return bindNames(current, [definition.name]);
|
|
569
|
-
}, scope);
|
|
570
|
-
checkNames(
|
|
571
|
-
expression.body,
|
|
572
|
-
bindUnique(
|
|
573
|
-
inner,
|
|
574
|
-
(expression.instances ?? []).flatMap((instance) =>
|
|
575
|
-
instance.name === undefined ? [] : [instance.name],
|
|
576
|
-
),
|
|
577
|
-
isDefined,
|
|
578
|
-
location,
|
|
579
|
-
),
|
|
580
|
-
isDefined,
|
|
581
|
-
);
|
|
582
|
-
return;
|
|
583
|
-
}
|
|
584
|
-
case "Forall":
|
|
585
|
-
case "Exists":
|
|
586
|
-
case "FunctionConstruction":
|
|
587
|
-
checkNames(
|
|
588
|
-
expression.body,
|
|
589
|
-
checkBoundNames(expression.bounds, scope, isDefined, location),
|
|
590
|
-
isDefined,
|
|
591
|
-
);
|
|
592
|
-
return;
|
|
593
|
-
case "SetFilter":
|
|
594
|
-
checkNames(
|
|
595
|
-
expression.predicate,
|
|
596
|
-
checkBoundNames([expression.bound], scope, isDefined, location),
|
|
597
|
-
isDefined,
|
|
598
|
-
);
|
|
599
|
-
return;
|
|
600
|
-
case "SetMap":
|
|
601
|
-
checkNames(
|
|
602
|
-
expression.expression,
|
|
603
|
-
checkBoundNames(expression.bounds, scope, isDefined, location),
|
|
604
|
-
isDefined,
|
|
605
|
-
);
|
|
606
|
-
return;
|
|
607
|
-
case "UnboundedQuantifier":
|
|
608
|
-
checkNames(
|
|
609
|
-
expression.body,
|
|
610
|
-
bindUnique(scope, expression.names, isDefined, location),
|
|
611
|
-
isDefined,
|
|
612
|
-
);
|
|
613
|
-
return;
|
|
614
|
-
case "Choose":
|
|
615
|
-
if (expression.set) checkNames(expression.set, scope, isDefined);
|
|
616
|
-
checkNames(
|
|
617
|
-
expression.body,
|
|
618
|
-
bindUnique(scope, getPatternNames(expression.pattern), isDefined, location),
|
|
619
|
-
isDefined,
|
|
620
|
-
);
|
|
621
|
-
return;
|
|
622
|
-
default:
|
|
623
|
-
getChildren(expression).forEach((child) => checkNames(child, scope, isDefined));
|
|
624
|
-
}
|
|
625
|
-
};
|
|
626
|
-
|
|
627
|
-
const getModuleNames = (module: Module) => [
|
|
628
|
-
...module.definitions.map((definition): ModuleName => ({
|
|
629
|
-
name: definition.name,
|
|
630
|
-
kind: "Definition",
|
|
631
|
-
location: definition.location,
|
|
632
|
-
isRecursive: definition.isRecursive,
|
|
633
|
-
})),
|
|
634
|
-
...module.instances.flatMap((instance): ModuleName[] =>
|
|
635
|
-
instance.name === undefined
|
|
636
|
-
? []
|
|
637
|
-
: [
|
|
638
|
-
{
|
|
639
|
-
name: instance.name,
|
|
640
|
-
kind: "Definition",
|
|
641
|
-
location: instance.location,
|
|
642
|
-
isRecursive: false,
|
|
643
|
-
},
|
|
644
|
-
],
|
|
645
|
-
),
|
|
646
|
-
...module.constants.map((constant): ModuleName => ({
|
|
647
|
-
name: constant.name,
|
|
648
|
-
kind: "Constant",
|
|
649
|
-
location: constant.location,
|
|
650
|
-
isRecursive: false,
|
|
651
|
-
})),
|
|
652
|
-
...module.variables.map((variable): ModuleName => ({
|
|
653
|
-
name: variable.name,
|
|
654
|
-
kind: "Variable",
|
|
655
|
-
location: variable.location,
|
|
656
|
-
isRecursive: false,
|
|
657
|
-
})),
|
|
658
|
-
];
|
|
659
|
-
|
|
660
|
-
export const checkRedefinitions = (module: Module, unit: ModuleUnit) => {
|
|
661
|
-
const byName = new Map<string, ModuleName[]>();
|
|
662
|
-
getModuleNames(module)
|
|
663
|
-
.sort((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
|
|
664
|
-
.forEach((entry) => byName.set(entry.name, [...(byName.get(entry.name) ?? []), entry]));
|
|
665
|
-
const isImportedBefore = (name: string, location: SourceLocation) => {
|
|
666
|
-
if (!unit.imported.has(name) && !CORE_SYMBOLS.has(name)) return false;
|
|
667
|
-
const importedAt = unit.importedAt.get(name);
|
|
668
|
-
return importedAt === undefined || isBefore(importedAt, location);
|
|
669
|
-
};
|
|
670
|
-
byName.forEach((entries) =>
|
|
671
|
-
entries.forEach((entry, index) => {
|
|
672
|
-
const isDuplicate = entries
|
|
673
|
-
.slice(0, index)
|
|
674
|
-
.some(
|
|
675
|
-
(earlier) =>
|
|
676
|
-
earlier.kind === "Definition" ||
|
|
677
|
-
entry.kind === "Definition" ||
|
|
678
|
-
earlier.kind !== entry.kind,
|
|
679
|
-
);
|
|
680
|
-
// A module may declare again a constant or variable that it extends or that encloses it.
|
|
681
|
-
const isRedeclared = unit.imported.get(entry.name)?.declaration === entry.kind;
|
|
682
|
-
if (isDuplicate || (!isRedeclared && isImportedBefore(entry.name, entry.location)))
|
|
683
|
-
throw createRedefinitionError(entry.name, entry.location);
|
|
684
|
-
}),
|
|
685
|
-
);
|
|
686
|
-
const getIsDefined =
|
|
687
|
-
(location: SourceLocation): IsDefined =>
|
|
688
|
-
(name) =>
|
|
689
|
-
(byName.get(name) ?? []).some(
|
|
690
|
-
(entry) => entry.isRecursive || isBefore(entry.location, location),
|
|
691
|
-
) || isImportedBefore(name, location);
|
|
692
|
-
module.definitions.forEach((definition) =>
|
|
693
|
-
checkDefinitionNames(definition, undefined, getIsDefined(definition.location)),
|
|
694
|
-
);
|
|
695
|
-
module.assumptions.forEach((assumption) =>
|
|
696
|
-
checkNames(assumption.expression, undefined, getIsDefined(assumption.location)),
|
|
697
|
-
);
|
|
698
|
-
module.instances.forEach((instance) => {
|
|
699
|
-
const isDefined = getIsDefined(instance.location);
|
|
700
|
-
const params = bindUnique(
|
|
701
|
-
undefined,
|
|
702
|
-
instance.params.map((param) => param.name),
|
|
703
|
-
isDefined,
|
|
704
|
-
instance.location,
|
|
705
|
-
);
|
|
706
|
-
instance.substitutions.forEach((substitution) =>
|
|
707
|
-
checkNames(substitution.expression, params, isDefined),
|
|
708
|
-
);
|
|
709
|
-
});
|
|
710
|
-
};
|
|
711
1040
|
|
|
712
1041
|
const createSymbols = (module: Module, imported: Symbols) => {
|
|
713
1042
|
const symbols = new Map([...CORE_SYMBOLS, ...imported]);
|
|
714
1043
|
module.constants.forEach((constant) =>
|
|
715
|
-
symbols.set(constant.name, { key: constant.name, arity: constant.arity }),
|
|
1044
|
+
symbols.set(constant.name, { key: constant.name, arity: constant.arity, kind: "Constant" }),
|
|
716
1045
|
);
|
|
717
1046
|
module.variables.forEach((variable) =>
|
|
718
|
-
symbols.set(variable.name, { key: variable.name, arity: 0 }),
|
|
1047
|
+
symbols.set(variable.name, { key: variable.name, arity: 0, kind: "Variable" }),
|
|
719
1048
|
);
|
|
720
1049
|
return symbols;
|
|
721
1050
|
};
|
|
722
1051
|
|
|
1052
|
+
// Generator.processSubst analyzes the explicit substitutions in the order they appear, then the
|
|
1053
|
+
// implicit ones, and then reports each declaration left without one. tla-ts reports what SANY goes on
|
|
1054
|
+
// after with the INSTANCE.
|
|
723
1055
|
export const resolveSubstitutions = (
|
|
724
1056
|
instance: Instance,
|
|
725
|
-
declarations:
|
|
1057
|
+
declarations: Declaration[],
|
|
726
1058
|
module: Module,
|
|
727
1059
|
unit: ModuleUnit,
|
|
728
1060
|
) => {
|
|
729
|
-
const scope =
|
|
1061
|
+
const scope = instance.params.reduce<LocalName | undefined>(
|
|
1062
|
+
(parent, param) => ({ name: param.name, arity: param.arity, kind: "Parameter", parent }),
|
|
1063
|
+
undefined,
|
|
1064
|
+
);
|
|
1065
|
+
const imported = createSymbols(module, unit.imported);
|
|
1066
|
+
const report: ModuleReport = { location: instance.location, errors: [], warnings: [] };
|
|
1067
|
+
unit.reports.push(report);
|
|
730
1068
|
const context: Unit = {
|
|
731
|
-
imported
|
|
1069
|
+
imported,
|
|
732
1070
|
definitions: unit.definitions,
|
|
733
1071
|
getKey: unit.getKey,
|
|
734
1072
|
location: instance.location,
|
|
735
1073
|
self: undefined,
|
|
736
1074
|
facts: new Set(),
|
|
1075
|
+
goals: new Map(),
|
|
1076
|
+
bindings: new Map(
|
|
1077
|
+
[...imported].map(([name, symbol]) => [name, { ...symbol, position: MODULE_START }]),
|
|
1078
|
+
),
|
|
1079
|
+
errors: report.errors,
|
|
1080
|
+
warnings: report.warnings,
|
|
1081
|
+
hasLabelScope: true,
|
|
1082
|
+
isLabelForbidden: false,
|
|
1083
|
+
isGenerated: false,
|
|
737
1084
|
};
|
|
738
|
-
const
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
1085
|
+
const resolveSubstitute = (declaration: Declaration, expression: Expression) => {
|
|
1086
|
+
if (declaration.arity === 0 || expression.kind !== "Identifier") {
|
|
1087
|
+
resolveExpression(expression, scope, context);
|
|
1088
|
+
return;
|
|
1089
|
+
}
|
|
1090
|
+
const arity = getSymbolArity(expression, expression.name, scope, context);
|
|
1091
|
+
if (arity !== undefined && arity !== declaration.arity) {
|
|
742
1092
|
throw new TlaSyntaxError(
|
|
743
|
-
`
|
|
1093
|
+
`An operator must be substituted for symbol '${declaration.name}', and it must have arity ${declaration.arity} in the instance of ${instance.module} at line ${instance.location.line}, column ${instance.location.column}`,
|
|
744
1094
|
);
|
|
745
1095
|
}
|
|
1096
|
+
};
|
|
1097
|
+
const substitutions = new Map<string, Expression>();
|
|
1098
|
+
instance.substitutions.forEach((substitution) => {
|
|
1099
|
+
const name = substitution.name;
|
|
1100
|
+
const declaration = declarations.find((item) => item.name === name);
|
|
1101
|
+
if (!declaration) {
|
|
1102
|
+
report.errors.push({
|
|
1103
|
+
location: formatRange(substitution.nameRange),
|
|
1104
|
+
message: `Identifier '${name}' is not a legal target of a substitution. \nA legal target must be a declared CONSTANT or VARIABLE in the module being instantiated. \n(Also, check for warnings about multiple declarations of this same identifier.)`,
|
|
1105
|
+
});
|
|
1106
|
+
return;
|
|
1107
|
+
}
|
|
1108
|
+
const expression = substitution.expression;
|
|
1109
|
+
resolveSubstitute(declaration, expression);
|
|
1110
|
+
const range = expression.location.range;
|
|
1111
|
+
if (!substitutions.has(name)) substitutions.set(name, expression);
|
|
1112
|
+
else if (range) {
|
|
1113
|
+
report.errors.push({
|
|
1114
|
+
location: formatRange(range),
|
|
1115
|
+
message: `Multiple substitutions for symbol '${name}' in substitution.`,
|
|
1116
|
+
});
|
|
1117
|
+
}
|
|
746
1118
|
});
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
const expression: Expression =
|
|
1119
|
+
const implicit = declarations.filter((declaration) => !substitutions.has(declaration.name));
|
|
1120
|
+
const isMissing = (declaration: Declaration) =>
|
|
1121
|
+
!lookupLocal(scope, declaration.name) && !findSymbol(declaration.name, context);
|
|
1122
|
+
implicit
|
|
1123
|
+
.filter((declaration) => !isMissing(declaration))
|
|
1124
|
+
.forEach((declaration) => {
|
|
1125
|
+
const expression: Expression = {
|
|
754
1126
|
kind: "Identifier",
|
|
755
|
-
name,
|
|
1127
|
+
name: declaration.name,
|
|
756
1128
|
location: instance.location,
|
|
757
1129
|
};
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
1130
|
+
resolveSubstitute(declaration, expression);
|
|
1131
|
+
substitutions.set(declaration.name, expression);
|
|
1132
|
+
});
|
|
1133
|
+
implicit.filter(isMissing).forEach((declaration) => {
|
|
1134
|
+
const range = instance.location.range;
|
|
1135
|
+
const declared = declaration.location.range;
|
|
1136
|
+
if (range && declared) {
|
|
1137
|
+
report.errors.push({
|
|
1138
|
+
location: formatRange(range),
|
|
1139
|
+
message: `Substitution missing for symbol ${declaration.name} declared at ${formatRange(declared)} \nand instantiated in module ${module.name}.`,
|
|
1140
|
+
});
|
|
1141
|
+
}
|
|
1142
|
+
});
|
|
1143
|
+
return substitutions;
|
|
1144
|
+
};
|
|
1145
|
+
|
|
1146
|
+
// SANY reports the errors of every unit it checks, in the order of the units in the module, after
|
|
1147
|
+
// the warnings it reports for the spec. A unit reports to its lists what SANY goes on after, and
|
|
1148
|
+
// throws the error that stops it. An error tla-ts cannot word as SANY does yields to the errors the
|
|
1149
|
+
// unit reported before it. Returns the errors.
|
|
1150
|
+
export const collectSemanticErrors = (
|
|
1151
|
+
units: {
|
|
1152
|
+
location: SourceLocation;
|
|
1153
|
+
check: (reported: SanyError[], warned: SanyError[]) => void;
|
|
1154
|
+
}[],
|
|
1155
|
+
warnings: SanyError[],
|
|
1156
|
+
) => {
|
|
1157
|
+
const errors: SanyError[] = [];
|
|
1158
|
+
const add = (list: SanyError[], item: SanyError) => {
|
|
1159
|
+
if (
|
|
1160
|
+
list.every(
|
|
1161
|
+
(existing) => existing.location !== item.location || existing.message !== item.message,
|
|
1162
|
+
)
|
|
1163
|
+
) {
|
|
1164
|
+
list.push(item);
|
|
1165
|
+
}
|
|
1166
|
+
};
|
|
1167
|
+
units
|
|
1168
|
+
.toSorted((left, right) =>
|
|
1169
|
+
isBefore(left.location, right.location)
|
|
1170
|
+
? -1
|
|
1171
|
+
: isBefore(right.location, left.location)
|
|
1172
|
+
? 1
|
|
1173
|
+
: 0,
|
|
1174
|
+
)
|
|
1175
|
+
.forEach((unit) => {
|
|
1176
|
+
const reported: SanyError[] = [];
|
|
1177
|
+
const warned: SanyError[] = [];
|
|
1178
|
+
try {
|
|
1179
|
+
unit.check(reported, warned);
|
|
1180
|
+
} catch (error) {
|
|
1181
|
+
if (!(error instanceof TlaSyntaxError) || (!error.sany && reported.length === 0)) {
|
|
1182
|
+
throw error;
|
|
1183
|
+
}
|
|
1184
|
+
reported.push(...(error.sany?.errors ?? []));
|
|
766
1185
|
}
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
1186
|
+
reported.forEach((item) => add(errors, item));
|
|
1187
|
+
warned.forEach((item) => add(warnings, item));
|
|
1188
|
+
});
|
|
1189
|
+
return errors;
|
|
770
1190
|
};
|
|
771
1191
|
|
|
1192
|
+
// SANY binds the names of a module in the order of its units, and reports a RECURSIVE declaration
|
|
1193
|
+
// that no definition follows at the end. Returns the errors.
|
|
772
1194
|
export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
773
1195
|
const symbols = createSymbols(module, unit.imported);
|
|
774
1196
|
const facts = new Set(
|
|
775
1197
|
[...module.theorems, ...module.assumptions].flatMap((fact) => fact.name ?? []),
|
|
776
1198
|
);
|
|
777
|
-
const
|
|
1199
|
+
const goals = new Map(
|
|
1200
|
+
module.theorems.flatMap((theorem): [string, Goal][] =>
|
|
1201
|
+
theorem.name !== undefined && theorem.statement.kind === "AssumeProve"
|
|
1202
|
+
? [[theorem.name, { statement: theorem.statement, isSuffices: false, isInProof: false }]]
|
|
1203
|
+
: [],
|
|
1204
|
+
),
|
|
1205
|
+
);
|
|
1206
|
+
const bindings = new Map<string, ModuleBinding>(
|
|
1207
|
+
[...CORE_SYMBOLS, ...unit.bound].map(([name, symbol]) => [
|
|
1208
|
+
name,
|
|
1209
|
+
{ ...symbol, position: MODULE_START },
|
|
1210
|
+
]),
|
|
1211
|
+
);
|
|
1212
|
+
const recursive = new Set<Binding>();
|
|
1213
|
+
const declarations: { declaration: Declaration; binding: ModuleBinding | undefined }[] = [];
|
|
1214
|
+
const createUnit = (
|
|
1215
|
+
location: SourceLocation,
|
|
1216
|
+
self: Definition | undefined,
|
|
1217
|
+
errors: SanyError[],
|
|
1218
|
+
warnings: SanyError[],
|
|
1219
|
+
hasLabelScope: boolean,
|
|
1220
|
+
): Unit => ({
|
|
778
1221
|
imported: symbols,
|
|
779
1222
|
definitions: unit.definitions,
|
|
780
1223
|
getKey: unit.getKey,
|
|
781
1224
|
location,
|
|
782
1225
|
self,
|
|
783
1226
|
facts,
|
|
1227
|
+
goals,
|
|
1228
|
+
bindings,
|
|
1229
|
+
errors,
|
|
1230
|
+
warnings,
|
|
1231
|
+
hasLabelScope,
|
|
1232
|
+
isLabelForbidden: false,
|
|
1233
|
+
isGenerated: false,
|
|
784
1234
|
});
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
1235
|
+
const bindDeclarations = (declarations: Declaration[], kind: "Constant" | "Variable") =>
|
|
1236
|
+
declarations.map((declaration) => ({
|
|
1237
|
+
location: declaration.location,
|
|
1238
|
+
check: (errors: SanyError[], warnings: SanyError[]) =>
|
|
1239
|
+
bindModuleName(
|
|
1240
|
+
declaration.name,
|
|
1241
|
+
{ kind, arity: declaration.arity, range: declaration.location.range },
|
|
1242
|
+
createUnit(declaration.location, undefined, errors, warnings, false),
|
|
1243
|
+
),
|
|
1244
|
+
}));
|
|
1245
|
+
return collectSemanticErrors(
|
|
1246
|
+
[
|
|
1247
|
+
...unit.reports.map((report) => ({
|
|
1248
|
+
location: report.location,
|
|
1249
|
+
check: (errors: SanyError[], warnings: SanyError[]) => {
|
|
1250
|
+
errors.push(...report.errors);
|
|
1251
|
+
warnings.push(...report.warnings);
|
|
1252
|
+
},
|
|
1253
|
+
})),
|
|
1254
|
+
...bindDeclarations(module.constants, "Constant"),
|
|
1255
|
+
...bindDeclarations(module.variables, "Variable"),
|
|
1256
|
+
...module.units
|
|
1257
|
+
.flatMap((item) => (item.kind === "Recursive" ? item.declarations : []))
|
|
1258
|
+
.map((declaration) => ({
|
|
1259
|
+
location: declaration.location,
|
|
1260
|
+
check: (errors: SanyError[], warnings: SanyError[]) => {
|
|
1261
|
+
const binding: ModuleBinding = {
|
|
1262
|
+
kind: "Definition",
|
|
1263
|
+
arity: declaration.arity,
|
|
1264
|
+
range: declaration.location.range,
|
|
1265
|
+
position: declaration.location,
|
|
1266
|
+
isPending: true,
|
|
1267
|
+
};
|
|
1268
|
+
const isBound = isBindable(
|
|
1269
|
+
declaration.name,
|
|
1270
|
+
bindings.get(declaration.name),
|
|
1271
|
+
binding,
|
|
1272
|
+
createUnit(declaration.location, undefined, errors, warnings, false),
|
|
1273
|
+
);
|
|
1274
|
+
if (isBound) {
|
|
1275
|
+
bindings.set(declaration.name, binding);
|
|
1276
|
+
recursive.add(binding);
|
|
1277
|
+
}
|
|
1278
|
+
declarations.push({ declaration, binding: isBound ? binding : undefined });
|
|
1279
|
+
},
|
|
1280
|
+
})),
|
|
1281
|
+
...module.definitions.map((definition) => ({
|
|
1282
|
+
location: definition.location,
|
|
1283
|
+
check: (errors: SanyError[], warnings: SanyError[]) => {
|
|
1284
|
+
const inner = createUnit(definition.location, definition, errors, warnings, true);
|
|
1285
|
+
if (isTheoremDefinition(module, definition)) {
|
|
1286
|
+
resolveExpression(definition.body, undefined, inner);
|
|
1287
|
+
} else if (resolveDefinition(definition, undefined, inner, recursive)) {
|
|
1288
|
+
bindings.set(definition.name, {
|
|
1289
|
+
kind: "Definition",
|
|
1290
|
+
arity: getArity(definition),
|
|
1291
|
+
range: definition.location.range,
|
|
1292
|
+
position: definition.location,
|
|
1293
|
+
});
|
|
1294
|
+
}
|
|
1295
|
+
},
|
|
1296
|
+
})),
|
|
1297
|
+
...module.instances.map((instance) => ({
|
|
1298
|
+
location: instance.location,
|
|
1299
|
+
check: (errors: SanyError[], warnings: SanyError[]) => {
|
|
1300
|
+
const inner = createUnit(instance.location, undefined, errors, warnings, false);
|
|
1301
|
+
bindParams(undefined, instance.params, inner);
|
|
1302
|
+
unit.instanced
|
|
1303
|
+
.get(instance)
|
|
1304
|
+
?.forEach((symbol, name) => bindModuleName(name, symbol, inner));
|
|
1305
|
+
if (instance.name !== undefined) {
|
|
1306
|
+
bindModuleName(
|
|
1307
|
+
instance.name,
|
|
1308
|
+
{ kind: "Instance", arity: instance.params.length, range: instance.location.range },
|
|
1309
|
+
inner,
|
|
1310
|
+
);
|
|
1311
|
+
}
|
|
1312
|
+
},
|
|
1313
|
+
})),
|
|
1314
|
+
// An assumption holds the subexpressions its references select, labels and all, so its own
|
|
1315
|
+
// labels go unchecked.
|
|
1316
|
+
...module.assumptions.map((assumption) => ({
|
|
1317
|
+
location: assumption.location,
|
|
1318
|
+
check: (errors: SanyError[], warnings: SanyError[]) => {
|
|
1319
|
+
const inner = createUnit(assumption.location, undefined, errors, warnings, true);
|
|
1320
|
+
resolveExpression(assumption.expression, undefined, inner);
|
|
1321
|
+
if (assumption.name !== undefined) {
|
|
1322
|
+
bindModuleName(
|
|
1323
|
+
assumption.name,
|
|
1324
|
+
{ kind: "Theorem", arity: 0, range: assumption.location.range },
|
|
1325
|
+
inner,
|
|
1326
|
+
);
|
|
1327
|
+
}
|
|
1328
|
+
},
|
|
1329
|
+
})),
|
|
1330
|
+
...module.theorems.map((theorem) => ({
|
|
1331
|
+
location: theorem.location,
|
|
1332
|
+
check: (errors: SanyError[], warnings: SanyError[]) =>
|
|
1333
|
+
resolveTheorem(
|
|
1334
|
+
theorem,
|
|
1335
|
+
createUnit(theorem.location, undefined, errors, warnings, theorem.name !== undefined),
|
|
1336
|
+
),
|
|
1337
|
+
})),
|
|
1338
|
+
...module.uses.map((use) => ({
|
|
1339
|
+
location: use.location,
|
|
1340
|
+
check: (errors: SanyError[], warnings: SanyError[]) =>
|
|
1341
|
+
checkFacts(
|
|
1342
|
+
use.facts,
|
|
1343
|
+
undefined,
|
|
1344
|
+
createUnit(use.location, undefined, errors, warnings, false),
|
|
1345
|
+
),
|
|
1346
|
+
})),
|
|
1347
|
+
{
|
|
1348
|
+
location: module.location.range?.end ?? module.location,
|
|
1349
|
+
check: (errors: SanyError[]) =>
|
|
1350
|
+
declarations.forEach((item) => {
|
|
1351
|
+
const range = item.declaration.location.range;
|
|
1352
|
+
if (range && (!item.binding || item.binding.isPending)) {
|
|
1353
|
+
errors.push({
|
|
1354
|
+
location: formatRange(range),
|
|
1355
|
+
message: `Symbol ${item.declaration.name} declared in RECURSIVE statement but not defined.`,
|
|
1356
|
+
});
|
|
1357
|
+
}
|
|
1358
|
+
}),
|
|
1359
|
+
},
|
|
1360
|
+
],
|
|
1361
|
+
unit.warnings,
|
|
793
1362
|
);
|
|
794
1363
|
};
|