Haskell——显示函数更容易阅读? (用于调试)
我正在寻找像 show 这样的函数,它可以产生更具可读性的输出。它当然不必适用于所有课程。我在谷歌上搜索了“haskell Pretty print”,但这似乎会产生编译器源代码打印机。调试如下内容(手动插入换行符以进行 stackoverflow 格式化)非常困难!
(fromList [(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)]
编辑
好吧,我忘记了“print”在haskell中更准确地称为“show”...有一个“pretty-show”包。然而,它似乎只是调用 show
,解析字符串,并尝试以良好的方式输出它。我真的想要一些公开新类结构的东西,例如 class PrettyShow a where PrettyShow :: a ->字符串
。
edit 2
pretty-show
对于我的情况来说不够好;它的输出几乎没有什么不同。我正在写一些带有跟踪缩进的 monad 的东西;如果代码演变成足够好的东西,也许我会将其发布在 hackage 上。此外,我想为我的自定义类编写 PrettyShow
实例,就像当前可以编写 show
实例一样。
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!
(fromList [(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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
show
函数并不是真正旨在产生可读性良好的输出。如果您查看deriving
子句和 语言规范如何谈论它,很明显show
和read
旨在充当简单序列化的一种形式。此外,序列化格式预计可以解析为 Haskell 源,这样(假设相关定义在范围内)将show
的输出解释为表达式,给出的值相当于使用read
对其进行反序列化。如果你想要漂亮的、格式化的输出,看起来不像 Haskell 源代码,那么标准术语仍然是“漂亮的打印”,并且没有标准的通用方法可以做到这一点。有一些漂亮的打印库提供了构建您自己的漂亮打印机的原语,但是,如果您稍微浏览一下 Hackage。请注意,即使他们谈论编译器中的漂亮打印语法树,这也只是一个示例;任何树状结构都应该同样有效。
作为一种快速替代方案,您可能希望
show
的输出至少是看起来更好的准源代码。这是合理且可能的,因为read
足够聪明,可以忽略空格等。groom
包 提供了此功能,以“做最愚蠢的事情”这可能会工作”的方式:它将show
的输出解析为 Haskell 源,然后漂亮地打印它。实际上,这非常有效。The
show
function isn't really intended to produce nicely readable output. If you look at the default implementations from thederiving
clause and at how the language spec talks about it, it's clear thatshow
andread
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 ofshow
as an expression gives a value equivalent to deserializing it withread
.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, sinceread
is smart enough to ignore whitespace and such. Thegroom
package provides this functionality, in a "doing the stupidest thing that could possibly work" manner: it parses the output ofshow
as Haskell source, then pretty-prints that. In practice, this works pretty well.