Types and Programming Languages Lecture 6 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 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.
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Substitution & Evaluation Order cos 441 David Walker.
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.
Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS Instructors: Crista Lopes Copyright © Instructors.
Advanced Formal Methods Lecture 2: Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from G. Klein, NICTA.
Foundations of Programming Languages: Introduction to Lambda Calculus
6. Introduction to the Lambda Calculus. © O. Nierstrasz PS — Introduction to the Lambda Calculus 6.2 Roadmap  What is Computability? — Church’s Thesis.
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 3 LECTURE: LAMBDA CALCULUS PRACTICAL/TUTORIAL: (i) Do exercises given out.
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.
Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.
C. Varela1 Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators Carlos Varela Rennselaer.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Functional Programming Element of Functional Programming.
Functional Languages. Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution.
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.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
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.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
Principles of programming languages 9: Lambda calculus Isao Sasano Department of Information Science and Engineering.
Lambda Calculus Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 11.
CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.
Types and Programming Languages Lecture 12 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Lambda Calculus CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
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?”
Lesson 3 Formalizing and Implementing Pure Lambda Calculus 1/15/02 Chapters 5.3, 6, 7.
Types and Programming Languages
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.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L04Ext-1 September 21, 2006http:// Some more thoughts.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CSE-321 Programming Languages -Calculus (II) POSTECH March 26, 2007 박성우.
CSE-321 Programming Languages Abstract Machine E POSTECH May 1, 2006 박성우.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Types and Programming Languages Lecture 16 Simon Gay Department of Computing Science University of Glasgow 2006/07.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
Chapter 2: Lambda Calculus
CS 550 Programming Languages Jeremy Johnson
Lambda Calculus CSE 340 – Principles of Programming Languages
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
CS 611: Lecture 9 More Lambda Calculus: Recursion, Scope, and Substitution September 17, 1999 Cornell University Computer Science Department Andrew Myers.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Carlos Varela Rennselaer Polytechnic Institute September 6, 2016
Carlos Varela Rennselaer Polytechnic Institute September 4, 2015
Programming Languages and Compilers (CS 421)
Scoping and Binding of Variables
Announcements Quiz 6 HW7 due Tuesday, October 30
Carlos Varela Rennselaer Polytechnic Institute September 8, 2017
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
CSE S. Tanimoto Lambda Calculus
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Programming Languages
Carlos Varela Rennselaer Polytechnic Institute September 8, 2015
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Carlos Varela Rennselaer Polytechnic Institute September 10, 2019
Presentation transcript:

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

Types and Programming Languages Lecture 6 - Simon Gay2 Lambda Calculus Lambda calculus is a notation for functions, and can be viewed as a core functional programming language, either untyped (initially) or typed (later). Syntax Terms e are defined by this grammar, assuming a supply of variables x, y, z, … : e ::= x (variable) | x.e (abstraction) | e e (application) Convention: has the lowest priority.

2006/07Types and Programming Languages Lecture 6 - Simon Gay3 Lambda Calculus Examples x.x is a function which returns its parameter x. y.xy is a function which, given a parameter x, returns a function which, given a parameter y, returns the result of applying x to y. To obtain more concrete examples, we can combine -notation with the syntax of SEL: x.x+1 x. y.x+y x. y.if x==1 then y else y+1

2006/07Types and Programming Languages Lecture 6 - Simon Gay4 Scope and Variable Binding x declares a new variable x with its own scope. Occurrences of x within this scope are said to be bound. Variables which are not bound are said to be free. x.y+x boundfree The name of a bound variable is not important as long as it is the same as the variable attached to the corresponding. x.y+x is equivalent to z.y+z Bound variables can be renamed as long as free variables are not captured. x.y+x is different from y.y+y

2006/07Types and Programming Languages Lecture 6 - Simon Gay5 Scope and Variable Binding Renaming bound variables is called alpha-conversion and terms which differ only in the names of bound variables are said to be alpha-equivalent. It is often convenient to assume that a term has been alpha-converted so that the names of all bound variables are different from each other and different from the names of all free variables. Variables are bound by the structurally nearest matching. x.( y. x.xy)x is alpha-equivalent to x.( y. z.zy)x

