User:Michiexile/MATH198/Lecture 7
From HaskellWiki
Michiexile (Talk  contribs) 
Michiexile (Talk  contribs) 

(11 intermediate revisions by one user not shown)  
Line 1:  Line 1:  
−  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 <math>U</math> be a left adjoint to <math>F</math>. We set <math>T = UF</math>. Then we have natural transformations 

+  
+  :<math>\mu: UFUF > UF</math> 

+  :<math>\mu_X = U \epsilon_{FX}</math> 

+  
+  :<math>\iota: 1 > UF</math> 

+  :<math>\iota_X = \eta_X</math> 

+  
+  such that <math>\mu</math> is associative and <math>\iota</math> is the unit of <math>\mu</math>. 

+  
+  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. 

+  
+  ===Algebraic objects in categories=== 

+  
+  We recall the definition of a monoid: 

+  
+  '''Definition''' A ''monoid'' is a set <math>M</math> equipped with an operation <math>\mu: M\times M\to M</math> that we call ''composition'' and an operation <math>e: 1\to M</math> that we call the identity, such that 

+  * <math>M \circ 1_M \times M = M \circ M \times 1_M</math> (associativity) 

+  * <math>M \circ 1_M \times e = M \circ e\times 1_M = 1_M</math> (unity) 

+  
+  If we have a ''monoidal category''  a category <math>C</math> with a bifunctor <math>\otimes: C\times C\to C</math> called the ''tensor product'' which is associative (up to natural isomorphisms) and has an object <math>I</math> acting as a unit (up to natural isomorphisms) for the tensor product. 

+  
+  The product in a category certainly works as a tensor product, with a terminal object acting as a unit. However, there is often reason to have a noncommutative tensor product for the monoidal structure of a category. This makes the category a ''cartesian monoidal category''. 

+  
+  For, say, abelian groups, or for vector spaces, we have the ''tensor product'' forming a noncartesian monoidal category structure. And it is important that we do. 

+  
+  And for the category of endofunctors on a category, we have a monoidal structure induced by composition of endofunctors: <math>F\otimes G = F\circ G</math>. The unit is the identity functor. 

+  
+  Now, we can move the definition of a monoid out of the category of sets, and define a generic ''monoid object'' in a monoidal category: 

+  
+  '''Definition''' A ''monoid object'' in a monoidal category <math>C</math> is an object <math>M</math> equipped with morphisms <math>\mu: M\otimes M\to M</math> and <math>e: 1\to M</math> such that 

+  * <math>M \circ 1_M \otimes M = M \circ M\otimes 1_M</math> (associativity) 

+  * <math>M \circ 1_M \otimes e = M \circ e\otimes 1_M = 1_M</math> (unity) 

+  
+  As an example, a monoid object in the cartesian monoidal category <math>Set</math> is just a monoid. A monoid object in the category of abelian groups is a ring. 

+  
+  A monoid object in the category of abelian groups, with the tensor product for the monoidal structure is a ''ring''. 

+  
+  And the composition <math>UF</math> 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 <math>\Delta: G\to G\times G</math>, and an inversion map <math>i: M\to M</math> 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'' <math>S = (G, D, L, K)</math> consists of a graph <math>G</math>, a set of diagrams <math>D</math>, a set <math>L</math> of cones in <math>G</math> and a set <math>K</math> of cocones in <math>G</math>. 

+  
+  A ''model'' of a sketch ''S'' in a category <math>C</math> is a graph homomorphism <math>G\to C</math> such that the image of each diagram in <math>D</math> 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 <math>C</math>'' to be a monoid object in the category of endofunctors on that category. 

+  
+  Specifically, this means: 

+  
+  '''Definition''' A ''monad'' in a category <math>C</math> is an endofunctor <math>T: C\to C</math> equipped with natural transformations <math>\mu: T^2\to T</math> and <math>\eta: 1\to T</math> such that the following diagrams commute: 

+  
+  [[Image:MonadAssociative.png]] 

+  [[Image:MonadUnit.png]] 

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

+  <haskell> 

+  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 

+  </haskell> 

