在尝试使用Data.Has
,我已经写了如下代码:哈斯克尔类型的家庭,认识错误消息
data Name = Name; type instance TypeOf Name = Text
type NameRecord = FieldOf Name;
我发现:
instance I NameRecord where
...
抛出一个编译错误,即:
Illegal type synonym family application in instance
鉴于:
instance (NameRecord ~ a) => I a where
...
编译好。
我相信这个错误与GHC中的this票有关,标记为无效。
售票的回应说:
I am not sure what you are suggesting. We cannot automatically transform
instance C (Fam Int) -- (1)
into
instance (Fam Int ~ famint) => C famint -- (2)
This works if there is only one instance, but as soon as there are two such instances, they always overlap.
Maybe you are suggesting that we should do it anyway and programmers should just take the implicit transformation into account. I don't think that this is a good idea. It's confusing for very little benefit (as you can always write the transformed instance yourself with little effort).
谁能细说了解释,或许与一些示例代码,其中(1)失败,但(2)没有,为什么?
出于兴趣,您是否碰巧知道GHC的解析类型类的方法是否是明确的设计决策?如果是这样,原因呢? (或者也许这个选择太复杂了,不能很好地实现?) – huon
其原因是Haskell报告要求它的行为如此。 **认为**的原因是,如果它不是这样的行为,那么就必须有一种算法给出了“类型适合约束的好坏程度”的启发式算法;你必须争辩说:“是的,这种类型适合这个类的实例,但是这个类实例更适合,因为{约束更少,缩短的距离更短,......}。”开发这样的启发式可能是可能的,但它会打破开放世界假设,这是类型类的一个关键概念。 – dflemstr
想象一下'instance String〜a => Foo a'和'instance a〜[b] => Foo a'。这是一个你需要一个算法来解析'Foo [Char]'的例子。 – dflemstr