为什么使用 QuantifiedConstraints 指定类型类的子类还需要子类的实例?

2023-11-23

我正在尝试多种无标签编码Free

{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TypeOperators #-}
module Free where
import GHC.Types

type (a :: k) ~> (b :: k) = Morphism k a b

newtype Natural (f :: j -> k) (g :: j -> k) = 
  Natural { getNatural :: forall (x :: j). f x ~> g x }

type family Morphism k :: k -> k -> Type where
  Morphism Type = (->)
  Morphism (j -> k) = Natural

class DataKind k where
  data Free :: (k -> Constraint) -> k -> k
  interpret :: forall (cls :: k -> Constraint) (u :: k) (v :: k). 
               cls v => (u ~> v) -> (Free cls u ~> v)
  call :: forall (cls :: k -> Constraint) (u :: k). 
          u ~> Free cls u

instance DataKind Type where
  newtype Free cls u = Free0
    { runFree0 :: forall v. cls v => (u ~> v) -> v }
  interpret f = \(Free0 g) -> g f
  call = \u -> Free0 $ \f -> f u

我可以写Semigroup的实例Free Semigroup and Free Monoid没有问题:

instance Semigroup (Free Semigroup u) where
  Free0 g <> Free0 g' = Free0 $ \f -> g f <> g' f

instance Semigroup (Free Monoid u) where
  Free0 g <> Free0 g' = Free0 $ \f -> g f <> g' f

这些实例是相同的,并且适用于任何其他子类Semigroup.

我想用QuantifiedConstraints所以我可以为所有子类编写一个实例Semigroup:

instance (forall v. cls v => Semigroup v) => Semigroup (Free cls u) where
  Free0 g <> Free0 g' = Free0 $ \f -> g f <> g' f

但编译器(GHC-8.6.3)抱怨它无法推断cls (Free cls u):

Free.hs:57:10: error:
    • Could not deduce: cls (Free cls u)
        arising from a use of ‘GHC.Base.$dmsconcat’
      from the context: forall v. cls v => Semigroup v
        bound by the instance declaration at Free.hs:57:10-67
    • In the expression: GHC.Base.$dmsconcat @(Free cls u)
      In an equation for ‘GHC.Base.sconcat’:
          GHC.Base.sconcat = GHC.Base.$dmsconcat @(Free cls u)
      In the instance declaration for ‘Semigroup (Free cls u)’
    • Relevant bindings include
        sconcat :: GHC.Base.NonEmpty (Free cls u) -> Free cls u
          (bound at Free.hs:57:10)
   |
57 | instance (forall v. cls v => Semigroup v) => Semigroup (Free cls u) where
   |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Free.hs:57:10: error:
    • Could not deduce: cls (Free cls u)
        arising from a use of ‘GHC.Base.$dmstimes’
      from the context: forall v. cls v => Semigroup v
        bound by the instance declaration at Free.hs:57:10-67
      or from: Integral b
        bound by the type signature for:
                   GHC.Base.stimes :: forall b.
                                      Integral b =>
                                      b -> Free cls u -> Free cls u
        at Free.hs:57:10-67
    • In the expression: GHC.Base.$dmstimes @(Free cls u)
      In an equation for ‘GHC.Base.stimes’:
          GHC.Base.stimes = GHC.Base.$dmstimes @(Free cls u)
      In the instance declaration for ‘Semigroup (Free cls u)’
    • Relevant bindings include
        stimes :: b -> Free cls u -> Free cls u (bound at Free.hs:57:10)
   |
57 | instance (forall v. cls v => Semigroup v) => Semigroup (Free cls u) where
   |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

当我将其添加为实例的上下文时,它可以正常编译:

instance (cls (Free cls u), forall v. cls v => Semigroup v) => Semigroup (Free cls u) where
  Free0 g <> Free0 g' = Free0 $ \f -> g f <> g' f

添加的上下文有点冗长,但由于整个要点Free就是它cls (Free cls u)总是真实的,并不繁琐。

我不明白的是whyGHC 需要能够得出结论cls (Free cls u)对于子类Semigroup为了Semigroup要编译的实例。我尝试替换的定义(<>) with undefined并得到了同样的错误,所以我认为问题不在于实现本身,而在于实例的声明;可能是由于某些方面QuantifiedConstraints我不明白。


错误消息指出这些错误来自于默认定义sconcat and stimes。量化上下文就像instances:在你的范围内instance Semigroup (Free cls v),就好像有一个instance cls v => Semigroup v在适用范围。instances 通过匹配来选择。sconcat and stimes want Semigroup (Free cls v),所以他们将想要的内容与上下文相匹配instance forall z. cls z => Semigroup z, 成功z ~ Free cls v,并进一步通缉cls (Free cls v)。即使我们也有递归,这种情况也会发生instance _etc => Semigroup (Free cls v)大约。请记住,我们假设类型类实例是一致的;无论是使用量化上下文还是使用当前定义的实例,都应该没有区别,因此 GHC 只是选择它想要使用的实例。

然而,这并不是一个好的情况。量化的背景overlaps与我们的实例(实际上,它与every Semigroup例),这是令人震惊的。如果你尝试类似的事情(<>) = const (Free0 _etc) ([1, 2] <> [3, 4]),你会得到类似的错误,因为量化的上下文掩盖了真实的上下文instance Semigroup [a]在图书馆。我认为包括一些想法问题 14877可以让这不那么不舒服:

class (a => b) => Implies a b
instance (a => b) => Implies a b
instance (forall v. cls v `Implies` Semigroup v) => Semigroup (Free cls u) where
  Free0 g <> Free0 g' = Free0 $ \f -> g f <> g' f

Using Implies这里意味着量化的上下文不再符合需要Semigroup (Free cls v)而是通过递归来释放。然而,约束背后的要求并没有改变。本质上,我们为用户保留量化约束的需求片段,即Semigroup v应该暗示cls v,同时在放电片段上施加安全性以进行实现,因此它不会扰乱我们的约束解析。这Implies约束仍然可以而且必须用来证明Semigroup v约束于(<>),但它被认为是显式之后的最后手段Semigroup实例已耗尽。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么使用 QuantifiedConstraints 指定类型类的子类还需要子类的实例? 的相关文章

随机推荐