FACTOID # 157: People trust Swedes! Swedish companies are the world’s least-likely to be perceived as paying bribes.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

FACTS & STATISTICS    Simple view

  1. Select countries to view: (hold down Control key and click to select several)

     

     

    Compare:

     

     

  1. Select fact or statistic: (* = graphable)

     

     

     

  2. (OPTIONAL) Compare to statistic: (both need to be graphable)

     

     

     

  3. View result as:

     

       
(OR) SEARCH ALL encyclopedia, stats & forums:   

Encyclopedia > Polymorphism (computer science)

In computer science, polymorphism means allowing a single definition to be used with different types of data (specifically, different classes of objects). For instance, a polymorphic function definition can replace several type-specific ones, and a single polymorphic operator can act in expressions of various types. Many programming languages and paradigms implement some forms of polymorphism; for a popular example, see polymorphism in object-oriented programming. Computer science is the study of the theoretical foundations of information and computation and their implementation and application in computer systems. ... On computer science, a datatype (often simply type) is a name or label for a set of values and some operations which can be performed on that set of values. ... In object-oriented programming, classes are used to group related variables and functions. ... In strictly mathematical branches of computer science the term object is used in a purely mathematical sense to refer to any thing. While this interpretation is useful in the discussion of abstract theory, it is not concrete enough to serve as a primitive datatype in the discussion of more concrete... In object-oriented programming theory, polymorphism is the ability of objects belonging to different types to respond to method calls of methods of the same name, each one according to the right type-specific behavior. ...


The concept of polymorphism applies to data types in addition to functions. A function that can evaluate to and be applied to values of different types is known as a polymorphic function. A data type that contains elements of different types is known as a polymorphic data type. On computer science, a datatype (often simply type) is a name or label for a set of values and some operations which can be performed on that set of values. ... In computer science, a subroutine (function, procedure, or subprogram) is a sequence of code which performs a specific task, as part of a larger program, and is grouped as one, or more, statement blocks; such code is sometimes collected into software libraries. ...


There are two fundamentally different kinds of polymorphism, as first informally described by Christopher Strachey in 1967. If the range of actual types that can be used is finite and the combinations must be specified individually prior to use, it is called ad-hoc polymorphism. If all code is written without mention of any specific type and thus can be used transparently with any number of new types, it is called parametric polymorphism. In their formal treatment of the topic in 1985, Luca Cardelli and Peter Wegner later restricted the term parametric polymorphism to instances with type parameters, recognizing also other kinds of universal polymorphism. Christopher Strachey (1916–1975) was a British computer scientist. ... 1967 (MCMLXVII) was a common year starting on Sunday of the Gregorian calendar (the link is to a full 1967 calendar). ... Look up Ad hoc in Wiktionary, the free dictionary. ... A parameter is a measurement or value on which something else depends. ... This article is about the year. ... An Italian computer scientist, currently working for Microsoft Research in Cambridge. ...


Programming using parametric polymorphism is called generic programming, particularly in the object-oriented community. Advocates of object-oriented programming often cite polymorphism as one of the major benefits of that paradigm over others. Advocates of functional programming reject this claim on the grounds that the notion of parametric polymorphism is so deeply ingrained in many statically typed functional programming languages that most programmers simply take it for granted. However, the rise in popularity of object-oriented programming languages did contribute greatly to awareness and use of polymorphism in the mainstream programming community. In computer science, generics is a technique that allows one value to take different datatypes (so-called polymorphism) as long as certain contracts such as subtypes and signature are kept. ... Object-oriented programming (OOP) is a computer programming paradigm in which a software system is modeled as a set of objects that interact with each other. ... Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions. ... On computer science, a datatype (often simply type) is a name or label for a set of values and some operations which can be performed on that set of values. ... Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions. ...

Contents


Parametric polymorphism

Using parametric polymorphism, a function or a data type can be written generically so that it can deal equally well with any objects without depending on their type. 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 [a] × [a] → [a], where [a] denotes a list of 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. A parameter is a measurement or value on which something else depends. ...


