Download
Download Presentation
David Evans cs.virginia/~evans

# David Evans cs.virginia/~evans

Télécharger la présentation

## David Evans cs.virginia/~evans

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

1. Lecture 15: Crazy Eddie and the Fixed Points David Evans http://www.cs.virginia.edu/~evans CS655: Programming Languages University of Virginia Computer Science

2. Menu • Billy Bob and the Beta Reducers • Survey Results • Fixed Points University of Virginia CS 655

3. -term Evaluation Y   f. (( x.f (xx))( x. f (xx))) What is (Y I)? Recall I = z.z • Evaluation rule: • -reduction (substitution) • (x. M)N   M [ x := N] • Substitute N for x in M. University of Virginia CS 655

4. 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))   ... University of Virginia CS 655

5. 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)   ... University of Virginia CS 655

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! University of Virginia CS 655

7. Survey Results • Only 2 really conclusive results: • Less Readings (6 way too much, 15 too much, 4 just right) • More Pizza (1 for, none against) University of Virginia CS 655

8. Survey Results • Lectures • majority too fast, but not overwhelming • Programming • 12 just right, 9 not enough, 1 too much • Topics • 6 fewer topics/more depth, 10 more topics/less depth, 2 fewer topics/less depth, 2 more topics/more depth (5 write in like current mix) • Grading • 13 prefer unbounded, 8 prefer traditional, 4 don’t care University of Virginia CS 655

9. Elevator Speeches University of Virginia CS 655

10. 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) University of Virginia CS 655

11. More interesting for functions Functions are just sets of input/output pairs. f: (Nat  Nat)= n. (1 + ( fn)) f: (Nat  Nat) = n. ( f (1 + n)) No solutions (over natural numbers) – would require x = 1 + x. Infinitely many solutions – e.g., f(x) = 3. University of Virginia CS 655

12. Generating Functions • Any recursive definition can be encoded with a (non-recursive) generating function f: (Nat  Nat)= n. (1 + ( fn))  g: (Nat  Nat) (Nat  Nat)=  f. n. (1 + ( fn)) University of Virginia CS 655

13. Fixed Points • A fixed point of a function f: (D  D)is an element d D such that (f d) = d. • Solution to a recursive definition is a fixed point of its associated generating function. University of Virginia CS 655

14. Example fact: (Nat  Nat)=  n. if (n = 0) then 1 else (n * ( fact (n - 1))) gfact:(Nat  Nat)  (Nat  Nat) =  f. n. if (n = 0) then 1 else (n * ( f (n - 1))) University of Virginia CS 655

15. (gfact I) (gfact (z.z)) = n. if (n = 0) then 1 else (n * ((z.z) (n - 1))) Function that returns 1 for 0, 0 for 1, 2 for 2, ..., n * n-1 for n ~= 0. University of Virginia CS 655

16. (gfact factorial) (gfact factorial) = n. if (n = 0) then 1 else (n * (factorial (n - 1))) = factorial factorial is a fixed point ofgfact University of Virginia CS 655

17. Unsettling Questions • Is a 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? University of Virginia CS 655

18. An odd example odds = { 1 }  { x + 2 | x  odds } godds = ({Nat}{Nat}) =  s. { 1 }  { x + 2 | x  s} godds ({}) = {1} godds(godds ({}) ) = godds{1} = {1 , 3} goddsn({}) = {1, 3, ..., (n * 2) + 1} OOPS - this is broken University of Virginia CS 655

19. Oddities of Odds • What is a fixed point of godds? All odd numbers: { 1, 3, 5, ... } What about: { 0, 1, 2, 3, 4, ... } ? What about: { 1, 3, 4, 5, 6, 7, ... } ? • godds has infinitely many fixed points, the set of all odd numbers is the least fixed point. OOPS - this is broken University of Virginia CS 655

20. Iterative Fixed Point Technique • 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. • 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 . University of Virginia CS 655

21. a a • a b and b a imply a = b • a b and b c imply a c Partial Order A partial order is a pair (D, ) of a domain D and a relation that is for all a, b, c D reflexive transitive and anti-symmetric University of Virginia CS 655

22. Partial Orders? • (Nat, <=) • (Nat, =) • ({ burger, fries, coke }, yummier) where burger yummier fries, burger yummier coke, fries yummier coke • ({ burger, fries, beer }, yummier) where burger yummier fries, fries yummier beer, beer yummier burger University of Virginia CS 655

23. 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 {} University of Virginia CS 655

24. double:Nat  Nat= n. if (n = 0) then 0 else (2 + (double (n –1)) Is double fact under (Nat, <=)? Partial Order of Functions • ((D, D) (E, E), DE) is a partial order using: fDEgif for alldD, (fd) E (g d). No, (double 1) is not <= (fact 1) University of Virginia CS 655

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. University of Virginia CS 655

26. Least Fixed Point of gfact gfact:(Nat  Nat)  (Nat  Nat) =  f. n. if (n = 0) then 1 else (n * ( f (n - 1))) (gfactn (function with graph {})) = fact as n approaches infinity. University of Virginia CS 655

27. Fixed Point Theorem • Do all -calculus terms have a fixed point? •  F ,  X such thatFX = X • Proof: LetW =  x.F(xx) andX = WW. X = WW = ( x.F(xx))W   F (WW) = FX University of Virginia CS 655

28. Why of Y? • Y is  f. WW: Y   f. (( x.f (xx))( x. f (xx))) • Y calculates a fixed point (but not necessarily the least fixed point) of any lambda term! • If you’re not convinced, try calculating ((Y fact) n). University of Virginia CS 655

29. Still Uncomfortable? University of Virginia CS 655

30. Remember the problem reducing Y • Different reduction orders produce different results • Reduction may never terminate • Normal Form means no more ß-reductions can be done • There are no subterms of the form (x. M)N • Not all -terms can be written in normal form University of Virginia CS 655

31. Church-Rosser Theorem • If a -term has a normal form, any reduction sequence that produces a normal form always the same normal form • Computation is deterministic • Some orders of evaluation might not terminate though • Evaluating left-to-right finds the normal form if there is one. • Proof by lack of space/time. University of Virginia CS 655

32. Charge • Next time: pragmatic issues of functional programming • Rewrite rules • Type inference • Depositions due today, will be sent to opposing attorneys (including my comments if any) by Thursday • PS3 is harder than PS2 University of Virginia CS 655