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