Fix warnings in utils/State
[ghc-hetmet.git] / compiler / utils / State.hs
1
2 module State where
3
4 newtype State s a = State { runState' :: s -> (# a, s #) }
5
6 instance Monad (State s) where
7     return x = State $ \s -> (# x, s #)
8     m >>= n  = State $ \s -> case runState' m s of
9                              (# r, s' #) -> runState' (n r) s'
10
11 get :: State s s
12 get = State $ \s -> (# s, s #)
13
14 gets :: (s -> a) -> State s a
15 gets f = State $ \s -> (# f s, s #)
16
17 put :: s -> State s ()
18 put s' = State $ \_ -> (# (), s' #)
19
20 modify :: (s -> s) -> State s ()
21 modify f = State $ \s -> (# (), f s #)
22
23
24 evalState :: State s a -> s -> a
25 evalState s i = case runState' s i of
26                 (# a, _ #) -> a
27
28
29 execState :: State s a -> s -> s
30 execState s i = case runState' s i of
31                 (# _, s' #) -> s'
32
33
34 runState :: State s a -> s -> (a, s)
35 runState s i = case runState' s i of
36                (# a, s' #) -> (a, s')
37
38
39 mapAccumLM
40         :: Monad m
41         => (acc -> x -> m (acc, y))     -- ^ combining funcction
42         -> acc                          -- ^ initial state
43         -> [x]                          -- ^ inputs
44         -> m (acc, [y])                 -- ^ final state, outputs
45
46 mapAccumLM _ s [] = return (s, [])
47 mapAccumLM f s (x:xs)
48  = do (s1, x')  <- f s x
49       (s2, xs') <- mapAccumLM f s1 xs
50       return (s2, x' : xs')
51