Polymorphism (Ad-hoc and Universal
Sérgio Souza Costa
Universidade Federaldo Maranhão
3 de julho de 2016
Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 3 de julho de 2016 1 / 20
Based in On understanding types, data abstraction, and polymorphism (Luca Cardelli and Peter
Wegner)
Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 3 de julho de 2016 2 / 20
Summary
Static and Strong Typing
Polymorphism
Kinds of Polymorphism
Ad-hoc - Overloading
Ad-hoc - Coercion
Parametric polymorphism
Inclusion polymorphism
Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 3 de julho de 2016 3 / 20
Static and Strong Typing
In mathematics as in programming, types impose constraints which help to enforce
correctness.
To prevent type violations, we generally impose a static type structure on programs. Types
are associated with constants, operators, variables, and function symbols.
A type inference system can be used to infer the types of expressions when little or no type
information is given explicitly.
In languages like Pascal and Ada, the type of variables and function symbols is dened by
redundant declarations and the compiler can check the consistency of denition and use.
In languages like ML, explicit declarations are avoided wherever possible and the system
may infer the type of expressions from local context, while still establishing
Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 3 de julho de 2016 4 / 20
Static and Strong Typing
Denição
Programming languages in which the type of every expression can be determined by static
program analysis are said to be statically typed.
Static typing is a useful property, but the requirement that all variables and expressions are
bound to a type at compile time is sometimestoo restrictive.
It may be replaced by the weaker requirement that all expressions are guaranteed to be
type-consistent although the type itself may be statically unknown; this can be generally
done by introducing some run-time type checking.
Denição
Languages in which all expressions are type-consistent are called strongly typed languages.
Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 3 de julho de 2016 5 / 20
Static and Strong Typing
Pros:
In general, we should strive for strong typing, and adopt static typing whenever possible..
Static typing allows type inconsistencies to be discovered at compile time and guarantees
that executed programs are type-consistent.
It facilitates early detection of type errors and allows greater execution-time eciency. It
enforces a programming discipline on the programmer that makes programs more
structured and easier to read.
Cons:
Static typing may also lead to a loss of exibility and expressive power by prematurely
constraining the behavior of objects to that associated with a particular type.
Traditional statically typed systems exclude programming techniques which, although
sound, are incompatible with early binding of program objects to a specic type. For
example they exclude generic procedures, e.g. sorting, that capture the structure of an
algorithm uniformly applicable to a range of types.
Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 3 de julho de 2016 6 / 20
Polymorphism
Conventional typed languages, such as Pascal, are based on the idea that functions and
procedures, and hence their operands, have a unique type. Such languages are said to be
monomorphic, in the sense that everyvalue and variable can be interpreted to be of
one and only one type.
Polymorphic languagesin which some values and variables may have more than one
type.
Polymorphic functionsare functions whose operands (actual parameters) can have more
than one type.
Polymorphic typesare types whose operations are applicable to values of more than one
type.
Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 3 de julho de 2016 7 / 20
Kinds of Polymorphism
Cardelli classication
Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 3 de julho de 2016 8 / 20
Kinds of Polymorphism
Ad-hocpolymorphism is obtained when afunction works, or appears to work, on
several dierent types(which may not exhibit a common structure) and may behave in
unrelated ways for each type.
Universallypolymorphic functions will normally work on aninnite number of types
(all the types having a given common structure), while an ad-hoc polymorphic function
will only work on a nite set of dierent and potentially unrelated types.
In terms of implementation, auniversallypolymorphic function will execute thesame
codefor arguments of any admissible type, while anad-hocpolymorphic functionmay
execute dierent code for each type of argument.
Universal polymorphism is consideredtrue polymorphism, while ad-hoc polymorphism is
some kind ofapparentpolymorphism
Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 3 de julho de 2016 9 / 20
Ad-hoc - Overloading
In overloadingthe same variable name is used to denote dierent functions, and the
context is used to decide which function is denoted by a particular instance of the name.
Example (c++):
1main(){
2 int, b;
3 float, y;
4 a // a = somaint (a, b);
5 x // x = somafloat (x, y);
6}
Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 3 de julho de 2016 10 / 20
Ad-hoc - Coercion
A coercion is instead a semantic operation which is needed to convert an argument to the
type expected by a function, in a situation which would otherwise result in a type error.
Coercions can be provided statically, by automatically inserting them between arguments
and functions at compile time, or may have to be determined dynamically by run-time
tests on the arguments.
Implicit conversion between types.
Example (c++):
1main() {
2 int;
3 float
4 float;
5 x // x = somafloat (x, y)
6 x // x = somafloat (x, intToFloat (w) );
7}
Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 3 de julho de 2016 11 / 20
Universal - Parametric polymorphism
Denição
In parametric polymorphism, a polymorphic function has an implicit or explicit type parameter,
which determines the type of the argument for each application of that function.
Example in Java
class
publicT<TT a,
ifa.compareTo(b));
else;
}
}
Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 3 de julho de 2016 12 / 20
Universal - Parametric polymorphism
C++
1templateclass>
2T maior (T a, T b) {
3return>b)?
4}
Haskell
1maiorOrd
2maior
3|
4|
Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 3 de julho de 2016 13 / 20
Universal - Parametric polymorphism
The functions that exhibit parametric polymorphism are also called generic functions.
For example, the length function from lists of arbitrary type to integers is called a generic
length function.
1length
A generic function is one which can work for arguments of many types, generally doing the
same kind of work independently of the argument type.
Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 3 de julho de 2016 14 / 20
Universal - Parametric polymorphism
Parametric type and operator overloading
1templateclass>
2class
3public:
4 T x, y;
5 Point (T x1, T y1):x(x1),y(y1) {}
6 Point&
7 return+other.x,y+other.y);
8 }
9 friend&&
10 out
11 return
12 }
13};
Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 3 de julho de 2016 15 / 20
Universal - Parametric polymorphism
1int() {
2 Point<string>,"cd");
3 Point<string>,"de");
4 Point<string>
5
6 cout
7
8 return;
9}
Observação
Parametric polymorphism is supported in Java through Generic Types.
Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 3 de julho de 2016 16 / 20
Inclusion polymorphism
Denição
Inclusion polymorphism is a type system in which a type may have subtypes, which inherit
operations from that type. Where a typeTis a set of values, equipped with some operations.
A subtype ofTis a subset of the values ofT, equipped with the same operations asT. Every
value of the subtype is also a value of typeT, and therefore may be used in a context where a
value of typeTis expected (David Watt).
In inclusion polymorphism an object can be viewed as belonging to many dierent classes
which need not be disjoint, i.e. there may be inclusion of classes.
Inclusion polymorphism as a relation among types which allows operations to be applied to
object of dierent types related by inclusion.
Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 3 de julho de 2016 17 / 20
Inclusion polymorphism - Example
class
protected,;
public // Draw this point.
. // other methods
class
private;
public //Draw this circle.
. // other methods
class
private,;
public // Draw this rectangle.
. // other methods
Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 3 de julho de 2016 18 / 20
Inclusion polymorphism - Example
Java allows objects of any subclass to be treated like objects of the superclass. Consider
the following variable:
Point p;
...
p(3.0,);
...
p(3.0,,);
This variable may refer to an object of class Point or any subclass of Point. In other
words, this variable may refer to any value of type Point.
Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 3 de julho de 2016 19 / 20
Inclusion polymorphism - Example
Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 3 de julho de 2016 20 / 20