82
The roadmap The aim is to understand this: In category theory, the concept of catamorphism denotes the unique homomorphism from an initial algebra into some other algebra. To better understand this: In functional programming, catamorphisms provide generalizations of folds of lists to arbitrary algebraic data types, which can be described as initial algebras. The dual concept is that of anamorphism that generalize unfolds. A hylomorphism is the composition of an anamorphism followed by a catamorphism. 1 / 28

homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The roadmap

The aim is to understand this:

In category theory, the concept of catamorphism denotes the uniquehomomorphism from an initial algebra into some other algebra.

To better understand this:

In functional programming, catamorphisms provide generalizations offolds of lists to arbitrary algebraic data types, which can be describedas initial algebras. The dual concept is that of anamorphism thatgeneralize unfolds. A hylomorphism is the composition of ananamorphism followed by a catamorphism.

1 / 28

Page 2: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The outline

I Basic category theory

I Haskell as a category (if you squint)I Category theoretic functors, Haskell Functors endofunctorsI Algebras over an endofunctorI F-Algebra homomorphisms, or arrows in the category of F-algebrasI Initial objectsI Catamorphisms as unique homomorphisms from initial objectsI Flip the arrows

2 / 28

Page 3: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The outline

I Basic category theoryI Haskell as a category (if you squint)

I Category theoretic functors, Haskell Functors endofunctorsI Algebras over an endofunctorI F-Algebra homomorphisms, or arrows in the category of F-algebrasI Initial objectsI Catamorphisms as unique homomorphisms from initial objectsI Flip the arrows

2 / 28

Page 4: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The outline

I Basic category theoryI Haskell as a category (if you squint)I Category theoretic functors, Haskell Functors endofunctors

I Algebras over an endofunctorI F-Algebra homomorphisms, or arrows in the category of F-algebrasI Initial objectsI Catamorphisms as unique homomorphisms from initial objectsI Flip the arrows

2 / 28

Page 5: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The outline

I Basic category theoryI Haskell as a category (if you squint)I Category theoretic functors, Haskell Functors endofunctorsI Algebras over an endofunctor

I F-Algebra homomorphisms, or arrows in the category of F-algebrasI Initial objectsI Catamorphisms as unique homomorphisms from initial objectsI Flip the arrows

2 / 28

Page 6: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The outline

I Basic category theoryI Haskell as a category (if you squint)I Category theoretic functors, Haskell Functors endofunctorsI Algebras over an endofunctorI F-Algebra homomorphisms, or arrows in the category of F-algebras

I Initial objectsI Catamorphisms as unique homomorphisms from initial objectsI Flip the arrows

2 / 28

Page 7: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The outline

I Basic category theoryI Haskell as a category (if you squint)I Category theoretic functors, Haskell Functors endofunctorsI Algebras over an endofunctorI F-Algebra homomorphisms, or arrows in the category of F-algebrasI Initial objects

I Catamorphisms as unique homomorphisms from initial objectsI Flip the arrows

2 / 28

Page 8: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The outline

I Basic category theoryI Haskell as a category (if you squint)I Category theoretic functors, Haskell Functors endofunctorsI Algebras over an endofunctorI F-Algebra homomorphisms, or arrows in the category of F-algebrasI Initial objectsI Catamorphisms as unique homomorphisms from initial objects

I Flip the arrows

2 / 28

Page 9: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The outline

I Basic category theoryI Haskell as a category (if you squint)I Category theoretic functors, Haskell Functors endofunctorsI Algebras over an endofunctorI F-Algebra homomorphisms, or arrows in the category of F-algebrasI Initial objectsI Catamorphisms as unique homomorphisms from initial objectsI Flip the arrows

2 / 28

Page 10: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

What’s a category?

Pick some things:I Objects (X,Y,Z)

I Arrows between objects (f,g,g ◦ f )

Assert some properties:

I All arrows compose associativelyI Every object has an identity arrow

X Y

Z

fidX

g

idY

g ◦ f

idZ

Figure 1:

3 / 28

Page 11: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

What’s a category?

Pick some things:I Objects (X,Y,Z)I Arrows between objects (f,g,g ◦ f )

Assert some properties:

I All arrows compose associativelyI Every object has an identity arrow

X Y

Z

fidX

g

idY

g ◦ f

idZ

Figure 1:

3 / 28

Page 12: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

What’s a category?

Pick some things:I Objects (X,Y,Z)I Arrows between objects (f,g,g ◦ f )

Assert some properties:I All arrows compose associatively

I Every object has an identity arrow

X Y

Z

fidX

g

idY

g ◦ f

idZ

Figure 1:

3 / 28

Page 13: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

What’s a category?

Pick some things:I Objects (X,Y,Z)I Arrows between objects (f,g,g ◦ f )

Assert some properties:I All arrows compose associativelyI Every object has an identity arrow

X Y

Z

fidX

g

idY

g ◦ f

idZ

Figure 1:

3 / 28

Page 14: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

What’s a category?

Pick some things:I Objects (X,Y,Z)I Arrows between objects (f,g,g ◦ f )

Assert some properties:I All arrows compose associativelyI Every object has an identity arrow

X Y

Z

fidX

g

idY

g ◦ f

idZ

Figure 1:

3 / 28

Page 15: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

What’s a category?

Pick some things:I Objects (X,Y,Z)I Arrows between objects (f,g,g ◦ f )

Assert some properties:I All arrows compose associativelyI Every object has an identity arrow

X Y

Z

fidX

g

idY

g ◦ f

idZ

Figure 1:

3 / 28

Page 16: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Haskell as sort of a categoryPick some things:

I Objects are types (not values!)

I Arrows are functions between types

Assert some properties:

I All arrows compose associativelyI Every object has an identity arrow

(.) :: (b -> c) -> (a -> b) -> a -> cid :: a -> a

NB: it’s lies, all lies!

seq undefined () = undefinedseq (undefined . id) () = () 4 / 28

Page 17: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Haskell as sort of a categoryPick some things:

I Objects are types (not values!)I Arrows are functions between types

Assert some properties:

I All arrows compose associativelyI Every object has an identity arrow

(.) :: (b -> c) -> (a -> b) -> a -> cid :: a -> a

NB: it’s lies, all lies!

seq undefined () = undefinedseq (undefined . id) () = () 4 / 28

Page 18: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Haskell as sort of a categoryPick some things:

I Objects are types (not values!)I Arrows are functions between types

Assert some properties:I All arrows compose associatively

I Every object has an identity arrow

(.) :: (b -> c) -> (a -> b) -> a -> cid :: a -> a

NB: it’s lies, all lies!

seq undefined () = undefinedseq (undefined . id) () = () 4 / 28

Page 19: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Haskell as sort of a categoryPick some things:

I Objects are types (not values!)I Arrows are functions between types

Assert some properties:I All arrows compose associativelyI Every object has an identity arrow

(.) :: (b -> c) -> (a -> b) -> a -> cid :: a -> a

NB: it’s lies, all lies!

seq undefined () = undefinedseq (undefined . id) () = () 4 / 28

Page 20: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Functors, endofunctorsA functor is a mapping between categories that sends objects to objects (typesto types) and arrows to arrows (terms to terms), preserving identity arrowsand composition, possibly across categories.

fmap id = idfmap f . fmap g = fmap (f . g)

Endofunctors map from a category to the same category.In the case of Hask:

class Functor (f :: * -> *) wherefmap :: (a -> b) -> f a -> f b

5 / 28

Page 21: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Algebra “over” an endofunctor

For a category C and endofunctor F analgebra of F is an object X in C and amorphism:

alg : F (X )→ X

X is called the “carrier” of the algebra.

-- For a category and endofunctordata F a = Zero | Succ a

instance Functor F wherefmap _ Zero = Zerofmap f (Succ a) = Succ (f a)

-- An algebra of F is an X in Ctype X = Natural

-- And a morphismalg :: F X -> Xalg Zero = 0alg (Succ n) = n + 1 6 / 28

Page 22: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

F Natural -> Natural

data F a = Zero | Succ a

alg :: F Natural -> Naturalalg Zero = 0alg (Succ n) = n + 1

> alg Zero0> alg $ Succ $ alg Zero1> alg $ Succ $ alg $ Succ $ alg Zero2

7 / 28

Page 23: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

An alternate algebra, same F and C, different X

alg' :: F String -> Stringalg' Zero = "!"alg' (Succ s) = "QUACK" ++ s

> alg' $ Succ $ alg' $ Succ $ alg' Zero"QUACKQUACK!"

