Template:Short description Template:Distinguish Template:For
In category theory, a branch of mathematics, a monad is a triple <math>(T, \eta, \mu)</math> consisting of a functor T from a category to itself and two natural transformations <math>\eta, \mu</math> that satisfy the conditions like associativity. For example, if <math>F, G</math> are functors adjoint to each other, then <math>T = G \circ F</math> together with <math>\eta, \mu</math> 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 mapping a category to itself). According to John Baez, a monad can be considered at least in two ways: <ref name="Baez"> https://golem.ph.utexas.edu/category/2009/07/the_monads_hurt_my_head_but_no.html</ref>
- 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.<ref>Template:Citation</ref>
Introduction and definitionEdit
A monad is a certain type of endofunctor. For example, if <math>F</math> and <math>G</math> are a pair of adjoint functors, with <math>F</math> left adjoint to <math>G</math>, then the composition <math>G \circ F</math> is a monad. If <math>F</math> and <math>G</math> 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 <math>F \circ G</math>, is discussed under the dual theory of comonads.
Formal definitionEdit
Throughout this article, <math>C</math> denotes a category. A monad on <math>C</math> consists of an endofunctor <math>T \colon C \to C</math> together with two natural transformations: <math>\eta \colon 1_{C} \to T</math> (where <math>1_{C}</math> denotes the identity functor on <math>C</math>) and <math>\mu \colon T^{2} \to T</math> (where <math>T^{2}</math> is the functor <math>T \circ T</math> from <math>C</math> to <math>C</math>). These are required to fulfill the following conditions (sometimes called coherence conditions):
- <math>\mu \circ T\mu = \mu \circ \mu T</math> (as natural transformations <math>T^{3} \to T</math>); here <math>T\mu</math> and <math>\mu T</math> are formed by "horizontal composition".
- <math>\mu \circ T \eta = \mu \circ \eta T = 1_{T}</math> (as natural transformations <math>T \to T</math>; here <math>1_{T}</math> denotes the identity transformation from <math>T</math> to <math>T</math>).
We can rewrite these conditions using the following commutative diagrams:
Template:Spaces |
See the article on natural transformations for the explanation of the notations <math>T\mu</math> and <math>\mu T</math>, or see below the commutative diagrams not using these notions:
File:Monad multiplication explicit.svg | Template:Spaces | File:Monad unit explicit.svg |
The first axiom is akin to the associativity in monoids if we think of <math>\mu</math> as the monoid's binary operation, and the second axiom is akin to the existence of an identity element (which we think of as given by <math>\eta</math>). Indeed, a monad on <math>C</math> can alternatively be defined as a monoid in the category <math>\mathbf{End}_{C}</math> whose objects are the endofunctors of <math>C</math> and whose morphisms are the natural transformations between them, with the monoidal structure induced by the composition of endofunctors.
The power set monadEdit
The power set monad is a monad <math>\mathcal{P}</math> on the category <math>\mathbf{Set}</math>: For a set <math>A</math> let <math>T(A)</math> be the power set of <math>A</math> and for a function <math>f \colon A \to B</math> let <math>T(f)</math> be the function between the power sets induced by taking direct images under <math>f</math>. For every set <math>A</math>, we have a map <math>\eta_{A} \colon A \to T(A)</math>, which assigns to every <math>a\in A</math> the singleton <math>\{a\}</math>. The function
- <math>\mu_{A} \colon T(T(A)) \to T(A)</math>
takes a set of sets to its union. These data describe a monad.
RemarksEdit
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 <math>\operatorname{End}(C)</math>, 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 <math>\mathcal{P} \circ \mathcal{P}</math> does not admit any monad structure.<ref>Template:Citation</ref>
ComonadsEdit
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 <math>C</math> is a monad for the opposite category <math>C^{\mathrm{op}}</math>. It is therefore a functor <math>U</math> from <math>C</math> 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 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 historyEdit
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".Template:Sfn The term "monad" is used at latest 1967, by Jean Bénabou.<ref>Template:Cite book</ref><ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>
ExamplesEdit
IdentityEdit
The identity functor on a category <math>C</math> is a monad. Its multiplication and unit are the identity function on the objects of <math>C</math>.
Monads arising from adjunctionsEdit
Any adjunction
- <math>F: C \rightleftarrows D : G</math>
gives rise to a monad on C. This very widespread construction works as follows: the endofunctor is the composite
- <math>T = G \circ F.</math>
This endofunctor is quickly seen to be a monad, where the unit map stems from the unit map <math>\operatorname{id}_C \to G \circ F</math> of the adjunction, and the multiplication map is constructed using the counit map of the adjunction:
- <math>T^2 = G \circ F \circ G \circ F \xrightarrow{G \circ \text{counit} \circ F} G \circ F = T.</math>
In fact, any monad can be found as an explicit adjunction of functors using the Eilenberg–Moore category <math>C^T</math> (the category of <math>T</math>-algebras).<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>
Double dualizationEdit
The double dualization monad, for a fixed field k arises from the adjunction
- <math>(-)^* : \mathbf{Vect}_k \rightleftarrows \mathbf{Vect}_k^{op} : (-)^*</math>
where both functors are given by sending a vector space V to its dual vector space <math>V^* := \operatorname{Hom}(V, k)</math>. The associated monad sends a vector space V to its double dual <math>V^{**}</math>. This monad is discussed, in much greater generality, by Template:Harvtxt.
Closure operators on partially ordered setsEdit
For categories arising from partially ordered sets <math>(P, \le)</math> (with a single morphism from <math>x</math> to <math>y</math> if and only if <math>x \le y</math>), then the formalism becomes much simpler: adjoint pairs are Galois connections and monads are closure operators.
Free-forgetful adjunctionsEdit
For example, let <math>G</math> be the forgetful functor from the category Grp of groups to the category Set of sets, and let <math>F</math> be the free group functor from the category of sets to the category of groups. Then <math>F</math> is left adjoint of <math>G</math>. In this case, the associated monad <math>T = G \circ F</math> takes a set <math>X</math> and returns the underlying set of the free group <math>\mathrm{Free}(X)</math>. The unit map of this monad is given by the maps
- <math>X \to T(X) </math>
including any set <math>X</math> into the set <math>\mathrm{Free}(X)</math> in the natural way, as strings of length 1. Further, the multiplication of this monad is the map
- <math>T(T(X)) \to T(X) </math>
made out of a natural concatenation 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 <math>T</math> is the endofunctor on the category of vector spaces which maps a vector space <math>V</math> to its tensor algebra <math>T(V)</math>, and which maps linear maps to their tensor product. We then have a natural transformation corresponding to the embedding of <math>V</math> into its tensor algebra, and a natural transformation corresponding to the map from <math>T(T(V))</math> to <math>T(V)</math> obtained by simply expanding all tensor products.
Codensity monadsEdit
Under mild conditions, functors not admitting a left adjoint also give rise to a monad, the so-called codensity monad. For example, the inclusion
- <math>\mathbf{FinSet} \subset \mathbf{Set}</math>
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 Template:Harvtxt.
Monads used in denotational semanticsEdit
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 monadEdit
The endofunctor of the maybe or partiality monad adds a disjoint point:Template:Sfn
- <math>(-)_{*}: \mathbf{Set}\to\mathbf{Set}</math>
- <math>X\mapsto X\cup\{*\}</math>
The unit is given by the inclusion of a set <math>X</math> into <math>X_*</math>:
- <math>\eta_X: X\to X_{*}</math>
- <math>x\mapsto x</math>
The multiplication maps elements of <math>X</math> to themselves, and the two disjoint points in <math>(X_{*})_{*}</math> to the one in <math>X_{*}</math>.
In both functional programming and denotational semantics, the maybe monad models partial computations, that is, computations that may fail.
The state monadEdit
Given a set <math>S</math>, the endofunctor of the state monad maps each set <math>X</math> to the set of functions <math>S\to S \times X</math>. The component of the unit at <math>X</math> maps each element <math>x\in X</math> to the function
- <math>\eta_X(x): S\to S\times X</math>
- <math>s\mapsto (s, x)</math>
The multiplication maps the function <math>f: S\to S\times (S\to S\times X), s\mapsto (s',f')</math> to the function
- <math>\mu_X(f): S\to S\times X</math>
- <math>s\mapsto f'(s')</math>
In functional programming and denotational semantics, the state monad models stateful computations.
The environment monadEdit
Given a set <math>E</math>, the endofunctor of the reader or environment monad maps each set <math>X</math> to the set of functions <math>E\to X</math>. Thus, the endofunctor of this monad is exactly the hom functor <math>\mathrm{Hom}(E, -)</math>. The component of the unit at <math>X</math> maps each element <math>x\in X</math> to the constant function <math>e\mapsto x</math>.
In functional programming and denotational semantics, the environment monad models computations with access to some read-only data.
The list and set monadsEdit
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 [x]. 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 monadEdit
Given a monad <math>(T,\eta,\mu)</math> on a category <math>C</math>, it is natural to consider <math>T</math>-algebras, i.e., objects of <math>C</math> acted upon by <math>T</math> in a way which is compatible with the unit and multiplication of the monad. More formally, a <math>T</math>-algebra <math>(x,h)</math> is an object <math>x</math> of <math>C</math> together with an arrow <math>h\colon Tx\to x</math> of <math>C</math> called the structure map of the algebra such that the diagrams
File:Monad multi algebra.svg | and | File:Monad unit algebra.svg |
commute.
A morphism <math>f\colon (x,h)\to(x',h')</math> of <math>T</math>-algebras is an arrow <math>f\colon x\to x'</math> of <math>C</math> such that the diagram
commutes. <math>T</math>-algebras form a category called the Eilenberg–Moore category and denoted by <math>C^T</math>.
ExamplesEdit
Algebras over the free group monadEdit
For example, for the free group monad discussed above, a <math>T</math>-algebra is a set <math>X</math> together with a map from the free group generated by <math>X</math> towards <math>X</math> subject to associativity and unitality conditions. Such a structure is equivalent to saying that <math>X</math> is a group itself.
Algebras over the distribution monadEdit
Another example is the distribution monad <math>\mathcal{D}</math> on the category of sets. It is defined by sending a set <math>X</math> to the set of functions <math>f : X \to [0,1]</math> with finite support and such that their sum is equal to <math>1</math>. In set-builder notation, this is the set<math display="block">\mathcal{D}(X) = \left\{ f: X \to [0,1] : \begin{matrix} \#\text{supp}(f) < +\infty \\ \sum_{x \in X} f(x) = 1 \end{matrix} \right\}</math>By inspection of the definitions, it can be shown that algebras over the distribution monad are equivalent to convex sets, i.e., sets equipped with operations <math>x +_r y</math> for <math>r \in [0,1]</math> subject to axioms resembling the behavior of convex linear combinations <math>rx + (1-r)y</math> in Euclidean space.<ref>Template:Citation,
Template:Citation</ref>
Algebras over the symmetric monadEdit
Another useful example of a monad is the symmetric algebra functor on the category of <math>R</math>-modules for a commutative ring <math>R</math>.<math display="block">\text{Sym}^\bullet(-): \text{Mod}(R) \to \text{Mod}(R)</math>sending an <math>R</math>-module <math>M</math> to the direct sum of symmetric tensor powers<math display="block">\text{Sym}^\bullet(M) = \bigoplus_{k=0}^\infty \text{Sym}^k(M)</math>where <math>\text{Sym}^0(M) = R</math>. For example, <math>\text{Sym}^\bullet(R^{\oplus n}) \cong R[x_1,\ldots, x_n]</math> where the <math>R</math>-algebra on the right is considered as a module. Then, an algebra over this monad are commutative <math>R</math>-algebras. There are also algebras over the monads for the alternating tensors <math>\text{Alt}^\bullet(-)</math> and total tensor functors <math>T^\bullet(-)</math> giving anti-symmetric <math>R</math>-algebras, and free <math>R</math>-algebras, so<math display="block">\begin{align} \text{Alt}^\bullet(R^{\oplus n}) &= R(x_1,\ldots, x_n)\\ \text{T}^\bullet(R^{\oplus n}) &= R\langle x_1,\ldots, x_n \rangle \end{align}</math>where the first ring is the free anti-symmetric algebra over <math>R</math> in <math>n</math>-generators and the second ring is the free algebra over <math>R</math> in <math>n</math>-generators.
Commutative algebras in E-infinity ring spectraEdit
There is an analogous construction for commutative <math>\mathbb{S}</math>-algebras<ref>Template:Cite journal</ref>pg 113 which gives commutative <math>A</math>-algebras for a commutative <math>\mathbb{S}</math>-algebra <math>A</math>. If <math>\mathcal{M}_A</math> is the category of <math>A</math>-modules, then the functor <math>\mathbb{P}: \mathcal{M}_A \to \mathcal{M}_A</math> is the monad given by<math display="block">\mathbb{P}(M) = \bigvee_{j \geq 0} M^j/\Sigma_j</math>where<math display="block">M^j = M\wedge_A \cdots \wedge_A M</math> <math>j</math>-times. Then there is an associated category <math>\mathcal{C}_A</math> of commutative <math>A</math>-algebras from the category of algebras over this monad.
Monads and adjunctionsEdit
As was mentioned above, any adjunction gives rise to a monad. Conversely, every monad arises from some adjunction, namely the free–forgetful adjunction
- <math>T(-) : C \rightleftarrows C^T : \text{forget}</math>
whose left adjoint sends an object X to the free T-algebra T(X). However, there are usually several distinct adjunctions giving rise to a monad: let <math>\mathbf{Adj}(C,T)</math> be the category whose objects are the adjunctions <math>(F,G,e,\varepsilon)</math> such that <math>(GF, e, G\varepsilon F)=(T,\eta,\mu)</math> and whose arrows are the morphisms of adjunctions that are the identity on <math>C</math>. Then the above free–forgetful adjunction involving the Eilenberg–Moore category <math>C^T</math> is a terminal object in <math>\mathbf{Adj}(C,T)</math>. An initial object is the Kleisli category, which is by definition the full subcategory of <math>C^T</math> consisting only of free T-algebras, i.e., T-algebras of the form <math>T(x)</math> for some object x of C.
Monadic adjunctionsEdit
Given any adjunction <math>(F : C \to D,G : D \to C,\eta,\varepsilon)</math> with associated monad T, the functor G can be factored as
- <math>D \overset{\widetilde{G}}\longrightarrow C^T \xrightarrow{\text{forget}} C,</math>
i.e., G(Y) can be naturally endowed with a T-algebra structure for any Y in D. The adjunction is called a monadic adjunction if the first functor <math>\tilde G</math> yields an equivalence of categories between D and the Eilenberg–Moore category <math>C^T</math>.<ref>Template:Harvtxt uses a stronger definition, where the two categories are isomorphic rather than equivalent.</ref> By extension, a functor <math>G\colon D\to C</math> is said to be monadic if it has a left adjoint Template:Mvar forming a monadic adjunction. For example, the free–forgetful adjunction between groups and sets is monadic, since algebras over the associated monad are groups, as was mentioned above. In general, knowing that an adjunction is monadic allows one to reconstruct objects in D out of objects in C and the T-action.
Beck's monadicity theoremEdit
Beck's monadicity theorem gives a necessary and sufficient condition for an adjunction to be monadic. A simplified version of this theorem states that G is monadic if it is conservative (or G reflects isomorphisms, i.e., a morphism in D is an isomorphism if and only if its image under G is an isomorphism in C) and C has and G preserves coequalizers.
For example, the forgetful functor from the category of compact Hausdorff spaces to sets is monadic. However the forgetful functor from all topological spaces to sets is not conservative since there are continuous bijective maps (between non-compact or non-Hausdorff spaces) that fail to be homeomorphisms. Thus, this forgetful functor is not monadic.<ref>Template:Harvtxt</ref> The dual version of Beck's theorem, characterizing comonadic adjunctions, is relevant in different fields such as topos theory and topics in algebraic geometry related to descent. A first example of a comonadic adjunction is the adjunction
- <math>- \otimes_A B : \mathbf{Mod}_A \rightleftarrows \mathbf{Mod}_B : \operatorname{forget}</math>
for a ring homomorphism <math>A \to B</math> between commutative rings. This adjunction is comonadic, by Beck's theorem, if and only if B is faithfully flat as an A-module. It thus allows to descend B-modules, equipped with a descent datum (i.e., an action of the comonad given by the adjunction) to A-modules. The resulting theory of faithfully flat descent is widely applied in algebraic geometry.
UsesEdit
Monads are used in functional programming to express types of sequential computation (sometimes with side-effects). See monads in functional programming, and the more mathematically oriented Wikibook module b:Haskell/Category theory.
Monads are used in the denotational semantics of impure functional and imperative programming languages.<ref>Template:Cite book "The concept of a monad, which arises from category theory, has been applied by Moggi to structure the denotational semantics of programming languages."</ref><ref>Template:Cite journal</ref>
In categorical logic, an analogy has been drawn between the monad-comonad theory, and modal logic via closure operators, interior algebras, and their relation to models of S4 and intuitionistic logics.
GeneralizationEdit
It is possible to define monads in a 2-category <math>C</math>. Monads described above are monads for <math>C = \mathbf{Cat}</math>.
See alsoEdit
- Distributive law between monads
- Lawvere theory
- Monad (functional programming)
- Polyad
- Strong monad
- Giry monad
- Monoidal monad
ReferencesEdit
Further readingEdit
- Template:Citation
- Template:Citation
- Template:Citation
- Template:Citation
- Template:Citation
- Template:Cite book
- Template:Citation
- Template:Citation
- Template:Citation
- https://mathoverflow.net/questions/55182/what-is-known-about-the-category-of-monads-on-set
- Ross Street, The formal theory of monads [1]
External linksEdit
- Monads, a YouTube video of five short lectures (with one appendix).
- John Baez's This Week's Finds in Mathematical Physics (Week 89) covers monads in 2-categories.
- Monads and comonads, video tutorial.
- https://medium.com/@felix.kuehl/a-monad-is-just-a-monoid-in-the-category-of-endofunctors-lets-actually-unravel-this-f5d4b7dbe5d6