Lambda Calculus-2. Church Rosser Property

Similar documents
Lambda Calculus. Lambda Calculus

CITS3211 FUNCTIONAL PROGRAMMING

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08

Functional Programming

Computer Science 203 Programming Languages Fall Lecture 10. Bindings, Procedures, Functions, Functional Programming, and the Lambda Calculus

CMPUT 325 : Lambda Calculus Basics. Lambda Calculus. Dr. B. Price and Dr. R. Greiner. 13th October 2004

Lambda Calculus. Lecture 4 CS /26/10

Fundamentals and lambda calculus

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

Chapter 5: The Untyped Lambda Calculus

Introduction to the Lambda Calculus

VU Semantik von Programmiersprachen

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Functional Languages and Higher-Order Functions

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

Concepts of programming languages

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility

CITS3211 FUNCTIONAL PROGRAMMING. 14. Graph reduction

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

CMSC 330: Organization of Programming Languages. Lambda Calculus

Activity. CSCI 334: Principles of Programming Languages. Lecture 4: Fundamentals II. What is computable? What is computable?

Introduction to Lambda Calculus. Lecture 7 CS /08/09

Pure Lambda Calculus. Lecture 17

Lambda Calculus and Type Inference

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

Lambda Calculus and Extensions as Foundation of Functional Programming

Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

Untyped Lambda Calculus

CSCI.4430/6969 Programming Languages Lecture Notes

Lambda Calculus. Variables and Functions. cs3723 1

Lecture 9: More Lambda Calculus / Types

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

1 Scope, Bound and Free Occurrences, Closed Terms

Pure (Untyped) λ-calculus. Andrey Kruglyak, 2010

Functional Programming

Introduction to the λ-calculus

Functional Programming. Pure Functional Languages

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Functional Programming. Pure Functional Languages

Urmas Tamm Tartu 2013

Recursive Definitions, Fixed Points and the Combinator

J. Barkley Rosser, 81, a professor emeritus of mathematics and computer science at the University of Wisconsin who had served in government, died

Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

Course Notes Equational Programming: Lambda Calculus. Femke van Raamsdonk

3.1 λ-calculus: Syntax

Lambda Calculus. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

Type Systems Winter Semester 2006

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

Recursion. Lecture 6: More Lambda Calculus Programming. Fixed Points. Recursion

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

lambda calculus History 11/28/13 Jianguo Lu A formal system designed to inves:gate func:on defini:on, func:on applica:on, and recursion.

Lecture 5: The Untyped λ-calculus

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur

10.6 Theoretical Foundations

301AA - Advanced Programming

From the λ-calculus to Functional Programming Drew McDermott Posted

A Quick Overview. CAS 701 Class Presentation 18 November Department of Computing & Software McMaster University. Church s Lambda Calculus

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

CSE 505: Concepts of Programming Languages

Last class. CS Principles of Programming Languages. Introduction. Outline

Plan. Syntax Substitution Operational Semantics ( with contexts!) Evaluations strategies Equality

Functional Programming. Overview. Topics. Recall λ-terms. Examples

Introduction to the Lambda Calculus. Chris Lomont

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

λ calculus is inconsistent

The Untyped Lambda Calculus

Grades. 10% - participation & exercises. 90% - term paper

Functional Programming. Big Picture. Design of Programming Languages

Homework. Lecture 7: Parsers & Lambda Calculus. Rewrite Grammar. Problems

Lecture 5: Lazy Evaluation and Infinite Data Structures

The Untyped Lambda Calculus

INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS. Instructors: Crista Lopes Copyright Instructors.

CSE 3302 Programming Languages Lecture 8: Functional Programming

Implementing functional languages with abstract machines

Lambda Calculus. Concepts in Programming Languages Recitation 6:

CIS 500 Software Foundations Fall September 25

Chapter 15. Functional Programming Languages

dynamically typed dynamically scoped

Lexicografie computationala Feb., 2012

