HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulestatestack-0.3.1.1Haskell2010

Control.Monad.StateStack

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
  • 2 types
  • 1 class
  • 7 values
  • Packagestatestack-0.3.1.1
  • Exports10
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceStateStack.hs

The MonadStateStack class

1 declaration
classclass MonadState s m => MonadStateStack s (m :: Type -> Type) where
#

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

Methods

Instances10MonadStateStack, …

The StateStackT transformer

2 declarations
newtypenewtype StateStackT s (m :: Type -> Type) a
#

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

Constructors

Instances8MonadState, MonadStateStack, MonadTrans, Monad, Functor, Applicative, …

Running StateStackT and StateStack computations

7 declarations
valuerunStateStackT :: 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.

valuerunStateStack :: 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.

valueliftState :: 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.