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

sugoih-in-osaka#13

yashigani
February 19, 2014

 sugoih-in-osaka#13

すごいH本読書会 #13
http://iseebi.github.io/sugoih/

yashigani

February 19, 2014
Tweet

More Decks by yashigani

Other Decks in Programming

Transcript

  1. Functor • จ຺Λ࣋ͬͨ஋ʹؔ਺Λద༻͢Δ • f aʹ(a -> b)Λద༻͍ͨ͠ • r

    -> a͔Βr -> bʹ • fmap :: (Functor f) => (a -> b) -> f a -> f b
  2. Applicative Functor • จ຺Λ࣋ͬͨ஋ʹจ຺Λ࣋ͬͨؔ਺Λద༻͢ Δ • (<*>) :: (Applicatiove f)

    => f (a -> b) -> f a -> f b • ௨ৗͷ஋Λจ຺ʹ͘ΔΉ(pure) • Applicative஋͸ʮจ຺ͷ௥Ճ͞Εͨ஋ʯ • ࣦഊ͢Δ͔΋͠Εͳ͍ɼඇܾఆతetc
  3. Monad • Applicative஋ͷࣗવͳ֦ு(ڧ͍൛) • ී௨ͷ஋Λͱͬͯจ຺෇͖ͷ஋Λฦؔ͢਺ ʹɼจ຺෇͖ͷ஋Λ౉͍ͨ͠ • (>>=) :: (Monad

    m) => m a -> (a -> m b) -> m b • Monad͸>>=Λαϙʔτ͢ΔApplicative Functor • >>=͸όΠϯυͱݺͿ
  4. Maybe͸ࣦഊ͢Δ͔΋͠Εͳ͍ܭࢉ > Just (++ "&") <*> Just "yotsuba" yotsuba& >

    Nothing <*> Just "yanda" Nothing > (,) <$> Just "yotsuba" <*> Just "to-chan" Just ("yotsuba", "to-chan") > (,) <$> Just "yanda" <*> Nothing Nothing
  5. >>=Λ࣮૷͢Δ • Applicative FunctorͷΑ͏ʹҰ౓ͱ͍͔ͯΒద ༻͢Δ applyMaybe :: Maybe a ->

    (a -> Maybe b) -> Maybe b applyMaybe Nothing f = Nothing applyMaybe (Just x) f = f x
  6. ࢖ͬͯΈΔ > Just "yotsuba" `applyMaybe` \x -> Just (x +

    + "&") Just "yotsuba&" > Nothing `applyMaybe` \x -> Just (x ++ "&") Nothing ! > Just "yotsuba" `applyMaybe` \x -> if x == "yanda" then Nothing else Just (x ++ "&") Just "yotsuba&" > Just "yanda" `applyMaybe` \x -> if x == "yanda" then Nothing else Just (x ++ "&") Nothing
  7. MonadܕΫϥε • ApplicativeͷΠϯελϯεͰ͸ͳ͍ class Monad m where return :: a

    -> m a ! (>>=) :: m a -> (a -> m b) -> m b ! (>>) :: m a -> m b -> m b x >> y = x >>= \_ -> y ! fail :: String -> m a fail msg = error msg
  8. Monadͷؔ਺(1) • return • ApplicativeͰ͍͏pure • IOͰ࢖ͬͨ΍ͭ • >>= •

    όΠϯυ • Monad஋ʹʮ௨ৗͷ஋ΛऔͬͯMonad஋Λฦ ؔ͢਺ʯΛద༻͢Δ
  9. Maybeͷ࣮૷ΛݟͯΈΔ instance Monad Maybe where return x = Just x

    Nothing >>= f = Nothing Just x >>= f = f x fail _ = Nothing
  10. • ύλʔϯϚονແ͠ͰMaybe͔Β஋ΛऔΓग़͠ ͨΑ͏ʹݟ͑Δ • จ຺Λҡ͍࣋ͯ͠Δ(Nothingͷͱ͖͸Nothing) > return "yotsuba" :: Maybe

    String Just "yotsuba" > Just "yotsuba" >>= \x -> Just (x ++ "&") Just "yotsuba&" > Nothing >>= \x -> Just (x ++ "&") Nothing
  11. ·ͣ͸ී௨ʹ࡞Δ > landLeft 2 (0, 0) (2, 0) > landRight

    1 (1, 2) (1, 3) > landRight -1 (1, 2) (1, 1) > land Left 2 (landRight 1 (landLeft 1 (0, 0))) > (3, 1) ! # -:を導入 > (0, 0) -: landLeft 1 -: landRight 4 -: landLeft (-1) -: landRight (-2) (0, 2)
  12. ͜͜Ͱᰜ૘ͱ>>=ͷొ৔ʂ > landRight 1 (0, 0) >>= landLeft 2 Just

    (2, 1) # Nothingを渡してみる > Nothing >>= landLeft 2 Nothing > return (0, 0) >>= landRight 2 >>= landLeft 2 >>= landRight 2 Just (2, 4) # さっきダメだった例を試す > return (0, 0) >>= landLeft 1 >>= landRight 4 >>= landLeft (-1) >>= landRight (-2) Nothing
  13. ϩʔϓ্ͷόφφ • ϐΤʔϧΛ׈ΒͤΔbananaͷಋೖ > return (0, 0) >>= landLeft 1

    >>= banana >>= landRight 1 Nothing banana :: Pole -> Maybe Pole banana _ = Nothing
  14. >>Λ࢖͑͹͍͍Α > Nothing >> Just 3 Nothing > Just 3

    >> Just 4 Just 4 > Just 3 >> Nothing Nothing ! # bananaのかわりに使う > return (0, 0) >>= landLeft 1 >> Nothing >>= landRight 1 Nothing (>>) :: (Monad m) => m a -> m b -> m b m >> n = m >>= \_ -> n
  15. ΋͠΋Monad͕ແ͔ͬͨΒ routine :: Maybe Pole routine = case landLeft 1

    (0, 0) of Nothing -> Nothing Just pole1 -> case landRight 4 pole1 of Nothing -> Nothing Just pole2 -> case landLeft 2 pole2 of Nothing -> Nothing Just pole3 -> landLeft 1 pole3
  16. doه๏ foo :: Maybe String foo = Just 3 >>=

    (\x -> Just "!" >>= (\y -> Just (show x ++ y))) ! -- これはdo記法を使うとこう書ける ! foo' :: Maybe String foo' = do x <- Just 3 y <- Just "!" Just (show x ++ y)
  17. doه๏͸syntax sugar • ҎԼͷ;ͨͭ͸ಉ͡ҙຯ do { x ; result <-

    y ; foo result } ! x >> y >>= \result -> foo result
  18. ϦετϞφυ • Applicativeͱͯ͠ͷϦετ͸ඇܾఆతܭࢉΛද ͢ > (*) <$> [1, 2, 3]

    <*> [10, 100, 1000] [10, 100, 1000, 20, 200, 2000, 30, 300, 3000] ! > [1, 2, 3] >>= \x -> [10, 100, 1000] >>= \y -> [x * y] [10, 100, 1000, 20, 200, 2000, 30, 300, 3000]
  19. instance Monad [] where return x = [x] xs >>=

    f = concat (map f xs) fail _ = [] ϦετϞφυͷ࣮૷ > [3, 4, 5] >>= \x -> [-x, x] [-3, 3, -4, 4, -5, 5] ! [3, 4, 5] >>= \x -> [-x, x] concat (map (\x -> [-x, x]) [3, 4, 5]) concat [[-3, 3], [-4, 4], [-5, 5]] [-3, 3, -4, 4, -5, 5]
  20. ࿈݁ͤͯ͞ΈΔ > [1, 2] >>= \n -> ['a', 'b'] >>=

    \ch -> return (n, ch) [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')] listOfTuples :: [(Int, Char)] listOfTuples = do n <- [1, 2] ch <- ['a', 'b'] return (n, ch)
  21. MonadPlus class Monad m => MonadPlus m where mzero ::

    m a mplus :: m a => m a -> m a ! -- mzero = mempty -- mplus = mappend ! instance MonadPlus [] where mzero = [] mplus = (++) • Monoidͷੑ࣭Λ߹Θͤ࣋ͭܕΫϥε
  22. guardؔ਺ guard :: (MonadPlus m) => Bool -> m ()

    guard True = return () guard False = mzero • TrueͳΒ()Λ࠷খͷߏจʹೖΕɼFalseͳΒࣦഊ > guard (5 > 2) :: Maybe () Just () > guard (1 > 2) :: Maybe () Nothing > guard (5 > 2) :: [()] [()] > guard (1 > 2) :: [()] []
  23. Ϧετ಺แදهͱൺ΂Δ > [x | x <- [1..50], '7' `elem` show

    x] [7, 17, 27, 37, 47] ! > [1..50] >>= (\x -> guard ('7' `elem` show x) >> return x) [7, 17, 27, 37, 47] sevensOnly :: [Int] sevensOnly = do x <- [1..50] guard ('7' `elem` show x) return x
  24. ࠨ߃౳ੑ • returnͨ͠΋ͷΛ>>=ͨ͠ͱ͖͸ɼݩͷ஋ʹؔ ਺Λద༻ͨ͠ͱ͖ͱಉ͡ > return 3 >>= (\x ->

    Just (x + 100000)) Just 100003 > (\x -> Just (x + 100000)) 3 Just 100003 > return "WoM" >>= (\x -> [x, x, x]) ["WoM", "WoM", "WoM"] > (\x -> [x, x, x]) "WoM" ["WoM", "WoM", "WoM"]
  25. ӈ߃౳ੑ • >>=Λ࢖ͬͯMonad஋Λreturnʹద༻ͯ͠΋Կ ΋ى͜Βͳ͍ > Just "move on up" >>=

    return Just "move on up" > [1, 2, 3, 4] >>= return [1, 2, 3, 4] > putStrLn "Wah!" >>= return Wah!
  26. ݁߹๏ଇ • >>=ͷ࿈࠯͕͋Δͱ͖ɼͲͷॱͰධՁͯ͠΋݁ Ռ͸ಉ͡ • (m >>= f) >>= gͱm

    >>= (\x -> f x >>= g) > ((return (0, 0) >>= landRight 2) >>= landLeft 2 >>= landRight 2 Just (2, 4) > return (0, 0) >>= (\x -> landRight 2 x >>= (\y -> landLeft 2 >>= (\z -> landRight 2 z))) Just (2, 4)
  27. Ϟφυؔ਺ͷ߹੒ • >>=Λ࢖͑͹߹੒Ͱ͖Δ (<=<) :: (Monad m) => (b ->

    m c) -> (a -> m b) -> (a -> m c) f <=< g = (\x -> g x >>= f) > ((\x -> [-x, x]) <=< (\y -> [y * 2, y * 3])) 5 [-10, 10, -15, 15]