我正在阅读我左边是小丑,右边是小丑,在解剖类中四处游玩,我遇到了一个覆盖条件错误。代码:
{-# 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')GHC错误消息:
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')
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^我很难理解给出的原因:我觉得p :+: q类型应该决定rhs类型,因为约束Diss p p'和Diss q q'隐含着依赖关系p -> p'和q -> q'。启用UndecidableInstances确实会删除错误,但我想了解为什么在这种情况下有必要这样做。
发布于 2018-08-09 22:13:34
对于类似的场景,GHC文件有这样的说法:
类Mul a b c\a b -> c c其中( .*. ):a -> b -> c实例Mul Int其中(.*.) = (*)实例Mul Int浮动在x.*处。Y= fromIntegral x*y实例Mul a b c => Mul a b其中x .*。V=地图(x.*)V
(应答者的注意:与您的示例一样,您可能在这里认为,在Mul a b c => Mul a [b] [c]实例中,由于a b确定了c,所以a [b]也应该清楚地确定[c]。)
第三个实例声明不符合覆盖条件;实际上(有点奇怪)定义: F=\b x y ->如果b那么x .*。你别这样 使实例推理进入循环,因为它需要约束
Mul a [b] b。
因此,覆盖范围的条件是明确地排除像你这样的情况,看起来很好,但可能不是。
https://stackoverflow.com/questions/51775968
复制相似问题