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

    -- ** Handlers
  , runInput
  , runInputAction

    -- ** Operations
  , input
  , inputs

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

import Effectful
import Effectful.Dispatch.Dynamic
import Effectful.Labeled
import Effectful.Input.Dynamic (Input(..))
import Effectful.Input.Dynamic qualified as I

-- | Run the 'Input' effect with the given value.
runInput
  :: forall label i es a
   . HasCallStack
  => i
  -- ^ The input value.
  -> Eff (Labeled label (Input i) : es) a
  -> Eff es a
runInput :: forall {k} (label :: k) i (es :: [(Type -> Type) -> Type -> Type])
       a.
HasCallStack =>
i -> Eff (Labeled label (Input i) : es) a -> Eff es a
runInput = 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 (Input i : es) a -> Eff es a)
 -> Eff (Labeled label (Input i) : es) a -> Eff es a)
-> (i -> Eff (Input i : es) a -> Eff es a)
-> i
-> Eff (Labeled label (Input i) : es) a
-> Eff es a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. i -> Eff (Input i : es) a -> Eff es a
forall i (es :: [(Type -> Type) -> Type -> Type]) a.
HasCallStack =>
i -> Eff (Input i : es) a -> Eff es a
I.runInput

-- | Run the 'Input' effect with the given action that supplies values.
runInputAction
  :: forall label i es a
   . HasCallStack
  => (HasCallStack => Eff es i)
  -- ^ The action for input generation.
  -> Eff (Labeled label (Input i) : es) a
  -> Eff es a
runInputAction :: forall {k} (label :: k) i (es :: [(Type -> Type) -> Type -> Type])
       a.
HasCallStack =>
(HasCallStack => Eff es i)
-> Eff (Labeled label (Input i) : es) a -> Eff es a
runInputAction = 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 (Input i : es) a -> Eff es a)
 -> Eff (Labeled label (Input i) : es) a -> Eff es a)
-> (Eff es i -> Eff (Input i : es) a -> Eff es a)
-> Eff es i
-> Eff (Labeled label (Input i) : es) a
-> Eff es a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Eff es i -> Eff (Input i : es) a -> Eff es a
(HasCallStack => Eff es i) -> Eff (Input i : es) a -> Eff es a
forall i (es :: [(Type -> Type) -> Type -> Type]) a.
HasCallStack =>
(HasCallStack => Eff es i) -> Eff (Input i : es) a -> Eff es a
I.runInputAction

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

-- | Fetch the value.
input
  :: forall label i es
   . (HasCallStack, Labeled label (Input i) :> es)
  => Eff es i
input :: forall {k} (label :: k) i (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, Labeled label (Input i) :> es) =>
Eff es i
input = Labeled label (Input i) (Eff es) i -> Eff es i
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 (Input i) (Eff es) i -> Eff es i)
-> Labeled label (Input i) (Eff es) i -> Eff es i
forall a b. (a -> b) -> a -> b
$ 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 Input i (Eff es) i
forall i (a :: Type -> Type). Input i a i
Input

-- | Fetch the result of applying a function to the value.
--
-- @'inputs' f ≡ f '<$>' 'input'@
inputs
  :: forall label i es a
   . (HasCallStack, Labeled label (Input i) :> es)
  => (i -> a) -- ^ The function to apply to the value.
  -> Eff es a
inputs :: forall {k} (label :: k) i (es :: [(Type -> Type) -> Type -> Type])
       a.
(HasCallStack, Labeled label (Input i) :> es) =>
(i -> a) -> Eff es a
inputs i -> a
f = i -> a
f (i -> a) -> Eff es i -> Eff es a
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (label :: k) i (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, Labeled label (Input i) :> es) =>
Eff es i
forall {k} (label :: k) i (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, Labeled label (Input i) :> es) =>
Eff es i
input @label