tla-ts 0.0.1 → 0.0.3-dev.19c763f
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +98 -39
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +225 -30
- package/dist/index.d.ts +225 -30
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +2 -2
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +21 -1
- package/dist/run-cli.js +21 -1
- package/dist/transfer.cjs +400 -39
- package/dist/transfer.js +400 -39
- package/package.json +6 -2
- package/src/ast.ts +213 -6
- package/src/bags.ts +161 -71
- package/src/builtins.ts +203 -23
- package/src/bundled-modules.ts +63 -32
- package/src/check.ts +1086 -244
- package/src/cli-options.ts +64 -0
- package/src/codegen.ts +58 -34
- package/src/config.ts +39 -11
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +557 -153
- package/src/errors.ts +66 -1
- package/src/evaluate.ts +547 -255
- package/src/exit-codes.ts +53 -0
- package/src/fingerprints.ts +48 -13
- package/src/index.ts +9 -4
- package/src/instances.ts +235 -79
- package/src/java-overrides.ts +241 -0
- package/src/json.ts +153 -72
- package/src/levels.ts +850 -0
- package/src/lexer.ts +108 -29
- package/src/live-check.ts +710 -0
- package/src/liveness.ts +37 -599
- package/src/obligation-action.ts +384 -0
- package/src/obligation-anon.ts +575 -0
- package/src/obligation-deref.ts +637 -0
- package/src/obligation-expression.ts +1060 -0
- package/src/obligation-format.ts +406 -0
- package/src/obligation-generate.ts +1074 -0
- package/src/obligation-levels.ts +329 -0
- package/src/obligation-module.ts +1042 -0
- package/src/obligation-printer.ts +889 -0
- package/src/obligation-proof.ts +76 -0
- package/src/obligation-source.ts +1084 -0
- package/src/obligation-tuples.ts +184 -0
- package/src/obligations.ts +316 -0
- package/src/parallel-worker.ts +186 -201
- package/src/parallel.ts +179 -84
- package/src/parser.ts +970 -206
- package/src/pretty-print.ts +259 -0
- package/src/randomization.ts +24 -113
- package/src/resolve.ts +483 -51
- package/src/run-cli.ts +473 -62
- package/src/runtime.ts +65 -28
- package/src/spec-description.ts +521 -0
- package/src/standard-levels.ts +105 -0
- package/src/standard-modules.ts +223 -92
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +31 -45
- package/src/substitution-levels.ts +184 -0
- package/src/successors.ts +276 -53
- package/src/symmetry.ts +79 -23
- package/src/tableau.ts +43 -37
- package/src/tlc-ext.ts +148 -44
- package/src/tlc-module.ts +71 -66
- package/src/tlc-modules.ts +1022 -0
- package/src/tlc-order.ts +511 -0
- package/src/tlc-random.ts +411 -0
- package/src/tlc-summary.ts +109 -0
- package/src/transfer.ts +26 -5
- package/src/values.ts +1245 -176
package/src/resolve.ts
CHANGED
|
@@ -1,19 +1,26 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
isLocallyResolved,
|
|
4
|
+
type AssumeProve,
|
|
5
|
+
type Bound,
|
|
6
|
+
type BoundPattern,
|
|
7
|
+
type Definition,
|
|
8
|
+
type Expression,
|
|
9
|
+
type Instance,
|
|
10
|
+
type Module,
|
|
11
|
+
type NewSymbol,
|
|
12
|
+
type OperatorParam,
|
|
13
|
+
type Proof,
|
|
14
|
+
type ProofStep,
|
|
15
|
+
type SourceLocation,
|
|
16
|
+
type Theorem,
|
|
10
17
|
} from "./ast";
|
|
11
18
|
import { TlaSyntaxError } from "./errors";
|
|
12
|
-
import { getChildren, isLocallyResolved } from "./evaluate";
|
|
13
19
|
|
|
14
20
|
export interface ModuleSymbol {
|
|
15
21
|
key: string;
|
|
16
22
|
arity: number;
|
|
23
|
+
declaration?: "Constant" | "Variable";
|
|
17
24
|
}
|
|
18
25
|
|
|
19
26
|
export type Symbols = Map<string, ModuleSymbol>;
|
|
@@ -30,14 +37,21 @@ interface Unit {
|
|
|
30
37
|
getKey: (definition: Definition) => string;
|
|
31
38
|
location: SourceLocation;
|
|
32
39
|
self: Definition | undefined;
|
|
40
|
+
facts: Set<string>;
|
|
33
41
|
}
|
|
34
42
|
|
|
35
|
-
const LOCAL_RESOLUTION = { kind: "Local" } as const;
|
|
43
|
+
export const LOCAL_RESOLUTION = { kind: "Local" } as const;
|
|
36
44
|
|
|
37
45
|
const renamedArities = new WeakMap<Expression, number>();
|
|
38
46
|
|
|
39
47
|
export const KEY_SEPARATOR = "@";
|
|
40
48
|
|
|
49
|
+
// A key is a name, the separator, and an id, so the operator @@ is not one.
|
|
50
|
+
const isKey = (name: string) => {
|
|
51
|
+
const index = name.lastIndexOf(KEY_SEPARATOR);
|
|
52
|
+
return index > 0 && index < name.length - 1;
|
|
53
|
+
};
|
|
54
|
+
|
|
41
55
|
const CORE_SYMBOLS: Symbols = new Map(
|
|
42
56
|
["~>", "-+->", "\\cdot"].map((name) => [name, { key: name, arity: 2 }]),
|
|
43
57
|
);
|
|
@@ -45,7 +59,7 @@ const CORE_SYMBOLS: Symbols = new Map(
|
|
|
45
59
|
export const isBefore = (left: SourceLocation, right: SourceLocation) =>
|
|
46
60
|
left.line < right.line || (left.line === right.line && left.column < right.column);
|
|
47
61
|
|
|
48
|
-
const getArity = (definition: Definition) =>
|
|
62
|
+
export const getArity = (definition: Definition) =>
|
|
49
63
|
definition.kind === "Operator" ? definition.params.length : 0;
|
|
50
64
|
|
|
51
65
|
const isInstantiated = (definition: Definition) => definition.name.includes("!");
|
|
@@ -60,9 +74,44 @@ const lookupLocal = (scope: LocalName | undefined, name: string) => {
|
|
|
60
74
|
const bindNames = (scope: LocalName | undefined, names: string[], arity = 0) =>
|
|
61
75
|
names.reduce<LocalName | undefined>((parent, name) => ({ name, arity, parent }), scope);
|
|
62
76
|
|
|
63
|
-
const
|
|
77
|
+
const bindParams = (scope: LocalName | undefined, params: OperatorParam[]) =>
|
|
78
|
+
params.reduce<LocalName | undefined>(
|
|
79
|
+
(parent, param) => ({ name: param.name, arity: param.arity, parent }),
|
|
80
|
+
scope,
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
export const getPatternNames = (pattern: BoundPattern) =>
|
|
64
84
|
pattern.kind === "Name" ? [pattern.name] : pattern.names;
|
|
65
85
|
|
|
86
|
+
export const getBoundNames = (bounds: Bound[]) =>
|
|
87
|
+
bounds.flatMap((bound) => getPatternNames(bound.pattern));
|
|
88
|
+
|
|
89
|
+
const getVisibleDefinition = (name: string, unit: Unit) => {
|
|
90
|
+
const definition = unit.definitions.get(name);
|
|
91
|
+
const isVisible =
|
|
92
|
+
definition !== undefined &&
|
|
93
|
+
(definition === unit.self
|
|
94
|
+
? definition.kind === "Function" || definition.isRecursive
|
|
95
|
+
: definition.isRecursive || isBefore(definition.location, unit.location));
|
|
96
|
+
return isVisible ? definition : undefined;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const findSymbol = (name: string, unit: Unit): ModuleSymbol | undefined => {
|
|
100
|
+
const definition = getVisibleDefinition(name, unit);
|
|
101
|
+
return definition
|
|
102
|
+
? { key: unit.getKey(definition), arity: getArity(definition) }
|
|
103
|
+
: unit.imported.get(name);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// SANY names an undefined operator symbol such as + differently from an undefined identifier.
|
|
107
|
+
const isOperatorSymbol = (name: string) =>
|
|
108
|
+
name[0].toLowerCase() === name[0].toUpperCase() &&
|
|
109
|
+
name[0] !== "_" &&
|
|
110
|
+
(name[0] < "0" || name[0] > "9");
|
|
111
|
+
|
|
112
|
+
const isStepName = (name: string) =>
|
|
113
|
+
name.startsWith("<") && ((name[1] >= "0" && name[1] <= "9") || name[1] === "*");
|
|
114
|
+
|
|
66
115
|
const getSymbolArity = (
|
|
67
116
|
expression: Expression & { kind: "Identifier" | "Apply" | "OperatorReference" },
|
|
68
117
|
name: string,
|
|
@@ -72,28 +121,34 @@ const getSymbolArity = (
|
|
|
72
121
|
const location = expression.location;
|
|
73
122
|
const renamed = renamedArities.get(expression);
|
|
74
123
|
if (renamed !== undefined) return renamed;
|
|
75
|
-
if (name
|
|
124
|
+
if (isKey(name)) return expression.kind === "Apply" ? expression.args.length : 0;
|
|
125
|
+
if (isStepName(name)) return 0;
|
|
76
126
|
const local = lookupLocal(scope, name);
|
|
77
127
|
if (local && expression.kind !== "OperatorReference") expression.resolution = LOCAL_RESOLUTION;
|
|
78
128
|
if (local) return local.arity;
|
|
79
|
-
const
|
|
80
|
-
const isVisible =
|
|
81
|
-
definition !== undefined &&
|
|
82
|
-
(definition === unit.self
|
|
83
|
-
? definition.kind === "Function" || definition.isRecursive
|
|
84
|
-
: definition.isRecursive || isBefore(definition.location, unit.location));
|
|
85
|
-
const symbol =
|
|
86
|
-
definition && isVisible
|
|
87
|
-
? { key: unit.getKey(definition), arity: getArity(definition) }
|
|
88
|
-
: unit.imported.get(name);
|
|
129
|
+
const symbol = findSymbol(name, unit);
|
|
89
130
|
if (symbol) {
|
|
90
131
|
if (expression.kind === "Identifier") expression.name = symbol.key;
|
|
91
132
|
else expression.operator = symbol.key;
|
|
92
133
|
renamedArities.set(expression, symbol.arity);
|
|
93
134
|
return symbol.arity;
|
|
94
135
|
}
|
|
136
|
+
// A name such as Inv!P1 selects the label P1 of a definition or theorem, which takes its own
|
|
137
|
+
// arguments.
|
|
138
|
+
const prefix = name.slice(0, name.indexOf("!"));
|
|
139
|
+
const isSelected =
|
|
140
|
+
prefix !== "" &&
|
|
141
|
+
(lookupLocal(scope, prefix) !== undefined ||
|
|
142
|
+
findSymbol(prefix, unit) !== undefined ||
|
|
143
|
+
unit.facts.has(prefix));
|
|
144
|
+
if (isSelected) {
|
|
145
|
+
return expression.kind === "Apply" ? expression.args.length : 0;
|
|
146
|
+
}
|
|
147
|
+
const position = `at line ${location.line}, column ${location.column}`;
|
|
95
148
|
throw new TlaSyntaxError(
|
|
96
|
-
|
|
149
|
+
isOperatorSymbol(name)
|
|
150
|
+
? `Could not find declaration or definition of symbol '${name}' ${position}`
|
|
151
|
+
: `Unknown operator: \`${name}' ${position}`,
|
|
97
152
|
);
|
|
98
153
|
};
|
|
99
154
|
|
|
@@ -123,11 +178,7 @@ const resolveDefinition = (definition: Definition, scope: LocalName | undefined,
|
|
|
123
178
|
markRecursiveConstructions(definition.body, definition.name);
|
|
124
179
|
return;
|
|
125
180
|
}
|
|
126
|
-
|
|
127
|
-
(parent, param) => ({ name: param.name, arity: param.arity, parent }),
|
|
128
|
-
self,
|
|
129
|
-
);
|
|
130
|
-
resolveExpression(definition.body, params, unit);
|
|
181
|
+
resolveExpression(definition.body, bindParams(self, definition.params), unit);
|
|
131
182
|
};
|
|
132
183
|
|
|
133
184
|
const resolveDefinitions = (
|
|
@@ -147,22 +198,67 @@ const resolveDefinitions = (
|
|
|
147
198
|
}, recursiveScope);
|
|
148
199
|
};
|
|
149
200
|
|
|
201
|
+
// OpDefNode#match: a parameter of arity 0 takes an expression, and a parameter of arity n takes an
|
|
202
|
+
// operator of arity n whose own parameters take expressions.
|
|
203
|
+
const checkArgument = (
|
|
204
|
+
definition: Definition,
|
|
205
|
+
index: number,
|
|
206
|
+
arg: Expression,
|
|
207
|
+
argArity: number,
|
|
208
|
+
argDefinition: Definition | undefined,
|
|
209
|
+
) => {
|
|
210
|
+
const param = definition.kind === "Operator" ? definition.params[index] : undefined;
|
|
211
|
+
const location = arg.location;
|
|
212
|
+
if (param?.arity === 0 && argArity > 0) {
|
|
213
|
+
throw new TlaSyntaxError(
|
|
214
|
+
`Operator used in argument number ${index + 1} has incorrect number of arguments at line ${location.line}, column ${location.column}`,
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
const isHigherOrder =
|
|
218
|
+
argDefinition?.kind === "Operator" && argDefinition.params.some((inner) => inner.arity > 0);
|
|
219
|
+
if (param !== undefined && param.arity > 0 && (argArity !== param.arity || isHigherOrder)) {
|
|
220
|
+
throw new TlaSyntaxError(
|
|
221
|
+
`Argument number ${index + 1} to operator '${definition.name}' should be a ${param.arity}-parameter operator at line ${location.line}, column ${location.column}`,
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
const getArgumentName = (arg: Expression) =>
|
|
227
|
+
arg.kind === "Identifier"
|
|
228
|
+
? arg.name
|
|
229
|
+
: arg.kind === "OperatorReference"
|
|
230
|
+
? arg.operator
|
|
231
|
+
: undefined;
|
|
232
|
+
|
|
150
233
|
const resolveApply = (
|
|
151
234
|
expression: Expression & { kind: "Apply" },
|
|
152
235
|
scope: LocalName | undefined,
|
|
153
236
|
unit: Unit,
|
|
154
237
|
) => {
|
|
238
|
+
const callee = lookupLocal(scope, expression.operator)
|
|
239
|
+
? undefined
|
|
240
|
+
: getVisibleDefinition(expression.operator, unit);
|
|
155
241
|
const arity = getSymbolArity(expression, expression.operator, scope, unit);
|
|
156
242
|
if (arity !== expression.args.length) {
|
|
157
243
|
throw new TlaSyntaxError(
|
|
158
244
|
`The operator ${expression.operator} requires ${arity} arguments at line ${expression.location.line}, column ${expression.location.column}`,
|
|
159
245
|
);
|
|
160
246
|
}
|
|
161
|
-
expression.args.forEach((arg) =>
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
247
|
+
expression.args.forEach((arg, index) => {
|
|
248
|
+
const name = getArgumentName(arg);
|
|
249
|
+
const argDefinition =
|
|
250
|
+
name === undefined || lookupLocal(scope, name) ? undefined : getVisibleDefinition(name, unit);
|
|
251
|
+
const argArity = resolveArgument(arg, scope, unit);
|
|
252
|
+
if (callee) checkArgument(callee, index, arg, argArity, argDefinition);
|
|
253
|
+
});
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
// An argument may name an operator that takes arguments of its own.
|
|
257
|
+
const resolveArgument = (arg: Expression, scope: LocalName | undefined, unit: Unit) => {
|
|
258
|
+
if (arg.kind === "Identifier") return getSymbolArity(arg, arg.name, scope, unit);
|
|
259
|
+
if (arg.kind === "OperatorReference") return getSymbolArity(arg, arg.operator, scope, unit);
|
|
260
|
+
resolveExpression(arg, scope, unit);
|
|
261
|
+
return arg.kind === "Lambda" ? arg.params.length : 0;
|
|
166
262
|
};
|
|
167
263
|
|
|
168
264
|
const resolveExpression = (
|
|
@@ -174,6 +270,7 @@ const resolveExpression = (
|
|
|
174
270
|
expressions.forEach((child) => resolveExpression(child, scope, unit));
|
|
175
271
|
switch (expression.kind) {
|
|
176
272
|
case "Number":
|
|
273
|
+
case "Decimal":
|
|
177
274
|
case "String":
|
|
178
275
|
case "ExceptAt":
|
|
179
276
|
return;
|
|
@@ -288,24 +385,338 @@ const resolveExpression = (
|
|
|
288
385
|
resolveExpression(expression.body, scope, unit);
|
|
289
386
|
return;
|
|
290
387
|
case "Subexpression":
|
|
291
|
-
|
|
292
|
-
|
|
388
|
+
expression.selectors
|
|
389
|
+
.flatMap((selector) => ("args" in selector ? selector.args : []))
|
|
390
|
+
.forEach((arg) => resolveArgument(arg, scope, unit));
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
const resolveNewSymbol = (symbol: NewSymbol, scope: LocalName | undefined, unit: Unit) => {
|
|
395
|
+
if (symbol.set) resolveExpression(symbol.set, scope, unit);
|
|
396
|
+
return { name: symbol.name, arity: symbol.arity, parent: scope };
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
// The NEW symbols of an ASSUME stay in scope for its conclusion and the proof that follows.
|
|
400
|
+
const resolveAssumeProve = (
|
|
401
|
+
assumeProve: AssumeProve,
|
|
402
|
+
scope: LocalName | undefined,
|
|
403
|
+
unit: Unit,
|
|
404
|
+
): LocalName | undefined => {
|
|
405
|
+
const assumed = assumeProve.assumptions.reduce((current, assumption) => {
|
|
406
|
+
if (assumption.kind === "NewSymbol") return resolveNewSymbol(assumption, current, unit);
|
|
407
|
+
if (assumption.kind === "AssumeProve") resolveAssumeProve(assumption, current, unit);
|
|
408
|
+
else resolveExpression(assumption, current, unit);
|
|
409
|
+
return current;
|
|
410
|
+
}, scope);
|
|
411
|
+
resolveExpression(assumeProve.conclusion, assumed, unit);
|
|
412
|
+
return assumed;
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
const resolveStatement = (
|
|
416
|
+
statement: Expression | AssumeProve,
|
|
417
|
+
scope: LocalName | undefined,
|
|
418
|
+
unit: Unit,
|
|
419
|
+
) => {
|
|
420
|
+
if (statement.kind === "AssumeProve") return resolveAssumeProve(statement, scope, unit);
|
|
421
|
+
resolveExpression(statement, scope, unit);
|
|
422
|
+
return scope;
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
// SANY resolves the expressions of a proof, while its facts and DEF clauses may also name
|
|
426
|
+
// theorems, assumptions, and modules.
|
|
427
|
+
const resolveProof = (proof: Proof | undefined, scope: LocalName | undefined, unit: Unit) => {
|
|
428
|
+
if (proof?.kind !== "Steps") return;
|
|
429
|
+
proof.steps.reduce((current, step) => resolveStep(step, current, unit), scope);
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
const resolveStep = (
|
|
433
|
+
step: ProofStep,
|
|
434
|
+
scope: LocalName | undefined,
|
|
435
|
+
unit: Unit,
|
|
436
|
+
): LocalName | undefined => {
|
|
437
|
+
const body = step.body;
|
|
438
|
+
switch (body.kind) {
|
|
439
|
+
case "Assert": {
|
|
440
|
+
const assumed = resolveStatement(body.statement, scope, unit);
|
|
441
|
+
resolveProof(step.proof, assumed, unit);
|
|
442
|
+
return body.isSuffices ? assumed : scope;
|
|
443
|
+
}
|
|
444
|
+
case "Define":
|
|
445
|
+
return resolveDefinitions(
|
|
446
|
+
body.definitions.flatMap((definition) =>
|
|
447
|
+
definition.kind === "Instance" ? [] : [definition],
|
|
448
|
+
),
|
|
449
|
+
scope,
|
|
450
|
+
unit,
|
|
293
451
|
);
|
|
452
|
+
case "Have":
|
|
453
|
+
case "Case":
|
|
454
|
+
resolveExpression(body.expression, scope, unit);
|
|
455
|
+
resolveProof(step.proof, scope, unit);
|
|
456
|
+
return scope;
|
|
457
|
+
case "Witness":
|
|
458
|
+
body.expressions.forEach((expression) => resolveExpression(expression, scope, unit));
|
|
459
|
+
return scope;
|
|
460
|
+
case "Take":
|
|
461
|
+
return bindNames(resolveBounds(body.bounds, scope, unit), body.names);
|
|
462
|
+
case "Pick": {
|
|
463
|
+
const picked = bindNames(resolveBounds(body.bounds, scope, unit), body.names);
|
|
464
|
+
resolveExpression(body.body, picked, unit);
|
|
465
|
+
resolveProof(step.proof, scope, unit);
|
|
466
|
+
return picked;
|
|
467
|
+
}
|
|
468
|
+
default:
|
|
469
|
+
resolveProof(step.proof, scope, unit);
|
|
470
|
+
return scope;
|
|
294
471
|
}
|
|
295
472
|
};
|
|
296
473
|
|
|
474
|
+
const resolveTheorem = (theorem: Theorem, unit: Unit) => {
|
|
475
|
+
const isDefinition = theorem.name !== undefined && theorem.statement.kind !== "AssumeProve";
|
|
476
|
+
const scope = isDefinition ? undefined : resolveStatement(theorem.statement, undefined, unit);
|
|
477
|
+
resolveProof(theorem.proof, scope, unit);
|
|
478
|
+
};
|
|
479
|
+
|
|
297
480
|
interface ModuleUnit {
|
|
298
481
|
imported: Symbols;
|
|
482
|
+
importedAt: Map<string, SourceLocation>;
|
|
299
483
|
definitions: Map<string, Definition>;
|
|
300
484
|
getKey: (definition: Definition) => string;
|
|
301
485
|
}
|
|
302
486
|
|
|
487
|
+
interface ModuleName {
|
|
488
|
+
name: string;
|
|
489
|
+
kind: "Definition" | "Constant" | "Variable";
|
|
490
|
+
location: SourceLocation;
|
|
491
|
+
isRecursive: boolean;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
type IsDefined = (name: string) => boolean;
|
|
495
|
+
|
|
496
|
+
const createRedefinitionError = (name: string, location: SourceLocation) =>
|
|
497
|
+
new TlaSyntaxError(
|
|
498
|
+
`Multiply-defined symbol '${name}' at line ${location.line}, column ${location.column}`,
|
|
499
|
+
);
|
|
500
|
+
|
|
501
|
+
const bindUnique = (
|
|
502
|
+
scope: LocalName | undefined,
|
|
503
|
+
names: string[],
|
|
504
|
+
isDefined: IsDefined,
|
|
505
|
+
location: SourceLocation,
|
|
506
|
+
) =>
|
|
507
|
+
names.reduce<LocalName | undefined>((parent, name) => {
|
|
508
|
+
if (lookupLocal(parent, name) || isDefined(name)) throw createRedefinitionError(name, location);
|
|
509
|
+
return { name, arity: 0, parent };
|
|
510
|
+
}, scope);
|
|
511
|
+
|
|
512
|
+
const checkBoundNames = (
|
|
513
|
+
bounds: Bound[],
|
|
514
|
+
scope: LocalName | undefined,
|
|
515
|
+
isDefined: IsDefined,
|
|
516
|
+
location: SourceLocation,
|
|
517
|
+
) => {
|
|
518
|
+
bounds.forEach((bound) => checkNames(bound.set, scope, isDefined));
|
|
519
|
+
return bounds.reduce(
|
|
520
|
+
(current, bound) => bindUnique(current, getPatternNames(bound.pattern), isDefined, location),
|
|
521
|
+
scope,
|
|
522
|
+
);
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
const checkDefinitionNames = (
|
|
526
|
+
definition: Definition,
|
|
527
|
+
scope: LocalName | undefined,
|
|
528
|
+
isDefined: IsDefined,
|
|
529
|
+
) => {
|
|
530
|
+
if (definition.kind === "Function") {
|
|
531
|
+
const self = bindNames(scope, [definition.name]);
|
|
532
|
+
const bound = checkBoundNames(definition.bounds, self, isDefined, definition.location);
|
|
533
|
+
checkNames(definition.body, bound, isDefined);
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
const params = bindUnique(
|
|
537
|
+
scope,
|
|
538
|
+
definition.params.map((param) => param.name),
|
|
539
|
+
isDefined,
|
|
540
|
+
definition.location,
|
|
541
|
+
);
|
|
542
|
+
checkNames(definition.body, params, isDefined);
|
|
543
|
+
};
|
|
544
|
+
|
|
545
|
+
const checkNames = (
|
|
546
|
+
expression: Expression,
|
|
547
|
+
scope: LocalName | undefined,
|
|
548
|
+
isDefined: IsDefined,
|
|
549
|
+
): void => {
|
|
550
|
+
const location = expression.location;
|
|
551
|
+
switch (expression.kind) {
|
|
552
|
+
case "Lambda":
|
|
553
|
+
checkNames(
|
|
554
|
+
expression.body,
|
|
555
|
+
bindUnique(scope, expression.params, isDefined, location),
|
|
556
|
+
isDefined,
|
|
557
|
+
);
|
|
558
|
+
return;
|
|
559
|
+
case "Let": {
|
|
560
|
+
const recursive = expression.definitions
|
|
561
|
+
.filter((definition) => definition.isRecursive)
|
|
562
|
+
.map((definition) => definition.name);
|
|
563
|
+
const inner = expression.definitions.reduce((current, definition) => {
|
|
564
|
+
if (lookupLocal(current, definition.name) || isDefined(definition.name)) {
|
|
565
|
+
throw createRedefinitionError(definition.name, definition.location);
|
|
566
|
+
}
|
|
567
|
+
checkDefinitionNames(definition, bindNames(current, recursive), isDefined);
|
|
568
|
+
return bindNames(current, [definition.name]);
|
|
569
|
+
}, scope);
|
|
570
|
+
checkNames(
|
|
571
|
+
expression.body,
|
|
572
|
+
bindUnique(
|
|
573
|
+
inner,
|
|
574
|
+
(expression.instances ?? []).flatMap((instance) =>
|
|
575
|
+
instance.name === undefined ? [] : [instance.name],
|
|
576
|
+
),
|
|
577
|
+
isDefined,
|
|
578
|
+
location,
|
|
579
|
+
),
|
|
580
|
+
isDefined,
|
|
581
|
+
);
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
case "Forall":
|
|
585
|
+
case "Exists":
|
|
586
|
+
case "FunctionConstruction":
|
|
587
|
+
checkNames(
|
|
588
|
+
expression.body,
|
|
589
|
+
checkBoundNames(expression.bounds, scope, isDefined, location),
|
|
590
|
+
isDefined,
|
|
591
|
+
);
|
|
592
|
+
return;
|
|
593
|
+
case "SetFilter":
|
|
594
|
+
checkNames(
|
|
595
|
+
expression.predicate,
|
|
596
|
+
checkBoundNames([expression.bound], scope, isDefined, location),
|
|
597
|
+
isDefined,
|
|
598
|
+
);
|
|
599
|
+
return;
|
|
600
|
+
case "SetMap":
|
|
601
|
+
checkNames(
|
|
602
|
+
expression.expression,
|
|
603
|
+
checkBoundNames(expression.bounds, scope, isDefined, location),
|
|
604
|
+
isDefined,
|
|
605
|
+
);
|
|
606
|
+
return;
|
|
607
|
+
case "UnboundedQuantifier":
|
|
608
|
+
checkNames(
|
|
609
|
+
expression.body,
|
|
610
|
+
bindUnique(scope, expression.names, isDefined, location),
|
|
611
|
+
isDefined,
|
|
612
|
+
);
|
|
613
|
+
return;
|
|
614
|
+
case "Choose":
|
|
615
|
+
if (expression.set) checkNames(expression.set, scope, isDefined);
|
|
616
|
+
checkNames(
|
|
617
|
+
expression.body,
|
|
618
|
+
bindUnique(scope, getPatternNames(expression.pattern), isDefined, location),
|
|
619
|
+
isDefined,
|
|
620
|
+
);
|
|
621
|
+
return;
|
|
622
|
+
default:
|
|
623
|
+
getChildren(expression).forEach((child) => checkNames(child, scope, isDefined));
|
|
624
|
+
}
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
const getModuleNames = (module: Module) => [
|
|
628
|
+
...module.definitions.map((definition): ModuleName => ({
|
|
629
|
+
name: definition.name,
|
|
630
|
+
kind: "Definition",
|
|
631
|
+
location: definition.location,
|
|
632
|
+
isRecursive: definition.isRecursive,
|
|
633
|
+
})),
|
|
634
|
+
...module.instances.flatMap((instance): ModuleName[] =>
|
|
635
|
+
instance.name === undefined
|
|
636
|
+
? []
|
|
637
|
+
: [
|
|
638
|
+
{
|
|
639
|
+
name: instance.name,
|
|
640
|
+
kind: "Definition",
|
|
641
|
+
location: instance.location,
|
|
642
|
+
isRecursive: false,
|
|
643
|
+
},
|
|
644
|
+
],
|
|
645
|
+
),
|
|
646
|
+
...module.constants.map((constant): ModuleName => ({
|
|
647
|
+
name: constant.name,
|
|
648
|
+
kind: "Constant",
|
|
649
|
+
location: constant.location,
|
|
650
|
+
isRecursive: false,
|
|
651
|
+
})),
|
|
652
|
+
...module.variables.map((variable): ModuleName => ({
|
|
653
|
+
name: variable.name,
|
|
654
|
+
kind: "Variable",
|
|
655
|
+
location: variable.location,
|
|
656
|
+
isRecursive: false,
|
|
657
|
+
})),
|
|
658
|
+
];
|
|
659
|
+
|
|
660
|
+
export const checkRedefinitions = (module: Module, unit: ModuleUnit) => {
|
|
661
|
+
const byName = new Map<string, ModuleName[]>();
|
|
662
|
+
getModuleNames(module)
|
|
663
|
+
.sort((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
|
|
664
|
+
.forEach((entry) => byName.set(entry.name, [...(byName.get(entry.name) ?? []), entry]));
|
|
665
|
+
const isImportedBefore = (name: string, location: SourceLocation) => {
|
|
666
|
+
if (!unit.imported.has(name) && !CORE_SYMBOLS.has(name)) return false;
|
|
667
|
+
const importedAt = unit.importedAt.get(name);
|
|
668
|
+
return importedAt === undefined || isBefore(importedAt, location);
|
|
669
|
+
};
|
|
670
|
+
byName.forEach((entries) =>
|
|
671
|
+
entries.forEach((entry, index) => {
|
|
672
|
+
const isDuplicate = entries
|
|
673
|
+
.slice(0, index)
|
|
674
|
+
.some(
|
|
675
|
+
(earlier) =>
|
|
676
|
+
earlier.kind === "Definition" ||
|
|
677
|
+
entry.kind === "Definition" ||
|
|
678
|
+
earlier.kind !== entry.kind,
|
|
679
|
+
);
|
|
680
|
+
// A module may declare again a constant or variable that it extends or that encloses it.
|
|
681
|
+
const isRedeclared = unit.imported.get(entry.name)?.declaration === entry.kind;
|
|
682
|
+
if (isDuplicate || (!isRedeclared && isImportedBefore(entry.name, entry.location)))
|
|
683
|
+
throw createRedefinitionError(entry.name, entry.location);
|
|
684
|
+
}),
|
|
685
|
+
);
|
|
686
|
+
const getIsDefined =
|
|
687
|
+
(location: SourceLocation): IsDefined =>
|
|
688
|
+
(name) =>
|
|
689
|
+
(byName.get(name) ?? []).some(
|
|
690
|
+
(entry) => entry.isRecursive || isBefore(entry.location, location),
|
|
691
|
+
) || isImportedBefore(name, location);
|
|
692
|
+
module.definitions.forEach((definition) =>
|
|
693
|
+
checkDefinitionNames(definition, undefined, getIsDefined(definition.location)),
|
|
694
|
+
);
|
|
695
|
+
module.assumptions.forEach((assumption) =>
|
|
696
|
+
checkNames(assumption.expression, undefined, getIsDefined(assumption.location)),
|
|
697
|
+
);
|
|
698
|
+
module.instances.forEach((instance) => {
|
|
699
|
+
const isDefined = getIsDefined(instance.location);
|
|
700
|
+
const params = bindUnique(
|
|
701
|
+
undefined,
|
|
702
|
+
instance.params.map((param) => param.name),
|
|
703
|
+
isDefined,
|
|
704
|
+
instance.location,
|
|
705
|
+
);
|
|
706
|
+
instance.substitutions.forEach((substitution) =>
|
|
707
|
+
checkNames(substitution.expression, params, isDefined),
|
|
708
|
+
);
|
|
709
|
+
});
|
|
710
|
+
};
|
|
711
|
+
|
|
303
712
|
const createSymbols = (module: Module, imported: Symbols) => {
|
|
304
713
|
const symbols = new Map([...CORE_SYMBOLS, ...imported]);
|
|
305
714
|
module.constants.forEach((constant) =>
|
|
306
715
|
symbols.set(constant.name, { key: constant.name, arity: constant.arity }),
|
|
307
716
|
);
|
|
308
|
-
module.variables.forEach((variable) =>
|
|
717
|
+
module.variables.forEach((variable) =>
|
|
718
|
+
symbols.set(variable.name, { key: variable.name, arity: 0 }),
|
|
719
|
+
);
|
|
309
720
|
return symbols;
|
|
310
721
|
};
|
|
311
722
|
|
|
@@ -315,48 +726,69 @@ export const resolveSubstitutions = (
|
|
|
315
726
|
module: Module,
|
|
316
727
|
unit: ModuleUnit,
|
|
317
728
|
) => {
|
|
318
|
-
const scope =
|
|
319
|
-
undefined,
|
|
320
|
-
instance.params.map((param) => param.name),
|
|
321
|
-
);
|
|
729
|
+
const scope = bindParams(undefined, instance.params);
|
|
322
730
|
const context: Unit = {
|
|
323
731
|
imported: createSymbols(module, unit.imported),
|
|
324
732
|
definitions: unit.definitions,
|
|
325
733
|
getKey: unit.getKey,
|
|
326
734
|
location: instance.location,
|
|
327
735
|
self: undefined,
|
|
736
|
+
facts: new Set(),
|
|
328
737
|
};
|
|
329
738
|
const explicit = new Map(instance.substitutions.map((item) => [item.name, item.expression]));
|
|
739
|
+
const position = `in the instance of ${instance.module} at line ${instance.location.line}, column ${instance.location.column}`;
|
|
740
|
+
explicit.forEach((_, name) => {
|
|
741
|
+
if (declarations.every((declaration) => declaration.name !== name)) {
|
|
742
|
+
throw new TlaSyntaxError(
|
|
743
|
+
`Identifier '${name}' is not a legal target of a substitution ${position}`,
|
|
744
|
+
);
|
|
745
|
+
}
|
|
746
|
+
});
|
|
330
747
|
return new Map(
|
|
331
748
|
declarations.map((declaration): [string, Expression] => {
|
|
332
|
-
const
|
|
749
|
+
const name = declaration.name;
|
|
750
|
+
if (!explicit.has(name) && !lookupLocal(scope, name) && !findSymbol(name, context)) {
|
|
751
|
+
throw new TlaSyntaxError(`Substitution missing for symbol ${name} ${position}`);
|
|
752
|
+
}
|
|
753
|
+
const expression: Expression = explicit.get(name) ?? {
|
|
333
754
|
kind: "Identifier",
|
|
334
|
-
name
|
|
755
|
+
name,
|
|
335
756
|
location: instance.location,
|
|
336
757
|
};
|
|
337
|
-
if (declaration.arity
|
|
338
|
-
getSymbolArity(expression, expression.name, scope, context);
|
|
339
|
-
} else {
|
|
758
|
+
if (declaration.arity === 0 || expression.kind !== "Identifier") {
|
|
340
759
|
resolveExpression(expression, scope, context);
|
|
760
|
+
return [name, expression];
|
|
341
761
|
}
|
|
342
|
-
|
|
762
|
+
if (getSymbolArity(expression, expression.name, scope, context) !== declaration.arity) {
|
|
763
|
+
throw new TlaSyntaxError(
|
|
764
|
+
`An operator must be substituted for symbol '${name}', and it must have arity ${declaration.arity} ${position}`,
|
|
765
|
+
);
|
|
766
|
+
}
|
|
767
|
+
return [name, expression];
|
|
343
768
|
}),
|
|
344
769
|
);
|
|
345
770
|
};
|
|
346
771
|
|
|
347
772
|
export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
348
773
|
const symbols = createSymbols(module, unit.imported);
|
|
349
|
-
const
|
|
774
|
+
const facts = new Set(
|
|
775
|
+
[...module.theorems, ...module.assumptions].flatMap((fact) => fact.name ?? []),
|
|
776
|
+
);
|
|
777
|
+
const createUnit = (location: SourceLocation, self: Definition | undefined) => ({
|
|
350
778
|
imported: symbols,
|
|
351
779
|
definitions: unit.definitions,
|
|
352
780
|
getKey: unit.getKey,
|
|
353
781
|
location,
|
|
354
782
|
self,
|
|
783
|
+
facts,
|
|
355
784
|
});
|
|
356
785
|
module.definitions.forEach((definition) =>
|
|
357
|
-
resolveDefinition(definition, undefined,
|
|
786
|
+
resolveDefinition(definition, undefined, createUnit(definition.location, definition)),
|
|
358
787
|
);
|
|
359
788
|
module.assumptions.forEach((assumption) =>
|
|
360
|
-
resolveExpression(assumption.expression, undefined,
|
|
789
|
+
resolveExpression(assumption.expression, undefined, createUnit(assumption.location, undefined)),
|
|
790
|
+
);
|
|
791
|
+
module.theorems.forEach((theorem) =>
|
|
792
|
+
resolveTheorem(theorem, createUnit(theorem.location, undefined)),
|
|
361
793
|
);
|
|
362
794
|
};
|