无法确定的功能依赖性

时间:2018-08-09 21:23:28

标签: haskell

我正在阅读Clowns to the left of me, Jokers to the right并在玩Dissection类,发现覆盖条件错误。代码:

\p{Pd}

GHC错误消息:

{-# LANGUAGE TypeOperators, MultiParamTypeClasses, FunctionalDependencies #-}
import Data.Bifunctor
import GHC.Generics

data Add2 p q x y = L2 (p x y) | R2 (q x y)

instance (Bifunctor p, Bifunctor q) => Bifunctor (Add2 p q) where
  bimap f g (L2 p) = L2 (bimap f g p)
  bimap f g (R2 q) = R2 (bimap f g q)

class (Functor p, Bifunctor p') => Diss p p' | p -> p' where

instance (Diss p p', Diss q q') => Diss (p :+: q) (Add2 p' q')

我正在努力理解给出的原因:我觉得类型cj.hs:13:10: error: • Illegal instance declaration for ‘Diss (p :+: q) (Add2 p' q')’ The coverage condition fails in class ‘Diss’ for functional dependency: ‘p -> p'’ Reason: lhs type ‘p :+: q’ does not determine rhs type ‘Add2 p' q'’ Un-determined variables: p', q' Using UndecidableInstances might help • In the instance declaration for ‘Diss (p :+: q) (Add2 p' q')’ | 13 | instance (Diss p p', Diss q q') => Diss (p :+: q) (Add2 p' q') | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 应该确定rhs类型,因为约束{{1}隐含了p :+: qp -> p' }}和q -> q'。启用UndecidableInstances确实可以消除该错误,但是我想了解为什么在这种情况下有必要这样做。

1 个答案:

答案 0 :(得分:2)

GHC documentation对此有类似的说法:

class Mul a b c | a b -> c where
  (.*.) :: a -> b -> c

instance Mul Int Int Int where (.*.) = (*)
instance Mul Int Float Float where x .*. y = fromIntegral x * y
instance Mul a b c => Mul a [b] [c] where x .*. v = map (x.*.) v

(答案:与您的示例一样,您可能在这里认为在Mul a b c => Mul a [b] [c]实例中,由于a b确定了c,因此{{1 }}明确确定a [b]。)

  

第三实例声明不遵守覆盖条件;确实(有点奇怪)的定义:

[c]
     

使实例推理进入循环,因为它需要约束f = \ b x y -> if b then x .*. [y] else y

因此,承保条件明确地是指排除像看起来那样良性但可能不是这样的情况。