package bap-std

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

Monad is an interface, that is pervasives through many data types. Such types are called monadic. Monad interface is practical for both data (list, option, etc), and codata (Future, Lwt, Deferred, Lazy, Continuation). The interpretation of each monad interface is different for each data type. But in general monad interface consists of two functions:

  • return : 'a -> 'a t
  • bind : 'a t -> ('a -> 'b t) -> 'b t.

This is a minimal definition. Many functions can be inferred based on this interface, see Monad.S for the full list.

Option monad, as well as Or_error and Result monads, is suitable for building control flow. They can be seen as a reification of exception. A chain of such monads link with the bind function, will break as soon as monadic zero value occurs.

Future, as well as Lwt or Async, provides a safe access to codata, i.e., to a value that is defined somewhere outside of the main program, or to a value that is represented by an effectful computation, not by inductive data. Lwt and Async are also so called IO monads, that reifies side-effects, mostly IO. State monad is another example of reification of computation effect. The State monad is useful to build purely functional computation with side-effects. The side-effect, e.g., writing to a memory cell, is reified into an OCaml value, and practically becomes a first class value, i.e., it can be stored, printed, etc.

module type Basic = Core_kernel.Std.Monad.Basic
module type Basic2 = Core_kernel.Std.Monad.Basic2
module type Infix = Core_kernel.Std.Monad.Infix
module type Infix2 = Core_kernel.Std.Monad.Infix2
module type S = Core_kernel.Std.Monad.S
module type S2 = Core_kernel.Std.Monad.S2
module Make (M : Basic) : S with type 'a t := 'a M.t
module Make2 (M : Basic2) : S2 with type ('a, 's) t := ('a, 's) M.t
module State : sig ... end

State monad. See State for more info.

module T : sig ... end

Monad transformers.

OCaml

Innovation. Community. Security.