Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
×

# TypeLevel Summit

Slides of my talk about the intuition behind monads
https://typelevel.org/event/2018-03-summit-boston/

• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here

### TypeLevel Summit

1. 1. March 20, 2018 Why Monads? Luca Belli Twitter Cortex
2. 2. Luca Belli, Ph.D. Senior Software Engineer at Twitter Cortex @__lucab • studied math a bunch of years • lived in the area for 3 winters ❄ • moved to sunny California to do Machine Learning Deck title About me X
3. 3. 6 but why?
4. 4. 8 COMPOSITION
5. 5. 9 game :: Maybe Deck game = case addCard 5 0 of -- returns a Maybe Deck Nothing -> Nothing Just deck1 -> case addCard 4 deck1 of -- returns a Maybe Deck Nothing -> Nothing Just deck2 -> case addCard 3 deck2 of -- returns a Maybe Deck Nothing -> Nothing Just deck3 -> addCard 2 deck3 -- returns a Maybe Deck Haskell Primer
6. 6. 10 game = emptyDeck >>= addFirstCard >>= addSecondCard >>= addThirdCard Haskell Primer
7. 7. 11 1 2 3 Haskell Functor Monads (Functor++) Agenda
8. 8. 13 -- define a function of one variable f :: Int -> Int f x = x + 1 λ> f 2 3 Haskell Primer
9. 9. 14 -- two variables g :: Int -> Int -> Int g x y = x + y λ> g 1 2 3 Haskell Primer
10. 10. 15 -- currying is free! g :: Int -> Int -> Int g x y = x + y f :: Int -> Int f y = y + 1 f = g 1 Haskell Primer
11. 11. 16 g :: Str -> Int g x = length x f :: Int -> Int f x = x + 1 Haskell Primer
12. 12. 17 g :: Str -> Int g x = length x f :: Int -> Int f x = x + 1 h :: Str -> Int h x = f(g(x)) = f (g x) = (f . g) x Haskell Primer
13. 13. 18 data Maybe a = Just a | Nothing Haskell Primer Represent failure
14. 14. 19 data Maybe a = Just a | Nothing -- non secure connections are discarded gMaybe :: Str -> Maybe Int gMaybe str | "https" `isPrefixOf` str = Just (length str) | otherwise = Nothing Haskell Primer Variation on g
15. 15. 20 -- can we compose with f now? gMaybe :: Str -> Maybe Int f :: Int -> Int f x = x + 1 Haskell Primer
16. 16. 21 -- let's try to change f fMaybe :: Maybe Int -> Int Haskell Primer Adapting f
17. 17. 22 -- let's try to change f fMaybe :: Maybe Int -> Int fMaybe Just x = f x Haskell Primer Adapting f
18. 18. 23 -- let's try to change f fMaybe :: Maybe Int -> Int fMaybe Just x = f x fMaybe Nothing = ??? -- 0? Infinity? Haskell Primer Adapting f
19. 19. 24 -- f :: Int -> Int fMaybe :: Maybe Int -> Maybe Int fMaybe Just x = Just (f x) fMaybe Nothing = Nothing Haskell Primer
20. 20. 25 gList :: Str -> [Int] Haskell Primer List
21. 21. 26 gList :: Str -> [Int] fList :: [Int] -> Int Haskell Primer
22. 22. 27 gList :: Str -> [Int] fList :: [Int] -> Int fList [] = ??? Haskell Primer
23. 23. 28 gList :: Str -> [Int] fList :: [Int] -> Int fList [] = ??? fList x:xs = ??? -- what do I do with those? Haskell Primer
24. 24. 29 gList :: Str -> [Int] fList :: [Int] -> [Int] fList [] = [] fList xs = map f xs Haskell Primer Last example
25. 25. 30 g :: Str -> Int f :: Int -> Int gMaybe :: Str -> Maybe Int fMaybe :: Maybe Int -> Maybe Int Haskell Primer
26. 26. 31 g :: Str -> Int f :: Int -> Int gList :: Str -> [Int] fList :: [Int] -> [Int] Haskell Primer
27. 27. 32 • composed 2 functions f(g(x)) Recap
28. 28. 33 • composed 2 functions f(g(x)) • changed g to return enhanced types • had to change f accordingly Recap
29. 29. 34 • composed 2 functions f(g(x)) • changed g to return enhanced types • had to change f accordingly Recap
30. 30. 35 why f?
31. 31. 36 class Functor F where map :: (a -> b) -> F a -> F b Haskell Primer Abstracting the pattern
32. 32. 37 class Functor F where map :: (a -> b) -> (F a -> F b) Haskell Primer Abstracting the pattern
33. 33. 38 -- map :: (a -> b) -> F a -> F b -- map :: (a -> b) -> Maybe a -> Maybe b Haskell Primer Abstracting the pattern
34. 34. 39 -- map :: (a -> b) -> F a -> F b -- map :: (a -> b) -> Maybe a -> Maybe b instance Functor Maybe where map f Nothing = Nothing Haskell Primer Abstracting the pattern
35. 35. 40 -- map :: (a -> b) -> F a -> F b -- map :: (a -> b) -> Maybe a -> Maybe b instance Functor Maybe where map f Nothing = Nothing map f (Just x) = Just (f x) Haskell Primer Abstracting the pattern
36. 36. 41 -- map :: (a -> b) -> F a -> F b -- map :: (a -> b) -> Maybe a -> Maybe b instance Functor Maybe where map f Nothing = Nothing map f (Just x) = Just (f x) fMaybe = map f Haskell Primer Abstracting the pattern
37. 37. 42 -- map :: (a -> b) -> F a -> F b -- map :: (a -> b) -> [a] -> [b] Haskell Primer Abstracting the pattern
38. 38. 43 -- map :: (a -> b) -> F a -> F b -- map :: (a -> b) -> [a] -> [b] instance Functor List where map _ [] = [] Haskell Primer Abstracting the pattern
39. 39. 44 -- map :: (a -> b) -> F a -> F b -- map :: (a -> b) -> [a] -> [b] instance Functor List where map _ [] = [] map f x:xs = f x : map xs Haskell Primer Abstracting the pattern
40. 40. 45 -- map :: (a -> b) -> F a -> F b -- map :: (a -> b) -> [a] -> [b] instance Functor List where map _ [] = [] map f x:xs = f x : map xs fList = map f Haskell Primer Abstracting the pattern
41. 41. 46 • enhanced type lift functions • composition is possible via map f Recap
42. 42. 47 type Deck = Int type Card = Int Monads Let’s play a game
43. 43. 48 type Deck = Int type Card = Int addCard :: Card -> Deck -> Deck Let’s play a game Monads
44. 44. 49 type Deck = Int type Card = Int addCard :: Card -> Deck -> Deck addCard c d = c + d Let’s play a game Monads
45. 45. 50 -- Let's start a game: oneCard :: Deck oneCard = addCard 5 0 Monads
46. 46. 51 twoCard = addCard 4 oneCard -- equivalent to addCard 4 (addCard 5 0) Monads
47. 47. 52 -- what happens if we add more? addCard 2 (addCard 3 (addCard 4 (addCard 5 0 ))) Haskell Primer The whole game
48. 48. 53 addCard :: Card -> Deck -> Maybe Deck addCard c d | c + d < 10 = Just (c + d) | otherwise = Nothing -- you lose Let’s make the game more interesting Monads
49. 49. 54 addCard :: Card -> Deck -> Maybe Deck oneCard :: Maybe Deck oneCard = addCard 5 0 Monads
50. 50. 55 addCard :: Card -> Deck -> Maybe Deck oneCard :: Maybe Deck oneCard = addCard 5 0 -- Deck != Maybe Deck addCard 3 oneCard Monads
51. 51. 56 game :: Maybe Deck game = case addCard 5 0 of -- returns a Maybe Deck Nothing -> Nothing Just deck1 -> case addCard 4 deck1 of -- returns a Maybe Deck Nothing -> Nothing Just deck2 -> case addCard 3 deck2 of -- returns a Maybe Deck Nothing -> Nothing Just deck3 -> addCard 2 deck3 -- returns a Maybe Deck Monads
52. 52. 58 addFirstCard :: Deck -> Maybe Deck addFirstCard = addCard 5 addSecondCard :: Deck -> Maybe Deck addSecondCard = addCard 4 addThirdCard :: Deck -> Maybe Deck addThirdCard = addCard 3 Monads
53. 53. 59 Maybe Deck -> -- previous total (Deck -> Maybe Deck) -> -- addCard Maybe Deck -- new total Monads
54. 54. 60 (>>=) :: Maybe Deck -> -- previous total (Deck -> Maybe Deck) -> -- addCard Maybe Deck -- new total Monads
55. 55. 61 emptyDeck :: Maybe Deck addCard 5 :: Deck -> Maybe Deck emptyDeck >>= addCard 5 Monads
57. 57. 63 -- doesn’t look right emptyDeck = addCard 0 0 Monads
58. 58. 64 -- the minimal context that contains a 0 emptyDeck = return 0 Monads
59. 59. 65 Monad = Functor + return + (>>=)
60. 60. 66 (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b Monads
61. 61. 67 (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b instance Monad Maybe where return x = Just x Monads
62. 62. 68 (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b instance Monad Maybe where return x = Just x Nothing >>= _ = Nothing Monads
63. 63. 69 (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b instance Monad Maybe where return x = Just x Nothing >>= _ = Nothing (Just x) >>= f = f x Monads
64. 64. 70 (>>=) :: [a] -> (a -> [b]) -> [b] What about Lists? Monads
65. 65. 71 (>>=) :: [a] -> (a -> [b]) -> [b] (>>=) = flatMap Haskell Primer What about Lists?
66. 66. 72 (>>=) :: [a] -> (a -> [b]) -> [b] (>>=) = flatMap instance Monad List where return x = [x] Haskell Primer What about Lists?
67. 67. 73 (>>=) :: [a] -> (a -> [b]) -> [b] (>>=) = flatMap instance Monad List where return x = [x] xs >>= f = concat (map f xs) Haskell Primer What about Lists?
68. 68. 74 g :: a -> Maybe b f :: b -> Maybe c Recap Recap
69. 69. 75 g :: a -> Maybe b f :: b -> Maybe c (>>=) :: m b -> (b -> m c) -> m c extracts the b in the output of g and feeds it to f Recap Recap
70. 70. 76 COMPOSITION
71. 71. Luca Belli March 20, 2018 Thank you! @__lucab