Pure Lambda Calculus. Lecture 17

Similar documents
Lambda Calculus. Lecture 4 CS /26/10

1 Scope, Bound and Free Occurrences, Closed Terms

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

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

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

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

Concepts of programming languages

Fundamentals and lambda calculus

Introduction to the Lambda Calculus. Chris Lomont

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

Lambda Calculus. Variables and Functions. cs3723 1

Lambda Calculus. Lambda Calculus

CITS3211 FUNCTIONAL PROGRAMMING

Course Notes Equational Programming: Lambda Calculus. Femke van Raamsdonk

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

Constraint-based Analysis. Harry Xu CS 253/INF 212 Spring 2013

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

Lexicografie computationala Feb., 2012

The Untyped Lambda Calculus

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

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

1 The λ-calculus. Introduction

The Untyped Lambda Calculus

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

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

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. Lambda Calculus

VU Semantik von Programmiersprachen

Recursive Definitions, Fixed Points and the Combinator

Functional Programming

Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages

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?

Untyped Lambda Calculus

Type Systems Winter Semester 2006

The Untyped Lambda Calculus

3.1 λ-calculus: Syntax

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

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

Chapter 5: The Untyped Lambda Calculus

Lambda Calculus and Extensions as Foundation of Functional Programming

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.

CIS 500 Software Foundations Fall September 25

Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in...

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Formal Systems and their Applications

CMSC 330: Organization of Programming Languages

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

> module Lambda where > import Data.List -- I need some standard list functions

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

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

CSE 505: Concepts of Programming Languages

CS 6110 S14 Lecture 1 Introduction 24 January 2014

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

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

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

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

Functions as data. Massimo Merro. 9 November Massimo Merro The Lambda language 1 / 21

An Inverse Lambda Calculus Algorithm. For Natural Language Processing. Marcos Alvarez Gonzalez

1.3. Conditional expressions To express case distinctions like

Lecture #3: Lambda Calculus. Last modified: Sat Mar 25 04:05: CS198: Extra Lecture #3 1

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

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

CMSC 330: Organization of Programming Languages

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

Lambda Calculus and Type Inference

Lecture 5: The Untyped λ-calculus

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

Overview. A normal-order language. Strictness. Recursion. Infinite data structures. Direct denotational semantics. Transition semantics

If a program is well-typed, then a type can be inferred. For example, consider the program

Advanced Seminar Softwareengineering - Denotational semantics

CSE-321 Programming Languages 2012 Midterm

Introduction to the λ-calculus

Part I. Historical Origins

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

n λxy.x n y, [inc] [add] [mul] [exp] λn.λxy.x(nxy) λmn.m[inc]0 λmn.m([add]n)0 λmn.n([mul]m)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.

Lecture Notes on Data Representation

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

The Typed λ Calculus and Type Inferencing in ML

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

Lambda Calculus-2. Church Rosser Property

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

Functional Programming

Costly software bugs that could have been averted with type checking

Elixir, functional programming and the Lambda calculus.

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Denotational Semantics; Lambda Calculus Basics Section and Practice Problems Week 4: Tue Feb 13 Fri Feb 16, 2018

Abstract register machines Lecture 23 Tuesday, April 19, 2016

Basic Foundations of Isabelle/HOL

Lambda Calculus.

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

CS 242. Fundamentals. Reading: See last slide

Lambda Calculus and Type Inference

An Introduction to the Lambda Calculus

The Arrow Calculus (Functional Pearl)

Transcription:

Pure Lambda Calculus Lecture 17

Lambda Calculus Lambda Calculus (λ-calculus) is a functional notation introduced by Alonzo Church in the early 1930s to formalize the notion of computability. Pure λ-calculus is an abstract model of computation and is used to study the computation with functions. It is mainly concerned with functional applications and the evaluation of λ-expressions by techniques of substitution. The λ-calculus is an expressive and sufficiently powerful simple language with few constructs and a simple semantics and can be used to express all computable functions.

Cont Pure λ-calculus does not have any built-in functions or constants but these are included in applied λ- calculus. Different languages are generated from different choices of functions and constants. Functional programming languages ( e.g., LISP, SCHEME, POP-2, SML) are based on applied λ- calculus. Calculation in the λ-calculus is by rewriting (reducing) a λ-expression to a normal form. For pure λ-calculus, λ-expressions are reduced by substitution. Every occurrences of the parameter in the body are replaced with (copies of) the argument. In extended λ-calculus, we also apply the usual reduction rules.

Pure λ-calculus Mainly three constructs to define λ-term variables, function application and function abstraction. Notational conventions A variable denoted by x, y, z, f, g, λ-term denoted by M, N, P, Q,

λ-term All variables are λ-terms and are called atoms. If M and N are arbitrary λ-terms, then (MN) is a λ-term, called function application. More usual notation for function application is M(N) but historically (MN) has become standard in λ-calculus. If M is any λ-term and x is any variable, then (λx.m) is a λ-term. This is called an function abstraction.

