Announcements Quiz 6 HW7 due Tuesday, October 30 Questions? We’ll have grades for HWs 4 over the weekend Fall 18 CSCI 4430, A Milanova/BG Ryder
Announcements Exam 2 on Friday, November 2nd Topics You are allowed 2 “cheat” pages only Practice tests under Course Materials Topics Binding and scoping Attribute grammars Scheme Lists, recursion, higher-order functions (especially map and fold), tail recursion, let expressions, scoping Lambda calculus and reduction strategies (as known as evaluation order)
Last class Scheme Tail recursion Binding with let expressions Scoping in Scheme, closures Scoping, revisited Fall 18 CSCI 4430, A Milanova/BG Ryder
Today’s Lecture Outline Lambda calculus Introduction Syntax and semantics Free and bound variables Substitution, formally Rules of lambda calculus Normal forms Reduction strategies Fall 18 CSCI 4430, A Milanova/BG Ryder
Lambda Calculus Reading: Scott, Ch. 11 on CD
Lambda Calculus A theory of functions Theory behind functional programming Turing-complete: any computable function can be expressed and evaluated using the calculus “Lingua franca” of programming language research Lambda () calculus expresses function definition and function application f(x)=x*x becomes x. x*x g(x)=x+1 becomes x. x+1 f(5) becomes (x. x*x) 5 5*5 25 Fall 18 CSCI 4430, A Milanova
Syntax of Pure Lambda Calculus Convention: notation f, x, y, z for variables; E, M, N, P, Q for expressions E ::= x | ( x. E1 ) | ( E1 E2 ) A -expression is one of Variable: x Abstraction (i.e., function definition): x. E1 Application: E1 E2 -calculus formulae (e.g., ( x. (x y) )) are called expressions or terms ( x. (x y) ) corresponds to (lambda (x) (x y)) in Scheme! I use notation f, x, y, z for variables; E, M, N, P, Q for expressions Fall 18 CSCI 4430, A Milanova/BG Ryder
Syntactic Conventions Parentheses may be dropped from ( E1 E2 ) or ( x.E ) E.g., ( f x ) may be written as f x Function application groups from left-to-right (i.e., it is left-associative) E.g., x y z abbreviates ( ( x y ) z ) E.g., E1 E2 E3 E4 abbreviates ( ( ( E1 E2 ) E3 ) E4 ) Parentheses in x (y z) are necessary! Why? Fall 18 CSCI 4430, A Milanova/BG Ryder
Syntactic Conventions Application has higher precedence than abstraction Another way to say this is that the scope of the dot extends as far to the right as possible E.g., x. x z = x. ( x z ) = ( x. ( x z ) ) = ( x. x z ) ≠ ( ( x. x ) z ) WARNING: This is the most common syntactic convention (e.g., Pierce 2002). However some books give abstraction higher precedence; you might have seen that different convention
Terminology Parameter (also, formal parameter) E.g., x is the parameter in x. x z Argument (also, actual argument) E.g., expression z. z is the argument in (x. x) (z. z) Can you guess what this evaluates to? Fall 18 CSCI 4430, A Milanova/BG Ryder
Currying In lambda calculus, all functions have one parameter How do we express n-ary functions? Currying expresses an n-ary function in terms of n unary functions f(x,y) = x+y, becomes (x.y. x + y) (x.y. x + y) 2 3 (y. 2 + y) 3 2 + 3 = 5 Fall 18 CSCI 4430, A Milanova/BG Ryder
Currying in Scheme (define curried-plus (lambda (a) (lambda (b) (+ a b)))) (curried-plus 3) returns what? Returns the plus-3 function (or more precisely, it returns a closure) ((curried-plus 3) 2) returns what? 5 Scheme departs from the lamda calculus and allows us to define n-ary functions. Of course, we can still defined curried functions in Scheme. Here is an example. Fall 18 CSCI 4430, A Milanova/BG Ryder
Currying f(x1, x2,…,xn) = g x1 x2 … xn Function g is said to be the curried form of f. g1 x2 g2 x3 … Fall 18 CSCI 4430, A Milanova/BG Ryder
Semantics of Pure Lambda Calculus An expression has as its meaning the value that results after evaluation is carried out Somewhat informally, evaluation is the process of reducing expressions E.g., (x.y. x + y) 3 2 (y. 3 + y) 2 3 + 2 = 5 (Note: this example is just an informal illustration. There is no + in the pure lambda calculus!) x.y. x is assigned the meaning of TRUE x.y. y is assigned the meaning of FALSE
Free and Bound Variables Reducing expressions Consider expression ( x.y. x y ) (y w) Try 1: Reducing this expression results in the following ( y. x y ) [(y w)/x] = ( y. (y w) y ) The above notation means: we substitute argument (y w) for every occurrence of parameter x in body ( y. x y ). But what is wrong here? ( x.y. x y ) (y w): different y’s! If we substitute (y w) for x, the “free” y will become “bound”!
Free and Bound Variables Try 2: Rename “bound” y in y. x y to z: z. x z (x.y. x y) (y w) => (x.z. x z) (y w) E.g., in C, int id(int p) { return p; } is exactly the same as int id(int q) { return q; } Applying the reduction rule results in ( z. x z ) [(y w)/x] => ( z. (y w) z ) Fall 18 CSCI 4430, A Milanova/BG Ryder
Free and Bound Variables Abstraction ( x. E ) is also referred as binding Variable x is said to be bound in x. E The set of free variables of E is the set of variables that are unbound in E Defined by cases on E Var x: App E1 E2: Abs x.E: free(x) = {x} free(E1 E2) = free(E1) U free(E2) free(x.E) = free(E) - {x} Fall 18 CSCI 4430, A Milanova
Free and Bound Variables A variable x is bound if it is in the scope of a lambda abstraction: as in x. E Variable is free otherwise 1. (x. x) y 2. (z. z z) (x. x) 3. x.y.z. x z (y (u. u)) y is free. No free variables Fall 18 CSCI 4430, A Milanova
Free and Bound Variables We must take free and bound variables into account when reducing expressions E.g., (x.y. x y) (y w) First, rename bound y in y. x y to z: z. x z (more precisely, we have to rename to a variable that is NOT free in (y w)) (x.y. x y) (y w) (x.z. x z) (y w) Second, apply the reduction rule that substitutes (y w) for x in the body ( z. x z ) ( z. x z ) [(y w)/x] ( z. (y w) z ) = z. y w z
Substitution, formally (x.E) M E[M/x] replaces all free occurrences of x in E by M E[M/x] is defined by cases on E: Var: y[M/x] = y[M/x] = App: (E1 E2)[M/x] = Abs: (y.E1)[M/x] = (y.E1)[M/x] = M if x = y y otherwise (E1[M/x] E2[M/x]) y.E1 if x = y z.((E1[z/y])[M/x]) otherwise, where z NOT in free(E1) U free(M) U {x} Fall 18 CSCI 4430, A Milanova
Substitution, formally (x.y. x y) (y w) (y. x y)[(y w)/x] 1_. ( ((x y)[1_/y])[(y w)/x] ) 1_. ( (x 1_)[(y w)/x] ) 1_. ( (y w) 1_ ) 1_. y w 1_ You will have to implement this substitution algorithm in Haskell Fall 18 CSCI 4430, A Milanova/BG Ryder
Rules (Axioms) of Lambda Calculus rule (-conversion): renaming of parameter (choice of parameter name does not matter) x. E z. E[z/x] provided that z is not free in E e.g., x. x x is the same as z. z z rule (-reduction): function application (substitutes argument for parameter) (x.E) M E[M/x] Note: E[M/x] as defined on previous slide! e.g., (x. x) z z Fall 18 CSCI 4430, A Milanova
Rules of Lambda Calculus: Exercises Use -conversion and/or β-reduction: (x. x) y ? (x. x) (y. y) ? (x.y.z. x z (y z)) (u. u) (v. v) Notation: denotes that expression on the left reduces to the expression on the right, through a sequence -conversions and β-reductions. Fall 18 CSCI 4430, A Milanova
Reductions An expression ( x.E ) M is called a redex (for reducible expression) An expression is in normal form if it cannot be β-reduced The normal form is the meaning of the term, the “answer” Fall 18 CSCI 4430, A Milanova
Questions Is z. z z in normal form? Answer: yes, it cannot be beta-reduced Is (z. z z) (x. x) in normal form? Answer: no, it can be beta-reduced Fall 18 CSCI 4430, A Milanova/BG Ryder
Definitions of Normal Form Normal form (NF): a term without redexes Head normal form (HNF) x is in HNF (x. E) is in HNF if E is in HNF (x E1 E2 … En) is in HNF Weak head normal form (WHNF) x is in WHNF (x. E) is in WHNF (x E1 E2 … En) is in WHNF Fall 18 CSCI 4430, A Milanova (from MIT’s 2015 Program Analysis OCW)
Questions z. z z is in NF, HNF, or WHNF? (z. z z) (x. x) is in? x.y.z. x z (y (u. u)) is in? (x.y. x) z ((x. z x) (x. z x)) is in? z ((x. z x) (x. z x)) is in? z.(x.y. x) z ((x. z x) (x. z x)) is in? (We will be reducing to NF, mostly)
More Reduction Exercises C = x.y.f. f x y H = f. f (x.y. x) T = f. f (x.y. y) What is H (C a b)? (f. f (x.y. x)) (C a b) (C a b) (x.y. x) ((x.y.f. f x y) a b) (x.y. x) (f. f a b) (x.y. x) (x.y. x) a b a Fall 18 CSCI 4430, A Milanova (from MIT 2015 Program Analysis OCW)
Exercise S = x.y.z. x z (y z) I = x. x What is S I I I? An expression with no free variables is called combinator. S, I, C, H, T are combinators. S = x.y.z. x z (y z) I = x. x What is S I I I? ( x.y.z. x z (y z) ) I I I (y.z. I z (y z) ) I I (z. I z (I z) ) I I I (I I) = (x. x) I (I I) I (I I) = (x. x) (I I) I I = (x. x) I I Reducible expression is underlined at each step. Fall 18 CSCI 4430, A Milanova
Today’s Lecture Outline Lambda calculus Introduction Syntax and semantics Free and bound variables Substitution, formally Rules of the lambda calculus Normal forms Reduction strategies Fall 18 CSCI 4430, A Milanova/BG Ryder