<s>
In	O
programming	O
language	O
theory	O
and	O
type	O
theory	O
,	O
polymorphism	B-Application
is	O
the	O
provision	O
of	O
a	O
single	O
interface	B-Application
to	O
entities	O
of	O
different	O
types	O
or	O
the	O
use	O
of	O
a	O
single	O
symbol	O
to	O
represent	O
multiple	O
different	O
types	O
.	O
</s>
<s>
The	O
most	O
commonly	O
recognized	O
major	O
classes	O
of	O
polymorphism	B-Application
are	O
:	O
</s>
<s>
Ad	O
hoc	O
polymorphism	B-Application
:	O
defines	O
a	O
common	O
interface	B-Application
for	O
an	O
arbitrary	O
set	O
of	O
individually	O
specified	O
types	O
.	O
</s>
<s>
Parametric	O
polymorphism	B-Application
:	O
not	O
specifying	O
concrete	O
types	O
and	O
instead	O
use	O
abstract	O
symbols	O
that	O
can	O
substitute	O
for	O
any	O
type	O
.	O
</s>
<s>
Subtyping	O
(	O
also	O
called	O
subtype	O
polymorphism	B-Application
or	O
inclusion	O
polymorphism	B-Application
)	O
:	O
when	O
a	O
name	O
denotes	O
instances	O
of	O
many	O
different	O
classes	O
related	O
by	O
some	O
common	O
superclass	O
.	O
</s>
<s>
Interest	O
in	O
polymorphic	B-Application
type	O
systems	O
developed	O
significantly	O
in	O
the	O
1990s	O
,	O
with	O
practical	O
implementations	O
beginning	O
to	O
appear	O
by	O
the	O
end	O
of	O
the	O
decade	O
.	O
</s>
<s>
Ad	O
hoc	O
polymorphism	B-Application
and	O
parametric	O
polymorphism	B-Application
were	O
originally	O
described	O
in	O
Christopher	O
Strachey	O
's	O
Fundamental	O
Concepts	O
in	O
Programming	O
Languages	O
,	O
where	O
they	O
are	O
listed	O
as	O
"	O
the	O
two	O
main	O
classes	O
"	O
of	O
polymorphism	B-Application
.	O
</s>
<s>
Ad	O
hoc	O
polymorphism	B-Application
was	O
a	O
feature	O
of	O
Algol	B-Language
68	I-Language
,	O
while	O
parametric	O
polymorphism	B-Application
was	O
the	O
core	O
feature	O
of	O
ML	B-Language
's	O
type	O
system	O
.	O
</s>
<s>
In	O
a	O
1985	O
paper	O
,	O
Peter	O
Wegner	O
and	O
Luca	O
Cardelli	O
introduced	O
the	O
term	O
inclusion	O
polymorphism	B-Application
to	O
model	O
subtypes	O
and	O
inheritance	B-Language
,	O
citing	O
Simula	B-Language
as	O
the	O
first	O
programming	O
language	O
to	O
implement	O
it	O
.	O
</s>
<s>
Christopher	O
Strachey	O
chose	O
the	O
term	O
ad	O
hoc	O
polymorphism	B-Application
to	O
refer	O
to	O
polymorphic	B-Application
functions	I-Application
that	O
can	O
be	O
applied	O
to	O
arguments	O
of	O
different	O
types	O
,	O
but	O
that	O
behave	O
differently	O
depending	O
on	O
the	O
type	O
of	O
the	O
argument	O
to	O
which	O
they	O
are	O
applied	O
(	O
also	O
known	O
as	O
function	B-Language
overloading	I-Language
or	O
operator	O
overloading	B-Application
)	O
.	O
</s>
<s>
The	O
term	O
"	O
ad	O
hoc	O
"	O
in	O
this	O
context	O
is	O
not	O
intended	O
to	O
be	O
pejorative	O
;	O
it	O
refers	O
simply	O
to	O
the	O
fact	O
that	O
this	O
type	O
of	O
polymorphism	B-Application
is	O
not	O
a	O
fundamental	O
feature	O
of	O
the	O
type	O
system	O
.	O
</s>
<s>
In	O
the	O
Pascal	B-Application
/	O
Delphi	B-Language
example	O
below	O
,	O
the	O
Add	O
functions	O
seem	O
to	O
work	O
generically	O
over	O
various	O
types	O
when	O
looking	O
at	O
the	O
invocations	O
,	O
but	O
are	O
considered	O
to	O
be	O
two	O
entirely	O
distinct	O
functions	O
by	O
the	O
compiler	O
for	O
all	O
intents	O
and	O
purposes	O
:	O
</s>
<s>
Implicit	O
type	O
conversion	O
has	O
also	O
been	O
defined	O
as	O
a	O
form	O
of	O
polymorphism	B-Application
,	O
referred	O
to	O
as	O
"	O
coercion	O
polymorphism	B-Application
"	O
.	O
</s>
<s>
Parametric	O
polymorphism	B-Application
allows	O
a	O
function	O
or	O
a	O
data	O
type	O
to	O
be	O
written	O
generically	O
,	O
so	O
that	O
it	O
can	O
handle	O
values	O
uniformly	O
without	O
depending	O
on	O
their	O
type	O
.	O
</s>
<s>
Parametric	O
polymorphism	B-Application
is	O
a	O
way	O
to	O
make	O
a	O
language	O
more	O
expressive	O
while	O
still	O
maintaining	O
full	O
static	O
type-safety	B-Language
.	O
</s>
<s>
The	O
concept	O
of	O
parametric	O
polymorphism	B-Application
applies	O
to	O
both	O
data	O
types	O
and	O
functions	O
.	O
</s>
<s>
A	O
function	O
that	O
can	O
evaluate	O
to	O
or	O
be	O
applied	O
to	O
values	O
of	O
different	O
types	O
is	O
known	O
as	O
a	O
polymorphic	B-Application
function	I-Application
.	O
</s>
<s>
a	O
list	O
with	O
elements	O
of	O
arbitrary	O
type	O
)	O
is	O
designated	O
polymorphic	B-Application
data	O
type	O
like	O
the	O
generalized	O
type	O
from	O
which	O
such	O
specializations	O
are	O
made	O
.	O
</s>
<s>
Parametric	O
polymorphism	B-Application
is	O
ubiquitous	O
in	O
functional	O
programming	O
,	O
where	O
it	O
is	O
often	O
simply	O
referred	O
to	O
as	O
"	O
polymorphism	B-Application
"	O
.	O
</s>
<s>
The	O
following	O
example	O
in	O
Haskell	B-Language
shows	O
a	O
parameterized	O
list	O
data	O
type	O
and	O
two	O
parametrically	O
polymorphic	B-Application
functions	I-Application
on	O
them	O
:	O
</s>
<s>
Parametric	O
polymorphism	B-Application
is	O
also	O
available	O
in	O
several	O
object-oriented	B-Language
languages	I-Language
.	O
</s>
<s>
For	O
instance	O
,	O
templates	B-Application
in	O
C++	O
and	O
D	O
,	O
or	O
under	O
the	O
name	O
generics	B-Language
in	O
C#	O
,	O
Delphi	B-Language
,	O
Java	O
and	O
Go	O
:	O
</s>
<s>
John	O
C	O
.	O
Reynolds	O
(	O
and	O
later	O
Jean-Yves	O
Girard	O
)	O
formally	O
developed	O
this	O
notion	O
of	O
polymorphism	B-Application
as	O
an	O
extension	O
to	O
lambda	B-Language
calculus	I-Language
(	O
called	O
the	O
polymorphic	B-Application
lambda	B-Language
calculus	I-Language
or	O
System	O
F	O
)	O
.	O
</s>
<s>
Any	O
parametrically	O
polymorphic	B-Application
function	I-Application
is	O
necessarily	O
restricted	O
in	O
what	O
it	O
can	O
do	O
,	O
working	O
on	O
the	O
shape	O
of	O
the	O
data	O
instead	O
of	O
its	O
value	O
,	O
leading	O
to	O
the	O
concept	O
of	O
parametricity	O
.	O
</s>
<s>
Some	O
languages	O
employ	O
the	O
idea	O
of	O
subtyping	O
(	O
also	O
called	O
subtype	O
polymorphism	B-Application
or	O
inclusion	O
polymorphism	B-Application
)	O
to	O
restrict	O
the	O
range	O
of	O
types	O
that	O
can	O
be	O
used	O
in	O
a	O
particular	O
case	O
of	O
polymorphism	B-Application
.	O
</s>
<s>
In	O
these	O
languages	O
,	O
subtyping	O
allows	O
a	O
function	O
to	O
be	O
written	O
to	O
take	O
an	O
object	O
of	O
a	O
certain	O
type	O
T	O
,	O
but	O
also	O
work	O
correctly	O
,	O
if	O
passed	O
an	O
object	O
that	O
belongs	O
to	O
a	O
type	O
S	O
that	O
is	O
a	O
subtype	O
of	O
T	O
(	O
according	O
to	O
the	O
Liskov	B-Application
substitution	I-Application
principle	I-Application
)	O
.	O
</s>
<s>
This	O
type	O
relation	O
is	O
sometimes	O
written	O
S	O
:	O
T	O
.	O
Conversely	O
,	O
T	O
is	O
said	O
to	O
be	O
a	O
supertype	O
of	O
S	O
—	O
written	O
T:S	O
.	O
Subtype	O
polymorphism	B-Application
is	O
usually	O
resolved	O
dynamically	O
(	O
see	O
below	O
)	O
.	O
</s>
<s>
The	O
actual	O
type	O
of	O
the	O
object	O
can	O
be	O
hidden	O
from	O
clients	O
into	O
a	O
black	O
box	O
,	O
and	O
accessed	O
via	O
object	O
identity	B-Application
.	O
</s>
<s>
This	O
particular	O
kind	O
of	O
type	O
hierarchy	O
is	O
known	O
—	O
especially	O
in	O
the	O
context	O
of	O
the	O
Scheme	B-Language
programming	I-Language
language	I-Language
—	O
as	O
a	O
numerical	O
tower	O
,	O
and	O
usually	O
contains	O
many	O
more	O
types	O
.	O
</s>
<s>
Object-oriented	B-Language
programming	I-Language
languages	I-Language
offer	O
subtype	O
polymorphism	B-Application
using	O
subclassing	O
(	O
also	O
known	O
as	O
inheritance	B-Language
)	O
.	O
</s>
<s>
In	O
typical	O
implementations	O
,	O
each	O
class	O
contains	O
what	O
is	O
called	O
a	O
virtual	B-Language
table	I-Language
—	O
a	O
table	O
of	O
functions	O
that	O
implement	O
the	O
polymorphic	B-Application
part	O
of	O
the	O
class	O
interface	B-Application
—	O
and	O
each	O
object	O
contains	O
a	O
pointer	O
to	O
the	O
"	O
vtable	B-Language
"	O
of	O
its	O
class	O
,	O
which	O
is	O
then	O
consulted	O
whenever	O
a	O
polymorphic	B-Application
method	O
is	O
called	O
.	O
</s>
<s>
late	O
binding	O
,	O
because	O
virtual	B-Application
function	I-Application
calls	O
are	O
not	O
bound	O
until	O
the	O
time	O
of	O
invocation	O
;	O
</s>
<s>
single-argument	O
polymorphism	B-Application
)	O
,	O
because	O
virtual	B-Application
function	I-Application
calls	O
are	O
bound	O
simply	O
by	O
looking	O
through	O
the	O
vtable	B-Language
provided	O
by	O
the	O
first	O
argument	O
(	O
the	O
this	O
object	O
)	O
,	O
so	O
the	O
runtime	O
types	O
of	O
the	O
other	O
arguments	O
are	O
completely	O
irrelevant	O
.	O
</s>
<s>
The	O
same	O
goes	O
for	O
most	O
other	O
popular	O
object	B-Language
systems	I-Language
.	O
</s>
<s>
Some	O
,	O
however	O
,	O
such	O
as	O
Common	B-Application
Lisp	I-Application
Object	I-Application
System	I-Application
,	O
provide	O
multiple	O
dispatch	O
,	O
under	O
which	O
method	O
calls	O
are	O
polymorphic	B-Application
in	O
all	O
arguments	O
.	O
</s>
<s>
The	O
interaction	O
between	O
parametric	O
polymorphism	B-Application
and	O
subtyping	O
leads	O
to	O
the	O
concepts	O
of	O
variance	O
and	O
bounded	O
quantification	O
.	O
</s>
<s>
Row	O
polymorphism	B-Application
is	O
a	O
similar	O
,	O
but	O
distinct	O
concept	O
from	O
subtyping	O
.	O
</s>
<s>
A	O
polytypic	O
function	O
is	O
more	O
general	O
than	O
polymorphic	B-Application
,	O
and	O
in	O
such	O
a	O
function	O
,	O
"	O
though	O
one	O
can	O
provide	O
fixed	O
ad	O
hoc	O
cases	O
for	O
specific	O
data	O
types	O
,	O
an	O
ad	O
hoc	O
combinator	O
is	O
absent	O
"	O
.	O
</s>
<s>
Rank	O
polymorphism	B-Application
is	O
one	O
of	O
the	O
defining	O
features	O
of	O
the	O
array	O
programming	O
languages	O
,	O
like	O
APL	O
.	O
</s>
<s>
The	O
essence	O
of	O
the	O
rank-polymorphic	O
programming	O
model	O
is	O
implicitly	O
treating	O
all	O
operations	O
as	O
aggregate	O
operations	O
,	O
usable	O
on	O
arrays	O
with	O
arbitrarily	O
many	O
dimensions	O
,	O
which	O
is	O
to	O
say	O
that	O
rank	O
polymorphism	B-Application
allows	O
functions	O
to	O
be	O
defined	O
to	O
operate	O
on	O
arrays	O
of	O
any	O
shape	O
and	O
size	O
.	O
</s>
<s>
Polymorphism	B-Application
can	O
be	O
distinguished	O
by	O
when	O
the	O
implementation	O
is	O
selected	O
:	O
statically	O
(	O
at	O
compile	O
time	O
)	O
or	O
dynamically	O
(	O
at	O
run	O
time	O
,	O
typically	O
via	O
a	O
virtual	B-Application
function	I-Application
)	O
.	O
</s>
<s>
This	O
is	O
known	O
respectively	O
as	O
static	B-Language
dispatch	I-Language
and	O
dynamic	O
dispatch	O
,	O
and	O
the	O
corresponding	O
forms	O
of	O
polymorphism	B-Application
are	O
accordingly	O
called	O
static	O
polymorphism	B-Application
and	O
dynamic	O
polymorphism	B-Application
.	O
</s>
<s>
Static	O
polymorphism	B-Application
executes	O
faster	O
,	O
because	O
there	O
is	O
no	O
dynamic	O
dispatch	O
overhead	O
,	O
but	O
requires	O
additional	O
compiler	O
support	O
.	O
</s>
<s>
Further	O
,	O
static	O
polymorphism	B-Application
allows	O
greater	O
static	O
analysis	O
by	O
compilers	O
(	O
notably	O
for	O
optimization	O
)	O
,	O
source	O
code	O
analysis	O
tools	O
,	O
and	O
human	O
readers	O
(	O
programmers	O
)	O
.	O
</s>
<s>
Dynamic	O
polymorphism	B-Application
is	O
more	O
flexible	O
but	O
slower	O
—	O
for	O
example	O
,	O
dynamic	O
polymorphism	B-Application
allows	O
duck	B-Application
typing	I-Application
,	O
and	O
a	O
dynamically	O
linked	O
library	B-Library
may	O
operate	O
on	O
objects	O
without	O
knowing	O
their	O
full	O
type	O
.	O
</s>
<s>
Static	O
polymorphism	B-Application
typically	O
occurs	O
in	O
ad	O
hoc	O
polymorphism	B-Application
and	O
parametric	O
polymorphism	B-Application
,	O
whereas	O
dynamic	O
polymorphism	B-Application
is	O
usual	O
for	O
subtype	O
polymorphism	B-Application
.	O
</s>
<s>
However	O
,	O
it	O
is	O
possible	O
to	O
achieve	O
static	O
polymorphism	B-Application
with	O
subtyping	O
through	O
more	O
sophisticated	O
use	O
of	O
template	B-Language
metaprogramming	I-Language
,	O
namely	O
the	O
curiously	B-Language
recurring	I-Language
template	I-Language
pattern	I-Language
.	O
</s>
<s>
When	O
polymorphism	B-Application
is	O
exposed	O
via	O
a	O
library	B-Library
,	O
static	O
polymorphism	B-Application
becomes	O
impossible	O
for	O
dynamic	B-Application
libraries	I-Application
as	O
there	O
is	O
no	O
way	O
of	O
knowing	O
what	O
types	O
the	O
parameters	O
are	O
when	O
the	O
shared	O
object	O
is	O
built	O
.	O
</s>
<s>
While	O
languages	O
like	O
C++	O
and	O
Rust	O
use	O
monomorphized	O
templates	B-Application
,	O
the	O
Swift	B-Application
programming	I-Application
language	I-Application
makes	O
extensive	O
use	O
of	O
dynamic	O
dispatch	O
to	O
build	O
the	O
application	B-Operating_System
binary	I-Operating_System
interface	I-Operating_System
for	O
these	O
libraries	O
by	O
default	O
.	O
</s>
