3a_asm: ghc flags
[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 {-# LANGUAGE FunctionalDependencies, MultiParamTypeClasses #-}
22
23 module Control.Monad.State.Class (
24     -- * MonadState class
25     MonadState(..),
26     modify,
27     gets,
28   ) where
29
30 -- ---------------------------------------------------------------------------
31 -- | /get/ returns the state from the internals of the monad.
32 --
33 -- /put/ replaces the state inside the monad.
34
35 class (Monad m) => MonadState s m | m -> s where
36     get :: m s
37     put :: s -> m ()
38
39 -- | Monadic state transformer.
40 --
41 --      Maps an old state to a new state inside a state monad.
42 --      The old state is thrown away.
43 --
44 -- >      Main> :t modify ((+1) :: Int -> Int)
45 -- >      modify (...) :: (MonadState Int a) => a ()
46 --
47 --    This says that @modify (+1)@ acts over any
48 --    Monad that is a member of the @MonadState@ class,
49 --    with an @Int@ state.
50
51 modify :: (MonadState s m) => (s -> s) -> m ()
52 modify f = do
53     s <- get
54     put (f s)
55
56 -- | Gets specific component of the state, using a projection function
57 -- supplied.
58
59 gets :: (MonadState s m) => (s -> a) -> m a
60 gets f = do
61     s <- get
62     return (f s)
63