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

Monads and usage in PHP

Mathieu Ledru
September 30, 2021

Monads and usage in PHP

This talk is about Monads and theirs use in PHP

Monads exemple code associated to this talk

https://github.com/darkwood-fr/flow

Article associated to this talk

https://blog.darkwood.fr/article/les-monades-et-leur-utilisation-en-php

Video associated to this talk

https://www.youtube.com/watch?v=0M6MhUpra9o

Mathieu Ledru

September 30, 2021
Tweet

More Decks by Mathieu Ledru

Other Decks in Programming

Transcript

  1. Monads
    @matyo91
    29-09-2021
    In PHP

    View Slide

  2. Why?
    @matyo91
    29-09-2021

    View Slide

  3. A new way to program with
    effects

    @matyo91
    29-09-2021

    View Slide

  4. “Once you understand Monads


    You lose the ability to explain
    them to other people”


    Crockford’s Paradox

    @matyo91
    29-09-2021

    View Slide

  5. What are Monads?
    @matyo91
    29-09-2021
    As functional programming

    View Slide

  6. Data Expr = Val Int | Div Expr Expr
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  7. Math world


    1


    6 / 2


    6 / ( 3 / 1 )
    @matyo91
    @matyo91
    29-09-2021
    Haskell world


    Val 1


    Div (Val 6) (Val 2)


    Div (Val 6) (Div (Val 3) (Val 1))

    View Slide

  8. eval :: Expr -> Int


    eval (Val n) = n


    eval (Div (Expr x) (Expr y)) = eval(x) / eval(y)
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  9. safediv :: Int -> Int -> MayBe Int


    safediv (Int n) (Int m) = if m == 0 then Nothing else Just(n / m)
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  10. eval :: Expr -> Maybe


    eval (Val n) = Just(n)


    eval (Div (Expr x) (Expr y)) = case eval(x) at


    Nothing -> Nothing


    Just n -> case eval(y) of


    Nothing -> Nothing


    Just m -> safediv (n m)
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  11. case [m] as Maybe of


    Nothing -> Nothing


    Just x -> [f] as function x
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  12. m >>= f case m of


    Nothing -> Nothing


    Just x -> f x
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  13. eval :: Expr -> Maybe Int


    eval (Val n) = return n


    eval (Div (Expr x) (Expr y)) = eval(x) >>= (ƛn ->


    eval(y) >>= (ƛm ->


    safediv n m))


    @matyo91
    @matyo91
    29-09-2021

    View Slide

  14. eval :: Expr -> Maybe Int


    eval (Val n) = return n


    eval (Div (Expr x) (Expr y)) = do n <- end x


    m <- end y


    safediv n m


    @matyo91
    @matyo91
    29-09-2021

    View Slide

  15. return :: a -> Maybe a


    >>= :: Maybe a -> (a -> Maybe b) -> Maybe b
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  16. Pure Function
    29-09-2021 @matyo91

    View Slide

  17. Referential
    transparency and
    side effects
    @matyo91
    29-09-2021

    View Slide

  18. “Go learn Haskell”


    “Go learn Category Theory”


    “A monad is just a monoid in category of endofunctors”


    (M t) -> (t -> Mu) -> (M u)


    (>>=) :: (Monad m) => m a -> (a -> m b) -> m b
    What is a Monad?
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  19. I don’t fully understand them


    I will share what I’ve learned so far


    You fill in the blanks


    Monads are a loophole in the “Functional contract”


    A way to write pure functions with referential transparency
    The loophole
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  20. Fundamentals
    @matyo91
    29-09-2021
    And theory

    View Slide

  21. function unit($value)


    function bind($monad, function($value) { … })


    $value passed to function in bind <=> $value passed to unit


    All return a new monad


    Function that returns new thing <=> constructor


    All monads are constructors


    Not all constructors are monads
    Unit & bind
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  22. bind(unit($value), $fn) <=> $fn($value)


    bind($monad, unit) <=> $monad


    bind(bind($monad, $fnA), $fnB)


    <=>


    bind($monad, function($value) {

    return bind($fnA($value), $fnB);

    })
    Axioms
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  23. OOP
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  24. Monad::unit($value)->bind($fn) <=> $fn($value)


    $monad->bind(Monad::unit) <=> $monad


    $monad->bind($fnA)->bind($fnB)


    <=>


    $monad->bind(function($value) {

    return $fnA($value)->bind($fnB);

    })
    Axioms of class
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  25. Implementation
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  26. Applications
    @matyo91
    29-09-2021
    And examples

    View Slide

  27. Identity
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  28. Maybe
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  29. Maybe applied to GrandParentName
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  30. Maybe applied to GrandParentName
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  31. Maybe applied to GrandParentName
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  32. Everywhere
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  33. Everywhere
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  34. ircmaxell/monad-php : Monad, Identity, Maybe, Chain, Deferred,
    ListMonad, Promise


    schmittjoh/php-option : Option, LazyOption, Some, None


    GrahamCampbell/Result-Type : Result, Success, Error


    whsv26/functional : Option (do notation), Some, None, Either


    darkwood-fr/railway-fbp : Rail
    Github
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  35. Monads are category of design pattern


    We have been using them for years


    Most common forms : simple implementation


    Strong mathematical knowledge are not required


    Go play !
    Conclusion
    @matyo91
    @matyo91
    29-09-2021

    View Slide

  36. Hello!


    I Am Mathieu Ledru
    You can contact me at @matyo91
    29-09-2021 @matyo91
    Thanks!
    Any questions?

    View Slide