From: "Nicolas Pouillard" <nicolas.pouillard@gmail.com>
To: Gabriel Kerneis <gabriel.kerneis@enst.fr>
Cc: "O'Caml Mailing List" <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] [Camlp4][trivial ?] Anonymous bind (for monads)
Date: Thu, 09 Aug 2007 20:29:49 +0200 [thread overview]
Message-ID: <1186684184-sup-2689@ert.local> (raw)
In-Reply-To: <E1IFv09-00041N-5v@kerneis.info>
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
prev parent reply other threads:[~2007-08-09 18:30 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-07-31 16:55 Gabriel Kerneis
2007-07-31 17:02 ` [Caml-list] " Gabriel Kerneis
2007-07-31 17:50 ` Julien Moutinho
2007-08-09 18:29 ` Nicolas Pouillard [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1186684184-sup-2689@ert.local \
--to=nicolas.pouillard@gmail.com \
--cc=caml-list@yquem.inria.fr \
--cc=gabriel.kerneis@enst.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox