[Template-haskell] newbie question

Simon Peyton-Jones simonpj@microsoft.com
Wed, 9 Jul 2003 09:11:46 +0100


[Isaac, you need to join the TH list so you can post to it without me
having to intervene.]

| > genSummers n =3D returnQ [Fun ("makeSum" ++ (show i)) [Clause []
(Normal $(makeSum i)) [] ] | i
| <- [1..n] ]
|=20
| But actually, I run into a stage restriction.

You don't want a splice there!  But if you remove it, (makeSum i)
returns an ExpQ, whereas Normal wants an Exp.

| > makeSumE :: Int -> Exp

If you want to generate fresh (rather than fixed) variable names in
makeSum, it definitely needs to return an ExpQ, not an Exp.  An Exp is
simply a data structure.  An ExpQ (=3D Q Exp) is a monadic computation
which can generate fresh names.  Usually better.  Your original makeSum
looked better.

| > genSummers n =3D returnQ [Fun ("makeSum" ++ (show i)) [Clause []
(Normal (makeSumE i)) [] ] | i
| <- [1..n] ]

if makeSum returns an ExpQ, you can't do this for the reason above.

Solution: use the "smart constructors" from the paper.  More like this:

    genSummers n =3D fun ("makeSum" ++ (show i)) [clause [] (normal
(makeSum i)) [] ]

Simon