[prev in list] [next in list] [prev in thread] [next in thread]
List: haskell-cafe
Subject: Re: [Haskell-cafe] pretty-printing with fixed indentation increase per sub-structure
From: Benjamin Redelings <benjamin.redelings () gmail ! com>
Date: 2023-05-04 15:50:02
Message-ID: b3d382e6-74fe-54dd-5075-efd369e6d4aa () gmail ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
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.
[Attachment #5 (text/html)]
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Hi,</p>
1. Have you seen the "prettiest printer" article here?
<p>
<a class="moz-txt-link-freetext" \
href="https://jyp.github.io/posts/towards-the-prettiest-printer.html">https://jyp.github.io/posts/towards-the-prettiest-printer.html</a></p>
<p>It says:</p>
<p>
<blockquote type="cite">
<p>Wadler’s design fares somewhat better. It does not suffer
from the above problem… <em>by default</em>. That is, it
lacks the capability to express that sub-documents should be
vertically aligned — compositionally.</p>
</blockquote>
...</p>
<p>
<blockquote type="cite"><br>
<ul>
<li>
<p>Objection 2: <em>Leijen’s extension of Wadler’s design
solves the issue: it provides an <code>align</code>
combinator.</em></p>
</li>
</ul>
</blockquote>
<br>
</p>
<p>A package based on (a later version of) the design in this
article is available here:</p>
<p> <a class="moz-txt-link-freetext" \
href="https://hackage.haskell.org/package/pretty-compact">https://hackage.haskell.org/package/pretty-compact</a></p>
<p>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. <br>
</p>
<p><br>
</p>
<p>2. Doesn't the wl-print package already have a `nest` combinator?</p>
<p>
<a class="moz-txt-link-freetext" \
href="https://hackage.haskell.org/package/wl-pprint-1.2.1/docs/Text-PrettyPrint-Leijen \
.html">https://hackage.haskell.org/package/wl-pprint-1.2.1/docs/Text-PrettyPrint-Leijen.html</a></p>
<p>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?</p>
<p><br>
</p>
<p></p>
<p>3. Have you seen hindent? It has a module called HIndent.Pretty
that might be relevant to laying out Haskell source.<br>
</p>
Does that help?<br>
<p>-BenRI</p>
<p><br>
</p>
<div class="moz-cite-prefix">On 4/17/23 6:50 AM, Johannes Waldmann
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:f64ff1e0-6d05-84e0-94d5-26c0768712af@htwk-leipzig.de">Dear
Cafe,
<br>
<br>
I was looking for a way to pretty-print Haskell literals
<br>
(with lists, tuples, records with named and positional notation)
<br>
like this example
<br>
<br>
( Leftist
<br>
{ tree = Branch
<br>
{ left = Branch { left = Leaf, key = 4, right = Leaf }
<br>
, key = 3
<br>
, right = Leaf
<br>
}
<br>
, refs = listToFM [ ( Ref 13, [ 0 ] ), ( Ref 17, [ ] ) ]
<br>
}
<br>
, [ Any, Any ]
<br>
)
<br>
<br>
for each sub-structure, the indentation level
<br>
(for the following lines) should increase - by a _fixed_ amount.
<br>
in the above example: line break after "tree = Branch".
<br>
But (missing from this example), line break _before_
<br>
the list starts in "{ foo = [ 42 , ... ] ... }".
<br>
<br>
I found this impossible to do with wl-pprint
<br>
but perhaps I did not try hard enough.
<br>
<br>
<br>
Instead, I "invented" combinators `nest` and `skip`
<br>
and made this prototypical implementation
<br>
<a class="moz-txt-link-freetext" \
href="https://gitlab.imn.htwk-leipzig.de/autotool/all0/-/blob/master/todoc/src/Text/Pr \
ettyPrint/Dent.hs">https://gitlab.imn.htwk-leipzig.de/autotool/all0/-/blob/master/todoc/src/Text/PrettyPrint/Dent.hs</a>
(it has some explanatory text at the top)
<br>
see also
<a class="moz-txt-link-freetext" \
href="https://gitlab.imn.htwk-leipzig.de/autotool/all0/-/issues/960">https://gitlab.imn.htwk-leipzig.de/autotool/all0/-/issues/960</a>
<br>
<br>
but certainly this cannot be a new idea.
<br>
<br>
<br>
While I do like the semantics (in the context of my application),
<br>
I don't like the performance of my implementation.
<br>
What am I doing wrong?
<br>
It's just updating indentation level and current position,
<br>
this should not take any time at all?
<br>
<br>
Of course, it would be best if I don't need the implementation at
all -
<br>
if the effect could be achieved via some combinators in
<br>
established libraries (that have optimized implementation).
<br>
<br>
Any pointers appreciated.
<br>
<br>
<br>
Best regards - J.
<br>
_______________________________________________
<br>
Haskell-Cafe mailing list
<br>
To (un)subscribe, modify options or view archives go to:
<br>
<a class="moz-txt-link-freetext" \
href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a>
<br>
Only members subscribed via the mailman list are allowed to post.<br>
</blockquote>
</body>
</html>
[Attachment #6 (text/plain)]
_______________________________________________
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.
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic