Cannot make a derived instance of monad transformer
Asked Answered
T

1

6

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?

Tita answered 24/1, 2017 at 8:50 Comment(0)
J
9

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 (* -> *) -> * -> *).

Jerejereld answered 24/1, 2017 at 9:22 Comment(3)
What do you mean by "underlying instance for a class" in this context?Tita
Is there a way to derive trivial instances for MonadTrans?Tita
Almost a year later it seems I keep on hitting the same wall.Tita

© 2022 - 2024 — McMap. All rights reserved.