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

Johannes Waldmann johannes.waldmann at htwk-leipzig.de
Mon Apr 17 10:50:48 UTC 2023


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.


More information about the Haskell-Cafe mailing list