Skip to content

Commit

Permalink
feat: display coercions with a type ascription (leanprover#6119)
Browse files Browse the repository at this point in the history
This PR adds a new delab option `pp.coercions.types` which, when
enabled, will display all coercions with an explicit type ascription.

[Link to Zulip
discussion](https://leanprover.zulipchat.com/#narrow/channel/239415-metaprogramming-.2F-tactics/topic/Roundtripping.20delaboration.20involving.20coercions)

Towards leanprover#4315
  • Loading branch information
tonyxty authored and JovanGerb committed Jan 21, 2025
1 parent c8ec11c commit b1ac3ed
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/Lean/PrettyPrinter/Delaborator/Builtins.lean
Original file line number Diff line number Diff line change
Expand Up @@ -1115,17 +1115,18 @@ def coeDelaborator : Delab := whenPPOption getPPCoercions do
delabAppCore nargs (delabHead info nargs) (unexpand := false)
where
delabHead (info : CoeFnInfo) (nargs : Nat) (insertExplicit : Bool) : Delab := do
guard <| !insertExplicit
if info.type == .coeFun && nargs > 0 then
-- In the CoeFun case, annotate with the coercee itself.
-- We can still see the whole coercion expression by hovering over the whitespace between the arguments.
withNaryArg info.coercee <| withAnnotateTermInfo delab
else
withAnnotateTermInfo do
match info.type with
| .coe => `(↑$(← withNaryArg info.coercee delab))
| .coeFun => `(⇑$(← withNaryArg info.coercee delab))
| .coeSort => `(↥$(← withNaryArg info.coercee delab))
withTypeAscription (cond := ← getPPOption getPPCoercionsTypes) do
guard <| !insertExplicit
if info.type == .coeFun && nargs > 0 then
-- In the CoeFun case, annotate with the coercee itself.
-- We can still see the whole coercion expression by hovering over the whitespace between the arguments.
withNaryArg info.coercee <| withAnnotateTermInfo delab
else
withAnnotateTermInfo do
match info.type with
| .coe => `(↑$(← withNaryArg info.coercee delab))
| .coeFun => `(⇑$(← withNaryArg info.coercee delab))
| .coeSort => `(↥$(← withNaryArg info.coercee delab))

@[builtin_delab app.dite]
def delabDIte : Delab := whenNotPPOption getPPExplicit <| whenPPOption getPPNotation <| withOverApp 5 do
Expand Down
6 changes: 6 additions & 0 deletions src/Lean/PrettyPrinter/Delaborator/Options.lean
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ register_builtin_option pp.coercions : Bool := {
group := "pp"
descr := "(pretty printer) hide coercion applications"
}
register_builtin_option pp.coercions.types : Bool := {
defValue := false
group := "pp"
descr := "(pretty printer) display coercion applications with a type ascription"
}
register_builtin_option pp.universes : Bool := {
defValue := false
group := "pp"
Expand Down Expand Up @@ -251,6 +256,7 @@ def getPPLetVarTypes (o : Options) : Bool := o.get pp.letVarTypes.name (getPPAll
def getPPNumericTypes (o : Options) : Bool := o.get pp.numericTypes.name pp.numericTypes.defValue
def getPPNatLit (o : Options) : Bool := o.get pp.natLit.name (getPPNumericTypes o && !getPPAll o)
def getPPCoercions (o : Options) : Bool := o.get pp.coercions.name (!getPPAll o)
def getPPCoercionsTypes (o : Options) : Bool := o.get pp.coercions.types.name pp.coercions.types.defValue
def getPPExplicit (o : Options) : Bool := o.get pp.explicit.name (getPPAll o)
def getPPNotation (o : Options) : Bool := o.get pp.notation.name (!getPPAll o)
def getPPParens (o : Options) : Bool := o.get pp.parens.name pp.parens.defValue
Expand Down
26 changes: 26 additions & 0 deletions tests/lean/run/coeAttrs.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/-!
# Tests for `pp.coercions.types` option
-/

-- With the option off (default)
/--
info: n : Nat
h : n = 0
⊢ ↑n = 0
-/
#guard_msgs in
example (n : Nat) (h : n = 0) : (↑n : Int) = 0 := by
trace_state
simp [h]

-- With the option on
/--
info: n : Nat
h : n = 0
⊢ (↑n : Int) = 0
-/
#guard_msgs in
set_option pp.coercions.types true in
example (n : Nat) (h : n = 0) : (↑n : Int) = 0 := by
trace_state
simp [h]

0 comments on commit b1ac3ed

Please sign in to comment.