+  
+  Those of you used to Haskell will notice that this is not the same as the <hask>Monad</hask> typeclass. That type class calls for a natural transformation <hask>(>>=) :: m a > (a > m b) > m b</hask> (or <hask>bind</hask>). 

+  
+  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 ''EilenbergMoore category'' which has as objects the ''algebras of the monad <math>T</math>'': morphisms <math>h: Tx \to x</math>. A morphism <math>f: (\alpha: TA\to A)\to (\beta: TB\to B)</math> is just some morphism <math>f:A\to B</math> in the category <math>C</math> such that <math>f\circ\alpha = \beta\circ T(f)</math>. 

+  
+  We require of <math>T</math>algebras two additional conditions: 

+  * <math>1_A = h\circ\eta_A</math> (unity) 

+  * <math>h\circ\mu_A = h \circ Th</math> (associativity) 

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

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

+  * <math>1_{TC} = \mu_C\circ\eta_{TC}</math> 

+  * <math>\mu_C\circ\mu_{TC} = \mu_C\circ T\mu_C</math> 

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

+  
+  [[Image:MonadAssociative.png]] 

+  [[Image:MonadUnit.png]] 

+  
+  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 <math>T</math>. However  while the EilenbergMoore 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 <math>T</math> over a category <math>C</math>, equipped with unit <math>\eta</math> and concatenation <math>\mu</math>, we shall construct a new category <math>K(T)</math>, and an adjoint pair of functors <math>U,F</math> factorizing the monad into <math>T=UF</math>. 

+  
+  We first define <math>K(T)_0 = C_0</math>, keeping the objects from the original category. 

+  
+  Then, we set <math>K(T)_1</math> to be the collection of arrows, in <math>C</math>, on the form <math>A\to TB</math>. 

+  
+  The composition of <math>f: A\to TB</math> with <math>g: B\to TC</math> is given by the sequence 

+  <math>A\to^f TB\to^{Tg} T^2C\to^{\mu_C} TC</math> 

+  
+  The identity is the arrow <math>\eta_A: A\to TA</math>. The identity property follows directly from the unity axiom for the monad, since <math>\eta_A</math> composing with <math>\mu_A</math> is the identity. 

+  
+  Given this category, we next define the functors: 

+  * <math>U(A) = TA</math> 

+  * <math>U(f: A\to TB) = TA\to^{Tf} T^2B\to^{\mu_B} TB</math> 

+  
+  * <math>F(A) = A</math> 

+  * <math>F(g: A\to B) = A\to^{\eta_A} TA\to^{Tg} TB</math> 

+  
+  This definition makes <math>U, F</math> an adjoint pair. Furthermore, we get 

+  * <math>UF(A) = U(A) = TA</math> 

+  * <math>UF(g: A\to B) = U(Tg\circ\eta_A) = \mu_B\circ T(Tg\circ\eta_A)</math> 

+  : <math>=\mu_B\circ T^2g \circ T\eta_A</math>, and by naturality of <math>\mu</math>, we can rewrite this as 

+  : <math>=Tg \circ \mu_A\circ T\eta_A = Tg\circ 1_{TA} = Tg</math> by unitality of <math>\eta</math>. 

+  
+  We've really just chased through this commutative diagram: 

+  :[[Image:KleisliFactorizationDiagram.png]] 

+  
+  Hence, the composite <math>UF</math> really is just the original monad functor <math>T</math>. 

+  
+  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. Indeed, the Kleisli arrow for an arrow <hask>f :: a > b</hask> and a monad <hask>Monad m</hask> is something on the shape <hask>fk :: a > m b</hask>. And the Kleisli factorization tells us that the Haskell monad specification and the Haskell monad laws are equivalent to their categorical counterparts. 

+  
+  And the composition of Kleisli arrows is easy to write in Haskell: 

+  <haskell> 

+  f :: a > m b 

+  g :: b > m c 

+  
+  (>>=) :: m a > (a > m b) > m b  Monadic bind, the Haskell definition 

+  
+  kleisliCompose f g :: a > m c 

+  kleisliCompose f g = (>>= g) . f 

+  </haskell> 

+  
+  ===Examples=== 

+  
+  Some monads in Haskell are: 

+  
+  ====The List monad==== 

