From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.0 required=5.0 tests=AWL,SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 0363FBC6B for ; Thu, 9 Aug 2007 20:30:17 +0200 (CEST) Received: from ausone.inria.fr (peray.inria.fr [128.93.8.98]) by concorde.inria.fr (8.13.6/8.13.6) with SMTP id l79IUB9o018446; Thu, 9 Aug 2007 20:30:12 +0200 Received: by ausone.inria.fr (sSMTP sendmail emulation); Thu, _d Aug 2007 20:29:49 +0200 From: "Nicolas Pouillard" Cc: "O'Caml Mailing List" Subject: Re: [Caml-list] [Camlp4][trivial ?] Anonymous bind (for monads) To: Gabriel Kerneis References: In-Reply-To: Date: Thu, 09 Aug 2007 20:29:49 +0200 Message-Id: <1186684184-sup-2689@ert.local> User-Agent: Sup/0.1.$Revision:$ Content-Disposition: inline Content-Type: text/plain; charset=utf-8 X-j-chkmail-Score: MSGID : 46BB5D33.000 on concorde : j-chkmail score : X : 0/20 1 0.000 -> 1 X-Miltered: at concorde with ID 46BB5D33.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; camlp:01 trivial:01 monads:01 camlp:01 ocaml:01 struct:01 syntax:01 sig:01 syntax:01 struct:01 expr:01 expr:01 foo:01 ocaml:01 ocamlc:01 Excerpts from Gabriel Kerneis's message of Tue Jul 31 18:55:05 +0200 2007: > Hello, > > I've got a little problem with camlp4. The following works perfectly > well : > ##################################################################### > ~/ocaml/camlp4% cat pa_bind.ml > module Id = struct > let name = "pa_bind" > let version = "$Id:$" > end > > module Example (Syntax : Camlp4.Sig.Camlp4Syntax) = struct > include Syntax > > EXTEND Gram > expr: LEVEL "top" > [[ t=expr; "foo"; f=expr -> > <:expr< > Lwt.bind $t$ (fun () -> $f$) > >>]]; > END > end > > module M = Camlp4.Register.OCamlSyntaxExtension(Id)(Example) > > ~/ocaml/camlp4% ocamlc -c -pp camlp4of -I +camlp4 pa_bind.ml > > ~/ocaml/camlp4% cat test_bind.ml > let r = (Lwt_unix.sleep 5) foo (print_endline "42") > > ~/ocaml/camlp4% camlp4of -parser pa_bind.cmo test_bind.ml > let r = Lwt.bind (Lwt_unix.sleep 5) (fun () -> print_endline "42") > > ##################################################################### > > But if I change "foo" to ">>" (which is the usual way to write an > anonymous bind for monads), it doesn't work anymore. I first thought the > reason was that ">>" is also the symbol for closing a quotation, but > trying "++" instead proved useless as well. > > I'm certainly not a camlp4 guru so... what am I missing? When you use "foo", you create a new keyword. You can therefor choose its priority. However the parsing of ">>" is predefined since OCaml supports user-defined operators. In your example using t=expr is too greedy since it contains ">>" in the infixop0 rule. However if you use SELF instead of expr it works fine. Indeed SELF handles the left recursion and don't blindly parse an expr but remember that ">>" is waited. ... EXTEND Gram expr: LEVEL "top" [[ t = SELF; ">>"; f = SELF -> <:expr< Lwt.bind $t$ (fun () -> $f$) >>]]; END ... Best regards, -- Nicolas Pouillard aka Ertai