尝试使用Data.Has
时,我编写了以下代码:
data Name = Name; type instance TypeOf Name = Text
type NameRecord = FieldOf Name;
我发现:
instance I NameRecord where
...
抛出编译错误,即:
非法类型同义词族在实例中应用
而:
instance (NameRecord ~ a) => I a where
...
编译正常。
我认为错误与GHC中标记为无效的此票有关。
对于该票的回复如下:
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)不失败的情况,并说明原因。
instance String ~ a => Foo a
和instance a ~ [b] => Foo a
。这是需要算法来解决Foo [Char]
的实例的一个例子。 - dflemstr