














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
Saturday, April 7, 2012 1
Expressions (^) Values
Types
Saturday, April 7, 2012 2
Next: Building datatypes
Saturday, April 7, 2012 5
Next: Building datatypes
Saturday, April 7, 2012 6
Suppose I wanted … … a program that processed lists of attributes
Many kinds of attributes (too many to put in a record)
Constructing Datatypes
a value of type t1 placed in a box labeled C Or a value of type t2 placed in a box labeled C Or … Or a value of type tn placed in a box labeled Cn
type t = C1 of t1 | C2 of t2 | … | Cn of tn
Constructing Datatypes
“Ranjit”
Name
Age
type attrib = Name of string | Age of int | DOB of intintint | Address of string | Height of float | Alive of bool | Phone of int*int | Email of string; ;
All have type attrib
Name “Ranjit” (^) Age 34 DOB (9,8,77)
Saturday, April 7, 2012 13
One-of types
datatype attrib = Name of string | Age of int | DOB of intintint | Address of string | Height of real | Alive of bool | Phone of int*int | Email of string ;
Saturday, April 7, 2012 14
How to TEST & TAKE whats in box?
Is it a ...
or an
or an
or ...
How to TEST & TAKE whats in box?
Tag Look at TAG!
How to tell whats in the box?
match e with | Name s -> printf "%s" s | Age i -> printf "%d" i | DOB( d , m , y ) -> printf "%d/%d/%d" d m y | Address s -> printf "%s" s | Height h -> printf "%f" h | Alive b -> printf "%b" b s | Phone( a , r ) -> printf "(%d)-%d" a r
Saturday, April 7, 2012 17
How to tell whats in the box?
type attrib = Name of string | Age of int | DOB of _intintint_** | Address of string | Height of float | Alive of bool | Phone of _intint_*
match e with | Name s -> ... _(s: string )_ | Age i -> ... _(i: int )_ | DOB( d,m,y )-> ... _(d: int,m: int,y: int)** | Address a -> ... **(a: string)** | Height h -> ... **(h: int )_ | Alive b -> ... _(b: bool)** | Phone( y,r )-> ... **(a: int, r: int)_**
Saturday, April 7, 2012 18
How to tell whats in the box
None of the cases matched the tag (Name) Causes nasty Run-Time Error
| Name s -‐> printf "Hello %s\n" s | Age i -‐> printf "%d years old" i ;;
Hello Ranjit -‐ : unit = ()
How to TEST & TAKE whats in box?
BEWARE!!
Be sure to
handle all
TAGS!
Tag
Benefits of match-with
type t = C1 of t | C2 of t | … | Cn of tn
match e with C1 x1 -> e | C2 x2 -> e | … | Cn xn -> en
Saturday, April 7, 2012 25
match-with is an Expression
match e with C1 x1 -> e | C2 x2 -> e | … | Cn xn -> en
match e with Name s -> e | Age i -> e | DOB (m,d,y) -> e | Address a -> e | Height h -> e | Alive b -> e | Phone (a,n) -> e | Email e -> e
T T
match-with is an Expression Next: Building datatypes
“Recursive” types
type nat = Zero | Succ of nat
Saturday, April 7, 2012 29
“Recursive” types
type nat = Zero | Succ of nat
Saturday, April 7, 2012 30
“Recursive” types
type nat = Zero | Succ of nat
“Recursive” types
type nat = Zero | Succ of nat
What are values of nat?
“Recursive” types
What are values of nat?
type nat = Zero | Succ of nat
Zero
Succ
Succ
Succ
One nat contains another!
Saturday, April 7, 2012 37
Next: Building datatypes
Saturday, April 7, 2012 38
Next: Lets get cosy with Recursion
Next: Lets get cosy with Recursion
Code Structure = Type Structure!!!
to_int : nat -> int
let rec to_int n =
type nat = | Zero | Succ of nat
Saturday, April 7, 2012 41
to_int : nat -> int
let rec to_int n =
type nat = | Zero | Succ of nat
Base pattern Inductive pattern
Saturday, April 7, 2012 42
to_int : nat -> int
let rec to_int n = match n with | Zero -> | Succ m ->
type nat = | Zero | Succ of nat
Base pattern Inductive pattern
Base pattern Inductive pattern
to_int : nat -> int
let rec to_int n = match n with | Zero -> 0 | Succ m -> 1 + to_int m
type nat = | Zero | Succ of nat
Base pattern Inductive pattern
Base Expression Inductive Expression
Base pattern Inductive pattern
plus : nat*nat -> nat
type nat = | Zero | Succ of nat
let rec plus (n,m) =
Saturday, April 7, 2012 49
plus : nat*nat -> nat
type nat = | Zero | Succ of nat
let rec plus (n,m) =
Base pattern Inductive pattern
Saturday, April 7, 2012 50
plus : nat*nat -> nat
type nat = | Zero | Succ of nat
let rec plus (n,m) = match m with | Zero -> | Succ m’ ->
Base pattern Inductive pattern
Base pattern Inductive pattern
plus : nat*nat -> nat
type nat = | Zero | Succ of nat
let rec plus (n,m) = match m with | Zero -> n | Succ m’ -> Succ (plus (n,m’))
Base Expression
Inductive Expression
Base pattern Inductive pattern
Base pattern Inductive pattern
times: nat*nat -> nat
type nat = | Zero | Succ of nat
let rec times (n,m) =
Saturday, April 7, 2012 53
times: nat*nat -> nat
type nat = | Zero | Succ of nat
let rec times (n,m) =
Base pattern Inductive pattern
Saturday, April 7, 2012 54
times: nat*nat -> nat
type nat = | Zero | Succ of nat
let rec times (n,m) = match m with | Zero -> | Succ m’ ->
Base pattern Inductive pattern
Base pattern Inductive pattern
plus : nat*nat -> nat
type nat = | Zero | Succ of nat
let rec times (n,m) = match m with | Zero -> Zero | Succ m’ -> plus n (times (n,m’))
Base Expression
Inductive Expression
Base pattern Inductive pattern
Base pattern Inductive pattern
let rec append (l1,l2) = Base Expression
Inductive Expression
Base pattern
Ind pattern
Saturday, April 7, 2012 61
let rec max xs = Base Expression
Inductive Expression
Base pattern
Ind pattern
Saturday, April 7, 2012 62
null, hd, tl are all functions …
Bad ML style: More than aesthetics!
Pattern-matching better than test-extract:
Next: Lets get cosy with Recursion
Representing Trees
type tree = | Leaf of int | Node of tree*tree
Leaf
Node(Node(Leaf 1 , Leaf 2), Leaf 3)
Saturday, April 7, 2012 65
Representing Trees
type tree = | Leaf of int | Node of tree*tree
Leaf
Node(Node(Leaf 1, Leaf 2 ), Leaf 3)
Saturday, April 7, 2012 66
Representing Trees
type tree = | Leaf of int | Node of tree*tree
Leaf
Node
Leaf
Node(Node(Leaf 1, Leaf 2), Leaf 3)
Representing Trees
type tree = | Leaf of int | Node of tree*tree
Leaf
Node(Node(Leaf 1, Leaf 2), Leaf 3 )
type tree = | Leaf of int | Node of tree*tree
sum_leaf: tree -> int
let rec sum_leaf t =
Saturday, April 7, 2012 73
type tree = | Leaf of int | Node of tree*tree
sum_leaf: tree -> int
let rec sum_leaf t =
Base pattern Inductive pattern
Saturday, April 7, 2012 74
type tree = | Leaf of int | Node of tree*tree
sum_leaf: tree -> int
let rec sum_leaf t = match t with |Leaf n -> |Node(t1,t2)->
Base pattern nductive pattern
Base pattern nductive pattern
type tree = | Leaf of int | Node of tree*tree
sum_leaf: tree -> int
let rec sum_leaf t = match t with |Leaf n -> n |Node(t1,t2)-> sum_leaf t1 + sum_leaf t
Base Expression
Inductive Expression
Base pattern Inductive pattern
Base pattern Inductive pattern
Saturday, April 7, 2012 77
Another Example: Calculator Want an arithmetic calculator to evaluate expressions like:
Saturday, April 7, 2012 78
Another Example: Calculator Want an arithmetic calculator to evaluate expressions like:
Whats a ML TYPE for REPRESENTING expressions?
Another Example: Calculator
type expr = | Num of float | Add of exprexpr | Sub of exprexpr | Mul of expr*expr
Want an arithmetic calculator to evaluate expressions like:
Whats a ML TYPE for REPRESENTING expressions?