caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Chris Yocum <cyocum@gmail.com>
Cc: "Michał Kurcewicz" <michal.kurcewicz@gmail.com>, caml-list@inria.fr
Subject: Re: [Caml-list] Monad Library?
Date: Sat, 24 Nov 2012 19:34:44 +0100	[thread overview]
Message-ID: <CAPFanBE4ki9iaOobz3RzwUZt5fQeNGcgvp0v9amEOXXvy=aMog@mail.gmail.com> (raw)
In-Reply-To: <20121124163405.GA2158@gmail.com>

> [...]
>   type 'a t = Store of 'a
> [...]
>
> This is a very, very basic implementation of a state monad?

No it's not.  What you implemented is the identity monad (type 'a t =
'a, only wrapped under a constructor) that doesn't emulate any effect
at all. The state monad (over some fixed "state" type, for example
"int" if your state is an integer) would use a definition of the form
(untested code):

module StateMonad : sig
  include MONAD
  type state
  val get : state t
  val set : state -> unit t
  val run : state -> 'a t -> state * 'a
end = struct
  type state
  type 'a t = state -> state * 'a
  [...]
end

You should probably try to wrap your head around monads by reading
documentation and implementing some of them yourself *before* looking
for a third-party library that provide monadic facilities.

Wojciech gave excellent references about monads. If you want something
maybe more easy-going and still in OCaml, you could also have a look
at Brian Hurt's "a Monad Tutorial for OCaml":
http://blog.enfranchisedmind.com/2007/08/a-monad-tutorial-for-ocaml/

Of course, most of the literature on monads uses Haskell, the language
in which they became popular first. It's actually quite easy to read
Haskell code if you're already familiar with OCaml, so those documents
should be accessible as well. Of course I don't recommend that if
you're not already comfortable with OCaml semantics (... or
Haskell's).

On Sat, Nov 24, 2012 at 5:34 PM, Chris Yocum <cyocum@gmail.com> wrote:
> Thanks everyone,
>
> So basically from what I understand this is kind of like a StateMonad.
> It seems to me that the state monad (hiding state in the type system)
> would be a large boon.
>
> So, for instance,
>
> module type MONAD =
> sig
>   type 'a t
>   val return : 'a -> 'a t
>   val bind : 'a t -> ('a -> 'b t) -> 'b t
> end
>
> module StateMonad : MONAD =
> struct
>   type 'a t = Store of 'a
>   let return x = Store(x)
>   let bind s f =
>     match s with
>       | Store(x) -> f x
>   let access = function Store(x) -> x
> end
>
> This is a very, very basic implementation of a state monad?
>
> Thanks again.
>
> Chris
>
> On Fri, Nov 23, 2012 at 09:59:30PM +0100, Michał Kurcewicz wrote:
>> If Core is too complex for you then you may take a look at spotlib - a
>> small and elegant library written by Jun Furuse:
>>
>> https://bitbucket.org/camlspotter/spotlib
>>
>> I use it together with pa_monad_custom for all my monad needs.
>>
>> Best,
>>
>> --mk
>>
>> On Fri, Nov 23, 2012 at 12:43 PM, Chris Yocum <cyocum@gmail.com> wrote:
>> > Hi,
>> >
>> > I was looking into using Monads in my programs but I am slightly at a
>> > loss as to what library is in general use.  There is pa_monad but that
>> > seems to be a ocamlp4 exention and not a library.  There is
>> > http://lambda.jimpryor.net/monad_library/ but that doesn't seem to be
>> > in opam or in godi so I am unsure as to its status.
>> >
>> > Does anyone have any suggestion for a well supported monad library for
>> > Ocaml?
>> >
>> > All the best,
>> > Chris

      parent reply	other threads:[~2012-11-24 18:35 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-23 11:43 Chris Yocum
2012-11-23 11:54 ` Wojciech Meyer
2012-11-23 12:05   ` Chris Yocum
2012-11-23 12:09   ` David House
2012-11-23 12:09     ` David House
2012-11-23 20:59 ` Michał Kurcewicz
2012-11-24 16:34   ` Chris Yocum
2012-11-24 16:54     ` Malcolm Matalka
2012-11-24 17:04       ` Chris Yocum
2012-11-24 18:34     ` Gabriel Scherer [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='CAPFanBE4ki9iaOobz3RzwUZt5fQeNGcgvp0v9amEOXXvy=aMog@mail.gmail.com' \
    --to=gabriel.scherer@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=cyocum@gmail.com \
    --cc=michal.kurcewicz@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).