Slide 1

Slide 1 text

A Need To Specify and Verify Standard Functions Nikolay Shilov A.P. Ershov Institute of Informatics Systems (Novosibirsk, Russia)

Slide 2

Slide 2 text

=4 BACAUSE OF RAND() Part 1 11/13/2015 2 N.Shilov -TMPA-2015 talk

Slide 3

Slide 3 text

MonteCarlo.c #include #include #include int main(void){ srand(time(NULL)); int i, j, r, n = 10; float pi_val, x, y; int n_hits, n_trials=1000000; for(j = 0; j < n; j++){n_hits=0; for(i = 0; i

Slide 4

Slide 4 text

Experiment 11/13/2015 4 N.Shilov -TMPA-2015 talk

Slide 5

Slide 5 text

Proof Psq = 4d, Pcr = d 11/13/2015 5 N.Shilov -TMPA-2015 talk

Slide 6

Slide 6 text

Proof (cont.) Prs = 4d, Pcr = d 11/13/2015 6 N.Shilov -TMPA-2015 talk

Slide 7

Slide 7 text

Proof (cont.) Pgs = 4d, Pcr = d 11/13/2015 7 N.Shilov -TMPA-2015 talk

Slide 8

Slide 8 text

Proof (cont.) Pgs = 4d, Pcr = d 11/13/2015 8 N.Shilov -TMPA-2015 talk

Slide 9

Slide 9 text

Proof (cont.) • The figure around the circle converges to the circle; hence its perimeter converges to d. • but the value of the perimeter is constant 4d; • hence =4. 11/13/2015 9 N.Shilov -TMPA-2015 talk

Slide 10

Slide 10 text

Formal Methods as a Rescue • Let us specify the program in Hoare style by pre- and post-conditions. The pre-condition may be TRUE since the program has no input. • The post-condition may be pi_val==4.0, but since the real program works with floating point values, it makes sense relax the post- condition a little bit. • Due to the exercise we may hope that ╞[TRUE] PiMC [3.9<=pi_val<=4.1]. 11/13/2015 10 N.Shilov -TMPA-2015 talk

Slide 11

Slide 11 text

Formal Methods as a Rescue • But if we try to apply Floyd-Hoare methodic to generate verification conditions and prove the assertion then we encounter a problem of formal semantics of the function rand() in the assignment r = rand()% 10000000; that has 2 instances in the program. 11/13/2015 11 N.Shilov -TMPA-2015 talk

Slide 12

Slide 12 text

Formal Methods as a Rescue • The standard rule to generate verification condition for assignment reads (x)(t) ; [(x)] x=t [(x)] • for function rand()it leads to (x)(rand()) . [(x)] x=rand() [(x)] 11/13/2015 12 N.Shilov -TMPA-2015 talk

Slide 13

Slide 13 text

What is rand()?! (C reference. Rand. http://en.cppreference.com/w/c/numeric/random/rand.) Parameters (none) Return value Pseudo-random integral value between ​0​ and RAND_MAX, inclusive. Notes There are no guarantees as to the quality of the random sequence produced. … POSIX requires that the period of the pseudo-random number generator used by rand is at least 232 POSIX offered a thread-safe version of rand called rand_r, which is obsolete in favor of the drand48 family of functions. 11/13/2015 13 N.Shilov -TMPA-2015 talk

Slide 14

Slide 14 text

WHAT IS SQRT? Part II 11/13/2015 14 N.Shilov -TMPA-2015 talk

Slide 15

Slide 15 text

Solving Quadratic Equations • A very popular approach to teach standard input/output, floating point type, etc., is a program “solving” quadratic equation ax2 + bx + c = 0. #include #include int main(void){ float a, b, c, d, x; printf("Input coefficients a, b and c and type 'enter' after each:"); scanf("%f%f%f",&a,&b,&c); d=b*b -4*a*c; if (d<0) printf("No root(s)."); else {x= (-b + sqrt(d))/(2*a); printf("A root is %f.", x);} return 0;} 11/13/2015 15 N.Shilov -TMPA-2015 talk

Slide 16

Slide 16 text

Solving Quadratic Equations • We put “solving” to quotation marks because non of conventional computers can find root of a simple equation x2 – 2 = 0 due to irrational nature of the number but finite size all numeric data types in every implementation of C. 11/13/2015 16 N.Shilov -TMPA-2015 talk

Slide 17

Slide 17 text

Specification says … (C refernce. Sqrt, sqrtf, sqrtl. http://en.cppreference.com/w/c/numeric/math/sqrt. ) sqrt, sqrtf, sqrtl C Numerics Common mathematical functions Defined in header … Parameters arg - floating point value Return value If no errors occur, square root of arg , is returned. 11/13/2015 17 N.Shilov -TMPA-2015 talk

Slide 18

Slide 18 text

Alternatives for sqrt • It makes sense to introduce another function with two arguments SQR(Y, E) where Y stays for the argument and E stays for accuracy, that can be formally specified by the following clauses: • If Y0 then let A0 be square root of Y, i.e. Y=A2. • if E>0 then SQR(Y, E) must return a floating value X 0 that differs from A less than E, i.e. |X-A|

Slide 19

Slide 19 text

(NOT YET A ) CONCLUSION Part III 11/13/2015 N.Shilov -TMPA-2015 talk 19

Slide 20

Slide 20 text

(Not yet a ) Conclusion • A need of better specification and validation of standard functions is well-recognized by industrial and academic professional community as well as the problem of conformance of their implementation with the specification 11/13/2015 20 N.Shilov -TMPA-2015 talk

Slide 21

Slide 21 text

(Not yet a ) Conclusion • J. Harrison, Formal Verification of Square Root Algorithms. Formal Methods in System Design, 2003, Vol.22(2), p.143-153. • V. Kuliamin, Standardization and Testing of Mathematical Functions Programming and Computer Software, 2007, Vol. 33 (3), p.154-173. • V.V. Kuliamin, Standardization and Testing of Mathematical Functions in floating point numbers. Proceedings of Int. Conf. Perspectives of Systems Informatics PSI-2009. Lecture Notes in Computer Science, 2010, Vol. 5947, p. 257-268. • A.V. Promsky, C Program Verification: Verification Condition Explanation and Standard Library. Automatic Control and Computer Sciences, 2012, Vol. 46, No. 7, p. 394–401. • A.V. Promsky, Experiments on self-applicability in the C-light verification system. Bull. Nov.Comp. Center, Comp. Science, Vol.35, 2013, p.85-99. 11/13/2015 21 N.Shilov -TMPA-2015 talk

Slide 22

Slide 22 text

(Not yet a ) Conclusion • A very serious obstacle for formal verification of standard mathematical functions is a need of axiomatization of floating point arithmetic. • Maybe interval analysis approach and formalization of interval arithmetic may help to tackle the problem for functions like sqrt (but not for functions like rand). 11/13/2015 22 N.Shilov -TMPA-2015 talk