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

Simon Peyton-Jones simonpj@microsoft.com
Thu, 6 Feb 2003 17:17:12 -0000


This is a multi-part message in MIME format.

------_=_NextPart_001_01C2CE03.9657DA27
Content-Type: text/plain;
	charset="US-ASCII"
Content-Transfer-Encoding: quoted-printable

I've been chasing this TH bug down.=20

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 =3D pcon "GHC.Base::" [x,xs]

should have worked fine, and didn't.  (The form Foo:baz is an "original
name" for the thing.)=20

The failure turned out to be a consequence of the fact that the Exp data
type in THSyntax says;

	data Exp =3D 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 ?
|=20
| Hi,
|=20
| 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.
|=20
| 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 ?
|=20
| the code :
|=20
| -- call : $(zipN 3) as bs cs
|=20
| zipN :: Int -> Expr
| zipN n
|     | (n <=3D 0)
|         =3D fail "Incorrect arg to 'zipN' - zipN n, n >=3D 1"
|     | otherwise
|         =3D [| let zp =3D $(mkZip n [| zp |])
|              in  zp |]
|=20
| mkZip :: Int -> Expr -> Expr
| mkZip n name =3D lam pYs (caseE (tup eYs) [m1, m2])
|     where
|           pXs, pYs, pXSs :: [Patt]
|           eXs, eYs, eXSs :: [Expr]
|           (pXs,  eXs)  =3D genPE "x"  n  -- x1,  x2,...
|           (pYs,  eYs)  =3D genPE "y"  n  -- y1,  y2,...
|           (pXSs, eXSs) =3D genPE "xs" n  -- xs1, xs2,...
|           --pcons x xs =3D [p| $x : $xs |]
|           pcons :: Patt -> Patt -> Patt
|           pcons x xs =3D ptup (x : [xs]) -- NOT correct, gives (x, xs)
| instead of x : xs
|           b :: Expr -- ((x1, x2,...) : (zp xs1 xs2 ...))
|           b =3D listExp [tup eXs, apps (name : eXSs)]
|           m1 :: Mtch -- ((x1:xs1), (x2:xs2),...) -> ((x1, x2,...) :
(zp
| xs1 xs2 ...))
|           m1 =3D alt (ptup (zipWith pcons pXs pXSs)) b
|           m2 :: Mtch -- (_, _,...) -> []
|           m2 =3D alt (ptup (replicate n pwild)) (listExp [])
|=20
| -- alt =3D (a1, a2, ..., an) -> ai  -- the match part of a case clause
| (just an example)
| alt :: Patt -> Expr ->  Mtch
| alt p e =3D
|     do x <- e
|        return (Mat p (Normal x) [])
|=20
| Thanks,
| Alain
|=20
| _______________________________________________
| template-haskell mailing list
| template-haskell@haskell.org
| http://www.haskell.org/mailman/listinfo/template-haskell

------_=_NextPart_001_01C2CE03.9657DA27
Content-Type: application/octet-stream;
	name="Zip.hs"
Content-Transfer-Encoding: base64
Content-Description: Zip.hs
Content-Disposition: attachment;
	filename="Zip.hs"

ey0jIE9QVElPTlMgLWZnbGFzZ293LWV4dHMgIy19Cm1vZHVsZSBaaXAgd2hlcmUKCmltcG9ydCBM
YW5ndWFnZS5IYXNrZWxsLlRIU3ludGF4CgoKemlwTiA6OiBJbnQgLT4gRXhwcgp6aXBOIG4KICAg
IHwgKG4gPD0gMCkKICAgICAgICA9IGZhaWwgIkluY29ycmVjdCBhcmcgdG8gJ3ppcE4nIC0gemlw
TiBuLCBuID49IDEiCiAgICB8IG90aGVyd2lz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oIHBh
cnQgb2YgYSBjYXNlIGNsYXVzZSAKLS0gKGp1c3QgYW4gZXhhbXBsZSkKYWx0IDo6IFBhdHQgLT4g
RXhwciAtPiAgTXRjaAphbHQgcCBlID0KICAgIGRvIHggPC0gZQogICAgICAgcmV0dXJuIChNYXQg
cCAoTm9ybWFsIHgpIFtdKQoKICAKcGNvbnMgOjogUGF0dCAtPiBQYXR0IC0+IFBhdHQKLS0gcGNv
bnMgeCB4cyA9IHB0dXAgKHggOiBbeHNdKSAtLSBOT1QgY29ycmVjdCwgZ2l2ZXMgKHgsIHhzKSBp
bnN0ZWFkIG9mIHggOiB4cwpwY29ucyB4IHhzID0gcGNvbiAiR0hDLkJhc2U6OiIgW3gseHNd

------_=_NextPart_001_01C2CE03.9657DA27
Content-Type: application/octet-stream;
	name="Main.hs"
Content-Transfer-Encoding: base64
Content-Description: Main.hs
Content-Disposition: attachment;
	filename="Main.hs"

ey0jIE9QVElPTlMgLWZnbGFzZ293LWV4dHMgIy19Cgptb2R1bGUgTWFpbiB3aGVyZQogCmltcG9y
dCBaaXAKCgpmIHhzIHlzID0gJCh6aXBOIDIpIHhzIHlzCgpnIHggPSB4IDogW10KCm1haW4gPSBw
cmludCAoZiAiZm9vIiAiYmF6IikK

------_=_NextPart_001_01C2CE03.9657DA27--