Transcript slides
Class 35: 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) CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/~evans Menu • Why is Scheme too complicated? • What is a Calculus? • Lambda Calculus 17 April 2002 CS 200 Spring 2002 2 Complexity in Scheme • Special Forms – if, cond, define, etc. • Primitives If we have lazy evaluation, (and don’t care about abstraction) we don’t need these. – Numbers (infinitely many) – Booleans: #t, #f Hard to get rid of? – Functions (+, -, and, or, etc.) • Evaluation Complexity – Environments (more than ½ of our interpreter) Can we get rid of all this and still have a useful language? 17 April 2002 CS 200 Spring 2002 3 -calculus Alonzo Church, 1940 (LISP was developed from -calculus, not the other way round.) term = variable | term term | (term) | variable . term 17 April 2002 CS 200 Spring 2002 4 What is Calculus? • In High School: d/dx xn = nxn-1 [Power Rule] d/dx (f + g) = d/dx f + d/dx g [Sum Rule] Calculus is a branch of mathematics that deals with limits and the differentiation and integration of functions of one or more variables... 17 April 2002 CS 200 Spring 2002 5 Real Definition • A calculus is just a bunch of rules for manipulating symbols. • People can give meaning to those symbols, but that’s not part of the calculus. • Differential calculus is a bunch of rules for manipulating symbols. There is an interpretation of those symbols corresponds with physics, slopes, etc. 17 April 2002 CS 200 Spring 2002 6 Lambda Calculus • Rules for manipulating strings of symbols in the language: term = variable | term term | (term) | variable . term • Humans can give meaning to those symbols in a way that corresponds to computations. 17 April 2002 CS 200 Spring 2002 7 Why? • Once we have precise and formal rules for manipulating symbols, we can use it to reason with. • Since we can interpret the symbols as representing computations, we can use it to reason about programs. 17 April 2002 CS 200 Spring 2002 8 Evaluation Rules -reduction (renaming) y. M v. (M [y v]) where v does not occur in M. -reduction (substitution) (x. M)N M [ x N ] 17 April 2002 CS 200 Spring 2002 9 Identity () xy if x and y are same name M1 M2 N1 N2 if M1 N1 M2 N2 (M) (N) if M N x. M y. N if x y M N 17 April 2002 CS 200 Spring 2002 10 Defining Substitution (|) 1. 2. 3. y [x | N] = N where x y y [x | N] = y where x y M1 M2 [x | N] = M1 [x | N] M2 [x | N] 4. 5a. (M) [x | N] = (M [x | N]) (x. M) [x | N] = x. M 17 April 2002 CS 200 Spring 2002 11 Defining Substitution (|) 5b. (y. M) [x N] = y. (M [x N]) | | where x y and y does not appear free in N and x appears free in M. 5c. (y. M) [x N] = y. M | where x y and y does or does not appear free in N and x does not appear free in M. 17 April 2002 CS 200 Spring 2002 12 Defining Substitution (|) 5c. (y. M) [x N] = y. (M [x N]) | | where x y and y does not appear free in N or x does not appear free in M. 5d. (y. M) [x | N] = z. (M [y | z]) [x | N] 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. 17 April 2002 CS 200 Spring 2002 13 Reduction (Uninteresting Rules) y. M v. (M [y v]) where v does not occur in M. MM M N PM PN M N MP NP M N x. M x. N M N and N P M P 17 April 2002 CS 200 Spring 2002 14 -Reduction (the source of all computation) (x. M)N M [ x N ] 17 April 2002 CS 200 Spring 2002 15 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. 17 April 2002 CS 200 Spring 2002 16 Some Simple Functions I x.x C xy.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 17 April 2002 CS 200 Spring 2002 17 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. 17 April 2002 CS 200 Spring 2002 18 Example f. (( x.f (xx)) ( x. f (xx))) 17 April 2002 CS 200 Spring 2002 19 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)) ... 17 April 2002 CS 200 Spring 2002 20 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) ... 17 April 2002 CS 200 Spring 2002 21 Be Very Afraid! • Some -calculus terms can be -reduced forever! • The order in which you choose to do the reductions might change the result! 17 April 2002 CS 200 Spring 2002 22 Take on Faith (until CS655) • 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 17 April 2002 CS 200 Spring 2002 23 Church-Turing Thesis • Any mechanical computation can be performed by Lambda Calculus reduction rules • There is a Lambda Calculus term equivalent to every Turing Machine 17 April 2002 CS 200 Spring 2002 24 Proof • Show you can implement a Turing Machine using Lambda Calculus • Next time: we won’t make a whole Turing machine, but will show we can make everything we are likely to need 17 April 2002 CS 200 Spring 2002 25 Charge • Now: Selvin’s talk • Projects – make progress for Monday! 17 April 2002 CS 200 Spring 2002 26