You are on page 1of 15

Semantics of Programming

Maarten de Rijke
mdr@wins.uva.nl

January 28, 1998

Learning aims for today


I Denotational semantics
I Fixed point theory


http://www.wins.uva.nl/ mdr/Teaching/index.html
January 28, 1998
Monotone functions 1

Let (D; v) and ( v ) be ccpo’s and consider a (total) function


D ;
0 0
f : D ! D
0
.

Then f is monotone if for all d , d : 1 2


1 v 2 implies 1 v
d d f d
0
2
f d :

Examples and non-examples Consider the ccpo ( (N ); P ) and the following


P
functions (N ) !P
(N ):

I 1
f X =N n X

I 2
f X = [ f27g
X

I 3
f X = \ f7 9 2898g
X ; ;

I 4
f X =f 2n Xj is primeg n

I 5
f X = f2  j 2 g
n n X
January 28, 1998
Monotone functions 2

Which of the following functionals of type

( State ! State) ! (State ! State)


, ,

are monotone:

I 0 F g = g
(
I 1 =
g 1 if g = g 2 16
, where g = g .2
F g
g 2 otherwise
(
I gs 6 0
if sx =
(F 0 g )s =
s 0
if sx =

Fact The composition of two monotone functions is a monotone function again.


Monotone functions on ccpo’s
January 28, 1998 3

The image of a chain under a monotone function is itself a chain.

v v
Lemma 4.30 Let (D; ) and (D 0; 0) be ccpo’s, and let f : D ! D
0
be a
monotone function. If Y is a chain in D , then

I f j 2 g is a chain in
f d d Y D
0

I F f j 2 g v (F ).
0
f d d Y
0
f Y

Proof: if = ; the result follows from ? v ?.


Y
0 0
f

What if Y = ? 6 ;
Monotonicity is not enough
January 28, 1998 4

Monotone functions do preserve chains, but they do not preserve least upper
bounds on chains.

Consider f : P (N [ f g) ! P (N [ f g) defined by
a a
(
X if X is finite
f X =
X [f g
a if X is infinite
Then f is monotone, but least upepr bounds are not always preserved:
G G
0
f j 2 g=
fd d Y f ( Y )
does not always hold.

Let Y = ff0 1; g j  0g. Then Ff


;:::;n n fX j 2 g=F
X Y Y =N
but
G
f ( Y ) = fN = N [f g a
January 28, 1998
Continuous functions 5

v v
Let (D; ) and (D 0; 0) be ccpo’s, and consider a (total) function f : D ! D
0
.
Then f is continuous if

I is monotone
f

I F f j 2 g = (F
0
fd d Y f Y )

for all non-empty chains Y of D .

Exercise Let (D; v) and ( v ) be ccpo’s, and let


D ;
0 0
f : D ! D
0
be a (total)
function satisfying
G G
0
f j 2 g=
fd d Y f ( Y )
for all non-empty chains Y of D .
Then f is monotone.
Composition revisited
January 28, 1998 6

v
Lemma Let (D; ) and (D 0; 0) be ccpo’s, and let f : D v ! Dand0

f : D
0 0 00
!
D be continuous functions. Then f
0
f : D  ! 00
D is a continuous

function.

First: f  f
0
is monotone.

For continuity, let Y be a non-empty chain in D . By continuity of f


G G
0
f j 2 g=
fd d Y f ( Y ):
Now f j 2 g is a non-empty chain in ; use continuity of :
fd d Y D
0
f
0

G G
f j 2 f j 2 gg = ( f j 2 g)
00
f d
0 0
d
0
fd d Y f
0 0
fd d Y

But this is the same as


G G
00
f f fd
0
j 2 g=
d Y f
0
(f ( Y ))
January 28, 1998
Fixed Point Theorem 7

Some notation: 0 = id, n+1 =  n (  0)


f f f f n

Theorem 4.37 Let : ! be a continuous function on the ccpo ( v)


f D D D;

with least element ?. Then


G n
FIX = f ? j  0g
f f n

defines an element of D and this element is the least fixed point of f .

Proof:

1. FIX f is well-defined

2. FIX f is a fixed point of f

3. FIX f is the least fixed point of f .


Example
January 28, 1998 8

(
if sx = gs 6 0
Consider the function (F 0g )s =
s if sx = 0
First, F 0 is continuous! Second, determine the set F 0n f ? j  0g, where ? is
n

the everywhere undefined function.


0?
(F 0 )s = (id )s = undef ?
(
1?) ? undef if sx = 6 0
(F 0 s = (F 0 )s =
s if sx = 0
( 1?) (
2 ? 1?) (F 0
s 6 0
if sx = undef if sx = 6 0
( F 0 2 )s = (F 0 =
0 =
0
0
F s
s if sx = s if sx =
? = n+1? for 0. So
In general F 0n F
0
n >
G n G 0 G
f ? j  0g = f ? ?g = f? 1?g = 1?
F
0
n
1 F
0
;F
0
;F
0
F
0

Thus the least fixed point of is the function 1 with 1 = undef if x 6= 0,


F
0
g g s s

and 1 = if x = 0.
g s s s
Example: factorial program
January 28, 1998 9

Sds[[y := 1 ; while :(x = 1) do (y := y  x ; x := x 1)]]s =


(FIX )( [ 7! 1]) F s y

where
(
(F g )s =
g s ( [y 7! ( y 
s x)][x 7! (sx)
s 6
1]) if sx = 1
s if sx = 1
We need to ensure that

 (State ! State v) is a ccpo (this was done before)


, ;

 F is continuous.

Then the Fixed Point Theorem applies, and gives us the semantics for the
factorial program.
Example: factorial program
January 28, 1998 10

(F 0?) = undef
s
(
(F 1?) s =
undef if sx = 1 6
if sx = 1
8
s

2?)
>
< undef 6
if sx = 1 and sx = 26
(F = s[y 7! (sy  2][x 7! 1] if sx = 2
s
>
:s if sx = 1
(
if sx < 1 or sx > n
?
(F n )s =
undef
s[y7! (sy)       2  1][x 7!  
1] if sx = j and 1 j
(
j n

undef if sx < 1
(FIX F )s =
s[y7! (sy)       2  1][x 7!
n 1] if sx = n and 1  
j n
January 28, 1998
Denotational semantics 11

I Sds : Stm ! (State ! State)


,

I Sds[[ := ]] = [ 7! A[[ ]] ]
x a s s x a s

I Sds[[skip]] = id
I Sds[[ 1 ; 2]] = Sds[[ 2]]  Sds[[ 1]].
S S S S

I Sds[[if then 1else 2]] = cond(B[[ ]] Sds[[ 1]] Sds[[ 2]])


b S S b ; S ; S

I Sds[[while do ]] = FIX , where


b S F

= cond(B[[ ]]  Sds [[ ]] id)


Fg b ;g S ; :
January 28, 1998
Denotational semantics 12

Important tools

I Let f : D !
D be a continuous function on the ccpo (D; v) with least
?
element . Then
G
FIX f = f n ? j  0g
f n

defines an element of D and this element is the least fixed point of f .

I (State ! State v) is a ccpo.


, ;

I The least upper bound F of a chain Y Y is given by


(
(
G
Y )s =
gs 6
if gs = undef for some g 2 Y

undef otherwise
Well-definedness of Sds
January 28, 1998 13

S
The semantic equations for ds define a total function from Stm to
State ! State
, .

To prove this we need:

I 0 State ! State
Let g : , ,p: State ! ftt g and define
;

0
F g = cond(p; g; g ).

Then F is continuous.

I 0 State ! State
Let g : , , and define F g = g  0.
g

Then F is continuous.
January 28, 1998
Next Time 14

Material

I Equivalence
I Extensions

Reading material

I Nielson & Nielson, pages 100–112

Exercises

I Exercises from N&N, Section 4.2

You might also like