{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeApplications #-}
-- | Labelled 'Reader' operations.
--
-- @since 1.0.2.0
module Control.Effect.Reader.Labelled
( -- * Reader effect
  Reader
, ask
, asks
, local
  -- * Re-exports
, Algebra
, Effect
, Has
, run
) where

import           Control.Effect.Labelled
import qualified Control.Effect.Reader as R
import           Control.Effect.Reader.Internal

-- | Retrieve the environment value.
--
-- @
-- runReader a ('runLabelled' @label ('ask' @label) '>>=' k) = runReader a (k a)
-- @
--
-- @since 1.0.2.0
ask :: forall label r m sig . HasLabelled label (Reader r) sig m => m r
ask :: m r
ask = UnderLabel label (Reader r) m r -> m r
forall k (label :: k) (sub :: (* -> *) -> * -> *) (m :: * -> *) a.
UnderLabel label sub m a -> m a
runUnderLabel @label UnderLabel label (Reader r) m r
forall r (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (Reader r) sig m =>
m r
R.ask
{-# INLINE ask #-}

-- | Project a function out of the current environment value.
--
-- @
-- 'asks' @label f = 'fmap' f ('ask' @label)
-- @
--
-- @since 1.0.2.0
asks :: forall label r m a sig . HasLabelled label (Reader r) sig m => (r -> a) -> m a
asks :: (r -> a) -> m a
asks f :: r -> a
f = UnderLabel label (Reader r) m a -> m a
forall k (label :: k) (sub :: (* -> *) -> * -> *) (m :: * -> *) a.
UnderLabel label sub m a -> m a
runUnderLabel @label ((r -> a) -> UnderLabel label (Reader r) m a
forall r (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has (Reader r) sig m =>
(r -> a) -> m a
R.asks r -> a
f)
{-# INLINE asks #-}

-- | Run a computation with an environment value locally modified by the passed function.
--
-- @
-- runReader a ('runLabelled' @label ('local' @label f m)) = runReader (f a) m
-- @
--
-- @since 1.0.2.0
local :: forall label r m a sig . HasLabelled label (Reader r) sig m => (r -> r) -> m a -> m a
local :: (r -> r) -> m a -> m a
local f :: r -> r
f m :: m a
m = UnderLabel label (Reader r) m a -> m a
forall k (label :: k) (sub :: (* -> *) -> * -> *) (m :: * -> *) a.
UnderLabel label sub m a -> m a
runUnderLabel @label ((r -> r)
-> UnderLabel label (Reader r) m a
-> UnderLabel label (Reader r) m a
forall r (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has (Reader r) sig m =>
(r -> r) -> m a -> m a
R.local r -> r
f (m a -> UnderLabel label (Reader r) m a
forall k (label :: k) (sub :: (* -> *) -> * -> *) (m :: * -> *) a.
m a -> UnderLabel label sub m a
UnderLabel m a
m))
{-# INLINE local #-}