Quoting functions in Template Haskell
September 7, 2015While working on
this patch,
I learned a cool feature of Template Haskell called quoting function.
Instead of using VarE (mkName "show")
to create an Exp
type, you
can safely create the same using this: VarE 'Prelude.show
. When
generating declartions using splicing, the import of the module will
be taken care of by itself. A self contained example:
{-#LANGUAGE TemplateHaskell#-}
-- External.hs
module External where
import Data.Time
import Language.Haskell.TH
example :: Name
= 'getCurrentTime
example
sampleDec :: Q [Dec]
= return $
sampleDec ValD (VarP (mkName "hello")) (NormalB (VarE example)) []] [
And the other module:
{-#LANGUAGE TemplateHaskell#-}
-- Other.hs
import External
$(sampleDec)
= do
main <- hello
x print x
Now you can see this in ghci
:
> main
λ2015-09-01 22:01:34.532374 UTC
See how I didn’t have to import Data.Time
in the Other.hs
file.
The single quote can be also used for data constructors. Similarly, a
type can be converted to Name
using double quote.