Macros in Haskell
playingwithpointers.com
playingwithpointers.com
I mean, it might even be clearer-looking to output strings. Not saying that outputting string programs is good, but it is a good lower limit for how complex the code for your macro system can be in order for it to be useful.
$ 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.
http://coder.bsimmons.name/blog/2010/12/working-with-templat...
Is there any particularly well-done TH being used somewhere in the real world? (You don't have to show me the code, just what problem you had that TH solved.)
https://github.com/msgpack/msgpack/blob/master/haskell/test/...
I'm a Haskell newbie, and have not done much with TH yet. But I can imagine a bunch of TH code generating a lexer or a parser out of some meta-description. Like Happy, but with everything directly embedded in the source itself.
I'm only reminded of this quotation:
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
– Brian W. Kernighan
For example, two major uses of macros in Lisp are with-macros and embedded DSLs. You don't need macros for either of those in Haskell, because first-class functions have you covered.
There is no direct way in base Haskell to declare all of 1- to 10-tuples with a single type declaration in a map or loop, for example. Template Haskell can do this, though.