开发者

Haskell -- easier to read show function? (for debugging)

I'm looking for a function like show that produces more readable output. It certainly doesn't have to work on all classes. I searched "haskell pretty print" on Google, but that seems to produce compiler source code printers. Debugging stuff like the following (newlines inserted manually for stackoverflow formatting) is difficult!

(fromL开发者_如何学运维ist [(Ref {name = "phi", lenRef = 4},fromList [CompNode {val = 1, ident = CNId {uid =     4,
 zone = 0}, deps = []},CompNode {val = 2, ident = CNId {uid = 5, zone = 0}, deps = []},CompNode 
{val = 3, ident = CNId {uid = 6, zone = 0}, deps = []},CompNode {val = 4, ident = CNId {uid = 7, 
zone = 0}, deps = []}] :: Data.Vector.Vector),(Ref {name = "phi'", lenRef = 2},fromList [CompNode 
{val = -1, ident = CNId {uid = 0, zone = 0}, deps = []},CompNode {val = -1, ident = CNId {uid = 1, 
zone = 0}, deps = []}] :: Data.Vector.Vector),(Ref {name = "psi", lenRef = 2},fromList [CompNode 
{val = -1, ident = CNId {uid = 8, zone = 0}, deps = [CompNode {val = 1, ident = CNId {uid = 4, zone
 = 0}, deps = []},CompNode {val = 2, ident = CNId {uid = 5, zone = 0}, deps = []}]},CompNode {val = 
-1, ident = CNId {uid = 3, zone = 0}, deps = []}] :: Data.Vector.Vector)]

edit

okay, I forgot "print" is more accurately called "show" in haskell... there is a "pretty-show" package. however, it seems to just call show, parse the string, and try to output it in a nice way. i really want something that exposes a new class structure, e.g. class PrettyShow a where prettyShow :: a -> String.

edit 2

pretty-show isn't good enough for my situation; its output is hardly different. I'm writing something with a monad that tracks indentation; if the code evolves into something good enough maybe I'll post it on hackage. Further, I want to write PrettyShow instances for my custom classes just like one can currently write show instances.


The show function isn't really intended to produce nicely readable output. If you look at the default implementations from the deriving clause and at how the language spec talks about it, it's clear that show and read are intended to serve as a form of simple serialization. Additionally, the serialized format is expected to be parsable as Haskell source such that (assuming relevant definitions are in scope) interpreting the output of show as an expression gives a value equivalent to deserializing it with read.

If you want nice, formatted output that doesn't look like Haskell source, the standard term for that is still "pretty printing", and there's no standard generic way to do it. There are pretty-printing libraries out there that provide primitives to build your own pretty-printers, however, if you browse around Hackage a bit. Note that even if they talk about pretty-printing syntax trees in a compiler that's just an example; any tree-like structure ought to work just as well.

As a quick alternative, you might wish that the output of show was at least better-looking quasi-source-code. This is reasonable and possible, since read is smart enough to ignore whitespace and such. The groom package provides this functionality, in a "doing the stupidest thing that could possibly work" manner: it parses the output of show as Haskell source, then pretty-prints that. In practice, this works pretty well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