Docsity

Polymorphism - Software Foundations - Lecture Slides | CIS 500, Study notes of Computer Science

Partial preview of the text

Download Polymorphism - Software Foundations - Lecture Slides | CIS 500 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.”

A polymorphic

Polymorphic

What is the type of

 

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.

Note that, like map,

is polymorphic—it works on lists of any

type.

Defining

comes predefined in the OCaml system, but there is nothing

magic about it—we can easily define our own



function with the same

behavior.

The type of



is probably even more polymorphic than you expected!

The list that it returns can actually be of a

different

type from its

argument:

                                   

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?



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 structuresubtype polymporphism allows operations to be defined for collections

e.g., a

operation might make sense for values of

and all

its “refinements”—

, etc.