CS 541 Lecture -*- Outline -*- * Recursion and Fixed Points ** Motivation: problems with recursive definitions adapted from Schmidt sections 3.3 and 6.0 (compare Watt section 5.3) *** the problem It's convenient to describe functions using recursion, certainly in programming languages, also tempting in semantics. The problem is, are they well defined? (can prove they are for recursion over abstract syntax, but not in general, as the following example shows) ---------------- EXAMPLE RECURSIVE DEFINITION q: Natural -> Natural ---------------------- think of Natural as a lifted domain; i.e., has \bot in it (Watt's convention) ----------------------- q(x) = if (x = 0) then 1 else q(sum(x,1)) ---------------- Q: is this well-defined? What does well-defined mean? Q: What is q(1)? q(2)? Some possible functions that satisfy this equation f(x) = if (x = 0) then 1 else \bot f0(x) = if (x = 0) then 1 else 0 f1(x) = if (x = 0) then 1 else 1 f2(x) = if (x = 0) then 1 else 2 f3(x) = if (x = 0) then 1 else 3 ... and infinitely many others! Q: is it possible to have a recursive definition with just 1 solution? q(x) = 1, usual def of factorial Here's one with no solution, if Nat = {0,1,2,...} doesn't contain \bot ------------------- f: Nat -> Nat (Nat = {0,1,2,...}) f(n) = f(n)+1 ------------------- The problem is how to pick from among the possible solutions a best solution *** Recursive domain equations have similar problems ---------------- RECURSIVE DOMAIN EQUATIONS Alist = Unit + (A x Alist) D = B + (D -> D) ---------------- second example has cardinality problems in set theory, and was what led Scott to his theory of domains *** What we want out of a solution ----------------------- CRITERIA FOR SOLUTION TECHNIQUES FOR SOLVING RECURSIVE EQUATIONS 1. guarantee that a recursive def has at least one solution 2. a way to choose the "best" such solution 3. the "best" solution should corresponds to the operational semantics ---------------------- Q: what meaning would a programming language give for def of q? that is, what is it's operational meaning? ** Examples let \bot be "less defined" than any number (expression) think of \bot as infinite loop *** factorial example --------------- FACTORIAL EXAMPLE fac: Nat -> Nat_{\bot} fac(n) = if (n = 0) then 1 else product (n, fac(difference(n, 1))) ----------------------- only function that satisfies this is factorial function: ! ----------------------- graph(!) = {(0,1), (1,1), (2,2), (3,6), ..., (n, n!), ...} ---------------------- **** idea: successive approximations to fac's graph look at behavior for 0, 1, ..., n unfoldings of its rec. def. ------------------- APPROXIMATIONS fac[0] = \n . \bot fac[1] = \n . if (n = 0) then 1 else product (n, fac[0](difference(n, 1))) ... fac[i] = \n . if (n = 0) then 1 else product (n, fac[i-1](difference(n, 1))) --------------------- each fac[i] is a better approximation to ! than fac[i-1]. Q: What is fac[0](0)? fac[1](0)? fac[1](1)? fac[4](3)? --------------------- \inf union graph(fac[i]) = graph(!) i=0 ------------------- This definition is inductive, not recursive. But it's rather unwieldy. Q: Can the definition of the approximations be written down more compactly? If you abstract out i, still get a recursive function! So think about abstracting the process that generates the next approximation from a given one. Q: what is the function that generates a better approximation from a given approximation? Another way to think of this: how to express fac in terms where we've taken fac out as a parameter in the form fac = F(fac) **** functional abstraction of all fac[i] --------------- ABSTRACTION OF fac[i] F: (Nat -> Nat_{\bot}) -> (Nat -> Nat_{\bot}) F = \f . \n . if (n = 0) then 1 else product (n, f(difference(n, 1))) Note that the original definition was: fac = F(fac) --------------- so every solution of the recursive defintion has this relation to F. (is a fixed point of F) so fac[0] = F(\n.\bot) and fac[i] = F(fac[i-1]), Q: What is F(fac[i])? F(F(fac[i]))? F(F(F(fac[i])))? F(F(fac[0]))? **** limit of approximations ------------------- LIMIT OF APPROXIMATIONS \inf union graph(F^i(\n.\bot)) = graph(!), i=0 where F^0(f) = f and F^i(f) = F(F ... (F(f))), if i>0 (i times) ------------------- **** limit is a fixed point lots of things in life are fixed points (stable under some influence) being in love (under affection), being religious (under worship) --------------------- FIXED POINTS def: a fixed point of a function f: D -> D is a x in D such that f(x) = x. EXAMPLE F(!) = !, because graph(F(!)) = graph(!) ---------------------- reasoning uses extensionality fixed points are important because they exactly characterize solutions to recursive defintions. E.g., fac = F(fac) Hence our approximation process yielded a fixed point, that gives us a solution to the recursive equation *** q example recall this from before ---------------- SOLVING THE q EXAMPLE q: Nat -> Nat_{\bot} q(x) = if (x = 0) then 1 else q(sum(x,1)) ---------------- **** approximate q's graph by looking at its behavior for 0, 1, ..., n unfoldings of its recursive definition. ---------------- APPROXIMATIONS q[0] = \x . \bot q[i] = \x . if (x = 0) then 1 else q[i-1](sum(x,1)) ---------------- each q[i] is a better approximation to infinite unfolding than q[i-1]. **** abstraction of all q[i]: ---------------- ABSTRACTION OF q[i] Fq: (Nat -> Nat_{\bot}) -> (Nat -> Nat_{\bot}) Fq = \f . \x . if (x = 0) then 1 else f(sum(x,1)) ---------------- **** calculate limit -------------------- CALCULATION OF LIMIT \inf union graph(Fq^i(\x.\bot)) = ? i=0 Fq^0(\x.\bot) = \x.bot Fq^1(\x.\bot) = Fq(\x.\bot) = \x. if (x = 0) then 1 else (\x.\bot)(sum(x,1)) = \x. if (x = 0) then 1 else \bot Fq^2(\x.bot) = Fq(Fq(\x.\bot)) = \x. if (x = 0) then 1 else (Fq(\x.\bot))(sum(x,1)) = \x. if (x = 0) then 1 else (\x. if (x = 0) then 1 else \bot) (sum(x,1)) = \x. if (x = 0) then 1 else \bot ------------------------ Q: What is Fq^3(\x.\bot)? ---------------------- so graph(Fq^1(\x.\bot)) = graph(Fq^2(\x.\bot)) = {(0,1)} union {(x,\bot)| x <> 0} ------------------------ Q: So what is graph(Fq^3(\x.\bot))? and the limit? **** importance of fixed points in a word: fixed points *are* solutions to recursive equations let qlimit be function with this graph i.e., qlimit = \x. if (x = 0) then 1 else \bot Q: is the limit a fixed point of Fq? yes, Fq(qlimit) = qlimit Q: what other functions satisfy q's definition? Q: Can you write down a description of them? Each function that satisfies q's def has graph of form {(0,1)} union {(i,k) | k in Nat_{\bot}} Q: Is each such function is a fixed point of Fq? (yes) Q: Which fixed point should we pick? Want the one that matches what computer does operationally, which is qlimit How to pick this by a definition, without reference to operational sem? consider \bot to be "less defined than" any proper k, extending this notion to the graphs of functions pointwise, get that qlimit is "less defined than" any other fixed point of Fq. pick the least defined one (makes fewest arbitrary choices) ** Mathematics: cpos and continuous functions (compare Watt section 5.2) each concept comes with a kind of function that preserves that concept *** Poset (partially ordered set) and monotone functions ------------- POSETS AND MONOTONE FUNCTIONS def: a partial order on D is a binary relation on D that is reflexive, transitive, and anti-symmetric. def: (D, R_D) is a poset iff R_D is a partial order on D. ------------- e.g., subset ordering on {a,b,c} --------------- def: a function f: (D,R_D) -> (E,R_E) is monotone iff for all x,y in D, x R_D y ==> f(x) R_E f(y) ----------------- e.g, function that adds c to a set is monotone idea in semantics is that x R_D y if y has "more information" or x is "less defined than" y. Relate to the examples above. monotone means that if a functional is given more information, it produces a better answer *** pointed poset ----------------- POINTED POSET def: a poset (D,R_D) is pointed iff it contains a least element, written \bot_D ------------------ i.e., (D,R_D,\bot_D) \bot_D is the least element of D if it exists written \bot if D understood e.g., empty set in {a,b,c} ordered by subset is the bottom element in semantics, \bot is thought of as no information, e.g., completely undefined, nontermination is one case of this *** strict functions ----------------- STRICT FUNCTION def: a function f: (D,R_D) -> (E,R_E) between pointed posets is strict iff f(\bot_D) = \bot_E ----------------- e.g., identity function on sets a strict function is intuitively one that needs the information in all its arguments. *** lub and glb ------------------ JOIN (lub) and MEET (glb) join (lub) is like: union in sets or in booleans meet (glb) is like: intersection in sets and in booleans def: x join y is element of D such that x R_D (x join y), y R_D (x join y), and if x R_D d and y R_D d then (x join y) R_D d The join may not exist. ------------------------------ Crucial that the join be *in* D. e.g., Consider the set D = { {}, {2}, {3} } ordered by subset, there is no join of {2} and {3}, because {2,3} is not in D Join only exists in semantics if information is consistent, e.g, can't join "answer is 1" with "answer is 2" if the answer is supposed to be deterministic. but can join "answer is 1" with "answer is unknown (\bot)" giving "answer is 1" ------------------------------ lub is join of a set of elements def: x meet y is element of D such that (x meet y) R_D x, (x meet y) R_D y, and if d R_D x and d R_D y then d R_D (x meet y) The meet may not exist; glb is meet of a set of elements ------------------ meet is the *dual* of join sometimes lub is used for both join and lub as defined above. Another notation for lub is |_|, dually for glb. *** lattice def: a lattice is a poset with lub and glbs of finite sets of elements i.e., (D,R_D,join,meet) e.g., subset ordering on {a, b, c} def: a complete (w-complete) lattice has lub and glb of arbitrary subsets i.e., (D,R_D,lub,glb) this turns out not to be very important in denotational semantics *** chains ---------------------------- CHAINS def: a chain is a nonempty, totally ordered poset Example: (Integers, <=) ({fac[i] | i in Nat}, {(fac[i],fac[i+1])}) Counterexample: (PowerSet({2,3}), subset) --------------------------- in semantics, a chain is family of elements that have consistent info e.g., sequence of approximations (as in examples) chains either finite or infinite, but non-empty *** cpos (complete partial orders, domains, w-complete partial orders, w-CPOs) (D,R_D,lub) ----------------------- COMPLETE PARTIAL ORDERS (CPOS) def: a cpo is a partial order such that every chain has a lub. --------------------- think of lub of a chain as its limit --------------------- Other name: w-cpos examples: any finite poset counterexample: (Nat, <=) --------------------- this is a key concept, and sometimes called domains *** pointed (strict) cpo (D,R_D,lub,\bot_D) ---------------------- DOMAINS = POINTED CPOs def: a pointed cpo is a cpo with a least element (\bot). Other name: domain, strict cpo ----------------------- these are important subclass of cpos, since least fixed points of fuctions to pointed cpos are guaranteed to exist. ** Standard domains (Watt 5.2, Schmidt 6.5) ------------------------ STANDARD DOMAINS Truth-value: true false \ / | Natural: 0 1 2 3 4 5 6 ... | def: a domain (D,R_D) is flat iff for all x, y in D, if x R_D y and not(x=y), then x = | ------------------------ In the above, | means \bot *** products and sums: related pointwise -------------------------- PRODUCT AND SUM DOMAINS def: if D and E are domains, then D x E is the domain with R_{DxE} defined by (d,e) R_{DxE} (d',e') iff d R_D d' and e R_E e'. def: if D and E are domains, then inD D + inE E is the domain with R_{D+E} defined by -------------------------- fill in the rest of D+E in class this kind of ordering is called "pointwise" ordering *** function domains to define function domains, need to define the notion of continuity, this address the cardinality problems in the equation V = B + (V -> V) by only taking continuous (= computable) functions, not all functions **** continuous functions = monotone function that preserves lubs of chains Watt 5.3 ------------------------- CONTINUOUS FUNCTIONS def: a function f: D -> E is continuous iff both f is monotone and for all chains X in D, f(lub(X)) = lub(f(X)) ------------------------- usually D and E will be (the same) space of functions, as in examples above of finding fixed points essentially, a continuous function can work on approximations, and the limit of its answers matches the limit of the argument approximations this allows compuation with (necessarily) finite approximations especially for functions, which can only be approximated, never fully represented in the computer since they are infinite. in short: allows information about function to be built up piecewise why do we want monotone functions? non-monotone functions are sometimes uncomputable e.g., program-halts also: if F is monotone, then the set {F^i(\bot)| i in Nat} is a chain usually treat pointed cpos as domains, so only worry about continuous functions strict continuous functions, f(\bot) = \bot not every continuous function is strict. *** order function domains pointwise ---------------------------- CONTINUOUS FUNCTION DOMAIN def: if D and E are domains, then D -> E is the domain of continuous functions from D to E such that for all f,g:D -> E, f R_{D->E} g iff for all x, f(x) R_E g(x) Thm: f: (D1 x D2) -> E is continuous iff continuous in each argument. Thm (6.24 in Schmidt): an operation built using function notation (lambda notation, no recursion) is continuous. ---------------------------- Note for topologists: see exercise 20 on page 136 of Schmidt. ** Review/Quiz on above 0. What's the solution of the following? f: Nat -> Nat_{\bot} f(n) = f(n)+1 define each of the following terms 1. partial order (D, R_D) 2. monotone function f:A->B, a1 R_A a2 implies f(a1) R_B f(a2) 3. cpo (D, R_D, lub) 4. continuous function f:A->B is a monotone function such that for all chains X in A, f(lub(X)) = lub(F(X)) 5. pointed cpo (D, R_D, lub, \bot) 6. strict function: f: A -> B is a function such that f(\bot)=\bot Can talk a bit about category theory now! (sets with certain morphisms) ** Fixed points and meanings of recursive definitions *** fixed point Recall: d in D is a fixed point of F: D -> D iff F(d) = d *** least fixed point ---------------------- LEAST FIXED POINT def: Let D be a poset, F: D -> D. Then d in D is the least fixed point of F iff d is a fixed point of F, and for all fixed points e of F in D, d R_D e Notation: fix F i.e., fix: (D->D) -> D Theorem (Scott): If D is a pointed cpo, then the least fixed point of a continuous function F: D -> D exists and is fix F = lub {F^i(\bot) | i in Nat} Application: meaning of the recursive definition f = F(f) is fix F ---------------------- *** Fixed point induction (Schmidt 6.7) (omitted) a predicate P: D -> Bool is inclusive iff for every chain C in D if for all c in C, P(c)=true, then P(lub(C))=true. def: let D be a pointed cpo, F:D->D a continuous funtion, and P:D->Bool an inclusive predicate, if 1. P(\bot) holds 2. for all d in D, if P(d) holds, then P(F(d)) holds, then P(fix F) holds.