+  
+  Lists form a monad, with the following (redundant) definition: 

+  <haskell> 

+  instance Monad [] where 

+  return x = [x] 

+  
+  [] >>= _ = [] 

+  (x:xs) >>= f = f x : xs >>= f 

+  
+  join [] = [] 

+  join (l:ls) = l ++ join ls 

+  </haskell> 

+  
+  As it turns out, the lists monad can be found by considering the free and forgetful functors between sets and monoids. Indeed, the lists are what we get from the Kleene star operation, which is the monad we acquire by composing the free monoid functor with the forgetful functor. 

+  
+  ====Error handling==== 

+  
+  We can put a monadic structure on a coproduct <math>A+B</math> so that the monadic bind operation performs computations <math>A+B\to A'+B</math> until some computation fails, returning an error, typed <math>B</math>, after which we bypass any further computations, just carrying the error out of the entire computation. 

+  
+  The endofunctor here is <math>+B</math>. So the monad is given from a way to go from <math>A+B+B\to A+B</math>. Doing this is easy: in Haskell terms, we just remove the constructor differences between the two copies of <math>B</math> floating around. Mathematically, this is just using the functoriality of the coproduct construction on the inclusion maps into <math>A+B</math>. 

+  
+  For our example, we shall return the first value of <math>B</math> to ever occur, thus making our join operator look like this: 

+  <haskell> 

+  join :: (Either b (Either b a)) > Either b a 

+  join (Left y) = Left y 

+  join (Right (Left y)) = Left y 

+  join (Right (Right x)) = Right x 

+  </haskell> 

+  
+  This gives us a Haskell monad defined by: 

+  <haskell> 

+  instance Monad (Either b) where 

+  return x = Right x 

+  
+  Left y >>= _ = Left y 

+  Right x >>= f = f x 

+  </haskell> 

+  
+  
+  ===Additional reading=== 

+  
+  * http://blog.sigfpe.com/2006/08/youcouldhaveinventedmonadsand.html (one of the least dramatic monads tutorials out there) 

+  * http://www.disi.unige.it/person/MoggiE/ftp/lc88.ps.gz (Moggi: ''Computational lambdacalculus and monads'', one of the papers that started the interest in monads. Logic, dense reading.) 

+  * http://www.haskell.org/haskellwiki/Research_papers/Monads_and_arrows (good catalogue over further reading on monads) 

+  
+  ===Homework=== 

+  
+  Full marks will be given for 4 out of the 7 questions. 

+  
+  # Prove that the Kleisli category adjunction is an adjunction. 

+  # Prove that the EilenbergMoore category adjunction is an adjunction. 

+  # Given monad structures on <math>S</math> and <math>T</math>, 

+  # The ''writer'' monad <hask>W</hask> is defined by 

+  #* <hask>data Monoid m => W m x = W (x, m)</hask> 

+  #* <hask>fmap f (W (x, m)) = W (f x, m)</hask> 

+  #* <hask>return x = W (x, mempty)</hask> 

+  #* <hask>join (W (W (x, m), n)) = W (x, m `mappend` n)</hask> 

+  ## (2pt) Prove that this yields a monad. 

+  ## (2pt) Give the Kleisli factorization of the writer monad. 

+  ## (2pt) Give the EilenbergMoore factorization of the writer monad. 

+  ## (2pt) Is there a nice, 'natural' adjunction factorizing the writer monad? 
Latest revision as of 19:16, 4 November 2009
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 
[edit] 1 Algebraic objects in categories
We recall the definition of a monoid:
Definition A monoid is a set M equipped with an operation that we call composition and an operation that we call the identity, such that
 (associativity)
 (unity)
If we have a monoidal category  a category C with a bifunctor called the tensor product which is associative (up to natural isomorphisms) and has an object I acting as a unit (up to natural isomorphisms) for the tensor product.
The product in a category certainly works as a tensor product, with a terminal object acting as a unit. However, there is often reason to have a noncommutative tensor product for the monoidal structure of a category. This makes the category a cartesian monoidal category.
For, say, abelian groups, or for vector spaces, we have the tensor product forming a noncartesian monoidal category structure. And it is important that we do.
And for the category of endofunctors on a category, we have a monoidal structure induced by composition of endofunctors: . The unit is the identity functor.
Now, we can move the definition of a monoid out of the category of sets, and define a generic monoid object in a monoidal category:
Definition A monoid object in a monoidal category C is an object M equipped with morphisms and such that
 (associativity)
 (unity)
As an example, a monoid object in the cartesian monoidal category Set is just a monoid. A monoid object in the category of abelian groups is a ring.
A monoid object in the category of abelian groups, with the tensor product for the monoidal structure 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 , and an inversion map 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 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.
Specifically, this means:
Definition A monad in a category C is an endofunctor equipped with natural transformations and such that the following diagrams commute:
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
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.
[edit] 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 EilenbergMoore category which has as objects the algebras of the monad T: morphisms . A morphism is just some morphism in the category C such that .
We require of Talgebras two additional conditions:
 (unity)
 (associativity)
There is a forgetful functor that takes some h to t(h), picking up the object of the Talgebra. Thus , and U(f) = f.
We shall construct a left adjoint F to this from the data of the monad T by setting , making TC the corresponding object. And plugging the corresponding data into the equations, we get:
which we recognize as the axioms of unity and associativity for the monad.
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 EilenbergMoore 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} = 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 .
The composition of with is given by the sequence
The identity is the arrow . 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
 F(A) = A
This definition makes U,F an adjoint pair. Furthermore, we get
 UF(A) = U(A) = TA
 , and by naturality of μ, we can rewrite this as
 by unitality of η.
We've really just chased through this commutative diagram:
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. Indeed, the Kleisli arrow for an arrowAnd the composition of Kleisli arrows is easy to write in Haskell:
f :: a > m b g :: b > m c (>>=) :: m a > (a > m b) > m b  Monadic bind, the Haskell definition kleisliCompose f g :: a > m c kleisliCompose f g = (>>= g) . f
[edit] 3 Examples
Some monads in Haskell are:
[edit] 3.1 The List monad
Lists form a monad, with the following (redundant) definition:
instance Monad [] where return x = [x] [] >>= _ = [] (x:xs) >>= f = f x : xs >>= f join [] = [] join (l:ls) = l ++ join ls
As it turns out, the lists monad can be found by considering the free and forgetful functors between sets and monoids. Indeed, the lists are what we get from the Kleene star operation, which is the monad we acquire by composing the free monoid functor with the forgetful functor.
[edit] 3.2 Error handling
We can put a monadic structure on a coproduct A + B so that the monadic bind operation performs computations until some computation fails, returning an error, typed B, after which we bypass any further computations, just carrying the error out of the entire computation.
The endofunctor here is + B. So the monad is given from a way to go from . Doing this is easy: in Haskell terms, we just remove the constructor differences between the two copies of B floating around. Mathematically, this is just using the functoriality of the coproduct construction on the inclusion maps into A + B.
For our example, we shall return the first value of B to ever occur, thus making our join operator look like this:
join :: (Either b (Either b a)) > Either b a join (Left y) = Left y join (Right (Left y)) = Left y join (Right (Right x)) = Right x
This gives us a Haskell monad defined by:
instance Monad (Either b) where return x = Right x Left y >>= _ = Left y Right x >>= f = f x
[edit] 4 Additional reading
 http://blog.sigfpe.com/2006/08/youcouldhaveinventedmonadsand.html (one of the least dramatic monads tutorials out there)
 http://www.disi.unige.it/person/MoggiE/ftp/lc88.ps.gz (Moggi: Computational lambdacalculus and monads, one of the papers that started the interest in monads. Logic, dense reading.)
 http://www.haskell.org/haskellwiki/Research_papers/Monads_and_arrows (good catalogue over further reading on monads)
[edit] 5 Homework
Full marks will be given for 4 out of the 7 questions.
 Prove that the Kleisli category adjunction is an adjunction.
 Prove that the EilenbergMoore category adjunction is an adjunction.
 Given monad structures on S and T,
 The writer monad is defined byW
 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 EilenbergMoore factorization of the writer monad.
 (2pt) Is there a nice, 'natural' adjunction factorizing the writer monad?
