



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




_Dr. Jim Sasaki, CS Dept, Illinois Institute of Chicago Problem 1: Predicate Satisfaction and Validity (30 = 65 points)_* (For each of the following satisfaction and validity claims, use the definition of! to show that the claim is true in the general case; alternatively, provide a counterexample. Assume that states are proper.) a. ![x := 3]! !x : x > 5. This is true: ![x := 3]! !x : x > 5 if there is some "!"! Z such that ![x := 3][x := "]! x > 5 if there is some "!"! Z such that ![x := "]! x > 5 if there is some "!"! Z such that (![x := "])(x) > 5 if there is some "!"! Z such that " > 5 which is certainly true (using " = 6, for example). Note that since we're trying to satisfy an existential over x, it doesn't matter what value our initial state gives to x. b. #! $a#>#1 : !b : a#<#b < a" [Note a#<#b#<#a" is an abbreviation for a#<#b % b#<#a".] This is true: #! $a#>#1 : !b : a#<#b < a" if for all "!"! Z where #[a#:=#"]! a#>#1, we have #[a#:=#"]! !b : a#<#b < a" if for all "!"! Z where "#>#1, there is some $!"! Z such that #[a#:=#"][b#:=#$]! a#<#b < a" if for all "!"! Z where "#>#1, there is some $!"! Z such that "#<#$ < "" which is true: Since "" – " = "("#–#1) # 2 $1 = 2, if we have $#=#"+1, then $ > " and < "". c.!! !x : x!x < y This is false; for a counterexample, let !(y) = 0, then!! !x : x!x < y iff exists "!"! Z such that ![x := "]! x!x < y iff exists "!"! Z such that "$" < !(y) = 0 which is false. Note that if the problem had been phrased “Is there a! that satisfies !x : x!x < y?” then the answer would have been “yes“. d.! $#x#>#1 : !#y#>#x!x : y#<#(x+1)!(x+1) This is true:!! $#x#>#1 : !#y#>#x!x : y#<#(x+1)!(x+1) iff for every "!"! Z , ![x := "]! x#># 1 & !#y#>#x!x : y#<#(x+1)!(x+1) iff for every ", if ![x := "]! x#>#1 then ![x := "]! !#y#>#x!x : y#<#(x+1)!(x+1) iff for every ", if " > 1 then ![x := "]! !#y#>#x!x : y#<#(x+1)!(x+1) iff for every ", if " > 1 then there exists an %!"! Z such % that ![x := "][y := %]! y#>#x!x % y#<#(x+1)!(x+1) iff for every "!> 1, there exists an %!"! Z such that % > "" and % < ("+1)".
This is true because we can use ""+1 for %: We need "" < % = ""+1 < ("+1)" = ""+2"+1. To show ""+1 < ""+2"+1, we need 0 < 2" which is true because 0 < ". e.!! p % q & q This is true:!! p % q & q if (!! p % q) implies (!! q) if (!! p and!! q) implies (!! q) if!! q implies (!! q) if true. f.! p & q & p (Recall that & is right-associative.) This is true:!! p & q & p if (!! p) implies (!! q & p) if (!! p) implies ((!! q) implies (!! p)) if!! p implies!! p if true. _Problem 2: Program Translation (15 = 35 points)_* Translate the following C++/Java code fragments into equivalent programs in our simple pro- gramming language. a. for (x=0; (x+1)!(x+1) <= n; x++) { } x := 0; while (x+1)!(x+1) & n do x := x+1 od b. while (++x <= y) {z += x;} x := x+1; while x & y do z := z+x; x := x+1 od [If we want to use x!& y as the while loop test, we need to increment x before the first while test, so it helps to be consistent and increment x just before each while test. We do that by putting x := x+1 at the end of the loop body. If you want to do the test be- fore the increment, then we can use while x+1 & y do x := x+1; z := z+x od ; x := x+1. Note there has to be an increment after the while loop to handle the ++x in the failing while (++x!<=!y) test.] c. int i=1, s=0; while (i++ <= n) {s += (i%2 != 0? i : 0);} i := 1; s := 0 while i & n do i := i+1; s := s + if (i % 2 ' 0 then i else 0 fi od ; i := i+1!. [Note we need an increment of i after the loop to handle the post-increment of i when while (i++!<= n) fails.] _Problem 3: Program Evaluation (25 = 55 points total)_* For each of the following cases, use the definition of M to compute the meaning of the pro- gram in the given state. a. M "a#:=#n; b#:=#a+a#(!') = M "b#:=#a+a#( M "a#:=#n#(!')) = M "b#:=#a+a#( {!'[n#:=#!'(a)]} ) = M "b#:=#a+a#( {(} ) where (#=#!'[n#:=#)] and ) = !'(a)
= M "s := s+i#( {([i := ((i)+1]} ) = {([i := ((i)+1][s := (i := ((i)+1]} = {([i := ((i)+1][s := (i := ((i)+1+(i := ((i)+1]} = {([i := ((i)+1][s := ((s)+((i)+1]} Now, starting with (. = !-[n := 2][i := 0][s := 0], define the sequence {(.}, {('}, … where {(k+1} = M !"S'#( {(k} ) = {(k[i := (k(i)+1][s := (k(s)+(k(i)+1]}, …. This gives us {(.} = !-[n := 2][i := 0][s := 0], {('} = M !"S'#( {(.} ) = {(.[i := (.(i)+1][s := (.(s)+(.(i)+1]} = {!-[n := 2][i := 1][s := 1] }, {(} = M !"S'#( {('} ) = {('[i := ('(i)+1][s := ('(s)+('(i)+1]} = {!-[n := 2][i := 2][s := 3] }, {(+} = M !"S'#( {(} ) = {([i := ((i)+1][s := ((s)+((i)+1]} = {!-[n := 2][i := 3][s := 6] }, …. We find that (., (', and (* all! i & n, but (+ " i & n, so {(+} is the resulting state after the loop (and the state after our program). _Problem 4: Correctness Triple Satisfaction and Validity (30 = 56 points)_* For each of the following cases, use the definitions of satisfaction and validity for partial cor- rectness triples (i.e., “!! {p} S {q}” and “! {p} S {q}”) to determine which of the following claims of correctness are correct. Give a counterexample when a correctness claim is incor- rect. Use the style of Problem 1 to write your proofs. a. !'! {true} n := n!n+1; i := 0; s := i { n # i # 0 % s = sum(0, i) } [Recall: sum(x, y) = x + (x+1) + … + y. If x > y then sum(x, y) = 0.] This is true. Let )#=#!'(n), then M "n := n!n+1; i := 0; s := i#(!') = M "i := 0; s := i#({!'[n# :=#!'(n!n+1)]}) = M "i := 0; s := i#({!'[n#:=#)"+1]}) = M "s := i#(!'[n#:=#)"+1][i#:=#0]) = {!'[n# :=#)"+1][i#:=#0][s#:=#(!'[n#:=#)"+1][i#:=#0)(i)]} = {('} where ('#=#!'[n#:=# )"+1][i#:=#0][s#:=#0]. We can verify ('! n!# i!# 0 % s = sum(0, i), because )"+1 # 0 # 0 and 0!=!sum(0, 0). b. !! {t = s = sum(0, i)}!i := i+1; s := s+i!{s = t+i} This is true. Assume !! t = s = sum(0, i); we must show that M "i := i+1; s := s+i#!(!) ! s = t+i. Let !(s) = !(t) = ) and let !(i) = /. Let {(} = M "i!:= i+1; s!:= s+i#(!), then we must show that (! s#=#t+i. From Problem 3(e) above, ( = ![i := /+1][s := )+/+1]. Then (! s = t+i if ((s) = ((t)+((i), which is true if )+/+1 = !(t) + /+1 = )+/+1, which is true. c.! {#r#=#prod(1,#k)#}!r := r!k; k := k+1!{#r#=#prod(1,#k)#} This is false. Say !+! r#=#prod(1,#k). Let )#=#!+(r), let /#=#!+(k), and let {(+}% = M "r := r!k; k := k+1#(!+) % = M "k := k+1#( M "r := r!k#(!+)) % = M "k := k+1#( {!+[r!:= !+(r!k)} ) % = M "k := k+1#( {!+[r!:= )$/} ) % = {!+[r#:=#)$/][k#:=#/+1]}
For our counterexample, take )#=#/ = 1. Then !+! r#=#prod(1,#k), but (+ " r#=#prod(1,#k) because (+(r) = 1$1 = 1, but (+(prod(1,#k)) = 1$2 = 2. (If we changed the assignments to r := r!(k+1); k := k+1, then the triple would be valid.) d. !,! {n!# 0} n!:=!n-1 {n!# 0} This is false. If !,(n) = 0, then !,! n!# 0, M "n!:= n–1#(!,) = {!,[n := !,(n)–1]} = {!,[n!:= 0-1]} = {!,[n := -1]} " n!# 0. (Again, if the problem had been stated as “Is there a !, that satisfies the triple?“ then the answer would have been “yes”.) e. !-[x := 0]! {x > 0} S {2+2 = 5} This is true. Since !-[x := 0] " x!> 0, it doesn’t matter whether or not M "S#(!-[x!:= 0]) ! 2+2#=#5 (which is good, since it doesn’t).