Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07."— Presentation transcript:

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

2 Types and Programming Languages Lecture 8 - Simon Gay2 Product Types Structured data types are useful for programming. The simplest are product types. If T and U are types then T U is the type of pairs whose first component 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 just cartesian product of sets. (T-Pair) (T-Fst)(T-Snd)

3 2006/07Types and Programming Languages Lecture 8 - Simon Gay3 Product Types The reduction rules for products are straightforward. (R-PairL) (R-PairR) fst (v,w) v (R-FstPair)snd (v,w) w (R-SndPair) (R-Fst) (R-Snd) We extend the definition of value so that a pair (v,w) is a value if v and w are values. (These are call by value rules.)

4 2006/07Types and Programming Languages Lecture 8 - Simon Gay4 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 int int int. 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).

5 2006/07Types and Programming Languages Lecture 8 - Simon Gay5 General Product Types More generally we can consider types of the form whose values, generallyare called tuples. The typing rules are generalizations of the rules for pairs. We need a general collection of projection operators instead of just fst 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 has just one value, ( ).

6 2006/07Types and Programming Languages Lecture 8 - Simon Gay6 Record Types A record type is a product type in which the components are labelled so that they can be accessed by name instead of by position. Theare the field names of this record type. (T-Record) (T-Field) Language design choice: is the order of the fields significant?

7 2006/07Types and Programming Languages Lecture 8 - Simon Gay7 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 the field labels are 1, 2, … and so on; also is a value.

8 2006/07Types and Programming Languages Lecture 8 - Simon Gay8 Sum Types Sum types are sometimes called disjoint union types or discriminated union types. If T and U are types then T+U is the type whose values are either values of type T or values of type U, tagged to indicate which type they belong to. (T-Left)(T-Right) Given an expression e of type T+U, we can process it by using a case construct: case e of inl(x) => f | inr(x) => g

9 2006/07Types and Programming Languages Lecture 8 - Simon Gay9 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. 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]

10 2006/07Types and Programming Languages Lecture 8 - Simon Gay10 Sum Types type kind = (staff,student); type person = record name : 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?

11 2006/07Types and Programming Languages Lecture 8 - Simon Gay11 Sum Types and Variant Records A record type has a fixed set of fields, so we cant have both office and year. But we can use a field details with type string + 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 cant update fields yet.

12 2006/07Types and Programming Languages Lecture 8 - Simon Gay12 Practical Sum Types In a practical programming language its useful to allow sums of more than two types with programmer specified constructor names. 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}

13 2006/07Types and Programming Languages Lecture 8 - Simon Gay13 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, Parent we need to view it as a variant type: a value of this type is of the form Staff(s) where s is a value of type string, or Student(s) where s is a value of type int, or Parent(s) where s is a value of type string.

14 2006/07Types and Programming Languages Lecture 8 - Simon Gay14 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

15 2006/07Types and Programming Languages Lecture 8 - Simon Gay15 Reading Pierce: 11 Exercises Pierce: 11.5.2, 11.9.1, 11.11.1, 11.11.2 Exercise sheet 4


Download ppt "Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07."

Similar presentations


Ads by Google