{-# LANGUAGE DeepSubsumption #-} module Foo where import Control.Monad.Reader import Control.Monad.State data Bar db :: forall m. Monad m => ReaderT Bar m Int db = undefined newtype DBR a = DBR {unDBR :: forall m. Monad m => ReaderT Bar m a} instance Functor DBR where fmap :: (a -> b) -> DBR a -> DBR b fmap f (DBR a) = DBR $ fmap f a instance Applicative DBR where pure :: a -> DBR a pure a = DBR $ pure a (<*>) :: DBR (a -> b) -> DBR a -> DBR b (<*>) (DBR f) (DBR a) = DBR $ f <*> a instance Monad DBR where (>>=) :: DBR a -> (a -> DBR b) -> DBR b (>>=) (DBR a) f = DBR $ a >>= unDBR . f data Baz works :: StateT Baz DBR Int works = lift $ DBR $ db doesnotwork :: StateT Baz DBR Int doesnotwork = lift . DBR $ db