pub struct TermParser<'a> { /* private fields */ }Expand description
Recursive descent parser for Term syntax.
Implementations§
Source§impl<'a> TermParser<'a>
impl<'a> TermParser<'a>
Sourcepub const NAMED_ARG_MARKER: &'static str = "\u{27e8}named\u{27e9}"
pub const NAMED_ARG_MARKER: &'static str = "\u{27e8}named\u{27e9}"
Reserved head marking a NAMED ARGUMENT (x := e) at an argument position.
The parser emits MARKER x e – the parameter name carried as a Global, exactly as
crate::elaborate::DOT_MARKER carries a field name. The elaborator resolves it into the
declaration’s parameter order and discards the marker; it NEVER reaches a kernel term.
Sourcepub const PARAM_DEFAULT_MARKER: &'static str = "\u{27e8}default\u{27e9}"
pub const PARAM_DEFAULT_MARKER: &'static str = "\u{27e8}default\u{27e9}"
Reserved head wrapping a binder’s DECLARED TYPE when the binder carries a DEFAULT value:
(m : Int := 3) becomes Pi(m : MARKER Int 3). ....
The binder stays an ordinary Term::Pi and the default travels inside its param_type,
so the default reaches the declaration door through every existing pipe with no signature
change anywhere – and a consumer that forgets to strip it fails LOUDLY on an unbound
global rather than silently dropping the default.
Sourcepub const AUTO_PARAM_MARKER: &'static str = "\u{27e8}autoparam\u{27e9}"
pub const AUTO_PARAM_MARKER: &'static str = "\u{27e8}autoparam\u{27e9}"
Reserved head wrapping a binder’s declared type when the binder is TACTIC-FILLED:
(h : P := by decide) becomes Pi(h : MARKER P decide). ..., the tactic name carried as
a Global. Distinct from Self::PARAM_DEFAULT_MARKER because the two are discharged
differently: a default is a TERM inlined once per omission, an auto-param is a PROOF
searched afresh at every call site, about that call site’s own proposition.
Sourcepub const EXPLICIT_ALL_MARKER: &'static str = "\u{27e8}explicit\u{27e9}"
pub const EXPLICIT_ALL_MARKER: &'static str = "\u{27e8}explicit\u{27e9}"
Reserved head marking an EXPLICIT-ALL application @f – Lean’s escape hatch, and the
one the language had no spelling for at all.
★ WITHOUT IT AN IMPLICIT ARGUMENT CANNOT BE WRITTEN. Once a declaration’s binder kinds are
recorded, idf Nat Zero is not “supply the implicit and the explicit”: the elaborator
inserts a metavariable for the implicit position FIRST and then has two arguments for one
explicit parameter. @ is the only way to say “I am writing every parameter myself”, which
is exactly what a user reaches for when inference has failed – so a language without it
has no escape hatch on the day it is needed most.
@f a b is emitted as MARKER f a b: the marker is the SPINE HEAD, so a consumer
decomposing an application sees it in the position it already inspects, and the head it
marks is args[0]. TermParser::is_explicit_all_head and
TermParser::as_explicit_all are the whole reading interface.
Sourcepub const ELLIPSIS_MARKER: &'static str = "\u{27e8}..\u{27e9}"
pub const ELLIPSIS_MARKER: &'static str = "\u{27e8}..\u{27e9}"
Reserved atom marking the ELLIPSIS .. – “fill every remaining position with _”.
Only the elaborator knows the arity of the thing being applied, so the parser records the
REQUEST and the elaborator expands it into that many solved holes. Emitted as the last
component of <a, b, ..> and as the last argument of f ...
Sourcepub const IMPLICIT_BINDER_MARKER: &'static str = "\u{27e8}implicit\u{27e9}"
pub const IMPLICIT_BINDER_MARKER: &'static str = "\u{27e8}implicit\u{27e9}"
Reserved head wrapping a binder’s declared type when the binder is IMPLICIT:
{A : Type} -> B becomes Pi(A : MARKER Type). B.
Unlike the other three, a kind marker never leaves this module: every public entry point
reads the telescope’s kinds off it and then strips it, so the emitted term is the same
pure Term::Pi an explicit binder produces. It exists because a binder KIND is a
position in a telescope, and the only structure that survives the recursive descent in
telescope order is the telescope itself.
Sourcepub const STRICT_IMPLICIT_BINDER_MARKER: &'static str = "\u{27e8}strict\u{27e9}"
pub const STRICT_IMPLICIT_BINDER_MARKER: &'static str = "\u{27e8}strict\u{27e9}"
Reserved head wrapping a STRICT-IMPLICIT binder’s declared type: ⦃A : Type⦄ -> B.
Distinct from Self::IMPLICIT_BINDER_MARKER because Lean inserts the two at different
moments, and collapsing them at the parser would make the difference unrecoverable.
Sourcepub const INSTANCE_BINDER_MARKER: &'static str = "\u{27e8}instance\u{27e9}"
pub const INSTANCE_BINDER_MARKER: &'static str = "\u{27e8}instance\u{27e9}"
Reserved head wrapping an INSTANCE binder’s class type: [Inhabited A] -> B.
Sourcepub const STRICT_IMPLICIT_OPEN_ASCII: &'static str = "{{"
pub const STRICT_IMPLICIT_OPEN_ASCII: &'static str = "{{"
Lean’s ASCII spelling of the strict-implicit OPEN bracket ⦃ – {{.
Lean/Parser/Term.lean:346-347 defines both brackets as an alternation, guillemet or
doubled brace, and :377 documents them as one binder with two spellings: “A strict
implicit binder, ⦃y z : A⦄ or its ASCII alternative {{y z : A}}”. The ASCII one is the
spelling available to a writer with no unicode input method, so refusing it made Lean’s
fourth binder kind unreachable in practice from an ordinary keyboard.
Sourcepub const STRICT_IMPLICIT_CLOSE_ASCII: &'static str = "}}"
pub const STRICT_IMPLICIT_CLOSE_ASCII: &'static str = "}}"
Lean’s ASCII spelling of the strict-implicit CLOSE bracket ⦄ – }}.
See STRICT_IMPLICIT_OPEN_ASCII.
Sourcepub const EXPLICIT: BinderKind = BinderKind::Explicit
pub const EXPLICIT: BinderKind = BinderKind::Explicit
BinderKind::Explicit, nameable without naming the enum.
Sourcepub const IMPLICIT: BinderKind = BinderKind::Implicit
pub const IMPLICIT: BinderKind = BinderKind::Implicit
BinderKind::Implicit, nameable without naming the enum.
Sourcepub const STRICT_IMPLICIT: BinderKind = BinderKind::StrictImplicit
pub const STRICT_IMPLICIT: BinderKind = BinderKind::StrictImplicit
BinderKind::StrictImplicit, nameable without naming the enum.
Sourcepub const INSTANCE: BinderKind = BinderKind::Instance
pub const INSTANCE: BinderKind = BinderKind::Instance
BinderKind::Instance, nameable without naming the enum.
Sourcepub fn as_named_arg(term: &Term) -> Option<(&str, &Term)>
pub fn as_named_arg(term: &Term) -> Option<(&str, &Term)>
Recognize a NAMED ARGUMENT, returning the parameter name and the supplied value.
The spine is decomposed rather than pattern-matched on a fixed depth, so a named argument
whose value is itself an application ((x := f y)) is recognized too.
Sourcepub fn is_ellipsis(term: &Term) -> bool
pub fn is_ellipsis(term: &Term) -> bool
Is this term the ELLIPSIS marker?
Sourcepub fn is_explicit_all_head(term: &Term) -> bool
pub fn is_explicit_all_head(term: &Term) -> bool
Is this the HEAD of an explicit-all application – the thing a decomposed spine’s innermost
position holds for @f a b? The head f is then that spine’s first argument.
Sourcepub fn as_explicit_all(term: &Term) -> Option<&Term>
pub fn as_explicit_all(term: &Term) -> Option<&Term>
Recognize an explicit-all application @f, returning the head it marks. Only the
UNAPPLIED shape; an applied @f a b is read off the spine with
is_explicit_all_head, because that is the form the
elaborator’s application arm already has in hand.
Sourcepub fn as_default_param(param_type: &Term) -> Option<(&Term, &Term)>
pub fn as_default_param(param_type: &Term) -> Option<(&Term, &Term)>
Recognize a DEFAULTED binder’s param_type, returning the declared type and the default
value the caller may omit. A binder may be BOTH defaulted and non-explicit
({m : Int := 3}), so the kind marker is peeled first – reading only the outermost
marker would have made a defaulted implicit look like an ordinary one and dropped its
default in silence.
Sourcepub fn as_auto_param(param_type: &Term) -> Option<(&Term, &str)>
pub fn as_auto_param(param_type: &Term) -> Option<(&Term, &str)>
Recognize an AUTO-PARAM binder’s param_type, returning the declared type (the obligation)
and the name of the tactic that must discharge it at each call site.
Sourcepub fn binder_kinds_of(ty: &Term) -> Vec<BinderKind>
pub fn binder_kinds_of(ty: &Term) -> Vec<BinderKind>
The BINDER KINDS of a type’s top-level telescope, outermost first – one entry per Pi,
Explicit where no kind marker stands.
★ A KIND IS A POSITION, NOT A COUNT. A marker written inside a PARAMETER’S OWN TYPE
({A : Type} -> ({B : Type} -> B) -> A) lives under that parameter’s param_type and is
therefore not on this spine at all – which is precisely the distinction the running
implicit_count could not make, and why it reported two parameters for a declaration
that has one.
Sourcepub fn strip_binder_markers(term: &Term) -> Term
pub fn strip_binder_markers(term: &Term) -> Term
The same term with every BINDER marker removed – the pure kernel type a declaration is registered at, once the defaults have been read off it.
Argument-position sugar (a named argument, an ellipsis) is deliberately NOT stripped: those carry a request the elaborator has to ACT on, and silently dropping one would change what the user wrote into something that merely typechecks. Only the decoration on a binder’s type is erased here.
Sourcepub fn strip_kind_markers(term: &Term) -> Term
pub fn strip_kind_markers(term: &Term) -> Term
The same term with every BINDER-KIND marker removed and nothing else touched – the pure
kernel Pi telescope, with a defaulted or tactic-filled binder still carrying its payload
for the declaration door to read.
Sourcepub fn substitute_param_ref(term: &Term, name: &str, value: &Term) -> Term
pub fn substitute_param_ref(term: &Term, name: &str, value: &Term) -> Term
Substitute a declaration’s ACTUAL argument for a reference to parameter name inside a
DEFAULT value – (m : Int := mul n 2) applied as sized 5 inlines mul 5 2.
A default is parsed in the scope of the binders before it, so a reference to a NAMED
earlier binder is already a Term::Var and is substituted capture-avoidingly (an inner
binder of the same name shadows it, and substitution stops). A reference to an ANONYMOUS
earlier binder – Int -> (m : Int := mul n 2) -> Int, where position 0’s Pi binder is
"_" and the name n exists only in the definition body’s lambda telescope – parsed as
a Term::Global, and a Global is not shadowed by any binder, so it is replaced
unconditionally. Handling both is what keeps an inlined default from escaping as an open
term with a free n in it, which is the way this goes wrong.
Sourcepub fn find_top_level(input: &str, sep: &str) -> Option<usize>
pub fn find_top_level(input: &str, sep: &str) -> Option<usize>
The byte index of the first sep at TOP LEVEL of input – outside every bracket, outside
a guillemet name, and outside a string literal – or None if there is none.
WHY THE VERNACULAR NEEDS THIS. Definition <name> : <type> := <body>. is split by searching
for :=, and the FIRST := in the text is not always the separator. A defaulted binder puts
one inside the TYPE (Definition f : Int -> (m : Int := 3) -> Int := ... splits at the
default and the command is refused for a reason that names neither the default nor the
body), and a guillemet name puts one inside the NAME (Definition \u{ab}a := b\u{bb} : Nat := Zero.).
The same applies to the : that opens the type ascription. The lexical structure of a term
is this parser’s knowledge, so the scan lives here rather than being re-derived – once per
bug – by every door that has to split a sentence.
sep must not begin with a bracket, a quote or a guillemet: those characters are what the
scan is tracking, so a separator starting with one could never be at top level anyway.
Sourcepub fn parse_in(surface: &Surface, input: &str) -> Result<Term, ParseError>
pub fn parse_in(surface: &Surface, input: &str) -> Result<Term, ParseError>
Parse a term through a SURFACE: macros are expanded to a fixpoint first, then the notation in scope is read as part of the grammar.
Macro expansion is source-to-source and runs BEFORE the parse, because an alternative may invoke the macro it defines; notation is a parser layer, because a notation has a precedence and text has none. That is the whole staging, and it is the staging Lean uses.
Sourcepub fn parse_with_implicits_in(
surface: &Surface,
input: &str,
) -> Result<(Term, usize), ParseError>
pub fn parse_with_implicits_in( surface: &Surface, input: &str, ) -> Result<(Term, usize), ParseError>
parse_in, also reporting the leading implicit-binder count.
Sourcepub fn parse_with_binder_kinds(
input: &str,
) -> Result<(Term, Vec<BinderKind>), ParseError>
pub fn parse_with_binder_kinds( input: &str, ) -> Result<(Term, Vec<BinderKind>), ParseError>
parse, also reporting the BINDER KIND of every parameter of the parsed
type’s top-level telescope – the vector Context::set_binder_kinds is keyed at.
This is the honest successor to parse_with_implicits: a
count can only describe a PREFIX of implicits, so Nat -> {A : Type} -> A – an implicit
in second position, which Lean writes constantly – has no number that describes it.
Sourcepub fn parse_with_binder_kinds_in(
surface: &Surface,
input: &str,
) -> Result<(Term, Vec<BinderKind>), ParseError>
pub fn parse_with_binder_kinds_in( surface: &Surface, input: &str, ) -> Result<(Term, Vec<BinderKind>), ParseError>
parse_with_binder_kinds, through a Surface.
Sourcepub fn read_declaration(input: &str) -> Result<DeclarationSurface, ParseError>
pub fn read_declaration(input: &str) -> Result<DeclarationSurface, ParseError>
Parse a DECLARED TYPE into the kernel type it is registered at plus the surface information that has to be recorded beside it – binder kinds, default values and auto-param obligations, in telescope order.
The one call the declaration door needs. See DeclarationSurface.
Sourcepub fn read_declaration_in(
surface: &Surface,
input: &str,
) -> Result<DeclarationSurface, ParseError>
pub fn read_declaration_in( surface: &Surface, input: &str, ) -> Result<DeclarationSurface, ParseError>
read_declaration, through a Surface.
Sourcepub fn read_declared_type_in(
surface: &Surface,
input: &str,
) -> Result<(Term, usize), ParseError>
pub fn read_declared_type_in( surface: &Surface, input: &str, ) -> Result<(Term, usize), ParseError>
A DECLARED TYPE as the declaration door must carry it: the parsed type with its binder-kind markers STILL ON, beside the leading-implicit count the legacy model reads.
★ THE CHANNEL BETWEEN THE TWO HALVES WAS A usize, AND MATHLIB DOES NOT FIT THROUGH IT.
The elaborator has read Context::binder_kinds – one ParamKind per POSITION – ever
since full_param_kinds and the kinds match in elab_surface_uncoerced were written, and
this file has parsed all four of Lean’s brackets since the binder grammar landed. Between
them sat parse_with_implicits_in, which STRIPS the kind
markers and reports a number. A declaration door reading its type through that one cannot
recover a kind afterwards: declaration_surface then reads
Explicit for every binder, correctly, because by then the type no longer says anything
else. So Nat -> {A : Type} -> A -> A registered THREE EXPLICIT parameters – the count
stops at the first non-leading binder and describes only the prefix – ⦃A : Type⦄ became
an argument the caller now has to write, and [Inhabited A] stopped consulting the
instance database at all. theorem foo {α} [Monoid α] (a : α), the shape of very nearly
every declaration in mathlib, had no working spelling.
This door keeps the markers on, so the call the declaration door already makes –
declaration_surface, which reads as_kind_marked and whose kernel_type comes back
fully stripped – returns the truth instead of a flattened telescope. The count is
reported unchanged beside it, so the leading-implicit model that lives next to it is not
disturbed.
★ THE FIRST COMPONENT IS NOT A KERNEL TERM. Its parameter types are marker applications,
and every marker is an undeclared global the kernel refuses by name
(no_reserved_marker_is_inhabited_so_a_forgotten_one_fails_loudly) – which is the point:
a door that forgets to split gets UnboundVariable, not a term that typechecks by
accident. Take DeclarationSurface::kernel_type before registering anything.
Sourcepub fn read_declared_type(input: &str) -> Result<(Term, usize), ParseError>
pub fn read_declared_type(input: &str) -> Result<(Term, usize), ParseError>
read_declared_type_in, with no Surface in scope.
Sourcepub fn declaration_surface(ty: &Term) -> DeclarationSurface
pub fn declaration_surface(ty: &Term) -> DeclarationSurface
Split a declared Pi type or an inferred definition’s lambda telescope into its marker-free term and per-binder surface.
Safe to call on a type that has been through
parse_with_implicits: the binder KINDS are stripped by
that door and every binder then reads as Explicit, while the defaults and auto-params
– which that door deliberately leaves in place – are still recovered.
Sourcepub fn name_anonymous_binders(binders: &mut [BinderSurface], body: &Term)
pub fn name_anonymous_binders(binders: &mut [BinderSurface], body: &Term)
Give every ANONYMOUS binder the name the definition’s BODY binds at that position.
★ IN LOGOS THE BODY IS WHERE THE NAMES ARE. A -> B -> C is the only spelling of a
non-dependent signature and it names nothing – parse_arrow builds every one of those
binders as Pi(_ : A). .... So for Definition sub3 : Int -> Int -> Int -> Int := fun x => fun y => fun z => ... the parameter names x, y, z exist ONLY in the body’s
lambda telescope, and a named argument sub3 (y := 2) can be resolved from nowhere else.
A body name is taken only where the declared binder is anonymous, only while the two telescopes run in step, and never when it would COLLIDE with a name the declared type already gives another parameter – two parameters answering to one name is worse than one parameter with no name, because a named argument would then silently pick one of them.
Sourcepub fn name_anonymous_positions(names: &mut [String], body: &Term)
pub fn name_anonymous_positions(names: &mut [String], body: &Term)
name_anonymous_binders over a bare telescope of NAMES –
the shape a caller has when it read the names off a Pi spine rather than off a
DeclarationSurface.
★ ONE ANSWER TO “WHERE ARE THE PARAMETER NAMES”, IN THE FILE THAT OWNS THE SURFACE. The
rule is not obvious and it is not local: a non-dependent signature names nothing, the body’s
λ-telescope is the only place the writer’s names exist, and the two telescopes only run in
step until the first one ends. Re-deriving that at each door is how two doors come to
disagree about which parameter f (x := …) means.
★ AND A NAME MAY BE TAKEN ONLY ONCE. Two parameters answering to ONE name is worse than one
parameter with no name at all: a named argument silently picks the first, the other becomes
unreachable by name, and nothing reports it. (x : Int) -> Int -> Int defined as
fun y : Int => fun x : Int => … is exactly that shape – position 0 is DECLARED x while
position 1’s body binder is also x – and it is ordinary text, not a contrivance. A
position whose body name is already taken stays "_", so a named argument resolves to the
position that really is x or is refused by name.
Sourcepub fn inline_default(
binders: &[BinderSurface],
index: usize,
args: &[Term],
) -> Option<Term>
pub fn inline_default( binders: &[BinderSurface], index: usize, args: &[Term], ) -> Option<Term>
The DEFAULT value for parameter index, with every earlier parameter’s ACTUAL argument
substituted into it – sized 5 against Int -> (m : Int := mul n 2) -> Int inlines
mul 5 2, never an open term with a free n in it.
args must hold the arguments already resolved for positions 0..index; with fewer than
that this returns None rather than a term it cannot close. None also means the
parameter simply has no default.
★ CALL name_anonymous_binders FIRST. A default parsed in
the scope of an ANONYMOUS earlier binder reads that reference as a Term::Global, because
A -> B binds nothing the parser could have put in scope. Substitution is by NAME, so a
telescope still full of "_" substitutes nothing and hands back exactly the open term this
function exists to prevent – which is why the two are documented as a pair and tested as
one.
Sourcepub fn parse_with_implicits(input: &'a str) -> Result<(Term, usize), ParseError>
pub fn parse_with_implicits(input: &'a str) -> Result<(Term, usize), ParseError>
Parse a term, also returning how many leading implicit binders {x:T} it had —
the implicit-argument count the elaborator uses at application sites.