SlideShare une entreprise Scribd logo
1  sur  29
Télécharger pour lire hors ligne
Just Enough Category Theory
for Haskell (part 1)
Heejong Ahn
(github.com/heejongahn)
Tabel of Contents
• Purpose and Focus
• Category
• Functor
Purpose and Focus
Purpose
• “Do I have to read this thick math book with stubborn, dull cover
thoroughly, just to write a program in Haskell?”
• You don’t.
• We’ve already gone through 5 weeks of exercises without any book!
• Yet, as Haskell borrowed a lot of its core concepts from category
theory, some knowledge on it might help you a lot.
Focus
• I’ll try to avoid analogues, which is often used to give an intuition.
• That’s because sometimes those keep you from getting actual, clear
idea about a concept.
• Instead, I’d rather stick to mathematical terms and laws.
Disclaimer
• I’ve never been excellent in math.
• Haven’t been really passionate about it, neither.
• Though I’ve put a huge amount of time, there will be lots of unclear,
even wrong spots throughout the slides.
• If you find any, please feel free to interrupt and correct me ☺
Category
Category
Category is just a collection which has:
• A collection of objects
• A collection of morphisms
• A notion of composition
X Y
Z
f
g
g ◦ f
Category
Category is just a collection which has:
• A collection of objects
• A collection of morphisms
• A notion of composition
X Y
Z
f
g
g ◦ f
Category
Category is just a collection which has:
• A collection of objects
• A collection of morphisms
• A notion of composition
X Y
Z
f
g
g ◦ f
source object X
target object Y
Category
Category is just a collection which has:
• A collection of objects
• A collection of morphisms
• A notion of composition
X Y
Z
f
g
g ◦ f
Category Example
• Category of sets(Set) is a category.
• Set has a collection of objects, which is, well, all sets.
• Set has a collection of morphisms, which is a collection of functions
from a set to another set.
• Set has a notion of composition, which is just a function composition.
Category Example (cont’d)
• Category of groups(Grp) is also a category.
• Grp has a collection of objects, which is, again, all groups.
• Grp has a collection of morphisms, which is a collection of functions
which preserves group operation (the group homomorphism)
• Grp has a notion of composition, which is just a function composition.
Category Laws
Categories must meet these requirements :
• Composition of mophisms should be associative
• For every object in a category, there must be an identity morphism
Hask
• Hask, the category of Haskell types and functions, is our main topic.
• Speaking of category, we’ve heard that every category must have…
• Objects, morphisms, and composition!
Hask (cont’d)
• Objects : Haskell types
• Morphisms : Haskell functions
• Composition : (.)
• We know haskell functions are associative, and we have id.
• On top of that, we define two functions f and g as the same
morphism if f x = g x for all x. (Why? Is it really needed?)
Functor
Functor
• “In mathematics, a functor is a type of mapping between categories
which is applied in category theory. Functors can be thought of as
homomorphisms between categories.” (Wikipedia)
• Sorry, what?
Functor (cont’d)
•
“In mathematics, a functor is a type of mapping between categories which is applied in category theory.
Functors can be thought of as homomorphisms between categories.”
• To map a category to another category, we have to map…
• Objects, Morphisms, and Composition!
• Composition is mostly trivial for this case, so we won’t focus on it.
Functor Laws
Functors must meet these requirements :
• Functor must map identity of an object to that of a mapped object
• Functor must distribute over morphism composition
Functor Example
• Diagram of a functor from
category C to category D.
• You can see:
• Object mapping (yellow)
• Morphism mapping (purple)
Functor in Hask
• What does Maybe do?
• It turns a Hask object(type), T, to another object, Maybe T.
Functor in Hask (cont’d)
• What does fmap do, when applied to a Maybe instance?
• It turns a Hask morphism(function), with type (A -> B),

to another morphism, with type (Maybe A -> Maybe B).
Functor in Hask (cont’d)
• So as we have
• Type constructor, which maps objects between categories
• fmap, which maps morphisms between categories
• Maybe is an instance of Functor - it maps Hask to its subcategory,
a category of Maybe types and functions defined on Maybe types.
Functor in Hask (cont’d)
• Same logic applies to any Functor types.
• Both Functor laws are fulfilled for any functor on Hask.
• fmap id = id
• fmap (f . g) = (fmap f) . (fmap g)
Summary
Summary
• We’ve talked about Hask, the category of Haskell types and functions.
• (types, functions, (.) ) corresponds to 

