230 likes | 362 Views
Homework 2 (simple loops in TL) : Write and test the following function: % Return the number of occurrences of character c in string s . func count_chars ( c : char, const ref s : string ) : int ... your code here …
E N D
Homework 2 (simple loops in TL) : Write and test the following function: % Return the number of occurrences of character c in string s . funccount_chars ( c : char, const ref s : string ) : int ... your code here … NOTE: Proper comments, including the loop invariant and the termination argument, are an essential part of a complete solution! HINT: Read and understand the second example in the lecture “Arrays and quantifiers”. Also: recall the counting quantifier N . EXAMPLE: The statement TRACE count_chars( ‘a’, “Able was I ere I saw Elba” ) should print out the value 3 . NOTE: 1. Please do it just by yourself, don’t show your solution to, or discuss it with others. 2. Run your program and check that it does what you expect. 3. Please hand in your submission on paper, not via e-mail.
Homework 2 (simple loops in TL) : Write and test the following function: % Return the number of occurrences of character c in string s . funccount_chars ( c : char, const ref s : string ) : int ... your code here … NOTE: Proper comments, including the loop invariant and the termination argument, are an essential part of a complete solution! HINT: Read and understand the second example in the lecture “Arrays and quantifiers”. Also: recall the counting quantifier N . EXAMPLE: The statement TRACE count_chars( ‘a’, “Able was I ere I saw Elba” ) should print out the value 3 . NOTE: 1. Please do it just by yourself, don’t show your solution to, or discuss it with others. 2. Run your program and check that it does what you expect. 3. Please hand in your submission on paper, not via e-mail.
Homework 2 (simple loops in TL) : Write and test the following function: % Return the number of occurrences of character c in string s . funccount_chars ( c : char, const ref s : string ) : int ... your code here … NOTE: Proper comments, including the loop invariant and the termination argument, are an essential part of a complete solution! HINT: Read and understand the second example in the lecture “Arrays and quantifiers”. Also: recall the counting quantifier N . EXAMPLE: The statement TRACE count_chars( ‘a’, “Able was I ere I saw Elba” ) should print out the value 3 . NOTE: 1. Please do it just by yourself, don’t show your solution to, or discuss it with others. 2. Run your program and check that it does what you expect. 3. Please hand in your submission on paper, not via e-mail.
Homework 2 (simple loops in TL) : Write and test the following function: % Return the number of occurrences of character c in string s . funccount_chars ( c : char, const ref s : string ) : int ... your code here … NOTE: Proper comments, including the loop invariant and the termination argument, are an essential part of a complete solution! HINT: Read and understand the second example in the lecture “Arrays and quantifiers”. Also: recall the counting quantifier N . EXAMPLE: The statement TRACE count_chars( ‘a’, “Able was I ere I saw Elba” ) should print out the value 3 . NOTE: 1. Please do it just by yourself, don’t show your solution to, or discuss it with others. 2. Run your program and check that it does what you expect. 3. Please hand in your submission on paper, not via e-mail.
Homework 2 (simple loops in TL) : Write and test the following function: % Return the number of occurrences of character c in string s . funccount_chars ( c : char, const ref s : string ) : int ... your code here … NOTE: Proper comments, including the loop invariant and the termination argument, are an essential part of a complete solution! HINT: Read and understand the second example in the lecture “Arrays and quantifiers”. Also: recall the counting quantifier N . EXAMPLE: The statement TRACE count_chars( ‘a’, “Able was I ere I saw Elba” ) should print out the value 3 . NOTE: 1. Please do it just by yourself, don’t show your solution to, or discuss it with others. 2. Run your program and check that it does what you expect. 3. Please hand in your submission on paper, not via e-mail.
Let a be an array with N elements. We want to establish: R : sumsq = ( S i : 0 =< i < N : a[ i ] * a[ i ] ) Our invariant will be P( k ) : (sumsq = ( S i : 0 =< i < k : a[ i ] * a[ i ] ) and k =< N. sumsq, k := 0, 0 ;% P( k ) while k != N do % P( k ) and k != N sumsq := sumsq + a[ k ] * a[ k ] ; % P( k ) violated k := k + 1 % progress, P( k ) restored od % P( k ) and k = N, hence R State spaces
Homework 2 (simple loops in TL) : Write and test the following function: % Return the number of occurrences of character c in string s . funccount_chars ( c : char, const ref s : string ) : int ... your code here … NOTE: Proper comments, including the loop invariant and the termination argument, are an essential part of a complete solution! HINT: Read and understand the second example in the lecture “Arrays and quantifiers”. Also: recall the counting quantifier N . EXAMPLE: The statement TRACE count_chars( ‘a’, “Able was I ere I saw Elba” ) should print out the value 3 . NOTE: 1. Please do it just by yourself, don’t show your solution to, or discuss it with others. 2. Run your program and check that it does what you expect. 3. Please hand in your submission on paper, not via e-mail.
Other useful quantifiers: sum, product, count. (S i : 0 =< i < N : a[ i ]) (P i : 0 =< i < N : a[ i ]) (N i : 0 =< i < N : a[ i ] = 0) The number of elements of a in the range [ 0, N ) whose value is 0. (Here, a[ i ] = 0 is just an example of a predicate and a[ i ] is just an example of an expression). NOTE: These are not predicates: their values are numerical. State spaces
Let a be an array with N elements. We want to establish: R : sumsq = ( S i : 0 =< i < N : a[ i ] * a[ i ] ) Our invariant will be P( k ) : (sumsq = ( S i : 0 =< i < k : a[ i ] * a[ i ] ) and k =< N. sumsq, k := 0, 0 ;% P( k ) while k != N do % P( k ) and k != N sumsq := sumsq + a[ k ] * a[ k ] ; % P( k ) violated k := k + 1 % progress, P( k ) restored od % P( k ) and k = N, hence R We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) State spaces
Let a be an array with N elements. We want to establish: R : sumsq = ( S i : 0 =< i < N : a[ i ] * a[ i ] ) Our invariant will be P( k ) : (sumsq = ( S i : 0 =< i < k : a[ i ] * a[ i ] ) and k =< N. sumsq, k := 0, 0 ;% P( k ) while k != N do % P( k ) and k != N sumsq := sumsq + a[ k ] * a[ k ] ; % P( k ) violated k := k + 1 % progress, P( k ) restored od % P( k ) and k = N, hence R We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be WHAT? State spaces
Let a be an array with N elements. We want to establish: R : sumsq = ( S i : 0 =< i < N : a[ i ] * a[ i ] ) Our invariant will be P( k ) : (sumsq = ( S i : 0 =< i < k : a[ i ] * a[ i ] ) and k =< N. sumsq, k := 0, 0 ;% P( k ) while k != N do % P( k ) and k != N sumsq := sumsq + a[ k ] * a[ k ] ; % P( k ) violated k := k + 1 % progress, P( k ) restored od % P( k ) and k = N, hence R We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) State spaces
We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) In other words: count is equal to the number of occurrences of c in s at positions strictly lower than the current value of k. State spaces
We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) Note that P( k ) and k = size( s ) implies R and this is what makes it a good candidate for an invariant! State spaces
We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) Note that P( k ) and k = size( s ) implies R and this is what makes it a good candidate for an invariant! Also, our loop condition must be not k = size( s ) , i.e., k != size( s ) . State spaces
We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) Note that P( k ) and k = size( s ) implies R and this is what makes it a good candidate for an invariant! Also, our loop condition must be not k = size( s ) , i.e., k != size( s ) . % establish P( k ) while k != size( s ) do ... od Why is k != size( s ) a better loop condition than k < size( s ) ? State spaces
We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) Note that P( k ) and k = size( s ) implies R and this is what makes it a good candidate for an invariant! Also, our loop condition must be not k = size( s ) , i.e., k != size( s ) . % establish P( k ) while k != size( s ) do ... od Why is k != size( s ) a better loop condition than k < size( s ) ? We want to make it as weak as possible, to increase the likelihood of nontermination in case we mess things up (e.g., forget proper initialisation). WHY? State spaces
We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) Note that P( k ) and k = size( s ) implies R and this is what makes it a good candidate for an invariant! Also, our loop condition must be not k = size( s ) , i.e., k != size( s ) . % establish P( k ) while k != size( s ) do ... od Why is k != size( s ) a better loop condition than k < size( s ) ? We want to make it as weak as possible, to increase the likelihood of nontermination in case we mess things up (e.g., forget proper initialisation). Remember: no result is much better than the wrong result!
We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) Note that P( k ) and k = size( s ) implies R and this is what makes it a good candidate for an invariant! Also, our loop condition must be not k = size( s ) , i.e., k != size( s ) . % establish P( k ) while k != size( s ) do ... od How do we establish P( k ) ?
We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) Note that P( k ) and k = size( s ) implies R and this is what makes it a good candidate for an invariant! Also, our loop condition must be not k = size( s ) , i.e., k != size( s ) . k , count := 0, 0 ; % P( k ) established while k != size( s ) do ... od How do we establish P( k ) ?
We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) Note that P( k ) and k = size( s ) implies R and this is what makes it a good candidate for an invariant! Also, our loop condition must be not k = size( s ) , i.e., k != size( s ) . k , count := 0, 0 ; % P( k ) established while k != size( s ) do if s[ k ] = c then count := count + 1 % P( k ) violated fi; ... od
We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) Note that P( k ) and k = size( s ) implies R and this is what makes it a good candidate for an invariant! Also, our loop condition must be not k = size( s ) , i.e., k != size( s ) . k , count := 0, 0 ; % P( k ) established while k != size( s ) do if s[ k ] = c then count := count + 1 % P( k ) violated fi; k : = k + 1 % progress, P( k ) re-established od
% Invariant: P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) % Termination: size( s ) – k >= 0, decreases with each iteration. k , count := 0, 0 ; % P( k ) established while k != size( s ) do if s[ k ] = c then count := count + 1 % P( k ) violated fi; k : = k + 1 % progress, P( k ) re-established od
% Return the number of occurrences of character c in string s . funccount_chars ( c : char, constref s : string ) : int begin varcount : int; var k : int; % Invariant: P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) % Termination: size( s ) – k >= 0, decreases with each iteration. k , count := 0, 0 ; % P( k ) established while k != size( s ) do if s[ k ] = c then count := count + 1 % P( k ) violated fi; k : = k + 1 % progress, P( k ) re-established od; return count end