This module defines the MonadState type class and its instances.

#MonadState Source

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.


  • 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


#get Source

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

Get the current state.

#gets Source

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

Get a value which depends on the current state.

#put Source

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

Set the state.

#modify Source

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

Modify the state by applying a function to the current state.