Groom: human readable Show for Haskell
by Edward Z. Yang
Tapping away at a complex datastructure, I find myself facing a veritable wall of Babel.

“Zounds!” I exclaim, “The GHC gods have cursed me once again with a derived Show instance with no whitespace!” I mutter discontently to myself, and begin pairing up parentheses and brackets, scanning the sheet of text for some discernible feature that may tell me of the data I am looking for.
But then, a thought comes to me: “Show is specified to be a valid Haskell expression without whitespace. What if I parsed it and then pretty-printed the resulting AST?”
Four lines of code later (with the help of Language.Haskell)...

How to use it. In your shell:
cabal install groom
and in your program:
import Text.Groom main = putStrLn . groom $ yourDataStructureHere
Update. Gleb writes in to mention ipprint which does essentially the same thing but also has a function for putStrLn . show and has some tweaked defaults including knowledge of your terminal size.
Update 2. Don mentions to me the pretty-show package by Iavor S. Diatchki which also does similar functionality, and comes with an executable that lets you prettify output offline!
Did you enjoy this post? Please subscribe to my feed!
Cool idea.
Could you please fix the image links (they should point to http://blog.ezyang.com/img/groom/messy.png and http://blog.ezyang.com/img/groom/clean.png)?
All your images seem to 404 for me…
Your images point to http://blog.ezyang.com/2010/07/groom-human-readable-show-for-haskell/img/groom/messy.png but should point to http://blog.ezyang.com/img/groom/messy.png. For the benefit of other readers:
they are relative urls, lacking a “/”
http://blog.ezyang.com/img/groom/messy.png
http://blog.ezyang.com/img/groom/clean.png
I see the images from the main page
(urls like: http://blog.ezyang.com/img/groom/messy.png)
but not from the article page
(urls like: http://blog.ezyang.com/2010/07/groom-human-readable-show-for-haskell/img/groom/messy.png)
ipprint package does pretty much the same thing.
They are broken here as well.
Needs a leading ‘/’.
Oops! Fixed now.
Gleb, it looks like someone’s beat me to the punch. It looks like ipprint also does a bunch of extra magic like check what your terminal size is and format the code accordingly; that’s pretty cool. :-)
Oh, that’s just AWESOME. Thank you!