category theory - & functional data abstraction · 2017-11-04 · grp and to set a functor from...

29
Category Theory & Functional Data Abstraction Brandon Shapiro Math 100b Brandon Shapiro Category Theory

Upload: others

Post on 15-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Category Theory& Functional Data Abstraction

Brandon Shapiro

Math 100b

Brandon Shapiro Category Theory

Page 2: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Categories

A category C is a collection of objects with arrows (oftencalled morphisms) pointing between themHomC(X ,Y ) is the set of morphisms in C from X to YIf f ∈ HomC(X ,Y ) and g ∈ HomC(Y ,Z ), then there exists amorphism f ◦ g in HomC(X ,Z ) (composition is associative)For every object X in C, there is an identity morphism1X ∈ HomC(X ,X ) (f ◦ 1X = f and 1X ◦ g = g)

Brandon Shapiro Category Theory

Page 3: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Categories

A category C is a collection of objects with arrows (oftencalled morphisms) pointing between themHomC(X ,Y ) is the set of morphisms in C from X to YIf f ∈ HomC(X ,Y ) and g ∈ HomC(Y ,Z ), then there exists amorphism f ◦ g in HomC(X ,Z ) (composition is associative)For every object X in C, there is an identity morphism1X ∈ HomC(X ,X ) (f ◦ 1X = f and 1X ◦ g = g)

Brandon Shapiro Category Theory

Page 4: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Examples

Set is the category of all sets, with functions between setsas the morphismsAll groups also form a category, Grp, with grouphomomorphisms as its morphismsRing and R-mod for some ring R can be formed with ringand module homomorphisms as morphismsA subcategory of category C is a category with all of itsobjects and morphisms contained in CFinite sets and the functions between them form asubcategory of Set, and abelian groups are a subcategoryof Grp. Fields form a subcategory of the category ofcommutative rings, which is itself a subcategory of Ring

Brandon Shapiro Category Theory

Page 5: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Examples

Set is the category of all sets, with functions between setsas the morphismsAll groups also form a category, Grp, with grouphomomorphisms as its morphismsRing and R-mod for some ring R can be formed with ringand module homomorphisms as morphismsA subcategory of category C is a category with all of itsobjects and morphisms contained in CFinite sets and the functions between them form asubcategory of Set, and abelian groups are a subcategoryof Grp. Fields form a subcategory of the category ofcommutative rings, which is itself a subcategory of Ring

Brandon Shapiro Category Theory

Page 6: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Functors

A functor is a structure preserving map between categoriesFor categories C and D, a covariant functor F : C→ Dsends the objects of C to objects in D, and sends themorphisms in C to morphisms in DIf f ∈ HomC(X ,Y ), F(f ) ∈ HomD(F(X ),F(Y ))

F(1X ) = 1F(X), F(f ◦ g) = F(f ) ◦ F(g)

Brandon Shapiro Category Theory

Page 7: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Functors

A functor is a structure preserving map between categoriesFor categories C and D, a covariant functor F : C→ Dsends the objects of C to objects in D, and sends themorphisms in C to morphisms in DIf f ∈ HomC(X ,Y ), F(f ) ∈ HomD(F(X ),F(Y ))

F(1X ) = 1F(X), F(f ◦ g) = F(f ) ◦ F(g)

Brandon Shapiro Category Theory

Page 8: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Examples

The identity functor from C to C sends every object andmorphism in C to itself.Let F be a map from Grp to Set sending groups andhomomorphisms in Grp to themselves in Set. F is afunctor from Grp to Set called the ‘forgetful functor’Similarly, forgetful functors exist from Ring and R-mod toGrp and to SetA functor from a category to itself is called an endofunctorThe identity functor is an endofunctor

Brandon Shapiro Category Theory

Page 9: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Examples

The identity functor from C to C sends every object andmorphism in C to itself.Let F be a map from Grp to Set sending groups andhomomorphisms in Grp to themselves in Set. F is afunctor from Grp to Set called the ‘forgetful functor’Similarly, forgetful functors exist from Ring and R-mod toGrp and to SetA functor from a category to itself is called an endofunctorThe identity functor is an endofunctor

Brandon Shapiro Category Theory

Page 10: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Date Types

In computer programming languages, a data type is a setof elements that can be represented by a computer (finitelyin binary) in the same wayTwo of the most common data types are Z and RReal-world computing has constraints on memory, etc.Mathematically, a data type can be treated just as a set

Brandon Shapiro Category Theory

Page 11: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Maybe

Set has sets as objects and functions as morphisms

Maybe : Set→ SetMaybe(A) = A ∪ {Nothing}