(objects, morphisms, composition), respectively.
• Functor typeclasses works as Functor in category theory, that is, a
mapping between two different categories.
• Next topics : Monoid, Applicative Functor, Monad, …
Appendix
References
• https://en.wikibooks.org/wiki/Haskell/Category_theory
• https://wiki.haskell.org/Hask
• https://en.wikipedia.org/wiki/Category_theory
• https://en.wikipedia.org/wiki/Category_(mathematics)
• https://en.wikipedia.org/wiki/Group_(mathematics)
• https://en.wikipedia.org/wiki/Group_homomorphism
• https://en.wikipedia.org/wiki/Category_of_sets
• https://en.wikipedia.org/wiki/Category_of_groups
• https://wiki.haskell.org/Typeclassopedia

Contenu connexe

Tendances

Network lap pgms 7th semester
Network lap pgms 7th semesterNetwork lap pgms 7th semester
Network lap pgms 7th semesterDOSONKA Group
 
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaAlgorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaPriyanka Rana
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...cprogrammings
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of javakamal kotecha
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithmRuchika Sinha
 
OOP in C++
OOP in C++OOP in C++
OOP in C++ppd1961
 
working file handling in cpp overview
working file handling in cpp overviewworking file handling in cpp overview
working file handling in cpp overviewgourav kottawar
 
2_2Specification of Tokens.ppt
2_2Specification of Tokens.ppt2_2Specification of Tokens.ppt
2_2Specification of Tokens.pptRatnakar Mikkili
 
Mastering Oracle ADF Bindings
Mastering Oracle ADF BindingsMastering Oracle ADF Bindings
Mastering Oracle ADF BindingsEuegene Fedorenko
 
Turing Machine
Turing MachineTuring Machine
Turing MachineRajendran
 
non-strict functions, bottom and scala by-name parameters
non-strict functions, bottom and scala by-name parametersnon-strict functions, bottom and scala by-name parameters
non-strict functions, bottom and scala by-name parametersPhilip Schwarz
 

Tendances (20)

Network lap pgms 7th semester
Network lap pgms 7th semesterNetwork lap pgms 7th semester
Network lap pgms 7th semester
 
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaAlgorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
 
Os lab manual
Os lab manualOs lab manual
Os lab manual
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 
NFA & DFA
NFA & DFANFA & DFA
NFA & DFA
 
Exception handling
Exception handlingException handling
Exception handling
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
 
Sql syllabus
Sql syllabusSql syllabus
Sql syllabus
 
16 virtual function
16 virtual function16 virtual function
16 virtual function
 
Dot net assembly
Dot net assemblyDot net assembly
Dot net assembly
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
 
working file handling in cpp overview
working file handling in cpp overviewworking file handling in cpp overview
working file handling in cpp overview
 
Cpp reference card
Cpp reference cardCpp reference card
Cpp reference card
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
2_2Specification of Tokens.ppt
2_2Specification of Tokens.ppt2_2Specification of Tokens.ppt
2_2Specification of Tokens.ppt
 
Mastering Oracle ADF Bindings
Mastering Oracle ADF BindingsMastering Oracle ADF Bindings
Mastering Oracle ADF Bindings
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
 
non-strict functions, bottom and scala by-name parameters
non-strict functions, bottom and scala by-name parametersnon-strict functions, bottom and scala by-name parameters
non-strict functions, bottom and scala by-name parameters
 

Similaire à Just Enough Category Theory for Haskell, part 1

Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptxSAICHARANREDDYN
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOPMuhammad Hammad Waseem
 
UML-class diagram for beginners to adance.ppt
UML-class diagram for beginners to adance.pptUML-class diagram for beginners to adance.ppt
UML-class diagram for beginners to adance.pptWorkDrive2
 
Java Hands-On Workshop
Java Hands-On WorkshopJava Hands-On Workshop
Java Hands-On WorkshopArpit Poladia
 
Tutorial spyout
Tutorial spyoutTutorial spyout
Tutorial spyoutpyhasse
 
Code Like Pythonista
Code Like PythonistaCode Like Pythonista
Code Like PythonistaChiyoung Song
 
C++ Introduction brown bag
C++ Introduction brown bagC++ Introduction brown bag
C++ Introduction brown bagJacob Green
 
Chapter 5 searching and sorting handouts with notes
Chapter 5   searching and sorting handouts with notesChapter 5   searching and sorting handouts with notes
Chapter 5 searching and sorting handouts with notesmailund
 
Chemical features: how do we describe a compound to a computer?
Chemical features: how do we describe a compound to a computer?Chemical features: how do we describe a compound to a computer?
Chemical features: how do we describe a compound to a computer?Richard Lewis
 

Similaire à Just Enough Category Theory for Haskell, part 1 (20)

Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP
 
Javasession7
Javasession7Javasession7
Javasession7
 
Lecture 1 - Objects and classes
Lecture 1 - Objects and classesLecture 1 - Objects and classes
Lecture 1 - Objects and classes
 
OOP in JS
OOP in JSOOP in JS
OOP in JS
 
