Monads_and_effects
Published: 13:19, Thursday 14 July 2011
Notes
What's this? See my article about Notes.
View as plain text file.
monads: triples
computation: partiality, nondeterminism, side-effects, exceptions, continuations, interactive input, interactive output
category theory:
category C
set/class of objects: |C|
hom-set of morphisms from A to B: C(A,B)
composition: g o f
identity: id_A
functor: F:C->D
natural transformation: \s:F->G
right/left adjoint
kleisli triple
kleisly category
monad over C
triple (T,\n,\m):
T:C->C fnctor, \n:id_C->T and \m:T^2->T natural transformations
and 2 diagrams
EX bijection between kleisli triples and monads
eilenberg-moore category
monad morphism
metalanguage
eilenberg-moore algebras
cnb translation of haskell
algol translation
typing
cbv translation
monad transformers
monads in haskell
kleisli triple over cat C is (T,\n,_*), T:|C|->|C|,\nA:A->TA for A in |C|
f*:TA->TB for f:A->TB and ...
C is category with
haskell types as objects and haskell functions as arrows
T is a parameterised type
\n is return :: a -> T a
_* is (>>=) :
(a -> T b) -> (T a -> T b)
more convenient :: T a -> (a -> T b) -> T b
e1 :: T a, e2 :: T b
e1 >>= \x -> e2 :: T b
e1 is translated into something which can be taken by x
a :: t
return a :: T t
return a >>= \x -> return x :: T t
partiality: data Maybe a
side effects:
type State s a = s -> (s,a)
newtype State s a = State (s -> (s, a)) -- chosen
data State s a = State (s -> (s, a))
readState :: State s s
readState = State (\s -> (s,s))
writeState :: s -> State s ()
writeState s = State (\_ -> (s,()))
increment :: State Int ()
increment = readState >>= \s -> writeState (s+1)
generic monadic computations:
addM a b = a >>= \m -> b >>= \n -> return (m+n)
addM (Just 2) (Just 3)
addM = liftM2 (+)
liftM :: Monad m => (a -> b) -> m a -> m b
liftM2 :: Monad m => (a -> b -> c) -> m a -> m b -> m c
sequence :: Monad m => [m a] -> m [a]
applying monads:
type IO a = State Universe a
mix lazy evaluation cleanly with i/o is unique to haskell
imperative algos: some can be done only with monads
domain specific embedded languages: parser
monad transformers:
partiality monad transformer:
newtype MaybeT m a = MaybeT (m (Maybe a))
class of monad transformers:
class (Monad m, Monad (t m)) => MonadTransformer t m where
lift :: m a -> t m a
instance Monad m => MonadTransformer MaybeT m where
lift m = MaybeT (do x <- m
return (Just x))
class Monad m => MaybeMonad m where
failure :: m a
handle :: m a -> m a -> m a
instance Monad m => MaybeMonad (MaybeT m) where
failure = MaybeT (return Nothing)
MaybeT m 'handle' MaybeT m' =
MaybeT (do x <- m
case x of
Nothing -> m'
Just a -> return (Just a))
runMaybe :: Monad m => MaybeT m a -> m a
runMaybe (MaybeT m) = do x <- m
case x of
Just a -> return a
state transformers
environment readers
continuations
monads and dsls
design principle
guide the implementation
share a parte of their interface
exercises
intermediate languages for compilation
type and effect systems
monads and effect systems
Write a Comment
* These fields are mandatory.