# User:Michiexile/MATH198/Lecture 7

(Difference between revisions)
 Revision as of 21:13, 2 November 2009 (edit)← Previous diff Revision as of 19:10, 3 November 2009 (edit) (undo)Next diff → Line 18: Line 18: ===Algebraic objects in categories=== ===Algebraic objects in categories=== - \$THEORY objects in Categories. + We recall the definition of a monoid: - Now, we call something a monad in a category if it is a monoid object + '''Definition''' A ''monoid'' is a set $M$ equipped with an operation $\mu: M\times M\to M$ that we call ''composition'' and an operation $e: 1\to M$ that we call the identity, such that - in the category of endofunctors of that category. + * $M \circ 1_M \times M = M \circ M \times 1_M$ (associativity) + * $M \circ 1_M \times e = M \circ e\times 1_M = 1_M$ (unity) + + We can move this definition out of the category of sets by defining a ''monoid object'' in a category: + + '''Definition''' A ''monoid object'' in a category $C$ with terminal object and pairwise products is an object $M$ equipped with morphisms $\mu: M\times M\to M$ and $e: 1\to M$ such that + * $M \circ 1_M \times M = M \circ M \times 1_M$ (associativity) + * $M \circ 1_M \times e = M \circ e\times 1_M = 1_M$ (unity) + + As an example, a monoid object in $Set$ is just a monoid. A monoid object in the category of abelian groups is a ring. + + And the composition $UF$ for an adjoint pair is a monoid object in the category of endofunctors on the category. + + The same kind of construction can be made translating familiar algebraic definitions into categorical constructions with many different groups of definitions. For ''groups'', the corresponding definition introduces a diagonal map $\Delta: G\to G\times G$, and an inversion map $i: M\to M$ to codify the entire definition. + + One framework that formalizes the whole thing, in such a way that the definitions themselves form a category is the theory of Sketches by Charles Wells. In one formulation we get the following definition: + + '''Definition''' A ''sketch'' $S = (G, D, L, K)$ consists of a graph $G$, a set of diagrams $D$, a set $L$ of cones in $G$ and a set $K$ of cocones in $G$. + + A ''model'' of a sketch ''S'' in a category $C$ is a graph homomorphism $G\to C$ such that the image of each diagram in $D$ is commutative, each of the cones is a limit cone and each of the cocones is a colimit cocone. + + A ''homomorphism of models'' is just a natural transformation between the models. ---- ---- - We thus define a ''monad in a category $C$'' to be a monoid in that category. + We thus define a ''monad in a category $C$'' to be a monoid object in the category of endofunctors on that category. + + We can take this definition and write it out in Haskell code, as: + + class Functor m => MathematicalMonad m where + return :: a -> m a + join :: m (m a) -> m a + + -- such that + join . fmap return = id :: m a -> m a + join . return = id :: m a -> m a + join . join = join . fmap join :: m (m (m a)) -> m a + + + Those of you used to Haskell will notice that this is not the same as the Monad typeclass. That type class calls for a natural transformation (>>=) :: m a -> (a -> m b) -> m b (or bind). + + The secret of the connection between the two lies in the Kleisli category, and a way to build adjunctions out of monads as well as monads out of adjunctions. + + ===Kleisli category=== + + We know that an adjoint pair will give us a monad. But what about getting an adjoint pair out of a monad? Can we reverse the process that got us the monad in the first place? + + There are several different ways to do this. Awodey uses the ''Eilenberg-Moore category'' which has as objects the ''algebras of the monad $T$'': morphisms $h: Tx \to x$. A morphism $f: (\alpha: TA\to A)\to (\beta: TB\to B)$ is just some morphism $f:A\to B$ in the category $C$ such that $f\circ\alpha = \beta\circ T(f)$. + + We require of $T$-algebras two additional conditions: + * $1_A = h\circ\eta_A$ (unity) + * $h\circ\mu_A = h \circ Th$ (associativity) + + There is a forgetful functor that takes some $h$ to $t(h)$, picking up the object of the $T$-algebra. Thus $U(h:TA\to A) = A$, and $U(f) = f$. + + We shall construct a left adjoint $F$ to this from the data of the monad $T$ by setting $FC = (\mu_C: T^2C\to TC)$, making $TC$ the corresponding object. And plugging the corresponding data into the equations, we get: + * $1_{TC} = \mu_C\circ\eta_{TC}$ + * $\mu_C\circ\mu_{TC} = \mu_C\circ T\mu_C$ + which we recognize as the axioms of unity and associativity for the monad. + + (diagrams both here and above?) + + By working through the details of proving this to be an adjunction, and examining the resulting composition, it becomes clear that this is in fact the original monad $T$. However - while the Eilenberg-Moore construction is highly enlightening for constructing formal systems for algebraic theories, and even for the fixpoint definitions of data types, it is less enlightening to understand Haskell's monad definition. + + To get to terms with the Haskell approach, we instead look to a different construction aiming to fulfill the same aim: the ''Kleisli category'': + + Given a monad $T$ over a category $C$, equipped with unit $\eta$ and concatenation $\mu$, we shall construct a new category $K(T)$, and an adjoint pair of functors $U,F$ factorizing the monad into $T=UF$. + + We first define $K(T)_0 = C_0$, keeping the objects from the original category. + + Then, we set $K(T)_1$ to be the collection of arrows, in $C$, on the form $A\to TB$. + + The composition of $f: A\to TB$ with $g: B\to TC$ is given by the sequence + $A\to^f TB\to^{Tg} T^2C\to^{\mu_C} TC$ + + The identity is the arrow $\eta_A: A\to TA$. The identity property follows directly from the unity axiom for the monad, since $\eta_A$ composing with $\mu_A$ is the identity. + + Given this category, we next define the functors: + * $U(A) = TA$ + * $U(f: A\to TB) = TA\to^{Tf} T^2B\to^{\mu_B} TB$ + + * $F(A) = A$ + * $F(g: A\to B) = A\to^{\eta_A} TA\to^{Tg} TB$ + + This definition makes $U, F$ an adjoint pair. Furthermore, we get + * $UF(A) = U(A) = TA$ + * $UF(g: A\to B) = U(Tg\circ\eta_A) = \mu_B\circ T(Tg\circ\eta_A)$ + : $=\mu_B\circ T^2g \circ T\eta_A$, and by naturality of $\mu$, we can rewrite this as + : $=Tg \circ \mu_A\circ T\eta_A = Tg\circ 1_{TA} = Tg$ by unitality of $\eta$. + + Hence, the composite $UF$ really is just the original monad functor $T$. + + But what's the big deal with this? you may ask. The big deal is that we now have a monad specification with a different signature. + + ((TALK TO VAUGHAN HERE!!)) + + + + + - Thus: a monad with this definition in Haskell is: - * a type of kind m :: * -> *. - * equipped with functions - ** return :: a -> m a - ** join :: m m a -> m a - * examples: List: List: Line 62: Line 151: + ===Homework=== - + # Prove that the Kleisli category adjunction is an adjunction. - + # Prove that the Eilenberg-Moore category adjunction is an adjunction. + # The ''writer'' monad W is defined by + #* data Monoid m => W m x = W (x, m) + #* fmap f (W (x, m)) = W (f x, m) + #* return x = W (x, mempty) + #* join (W (W (x, m), n)) = W (x, m mappend n) + ## (2pt) Prove that this yields a monad. + ## (2pt) Give the Kleisli factorization of the writer monad. + ## (2pt) Give the Eilenberg-Moore factorization of the writer monad.

