Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson

Similar presentations


Presentation on theme: "Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson"— Presentation transcript:

1 Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson http://www.daimi.au.dk/~bra8130/Wiley_book/wiley.html

2 Outline u Natural Semantics of IMP u Properties of the Natural Semantics u Structural Operational Semantics for IMP u Equivalence Result u Extensions to IMP –Abort –Non determinism –Parallel constructs –Blocks and procedures

3 Assignment Rule  [n/X](Y)= n Y=X YXYX  (Y) [X  1, Y  2, Z  1][5/X]= [X  5, Y  2, Z  1]

4 Natural Semantics (IMP)   ’,   ’’   ’’  true,   ’   ’     [m/X]  m  false,   ’   ’

5 Natural Semantics (IMP)  true,   ’,   ’’   ’’  false  

6 Semantic Equivalence u com 1 and com 2 are semantically equivalent if for all  and  ’   ’ if and only if   ’ u Simple example “while b do com” is semantically equivalent to: “if b then (com ; while b do com) else skip”

7 Properties of Natural Semantics u Equivalence of program constructs –“skip ; com” is semantically equivalent to “com” –“com; skip” is semantically equivalent to “com” –“((com 1 ; com 2 ) ; com 3 )” is semantically equivalent to “(com 1 ;( com 2 ; com 3 ))” –“(X := 5 ; Y := X * 8)” is semantically equivalent to “(X :=5; Y := 40)” u Deterministic –If   1 and   2 then  1 =  2

8 Deterministic Semantics for IMP u If   1 and   2 then  1 =  2 u 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

9 The Semantic Function S ns u The meaning of a command com is defined as a partial function from State to State u S ns : Com  (State  State) u S ns  com  (  ) =  ’ if  ’ and otherwise S ns  com  (  ) is undefined u Examples –S ns  skip  (  ) =  –S ns  X:=1  (  ) =  [1/X] –S ns  while true do skip  (  ) = undefined

10 Structural Operational Semantics u Emphasizes the individual steps u Usually more suitable for static analysis u For every command S, write meaning rules   “If the first step of executing the command com on  leads to  ” u Two possibilities for  –  = »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 u 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

11 SOS (IMP)     [m/X]  m   ’ 

12 SOS (IMP)  true   false 

13 SOS (IMP) 

14 Derivation Sequences u A finite derivation sequence starting at  0,  1,  2 …,  k such that –  0 = –  i   i+1 –  k is either stuck configuration or a final state u An infinite derivation sequence starting at  0,  1,  2 … such that –  0 = –  i   i+1 u  0  i  i in i steps u  0  *  i in finite number of steps u For each step there is a derivation tree

15 Example u Let  0 such that  0 (X) = 5 and  0 (Y) = 7 u com = (Z:=X; X := Y); Y := Z

16 Factorial Program Input state s such that  (X) = 3 Y := 1; while  (X=1) do Y := Y * X; X := X - 1

17 Program Termination u Given a command com and input  –com terminates on  if there exists a finite derivation sequence starting at –com terminates successfully on  if there exists a finite derivation sequence starting at leading to a final state –com loops on  if there exists an infinite derivation sequence starting at

18 Properties of the Semantics u com 1 and com 2 are semantically equivalent if: – for all  and  ’  *  ’ if and only if  *  ’ –there is an infinite derivation sequence starting at if and only if there is an infinite derivation sequence starting at u Deterministic –If  *  1 and  *  2 then  1 =  2

19 Sequential Composition u If  k  ’’ then there exists a state  ’ and numbers k 1 and k 2 such that –  k1  ’ –  k2  ’’ –and k = k 1 + k 2 u 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 k+1 using the fact it holds on all sequences of length k (induction hypothesis)

20 The Semantic Function S sos u The meaning of a command com is defined as a partial function from State to State u S sos : Com  (State  State) u S sos  com   =  ’ if  *  ’ and otherwise S sos  com  s is undefined

21 An Equivalence Result u For every command com of the IMP language –S nat  com  = S sos  com 

22 Extensions to IMP u Abort command (like C exit) u Non determinism u Parallelism u Local Variables u Procedures –Static Scope –Dynamic scope

23 IMP+ Abort u Abstract syntax com::= X := a | skip | com 1 ; com 2 | if b then com 1 else com 2 | while b do com| abort u Abort terminates the execution u No new rules are needed in natural and structural operational semantics u commands –skip –abort –while true do skip

24 Conclusion u The natural semantics cannot distinguish between looping and abnormal termination (unless the states are modified) u In the structural operational semantics looping is reflected by infinite derivations and abnormal termination is reflected by stuck configuration

25 IMP+ Non-Determinism u Abstract syntax com::= X := a | skip | com 1 ; com 2 | if b then com 1 else com 2 | while b do com| com 1 or com 2 u Either com 1 or com 2 is executed u Example –X := 1 or (X :=2 ; X := X+2)

26   ’ IMP+Non-Determinism Natural Semantics   ’

27 IMP+ Non-Determinism SOS

28 IMP +Non-Determinism Examples u X := 1 or (X :=2 ; X := X+2) u (while true do skip) or (X :=2 ; X := X+2)

29 Conclusion u In the natural semantics non-determinism will suppress looping if possible (mnemonic) u In the structural operational semantics non- determinism does not suppress looping

30 IMP+ Parallel Constructs u Abstract syntax com::= X := a | skip | com 1 ; com 2 | if b then com 1 else com 2 | while b do com| com 1 par com 2 u All the interleavings of com 1 or com 2 are executed u Example –X := 1 par (X :=2 ; X := X+2)

31 IMP+ Parallel Constructs SOS

32 IMP+ Parallel Constructs Natural Semantics

33 Conclusion u In the natural semantics immediate constituent is an atomic entity so we cannot express interleaving of computations u In the structural operational semantics we concentrate on small steps so interleaving of computations can be easily expressed

34 IMP + local variables u Abstract syntax com::= X := a | skip | com 1 ; com 2 | if b then com 1 else com 2 | while b do com| begin Vars com end Vars ::= var X := a ; Vars | 

35 Example begin var Y := 1; X := 1; begin var X :=2; Y := X + 1 end; X := Y + X end

36 Conclusions Local Variables u The natural semantics can “remember” local states u Need to introduce stack or heap into state of the structural semantics

37 IMP + local variables and procedures u Abstract syntax com::= X := a | skip | com 1 ; com 2 | if b then com 1 else com 2 | while b do com| begin Vars Procs com end | call p Vars ::= var X := a ; Vars |  Procs ::= proc p is com ; Procs | 

38 Conclusions u Structural operational semantics allows us to simulate low level computations without getting bugged into too many details u Natural semantics allows to abstract more –Local memory –Non termination


Download ppt "Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson"

Similar presentations


Ads by Google