RE: [Template-haskell] The ':' operation with patterns ?
I've been chasing this TH bug down. There are two issues. One is the question of pattern splices, which I'll put in a separate email. However, Sean's suggestion: pcons x xs = pcon "GHC.Base::" [x,xs] should have worked fine, and didn't. (The form Foo:baz is an "original name" for the thing.) The failure turned out to be a consequence of the fact that the Exp data type in THSyntax says; data Exp = Var String | Con String | ... | Infix (Maybe Exp) String (Maybe Exp) | ... The String was assumed to be the name of a variable, not a data constructor. So GHC looked it up in a different name space than the namespace of constructors. Notice, in contrast, that Exp distinguishes Var and Con occurrences all by themselves. This inconsistency led to the bug. For now, I have changed Exp to say Infix (Maybe Exp) Exp (Maybe Exp) where the operator is expected to be either "Var s" or "Con s". Not ideal, but it'll make it all work again, I think. OK, so now it works. A bit of a wild goose chase. I attach working Zip and Main files. Simon | -----Original Message----- | From: Alain Cremieux [mailto:alcremi@pobox.com] | Sent: 26 January 2003 22:07 | To: template-haskell@haskell.org | Subject: [Template-haskell] 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 | | _______________________________________________ | template-haskell mailing list | template-haskell@haskell.org | http://www.haskell.org/mailman/listinfo/template-haskell
participants (1)
-
Simon Peyton-Jones