22bd85ebeac78101bbb541245459c74eb98f817e
[calu.git] / 3a_asm / Control / Monad / State / Class.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  Control.Monad.State.Class
4 -- Copyright   :  (c) Andy Gill 2001,
5 --                (c) Oregon Graduate Institute of Science and Technology, 2001
6 -- License     :  BSD-style (see the file libraries/base/LICENSE)
7 --
8 -- Maintainer  :  libraries@haskell.org
9 -- Stability   :  experimental
10 -- Portability :  non-portable (multi-param classes, functional dependencies)
11 --
12 -- MonadState class.
13 --
14 --      This module is inspired by the paper
15 --      /Functional Programming with Overloading and
16 --          Higher-Order Polymorphism/,
17 --        Mark P Jones (<http://web.cecs.pdx.edu/~mpj/>)
18 --          Advanced School of Functional Programming, 1995.
19
20 -----------------------------------------------------------------------------
21
22 module Control.Monad.State.Class (
23     -- * MonadState class
24     MonadState(..),
25     modify,
26     gets,
27   ) where
28
29 -- ---------------------------------------------------------------------------
30 -- | /get/ returns the state from the internals of the monad.
31 --
32 -- /put/ replaces the state inside the monad.
33
34 class (Monad m) => MonadState s m | m -> s where
35     get :: m s
36     put :: s -> m ()
37
38 -- | Monadic state transformer.
39 --
40 --      Maps an old state to a new state inside a state monad.
41 --      The old state is thrown away.
42 --
43 -- >      Main> :t modify ((+1) :: Int -> Int)
44 -- >      modify (...) :: (MonadState Int a) => a ()
45 --
46 --    This says that @modify (+1)@ acts over any
47 --    Monad that is a member of the @MonadState@ class,
48 --    with an @Int@ state.
49
50 modify :: (MonadState s m) => (s -> s) -> m ()
51 modify f = do
52     s <- get
53     put (f s)
54
55 -- | Gets specific component of the state, using a projection function
56 -- supplied.
57
58 gets :: (MonadState s m) => (s -> a) -> m a
59 gets f = do
60     s <- get
61     return (f s)
62