Maybe lets us define ‘safe’ versions of partial functions

f : R→Maybe(R)f (0) = Nothing

f (x) = 1/x (x 6= 0)

Brandon Shapiro Category Theory

Page 12: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Maybe

Set has sets as objects and functions as morphisms

Maybe : Set→ SetMaybe(A) = A ∪ {Nothing}

Maybe lets us define ‘safe’ versions of partial functions

f : R→Maybe(R)f (0) = Nothing

f (x) = 1/x (x 6= 0)

Brandon Shapiro Category Theory

Page 13: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Maybe

Maybe is a functor from Set to Set (endofunctor)Needs a mapping for the morphisms (functions)

Mmap : Hom(A,B)→ Hom(Maybe(A),Maybe(B))Mmap(f )(Nothing) = NothingMmap(f )(x) = f (x) (x 6= Nothing)

Mmap(1A) = 1Maybe(A)

Mmap(f ◦ g) =Mmap(f ) ◦Mmap(g)

Brandon Shapiro Category Theory

Page 14: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Maybe

Maybe is a functor from Set to Set (endofunctor)Needs a mapping for the morphisms (functions)

Mmap : Hom(A,B)→ Hom(Maybe(A),Maybe(B))Mmap(f )(Nothing) = NothingMmap(f )(x) = f (x) (x 6= Nothing)

Mmap(1A) = 1Maybe(A)

Mmap(f ◦ g) =Mmap(f ) ◦Mmap(g)

Brandon Shapiro Category Theory

Page 15: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

List

List sends a set A to the set of ‘lists’ of elements in A

List : Set→ SetList(A) = {()} ∪ {(x , xlist)|x ∈ A, xlist ∈ List(A)}

() is called the empty list

(1, (2, (3, (4, ())))) ∈ List(Z)(1/2, (Nothing, (1/4, ()))) ∈ List(Maybe(Q))

(1,2,3,4) ∈ List(Z)

Brandon Shapiro Category Theory

Page 16: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

List

List sends a set A to the set of ‘lists’ of elements in A

List : Set→ SetList(A) = {()} ∪ {(x , xlist)|x ∈ A, xlist ∈ List(A)}

() is called the empty list

(1, (2, (3, (4, ())))) ∈ List(Z)(1/2, (Nothing, (1/4, ()))) ∈ List(Maybe(Q))

(1,2,3,4) ∈ List(Z)

Brandon Shapiro Category Theory

Page 17: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

List

List sends a set A to the set of ‘lists’ of elements in A

List : Set→ SetList(A) = {()} ∪ {(x , xlist)|x ∈ A, xlist ∈ List(A)}

() is called the empty list

(1, (2, (3, (4, ())))) ∈ List(Z)(1/2, (Nothing, (1/4, ()))) ∈ List(Maybe(Q))

(1,2,3,4) ∈ List(Z)

Brandon Shapiro Category Theory

Page 18: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

List

List is an endofunctor on SetNeeds a mapping for the morphisms (functions)

Lmap : Hom(A,B)→ Hom(List(A),List(B))Lmap(f )(()) = ()

Lmap(f )((x , xlist)) = (f (x),Lmap(f )(xlist))

For f (x) = x2, Lmap(f )((1,2,3,4)) = (1,4,9,16)

Clearly satisfies functor laws (identity and composition)

Brandon Shapiro Category Theory

Page 19: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

List

List is an endofunctor on SetNeeds a mapping for the morphisms (functions)

Lmap : Hom(A,B)→ Hom(List(A),List(B))Lmap(f )(()) = ()

Lmap(f )((x , xlist)) = (f (x),Lmap(f )(xlist))

For f (x) = x2, Lmap(f )((1,2,3,4)) = (1,4,9,16)

Clearly satisfies functor laws (identity and composition)

Brandon Shapiro Category Theory

Page 20: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

List

List is an endofunctor on SetNeeds a mapping for the morphisms (functions)

Lmap : Hom(A,B)→ Hom(List(A),List(B))Lmap(f )(()) = ()

Lmap(f )((x , xlist)) = (f (x),Lmap(f )(xlist))

For f (x) = x2, Lmap(f )((1,2,3,4)) = (1,4,9,16)

Clearly satisfies functor laws (identity and composition)

Brandon Shapiro Category Theory

Page 21: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

List

List is an endofunctor on SetNeeds a mapping for the morphisms (functions)

Lmap : Hom(A,B)→ Hom(List(A),List(B))Lmap(f )(()) = ()

Lmap(f )((x , xlist)) = (f (x),Lmap(f )(xlist))

For f (x) = x2, Lmap(f )((1,2,3,4)) = (1,4,9,16)

