cse 130: fall 2011...polymorphic data structures •container data structures independent of type !...

25
CSE 130: Fall 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 9: Type Inference

Upload: others

Post on 31-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

CSE 130: Fall 2011

Programming Languages

Ranjit Jhala

UC San Diego

Lecture 9: Type Inference

Page 2: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

News

• PA4 Updated

– Do second question before midterm …

• “Late Day” = 1 second after 5pm• “Late Day” = 1 second after 5pm

• Midterm next week in class

Page 3: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

Polymorphic Data Structures

• Container data structures independent of type !

• Appropriate type is instantiated at each use:

’a list

(’a , ’b) tree

(’a , ’b) hashtbl …

key, data

• Appropriate type instantiated at use– No unsafe casting as in C++/Java

• Static type checking catches errors early– Cannot add int key to string hashtable

• Generics: in Java,C#,VB (borrowed from ML)

(’a , ’b) hashtbl …

Page 4: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

Other kinds of polymorphisms

• That was OCaml...

• But what about other kinds of

polymorphisms..

4

polymorphisms..

Page 5: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

Other kinds of polymorphisms

• Sub-type polymorphismvoid f(Shape s)

– Can pass in any sub-type of Shape

5

• Parametric polymorphism

void proc_elems(list[T])

– can pass in ANY T

– this is the kind in OCaml!

Page 6: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

Other kinds of polymorphisms

• Bounded polymorphism

– Like parametric, except can provide a bound

void proc_elems(list[T]) T extends Printable

– Hey... isn’t this subtype polymorphism?

6

– Hey... isn’t this subtype polymorphism?

– No, for example:

bool ShapeEq(T a, T b) T extends Shape

– Can call on

•(Rect, Rect)

•(Circle, Circle)

– But not (Rect, Circle)

Page 7: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

Summary of polymorphism

• Subtype

• Parametric

7

• Bounded = Parametric + Subtype

(In Java/C#)

Page 8: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

Back to OCaml

• Polymorphic types allow us to reuse code

• However, not always obvious from staring

at code

8

at code

• But... Types never entered w/ program!

Page 9: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

Type inference

9

aka: how in the world does Ocaml

figure out all the types ???

Page 10: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

Polymorphic Types

• Polymorphic types are tricky

• Not always obvious from staring at code

• How to ensure correctness ?

• Types (almost) never entered w/ program!

Page 11: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

Polymorphic Type Inference

• Computing the types of all expressions

– At compile time : Statically Typed

• Each binding is processed in order• Each binding is processed in order

– Types are computed for each binding

– For expression and variable bound to

– Types used for subsequent bindings

• How is this different from values ?Values NOT computed statically (e.g. fun values)

Page 12: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

Polymorphic Type Inference

• Every expression accepted by ML must have

a valid inferred type

• Can have no idea what a function does, Value • Can have no idea what a function does,

but still know its exact type

• A function may never (or sometimes terminate),

but will still have a valid type

Value

Value

undefined

Page 13: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

Example 1

let x = 2 + 3;;

let y = string_of_int x;;let y = string_of_int x;;

Page 14: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

Example 2

let x = 2 + 3;;

let y = string_of_int x;;let y = string_of_int x;;

let inc y = x + y;;

Page 15: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

Example 3

let foo x =

let (y,z) = x inlet (y,z) = x in

z-y

;;

Page 16: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

Example 4

let rec cat l =

match l with

[] -> “”[] -> “”

| h::t -> h^(cat t)

ML doesn’t know what the

function does, or even that it

finishes only its type!

Page 17: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

Example 4ML doesn’t know what the

function does, or even that it

finishes only its type!

let rec cat l =

match l with

[] -> “”

| h::t -> h^(cat t)

string list -> string

| h::t -> h^(cat t)

let rec cat l =

match l with

[] -> cat []

| h::t -> h^(cat t)

Page 18: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

Example 5

let rec map f xs =

match xs with

[] -> []

(‘a -> ‘b) -> ‘a list -> ‘b list

[] -> []

| x::xs’ ->(f x)::(map f xs’)

Introduce unknown tyvar: Unify,solve,

Remaining tyvar gets a “forall”

Page 19: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

Example 6

let compose f g x = f (g x)

(‘b -> ‘c) -> (‘a -> ‘b) -> (‘a -> ‘c)

Page 20: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

Example 7

let rec fold f cur xs =

match xs with

[] -> cur

(‘a -> ‘b -> ‘a) -> ‘a -> ‘b list -> ‘a

[] -> cur

| x::xs’ -> fold f (f cur x) xs’

Page 21: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

(In Class Exercise A)

let rec split xs =

match xs with

| [] -> ([], []) | [] -> ([], [])

| [x] -> ([x], [])

| y::z::xs’ ->

let ys,zs = split xs’ in

(y::ys, z::zs)

Page 22: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

(In Class Exercise B)

let rec merge xs ys =

match (xs, ys) with

| ([],_) -> ys

| (_,[]) -> xs| (_,[]) -> xs

| (x::xs’, y::ys’) when x<=y

-> x :: (merge xs’ ys)

| (x::xs’, y::ys’)

-> y :: (merge xs ys’)

Page 23: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

(In Class Exercise C)

let rec msort xs =

match xs with

| [] -> | [] ->

[]

| x::xs’ ->

let ys,zs = split xs in

merge (msort ys) (msort zs)

Page 24: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

Example 11

let foo1 f g x =

if f x

then x then x

else g x

Page 25: CSE 130: Fall 2011...Polymorphic Data Structures •Container data structures independent of type ! •Appropriate type is instantiated at each use: ’a list (’a , ’b) tree (’a

Example 12

let foo2 f g x =

if f x

then x

else foo2 f g (g x)else foo2 f g (g x)