请输入您要查询的百科知识:

 

词条 Parametric polymorphism
释义

  1. History

  2. Higher-ranked polymorphism

      Rank-1 (prenex) polymorphism    Rank-k polymorphism    Rank-n ("higher-rank") polymorphism  

  3. Predicativity and impredicativity

      Predicative polymorphism    Impredicative polymorphism  

  4. Bounded parametric polymorphism

  5. See also

  6. Notes

  7. References

{{polymorphism}}

In programming languages and type theory, parametric polymorphism is a way to make a language more expressive, while still maintaining full static type-safety. Using parametric polymorphism, a function or a data type can be written generically so that it can handle values identically without depending on their type.{{sfn|Pierce|2002}} Such functions and data types are called generic functions and generic datatypes respectively and form the basis of generic programming.

For example, a function append that joins two lists can be constructed so that it does not care about the type of elements: it can append lists of integers, lists of real numbers, lists of strings, and so on. Let the type variable a denote the type of elements in the lists. Then append can be typed

forall a. [a] × [a] -> [a]

where [a] denotes the type of lists with elements of type a. We say that the type of append is parameterized by a for all values of a. (Note that since there is only one type variable, the function cannot be applied to just any pair of lists: the pair, as well as the result list, must consist of the same type of elements.) For each place where append is applied, a value is decided for a.

Following Christopher Strachey,{{sfn|Strachey|1967}} parametric polymorphism may be contrasted with ad hoc polymorphism, in which a single polymorphic function can have a number of distinct and potentially heterogeneous implementations depending on the type of argument(s) to which it is applied. Thus, ad hoc polymorphism can generally only support a limited number of such distinct types, since a separate implementation has to be provided for each type.

History

Parametric polymorphism was first introduced to programming languages in ML in 1975.[1] Today it exists in Standard ML, OCaml, F#, Ada, Haskell, Mercury, Visual Prolog, Scala, Julia, and others. Java, C#, Visual Basic .NET and Delphi have each introduced "generics" for parametric polymorphism. Some implementations of type polymorphism are superficially similar to parametric polymorphism while also introducing ad hoc aspects. One example is C++ template specialization.

The most general form of polymorphism is "higher-rank impredicative polymorphism". Two popular restrictions of this form are restricted rank polymorphism (for example, rank-1 or prenex polymorphism) and predicative polymorphism. Together, these restrictions give "predicative prenex polymorphism", which is essentially the form of polymorphism found in ML and early versions of Haskell.

Higher-ranked polymorphism

Rank-1 (prenex) polymorphism

{{Unreferenced section|date=February 2019}}

In a prenex polymorphic system, type variables may not be instantiated with polymorphic types. This is very similar to what is called "ML-style" or "Let-polymorphism" (technically ML's Let-polymorphism has a few other syntactic restrictions).

This restriction makes the distinction between polymorphic and non-polymorphic types very important; thus in predicative systems polymorphic types are sometimes referred to as type schemas to distinguish them from ordinary (monomorphic) types, which are sometimes called monotypes. A consequence is that all types can be written in a form that places all quantifiers at the outermost (prenex) position.

For example, consider the append function described above, which has type

forall a. [a] × [a] -> [a]

In order to apply this function to a pair of lists, a type must be substituted for the variable a in the type of the function such that the type of the arguments matches up with the resulting function type. In an impredicative system, the type being substituted may be any type whatsoever, including a type that is itself polymorphic; thus append can be applied to pairs of lists with elements of any type—even to lists of polymorphic functions such as append itself.

Polymorphism in the language ML is predicative.{{citation needed|date=February 2019}} This is because predicativity, together with other restrictions, makes the type system simple enough that full type inference is always possible.

As a practical example, OCaml (a descendant or dialect of ML) performs type inference and supports impredicative polymorphism, but in some cases when impredicative polymorphism is used, the system's type inference is incomplete unless some explicit type annotations are provided by the programmer.

Rank-k polymorphism

{{expand section|date=November 2013}}

For some fixed value k, rank-k polymorphism is a system in which a quantifier may not appear to the left of k or more arrows (when the type is drawn as a tree).{{sfn|Pierce|2002}}

Type inference for rank-2 polymorphism is decidable, but reconstruction for rank-3 and above is not.{{sfn|Pierce|2002|p=359}}

Rank-n ("higher-rank") polymorphism

{{expand section|date=November 2013}}

Rank-n polymorphism is polymorphism in which quantifiers may appear to the left of arbitrarily many arrows.

Predicativity and impredicativity

Predicative polymorphism

In a predicative parametric polymorphic system, a type containing a type variable may not be used in such a way that is instantiated to a polymorphic type. Predicative type theories include Martin-Löf Type Theory and NuPRL.

Impredicative polymorphism

Impredicative polymorphism (also called first-class polymorphism) is the most powerful form of parametric polymorphism.{{sfn|Pierce|2002|p=340}} A definition is said to be impredicative if it is self-referential; in type theory this allows the instantiation of a variable in a type with any type, including polymorphic types, such as itself. An example of this is the System F with the type variable X in the type , where X could even refer to T itself.

In type theory, the most frequently studied impredicative typed λ-calculi are based on those of the lambda cube, especially System F.{{sfn|Pierce|2002}}

Bounded parametric polymorphism

{{main|Bounded quantification}}

In 1985, Luca Cardelli and Peter Wegner recognized the advantages of allowing bounds on the type parameters.{{sfn|Cardelli|Wegner|1985}} Many operations require some knowledge of the data types, but can otherwise work parametrically. For example, to check whether an item is included in a list, we need to compare the items for equality. In Standard ML, type parameters of the form ’’a are restricted so that the equality operation is available, thus the function would have the type ’’a × ’’a list → bool and ’’a can only be a type with defined equality. In Haskell, bounding is achieved by requiring types to belong to a type class; thus the same function has the type in Haskell. In most object-oriented programming languages that support parametric polymorphism, parameters can be constrained to be subtypes of a given type (see Subtype polymorphism and the article on Generic programming).

See also

  • Parametricity
  • Polymorphic recursion
  • Type class#Higher-kinded polymorphism

Notes

1. ^Milner, R., Morris, L., Newey, M. "A Logic for Computable Functions with reflexive and polymorphic types", Proc. Conference on Proving and Improving Programs, Arc-et-Senans (1975)

References

  • {{citation|first=Christopher|last=Strachey|authorlink=Christopher Strachey|year=1967|title=Fundamental Concepts in Programming Languages|type=Lecture notes|publisher=International Summer School in Computer Programming|location=Copenhagen|ref=harv|title-link=Fundamental Concepts in Programming Languages}}. Republished in: {{cite journal|journal=Higher-Order and Symbolic Computation|volume=13|pages=11–49|year=2000|doi=10.1023/A:1010000313106|last1=Strachey|first1=Christopher}}
  • {{citation

| last = Hindley | first = J. Roger
| author-link = J. Roger Hindley
| year = 1969
| title = The principal type scheme of an object in combinatory logic
| journal = Transactions of the American Mathematical Society
| volume = 146
| pages = 29–60
| doi = 10.2307/1995158
| jstor = 1995158
| mr = 0253905
| ref = harv

}}.

  • {{cite conference

| first = Jean-Yves | last = Girard | authorlink = Jean-Yves Girard
| title = Une Extension de l'Interpretation de Gödel à l'Analyse, et son Application à l'Élimination des Coupures dans l'Analyse et la Théorie des Types
| booktitle = Proceedings of the Second Scandinavian Logic Symposium
| year = 1971
| location = Amsterdam
| pages = 63–92
| language = French
| doi = 10.1016/S0049-237X(08)70843-7
| ref = harv
}}
  • {{citation

| last = Girard | first = Jean-Yves
| author-link = Jean-Yves Girard
| year = 1972
| title = Interprétation fonctionnelle et élimination des coupures de l'arithmétique d'ordre supérieur
| type = Ph.D. thesis
| publisher = Université Paris 7
| language = French
| ref = harv

}}.

  • {{citation

| last = Reynolds | first = John C.
| authorlink = John C. Reynolds
| year = 1974
| title = Towards a Theory of Type Structure
| journal = Colloque Sur la Programmation
| location = Paris
| series = Lecture Notes in Computer Science
| volume = 19
| pages = 408–425
| doi = 10.1007/3-540-06859-7_148
| ref = harv
| isbn = 978-3-540-06859-4
  • {{cite journal

| last = Milner | first = Robin
| author-link = Robin Milner
| year = 1978
| title = A Theory of Type Polymorphism in Programming
| journal = Journal of Computer and System Sciences
| volume = 17
| issue = 3
| pages = 348–375
| doi = 10.1016/0022-0000(78)90014-4
| ref = harv
}}
  • {{cite journal|last1 = Cardelli|first1=Luca|authorlink1=Luca Cardelli|last2=Wegner|first2=Peter|authorlink2=Peter Wegner|title = On Understanding Types, Data Abstraction, and Polymorphism|url=http://lucacardelli.name/Papers/OnUnderstanding.A4.pdf|journal = ACM Computing Surveys|date = December 1985| volume = 17|issue=4|issn = 0360-0300|pages = 471–523|doi = 10.1145/6041.6042| ref = harv|citeseerx=10.1.1.117.695}}
  • {{cite book|first=Benjamin C.|last=Pierce|authorlink=Benjamin C. Pierce|year=2002|title=Types and Programming Languages|publisher=MIT Press|isbn=978-0-262-16209-8 | ref = harv}}
{{Data types}}

3 : Generic programming|Polymorphism (computer science)|Type theory

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/12 8:08:36