Title: CSE 130 : Winter 2006 Programming Languages
1CSE 130 Winter 2006 Programming Languages
Lecture Datatypes and Recursion
- Ranjit Jhala
- UC San Diego
2What about more complex data ?
- Weve seen some base types and values
- Integers, Reals, Bool, String etc.
- Some ways to build up types
- Products (tuples), records, lists
- Functions
- Design Principle Orthogonality
- Dont clutter core language with stuff
- Few, powerful orthogonal building techniques
- Put derived types, values, functions in
libraries
- Well see this in a couple of weeks
3Today Building datatypes
We have seen 2 of these before ? Hmm ?
- Three key ways to build complex types/values
- 1. Each-of types
- Value of T contains value of T1 and a value of T2
- 2. One-of types
- Value of T contains value of T1 or a value of T2
- 3. Recursive
- Value of T contains (sub)-value of same type T
type
type
tuples,records (2, ranjit) int string, int
and a string
type
type
type
Today the power of these concepts
type
Lists! 1,2,3,4,5 int list, but this is
12,3,4,5
----------- int list!
4Suppose I wanted
- a program that processed lists of attributes
- Name (string)
- Age (integer)
- DOB (int-int-int)
- Address (string)
- Height (real)
- Alive (boolean)
- Phone (int-int)
- email (string)
- Many kinds of attributes
- too many to put in a record
- can have multiple names, addresses, phones,
emails etc. - Want to store them in a list. Can I ?
but but lists must have uniform type!
5Constructing Datatypes
- t is a new datatype.
- A value of type t is either
- a value of type t1 placed in a box labeled
C1 - Or a value of type t2 placed in a box labeled
C2 - Or
- Or a value of type tn placed in a box labeled
Cn
datatype t C1 of t1 C2 of t2 Cn of tn
Labels called datatype constructors
v1t1
v2t2
vntn
C1
C2
Cn
6Suppose I wanted
Type Name
- Attributes
- Name (string)
- Age (integer)
- DOB (int-int-int)
- Address (string)
- Height (real)
- Alive (boolean)
- Phone (int-int)
- email (string)
datatype attrib Name of string Age of
int DOB of intintint Address of string
Height of real Alive of bool Phone of
intint Email of string
Datatype Constructor
7Creating Values
SHOW CODE
- How to create values of type attrib ?
- val a1 Name Ranjit
- val x Name Ranjit attrib
- - val a2 Height 5.83
- val a2 Height 5.83 attrib
- - val year 1977
- val year 1977 int
- - val a3 DOB (9,8,year)
- val a3 DOB (9,8,1977) attrib
- - val a_l a1,a2,a3
- val a3 attrib list
datatype attrib Name of string Age of
int DOB of intintint Address of string
Height of real Alive of bool Phone of
intint Email of string
Ranjit
5.83
(9,8,1977)
Name
Height
DOB
8One-of types
- Weve defined a one-of type named attrib
- Elements are one of
- String,
- int,
- intintint,
- real,
- bool
- Can create uniform attrib lists
- Suppose I want a function to print attribs
datatype attrib Name of string Age of
int DOB of intintint Address of string
Height of real Alive of bool Phone of
intint Email of string
9How to tell whats in the box ?
case e of Name s gt e1 Age i gt e2 DOB
(m,d,y) gt e3 Address addr gt e4 Height h gt
e5 Alive b gt e6 Phone (a,n) gt e7 Email e
gt e8
datatype attrib Name of string Age of
int DOB of intintint Address of string
Height of real Alive of bool Phone of
intint Email of string
- Pattern-match expression check if e is of the
form - On match
- value in box bound to pattern variable
- corresponding result expression is evaluated
- Simultaneously test and extract contents of box
10Case-of is an Expression
SHOW PRINT CODE
Ranjit
case e of Name s gt e1 Age i gt e2 DOB
(d,m,y) gt e3 Address addr gt e4 Height h gt
e5 Alive b gt e6 Phone (a,n) gt e7 Email e
gt e8
Name
S ? to Ranjit e1 evaluated (with binding)
(9,8,1977)
d ? 9, m ? 8, y ? 1977 e3 evaluated (with
binding)
DOB
5.83
Height
H ? 5.83 e5 evaluated (with binding)
- Pattern-match expression check if e is of the
form - On match
- value in box bound to pattern variable
- corresponding result expression is evaluated
- Simultaneously test and extract contents of box
11Case-of is an Expression
case e of C1 x1 gt e1 C2 x2 gt e2 Cn
xn gt en
T
T
T
T
- Type rules ?
- e1, e2,,en must have same type
- Which is type of whole expression
e1 T e2 T en T case e of T
12Benefits of case-of
datatype t C1 of t1 C2 of t2 Cn of
tn
case e of C1 x1 gt e1 C2 x2 gt e2 Cn
xn gt en
- 1. Simultaneous test-extract-bind
- 2. Compile-time checks for
- missed cases ML warns if you miss a t value
- redundant cases ML warns if a case never matches
13What about Recursive types ?
SHOW LIST CODE
datatype int_list Nil Cons of int
int_list
- Think about this! What are values of int_list ?
Cons(2,Cons(3,Nil))
Cons(1,Cons(2,Cons(3,Nil)))
Nil
Cons(3,Nil)
14Lists arent built-in !
Recursion!
datatype int_list Nil Cons of int
int_list
One-of
Each-of
- Lists are a derived type, built using elegant
core! - 1. Each-of
- 2. One-of
- 3. Recursive
- is just a pretty way to say Cons
- is just a pretty way to say Nil
Syntactic Sugar
15Some functions on Lists Length
SHOW Len CODE
fun len l case l of Nil gt 0
Cons(h,t) gt 1 (len t)
Base Expression
Base pattern
Not really used Why bind?
Inductive Expression
Ind pattern
fun len l case l of Nil gt 0
Cons(_,t) gt 1 (len t)
fun len l case l of Cons(_,t) gt 1
(len t) _ gt 0
- Matches everything, no binding
Pattern-matching in order - Must match with Nil
16Some functions on Lists Append
case l1 of Nil gt l2 Cons(h,t) gt
Cons(h,append (t,l2))
fun append (l1,l2)
Base Expression
Base pattern
Ind pattern
Inductive Expression
- Find the right induction strategy
- Base case pattern expression
- Induction case pattern expression
- Well designed datatype gives strategy
17null, hd, tl are all functions
SHOW CODE
- Bad ML style More than aesthetics !
- Pattern-matching better than test-extract
- ML checks all cases covered
- ML checks no redundant cases
- at compile-time
- fewer errors (crashes) during execution
- get the bugs out ASAP!
18Another Example Calculator
SHOW CODE
- We want an arithmetic calculator to
- evaluate expressions like
- 4.0 2.9
- 3.78 5.92
- (4.0 2.9) (3.78 -5.92)
- Whats a ML datatype for such expressions ?
Mul
Add (Real 4.0, Real 2.9)
6.9
-2.14
Sub (Real 3.78, Real 5.92)
-14.766
What is an expression ? Real Or exp
exp Or exp exp Or exp exp
datatype expr Real of real Add of expr
expr Sub of expr expr Mul of expr expr
19Another Example Calculator
SHOW CODE
- We want an arithmetic calculator to
- evaluate expressions like
- 4.0 2.9
- 3.78 5.92
- (4.0 2.9) (3.78 -5.92)
- Whats a ML function for evaluating such
expressions ?
6.9
-2.14
-14.766
Fun eval e case e of Real r gt r
Add (e1,e2) gt eval e1 eval e2 Sub (e1,e2) gt
eval e1 eval e2 Mul (e1,e2) gt eval e1 eval
e2
20Random Art from Expressions
- PA 1.
- Build more funky expressions, evaluate them, to
produce
21Functions
Values
Expressions
Types
Functioning recursively in ML
22Example Factorial
fun fac n if n0 then 1 else n fac
(n-1)
Induction Condition
Base Expression
Inductive Expression
23Example Clone
fun clone (x,n) if n0 then else x
clone(x,n-1)
Induction Condition
Base Expression
Inductive Expression
24Example interval
fun interval (i,j) if i gt j then
else i interval(i1,j)
Induction Condition
Base Expression
Inductive Expression
25Example List Maximum
Find maximum element in ve int list
fun max (x,y) if x gt y then x else y fun
listMax l let fun helper (cur,l)
if (null l) then else helper
(max(cur,hd l),tl l) in helper (0, l)
end
Induction Condition
Base Expression
Inductive Expression
26Example List Maximum
Find maximum element in ve int list in a more
ML-ish way
fun max (x,y) if x gt y then x else y fun
listMax l let fun helper (_,)
helper (cur,ht) helper(max(cur,h),t) in
helper (0, l) end
Base pattern
Base Expression
Inductive Expression
Ind. pattern
27Example List Append
Roll our own _at_
Base pattern
fun append (,l) l append (ht,l)
h(append (t,l))
Base Expression
Inductive Expression
Ind. pattern
28Example List Filter
Base pattern
fun filter (f,) filter (f,ht)
let val t filter (f,t) in if
(f h) then ht else t end
Base Expression
Ind. pattern
Inductive Expression
29Some functions on Lists Append
fun append (l1,l2) case l1 of Nil gt
Nil Cons(h,t) gt Cons(h,Append (t,l2))
Base Expression
Base pattern
Ind pattern
Inductive Expression
case l1 of Nil gt Nil Cons(h,t) gt
Cons(h,append (t,l2))
fun len l case l of Nil gt 0
Cons(_,t) gt 1 (len t)
fun len l case l of Cons(_,t) gt 1
(len t) _ gt 0
- Matches everything, no binding
Pattern-matching in order - Must match with Nil