HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

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
Contents
  1. The MonadStateStack class
  2. The StateStackT transformer
  3. Running StateStackT and StateStack computations
Synopsis
  • Packagestatestack-0.3.1.1
  • Exports10
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceStateStack.hs

The MonadStateStack class

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

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

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.