本文介绍了Scala中更高级的类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以在网络上找到以下内容:


  1. 高类型==类型构造函数? strong>

    是一种更高类型的类型,因为它
    对符合定义的类型进行抽象。

    。 (例如,在)。


  2. 高类型类型==类型构造函数,它接受类型构造函数作为类型参数



    a href =http://adriaanm.github.com/files/higher.pdf>更高级的泛型,您可以阅读

    这表示

    价值构造函数通常称为函数或方法。这些构造函数也被称为多态(因为它们可以用于构造不同形状的东西)或抽象(因为它们抽象了不同的多态实例之间的变化)。



    在抽象/多态的上下文中,一阶指的是抽象的单一使用:你对一个类型抽象一次,但是该类型本身不能抽象。 Java 5泛型是一阶的。



    抽象的上述表征的一阶解释是:

    要强调的是没有涉及的抽象(我想你可以调用这个零阶,但我没有看到这个在任何地方使用),如值 1 或类型 String ,我们通常说某事是一个正确的值或类型。



    一个合适的值是立即可用,因为它不等待参数(它不抽象它们)。将它们视为可以轻松打印/检查的值(序列化函数是欺骗!)。



    一个合适的类型是对值进行分类的类型(包括值构造函数),类型构造函数不对任何值进行分类(它们首先需要应用于正确的类型参数产生适当的类型)。要实例化一个类型,有必要(但不是足够)它是一个正确的类型。 (它可能是一个抽象类,或者你不能访问的类。)



    高阶只是一个通用术语,的多态性/抽象。它对于多态类型和值意味着同样的事情。具体来说,高阶抽象抽象了抽象某物的东西。对于类型,术语更高级是更通用的更高级的特殊目的版本。



    因此,我们的特征的高阶版本变成:

    ,高阶只是意味着当你说抽象在X,你真的是意味着它!抽象的 X 不会失去自己的抽象权限:它可以抽象所有想要的。 (顺便说一下,我在这里使用动词abstract表示:省略对于值或类型的定义不是必需的东西,使得它可以由抽象的用户作为参数来改变/提供。)



    以下是一些适当的一阶和更高阶的值和类型的示例(由Lutz的电子邮件问题启发):

     正确的一阶高阶

    值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 [_]]
    pre>

    为了避免通过定义类的间接,你需要以某种方式表达匿名类型的函数,这在Scala中不能直接表达,但是你可以使用结构类型没有太多句法开销(#λ -style是由于 afaik):



    在一些支持匿名类型函数的Scala未来版本中,你可以缩短最后一行示例:

      types(非正式)String [x] => x [F [x]] => F [String])//我重复一遍,这是无效的Scala,可能永远不是

    在个人方面,我很遗憾曾经谈过更高类型的类型,它们只是类型!当你绝对需要消除歧义时,我建议说类型构造函数参数,类型构造函数成员或类型构造函数别名,强调你不是在讨论正确的类型。)



    ps:进一步复杂的事情,多态不同的方式,因为多态类型有时意味着通用量化类型,例如 Forall T,T =因为它分类多态值(在Scala中,该值可以写为结构类型 {def apply [T](x:T)),所以这是一个合适的类型。 :T = x}


    You can find the following on the web:

    1. Higher kinded type == type constructor?

      Some say this is a higher kinded type, because itabstracts over types which would be compliant with the definition.

      These though are also known as type constructor. (For example, in Programming in Scala).

    2. Higher kinded type == type constructor which takes type constructor as a type parameter?

      In the paper Generics of a Higher Kind, you can read

      which suggests that

      is a higher kinded type.

    So with this in mind, it is difficult to distinguish between type constructor, higher kinded type and type constructor which takes type constructors as type parameter, therefore the question above.

    解决方案

    Let me make up for starting some of this confusion by pitching in with some disambiguation. I like to use the analogy to the value level to explain this, as people tend to be more familiar with it.

    Value constructors are usually called "functions" or "methods". These "constructors" are also said to be "polymorphic" (because they can be used to construct "stuff" of varying "shape"), or "abstractions" (since they abstract over what varies between different polymorphic instantiations).

    In the context of abstraction/polymorphism, first-order refers to "single use" of abstraction: you abstract over a type once, but that type itself cannot abstract over anything. Java 5 generics are first-order.

    The first-order interpretation of the above characterizations of abstractions are:

    To emphasize there's no abstraction involved (I guess you could call this "zero-order", but I have not seen this used anywhere), such as the value 1 or the type String, we usually say something is a "proper" value or type.

    A proper value is "immediately usable" in the sense that it is not waiting for arguments (it does not abstract over them). Think of them as values that you can easily print/inspect (serializing a function is cheating!).

    A proper type is a type that classifies values (including value constructors), type constructors do not classify any values (they first need to be applied to the right type arguments to yield a proper type). To instantiate a type, it's necessary (but not sufficient) that it be a proper type. (It might be an abstract class, or a class that you don't have access to.)

    "Higher-order" is simply a generic term that means repeated use of polymorphism/abstraction. It means the same thing for polymorphic types and values. Concretely, a higher-order abstraction abstracts over something that abstracts over something. For types, the term "higher-kinded" is a special-purpose version of the more general "higher-order".

    Thus, the higher-order version of our characterization becomes:

    Thus, "higher-order" simply means that when you say "abstracting over X", you really mean it! The X that is abstracted over does not lose its own "abstraction rights": it can abstract all it wants. (By the way, I use the verb "abstract" here to mean: to leave out something that is not essential for the definition of a value or type, so that it can be varied/provided by the user of the abstraction as an argument.)

    Here are some examples (inspired by Lutz's questions by email) of proper, first-order and higher-order values and types:

                       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]})#λ
    

    Where the used classes were defined as:

    class String
    class List[T]
    class Functor[F[_]]
    

    To avoid the indirection through defining classes, you need to somehow express anonymous type functions, which are not expressible directly in Scala, but you can use structural types without too much syntactic overhead (the -style is due to http://stackoverflow.com/users/160378/retronym afaik):

    In some hypothetical future version of Scala that supports anonymous type functions, you could shorten that last line from the examples to:

    types (informally) String    [x] => x              [F[x]] => F[String]) // I repeat, this is not valid Scala, and might never be
    

    (On a personal note, I regret ever having talked about "higher-kinded types", they're just types after all! When you absolutely need to disambiguate, I suggest saying things like "type constructor parameter", "type constructor member", or "type constructor alias", to emphasize that you're not talking about just proper types.)

    ps: To complicate matters further, "polymorphic" is ambiguous in a different way, since a polymorphic type sometimes means a universally quantified type, such as Forall T, T => T, which is a proper type, since it classifies polymorphic values (in Scala, this value can be written as the structural type {def apply[T](x: T): T = x})

    这篇关于Scala中更高级的类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 07:09