Docsity

Polymorphism in Programming Languages: Lecture 9, Slides of Computer Engineering and Programming

Toggle sidebar
bg1
CSE 130: Spring 2012
Programming Languages
Lecture 9: Polymorphism
1
Tuesday, May 1, 2012
News
•
–
•
–
•
–
2
Tuesday, May 1, 2012
Polymorphism enables Reuse
• Can reuse generic functions:
map :’a * ’b -> ’b * ’a
filter: (’a -> bool) -> ’a list -> ’a list
rev: ’a list -> ’a list
length: ’a list -> int
swap: ’a * ’b -> ’b * ’a
sort: (’a -> ’a -> bool) -> ’a list -> ’a list
fold: …
• If function (algorithm) is independent of
type, can reuse code for all types !
3
Tuesday, May 1, 2012
Polymorphic Data Types
•Data types are also polymorphic!
•Type is instantiated for each use:
type ’a list =
Nil
| Cons of (’a * ’a list)
Cons(1,Cons(2,Nil)) :
Cons(“a”,Cons(“b”,Nil)) :
Cons((1,2),Cons((3,4),Nil)) :
Nil :
4
Tuesday, May 1, 2012
Docsity.com
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Polymorphism in Programming Languages: Lecture 9 and more Slides Computer Engineering and Programming in PDF only on Docsity!

CSE 130: Spring 2012

Programming Languages

Lecture 9: Polymorphism

Tuesday, May 1, 2012 1

News

Tuesday, May 1, 2012 2

Polymorphism enables Reuse

  • Can reuse generic functions: map :’a * ’b -> ’b * ’a filter : (’a -> bool) -> ’a list -> ’a list rev : ’a list -> ’a list length : ’a list -> int swap : ’a * ’b -> ’b * ’a sort: (’a -> ’a -> bool) -> ’a list -> ’a list fold: …
  • If function (algorithm) is independent of type, can reuse code for all types!

Polymorphic Data Types

  • Data types are also polymorphic!
  • Type is instantiated for each use:

type ’a list = Nil | Cons of (’a * ’a list)

Cons(1,Cons(2,Nil)) : Cons(“a”,Cons(“b”,Nil)) : Cons((1,2),Cons((3,4),Nil)) : Nil :

Polymorphic Data Types

  • Data types are also polymorphic!
  • Type is instantiated for each use:

type ’a list = Nil | Cons of (’a * ’a list)

Cons(1,Cons(2,Nil)) : int list Cons(“a”,Cons(“b”,Nil)) : string list Cons((1,2),Cons((3,4),Nil)) : (int*int) list Nil : ’a list

Tuesday, May 1, 2012 5

Options

  • Consider the type:
  • Why is this useful?

type ’a option = None | Some of ’a

Tuesday, May 1, 2012 6

Remember This Guy?

val assoc: ’b ->’a -> (’a*’b) list -> ’b

We had to pass in “default” value (yuck!)

Instead, use “option” to return failure…

val assoc: ’a ->(’a*’b) list-> ’b option

Example: Calculator Revisited

type expr = | Num of int | Div of expr * expr

val eval : expr -> int

Can you write a function?

Datatypes with many type variables

  • Multiple type variables
  • Type is instantiated for each use:

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

Node(“alice”, 2, Leaf, Leaf) Node(“charlie”, 3, Leaf, Leaf) Node(“bob”, 13, , Node(“alice”, 2, Leaf, Leaf) , Node(3, “charlie”, Leaf, Leaf))

Tuesday, May 1, 2012 13

Binary Search Trees

BST Property:

keys in left < key < keys in right

Node (key, value, left, right)

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

Tuesday, May 1, 2012 14

Node: “bob”, 13 Node: “alice”, 2 Leaf Leaf

Node: “charlie”, 7 Leaf Leaf

Node(“bob”, 13 , Node(“alice”, 2, Leaf, Leaf) , Node(“charlie”, 3, Leaf, Leaf))

BST Property : keys in left < key < keys in right

In-Class Exercise!

Write a function to lookup keys…

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

val lookup : ’a ->(’a,’b)tree ->’b option

BST Property : keys in left < key < keys in right

A tricky question: consider this type

Which is a valid Ocaml Expression?

type (’a, ’b) wierdlist = Nil | Cons ’a* (’b, ’a) wierdlist

(a) Cons(1, Cons(“a”, Cons(3.14, Nil))) (b) Cons(1, Cons(“a”, Cons(1, Nil))) (c) Cons(1, Cons(“a”, Cons(“a”, Nil))) (d) Cons(1, Cons(1, Cons(“a”, Nil))) (e) Cons(1, Cons(1, Cons(1, Nil)))

Tuesday, May 1, 2012 17

Polymorphic Data Structures

  • Container data structures independent of type!
  • Appropriate type is instantiated at each use:
  • Static type checking catches errors early
    • Cannot add int key to string hashtable
  • Generics: in Java,C#,VB (borrowed from ML)

’a list (’a , ’b) tree (’a , ’b) hashtbl …

Tuesday, May 1, 2012 18

Type Inference

How DOES Ocaml figure out all the types ?!

Polymorphic Types

  • Polymorphic types are tricky
  • Not always obvious from staring at code
  • How to ensure correctness?
  • Types (almost) never entered w/ program!

Whats the type of foo?

let foo x =

let (y, z) = x in

z-y

(a) int

(b) int * int

(c) int * int -> int

(d) int -> int -> int

(e) Error

Tuesday, May 1, 2012 25

Example 4

let rec cat xs = match xs with | [] -> cat [] | x::xs -> x^(cat xs)

(a) string -> string (b) string (c) string list -> string list (d) string list -> string (e) Error Tuesday, May 1, 2012 26

Example 5

let rec cat xs = match xs with | [] -> “” | x::xs -> x^(cat xs)

ML doesn’t know what function does, or even that it finishes only its type!

let rec cat xs = match xs with | [] -> “” | x::xs -> x^(cat xs)

let rec cat xs = match xs with | [] -> cat [] | x::xs -> x^(cat xs)

Example 5

let rec map f xs = match xs with | [] -> [] | x::xs’ ->(f x)::(map f xs’)

Tuesday, May 1, 2012 29

Example 5

“Generalize” Unconstrained Vars

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

let rec map f xs = match xs with | [] -> [] | x::xs’ ->(f x)::(map f xs’)

Tuesday, May 1, 2012 30

What is the type of (<+>)

let (<+>) f g x = g (f x)

(a) ’a -> ’b -> ’c -> ’d (b) (’a->’b)->(’a ->’b)->(’a ->’b) (c) (int->char)->(char->bool)->(int->bool) (d) (int->int)->(int->int)->(int->int) (e) (’a->’b)->(’b ->’c)->(’a ->’c)

Example 7

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