    {-# LANGUAGE DeriveFunctor #-}
    {-# LANGUAGE UnboxedTuples #-}

    module GHC.Utils.Monad.State where

    import GHC.Prelude

    newtype State s a = State { forall s a. State s a -> s -> (# a, s #)
    runState' :: s -> (# a, s #) }
        deriving (forall a b. a -> State s b -> State s a
    forall a b. (a -> b) -> State s a -> State s b
    forall s a b. a -> State s b -> State s a
    forall s a b. (a -> b) -> State s a -> State s b
    forall (f :: * -> *).
    (forall a b. (a -> b) -> f a -> f b)
    -> (forall a b. a -> f b -> f a) -> Functor f
    <$ :: forall a b. a -> State s b -> State s a
    $c<$ :: forall s a b. a -> State s b -> State s a
    fmap :: forall a b. (a -> b) -> State s a -> State s b
    $cfmap :: forall s a b. (a -> b) -> State s a -> State s b
    Functor)

    instance Applicative (State s) where
       pure :: forall a. a -> State s a
    pure a
    x   = forall s a. (s -> (# a, s #)) -> State s a
    State forall a b. (a -> b) -> a -> b
    $ \s
    s -> (# a
    x, s
    s #)
       State s (a -> b)
    m <*> :: forall a b. State s (a -> b) -> State s a -> State s b
    <*> State s a
    n  = forall s a. (s -> (# a, s #)) -> State s a
    State forall a b. (a -> b) -> a -> b
    $ \s
    s -> case forall s a. State s a -> s -> (# a, s #)
    runState' State s (a -> b)
    m s
    s of
                                (# a -> b
    f, s
    s' #) -> case forall s a. State s a -> s -> (# a, s #)
    runState' State s a
    n s
    s' of
                                               (# a
    x, s
    s'' #) -> (# a -> b
    f a
    x, s
    s'' #)

    instance Monad (State s) where
        State s a
    m >>= :: forall a b. State s a -> (a -> State s b) -> State s b
    >>= a -> State s b
    n  = forall s a. (s -> (# a, s #)) -> State s a
    State forall a b. (a -> b) -> a -> b
    $ \s
    s -> case forall s a. State s a -> s -> (# a, s #)
    runState' State s a
    m s
    s of
                                 (# a
    r, s
    s' #) -> forall s a. State s a -> s -> (# a, s #)
    runState' (a -> State s b
    n a
    r) s
    s'

    get :: State s s
    get :: forall s. State s s
    get = forall s a. (s -> (# a, s #)) -> State s a
    State forall a b. (a -> b) -> a -> b
    $ \s
    s -> (# s
    s, s
    s #)

    gets :: (s -> a) -> State s a
    gets :: forall s a. (s -> a) -> State s a
    gets s -> a
    f = forall s a. (s -> (# a, s #)) -> State s a
    State forall a b. (a -> b) -> a -> b
    $ \s
    s -> (# s -> a
    f s
    s, s
    s #)

    put :: s -> State s ()
    put :: forall s. s -> State s ()
    put s
    s' = forall s a. (s -> (# a, s #)) -> State s a
    State forall a b. (a -> b) -> a -> b
    $ \s
    _ -> (# (), s
    s' #)

    modify :: (s -> s) -> State s ()
    modify :: forall s. (s -> s) -> State s ()
    modify s -> s
    f = forall s a. (s -> (# a, s #)) -> State s a
    State forall a b. (a -> b) -> a -> b
    $ \s
    s -> (# (), s -> s
    f s
    s #)

    evalState :: State s a -> s -> a
    evalState :: forall s a. State s a -> s -> a
    evalState State s a
    s s
    i = case forall s a. State s a -> s -> (# a, s #)
    runState' State s a
    s s
    i of
                    (# a
    a, s
    _ #) -> a
    a

    execState :: State s a -> s -> s
    execState :: forall s a. State s a -> s -> s
    execState State s a
    s s
    i = case forall s a. State s a -> s -> (# a, s #)
    runState' State s a
    s s
    i of
                    (# a
    _, s
    s' #) -> s
    s'

    runState :: State s a -> s -> (a, s)
    runState :: forall s a. State s a -> s -> (a, s)
    runState State s a
    s s
    i = case forall s a. State s a -> s -> (# a, s #)
    runState' State s a
    s s
    i of
                   (# a
    a, s
    s' #) -> (a
    a, s
    s')