1 / 15

Types and Programming Languages

Types and Programming Languages. Lecture 8. Simon Gay Department of Computing Science University of Glasgow. 2006/07. 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

nerina
Download Presentation

Types and Programming Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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) Types and Programming Languages Lecture 8 - Simon Gay

  3. 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.) 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 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). Types and Programming Languages Lecture 8 - Simon Gay

  5. General Product Types More generally we can consider types of the form whose values, generally are 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, ( ). Types and Programming Languages Lecture 8 - Simon Gay

  6. 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. The are the field names of this record type. (T-Record) (T-Field) Language design choice: is the order of the fields significant? 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). and so on; also is a value. A product type can be regarded as a record type in which the field labels are 1, 2, … Types and Programming Languages Lecture 8 - Simon Gay

  8. 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 Types and Programming Languages Lecture 8 - Simon Gay

  9. Sum Types Exercises: 1. What are the reduction rules for inl, inr and 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] 2. What is the typing rule for case ? Types and Programming Languages Lecture 8 - Simon Gay

  10. Sum Types Recall the example of a variant record in Pascal: type kind = (staff,student); type person = record name : string; case k : kind of staff : (office : string) student : (year : integer) end; How can we express something like this using sum types? 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 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 can’t update fields yet. Types and Programming Languages Lecture 8 - Simon Gay

  12. Practical Sum Types In a practical programming language it’s 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} Types and Programming Languages Lecture 8 - Simon Gay

  13. Practical Sum Types We can think of the type info (below) as string + int + string datatype info = Staff of string | Student of int | Parent of string but to represent the labels (constructors) Staff, Student, Parent we 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 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. 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” 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 Types and Programming Languages Lecture 8 - Simon Gay

More Related