8 / 28

Page 24: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Prescient fun fact

An initial object of a category C is an object I in C such that for everyobject X in C, there exists precisely one morphism I → X. - Wikipedia

(up to isomorphism)

9 / 28

Page 25: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Initial, in the category of algebras

An initial algebra for an endofunctor F on a category C is an initial object inthe category of algebras of F.

Category of algebras of F:

I Objects: alg, alg’, . . .I Arrows : structure preserving maps (homomorphisms) from an algebra toanother

10 / 28

Page 26: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Initial, in the category of algebras

An initial algebra for an endofunctor F on a category C is an initial object inthe category of algebras of F.Category of algebras of F:

I Objects: alg, alg’, . . .

I Arrows : structure preserving maps (homomorphisms) from an algebra toanother

10 / 28

Page 27: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Initial, in the category of algebras

An initial algebra for an endofunctor F on a category C is an initial object inthe category of algebras of F.Category of algebras of F:

I Objects: alg, alg’, . . .I Arrows : structure preserving maps (homomorphisms) from an algebra toanother

10 / 28

Page 28: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Homomorphisms between two algebras.An arrow in the category of F-algebras of a given endofunctor e.g. between(Natural, alg) and (String, alg’) is a function mapping the carrier in theunderlying category (Hask, hom : Natural -> String), such that the followingsquare commutes:

F (Natural) F (String)

Natural String

F (hom)

alg

hom

alg′

Figure 2:11 / 28

Page 29: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

That is to say that

fNat :: F NaturalfNat = Succ 1

hom :: Natural -> Stringhom n = timesN n "QUACK" ++ "!"

> alg fNat -- 2> fmap hom fNat -- Succ "QUACK!"> hom $ alg fNat -- "QUACKQUACK!"> alg' $ fmap hom fNat -- "QUACKQUACK!"

hom ◦ alg ≡ alg ′ ◦ fmap hom

F (Natural) F (String)

Natural String

F (hom)

alg

hom

alg′

Figure 3:

12 / 28

Page 30: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Initial, in the category of algebras

An initial algebra for an endofunctor F on a category C is an initial object inthe category of algebras of F.Category of algebras of F:

I Objects: alg, alg’, . . .

I Arrows : structure preserving maps (homomorphisms) from an algebra toanother

Initial:

I There is a unique morphism from the initial algebra to all other algebras.

13 / 28

Page 31: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Initial, in the category of algebras

An initial algebra for an endofunctor F on a category C is an initial object inthe category of algebras of F.Category of algebras of F:

I Objects: alg, alg’, . . .I Arrows : structure preserving maps (homomorphisms) from an algebra toanother

Initial:

I There is a unique morphism from the initial algebra to all other algebras.

13 / 28

Page 32: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Initial, in the category of algebras

An initial algebra for an endofunctor F on a category C is an initial object inthe category of algebras of F.Category of algebras of F:

I Objects: alg, alg’, . . .I Arrows : structure preserving maps (homomorphisms) from an algebra toanother

Initial:

I There is a unique morphism from the initial algebra to all other algebras.

13 / 28

Page 33: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Initial, in the category of algebras

An initial algebra for an endofunctor F on a category C is an initial object inthe category of algebras of F.Category of algebras of F:

I Objects: alg, alg’, . . .I Arrows : structure preserving maps (homomorphisms) from an algebra toanother

Initial:

I There is a unique morphism from the initial algebra to all other algebras.

13 / 28

Page 34: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

What fits?

I The carrier must not “lose” anyinformation, or there is somealgebra that it cannot map to.

I The carrier can’t add information,or the morphism won’t be unique.

I The algebra must have type: FInitF -> InitF

I Lambek’s theorem says that ifthere is an initial object, it isisomorphic to the carrier via thealgebra

I data InitF = InitF (F InitF)

X initial =⇒ F (X ) ∼= X

F InitF F (String)

InitF String

F (hom)

alg

hom

alg′

Figure 4:

14 / 28

Page 35: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

What fits?

I The carrier must not “lose” anyinformation, or there is somealgebra that it cannot map to.

I The carrier can’t add information,or the morphism won’t be unique.

I The algebra must have type: FInitF -> InitF

I Lambek’s theorem says that ifthere is an initial object, it isisomorphic to the carrier via thealgebra