Cont Formally, the grammar of λ-term in pure λ- calculus is defined as: <λ-term> :: = x (MN) (λx.m) Examples: Following are examples of λ-terms. 1. x 2. (x y) 3. (λx. x) 4. (λx. (y z) ) 5. ((λx. x) y) 4. (x (λy. y)) 5. (λx. (λy. (x y))) 6. (λx. y) (λy. z)

Informal Interpretation of λ-term A term (λx. M) represents a function whose value at an argument N such as (λx. M)N is calculated by substituting N for all free occurrence of x in M. Example: (λf. f(f y)) represents an operation of applying a function twice to an object denoted by y. e.g., (λf. f(f y)) N = N(N y) If M has been interpreted as a function, then (MN) is interpreted as the result of applying M to an argument N provided the result is meaningful. Example: If M = (λx. x y), then MN = (λx. x y)n = N y

Cont Definition: The λ-expression of the form (λx. y) is called a constant function which when applied to any argument N gives y as a result e.g., (λx. y) N = y or (λx. y) M = y. Definition: The λ-expression of the form (λx. x) is called identity function which when applied to any argument N gives itself e.g., (λx. x) N = N. Pure lambda calculus is untyped and functions can be applied freely. The λ-term (x x) is valid, where variable x is applied to itself. But in notion of computation, it may not be meaningful. For the sake of simplicity, the following syntactic conventions are used to minimize brackets. MNPQ is same as (((MN)P)Q) - (left associative). λx. MN is same as (λx. (MN))

Currying of Function Currying of function means λ-function with more arguments can be expressed as a function of less number of arguments. We can write λx 1. (λx 2. (. (λx n.m) )..) = λx 1 x 2... x n.m λ-function with more variables is right associative. if (λxy. x y) is applied to a single argument say, z then (λxy. x y) z results in λy. z y if (λxyz. x y z) is applied to two argument say, p & q then (λxyz. x y z) p q results in λz. p q z λ-function with n arguments is when applied on one argument, then it reduces to a function of (n-1) arguments two arguments, then it reduces to a function of (n-2) arguments and so on.

Scope of λ-function The scope of various λ s in the following function are given below: (λ 1 y. y x (λ 2 x. y (λ 3 y. z)x)) v w Scope of λ 1 is y x (λ 2 x. y (λ 3 y. z)x) Scope of λ 2 is y(λ 3 y. z)x Scope of λ 3 is z (v w) does not fall under the scope of any λ.

Definitions Definition: An occurrence of a variable x in a λ-term P is bound iff it is in a part of P with the form (λx. M) otherwise x is said to be free variable. The set of all such variables is denoted by FV(P). Definition: A term is closed if it does not have free variables. Substitutions: The notation M [N/x] means to get a result after substituting N for each free occurrence of x in a term M. The substitution is said to be valid if no free variable in N becomes bound after substitution.

Substitution Rules There are few substitution rules: 1. x [N/x] = N 2. y [N/x] = y, y x 3. (P Q) [N/x] = (P [N/x]) (Q [N/x]) 4. (λx. M) [N/x] = λx. M (since x is bound in M thus can not be replaced by N) 5. (λy. M) [N/x] = λy. M [N/x], if y x and y FV(N) 6. (λy. M) [N/x] = λz. ((M [z/y]) [N/x] ), if y x and y ε FV(N) and z FV(MN). Some obvious results: M [x/x] = M M [N/x] = M, if x FV(M) FV(M [N/x]) = FV(N) (FV(M) {x}), if x FV(M) FV(MN) = FV(M) FV(N) FV(λx. M) = FV(M) {x} FV(xy) = {x,y}

Examples Evaluate the following λ-expressions (constant functions in following examples) using substitution rules. 1. (λy. x) [z/x] Using rule (5), we get (λy. x) [z/x] = λy. x [z/x] Further, using rule (1), we get = λy. z Hence, (λy. x) [z/x] = λy. z 2. (λy. x) [y/x] Here rule (5) can not be applied as y ε FV(N) = {y}. Using rule (6), we get (λy. x) [y/x] = λz. ((x [z/y]) [y/x]) = λz. x [y/x] {using rule (2)} = λz. y {using rule (1)} Hence, (λy. x) [y/x] = λz. y

Cont 3. (λy. x (λx. x)) [(λy. x y)/x] Here N = (λy. x y), FV(N) = {x} M = x (λx. x) Since y FV(N)}, using rule 5, we get (λy. x (λx. x) ) [ (λy. x y)/x] = λy. (x (λx. x)) [(λy. x y)/x] using (3), we get = λy. x [(λy. x y)/x] (λx. x) [(λy. x y)/x] Using (1) and (4) = λy. (λy. x y ) (λx. x) Hence, (λy. x(λx. x) ) [(λy. x y)/x] = λy. (λy. x y) (λx. x)

λ-conversion Rules for simplification The λ-expression (λ-term) is simplified by using conversion or reduction rules. There are three types of λ - conversions rules. These make use of substitution explained above. α-conversion (alpha) β-conversion (beta) η-conversion (eta)

