繁体   English   中英

是否可以为此树类型派生'Ord`的实现? 如果没有,为什么?

[英]Is it possible to derive an implementation for `Ord` for this tree type? And if not, why?

我已经定义了以下非常通用的树类型:

data YTree f g a = YNode (f a (g (YTree f g a)))

我已经定义了一个仿函数实例,如下所示(为了证明它可以使用):

instance (Bifunctor f, Functor g) => Functor (YTree f g) where
    fmap f (YNode m) = YNode $ bimap f (fmap (fmap f)) m

然后我成功地派生了一些实例(使用StandaloneDerivingExplicitForAllQuantifiedConstraints ):

deriving instance (Show a, forall x y. (Show x, Show y) => Show (f x y), forall z. Show z => Show (g z)) => Show (YTree f g a)
deriving instance (Read a, forall x y. (Read x, Read y) => Read (f x y), forall z. Read z => Read (g z)) => Read (YTree f g a)
deriving instance (Eq a, forall x y. (Eq x, Eq y) => Eq (f x y), forall z. Eq z => Eq (g z)) => Eq (YTree f g a)

但是,以下deriving instance子句在包含时会引发错误:

deriving instance (Ord a, forall x y. (Ord x, Ord y) => Ord (f x y), forall z. Ord z => Ord (g z)) => Ord (YTree f g a)

也就是说,

Data\Tree\Generalized.hs:32:1: error:
    * Could not deduce (Ord z)
        arising from the superclasses of an instance declaration
      from the context: (Ord a,
                         forall x y. (Ord x, Ord y) => Ord (f x y),
                         forall z. Ord z => Ord (g z))
        bound by the instance declaration
        at Data\Tree\Generalized.hs:32:1-119
      or from: Eq z
        bound by a quantified context at Data\Tree\Generalized.hs:1:1
      Possible fix: add (Ord z) to the context of a quantified context
    * In the instance declaration for `Ord (YTree f g a)'
   |
32 | deriving instance (Ord a, forall x y. (Ord x, Ord y) => Ord (f x y), forall z. Ord z => Ord (g z)) => Ord (YTree f g a)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

以及另一个错误。

据我所知,实施只是:

instance (Ord a, forall x y. (Ord x, Ord y) => Ord (f x y), forall z. Ord z => Ord (g z)) => Ord (YTree f g a) where
    compare (YNode x) (YNode y) = compare x y

但是,这会产生相同的错误。

有可能推导出这个吗? 为什么它目前不起作用? 甚至可以实施吗? 我错了吗?

deriving instance
  ( Ord a, forall x y. (Ord x, Ord y) => Ord (f x y), forall z. Ord z => Ord (g z)
  , Eq  a, forall x y. (Eq  x, Eq  y) => Eq  (f x y), forall z. Eq  z => Eq  (g z)
  ) => Ord (YTree f g a)

将编译,但我不知道为什么我们需要Eq约束。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM