6

以前,为了对像这样的类型类使用量化约束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建议不起作用。(你得到相同的错误,然后是类上的每个方法的相同错误)

有谁知道这个的解决方法?

4

1 回答 1

3

解决它的一种简单方法是将第二个派生子句更改为:

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

我还没有很好的解释为什么会发生这种情况。

于 2021-08-27T17:11:46.843 回答