Pretty class as alternative for Show class

John Meacham john at
Wed Feb 13 22:46:31 EST 2008

On Wed, Feb 13, 2008 at 03:01:21PM +0100, Christian Maeder wrote:
> John Meacham wrote:
> > See my 'Doc' project. which includes both a show alternative class like
> > you describe as
> > well as a class abstracting different pretty printers.
> > 
> >
> Why should I use your DocLike module (instead of
> Text.PrettyPrint.HughesPJ directly)?

Because then the same code will work with any of the pretty printers out
there, Strings, and ShowS directly. It is an abstract type. I also have
ones that spit out html and one that spits out ascii text along with a
function that goes from screen position to what was used to create it so
you can find out what people clicked a mouse on.

> from module DocLike:
>     x <+> y = x <> char ' ' <> y
>     x <$> y = x <> char '\n' <> y
>     encloseSep l r s ds = enclose l r (hcat $ punctuate s ds)
>     enclose l r x   = l <> x <> r
>     list            = encloseSep lbracket rbracket comma
>     tupled          = encloseSep lparen   rparen  comma
>     semiBraces      = encloseSep lbrace   rbrace  semi
> 1. The indentation is wrong for Doc from your module Pretty (and strings):
> *Pretty> text "a" DocLike.<+> (text "b" DocLike.<$> text "c") :: Doc
> a b
> c
> 2. encloseSep may produce too long lines without breaks
> The HughesPJ output is correct:
> *DocLike> P.text "a" <+> (P.text "b" <$> P.text "c")
> a b
>   c

This is simply due to the underlying implementation you are using, there
are many instances, you should get exactly the same results as you do
with HughesPJ if you use the HughesPJ instance as it is what is used to
do the formatting. It is possible however that I did not override all
the needed methods for the HughesPJ instance as I more often use a
modified version of Daan's parser that can handle things like html and
ansi color codes so might not have noticed if I forgot one.

those are only default implementations, suitable for things like
'String' or 'ShowS', it is expected instances that use a real back end
would override them with more interesting things.


John Meacham - ⑆⑆john⑈

More information about the Libraries mailing list