Documente Academic
Documente Profesional
Documente Cultură
Constructing Monads
Constructing Monads
What is a monad?
According to
www.googlism.com
a monad is...
Constructing Monads
What is a monad?
According to
www.googlism.com
. . . a card game?
a monad is...
Constructing Monads
What is a monad?
According to
www.googlism.com
a monad is...
. . . a card game?
. . . the highest spiritual aspect of the human being?
Constructing Monads
What is a monad?
According to
www.googlism.com
a monad is...
. . . a card game?
. . . the highest spiritual aspect of the human being?
. . . a very specic type of agreement?
Constructing Monads
What is a monad?
According to
www.googlism.com
a monad is...
. . . a card game?
. . . the highest spiritual aspect of the human being?
. . . a very specic type of agreement?
. . . capable of creating twelve souls?
Constructing Monads
What is a monad?
According to
www.googlism.com
a monad is...
. . . a card game?
. . . the highest spiritual aspect of the human being?
. . . a very specic type of agreement?
. . . capable of creating twelve souls?
. . . like a mirror?
Constructing Monads
What is a monad?
According to
www.googlism.com
a monad is...
. . . a card game?
. . . the highest spiritual aspect of the human being?
. . . a very specic type of agreement?
. . . capable of creating twelve souls?
. . . like a mirror?
. . . surrounded by a hazy glow?
Constructing Monads
Monads in Haskell
class Monad m where
return :: a m a
(>>=) :: m a (a m b) m b
Constructing Monads
Monads in Haskell
class Monad m where
return :: a m a
(>>=) :: m a (a m b) m b
Example
ex1 :: (Monad m) m
ex1 = do x return
y return
return (x +
Int
1
2
y)
Constructing Monads
Monads in Haskell
class Monad m where
return :: a m a
(>>=) :: m a (a m b) m b
Example
ex1 :: (Monad m) m
ex1 = do x return
y return
return (x +
Int
1
2
y)
Desugared to:
ex1 :: (Monad
ex1 = return
return
return
m) m Int
1 >>= x
2 >>= y
(x + y)
Constructing Monads
Constructing Monads
Constructing Monads
Constructing Monads
Constructing Monads
State Monads
Constructing Monads
State Monads
Constructing Monads
Exception Monads
Constructing Monads
Exception Monads
Constructing Monads
Continuation Monads
Constructing Monads
Continuation Monads
ten :: (ContM m) m () m ()
ten m = do (x,l) labelCC 1
m
when (x 10) (jump (x+1) l)
Constructing Monads
Constructing Monads
test :: (...) m ()
test = do env ask
x get
when (x > env) (raise "Too big!")
Constructing Monads
test :: (...) m ()
test = do env ask
x get
when (x > env) (raise "Too big!")
The context is:
Constructing Monads
Constructing Monads
Constructing Monads
Monad Transformers
Constructing Monads
Monad Transformers
Constructing Monads
Monad Transformers
Constructing Monads
Base Monads
To construct a monad:
Constructing Monads
Base Monads
To construct a monad:
n where
Example:
Id
and
Lift
Constructing Monads
Constructing Monads
A Monadic Tower
Constructing Monads
Constructing Monads
Execution
So far:
Constructing Monads
Execution
So far:
runReaderT
:: i ReaderT i m a m a
Constructing Monads
Execution
So far:
runReaderT
runWriterT
:: i ReaderT i m a m a
:: WriterT i m a m (a,i)
Constructing Monads
Execution
So far:
runReaderT
runWriterT
runStateT
:: i ReaderT i m a m a
:: WriterT i m a m (a,i)
:: i StateT i m a m (a,i)
Constructing Monads
Execution
So far:
runReaderT :: i ReaderT i m a m a
runWriterT :: WriterT i m a m (a,i)
runStateT
:: i StateT i m a m (a,i)
runExceptionT::ExceptionT i m a m (Either i a)
Constructing Monads
Execution
So far:
runReaderT :: i ReaderT i m a m a
runWriterT :: WriterT i m a m (a,i)
runStateT
:: i StateT i m a m (a,i)
runExceptionT::ExceptionT i m a m (Either i a)
runContT
:: (a m i) ContT i m a m i
Constructing Monads
Example
test :: IO ()
test = do x runStateT 0
$ runExceptionT
$ runReaderT 'q' loop
print x
where
loop = do x inBase getChar
end ask
when (x 6= end) $
do when (not (isDigit x)) $ raise "Bad input."
let n = fromEnum x - fromEnum '0'
tot get
set (tot + n)
loop
Constructing Monads
Orthogonality of Eects
Constructing Monads
Example
Constructing Monads
Example
Constructing Monads
Overloaded Execution
Constructing Monads
RunReader
runReaderT
:: i ReaderT i m a m a
i where
Constructing Monads
RunReader
runReaderT
:: i ReaderT i m a m a
i where
Example
inModCtxt :: (RunReaderM m i) (i i) m a m a
inModCtxt f m = do x ask
local (f x) m
Constructing Monads
RunWriter
runWriterT
:: WriterT i m a m (a,i)
i where
Constructing Monads
RunWriter
runWriterT
:: WriterT i m a m (a,i)
i where
Example
censor :: (RunWriterM m i) (i i) m a m a
censor f m = do (a,xs) collect m
put (f xs)
return a
Constructing Monads
RunException
i where
Constructing Monads
RunException
i where
Example
handle :: (RunExceptionM m x) m a (x m a) m a
handle m h = do r try m
case r of
Left err h err
Right a return a
Constructing Monads
Summary
Constructing Monads
Resources
Libraries:
http://www.galois.com/~diatchki/monadLib
Some papers:
Monad Transformers and Modular Interpreters
Mark P. Jones