Le 17/09/2011 08:38, Dmitry Grebeniuk a écrit :
>   It does not respect the monad associativity:
> 
>     (m >>= f) >>= g   ==   m >>= (fun x -> f x >>= g)
> [...]
> ==============
> open Lwt;
> 
> value () = Printf.printf "case 1, enter two lines:\n%!";
> value m = Lwt_io.read_line Lwt_io.stdin;
> value f_v = Lwt_io.read_line Lwt_io.stdin;
> value f = fun _ -> f_v;
> value g = Lwt.return;
> value res = (m >>= f) >>= g;
> 
> (*
> value () = Printf.printf "case 2, enter two lines:\n%!";
> value f_v = Lwt_io.read_line Lwt_io.stdin;
> value m = Lwt_io.read_line Lwt_io.stdin;
> value f = fun _ -> f_v;
> value g = Lwt.return;
> value res = m >>= (fun x -> f x >>= g);
> *)
> 
> value () = Lwt_main.run (res >>= fun s ->
>   Lwt_io.write_line Lwt_io.stdout ("res = " ^ s));
> ==============

Using Lwt doesn't automatigally make OCaml purely functional... in your
example, you change the order of m and f_v, which both do side-effects,
so they are different in the two instances of res, so they cannot be
compared. Can you give an example that involves only pure functions?

>   When I began to use Lwt, I was badly surprised
> by the fact that Lwt.t values are not real I/O actions.
> So I had to get a new habit: if the top-level module
> value has type Lwt.t 'a, I have to defer its
> computation by wrapping it under "fun () -> <expr>".
> And I have to remember which of my functions return
> Lwt.t values, which of my abstract types really have
> type Lwt.t 'a or contain the Lwt.t 'a values, to defer
> their computations/sideeffects too.

Maybe what you expected was something that encapsulates the IO monad
too. Lwt doesn't do that: as with raw OCaml, IOs are direct with Lwt.


Cheers,

-- 
Stéphane



-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Reply via email to