Robust prettyprinter for output of auto-generated Show instances

Version on this page:0.2@rev:1
LTS Haskell 14.27:
Stackage Nightly 2019-09-21:
Latest on Hackage:

See all snapshots show-prettyprint appears in

BSD-3-Clause licensed and maintained by David Luposchainsky <dluposchainsky (λ) google>
This version can be pinned in stack with:show-prettyprint-0.2@sha256:24ee3b70d63e2bb27a70080f581ca61da01a371c3411355f2677f43b53c16ec2,1674

Module documentation for 0.2

Prettyprint Show output

Output of nested data structures by Show instances is often very hard to read. This package offers a simple function to insert line breaks and indentation into that output so that the semantics are unchanged, but makes it much easier to read.

The package does not rely on a parser for actual Haskell; instead, it merely reacts on parentheses, commas and the like. This makes it fairly robust even in the face of invalid Show instances, that may not produce valid Haskell code.

For example, consider this nested data structure:

nestedExample = fromList
    [ ("hello", Left  (Pair True ()))
    , ("world", Right (Record { r1 = ('c', -1.2e34), r2 = 123 }))
    , ("!"    , Left  (Pair False ())) ]

Applying show to it results in the fairly dense representation

fromList [("!",Left (Pair False ())),("hello",Left (Pair True ())),("world",Right (Record {r1 = ('c',-1.2e34), r2 = 123}))]

With the functions defined in this module, we can make this output a bit more readable,

fromList [("!"
          ,Left (Pair False ()))
         ,("hello",Left (Pair True ()))
          ,Right (Record {r1 = ('c'
                         ,r2 = 123}))]

Related packages

There is a similar package on Hackage called pretty-show, which takes a more flexible approach to prettyprinting show-formatted strings. This has its advantages and disadvantages.

  prettyprint-show (this) pretty-show (alternative)
Use case fire and forget, debugging flexible, working with the output
API One core value (rest: convenience) Multiple combinable values
Extensibility low, not intended decent, e.g. HTML rendering
Robustness high: only cares about parentheses, bugs in bad output can be hacked around medium: relies on output that follows Haskell’s lexical structure



Prettyprint based on the prettyprinter library, instead of ansi-wl-pprint. To support the Diagnostic module, the Trifecta-generated Doc has to be rendered still, so we cannot drop the dependency on ansi-wl-pprint just yet.