Parametric polymorphism was first introduced to programming languages in ML in 1976. Today it exists in Standard ML, O'Caml, Haskell, and others. Some argue that templates should be considered an example of parametric polymorphism, though instead of actually producing generic code, the implementations generate specific code for each type value of a that a function is used with. ML is a general-purpose functional programming language developed by Robin Milner and others in the late 1970s at the University of Edinburgh, whose syntax is inspired by ISWIM. Historically, ML stands for metalanguage as it was conceived to develop proof tactics in the LCF theorem prover (the language of... 1976 (MCMLXXVI) was a leap year starting on Thursday (the link is to a full 1976 calendar). ... Standard ML (SML) is a general-purpose, modular, functional programming language with compile-time type checking and type inference. ... Objective Caml (OCaml) is an advanced programming language which is a member of the ML family. ... Haskell is a standardized pure functional programming language with non-strict semantics named after the logician Haskell Curry. ... In computer science, generics is a technique that allows one value to take different datatypes (so-called polymorphism) as long as certain contracts such as subtypes and signature are kept. ...


Parametric polymorphism is a way to make a language more expressive, while still maintaining full static type-safety. It is thus irrelevant in dynamically typed languages, since by definition they lack static type-safety. However, any dynamically typed function f that takes n arguments can be given a static type using parametric polymorphism: f : p1 × ... × pn → r, where p1, ..., pn and r are type parameters. Of course, this type is completely insubstantial and thus essentially useless. Instead, the types of arguments and return value are observed in run-time to match the operations performed on them. This article or section contains information that has not been verified and thus might not be reliable. ... On computer science, a datatype (often simply type) is a name or label for a set of values and some operations which can be performed on that set of values. ...


