noam rinetzky lecture 4: denotational semantics

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

Upload: lixue

Post on 23-Feb-2016

61 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Noam Rinetzky Lecture  4:  Denotational Semantics

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, Mooly Sagiv, Eran Yahav

Page 2: Noam Rinetzky Lecture  4:  Denotational Semantics

2

Good manners

• Mobiles

Page 3: Noam Rinetzky Lecture  4:  Denotational Semantics

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

Page 4: Noam Rinetzky Lecture  4:  Denotational Semantics

4

What do we mean?

Psyntax semantics

P: x7

Page 5: Noam Rinetzky Lecture  4:  Denotational 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?

Page 6: Noam Rinetzky Lecture  4:  Denotational Semantics

6

Programming Languages

• Syntax • “how do I write a program?”

– BNF– “Parsing”

• Semantics • “What does my program mean?”

– …

Page 7: Noam Rinetzky Lecture  4:  Denotational Semantics

7

Program semantics

• Operational: State-transformer• Denotational: Mathematical objects• Axiomatic: Predicate-transformer

Page 8: Noam Rinetzky Lecture  4:  Denotational Semantics

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

Page 9: Noam Rinetzky Lecture  4:  Denotational Semantics

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| if b then S1 else S2

| while b do S

Page 10: Noam Rinetzky Lecture  4:  Denotational Semantics

10

Syntactic categories

n Num numeralsx Var program variablesa Aexp arithmetic expressionsb Bexp boolean expressionsS Stm statements

Page 11: Noam Rinetzky Lecture  4:  Denotational Semantics

11

Denotational semantics• A: Aexp ( N)• B: Bexp ( T)• S: Stm ( )• Defined by structural induction

A a , B b , Sns S , Ssos S

Page 12: Noam Rinetzky Lecture  4:  Denotational Semantics

12

Semantic categories

Z Integers {0, 1, -1, 2, -2, …}TTruth values {ff, tt}State Var Z

Example state: s=[x5, y7, z0]Lookup: s x = 5Update: s[x6] = [x6, y7, z0]

Page 13: Noam Rinetzky Lecture  4:  Denotational Semantics

13

Denotational Semantics• A “mathematical” semantics– S is a mathematical object– A fair amount of mathematics is involved

• Compositional– while b do S = 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

S, s s’, while b do S, s’ s’’while b do S, s s’’

if B b s = tt

Page 14: Noam Rinetzky Lecture  4:  Denotational Semantics

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 computational intuition

Page 15: Noam Rinetzky Lecture  4:  Denotational Semantics

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

Page 16: Noam Rinetzky Lecture  4:  Denotational Semantics

16

Plan

• Denotational semantics of While (1st attempt)• Math– Complete partial orders– Monotonicity– Continuity

• Denotational semantics of while

Page 17: Noam Rinetzky Lecture  4:  Denotational Semantics

17

Denotational semantics• A: Aexp (N)• B: Bexp (T)• S: Stm ()• Defined by structural induction– Compositional definition

Page 18: Noam Rinetzky Lecture  4:  Denotational Semantics

18

Denotational semantics• A: Aexp (N)• B: Bexp (T)• S: Stm ()• Defined by structural induction– Compositional definition

A a , B b , Sns S , Ssos S

Page 19: Noam Rinetzky Lecture  4:  Denotational Semantics

19

Denotational semantics of Aexp• A: Aexp (N)• A n = {(, n) | }• A X = {(, X) | }• A a0+a1 = {(, n0+n1) | (, n0)Aa0, (,n1)Aa1}• A a0-a1 = {(, n0-n1) | (, n0)Aa0, (,n1)Aa1}• A a0a1 = {(, n0 n1) | (, n0)Aa0, (,n1)Aa1}

Lemma: A a is a functionFunctions represented as sets of pairs

Page 20: Noam Rinetzky Lecture  4:  Denotational Semantics

20

Denotational semantics of Aexp with • A: Aexp (N)• A n = .n• A X = .(X)• A a0+a1 = .(A a0+Aa1)• A a0-a1 = .(A a0-Aa1)• A a0a1 = .(A a0 Aa1)

