:: a -> f a (<*>) :: f (a -> b) -> f a -> f b (*>) :: f a -> f b -> f b • instance Applicative Maybe where pure = Just Nothing <*> _ = Nothing (Just f) <*> something = fmap f something • Just (+3) <*> Just 2
-> (a -> m b) -> m b (>>) :: m a -> m b -> m b return :: a -> m a • instance Monad Maybe where return x = Just x (>>=) Nothing f = Nothing (>>=) (Just x) f = f x • Just 1000 >>= half >>= half >>= half >>= half.... half x = if even x then Just (x `div` 2) else Nothing
a -> f a (<*>) :: f (a -> b) -> f a -> f b (*>) :: f a -> f b -> f b class Monad m where (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b return :: a -> m a ※画像引用元 http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html
Minimal complete definition: (>>=) or join (>>=) :: m a -> (a -> m b) -> m b m >>= f = join (fmap f m) (>>) :: m a -> m b -> m b (>>) = (*>) join :: m (m a) -> m a join m = m >>= id return :: a -> m a return = pure fail :: String -> m a fail s = error s