[Haskell-cafe] HughesPJ vs. Wadler-Leijen

Evan Laforge qdunkan at gmail.com
Tue Mar 20 18:26:59 CET 2012

On Tue, Mar 20, 2012 at 6:52 AM, Stephen Tetley
<stephen.tetley at gmail.com> wrote:
> Hi Ivan
> I haven't found any bugs in WL, however I do find the API somewhat
> confusing regarding line breaking (I would need to consult the manual
> to tell you the difference between linebreak, softline etc.). This is
> likely my failing rather than WL as usually I want formatting - "I
> know the layout" - rather than pretty printing - "the fit function
> finds the best layout".

Yeah, the 'group' combinator is at the center of it, but it took me
some fiddling around to get a feel for how it worked... and I still
don't have a feel for how it works when nested (as it is pervasively
if you use fillSep and the like).  Maybe it's elegantly minimal, but
it doesn't seem to be that intuitive, unless someday I come to a
realization that clears it all up.  The thing is, I don't think there
is a fit function that finds the best layout, I think it simply does
what the composition of groups tells it to do.

> I think there is room in the design space for a library whose API
> "favors" formatting rather than pretty-printing. I.e it has line
> printing that cannot be undone by `group`, or the combinators that use
> group are given more long-winded makes to make them secondary. I've
> bits and bobs on the go to do this, but nothing near a concrete
> library.

What I think I would like is some way to express a hierarchy of line
breaks.  So if I'm formatting a list, there's a break before/after
each comma and they are all equally good breaks.  But then if I nest
and format a list of lists, the outer breaks are considered better
breaks than the inner ones.  This would preserve the hierarchical
structure of the data by trying to break on the largest chunks first,
and control indentation too.  In fact, HughesPJ's fsep (or maybe it's
the 'best' in renderStyle) seems to get that right all on its own.

I also have a personal style that's hard to reconcile with the pprint
combinators, namely that lists that fit on one line don't have spaces
around the brackets: [1, 2, 3], but ones that must be wrapped do, and
the close bracket lines up with the open one:

[ 1, 2
, 3, 4

One other thing I've thought about is to have a pretty printer have
the option of returning a list of Docs, in increasing detail.  Then a
smart viewer (perhaps HTML + JS) could let you expand things by
clicking on them.  Or maybe it would be more practical to just teach
vim or emacs the output syntax and let folding take care of it.

More information about the Haskell-Cafe mailing list