{-# LANGUAGE CPP #-}
#include "containers.h"
{-# OPTIONS_HADDOCK hide #-}

-- | A clone of Control.Monad.State.Strict.
module Utils.Containers.Internal.State where

import Control.Monad (ap, liftM2)
import Control.Applicative (liftA)
import Utils.Containers.Internal.Prelude
import Prelude ()

newtype State s a = State {forall s a. State s a -> s -> (s, a)
runState :: s -> (s, a)}

instance Functor (State s) where
    fmap :: forall a b. (a -> b) -> State s a -> State s b
fmap = forall (f :: * -> *) a b. Applicative f => (a -> b) -> f a -> f b
liftA

instance Monad (State s) where
    {-# INLINE return #-}
    {-# INLINE (>>=) #-}
    return :: forall a. a -> State s a
return = forall (f :: * -> *) a. Applicative f => a -> f a
pure
    State s a
m >>= :: forall a b. State s a -> (a -> State s b) -> State s b
>>= a -> State s b
k = forall s a. (s -> (s, a)) -> State s a
State forall a b. (a -> b) -> a -> b
$ \ s
s -> case forall s a. State s a -> s -> (s, a)
runState State s a
m s
s of
        (s
s', a
x) -> forall s a. State s a -> s -> (s, a)
runState (a -> State s b
k a
x) s
s'

instance Applicative (State s) where
    {-# INLINE pure #-}
    pure :: forall a. a -> State s a
pure a
x = forall s a. (s -> (s, a)) -> State s a
State forall a b. (a -> b) -> a -> b
$ \ s
s -> (s
s, a
x)
    <*> :: forall a b. State s (a -> b) -> State s a -> State s b
(<*>) = forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
    State s a
m *> :: forall a b. State s a -> State s b -> State s b
*> State s b
n = forall s a. (s -> (s, a)) -> State s a
State forall a b. (a -> b) -> a -> b
$ \s
s -> case forall s a. State s a -> s -> (s, a)
runState State s a
m s
s of
      (s
s', a
_) -> forall s a. State s a -> s -> (s, a)
runState State s b
n s
s'
    liftA2 :: forall a b c. (a -> b -> c) -> State s a -> State s b -> State s c
liftA2 = forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2

execState :: State s a -> s -> a
execState :: forall s a. State s a -> s -> a
execState State s a
m s
x = forall a b. (a, b) -> b
snd (forall s a. State s a -> s -> (s, a)
runState State s a
m s
x)