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

Le samedi 17 septembre 2011 à 09:38 +0300, Dmitry Grebeniuk a écrit : 
> 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.

You are comparing Lwt with the IO monad. They are two different monads:
IO deals with actions while Lwt deals with threads. If you write:

  let m = IO.read_char IO.stdin

then [m] is just the description of the action of reading a character
from [stdin]. On the contrary, if you write:

  let m = Lwt_io.read_char Lwt_io.stdin

then [m] is really a thread waiting for a character from [stdin]. If you
use "fun () -> ..." everywhere then this becomes actions but it is not
threads anymore and you loose most of the advantages of Lwt. For example
with Lwt you can write:

  let t1 = f1 ()
  and t2 = f2 ()
  ...
  and tn = fn () in 
  Lwt.bind t1 (fun x1 -> Lwt.bind t2 (fun x2 -> ... Lwt.bind tn (fun xn -> return (x1, x2, ..., xn)) ...))

and this will let [t1], ..., [tn] run concurrently. With the IO monad
this is just exactly the same as:

  let x1 = f1 () in
  let x2 = f2 () in
  ...
  let xn = fn () in
  (x1, x2, ..., xn)

in a non-monadic world. There is no concurrency at all.

Cheers,

-- 
Jérémie



      parent reply	other threads:[~2011-09-17 10:20 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
2011-09-17 10:20         ` Jérémie Dimino [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=1316254818.28210.37.camel@aurora \
    --to=jeremie@dimino.org \
    --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;
as well as URLs for NNTP newsgroup(s).