Clearly satisfies functor laws (identity and composition)

Brandon Shapiro Category Theory

Page 22: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Applicative Functors

What does an endofunctor on Set to do a set of functions?An applicative functor is a functor with a ‘splat’ function

Fsplat : F(Hom(A,B))→ Hom(F(A),F(B))

Fsplat can also be defined as a binary function

Fsplat : F(Hom(A,B))×F(A)→ F(B)

There are rules applicative functors must follow

Brandon Shapiro Category Theory

Page 23: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Applicative Functors

What does an endofunctor on Set to do a set of functions?An applicative functor is a functor with a ‘splat’ function

Fsplat : F(Hom(A,B))→ Hom(F(A),F(B))

Fsplat can also be defined as a binary function

Fsplat : F(Hom(A,B))×F(A)→ F(B)

There are rules applicative functors must follow

Brandon Shapiro Category Theory

Page 24: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Applicative Functors

What does an endofunctor on Set to do a set of functions?An applicative functor is a functor with a ‘splat’ function

Fsplat : F(Hom(A,B))→ Hom(F(A),F(B))

Fsplat can also be defined as a binary function

Fsplat : F(Hom(A,B))×F(A)→ F(B)

There are rules applicative functors must follow

Brandon Shapiro Category Theory

Page 25: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Applicative Functors

What does an endofunctor on Set to do a set of functions?An applicative functor is a functor with a ‘splat’ function

Fsplat : F(Hom(A,B))→ Hom(F(A),F(B))

Fsplat can also be defined as a binary function

Fsplat : F(Hom(A,B))×F(A)→ F(B)

There are rules applicative functors must follow

Brandon Shapiro Category Theory

Page 26: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Applicative Functors

Maybe is an applicative functor

Msplat :Maybe(Hom(A,B))×Maybe(A)→Maybe(B)Msplat(Nothing)(_) =Msplat(_)(Nothing) = Nothing

Msplat(f )(x) = f (x)

List is an applicative functor

Lsplat : List(Hom(A,B))× List(A)→ List(B)Lsplat1(())(_) = Lsplat1(_)(()) = ()

Lsplat1((f , flist))((x , xlist)) = (f (x),Lsplat1(flist)(xlist))

Could List be an applicative functor in any other ways?

Brandon Shapiro Category Theory

Page 27: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Applicative Functors

Maybe is an applicative functor

Msplat :Maybe(Hom(A,B))×Maybe(A)→Maybe(B)Msplat(Nothing)(_) =Msplat(_)(Nothing) = Nothing

Msplat(f )(x) = f (x)

List is an applicative functor

Lsplat : List(Hom(A,B))× List(A)→ List(B)Lsplat1(())(_) = Lsplat1(_)(()) = ()

Lsplat1((f , flist))((x , xlist)) = (f (x),Lsplat1(flist)(xlist))

Could List be an applicative functor in any other ways?

Brandon Shapiro Category Theory

Page 28: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Applicative Functors

Maybe is an applicative functor

Msplat :Maybe(Hom(A,B))×Maybe(A)→Maybe(B)Msplat(Nothing)(_) =Msplat(_)(Nothing) = Nothing

Msplat(f )(x) = f (x)

List is an applicative functor

Lsplat : List(Hom(A,B))× List(A)→ List(B)Lsplat1(())(_) = Lsplat1(_)(()) = ()

Lsplat1((f , flist))((x , xlist)) = (f (x),Lsplat1(flist)(xlist))

Could List be an applicative functor in any other ways?

Brandon Shapiro Category Theory

Page 29: Category Theory - & Functional Data Abstraction · 2017-11-04 · Grp and to Set A functor from a category to itself is called an endofunctor The identity functor is an endofunctor

Sources & Images

Sources

Abstract Algebra by Dummit and Footehttp://en.wikibooks.org/wiki/Haskell/Category_theoryLectures by and conversations with Kenny Foner

Images

https://bartoszmilewski.files.wordpress.com/2014/10/img_1330.jpghttp://shuklan.com/haskell/L12_files/category.pnghttp://ncatlab.org/nlab/files/functor.jpghttp://www.programmingtunes.com/wp-content/uploads/2013/04/Types-of-Data.jpghttps://lh3.googleusercontent.com/proxy/W-kz6vWx9ntZrS2FCduApSQ0E-YsddspOrfnWyKP2J-49Uu8_5ahu-lOEfHLmT7w2lZMvQ_vhDGxCkqHlMo1C_0VCkrCFeSzfvtW4PjDjoXAulQ-=w426-h310 Brandon Shapiro Category Theory