caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Dmitry Grebeniuk <gdsfh1@gmail.com>
To: "Jérémie Dimino" <caml-list@inria.fr>
Subject: Re: [Caml-list] Lwt and exceptions
Date: Sat, 17 Sep 2011 09:38:52 +0300	[thread overview]
Message-ID: <CAPi0vKUhnNuLDyduCs3yBq_O2_3LpzV5=47Y7jfv8FN0kRPc2Q@mail.gmail.com> (raw)
In-Reply-To: <1316092241.28210.14.camel@aurora>

Hello.

>> It uses "bind" and "return", but I'm very unsure
>> whether the code can be called "monadic IO",
>> since lwt itself does not respect the monad laws.
>
> What monadic laws lwt does not respect ?

  Lwt sees values of type Lwt.t not as IO actions,
but sometimes as the values of these actions.

  It does not respect the monad associativity:

    (m >>= f) >>= g   ==   m >>= (fun x -> f x >>= g)

  Look at the source, see the left and the right
parts of this law as the "res" values, uncomment
either block, run the program, enter two lines
of text to program's stdin and see the output.
The output should be the same in both cases
if the law is respected.

==============
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));
==============

  In the first case it's:

case 1, enter two lines:
a
b
res = b

  In the second case it's:

case 2, enter two lines:
a
b
res = a

  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.

  reply	other threads:[~2011-09-17  6:39 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 [this message]
2011-09-17  9:23         ` Stéphane Glondu
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='CAPi0vKUhnNuLDyduCs3yBq_O2_3LpzV5=47Y7jfv8FN0kRPc2Q@mail.gmail.com' \
    --to=gdsfh1@gmail.com \
    --cc=caml-list@inria.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;
as well as URLs for NNTP newsgroup(s).