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

    -- ** Handlers
  , runInput
  , runInputAction

    -- ** Operations
  , input
  , inputs
  ) where

import Effectful
import Effectful.Dispatch.Dynamic

-- | Provide access to values of type @i@.
data Input i :: Effect where
  Input :: Input i m i

type instance DispatchOf (Input i) = Dynamic

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

-- | Run the 'Input' effect with the given value.
runInput
  :: HasCallStack
  => i
  -- ^ The input value.
  -> Eff (Input i : es) a
  -> Eff es a
runInput :: forall i (es :: [(Type -> Type) -> Type -> Type]) a.
HasCallStack =>
i -> Eff (Input i : es) a -> Eff es a
runInput i
inputValue = EffectHandler_ (Input i) es -> Eff (Input i : es) a -> Eff es a
forall (e :: (Type -> Type) -> Type -> Type)
       (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, DispatchOf e ~ 'Dynamic) =>
EffectHandler_ e es -> Eff (e : es) a -> Eff es a
interpret_ (EffectHandler_ (Input i) es -> Eff (Input i : es) a -> Eff es a)
-> EffectHandler_ (Input i) es -> Eff (Input i : es) a -> Eff es a
forall a b. (a -> b) -> a -> b
$ \case
  Input i (Eff localEs) a
Input -> a -> Eff es a
forall a. a -> Eff es a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure i
a
inputValue

-- | Run the 'Input' effect with the given action that supplies values.
runInputAction
  :: forall i es a
   . HasCallStack
  => (HasCallStack => Eff es i)
  -- ^ The action for input generation.
  -> Eff (Input i : es) a
  -> Eff es a
runInputAction :: forall i (es :: [(Type -> Type) -> Type -> Type]) a.
HasCallStack =>
(HasCallStack => Eff es i) -> Eff (Input i : es) a -> Eff es a
runInputAction HasCallStack => Eff es i
inputAction = EffectHandler_ (Input i) es -> Eff (Input i : es) a -> Eff es a
forall (e :: (Type -> Type) -> Type -> Type)
       (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, DispatchOf e ~ 'Dynamic) =>
EffectHandler_ e es -> Eff (e : es) a -> Eff es a
interpret_ (EffectHandler_ (Input i) es -> Eff (Input i : es) a -> Eff es a)
-> EffectHandler_ (Input i) es -> Eff (Input i : es) a -> Eff es a
forall a b. (a -> b) -> a -> b
$ \case
  Input i (Eff localEs) a
Input -> Eff es i
Eff es a
HasCallStack => Eff es i
inputAction

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

-- | Fetch the value.
input :: (HasCallStack, Input i :> es) => Eff es i
input :: forall i (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, Input i :> es) =>
Eff es i
input = 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 Input i (Eff es) i
forall i (m :: Type -> Type). Input i m i
Input

-- | Fetch the result of applying a function to the value.
--
-- @'inputs' f ≡ f '<$>' 'input'@
inputs
  :: (HasCallStack, Input i :> es)
  => (i -> a) -- ^ The function to apply to the value.
  -> Eff es a
inputs :: forall i (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, 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
<$> Eff es i
forall i (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, Input i :> es) =>
Eff es i
input