-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.hs
More file actions
33 lines (22 loc) · 687 Bytes
/
State.hs
File metadata and controls
33 lines (22 loc) · 687 Bytes
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
module State (State, get, put, runState, evalState, execState) where
import Control.Monad
newtype State s a = S (s -> (a, s))
instance Functor (State s) where
fmap = liftM
instance Applicative (State s) where
pure = return
(<*>) = ap
instance Monad (State s) where
return x = S (\s -> (x, s))
st >>= f = S (\s -> let (x, s') = runState st s
in runState (f x) s')
runState :: State s a -> s -> (a, s)
runState (S f) = f
evalState :: State s a -> s -> a
evalState s = fst . runState s
execState :: State s a -> s -> s
execState s = snd . runState s
get :: State s s
get = S (\s -> (s, s))
put :: s -> State s ()
put s = S (const ((), s))