Module

Control.Monad.State

This module defines the State monad.

#State

type State s = StateT s Identity

The State monad is a synonym for the StateT monad transformer, applied to the Identity monad.

#runState

runState :: forall s a. State s a -> s -> Tuple a s

Run a computation in the State monad

#evalState

evalState :: forall s a. State s a -> s -> a

Run a computation in the State monad, discarding the final state

#execState

execState :: forall s a. State s a -> s -> s

Run a computation in the State monad, discarding the result

#mapState

mapState :: forall s a b. (Tuple a s -> Tuple b s) -> State s a -> State s b

Change the type of the result in a State action

#withState

withState :: forall s a. (s -> s) -> State s a -> State s a

Modify the state in a State action

Re-exports from Control.Monad.State.Class

#MonadState

class (Monad m) <= MonadState s m | m -> s where

The MonadState s type class represents those monads which support a single piece of mutable state of type s.

  • state f updates the state using the function f.

An implementation is provided for StateT, and for other monad transformers defined in this library.

Laws:

  • do { get ; get } = get
  • do { put x ; put y } = put y
  • do { put x ; get } = put x $> x
  • do { s <- get ; put s } = pure unit

Members

#put

put :: forall m s. MonadState s m => s -> m Unit

Set the state.

#modify_

modify_ :: forall s m. MonadState s m => (s -> s) -> m Unit

#modify

modify :: forall s m. MonadState s m => (s -> s) -> m s

Modify the state by applying a function to the current state. The returned value is the new state value.

#gets

gets :: forall s m a. MonadState s m => (s -> a) -> m a

Get a value which depends on the current state.

#get

get :: forall m s. MonadState s m => m s

Get the current state.

Re-exports from Control.Monad.State.Trans

#StateT

newtype StateT s m a

The state monad transformer.

This monad transformer extends the base monad with the operations get and put which can be used to model a single piece of mutable state.

The MonadState type class describes the operations supported by this monad.

Constructors

Instances

#MonadTrans

class MonadTrans t  where

The MonadTrans type class represents monad transformers.

A monad transformer is a type constructor of kind (* -> *) -> * -> *, which takes a Monad as its first argument, and returns another Monad.

This allows us to add additional effects to an existing monad. By iterating this process, we create monad transformer stacks, which contain all of the effects required for a particular computation.

The laws state that lift is a Monad morphism.

Laws:

  • lift (pure a) = pure a
  • lift (do { x <- m ; y }) = do { x <- lift m ; lift y }

Members

#withStateT

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

Modify the final state in a StateT monad action.

#runStateT

runStateT :: forall s m a. StateT s m a -> s -> m (Tuple a s)

Run a computation in the StateT monad.

#mapStateT

mapStateT :: forall s m1 m2 a b. (m1 (Tuple a s) -> m2 (Tuple b s)) -> StateT s m1 a -> StateT s m2 b

Change the result type in a StateT monad action.

#execStateT

execStateT :: forall s m a. Functor m => StateT s m a -> s -> m s

Run a computation in the StateT monad discarding the result.

#evalStateT

evalStateT :: forall s m a. Functor m => StateT s m a -> s -> m a

Run a computation in the StateT monad, discarding the final state.

Modules