GHC 9 中量化约束的行为变化

ois*_*sdk 6 haskell quantified-constraints

以前,为了在类型类上使用量化约束,例如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)
Run Code Online (Sandbox Code Playgroud)

(这实际上正是这个问题中给出的解决方案)。

但是,在 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)’
Run Code Online (Sandbox Code Playgroud)

不幸的是,该AllowAmbiguousTypes建议不起作用。(您得到相同的错误,然后是类中每个方法的相同错误)

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

Nou*_*are 3

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

deriving instance (Eq (A f), forall a. Ord a => Ord (f a)) => Ord (A f)
Run Code Online (Sandbox Code Playgroud)

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

  • GHC 9.0“现在更忠实地实现了实例查找方案...””发行说明 https://downloads.haskell.org/~ghc/9.0.1/docs/html/users_guide/9.0.1-notes.html ——看起来和这里的例子一模一样。 (2认同)