Skip to content

Commit

Permalink
feat: @[app_delab] (#4976)
Browse files Browse the repository at this point in the history
Adds `@[app_delab ident]` as a macro for `@[delab app.ident]`. Resolves
the identifier when expanding the macro, saving needing to use the fully
qualified identifiers that `@[delab]` requires. Also, unlike `@[delab]`,
throws an error if the identifier cannot be resolved.

Closes #4899
  • Loading branch information
kmill authored Aug 10, 2024
1 parent 95bf679 commit 5f31e93
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Lean/PrettyPrinter/Delaborator/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ unsafe def mkDelabAttribute : IO (KeyedDeclsAttribute Delab) :=
} `Lean.PrettyPrinter.Delaborator.delabAttribute
@[builtin_init mkDelabAttribute] opaque delabAttribute : KeyedDeclsAttribute Delab

macro "app_delab" id:ident : attr => do
match ← Macro.resolveGlobalName id.getId with
| [] => Macro.throwErrorAt id s!"unknown declaration '{id.getId}'"
| [(c, [])] => `(attr| delab $(mkIdentFrom (canonical := true) id (`app ++ c)))
| _ => Macro.throwErrorAt id s!"ambiguous declaration '{id.getId}'"

def getExprKind : DelabM Name := do
let e ← getExpr
pure $ match e with
Expand Down
34 changes: 34 additions & 0 deletions tests/lean/run/delabApp.lean
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Lean
/-!
# Testing features of the app delaborator, including overapplication
-/
Expand Down Expand Up @@ -77,3 +78,36 @@ def g (a : Nat) (b := 1) (c := 2) (d := 3) := a + b + c + d
-- Both the `start` and `stop` arguments are omitted.
/-- info: fun a => Array.foldl (fun x y => x + y) 0 a : Array Nat → Nat -/
#guard_msgs in #check fun (a : Array Nat) => a.foldl (fun x y => x + y) 0


/-!
Testing overriding the app delaborator with an `@[app_delab]`
-/

def myFun (x : Nat) : Nat := x

/-- info: myFun 2 : Nat -/
#guard_msgs in #check myFun 2

open Lean PrettyPrinter Delaborator in
@[app_delab myFun] def delabMyFun : Delab := withOverApp 0 `(id)

/-- info: id✝ 2 : Nat -/
#guard_msgs in #check myFun 2

/-!
Testing `@[app_delab]` error conditions.
-/

/-- error: unknown declaration 'noSuchFunction' -/
#guard_msgs in
open Lean PrettyPrinter Delaborator in
@[app_delab noSuchFunction] def delabErr1 : Delab := withOverApp 0 `(id)

def A.f := 0
def B.f := 0
open A B
/-- error: ambiguous declaration 'f' -/
#guard_msgs in
open Lean PrettyPrinter Delaborator in
@[app_delab f] def delabErr2 : Delab := withOverApp 0 `(id)

0 comments on commit 5f31e93

Please sign in to comment.