rio-0.1.22.0: A standard library for Haskell

Safe Haskell | Safe-Inferred
Language | Haskell2010

RIO.State

Description

Provides reexports of `MonadState` and related helpers.

Since: 0.1.4.0

Synopsis

- class Monad m => MonadState s (m :: Type -> Type) | m -> s where
  - get :: m s
  - put :: s -> m ()
  - state :: (s -> (a, s)) -> m a
- gets :: MonadState s m => (s -> a) -> m a
- modify :: MonadState s m => (s -> s) -> m ()
- modify' :: MonadState s m => (s -> s) -> m ()
- 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 :: Type -> Type) 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 :: forall s (m :: Type -> Type) a. (s -> s) -> StateT s m a -> StateT s m a

# Documentation

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

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

Minimal complete definition

state | get, put

Methods

get :: m s #

Return the state from the internals of the monad.

put :: s -> m () #

Replace the state inside the monad.

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

Embed a simple state action into the monad.

#### Instances

Instances details

MonadState s m => MonadState s (CatchT m) |
Instance details Defined in Control.Monad.Catch.Pure Methods get :: CatchT m s # put :: s -> CatchT m () # state :: (s -> (a, s)) -> CatchT m a #
HasStateRef s env => MonadState s (RIO env) # |
Instance details Defined in RIO.Prelude.RIO Methods get :: RIO env s # put :: s -> RIO env () # state :: (s -> (a, s)) -> RIO env a #
MonadState s m => MonadState s (MaybeT m) |
Instance details Defined in Control.Monad.State.Class Methods get :: MaybeT m s # put :: s -> MaybeT m () # state :: (s -> (a, s)) -> MaybeT m a #
(Monoid w, MonadState s m) => MonadState s (AccumT w m) | Since: mtl-2.3
Instance details Defined in Control.Monad.State.Class Methods get :: AccumT w m s # put :: s -> AccumT w m () # state :: (s -> (a, s)) -> AccumT w m a #
MonadState s m => MonadState s (ExceptT e m) | Since: mtl-2.2
Instance details Defined in Control.Monad.State.Class Methods get :: ExceptT e m s # put :: s -> ExceptT e m () # state :: (s -> (a, s)) -> ExceptT e m a #
MonadState s m => MonadState s (IdentityT m) |
Instance details Defined in Control.Monad.State.Class Methods get :: IdentityT m s # put :: s -> IdentityT m () # state :: (s -> (a, s)) -> IdentityT m a #
MonadState s m => MonadState s (ReaderT r m) |
Instance details Defined in Control.Monad.State.Class Methods get :: ReaderT r m s # put :: s -> ReaderT r m () # state :: (s -> (a, s)) -> ReaderT r m a #
MonadState s m => MonadState s (SelectT r m) | Since: mtl-2.3
Instance details Defined in Control.Monad.State.Class Methods get :: SelectT r m s # put :: s -> SelectT r m () # state :: (s -> (a, s)) -> SelectT r m a #
Monad m => MonadState s (StateT s m) |
Instance details Defined in Control.Monad.State.Class Methods get :: StateT s m s # put :: s -> StateT s m () # state :: (s -> (a, s)) -> StateT s m a #
Monad m => MonadState s (StateT s m) |
Instance details Defined in Control.Monad.State.Class Methods get :: StateT s m s # put :: s -> StateT s m () # state :: (s -> (a, s)) -> StateT s m a #
(Monoid w, MonadState s m) => MonadState s (WriterT w m) | Since: mtl-2.3
Instance details Defined in Control.Monad.State.Class Methods get :: WriterT w m s # put :: s -> WriterT w m () # state :: (s -> (a, s)) -> WriterT w m a #
(Monoid w, MonadState s m) => MonadState s (WriterT w m) |
Instance details Defined in Control.Monad.State.Class Methods get :: WriterT w m s # put :: s -> WriterT w m () # state :: (s -> (a, s)) -> WriterT w m a #
(Monoid w, MonadState s m) => MonadState s (WriterT w m) |
Instance details Defined in Control.Monad.State.Class Methods get :: WriterT w m s # put :: s -> WriterT w m () # state :: (s -> (a, s)) -> WriterT w m a #
MonadState s m => MonadState s (ContT r m) |
Instance details Defined in Control.Monad.State.Class Methods get :: ContT r m s # put :: s -> ContT r m () # state :: (s -> (a, s)) -> ContT r m a #
(Monad m, Monoid w) => MonadState s (RWST r w s m) | Since: mtl-2.3
Instance details Defined in Control.Monad.State.Class Methods get :: RWST r w s m s # put :: s -> RWST r w s m () # state :: (s -> (a, s)) -> RWST r w s m a #
(Monad m, Monoid w) => MonadState s (RWST r w s m) |
Instance details Defined in Control.Monad.State.Class Methods get :: RWST r w s m s # put :: s -> RWST r w s m () # state :: (s -> (a, s)) -> RWST r w s m a #
(Monad m, Monoid w) => MonadState s (RWST r w s m) |
Instance details Defined in Control.Monad.State.Class Methods get :: RWST r w s m s # put :: s -> RWST r w s m () # state :: (s -> (a, s)) -> RWST r w s m a #

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

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.

