Module

Control.Monad.State.Trans

Package
purescript-transformers
Repository
purescript/purescript-transformers

This module defines the state monad transformer, StateT.

#StateT Source

newtype StateT :: Type -> (Type -> Type) -> Type -> Typenewtype 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

#runStateT Source

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

Run a computation in the StateT monad.

#evalStateT Source

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.

#execStateT Source

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

Run a computation in the StateT monad discarding the result.

#mapStateT Source

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.

#withStateT Source

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

Modify the final state in a StateT monad action.

Re-exports from Control.Monad.State.Class

#MonadState Source

class MonadState :: Type -> (Type -> Type) -> Constraintclass (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 Source

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

Set the state.

#modify_ Source

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

#modify Source

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 Source

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

Get a value which depends on the current state.

#get Source

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

Get the current state.

Re-exports from Control.Monad.Trans.Class

#MonadTrans Source

class MonadTrans :: ((Type -> Type) -> Type -> Type) -> Constraintclass 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