New branch implementing typed/untyped Template Haskell
Geoffrey Mainland
mainland at apeiron.net
Thu May 16 17:21:30 CEST 2013
I have pushed a new branch, th-new, that partially implements the
proposal outlined in Simon PJ's "New directions for Template Haskell"
post at:
http://hackage.haskell.org/trac/ghc/blog/Template%20Haskell%20Proposal
The main missing features are top-level pattern splices and local
declaration splices.
Typed expression quotations and typed expression splices *are*
implemented. Syntax is as in Simon's original proposal: [|| ||] for
typed expression brackets and $$(...) for typed expression splices. One
can now write:
power :: Int -> TExp (Int -> Int)
power n = [|| \x -> $$(go n [|| x ||]) ||]
where
go :: Int -> TExp Int -> TExp Int
go 0 x = [|| 1 ||]
go n x = [|| $$x * $$(go (n-1) x) ||]
In fact, one can even write:
power :: Num a => Int -> TExp (a -> a)
power n = [|| \x -> $$(go n [|| x ||]) ||]
where
go :: Num a => Int -> TExp a -> TExp a
go 0 x = [|| 1 ||]
go n x = [|| $$x * $$(go (n-1) x) ||]
Writing the following
f :: TExp Char -> TExp Integer
f x = [|| $$x * 3 ||]
gives the error
Main.hs:28:7:
Couldn't match type ‛Char’ with ‛Integer’
Expected type: TExp Integer
Actual type: TExp Char
In the Template Haskell quotation [|| $$x * 3 ||]
In the expression: [|| $$x * 3 ||]
In an equation for ‛f’: f x = [|| $$x * 3 ||]
The th-new branch of ghc has accompanying branches for template-haskell
and testsuite, both named th-new. These are mirrors of my local branch,
and I reserve the right to rebase them :). That said, please do try it
out!
Geoff
More information about the ghc-devs
mailing list