是否有可能具有类型类中未提及的变量的关联类型同义词? [英] Is it possible to have an associated type synonym with variables not mentioned in the type class?
问题描述
在 关联类型同义词 (Chakravarty,Keller,Jones)这篇文章似乎表明以下内容是有效的:
class C a where
type S a(k :: * - > *):: *
然而,我尝试运行这个我得到一个编译器错误(使用 -XTypeFamilies
):
不在范围内:类型变量`k'
我是否缺少任何东西,或者是实际的在GHC中的实现与文章中提到的不一样吗? 正如你已经发现的那样,这在GHC中是不可能的:
与关联数据声明的情况完全相同,命名的类型参数必须是子项的排列t的类参数。示例
class C abc where {type T ca :: *} - OK
class D a where {type T ax :: *} - 否:x不是类参数
class D a where {type T a :: * - > *} - OK
无法定义 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:
Exactly as in the case of an associated data declaration, the named type parameters must be a permutation of a subset of the class parameters. Examples
class C a b c where { type T c a :: * } -- OK class D a where { type T a x :: * } -- No: x is not a class parameter class D a where { type T a :: * -> * } -- OK
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.这篇关于是否有可能具有类型类中未提及的变量的关联类型同义词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!