6. Introduction to the Lambda Calculus. © O. Nierstrasz PS — Introduction to the Lambda Calculus 6.2 Roadmap  What is Computability? — Church’s Thesis.

Slides:



Advertisements
Similar presentations
Substitution & Evaluation Order cos 441 David Walker.
Advertisements

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
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS Instructors: Crista Lopes Copyright © Instructors.
MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM.
10. Petri Nets Prof. O. Nierstrasz. Roadmap  Definition: —places, transitions, inputs, outputs —firing enabled transitions  Modelling: —concurrency.
Foundations of Programming Languages: Introduction to Lambda Calculus
CPSC 411, Fall 2008: Set 12 1 CPSC 411 Design and Analysis of Algorithms Set 12: Undecidability Prof. Jennifer Welch Fall 2008.
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap  Syntax and Semantics  Semantics of Expressions 
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 3 LECTURE: LAMBDA CALCULUS PRACTICAL/TUTORIAL: (i) Do exercises given out.
4. Functional Programming. © O. Nierstrasz PS — Functional Programming 4.2 Roadmap Overview  Functional vs. Imperative Programming  Pattern Matching.
1 Undecidability Andreas Klappenecker [based on slides by Prof. Welch]
ESE Einführung in Software Engineering N. XXX Prof. O. Nierstrasz Fall Semester 2009.
13. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Summary, Trends, Research...  Summary: functional, logic and object-oriented.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus.
CP — Concurrent Programming 12. Petri Nets Prof. O. Nierstrasz Wintersemester 2005 / 2006.
Metamodeling Seminar X. CHAPTER Prof. O. Nierstrasz Spring Semester 2008.
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.
ESE Einführung in Software Engineering X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.
N. XXX Prof. O. Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap Overview  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed.
4. Functional Programming. © O. Nierstrasz PS — Functional Programming 4.2 Roadmap  Functional vs. Imperative Programming  Pattern Matching  Referential.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus.
12. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Roadmap  Summary: —Trends in programming paradigms  Research:...
Tim Sheard Oregon Graduate Institute Lecture 11: A Reduction Semantics for MetaML CS510 Section FSC Winter 2005 Winter 2005.
C. Varela1 Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators Carlos Varela Rennselaer.
OORPT Object-Oriented Reengineering Patterns and Techniques X. CHAPTER Prof. O. Nierstrasz.
CP — Concurrent Programming X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
12. eToys. © O. Nierstrasz PS — eToys 12.2 Denotational Semantics Overview:  … References:  …
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction λ.
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.
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.
© 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.
-Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH.
Lambda Calculus CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
CSE-321 Programming Languages -Calculus (II) POSTECH March 26, 2007 박성우.
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
Mathematical Foundations
CS 550 Programming Languages Jeremy Johnson
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
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.
CSCE 411 Design and Analysis of Algorithms
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
L Calculus.
CSE S. Tanimoto Lambda Calculus
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
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:

