Você está na página 1de 7

The exercises and answer for programming language class

Neversay: g9403@cherry.cs.nccu.edu.tw March 14, 2006


Abstract There are exercises and its answer in detail. Please read it carefully. This exercise practice includes: 2.4: Determine whether or not the given statement therminateing by apply the rule or axiom you learn in natrual semantics. 2.7: Dene the natrual semantics(NS) of repeat-construct in language While, and prove the equivalence of two sentences. 2.8: Dene the natrual semantics(NS) of for-construct in language While, and evaluate some statement by using derivation tree. 2.17: Dene the structural operational semantics(SOS) of repeatconstruct in language While. 2.18: Dene the structural operational semantics(SOS) of for-construct in language While. Homework 2: Dene the NS and SOS of (for)-construct in language While. If you have any question about this article, please call TA for help.

[assns ] [skipns ] [compns ]


tt [ifns ]

< x := a, s > s[x A[||]s] < skip, s > s < S1 , s > s , < S2 , s > s < S 1 ; S 2 , s > s < S1 , s > s < if b then S1 else S2 , s > s < S 2 , s > s < if b then S1 else S2 , s > s if B [|b|]s = tt if B [|b|]s =

ff [ifns ]

[whilett ns ]
f [whilef ns ]

< S, s > s , < while b do S, s > s if B [|b|]s = tt < while b do S, s > s < while b do S, s > s if B [|b|]s = Table 1: Natural semantics for While

1
1.1

Exercise 2.4
Question

Consider the following statements: 1. while (x = 1) do (y := y x; x := x 1) 2. while 1 x do (y := y x; x := x 1) 3. while true do skip For each statement determine whether or not it always terminates and whether or not it always loops. Try to argue for your answers using the axioms and rules of Table 1.

1.2

Answer
x; x := x 1) It terminates when only x 1 otherwise it loops.

