© Kenneth C. Louden, 20031 Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.

Slides:



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

Substitution & Evaluation Order cos 441 David Walker.
Semantics Static semantics Dynamic semantics attribute grammars
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
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Copyright © Cengage Learning. All rights reserved.
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.
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
School of Computing and Mathematics, University of Huddersfield CHA2545 Last Lecture LECTURE: LAMBDA CALCULUS SEMANTICS TUTORIAL: Revision.
Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
The Game of Algebra or The Other Side of Arithmetic The Game of Algebra or The Other Side of Arithmetic © 2007 Herbert I. Gross by Herbert I. Gross & Richard.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction λ.
The Relational Model: Relational Calculus
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Lambda Calculus History and Syntax. History The lambda calculus is a formal system designed to investigate function definition, function application and.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
1 ML fun x -> e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors let f x = e variables These.
Chapter Fifteen: Functional Programming Languages Lesson 12.
1 Relational Algebra and Calculas Chapter 4, Part A.
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.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
Lambda Calculus Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 11.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Comp 311 Principles of Programming Languages Lecture 4 The Scope of Variables Corky Cartwright September 3, 2008.
CMSC 330: Organization of Programming Languages Operational Semantics.
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 8 LECTURE: LAMBDA CALCULUS SEMANTICS TUTORIAL: exercises.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
© Kenneth C. Louden, Chapter 7 - Control I: Expressions and Statements Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
MA/CSSE 474 Theory of Computation How many regular/non-regular languages are there? Closure properties of Regular Languages (if there is time) Pumping.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Chapter 2: Lambda Calculus
CS 550 Programming Languages Jeremy Johnson
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
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.
Corky Cartwright January 18, 2017
Carlos Varela Rennselaer Polytechnic Institute September 6, 2016
Carlos Varela Rennselaer Polytechnic Institute September 4, 2015
Programming Languages and Compilers (CS 421)
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
Material in the textbook Sections to 1.2.1
Announcements Exam 2 on Friday, November 2nd Topics
L Calculus.
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
Carlos Varela Rennselaer Polytechnic Institute September 8, 2015
COMPILER CONSTRUCTION
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Presentation transcript:

© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden

Chapter 11 - Part IIIK. Louden, Programming Languages2 Recursive Functions Abstractly, a function f is a rule that associates to each x in some set X (the domain), a y in another set Y (the range): f: X  Y. Alternatively, a function can be viewed as a subset S of the Cartesian product X  Y with the property that (x,y), (x,y')  S implies y = y'. Functions can be given as sets simply by listing their elements (definition by extension). More usually, functions are given by a formula (definition by comprension), which may or may not be recursive.

Chapter 11 - Part IIIK. Louden, Programming Languages3 Recursive Functions (2) Chapter 8 shows how to implement recursive functions. But how are such functions mathematically defined? One way to think abstractly about a recursive definition is to consider it to imply a (countably infinite) union of sets that are built, one element at a time, out of the base case or cases. Factorial example: fact n = if n = 0 then 1 else n * fact (n-1) The "base" point is (0,1): fact 0 = { (0,1) } Now consider the formula as a set equation: fact = fact 0 U fact', where fact' is the function formed for each n by the formula n * fact (n-1).

Chapter 11 - Part IIIK. Louden, Programming Languages4 Recursive Functions (3) Unfortunately, we don't yet know either fact or fact'. But consider what would happen if we used fact 0 as an approximation for fact in the formula for fact': fact 1 = fact 0 U fact 0 ', where fact 0 ' = { (n,m) | (n-1,p)  fact 0, m = n*p } = { (1,1) } [since (1-1,1) = (0,1)  fact 0 ] Then fact 1 = { (0,1) } U { (1,1) }. We have a new point! So apply the equation again, this time using fact 1 as an approximation for fact in fact'. We get yet another point! Call this function fact 2. Continue. Now let fact  = U n = 1..  fact n. What function is this?

Chapter 11 - Part IIIK. Louden, Programming Languages5 Recursive Functions (4) If we try the process once again, we find that we get no new points: fact  = fact 0 U fact  '. The function fact  is said to be a fixed point of the recursive equation for fact. Indeed, fact  is the smallest such set with this property and is essentially unique. So it makes sense to define the fact function to be fact  : we say that the recursive definition has least-fixed-point semantics. Not all sets allow least fixed point solutions to recursive equations. Sets that do are called domains. Domain theory tells us when recursive definitions will work (and when they won't).

