[Haskell-cafe] Template Haskell question
Ryan Ingram
ryani.spam at gmail.com
Wed Jan 7 20:54:46 EST 2009
On Wed, Jan 7, 2009 at 12:58 PM, Jeff Heard <jefferson.r.heard at gmail.com> wrote:
> And how do I encode
>
> a{ mousePositionf = b }
>
> in template haskell without using the [| |] syntax, so that I can use mkName?
Whenever I have a question like that, I just ask ghci:
$ ghci -fth
ghci> :m Control.Monad.Identity Language.Haskell.TH
ghci> runQ [| 1 + 1 |]
InfixE (Just (LitE (IntegerL 1))) (VarE GHC.Num.+) (Just (LitE (IntegerL 1)))
ghci> runQ [| \x -> x { runIdentity = 1 } |]
LamE [VarP x_1] (RecUpdE (VarE x_1) [(Control.Monad.Identity.runIdentity,LitE (I
ntegerL 1))])
Note that GHCi shows TH names without "mkName" or quotes, so you need
to add those. But it shows you the structure you want to generate.
You can also use $() and [| |] inside [| |] to generate additional
data in TH directly:
ghci> runQ $ do { VarE n <- [| runIdentity |] ; [| \x -> $(recUpdE [|
x |] [ fmap (\e -> (n,e)) [| 1 |] ]) |] }
LamE [VarP x_2] (RecUpdE (VarE x_2) [(Control.Monad.Identity.runIdentity,LitE (I
ntegerL 1))])
Note the "VarE n <- [| identifier |]" trick to extract the name from
an identifier.
-- ryan
More information about the Haskell-Cafe
mailing list