Few Definitions Definition: Let a term P contains an occurrence of (λx.m) and let y FV(M). Then the act renaming x by y is replacing (λx. M) by (λy. M [y/x]). It is called a change of bound variable in P. Definition: P α-converts to Q iff Q has been obtained from P by finite series of changes of bound variables. The terms P and Q have identical interpretations and play identical roles in any application of λ-calculus. Definition: Two terms M and N are congruent if M α-converts to N. It is denoted by M α N.

α-conversion rule Any abstraction of the form λx. M can be converted to λy. M [y/x] provided the substitution is valid. This is called α-conversion or α-reduction rule. α-reduction Rule: λx. M α λy. M [y/x] Examples: 1. λx. x y α λz. z y, whereas λx. (x y) α λy. (y y) 2. λx y. x(x y) α λu z. u(u z) Proof: λx y. x(x y) = λx. (λy. x(x y) ) (currying of function) α λx. (λz. x(x z) ) α λu. (λz. u(u z) ) α λu z. u(u z)

β-conversion rule A λ-expression (λx. M)N represents a function (λx. M) applied to an argument N. Any λ-expression of the form (λx. M)N is reduced to M [N/x] provided the substitution of N for x in M is valid. This type of reduction is called β-conversion or β-reduction rule. It is the most important conversion rule.

Cont Definition: Any term of the form (λx. M) N is called a β-redex (redex stands for reducible expression) and the corresponding term M [N/x] is called its contractum. Definition: If a term P contains an occurrence of (λx.m) N and if we replace that occurrence by M [N/x] to obtain a result Q, then we say that we have contracted the redex occurrence in P or P β-contracts to Q (denoted as P β Q). Alternatively, P β-reduces to Q (P β Q) iff Q is obtained from P by finite (perhaps empty) series of β-contractions. β-reduction Rule: (λx. M)N β M [N/x]

Examples β-reduction 1. (λx. x) y x [y/x] y Hence, (λx. x) y β y 2. (λx. x(x y)) N x(x y) [N/x] N(N y) Hence (λx. x(x y)) N β N(N y) 3. (λx. y) N y [N/x] y Hence (λx. y) N β y

Examples cont 4. Show that (λx. (λy. y x) z) v β z v Proof: (λx. (λy. y x) z) v ((λy. y x)z) [v/x] (λy. y v) z (y v) [z/y] z v Hence (λx.(λy. y x) z) v β z v

η-conversion Any abstraction of the form (λx. M x) is reduced to M if x is not free in M. A λ-expression (abstraction form) to which η-reduction can be applied is called η-redex. The rule of η-conversion expresses the property that two functions are equal if they give the same results when applied to the same arguments. η-reduction Rule: (λx. M x) M

Equality of λ-expressions Two λ-expressions M and N are equal if they can be transformed into each other by finite sequence of λ-conversions. The meaning of λ-expressions are preserved after applying any conversion rule. For example, λx. x is same as λz. z. We represent M equal to N by M = N.

Properties of Equality Equality satisfies the following properties (equivalence relation in set theory): Idempotence: A term M is equal to itself e.g., M = M. Commutativity: If M is equal to N, then N is also equal to M i.e., if M = N, then N = M. Transitivity: If M is equal to N and N is equal to P then M is equal to P i.e., if M = N and N = P then M = P.

Identical Expressions Definition: Two λ-expressions M and N are identical, if they consists of same sequence of characters. Example: λx.xy and λz.zy are not identical but they are equal. So, identical expressions are equal but converse may not be true.

Computation with Pure λ-term Computation in the λ-calculus is done by rewriting (reducing) a λ-expression into a simple form as far as possible. The result of computation is independent of the order in which reduction is applied. A reduction is any sequence of λ-conversions. If a term can not be further reduced, then it is said to be in a normal form.

Normal Form Definition: A λ-expression is said to be in normal form if no beta-redex (e.g. a sub exp of the form (λx. M)N ) occurs in it. Example: The normal form of the λ-expression (λx. (λy. y x) z) v z v It is not necessary that all λ-expressions have the normal forms because λ-expression may not be terminating.

Example of λ-expression with no Normal Form Example: λ-expression (λx. xxy ) (λx. xxy ) does not have a normal form because it is non terminating. (λx. xxy ) (λx. xxy ) (xxy) [(λx. xxy)/x] (λx. xxy ) (λx. xxy ) y (xxy) [(λx. xxy)/x] y (λx. xxy ) (λx. xxy ) yy : : This reduction is nonterminating

Reduction Order λ-conversion rules provide the mechanism to reduce a λ-expression to normal form but do not tell us what order to apply the reductions when more than one redex is available. Mathematician Curry proved that if an expression has a normal form, then it can be found by leftmost reduction. The leftmost reduction is called lazy reduction because it does not first evaluate the arguments but substitutes the arguments directly into the expression. Curried functions (f x y z) use lazy reduction. Eager reduction is one where the arguments are evaluated before substitution. Function f(x,y,z) use eagar reduction.