timeout-with-results-0.2: Runs a time-limited computation alowing it to return intermediate results.

Safe HaskellSafe-Inferred

System.Timeout.Returning.Writer

Description

Defines a writer monad for computations that can be interrupted by a timeout. Written partial results are combined using their monoid operation and if a timeout occurs, the result is returned.

Several utility monoids that force their values to weak head normal form or to normal form are provided.

Synopsis

Documentation

class (Monoid w, Monad m) => MonadWriter w m | m -> w where

Methods

writer :: (a, w) -> m a

writer (a,w) embeds a simple writer action.

tell :: w -> m ()

tell w is an action that produces the output w.

listen :: m a -> m (a, w)

listen m is an action that executes the action m and adds its output to the value of the computation.

pass :: m (a, w -> w) -> m a

pass m is an action that executes the action m, which returns a value and a function, and returns the value, applying the function to the output.

Instances

(Monad (TimeoutWriter w), Monoid w) => MonadWriter w (TimeoutWriter w) 
(Monoid w, Monad (MaybeT m), MonadWriter w m) => MonadWriter w (MaybeT m) 
(Monoid w, Monad (IdentityT m), MonadWriter w m) => MonadWriter w (IdentityT m) 
(Monad (WriterT w m), Monoid w, Monad m) => MonadWriter w (WriterT w m) 
(Monad (WriterT w m), Monoid w, Monad m) => MonadWriter w (WriterT w m) 
(Monoid w, Monad (StateT s m), MonadWriter w m) => MonadWriter w (StateT s m) 
(Monoid w, Monad (StateT s m), MonadWriter w m) => MonadWriter w (StateT s m) 
(Monoid w, Monad (ReaderT r m), MonadWriter w m) => MonadWriter w (ReaderT r m) 
(Monoid w, Monad (ErrorT e m), Error e, MonadWriter w m) => MonadWriter w (ErrorT e m) 
(Monad (RWST r w s m), Monoid w, Monad m) => MonadWriter w (RWST r w s m) 
(Monad (RWST r w s m), Monoid w, Monad m) => MonadWriter w (RWST r w s m) 

class Monad m => MonadTimeout w m | m -> w whereSource

Monad for computations that can save partial results of type w during their evaluation.

Methods

partialResult :: w -> m ()Source

Store a new partial result. The precise semantics of what happens with the written value is by intent unspecified and left to be decided by implementations.

yield :: m ()Source

Explicitly allow interrupting the computation at this point. Experimental.

class (Monoid w, MonadTimeout w m, MonadWriter w m) => MonadTimeoutWriter w m | m -> w whereSource

Extends MonadTimeout to MonadWriter. Written values are combined together using w's monoid. In addition, allows to run a sub-computation in a contained environment, without affecting the current partial result.

Methods

contained :: m r -> m (r, w)Source

Runs the given computation separately and return its result. Does not modify the current result!

data TimeoutWriter w a Source

An IO-based implementation of MonadTimeoutWriter. Calling partialResult (or equivalently tell) combines the value with any previously written values using w's monoidal operation.

runTimeoutSource

Arguments

:: Monoid w 
=> Int

TimeoutWriter in microseconds.

-> TimeoutWriter w r

The computation.

-> IO (Maybe r, w)

The final result (if available) and the saved partial result.

Execute the given computation with a timeout limit. Each time a value is written, the result of mappend with the previous one is evaluated to weak head normal form.

withTimeoutWriter :: (w' -> w) -> TimeoutWriter w' a -> TimeoutWriter w aSource

Modify written values using the given function.

newtype Last' a Source

A monoid equivalent to Last. In addition, it forces evaluation of values inside Maybe using rseq. This means that when it is used in runTimeout, the computations will be forced in the producing thread, not in the consuming one. If you want to force evaluation to NF, wrap it inside NFMonoid.

Constructors

Last' 

Fields

getLast' :: Maybe a
 

Instances

Functor Last' 
Eq a => Eq (Last' a) 
(Eq (Last' a), Ord a) => Ord (Last' a) 
Read a => Read (Last' a) 
Show a => Show (Last' a) 
Monoid (Last' a) 
NFData a => NFData (Last' a) 

newtype SeqMax a b Source

A monoid whose mappend picks the grater value according to the second field of the tuple. SeqMax Nothing is the least element of the ordering. If the second fields are the same, the left value is preferred. In addition, the first field of the selected tuple is forced to evaluate using rseq.

Constructors

SeqMax (Maybe (a, b)) 

Instances

Functor (SeqMax a) 
(Eq a, Eq b) => Eq (SeqMax a b) 
(Eq (SeqMax a b), Ord a, Ord b) => Ord (SeqMax a b) 
(Read a, Read b) => Read (SeqMax a b) 
(Show a, Show b) => Show (SeqMax a b) 
Ord b => Monoid (SeqMax a b) 
(NFData a, NFData b) => NFData (SeqMax a b) 

newtype NFMonoid a Source

A wrapper monoid that forces each result of mappend to normal form'

Constructors

NFMonoid 

Fields

getNFMonoid :: a
 

Instances

Functor NFMonoid 
Bounded a => Bounded (NFMonoid a) 
Eq a => Eq (NFMonoid a) 
(Eq (NFMonoid a), Ord a) => Ord (NFMonoid a) 
Read a => Read (NFMonoid a) 
Show a => Show (NFMonoid a) 
(NFData a, Monoid a) => Monoid (NFMonoid a) 

defaultListen :: MonadTimeoutWriter w m => m a -> m (a, w)Source

A default implementation of listen using contained. Useful only for authors of implementations of MonadTimeout.

defaultPass :: MonadTimeoutWriter w m => m (a, w -> w) -> m aSource

A default implementation of pass using contained. Useful only for authors of implementations of MonadTimeout.