1 -----------------------------------------------------------------------------
3 -- Module : Control.Monad.State
4 -- Copyright : (c) Andy Gill 2001,
5 -- (c) Oregon Graduate Institute of Science and Technology, 2001
6 -- License : BSD-style (see the file libraries/core/LICENSE)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : experimental
10 -- Portability : non-portable ( requires mulit-parameter type classes,
11 -- requires functional dependencies )
15 -- Inspired by the paper
16 -- \em{Functional Programming with Overloading and
17 -- Higher-Order Polymorphism},
18 -- \A[HREF="http://www.cse.ogi.edu/~mpj"]{Mark P Jones},
19 -- Advanced School of Functional Programming, 1995.}
20 -----------------------------------------------------------------------------
22 module Control.Monad.State (
39 module Control.Monad.Fix,
40 module Control.Monad.Trans,
46 import Control.Monad.Fix
47 import Control.Monad.Trans
48 import Control.Monad.Reader
49 import Control.Monad.Writer
51 -- ---------------------------------------------------------------------------
54 -- get: returns the state from the internals of the monad.
55 -- put: changes (replaces) the state inside the monad.
57 class (Monad m) => MonadState s m | m -> s where
61 -- Monadic state transformer.
63 -- Maps an old state to a new state inside a state monad.
64 -- The old state is thrown away.}
66 -- Main> :t modify ((+1) :: Int -> Int)
67 -- modify (...) :: (MonadState Int a) => a ()
69 -- This says that modify (+1) acts over any
70 -- Monad that is a member of the MonadState class,
73 modify :: (MonadState s m) => (s -> s) -> m ()
78 -- Get part of the state
80 -- gets specific component of the state,
81 -- using a projection function supplied.
83 gets :: (MonadState s m) => (s -> a) -> m a
88 -- ---------------------------------------------------------------------------
89 -- Our parameterizable state monad
91 newtype State s a = State { runState :: s -> (a, s) }
93 -- The State Monad structure is paramterized over just the state.
95 instance Functor (State s) where
96 fmap f m = State $ \s -> let
97 (a, s') = runState m s
100 instance Monad (State s) where
101 return a = State $ \s -> (a, s)
102 m >>= k = State $ \s -> let
103 (a, s') = runState m s
106 instance MonadFix (State s) where
107 mfix f = State $ \s -> let (a, s') = runState (f a) s in (a, s')
109 instance MonadState s (State s) where
110 get = State $ \s -> (s, s)
111 put s = State $ \_ -> ((), s)
114 evalState :: State s a -> s -> a
115 evalState m s = fst (runState m s)
117 execState :: State s a -> s -> s
118 execState m s = snd (runState m s)
120 mapState :: ((a, s) -> (b, s)) -> State s a -> State s b
121 mapState f m = State $ f . runState m
123 withState :: (s -> s) -> State s a -> State s a
124 withState f m = State $ runState m . f
126 -- ---------------------------------------------------------------------------
127 -- Our parameterizable state monad, with an inner monad
129 newtype StateT s m a = StateT { runStateT :: s -> m (a,s) }
131 --The StateT Monad structure is parameterized over two things:
134 -- * m - The inner monad.
136 -- Here are some examples of use:
138 -- (Parser from ParseLib with Hugs)
139 -- type Parser a = StateT String [] a
140 -- ==> StateT (String -> [(a,String)])
141 -- For example, item can be written as:
142 -- item = do (x:xs) <- get
146 -- type BoringState s a = StateT s Indentity a
147 -- ==> StateT (s -> Identity (a,s))
149 -- type StateWithIO s a = StateT s IO a
150 -- ==> StateT (s -> IO (a,s))
152 -- type StateWithErr s a = StateT s Maybe a
153 -- ==> StateT (s -> Maybe (a,s))
155 instance (Monad m) => Functor (StateT s m) where
156 fmap f m = StateT $ \s -> do
157 (x, s') <- runStateT m s
160 instance (Monad m) => Monad (StateT s m) where
161 return a = StateT $ \s -> return (a, s)
162 m >>= k = StateT $ \s -> do
163 (a, s') <- runStateT m s
165 fail str = StateT $ \_ -> fail str
167 instance (MonadPlus m) => MonadPlus (StateT s m) where
168 mzero = StateT $ \_ -> mzero
169 m `mplus` n = StateT $ \s -> runStateT m s `mplus` runStateT n s
171 instance (MonadFix m) => MonadFix (StateT s m) where
172 mfix f = StateT $ \s -> mfix $ \ ~(a, _) -> runStateT (f a) s
174 instance (Monad m) => MonadState s (StateT s m) where
175 get = StateT $ \s -> return (s, s)
176 put s = StateT $ \_ -> return ((), s)
178 instance MonadTrans (StateT s) where
179 lift m = StateT $ \s -> do
183 instance (MonadIO m) => MonadIO (StateT s m) where
184 liftIO = lift . liftIO
186 instance (MonadReader r m) => MonadReader r (StateT s m) where
188 local f m = StateT $ \s -> local f (runStateT m s)
190 instance (MonadWriter w m) => MonadWriter w (StateT s m) where
192 listen m = StateT $ \s -> do
193 ((a, s'), w) <- listen (runStateT m s)
195 pass m = StateT $ \s -> pass $ do
196 ((a, f), s') <- runStateT m s
200 evalStateT :: (Monad m) => StateT s m a -> s -> m a
202 (a, _) <- runStateT m s
205 execStateT :: (Monad m) => StateT s m a -> s -> m s
207 (_, s') <- runStateT m s
210 mapStateT :: (m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b
211 mapStateT f m = StateT $ f . runStateT m
213 withStateT :: (s -> s) -> StateT s m a -> StateT s m a
214 withStateT f m = StateT $ runStateT m . f
216 -- ---------------------------------------------------------------------------
217 -- MonadState instances for other monad transformers
219 instance (MonadState s m) => MonadState s (ReaderT r m) where
223 instance (Monoid w, MonadState s m) => MonadState s (WriterT w m) where