This is another example demonstrating the value of logic in computer programming. ``Proof of correctness'' is carried out by means of proofs of wffs, called Hoare triples.
``Beware of bugs in the above code; I have only proved it correct, not tried it.'' Donald E. Knuth, author of The Art of Computer Programming.
``Correctness'' addresses the issue of verification - whether the program does what we intend it to do - rather than validation - what the client wants it to do.
``Testing can prove the presence of errors but never their absence.'' p. 69. And so we attempt to prove that errors do not exist, using logic.
``Proof of correctness generally is applied only to small and critical sections of code, rather than to the entire program.'' p. 69. I must admit that I've never done any of it in my own programming....
Assertion: condition on the variables of a program that should be verified at some given step, if the program is running correctly.
The specification of a program can be formalized as follow:
Example: (top of p. 70)
or, in this case,
The abbreviation we will use for (1) is (a so-called
Hoare triple) (named after
Tony Hoare
, developer of the Quicksort algorithm), where Q is the precondition and
R is the postcondition.
A general program is broken down into many Hoare triples, each with its precondition and postcondition - ``impressionistically'',
and P is provably correct if the implications ,
,
,
are valid.
If
Example: Practice 30, p. 72
Example: Practice 31, p. 73
If we have a Hoare triple of the form , where s is a
conditional statement of the form
then is valid provided
and
are valid.
Example: Practice 32, p. 75