The Untyped Lambda Calculus

Appendix A A Tutorial on Combinator Graph Reduction

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CITS3211 FUNCTIONAL PROGRAMMING. 10. Programming in the pure λ calculus

CMSC 330: Organization of Programming Languages

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

Functional Programming

More Lambda Calculus and Intro to Type Systems

Lambda Calculus and Type Inference

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Chapter 5: The Untyped Lambda Calculus

Self-replicating Expressions in the Lambda Calculus

An Introduction to the Lambda Calculus

Interpretations of Classical Logic Using λ-calculus. Freddie Agestam

Transcription:

Lambda Calculus-2 Church-Rosser theorem Supports referential transparency of function application Says if it exists, normal form of a term is UNIQUE 1 Church Rosser Property Fundamental result of λ-calculus: Result of a computation is independent of the order in which β-reductions are applied Leads to referential transparency in functional PL s Another interpretation is that most terms in the λ-calculus have a normal form, a form that cannot be reduced any simpler; Church Rosser says if a normal form exists, then all reduction sequences lead to it 2 1

Normal Form Does every λ-expression have a normal form? NO, because there are terms which cannot be simplified, yet they contain redices (λx.x x) (λx.x x) = (λy. y y) (λx.x x), α-reduction = (λx.x x) (λx.x x), β-reduction this term has no normal form (λx.x x x) (λx.x x x) = (λy. y y y) (λx.x x x), α-red = (λx.x x x) (λx.x x x) (λx.x x x),β-red this term grows as we apply β-reductions! 3 Normal Form If add6 λx. x+6, twice λfλx. f (f x), what is value of (twice add6)? (twice add6) = (λf.λz.f (f z)) (λx.x+6) = λz. ((λx.x+6) ((λx.x+6) z)) = λz. ((λx.x+6) (z+6)) = λz. (z + 12), normal form free normal form of {λx. ((λz.z x) (λx.x))} y? {λx. ((λz.z x) (λx.x))} y = {λx. ((λx.x) x)} y = {λx. x} y = y bound 4 2

Equality of Terms How check equality of 2 terms? Reduce each term to its normal form and compare But whether or not a term has a normal form is undecidable (related to halting problem for Turing machies) Same term may have terminating and nonterminating β-reduction sequences; if at least one terminates, use its result as the normal form for that term 5 Church Rosser Property (GHT)Theorem 1: If a λ-expression reduces to a normal form, it is unique (GHT)Theorem 2: If we always reduce leftmost redex first, the reduction sequence will terminate in a normal form, if it exists..a.b both A and B are redices. if first λ in A is to the left of first λ in B, then A is to the left of B A redex to left of all other redices in a λ-expression is leftmost Principles of Functional Programming, H. Glaser, C. Hankin, D. Till, Prentice Hall, 1984 6 3

Church Rosser Property (Better Statement) (Sethi) Theorem: For λ-expressions M,P,Q, let stand for a sequence of α and β-reductions. if M P and M Q then a term R such that P R and Q R Theorem says all reduction sequences progress towards the same end result if they all terminate M P Q R 7 Demonstration of CR by Example (λx.λy.x-y) ((λz.z) 2) ((λr.r+2) 3) first eval ~ f g h second eval substituting for x first: = (λy.((λz.z) 2) - y) ((λr.r+2) 3), simplify = (λy.2-y) ((λr.r+2) 3), apply lambda-expr = 2 - ((λr.r+2) 3), apply lambda-expr = 2-5 = -3 8 4

