refactor(delate): Unfold matchers into function

This commit is contained in:
Leni Aniva 2025-03-17 11:26:13 -07:00
parent 3b4b196a30
commit 4643992c3b
Signed by: aniva
GPG Key ID: 4D9B1C8D10EA4C50
1 changed files with 17 additions and 15 deletions

View File

@ -67,8 +67,17 @@ def _root_.Lean.Name.isAuxLemma (n : Lean.Name) : Bool := n matches .num (.str _
/-- Unfold all lemmas created by `Lean.Meta.mkAuxLemma`. These end in `_auxLemma.nn` where `nn` is a number. -/ /-- Unfold all lemmas created by `Lean.Meta.mkAuxLemma`. These end in `_auxLemma.nn` where `nn` is a number. -/
@[export pantograph_unfold_aux_lemmas_m] @[export pantograph_unfold_aux_lemmas_m]
def unfoldAuxLemmas (e : Expr) : CoreM Expr := do def unfoldAuxLemmas : Expr → CoreM Expr :=
Lean.Meta.deltaExpand e Lean.Name.isAuxLemma (Lean.Meta.deltaExpand · Lean.Name.isAuxLemma)
/-- Unfold all matcher applications -/
@[export pantograph_unfold_matchers_m]
def unfoldMatchers (expr : Expr) : CoreM Expr :=
Core.transform expr λ e' => do
let .some mapp ← Meta.matchMatcherApp? e' | return .continue e'
let .some matcherInfo := (← getEnv).find? mapp.matcherName | panic! "Matcher must exist"
let f ← Meta.instantiateValueLevelParams matcherInfo mapp.matcherLevels.toList
let mdata := KVMap.empty.insert `matcher (DataValue.ofName mapp.matcherName)
return .visit $ .mdata mdata (f.betaRev e'.getAppRevArgs (useZeta := true))
/-- /--
Force the instantiation of delayed metavariables even if they cannot be fully Force the instantiation of delayed metavariables even if they cannot be fully
@ -157,28 +166,21 @@ partial def instantiateDelayedMVars (eOrig: Expr) : MetaM Expr :=
/-- /--
Convert an expression to an equiavlent form with Convert an expression to an equiavlent form with
1. No nested delayed assigned mvars 1. No nested delayed assigned mvars
2. No aux lemmas 2. No aux lemmas or matchers
3. No assigned mvars 3. No assigned mvars
-/ -/
@[export pantograph_instantiate_all_m] @[export pantograph_instantiate_all_m]
def instantiateAll (e: Expr): MetaM Expr := do def instantiateAll (e : Expr) : MetaM Expr := do
let e ← instantiateDelayedMVars e let e ← instantiateDelayedMVars e
let e ← unfoldAuxLemmas e let e ← unfoldAuxLemmas e
let e ← Core.transform e λ e' => do let e ← unfoldMatchers e
match ← Meta.matchMatcherApp? e' with
| .none => return .continue e'
| .some mapp =>
let .some matcherInfo := (← getEnv).find? mapp.matcherName | panic! "Matcher must exist"
let f ← Meta.instantiateValueLevelParams matcherInfo mapp.matcherLevels.toList
let mdata := KVMap.empty.insert `matcher (DataValue.ofName mapp.matcherName)
return .visit $ .mdata mdata (f.betaRev e'.getAppRevArgs (useZeta := true))
return e return e
structure DelayedMVarInvocation where structure DelayedMVarInvocation where
mvarIdPending: MVarId mvarIdPending : MVarId
args: Array (FVarId × (Option Expr)) args : Array (FVarId × (Option Expr))
-- Extra arguments applied to the result of this substitution -- Extra arguments applied to the result of this substitution
tail: Array Expr tail : Array Expr
-- The pending mvar of any delayed assigned mvar must not be assigned in any way. -- The pending mvar of any delayed assigned mvar must not be assigned in any way.
@[export pantograph_to_delayed_mvar_invocation_m] @[export pantograph_to_delayed_mvar_invocation_m]