* [Caml-list] Monad Library? @ 2012-11-23 11:43 Chris Yocum 2012-11-23 11:54 ` Wojciech Meyer 2012-11-23 20:59 ` Michał Kurcewicz 0 siblings, 2 replies; 10+ messages in thread From: Chris Yocum @ 2012-11-23 11:43 UTC (permalink / raw) To: caml-list [-- Attachment #1: Type: text/plain, Size: 428 bytes --] 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 [-- Attachment #2: Digital signature --] [-- Type: application/pgp-signature, Size: 230 bytes --] ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] Monad Library? 2012-11-23 11:43 [Caml-list] Monad Library? 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 20:59 ` Michał Kurcewicz 1 sibling, 2 replies; 10+ messages in thread From: Wojciech Meyer @ 2012-11-23 11:54 UTC (permalink / raw) To: Chris Yocum; +Cc: Caml List On Fri, Nov 23, 2012 at 11:43 AM, Chris Yocum <cyocum@gmail.com> wrote: > 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 pa_monad is really useful and nice, I think the updated library is available as pa_monad_custom OPAM package. > 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? First I would need to find an answer what do you mean by a monad library. Monads is a general abstraction with a very simple interface. so they don't require library as such. However I agree it would be good to have some library that provides a monadic interface to some common functionality found maybe in std libraries, also some way of composing monads would be good having monad transformers along. Batteries included offer monadic interface to some common data types like list, bool, option. You could look at Xavier Leroy's lectures here [1] to see the excellent examples of using monads in OCaml. For somewhat advanced type system trickery to use generic interface for monads you can look at this excellent post [2]. -Wojciech [1] http://gallium.inria.fr/~xleroy/mpri/progfunc/monads.2up.pdf [2] http://alaska-kamtchatka.blogspot.co.uk/2011/09/higher-order-fun.html [3] http://batteries.forge.ocamlcore.org/ ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] Monad Library? 2012-11-23 11:54 ` Wojciech Meyer @ 2012-11-23 12:05 ` Chris Yocum 2012-11-23 12:09 ` David House 1 sibling, 0 replies; 10+ messages in thread From: Chris Yocum @ 2012-11-23 12:05 UTC (permalink / raw) To: Wojciech Meyer; +Cc: Caml List [-- Attachment #1: Type: text/plain, Size: 2406 bytes --] On Fri, Nov 23, 2012 at 11:54:40AM +0000, Wojciech Meyer wrote: > On Fri, Nov 23, 2012 at 11:43 AM, Chris Yocum <cyocum@gmail.com> wrote: > > 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 > > pa_monad is really useful and nice, I think the updated library is > available as pa_monad_custom OPAM package. > Yeah, I saw that. I will install it and have a look. > > 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? > > First I would need to find an answer what do you mean by a monad library. > > Monads is a general abstraction with a very simple interface. so they > don't require library as such. > > However I agree it would be good to have some library that provides a > monadic interface to some common functionality found maybe in std > libraries, also some way of composing monads would be good having > monad transformers along. Batteries included offer monadic interface > to some common data types like list, bool, option. > Ok, I had only seen the Monad module in batteries. Also, I tend not to like batteries as it bloats the executable size; I get something like a 5MB executable for a small amount of code. In general, what I would like in a "monad library" would be a collection of "standard monads". I am using scarequotes because I don't know what all of this means. Looking at the Haskell Wiki (http://www.haskell.org/haskellwiki/Monad#Common_monads) lists the common monads as "Maybe, List, State, Reader, and IO". I would assume that a monad library would have these and a method for building new ones. > You could look at Xavier Leroy's lectures here [1] to see the > excellent examples of using monads in OCaml. > > For somewhat advanced type system trickery to use generic interface > for monads you can look at this excellent post [2]. Great. Thanks for the info! Chris > > -Wojciech > > [1] http://gallium.inria.fr/~xleroy/mpri/progfunc/monads.2up.pdf > [2] http://alaska-kamtchatka.blogspot.co.uk/2011/09/higher-order-fun.html > [3] http://batteries.forge.ocamlcore.org/ [-- Attachment #2: Digital signature --] [-- Type: application/pgp-signature, Size: 230 bytes --] ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] Monad Library? 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 1 sibling, 1 reply; 10+ messages in thread From: David House @ 2012-11-23 12:09 UTC (permalink / raw) To: Wojciech Meyer; +Cc: Chris Yocum, Caml List The Core library [1] has a good monad library that gets used in basically every single Jane Street program (and notably inside async). Among its features are: * Module types for monads with one or two type parameters * Functors to make modules satisfying those signatures given just bind + return * "Infix" submodules which one can locally open ("let open Option.Monad_infix in") in order to temporarily work inside another monad -- very useful. It notably does *not* feature a syntax extension to make monadic computations look pretty. You must write out the binds and returns yourself. To be honest, the reason for this is mostly history, but we actually have some anecdotal evidence now that writing in the explicit style makes things easier to understand for authors and readers of code. [1]: https://bitbucket.org/yminsky/ocaml-core/wiki/Home On Fri, Nov 23, 2012 at 11:54 AM, Wojciech Meyer <wojciech.meyer@googlemail.com> wrote: > On Fri, Nov 23, 2012 at 11:43 AM, Chris Yocum <cyocum@gmail.com> wrote: >> 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 > > pa_monad is really useful and nice, I think the updated library is > available as pa_monad_custom OPAM package. > >> 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? > > First I would need to find an answer what do you mean by a monad library. > > Monads is a general abstraction with a very simple interface. so they > don't require library as such. > > However I agree it would be good to have some library that provides a > monadic interface to some common functionality found maybe in std > libraries, also some way of composing monads would be good having > monad transformers along. Batteries included offer monadic interface > to some common data types like list, bool, option. > > You could look at Xavier Leroy's lectures here [1] to see the > excellent examples of using monads in OCaml. > > For somewhat advanced type system trickery to use generic interface > for monads you can look at this excellent post [2]. > > -Wojciech > > [1] http://gallium.inria.fr/~xleroy/mpri/progfunc/monads.2up.pdf > [2] http://alaska-kamtchatka.blogspot.co.uk/2011/09/higher-order-fun.html > [3] http://batteries.forge.ocamlcore.org/ > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa.inria.fr/sympa/arc/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] Monad Library? 2012-11-23 12:09 ` David House @ 2012-11-23 12:09 ` David House 0 siblings, 0 replies; 10+ messages in thread From: David House @ 2012-11-23 12:09 UTC (permalink / raw) To: Wojciech Meyer; +Cc: Chris Yocum, Caml List Of, and of course it features instances for Option.t, Result.t, Or_error.t, List.t and all the other standard types inside core! On Fri, Nov 23, 2012 at 12:09 PM, David House <dhouse@janestreet.com> wrote: > The Core library [1] has a good monad library that gets used in > basically every single Jane Street program (and notably inside async). > Among its features are: > > * Module types for monads with one or two type parameters > > * Functors to make modules satisfying those signatures given just bind + return > > * "Infix" submodules which one can locally open ("let open > Option.Monad_infix in") in order to temporarily work inside another > monad -- very useful. > > It notably does *not* feature a syntax extension to make monadic > computations look pretty. You must write out the binds and returns > yourself. To be honest, the reason for this is mostly history, but we > actually have some anecdotal evidence now that writing in the explicit > style makes things easier to understand for authors and readers of > code. > > [1]: https://bitbucket.org/yminsky/ocaml-core/wiki/Home > > > On Fri, Nov 23, 2012 at 11:54 AM, Wojciech Meyer > <wojciech.meyer@googlemail.com> wrote: >> On Fri, Nov 23, 2012 at 11:43 AM, Chris Yocum <cyocum@gmail.com> wrote: >>> 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 >> >> pa_monad is really useful and nice, I think the updated library is >> available as pa_monad_custom OPAM package. >> >>> 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? >> >> First I would need to find an answer what do you mean by a monad library. >> >> Monads is a general abstraction with a very simple interface. so they >> don't require library as such. >> >> However I agree it would be good to have some library that provides a >> monadic interface to some common functionality found maybe in std >> libraries, also some way of composing monads would be good having >> monad transformers along. Batteries included offer monadic interface >> to some common data types like list, bool, option. >> >> You could look at Xavier Leroy's lectures here [1] to see the >> excellent examples of using monads in OCaml. >> >> For somewhat advanced type system trickery to use generic interface >> for monads you can look at this excellent post [2]. >> >> -Wojciech >> >> [1] http://gallium.inria.fr/~xleroy/mpri/progfunc/monads.2up.pdf >> [2] http://alaska-kamtchatka.blogspot.co.uk/2011/09/higher-order-fun.html >> [3] http://batteries.forge.ocamlcore.org/ >> >> -- >> Caml-list mailing list. Subscription management and archives: >> https://sympa.inria.fr/sympa/arc/caml-list >> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners >> Bug reports: http://caml.inria.fr/bin/caml-bugs ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] Monad Library? 2012-11-23 11:43 [Caml-list] Monad Library? Chris Yocum 2012-11-23 11:54 ` Wojciech Meyer @ 2012-11-23 20:59 ` Michał Kurcewicz 2012-11-24 16:34 ` Chris Yocum 1 sibling, 1 reply; 10+ messages in thread From: Michał Kurcewicz @ 2012-11-23 20:59 UTC (permalink / raw) To: Chris Yocum; +Cc: caml-list 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 ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] Monad Library? 2012-11-23 20:59 ` Michał Kurcewicz @ 2012-11-24 16:34 ` Chris Yocum 2012-11-24 16:54 ` Malcolm Matalka 2012-11-24 18:34 ` Gabriel Scherer 0 siblings, 2 replies; 10+ messages in thread From: Chris Yocum @ 2012-11-24 16:34 UTC (permalink / raw) To: Michał Kurcewicz; +Cc: caml-list [-- Attachment #1: Type: text/plain, Size: 1520 bytes --] 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 [-- Attachment #2: Digital signature --] [-- Type: application/pgp-signature, Size: 230 bytes --] ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] Monad Library? 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 1 sibling, 1 reply; 10+ messages in thread From: Malcolm Matalka @ 2012-11-24 16:54 UTC (permalink / raw) To: Chris Yocum; +Cc: Michał Kurcewicz, caml-list I haven't experienced a strong need for a state monad in Ocaml. I do tend to use Core's Option and Return (>>=) functions for sequencing code though. /M P.S. In general Ocamler's seem to prefer to write 'Store x' rather than 'Store(x)' Chris Yocum <cyocum@gmail.com> writes: > 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 ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] Monad Library? 2012-11-24 16:54 ` Malcolm Matalka @ 2012-11-24 17:04 ` Chris Yocum 0 siblings, 0 replies; 10+ messages in thread From: Chris Yocum @ 2012-11-24 17:04 UTC (permalink / raw) To: Malcolm Matalka; +Cc: Michał Kurcewicz, caml-list [-- Attachment #1: Type: text/plain, Size: 2253 bytes --] Yeah, I was thinking again after sending it that this isn't really needed when you have functional updates and other ways of managing state. Chris On Sat, Nov 24, 2012 at 11:54:48AM -0500, Malcolm Matalka wrote: > I haven't experienced a strong need for a state monad in Ocaml. I do > tend to use Core's Option and Return (>>=) functions for sequencing code > though. > > /M > > P.S. In general Ocamler's seem to prefer to write 'Store x' rather than > 'Store(x)' > > > Chris Yocum <cyocum@gmail.com> writes: > > > 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 [-- Attachment #2: Digital signature --] [-- Type: application/pgp-signature, Size: 230 bytes --] ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] Monad Library? 2012-11-24 16:34 ` Chris Yocum 2012-11-24 16:54 ` Malcolm Matalka @ 2012-11-24 18:34 ` Gabriel Scherer 1 sibling, 0 replies; 10+ messages in thread From: Gabriel Scherer @ 2012-11-24 18:34 UTC (permalink / raw) To: Chris Yocum; +Cc: Michał Kurcewicz, caml-list > [...] > 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 ^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2012-11-24 18:35 UTC | newest] Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2012-11-23 11:43 [Caml-list] Monad Library? 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 is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox