Haskell-显示功能更易于阅读? (用于调试) [英] Haskell -- easier to read show function? (for debugging)

查看:67
本文介绍了Haskell-显示功能更易于阅读? (用于调试)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找像show这样的函数,该函数可以产生更具可读性的输出.当然,它不必在所有课程上都适用.我在Google上搜索了漂亮的haskell漂亮字体",但这似乎产生了编译器源代码打印机.调试以下内容(手动插入换行符以进行stackoverflow格式化)非常困难!

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)]

编辑

好吧,我忘记了haskell中的打印"更准确地称为显示" ...有一个漂亮显示"程序包.但是,它似乎只是调用show,解析该字符串,然后尝试以一种不错的方式输出它.我真的想要一些可以揭示新类结构的东西,例如class PrettyShow a where prettyShow :: a -> String.

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.

pretty-show对于我的情况还不够好;它的输出几乎没有不同.我正在写一个单调跟踪缩进的东西.如果代码演变成足够好的东西,也许我会把它发布在黑客上.此外,我想为自定义类编写PrettyShow实例,就像当前可以编写show实例一样.

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.

推荐答案

show函数并不是真正旨在产生可读性强的输出.如果您查看deriving子句的默认实现以及语言规范如何讨论,很明显showread旨在用作简单的序列化形式.此外,预计序列化的格式可以解析为Haskell源,这样(假设相关定义在范围内)将show的输出解释为表达式,该值等同于使用.

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.

如果您想要看起来不错而不像Haskell源的格式化输出,则其标准术语仍然是漂亮的打印",并且没有标准的通用方法可以做到这一点.那里有漂亮的打印库,它们提供了用于构建自己的漂亮打印机的原语,但是,如果您稍微浏览一下Hackage的话.请注意,即使他们谈论在编译器中漂亮打印语法树,这只是一个例子;任何树状结构也应同样起作用.

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.

作为快速替代方案,您可能希望show的输出至少看起来更好.这是合理且可行的,因为read足够聪明,可以忽略空白等. groom软件包提供了此功能,以做可能最有效的最愚蠢的事情".方式:它将show的输出解析为Haskell源,然后漂亮地打印出来.实际上,这很好.

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.

这篇关于Haskell-显示功能更易于阅读? (用于调试)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