什么是Scala中的高级类型?
您可以在web上找到以下内容:什么是Scala中的高级类型?,scala,generics,types,higher-kinded-types,type-constructor,Scala,Generics,Types,Higher Kinded Types,Type Constructor,您可以在web上找到以下内容: 更高种类的类型==类型构造函数? class AClass[T]{…}//例如,class List[T] 有人说这是一种更高级的类型,因为 对符合定义的类型进行抽象 高级类型是指采用其他类型并构造新类型的类型 但这些也被称为类型构造函数。(例如,在中) 更高种类的类型==将类型构造函数作为类型参数的类型构造函数? 在报纸上,你可以阅读 。。。对类型进行抽象的类型对类型进行抽象的类型 (‘高级类型’)……” 这表明 classxclass[M[T]]{…}//或
class AClass[T]{…}//例如,class List[T]
有人说这是一种更高级的类型,因为
对符合定义的类型进行抽象
高级类型是指采用其他类型并构造新类型的类型
但这些也被称为类型构造函数。(例如,在中)classxclass[M[T]]{…}//或
特征YTrait[N[]]{…}//例如特征函子[F[]]
是一种更高级的类型Int
和Char
,它们的实例是值,它们的类型是*
像Maybe
这样的构造函数是*->*
;像或者这样的二进制类型构造函数有()类*->*->*
,等等。你可以将像Maybe
和或者这样的类型看作类型级函数:它们接受一个或多个类型,并返回一个类型
如果函数的阶数大于1,则该函数为高阶函数,其阶数(非正式地)为函数箭头左侧的嵌套深度:
- 订单0:
1::Int
- 订单1:
chr::Int->Char
- 顺序2:
fix:(a->a)->a
,map:(a->b)->[a]->[b]
- 顺序3:
((A->B)->C)->D
- 顺序4:
((A->B)->C)->D)->E
因此,长话短说,更高种类的类型只是一个类型级别的高阶函数,它抽象了类型构造函数:
- 订单0:
Int:*
- 订单1:
可能::*->*
- 顺序2:
函子::(*->*)->约束
-更高级:将一元类型构造函数转换为类型类约束
我会说:在类型构造函数上有一种更高类型的抽象。
trait Functor [F[_]] {
def map[A,B] (fn: A=>B)(fa: F[A]): F[B]
}
这里的Functor
是一种“高级类型”(在中使用)。它不是像List
那样的具体(“一阶”)类型构造函数(只对适当的类型进行抽象)。它对所有一元(“一阶”)类型构造函数进行抽象(用F[\uz]
表示)
或者换一种说法:在Java中,我们有明确的类型构造函数(例如,List
),但我们没有“更高级的类型”,因为我们不能对它们进行抽象(例如,我们不能编写上面定义的Functor
接口-至少不能)
术语“高阶(类型构造函数)多态性”用于描述支持“高类类型”的系统。(此答案试图通过一些图形和历史信息来修饰Adrian Moors的答案。)
自2.5版以来,更高种类的类型是Scala的一部分
- 在Scala之前,就像Java到现在一样,
不允许使用类型构造函数
(“Java中的泛型”)用作
类型构造函数的类型参数。例如
trait Monad[M[]]
这是不可能的
在Scala 2.5中,类型系统通过分类功能进行了扩展
更高级别上的类型(称为类型构造函数多态性)
分类被称为种类
(图片来源于)
结果是,可以使用该类型构造函数(例如,List
)
就像其他类型在类型构造函数的类型参数位置一样
自Scala 2.5以来,它们成为了第一类类型(类似于Scala中作为第一类值的函数)
在支持更高种类的类型系统的上下文中,我们可以
区分适当类型、类型如Int
或List[Int]
与一阶类型如List
和更高类型的类型如函子
或单子
(对类型进行抽象而对类型进行抽象的类型)
另一方面,Java的类型系统不支持种类,因此没有类型
属于“更高的种类”
因此,这必须在支撑型系统的背景下进行观察
- 在Scala的例子中,您经常看到类型构造函数的示例,如
标题为“高级类型”,例如
这有时是错误的,因为许多人将
容器
称为更高级的类型,而不是Iterable
,但更准确的是
使用Container
作为此处更高种类(高阶)类型的类型构造函数参数Iterable
让我用一些消除歧义的方法来弥补开始时的一些困惑。我喜欢用价值层次的类比来解释这一点,因为人们往往更熟悉它
类型构造函数是可以应用于类型参数以“构造”类型的类型
值构造函数是可以应用于值参数以“构造”值的值
值构造函数通常被称为“函数”或“方法”。这些“构造函数”也被称为“多态”(因为它们可以用来构造不同“形状”的“东西”)或“抽象”(因为它们对不同多态实例化之间的差异进行抽象)
在抽象/多态的上下文中,一阶是指抽象的“单一使用”:您对一个类型抽象一次,但该类型本身不能抽象
trait Iterable[A, Container[_]]
proper first-order higher-order
values 10 (x: Int) => x (f: (Int => Int)) => f(10)
types (classes) String List Functor
types String ({type λ[x] = x})#λ ({type λ[F[x]] = F[String]})#λ
class String
class List[T]
class Functor[F[_]]
types (informally) String [x] => x [F[x]] => F[String]) // I repeat, this is not valid Scala, and might never be
scala> :help kind
:kind [-v] <type>
Displays the kind of a given type.
scala> trait Foo[A]
trait Foo
scala> trait Bar[F[_]]
trait Bar
scala> :kind -v Foo
Foo's kind is F[A]
* -> *
This is a type constructor: a 1st-order-kinded type.
scala> :kind -v Foo[Int]
Foo[Int]'s kind is A
*
This is a proper type.
scala> :kind -v Bar
Bar's kind is X[F[A]]
(* -> *) -> *
This is a type constructor that takes type constructor(s): a higher-kinded type.
scala> :kind -v Bar[Foo]
Bar[Foo]'s kind is A
*
This is a proper type.
scala> :help kind
:kind [-v] <type>
Displays the kind of a given type.
-v Displays verbose info.
"Kind" is a word used to classify types and type constructors
according to their level of abstractness.
Concrete, fully specified types such as `Int` and `Option[Int]`
are called "proper types" and denoted as `A` using Scala
notation, or with the `*` symbol.
scala> :kind Option[Int]
Option[Int]'s kind is A
In the above, `Option` is an example of a first-order type
constructor, which is denoted as `F[A]` using Scala notation, or
* -> * using the star notation. `:kind` also includes variance
information in its output, so if we ask for the kind of `Option`,
we actually see `F[+A]`:
scala> :k -v Option
Option's kind is F[+A]
* -(+)-> *
This is a type constructor: a 1st-order-kinded type.
When you have more complicated types, `:kind` can be used to find
out what you need to pass in.
scala> trait ~>[-F1[_], +F2[_]] {}
scala> :kind ~>
~>'s kind is X[-F1[A1],+F2[A2]]
This shows that `~>` accepts something of `F[A]` kind, such as
`List` or `Vector`. It's an example of a type constructor that
abstracts over type constructors, also known as a higher-order
type constructor or a higher-kinded type.