1 / 68

Noam Rinetzky Lecture 4: Denotational Semantics

Program Analysis and Verification 0368- 4479 http://www.cs.tau.ac.il/~maon/teaching/2013-2014/paav/paav1314b.html. Noam Rinetzky Lecture 4: Denotational Semantics. Slides credit: Roman Manevich , Mooly Sagiv , Eran Yahav. Good manners. Mobiles. Admin. Grades

lixue
Download Presentation

Noam Rinetzky Lecture 4: Denotational Semantics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Program Analysis and Verification 0368-4479http://www.cs.tau.ac.il/~maon/teaching/2013-2014/paav/paav1314b.html Noam Rinetzky Lecture 4: Denotational Semantics Slides credit: Roman Manevich, MoolySagiv, EranYahav

  2. Good manners • Mobiles

  3. Admin • Grades • First home assignment will be published on Tuesday • (contents according to progress today) • Due lesson 6 • Scribes (this week) • Scribes (next week) • From now on – in singles

  4. What do we mean? P P: x7 syntax semantics

  5. Why formal semantics? • Implementation-independent definition of a programming language • Automatically generating interpreters (and some day maybe full fledged compilers) • Verification and debugging • if you don’t know what it does, how do you know its incorrect?

  6. Programming Languages • Syntax • “how do I write a program?” • BNF • “Parsing” • Semantics • “What does my program mean?” • …

  7. Program semantics Operational: State-transformer Denotational: Mathematical objects Axiomatic: Predicate-transformer

  8. Denotational semantics • Giving mathematical models of programming languages • Meanings for program phrases (statements) defined abstractly as elements of some suitable mathematical structure. • It is not necessary for the semantics to determine an implementation, but it should provide criteria for showing that an implementation is correct • Dana Scott 1980

  9. Syntax: While Abstract syntax: a ::= n | x | a1+a2 | a1a2 | a1–a2 b ::=true|false|a1=a2|a1a2|b|b1b2 S::=x:=a | skip | S1;S2| ifbthenS1elseS2 | whilebdoS

  10. Syntactic categories n Num numerals x Var program variables a Aexp arithmetic expressions b Bexp boolean expressions S Stm statements

  11. Denotational semantics A a  ,B  b ,Sns S , Ssos S  A: Aexp (N) B: Bexp(T) S: Stm() Defined by structural induction

  12. Semantic categories ZIntegers {0, 1, -1, 2, -2, …} T Truth values {ff, tt} StateVar Z Example state: s=[x5, y7, z0] Lookup: s x = 5 Update: s[x6] = [x6, y7, z0]

  13. Denotational Semantics S, s s’, whilebdoS, s’s’’whilebdoS, ss’’ • if B b s = tt • A “mathematical” semantics • S is a mathematical object • A fair amount of mathematics is involved • Compositional • whilebdoS = F(b, S) • Recall: • More abstract and canonical than Op. Sem. • No notion of “execution” • Merely definitions • No small step vs. big step • Concurrency is an issue

  14. Denotational Semantics • Denotational semantics is also called • Fixed point semantics • Mathematical semantics • Scott-Strachey semantics • The mathematical objects are called denotations • Denotation: meaning; especially, a direct specific meaning as distinct from an implied or associated idea • Though we still maintain a computationalintuition

  15. Important features Syntax independence: The denotations of programs should not involve the syntax of the source language. Soundness: All observably distinct programs have distinct denotations; Full abstraction: Two programs have the same denotations precisely when they are observationally equivalent. Compositionality

  16. Plan • Denotational semantics of While (1st attempt) • Math • Complete partial orders • Monotonicity • Continuity • Denotational semantics of while

  17. Denotational semantics • A: Aexp (N) • B: Bexp(T) • S: Stm() • Defined by structural induction • Compositional definition

  18. Denotational semantics A a  ,B  b ,Sns S , Ssos S  • A: Aexp (N) • B: Bexp(T) • S: Stm() • Defined by structural induction • Compositional definition

  19. Denotational semantics of Aexp Lemma: A a  is a function Functions represented as sets of pairs A: Aexp (N) An = {(, n) |  } AX = {(,  X) |  } Aa0+a1 = {(, n0+n1) | (, n0)Aa0, (,n1)Aa1} Aa0-a1 = {(, n0-n1) | (, n0)Aa0, (,n1)Aa1} Aa0a1 = {(, n0  n1) | (, n0)Aa0, (,n1)Aa1}

  20. Denotational semantics of Aexpwith  Functions represented as lambda expressions A: Aexp (N) An = .n AX = .(X) Aa0+a1 = .(Aa0+Aa1) Aa0-a1 = .(Aa0-Aa1) Aa0a1 = .(Aa0 Aa1)

  21. Denotational semantics of Bexp Lemma: Bb is a function B: Bexp (T) Btrue = {(, true) |  } Bfalse = {(, false) |  } Ba0=a1 = {(, true) |   & Aa0=Aa1 }{(, false) |   & Aa0Aa1 } Ba0a1 = {(, true) |   & Aa0  Aa1 }{(, false) |   & Aa0Aa1 } Bb = {(, T t) |  , (, t) Bb} Bb0b1 = {(, t0 Tt1) |  , (, t0) Bb0, (, t1) Bb1 } Bb0b1 = {(, t0 Tt1) |  , (, t0) Bb0, (, t1) Bb1 }

  22. Denotational semantics of statements? • Intuition: • Running a statement s starting from a state  yields another state ’ • Can we define Ss as a function that maps  to ‘ ? • S.: Stm()

  23. Denotational semantics of commands? • Problem: running a statement might not yield anything if the statement does not terminate • Solution: a special element to denote a special outcome that stands for non-termination • For any set X, we write X for X  {} • Convention: • whenever f  X  X  we extend f to X  X“strictly” so that f() = 

  24. Denotational semantics of statements? • We try: • S . : Stm(  ) • S skip= • S  s0 ; s1= S s1 (S s0 ) • S if b then s0 else s1= if Bb  then S s0 else S s1

  25. Examples • S X:= 2; X:=1= [X1] • S if true then X:=2; X:=1 else …= [X1] • The semantics does not care about intermediate states • So far, we did not explicitly need 

  26. Denotational semantics of loops? S while b do s  = ?

  27. Denotational semantics of loops? • Goal: Find a function from states to states such which defines the meaning of W • Intuition: • while b do s  • if b then (s; while b do s) else skip

  28. Denotational semantics of loops? • Goal: Find a function from states to states such which defines the meaning of W • Intuition: • Swhile b do s = • Sif b then (s; while b do s) else skip

  29. Denotational semantics of loops? • Goal: Find a function from states to states such which defines the meaning of W • Intuition: • Swhile b do s = • Sif b then (s; while b do s) else skip

  30. Denotational semantics of loops? • Abbreviation W=S while b do s • Solution 1: • W() = if Bb then W(Ss) else  • Unacceptable solution • Defines W in terms of itself • It not evident that a suitable W exists • It may not describe W uniquely (e.g., for while true do skip)

  31. Denotational semantics of loops? • Goal: Find a function from states to states such which defines the meaning of W • Approach: Solve domain equation • Swhile b do s = • Sif b then (s; while b do s) else skip

  32. Introduction to Domain Theory output input We will solve the unwinding equation through a general theory of recursive equations Think of programs as processors of streams of bits (streams of 0’s and 1’s, possibly terminated by $)What properties can we expect?

  33. Motivation • Let “isone” be a function that must return “1$” when the input string has at least a 1 and “0$” otherwise • isone(00…0$) = 0$ • isone(xx…1…$) =1$ • isone(0…0) =? • Monotonicity: in terms of information • Output is never retracted • More information about the input is reflected in more information about the output • How do we express monotonicity precisely?

  34. Montonicity • Define a partial orderx  y • A partial order is reflexive, transitive, and anti-symmetric • y is a refinement of x • “more precise” • For streams of bits x y when x is a prefix of y • For programs, a typical order is: • No output (yet)  some output

  35. Montonicity • A set equipped with a partial order is a poset • Definition: • D and E are postes • A function f: D E is monotonic ifx, y D: x D y  f(x) E f(y) • The semantics of the program ought to be a monotonic function • More information about the input leads to more information about the output

  36. Montonicity Example • Consider our “isone” function with the prefix ordering • Notation: • 0k is the stream with k consecutive 0’s • 0is the infinite stream with only 0’s • Question (revisited): what is isone(0k )? • By definition, isone(0k$) = 0$ and isone(0k1$) = 1$ • But 0k 0k$ and 0k 0 k1$ • “isone” must be monotone, so: • isone( 0k ) isone( 0k$) = 0$ • isone( 0k ) isone( 0k1$) = 1$ • Therefore, monotonicity requires that isone(0k ) is a common prefix of 0$ and 1$, namely 

  37. Motivation • Are there other constraints on “isone”? • Define “isone” to satisfy the equations • isone()= • isone(1s)=1$ • isone(0s)=isone(s) • isone($)=0$ • What about 0? • Continuity: finite output depends only on finite input (no infinite lookahead) • Intuition: A program that can produce observable results can do it in a finite time

  38. Chains • A chain is a countable increasing sequence<xi> = {xiX | x0x1 … } • An upper bound of a set if an element “bigger” than all elements in the set • The least upper bound is the “smallest” among upper bounds: • xi  <xi> for all i N • <xi> y for all upper bounds y of <xi> and it is unique if it exists

  39. 0 1 2 …  Complete Partial Orders  2 1 0 The chain 0 12… does not have an upper bound • Not every poset has an upper bound • with  n and nn for all n N • {1, 2} does not have an upper bound • Sometimes chains have no upper bound

  40. Complete Partial Orders It is convenient to work with posets where every chain (not necessarily every set) has a least upper bound A partial order P is complete if every chain in P has a least upper bound also in P We say that P is a complete partial order (cpo) A cpo with a least (“bottom”) element  is a pointed cpo(pcpo)

  41. Examples of cpo’s • Any set P with the order x y if and only if x = y is a cpoIt is discrete or flat • If we add  so that  x for all x  P, we get a flat pointed cpo • The set N with  is a poset with a bottom, but not a complete one • The set N  {  } with n  is a pointed cpo • The set N with is a cpo without bottom • Let S be a set and P(S) denotes the set of all subsets of S ordered by set inclusion • P(S) is a pointed cpo

  42. Constructing cpos If D and E are pointed cpos, then so is D × E(x, y) D×E (x’, y’) iff x D x’ and yE y’D×E = (D , E ) (x i , yi ) = ( D xi , E y i)

  43. Constructing cpos (2) If S is a set of E is a pcpos, then so is S  Em  m’iffs S: m(s)E m’(s)SE = s. E (m , m’ ) = s.m(s)E m’(s)

  44. Continuity A monotonic function maps a chain of inputs into a chain of outputs:x0 x1…  f(x0)  f(x1)  … It is always true that:i <f(xi)>  f(i<xi>) Butf(i<xi>)i <f(xi)> is not always true

  45. A Discontinuity Example   3 2 1 0 f(i <xi>)i <f(xi)>  1

  46. Continuity Each f(xi) uses a “finite” view of the input f(<xi> ) uses an “infinite” view of the input A function is continuous whenf(<xi>) =i<f(xi)> The output generated using an infinite view of the input does not contain more information than all of the outputs based on finite inputs

  47. Continuity Each f(xi) uses a “finite” view of the input f(<xi> ) uses an “infinite” view of the input A function is continuous whenf(<xi>) =i<f(xi)> The output generated using an infinite view of the input does not contain more information than all of the outputs based on finite inputs Scott’s thesis: The semantics of programs can be described by a continuous functions

  48. Examples of Continuous Functions • For the partial order ( N { },  ) • The identity function is continuousid(ni) =id(ni ) • The constant function “five(n)=5” is continuousfive(ni) =five(ni ) • If isone(0) = then isone is continuos • For a flat cpo A, any monotonic function f: A Asuch that f is strict is continuous • Chapter 8 of the Wynskel textbook includes many more continuous functions

  49. Fixed Points { W(Ss ) if Bb()=true W() = if Bb()=false  if Bb()=  { • W(Ss )if Bb()=true •  if Bb()=false  if Bb()=  Solve equation:where W:∑ ∑ ; W= Swhile be do s Alternatively, W = F(W) where: F(W) = .

  50. Fixed Point (cont) • Thus we are looking for a solution for W = F( W) • a fixed point of F • Typically there are many fixed points • We may argue that W ought to be continuousW [∑ ∑] • Cut the number of solutions • We will see how to find the least fixed point for such an equation provided that F itself is continuous

More Related