# User:Michiexile/MATH198/Lecture 8

### From HaskellWiki

Michiexile (Talk | contribs) |
m |
||

(9 intermediate revisions by 2 users not shown) | |||

Line 1: | Line 1: | ||

− | + | ===Algebras over monads=== | |

− | = | + | We recall from the last lecture the definition of an Eilenberg-Moore algebra over a monad <math>T = (T, \eta, \mu)</math>: |

− | + | '''Definition''' An ''algebra'' over a monad <math>T</math> in a category <math>C</math> (a <math>T</math>-algebra) is a morphism <math>\alpha\in C(TA, A)</math>, such that the diagrams below both commute: | |

− | + | ||

− | + | ||

+ | [[Image:EilenbergMooreUnity.png]] | ||

+ | [[Image:EilenbergMooreAssociativity.png]] | ||

+ | While a monad corresponds to the imposition of some structure on the objects in a category, an algebra over that monad corresponds to some evaluation of that structure. | ||

− | === | + | ====Example: monoids==== |

− | + | Let <math>T</math> be the Kleene star monad - the one we get from the adjunction of free and forgetful functors between Monoids and Sets. Then a <math>T</math>-algebra on a set <math>A</math> is equivalent to a monoid structure on <math>A</math>. | |

− | + | ||

− | == | + | Indeed, if we have a monoid structure on <math>A</math>, given by <math>m:A^2\to A</math> and <math>u:1\to A</math>, we can construct a <math>T</math>-algebra by |

+ | :<math>\alpha([]) = u</math> | ||

+ | :<math>\alpha([a_1,a_2,\dots,a_n]) = m(a_1,\alpha([a_2,\dots,a_n]))</math> | ||

+ | This gives us, indeed, a <math>T</math>-algebra structure on <math>A</math>. Associativity and unity follows from the corresponding properties in the monoid. | ||

− | = | + | On the other hand, if we have a <math>T</math>-algebra structure on <math>A</math>, we can construct a monoid structure by setting |

+ | :<math>u = \alpha([])</math> | ||

+ | :<math>m(a,b) = \alpha([a,b])</math> | ||

+ | It is clear that associativity of <math>m</math> follows from the associativity of <math>\alpha</math>, and unitality of <math>u</math> follows from the unitality of <math>\alpha</math>. | ||

− | ==== | + | ====Example: Vector spaces==== |

− | + | We have free and forgetful functors | |

+ | :<math>Set \to^{free} k-Vect \to^{forgetful} Set</math> | ||

+ | forming an adjoint pair; where the free functor takes a set <math>S</math> and returns the vector space with basis <math>S</math>; while the forgetful functor takes a vector space and returns the set of all its elements. | ||

− | == | + | The composition of these yields a monad <math>T</math> in <math>Set</math> taking a set <math>S</math> to the set of all formal linear combinations of elements in <math>S</math>. The monad multiplication takes formal linear combinations of formal linear combinations and multiplies them out: |

+ | :<math>3(2v+5w)-5(3v+2w) = 6v+15w-15v-10w = -9v+5w</math> | ||

− | === | + | A <math>T</math>-algebra is a map <math>\alpha: TA\to A</math> that ''acts like a vector space'' in the sense that <math>\alpha(\sum\alpha_i(\sum\beta_jv_j)) = \alpha(\sum\alpha_i\beta_jv_j)</math>. |

+ | |||

+ | We can define <math>\lambda\cdot v = \alpha(\lambda v)</math> and <math>v+w=\alpha(v+w)</math>. The operations thus defined are associative, distributive, commutative, and everything else we could wish for in order to define a vector space - precisely because the operations inside <math>TA</math> are, and <math>\alpha</math> is associative. | ||

+ | |||

+ | ---- | ||

+ | |||

+ | The moral behind these examples is that using monads and monad algebras, we have significant power in defining and studying algebraic structures with categorical and algebraic tools. This paradigm ties in closely with the theory of ''operads'' - which has its origins in topology, but has come to good use within certain branches of universal algebra. | ||

