320 likes | 443 Views
Lecture 6: Lambda Calculus. God created the integers – all else is the result of man. Leopold Kronecker God created application – all else is the result of man. Alonzo Church (not really). CS655: Programming Languages University of Virginia Computer Science. David Evans
E N D
Lecture 6: Lambda Calculus God created the integers – all else is the result of man. Leopold Kronecker God created application – all else is the result of man. Alonzo Church (not really) CS655: Programming Languages University of Virginia Computer Science David Evans http://www.cs.virginia.edu/~evans
English (OED: 500,000 words) C++ (680 pages) Scheme (50 pages) ??? (.5 page) Language Complexity and Brain Cell Decay Language Complexity (Number of Morphemes) 0 5 10 15 20 25 Years Spent in School CS 655: Lecture 6
Complexity in Scheme If we have lazy evaluation, (and don’t care about abstraction) we don’t need these. • Special Forms • if, cond, define, etc. • Primitives • Numbers (infinitely many) • Booleans: #t, #f • Functions (+, -, and, or, etc.) • Evaluation Complexity • Environments (more than ½ of our interpreter) Hard to get rid of? Can we get rid of all this and still have a useful language? CS 655: Lecture 6
-calculus Alonzo Church, 1940 (LISP was developed from -calculus, not the other way round.) term = variable | term term | (term) | variable .term CS 655: Lecture 6
Mystery Function (Teaser) p xy. pca.pca (x.x xy.x) x) y (p ((x.x xy.y) x) (x. z.z (xy.y) y) m xy. pca.pca (x.x xy.x) x) x.x (py (m ((x.x xy.y) x) y)) f x. pca.pca ((x.x xy.x) x) (z.z (xy.y) (x.x)) (mx (f ((x.x xy.y) x))) if x = 0 1 x * f (x – 1) CS 655: Lecture 6
Evaluation Rules -reduction (renaming) y. M v. (M [yv]) where v does not occur in M. -reduction (substitution) (x. M)N M [ xN ] CS 655: Lecture 6
Identity () x y if x and y are same name M1 M2 N1 N2 if M1 N1 M2 N2 (M) (N)if M N x. M y. Nif x y M N CS 655: Lecture 6
Defining Substitution ( ) x [xN] N y [xN] y where x y M1 M2[xN] M1 [xN]M2[xN] (x. M) [xN] x. M CS 655: Lecture 6
Defining Substitution ( ) (y. M) [xN] y. (M [xN]) where x y and y does not appear free in N or x does not appear free in M. (y. M) [xN] z. (M [yz]) [xN] where x y, z x and z y and z does not appear in M or N, x does occur free in M and y does occur free in N. CS 655: Lecture 6
Reduction (Uninteresting Rules) y. M v. (M [yv]) where v does not occur in M. M M M N PM PN M N MP NP M N x. M x. N M N and N P M P CS 655: Lecture 6
-Reduction (the source of all computation) (x. M)N M [ xN ] CS 655: Lecture 6
Recall Apply in Scheme “To apply a procedure to a list of arguments, evaluate the procedure in a new environment that binds the formal parameters of the procedure to the arguments it is applied to.” • We’ve replaced environments with substitution. • We’ve replaced eval with reduction. CS 655: Lecture 6
Some Simple Functions I x.x Cxy.yx Abbreviation for x.(y. yx) CII = (x.(y. yx)) (x.x) (x.x) (y. y (x.x))(x.x) x.x (x.x) x.x = I CS 655: Lecture 6
Evaluating Lambda Expressions • redex: Term of the form (x. M)N Something that can be -reduced • An expression is in normal form if it contains no redexes (redices). • To evaluate a lambda expression, keep doing reductions until you get to normal form. CS 655: Lecture 6
Example f. (( x.f (xx))( x. f (xx))) CS 655: Lecture 6
Alyssa P. Hacker’s Answer ( f. (( x.f (xx))( x. f (xx)))) (z.z) (x.(z.z)(xx))( x. (z.z)(xx)) (z.z) ( x.(z.z)(xx)) ( x.(z.z)(xx)) (x.(z.z)(xx)) ( x.(z.z)(xx)) (z.z) ( x.(z.z)(xx)) ( x.(z.z)(xx)) (x.(z.z)(xx)) ( x.(z.z)(xx)) ... CS 655: Lecture 6
Ben Bitdiddle’s Answer ( f. (( x.f (xx))( x. f (xx)))) (z.z) (x.(z.z)(xx))( x. (z.z)(xx)) (x.xx)(x.(z.z)(xx)) (x.xx)(x.xx) (x.xx)(x.xx) ... CS 655: Lecture 6
Be Very Afraid! • Some -calculus terms can be -reduced forever! • The order in which you choose to do the reductions might change the result! CS 655: Lecture 6
Take on Faith (for now) • All ways of choosing reductions that reduce a lambda expression to normal form will produce the same normal form (but some might never produce a normal form). • If we always apply the outermost lambda first, we will find the normal form is there is one. • This is normal order reduction – corresponds to normal order (lazy) evaluation CS 655: Lecture 6
Who needs primitives? T xy. x F xy. y if pca . pca CS 655: Lecture 6
Evaluation T xy. x F xy. y if pca . pca if T M N ((pca . pca) (xy. x)) M N • (ca . (x.(y. x)) ca)) M N • (x.(y. x))M N • (y. M ))N M CS 655: Lecture 6
Who needs primitives? and xy. ifxyF or xy. ifxTy CS 655: Lecture 6
Coupling [M, N]z.z M N first p.p T second p.p F first [M, N] = p.p T (z.z M N) (z.z M N) T = (z.z M N)xy. x (xy. x) M N M CS 655: Lecture 6
Tupling n-tuple: [M] = M [M0,..., Mn-1, Mn] = [M0,[M1 ,..., [Mn-1, Mn ]... ] n-tuple direct: [M0,..., Mn-1, Mn] = z.z M0,..., Mn-1, Mn Pi,n = x.x Ui,n Ui,n = x0... xn. xi What isP1,2? CS 655: Lecture 6
Primitives What about all those pesky numbers? CS 655: Lecture 6
What are numbers? • We need three (?) functions: succ: n n + 1 pred: n n – 1 zero?: n (n = 0) • Is that enough to define add? CS 655: Lecture 6
Adding for Post-Docs add xy.if (zero? x) y (add (pred x)(succ y) CS 655: Lecture 6
Counting 0 I 1 [F, I] 2 [F, [F, I]] 3 [F, [F [F, I]] ... n + 1 [F, n] CS 655: Lecture 6
Arithmetic Zero? x.x T Zero?0= (x.x T) I = T Zero? 1= (x.x T) [F, I] = F succ x.[F, x] pred x.x F pred 1 = (x.x F) [F, I] = [F, I]F = I = 0 pred 0 = (x.x F) I = IF = F CS 655: Lecture 6
Factorial mult xy.if (zero?x) 0 (addy (mult (predx) y)) fact x. if (zero?x) 1 (multx (fact (predx))) Recursive definitions should make you uncomfortable. Need for definitions should also bother you. CS 655: Lecture 6
Summary • All you need is application and abstraction and you can compute anything • This is just one way of representing numbers, booleans, etc. – many others are possible • Integers, booleans, if, while, +, *, =, <, subtyping, multiple inheritance, etc. are for wimps! Real programmers only use . CS 655: Lecture 6
Charge • Problem Set 2 out today • Make a quantum scheme interpreter • Longer and harder than PS1 • New teams assigned • Read Prakash’s Notes • Be uncomfortable about recursive definitions CS 655: Lecture 6