Chapter 11 - Part IIIK. Louden, Programming Languages6 Lambda Calculus The lambda calculus is an abstraction and simplification of a functional programming language, much as a Turing machine is an abstraction and simplification of a computer. Lambda calculus is Turing-complete, so it it can be used as a model for computation instead of TMs. Issues such as delayed evaluation, recursion, and scope can be studied with mathematical precision in the lambda calculus.

Chapter 11 - Part IIIK. Louden, Programming Languages7 Syntax of Lambda Calculus Two sets of basic expressions: Variables: x,y,z, etc. (essentially identifiers representing parameters) Constants: numbers like 1, 2, 3; "built-in" functions like +, *, cons. Two operations: Abstraction (like lambda expressions in Scheme, ML, or Haskell - anonymous function creation). Application (like function call). Grammar: lexpr  variable. lexpr | lexpr lexpr | ( lexpr ) | variable | constant abstractionapplication

Chapter 11 - Part IIIK. Louden, Programming Languages8 Examples of Lambda Calculus Expressions x. + (( y. ( x.  x y) 2) x) y ( x. x y) y (( y. 2) (( x. x x) ( x. x x))) ( h. ( x. h (x x)) ( x. h (x x)))

Chapter 11 - Part IIIK. Louden, Programming Languages9 Notes Lambda calculus is fully Curried. Application works left to right, abstraction right to left. Application has higher precedence than abstraction. The set of variables is unspecified, but doesn't matter very much, as long as it is (countably) infinite. The set of constants isn't specified either, and this can make a difference in terms of what you want to express. This set may be infinite (all integers) or finite or even empty (pure lambda calculus). Thus, there are really many kinds of lambda calculus.

Chapter 11 - Part IIIK. Louden, Programming Languages10 Notes (continued) There are no semantic restrictions on the syntactically legal expressions in the lambda calculus: all expressions given by the syntax are legal. By the previous statement, there can be no type checking in the lambda calculus. There is in fact an alternate version called the typed lambda calculus that does have types. Not all lambda expressions make sense as programs: (x x) is legal, and so is (2 x). (Exercise: try typing x. x x in ML or Haskell.)

Chapter 11 - Part IIIK. Louden, Programming Languages11 Semantics of Lambda Calculus Some expressions are equivalent to others in lambda calculus even though they are syntactically distinct: x. x is equivalent to y. y(  ) x. y x is equivalent to y (  ) ( x. x) y is equivalent to y(  ) These equivalences have historical names: alpha-conversion (  -conversion), eta-conversion (  -conversion), and beta- conversion (  -conversion). When a conversion simplifies an expression it is called a reduction.

Chapter 11 - Part IIIK. Louden, Programming Languages12 Semantics (2) Conversion operations depend on the notion of the scope (or binding) of a variable in an abstraction. The variable x in the expression ( x.E) is said to be bound by the lambda. The scope of the binding is the expression E. An occurrence of a variable outside the scope of any binding of it by a lambda is a free occurrence. An occurrence that is not free is a bound occurrence. Thus, in the expression x. E, all occurrences of x in E are bound.

Chapter 11 - Part IIIK. Louden, Programming Languages13 Examples of bound and free vars: In the expression ( x. x y) y the variable x is bound and both instances of the variable y are free. In the expression x. + (( y. ( x.  x y) 2) x) y both instances of x are bound (but by different lambdas), and the first instance of y is bound, while the second one is free (arrows point to bindings): x. + (( y. ( x.  x y) 2) x) y

