1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
module Control.Monad.State where
data State s a = State (s -> (a, s))
instance Functor (State s) where
fmap f (State g) = State $ \s -> let (a,s1) = g s in (f a,s1)
instance Monad (State s) where
return x = state (\s -> (x, s))
m >>= f = state (\s -> let (x, s') = runState m s
in runState (f x) s')
runState :: State s a -> (s -> (a,s))
runState (State st) = st
state :: (s -> (a, s)) -> State s a
state = State
get :: State s s
get = state (\s -> (s, s))
put :: s -> State s ()
put s = state (\_ -> ((), s))
modify :: (s -> s) -> State s ()
modify f = state (\s -> ((), f s))
evalState :: State s a -> s -> a
evalState m s = fst (runState m s)
execState :: State s a -> s -> s
execState m s = snd (runState m s)
|