Professional Documents
Culture Documents
Lecture 2
Lecture 2
Programming Languages
Florian Zuleger
SS 2023
1
The language While
E 2 Arith::= x | n | E + E | E * E | ...
B 2 Bool ::= true | false | E = E | E · E
|BÆB|:B
C 2 Com ::= x := E | if B then C else C | C ; C
| skip | while B do C
2
Example Program
y := 1;
while : (x=1) do
y := y * x;
x := x – 1
Questions:
• What is the behavior of this program?
• How is this program executed?
3
Evaluating Expressions
Value of expressions depend on current values
of the variables, e.g., the value of
➢x + y - 1
«x¬ s = s(x),
«n¬ s = n, using n = number(n)
«E1 + E2¬ s = «E1¬ s + «E2¬ s,
«E1 * E2¬ s = «E1¬ s * «E2¬ s,
…
Judgements:
hC,si s’
Meaning:
Command C with initial state s evaluates to final
state s’ when executing C on s.
7
Big-step Semantics of While
B-ASS
hx := E,si s[x «E¬ s]
hC1,si s1 hC2,s1i s’
B-SKIP B-SEQ
hskip,si s hC1 ; C2,si s’
hC1,si s’
B-IF.T «B¬ s = true
hif B then C1 else C2,si s’
hC2,si s’
B-IF.F «B¬ s = false
hif B then C1 else C2,si s’
Intuition:
(1) evaluate E relative to state s to some value n = «E¬ s
(2) update state s with new value n for variable x
9
The Skip Command
Evaluate command skip relative to state s?
Intuition:
(1) nothing to do
Inference Rule:
B-SKIP
hskip,si s
10
Sequential Composition
Evaluate command C1 ; C2 relative to state s?
Intuition:
(1) evaluate C1 relative to state s, to get new state s1
(2) then evaluate C2 relative to new state s1
Rule:
hC1,si s1 hC2,s1i s’
B-SEQ
hC1 ; C2,si s’
11
If Commands
Evaluate command if B then C1 else C2 relative to state s?
Intuition:
(1) first evaluate B to some Boolean value b
(2) if b equals true evaluate C1 relative to state s
(3) if b equals false evaluate C2 relative to state s
Rules:
hC1,si s’ «B¬ s = true
B-IF.T
hif B then C1 else C2,si s’
hC2,si s’
B-IF.F «B¬ s = false
hif B then C1 else C2,si s’
12
While Commands
Evaluate command while B do C relative to state s?
Intuition:
(1) first evaluate B to some Boolean value b
(2) if b equals false nothing to be done
(3) if b equals true evaluate C with state s to get new state s1
(4) then recursively evaluate while B do C relative to s1
Rules:
B-WHILE.F «B¬ s = false
hwhile B do C,si s
hC,si s1 hwhile B do C,s1i s’
B-WHILE.T «B¬ s = true
hwhile B do C,si s’
13
Example
Factorial: y := 1; while : (x=1) do (y := y * x; x := x – 1)
We assume s1(x) = 2.
s4 = s 5
s2[y 2] = s3 s3[x 1] = s4
B-ASS B-ASS «:(x=1)¬ s5 = false
14
Non-termination
Let C be the command
while : (x=1) do (y := y * x; x := x – 1).
15
Non-termination
Proof by contradiction:
Assume there is an s with s(x) · 0 and we can prove hC,si s’
for some s’.
Let s be a state such that hC,si s’ can be proven with the
fewest number of rule applications, say k rule applications.
By definition of the While the last step in deriving hC,si s’ was
by the rule:
hC,si s1 hwhile : (x=1) do (y := y * x; x := x – 1),s1i s’
17
The Meaning of Commands
«-¬B: Com ! States States
»
«C¬B transforms an initial state s into a final state.
Definition:
s’ if hC,si s’
«C¬B(s) =
? otherwise
Determinacy ensures this is proper definition.
? stands for ‘undefined’.
18
Example
Let C be the command
y := 1; while : (x=1) do (y := y * x; x := x – 1).
What is «C¬B?
Lemma
«while B do C¬B =
«if B then (C;while B do C) else skip¬B.
Proof
We split the proof into
hwhile B do C,si s’ implies
hif B then (C;while B do C) else skip,si s’
and
hif B then (C;while B do C) else skip,si s’ implies
hwhile B do C,si s’.
20
hwhile B do C,si s’ implies hif B then
(C;while B do C) else skip,si s’
Assume hwhile B do C,si s’ and «B¬ s = true.
By B-WHILE.T there is a proof for hwhile B do C,si s’,
where T1 is a proof for hC,si s1 and T2 is a proof for
hwhile B do C,s1i s’:
T1 T2
B-WHILE.T
hwhile B do C,si s’
We construct a new proof
T1 T2
B-SEQ
hC ; while B do C,si s’
B-IF.T
hif B then (C;while B do C) else skip,si s’
21
hwhile B do C,si s’ implies hif B then
(C;while B do C) else skip,si s’
Assume hwhile B do C,si s’ and «B¬ s = false.
By B-WHILE.F we have:
B-WHILE.F
hwhile B do C,si s’
Meaning:
Starting from state s when executing command C one
step of computation leads to state s’ with command C’
remaining to be executed or simply to state s’ when there
is no command C remaining.
What is a step?
Depends..
25
What is a step?
Decision:
• Ignore how expressions and Booleans are evaluated
• One step consists of
– state update
– or branching decision
26
Small-step Semantics of While
S-ASS S-SKIP
hx := E,si ! s[x «E¬ s] hskip,si ! s
Intuition:
(1) evaluate E relative to state s to some value n = «E¬ s
(2) update state s with new value n for variable x
Inference Rule:
S-ASS
hx := E,si ! s[x «E¬ s]
Intuition:
• skip has no effect on the state s, i.e., the final state
equals the initial state
Rule:
S-SKIP
hskip,si ! s
29
Sequential Composition
Execute command C1 ; C2 relative to state s?
Intuition:
(1) execute one step of C1 relative to state s
(2) if C1 has terminated start executing C2
Rules:
hC1,si ! hC1‘,s‘i
S-SEQ.STEP
hC1 ; C2,si ! hC1‘ ; C2,s’i
hC1,si ! s‘
S-SEQ.FINAL
hC1 ; C2,si ! hC2,s’i
30
If Commands
Execute command if B then C1 else C2 relative to state s?
Intuition:
(1) first evaluate B to some Boolean value b
(2) if b equals true execute C1 relative to state s
(3) if b equals false execute C2 relative to state s
Rules:
S-IF.T «B¬ s = true
hif B then C1 else C2,si ! hC1,si
Intuition:
(1) first evaluate B to some Boolean value b
(2) if b equals false then terminate
(3) if b equals true create a copy of C to be executed
before while B do C
Rules:
S-WHILE.F «B¬ s = false
hwhile B do C,si ! s
S-WHILE.T «B¬ s = true
hwhile B do C,si ! hC;while B do C,si
32
Alternative: While Commands
Execute command while B do C relative to state s?
Intuition:
• combination of (if B then C1 else C2) and
sequential composition
Rules:
S-WHILE
hwhile B do C,si !
hif B then C;while B do C else skip,si
33
Example
Factorial: y := 1; while : (x=1) do (y := y * x; x := x – 1)
We assume s1(x) = 2.
34
Example
Factorial: y := 1; while : (x=1) do (y := y * x; x := x – 1)
We assume s1(x) = 2.
35
Example
Factorial: y := 1; while : (x=1) do (y := y * x; x := x – 1)
We assume s1(x) = 2.
36
Example
Factorial: y := 1; while : (x=1) do (y := y * x; x := x – 1)
We assume s1(x) = 2.
37
Example
Factorial: y := 1; while : (x=1) do (y := y * x; x := x – 1)
We assume s1(x) = 2.
38
Example
Factorial: y := 1; while : (x=1) do (y := y * x; x := x – 1)
We assume s1(x) = 2.
hy := y * x,s2i ! s3 where s3 = s2[y 2]
h(y := y * x; x := x – 1),s2i ! hx := x – 1,s3i
39
Example
Factorial: y := 1; while : (x=1) do (y := y * x; x := x – 1)
We assume s1(x) = 2.
40
Example
Factorial: y := 1; while : (x=1) do (y := y * x; x := x – 1)
We assume s1(x) = 2.
41
Example
Factorial: y := 1; while : (x=1) do (y := y * x; x := x – 1)
We assume s1(x) = 2.
42
Example
Factorial: y := 1; while : (x=1) do (y := y * x; x := x – 1)
We assume s1(x) = 2.
43
Small-step vs Big-step Semantics
hC1,si ! hC1‘,s‘i
S-SEQ.STEP
hC1 ; C2,si ! hC1‘ ; C2,s’i
hC1,si ! s‘
S-SEQ.FINAL
hC1 ; C2,si ! hC2,s’i
hC1,si s1 hC2,s1i s’
B-SEQ
hC1 ; C2,si s’
45
Running commands: Infinite Loops
Let C be command while true do skip.
hC,si ! hskip;C, si ! hC,si ! hskip;C,si ! …
Thus, there is no state s’ such that hC,si !* s’!
Progress property:
Configurations s are terminal.
For every configuration hC,si there is either a step
hC,si ! hC’,s’i or a step hC,si ! s’.
46
Properties of Small-step Semantics
Lemma:
If hC1;C2,si !k s’ then there exists a state s’’ and
natural numbers k1 and k2 such that hC1;si !k1 s’’
and hC2,s’’i !k2 s’ where k1 + k2 = k.
Lemma:
If hC1,si !k s’ then hC1;C2,si !k hC2,s’i.
Determinacy:
If hC,si !* s1 and hC,si !* s2 then s1 = s2.
47
The Meaning of Commands
«-¬S: Com ! States States
»
«C¬S transforms an initial state s into a final (aka
terminal) state
Definition:
s’ if hC,si !* s’
«C¬S(s) =
? otherwise
Determinacy ensures this is proper definition.
? stands for ‘undefined’. 48
An Equivalence Result
Theorem
For all commands C we have «C¬B = «C¬S.
Proof
We split the proof into the two lemmas for the cases
hC,si !* s’ implies hC,si s’
and
hC,si s’ implies hC,si !* s’.
49
Mathematical Induction
Allows us to prove a property P(n) for every natural
number n:
• Base Case:
– prove P(0) is true using known facts
• Induction Case:
– assume the inductive hypothesis: P(k) is true
– prove P(k+1) is true using known facts and the
inductive hypothesis
50
Strong Mathematical Induction
To prove a property P(n) for every natural number n:
• Consider some number k .
• Assume the inductive hypothesis: P(k’) is true for
every number k’ < k
• prove P(k) is true using known facts and the inductive
hypothesis
51
Structural Induction
Consider some inductively defined structure given by
axioms and constructors, e.g.,
Tree ::= Leaf | Branch(Tree,Tree).
52
Example
leaves(Leaf) = 1
leaves(Branch(T1,T2)) = leaves(T1) + leaves(T2)
branches(Leaf) = 0
branches(Branch(T1,T2)) = branches (T1) +
branches (T2) + 1
53
Rule Induction for Deductive Systems
Assume some universe of elements U.
Axioms: an element a of U
Rules: h1 hn
...
c
where h1 ,…, hn,c are elements of U.
Let D(U) be the set of elements that can be derived from
the axioms and rules.
54
Rule Induction for Deductive Systems
To prove a property P(t) for every t 2 D(U):
• Base Case (for every axiom):
– prove P(a) using known facts
• Induction Case (for every rule):
– assume the inductive hypothesis: P(hi) is true for
every hypothesis hi
– prove P(c) is true using known facts and the
inductive hypothesis
55
hC,si s’ implies hC,si !* s’
The proof proceeds by rule induction on
hC,si s’.
Case B-ASS:
We assume hx := E,si s[x «E¬ s].
From S-ASS we get hx := E,si ! s[x «E¬ s].
56
hC,si s’ implies hC,si !* s’
Case B-SEQ:
We assume hC1 ; C2,si s’ has been derived from
hC1,si s’’ and hC2,s’’i s’ .
The induction hypothesis can be applied to both
premises hC1,si s’’ and hC2,s’’i s’ .
This gives us hC1,si !* s’’ and hC2,s’’i !* s’.
According to the lemma on slide 47 we have
hC1 ; C2,si !* hC2,s’’i.
Thus hC1 ; C2,si !* s’.
57
hC,si s’ implies hC,si !* s’
Case B-IF.T:
We assume hif B then C1 else C2,si s’ has been
derived from hC1,si s’ and «B¬ s = true.
The induction hypothesis can be applied to the premise
hC1,si s’.
This gives us hC1,si !* s’.
From S-ASS we get hif B then C1 else C2,si ! hC1,si.
Thus hif B then C1 else C2,si !* s’.
58
hC,si s’ implies hC,si !* s’
Case B-WHILE.T:
We assume hwhile B do C,si s’ has been derived from
hC,si s’’, hwhile B do C,s’’i s’ and «B¬ s = true.
The induction hypothesis can be applied to both premises
hC,si s’’ and hwhile B do C,s’’i s’.
This gives us hC,si !* s’’ and hwhile B do C,s’’i !* s’.
According to the lemma on slide 47 we have
hC ; while B do C,si !* hwhile B do C,s’’i.
From S-WHILE.T we get
hwhile B do C,si ! hC ; while B do C,si.
Together we get hwhile B do C,si !* s’.
60
hC,si !* s’ implies hC,si s’
Case C1 ; C2:
We assume that hC1 ; C2,si !k+1 s’.
According to the lemma on slide 47 we have
hC1,si !k1 s’’ and hC2,s’’i !k2 s’ for same state s’’
and some numbers k1 and k2 with k1 + k2 = k+1.
Because of k1 · k and k2 · k the induction
hypothesis can be applied to both derivation
sequences hC1,si !k1 s’’ and hC2,s’’i !k2 s’.
This gives us hC1,si s’’ and hC2,s’’i s’.
From B-SEQ we get hC1 ; C2,si s’.
61
hC,si !* s’ implies hC,si s’
Case if B then C1 else C2:
We have
hif B then C1 else C2,si ! hCi,si !k s’,
where i=1 resp. i=2 for «B¬ s = true resp. false.
The induction hypothesis can be applied to the
derivation sequence hCi,si !k s’.
This gives us hCi,si s’.
From B-IF.T resp. B-IF.F we get
hif B then C1 else C2,si s’.
62
hC,si !* s’ implies hC,si s’
Case while B do C:
«B¬ s = false: We have hwhile B do C,si ! s.
From B-WHILE.F we get hwhile B do C,si s.
63