简体   繁体   中英

`Show` instance for GHC core

I am trying to work with GHC core data types. I am able to compile my Haskell source to core representation with type Bind CoreBndr . As we know there is no default Show instance for this data type. 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. It would be much easier if we had a Show instance of GHC core. 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?

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. The reason for this is because internally GHC has recursion among many of its data types. For instance, between TyCon , AlgTyConRhs , and DataCon we have:

TyCon has AlgTyCon , which contains AlgTyConRhs .

AlgTyConRhs contains data_cons :: [DataCon] as one of its record fields.

DataCon contains dcRepTyCon :: TyCon as one of its fields.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM