Download
david evans http www cs virginia edu evans n.
Skip this Video
Loading SlideShow in 5 Seconds..
David Evans cs.virginia/evans PowerPoint Presentation
Download Presentation
David Evans cs.virginia/evans

David Evans cs.virginia/evans

88 Views Download Presentation
Download Presentation

David Evans cs.virginia/evans

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Class 33: Making Recursion • M.C. Escher, Ascending and Descending • also known as • f. (( x.f (xx)) ( x. f (xx)) David Evans http://www.cs.virginia.edu/evans CS200: Computer Science University of Virginia Computer Science

  2. Menu • Recap: Proving Lambda Calculus is Universal • How to make recursive definitions without define • Fixed points CS 200 Spring 2003

  3. Lambda Calculus Review term ::= variable |term term | (term)|  variable .term ( f. (( x.f (xx))( x. f (xx)))) (z.z) Parens are just for grouping, not like in Scheme -reduction (renaming) y. M v. (M [yv]) where v does not occur in M. -reduction (substitution) (x. M)N   M [ xN ] CS 200 Spring 2003

  4. Universal Language • Is Lambda Calculus a universal language? • Can we compute any computable algorithm using Lambda Calculus? • To prove it isn’t: • Find some Turing Machine that cannot be simulated with Lambda Calculus • To prove it is: • Show you can simulate every Turing Machine using Lambda Calculus CS 200 Spring 2003

  5. Simulating Computation z z z z z z z z z z z z z z z z z z z z • Lambda expression corresponds to a computation: input on the tape is transformed into a lambda expression • Normal form is that value of that computation: output is the normal form • How do we simulate the FSM? ), X, L ), #, R (, #, L 2: look for ( 1 Start (, X, R HALT #, 0, - #, 1, - Finite State Machine CS 200 Spring 2003

  6. Lambda Calculus is a Universal Computer z z z z z z z z z z z z z z z z z z z z ), X, L ), #, R (, #, L 2: look for ( 1 • Read/Write Infinite Tape • Mutable Lists • Finite State Machine • Numbers to keep track of state • Processing • Way of making decisions (if) • Way to keep going Start (, X, R HALT #, 0, - #, 1, - Finite State Machine We have this, but we cheated using  to make recursive definitions! CS 200 Spring 2003

  7. 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 200 Spring 2003

  8. Recursive Definitions Which of these make you uncomfortable? x = 1 + x x = 4 – x x = 9 / x x = x x = 1 / (16x3) No solutions over integers 1 integer solution, x = 2 2 integer solutions, x = 3, x = -3 Infinitely many integer solutions No integer solutions, two rational solutions (1/2 and –1/2), four complex solutions (1/2, -1/2, i/2, -i/2) CS 200 Spring 2003

  9. Equations  Functions x = 4 – x f  x. sub 4 x Find anxsuch that (fx) = x. Same as solve for x. sub  x . y. if (zero? y) x (sub (pred x)(pred y)) CS 200 Spring 2003

  10. What’s a function? f  x. sub 4 x f: Nat  Nat { <0, 4>, <1, 3>, <2, 2>, <3, 1>, <4, 0>, <5, ???>, ... } CS 200 Spring 2003

  11. Domains • Set: unordered collection of values Nat= { 0, 2, 1, 4, 3, 6, 5, 8, 7, ... } • Domains: like a set, but has structure Nat= { 0, 1, 2, 3, 4, 5, 6, 7, 8, ... } where 0 is the least element, and there is an order of the elements. CS 200 Spring 2003

  12. Making Domains • Primitive domains can be combined to make new domains • Product domain: D1x D2 • Pairs of values Nat x Nat = { (tupleNat,Nat 0 0), (tupleNat,Nat 0 1), (tupleNat,Nat 1 0), (tupleNat,Nat 1 1), (tupleNat,Nat 0 2), ... } CS 200 Spring 2003

  13. Functions • A set of input-output pairs • The inputs (Di) and outputs (Do) are elements of a domain • The function is an element of the domain Di Do. • It is a (completely defined) function if and only if for every element d Di, the set of input-output pairs has one member whose first member matches d. CS 200 Spring 2003

  14. Functions? f:Nat  Nat  z. subNat  Nat4 z f: Nat  Int  z. subNat  Int4 z f: Nat  Nat  z. addNat  Nat z 1 Not a function since there is no value in output domain forz > 4. CS 200 Spring 2003

  15. Functions f: (Nat  Nat) n. add1 ( fn)) f: (Nat  Nat) n. f (add 1 n)) No solutions (over natural numbers) – would require x = 1 + x. Infinitely many solutions – e.g., f(x)= x. 3 { <0, 3>, <1, 3>, <2, 3>, ... } CS 200 Spring 2003

  16. Fixed Points • A fixed point of a function f: (D  D)is an element d D such that (f d) = d. • Examples: f: Nat  Int  z. sub4 z f: Nat  Nat  z. mul2 z f: Nat  Nat  z.z 2 0 infinitely many CS 200 Spring 2003

  17. Generating Functions • Any recursive definition can be encoded with a (non-recursive) generating function f: (Nat  Nat) n. (add 1 ( fn))  g: (Nat  Nat) (Nat  Nat)  f. n. (add 1 ( fn)) • Solution to a recursive definition is a fixed point of its associated generating function CS 200 Spring 2003

  18. Example fact: (Nat  Nat) n. if (=n0) 1 (muln (fact (subn 1))) gfact:(Nat  Nat)  (Nat  Nat) f.n. if (= n0) 1 (mul (n (f (subn 1))) CS 200 Spring 2003

  19. gfact:(Nat  Nat)  (Nat  Nat)  f.n. if (= n0) 1 (mul (n (f (subn 1))) gfact I gfact (z.z)  n. if (= n0) 1 (mul (n ((z.z) (subn 1))) What is (gfactI) 5? CS 200 Spring 2003

  20. gfact:(Nat  Nat)  (Nat  Nat)  f.n. if (= n0) 1 (mul (n (f (subn 1))) fact :(Nat  Nat)  n. if (= n0) 1 (mul (n (fact (subn 1))) gfact fact gfact fact  n. if (= n 0) 1 (muln (fact (subn 1)))  fact fact is a fixed point ofgfact CS 200 Spring 2003

  21. Unsettling Questions • Is the factorial function the only fixed point of gfact? • Given an arbitrary function, how does one find a fixed point? • If there is more than one fixed point, how do you know which is the right one? CS 200 Spring 2003

  22. Iterative Fixed Point Technique To find a fixed point of g: D D • Start with some element d D • Calculate g(d), g(g(d)), g(g(g(d))), ... until you get g(g(v))=v then v is a fixed point. CS 200 Spring 2003

  23. Where to start? • If you start with  D you get the least fixed point (which is the “best” one) •  (pronounced “bottom”) is the element of Dsuch that for any element dD,  d. • means “has less information than” or “is weaker than” • Not all domains have a . CS 200 Spring 2003

  24. Pointed Partial Order • A partial order (D, ) is pointed if it has a bottom element u D such that ud for all elements d D. • Bottom of (Nat, <=)? • Bottom of (Nat, =)? • Bottom of (Int, <=)? • Bottom of ({Nat}, <=)? 0 Not a pointed partial order Not a pointed partial order {} CS 200 Spring 2003

  25. Getting to the  of things • Think of bottom as the element with the least information, or the “worst” possible approximation. • Bottom of Nat  Nat is a function that is undefined for all inputs. That is, the function with the graph {}. • To find the least fixed point in a function domain, start with the function whose graph is {} and iterate. CS 200 Spring 2003

  26. Least Fixed Point of gfact gfact:(Nat  Nat)  (Nat  Nat)   f. n. if (= n0) 1 muln ( f (subn 1))) gfactn (function with graph {}) = fact as n approaches infinity. CS 200 Spring 2003

  27. Fixed Point Theorem Do all -calculus terms have a fixed point? CS 200 Spring 2003

  28. Fixed Point Theorem •  F ,  X such thatFX = X • Proof: LetW =  x.F(xx) andX = WW. X = WW = ( x.F(xx))W   F (WW) = FX CS 200 Spring 2003

  29. Why of Y? • Y is  f. WW: Y   f. ( x.f (xx))( x. f (xx)) • Y calculates a fixed point of any lambda term! • Hence: we don’t need define to do recursion! • Works in Scheme too - check the “lecture” from the Adventure Game CS 200 Spring 2003

  30. Fixed Point The fixed point of our Turing Machine simulator on some input is the result of running the TM on that input. If there is no fixed point, the TM doesn’t halt! fixed-point TM input   result of running TM on input CS 200 Spring 2003

  31. Lambda Calculus is a Universal Computer! z z z z z z z z z z z z z z z z z z z z ), X, L ), #, R • Read/Write Infinite Tape • Mutable Lists • Finite State Machine • Numbers to keep track of state • Processing • Way of making decisions (if) • Way to keep going (, #, L 2: look for ( 1 Start (, X, R HALT #, 0, - #, 1, - Finite State Machine CS 200 Spring 2003

  32. 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))) CS 200 Spring 2003

  33. QED • Lambda calculus is a Universal Programming language • All you need is beta-reduction and you can compute anything • Integers, booleans, if, while, +, *, =, <, classes, define, inheritance, etc. are for wimps! Real programmers only use  and beta-reduction. CS 200 Spring 2003

  34. Charge • Friday: chance to ask questions before Exam 2 is handed out • Email questions before if you want to make sure they are covered • Today’s lecture is not covered by Exam 2 • Office hours this week: • Wednesday, after class - 3:45 • Thursday 10am-10:45am; 4-5pm. CS 200 Spring 2003