Lean.Meta.WHNF

# Smart unfolding support #

@[extern lean_get_structural_rec_arg_pos]

Forward declaration. It is defined in the module src/Lean/Elab/PreDefinition/Structural/Eqns.lean. It is possible to avoid this hack if we move Structural.EqnInfo and Structural.eqnInfoExt to this module.

@[inline]
Equations

Add auxiliary annotation to indicate the match-expression e must be reduced when performing smart unfolding.

Equations
Equations

Add auxiliary annotation to indicate expression e (a match alternative rhs) was successfully reduced by smart unfolding.

Equations
Equations

# Helper methods #

def Lean.Meta.isAuxDef (constName : Lean.Name) :
Equations

# Helper functions for reducing recursors #

def Lean.Meta.mkProjFn (ctorVal : Lean.ConstructorVal) (us : ) (params : ) (i : Nat) (major : Lean.Expr) :

Create the ith projection major. It tries to use the auto-generated projection functions if available. Otherwise falls back to Expr.proj.

Equations
• One or more equations did not get rendered due to their size.

# Helper function for extracting "stuck term" #

Return some (Expr.mvar mvarId) if metavariable mvarId is blocking reduction.

# Weak Head Normal Form auxiliary combinators #

@[specialize #[]]
partial def Lean.Meta.whnfEasyCases (e : Lean.Expr) (k : ) :

Auxiliary combinator for handling easy WHNF cases. It takes a function for handling the "hard" cases as an argument

Instances For

The "match" compiler uses if-then-else expressions and other auxiliary declarations to compile match-expressions such as

match v with
| 'a' => 1
| 'b' => 2
| _   => 3


because it is more efficient than using casesOn recursors. The method reduceMatcher? fails if these auxiliary definitions (e.g., ite) cannot be unfolded in the current transparency setting. This is problematic because tactics such as simp use TransparencyMode.reducible, and most users assume that expressions such as

match 0 with
| 0 => 1
| 100 => 2
| _ => 3


should reduce in any transparency mode. Thus, we define a custom canUnfoldAtMatcher predicate for whnfMatcher.

This solution is not very modular because modications at the match compiler require changes here. We claim this is defensible because it is reducing the auxiliary declaration defined by the match compiler.

Alternative solution: tactics that use TransparencyMode.reducible should rely on the equations we generated for match-expressions. This solution is also not perfect because the match-expression above will not reduce during type checking when we are not using TransparencyMode.default or TransparencyMode.all.

Equations
• One or more equations did not get rendered due to their size.
Equations
• One or more equations did not get rendered due to their size.
def Lean.Meta.project? (e : Lean.Expr) (i : Nat) :
Equations

Reduce kernel projection Expr.proj .. expression.

Equations
partial def Lean.Meta.whnfCore (e : Lean.Expr) (deltaAtProj : ) (simpleReduceOnly : ) :

Apply beta-reduction, zeta-reduction (i.e., unfold let local-decls), iota-reduction, expand let-expressions, expand assigned meta-variables.

The parameter deltaAtProj controls how to reduce projections s.i. If deltaAtProj == true, then delta reduction is used to reduce s (i.e., whnf is used), otherwise whnfCore.

If simpleReduceOnly, then iota and projection reduction are not performed. Note that the value of deltaAtProj is irrelevant if simpleReduceOnly = true.

partial def Lean.Meta.whnfCore.go (deltaAtProj : ) (simpleReduceOnly : ) (e : Lean.Expr) :

Recall that _sunfold auxiliary definitions contains the markers: markSmartUnfoldingMatch (*) and markSmartUnfoldingMatchAlt (**). For example, consider the following definition

def r (i j : Nat) : Nat :=
i +
match j with
| Nat.zero => 1
| Nat.succ j =>
i + match j with
| Nat.zero => 2
| Nat.succ j => r i j


produces the following _sunfold auxiliary definition with the markers

def r._sunfold (i j : Nat) : Nat :=
i +
(*) match j with
| Nat.zero => (**) 1
| Nat.succ j =>
i + (*) match j with
| Nat.zero => (**) 2
| Nat.succ j => (**) r i j


match expressions marked with markSmartUnfoldingMatch (*) must be reduced, otherwise the resulting term is not definitionally equal to the given expression. The recursion may be interrupted as soon as the annotation markSmartUnfoldingAlt (**) is reached.

For example, the term r i j.succ.succ reduces to the definitionally equal term i + i * r i j

Equations

Auxiliary method for unfolding a class projection.

Auxiliary method for unfolding a class projection. when transparency is set to TransparencyMode.instances. Recall that class instance projections are not marked with [reducible] because we want them to be in "reducible canonical form".

Unfold definition using "smart unfolding" if possible.

Equations
• One or more equations did not get rendered due to their size.
@[specialize #[]]
partial def Lean.Meta.whnfHeadPred (e : Lean.Expr) (pred : ) :
def Lean.Meta.whnfUntil (e : Lean.Expr) (declName : Lean.Name) :
Equations

Try to reduce matcher/recursor/quot applications. We say they are all "morally" recursor applications.

Equations
• One or more equations did not get rendered due to their size.
@[implemented_by Lean.Meta.reduceBoolNativeUnsafe]
opaque Lean.Meta.reduceBoolNative (constName : Lean.Name) :
@[implemented_by Lean.Meta.reduceNatNativeUnsafe]
opaque Lean.Meta.reduceNatNative (constName : Lean.Name) :
Equations
• One or more equations did not get rendered due to their size.
@[inline]
def Lean.Meta.withNatValue {α : Type} (a : Lean.Expr) (k : NatLean.MetaM ()) :
Equations
Equations
• One or more equations did not get rendered due to their size.
Equations
Equations
• One or more equations did not get rendered due to their size.
@[export lean_whnf]
partial def Lean.Meta.whnfImp (e : Lean.Expr) :

If e is a projection function that satisfies p, then reduce it

Equations
• One or more equations did not get rendered due to their size.