Using OCaml’s module functors to provide monadic contexts for Batteries
Hi,
Several days ago, bluestorm and I started working on monads-related stuffs for Batteries, so that we’d be able to provide a good API for writing and using monads in Objective Caml.
Well, we’ve now written the basis of the future monad-related functions and modules in Batteries.
Of course, we’ve introduced two key monads, keeping Batteries’ spirit : the Option monad and the Enum monad. The first is the equivalent of Haskell’s Maybe monad, whereas the latter is somehow “equivalent” to Haskell’s List monad, with the difference that Batteries’ key data structure is Enum, whereas Haskell’s standard library’s is List.
We’ve also written two key functions for working with monads… If you are familiar with foldM and sequence in Haskell, working over lists, you won’t be lost when using our fold_monad and sequence functions, working over enums.
Hey, wait a minute…
OCaml’s hasn’t typeclasses ! How can we “overload” bind and return and make sequence and fold_monac guess what is the monad they have to work in ?
The answer is the pa_openin syntax extension.
Now, even if we’re working on adding some trivial uses of our monad related stuffs in Batteries’ testsuite, you can take a look at the following sample which works on my local Batteries version…
1 2 3 4 5 6 | # let module M = Option.Monad in open Enum.WithMonad(M),M in sequence [? Some x |x <- 1--100 ] ;; - : int Batteries.Enum.t option = Some <abstr> # let module M = Option.Monad in open Enum.WithMonad(M),M in sequence (List.enum [Some 1; None ; Some 3]) ;; - : int Batteries.Enum.t option = None |
By the way, do you think, you, user, the (>>=) operator should be provided by default in our XXX.Monad modules ?
Enjoy !
[Link : my git commit]



Can you explain what your code samples do?
I am aware of only very few uses for monads in an impure language like OCaml…
That’s a great initiative.
I don’t care if at first it’s not damn useful, someone had to start it so that the monadic style can flourish.
@Jon : here, I’m calling sequence, which takes an Enum.t of monadic elements ([m a] in Haskell with Lists, ‘a m Enum.t for us, where m is the monad in which we are), and returns a “monadic Enum.t”. In our case, in the Option monad, if we call sequence on an enumeration containing None, it returns None, otherwise if we call it on an enumeration containing, say, Some 1, Some 3 and Some 5, it will return Some e, where e is the enumeration containing 1, 3 and 5.
Such operations are more useful in Haskell when we have a list of IO actions to do, for example.
As we only have two monads in Batteries (in my branch of Batteries, btw, not in the master one), which are Option and Enum, we can’t demonstrate a pretty and handy example of sequence (neither of fold_monad, IMO).
To conclude, my code sample doesn’t compute something very useful, it just shows there are some new code related to monads in Batteries, which can help for building a strong and large codebase of Monads in OCaml.
@Spiceguid : Thanks