Title: Operational Semantics
 1Operational Semantics
- Semantics with Applications 
- Chapter 2 
- H. Nielson and F. Nielsonhttp//www.daimi.au.dk/
 bra8130/Wiley_book/wiley.html
2Outline
- Natural Semantics of IMP 
- Properties of the Natural Semantics 
- Structural Operational Semantics for IMP 
- Equivalence Result 
- Extensions to IMP 
- Abort 
- Non determinism 
- Parallel constructs 
- Blocks and procedures
3Assignment Rule
n 
YX 
?n/X(Y)
?(Y) 
Y?X 
X?1, Y ?2, Z?15/X X?5, Y ?2, Z?1 
 4Natural Semantics (IMP)
ltskip, ? gt ? ? 
 5Natural Semantics (IMP) 
 6Semantic Equivalence
- com1 and com2 are semantically equivalent if for 
 all ? and ?ltcom1, ? gt ? ? if and only if
 ltcom2, ? gt ? ?
- Simple examplewhile b do comis semantically 
 equivalent toif b then (com  while b do com)
 else skip
7Properties of Natural Semantics
- Equivalence of program constructs 
- skip  com is semantically equivalent to com 
- com skip is semantically equivalent to com 
- ((com1  com2)  com3) is semantically 
 equivalent to (com1 ( com2  com3))
- (X  5  Y  X  8) is semantically 
 equivalent to(X 5 Y  40)
-  Deterministic 
- If ltcom, ?gt ? ?1 and ltcom, ? gt ? ?2 then ?1 ?2 
8Deterministic Semantics for IMP
-  If ltcom, ?gt ? ? 1 and ltcom, ? gt ? ? 2then ?1?2 
- The proof uses induction on the shape of 
 derivation trees
- Prove that the property holds for all simple 
 derivation trees by showing it holds for axioms
- Prove that the property holds for all composite 
 trees
- For each rule assume that the property holds for 
 its premises (induction hypothesis) and prove it
 holds for the conclusion of the rule
9The Semantic Function Sns
- The meaning of a command com is defined as a 
 partial function from State to State
- Sns Com ? (State ? State) 
- Sns ?com?(?)  ? if ltcom, ?gt ?? and otherwise 
 Sns ?com? (?) is undefined
- Examples 
- Sns ?skip?(?) ? 
- Sns ?X1?(?)  ? 1/X 
- Sns ?while true do skip?(?)  undefined 
10Structural Operational Semantics
- Emphasizes the individual steps 
- Usually more suitable for static analysis 
- For every command S, write meaning rules ltcom, ?gt 
 ? ?If the first step of executing the command
 com on ? leads to ?
- Two possibilities for ? 
- ?  ltcom, ?gt 
- The execution of com is not completed, com is 
 the remaining computation to be performed on ?
- ?  ? 
- The execution of com has terminated with a final 
 state ?
- ? is a stuck configuration when there are no 
 transitions
- The meaning of a program P on an input state s is 
 the set of final states that can be executed in
 arbitrary finite steps
11SOS (IMP)
ltskip, ? gt ? ? 
 12SOS (IMP) 
 13SOS (IMP)
 ltwhile b do com, ?gt ? ltif b then (comwhile b 
do com) else skip, ?gt 
 14Derivation Sequences 
- A finite derivation sequence starting at ltcom, 
 ?gt?0, ?1, ?2 , ?k such that
- ?0ltcom, ?gt 
- ?i ? ?i1 
- ?k is either stuck configuration or a final state 
- An infinite derivation sequence starting at 
 ltcom, ?gt?0, ?1, ?2  such that
- ?0ltcom, ?gt 
- ?i ? ?i1 
- ?0 ?i ?i in i steps 
- ?0 ? ?i in finite number of steps 
- For each step there is a derivation tree
15Example
- Let ?0 such that ?0 (X)  5 and  ?0 (Y)  7 
- com  (ZX X  Y) Y  Z
16Factorial Program
- Input state s such that ?(X)  3 
-  
Y  1 while ?(X1) do Y  Y  X X  X - 1 
 17Program Termination
- Given a command com and input ? 
- com terminates on ? if there exists a finite 
 derivation sequence starting at ltcom, ? gt
- com terminates successfully on ? if there exists 
 a finite derivation sequence starting at ltcom, ?gt
 leading to a final state
