Slide 1

Slide 1 text

A PRAGMATIC INTRODUCTION TO CATEGORY THEORY @DANIELASFREGOLA SCALA WORLD 2017 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

HELLOOOOO > ex Java Developer > OOP background > I am not a mathematician !

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 SCALA 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

OBJECT ORIENTED PROGRAMMING (OOP) > composition of objects > abstraction over the behaviour of each object

Slide 11

Slide 11 text

FUNCTIONAL PROGRAMMING (FP) > composition of functions > abstraction over mathematical laws

Slide 12

Slide 12 text

CATEGORY THEORY HOW THINGS COMPOSE

Slide 13

Slide 13 text

ARROW THEORY CATEGORY THEORY HOW THINGS COMPOSE

Slide 14

Slide 14 text

WHAT IS A CATEGORY?

Slide 15

Slide 15 text

COMPOSITION LAW

Slide 16

Slide 16 text

IDENTITY LAW

Slide 17

Slide 17 text

COMPOSITION + ASSOCIATIVITY

Slide 18

Slide 18 text

CATEGORY'S RULES > Identity > Composition > Associativity

Slide 19

Slide 19 text

A PRACTICAL EXAMPLE

Slide 20

Slide 20 text

CATEGORY WITH 1 OBJECT

Slide 21

Slide 21 text

CATEGORY WITH 1 OBJECT = MONOID

Slide 22

Slide 22 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 23

Slide 23 text

A PRACTICAL EXAMPLE

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

CATEGORY WITH 1+ OBJECT

Slide 27

Slide 27 text

CATEGORY IN A BOX

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

LIFTING: CONTEXT VS CONTENT

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

CATEGORY IN A BOX = FUNCTOR

Slide 32

Slide 32 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 33

Slide 33 text

LIFTING: CONTEXT VS CONTENT

Slide 34

Slide 34 text

CHANGE THE CONTENT OF A BOX > How to change the context

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

BOX FUNCTION + BOX VALUES

Slide 38

Slide 38 text

COMBINE MORE BOXES INTO ONE = APPLICATIVE

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 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 42

Slide 42 text

BOX FUNCTION + BOX VALUES

Slide 43

Slide 43 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 44

Slide 44 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 45

Slide 45 text

BOX IN A BOX

Slide 46

Slide 46 text

BOX IN A BOX

Slide 47

Slide 47 text

FUSE TWO BOXES TOGETHER = MONAD

Slide 48

Slide 48 text

MONAD'S RULES > Left Identity > Right Identity > Associativity

Slide 49

Slide 49 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 50

Slide 50 text

BOXES IN A SEQUENCE

Slide 51

Slide 51 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 52

Slide 52 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 53

Slide 53 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 54

Slide 54 text

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

Slide 55

Slide 55 text

MONAD IS A MONOID IN THE CATEGORY OF ENDOFUNCTORS

Slide 56

Slide 56 text

MONAD MONOID => pure + flatten ENDOFUNCTORS => map

Slide 57

Slide 57 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 58

Slide 58 text

Band BoundedSemilattice CommutativeGroup CommutativeMonoid CommutativeSemigroup Eq Group Semigroup Monoid Order PartialOrder Semilattice Alternative Applicative ApplicativeError Apply Bifoldable Bimonad Bitraverse Cartesian CoflatMap Comonad ContravariantCartesian FlatMap Foldable Functor Inject InvariantMonoidal Monad MonadError MonoidK NotNull Reducible SemigroupK Show ApplicativeAsk Bifunctor Contravariant Invariant Profunctor Strong Traverse Arrow Category Choice Compose Cats Type Classes kernel core/functor core/arrow core The highlighted type classes are the first ones you should learn. They’re well documented and well-known so it’s easy to get help. a |+| b a === b a =!= b a |@| b a *> b a <* b a <+> b a >>> b a <<< b a > b a >= b a < b a <= b Sync Async Effect LiftIO effect Some type classes introduce symbolic operators. NonEmptyTraverse InjectK CommutativeArrow CommutativeFlatMap CommutativeMonad ApplicativeLayer FunctorLayer ApplicativeLayerFunctor FunctorLayerFunctor ApplicativeLocal FunctorEmpty FunctorListen FunctorTell FunctorRaise MonadLayer MonadLayerFunctor MonadLayerControl MonadState TraverseEmpty Functor Applicative Monad Traverse mtl MTL type classes do not extend core type classes directly, but the effect is similar; the dashed line can be read “implies”.

Slide 59

Slide 59 text

FORGET ABOUT THE DETAILS FOCUS ON HOW THINGS COMPOSE

Slide 60

Slide 60 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 61

Slide 61 text

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