Generating valid Haskell code using the GHC API pretty printer

Claus Reinke claus.reinke at
Thu Jul 23 08:06:43 EDT 2009

Hallo Kiel!-)

>we are trying to use the GHC API for a source-to-source transformation
>on Haskell programs.  The result of parsing and typechecking a module
>enables us to apply the transformation, but writing the transformed
>module back using the pretty printer (Outputable) generates invalid
>Haskell code.

It would help to know more about your intended application. For instance,
if there is a need for user-visible output, the requirements are very different
from those of a simple preprocessor. 

In the former case, pretty-printing isn't going to get you far, even if you do 
write your own pretty printer - the AST doesn't have enough information to 
reconstruct the input program (one would need to combine the lexer's token 
stream with the modified AST to print out modified code - similarly to what 
was done in HaRe). In the latter case, you might not need to pretty-print, 
just modify the input before passing it on to later phases of GHC. Since 
you don't seem to care about formatting or comments, I suspect it is the 
latter - have you looked into Template Haskell as an alternative?

As Thomas pointed out, there is some control over what gets qualified
and what doesn't; the two indentation items you list look like plain bugs
in search of a ticket and patch. Please record them on GHC's trac.


More information about the Glasgow-haskell-users mailing list