rio-0.1.24.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 Source #

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

Minimal complete definition

state | get, put

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

Instances details

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

gets :: MonadState s m => (s -> a) -> m a Source #

Gets specific component of the state, using a projection function supplied.

modify :: MonadState s m => (s -> s) -> m () Source #

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 () Source #

A variant of `modify` in which the computation is strict in the new state.

Since: mtl-2.2

type State s = StateT s Identity Source #

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 Source #

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 Source #

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 Source #

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 Source #

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 Source #

``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 Source #

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 Source # add :: w -> StateT s m () Source # accum :: (w -> (a, w)) -> StateT s m a Source #
MonadError e m => MonadError e (StateT s m) |
Instance details Defined in Control.Monad.Error.Class Methods throwError :: e -> StateT s m a Source # catchError :: StateT s m a -> (e -> StateT s m a) -> StateT s m a Source #
MonadReader r m => MonadReader r (StateT s m) |
Instance details Defined in Control.Monad.Reader.Class Methods ask :: StateT s m r Source # local :: (r -> r) -> StateT s m a -> StateT s m a Source # reader :: (r -> a) -> StateT s m a Source #
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 Source #
Monad m => MonadState s (StateT s m) |
Instance details Defined in Control.Monad.State.Class Methods get :: StateT s m s Source # put :: s -> StateT s m () Source # state :: (s -> (a, s)) -> StateT s m a Source #
MonadWriter w m => MonadWriter w (StateT s m) |
Instance details Defined in Control.Monad.Writer.Class Methods writer :: (a, w) -> StateT s m a Source # tell :: w -> StateT s m () Source # listen :: StateT s m a -> StateT s m (a, w) Source # pass :: StateT s m (a, w -> w) -> StateT s m a Source #
MonadTrans (StateT s) |
Instance details Defined in Control.Monad.Trans.State.Lazy Methods lift :: Monad m => m a -> StateT s m a Source #
MonadIO m => MonadIO (StateT s m) |
Instance details Defined in Control.Monad.Trans.State.Lazy Methods liftIO :: IO a -> StateT s m a Source #
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' Source # (>$) :: b -> StateT s m b -> StateT s m a Source #
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 Source #
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 Source #

uninterruptibleMask :: HasCallStack => ((forall a. StateT s m a -> StateT s m a) -> StateT s m b) -> StateT s m b Source #

generalBracket :: HasCallStack => StateT s m a -> (a -> ExitCase b -> StateT s m c) -> (a -> StateT s m b) -> StateT s m (b, c) Source #

MonadThrow m => MonadThrow (StateT s m) |
Instance details Defined in Control.Monad.Catch Methods throwM :: (HasCallStack, Exception e) => e -> StateT s m a Source #
(Functor m, MonadPlus m) => Alternative (StateT s m) |
Instance details Defined in Control.Monad.Trans.State.Lazy Methods empty :: StateT s m a Source # (<|>) :: StateT s m a -> StateT s m a -> StateT s m a Source # some :: StateT s m a -> StateT s m [a] Source # many :: StateT s m a -> StateT s m [a] Source #
(Functor m, Monad m) => Applicative (StateT s m) |
Instance details Defined in Control.Monad.Trans.State.Lazy Methods pure :: a -> StateT s m a Source # (<*>) :: StateT s m (a -> b) -> StateT s m a -> StateT s m b Source # liftA2 :: (a -> b -> c) -> StateT s m a -> StateT s m b -> StateT s m c Source # (*>) :: StateT s m a -> StateT s m b -> StateT s m b Source # (<*) :: StateT s m a -> StateT s m b -> StateT s m a Source #
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 Source # (<$) :: a -> StateT s m b -> StateT s m a Source #
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 Source # (>>) :: StateT s m a -> StateT s m b -> StateT s m b Source # return :: a -> StateT s m a Source #
MonadPlus m => MonadPlus (StateT s m) |
Instance details Defined in Control.Monad.Trans.State.Lazy Methods mzero :: StateT s m a Source # mplus :: StateT s m a -> StateT s m a -> StateT s m a Source #
MonadFail m => MonadFail (StateT s m) |
Instance details Defined in Control.Monad.Trans.State.Lazy Methods fail :: String -> StateT s m a Source #
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 Source #
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 Source #
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 Source #

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 Source #
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.1-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 Source #

to :: Rep (StateT s m a) x -> StateT s m a Source #

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.1-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 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 :: Monad m => StateT s m a -> s -> m s 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 :: (m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b Source #

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 Source #

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

-     withStateT f m = modify f >> m