Skip to content

Allow "instances" for non-typeclass constraints in signature files

The contiguous library defines a typeclass for primitive arrays, with an associated constraint:

type Contiguous :: ( Type -> Type ) -> Constraint
class Contiguous arr where
  type Element arr :: Type -> Constraint
  -- ... array methods

Now, with backpack one might want to declare:

  • an array type that is an instance of Contiguous,
  • a key type which is an allowed element of the array.

Specifically:

data Key
data Array k
instance Contiguous Array
instance Element Array Key

This currently isn't allowed:

    * Illegal instance for a associated type family
      A class instance must be for a class
    * In the instance declaration for `Element Array Key'
   |
   | instance Element Array Key
   |          ^^^^^^^^^^^^^^^^^

However I think it would make sense to allow this; I don't think there's any other available mechanism to specify that any instantiation of Key and Array should satisfy the additional constraint Element Array Key. Of course it's not strictly an instance (as Element is not a typeclass, but a constraint synonym), but the alternative would be to invent backpack-specific syntax.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information