Assuming this algorithm stands alone, there is no pre condition to it.
In our notation,
represents the pre condition of the statement
on line 1. Because line 1 is the first line of code,
also
represents the pre condition of the entire algorithm.
Note that a pre condition is a condition. It expresses something that is true. How do we express true when we don't know anything is true? Well, we know something is always true, it is ``true'' itself!
Consequently, we can simply say that
.
What is the post condition of line 1? Since this is
a constant assignment statement, we know that variable must end up
with a value of 0. In other words, we know that
after this
statement. Consequently, we ``add'' the fact to
so that
If you look up the definition of conjunction,
is simply
. We can simplify our post condition so that
.
The same argument applies to line 2. Note that
because
there is nothing that can change what we know about the program between
the lines. What is
?
Line 2 changes variable to a constant of
.
We know that after line 2,
. However, this is
not the only thing that we know. What we knew earlier that
is still true. As a result,
.
Here comes the tricky part. What is
? If we
simply add the fact that
, then we end up with
.
However, this is impossible because
contradicts
. What
do we need to do?
What we need is a way to ``forget'' some facts because a new fact is in,
and the new fact may contradicts some old facts. This is accomplished by
a ``forget'' operation. let us define
to mean forgetting
all components in condition
that refers to variable
.
In our example, we can now derive
.
Literally, this means ``the post condition of line 3
is essentially the same as the post condition of line 2,
except that all components referring to
should be forgotten. Then,
we add the fact that
.''
This results in the following derivation:
![]() |
![]() |
![]() |
(1) |
![]() |
![]() |
(2) | |
![]() |
![]() |
(3) |
Now, onto the last statement. We only have to reapply what we learned in the previous step:
![]() |
![]() |
![]() |
(4) |
![]() |
![]() |
(5) | |
![]() |
![]() |
(6) |
Are you surprised at the result?
Let us consider the general case:
Assuming does not refer to variable
itself, then
.
Copyright © 2012-02-07 by Tak Auyeung