Skip to content

Add 'state' to the MonadState class

As discussed here: http://www.haskell.org/pipermail/libraries/2011-September/016812.html

Change the Control.Monad.State.Class module in the "mtl" and "monad-tf" libraries, by adding a method to the MonadState class:

class MonadState s m | m -> s where
    ...
    state :: (s -> (a,s)) -> m a -- for mtl
    state :: (StateType m -> (a, StateType m)) -> m a -- for monads-tf
    state f = do s <- get
                 let (a,s') = f s
                 set s'
                 return a

And change the modify function to use 'state':

modify f = state $ \s -> ((),f s)

And add the appropriate instances:

instance (Monad m) => MonadState s (Lazy.StateT s m) where
    ...
    state f = Lazy.state

instance (Monad m) => MonadState s (Strict.StateT s m) where
    ...
    state f = Strict.state

instance (Monad m, Monoid w) => MonadState s (LazyRWS.RWST r w s m) where
    ...
    state f = LazyRWS.state

instance (Monad m, Monoid w) => MonadState s (StrictRWS.RWST r w s m) where
    ...
    state f = StrictRWS.state

The actual implementations should go into the "transformers" library:

state :: Monad m => (s -> (a,s)) -> StateT s m a
state f = StateT $ return . f

state :: (Monoid w, Monad m) => (s -> (a,s)) -> RWST r w s m a
state f = RWST $ \_ s -> let (a,s') = f a  in  return (a, s', mempty)

Note that there is already a function named 'state' in Control.Monad.State, which has 'm' restricted to 'Identity'. This more general function would be a replacement.

Trac metadata
Trac field Value
Version 7.2.1
Type Task
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component libraries (other)
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information