Types and Programming Languages Lecture 7 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 8 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.
CS1Q Computer Systems Lecture 14
Modern Programming Languages, 2nd ed.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
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.
1 How to transform an analyzer into a verifier. 2 OUTLINE OF THE LECTURE a verification technique which combines abstract interpretation and Park’s fixpoint.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus.
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.
MinML: an idealized programming language CS 510 David Walker.
Tim Sheard Oregon Graduate Institute Lecture 11: A Reduction Semantics for MetaML CS510 Section FSC Winter 2005 Winter 2005.
Proof by Deduction. Deductions and Formal Proofs A deduction is a sequence of logic statements, each of which is known or assumed to be true A formal.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Functional Programming Element of Functional Programming.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Lesson 4 Typed Arithmetic Typed Lambda Calculus 1/21/02 Chapters 8, 9, 10.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
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.
Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Chapter SevenModern Programming Languages1 A Second Look At ML.
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
Types and Programming Languages
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
Types and Programming Languages Lecture 12a Simon Gay Department of Computing Science University of Glasgow 2006/07.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
Types and Programming Languages Lecture 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CMSC 330: Organization of Programming Languages Operational Semantics.
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.
Types and Programming Languages Lecture 16 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Operational Semantics of Scheme
CS5205: Foundations in Programming Languages
Programming Languages and Compilers (CS 421)
Lambda Calculus CSE 340 – Principles of Programming Languages
CSE-321 Programming Languages Simply Typed -Calculus
A lightening tour in 45 minutes
More on Lambda Calculus
CS 611: Lecture 9 More Lambda Calculus: Recursion, Scope, and Substitution September 17, 1999 Cornell University Computer Science Department Andrew Myers.
PROGRAMMING IN HASKELL
Corky Cartwright January 18, 2017
PROGRAMMING IN HASKELL
Lesson 4 Typed Arithmetic Typed Lambda Calculus
Mini Language Interpreter Programming Languages (CS 550)
Typed Arithmetic Expressions
Functional Programming
Announcements Quiz 6 HW7 due Tuesday, October 30
PROGRAMMING IN HASKELL
Lecture 10: Fixed Points ad Infinitum M.C. Escher, Moebius Ants
CSE S. Tanimoto Lambda Calculus
Presentation transcript:

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

Types and Programming Languages Lecture 7 - Simon Gay2 Lambda Calculus with Types We introduce function types: A B is the type of functions with a parameter of type A and a result of type B. Types are defined by this grammar: T ::= int | bool | T T By convention, associates to the right, so that A B C means A (B C). Example int int int curried function of two arguments (int int) int function which is given a function

2006/07Types and Programming Languages Lecture 7 - Simon Gay3 Typing Rules for Functions To make it easier to define the typing rules, we will modify the syntax so that a -abstraction explicitly specifies the type of its parameter. v ::= integer literal | true | false | x:T.e e ::= v | x | e + e | e == e | e & e | if e then e else e | ee T ::= int | bool | T T

2006/07Types and Programming Languages Lecture 7 - Simon Gay4 Typing Rules for Functions The rules are similar to those in SFL, but simpler because we have function types. (T-Abs) (T-App) We also need the typing rules for SEL, with environments added. The resulting system is called the simply typed lambda calculus. (A broad term: anything with T-Abs, T-App, and at least one base type or ground type such as int or bool.)

2006/07Types and Programming Languages Lecture 7 - Simon Gay5 Simply Typed Lambda Calculus true : bool false : bool v : intif v is an integer literal (T-Plus)(T-And) (T-Eq)(T-If) (T-Var) (T-Abs) (T-App)

2006/07Types and Programming Languages Lecture 7 - Simon Gay6 Examples

2006/07Types and Programming Languages Lecture 7 - Simon Gay7 Termination If a typable closed term of the simply typed lambda calculus is executed, it is guaranteed to terminate. If e:T then for some value v, e * v. (See Pierce chapter 12 for a proof.) This result is usually called strong normalization and it is true no matter which reduction strategy is used. It might seem surprising, because we know that recursion can be expressed in the lambda calculus (and we have even seen a non-terminating reduction sequence). The answer is that any term containing xx is untypable.

