Lambda Calculus. Lambda Calculus

Similar documents
Lambda Calculus-2. Church Rosser Property

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

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

Pure Lambda Calculus. Lecture 17

Lambda Calculus. Variables and Functions. cs3723 1

Fundamentals and lambda calculus

Lambda Calculus. Lecture 4 CS /26/10

1 Scope, Bound and Free Occurrences, Closed Terms

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

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?

Chapter 5: The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages

CITS3211 FUNCTIONAL PROGRAMMING

Lecture 5: The Untyped λ-calculus

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

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

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

CMSC 330: Organization of Programming Languages

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

Introduction to the Lambda Calculus. Chris Lomont

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Type Systems Winter Semester 2006

Functional Programming

Introduction to the Lambda Calculus

Recursive Definitions, Fixed Points and the Combinator

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

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

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

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

VU Semantik von Programmiersprachen

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

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.

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

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

CIS 500 Software Foundations Fall September 25

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

Lexicografie computationala Feb., 2012

Untyped Lambda Calculus

CMSC330. Objects, Functional Programming, and lambda calculus

Untyped Lambda Calculus

dynamically typed dynamically scoped

3.1 λ-calculus: Syntax

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

Introduction to the λ-calculus

Functional Programming

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

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

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

CSE 505: Concepts of Programming Languages

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

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

Lambda Calculus and Extensions as Foundation of Functional Programming

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

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

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

Concepts of programming languages

From the λ-calculus to Functional Programming Drew McDermott Posted

The Untyped Lambda Calculus

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

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

Functional Languages. Hwansoo Han

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

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

Part I. Historical Origins

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

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

The Untyped Lambda Calculus

CS 6110 S14 Lecture 1 Introduction 24 January 2014

The Untyped Lambda Calculus

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

Elixir, functional programming and the Lambda calculus.

Lambda Calculus and Type Inference

Programming Languages

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

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

Lambda Calculus.

Programming Language Pragmatics

Introductory Example

An Introduction to the Lambda Calculus

Lambda Calculus. Concepts in Programming Languages Recitation 6:

Chapter 5: The Untyped Lambda Calculus

CSCI.4430/6969 Programming Languages Lecture Notes

Lambda Calculus LC-1

Chapter 11 :: Functional Languages

CMSC 330: Organization of Programming Languages

CS 242. Fundamentals. Reading: See last slide

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

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

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages

Course Notes Equational Programming: Lambda Calculus. Femke van Raamsdonk

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

301AA - Advanced Programming

Transcription:

Lambda Calculus Formalism to describe semantics of operations in functional PLs Variables are free or bound Function definition vs function abstraction Substitution rules for evaluating functions Normal form Equivalent in descriptive power to Turing machines Substitution rules β reduction, α reduction, η-reduction LambdaCalculus-1, CS5314 Sp2016 BGRyder 1 Lambda Calculus A theory of functions Equivalent in descriptive power to Turing machines Syntax Exp ::= Var λ Var.Exp (Exp1 Exp2) λx.(x z) corresponds to (lambda (x) (x z)) in Scheme How to express a function call? Use notion of substitution Substitute E for y in M, {E/y} M ((λx.λy.(+ x y)) 2 3)-->((λy.(+ 2 y)) 3)--> (+ 2 3) LambdaCalculus-1, CS5314 Sp2016 BGRyder 2 1

