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/instances.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
isLocallyResolved,
|
|
4
|
+
type Bound,
|
|
5
|
+
type BoundPattern,
|
|
6
|
+
type Definition,
|
|
7
|
+
type Expression,
|
|
8
|
+
type Instance,
|
|
9
|
+
type Module,
|
|
10
|
+
type OperatorParam,
|
|
11
|
+
type SourceLocation,
|
|
12
|
+
type Theorem,
|
|
10
13
|
} from "./ast";
|
|
11
14
|
import { TlaSyntaxError } from "./errors";
|
|
12
|
-
import {
|
|
13
|
-
import { isBefore } from "./resolve";
|
|
14
|
-
|
|
15
|
-
const getPatternNames = (pattern: BoundPattern) =>
|
|
16
|
-
pattern.kind === "Name" ? [pattern.name] : pattern.names;
|
|
17
|
-
|
|
18
|
-
const getBoundNames = (bounds: Bound[]) =>
|
|
19
|
-
bounds.flatMap((bound) => getPatternNames(bound.pattern));
|
|
15
|
+
import { getArity, getBoundNames, getPatternNames, isBefore } from "./resolve";
|
|
20
16
|
|
|
21
17
|
export const substitutedExpressions = new WeakSet<Expression>();
|
|
22
18
|
|
|
19
|
+
// Applications inside an instance with substitutions or parameters, which TLC evaluates in a
|
|
20
|
+
// context that binds them.
|
|
21
|
+
export const contextualApplications = new WeakSet<Expression>();
|
|
22
|
+
|
|
23
23
|
export const renameLocals = (
|
|
24
24
|
expression: Expression,
|
|
25
25
|
names: Map<string, string>,
|
|
@@ -41,26 +41,39 @@ export const renameLocals = (
|
|
|
41
41
|
return mapped;
|
|
42
42
|
};
|
|
43
43
|
|
|
44
|
-
const mapBounds = (bounds: Bound[], map: (child: Expression) => Expression)
|
|
44
|
+
const mapBounds = (bounds: Bound[], map: (child: Expression) => Expression) =>
|
|
45
45
|
bounds.map((bound) => ({ pattern: bound.pattern, set: map(bound.set) }));
|
|
46
46
|
|
|
47
|
+
const sourceNames = new WeakMap<Definition, string>();
|
|
48
|
+
|
|
49
|
+
export const getSourceName = (definition: Definition) =>
|
|
50
|
+
sourceNames.get(definition) ?? definition.name;
|
|
51
|
+
|
|
52
|
+
const withSourceName = (definition: Definition, source: Definition) => {
|
|
53
|
+
if (getSourceName(source) !== definition.name) sourceNames.set(definition, getSourceName(source));
|
|
54
|
+
return definition;
|
|
55
|
+
};
|
|
56
|
+
|
|
47
57
|
export const mapDefinition = (
|
|
48
58
|
definition: Definition,
|
|
49
59
|
map: (child: Expression, bound: string[]) => Expression,
|
|
50
60
|
): Definition =>
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
withSourceName(
|
|
62
|
+
definition.kind === "Function"
|
|
63
|
+
? {
|
|
64
|
+
...definition,
|
|
65
|
+
bounds: mapBounds(definition.bounds, (child) => map(child, [])),
|
|
66
|
+
body: map(definition.body, getBoundNames(definition.bounds)),
|
|
67
|
+
}
|
|
68
|
+
: {
|
|
69
|
+
...definition,
|
|
70
|
+
body: map(
|
|
71
|
+
definition.body,
|
|
72
|
+
definition.params.map((param) => param.name),
|
|
73
|
+
),
|
|
74
|
+
},
|
|
75
|
+
definition,
|
|
76
|
+
);
|
|
64
77
|
|
|
65
78
|
export const mapChildren = (
|
|
66
79
|
expression: Expression,
|
|
@@ -69,6 +82,7 @@ export const mapChildren = (
|
|
|
69
82
|
const plain = (child: Expression) => map(child, []);
|
|
70
83
|
switch (expression.kind) {
|
|
71
84
|
case "Number":
|
|
85
|
+
case "Decimal":
|
|
72
86
|
case "String":
|
|
73
87
|
case "Identifier":
|
|
74
88
|
case "OperatorReference":
|
|
@@ -199,29 +213,131 @@ export const mapChildren = (
|
|
|
199
213
|
}
|
|
200
214
|
};
|
|
201
215
|
|
|
202
|
-
|
|
216
|
+
interface Instantiation {
|
|
203
217
|
params: OperatorParam[];
|
|
204
218
|
substitutions: Map<string, Expression>;
|
|
205
219
|
variables: Set<string>;
|
|
206
220
|
getDefinition: (key: string) => Definition | undefined;
|
|
207
221
|
isParameterized: (key: string) => boolean;
|
|
208
|
-
addDefinition: (name: string, create: (key: string) => Definition) => string;
|
|
222
|
+
addDefinition: (source: string, name: string, create: (key: string) => Definition) => string;
|
|
223
|
+
createName: (name: string) => string;
|
|
209
224
|
}
|
|
210
225
|
|
|
226
|
+
// LAMBDA is a keyword, so no name bound inside the instance can capture it.
|
|
227
|
+
const LAMBDA_NAME = "LAMBDA";
|
|
228
|
+
|
|
211
229
|
type RewriteMode = "Normal" | "Enabled" | "Primed";
|
|
212
230
|
|
|
213
231
|
interface Rewrite {
|
|
214
232
|
instantiation: Instantiation;
|
|
233
|
+
substitutedNames: Set<string>;
|
|
215
234
|
copies: Map<string, string>;
|
|
216
235
|
mode: RewriteMode;
|
|
217
236
|
modes: Map<RewriteMode, Rewrite>;
|
|
218
237
|
}
|
|
219
238
|
|
|
239
|
+
const getFreeNames = (expression: Expression, bound = new Set<string>()): string[] => {
|
|
240
|
+
const name =
|
|
241
|
+
expression.kind === "Identifier"
|
|
242
|
+
? expression.name
|
|
243
|
+
: expression.kind === "Apply" || expression.kind === "OperatorReference"
|
|
244
|
+
? expression.operator
|
|
245
|
+
: undefined;
|
|
246
|
+
const names = name === undefined || bound.has(name) ? [] : [name];
|
|
247
|
+
mapChildren(expression, (child, childBound) => {
|
|
248
|
+
names.push(
|
|
249
|
+
...getFreeNames(child, childBound.length === 0 ? bound : new Set([...bound, ...childBound])),
|
|
250
|
+
);
|
|
251
|
+
return child;
|
|
252
|
+
});
|
|
253
|
+
return names;
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
const createRenamer = (rewrite: Rewrite) => {
|
|
257
|
+
const renamed = new Map<string, string>();
|
|
258
|
+
return (name: string) => {
|
|
259
|
+
if (!rewrite.substitutedNames.has(name)) return name;
|
|
260
|
+
const fresh = renamed.get(name) ?? rewrite.instantiation.createName(name);
|
|
261
|
+
renamed.set(name, fresh);
|
|
262
|
+
return fresh;
|
|
263
|
+
};
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
const bindNames = (bound: Map<string, string>, names: string[], rename: (name: string) => string) =>
|
|
267
|
+
new Map([...bound, ...names.map((name): [string, string] => [name, rename(name)])]);
|
|
268
|
+
|
|
269
|
+
const renamePattern = (pattern: BoundPattern, rename: (name: string) => string): BoundPattern =>
|
|
270
|
+
pattern.kind === "Name"
|
|
271
|
+
? { kind: "Name", name: rename(pattern.name) }
|
|
272
|
+
: { kind: "Tuple", names: pattern.names.map(rename) };
|
|
273
|
+
|
|
274
|
+
const renameBounds = (bounds: Bound[], rename: (name: string) => string) =>
|
|
275
|
+
bounds.map((bound) => ({ pattern: renamePattern(bound.pattern, rename), set: bound.set }));
|
|
276
|
+
|
|
277
|
+
const renameParams = (definition: Definition, rename: (name: string) => string): Definition =>
|
|
278
|
+
definition.kind === "Function"
|
|
279
|
+
? { ...definition, bounds: renameBounds(definition.bounds, rename) }
|
|
280
|
+
: {
|
|
281
|
+
...definition,
|
|
282
|
+
params: definition.params.map((param) => ({
|
|
283
|
+
name: rename(param.name),
|
|
284
|
+
arity: param.arity,
|
|
285
|
+
})),
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
const renameNames = (
|
|
289
|
+
expression: Expression,
|
|
290
|
+
bound: Map<string, string>,
|
|
291
|
+
rename: (name: string) => string,
|
|
292
|
+
): Expression => {
|
|
293
|
+
const getBoundName = (name: string) => bound.get(name) ?? name;
|
|
294
|
+
switch (expression.kind) {
|
|
295
|
+
case "Identifier":
|
|
296
|
+
return { ...expression, name: getBoundName(expression.name) };
|
|
297
|
+
case "Apply":
|
|
298
|
+
case "OperatorReference":
|
|
299
|
+
return { ...expression, operator: getBoundName(expression.operator) };
|
|
300
|
+
case "Label":
|
|
301
|
+
return { ...expression, params: expression.params.map(getBoundName) };
|
|
302
|
+
case "Lambda":
|
|
303
|
+
return { ...expression, params: expression.params.map(rename) };
|
|
304
|
+
case "Let":
|
|
305
|
+
return {
|
|
306
|
+
...expression,
|
|
307
|
+
definitions: expression.definitions.map((definition) =>
|
|
308
|
+
withSourceName(
|
|
309
|
+
{ ...renameParams(definition, rename), name: rename(definition.name) },
|
|
310
|
+
definition,
|
|
311
|
+
),
|
|
312
|
+
),
|
|
313
|
+
};
|
|
314
|
+
case "Forall":
|
|
315
|
+
case "Exists":
|
|
316
|
+
case "FunctionConstruction":
|
|
317
|
+
case "SetMap":
|
|
318
|
+
return { ...expression, bounds: renameBounds(expression.bounds, rename) };
|
|
319
|
+
case "UnboundedQuantifier":
|
|
320
|
+
return { ...expression, names: expression.names.map(rename) };
|
|
321
|
+
case "Choose":
|
|
322
|
+
return { ...expression, pattern: renamePattern(expression.pattern, rename) };
|
|
323
|
+
case "SetFilter":
|
|
324
|
+
return {
|
|
325
|
+
...expression,
|
|
326
|
+
bound: {
|
|
327
|
+
pattern: renamePattern(expression.bound.pattern, rename),
|
|
328
|
+
set: expression.bound.set,
|
|
329
|
+
},
|
|
330
|
+
};
|
|
331
|
+
default:
|
|
332
|
+
return expression;
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
|
|
220
336
|
export const getEnabledVariableName = (name: string) => `${name}@ENABLED`;
|
|
221
337
|
|
|
222
338
|
const getMode = (rewrite: Rewrite, mode: RewriteMode) => rewrite.modes.get(mode) ?? rewrite;
|
|
223
339
|
|
|
224
|
-
const isVariable = (expression: Expression, rewrite: Rewrite, bound:
|
|
340
|
+
const isVariable = (expression: Expression, rewrite: Rewrite, bound: Map<string, string>) =>
|
|
225
341
|
expression.kind === "Identifier" &&
|
|
226
342
|
!bound.has(expression.name) &&
|
|
227
343
|
!isLocallyResolved(expression) &&
|
|
@@ -230,7 +346,7 @@ const isVariable = (expression: Expression, rewrite: Rewrite, bound: Set<string>
|
|
|
230
346
|
const getUnchangedVariables = (
|
|
231
347
|
expression: Expression,
|
|
232
348
|
rewrite: Rewrite,
|
|
233
|
-
bound:
|
|
349
|
+
bound: Map<string, string>,
|
|
234
350
|
): string[] | undefined => {
|
|
235
351
|
if (expression.kind === "Tuple") {
|
|
236
352
|
const names = expression.elements.map((element) =>
|
|
@@ -244,14 +360,14 @@ const getUnchangedVariables = (
|
|
|
244
360
|
? undefined
|
|
245
361
|
: rewrite.instantiation.getDefinition(expression.name);
|
|
246
362
|
return definition?.kind === "Operator" && definition.params.length === 0
|
|
247
|
-
? getUnchangedVariables(definition.body, rewrite, new
|
|
363
|
+
? getUnchangedVariables(definition.body, rewrite, new Map())
|
|
248
364
|
: undefined;
|
|
249
365
|
};
|
|
250
366
|
|
|
251
367
|
const substituteInEnabled = (
|
|
252
368
|
expression: Expression,
|
|
253
369
|
rewrite: Rewrite,
|
|
254
|
-
bound:
|
|
370
|
+
bound: Map<string, string>,
|
|
255
371
|
): Expression | undefined => {
|
|
256
372
|
if (rewrite.mode === "Normal") return undefined;
|
|
257
373
|
if (rewrite.mode === "Primed") {
|
|
@@ -318,16 +434,15 @@ const substituteInEnabled = (
|
|
|
318
434
|
};
|
|
319
435
|
};
|
|
320
436
|
|
|
321
|
-
const
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
}));
|
|
437
|
+
const createLocalIdentifier = (name: string, location: SourceLocation): Expression => ({
|
|
438
|
+
kind: "Identifier",
|
|
439
|
+
name,
|
|
440
|
+
location,
|
|
441
|
+
resolution: { kind: "Local" },
|
|
442
|
+
});
|
|
328
443
|
|
|
329
|
-
const
|
|
330
|
-
|
|
444
|
+
const getParamArguments = (params: OperatorParam[], location: SourceLocation) =>
|
|
445
|
+
params.map((param) => createLocalIdentifier(param.name, location));
|
|
331
446
|
|
|
332
447
|
const getInstanceOperator = (
|
|
333
448
|
key: string,
|
|
@@ -344,12 +459,7 @@ const getInstanceOperator = (
|
|
|
344
459
|
operator: key,
|
|
345
460
|
args: [
|
|
346
461
|
...getParamArguments(params, location),
|
|
347
|
-
...names.map((name)
|
|
348
|
-
kind: "Identifier",
|
|
349
|
-
name,
|
|
350
|
-
location,
|
|
351
|
-
resolution: { kind: "Local" },
|
|
352
|
-
})),
|
|
462
|
+
...names.map((name) => createLocalIdentifier(name, location)),
|
|
353
463
|
],
|
|
354
464
|
location,
|
|
355
465
|
},
|
|
@@ -360,7 +470,11 @@ const getInstanceOperator = (
|
|
|
360
470
|
const getCopy = (rewrite: Rewrite, key: string) =>
|
|
361
471
|
rewrite.instantiation.isParameterized(key) ? copyDefinition(rewrite, key) : undefined;
|
|
362
472
|
|
|
363
|
-
const substitute = (
|
|
473
|
+
const substitute = (
|
|
474
|
+
expression: Expression,
|
|
475
|
+
rewrite: Rewrite,
|
|
476
|
+
bound: Map<string, string>,
|
|
477
|
+
): Expression => {
|
|
364
478
|
const recurse = (node: Expression) => substitute(node, rewrite, bound);
|
|
365
479
|
const instantiation = rewrite.instantiation;
|
|
366
480
|
const params = instantiation.params;
|
|
@@ -375,7 +489,6 @@ const substitute = (expression: Expression, rewrite: Rewrite, bound: Set<string>
|
|
|
375
489
|
(expression.kind === "WeakFairness" || expression.kind === "StrongFairness") &&
|
|
376
490
|
rewrite.mode === "Normal"
|
|
377
491
|
) {
|
|
378
|
-
const enabled = getMode(rewrite, "Enabled");
|
|
379
492
|
return {
|
|
380
493
|
...expression,
|
|
381
494
|
subscript: recurse(expression.subscript),
|
|
@@ -387,7 +500,7 @@ const substitute = (expression: Expression, rewrite: Rewrite, bound: Set<string>
|
|
|
387
500
|
subscript: expression.subscript,
|
|
388
501
|
location: expression.location,
|
|
389
502
|
},
|
|
390
|
-
|
|
503
|
+
getMode(rewrite, "Enabled"),
|
|
391
504
|
bound,
|
|
392
505
|
),
|
|
393
506
|
};
|
|
@@ -417,22 +530,30 @@ const substitute = (expression: Expression, rewrite: Rewrite, bound: Set<string>
|
|
|
417
530
|
if (expression.kind === "Apply" && isFree(expression.operator)) {
|
|
418
531
|
const args = expression.args.map(recurse);
|
|
419
532
|
const replacement = instantiation.substitutions.get(expression.operator);
|
|
533
|
+
const isContextual =
|
|
534
|
+
instantiation.substitutions.size > 0 ||
|
|
535
|
+
params.length > 0 ||
|
|
536
|
+
contextualApplications.has(expression);
|
|
537
|
+
const mark = (rewritten: Expression) => {
|
|
538
|
+
if (isContextual) contextualApplications.add(rewritten);
|
|
539
|
+
return rewritten;
|
|
540
|
+
};
|
|
420
541
|
if (replacement?.kind === "Identifier" || replacement?.kind === "OperatorReference") {
|
|
421
542
|
const operator = replacement.kind === "Identifier" ? replacement.name : replacement.operator;
|
|
422
|
-
return {
|
|
543
|
+
return mark({
|
|
423
544
|
...expression,
|
|
424
545
|
operator,
|
|
425
546
|
args,
|
|
426
547
|
resolution: replacement.kind === "Identifier" ? replacement.resolution : undefined,
|
|
427
|
-
};
|
|
548
|
+
});
|
|
428
549
|
}
|
|
429
550
|
if (replacement?.kind === "Apply") {
|
|
430
|
-
return {
|
|
551
|
+
return mark({
|
|
431
552
|
...expression,
|
|
432
553
|
operator: replacement.operator,
|
|
433
554
|
args: [...replacement.args, ...args],
|
|
434
555
|
resolution: replacement.resolution,
|
|
435
|
-
};
|
|
556
|
+
});
|
|
436
557
|
}
|
|
437
558
|
if (replacement?.kind === "Lambda") {
|
|
438
559
|
return {
|
|
@@ -440,7 +561,7 @@ const substitute = (expression: Expression, rewrite: Rewrite, bound: Set<string>
|
|
|
440
561
|
definitions: [
|
|
441
562
|
{
|
|
442
563
|
kind: "Operator",
|
|
443
|
-
name:
|
|
564
|
+
name: LAMBDA_NAME,
|
|
444
565
|
params: replacement.params.map((name) => ({ name, arity: 0 })),
|
|
445
566
|
body: replacement.body,
|
|
446
567
|
isLocal: true,
|
|
@@ -450,7 +571,7 @@ const substitute = (expression: Expression, rewrite: Rewrite, bound: Set<string>
|
|
|
450
571
|
],
|
|
451
572
|
body: {
|
|
452
573
|
...expression,
|
|
453
|
-
operator:
|
|
574
|
+
operator: LAMBDA_NAME,
|
|
454
575
|
args,
|
|
455
576
|
resolution: { kind: "Local" },
|
|
456
577
|
},
|
|
@@ -463,7 +584,7 @@ const substitute = (expression: Expression, rewrite: Rewrite, bound: Set<string>
|
|
|
463
584
|
);
|
|
464
585
|
}
|
|
465
586
|
const copy = getCopy(rewrite, expression.operator);
|
|
466
|
-
if (copy === undefined) return { ...expression, args };
|
|
587
|
+
if (copy === undefined) return mark({ ...expression, args });
|
|
467
588
|
return {
|
|
468
589
|
...expression,
|
|
469
590
|
operator: copy,
|
|
@@ -479,9 +600,13 @@ const substitute = (expression: Expression, rewrite: Rewrite, bound: Set<string>
|
|
|
479
600
|
}
|
|
480
601
|
return { ...expression, operator: copy };
|
|
481
602
|
}
|
|
482
|
-
|
|
483
|
-
|
|
603
|
+
const rename = createRenamer(rewrite);
|
|
604
|
+
const mapped = mapChildren(expression, (child, names) =>
|
|
605
|
+
names.length === 0
|
|
606
|
+
? recurse(child)
|
|
607
|
+
: substitute(child, rewrite, bindNames(bound, names, rename)),
|
|
484
608
|
);
|
|
609
|
+
return renameNames(mapped, bound, rename);
|
|
485
610
|
};
|
|
486
611
|
|
|
487
612
|
const withInstanceParams = (
|
|
@@ -500,7 +625,7 @@ const withInstanceParams = (
|
|
|
500
625
|
body: {
|
|
501
626
|
kind: "Let",
|
|
502
627
|
definitions: [definition],
|
|
503
|
-
body:
|
|
628
|
+
body: createLocalIdentifier(definition.name, location),
|
|
504
629
|
location,
|
|
505
630
|
},
|
|
506
631
|
isLocal: definition.isLocal,
|
|
@@ -515,10 +640,14 @@ const copyDefinition = (rewrite: Rewrite, key: string): string => {
|
|
|
515
640
|
const instantiation = rewrite.instantiation;
|
|
516
641
|
const definition = instantiation.getDefinition(key);
|
|
517
642
|
if (!definition) return key;
|
|
518
|
-
return instantiation.addDefinition(definition.name, (copyKey) => {
|
|
643
|
+
return instantiation.addDefinition(key, definition.name, (copyKey) => {
|
|
519
644
|
rewrite.copies.set(key, copyKey);
|
|
520
|
-
const
|
|
521
|
-
|
|
645
|
+
const rename = createRenamer(rewrite);
|
|
646
|
+
const mapped = renameParams(
|
|
647
|
+
mapDefinition(definition, (child, bound) =>
|
|
648
|
+
substitute(child, rewrite, bindNames(new Map(), bound, rename)),
|
|
649
|
+
),
|
|
650
|
+
rename,
|
|
522
651
|
);
|
|
523
652
|
return withInstanceParams(mapped, instantiation.params, definition.location);
|
|
524
653
|
});
|
|
@@ -526,8 +655,17 @@ const copyDefinition = (rewrite: Rewrite, key: string): string => {
|
|
|
526
655
|
|
|
527
656
|
export const createInstantiation = (instantiation: Instantiation) => {
|
|
528
657
|
const modes = new Map<RewriteMode, Rewrite>();
|
|
658
|
+
const substitutedNames = new Set(
|
|
659
|
+
[...instantiation.substitutions.values()].flatMap((expression) => getFreeNames(expression)),
|
|
660
|
+
);
|
|
529
661
|
const createRewrite = (mode: RewriteMode): Rewrite => {
|
|
530
|
-
const created = {
|
|
662
|
+
const created = {
|
|
663
|
+
instantiation,
|
|
664
|
+
substitutedNames,
|
|
665
|
+
copies: new Map<string, string>(),
|
|
666
|
+
mode,
|
|
667
|
+
modes,
|
|
668
|
+
};
|
|
531
669
|
modes.set(mode, created);
|
|
532
670
|
return created;
|
|
533
671
|
};
|
|
@@ -541,7 +679,7 @@ export const createInstantiation = (instantiation: Instantiation) => {
|
|
|
541
679
|
if (copy !== undefined) return copy;
|
|
542
680
|
const definition = instantiation.getDefinition(key);
|
|
543
681
|
if (!definition) return key;
|
|
544
|
-
return instantiation.addDefinition(definition.name, () =>
|
|
682
|
+
return instantiation.addDefinition(key, definition.name, () =>
|
|
545
683
|
withInstanceParams(definition, instantiation.params, definition.location),
|
|
546
684
|
);
|
|
547
685
|
},
|
|
@@ -554,36 +692,54 @@ const hasLetInstance = (expression: Expression): boolean =>
|
|
|
554
692
|
|
|
555
693
|
const expandLetInstances = (
|
|
556
694
|
expression: Expression,
|
|
557
|
-
instantiate: (instance: Instance) => Definition[],
|
|
695
|
+
instantiate: (instance: Instance, lets: Definition[]) => Definition[],
|
|
696
|
+
lets: Definition[],
|
|
558
697
|
): Expression => {
|
|
559
|
-
const
|
|
698
|
+
const inner = expression.kind === "Let" ? [...lets, ...expression.definitions] : lets;
|
|
699
|
+
const mapped = mapChildren(expression, (child) => expandLetInstances(child, instantiate, inner));
|
|
560
700
|
if (mapped.kind !== "Let" || !mapped.instances) return mapped;
|
|
561
701
|
const definitions = mapped.instances.reduce((current, instance) => {
|
|
562
702
|
const position = current.findIndex((definition) =>
|
|
563
703
|
isBefore(instance.location, definition.location),
|
|
564
704
|
);
|
|
565
705
|
const index = position === -1 ? current.length : position;
|
|
566
|
-
return [...current.slice(0, index), ...instantiate(instance), ...current.slice(index)];
|
|
706
|
+
return [...current.slice(0, index), ...instantiate(instance, inner), ...current.slice(index)];
|
|
567
707
|
}, mapped.definitions);
|
|
568
708
|
return { kind: "Let", definitions, body: mapped.body, location: mapped.location };
|
|
569
709
|
};
|
|
570
710
|
|
|
711
|
+
// A named theorem's statement is linked as its definition.
|
|
712
|
+
const getUnnamedStatement = (theorem: Theorem) =>
|
|
713
|
+
theorem.name === undefined && theorem.statement.kind !== "AssumeProve"
|
|
714
|
+
? theorem.statement
|
|
715
|
+
: undefined;
|
|
716
|
+
|
|
571
717
|
export const withLetInstances = (
|
|
572
718
|
module: Module,
|
|
573
|
-
instantiate: (instance: Instance) => Definition[],
|
|
719
|
+
instantiate: (instance: Instance, lets: Definition[]) => Definition[],
|
|
574
720
|
): Module => {
|
|
575
|
-
const
|
|
721
|
+
const hasLetInstances =
|
|
576
722
|
module.definitions.some((definition) => hasLetInstance(definition.body)) ||
|
|
577
|
-
module.assumptions.some((assumption) => hasLetInstance(assumption.expression))
|
|
578
|
-
|
|
723
|
+
module.assumptions.some((assumption) => hasLetInstance(assumption.expression)) ||
|
|
724
|
+
module.theorems.some((theorem) => {
|
|
725
|
+
const statement = getUnnamedStatement(theorem);
|
|
726
|
+
return statement !== undefined && hasLetInstance(statement);
|
|
727
|
+
});
|
|
728
|
+
if (!hasLetInstances) return module;
|
|
579
729
|
return {
|
|
580
730
|
...module,
|
|
581
731
|
definitions: module.definitions.map((definition) =>
|
|
582
|
-
mapDefinition(definition, (child) => expandLetInstances(child, instantiate)),
|
|
732
|
+
mapDefinition(definition, (child) => expandLetInstances(child, instantiate, [])),
|
|
583
733
|
),
|
|
584
734
|
assumptions: module.assumptions.map((assumption) => ({
|
|
585
735
|
...assumption,
|
|
586
|
-
expression: expandLetInstances(assumption.expression, instantiate),
|
|
736
|
+
expression: expandLetInstances(assumption.expression, instantiate, []),
|
|
587
737
|
})),
|
|
738
|
+
theorems: module.theorems.map((theorem) => {
|
|
739
|
+
const statement = getUnnamedStatement(theorem);
|
|
740
|
+
return statement === undefined
|
|
741
|
+
? theorem
|
|
742
|
+
: { ...theorem, statement: expandLetInstances(statement, instantiate, []) };
|
|
743
|
+
}),
|
|
588
744
|
};
|
|
589
745
|
};
|