[Haskell-cafe] Template Haskell question

Simon Peyton-Jones simonpj at microsoft.com
Thu Jan 8 03:31:10 EST 2009


Good tricks!  Would one of you like to write them up on the Wiki?  http://haskell.org/haskellwiki/Template_Haskell

Simon

| -----Original Message-----
| From: haskell-cafe-bounces at haskell.org [mailto:haskell-cafe-bounces at haskell.org] On Behalf Of
| Ryan Ingram
| Sent: 08 January 2009 01:55
| To: Jeff Heard
| Cc: haskell
| Subject: Re: [Haskell-cafe] Template Haskell question
|
| 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
| _______________________________________________
| Haskell-Cafe mailing list
| Haskell-Cafe at haskell.org
| http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list