diff --git a/src/Lean/Elab/PreDefinition/Basic.lean b/src/Lean/Elab/PreDefinition/Basic.lean index 94e7e94dec..8ca0f1035a 100644 --- a/src/Lean/Elab/PreDefinition/Basic.lean +++ b/src/Lean/Elab/PreDefinition/Basic.lean @@ -164,8 +164,11 @@ def addNonRec (preDef : PreDefinition) (applyAttrAfterCompilation := true) (all /-- Eliminate recursive application annotations containing syntax. These annotations are used by the well-founded recursion module to produce better error messages. -/ -def eraseRecAppSyntaxExpr (e : Expr) : CoreM Expr := - Core.transform e (post := fun e => pure <| TransformStep.done <| if (getRecAppSyntax? e).isSome then e.mdataExpr! else e) +def eraseRecAppSyntaxExpr (e : Expr) : CoreM Expr := do + if e.find? hasRecAppSyntax |>.isSome then + Core.transform e (post := fun e => pure <| TransformStep.done <| if hasRecAppSyntax e then e.mdataExpr! else e) + else + return e def eraseRecAppSyntax (preDef : PreDefinition) : CoreM PreDefinition := return { preDef with value := (← eraseRecAppSyntaxExpr preDef.value) } diff --git a/src/Lean/Elab/RecAppSyntax.lean b/src/Lean/Elab/RecAppSyntax.lean index 3b75f11504..f537b1d85b 100644 --- a/src/Lean/Elab/RecAppSyntax.lean +++ b/src/Lean/Elab/RecAppSyntax.lean @@ -11,27 +11,35 @@ namespace Lean private def recAppKey := `_recApp /-- - We store the syntax at recursive applications to be able to generate better error messages - when performing well-founded and structural recursion. +We store the syntax at recursive applications to be able to generate better error messages +when performing well-founded and structural recursion. -/ def mkRecAppWithSyntax (e : Expr) (stx : Syntax) : Expr := - mkMData (KVMap.empty.insert recAppKey (DataValue.ofSyntax stx)) e + mkMData (KVMap.empty.insert recAppKey (.ofSyntax stx)) e /-- - Retrieve (if available) the syntax object attached to a recursive application. +Retrieve (if available) the syntax object attached to a recursive application. -/ def getRecAppSyntax? (e : Expr) : Option Syntax := match e with - | Expr.mdata d _ => + | .mdata d _ => match d.find recAppKey with | some (DataValue.ofSyntax stx) => some stx | _ => none | _ => none /-- - Checks if the `MData` is for a recursive applciation. +Checks if the `MData` is for a recursive applciation. -/ def MData.isRecApp (d : MData) : Bool := d.contains recAppKey +/-- +Return `true` if `getRecAppSyntax? e` is a `some`. +-/ +def hasRecAppSyntax (e : Expr) : Bool := + match e with + | .mdata d _ => d.isRecApp + | _ => false + end Lean