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

Slides:



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

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.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Semantics Static semantics Dynamic semantics attribute grammars
- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Flow-Insensitive Points-to Analysis with Term and Set Constraints Presentation by Kaleem Travis Patrick.
Type checking © Marcelo d’Amorim 2010.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
CS 355 – Programming Languages
Type Checking.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
Axiomatic Semantics Dr. M Al-Mulhem ICS
MinML: an idealized programming language CS 510 David Walker.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535.
Describing Syntax and Semantics
Cormac Flanagan University of California, Santa Cruz Hybrid Type Checking.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
CS1Q Computer Systems Lecture 8
Computing Science 1P Lecture 13: Friday 26 th January Simon Gay Department of Computing Science University of Glasgow 2006/07.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
1 Knowledge Based Systems (CM0377) Lecture 4 (Last modified 5th February 2001)
Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages.
Type Systems CS Definitions Program analysis Discovering facts about programs. Dynamic analysis Program analysis by using program executions.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
CS 363 Comparative Programming Languages Semantics.
Lesson 4 Typed Arithmetic Typed Lambda Calculus 1/21/02 Chapters 8, 9, 10.
11/1/20151 GC16/3011 Functional Programming Lecture 5 Miranda patterns, functions, recursion and lists.
Looping and Counting Lecture 3 Hartmut Kaiser
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Chapter 3 Part II Describing Syntax and Semantics.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Semantic Analysis Semantic Analysis v Lexically and syntactically correct programs may still contain other errors v Lexical and syntax analyses.
Types and Programming Languages Lecture 6 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 12 Simon Gay Department of Computing Science University of Glasgow 2006/07.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.
CS1Q Computer Systems Lecture 6 Simon Gay. Lecture 6CS1Q Computer Systems - Simon Gay2 Algebraic Notation Writing AND, OR, NOT etc. is long-winded and.
Function Definition by Cases and Recursion Lecture 2, Programmeringsteknik del A.
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
CS1Q Computer Systems Lecture 8
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
Types and Programming Languages
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
CSED101 INTRODUCTION TO COMPUTING FUNCTION ( 함수 ) 유환조 Hwanjo Yu.
Types and Programming Languages Lecture 12a Simon Gay Department of Computing Science University of Glasgow 2006/07.
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Types and Programming Languages Lecture 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley.
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.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
White-Box Testing Statement coverage Branch coverage Path coverage
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Language-Based Security: Overview of Types Deepak Garg Foundations of Security and Privacy October 27, 2009.
CS5205: Foundation in Programming Languages Type Reconstruction
Programming Languages and Compilers (CS 421)
Principles of programming languages 8: Types
Structural testing, Path Testing
Stateful Manifest Contracts
Functional Programming
Presentation transcript:

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

Types and Programming Languages Lecture 11 - Simon Gay2 Type Systems for Resource Control We will now look at how type systems used to control not only how data is used but also how many times it is used: in other words, regarding data as a resource. Examples: If we know that the object x will be used exactly once, then after its first use the allocated memory can be reclaimed: compile-time garbage collection. In a lazy language function parameters are evaluated on demand. If we know that f uses its parameter at least once, then we can use call by value, saving the overhead of implementing laziness.

2006/07Types and Programming Languages Lecture 11 - Simon Gay3 Type Systems for Resource Control Examples: In “Linear Types for Packet Processing”, the aim is to ensure that each packet is owned by just one thread. In programming languages for quantum computation, it is important to treat quantum data as a resource: it is not physically possible to duplicate an unknown quantum state. There are type systems which statically control the sequence of operations on a file or other OS resource: e.g. a file must be opened before writing to it. This only works if the resource has a unique owner.

2006/07Types and Programming Languages Lecture 11 - Simon Gay4 Linear Types First we’ll look at a type system in which each value is used exactly once. This is called linear typing. Examples: x.x will be typable x.1 will not be typable x.(x+x) will not be typable Pure linearity is a very severe restriction, and we will see how to make the system more flexible later. and x. y. x and x.(x+2)

2006/07Types and Programming Languages Lecture 11 - Simon Gay5 Linear Types Considering the examples on the previous slide, how can we modify the typing rules of lambda calculus to get this effect?   true : bool   false : bool   v : intif v is an integer literal These rules allow x:T, y:U  v : int in which variables are not used. But we don’t need an environment to type a constant.  true : bool  false : bool  v : intif v is an integer literal STANDARD LINEAR

2006/07Types and Programming Languages Lecture 11 - Simon Gay6 Linear Types This rule allows x:T, y:U  x : T in which variables are not used. But we don’t need extra variables in the environment. STANDARD LINEAR (T-Var) (L-Var) x : T  x : T

2006/07Types and Programming Languages Lecture 11 - Simon Gay7 Linear Types Now we have to think carefully about rules with more than one hypothesis. We want to allow but not Solution: rules of the form LINEAR where  and  must be disjoint and ,  means union.

2006/07Types and Programming Languages Lecture 11 - Simon Gay8 Linear Types Conditional expressions also require careful thought. if c then t else e evaluates either t or e. The only way to consistently describe the exact assumptions of this expression is if t and e use the same resources. L-If LINEAR Using these ideas we can complete the typing rules. Function application is similar to other binary operators. Abstraction is the same as before. It is common to use the symbol –o for linear function type.

2006/07Types and Programming Languages Lecture 11 - Simon Gay9 Lambda Calculus with Linear Types  true : bool  false : bool  v : intif v is an integer literal (L-Plus)(L-And) (L-Eq)(L-If) (L-Abs) (L-App) (L-Var) x : T  x : T

2006/07Types and Programming Languages Lecture 11 - Simon Gay10 Exercises 1.Show complete typing derivations for the following expressions in linear typed lambda calculus: x:int. f:int –o int. f(x) x:int. y:int. z:int. if x==y then z else z+1 How do they differ from typing derivations for the same expressions in simply typed lambda calculus? 2. Prove that if   e:T is derivable in linear typed lambda calculus then it is also derivable in simply typed lambda calculus.

2006/07Types and Programming Languages Lecture 11 - Simon Gay11 Operational Semantics The operational semantics of the lambda calculus with linear types is exactly the same as for the untyped lambda calculus. We have restricted the set of valid expressions, but those expressions which are valid execute in the same way as before. This is an example of one language with two different type systems, designed for different purposes.

2006/07Types and Programming Languages Lecture 11 - Simon Gay12 Type Safety: No Runtime Errors If we are interested in the absence of runtime errors, as before, then we can use a very simple argument to prove that a typed expression of the linear typed lambda calculus executes with no errors. If   e:T in the linear typed lambda calculus then we also have   e:T in the simply typed lambda calculus. Because the operational semantics is the same, we know that e executes without errors. But we want more: the purpose of the linear type system is to guarantee that resources are used exactly once, so we would like to prove that this is the case. We will look at this later.

2006/07Types and Programming Languages Lecture 11 - Simon Gay13 Typechecking with Linear Types A typical linear typing rule such as is not directly suitable for implementation in a typechecker. Reading the rule upwards, we have to guess how to split the given environment into  and . If we are asked to check  ’  e+f : int then how do we work out what  and  should be in order to build a derivation?  should be just the environment needed for e, and  should be the rest. While typechecking we have to examine the structure of e, so we can calculate  at the same time.

2006/07Types and Programming Languages Lecture 11 - Simon Gay14 Typechecking with Linear Types We can formulate the typechecking algorithm as inference rules for a new form of judgement:   e : T   ’ Given  and e, we calculate T and  ’  ’ is the part of  which is not needed in order to type e. Examples: x:int, y:bool  x : int  y:bool x:int, y:int  x+y : int   x:int, y:bool  1 : int  x:int, y:bool

2006/07Types and Programming Languages Lecture 11 - Simon Gay15 Typechecking with Linear Types   true : bool     false : bool     v : int   if v is an integer literal (LT-Plus) (LT-If) (L-Abs) (L-App) (LT-Var) , x : T,  ’  x : T  ,  ’ similarly &, ==

2006/07Types and Programming Languages Lecture 11 - Simon Gay16 Typechecking with Linear Types We want every value to be used exactly once, which means checking that the whole environment is used. To establish   e : T we check that   e : T   i.e. we run the typechecking algorithm and check that the final environment returned is empty. Examples Exercise: convert any of our typecheckers to a linear type system.

2006/07Types and Programming Languages Lecture 11 - Simon Gay17 Correctness of Linear Typechecking We now have two different inference systems. One is the original definition of the linear type system, and the other claims to be a typechecking algorithm. We should check that they agree. It is possible to prove:   e : T   if and only if   e : T Left to right: soundness of the typechecking algorithm Right to left: completeness of typechecking algorithm Proving this is a little complex; we need to prove a suitable generalisation (   e : T   is not strong enough as the I.H.).

2006/07Types and Programming Languages Lecture 11 - Simon Gay18 Beyond Pure Linearity A purely linear type system is very restrictive and in practice we need to regain the full power of lambda calculus. Now that the principles of linear typing are well understood, it is straightforward to combine linear and standard type constructors. The main point to be careful of is that a standard data structure must never contain linear data.