以前,为了在像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
建议不起作用。(您会得到相同的错误,然后类上的每个方法都出现相同的错误)
有谁知道解决这个问题的方法吗?
解决这个问题的一个简单方法是将第二派生子句更改为:
deriving instance (Eq (A f), forall a. Ord a => Ord (f a)) => Ord (A f)
我还没有一个很好的解释为什么会发生这种情况。