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

Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

  • View
    258

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Operational Semantics

Semantics with ApplicationsChapter 2

H. Nielson and F. Nielsonhttp://www.daimi.au.dk/~bra8130/Wiley_book/wiley.html

Page 2: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Outline 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

Page 3: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Assignment Rule

[n/X](Y)=

n Y=X

YX (Y)

[X1, Y 2, Z1][5/X]= [X5, Y 2, Z1]

Page 4: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Natural Semantics (IMP)

<com1 , > ’, <com2, ’> ’’

<com1; com2, > ’’

<b, > true, <com1 , > ’

<if b then com1 else com2, > ’

<skip, > <X := a, > [m/X]

<a, > m

<b, > false, <com2 , > ’

<if b then com1 else com2, > ’

Page 5: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Natural Semantics (IMP)

<b, > true, <com , > ’, <while b do com, ’> ’’

<while b do com, > ’’

<b, > false

<while b do com, >

Page 6: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Semantic Equivalence

com1 and com2 are semantically equivalent if for all and ’<com1, > ’ if and only if <com2, > ’

Simple example“while b do com”is semantically equivalent to:“if b then (com ; while b do com) else skip”

Page 7: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Properties 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 <com, > 1 and <com, > 2 then 1= 2

Page 8: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Deterministic Semantics for IMP

If <com, > 1 and <com, > 2

then 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

Page 9: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

The 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 <com, > ’ and otherwise Sns com () is undefined

Examples– Sns skip() =

– Sns X:=1() = [1/X]

– Sns while true do skip() = undefined

Page 10: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Structural Operational Semantics Emphasizes the individual steps Usually more suitable for static analysis For every command S, write meaning rules <com, >

“If the first step of executing the command com on leads to ”

Two possibilities for = <com’, ’>

» 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

Page 11: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

SOS (IMP)

<skip, > <X := a, > [m/X]

<a, > m

<com1 , > ’

<com1; com2, > <com2, ’>

<com1 , > <com1’, ’>

<com1; com2, > <com1’, com2, ’>

Page 12: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

SOS (IMP)

<b, > true

<if b then com1 else com2, > <com1, >

<b, > false

<if b then com1 else com2, > <com2, >

Page 13: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

SOS (IMP)

<while b do com, > <if b then (com;while b do com) else skip, >

Page 14: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Derivation Sequences A finite derivation sequence starting at <com, >

0, 1, 2 …, k such that

0=<com, >

i i+1

k is either stuck configuration or a final state

An infinite derivation sequence starting at <com, >0, 1, 2 … such that

0=<com, >

i i+1

0 i i in i steps

0 * i in finite number of steps

For each step there is a derivation tree

Page 15: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Example

Let 0 such that 0 (X) = 5 and 0 (Y) = 7

com = (Z:=X; X := Y); Y := Z

Page 16: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Factorial Program

Input state s such that (X) = 3

Y := 1; while (X=1) do Y := Y * X; X := X - 1

Page 17: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Program Termination

Given a command com and input – com terminates on if there exists a finite derivation

sequence starting at <com, >

– com terminates successfully on if there exists a finite derivation sequence starting at <com, > leading to a final state

– com loops on if there exists an infinite derivation sequence starting at <com, >

Page 18: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Properties of the Semantics com1 and com2 are semantically equivalent if:

– for all and ’<com1, > * ’ if and only if <com2, > * ’

– there is an infinite derivation sequence starting at <com1, > if and only if there is an infinite derivation sequence starting at <com2, >

Deterministic– If <com, > * 1 and <com, > * 2 then 1= 2

Page 19: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Sequential Composition If <com1; com2, > k ’’ then there exists a

state ’ and numbers k1 and k2 such that

– <com1, > k1 ’

– <com2, ’> 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 k+1 using the fact it holds on all sequences of length k (induction hypothesis)

Page 20: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

The Semantic Function Ssos

The meaning of a command com is defined as a partial function from State to State

Ssos: Com (State State)

Ssoscom = ’ if <com, > *’ and otherwise Ssos coms is undefined

Page 21: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

An Equivalence Result

For every command com of the IMP language– Snatcom = Ssoscom

Page 22: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Extensions to IMP

Abort command (like C exit) Non determinism Parallelism Local Variables Procedures

– Static Scope

– Dynamic scope

Page 23: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

IMP+ 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

Page 24: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Conclusion

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

Page 25: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

IMP+ 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 := X+2)

Page 26: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

<com1 , > ’

<com1 or com2, > ’

IMP+Non-DeterminismNatural Semantics

<com2 , > ’

<com1 or com2, > ’

Page 27: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

IMP+ Non-DeterminismSOS

Page 28: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

IMP +Non-DeterminismExamples

X := 1 or (X :=2 ; X := X+2) (while true do skip) or (X :=2 ; X := X+2)

Page 29: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Conclusion

In the natural semantics non-determinism will suppress looping if possible (mnemonic)

In the structural operational semantics non-determinism does not suppress looping

Page 30: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

IMP+ 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 := X+2)

Page 31: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

IMP+ Parallel ConstructsSOS

Page 32: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

IMP+ Parallel ConstructsNatural Semantics

Page 33: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Conclusion

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

Page 34: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

IMP + 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 |

Page 35: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Examplebegin

var Y := 1;

X := 1;

begin

var X :=2;

Y := X + 1

end;

X := Y + X

end

Page 36: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Conclusions Local Variables

The natural semantics can “remember” local states Need to introduce stack or heap into state of the

structural semantics

Page 37: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

IMP + 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 |

Page 38: Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson bra8130/Wiley_book/wiley.html

Conclusions

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