+ | |||

+ | An (non-symmetric) ''operad'' is a graded set <math>O = \bigoplus_i O_i</math> equipped with composition operations <math>\circ_i: O_n\oplus O_m\to O_{n+m-1}</math> that obey certain unity and associativity conditions. As it turns out, non-symmetric operads correspond to the summands in a monad with polynomial underlying functor, and from a non-symmetric operad we can construct a corresponding monad. | ||

+ | |||

+ | The designator non-symmetric floats in this text o avoid dealing with the slightly more general theory of symmetric operads - which allow us to resort the input arguments, thus including the symmetrizer of a symmetric monoidal category in the entire definition. | ||

+ | |||

+ | To read more about these correspondences, I can recommend you start with: the blog posts ''Monads in Mathematics'' here: | ||

+ | [http://embuchestissues.wordpress.com/tag/monads-in-mathematics/] | ||

+ | |||

+ | |||

+ | ===Algebras over endofunctors=== | ||

+ | |||

+ | Suppose we started out with an endofunctor that is not the underlying functor of a monad - or an endofunctor for which we don't want to settle on a monadic structure. We can still do a lot of the Eilenberg-Moore machinery on this endofunctor - but we don't get quite the power of algebraic specification that monads offer us. At the core, here, lies the lack of associativity for a generic endofunctor - and algebras over endofunctors, once defined, will correspond to non-associative correspondences to their monadic counterparts. | ||

+ | |||

+ | '''Definition''' For an endofunctor <math>P:C\to C</math>, we define a ''<math>P</math>-algebra'' to be an arrow <math>\alpha\in C(PA,A)</math>. | ||

+ | |||

+ | A homomorphism of <math>P</math>-algebras <math>\alpha\to\beta</math> is some arrow <math>f:A\to B</math> such that the diagram below commutes: | ||

+ | :[[Image:EilenbergMoorePMorphism.png]] | ||

+ | |||

+ | This homomorphism definition does not need much work to apply to the monadic case as well. | ||

+ | |||

+ | ====Example: Groups==== | ||

+ | |||

+ | A group is a set <math>G</math> with operations <math>u: 1\to G, i: G\to G, m: G\times G\to G</math>, such that <math>u</math> is a unit for <math>m</math>, <math>m</math> is associative, and <math>i</math> is an inverse. | ||

+ | |||

+ | Ignoring for a moment the properties, the theory of groups is captured by these three maps, or by a diagram | ||

+ | :[[Image:GroupDiagram.png]] | ||

+ | |||

+ | We can summarize the diagram as | ||

+ | :<math>1+G+G\times G \mapsto^{[u,i,m]} G</math> | ||

+ | and thus recognize that groups are some equationally defined subcategory of the category of <math>T</math>-algebras for the polynomial functor <math>T(X) = 1 + X + X\times X</math>. The subcategory is ''full'', since if we have two algebras <math>\gamma: T(G)\to G</math> and <math>\eta: T(H)\to H</math>, that both lie within the subcategory that fulfills all the additional axioms, then certainly any morphism <math>\gamma\to\eta</math> will be compatible with the structure maps, and thus will be a group homomorphism. | ||

+ | |||

+ | ---- | ||

+ | |||

+ | We shall denote the category of <math>P</math>-algebras in a category <math>C</math> by <math>P-Alg(C)</math>, or just <math>P-Alg</math> if the category is implicitly understood. | ||

+ | |||

+ | This category is wider than the corresponding concept for a monad. We don't require the kind of associativity we would for a monad - we just lock down the underlying structure. This distinction is best understood with an example: | ||

+ | |||

+ | The free monoids monad has monoids for its algebras. On the other hand, we can pick out the underlying functor of that monad, forgetting about the unit and multiplication. An algebra over this structure is a slightly more general object: we no longer require <math>(a\cdot b)\cdot c = a\cdot (b\cdot c)</math>, and thus, the theory we get is that of a ''magma''. We have concatenation, but we can't drop the brackets, and so we get something more reminiscent of a binary tree. | ||

+ | |||

+ | ===Initial <math>P</math>-algebras and recursion=== | ||

+ | |||

+ | Consider the polynomical functor <math>P(X) = 1 + X</math> on the category of sets. It's algebras form a category, by the definitions above - and an algebra needs to pick out one special element 0, and one endomorphism T, for a given set. | ||

+ | |||

+ | What would an initial object in this category of P-algebras look like? It would be an object <math>I</math> equipped with maps <math>1 \to^o I \leftarrow^n I</math>. For any other pair of maps <math>a: 1\to X, s: X\to X</math>, we'd have a unique arrow <math>u: I\to X</math> such that | ||

+ | :[[Image:NNOcommutes.png]] | ||

+ | commutes, or in equations such that | ||

+ | :<math>u(o) = a</math> | ||

+ | :<math>u(n(x)) = s(u(x))</math> | ||

+ | |||

+ | Now, unwrapping the definitions in place, we notice that we will have elements <math>o, n(o), n(n(o), \dots</math> in <math>I</math>, and the initiality will force us to not have any ''other'' elements floating around. Also, intiality will prevent us from having any elements not in this minimally forced list. | ||

+ | |||

+ | We can rename the elements to form something more recognizable - by equating an element in <math>I</math> with the number of applications of <math>n</math> to <math>o</math>. This yields, for us, elements <math>0, 1, 2, \dots</math> with one function that picks out the <math>0</math>, and another that gives us the successor. | ||

+ | |||

+ | This should be recognizable as exactly the natural numbers; with just enough structure on them to make the principle of mathematical induction work: suppose we can prove some statement <math>P(0)</math>, and we can extend a proof of <math>P(n)</math> to <math>P(n+1)</math>. Then induction tells us that the statement holds for all <math>P(n)</math>. | ||

+ | |||

+ | More importantly, recursive definitions of functions from natural numbers can be performed here by choosing an appropriate algebra mapping to. | ||

+ | |||

+ | This correspondence between the initial object of <math>P(X) = 1 + X</math> is the reason such an initial object in a category with coproducts and terminal objects is called a ''natural numbers object''. | ||

+ | |||

+ | For another example, we consider the functor <math>P(X) = 1 + X\times X</math>. | ||

+ | |||

+ | '''Pop Quiz''' Can you think of a structure with this as underlying defining functor? | ||

+ | |||

+ | An initial <math>1+X\times X</math>-algebra would be some diagram | ||

+ | :<math>1 \to^o I \leftarrow^m I\times I</math> | ||

+ | such that for any other such diagram | ||

+ | :<math>1 \to^a X \leftarrow^* X\times X</math> | ||

+ | we have a unique arrow <math>u:I\to X</math> such that | ||

+ | :[[Image:MonoidCommutes.png]] | ||

+ | commutes. | ||

+ | |||

+ | Unwrapping the definition, working over Sets again, we find we are forced to have some element <math>*</math>, the image of <math>o</math>. Any two elements <math>S,T</math> in the set give rise to some <math>(S,T)</math>, which we can view as being the binary tree | ||

+ | :[[Image:MagmaComposition.png]] | ||

+ | |||

+ | The same way that we could construct induction as an algebra map from a natural numbers object, we can use this object to construct a tree-shaped induction; and similarily, we can develop what amounts to the theory of ''structural induction'' using these more general approaches to induction. | ||

+ | |||

+ | ====Example of structural induction==== | ||

+ | |||

+ | Using the structure of <math>1+X\times X</math>-algebras we shall prove the following statement: | ||

+ | |||

+ | '''Proposition''' The number of leaves in a binary tree is one more than the number of internal nodes. | ||

+ | |||

+ | '''Proof''' We write down the actual Haskell data type for the binary tree initial algebra. | ||

+ | <haskell> | ||

+ | data Tree = Leaf | Node Tree Tree | ||

+ | |||

+ | nLeaves Leaf = 1 | ||

+ | nLeaves (Node s t) = nLeaves s + nLeaves t | ||

+ | |||

+ | nNodes Leaf = 0 | ||

+ | nNodes (Node s t) = 1 + nNodes s + nNodes t | ||

+ | </haskell> | ||

+ | |||

+ | Now, it is clear, as a base case, that for the no-nodes tree <hask>Leaf</hask>: | ||

+ | <haskell> | ||

+ | nLeaves Leaf = 1 + nNodes Leaf | ||

+ | </haskell> | ||

+ | |||

+ | For the structural induction, now, we consider some binary tree, where we assume the statement to be known for each of the two subtrees. Hence, we have | ||

+ | <haskell> | ||

+ | tree = Node s t | ||

+ | |||

+ | nLeaves s = 1 + nNodes s | ||

+ | nLeaves t = 1 + nNodes t | ||

+ | </haskell> | ||

+ | and we may compute | ||

+ | <haskell> | ||

+ | nLeaves tree = nLeaves s + nLeaves t | ||

+ | = 1 + nNodes s + 1 + nNodes t | ||

+ | = 2 + nNodes s + nNodes t | ||

+ | |||

+ | nNodes tree = 1 + nNodes s + nNodes t | ||

+ | </haskell> | ||

+ | |||

+ | Now, since the statement is proven for each of the cases in the structural description of the data, it follows form the principle of structural induction that the proof is finished. | ||

+ | |||

+ | In order to really nail down what we are doing here, we need to define what we mean by predicates in a strict manner. There is a way to do this using ''fibrations'', but this reaches far outside the scope of this course. For the really interested reader, I'll refer to [http://www.springerlink.com/content/d022nlv03n26nm03/]. | ||

+ | |||

+ | Another way to do this is to introduce a ''topos'', and work it all out in terms of its ''internal logic'', but again, this reaches outside the scope of this course. | ||

+ | |||

+ | ====Lambek's lemma==== | ||

+ | |||

+ | What we do when we write a recursive data type definition in Haskell ''really'' to some extent is to define a data type as the initial algebra of the corresponding functor. This intuitive equivalence is vindicated by the following | ||

+ | |||

+ | '''Lemma''' ''Lambek'' If <math>P: C\to C</math> has an initial algebra <math>I</math>, then <math>P(I) = I</math>. | ||

+ | |||

+ | '''Proof''' Let <math>a: PA\to A</math> be an initial <math>P</math>-algebra. We can apply <math>P</math> again, and get a chain | ||

+ | :<math>PPA \to^{Pa} PA \to^a A</math> | ||

+ | We can fill out the diagram | ||

+ | :[[Image:LambekPartialDiagram.png]] | ||

+ | to form the diagram | ||

+ | :[[Image:LambekDiagram.png]] | ||

+ | where <math>f</math> is induced by initiality, since <math>Pa \colon PPA \to PA</math> is also a <math>P</math>-algebra. | ||

+ | |||

+ | The diagram above commutes, and thus <math>af = 1_{PA}</math> and <math>fa = 1_A</math>. Thus <math>f</math> is an inverse to <math>a</math>. QED. | ||

+ | |||

+ | Thus, by Lambek's lemma we ''know'' that if <math>P_A(X) = 1 + A\times X</math> then for that <math>P_A</math>, the initial algebra - should it exist - will fulfill<math>I = 1 + A\times I</math>, which in turn is exactly what we write, defining this, in Haskell code: | ||

+ | <haskell> | ||

+ | List a = Nil | Cons a List | ||

+ | </haskell> | ||

+ | |||

+ | ====Recursive definitions with the unique maps from the initial algebra==== | ||

+ | |||

+ | Consider the following <math>P_A(X)</math>-algebra structure <math>l: P_A(\mathbb N)\to\mathbb N</math> on the natural numbers: | ||

+ | <haskell> | ||

+ | l(*) = 0 | ||

+ | l(a,n) = 1 + n | ||

+ | </haskell> | ||

+ | |||

+ | We get a unique map <math>f</math> from the initial algebra for <math>P_A(X)</math> (lists of elements of type <math>A</math>) to <math>\mathbb N</math> from this definition. This map will fulfill: | ||

+ | <haskell> | ||

+ | f(Nil) = l(*) = 0 | ||

+ | f(Cons a xs) = l(a,f(xs)) = 1 + f(xs) | ||

+ | </haskell> | ||

+ | which starts taking on the shape of the usual definition of the length of a list: | ||

+ | <haskell> | ||

+ | length(Nil) = 0 | ||

+ | length(Cons a xs) = 1 + length(xs) | ||

+ | </haskell> | ||

+ | |||

+ | And thus, the machinery of endofunctor algebras gives us a strong method for doing recursive definitions in a theoretically sound manner. | ||

+ | |||

+ | ===Homework=== | ||

+ | |||

+ | Complete credit will be given for 8 of the 13 questions. | ||

+ | |||

+ | # Find a monad whose algebras are ''associative algebras'': vector spaces with a binary, associative, unitary operation (multiplication) defined on them. Factorize the monad into a free/forgetful adjoint pair. | ||

+ | # Find an endofunctor of <math>Hask</math> whose initial object describes trees that are either binary of ternary at each point, carrying values from some <math>A</math> in the leaves. | ||

+ | # Write an implementation of the monad of vector spaces in Haskell. If this is tricky, restrict the domain of the monad to, say, a 3-element set, and implement the specific example of a 3-dimensional vector space as a monad. Hint: [http://sigfpe.blogspot.com] has written about this approach. | ||

+ | # Find a <math>X\mapsto 1+A\times X</math>-algebra <math>L</math> such that the unique map from the initial algebra <math>I</math> to <math>L</math> results in the function that will reverse a given list. | ||

+ | # Find a <math>X\mapsto 1+A\times X</math>-algebra structure on the object <math>1+A</math> that will pick out the first element of a list, if possible. | ||

+ | # Find a <math>X\mapsto \mathbb N+X\times X</math>-algebra structure on the object <math>\mathbb N</math> that will pick out the sum of the leaf values for the binary tree in the initial object. | ||

+ | # Complete the proof of Lambek's lemma by proving the diagram commutes. | ||

+ | # * We define a ''coalgebra'' for an endofunctor <math>T</math> to be some arrow <math>\gamma: A \to TA</math>. If <math>T</math> is a comonad - i.e. equipped with a counit <math>\epsilon: T\to 1</math> and a cocomposition <math>\Delta: T\to T\times T</math>, then we define a coalgebra for the comonad <math>T</math> to additionally fulfill <math>\gamma\circ T\gamma = \gamma\circ\Delta</math> (compatibility) and <math>\epsilon_A\circ\gamma = 1_A</math> (counitality). | ||

+ | ## (2pt) Prove that if <math>T</math> is an endofunctor, then if <math>T</math> has an initial algebra, then it is a coalgebra. Does <math>T</math> necessarily have a final coalgebra? | ||

+ | ## (2pt) Prove that if <math>U,F</math> are an adjoint pair, then <math>FU</math> forms a comonad. | ||

+ | ## (2pt) Describe a final coalgebra over the comonad formed from the free/forgetful adjunction between the categories of Monoids and Sets. | ||

+ | ## (2pt) Describe a final coalgebra over the endofunctor <math>P(X) = 1 + X</math>. | ||

+ | ## (2pt) Describe a final coalgebra over the endofunctor <math>P(X) = 1 + A\times X</math>. | ||

+ | ## (2pt) Prove that if <math>c: C\to PC</math> is a final coalgebra for an endofunctor <math>P:C\to C</math>, then <math>c</math> is an isomorphism. |

## Latest revision as of 15:45, 22 November 2009

## Contents |

### [edit] 1 Algebras over monads

We recall from the last lecture the definition of an Eilenberg-Moore algebra over a monad *T* = (*T*,η,μ):

**Definition** An *algebra* over a monad *T* in a category *C* (a *T*-algebra) is a morphism , such that the diagrams below both commute:

While a monad corresponds to the imposition of some structure on the objects in a category, an algebra over that monad corresponds to some evaluation of that structure.

#### [edit] 1.1 Example: monoids

Let *T* be the Kleene star monad - the one we get from the adjunction of free and forgetful functors between Monoids and Sets. Then a *T*-algebra on a set *A* is equivalent to a monoid structure on *A*.

Indeed, if we have a monoid structure on *A*, given by and , we can construct a *T*-algebra by

- α([]) =
*u*

This gives us, indeed, a *T*-algebra structure on *A*. Associativity and unity follows from the corresponding properties in the monoid.

On the other hand, if we have a *T*-algebra structure on *A*, we can construct a monoid structure by setting

*u*= α([])*m*(*a*,*b*) = α([*a*,*b*])

It is clear that associativity of *m* follows from the associativity of α, and unitality of *u* follows from the unitality of α.

#### [edit] 1.2 Example: Vector spaces

We have free and forgetful functors

forming an adjoint pair; where the free functor takes a set *S* and returns the vector space with basis *S*; while the forgetful functor takes a vector space and returns the set of all its elements.

The composition of these yields a monad *T* in *S**e**t* taking a set *S* to the set of all formal linear combinations of elements in *S*. The monad multiplication takes formal linear combinations of formal linear combinations and multiplies them out:

- 3(2
*v*+ 5*w*) − 5(3*v*+ 2*w*) = 6*v*+ 15*w*− 15*v*− 10*w*= − 9*v*+ 5*w*

A *T*-algebra is a map that *acts like a vector space* in the sense that .

We can define and *v* + *w* = α(*v* + *w*). The operations thus defined are associative, distributive, commutative, and everything else we could wish for in order to define a vector space - precisely because the operations inside *T**A* are, and α is associative.

The moral behind these examples is that using monads and monad algebras, we have significant power in defining and studying algebraic structures with categorical and algebraic tools. This paradigm ties in closely with the theory of *operads* - which has its origins in topology, but has come to good use within certain branches of universal algebra.

An (non-symmetric) *operad* is a graded set equipped with composition operations that obey certain unity and associativity conditions. As it turns out, non-symmetric operads correspond to the summands in a monad with polynomial underlying functor, and from a non-symmetric operad we can construct a corresponding monad.

The designator non-symmetric floats in this text o avoid dealing with the slightly more general theory of symmetric operads - which allow us to resort the input arguments, thus including the symmetrizer of a symmetric monoidal category in the entire definition.

To read more about these correspondences, I can recommend you start with: the blog posts *Monads in Mathematics* here:
[1]

### [edit] 2 Algebras over endofunctors

Suppose we started out with an endofunctor that is not the underlying functor of a monad - or an endofunctor for which we don't want to settle on a monadic structure. We can still do a lot of the Eilenberg-Moore machinery on this endofunctor - but we don't get quite the power of algebraic specification that monads offer us. At the core, here, lies the lack of associativity for a generic endofunctor - and algebras over endofunctors, once defined, will correspond to non-associative correspondences to their monadic counterparts.

**Definition** For an endofunctor , we define a * P-algebra* to be an arrow .

A homomorphism of *P*-algebras is some arrow such that the diagram below commutes:

This homomorphism definition does not need much work to apply to the monadic case as well.

#### [edit] 2.1 Example: Groups

A group is a set *G* with operations , such that *u* is a unit for *m*, *m* is associative, and *i* is an inverse.

Ignoring for a moment the properties, the theory of groups is captured by these three maps, or by a diagram

We can summarize the diagram as

and thus recognize that groups are some equationally defined subcategory of the category of *T*-algebras for the polynomial functor . The subcategory is *full*, since if we have two algebras and , that both lie within the subcategory that fulfills all the additional axioms, then certainly any morphism will be compatible with the structure maps, and thus will be a group homomorphism.

We shall denote the category of *P*-algebras in a category *C* by *P* − *A**l**g*(*C*), or just *P* − *A**l**g* if the category is implicitly understood.

This category is wider than the corresponding concept for a monad. We don't require the kind of associativity we would for a monad - we just lock down the underlying structure. This distinction is best understood with an example:

The free monoids monad has monoids for its algebras. On the other hand, we can pick out the underlying functor of that monad, forgetting about the unit and multiplication. An algebra over this structure is a slightly more general object: we no longer require , and thus, the theory we get is that of a *magma*. We have concatenation, but we can't drop the brackets, and so we get something more reminiscent of a binary tree.

### [edit] 3 Initial *P*-algebras and recursion

Consider the polynomical functor *P*(*X*) = 1 + *X* on the category of sets. It's algebras form a category, by the definitions above - and an algebra needs to pick out one special element 0, and one endomorphism T, for a given set.

What would an initial object in this category of P-algebras look like? It would be an object *I* equipped with maps . For any other pair of maps , we'd have a unique arrow such that

commutes, or in equations such that

*u*(*o*) =*a**u*(*n*(*x*)) =*s*(*u*(*x*))

Now, unwrapping the definitions in place, we notice that we will have elements in *I*, and the initiality will force us to not have any *other* elements floating around. Also, intiality will prevent us from having any elements not in this minimally forced list.

We can rename the elements to form something more recognizable - by equating an element in *I* with the number of applications of *n* to *o*. This yields, for us, elements with one function that picks out the 0, and another that gives us the successor.

This should be recognizable as exactly the natural numbers; with just enough structure on them to make the principle of mathematical induction work: suppose we can prove some statement *P*(0), and we can extend a proof of *P*(*n*) to *P*(*n* + 1). Then induction tells us that the statement holds for all *P*(*n*).

More importantly, recursive definitions of functions from natural numbers can be performed here by choosing an appropriate algebra mapping to.

This correspondence between the initial object of *P*(*X*) = 1 + *X* is the reason such an initial object in a category with coproducts and terminal objects is called a *natural numbers object*.

For another example, we consider the functor .

**Pop Quiz** Can you think of a structure with this as underlying defining functor?

An initial -algebra would be some diagram

such that for any other such diagram

we have a unique arrow such that

commutes.

Unwrapping the definition, working over Sets again, we find we are forced to have some element * , the image of *o*. Any two elements *S*,*T* in the set give rise to some (*S*,*T*), which we can view as being the binary tree

The same way that we could construct induction as an algebra map from a natural numbers object, we can use this object to construct a tree-shaped induction; and similarily, we can develop what amounts to the theory of *structural induction* using these more general approaches to induction.

#### [edit] 3.1 Example of structural induction

Using the structure of -algebras we shall prove the following statement:

**Proposition** The number of leaves in a binary tree is one more than the number of internal nodes.

**Proof** We write down the actual Haskell data type for the binary tree initial algebra.

data Tree = Leaf | Node Tree Tree nLeaves Leaf = 1 nLeaves (Node s t) = nLeaves s + nLeaves t nNodes Leaf = 0 nNodes (Node s t) = 1 + nNodes s + nNodes t

nLeaves Leaf = 1 + nNodes Leaf

For the structural induction, now, we consider some binary tree, where we assume the statement to be known for each of the two subtrees. Hence, we have

tree = Node s t nLeaves s = 1 + nNodes s nLeaves t = 1 + nNodes t

and we may compute

nLeaves tree = nLeaves s + nLeaves t = 1 + nNodes s + 1 + nNodes t = 2 + nNodes s + nNodes t nNodes tree = 1 + nNodes s + nNodes t

Now, since the statement is proven for each of the cases in the structural description of the data, it follows form the principle of structural induction that the proof is finished.

In order to really nail down what we are doing here, we need to define what we mean by predicates in a strict manner. There is a way to do this using *fibrations*, but this reaches far outside the scope of this course. For the really interested reader, I'll refer to [2].

Another way to do this is to introduce a *topos*, and work it all out in terms of its *internal logic*, but again, this reaches outside the scope of this course.

#### [edit] 3.2 Lambek's lemma

What we do when we write a recursive data type definition in Haskell *really* to some extent is to define a data type as the initial algebra of the corresponding functor. This intuitive equivalence is vindicated by the following

**Lemma** *Lambek* If has an initial algebra *I*, then *P*(*I*) = *I*.

**Proof** Let be an initial *P*-algebra. We can apply *P* again, and get a chain

We can fill out the diagram

to form the diagram

where *f* is induced by initiality, since is also a *P*-algebra.

The diagram above commutes, and thus *a**f* = 1_{PA} and *f**a* = 1_{A}. Thus *f* is an inverse to *a*. QED.

Thus, by Lambek's lemma we *know* that if then for that *P*_{A}, the initial algebra - should it exist - will fulfill, which in turn is exactly what we write, defining this, in Haskell code:

List a = Nil | Cons a List

#### [edit] 3.3 Recursive definitions with the unique maps from the initial algebra

Consider the following *P*_{A}(*X*)-algebra structure on the natural numbers:

l(*) = 0 l(a,n) = 1 + n

We get a unique map *f* from the initial algebra for *P*_{A}(*X*) (lists of elements of type *A*) to from this definition. This map will fulfill:

f(Nil) = l(*) = 0 f(Cons a xs) = l(a,f(xs)) = 1 + f(xs)

which starts taking on the shape of the usual definition of the length of a list:

length(Nil) = 0 length(Cons a xs) = 1 + length(xs)

And thus, the machinery of endofunctor algebras gives us a strong method for doing recursive definitions in a theoretically sound manner.

### [edit] 4 Homework

Complete credit will be given for 8 of the 13 questions.

- Find a monad whose algebras are
*associative algebras*: vector spaces with a binary, associative, unitary operation (multiplication) defined on them. Factorize the monad into a free/forgetful adjoint pair. - Find an endofunctor of
*H**a**s**k*whose initial object describes trees that are either binary of ternary at each point, carrying values from some*A*in the leaves. - Write an implementation of the monad of vector spaces in Haskell. If this is tricky, restrict the domain of the monad to, say, a 3-element set, and implement the specific example of a 3-dimensional vector space as a monad. Hint: [3] has written about this approach.
- Find a -algebra
*L*such that the unique map from the initial algebra*I*to*L*results in the function that will reverse a given list. - Find a -algebra structure on the object 1 +
*A*that will pick out the first element of a list, if possible. - Find a -algebra structure on the object that will pick out the sum of the leaf values for the binary tree in the initial object.
- Complete the proof of Lambek's lemma by proving the diagram commutes.
- * We define a
*coalgebra*for an endofunctor*T*to be some arrow . If*T*is a comonad - i.e. equipped with a counit and a cocomposition , then we define a coalgebra for the comonad*T*to additionally fulfill (compatibility) and (counitality).- (2pt) Prove that if
*T*is an endofunctor, then if*T*has an initial algebra, then it is a coalgebra. Does*T*necessarily have a final coalgebra? - (2pt) Prove that if
*U*,*F*are an adjoint pair, then*F**U*forms a comonad. - (2pt) Describe a final coalgebra over the comonad formed from the free/forgetful adjunction between the categories of Monoids and Sets.
- (2pt) Describe a final coalgebra over the endofunctor
*P*(*X*) = 1 +*X*. - (2pt) Describe a final coalgebra over the endofunctor .
- (2pt) Prove that if is a final coalgebra for an endofunctor , then
*c*is an isomorphism.

- (2pt) Prove that if