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