Enumerables
EnumerablesEnumerables
Enumerables
 
Taxonomy
TaxonomyTaxonomy
Taxonomy
 
Taxonomy: Do I Need One
Taxonomy: Do I Need OneTaxonomy: Do I Need One
Taxonomy: Do I Need One
 
UML-class diagram for beginners to adance.ppt
UML-class diagram for beginners to adance.pptUML-class diagram for beginners to adance.ppt
UML-class diagram for beginners to adance.ppt
 
UML-class_diagram.ppt
UML-class_diagram.pptUML-class_diagram.ppt
UML-class_diagram.ppt
 
Java Hands-On Workshop
Java Hands-On WorkshopJava Hands-On Workshop
Java Hands-On Workshop
 
Tutorial spyout
Tutorial spyoutTutorial spyout
Tutorial spyout
 
Code Like Pythonista
Code Like PythonistaCode Like Pythonista
Code Like Pythonista
 
C++ Introduction brown bag
C++ Introduction brown bagC++ Introduction brown bag
C++ Introduction brown bag
 
Java Lecture5.pptx
Java Lecture5.pptxJava Lecture5.pptx
Java Lecture5.pptx
 
mahout-cf
mahout-cfmahout-cf
mahout-cf
 
Chapter 5 searching and sorting handouts with notes
Chapter 5   searching and sorting handouts with notesChapter 5   searching and sorting handouts with notes
Chapter 5 searching and sorting handouts with notes
 
Chemical features: how do we describe a compound to a computer?
Chemical features: how do we describe a compound to a computer?Chemical features: how do we describe a compound to a computer?
Chemical features: how do we describe a compound to a computer?
 
Java
JavaJava
Java
 

Plus de Heejong Ahn

플렉스팀 프론트엔드 기술 스택의 이해: `lint`, `build`, `run`
플렉스팀 프론트엔드 기술 스택의 이해: `lint`, `build`, `run`플렉스팀 프론트엔드 기술 스택의 이해: `lint`, `build`, `run`
플렉스팀 프론트엔드 기술 스택의 이해: `lint`, `build`, `run`Heejong Ahn
 
프로그래머의 배움
프로그래머의 배움프로그래머의 배움
프로그래머의 배움Heejong Ahn
 
Being a Multiplier
Being a MultiplierBeing a Multiplier
Being a MultiplierHeejong Ahn
 
Hello, React Hooks!
Hello, React Hooks!Hello, React Hooks!
Hello, React Hooks!Heejong Ahn
 
TypeScript와 Flow: 
자바스크립트 개발에 정적 타이핑 도입하기
TypeScript와 Flow: 
자바스크립트 개발에 정적 타이핑 도입하기TypeScript와 Flow: 
자바스크립트 개발에 정적 타이핑 도입하기
TypeScript와 Flow: 
자바스크립트 개발에 정적 타이핑 도입하기Heejong Ahn
 
더 나은 팀을 위하여
더 나은 팀을 위하여더 나은 팀을 위하여
더 나은 팀을 위하여Heejong Ahn
 

Plus de Heejong Ahn (7)

플렉스팀 프론트엔드 기술 스택의 이해: `lint`, `build`, `run`
플렉스팀 프론트엔드 기술 스택의 이해: `lint`, `build`, `run`플렉스팀 프론트엔드 기술 스택의 이해: `lint`, `build`, `run`
플렉스팀 프론트엔드 기술 스택의 이해: `lint`, `build`, `run`
 
프로그래머의 배움
프로그래머의 배움프로그래머의 배움
프로그래머의 배움
 
Being a Multiplier
Being a MultiplierBeing a Multiplier
Being a Multiplier
 
Hello, React Hooks!
Hello, React Hooks!Hello, React Hooks!
Hello, React Hooks!
 
TypeScript와 Flow: 
자바스크립트 개발에 정적 타이핑 도입하기
TypeScript와 Flow: 
자바스크립트 개발에 정적 타이핑 도입하기TypeScript와 Flow: 
자바스크립트 개발에 정적 타이핑 도입하기
TypeScript와 Flow: 
자바스크립트 개발에 정적 타이핑 도입하기
 
Pure UI
Pure UIPure UI
Pure UI
 
더 나은 팀을 위하여
더 나은 팀을 위하여더 나은 팀을 위하여
더 나은 팀을 위하여
 

Dernier

"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 

Dernier (20)

"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 

