{-# LANGUAGE CPP #-}
{-# LANGUAGE UndecidableInstances #-}
module Control.Monad.State.Class (
MonadState(..),
modify,
modify',
gets
) where
import Control.Monad.Trans.Cont
import Control.Monad.Trans.Error
import Control.Monad.Trans.Except
import Control.Monad.Trans.Identity
import Control.Monad.Trans.List
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Reader
import qualified Control.Monad.Trans.RWS.Lazy as LazyRWS (RWST, get, put, state)
import qualified Control.Monad.Trans.RWS.Strict as StrictRWS (RWST, get, put, state)
import qualified Control.Monad.Trans.State.Lazy as Lazy (StateT, get, put, state)
import qualified Control.Monad.Trans.State.Strict as Strict (StateT, get, put, state)
import Control.Monad.Trans.Writer.Lazy as Lazy
import Control.Monad.Trans.Writer.Strict as Strict
import Control.Monad.Trans.Class (lift)
import Control.Monad
import Data.Monoid
class Monad m => MonadState s m | m -> s where
get :: m s
get = state (\s -> (s, s))
put :: s -> m ()
put s = state (\_ -> ((), s))
state :: (s -> (a, s)) -> m a
state f = do
s <- get
let ~(a, s') = f s
put s'
return a
#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 707
#endif
modify :: MonadState s m => (s -> s) -> m ()
modify f = state (\s -> ((), f s))
modify' :: MonadState s m => (s -> s) -> m ()
modify' f = state (\s -> let s' = f s in s' `seq` ((), s'))
gets :: MonadState s m => (s -> a) -> m a
gets f = do
s <- get
return (f s)
instance Monad m => MonadState s (Lazy.StateT s m) where
get = Lazy.get
put = Lazy.put
state = Lazy.state
instance Monad m => MonadState s (Strict.StateT s m) where
get = Strict.get
put = Strict.put
state = Strict.state
instance (Monad m, Monoid w) => MonadState s (LazyRWS.RWST r w s m) where
get = LazyRWS.get
put = LazyRWS.put
state = LazyRWS.state
instance (Monad m, Monoid w) => MonadState s (StrictRWS.RWST r w s m) where
get = StrictRWS.get
put = StrictRWS.put
state = StrictRWS.state
instance MonadState s m => MonadState s (ContT r m) where
get = lift get
put = lift . put
state = lift . state
instance (Error e, MonadState s m) => MonadState s (ErrorT e m) where
get = lift get
put = lift . put
state = lift . state
instance MonadState s m => MonadState s (ExceptT e m) where
get = lift get
put = lift . put
state = lift . state
instance MonadState s m => MonadState s (IdentityT m) where
get = lift get
put = lift . put
state = lift . state
instance MonadState s m => MonadState s (ListT m) where
get = lift get
put = lift . put
state = lift . state
instance MonadState s m => MonadState s (MaybeT m) where
get = lift get
put = lift . put
state = lift . state
instance MonadState s m => MonadState s (ReaderT r m) where
get = lift get
put = lift . put
state = lift . state
instance (Monoid w, MonadState s m) => MonadState s (Lazy.WriterT w m) where
get = lift get
put = lift . put
state = lift . state
instance (Monoid w, MonadState s m) => MonadState s (Strict.WriterT w m) where
get = lift get
put = lift . put
state = lift . state