[英]What Does Kind Constraint mean in Haskell typeclass instance?
When type variables are constrained by classes in Haskell, I understand what that means.当Haskell中的类约束类型变量时,我明白这意味着什么。
For example, in a function declaration,例如,在 function 声明中,
myFunction :: Foldable f => f a -> b
means that f is a type with an instance of Foldable which wraps some other type a.意味着 f 是一个具有 Foldable 实例的类型,它包装了一些其他类型 a。
But what does it mean when a type variable is constrained by a kind?但是,当类型变量受种类约束时,这意味着什么?
Consider for instance this definition for Foldable:例如考虑可折叠的这个定义:
class Foldable (t :: * -> *) where
Also, does the fact that 1 example is from a function definition and the other example is from a class definition make any difference in what the constraint means?此外,1 个示例来自 function 定义而另一个示例来自 class 定义这一事实对约束的含义有何影响?
t:: * -> *
is not a constraint, it is a kind annotation. t:: * -> *
不是约束,它是一种注解。 In this case, it is used to remark that Foldable
can take as arguments type constructors such as Maybe
, Identity
, []
, or even partially applied ones like Either Bool
and (,) String
.在这种情况下,它用于说明
Foldable
可以采用 arguments 类型构造函数,例如Maybe
、 Identity
、 []
,甚至部分应用的构造函数,例如Either Bool
和(,) String
。 By contrast Foldable Int
and Foldable [Bool]
would be kind errors.相比之下,
Foldable Int
和Foldable [Bool]
属于类错误。
t:: * -> *
can be read as " t
maps types to types". t:: * -> *
可以理解为“ t
将类型映射到类型”。
The point is, when we have Foldable f
we then use f
as in fa
, applying f
to one argument.关键是,当我们有
Foldable f
时,我们会像在fa
中一样使用f
,将f
应用于一个参数。 If we allow f = Maybe
we get Maybe a
which makes sense.如果我们允许
f = Maybe
我们得到Maybe a
这是有道理的。 If we allowed f = Int
, we would get Int a
which is meaningless.如果我们允许
f = Int
,我们将得到无意义Int a
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.