I data InitF = InitF (F InitF)

X initial =⇒ F (X ) ∼= X

F InitF F (String)

InitF String

F (hom)

alg

hom

alg′

Figure 4:

14 / 28

Page 36: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

What fits?

I The carrier must not “lose” anyinformation, or there is somealgebra that it cannot map to.

I The carrier can’t add information,or the morphism won’t be unique.

I The algebra must have type: FInitF -> InitF

I Lambek’s theorem says that ifthere is an initial object, it isisomorphic to the carrier via thealgebra

I data InitF = InitF (F InitF)

X initial =⇒ F (X ) ∼= X

F InitF F (String)

InitF String

F (hom)

alg

hom

alg′

Figure 4:

14 / 28

Page 37: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

What fits?

I The carrier must not “lose” anyinformation, or there is somealgebra that it cannot map to.

I The carrier can’t add information,or the morphism won’t be unique.

I The algebra must have type: FInitF -> InitF

I Lambek’s theorem says that ifthere is an initial object, it isisomorphic to the carrier via thealgebra

I data InitF = InitF (F InitF)

X initial =⇒ F (X ) ∼= X

F InitF F (String)

InitF String

F (hom)

alg

hom

alg′

Figure 4:

14 / 28

Page 38: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

What fits?

I The carrier must not “lose” anyinformation, or there is somealgebra that it cannot map to.

I The carrier can’t add information,or the morphism won’t be unique.

I The algebra must have type: FInitF -> InitF

I Lambek’s theorem says that ifthere is an initial object, it isisomorphic to the carrier via thealgebra

I data InitF = InitF (F InitF)

X initial =⇒ F (X ) ∼= X

F InitF F (String)

InitF String

F (hom)

alg

hom

alg′

Figure 4:

14 / 28

Page 39: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

More generally. . .

data Fix f = Roll { unRoll :: f (Fix f) }type InitF = Fix FRoll :: F InitF -> InitFunRoll :: InitF -> F InitF

fix2 :: InitFfix2 = Roll $ Succ $ Roll $ Succ $ Roll Zero

If this is the initial object in the category of algebras, there must be a uniquearrow from InitF to every algebra:

∀algebras ∃hom : InitF → carrier of algebra

15 / 28

Page 40: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

More generally. . .

data Fix f = Roll { unRoll :: f (Fix f) }type InitF = Fix FRoll :: F InitF -> InitFunRoll :: InitF -> F InitF

fix2 :: InitFfix2 = Roll $ Succ $ Roll $ Succ $ Roll Zero

If this is the initial object in the category of algebras, there must be a uniquearrow from InitF to every algebra:

∀algebras ∃hom : InitF → carrier of algebra

15 / 28

Page 41: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

More generally. . .

data Fix f = Roll { unRoll :: f (Fix f) }type InitF = Fix FRoll :: F InitF -> InitFunRoll :: InitF -> F InitF

fix2 :: InitFfix2 = Roll $ Succ $ Roll $ Succ $ Roll Zero

If this is the initial object in the category of algebras, there must be a uniquearrow from InitF to every algebra:

∀algebras ∃hom : InitF → carrier of algebra

15 / 28

Page 42: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

More generally. . .

data Fix f = Roll { unRoll :: f (Fix f) }type InitF = Fix FRoll :: F InitF -> InitFunRoll :: InitF -> F InitF

fix2 :: InitFfix2 = Roll $ Succ $ Roll $ Succ $ Roll Zero

If this is the initial object in the category of algebras, there must be a uniquearrow from InitF to every algebra:

∀algebras ∃hom : InitF → carrier of algebra15 / 28

Page 43: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The unique homomorphism

∀algebras ∃hom : InitF → carrier of algebra

F InitF F (Natural)

InitF Natural

F (hom)

Roll

hom

alg

Figure 5:

Roll :: F InitF -> InitF

hom :: InitF -> Natural

16 / 28

Page 44: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The unique homomorphism

∀algebras ∃hom : InitF → carrier of algebra

F InitF F (Natural)

InitF Natural

F (hom)

Roll

hom

alg

Figure 5:

Roll :: F InitF -> InitF

hom :: InitF -> Natural

16 / 28

Page 45: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The unique homomorphism

∀algebras ∃hom : InitF → carrier of algebra

F InitF F (Natural)

InitF Natural

F (hom)

Roll

hom

alg

Figure 5:

Roll :: F InitF -> InitF

hom :: InitF -> Natural

16 / 28

Page 46: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The unique homomorphism

∀algebras ∃hom : InitF → carrier of algebra

F InitF F (Natural)

InitF Natural

F (hom)

unRoll

hom

alg

Figure 6:

unRoll :: InitF -> F InitF

hom :: InitF -> Naturalhom = alg . fmap hom . unRoll

cata :: Functor f=> (f a -> a) -> Fix f -> a

cata alg =alg . fmap (cata alg) . unRoll

hom = cata alg

17 / 28

Page 47: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The unique homomorphism

∀algebras ∃hom : InitF → carrier of algebra

F InitF F (Natural)

InitF Natural

F (hom)

unRoll

hom

alg

Figure 6:

unRoll :: InitF -> F InitF

hom :: InitF -> Naturalhom = alg . fmap hom . unRoll

cata :: Functor f=> (f a -> a) -> Fix f -> a

cata alg =alg . fmap (cata alg) . unRoll

hom = cata alg

17 / 28

Page 48: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The unique homomorphism

∀algebras ∃hom : InitF → carrier of algebra

F InitF F (Natural)

InitF Natural

F (hom)

unRoll

hom

alg

Figure 6:

unRoll :: InitF -> F InitF

hom :: InitF -> Naturalhom = alg . fmap hom . unRoll

cata :: Functor f=> (f a -> a) -> Fix f -> a

cata alg =alg . fmap (cata alg) . unRoll

hom = cata alg 17 / 28

Page 49: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Evaluation of cata

cata :: Functor f=> (f a -> a) -> Fix f -> a

cata alg =alg . fmap (cata alg) . unRoll

alg :: F Natural -> Naturalalg Zero = 0alg (Succ n) = n + 1

cata alg (Roll $ Succ $ Roll Zero)

alg $ fmap (cata alg) (Succ $ Roll Zero)

alg $ Succ $ cata alg $ Roll Zero

alg $ Succ $ alg $ fmap (cata alg) Zero

alg $ Succ $ alg $ Zero

18 / 28

Page 50: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Evaluation of cata

cata :: Functor f=> (f a -> a) -> Fix f -> a

cata alg =alg . fmap (cata alg) . unRoll

alg :: F Natural -> Naturalalg Zero = 0alg (Succ n) = n + 1

cata alg (Roll $ Succ $ Roll Zero)

alg $ fmap (cata alg) (Succ $ Roll Zero)

alg $ Succ $ cata alg $ Roll Zero

alg $ Succ $ alg $ fmap (cata alg) Zero

alg $ Succ $ alg $ Zero

18 / 28

Page 51: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Evaluation of cata

cata :: Functor f=> (f a -> a) -> Fix f -> a

cata alg =alg . fmap (cata alg) . unRoll

alg :: F Natural -> Naturalalg Zero = 0alg (Succ n) = n + 1

cata alg (Roll $ Succ $ Roll Zero)

alg $ fmap (cata alg) (Succ $ Roll Zero)

alg $ Succ $ cata alg $ Roll Zero

alg $ Succ $ alg $ fmap (cata alg) Zero

alg $ Succ $ alg $ Zero

18 / 28

Page 52: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Evaluation of cata

cata :: Functor f=> (f a -> a) -> Fix f -> a

cata alg =alg . fmap (cata alg) . unRoll

alg :: F Natural -> Naturalalg Zero = 0alg (Succ n) = n + 1

cata alg (Roll $ Succ $ Roll Zero)

alg $ fmap (cata alg) (Succ $ Roll Zero)

alg $ Succ $ cata alg $ Roll Zero

alg $ Succ $ alg $ fmap (cata alg) Zero

alg $ Succ $ alg $ Zero

18 / 28

Page 53: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Evaluation of cata

cata :: Functor f=> (f a -> a) -> Fix f -> a

cata alg =alg . fmap (cata alg) . unRoll

alg :: F Natural -> Naturalalg Zero = 0alg (Succ n) = n + 1

cata alg (Roll $ Succ $ Roll Zero)

alg $ fmap (cata alg) (Succ $ Roll Zero)

alg $ Succ $ cata alg $ Roll Zero

alg $ Succ $ alg $ fmap (cata alg) Zero

