4

我正在玩一种多类型的无标签编码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 SemigroupFree 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)正确的,而不是繁琐的。

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

4

1 回答 1

6

错误消息指出这些错误来自 和 的默认sconcat定义stimes。量化上下文的作用类似于instances: 在 yourinstance Semigroup (Free cls v)中,就好像有一个instance cls v => Semigroup vin 范围。instances 是通过匹配选择的。sconcatstimes想要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 只会选择它感觉使用的任何实例。

然而,这并不是一个好的情况。量化的上下文与我们的实例重叠(实际上,它与每个 Semigroup实例都重叠),这是令人震惊的。如果你尝试类似的东西(<>) = const (Free0 _etc) ([1, 2] <> [3, 4]),你会得到类似的错误,因为量化的上下文掩盖instance Semigroup [a]了库中的真实情况。我认为包括issue 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

在这里使用Implies意味着量化的上下文不再匹配Semigroup (Free cls v)通过递归释放的需要。但是,约束背后的要求并没有改变。本质上,我们为用户保留了量化约束的需求片段,这Semigroup v应该由 暗示cls v,同时在排放片段上打一个安全,以便实现,所以它不会破坏我们的约束解决方案。Implies约束仍然可以并且必须用于证明 中的约束Semigroup v,但是在显式实例用尽(<>)后,它被认为是最后的手段。Semigroup

于 2019-06-13T05:25:08.347 回答