## Revision as of 19:10, 3 November 2009

IMPORTANT NOTE: THESE NOTES ARE STILL UNDER DEVELOPMENT. PLEASE WAIT UNTIL AFTER THE LECTURE WITH HANDING ANYTHING IN, OR TREATING THE NOTES AS READY TO READ.

Last week we saw what an adjunction was. Here's one thing we can do with adjunctions.

Now, let U be a left adjoint to F. We set T = UF. Then we have natural transformations

μ:UFUF − > UF μX = UεFX

ι:1 − > UF ιX = ηX

such that μ is associative and ι is the unit of μ.

These requirements remind us of the definition of a monoid - and this is not that much of a surprise. To see the exact connection, and to garner a wider spread of definitions.

## Contents

### 1 Algebraic objects in categories

We recall the definition of a monoid:

Definition A monoid is a set M equipped with an operation $\mu: M\times M\to M$ that we call composition and an operation $e: 1\to M$ that we call the identity, such that

• $M \circ 1_M \times M = M \circ M \times 1_M$ (associativity)
• $M \circ 1_M \times e = M \circ e\times 1_M = 1_M$ (unity)

We can move this definition out of the category of sets by defining a monoid object in a category:

Definition A monoid object in a category C with terminal object and pairwise products is an object M equipped with morphisms $\mu: M\times M\to M$ and $e: 1\to M$ such that

