{-# LANGUAGE AllowAmbiguousTypes #-}
-- | Convenience functions for the 'Labeled' 'Provider' effect.
--
-- @since 2.7.0.0
module Effectful.Labeled.Provider
  ( -- * Effect
    Provider(..)
  , Provider_

    -- ** Handlers
  , runProvider
  , runProvider_

    -- ** Operations
  , provide
  , provide_
  , provideWith
  , provideWith_

    -- * Re-exports
  , Labeled(..)
  ) where

import Data.Coerce
import Data.Functor.Identity

import Effectful
import Effectful.Dispatch.Dynamic
import Effectful.Labeled
import Effectful.Provider (Provider(..), Provider_)
import Effectful.Provider qualified as P

-- | Run the labeled 'Provider' effect with a given effect handler.
runProvider
  :: forall label e input f es a
   . HasCallStack
  => (forall r. HasCallStack => input -> Eff (e : es) r -> Eff es (f r))
  -- ^ The effect handler.
  -> Eff (Labeled label (Provider e input f) : es) a
  -> Eff es a
runProvider :: forall {k} (label :: k) (e :: Effect) input (f :: Type -> Type)
       (es :: [Effect]) a.
HasCallStack =>
(forall r. HasCallStack => input -> Eff (e : es) r -> Eff es (f r))
-> Eff (Labeled label (Provider e input f) : es) a -> Eff es a
runProvider forall r. HasCallStack => input -> Eff (e : es) r -> Eff es (f r)
provider = forall (label :: k) (e :: Effect) (es :: [Effect]) a b.
HasCallStack =>
(Eff (e : es) a -> Eff es b)
-> Eff (Labeled label e : es) a -> Eff es b
forall {k} (label :: k) (e :: Effect) (es :: [Effect]) a b.
HasCallStack =>
(Eff (e : es) a -> Eff es b)
-> Eff (Labeled label e : es) a -> Eff es b
runLabeled @label ((forall r. HasCallStack => input -> Eff (e : es) r -> Eff es (f r))
-> Eff (Provider e input f : es) a -> Eff es a
forall (e :: Effect) input (f :: Type -> Type) (es :: [Effect]) a.
HasCallStack =>
(forall r. HasCallStack => input -> Eff (e : es) r -> Eff es (f r))
-> Eff (Provider e input f : es) a -> Eff es a
P.runProvider input -> Eff (e : es) r -> Eff es (f r)
forall r. HasCallStack => input -> Eff (e : es) r -> Eff es (f r)
provider)

-- | Run the labeled 'Provider' effect with a given effect handler that doesn't
-- change its return type.
runProvider_
  :: forall label e input es a
   . HasCallStack
  => (forall r. HasCallStack => input -> Eff (e : es) r -> Eff es r)
  -- ^ The effect handler.
  -> Eff (Labeled label (Provider_ e input) : es) a
  -> Eff es a
runProvider_ :: forall {k} (label :: k) (e :: Effect) input (es :: [Effect]) a.
HasCallStack =>
(forall r. HasCallStack => input -> Eff (e : es) r -> Eff es r)
-> Eff (Labeled label (Provider_ e input) : es) a -> Eff es a
runProvider_ forall r. HasCallStack => input -> Eff (e : es) r -> Eff es r
provider = forall (label :: k) (e :: Effect) (es :: [Effect]) a b.
HasCallStack =>
(Eff (e : es) a -> Eff es b)
-> Eff (Labeled label e : es) a -> Eff es b
forall {k} (label :: k) (e :: Effect) (es :: [Effect]) a b.
HasCallStack =>
(Eff (e : es) a -> Eff es b)
-> Eff (Labeled label e : es) a -> Eff es b
runLabeled @label ((forall r. HasCallStack => input -> Eff (e : es) r -> Eff es r)
-> Eff (Provider_ e input : es) a -> Eff es a
forall (e :: Effect) input (es :: [Effect]) a.
HasCallStack =>
(forall r. HasCallStack => input -> Eff (e : es) r -> Eff es r)
-> Eff (Provider_ e input : es) a -> Eff es a
P.runProvider_ input -> Eff (e : es) r -> Eff es r
forall r. HasCallStack => input -> Eff (e : es) r -> Eff es r
provider)

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

-- | Run the effect handler.
provide
  :: forall label e f es a
   . (HasCallStack, Labeled label (Provider e () f) :> es)
  => Eff (e : es) a
  -> Eff es (f a)
provide :: forall {k} (label :: k) (e :: Effect) (f :: Type -> Type)
       (es :: [Effect]) a.
(HasCallStack, Labeled label (Provider e () f) :> es) =>
Eff (e : es) a -> Eff es (f a)
provide = Labeled label (Provider e () f) (Eff es) (f a) -> Eff es (f a)
forall (e :: Effect) (es :: [Effect]) a.
(HasCallStack, DispatchOf e ~ 'Dynamic, e :> es) =>
e (Eff es) a -> Eff es a
send (Labeled label (Provider e () f) (Eff es) (f a) -> Eff es (f a))
-> (Eff (e : es) a
    -> Labeled label (Provider e () f) (Eff es) (f a))
-> Eff (e : es) a
-> Eff es (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (label :: k) (e :: Effect) (a :: Type -> Type) b.
e a b -> Labeled label e a b
forall {k} (label :: k) (e :: Effect) (a :: Type -> Type) b.
e a b -> Labeled label e a b
Labeled @label (Provider e () f (Eff es) (f a)
 -> Labeled label (Provider e () f) (Eff es) (f a))
-> (Eff (e : es) a -> Provider e () f (Eff es) (f a))
-> Eff (e : es) a
-> Labeled label (Provider e () f) (Eff es) (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. () -> Eff (e : es) a -> Provider e () f (Eff es) (f a)
forall input (e :: Effect) (es :: [Effect]) a1 (f :: Type -> Type).
input -> Eff (e : es) a1 -> Provider e input f (Eff es) (f a1)
P.ProvideWith ()

-- | Run the effect handler with unchanged return type.
provide_
  :: forall label e es a
   . (HasCallStack, Labeled label (Provider_ e ()) :> es)
  => Eff (e : es) a
  -> Eff es a
provide_ :: forall {k} (label :: k) (e :: Effect) (es :: [Effect]) a.
(HasCallStack, Labeled label (Provider_ e ()) :> es) =>
Eff (e : es) a -> Eff es a
provide_ = Eff es (Identity a) -> Eff es a
forall (es :: [Effect]) a. Eff es (Identity a) -> Eff es a
dropIdentity (Eff es (Identity a) -> Eff es a)
-> (Eff (e : es) a -> Eff es (Identity a))
-> Eff (e : es) a
-> Eff es a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Labeled label (Provider_ e ()) (Eff es) (Identity a)
-> Eff es (Identity a)
forall (e :: Effect) (es :: [Effect]) a.
(HasCallStack, DispatchOf e ~ 'Dynamic, e :> es) =>
e (Eff es) a -> Eff es a
send (Labeled label (Provider_ e ()) (Eff es) (Identity a)
 -> Eff es (Identity a))
-> (Eff (e : es) a
    -> Labeled label (Provider_ e ()) (Eff es) (Identity a))
-> Eff (e : es) a
-> Eff es (Identity a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (label :: k) (e :: Effect) (a :: Type -> Type) b.
e a b -> Labeled label e a b
forall {k} (label :: k) (e :: Effect) (a :: Type -> Type) b.
e a b -> Labeled label e a b
Labeled @label (Provider e () Identity (Eff es) (Identity a)
 -> Labeled label (Provider_ e ()) (Eff es) (Identity a))
-> (Eff (e : es) a -> Provider e () Identity (Eff es) (Identity a))
-> Eff (e : es) a
-> Labeled label (Provider_ e ()) (Eff es) (Identity a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ()
-> Eff (e : es) a -> Provider e () Identity (Eff es) (Identity a)
forall input (e :: Effect) (es :: [Effect]) a1 (f :: Type -> Type).
input -> Eff (e : es) a1 -> Provider e input f (Eff es) (f a1)
P.ProvideWith ()

-- | Run the effect handler with a given input.
provideWith
  :: forall label e input f es a
   . (HasCallStack, Labeled label (Provider e input f) :> es)
  => input
  -- ^ The input to the effect handler.
  -> Eff (e : es) a
  -> Eff es (f a)
provideWith :: forall {k} (label :: k) (e :: Effect) input (f :: Type -> Type)
       (es :: [Effect]) a.
(HasCallStack, Labeled label (Provider e input f) :> es) =>
input -> Eff (e : es) a -> Eff es (f a)
provideWith input
input = Labeled label (Provider e input f) (Eff es) (f a) -> Eff es (f a)
forall (e :: Effect) (es :: [Effect]) a.
(HasCallStack, DispatchOf e ~ 'Dynamic, e :> es) =>
e (Eff es) a -> Eff es a
send (Labeled label (Provider e input f) (Eff es) (f a) -> Eff es (f a))
-> (Eff (e : es) a
    -> Labeled label (Provider e input f) (Eff es) (f a))
-> Eff (e : es) a
-> Eff es (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (label :: k) (e :: Effect) (a :: Type -> Type) b.
e a b -> Labeled label e a b
forall {k} (label :: k) (e :: Effect) (a :: Type -> Type) b.
e a b -> Labeled label e a b
Labeled @label (Provider e input f (Eff es) (f a)
 -> Labeled label (Provider e input f) (Eff es) (f a))
-> (Eff (e : es) a -> Provider e input f (Eff es) (f a))
-> Eff (e : es) a
-> Labeled label (Provider e input f) (Eff es) (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. input -> Eff (e : es) a -> Provider e input f (Eff es) (f a)
forall input (e :: Effect) (es :: [Effect]) a1 (f :: Type -> Type).
input -> Eff (e : es) a1 -> Provider e input f (Eff es) (f a1)
P.ProvideWith input
input

-- | Run the effect handler that doesn't change its return type with a given
-- input.
provideWith_
  :: forall label e input es a
   . (HasCallStack, Labeled label (Provider_ e input) :> es)
  => input
  -- ^ The input to the effect handler.
  -> Eff (e : es) a
  -> Eff es a
provideWith_ :: forall {k} (label :: k) (e :: Effect) input (es :: [Effect]) a.
(HasCallStack, Labeled label (Provider_ e input) :> es) =>
input -> Eff (e : es) a -> Eff es a
provideWith_ input
input = Eff es (Identity a) -> Eff es a
forall (es :: [Effect]) a. Eff es (Identity a) -> Eff es a
dropIdentity (Eff es (Identity a) -> Eff es a)
-> (Eff (e : es) a -> Eff es (Identity a))
-> Eff (e : es) a
-> Eff es a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Labeled label (Provider_ e input) (Eff es) (Identity a)
-> Eff es (Identity a)
forall (e :: Effect) (es :: [Effect]) a.
(HasCallStack, DispatchOf e ~ 'Dynamic, e :> es) =>
e (Eff es) a -> Eff es a
send (Labeled label (Provider_ e input) (Eff es) (Identity a)
 -> Eff es (Identity a))
-> (Eff (e : es) a
    -> Labeled label (Provider_ e input) (Eff es) (Identity a))
-> Eff (e : es) a
-> Eff es (Identity a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (label :: k) (e :: Effect) (a :: Type -> Type) b.
e a b -> Labeled label e a b
forall {k} (label :: k) (e :: Effect) (a :: Type -> Type) b.
e a b -> Labeled label e a b
Labeled @label (Provider e input Identity (Eff es) (Identity a)
 -> Labeled label (Provider_ e input) (Eff es) (Identity a))
-> (Eff (e : es) a
    -> Provider e input Identity (Eff es) (Identity a))
-> Eff (e : es) a
-> Labeled label (Provider_ e input) (Eff es) (Identity a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. input
-> Eff (e : es) a
-> Provider e input Identity (Eff es) (Identity a)
forall input (e :: Effect) (es :: [Effect]) a1 (f :: Type -> Type).
input -> Eff (e : es) a1 -> Provider e input f (Eff es) (f a1)
P.ProvideWith input
input

----------------------------------------
-- Helpers

dropIdentity :: Eff es (Identity a) -> Eff es a
dropIdentity :: forall (es :: [Effect]) a. Eff es (Identity a) -> Eff es a
dropIdentity = Eff es (Identity a) -> Eff es a
forall a b. Coercible a b => a -> b
coerce