caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Camlp4][trivial ?] Anonymous bind (for monads)
@ 2007-07-31 16:55 Gabriel Kerneis
  2007-07-31 17:02 ` [Caml-list] " Gabriel Kerneis
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Gabriel Kerneis @ 2007-07-31 16:55 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1285 bytes --]

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?

Thanks a lot.

Regards,
-- 
Gabriel

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] [Camlp4][trivial ?] Anonymous bind (for monads)
  2007-07-31 16:55 [Camlp4][trivial ?] Anonymous bind (for monads) Gabriel Kerneis
@ 2007-07-31 17:02 ` Gabriel Kerneis
  2007-07-31 17:50 ` Julien Moutinho
  2007-08-09 18:29 ` Nicolas Pouillard
  2 siblings, 0 replies; 4+ messages in thread
From: Gabriel Kerneis @ 2007-07-31 17:02 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 374 bytes --]

Le Tue, 31 Jul 2007 18:55:05 +0200, Gabriel Kerneis
<gabriel.kerneis@enst.fr> a écrit :
> ~/ocaml/camlp4% cat test_bind.ml 
> let r = (Lwt_unix.sleep 5) foo (print_endline "42")

Careful readers will notice that I should have had written: 
let r = (Lwt_unix.sleep 5) foo (Lwt.return (print_endline "42"))

But it doesn't change anything, of course.

-- 
Gabriel

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] [Camlp4][trivial ?] Anonymous bind (for monads)
  2007-07-31 16:55 [Camlp4][trivial ?] Anonymous bind (for monads) 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
  2 siblings, 0 replies; 4+ messages in thread
From: Julien Moutinho @ 2007-07-31 17:50 UTC (permalink / raw)
  To: Gabriel Kerneis; +Cc: caml-list

On Tue, Jul 31, 2007 at 06:55:05PM +0200, Gabriel Kerneis wrote:
>  EXTEND Gram
>  expr: LEVEL "top"
>    [[ t=expr; "foo"; f=expr ->
>         <:expr<
>             Lwt.bind $t$ (fun () -> $f$)
>         >>]];
>  END
> end
> [...]
> 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?

My guess is that the first [t=expr] swallows the ">>".
You can try "->" or "|" against which [expr] does not match,
but I think it is not advisable to use them in a bigger program.

However, I'm sure that Mr.Pouillard will be happy to enlight us on how to use ">>".

HTH a little.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] [Camlp4][trivial ?] Anonymous bind (for monads)
  2007-07-31 16:55 [Camlp4][trivial ?] Anonymous bind (for monads) 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
  2 siblings, 0 replies; 4+ messages in thread
From: Nicolas Pouillard @ 2007-08-09 18:29 UTC (permalink / raw)
  To: Gabriel Kerneis; +Cc: O'Caml Mailing List

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-08-09 18:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-07-31 16:55 [Camlp4][trivial ?] Anonymous bind (for monads) 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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).