Victor Denisov
Victor Denisov

Reputation: 747

Adding MonadReader instance to a ReaderT wrapper

I'm just curious why this code doesn't compile. Why does it require m to be MonadReader ConnState?:

newtype Netbeans m a = Netbeans { unNetbeans :: ReaderT ConnState m a }
                            deriving (Monad, Functor, Applicative, MonadIO, MonadTrans)

instance Monad m => MonadReader ConnState (Netbeans m) where
    ask = Netbeans $ ask
    local f x = Netbeans $ mapReaderT (local f) (unNetbeans x)

The error message is the following(line 88 is line with local function definition):

src/Vim/Netbeans.hs:88:40:
Could not deduce (MonadReader ConnState m)
  arising from a use of `local'
from the context (Monad m)
  bound by the instance declaration at src/Vim/Netbeans.hs:86:10-54
Possible fix:
  add (MonadReader ConnState m) to the context of
    the type signature for
      local :: (ConnState -> ConnState) -> Netbeans m a -> Netbeans m a
    or the instance declaration
  or add an instance declaration for (MonadReader ConnState m)
In the first argument of `mapReaderT', namely `(local f)'
In the second argument of `($)', namely
  `mapReaderT (local f) (unNetbeans x)'
In the expression: Netbeans $ mapReaderT (local f) (unNetbeans x)

Thanks in advance.

Upvotes: 1

Views: 239

Answers (1)

Petr
Petr

Reputation: 63409

The reason is that you're applying local f to the base monad m, within the ReaderT transformer. Instead, it should look just like

local f x = Netbeans $ local f (unNetbeans x)

Upvotes: 2

Related Questions