tags:

views:

74

answers:

1

The haskell-src-exts package has functions for pretty printing a Haskell AST. What I want to do is change its behavior on certain constructors, in my case the way SCC pragmas are printed. So everything else should be printed the default way, only SCCs are handled differently. Is it possible to do it without copying the source file and editing it, which is what I'm doing now?

+1  A: 

Well, the library has done one thing right, using a type class for Pretty. The challenge then is how to select a different instance for the constructors you want to print differently. Ideally, you would just newtype the AST node you care about, and somehow substitute that into the AST.

Now, the problem here is that the Haskell AST exported by the library has its type structure fixed. It doesn't, e.g. use two-level types, which would let you substitute newtypes for parts of the tree. So you would have to redefine the type of the AST down to the node you wish to change the type of.

Don Stewart
How could this technique be used to replace a node in the AST? Lets say I want to replace (in the definitions from the cited paper) `Arith` in `Term` with some custom type `MyArith`. Isn't `Term` fixed with its definition to use `Arith`?
Daniel Velkov