ezyang's blog

the arc of software bends towards understanding

Groom: human readable Show for Haskell

Tapping away at a complex datastructure, I find myself facing a veritable wall of Babel.

image

“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)…

image

Ah, much better!

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!

11 Comments

  1. Juho Vepsäläinen

    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)?

  2. Neil Mitchell
    All your images seem to 404 for me…
  3. zygoloid
  4. Anonymous
  5. Vincent

    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)

  6. Gleb
    ipprint package does pretty much the same thing.
  7. Edward Amsden
    They are broken here as well.
  8. Kartik Agaram
    Needs a leading ‘/’.
  9. Edward Z. Yang
    Oops! Fixed now.
  10. Edward Z. Yang
    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. :-)
  11. hydo
    Oh, that’s just AWESOME. Thank you!