From: "Stéphane Glondu" <steph@glondu.net>
To: Dmitry Grebeniuk <gdsfh1@gmail.com>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Lwt and exceptions
Date: Sat, 17 Sep 2011 11:23:51 +0200 [thread overview]
Message-ID: <4E746727.3090804@glondu.net> (raw)
In-Reply-To: <CAPi0vKUhnNuLDyduCs3yBq_O2_3LpzV5=47Y7jfv8FN0kRPc2Q@mail.gmail.com>
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
next prev parent reply other threads:[~2011-09-17 9:23 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-09-13 18:37 rixed
2011-09-13 19:17 ` Anil Madhavapeddy
2011-09-14 16:15 ` rixed
2011-09-15 12:24 ` Jérémie Dimino
2011-09-15 10:04 ` Mehdi Dogguy
2011-09-15 10:35 ` Anil Madhavapeddy
2011-09-15 12:09 ` Jérémie Dimino
2011-09-15 12:33 ` Mehdi Dogguy
2011-09-15 12:22 ` Dmitry Grebeniuk
2011-09-15 13:10 ` Jérémie Dimino
2011-09-17 6:38 ` Dmitry Grebeniuk
2011-09-17 9:23 ` Stéphane Glondu [this message]
2011-09-17 10:20 ` Jérémie Dimino
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=4E746727.3090804@glondu.net \
--to=steph@glondu.net \
--cc=caml-list@inria.fr \
--cc=gdsfh1@gmail.com \
/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