Skip to content

Commit

Permalink
chore: review delaborators, make sure they respond to pp.explicit (l…
Browse files Browse the repository at this point in the history
…eanprover#5830)

Rule: if an expression contains an implicit argument that the
delaborator would omit, only use the delaborator if `pp.explicit` is
false.
  • Loading branch information
kmill authored and tobiasgrosser committed Oct 27, 2024
1 parent bbf206e commit 4e292be
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
19 changes: 11 additions & 8 deletions src/Lean/PrettyPrinter/Delaborator/Builtins.lean
Original file line number Diff line number Diff line change
Expand Up @@ -1014,15 +1014,15 @@ Delaborates an `OfNat.ofNat` literal.
`@OfNat.ofNat _ n _` ~> `n`.
-/
@[builtin_delab app.OfNat.ofNat]
def delabOfNat : Delab := whenPPOption getPPCoercions <| withOverApp 3 do
def delabOfNat : Delab := whenNotPPOption getPPExplicit <| whenPPOption getPPCoercions <| withOverApp 3 do
delabOfNatCore (showType := (← getPPOption getPPNumericTypes))

/--
Delaborates the negative of an `OfNat.ofNat` literal.
`-@OfNat.ofNat _ n _` ~> `-n`
-/
@[builtin_delab app.Neg.neg]
def delabNeg : Delab := whenPPOption getPPCoercions do
def delabNeg : Delab := whenNotPPOption getPPExplicit <| whenPPOption getPPCoercions do
delabNegIntCore (showType := (← getPPOption getPPNumericTypes))

/--
Expand All @@ -1031,12 +1031,12 @@ Delaborates a rational number literal.
and `-@OfNat.ofNat _ n _ / @OfNat.ofNat _ m` ~> `-n / m`
-/
@[builtin_delab app.HDiv.hDiv]
def delabHDiv : Delab := whenPPOption getPPCoercions do
def delabHDiv : Delab := whenNotPPOption getPPExplicit <| whenPPOption getPPCoercions do
delabDivRatCore (showType := (← getPPOption getPPNumericTypes))

-- `@OfDecimal.ofDecimal _ _ m s e` ~> `m*10^(sign * e)` where `sign == 1` if `s = false` and `sign = -1` if `s = true`
@[builtin_delab app.OfScientific.ofScientific]
def delabOfScientific : Delab := whenPPOption getPPCoercions <| withOverApp 5 do
def delabOfScientific : Delab := whenNotPPOption getPPExplicit <| whenPPOption getPPCoercions <| withOverApp 5 do
let expr ← getExpr
guard <| expr.getAppNumArgs == 5
let .lit (.natVal m) ← pure (expr.getArg! 2) | failure
Expand Down Expand Up @@ -1080,6 +1080,9 @@ def coeDelaborator : Delab := whenPPOption getPPCoercions do
let e ← getExpr
let .const declName _ := e.getAppFn | failure
let some info ← Meta.getCoeFnInfo? declName | failure
if (← getPPOption getPPExplicit) && info.coercee != 0 then
-- Approximation: the only implicit arguments come before the coercee
failure
let n := e.getAppNumArgs
withOverApp info.numArgs do
match info.type with
Expand All @@ -1092,7 +1095,7 @@ def coeDelaborator : Delab := whenPPOption getPPCoercions do
| .coeSort => `(↥$(← withNaryArg info.coercee delab))

@[builtin_delab app.dite]
def delabDIte : Delab := whenPPOption getPPNotation <| withOverApp 5 do
def delabDIte : Delab := whenNotPPOption getPPExplicit <| whenPPOption getPPNotation <| withOverApp 5 do
-- Note: we keep this as a delaborator for now because it actually accesses the expression.
guard $ (← getExpr).getAppNumArgs == 5
let c ← withAppFn $ withAppFn $ withAppFn $ withAppArg delab
Expand All @@ -1109,7 +1112,7 @@ where
return (← delab, h.getId)

@[builtin_delab app.cond]
def delabCond : Delab := whenPPOption getPPNotation <| withOverApp 4 do
def delabCond : Delab := whenNotPPOption getPPExplicit <| whenPPOption getPPNotation <| withOverApp 4 do
guard $ (← getExpr).getAppNumArgs == 4
let c ← withAppFn $ withAppFn $ withAppArg delab
let t ← withAppFn $ withAppArg delab
Expand All @@ -1129,7 +1132,7 @@ def delabNamedPattern : Delab := do
`($x:ident@$h:ident:$p:term)

-- Sigma and PSigma delaborators
def delabSigmaCore (sigma : Bool) : Delab := whenPPOption getPPNotation do
def delabSigmaCore (sigma : Bool) : Delab := whenNotPPOption getPPExplicit <| whenPPOption getPPNotation do
guard $ (← getExpr).getAppNumArgs == 2
guard $ (← getExpr).appArg!.isLambda
withAppArg do
Expand Down Expand Up @@ -1209,7 +1212,7 @@ partial def delabDoElems : DelabM (List Syntax) := do
prependAndRec x : DelabM _ := List.cons <$> x <*> delabDoElems

@[builtin_delab app.Bind.bind]
def delabDo : Delab := whenPPOption getPPNotation do
def delabDo : Delab := whenNotPPOption getPPExplicit <| whenPPOption getPPNotation do
guard <| (← getExpr).isAppOfArity ``Bind.bind 6
let elems ← delabDoElems
let items ← elems.toArray.mapM (`(doSeqItem|$(·):doElem))
Expand Down
2 changes: 1 addition & 1 deletion tests/lean/delabOverApp.lean.expected.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ f +' g : Nat → Nat
f * g : Nat → Nat
(f * g) 1 : Nat
mul f g : Nat → Nat
mul f g 1 : Nat
mul f g (@OfNat.ofNat Nat 1 (instOfNatNat 1)) : Nat
4 changes: 2 additions & 2 deletions tests/lean/run/delabProjectionApp.lean
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ set_option pp.explicit true
#guard_msgs in #check x.val
/-- info: y.val : Nat -/
#guard_msgs in #check y.val
/-- info: (@Fin''.toFin0 5 z).val : Nat -/
/-- info: (@Fin''.toFin0 (@OfNat.ofNat Nat 5 (instOfNatNat 5)) z).val : Nat -/
#guard_msgs in #check z.val
/-- info: (@D.toA 5 d).x : Nat -/
/-- info: (@D.toA (@OfNat.ofNat Nat 5 (instOfNatNat 5)) d).x : Nat -/
#guard_msgs in #check d.x

end
Expand Down
4 changes: 2 additions & 2 deletions tests/lean/rwWithSynthesizeBug.lean.expected.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
rwWithSynthesizeBug.lean:36:18-39:15: error: unsolved goals
inst : Bar Nat := @Bar.mk Nat 0
h : @w Nat (@f Nat inst 5)
inst : Bar Nat := @Bar.mk Nat (@OfNat.ofNat Nat 0 (instOfNatNat 0))
h : @w Nat (@f Nat inst (@OfNat.ofNat Nat 5 (instOfNatNat 5)))
⊢ True

0 comments on commit 4e292be

Please sign in to comment.