[Haskell-cafe] Template Haskell question

David Menendez dave at zednenem.com
Wed Jan 7 22:55:23 EST 2009


On Wed, Jan 7, 2009 at 8:54 PM, Ryan Ingram <ryani.spam at gmail.com> wrote:

> 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.

You can use the single quote to get the name of a value.

ghci> runQ [| \x -> $(recUpdE [| x |] [ fmap (\e -> ('runIdentity, e))
[| 1 |] ]) |]
LamE [VarP x_1] (RecUpdE (VarE x_1)
[(Control.Monad.Identity.runIdentity,LitE (IntegerL 1))])

There's more in section 8.9.1 of the GHC manual.
-- 
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>


More information about the Haskell-Cafe mailing list