





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Programming Languages
Tuesday, May 1, 2012 1
News
Tuesday, May 1, 2012 2
Polymorphism enables Reuse
Polymorphic Data Types
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
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
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?
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
BST Property:
keys in left < key < keys in 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
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
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
’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
Whats the type of foo?
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’