Types and Programming Languages Lecture 8 Simon Gay
- Slides: 15
Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07 Types and Programming Languages Lecture 8 - Simon Gay
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) 2006/07 (T-Snd) Types and Programming Languages Lecture 8 - Simon Gay 2
Product Types The reduction rules for products are straightforward. (R-Pair. L) fst (v, w) v (R-Fst. Pair) (R-Fst) (R-Pair. R) snd (v, w) w (R-Snd. Pair) (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. ) 2006/07 Types and Programming Languages Lecture 8 - Simon Gay 3
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. 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). 2006/07 Types and Programming Languages Lecture 8 - Simon Gay 4
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, ( ). 2006/07 Types and Programming Languages Lecture 8 - Simon Gay 5
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? 2006/07 Types and Programming Languages Lecture 8 - Simon Gay 6
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, … 2006/07 Types and Programming Languages Lecture 8 - Simon Gay 7
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 2006/07 Types and Programming Languages Lecture 8 - Simon Gay 8
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 ? 2006/07 Types and Programming Languages Lecture 8 - Simon Gay 9
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? 2006/07 Types and Programming Languages Lecture 8 - Simon Gay 10
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(“G 093”)} {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. 2006/07 Types and Programming Languages Lecture 8 - Simon Gay 11
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} 2006/07 Types and Programming Languages Lecture 8 - Simon Gay 12
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. 2006/07 Types and Programming Languages Lecture 8 - Simon Gay 13
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” 2006/07 Types and Programming Languages Lecture 8 - Simon Gay 14
Reading Pierce: 11 Exercises Pierce: 11. 5. 2, 11. 9. 1, 11. 11. 2 Exercise sheet 4 2006/07 Types and Programming Languages Lecture 8 - Simon Gay 15
- Bakla typings
- Strongly typed vs weakly typed
- Integral data type in c
- Real-time systems and programming languages
- Elsa gunter uiuc
- Advantages and disadvantages of system software
- Real-time systems and programming languages
- Cs 421 programming languages and compilers
- 01:640:244 lecture notes - lecture 15: plat, idah, farad
- Real time example of multithreading in java
- Cxc it
- Introduction to programming languages
- Plc coding language
- Procedural programming languages
- Comparative programming languages
- Alternative programming languages