alg $ Succ $ alg $ Zero

18 / 28

Page 54: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Evaluation of cata

cata :: Functor f=> (f a -> a) -> Fix f -> a

cata alg =alg . fmap (cata alg) . unRoll

alg :: F Natural -> Naturalalg Zero = 0alg (Succ n) = n + 1

cata alg (Roll $ Succ $ Roll Zero)

alg $ fmap (cata alg) (Succ $ Roll Zero)

alg $ Succ $ cata alg $ Roll Zero

alg $ Succ $ alg $ fmap (cata alg) Zero

alg $ Succ $ alg $ Zero18 / 28

Page 55: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

This is recursion in a general sense

data Nat a = Succ a | Zero

data String a = Cons Char a | End

data BinaryTree a = Branch a a | Tip

data RoseTree a = Branches [a] | Tip

data Group a = Action a a | Inv a | Unit

19 / 28

Page 56: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

This is recursion in a general sense

data Nat a = Succ a | Zero

data String a = Cons Char a | End

data BinaryTree a = Branch a a | Tip

data RoseTree a = Branches [a] | Tip

data Group a = Action a a | Inv a | Unit

19 / 28

Page 57: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

This is recursion in a general sense

data Nat a = Succ a | Zero

data String a = Cons Char a | End

data BinaryTree a = Branch a a | Tip

data RoseTree a = Branches [a] | Tip

data Group a = Action a a | Inv a | Unit

19 / 28

Page 58: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

This is recursion in a general sense

data Nat a = Succ a | Zero

data String a = Cons Char a | End

data BinaryTree a = Branch a a | Tip

data RoseTree a = Branches [a] | Tip

data Group a = Action a a | Inv a | Unit

19 / 28

Page 59: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

This is recursion in a general sense

data Nat a = Succ a | Zero

data String a = Cons Char a | End

data BinaryTree a = Branch a a | Tip

data RoseTree a = Branches [a] | Tip

data Group a = Action a a | Inv a | Unit

19 / 28

Page 60: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Hutton’s razor - final taglessclass Calculator a where

lit :: Int -> aadd :: a -> a -> amult :: a -> a -> a

instance Calculator Int wherelit = idadd = (+)mult = (*)

instance Calculator String wherelit = showadd s1 s2 = s1 ++ " + " ++ s2mult s1 s2 = s1 ++ " x " ++ s2

20 / 28

Page 61: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Hutton’s razor - F algebra

data Calculator a = Lit Int | Add a a | Mult a a deriving Functor

evalAlg :: Calculator Int -> IntevalAlg (Lit i) = ievalAlg (Add i1 i2) = i1 + i2evalAlg (Mult i1 i2) = i1 * i2

ppAlg :: Calculator String -> StringppAlg (Lit i) = show ippAlg (Add s1 s2) = s1 ++ " + " ++ s2ppAlg (Mult s1 s2) = s1 ++ " x " ++ s2

pp :: Fix Calculator -> Stringpp = cata ppAlg

21 / 28

Page 62: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Damn the torpedos, flip the arrows

alg :: F Natural -> Naturalalg Zero = 0alg (Succ n) = n + 1

coalg :: Natural -> F Naturalcoalg 0 = Zerocoalg n = Succ (n - 1)

For a category C and endofunctor F aco-algebra of F is an object X in C anda morphism:

coalg : X → F (X )

22 / 28

Page 63: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Damn the torpedos, flip the arrows

alg :: F Natural -> Naturalalg Zero = 0alg (Succ n) = n + 1

coalg :: Natural -> F Naturalcoalg 0 = Zerocoalg n = Succ (n - 1)

For a category C and endofunctor F aco-algebra of F is an object X in C anda morphism:

coalg : X → F (X )

22 / 28

Page 64: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Damn the torpedos, flip the arrows

alg :: F Natural -> Naturalalg Zero = 0alg (Succ n) = n + 1

coalg :: Natural -> F Naturalcoalg 0 = Zerocoalg n = Succ (n - 1)

For a category C and endofunctor F aco-algebra of F is an object X in C anda morphism:

coalg : X → F (X )

22 / 28

Page 65: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Morphisms on coalgebras: co all of the things!

F (Natural) F (String)

Natural String

F (hom)

alg

hom

alg′

Figure 7:

