Docsity

Polymorphism in CIS 500, Fall 2003 - Prof. B. Pierce, Study notes of Computer Science

Toggle sidebar
bg1
CIS 500
Software Foundations
Fall 2003
8 September (continued)
CIS 500, 8 September (continued) 1
Polymorphism
This version of
is said to be polymorphic, because it can be applied
to many different types of arguments. (“Poly” = many, “morph” = shape.)
Note that the type of the elements of
is
(pronounced “alpha”). This
is a type variable, which can instantiated, each time we apply
, by
replacing
with any type that we like. The instances of the type
include
In other words,
can be read, “
is a function that takes a list of elements of any type
alpha and returns an element of alpha.”
CIS 500, 8 September (continued) 2
A polymorphic
!
!
"
#
$
%
!
&
!
'
!
'
!
(
!
!
%
!
&
!
!
'
!
'
!
(
)
)
!
)
)
)
)
!
)
)
!
!
)
)
!
)
)
!
)
)
!
)
)
CIS 500, 8 September (continued) 3
A polymorphic
*
+
"
$
,
"
$
,
!
!
"
"
$
,
#
$
"
"
$
,
!
!
"
"
#
$
"
)
)
!
)
)
!
)
)
!
)
)
!
!
)
)
!
)
)
!
)
)
!
)
)
"
!
$
!
!
-
.
.
$
!
CIS 500, 8 September (continued) 4
pf3
pf4
pf5

Partial preview of the text

Download Polymorphism in CIS 500, Fall 2003 - Prof. B. Pierce and more Study notes Computer Science in PDF only on Docsity!

CIS 500

Software Foundations

Fall 2003

8 September (continued)

Polymorphism

This version of

is said to be

polymorphic

, because it can be applied

Note that the type of the elements ofto many different types of arguments. (“Poly” = many, “morph” = shape.)

is

(pronounced “alpha”). This

is a

type variable

, which can

instantiated

, each time we apply

, by

replacing

with any type that we like. The instances of the type

include

In other words,

can be read, “

is a function that takes a list of elements of any type

alpha and returns an element of alpha.”

CIS 500, 8 September (continued)

A polymorphic

A polymorphic

CIS 500, 8 September (continued)

Polymorphic

    







 







What is the type of

?

Palindromes

A

palindrome

is a word, sentence, or other sequence that reads the same

forwards and backwards. 











 ) ! )  ) ! )  ) !

 ) !

)

 ) !

 ) !

CIS 500, 8 September (continued)

map: “apply-to-each”

OCaml has a predefined function

  

that takes a function

and a

list

and produces another list by applying

to each element of

. We’ll

soon see how to define

   , but first let’s look at some examples.

  

 $ 

  

Note that

  

is polymorphic: it works for lists of integers, strings,

booleans, etc.

More on map

An interesting feature of

  

is its first argument is itself a function.

For this reason, we call

  

a

higher-order

function.

often are) simulated using objects.In other languages such as Java, higher-order functions can be (andeasy to work with.One of OCaml’s strengths is that it makes higher-order functions veryNatural uses for higher-order functions arise frequently in programming.

CIS 500, 8 September (continued)

Approaches to Typing

A

strongly typed

language prevents programs from accessing private

data, corrupting memory, crashing the machine, etc.

A

weakly typed

language does not.

A

statically typed

language performs type-consistency checks at

when programs are first entered.

A

dynamically typed

language delays these checks until programs are

executed.

Weak

Strong

Dynamic

PERL

Lisp, Scheme

Static

C, C++

ML, ADA, Java



 Strictly speaking, Java should be called “mostly static”

Practice with Types

What are the types of the following functions?



 



CIS 500, 8 September (continued)







 

And one more:

Aside: Polymorphism

of The polymorphism in ML that arises from type parameters is an example

generic programming

. (

   ,   

, etc.) Are good examples of

Different languages support generic programming in different ways...generic functions.

parametric polymorphism allows functions to work

uniformly

over

arguments of different types.E.g.,

ad hoc polymporphism (or

overloading

) allows an operation to behave

in

different

ways when applied to arguments of different types. There

overloading (e.g.is no such polymorphism in OCaml, but most languages allow some

 &

and

 &

). Java and C++ allow one to

extend the overloading of a symbol (e.g.



). This form

of overloading is a

syntactic

convenience, but little more.

of types sharing some common structure subtype polymporphism allows operations to be defined for collections

CIS 500, 8 September (continued)

e.g., a

operation might make sense for values of

 

and all

its “refinements”—

 ,  

,  . . 

, etc.

CIS 500, 8 September (continued)

the Java community!polymorphism in the ML community and for subtype polymorphism inConfusingly, the bare term “polymorphism” is used to refer to parametricparametric polymorphism are under discussion.)but no parametric polymorphism. (Various Java extensions withJava provides a subtyping as well as moderately powerful overloading,dialects of ML.) It does not allow overloading.OCaml, its support for subtyping is what distinguishes it from otheralso supports subtyping (Though we shall not get to see this aspect of OCaml supports parametric polymorphism in a very general way, and

CIS 500, 8 September (continued)