Principles of programming languages 9: Lambda calculus Isao Sasano Department of Information Science and Engineering.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
Principles of programming languages 1: Introduction (with a simple language) Isao Sasano Department of Information Science and Engineering.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS Instructors: Crista Lopes Copyright © Instructors.
Principles of programming languages 3: Answers for exercises Isao Sasano Department of Information Science and Engineering.
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.
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.
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.
Comp 205: Comparative Programming Languages Semantics of Functional Languages Term- and Graph-Rewriting The λ-calculus Lecture notes, exercises, etc.,
Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's.
C. Varela1 Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators Carlos Varela Rennselaer.
David Evans CS200: Computer Science University of Virginia Computer Science Class 31: Universal Turing Machines.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Functional Languages. Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution.
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction λ.
10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look.
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.
Lexicografie computationala Feb., 2012 Anca Dinu University of Bucharest.
Principle of Programming Lanugages 2: Imperative languages Isao Sasano Department of Information Science and Engineering ( structured programming, control.
Principles of programming languages 5: An operational semantics of a small subset of C Department of Information Science and Engineering Isao Sasano.
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.
Semantic Construction lecture 2. Semantic Construction Is there a systematic way of constructing semantic representation from a sentence of English? This.
© 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.
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.
Principles of programming languages 6: Types 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
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
Lesson 3 Formalizing and Implementing Pure Lambda Calculus 1/15/02 Chapters 5.3, 6, 7.
CS383 Programming Languages Quiz Which one of the following is NOT a part of doing an inductive proof? a.Clearly state the induction hypothesis.
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction.
CSE-321 Programming Languages -Calculus (II) POSTECH March 26, 2007 박성우.
Lambda Calculus A fairly simple system for representing functions The base of typed functional programming languages such as ML and Haskell.
CS321 Functional Programming 2 © JAS The λ Calculus This is a formal system to capture the ideas of function abstraction and application introduced.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
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.
CS 550 Programming Languages Jeremy Johnson
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Lambda Calculus CSE 340 – Principles of Programming Languages
Principle of Programming Lanugages 2: Imperative languages
Principles of programming languages 8: Types
Information Science and Engineering
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
Principles of programming languages 4: Parameter passing, Scope rules
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.
Programming Languages and Compilers (CS 421)
Announcements Quiz 6 HW7 due Tuesday, October 30
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Announcements Exam 2 on Friday, November 2nd Topics
L Calculus.
Programming Languages and Compilers (CS 421)
Class 31: Universal Turing Machines CS200: Computer Science
CSE S. Tanimoto Lambda Calculus
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Lambda Expressions Cases
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Presentation transcript:

Principles of programming languages 9: Lambda calculus Isao Sasano Department of Information Science and Engineering

Lambda calculus Lambda calculus was invented in 1930’s (before physical digital computers were made). Nowadays it becomes a core of functional languages. It covers all the functions that are computable. [Lambda expression] ::= | | (. ) | ( ) ::= 0 | 1 | 2 | 3 | … ::= x | y | z | w | …

Lambda expressions Conventions when writing lambda expressions: 1.Function applications associate to the left, so that M 1 M 2 M 3 should be read as (M 1 M 2 ) M 3. 2.The scope of each variable bound in lambda abstraction is interpreted as being as large as possible, so an expression x. … may be parenthesized by inserting a left parenthesis after the dot, and the matching right parenthesis as far to the right as will produce a syntactically well-formed expression. 3.When parentheses are omitted, obey 1 and 2. (ex.) We read x y z as ((x y) z). (ex.) We read x. y. z. x y z as ( x. ( y. ( z. ((x y) z)))).

Lambda expressions Lambda expressions are invented while pursuing the essentials of computations. Intuitively a lambda abstraction is a form for writing a function. For example, the function g defined by g ( x ) = x + 1 is written using lambda abstraction as follows. g = x. x + 1

Lambda calculus Lambda calculus is a system for computation according to the idea where computation is a series of transformations (called beta reductions) of lambda expressions.

Examples of beta reductions Similarly, g ( x ) = x and g ( y ) = y have the same meaning in mathematics. (ex.) ( x. x) z  z (ex.) ( y. y) z  z Intuitively the expression λx. x is a function that takes an argument x and returns x, so even if we change the x’s to y’s, the meaning does not change.

Examples of beta reductions (cont.) (ex.) ( x. x y) ( z. z)  ( z. z) y (λx. x y) takes an argument x and applies x to y. In this example, (λz. z) is the actual argument, so it is applied to y. (This can further be beta reduced to y.) (ex.) (λx. x) (λy. y)  (λy. y) (λx. x) represents a function that takes an argument x and returns x. In this example, (λy. y) is the actual argument, so the result is (λy. y).

Meta variables We use the following meta variables. M, N, P, M 1, M 2, etc: lambda expressions c: constants (natural numbers) x, y, z : variables ( x, y, z, w, etc. In this slide, meta variables are written in italic and usual variables are written in roman.)

Beta reductions Beta reduction is written by  and defined as follows. ( x. M) N  M [N / x] If M  N, ( x. M)  ( x. N) MP  NP PM  PN M [N / x] (substitution) represents a lambda expression obtained by replacing all free occurrences of x in M with N. Note that bound variables are renamed when necessary.

An example The previous example ( x. x) z is beta reduced to x [ z / x ]. x [ z / x ] represents a lambda expression obtained by replacing x with z, namely z. The lambda expression ( x. x y) ( z. z) is beta reduced to (x y) [ ( z. z) / x ]. (x y) [ ( z. z) / x ] represents a lambda expression obtained by replacing all occurrences of x in (x y) with ( z. z), namely ( z. z) y.

Another example Notice (λx. y) [x / y] does not represent (λx. x). x is local variable in (λx. y), so it is not the same as x in [x / y]. So we rename the bound variable x in (λx. y) with new name such as z and obtain (λz. y). Then we replace y in (λz. y) with x and obtain (λz. x). We define substitution in the next page with taking into account the name collision.

The definition of M [ N / x ] When M is a constant: c [ N / x ] = c When M is a variable: x [ N / x ] = N y [ N / x ] = y ( x  y ) When M is a lambda abstraction: ( y. M) [ N / x ] = y. M (x = y) y. (M [N / x]) (x  y, y  FV (N)) z. ((M [ z / y ]) [N / x] ) ( x  y, y  FV (N), z  x, z  FV (M), z  FV(N) ) When M is a function application: (M 1 M 2 ) [N / x] = ( M 1 [ N / x ] ) ( M 2 [N / x] )

Free variables FV( c ) = { } FV ( x ) = { x } FV ( x. M ) = FV (M) \ { x } ( \ is the operation for taking the difference of two sets. ) FV (M 1 M 2 ) = FV (M 1 )  FV (M 2 ) Free variables in lambda expression M, written as FV(M), is defined as follows.

Examples (ex.) Free variables in a lambda expression (λx. x): FV (λx. x) = FV (x) \ {x} = {x} \ {x} = { } So (λx. x) has no free variable. (ex.) Free variables in a lambda expression (λx. x y): FV (λx. x y) = FV (x y) \ {x} = (FV (x) U FV(y)) \ {x} = ({x} U {y}) \ {x} = {x, y} \ {x} = {y} So (λx. x y) has just one free variable, y.

Exercise 1 (1) Obtain the free variables in ( z. z) w according to the definition of free variables. (2) Obtain the free variables in (λz. z y) ((λz. z) w) according to the definition of free variables.

Exercise 2 (1) What does (x y) [z/x] represent? (2) What does (λy. x y) [z/x] represent? (3) What does (λy. x y) [y/x] represent? (4) What does (λy. x y) [λz. z y/x] represent?

Exercise 3 (1)Beta reduce once (λx. x y) (λz. z). (2)Beta reduce once (λx. (λy. x y)) (λz. y z).

Sequences of beta reductions When M 1 is beta reduced more than or equal to 0 times to M n, M 1  M 2  …  M n Is called a sequence of beta reductions. (This can also be written as M 1  * M n.) When M  N and N  M, we write M  N.

An example ( x. y. x y) z w  ( y. z y) w  z w A lambda expression (λx. λy. x y) z w can be beta reduced to z w as follows.

Exercise 4 A lambda expression (λx. λy. x y) (λz. z) w can be transformed to w by applying beta reductions. Write the each step of beta reductions.

Church-Rosser theorem If M  * M 1 and M  * M 2 then there exists a lambda expression N such that M 1  * N and M 2  * N.

An example A lambda expression ( x. y. x y) ( z. z) w is beta reduced as follows. ( x. y. x y) ( z. z) w  (λy. (λz. z) y) w There are two ways to beta reduce the obtained lambda expression. (1)(λy. (λz. z) y) w  (λy. y) w (2)(λy. (λz. z) y) w  (λz. z) w By applying beta reduction once more, we obtain w. As this example shows, when a lambda expression is beta reduced to two different lambda expressions, they can be beta reduced to the same lambda expression.

Exercise 5 Beta reduce the lambda expression (λx. λy. x y) (λx. x y) w until obtaining a lambda expression that can not be beta reduced. In this example, there are two sequences of beta reductions. Show both of them.

References Alonzo Church, “An unsolvable problem of elementary number theory”, American Journal of Mathematics, vol. 58, pp , 1936.