F (Natural) F (String)

Natural String

F (hom′)

coalg

hom′

coalg′

Figure 8:

23 / 28

Page 66: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Morphisms on coalgebras: co all of the things!

F (Natural) F (String)

Natural String

F (hom)

alg

hom

alg′

Figure 7:

F (Natural) F (String)

Natural String

F (hom′)

coalg

hom′

coalg′

Figure 8:

23 / 28

Page 67: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

E.g.

F (Natural) F (String)

Natural String

F (hom′)

coalg

hom′

coalg′

Figure 9:

coalg :: Natural -> F Naturalcoalg 0 = Zerocoalg n = Succ (n - 1)

coalg' :: String -> F Stringcoalg' "!" =

Zerocoalg' ('Q':'U':'A':'C':'K':xs) =

Succ xs

24 / 28

Page 68: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

E.g.

F (Natural) F (String)

Natural String

F (hom′)

coalg

hom′

coalg′

Figure 9:

coalg :: Natural -> F Naturalcoalg 0 = Zerocoalg n = Succ (n - 1)

coalg' :: String -> F Stringcoalg' "!" =

Zerocoalg' ('Q':'U':'A':'C':'K':xs) =

Succ xs

24 / 28

Page 69: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

E.g.

F (Natural) F (String)

Natural String

F (hom′)

coalg

hom′

coalg′

Figure 10:

hom :: Natural -> Stringhom n = timesN n "QUACK" ++ "!"

hom' :: String -> Naturalhom' str =

(fromIntegral (length str) - 1)`div` 5

