TTY/OHJ4040 Con-Adv E1: Sequential 1. Formulate eah of the following as a prediate: a. The value of variable x is the ube of the value of the s is the variable i. b. The value of variable N N 0, that is, the sum of the rst N g 0 and 0. [0 : 1℄ : int is the greatest ommon divisor (gd) of d. The array y [0 : 1℄ : int N is the array sorted into asending order, N 2, i terms of this innite sum. . P1 i=1 th partial sum of x > x 0, that is, y > N y ontains exatly the same elements, but so that any element in is stritly y less ( <) than the next. 1 2. Compute and simplify eah of the following weakest preonditions: (skip b. wp( := a. wp ( ; gd X; Y x and ) = ( )), ( )= ( )^ gd x; y y; gd X; Y x Y are onstants, a j i; a a i gd x; y x > 0 < y ), where X ( [ ℄ := [ [ ℄℄ = ), assuming the array [0 : 1℄ with the relevant elements dened, onstant 1, and 0 . What else need you assume? d. wp( :=: ), e. wp( [ ℄ :=: [ + 1℄; := + 1 (8 : 0 : [℄ [ ℄) ^ 0 ), with the same assumptions about and . wp i a N N i; j < N x y; x < y a j a j a j j as above, (if f. wp x > j j ; i i < j a i N 0 ! := x a x 1 x > 1 ! := x x 2 ( )). ;P x N 3. Solve: (do b. wp(do a. wp x > x > 0 ! := 0 ! := x x x x 1 1 (do using the denition of wp x x ;x x > x x ;x ::: 4. Given two non-dereasing arrays [0 : G ;x F [0 : : 1℄ : int M and 1℄ : int and initial ondition : 0^ 0 ^ (8 : 0 1 : [ ℄ [ + 1℄) ^ (8 : 0 1 : [ ℄ [ + 1℄) N Q 1 ! := 2 od = 0) and 0 ! := 2 od = 0) od = 0) x > ; N M i i < M F i F i i i < N G i G i ; prove that the following program is totally orret and omputes R : = (# : 0 r i; j i < M f g ^0 j < N : [℄ F i [ ℄) > G j : Q var r : int := 0; m : int := M ; n : int := N fI g do m 6= 0 ^ n 6= 0 ! if F [m 1℄ > G[n 1℄ ! r := r + n; m := m [ 1℄ [ 1℄ ! := f g od f ^ [( = 0) _ ( = 0)℄gf g F m G n n n 1 1 I I m n R : (Hint: This has a tail-invariant, like Eulid's algorithm has, though not as simple.) [0 : 1℄ : int 0 5. Given an array x N , N , and two integers L and M, L M N , prove totally orret the following program to reverse the order of the elements in the array segment x L M . Initially: 0 [ : 1℄ : (8 : : [ ℄ = [ ℄) ^ 0 and the goal is: : (8 : : [ ℄ = [ 1 ( )℄) The auxiliary array [0 : 1℄ : int to denotes the Q i L i < M R N.B. x i i X i L L i < M logial x i X M N X M i L : N state of the array originally. This is eetively a onstant and universally quantied (proof must hold regardless of its atual value). f g Q var m : int := LfI g do m 6= (M + L) div 2 ! [ ℄ :=: [ 1 ( )℄; := + 1f g od f ^ ( = ( + ) div 2)gf g x m I x M m m M L L m m I R (Hints: Invariant must say something of the elements not yet moved. Doing this problem will take time!) 6. Homework: Given three integers Q : 0 ^ 0 ^ 0, N M B prove totally orret the following program to ompute: R : = (# : 0 a i i < B f g :( N div 2i) mod 2 6= ( Q M div var a : int := 0; b : int := 0; m : int := M ; n : int do b 6= B ! if n mod 2 6= m mod 2 ! a := a + 1 n mod 2 = m mod 2 ! skip ; b; n; m := b + 1; n div 2; m div 2fI g od fI ^ (b = B )gfRg: (Hint: assuming that M < ompute?) 2B ^ 7. Homework: Given an integer N orret a program to ompute: R : = ( : 1 : 2) r i i N i : N < 2i) mod 2) : := f g N I 2B , what does the program (12p) 0, write and prove totally (3p)