nLab
monad (in computer science)

Context

Type theory

natural deduction metalanguage, practical foundations

  1. type formation rule
  2. term introduction rule
  3. term elimination rule
  4. computation rule

type theory (dependent, intensional, observational type theory, homotopy type theory)

syntax object language

computational trinitarianism = propositions as types +programs as proofs +relation type theory/category theory

logiccategory theorytype theory
trueterminal object/(-2)-truncated objecth-level 0-type/unit type
falseinitial objectempty type
proposition(-1)-truncated objecth-level 1-type/h-prop
proofgeneralized elementprogram
conjunctionproductproduct type
disjunctioncoproduct ((-1)-truncation of)sum type (bracket type of)
implicationinternal homfunction type
negationinternal hom into initial objectfunction type into empty type
universal quantificationdependent productdependent product type
existential quantificationdependent sum ((-1)-truncation of)dependent sum type (bracket type of)
equivalencepath space objectidentity type
equivalence classquotientquotient type
inductioncolimitinductive type, W-type, M-type
higher inductionhigher colimithigher inductive type
completely presented setdiscrete object/0-truncated objecth-level 2-type/preset/h-set
setinternal 0-groupoidBishop set/setoid
universeobject classifiertype of types
modalityclosure operator monadmodal type theory, monad (in computer science)

homotopy levels

semantics

Contents

Idea

In computer science, a monad describes a “notion of computation”. Formally, it is a map that

  • sends every type X of some given programming language to a new type T(X) (called the “type of T-computations with values in X”);

  • equipped with a rule for composing two functions of the form f:XT(Y) (called Kleisli functions) and g:YT(Z) to a function gf:XT(Z) (their Kleisli composition);

  • in a way that is associative in the evident sense and unital with respect to a given unit function called pure X:XT(X), to be thought of as taking a value to the pure computation that simply returns that value.

This is essentially the same structure as a monad in category theory, but presented differently; see below for the precise relationship.

For imperative programs in functional programming

Monads provide one way to “embed imperative programming? in functional programming”, and are used that way notably in the programming language Haskell. (But monads, as well as comonads and related structures, exist much more generally in programming languages; an exposition is in (Harper)).

For instance when the monad T() forms product types T(X)X×Q with some fixed type Q that carries the structure of a monoid, then a Kleisli function f:XY×Q may be thought of as a function XY that produces a “side effect” output of type Q. The Kleisli composition of two functions f:XY×Q and g:YZ×Q then not only evaluates the two programs in sequence but also combines their Q-output using the monoid operation of Q; so if fx=(y,q) and gy=(z,q) then the final result of (gf)(x) will be (z,qq). For example, Q might be the set of strings of characters, and the monoid operation that of concatenation of strings (i.e. Q is the free monoid on the type of characters). If the software is designed such that values of type Q computed in this way appear on the user’s scteen or are written to memory, then this is a way to encode input/output in functional programming (see the IO monad? below).

But monads have plenty of further uses. They are as ubiquituous (sometimes in disguise) in computer science as monads in the sense of category theory are (sometimes in disguise) in category theory. This is no coincidence, see Relation to monads in category theory below.

Relation to monads in category theory

In computer science, a programming language may be formalised or studied by means of a category, called the syntactic category 𝒞, whose

This point of view (see computational trinitarianism) is particularly useful when studying purely functional programming languages.

Under this relation between type theory and category theory monads on the type system in the sense of computer science are monads in the sense of category theory, being certain endofunctors

T:𝒞𝒞T : \mathcal{C} \to \mathcal{C}

on the syntactic category. This functor

  1. sends each type, hence object X𝒞 to another object T(X);

  2. the unit natural transformation ϵ:Id 𝒞T of the monad T provides for each type X a component morphism pure X:XT(X);

  3. the multiplication natural transformation μ:TTT of the monad provides for each object X a morphism μ X:T(T(X))X which induces the Kleisli composition by the formula

(gf) (YgT(Y)) Kleisli(XfT(Y)) XfT(Y)T(g)T(T(Z))μ(Z)TZ,\begin{aligned} (g \circ f) &\coloneqq (Y \stackrel{g}{\to} T(Y)) \circ_{Kleisli} (X \stackrel{f}{\to} T(Y)) \\ & \coloneqq X \stackrel{f}{\to} T(Y) \stackrel{T(g)}{\to} T(T(Z)) \stackrel{\mu(Z)}{\to} T Z \end{aligned} \,,

Here the morphism T(g) in the middle of the last line makes use of the fact that T() is indeed a functor and hence may also be applied to morphisms / functions between types. The last morphism μ(Z) is the one that implements the ”T-computation”.

The monads arising this way in computer science are usually required also to interact nicely with the structure of the programming language, as encoded in the structure of its syntactic category; in most cases, terms of the language will be allowed to take more than one input, so the category 𝒞 will be at least monoidal, and the corresponding kind of ‘nice’ interaction corresponds to the monad’s being a strong monad.

Examples

  • A number of monads are definable in terms of standard type-forming operations, such as the maybe monad?, the reader monad?, the writer monad?, and the state monad?.

  • Other monads may be supplied “axiomatically” by the programming language, such as the IO monad? in Haskell.

  • Equipping homotopy type theory (say implemented as a programming language concretely in Coq or Agda) with two axiomatic idempotent monads, denoted and Π, with some additional data and relations, turns it into cohesive homotopy type theory. See also modal type theory.

Examples of (co)monads in type theory appear in

References

The original reference for monads as ‘notions of computation’ is

  • Eugenio Moggi, Notions of computation and monads Information And Computation, 93(1), 1991. (pdf)

Expositions are in

The specification of monads in Haskell is at

Discussion of aspects of quantum computing in terms of monads in functional programming are in

Revised on April 24, 2013 21:20:50 by Urs Schreiber (131.174.42.61)