Polymorphism (computer science)

Inprogramming language theoryandtype theory,polymorphismis the use of one symbol to represent multiple different types.[1]

Inobject-oriented programming,polymorphism is the provision of oneinterfaceto entities of differentdata types.[2]The concept is borrowed from a principle inbiologywhere an organism or species can have many different forms or stages.[3]

The most commonly recognized major forms of polymorphism are:

  • Ad hoc polymorphism:defines a common interface for an arbitrary set of individually specified types.
  • Parametric polymorphism:not specifying concrete types and instead use abstract symbols that can substitute for any type.
  • Subtyping(also calledsubtype polymorphismorinclusion polymorphism): when a name denotes instances of many different classes related by some common superclass.[4]

History

edit

Interest in polymorphictype systemsdeveloped significantly in the 1990s, with practical implementations beginning to appear by the end of the decade.Ad hoc polymorphismandparametric polymorphismwere originally described inChristopher Strachey'sFundamental Concepts in Programming Languages,[5]where they are listed as "the two main classes" of polymorphism. Ad hoc polymorphism was a feature ofALGOL 68,while parametric polymorphism was the core feature ofML'stype system.

In a 1985 paper,Peter WegnerandLuca Cardelliintroduced the terminclusion polymorphismto model subtypes andinheritance,[1]citingSimulaas the first programming language to implement it.

Forms

edit

Ad hoc polymorphism

edit

Christopher Stracheychose the termad hoc polymorphismto refer to polymorphic functions that can be applied to arguments of different types, but that behave differently depending on the type of the argument to which they are applied (also known asfunction overloadingoroperator overloading).[5]The term "ad hoc"in this context is not pejorative: instead, it means that this form of polymorphism is not a fundamental feature of the type system. In theJavaexample below, theAddfunctions seem to work generically over two types (integerandstring) when looking at the invocations, but are considered to be two entirely distinct functions by thecompilerfor all intents and purposes:

classAdHocPolymorphic{
publicStringadd(intx,inty){
return"Sum:"+(x+y);
}

publicStringadd(Stringname){
return"Added"+name;
}
}

publicclassadhoc{
publicstaticvoidmain(String[]args){
AdHocPolymorphicpoly=newAdHocPolymorphic();

System.out.println(poly.add(1,2));// prints "Sum: 3"
System.out.println(poly.add("Jay"));// prints "Added Jay"
}
}

Indynamically typedlanguages the situation can be more complex as the correct function that needs to be invoked might only be determinable at run time.

Implicit type conversionhas also been defined as a form of polymorphism, referred to as "coercion polymorphism".[1][6]

Parametric polymorphism

edit

Parametric polymorphismallows a function or a data type to be written generically, so that it can handle valuesuniformlywithout depending on their type.[7]Parametric polymorphism is a way to make a language more expressive while still maintaining full statictype safety.

The concept of parametric polymorphism applies to bothdata typesandfunctions.A function that can evaluate to or be applied to values of different types is known as apolymorphic function.A data type that can appear to be of a generalized type (e.g., alistwith elements of arbitrary type) is designatedpolymorphic data typelike the generalized type from which such specializations are made.

Parametric polymorphism is ubiquitous in functional programming, where it is often simply referred to as "polymorphism". The next example inHaskellshows a parameterized list data type and two parametrically polymorphic functions on them:

dataLista=Nil|Consa(Lista)

length::Lista->Integer
lengthNil=0
length(Consxxs)=1+lengthxs

map::(a->b)->Lista->Listb
mapfNil=Nil
mapf(Consxxs)=Cons(fx)(mapfxs)

Parametric polymorphism is also available in several object-oriented languages. For instance,templatesinC++andD,or under the namegenericsinC#,Delphi,Java, andGo:

classList<T>{
classNode<T>{
Telem;
Node<T>next;
}
Node<T>head;
intlength(){...}
}

List<B>map(Func<A,B>f,List<A>xs){
...
}

John C. Reynolds(and laterJean-Yves Girard) formally developed this notion of polymorphism as an extension to lambda calculus (called the polymorphic lambda calculus orSystem F). Any parametrically polymorphic function is necessarily restricted in what it can do, working on the shape of the data instead of its value, leading to the concept ofparametricity.

Subtyping

edit

Some languages employ the idea ofsubtyping(also calledsubtype polymorphismorinclusion polymorphism) to restrict the range of types that can be used in a particular case of polymorphism. In these languages, subtyping allows a function to be written to take an object of a certain typeT,but also work correctly, if passed an object that belongs to a typeSthat is a subtype ofT(according to theLiskov substitution principle). This type relation is sometimes writtenS<:T.Conversely,Tis said to be asupertypeofS,writtenT:>S.Subtype polymorphism is usually resolved dynamically (see below).

In the following Java example cats and dogs are made subtypes of pets. The procedureletsHear()accepts a pet, but will also work correctly if a subtype is passed to it:

abstractclassPet{
abstractStringspeak();
}

classCatextendsPet{
Stringspeak(){
return"Meow!";
}
}

classDogextendsPet{
Stringspeak(){
return"Woof!";
}
}

staticvoidletsHear(finalPetpet){
println(pet.speak());
}

staticvoidmain(String[]args){
letsHear(newCat());
letsHear(newDog());
}

In another example, ifNumber,Rational,andIntegerare types such thatNumber:>RationalandNumber:>Integer(RationalandIntegeras subtypes of a typeNumberthat is a supertype of them), a function written to take aNumberwill work equally well when passed anIntegerorRationalas when passed aNumber.The actual type of the object can be hidden from clients into ablack box,and accessed via objectidentity.If theNumbertype isabstract,it may not even be possible to get your hands on an object whosemost-derivedtype isNumber(seeabstract data type,abstract class). This particular kind of type hierarchy is known, especially in the context of theScheme language,as anumerical tower,and usually contains many more types.

Object-oriented programming languagesoffer subtype polymorphism usingsubclassing(also known asinheritance). In typical implementations, each class contains what is called avirtual table(shortly calledvtable) — a table of functions that implement the polymorphic part of the class interface—and each object contains a pointer to the vtable of its class, which is then consulted whenever a polymorphic method is called. This mechanism is an example of:

  • late binding,because virtual function calls are not bound until the time of invocation;
  • single dispatch(i.e., single-argument polymorphism), because virtual function calls are bound simply by looking through the vtable provided by the first argument (thethisobject), so the runtime types of the other arguments are completely irrelevant.

The same goes for most other popular object systems. Some, however, such asCommon Lisp Object System,providemultiple dispatch,under which method calls are polymorphic inallarguments.

The interaction between parametric polymorphism and subtyping leads to the concepts ofvarianceandbounded quantification.

Row polymorphism

edit

Row polymorphism[8]is a similar, but distinct concept from subtyping. It deals withstructural types.It allows the usage of all values whose types have certain properties, without losing the remaining type information.

Polytypism

edit

A related concept ispolytypism(ordata type genericity). A polytypic function is more general than polymorphic, and in such a function, "though one can provide fixed ad hoc cases for specific data types, an ad hoc combinator is absent".[9]

Rank polymorphism

edit

Rank polymorphism is one of the defining features of thearray programminglanguages, likeAPL.The essence of the rank-polymorphic programming model is implicitly treating all operations as aggregate operations, usable on arrays with arbitrarily many dimensions,[10]which is to say that rank polymorphism allows functions to be defined to operate on arrays of any shape and size.

Implementation aspects

edit

Static and dynamic polymorphism

edit

Polymorphism can be distinguished by when the implementation is selected: statically (at compile time) or dynamically (at run time, typically via avirtual function). This is known respectively asstatic dispatchanddynamic dispatch,and the corresponding forms of polymorphism are accordingly calledstatic polymorphismanddynamic polymorphism.

Static polymorphism executes faster, because there is no dynamic dispatch overhead, but requires additional compiler support. Further, static polymorphism allows greater static analysis by compilers (notably for optimization), source code analysis tools, and human readers (programmers). Dynamic polymorphism is more flexible but slower—for example, dynamic polymorphism allows duck typing, and a dynamically linked library may operate on objects without knowing their full type.

Static polymorphism typically occurs in ad hoc polymorphism and parametric polymorphism, whereas dynamic polymorphism is usual for subtype polymorphism. However, it is possible to achieve static polymorphism with subtyping through more sophisticated use oftemplate metaprogramming,namely thecuriously recurring template pattern.

When polymorphism is exposed via alibrary,static polymorphism becomes impossible fordynamic librariesas there is no way of knowing what types the parameters are when theshared objectis built. While languages like C++ and Rust usemonomorphizedtemplates, theSwift programming languagemakes extensive use of dynamic dispatch to build theapplication binary interfacefor these libraries by default. As a result, more code can be shared for a reduced system size at the cost of runtime overhead.[11]

See also

edit

References

edit
  1. ^abcCardelli, Luca;Wegner, Peter(December 1985)."On understanding types, data abstraction, and polymorphism"(PDF).ACM Computing Surveys.17(4): 471–523.CiteSeerX10.1.1.117.695.doi:10.1145/6041.6042.S2CID2921816.:"Polymorphic types are types whose operations are applicable to values of more than one type."
  2. ^Stroustrup, Bjarne(February 19, 2007)."Bjarne Stroustrup's C++ Glossary".polymorphism – providing a single interface to entities of different types.
  3. ^"Polymorphism".The Java Tutorials: Learning the Java Language: Interfaces and Inheritance.Oracle.Retrieved2021-09-08.
  4. ^Conallen, J.; Engle, M.; Houston, K.; Maksimchuk, R.; Young, B.;Booch, G.(2007).Object-Oriented Analysis and Design with Applications(3rd ed.). Pearson Education.ISBN9780132797443.
  5. ^abStrachey, Christopher(2000). "Fundamental Concepts in Programming Languages".Higher-Order and Symbolic Computation.13(1/2): 11–49.CiteSeerX10.1.1.332.3161.doi:10.1023/A:1010000313106.ISSN1573-0557.S2CID14124601.
  6. ^Tucker, Allen B. (2004).Computer Science Handbook(2nd ed.). Taylor & Francis. pp. 91–.ISBN978-1-58488-360-9.
  7. ^Pierce, B.C. (2002)."23.2 Varieties of Polymorphism".Types and Programming Languages.MIT Press. pp. 340–1.ISBN9780262162098.
  8. ^ Wand, Mitchell (June 1989). "Type inference for record concatenation and multiple inheritance".Proceedings. Fourth Annual Symposium on Logic in Computer Science.pp. 92–97.doi:10.1109/LICS.1989.39162.
  9. ^Lämmel, Ralf; Visser, Joost (2002). "Typed Combinators for Generic Traversal".Practical Aspects of Declarative Languages: 4th International Symposium.Springer. pp. 137–154, See p. 153.CiteSeerX10.1.1.18.5727.ISBN354043092X.
  10. ^Slepak, Justin; Shivers, Olin; Manolios, Panagiotis (2019). "The semantics of rank polymorphism".arXiv:1907.00509[cs.PL].
  11. ^Beingessner, Alexis."How Swift Achieved Dynamic Linking Where Rust Couldn't".
edit