[Haskell-cafe] Scoping in template haskell

Lars Oppermann loppermann at acm.org
Wed Mar 19 08:17:55 EDT 2008


Hi all,

I am trying to get familiar with template haskell and I'm wondering
whether the following is possible and if so, how it would be done
properly...

 I have two templates, and I'd like to use one from the other. For
instance the first one might be

gen1 :: Int -> ExpQ
gen1 i = [| "<<" ++ (show i) ++ ">>" |]

Thus, I gould do

> $(gen1 42)

and I'd get "<<42>>"

now I'd like to use gen2 in another generated expression so that I could so

> $(gen2 [42, 66])

and get "<<42>><<66>>"

My naive intention was to write

gen2 :: [Int] -> ExpQ
gen2 is = [| map (\x -> $(gen1 x)) is |]

which gives me "Stage error: `x' is bound at stage 2 but used at stage 1

So I guess my actual question would be: how do I pass a variable bound
outside  a quotation into another splice used in that quotation?

Bests,
Lars


More information about the Haskell-Cafe mailing list