Keywords and syntaxLanguage reference
In recent versions of GHC there is a notion of roles (see GHC Guide).
Types that have the same representation (e.g. a type and its newtype) can be converted between each other with
coerce or with
-XGeneralizedNewtypeDeriving. Roles exist to constrain ways in which types can be converted. For instance, this definition will disallow converting
Set a to
Set b even if
b is a newtype of
type role Set nominal
The reason it's not safe to convert a
Set is that the set will have a different structure depending on the
Ord instance of what's stored inside, and instances can be different for newtypes even if the representations are the same.
There are three roles:
nominal: types must be completely identical after type family reduction
representational: types must have the same representation
phantom: the type can be disregarded