why a monad is a monoid in the category of endofunctors
why a monad is a monoid in the category of endofunctors
Agenda
- Category
- Functor
- Natural transformation
- Bifunctor
- Monoidal category
- Monoid
- Monad
- Why a monad is ...
Bartosz Milewski
Category
- objects
- morphisms (arrows)
Every object must have an identity morphism
↺
↺
↺
Morphisms must compose
"g after f"
Composition must be associative
Example of a category: Set
- objects are sets
- morphisms are functions between sets
Another example: Scala
- objects are types
- morphisms are Scala functions
Functor
- Maps every object A to an object F(A)
- Maps every morphism f: A -> B to a morphism F(f): F(A) -> F(B)
Category C
Category D
Functor
Preserves identity:
Preserves composition:
Endofunctor
A functor that maps from some category C
to the same category C
Category C
(endo)functors in Scala
Pretty much anything with a map method
String
Int
length
"hello"
Option[String]
Option[Int]
Some("hello")
map(length)
5
Some(5)
Functors form a category
You can make a category Fun(C, D) of
all the functors between two categories C and D
- objects are functors
- morphisms are natural transformations
Can also have a category of endofunctors, Fun(C, C)
(in category theory, pretty much everything turns out to be a category)
Natural transformation
Given functors F and G from a category C to a category D,
is a family of morphisms:
for each object x in C
such that
"eta"
Natural transformations in Scala
trait NatTrans[F[_], G[_]] {
def apply[A](fa: F[A]): G[A]
}
object OptToList extends NatTrans[Option, List] {
def apply[A](fa: Option[A]): List[A] = fa match {
case Some(a) => a :: Nil
case None => Nil
}
}
Recap
- Category = objects + morphisms
- every object has an identity morphism
- morphisms compose associatively
- Functor = mapping between categories
- Endofunctor = mapping from a category to itself
- Natural transformation = mapping between functors
- Functors form a category
Bifunctor
A functor that takes a pair,
i.e. it maps some product category C x D to some category E
Monoidal category
a.k.a tensor category
A category C with an associative bifunctor
"tensor product"
and an object
"identity object"
"unit object"
that is both a left and right identity for
Monoid
In a monoidal category
with tensor product
and identity object
a monoid consists of:
- an object
- a morphism
- a morphism
"myu"
"multiplication"
"eta"
"unit"
Monoids in Scala
trait Monoid[A] {
def unit: A
def multiply(a1: A, a2: A): A
}
object IntWithAddition extends Monoid[Int] {
def unit: Int = 0
def multiply(a1: Int, a2: Int): Int = a1 + a2
}
object IntWithMultiplication extends Monoid[Int] {
def unit: Int = 1
def multiply(a1: Int, a2: Int): Int = a1 * a2
}
Monad
A monad in a category C consists of:
- An endofunctor
- A natural transformation
- A natural transformation
"myu"
"join"
identity functor
"eta"
"return"
such that
(associativity law)
(left and right identity laws)
Monads in Scala
trait Monad[F[_]] extends Functor[F] {
def unit(a: A): F[A]
def join(ffa: F[F[A]]): F[A]
def flatMap(fa: F[A])(f: A => F[A]): F[A] =
join(fa.map(f))
}
object MonadForList extends Monad[List] {
def unit(a: A): List[A] = List(a)
def join(lists: List[List[A]]) = lists.flatten
}
Recap
- Bifunctor
- functor that takes 2 arguments
- Monoidal category
- category with a bifunctor
- and a unit object
- Monoid
- object in monoidal category
- multiplication morphism
- unit morphism
- Monad
- endofunctor
- natural transformation
- natural transformation
"myu"
"eta"
Fun[C,C]
Category of endofunctors for category C
- objects are endofunctors
- morphisms are natural transformations
It's a monoidal category!
- = functor composition ( )
- = identity functor ( )
Monoids in Fun[C,C]
What does a monoid look like in this category?
- object
- morphism
- morphism
Monoid in general
Monoid in Fun[C,C]
- endofunctor
- nat trans
- nat trans
Hang on, that looks like...
Monad
- endofunctor
- nat trans
- nat trans
Monoid in Fun[C,C]
- endofunctor
- nat trans
- nat trans
why a monad is a monoid in the category of endofunctors
By Chris Birchall
why a monad is a monoid in the category of endofunctors
- 3,063