Zulip Chat Archive
Stream: lean4
Topic: Print expression with info
Bhavik Mehta (Apr 04 2025 at 13:15):
Here's a small command I made out of curiosity, which prints the ring normal form of an expression:
import Mathlib
section
open Lean Elab Meta Command Mathlib.Tactic
elab "#ring_nf " e:term : command =>
Command.runTermElabM fun _ => do
let e ← Elab.Term.elabTermAndSynthesize e none
let f ← RingNF.M.run (← IO.mkRef {}) {} (RingNF.rewrite e)
IO.println s!"{← ppExpr f.expr}"
end
variable {R : Type*} [CommRing R] (a b : R)
#ring_nf (a + b) ^ 4
but in the output, I can't hover over subterms and get useful information. I presume this is because one of ppExpr
and IO.println
is discarding that information, what should I be using instead?
Bhavik Mehta (Apr 04 2025 at 13:17):
(I realize #conv ring_nf
does the same thing, this is just a toy example since I'm trying to learn)
Patrick Massot (Apr 04 2025 at 13:19):
I would guess logInfo
Bhavik Mehta (Apr 04 2025 at 13:20):
Ah! That works, thank you Patrick!
Bhavik Mehta (Apr 04 2025 at 13:20):
For completeness:
elab "#ring_nf " e:term : command =>
Command.runTermElabM fun _ => do
let e ← Elab.Term.elabTermAndSynthesize e none
let f ← RingNF.M.run (← IO.mkRef {}) {} (RingNF.rewrite e)
logInfo f.expr
Jannis Limperg (Apr 04 2025 at 13:25):
More generally, String
and Format
(which ppExpr
produces) are just text. MessageData
(which the implicit coercion produces) has the necessary metadata for hovers etc.
Bhavik Mehta (Apr 04 2025 at 15:30):
Jannis Limperg said:
More generally,
String
andFormat
(whichppExpr
produces) are just text.MessageData
(which the implicit coercion produces) has the necessary metadata for hovers etc.
I'd figured this bit out, but not how to actually display MessageData, logInfo
was the exact missing piece for me!
Last updated: May 02 2025 at 03:31 UTC