Chapter 11 - Part IIIK. Louden, Programming Languages14 Conversion Rules First, let E[F/x] represent the expression obtained by substituting all free occurrences of x by F in the expression E.  -conversion: x. E is equivalent to y. E[y/x], provided E contains no free occurrences of y.  -conversion: ( x. E) F is equivalent to E[F/x], provided F contains no free variables that are bound in E.  -conversion: x. E x is equivalent to E, provided E contains no free occurrences of x.

Chapter 11 - Part IIIK. Louden, Programming Languages15 Name Capture Each of the above rules has a side condition which represents a name capture issue: if you substitute an expression with free variables into a lambda, one or more of those free variables may conflict with names bound by lambdas (and be captured by them), giving incorrect results (since variable bound/free status should not change): (  ) : x. y  y. y (  ) : ( x. y. x y) y  y. y y (  ) : x. x x  x

Chapter 11 - Part IIIK. Louden, Programming Languages16 Name Capture (2) The solution is to use  -conversion to change bound variable names that clash with free variables in  -reduction: ( x. y. x y) y =  ( x. z. x z) y =  z. y z In the other two cases, we simply cannot perform that particular conversion.

Chapter 11 - Part IIIK. Louden, Programming Languages17 Semantics (3) An expression is a normal form if there are no  -reductions that can be performed on it. The semantics of an expression in lambda calculus is expressed by converting it to a normal form, if possible. Such a normal form, if it exists, is unique, by the famous Church-Rosser Theorem. Not all reduction sequences lead to a normal form: ( y. 2) (( x. x x)( x. x x))

Chapter 11 - Part IIIK. Louden, Programming Languages18 Normal vs. Applicative Order A  -reduction ( x. E) F => E[F/x] that substitutes F for x before reducing F is called a normal-order reduction. It corresponds to the lazy evaluation of Haskell (without the memoization). If on the other hand, the  -reduction ( x. E) F => E[F/x] is performed only after reducing F, it is called an applicative order reduction. This corresponds to the evaluation rule of Scheme, where all arguments are evaluated before a call.

Chapter 11 - Part IIIK. Louden, Programming Languages19 Semantics (4) Main result (also a consequence of the Church-Rosser Theorem): if a normal form exists, a normal-order sequence of conversions will find it. Example 1: a normal form for ( y. 2) (( x. x x)( x. x x)) is 2, but an applicative-order  -reduction gets into an infinite loop. Example 2: x. + (( y. ( x.  x y) 2) x) y reduces to x. + (  2 x) y using either normal-order or applicative-order.

Chapter 11 - Part IIIK. Louden, Programming Languages20 Recursion Can recursion be expressed in lambda calculus? Note that lambda calculus has no mechanism for resolving free names, which is the usual way of expressing recursion: fact = n.if n==0 then 1 else n*fact(n-1) To get rid of the free name in the right-hand side, make it a parameter: H fact = g. n.if n==0 then 1 else n*g(n-1) Now if H fact f = f, then f must be fact ! Such an f is called a fixed point of H fact. Is there a lambda expression that can produce fixed points? YES!

Chapter 11 - Part IIIK. Louden, Programming Languages21 Recursion (2) The lambda expression Y = h.( x. h (x x))( x. h (x x)) produces arbitrary fixed points: let H be any lambda expression. Then Y H = ( x. H (x x))( x. H (x x)) = H (( x. H (x x)) ( x. H (x x))) = H (Y H), so Y H is a fixed point of H. Unfortunately, Y has no type in ML or Haskell (try it!), but we can fall back on the fixed point property itself as a definition of Y, in either ML, Haskell, or Scheme: y h = h (y h) To what extent this works in the actual languages is Exercise

Chapter 11 - Part IIIK. Louden, Programming Languages22 Implementing standard language features in lambda calculus Recursion can be implemented (as we have just seen). So can numbers (Exercise 11.47). So can lists (Exercise 11.46). So can Booleans and if-then-else: true = x. y. x, false = x. y. y, if = x. y. z. x y z, and now (if true E F) = E and (if false E F) = F Indeed, everything can (with some huffing and puffing, as with Turing Machines)!