newtype coercion wrapping status

Joachim Breitner mail at
Sat Sep 7 18:58:32 CEST 2013


Am Samstag, den 07.09.2013, 12:16 -0400 schrieb Richard Eisenberg:
> On Sep 6, 2013, at 12:47 PM, Simon Peyton-Jones <simonpj at> wrote:
> I've always lived under the impression that changing the word
> "newtype" to "data" should have exactly 0 effect on the compile-time
> behavior of Haskell programs.

I agree, that is why I raised the point.

> Yet, you're proposing that
> > module Foo (T) where   -- MkT is *not* exported
> >
> > newtype T a = MkT [a]
> is different from the same with "newtype" replaced with "data". To
> wit, in the "data" version, the instance (Coercible a b => Coercible
> (T a) (T b)) would exist, but this would not be derivable in the
> "newtype" version. Right?

In the current code, the 
        instance Coercible a b => Coercible (T a) (T b)
is available for both data and newtypes, if T’s type argument has
Representational role, but independent of any constructor presence. See
the note at
for a concise and complete list of the conditions for a Coercible

> A related issue is that, even with roles, I don't think
> GeneralizedNewtypeDeriving (GND) is in the Safe Haskell subset because
> it can break abstraction barriers -- you can use GND even when a
> newtype's constructor is not in scope. The above proposal for
> coercions will have the same problem.
> What to do? Map should certainly not export its constructor(s). Yet,
> we want (Coercible a b => Coercible (Map x a) (Map x b)). It seems
> that the writer of Map would have to explicitly export this instance.
> This goes at odds with the idea of "there aren't any instances of
> Coercible, really", but otherwise I have a hard time seeing how this
> would all work.

If I understood Simon’s last suggestion correctly than exporting a type
constructor with a non-Nominal role means “I am fine if you cast this
argument”. If this is not desired (e.g. maybe Ptr a is an example here),
then the library author has to annotate the type argument as Nominal.


Joachim Breitner
  e-Mail: mail at
  ICQ#: 74513189
  Jabber-ID: nomeata at
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <>

More information about the ghc-devs mailing list