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

module State where

import GhcPrelude

newtype State s a = State { State s a -> s -> (# a, s #)
runState' :: s -> (# a, s #) }
    deriving (a -> State s b -> State s a
(a -> b) -> State s a -> State s b
(forall a b. (a -> b) -> State s a -> State s b)
-> (forall a b. a -> State s b -> State s a) -> Functor (State s)
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
<$ :: a -> State s b -> State s a
$c<$ :: forall s a b. a -> State s b -> State s a
fmap :: (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 :: a -> State s a
pure a
x   = (s -> (# a, s #)) -> State s a
forall s a. (s -> (# a, s #)) -> State s a
State ((s -> (# a, s #)) -> State s a) -> (s -> (# a, s #)) -> State s a
forall a b. (a -> b) -> a -> b
$ \s
s -> (# a
x, s
s #)
   State s (a -> b)
m <*> :: State s (a -> b) -> State s a -> State s b
<*> State s a
n  = (s -> (# b, s #)) -> State s b
forall s a. (s -> (# a, s #)) -> State s a
State ((s -> (# b, s #)) -> State s b) -> (s -> (# b, s #)) -> State s b
forall a b. (a -> b) -> a -> b
$ \s
s -> case State s (a -> b) -> s -> (# a -> b, s #)
forall s a. State s a -> s -> (# a, s #)
runState' State s (a -> b)
m s
s of
                            (# a -> b
f, s
s' #) -> case State s a -> s -> (# a, s #)
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 >>= :: State s a -> (a -> State s b) -> State s b
>>= a -> State s b
n  = (s -> (# b, s #)) -> State s b
forall s a. (s -> (# a, s #)) -> State s a
State ((s -> (# b, s #)) -> State s b) -> (s -> (# b, s #)) -> State s b
forall a b. (a -> b) -> a -> b
$ \s
s -> case State s a -> s -> (# a, s #)
forall s a. State s a -> s -> (# a, s #)
runState' State s a
m s
s of
                             (# a
r, s
s' #) -> State s b -> s -> (# b, 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 :: State s s
get = (s -> (# s, s #)) -> State s s
forall s a. (s -> (# a, s #)) -> State s a
State ((s -> (# s, s #)) -> State s s) -> (s -> (# s, s #)) -> State s s
forall a b. (a -> b) -> a -> b
$ \s
s -> (# s
s, s
s #)

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

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

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


evalState :: State s a -> s -> a
evalState :: State s a -> s -> a
evalState State s a
s s
i = case State s a -> s -> (# a, s #)
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 :: State s a -> s -> s
execState State s a
s s
i = case State s a -> s -> (# a, s #)
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 :: State s a -> s -> (a, s)
runState State s a
s s
i = case State s a -> s -> (# a, s #)
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')