OCaml中的多态性-临时,参数化,包含/子类型化 [英] Polymorphism in OCaml - ad hoc,parametric, inclusion/subtyping

查看:140
本文介绍了OCaml中的多态性-临时,参数化,包含/子类型化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解不同类型的多态性时遇到问题,特别是在OCaml方面.我了解多态性允许在OCaml中将多种类型表示为'a,但我不了解什么是不同类型的多态性.
如果有人可以用相对较低水平的语言给我一个解释,那就太好了! 临时的,参数的,包含/子类型

I am having a problem understanding the different types of polymorphism, specifically in regards to OCaml. I understand that polymorphism allows for multiple types in OCaml denoted as 'a, but I do not understand what the different types of polymorphism are.
If someone could give me an explanation with relatively low-level language that would be awesome! ad hoc, parametric, inclusion/subtyping

推荐答案

这里是一个近似值.

即席多态性通常是指能够用不同的类型(例如)声明相同的名称(通常是一个函数). SML中的+ : int -> int -> int+ : float -> float -> float.这些是不同的函数,它们可以以完全不同的方式起作用,但是编译器或解释器根据上下文选择适当的函数.我想不出OCaml中有任何特定多态性实例.但是,这在C ++和Java中很常见.

Ad-hoc polymorphism usually refers to being able to declare the same name (usually a function) with different types, e.g. + : int -> int -> int and + : float -> float -> float in SML. These are different functions, and they can act in totally different ways, but the compiler or interpreter chooses the appropriate one depending on the context. I can't think of any instances of ad-hoc polymorphism in OCaml. It is common in C++ and Java, however.

参数多态性是指单个函数由于不尝试查看该参数的结构而可以与任何类型的参数一起使用的情况.例如,cons : 'a -> 'a list -> 'a list能够将任何类型的值v放在相同类型的值列表之前,因为cons无关v的结构(布局)是什么,或者它支持什么操作.用C术语,cons不需要解除引用"指针,也不需要对v进行任何特定于v实际类型的操作.请注意,与即席多态性不同,cons 必须对所有类型都具有相同的作用方式.因此,参数多态和即席多态性彼此之间自然地相对".参数多态性是OCaml中大多数多态性实例的原因.

Parametric polymorphism is when a single function can work with an argument of any type due to not trying to look into that argument's structure. For example, cons : 'a -> 'a list -> 'a list is able to prepend a value v of any type to a list of values of the same type, because it does not matter to cons what the structure (layout) of v is, or what operations it supports. In C terms, cons does not need to "dereference" the pointer, or perform any operation on v that is specific to the actual type of v. Note that unlike in ad-hoc polymorphism, cons has to act the same way for all types. Parametric and ad-hoc polymorphism are thus in a way natural "opposites" of each other. Parametric polymorphism is responsible for the great majority of instances of polymorphism in OCaml.

子类型多态性是指您可以使用类型为t的值(其中期望类型为u的值)的情况.这可能是因为类型t支持类型u的所有操作,或者是因为可以在需要u的地方使用t的结构.例如,可以是子类化(也许可以在车辆可以使用的地方使用公共汽车),也可以是多态变体(可以在需要使用'A | 'B | 'C的地方使用'A | 'B).

Subtype polymorphism is when you can use values of type t where values of type u are expected. This could be because type t supports all the operations of type u, or because t's structure can be used where u is expected. Examples of this would be subclassing (perhaps a Bus can be used wherever a Vehicle can), or polymorphic variants (you can use 'A | 'B where 'A | 'B | 'C is expected).

按评论编辑

但是,请注意,必须在OCaml中显式请求子类型.例如,如果您具有函数f : u -> int,并且要将其应用于v : t,其中tu的子类型,则必须编写f (v :> u). (v :> u)语法是强制类型.

Note, however, that subtyping has to be requested explicitly in OCaml. If you have, for example, a function f : u -> int, and you want to apply it to v : t where t is a subtype of u, you have to write f (v :> u). The (v :> u) syntax is a type coercion.

OCaml还支持行多态,这是带有约束的参数多态的一种形式.如果f改为f : #u -> int(对于对象类型)或f : [< u] -> int(对于多态变量),则#u/[< u]语法表示类型变量,类似于'a,但只能替换与u的相应子类型"(在严格意义上说,它们可以分别支持更多的字段/更少的构造函数).然后,您可以在没有强制的情况下执行f v. OCaml会自动推断出将行多态性用于涉及多态变体和对象的许多表达式的类型,但是如果要创建签名,则必须显式地编写类型.

OCaml also supports row polymorphism, which is a form of parametric polymorphism with constraints. If f is instead f : #u -> int (for object types) or f : [< u] -> int (for polymorphic variants), the #u/[< u] syntax represents a type variable, similar to 'a, but which can only be replaced with the respective "subtypes" of u (in the restricted sense that they can support more fields/less constructors, respectively). Then, you can do f v without the coercion. OCaml automatically infers types that use row polymorphism for many expressions involving polymorphic variants and objects, but you have to write the types explicitly if you are creating a signature.

行多态性还有更多用法和注意事项.我忽略了实际的行变量和其他语法,仅描述了看起来像有限量化的内容(如Java泛型).最好保存行多态性,其名称和/或其形式主义的更详细,准确的讨论,以解决单独的问题.

There are more usages and considerations to row polymorphism. I've neglected the actual row variables and additional syntax, and only described something that looks like bounded quantification (as in Java generics). More detailed and accurate discussion of row polymorphism, its name, and/or its formalism, is perhaps best saved for separate questions.

这篇关于OCaml中的多态性-临时,参数化,包含/子类型化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