[Haskell-cafe] pretty-printing with fixed indentation increase per sub-structure

Benjamin Redelings benjamin.redelings at gmail.com
Thu May 4 15:50:02 UTC 2023


Hi,

1. Have you seen the "prettiest printer" article here?

https://jyp.github.io/posts/towards-the-prettiest-printer.html

It says:

> Wadler’s design fares somewhat better. It does not suffer from the 
> above problem… /by default/. That is, it lacks the capability to 
> express that sub-documents should be vertically aligned — compositionally.
>
...

>
>  *
>
>     Objection 2: /Leijen’s extension of Wadler’s design solves the
>     issue: it provides an |align| combinator./
>

A package based on (a later version of) the design in this article is 
available here:

https://hackage.haskell.org/package/pretty-compact

This claims to be more ideal ("Prettiest") than either the Hughes 
("Pretty") or Wadler ("Prettier") printers.  I think it uses dynamic 
programming to avoid being too slow.  If I understand correctly, GHC 
internally uses a version of the Hughes pretty printer, not the 
Wadler-Leijen one.


2. Doesn't the wl-print package already have a `nest` combinator?

https://hackage.haskell.org/package/wl-pprint-1.2.1/docs/Text-PrettyPrint-Leijen.html

It also has the `align` combinator.  If I remember correctly, these are 
part of the Leijen extension to Wadler.  Are these not enough to get the 
behavior that you want?


3. Have you seen hindent?  It has a module called HIndent.Pretty that 
might be relevant to laying out Haskell source.

Does that help?

-BenRI


On 4/17/23 6:50 AM, Johannes Waldmann wrote:
> Dear Cafe,
>
> I was looking for a way to pretty-print Haskell literals
> (with lists, tuples, records with named and positional notation)
> like this example
>
> ( Leftist
>     { tree = Branch
>         { left = Branch { left = Leaf, key = 4, right = Leaf }
>         , key = 3
>         , right = Leaf
>         }
>     , refs = listToFM [ ( Ref 13, [ 0 ] ), ( Ref 17, [ ] ) ]
>     }
> , [ Any, Any ]
> )
>
> for each sub-structure, the indentation level
> (for the following lines) should increase - by a _fixed_ amount.
> in the above example: line break after "tree = Branch".
> But (missing from this example), line break _before_
> the list starts in "{ foo = [ 42 , ... ] ... }".
>
> I found this impossible to do with wl-pprint
> but perhaps I did not try hard enough.
>
>
> Instead, I "invented" combinators `nest` and `skip`
> and made this prototypical implementation
> https://gitlab.imn.htwk-leipzig.de/autotool/all0/-/blob/master/todoc/src/Text/PrettyPrint/Dent.hs 
>  (it has some explanatory text at the top)
> see also https://gitlab.imn.htwk-leipzig.de/autotool/all0/-/issues/960
>
> but certainly this cannot be a new idea.
>
>
> While I do like the semantics (in the context of my application),
> I don't like the performance of my implementation.
> What am I doing wrong?
> It's just updating indentation level and current position,
> this should not take any time at all?
>
> Of course, it would be best if I don't need the implementation at all -
> if the effect could be achieved via some combinators in
> established libraries (that have optimized implementation).
>
> Any pointers appreciated.
>
>
> Best regards - J.
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20230504/92fec007/attachment.html>


More information about the Haskell-Cafe mailing list