2006/07Types and Programming Languages Lecture 6 - Simon Gay6 Variable Binding Variable binding is a fundamental concept which occurs in many places: and of course in any programming language: int f(int x) { return x+1; } { int x = 1; { int y; y = x + 1; }

2006/07Types and Programming Languages Lecture 6 - Simon Gay7 Substitution To define the operational semantics of the lambda calculus we need to define substitution, in a similar way to SFL. if x and z are different variables if y is not free in e In general it may be necessary to rename bound variables to avoid variable capture during substitution: WRONG! CORRECT

2006/07Types and Programming Languages Lecture 6 - Simon Gay8 Lambda Calculus: Operational Semantics The reduction rule for function application: is similar to the rule in SFL. It is known as beta-reduction. Example ( x.x+1)2  (x+1)[2/x] = 2+1  3 if we include the syntax and reduction rules of SEL. ( x. y.x+y)2  ( y.x+y)[2/x] = y.2+y an example of a function which returns a function.

2006/07Types and Programming Languages Lecture 6 - Simon Gay9 Lambda Calculus: Reduction Strategies Different reduction strategies are based on where in a term it is possible to apply beta reduction. We will concentrate on call by value reduction, which means adding the following rules: Values v are -abstractions, and any other values which we might incorporate from SEL. Other reduction strategies are possible (e.g. call by name), but this has little effect on typing (which is what we are interested in).

2006/07Types and Programming Languages Lecture 6 - Simon Gay10 Lambda Calculus: what I’m not telling you Lambda calculus is powerful enough to be a general-purpose programming language: any computation can be expressed. Data can be encoded as functions, for example: true x. y.x false x. y.y if c then t else e (c t) e Non-terminating computation is possible, for example: ( x.xx)( x.xx)  ( x.xx)( x.xx) and recursive functions can be encoded.

2006/07Types and Programming Languages Lecture 6 - Simon Gay11 Lambda Calculus + Expressions As a step towards a better functional language (BFL) we can combine -calculus and SEL. v ::= integer literal | true | false | x.e e ::= v | x | e + e | e == e | e & e | if e then e else e | ee Syntax “x” stands for any variable

2006/07Types and Programming Languages Lecture 6 - Simon Gay12 Lambda Calculus + Expressions Reductions The rules for SEL, and the following extra rules: (R-AppAbs) note: applied to a value (R-AppL)(R-AppR) Remember that a -abstraction is a value.

2006/07Types and Programming Languages Lecture 6 - Simon Gay13 Do we have a Better Functional Language? We have functions, e.g. x.x+2 is a function, but can we really do functional programming? It’s essential to be able to define recursive functions, but our language has no direct support for recursive definitions. We have no way of associating a name with a function. It is possible to express nameless recursive functions in -calculus, and it’s interesting to see how this works.

2006/07Types and Programming Languages Lecture 6 - Simon Gay14 Recursion in -calculus Fixed points If f is a function then a fixed point of f is a value v such that fv = v. Fixed point combinators It is possible to find a -term Y with the curious property that for any term f, Yf is a fixed point of f. In other words, f(Yf) = Yf. For example, define Y = y.( x.y(xx))( x.y(xx)) Yf = ( y.( x.y(xx))( x.y(xx)))f  ( x.f(xx))( x.f(xx))  f(( x.f(xx))( x.f(xx))) = f(Yf) (we haven’t said what ‘=’ means, but it should include reduction)

2006/07Types and Programming Languages Lecture 6 - Simon Gay15 Recursion in -calculus Recursive definitions and fixed points Here is a recursive function which we would like to define, but can’t because we have no mechanism for naming functions: f = x.if x==0 then 1 else x*f(x-1) (1) If we abstract on f we get the following, which we call F just for convenience: F = f. x.if x==0 then 1 else x*f(x-1) Now imagine that g is a fixed point of F: g = Fg  x.if x==0 then 1 else x*g(x-1)) which strongly suggests that the meaning of the recursive definition (1) is given by a fixed point of F.

2006/07Types and Programming Languages Lecture 6 - Simon Gay16 Recursion in -calculus Conclusion Given a recursive function definition such as f = x.if x==0 then 1 else x*f(x-1) we can construct the corresponding functional: F = f. x.if x==0 then 1 else x*f(x-1) whose fixed point is the function we want. This fixed point can be expressed as YF and written as a -term without naming. Example YF = ( y.( x.y(xx))( x.y(xx)))( f. x.if x==0 then 1 else x*f(x-1)) (YF)3  * 6

2006/07Types and Programming Languages Lecture 6 - Simon Gay17 Reading Pierce: 5 Exercises Pierce: 5.2.1, 5.3.3, 5.3.7, 5.3.8