CS 541 Lecture -*- Outline -*- * Substitution in the lambda calculus ** occurrence (i) x occurs in x (ii) if x occurs in U or V, then x occurs in (U V) (iii) if x occurs in U, then for all y, x occurs in (\y.U) ** free and bound vars an occurrence of x in Y is bound iff it is inside a subexpression of Y of the form \x.Z; otherwise x is free in Y. note: x can be free and bound in the same term: (x (\x.x)) ** substitution notation [N/x]M, or M[N := x], older: M[N/x] (i) [N/x]x = N (ii) [N/x]y = y if y/=x (iii) [N/x](M1 M2) = (([N/x]M1)([N/x]M2)) (iv) [N/x](\x.Y) = \x.Y (v) [N/x](\y.M1) = if y/=x, (y does not occur in N or x does not occur in M1) then \y.([N/x]M1) elseif y/=x, z not free in M1, z not free in N, ("z is fresh") y occurs in N, x occurs in M1 then \z.[N/x]([z/y]M1) endif rule (v) avoids "capture", thus keeping static scope e.g., [w/x](\y.x) = \z.w however [w/x](\w.x) should be something like (\z.x), where z is fresh we would capture w if we used (\w.w) as the result, as this has a different form than (\w.x) ** Quiz on substitution 1. Consider the following Scheme program. (define (m x y) (lambda (f) (f x y))) (define (a z) (z (lambda (p q) p))) (define (r z) (z (lambda (p q) q))) What is the the result of the following expression? (let ((x 5) (y 6)) (a (r (m 3 (m 1 2))))) Answer: 1 2. Give fully parenthesized forms for the following lambda-terms. (Note that the backslash (\) is meant to represent lambda: (a) \x.x y z (b) x y \x. x y (c) x y (z w) Answers: (a) (\x.((x y) z)) (b) ((x y) (\x. (x y))) (c) ((x y) (z w)) 3. What are the free variables of the following terms? (a) \x.(x y) (b) (x (\x.x y)) \z.z Answers: (a) y (b) x and y 4. What is the result of the following substitutions? (a) [z/x](\x.x y) (b) [z/y](\x.x y) (c) [w/x]((\y.x) w) Answers: (a) (\x.x y) (b) (\x.x z) (c) ((\y.w) w)