Carlos Varela Rennselaer Polytechnic Institute September 4, 2015

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.
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.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Kernel language semantics Carlos Varela RPI Adapted with permission.
C. Varela1 Programming Languages (CSCI 4430/6969) Lambda Calculus Carlos Varela Rennselaer Polytechnic Institute September 17, 2007.
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.
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.
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.
Lambda Calculus Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 11.
-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 박성우.
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 8 LECTURE: LAMBDA CALCULUS SEMANTICS TUTORIAL: exercises.
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.
A LISP interepreter for the Lambda Calculus Functional Programming
Lesson 10 Type Reconstruction
Functional Programming
Chapter 2: Lambda Calculus
CS5205: Foundations in Programming Languages
Conditional Expressions
CS 550 Programming Languages Jeremy Johnson
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Lecture 6: Lambda Calculus
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Lambda Calculus CSE 340 – Principles of Programming Languages
Carlos Varela Rennselaer Polytechnic Institute September 27, 2016
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
Carlos Varela Rennselaer Polytechnic Institute September 1, 2017
More on Lambda Calculus
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.
Declarative Computation Model Kernel language semantics (Non-)Suspendable statements (VRH ) Carlos Varela RPI October 11, 2007 Adapted with.
PROGRAMMING IN HASKELL
Typed Arithmetic Expressions
Carlos Varela Rennselaer Polytechnic Institute September 6, 2016
Lesson2 Lambda Calculus Basics
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Announcements Quiz 6 HW7 due Tuesday, October 30
Carlos Varela Rennselaer Polytechnic Institute September 8, 2017
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
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)
Programming Languages and Compilers (CS 421)
CSE S. Tanimoto Lambda Calculus
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Programming Languages
Lambda Expressions Cases
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:

Carlos Varela Rennselaer Polytechnic Institute September 4, 2015 Lambda Calculus (PDCS 2) alpha-renaming, beta reduction, eta conversion, applicative and normal evaluation orders, Church-Rosser theorem, combinators, higher-order programming, recursion combinator, numbers, booleans Carlos Varela Rennselaer Polytechnic Institute September 4, 2015 C. Varela

Lambda Calculus Syntax and Semantics The syntax of a -calculus expression is as follows: e ::= v variable | v.e functional abstraction | (e e) function application The semantics of a -calculus expression is called beta-reduction: (x.E M)  E{M/x} where we alpha-rename the lambda abstraction E if necessary to avoid capturing free variables in M. C. Varela

Function Composition in Lambda Calculus S: x.(s x) (Square) I: x.(i x) (Increment) C: f.g.x.(f (g x)) (Function Composition) ((C S) I) ((f.g.x.(f (g x)) x.(s x)) x.(i x))  (g.x.(x.(s x) (g x)) x.(i x))  x.(x.(s x) (x.(i x) x))  x.(x.(s x) (i x))  x.(s (i x)) Recall semantics rule: (x.E M)  E{M/x} C. Varela

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

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 e1 e2 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. Varela

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))) and:  y In this example, normal order terminates whereas applicative order does not. Recall semantics rule: (x.E M)  E{M/x} Applicative Order Normal Order C. Varela

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)) Bound Variables Free Variables C. Varela

This reduction erroneously captures the free occurrence of y. Why -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.z.(x z) (y w))  z.((y w) z) where y remains free. C. Varela

-renaming Alpha renaming is used to prevent capturing free occurrences of variables when beta-reducing a lambda calculus expression. In the following, we rename x to z, (or any other fresh variable): (x.(y x) x) (z.(y z) x) Only bound variables can be renamed. No free variables can be captured (become bound) in the process. For example, we cannot alpha-rename x to y. α→ C. Varela

b-reduction (x.E M) E{M/x} Beta-reduction may require alpha renaming to prevent capturing free variable occurrences. For example: (x.y.(x y) (y w)) (x.z.(x z) (y w)) z.((y w) z) Where the free y remains free. α→ b→ C. Varela

h-conversion x.(E x) E if x is not free in E. For example: (x.y.(x y) (y w)) (x.z.(x z) (y w)) z.((y w) z) (y w) α→ b→ h→ C. Varela

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. Varela

Combinators in Functional Programming Languages 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 in Haskell as follows: \x -> x and in Scheme as follows: (lambda(x) x) C. Varela

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. Varela

Recursion Combinator (Y or rec) Suppose we want to express a factorial function in the l calculus. 1 n=0 f(n) = n! = n*(n-1)! n>0 We may try to write it as: f: n.(if (= n 0) 1 (* n (f (- n 1)))) But f is a free variable that should represent our factorial function. C. Varela

Recursion Combinator (Y or rec) We may try to pass f as an argument (g) as follows: f: g.n.(if (= n 0) 1 (* n (g (- n 1)))) The type of f is: f: (Z  Z)  (Z  Z) So, what argument g can we pass to f to get the factorial function? C. Varela

Recursion Combinator (Y or rec) f: (Z  Z)  (Z  Z) (f f) is not well-typed. (f I) corresponds to: 1 n=0 f(n) = n*(n-1) n>0 We need to solve the fixpoint equation: (f X) = X C. Varela

Recursion Combinator (Y or rec) (f X) = X The X that solves this equation is the following: X: (lx.(g.n.(if (= n 0) 1 (* n (g (- n 1)))) ly.((x x) y)) lx.(g.n.(if (= n 0) ly.((x x) y))) C. Varela

Recursion Combinator (Y or rec) X can be defined as (Y f), where Y is the recursion combinator. Y: lf.(lx.(f ly.((x x) y)) lx.(f ly.((x x) y))) Y: lf.(lx.(f (x x)) lx.(f (x x))) You get from the normal order to the applicative order recursion combinator by h-expansion (h-conversion from right to left). Applicative Order Normal Order C. Varela

Natural Numbers in Lambda Calculus |0|: x.x (Zero) |1|: x.x.x (One) … |n+1|: x.|n| (N+1) s: ln.lx.n (Successor) (s 0) (n.x.n x.x)  x.x.x Recall semantics rule: (x.E M)  E{M/x} C. Varela

Booleans and Branching (if) in l Calculus |true|: x.y.x (True) |false|: x.y.y (False) |if|: b.lt.le.((b t) e) (If) (((if true) a) b) (((b.t.e.((b t) e) x.y.x) a) b)  ((t.e.((x.y.x t) e) a) b)  (e.((x.ly.x a) e) b)  ((x.ly.x a) b) (y.a b) a Recall semantics rule: (x.E M)  E{M/x} C. Varela

Exercises PDCS Exercise 2.11.7 (page 31). Prove that your addition operation is correct using induction. PDCS Exercise 2.11.12 (page 31). Test your representation of booleans in Haskell. C. Varela