Document the Semigroup for Map
[packages/containers.git] / Utils / Containers / Internal / State.hs
1 {-# LANGUAGE CPP #-}
2 #include "containers.h"
3 {-# OPTIONS_HADDOCK hide #-}
4
5 -- | A clone of Control.Monad.State.Strict.
6 module Utils.Containers.Internal.State where
7
8 import Prelude hiding (
9 #if MIN_VERSION_base(4,8,0)
10 Applicative
11 #endif
12 )
13
14 import Control.Monad (ap)
15 import Control.Applicative (Applicative(..), liftA)
16
17 newtype State s a = State {runState :: s -> (s, a)}
18
19 instance Functor (State s) where
20 fmap = liftA
21
22 instance Monad (State s) where
23 {-# INLINE return #-}
24 {-# INLINE (>>=) #-}
25 return = pure
26 m >>= k = State $ \ s -> case runState m s of
27 (s', x) -> runState (k x) s'
28
29 instance Applicative (State s) where
30 {-# INLINE pure #-}
31 pure x = State $ \ s -> (s, x)
32 (<*>) = ap
33
34 execState :: State s a -> s -> a
35 execState m x = snd (runState m x)