{-# LANGUAGE DeriveAnyClass, DeriveFunctor, DeriveGeneric, DerivingStrategies, ExplicitForAll, FlexibleContexts, FlexibleInstances, MultiParamTypeClasses, TypeOperators, UndecidableInstances #-}
module Control.Effect.State.Internal
(
State(..)
, get
, gets
, put
, modify
, modifyLazy
, Member
) where
import Control.Effect.Carrier
import GHC.Generics (Generic1)
import Prelude hiding (fail)
data State s m k
= Get (s -> m k)
| Put s (m k)
deriving stock (Functor, Generic1)
deriving anyclass (HFunctor, Effect)
get :: (Member (State s) sig, Carrier sig m) => m s
get = send (Get pure)
{-# INLINEABLE get #-}
gets :: (Member (State s) sig, Carrier sig m) => (s -> a) -> m a
gets f = send (Get (pure . f))
{-# INLINEABLE gets #-}
put :: (Member (State s) sig, Carrier sig m) => s -> m ()
put s = send (Put s (pure ()))
{-# INLINEABLE put #-}
modify :: (Member (State s) sig, Carrier sig m) => (s -> s) -> m ()
modify f = do
a <- get
put $! f a
{-# INLINEABLE modify #-}
modifyLazy :: (Member (State s) sig, Carrier sig m) => (s -> s) -> m ()
modifyLazy f = get >>= put . f
{-# INLINEABLE modifyLazy #-}