David Evans http://www.cs.virginia.edu/~evans Lecture 15: Crazy Eddie and the Fixed Points Background just got here last week finished degree at MIT week before Philosophy of advising students don’t come to grad school to implement someone else’s idea can get paid more to do that in industry learn to be a researcher important part of that is deciding what problems and ideas are worth spending time on grad students should have their own project looking for students who can come up with their own ideas for research will take good students interested in things I’m interested in – systems, programming languages & compilers, security rest of talk – give you a flavor of the kinds of things I am interested in meant to give you ideas (hopefully even inspiration!) but not meant to suggest what you should work on CS655: Programming Languages University of Virginia Computer Science David Evans http://www.cs.virginia.edu/~evans
University of Virginia CS 655 Menu Billy Bob and the Beta Reducers Survey Results Fixed Points 15 May 2019 University of Virginia CS 655
University of Virginia CS 655 -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. 15 May 2019 University of Virginia CS 655
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)) ... 15 May 2019 University of Virginia CS 655
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) ... 15 May 2019 University of Virginia CS 655
University of Virginia CS 655 Be Very Afraid! Some -calculus terms can be -reduced forever! The order in which you choose to do the reductions might change the result! 15 May 2019 University of Virginia CS 655
University of Virginia CS 655 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) 15 May 2019 University of Virginia CS 655
University of Virginia CS 655 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 15 May 2019 University of Virginia CS 655
University of Virginia CS 655 Elevator Speeches 15 May 2019 University of Virginia CS 655
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) 15 May 2019 University of Virginia CS 655
More interesting for functions Functions are just sets of input/output pairs. f: (Nat Nat) = n. (1 + ( f n)) 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. 15 May 2019 University of Virginia CS 655
University of Virginia CS 655 Generating Functions Any recursive definition can be encoded with a (non-recursive) generating function f: (Nat Nat) = n. (1 + ( f n)) g: (Nat Nat) (Nat Nat) = f. n. (1 + ( f n)) 15 May 2019 University of Virginia CS 655
University of Virginia CS 655 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. 15 May 2019 University of Virginia CS 655
University of Virginia CS 655 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))) 15 May 2019 University of Virginia CS 655
University of Virginia CS 655 (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. 15 May 2019 University of Virginia CS 655
University of Virginia CS 655 (gfact factorial) (gfact factorial) = n. if (n = 0) then 1 else (n * (factorial (n - 1))) = factorial factorial is a fixed point of gfact 15 May 2019 University of Virginia CS 655
University of Virginia CS 655 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? 15 May 2019 University of Virginia CS 655
University of Virginia CS 655 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 15 May 2019 University of Virginia CS 655
University of Virginia CS 655 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 15 May 2019 University of Virginia CS 655
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 D such that for any element d D, d. means “has less information than” or “is weaker than” Not all domains have a . 15 May 2019 University of Virginia CS 655
University of Virginia CS 655 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 a a a b and b c imply a c a b and b a imply a = b 15 May 2019 University of Virginia CS 655
University of Virginia CS 655 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 15 May 2019 University of Virginia CS 655
University of Virginia CS 655 Pointed Partial Order A partial order (D, ) is pointed if it has a bottom element u D such that u d for all elements d D. Bottom of (Nat, <=)? Bottom of (Nat, =)? Bottom of (Int, <=)? Bottom of ({Nat}, <=)? Not a pointed partial order Not a pointed partial order {} 15 May 2019 University of Virginia CS 655
Partial Order of Functions ((D, D) (E, E), DE) is a partial order using: f DE g if for all d D, (f d) E (g d). double: Nat Nat = n. if (n = 0) then 0 else (2 + (double (n – 1)) Is double fact under (Nat, <=)? No, (double 1) is not <= (fact 1) 15 May 2019 University of Virginia CS 655
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. 15 May 2019 University of Virginia CS 655
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. 15 May 2019 University of Virginia CS 655
University of Virginia CS 655 Fixed Point Theorem Do all -calculus terms have a fixed point? F , X such that FX = X Proof: Let W = x.F(xx) and X = WW. X = WW = ( x.F(xx))W F (WW) = FX 15 May 2019 University of Virginia CS 655
University of Virginia CS 655 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). 15 May 2019 University of Virginia CS 655
University of Virginia CS 655 Still Uncomfortable? 15 May 2019 University of Virginia CS 655
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 15 May 2019 University of Virginia CS 655
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. 15 May 2019 University of Virginia CS 655
University of Virginia CS 655 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 15 May 2019 University of Virginia CS 655