-- | The dynamically dispatched variant of the 'Output' effect.
--
-- /Note:/ unless you plan to change interpretations at runtime, it's
-- recommended to use one of the statically dispatched variants,
-- i.e. "Effectful.Output.Static.Action", "Effectful.Output.Static.Local.Array",
-- "Effectful.Output.Static.Local.List", "Effectful.Output.Static.Shared.Array"
-- or "Effectful.Output.Static.Shared.List".
--
-- @since 2.7.0.0
module Effectful.Output.Dynamic
  ( -- * Effect
    Output(..)

    -- ** Handlers
  , runOutputAction
  , runOutputLocalArray
  , runOutputLocalList
  , runOutputSharedArray
  , runOutputSharedList

    -- ** Operations
  , output
  ) where

import Data.Primitive.Array

import Effectful
import Effectful.Dispatch.Dynamic
import Effectful.Output.Static.Local.Array qualified as LA
import Effectful.Output.Static.Local.List qualified as LL
import Effectful.Output.Static.Shared.Array qualified as SA
import Effectful.Output.Static.Shared.List qualified as SL

-- | Provide the ability to feed values of type @o@ to a handler.
data Output o :: Effect where
  Output :: o -> Output o m ()

type instance DispatchOf (Output o) = Dynamic

----------------------------------------
-- Handlers

-- | Run the 'Output' effect with the given action for receiving values.
runOutputAction
  :: forall o es a
   . HasCallStack
  => (HasCallStack => o -> Eff es ())
  -- ^ The action for output generation.
  -> Eff (Output o : es) a
  -> Eff es a
runOutputAction :: forall o (es :: [Effect]) a.
HasCallStack =>
(HasCallStack => o -> Eff es ())
-> Eff (Output o : es) a -> Eff es a
runOutputAction HasCallStack => o -> Eff es ()
outputAction = EffectHandler_ (Output o) es -> Eff (Output o : es) a -> Eff es a
forall (e :: Effect) (es :: [Effect]) a.
(HasCallStack, DispatchOf e ~ 'Dynamic) =>
EffectHandler_ e es -> Eff (e : es) a -> Eff es a
interpret_ (EffectHandler_ (Output o) es -> Eff (Output o : es) a -> Eff es a)
-> EffectHandler_ (Output o) es
-> Eff (Output o : es) a
-> Eff es a
forall a b. (a -> b) -> a -> b
$ \case
  Output o
o -> o -> Eff es a
o -> Eff es ()
HasCallStack => o -> Eff es ()
outputAction (o -> Eff es a) -> o -> Eff es a
forall a b. (a -> b) -> a -> b
$! o
o

-- | Run the 'Output' effect and return the final value along with the
-- accumulated array (via "Effectful.Output.Static.Local.Array").
runOutputLocalArray :: HasCallStack => Eff (Output o : es) a -> Eff es (a, Array o)
runOutputLocalArray :: forall o (es :: [Effect]) a.
HasCallStack =>
Eff (Output o : es) a -> Eff es (a, Array o)
runOutputLocalArray = (Eff (Output o : es) a -> Eff es (a, Array o))
-> (forall {a} {localEs :: [Effect]}.
    HasCallStack =>
    Output o (Eff localEs) a -> Eff (Output o : es) a)
-> Eff (Output o : es) a
-> Eff es (a, Array o)
forall (e :: Effect) (handlerEs :: [Effect]) a (es :: [Effect]) b.
(HasCallStack, DispatchOf e ~ 'Dynamic) =>
(Eff handlerEs a -> Eff es b)
-> EffectHandler_ e handlerEs -> Eff (e : es) a -> Eff es b
reinterpret_ Eff (Output o : es) a -> Eff es (a, Array o)
forall o (es :: [Effect]) a.
HasCallStack =>
Eff (Output o : es) a -> Eff es (a, Array o)
LA.runOutput ((forall {a} {localEs :: [Effect]}.
  HasCallStack =>
  Output o (Eff localEs) a -> Eff (Output o : es) a)
 -> Eff (Output o : es) a -> Eff es (a, Array o))
-> (forall {a} {localEs :: [Effect]}.
    HasCallStack =>
    Output o (Eff localEs) a -> Eff (Output o : es) a)
-> Eff (Output o : es) a
-> Eff es (a, Array o)
forall a b. (a -> b) -> a -> b
$ \case
  Output o
o -> o -> Eff (Output o : es) ()
forall o (es :: [Effect]).
(HasCallStack, Output o :> es) =>
o -> Eff es ()
LA.output o
o

-- | Run the 'Output' effect and return the final value along with the
-- accumulated list (via "Effectful.Output.Static.Local.List").
runOutputLocalList :: HasCallStack => Eff (Output o : es) a -> Eff es (a, [o])
runOutputLocalList :: forall o (es :: [Effect]) a.
HasCallStack =>
Eff (Output o : es) a -> Eff es (a, [o])
runOutputLocalList = (Eff (Output o : es) a -> Eff es (a, [o]))
-> (forall {a} {localEs :: [Effect]}.
    HasCallStack =>
    Output o (Eff localEs) a -> Eff (Output o : es) a)
-> Eff (Output o : es) a
-> Eff es (a, [o])
forall (e :: Effect) (handlerEs :: [Effect]) a (es :: [Effect]) b.
(HasCallStack, DispatchOf e ~ 'Dynamic) =>
(Eff handlerEs a -> Eff es b)
-> EffectHandler_ e handlerEs -> Eff (e : es) a -> Eff es b
reinterpret_ Eff (Output o : es) a -> Eff es (a, [o])
forall o (es :: [Effect]) a.
HasCallStack =>
Eff (Output o : es) a -> Eff es (a, [o])
LL.runOutput ((forall {a} {localEs :: [Effect]}.
  HasCallStack =>
  Output o (Eff localEs) a -> Eff (Output o : es) a)
 -> Eff (Output o : es) a -> Eff es (a, [o]))
-> (forall {a} {localEs :: [Effect]}.
    HasCallStack =>
    Output o (Eff localEs) a -> Eff (Output o : es) a)
-> Eff (Output o : es) a
-> Eff es (a, [o])
forall a b. (a -> b) -> a -> b
$ \case
  Output o
o -> o -> Eff (Output o : es) ()
forall o (es :: [Effect]).
(HasCallStack, Output o :> es) =>
o -> Eff es ()
LL.output o
o

-- | Run the 'Output' effect and return the final value along with the
-- accumulated array (via "Effectful.Output.Static.Shared.Array").
runOutputSharedArray :: HasCallStack => Eff (Output o : es) a -> Eff es (a, Array o)
runOutputSharedArray :: forall o (es :: [Effect]) a.
HasCallStack =>
Eff (Output o : es) a -> Eff es (a, Array o)
runOutputSharedArray = (Eff (Output o : es) a -> Eff es (a, Array o))
-> (forall {a} {localEs :: [Effect]}.
    HasCallStack =>
    Output o (Eff localEs) a -> Eff (Output o : es) a)
-> Eff (Output o : es) a
-> Eff es (a, Array o)
forall (e :: Effect) (handlerEs :: [Effect]) a (es :: [Effect]) b.
(HasCallStack, DispatchOf e ~ 'Dynamic) =>
(Eff handlerEs a -> Eff es b)
-> EffectHandler_ e handlerEs -> Eff (e : es) a -> Eff es b
reinterpret_ Eff (Output o : es) a -> Eff es (a, Array o)
forall o (es :: [Effect]) a.
HasCallStack =>
Eff (Output o : es) a -> Eff es (a, Array o)
SA.runOutput ((forall {a} {localEs :: [Effect]}.
  HasCallStack =>
  Output o (Eff localEs) a -> Eff (Output o : es) a)
 -> Eff (Output o : es) a -> Eff es (a, Array o))
-> (forall {a} {localEs :: [Effect]}.
    HasCallStack =>
    Output o (Eff localEs) a -> Eff (Output o : es) a)
-> Eff (Output o : es) a
-> Eff es (a, Array o)
forall a b. (a -> b) -> a -> b
$ \case
  Output o
o -> o -> Eff (Output o : es) ()
forall o (es :: [Effect]).
(HasCallStack, Output o :> es) =>
o -> Eff es ()
SA.output o
o

-- | Run the 'Output' effect and return the final value along with the
-- accumulated list (via "Effectful.Output.Static.Shared.List").
runOutputSharedList :: HasCallStack => Eff (Output o : es) a -> Eff es (a, [o])
runOutputSharedList :: forall o (es :: [Effect]) a.
HasCallStack =>
Eff (Output o : es) a -> Eff es (a, [o])
runOutputSharedList = (Eff (Output o : es) a -> Eff es (a, [o]))
-> (forall {a} {localEs :: [Effect]}.
    HasCallStack =>
    Output o (Eff localEs) a -> Eff (Output o : es) a)
-> Eff (Output o : es) a
-> Eff es (a, [o])
forall (e :: Effect) (handlerEs :: [Effect]) a (es :: [Effect]) b.
(HasCallStack, DispatchOf e ~ 'Dynamic) =>
(Eff handlerEs a -> Eff es b)
-> EffectHandler_ e handlerEs -> Eff (e : es) a -> Eff es b
reinterpret_ Eff (Output o : es) a -> Eff es (a, [o])
forall o (es :: [Effect]) a.
HasCallStack =>
Eff (Output o : es) a -> Eff es (a, [o])
SL.runOutput ((forall {a} {localEs :: [Effect]}.
  HasCallStack =>
  Output o (Eff localEs) a -> Eff (Output o : es) a)
 -> Eff (Output o : es) a -> Eff es (a, [o]))
-> (forall {a} {localEs :: [Effect]}.
    HasCallStack =>
    Output o (Eff localEs) a -> Eff (Output o : es) a)
-> Eff (Output o : es) a
-> Eff es (a, [o])
forall a b. (a -> b) -> a -> b
$ \case
  Output o
o -> o -> Eff (Output o : es) ()
forall o (es :: [Effect]).
(HasCallStack, Output o :> es) =>
o -> Eff es ()
SL.output o
o

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

-- | Feed the value to the underlying handler.
output
  :: (HasCallStack, Output o :> es)
  => o -- ^ The value.
  -> Eff es ()
output :: forall o (es :: [Effect]).
(HasCallStack, Output o :> es) =>
o -> Eff es ()
output = Output o (Eff es) () -> Eff es ()
forall (e :: Effect) (es :: [Effect]) a.
(HasCallStack, DispatchOf e ~ 'Dynamic, e :> es) =>
e (Eff es) a -> Eff es a
send (Output o (Eff es) () -> Eff es ())
-> (o -> Output o (Eff es) ()) -> o -> Eff es ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. o -> Output o (Eff es) ()
forall o (m :: Type -> Type). o -> Output o m ()
Output