 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 9: Crazy Eddie and the Fixed Points • M.C. Escher, Ascending and Descending (also known as • f. (( x.f (xx)) ( x. f (xx))) David Evans http://www.cs.virginia.edu/~evans CS655: Programming Languages University of Virginia Computer Science

2. -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

3. 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

4. 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

5. 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

6. Equations  Functions x = 4 – x f  z. 4 – z Find anxsuch that (f x) = f. Same as solve for x. University of Virginia CS 655

7. What’s a function? f  z. 4 – z f: Nat  Nat { <0, 4>, <1, 3>, <2, 2>, <3, 1>, <4, 0>, <5, ???>, ... } University of Virginia CS 655

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

9. 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), ... } University of Virginia CS 655

10. Product Domains Nat x Nat = { <0, 0>, <0, 1>, <1, 0>, <1, 1>, <0, 2>, ... } Watch out: remember the order matters! (Later today: what is the “right” order for the elements of Nat x Nat? The order shown is “wrong”.) University of Virginia CS 655

11. Projections • Product domains come with projection functions: ProjiD1,D2 ,...,Dn: D1x D2x ...x Dn  Di Proj1Nat,Nat = Nat x Nat Nat University of Virginia CS 655

12. Function Domains • Function domain: D1 D2 • An infix mapping from D1 to D2 z. z +Int1: Int Int z. z >Int0: Int Bool University of Virginia CS 655

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

14. Functions? f:Nat  Nat  z. 4 – z f: Nat  Int  z. 4 – z f: Nat  Nat  z. z + 1 f: ?? z. if(z = 0) then 0 else (2 + f (z – 1)) University of Virginia CS 655

15. Functions 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. { <0, 3>, <1, 3>, <2, 3>, ... } University of Virginia CS 655

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. 4 – z f: Nat  Nat  z. 2 * z f: Nat  Nat  z.z 2 0 infinitely many University of Virginia CS 655

17. Fixed Points Sequence type – like a list map: Nat* x (Nat  Nat) Nat*  lf. if (null? l) l (cons (f (car l)) (map (cdr l) f))) Some fixed points: <Null, any function>, <any list, x.x>, ... University of Virginia CS 655

18. Fixed Points Demo Ryan Persaud’s swarm simulation University of Virginia CS 655

19. 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)) • Solution to a recursive definition is a fixed point of its associated generating function. University of Virginia CS 655

20. 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

21. (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

22. (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

23. 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

24. 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

25. 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

26. 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

27. 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

28. 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

29. 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

30. 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

31. Fixed Point Theorem • Do all -calculus terms have a fixed point? • (Smullyan: Is there a Sage bird?) University of Virginia CS 655

32. Finding the Sage Bird •  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

33. 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). (PS1, 1e) University of Virginia CS 655

34. Still Uncomfortable? University of Virginia CS 655

35. 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

36. 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 leftmost first finds the normal form if there is one. • Proof by trust the theory people, but don’t become one. University of Virginia CS 655

37. Charge • PS2 is due next Thursday • Answers should be short • Domains are like types in programming languages, we will see them again soon... • Lots of readings out today, but check manifest carefully to see what you should read! University of Virginia CS 655