> (hom' "QUACKQUACK!", coalg' "QUACKQUACK!")(2, Succ "QUACK!")> (coalg $ hom' "QUACKQUACK!", fmap hom' $ coalg' "QUACKQUACK!")(Succ 1, Succ 1)

25 / 28

Page 70: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

E.g.

F (Natural) F (String)

Natural String

F (hom′)

coalg

hom′

coalg′

Figure 10:

hom :: Natural -> Stringhom n = timesN n "QUACK" ++ "!"

hom' :: String -> Naturalhom' str =

(fromIntegral (length str) - 1)`div` 5

> (hom' "QUACKQUACK!", coalg' "QUACKQUACK!")(2, Succ "QUACK!")> (coalg $ hom' "QUACKQUACK!", fmap hom' $ coalg' "QUACKQUACK!")(Succ 1, Succ 1)

25 / 28

Page 71: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

E.g.

F (Natural) F (String)

Natural String

F (hom′)

coalg

hom′

coalg′

Figure 10:

hom :: Natural -> Stringhom n = timesN n "QUACK" ++ "!"

hom' :: String -> Naturalhom' str =

(fromIntegral (length str) - 1)`div` 5

> (hom' "QUACKQUACK!", coalg' "QUACKQUACK!")(2, Succ "QUACK!")> (coalg $ hom' "QUACKQUACK!", fmap hom' $ coalg' "QUACKQUACK!")(Succ 1, Succ 1) 25 / 28

Page 72: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The unique homomorphism

∀algebras ∃hom : carrier of algebra → TermF

F TermF F (Natural)

TermF Natural

F (hom′)

unRoll

hom′

coalg

Figure 11:

type TermF = InitFunRoll :: TermF -> F TermF

hom' :: Natural -> TermF

26 / 28

Page 73: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The unique homomorphism

∀algebras ∃hom : carrier of algebra → TermF

F TermF F (Natural)

TermF Natural

F (hom′)

unRoll

hom′

coalg

Figure 11:

type TermF = InitFunRoll :: TermF -> F TermF

hom' :: Natural -> TermF

26 / 28

Page 74: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The unique homomorphism

∀algebras ∃hom : carrier of algebra → TermF

F TermF F (Natural)

TermF Natural

F (hom′)

unRoll

hom′

coalg

Figure 11:

type TermF = InitFunRoll :: TermF -> F TermF

hom' :: Natural -> TermF

26 / 28

Page 75: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The unique homomorphism

∀algebras ∃hom : carrier of algebra → TermF

F TermF F (Natural)

TermF Natural

F (hom′)

Roll

hom′

coalg

Figure 12:

Roll :: F TermF -> TermF

hom' :: Natural -> TermF

hom' = Roll . fmap hom' . coalg

ana :: Functor f=> (a -> f a) -> a -> Fix f

ana coalg =Roll . fmap (ana coalg) . f

hom' = ana coalg

27 / 28

Page 76: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The unique homomorphism

∀algebras ∃hom : carrier of algebra → TermF

F TermF F (Natural)

TermF Natural

F (hom′)

Roll

hom′

coalg

Figure 12:

Roll :: F TermF -> TermF

hom' :: Natural -> TermF

hom' = Roll . fmap hom' . coalg

ana :: Functor f=> (a -> f a) -> a -> Fix f

ana coalg =Roll . fmap (ana coalg) . f

hom' = ana coalg

27 / 28

Page 77: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The unique homomorphism

∀algebras ∃hom : carrier of algebra → TermF

F TermF F (Natural)

TermF Natural

F (hom′)

Roll

hom′

coalg

Figure 12:

Roll :: F TermF -> TermF

hom' :: Natural -> TermF

hom' = Roll . fmap hom' . coalg

ana :: Functor f=> (a -> f a) -> a -> Fix f

ana coalg =Roll . fmap (ana coalg) . f

hom' = ana coalg

27 / 28

Page 78: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

The unique homomorphism

∀algebras ∃hom : carrier of algebra → TermF

F TermF F (Natural)

TermF Natural

F (hom′)

Roll

hom′

coalg

Figure 12:

Roll :: F TermF -> TermF

hom' :: Natural -> TermF

hom' = Roll . fmap hom' . coalg

ana :: Functor f=> (a -> f a) -> a -> Fix f

ana coalg =Roll . fmap (ana coalg) . f

hom' = ana coalg27 / 28

Page 79: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Whilst we’re here

cata :: Functor f => (f a -> a) -> InitF -> acata alg = alg . fmap (cata alg) . unRoll

ana :: Functor f => (a -> f a) -> a -> TermFana coalg = Roll . fmap (ana coalg) . coalg

cata alg' $ ana coalg' $ hom 3> "QUACKQUACKQUACK!"

hylo :: Functor f => (f b -> b) -> (a -> f a) -> a -> bhylo alg coalg = alg . fmap (hylo alg coalg) . coalg

hylo alg' coalg' $ hom 3> "QUACKQUACKQUACK!"

28 / 28

Page 80: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Whilst we’re here

cata :: Functor f => (f a -> a) -> InitF -> acata alg = alg . fmap (cata alg) . unRoll

ana :: Functor f => (a -> f a) -> a -> TermFana coalg = Roll . fmap (ana coalg) . coalg

cata alg' $ ana coalg' $ hom 3> "QUACKQUACKQUACK!"

hylo :: Functor f => (f b -> b) -> (a -> f a) -> a -> bhylo alg coalg = alg . fmap (hylo alg coalg) . coalg

hylo alg' coalg' $ hom 3> "QUACKQUACKQUACK!"

28 / 28

Page 81: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Whilst we’re here

cata :: Functor f => (f a -> a) -> InitF -> acata alg = alg . fmap (cata alg) . unRoll

ana :: Functor f => (a -> f a) -> a -> TermFana coalg = Roll . fmap (ana coalg) . coalg

cata alg' $ ana coalg' $ hom 3> "QUACKQUACKQUACK!"

hylo :: Functor f => (f b -> b) -> (a -> f a) -> a -> bhylo alg coalg = alg . fmap (hylo alg coalg) . coalg

hylo alg' coalg' $ hom 3> "QUACKQUACKQUACK!"

28 / 28

Page 82: homomorphism from an initial algebra into some other ...code.ouroborus.net/fp-syd/past/2017/2017-08-Marie-FAlgebras.pdfTheroadmap Theaimistounderstandthis: In category theory, the

Whilst we’re here

cata :: Functor f => (f a -> a) -> InitF -> acata alg = alg . fmap (cata alg) . unRoll

ana :: Functor f => (a -> f a) -> a -> TermFana coalg = Roll . fmap (ana coalg) . coalg

cata alg' $ ana coalg' $ hom 3> "QUACKQUACKQUACK!"

hylo :: Functor f => (f b -> b) -> (a -> f a) -> a -> bhylo alg coalg = alg . fmap (hylo alg coalg) . coalg

hylo alg' coalg' $ hom 3> "QUACKQUACKQUACK!"

28 / 28