tla-ts 0.0.4-dev.b254db0 → 0.0.4-dev.ca40dfe
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 +31 -10
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/deciders.cjs +27 -27
- package/dist/deciders.js +34 -34
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +12 -14
- package/dist/index.d.ts +12 -14
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/prove-worker.cjs +1 -1
- package/dist/prove-worker.js +1 -1
- package/dist/prove.cjs +37 -27
- package/dist/prove.js +39 -29
- package/dist/run.cjs +45 -0
- package/dist/run.js +45 -0
- package/dist/tlapm.cjs +2 -2
- package/dist/tlapm.js +2 -2
- package/dist/transfer.cjs +76 -86
- package/dist/transfer.js +73 -83
- package/dist/worker.cjs +6 -6
- package/dist/worker.js +6 -6
- package/package.json +1 -1
- package/src/ast.ts +2 -2
- package/src/bags.ts +22 -23
- package/src/builtins.ts +16 -8
- package/src/check.ts +131 -1154
- package/src/{cli-options.ts → cli/options.ts} +54 -9
- package/src/cli/run.ts +63 -0
- package/src/cli/shared.ts +106 -0
- package/src/cli/tlapm.ts +374 -0
- package/src/cli/tlc.ts +941 -0
- package/src/cli/xml-exporter.ts +221 -0
- package/src/cli.ts +1 -1
- package/src/codegen.ts +17 -15
- package/src/config.ts +5 -5
- package/src/dependencies.ts +4 -6
- package/src/environment.ts +55 -60
- package/src/errors.ts +12 -36
- package/src/evaluate.ts +81 -101
- package/src/exit-codes.ts +1 -1
- package/src/fingerprints.ts +3 -3
- package/src/instances.ts +56 -46
- package/src/java-overrides.ts +79 -41
- package/src/json.ts +31 -36
- package/src/levels.ts +10 -13
- package/src/{live-check.ts → liveness-checker.ts} +169 -142
- package/src/liveness.ts +38 -34
- package/src/obligations/action.ts +9 -45
- package/src/obligations/{anon.ts → anonymize.ts} +13 -25
- package/src/obligations/equality.ts +260 -0
- package/src/obligations/expression.ts +42 -517
- package/src/obligations/generate.ts +88 -778
- package/src/obligations/index.ts +20 -22
- package/src/obligations/levels.ts +36 -41
- package/src/obligations/module.ts +88 -708
- package/src/obligations/normalize.ts +554 -0
- package/src/obligations/operators.ts +176 -0
- package/src/obligations/pragma.ts +21 -15
- package/src/obligations/printer.ts +81 -188
- package/src/obligations/proof.ts +0 -1
- package/src/obligations/regroup.ts +100 -0
- package/src/obligations/simplify.ts +668 -0
- package/src/obligations/source.ts +25 -131
- package/src/obligations/{deref.ts → subexpression.ts} +48 -56
- package/src/obligations/substitution.ts +261 -0
- package/src/obligations/temporal.ts +28 -26
- package/src/obligations/tuples.ts +37 -53
- package/src/parallel-worker.ts +37 -33
- package/src/parallel.ts +30 -24
- package/src/parser.ts +67 -74
- package/src/{pcal-ast.ts → pcal/ast.ts} +2 -17
- package/src/{pcal-parse.ts → pcal/parse.ts} +28 -64
- package/src/{pcal-tokenize.ts → pcal/tokenize.ts} +10 -39
- package/src/{pcal-translate.ts → pcal/translate.ts} +30 -109
- package/src/{pcal-validate.ts → pcal/validate.ts} +10 -20
- package/src/prove/decide-pool.ts +7 -12
- package/src/{obligations/pickle.ts → prove/first-order.ts} +79 -2
- package/src/prove/isabelle/decide.ts +3 -6
- package/src/prove/isabelle/encode.ts +35 -93
- package/src/prove/isabelle/isar/inner-syntax.ts +1 -1
- package/src/prove/isabelle/isar/method.ts +3 -3
- package/src/prove/isabelle/isar/type-check.ts +1 -1
- package/src/prove/isabelle/provers/blast/blast.ts +1 -1
- package/src/prove/isabelle/provers/blast/branch.ts +1 -1
- package/src/prove/isabelle/provers/blast/term.ts +3 -4
- package/src/prove/isabelle/provers/clasimp.ts +1 -1
- package/src/prove/isabelle/provers/classical.ts +3 -3
- package/src/prove/isabelle/provers/hypsubst.ts +1 -9
- package/src/prove/isabelle/pure/conv.ts +1 -1
- package/src/prove/isabelle/pure/drule.ts +2 -2
- package/src/prove/isabelle/pure/envir.ts +3 -35
- package/src/prove/isabelle/pure/logic.ts +4 -76
- package/src/prove/isabelle/pure/name.ts +1 -13
- package/src/prove/isabelle/pure/net.ts +2 -49
- package/src/prove/isabelle/pure/pattern.ts +3 -22
- package/src/prove/isabelle/pure/raw-simplifier.ts +3 -18
- package/src/prove/isabelle/pure/seq.ts +0 -23
- package/src/prove/isabelle/pure/simplifier.ts +2 -8
- package/src/prove/isabelle/pure/table.ts +1 -8
- package/src/prove/isabelle/pure/tactic.ts +5 -113
- package/src/prove/isabelle/pure/tactical.ts +1 -84
- package/src/prove/isabelle/pure/term-ord.ts +1 -55
- package/src/prove/isabelle/pure/term.ts +8 -128
- package/src/prove/isabelle/pure/thm.ts +4 -27
- package/src/prove/isabelle/pure/type.ts +0 -12
- package/src/prove/isabelle/pure/unify.ts +1 -1
- package/src/prove/isabelle/tla/classical-setup.ts +1 -1
- package/src/prove/ocaml.ts +1 -1
- package/src/prove/prep/action.ts +23 -36
- package/src/prove/prep/enabled-axioms.ts +2 -2
- package/src/prove/prep/fingerprint.ts +3 -3
- package/src/prove/prep/level-comparison.ts +9 -37
- package/src/prove/prep/md5.ts +2 -9
- package/src/prove/prep/normalize.ts +309 -0
- package/src/prove/prove-worker.ts +2 -5
- package/src/prove/prove.ts +98 -111
- package/src/prove/ptl/encode.ts +19 -31
- package/src/prove/smt/axioms.ts +45 -58
- package/src/prove/smt/encode.ts +124 -15
- package/src/prove/smt/instances.ts +2 -3
- package/src/prove/smt/rewrite.ts +24 -31
- package/src/prove/smt/solver/ast-util.ts +0 -56
- package/src/prove/smt/solver/case-split-queue.ts +1 -1
- package/src/prove/smt/solver/enode.ts +1 -1
- package/src/prove/smt/solver/lp/dep-intervals.ts +0 -14
- package/src/prove/smt/solver/model-macro-solver.ts +1 -1
- package/src/prove/smt/solver/nlsat/algebraic.ts +3 -3
- package/src/prove/smt/solver/nlsat/atoms.ts +1 -1
- package/src/prove/smt/solver/nlsat/explain.ts +1 -1
- package/src/prove/smt/solver/nlsat/interval-set.ts +1 -1
- package/src/prove/smt/solver/nlsat/levelwise.ts +4 -7
- package/src/prove/smt/solver/nlsat/polynomial.ts +4 -4
- package/src/prove/smt/solver/nlsat/upolynomial.ts +2 -2
- package/src/prove/smt/solver/pattern-inference.ts +1 -1
- package/src/prove/smt/solver/relevancy.ts +2 -2
- package/src/prove/smt/solver/rewriter.ts +1 -1
- package/src/prove/smt/solver/solver.ts +2 -2
- package/src/{tlapm-cache.ts → prove/tlapm/cache.ts} +11 -11
- package/src/{ocaml-marshal.ts → prove/tlapm/ocaml-marshal.ts} +1 -1
- package/src/prove/verdict.ts +0 -5
- package/src/prove/zenon/decide.ts +5 -11
- package/src/prove/zenon/encode.ts +21 -80
- package/src/prove/zenon/eqrel.ts +1 -1
- package/src/prove/zenon/equality-rules.ts +1 -1
- package/src/prove/zenon/expr.ts +2 -2
- package/src/prove/zenon/index-table.ts +1 -1
- package/src/prove/zenon/isar.ts +1 -1
- package/src/prove/zenon/llproof.ts +1 -1
- package/src/prove/zenon/mltoll.ts +1 -1
- package/src/prove/zenon/node.ts +1 -1
- package/src/prove/zenon/search.ts +1 -1
- package/src/prove/zenon/tla/terms.ts +1 -20
- package/src/prove/zenon/tla/tla.ts +1 -1
- package/src/randomization.ts +19 -24
- package/src/resolve.ts +15 -22
- package/src/runtime.ts +8 -8
- package/src/{sany-builtins.ts → sany/builtins.ts} +1 -1
- package/src/{sany-generator.ts → sany/generator.ts} +38 -55
- package/src/{sany-levels.ts → sany/levels.ts} +45 -48
- package/src/{sany-lexer.ts → sany/lexer.ts} +2 -3
- package/src/{sany-linter.ts → sany/linter.ts} +1 -1
- package/src/{sany-modules.ts → sany/modules.ts} +2 -2
- package/src/{sany-nodes.ts → sany/nodes.ts} +2 -2
- package/src/{sany-parse-error.ts → sany/parse-error.ts} +2 -2
- package/src/{sany-report.ts → sany/report.ts} +16 -17
- package/src/{sany-selectors.ts → sany/selectors.ts} +3 -5
- package/src/{sany-source.ts → sany/source.ts} +6 -7
- package/src/{sany-xml.ts → sany/xml.ts} +5 -6
- package/src/search.ts +969 -0
- package/src/spec-description.ts +26 -20
- package/src/{standard-levels.ts → standard-definitions.ts} +118 -2
- package/src/standard-modules.ts +16 -20
- package/src/state-graph.ts +9 -8
- package/src/substitution-levels.ts +2 -2
- package/src/successors.ts +16 -19
- package/src/symmetry.ts +1 -1
- package/src/tableau.ts +6 -7
- package/src/tlc/fingerprint.ts +214 -0
- package/src/{tlc-messages.ts → tlc/messages.ts} +119 -7
- package/src/{tlc-order.ts → tlc/order.ts} +7 -7
- package/src/{tlc-random.ts → tlc/random.ts} +3 -213
- package/src/{tlc-replay.ts → tlc/replay.ts} +40 -36
- package/src/{tlc-module.ts → tlc/tlc-operators.ts} +9 -9
- package/src/{tlc-ext.ts → tlc/tlcext-operators.ts} +30 -25
- package/src/transfer.ts +32 -32
- package/src/values.ts +72 -67
- package/src/worker.ts +1 -4
- package/dist/run-cli.cjs +0 -45
- package/dist/run-cli.js +0 -45
- package/src/canonical.ts +0 -40
- package/src/obligations/ctx.ts +0 -64
- package/src/prove/prep/defs.ts +0 -33
- package/src/prove/prep/elab.ts +0 -156
- package/src/prove/prep/enabled.ts +0 -43
- package/src/prove/prep/flatten.ts +0 -70
- package/src/prove/prep/trivial.ts +0 -22
- package/src/prove/smt/axiomatize.ts +0 -69
- package/src/prove/smt/simplify.ts +0 -57
- package/src/run-cli.ts +0 -1806
- package/src/standard-ranges.ts +0 -116
- package/src/value-sources.ts +0 -29
- /package/src/prove/prep/{pragma.ts → methods.ts} +0 -0
- /package/src/{tlc-modules.ts → tlc/jar-data.ts} +0 -0
- /package/src/{tlc-summary.ts → tlc/summary.ts} +0 -0
- /package/src/{tlc-version.ts → tlc/version.ts} +0 -0
|
@@ -1,53 +1,34 @@
|
|
|
1
|
-
//
|
|
2
|
-
// (Module.
|
|
1
|
+
// tlapm's module units, with the standard modules, flattening EXTENDS (Module.Flatten), the
|
|
2
|
+
// modules a module depends on (Module.Dep), and substitution in units (Module.Subst).
|
|
3
3
|
|
|
4
4
|
import { TlaProofError, TlapmBugError } from "../errors";
|
|
5
|
-
import { lambdify } from "./action";
|
|
6
|
-
import type { Anonymizer } from "./anon";
|
|
7
5
|
import {
|
|
8
|
-
adjoinAll,
|
|
9
6
|
at,
|
|
10
|
-
bumpBy,
|
|
11
|
-
consAll,
|
|
12
|
-
consSubstitution,
|
|
13
7
|
exprifySequent,
|
|
14
|
-
|
|
15
|
-
getAtPrefix,
|
|
16
|
-
getLocusText,
|
|
17
|
-
getLocusPrefix,
|
|
18
|
-
getScopeHyps,
|
|
19
|
-
mapAccumulate,
|
|
8
|
+
getFact,
|
|
20
9
|
getFresh,
|
|
21
|
-
|
|
22
|
-
shiftExpr,
|
|
23
|
-
substituteDefn,
|
|
24
|
-
substituteExpr,
|
|
25
|
-
substituteExprs,
|
|
26
|
-
substituteHyp,
|
|
27
|
-
substituteSequent,
|
|
28
|
-
shift,
|
|
10
|
+
mapAccumulate,
|
|
29
11
|
type Builtin,
|
|
30
12
|
type Defn,
|
|
31
13
|
type Expr,
|
|
32
14
|
type Hyp,
|
|
33
15
|
type InstanceDefinition,
|
|
34
16
|
type Locus,
|
|
35
|
-
type ObligationWarning,
|
|
36
|
-
type Scope,
|
|
37
17
|
type Sequent,
|
|
38
|
-
type Substitution,
|
|
39
18
|
type Visibility,
|
|
40
19
|
type WhereDefined,
|
|
41
20
|
} from "./expression";
|
|
21
|
+
import type { Proof, Usable } from "./proof";
|
|
42
22
|
import {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
23
|
+
bumpBy,
|
|
24
|
+
shift,
|
|
25
|
+
shiftExpr,
|
|
26
|
+
substituteDefn,
|
|
27
|
+
substituteExpr,
|
|
28
|
+
substituteExprs,
|
|
29
|
+
substituteSequent,
|
|
30
|
+
type Substitution,
|
|
31
|
+
} from "./substitution";
|
|
51
32
|
|
|
52
33
|
export type ModuleUnit = ModuleUnitCore & { location?: Locus };
|
|
53
34
|
|
|
@@ -70,7 +51,7 @@ type ModuleUnitCore =
|
|
|
70
51
|
proof: Proof;
|
|
71
52
|
}
|
|
72
53
|
| { kind: "Submod"; module: Mule }
|
|
73
|
-
| { kind: "Mutate"; mode: "Use" | "Hide";
|
|
54
|
+
| { kind: "Mutate"; mode: "Use" | "Hide"; usable: Usable }
|
|
74
55
|
| { kind: "Anoninst"; instance: InstanceDefinition; isLocal: boolean };
|
|
75
56
|
|
|
76
57
|
// depth is the size of the context the module was normalized in, which instantiation shifts
|
|
@@ -86,34 +67,29 @@ export interface Mule {
|
|
|
86
67
|
location?: Locus;
|
|
87
68
|
}
|
|
88
69
|
|
|
89
|
-
const
|
|
90
|
-
kind: "Definition",
|
|
91
|
-
definition: { kind: "Operator", name, body: { kind: "Internal", builtin } },
|
|
92
|
-
whereDefined: "Builtin",
|
|
93
|
-
visibility: "Visible",
|
|
94
|
-
isLocal: false,
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
const getBuiltinOperator = (arity: number, name: string, builtin: Builtin): ModuleUnit => ({
|
|
70
|
+
const defineBuiltin = (arity: number, name: string, builtin: Builtin): ModuleUnit => ({
|
|
98
71
|
kind: "Definition",
|
|
99
72
|
definition: {
|
|
100
73
|
kind: "Operator",
|
|
101
74
|
name,
|
|
102
|
-
body:
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
75
|
+
body:
|
|
76
|
+
arity === 0
|
|
77
|
+
? { kind: "Internal", builtin }
|
|
78
|
+
: {
|
|
79
|
+
kind: "Lambda",
|
|
80
|
+
params: Array.from({ length: arity }, (_item, index) => ({
|
|
81
|
+
name: `x${index + 1}`,
|
|
82
|
+
shape: 0,
|
|
83
|
+
})),
|
|
84
|
+
body: {
|
|
85
|
+
kind: "Apply",
|
|
86
|
+
operator: { kind: "Internal", builtin },
|
|
87
|
+
args: Array.from({ length: arity }, (_item, index) => ({
|
|
88
|
+
kind: "Ix",
|
|
89
|
+
index: arity - index,
|
|
90
|
+
})),
|
|
91
|
+
},
|
|
92
|
+
},
|
|
117
93
|
},
|
|
118
94
|
whereDefined: "Builtin",
|
|
119
95
|
visibility: "Visible",
|
|
@@ -121,62 +97,58 @@ const getBuiltinOperator = (arity: number, name: string, builtin: Builtin): Modu
|
|
|
121
97
|
});
|
|
122
98
|
|
|
123
99
|
const NATURALS: ModuleUnit[] = [
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
100
|
+
defineBuiltin(0, "Nat", "Nat"),
|
|
101
|
+
defineBuiltin(2, "+", "Plus"),
|
|
102
|
+
defineBuiltin(2, "-", "Minus"),
|
|
103
|
+
defineBuiltin(2, "*", "Times"),
|
|
104
|
+
defineBuiltin(2, "^", "Exp"),
|
|
105
|
+
defineBuiltin(2, "<", "Lt"),
|
|
106
|
+
defineBuiltin(2, ">", "Gt"),
|
|
107
|
+
defineBuiltin(2, "=<", "Lteq"),
|
|
108
|
+
defineBuiltin(2, ">=", "Gteq"),
|
|
109
|
+
defineBuiltin(2, "%", "Remainder"),
|
|
110
|
+
defineBuiltin(2, "\\div", "Quotient"),
|
|
111
|
+
defineBuiltin(2, "..", "Range"),
|
|
136
112
|
];
|
|
137
113
|
|
|
138
|
-
const INTEGERS = [
|
|
139
|
-
...NATURALS,
|
|
140
|
-
getBuiltinConstant("Int", "Int"),
|
|
141
|
-
getBuiltinOperator(1, "-.", "Uminus"),
|
|
142
|
-
];
|
|
114
|
+
const INTEGERS = [...NATURALS, defineBuiltin(0, "Int", "Int"), defineBuiltin(1, "-.", "Uminus")];
|
|
143
115
|
|
|
144
116
|
const REALS = [
|
|
145
117
|
...INTEGERS,
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
118
|
+
defineBuiltin(0, "Real", "Real"),
|
|
119
|
+
defineBuiltin(2, "/", "Ratio"),
|
|
120
|
+
defineBuiltin(0, "Infinity", "Infinity"),
|
|
149
121
|
];
|
|
150
122
|
|
|
151
123
|
const SEQUENCES = [
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
124
|
+
defineBuiltin(1, "Seq", "Seq"),
|
|
125
|
+
defineBuiltin(1, "Len", "Len"),
|
|
126
|
+
defineBuiltin(2, "BSeq", "BSeq"),
|
|
127
|
+
defineBuiltin(2, "\\o", "Cat"),
|
|
128
|
+
defineBuiltin(2, "Append", "Append"),
|
|
129
|
+
defineBuiltin(1, "Head", "Head"),
|
|
130
|
+
defineBuiltin(1, "Tail", "Tail"),
|
|
131
|
+
defineBuiltin(3, "SubSeq", "SubSeq"),
|
|
132
|
+
defineBuiltin(2, "SelectSeq", "SelectSeq"),
|
|
161
133
|
];
|
|
162
134
|
|
|
163
135
|
const TLC = [
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
136
|
+
defineBuiltin(2, "Print", "Print"),
|
|
137
|
+
defineBuiltin(1, "PrintT", "PrintT"),
|
|
138
|
+
defineBuiltin(2, "Assert", "Assert"),
|
|
139
|
+
defineBuiltin(0, "JavaTime", "JavaTime"),
|
|
140
|
+
defineBuiltin(1, "TLCGet", "TLCGet"),
|
|
141
|
+
defineBuiltin(2, "TLCSet", "TLCSet"),
|
|
142
|
+
defineBuiltin(2, ":>", "OneArg"),
|
|
143
|
+
defineBuiltin(2, "@@", "Extend"),
|
|
144
|
+
defineBuiltin(1, "Permutations", "Permutations"),
|
|
145
|
+
defineBuiltin(2, "SortSeq", "SortSeq"),
|
|
146
|
+
defineBuiltin(1, "RandomElement", "RandomElement"),
|
|
147
|
+
defineBuiltin(0, "Any", "Any"),
|
|
148
|
+
defineBuiltin(1, "ToString", "ToString"),
|
|
177
149
|
];
|
|
178
150
|
|
|
179
|
-
const
|
|
151
|
+
const getStandardModule = (name: string, body: ModuleUnit[]): Mule => ({
|
|
180
152
|
name,
|
|
181
153
|
extendees: [],
|
|
182
154
|
body,
|
|
@@ -187,25 +159,20 @@ const getStandard = (name: string, body: ModuleUnit[]): Mule => ({
|
|
|
187
159
|
// Module.Standard.initctx, apart from $TLAPM, whose Builtins! names no module of the corpora uses.
|
|
188
160
|
export const STANDARD_MODULES = new Map(
|
|
189
161
|
[
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
162
|
+
getStandardModule("Naturals", NATURALS),
|
|
163
|
+
getStandardModule("Integers", INTEGERS),
|
|
164
|
+
getStandardModule("Reals", REALS),
|
|
165
|
+
getStandardModule("Sequences", SEQUENCES),
|
|
166
|
+
getStandardModule("TLC", TLC),
|
|
195
167
|
].map((module) => [module.name, module]),
|
|
196
168
|
);
|
|
197
169
|
|
|
198
|
-
const getUnitHyps = (unit: ModuleUnit): Hyp[] => {
|
|
170
|
+
export const getUnitHyps = (unit: ModuleUnit): Hyp[] => {
|
|
199
171
|
const location = unit.location;
|
|
200
172
|
switch (unit.kind) {
|
|
201
173
|
case "Constants":
|
|
202
174
|
return unit.declarations.map((declaration) => ({
|
|
203
|
-
|
|
204
|
-
name: declaration.name,
|
|
205
|
-
shape: declaration.shape,
|
|
206
|
-
level: "Constant",
|
|
207
|
-
domain: undefined,
|
|
208
|
-
domainVisibility: "Visible",
|
|
175
|
+
...getFresh(declaration.name, "Constant", declaration.shape),
|
|
209
176
|
location,
|
|
210
177
|
}));
|
|
211
178
|
case "Recursives":
|
|
@@ -250,18 +217,14 @@ const getUnitHyps = (unit: ModuleUnit): Hyp[] => {
|
|
|
250
217
|
unit,
|
|
251
218
|
);
|
|
252
219
|
return unit.name === undefined
|
|
253
|
-
? [
|
|
220
|
+
? [getFact(expression, "Hidden", "Always", location)]
|
|
254
221
|
: getNamedFact(unit.name, expression, location);
|
|
255
222
|
}
|
|
256
223
|
case "Mutate":
|
|
257
224
|
return unit.mode === "Use"
|
|
258
|
-
? unit.usable.facts.map((fact, index) =>
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
visibility: "Visible",
|
|
262
|
-
time: "Always",
|
|
263
|
-
location,
|
|
264
|
-
}))
|
|
225
|
+
? unit.usable.facts.map((fact, index) =>
|
|
226
|
+
getFact(shiftExpr(index, fact), "Visible", "Always", location),
|
|
227
|
+
)
|
|
265
228
|
: [];
|
|
266
229
|
case "Submod":
|
|
267
230
|
case "Anoninst":
|
|
@@ -269,7 +232,7 @@ const getUnitHyps = (unit: ModuleUnit): Hyp[] => {
|
|
|
269
232
|
}
|
|
270
233
|
};
|
|
271
234
|
|
|
272
|
-
const getHypSize = (unit: ModuleUnit) =>
|
|
235
|
+
export const getHypSize = (unit: ModuleUnit) =>
|
|
273
236
|
unit.kind === "Axiom" || unit.kind === "Theorem"
|
|
274
237
|
? unit.name === undefined
|
|
275
238
|
? 1
|
|
@@ -285,13 +248,7 @@ const getNamedFact = (name: string, expression: Expr, location: Locus | undefine
|
|
|
285
248
|
isLocal: false,
|
|
286
249
|
location,
|
|
287
250
|
},
|
|
288
|
-
{
|
|
289
|
-
kind: "Fact",
|
|
290
|
-
expression: { kind: "Ix", index: 1, location },
|
|
291
|
-
visibility: "Hidden",
|
|
292
|
-
time: "Always",
|
|
293
|
-
location,
|
|
294
|
-
},
|
|
251
|
+
getFact({ kind: "Ix", index: 1, location }, "Hidden", "Always", location),
|
|
295
252
|
];
|
|
296
253
|
|
|
297
254
|
// Module.Flatten.exports: what an extending module sees, with local definitions inlined like
|
|
@@ -390,16 +347,6 @@ const isInstanceDefinition = (value: unknown): value is InstanceDefinition =>
|
|
|
390
347
|
export const compareNames = (left: string, right: string) =>
|
|
391
348
|
left < right ? -1 : left > right ? 1 : 0;
|
|
392
349
|
|
|
393
|
-
// latestModules is the module context of the latest anonymization of an axiom, theorem or USE,
|
|
394
|
-
// which tlapm's anonymizer keeps using for the instances inside later definitions.
|
|
395
|
-
export interface Pipeline {
|
|
396
|
-
anonymizer: Anonymizer;
|
|
397
|
-
generator: Generator;
|
|
398
|
-
salts: Counter;
|
|
399
|
-
latestModules: Map<string, Mule>;
|
|
400
|
-
onWarning: (warning: ObligationWarning) => void;
|
|
401
|
-
}
|
|
402
|
-
|
|
403
350
|
const substituteUsable = (substitution: Substitution, usable: Usable): Usable => ({
|
|
404
351
|
facts: substituteExprs(substitution, usable.facts),
|
|
405
352
|
defs: usable.defs.map((def) => {
|
|
@@ -467,339 +414,11 @@ const substituteUnit = (
|
|
|
467
414
|
}
|
|
468
415
|
};
|
|
469
416
|
|
|
470
|
-
const substituteUnits = (substitution: Substitution, units: ModuleUnit[]) =>
|
|
417
|
+
export const substituteUnits = (substitution: Substitution, units: ModuleUnit[]) =>
|
|
471
418
|
mapAccumulate(substitution, units, substituteUnit)[1];
|
|
472
419
|
|
|
473
|
-
const lambdifyTheorem = (
|
|
474
|
-
pipeline: Pipeline,
|
|
475
|
-
scope: Scope | undefined,
|
|
476
|
-
name: string | undefined,
|
|
477
|
-
sequent: Sequent,
|
|
478
|
-
) => {
|
|
479
|
-
try {
|
|
480
|
-
const lambdified = lambdify(pipeline.anonymizer, pipeline.onWarning, scope, {
|
|
481
|
-
kind: "Sequent",
|
|
482
|
-
sequent,
|
|
483
|
-
});
|
|
484
|
-
if (lambdified.kind !== "Sequent") throw new TlapmBugError();
|
|
485
|
-
return lambdified.sequent;
|
|
486
|
-
} catch (error) {
|
|
487
|
-
if (!(error instanceof TlaProofError) || error.failure === undefined) throw error;
|
|
488
|
-
const location = sequent.active.location;
|
|
489
|
-
const message = `${error.failure}\nCould not instantiate the theorem ${name === undefined ? "" : `named: ${name}, `}at: ${location ? getLocusText(location) : "<unknown location>"}`;
|
|
490
|
-
pipeline.onWarning({ text: `${getAtPrefix(location)}[ERROR]${message}`, channel: "stdout" });
|
|
491
|
-
pipeline.onWarning({ text: message, channel: "toolboxOnly" });
|
|
492
|
-
return { context: [], active: at({ kind: "Internal", builtin: "TRUE" }, sequent.active) };
|
|
493
|
-
}
|
|
494
|
-
};
|
|
495
|
-
|
|
496
|
-
const lambdifyUnit = (pipeline: Pipeline, scope: Scope | undefined, unit: ModuleUnit) => {
|
|
497
|
-
if (unit.kind === "Theorem") {
|
|
498
|
-
return { ...unit, sequent: lambdifyTheorem(pipeline, scope, unit.name, unit.sequent) };
|
|
499
|
-
}
|
|
500
|
-
if (unit.kind !== "Definition") return unit;
|
|
501
|
-
const definition = unit.definition;
|
|
502
|
-
if (definition.kind !== "Operator" && definition.kind !== "Bpragma") return unit;
|
|
503
|
-
const body = lambdify(pipeline.anonymizer, pipeline.onWarning, scope, definition.body);
|
|
504
|
-
return { ...unit, definition: { ...definition, body } };
|
|
505
|
-
};
|
|
506
|
-
|
|
507
|
-
// lambdify_enabled_cdot: the ENABLED and \cdot of each definition and theorem statement bind
|
|
508
|
-
// their variables before instantiation substitutes for them. A theorem that cannot be lambdified
|
|
509
|
-
// states TRUE.
|
|
510
|
-
const lambdifyUnits = (
|
|
511
|
-
pipeline: Pipeline,
|
|
512
|
-
scope: Scope | undefined,
|
|
513
|
-
units: ModuleUnit[],
|
|
514
|
-
): [Scope | undefined, ModuleUnit[]] =>
|
|
515
|
-
mapAccumulate(scope, units, (current, unit): [Scope | undefined, ModuleUnit] => {
|
|
516
|
-
if (unit.kind === "Submod") {
|
|
517
|
-
const [inner, body] = lambdifyUnits(pipeline, current, unit.module.body);
|
|
518
|
-
return [inner, { ...unit, module: { ...unit.module, body } }];
|
|
519
|
-
}
|
|
520
|
-
const lambdified = lambdifyUnit(pipeline, current, unit);
|
|
521
|
-
return [adjoinAll(current, getUnitHyps(lambdified)), lambdified];
|
|
522
|
-
});
|
|
523
|
-
|
|
524
|
-
// apply_subst: each parameter declaration of the instancee gives way to its substitute.
|
|
525
|
-
const applyParameters = (units: ModuleUnit[], substitutes: Map<string, Expr>) => {
|
|
526
|
-
const body: ModuleUnit[] = [];
|
|
527
|
-
let size = 0;
|
|
528
|
-
let rest = units;
|
|
529
|
-
while (rest.length > 0) {
|
|
530
|
-
const [unit, ...tail] = rest;
|
|
531
|
-
const split =
|
|
532
|
-
unit.kind === "Constants"
|
|
533
|
-
? {
|
|
534
|
-
name: unit.declarations[0].name,
|
|
535
|
-
remaining: { ...unit, declarations: unit.declarations.slice(1) },
|
|
536
|
-
}
|
|
537
|
-
: unit.kind === "Variables"
|
|
538
|
-
? { name: unit.names[0], remaining: { ...unit, names: unit.names.slice(1) } }
|
|
539
|
-
: undefined;
|
|
540
|
-
if (split) {
|
|
541
|
-
const substitute = substitutes.get(split.name);
|
|
542
|
-
if (!substitute) throw new TlaProofError(`Parameter ${split.name} has no substitute`);
|
|
543
|
-
const isEmpty = getHypSize(split.remaining) === 0;
|
|
544
|
-
rest = substituteUnits(
|
|
545
|
-
consSubstitution(shiftExpr(size, substitute), shift(0)),
|
|
546
|
-
isEmpty ? tail : [split.remaining, ...tail],
|
|
547
|
-
);
|
|
548
|
-
continue;
|
|
549
|
-
}
|
|
550
|
-
body.push(unit);
|
|
551
|
-
size += getHypSize(unit);
|
|
552
|
-
rest = tail;
|
|
553
|
-
}
|
|
554
|
-
return body;
|
|
555
|
-
};
|
|
556
|
-
|
|
557
|
-
// resub_for: references to the instancee's own units become applications to the instance
|
|
558
|
-
// arguments, which the definitions now bind themselves.
|
|
559
|
-
const getLocalSubstitution = (
|
|
560
|
-
count: number,
|
|
561
|
-
argumentCount: number,
|
|
562
|
-
location: Locus | undefined,
|
|
563
|
-
): Substitution => {
|
|
564
|
-
const args = Array.from({ length: argumentCount }, (_item, index) =>
|
|
565
|
-
at({ kind: "Ix", index: argumentCount - index }, { location }),
|
|
566
|
-
);
|
|
567
|
-
const parameters = Array.from({ length: argumentCount }, (_item, index) =>
|
|
568
|
-
at({ kind: "Ix", index: index + 1 }, { location }),
|
|
569
|
-
);
|
|
570
|
-
const locals = Array.from({ length: count }, (_item, index): Expr => {
|
|
571
|
-
const operator = at({ kind: "Ix", index: index + argumentCount + 1 }, { location });
|
|
572
|
-
return argumentCount > 0 ? at({ kind: "Apply", operator, args }, { location }) : operator;
|
|
573
|
-
});
|
|
574
|
-
return consAll([...locals, ...parameters].reverse(), shift(count + argumentCount));
|
|
575
|
-
};
|
|
576
|
-
|
|
577
|
-
const addParameters = (expression: Expr, args: string[]): Expr => {
|
|
578
|
-
const params = args.map((name) => ({ name, shape: 0 }));
|
|
579
|
-
if (expression.kind === "Lambda")
|
|
580
|
-
return { ...expression, params: [...params, ...expression.params] };
|
|
581
|
-
return args.length === 0
|
|
582
|
-
? expression
|
|
583
|
-
: at({ kind: "Lambda", params, body: expression }, expression);
|
|
584
|
-
};
|
|
585
|
-
|
|
586
|
-
// localize: renames the instancee's units under the instance name, salts its local definitions
|
|
587
|
-
const localize = (
|
|
588
|
-
pipeline: Pipeline,
|
|
589
|
-
units: ModuleUnit[],
|
|
590
|
-
prefix: string,
|
|
591
|
-
instance: InstanceDefinition,
|
|
592
|
-
isLocal: boolean,
|
|
593
|
-
location: Locus | undefined,
|
|
594
|
-
) => {
|
|
595
|
-
const getName = (name: string) => (prefix === "" ? name : `${prefix}!${name}`);
|
|
596
|
-
const argumentCount = instance.args.length;
|
|
597
|
-
const body: ModuleUnit[] = [];
|
|
598
|
-
let size = 0;
|
|
599
|
-
let rest = units;
|
|
600
|
-
let isComplained = argumentCount === 0;
|
|
601
|
-
const complain = (text: string) => {
|
|
602
|
-
if (isComplained) return;
|
|
603
|
-
isComplained = true;
|
|
604
|
-
pipeline.onWarning({ text, channel: "stderr" });
|
|
605
|
-
};
|
|
606
|
-
while (rest.length > 0) {
|
|
607
|
-
const [unit, ...tail] = rest;
|
|
608
|
-
rest = tail;
|
|
609
|
-
const localized = (expression: Expr) =>
|
|
610
|
-
addParameters(
|
|
611
|
-
substituteExpr(getLocalSubstitution(size, argumentCount, location), expression),
|
|
612
|
-
instance.args,
|
|
613
|
-
);
|
|
614
|
-
switch (unit.kind) {
|
|
615
|
-
case "Definition": {
|
|
616
|
-
const definition = unit.definition;
|
|
617
|
-
if (definition.kind !== "Operator" && definition.kind !== "Bpragma") {
|
|
618
|
-
throw new TlaProofError("Module.Elab.localize");
|
|
619
|
-
}
|
|
620
|
-
const name = unit.isLocal
|
|
621
|
-
? `${definition.name}$${getUnique(pipeline.salts)}`
|
|
622
|
-
: definition.name;
|
|
623
|
-
body.push({
|
|
624
|
-
...unit,
|
|
625
|
-
definition: { ...definition, name: getName(name), body: localized(definition.body) },
|
|
626
|
-
whereDefined: unit.isLocal ? "Builtin" : unit.whereDefined,
|
|
627
|
-
isLocal: unit.isLocal || isLocal,
|
|
628
|
-
});
|
|
629
|
-
size += 1;
|
|
630
|
-
continue;
|
|
631
|
-
}
|
|
632
|
-
case "Axiom": {
|
|
633
|
-
complain(
|
|
634
|
-
`${getLocusPrefix(location)}Instancing produces parametric axioms.\nSuch axioms will be forcibly hidden and cannot be cited.\n(This warning appears at-most once per INSTANCE declaration.)`,
|
|
635
|
-
);
|
|
636
|
-
const axiom: ModuleUnit = {
|
|
637
|
-
...unit,
|
|
638
|
-
name: unit.name === undefined ? undefined : getName(unit.name),
|
|
639
|
-
expression: localized(unit.expression),
|
|
640
|
-
};
|
|
641
|
-
body.push(axiom);
|
|
642
|
-
size += getHypSize(axiom);
|
|
643
|
-
continue;
|
|
644
|
-
}
|
|
645
|
-
case "Theorem": {
|
|
646
|
-
complain(
|
|
647
|
-
`${getAtPrefix(location)}Warning: Instancing produces parametric theorems.\nSuch theorems will be forcibly hidden and cannot be cited.\n(This warning appears at-most once per INSTANCE declaration.)`,
|
|
648
|
-
);
|
|
649
|
-
const expression = localized(at(exprifySequent(unit.sequent), unit));
|
|
650
|
-
body.push({
|
|
651
|
-
...unit,
|
|
652
|
-
name: unit.name === undefined ? undefined : getName(unit.name),
|
|
653
|
-
sequent:
|
|
654
|
-
expression.kind === "Sequent"
|
|
655
|
-
? expression.sequent
|
|
656
|
-
: { context: [], active: expression },
|
|
657
|
-
proof: { kind: "Omitted", omission: "Elsewhere", location: unit.location },
|
|
658
|
-
});
|
|
659
|
-
size += 1;
|
|
660
|
-
continue;
|
|
661
|
-
}
|
|
662
|
-
case "Mutate":
|
|
663
|
-
if (unit.mode === "Use") rest = substituteUnits(shift(-unit.usable.facts.length), rest);
|
|
664
|
-
continue;
|
|
665
|
-
case "Submod":
|
|
666
|
-
continue;
|
|
667
|
-
default:
|
|
668
|
-
throw new TlaProofError("Module.Elab.localize");
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
return body;
|
|
672
|
-
};
|
|
673
|
-
|
|
674
|
-
// localize_axioms: an axiom becomes a hypothesis of each theorem after it, and a named one also a
|
|
675
|
-
// definition.
|
|
676
|
-
const localizeAxioms = (units: ModuleUnit[]) => {
|
|
677
|
-
const body: ModuleUnit[] = [];
|
|
678
|
-
let rest = units;
|
|
679
|
-
while (rest.length > 0) {
|
|
680
|
-
const [unit, ...tail] = rest;
|
|
681
|
-
if (unit.kind !== "Axiom") {
|
|
682
|
-
body.push(unit);
|
|
683
|
-
rest = tail;
|
|
684
|
-
continue;
|
|
685
|
-
}
|
|
686
|
-
const location = unit.location;
|
|
687
|
-
if (unit.name !== undefined) {
|
|
688
|
-
body.push({
|
|
689
|
-
kind: "Definition",
|
|
690
|
-
definition: { kind: "Operator", name: unit.name, body: unit.expression, location },
|
|
691
|
-
whereDefined: { kind: "Proof", time: "Always" },
|
|
692
|
-
visibility: "Visible",
|
|
693
|
-
isLocal: false,
|
|
694
|
-
location,
|
|
695
|
-
});
|
|
696
|
-
}
|
|
697
|
-
const hyp: Hyp = {
|
|
698
|
-
kind: "Fact",
|
|
699
|
-
expression:
|
|
700
|
-
unit.name === undefined
|
|
701
|
-
? shiftExpr(1, unit.expression)
|
|
702
|
-
: at({ kind: "Ix", index: 2 }, unit),
|
|
703
|
-
visibility: "Visible",
|
|
704
|
-
time: "Always",
|
|
705
|
-
location,
|
|
706
|
-
};
|
|
707
|
-
let distance = 0;
|
|
708
|
-
const inserted = tail.map((next): ModuleUnit => {
|
|
709
|
-
const size = distance;
|
|
710
|
-
distance += getHypSize(next);
|
|
711
|
-
if (next.kind !== "Theorem") return next;
|
|
712
|
-
const sequent = substituteSequent(shift(1), next.sequent);
|
|
713
|
-
return {
|
|
714
|
-
...next,
|
|
715
|
-
sequent: { ...sequent, context: [substituteHyp(shift(size), hyp), ...sequent.context] },
|
|
716
|
-
axiomCount: next.axiomCount + 1,
|
|
717
|
-
};
|
|
718
|
-
});
|
|
719
|
-
rest = substituteUnits(shift(-1), inserted);
|
|
720
|
-
}
|
|
721
|
-
return body;
|
|
722
|
-
};
|
|
723
|
-
|
|
724
|
-
const getParameters = (module: Mule) =>
|
|
725
|
-
new Set(
|
|
726
|
-
module.body.flatMap((unit) =>
|
|
727
|
-
unit.kind === "Constants"
|
|
728
|
-
? unit.declarations.map((declaration) => declaration.name)
|
|
729
|
-
: unit.kind === "Variables"
|
|
730
|
-
? unit.names
|
|
731
|
-
: [],
|
|
732
|
-
),
|
|
733
|
-
);
|
|
734
|
-
|
|
735
|
-
// Module.Elab.instantiate: the units of an instantiated module, in the context of the instance.
|
|
736
|
-
const instantiate = (
|
|
737
|
-
pipeline: Pipeline,
|
|
738
|
-
modules: Map<string, Mule>,
|
|
739
|
-
scope: Scope | undefined,
|
|
740
|
-
instance: InstanceDefinition,
|
|
741
|
-
isLocal: boolean,
|
|
742
|
-
prefix: string,
|
|
743
|
-
location: Locus | undefined,
|
|
744
|
-
) => {
|
|
745
|
-
const module = modules.get(instance.module);
|
|
746
|
-
if (!module) throw new TlaProofError(`Module "${instance.module}" unknown`);
|
|
747
|
-
const parameters = getParameters(module);
|
|
748
|
-
for (const substitution of instance.substitutions) {
|
|
749
|
-
if (!parameters.has(substitution.name)) {
|
|
750
|
-
throw new TlaProofError(
|
|
751
|
-
`Parameter ${substitution.name} occurs in the statement \`INSTANCE ${instance.module} WITH\`, and is not declared in the module ${instance.module}`,
|
|
752
|
-
);
|
|
753
|
-
}
|
|
754
|
-
}
|
|
755
|
-
const instanceScope = adjoinAll(
|
|
756
|
-
scope,
|
|
757
|
-
instance.args.map((name) => getFresh(name, "Unknown")),
|
|
758
|
-
);
|
|
759
|
-
const substitutes = new Map(
|
|
760
|
-
instance.substitutions.map((entry) => [entry.name, entry.expression]),
|
|
761
|
-
);
|
|
762
|
-
for (const name of [...parameters].sort(compareNames)) {
|
|
763
|
-
if (substitutes.has(name)) continue;
|
|
764
|
-
if (!findNamed(instanceScope, name)) {
|
|
765
|
-
throw new TlaProofError(
|
|
766
|
-
`Required parameter ${name} for module ${instance.module} could not be inferred.`,
|
|
767
|
-
);
|
|
768
|
-
}
|
|
769
|
-
substitutes.set(name, at({ kind: "Opaque", name }, { location }));
|
|
770
|
-
}
|
|
771
|
-
const anonymized = new Map(
|
|
772
|
-
[...substitutes].map(([name, expression]) => [
|
|
773
|
-
name,
|
|
774
|
-
pipeline.anonymizer.expr(instanceScope, expression),
|
|
775
|
-
]),
|
|
776
|
-
);
|
|
777
|
-
const body = substituteUnits(
|
|
778
|
-
shift((instanceScope?.size ?? 0) - (module.depth ?? 0)),
|
|
779
|
-
module.body.map((unit): ModuleUnit =>
|
|
780
|
-
unit.kind === "Theorem"
|
|
781
|
-
? {
|
|
782
|
-
...unit,
|
|
783
|
-
proof: { kind: "Omitted", omission: "Implicit", location: unit.proof.location },
|
|
784
|
-
}
|
|
785
|
-
: unit,
|
|
786
|
-
),
|
|
787
|
-
);
|
|
788
|
-
const [, lambdified] = lambdifyUnits(pipeline, instanceScope, body);
|
|
789
|
-
return localizeAxioms(
|
|
790
|
-
localize(
|
|
791
|
-
pipeline,
|
|
792
|
-
applyParameters(lambdified, anonymized),
|
|
793
|
-
prefix,
|
|
794
|
-
instance,
|
|
795
|
-
isLocal,
|
|
796
|
-
location,
|
|
797
|
-
),
|
|
798
|
-
);
|
|
799
|
-
};
|
|
800
|
-
|
|
801
420
|
// A named theorem binds its name as a proof definition, which its statement shifts past.
|
|
802
|
-
const bindTheoremName = (
|
|
421
|
+
export const bindTheoremName = (
|
|
803
422
|
name: string | undefined,
|
|
804
423
|
sequent: Sequent,
|
|
805
424
|
location: Locus | undefined,
|
|
@@ -824,242 +443,3 @@ const bindTheoremName = (
|
|
|
824
443
|
],
|
|
825
444
|
sequent: substituteSequent(shift(1), sequent),
|
|
826
445
|
};
|
|
827
|
-
|
|
828
|
-
// make_defs: the definitions among the units an INSTANCE inside a LET or DEFINE stands for.
|
|
829
|
-
export const expandInstance = (
|
|
830
|
-
pipeline: Pipeline,
|
|
831
|
-
scope: Scope | undefined,
|
|
832
|
-
definition: Extract<Defn, { kind: "Instance" }>,
|
|
833
|
-
): Defn[] => {
|
|
834
|
-
const name = definition.instance.module;
|
|
835
|
-
if (!pipeline.latestModules.has(name)) {
|
|
836
|
-
pipeline.onWarning({
|
|
837
|
-
text: `${getLocusPrefix(definition.instance.location)}Module ${JSON.stringify(name)} unknown\n`,
|
|
838
|
-
channel: "toolbox",
|
|
839
|
-
});
|
|
840
|
-
throw new TlaProofError(`Module ${JSON.stringify(name)} unknown`, {
|
|
841
|
-
failure: "Module.Elab.anon#defns",
|
|
842
|
-
});
|
|
843
|
-
}
|
|
844
|
-
const units = instantiate(
|
|
845
|
-
pipeline,
|
|
846
|
-
pipeline.latestModules,
|
|
847
|
-
scope,
|
|
848
|
-
definition.instance,
|
|
849
|
-
true,
|
|
850
|
-
definition.name,
|
|
851
|
-
definition.location,
|
|
852
|
-
);
|
|
853
|
-
const definitions: Defn[] = [];
|
|
854
|
-
let rest = units;
|
|
855
|
-
while (rest.length > 0) {
|
|
856
|
-
const [unit, ...tail] = rest;
|
|
857
|
-
if (unit.kind !== "Definition") {
|
|
858
|
-
rest = substituteUnits(shift(-getHypSize(unit)), tail);
|
|
859
|
-
continue;
|
|
860
|
-
}
|
|
861
|
-
definitions.push(unit.definition);
|
|
862
|
-
rest = tail;
|
|
863
|
-
}
|
|
864
|
-
return definitions;
|
|
865
|
-
};
|
|
866
|
-
|
|
867
|
-
// Module.Elab.normalize: anonymizes each unit in the context of the ones before it, expands
|
|
868
|
-
// instances, merges repeated definitions, and simplifies the proofs of important modules. The
|
|
869
|
-
// result also joins the modules that later instances can name.
|
|
870
|
-
export const normalize = (
|
|
871
|
-
pipeline: Pipeline,
|
|
872
|
-
modules: Map<string, Mule>,
|
|
873
|
-
scope: Scope | undefined,
|
|
874
|
-
module: Mule,
|
|
875
|
-
): Mule => {
|
|
876
|
-
const body: ModuleUnit[] = [];
|
|
877
|
-
let current = scope;
|
|
878
|
-
const emit = (unit: ModuleUnit) => {
|
|
879
|
-
body.push(unit);
|
|
880
|
-
current = adjoinAll(current, getUnitHyps(unit));
|
|
881
|
-
};
|
|
882
|
-
const isRedundant = (name: string | undefined) =>
|
|
883
|
-
name !== undefined && findNamed(current, name) !== undefined;
|
|
884
|
-
// Instantiated units arrive anonymized.
|
|
885
|
-
const queue = module.body.map((unit) => ({ unit, isAnonymized: false }));
|
|
886
|
-
for (let index = 0; index < queue.length; index++) {
|
|
887
|
-
const unit = queue[index].unit;
|
|
888
|
-
const expand = (instance: InstanceDefinition, isLocal: boolean, prefix: string) => {
|
|
889
|
-
const units = instantiate(
|
|
890
|
-
pipeline,
|
|
891
|
-
modules,
|
|
892
|
-
current,
|
|
893
|
-
instance,
|
|
894
|
-
isLocal,
|
|
895
|
-
prefix,
|
|
896
|
-
unit.location,
|
|
897
|
-
);
|
|
898
|
-
queue.splice(index + 1, 0, ...units.map((inner) => ({ unit: inner, isAnonymized: true })));
|
|
899
|
-
};
|
|
900
|
-
switch (unit.kind) {
|
|
901
|
-
case "Anoninst":
|
|
902
|
-
expand(unit.instance, unit.isLocal, "");
|
|
903
|
-
continue;
|
|
904
|
-
case "Definition": {
|
|
905
|
-
const definition = unit.definition;
|
|
906
|
-
if (definition.kind === "Instance") {
|
|
907
|
-
expand(definition.instance, unit.isLocal, definition.name);
|
|
908
|
-
continue;
|
|
909
|
-
}
|
|
910
|
-
const anonymized =
|
|
911
|
-
definition.kind === "Recursive" || queue[index].isAnonymized
|
|
912
|
-
? definition
|
|
913
|
-
: pipeline.anonymizer.defn(current, definition);
|
|
914
|
-
const normalized: ModuleUnit = { ...unit, definition: anonymized };
|
|
915
|
-
if (definition.kind === "Recursive") {
|
|
916
|
-
emit(normalized);
|
|
917
|
-
continue;
|
|
918
|
-
}
|
|
919
|
-
const hyp = getUnitHyps(normalized)[0];
|
|
920
|
-
const found = findNamed(current, definition.name);
|
|
921
|
-
if (found?.hyp.kind === "Defn") {
|
|
922
|
-
const isMerged =
|
|
923
|
-
found.hyp.definition.kind === "Recursive" ||
|
|
924
|
-
isEqualHyp(substituteHyp(shift(found.depth + 1), found.hyp), hyp);
|
|
925
|
-
if (isMerged) {
|
|
926
|
-
const rest = queue.slice(index + 1);
|
|
927
|
-
const substituted = substituteUnits(
|
|
928
|
-
consSubstitution(at({ kind: "Ix", index: found.depth + 1 }, unit), shift(0)),
|
|
929
|
-
rest.map((entry) => entry.unit),
|
|
930
|
-
);
|
|
931
|
-
queue.splice(
|
|
932
|
-
index + 1,
|
|
933
|
-
rest.length,
|
|
934
|
-
...rest.map((entry, offset) => ({ ...entry, unit: substituted[offset] })),
|
|
935
|
-
);
|
|
936
|
-
continue;
|
|
937
|
-
}
|
|
938
|
-
}
|
|
939
|
-
emit(normalized);
|
|
940
|
-
continue;
|
|
941
|
-
}
|
|
942
|
-
case "Recursives":
|
|
943
|
-
case "Constants":
|
|
944
|
-
case "Variables":
|
|
945
|
-
emit(unit);
|
|
946
|
-
continue;
|
|
947
|
-
case "Axiom":
|
|
948
|
-
if (isRedundant(unit.name)) continue;
|
|
949
|
-
pipeline.latestModules = new Map(modules);
|
|
950
|
-
emit({ ...unit, expression: pipeline.anonymizer.expr(current, unit.expression) });
|
|
951
|
-
continue;
|
|
952
|
-
case "Theorem": {
|
|
953
|
-
const isDuplicate = isRedundant(unit.name);
|
|
954
|
-
const name = isDuplicate ? `(redundant)${unit.name}` : unit.name;
|
|
955
|
-
const proof: Proof = isDuplicate
|
|
956
|
-
? { kind: "Obvious", location: unit.proof.location }
|
|
957
|
-
: unit.proof;
|
|
958
|
-
pipeline.latestModules = new Map(modules);
|
|
959
|
-
const sequent = pipeline.anonymizer.sequent(current, unit.sequent);
|
|
960
|
-
const binding = bindTheoremName(name, sequent, unit.location);
|
|
961
|
-
const proofScope = adjoinAll(
|
|
962
|
-
adjoinAll(
|
|
963
|
-
current,
|
|
964
|
-
binding.hyps.map((hyp) => ({ ...hyp, isConstnessUnset: true })),
|
|
965
|
-
),
|
|
966
|
-
binding.sequent.context,
|
|
967
|
-
);
|
|
968
|
-
const anonymized = pipeline.anonymizer.proof(proofScope, proof);
|
|
969
|
-
const simplified = module.isImportant
|
|
970
|
-
? pipeline.generator.simplify(
|
|
971
|
-
getScopeHyps(proofScope),
|
|
972
|
-
binding.sequent.active,
|
|
973
|
-
anonymized,
|
|
974
|
-
getScopeTime(binding.sequent.context, "Always"),
|
|
975
|
-
)
|
|
976
|
-
: anonymized;
|
|
977
|
-
emit({ ...unit, name, sequent, proof: simplified });
|
|
978
|
-
continue;
|
|
979
|
-
}
|
|
980
|
-
case "Mutate":
|
|
981
|
-
pipeline.latestModules = new Map(modules);
|
|
982
|
-
emit({ ...unit, usable: pipeline.anonymizer.usable(current, unit.usable) });
|
|
983
|
-
continue;
|
|
984
|
-
case "Submod":
|
|
985
|
-
emit({ ...unit, module: normalize(pipeline, modules, current, unit.module) });
|
|
986
|
-
continue;
|
|
987
|
-
}
|
|
988
|
-
}
|
|
989
|
-
const normalized = { ...module, body, depth: scope?.size ?? 0 };
|
|
990
|
-
modules.set(module.name, normalized);
|
|
991
|
-
return normalized;
|
|
992
|
-
};
|
|
993
|
-
|
|
994
|
-
export interface TheoremSummary extends ProofSummary {
|
|
995
|
-
location: Locus | undefined;
|
|
996
|
-
}
|
|
997
|
-
|
|
998
|
-
export interface ModuleSummary extends ProofSummary {
|
|
999
|
-
theorems: TheoremSummary[];
|
|
1000
|
-
}
|
|
1001
|
-
|
|
1002
|
-
// Module.Gen.generate.
|
|
1003
|
-
export const generate = (
|
|
1004
|
-
pipeline: Pipeline,
|
|
1005
|
-
context: Hyp[],
|
|
1006
|
-
module: Mule,
|
|
1007
|
-
): { obligations: Obligation[]; summary: ModuleSummary } => {
|
|
1008
|
-
const obligations: Obligation[] = [];
|
|
1009
|
-
const theorems: TheoremSummary[] = [];
|
|
1010
|
-
let summary = EMPTY_SUMMARY;
|
|
1011
|
-
let current = context;
|
|
1012
|
-
for (const unit of module.body) {
|
|
1013
|
-
switch (unit.kind) {
|
|
1014
|
-
case "Theorem": {
|
|
1015
|
-
const binding = bindTheoremName(unit.name, unit.sequent, unit.location);
|
|
1016
|
-
const named = [...current, ...binding.hyps];
|
|
1017
|
-
const context = [...named, ...binding.sequent.context];
|
|
1018
|
-
const proof = pipeline.generator.generate(
|
|
1019
|
-
{ context, active: binding.sequent.active },
|
|
1020
|
-
unit.proof,
|
|
1021
|
-
getScopeTime(context, "Always"),
|
|
1022
|
-
);
|
|
1023
|
-
obligations.push(...pipeline.generator.collect(proof));
|
|
1024
|
-
const theorem = getProofSummary(proof);
|
|
1025
|
-
theorems.push({ ...theorem, location: unit.location });
|
|
1026
|
-
summary = joinSummaries(summary, theorem);
|
|
1027
|
-
const fact: Expr =
|
|
1028
|
-
unit.name === undefined
|
|
1029
|
-
? at(exprifySequent(unit.sequent), unit)
|
|
1030
|
-
: { kind: "Ix", index: 1, location: unit.location };
|
|
1031
|
-
current = [
|
|
1032
|
-
...named,
|
|
1033
|
-
{
|
|
1034
|
-
kind: "Fact",
|
|
1035
|
-
expression: fact,
|
|
1036
|
-
visibility: "Hidden",
|
|
1037
|
-
time: "Always",
|
|
1038
|
-
location: unit.location,
|
|
1039
|
-
},
|
|
1040
|
-
];
|
|
1041
|
-
continue;
|
|
1042
|
-
}
|
|
1043
|
-
case "Submod": {
|
|
1044
|
-
const generated = generate(pipeline, current, unit.module);
|
|
1045
|
-
obligations.push(...generated.obligations);
|
|
1046
|
-
summary = joinSummaries(summary, generated.summary);
|
|
1047
|
-
continue;
|
|
1048
|
-
}
|
|
1049
|
-
case "Mutate": {
|
|
1050
|
-
const [next, generated] = pipeline.generator.mutate(
|
|
1051
|
-
current,
|
|
1052
|
-
unit.mode,
|
|
1053
|
-
unit.usable,
|
|
1054
|
-
unit.location,
|
|
1055
|
-
);
|
|
1056
|
-
obligations.push(...generated);
|
|
1057
|
-
current = next;
|
|
1058
|
-
continue;
|
|
1059
|
-
}
|
|
1060
|
-
default:
|
|
1061
|
-
current = [...current, ...getUnitHyps(unit)];
|
|
1062
|
-
}
|
|
1063
|
-
}
|
|
1064
|
-
return { obligations, summary: { ...summary, theorems } };
|
|
1065
|
-
};
|