1
Programming Languages and Compilers (CS 421)
Based heavily on slides by Elsa Gunter, which were based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha
Operational Semantics
*
2
Operational Semantics
*
3
Operational Semantics
*
4
Operational Semantics
*
5
6
Questions before we start?
7
Transition (Small Step) Semantics
Transition Semantics
(C, m) --> (C’, m’) or (C, m) --> m’
8
Transition Semantics
Transition Semantics
(C, m) → (C’, m’) or (C, m) → m’
9
Transition Semantics
Evaluation Semantics
10
Transition Semantics
Evaluation Semantics
11
Transition Semantics
Evaluation Semantics
12
Transition Semantics
Simple Imperative Language Syntax
I ∈ Identifiers
N ∈ Numerals
B ::= true | false | B & B | B or B | � not B | E < E | E = E
E ::= N | I | E + E | E * E | E - E | - E | (E)
C ::= skip | C; C | I := E |� if B then C else C fi | while B do C od
13
Transition Semantics
Simple Imperative Language Syntax
I ∈ Identifiers
N ∈ Numerals
B ::= true | false | B & B | B or B | � not B | E < E | E = E
E ::= N | I | E + E | E * E | E - E | - E | (E)
C ::= skip | C; C | I := E |� if B then C else C fi | while B do C od
14
Transition Semantics
Simple Imperative Language Syntax
I ∈ Identifiers
N ∈ Numerals
B ::= true | false | B & B | B or B | � not B | E < E | E = E
E ::= N | I | E + E | E * E | E - E | - E | (E)
C ::= skip | C; C | I := E |� if B then C else C fi | while B do C od
15
Transition Semantics
Simple Imperative Language Syntax
I ∈ Identifiers
N ∈ Numerals
B ::= true | false | B & B | B or B | � not B | E < E | E = E
E ::= N | I | E + E | E * E | E - E | - E | (E)
C ::= skip | C; C | I := E |� if B then C else C fi | while B do C od
16
Transition Semantics
Simple Imperative Language Semantics
(I, m) → (m(I), m)
17
Transition Semantics
(E, m) → (V, m’)
Id
Simple Imperative Language Semantics
(I, m) → (m(I), m)
18
Transition Semantics
(E, m) → (V, m’)
Id
Look up identifiers
Simple Imperative Language Semantics
(false & B, m) → (false, m)
(true & B, m) → (B, m)
(B, m) → (B”, m)
(B & B’, m) → (B” & B’, m)
19
Transition Semantics
(B, m) → (B’, m’)
Short-circuit
And-F
And-T
And
Simple Imperative Language Semantics
(false & B, m) → (false, m)
(true & B, m) → (B, m)
(B, m) → (B”, m)
(B & B’, m) → (B” & B’, m)
20
Transition Semantics
(B, m) → (B’, m’)
Short-circuit
And-F
And-T
And
Simple Imperative Language Semantics
(false & B, m) → (false, m)
(true & B, m) → (B, m)
(B, m) → (B”, m)
(B & B’, m) → (B” & B’, m)
21
Transition Semantics
(B, m) → (B’, m’)
Short-circuit
And-F
And-T
And
Simple Imperative Language Semantics
(false & B, m) → (false, m)
(true & B, m) → (B, m)
(B, m) → (B”, m)
(B & B’, m) → (B” & B’, m)
22
Transition Semantics
(B, m) → (B’, m’)
Evaluate first clause one step
And-F
And-T
And
Simple Imperative Language Semantics
(false & B, m) → (false, m)
(true & B, m) → (B, m)
(B, m) → (B”, m)
(B & B’, m) → (B” & B’, m)
23
Transition Semantics
(B, m) → (B’, m’)
Evaluate first clause one step
And-F
And-T
And
Simple Imperative Language Semantics
(false & B, m) → (false, m)
(true & B, m) → (B, m)
(B, m) → (B”, m)
(B & B’, m) → (B” & B’, m)
24
Transition Semantics
(B, m) → (B’, m’)
Evaluate first clause one step
And-F
And-T
And
Simple Imperative Language Semantics
(true or B, m) → (true, m)
(false or B, m) → (B, m)
(B, m) → (B”, m)
(B or B’, m) → (B” or B’, m)
25
Transition Semantics
(B, m) → (B’, m’)
Or is similar
Or-T
Or-F
Or
Simple Imperative Language Semantics
(not true, m) → (false, m)
(not false, m) → (true, m)
(B, m) → (B’, m)
(not B, m) → (not B’, m)
26
Transition Semantics
(B, m) → (B’, m’)
Not-T
Not-F
Not
Not is not too different
27
Questions so far?
Transition Semantics
Simple Imperative Language Semantics
(E, m) → (E’’, m)
(E ~ E’, m) → (E’’ ~ E’, m)
(E, m) → (E’, m)
(V ~ E, m) → (V ~ E’, m)
(U ~ V, m) → (true, m) or (false, m) depending on whether U ~ V holds or not
28
Transition Semantics
(E, m) → (E’, m’)
Evaluate LHS first …
Rel-E
Rel-V
Simple Imperative Language Semantics
(E, m) → (E’’, m)
(E ~ E’, m) → (E’’ ~ E’, m)
(E, m) → (E’, m)
(V ~ E, m) → (V ~ E’, m)
(U ~ V, m) → (true, m) or (false, m) depending on whether U ~ V holds or not
29
Transition Semantics
(E, m) → (E’, m’)
Evaluate LHS first …
Rel-E
Rel-V
Simple Imperative Language Semantics
(E, m) → (E’’, m)
(E ~ E’, m) → (E’’ ~ E’, m)
(E, m) → (E’, m)
(V ~ E, m) → (V ~ E’, m)
(U ~ V, m) → (true, m) or (false, m) depending on whether U ~ V holds or not
30
Transition Semantics
(E, m) → (E’, m’)
Evaluate LHS first …
… until you get a value
Rel-E
Rel-V
Simple Imperative Language Semantics
(E, m) → (E’’, m)
(E ~ E’, m) → (E’’ ~ E’, m)
(E, m) → (E’, m)
(V ~ E, m) → (V ~ E’, m)
(U ~ V, m) → (true, m) or (false, m) depending on whether U ~ V holds or not
31
Transition Semantics
(E, m) → (E’, m’)
Evaluate LHS first …
… until you get a value
Rel-E
Rel-V
Simple Imperative Language Semantics
(E, m) → (E’’, m)
(E ~ E’, m) → (E’’ ~ E’, m)
(E, m) → (E’, m)
(V ~ E, m) → (V ~ E’, m)
(U ~ V, m) → (true, m) or (false, m) depending on whether U ~ V holds or not
32
Transition Semantics
(E, m) → (E’, m’)
Evaluate LHS first …
… until you get a value
Rel-E
Rel-V
Simple Imperative Language Semantics
(E, m) → (E’’, m)
(E op E’, m) → (E’’ op E’, m)
(E, m) → (E’, m)
(V op E, m) → (V op E’, m)
(U op V, m) → (N, m) � where N is the specified value for U op V
33
(E, m) → (E’, m’)
Transition Semantics
Evaluate LHS first …
… until you get a value
Arith-E
Arith-V
34
Questions so far?
Transition Semantics
Simple Imperative Language Semantics
(skip, m) → m
(E, m) → (E’, m)
(I ::= E, m) → (I ::= E’, m)
(I ::= V, m) → m[I ← V ]
35
(C, m) → m’
Transition Semantics
Skip
Assign-E
Assign-V
(C, m) →(C’, m’)
Simple Imperative Language Semantics
(skip, m) → m
(E, m) → (E’, m)
(I ::= E, m) → (I ::= E’, m)
(I ::= V, m) → m[I ← V ]
36
(C, m) → m’
Transition Semantics
Skip
Assign-E
Assign-V
Skip means done evaluating
(C, m) →(C’, m’)
Simple Imperative Language Semantics
(skip, m) → m
(E, m) → (E’, m)
(I ::= E, m) → (I ::= E’, m)
(I ::= V, m) → m[I ← V ]
37
(C, m) → m’
Transition Semantics
Skip
Assign-E
Assign-V
Skip means done evaluating
(C, m) →(C’, m’)
Simple Imperative Language Semantics
(skip, m) → m
(E, m) → (E’, m)
(I ::= E, m) → (I ::= E’, m)
(I ::= V, m) → m[I ← V ]
38
(C, m) → m’
Transition Semantics
Skip
Assign-E
Assign-V
Evaluate RHS of assignment …
(C, m) →(C’, m’)
Simple Imperative Language Semantics
(skip, m) → m
(E, m) → (E’, m)
(I ::= E, m) → (I ::= E’, m)
(I ::= V, m) → m[I ← V ]
39
(C, m) → m’
Transition Semantics
Skip
Assign-E
Assign-V
Evaluate RHS of assignment …
(C, m) →(C’, m’)
Simple Imperative Language Semantics
(skip, m) → m
(E, m) → (E’, m)
(I ::= E, m) → (I ::= E’, m)
(I ::= V, m) → m[I ← V ]
40
(C, m) → m’
Transition Semantics
Skip
Assign-E
Assign-V
Evaluate RHS of assignment …
… until you get a value
(C, m) →(C’, m’)
Simple Imperative Language Semantics
(skip, m) → m
(E, m) → (E’, m)
(I ::= E, m) → (I ::= E’, m)
(I ::= V, m) → m[I ← V ]
41
(C, m) → m’
Transition Semantics
Skip
Assign-E
Assign-V
Evaluate RHS of assignment …
… until you get a value
(C, m) →(C’, m’)
Simple Imperative Language Semantics
(C, m) → (C”, m’ )
(C; C’, m) → (C”; C’, m’)
(C, m) → m’
(C; C’, m) → (C’, m’ )
42
(C, m) → m’
Transition Semantics
Evaluate first command of sequence first …
Seq-L
Seq-R
(C, m) →(C’, m’)
Simple Imperative Language Semantics
(C, m) → (C”, m’ )
(C; C’, m) → (C”; C’, m’)
(C, m) → m’
(C; C’, m) → (C’, m’ )
43
(C, m) → m’
Transition Semantics
Evaluate first command of sequence first …
Seq-L
Seq-R
(C, m) →(C’, m’)
Simple Imperative Language Semantics
(C, m) → (C”, m’ )
(C; C’, m) → (C”; C’, m’)
(C, m) → m’
(C; C’, m) → (C’, m’ )
44
(C, m) → m’
Transition Semantics
Evaluate first command of sequence first …
Seq-L
Seq-R
… until it gives back a memory
(C, m) →(C’, m’)
Simple Imperative Language Semantics
(C, m) → (C”, m’ )
(C; C’, m) → (C”; C’, m’)
(C, m) → m’
(C; C’, m) → (C’, m’ )
45
(C, m) → m’
Transition Semantics
Evaluate first command of sequence first …
Seq-L
Seq-R
… until it gives back a memory
(C, m) →(C’, m’)
Simple Imperative Language Semantics
(if true then C else C’ fi, m) → (C, m)
(if false then C else C’ fi, m) → (C’, m)
(B, m) → (B’, m)
(if B then C else C’ fi, m) → (if B’ then C else C’ fi, m)
46
(C, m) → m’
(C, m) →(C’, m’)
Transition Semantics
If-T
If-F
If
Simple Imperative Language Semantics
(if true then C else C’ fi, m) → (C, m)
(if false then C else C’ fi, m) → (C’, m)
(B, m) → (B’, m)
(if B then C else C’ fi, m) → (if B’ then C else C’ fi, m)
47
(C, m) → m’
(C, m) →(C’, m’)
Transition Semantics
If-T
If-F
If
If guard is true, evaluate first branch
Simple Imperative Language Semantics
(if true then C else C’ fi, m) → (C, m)
(if false then C else C’ fi, m) → (C’, m)
(B, m) → (B’, m)
(if B then C else C’ fi, m) → (if B’ then C else C’ fi, m)
48
(C, m) → m’
(C, m) →(C’, m’)
Transition Semantics
If-T
If-F
If
If guard is false, evaluate second branch
Simple Imperative Language Semantics
(if true then C else C’ fi, m) → (C, m)
(if false then C else C’ fi, m) → (C’, m)
(B, m) → (B’, m)
(if B then C else C’ fi, m) → (if B’ then C else C’ fi, m)
49
(C, m) → m’
(C, m) →(C’, m’)
Transition Semantics
If-T
If-F
If
Evaluate guard until it’s a value, so one of the above applies
50
Questions so far?
Transition Semantics
Simple Imperative Language Semantics
(B, m) 🡪 (B’, m)
??
(while B do C od, m) → ??(while B’ do C od, m)
51
(C, m) → m’
(C, m) →(C’, m’)
Transition Semantics
Question: What should while do?
Simple Imperative Language Semantics
(B, m) 🡪 (B’, m)
(B, m) → (B’, m)
(while B do C od, m) → (while B’ do C od, m)
(while B’ do C od, m)
52
(C, m) → m’
(C, m) →(C’, m’)
Transition Semantics
This is tempting, but it is wrong!
Simple Imperative Language Semantics
(B, m) 🡪 (B’, m)
(while B do C od, m) →� (if B then C; while B do C od else skip fi, m)
(while B’ do C od, m)
53
(C, m) → m’
(C, m) →(C’, m’)
Transition Semantics
Rather, we unroll the loop once
Simple Imperative Language Semantics
(B, m) 🡪 (B’, m)
(while B do C od, m) →� (if B then C; while B do C od else skip fi, m)
(while B’ do C od, m)
54
(C, m) → m’
(C, m) →(C’, m’)
Transition Semantics
Rather, we unroll the loop once …
Simple Imperative Language Semantics
(B, m) 🡪 (B’, m)
(while B do C od, m) →� (if B then C; while B do C od else skip fi, m)
(while B’ do C od, m)
55
(C, m) → m’
(C, m) →(C’, m’)
Transition Semantics
… continue while guard is true …
Simple Imperative Language Semantics
(B, m) 🡪 (B’, m)
(while B do C od, m) →� (if B then C; while B do C od else skip fi, m)
(while B’ do C od, m)
56
(C, m) → m’
(C, m) →(C’, m’)
Transition Semantics
… and stop when guard is false.
57
Questions so far?
58
Example
Example Evaluation
First step:
(if x > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) → ??
59
Transition Semantics Example
Example Evaluation
First step:
(if x > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →� ??
60
Transition Semantics Example
If
Evaluate guard until it’s a value
Example Evaluation
First step:
(x > 5, {x -> 7}) → ??
(if x > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →� ??
61
Transition Semantics Example
If
Evaluate guard until it’s a value
Example Evaluation
First step:
(x > 5, {x -> 7}) → ??
(if x > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →� ??
62
Transition Semantics Example
If
How to evaluate guard?
Example Evaluation
First step:
??
(x > 5, {x -> 7}) → ??
(if x > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →� ??
63
Transition Semantics Example
If
The guard is a relation
Rel-E
Example Evaluation
First step:
(x, {x -> 7}) → ??
(x > 5, {x -> 7}) → ??
(if x > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →� ??
64
Transition Semantics Example
If
And x is not yet a value, so we evaluate LHS first
Rel-E
Example Evaluation
First step:
(x, {x -> 7}) → ??
(x > 5, {x -> 7}) → ??
(if x > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →� ??
65
Transition Semantics Example
If
x is an identifier …
Rel-E
Id
Example Evaluation
First step:
(x, {x -> 7}) → (7, {x -> 7})
(x > 5, {x -> 7}) → ??
(if x > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →� ??
66
Transition Semantics Example
If
x is an identifier so we look it up
Id
Rel-E
Example Evaluation
First step:
(x, {x -> 7}) → (7, {x -> 7})
(x > 5, {x -> 7}) → (7 > 5, {x -> 7})
(if x > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →� ??
67
Transition Semantics Example
If
Propagate downward
Id
Rel-E
Example Evaluation
First step:
(x, {x -> 7}) → (7, {x -> 7})
(x > 5, {x -> 7}) → (7 > 5, {x -> 7})
(if x > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) → �(if 7 > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7})
68
Transition Semantics Example
If
Propagate downward
Id
Rel-E
69
Questions so far?
Transition Semantics Example
Example Evaluation
First step:
(x, {x -> 7}) → (7, {x -> 7})
(x > 5, {x -> 7}) → (7 > 5, {x -> 7})
(if x > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) → �(if 7 > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7})
70
Transition Semantics Example
If
Id
Rel-E
Example Evaluation
Second step:
(if 7 > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) → � ??
71
Transition Semantics Example
Example Evaluation
Second step:
(7 > 5, {x -> 7}) → ??
(if 7 > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) → � ??
72
Transition Semantics Example
Evaluate guard until it’s a value (still not a value)
If
Example Evaluation
Second step:
(7 > 5, {x -> 7}) → (true, {x -> 7})
(if 7 > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) → � ??
73
Transition Semantics Example
By semantics of >
If
Evaluate guard until it’s a value (still not a value)
Example Evaluation
Second step:
(7 > 5, {x -> 7}) → (true, {x -> 7})
(if 7 > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) → �(if true then y := 2 + 3 else y := 3 + 4 fi, {x -> 7})
74
Transition Semantics Example
By semantics of >
If
Evaluate guard until it’s a value (still not a value)
Example Evaluation
Second step:
(7 > 5, {x -> 7}) → (true, {x -> 7})
(if 7 > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) → �(if true then y := 2 + 3 else y := 3 + 4 fi, {x -> 7})
75
Transition Semantics Example
By semantics of >
If
Evaluate guard until it’s a value (now it’s a value!)
Example Evaluation
Second step:
(7 > 5, {x -> 7}) → (true, {x -> 7})
(if 7 > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) → �(if true then y := 2 + 3 else y := 3 + 4 fi, {x -> 7})
Third step:
(if true then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) → � ??
76
Transition Semantics Example
If
If-T
Example Evaluation
Second step:
(7 > 5, {x -> 7}) → (true, {x -> 7})
(if 7 > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) → �(if true then y := 2 + 3 else y := 3 + 4 fi, {x -> 7})
Third step:
(if true then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) → � (y := 2 + 3, {x -> 7})
77
Transition Semantics Example
If
If-T
Step to then case
Example Evaluation
Fourth Step:
(2 + 3, {x -> 7}) → (5, {x -> 7})
(y := 2 + 3, {x -> 7}) → (y := 5, {x -> 7})
78
Transition Semantics Example
Assign-E
Example Evaluation
Fourth Step:
(2 + 3, {x -> 7}) → (5, {x -> 7})
(y := 2 + 3, {x -> 7}) → (y := 5, {x -> 7})
Fifth Step:
(y := 5, {x -> 7}) → {y -> 5, x -> 7}
79
Transition Semantics Example
Assign-E
Assign-V
Example Evaluation
Bottom line:
(if x > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →
(if 7 > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →
(if true then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →
(y := 2 + 3, {x -> 7}) →
(y := 5, {x -> 7}) →
{y -> 5, x -> 7}
80
Transition Semantics Example
Example Evaluation
Bottom line:
(if x > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →
(if 7 > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →
(if true then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →
(y := 2 + 3, {x -> 7}) →
(y := 5, {x -> 7}) →
{y -> 5, x -> 7}
81
Transition Semantics Example
Example Evaluation
Bottom line:
(if x > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →
(if 7 > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →
(if true then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →
(y := 2 + 3, {x -> 7}) →
(y := 5, {x -> 7}) →
{y -> 5, x -> 7}
82
Transition Semantics Example
Example Evaluation
Bottom line:
(if x > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →
(if 7 > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →
(if true then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →
(y := 2 + 3, {x -> 7}) →
(y := 5, {x -> 7}) →
{y -> 5, x -> 7}
83
Transition Semantics Example
Example Evaluation
Bottom line:
(if x > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →
(if 7 > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →
(if true then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →
(y := 2 + 3, {x -> 7}) →
(y := 5, {x -> 7}) →
{y -> 5, x -> 7}
84
Transition Semantics Example
Example Evaluation
Bottom line:
(if x > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →
(if 7 > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →
(if true then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →
(y := 2 + 3, {x -> 7}) →
(y := 5, {x -> 7}) →
{y -> 5, x -> 7}
(if x > 5 then y := 2 + 3 else y := 3 + 4 fi, {x -> 7}) →* {y -> 5, x -> 7}
85
Transition Semantics Example
Transition Semantics Evaluation
(C1, m1)→(C2, m2) → (C3, m3) → … → m
86
Transition Semantics Example
87
Questions?
88
Next Class: Lambda Calculus
Next Class
89
Operational
Semantics
Lambda Calculus
Axiomatic Semantics
III : Language Semantics
Lamda Calculus
Next Class
90