[Haskell-cafe] Pattern variables in Template Haskell

Richard Eisenberg eir at cis.upenn.edu
Mon Nov 3 14:53:21 UTC 2014


I just tried

> foo $( [p| x |] ) = x

and got that x is unbound on the RHS. This may be a GHC bug if no one here knows how this is supposed to work. I sure don't, but I've never explored pattern splices.

Richard

On Nov 3, 2014, at 3:07 AM, Alejandro Serrano Mena <trupill at gmail.com> wrote:

> Dear Haskell-cafe,
> I'm trying to use Template Haskell to generate patterns in Haskell code, but after a couple of days of trying, I have not come with a solution. I hope somebody could help me.
> 
> My problem is basically that I want something like:
> 
> example :: Maybe a -> a
> example $(j "x") = x
> 
> to work correctly. The idea of this simple quoter is to generate code similar to:
> 
> example :: Maybe a -> a
> example (Just x_123) = x_123
> 
> that is, to create the "Just" pattern, and to make "x" bind to the same element. Right now my code reads:
> 
> j :: String -> Q Pat
> j s = return (ConP 'Just [VarP (mkName s)])
> 
> which generated a new variable "x" each time, thus unable to match with the "x" in the RHS. I have also tried with lookupValueName.
> 
> Note that my problem is a bit more complicated, with variable names coming from a different source. However, I think that if I solve this problem where I can make variables in a pattern and in an expression match, I should be able to use them in my complete solution.
> 
> Thanks in advance.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list