tla-ts 0.0.3-dev.3504663 → 0.0.3-dev.3539317
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/index.d.cts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/run-cli.cjs +3 -3
- package/dist/run-cli.js +20 -20
- package/dist/transfer.cjs +24 -23
- package/dist/transfer.js +24 -23
- package/package.json +1 -1
- package/src/ast.ts +20 -3
- package/src/check.ts +7 -6
- package/src/environment.ts +264 -93
- package/src/errors.ts +15 -4
- package/src/instances.ts +1 -1
- package/src/levels.ts +31 -26
- package/src/parser.ts +103 -36
- package/src/resolve.ts +1105 -525
- package/src/run-cli.ts +2 -0
- package/src/standard-ranges.ts +116 -0
- package/src/subexpressions.ts +210 -162
- package/src/symmetry.ts +93 -35
package/src/run-cli.ts
CHANGED
|
@@ -15,6 +15,7 @@ import { defineCommand, type ParsedArgs } from "./cli-options";
|
|
|
15
15
|
import { parseConfig } from "./config";
|
|
16
16
|
import {
|
|
17
17
|
createSanyError,
|
|
18
|
+
formatSanyWarnings,
|
|
18
19
|
getErrorExitCode,
|
|
19
20
|
TlaConfigError,
|
|
20
21
|
TlaError,
|
|
@@ -334,6 +335,7 @@ const printError = (error: TlaError, path?: string) => {
|
|
|
334
335
|
const report = error instanceof TlaSyntaxError ? error.sany : undefined;
|
|
335
336
|
if (!report || path === undefined) return console.error(`${error.name}: ${error.message}`);
|
|
336
337
|
if (report.parseError !== undefined) console.log(report.parseError);
|
|
338
|
+
if (report.warnings.length > 0) console.log(formatSanyWarnings(report.warnings));
|
|
337
339
|
console.log(
|
|
338
340
|
report.isFatal
|
|
339
341
|
? `\nFatal errors while parsing TLA+ spec in file ${basename(path, ".tla")}\n\n${report.errors.map((item) => `${item.location}\n\n${item.message}\n`).join("")}${formatSanyErrors(report)}\n`
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { SourceRange } from "./ast";
|
|
2
|
+
|
|
3
|
+
// Where the modules in TLC's tla2sany/StandardModules define the operators that tla-ts implements
|
|
4
|
+
// natively, which SANY reports when a spec defines one of them again.
|
|
5
|
+
const RANGES: Record<string, Record<string, [number, number, number, number]>> = {
|
|
6
|
+
Naturals: {
|
|
7
|
+
Nat: [14, 1, 14, 16],
|
|
8
|
+
"+": [15, 1, 15, 19],
|
|
9
|
+
"-": [17, 1, 17, 33],
|
|
10
|
+
"*": [18, 1, 18, 17],
|
|
11
|
+
"^": [19, 1, 19, 19],
|
|
12
|
+
"<": [20, 1, 20, 19],
|
|
13
|
+
">": [21, 1, 21, 19],
|
|
14
|
+
"<=": [22, 1, 22, 19],
|
|
15
|
+
">=": [23, 1, 23, 19],
|
|
16
|
+
"%": [33, 1, 33, 20],
|
|
17
|
+
"\\div": [34, 1, 34, 20],
|
|
18
|
+
"..": [35, 1, 35, 20],
|
|
19
|
+
},
|
|
20
|
+
Integers: {
|
|
21
|
+
Int: [18, 1, 18, 12],
|
|
22
|
+
"-.": [22, 1, 22, 13],
|
|
23
|
+
},
|
|
24
|
+
Sequences: {
|
|
25
|
+
Seq: [20, 1, 20, 41],
|
|
26
|
+
Len: [25, 1, 25, 44],
|
|
27
|
+
"\\o": [30, 1, 31, 76],
|
|
28
|
+
Append: [36, 1, 36, 26],
|
|
29
|
+
Head: [41, 1, 41, 15],
|
|
30
|
+
Tail: [42, 1, 42, 61],
|
|
31
|
+
SubSeq: [48, 1, 48, 50],
|
|
32
|
+
SelectSeq: [53, 1, 65, 14],
|
|
33
|
+
},
|
|
34
|
+
FiniteSets: {
|
|
35
|
+
IsFiniteSet: [14, 1, 19, 68],
|
|
36
|
+
Cardinality: [21, 1, 27, 11],
|
|
37
|
+
},
|
|
38
|
+
Bags: {
|
|
39
|
+
IsABag: [20, 1, 24, 41],
|
|
40
|
+
BagToSet: [26, 1, 26, 23],
|
|
41
|
+
SetToBag: [31, 1, 31, 30],
|
|
42
|
+
BagIn: [36, 1, 36, 31],
|
|
43
|
+
EmptyBag: [41, 1, 41, 24],
|
|
44
|
+
"\\oplus": [43, 1, 49, 46],
|
|
45
|
+
"\\ominus": [51, 1, 60, 50],
|
|
46
|
+
BagUnion: [74, 1, 79, 58],
|
|
47
|
+
"\\sqsubseteq": [81, 1, 87, 42],
|
|
48
|
+
SubBag: [89, 1, 110, 11],
|
|
49
|
+
BagOfAll: [122, 1, 130, 66],
|
|
50
|
+
BagCardinality: [132, 1, 138, 8],
|
|
51
|
+
CopiesIn: [140, 1, 145, 33],
|
|
52
|
+
},
|
|
53
|
+
TLC: {
|
|
54
|
+
Print: [17, 1, 17, 22],
|
|
55
|
+
PrintT: [18, 1, 18, 19],
|
|
56
|
+
Assert: [24, 1, 25, 54],
|
|
57
|
+
JavaTime: [26, 1, 26, 32],
|
|
58
|
+
TLCGet: [75, 1, 75, 28],
|
|
59
|
+
TLCSet: [76, 1, 76, 20],
|
|
60
|
+
":>": [78, 1, 78, 27],
|
|
61
|
+
"@@": [79, 1, 80, 50],
|
|
62
|
+
Permutations: [81, 1, 82, 54],
|
|
63
|
+
SortSeq: [88, 1, 92, 40],
|
|
64
|
+
RandomElement: [110, 1, 110, 41],
|
|
65
|
+
Any: [119, 1, 119, 22],
|
|
66
|
+
ToString: [121, 1, 121, 58],
|
|
67
|
+
TLCEval: [145, 1, 145, 15],
|
|
68
|
+
},
|
|
69
|
+
TLCExt: {
|
|
70
|
+
AssertEq: [14, 1, 20, 14],
|
|
71
|
+
AssertError: [23, 1, 26, 72],
|
|
72
|
+
TLCGetOrDefault: [30, 1, 31, 17],
|
|
73
|
+
TLCGetAndSet: [33, 1, 35, 57],
|
|
74
|
+
Trace: [41, 1, 57, 6],
|
|
75
|
+
CounterExample: [61, 1, 75, 8],
|
|
76
|
+
ToTrace: [77, 1, 87, 12],
|
|
77
|
+
TLCModelValue: [91, 1, 120, 29],
|
|
78
|
+
TLCDefer: [122, 1, 134, 6],
|
|
79
|
+
TLCNoOp: [138, 1, 145, 5],
|
|
80
|
+
PickSuccessor: [148, 1, 162, 37],
|
|
81
|
+
TLCCache: [166, 1, 173, 12],
|
|
82
|
+
TLCFP: [175, 1, 181, 24],
|
|
83
|
+
TLCEvalDefinition: [185, 1, 190, 6],
|
|
84
|
+
},
|
|
85
|
+
Randomization: {
|
|
86
|
+
RandomSubset: [30, 1, 30, 64],
|
|
87
|
+
RandomSetOfSubsets: [48, 1, 49, 67],
|
|
88
|
+
TestRandomSetOfSubsets: [61, 1, 62, 71],
|
|
89
|
+
},
|
|
90
|
+
Json: {
|
|
91
|
+
ToJson: [46, 1, 55, 37],
|
|
92
|
+
ToJsonArray: [57, 1, 64, 42],
|
|
93
|
+
ToJsonObject: [66, 1, 73, 43],
|
|
94
|
+
JsonSerialize: [75, 1, 80, 6],
|
|
95
|
+
JsonDeserialize: [82, 1, 87, 19],
|
|
96
|
+
ndJsonSerialize: [89, 1, 95, 6],
|
|
97
|
+
ndJsonDeserialize: [97, 1, 103, 19],
|
|
98
|
+
},
|
|
99
|
+
_Possible: {
|
|
100
|
+
_Track: [10, 1, 17, 38],
|
|
101
|
+
_Counts: [24, 1, 30, 14],
|
|
102
|
+
_CheckName: [32, 1, 35, 35],
|
|
103
|
+
_PrintCounts: [37, 1, 37, 31],
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export const getStandardRange = (module: string, name: string): SourceRange | undefined => {
|
|
108
|
+
const range = RANGES[module]?.[name];
|
|
109
|
+
return (
|
|
110
|
+
range && {
|
|
111
|
+
module,
|
|
112
|
+
start: { line: range[0], column: range[1] },
|
|
113
|
+
end: { line: range[2], column: range[3] },
|
|
114
|
+
}
|
|
115
|
+
);
|
|
116
|
+
};
|
package/src/subexpressions.ts
CHANGED
|
@@ -1,22 +1,30 @@
|
|
|
1
1
|
import {
|
|
2
2
|
getChildren,
|
|
3
|
+
type AssumeProve,
|
|
3
4
|
type Bound,
|
|
4
5
|
type Definition,
|
|
5
6
|
type Expression,
|
|
7
|
+
type Fact,
|
|
6
8
|
type Instance,
|
|
7
9
|
type Module,
|
|
8
10
|
type OperatorParam,
|
|
11
|
+
type Proof,
|
|
9
12
|
type Selector,
|
|
10
13
|
type SourceLocation,
|
|
14
|
+
type SourceRange,
|
|
15
|
+
type StepBody,
|
|
11
16
|
} from "./ast";
|
|
12
|
-
import { TlaSyntaxError } from "./errors";
|
|
17
|
+
import { formatRange, TlaSyntaxError } from "./errors";
|
|
13
18
|
import { getSourceName, mapChildren, mapDefinition } from "./instances";
|
|
14
19
|
import {
|
|
15
20
|
getArity,
|
|
16
|
-
|
|
21
|
+
getBinder,
|
|
22
|
+
getNameSelectors,
|
|
17
23
|
getOperandIndex,
|
|
18
|
-
|
|
24
|
+
getOperands,
|
|
19
25
|
LOCAL_RESOLUTION,
|
|
26
|
+
selectionErrors,
|
|
27
|
+
selections,
|
|
20
28
|
} from "./resolve";
|
|
21
29
|
|
|
22
30
|
type Lookup = (name: string) => Definition | undefined;
|
|
@@ -33,108 +41,19 @@ interface Selection {
|
|
|
33
41
|
location: SourceLocation;
|
|
34
42
|
}
|
|
35
43
|
|
|
44
|
+
// A name that selects a subexpression, with the range of each part and of the arguments of each
|
|
45
|
+
// part, where SANY reports an error in selecting that part.
|
|
46
|
+
interface SelectingName {
|
|
47
|
+
location: SourceLocation;
|
|
48
|
+
selectorRanges?: SourceRange[];
|
|
49
|
+
argumentRanges?: (SourceRange | undefined)[];
|
|
50
|
+
}
|
|
51
|
+
|
|
36
52
|
const SELECTION_NAME = "$subexpression";
|
|
37
53
|
|
|
38
54
|
const createError = (message: string, location: SourceLocation) =>
|
|
39
55
|
new TlaSyntaxError(`${message} at line ${location.line}, column ${location.column}`);
|
|
40
56
|
|
|
41
|
-
const getBoundSets = (bounds: Bound[]) =>
|
|
42
|
-
bounds
|
|
43
|
-
.filter((bound, index) => index === 0 || bounds[index - 1].set !== bound.set)
|
|
44
|
-
.map((bound) => bound.set);
|
|
45
|
-
|
|
46
|
-
const getOperands = (expression: Expression): Expression[] | undefined => {
|
|
47
|
-
switch (expression.kind) {
|
|
48
|
-
case "Apply":
|
|
49
|
-
return expression.args;
|
|
50
|
-
case "And":
|
|
51
|
-
case "Or":
|
|
52
|
-
return expression.operands;
|
|
53
|
-
case "If":
|
|
54
|
-
return [expression.condition, expression.consequent, expression.alternative];
|
|
55
|
-
case "Tuple":
|
|
56
|
-
case "SetEnumeration":
|
|
57
|
-
return expression.elements;
|
|
58
|
-
case "Record":
|
|
59
|
-
case "RecordSet":
|
|
60
|
-
return expression.fields.map((field) => field.value);
|
|
61
|
-
case "FunctionApplication":
|
|
62
|
-
return [
|
|
63
|
-
expression.function,
|
|
64
|
-
expression.args.length === 1
|
|
65
|
-
? expression.args[0]
|
|
66
|
-
: { kind: "Tuple", elements: expression.args, location: expression.location },
|
|
67
|
-
];
|
|
68
|
-
case "FieldAccess":
|
|
69
|
-
return [
|
|
70
|
-
expression.record,
|
|
71
|
-
{ kind: "String", value: expression.field, location: expression.location },
|
|
72
|
-
];
|
|
73
|
-
case "Prime":
|
|
74
|
-
case "Unchanged":
|
|
75
|
-
case "Enabled":
|
|
76
|
-
case "Always":
|
|
77
|
-
case "Eventually":
|
|
78
|
-
return [expression.expression];
|
|
79
|
-
case "BoxAction":
|
|
80
|
-
case "AngleAction":
|
|
81
|
-
return [expression.action, expression.subscript];
|
|
82
|
-
case "WeakFairness":
|
|
83
|
-
case "StrongFairness":
|
|
84
|
-
return [expression.subscript, expression.action];
|
|
85
|
-
case "FunctionSet":
|
|
86
|
-
return [expression.domain, expression.range];
|
|
87
|
-
case "Product":
|
|
88
|
-
return expression.factors;
|
|
89
|
-
case "Except":
|
|
90
|
-
return [expression.function];
|
|
91
|
-
default:
|
|
92
|
-
return undefined;
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
const getBinder = (
|
|
97
|
-
expression: Expression,
|
|
98
|
-
): { names: string[]; body: Expression; sets: Expression[] } | undefined => {
|
|
99
|
-
switch (expression.kind) {
|
|
100
|
-
case "Forall":
|
|
101
|
-
case "Exists":
|
|
102
|
-
case "FunctionConstruction":
|
|
103
|
-
return {
|
|
104
|
-
names: getBoundNames(expression.bounds),
|
|
105
|
-
body: expression.body,
|
|
106
|
-
sets: getBoundSets(expression.bounds),
|
|
107
|
-
};
|
|
108
|
-
case "SetMap":
|
|
109
|
-
return {
|
|
110
|
-
names: getBoundNames(expression.bounds),
|
|
111
|
-
body: expression.expression,
|
|
112
|
-
sets: getBoundSets(expression.bounds),
|
|
113
|
-
};
|
|
114
|
-
case "SetFilter":
|
|
115
|
-
return {
|
|
116
|
-
names: getPatternNames(expression.bound.pattern),
|
|
117
|
-
body: expression.predicate,
|
|
118
|
-
sets: [expression.bound.set],
|
|
119
|
-
};
|
|
120
|
-
case "Choose":
|
|
121
|
-
return {
|
|
122
|
-
names: getPatternNames(expression.pattern),
|
|
123
|
-
body: expression.body,
|
|
124
|
-
sets: expression.set ? [expression.set] : [],
|
|
125
|
-
};
|
|
126
|
-
case "UnboundedQuantifier":
|
|
127
|
-
case "Lambda":
|
|
128
|
-
return {
|
|
129
|
-
names: expression.kind === "Lambda" ? expression.params : expression.names,
|
|
130
|
-
body: expression.body,
|
|
131
|
-
sets: [],
|
|
132
|
-
};
|
|
133
|
-
default:
|
|
134
|
-
return undefined;
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
|
|
138
57
|
const findLabel = (
|
|
139
58
|
expression: Expression,
|
|
140
59
|
name: string,
|
|
@@ -153,9 +72,6 @@ const findLabel = (
|
|
|
153
72
|
return undefined;
|
|
154
73
|
};
|
|
155
74
|
|
|
156
|
-
const takeArguments = (args: Expression[], count: number, pool: Expression[]) =>
|
|
157
|
-
args.length === 0 && pool.length > 0 ? pool.splice(0, count) : args;
|
|
158
|
-
|
|
159
75
|
const wrapLets = (body: Expression, lets: Definition[][], location: SourceLocation) =>
|
|
160
76
|
lets.reduceRight<Expression>(
|
|
161
77
|
(inner, definitions) => ({ kind: "Let", definitions, body: inner, location }),
|
|
@@ -235,14 +151,30 @@ const applyDefinition = (definition: Definition, selection: Selection) => {
|
|
|
235
151
|
return createApplication({ ...selection, params: [...selection.params, ...params] }, reference);
|
|
236
152
|
};
|
|
237
153
|
|
|
154
|
+
// Generator.selectorToNode reports the first part of a name that selects nothing, after it analyzes
|
|
155
|
+
// the arguments of the parts before, and goes on after the name. An operator or a label must be given
|
|
156
|
+
// all its arguments, except in a name that an operator argument may select whole.
|
|
238
157
|
const selectSubexpression = (
|
|
239
158
|
selectors: Selector[],
|
|
240
159
|
lookup: Lookup,
|
|
241
|
-
|
|
242
|
-
|
|
160
|
+
reference: Expression & SelectingName,
|
|
161
|
+
isOperator: boolean,
|
|
243
162
|
): Expression => {
|
|
163
|
+
const location = reference.location;
|
|
244
164
|
const selection: Selection = { params: [], args: [], lets: [], location };
|
|
245
|
-
const state = { index: 0, current: undefined as Expression | undefined };
|
|
165
|
+
const state = { index: 0, current: undefined as Expression | undefined, isFailed: false };
|
|
166
|
+
|
|
167
|
+
const report = (range: SourceRange | undefined, message: string) => {
|
|
168
|
+
if (!range) throw createError(message, location);
|
|
169
|
+
selectionErrors.set(reference, {
|
|
170
|
+
args: selectors
|
|
171
|
+
.slice(0, state.index)
|
|
172
|
+
.flatMap((selector) => ("args" in selector ? selector.args : [])),
|
|
173
|
+
error: { location: formatRange(range), message },
|
|
174
|
+
});
|
|
175
|
+
state.isFailed = true;
|
|
176
|
+
return undefined;
|
|
177
|
+
};
|
|
246
178
|
|
|
247
179
|
const findDefinition = (local: Definition[] | undefined) => {
|
|
248
180
|
const names: string[] = [];
|
|
@@ -256,9 +188,8 @@ const selectSubexpression = (
|
|
|
256
188
|
const definition = local
|
|
257
189
|
? local.find((candidate) => getSourceName(candidate) === name)
|
|
258
190
|
: lookup(name);
|
|
259
|
-
if (definition)
|
|
260
|
-
return { definition, args:
|
|
261
|
-
}
|
|
191
|
+
if (definition)
|
|
192
|
+
return { definition, name, args, earlier: args.length - selector.args.length };
|
|
262
193
|
state.index++;
|
|
263
194
|
}
|
|
264
195
|
throw createError(`Unknown operator ${names.join("!")}`, location);
|
|
@@ -269,8 +200,11 @@ const selectSubexpression = (
|
|
|
269
200
|
const definition = found.definition;
|
|
270
201
|
const arity = getArity(definition);
|
|
271
202
|
const isLast = state.index === selectors.length - 1;
|
|
272
|
-
if (found.args.length !== arity && found.args.length !== 0) {
|
|
273
|
-
|
|
203
|
+
if (found.args.length !== arity && (!isOperator || found.args.length !== 0)) {
|
|
204
|
+
return report(
|
|
205
|
+
reference.argumentRanges?.[state.index] ?? location.range,
|
|
206
|
+
`The operator ${found.name} requires ${arity - found.earlier} arguments.`,
|
|
207
|
+
);
|
|
274
208
|
}
|
|
275
209
|
if (isLast) return { definition, args: found.args };
|
|
276
210
|
if (definition.kind !== "Operator") {
|
|
@@ -284,14 +218,19 @@ const selectSubexpression = (
|
|
|
284
218
|
};
|
|
285
219
|
|
|
286
220
|
const followLabels = () => {
|
|
287
|
-
while (state.index < selectors.length) {
|
|
221
|
+
while (state.index < selectors.length && !state.isFailed) {
|
|
288
222
|
const selector = selectors[state.index];
|
|
289
223
|
if (selector.kind !== "Name") return;
|
|
224
|
+
const range = reference.selectorRanges?.[state.index];
|
|
290
225
|
const found = state.current && findLabel(state.current, selector.name, []);
|
|
291
|
-
if (!found)
|
|
226
|
+
if (!found) return report(range, `Cannot find label \`${selector.name}'.`);
|
|
227
|
+
const params = found.label.params;
|
|
228
|
+
if (!isOperator && selector.args.length !== params.length) {
|
|
229
|
+
return report(range, `Label \`${selector.name}' used with wrong number of arguments.`);
|
|
230
|
+
}
|
|
292
231
|
selection.lets.push(...found.lets);
|
|
293
|
-
selection.params.push(...
|
|
294
|
-
selection.args.push(...
|
|
232
|
+
selection.params.push(...params.map((name) => ({ name, arity: 0 })));
|
|
233
|
+
selection.args.push(...selector.args);
|
|
295
234
|
state.current = found.label.body;
|
|
296
235
|
state.index++;
|
|
297
236
|
}
|
|
@@ -317,9 +256,7 @@ const selectSubexpression = (
|
|
|
317
256
|
const binder = getBinder(current);
|
|
318
257
|
if (binder && (selector.kind === "Bound" || selector.kind === "At")) {
|
|
319
258
|
selection.params.push(...binder.names.map((name) => ({ name, arity: 0 })));
|
|
320
|
-
if (selector.kind === "Bound")
|
|
321
|
-
selection.args.push(...takeArguments(selector.args, binder.names.length, pool));
|
|
322
|
-
}
|
|
259
|
+
if (selector.kind === "Bound") selection.args.push(...selector.args);
|
|
323
260
|
state.current = binder.body;
|
|
324
261
|
return;
|
|
325
262
|
}
|
|
@@ -349,9 +286,10 @@ const selectSubexpression = (
|
|
|
349
286
|
};
|
|
350
287
|
|
|
351
288
|
const first = enterDefinition(undefined);
|
|
289
|
+
if (state.isFailed) return reference;
|
|
352
290
|
if (first) return applyDefinition(first.definition, { ...selection, args: first.args });
|
|
353
291
|
followLabels();
|
|
354
|
-
while (state.index < selectors.length) {
|
|
292
|
+
while (state.index < selectors.length && !state.isFailed) {
|
|
355
293
|
const selector = selectors[state.index];
|
|
356
294
|
if (selector.kind === "Name") {
|
|
357
295
|
while (state.current?.kind === "Label") state.current = state.current.body;
|
|
@@ -360,6 +298,7 @@ const selectSubexpression = (
|
|
|
360
298
|
}
|
|
361
299
|
selection.lets.push(state.current.definitions);
|
|
362
300
|
const last = enterDefinition(state.current.definitions);
|
|
301
|
+
if (state.isFailed) return reference;
|
|
363
302
|
if (last) {
|
|
364
303
|
return applyDefinition(last.definition, {
|
|
365
304
|
...selection,
|
|
@@ -372,19 +311,22 @@ const selectSubexpression = (
|
|
|
372
311
|
selectOperand(selector);
|
|
373
312
|
state.index++;
|
|
374
313
|
}
|
|
314
|
+
if (state.isFailed) return reference;
|
|
375
315
|
if (!state.current) throw createError("Subexpression selection failed", location);
|
|
376
316
|
return createApplication(selection, state.current);
|
|
377
317
|
};
|
|
378
318
|
|
|
379
|
-
|
|
319
|
+
// A name such as Op(1)!lab(2) whose first parts name a definition selects part of it.
|
|
320
|
+
const getQualifiedSelectors = (
|
|
321
|
+
name: string,
|
|
322
|
+
args: Expression[],
|
|
323
|
+
counts: number[] | undefined,
|
|
324
|
+
lookup: Lookup,
|
|
325
|
+
) => {
|
|
380
326
|
if (!name.includes("!") || lookup(name)) return undefined;
|
|
381
327
|
const parts = name.split("!");
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
return [
|
|
385
|
-
{ kind: "Name", name: parts.slice(0, prefix + 1).join("!"), args: [] },
|
|
386
|
-
...parts.slice(prefix + 1).map((part): Selector => ({ kind: "Name", name: part, args: [] })),
|
|
387
|
-
];
|
|
328
|
+
if (!parts.some((_, index) => lookup(parts.slice(0, index + 1).join("!")))) return undefined;
|
|
329
|
+
return getNameSelectors(name, args, counts);
|
|
388
330
|
};
|
|
389
331
|
|
|
390
332
|
const containsReference = (expression: Expression): boolean =>
|
|
@@ -393,69 +335,175 @@ const containsReference = (expression: Expression): boolean =>
|
|
|
393
335
|
(expression.kind === "Apply" && expression.operator.includes("!")) ||
|
|
394
336
|
getChildren(expression).some(containsReference);
|
|
395
337
|
|
|
396
|
-
// A reference
|
|
338
|
+
// A reference that starts with a name isGoal holds for, such as a theorem whose statement is an
|
|
339
|
+
// ASSUME/PROVE, stays for name resolution to check. isOperator holds where an operator may be given,
|
|
340
|
+
// as the argument of an operator that may take one.
|
|
397
341
|
const resolveSubexpressions = (
|
|
398
342
|
expression: Expression,
|
|
399
343
|
lookup: Lookup,
|
|
400
|
-
|
|
344
|
+
isGoal: (name: string) => boolean,
|
|
345
|
+
isOperator = false,
|
|
401
346
|
): Expression => {
|
|
402
347
|
if (!containsReference(expression)) return expression;
|
|
403
348
|
if (expression.kind === "Let") {
|
|
404
349
|
const local: Lookup = (name) =>
|
|
405
350
|
expression.definitions.find((definition) => definition.name === name) ?? lookup(name);
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
351
|
+
const mapped = mapChildren(expression, (child) => resolveSubexpressions(child, local, isGoal));
|
|
352
|
+
return mapped.kind === "Let" && mapped.instances
|
|
353
|
+
? {
|
|
354
|
+
...mapped,
|
|
355
|
+
instances: mapped.instances.map((instance) => resolveInstance(instance, local, isGoal)),
|
|
356
|
+
}
|
|
357
|
+
: mapped;
|
|
413
358
|
}
|
|
414
|
-
const
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
359
|
+
const callee = expression.kind === "Apply" ? lookup(expression.operator) : undefined;
|
|
360
|
+
const mapped =
|
|
361
|
+
expression.kind === "Apply"
|
|
362
|
+
? {
|
|
363
|
+
...expression,
|
|
364
|
+
args: expression.args.map((arg, index) =>
|
|
365
|
+
resolveSubexpressions(
|
|
366
|
+
arg,
|
|
367
|
+
lookup,
|
|
368
|
+
isGoal,
|
|
369
|
+
callee?.kind !== "Operator" || callee.params[index]?.arity !== 0,
|
|
370
|
+
),
|
|
371
|
+
),
|
|
372
|
+
}
|
|
373
|
+
: mapChildren(expression, (child) => resolveSubexpressions(child, lookup, isGoal));
|
|
374
|
+
const selectors =
|
|
375
|
+
mapped.kind === "Subexpression"
|
|
376
|
+
? mapped.selectors
|
|
377
|
+
: mapped.kind === "Identifier"
|
|
378
|
+
? getQualifiedSelectors(mapped.name, [], undefined, lookup)
|
|
379
|
+
: mapped.kind === "Apply"
|
|
380
|
+
? getQualifiedSelectors(mapped.operator, mapped.args, mapped.argumentCounts, lookup)
|
|
381
|
+
: undefined;
|
|
382
|
+
const first = selectors?.[0];
|
|
383
|
+
if (!selectors || (first?.kind === "Name" && isGoal(first.name))) return mapped;
|
|
384
|
+
const selected = selectSubexpression(selectors, lookup, mapped, isOperator);
|
|
385
|
+
if (selected !== mapped) selections.add(selected);
|
|
386
|
+
return selected;
|
|
422
387
|
};
|
|
423
388
|
|
|
389
|
+
const resolveInstance = (
|
|
390
|
+
instance: Instance,
|
|
391
|
+
lookup: Lookup,
|
|
392
|
+
isGoal: (name: string) => boolean,
|
|
393
|
+
): Instance => ({
|
|
394
|
+
...instance,
|
|
395
|
+
substitutions: instance.substitutions.map((substitution) => ({
|
|
396
|
+
...substitution,
|
|
397
|
+
expression: resolveSubexpressions(substitution.expression, lookup, isGoal, true),
|
|
398
|
+
})),
|
|
399
|
+
});
|
|
400
|
+
|
|
424
401
|
const createLookup = (definitions: Definition[]): Lookup => {
|
|
425
402
|
const byName = new Map(definitions.map((definition) => [definition.name, definition]));
|
|
426
403
|
return (name) => byName.get(name);
|
|
427
404
|
};
|
|
428
405
|
|
|
429
|
-
export const resolveInstanceSubexpressions = (instance: Instance, module: Module): Instance =>
|
|
430
|
-
|
|
431
|
-
return {
|
|
432
|
-
...instance,
|
|
433
|
-
substitutions: instance.substitutions.map((substitution) => ({
|
|
434
|
-
name: substitution.name,
|
|
435
|
-
expression: resolveSubexpressions(substitution.expression, lookup, new Set()),
|
|
436
|
-
})),
|
|
437
|
-
};
|
|
438
|
-
};
|
|
406
|
+
export const resolveInstanceSubexpressions = (instance: Instance, module: Module): Instance =>
|
|
407
|
+
resolveInstance(instance, createLookup(module.definitions), () => false);
|
|
439
408
|
|
|
409
|
+
// Generator.selectorToNode resolves a name in a theorem, a proof, or a fact as it does in a definition.
|
|
440
410
|
export const resolveModuleSubexpressions = (module: Module, available: Definition[]): Module => {
|
|
441
411
|
const lookup = createLookup([...available, ...module.definitions]);
|
|
442
|
-
const hasReferences =
|
|
443
|
-
module.definitions.some((definition) => containsReference(definition.body)) ||
|
|
444
|
-
module.assumptions.some((assumption) => containsReference(assumption.expression));
|
|
445
|
-
if (!hasReferences) return module;
|
|
446
412
|
const goals = new Set(
|
|
447
413
|
module.theorems.flatMap((theorem) =>
|
|
448
414
|
theorem.name !== undefined && theorem.statement.kind === "AssumeProve" ? [theorem.name] : [],
|
|
449
415
|
),
|
|
450
416
|
);
|
|
451
|
-
|
|
417
|
+
const isGoal = (name: string) => goals.has(name);
|
|
418
|
+
const resolve = (expression: Expression) => resolveSubexpressions(expression, lookup, isGoal);
|
|
419
|
+
// A name in a theorem, a proof, or a fact may also start with a step, a theorem, or a symbol that
|
|
420
|
+
// no definition names.
|
|
421
|
+
const isProofGoal = (name: string) => isGoal(name) || !lookup(name);
|
|
422
|
+
const resolveInProof = (expression: Expression) =>
|
|
423
|
+
resolveSubexpressions(expression, lookup, isProofGoal);
|
|
424
|
+
const resolveFacts = (facts: Fact[]) =>
|
|
425
|
+
facts.map((fact) => (fact.kind === "ModuleFact" ? fact : resolveInProof(fact)));
|
|
426
|
+
const resolveBounds = (bounds: Bound[]) =>
|
|
427
|
+
bounds.map((bound) => ({ ...bound, set: resolveInProof(bound.set) }));
|
|
428
|
+
const resolveStatement = (statement: Expression | AssumeProve): Expression | AssumeProve =>
|
|
429
|
+
statement.kind === "AssumeProve"
|
|
430
|
+
? {
|
|
431
|
+
...statement,
|
|
432
|
+
assumptions: statement.assumptions.map((assumption) =>
|
|
433
|
+
assumption.kind === "NewSymbol"
|
|
434
|
+
? { ...assumption, set: assumption.set && resolveInProof(assumption.set) }
|
|
435
|
+
: resolveStatement(assumption),
|
|
436
|
+
),
|
|
437
|
+
conclusion: resolveInProof(statement.conclusion),
|
|
438
|
+
}
|
|
439
|
+
: resolveInProof(statement);
|
|
440
|
+
const resolveStep = (body: StepBody): StepBody => {
|
|
441
|
+
switch (body.kind) {
|
|
442
|
+
case "Use":
|
|
443
|
+
case "Hide":
|
|
444
|
+
return { ...body, facts: resolveFacts(body.facts) };
|
|
445
|
+
case "Instance":
|
|
446
|
+
return resolveInstance(body, lookup, isProofGoal);
|
|
447
|
+
case "Define":
|
|
448
|
+
return {
|
|
449
|
+
...body,
|
|
450
|
+
definitions: body.definitions.map((definition) =>
|
|
451
|
+
definition.kind === "Instance"
|
|
452
|
+
? resolveInstance(definition, lookup, isProofGoal)
|
|
453
|
+
: mapDefinition(definition, resolveInProof),
|
|
454
|
+
),
|
|
455
|
+
};
|
|
456
|
+
case "Have":
|
|
457
|
+
case "Case":
|
|
458
|
+
return { ...body, expression: resolveInProof(body.expression) };
|
|
459
|
+
case "Witness":
|
|
460
|
+
return { ...body, expressions: body.expressions.map(resolveInProof) };
|
|
461
|
+
case "Take":
|
|
462
|
+
return { ...body, bounds: resolveBounds(body.bounds) };
|
|
463
|
+
case "Pick":
|
|
464
|
+
return { ...body, bounds: resolveBounds(body.bounds), body: resolveInProof(body.body) };
|
|
465
|
+
case "Assert":
|
|
466
|
+
return { ...body, statement: resolveStatement(body.statement) };
|
|
467
|
+
default:
|
|
468
|
+
return body;
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
const resolveProof = (proof: Proof | undefined): Proof | undefined =>
|
|
472
|
+
proof?.kind === "By"
|
|
473
|
+
? { ...proof, facts: resolveFacts(proof.facts) }
|
|
474
|
+
: proof?.kind === "Steps"
|
|
475
|
+
? {
|
|
476
|
+
...proof,
|
|
477
|
+
steps: proof.steps.map((step) => ({
|
|
478
|
+
...step,
|
|
479
|
+
body: resolveStep(step.body),
|
|
480
|
+
proof: resolveProof(step.proof),
|
|
481
|
+
})),
|
|
482
|
+
}
|
|
483
|
+
: proof;
|
|
484
|
+
const resolved = {
|
|
452
485
|
...module,
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
486
|
+
// A named theorem's statement is resolved as its definition.
|
|
487
|
+
theorems: module.theorems.map((theorem) => ({
|
|
488
|
+
...theorem,
|
|
489
|
+
statement:
|
|
490
|
+
theorem.name !== undefined && theorem.statement.kind !== "AssumeProve"
|
|
491
|
+
? theorem.statement
|
|
492
|
+
: resolveStatement(theorem.statement),
|
|
493
|
+
proof: resolveProof(theorem.proof),
|
|
494
|
+
})),
|
|
495
|
+
uses: module.uses.map((use) => ({ ...use, facts: resolveFacts(use.facts) })),
|
|
496
|
+
};
|
|
497
|
+
const hasReferences =
|
|
498
|
+
module.definitions.some((definition) => containsReference(definition.body)) ||
|
|
499
|
+
module.assumptions.some((assumption) => containsReference(assumption.expression));
|
|
500
|
+
if (!hasReferences) return resolved;
|
|
501
|
+
return {
|
|
502
|
+
...resolved,
|
|
503
|
+
definitions: module.definitions.map((definition) => mapDefinition(definition, resolve)),
|
|
456
504
|
assumptions: module.assumptions.map((assumption) => ({
|
|
457
505
|
...assumption,
|
|
458
|
-
expression:
|
|
506
|
+
expression: resolve(assumption.expression),
|
|
459
507
|
})),
|
|
460
508
|
};
|
|
461
509
|
};
|