4
float sqrt( float x ) { require( x >= 0 ); const float eps = 0.0001; float guess = 1; while( abs(x - guess * guess ) ) > eps ) guess = ( guess + x / guess ) / 2; promise( guess * guess - x <= eps ); return guess; }
void bubble(int r[], int length) {\c{\fbox{bubble}}
int k = length; int sorted = 0; while ( ( k > 0 ) && !sorted ) { sorted = 1; for( int j = 0; j < k ; j++ ) if ( r[j] > r[j+1] ) { swap(r[j], r[j+1]); sorted = 0; } k = k - 1; } }
class A {\fbox{invariant A: }
public: A() { n = 0; } int value() { return next(n); } void strange() { next(-3); } protected: virtual int next( int i ) { return n = n + i * i; } int n; };
class B : public A {\fbox{not \ifsli{ inv A }{invariant A } }
public: B() : A() { } protected: virtual int next( int i ) { return n = n + (n + 1) * i; } };
A* a = new A; a->value(); a->strange(); a->value();ok
A* b = new B; b->value(); b->strange(); b->value();\c{//} error
int f(A* a) { a->strange(); return a->value(); }