Semantics of Programming Languages Lecture 5
Semantics of Programming Languages Lecture 5
Programming Languages
Florian Zuleger
SS 2023
1
Denotational Semantics
• In small-step semantic we were interested in how
a While program is executed.
• In contrary, we are just interested in the effect
(i.e. relating input and output) of executing a
program in denotational semantics.
• Additionally, we want to give a semantics to
While that is compositional.
2
How to define the effect of an
command?
We already defined the denotational semantics of
arithmetic and Boolean expressions as functions:
«-¬: Exp ! States ! N
«-¬: Bool ! States ! T = {true, false}
3
State Transformers
Idea:
We want to directly define a (partial) function
«-¬: Com ! States States
»
using structural induction.
5
Assignments
Effect of the command (x := E) relative to state s?
Intuition:
(1) evaluate E relative to state s to some value n = «E¬ s
(2) return the state s with new value n for variable x
State transformer:
Intuition:
(1) nothing to do
(2) return the state s
State transformer:
Intuition:
(1) determine the effect of command C1
(2) determine the effect of command C2
(3) compose the effects of the two commands
9
Sequential Composition and
Non-termination
Consider the sequential composition C1 ; C2 and
consider some state s that leads to non-termination
in command C1.
This means «C1¬(s) = ;.
Using the definition for sequential composition we
now calculate «C1 ; C2¬(s) = «C2¬ ± «C1¬(s) =
«C2¬(«C1¬(s)) = «C2¬(;) = ;.
This means that C1 ; C2 is also non-terminating for
state s as desired!
10
Filters
We introduce filter functions
filterP: (States ! T) ! States ! 2States
filterN: (States ! T) ! States ! 2States
by
{s} if b(s)=true,
filterP(b)(s) =
{} if b(s)=false.
{s} if b(s)=false,
filterN(b)(s) = {} if b(s)=true.
Intuition:
(1) determine the effect of Boolean B for the filter functions and
determine the effect of the commands C1 and C2
(2) we compose the filter functions with the effect of C1 and C2
(3) we union both effects from the previous step
13
While Commands
So far we succeeded in giving compositional
definitions to the commands in While.
«while B do C¬(s) =
filterN(«B¬) (s)
[ filterN(«B¬) ± «C¬ ± filterP(«B¬) (s)
[ filterN(«B¬) ± «C¬ ± filterP(«B¬) ± «C¬ ± filterP(«B¬) (s)
[ … [ filterN(«B¬) ± («C¬ ± filterP(«B¬))i (s) [ …
= i¸0 filterN(«B¬) ± («C¬ ± filterP(«B¬))i(s)
15
A Remark on Non-terminating Loops
Let us assume while B do C does not terminate for
some state s.
«while B do C¬(s) =
= i¸0 filterN(«B¬) ± («C¬ ± filterP(«B¬))i(s)
=;
16
Denotational Semantics of while
From the two previous slides we get the following
semantics for while:
«while B do C¬ =
filterN(«B¬)
[ filterN(«B¬) ± «C¬ ± filterP(«B¬)
[ filterN(«B¬) ± «C¬ ± filterP(«B¬) ± «C¬ ± filterP(«B¬)
[ … [ filterN(«B¬) ± («C¬ ± filterP(«B¬))i [ …
= i¸0 filterN(«B¬) ± («C¬ ± filterP(«B¬))i
17
An equation for while
We already know
«while B do C¬B =
«if B then (C;while B do C) else skip¬B.
18
Fixed Point Equation
«while B do C¬ =
«if B then (C;while B do C)Any solution
else for f =in such
skip¬
an equation
«C;while B do C¬ ± filterP(«B¬) [ «skip¬is ±called a
filterN(«B¬) =
fixed point!
«while B do C¬ ± «C¬ ± filterP(«B¬) [ filterN(«B¬)
19
«while B do C¬ is Fixed Point
Indeed, F «while B do C¬ =
= ( i¸0 filterN(«B¬) ± («C¬ ± filterP(«B¬))i ) ± «C¬ ± filterP(«B¬) [
filterN(«B¬)
= i¸0 filterN(«B¬) ± («C¬ ± filterP(«B¬))i ± «C¬ ± filterP(«B¬) [
filterN(«B¬)
= i¸0 filterN(«B¬) ± («C¬ ± filterP(«B¬))i+1 [ filterN(«B¬)
= i¸1 filterN(«B¬) ± («C¬ ± filterP(«B¬))i [ filterN(«B¬)
= i¸0 filterN(«B¬) ± («C¬ ± filterP(«B¬))i = «while B do C¬,
We have
F f = f ± «skip¬ ± filterP(«true¬) [ filterN(«true¬) =
= f ± Id ± Id [ ; = f.
22
Order of Functions
We define an order on functions States ! 2States.
23
Comparison with other Fixed Points?
Let f be a pre-fixed point of F, i.e.,
f ± «C¬ ± filterP(«B¬) [ filterN(«B¬) = F f µ f.
How does f compare to
i¸0 filterN(«B¬) ± («C¬ ± filterP(«B¬))i ?
We have filterN(«B¬) µ f.
Thus filterN(«B¬) ± «C¬ ± filterP(«B¬) µ f.
By induction filterN(«B¬) ± («C¬ ± filterP(«B¬))i µ f for all i.
Therefore i¸0 filterN(«B¬) ± («C¬ ± filterP(«B¬))i µ f.
Because fixed points are also pre fixed points we have that
i¸0 filterN(«B¬) ± («C¬ ± filterP(«B¬))i is the least fixed point of F with
regard to the order µ.
24
Notation
We use the lambda expressions
¸f. f ± «C¬ ± filterP(«B¬) [ filterN(«B¬)
as an abbreviation for the functional F defined by
F f = f ± «C¬ ± filterP(«B¬) [ filterN(«B¬).
We denote by
lfp ¸f. f ± «C¬ ± filterP(«B¬) [ filterN(«B¬)
the least fixed point of the functional F.
25
While Commands
Effect of command while B do C?
Intuition:
(1) determine the effect of Boolean B for the filter functions and
determine the effect of the command C
(2) we compose the filter function with the effect of C
(3) we determine the least fixed point of the functional that
represents iterating the loop
State transformer:
«while B do C¬ =
lfp ¸f. f ± «C¬ ± filterP(«B¬) [ filterN(«B¬)
26
Denotational Semantics
«x := E¬(s) = { s[x «E¬ s] }
«skip¬ = Id
«C1 ; C2¬ = «C2¬ ± «C1¬
«if B then C1 else C2¬ =
«C1¬ ± filterP(«B¬) [ «C2¬ ± filterN(«B¬)
«while B do C¬ =
lfp ¸f. f ± «C¬ ± filterP(«B¬) [ filterN(«B¬)
27
Example
«y := 1; while : (x=1) do (y := y * x; x := x – 1)¬
= «while : (x=1) do (y := y * x; x := x – 1)¬ ± «y := 1¬
= i¸0 filterN(«: (x=1)¬) ± («y := y * x; x := x – 1¬ ±
filterP(«: (x=1)¬))i ± «y := 1¬
= i¸0 filterP(«(x=1)¬) ± («x := x – 1¬ ± «y := y * x¬±
filterN(«(x=1) ¬))i ± «y := 1¬
= i¸0 ¸s. filterP(«(x=1)¬) ± («x := x – 1¬ ± «y := y * x¬±
filterN(«(x=1) ¬))i ± «y := 1¬(s)
28
Example
«y := 1; while : (x=1) do (y := y * x; x := x – 1)¬
= «while : (x=1) do (y := y * x; x := x – 1)¬ ± «y := 1¬
= i¸0 filterN(«: (x=1)¬) ± («y := y * x; x := x – 1¬ ±
filterP(«: (x=1)¬))i ± «y := 1¬
= i¸0 filterP(«(x=1)¬) ± («x := x – 1¬ ± «y := y * x¬±
filterN(«(x=1) ¬))i ± «y := 1¬
= i¸0 ¸s. filterP(«(x=1)¬) ± («x := x – 1¬ ± «y := y * x¬±
filterN(«(x=1) ¬))i ± «y := 1¬(s)
29
Determinism
Lemma
We have |«C¬(s)| · 1 for all commands C and all
states s.
Proof
By structural induction on the commands C.
30
Determinism
Case C1 ; C2 :
By definition we have «C1 ; C2¬ = «C2¬ ± «C1¬.
By induction assumption we have |«C1¬(s)| · 1 and
|«C2¬(s)| · 1 for all states s.
Let s be an arbitrary state.
We have «C2¬ ± «C1¬(s) = «C2¬(«C1¬(s)).
Thus either «C1¬(s) = ; or «C1¬(s) = {t} for some state t.
In the first case we get |«C2¬(«C1¬(s))| = |«C2¬(;)| = |;| = 0.
In the second case we get |«C2¬(«C1¬(s))| = |«C2¬(t)| · 1.
In both cases |«C2¬ ± «C1¬(s)| = |«C2¬(«C1¬(s))| · 1.
31
Determinism
Case if B then C1 else C2:
By definition we have «if B then C1 else C2¬ =
«C1¬ ± filterP(«B¬) [ «C2¬ ± filterN(«B¬).
By induction assumption we have |«C1¬(s)| · 1 and
|«C2¬(s)| · 1 for all states s (*).
Let s be an arbitrary state.
We either have «B¬(s) = true or «B¬(s) = false.
Hence, either (s,s) 2 filterP(«B¬) or (s,s) 2 filterN(«B¬).
Thus «C1¬ ± filterP(«B¬) [ «C2¬ ± filterN(«B¬)(s) is either
«C1¬(s) or «C2¬(s).
Therefore the conclusion follows from (*).
32
Determinism
Case while B do C:
We have «while B do C¬ = lfp ¸f. f ± «C¬ ± filterP(«B¬) [ filterN(«B¬)
= i¸0 filterN(«B¬) ± («C¬ ± filterP(«B¬))i.
By induction assumption we have |«C¬(s)| · 1 for all states s.
Thus |filterN(«B¬) ± («C¬ ± filterP(«B¬))i(s)| · 1 for all states s.
We observe that there is at most one i with
|filterN(«B¬) ± («C¬ ± filterP(«B¬))i(s)| = 1
for all states s.
Hence,
| i¸0 filterN(«B¬) ± («C¬ ± filterP(«B¬))i(s)| · 1
for all states s.
33
Contexts: Commands with Holes
C 2 Com ::= - | x := E | if B then C else C |
C ; C | skip | while B do C
Example:
for the context
C[-] = - ; while B do -
we have
C[x := x + 1] = x := x + 1 ; while B do x := x + 1
34
Contextual Equivalence
Definition
Commands C1 and C2 are contextually equivalent
with respect to the small-step semantics, if for all
contexts C[−] and states s,s’
35
Denotational Semantics
Frege’s principle of compositionality:
“The meaning of a composite phrase can be defined in
terms of the meanings of its parts”
37
Compositionality and Contextual
Equivalence
A consequence of such a function
fC: (States ! 2States) ! (States ! 2States) for a
context C[−] is that
if «C1¬ = «C2¬ then
«C[C1]¬ = fC(«C1¬) = fC(«C2¬) = «C[C2]¬!
38
An Equivalence Result
Theorem
For all commands C we have «C¬ = «C¬S.
Proof
We split the proof into the two lemmas for the cases
«C¬S µ «C¬.
and
«C¬S ¶ «C¬.
39
«C¬S µ «C¬
It is sufficient to prove that for all states s and s’
hC,si !* s’ implies «C¬(s) = s’.
40
hC,si ! s’ implies «C¬(s) = s’
Case S-ASS:
We have hx := E,si ! s[x «E¬ s].
Since «x := E¬(s) = s[x «E¬ s] the claim follows.
41
hC,si ! s’ implies «C¬(s) = s’
Case S-SEQ.FINAL:
We assume hC1 ; C2,si ! hC2,s’i.
The rule hypothesis gives us hC1,si ! s’.
We can apply the induction assumption to this
transition and get «C1¬(s) = s’.
Thus «C1 ; C2¬(s) = «C2¬ ± «C1¬(s) =
«C2¬(«C1¬(s)) = «C2¬(s’).
42
hC,si ! hC’,s’i implies «C¬(s) = «C’¬s’
Case S-SEQ.STEP:
We assume hC1 ; C2,si ! hC1’ ; C2,s’i.
The rule hypothesis gives us hC1,si ! hC1’,s’i.
We can apply the induction assumption to this
transition and get «C1¬(s) = «C1’¬(s’).
Thus «C1 ; C2¬(s) = «C2¬ ± «C1¬(s) = «C2¬(«C1¬(s)) =
«C2¬(«C1’¬(s’)) = «C2¬ ± «C1’¬(s’) = «C1‘ ; C2¬(s’).
43
hC,si ! hC’,s’i implies «C¬(s) = «C’¬s’
Case S-IF.T:
We assume «B¬ s = true.
Then, hif B then C1 else C2,si ! hC1,si.
Hence, «if B then C1 else C2¬(s) =
(«C1¬ ± filterP(«B¬) [ «C2¬ ± filterN(«B¬))(s) =
«C1¬ ± filterP(«B¬)(s) [ «C2¬ ± filterN(«B¬)(s) =
«C1¬(s) [ «C2¬ ± ; = «C1¬(s).
44
hC,si ! hC’,s’i implies «C¬(s) = «C’¬s’
Case S-WHILE:
We assume hwhile B do C,si !
hif B then C;while B do C else skip,si.
We have «while B do C¬ =
lfp ¸f. f ± «C¬ ± filterP(«B¬) [ filterN(«B¬) =
«while B do C¬ ± «C¬ ± filterP(«B¬) [ filterN(«B¬) =
«C ; while B do C¬ ± filterP(«B¬) [ Id ± filterN(«B¬) =
«if B then C ; while B do C else skip¬
45
«C¬S ¶ «C¬
We proceed by structural induction on the
command C.
Case x := E:
We have «x := E¬(s) = s[x «E¬ s].
We get hx := E,si ! s[x «E¬ s] from rule S-ASS.
46
«C¬S ¶ «C¬
Case C1 ; C2 :
By induction assumption we have «C1¬ µ «C1¬S
and «C2¬ µ «C2¬S.
Thus, we have «C1 ; C2¬ = «C2¬ ± «C1¬ µ «C2¬S ± «C1¬S.
From lecture 2 slide 38 we know that
If hC1,si !k s’ then hC1;C2,si !k hC2,s’i.
This gives us «C2¬S ± «C1¬S µ «C1 ; C2¬S.
The property then follows from the transitivity of
µ.
47
«C¬S ¶ «C¬
Case if B then C1 else C2:
By induction assumption we have «C1¬ µ «C1¬S
and «C2¬ µ «C2¬S.
We have «if B then C1 else C2¬
= «C1¬ ± filterP(«B¬) [ «C2¬ ± filterN(«B¬)
µ «C1¬S ± filterP(«B¬) [ «C2¬S ± filterN(«B¬)
= «if B then C1 else C2¬S.
48
«C¬S ¶ «C¬
Case while B do C:
By induction assumption we have «C¬ µ «C¬S.
We have «while B do C¬S =
«C ; while B do C¬S ± filterP(«B¬) [ «skip¬S ± filterN(«B¬)
¶ «while B do C¬S ± «C¬S ± filterP(«B¬) [ filterN(«B¬)
¶ «while B do C¬S ± «C¬ ± filterP(«B¬) [ filterN(«B¬)
• reflexifity: 8 x 2 L. x v x
• transitivity: 8 x,y,z 2 L. x v y Æ y v z ! x v z
• anti-symmetry: 8 x,y 2 L. x v y Æ y v x ! y = x
Upper and Lower Bounds
Let (L,v) be a partial order and X µ L some subset.
For y 2 L we define:
X v y :, ∀ x 2 X. x v y “y is an upper bound for X”
y v X :, ∀ x 2 X. y v x “y is a lower bound for X”
tX is a least upper bound for X if:
X v tX and ∀y 2 L. X v y ⇒ tX v y
uX is a greatest lower bound X if:
uX v X and ∀y 2 L. y v X ⇒ y v uX
Note that tX and uX do not always exist. If they exist, they
are unique.
Lattice
For X = {x,y} we also write x t y instead of t{x,y}
and x u y instead of u{x,y} .
Why?
These Partial Orders Are Not Lattices
Literature
"Introduction to lattices and order", B. A. Davey
and H. A. Priestley is a good introduction to
lattice theory
56
Lattice Properties
Let (L,t,u) be a lattice.
57
Top and Bottom Elements
If L has a largest element, i.e., if tL exists,
> := tL is called the top element of L.
;
Complete Lattices
A lattice (L,t,u) is complete,
if tX and uX exist for all X µ L.
Fact:
Every finite lattice is complete.
Why?
Complete Lattices
A lattice (L,t,u) is complete,
if tX and uX exist for all X v L.
Note that for a complete lattice there is always a
smallest and largest element.
Fact:
Every finite lattice is complete.
Show that tX and uX exist by induction on |X|
(the number of elements in X).
Map Lattice
Given a set A and a lattice L we define the map lattice A ! L by point-
wise operations.
Example:
Consider the map lattice States ! 2States,
where f µ g for f,g 2 States ! 2States iff f(s) µ g(s) for all s 2 States.
Function concatenation
± : (L ! L) ! (L ! L)
is montone in its first argument, i.e.,
f1 v f2 ) f1 ± g v f2 ± g for all f1,f2,g 2 L ! L,
and montone in its second argument
f1 v f2 ) g ± f1 v g ± f2 for all f1,f2 2 L ! L
if g 2 L ! L is a monotone function.
64
Example
We consider the complete lattice
(States,[,Å,>,?) and the corresponding map
lattice States ! 2States, which is also complete.
Theorem
Every continuous function f has a unique least fixed-point:
lfp(f) = ti¸0 fi(?)
Moreover for every pre fixed point x, i.e., f(x) v x,
we have lfp(f) v x.
70