Functions represented as lambda expressions

Page 21: Noam Rinetzky Lecture  4:  Denotational Semantics

21

Denotational semantics of Bexp• B: Bexp (T)• B true = {(, true) | }• B false = {(, false) | }• B a0=a1 = {(, true) | & Aa0=Aa1 }

{(, false) | & Aa0Aa1 }• B a0a1 = {(, true) | & Aa0 Aa1 }

{(, false) | & Aa0Aa1 }• B b = {(, T t) | , (, t) Bb} • B b0b1 = {(, t0 Tt1) | , (, t0) Bb0, (, t1) Bb1 }• B b0b1 = {(, t0 Tt1) | , (, t0) Bb0, (, t1) Bb1 }

Lemma: Bb is a function

Page 22: Noam Rinetzky Lecture  4:  Denotational Semantics

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 ( )

Page 23: Noam Rinetzky Lecture  4:  Denotational Semantics

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() =

Page 24: Noam Rinetzky Lecture  4:  Denotational Semantics

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 Bb then S s0 else S s1

Page 25: Noam Rinetzky Lecture  4:  Denotational Semantics

25

Examples

• S X:= 2; X:=1= [X1]• S if true then X:=2; X:=1 else … = [X1]

– The semantics does not care about intermediate states

– So far, we did not explicitly need

Page 26: Noam Rinetzky Lecture  4:  Denotational Semantics

26

Denotational semantics of loops?

• S while b do s = ?

Page 27: Noam Rinetzky Lecture  4:  Denotational Semantics

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

Page 28: Noam Rinetzky Lecture  4:  Denotational Semantics

28

Denotational semantics of loops?• Goal: Find a function from states to states such

which defines the meaning of W

• Intuition: – Swhile b do s =– Sif b then (s; while b do s) else skip

Page 29: Noam Rinetzky Lecture  4:  Denotational Semantics

29

Denotational semantics of loops?• Goal: Find a function from states to states such

which defines the meaning of W

• Intuition: – Swhile b do s =– Sif b then (s; while b do s) else skip

Page 30: Noam Rinetzky Lecture  4:  Denotational Semantics

30

Denotational semantics of loops?• Abbreviation W=S while b do s• Solution 1: – W() = if Bb then W(Ss ) 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)

Page 31: Noam Rinetzky Lecture  4:  Denotational Semantics

31

Denotational semantics of loops?• Goal: Find a function from states to states such

which defines the meaning of W

• Approach: Solve domain equation – Swhile b do s =– Sif b then (s; while b do s) else skip

Page 32: Noam Rinetzky Lecture  4:  Denotational Semantics

32

Introduction to Domain Theory

• 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?

input output

Page 33: Noam Rinetzky Lecture  4:  Denotational Semantics

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?

Page 34: Noam Rinetzky Lecture  4:  Denotational Semantics

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

Page 35: Noam Rinetzky Lecture  4:  Denotational Semantics

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

Page 36: Noam Rinetzky Lecture  4:  Denotational Semantics

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

Page 37: Noam Rinetzky Lecture  4:  Denotational Semantics

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

Page 38: Noam Rinetzky Lecture  4:  Denotational Semantics

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

Page 39: Noam Rinetzky Lecture  4:  Denotational Semantics

39

Complete Partial Orders

• Not every poset has an upper bound– with n and nn for all n N– {1, 2} does not have an upper bound

• Sometimes chains have no upper bound

0 1 2…

210

The chain 0 12…

does not have an upper bound

Page 40: Noam Rinetzky Lecture  4:  Denotational Semantics

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)

Page 41: Noam Rinetzky Lecture  4:  Denotational Semantics

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

Page 42: Noam Rinetzky Lecture  4:  Denotational Semantics

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 yE y’D×E = (D , E ) (x i , y i ) = ( D x i , E y i)

Page 43: Noam Rinetzky Lecture  4:  Denotational Semantics

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)SE = s. E

(m , m’ ) = s.m(s) E m’(s)

Page 44: Noam Rinetzky Lecture  4:  Denotational Semantics

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

Page 45: Noam Rinetzky Lecture  4:  Denotational Semantics

45

A Discontinuity Example

3210

1

f(i <xi>) i <f(xi)>

Page 46: Noam Rinetzky Lecture  4:  Denotational Semantics

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 when

f(<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

Page 47: Noam Rinetzky Lecture  4:  Denotational Semantics

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 when

f(<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

Page 48: Noam Rinetzky Lecture  4:  Denotational Semantics

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 Asuch that f is strict is continuous

• Chapter 8 of the Wynskel textbook includes many more continuous functions

Page 49: Noam Rinetzky Lecture  4:  Denotational Semantics

49

Fixed Points

• Solve equation:

where W:∑ ∑ ; W= Swhile be do s

• Alternatively, W = F(W) where:

F(W) = .

W(Ss ) if Bb()=true W() = if Bb()=false

if Bb()= {

W(Ss ) if Bb()=true if Bb()=false

if Bb()= {

Page 50: Noam Rinetzky Lecture  4:  Denotational Semantics

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 continuous

W [∑ ∑]• 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

Page 51: Noam Rinetzky Lecture  4:  Denotational Semantics

51

Fixed Point Theorem• Define Fk = x. F( F(… F( x)…)) (F composed k times)• If D is a pointed cpo and F : D D is continuous,

then – for any fixed-point x of F and k N

Fk () x– The least of all fixed points is

k Fk ()• Proof:

i. By induction on k.• Base: F0 ( ) = x• Induction step: Fk+1 ( ) = F( Fk ( )) F( x) = x

ii. It suffices to show that k Fk () is a fixed-point• F(k Fk ()) = k Fk+1 ( ) = k Fk ()

Page 52: Noam Rinetzky Lecture  4:  Denotational Semantics

52

Fixed-Points (notes)• If F is continuous on a pointed cpo, we know

how to find the least fixed point• All other fixed points can be regarded as

refinements of the least one– They contain more information, they are more

precise– In general, they are also more arbitrary

Page 53: Noam Rinetzky Lecture  4:  Denotational Semantics

53

Fixed-Points (notes)• If F is continuous on a pointed cpo, we know

how to find the least fixed point• All other fixed points can be regarded as

refinements of the least one– They contain more information, they are more

precise– In general, they are also more arbitrary

– They also make less sense for our purposes

Page 54: Noam Rinetzky Lecture  4:  Denotational Semantics

54

Denotational Semantics of While• ∑ is a flat pointed cpo

– A state has more information on non-termination– Otherwise, the states must be equal to be comparable (information-

wise)

• We want strict functions ∑ ∑– therefore, continuous functions

• The partial order on ∑ ∑ f g iff f(x) = or f(x) = g(x) for all x ∑– g terminates with the same state whenever f terminates– g might terminate for more inputs

Page 55: Noam Rinetzky Lecture  4:  Denotational Semantics

55

Denotational Semantics of While• Recall that W is a fixed point of

F:[[∑ ∑][∑ ∑]]

• F is continuous

• Thus, we set Swhile b do c = Fk()– Least fixed point

• Terminates least often of all fixed points

• Agrees on terminating states with all fixed point

w(Ss()) if Bb()=true F(w) = . if Bb()=false

if Bb()= {

Page 56: Noam Rinetzky Lecture  4:  Denotational Semantics

56

Denotational Semantics of While

• S skip = .• S X := exp = .[X Aexp ]

• S s0 ; s1 = . S s1 (S s0 )

• S if b then s0 else s1 =

. if Bb then S s0 else S s1 • S while b do s = Fk() – k=0, 1, … – F = w. . if Bb()=true w(Ss()) else

Page 57: Noam Rinetzky Lecture  4:  Denotational Semantics

57

Example(1)

• while true do skip• F:[[∑ ∑][∑ ∑]]

w(Ss()) if Bb()=true

F = w.. if Bb()=false if Bb()=

Btrue=.trueSskip=.

F = w..w()F0()= F1() = F2() = =

{

Page 58: Noam Rinetzky Lecture  4:  Denotational Semantics

58

Example(2)

• while false do s• F:[[∑ ∑][∑ ∑]]

Bfalse=.false

F = w..

F0()= F1() = . F2() = . =.

w(Ss()) if Bb()=true

F = w.. if Bb()=false if Bb()=

{

Page 59: Noam Rinetzky Lecture  4:  Denotational Semantics

59

Example(3) while x3 do x = x -1 = Fk() k=0, 1, …

whereF = w. . if (x)3 w([x (x) -1]) else

F0() F1() if (x)3 ([x (x) -1]) else

if (x)3 then else F2() if (x)3 then F1([x (x) -1] ) else

if (x)3 then (if [x (x) -1] x 3 then else [x (x) -1] ) else if (x)3 (if (x) 4 then else [x (x) -1] ) else if (x) {3, 4} then [x 3] else

Fk()lfp(F)

if (x) {3, 4, …k} then [x 3] else if (x) 3 then [x 3] else

Page 60: Noam Rinetzky Lecture  4:  Denotational Semantics

60

Example 4 Nested Loops

P== Z := 0; while X > 0 do(

Y := X; while (Y>0) do

Z := Z + Y; Y: = Y- 1) ;

X = X – 1

)

Page 61: Noam Rinetzky Lecture  4:  Denotational Semantics

Example 4 Nested Loops

P== Z := 0; while X > 0 do(

Y := X; while (Y>0) do

Z := Z + Y; Y: = Y- 1) ;

X = X – 1

)

sinner-loop=[Y0[] Z ) (+Z ) ( * )Y ) (+1(/2Y] if (Y)0

if (Y)<0

souter-loop=

[Y0][X0]

[ Z ) (+Z ) ( X )) ( + 1( X )1 + )2) ( + 1(/3(/4X]

if (X)0

if (X)<0sS=

[Y0][X0]

[ Z ) ( X )) ( + 1( X )1 + )2) ( + 1(/3(/4X] if (X)0

if (X)<0

61

{ {

{

Page 62: Noam Rinetzky Lecture  4:  Denotational Semantics

62

Equivalence of Semantics

• , ’: ’=Ss<s, > ’ <s, >* ’

Page 63: Noam Rinetzky Lecture  4:  Denotational Semantics

63

Complete Partial Orders

• Let (D, ) be a partial order– D is a complete lattice if every subset has both

greatest lower bounds and least upper bounds

Page 64: Noam Rinetzky Lecture  4:  Denotational Semantics

64

Knaster-Tarski Theorem

• Let f: L L be a monotonic function on a complete lattice L

• The least fixed point lfp(f) exists– lfp(f) = {x L: f(x)x}

Page 65: Noam Rinetzky Lecture  4:  Denotational Semantics

65

Fixed Points

• A monotone function f: L L where (L, , , , , ) is a complete lattice

• Fix(f) = { l: l L, f(l) = l}• Red(f) = {l: l L, f(l) l}• Ext(f) = {l: l L, l f(l)}– l1 l2 f(l1 ) f(l2 )

• Tarski’s Theorem 1955: if f is monotone then:– lfp(f) = Fix(f) = Red(f) Fix(f)– gfp(f) = Fix(f) = Ext(f) Fix(f)

f()

f()

f2()

f2()

Fix(f)

Ext(f)

Red(f)gfp(f)

lfp(f)

Page 66: Noam Rinetzky Lecture  4:  Denotational Semantics

66

Summary• Denotational definitions are not necessarily better than

operational semantics, and they usually require more mathematical work

• The mathematics may be done once and for all• The mathematics may pay off:• Some of its techniques are being transferred to operational

semantics.• It is trivial to prove that

– If Bb1 = Bb2 and C c1 = Cc2 – Then Cwhile b1 do c1 = Cwhile b2 do c2

• compare with the operational semantics

Page 67: Noam Rinetzky Lecture  4:  Denotational Semantics

67

Summary• Denotational semantics provides a way to declare

the meaning of programs in an abstract way• side-effects• loops• Recursion• Gotos• non-determinism

– But not low level concurrency• Fixed point theory provides a declarative way to

specify computations– Many usages

Page 68: Noam Rinetzky Lecture  4:  Denotational Semantics

68

The End