logicaffeine_kernel/interface/command.rs
1//! Vernacular commands for interacting with the Kernel.
2
3use super::surface::{Name, OpenClause, SurfaceDecl};
4use crate::Term;
5
6/// A command in the Vernacular language.
7#[derive(Debug, Clone)]
8pub enum Command {
9 /// Definition name : type := body.
10 ///
11 /// If `ty` is None, the type is inferred from the body.
12 /// If `is_hint` is true, register as a hint for auto tactic.
13 Definition {
14 name: String,
15 ty: Option<Term>,
16 body: Term,
17 is_hint: bool,
18 /// How many leading parameters of `ty` are implicit (`{x:T}` binders).
19 implicit_count: usize,
20 /// For a literate `## Theorem … Proof: <tactic>.`, the goal (in `Syntax` form) the
21 /// tactic derivation must CONCLUDE. `Some` marks this definition as a checked tactic
22 /// proof: the REPL rejects it unless `concludes body` reduces to exactly this goal,
23 /// so a failed tactic (which yields a well-typed *error* derivation) cannot pass as a
24 /// proof. `None` for every ordinary definition (including raw `d : Derivation := …`).
25 proof_goal: Option<Term>,
26 },
27
28 /// Check term.
29 ///
30 /// Prints the type of the term.
31 Check(Term),
32
33 /// Eval term.
34 ///
35 /// Normalizes and prints the term.
36 Eval(Term),
37
38 /// A parser-extension declaration: notation, fixity, a macro, or a section boundary.
39 ///
40 /// It extends the SURFACE and never the namespace. That separation is the point: a flat global
41 /// namespace with one phantom per notation is a collision waiting to happen, so a command that
42 /// declares a way of SPELLING something must have nowhere to put a global even by accident.
43 Surface(SurfaceDecl),
44
45 /// `Open Alpha.` / `Open Alpha Beta.` / `Open Alpha (a b).` / `Open Alpha hiding (a).`
46 ///
47 /// It is a `Command` and not a [`SurfaceDecl`] for the reason [`Command::Export`] is: a clause
48 /// that NAMES members (`(a b)`, `hiding (a)`) can only be checked against the ENVIRONMENT, and
49 /// a clause naming something the namespace does not declare narrows the opening to nothing
50 /// while reading exactly like one that worked. Both forms go through one door so the check can
51 /// never be skipped by taking the other.
52 Open(Vec<OpenClause>),
53
54 /// `Open Alpha in <command>.` — an opening scoped to exactly ONE command.
55 ///
56 /// It is a `Command` and not a [`SurfaceDecl`] because it is CONTROL FLOW: the scope has to be
57 /// wound back after the command it brackets, which nothing that merely mutates the surface can
58 /// do. Lean's `open … in` scopes the same way and for the same reason.
59 OpenIn {
60 opens: Vec<OpenClause>,
61 inner: Box<Command>,
62 },
63
64 /// `Export Alpha (token other).` — make `Alpha.token` answer to `token` IN THE CURRENT
65 /// NAMESPACE, permanently, rather than only while an `open` is in scope. Lean's alias is a
66 /// persistent environment extension, so it outlives the `End` of the namespace it was written
67 /// in — which is the whole difference between `export` and `open`.
68 ///
69 /// It is a `Command` and not a [`SurfaceDecl`] because every one of its failure modes needs the
70 /// ENVIRONMENT to detect: a name the source namespace does not declare, and a name the current
71 /// namespace already declares. A surface declaration cannot see the context, and an `export`
72 /// that could not check would be an alias pointing nowhere with nothing to say so.
73 ///
74 /// Lean's `export` takes its name list explicitly and so does this. There is no way to enumerate
75 /// a namespace's members from the kernel context, and a bare `Export Alpha.` that exported
76 /// nothing would be exactly the quiet no-op an unknown `Open` would have been.
77 Export { namespace: Name, names: Vec<Name> },
78
79 /// `Protected Definition Foo.x : …` — Lean's `protected def`, which declares an ordinary
80 /// constant that stops answering to its SHORT name.
81 ///
82 /// It is a WRAPPER rather than a flag on the declaration for the reason `Open … in` is one: the
83 /// mark is applied AFTER the inner command succeeds, so a declaration that does not check
84 /// protects nothing, and no declaration door has to grow a field it would then have to
85 /// remember to pass on. Corpus case T02-012 names this exact behaviour as the thing a
86 /// translator must not assume is inert — "it changes which short name resolves at a use site".
87 Protected(Box<Command>),
88
89 /// `Print Axioms <name>.` — what a declaration RESTS ON: the axioms it cites transitively
90 /// through its own definition graph, and the names that resolve to nothing in this context.
91 ///
92 /// Lean's `#print axioms`, and it is the report that makes an admission visible. Lean warns on
93 /// the declaration that CONTAINS a `sorry` and says nothing about its dependants, so a theorem
94 /// three lemmas downstream of one looks finished; this reads the transitive closure, so the
95 /// taint is a property of the proof rather than of a warning somebody might have missed, and it
96 /// cannot be lost by renaming, re-exporting or citing at a distance.
97 ///
98 /// It carries a [`Name`] rather than a key because it is a REFERENCE, and a reference reads in
99 /// the namespace scope it is written in, exactly as a term's does.
100 PrintAxioms(Name),
101
102 /// A documentation block (`/-- … -/`, `/-! … -/`) or a line comment (`-- …`).
103 ///
104 /// Carries NO payload, by construction. `Literal::Text` exists, so a docstring *could* be
105 /// emitted as a term — it would typecheck, put a name the source never declared into the flat
106 /// namespace where it can collide, and be useless, since there are no string operations. A
107 /// variant with nothing in it makes that mistake unrepresentable: prose is tolerated beside a
108 /// declaration and is never data.
109 Comment,
110
111 /// `Theorem name : P := proof.` — a PROPOSITION and its proof.
112 ///
113 /// The statement must be a proposition, the proof is checked by both kernels against it by
114 /// exact conversion, and the name is then published as Lean publishes a theorem
115 /// (`Context::declare_theorem_checked`): later conversion may unfold it, at the lowest delta
116 /// priority. The proof travels in the command as the evidence a reader re-checks.
117 Theorem { name: String, ty: Term, proof: Term },
118
119 /// `Theorem name : P := by tac; tac.` — a proposition proved by a tactic script over the kernel's
120 /// own goals ([`crate::tactics::GoalState`]). The script's finished proof term enters through the
121 /// door a [`Command::Theorem`]'s proof does; the script is never trusted.
122 TheoremBy {
123 name: String,
124 ty: Term,
125 script: Vec<crate::tactics::TacticStep>,
126 },
127
128 /// `Opaque name : T := value.` — the checked-then-opaque gateway (Lean's `opaque`): checked
129 /// like a `Theorem`, with no requirement that `T` be a proposition, and never unfolded.
130 Opaque { name: String, ty: Term, value: Term },
131
132 /// `Axiom name : T.` — an explicit assumption. Its type is checked to be a type, it never has a
133 /// body, and `Print Axioms` lists it on itself and on everything resting on it.
134 Axiom { name: String, ty: Term },
135
136 /// `Inductive Name (params) : arity := C1 : T1 | … with Name2 (params) : arity := … .`
137 ///
138 /// An inductive BLOCK registered exactly as declared, through the kernel's checked block door
139 /// (`Context::add_mutual_inductives` / `add_universe_poly_inductives`) — the door the Lean
140 /// importer uses. The written parameters ARE the parameters (never re-inferred), the rest of the
141 /// arity is indices, and a block of several members is one mutual block whether or not they
142 /// mention each other.
143 ///
144 /// Written whenever a header declares its arity, names universe parameters, or joins members
145 /// with `with`; a header that does none of those is the legacy [`Command::Inductive`].
146 InductiveBlock { members: Vec<InductiveMember> },
147
148 /// `Quotient Type.{u}.`, `Quotient Constructor.{u}.`, `Quotient Lift.{u, v} using Eq.`,
149 /// `Quotient Induction.{u}.` — one of the kernel's four quotient primitives (Lean's `init_quot`).
150 ///
151 /// The kernel has exactly one certified quotient, keyed at `kernel.quot.type`, `.mk`, `.lift`
152 /// and `.ind`; the command names WHICH primitive to install and its universe parameters (always
153 /// written, through [`Command::Universes`]), and `Lift` names the equality its respect hypothesis
154 /// is stated with. `quotient::install` checks the rest: the order the primitives come in, and
155 /// that the named equality is the canonical one.
156 Quotient {
157 kind: crate::quotient::Kind,
158 equality: Option<String>,
159 },
160
161 /// `Structure Prod := Prod.fst Prod.snd.` — structure η for an inductive already declared,
162 /// through projections already declared.
163 ///
164 /// It declares nothing: it records that `inductive` is a STRUCTURE, which is what licenses
165 /// η (`x ≡ mk p̄ (proj₀ p̄ x) … (projₙ p̄ x)`) and Lean's structure iota. The `Repl` grants it only
166 /// once each projection is shown to COMPUTE its field — `projᵢ p̄ (mk p̄ f̄) ≡ fᵢ` for a generic
167 /// constructor application — the same check the environment certificate makes before it
168 /// registers η, so a document and a certificate cannot disagree about what a structure is.
169 Structure {
170 inductive: String,
171 projections: Vec<String>,
172 },
173
174 /// `Definition name.{u, v} …` (and `Theorem`, `Opaque`, `Axiom`, `Inductive`) — universe
175 /// parameters BINDING over the declaration they are written on.
176 ///
177 /// The level variables `params` may occur in the declaration's types and terms and nowhere else;
178 /// the declaration registers through the kernel's universe-polymorphic door
179 /// (`declare_universe_poly`, `add_universe_poly_declaration`, `declare_opaque_checked` with a
180 /// telescope, `add_universe_poly_inductives`), which REFUSES a level variable the telescope
181 /// does not bind. It is a wrapper rather than a field for the reason `Protected` and `Open … in`
182 /// are wrappers: it scopes one declaration, every declaration door takes it the same way, and a
183 /// telescope on a command that declares nothing is refused rather than ignored.
184 Universes {
185 params: Vec<String>,
186 inner: Box<Command>,
187 },
188
189 /// Inductive Name (params) := C1 : T1 | C2 : T2.
190 ///
191 /// Defines an inductive type with its constructors.
192 /// Supports optional type parameters for polymorphic inductives.
193 Inductive {
194 name: String,
195 /// Type parameters: (param_name, param_type)
196 /// e.g., for `List (A : Type)`, params = [("A", Type)]
197 params: Vec<(String, Term)>,
198 sort: Term,
199 constructors: Vec<(String, Term)>,
200 },
201}
202
203/// One member of an [`Command::InductiveBlock`].
204///
205/// The shape is the legacy [`Command::Inductive`]'s: the parameters are split out and bound by
206/// name, and `sort` is the DECLARED arity after the parameters — its binders are the indices and
207/// its final sort is the universe the type lives in; it refers to a parameter as the global of
208/// that name.
209///
210/// A constructor is written one of two ways, the same for every constructor of a member:
211///
212/// * `full_constructors == false` — `C : T`, where `T` omits the parameter prefix and refers to a
213/// parameter as the global of its name; the registration puts the member's parameters back;
214/// * `full_constructors == true` — `C (p₁ : T₁) … (pₙ : Tₙ) : T`, the constructor RESTATING its
215/// parameter telescope, and `constructors` holds each FULL type, parameters bound as ordinary
216/// binders. The restated types may spell a parameter differently from the member (Lean's
217/// `HAdd.mk` binds `γ : outParam (Type w)` where `HAdd`'s arity says `Type w`); the kernel's block
218/// door checks them and registers the constructor exactly as written. A member with no
219/// parameters has one form only, and the flag is `false`.
220#[derive(Debug, Clone)]
221pub struct InductiveMember {
222 pub name: String,
223 pub params: Vec<(String, Term)>,
224 pub sort: Term,
225 pub constructors: Vec<(String, Term)>,
226 pub full_constructors: bool,
227}