statestack-0.2.0.5: Simple State-like monad transformer with saveable and restorable state

Copyright(c) 2011 Brent Yorgey
LicenseBSD-style (see LICENSE)
Maintainerbyorgey@cis.upenn.edu
Safe HaskellNone
LanguageHaskell2010

Control.Monad.StateStack

Contents

Description

A state monad which allows the state to be saved and restored on a stack.

Computation type:
Computations with implicit access to a read/write state, with additional operations for pushing the current state on a stack and later restoring the state from the top of the stack.
Binding strategy:
Same as for the usual state monad; the state and accompanying stack of saved states are threaded through computations.
Useful for:
Remembering state while emitting commands for some system which itself has saveable/restorable state, such as OpenGL or Cairo.

Simple example:

ghci> let p = get >>= liftIO . print
ghci> evalStateStackT (put 2 >> p >> save >> put 3 >> p >> restore >> p) 0
2
3
2

Synopsis

The MonadStateStack class

class MonadState s m => MonadStateStack s m where #

Class of monads which support a state along with a stack for saving and restoring states.

Minimal complete definition

save, restore

Methods

save :: m () #

restore :: m () #

Instances

MonadStateStack s m => MonadStateStack s (MaybeT m) # 

Methods

save :: MaybeT m () #

restore :: MaybeT m () #

MonadStateStack s m => MonadStateStack s (ListT m) # 

Methods

save :: ListT m () #

restore :: ListT m () #

(Monoid w, MonadStateStack s m) => MonadStateStack s (WriterT w m) # 

Methods

save :: WriterT w m () #

restore :: WriterT w m () #

(Monoid w, MonadStateStack s m) => MonadStateStack s (WriterT w m) # 

Methods

save :: WriterT w m () #

restore :: WriterT w m () #

MonadStateStack s m => MonadStateStack s (StateT s m) # 

Methods

save :: StateT s m () #

restore :: StateT s m () #

MonadStateStack s m => MonadStateStack s (StateT s m) # 

Methods

save :: StateT s m () #

restore :: StateT s m () #

MonadStateStack s m => MonadStateStack s (IdentityT * m) # 

Methods

save :: IdentityT * m () #

restore :: IdentityT * m () #

MonadStateStack s m => MonadStateStack s (ExceptT e m) # 

Methods

save :: ExceptT e m () #

restore :: ExceptT e m () #

Monad m => MonadStateStack s (StateStackT s m) # 

Methods

save :: StateStackT s m () #

restore :: StateStackT s m () #

MonadStateStack s m => MonadStateStack s (ReaderT * r m) # 

Methods

save :: ReaderT * r m () #

restore :: ReaderT * r m () #

MonadStateStack s m => MonadStateStack s (ContT * r m) # 

Methods

save :: ContT * r m () #

restore :: ContT * r m () #

The StateStackT transformer

newtype StateStackT s m a #

A monad transformer which adds a save/restorable state to an existing monad.

Constructors

StateStackT 

Fields

Instances

Monad m => MonadState s (StateStackT s m) # 

Methods

get :: StateStackT s m s #

put :: s -> StateStackT s m () #

state :: (s -> (a, s)) -> StateStackT s m a #

Monad m => MonadStateStack s (StateStackT s m) # 

Methods

save :: StateStackT s m () #

restore :: StateStackT s m () #

MonadTrans (StateStackT s) # 

Methods

lift :: Monad m => m a -> StateStackT s m a #

Monad m => Monad (StateStackT s m) # 

Methods

(>>=) :: StateStackT s m a -> (a -> StateStackT s m b) -> StateStackT s m b #

(>>) :: StateStackT s m a -> StateStackT s m b -> StateStackT s m b #

return :: a -> StateStackT s m a #

fail :: String -> StateStackT s m a #

Functor m => Functor (StateStackT s m) # 

Methods

fmap :: (a -> b) -> StateStackT s m a -> StateStackT s m b #

(<$) :: a -> StateStackT s m b -> StateStackT s m a #

Monad m => Applicative (StateStackT s m) # 

Methods

pure :: a -> StateStackT s m a #

(<*>) :: StateStackT s m (a -> b) -> StateStackT s m a -> StateStackT s m b #

(*>) :: StateStackT s m a -> StateStackT s m b -> StateStackT s m b #

(<*) :: StateStackT s m a -> StateStackT s m b -> StateStackT s m a #

MonadIO m => MonadIO (StateStackT s m) # 

Methods

liftIO :: IO a -> StateStackT s m a #

MonadCont m => MonadCont (StateStackT s m) # 

Methods

callCC :: ((a -> StateStackT s m b) -> StateStackT s m a) -> StateStackT s m a #

Running StateStackT and StateStack computations

runStateStackT :: Monad m => StateStackT s m a -> s -> m (a, s) #

Run a StateStackT computation from an initial state, resulting in a computation of the underlying monad which yields the return value and final state.

evalStateStackT :: Monad m => StateStackT s m a -> s -> m a #

Like runStateStackT, but discard the final state.

execStateStackT :: Monad m => StateStackT s m a -> s -> m s #

Like runStateStackT, but discard the return value and yield only the final state.

runStateStack :: StateStack s a -> s -> (a, s) #

Run a StateStack computation from an initial state, resulting in a pair of the final return value and final state.

evalStateStack :: StateStack s a -> s -> a #

Like runStateStack, but discard the final state.

execStateStack :: StateStack s a -> s -> s #

Like runStateStack, but discard the return value and yield only the final state.

liftState :: Monad m => StateT s m a -> StateStackT s m a #

StateT computations can always be lifted to StateStackT computations which do not manipulate the state stack.