runState #

Arguments

:: 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`.)

evalState #

Arguments

:: State s a | state-passing computation to execute
-> s | 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)

execState #

Arguments

:: State s a | state-passing computation to execute
-> s | initial value
-> s | 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 :: ((a, s) -> (b, s)) -> State s a -> State s b #

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

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

withState :: (s -> s) -> State s a -> State s a #

``withState` f m` executes action `m` on a state modified by applying `f`.

-     withState f m = modify f >> m

newtype StateT s (m :: Type -> Type) 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.

Constructors

StateT |
Fields runStateT :: s -> m (a, s)

#### Instances

Instances details

MonadAccum w m => MonadAccum w (StateT s m) | Since: mtl-2.3
Instance details Defined in Control.Monad.Accum Methods look :: StateT s m w # add :: w -> StateT s m () # accum :: (w -> (a, w)) -> StateT s m a #
MonadError e m => MonadError e (StateT s m) |
Instance details Defined in Control.Monad.Error.Class Methods throwError :: e -> StateT s m a # catchError :: StateT s m a -> (e -> StateT s m a) -> StateT s m a #
MonadReader r m => MonadReader r (StateT s m) |
Instance details Defined in Control.Monad.Reader.Class Methods ask :: StateT s m r # local :: (r -> r) -> StateT s m a -> StateT s m a # reader :: (r -> a) -> StateT s m a #
MonadSelect w m => MonadSelect w (StateT s m) | 'Readerizes' the state: the 'ranking' function can see a value of type `s`, but not modify it. Effectively, can be thought of as 'extending' the 'ranking' by all values in `s`, but which `s` gets given to any rank calls is predetermined by the 'outer state' (and cannot change). Since: mtl-2.3
Instance details Defined in Control.Monad.Select Methods select :: ((a -> w) -> a) -> StateT s m a #
Monad m => MonadState s (StateT s m) |
Instance details Defined in Control.Monad.State.Class Methods get :: StateT s m s # put :: s -> StateT s m () # state :: (s -> (a, s)) -> StateT s m a #
MonadWriter w m => MonadWriter w (StateT s m) |
Instance details Defined in Control.Monad.Writer.Class Methods writer :: (a, w) -> StateT s m a # tell :: w -> StateT s m () # listen :: StateT s m a -> StateT s m (a, w) # pass :: StateT s m (a, w -> w) -> StateT s m a #
MonadTrans (StateT s) |
Instance details Defined in Control.Monad.Trans.State.Lazy Methods lift :: Monad m => m a -> StateT s m a #
MonadFail m => MonadFail (StateT s m) |
Instance details Defined in Control.Monad.Trans.State.Lazy Methods fail :: String -> StateT s m a #
MonadFix m => MonadFix (StateT s m) |
Instance details Defined in Control.Monad.Trans.State.Lazy Methods mfix :: (a -> StateT s m a) -> StateT s m a #
MonadIO m => MonadIO (StateT s m) |
Instance details Defined in Control.Monad.Trans.State.Lazy Methods liftIO :: IO a -> StateT s m a #
Contravariant m => Contravariant (StateT s m) |
Instance details Defined in Control.Monad.Trans.State.Lazy Methods contramap :: (a' -> a) -> StateT s m a -> StateT s m a' # (>$) :: b -> StateT s m b -> StateT s m a #
(Functor m, MonadPlus m) => Alternative (StateT s m) |
Instance details Defined in Control.Monad.Trans.State.Lazy Methods empty :: StateT s m a # (<|>) :: StateT s m a -> StateT s m a -> StateT s m a # some :: StateT s m a -> StateT s m [a] # many :: StateT s m a -> StateT s m [a] #
(Functor m, Monad m) => Applicative (StateT s m) |
Instance details Defined in Control.Monad.Trans.State.Lazy Methods pure :: a -> StateT s m a # (<*>) :: StateT s m (a -> b) -> StateT s m a -> StateT s m b # liftA2 :: (a -> b -> c) -> StateT s m a -> StateT s m b -> StateT s m c # (*>) :: StateT s m a -> StateT s m b -> StateT s m b # (<*) :: StateT s m a -> StateT s m b -> StateT s m a #
Functor m => Functor (StateT s m) |
Instance details Defined in Control.Monad.Trans.State.Lazy Methods fmap :: (a -> b) -> StateT s m a -> StateT s m b # (<$) :: a -> StateT s m b -> StateT s m a #
Monad m => Monad (StateT s m) |
Instance details Defined in Control.Monad.Trans.State.Lazy Methods (>>=) :: StateT s m a -> (a -> StateT s m b) -> StateT s m b # (>>) :: StateT s m a -> StateT s m b -> StateT s m b # return :: a -> StateT s m a #
MonadPlus m => MonadPlus (StateT s m) |
Instance details Defined in Control.Monad.Trans.State.Lazy Methods mzero :: StateT s m a # mplus :: StateT s m a -> StateT s m a -> StateT s m a #
MonadCatch m => MonadCatch (StateT s m) |
Instance details Defined in Control.Monad.Catch Methods catch :: (HasCallStack, Exception e) => StateT s m a -> (e -> StateT s m a) -> StateT s m a #
MonadMask m => MonadMask (StateT s m) |
Instance details Defined in Control.Monad.Catch Methods mask :: HasCallStack => ((forall a. StateT s m a -> StateT s m a) -> StateT s m b) -> StateT s m b # uninterruptibleMask :: HasCallStack => ((forall a. StateT s m a -> StateT s m a) -> StateT s m b) -> StateT s m b # generalBracket :: HasCallStack => StateT s m a -> (a -> ExitCase b -> StateT s m c) -> (a -> StateT s m b) -> StateT s m (b, c) #
MonadThrow m => MonadThrow (StateT s m) |
Instance details Defined in Control.Monad.Catch Methods throwM :: (HasCallStack, Exception e) => e -> StateT s m a #
MonadCont m => MonadCont (StateT s m) |
Instance details Defined in Control.Monad.Cont.Class Methods callCC :: ((a -> StateT s m b) -> StateT s m a) -> StateT s m a #
PrimMonad m => PrimMonad (StateT s m) |
Instance details Defined in Control.Monad.Primitive Associated Types
type PrimState (StateT s m) |
Instance details Defined in Control.Monad.Primitive type PrimState (StateT s m) = PrimState m
Methods primitive :: (State# (PrimState (StateT s m)) -> (# State# (PrimState (StateT s m)), a #)) -> StateT s m a #
Monad z => Zoom (StateT s z) (StateT t z) s t |
Instance details Defined in Lens.Micro.Mtl.Internal Methods zoom :: LensLike' (Zoomed (StateT s z) c) t s -> StateT s z c -> StateT t z c #
Generic (StateT s m a) |
Instance details Defined in Control.Monad.Trans.State.Lazy Associated Types
type Rep (StateT s m a) |
Instance details Defined in Control.Monad.Trans.State.Lazy type Rep (StateT s m a) = D1 ('MetaData "StateT" "Control.Monad.Trans.State.Lazy" "transformers-0.6.1.0-inplace" 'True) (C1 ('MetaCons "StateT" 'PrefixI 'True) (S1 ('MetaSel ('Just "runStateT") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (s -> m (a, s)))))
Methods from :: StateT s m a -> Rep (StateT s m a) x # to :: Rep (StateT s m a) x -> StateT s m a #
type Zoomed (StateT s z) |
Instance details Defined in Lens.Micro.Mtl.Internal type Zoomed (StateT s z) = Focusing z
type PrimState (StateT s m) |
Instance details Defined in Control.Monad.Primitive type PrimState (StateT s m) = PrimState m
type Rep (StateT s m a) |
Instance details Defined in Control.Monad.Trans.State.Lazy type Rep (StateT s m a) = D1 ('MetaData "StateT" "Control.Monad.Trans.State.Lazy" "transformers-0.6.1.0-inplace" 'True) (C1 ('MetaCons "StateT" 'PrefixI 'True) (S1 ('MetaSel ('Just "runStateT") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (s -> m (a, s)))))

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)

mapStateT :: (m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b #

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

-     runStateT (mapStateT f m) = f . runStateT m

withStateT :: forall s (m :: Type -> Type) a. (s -> s) -> StateT s m a -> StateT s m a #

``withStateT` f m` executes action `m` on a state modified by applying `f`.

-     withStateT f m = modify f >> m