From GHC's manual, Section Safe Language:
Module boundary control — Haskell code compiled using the safe language is guaranteed to only access symbols that are publicly available to it through other modules export lists. An important part of this is that safe compiled code is not able to examine or create data values using data constructors that it cannot import. If a module M establishes some invariants through careful use of its export list then code compiled using the safe language that imports M is guaranteed to respect those invariants. Because of this, Template Haskell and
GeneralizedNewtypeDeriving
are disabled in the safe language as they can be used to violate this property.
How can one break a module's invariants using GeneralizedNewtypeDeriving
?
Luqui linked to my blog post on the subject. Basically,
GeneralizedNewtypeDeriving
as implemented in GHC assumes that a certain kind of isomorphism (namely the operationally irrelevant isomorphism implied bynewtype
) implies leibniz equality. This was true in Haskell 98 sort of--but is not at all true in Haskell plus extensions.That is, a newtype provides a pair of functions
that don't do anything in the core, but it is not okay to conclude
because
f
might be a type function or a GADT. This is the form of equality needed forGeneralizedNewtypeDeriving
Even in Haskell 98 it breaks module boundries. You can have things like
which will do bad things...
My blog post shows how to implement
unsafeCoerce
several ways using other extensions (all safe) andGeneralizedNewtypeDeriving.
I have a better understanding of why this is now, and am much more confident thatGeneralizedNewtypeDeriving
is unable to produceunsafeCoerce
without the "System FC" style extensions (type familes, GADTs). Sill, it is unsafe, and should be used with care if at all. My understanding is that Lennart Augustsson (user augustss) implemented it very differently in hbc, and this implementation was safe. A safe implementation would be more limited, and more complicated.UPDATE: With new enough versions of GHC (all problems should be gone as of 7.8.1)
GeneralizedNewtypeDeriving
is safe because of the new roles system