contstuff-1.0.1: Fast, easy to use CPS-based monad transformers

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), MonadTrans 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), MonadTrans t) => m Bool -> t m ()Source

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

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 (IdentityT 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