Going forward, but gently...

This commit is contained in:
Yorick Barbanneau 2023-05-05 01:48:36 +02:00
parent 4a30965654
commit 97a81f159a
4 changed files with 36 additions and 8 deletions

View file

@ -318,6 +318,9 @@ int abs2(int n)
/*@ /*@
loop assign i, min, max; loop assign i, min, max;
*/ */
while ( i < n )
{
// ...
\end{lstlisting} \end{lstlisting}
\end{solutionorbox} \end{solutionorbox}
@ -343,7 +346,14 @@ int max_dist(int *tab, unsigned int n)
\begin{solutionorbox} \begin{solutionorbox}
METTEZ VOTRE RÉPONSE ICI. Cette fonction incorrecte prend en paramètre deux éléments -- un tableau et un entier positif -- et en retrourne un -- un entier. C'est exactement la même chose que pour notre fonction correcte. Le contrat s'écrirait alors comme ci-dessous pour Frama-C :
\begin{lstlisting}
\*@
require 0 < n <= UINT_MAX;
require \valid(tab+(0..n-1));
ensures \result >= 0;
*\
\end{lstlisting}
\end{solutionorbox} \end{solutionorbox}
\part Donnez une postcondition vérifiée par la fonction correcte qui nest pas vérifiée par celle-ci. \part Donnez une postcondition vérifiée par la fonction correcte qui nest pas vérifiée par celle-ci.
@ -351,7 +361,7 @@ int max_dist(int *tab, unsigned int n)
\begin{solutionorbox} \begin{solutionorbox}
METTEZ VOTRE RÉPONSE ICI.
\end{solutionorbox} \end{solutionorbox}

View file

@ -6,16 +6,20 @@
/*@ logic integer abs(integer n) = 0<n?n:-n;*/ /*@ logic integer abs(integer n) = 0<n?n:-n;*/
/*@ /*@
requires INT_MIN <= n; requires INT_MIN < n < INT_MAX;
terminates \true; terminates \true;
ensures \result == abs(n); ensures \result == abs(n);
assigns \nothing;
*/ */
int abs(int n); int abs(int n);
/*@ /*@
requires INT_MIN <= n * (int)((int)n % 2 + ((int)n +1)%2) <= INT_MAX; requires INT_MIN < n < INT_MAX;
requires INT_MIN <= n * (int)((int)n % 2 + ((int)n + 1)%2) <= INT_MAX;
terminates \true; terminates \true;
ensures \result == abs(n);*/ ensures \result == abs(n);
assigns \nothing;
*/
int abs2(int n); int abs2(int n);
#endif #endif

View file

@ -5,6 +5,16 @@ int max_dist(int *tab, unsigned int n)
int min = tab[0]; int min = tab[0];
int max = tab[0]; int max = tab[0];
unsigned int i = 1; unsigned int i = 1;
/*@
loop assigns i, max, min;
loop invariant I1: \at(i, LoopEntry) <= i <= n;
loop invariant I2: min <= max;
loop invariant I3: \forall integer j; (\at(i, LoopEntry) <= j < i ==> max >= tab[j] >= min);
loop invariant I4: \exists integer j; ( 0 < j < i ==> max == tab[j]);
loop invariant I5: \exists integer j; ( 0 < j < i ==> min == tab[j]);
loop variant n - i;
*/
while (i < n) while (i < n)
{ {
if (tab[i] < min) if (tab[i] < min)

View file

@ -1,6 +1,10 @@
#include <limits.h> #include <limits.h>
#include "abs.h" #include "abs.h"
/*@ ensures \forall integer a,b; 0 <= a < b < n ==> \result >= abs(tab[a]-tab[b]); /*@
requires 0 < n < UINT_MAX;
requires \valid_read(tab+(0..n-1));
terminates \true;
ensures \forall integer a,b; 0 <= a < b < n ==> \result >= abs(tab[a]-tab[b]);
*/ */
int max_dist(int *tab, unsigned int n); int max_dist(int *tab, unsigned int n);