Demonstration of CR by Example (λx.λy.x-y) ((λz.z) 2) ((λr.r+2) 3) substitute for y first: = (λx.x- ((λr.r+2) 3)) ((λz.z) 2), simplify = (λx.x - 5) ((λz.z) 2), apply lambda-expr = (((λz.z) 2) 5), apply lambda-expr = ( 2-5) = -3, the same result! subs%tu%ng for x first: = (λy.((λz.z) 2) - y) ((λr.r+2) 3) = (λy.2- y) ((λr.r+2) 3) = 2 - ((λr.r+2) 3) = 2-5 = - 3 9 Call by Name Can result in some parameter being evaluated several times - inefficient Evaluates arguments only when they are needed (Algol60 thunks) Abandoned in modern PLs because of inefficiency However, guaranteed to reach a normal form if it exists 10 5

Call by Value Efficient Potentially does a calculation that may not be used (if fcn is not strict in that parameter) Can lead to non-terminating computation Used in C, Pascal, C++, Scheme, functional languages Often obtains a normal form in real programs 11 Call by Need Lazy evaluation - once we evaluate an argument, then memoize its value to use again, if needed In between two other methods: value and name Accomplished by embedding a pointer to a value instead of the argument itself in the expression. Then, when value is first calculated, it is saved so it will be available for other uses 12 6

Call by Need Allows use of unbounded streams of input as well What if we need a function to generate list(n), a list of length n? hd ( tl (list(n)) ) needs only the first 2 elements to be generated; system will only evaluate this many elements which prefix the list. 13 Reduction Order Distinguishing order of applying β-reductions only matters when some reduction order leads to a non-terminating computation Sethi, p560: Leftmost outermost redex first is call by name (normal order) Leftmost, innermost redex first is call by value Where inner and outer refer to nesting of terms (λ yz. (λx.x) z (y z)) (λ x.x) 14 7

Reduction Order Start with fully parenthesized expression: (λv. e) (i) - always reduce e first (c b) (ii) - if c is not of form (i), then reduce c until it is of that form. Then, we have a choice as to how to proceed: call by name: reduce (c b) without further reducing inside c or b. call by value: reduce any redices in c, then those in b, and then reduce (c b). 15 Example 1 (Sethi, p560) {[λy.λz. ((λx.x) z) (y z))] (λx.x)} = (c b) call by value: reduce c. [λy.λz.(z (y z))] (λx.x) = (c b) where b already reduced. reduce (c b) yielding λz.( z ((λx.x) z) ) = λz.(z (c b )). reduce (c b ) which yields λz.( z z), the final term. call by name: c is an abstraction (form i). so instantiate b directly into c yielding λz.(((λx.x) z) ((λx.x) z)) = λ z. (c* b*) now reduce c* so we get an abstraction (form i.), yielding z. then can perform final reduction of λz.(z ((λx.x) z)), yielding λz. z z, the final term, same as above. 16 8

Example 2 (((λx.λy.x) z) ((λr.r r) (λs. s s))) = (c b). call by value: reduce c to yield ((λy.z) ((λr.r r) (λs. s s))) which is ((λy.z) (c b )). reduce (c b ) yielding ((λy.z) ((λs.s s) (λs. s s))). we end up with a similar term b. repeating this reduction will result in a nonterminating computation call by name: reduce c to yield ((λy.z) ((λr.r r) (λs. s s))). now substitute b into the reduced c, yielding z, because there is no bound y in λy.z. z is the normal form for the above term, by definition. 17 c b Example 3 {(λz. (λx.x+6) ((λx.x+6) z) ) 1} = { c b } ( c, b ) call by value: reduce redices in c = (c b ) where b = (c b ). (c b ) evaluates to b = z+6, yielding {(λz. (λx.x+6) (z+6)) 1}. now evaluating (c b ) yields {(λz. (z+6)+6) 1} = {(λz. z+12) 1} now evaluating {c b} yields 1 + 12 = 13. call by name: c is of correct form, an abstraction (form i.). so substitute b into c yielding ((λx.x+6) ((λx.x+6) 1)) = (c* b*). substitute b* into c* yielding ((λx.x+6) 1) + 6 = (c^ b^) + 6. substitute b^ into c^ yielding (1 + 6) + 6 = 7+6 = 13. 18 9