A practical introduction to Category Theory - ScalaItaly 2017

A practical introduction to Category Theory - ScalaItaly 2017

Category Theory has become one of the hot topics in our community. Why is this theory suddenly so interesting for developers? Why are the cool kids talking so much about it? How can we apply its principles in our code?

This talk will introduce the general principles behind Category Theory, it will show practical examples of how this theory has managed to simplify and solve common challenges that we encounter in our code daily.

E99b07644586e9e1723757bf8e34ea68?s=128

Daniela Sfregola

May 13, 2017
Tweet

Transcript

  1. A PRACTICAL INTRODUCTION TO CATEGORY THEORY @DANIELASFREGOLA SCALA ITALY 2017

  2. HELLOOOOO > ex Java Developer > OOP background > I

    am not a mathematician !
  3. OUR GOAL

  4. CATS HTTPS://GITHUB.COM/TYPELEVEL/CATS

  5. HOW DO WE REASON ?

  6. COMPOSITION ABSTRACTION

  7. OBJECT ORIENTED PROGRAMMING (OOP) > composition of objects > abstraction

    over the behaviour of each object
  8. FUNCTIONAL PROGRAMMING (FP) > composition of functions > abstraction over

    mathematical laws
  9. CATEGORY THEORY

  10. CATEGORY THEORY ARROW THEORY

  11. WHAT IS A CATEGORY?

  12. COMPOSITION LAW

  13. IDENTITY LAW

  14. COMPOSITION + ASSOCIATIVITY

  15. A PRACTICAL EXAMPLE

  16. CATEGORY WITH 1 OBJECT

  17. A PRACTICAL EXAMPLE

  18. MONOID trait Monoid[A] { def combine(x: A, y: A): A

    def empty: A }
  19. CREATE CATEGORIES FROM CATEGORIES

  20. PUTTING THINGS IN A BOX

  21. EXAMPLE OF CONTAINERS > Option > Future > Try >

    List > Either
  22. LIFTING: CONTEXT VS CONTENT

  23. FUNCTOR class Functor[Box[_]] { def map[A, B](value: Box[A]) (f: A

    => B): Box[B] }
  24. EXAMPLE OF FUNCTOR scala> Some("daniela").map(s => "yo " + s)

    res0: Option[String] = Some(yo daniela) scala> None.map(s => Some("yo " + s)) res1: Option[String] = None
  25. BOX FUNCTION + BOX VALUES

  26. 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 map[A, B](value: Box[A])(f: A => B): Box[B] = { val boxF = pure(f) ap(boxF)(value) } }
  27. BOX FUNCTION + BOX VALUES

  28. 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 }
  29. BOX IN A BOX

  30. BOX IN A BOX

  31. MONAD class Monad[Box[_]] extends Applicative[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 } }
  32. BOXES IN A SEQUENCE

  33. 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
  34. 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
  35. Band BoundedSemilattice CommutativeGroup CommutativeMonoid CommutativeSemigroup Eq Group Semigroup Monoid Order

    PartialOrder Semilattice Alternative Applicative AppllicativeError Apply Bifoldable Bimonad Bitraverse Cartesian CoflatMap Comonad ContravariantCartesian FlatMap Foldable Functor FunctorFilter Inject InvariantMonoidal Monad MonadCombine MonadError MonoidK NotNull Reducible SemigroupK Show TransLift TraverseFilter Trivial Unapply Bifunctor Contravariant Invariant Profunctor Strong Traverse MonadFilter MonadReader MonadState MonadWriter Arrow Category Choice Compose Split 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 core MTL effect Some type classes introduce symbolic operators.
  36. FORGET ABOUT THE DETAILS FOCUS ON HOW THINGS COMPOSE

  37. WANNA KNOW MORE? > Category Theory for the WH by

    @PhilipWadler > Category Theory by @BartoszMilewski > Cats-Infographics by Rob Norris - @tpolecat > Cats Documentation - Type Classes
  38. THANK YOU! > Twitter: @DanielaSfregola > Blog: danielasfregola.com