[英]`Show` instance for GHC core
I am trying to work with GHC core data types. 我正在尝试使用GHC核心数据类型。 I am able to compile my Haskell source to core representation with type
Bind CoreBndr
. 我能够将我的Haskell源代码编译为类型为
Bind CoreBndr
核心表示。 As we know there is no default Show
instance for this data type. 我们知道此数据类型没有默认的
Show
实例。 There is a way to pretty print this representation but it has way too much noise associated with it. 有一种方法可以很好地打印这种表示,但它有太多与之相关的噪音。 I want to treat GHC core as any other algebraic data type and write functions with it.
我想将GHC核心视为任何其他代数数据类型并用它来编写函数。 It would be much easier if we had a
Show
instance of GHC core. 如果我们有一个GHC核心的
Show
实例会容易得多。 Has anybody already written a show instance which I can reuse? 有没有人写过我可以重用的节目实例?
Aside, how does the community write and verify programs that deal with GHC core? 此外,社区如何编写和验证处理GHC核心的程序?
Having gone dumpster diving in GHC and pondered the same question, I can confidently say that a naive implementation of Show
in GHC is NOT what you want. 在经历垃圾箱在GHC和思考同样的问题,我可以自信地说,一个天真的实现
Show
在GHC是不是你想要的。 The reason for this is because internally GHC has recursion among many of its data types. 这是因为内部GHC在其许多数据类型中都有递归。 For instance, between TyCon , AlgTyConRhs , and DataCon we have:
例如,在TyCon , AlgTyConRhs和DataCon之间我们有:
TyCon
has AlgTyCon
, which contains AlgTyConRhs
. TyCon
有AlgTyCon
,其中包含AlgTyConRhs
。
AlgTyConRhs
contains data_cons :: [DataCon]
as one of its record fields. AlgTyConRhs
包含data_cons :: [DataCon]
作为其记录字段之一。
DataCon
contains dcRepTyCon :: TyCon
as one of its fields. DataCon
包含dcRepTyCon :: TyCon
作为其字段之一。
And thus we come full circle. 因此我们走了一圈。 Because of how
Show
works, recursion like this will create infinite output if you ever attempt to print it. 由于
Show
工作方式,如果您尝试打印它,这样的递归将创建无限输出。
In order to get a "nice" custom representation with data constructors and everything showing, you would have to write it yourself. 为了获得具有数据构造函数和所有显示的“漂亮”自定义表示,您必须自己编写它。 This is actually somewhat challenging, since you have to consider and debug cases of recursion like this that default pretty printers have solved.
这实际上有点挑战性,因为你必须考虑和调试这样的递归情况,默认漂亮的打印机已经解决了。
It's tedious, many tabs were opened, but it's a good learning experience :) 这很乏味,很多标签都打开了,但这是一个很好的学习经历:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.