From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA08570; Tue, 13 May 2003 17:23:41 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA08496 for ; Tue, 13 May 2003 17:23:40 +0200 (MET DST) Received: from isis.lip6.fr (isis.lip6.fr [132.227.60.2]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id h4DFNeT07485 for ; Tue, 13 May 2003 17:23:40 +0200 (MET DST) Received: from spi.lip6.fr (IDENT:root@spi.lip6.fr [132.227.83.55]) by isis.lip6.fr (8.12.9/jtpda-5.4+victor) with ESMTP id h4DFNcA7008638 ; Tue, 13 May 2003 17:23:38 +0200 X-pt: isis.lip6.fr Received: from localhost.localdomain (etna.lip6.fr [132.227.83.215]) by spi.lip6.fr (8.8.7/jtpda-5.2) with SMTP id RAA03482 ; Tue, 13 May 2003 17:23:35 +0200 Date: Tue, 13 May 2003 17:23:34 +0200 From: Virgile Prevosto To: Jim Farrand Cc: caml-list@inria.fr Subject: Re: [Caml-list] Inline operators in the revised syntax Message-Id: <20030513172334.29bae6ca.virgile.prevosto@lip6.fr> In-Reply-To: <20030513122147.GA8982@farrand.net> References: <20030513122147.GA8982@farrand.net> X-Mailer: Sylpheed version 0.8.9claws (GTK+ 1.2.10; i686-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Scanned-By: isis.lip6.fr X-Spam: no; 0.00; virgile:01 prevosto:01 caml-list:01 farrand:01 camlp:01 overkill:01 ocamlc:01 -pp:01 mlast:01 -impl:01 pcaml:01 endline:01 lident:01 expr:01 recognizes:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Le mardi 13 mai, =E0 13h21 CEST, Jim Farrand a =E9crit: > Is there a simple way to define and use an inline operator in the revised > syntax? It seems they can be defined but not used. >=20 > The equivelent in the old syntax works fine. (I realise it's probably > possible to use camlp4's grammar extension features to do the same, but > I don't know how to do this, and it seems like overkill when it was so > easy in the old syntax.) Unfortunately for you, the camlp4 manual suggests to use syntax extensions = for infix operators (http://caml.inria.fr/camlp4/manual/manual007.html)...=20 But this doesn't mean that you have to write one syntax extension for every infix operator you want to create: you may extend the syntax once wit= h a rule which will itself make the necessary extensions, such as the following example: --- (* file infix.ml4. compile it with ocamlc -c -pp 'camlp4o pa_extend.cmo q_MLast.cmo -impl' -I +camlp4=20 -impl infix.ml4 usage: infix id x y =3D ... ; creates an infix operator. *) open Pcaml if !Sys.interactive then print_endline "infix definitions";; EXTEND str_item: [ [ "infix"; p =3D LIDENT; argts =3D LIST0 LIDENT; "=3D"; e=3Dexpr -> let def =3D List.fold_right (fun arg e -> <:expr< fun $lid:arg$ -> $e= $>>) argts e in=20 (* we create a new extension which recognizes the expression e1 'p' e2, where 'p' is our new operator. Unfortunately, the EXTEND ... END syntactic sugar cannot help here, since p would be=20 considered as an entry of the grammar and not as a new keyword*) Grammar.extend [Grammar.Entry.obj expr, Some (Gramext.Before "apply"), [None, None,=20 [[Gramext.Sself; Gramext.Stoken ("",p);Gramext.Sself], Gramext.action (fun e2 _ e1 loc ->=20 (MLast.ExApp=20 (loc, MLast.ExApp (loc, <:expr< $lid:p$>>, e1), e2)))]= ]]; (* definition of the operator itself. *) <:str_item< value $lid:p$ =3D $exp:def$ >>]]; END=20 ----=20 Note that this is far from being perfect, since for instance it does not=20 allow you to give the type of the arguments of your operator. Anyway, it=20 works on simple examples: Objective Caml version 3.06 # #load "camlp4r.cma";; Camlp4 Parsing version 3.06 # #load "infix.cmo"; infix definitions # infix \=3D=3D=3D x y =3D x =3D y; value ( =3D=3D=3D ) : 'a -> 'a -> bool =3D # \=3D=3D=3D 1 1; - : bool =3D True # 1 =3D=3D=3D 1; - : bool =3D True # infix foo x y =3D x + y; value foo : int -> int -> int =3D # 1 foo 1; - : int =3D 2 # \foo 1 1; - : int =3D 2 Hope this helps, --=20 E tutto per oggi, a la prossima volta Virgile ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners