Variables in a custom Haskell template quasi-quateretele

An example is worth a thousand words. Here's a fairly simple quasi-cycler 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 like 2

to get attached to x

. So: is there a way to introduce variable templates to custom quasiquotes that we can use again? Note that my actual use case is a fairer bit 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:

 ghci> let [quoter|$x|] = 1 in x
 error: Variable not in scope: x

      

So is this a bug in GHCi?

+3


source to share


1 answer


This has now been fixed in GHC 8.2 (see this commit ).



0


source







All Articles