Safe Haskell | Safe |
---|---|

Language | Haskell2010 |

`MSF`

s with a `State`

monadic layer.

This module contains functions to work with `MSF`

s that include a `State`

monadic layer. This includes functions to create new `MSF`

s that include an
additional layer, and functions to flatten that layer out of the `MSF`

's
transformer stack.

It is based on the _strict_ state monad `Strict`

,
so when combining it with other modules such as `mtl`

's,
the strict version has to be included, i.e. `Strict`

instead of `State`

or `Lazy`

.

## Synopsis

- newtype StateT s (m :: Type -> Type) a = StateT {
- runStateT :: s -> m (a, s)

- 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
- 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
- liftCallCC' :: CallCC m (a, s) (b, s) -> CallCC (StateT s m) a b
- gets :: Monad m => (s -> a) -> StateT s m a
- modify' :: Monad m => (s -> s) -> StateT s m ()
- modify :: Monad m => (s -> s) -> StateT s m ()
- put :: Monad m => s -> StateT s m ()
- get :: Monad m => StateT s m s
- state :: Monad m => (s -> (a, s)) -> StateT s m a
- stateS :: (Functor m, Monad m) => MSF m (s, a) (s, b) -> MSF (StateT s m) a b
- runStateS :: (Functor m, Monad m) => MSF (StateT s m) a b -> MSF m (s, a) (s, b)
- runStateS_ :: (Functor m, Monad m) => MSF (StateT s m) a b -> s -> MSF m a (s, b)
- runStateS__ :: (Functor m, Monad m) => MSF (StateT s m) a b -> s -> MSF m a b

# Documentation

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.

## Instances

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 |

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 m`m`

on a state modified by
applying `f`

.

`withStateT`

f m =`modify`

f >> m

liftCallCC' :: CallCC m (a, s) (b, s) -> CallCC (StateT s m) a b #

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 uniformity property (see Control.Monad.Signatures).

:: Monad m | |

=> (s -> (a, s)) | pure state transformer |

-> StateT s m a | equivalent state-passing computation |

Construct a state monad computation from a function.
(The inverse of `runState`

.)