{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE PatternSynonyms #-}
module GHC.Utils.Monad.State.Strict
(
State(State)
, state
, evalState
, execState
, runState
, get
, gets
, put
, modify
) where
import GHC.Prelude
import GHC.Exts (oneShot)
newtype State s a = State' { forall s a. State s a -> s -> (# a, s #)
runState' :: s -> (# a, s #) }
pattern State :: (s -> (# a, s #))
-> State s a
pattern $bState :: forall s a. (s -> (# a, s #)) -> State s a
$mState :: forall {r} {s} {a}.
State s a -> ((s -> (# a, s #)) -> r) -> ((# #) -> r) -> r
State m <- State' m
where
State s -> (# a, s #)
m = forall s a. (s -> (# a, s #)) -> State s a
State' (oneShot :: forall a b. (a -> b) -> a -> b
oneShot forall a b. (a -> b) -> a -> b
$ \s
s -> forall a s. (# a, s #) -> (# a, s #)
forceState (s -> (# a, s #)
m s
s))
forceState :: (# a, s #) -> (# a, s #)
forceState :: forall a s. (# a, s #) -> (# a, s #)
forceState (# a
a, !s
s #) = (# a
a, s
s #)
instance Functor (State s) where
fmap :: forall a b. (a -> b) -> State s a -> State s b
fmap a -> b
f State s a
m = 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
x, s
s' #) -> (# a -> b
f a
x, s
s' #)
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'
state :: (s -> (a, s)) -> State s a
state :: forall s a. (s -> (a, s)) -> State s a
state s -> (a, s)
f = forall s a. (s -> (# a, s #)) -> State s a
State forall a b. (a -> b) -> a -> b
$ \s
s -> case s -> (a, s)
f s
s of (a
r, s
s') -> (# 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')