module Control.Effect.State.Strict
  ( module Control.Effect.State
  , runState
  , evalState
  , execState
  ) where

import Control.Effect.Base
import Control.Effect.State
import Control.Effect.Internal (evalState)
import Data.Tuple (swap)

-- | Handles a @'State'@ effect using a strict cell of mutable state—each use
-- of 'put' or 'modify' eagerly forces the new value. The state is initialized
-- to the given value, and the final state is returned alongside the
-- computation’s result.
runState :: s -> Eff (State s ': effs) a -> Eff effs (s, a)
runState :: s -> Eff (State s : effs) a -> Eff effs (s, a)
runState s
s Eff (State s : effs) a
m = s -> Eff (State s : effs) (s, a) -> Eff effs (s, a)
forall s (effs :: [(* -> *) -> * -> *]) a.
s -> Eff (State s : effs) a -> Eff effs a
evalState s
s (((a, s) -> (s, a)) -> a -> s -> (s, a)
forall a b c. ((a, b) -> c) -> a -> b -> c
curry (a, s) -> (s, a)
forall a b. (a, b) -> (b, a)
swap (a -> s -> (s, a))
-> Eff (State s : effs) a -> Eff (State s : effs) (s -> (s, a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Eff (State s : effs) a
m Eff (State s : effs) (s -> (s, a))
-> Eff (State s : effs) s -> Eff (State s : effs) (s, a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Eff (State s : effs) s
forall s (effs :: [(* -> *) -> * -> *]).
(State s :< effs) =>
Eff effs s
get)

execState :: s -> Eff (State s ': effs) a -> Eff effs s
execState :: s -> Eff (State s : effs) a -> Eff effs s
execState s
s Eff (State s : effs) a
m = s -> Eff (State s : effs) s -> Eff effs s
forall s (effs :: [(* -> *) -> * -> *]) a.
s -> Eff (State s : effs) a -> Eff effs a
evalState s
s (Eff (State s : effs) a
m Eff (State s : effs) a
-> Eff (State s : effs) s -> Eff (State s : effs) s
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Eff (State s : effs) s
forall s (effs :: [(* -> *) -> * -> *]).
(State s :< effs) =>
Eff effs s
get)