The ':' operation with patterns ?
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 ? the code : -- call : $(zipN 3) as bs cs zipN :: Int -> Expr zipN n | (n <= 0) = fail "Incorrect arg to 'zipN' - zipN n, n >= 1" | otherwise = [| let zp = $(mkZip n [| zp |]) in zp |] mkZip :: Int -> Expr -> Expr mkZip n name = lam pYs (caseE (tup eYs) [m1, m2]) where pXs, pYs, pXSs :: [Patt] eXs, eYs, eXSs :: [Expr] (pXs, eXs) = genPE "x" n -- x1, x2,... (pYs, eYs) = genPE "y" n -- y1, y2,... (pXSs, eXSs) = genPE "xs" n -- xs1, xs2,... --pcons x xs = [p| $x : $xs |] pcons :: Patt -> Patt -> Patt pcons x xs = ptup (x : [xs]) -- NOT correct, gives (x, xs) instead of x : xs b :: Expr -- ((x1, x2,...) : (zp xs1 xs2 ...)) b = listExp [tup eXs, apps (name : eXSs)] m1 :: Mtch -- ((x1:xs1), (x2:xs2),...) -> ((x1, x2,...) : (zp xs1 xs2 ...)) m1 = alt (ptup (zipWith pcons pXs pXSs)) b m2 :: Mtch -- (_, _,...) -> [] m2 = alt (ptup (replicate n pwild)) (listExp []) -- alt = (a1, a2, ..., an) -> ai -- the match part of a case clause (just an example) alt :: Patt -> Expr -> Mtch alt p e = do x <- e return (Mat p (Normal x) []) Thanks, Alain
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
participants (2)
-
Alain Cremieux -
Sean Seefried