Category Theory & Functional Data Abstraction Brandon Shapiro - - PowerPoint PPT Presentation

category theory
SMART_READER_LITE
LIVE PREVIEW

Category Theory & Functional Data Abstraction Brandon Shapiro - - PowerPoint PPT Presentation

Category Theory & Functional Data Abstraction Brandon Shapiro Math 100b Brandon Shapiro Category Theory Categories A category C is a collection of objects with arrows (often called morphisms) pointing between them Hom C ( X , Y ) is the


slide-1
SLIDE 1

Category Theory

& Functional Data Abstraction Brandon Shapiro Math 100b

Brandon Shapiro Category Theory

slide-2
SLIDE 2

Categories

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

Brandon Shapiro Category Theory

slide-3
SLIDE 3

Categories

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

Brandon Shapiro Category Theory

slide-4
SLIDE 4

Examples

Set is the category of all sets, with functions between sets as the morphisms All groups also form a category, Grp, with group homomorphisms as its morphisms Ring and R-mod for some ring R can be formed with ring and module homomorphisms as morphisms A subcategory of category C is a category with all of its

  • bjects and morphisms contained in C

Finite sets and the functions between them form a subcategory of Set, and abelian groups are a subcategory

  • f Grp. Fields form a subcategory of the category of

commutative rings, which is itself a subcategory of Ring

Brandon Shapiro Category Theory

slide-5
SLIDE 5

Examples

Set is the category of all sets, with functions between sets as the morphisms All groups also form a category, Grp, with group homomorphisms as its morphisms Ring and R-mod for some ring R can be formed with ring and module homomorphisms as morphisms A subcategory of category C is a category with all of its

  • bjects and morphisms contained in C

Finite sets and the functions between them form a subcategory of Set, and abelian groups are a subcategory

  • f Grp. Fields form a subcategory of the category of

commutative rings, which is itself a subcategory of Ring

Brandon Shapiro Category Theory

slide-6
SLIDE 6

Functors

A functor is a structure preserving map between categories For categories C and D, a covariant functor F : C → D sends the objects of C to objects in D, and sends the morphisms in C to morphisms in D If 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

slide-7
SLIDE 7

Functors

A functor is a structure preserving map between categories For categories C and D, a covariant functor F : C → D sends the objects of C to objects in D, and sends the morphisms in C to morphisms in D If 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

slide-8
SLIDE 8

Examples

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

Brandon Shapiro Category Theory

slide-9
SLIDE 9

Examples

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

Brandon Shapiro Category Theory

slide-10
SLIDE 10

Date Types

In computer programming languages, a data type is a set

  • f elements that can be represented by a computer (finitely

in binary) in the same way Two of the most common data types are Z and R Real-world computing has constraints on memory, etc. Mathematically, a data type can be treated just as a set

Brandon Shapiro Category Theory

slide-11
SLIDE 11

Maybe

Set has sets as objects and functions as morphisms Maybe : Set → Set Maybe(A) = A ∪ {Nothing} Maybe lets us define ‘safe’ versions of partial functions f : R → Maybe(R) f(0) = Nothing f(x) = 1/x (x = 0)

Brandon Shapiro Category Theory

slide-12
SLIDE 12

Maybe

Set has sets as objects and functions as morphisms Maybe : Set → Set Maybe(A) = A ∪ {Nothing} Maybe lets us define ‘safe’ versions of partial functions f : R → Maybe(R) f(0) = Nothing f(x) = 1/x (x = 0)

Brandon Shapiro Category Theory

slide-13
SLIDE 13

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) = Nothing Mmap(f)(x) = f(x) (x = Nothing) Mmap(1A) = 1Maybe(A) Mmap(f ◦ g) = Mmap(f) ◦ Mmap(g)

Brandon Shapiro Category Theory

slide-14
SLIDE 14

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) = Nothing Mmap(f)(x) = f(x) (x = Nothing) Mmap(1A) = 1Maybe(A) Mmap(f ◦ g) = Mmap(f) ◦ Mmap(g)

Brandon Shapiro Category Theory

slide-15
SLIDE 15

List

List sends a set A to the set of ‘lists’ of elements in A List : Set → Set List(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

slide-16
SLIDE 16

List

List sends a set A to the set of ‘lists’ of elements in A List : Set → Set List(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

slide-17
SLIDE 17

List

List sends a set A to the set of ‘lists’ of elements in A List : Set → Set List(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

slide-18
SLIDE 18

List

List is an endofunctor on Set Needs 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

slide-19
SLIDE 19

List

List is an endofunctor on Set Needs 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

slide-20
SLIDE 20

List

List is an endofunctor on Set Needs 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

slide-21
SLIDE 21

List

List is an endofunctor on Set Needs 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

slide-22
SLIDE 22

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

slide-23
SLIDE 23

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

slide-24
SLIDE 24

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

slide-25
SLIDE 25

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

slide-26
SLIDE 26

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

slide-27
SLIDE 27

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

slide-28
SLIDE 28

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

slide-29
SLIDE 29

Sources & Images

Sources Abstract Algebra by Dummit and Foote http://en.wikibooks.org/wiki/Haskell/Category_theory Lectures by and conversations with Kenny Foner Images https://bartoszmilewski.files.wordpress.com/2014/10/img_1330.jpg http://shuklan.com/haskell/L12_files/category.png http://ncatlab.org/nlab/files/functor.jpg http://www.programmingtunes.com/wp- content/uploads/2013/04/Types-of-Data.jpg https://lh3.googleusercontent.com/proxy/W- kz6vWx9ntZrS2FCduApSQ0E-YsddspOrfnWyKP2J- 49Uu8_5ahu- lOEfHLmT7w2lZMvQ_vhDGxCkqHlMo1C_0VCkrCFeSzfvtW4PjDjoXA =w426-h310

Brandon Shapiro Category Theory