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