-- | Support for access to a value of a particular type.
--
-- @since 2.7.0.0
module Effectful.Input.Static
  ( -- * Effect
    Input

    -- ** Handlers
  , runInput

    -- ** Operations
  , input
  , inputs
  ) where

import Data.Kind

import Effectful
import Effectful.Dispatch.Static

-- | Provide access to a value of type @i@.
data Input (i :: Type) :: Effect

type instance DispatchOf (Input i) = Static NoSideEffects
newtype instance StaticRep (Input i) = Input i

-- | 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 = StaticRep (Input i) -> Eff (Input i : es) a -> Eff es a
forall (e :: (Type -> Type) -> Type -> Type)
       (sideEffects :: SideEffects)
       (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, DispatchOf e ~ 'Static sideEffects,
 MaybeIOE sideEffects es) =>
StaticRep e -> Eff (e : es) a -> Eff es a
evalStaticRep (StaticRep (Input i) -> Eff (Input i : es) a -> Eff es a)
-> (i -> StaticRep (Input i))
-> i
-> Eff (Input i : es) a
-> Eff es a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. i -> StaticRep (Input i)
forall i. i -> StaticRep (Input i)
Input

-- | 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 = do
  Input i
i <- Eff es (StaticRep (Input i))
forall (e :: (Type -> Type) -> Type -> Type)
       (sideEffects :: SideEffects)
       (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, DispatchOf e ~ 'Static sideEffects, e :> es) =>
Eff es (StaticRep e)
getStaticRep
  i -> Eff es i
forall a. a -> Eff es a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure i
i

-- | 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