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