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

Monads @ ATLRUG

Monads @ ATLRUG

An introduction to monads for Rubyists, with accompanying code at https://github.com/jamesdabbs/rb-monads

James Dabbs

August 14, 2013
Tweet

More Decks by James Dabbs

Other Decks in Technology

Transcript

  1. An Intro to Monads
    Categories for the Working Programmer
    James Dabbs
    [email protected] | github.com/jamesdabbs

    View full-size slide

  2. What Is a Monad?

    View full-size slide

  3. What Is a Monad?
    Definition 1:

    View full-size slide

  4. What Is a Monad?
    Definition 1:
    A monad is a monoid in the
    category of endofunctors

    View full-size slide

  5. Choosing the right abstraction is the difference
    between working code and great code.

    View full-size slide

  6. Choosing the right abstraction is the difference
    between working code and great code.
    - James Dabbs

    View full-size slide

  7. Well-Abstracted Code is

    View full-size slide

  8. Well-Abstracted Code is
    Readable

    View full-size slide

  9. Well-Abstracted Code is
    Readable
    Testable

    View full-size slide

  10. Well-Abstracted Code is
    Readable
    Testable
    Extensible

    View full-size slide

  11. Well-Abstracted Code is
    Readable
    Testable
    Extensible
    Maintainable

    View full-size slide

  12. Well-Abstracted Code is
    Readable
    Testable
    Extensible
    Maintainable
    Elegant

    View full-size slide

  13. Abstractioneering

    View full-size slide

  14. Abstractioneering
    Connection details are abstracted out so they can be
    changed in one place

    View full-size slide

  15. Abstractioneering
    Connection details are abstracted out so they can be
    changed in one place
    The User object encapsulates a user’s behavior

    View full-size slide

  16. Abstractioneering
    Connection details are abstracted out so they can be
    changed in one place
    The User object encapsulates a user’s behavior
    Separation of data and representation

    View full-size slide

  17. Abstractioneering

    View full-size slide

  18. Abstractioneering

    View full-size slide

  19. Abstractioneering
    The real value of Rails (or any MV* framework) is in
    providing a set of useful abstractions to solve common
    problems and promote code reuse.

    View full-size slide

  20. Abstractioneering
    The real value of Rails (or any MV* framework) is in
    providing a set of useful abstractions to solve common
    problems and promote code reuse.
    Our goal as software engineers should be to recognize
    common problems and write reusable abstractions to
    solve them.

    View full-size slide

  21. Reusing Code

    View full-size slide

  22. Reusing Code
    Inheritance / subclassing

    View full-size slide

  23. Reusing Code
    Inheritance / subclassing
    Modules / mixins

    View full-size slide

  24. Reusing Code
    Inheritance / subclassing
    Modules / mixins
    Object composition / decorators / presenters

    View full-size slide

  25. Reusing Code
    Inheritance / subclassing
    Modules / mixins
    Object composition / decorators / presenters
    Function composition

    View full-size slide

  26. Reusing Code
    Inheritance / subclassing
    Modules / mixins
    Object composition / decorators / presenters
    Function composition
    Monads

    View full-size slide

  27. Reusing Code
    Inheritance / subclassing
    Modules / mixins
    Object composition / decorators / presenters
    Function composition
    Monads

    View full-size slide

  28. Reusing Code
    Inheritance / subclassing
    Modules / mixins
    Object composition / decorators / presenters
    Function composition
    Monads
    Object oriented

    View full-size slide

  29. Reusing Code
    Inheritance / subclassing
    Modules / mixins
    Object composition / decorators / presenters
    Function composition
    Monads
    Object oriented
    Functional

    View full-size slide

  30. Functional Programming

    View full-size slide

  31. Functional Programming
    Start with functions - these represent actions or
    behaviors

    View full-size slide

  32. Functional Programming
    Start with functions - these represent actions or
    behaviors
    Build programs by combining simple functions into
    more complicated ones

    View full-size slide

  33. Functional Programming
    Start with functions - these represent actions or
    behaviors
    Build programs by combining simple functions into
    more complicated ones
    Verbs, not nouns

    View full-size slide

  34. Functional Programming

    View full-size slide

  35. Functional Programming
    Category theory is the mathematical study of
    functions and composition

    View full-size slide

  36. Functional Programming
    Category theory is the mathematical study of
    functions and composition
    Monads are important objects in category theory

    View full-size slide

  37. Functional Programming
    Category theory is the mathematical study of
    functions and composition
    Monads are important objects in category theory
    As such, monads tend to be more important and
    prevalent in strongly functional languages.

    View full-size slide

  38. Functional Programming
    For instance, Haskell programs can’t do anything
    without monads

    View full-size slide

  39. Functional Programming
    A language that doesn't affect the way you think about
    programming, is not worth knowing.
    For instance, Haskell programs can’t do anything
    without monads

    View full-size slide

  40. Functional Programming
    A language that doesn't affect the way you think about
    programming, is not worth knowing.
    - Alan Perlis
    For instance, Haskell programs can’t do anything
    without monads

    View full-size slide

  41. Functional Programming
    Ruby doesn’t have first-class functions, but does
    support some common functional idioms:

    View full-size slide

  42. Functional Programming
    In a pure functional language, this construction could look
    something like

    View full-size slide

  43. Functional Programming
    In a pure functional language, this construction could look
    something like

    View full-size slide

  44. Functional Programming
    In a pure functional language, this construction could look
    something like
    Or, simpler

    View full-size slide

  45. Functional Programming
    In a pure functional language, this construction could look
    something like
    Or, simpler

    View full-size slide

  46. Composition
    This worked out because the types plumbed up nicely

    View full-size slide

  47. Composition
    This worked out because the types plumbed up nicely
    What if we aren’t so lucky?

    View full-size slide

  48. Extending Composition

    View full-size slide

  49. Composition
    Here we have the following types

    View full-size slide

  50. What Is a Monad?

    View full-size slide

  51. What Is a Monad?
    Definition 2:

    View full-size slide

  52. What Is a Monad?
    Definition 2:
    A monad is the glue you need to bind these
    functions together and “cross-compose”

    View full-size slide

  53. What Is a Monad?
    Definition 2:
    A monad is the glue you need to bind these
    functions together and “cross-compose”

    View full-size slide

  54. What Is a Monad?

    View full-size slide

  55. What Is a Monad?
    Definition 3:

    View full-size slide

  56. What Is a Monad?
    Definition 3:
    A monad is a structure m with two functions*

    View full-size slide

  57. What Is a Monad?
    Definition 3:
    A monad is a structure m with two functions*

    View full-size slide

  58. What Is a Monad?
    Definition 3:
    A monad is a structure m with two functions*
    In the case of IO, this becomes

    View full-size slide

  59. What Is a Monad?
    Definition 3:
    A monad is a structure m with two functions*
    In the case of IO, this becomes

    View full-size slide

  60. Monad Laws
    We need to specify a few restrictions

    View full-size slide

  61. Monad Laws
    We need to specify a few restrictions

    View full-size slide

  62. Monad Laws
    We need to specify a few restrictions
    This bind totally disregards the bound value

    View full-size slide

  63. Monad Laws
    The last law looks complicated, but that’s because we’re
    at the wrong level of abstraction.
    With an appropriate cross-composition operator it’s

    View full-size slide

  64. Monad Laws
    The last law looks complicated, but that’s because we’re
    at the wrong level of abstraction.
    With an appropriate cross-composition operator it’s

    View full-size slide

  65. The Ubiquitous Monad

    View full-size slide

  66. The Ubiquitous Monad
    IO - as discussed, and hence the do joke

    View full-size slide

  67. The Ubiquitous Monad
    IO - as discussed, and hence the do joke
    List - represents non-deterministic (or, with a little
    modification, probabilistic) computation

    View full-size slide

  68. The Ubiquitous Monad
    IO - as discussed, and hence the do joke
    List - represents non-deterministic (or, with a little
    modification, probabilistic) computation
    Maybe - represents computation that might fail to
    return a value

    View full-size slide

  69. The Ubiquitous Monad
    IO - as discussed, and hence the do joke
    List - represents non-deterministic (or, with a little
    modification, probabilistic) computation
    Maybe - represents computation that might fail to
    return a value
    State - functions that alter program state

    View full-size slide

  70. The Ubiquitous Monad
    IO - as discussed, and hence the do joke
    List - represents non-deterministic (or, with a little
    modification, probabilistic) computation
    Maybe - represents computation that might fail to
    return a value
    State - functions that alter program state
    Persistent - functions that alter a database

    View full-size slide

  71. Examples
    Refer to console / editor

    View full-size slide

  72. Takeaways
    Monads are a different type of composition, well suited
    to managing state, history, or side effects

    View full-size slide

  73. Takeaways
    Monads are a different type of composition, well suited
    to managing state, history, or side effects
    Functional programming and category theory are worth
    studying - especially together

    View full-size slide

  74. Fin
    Questions?

    View full-size slide

  75. References
    Source code - https://github.com/jamesdabbs/rb-
    monads
    The Kingdom of Nouns - http://steve-
    yegge.blogspot.com/2006/03/execution-in-kingdom-
    of-nouns.html
    Learn You a Haskell - http://learnyouahaskell.com/

    View full-size slide