The only possible trees for the statements given in the... ... S ;s

advertisement
Solutions to tutorial on NS
Exercise 2.6
Prove
The only possible trees for the statements given in the exercise are of the form:
..
..
.
.
..
0
00
00
h
S
;
s
i
!
s
;
h
S
;
s
i
! s000
.
2
3
hS2 ; S3 ; s0 i ! s000
hS1 ; si ! s0 ;
hS1 ; (S2 ; S3 ); si ! s000
..
.
..
.
..
hS1 ; si ! s0 ; hS2 ; s0 i ! s00
.
hS1 ; S2 ; si ! s00
; hS3 ; s00 i ! s000
h(S1 ; S2 ); S3 ; si ! s000
From the uniqueness of the rst tree we have that hS1 ; (S2 ; S3 ); si ! s000 i (if and only if)
there exist states s0 and s00 such that hS1 ; si ! s0 hS2 ; s0 i ! s00 hS3 ; s00 i ! s000 :
(1)
Similarly, from the uniqueness of the second tree we have that h(S1 ; S2 ); S3 ; si ! s000 i (1).
Hence hS1 ; (S2 ; S3 ); si ! s000 i h(S1 ; S2 ); S3 ; si ! s000 . This means that the two statements
are semantically equivalent.
Discussion
The style of this proof diers somewhat from the one used in the book. In the book the equivalence
is proved in two steps|rst the \!" part, then the \ " part. That method often leads to longer
and more unclear proofs.
This exercise is quite simple. One quite easily sees what the assumptions of the inference trees
should be, and the trees can be constructed from premises to conclusions.
In general it could be dicult to directly see what the premises would be. Then you could start
either as in the book by trying building one tree and nding the premises from which the the other
tree is constructed, or by building the two trees in parallel until a suitable set of premises is found.
(Finding the proof is dierent from stating it.)
1
Construct and show
One of the simplest constructions in this case is updating the same variable with two dierent values
in dierent order.
An important fact regarding multiple updating of states is:
(s[x 7! m][x 7! n])x = n
The following inferences show that the two statements x:=1 and x:=2 produces states giving
dierent values for x when executed in dierent order:
hx := 1; si ! s[x 7! 1]; hx := 2; s[x 7! 1]i ! s[x 7! 1][x 7! 2]
hx := 1; x := 2; si ! s[x 7! 1][x 7! 2]
hx := 2; si ! s[x 7! 2]; hx := 1; s[x 7! 2]i ! s[x 7! 2][x 7! 1]
hx := 2; x := 1; si ! s[x 7! 2][x 7! 1]
(To avoid too messy trees I have excluded some of the simplest computations, such as applying
A to numerals.)
Exercise 2.7
Extend
The repeat construction has its test at the end. Thus, rst the statement is executed once. Then
the Boolean expression is evaluated. If the value is true the repeat construction is completed. If
the value is false the statement is executed once again and the Boolean expression is evaluated, etc.
This informal description gives the following denitions:
hS; si ! s0
0
hrepeat S until b; si ! s0 if B[ b] s = tt
s0 ; hrepeat S until b; s0 i ! s00 if B[ b] s0 = [repeatns ] hS; si !hrepeat
S until b; si ! s00
[repeatttns ]
2
Prove
These are the only possible trees constructible for the statements in the exercise
..
.
0
If B[ b] s = tt :
hS; si ! s0
hrepeat S until b; si ! s0
..
..
.
.
0
If B[ b] s = :
hS; si ! s0 ; hrepeat S until b; s0 i ! s00
hrepeat S until b; si ! s00
If B[ b] s0 = tt :
If B[ b] s0 = :
..
.
hskip; s0 i ! s0
hS; si ! s0 ;
..
.
hif b then skip else (repeat S until b); s0 i ! s0
hS ; if b then skip else (repeat S until b); si ! s0
..
.
hrepeat S until b; s0 i ! s00
hif b then skip else (repeat S until b); s0 i ! s00
hS ; if b then skip else (repeat S until b); si ! s00
hS; si ! s0 ;
The rst and third of these trees give the equivalence in the tt case and the other two trees give
it in the case.
Discussion
How to nd such a proof? Well I think it could be easier to construct a tree down from the leaves
if there are more premises available. Thus I would start constructing an inference tree from the
root for the second statement (with composition, if, and skip), since more constructions could lead
to more premises.
Where/when to stop? (the case) Well, since you are going to use the premises as leaves of
another tree you should stop when you have enough to do that. It could sometimes be dicult to
really know when that is the case. One method could be to build a part of the tree and see if its
possible to construct the other tree from what you've got. If not, you should continue expanding
the rst tree a bit. In these exercises you could follow the rule \as much as possible without going
into recursion".
Exercise 2.8
Extend
The semantics for for could be presented in a number of ways. I will present two main versions,
one of the versions having two variants.
The informal meaning of for x := a1 to a2 do S is that x should assume each (integer) value from
and including the value of a1 to and including the value of a2 , and for each value the statement S
should be executed. (In my solution I assume that a2 is evaluated every time S is executed.)
3
Version 1
The dierence between the two versions is how the successive new values of the variable are handled.
In the rst version the hint in the exercise text is used: We assume that there is a function N 1
that maps numbers to numerals. That function is used to build new versions of the for statement.
Like in the repeat statement there are two cases in the semantics for the for statement. This
time the choice is based on the relation between two numeric values|the actual value of the loop
variable and the last value the loop variable should assume. When the loop is nished there is no
need for constructing new code, and there is no dierence between the two alternatives:
[for>ns ] hfor x := a1 to a2 do S; si ! s if A[ a1 ] s > A[ a2 ] s
Alternative 1:
There are two alternatives for handling the updating of the loop variable. In the rst alternative
the state is changed to bind the loop variable to its new value:
hS; s[x 7! A[ a1 ] s]i ! s0 ; hfor x := N 1 (A[ a1 ] s + 1) to a2 do S; s0 i ! s00 if A[ a1 ] s 6 A[ a2 ] s
hfor x := a1 to a2 do S; si ! s00
The N 1 (A[ a1 ] s + 1) construction means
[for6ns ]
1. Evaluate the expression for the current value of the loop variable.
2. Add 1 to that value.
3. Produce the numeral corresponding to that numeric value.
Alternative 2:
In the second alternative the updating of the loop variable is done by an explicit assignment
statement:
[for6ns ]
hx:=a1 ; si ! s0 ; hS; s0 i ! s00 ; hfor x := N 1 (A[ a1 ] s + 1) to a2 do S; s00 i ! s000 if A[ a1 ] s 6 A[ a2 ] s
hfor x := a1 to a2 do S; si ! s000
Version 2
In the second version the N
track of the updated values.
1
is not used. Instead a new kind of conguration is used to keep
[forns]
h(for0 x; A[ a1 ] s; a2 ; S ); si ! s0
hfor x := a1 to a2 do S; si ! s0
[for0>ns ] h(for0 x; n; a; S ); si ! s if n > A[ a] s
[for06ns ]
hS; s[x 7! n]i ! s0 ; h(for0 x; n + 1; a; S ); s0i ! s00 if n 6 A[ a] s
h(for0 x; n; a; S ); si ! s00
4
Evaluate
hS; s000 i ! s(4) ; h(for0 z; 4; x; S ); s(4)i ! s(4)
h(for0 z; 3; x; S ); s000i ! s(4)
0
00
0
hS; s i ! s ;
h(for z; 2; x; S ); s00i ! s(4)
0
h(for z; 1; x; S ); s0i ! s(4)
hfor z := 1 to x do S ; s0 i ! s (4 )
hy := 1; for z := 1 to x do S ; s0 i ! s (4 )
hS; s00 i ! s000 ;
hy := 1; si ! s0 ;
where the following abbreviations are used:
S
sx
s0
s00
s000
s(4)
=
=
=
=
=
=
y := y + x; x := x
5
s[y 7! 1]
s0 [y 7! 6][x 7! 4]
s00 [y 7! 10][x 7! 3]
s000 [y 7! 15][x 7! 2]
Thus, the nal value of y will be 15 and x will not reach 0.
5
1
Download