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

Evan Laforge qdunkan at gmail.com
Sun Apr 23 01:51:52 UTC 2023


Sorry I'm late to the party.  I wanted something like that a long time
ago, and I thought "this should be easy" so I wrote a "simple" library
for it:

https://github.com/elaforge/karya/blob/work/Util/Format.hs
https://github.com/elaforge/karya/blob/work/Util/Pretty.hs

The reason for the scare quotes is that it turned out to be much
harder and more finicky than I thought.  This implementation still has
at least one formatting bug which I did track down at some point, but
I believe it was a missing logical distinction which would be hard to
fix.  But in fact I've been using it for about 9 years and it's worked
well enough.  I haven't done any performance analysis, but I've never
felt the need for it, and I've printed some pretty large things.

So, I don't know if it's hard because it's actually hard, or because I
didn't do a great design, or because I wasn't motivated to put further
work into it after it got to good enough.

On Mon, Apr 17, 2023 at 3:51 AM Johannes Waldmann
<johannes.waldmann at htwk-leipzig.de> 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.


More information about the Haskell-Cafe mailing list