Monads @ ATLRUG

Monads @ ATLRUG

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

908eb2dedca30a0209477244e069b22c?s=128

James Dabbs

August 14, 2013
Tweet

Transcript

  1. An Intro to Monads Categories for the Working Programmer James

    Dabbs jdabbs@emcien.com | github.com/jamesdabbs
  2. What Is a Monad?

  3. What Is a Monad? Definition 1:

  4. What Is a Monad? Definition 1: A monad is a

    monoid in the category of endofunctors
  5. None
  6. Choosing the right abstraction is the difference between working code

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

    and great code. - James Dabbs
  8. Well-Abstracted Code is

  9. Well-Abstracted Code is Readable

  10. Well-Abstracted Code is Readable Testable

  11. Well-Abstracted Code is Readable Testable Extensible

  12. Well-Abstracted Code is Readable Testable Extensible Maintainable

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

  14. Abstractioneering

  15. Abstractioneering Connection details are abstracted out so they can be

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

    changed in one place The User object encapsulates a user’s behavior
  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
  18. Abstractioneering

  19. Abstractioneering

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

  23. Reusing Code Inheritance / subclassing

  24. Reusing Code Inheritance / subclassing Modules / mixins

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

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

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

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

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

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

    / decorators / presenters Function composition Monads Object oriented Functional
  31. Functional Programming

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

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

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

    behaviors Build programs by combining simple functions into more complicated ones Verbs, not nouns
  35. Functional Programming

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

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

    and composition Monads are important objects in category theory
  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.
  39. Functional Programming For instance, Haskell programs can’t do anything without

    monads
  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
  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
  42. Functional Programming Ruby doesn’t have first-class functions, but does support

    some common functional idioms:
  43. Functional Programming In a pure functional language, this construction could

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

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

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

    look something like Or, simpler
  47. Composition This worked out because the types plumbed up nicely

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

    What if we aren’t so lucky?
  49. Extending Composition

  50. Composition Here we have the following types

  51. What Is a Monad?

  52. What Is a Monad? Definition 2:

  53. What Is a Monad? Definition 2: A monad is the

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

    glue you need to bind these functions together and “cross-compose”
  55. What Is a Monad?

  56. What Is a Monad? Definition 3:

  57. What Is a Monad? Definition 3: A monad is a

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

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

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

    structure m with two functions* In the case of IO, this becomes
  61. Monad Laws We need to specify a few restrictions

  62. Monad Laws We need to specify a few restrictions

  63. Monad Laws We need to specify a few restrictions This

    bind totally disregards the bound value
  64. Monad Laws

  65. Monad Laws

  66. Monad Laws

  67. Monad Laws

  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
  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
  70. The Ubiquitous Monad

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

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

    do joke List - represents non-deterministic (or, with a little modification, probabilistic) computation
  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
  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
  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
  76. Examples Refer to console / editor

  77. Takeaways

  78. Takeaways Monads are a different type of composition, well suited

    to managing state, history, or side effects
  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
  80. Fin Questions?

  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/