caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Nicolas Pouillard" <nicolas.pouillard@gmail.com>
To: Paolo Donadeo <p.donadeo@gmail.com>
Cc: Caml_mailing list <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] Disappointment
Date: Tue, 22 Jul 2008 14:57:31 +0200	[thread overview]
Message-ID: <1216730478-sup-1986@port-ext16.ensta.fr> (raw)
In-Reply-To: <4b5157c30807211428r19ef9865n6a65e81ac2f5fe31@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2431 bytes --]

Excerpts from Paolo Donadeo's message of Mon Jul 21 23:28:36 +0200 2008:
> 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?

Two key points that helped me:

* Monads help to separate some plumbing from your code.

* Monads provide a way to abstract code over some "let" construct.

I will note that specific "let" construct "let!", it's somewhat
like the do-notation but more atomic.

Monads also come with "return", but that's not the essence of them.

Think about that example:

  val div : int -> int -> int option
  val square : int -> option

  let f x y =
    match div x y with
    | None -> None (* here 'y' was equal to 0 *)
    | Some z ->
        match square z with
        | None -> None
        | Some x2 -> Some x2

In the previous example the plumbing is error handling (where errors are
represented by None), and the "let!" construct is:

  let! x = e1 in e2   ===>   match e1 with None -> None | Some x -> e2

And "return" is "Some".

Another similar example:

  type ('a,'b) either = Left of 'a | Right of 'b

  val div : int -> int -> (string, int) either
  val square : int -> (string, int) either

  let f x y =
    match div x y with
    | Left error_msg -> Left error_msg
    | Right z ->
        match square z with
        | Left error_msg -> Left error_msg
        | Right x2 -> Right x2

Here the plumbing is still there for "error handling", but is somewhat
different. The "let!" construct is:

  let! x = e1 in e2   ===>   match e1 with Left m -> Left m | Right x -> e2

And "return" is "Right".

Finally one could have used the "let!" construct
abstractly (and also "return").

  let f x y =
    let! z = div x y in
    let! x2 = square z in
    return x2

One can obtain the two previous versions by choosing
which "let!"/"return" we want, namely choosing the monad.

Hope that helps,

-- 
Nicolas Pouillard aka Ertai

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 194 bytes --]

  parent reply	other threads:[~2008-07-22 12:58 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-07-21 21:28 Disappointment Paolo Donadeo
2008-07-21 21:42 ` [Caml-list] Disappointment Till Crueger
2008-07-22  3:41   ` Fabrice Marchant
2008-07-22  6:50 ` Gabriel Kerneis
2008-07-22 10:56 ` Dr. Thomas Fischbacher
2008-07-22 13:27   ` Axel Poigné
2008-07-22 15:17     ` Andrej Bauer
2008-07-22 15:21       ` Axel Poigné
2008-07-22 12:57 ` Nicolas Pouillard [this message]
2008-07-22 13:16   ` Dario Teixeira
2008-07-22 16:11   ` Christophe TROESTLER
2008-07-22 21:55   ` Paolo Donadeo
2008-07-22 16:10 ` Warren Harris

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=1216730478-sup-1986@port-ext16.ensta.fr \
    --to=nicolas.pouillard@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=p.donadeo@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).