Haskell’s Show and pretty printing bad practice

Most of times when I was coding Haskell code, I always implemented the Show classtype whenever I wanted to print things on the screen in a nice and fashionable way.

It seems however this is a really bad practice that hasn’t been taught enough in the Haskell community. I discovered this in a StackOverflow question that had nothing to do with pretty printing, but the discussion led to that1.

A user named Matt Fenwick suggested that for whatever reason the guy who asked the question shouldn’t implement the Show classtype, later on I started to comment:

Me: Why not declare Show classtype for the Expr? just curious?

hammar: Show is meant as a form of lightweight serialization. It’s not meant for pretty-printing, although people often abuse it that way. In particular, if a Read instance is also defined, one should be able to expect that read . show is the identity function.

After that realization, I went out and looked over for different pretty printing solutions in Haskell. This was quite frustrating, for some reason I didn’t find any library that would define pretty printing functions for basic containers like List, Set, Map.

The closest I’ve found to an out of the box pretty printing solution was the pretty-show package. However this doesn’t create a new classtype for pretty printing, making it really limiting IMO.

There is also the well established pretty package, This one provides combinators to create pretty printing functions easily, however, it doesn’t provide a classtype or a default implementation for the containers mentioned above.

Lastly the most promising package I’ve found is called GenericPretty. This one provides a classtype for pretty printing and implements some Prelude ADT, however no containers whatsoever and no combinators for generating pretty printing.

In conclusion, the pretty printing solutions are so, so… yet there is potential for something new raising up in the future.


1 | Because of this the title was later changed to Show and Pretty-Printing