Skip to content

Instantly share code, notes, and snippets.

@LambdaP
Last active August 29, 2015 14:17
Show Gist options
  • Save LambdaP/edfe1b76d6028e85b63d to your computer and use it in GitHub Desktop.
Save LambdaP/edfe1b76d6028e85b63d to your computer and use it in GitHub Desktop.
Monad equivalence
{-# LANGUAGE UndecidableInstances, FlexibleInstances #-}
import Prelude hiding (return, join, (=<<))
-- A Monad can be defined either with (>>=) + return, or with
-- fmap + join + return
class Return r where
return :: a -> r a
class Return m => MonadBind m where
(=<<) :: (a -> m b) -> m a -> m b
class (Return m, Functor m) => MonadJoin m where
join :: m (m a) -> m a
instance MonadBind m => Functor m where
fmap = (=<<) . (return .)
instance MonadBind m => MonadJoin m where
join = (=<<) id
instance MonadJoin m => MonadBind m where
(=<<) = (join .) . fmap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment