3 ms·
A chunk of Haskell syntax is directly available: $ ghci -XTemplateHaskell > :m + Language.Haskell.TH > d <- runQ . return $ FunD (mkName "f") [Clause [Va
by thu 15y ago
A chunk of Haskell syntax is directly available:
$ ghci -XTemplateHaskell
> :m + Language.Haskell.TH
> d <- runQ . return $ FunD (mkName "f") [Clause [VarP $ mkName "x"] (NormalB (LitE (IntegerL 5))) []]
> [d] <- runQ [d| f x = 5 |]
The two last lines define the same `d` AST.
I have written some TH lately, and while being boring, you can write the first line as easily as the second one.
A nice thing to do when writing your TH code is to use runIO and pprint to display the generated code as it is spliced.
- thedigitalengel 15y agoThis (the second) way of doing things looks much more sensible -- thanks!