• $M \circ 1_M \times M = M \circ M \times 1_M$ (associativity)
• $M \circ 1_M \times e = M \circ e\times 1_M = 1_M$ (unity)

As an example, a monoid object in Set is just a monoid. A monoid object in the category of abelian groups is a ring.

And the composition UF for an adjoint pair is a monoid object in the category of endofunctors on the category.

The same kind of construction can be made translating familiar algebraic definitions into categorical constructions with many different groups of definitions. For groups, the corresponding definition introduces a diagonal map $\Delta: G\to G\times G$, and an inversion map $i: M\to M$ to codify the entire definition.

One framework that formalizes the whole thing, in such a way that the definitions themselves form a category is the theory of Sketches by Charles Wells. In one formulation we get the following definition:

Definition A sketch S = (G,D,L,K) consists of a graph G, a set of diagrams D, a set L of cones in G and a set K of cocones in G.

A model of a sketch S in a category C is a graph homomorphism $G\to C$ such that the image of each diagram in D is commutative, each of the cones is a limit cone and each of the cocones is a colimit cocone.

A homomorphism of models is just a natural transformation between the models.

We thus define a monad in a category C to be a monoid object in the category of endofunctors on that category.

We can take this definition and write it out in Haskell code, as:

class Functor m => MathematicalMonad m where
return :: a -> m a
join   :: m (m a) -> m a

-- such that
join . fmap return = id               :: m a -> m a
join . return      = id               :: m a -> m a
join . join        = join . fmap join :: m (m (m a)) -> m a
Those of you used to Haskell will notice that this is not the same as the
typeclass. That type class calls for a natural transformation
(>>=) :: m a -> (a -> m b) -> m b
(or
bind
).

The secret of the connection between the two lies in the Kleisli category, and a way to build adjunctions out of monads as well as monads out of adjunctions.

### 2 Kleisli category

We know that an adjoint pair will give us a monad. But what about getting an adjoint pair out of a monad? Can we reverse the process that got us the monad in the first place?

There are several different ways to do this. Awodey uses the Eilenberg-Moore category which has as objects the algebras of the monad T: morphisms $h: Tx \to x$. A morphism $f: (\alpha: TA\to A)\to (\beta: TB\to B)$ is just some morphism $f:A\to B$ in the category C such that $f\circ\alpha = \beta\circ T(f)$.

We require of T-algebras two additional conditions:

• $1_A = h\circ\eta_A$ (unity)
• $h\circ\mu_A = h \circ Th$ (associativity)

There is a forgetful functor that takes some h to t(h), picking up the object of the T-algebra. Thus $U(h:TA\to A) = A$, and U(f) = f.

