Docsity

Datatypes - Programming Language - Lecture Slides, Slides of Computer Engineering and Programming

Toggle sidebar
bg1
CSE 130 : Spring 2012
Programming Languages
Lecture 3: Datatypes
1
Saturday, April 7, 2012
Expressions Val ue s
Types
Many kinds of expressions:
1. Simple
2. Vari able s
3. Functions
What about more complex data ?
2
Saturday, April 7, 2012
What about more complex data ?
•We’ve seen some base types and values:
– Integers, Floats, Bool, String etc.
• Some ways to build up types:
–Product s (tuples), records, “l ists”
–Functions
• Design Principle: Orthogonality
–Don’t clutter core language with stuff
– Few, powerful orthogonal building techniques
–Put “derived” types, values, functions in libraries
3
Saturday, April 7, 2012
What about more complex data ?
•We’ve seen some base types and values:
– Integers, Floats, Bool, String etc.
• Some ways to build up types:
–Product s (tuples), records, “l ists”
–Functions
4
Saturday, April 7, 2012
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Datatypes - Programming Language - Lecture Slides and more Slides Computer Engineering and Programming in PDF only on Docsity!

CSE 130 : Spring 2012

Programming Languages

Lecture 3: Datatypes

Saturday, April 7, 2012 1

Expressions (^) Values

Types

Many kinds of expressions:

1. Simple

2. Variables

3. Functions

What about more complex data?

Saturday, April 7, 2012 2

What about more complex data?

  • We’ve seen some base types and values:
    • Integers, Floats, Bool, String etc.
  • Some ways to build up types:
    • Products (tuples), records, “lists”
    • Functions
  • Design Principle: Orthogonality
    • Don’t clutter core language with stuff
    • Few, powerful orthogonal building techniques
    • Put “derived” types, values, functions in libraries

What about more complex data?

  • We’ve seen some base types and values:
    • Integers, Floats, Bool, String etc.
  • Some ways to build up types:
    • Products (tuples), records, “lists”
    • Functions

Next: Building datatypes

Three key ways to build complex types/values

1. “Each-of” types

Value of T contains value of T1 and a value of T

2. “One-of” types

Value of T contains value of T1 or a value of T

3. “Recursive”

Value of T contains (sub)-value of same type T

Saturday, April 7, 2012 5

Next: Building datatypes

Three key ways to build complex types/values

1. “Each-of” types (T1 * T2)

Value of T contains value of T1 and a value of T

2. “One-of” types

Value of T contains value of T1 or a value of T

3. “Recursive”

Value of T contains (sub)-value of same type T

Saturday, April 7, 2012 6

Suppose I wanted … … a program that processed lists of attributes

  • Name (string)
  • Age (integer)
  • DOB (int-int-int)
  • Address (string)
  • Height (float)
  • 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?

Constructing Datatypes

t is a new datatype.

A value of type t is either:

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

OR

Age

OR

DOB

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

  • We’ve defined a^ “one-of”^ type named^ attrib
  • Elements are one of:
    • string,
    • int,
    • intintint,
    • float,
    • bool …
  • Can create uniform attrib lists
  • Say 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 int*int | Email of string ;

Saturday, April 7, 2012 14

How to TEST & TAKE whats in box?

Is it a ...

string?

or an

int?

or an

intintint?

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

Pattern-match expression: check if e is of the form …

  • On match:
    • value in box bound to pattern variable
    • matching result expression is evaluated
  • Simultaneously test and extract contents of box

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_*

Pattern-match expression: check if e is of the form …

  • On match:
    • value in box bound to pattern variable
    • matching result expression is evaluated
  • Simultaneously test and extract contents of box

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

match (Name “Ranjit”) with

| 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

  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

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

Type Rule

  • (^) e1 , e2,…,en must have same type T
  • (^) Type of whole expression is T Saturday, April 7, 2012 26

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

Type Rule

  • (^) e1 , e2,…,en must have same type T
  • (^) Type of whole expression is T

match-with is an Expression Next: Building datatypes

Three key ways to build complex types/values

1. “Each-of” types t1 * t

Value of T contains value of T1 and a value of T

2. “One-of” types type t = C1 of t1 | C2 of t

Value of T contains value of T1 or a value of T

3. “Recursive” type

Value of T contains (sub)-value of same type T

“Recursive” types

type nat = Zero | Succ of nat

Saturday, April 7, 2012 29

“Recursive” types

type nat = Zero | Succ of nat

Wait a minute! Zero of what ?!

Saturday, April 7, 2012 30

“Recursive” types

type nat = Zero | Succ of nat

Wait a minute! Zero of what ?!

Relax.

Means “empty box with label Zero ”

“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!

nat = recursive type

Saturday, April 7, 2012 37

Next: Building datatypes

Three key ways to build complex types/values

1. “Each-of” types t1 * t

Value of T contains value of T1 and a value of T

2. “One-of” types type t = C1 of t1 | C2 of t

Value of T contains value of T1 or a value of T

3. “Recursive” type type t = ...| C of (...*t)

Value of T contains (sub)-value of same type T

Saturday, April 7, 2012 38

Next: Lets get cosy with Recursion

Recursive Code Mirrors Recursive Data

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

Some functions on Lists : Append

let rec append (l1,l2) = Base Expression

Inductive Expression

Base pattern

Ind pattern

  • Find the right induction strategy
    • Base case: pattern + expression
    • Induction case: pattern + expression

Well designed datatype gives strategy

Saturday, April 7, 2012 61

Some functions on Lists : Max

let rec max xs = Base Expression

Inductive Expression

Base pattern

Ind pattern

  • Find the right induction strategy
    • Base case: pattern + expression
    • Induction case: pattern + expression

Well designed datatype gives strategy

Saturday, April 7, 2012 62

null, hd, tl are all functions …

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!

Next: Lets get cosy with Recursion

Recursive Code Mirrors Recursive Data

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

Recursive Code Mirrors Recursive Data

Code almost writes itself!

Saturday, April 7, 2012 77

Another Example: Calculator Want an arithmetic calculator to evaluate expressions like:

  • 4.0 + 2.
  • 3.78 – 5.
  • (4.0 + 2.9) * (3.78 -5.92)

Saturday, April 7, 2012 78

Another Example: Calculator Want an arithmetic calculator to evaluate expressions like:

  • 4.0 + 2.9 ====> 6.
  • 3.78 – 5.92 ====> -2.
  • (4.0 + 2.9) * (3.78 -5.92) ====> -14.

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:

  • 4.0 + 2.9 ====> 6.
  • 3.78 – 5.92 ====> -2.
  • (4.0 + 2.9) * (3.78 -5.92) ====> -14.

Whats a ML TYPE for REPRESENTING expressions?