Discussion: adding displayException to Exception class

Christopher Done chrisdone at gmail.com
Wed Oct 29 15:20:24 UTC 2014


I’m a bit late but I thought I’d chime in as the Show situation is
delicate for me. Pretty much as you said, I’d categorize printing (of
general Haskell values) by the audience:

Users
Programmers
Programs

Here’s an example of each:

"The tar archive is corrupt (truncated)." :: String
TruncatedArchive :: FormatError
"\1" :: ByteString

I would like to add that for the kind targeting programmers, it’s not
that I would necessarily like to be able to copy/paste the printing of
something into my source code. I use Show for debugging. Rather, I
want to know what the value is. So I need the constructor and any
fields in it. I think it’s essential for debugging, and I feel spoiled
in Haskell that most types have a reasonable Show instance that shows
the structure of a value for me. But often, cases like this, it’s
just a human-readable string which, as a programmer, is very unhelpful
to me.

So we already have standard ways of doing each of these, of course:

We have things like Doc for pretty printing for users.
We have Show for printing the structure of a value.
We have binary/cereal/aeson for serializing.

The issue is that pretty printing is not very helpful for me. For
example, working with the GHC API is particularly difficult because
almost all types do not have a Show instance. This breaks my normal
Haskell development workflow, which is that I’ll run a function and
print out the values.

For example, say I’m working with Core and I have an Expr type. I’m
pattern matching on the AST constructor by constructor. I generate
some core, and I want to see what constructor is used where. If I use
the Outputable class to print the Core, all I get is a pretty printed
concrete syntax. Pretty, but not useful to me. How can I get that
information? So I end up defining a gshow :: Data => a -> Show
function, just to see exactly the structure of the data I’m working
with, which is what show normally gives me.

I think the case of “being able to copy/paste it as source code” is a
little bit out of place given things like fromList [(1,'a')] for a
Map Int Char and Handle {<1>} (or whatever it was for the Handle
type). The Handle’s Show instance is, to my mind, a perfectly
legitimate instance for telling you what this value is. It contains an
fd and it prints it. It has no Read instance, there’s no pretense
of being “serializable” or “copy/pastable”.

I have the same issues with exceptions, as noted by Michael. I’d like
to know as a programmer what the exception is, as a Haskell
value. Separately a way of printing exceptions for users would be
welcome too and in the case of web/UI apps it seems very useful.


More information about the Libraries mailing list