2006/07Types and Programming Languages Lecture 7 - Simon Gay8 xx is untypable If xx were typable, with some type T, then there would be a derivation of x:U xx :T for some type U. What would this derivation look like? We can see that U must be of the form U T but this is not possible (unless we have recursive types, which are coming later).

2006/07Types and Programming Languages Lecture 7 - Simon Gay9 Recursion in simply typed lambda calculus If we want to use the fixed point operator Y to express recursive function definitions in the simply typed lambda calculus then we must introduce it as a new kind of term with its own reduction and typing rules. In fact we need a collection of Ys, one for each type. To improve readability well call them fix. (T-Fix) (R-Fix) where x is not free in v Exercise: why is R-Fix like this, instead of ? And why are we only interested in fix at function types?

2006/07Types and Programming Languages Lecture 7 - Simon Gay10 Recursion in simply typed lambda calculus Example f = x:int.if x==0 then 1 else x*f(x-1) f:int int. x:int.if x==0 then 1 else x*f(x-1) Given a recursive function definition: f(x:int) = if x==0 then 1 else x*f(x-1) we can rewrite it as a recursively-defined -abstraction: then abstract again on the function name: this term has type (int int) (int int) and we use the appropriate fix to get the function:

2006/07Types and Programming Languages Lecture 7 - Simon Gay11 Naming The lambda calculus does not seem to give a direct way of expressing programs in the SFL style: a sequence of named function definitions followed by an expression. But we do have a way of associating a term with a name: -abstraction and function application. Given the program f(x:int):int = body-f g(x:int):int = body-g e we can construct -terms F and G corresponding to f and g, then form (( f:int int. g:int int.e)F)G so that f and g can be used within e, and F and G are only written once.

2006/07Types and Programming Languages Lecture 7 - Simon Gay12 Naming We prefer to work more directly with a programmer-friendly syntax. The idea is to write let f(x:int):int = body-f g(x:int):int = body-g in e end and we can define a typing rule for let: (T-Let) Semantically we regard let x = e in e as an abbreviation for ( x:T.e)e.

2006/07Types and Programming Languages Lecture 7 - Simon Gay13 Let and Multiple Definitions A let with several definitions can be regarded as an abbreviation: let f(x:int):int = body-f g(x:int):int = body-g in e end let f(x:int):int = body-f in let g(x:int):int = body-g in e end

2006/07Types and Programming Languages Lecture 7 - Simon Gay14 Let and Recursive Definitions We want to allow let definitions to be recursive, intending that recursive function definitions will be translated into the equivalent of fix expressions. Theres a small issue: let allows us to define values as well as functions, but we dont want to allow recursively-defined values (why not?) A convenient solution is to distinguish between value and function definitions by using the keywords val and fun. let fun f(x:int):int = body-f fun g(x:int):int = body-g val a:int = 1 in e end

2006/07Types and Programming Languages Lecture 7 - Simon Gay15 Simply Typed Lambda Calculus is Safe It is straightforward to prove type preservation and type safety theorems for the simply typed lambda calculus, in the same way as for the Simple Functional Language. The same is true when we add recursion (using fix). Exercise: prove type preservation and type safety theorems for the simply typed lambda calculus.

2006/07Types and Programming Languages Lecture 7 - Simon Gay16 A Better Functional Language Combining the ideas so far (function types, let for introducing names, fun and val to distinguish between function definitions (can be recursive) and value definitions (cant be recursive)), introducing more convenient syntax for (fn x:T => e instead of x:T.e), and using = instead of is in function definitions, we get a BFL. BFL looks very much like a small subset of Standard ML. Multi-argument functions must be curried. An implementation of a typechecker for BFL can be found on the course web page, together with a commentary.

2006/07Types and Programming Languages Lecture 7 - Simon Gay17 Reading Pierce: 9; 12 (more challenging) for the proof of termination. Exercises Pierce: 9.2.1, 9.2.2, 9.2.3, 9.3.9, ,