Save 37% off PRO during our Black Friday Sale! »

Monads and usage in PHP

A8ca33aaa087bf3b664e8a0cab415455?s=47 Mathieu Ledru
September 30, 2021

Monads and usage in PHP

This talk is about Monads and theirs use in PHP

Monad exemple code associated to this talk

https://github.com/darkwood-fr/railway-fbp

Article associated to this talk

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

Video associated to this talk

https://youtu.be/0M6MhUpra9o

A8ca33aaa087bf3b664e8a0cab415455?s=128

Mathieu Ledru

September 30, 2021
Tweet

Transcript

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

  2. Why? @matyo91 29-09-2021

  3. A new way to program with effects “ @matyo91 29-09-2021

  4. “Once you understand Monads You lose the ability to explain

    them to other people” Crockford’s Paradox “ @matyo91 29-09-2021
  5. What are Monads? @matyo91 29-09-2021 As functional programming

  6. Data Expr = Val Int | Div Expr Expr @matyo91

    @matyo91 29-09-2021
  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))
  8. eval :: Expr -> Int eval (Val n) = n

    eval (Div (Expr x) (Expr y)) = eval(x) / eval(y) @matyo91 @matyo91 29-09-2021
  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
  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
  11. case [m] as Maybe of Nothing -> Nothing Just x

    -> [f] as function x @matyo91 @matyo91 29-09-2021
  12. m >>= f case m of Nothing -> Nothing Just

    x -> f x @matyo91 @matyo91 29-09-2021
  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
  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
  15. return :: a -> Maybe a >>= :: Maybe a

    -> (a -> Maybe b) -> Maybe b @matyo91 @matyo91 29-09-2021
  16. Pure Function 29-09-2021 @matyo91

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

  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
  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
  20. Fundamentals @matyo91 29-09-2021 And theory

  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
  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
  23. OOP @matyo91 @matyo91 29-09-2021

  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
  25. Implementation @matyo91 @matyo91 29-09-2021

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

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

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

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

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

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

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

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

  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
  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
  36. Hello! I Am Mathieu Ledru You can contact me at

    @matyo91 29-09-2021 @matyo91 Thanks! Any questions?