With quantified constraints I can derive Eq (A f) just fine? However, when I try to derive Ord (A f) it fails. I do not understand how to use quantified constraints when the constraint class has a superclass. How do I derive Ord (A f) and other classes that have superclasses?
> newtype A f = A (f Int)
> deriving instance (forall a. Eq a => Eq (f a)) => Eq (A f)
> deriving instance (forall a. Ord a => Ord (f a)) => Ord (A f)
<interactive>:3:1: error:
• Could not deduce (Ord a)
arising from the superclasses of an instance declaration
from the context: forall a. Ord a => Ord (f a)
bound by the instance declaration at <interactive>:3:1-61
or from: Eq a bound by a quantified context at <interactive>:1:1
Possible fix: add (Ord a) to the context of a quantified context
• In the instance declaration for 'Ord (A f)'
PS. I have also examined ghc proposals 0109-quantified-constraints. Using ghc 8.6.5
The problem is that
Eqis a superclass ofOrd, and the constraint(forall a. Ord a => Ord (f a))does not entail the superclass constraintEq (A f)that's required to declare anOrd (A f)instance.We have
(forall a. Ord a => Ord (f a))We need
Eq (A f), i.e.,(forall a. Eq a => Eq (f a)), which is not implied by what we have.Solution: add
(forall a. Eq a => Eq (f a))to theOrdinstance.(I don't actually understand how the error message given by GHC relates to the problem.)
Or a little more tidily: