Copyright | (c) Edward Kmett & Sjoerd Visscher 2011 |
---|---|
License | BSD3 |
Maintainer | ekmett@gmail.com |
Stability | experimental |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
A generalized State monad, parameterized by a Representable functor. The representation of that functor serves as the state.
Synopsis
- type State (g :: Type -> Type) = StateT g Identity
- runState :: forall (g :: Type -> Type) a. Representable g => State g a -> Rep g -> (a, Rep g)
- evalState :: forall (g :: Type -> Type) a. Representable g => State g a -> Rep g -> a
- execState :: forall (g :: Type -> Type) a. Representable g => State g a -> Rep g -> Rep g
- mapState :: forall (g :: Type -> Type) a b. Functor g => ((a, Rep g) -> (b, Rep g)) -> State g a -> State g b
- newtype StateT (g :: Type -> Type) (m :: Type -> Type) a = StateT {}
- stateT :: forall (g :: Type -> Type) m a. Representable g => (Rep g -> m (a, Rep g)) -> StateT g m a
- runStateT :: forall (g :: Type -> Type) m a. Representable g => StateT g m a -> Rep g -> m (a, Rep g)
- evalStateT :: forall (g :: Type -> Type) m a. (Representable g, Monad m) => StateT g m a -> Rep g -> m a
- execStateT :: forall (g :: Type -> Type) m a. (Representable g, Monad m) => StateT g m a -> Rep g -> m (Rep g)
- mapStateT :: forall (g :: Type -> Type) m a n b. Functor g => (m (a, Rep g) -> n (b, Rep g)) -> StateT g m a -> StateT g n b
- liftCallCC :: forall (g :: Type -> Type) a m b. 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
- liftCallCC' :: forall (g :: Type -> Type) a m b. 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
- class Monad m => MonadState s (m :: Type -> Type) | m -> s where
Documentation
type State (g :: Type -> Type) = 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.
:: forall (g :: Type -> Type) a. 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
.)
:: forall (g :: Type -> Type) a. Representable g | |
=> State g a | state-passing computation to execute |
-> Rep g | initial value |
-> a | return value of the state computation |
mapState :: forall (g :: Type -> Type) a b. Functor g => ((a, Rep g) -> (b, Rep g)) -> State g a -> State g b Source #
newtype StateT (g :: Type -> Type) (m :: Type -> Type) a Source #
A state transformer monad parameterized by:
g
- A representable functor used to memoize results for a stateRep 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.
Instances
(Functor f, Representable g, MonadFree f m) => MonadFree f (StateT g m) Source # | |
Defined in Control.Monad.Representable.State | |
(Representable g, MonadReader e m) => MonadReader e (StateT g m) Source # | |
(Representable g, Monad m, Rep g ~ s) => MonadState s (StateT g m) Source # | |
(Representable g, MonadWriter w m) => MonadWriter w (StateT g m) Source # | |
Representable f => BindTrans (StateT f) Source # | |
Defined in Control.Monad.Representable.State | |
Representable f => MonadTrans (StateT f) Source # | |
Defined in Control.Monad.Representable.State | |
(Representable g, Functor m, Monad m) => Applicative (StateT g m) Source # | |
Defined in Control.Monad.Representable.State | |
(Functor g, Functor m) => Functor (StateT g m) Source # | |
(Representable g, Monad m) => Monad (StateT g m) Source # | |
(Representable g, MonadCont m) => MonadCont (StateT g m) Source # | |
(Representable g, Bind m) => Apply (StateT g m) Source # | |
(Representable g, Bind m) => Bind (StateT g m) Source # | |
stateT :: forall (g :: Type -> Type) m a. Representable g => (Rep g -> m (a, Rep g)) -> StateT g m a Source #
runStateT :: forall (g :: Type -> Type) m a. Representable g => StateT g m a -> Rep g -> m (a, Rep g) Source #
evalStateT :: forall (g :: Type -> Type) m a. (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 :: forall (g :: Type -> Type) m a. (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 :: forall (g :: Type -> Type) m a n b. Functor g => (m (a, Rep g) -> n (b, Rep g)) -> StateT g m a -> StateT g n b Source #
liftCallCC :: forall (g :: Type -> Type) a m b. 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' :: forall (g :: Type -> Type) a m b. 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 #
Minimal definition is either both of get
and put
or just state
Return the state from the internals of the monad.
Replace the state inside the monad.
state :: (s -> (a, s)) -> m a #
Embed a simple state action into the monad.