haskell - 不可判定的函数依赖
问题描述
我正在阅读左边的小丑,右边的小丑并玩解剖类,我遇到了覆盖条件错误。代码:
{-# 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 类型,因为依赖关系p -> p'
以及q -> q'
约束Diss p p'
和Diss q q'
. 启用 UndecidableInstances 确实消除了错误,但我想了解为什么在这种情况下它是必要的。
解决方案
GHC 文档对类似情况有这样的说法:
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
,它也应该是a [b]
明确确定的情况[c]
。)
三审声明不符合覆盖条件;确实是(有点奇怪)的定义:
f = \ b x y -> if b then x .*. [y] else y
使实例推理进入循环,因为它需要约束
Mul a [b] b
。
因此,覆盖条件明确排除了像您这样看起来良性但可能不是的情况。
推荐阅读
- scala - 使用 play-slick,如何使用配置生成的数据库 API 定义通用组件
- javascript - Mogno 用户和评论
- validation - Symfony 验证单个属性
- python - 我可以用 Python 运行其他程序吗?
- java - 使用字段值初始化的数组中的 ArrayIndexOutOfBoundsException
- c# - 简单文本的正则表达式模式
- python - python - 如何将两个列表中的特定元素合并到python中的新二维数组或矩阵中?
- c# - 授予对托管服务器上的 Google API 的访问权限
- netlogo - NetLogo 帮助使用函数“let”
- java - 如何识别随机id并将其称为孩子?