CSc 520 - Principles of Programming Languages
13 : Types -- Introduction

Christian Collberg

Department of Computer Science

University of Arizona

1 Why types?

2 Why types...?

3 Why types...?

global x,y,z
procedure p()
   x := x + y    # integer addition
   x := x || y   # string concatenation
   x := x ||| y  # list concatenation
end

4 Why types...?

int x;
String y;
float z;
void p() {
   x = x + 5;      /* integer addition */ 
   z = z + 5.0;    /* float addition */ 
   y = y + "X";    /* string concatenation */
}

5 Why types...?

6 Why types...?

7 Type Systems

8 Type Systems...

9 Type Systems...

10 Type Systems...

11 Type Checking

12 Type Checking -- Strong Typing

13 Type Checking -- Weak Typing

14 Type Checking -- Static/Dynamic Typing

15 Terminology

16 Examples -- Pascal

17 Pascal - Untagged Variant Records


7#7

18 Examples -- C

19 Examples -- Ada

type Device is (Printer, Disk, Drum);
type Peripheral(Unit : Device := Disk) is record
    case Unit is
       when Printer => Line_Count : Integer ;
       when others =>  Cylinder   : CIndex;
    end case;
    end record;

20 Examples -- Ada...

21 Examples -- Scheme

(define (sum l)
   (cond 
      ((null? l) 0)
      ((not (list? l)) 
             (error "list expected"))
      ((not (number? (car l))) 
             (error "list of numbers expected"))
      (else (+ (car l) (sum (cdr l))))
))

22 Examples -- Java

class A {}
class B extends A {
   int x;
}
void p() {
   B b = (B) new A();
}

23 Typing

typing

24 Type Inference

25 Type Inference...

26 So, What is a Type?

27 Denotational View

28 Constructive View

29 Abstraction-Based View

30 Abstraction-Based View...

        
INTERFACE Word;
   TYPE T = INTEGER;
   PROCEDURE Plus  (x,y: T): T;    
   PROCEDURE Times (x,y: T): T;    
   PROCEDURE Minus (x,y: T): T;    
   PROCEDURE Divide(x,y: T): T;    
   PROCEDURE Mod(x,y: T): T;       
   PROCEDURE LT(x,y: T): BOOLEAN;  
   PROCEDURE LE(x,y: T): BOOLEAN;  
   PROCEDURE GT(x,y: T): BOOLEAN;  
   PROCEDURE GE(x,y: T): BOOLEAN;  
   PROCEDURE And(x,y: T): T;       
   PROCEDURE Or (x,y: T): T;       
   PROCEDURE Xor(x,y: T): T;       
   PROCEDURE Not  (x: T):  T;      
   PROCEDURE Shift(x: T; n: INTEGER): T;
   PROCEDURE Rotate(x: T; n: INTEGER): T;
   PROCEDURE Extract(x: T; i, n: CARDINAL): T;
   PROCEDURE Insert(x: T; y: T; i, n: CARDINAL): T;
END Word.

31 Readings and References



Christian Collberg 2008-02-18