{-# LANGUAGE Safe #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-redundant-constraints #-}
module Control.Monad.State.Class (
MonadState(..),
modify,
modify',
gets
) where
import Control.Monad.Trans.Cont (ContT)
import Control.Monad.Trans.Except (ExceptT)
import Control.Monad.Trans.Identity (IdentityT)
import Control.Monad.Trans.Maybe (MaybeT)
import Control.Monad.Trans.Reader (ReaderT)
import qualified Control.Monad.Trans.RWS.Lazy as LazyRWS
import qualified Control.Monad.Trans.RWS.Strict as StrictRWS
import qualified Control.Monad.Trans.State.Lazy as Lazy
import qualified Control.Monad.Trans.State.Strict as Strict
import qualified Control.Monad.Trans.Writer.Lazy as Lazy
import qualified Control.Monad.Trans.Writer.Strict as Strict
import Control.Monad.Trans.Accum (AccumT)
import Control.Monad.Trans.Select (SelectT)
import qualified Control.Monad.Trans.RWS.CPS as CPSRWS
import qualified Control.Monad.Trans.Writer.CPS as CPS
import Control.Monad.Trans.Class (lift)
class Monad m => MonadState s m | m -> s where
get :: m s
get = forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (\s
s -> (s
s, s
s))
put :: s -> m ()
put s
s = forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (\s
_ -> ((), s
s))
state :: (s -> (a, s)) -> m a
state s -> (a, s)
f = do
s
s <- forall s (m :: * -> *). MonadState s m => m s
get
let ~(a
a, s
s') = s -> (a, s)
f s
s
forall s (m :: * -> *). MonadState s m => s -> m ()
put s
s'
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
{-# MINIMAL state | get, put #-}
modify :: MonadState s m => (s -> s) -> m ()
modify :: forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify s -> s
f = forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (\s
s -> ((), s -> s
f s
s))
modify' :: MonadState s m => (s -> s) -> m ()
modify' :: forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify' s -> s
f = do
s
s' <- forall s (m :: * -> *). MonadState s m => m s
get
forall s (m :: * -> *). MonadState s m => s -> m ()
put forall a b. (a -> b) -> a -> b
$! s -> s
f s
s'
gets :: MonadState s m => (s -> a) -> m a
gets :: forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets s -> a
f = do
s
s <- forall s (m :: * -> *). MonadState s m => m s
get
forall (m :: * -> *) a. Monad m => a -> m a
return (s -> a
f s
s)
instance Monad m => MonadState s (Lazy.StateT s m) where
get :: StateT s m s
get = forall (m :: * -> *) s. Monad m => StateT s m s
Lazy.get
put :: s -> StateT s m ()
put = forall (m :: * -> *) s. Monad m => s -> StateT s m ()
Lazy.put
state :: forall a. (s -> (a, s)) -> StateT s m a
state = forall (m :: * -> *) s a. Monad m => (s -> (a, s)) -> StateT s m a
Lazy.state
instance Monad m => MonadState s (Strict.StateT s m) where
get :: StateT s m s
get = forall (m :: * -> *) s. Monad m => StateT s m s
Strict.get
put :: s -> StateT s m ()
put = forall (m :: * -> *) s. Monad m => s -> StateT s m ()
Strict.put
state :: forall a. (s -> (a, s)) -> StateT s m a
state = forall (m :: * -> *) s a. Monad m => (s -> (a, s)) -> StateT s m a
Strict.state
instance (Monad m, Monoid w) => MonadState s (CPSRWS.RWST r w s m) where
get :: RWST r w s m s
get = forall (m :: * -> *) r w s. Monad m => RWST r w s m s
CPSRWS.get
put :: s -> RWST r w s m ()
put = forall (m :: * -> *) s r w. Monad m => s -> RWST r w s m ()
CPSRWS.put
state :: forall a. (s -> (a, s)) -> RWST r w s m a
state = forall (m :: * -> *) s a r w.
Monad m =>
(s -> (a, s)) -> RWST r w s m a
CPSRWS.state
instance (Monad m, Monoid w) => MonadState s (LazyRWS.RWST r w s m) where
get :: RWST r w s m s
get = forall w (m :: * -> *) r s. (Monoid w, Monad m) => RWST r w s m s
LazyRWS.get
put :: s -> RWST r w s m ()
put = forall w (m :: * -> *) s r.
(Monoid w, Monad m) =>
s -> RWST r w s m ()
LazyRWS.put
state :: forall a. (s -> (a, s)) -> RWST r w s m a
state = forall w (m :: * -> *) s a r.
(Monoid w, Monad m) =>
(s -> (a, s)) -> RWST r w s m a
LazyRWS.state
instance (Monad m, Monoid w) => MonadState s (StrictRWS.RWST r w s m) where
get :: RWST r w s m s
get = forall w (m :: * -> *) r s. (Monoid w, Monad m) => RWST r w s m s
StrictRWS.get
put :: s -> RWST r w s m ()
put = forall w (m :: * -> *) s r.
(Monoid w, Monad m) =>
s -> RWST r w s m ()
StrictRWS.put
state :: forall a. (s -> (a, s)) -> RWST r w s m a
state = forall w (m :: * -> *) s a r.
(Monoid w, Monad m) =>
(s -> (a, s)) -> RWST r w s m a
StrictRWS.state
instance MonadState s m => MonadState s (ContT r m) where
get :: ContT r m s
get = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall s (m :: * -> *). MonadState s m => m s
get
put :: s -> ContT r m ()
put = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => s -> m ()
put
state :: forall a. (s -> (a, s)) -> ContT r m a
state = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state
instance MonadState s m => MonadState s (ExceptT e m) where
get :: ExceptT e m s
get = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall s (m :: * -> *). MonadState s m => m s
get
put :: s -> ExceptT e m ()
put = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => s -> m ()
put
state :: forall a. (s -> (a, s)) -> ExceptT e m a
state = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state
instance MonadState s m => MonadState s (IdentityT m) where
get :: IdentityT m s
get = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall s (m :: * -> *). MonadState s m => m s
get
put :: s -> IdentityT m ()
put = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => s -> m ()
put
state :: forall a. (s -> (a, s)) -> IdentityT m a
state = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state
instance MonadState s m => MonadState s (MaybeT m) where
get :: MaybeT m s
get = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall s (m :: * -> *). MonadState s m => m s
get
put :: s -> MaybeT m ()
put = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => s -> m ()
put
state :: forall a. (s -> (a, s)) -> MaybeT m a
state = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state
instance MonadState s m => MonadState s (ReaderT r m) where
get :: ReaderT r m s
get = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall s (m :: * -> *). MonadState s m => m s
get
put :: s -> ReaderT r m ()
put = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => s -> m ()
put
state :: forall a. (s -> (a, s)) -> ReaderT r m a
state = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state
instance (Monoid w, MonadState s m) => MonadState s (CPS.WriterT w m) where
get :: WriterT w m s
get = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall s (m :: * -> *). MonadState s m => m s
get
put :: s -> WriterT w m ()
put = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => s -> m ()
put
state :: forall a. (s -> (a, s)) -> WriterT w m a
state = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state
instance (Monoid w, MonadState s m) => MonadState s (Lazy.WriterT w m) where
get :: WriterT w m s
get = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall s (m :: * -> *). MonadState s m => m s
get
put :: s -> WriterT w m ()
put = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => s -> m ()
put
state :: forall a. (s -> (a, s)) -> WriterT w m a
state = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state
instance (Monoid w, MonadState s m) => MonadState s (Strict.WriterT w m) where
get :: WriterT w m s
get = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall s (m :: * -> *). MonadState s m => m s
get
put :: s -> WriterT w m ()
put = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => s -> m ()
put
state :: forall a. (s -> (a, s)) -> WriterT w m a
state = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state
instance
( Monoid w
, MonadState s m
) => MonadState s (AccumT w m) where
get :: AccumT w m s
get = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall s (m :: * -> *). MonadState s m => m s
get
put :: s -> AccumT w m ()
put = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => s -> m ()
put
state :: forall a. (s -> (a, s)) -> AccumT w m a
state = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state
instance MonadState s m => MonadState s (SelectT r m) where
get :: SelectT r m s
get = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall s (m :: * -> *). MonadState s m => m s
get
put :: s -> SelectT r m ()
put = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => s -> m ()
put
state :: forall a. (s -> (a, s)) -> SelectT r m a
state = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state