C. Varela1 Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators Carlos Varela Rennselaer.

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
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
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
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.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Kernel language semantics Carlos Varela RPI Adapted with permission.
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.
C. Varela1 Programming Languages (CSCI 4430/6969) Lambda Calculus Carlos Varela Rennselaer Polytechnic Institute September 17, 2007.
Prof. Fateman CS 164 Lecture 23
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.
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.
C. Varela1 Programming Languages (CSCI 4430/6430) Part 1: Functional Programming: Summary Carlos Varela Rennselaer Polytechnic Institute September 29,
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
C. Varela1 Programming Languages (CSCI 4430/6430) History, Syntax, Semantics, Essentials, Paradigms Carlos Varela Rennselaer Polytechnic Institute September.
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.
Principles of programming languages 9: Lambda calculus Isao Sasano Department of Information Science and Engineering.
Chapter 5. Functional Decomposition and Symmetric Functions.
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 박성우.
Lesson2 Lambda Calculus Basics 1/10/02 Chapter 5.1, 5.2.
Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.
1-1 An Introduction to Functional Programming Sept
-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
industrial mathematics - i
Lesson 3 Formalizing and Implementing Pure Lambda Calculus 1/15/02 Chapters 5.3, 6, 7.
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction.
CSE-321 Programming Languages -Calculus (II) POSTECH March 26, 2007 박성우.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
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.
1 Topic #3: Lambda CSE 413, Autumn 2007 Programming Languages.
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.
Functional Programming
Chapter 2: Lambda Calculus
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
Carlos Varela Rennselaer Polytechnic Institute September 1, 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
Lesson2 Lambda Calculus Basics
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
Announcements Exam 2 on Friday, November 2nd Topics
L Calculus.
CSE S. Tanimoto Lambda Calculus
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:

C. Varela1 Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators Carlos Varela Rennselaer Polytechnic Institute February 14, 2011

C. Varela2 Mathematical Functions Take the mathematical function: f(x) = x 2 f is a function that maps integers to integers: f: Z  Z We apply the function f to numbers in its domain to obtain a number in its range, e.g.: f(-2) = 4 Function Domain Range

C. Varela3 Function Composition Given the mathematical functions: f(x) = x 2, g(x) = x+1 f  g is the composition of f and g: f  g (x) = f(g(x)) f  g (x) = f(g(x)) = f(x+1) = (x+1) 2 = x 2 + 2x + 1 g  f (x) = g(f(x)) = g(x 2 ) = x Function composition is therefore not commutative. Function composition can be regarded as a (higher-order) function with the following type:  : (Z  Z) x (Z  Z)  (Z  Z)

C. Varela4 Lambda Calculus (Church and Kleene 1930’s) A unified language to manipulate and reason about functions. Given f(x) = x 2 x. x 2 represents the same f function, except it is anonymous. To represent the function evaluation f(2) = 4, we use the following -calculus syntax: ( x. x 2 2)  2 2  4

C. Varela5 Lambda Calculus Syntax and Semantics The syntax of a -calculus expression is as follows: e ::=vvariable | v.efunctional abstraction |(e e)function application The semantics of a -calculus expression is as follows: ( x.E M)  E{M/x} where we choose a fresh x, alpha-renaming the lambda abstraction if necessary to avoid capturing free variables in M.

C. Varela6 Currying The lambda calculus can only represent functions of one variable. It turns out that one-variable functions are sufficient to represent multiple-variable functions, using a strategy called currying. E.g., given the mathematical function:h(x,y) = x+y of typeh: Z x Z  Z We can represent h as h’ of type:h’: Z  Z  Z Such that h(x,y) = h’(x)(y) = x+y For example, h’(2) = g, where g(y) = 2+y We say that h’ is the curried version of h.

C. Varela7 Function Composition in Lambda Calculus S: x.x 2 (Square) I: x.x+1(Increment) C: f. g. x.(f (g x))(Function Composition) ((C S) I) (( f. g. x.(f (g x)) x.x 2 ) x.x+1)  ( g. x.( x.x 2 (g x)) x.x+1)  x.( x.x 2 ( x.x+1 x))  x.( x.x 2 x+1)  x.x+1 2 Recall semantics rule: ( x.E M)  E{M/x} Recall semantics rule: ( x.E M)  E{M/x}

C. Varela8 Free and Bound Variables The lambda functional abstraction is the only syntactic construct that binds variables. That is, in an expression of the form: v.e we say that occurrences of variable v in expression e are bound. All other variable occurrences are said to be free. E.g., ( x. y.(x y) (y w)) Free Variables Bound Variables

C. Varela9  -renaming Alpha renaming is used to prevent capturing free occurrences of variables when reducing a lambda calculus expression, e.g., ( x. y.(x y) (y w))  y.((y w) y) This reduction erroneously captures the free occurrence of y. A correct reduction first renames y to z, (or any other fresh variable) e.g., ( x. y.(x y) (y w))  ( x. z.(x z) (y w))  z.((y w) z) where y remains free.

C. Varela10 Order of Evaluation in the Lambda Calculus Does the order of evaluation change the final result? Consider: x.( x.x 2 ( x.x+1 x)) There are two possible evaluation orders: x.( x.x 2 ( x.x+1 x))  x.( x.x 2 x+1)  x.x+1 2 and: x.( x.x 2 ( x.x+1 x))  x.( x.x+1 x) 2  x.x+1 2 Is the final result always the same? Recall semantics rule: ( x.E M)  E{M/x} Recall semantics rule: ( x.E M)  E{M/x} Applicative Order Normal Order

C. Varela11 Church-Rosser Theorem If a lambda calculus expression can be evaluated in two different ways and both ways terminate, both ways will yield the same result. e e 1 e 2 e’ Also called the diamond or confluence property. Furthermore, if there is a way for an expression evaluation to terminate, using normal order will cause termination.

C. Varela12 Order of Evaluation and Termination Consider: ( x.y ( x.(x x) x.(x x))) There are two possible evaluation orders: ( x.y ( x.(x x) x.(x x)))  ( x.y ( x.(x x) x.(x x))) and: ( x.y ( x.(x x) x.(x x)))  y In this example, normal order terminates whereas applicative order does not. Recall semantics rule: ( x.E M)  E{M/x} Recall semantics rule: ( x.E M)  E{M/x} Applicative Order Normal Order

C. Varela13 Combinators A lambda calculus expression with no free variables is called a combinator. For example: I: x.x (Identity) App: f. x.(f x)(Application) C: f. g. x.(f (g x))(Composition) L: ( x.(x x) x.(x x))(Loop) Cur: f. x. y.((f x) y)(Currying) Seq: x. y.( z.y x)(Sequencing--normal order) ASeq: x. y.(y x)(Sequencing--applicative order) where y denotes a thunk, i.e., a lambda abstraction wrapping the second expression to evaluate. The meaning of a combinator is always the same independently of its context.

C. Varela14 Combinators in Functional Programming Languages Most functional programming languages have a syntactic form for lambda abstractions. For example the identity combinator: x.x can be written in Oz as follows: fun {$ X} X end and it can be written in Scheme as follows: (lambda(x) x)

C. Varela15 Currying Combinator in Oz The currying combinator can be written in Oz as follows: fun {$ F} fun {$ X} fun {$ Y} {F X Y} end It takes a function of two arguments, F, and returns its curried version, e.g., {{{Curry Plus} 2} 3}  5

C. Varela16 Exercises 20.Lambda Calculus Handout Exercise Lambda Calculus Handout Exercise Lambda Calculus Handout Exercise Lambda Calculus Handout Exercise 6.