module Effectful.Reader.Dynamic
(
Reader(..)
, runReader
, withReader
, ask
, asks
, local
) where
import Effectful
import Effectful.Dispatch.Dynamic
import qualified Effectful.Reader.Static as R
data Reader r :: Effect where
Ask :: Reader r m r
Local :: (r -> r) -> m a -> Reader r m a
type instance DispatchOf (Reader r) = Dynamic
runReader
:: r
-> Eff (Reader r : es) a
-> Eff es a
runReader :: forall r (es :: [(Type -> Type) -> Type -> Type]) a.
r -> Eff (Reader r : es) a -> Eff es a
runReader r
r = forall (e :: (Type -> Type) -> Type -> Type)
(handlerEs :: [(Type -> Type) -> Type -> Type]) a
(es :: [(Type -> Type) -> Type -> Type]) b.
(DispatchOf e ~ 'Dynamic) =>
(Eff handlerEs a -> Eff es b)
-> EffectHandler e handlerEs -> Eff (e : es) a -> Eff es b
reinterpret (forall r (es :: [(Type -> Type) -> Type -> Type]) a.
r -> Eff (Reader r : es) a -> Eff es a
R.runReader r
r) forall a b. (a -> b) -> a -> b
$ \LocalEnv localEs (Reader r : es)
env -> \case
Reader r (Eff localEs) a
Ask -> forall r (es :: [(Type -> Type) -> Type -> Type]).
(Reader r :> es) =>
Eff es r
R.ask
Local r -> r
f Eff localEs a
m -> forall (es :: [(Type -> Type) -> Type -> Type])
(handlerEs :: [(Type -> Type) -> Type -> Type])
(localEs :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, SharedSuffix es handlerEs) =>
LocalEnv localEs handlerEs
-> ((forall r. Eff localEs r -> Eff es r) -> Eff es a) -> Eff es a
localSeqUnlift LocalEnv localEs (Reader r : es)
env forall a b. (a -> b) -> a -> b
$ \forall r. Eff localEs r -> Eff (Reader r : es) r
unlift -> forall r (es :: [(Type -> Type) -> Type -> Type]) a.
(Reader r :> es) =>
(r -> r) -> Eff es a -> Eff es a
R.local r -> r
f (forall r. Eff localEs r -> Eff (Reader r : es) r
unlift Eff localEs a
m)
withReader
:: (r1 -> r2)
-> Eff (Reader r2 : es) a
-> Eff (Reader r1 : es) a
withReader :: forall r1 r2 (es :: [(Type -> Type) -> Type -> Type]) a.
(r1 -> r2) -> Eff (Reader r2 : es) a -> Eff (Reader r1 : es) a
withReader r1 -> r2
f Eff (Reader r2 : es) a
m = do
r1
r <- forall r (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, Reader r :> es) =>
Eff es r
ask
forall (es :: [(Type -> Type) -> Type -> Type]) a
(e :: (Type -> Type) -> Type -> Type).
Eff es a -> Eff (e : es) a
raise forall a b. (a -> b) -> a -> b
$ forall r (es :: [(Type -> Type) -> Type -> Type]) a.
r -> Eff (Reader r : es) a -> Eff es a
runReader (r1 -> r2
f r1
r) Eff (Reader r2 : es) a
m
ask :: (HasCallStack, Reader r :> es) => Eff es r
ask :: forall r (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, Reader r :> es) =>
Eff es r
ask = 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 forall r (m :: Type -> Type). Reader r m r
Ask
asks
:: (HasCallStack, Reader r :> es)
=> (r -> a)
-> Eff es a
asks :: forall r (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, Reader r :> es) =>
(r -> a) -> Eff es a
asks r -> a
f = r -> a
f forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, Reader r :> es) =>
Eff es r
ask
local
:: (HasCallStack, Reader r :> es)
=> (r -> r)
-> Eff es a
-> Eff es a
local :: forall r (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, Reader r :> es) =>
(r -> r) -> Eff es a -> Eff es a
local r -> r
f = 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r (m :: Type -> Type) a. (r -> r) -> m a -> Reader r m a
Local r -> r
f