Skip to content

Instantly share code, notes, and snippets.

@mikamix
Last active August 29, 2015 14:18
Show Gist options
  • Save mikamix/da261e86a251ba954c4b to your computer and use it in GitHub Desktop.
Save mikamix/da261e86a251ba954c4b to your computer and use it in GitHub Desktop.
module Monad where
import Prelude hiding (fmap, Functor, Monad)
class Functor f where
fmap :: (a -> b) -> f a -> f b
class Functor m => Applicative m where
pure :: a -> m a
ap :: m (a -> b) -> m a -> m b
class (Functor m, Applicative m) => Monad m where
(<$>) :: (a -> b) -> m a -> m b
f <$> a = bind (pure . f) a
(<*>) :: m (a -> b) -> m a -> m b
f <*> a = bind (<$> a) f
join :: m (m a) -> m a
join = bind id
bind :: (a -> m b) -> m a -> m b
bind f = join . fmap f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment