简体   繁体   English

类型理论:类型种类

[英]Type theory: type kinds

I've read a lot of interesting things about type kinds, higher-kinded types and so on. 我已经阅读了很多有关类型,高级类型等等的有趣内容。 By default Haskell supports two sorts of kind: 默认情况下,Haskell支持两种类型:

  • Simple type: * 简单类型: *
  • Type constructor: * → * 类型构造函数: * → *

Latest GHC's language extensions ConstraintKinds adds a new kind: 最新的GHC语言扩展ConstraintKinds增加了一种新的:

  • Type parameter constraint: Constraint 类型参数约束: Constraint

Also after reading this mailing list it becomes clear that another type of kind may exists, but it is not supported by GHC (but such support is implemented in .NET): 在阅读这个邮件列表后,很明显可能存在另一种类型,但GHC不支持它(但这种支持是在.NET中实现的):

  • Unboxed type: # 未装箱类型: #

I've learned about polymorphic kinds and I think I understand the idea. 我已经了解了多态种类 ,我想我理解这个想法。 Also Haskell supports explicitly-kinded quantification. Haskell也支持明确的kinded量化。

So my questions are: 所以我的问题是:

  • Do any other types of kinds exists? 是否存在其他类型的种类?
  • Are there any other kind-releated language features? 还有其他类似的语言功能吗?
  • What does subkinding mean? subkinding是什么意思? Where is it implemented/useful? 它在哪里实施/有用?
  • Is there a type system on top of kinds , like kinds are a type system on top of types ? 是否有顶部的类型系统kinds ,像kinds都在顶部的类型系统types (just interested) (只是感兴趣)

Yes, other kinds exist. 是的,存在其他种类。 The page Intermediate Types describes other kinds used in GHC (including both unboxed types and some more complicated kinds as well). 中间类型页面描述了GHC中使用的其他类型(包括未装箱类型和一些更复杂的类型)。 The Ωmega language takes higher-kinded types to the maximum logical extension, allowing for user-definable kinds (and sorts, and higher). Ωmega语言将更高级的类型转换为最大逻辑扩展,允许用户可定义的类型(和排序,以及更高)。 This page proposes a kind system extension for GHC which would allow for user-definable kinds in Haskell, as well as a good example of why they would be useful. 这个页面为GHC提供了一种类型的系统扩展,它允许Haskell中用户可定义的类型,以及它们为什么有用的一个很好的例子。

As a short excerpt, suppose you wanted a list type which had a type-level annotation of the length of the list, like this: 作为一个简短的摘录,假设您想要一个列表类型,它具有列表长度的类型级别注释,如下所示:

data Zero
data Succ n

data List :: * -> * -> * where
  Nil   :: List a Zero
  Cons  :: a -> List a n -> List a (Succ n)

The intention is that the last type argument should only be Zero or Succ n , where n is again only Zero or Succ n . 目的是最后一个类型参数应该只是ZeroSucc n ,其中n再次只是ZeroSucc n In short, you need to introduce a new kind, called Nat which contains only the two types Zero and Succ n . 简而言之,您需要引入一种名为Nat的新类型,它只包含ZeroSucc n两种类型。 Then the List datatype could express that the last argument is not a * , but a Nat , like 然后List数据类型可以表示最后一个参数不是* ,而是Nat ,就像

data List :: * -> Nat -> * where
  Nil   :: List a Zero
  Cons  :: a -> List a n -> List a (Succ n)

This would allow the type checker to be much more discriminative in what it accepts, as well as making type-level programming much more expressive. 这将允许类型检查器在它接受的内容中更具辨别力,以及使类型级编程更具表现力。

Just as types are classified with kinds, kinds are classified with sorts. 正如类型按类别分类一样,种类也按类别分类。

Ωmega programming language has a kind system with user definable kinds at any level. Ωmega编程语言在任何级别都有一个用户可定义种类的系统。 (So says its wiki. I think it refers to the sorts and the levels above, but I am not sure.) (所以说它的wiki。我认为它指的是上面的种类和级别,但我不确定。)

There has been a proposal to lift types into the kind level and values into the type level. 有人提议将类型提升到类型级别,将值提升到类型级别。 But I don't know if that is already implemented (or if it ever will reach "prime time") 但我不知道这是否已经实施(或者它是否会达到“黄金时间”)

Consider the following code: 请考虑以下代码:

data Z
data S a 

data Vec (a :: *) (b :: *) where
  VNil  :: Vec Z a 
  VCons :: a -> Vec l a -> Vec (S l) a 

This is a Vector that has it's dimension encoded in the type. 这是一个Vector,它的维度在类型中编码。 We are using Z and S to generate the natural number. 我们使用Z和S来生成自然数。 That's kind of nice but we cannot "type check" if we are using the right types when generating a Vec (we could accidently switch length an content type) and we also need to generate a type S and Z, which is inconvenient if we already defined the natural numbers like so: 这有点不错但我们不能“打字检查”如果我们在生成Vec时使用正确的类型(我们可能会意外地切换内容类型的长度)并且我们还需要生成类型S和Z,如果我们已经很不方便定义了这样的自然数:

data Nat = Z | S Nat

With the proposal you could write something like this: 根据提案,你可以这样写:

data Nat = Z | S Nat

data Vec (a :: Nat) (b :: *) where                                              
  VNil  :: Vec Z a
  VCons :: a -> Vec l a -> Vec (S l) a

This would lift Nat into the kind level and S and Z into the type level if needed. 如果需要,这会将Nat提升到类型级别,将S和Z提升到类型级别。 So Nat is another kind and lives on the same level as *. 所以Nat是另一种,与*一样生活在同一水平。

Here is the presentation by Brent Yorgey 以下是Brent Yorgey的演讲

Typed type-level functional programming in GHC 在GHC中键入类型级函数编程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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