I was trying to implement the state monad in OCaml (as an exercise). My implementation looks like this:
module type MONAD_BUILDER =
sig
type 'a t
val return : 'a -> 'a t
val bind : 'a t -> ('a -> 'b t) -> 'b t
end;;
module MonadBuilder = functor (M: MONAD_BUILDER) ->
struct
let ( >>= ) = M.bind
let return = M.return
end;;
module StateM =
struct
type 'a state = { state: 's . 's -> ('a * 's) }
type 'a t = 'a state
let return x = { state = fun s -> (x, s) }
let bind m f =
let aux s =
let (x, s') = m.state s in
(f x).state s'
in { state = aux }
let run m x = fst (m.state x)
end;;
I chose the existential type for the record field since I don't like the idea of using a functor and wrapping the state type in a module. The above implementation works, but I
ran into a problem while implementing getState
and setState
. I tried to implement them like:
let getState = { state = fun s -> (s, s) }
let setState s = { state = fun _ -> ((), s) }
This doesn't work since the inferred field types, e.g. 'a -> ('a * 'a)
and 'a -> (unit * 'a)
, are less generic than the declared type 's . 's -> ('a * 's)
. I understand why this is happening, but I was wondering if there is another way of making it work using the record approach?
Thanks.
Cheers, Alex
The state monad is a built in monad in Haskell that allows for chaining of a state variable (which may be arbitrarily complex) through a series of function calls, to simulate stateful code.
Monads in OCaml are generally used in a slightly different way than in other languages. Rather than using >>= for monadic binds, values are generally bound using a ppx with a syntax such as let%bind x = ... . Additionally, as of OCaml 4.08, the langauge itself contains syntax for calling monadic functions.
Monads are simply a way to wrapping things and provide methods to do operations on the wrapped stuff without unwrapping it. For example, you can create a type to wrap another one, in Haskell: data Wrapped a = Wrap a. To wrap stuff we define return :: a -> Wrapped a return x = Wrap x.
Monads are monoids in the category of endofunctors. Therefore, a monad is just one example of monoid, which is a more general concept.
's. 's -> ('a * 's)
is an universal type. You're going to have a hard time implementing a state with universal types...
There's no clean way of encapsulating an existential type there without using a module (because existential types is what abstract types are for).
You could, of course, publish the state type instead:
type ('a,'s) state = { state : 's -> ('a * 's) }
Or even shorter,
type ('a,'s) state = 's -> 'a * 's
With the additional parameter, your code runs. However, you run into the fact that your parameter must be handled by the monad. So, you can either hide it when building the monad:
module Monad = MonadBuilder(struct
include StateM
type 'a t = ('a,myStateType) state
end)
Or alter your monad design to incorporate an additional type parameter that is to be used for existential types:
module type MONAD_BUILDER =
sig
type ('a,'param) t
val return : 'a -> ('a,'param) t
val bind : ('a,'param) t -> ('a -> ('b,'param) t) -> ('b,'param) t
end;;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With