6 { runState' :: s -> (# a, s #) }
8 instance Monad (State s) where
9 return x = State $ \s -> (# x, s #)
10 m >>= n = State $ \s ->
12 (# r, s' #) -> runState' (n r) s'
15 get = State $ \s -> (# s, s #)
17 gets :: (s -> a) -> State s a
18 gets f = State $ \s -> (# f s, s #)
20 put :: s -> State s ()
21 put s' = State $ \s -> (# (), s' #)
23 modify :: (s -> s) -> State s ()
24 modify f = State $ \s -> (# (), f s #)
27 evalState :: State s a -> s -> a
29 = case runState' s i of
33 execState :: State s a -> s -> s
35 = case runState' s i of
39 runState :: State s a -> s -> (a, s)
41 = case runState' s i of
42 (# a, s' #) -> (a, s')
47 => (acc -> x -> m (acc, y)) -- ^ combining funcction
48 -> acc -- ^ initial state
50 -> m (acc, [y]) -- ^ final state, outputs
52 mapAccumLM _ s [] = return (s, [])
56 (s2, xs') <- mapAccumLM f s1 xs