From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.6 required=5.0 tests=AWL,DNS_FROM_RFC_POST, SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id C0CD3BBAF for ; Tue, 22 Jul 2008 18:10:29 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApkAAPyihUhKfS4cfGdsb2JhbACSB0MBAQsDCgcRA5dehgo X-IronPort-AV: E=Sophos;i="4.31,232,1215381600"; d="scan'208";a="27576180" Received: from yw-out-2324.google.com ([74.125.46.28]) by mail4-smtp-sop.national.inria.fr with ESMTP; 22 Jul 2008 18:10:29 +0200 Received: by yw-out-2324.google.com with SMTP id 5so745121ywb.3 for ; Tue, 22 Jul 2008 09:10:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:cc:message-id:from:to :in-reply-to:content-type:content-transfer-encoding:mime-version :subject:date:references:x-mailer; bh=TvlVWBegshkfOKjj+g4rAzbK9GqM197non5/DOW11hE=; b=hHmVZ2wWVYt3it0nIncAsErEjUEZMUiJed1NXn4eu9m9/IMLLsCGFzVpoxzYiDhfth wG6VSC+OXoGjWvcOBwaxfPKsF3FLaRN4C5Y/fsiOppcVIoqXua6R1Xhtd5sZYIAuartw 0UoYg5Ilx7RRIXhQkYNyCa/KBWwE7KaiC4fYk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=cc:message-id:from:to:in-reply-to:content-type :content-transfer-encoding:mime-version:subject:date:references :x-mailer; b=gvnyU1qENnMis1YE7QsyiMHjm5HnzrcYw+qS4vEmHmGs9TSYbbqq/M6EJczUAiZfR9 hePx+M45mgjmdfuMBHN/qJV/dJofQntYm36WtSGLiwTmXTShcU1/xeVDervOfHzNmNVj CFjXglv9hCapxzysaOe+bcbNe+/4GN35Mbef4= Received: by 10.143.32.4 with SMTP id k4mr1910932wfj.251.1216743027226; Tue, 22 Jul 2008 09:10:27 -0700 (PDT) Received: from timesink.local ( [98.210.196.241]) by mx.google.com with ESMTPS id 31sm1396870wff.16.2008.07.22.09.10.25 (version=TLSv1/SSLv3 cipher=RC4-MD5); Tue, 22 Jul 2008 09:10:26 -0700 (PDT) Cc: "caml-list caml-list" Message-Id: <9EBEAD3E-7D57-480E-97C8-16362F0EFA68@gmail.com> From: Warren Harris To: Paolo Donadeo - p.donadeo@gmail.com <+caml+warren+98d0e878bf.p.donadeo#gmail.com@spamgourmet.com> In-Reply-To: <4b5157c30807211428r19ef9865n6a65e81ac2f5fe31@mail.gmail.com> Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (Apple Message framework v926) Subject: Re: [Caml-list] Disappointment Date: Tue, 22 Jul 2008 09:10:22 -0700 References: <4b5157c30807211428r19ef9865n6a65e81ac2f5fe31@mail.gmail.com> X-Mailer: Apple Mail (2.926) X-Spam: no; 0.00; monads:01 haskell:01 implements:01 monads:01 monadic:01 monadic:01 abstraction:01 vaguely:01 haskell:01 wikipedia:01 wiki:01 univ-orleans:01 lifo:01 beginner's:01 ocaml:01 Here's my layman's perspective... I think the most important thing to understand about monads is that they allow a bit of data (state) to follow the control flow of your program... around loops (via tail-recursion), through assignments (via the 'bind' operator) and out of one routine and into another (via 'return'). As the state goes along for this ride, it can be accessed and updated (replaced with a new state). This is how Haskell implements imperative constructs -- it propagates them in this bit of state that flows through the program. To understand this, I would start with the state-transition monad. There it's easy to see how 'bind' propagates state, and how 'return' packages state into the result. Things get interesting with monads when this state contains continuations -- functions that have been captured along the way and stored in the state can be later invoked to return control to previous points in your program. A monadic version of call/cc (e.g. from scheme) can be implemented this way. This is essentially what enables lightweight thread packages like Lwt to suspend and resume control. Perhaps one non-obvious thing about monads is how they hide this state from the program that uses it. Operations that are "in the monad" can see an manipulate the state. It appears as additional parameters to the operation, or (more accurately) as parameters to a function being returned from the monadic operation. Operations that simply use the monad are really composing these functional pieces together (along with their own bits of non-monadic code) to build up bigger monadic operations. Running a monadic program really has 2 distinct phases: First composing all the pieces together (which is why it's critical that non- monadic operations be lazy or hidden inside functions that defer them until the second phase). (Monadic interpreters use the AST to drive this composition process.) Second is the application to the state which puts the whole machine in motion, producing the final result or effect of the program. I hope this explanation helps. I've found monads to be an amazing powerful abstraction, although the explanations of them are often academic and difficult to get one's head around. Understanding them can open many new possibilities however, Warren On Jul 21, 2008, at 2:28 PM, Paolo Donadeo - p.donadeo@gmail.com wrote: > I'm disappointed with myself and my incredibly low IQ. Late this > evening I decided -- and this is the third time -- to be enlightened > by the concept of monad. > > I like functional programming, but monads [1] must be too little to be > grabbed by my mind. This time the interest in monads was aroused by > the interesting article of David Teller, Arnaud Spiwack and Till > Varoquaux [2] about the error monad, but for using the library they > wrote I need at least some knowledge about monads and the do-notation. > > I tried with some tutorials found around, but I still cannot catch the > point: what the hell is a monad? > > I ask you all: can anyone make me a practical example, something > involving strings, files, the network, an image or sound processing > algorithm, something vaguely real? Not abstract mathematical > structures, beautiful algebraic properties and general statements, > please: the net is full of such tutorials, especially Haskell fan > sites ;-) > > Thanks in advance for your patience. > > > [1] http://en.wikipedia.org/wiki/Monad_(symbol) > [2] http://www.univ-orleans.fr/lifo/Members/David.Teller/publications/ml2008.pdf > > > -- > Ing. Paolo Donadeo > Studio Associato 4Sigma > Website: http://www.4sigma.it > Personal website: http://www.donadeo.net/blog > ~ > ~ > :wq > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs