Copyright (c) Edward Kmett & Sjoerd Visscher 2011 BSD3 ekmett@gmail.com experimental Safe Haskell98

Description

A generalized State monad, parameterized by a Representable functor. The representation of that functor serves as the state.

Synopsis

# Documentation

type State g = StateT g Identity Source #

A memoized state monad parameterized by a representable functor g, where the representatation of g, Rep g is the state to carry.

The return function leaves the state unchanged, while >>= uses the final state of the first computation as the initial state of the second.

Arguments

 :: Representable g => State g a state-passing computation to execute -> Rep g initial state -> (a, Rep g) return value and final state

Unwrap a state monad computation as a function. (The inverse of state.)

Arguments

 :: Representable g => State g a state-passing computation to execute -> Rep g initial value -> a return value of the state computation

Evaluate a state computation with the given initial state and return the final value, discarding the final state.

• evalState m s = fst (runState m s)

Arguments

 :: Representable g => State g a state-passing computation to execute -> Rep g initial value -> Rep g final state

Evaluate a state computation with the given initial state and return the final state, discarding the final value.

• execState m s = snd (runState m s)

mapState :: Functor g => ((a, Rep g) -> (b, Rep g)) -> State g a -> State g b Source #

Map both the return value and final state of a computation using the given function.

• runState (mapState f m) = f . runState m

newtype StateT g m a Source #

A state transformer monad parameterized by:

• g - A representable functor used to memoize results for a state Rep g
• m - The inner monad.

The return function leaves the state unchanged, while >>= uses the final state of the first computation as the initial state of the second.

Constructors

 StateT FieldsgetStateT :: g (m (a, Rep g))
Instances

stateT :: Representable g => (Rep g -> m (a, Rep g)) -> StateT g m a Source #

runStateT :: Representable g => StateT g m a -> Rep g -> m (a, Rep g) Source #

evalStateT :: (Representable g, Monad m) => StateT g m a -> Rep g -> m a Source #

Evaluate a state computation with the given initial state and return the final value, discarding the final state.

• evalStateT m s = liftM fst (runStateT m s)

execStateT :: (Representable g, Monad m) => StateT g m a -> Rep g -> m (Rep g) Source #

Evaluate a state computation with the given initial state and return the final state, discarding the final value.

• execStateT m s = liftM snd (runStateT m s)

mapStateT :: Functor g => (m (a, Rep g) -> n (b, Rep g)) -> StateT g m a -> StateT g n b Source #

liftCallCC :: Representable g => ((((a, Rep g) -> m (b, Rep g)) -> m (a, Rep g)) -> m (a, Rep g)) -> ((a -> StateT g m b) -> StateT g m a) -> StateT g m a Source #

Uniform lifting of a callCC operation to the new monad. This version rolls back to the original state on entering the continuation.

liftCallCC' :: Representable g => ((((a, Rep g) -> m (b, Rep g)) -> m (a, Rep g)) -> m (a, Rep g)) -> ((a -> StateT g m b) -> StateT g m a) -> StateT g m a Source #

In-situ lifting of a callCC operation to the new monad. This version uses the current state on entering the continuation. It does not satisfy the laws of a monad transformer.

class Monad m => MonadState s (m :: Type -> Type) | m -> s where Source #

Minimal definition is either both of get and put or just state

Minimal complete definition

Methods

get :: m s Source #

Return the state from the internals of the monad.

put :: s -> m () Source #

Replace the state inside the monad.

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

Embed a simple state action into the monad.

Instances