Whether a local declaration should be found by type class search, tactics, etc. and shown in the goal display.
- default : LocalDeclKind
Participates fully in type class search, tactics, and is shown even if inaccessible.
For example: the
x
infun x => _
has the default kind. - implDetail : LocalDeclKind
Invisible to type class search or tactics, and hidden in the goal display.
This kind is used for temporary variables in macros. For example:
return (← foo) + bar
expands tofoo >>= fun __tmp => pure (__tmp + bar)
, where__tmp
has theimplDetail
kind. - auxDecl : LocalDeclKind
Auxiliary local declaration for recursive calls. The behavior is similar to
implDetail
.For example:
def foo (n : Nat) : Nat := _
adds the local declarationfoo : Nat → Nat
to allow recursive calls. This declaration has theauxDecl
kind.
Instances For
A declaration for a LocalContext
. This is used to register which free variables are in scope.
See LocalDecl.index
, LocalDecl.fvarId
, LocalDecl.userName
, LocalDecl.type
for accessors for
arguments common to both constructors.
- cdecl
(index : Nat)
(fvarId : FVarId)
(userName : Name)
(type : Expr)
(bi : BinderInfo)
(kind : LocalDeclKind)
: LocalDecl
A local variable without any value.
Lean.LocalContext.mkBinding
creates lambdas or foralls fromcdecl
s. - ldecl
(index : Nat)
(fvarId : FVarId)
(userName : Name)
(type value : Expr)
(nondep : Bool)
(kind : LocalDeclKind)
: LocalDecl
A let-bound free variable, with a value
value : Expr
. Ifnondep := false
, then the variable is definitionally equal to its value. Ifnondep := true
, then the variable has an opaque value; we call these "have-bound free variables."Lean.LocalContext.mkBinding
creates let/have expressions fromldecl
s.Important: The
nondep := true
case is subtle; it is not merely an opaqueldecl
!- In most contexts, nondependent
ldecl
s should be treated likecdecl
s. For example, suppose we have a tactic goalx : α := v (nondep) ⊢ b
. It would be incorrect forrevert x
to produce the goal⊢ have x : α := v; b
, since this would be saying "to proveb
without knowledge of the value ofx
, it suffices to provehave x : α := v; b
for this particular value ofx
." Instead,revert x
must produce the goal⊢ ∀ x : α, b
. Furthermore, given a goal⊢ have x : α := v; b
, theintro x
tactic should yield a dependentldecl
, since users expect to be able to make use of the value ofx
, plus, as discussed, ifintro
yielded a nondepldecl
thenintro x; revert x
would convert the goal into a forall, not ahave
. - Also:
value
might not be type correct. Metaprograms may decide to pretend that allnondep := true
ldecl
s arecdecl
s (for example, when reverting variables). As a consequence, nondepldecl
s may have type-incorrect values. This design decision allows metaprograms to not have to think about nondepldecl
s, so long asLocalDecl
values are consumed throughLocalDecl.isLet
andLocalDecl.value?
with(allowNondep := false)
. Rule: never use(generalizeNondepLet := false)
inmkBinding
-family functions within a local context you do not own. SeeLocalDecl.setNondep
for some additional discussion. - Where then do nondep ldecls come from? Common functions are
Meta.mapLetDecl
,Meta.withLetDecl
, andMeta.letTelescope
. Thehave
term syntax makes use of a nondep ldecl as well.
Therefore,
nondep := true
should be used with consideration. Its primary use is in metaprograms that enterlet
/have
telescopes and wish to reconstruct them. - In most contexts, nondependent
Instances For
Equations
Instances For
Equations
Instances For
The position of the decl in the local context.
Equations
Instances For
The unique id of the free variable.
Equations
Instances For
The pretty-printable name of the variable.
Equations
Instances For
Is the local declaration an implementation-detail hypothesis (including auxiliary declarations)?
Equations
Instances For
Sets the nondep
flag of an ldecl
, otherwise returns cdecl
s unchanged.
This is a low-level function, and it is the responsibility of the caller to ensure that
transitions of nondep
are valid.
Rules:
- If the declaration is not under the caller's control, then setting
nondep := false
must not be done. General nondependentldecl
s should be treated likecdecl
s. See also the docstring forLocalDecl.ldecl
about thevalue
not necessarily being type correct. - Setting
nondep := true
is usually fine.- Caution: be sure any relevant caches are cleared so that the value associated to this
FVarId
does not leak. - Caution: be sure that metavariables dependent on this declaration created before and after the transition are not mixed,
since unification does not check "
nondep
-compatibility" of local contexts when assigning metavariables.
- Caution: be sure any relevant caches are cleared so that the value associated to this
For example, setting nondep := false
is fine from within a telescope combinator, to update the local context
right before calling mkLetFVars
:
let lctx ← getLCtx
letTelescope e fun xs b => do
let lctx' ← xs.foldlM (init := lctx) fun lctx' x => do
let decl ← x.fvarId!.getDecl
-- Clear the flag if it's not a prop.
let decl' := decl.setNondep <| ← pure decl.isNondep <&&> Meta.isProp decl.type
pure <| lctx'.addDecl decl'
withLCtx' lctx' do
mkLetFVars (usedLetOnly := false) (generalizeNondepLet := false) xs b
- The declarations for
xs
are in the control of this metaprogram. mkLetFVars
does make use ofMetaM
caches.- Even if
e
has metavariables, these do not includexs
in their contexts, so the change of thenondep
flag does not cause any issues in theabstractM
system used bymkLetFVars
.
Equations
Instances For
A LocalContext is an ordered set of local variable declarations.
It is used to store the free variables (also known as local constants) that
are in scope. It also maps free variables corresponding to auxiliary declarations
(recursive references and where
and let rec
bindings) to their fully-qualified global names.
When inspecting a goal or expected type in the infoview, the local
context is all of the variables above the ⊢
symbol.
- fvarIdToDecl : PersistentHashMap FVarId LocalDecl
- decls : PersistentArray (Option LocalDecl)
Instances For
Equations
Instances For
Equations
Instances For
Low level API for creating local declarations (LocalDecl.cdecl
).
It is used to implement actions in the monads Elab
and Tactic
.
It should not be used directly since the argument (fvarId : FVarId)
is
assumed to be unique. You can create a unique fvarId with mkFreshFVarId
.
Equations
Instances For
Low level API for let declarations. Do not use directly.
Equations
Instances For
Low level API for auxiliary declarations. Do not use directly.
Equations
Instances For
Low level API for adding a local declaration. Do not use directly.
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
Instances For
Gets the declaration for expression e
in the local context.
If e
is not a free variable or not present then panics.
Equations
Instances For
Equations
Instances For
Returns true when the lctx contains the free variable e
.
Panics if e
is not an fvar.
Equations
Instances For
Return all of the free variables in the given context.
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
Instances For
Low-level function for updating the local context.
Assumptions about f
, the resulting nested expressions must be definitionally equal to their original values,
the index
nor fvarId
are modified.
Equations
Instances For
Set the kind of the given fvar.
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
Instances For
Given lctx₁ - exceptFVars
of the form (x_1 : A_1) ... (x_n : A_n)
, then return true
iff there is a local context B_1* (x_1 : A_1) ... B_n* (x_n : A_n)
which is a prefix
of lctx₂
where B_i
's are (possibly empty) sequences of local declarations.
Equations
Instances For
Equations
Instances For
Creates the expression fun x₁ .. xₙ => b
for free variables xs = #[x₁, .., xₙ]
,
suitably abstracting b
and the types for each of the xᵢ
.
Equations
Instances For
Creates the expression (x₁:α₁) → .. → (xₙ:αₙ) → b
for free variables xs = #[x₁, .., xₙ]
,
suitably abstracting b
and the types for each of the xᵢ
, αᵢ
.
Equations
Instances For
Equations
Instances For
Equations
Instances For
Return true
if lctx
contains a local declaration satisfying p
.
Equations
Instances For
Return true
if all declarations in lctx
satisfy p
.
Equations
Instances For
If option pp.sanitizeNames
is set to true
, add tombstone to shadowed local declaration names and ones contains macroscopes.
Equations
Instances For
Given an FVarId
, this function returns the corresponding user name,
but only if the name can be used to recover the original FVarId.
Equations
Instances For
Sort the given FVarId
s by the order in which they appear in lctx
. If any of
the FVarId
s do not appear in lctx
, the result is unspecified.