<<

Announcements Introduction to • 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 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 – 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. ⊥) k – F ( λx. ⊥) x = where F f x = if b x then f ( c x) else x –F2 (λx. ⊥) x =

–F3 (λx. ⊥) x =

– LFP F =

• Not easy to find the closed form for general LFPs! 35 36

6 Examples: DS for while Discussion: Denotational Semantics ⊔ k λ ⊥ while b do c = k F ( x. ) • We can write the denotational semantics where F f x = if b x then f ( c x) else x but we cannot always compute it. λ • while true do skip = x. ⊥ – Otherwise, we could decide the halting ≠≠≠ • while x 0 then x := x – 1 problem λ = – F ( x. ⊥) x = if x 0 then x else ⊥ ≠ –F2 (λx. ⊥) x = if x = 0 then x else –H is halting for input 0 iff H 0 ⊥ if x–1 = 0 then x–1 else ⊥ • We have derived this for programs with = if 1 ≥ x ≥ 0 then 0 else ⊥ one variable –F3 (λx. ⊥) x = if 2 ≥ x ≥ 0 then 0 else ⊥

– LFP F = if x ≥ 0 then 0 else ⊥ – Generalize to multiple variables, even to variables ranging over richer data types, • Not easy to find the closed form for general LFPs! even higher-order functions: domain theory 37 38

Domain Theory Congratulations!

You just survived the hardest lectures in 5535. • A set D is a domain if It’s all downhill from here. – It has a partial order x ⊑ y • Reflexive, transitive, and anti-symmetric – There is a least element ⊥ called bottom

– Any chain x1 ⊑ … ⊑ xn ⊑ … has a least-upper bound ⊔i xi

• For all i, xi ⊑ ⊔i xi (is an upper bound)

• For any y such that ( ∀i. x i ⊑ y), we have ⊔i xi ⊑ y (least upper bound)

• Usual sets of semantic values are domains 39 40

Recall: Learning Goals For Next Time

• Key features of denotational semantics • Homework 3 out tonight, due Mon Feb 16 – Compositional – Meaning is a “ math object ” – When to use DS?

• DS uses ⊥⊥⊥ (“bottom”) to mean non- termination • DS uses fixed points and domains to handle while Remember SLAM – This is the tricky bit and BLAST? 41 42

7 Monotonicity

• Consider f0 ⊑ f1. What can we say about the relationship between F f 0 x and F f 1 x, for any x ? ≠ • Assume F f 0 x = n ⊥. Show that F f 1 x = n

Bonus: Monotonicity, – In computing F f 0 x, f0 is invoked a finite number of times Continuity, and Domains – All those invocations terminate with some values – The value of f0 at other points does not matter !

– But f1 terminates with same results everywhere f0 terminates

– Thus F f 1 x = n (determinism of F) ≠ • If F f 0 x = ⊥ , it could be that F f 1 x ⊥

– Take F f x = f x , f0 (0) = ⊥ and f1 (0) = 0

• In general, if f0 ⊑ f1 then F f 0 ⊑ F f 1 • We say that F must be monotonic

44

Monotonicity of Contexts Continuity

• If we replace the sub-command with one that • Consider F corresponding to a context in our language terminates more often, the host command will • Consider a chain g0 ⊑ … ⊑ gk with ⊔k gk = G terminate more often – Note that F gk form a chain also, because F is monotonic • The following F is not monotonic: • We’ll show that, for any x, F G x = ( ⊔k (F gk)) x F w x = if w x = ⊥ then 0 else ⊥ – We say that such functions F are continuous – This function does not correspond to a computable • If F G x = n ≠ ⊥ , then G was invoked a finite number context of times, and terminated each time • The semantics of computable contexts are – For each such invocation, there is a j, such that gj terminates with the same result monotonic – Let max be the maximum such j, for all invocations

– Can be proved by induction on the structure of – Thus, F gmax x = n , and (⊔k (F gk)) x = n context • Similar reasoning for F G x = ⊥ 45 46

Monotonicity and Continuity Example of Domains

Z Z • A function f : D 1 → D2 is monotonic iff • Example: D = → ⊥ for all x, y ∈ D: x ⊑ y ⇒ f x ⊑ f y – f ⊑ g iff for all n ∈ Z, f n = ⊥ or f n = g n λ – ⊥D = n. ⊥ • A function F : D 1 → D2 is continuous iff – For a chain f the LUB = λn. if ∃k.f x = m ≠ ⊥ then ⊔ ⊔ i k for all chains x i in D 1: F ( i xi) = i (F x i) m else ⊥ • Example: Take a set A and a special element • We can show that functions corresponding to ⊥, then A⊥ = A ∪ { ⊥} is a flat domain : usual language constructs are monotonic and – a ⊑ b iff a = ⊥ or a = b continuous ≠ – For a chain ai, LUB = if ∃k.a k ⊥ then ak else ⊥ – Show that F f x = f (f 0 x) is monotonic and • Exercise: If D and D are domains, then D → continuous, for any f that is monotonic and 1 2 1 0 D is a domain, and so is D × D continuous 2 1 2 CS 263 47 CS 263 48

8