3
votes

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?

1
What kind of noise are you talking about?Reid Barton
The output from ghc -c file.hs -ddump-simpl gives core representation of the program pertty printed with explicit types, dictionary arguments occurance checks etc. but in a program context the core representation is just a value of some data type.ankitrokdeonsns
The primary purpose of core is not to be pretty printed; it is an internal, low level language whose design is largely for the benefit of the compiler, not the human programmer. However, "I want to treat GHC core as any other algebraic data type and write functions with it" - this is already precisely the case. Core is literally just a Haskell ADT - what leads you to believe otherwise? Note that there is a pretty printer for core in the GHC api already.user2407038
How would a Show instance help you to "treat GHC core as any other algebraic data type and write functions with it"?Daniel Wagner
@user2407038 I know GHC core is ADT but while writing transformations for the same it is easier if i get the show representation. Pretty printer doesnt really tell me the the structure of the value in terms of constructors, variables and literals.ankitrokdeonsns

1 Answers

1
votes

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