简体   繁体   English

为什么Functor是一个更高级的类型

[英]Why Is Functor a Higher-Kinded type

I have following functor definition 我有以下仿函数定义

trait Functor[F[_]] { 
    def map[A, B](fa: F[A])(f: A => B): F[B]
}
object ListFunctor extends Functor[List] { //
    def map[A, B](f: A => B)(data: List[A]): List[B] = data map f
}

In scala, it is very common that F is a collection type, such as List,Seq,Option, I would ask why Functor has to be higher kinded type, and what does the type parameter F really mean? 在scala中,很常见的是F是一个集合类型,例如List,Seq,Option,我会问为什么Functor必须是更高的kinded类型,类型参数F真正意味着什么?

why Functor has to be higher kinded type 为什么Functor必须是更高级的kinded类型

Functor has to be higher kinded because we want to abstract over a type parameter which itself takes a type parameter (we call this a type constructor , think Functor[List] as a concrete example). Functor必须更高级,因为我们想要抽象一个类型参数 ,它本身采用一个类型参数(我们称之为类型构造函数 ,将Functor[List]视为具体示例)。

The type of types that Functor deals with are called "first order kinds", their kind is of * -> * . Functor处理的类型类型称为“一阶种类”,其类型为* -> * When you looking at concrete instances for Functor , you see we dont provide the inner type parameter. 当您查看Functor具体实例时,您会看到我们不提供内部类型参数。 For example, when you define a functor for List as you did in your example, you define it as a Functor[List] . 例如,当您为示例中的List定义仿函数时,将其定义为Functor[List] We're not creating a functor for a proper type (ie List[Int] ), but rather any type contained inside the List . 我们不是为正确的类型创建一个仿函数(即List[Int] ),而是创建List包含的任何类型。 This abstraction brings great power, because you can utilize it for any proper List type (of kind *), be it List[String] , List[Int] , etc.. 这种抽象带来了巨大的力量,因为你可以将它用于任何正确的List类型(类型*),无论是List[String]List[Int]等。

I always like to refer to the image drawn by Adriaan Moore in his paper "Genrics Of A Higher Kind": 我总是喜欢参考Adriaan Moore在他的论文“更高级的Genrics”中绘制的图像:

更高的种类

What does the type parameter F really mean 类型参数F的真正含义是什么?

F s sole purpose is to define a contract with the implementer of the Functor . F全权目的是确定用的实施者合同Functor By the signature of F we can deduce what kind of type Functor expects. 通过F的签名,我们可以推断出Functor期望的类型。 When we see that it has one "placeholder" ( [_] ) we know, by convention, that this means that F should take a single type parameter. 当我们看到它有一个“占位符”( [_] )时,按照惯例,我们知道这意味着F应该采用单个类型参数。 If we think about all the types that take a single type parameter, we can see that there are many, for example List , Option , Try , Future , Task , etc. 如果我们考虑采用单个类型参数的所有类型,我们可以看到有很多,例如ListOptionTryFutureTask等。

For a more broad explanation regarding higher kinded types, see What is a higher kinded type in Scala? 有关更高级别的kinded类型的更广泛的解释,请参阅Scala中什么是更高级的类型?

I'd answer this a bit differently. 我的回答有点不同。 The presence of a (higher-kinded) type parameter F[_] in Functor signature means Functor is a typeclass. Functor签名中存在(高级)类型参数F[_]意味着Functor是一个类型类。 Typeclasses is a Scala feature that allows one to add features to existing types without modifying them. Typeclasses是一种Scala功能,允许用户在不修改现有类型的情况下添加功能。 So Functor[F[_]] here is not a single type, but a blueprint for many other types. 所以Functor[F[_]]这里不是单一类型,而是许多其他类型的蓝图。 Or, in other words, it is not a single type, but a class of types, hence the word "typeclass". 或者,换句话说,它不是单一类型,而是一类类型,因此称为“类型类”。

Now to create a concrete functor, eg functor of list, one needs to pass that concrete type as a type parameter to that blueprint, and that concrete type itself has to have one type parameter, by mathematical definition of functor, if you wish. 现在要创建一个具体的仿函数,例如list的functor,需要将该具体类型作为类型参数传递给该蓝图,如果您愿意,该具体类型本身必须有一个类型参数,通过仿函数的数学定义。 This is what higher-kinded type F[_] in Functor signature means. 这就是Functor签名中的高级类型F[_]意味着什么。

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

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