module Effectful.Writer.Static.Local
(
Writer
, runWriter
, execWriter
, tell
, listen
, listens
) where
import Control.Exception (onException, mask)
import Data.Kind
import Effectful
import Effectful.Dispatch.Static
import Effectful.Dispatch.Static.Primitive
data Writer (w :: Type) :: Effect
type instance DispatchOf (Writer w) = Static NoSideEffects
newtype instance StaticRep (Writer w) = Writer w
runWriter :: (HasCallStack, Monoid w) => Eff (Writer w : es) a -> Eff es (a, w)
runWriter :: forall w (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, Monoid w) =>
Eff (Writer w : es) a -> Eff es (a, w)
runWriter Eff (Writer w : es) a
m = do
(a
a, Writer w
w) <- StaticRep (Writer w)
-> Eff (Writer w : es) a -> Eff es (a, StaticRep (Writer w))
forall (e :: (Type -> Type) -> Type -> Type)
(sideEffects :: SideEffects)
(es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, DispatchOf e ~ 'Static sideEffects,
MaybeIOE sideEffects es) =>
StaticRep e -> Eff (e : es) a -> Eff es (a, StaticRep e)
runStaticRep (w -> StaticRep (Writer w)
forall w. w -> StaticRep (Writer w)
Writer w
forall a. Monoid a => a
mempty) Eff (Writer w : es) a
m
(a, w) -> Eff es (a, w)
forall a. a -> Eff es a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (a
a, w
w)
execWriter :: (HasCallStack, Monoid w) => Eff (Writer w : es) a -> Eff es w
execWriter :: forall w (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, Monoid w) =>
Eff (Writer w : es) a -> Eff es w
execWriter Eff (Writer w : es) a
m = do
Writer w
w <- StaticRep (Writer w)
-> Eff (Writer w : es) a -> Eff es (StaticRep (Writer w))
forall (e :: (Type -> Type) -> Type -> Type)
(sideEffects :: SideEffects)
(es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, DispatchOf e ~ 'Static sideEffects,
MaybeIOE sideEffects es) =>
StaticRep e -> Eff (e : es) a -> Eff es (StaticRep e)
execStaticRep (w -> StaticRep (Writer w)
forall w. w -> StaticRep (Writer w)
Writer w
forall a. Monoid a => a
mempty) Eff (Writer w : es) a
m
w -> Eff es w
forall a. a -> Eff es a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure w
w
tell :: (HasCallStack, Writer w :> es, Monoid w) => w -> Eff es ()
tell :: forall w (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, Writer w :> es, Monoid w) =>
w -> Eff es ()
tell w
w = (StaticRep (Writer w) -> ((), StaticRep (Writer w))) -> Eff es ()
forall (e :: (Type -> Type) -> Type -> Type)
(sideEffects :: SideEffects)
(es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, DispatchOf e ~ 'Static sideEffects, e :> es) =>
(StaticRep e -> (a, StaticRep e)) -> Eff es a
stateStaticRep ((StaticRep (Writer w) -> ((), StaticRep (Writer w))) -> Eff es ())
-> (StaticRep (Writer w) -> ((), StaticRep (Writer w)))
-> Eff es ()
forall a b. (a -> b) -> a -> b
$ \(Writer w
w0) -> ((), w -> StaticRep (Writer w)
forall w. w -> StaticRep (Writer w)
Writer (w
w0 w -> w -> w
forall a. Semigroup a => a -> a -> a
<> w
w))
listen :: (HasCallStack, Writer w :> es, Monoid w) => Eff es a -> Eff es (a, w)
listen :: forall w (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, Writer w :> es, Monoid w) =>
Eff es a -> Eff es (a, w)
listen Eff es a
m = (Env es -> IO (a, w)) -> Eff es (a, w)
forall (es :: [(Type -> Type) -> Type -> Type]) a.
(Env es -> IO a) -> Eff es a
unsafeEff ((Env es -> IO (a, w)) -> Eff es (a, w))
-> (Env es -> IO (a, w)) -> Eff es (a, w)
forall a b. (a -> b) -> a -> b
$ \Env es
es -> ((forall a. IO a -> IO a) -> IO (a, w)) -> IO (a, w)
forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
mask (((forall a. IO a -> IO a) -> IO (a, w)) -> IO (a, w))
-> ((forall a. IO a -> IO a) -> IO (a, w)) -> IO (a, w)
forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
unmask -> do
w
w0 <- Env es
-> (EffectRep (DispatchOf (Writer w)) (Writer w)
-> (w, EffectRep (DispatchOf (Writer w)) (Writer w)))
-> IO w
forall (e :: (Type -> Type) -> Type -> Type)
(es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, e :> es) =>
Env es
-> (EffectRep (DispatchOf e) e -> (a, EffectRep (DispatchOf e) e))
-> IO a
stateEnv Env es
es ((EffectRep (DispatchOf (Writer w)) (Writer w)
-> (w, EffectRep (DispatchOf (Writer w)) (Writer w)))
-> IO w)
-> (EffectRep (DispatchOf (Writer w)) (Writer w)
-> (w, EffectRep (DispatchOf (Writer w)) (Writer w)))
-> IO w
forall a b. (a -> b) -> a -> b
$ \(Writer w
w) -> (w
w, w -> StaticRep (Writer w)
forall w. w -> StaticRep (Writer w)
Writer w
forall a. Monoid a => a
mempty)
a
a <- IO a -> IO a
forall a. IO a -> IO a
unmask (Eff es a -> Env es -> IO a
forall (es :: [(Type -> Type) -> Type -> Type]) a.
Eff es a -> Env es -> IO a
unEff Eff es a
m Env es
es) IO a -> IO w -> IO a
forall a b. IO a -> IO b -> IO a
`onException` Env es -> w -> IO w
forall {w} {es :: [(Type -> Type) -> Type -> Type]}.
(Writer w :> es, Semigroup w) =>
Env es -> w -> IO w
merge Env es
es w
w0
(a
a, ) (w -> (a, w)) -> IO w -> IO (a, w)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Env es -> w -> IO w
forall {w} {es :: [(Type -> Type) -> Type -> Type]}.
(Writer w :> es, Semigroup w) =>
Env es -> w -> IO w
merge Env es
es w
w0
where
merge :: Env es -> w -> IO w
merge Env es
es w
w0 =
Env es
-> (EffectRep (DispatchOf (Writer w)) (Writer w)
-> (w, EffectRep (DispatchOf (Writer w)) (Writer w)))
-> IO w
forall (e :: (Type -> Type) -> Type -> Type)
(es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, e :> es) =>
Env es
-> (EffectRep (DispatchOf e) e -> (a, EffectRep (DispatchOf e) e))
-> IO a
stateEnv Env es
es ((EffectRep (DispatchOf (Writer w)) (Writer w)
-> (w, EffectRep (DispatchOf (Writer w)) (Writer w)))
-> IO w)
-> (EffectRep (DispatchOf (Writer w)) (Writer w)
-> (w, EffectRep (DispatchOf (Writer w)) (Writer w)))
-> IO w
forall a b. (a -> b) -> a -> b
$ \(Writer w
w1) -> (w
w1, w -> StaticRep (Writer w)
forall w. w -> StaticRep (Writer w)
Writer (w
w0 w -> w -> w
forall a. Semigroup a => a -> a -> a
<> w
w1))
listens
:: (HasCallStack, Writer w :> es, Monoid w)
=> (w -> b)
-> Eff es a
-> Eff es (a, b)
listens :: forall w (es :: [(Type -> Type) -> Type -> Type]) b a.
(HasCallStack, Writer w :> es, Monoid w) =>
(w -> b) -> Eff es a -> Eff es (a, b)
listens w -> b
f Eff es a
m = do
(a
a, w
w) <- Eff es a -> Eff es (a, w)
forall w (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, Writer w :> es, Monoid w) =>
Eff es a -> Eff es (a, w)
listen Eff es a
m
(a, b) -> Eff es (a, b)
forall a. a -> Eff es a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (a
a, w -> b
f w
w)