Formal Semantics of Programming Languages

Download Report

Transcript Formal Semantics of Programming Languages

Formal Semantics of Programming Languages
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
boolean expressions Bexp
 b  Bexp
commands Com
 c  Com
Abstract Syntax for IMP

Aexp


Bexp


a ::= n | X | a0 + a1 | a0 – a1 | a0  a1
b ::= true | false | a0 = a1 | a0  a1 | b | b0 b1
| b 0  b1
Com

c ::= skip | X := a | c0 ; c1 | if b then c0 else c1
| while b do c
2+34-5
(2+(34))-5
((2+3)4))-5
(3+5)  3 + 5
3 + 5  5+ 3
Example Program
Y := 1;
while (X=1) do
Y := Y * X;
X := X - 1
But what about semantics
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
Evaluating (a0 + a1) at 



Evaluate a0 to get a number n0 at 
Evaluate a1 to get a number n1 at 
Add n0 and n1
Expression Evaluation Rules

Numbers
Axioms


<n, >  n
Locations

<X, > (X)
 a0,   n0,  a1,   n1
where n  n0  n1
 a0  a1,   n

Sums

Subtractions

Products
 a0,   n0,  a1,   n1
where
 a0  a1,   n
 a0,   n0,  a1,   n1
where
 a0  a1,   n
n  n0  n1
n  n0  n1
Derivations

A rule instance

Instantiating meta variables with
corresponding values
 2,  0  2,  3,  0  3
 2  3,  0  6
 2,  0  3,  3,  0  4
 2  3,  0  12
Derivation (Tree)


Axioms in the leafs
Rule instances at internal nodes
 Init , 0  0  5, 0  5
 (Init  5) , 0  5
 7 , 0  7  9, 0  9
 7  9, 0  16
 (Init  5)  (7  9) , 0  21
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”
 Init , 0  0  5, 0  5
 (Init  5) , 0  ?5
 7 , 0  7
 9, 0  9
 7  9, 0  ?16
 (Init  5)  (7  9) , 0  ?21
Recap

Operational Semantics



Structural Operational Semantics


The rules can be implemented easily
Define interpreter
Syntax directed
Natural semantics
Equivalence of IMP
expressions
a0  a1
iff
n  N  .  a0,   n  a1,   n
Boolean Expression Evaluation Rules
 <true, >  true
 <false, >  false

 a0,   n,  a1,   m
if
 a0  a1,   true

 a0,   n,  a1,   m
if
 a0  a1,   false

 a0,   n,  a1,   m
if
 a0  a1,   true

 a0,   n,  a1,   m
if
 a0  a1,   true
nm
nm
nm
not n  m
Boolean Expression Evaluation Rules(cont)

 b,   true
 b,   false

 b,   false
 b,   true

 b0,   t 0,  b1,   t 1 where t  true when t 0  t1  true
and t  false otherwise
 b0  b1,   t

 b0,   t 0,  b1,   t 1
 b0  b1,   t
where
t  false when t 0  t1  false
and t  true otherwise
Equivalence of Boolean
expressions
b0 b1
iff
t T .  b0,   t  b1,   t
Extensions



Shortcut evaluation of Boolean expressions
“Parallell” evaluation of Boolean expressions
Other data types
The execution of commands

<c, >  ’


Initial state 0


c terminates on  in a final state ’
0(X)=0 for all X
Handling assignments <X:=5, >  ’
m
if Y  X
A notation:
{ (Y)
 [m / X ](Y ) 

<X:=5, >  [5/X]
if Y  X
Rules for commands
Atomic
commands

<skip, >  
 a,   n
 X : a,    [m / X ]
 c1,    ' '  c1,  ' '   '
 Sequencing:
 c0 ; c1,    '

Conditionals:
 b,   true  c0,    '
 if b then c0 else c1,    '
 b,   false  c1,    '
 if b then c0 else c1,    '
Rules for commands (while)
 b,   false
 while b do c ,   
 b,   true  c,     ' '  while b do c ,  ' '   '
 while b do c ,    '
Example Program
Y := 1;
while (X=1) do
Y := Y * X;
X := X - 1
Equivalence of commands
c0 c1
iff
 ,  ' .  c0,    '  c1,    '
Proposition 2.8
while b do c  if then (c; while b do c) else skip
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 ’
Small Step Semantics for
Additions
 a 0,   1  a'0,  
 a 0  a1,   1  a'0  a1,  
 a1,   1  a'1,  
 n  a1,   1  n  a'1,  
 n  m,   1  p,  
where p  n  m
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
Exercise 2
(1) Evaluate a  (X4) + 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.