tla-ts 0.0.3-dev.6a30eee → 0.0.3-dev.6ac5593
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +70 -22
- package/dist/index.d.ts +70 -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 +20 -18
- package/dist/run-cli.js +22 -20
- package/dist/transfer.cjs +41 -15
- package/dist/transfer.js +41 -15
- package/package.json +1 -1
- package/src/ast.ts +121 -4
- package/src/builtins.ts +9 -3
- package/src/check.ts +480 -106
- package/src/cli-options.ts +11 -5
- package/src/codegen.ts +15 -22
- package/src/config.ts +224 -170
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +410 -130
- package/src/errors.ts +104 -3
- package/src/evaluate.ts +402 -182
- package/src/exit-codes.ts +21 -14
- package/src/fingerprints.ts +2 -1
- package/src/instances.ts +17 -16
- package/src/levels.ts +185 -44
- package/src/live-check.ts +132 -70
- package/src/liveness.ts +118 -24
- package/src/parallel-worker.ts +2 -2
- package/src/parallel.ts +5 -2
- package/src/parser.ts +172 -53
- package/src/resolve.ts +1378 -420
- package/src/run-cli.ts +106 -18
- package/src/runtime.ts +2 -0
- package/src/sany-lexer.ts +615 -0
- package/src/sany-parse-error.ts +3143 -0
- package/src/spec-description.ts +19 -8
- package/src/standard-modules.ts +54 -45
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +231 -171
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +177 -37
- package/src/symmetry.ts +109 -41
- package/src/tableau.ts +51 -24
- package/src/tlc-ext.ts +29 -12
- package/src/tlc-module.ts +14 -33
- 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 +242 -61
package/src/resolve.ts
CHANGED
|
@@ -1,46 +1,124 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
isLocallyResolved,
|
|
4
|
+
type AssumeProve,
|
|
5
|
+
type Bound,
|
|
6
|
+
type BoundPattern,
|
|
7
|
+
type Declaration,
|
|
8
|
+
type Definition,
|
|
9
|
+
type Expression,
|
|
10
|
+
type Fact,
|
|
11
|
+
type Instance,
|
|
12
|
+
type LabelExpression,
|
|
13
|
+
type Module,
|
|
14
|
+
type NewSymbol,
|
|
15
|
+
type OperatorParam,
|
|
16
|
+
type Proof,
|
|
17
|
+
type ProofStep,
|
|
18
|
+
type Selector,
|
|
19
|
+
type SourceLocation,
|
|
20
|
+
type SourceRange,
|
|
21
|
+
type Theorem,
|
|
15
22
|
} from "./ast";
|
|
16
|
-
import { TlaSyntaxError } from "./errors";
|
|
17
|
-
import { getChildren, isLocallyResolved } from "./evaluate";
|
|
23
|
+
import { formatRange, TlaSyntaxError, type SanyError } from "./errors";
|
|
18
24
|
|
|
19
|
-
|
|
20
|
-
|
|
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;
|
|
21
41
|
arity: number;
|
|
22
|
-
|
|
42
|
+
params?: number[];
|
|
43
|
+
range?: SourceRange;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface ModuleSymbol extends Binding {
|
|
47
|
+
key: string;
|
|
23
48
|
}
|
|
24
49
|
|
|
25
50
|
export type Symbols = Map<string, ModuleSymbol>;
|
|
26
51
|
|
|
27
|
-
|
|
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 {
|
|
28
62
|
name: string;
|
|
29
|
-
arity: number;
|
|
30
63
|
parent: LocalName | undefined;
|
|
64
|
+
goal?: Goal;
|
|
65
|
+
isPending?: boolean;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// A name the module binds, from position on.
|
|
69
|
+
interface ModuleBinding extends Binding {
|
|
70
|
+
position: SourceLocation;
|
|
71
|
+
isPending?: boolean;
|
|
31
72
|
}
|
|
32
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, or where isInExcept holds, in the new value of an EXCEPT. labelParams holds the
|
|
78
|
+
// names bound since the innermost definition, label, or step, which a label must take as parameters,
|
|
79
|
+
// and labels the labels there. isGenerated holds in a definition tla-ts generates for a LET INSTANCE,
|
|
80
|
+
// and in a subexpression that a name selects, which SANY analyzes where it is defined.
|
|
81
|
+
// getInstance gives what an INSTANCE in a LET or a proof instantiates.
|
|
33
82
|
interface Unit {
|
|
34
83
|
imported: Symbols;
|
|
84
|
+
getInstance: (instance: Instance) => InstancedModule;
|
|
35
85
|
definitions: Map<string, Definition>;
|
|
36
86
|
getKey: (definition: Definition) => string;
|
|
37
87
|
location: SourceLocation;
|
|
38
88
|
self: Definition | undefined;
|
|
39
89
|
facts: Set<string>;
|
|
90
|
+
goals: Map<string, Goal>;
|
|
91
|
+
bindings: Map<string, ModuleBinding>;
|
|
92
|
+
errors: SanyError[];
|
|
93
|
+
warnings: SanyError[];
|
|
94
|
+
hasLabelScope: boolean;
|
|
95
|
+
isLabelForbidden: boolean;
|
|
96
|
+
isInExcept: boolean;
|
|
97
|
+
labelParams: string[];
|
|
98
|
+
labels: Set<string>;
|
|
99
|
+
isGenerated: boolean;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
interface SelectionError {
|
|
103
|
+
args: Expression[];
|
|
104
|
+
error: SanyError;
|
|
40
105
|
}
|
|
41
106
|
|
|
107
|
+
interface SelectionState {
|
|
108
|
+
node: Expression | NewSymbol | AssumeProve;
|
|
109
|
+
isGoal: boolean;
|
|
110
|
+
isSufficesOpen: boolean;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
type Reference = Expression & { kind: "Identifier" | "Apply" | "Subexpression" };
|
|
114
|
+
|
|
42
115
|
export const LOCAL_RESOLUTION = { kind: "Local" } as const;
|
|
43
116
|
|
|
117
|
+
// The subexpressions that names select, and for a name that selects none, the error SANY reports and
|
|
118
|
+
// the arguments it analyzes before it.
|
|
119
|
+
export const selections = new WeakSet<Expression>();
|
|
120
|
+
export const selectionErrors = new WeakMap<Expression, SelectionError>();
|
|
121
|
+
|
|
44
122
|
const renamedArities = new WeakMap<Expression, number>();
|
|
45
123
|
|
|
46
124
|
export const KEY_SEPARATOR = "@";
|
|
@@ -52,7 +130,7 @@ const isKey = (name: string) => {
|
|
|
52
130
|
};
|
|
53
131
|
|
|
54
132
|
const CORE_SYMBOLS: Symbols = new Map(
|
|
55
|
-
["~>", "-+->", "\\cdot"].map((name) => [name, { key: name, arity: 2 }]),
|
|
133
|
+
["~>", "-+->", "\\cdot"].map((name) => [name, { key: name, arity: 2, kind: "Builtin" }]),
|
|
56
134
|
);
|
|
57
135
|
|
|
58
136
|
export const isBefore = (left: SourceLocation, right: SourceLocation) =>
|
|
@@ -61,6 +139,9 @@ export const isBefore = (left: SourceLocation, right: SourceLocation) =>
|
|
|
61
139
|
export const getArity = (definition: Definition) =>
|
|
62
140
|
definition.kind === "Operator" ? definition.params.length : 0;
|
|
63
141
|
|
|
142
|
+
export const getParamArities = (definition: Definition) =>
|
|
143
|
+
definition.kind === "Operator" ? definition.params.map((param) => param.arity) : [];
|
|
144
|
+
|
|
64
145
|
const isInstantiated = (definition: Definition) => definition.name.includes("!");
|
|
65
146
|
|
|
66
147
|
const lookupLocal = (scope: LocalName | undefined, name: string) => {
|
|
@@ -70,12 +151,99 @@ const lookupLocal = (scope: LocalName | undefined, name: string) => {
|
|
|
70
151
|
return undefined;
|
|
71
152
|
};
|
|
72
153
|
|
|
73
|
-
|
|
74
|
-
|
|
154
|
+
// A name the module binds before the unit and before the range, which a copy of a subexpression
|
|
155
|
+
// that a reference selects keeps from its definition.
|
|
156
|
+
const getModuleBinding = (name: string, unit: Unit, range: SourceRange | undefined) => {
|
|
157
|
+
const binding = unit.bindings.get(name);
|
|
158
|
+
return binding &&
|
|
159
|
+
isBefore(binding.position, unit.location) &&
|
|
160
|
+
(!range || isBefore(binding.position, range.start))
|
|
161
|
+
? binding
|
|
162
|
+
: undefined;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const isSameRange = (left: SourceRange | undefined, right: SourceRange | undefined) =>
|
|
166
|
+
left !== undefined && right !== undefined && formatRange(left) === formatRange(right);
|
|
167
|
+
|
|
168
|
+
// SymbolTable.addSymbol binds a name nothing binds yet. It rejects a built-in operator, a parameter
|
|
169
|
+
// or bound name already in use, and a symbol of another kind or arity than the one bound, and warns
|
|
170
|
+
// of any other duplicate. tla-ts binds a symbol SANY never sees, such as one it generates. Tells
|
|
171
|
+
// whether to bind the added symbol.
|
|
172
|
+
const isBindable = (name: string, current: Binding | undefined, added: Binding, unit: Unit) => {
|
|
173
|
+
if (current === undefined) return true;
|
|
174
|
+
if (isSameRange(current.range, added.range)) return false;
|
|
175
|
+
const range = added.range;
|
|
176
|
+
if (range === undefined || unit.isGenerated || (current.kind !== "Builtin" && !current.range)) {
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
const location = formatRange(range);
|
|
180
|
+
const existing = current.range && formatRange(current.range);
|
|
181
|
+
if (current.kind === "Builtin") {
|
|
182
|
+
unit.errors.push({
|
|
183
|
+
location,
|
|
184
|
+
message: `Symbol ${name} is a built-in operator, and cannot be redefined.`,
|
|
185
|
+
});
|
|
186
|
+
} else if (
|
|
187
|
+
added.kind === "Parameter" ||
|
|
188
|
+
current.kind !== added.kind ||
|
|
189
|
+
current.arity !== added.arity
|
|
190
|
+
) {
|
|
191
|
+
unit.errors.push({
|
|
192
|
+
location,
|
|
193
|
+
message: `Multiply-defined symbol '${name}': this definition or declaration conflicts \nwith the one at ${existing}.`,
|
|
194
|
+
});
|
|
195
|
+
} else {
|
|
196
|
+
unit.warnings.push({
|
|
197
|
+
location,
|
|
198
|
+
message: `Multiple declarations or definitions for symbol ${name}. \nThis duplicates the one at ${existing}.`,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
return false;
|
|
202
|
+
};
|
|
75
203
|
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
204
|
+
const bindName = (
|
|
205
|
+
scope: LocalName | undefined,
|
|
206
|
+
name: string,
|
|
207
|
+
added: Binding,
|
|
208
|
+
unit: Unit,
|
|
209
|
+
): LocalName | undefined =>
|
|
210
|
+
isBindable(
|
|
211
|
+
name,
|
|
212
|
+
lookupLocal(scope, name) ?? getModuleBinding(name, unit, added.range),
|
|
213
|
+
added,
|
|
214
|
+
unit,
|
|
215
|
+
)
|
|
216
|
+
? { ...added, name, parent: scope }
|
|
217
|
+
: scope;
|
|
218
|
+
|
|
219
|
+
// Binds the names of a module from the unit on.
|
|
220
|
+
const bindModuleName = (name: string, added: Binding, unit: Unit) => {
|
|
221
|
+
if (isBindable(name, unit.bindings.get(name), added, unit)) {
|
|
222
|
+
unit.bindings.set(name, { ...added, position: unit.location });
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
const bindNames = (
|
|
227
|
+
scope: LocalName | undefined,
|
|
228
|
+
names: string[],
|
|
229
|
+
ranges: SourceRange[] | undefined,
|
|
230
|
+
unit: Unit,
|
|
231
|
+
) =>
|
|
232
|
+
names.reduce(
|
|
233
|
+
(parent, name, index) =>
|
|
234
|
+
bindName(parent, name, { kind: "Parameter", arity: 0, range: ranges?.[index] }, unit),
|
|
235
|
+
scope,
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
const bindParams = (scope: LocalName | undefined, params: OperatorParam[], unit: Unit) =>
|
|
239
|
+
params.reduce(
|
|
240
|
+
(parent, param) =>
|
|
241
|
+
bindName(
|
|
242
|
+
parent,
|
|
243
|
+
param.name,
|
|
244
|
+
{ kind: "Parameter", arity: param.arity, range: param.location?.range },
|
|
245
|
+
unit,
|
|
246
|
+
),
|
|
79
247
|
scope,
|
|
80
248
|
);
|
|
81
249
|
|
|
@@ -98,10 +266,33 @@ const getVisibleDefinition = (name: string, unit: Unit) => {
|
|
|
98
266
|
const findSymbol = (name: string, unit: Unit): ModuleSymbol | undefined => {
|
|
99
267
|
const definition = getVisibleDefinition(name, unit);
|
|
100
268
|
return definition
|
|
101
|
-
? {
|
|
269
|
+
? {
|
|
270
|
+
key: unit.getKey(definition),
|
|
271
|
+
arity: getArity(definition),
|
|
272
|
+
params: getParamArities(definition),
|
|
273
|
+
kind: "Definition",
|
|
274
|
+
}
|
|
102
275
|
: unit.imported.get(name);
|
|
103
276
|
};
|
|
104
277
|
|
|
278
|
+
const findBinding = (name: string, scope: LocalName | undefined, unit: Unit): Binding | undefined =>
|
|
279
|
+
lookupLocal(scope, name) ?? findSymbol(name, unit);
|
|
280
|
+
|
|
281
|
+
// SANY reports a name at the range of its text.
|
|
282
|
+
const getNameRange = (location: SourceLocation, name: string): SourceRange => ({
|
|
283
|
+
module: location.range?.module ?? "",
|
|
284
|
+
start: location,
|
|
285
|
+
end: { line: location.line, column: location.column + name.length - 1 },
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
// SANY reports an operator symbol applied to operands as one it cannot resolve.
|
|
289
|
+
const getFixity = (expression: Expression) => {
|
|
290
|
+
if (expression.kind !== "Apply") return undefined;
|
|
291
|
+
if (expression.args.length === 2) return "infix";
|
|
292
|
+
if (expression.args.length !== 1) return undefined;
|
|
293
|
+
return isBefore(expression.location, expression.args[0].location) ? "prefix" : "postfix";
|
|
294
|
+
};
|
|
295
|
+
|
|
105
296
|
// SANY names an undefined operator symbol such as + differently from an undefined identifier.
|
|
106
297
|
const isOperatorSymbol = (name: string) =>
|
|
107
298
|
name[0].toLowerCase() === name[0].toUpperCase() &&
|
|
@@ -111,6 +302,407 @@ const isOperatorSymbol = (name: string) =>
|
|
|
111
302
|
const isStepName = (name: string) =>
|
|
112
303
|
name.startsWith("<") && ((name[1] >= "0" && name[1] <= "9") || name[1] === "*");
|
|
113
304
|
|
|
305
|
+
export const getOperandIndex = (operand: number | "<<" | ">>", count: number) => {
|
|
306
|
+
if (typeof operand === "number") return operand >= 1 && operand <= count ? operand - 1 : -1;
|
|
307
|
+
if (operand === "<<") return count > 0 ? 0 : -1;
|
|
308
|
+
return count === 2 ? 1 : -1;
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
// The parts of a name such as F(x)!l(y), each with its own arguments. counts holds the number each
|
|
312
|
+
// part takes, when a part before the last takes some.
|
|
313
|
+
export const getNameSelectors = (
|
|
314
|
+
name: string,
|
|
315
|
+
args: Expression[],
|
|
316
|
+
counts: number[] | undefined,
|
|
317
|
+
): Selector[] => {
|
|
318
|
+
const parts = name.split("!");
|
|
319
|
+
const partCounts =
|
|
320
|
+
counts ?? parts.map((_, index) => (index === parts.length - 1 ? args.length : 0));
|
|
321
|
+
return parts.map((part, index) => {
|
|
322
|
+
const start = partCounts.slice(0, index).reduce((sum, count) => sum + count, 0);
|
|
323
|
+
return { kind: "Name", name: part, args: args.slice(start, start + partCounts[index]) };
|
|
324
|
+
});
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
const getSelectors = (reference: Reference): Selector[] =>
|
|
328
|
+
reference.kind === "Subexpression"
|
|
329
|
+
? reference.selectors
|
|
330
|
+
: reference.kind === "Identifier"
|
|
331
|
+
? getNameSelectors(reference.name, [], undefined)
|
|
332
|
+
: getNameSelectors(reference.operator, reference.args, reference.argumentCounts);
|
|
333
|
+
|
|
334
|
+
const getBoundSets = (bounds: Bound[]) =>
|
|
335
|
+
bounds
|
|
336
|
+
.filter((bound, index) => index === 0 || bounds[index - 1].set !== bound.set)
|
|
337
|
+
.map((bound) => bound.set);
|
|
338
|
+
|
|
339
|
+
// The names a binder binds, its body, and the sets it binds them to, which a selector such as !1
|
|
340
|
+
// chooses from.
|
|
341
|
+
export const getBinder = (
|
|
342
|
+
expression: Expression,
|
|
343
|
+
): { names: string[]; body: Expression; sets: Expression[] } | undefined => {
|
|
344
|
+
switch (expression.kind) {
|
|
345
|
+
case "Forall":
|
|
346
|
+
case "Exists":
|
|
347
|
+
case "FunctionConstruction":
|
|
348
|
+
return {
|
|
349
|
+
names: getBoundNames(expression.bounds),
|
|
350
|
+
body: expression.body,
|
|
351
|
+
sets: getBoundSets(expression.bounds),
|
|
352
|
+
};
|
|
353
|
+
case "SetMap":
|
|
354
|
+
return {
|
|
355
|
+
names: getBoundNames(expression.bounds),
|
|
356
|
+
body: expression.expression,
|
|
357
|
+
sets: getBoundSets(expression.bounds),
|
|
358
|
+
};
|
|
359
|
+
case "SetFilter":
|
|
360
|
+
return {
|
|
361
|
+
names: getPatternNames(expression.bound.pattern),
|
|
362
|
+
body: expression.predicate,
|
|
363
|
+
sets: [expression.bound.set],
|
|
364
|
+
};
|
|
365
|
+
case "Choose":
|
|
366
|
+
return {
|
|
367
|
+
names: getPatternNames(expression.pattern),
|
|
368
|
+
body: expression.body,
|
|
369
|
+
sets: expression.set ? [expression.set] : [],
|
|
370
|
+
};
|
|
371
|
+
case "UnboundedQuantifier":
|
|
372
|
+
case "Lambda":
|
|
373
|
+
return {
|
|
374
|
+
names: expression.kind === "Lambda" ? expression.params : expression.names,
|
|
375
|
+
body: expression.body,
|
|
376
|
+
sets: [],
|
|
377
|
+
};
|
|
378
|
+
default:
|
|
379
|
+
return undefined;
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
// The operands that a selector such as !1 chooses from, where they do not depend on bound names.
|
|
384
|
+
export const getOperands = (expression: Expression): Expression[] | undefined => {
|
|
385
|
+
switch (expression.kind) {
|
|
386
|
+
case "Apply":
|
|
387
|
+
return expression.args;
|
|
388
|
+
case "And":
|
|
389
|
+
case "Or":
|
|
390
|
+
return expression.operands;
|
|
391
|
+
case "If":
|
|
392
|
+
return [expression.condition, expression.consequent, expression.alternative];
|
|
393
|
+
case "Tuple":
|
|
394
|
+
case "SetEnumeration":
|
|
395
|
+
return expression.elements;
|
|
396
|
+
case "Record":
|
|
397
|
+
case "RecordSet":
|
|
398
|
+
return expression.fields.map((field) => field.value);
|
|
399
|
+
case "FunctionApplication":
|
|
400
|
+
return [
|
|
401
|
+
expression.function,
|
|
402
|
+
expression.args.length === 1
|
|
403
|
+
? expression.args[0]
|
|
404
|
+
: { kind: "Tuple", elements: expression.args, location: expression.location },
|
|
405
|
+
];
|
|
406
|
+
case "FieldAccess":
|
|
407
|
+
return [
|
|
408
|
+
expression.record,
|
|
409
|
+
{ kind: "String", value: expression.field, location: expression.location },
|
|
410
|
+
];
|
|
411
|
+
case "Prime":
|
|
412
|
+
case "Unchanged":
|
|
413
|
+
case "Enabled":
|
|
414
|
+
case "Always":
|
|
415
|
+
case "Eventually":
|
|
416
|
+
return [expression.expression];
|
|
417
|
+
case "BoxAction":
|
|
418
|
+
case "AngleAction":
|
|
419
|
+
return [expression.action, expression.subscript];
|
|
420
|
+
case "WeakFairness":
|
|
421
|
+
case "StrongFairness":
|
|
422
|
+
return [expression.subscript, expression.action];
|
|
423
|
+
case "FunctionSet":
|
|
424
|
+
return [expression.domain, expression.range];
|
|
425
|
+
case "Product":
|
|
426
|
+
return expression.factors;
|
|
427
|
+
case "Except":
|
|
428
|
+
return [expression.function];
|
|
429
|
+
default:
|
|
430
|
+
return undefined;
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
const isReference = (expression: Expression): expression is Reference =>
|
|
435
|
+
expression.kind === "Identifier" ||
|
|
436
|
+
expression.kind === "Apply" ||
|
|
437
|
+
expression.kind === "Subexpression";
|
|
438
|
+
|
|
439
|
+
const isInScopeOfDeclaration = (assumeProve: AssumeProve, clause: number) =>
|
|
440
|
+
assumeProve.assumptions.slice(0, clause).some((assumption) => assumption.kind === "NewSymbol");
|
|
441
|
+
|
|
442
|
+
const findLabel = (expression: Expression, name: string): LabelExpression | undefined => {
|
|
443
|
+
if (expression.kind === "Label") return expression.name === name ? expression : undefined;
|
|
444
|
+
if (expression.kind === "Lambda") return undefined;
|
|
445
|
+
if (expression.kind === "Let") return findLabel(expression.body, name);
|
|
446
|
+
return getChildren(expression).reduce<LabelExpression | undefined>(
|
|
447
|
+
(found, child) => found ?? findLabel(child, name),
|
|
448
|
+
undefined,
|
|
449
|
+
);
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
// SANY drops a label in the scope of a NEW in a nested ASSUME, after it reports the label.
|
|
453
|
+
const findClauseLabel = (
|
|
454
|
+
clause: Expression | NewSymbol | AssumeProve,
|
|
455
|
+
name: string,
|
|
456
|
+
isForbidden: boolean,
|
|
457
|
+
): LabelExpression | undefined => {
|
|
458
|
+
if (clause.kind === "AssumeProve") {
|
|
459
|
+
return [...clause.assumptions, clause.conclusion].reduce<LabelExpression | undefined>(
|
|
460
|
+
(found, inner, index) =>
|
|
461
|
+
found ?? findClauseLabel(inner, name, isForbidden || isInScopeOfDeclaration(clause, index)),
|
|
462
|
+
undefined,
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
if (isForbidden) return undefined;
|
|
466
|
+
if (clause.kind === "NewSymbol") return clause.set && findLabel(clause.set, name);
|
|
467
|
+
return findLabel(clause, name);
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
const getLabelBody = (expression: Expression): Expression =>
|
|
471
|
+
expression.kind === "Label" ? getLabelBody(expression.body) : expression;
|
|
472
|
+
|
|
473
|
+
const getOperandName = (operand: number | "<<" | ">>") =>
|
|
474
|
+
typeof operand === "number" ? `argument selector ${operand}` : operand;
|
|
475
|
+
|
|
476
|
+
const SCOPE_MESSAGE = "within the scope of a declaration\n from outside that declaration's scope.";
|
|
477
|
+
|
|
478
|
+
// Generator.selectorToNode for a name that selects part of a theorem or step. A part in the scope of
|
|
479
|
+
// a NEW in its ASSUME may be selected only within the proof of the theorem, or only after the proof
|
|
480
|
+
// of a SUFFICES step. A label takes its own arguments, and an operand must exist, unless the part
|
|
481
|
+
// selected depends on bound names. A fact may select an ASSUME/PROVE or a NEW clause.
|
|
482
|
+
const checkSelection = (
|
|
483
|
+
reference: Reference,
|
|
484
|
+
selectors: Selector[],
|
|
485
|
+
goal: Goal,
|
|
486
|
+
scope: LocalName | undefined,
|
|
487
|
+
unit: Unit,
|
|
488
|
+
isFact: boolean,
|
|
489
|
+
) => {
|
|
490
|
+
const report = (index: number | undefined, message: string) => {
|
|
491
|
+
const range =
|
|
492
|
+
(index === undefined ? undefined : reference.selectorRanges?.[index]) ??
|
|
493
|
+
reference.location.range;
|
|
494
|
+
if (range) unit.errors.push({ location: formatRange(range), message });
|
|
495
|
+
};
|
|
496
|
+
const isOutOfScope = goal.isSuffices === goal.isInProof;
|
|
497
|
+
const statement = goal.statement;
|
|
498
|
+
const state: SelectionState = {
|
|
499
|
+
node: statement,
|
|
500
|
+
isGoal: true,
|
|
501
|
+
isSufficesOpen: goal.isSuffices,
|
|
502
|
+
};
|
|
503
|
+
for (const [offset, selector] of selectors.slice(1).entries()) {
|
|
504
|
+
const index = offset + 1;
|
|
505
|
+
const node = state.node;
|
|
506
|
+
if (selector.kind === "Name") {
|
|
507
|
+
if (selectors[index - 1].kind !== "Name") {
|
|
508
|
+
const isLet =
|
|
509
|
+
node.kind !== "NewSymbol" &&
|
|
510
|
+
node.kind !== "AssumeProve" &&
|
|
511
|
+
getLabelBody(node).kind === "Let";
|
|
512
|
+
return isLet
|
|
513
|
+
? undefined
|
|
514
|
+
: report(index - 1, "A name selector here must be from a LET clause.");
|
|
515
|
+
}
|
|
516
|
+
const clauses =
|
|
517
|
+
statement.kind === "AssumeProve"
|
|
518
|
+
? [...statement.assumptions, statement.conclusion]
|
|
519
|
+
: [statement];
|
|
520
|
+
const found =
|
|
521
|
+
index === 1
|
|
522
|
+
? clauses
|
|
523
|
+
.map((clause, position) => ({
|
|
524
|
+
position,
|
|
525
|
+
label: findClauseLabel(clause, selector.name, false),
|
|
526
|
+
}))
|
|
527
|
+
.find((item) => item.label !== undefined)
|
|
528
|
+
: {
|
|
529
|
+
position: 0,
|
|
530
|
+
label: node.kind === "Label" ? findLabel(node.body, selector.name) : undefined,
|
|
531
|
+
};
|
|
532
|
+
if (!found?.label) return report(index, `Cannot find label \`${selector.name}'.`);
|
|
533
|
+
if (
|
|
534
|
+
index === 1 &&
|
|
535
|
+
statement.kind === "AssumeProve" &&
|
|
536
|
+
isInScopeOfDeclaration(statement, found.position) &&
|
|
537
|
+
isOutOfScope
|
|
538
|
+
) {
|
|
539
|
+
return report(
|
|
540
|
+
1,
|
|
541
|
+
`Accessing subexpression labeled \`${selector.name}' of ASSUME/PROVE clause ${SCOPE_MESSAGE}`,
|
|
542
|
+
);
|
|
543
|
+
}
|
|
544
|
+
if (selector.args.length !== found.label.params.length) {
|
|
545
|
+
return report(index, `Label \`${selector.name}' used with wrong number of arguments.`);
|
|
546
|
+
}
|
|
547
|
+
state.node = found.label;
|
|
548
|
+
state.isGoal = false;
|
|
549
|
+
state.isSufficesOpen = false;
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
if (selector.kind !== "Operand") return;
|
|
553
|
+
const operand = getOperandName(selector.operand);
|
|
554
|
+
if (state.isSufficesOpen) {
|
|
555
|
+
if (getOperandIndex(selector.operand, 1) !== 0) {
|
|
556
|
+
return report(
|
|
557
|
+
index,
|
|
558
|
+
node.kind === "AssumeProve"
|
|
559
|
+
? "Accessing non-existent subexpression of a SUFFICES"
|
|
560
|
+
: `Nonexistent operand specified by \`${operand}'.`,
|
|
561
|
+
);
|
|
562
|
+
}
|
|
563
|
+
state.isSufficesOpen = false;
|
|
564
|
+
continue;
|
|
565
|
+
}
|
|
566
|
+
if (node.kind === "NewSymbol") return;
|
|
567
|
+
if (node.kind !== "AssumeProve") {
|
|
568
|
+
const expression = getLabelBody(node);
|
|
569
|
+
if (["Number", "Decimal", "String"].includes(expression.kind)) {
|
|
570
|
+
return report(index, "Selecting subexpression of expression that has none.");
|
|
571
|
+
}
|
|
572
|
+
const kind =
|
|
573
|
+
expression.kind === "Identifier"
|
|
574
|
+
? findBinding(expression.name, scope, unit)?.kind
|
|
575
|
+
: undefined;
|
|
576
|
+
if (kind === "Variable" || kind?.startsWith("New")) {
|
|
577
|
+
return report(index, `Choosing operand \`${operand}' of subexpression with no operands.`);
|
|
578
|
+
}
|
|
579
|
+
const sets = getBinder(expression)?.sets;
|
|
580
|
+
const operands =
|
|
581
|
+
expression.kind === "Identifier"
|
|
582
|
+
? []
|
|
583
|
+
: sets
|
|
584
|
+
? sets.length > 0
|
|
585
|
+
? sets
|
|
586
|
+
: undefined
|
|
587
|
+
: expression.kind === "Except"
|
|
588
|
+
? undefined
|
|
589
|
+
: getOperands(expression);
|
|
590
|
+
if (!operands) return;
|
|
591
|
+
const position = getOperandIndex(selector.operand, operands.length);
|
|
592
|
+
if (position === -1) return report(index, `Nonexistent operand specified by \`${operand}'.`);
|
|
593
|
+
state.node = operands[position];
|
|
594
|
+
state.isGoal = false;
|
|
595
|
+
continue;
|
|
596
|
+
}
|
|
597
|
+
const clauses = [...node.assumptions, node.conclusion];
|
|
598
|
+
const position = getOperandIndex(selector.operand, clauses.length);
|
|
599
|
+
if (position === -1) return report(index, `Nonexistent operand specified by \`${operand}'.`);
|
|
600
|
+
if (isInScopeOfDeclaration(node, position) && (!state.isGoal || isOutOfScope)) {
|
|
601
|
+
return report(index, `Accessing ASSUME/PROVE clause ${SCOPE_MESSAGE}`);
|
|
602
|
+
}
|
|
603
|
+
const clause = clauses[position];
|
|
604
|
+
if (clause.kind === "NewSymbol" && index !== selectors.length - 1) {
|
|
605
|
+
return report(index, "Selected a subexpression of a NEW clause of an ASSUME.");
|
|
606
|
+
}
|
|
607
|
+
state.node = clause;
|
|
608
|
+
state.isGoal = false;
|
|
609
|
+
}
|
|
610
|
+
if (isFact) return;
|
|
611
|
+
if (state.node.kind === "AssumeProve") {
|
|
612
|
+
report(undefined, "Selected ASSUME/PROVE instead of expression.");
|
|
613
|
+
}
|
|
614
|
+
if (state.node.kind === "NewSymbol") {
|
|
615
|
+
report(undefined, "Selected a NEW declaration as an expression or operator.");
|
|
616
|
+
}
|
|
617
|
+
};
|
|
618
|
+
|
|
619
|
+
// Generator.generateLabel reports a label where it may not appear, and skips the label's body.
|
|
620
|
+
const isLabelReported = (label: LabelExpression, unit: Unit) => {
|
|
621
|
+
const message = unit.isGenerated
|
|
622
|
+
? undefined
|
|
623
|
+
: !unit.hasLabelScope
|
|
624
|
+
? "Label not in definition or proof step."
|
|
625
|
+
: unit.isLabelForbidden
|
|
626
|
+
? "Label not allowed within scope of declaration in nested ASSUME/PROVE."
|
|
627
|
+
: unit.isInExcept
|
|
628
|
+
? "Labels inside EXCEPT clauses are not yet implemented."
|
|
629
|
+
: undefined;
|
|
630
|
+
if (message === undefined || !label.location.range) return false;
|
|
631
|
+
unit.errors.push({ location: formatRange(label.location.range), message });
|
|
632
|
+
return true;
|
|
633
|
+
};
|
|
634
|
+
|
|
635
|
+
// Generator.generateLabel reports, after the label's body, each parameter that names no bound name,
|
|
636
|
+
// then formalParamsEqual each parameter given twice, each name bound since the label scope began
|
|
637
|
+
// that the label leaves out, and the parameters it adds, and then a label of a name already used.
|
|
638
|
+
const checkLabel = (label: LabelExpression, scope: LocalName | undefined, unit: Unit) => {
|
|
639
|
+
const range = label.location.range;
|
|
640
|
+
if (!range || unit.isGenerated) return;
|
|
641
|
+
const location = formatRange(range);
|
|
642
|
+
const legal = label.params.filter((param, index) => {
|
|
643
|
+
const isLegal = lookupLocal(scope, param)?.kind === "Parameter";
|
|
644
|
+
const paramRange = label.paramRanges?.[index];
|
|
645
|
+
if (!isLegal && paramRange) {
|
|
646
|
+
unit.errors.push({
|
|
647
|
+
location: formatRange(paramRange),
|
|
648
|
+
message: `Illegal parameter ${param} of label \`${label.name}'.`,
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
return isLegal;
|
|
652
|
+
});
|
|
653
|
+
legal
|
|
654
|
+
.filter((param, index) => legal.indexOf(param) !== index)
|
|
655
|
+
.forEach((param) =>
|
|
656
|
+
unit.errors.push({
|
|
657
|
+
location,
|
|
658
|
+
message: `Repeated formal parameter ${param} \nin label \`${label.name}'.`,
|
|
659
|
+
}),
|
|
660
|
+
);
|
|
661
|
+
unit.labelParams
|
|
662
|
+
.filter((param) => !legal.includes(param))
|
|
663
|
+
.forEach((param) =>
|
|
664
|
+
unit.errors.push({
|
|
665
|
+
location,
|
|
666
|
+
message: `Label ${label.name} must contain formal parameter \`${param}'.`,
|
|
667
|
+
}),
|
|
668
|
+
);
|
|
669
|
+
const extra = [
|
|
670
|
+
...new Set(legal.filter((param) => !unit.labelParams.includes(param))),
|
|
671
|
+
...label.params.filter((param) => !legal.includes(param)),
|
|
672
|
+
];
|
|
673
|
+
if (extra.length > 0) {
|
|
674
|
+
unit.errors.push({
|
|
675
|
+
location,
|
|
676
|
+
message: `Label ${label.name} declares extra parameter(s) ${extra.map((param) => `${param} `).join("")}`,
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
if (unit.labels.has(label.name)) {
|
|
680
|
+
unit.errors.push({ location, message: `Duplicate label \`${label.name}'.` });
|
|
681
|
+
}
|
|
682
|
+
unit.labels.add(label.name);
|
|
683
|
+
};
|
|
684
|
+
|
|
685
|
+
// Checks a name that selects part of a theorem or step, and tells whether it is one.
|
|
686
|
+
const checkReference = (
|
|
687
|
+
expression: Expression,
|
|
688
|
+
scope: LocalName | undefined,
|
|
689
|
+
unit: Unit,
|
|
690
|
+
isFact: boolean,
|
|
691
|
+
) => {
|
|
692
|
+
if (!isReference(expression)) return false;
|
|
693
|
+
const selectors = getSelectors(expression);
|
|
694
|
+
const first = selectors[0];
|
|
695
|
+
const goal =
|
|
696
|
+
selectors.length > 1 && first.kind === "Name"
|
|
697
|
+
? (lookupLocal(scope, first.name)?.goal ?? unit.goals.get(first.name))
|
|
698
|
+
: undefined;
|
|
699
|
+
if (goal) checkSelection(expression, selectors, goal, scope, unit, isFact);
|
|
700
|
+
return goal !== undefined;
|
|
701
|
+
};
|
|
702
|
+
|
|
703
|
+
// Generator.selectorToNode reports an unknown operator and goes on without analyzing its arguments,
|
|
704
|
+
// so its arity is undefined. It does the same for an operator given the wrong number of arguments,
|
|
705
|
+
// and for a name that selects nothing, after the arguments of the parts before.
|
|
114
706
|
const getSymbolArity = (
|
|
115
707
|
expression: Expression & { kind: "Identifier" | "Apply" | "OperatorReference" },
|
|
116
708
|
name: string,
|
|
@@ -118,10 +710,13 @@ const getSymbolArity = (
|
|
|
118
710
|
unit: Unit,
|
|
119
711
|
) => {
|
|
120
712
|
const location = expression.location;
|
|
713
|
+
if (isSelectionReported(expression, scope, unit)) return undefined;
|
|
121
714
|
const renamed = renamedArities.get(expression);
|
|
122
715
|
if (renamed !== undefined) return renamed;
|
|
123
716
|
if (isKey(name)) return expression.kind === "Apply" ? expression.args.length : 0;
|
|
124
|
-
if (isStepName(name))
|
|
717
|
+
if (checkReference(expression, scope, unit, false) || isStepName(name)) {
|
|
718
|
+
return expression.kind === "Apply" ? expression.args.length : 0;
|
|
719
|
+
}
|
|
125
720
|
const local = lookupLocal(scope, name);
|
|
126
721
|
if (local && expression.kind !== "OperatorReference") expression.resolution = LOCAL_RESOLUTION;
|
|
127
722
|
if (local) return local.arity;
|
|
@@ -134,7 +729,7 @@ const getSymbolArity = (
|
|
|
134
729
|
}
|
|
135
730
|
// A name such as Inv!P1 selects the label P1 of a definition or theorem, which takes its own
|
|
136
731
|
// arguments.
|
|
137
|
-
const prefix = name.slice(0, name.indexOf("!"));
|
|
732
|
+
const prefix = name.includes("!") ? name.slice(0, name.indexOf("!")) : "";
|
|
138
733
|
const isSelected =
|
|
139
734
|
prefix !== "" &&
|
|
140
735
|
(lookupLocal(scope, prefix) !== undefined ||
|
|
@@ -143,18 +738,30 @@ const getSymbolArity = (
|
|
|
143
738
|
if (isSelected) {
|
|
144
739
|
return expression.kind === "Apply" ? expression.args.length : 0;
|
|
145
740
|
}
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
741
|
+
const range = formatRange(getNameRange(location, name === "-." ? "-" : name));
|
|
742
|
+
if (!isOperatorSymbol(name)) {
|
|
743
|
+
unit.errors.push({ location: range, message: `Unknown operator: \`${name}'.` });
|
|
744
|
+
return undefined;
|
|
745
|
+
}
|
|
746
|
+
unit.errors.push({
|
|
747
|
+
location: range,
|
|
748
|
+
message: `Could not find declaration or definition of symbol '${name}'.`,
|
|
749
|
+
});
|
|
750
|
+
const fixity = getFixity(expression);
|
|
751
|
+
if (fixity && location.range) {
|
|
752
|
+
unit.errors.push({
|
|
753
|
+
location: formatRange(location.range),
|
|
754
|
+
message: `Couldn't resolve ${fixity} operator symbol \`${name}'.`,
|
|
755
|
+
});
|
|
756
|
+
}
|
|
757
|
+
return undefined;
|
|
152
758
|
};
|
|
153
759
|
|
|
154
760
|
const resolveBounds = (bounds: Bound[], scope: LocalName | undefined, unit: Unit) => {
|
|
155
761
|
bounds.forEach((bound) => resolveExpression(bound.set, scope, unit));
|
|
156
762
|
return bounds.reduce(
|
|
157
|
-
(current, bound) =>
|
|
763
|
+
(current, bound) =>
|
|
764
|
+
bindNames(current, getPatternNames(bound.pattern), bound.pattern.ranges, unit),
|
|
158
765
|
scope,
|
|
159
766
|
);
|
|
160
767
|
};
|
|
@@ -170,94 +777,237 @@ const markRecursiveConstructions = (expression: Expression, name: string): void
|
|
|
170
777
|
getChildren(expression).forEach((child) => markRecursiveConstructions(child, name));
|
|
171
778
|
};
|
|
172
779
|
|
|
173
|
-
|
|
174
|
-
|
|
780
|
+
// Generator.processOperator binds the parameters, reports a name already bound unless a RECURSIVE
|
|
781
|
+
// declaration of this level awaits the definition, analyzes the body, and then binds the name.
|
|
782
|
+
// processFunction binds the bound names before it looks up the name, and binds the name only where
|
|
783
|
+
// nothing else does. recursive holds the declarations of this level. Tells whether to bind the name.
|
|
784
|
+
const resolveDefinition = (
|
|
785
|
+
definition: Definition,
|
|
786
|
+
scope: LocalName | undefined,
|
|
787
|
+
outer: Unit,
|
|
788
|
+
recursive: Set<Binding>,
|
|
789
|
+
) => {
|
|
790
|
+
const unit = {
|
|
791
|
+
...outer,
|
|
792
|
+
hasLabelScope: true,
|
|
793
|
+
labelParams: definition.kind === "Function" ? getBoundNames(definition.bounds) : [],
|
|
794
|
+
labels: new Set<string>(),
|
|
795
|
+
};
|
|
796
|
+
const name = definition.name;
|
|
797
|
+
const range = definition.location.range;
|
|
798
|
+
const report = (message: string) => {
|
|
799
|
+
if (range && !unit.isGenerated) unit.errors.push({ location: formatRange(range), message });
|
|
800
|
+
};
|
|
801
|
+
const inner =
|
|
802
|
+
definition.kind === "Function"
|
|
803
|
+
? resolveBounds(definition.bounds, scope, unit)
|
|
804
|
+
: bindParams(scope, definition.params, unit);
|
|
805
|
+
const current = lookupLocal(inner, name) ?? getModuleBinding(name, unit, range);
|
|
806
|
+
const isDeclared = current?.isPending === true && recursive.has(current);
|
|
807
|
+
if (current?.isPending && !isDeclared) {
|
|
808
|
+
report(
|
|
809
|
+
`Recursive ${definition.kind === "Function" ? "function" : "operator"} ${name} defined at wrong LET/IN level.`,
|
|
810
|
+
);
|
|
811
|
+
} else if (current && !isDeclared) {
|
|
812
|
+
report(
|
|
813
|
+
definition.kind === "Function"
|
|
814
|
+
? `Function name \`${name}' already defined or declared.`
|
|
815
|
+
: `Operator ${name} already defined or declared.`,
|
|
816
|
+
);
|
|
817
|
+
}
|
|
175
818
|
if (definition.kind === "Function") {
|
|
176
|
-
resolveExpression(
|
|
177
|
-
|
|
178
|
-
|
|
819
|
+
resolveExpression(
|
|
820
|
+
definition.body,
|
|
821
|
+
{ name, kind: "Parameter", arity: 0, range, parent: inner },
|
|
822
|
+
unit,
|
|
823
|
+
);
|
|
824
|
+
markRecursiveConstructions(definition.body, name);
|
|
825
|
+
} else resolveExpression(definition.body, inner, unit);
|
|
826
|
+
if (current && isDeclared) {
|
|
827
|
+
current.isPending = false;
|
|
828
|
+
current.range = range;
|
|
829
|
+
return false;
|
|
179
830
|
}
|
|
180
|
-
|
|
831
|
+
return definition.kind === "Function"
|
|
832
|
+
? current === undefined
|
|
833
|
+
: isBindable(
|
|
834
|
+
name,
|
|
835
|
+
lookupLocal(scope, name) ?? getModuleBinding(name, unit, range),
|
|
836
|
+
{ kind: "Definition", arity: getArity(definition), range },
|
|
837
|
+
unit,
|
|
838
|
+
);
|
|
181
839
|
};
|
|
182
840
|
|
|
841
|
+
// SANY analyzes an INSTANCE in a LET where it appears, before the definitions that follow it, which
|
|
842
|
+
// tla-ts generates at its location and analyzes without reporting.
|
|
183
843
|
const resolveDefinitions = (
|
|
184
844
|
definitions: Definition[],
|
|
185
845
|
scope: LocalName | undefined,
|
|
186
846
|
unit: Unit,
|
|
847
|
+
instances: Instance[] = [],
|
|
187
848
|
) => {
|
|
188
|
-
const
|
|
849
|
+
const recursive = new Set<Binding>();
|
|
850
|
+
const declared = definitions
|
|
189
851
|
.filter((definition) => definition.isRecursive)
|
|
190
|
-
.reduce<LocalName | undefined>(
|
|
191
|
-
(parent, definition
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
) =>
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
)
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
852
|
+
.reduce<LocalName | undefined>((parent, definition) => {
|
|
853
|
+
if (lookupLocal(parent, definition.name)?.isPending) return parent;
|
|
854
|
+
const local: LocalName = {
|
|
855
|
+
name: definition.name,
|
|
856
|
+
arity: getArity(definition),
|
|
857
|
+
params: getParamArities(definition),
|
|
858
|
+
kind: "Definition",
|
|
859
|
+
parent,
|
|
860
|
+
isPending: true,
|
|
861
|
+
};
|
|
862
|
+
recursive.add(local);
|
|
863
|
+
return local;
|
|
864
|
+
}, scope);
|
|
865
|
+
const pending = unit.isGenerated ? [] : [...instances];
|
|
866
|
+
const resolveInstances = (
|
|
867
|
+
current: LocalName | undefined,
|
|
868
|
+
isDue: (instance: Instance) => boolean,
|
|
869
|
+
) => {
|
|
870
|
+
const count = pending.findIndex((instance) => !isDue(instance));
|
|
871
|
+
return pending
|
|
872
|
+
.splice(0, count === -1 ? pending.length : count)
|
|
873
|
+
.reduce((parent, instance) => resolveInstance(instance, parent, unit), current);
|
|
874
|
+
};
|
|
875
|
+
const resolved = definitions.reduce<LocalName | undefined>((previous, definition) => {
|
|
876
|
+
const current = isKey(definition.name)
|
|
877
|
+
? previous
|
|
878
|
+
: resolveInstances(previous, (instance) => !isBefore(definition.location, instance.location));
|
|
879
|
+
const local: LocalName = {
|
|
880
|
+
name: definition.name,
|
|
881
|
+
arity: getArity(definition),
|
|
882
|
+
kind: "Definition",
|
|
883
|
+
range: definition.location.range,
|
|
884
|
+
parent: current,
|
|
885
|
+
};
|
|
886
|
+
if (isInstantiated(definition)) return local;
|
|
887
|
+
const isInstanced = instances.some((instance) => instance.location === definition.location);
|
|
888
|
+
const isGenerated = unit.isGenerated || definition.isLocal || isKey(definition.name);
|
|
889
|
+
const inner = isInstanced
|
|
890
|
+
? { ...unit, isGenerated: true, errors: [], warnings: [] }
|
|
891
|
+
: isGenerated
|
|
892
|
+
? { ...unit, isGenerated }
|
|
893
|
+
: unit;
|
|
894
|
+
return resolveDefinition(definition, current, inner, recursive)
|
|
895
|
+
? { ...local, params: getParamArities(definition) }
|
|
896
|
+
: current;
|
|
897
|
+
}, declared);
|
|
898
|
+
return resolveInstances(resolved, () => true);
|
|
223
899
|
};
|
|
224
900
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
: undefined;
|
|
231
|
-
|
|
901
|
+
// Generator.generateExprOrOpArg analyzes an argument as an expression, unless a defined operator
|
|
902
|
+
// takes an operator in its place, which a name, a built-in operator, or a LAMBDA of the parameter's
|
|
903
|
+
// arity must give. OpDefNode#match then reports each argument that gives no such operator, or one
|
|
904
|
+
// whose own parameters take operators. tla-ts lets an operator whose parameters it does not know
|
|
905
|
+
// take any argument.
|
|
232
906
|
const resolveApply = (
|
|
233
907
|
expression: Expression & { kind: "Apply" },
|
|
234
908
|
scope: LocalName | undefined,
|
|
235
909
|
unit: Unit,
|
|
236
910
|
) => {
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
911
|
+
const operator = expression.operator;
|
|
912
|
+
const callee = findBinding(operator, scope, unit);
|
|
913
|
+
const arity = getSymbolArity(expression, operator, scope, unit);
|
|
914
|
+
if (arity === undefined) return;
|
|
241
915
|
if (arity !== expression.args.length) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
916
|
+
unit.errors.push({
|
|
917
|
+
location: formatRange(
|
|
918
|
+
expression.argumentsRange ?? getNameRange(expression.location, operator),
|
|
919
|
+
),
|
|
920
|
+
message: `The operator ${operator} requires ${arity} arguments.`,
|
|
921
|
+
});
|
|
922
|
+
return;
|
|
245
923
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
924
|
+
const params = callee && (callee.kind === "Definition" ? callee.params : []);
|
|
925
|
+
if (params === undefined) {
|
|
926
|
+
expression.args.forEach((arg) => resolveArgument(arg, scope, unit));
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
const report = (range: SourceRange | undefined, message: string) => {
|
|
930
|
+
if (range) unit.errors.push({ location: formatRange(range), message });
|
|
931
|
+
};
|
|
932
|
+
expression.args
|
|
933
|
+
.map((arg, index) => {
|
|
934
|
+
const expected = params[index] ?? 0;
|
|
935
|
+
// A prefix operator argument such as SUBSET, which tla-ts reads as a LAMBDA, has no range.
|
|
936
|
+
const range = arg.location.range;
|
|
937
|
+
if (expected === 0) {
|
|
938
|
+
if (arg.kind === "Lambda" && range) {
|
|
939
|
+
report(range, "LAMBDA expression used where an expression is required.");
|
|
940
|
+
} else if (arg.kind === "OperatorReference") {
|
|
941
|
+
// SANY fails here with an internal error, so tla-ts words it as it does for a name.
|
|
942
|
+
const argArity = getSymbolArity(arg, arg.operator, scope, unit);
|
|
943
|
+
if (argArity !== undefined) {
|
|
944
|
+
report(range, `The operator ${arg.operator} requires ${argArity} arguments.`);
|
|
945
|
+
}
|
|
946
|
+
} else resolveExpression(arg, scope, unit);
|
|
947
|
+
return true;
|
|
948
|
+
}
|
|
949
|
+
// A name that selects a subexpression gives an operator of the names it leaves unbound.
|
|
950
|
+
if (arg.kind === "Lambda") {
|
|
951
|
+
resolveExpression(arg, scope, unit);
|
|
952
|
+
const isMatched = arg.params.length === expected;
|
|
953
|
+
if (!isMatched && selections.has(arg)) {
|
|
954
|
+
report(
|
|
955
|
+
range,
|
|
956
|
+
`Expected arity ${expected} but found operator of arity ${arg.params.length}.`,
|
|
957
|
+
);
|
|
958
|
+
} else if (!isMatched && range) {
|
|
959
|
+
report(
|
|
960
|
+
expression.selectorRanges?.at(-1) ?? getNameRange(expression.location, operator),
|
|
961
|
+
`Lambda expression with arity ${arg.params.length} used as argument ${index + 1} of operator \`${operator}', \nbut an operator of arity ${expected} is required.`,
|
|
962
|
+
);
|
|
963
|
+
}
|
|
964
|
+
return isMatched;
|
|
965
|
+
}
|
|
966
|
+
if (arg.kind === "Subexpression") {
|
|
967
|
+
resolveExpression(arg, scope, unit);
|
|
968
|
+
return true;
|
|
969
|
+
}
|
|
970
|
+
if (arg.kind !== "Identifier" && arg.kind !== "OperatorReference") {
|
|
971
|
+
report(
|
|
972
|
+
range,
|
|
973
|
+
`An expression appears as argument number ${index + 1} (counting from 1) to operator '${operator}', in a position an operator is required.`,
|
|
974
|
+
);
|
|
975
|
+
return false;
|
|
976
|
+
}
|
|
977
|
+
const name = arg.kind === "Identifier" ? arg.name : arg.operator;
|
|
978
|
+
const argParams = findBinding(name, scope, unit)?.params ?? [];
|
|
979
|
+
const argArity = getSymbolArity(arg, name, scope, unit);
|
|
980
|
+
if (arg.kind === "Identifier" && argArity !== undefined && argArity !== expected) {
|
|
981
|
+
report(
|
|
982
|
+
getNameRange(arg.location, name),
|
|
983
|
+
`Expected arity ${expected} but found operator of arity ${argArity}.`,
|
|
984
|
+
);
|
|
985
|
+
}
|
|
986
|
+
return argArity === expected && argParams.every((param) => param === 0);
|
|
987
|
+
})
|
|
988
|
+
.forEach((isMatched, index) => {
|
|
989
|
+
if (!isMatched) {
|
|
990
|
+
report(
|
|
991
|
+
expression.location.range,
|
|
992
|
+
`Argument number ${index + 1} to operator '${operator}' \nshould be a ${params[index]}-parameter operator.`,
|
|
993
|
+
);
|
|
994
|
+
}
|
|
995
|
+
});
|
|
253
996
|
};
|
|
254
997
|
|
|
255
998
|
// An argument may name an operator that takes arguments of its own.
|
|
256
999
|
const resolveArgument = (arg: Expression, scope: LocalName | undefined, unit: Unit) => {
|
|
257
|
-
if (arg.kind === "Identifier")
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
1000
|
+
if (arg.kind === "Identifier") getSymbolArity(arg, arg.name, scope, unit);
|
|
1001
|
+
else resolveExpression(arg, scope, unit);
|
|
1002
|
+
};
|
|
1003
|
+
|
|
1004
|
+
// Reports the error of a name that selects nothing, after the arguments SANY analyzes before it.
|
|
1005
|
+
const isSelectionReported = (expression: Expression, scope: LocalName | undefined, unit: Unit) => {
|
|
1006
|
+
const failed = selectionErrors.get(expression);
|
|
1007
|
+
if (!failed) return false;
|
|
1008
|
+
failed.args.forEach((arg) => resolveArgument(arg, scope, unit));
|
|
1009
|
+
unit.errors.push(failed.error);
|
|
1010
|
+
return true;
|
|
261
1011
|
};
|
|
262
1012
|
|
|
263
1013
|
const resolveExpression = (
|
|
@@ -265,8 +1015,16 @@ const resolveExpression = (
|
|
|
265
1015
|
scope: LocalName | undefined,
|
|
266
1016
|
unit: Unit,
|
|
267
1017
|
): void => {
|
|
1018
|
+
if (selections.has(expression) && !unit.isGenerated) {
|
|
1019
|
+
resolveExpression(expression, scope, { ...unit, isGenerated: true });
|
|
1020
|
+
return;
|
|
1021
|
+
}
|
|
268
1022
|
const resolveAll = (expressions: Expression[]) =>
|
|
269
1023
|
expressions.forEach((child) => resolveExpression(child, scope, unit));
|
|
1024
|
+
const bindLabelParams = (names: string[]) => ({
|
|
1025
|
+
...unit,
|
|
1026
|
+
labelParams: [...unit.labelParams, ...names],
|
|
1027
|
+
});
|
|
270
1028
|
switch (expression.kind) {
|
|
271
1029
|
case "Number":
|
|
272
1030
|
case "Decimal":
|
|
@@ -274,11 +1032,13 @@ const resolveExpression = (
|
|
|
274
1032
|
case "ExceptAt":
|
|
275
1033
|
return;
|
|
276
1034
|
case "Identifier": {
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
1035
|
+
const name = expression.name;
|
|
1036
|
+
const arity = getSymbolArity(expression, name, scope, unit);
|
|
1037
|
+
if (arity !== undefined && arity > 0) {
|
|
1038
|
+
unit.errors.push({
|
|
1039
|
+
location: formatRange(getNameRange(expression.location, name)),
|
|
1040
|
+
message: `The operator ${name} requires ${arity} arguments.`,
|
|
1041
|
+
});
|
|
282
1042
|
}
|
|
283
1043
|
return;
|
|
284
1044
|
}
|
|
@@ -289,7 +1049,11 @@ const resolveExpression = (
|
|
|
289
1049
|
resolveApply(expression, scope, unit);
|
|
290
1050
|
return;
|
|
291
1051
|
case "Lambda":
|
|
292
|
-
resolveExpression(
|
|
1052
|
+
resolveExpression(
|
|
1053
|
+
expression.body,
|
|
1054
|
+
bindNames(scope, expression.params, expression.paramRanges, unit),
|
|
1055
|
+
bindLabelParams(expression.params),
|
|
1056
|
+
);
|
|
293
1057
|
return;
|
|
294
1058
|
case "And":
|
|
295
1059
|
case "Or":
|
|
@@ -312,24 +1076,32 @@ const resolveExpression = (
|
|
|
312
1076
|
case "Let":
|
|
313
1077
|
resolveExpression(
|
|
314
1078
|
expression.body,
|
|
315
|
-
resolveDefinitions(expression.definitions, scope, unit),
|
|
1079
|
+
resolveDefinitions(expression.definitions, scope, unit, expression.instances),
|
|
316
1080
|
unit,
|
|
317
1081
|
);
|
|
318
1082
|
return;
|
|
319
1083
|
case "Forall":
|
|
320
1084
|
case "Exists":
|
|
321
1085
|
case "FunctionConstruction":
|
|
322
|
-
resolveExpression(
|
|
1086
|
+
resolveExpression(
|
|
1087
|
+
expression.body,
|
|
1088
|
+
resolveBounds(expression.bounds, scope, unit),
|
|
1089
|
+
bindLabelParams(getBoundNames(expression.bounds)),
|
|
1090
|
+
);
|
|
323
1091
|
return;
|
|
324
1092
|
case "UnboundedQuantifier":
|
|
325
|
-
resolveExpression(
|
|
1093
|
+
resolveExpression(
|
|
1094
|
+
expression.body,
|
|
1095
|
+
bindNames(scope, expression.names, expression.nameRanges, unit),
|
|
1096
|
+
bindLabelParams(expression.names),
|
|
1097
|
+
);
|
|
326
1098
|
return;
|
|
327
1099
|
case "Choose":
|
|
328
1100
|
if (expression.set) resolveExpression(expression.set, scope, unit);
|
|
329
1101
|
resolveExpression(
|
|
330
1102
|
expression.body,
|
|
331
|
-
bindNames(scope, getPatternNames(expression.pattern)),
|
|
332
|
-
|
|
1103
|
+
bindNames(scope, getPatternNames(expression.pattern), expression.pattern.ranges, unit),
|
|
1104
|
+
bindLabelParams(getPatternNames(expression.pattern)),
|
|
333
1105
|
);
|
|
334
1106
|
return;
|
|
335
1107
|
case "SetEnumeration":
|
|
@@ -337,31 +1109,40 @@ const resolveExpression = (
|
|
|
337
1109
|
resolveAll(expression.elements);
|
|
338
1110
|
return;
|
|
339
1111
|
case "SetFilter":
|
|
340
|
-
resolveExpression(
|
|
1112
|
+
resolveExpression(
|
|
1113
|
+
expression.predicate,
|
|
1114
|
+
resolveBounds([expression.bound], scope, unit),
|
|
1115
|
+
bindLabelParams(getPatternNames(expression.bound.pattern)),
|
|
1116
|
+
);
|
|
341
1117
|
return;
|
|
342
1118
|
case "SetMap":
|
|
343
|
-
resolveExpression(
|
|
1119
|
+
resolveExpression(
|
|
1120
|
+
expression.expression,
|
|
1121
|
+
resolveBounds(expression.bounds, scope, unit),
|
|
1122
|
+
bindLabelParams(getBoundNames(expression.bounds)),
|
|
1123
|
+
);
|
|
344
1124
|
return;
|
|
345
1125
|
case "FunctionSet":
|
|
346
1126
|
resolveAll([expression.domain, expression.range]);
|
|
347
1127
|
return;
|
|
348
1128
|
case "Record":
|
|
349
1129
|
case "RecordSet":
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
1130
|
+
expression.fields.forEach((field, index) => {
|
|
1131
|
+
if (expression.fields.slice(0, index).some((earlier) => earlier.name === field.name)) {
|
|
1132
|
+
unit.errors.push({
|
|
1133
|
+
location: formatRange(field.location.range ?? getNameRange(field.location, field.name)),
|
|
1134
|
+
message: "Non-unique fields in constructor.",
|
|
1135
|
+
});
|
|
1136
|
+
}
|
|
1137
|
+
resolveExpression(field.value, scope, unit);
|
|
1138
|
+
});
|
|
356
1139
|
return;
|
|
357
1140
|
case "Except":
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
]),
|
|
364
|
-
]);
|
|
1141
|
+
resolveExpression(expression.function, scope, unit);
|
|
1142
|
+
expression.updates.forEach((update) => {
|
|
1143
|
+
resolveAll(update.path.flatMap((step) => (step.kind === "Index" ? step.args : [])));
|
|
1144
|
+
resolveExpression(update.value, scope, { ...unit, isInExcept: true });
|
|
1145
|
+
});
|
|
365
1146
|
return;
|
|
366
1147
|
case "FunctionApplication":
|
|
367
1148
|
resolveAll([expression.function, ...expression.args]);
|
|
@@ -381,34 +1162,66 @@ const resolveExpression = (
|
|
|
381
1162
|
resolveAll([expression.subscript, expression.action]);
|
|
382
1163
|
return;
|
|
383
1164
|
case "Label":
|
|
384
|
-
|
|
1165
|
+
if (isLabelReported(expression, unit)) return;
|
|
1166
|
+
resolveExpression(expression.body, scope, { ...unit, labelParams: [], labels: new Set() });
|
|
1167
|
+
checkLabel(expression, scope, unit);
|
|
385
1168
|
return;
|
|
386
|
-
case "Subexpression":
|
|
1169
|
+
case "Subexpression": {
|
|
1170
|
+
if (isSelectionReported(expression, scope, unit)) return;
|
|
387
1171
|
expression.selectors
|
|
388
1172
|
.flatMap((selector) => ("args" in selector ? selector.args : []))
|
|
389
1173
|
.forEach((arg) => resolveArgument(arg, scope, unit));
|
|
1174
|
+
checkReference(expression, scope, unit, false);
|
|
1175
|
+
}
|
|
390
1176
|
}
|
|
391
1177
|
};
|
|
392
1178
|
|
|
1179
|
+
// Generator.generateUseOrHide looks up a fact that is a name as it looks up a name in an expression,
|
|
1180
|
+
// except that the name may select an ASSUME/PROVE or a NEW clause or name a theorem or assumption,
|
|
1181
|
+
// and analyzes any other fact as an expression.
|
|
1182
|
+
const checkFacts = (facts: Fact[], scope: LocalName | undefined, unit: Unit) =>
|
|
1183
|
+
facts.forEach((fact) => {
|
|
1184
|
+
if (fact.kind === "ModuleFact" || checkReference(fact, scope, unit, true)) return;
|
|
1185
|
+
const name =
|
|
1186
|
+
fact.kind === "Identifier" ? fact.name : fact.kind === "Apply" ? fact.operator : "";
|
|
1187
|
+
if (!unit.facts.has(name)) resolveExpression(fact, scope, unit);
|
|
1188
|
+
});
|
|
1189
|
+
|
|
393
1190
|
const resolveNewSymbol = (symbol: NewSymbol, scope: LocalName | undefined, unit: Unit) => {
|
|
394
1191
|
if (symbol.set) resolveExpression(symbol.set, scope, unit);
|
|
395
|
-
return
|
|
1192
|
+
return bindName(
|
|
1193
|
+
scope,
|
|
1194
|
+
symbol.name,
|
|
1195
|
+
{ kind: `New${symbol.level}`, arity: symbol.arity, range: symbol.nameRange },
|
|
1196
|
+
unit,
|
|
1197
|
+
);
|
|
396
1198
|
};
|
|
397
1199
|
|
|
398
|
-
// The NEW symbols of an ASSUME stay in scope for its conclusion and the proof that follows.
|
|
1200
|
+
// The NEW symbols of an ASSUME stay in scope for its conclusion and the proof that follows. SANY
|
|
1201
|
+
// forbids a label in the scope of a NEW in a nested ASSUME.
|
|
399
1202
|
const resolveAssumeProve = (
|
|
400
1203
|
assumeProve: AssumeProve,
|
|
401
1204
|
scope: LocalName | undefined,
|
|
402
1205
|
unit: Unit,
|
|
1206
|
+
isNested = false,
|
|
403
1207
|
): LocalName | undefined => {
|
|
404
|
-
const assumed = assumeProve.assumptions.reduce(
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
1208
|
+
const assumed = assumeProve.assumptions.reduce(
|
|
1209
|
+
(current, assumption) => {
|
|
1210
|
+
if (assumption.kind === "NewSymbol") {
|
|
1211
|
+
return {
|
|
1212
|
+
scope: resolveNewSymbol(assumption, current.scope, current.unit),
|
|
1213
|
+
unit: isNested ? { ...current.unit, isLabelForbidden: true } : current.unit,
|
|
1214
|
+
};
|
|
1215
|
+
}
|
|
1216
|
+
if (assumption.kind === "AssumeProve") {
|
|
1217
|
+
resolveAssumeProve(assumption, current.scope, current.unit, true);
|
|
1218
|
+
} else resolveExpression(assumption, current.scope, current.unit);
|
|
1219
|
+
return current;
|
|
1220
|
+
},
|
|
1221
|
+
{ scope, unit },
|
|
1222
|
+
);
|
|
1223
|
+
resolveExpression(assumeProve.conclusion, assumed.scope, assumed.unit);
|
|
1224
|
+
return assumed.scope;
|
|
412
1225
|
};
|
|
413
1226
|
|
|
414
1227
|
const resolveStatement = (
|
|
@@ -423,44 +1236,92 @@ const resolveStatement = (
|
|
|
423
1236
|
|
|
424
1237
|
// SANY resolves the expressions of a proof, while its facts and DEF clauses may also name
|
|
425
1238
|
// theorems, assumptions, and modules.
|
|
426
|
-
const resolveProof = (proof: Proof | undefined, scope: LocalName | undefined,
|
|
1239
|
+
const resolveProof = (proof: Proof | undefined, scope: LocalName | undefined, outer: Unit) => {
|
|
1240
|
+
const unit = outer.hasLabelScope ? { ...outer, hasLabelScope: false } : outer;
|
|
1241
|
+
if (proof?.kind === "By") checkFacts(proof.facts, scope, unit);
|
|
427
1242
|
if (proof?.kind !== "Steps") return;
|
|
428
1243
|
proof.steps.reduce((current, step) => resolveStep(step, current, unit), scope);
|
|
429
1244
|
};
|
|
430
1245
|
|
|
1246
|
+
// A step binds the name of the INSTANCE after the names it adds.
|
|
1247
|
+
const resolveStepInstance = (instance: Instance, scope: LocalName | undefined, unit: Unit) => {
|
|
1248
|
+
const instanced = resolveInstance(instance, scope, unit);
|
|
1249
|
+
return instance.name === undefined
|
|
1250
|
+
? instanced
|
|
1251
|
+
: bindName(
|
|
1252
|
+
instanced,
|
|
1253
|
+
instance.name,
|
|
1254
|
+
{ kind: "Instance", arity: instance.params.length, range: instance.location.range },
|
|
1255
|
+
unit,
|
|
1256
|
+
);
|
|
1257
|
+
};
|
|
1258
|
+
|
|
431
1259
|
const resolveStep = (
|
|
432
1260
|
step: ProofStep,
|
|
433
1261
|
scope: LocalName | undefined,
|
|
434
1262
|
unit: Unit,
|
|
435
1263
|
): LocalName | undefined => {
|
|
436
1264
|
const body = step.body;
|
|
1265
|
+
const stepUnit =
|
|
1266
|
+
step.name === undefined
|
|
1267
|
+
? unit
|
|
1268
|
+
: { ...unit, hasLabelScope: true, labelParams: [], labels: new Set<string>() };
|
|
437
1269
|
switch (body.kind) {
|
|
1270
|
+
// The NEW symbols of a SUFFICES ASSUME are in scope after its proof, and those of any other
|
|
1271
|
+
// ASSUME in its proof.
|
|
438
1272
|
case "Assert": {
|
|
439
|
-
const assumed = resolveStatement(body.statement, scope,
|
|
440
|
-
|
|
441
|
-
|
|
1273
|
+
const assumed = resolveStatement(body.statement, scope, stepUnit);
|
|
1274
|
+
const proofScope = body.isSuffices ? scope : assumed;
|
|
1275
|
+
const name = step.name;
|
|
1276
|
+
if (name === undefined) {
|
|
1277
|
+
resolveProof(step.proof, proofScope, unit);
|
|
1278
|
+
return body.isSuffices ? assumed : scope;
|
|
1279
|
+
}
|
|
1280
|
+
const goal = { statement: body.statement, isSuffices: body.isSuffices, isInProof: true };
|
|
1281
|
+
resolveProof(step.proof, { name, arity: 0, kind: "Theorem", parent: proofScope, goal }, unit);
|
|
1282
|
+
goal.isInProof = false;
|
|
1283
|
+
return { name, arity: 0, kind: "Theorem", parent: body.isSuffices ? assumed : scope, goal };
|
|
442
1284
|
}
|
|
1285
|
+
case "Use":
|
|
1286
|
+
case "Hide":
|
|
1287
|
+
checkFacts(body.facts, scope, unit);
|
|
1288
|
+
return scope;
|
|
1289
|
+
case "Instance":
|
|
1290
|
+
return resolveStepInstance(body, scope, stepUnit);
|
|
443
1291
|
case "Define":
|
|
444
|
-
return
|
|
445
|
-
|
|
446
|
-
definition.kind === "Instance"
|
|
447
|
-
|
|
1292
|
+
return body.definitions.reduce(
|
|
1293
|
+
(current, definition) =>
|
|
1294
|
+
definition.kind === "Instance"
|
|
1295
|
+
? resolveStepInstance(definition, current, stepUnit)
|
|
1296
|
+
: resolveDefinitions([definition], current, unit),
|
|
448
1297
|
scope,
|
|
449
|
-
unit,
|
|
450
1298
|
);
|
|
451
1299
|
case "Have":
|
|
452
1300
|
case "Case":
|
|
453
|
-
resolveExpression(body.expression, scope,
|
|
1301
|
+
resolveExpression(body.expression, scope, stepUnit);
|
|
454
1302
|
resolveProof(step.proof, scope, unit);
|
|
455
1303
|
return scope;
|
|
456
1304
|
case "Witness":
|
|
457
|
-
body.expressions.forEach((expression) => resolveExpression(expression, scope,
|
|
1305
|
+
body.expressions.forEach((expression) => resolveExpression(expression, scope, stepUnit));
|
|
458
1306
|
return scope;
|
|
459
1307
|
case "Take":
|
|
460
|
-
return bindNames(
|
|
1308
|
+
return bindNames(
|
|
1309
|
+
resolveBounds(body.bounds, scope, stepUnit),
|
|
1310
|
+
body.names,
|
|
1311
|
+
body.nameRanges,
|
|
1312
|
+
stepUnit,
|
|
1313
|
+
);
|
|
461
1314
|
case "Pick": {
|
|
462
|
-
const picked = bindNames(
|
|
463
|
-
|
|
1315
|
+
const picked = bindNames(
|
|
1316
|
+
resolveBounds(body.bounds, scope, stepUnit),
|
|
1317
|
+
body.names,
|
|
1318
|
+
body.nameRanges,
|
|
1319
|
+
stepUnit,
|
|
1320
|
+
);
|
|
1321
|
+
resolveExpression(body.body, picked, {
|
|
1322
|
+
...stepUnit,
|
|
1323
|
+
labelParams: [...stepUnit.labelParams, ...getBoundNames(body.bounds), ...body.names],
|
|
1324
|
+
});
|
|
464
1325
|
resolveProof(step.proof, scope, unit);
|
|
465
1326
|
return picked;
|
|
466
1327
|
}
|
|
@@ -470,324 +1331,421 @@ const resolveStep = (
|
|
|
470
1331
|
}
|
|
471
1332
|
};
|
|
472
1333
|
|
|
1334
|
+
// SANY binds the name of a theorem after its statement and before its proof.
|
|
473
1335
|
const resolveTheorem = (theorem: Theorem, unit: Unit) => {
|
|
474
1336
|
const isDefinition = theorem.name !== undefined && theorem.statement.kind !== "AssumeProve";
|
|
475
1337
|
const scope = isDefinition ? undefined : resolveStatement(theorem.statement, undefined, unit);
|
|
1338
|
+
if (theorem.name !== undefined) {
|
|
1339
|
+
bindModuleName(
|
|
1340
|
+
theorem.name,
|
|
1341
|
+
{ kind: "Theorem", arity: 0, range: theorem.location.range },
|
|
1342
|
+
unit,
|
|
1343
|
+
);
|
|
1344
|
+
}
|
|
1345
|
+
const goal = unit.goals.get(theorem.name ?? "");
|
|
1346
|
+
if (goal) goal.isInProof = true;
|
|
476
1347
|
resolveProof(theorem.proof, scope, unit);
|
|
1348
|
+
if (goal) goal.isInProof = false;
|
|
477
1349
|
};
|
|
478
1350
|
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
1351
|
+
// The declarations of the module an INSTANCE instantiates, the names the INSTANCE adds, and the
|
|
1352
|
+
// module that instantiates it.
|
|
1353
|
+
export interface InstancedModule {
|
|
1354
|
+
declarations: Declaration[];
|
|
1355
|
+
symbols: Symbols;
|
|
1356
|
+
instancer: string;
|
|
484
1357
|
}
|
|
485
1358
|
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
1359
|
+
// What SANY reports at a location as it links a module, such as a conflict between the modules it
|
|
1360
|
+
// extends.
|
|
1361
|
+
export interface ModuleReport {
|
|
489
1362
|
location: SourceLocation;
|
|
490
|
-
|
|
1363
|
+
errors: SanyError[];
|
|
1364
|
+
warnings: SanyError[];
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1367
|
+
// bound holds the names in scope where the module starts: the built-in operators, the names of the
|
|
1368
|
+
// module that encloses it, and those of the modules it extends. instanced holds the names each
|
|
1369
|
+
// INSTANCE adds, getInstance what an INSTANCE in a LET or a proof instantiates, facts the theorems
|
|
1370
|
+
// and assumptions the modules it extends or instantiates export, and warnings the warnings SANY
|
|
1371
|
+
// reports for the spec.
|
|
1372
|
+
export interface ModuleUnit {
|
|
1373
|
+
imported: Symbols;
|
|
1374
|
+
getInstance: (instance: Instance) => InstancedModule;
|
|
1375
|
+
bound: Symbols;
|
|
1376
|
+
definitions: Map<string, Definition>;
|
|
1377
|
+
getKey: (definition: Definition) => string;
|
|
1378
|
+
instanced: Map<Instance, Symbols>;
|
|
1379
|
+
facts: Set<string>;
|
|
1380
|
+
reports: ModuleReport[];
|
|
1381
|
+
warnings: SanyError[];
|
|
491
1382
|
}
|
|
492
1383
|
|
|
493
|
-
|
|
1384
|
+
export const MODULE_START: SourceLocation = { line: 0, column: 0 };
|
|
494
1385
|
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
1386
|
+
// A named theorem or assumption is linked as a definition too.
|
|
1387
|
+
export const isTheoremDefinition = (module: Module, definition: Definition) =>
|
|
1388
|
+
[...module.theorems, ...module.assumptions].some(
|
|
1389
|
+
(item) =>
|
|
1390
|
+
item.name === definition.name &&
|
|
1391
|
+
item.location.line === definition.location.line &&
|
|
1392
|
+
item.location.column === definition.location.column,
|
|
498
1393
|
);
|
|
499
1394
|
|
|
500
|
-
const
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
) =>
|
|
506
|
-
|
|
507
|
-
if (lookupLocal(parent, name) || isDefined(name)) throw createRedefinitionError(name, location);
|
|
508
|
-
return { name, arity: 0, parent };
|
|
509
|
-
}, scope);
|
|
510
|
-
|
|
511
|
-
const checkBoundNames = (
|
|
512
|
-
bounds: Bound[],
|
|
513
|
-
scope: LocalName | undefined,
|
|
514
|
-
isDefined: IsDefined,
|
|
515
|
-
location: SourceLocation,
|
|
516
|
-
) => {
|
|
517
|
-
bounds.forEach((bound) => checkNames(bound.set, scope, isDefined));
|
|
518
|
-
return bounds.reduce(
|
|
519
|
-
(current, bound) => bindUnique(current, getPatternNames(bound.pattern), isDefined, location),
|
|
520
|
-
scope,
|
|
1395
|
+
const createSymbols = (module: Module, imported: Symbols) => {
|
|
1396
|
+
const symbols = new Map([...CORE_SYMBOLS, ...imported]);
|
|
1397
|
+
module.constants.forEach((constant) =>
|
|
1398
|
+
symbols.set(constant.name, { key: constant.name, arity: constant.arity, kind: "Constant" }),
|
|
1399
|
+
);
|
|
1400
|
+
module.variables.forEach((variable) =>
|
|
1401
|
+
symbols.set(variable.name, { key: variable.name, arity: 0, kind: "Variable" }),
|
|
521
1402
|
);
|
|
1403
|
+
return symbols;
|
|
522
1404
|
};
|
|
523
1405
|
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
1406
|
+
// Generator.processSubst analyzes the explicit substitutions in the order they appear, then the
|
|
1407
|
+
// implicit ones, and then reports each declaration left without one. The parameters of the INSTANCE
|
|
1408
|
+
// are in scope.
|
|
1409
|
+
const checkSubstitutions = (
|
|
1410
|
+
instance: Instance,
|
|
1411
|
+
declarations: Declaration[],
|
|
1412
|
+
instancer: string,
|
|
1413
|
+
outer: LocalName | undefined,
|
|
1414
|
+
unit: Unit,
|
|
528
1415
|
) => {
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
checkNames(definition.body, bound, isDefined);
|
|
533
|
-
return;
|
|
534
|
-
}
|
|
535
|
-
const params = bindUnique(
|
|
536
|
-
scope,
|
|
537
|
-
definition.params.map((param) => param.name),
|
|
538
|
-
isDefined,
|
|
539
|
-
definition.location,
|
|
1416
|
+
const scope = instance.params.reduce<LocalName | undefined>(
|
|
1417
|
+
(parent, param) => ({ name: param.name, arity: param.arity, kind: "Parameter", parent }),
|
|
1418
|
+
outer,
|
|
540
1419
|
);
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
const checkNames = (
|
|
545
|
-
expression: Expression,
|
|
546
|
-
scope: LocalName | undefined,
|
|
547
|
-
isDefined: IsDefined,
|
|
548
|
-
): void => {
|
|
549
|
-
const location = expression.location;
|
|
550
|
-
switch (expression.kind) {
|
|
551
|
-
case "Lambda":
|
|
552
|
-
checkNames(
|
|
553
|
-
expression.body,
|
|
554
|
-
bindUnique(scope, expression.params, isDefined, location),
|
|
555
|
-
isDefined,
|
|
556
|
-
);
|
|
557
|
-
return;
|
|
558
|
-
case "Let": {
|
|
559
|
-
const recursive = expression.definitions
|
|
560
|
-
.filter((definition) => definition.isRecursive)
|
|
561
|
-
.map((definition) => definition.name);
|
|
562
|
-
const inner = expression.definitions.reduce((current, definition) => {
|
|
563
|
-
if (lookupLocal(current, definition.name) || isDefined(definition.name)) {
|
|
564
|
-
throw createRedefinitionError(definition.name, definition.location);
|
|
565
|
-
}
|
|
566
|
-
checkDefinitionNames(definition, bindNames(current, recursive), isDefined);
|
|
567
|
-
return bindNames(current, [definition.name]);
|
|
568
|
-
}, scope);
|
|
569
|
-
checkNames(
|
|
570
|
-
expression.body,
|
|
571
|
-
bindUnique(
|
|
572
|
-
inner,
|
|
573
|
-
(expression.instances ?? []).flatMap((instance) =>
|
|
574
|
-
instance.name === undefined ? [] : [instance.name],
|
|
575
|
-
),
|
|
576
|
-
isDefined,
|
|
577
|
-
location,
|
|
578
|
-
),
|
|
579
|
-
isDefined,
|
|
580
|
-
);
|
|
1420
|
+
const resolveSubstitute = (declaration: Declaration, expression: Expression) => {
|
|
1421
|
+
if (declaration.arity === 0 || expression.kind !== "Identifier") {
|
|
1422
|
+
resolveExpression(expression, scope, unit);
|
|
581
1423
|
return;
|
|
582
1424
|
}
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
expression.body,
|
|
588
|
-
checkBoundNames(expression.bounds, scope, isDefined, location),
|
|
589
|
-
isDefined,
|
|
590
|
-
);
|
|
591
|
-
return;
|
|
592
|
-
case "SetFilter":
|
|
593
|
-
checkNames(
|
|
594
|
-
expression.predicate,
|
|
595
|
-
checkBoundNames([expression.bound], scope, isDefined, location),
|
|
596
|
-
isDefined,
|
|
597
|
-
);
|
|
598
|
-
return;
|
|
599
|
-
case "SetMap":
|
|
600
|
-
checkNames(
|
|
601
|
-
expression.expression,
|
|
602
|
-
checkBoundNames(expression.bounds, scope, isDefined, location),
|
|
603
|
-
isDefined,
|
|
604
|
-
);
|
|
605
|
-
return;
|
|
606
|
-
case "UnboundedQuantifier":
|
|
607
|
-
checkNames(
|
|
608
|
-
expression.body,
|
|
609
|
-
bindUnique(scope, expression.names, isDefined, location),
|
|
610
|
-
isDefined,
|
|
611
|
-
);
|
|
612
|
-
return;
|
|
613
|
-
case "Choose":
|
|
614
|
-
if (expression.set) checkNames(expression.set, scope, isDefined);
|
|
615
|
-
checkNames(
|
|
616
|
-
expression.body,
|
|
617
|
-
bindUnique(scope, getPatternNames(expression.pattern), isDefined, location),
|
|
618
|
-
isDefined,
|
|
1425
|
+
const arity = getSymbolArity(expression, expression.name, scope, unit);
|
|
1426
|
+
if (arity !== undefined && arity !== declaration.arity) {
|
|
1427
|
+
throw new TlaSyntaxError(
|
|
1428
|
+
`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}`,
|
|
619
1429
|
);
|
|
620
|
-
|
|
621
|
-
default:
|
|
622
|
-
getChildren(expression).forEach((child) => checkNames(child, scope, isDefined));
|
|
623
|
-
}
|
|
624
|
-
};
|
|
625
|
-
|
|
626
|
-
const getModuleNames = (module: Module) => [
|
|
627
|
-
...module.definitions.map((definition): ModuleName => ({
|
|
628
|
-
name: definition.name,
|
|
629
|
-
kind: "Definition",
|
|
630
|
-
location: definition.location,
|
|
631
|
-
isRecursive: definition.isRecursive,
|
|
632
|
-
})),
|
|
633
|
-
...module.instances.flatMap((instance): ModuleName[] =>
|
|
634
|
-
instance.name === undefined
|
|
635
|
-
? []
|
|
636
|
-
: [
|
|
637
|
-
{
|
|
638
|
-
name: instance.name,
|
|
639
|
-
kind: "Definition",
|
|
640
|
-
location: instance.location,
|
|
641
|
-
isRecursive: false,
|
|
642
|
-
},
|
|
643
|
-
],
|
|
644
|
-
),
|
|
645
|
-
...module.constants.map((constant): ModuleName => ({
|
|
646
|
-
name: constant.name,
|
|
647
|
-
kind: "Constant",
|
|
648
|
-
location: constant.location,
|
|
649
|
-
isRecursive: false,
|
|
650
|
-
})),
|
|
651
|
-
...module.variables.map((variable): ModuleName => ({
|
|
652
|
-
name: variable.name,
|
|
653
|
-
kind: "Variable",
|
|
654
|
-
location: variable.location,
|
|
655
|
-
isRecursive: false,
|
|
656
|
-
})),
|
|
657
|
-
];
|
|
658
|
-
|
|
659
|
-
export const checkRedefinitions = (module: Module, unit: ModuleUnit) => {
|
|
660
|
-
const byName = new Map<string, ModuleName[]>();
|
|
661
|
-
getModuleNames(module)
|
|
662
|
-
.sort((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
|
|
663
|
-
.forEach((entry) => byName.set(entry.name, [...(byName.get(entry.name) ?? []), entry]));
|
|
664
|
-
const isImportedBefore = (name: string, location: SourceLocation) => {
|
|
665
|
-
if (!unit.imported.has(name) && !CORE_SYMBOLS.has(name)) return false;
|
|
666
|
-
const importedAt = unit.importedAt.get(name);
|
|
667
|
-
return importedAt === undefined || isBefore(importedAt, location);
|
|
1430
|
+
}
|
|
668
1431
|
};
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
) || isImportedBefore(name, location);
|
|
691
|
-
module.definitions.forEach((definition) =>
|
|
692
|
-
checkDefinitionNames(definition, undefined, getIsDefined(definition.location)),
|
|
693
|
-
);
|
|
694
|
-
module.assumptions.forEach((assumption) =>
|
|
695
|
-
checkNames(assumption.expression, undefined, getIsDefined(assumption.location)),
|
|
696
|
-
);
|
|
697
|
-
module.instances.forEach((instance) => {
|
|
698
|
-
const isDefined = getIsDefined(instance.location);
|
|
699
|
-
const params = bindUnique(
|
|
700
|
-
undefined,
|
|
701
|
-
instance.params.map((param) => param.name),
|
|
702
|
-
isDefined,
|
|
703
|
-
instance.location,
|
|
704
|
-
);
|
|
705
|
-
instance.substitutions.forEach((substitution) =>
|
|
706
|
-
checkNames(substitution.expression, params, isDefined),
|
|
707
|
-
);
|
|
1432
|
+
const substitutions = new Map<string, Expression>();
|
|
1433
|
+
instance.substitutions.forEach((substitution) => {
|
|
1434
|
+
const name = substitution.name;
|
|
1435
|
+
const declaration = declarations.find((item) => item.name === name);
|
|
1436
|
+
if (!declaration) {
|
|
1437
|
+
unit.errors.push({
|
|
1438
|
+
location: formatRange(substitution.nameRange),
|
|
1439
|
+
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.)`,
|
|
1440
|
+
});
|
|
1441
|
+
return;
|
|
1442
|
+
}
|
|
1443
|
+
const expression = substitution.expression;
|
|
1444
|
+
resolveSubstitute(declaration, expression);
|
|
1445
|
+
const range = expression.location.range;
|
|
1446
|
+
if (!substitutions.has(name)) substitutions.set(name, expression);
|
|
1447
|
+
else if (range) {
|
|
1448
|
+
unit.errors.push({
|
|
1449
|
+
location: formatRange(range),
|
|
1450
|
+
message: `Multiple substitutions for symbol '${name}' in substitution.`,
|
|
1451
|
+
});
|
|
1452
|
+
}
|
|
708
1453
|
});
|
|
1454
|
+
const implicit = declarations.filter((declaration) => !substitutions.has(declaration.name));
|
|
1455
|
+
const isMissing = (declaration: Declaration) =>
|
|
1456
|
+
!lookupLocal(scope, declaration.name) && !findSymbol(declaration.name, unit);
|
|
1457
|
+
implicit
|
|
1458
|
+
.filter((declaration) => !isMissing(declaration))
|
|
1459
|
+
.forEach((declaration) => {
|
|
1460
|
+
const expression: Expression = {
|
|
1461
|
+
kind: "Identifier",
|
|
1462
|
+
name: declaration.name,
|
|
1463
|
+
location: instance.location,
|
|
1464
|
+
};
|
|
1465
|
+
resolveSubstitute(declaration, expression);
|
|
1466
|
+
substitutions.set(declaration.name, expression);
|
|
1467
|
+
});
|
|
1468
|
+
implicit.filter(isMissing).forEach((declaration) => {
|
|
1469
|
+
const range = instance.location.range;
|
|
1470
|
+
const declared = declaration.location.range;
|
|
1471
|
+
if (range && declared) {
|
|
1472
|
+
unit.errors.push({
|
|
1473
|
+
location: formatRange(range),
|
|
1474
|
+
message: `Substitution missing for symbol ${declaration.name} declared at ${formatRange(declared)} \nand instantiated in module ${instancer}.`,
|
|
1475
|
+
});
|
|
1476
|
+
}
|
|
1477
|
+
});
|
|
1478
|
+
return substitutions;
|
|
709
1479
|
};
|
|
710
1480
|
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
);
|
|
716
|
-
|
|
717
|
-
|
|
1481
|
+
// Generator.processSubst analyzes an INSTANCE in a LET or a proof, which then binds the names the
|
|
1482
|
+
// INSTANCE adds.
|
|
1483
|
+
const resolveInstance = (instance: Instance, scope: LocalName | undefined, unit: Unit) => {
|
|
1484
|
+
const instanced = unit.getInstance(instance);
|
|
1485
|
+
checkSubstitutions(instance, instanced.declarations, instanced.instancer, scope, unit);
|
|
1486
|
+
return [...instanced.symbols].reduce(
|
|
1487
|
+
(parent, [name, symbol]) => bindName(parent, name, symbol, unit),
|
|
1488
|
+
scope,
|
|
718
1489
|
);
|
|
719
|
-
return symbols;
|
|
720
1490
|
};
|
|
721
1491
|
|
|
1492
|
+
// tla-ts reports what SANY goes on after with an INSTANCE of the module.
|
|
722
1493
|
export const resolveSubstitutions = (
|
|
723
1494
|
instance: Instance,
|
|
724
|
-
declarations:
|
|
1495
|
+
declarations: Declaration[],
|
|
725
1496
|
module: Module,
|
|
726
1497
|
unit: ModuleUnit,
|
|
727
1498
|
) => {
|
|
728
|
-
const
|
|
729
|
-
const
|
|
730
|
-
|
|
1499
|
+
const imported = createSymbols(module, unit.imported);
|
|
1500
|
+
const report: ModuleReport = { location: instance.location, errors: [], warnings: [] };
|
|
1501
|
+
unit.reports.push(report);
|
|
1502
|
+
return checkSubstitutions(instance, declarations, module.name, undefined, {
|
|
1503
|
+
imported,
|
|
1504
|
+
getInstance: unit.getInstance,
|
|
731
1505
|
definitions: unit.definitions,
|
|
732
1506
|
getKey: unit.getKey,
|
|
733
1507
|
location: instance.location,
|
|
734
1508
|
self: undefined,
|
|
735
1509
|
facts: new Set(),
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
1510
|
+
goals: new Map(),
|
|
1511
|
+
bindings: new Map(
|
|
1512
|
+
[...imported].map(([name, symbol]) => [name, { ...symbol, position: MODULE_START }]),
|
|
1513
|
+
),
|
|
1514
|
+
errors: report.errors,
|
|
1515
|
+
warnings: report.warnings,
|
|
1516
|
+
hasLabelScope: false,
|
|
1517
|
+
isLabelForbidden: false,
|
|
1518
|
+
isInExcept: false,
|
|
1519
|
+
labelParams: [],
|
|
1520
|
+
labels: new Set(),
|
|
1521
|
+
isGenerated: false,
|
|
745
1522
|
});
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
1523
|
+
};
|
|
1524
|
+
|
|
1525
|
+
// SANY reports the errors of every unit it checks, in the order of the units in the module, after
|
|
1526
|
+
// the warnings it reports for the spec. A unit reports to its lists what SANY goes on after, and
|
|
1527
|
+
// throws the error that stops it. An error tla-ts cannot word as SANY does yields to the errors the
|
|
1528
|
+
// unit reported before it. Returns the errors.
|
|
1529
|
+
export const collectSemanticErrors = (
|
|
1530
|
+
units: {
|
|
1531
|
+
location: SourceLocation;
|
|
1532
|
+
check: (reported: SanyError[], warned: SanyError[]) => void;
|
|
1533
|
+
}[],
|
|
1534
|
+
warnings: SanyError[],
|
|
1535
|
+
) => {
|
|
1536
|
+
const errors: SanyError[] = [];
|
|
1537
|
+
const add = (list: SanyError[], item: SanyError) => {
|
|
1538
|
+
if (
|
|
1539
|
+
list.every(
|
|
1540
|
+
(existing) => existing.location !== item.location || existing.message !== item.message,
|
|
1541
|
+
)
|
|
1542
|
+
) {
|
|
1543
|
+
list.push(item);
|
|
1544
|
+
}
|
|
1545
|
+
};
|
|
1546
|
+
units
|
|
1547
|
+
.toSorted((left, right) =>
|
|
1548
|
+
isBefore(left.location, right.location)
|
|
1549
|
+
? -1
|
|
1550
|
+
: isBefore(right.location, left.location)
|
|
1551
|
+
? 1
|
|
1552
|
+
: 0,
|
|
1553
|
+
)
|
|
1554
|
+
.forEach((unit) => {
|
|
1555
|
+
const reported: SanyError[] = [];
|
|
1556
|
+
const warned: SanyError[] = [];
|
|
1557
|
+
try {
|
|
1558
|
+
unit.check(reported, warned);
|
|
1559
|
+
} catch (error) {
|
|
1560
|
+
if (!(error instanceof TlaSyntaxError) || (!error.sany && reported.length === 0)) {
|
|
1561
|
+
throw error;
|
|
1562
|
+
}
|
|
1563
|
+
reported.push(...(error.sany?.errors ?? []));
|
|
765
1564
|
}
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
1565
|
+
reported.forEach((item) => add(errors, item));
|
|
1566
|
+
warned.forEach((item) => add(warnings, item));
|
|
1567
|
+
});
|
|
1568
|
+
return errors;
|
|
769
1569
|
};
|
|
770
1570
|
|
|
1571
|
+
// SANY binds the names of a module in the order of its units, and reports a RECURSIVE declaration
|
|
1572
|
+
// that no definition follows at the end. Returns the errors.
|
|
771
1573
|
export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
772
1574
|
const symbols = createSymbols(module, unit.imported);
|
|
773
|
-
const facts = new Set(
|
|
774
|
-
|
|
1575
|
+
const facts = new Set([
|
|
1576
|
+
...unit.facts,
|
|
1577
|
+
...[...module.theorems, ...module.assumptions].flatMap((fact) => fact.name ?? []),
|
|
1578
|
+
]);
|
|
1579
|
+
const goals = new Map(
|
|
1580
|
+
module.theorems.flatMap((theorem): [string, Goal][] =>
|
|
1581
|
+
theorem.name !== undefined && theorem.statement.kind === "AssumeProve"
|
|
1582
|
+
? [[theorem.name, { statement: theorem.statement, isSuffices: false, isInProof: false }]]
|
|
1583
|
+
: [],
|
|
1584
|
+
),
|
|
775
1585
|
);
|
|
776
|
-
const
|
|
1586
|
+
const bindings = new Map<string, ModuleBinding>(
|
|
1587
|
+
[...CORE_SYMBOLS, ...unit.bound].map(([name, symbol]) => [
|
|
1588
|
+
name,
|
|
1589
|
+
{ ...symbol, position: MODULE_START },
|
|
1590
|
+
]),
|
|
1591
|
+
);
|
|
1592
|
+
const recursive = new Set<Binding>();
|
|
1593
|
+
const declarations: { declaration: Declaration; binding: ModuleBinding | undefined }[] = [];
|
|
1594
|
+
const createUnit = (
|
|
1595
|
+
location: SourceLocation,
|
|
1596
|
+
self: Definition | undefined,
|
|
1597
|
+
errors: SanyError[],
|
|
1598
|
+
warnings: SanyError[],
|
|
1599
|
+
hasLabelScope: boolean,
|
|
1600
|
+
): Unit => ({
|
|
777
1601
|
imported: symbols,
|
|
1602
|
+
getInstance: unit.getInstance,
|
|
778
1603
|
definitions: unit.definitions,
|
|
779
1604
|
getKey: unit.getKey,
|
|
780
1605
|
location,
|
|
781
1606
|
self,
|
|
782
1607
|
facts,
|
|
1608
|
+
goals,
|
|
1609
|
+
bindings,
|
|
1610
|
+
errors,
|
|
1611
|
+
warnings,
|
|
1612
|
+
hasLabelScope,
|
|
1613
|
+
isLabelForbidden: false,
|
|
1614
|
+
isInExcept: false,
|
|
1615
|
+
labelParams: [],
|
|
1616
|
+
labels: new Set(),
|
|
1617
|
+
isGenerated: false,
|
|
783
1618
|
});
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
1619
|
+
const bindDeclarations = (declarations: Declaration[], kind: "Constant" | "Variable") =>
|
|
1620
|
+
declarations.map((declaration) => ({
|
|
1621
|
+
location: declaration.location,
|
|
1622
|
+
check: (errors: SanyError[], warnings: SanyError[]) =>
|
|
1623
|
+
bindModuleName(
|
|
1624
|
+
declaration.name,
|
|
1625
|
+
{ kind, arity: declaration.arity, range: declaration.location.range },
|
|
1626
|
+
createUnit(declaration.location, undefined, errors, warnings, false),
|
|
1627
|
+
),
|
|
1628
|
+
}));
|
|
1629
|
+
return collectSemanticErrors(
|
|
1630
|
+
[
|
|
1631
|
+
...unit.reports.map((report) => ({
|
|
1632
|
+
location: report.location,
|
|
1633
|
+
check: (errors: SanyError[], warnings: SanyError[]) => {
|
|
1634
|
+
errors.push(...report.errors);
|
|
1635
|
+
warnings.push(...report.warnings);
|
|
1636
|
+
},
|
|
1637
|
+
})),
|
|
1638
|
+
...bindDeclarations(module.constants, "Constant"),
|
|
1639
|
+
...bindDeclarations(module.variables, "Variable"),
|
|
1640
|
+
...module.units
|
|
1641
|
+
.flatMap((item) => (item.kind === "Recursive" ? item.declarations : []))
|
|
1642
|
+
.map((declaration) => ({
|
|
1643
|
+
location: declaration.location,
|
|
1644
|
+
check: (errors: SanyError[], warnings: SanyError[]) => {
|
|
1645
|
+
const binding: ModuleBinding = {
|
|
1646
|
+
kind: "Definition",
|
|
1647
|
+
arity: declaration.arity,
|
|
1648
|
+
range: declaration.location.range,
|
|
1649
|
+
position: declaration.location,
|
|
1650
|
+
isPending: true,
|
|
1651
|
+
};
|
|
1652
|
+
const isBound = isBindable(
|
|
1653
|
+
declaration.name,
|
|
1654
|
+
bindings.get(declaration.name),
|
|
1655
|
+
binding,
|
|
1656
|
+
createUnit(declaration.location, undefined, errors, warnings, false),
|
|
1657
|
+
);
|
|
1658
|
+
if (isBound) {
|
|
1659
|
+
bindings.set(declaration.name, binding);
|
|
1660
|
+
recursive.add(binding);
|
|
1661
|
+
}
|
|
1662
|
+
declarations.push({ declaration, binding: isBound ? binding : undefined });
|
|
1663
|
+
},
|
|
1664
|
+
})),
|
|
1665
|
+
...module.definitions.map((definition) => ({
|
|
1666
|
+
location: definition.location,
|
|
1667
|
+
check: (errors: SanyError[], warnings: SanyError[]) => {
|
|
1668
|
+
const inner = createUnit(definition.location, definition, errors, warnings, true);
|
|
1669
|
+
if (isTheoremDefinition(module, definition)) {
|
|
1670
|
+
resolveExpression(definition.body, undefined, inner);
|
|
1671
|
+
} else if (resolveDefinition(definition, undefined, inner, recursive)) {
|
|
1672
|
+
bindings.set(definition.name, {
|
|
1673
|
+
kind: "Definition",
|
|
1674
|
+
arity: getArity(definition),
|
|
1675
|
+
range: definition.location.range,
|
|
1676
|
+
position: definition.location,
|
|
1677
|
+
});
|
|
1678
|
+
}
|
|
1679
|
+
},
|
|
1680
|
+
})),
|
|
1681
|
+
...module.instances.map((instance) => ({
|
|
1682
|
+
location: instance.location,
|
|
1683
|
+
check: (errors: SanyError[], warnings: SanyError[]) => {
|
|
1684
|
+
const inner = createUnit(instance.location, undefined, errors, warnings, false);
|
|
1685
|
+
bindParams(undefined, instance.params, inner);
|
|
1686
|
+
unit.instanced
|
|
1687
|
+
.get(instance)
|
|
1688
|
+
?.forEach((symbol, name) => bindModuleName(name, symbol, inner));
|
|
1689
|
+
if (instance.name !== undefined) {
|
|
1690
|
+
bindModuleName(
|
|
1691
|
+
instance.name,
|
|
1692
|
+
{ kind: "Instance", arity: instance.params.length, range: instance.location.range },
|
|
1693
|
+
inner,
|
|
1694
|
+
);
|
|
1695
|
+
}
|
|
1696
|
+
},
|
|
1697
|
+
})),
|
|
1698
|
+
...module.assumptions.map((assumption) => ({
|
|
1699
|
+
location: assumption.location,
|
|
1700
|
+
check: (errors: SanyError[], warnings: SanyError[]) => {
|
|
1701
|
+
const inner = createUnit(
|
|
1702
|
+
assumption.location,
|
|
1703
|
+
undefined,
|
|
1704
|
+
errors,
|
|
1705
|
+
warnings,
|
|
1706
|
+
assumption.name !== undefined,
|
|
1707
|
+
);
|
|
1708
|
+
resolveExpression(assumption.expression, undefined, inner);
|
|
1709
|
+
if (assumption.name !== undefined) {
|
|
1710
|
+
bindModuleName(
|
|
1711
|
+
assumption.name,
|
|
1712
|
+
{ kind: "Theorem", arity: 0, range: assumption.location.range },
|
|
1713
|
+
inner,
|
|
1714
|
+
);
|
|
1715
|
+
}
|
|
1716
|
+
},
|
|
1717
|
+
})),
|
|
1718
|
+
...module.theorems.map((theorem) => ({
|
|
1719
|
+
location: theorem.location,
|
|
1720
|
+
check: (errors: SanyError[], warnings: SanyError[]) =>
|
|
1721
|
+
resolveTheorem(
|
|
1722
|
+
theorem,
|
|
1723
|
+
createUnit(theorem.location, undefined, errors, warnings, theorem.name !== undefined),
|
|
1724
|
+
),
|
|
1725
|
+
})),
|
|
1726
|
+
...module.uses.map((use) => ({
|
|
1727
|
+
location: use.location,
|
|
1728
|
+
check: (errors: SanyError[], warnings: SanyError[]) =>
|
|
1729
|
+
checkFacts(
|
|
1730
|
+
use.facts,
|
|
1731
|
+
undefined,
|
|
1732
|
+
createUnit(use.location, undefined, errors, warnings, false),
|
|
1733
|
+
),
|
|
1734
|
+
})),
|
|
1735
|
+
{
|
|
1736
|
+
location: module.location.range?.end ?? module.location,
|
|
1737
|
+
check: (errors: SanyError[]) =>
|
|
1738
|
+
declarations.forEach((item) => {
|
|
1739
|
+
const range = item.declaration.location.range;
|
|
1740
|
+
if (range && (!item.binding || item.binding.isPending)) {
|
|
1741
|
+
errors.push({
|
|
1742
|
+
location: formatRange(range),
|
|
1743
|
+
message: `Symbol ${item.declaration.name} declared in RECURSIVE statement but not defined.`,
|
|
1744
|
+
});
|
|
1745
|
+
}
|
|
1746
|
+
}),
|
|
1747
|
+
},
|
|
1748
|
+
],
|
|
1749
|
+
unit.warnings,
|
|
792
1750
|
);
|
|
793
1751
|
};
|