Module Preface_specs.Monad

A Monad allow to sequences operations that are dependent from one to another, in contrast to Applicative, which executes a series of independent actions.

Laws

To have a predictable behaviour, the instance of Monad must obey some laws.

  1. return a >>= f = f a
  2. m >>= return = m
  3. (m >>= f) >>= g = m >>= (fun x +> f x >>= g)
  4. join % join = join % (map join)
  5. join % return = id = join % map pure
  6. map id = id
  7. map (g % f) = map g % map f
  8. map f % join = join % map (map f)
  9. map f % pure = pure % f
  10. return >=> g = g
  11. f >=> return = f
  12. (f >=> g) >=> h = f >=> (g >=> h)

Minimal definition

module type WITH_RETURN = sig ... end

Minimal interface using map and product.

module type WITH_RETURN_AND_BIND = sig ... end

Minimal definition using return and bind.

module type WITH_RETURN_MAP_AND_JOIN = sig ... end

Minimal definition using return, map and join.

module type WITH_RETURN_AND_KLEISLI_COMPOSITION = sig ... end

Minimal definition using return and compose_left_to_right.

Structure anatomy

module type CORE = sig ... end

Basis operations.

module type OPERATION = sig ... end

Additional operations.

module type SYNTAX = sig ... end

Syntax extensions.

module type INFIX = sig ... end

Infix operators.

Complete API

module type API = sig ... end

The complete interface of a Monad.

Additional interfaces

Transformer

A standard representation of a monad transformer. (It is likely that not all transformers respect this interface)

module type TRANSFORMER = sig ... end

Additional references