以前,为了对像这样的类型类使用量化约束Ord
,您必须在实例中包含超类,如下所示:
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)
(这实际上正是这个问题中给出的解决方案)。
然而,在 GHC 9 中,上述代码不起作用。它失败并出现以下错误:
• 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)’
不幸的是,这个AllowAmbiguousTypes
建议不起作用。(你得到相同的错误,然后是类上的每个方法的相同错误)
有谁知道这个的解决方法?