Monad.Based_on_map
In a computer science context, this is a rarer definition of a monadic type constructor. It is however equivalent to the Kleisli triple one, and may be simpler to use for certain monads, such as the list
one.
This approach requires three monadic operations. The first one, as in the Kleisli triple approach, is a return
function, allowing to plundge values into the monad. The second one is a map
, that can be understand as a parallel of the map
on list, i.e a way to apply a function through the monad. Finally, the third one is a flatten
function, which allows to handle nested applications of the monad.
As in the Kleisli triple approach, all those functions must satisfy some laws to fully qualify as a monad :
1. ∀x:'a, ∀f:('a -> 'b), return (f x) ≣ map f (return x)
2. ∀m:('a t t t), flatten (map flatten m) ≣ flatten (flatten m)
3. ∀m:('a t), flatten (map return m) ≣ flatten (return m) ≣ m
4. ∀m:('a t t), ∀f: ('a -> 'b), flatten (map (map f) m) ≣ map f (flatten m)
As there is no way in the OCaml type system to enforce those properties, users have to trust the implemented monad when using it, and developers have to manually check that they are respected.
More explanations on this approach on monads and its deep roots in the category theory can be found at the end of this file.
val return : 'a -> 'a t