Define the
state of a
program (e.g. n-tuple of values of all variables, special value is
needed for undefined variables).
Condition (assertion) is a proposition about the program
state (true or false).
Conditions can be combined using logic operations of the
sentential calculus (negation, conjunction, disjunction,
,,,).
Precondition is a condition that holds before the execution
of a program.
Postcondition is a condition that holds after the execution
of a program.
External specification of a program is a pair
(Precondition, Postcondition).
Example
Theorem of total correctness:
If the program starts the execution in a state that conforms to
precondition, then the program finishes the execution in a state,
that conforms to postcondition after finite number of steps
(halting is important!).
Algorithm is
(totally) correct (with regard to a given
specification) if it is possible to prove the theorem of (total)
correctness.
To prove the correctness w.r.t. specification (P, Q) we construct
the
weakest precondition Wp that is deducible from each
precondition P.
We use the postcondition Q and structural rules for the algorithm
to construct the Wp
:
P => Wp && for the pair (Wp, Q) the (total) correctness
can be proved.
Structural rules:
Empty algorithm - program
state does not change.
Superposition (sequence of
steps): postcondition of the first step is precondition of
the second step.
Assignment x:=e: replace all occurrences of the variable x
in the postcondition Q by expression e (evaluation of e must be
free from side-effects).
If-statement: branches have the same postconditon Q,
precondition is combined from preconditions of the branches and
if-condition.
if (t) A1
else A2;
(t && Wp1|| !t && Wp2, Q)
For loops we need a notion of
partial correctness: if the
program starts the execution in a state that conforms to
precondition, then the program finishes the execution in a state,
that conforms to the postcondition or the execution does not
stop at all (program does not halt).
Halting theorem: if the program starts the execution in a
state that conforms to precondition, then the program finishes the
execution after finite number of steps.
Total correctness = Partial correctness + Halting
Loop invariant: condition that holds before each step of
the loop.
while-loop:
while(t) A;
loop invariant is R
- for each step A: {t&&R}A{R}
- !t && R => Q (after execution
the postcondition Q holds)
- P => R (before execution the invariant holds)
To prove the total correcnes of a loop we need to construct the
loop invariant and prove both partial correctness theorem and
halting theorem.
Example: FPP
prover
http://psc.informatik.uni-jena.de/fpp/fpp-main.htm
FPP (Frege Program Prover) University of Jena, Germany