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