Evaluating Functions Not so easy to do if foo is (λx. λx.x) then (foo 2) is λx.x not 2 (x is a parameter of the function λx.x that is an argument and unrelated to x. if area is (λx.λy. (* x y)) then (area 3 y) should be (* 3 y) not (* y y), but 2nd result happens by blindly following simple substitution free/bound variables distinguish these cases λx.(x + y) y is free, x is bound LambdaCalculus-1, CS5314 Sp2016 BGRyder 3 Rules of Lambda Calculus α rule: choice of parameter names doesn t matter, λx. M = λy. {y/x} M if y not free in M e.g., λx. (* 2 x) is same as λz. (* 2 z) β rule: function application is substitution of argument for free parameter e.g., (λx.(* 2 x) 4) is (* 2 4); more generally, ((λx. M) E) is {E/x} M η rule: these 2 functions always yield same results on equal arguments: ((λx. M) E) and E, if x is not free in E. LambdaCalculus-1, CS5314 Sp2016 BGRyder 4 2

Evaluation Order More than one possible evaluation order e.g., ((λx. (* x x)) (+ 2 3)) can be evaluated as ((λx.(* x x)) 5) -->(* 5 5) --> 25 (by value) OR ((λx.(* x x) (+ 2 3)) --> (* (+ 2 3) (+ 2 3)) --> (* 5 5) --> 25 (by name) by name evaluation is lazy in that the argument is not evaluated unless it is used, e.g., (define (foo v w) (if (> v 0) (* v v) (* w v))) evaluates as (foo 3 (fact 500)) will not need to evaluate (fact 500), an expensive calculation, unless it is necessary because of the code of foo by value evaluation is eager LambdaCalculus-1, CS5314 Sp2016 BGRyder 5 Normal Form Normal form implies there are no more function evaluations possible in the lambda term Church-Rosser theorem 1936 Alonzo Church and J. Barkley Rosser both mathematicians/ logicians Normal forms are unique If there is a normal form, (by name) substitution will find it Not every expression has a normal form e.g., if bar is (λx.(x x)), evaluate (bar bar) LambdaCalculus-1, CS5314 Sp2016 BGRyder 6 3

Lambda Calculus Universal theory of functions λ-calculus (Church, Rosser), recursive function theory (Kleene), Turing machines (Turing) all were formal systems to describe computation, developed at the same time in the 1930 s Shown formally equivalent to each other Results from one, apply to others LambdaCalculus-1, CS5314 Sp2016 BGRyder 7 Lambda Calculus Conjecture: class of programs written in λ- calculus is equivalent to those which can be simulated on Turing machines. All partial recursive functions can be defined in λ-calculus. Pure λ-calculus involves functions with no side effects and no types. LambdaCalculus-1, CS5314 Sp2016 BGRyder 8 4

Lambda Calculus Teminology Function: a map from a domain to a range Terms: variable (X) function abstraction or definition (λx.m) function application (M N) LambdaCalculus-1, CS5314 Sp2016 BGRyder 9 Function Definition (Abstraction) F(y) = 2 + y -- mathematics F λ y. 2+y -- λ calculus bound variable or argument function body λ x.x (identity function) λ y. 2 (constant function whose value is 2) LambdaCalculus-1, CS5314 Sp2016 BGRyder 10 5

Function Application Process: take the argument and substitute it everywhere in the function body for the parameter (F 3) is 2 + 3 = 5; ((λ x.x) λ y.2) is λ y.2; ((λ z. z+5) 3) is 3+5 = 8 Functions are first class citizens 1. Can be returned as a value 2. Can be passed as an argument 3. Can be put into a data structure as a value 4. Can be the value of an expression LambdaCalculus-1, CS5314 Sp2016 BGRyder 11 Relation to C Function Pointers? Can simulate #1-4 with C function pointers, but this abstraction is closer to the machine than a function abstraction. Functions as values are defined more cleanly in Lisp and its descendants. No analogue in C for an unnamed function, (Lisp lambda expression) LambdaCalculus-1, CS5314 Sp2016 BGRyder 12 6

Function Application Is a left associative operator (f g h) is ((f g) h) λ x.m x is same as λ x.(m x) Function application has highest precedence Currying (cf. Haskell Curry) Area of triangle is λ b. λ h.(b*h)/2 (Area 3) is a function, λ h.(3*h)/2, that describes the area of a family of triangles all with base 3 ((Area 3) 7) = 3 * 7 / 2 = 10.5 Recall that in curried form, a function takes its arguments one-by-one LambdaCalculus-1, CS5314 Sp2016 BGRyder 13 Type Signatures Can write function Area (λ b. λ h.(b*h)/2) in two ways un-curried: α * β γ, given b,h as a pair of values, the function returns area curried: α (β γ), given b, returns a function to calculate area when given h(height) LambdaCalculus-1, CS5314 Sp2016 BGRyder 14 7

Free and Bound Variables Bound variable: x is bound when there is a corresponding λx in front of the λ expression: (( λ y. y) y) is y Bound free Free variable: x is not bound (analogous to a variable inherited from an encompassing imperative scope) binding occurrence of z (λ y. z) (λ z. z) bound occurrence of z free occurrence of z LambdaCalculus-1, CS5314 Sp2016 BGRyder 15 Free and Bound Variables Sethi, p550 x is free in x, free(x) = x x is free (bound) in Y Z if x is free (bound) in Y or in Z, free(y Z)= free(y) free(z) x V, then x free (bound) in λv.y iff it occurs free (bound) in Y. All occurrences of elements of V are bound in λ V.Y, free(λx.m) = free(m) - {x} x free (bound) in (Y), if x is free (bound) in Y LambdaCalculus-1, CS5314 Sp2016 BGRyder 16 8

Substitution Idea: function application is seen as a kind of substitution which simplifies a term ( (λx.m) N) as substituting N for x in M ; written as {N x} M Rules - Sethi, p551 1. If free variables of N have no bound occurrences in M, then {N x} M is formed by replacing all free occurrences of x in M by N. LambdaCalculus-1, CS5314 Sp2016 BGRyder 17 Substitution plus λa.λb. a+b then (plus 2) λb. 2+b but if we naively evaluate (plus b 3) we get into trouble! problem: (plus b 3) = (λa.λb. a+b b 3) b is a bound = (λb. b+b 3) = 3 + 3 = 6 variable; need to rename before (plus b 3) = (λa.λc. a+c b 3) = (λc. b+c 3) substitute. = b + 3, what we expected! LambdaCalculus-1, CS5314 Sp2016 BGRyder 18 9

Substitution 2. If variable y free in N and bound in M, replace binding and bound occurrences of y by a new variable named z. Repeat until case 1. applies. Examples {u x} x = u //u not bound in M=x {u x} (x u) = (u u) //u not bound in M=(x u) {λx.x x} x = λx.x //λx.x not bound in M=x {u x} y = y //no free occurrences of x in M=y so no sub {u x} λx.x = λx.x //no free occurrences of x in M= λx.x so no sub {u x} (λu.x) = {u x} (λz.x) = λz.u {u x} (λu.u) = {u x} (λz.z) = λz.z Examples of need for change of variables. LambdaCalculus-1, CS5314 Sp2016 BGRyder 19 Reductions β-reduction (λx.m) N = {N x} M with above rules α-reduction (λx.m) = λz.{z x} M, if z not free in M (allows change of bound variable names) η-reduction (λ x.(m x)) = M, if x not free in M (allows stripping off of layers of indirection in function application) See Sethi, Figure 14.1, p 553 for rules about β- equality of terms LambdaCalculus-1, CS5314 Sp2016 BGRyder 20 10

Example Evaluate (λxyz. xz (yz)) (λx. x) (λy. y) (λxyz. (xz (yz))) (λx. x) (λy. y), 2 α-reds + fully parenthesize = [ { ( λabz.( a z (b z))) (λx.x)} (λy.y)] change vars = [ { ( λbz. ((λx.x) z (b z))) } (λy.y)], {λx.x a} = [ { λbz. (((λx.x) z) (b z))} (λy.y)], fully parenthesize = [ {λbz. (z (b z))} (λy.y)], {z x} = [ { λz. (z ((λy.y) z))}], {λy.y b} = { (λz. z z)}, {z y} Note: we picked the order of β-reductions here LambdaCalculus-1, CS5314 Sp2016 BGRyder 21 Substitution Rules Sethi p 555 M {N x} M x N y M if M a variable, then if M x get M, else get N (3.1 GHH) PQ {N x} P {N x} Q result of substitution applied to function application is to apply that substitution to the function and its argument and then perform the resulting application (3.2 GHH) LambdaCalculus-1, CS5314 Sp2016 BGRyder 22 11

Substitution Rules Sethi p 555 M {N x} M 3.3a) λx.p λx.p never substitute for a bound variable within its scope 3.3b) λy.p λy.p if there are no free occurrences of x in P 3.3c) λy.p λy.{n x} P when there are no free occurrences of y in N 3.3d) λy.p λz.{n x} { z y} P when there is a free occurrence of y in N and z is not free in P or N, substitute z for y in P and continue. LambdaCalculus-1, CS5314 Sp2016 BGRyder 23 Substitution Rules All these checks are aimed at ensuring that we don t link variable occurrences that are independent! Our example ((λ a.λ b.a+b) b), would use 3.3d to change variables before doing the substitution Normal form of a term - a form which can allow no further β or η reductions No remaining ((λx.m) N), called a redex or term which can be reduced LambdaCalculus-1, CS5314 Sp2016 BGRyder 24 12

Example {y x} λ y. x y //use 3.3d to change bound var λ z. {y x} ({z y} (x y)) //apply 3.2 for fcn appln λ z. {y x} ({z y} (x) {z y} (y)) //apply 3.1 twice λ z. {y x} (x z) //apply 3.2 λ z. ({y x} (x) {y x} (z)) //apply 3.1 twice λ z. y z //final result; compare this to what we started with! EG from Principles of Functional Programming, H. Glaser, C. Hankin, D. Till, Prentice Hall, 1984 LambdaCalculus-1, CS5314 Sp2016 BGRyder 25 13