monad-task-0.2.0: A monad transformer that turns event processing into co-routine programming.

Safe HaskellNone
LanguageHaskell98

Control.Monad.Task

Contents

Description

Task monad transformer can help refactor event and callback heavy programs into monads via co-routines. The idea is loosely based on Combining Events And Threads For Scalable Network Services, by Peng Li and Steve Zdancewic, in PLDI, 2007. (http://www.cis.upenn.edu/~stevez/papers/abstracts.html#LZ07), but with deterministic and co-operative lightweight threads, also known as co-routines, so that the base monad can be anything ranging from IO to state monads, or your favorite monad transformer stack.

Besides, Task monad transformer also provides a simple mechanism to signal and watch for events, which allows complex event processing logic to be expressed as streamlined monadic co-routines.

Task monad transformer is essentially a ContT, or continuation transformer, defined to extract the control flow of monadic programs with co-operative multi-threading. After the CPS transformation, the program trace is then executed with a simple round-robin scheduler.

Synopsis

MonadTask class

class Monad m => MonadTask e m | m -> e where Source #

MonadTask specifies a task monad m over an event type e.

Minimal complete definition

yield, fork, watch, signal, exit

Methods

yield :: m () Source #

yield temporarily suspends current task to let others run.

fork :: m a -> m () Source #

fork spawns a task and runs it immediately until it ends or suspends before returning to current task.

watch :: (e -> Maybe a) -> m a Source #

watch suspends the current task to wait for future events, and will resume execution when an event triggers its watching function.

signal :: e -> m () Source #

signal broadcasts an event to all other tasks that are watching, and give those who wake up the priority to run.

exit :: m () Source #

exit ends all tasks and returns immediately.

Instances

(Monad m, MonadTask a m) => MonadTask a (MaybeT m) Source # 

Methods

yield :: MaybeT m () Source #

fork :: MaybeT m a -> MaybeT m () Source #

watch :: (a -> Maybe a) -> MaybeT m a Source #

signal :: a -> MaybeT m () Source #

exit :: MaybeT m () Source #

(Monad m, MonadTask a m) => MonadTask a (ListT m) Source # 

Methods

yield :: ListT m () Source #

fork :: ListT m a -> ListT m () Source #

watch :: (a -> Maybe a) -> ListT m a Source #

signal :: a -> ListT m () Source #

exit :: ListT m () Source #

(Monoid w, Monad m, MonadTask a m) => MonadTask a (WriterT w m) Source # 

Methods

yield :: WriterT w m () Source #

fork :: WriterT w m a -> WriterT w m () Source #

watch :: (a -> Maybe a) -> WriterT w m a Source #

signal :: a -> WriterT w m () Source #

exit :: WriterT w m () Source #

(Monoid w, Monad m, MonadTask a m) => MonadTask a (WriterT w m) Source # 

Methods

yield :: WriterT w m () Source #

fork :: WriterT w m a -> WriterT w m () Source #

watch :: (a -> Maybe a) -> WriterT w m a Source #

signal :: a -> WriterT w m () Source #

exit :: WriterT w m () Source #

(Monad m, MonadTask a m) => MonadTask a (IdentityT * m) Source # 

Methods

yield :: IdentityT * m () Source #

fork :: IdentityT * m a -> IdentityT * m () Source #

watch :: (a -> Maybe a) -> IdentityT * m a Source #

signal :: a -> IdentityT * m () Source #

exit :: IdentityT * m () Source #

(Monad m, MonadTask a m) => MonadTask a (ExceptT e m) Source # 

Methods

yield :: ExceptT e m () Source #

fork :: ExceptT e m a -> ExceptT e m () Source #

watch :: (a -> Maybe a) -> ExceptT e m a Source #

signal :: a -> ExceptT e m () Source #

exit :: ExceptT e m () Source #

Monad m => MonadTask e (TaskT e m) Source # 

Methods

yield :: TaskT e m () Source #

fork :: TaskT e m a -> TaskT e m () Source #

watch :: (e -> Maybe a) -> TaskT e m a Source #

signal :: e -> TaskT e m () Source #

exit :: TaskT e m () Source #

(Monad m, MonadTask a m) => MonadTask a (ReaderT * r m) Source # 

Methods

yield :: ReaderT * r m () Source #

fork :: ReaderT * r m a -> ReaderT * r m () Source #

watch :: (a -> Maybe a) -> ReaderT * r m a Source #

signal :: a -> ReaderT * r m () Source #

exit :: ReaderT * r m () Source #

TaskT monad transformer

newtype TaskT e m a Source #

Task monad transformer.

Constructors

TaskT 

Fields

Instances

MonadState s m => MonadState s (TaskT e m) Source # 

Methods

get :: TaskT e m s #

put :: s -> TaskT e m () #

state :: (s -> (a, s)) -> TaskT e m a #

MonadReader s m => MonadReader s (TaskT e m) Source # 

Methods

ask :: TaskT e m s #

local :: (s -> s) -> TaskT e m a -> TaskT e m a #

reader :: (s -> a) -> TaskT e m a #

Monad m => MonadTask e (TaskT e m) Source # 

Methods

yield :: TaskT e m () Source #

fork :: TaskT e m a -> TaskT e m () Source #

watch :: (e -> Maybe a) -> TaskT e m a Source #

signal :: e -> TaskT e m () Source #

exit :: TaskT e m () Source #

MonadTrans (TaskT e) Source # 

Methods

lift :: Monad m => m a -> TaskT e m a #

Monad m => Monad (TaskT e m) Source # 

Methods

(>>=) :: TaskT e m a -> (a -> TaskT e m b) -> TaskT e m b #

(>>) :: TaskT e m a -> TaskT e m b -> TaskT e m b #

return :: a -> TaskT e m a #

fail :: String -> TaskT e m a #

Functor (TaskT e m) Source # 

Methods

fmap :: (a -> b) -> TaskT e m a -> TaskT e m b #

(<$) :: a -> TaskT e m b -> TaskT e m a #

Applicative (TaskT e m) Source # 

Methods

pure :: a -> TaskT e m a #

(<*>) :: TaskT e m (a -> b) -> TaskT e m a -> TaskT e m b #

liftA2 :: (a -> b -> c) -> TaskT e m a -> TaskT e m b -> TaskT e m c #

(*>) :: TaskT e m a -> TaskT e m b -> TaskT e m b #

(<*) :: TaskT e m a -> TaskT e m b -> TaskT e m a #

MonadIO m => MonadIO (TaskT e m) Source # 

Methods

liftIO :: IO a -> TaskT e m a #

Functions

runTask :: Monad m => TaskT e m a -> m () Source #

runTask runs a task monad until to its completion, i.e., no more active tasks to run, or until it exits.

orElse :: (e -> Maybe a) -> (e -> Maybe b) -> e -> Maybe (Either a b) Source #

orElse is a helper function for combining two trigger functions disjuctively, favoring the first one.