blob: 51f60e8262031efa9400949682a3979a281b6f8e [file]
/-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
module
prelude
public import Lean.Elab.PreDefinition.Basic
public import Lean.Elab.PreDefinition.Structural.Basic
public section
namespace Lean.Elab.Structural
open Meta
partial def addSmartUnfoldingDefAux (preDef : PreDefinition) (recFns : Array (Name × Nat)) :
MetaM PreDefinition := do
return { preDef with
declName := mkSmartUnfoldingNameFor preDef.declName
value := (← visit preDef.value)
modifiers := default
}
where
/--
Auxiliary method for annotating `match`-alternatives with `markSmartUnfoldingMatch` and `markSmartUnfoldingMatchAlt`.
It uses the following approach:
- Whenever it finds a `match` application `e` s.t. `recArgHasLooseBVarsAt fnName recArgPos e`
for some function `fnName` of the mutual group with recursive argument at `recArgPos`,
it marks the `match` with `markSmartUnfoldingMatch`, and each alternative that does not contain a nested marked `match`
is marked with `markSmartUnfoldingMatchAlt`.
Recall that this is the condition used at `mkBRecOn`.
-/
visit (e : Expr) : MetaM Expr := do
match e with
| Expr.lam .. => lambdaTelescope e fun xs b => do mkLambdaFVars xs (← visit b)
| Expr.forallE .. => forallTelescope e fun xs b => do mkForallFVars xs (← visit b)
| Expr.letE n type val body nondep =>
mapLetDecl n type (← visit val) (nondep := nondep) fun x => do
visit (body.instantiate1 x)
| Expr.mdata d b => return mkMData d (← visit b)
| Expr.proj n i s => return mkProj n i (← visit s)
| Expr.app .. =>
let processApp (e : Expr) : MetaM Expr :=
e.withApp fun f args =>
return mkAppN (← visit f) (← args.mapM visit)
match (← matchMatcherApp? e) with
| some matcherApp =>
if recFns.all fun (fnName, recArgPos) => !recArgHasLooseBVarsAt fnName recArgPos e then
processApp e
else
let mut altsNew := #[]
for alt in matcherApp.alts, numParams in matcherApp.altNumParams do
let altNew ← lambdaBoundedTelescope alt numParams fun xs altBody => do
unless xs.size = numParams do
throwError "unexpected matcher application alternative{indentExpr alt}\nat application{indentExpr e}"
let altBody ← visit altBody
let containsSUnfoldMatch := Option.isSome <| altBody.find? fun e => smartUnfoldingMatch? e |>.isSome
let altBody := if !containsSUnfoldMatch then markSmartUnfoldingMatchAlt altBody else altBody
mkLambdaFVars xs altBody
altsNew := altsNew.push altNew
return markSmartUnfoldingMatch { matcherApp with alts := altsNew }.toExpr
| _ => processApp e
| _ => return e
partial def addSmartUnfoldingDef
(docCtx : LocalContext × LocalInstances) (preDef : PreDefinition) (recFns : Array (Name × Nat)) :
TermElabM Unit := do
if (← isProp preDef.type) then
return ()
else
withEnableInfoTree false do
let preDefSUnfold ← addSmartUnfoldingDefAux preDef recFns
-- `structuralRecursion` has already abstracted the nested proofs in `preDef.value`
addNonRec docCtx preDefSUnfold (cleanupValue := true) (abstractProofs := false)
end Lean.Elab.Structural