module Effectful.Writer.Static.Shared
(
Writer
, runWriter
, execWriter
, tell
, listen
, listens
) where
import Control.Exception (onException, uninterruptibleMask)
import Effectful
import Effectful.Dispatch.Static
import Effectful.Dispatch.Static.Primitive
import Effectful.Internal.Utils
data Writer w :: Effect
type instance DispatchOf (Writer w) = Static NoSideEffects
newtype instance StaticRep (Writer w) = Writer (MVar' w)
runWriter :: Monoid w => Eff (Writer w : es) a -> Eff es (a, w)
runWriter :: forall w (es :: [(Type -> Type) -> Type -> Type]) a.
Monoid w =>
Eff (Writer w : es) a -> Eff es (a, w)
runWriter Eff (Writer w : es) a
m = do
MVar' w
v <- forall a (es :: [(Type -> Type) -> Type -> Type]). IO a -> Eff es a
unsafeEff_ forall a b. (a -> b) -> a -> b
$ forall a. a -> IO (MVar' a)
newMVar' forall a. Monoid a => a
mempty
a
a <- forall (e :: (Type -> Type) -> Type -> Type)
(sideEffects :: SideEffects)
(es :: [(Type -> Type) -> Type -> Type]) a.
(DispatchOf e ~ 'Static sideEffects, MaybeIOE sideEffects es) =>
StaticRep e -> Eff (e : es) a -> Eff es a
evalStaticRep (forall w. MVar' w -> StaticRep (Writer w)
Writer MVar' w
v) Eff (Writer w : es) a
m
(a
a, ) forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a (es :: [(Type -> Type) -> Type -> Type]). IO a -> Eff es a
unsafeEff_ (forall a. MVar' a -> IO a
readMVar' MVar' w
v)
execWriter :: Monoid w => Eff (Writer w : es) a -> Eff es w
execWriter :: forall w (es :: [(Type -> Type) -> Type -> Type]) a.
Monoid w =>
Eff (Writer w : es) a -> Eff es w
execWriter Eff (Writer w : es) a
m = do
MVar' w
v <- forall a (es :: [(Type -> Type) -> Type -> Type]). IO a -> Eff es a
unsafeEff_ forall a b. (a -> b) -> a -> b
$ forall a. a -> IO (MVar' a)
newMVar' forall a. Monoid a => a
mempty
a
_ <- forall (e :: (Type -> Type) -> Type -> Type)
(sideEffects :: SideEffects)
(es :: [(Type -> Type) -> Type -> Type]) a.
(DispatchOf e ~ 'Static sideEffects, MaybeIOE sideEffects es) =>
StaticRep e -> Eff (e : es) a -> Eff es a
evalStaticRep (forall w. MVar' w -> StaticRep (Writer w)
Writer MVar' w
v) Eff (Writer w : es) a
m
forall a (es :: [(Type -> Type) -> Type -> Type]). IO a -> Eff es a
unsafeEff_ forall a b. (a -> b) -> a -> b
$ forall a. MVar' a -> IO a
readMVar' MVar' w
v
tell :: (Writer w :> es, Monoid w) => w -> Eff es ()
tell :: forall w (es :: [(Type -> Type) -> Type -> Type]).
(Writer w :> es, Monoid w) =>
w -> Eff es ()
tell w
w1 = forall (es :: [(Type -> Type) -> Type -> Type]) a.
(Env es -> IO a) -> Eff es a
unsafeEff forall a b. (a -> b) -> a -> b
$ \Env es
es -> do
Writer MVar' w
v <- forall (e :: (Type -> Type) -> Type -> Type)
(es :: [(Type -> Type) -> Type -> Type]).
(e :> es) =>
Env es -> IO (EffectRep (DispatchOf e) e)
getEnv Env es
es
forall a. MVar' a -> (a -> IO a) -> IO ()
modifyMVar_' MVar' w
v forall a b. (a -> b) -> a -> b
$ \w
w0 -> let w :: w
w = w
w0 forall a. Semigroup a => a -> a -> a
<> w
w1 in forall (f :: Type -> Type) a. Applicative f => a -> f a
pure w
w
listen :: (Writer w :> es, Monoid w) => Eff es a -> Eff es (a, w)
listen :: forall w (es :: [(Type -> Type) -> Type -> Type]) a.
(Writer w :> es, Monoid w) =>
Eff es a -> Eff es (a, w)
listen Eff es a
m = forall (es :: [(Type -> Type) -> Type -> Type]) a.
(Env es -> IO a) -> Eff es a
unsafeEff forall a b. (a -> b) -> a -> b
$ \Env es
es -> do
forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
uninterruptibleMask forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
unmask -> do
MVar' w
v1 <- forall a. a -> IO (MVar' a)
newMVar' forall a. Monoid a => a
mempty
MVar' w
v0 <- forall (e :: (Type -> Type) -> Type -> Type)
(es :: [(Type -> Type) -> Type -> Type]) a.
(e :> es) =>
Env es
-> (EffectRep (DispatchOf e) e
-> IO (a, EffectRep (DispatchOf e) e))
-> IO a
stateEnv Env es
es forall a b. (a -> b) -> a -> b
$ \(Writer MVar' w
v) -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (MVar' w
v, forall w. MVar' w -> StaticRep (Writer w)
Writer MVar' w
v1)
a
a <- forall a. IO a -> IO a
unmask (forall (es :: [(Type -> Type) -> Type -> Type]) a.
Eff es a -> Env es -> IO a
unEff Eff es a
m Env es
es) forall a b. IO a -> IO b -> IO a
`onException` forall {b} {es :: [(Type -> Type) -> Type -> Type]}.
(Writer b :> es, Semigroup b) =>
Env es -> MVar' b -> MVar' b -> IO b
merge Env es
es MVar' w
v0 MVar' w
v1
(a
a, ) forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {b} {es :: [(Type -> Type) -> Type -> Type]}.
(Writer b :> es, Semigroup b) =>
Env es -> MVar' b -> MVar' b -> IO b
merge Env es
es MVar' w
v0 MVar' w
v1
where
merge :: Env es -> MVar' b -> MVar' b -> IO b
merge Env es
es MVar' b
v0 MVar' b
v1 = do
forall (e :: (Type -> Type) -> Type -> Type)
(es :: [(Type -> Type) -> Type -> Type]).
(e :> es) =>
Env es -> EffectRep (DispatchOf e) e -> IO ()
putEnv Env es
es forall a b. (a -> b) -> a -> b
$ forall w. MVar' w -> StaticRep (Writer w)
Writer MVar' b
v0
b
w1 <- forall a. MVar' a -> IO a
readMVar' MVar' b
v1
forall a. MVar' a -> (a -> IO a) -> IO ()
modifyMVar_' MVar' b
v0 forall a b. (a -> b) -> a -> b
$ \b
w0 -> let w :: b
w = b
w0 forall a. Semigroup a => a -> a -> a
<> b
w1 in forall (f :: Type -> Type) a. Applicative f => a -> f a
pure b
w
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure b
w1
listens :: (Writer w :> es, Monoid w) => (w -> b) -> Eff es a -> Eff es (a, b)
listens :: forall w (es :: [(Type -> Type) -> Type -> Type]) b a.
(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) <- forall w (es :: [(Type -> Type) -> Type -> Type]) a.
(Writer w :> es, Monoid w) =>
Eff es a -> Eff es (a, w)
listen Eff es a
m
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (a
a, w -> b
f w
w)