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

Slides:



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

Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial 19 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 5 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 15 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CS1Q Computer Systems Lecture 14
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
More Set Definitions and Proofs 1.6, 1.7. Ordered n-tuple The ordered n-tuple (a1,a2,…an) is the ordered collection that has a1 as its first element,
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
5/11/2015IT 3271 Types in ML (Ch 11) datatype bool = true | false; datatype 'element list = nil | :: of 'element * 'element list n Predefined, but not.
Type Checking.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
Disjoint Unions (quick review)
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Composite types Cartesian products –tuples, records, structures disjoint unions –union, discriminated or variant records mappings –arrays, functions recursive.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Lecture notes, exercises, etc., can be found at:
CS 454 Theory of Computation Sonoma State University, Fall 2011 Instructor: B. (Ravi) Ravikumar Office: 116 I Darwin Hall Original slides by Vahid and.
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
Tuples and Lists Lecture 3, Programmeringsteknik del A.
Computing Science 1P Lecture 21: Friday 20 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
CS1Q Computer Systems Lecture 8
Michaelmas Term 2004 Discrete Mathematics CSC 141 Discrete Mathematics Dr. Corina Sas and Ms. Nelly Bencomo
CSE S. Tanimoto Introduction to ML 1 Introduction to ML History Special features Interacting with ML ML’s basic types ML’s composite types Math.
1 Functional Programming Lecture 6 - Algebraic Data Types.
F28PL1 Programming Languages Lecture 13: Standard ML 3.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Computing Science 1P Lecture 14: Friday 2 nd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 6 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CS1Q Computer Systems Lecture 8
Types and Programming Languages
Types and Programming Languages Lecture 12a Simon Gay Department of Computing Science University of Glasgow 2006/07.
CMSC 330: Organization of Programming Languages Operational Semantics.
Types and Programming Languages Lecture 10 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Lesson 5 Simple extensions of the typed lambda calculus
Types CSCE 314 Spring 2016.
ML: a quasi-functional language with strong typing
Chapter 2: Data Abstraction 2
A lightening tour in 45 minutes
Expanded Recursive Diagrams OCAML rapid tour, day 2
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
CSE S. Tanimoto Introduction to ML
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
CSE S. Tanimoto Introduction to ML
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
CSE S. Tanimoto Introduction to ML
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
CSE S. Tanimoto Introduction to ML
Lisp.
Review Previously User-defined data types: records, variants
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Review Functions Lists Today: Let expressions
Terminology and Symbols
Presentation transcript:

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

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)

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.)

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).

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, ( ).

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?

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.

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

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]

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?

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.

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}

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.

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

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