Add pprList method to Outputable for better String rendering
Needs ReviewPublic

Authored by osa1 on Oct 6 2018, 11:17 AM.

Details

Summary

The idea is the same as Show's showList method. With this we render
String as an actual string rather than a list of chars.

osa1 created this revision.Oct 6 2018, 11:17 AM
osa1 retitled this revision from RFC: Add pprList method to Outputable for better String rendering to Add pprList method to Outputable for better String rendering.Oct 6 2018, 11:18 AM

It's not obvious to me that we should render a String like this in Outputable, after all we don't render a FastString with double quotes, and we don't render a Char with single quotes. Outputable is not intended to produce Haskell concrete syntax, it's mainly for pretty-printing the various internal compiler data structures.

Yes, I'm not very bothered that ppr "foo" prints ['f', 'o', 'o']. I believe that getting nicer output in this one case is what motivates the patch (though you do not express a clear motivation).

I'd be inclined not to do this; we can just write text "foo" instead.

But perhaps you have a strong motivation?

osa1 added a comment.Oct 8 2018, 2:58 AM

The use case is when I have a String in a larger type, e.g. Maybe [String]. Now I want to pprDebug this, but by default it gives me bad output because string is rendered as a list of characters. I could try fmap (map text) x but that would not work because we don't have Outputable instance for SDoc (that is D2761). For the same reason text "..." also doesn't work (you may get an unrenderable type, e.g. Just [text "..."] is not renderable).

Even with D2761 these are more work than just doing ppr.

As for how to render String: I think it's fine as long as we don't render as a list of chars, which is completely unreadable and takes too much space.

It just means that every Outputable dictionary is now a boxed pair rather than a simple function. Maybe that doesn't matter.

GHC doesn't have many Strings anyway.

It's not a huge deal of course.

I wonder if anyone else cares? Ask ghc-devs and point them to this patch?

osa1 added a comment.Oct 8 2018, 8:33 AM

It just means that every Outputable dictionary is now a boxed pair rather than a simple function. Maybe that doesn't matter.

Outputable already has two methods today: ppr and pprPrec.

For Maybe [String] I would just pprDebug $ text (show x).

@osa1, can you rebase this? I am wondering whether the haddock.base regression is real.

@osa1, what is the status of this?