restrictions on Coercible

Richard Eisenberg eir at
Wed Nov 13 20:15:30 UTC 2013

Hi Simon, Joachim, and others,

I'm in the midst of reimplementing GeneralizedNewtypeDeriving in terms of coerce. See #8503 for why I'm doing this. But, I've run up against a limitation of Coercible I'd like to know more about. Currently, the stage2 compiler fails to build because of the problem.

In Module.lhs, there is this line:

> newtype PackageId = PId FastString deriving( Eq, Typeable )

The deriving mechanism sensibly prefers to use the GND mechanism when it can, and it can (seemingly) for Eq here. But, I get this error:

> compiler/basicTypes/Module.lhs:297:46:
>     No instance for (ghc-prim:GHC.Types.Coercible FastString PackageId)
>       because ‛PackageId’ is a recursive type constuctor

This is curious, because PackageId is manifestly *not* recursive. A little poking around tells me that any datatype mentioned in a .hs-boot file is considered recursive. There is sense to this, but the error message sure is confusing. In any case, this opens up a broader issue: we want GND to work with recursive newtypes. For example:

> class C a where
>   meth :: a
> instance C (Either a String) where
>   meth = Right ""
> newtype RecNT = MkRecNT (Either RecNT String)
>   deriving C

The above code works just fine in 7.6.3. But, if Coercible isn't allowed over recursive newtypes, then this wouldn't work if GND is implemented in terms of coerce.

So, my question is: why have this restriction? And, if there is a good reason for it, it should probably be documented somewhere. I couldn't find mention of it in the user's guide or in the haddock docs. If we do keep this restriction, what to do about GND? Seems like this may kill the idea of implementing GND in terms of coerce, but that makes me sad.


More information about the ghc-devs mailing list