contstuff-0.7.0: Fast, easy to use CPS-based monads

Stabilityexperimental
MaintainerErtugrul Soeylemez <es@ertes.de>

Control.ContStuff.Classes

Contents

Description

This module implements the various effect classes supported by contstuff.

Synopsis

Effect classes

Abortion

class Abortable m whereSource

Monads supporting abortion.

Associated Types

type Result m Source

End result of the computation.

Methods

abort :: Result m -> m aSource

Ignore current continuation and abort.

Instances

Call with current continuation

class CallCC m whereSource

Monads supporting *call-with-current-continuation* (aka callCC).

Methods

callCC :: ((a -> m b) -> m a) -> m aSource

Call with current continuation.

Instances

CallCC (ContT r m) 
CallCC (MaybeT r m) 
CallCC (EitherT r e m) 
CallCC (StateT r s m) 

data Label m a Source

labelCC :: (Applicative m, CallCC m) => a -> m (a, Label m a)Source

Capture the current continuation for later use.

goto :: Label m a -> a -> m ()Source

Jump to a label.

Exceptions

class HasExceptions m whereSource

Monads with exception support.

Associated Types

type Exception m Source

The exception type.

Methods

raise :: Exception m -> m aSource

Raise an exception.

try :: m a -> m (Either (Exception m) a)Source

Run computation catching exceptions.

bracket :: (HasExceptions m, Monad m) => m res -> (res -> m b) -> (res -> m a) -> m aSource

Get a resource, run a computation, then release the resource, even if an exception is raised:

 bracket acquire release use

Please note that this function behaves slightly different from the usual E.bracket. If both the user and the releaser throw an exception, the user exception is significant.

bracket_ :: (HasExceptions m, Monad m) => m a -> m b -> m c -> m cSource

Initialize, then run, then clean up safely, even if an exception is raised:

 bracket_ init cleanup run

Please note that this function behaves slightly different from the usual E.bracket_. If both the user and the releaser throw an exception, the user exception is significant.

catch :: (HasExceptions m, Monad m) => m a -> (Exception m -> m a) -> m aSource

Catch exceptions using an exception handler.

finally :: (HasExceptions m, Monad m) => m a -> m b -> m aSource

Run a final computation regardless of whether an exception was raised.

forbid :: (Exception (t m) ~ (), HasExceptions (t m), Monad m, Monad (t m), Transformer t) => m Bool -> t m ()Source

Fail (in the sense of the given transformer), if the given underlying computation returns True.

handle :: (HasExceptions m, Monad m) => (Exception m -> m a) -> m a -> m aSource

Catch exceptions using an exception handler (flip catch).

raiseUnless :: (HasExceptions m, Monad m) => Exception m -> m Bool -> m ()Source

Throw given exception, if the given computation returns False.

raiseWhen :: (HasExceptions m, Monad m) => Exception m -> m Bool -> m ()Source

Throw given exception, if the given computation returns True.

require :: (Exception (t m) ~ (), HasExceptions (t m), Monad m, Monad (t m), Transformer t) => m Bool -> t m ()Source

Fail (in the sense of the given transformer), if the given underlying computation returns False.

Lifting

class Transformer t whereSource

The monad transformer class. Lifting computations one level down the monad stack, or stated differently promoting a computation of the underlying monad to the transformer.

Methods

lift :: Monad m => m a -> t m aSource

Promote a monadic computation to the transformer.

class LiftBase m whereSource

Monads, which support lifting base monad computations.

Associated Types

type Base m :: * -> *Source

Base monad of m.

Methods

base :: Base m a -> m aSource

Promote a base monad computation.

Instances

LiftBase [] 
LiftBase IO 
LiftBase Maybe 
LiftBase Id 
LiftBase ((->) r) 
LiftBase (ST s) 
(LiftBase m, Monad m) => LiftBase (IdT m) 
(LiftBase m, Monad m) => LiftBase (ContT r m) 
(LiftBase m, Monad m) => LiftBase (MaybeT r m) 
(LiftBase m, Monad m) => LiftBase (ChoiceT r i m) 
(LiftBase m, Monad m) => LiftBase (EitherT r e m) 
(LiftBase m, Monad m) => LiftBase (StateT r s m) 

io :: (LiftBase m, Base m ~ IO) => Base m a -> m aSource

Handy alias for lifting IO computations.

Running

class Runnable t r m a whereSource

Every monad transformer t that supports transforming t m a to m a can be an instance of this class.

Associated Types

type Argument t r m a Source

Arguments needed to run.

Methods

runT :: Argument t r m a -> t m a -> m rSource

Run the transformer.

Instances

Runnable IdT r m r 
Runnable (ContT r) r m a 
Runnable (MaybeT r) r m a 
Runnable (EitherT r e) r m a 
Runnable (StateT r s) r m a 

State

class Stateful m whereSource

Stateful monads.

Minimal complete definition: StateOf, get and putLazy.

Associated Types

type StateOf m Source

State type of m.

Methods

get :: m (StateOf m)Source

Get the current state.

put :: StateOf m -> m ()Source

Set the current state and force it.

putLazy :: StateOf m -> m ()Source

Set the current state, but don't force it.

Instances

(Monad m, Stateful m) => Stateful (IdT m) 
(Monad m, Stateful m) => Stateful (ContT r m) 
(Monad m, Stateful m) => Stateful (MaybeT r m) 
(Monad m, Stateful m) => Stateful (ChoiceT r i m) 
(Monad m, Stateful m) => Stateful (EitherT r e m) 
Stateful (StateT r s m) 

getField :: (Functor m, Stateful m) => (StateOf m -> a) -> m aSource

Get a certain field.

modify :: (Monad m, Stateful m) => (StateOf m -> StateOf m) -> m ()Source

Apply a function to the current state.

modifyField :: (Monad m, Stateful m) => (StateOf m -> a) -> (a -> StateOf m) -> m ()Source

Get a field and modify the state.

modifyFieldLazy :: (Monad m, Stateful m) => (StateOf m -> a) -> (a -> StateOf m) -> m ()Source

Get a field and modify the state. Lazy version.

modifyLazy :: (Monad m, Stateful m) => (StateOf m -> StateOf m) -> m ()Source

Apply a function to the current state. Lazy version.

Logging support (writers)

class Writable m w whereSource

Monads with support for logging. Traditionally these are called *writer monads*.

Methods

tell :: w -> m ()Source

Log a value.

Instances

(Functor m, Monoid w) => Writable (ContT (r, w) m) w 
Alternative m => Writable (ContT r m) r 
(Functor m, Monoid w) => Writable (MaybeT (r, w) m) w 
Alternative m => Writable (MaybeT r m) r 
(Functor m, Monoid w) => Writable (EitherT (r, w) e m) w 
Alternative m => Writable (EitherT r e m) r 
(Functor m, Monoid w) => Writable (StateT (r, w) s m) w 
Alternative m => Writable (StateT r s m) r