Slide 1

Slide 1 text

Monads @matyo91 29-09-2021 In PHP

Slide 2

Slide 2 text

Why? @matyo91 29-09-2021

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

“Once you understand Monads You lose the ability to explain them to other people” Crockford’s Paradox “ @matyo91 29-09-2021

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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))

Slide 8

Slide 8 text

eval :: Expr -> Int eval (Val n) = n eval (Div (Expr x) (Expr y)) = eval(x) / eval(y) @matyo91 @matyo91 29-09-2021

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

m >>= f case m of Nothing -> Nothing Just x -> f x @matyo91 @matyo91 29-09-2021

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Pure Function 29-09-2021 @matyo91

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

“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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Fundamentals @matyo91 29-09-2021 And theory

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

OOP @matyo91 @matyo91 29-09-2021

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Implementation @matyo91 @matyo91 29-09-2021

Slide 26

Slide 26 text

Applications @matyo91 29-09-2021 And examples

Slide 27

Slide 27 text

Identity @matyo91 @matyo91 29-09-2021

Slide 28

Slide 28 text

Maybe @matyo91 @matyo91 29-09-2021

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Everywhere @matyo91 @matyo91 29-09-2021

Slide 33

Slide 33 text

Everywhere @matyo91 @matyo91 29-09-2021

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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