1. while (x = 1) do (y := y

2. while 1 x do (y := y x; x := x 1) It always terminates. 3. while true do skip It always loops because the derivation tree show that: ...... < skip, s > s, < while true do skip, s > s < skip, s > s, < while true do skip, s > s [whilett ns ] < while true do skip, s > s because B [|true|]s == tt

2
2.1

Exercise 2.7
Question
repeat S until b

1.Extend the language While with the statement and dene the relation for it. (The semantic of the repeat-construct is not allowed to rely on the existence of a while-construct in the language.) 2. Prove that repeat S until b and S ; if b then skip else (repeat S until b) are semantically equivalent.

2.2
1.

Answer
< S, s > s < repeat S until b, s > s

[repeattt ns ]
f [repeatf ns ]

if B [|b|]s = tt if B [|b|]s =

< S, s > s , < repeat S until b, s > s < repeat S until b, s > s

2. We must prove that repeat S until b S ; if b then skip else repeat S until b a. First we prove part 1 : repeat S until b S ; if b then skip else (repeat S until b) 1. case [repeattt ns ] < S, s > s < repeat S until b, s > s if B [|b|]s = tt

by axiom < skip, s > s , because B [|b|]s = tt, then < skip, s > s < if b then skip else (repeat S until b), s > s
tt by applying to [ifns ], and then

if B [|b|]s = tt

< S, s > s , < if b then skip else (repeat S until b), s > s < S ; if b then skip else (repeat S until b), s > s by applying to [compns ] we got the prove. 2. case
f [repeatf ns ]

< S, s > s , < repeat S until b, s > s < repeat S until b, s > s

if B [|b|]s =

< (repeat S until b), s > s < if b then skip else (repeat S until b), s > s
ff by applying [ifns ] because B [|b|]s =

if B [|b|]s =

< S, s > s , < if b then skip else (repeat S until b), s > s < S ; if b then skip else (repeat S until b), s > s by applying [compns ] and then we got the prove. By combining of case 1 and 2 we complete the part 1 of this prove.

b. Now we prove part 2 : repeat S until b S ; if b then skip else (repeat S until b)

tt 1. case : [ifns ]. we assume s where B [|b|]s = tt

First, we extract sentence we want to prove by applying [compns ]: < S, s > s , < if b then skip else (repeat S until b), s > s < S ; if b then skip else (repeat S until b), s > s And we are going to extract < skip, s > s from if b then skip else (repeat S until b): < skip, s > s < if b then skip else (repeat S until b), s > s and by axiom < skip, s > s , we know s = s. (equation 1) And then we prove by combining equation 1 and denition of [repeattt ns ]: < S, s > s because B [|b|]s = tt < repeat S until b, s > s = s
ff 2. case : [ifns ]. we assume s where B [|b|]s =

if B [|b|]s = tt

First, we extract sentence we want to prove by applying [compns ]: < S, s > s , < if b then skip else (repeat S until b), s > s < S ; if b then skip else (repeat S until b), s > s And we are going to extract < repeat S until b, s > s from if b then skip else (repeat S until b): < (repeat S until b), s > s < if b then skip else (repeat S until b), s > s and we have: 4 if B [|b|]s =

f [repeatf ns ]

< S, s > s , < repeat S until b, s > s < repeat S until b, s > s because B [|b|]s = , so we complete this prove.

3
3.1

Exercise 2.8
Question
for x := a1 to a2 do S 1. Extend the language While with this statement and dene the relation for it. 2. Evaluate the statement y := 1; for z := 1 to x do (y := y x; x := x 1) from a state where x has the value 5. Hint: You may need to assume that you have an inverse to N , so that there is a numeral for each number that may arise during the computation. (The semantics for the for-construct is not allowed to rely on the existence of a while-coonstruct.)

Another iterative construct is :

3.2
1.

Answer
< x := a1 , s0 > s1 , < S, s1 > s2 , < for x := a3 to a2 do S, s2 > s3 < for x := a1 to a2 do S, s0 > s3 where a3 = N 1 [|A[|a1 |]s2 + 1|] if B [|(x = a2 )|]s1 = tt < x := a1 , s0 > s1 ff ] [f orns < for x := a1 to a2 do S, s0 > s1 if B [|(x = a2 )|]s1 =
tt [f orns ]

Please note that the state of x always increases by 1 after evaluating S . 2. We evaluate this by using derivation tree: We encode the state variable sx,y,z which corresponding to the value of x, y, and z in s. For example, s1,2,3 means x=1,y=2,z=3 in this state enviorment. For saving space, we use S replace (y := y x; x := x 1). < y := 1, s5,0,0 > s5,1,0 , < for z := 1 to x do S, s5,1,0 > s3,20,3 by < y := 1; for z := 1 to x do S, s5,0,0 > s3,20,3 [compns ].
tt And now we derivate the tree by [f orns ]: < z := 1, s5,1,0 > s5,1,1 , < S, s5,1,1 > s4,5,1 , < for z := 2 to x do S, s4,5,1 > s3,20,3 < for z := 1 to x do S, s5,1,0 > s3,20,3 where 2 = N 1 [|A[|1|]s4,5,1 + 1|] if B [|(z = x)|]s5,1,1 = tt

[asssos ] [skipsos ] [comp1 sos ] [comp2 sos ]


tt [ifsos ] ff [ifsos ]

< x := a, s > s[x A[||]s] < skip, s > s < S1 , s >< S1 , s > < S1 ; S2 , s >< S1 ; S2 , s > < S1 , s > s < S1 ; S2 , s >< S2 , s > < if b then S1 else S2 , s >< S1 , s > if B [|b|]s = tt < if b then S1 else S2 , s >< S2 , s > if B [|b|]s = < while b do S, s > < if b then (S ; while b do S ) else skip, s >

[whilesos ]

Table 2: Structural Operational Semantics for While For now we know how to extract < (y := y x; x := x 1), s5,1,1 > s4,5,1 by [compns ]: < y := y x, s5,1,1 > s5,5,1 , < x := x 1, s5,5,1 > s4,5,1 < y := y x; x := x 1, s5,1,1 > s4,5,1 For simplizing our derivation process, we will not explain tree like this any more. And now we focus on the tree of [f or]: < z := 2, s4,5,1 > s4,5,2 , < S, s4,5,2 > s3,20,2 , < for z := 3 to x do S, s3,20,2 > s3,20,3 < for z := 2 to x do S, s4,5,1 > s3,20,3 where 3 = N 1 [|A[|2|]s3,20,2 + 1|] if B [|(z = x)|]s4,5,2 = tt < z := 3, s3,20,2 > s3,20,3 < for z := 3 to x do S, s3,20,2 > s3,20,3 if B [|(z = x)|]s3,20,3 =

4
4.1

Exercise 2.17
Question

Extend While with the construct repeat S until b and specify a structural operational semantics for it. (The semantics for the repeat-construct is not allowed to rely on the existence of a while-construct.)

4.2

Answer

It is a piece of cake: [repeatsos ] < repeat S until b, s > < S ; if b then skip else (repeat S until b), s >

5
5.1

Exercise 2.18
Question
1. Extend While with the construct for x := a1 to a2 do S and specify the structural operational semantics for it. Hint: You may need to assume that you have an inverse to N , so that there is a numeral for each number that may arise during the computation. (The semantics for the for-construct is not allowed to rely on the existence of a while-coonstruct.) 2. Evaluate the statement y := 1; for z := 1 to x do (y := y x; x := x 1) from a state where x has the value 5.

5.2

Answer

1. The answer for for-construct is simple: [f orsos ] < x := a1 , s > s < for x := a1 to a2 do S, s >< if (x = a2 ) then (S ; for x := a3 to a2 do S ) else skip, s > where a3 = N 1 [|A[|a1 |]s + 1|] 2. We place S with (y := y x; x := x1) for shorting the shape of evaluation. < y := 1; for z := 1 to x do S, s5,0,0 > < for z := 1 to x do S, s5,1,0 > < if (z = x) then (S ; for z := 2 to x do S ) else skip, s5,1,1 > < (y := y x; x := x 1); for z := 2 to x do S, s5,1,1 > < x := x 1; for z := 2 to x do S, s5,5,1 > < for z := 2 to x do S, s4,5,1 > ... < for z := 3 to x do S, s3,20,2 > < if (z = x) then (S ; for z := 1 to x do S ) else skip, s3,20,3 > < skip, s3,20,3 > s3,20,3

Homework 2

Extend the language While with the statement for x := a1 to a2 step Ss do S where Ss is the step statement and always executes after S . And dene the relation and for it. Hint: You may need to assume that you have an inverse to N , so that there is a numeral for each number that may arise during the computation. (The semantics for the for-construct is not allowed to rely on the existence of a while-coonstruct.) 7

Você também pode gostar