Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Synopsis
- type State s = StateT s Identity
- runState :: State s a -> s -> (a, s)
- evalState :: State s a -> s -> a
- execState :: State s a -> s -> s
- mapState :: ((a, s) -> (b, s)) -> State s a -> State s b
- withState :: (s -> s) -> State s a -> State s a
- newtype StateT s (m :: * -> *) a = StateT {
- runStateT :: s -> m (a, s)
- evalStateT :: Monad m => StateT s m a -> s -> m a
- execStateT :: Monad m => StateT s m a -> s -> m s
- mapStateT :: (m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b
- withStateT :: (s -> s) -> StateT s m a -> StateT s m a
- class Monad m => MonadState s (m :: * -> *) | m -> s where
- gets :: MonadState s m => (s -> a) -> m a
- modify :: MonadState s m => (s -> s) -> m ()
- modify' :: MonadState s m => (s -> s) -> m ()
- class (MonadState s m, MonadState t n) => Zoom (m :: * -> *) (n :: * -> *) s t | m -> s, n -> t, m t -> n, n s -> m
- zoom :: Zoom m n s t => LensLike' (Zoomed m c) t s -> m c -> n c
- type family Zoomed (m :: * -> *) :: * -> * -> *
State
type State s = StateT s Identity #
A state monad parameterized by the type s
of 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.
:: State s a | state-passing computation to execute |
-> s | initial state |
-> (a, s) | return value and final state |
Unwrap a state monad computation as a function.
(The inverse of state
.)
:: State s a | state-passing computation to execute |
-> s | initial value |
-> a | return value of the state computation |
:: State s a | state-passing computation to execute |
-> s | initial value |
-> s | final state |
StateT
newtype StateT s (m :: * -> *) a #
A state transformer monad parameterized by:
s
- The state.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
evalStateT :: Monad m => StateT s m a -> s -> m a #
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 :: Monad m => StateT s m a -> s -> m s #
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)
withStateT :: (s -> s) -> StateT s m a -> StateT s m a #
executes action withStateT
f mm
on a state modified by
applying f
.
withStateT
f m =modify
f >> m
MonadState
class Monad m => MonadState s (m :: * -> *) | 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.
Instances
MonadState s m => MonadState s (TestT m) | |
MonadState s m => MonadState s (PropertyT m) | |
MonadState s m => MonadState s (GenT m) | |
MonadState s m => MonadState s (MaybeT m) | |
MonadState s m => MonadState s (ListT m) | |
MonadState s m => MonadState s (LogicT m) | |
MonadState s m => MonadState s (ListT m) | |
(Functor m, MonadState s m) => MonadState s (Free m) | |
(Monoid w, MonadState s m) => MonadState s (WriterT w m) | |
(Monoid w, MonadState s m) => MonadState s (WriterT w m) | |
Monad m => MonadState s (StateT s m) | |
Monad m => MonadState s (StateT s m) | |
MonadState s m => MonadState s (IdentityT m) | |
MonadState s m => MonadState s (ExceptT e m) | Since: mtl-2.2 |
(Error e, MonadState s m) => MonadState s (ErrorT e m) | |
(Functor f, MonadState s m) => MonadState s (FreeT f m) | |
(Stream s, MonadState st m) => MonadState st (ParsecT e s m) | |
MonadState s m => MonadState s (ReaderT r m) | |
MonadState s m => MonadState s (ContT r m) | |
(Monad m, Monoid w) => MonadState s (RWST r w s m) | |
(Monad m, Monoid w) => MonadState s (RWST r w s m) | |
gets :: MonadState s m => (s -> a) -> m a #
Gets specific component of the state, using a projection function supplied.
modify :: MonadState s m => (s -> s) -> m () #
Monadic state transformer.
Maps an old state to a new state inside a state monad. The old state is thrown away.
Main> :t modify ((+1) :: Int -> Int) modify (...) :: (MonadState Int a) => a ()
This says that modify (+1)
acts over any
Monad that is a member of the MonadState
class,
with an Int
state.
modify' :: MonadState s m => (s -> s) -> m () #
A variant of modify
in which the computation is strict in the
new state.
Since: mtl-2.2
Zoom
class (MonadState s m, MonadState t n) => Zoom (m :: * -> *) (n :: * -> *) s t | m -> s, n -> t, m t -> n, n s -> m #
This class allows us to use zoom
in, changing the State
supplied by
many different Monad
transformers, potentially quite
deep in a Monad
transformer stack.
Instances
Zoom m n s t => Zoom (MaybeT m) (MaybeT n) s t | |
Zoom m n s t => Zoom (ListT m) (ListT n) s t | |
Zoom m n s t => Zoom (IdentityT m) (IdentityT n) s t | |
Zoom m n s t => Zoom (ExceptT e m) (ExceptT e n) s t | |
(Functor f, Zoom m n s t) => Zoom (FreeT f m) (FreeT f n) s t | |
(Error e, Zoom m n s t) => Zoom (ErrorT e m) (ErrorT e n) s t | |
(Monoid w, Zoom m n s t) => Zoom (WriterT w m) (WriterT w n) s t | |
Monad z => Zoom (StateT s z) (StateT t z) s t | |
Monad z => Zoom (StateT s z) (StateT t z) s t | |
(Monoid w, Zoom m n s t) => Zoom (WriterT w m) (WriterT w n) s t | |
Zoom m n s t => Zoom (ReaderT e m) (ReaderT e n) s t | |
(Monoid w, Monad z) => Zoom (RWST r w s z) (RWST r w t z) s t | |
(Monoid w, Monad z) => Zoom (RWST r w s z) (RWST r w t z) s t | |
zoom :: Zoom m n s t => LensLike' (Zoomed m c) t s -> m c -> n c infixr 2 #
Run a monadic action in a larger State
than it was defined in,
using a Lens'
or Traversal'
.
This is commonly used to lift actions in a simpler State
Monad
into a State
Monad
with a larger State
type.
When applied to a Traversal'
over
multiple values, the actions for each target are executed sequentially
and the results are aggregated.
This can be used to edit pretty much any Monad
transformer stack with a State
in it!
>>>
flip State.evalState (a,b) $ zoom _1 $ use id
a
>>>
flip State.execState (a,b) $ zoom _1 $ id .= c
(c,b)
>>>
flip State.execState [(a,b),(c,d)] $ zoom traverse $ _2 %= f
[(a,f b),(c,f d)]
>>>
flip State.runState [(a,b),(c,d)] $ zoom traverse $ _2 <%= f
(f b <> f d <> mempty,[(a,f b),(c,f d)])
>>>
flip State.evalState (a,b) $ zoom both (use id)
a <> b
zoom
::Monad
m =>Lens'
s t ->StateT
t m a ->StateT
s m azoom
:: (Monad
m,Monoid
c) =>Traversal'
s t ->StateT
t m c ->StateT
s m czoom
:: (Monad
m,Monoid
w) =>Lens'
s t ->RWST
r w t m c ->RWST
r w s m czoom
:: (Monad
m,Monoid
w,Monoid
c) =>Traversal'
s t ->RWST
r w t m c ->RWST
r w s m czoom
:: (Monad
m,Monoid
w,Error
e) =>Lens'
s t ->ErrorT
e (RWST
r w t m) c ->ErrorT
e (RWST
r w s m) czoom
:: (Monad
m,Monoid
w,Monoid
c,Error
e) =>Traversal'
s t ->ErrorT
e (RWST
r w t m) c ->ErrorT
e (RWST
r w s m) c ...
type family Zoomed (m :: * -> *) :: * -> * -> * #
This type family is used by Zoom
to describe the common effect type.
Instances
type Zoomed (MaybeT m) | |
Defined in Control.Lens.Zoom | |
type Zoomed (ListT m) | |
Defined in Control.Lens.Zoom | |
type Zoomed (IdentityT m) | |
Defined in Control.Lens.Zoom | |
type Zoomed (ExceptT e m) | |
Defined in Control.Lens.Zoom | |
type Zoomed (FreeT f m) | |
Defined in Control.Lens.Zoom | |
type Zoomed (ErrorT e m) | |
Defined in Control.Lens.Zoom | |
type Zoomed (WriterT w m) | |
Defined in Control.Lens.Zoom | |
type Zoomed (StateT s z) | |
Defined in Control.Lens.Zoom | |
type Zoomed (StateT s z) | |
Defined in Control.Lens.Zoom | |
type Zoomed (WriterT w m) | |
Defined in Control.Lens.Zoom | |
type Zoomed (ReaderT e m) | |
Defined in Control.Lens.Zoom | |
type Zoomed (RWST r w s z) | |
Defined in Control.Lens.Zoom | |
type Zoomed (RWST r w s z) | |
Defined in Control.Lens.Zoom |