#5714: Add 'state' to the MonadState class
------------------------------+---------------------------------------------
 Reporter:  twanvl            |          Owner:                   
     Type:  task              |         Status:  new              
 Priority:  normal            |      Component:  libraries (other)
  Version:  7.2.1             |       Keywords:  mtl, monads-tf   
       Os:  Unknown/Multiple  |   Architecture:  Unknown/Multiple 
  Failure:  None/Unknown      |       Testcase:                   
Blockedby:                    |       Blocking:                   
  Related:                    |  
------------------------------+---------------------------------------------
 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.

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5714>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs

Reply via email to