The object c has a unique global element.

There exists another object d for which there is a unique generalized element of shape d in c.

For all objects d, there is a unique generalized element of each shape d in c

It seems 2 and 3 are straightforward, but 1 got me totally confused. To avoid spoilers - any hints to get started?

(To be clear - I am trying something out but got confused because what I currently have doesn't match my intuition)

]]>(a) Given a List-algebra \(a: List(X) \rightarrow X\), construct a monoid on the set X.

(b) Given a monoid (X,*,e), construct a list algebra

(c) Show that your two constructions are inverses (we hope they are!)

]]>data F a = Nil | Cons Int a deriving Functor

We define the recursive type:

type ListInt = Fix F

(a) Let `isEven :: Int -> Bool`

take an integer to `True`

if it is even, and `False`

otherwise. Here is an F-algebra.

hello :: F Bool -> Bool

hello Nil = False

hello Cons n a = isEven n || a

What is the induced catamorphism `cata hello :: List Int -> Bool`

?

(b) Implement the function `product :: ListInt -> Int`

that takes a list of integers and returns their product.

Let \(C\) and \(D\) be categories. Given any object \(d\) in \(D\), we can define the *constant functor* \(K_d: C \rightarrow D\) on \(d\). This functor sends *every* object \(C\) to \(d \in Ob\ D\), and *every* morphism of \(C\) to the identity morphism on \(d\).

(a) Take the set \(B\) = {\(T,F\)}. Show that the constant functor \(K_B: Set \rightarrow Set\) obeys the two functor laws: preservation of composition and preservation of identities.

(b) Implement in Haskell the constant functor on the type Bool.

]]>

Given a set \(X\), a binary operation on \(X\) is a function \(*: X \times X \rightarrow X\) -- that is, a way of taking two elements of \(X\) and returning a third. This question explores how the product unifies many seemingly quite different binary operations commonly used in math.

(a) Consider the category where the objects are natural numbers and where there is a unique morphism from \(m\) to \(n\) if \(m\) divides \(n\). Given two numbers, for example 42 and 27, what is their product? What is the name of this binary operation?

(b) Consider the category where the objects are subsets of the set \(\lbrace a,b,c,d \rbrace\), and where there is a unique morphism from \(X\) to \(Y\) if \(X\) is a subset of \(Y\). Given two subsets, for example \(\lbrace a,b,c \rbrace\) and \(\lbrace b,c,d \rbrace\), what is their product? What is the name of this binary operation?

(c) Consider the category where the objects are True and False, and where there is a unique morphism from \(a\) to \(b\) if \(a\) implies \(b\). Given two objects, for example True and False, what is their product? What is the name of this binary operation?

]]>

\( FA : Set \rightarrow Set; \)

\( X \rightarrow A + X \times X; \)

\( (f : X \rightarrow Y ) \mapsto (idA + f \times f): A + X \times X \rightarrow A + Y \times Y \)

(a) Define a monad T : Set → Set that maps a set A to the set of trees with leaves valued in A.

(b) Implement this monad in Haskell

]]>In this question, we implement merge sort using a hylomorphism. Here’s the idea: The seed (the carrier of the coalgebra) is the list to be sorted. Use this function

split :: [a] -> ([a], [a])

split (a: b: t) = (a: t1, b: t2)

where

`(t1, t2) = split t split l = (l, [])`

to split the list into two lists and use them as new seeds. Make sure you know how to deal with empty lists.

The carrier of the algebra is again a list (this time it’s actually a sorted list, but this cannot be reflected in the type). Your partial results are sorted lists. You combine them using this function.

merge :: Ord a => [a] -> [a] -> [a]

merge (a: as) (b: bs) =

if a <= b

then a : merge as (b: bs)

else b : merge (a: as) bs

merge as [] = as

merge [] bs = bs

