Slide 1

Slide 1 text

A PRAGMATIC INTRODUCTION TO CATEGORY THEORY @DANIELASFREGOLA SCALA MATSURI 2018 github.com/DanielaSfregola/tutorial-cat

Slide 2

Slide 2 text

AGENDA - Intro - Monoid - Functor - Applicative - Monad github.com/DanielaSfregola/tutorial-cat

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

I AM NOT A MATHEMATICIAN

Slide 5

Slide 5 text

YOU DO NOT NEED TO KNOW CATEGORY THEORY TO WRITE GOOD CODE

Slide 6

Slide 6 text

YOU DO NOT NEED TO KNOW CATEGORY THEORY TO WRITE FUNCTIONAL CODE

Slide 7

Slide 7 text

CATEGORY THEORY DEEPER UNDERSTANDING ON OUR CODE

Slide 8

Slide 8 text

HOW DO WE REASON ?

Slide 9

Slide 9 text

COMPOSITION ABSTRACTION

Slide 10

Slide 10 text

CATEGORY THEORY HOW THINGS COMPOSE

Slide 11

Slide 11 text

ARROW THEORY CATEGORY THEORY HOW THINGS COMPOSE

Slide 12

Slide 12 text

WHAT IS A CATEGORY?

Slide 13

Slide 13 text

COMPOSITION LAW

Slide 14

Slide 14 text

IDENTITY LAW

Slide 15

Slide 15 text

COMPOSITION + ASSOCIATIVITY

Slide 16

Slide 16 text

CATEGORY'S RULES > Identity > Composition > Associativity

Slide 17

Slide 17 text

A PRACTICAL EXAMPLE

Slide 18

Slide 18 text

CATEGORY WITH 1 OBJECT

Slide 19

Slide 19 text

CATEGORY WITH 1 OBJECT = MONOID

Slide 20

Slide 20 text

MONOID'S RULES Identity n o id == id o n == n Composition forall x, y => x o y Associativity x o (y o z) == (x o y) o z

Slide 21

Slide 21 text

A PRACTICAL EXAMPLE

Slide 22

Slide 22 text

MONOID trait Monoid[A] { def identity: A def compose(x: A, y: A): A }

Slide 23

Slide 23 text

EXERCISES ON MONOID > Define a monoid for Int > Define a monoid for String sbt 'testOnly *Monoid*' github.com/DanielaSfregola/tutorial-cat

Slide 24

Slide 24 text

CATEGORY WITH 1+ OBJECT

Slide 25

Slide 25 text

CATEGORY IN A BOX

Slide 26

Slide 26 text

CATEGORY IN A BOX > Objects are in a Box > All the arrows are mapped

Slide 27

Slide 27 text

LIFTING: CONTEXT VS CONTENT

Slide 28

Slide 28 text

EXAMPLE OF BOXES > Option > Future > Try > List > Either

Slide 29

Slide 29 text

CATEGORY IN A BOX = FUNCTOR

Slide 30

Slide 30 text

FUNCTOR'S RULES Identity map(id) == id Composition map(g o f) == map(g) o map(f) Associativity map(h o g) o map(f) == map(h) o map(g o f)

Slide 31

Slide 31 text

LIFTING: CONTEXT VS CONTENT

Slide 32

Slide 32 text

FUNCTOR class Functor[Box[_]] { def map[A, B](boxA: Box[A]) (f: A => B): Box[B] }

Slide 33

Slide 33 text

EXERCISES ON FUNCTOR > Define a functor for Maybe > Define a functor for ZeroOrMore sbt 'testOnly *Functor*' github.com/DanielaSfregola/tutorial-cat

Slide 34

Slide 34 text

BOX FUNCTION + BOX VALUES

Slide 35

Slide 35 text

COMBINE MORE BOXES = APPLICATIVE

Slide 36

Slide 36 text

APPLICATIVE'S RULES > Identity > Associativity > Homorphism > Interchange ...and more!

Slide 37

Slide 37 text

COMBINE BOXES TOGETHER > How to create a new box > How to combine their values together

Slide 38

Slide 38 text

