Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot make a derived instance of monad transformer

I have the following newtype:

{-# LANGUAGE GeneralizedNewtypeDeriving #-}

newtype Wrap m a = Wrap {runWrap :: m a}
  deriving (Functor, Applicative, Monad, MonadTrans)

I'm trying to derive MonadTrans automatically, but I get the following error:

• Can't make a derived instance of ‘MonadTrans Wrap’
    (even with cunning GeneralizedNewtypeDeriving):
    cannot eta-reduce the representation type enough
• In the newtype declaration for ‘Wrap’

However, writing the trivial instance for MonadTrans works just fine:

instance MonadTrans Wrap where
  lift = Wrap

What is the reason for such an error message?

like image 614
Damian Nadales Avatar asked Jan 24 '17 08:01

Damian Nadales


1 Answers

GeneralizedNewtypeDeriving uses the underlying instance for a class for the implementation of the class for the newtype. However, in this case that doesn't make any sense, because m isn't of the right kind to even be an instance of MonadTrans (recall that m :: * -> *, but MonadTrans wants (* -> *) -> * -> *).

like image 106
ocharles Avatar answered Nov 01 '22 08:11

ocharles