19

I'm following the Yesod book, which states:

But by using the -ddump-splices GHC option, we can get an immediate look at the generated code. A much cleaned up version of it is:

How would I do this? I've tried compiling my file with ghc -XTemplateHaskell -ddump-splices Page.hs, which leaves the directory as follows:

Page Page.hi Page.hs Page.hs~ Page.o

None of these files, however, contain the intermediate code generated by Template Haskell.

http://www.yesodweb.com/book/basics

Matthew H
  • 5,831
  • 8
  • 47
  • 82
  • 1
    Total newbie here - how is this command meant to be used? If I run ghc as you have written (with helloworld.hs instead), I get "Could not find module ‘Yesod’" I can only compile/run using `stack runghc helloworld.hs` – User Oct 21 '17 at 13:36

2 Answers2

17

Meanwhile the behaviour changed and the -ddump-to-file flag in addition to the -ddump-splices flag causes the splices to be written to a file, see Section 9.26 of the current (GHC 8.2.1) documentation for more details.


On older versions of GHC (I didn't check in which version exactly the behaviour changed), -ddump-splices worked differently:

The -ddump-splices option causes GHC to dump the splices to stderr. Unfortunately, the -ddump-to-file flag doesn't affect splices (I don't know whether that has deeper reasons or is just an oversight), so you need to capture the stderr output to save the splices for later investigation,

ghc -XTemplateHaskell -ddump-splices Page.hs 2> Page.dump-splices

on sufficiently bash-like shells.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • 1
    Thanks. Why does it output to stderr rather than, say, stdout? – Matthew H Apr 06 '13 at 13:13
  • 3
    I don't know. There may be a reason, or it could be historical accident. One would probably need to ask HQ for an answer. – Daniel Fischer Apr 06 '13 at 13:19
  • 1
    According to https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/template-haskell.html, this has been changed in the mean-time. It claims to dump to stdout by default, and `-ddump-to-file` should dump to a `.dump-splices` file. – robx May 21 '15 at 18:56
4

In large projects doing this for all modules is problematic. Fortunately, you can dump per module. This also works in ghci.

You can add this line to the top of your module

{-# OPTIONS_GHC -ddump-splices #-}

It dumps it to stderr.


{-# OPTIONS_GHC -ddump-to-file #-}

is also usefull which puts the dump in a file next to the module with the prefix .dump-simpl.

See reference: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/debugging.html#ghc-flag--ddump-to-file

https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/debugging.html#ghc-flag--ddump-splices

You can add these as separate lines (for easy copy pasting)

Jappie Kerk
  • 1,257
  • 12
  • 16