no-role-annots-1.1: Role annotations without -XRoleAnnotations

MaintainerRichard Eisenberg (
Safe HaskellTrustworthy



This module provides three class constraints that can be used to make roles stricter in your datatypes. Here is a typical use case:

 -- Use an association list as a map:
 data (Nominal k, Representational v) => Map k v = MkMap [(k,v)]

With a declaration such as this, GHC will assign correct roles to k and v. In versions of GHC before roles, these constraints have no effect. You will need to make sure the language extension -XDatatypeContexts is enabled. This extension is enabled by default with a default language of either Haskell98 or Haskell2010, but not by default with a vanilla invocation of GHC. When enabling it manually, you may also want to specify -fno-warn-deprecated-flags, as datatype contexts are generally a misfeature.

Note that these constraints can only make roles stricter, such as a change from representational to nominal. Indeed, going the other way would not be type-safe! Thus, there is no guarantee that a parameter has the role given -- the guarantee is only that a parameter as at least the role given. (Thus, Phantom is always redundant and is included only for completeness.)

To check that the roles are what you would expect, see module Language.Haskell.RoleAnnots.Check.



class Nominal a Source

Mark a type parameter as having a nominal role


Nominal k a 

class Representational a Source

Mark a type parameter as having a representational role


class Phantom a Source

Mark a type parameter as having a phantom role. (This is always redundant.)


Phantom k a 

roleAnnot :: [Role] -> Q [Dec] -> Q [Dec]Source

Deprecated: The roleAnnot function is not necessary. Use a role constraint in a datatype context instead.

Deprecated since role inference looks at datatype contexts (with the release of GHC 7.8).

data Role Source

This declaration mirrors the declaration within Template Haskell, for use in earlier versions of GHC.