Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of...

15
Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07

Transcript of Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of...

Page 1: Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.

Types and Programming Languages

Lecture 8

Simon GayDepartment of Computing Science

University of Glasgow

2006/07

Page 2: Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Types and Programming Languages Lecture 8 - Simon Gay 2

Product Types

Structured data types are useful for programming. The simplestare product types.

If T and U are types then TU is the type of pairs whose firstcomponent has type T and whose second component has type U.

If we think of a type as defining a set of values, then this is justcartesian product of sets.

UTfeUfTe

:),(

::

(T-Pair)

Te fstUTe:

:

(T-Fst)Ue sndUTe:

:

(T-Snd)

Page 3: Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Types and Programming Languages Lecture 8 - Simon Gay 3

Product Types

The reduction rules for products are straightforward.

),'(),('

fefeee

(R-PairL))',(),(

'evev

ee

(R-PairR)

fst (v,w) v (R-FstPair) snd (v,w) w (R-SndPair)

''

e fste fstee

(R-Fst)'

'e snde snd

ee

(R-Snd)

We extend the definition of value so that a pair (v,w) is a value ifv and w are values.

(These are call by value rules.)

Page 4: Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Types and Programming Languages Lecture 8 - Simon Gay 4

Product Types and Functions

Product types almost let us define functions of two arguments,without the need for currying.

fun f(x:int*int):int = (fst x) + (snd x)

The type of f is intintint .

Note that we are not yet able to write

fun f(x:int,y:int):int = x + y

which requires pattern matching (we might look at this later).

Page 5: Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Types and Programming Languages Lecture 8 - Simon Gay 5

General Product Types

More generally we can consider types of the form

nTTT 21

whose values, generally ),,,( 21 neee are called tuples.

The typing rules are generalizations of the rules for pairs. Weneed a general collection of projection operators instead of justfst and snd. In Standard ML these are #1, #2, …

Eg: #3 (1,true,2,(2,false)) 2

The case n=0 also makes sense: we get the type unit which hasjust one value, ( ).

Page 6: Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Types and Programming Languages Lecture 8 - Simon Gay 6

Record Types

A record type is a product type in which the components arelabelled so that they can be accessed by name instead of byposition.

}:,,:,:{ 2211 nn TmTmTm

The im are the field names of this record type.

}:,,:{:},,{::

1111

11

nnnn

nnTmTmemem

TeTe

(T-Record)

ii

nnTem

TmTme:#

}:,,:{: 11

(T-Field)

Language design choice: is the order of the fields significant?

Page 7: Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Types and Programming Languages Lecture 8 - Simon Gay 7

Record Types

The reduction rules for records are similar to those for products(exercise: write them down).

A product type can be regarded as a record type in which thefield labels are 1, 2, …

},,'{},,{'

11 nnnn ememememee

},,',{},,,{'

2121 nnnn ememvmememvmee

and so on; also

'##'

ememee

ii

inni vvmvmm },,{# 11

},,{ 11 nn vmvm is a value.

Page 8: Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Types and Programming Languages Lecture 8 - Simon Gay 8

Sum Types

Sum types are sometimes called disjoint union types ordiscriminated union types.

If T and U are types then T+U is the type whose values areeither values of type T or values of type U, tagged to indicatewhich type they belong to.

UT)einl(Te

::

(T-Left)UT)einr(

Ue

::

(T-Right)

Given an expression e of type T+U, we can process it by using acase construct:

case e of inl(x) => f | inr(x) => g

Page 9: Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Types and Programming Languages Lecture 8 - Simon Gay 9

Sum Types

Exercises:

1. What are the reduction rules for inl, inr and case ?

2. What is the typing rule for case ?

inl(v) and inr(v) are values.

)'()('

einleinlee

)'()('

einreinree

of e' case of e caseee

'

case inl(v) of inl(x)=>f | inr(x)=>g f[v/x]

case inr(v) of inl(x)=>f | inr(x)=>g g[v/x]

Tginr(x) | finl(x) of e caseTgBxTfAxBAe:::,::,:

Page 10: Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Types and Programming Languages Lecture 8 - Simon Gay 10

Sum Types

type kind = (staff,student);type person =

recordname : string;case k : kind of

staff : (office : string)student : (year : integer)

end;

Recall the example of a variant record in Pascal:

How can we express something like this using sum types?

Page 11: Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Types and Programming Languages Lecture 8 - Simon Gay 11

Sum Types and Variant Records

A record type has a fixed set of fields, so we can’t have bothoffice and year. But we can use a field details with typestring + int.

type person = {name:string, details:string+int}

Example values of type person:

{name = “Simon”, details = inl(“G093”)}

{name = “Fred”, details = inr(2)}

Using a value:

case #details(p) of inl(x) => “Staff” | inr(x) => “Student”

Unlike Pascal, we can’t update fields yet.

Page 12: Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Types and Programming Languages Lecture 8 - Simon Gay 12

Practical Sum Types

In a practical programming language it’s useful to allow sums ofmore than two types with programmer specified constructornames. Example (Standard ML):

datatype info = Staff of string| Student of int| Parent of string

For more information we can use records:

datatype info = Staff of {office:string}| Student of {year:int}| Parent of {student:string}

Page 13: Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Types and Programming Languages Lecture 8 - Simon Gay 13

Practical Sum Types

datatype info = Staff of string| Student of int| Parent of string

We can think of the type info (below) as string + int + string

but to represent the labels (constructors) Staff, Student, Parentwe need to view it as a variant type:

< Staff:string, Student:int, Parent:string >

a value of this type is of the form Staff(s) where s is a value oftype string, or Student(s) where s is a value of type int, orParent(s) where s is a value of type string.

Page 14: Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Types and Programming Languages Lecture 8 - Simon Gay 14

Programming with Sum Types

Example

fun message(x:info):string = case x of Staff(y) => “staff member” | Student(y) => “student” | Parent(y) => “parent”

message(Staff(“Simon”)) * “staff member”

Page 15: Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Types and Programming Languages Lecture 8 - Simon Gay 15

Reading

Pierce: 11

Exercises

Pierce: 11.5.2, 11.9.1, 11.11.1, 11.11.2

Exercise sheet 4