[Template-haskell] quasi quotes and Q monad

Ch. A. Herrmann herrmann at infosun.fmi.uni-passau.de
Mon Jan 2 11:16:36 EST 2006


Dear TH experts,

I have a problem concerning the interaction of quasi quotes and the
quotation monad. Assume a code generating function f (... -> Q Exp) 
which is parameterized by a code generating function g (of type
Exp -> Q Exp, or(?) Q Exp -> Q Exp).

Expressing the problem in the simplest form, the actual instance for g
is (\x -> [| h($x) |]), where h is a toplevel Haskell function working 
on arbitrary types, and function f instantiates x
with an expression which consists just of a single variable (VarE). In
order to splice x in the code ($x), the type of x must be (Q Exp). The 
reason for that, as mentioned in the 2002 paper
by Sheard and Peyton Jones "Template Metaprogramming for Haskell", is
that the computation of x must be able to access the Q monad. The place 
inside f where the actual name for the variable x is generated, has 
already access to the Q monad and the *result* of g is
embedded in this monad, no problem. However, I cannot figure out how
this monad can be passed as an *argument* to g and conceptually, there 
is no justification to pass this monad: it is just an offspring version
of the one where the lexical scope of g belongs to.

The value I want to pass for x is of type Exp. Of course, I could turn
this type into (Q Exp) by applying return, but this artificial instance 
of the Q monad would come from nowhere, not being connected with the
regular instance used, e.g., for the fresh name generation.

Especially, I have the following questions:
* Is there a simple solution to this problem? If so, please tell me and
   forget about the following questions.
* Is the quasi quote mechanism at all appropriate for what I want to do
   or should one better change to the concrete AST representation? That 

   would be unfortunate because my aim is to develop Template Haskell
   examples which demonstrate ease of use.
* If return is used to turn an expression into monadic form before
   splicing, is it possible that
   (a) the consistency of fresh name generation is lost, even if one
       does the name generation for the spliced expression oneself,
   (b) something else goes wrong?

Many thanks in advance and a Happy New Year
--
Christoph Herrmann





More information about the template-haskell mailing list