Just Enough Category Theory for Haskell, part 1

  • 1. Just Enough Category Theory for Haskell (part 1) Heejong Ahn (github.com/heejongahn)
  • 2. Tabel of Contents • Purpose and Focus • Category • Functor
  • 4. Purpose • “Do I have to read this thick math book with stubborn, dull cover thoroughly, just to write a program in Haskell?” • You don’t. • We’ve already gone through 5 weeks of exercises without any book! • Yet, as Haskell borrowed a lot of its core concepts from category theory, some knowledge on it might help you a lot.
  • 5. Focus • I’ll try to avoid analogues, which is often used to give an intuition. • That’s because sometimes those keep you from getting actual, clear idea about a concept. • Instead, I’d rather stick to mathematical terms and laws.
  • 6. Disclaimer • I’ve never been excellent in math. • Haven’t been really passionate about it, neither. • Though I’ve put a huge amount of time, there will be lots of unclear, even wrong spots throughout the slides. • If you find any, please feel free to interrupt and correct me ☺
  • 8. Category Category is just a collection which has: • A collection of objects • A collection of morphisms • A notion of composition X Y Z f g g ◦ f
  • 9. Category Category is just a collection which has: • A collection of objects • A collection of morphisms • A notion of composition X Y Z f g g ◦ f
  • 10. Category Category is just a collection which has: • A collection of objects • A collection of morphisms • A notion of composition X Y Z f g g ◦ f source object X target object Y
  • 11. Category Category is just a collection which has: • A collection of objects • A collection of morphisms • A notion of composition X Y Z f g g ◦ f
  • 12. Category Example • Category of sets(Set) is a category. • Set has a collection of objects, which is, well, all sets. • Set has a collection of morphisms, which is a collection of functions from a set to another set. • Set has a notion of composition, which is just a function composition.
  • 13. Category Example (cont’d) • Category of groups(Grp) is also a category. • Grp has a collection of objects, which is, again, all groups. • Grp has a collection of morphisms, which is a collection of functions which preserves group operation (the group homomorphism) • Grp has a notion of composition, which is just a function composition.
  • 14. Category Laws Categories must meet these requirements : • Composition of mophisms should be associative • For every object in a category, there must be an identity morphism
  • 15. Hask • Hask, the category of Haskell types and functions, is our main topic. • Speaking of category, we’ve heard that every category must have… • Objects, morphisms, and composition!
  • 16. Hask (cont’d) • Objects : Haskell types • Morphisms : Haskell functions • Composition : (.) • We know haskell functions are associative, and we have id. • On top of that, we define two functions f and g as the same morphism if f x = g x for all x. (Why? Is it really needed?)
  • 18. Functor • “In mathematics, a functor is a type of mapping between categories which is applied in category theory. Functors can be thought of as homomorphisms between categories.” (Wikipedia) • Sorry, what?
  • 19. Functor (cont’d) • “In mathematics, a functor is a type of mapping between categories which is applied in category theory. Functors can be thought of as homomorphisms between categories.” • To map a category to another category, we have to map… • Objects, Morphisms, and Composition! • Composition is mostly trivial for this case, so we won’t focus on it.
  • 20. Functor Laws Functors must meet these requirements : • Functor must map identity of an object to that of a mapped object • Functor must distribute over morphism composition
  • 21. Functor Example • Diagram of a functor from category C to category D. • You can see: • Object mapping (yellow) • Morphism mapping (purple)
  • 22. Functor in Hask • What does Maybe do? • It turns a Hask object(type), T, to another object, Maybe T.
  • 23. Functor in Hask (cont’d) • What does fmap do, when applied to a Maybe instance? • It turns a Hask morphism(function), with type (A -> B),
 to another morphism, with type (Maybe A -> Maybe B).
  • 24. Functor in Hask (cont’d) • So as we have • Type constructor, which maps objects between categories • fmap, which maps morphisms between categories • Maybe is an instance of Functor - it maps Hask to its subcategory, a category of Maybe types and functions defined on Maybe types.
  • 25. Functor in Hask (cont’d) • Same logic applies to any Functor types. • Both Functor laws are fulfilled for any functor on Hask. • fmap id = id • fmap (f . g) = (fmap f) . (fmap g)
  • 27. Summary • We’ve talked about Hask, the category of Haskell types and functions. • (types, functions, (.) ) corresponds to 
 (objects, morphisms, composition), respectively. • Functor typeclasses works as Functor in category theory, that is, a mapping between two different categories. • Next topics : Monoid, Applicative Functor, Monad, …
  • 29. References • https://en.wikibooks.org/wiki/Haskell/Category_theory • https://wiki.haskell.org/Hask • https://en.wikipedia.org/wiki/Category_theory • https://en.wikipedia.org/wiki/Category_(mathematics) • https://en.wikipedia.org/wiki/Group_(mathematics) • https://en.wikipedia.org/wiki/Group_homomorphism • https://en.wikipedia.org/wiki/Category_of_sets • https://en.wikipedia.org/wiki/Category_of_groups • https://wiki.haskell.org/Typeclassopedia