- com loops on ? if there exists an infinite 
 derivation sequence starting at ltcom, ?gt
18Properties of the Semantics
- com1 and com2 are semantically equivalent if 
-  for all ? and ?ltcom1, ? gt ? ? if and only if 
 ltcom2, ? gt ? ?
- there is an infinite derivation sequence starting 
 at ltcom1, ? gt if and only if there is an
 infinite derivation sequence starting at ltcom2, ?
 gt
- Deterministic 
- If ltcom, ? gt ? ?1 and ltcom, ?gt ? ? 2 then ?1 ?2
19Sequential Composition
-  If ltcom1 com2, ? gt ?k ? then there exists a 
 state ? and numbers k1 and k2 such that
- ltcom1, ? gt ?k1 ? 
- ltcom2, ?gt ?k2 ? 
- and k  k1  k2 
- The proof uses induction on the length of 
 derivation sequences
- Prove that the property holds for all derivation 
 sequences of length 0
- Prove that the property holds for all other 
 derivation sequences
- Show that the property holds for sequences of 
 length k1 using the fact it holds on all
 sequences of length k (induction hypothesis)
20The Semantic Function Ssos
- The meaning of a command com is defined as a 
 partial function from State to State
- Ssos Com ? (State ? State) 
- Ssos?com??  ? if ltcom, ?gt ?? and otherwise 
 Ssos ?com?s is undefined
21An Equivalence Result
- For every command com of the IMP language 
- Snat?com?  Ssos?com?
22Extensions to IMP
- Abort command (like C exit) 
- Non determinism 
- Parallelism 
- Local Variables 
- Procedures 
- Static Scope 
- Dynamic scope
23IMP Abort
- Abstract syntaxcom X  a  skip  com1  
 com2   if b then com1 else com2
 while b do com abort
- Abort terminates the execution 
- No new rules are needed in natural and 
 structural operational semantics
- commands 
- skip 
- abort 
- while true do skip 
24Conclusion
- The natural semantics cannot distinguish between 
 looping and abnormal termination (unless the
 states are modified)
- In the structural operational semantics looping 
 is reflected by infinite derivations and abnormal
 termination is reflected by stuck configuration
25IMP Non-Determinism
- Abstract syntaxcom X  a  skip  com1  
 com2  if b then com1 else
 com2  while b do com
 com1 or com2
- Either com1 or com2 is executed 
- Example 
- X  1 or (X 2  X  X2) 
26IMPNon-DeterminismNatural Semantics 
 27IMP Non-DeterminismSOS 
 28IMP Non-DeterminismExamples
-  X  1 or (X 2  X  X2) 
- (while true do skip) or (X 2  X  X2)
29Conclusion
- In the natural semantics non-determinism will 
 suppress looping if possible (mnemonic)
- In the structural operational semantics 
 non-determinism does not suppress looping
30IMP Parallel Constructs
- Abstract syntaxcom X  a  skip  com1  
 com2  if b then com1 else com2
 while b do com  com1 par com2
- All the interleavings of com1 or com2 are 
 executed
- Example 
- X  1 par (X 2  X  X2) 
31IMP Parallel ConstructsSOS 
 32IMP Parallel ConstructsNatural Semantics 
 33Conclusion
- In the natural semantics immediate constituent is 
 an atomic entity so we cannot express
 interleaving of computations
- In the structural operational semantics we 
 concentrate on small steps so interleaving of
 computations can be easily expressed
34IMP  local variables
- Abstract syntaxcom X  a  skip  com1  
 com2  if b then com1 else com2
 while b do com  begin Vars
 com endVars  var X  a  Vars  ?
35Example
begin var Y  1 X  1 begin 
 var X 2 Y  X  1 
end X  Y  X end 
 36Conclusions Local Variables
- The natural semantics can remember local states 
- Need to introduce stack or heap into state of the 
 structural semantics
37IMP  local variables and procedures
- Abstract syntaxcom X  a  skip  com1  
 com2  if b then com1 else com2
 while b do com  begin Vars
 Procs com end  call pVars  var X  a  Vars
 ?Procs  proc p is com  Procs  ?
38Conclusions
- Structural operational semantics allows us to 
 simulate low level computations without getting
 bugged into too many details
- Natural semantics allows to abstract more 
- Local memory 
- Non termination