module Control.Monad.State.Class (
MonadState(..),
modify,
gets,
) where
import Control.Monad.Trans (lift)
import Control.Monad.Trans.Cont
import Control.Monad.Trans.Error
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)
import qualified Control.Monad.Trans.RWS.Strict as StrictRWS (RWST, get, put)
import qualified Control.Monad.Trans.State.Lazy as Lazy (StateT, get, put)
import qualified Control.Monad.Trans.State.Strict as Strict (StateT, get, put)
import Control.Monad.Trans.Writer.Lazy as Lazy
import Control.Monad.Trans.Writer.Strict as Strict
import Data.Monoid
class (Monad m) => MonadState m where
type StateType m
get :: m (StateType m)
put :: StateType m -> m ()
modify :: (MonadState m) => (StateType m -> StateType m) -> m ()
modify f = do
s <- get
put (f s)
gets :: (MonadState m) => (StateType m -> a) -> m a
gets f = do
s <- get
return (f s)
instance (Monad m) => MonadState (Lazy.StateT s m) where
type StateType (Lazy.StateT s m) = s
get = Lazy.get
put = Lazy.put
instance (Monad m) => MonadState (Strict.StateT s m) where
type StateType (Strict.StateT s m) = s
get = Strict.get
put = Strict.put
instance (Monad m, Monoid w) => MonadState (LazyRWS.RWST r w s m) where
type StateType (LazyRWS.RWST r w s m) = s
get = LazyRWS.get
put = LazyRWS.put
instance (Monad m, Monoid w) => MonadState (StrictRWS.RWST r w s m) where
type StateType (StrictRWS.RWST r w s m) = s
get = StrictRWS.get
put = StrictRWS.put
instance (MonadState m) => MonadState (ContT r m) where
type StateType (ContT r m) = StateType m
get = lift get
put = lift . put
instance (Error e, MonadState m) => MonadState (ErrorT e m) where
type StateType (ErrorT e m) = StateType m
get = lift get
put = lift . put
instance (MonadState m) => MonadState (IdentityT m) where
type StateType (IdentityT m) = StateType m
get = lift get
put = lift . put
instance (MonadState m) => MonadState (ListT m) where
type StateType (ListT m) = StateType m
get = lift get
put = lift . put
instance (MonadState m) => MonadState (MaybeT m) where
type StateType (MaybeT m) = StateType m
get = lift get
put = lift . put
instance (MonadState m) => MonadState (ReaderT r m) where
type StateType (ReaderT r m) = StateType m
get = lift get
put = lift . put
instance (Monoid w, MonadState m) => MonadState (Lazy.WriterT w m) where
type StateType (Lazy.WriterT w m) = StateType m
get = lift get
put = lift . put
instance (Monoid w, MonadState m) => MonadState (Strict.WriterT w m) where
type StateType (Strict.WriterT w m) = StateType m
get = lift get
put = lift . put