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 Slide

  2. What Is a Monad?

    View Slide

  3. What Is a Monad?
    Definition 1:

    View Slide

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

    View Slide

  5. View Slide

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

    View Slide

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

    View Slide

  8. Well-Abstracted Code is

    View Slide

  9. Well-Abstracted Code is
    Readable

    View Slide

  10. Well-Abstracted Code is
    Readable
    Testable

    View Slide

  11. Well-Abstracted Code is
    Readable
    Testable
    Extensible

    View Slide

  12. Well-Abstracted Code is
    Readable
    Testable
    Extensible
    Maintainable

    View Slide

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

    View Slide

  14. Abstractioneering

    View Slide

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

    View Slide

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

    View Slide

  17. 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 Slide

  18. Abstractioneering

    View Slide

  19. Abstractioneering

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

    View Slide

  21. 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 Slide

  22. Reusing Code

    View Slide

  23. Reusing Code
    Inheritance / subclassing

    View Slide

  24. Reusing Code
    Inheritance / subclassing
    Modules / mixins

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  31. Functional Programming

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  35. Functional Programming

    View Slide

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

    View Slide

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

    View Slide

  38. 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 Slide

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

    View Slide

  40. 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 Slide

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

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  49. Extending Composition

    View Slide

  50. Composition
    Here we have the following types

    View Slide

  51. What Is a Monad?

    View Slide

  52. What Is a Monad?
    Definition 2:

    View Slide

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

    View Slide

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

    View Slide

  55. What Is a Monad?

    View Slide

  56. What Is a Monad?
    Definition 3:

    View Slide

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

    View Slide

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

    View 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 Slide

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

    View Slide

  61. Monad Laws
    We need to specify a few restrictions

    View Slide

  62. Monad Laws
    We need to specify a few restrictions

    View Slide

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

    View Slide

  64. Monad Laws

    View Slide

  65. Monad Laws

    View Slide

  66. Monad Laws

    View Slide

  67. Monad Laws

    View Slide

  68. 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 Slide

  69. 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 Slide

  70. The Ubiquitous Monad

    View Slide

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

    View Slide

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

    View Slide

  73. 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 Slide

  74. 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 Slide

  75. 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 Slide

  76. Examples
    Refer to console / editor

    View Slide

  77. Takeaways

    View Slide

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

    View Slide

  79. 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 Slide

  80. Fin
    Questions?

    View Slide

  81. 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 Slide