27
Formal Semantics of Programming Languages 虞虞虞 [email protected] Topic 2: Operational Semantics

Formal Semantics of Programming Language s

  • Upload
    signa

  • View
    32

  • Download
    0

Embed Size (px)

DESCRIPTION

Formal Semantics of Programming Language s. Topic 2: Operational Semantics. 虞慧群 [email protected]. IMP: A Simple Imperative Language. numbers N Positive and negative numbers n, m  N truth values T={true, false} locations Loc X, Y  Loc arithmetic Aexp a  Aexp - PowerPoint PPT Presentation

Citation preview

Page 1: Formal  Semantics  of  Programming Language s

Formal Semantics of Programming Languages

虞慧群[email protected]

Topic 2: Operational Semantics

Page 2: Formal  Semantics  of  Programming Language s

IMP: A Simple Imperative Language

numbers N Positive and negative numbers n, m N

truth values T={true, false} locations Loc

X, Y Loc arithmetic Aexp

a Aexp boolean expressions Bexp

b Bexp commands Com

c Com

Page 3: Formal  Semantics  of  Programming Language s

(3+5 ) 3 + 5

3 + 5 5+ 3

Abstract Syntax for IMP Aexp

a ::= n | X | a0 + a1 | a0 – a1 | a0 a1

Bexp b ::= true | false | a0 = a1 | a0 a1 | b | b0 b1

| b0 b1

Com c ::= skip | X := a | c0 ; c1 | if b then c0 else c1

| while b do c

2+34-5

(2+(34))-5 ((2+3)4))-5

Page 4: Formal  Semantics  of  Programming Language s

Example Program

Y := 1;

while (X=1) do

Y := Y * X;

X := X - 1

Page 5: Formal  Semantics  of  Programming Language s

But what about semantics

Page 6: Formal  Semantics  of  Programming Language s

Expression Evaluation States

Mapping locations to values - The set of states

: Loc N (X)= X=value of X in = [ X 5, Y 7]

The value of X is 5 The value of Y is 7 The value of Z is undefined

For a Exp, , n N, <a, > n

a is evaluated in to n

Page 7: Formal  Semantics  of  Programming Language s

Evaluating (a0 + a1) at

Evaluate a0 to get a number n0 at

Evaluate a1 to get a number n1 at Add n0 and n1

Page 8: Formal  Semantics  of  Programming Language s

Expression Evaluation Rules Numbers

<n, > n Locations

<X, > (X) Sums

Subtractions

Products

10,10

1,1,0,0nnnwhere

naa

nana

10,10

1,1,0,0nnnwhere

naa

nana

10,10

1,1,0,0nnnwhere

naa

nana

Axiom

s

Page 9: Formal  Semantics  of  Programming Language s

Derivations

A rule instance Instantiating meta variables with

corresponding values

632

3322

0,

0,,0,

1232

4332

0,

0,,0,

Page 10: Formal  Semantics  of  Programming Language s

Derivation (Tree)

Axioms in the leafs Rule instances at internal nodes

0Init 0 , 55 0 , 77 0 , 99 0 ,

55)Init( 0 , 1697 0 ,

219)(75)Init( 0 ,

Page 11: Formal  Semantics  of  Programming Language s

Computing a derivation

We write <a, > n when there exists a derivation tree whose root is <a, > n

Can be computed in a top-down manner At every node try all derivations “in parallel”

0Init 0 , 55 0 , 77 0 , 99 0 ,

?5)Init( 0 , ?97 0 ,

?9)(75)Init( 0 ,

5 16

21

Page 12: Formal  Semantics  of  Programming Language s

Recap

Operational Semantics The rules can be implemented easily Define interpreter

Structural Operational Semantics Syntax directed

Natural semantics

Page 13: Formal  Semantics  of  Programming Language s

Equivalence of IMP expressions

a0 a1 iff

nanaNn ,,. 10

Page 14: Formal  Semantics  of  Programming Language s

Boolean Expression Evaluation Rules <true, > true <false, > false

mnifaa

mana

true

,10

,1,,0

mnifaa

mana

false

,10

,1,,0

mnifaa

mana

true,10

,1,,0

mnnotifaa

mana

true

,10

,1,,0

Page 15: Formal  Semantics  of  Programming Language s

Boolean Expression Evaluation Rules(cont)

otherwisetand

whentwhere 10

,10

,1,0,0 1

false

truetrue

tt

tbb

tbtb

false

true

,

,

b

b

true

false

,

,

b

b

otherwisetand

whentwhere 10

,10

,1,0,0 1

true

falsefalse

tt

tbb

tbtb

Page 16: Formal  Semantics  of  Programming Language s

Equivalence of Boolean expressions

b0 b1 iff

tbtbTt ,,. 10

Page 17: Formal  Semantics  of  Programming Language s

Extensions

Shortcut evaluation of Boolean expressions “Parallell” evaluation of Boolean expressions Other data types

Page 18: Formal  Semantics  of  Programming Language s

The execution of commands <c, > ’

c terminates on in a final state ’

Initial state 0

0(X)=0 for all X

Handling assignments <X:=5, > ’

A notation:

XY

XYmYXm

if(Y)

if{)](/[

<X:=5, > [5/X]

Page 19: Formal  Semantics  of  Programming Language s

Rules for commands

<skip, >

Sequencing:

Conditionals:

]/[: ,

,

XmaX

na

'

'

,10

,0,

ccb

cb

elsethenif

true

Atom

ic com

mands

'

'

,10

,1,

ccb

cb

elsethenif

false

'

'''1''1

,10

,,

cc

cc

;

Page 20: Formal  Semantics  of  Programming Language s

Rules for commands (while)

,

,

cb

b

dowhile

false

'

'''

,

,,

cb

cbb

dowhile

dowhile '' c, true

Page 21: Formal  Semantics  of  Programming Language s

Example Program

Y := 1;

while (X=1) do

Y := Y * X;

X := X - 1

Page 22: Formal  Semantics  of  Programming Language s

Equivalence of commands

c0 c1 iff

',',.', 10 cc

Page 23: Formal  Semantics  of  Programming Language s

Proposition 2.8

while b do c if then (c; while b do c) else skip

Page 24: Formal  Semantics  of  Programming Language s

Small Step Operational Semantics

The natural semantics define evaluation in large steps Abstracts “computation time”

It is possible to define a small step operational semantics <a, > 1 <a’, ’>

“one” step of executing a in a state yields a’ in a state ’

Page 25: Formal  Semantics  of  Programming Language s

Small Step Semantics for Additions

,101,10

,01,0

'

'

aaaa

aa

,11,1

,11,1

'

'

anan

aa

mnpwherepmn

,1,

Page 26: Formal  Semantics  of  Programming Language s

Summary

Operational semantics enables to naturally express program behavior

Can handle Non determinism Concurrency Procedures Object oriented Pointers and dynamically allocated structures

But remains very closed to the implementation Two programs which compute the same

functions are not necessarily equivalent

Page 27: Formal  Semantics  of  Programming Language s

Exercise 2

(1) Evaluate a (X4) + 3 in a state s.t. (X)=0.

(2) Write down rules which express the “parallel” evaluation of b0 and b1 in b0 V b1 so that evaluates to true if either b0 evaluates to true, and b1 is unevaluated, or b1 evaluates to true, and b0 is unevaluated.