简体   繁体   English

Haskell 类型类实例中的 Kind Constraint 是什么意思?

[英]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 类型构造函数,例如MaybeIdentity[] ,甚至部分应用的构造函数,例如Either Bool(,) String By contrast Foldable Int and Foldable [Bool] would be kind errors.相比之下, Foldable IntFoldable [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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM