I read the pozorvlak baby step post on Template Haskell in an attempt to figure it out myself, and I came across this section:
Recall that we tried to programmatically create declarations of the form data Fred = Fred . Letβs try it with quasicotage. Due to restrictions on calling TH code, we will have to put it in our own module, so put the following in Keyword.hs so that the compiler can find it:
module Keyword (keyword) where import Language.Haskell.TH.Syntax keyword name = [d| data $(name) = $(name) |]
Now compile:
Prelude> :l Keyword.hs [1 of 1] Compiling Keyword ( Keyword.hs, interpreted ) Keyword.hs:6:24: parse error on input `$('
This rung rang up with me and seemed like something else that I recently read, Template Haskell package documentation :
For a dynamically related thing ( NameS ), we probably want them to depend on the context, so we no longer need a namespace. For example:
let v = mkName "T" in [| data $v = $v |]
Here we use the same Name for the type constructor and data constructor
Well, thatβs pretty much the same, letβs see if I can make it work:
module Example where import Language.Haskell.TH let v = mkName "T" in [| data $v = $v |]
Give him a whirlwind:
% ghc -XTemplateHaskell -c Example.hs Example.hs:3:25: parse error on input `data'
Hm ... Oh, maybe I need to use ad d for an ad?
let v = mkName "T" in [d| data $v = $v |]
and now:
Example.hs:3:31: parse error on input `$v'
So ... what's going on? Using explicit splice does not change any of the errors. Am I taking Template Haskell documentation out of context, or is this just wrong?