An example is worth a thousand words. Here is a pretty simple quasi quoter I just made up.
import Language.Haskell.TH.Quote
import Language.Haskell.TH.Syntax
quoter :: QuasiQuoter
quoter = QuasiQuoter { quotePat = parse }
where
parse :: String -> Q Pat
parse ('$':x) = pure (VarP (mkName x))
parse "_" = pure WildP
parse _ = fail "Invalid pattern"
Then, using it in GHCi
ghci> :set -XQuasiQuotes
ghci> [quoter|_|] = 2
ghci> [quoter|$x|] = 2
ghci> x
error: Variable not in scope: x
I would have liked 2
to be bound to x
. So: is there any way to introduce variable patterns in custom quasi-quotes that we can use again? Note that my actual use case is a fair bit more involved than the above - parse
actual does some substantial work.
EDIT
The following works:
ghci> inc [quoter|$x|] = x + 1
ghci> inc 2
3
and the following doesn't
ghci> let [quoter|$x|] = 1 in x
error: Variable not in scope: x
so is this a bug in GHCi?