Home > Enterprise >  Change in Behaviour of Quantified Constraints in GHC 9
Change in Behaviour of Quantified Constraints in GHC 9

Time:09-17

Previously, in order to use quantified constraints on typeclasses like Ord, you had to include the superclass in the instance like so:

newtype A f = A (f Int)
deriving instance (forall a. Eq a => Eq (f a)) => Eq (A f)
deriving instance (forall a. Eq a => Eq (f a), forall a. Ord a => Ord (f a)) => Ord (A f)

(This is in fact precisely the solution given in this question).

In GHC 9, however, the above code doesn't work. It fails with the following error:

Could not deduce (Eq (f a))
      from the context: (forall a. Eq a => Eq (f a),
                         forall a. Ord a => Ord (f a))
        bound by a stand-alone deriving instance declaration:
                   forall (f :: * -> *).
                   (forall a. Eq a => Eq (f a), forall a. Ord a => Ord (f a)) =>
                   Ord (A f)
      or from: Eq a
        bound by a quantified context
    • In the ambiguity check for a stand-alone deriving instance declaration
      To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
      In the stand-alone deriving instance for
        ‘(forall a. Eq a => Eq (f a), forall a. Ord a => Ord (f a)) =>
         Ord (A f)’

Unfortunately the AllowAmbiguousTypes suggestion doesn't work. (you get the same error, followed by the same error for every method on the class)

Does anyone know of a workaround for this?

CodePudding user response:

One simple way to solve it is to change the second deriving clause to:

deriving instance (Eq (A f), forall a. Ord a => Ord (f a)) => Ord (A f)

I don't yet have a good explanation for why this is happening though.

  • Related