Question

I have the following Template Haskell code in my module, which is part of a larger application.

$(derive makeFoldable ''JStatement)

I suspect that the generated instance of Foldable is not exactly what I originally had in mind, but I can't find a way to verify this. So, preferrably using only ghci, is it possible to view the generated instance?

I tried the following and got a syntax error and I'm guessing this is because I'm Doing It Wrong™.

> derive makeFoldable ''JStatement

<interactive>:1:21:
    lexical error in string/character literal at character '\''
Was it helpful?

Solution

I figured it out. If you pass -ddump-splices it will print the generated instances to the terminal when compiling the module.

OTHER TIPS

GHCi reports "lexical error..." because you don't have Template Haskell activated in your GHCi session. You can activate it either by passing -XTemplateHaskell on the command line or from within GHCi itself:

ghci> :set -XTemplateHaskell

After fixing that, you should get an error in the likes of:

No instance for (Show DecsQ) arising from a use of ‘print’
In a stmt of an interactive GHCi command: print it

Now, you have several options to print things that are inside the Q monad:

  • Use -ddump-splices (as already pointed out in Deniz Dogan's answer)

  • Pretty print the generated Haskell code from GHCi itself:

    > putStrLn $(stringE . pprint =<< derive makeFoldable ''JStatement)
    instance Foldable (JStatement ...)
      where foldr ... = ...
    
  • Show the actual structure based on constructors:

    > putStrLn $(stringE . show =<< derive makeFoldable ''JStatement)
    [InstanceD [] (AppT (ConT Foldable) (... JStatement ...)) [...]]
    

The last two might be simplified using runQ, but this does not work for code generation that uses some Template Haskell functions, such as reify operations. This includes some (or maybe most?) derivations of the derive package.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top