$30 off During Our Annual Pro Sale. View Details »

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. “Once you understand Monads You lose the ability to explain

    them to other people” Crockford’s Paradox “ @matyo91 29-09-2021
  2. 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))
  3. eval :: Expr -> Int eval (Val n) = n

    eval (Div (Expr x) (Expr y)) = eval(x) / eval(y) @matyo91 @matyo91 29-09-2021
  4. safediv :: Int -> Int -> MayBe Int safediv (Int

    n) (Int m) = if m == 0 then Nothing else Just(n / m) @matyo91 @matyo91 29-09-2021
  5. 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
  6. case [m] as Maybe of Nothing -> Nothing Just x

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

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

    -> (a -> Maybe b) -> Maybe b @matyo91 @matyo91 29-09-2021
  11. “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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. Hello! I Am Mathieu Ledru You can contact me at

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