CSc 520 - Principles of Programming Languages
16 : Types -- Polymorphism

Christian Collberg

Department of Computer Science

University of Arizona

1 What is polymorphism?

2 Polymorphic Variables

3 Polymorphic Variables in Ruby

x = 42; puts x
x = "42"; puts x
x = 42.42; puts x
x = [42,42.0,"42"]; puts x
x = /duck/
puts "duckduckduck".sub(x,"ruby")
x = 42
puts "duckduckduck".sub(x,"ruby")

4 Polymorphic Functions

5 Types of Polymorphism

6 Types of Polymorphism -- Parametric

7 Types of Polymorphism -- Inclusion

8 Levels of Freedom

9 Levels of Freedom...


C's Generic Reference Types


10 C

11 Polymorphic sort in C

12 Polymorphic sort in C -- Sorting ints

int compare_ints(void *a, void *b) {
    int ia = *(int *)a; 
    int ib = *(int *)b;
    return (ia<ib)?-1:1;
}
void main() {
    int data[] = { ... }; 
    qsort(data, num_elmts, 
          sizeof(int), compare_ints);
}


Pascal's Variant Records


13 Pascal

14 Tagged Variant Records


2#2

15 Tagged Variant Records...


3#3

16 Untagged Variant Records


4#4

17 Generic sort

18 Generic sort in Pascal 1

type R = record
            case tag : boolean of 
               true : (x : integer);
               false : (y : real);
         end;
    A = array [1..100] of R;

procedure sort(p:A);
var greater : boolean;
    i,j     : integer;
begin
   i := 1;j := 2;
   if p[i].tag=true then greater := p[i].x > p[j].x
   else                  greater := p[i].y > p[j].y;      
end;

19 Generic sort in Pascal 1...

var x:A;
begin 
   x[1].tag := true;
   x[1].x := 42;
   x[2].tag := true;
   x[2].x := 52;
   sort(x);
   x[1].tag := false;
   x[1].y := 42.5;
   x[2].tag := false;
   x[2].y := 52.3;
   sort(x);
end.

20 Generic sort in Pascal 2

program p;
type R = record
            case boolean of 
               true : (x : integer);
               false : (y : real);
         end;

    A = array [1..100] of R;

function cmpInts(p:A;i:integer;j:integer):boolean;
begin return p[i].x > p[j].x; end;

function cmpReals(p:A;i:integer;j:integer):boolean;
begin return p[i].y > p[j].y; end;

21 Generic sort in Pascal 2...

procedure sort(p:A; 
    function cmp(x:A;i:integer;j:integer):boolean);
var greater : boolean;
    i,j     : integer;
begin
   i := 1; j := 2;
   greater := cmp(p,i,j);
end;

22 Generic sort in Pascal 2...

var x:A;
begin 
   x[1].x := 42;
   x[2].x := 52;
   sort(x, greaterInts);
   x[1].y := 42.5;
   x[2].y := 52.3;
   sort(x, greaterReals);
   x[1].x := 42;
   x[2].x := 52;
   sort(x, greaterReals);
end.


Modula-2's conformant arrays


23 Pascal array parameters

24 Modula-2's arrays


Ada's Generics


25 Ada's generic modules

26 Ada


5#5

27 Ada...


6#6

28 Generics as Macros

29 Avoiding code-bloat


Inclusion Polymorphism in Java


30 Java

31 A Java class hierarchy

class Vehicle {}
   class Motorized extends Vehicle {}
      class TwoWheels extends Motorized {}
          class Moped extends TwoWheels {}
          class Motorcycle extends TwoWheels {}
      class FourWheels extends Motorized {}
         class Car extends FourWheels {}
            class SUV extends Car {}
    class Manual extends Vehicle {}  
       class Bicycle extends Manual {}

32 Inclusion polymorphism

33 A Java class hierarchy...

class Main {
   public static void main(String args[]) {
      Car civic = new Car();
      Vehicle v = civic;    /* OK */
      Bicycle b = civic;    /* static error */
      FourWheels f = civic; /* OK */
      SUV s = (SUV)f;       /* runtime error */
   }
}


Parametric Polymorphism in Haskell


34 Haskell Polymorphic Functions

35 Constrained Parametric Polymorphism

36 Context Predicates

37 Readings and References



Christian Collberg 2008-02-25