Share
Embed
Transcript
40 slides
-
Functor • จ຺Λ࣋ͬͨʹؔΛద༻͢Δ • f aʹ(a -> b)Λద༻͍ͨ͠ • r
-> a͔Βr -> bʹ • fmap :: (Functor f) => (a -> b) -> f a -> f b -
Applicative Functor • จ຺Λ࣋ͬͨʹจ຺Λ࣋ͬͨؔΛద༻͢ Δ • (<*>) :: (Applicatiove f)
=> f (a -> b) -> f a -> f b • ௨ৗͷΛจ຺ʹ͘ΔΉ(pure) • Applicativeʮจ຺ͷՃ͞Εͨʯ • ࣦഊ͢Δ͔͠Εͳ͍ɼඇܾఆతetc -
Monad • Applicativeͷࣗવͳ֦ு(ڧ͍൛) • ී௨ͷΛͱͬͯจ຺͖ͷΛฦؔ͢ ʹɼจ຺͖ͷΛ͍ͨ͠ • (>>=) :: (Monad
m) => m a -> (a -> m b) -> m b • Monad>>=Λαϙʔτ͢ΔApplicative Functor • >>=όΠϯυͱݺͿ -
Maybeࣦഊ͢Δ͔͠Εͳ͍ܭࢉ > Just (++ "&") <*> Just "yotsuba" yotsuba& >
Nothing <*> Just "yanda" Nothing > (,) <$> Just "yotsuba" <*> Just "to-chan" Just ("yotsuba", "to-chan") > (,) <$> Just "yanda" <*> Nothing Nothing -
>>=Λ࣮͢Δ • Applicative FunctorͷΑ͏ʹҰͱ͍͔ͯΒద ༻͢Δ applyMaybe :: Maybe a ->
(a -> Maybe b) -> Maybe b applyMaybe Nothing f = Nothing applyMaybe (Just x) f = f x -
ͬͯΈΔ > 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 -
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 -
Monadͷؔ(1) • return • ApplicativeͰ͍͏pure • IOͰͬͨͭ • >>= •
όΠϯυ • Monadʹʮ௨ৗͷΛऔͬͯMonadΛฦ ؔ͢ʯΛద༻͢Δ -
Monadͷؔ(2) • >> • طఆͷMonadΛฦؔ͢ • ͋ͱͰৄ͘͠ • fail •
ύλʔϯϚονͷΤϥʔϋϯυϦϯάʹ ͏ -
Maybeͷ࣮ΛݟͯΈΔ instance Monad Maybe where return x = Just x
Nothing >>= f = Nothing Just x >>= f = f x fail _ = Nothing -
• ύλʔϯϚονແ͠ͰMaybe͔ΒΛऔΓग़͠ ͨΑ͏ʹݟ͑Δ • จ຺Λҡ͍࣋ͯ͠Δ(Nothingͷͱ͖Nothing) > return "yotsuba" :: Maybe
String Just "yotsuba" > Just "yotsuba" >>= \x -> Just (x ++ "&") Just "yotsuba&" > Nothing >>= \x -> Just (x ++ "&") Nothing -
·ͣී௨ʹ࡞Δ > 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) -
ࣦഊΛදݱͰ͖ΔΑ͏ʹMaybeͰแΉ > landLeft 2 (0, 0) Just (2, 0) >
landLeft 10 (0, 3) Nothing -
͜͜Ͱᰜͱ>>=ͷొʂ > 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 -
Monad͍͢͝ʂ • Maybe͕Applicativeͳ͚ͩͰ͜͏ͳΒͳ͍ • Applicative FunctorͰApplicativeͲ͏͠Λ ਂ͘૬ޓ࡞༻Ͱ͖ͳ͍ • ௨ৗͷؔʹApplicative StyleͰҾΛ͢
• ϐΤʔϧͷྫલͷ݁Ռʹґଘ͍ͯ͠Δ • લͷ݁ՌΛ౿·͑ͯ݁Ռ͕ݕূ͞ΕΔ -
ϩʔϓ্ͷόφφ • ϐΤʔϧΛΒͤΔbananaͷಋೖ > return (0, 0) >>= landLeft 1
>>= banana >>= landRight 1 Nothing banana :: Pole -> Maybe Pole banana _ = Nothing -
>>Λ͍͍͑Α > 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 -
͠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 -
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) -
doه๏syntax sugar • ҎԼͷ;ͨͭಉ͡ҙຯ do { x ; result <-
y ; foo result } ! x >> y >>= \result -> foo result -
ϦετϞφυ • 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] -
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] -
ۭϦετͷͱ͖ʁ > [] >>= \x -> ["bad", "mad", "red"] []
> [1, 2, 3] >>= \x -> [] [] -
࿈݁ͤͯ͞ΈΔ > [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) -
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ͷੑ࣭Λ߹Θͤ࣋ͭܕΫϥε -
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) :: [()] [] -
ϦετแදهͱൺΔ > [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 -
ࠨ߃ੑ • 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"] -
ӈ߃ੑ • >>=ΛͬͯMonadΛreturnʹద༻ͯ͠Կ ى͜Βͳ͍ > Just "move on up" >>=
return Just "move on up" > [1, 2, 3, 4] >>= return [1, 2, 3, 4] > putStrLn "Wah!" >>= return Wah! -
݁߹๏ଇ • >>=ͷ࿈͕͋Δͱ͖ɼͲͷॱͰධՁͯ݁͠ Ռಉ͡ • (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) -
Ϟφυؔͷ߹ • >>=Λ͑߹Ͱ͖Δ (<=<) :: (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]