
Announcements Introduction to Domain Theory • Homework 2 is due today • Thanks for the feedback and keep it coming! Meeting 8, CSCI 5535, Spring 2009 – MW class, reading posts after class 2 Questions? Review of Denotational Semantics 3 Denotational Semantics of Rough Idea of Denotational Semantics Arithmetic Expressions • The meaning of an arithmetic expression e in • We inductively define a function state σ is a number n A· : Aexp → (Σ → Z) • So, we try to define Ae as a function that maps the current state to an integer : An σ = the integer denoted by literal n A· : Aexp → (Σ → Z) Ax σ = σ(x) • The meaning of boolean expressions is defined Ae +e σ = A e σ + A e σ in a similar way 1 2 1 2 Ae -e σ = A e σ -Ae σ B· : Bexp → (Σ → {true, false}) 1 2 1 2 Ae *e σ = A e σ * A e σ • All of these denotational function are total 1 2 1 2 – Defined for all syntactic elements – For other languages it might be convenient to • This is a total function (= defined for all define the semantics only for well-typed elements expressions) 5 6 1 Denotational Semantics of Seems Easy So Far Boolean Expressions • We inductively define a function B· : Bexp → (Σ → {true , false }) Btrue σ = true Bfalse σ = false σ σ σ Bb1 Æ b2 = B b1 Æ Bb2 σ σ σ Be1 = e2 = if A e1 = A e2 then true else false 7 8 Denotational Semantics of Commands Denotational Semantics of Commands • We try: Σ Σ C· : Com → ( → ⊥) Cwhile b do c σ = ?? Cskip σ = σ σ σ σ Cx := e = [x := Ae ] • Can’t use the same tricks as in σ σ Cc1; c2 = C c2 (C c1 ) operational semantics (directly) σ Cif b then c1 else c2 = σ σ σ if Bb then C c1 else C c2 9 10 What’s Start from the Unwinding Equation DS of While: Unwinding this for? • What’s the unwinding equation? • Notation: W = Cwhile b do c • Idea: rely on the equivalence (justification?) while b do c ≈ if b then c; while b do c else skip • Try W( σ) = if Bbσ then W(Ccσ) else σ • This is called the unwinding equation • It is not a good denotation of W because: – It defines W in terms of itself – It is not evident that such a W exists – It does not describe W uniquely – It is not compositional 11 12 2 Preview: while k-steps Semantics while Semantics Σ → Σ ∈ N • Define Wk: ⊥ (for k ) such that • How do we get W from Wk? σ’ if “while b do c” in state σ σ σ σ ≠ ’ if ∃k.Wk( ) = ’ ⊥ terminates in fewer than k W( σ) = W (σ) = ⊥ otherwise k iterations in state σ’ ⊥ otherwise • We can define the Wk functions as follows: σ W0( ) = ⊥ • This is a compositional definition of W W (Ccσ) if Bbσ for k ≥ 1 – Depends only on Cc and Bb W (σ) = k-1 k σ otherwise 13 14 It Works, But … • This solution is not quite satisfactory because End of Review – It has an operational flavor (= “run the loop”) – It does not generalize easily to more On to Domain Theory complicated semantics (e.g., higher-order functions) 15 Recall: Denotational Game Plan Simple Domain Theory • Since while is recursive • Consider programs in an eager, – always have something like: W( σ) = F(W( σ)) deterministic language with one variable • Admits many possible values for W( σ) called “x” • We will order them – All these restrictions are just to simplify the examples – With respect to non-termination = “least” • A state σ is just the value of x • And then find the least fixed point – Thus we can use Z instead of Σ What’s a fixed point? • The semantics of a command give the LFP W( σ)=F(W( σ)) == meaning of “while” value of final x as a function of input x C c : Z → Z 17 ⊥ 18 3 Examples Revisited What is the Desired Solution? • Take Cwhile true do skip – Unwinding equation reduces to W(x) = W(x) – Any function satisfies the unwinding equation – Desired solution is W(x) = ⊥ • Take Cwhile x ≠ 0 do x := x – 2 – Unwinding equation: W(x) = if x ≠ 0 then W(x – 2) else x Z – Solutions (for all values n, m ∈ ⊥): W(x) = if x ≥ 0 then if x even then 0 else n else m – Desired solution: W(x) = if x ≥ 0 Æ x even then 0 else ⊥ 19 20 An Ordering of Solutions Alternative Views of Function Ordering Z Z • The desired solution is the one in which all the • A semantic function f ∈ → ⊥ can be arbitrariness is replaced with non-termination Z Z written as Sf ⊆ × as follows: – The arbitrary values in a solution are not uniquely Z ≠ determined by the semantics of the code Sf = { (x, y) | x ∈ , f(x) = y ⊥ } • We introduce an ordering of semantic – set of “terminating” values for the function functions • If f ⊑ g then • Let f, g ∈ Z → Z ⊥ – Sf ⊆ Sg (and vice-versa) • Define f ⊑ g as – We say that g refines f Z ∀x ∈ . f(x) = ⊥ or f(x) = g(x) – We say that f approximates g – A “smaller” function terminates at most as often , – We say that g provides more information and when it terminates it produces the same result 21 than f 22 The “Best” Solution Any questions that come to mind? • Consider again Cwhile x ≠ 0 do x := x – 2 – Unwinding equation: W(x) = if x ≠ 0 then W(x – 2) else x • Not all solutions are comparable: W(x) = if x ≥ 0 then if x even then 0 else 1 else 2 W(x) = if x ≥ 0 then if x even then 0 else ⊥ else 3 W(x) = if x ≥ 0 then if x even then 0 else ⊥ else ⊥ (last one is least and best) 23 24 4 Any questions that come to mind? A Recursive Labyrinth • Is there always a least solution? • How do we find it? • If only we had a general framework for answering these questions … 25 26 Fixed-Point Equations Fixed-Point Equations • Consider the general unwinding equation for • We can find such a (recursive) context for any while looping construct while b do c ≡ if b then c; while b do c else skip – Consider: fact n = if n = 0 then 1 else n * fact (n – 1) • We define a context K (command with a hole) K = if b then c; • else skip K(n) = while b do c ≡ K[while b do c] – The grammar for K does not contain “while b do c” fact = 27 28 Fixed-Point Equations Fixed-Point Equations • We can find such a (recursive) context for any • The meaning of a context is a semantic functional Z Z Z Z looping construct F : ( → ⊥) → ( → ⊥) such that Recall: What is – Consider: fact n = if n = 0 then 1 else n * fact (n – 1) F K[w] = F w the type of ·? – K(n) = if n = 0 then 1 else n * • (n – 1) – fact = K[ fact ] • For “while”: K = if b then c; • else skip F w x = if b x then w ( c x) else x –F depends only on c and b • We can rewrite the unwinding equation for while – W(x) = if b x then W( c x) else x – or, W x = F W x for all x, – or, W = F W (by function equality) 29 30 5 Fixed-Point Equations Can We Solve This? • The meaning of “while” is a solution for W = F W • Good news: the functions F that • Such a W is called a fixed point of F correspond to contexts in our language • We want the least fixed point have least fixed points ! – We need a general way to find least fixed points • The only way F w x uses w is by invoking it • Whether such a least fixed point exists depends on the properties of function F • If any such invocation diverges, then F w x – Counterexample: F w x = if w x = ⊥ then 0 else ⊥ diverges! – Assume W is a fixed point – F W x = W x = if W x = ⊥ then 0 else ⊥ – Pick an x, then if W x = ⊥ then W x = 0 else W x = ⊥ • It turns out: F is monotonic, continuous – Contradiction. This F has no fixed point! – Not shown here! 31 32 New Notation: λλλ The Fixed-Point Theorem • λλλx. e • If F is a semantic function corresponding to a context in our language – an anonymous function with body e and argument x apply –F is monotonic and continuous (we assert) F, k – For any fixed-point G of F and k ∈ N times Fk(λx. ⊥ ) ⊑ G • Example: double(x) = x+x leastleast-- – The least of all fixed points is upper- double = λx. x+x k bound ⊔k F (λx. ⊥) • Example: allFalse(x) = false • Proof (not detailed in the lecture): allFalse = λx. false 1. By mathematical induction on k. Base: F0(λx. ⊥ ) = λx. ⊥ ⊑ G • Example: multiply(x,y) = x*y Inductive: Fk+1(λx. ⊥ ) = F(Fk(λx. ⊥ )) ⊑ F(G) = G k multiply = λx. λy. x*y – Suffices to show that ⊔k F (λx. ⊥ ) is a fixed-point k k+1 k F( ⊔k F (λx. ⊥ )) = ⊔k F (λx. ⊥ ) = ⊔k F (λx. ⊥ ) 33 by continuity 34 Denotational Semantics for while Examples: DS for while k λ • We can use the fixed-point theorem to write while b do c = ⊔k F ( x. ⊥) the denotational semantics of while: where F f x = if b x then f ( c x) else x • while true do skip = • while x ≠≠≠ 0 then x := x – 1 while b do c = ⊔ Fk (λx.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages8 Page
-
File Size-