Question:
what is meant by parametric polymorphism ?
Arpit
2006-12-03 03:40:48 UTC
what is meant by parametric polymorphism ?
Five answers:
aarpit31
2006-12-04 08:11:41 UTC
Polymorphism by parameter.
2006-12-03 11:46:07 UTC
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.



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.



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.



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
durga p
2006-12-03 11:57:33 UTC
Well i will start with the basic definition of the polymorphisam.



Definition: Polymorphism is nothing but behaving differently at different situations.





Explanation: Polymorphisam provies the option to have many funtions with same name but different functionalities.



suppose take an example of the a funtion defined as below.



int add( int a, int b);



this funtion is used to add a and b.



the polymorphisam allows another function with the same name with the following definition.



int add( int a, int b, int c).



this may be used for adding three intezers.







conclusion: so parametric polymorphism is nothing but the polymorphysm where the functions differ in the number of parameters.



Best Example :



the best example that we genreally see very frequently during iur programming practice is Prinft function



1) printf(" hai my name is durga prasad");



2) int a=23;

printf(" i am %d years old", a);





Printf is the function used in both above cases. But in 1 it has only one parameter ( string). where as in 2 it has two parameters ( string and variable).





thats..it..

have a great day.
2006-12-03 11:46:41 UTC
A concept first identified by Christopher Strachey (1967) and developed by Hindley and Milner, allowing types such as list of anything. E.g. in Haskell: length :: [a] -> Int is a function which operates on a list of objects of any type, a (a is a type variable). This is known as parametric polymorphism. Polymorphic typing allows strong type checking as well as generic functions. ML in 1976 was the first language with polymorphic typing. Ad-hoc polymorphism (better described as overloading) is the ability to use the same syntax for objects of different types, e.g. "+" for addition of reals and integers or "-" for unary negation or diadic subtraction. Parametric polymorphism allows the same object code for a function to handle arguments of many types but overloading only reuses syntax and requires different code to handle different types. See also generic type variable. In object-oriented programming, the term is used to describe a variable that may refer to objects whose class is not known at compile time and which respond at run time according to the actual class of the object to which they refer. (2002-08-08)
cowboybabeeup
2006-12-03 11:46:02 UTC
get a very big dictionary and also look at your text book!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...