33
CSE 130: Spring 2010 Programming Languages Lecture 9: Polymorphism ( ht th d l ith ’ ) (or, whats the deal with ’a) Ranjit Jhala UC San Diego

CSE 130: Spring 2010 - University of California, San Diego · CSE 130: Spring 2010 Programming Languages Lecture 9: Polymorphism ( (or, whthats th d l ith ’ )the deal with ’a)

  • Upload
    hadung

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

CSE 130: Spring 2010

Programming Languages

Lecture 9: Polymorphism ( h t th d l ith ’ )(or, whats the deal with ’a)

Ranjit JhalaUC San Diego

NewsNews

• PA4 is out (Extra long)PA4 is out (Extra long)– New tools (Ocamllex/OcamlYacc/Make)

Y t ill i k thi – Yes, we expect you will pick this up…… with a little help from us.

• Midterm next Tu (in class)• Midterm next Tu (in class)– Open book etc.– Practice materials on Webpage

Functions are “first-class” valuesFunctions are first class values

• Arguments, return values, bindings …g , , g• What are the benefits ?

Creating,(Returning)Functions

Using,(Taking)

Functions

Everywhere:• Javascript• Google/Mapreduce Yahoo/Hadoop• Google/Mapreduce, Yahoo/Hadoop,• C++0x

Parametric types

aka: what’s up with those ’a ?aka: what s up with those a ?

What is the deal with ’a ?What is the deal with a ?

These meta-functions have strange types:These meta functions have strange types:

map: (’a → ’b) → ’a list → ’b list

filter: (’a → bool) → ’a list → ’a list

Why ?

PolymorphismPolymorphism

• Poly = many morph = kind• Poly = many, morph = kind

’a * ’b → ’b * ’a let swap (x,y) = (y,x)

• ’a and ’b are type variables!• For-all types: For all ’a, ’b: ’a * ’b → ’b * ’a • For all types:

’ ’b b i t ti t d ith t

For all a, b: a b → b a

• ’a,’b can be instantiated with any type:w/ int,string : int * string → string * int

/ char int list : char * int list int list * charw/ char, int list : char * int list → int list * charw/ int→ int , bool : (int → Int) * bool → bool * (int → int)

Instantiation at UseInstantiation at Use

map: (’a → ’b) → ’a list → ’b list map: ( a → b) → a list → b list

let f x = x + 10;;l t f flet fm = map f;;

f : int -> int first arg. of map: a->b Instantiated : ’a with int ’b with int

let f x = x^“ like”;;

Instantiated : ’a with int, ’b with int

let f x = x like ;;let fm = map f [“cat”; “dog”; “burrito”];;

f : string > string first arg of map: a >b f : string -> string first arg. of map: a->b Instantiated : ’a with str, ’b with str

Instantiation at Use: be carefulInstantiation at Use: be careful

map: (’a → ’b) → ’a list → ’b list map:

let f x = x^“ like”;;

( a → b) → a list → b list

let fm = map f [1;2;3;4];;

f : string > string first arg of map: a >b f : string -> string first arg. of map: a->b Instantiated : ’a with str, ’b with str’So list must be ’a list = string list !So, list must be a list = string list !

Polymorphic ML typesPolymorphic ML types

• Poly = many morph = kind• Poly = many, morph = kind

• Possible ML types:

tv = ’a | ’b | ’c | … i b l i h T = int | bool | string | char | …T1 * T2 * … Tn | T1 → T2 | tv

• Implicit for-all at the “left” of all typesNever printed out– Never printed out

map: (’a → ’b) → ’a list → ’b list For all ’a , ’b :

Polymorphism enables ReusePolymorphism enables Reuse

• Can reuse generic functions:• Can reuse generic functions:map :’a * ’b → ’b * ’afilter: (’a → bool) → ’a list → ’a list filter: (’a → bool) → ’a list → ’a list rev: ’a list → ’a listlength: ’a list → int length: a list → int swap: ’a * ’b → ’b * ’a

sort: (’a → ’a → bool) → ’a list → ’a listsort: ( a → a → bool) → a list → a listfold: …

If function (algorithm) is independent of • If function (algorithm) is independent of type, can reuse code for all types !

Not just functions Not just functions …• Data types are also polymorphic!

type ’a list = Nil

| Cons of (’a * ’a list)

• Type is instantiated for each use:Cons(1 Cons(2 Nil)) : Cons(1,Cons(2,Nil)) :

Cons(“a”,Cons(“b”,Nil)) :

Cons((1,2),Cons((3,4),Nil)) :

Nil 11

Nil :

Not just functions Not just functions …• Data types are also polymorphic!

type ’a list = Nil

| Cons of (’a * ’a list)

• Type is instantiated for each use:Cons(1 Cons(2 Nil)) : int listCons(1,Cons(2,Nil)) : int list

Cons(“a”,Cons(“b”,Nil)) : string list

Cons((1,2),Cons((3,4),Nil)) : (int*int) list