We shall construct a left adjoint F to this from the data of the monad T by setting $FC = (\mu_C: T^2C\to TC)$, making TC the corresponding object. And plugging the corresponding data into the equations, we get:

• $1_{TC} = \mu_C\circ\eta_{TC}$
• $\mu_C\circ\mu_{TC} = \mu_C\circ T\mu_C$

which we recognize as the axioms of unity and associativity for the monad.

(diagrams both here and above?)

By working through the details of proving this to be an adjunction, and examining the resulting composition, it becomes clear that this is in fact the original monad T. However - while the Eilenberg-Moore construction is highly enlightening for constructing formal systems for algebraic theories, and even for the fixpoint definitions of data types, it is less enlightening to understand Haskell's monad definition.

To get to terms with the Haskell approach, we instead look to a different construction aiming to fulfill the same aim: the Kleisli category:

Given a monad T over a category C, equipped with unit η and concatenation μ, we shall construct a new category K(T), and an adjoint pair of functors U,F factorizing the monad into T = UF.

We first define K(T)0 = C0, keeping the objects from the original category.

Then, we set K(T)1 to be the collection of arrows, in C, on the form $A\to TB$.

The composition of $f: A\to TB$ with $g: B\to TC$ is given by the sequence $A\to^f TB\to^{Tg} T^2C\to^{\mu_C} TC$

The identity is the arrow $\eta_A: A\to TA$. The identity property follows directly from the unity axiom for the monad, since ηA composing with μA is the identity.

Given this category, we next define the functors:

• U(A) = TA
• $U(f: A\to TB) = TA\to^{Tf} T^2B\to^{\mu_B} TB$
• F(A) = A
• $F(g: A\to B) = A\to^{\eta_A} TA\to^{Tg} TB$

This definition makes U,F an adjoint pair. Furthermore, we get

• UF(A) = U(A) = TA
• $UF(g: A\to B) = U(Tg\circ\eta_A) = \mu_B\circ T(Tg\circ\eta_A)$
$=\mu_B\circ T^2g \circ T\eta_A$, and by naturality of μ, we can rewrite this as
$=Tg \circ \mu_A\circ T\eta_A = Tg\circ 1_{TA} = Tg$ by unitality of η.

Hence, the composite UF really is just the original monad functor T.

But what's the big deal with this? you may ask. The big deal is that we now have a monad specification with a different signature.

((TALK TO VAUGHAN HERE!!))

List:
return x = [x]
join (l:lsts) = l ++ join lsts

Maybe:
return x = Just x
join (Just (Just x)) = Just x
join _ = Nothing

Note: not quite what Haskell claims a monad to be. Other related concepts:

• Kleisli category & factorization of monads: if we start with T a

monad, then can we find an adjunction U -| F to _somewhere_ such that T = UF? And the monoidal structure is given by U e_FX and eta_X?

• From the Kleisli Category to monadic bind.
• Monads and sequencing.

### 3 Homework

1. Prove that the Kleisli category adjunction is an adjunction.
2. Prove that the Eilenberg-Moore category adjunction is an adjunction.
3. The writer monad
W
is defined by
• data Monoid m => W m x = W (x, m)
• fmap f (W (x, m)) = W (f x, m)
#* <hask>return x = W (x, mempty)
• join (W (W (x, m), n)) = W (x, m mappend n)
1. (2pt) Prove that this yields a monad.
2. (2pt) Give the Kleisli factorization of the writer monad.
3. (2pt) Give the Eilenberg-Moore factorization of the writer monad.

### 4 Some adjunctions we already know

• initial/terminal are adjunctions.
• (co)-products are adjunctions.
• Actually, all (co)limits are adjunctions.

### 5 Some adjunctions we don't know yet

• Existential and universal qualifiers as adjunctions.
• Powersets and im(f) -| f^\inv

### 6 Properties of adjoints

#### 6.2 Recognizing adjoints

Theorem (Freyd: The Adjoint Functor Theorem)