Skip to content

Instantly share code, notes, and snippets.

@johnjcamilleri
Created March 15, 2013 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnjcamilleri/5171075 to your computer and use it in GitHub Desktop.
Save johnjcamilleri/5171075 to your computer and use it in GitHub Desktop.
-- General
foldl :: (a -> b -> a) -> a -> [b] -> a
foldr :: (a -> b -> b) -> b -> [a] -> b
curry :: ((a, b) -> c) -> a -> b -> c
uncurry :: (a -> b -> c) -> ((a, b) -> c)
(.) :: (b -> c) -> (a -> b) -> (a -> c)
-- Monoids
class Monoid a where
mempty :: a
mappend :: a -> a -> a
-- Law 1. 0 + m == m
-- Law 2. m + 0 == m
-- Law 3. (m1 + m2) + m3 == m1 + (m2 + m3)
-- Functors
class Functor f where
fmap :: (a -> b) -> f a -> f b
-- Identity: fmap id == id
-- Composition: fmap (f . g) == fmap f . fmap g
-- Applicative functors
class Functor f => Applicative f where
pure :: a -> f a
(<*>) :: f (a -> b) -> f a -> f b
(*>) :: f a -> f b -> f b
(<*) :: f a -> f b -> f a
-- Identity: pure id <*> v = v
-- Composition: pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
-- Homomorphism: pure f <*> pure x = pure (f x)
-- Interchange: u <*> pure y = pure ($ y) <*> u
-- Ignore left: u *> v = pure (const id) <*> u <*> v
-- Ignore right: u <* v = pure const <*> u <*> v
-- Monads
class {- Functor m => -} Monad m where
(>>=) :: m a -> (a -> m b) -> m b
(>>) :: m a -> m b -> m b
return :: a -> m a
fail :: String -> m a
-- Law 1. return x >>= f == f x
-- Law 2. m >>= return == m
-- Law 3. (m >>= f) >>= g == m >>= (\x -> f x >>= g)
Monad composition: f >=> g (same as \x -> f x >>= g)
liftM :: (Monad m) => (a -> b) -> (m a -> m b)
liftM2 :: (Monad m) => (a -> b -> c) -> (m a -> m b -> m c)
liftIO :: MonadIO m => IO a -> m a
mapM :: Monad m => (a -> m b) -> [a] -> m [b]
foldM :: Monad m => (a -> b -> m a) -> a -> [b] -> m a
sequence :: Monad m => [m a] -> m [a]
( Suffixed _ makes value type () )
class MonadTrans t where
lift :: Monad m => m a -> t a
class Monad m => MonadPlus m where
mzero :: m a
mplus :: m a -> m a -> m a
-- Control.Monad.Reader
newtype ReaderT r m a = ReaderT { runReaderT :: r -> m a }
type Reader r = ReaderT r Identity
class (Monad m) => MonadReader r m | m -> r where
ask :: m r
reader :: (r -> a) -> m a
local :: (r -> r) -> m a -> m a
asks :: MonadReader r m => (r -> a) -> m a
-- Control.Monad.Writer
newtype WriterT w m a = WriterT { runWriterT :: m (a, w) }
type Writer w = WriterT w Identity
instance (Monoid w, Monad m) => Monad (WriterT w m)
class (Monoid w, Monad m) => MonadWriter w m | m -> w where
tell :: w -> m ()
listen :: m a -> m (a, w)
pass :: m (a, w -> w) -> m a
-- Control.Monad.State
newtype StateT s m a = StateT { runStateT :: s -> m (a, s) }
type State s = StateT s Identity
class (Monad m) => MonadState s m | m -> s where
get :: m s
put :: s -> m ()
gets :: (MonadState s m) => (s -> a) -> m a
modify :: (MonadState s m) => (s -> s) -> m ()
-- Control.Monad.Error
data Either e a = Left e | Right a -- Haskell Prelude
-- Either e is already MonadError
newtype ErrorT e m a = ErrorT { runErrorT :: m (Either e a) }
class (Monad m) => MonadError e m | m -> e where
throwError :: e -> m a
catchError :: m a -> (e -> m a) -> m a
-- Type families
class Add a b where -- uses MultiParamTypeClasses
type AddTy a b -- uses TypeFamilies; :: *
add :: a -> b -> AddTy a b
instance Add Double Integer where
type AddTy Double Integer = Double
add x y = x + fromIntegral y
instance (Num a) => Add a a where -- uses FlexibleInstances
type AddTy a a = a
add x y = x + y
instance (Add Integer a) =>
Add Integer [a] where -- uses FlexibleContexts
type AddTy Integer [a] = [AddTy Integer a]
add x ys = map (add x) ys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment