Which pretty print library? [closed]
Asked Answered
I

1

40

So from a glance at hackage I can see 5 pretty printing libraries:

  • good old HughesPJ in pretty
  • wl-pprint-extras
  • wl-pprint-terminfo
  • wl-pprint
  • ansi-wl-pprint
  • wl-pprint-text

Oh wait, was that 6? 6 pretty printing libraries... no wait, we'll come in again.

Anyway, they're all Wadler-Leijen except of course HughesPJ. My understanding is that WL is simpler and faster, so is probably preferred for new code.

wl-pprint and wl-pprint-extras seem to be the same... I can't tell what's "extra" about the latter, or what "Free" means here (the module is Text.PrettyPrint.Free).

wl-pprint-terminfo and ansi-wl-pprint both seem to be variants with ANSI terminal colors and whatnot, and seem equivalent except that wl-pprint-terminfo doesn't have any docs.

wl-pprint-text, of course, uses Text. I don't know how much difference that actually makes wrt speed.

The thing that worries me about these is that many of them have many releases. This implies they've had features added, bugs fixed, etc. But have they all had the same bugs fixed? I'm inclined to favor ansi-wl-pprint because it has documentation and its last upload was in 2012, and has a bunch of releases which implies the author still works on it.

But I don't know for sure. Anyone have any advice? And I'm sure others agree that 5 almost-but-not-quite copy-paste modules could do with some consolidation...

Involved answered 18/3, 2012 at 19:25 Comment(1)
wl-pprint has more flexible nesting / indenting than HughesPJ. It also has better documentation - there is a full user guide on Daan Leijen's old website. In a nutshell wl-pprint is "better" (in my opinion, of course) than HughesPJ but HughesPJ is standard.Therianthropic
D
24

In no particular order:

  • The "Free" in Text.PrettyPrint.Free means free monad, as per the package description: "A free monad based on the Wadler/Leijen pretty printer"; its Doc type is parametrised on another type, and it has a Monad instance, allowing you to embed "effects" into Doc values. This is used by wl-pprint-terminfo to add formatting functionality; it's not not a competing package, but rather an extension library by the same author. See the list of additions in wl-pprint-extras' documentation compared to wl-pprint's list for more detailed information on what it adds.

  • wl-pprint-terminfo uses the terminfo package to do formatting, so it'll only work on POSIX-y systems, whereas ansi-wl-pprint uses the ansi-terminal package, so it'll work on Windows.

  • wl-pprint-text might be useful if you're working with Text already, but it's unlikely to have a major performance impact unless you're using the pretty printer really heavily; it's not exactly a massively computationally-intensive task.

Unless I had specific requirements, I'd probably just use the pretty package, since it's one of the boot packages, and thus available everywhere. I'd go for ansi-wl-pprint if I wanted formatting, and wl-pprint-text if I was working with Text, but otherwise I don't really see a particularly compelling reason to use a third-party library.

Democracy answered 18/3, 2012 at 19:32 Comment(9)
Thanks for the response, but... comparing wl-pprint and wl-ppprint-extras, the only difference seems to be the type argument in *-extras. I guess that's as advertised. The main thing is that the WL formatters have 'group' and the softline etc. from that, but in the HughesPJ you have to use fcat or fsep. I haven't yet figured out if those are equivalent in power, but group seems more intuitive...Involved
Yeah, it does seem that the effects stuff is the only documented addition, at least. If you have a reason to prefer the Wadler-Leijen interface but don't need any of the additional features the other libraries have, I'd go with the original wl-pprint. You might want to take a look at the reverse dependencies for the packages you're considering if you want additional data to help you make a decision.Democracy
Good advice. ansi-wl-pprint does best here, if you need color. Or if you might need color, switching from wl-pprint to any of the others would be easier than from HughesPJ. And HughesPJ's lack of Monoid in older versions means awkward ifdeffery to support multiple ghc versions... that's a downside to being a bootlib...Involved
I wrote wl-pprint-text, and I found the end result to have a huge speed increase over my previous use of pretty. Also, there are a few issues in wl-pprint wrt indentation and spacing that I've fixed in wl-pprint-text.Ramakrishna
Yeah, see that "fixed a few issues" worries me. Well, it's great that you fixed them of course, but did you submit those fixes back to the other libraries? (yeah yeah, all 4 of them...)Involved
@EvanLaforge at the time, I think the wl-pprint maintainer was AWOL (I just happened to stumble across your response now)... and the other libraries didn't exist yet.Ramakrishna
@Ramakrishna I'm maintaining wl-pprint now, eons after you wrote that comment. Any chance of porting back the fixes? :)Hydrothorax
@Hydrothorax possibly; I don't check SO very much though so you're more likely to find me elsewhere :) (The changes were to do with alignment that I copied fixes from pretty from, but don't recall what off the top of my head.)Ramakrishna
in 2017, prettyprinter seems like the more modernized and better documented version hackage.haskell.org/package/prettyprinter-1.1.1/docs/…Hadron

© 2022 - 2024 — McMap. All rights reserved.