[Template-haskell] Defining a function and using it in a splice within the same module

Simon Peyton-Jones simonpj at microsoft.com
Wed Oct 24 08:01:34 EDT 2007

| "You can only run a function at compile time if it is imported from
| another module. That is, you can't define a function in a module, and
| call it from within a splice in the same module."
| Unfortunately I have encountered this problem more times than I'd like
| while using TH to implement a DSL. My usual solution is to generate a
| function to process the extra arguments which cannot be passed to the
| splice (i.e. push the arguments out of the splice so that I can use
| functions defined in the same module).

I don't quite understand your workaround.  Can you describe it a bit more?

| Nevertheless, I'd like to know if there are any chances of seeing this
| problem solved anytime soon.

Here's why it's hard.  It involves compiling the functions *before* the splice to bytecode, so that they can be run.  But we don't always want to do that!  Usually we do not run any of these functions, and we want to compile them to object code.  I don't think it's acceptable to *always* compile *everything* to bytecode, just in case.

So the trickiness is simply the plumbing required to notice that f is called from inside a splice, so we'd better compile it to bytecode early.   Oh, and the transitive closure of things called by f.

A possible approximation is: see if *anything* defined in this module is called from within a splice, and if so compile *everything* to bytecode, just in case.

I doubt I'll get to this soon.  Is anyone else interested in working on it?

I've created a ticket for it though: http://hackage.haskell.org/trac/ghc/ticket/1800

If there is more to add (e.g. details of your workaround), add commments to that ticket so we accumulate the lore in one place.


More information about the template-haskell mailing list