Make sure your program also works for empty lists (it should return an empty list).

]]>A preorder is a category such that, for every two objects \(a,b \), there is at most one morphism \(a \to b \). That is, there either is or is not a morphism from \(a \) to \(b \), but there are never two morphisms \(a \) to \(b \). If there is a morphism \(a \to b \), we write \(a \leq b \); if there is not a morphism \(a \to b \), we don’t. For example, there is a preorder \(P \) whose objects are the positive integers \(Ob(P) = \mathbb{N}_{≥1} \) and whose hom-sets are given by \(P(a,b) := \{x \in N | x∗a = b \} \) This is a preorder because either \(P(a,b) \) is empty (if \(b \) is not divisible by \(a \)) or contains exactly one element.

--

(a) What is the identity on 12?

(b) Show that if \(x : a \to b \) and \(y : b \to c \) are morphisms, then there is a morphism \(y◦x \) to serve as their composite.

(c) Would it have worked just as well to take \(P \) to have all of \(\mathbb{N} \) as objects, rather than just the positive integers?

]]>data Nat = Zero | Succ Nat

(a) Implement a type `Nat2`

, isomorphic to `Nat`

, but this time defined as an initial
algebra of a functor.

(b) Using a catamorphism, define a function `Nat2 -> Int`

that maps n to the n-th
Fibonacci number.

(c) Define a coalgebra whose anamorphism is a (partial) function `Int -> Nat2`

that
sends a non-negative Int into its fixed-point representation (ie. its representation
as a value of Nat2).

Consider the category **3**, which has three objects and six morphisms, and is depicted as follows:

\begin{CD} 1 @>a>> 2 @>b>>3 \end{CD}

How many functors are there from **Set** to **3**? Write them down.

]]>

A bifunctor is a polymorphic type constructor F that takes two variables together with an implementation of a function:

bimap :: (a -> b) -> (c -> d) -> F a c -> F b d

Using the universal property of the coproduct, provide an implementation of bimap for the sum type constructor Either a b.

]]>

Recall that Haskell has a built-in product (pair) type with constructors written:

data (a,b) = (a,b)

Implement isomorphisms of the following type signatures by drawing diagrams and translating them into code. Explain why the functions you have constructed are isomorphisms.

(a) swap :: (a,b) -> (b,a)

(b) unit :: a -> ((),a)

(c) assoc :: (a,(b,c)) -> ((a,b),c)

]]>

Recall the definitions of terminal object and product from Chapter 3 in the notes.

(a) Show that if \(t\) and \(t'\) are both terminal objects in a category, then \(t\) and \(t'\) are isomorphic.

(b) Let \(a\) and \(b\) be objects of a category. Show that if \(p\) and \(p'\) are both products of \(a\) and \(b\), then they are isomorphic.

(c) Discuss the similarities between your two proofs. Could the same idea be used to show that any two initial objects are isomorphic?

]]>

(a) Give an example of some data – objects, morphisms, composition, and identities– that satisfies the associative laws but not the unit law.

(b) Give an example of some data – objects, morphisms, composition, and identities– that satisfies the unit laws but not the associative law.

]]>Tell a story about what's going on in this course. Is there an example you find interesting or enlightening, or just fun? Have you had any *a-ha!* moments? Share one.

]]>

Let \(C\) and \(D\) be categories, and let \(F,G: C \rightarrow D\) be functors. Recall that a natural transformation \(\alpha: F \implies G\) consists of a morphism \(\alpha_c: F(c) \rightarrow G(c)\) for each \(c \in Ob(C)\), such that for each \(f: c_1 \rightarrow c_2\) in \(C\) the square:

\begin{CD} F(c_1) @>F(f)>> F(c_2) \\@V{\alpha_{c_1}}VV {}@VV{\alpha_{c_2}}V \\ G(c_1) @>>G(f)> G(c_2) \end{CD}

commutes in \(D\).

Write \(id_{Set}: Set \rightarrow Set\) for the identity functor on \(Set\), and Double: \(Set \rightarrow Set\) for the functor that sends a set \(X\) to the set \(X \times X\), and a function \(f: X \rightarrow Y\) to the function Double(\(f\)): \(X \times X \rightarrow Y \times Y\) that maps \((x_1,x_2)\) to \((f(x_1),f(x_2))\).

**Part A**. For each set \(X\), define the *diagonal function* \(\delta_x: X \rightarrow X \times X\) to map \(x\) to the pair \((x,x)\). Prove that \(\delta: id_{Set} \implies\) Double defines a natural transformation.

