{-# LANGUAGE AllowAmbiguousTypes #-}
-- | Convenience functions for the 'Labeled' 'Writer' effect.
--
-- @since 2.4.0.0
module Effectful.Labeled.Writer
  ( -- * Effect
    Writer(..)

    -- ** Handlers

    -- *** Local
  , runWriterLocal
  , execWriterLocal

    -- *** Shared
  , runWriterShared
  , execWriterShared

    -- * Operations
  , tell
  , listen
  , listens
  ) where

import Effectful
import Effectful.Dispatch.Dynamic
import Effectful.Labeled
import Effectful.Writer.Dynamic (Writer(..))
import Effectful.Writer.Dynamic qualified as W

----------------------------------------
-- Local

-- | Run the 'Writer' effect and return the final value along with the final
-- output (via "Effectful.Writer.Static.Local").
runWriterLocal
  :: forall label w es a
   . (HasCallStack, Monoid w)
  => Eff (Labeled label (Writer w) : es)
  a -> Eff es (a, w)
runWriterLocal :: forall {k} (label :: k) w (es :: [(Type -> Type) -> Type -> Type])
       a.
(HasCallStack, Monoid w) =>
Eff (Labeled label (Writer w) : es) a -> Eff es (a, w)
runWriterLocal = forall (label :: k) (e :: (Type -> Type) -> Type -> Type)
       (es :: [(Type -> Type) -> Type -> Type]) a b.
HasCallStack =>
(Eff (e : es) a -> Eff es b)
-> Eff (Labeled label e : es) a -> Eff es b
forall {k} (label :: k) (e :: (Type -> Type) -> Type -> Type)
       (es :: [(Type -> Type) -> Type -> Type]) a b.
HasCallStack =>
(Eff (e : es) a -> Eff es b)
-> Eff (Labeled label e : es) a -> Eff es b
runLabeled @label Eff (Writer w : es) a -> Eff es (a, w)
forall w (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, Monoid w) =>
Eff (Writer w : es) a -> Eff es (a, w)
W.runWriterLocal

-- | Run a 'Writer' effect and return the final output, discarding the final
-- value (via "Effectful.Writer.Static.Local").
execWriterLocal
  :: forall label w es a
   . (HasCallStack, Monoid w)
  => Eff (Labeled label (Writer w) : es) a
  -> Eff es w
execWriterLocal :: forall {k} (label :: k) w (es :: [(Type -> Type) -> Type -> Type])
       a.
(HasCallStack, Monoid w) =>
Eff (Labeled label (Writer w) : es) a -> Eff es w
execWriterLocal = forall (label :: k) (e :: (Type -> Type) -> Type -> Type)
       (es :: [(Type -> Type) -> Type -> Type]) a b.
HasCallStack =>
(Eff (e : es) a -> Eff es b)
-> Eff (Labeled label e : es) a -> Eff es b
forall {k} (label :: k) (e :: (Type -> Type) -> Type -> Type)
       (es :: [(Type -> Type) -> Type -> Type]) a b.
HasCallStack =>
(Eff (e : es) a -> Eff es b)
-> Eff (Labeled label e : es) a -> Eff es b
runLabeled @label Eff (Writer w : es) a -> Eff es w
forall w (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, Monoid w) =>
Eff (Writer w : es) a -> Eff es w
W.execWriterLocal

----------------------------------------
-- Shared

-- | Run the 'Writer' effect and return the final value along with the final
-- output (via "Effectful.Writer.Static.Shared").
runWriterShared
  :: forall label w es a
   . (HasCallStack, Monoid w)
  => Eff (Labeled label (Writer w) : es) a
  -> Eff es (a, w)
runWriterShared :: forall {k} (label :: k) w (es :: [(Type -> Type) -> Type -> Type])
       a.
(HasCallStack, Monoid w) =>
Eff (Labeled label (Writer w) : es) a -> Eff es (a, w)
runWriterShared = forall (label :: k) (e :: (Type -> Type) -> Type -> Type)
       (es :: [(Type -> Type) -> Type -> Type]) a b.
HasCallStack =>
(Eff (e : es) a -> Eff es b)
-> Eff (Labeled label e : es) a -> Eff es b
forall {k} (label :: k) (e :: (Type -> Type) -> Type -> Type)
       (es :: [(Type -> Type) -> Type -> Type]) a b.
HasCallStack =>
(Eff (e : es) a -> Eff es b)
-> Eff (Labeled label e : es) a -> Eff es b
runLabeled @label Eff (Writer w : es) a -> Eff es (a, w)
forall w (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, Monoid w) =>
Eff (Writer w : es) a -> Eff es (a, w)
W.runWriterShared

-- | Run the 'Writer' effect and return the final output, discarding the final
-- value (via "Effectful.Writer.Static.Shared").
execWriterShared
  :: forall label w es a
   . (HasCallStack, Monoid w)
  => Eff (Labeled label (Writer w) : es) a
  -> Eff es w
execWriterShared :: forall {k} (label :: k) w (es :: [(Type -> Type) -> Type -> Type])
       a.
(HasCallStack, Monoid w) =>
Eff (Labeled label (Writer w) : es) a -> Eff es w
execWriterShared = forall (label :: k) (e :: (Type -> Type) -> Type -> Type)
       (es :: [(Type -> Type) -> Type -> Type]) a b.
HasCallStack =>
(Eff (e : es) a -> Eff es b)
-> Eff (Labeled label e : es) a -> Eff es b
forall {k} (label :: k) (e :: (Type -> Type) -> Type -> Type)
       (es :: [(Type -> Type) -> Type -> Type]) a b.
HasCallStack =>
(Eff (e : es) a -> Eff es b)
-> Eff (Labeled label e : es) a -> Eff es b
runLabeled @label Eff (Writer w : es) a -> Eff es w
forall w (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, Monoid w) =>
Eff (Writer w : es) a -> Eff es w
W.execWriterShared

----------------------------------------
-- Operations

-- | Append the given output to the overall output of the 'Writer'.
tell
  :: forall label w es
   . (HasCallStack, Labeled label (Writer w) :> es)
  => w
  -> Eff es ()
tell :: forall {k} (label :: k) w (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, Labeled label (Writer w) :> es) =>
w -> Eff es ()
tell = Labeled label (Writer w) (Eff es) () -> Eff es ()
forall (e :: (Type -> Type) -> Type -> Type)
       (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, DispatchOf e ~ 'Dynamic, e :> es) =>
e (Eff es) a -> Eff es a
send (Labeled label (Writer w) (Eff es) () -> Eff es ())
-> (w -> Labeled label (Writer w) (Eff es) ()) -> w -> Eff es ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (label :: k) (e :: (Type -> Type) -> Type -> Type)
       (a :: Type -> Type) b.
e a b -> Labeled label e a b
forall {k} (label :: k) (e :: (Type -> Type) -> Type -> Type)
       (a :: Type -> Type) b.
e a b -> Labeled label e a b
Labeled @label (Writer w (Eff es) () -> Labeled label (Writer w) (Eff es) ())
-> (w -> Writer w (Eff es) ())
-> w
-> Labeled label (Writer w) (Eff es) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. w -> Writer w (Eff es) ()
forall w (a :: Type -> Type). w -> Writer w a ()
Tell

-- | Execute an action and append its output to the overall output of the
-- 'Writer'.
listen
  :: forall label w es a
   . (HasCallStack, Labeled label (Writer w) :> es)
  => Eff es a
  -> Eff es (a, w)
listen :: forall {k} (label :: k) w (es :: [(Type -> Type) -> Type -> Type])
       a.
(HasCallStack, Labeled label (Writer w) :> es) =>
Eff es a -> Eff es (a, w)
listen = Labeled label (Writer w) (Eff es) (a, w) -> Eff es (a, w)
forall (e :: (Type -> Type) -> Type -> Type)
       (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, DispatchOf e ~ 'Dynamic, e :> es) =>
e (Eff es) a -> Eff es a
send (Labeled label (Writer w) (Eff es) (a, w) -> Eff es (a, w))
-> (Eff es a -> Labeled label (Writer w) (Eff es) (a, w))
-> Eff es a
-> Eff es (a, w)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (label :: k) (e :: (Type -> Type) -> Type -> Type)
       (a :: Type -> Type) b.
e a b -> Labeled label e a b
forall {k} (label :: k) (e :: (Type -> Type) -> Type -> Type)
       (a :: Type -> Type) b.
e a b -> Labeled label e a b
Labeled @label (Writer w (Eff es) (a, w)
 -> Labeled label (Writer w) (Eff es) (a, w))
-> (Eff es a -> Writer w (Eff es) (a, w))
-> Eff es a
-> Labeled label (Writer w) (Eff es) (a, w)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Eff es a -> Writer w (Eff es) (a, w)
forall (a :: Type -> Type) a1 w. a a1 -> Writer w a (a1, w)
Listen

-- | Execute an action and append its output to the overall output of the
-- 'Writer', then return the final value along with a function of the recorded
-- output.
--
-- @'listens' f m ≡ 'Data.Bifunctor.second' f '<$>' 'listen' m@
listens
  :: forall label w es a b
   . (HasCallStack, Labeled label (Writer w) :> es)
  => (w -> b)
  -> Eff es a
  -> Eff es (a, b)
listens :: forall {k} (label :: k) w (es :: [(Type -> Type) -> Type -> Type])
       a b.
(HasCallStack, Labeled label (Writer w) :> es) =>
(w -> b) -> Eff es a -> Eff es (a, b)
listens w -> b
f Eff es a
m = do
  (a
a, w
w) <- forall (label :: k) w (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, Labeled label (Writer w) :> es) =>
Eff es a -> Eff es (a, w)
forall {k} (label :: k) w (es :: [(Type -> Type) -> Type -> Type])
       a.
(HasCallStack, Labeled label (Writer w) :> es) =>
Eff es a -> Eff es (a, w)
listen @label 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)