Nil ’ li t 12

Nil : ’a list

Datatypes with many type variablesDatatypes with many type variables

• Multiple type variablestype (’a,’b) tree =

Leaf of (’a * ’b) | Node of (’a,’b) tree * (’a,’b) tree

• Type is instantiated for each use:L f(“j ” 1) Leaf(“joe”,1) :

Leaf(“william”,2) :

Node(…,…) :

13Node(Leaf(“joe”,1),Leaf(3.14, “pi”)):

Datatypes with many type variablesDatatypes with many type variables

• Multiple type variablestype (’a,’b) tree =

Leaf of (’a * ’b) | Node of (’a,’b) tree * (’a,’b) tree

• Type is instantiated for each use:L f(“j ” 1) ( t i i t) t Leaf(“joe”,1) : (string,int) tree

Leaf(“william”,2) : (string,int) tree

Node(…,…) : (string,int) tree

14Node(Leaf(“joe”,1),Leaf(3.14, “pi”))

Polymorphic Data StructuresPolymorphic Data Structures• Container data structures independent of type !• Appropriate type is instantiated at each use:

’a list(’ ’b) key data

A i t t i t ti t d t

(’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)

Other kinds of polymorphismsOther kinds of polymorphisms

• That was OCaml• That was OCaml...

B h b h ki d f • But what about other kinds of polymorphisms..

16

Other kinds of polymorphismsOther kinds of polymorphisms

• Sub-type polymorphism• Sub type polymorphismvoid f(Shape s)

– Can pass in any sub-type of ShapeCan pass in any sub type of Shape

P t i l hi• Parametric polymorphismvoid proc_elems(list[T])

– can pass in ANY T– this is the kind in OCaml!

17

Other kinds of polymorphismsOther kinds of polymorphisms• Bounded polymorphismp y p

– Like parametric, except can provide a boundvoid proc_elems(list[T]) T extends Printable

– Hey... isn’t this subtype polymorphism?– No, for example:, p

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

– Can call on •(Rect, Rect)•(Circle, Circle)

18– But not (Rect, Circle)

Summary of polymorphismSummary of polymorphism

• Subtype• Subtype

•Parametric

• Bounded = Parametric + Subtype • Bounded = Parametric + Subtype (Java/C#)

19

Back to OCamlBack to OCaml

• Polymorphic types allow us to reuse code• Polymorphic types allow us to reuse code

H l b i f i • However, not always obvious from staring at code

• But... Types never entered w/ program!yp p g

20

Type inference

aka: how in the world does Ocaml aka: how in the world does Ocaml figure out all the types ???

21

Polymorphic TypesPolymorphic Types

• Polymorphic types are tricky• Polymorphic types are tricky

N l b i f i d• Not always obvious from staring at code

• How to ensure correctness ?

• Types (almost) never entered w/ program!

Polymorphic Type InferencePolymorphic Type Inference• Computing the types of all expressions

– At compile time : Statically Typed

• 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)

Polymorphic Type InferencePolymorphic Type Inference

• Every expression accepted by ML must haveEvery expression accepted by ML must havea valid inferred type

• Can have no idea what a function does, but still know its exact type

Value but still know its exact type

A function may never (or sometimes terminate)• A function may never (or sometimes terminate),but will still have a valid type Value

d fi d undefined

Polymorphic Type InferencePolymorphic Type Inference

Example 1Example 1

let x = 2 + 3;;

let y = string_of_int x;;

Example 2Example 2

let x = 2 + 3;;

let y = string_of_int x;;

let inc y = x + y;;

Example 3Example 3

let foo x =let foo x let (y,z) = x inz-yz y

;;

Example 4 ML doesn’t know what the f ti d th t it Example 4 function does, or even that it

finishes only its type!

let rec cat = function| [] > “”| [] -> “”| h::t -> h^(cat t)

Example 4ML doesn’t know what the function does, or even that it Example 4

finishes only its type!

let rec cat = functionlet rec cat = function| [] -> “”| h::t -> h^(cat t)| h::t > h (cat t)

string list -> string

let rec cat = function| [] -> cat []| h::t -> h^(cat t)

Example 5 (‘a -> ‘b) -> ‘a list -> ‘b listExample 5 ( a > b) > a list > b list

let rec map f = function|[] -> []| h::t ->(map f)::(map f t)

Introduce unknown tyvar: Unify solveIntroduce unknown tyvar: Unify,solve,Remaining tyvar gets a “forall”

Example 6 (‘b -> ‘c) * (‘a -> ‘b) -> (‘a -> ‘c)Example 6 ( b > c) ( a > b) > ( a > c)

let compose (f,g) x = f (g x)

Example 7 (‘a ->‘b -> ‘a) -> ‘a -> ‘b list -> ‘aExample 7 ( a > b > a) > a > b list > a

let rec fold f cur = function | [] -> cur| x::xs -> fold f (f cur x) xs