Com S 342 meeting -*- Outline -*- * beta conversion (4.2) (omit) Note for next time: Be sure to discuss the notion of a beta redex! transformation rule about procedures setting: the lambda calculus, to avoid distraction (so this is theory) recall that this is a core of most programming languages Q: What features of other languages are captured by lambda calculus? ** beta rule ------------------------------------------ THE BETA RULE (4.2) Syntax of lambda calculus with constants ::= | | (lambda () ) | ( ) Beta rule: Examples: ((lambda (x) 1) 2) = ((lambda (z) z) 2) = ((lambda (z) y) 2) = ((lambda (x) (x (x 2))) f) = ((lambda (f) (lambda (g) (lambda (x) (f (g x))))) car) = ------------------------------------------ ... 1 ... 2 ... y ... ((lambda (var) exp) rand) = exp[rand/var] where the substitution notation is what we defined before (or see the book) ... (f (f 2)) ... (lambda (g) (lambda (x) (car (g x)))) define a redex (reducible expression) and emphasize what a beta redex looks like ------------------------------------------ FOR YOU TO DO (IN PAIRS) Use beta reduction to simplify, if possible, the following. Show all steps. a. ((lambda (x) x) (lambda (x) x)) b. (((lambda (x) (lambda (y) (x y))) add1) 4) c. ((lambda (x) (x x)) (lambda (x) (x x))) ------------------------------------------ Have to watch out for capture, etc., see the book for details. discuss *normal forms* ** conversion and reduction ------------------------------------------ CONVERSION AND REDUCTION conversion ((lambda (x) E) M) = E[M/x] e.g., ((lambda (x) (f x)) 3) = (f 3) reduction ((lambda (x) E) M) => E[M/x] e.g., ((lambda (x) ((lambda (y) y) x)) f) => ((lambda (y) y) f) => anti-reduction E[M/x] <= ((lambda (x) E) M) e.g., ((ccons (car (cdr ls1))) (car (cdr ls2))) <= ((lambda (cadr) ((ccons (cadr ls1)) (cadr ls2))) ------------------------------------------ ... f ... (lambda (ls) (car (cdr ls)))) Q: What would that last expression be like using a let? The idea of anti-reduction is important in finding subroutines How? - Identify the unchanging parts - make the changing parts parameters ------------------------------------------ UTILITY OF BETA REDUCTION Transforming procedural rep to record rep: (define seq-repeat ; TYPE: (-> (number) sequence) (lambda (num) (lambda (n) num))) (define seq-generator ; TYPE: (-> ((-> (number) number)) ; sequence) (lambda (f) (lambda (n) (f n)))) (define seq-nth ; TYPE: (-> (sequence) number) (lambda (seq n) (seq n))) becomes: (define-record repeated-seq (num)) (define-record generated-seq (f)) (define seq-nth ; TYPE: (-> (sequence) number) (lambda (seq n) (variant-case seq (seq-repeat (num) ((lambda (n) num) n)) (seq-generator (f) ((lambda (n) (f n)) n))))) FOR YOU TO DO Beta-reduce each case of the variant-case ------------------------------------------ Q: Does that give the original code? yes, so this is how it's constructed ** eta conversion (p. 107) ------------------------------------------ ETA CONVERSION IDEA ((lambda (n) (f n)) y) = So: (define seq-generator ; TYPE: (-> ((-> (number) number)) ; sequence) (lambda (f) (lambda (n) (f n)))) = Similarly: ((lambda (f) (lambda (n) (f n))) g) = ------------------------------------------ ... (f n) Q: Does beta reduction mean both sides are equal? yes Q: When are two functions equal? when they have the same output for all arguments Q: So are f and (lambda (n) (f n)) equal? yes That's the idea! ... (lambda (f) f) ... OR ... (lambda (n) (g n)) ((lambda (f) f) g) = = g g Note that they both come out to the same thing, important, so that B's interpreter doesn't give one answer, and E's another. ------------------------------------------ FOR YOU TO DO Reduce the following: (lambda (sym val ff) (make-extended-ff sym val ff)) Simplify the following code: (define lambda->formal ; TYPE: (-> (exp) exp) (lambda (exp) (caadr exp))) ------------------------------------------ Q: Is it possible to make this simplification in C++? ------------------------------------------ ETA CONVERSION RULE conversion: if x is not free in E, then (lambda (x) (E x)) = E reduction: if x is not free in E, then (lambda (x) (E x)) => E TERMS redex, beta-redex, eta-redex ((lambda (x) (f x)) 3) ------------------------------------------ Q: What if x is free in E? (lambda (z) (z z)) is not = z (lambda (y) ((eq (f y)) y)) is not = (eq (f y)) Q: What would eta-anti-reduction (aka eta-expansion) look like? what might it be good for? turning a value into a procedure (delaying eval) (oh-no) <= (lambda (x) ((oh-no) x))