module Control.Monad.DeepReader where

import Data.DeepLenses (DeepLenses(deepLens))

import Control.Lens (over)
import qualified Control.Lens as Lens (view)
import qualified Control.Monad.Reader.Class as MR (MonadReader(ask))

class Monad m => MonadDeepReader (r :: *) (r' :: *) (m :: * -> *) | m -> r where
  ask :: m r'
  local :: (r' -> r') -> m a -> m a
  asks :: (r' -> a) -> m a

instance (Monad m, DeepLenses r r') => MonadDeepReader r r' (ReaderT r m) where
  ask :: ReaderT r m r'
ask =
    Getting r' r r' -> r -> r'
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
Lens.view Getting r' r r'
forall s s'. DeepLenses s s' => Lens' s s'
deepLens (r -> r') -> ReaderT r m r -> ReaderT r m r'
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReaderT r m r
forall r (m :: * -> *). MonadReader r m => m r
MR.ask
  local :: (r' -> r') -> ReaderT r m a -> ReaderT r m a
local r' -> r'
f ReaderT r m a
m =
    (r -> m a) -> ReaderT r m a
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((r -> m a) -> ReaderT r m a) -> (r -> m a) -> ReaderT r m a
forall a b. (a -> b) -> a -> b
$ ReaderT r m a -> r -> m a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT ReaderT r m a
m (r -> m a) -> (r -> r) -> r -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter r r r' r' -> (r' -> r') -> r -> r
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter r r r' r'
forall s s'. DeepLenses s s' => Lens' s s'
deepLens r' -> r'
f
  asks :: (r' -> a) -> ReaderT r m a
asks =
    ((r' -> a) -> ReaderT r m r' -> ReaderT r m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReaderT r m r'
forall r r' (m :: * -> *). MonadDeepReader r r' m => m r'
ask)