02 Natural Semantics
02 Natural Semantics
02 Natural Semantics
Natural Semantics
Pablo Lpez
University of Mlaga
1 / 41
Outline
Introduction
Natural Semantics
2 / 41
Outline
Introduction
Natural Semantics
3 / 41
_S=C=5PB H BY6B7=NHMBQ =EY6C=NW
JYHX\ HXMNY6E5X=NW^ {J K+JY=@BCE7=
B77 H5PBQWK+JYQ\C=YPB7HN^
Abstract Syntax of WHILE
=BHXH\C=5PSB55PS=H5PY\M5X\YX=JKQ\C=YPB7HBQW BYX6B 7=H6H:6 =Q =N7HX=
=@BCE7=Q\C=NYXB7HC6:S5 =H5XYX6Q:HJKW6:65XHBQvW BY6B7=NHH5PYX6Q:HJK7=5X
W6:65XHH5PBY5X6Q: 65XSB7=55P=NY^_S=H5PY\M5X\YX=JK5PS=J5XS=NYMJQH5PYX\M5PH6H
D F>F r J KF r M KF rO
D S
FX
F r D F r\ F]
F r
o D >
D F F o r toF
o r o
F
k o
_S\H B JJ7=BQ=@EYX=HXH6JQ MNBQJQ7SB =JQ=JKHX6@K+JYXCHN ^ 56HMB77=NW
CCWe C covered
6K 656H theS semantics
JY X ofJYarithmetic SBH5PS=K+J Y
(Aexp)C r D Boolean
and J Y
r\ K S=NY =
BY= (Bexp)
BY65PSC=5P expressions.
6
M
=
@
E
Y N
=
H X
H
6
J
Q N
H
^
5
6
H N
M
B
7
7 N
= W v
B
V
V C
C
C
C
6
K
6
5
S
B
H P
5 S=
4 / 41
Operational Semantics and State Change
5 / 41
Two Styles of Operational Semantics
6 / 41
Transition System
7 / 41
Quiz
8 / 41
Quiz
8 / 41
Quiz
8 / 41
Quiz
8 / 41
Configurations for WHILE
9 / 41
Configurations for WHILE
9 / 41
Outline
Introduction
Natural Semantics
10 / 41
Transition System for Natural Semantics
10 / 41
Fundamentals of Natural Semantics
11 / 41
But how do we know it?
hy := y 1; skip; x := x + 1, [x 7 3, y 7 4]i [x 7 4, y 7 3]
holds ?
12 / 41
But how do we know it?
hy := y 1; skip; x := x + 1, [x 7 3, y 7 4]i [x 7 4, y 7 3]
holds ?
We define the transition relation by a set of rules and axioms.
12 / 41
Rules and Axioms (I)
The definition of is given by a set of rules and axioms.
A rule has the form:
hS1 , s1 i s01 , hSn , sn i s0n
[rule name] if condition
hS, si s0
where:
I premises are written above the solid line
I the conclusion is written below the solid line
I S1 , . . . , Sn are immediate constituents of S or constructed
from immediate constituents of S
I a rule may also have a number of conditions or provisos that
must be fulfilled for the rule to be applied
I if all the conditions and premises hold then the conclusion
holds
13 / 41
Rules and Axioms (II)
14 / 41
Natural Semantics for WHILE
15 / 41
The Assignment Statement :=
hx := x + 1, s0 i s0 [x 7 1]
assuming that s0 x = 0
I in general, we shall use axiom and rule to mean axiom schema
and rule schema
16 / 41
The skip Statement
[skipns ] hskip, si s
17 / 41
The ; Statement
18 / 41
Quiz
Assume that s0 x = 0.
I is this an instance of [compns ]?
hskip, s0 i s0 hx := x + 1, s0 i s0 [x 7 1]
hskip; x := x + 1, s0 i s0 [x 7 1]
19 / 41
Quiz
Assume that s0 x = 0.
I is this an instance of [compns ]?
hskip, s0 i s0 hx := x + 1, s0 i s0 [x 7 1]
hskip; x := x + 1, s0 i s0 [x 7 1]
Yes, it is.
19 / 41
Quiz
Assume that s0 x = 0.
I is this an instance of [compns ]?
hskip, s0 i s0 hx := x + 1, s0 i s0 [x 7 1]
hskip; x := x + 1, s0 i s0 [x 7 1]
Yes, it is.
I is this an instance of [compns ]?
hskip, s0 i s0 [x 7 5] hx := x + 1, s0 [x 7 5]i s0
hskip; x := x + 1, s0 i s0
19 / 41
Quiz
Assume that s0 x = 0.
I is this an instance of [compns ]?
hskip, s0 i s0 hx := x + 1, s0 i s0 [x 7 1]
hskip; x := x + 1, s0 i s0 [x 7 1]
Yes, it is.
I is this an instance of [compns ]?
hskip, s0 i s0 [x 7 5] hx := x + 1, s0 [x 7 5]i s0
hskip; x := x + 1, s0 i s0
Yes; even though the premises are not instances of the
corresponding axioms!
19 / 41
The if then else Statement
tt hS1 , si s0
[ifns ] if BJbKs = tt
hif b then S1 else S2 , si s0
ff hS2 , si s0
[ifns ] if BJbKs = ff
hif b then S1 else S2 , si s0
20 / 41
Quiz
tt hS1 , si s0
[ifns ] if BJbKs = tt
hif (b) S1 else S2 , si s0
21 / 41
Quiz
tt hS1 , si s0
[ifns ] if BJbKs = tt
hif (b) S1 else S2 , si s0
21 / 41
The while Statement
We need an axiom:
and a rule:
hS, si s0 , hwhile b do S, s0 i s00
[whilett
ns ] if BJbKs = tt
hwhile b do S, si s00
22 / 41
The Natural Semantics of WHILE is not Compositional
23 / 41
Derivation Trees
24 / 41
YC=B>:M9L[Y8MV?:=BOM89L[HB?[?HMVV=BT F1 Y>
[^ ?9:=BC?:=OAM89:[H?M8T
An
Z1>:YO Example
9:;= ?9L>:IH9LIof
>LMVa
YC Derivation
=>LM9:[Y8MV?:=BO M89L[HB
Tree ? ;[T[8^ = =8OY>= =E=HBI9:[Y8 T
89:;For
=8M9:I>LMV?=BOM89L[H?9L;==E=BHI9L[Y8YZ9L;==EMOCV=C>:Y^>LMO
the statement (z:= x; x:= y); y:= z we get the derivation[8 9L;=?LMO=
tree:
=Z1Y>= [VV =>=BC>=B?:=89L=BT 9:;=Z1YVVY [8
M? ^ T=B>[
M9L[Y8 9L>=B=
# %' w ' %
w
# %''
' %
% #
# %
' ' ' %'
% #
;=>:=
=;M =I?=BT9:;=M >= [M9L[Y8?
s% 0 x =
where ' 5, s0 y = # s0_ = 0, and:
7,
w % ' #
% ' s# 1 = s0 [z 7 5]
% ' s# 2 = s1 [x 7 7]
s = s [y 7 5]
d;[?[?9LY =>:=MT ?
M?Z1YVVY 3d;= =E=2HBI9:[Y8YZ # %'[89L;=?9M9L= [VV>:=B?
9L;=?9LM9L
= M8TA9L;==E=HBI9:[Y8 YZ ' %
[8?9M9L= w [VV>:=?:IV9[8?9LM9L
= bd;=B
w 25 / 41
How to Build a Derivation Tree
26 / 41
Exercises
27 / 41
Termination and Looping
28 / 41
Exercises
29 / 41
Outline
Introduction
Natural Semantics
30 / 41
Semantic Equivalence for Natural Semantics
30 / 41
Loop Unfolding for while
is semantically equivalent to
if b then ( S ; while b do S ) else skip
31 / 41
Exercises (I)
and define the relation for it. You are not allowed to rely on the
while construct. Prove that repeat S until b and
S ; if b then skip else ( repeat S until b )
32 / 41
Exercises (II)
and define the relation for it. You are not allowed to rely on the
whileconstruct. Evaluate the statement:
y := 1; for x := 1 to x do ( y := y * x ; x := x -1)
from a state where x has the value 5. Hint: Assume that you have
an inverse to N to compute the numeral from a given number.
33 / 41
Deterministic Natural Semantics
This means that for every statement S and initial state s we can
uniquely determine the final state s0 if (and only if) the execution
of S terminates.
34 / 41
WHILE is Deterministic
35 / 41
Induction on the Shape of the Derivation (Rule Induction)
36 / 41
Exercise
Argue that this means that the extended semantics (i.e. the natural
semantics extended to include repeat until) is deterministic.
37 / 41
The Semantic Function Sns
defined as:
s0 if hS, si s0
Sns JSKs =
undef otherwise
38 / 41
Quiz
39 / 41
Quiz
39 / 41
Quiz
39 / 41
Exercises (I)
Exercise 2.11 The semantics of arithmetic expressions can be
given by a natural semantics specification using the following two
configurations:
I ha, si denoting that a is to be evaluated in state s
I z denoting the final value (z Z)
The transition relation Aexp has the form:
ha, si Aexp z
41 / 41