In
category theory
Category theory is a general theory of mathematical structures and their relations. It was introduced by Samuel Eilenberg and Saunders Mac Lane in the middle of the 20th century in their foundational work on algebraic topology. Category theory ...
, a branch of
mathematics
Mathematics is a field of study that discovers and organizes methods, Mathematical theory, theories and theorems that are developed and Mathematical proof, proved for the needs of empirical sciences and mathematics itself. There are many ar ...
, a monad is a triple
consisting of a
functor
In mathematics, specifically category theory, a functor is a Map (mathematics), mapping between Category (mathematics), categories. Functors were first considered in algebraic topology, where algebraic objects (such as the fundamental group) ar ...
''T'' from a category to itself and two
natural transformations
that satisfy the conditions like associativity. For example, if
are functors
adjoint to each other, then
together with
determined by the adjoint relation is a monad.
In concise terms, a monad is a
monoid in the
category of
endofunctors of some fixed category (an endofunctor is a
functor
In mathematics, specifically category theory, a functor is a Map (mathematics), mapping between Category (mathematics), categories. Functors were first considered in algebraic topology, where algebraic objects (such as the fundamental group) ar ...
mapping a category to itself). According to
John Baez, a monad can be considered at least in two ways:
[ https://golem.ph.utexas.edu/category/2009/07/the_monads_hurt_my_head_but_no.html]
# A monad as a generalized monoid; this is clear since a monad is a monoid in a certain category,
# A monad as a tool for studying
algebraic gadgets; for example, a
group can be described by a certain monad.
Monads are used in the theory of pairs of
adjoint functors, and they generalize
closure operators on
partially ordered sets to arbitrary categories. Monads are also useful in the
theory of datatypes, the
denotational semantics of
imperative programming languages, and in
functional programming languages, allowing languages without mutable state to do things such as simulate
for-loops; see
Monad (functional programming).
A monad is also called, especially in old literature, a triple, triad, standard construction and fundamental construction.
Introduction and definition
A monad is a certain type of
endofunctor. For example, if
and
are a pair of
adjoint functors, with
left adjoint to
, then the composition
is a monad. If
and
are inverse to each other, the corresponding monad is the
identity functor. In general, adjunctions are not
equivalences—they relate categories of different natures. The monad theory matters as part of the effort to capture what it is that adjunctions 'preserve'. The other half of the theory, of what can be learned likewise from consideration of
, is discussed under the dual theory of ''comonads''.
Formal definition
Throughout this article,
denotes a
category. A ''monad'' on
consists of an endofunctor
together with two
natural transformations:
(where
denotes the identity functor on
) and
(where
is the functor
from
to
). These are required to fulfill the following conditions (sometimes called
coherence conditions):
*
(as natural transformations
); here
and
are formed by "
horizontal composition".
*
(as natural transformations
; here
denotes the identity transformation from
to
).
We can rewrite these conditions using the following
commutative diagrams:
See the article on
natural transformations for the explanation of the notations
and
, or see below the commutative diagrams not using these notions:
The first axiom is akin to the
associativity in
monoids if we think of
as the monoid's binary operation, and the second axiom is akin to the existence of an
identity element
In mathematics, an identity element or neutral element of a binary operation is an element that leaves unchanged every element when the operation is applied. For example, 0 is an identity element of the addition of real numbers. This concept is use ...
(which we think of as given by
). Indeed, a monad on
can alternatively be defined as a
monoid in the category
whose objects are the endofunctors of
and whose morphisms are the natural transformations between them, with the
monoidal structure induced by the composition of endofunctors.
The power set monad
The ''power set monad'' is a monad
on the category
: For a set
let
be the
power set of
and for a function
let
be the function between the power sets induced by taking
direct images under
. For every set
, we have a map
, which assigns to every
the
singleton . The function
:
takes a set of sets to its
union. These data describe a monad.
Remarks
The axioms of a monad are formally similar to the
monoid axioms. In fact, monads are special cases of monoids, namely they are precisely the monoids among
endofunctors
, which is equipped with the multiplication given by composition of endofunctors.
Composition of monads is not, in general, a monad. For example, the double power set functor
does not admit any monad structure.
Comonads
The
categorical dual definition is a formal definition of a ''comonad'' (or ''cotriple''); this can be said quickly in the terms that a comonad for a category
is a monad for the
opposite category . It is therefore a functor
from
to itself, with a set of axioms for ''counit'' and ''comultiplication'' that come from reversing the arrows everywhere in the definition just given.
Monads are to monoids as comonads are to ''
comonoids''. Every set is a comonoid in a unique way, so comonoids are less familiar in
abstract algebra
In mathematics, more specifically algebra, abstract algebra or modern algebra is the study of algebraic structures, which are set (mathematics), sets with specific operation (mathematics), operations acting on their elements. Algebraic structur ...
than monoids; however, comonoids in the category of vector spaces with its usual tensor product are important and widely studied under the name of
coalgebras.
Terminological history
The notion of monad was invented by
Roger Godement in 1958 under the name "standard construction". Monad has been called "dual standard construction", "triple", "monoid" and "triad". The term "monad" is used at latest 1967, by
Jean Bénabou.
Examples
Identity
The
identity functor on a category
is a monad. Its multiplication and unit are the
identity function on the objects of
.
Monads arising from adjunctions
Any
adjunction
:
gives rise to a monad on ''C''. This very widespread construction works as follows: the endofunctor is the composite
:
This endofunctor is quickly seen to be a monad, where the unit map stems from the unit map
of the adjunction, and the multiplication map is constructed using the counit map of the adjunction:
:
In fact, any monad can be found as an explicit adjunction of functors using the
Eilenberg–Moore category (the category of
-algebras).
Double dualization
The ''double dualization monad'', for a fixed
field ''k'' arises from the adjunction
:
where both functors are given by sending a
vector space
In mathematics and physics, a vector space (also called a linear space) is a set (mathematics), set whose elements, often called vector (mathematics and physics), ''vectors'', can be added together and multiplied ("scaled") by numbers called sc ...
''V'' to its
dual vector space . The associated monad sends a vector space ''V'' to its
double dual . This monad is discussed, in much greater generality, by .
Closure operators on partially ordered sets
For categories arising from
partially ordered sets
(with a single morphism from
to
if and only if
In logic and related fields such as mathematics and philosophy, "if and only if" (often shortened as "iff") is paraphrased by the biconditional, a logical connective between statements. The biconditional is true in two cases, where either bo ...
), then the formalism becomes much simpler: adjoint pairs are
Galois connections and monads are
closure operators.
Free-forgetful adjunctions
For example, let
be the
forgetful functor
In mathematics, more specifically in the area of category theory, a forgetful functor (also known as a stripping functor) "forgets" or drops some or all of the input's structure or properties mapping to the output. For an algebraic structure of ...
from
the category Grp of
groups to the
category Set of sets, and let
be the
free group functor from the category of sets to the category of groups. Then
is left adjoint of
. In this case, the associated monad
takes a set
and returns the underlying set of the free group
.
The unit map of this monad is given by the maps
:
including any set
into the set
in the natural way, as strings of length 1. Further, the multiplication of this monad is the map
:
made out of a natural
concatenation
In formal language theory and computer programming, string concatenation is the operation of joining character strings end-to-end. For example, the concatenation of "snow" and "ball" is "snowball". In certain formalizations of concatenati ...
or 'flattening' of 'strings of strings'. This amounts to two
natural transformations.
The preceding example about free groups can be generalized to any type of algebra in the sense of a
variety of algebras in
universal algebra. Thus, every such type of algebra gives rise to a monad on the category of sets. Importantly, the algebra type can be recovered from the monad (as the category of Eilenberg–Moore algebras), so monads can also be seen as generalizing varieties of universal algebras.
Another monad arising from an adjunction is when
is the endofunctor on the category of vector spaces which maps a vector space
to its
tensor algebra , and which maps linear maps to their tensor product. We then have a natural transformation corresponding to the embedding of
into its
tensor algebra, and a natural transformation corresponding to the map from
to
obtained by simply expanding all tensor products.
Codensity monads
Under mild conditions, functors not admitting a left adjoint also give rise to a monad, the so-called
codensity monad. For example, the inclusion
:
does not admit a left adjoint. Its codensity monad is the monad on sets sending any set ''X'' to the set of
ultrafilters on ''X''. This and similar examples are discussed in .
Monads used in denotational semantics
The following monads over the category of sets are used in
denotational semantics of
imperative programming languages, and analogous constructions are used in functional programming.
The maybe monad
The endofunctor of the maybe or partiality monad adds a disjoint point:
:
:
The unit is given by the inclusion of a set
into
:
:
:
The multiplication maps elements of
to themselves, and the two disjoint points in
to the one in
.
In both functional programming and denotational semantics, the maybe monad models
partial computations, that is, computations that may fail.
The state monad
Given a set
, the endofunctor of the state monad maps each set
to the set of functions
. The component of the unit at
maps each element
to the function
:
:
The multiplication maps the function
to the function
:
:
In functional programming and denotational semantics, the state monad models
stateful computations.
The environment monad
Given a set
, the endofunctor of the reader or environment monad maps each set
to the set of functions
. Thus, the endofunctor of this monad is exactly the
hom functor
In mathematics, specifically in category theory, hom-sets (i.e. sets of morphisms between object (category theory), objects) give rise to important functors to the category of sets. These functors are called hom-functors and have numerous applicati ...
. The component of the unit at
maps each element
to the
constant function .
In functional programming and denotational semantics, the environment monad models computations with access to some read-only data.
The list and set monads
The list or nondeterminism monad maps a set ''X'' to the set of finite
sequences (i.e.,
lists) with elements from ''X''. The unit maps an element ''x'' in ''X'' to the singleton list
The multiplication concatenates a list of lists into a single list.
In functional programming, the list monad is used to model
nondeterministic computations. The covariant powerset monad is also known as the set monad, and is also used to model nondeterministic computation.
Algebras for a monad
Given a monad
on a category
, it is natural to consider ''
-algebras'', i.e., objects of
acted upon by
in a way which is compatible with the unit and multiplication of the monad. More formally, a
-algebra
is an object
of
together with an arrow
of
called the ''structure map'' of the algebra such that the diagrams
commute.
A morphism
of
-algebras is an arrow
of
such that the diagram commutes.
-algebras form a category called the ''Eilenberg–Moore category'' and denoted by
.
Examples
Algebras over the free group monad
For example, for the free group monad discussed above, a
-algebra is a set
together with a map from the free group generated by
towards
subject to associativity and unitality conditions. Such a structure is equivalent to saying that
is a group itself.
Algebras over the distribution monad
Another example is the ''distribution monad''
on the category of sets. It is defined by sending a set
to the set of functions