问题描述
在
class C a where
type S a(k :: * - > *):: *
然而,我尝试运行这个我得到一个编译器错误(使用 -XTypeFamilies
):
不在范围内:类型变量`k'
我是否缺少任何东西,或者是实际的在GHC中的实现与文章中提到的不一样吗? 正如你已经发现的那样,:
无法定义 S
之类的原因。如果你这样做:
class C a where
pre>
type S a ::(* - > ; *) - > *
data TupK ak = TupK(a,ka)
实例C [a]其中
类型S [a] = TupK a
然而,现在你被困在新的数据类型中。使用类型同义词将不起作用(类型同义词`TupK'应该有两个参数),并且向
S
添加更多参数将无济于事(参数数量必须匹配家庭声明;预期1),如票证中所记录的。In Associated Type Synonyms (Chakravarty, Keller, Jones) the paper seems to indicate that the following is valid:
class C a where type S a (k :: * -> *) :: *
However, when I try and run this I get a compiler error (with
-XTypeFamilies
):Not in scope: type variable `k'
Am I missing anything, or is the actual implementation in GHC not the same as what is mentioned in the paper?
解决方案As you already found out, this is not possible in GHC:
The ticket you referred to actually explains the reason for not being able to define something like
S
. It works if you do it like this:class C a where type S a :: (* -> *) -> * data TupK a k = TupK (a, k a) instance C [a] where type S [a] = TupK a
However, now you're stuck with a new data type. Using type synonyms won't work ("Type synonym `TupK' should have 2 arguments"), and adding more parameters to
S
won't help ("Number of parameters must match family declaration; expected 1"), as documented in the ticket.这篇关于是否有可能具有类型类中未提及的变量的关联类型同义词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!