Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ScalaWorld 2017 - A Pragmatic Introduction to Category Theory

ScalaWorld 2017 - A Pragmatic Introduction to Category Theory

Daniela Sfregola

September 19, 2017
Tweet

More Decks by Daniela Sfregola

Other Decks in Programming

Transcript

  1. A PRAGMATIC
    INTRODUCTION TO
    CATEGORY THEORY
    @DANIELASFREGOLA
    SCALA WORLD 2017
    github.com/DanielaSfregola/tutorial-cat

    View Slide

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

    View Slide

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

    View Slide

  4. I AM NOT A MATHEMATICIAN

    View Slide

  5. YOU DO NOT NEED TO KNOW
    CATEGORY THEORY
    TO WRITE
    SCALA CODE

    View Slide

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

    View Slide

  7. CATEGORY THEORY
    DEEPER UNDERSTANDING ON
    OUR CODE

    View Slide

  8. HOW DO WE
    REASON ?

    View Slide

  9. COMPOSITION
    ABSTRACTION

    View Slide

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

    View Slide

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

    View Slide

  12. CATEGORY THEORY
    HOW THINGS COMPOSE

    View Slide

  13. ARROW THEORY
    CATEGORY THEORY
    HOW THINGS COMPOSE

    View Slide

  14. WHAT IS A CATEGORY?

    View Slide

  15. COMPOSITION LAW

    View Slide

  16. IDENTITY LAW

    View Slide

  17. COMPOSITION + ASSOCIATIVITY

    View Slide

  18. CATEGORY'S RULES
    > Identity
    > Composition
    > Associativity

    View Slide

  19. A PRACTICAL EXAMPLE

    View Slide

  20. CATEGORY WITH 1 OBJECT

    View Slide

  21. CATEGORY WITH 1 OBJECT
    =
    MONOID

    View Slide

  22. 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

    View Slide

  23. A PRACTICAL EXAMPLE

    View Slide

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

    View Slide

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

    View Slide

  26. CATEGORY WITH 1+ OBJECT

    View Slide

  27. CATEGORY IN A BOX

    View Slide

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

    View Slide

  29. LIFTING: CONTEXT VS CONTENT

    View Slide

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

    View Slide

  31. CATEGORY IN A BOX
    =
    FUNCTOR

    View Slide

  32. 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)

    View Slide

  33. LIFTING: CONTEXT VS CONTENT

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  37. BOX FUNCTION + BOX VALUES

    View Slide

  38. COMBINE MORE BOXES
    INTO ONE
    =
    APPLICATIVE

    View Slide

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

    View Slide

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

    View Slide

  41. 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
    }

    View Slide

  42. BOX FUNCTION + BOX VALUES

    View Slide

  43. 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
    }

    View Slide

  44. 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

    View Slide

  45. BOX IN A BOX

    View Slide

  46. BOX IN A BOX

    View Slide

  47. FUSE TWO BOXES
    TOGETHER
    =
    MONAD

    View Slide

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

    View Slide

  49. 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
    }
    }

    View Slide

  50. BOXES IN A SEQUENCE

    View Slide

  51. 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

    View Slide

  52. 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] = ???
    }

    View Slide

  53. 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

    View Slide

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

    View Slide

  55. MONAD IS
    A MONOID
    IN THE CATEGORY OF
    ENDOFUNCTORS

    View Slide

  56. MONAD
    MONOID => pure + flatten
    ENDOFUNCTORS => map

    View Slide

  57. 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

    View Slide

  58. 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”.

    View Slide

  59. FORGET ABOUT THE DETAILS
    FOCUS ON
    HOW THINGS COMPOSE

    View Slide

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

    View Slide

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

    View Slide