Skip to content

Allow role inference on datatype contexts

Currently, role inference on a datatype examines the use of a type parameter in all constructors of that datatype. This, of course, includes any constraint contexts on these datatypes. But, the (stupid) datatype context is not consulted, as there is no need to do so for type safety.

However, by examining the datatype context, we can create a lightweight way of annotating roles, like this:

class Nominal a
instance Nominal a
type role Nominal nominal    -- this is redundant, but here for completeness

class Representational a
instance Representational a
type role Representational representational    -- this requires -XIncoherentInstances

class Phantom a                  -- any use of this class is redundant, but here for completeness
instance Phantom a
type role Phantom phantom

data (Nominal k, Representational v) => Map k v = ...

Because of the universal instances, these constraints never get in the way. This is admittedly an abuse of existing constructs, but it seems so useful that it's worth a little abuse.

Trac metadata
Trac field Value
Version 7.8.1-rc2
Type FeatureRequest
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information