**Part B**. Using the universal property of the product, write a polymorphic Haskell function

diag :: a -> (a,a)

implementing this natural transformation.

]]>

Given two categories \(C\) and \(D\), we may construct a new category \(C \times C\) by taking pairs of objects and morphisms.. More precisely:

- The objects of \(C \times D\) are pairs \((c,d)\) where \(c \in Ob\ C\) and \(d \in Ob\ D\).
- The morphisms \((c_1, d_1) \rightarrow (c_2,d_2)\) are pairs \((f,g)\) where \(f: c_1 \rightarrow c_2\) in \(C\) and \(g: d_1 \rightarrow d_2\) in \(D\).
- Composition is given pointwise: given \((f,g): (c_1,d_1) \rightarrow (c_2,d_2)\) and \((h,k): (c_2,d_2) \rightarrow (c_3,d_3)\), their composite is \((h \circ f, k \circ g): (c_1,d_1) \rightarrow (c_3,d_3)\).
- Similarly, the identity morphisms are given by \((id_c,id_d): (c,d) \rightarrow (c,d)\).

Recall that the category Cat whose objects are categories and morphisms are functors. Show that \(C \times D\) is the product of \(C\) and \(D\) in Cat.

]]>

A monoid \((M,∗,e) \) is

a set \(M \);

a function \(∗:M×M \to M \); and

an element \(e \in M \) called the identity;

subject to two laws:

Unit: the equations \(e∗m=m \) and \(m∗e=m \) hold for any \(m \in M \).

Associative: the equation \((m1∗m2)∗m3=m1∗(m2∗m3) \) holds for any \(m1,m2,m3 \in M \).

--

(a) Show that \((\mathbb{N},+,0) \) forms a monoid.

(b) A string in 0 and 1 is a (possibly) empty sequence of 0s and 1s; examples include 0, 11, 0110, 0101110 and so on. We write the empty string []. Let \(List_{0,1} \) be the set of strings in 0 and 1. Given two strings \(a \) and \(b \), we may concatenate them to form a new string \(ab \). Show that \(List_{0,1} \), together with concatenation and the empty string [], form a monoid.

(c) Explain why (prove that) every monoid can be viewed as a category with a single object.

]]>Suppose \(f : Int \to Int \) sends an integer to its square, \(f(x) := x^2 \) and that \(g : Int \to Int \) sends an integer to its successor, \(g(x) := x+ 1\).

(a) Write \(f \) and \(g \) in Haskell, including their type signature and their implementation.

(b) Let \(h := f◦g \). What is \(h(2) \)?

(c) Let \(i := f ; g \). What is \(i(2) \)?

]]>Recall that a category consists of the data:

a set \(Ob(C) \) of objects;

for every pair of objects \(c,d \in Ob(C) \) a set \(C(c,d) \) of morphisms;

for every three objects \(b,c,d \) and morphisms \(f : b \to c \) and \(g : c \to d \), a specified morphism \((f ; g) : b \to d \) called the composite of \(f \) and \(g \);

for every object \(c \), an identity morphism \(id_c \in C(c,c) \); and

subject to two laws:

Unit: for any \(f : c \to d \), the equations \(id_c ; f = f \) and \(f ; id_d = f \) hold.

Associative: for any \(f_1 : c_1 \to c_2, f_2 : c_2 \to c_3 \), and \(f_3 : c_3 \to c_4 \), the equation \((f_1 ; f_2) ; f_3 = f_1 ; (f_2 ; f_3) \) holds.

--

This tiny category is sometimes called the walking arrow category \(2 \).

(a) Write down the set of objects, the four sets of morphisms, the composition rule, and the identity morphisms.

(b) Prove that this category obeys the unit and associative laws.

]]>Suppose that someone tells you that their category \(C \) has two objects \(c,d \) and two non-identity morphisms, \(f : c \to d \) and \(g : d \to c \), but no other morphisms. Does \(f \) have to be the inverse of \(g \), i.e. is it forced by the category axioms that \( g◦f = id_c \) and \(f◦g= id_d \)?

]]>