[Template-haskell] The ':' operation with patterns ?

Sean Seefried seefried@itee.uq.edu.au
Tue, 28 Jan 2003 16:43:29 +1000


Alain Cremieux wrote:

 > Hi,
 >
 > I'm trying to code the exemples of "Template Meta-Programming" for
 > Haskell. Since there are differences in the implementation, I'm
 > obliged to understand what I code, which is a good exercise.
 >
 > At present I'm stuck in the mkZip function, because I'm unable to
 > create a pattern which is a list concatenation of 2 patterns.
 > For expressions I can use 'listExp', but what is the equivalent for
 > patterns ?

I've been trying to get your program to work for a little bit as well
and I'm not having much luck either.

Firstly, if we defined

pcons x xs = [p| $x : $xs |]

as per the Template Haskell paper we get the error message

MkZip.hs:23: Parse error in pattern

I tried to be a little more tricky.   I printed out the reified data
structure for

d = [d| f (x:xs) = x:xs |]

And found that the pattern was

Pcon "GHC.Base::" [Pvar "x'0", Pvar "xs'0"]

So I tried to defined pcons as

pcons x xs = Pcons "GHC.Base::" [x,xs]

but now I get the error message

tcLookup: `GHC.Base.:' is not in scope

I think the problem may be that cons (:) is built in syntax in Haskell
and hence doesn't have a definition anywhere.  Am I right?

It could well be that this problem you're experiencing has no solution
at the moment.  I've tried everything I could think of.

Just to let you know there is a small mistake in your program as it stands

b = listExp [tup eXs, apps (name: eXSs) ]

will produce the code (say for zip3)

"[ (x1,x2,x3), zip3 xs1 xs2 xs3 ]"

which is not quite what you want.  What you want is:

"(x1,x2,x3) : zip3 xs1 xs2 xs3"

The original definition

b = [| $(tup eXs) : $(apps (name : eXSs)) |]

will work fine though.

Sean