Cardelli and Wegner recognized in 1985 the advantages of allowing bounds on the type parameters. Many operations require some knowledge of the data types but can otherwise work parametrically. For example, to check if 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 Eq a a → [a] → Bool in Haskell. In most object-oriented programming languages that support parametric polymorphism, parameters can be constrained to be subtypes of a given type (see #Subtyping polymorphism below and the article on Generic programming). Standard ML (SML) is a general-purpose, modular, functional programming language with compile-time type checking and type inference. ... Haskell is a standardized pure functional programming language with non-strict semantics named after the logician Haskell Curry. ... The type system of the Haskell programming language includes a construct called the type class that provides a powerful form of restricted parametric polymorphism. ... ... In computer science, generics is a technique that allows one value to take different datatypes (so-called polymorphism) as long as certain contracts such as subtypes and signature are kept. ...


Predicative and impredicative polymorphism

Type systems with parametric polymorphism can be classified into predicative and impredicative systems. The key difference is in how parametric values may be instantiated. For example, consider the append function described above, which has type [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. In a predicative system, type variables may not be instantiated with polymorphic types. 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. In mathematics, a predicate is a relation. ...


Polymorphism in the language ML and its close relatives is predicative. This is because predicativity, together with other restrictions, makes the type system simple enough that type inference is possible. In languages where explicit type annotations are necessary when applying a polymorphic function, the predicativity restriction is less important; thus these languages are generally impredicative. Haskell manages to achieve type inference without predicativity but with a few complications. Type inference is a feature present in some strongly statically typed programming languages. ...


In type theory, the most frequently studied impredicative typed λ-calculi are based on those of the lambda cube, especially System F. Predicative type theories include Martin-Löf Type Theory and NuPRL. At the broadest level, type theory is the branch of mathematics and logic that concerns itself with classifying entities into collections called types. ... Typed versions of the lambda calculus extend the standard lambda calculus with types. ... In mathematical logic and type theory, the λ-cube is a framework for exploring the axes of refinement in Coquands Calculus of Constructions, starting from the simply typed lambda calculus as the vertex of a (3-D) cube placed at the origin, and the calculus of constructions (= higher order... System F is a typed lambda calculus. ... Intuitionistic Type Theory, or Constructive Type Theory, or Martin-Löf Type Theory or just Type Theory (with capital letters) is at the same time a functional programming language, a logic and a set theory based on the principles of mathematical constructivism. ... This article lacks information on the importance of the subject matter. ...


Subtyping polymorphism

Some languages employ the idea of subtypes to restrict the range of types that can be used in a particular case of parametric polymorphism. In these languages, subtyping polymorphism (sometimes referred to as dynamic polymorphism or dynamic typing) allows a function to be written to take an object of a certain type T, but also work correctly if passed an object that belongs to a type S that is a subtype of T (according to the Liskov substitution principle). This type relation is sometimes written S <: T. Conversely, T is said to be a supertype of S—written T :> S. ... In object-oriented computer programming, the Liskov substitution principle is a particular definition of subtype that was introduced by Barbara Liskov and Jeannette Wing in a 1993 paper entitled Family Values: A Behavioral Notion of Subtyping. ...


For example, if Number, Rational, and Integer are types such that Number :> Rational and Number :> Integer, a function written to take a Number will work equally well when passed an Integer or Rational as when passed a Number. The actual type of the object can be hidden from clients into a black box, and accessed via object identity. In fact, if the Number type is abstract, it may not even be possible to get your hands on an object whose most-derived type is Number (see abstract data type, abstract class). This particular kind of type hierarchy is known—especially in the context of the Scheme programming language—as a numerical tower, and usually contains a lot more types. This is a disambiguation page — a navigational aid which lists other pages that might otherwise share the same title. ... An identity in object-oriented programming, object-oriented design and object-oriented analysis describes the property of objects that the object can be distinguished from other objects. ... To meet Wikipedias quality standards, this article or section may require cleanup. ... In object-oriented programming, a class consists of encapsulated instance variables and subprograms, the methods mentioned below. ... Scheme is a functional programming language and a dialect of Lisp. ... In computing, numerical tower refers to the set of number types (datatypes that represent numbers) in a given programming language. ...


Object-oriented programming languages offer subtyping polymorphism using subclassing (also known as inheritance). In typical implementations, each class contains what is called a virtual table—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 An object-oriented programming language (also called an OO language) is one that allows or encourages, to some degree, object-oriented programming methods. ... In object-oriented programming, a subclass is a class that inherits some properties from its superclass. ... See inheritance (computer science) for other computing uses of inheritance. ... A virtual table, or vtable, is a mechanism used in Programming languages to support dynamic polymorphism, i. ...

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

The same goes for most other popular object systems. Some, however, such as CLOS, provide multiple dispatch, under which method calls are polymorphic in all arguments. In computer science, binding is associating objects and implementations with names in programming language so that those objects and implementations can be accessed by the names. ... In computer science, dynamic dispatch is the process of mapping a method call to a specific sequence of code at runtime (i. ... The Common Lisp Object System, a powerful system for object-oriented programming which forms part of Common Lisp. ... Multiple dispatch or multimethods is the feature of some object-oriented programming languages in which a function or method can be specialized on the type of more than one of its arguments. ...


Ad-hoc polymorphism

Ad-hoc polymorphism usually refers to simple overloading, but sometimes automatic type conversion, known as coercion, is also considered to be a kind of ad-hoc polymorphism (see the example section below). Common to these two types is the fact that the programmer has to specify exactly what types are to be usable with the polymorphic function. Look up Ad hoc in Wiktionary, the free dictionary. ... In computer science, polymorphism is the idea of allowing the same code to be used with different types, resulting in more general and abstract implementations. ...


The name refers to the manner in which this kind of polymorphism is typically introduced: "Oh, hey, let's make the + operator work on strings, too!" Some argue that ad-hoc polymorphism is not polymorphism in a meaningful computer science sense at all—that it is just syntactic sugar for calling append_integer, append_string, etc., manually. One way to see it is that Syntactic sugar is a term coined by Peter J. Landin for additions to the syntax of a computer language that do not affect its expressiveness but make it sweeter for humans to use. ...

  • to the user, there appears to be only one function, but one that takes different types of input and is thus type polymorphic; on the other hand,
  • to the author, there are several functions that need to be written—one for each type of input—so there's essentially no polymorphism.

In other words, ad hoc polymorphism is a dispatch mechanism: code moving through one named function is dispatched to various other functions without having to specify the exact function being called. A dispatch can be: A report sent to a newspaper by a correspondent. ...


Overloading

Overloading allows multiple functions taking different types to be defined with the same name; the compiler or interpreter automatically calls the right one. This way, functions appending lists of integers, lists of strings, lists of real numbers, and so on could be written, and all be called append—and the right append function would be called based on the type of lists being appended. This differs from parametric polymorphism, in which the function would need to be written generically, to work with any kind of list. Using overloading, it is possible to have a function perform two completely different things based on the type of input passed to it; this is not possible with parametric polymorphism. Another way to look at overloading is that a routine is unequivocally identified not only by its name, but also by the nature of its parameters (the number, the order and the type). A diagram of the operation of a typical multi-language compiler. ... An interpreter is a computer program that executes other programs. ...


This type of polymorphism is common in object-oriented programming languages, many of which allow operators to be overloaded in a manner similar to functions (see operator overloading). It is also used extensively in the purely functional programming language Haskell in the form of type classes. Many languages lacking ad-hoc polymorphism suffer from long-winded names such as print_int, print_string, etc. (see C, Objective Caml). In computer science, object-oriented programming, OOP for short, is a computer programming paradigm. ... In mathematics, an operator is a function that performs some sort of operation on a number, variable, or function. ... In computer programming, operator overloading (less commonly known as operator ad-hoc polymorphism) is a specific case of polymorphism in which some or all of operators like +, = or == have different implementations depending on the types of their arguments. ... Haskell is a standardized pure functional programming language with non-strict semantics named after the logician Haskell Curry. ... The type system of the Haskell programming language includes a construct called the type class that provides a powerful form of restricted parametric polymorphism. ... The C Programming Language, Brian Kernighan and Dennis Ritchie, the original edition that served for many years as an informal specification of the language The C programming language is a general-purpose, procedural, imperative computer programming language developed in the early 1970s by Dennis Ritchie for use on the UNIX... Objective Caml, also known as OCaml or OCaml for short, is an advanced programming language that is part of the ML family. ...


An advantage that is sometimes gained from overloading is the appearance of specialization, e.g., a function with the same name can be implemented in multiple different ways, each optimized for the particular data types that it operates on. This can provide a convenient interface for code that needs to be specialized to multiple situations for performance reasons.


In general, overloading is done at compile time, so it is not a substitute for double or multiple dispatch. Double dispatch is a mechanism that dispatches a function call to different concrete functions depending on the runtime types of multiple objects involved in the call. ... Multiple dispatch or multimethods is the feature of some object-oriented programming languages in which a function or method can be specialized on the type of more than one of its arguments. ...


Coercion

Main article: type conversion

Most languages provide mechanisms (collectively called type conversion) that programmers can use to convert, or translate, values of one type into semantically analogous values of another type. For instance, it is usually possible to convert a value of a floating-point numeric type into a value of integer type with a rounding operation, or to convert an integer to a string using a function that constructs the integer's decimal representation. In some languages, certain of these conversions are performed automatically when a value of one type appears in a context that demands a value of a different type. This feature is called coercion. It is also called implicit type conversion, to reflect the understanding that a programmer who writes such an expression is "implicitly" asking that the value he provides be converted to the required type before use. This article needs to be cleaned up to conform to a higher standard of quality. ...


The C programming language provides a typical example. C implicitly performs any type conversions that do not "lose information": for instance, when an expression of type int appears in a context that requires a value of type double, the compiler inserts a conversion operation to turn the integer into a double-precision floating-point value. The same applies to "widening" coercions such as from int to long. Conversions that may not preserve the meaning of the original value, such as from double to int, are not performed implicitly and must be specified by the programmer. Some languages, notably C++ and C#, allow programmers to specify implicit conversion operations of their own in addition to those built into the language; in these cases it is the programmer's responsibility to ensure that such conversions are well-behaved in any appropriate sense. The C Programming Language, Brian Kernighan and Dennis Ritchie, the original edition that served for many years as an informal specification of the language The C programming language is a general-purpose, procedural, imperative computer programming language developed in the early 1970s by Dennis Ritchie for use on the UNIX... C++ (generally pronounced see plus plus) is a general-purpose programming language. ... The title given to this article is incorrect due to technical limitations. ...


In statically typed languages, the need to coerce a value is evident at compile time, and the exact nature of the coercion is statically determined. In dynamically typed languages, the need to convert a value to a different type is generally not discovered until the moment when the conversion must be performed, and only then can it be known from what type it must be converted and what must be done to convert it.


Strictly speaking, coercion is not polymorphism, since operations do not need to act on values of more than one type if their operands are implicitly converted beforehand. However, it is tempting for programmers to perceive coercion as a weakened form of overloading, and indeed, in some cases the distinction between the two is difficult to draw and not very useful, especially in languages that support both. For instance, does C have one addition operator per numeric type and perform implicit conversion, or does it have a separate addition operator for every combination of operand types? This is not a particularly useful distinction. (It is unarguable that C supports implicit conversion, however: a programmer is allowed to write a function expecting a parameter of type double and call that function with an integer argument.) In general, however, coercion is a strictly weaker construct than overloading: coercion only affects the way a function or operator can be applied, while overloading allows the meaning of a name or symbol to vary depending on the context.


Coercion is also related to subtyping, and indeed it is possible to consider σ to be a subtype of τ if there exists an implicit conversion from σ to τ, although this form of subtyping is qualitatively different from the kind of subtyping in which every member of σ is a member of τ. See Subtype for details. ...


Example

This example aims to illustrate three different kinds of polymorphism described in this article. Though overloading an originally arithmetic operator to do a wide variety of things in this way may not be the most clear-cut example, it allows some subtle points to be made. In practice, the different types of polymorphism are not generally mixed up as much as they are here.


Imagine, if you will, an operator + that may be used in the following ways:

  1. 1 + 2 → 3
  2. 3.14 + 0.0015 → 3.1415
  3. 1 + 3.7 → 4.7
  4. [1, 2, 3] + [4, 5, 6] → [1, 2, 3, 4, 5, 6]
  5. [true, false] + [false, true] → [true, false, false, true]
  6. "foo" + "bar" → "foobar"

Overloading

To handle these six function calls, four different pieces of code are needed—or three, if strings are considered to be lists of characters:

  • In the first case, integer addition must be invoked.
  • In the second and third cases, floating-point addition must be invoked.
  • In the fourth and fifth cases, list concatenation must be invoked.
  • In the last case, string concatenation must be invoked, unless this too is handled as list concatenation (as in, e.g., Haskell).

Thus, the name + actually refers to three or four completely different functions. This is an example of overloading. In computer science, the term integer is used to refer to any data type which can represent some subset of the mathematical integers. ... A floating-point number is a digital representation for a number in a certain subset of the rational numbers, and is often used to approximate an arbitrary real number on a computer. ... Look up list in Wiktionary, the free dictionary This article is about the word list as used in computer science. ... In formal language theory (and therefore in programming languages), concatenation is the operation of joining two character strings end to end. ... In various branches of mathematics and computer science, strings are sequences of various simple objects (symbols, tokens, characters, etc. ... Haskell is a standardized functional programming language with non-strict semantics, named after the logician Haskell Curry. ...


Coercion

As we've seen, there's one function for adding two integers and one for adding two floating-point numbers in this hypothetical programming environment, but note that there is no function for adding an integer to a floating-point number. The reason why we can still do this is that when the compiler/interpreter finds a function call f(a1, a2, ...) that no existing function named f can handle, it starts to look for ways to convert the arguments into different types in order to make the call conform to the signature of one of the functions named f. This is called coercion. Both coercion and overloading are kinds of ad-hoc polymorphism. A diagram of the operation of a typical multi-language compiler. ... An interpreter is a computer program that executes other programs. ...


In our case, since any integer can be converted into a floating-point number without loss of precision, 1 is converted into 1.0 and floating-point addition is invoked. There was really only one reasonable outcome in this case, because a floating-point number cannot generally be converted into an integer, so integer addition could not have been used; but significantly more complex, subtle, and ambiguous situations can occur in, e.g., C++.


Parametric polymorphism

Finally, the reason why we can concatenate both lists of integers, lists of booleans, and lists of characters, is that the function for list concatenation was written without any regard to the type of elements stored in the lists. This is an example of parametric polymorphism. If you wanted to, you could make up a thousand different new types of lists, and the generic list concatenation function would happily and without requiring any augmentation accept instances of them all.


It can be argued, however, that this polymorphism is not really a property of the function per se; that if the function is polymorphic, it is due to the fact that the list data type is polymorphic. This is true—to an extent, at least—but it is important to note that the function could just as well have been defined to take as a second argument an element to append to the list, instead of another list to concatenate to the first. If this were the case, the function would indisputably be parametrically polymorphic, because it could then not know anything about its second argument, except that the type of the element should match the type of the elements of the list.


See also

In computer science, duck typing is a term for dynamic typing typical of some programming languages, such as Smalltalk, where a variables value itself determines what the variable can do. ... System F is a typed lambda calculus. ... The lambda calculus is a formal system designed to investigate function definition, function application, and recursion. ...

References

  • Luca Cardelli, Peter Wegner. On Understanding Types, Data Abstraction, and Polymorphism, from Computing Surveys, (December, 1985) [1]
  • Philip Wadler, Stephen Blott. How to make ad-hoc polymorphism less ad hoc, from Proc. 16th ACM Symposium on Principles of Programming Languages, (January, 1989) [2]
  • Christopher Strachey. Fundamental Concepts in Programming Languages, from Higher-Order and Symbolic Computation, (April, 2000) [3]
  • Paul Hudak, John Peterson, Joseph Fasel. A Gentle Introduction to Haskell Version 98. [4]

  Results from FactBites:
 
Polymorphism (computer science) - Wikipedia, the free encyclopedia (2951 words)
In computer science, polymorphism means allowing a single definition to be used with different types of data (specifically, different classes of objects).
Polymorphism in the language ML and its close relatives is predicative.
In these languages, subtyping polymorphism (sometimes referred to as dynamic polymorphism or dynamic typing) allows a function to be written to take an object of a certain type T, but also work correctly if passed an object that belongs to a type S that is a subtype of T (according to the Liskov substitution principle).
  More results at FactBites »


 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments
Please enter the 5-letter protection code

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.