APPLICATIVE class Applicative[Box[_]] extends Functor[Box] { def pure[A](a: A): Box[A] def ap[A, B](boxF: Box[A => B])(boxA: Box[A]): Box[B] def map[A, B](boxA: Box[A])(f: A => B): Box[B] = ??? // spoiler alert }

Slide 39

Slide 39 text

BOX FUNCTION + BOX VALUES

Slide 40

Slide 40 text

APPLICATIVE class Applicative[Box[_]] extends Functor[Box] { def pure[A](a: A): Box[A] def ap[A, B](boxF: Box[A => B])(value: Box[A]): Box[B] def ap2[A1, A2, B](boxF: Box[(A1, A2) => B]) (value1: Box[A1], value2: Box[A2]): Box[B] // up to 22 values! // same for map }

Slide 41

Slide 41 text

EXERCISES ON APPLICATIVE > Define map in terms of ap and pure > Define an applicative for Maybe > Define an applicative for ZeroOrMore sbt 'testOnly *Applicative*' github.com/DanielaSfregola/tutorial-cat

Slide 42

Slide 42 text

BOX IN A BOX

Slide 43

Slide 43 text

BOX IN A BOX

Slide 44

Slide 44 text

FUSE TWO BOXES = MONAD

Slide 45

Slide 45 text

MONAD'S RULES > Identity > Composition > Associativity

Slide 46

Slide 46 text

MONAD (AS FUNCTOR) class Monad[Box[_]] extends Functor[Box] { def flatten[A](bb: Box[Box[A]]): Box[A] def flatMap[A, B](valueA: Box[A])(f: A => Box[B]): Box[B] = { val bb: Box[Box[B]] = map(valueA)(f) bb.flatten } }

Slide 47

Slide 47 text

BOXES IN A SEQUENCE

Slide 48

Slide 48 text

FOR-COMPREHENSION val boxA: Box[A] def toBoxB: A => Box[B] def toBoxC: B => Box[C] def toBoxD: C => Box[D] for { a <- boxA b <- toBoxB(a) c <- toBoxC(b) d <- toBoxD(c) } yield d

Slide 49

Slide 49 text

MONAD (AS APPLICATIVE) trait Monad[Box[_]] extends Applicative[Box] { def flatMap[A, B](boxA: Box[A])(f: A => Box[B]): Box[B] // TODO - implement using flatMap def flatten[A](boxBoxA: Box[Box[A]]): Box[A] = ??? // TODO - implement using flatMap and map override def ap[A, B](boxF: Box[A => B]) (boxA: Box[A]): Box[B] = ??? // TODO - implement using flatMap and pure override def map[A, B](boxA: Box[A])(f: A => B): Box[B] = ??? }

Slide 50

Slide 50 text

EXERCISES ON MONAD (1) > Define flatten using flatMap > Define map using flatMap and pure > Define ap using flatMap and map github.com/DanielaSfregola/tutorial-cat

Slide 51

Slide 51 text

EXERCISES ON MONAD (2) > Define a monad for Maybe > Define a monad for ZeroOrMore sbt 'testOnly *Monad*' github.com/DanielaSfregola/tutorial-cat

Slide 52

Slide 52 text

FUNCTOR VS ENDOFUNCTOR

Slide 53

Slide 53 text

MONAD IS A MONOID IN THE CATEGORY OF ENDOFUNCTORS

Slide 54

Slide 54 text

MONAD MONOID => pure + flatten ENDOFUNCTORS => map

Slide 55

Slide 55 text

SUMMARY CATEGORY THEORY >> how things compose MONOID >> combining 2 values into 1 FUNCTOR >> values lifted to a context APPLICATIVE >> independent values applied to a function in a context MONAD >> ops in sequence in a context

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

FORGET ABOUT THE DETAILS FOCUS ON HOW THINGS COMPOSE

Slide 58

Slide 58 text

WANNA KNOW MORE? > Category Theory for the WH by @PhilipWadler > Category Theory by @BartoszMilewski > Cats-Infographics by Rob Norris - @tpolecat > Cats Documentation - Type Classes

Slide 59

Slide 59 text

THANK YOU! > Twitter: @DanielaSfregola > Blog: danielasfregola.com github.com/DanielaSfregola/tutorial-cat