6. Introduction to the Lambda Calculus

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.2 Roadmap  What is Computability? — Church’s Thesis  Lambda Calculus — operational semantics  The Church-Rosser Property  Modelling basic programming constructs

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.3 References  Paul Hudak, “Conception, Evolution, and Application of Functional Programming Languages,” ACM Computing Surveys 21/3, Sept. 1989, pp  Kenneth C. Louden, Programming Languages: Principles and Practice, PWS Publishing (Boston),  H.P. Barendregt, The Lambda Calculus — Its Syntax and Semantics, North-Holland, 1984, Revised edition.

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.4 Roadmap  What is Computability? — Church’s Thesis  Lambda Calculus — operational semantics  The Church-Rosser Property  Modelling basic programming constructs

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.5 What is Computable? Computation is usually modelled as a mapping from inputs to outputs, carried out by a formal “machine,” or program, which processes its input in a sequence of steps. An “effectively computable” function is one that can be computed in a finite amount of time using finite resources. ……… ……… ……… input yes no output Effectively computable function

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.6 Church’s Thesis Effectively computable functions [from positive integers to positive integers] are just those definable in the lambda calculus. Or, equivalently: It is not possible to build a machine that is more powerful than a Turing machine. Church’s thesis cannot be proven because “effectively computable” is an intuitive notion, not a mathematical one. It can only be refuted by giving a counter-example — a machine that can solve a problem not computable by a Turing machine. So far, all models of effectively computable functions have shown to be equivalent to Turing machines (or the lambda calculus).

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.7 Uncomputability A problem that cannot be solved by any Turing machine in finite time (or any equivalent formalism) is called uncomputable. Assuming Church’s thesis is true, an uncomputable problem cannot be solved by any real computer. The Halting Problem: Given an arbitrary Turing machine and its input tape, will the machine eventually halt? The Halting Problem is provably uncomputable — which means that it cannot be solved in practice.

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.8 What is a Function? (I) Extensional view: A (total) function f: A  B is a subset of A  B (i.e., a relation) such that: 1. for each a  A, there exists some (a,b)  f (i.e., f(a) is defined), and 2. if (a,b 1 )  f and (a, b 2 )  f, then b 1 = b 2 (i.e., f(a) is unique)

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.9 What is a Function? (II) Intensional view: A function f: A  B is an abstraction x.e, where x is a variable name, and e is an expression, such that when a value a  A is substituted for x in e, then this expression (i.e., f(a)) evaluates to some (unique) value b  B.

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.10 Roadmap  What is Computability? — Church’s Thesis  Lambda Calculus — operational semantics  The Church-Rosser Property  Modelling basic programming constructs

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.11 What is the Lambda Calculus? The Lambda Calculus was invented by Alonzo Church [1932] as a mathematical formalism for expressing computation by functions. Syntax: e::=xa variable | x. ean abstraction (function) |e 1 e 2 a (function) application Examples: x. x — is a function taking an argument x, and returning x f x — is a function f applied to an argument x NB: same as f(x) !

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.12 Parsing Lambda Expressions Lambda extends as far as possible to the right f.x y  f.(x y) Application is left-associative x y z  (x y) z Multiple lambdas may be suppressed f g.x  f. g.x

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.13 What is the Lambda Calculus?... (Operational) Semantics: The lambda calculus can be viewed as the simplest possible pure functional programming language.  conversion (renaming): x. e  y. [ y/x ] e where y is not free in e  reduction (application): ( x. e 1 ) e 2  [ e 2 /x ] e 1 avoiding name capture  reduction: x. e x  eif x is not free in e

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.14 Beta Reduction Beta reduction is the computational engine of the lambda calculus: Define: I  x. x Now consider: I I = ( x. x) ( x. x )  [ x. x / x] x  reduction = x. xsubstitution = I

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.15 Lambda expressions in Haskell We can implement most lambda expressions directly in Haskell: i = \x -> x ? i 5 5 (2 reductions, 6 cells) ? i i 5 5 (3 reductions, 7 cells)

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.16 Lambdas are anonymous functions A lambda abstraction is just an anonymous function. Consider the Haskell function: The value of compose is the anonymous lambda abstraction: f g x. f (g x) NB: This is the same as: f. g. x. f (g x) compose f g x = f(g(x))

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.17 A Few Examples 1. ( x.x) y 2. ( x.f x) 3. x y 4. ( x.x) ( x.x) 5. ( x.x y) z 6. ( x y.x) t f 7. ( x y z.z x y) a b ( x y.x) 8. ( f g.f g) ( x.x) ( x.x) z 9. ( x y.x y) y 10. ( x y.x y) ( x.x) ( x.x) 11. ( x y.x y) (( x.x) ( x.x))

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.18 Free and Bound Variables The variable x is bound by in the expression: x.e A variable that is not bound, is free : An expression with no free variables is closed. (AKA a combinator.) Otherwise it is open. For example, y is bound and x is free in the (open) expression: y. x y fv(x)={ x } fv(e 1 e 2 )= fv(e 1 )  fv(e 2 ) fv( x. e) =fv(e) - { x }

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.19 “Hello World” in the Lambda Calculus hello world  Is this expression open? Closed?

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.20 Roadmap  What is Computability? — Church’s Thesis  Lambda Calculus — operational semantics  The Church-Rosser Property  Modelling basic programming constructs

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.21 Why macro expansion is wrong Syntactic substitution will not work: ( x y. x y ) y  [ y / x] ( y. x y)  reduction ≠ ( y. y y )incorrect substitution! Since y is already bound in ( y. x y), we cannot directly substitute y for x.

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.22 Substitution We must define substitution carefully to avoid name capture: [e/x] x=e [e/x] y=yif x ≠ y [e/x] (e 1 e 2 )=([e/x] e 1 ) ([e/x] e 2 ) [e/x] ( x. e 1 ) = ( x. e 1 ) [e/x] ( y. e 1 ) = ( y. [e/x] e 1 )if x ≠ y and y  fv(e) [e/x] ( y. e 1 ) = ( z. [e/x] [z/y] e 1 ) if x ≠ y and z  fv(e)  fv(e 1 ) ( x. (( y. x) ( x. x)) x ) y  [y / x] (( y. x) ( x. x)) x = (( z. y ) ( x. x)) y Consider:

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.23 Alpha Conversion Alpha conversions allow us to rename bound variables. A bound name x in the lambda abstraction ( x.e) may be substituted by any other name y, as long as there are no free occurrences of y in e: Consider: ( x y. x y ) y  ( x z. x z) y  conversion  [ y / x] ( z. x z)  reduction  ( z. y z) = y  reduction

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.24 Eta Reduction Eta reductions allow one to remove “redundant lambdas”. Suppose that f is a closed expression (i.e., there are no free variables in f). Then: ( x. f x ) y  f y  reduction So, ( x. f x ) behaves the same as f ! Eta reduction says, whenever x does not occur free in f, we can rewrite ( x. f x ) as f.

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.25  ( x y. x y) ( x. x y) ( a b. a b)NB: left assoc.  ( x z. x z) ( x. x y) ( a b. a b)  conversion  ( z. ( x. x y) z) ( a b. a b)  reduction  ( x. x y) ( a b. a b)  reduction  ( a b. a b) y  reduction  ( b. y b)  reduction  y  reduction

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.26 Normal Forms A lambda expression is in normal form if it can no longer be reduced by beta or eta reduction rules. Not all lambda expressions have normal forms!  = ( x. x x) ( x. x x)  [ ( x. x x) / x ] ( x x ) =( x. x x) ( x. x x)  reduction  ( x. x x) ( x. x x)  reduction ... Reduction of a lambda expression to a normal form is analogous to a Turing machine halting or a program terminating.

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.27 Evaluation Order Most programming languages are strict, that is, all expressions passed to a function call are evaluated before control is passed to the function. Most modern functional languages, on the other hand, use lazy evaluation, that is, expressions are only evaluated when they are needed. Consider: Applicative-order reduction: Normal-order reduction: sqr n = n * n sqr (2+5)  sqr 7  7*7  49 sqr (2+5)  (2+5) * (2+5)  7 * (2+5)  7 * 7  49

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.28 The Church-Rosser Property “If an expression can be evaluated at all, it can be evaluated by consistently using normal-order evaluation. If an expression can be evaluated in several different orders (mixing normal-order and applicative order reduction), then all of these evaluation orders yield the same result.” So, evaluation order “does not matter” in the lambda calculus.

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.29 Roadmap  What is Computability? — Church’s Thesis  Lambda Calculus — operational semantics  The Church-Rosser Property  Modelling basic programming constructs

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.30 Non-termination However, applicative order reduction may not terminate, even if a normal form exists! Compare to the Haskell expression: ( x. y) ( ( x. x x) ( x. x x) ) Applicative order reductionNormal order reduction  ( x. y) ( ( x. x x) ( x. x x) )  y  ( x. y) ( ( x. x x) ( x. x x) ) … (\x -> \y -> x) 1 (5/0)  1

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.31 Currying Since a lambda abstraction only binds a single variable, functions with multiple parameters must be modelled as Curried higher-order functions. As we have seen, to improve readability, multiple lambdas are suppressed, so: x y. x= x. y. x b x y. b x y= b. x. y. ( b x ) y

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.32 Representing Booleans Many programming concepts can be directly expressed in the lambda calculus. Let us define: True  x y. x False  x y. y not  b. b False True if b then x else y  b x y. b x y then: not True= ( b. b False True ) ( x y. x )  ( x y. x ) False True  False if True then x else y= ( b x y. b x y ) ( x y. x) x y  ( x y. x) x y  x

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.33 Representing Tuples Although tuples are not supported by the lambda calculus, they can easily be modelled as higher-order functions that “wrap” pairs of values. n-tuples can be modelled by composing pairs... Define:pair  ( x y z. z x y) first  ( p. p True ) second  ( p. p False ) then:(1, 2)=pair 1 2  ( z. z 1 2) first (pair 1 2)  (pair 1 2) True  True 1 2  1

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.34 Tuples as functions In Haskell: t= \x -> \y -> x f= \x -> \y -> y pair= \x -> \y -> \z -> z x y first= \p -> p t second= \p -> p f ? first (pair 1 2) 1 ? first (second (pair 1 (pair 2 3))) 2

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.35 What you should know!  Is it possible to write a Pascal compiler that will generate code just for programs that terminate?  What are the alpha, beta and eta conversion rules?  What is name capture? How does the lambda calculus avoid it?  What is a normal form? How does one reach it?  What are normal and applicative order evaluation?  Why is normal order evaluation called lazy?  How can Booleans and tuples be represented in the lambda calculus?

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.36 Can you answer these questions?  How can name capture occur in a programming language?  What happens if you try to program  in Haskell? Why?  What do you get when you try to evaluate (pred 0)? What does this mean?  How would you model numbers in the lambda calculus? Fractions?

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.37 License > Attribution-ShareAlike 2.5 You are free: to copy, distribute, display, and perform the work to make derivative works to make commercial use of the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above.