From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p8HAKMCT006944 for ; Sat, 17 Sep 2011 12:20:22 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: An0FAAN0dE7VuiYS/2dsb2JhbABBhAxJoQ6BfneBUwEBBQwXBFIQCQIODAImAgIsKwaIDKFnkRcOgR6EO4ERBKR4 X-IronPort-AV: E=Sophos;i="4.68,397,1312149600"; d="scan'208";a="109418858" Received: from solaria.dimino.org ([213.186.38.18]) by mail4-smtp-sop.national.inria.fr with ESMTP; 17 Sep 2011 12:20:17 +0200 Received: from aurora (localhost.localdomain [127.0.0.1]) by solaria.dimino.org (Postfix) with ESMTP id 9772080048; Sat, 17 Sep 2011 12:20:16 +0200 (CEST) Received: by aurora (Postfix, from userid 1000) id 8FD8140E71; Sat, 17 Sep 2011 12:20:18 +0200 (CEST) From: =?ISO-8859-1?Q?J=E9r=E9mie?= Dimino To: Dmitry Grebeniuk Cc: caml-list@inria.fr Date: Sat, 17 Sep 2011 12:20:18 +0200 In-Reply-To: References: <20110913183714.GA15241@yeeloong.happyleptic.org> <4E71CDB8.5020704@dogguy.org> <1316092241.28210.14.camel@aurora> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.0.3- Message-ID: <1316254818.28210.37.camel@aurora> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by walapai.inria.fr id p8HAKMCT006944 Subject: Re: [Caml-list] Lwt and exceptions 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 () -> ". > 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