Last active
August 29, 2015 14:13
-
-
Save simonh1000/fc982d1c8f9409d660ff to your computer and use it in GitHub Desktop.
Functor < Applicative < Monad - demonstration of the hierarchy of type Classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Applicative | |
import Control.Monad | |
-- :k MonadClass :: * -> * | |
data MonadClass a = MonadClass a | |
-- fmap :: (a -> b) -> (m a -> m b) | |
instance Functor MonadClass where | |
fmap f = (<*>) (MonadClass f) | |
-- (<*>) :: m (a -> b) -> m a -> m b | |
instance Applicative MonadClass where | |
pure = return | |
(MonadClass f) <*> m = m >>= (\a -> MonadClass (f a)) | |
{- | |
identity | |
pure id <*> m = | |
m >>= (\a -> MonadClass (id a)) | |
m >>= (\a -> MonadClass a) | |
m >>= return | |
m | |
composition | |
pure (.) <*> u <*> v <*> w = u <*> (v <*> w) | |
u >>= (\a -> MonadClass ((.) a)) <*> v <*> w ..... | |
homomorphism | |
pure f <*> pure x = pure (f x) | |
(return x) >>= (\a -> MonadClass (f a)) | |
g x, where g = (\a -> MonadClass (f a)) | |
MonadClass (f x) | |
return (f x) | |
interchange | |
u <*> pure y = pure ($ y) <*> u | |
-} | |
-- (>>=) :: Monad m => m a -> (a -> m b) -> m b | |
instance Monad MonadClass where | |
return = MonadClass | |
m >>= f = undefined | |
{- | |
Laws | |
(return x) >>= f == f x | |
m >>= return == m | |
(m >>= f) >>= g == m >>= (\x -> f x >>= g) | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment