Title: CSE 130 : Winter 2006 Programming Languages
1CSE 130 Winter 2006 Programming Languages
Lecture 7 Polymorphism
- Ranjit Jhala
- UC San Diego
2Functions are first-class values
- Arguments, return values, bindings
- What are the benefits ?
Iterator, Accumul, Reuse computation
pattern w/o exposing local info
Parameterized, similar functions (e.g. Testers)
Creating, (Returning) Functions
Using, (Taking) Functions
Compose Functions Flexible way to build Complex
functions from primitives.
3What is the deal with a ?
- These meta-functions have strange types
- map
- filter
- Why ?
(a ! b) ! a list ! b list
(a ! bool) ! a list ! a list
Otherwise, wed need a separate function for
int,string,bool,intint, int list, int string
list Meta-functions are then useless! Need to
write a new one for every instance! Useful if the
SAME function could be reused in every
situation
4Polymorphism
a b ! b a
fun swap (x,y) (y,x)
- a and b are type variables!
- For-all types
- a,b can be instantiated with any type
- w/ int,string int string ! string int
- w/ char,int list char int list ! int list
char - w/ int! int,bool (int ! Int) bool ! bool
(int ! int)
For all a, b a b ! b a
5Instantiation at use
(a ! b) ! a list ! b list
fun f x x 10 val fm map f
Instantiated a with int, b with int
fun f x x like val fm map f1 cat,
dog, burrito
Instantiated a with string,
b with string
6Polymorphic ML types
tv a b c T int bool string
char T1 T2 Tn T1 ! T2 tv
- Implict for-all at the left of all types
- Never printed out, or specified
7Polymorphism 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
8Genericity is everywhere
- Whats the type of this function ?
fun sort (lt,l) case l of gt
(ht) let val (l,r)
partition ((lt h),t) in (sort
(lt,l))_at_h(sort (lt,r)) end
REUSE same sort code for all lists String, int,
real, intint, .
(a ! a ! bool a list) ! a list
out list
In list
comparison
9Polymorphism 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 !
10Not just functions
- Data types are also polymorphic!
- Type is instantiated for each use
datatype a list Nil Cons of (a a
list)
int list
Cons(1,Cons(2,Nil))
string list
Cons(a,Cons(b,Nil))
intint list
Cons((1,2),Cons((3,4),Nil))
Nil
a list
11Datatypes with many type variables
- Multiple type variables
- Type is instantiated for each use
datatype (a,b) tree Leaf of (a b)
Node of (a,b) tree (a,b) tree
Leaf(ranjit,1)
Leaf(william,2)
(string,int) tree
Node(,)
Node(Leaf(ranjit,1),Leaf(3.14, pi))
12Polymorphic Data Structures
- Container data structures independent of type !
- Appropriate type is instantiated at each use
- Appropriate type instantiated at use
- No casting as in C/Java
- Static type checking catches errors early
- Cannot add int key to string hashtable
- Generics feature of next versions of Java,C,VB
a list (a , b) Tree (a , b) Hashtbl
key, data
13Polymorphic Types
- Polymorphic types are tricky
- Not always obvious from staring at code
- How to ensure correctness ?
- Types (almost) never entered w/ program!
14Remember ML is statically typed
Exec-time Dynamic
Expressions (Syntax)
Values (Semantics)
Compile-time Static
Types
- Programmer enters expression
- ML checks if expression is well-typed
- Using a precise set of rules, ML tries to find a
unique - type for the expression meaningful type for
the expr - ML evaluates expression to compute value
- Of the same type found in step 2
15Polymorphic 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
Value
Value
How is this different from values ? Values NOT
computed statically (e.g. fun values)
16Polymorphic Type Inference
- 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
- Every expression accepted by ML must have
- a valid inferred type
Value
Value undefined
17Example 1
val x 2 3 val y Int.toString x
18Example 2
val x 2 3 val y Int.toString x fun inc y
x y
19Example 3
fun foo x let val (y,z) x in (y)
z end
20Example 4
fun cat cat (ht) h(cat t)
ML doesnt know what the function does, or even
that it finishes only its type!
21Example 4
ML doesnt know what the function does, or even
that it finishes only its type!
fun cat cat (ht) h(cat t)
string list -gt string
fun cat cat cat (ht) h(cat t)
22Example 5
(a -gt b) -gt a list -gt b list
fun map f map f (ht)(map h)(map
f t)
Introduce unknown tyvar Unify,solve, Remaining
tyvar gets a forall
23Example 6
(b -gt c) (a -gt b) -gt (a -gt c)
fun compose (f,g) x f (g x)
24Example 7
(a b -gt b) -gt b -gt a list -gt b
fun fold f b b fold f (ht)fold f (f
(h,b)) t
25ML Review many key PL ideas
- Expressions, Types, Values
- Environments No mutation/assignment
- Static Scoping
- Recursive Functions
- Functions taking/returning functions
- Polymorphism
- Modules and Signatures
- Constructing large systems from subsystems
- Managing complexity
- Safe reuse
26A little Prolog
- Radically different view of computation
- Program facts rules
- Facts mexican(burrito)
- Rules delicious(x) - mexican(x)
- Computation does fact follow from rules ?
- delicious(burrito) ?