|
we are not to worry about the invariant, because it does not apply?
No. You must make sure that the invariant is true. It applies to all states entering the body of the loop. And if you write the right loop, it will make my invariant true in all cases, trivial or otherwise.
Is that what you mean when you say that something is trivially true?
Not at all. When something is ``trivially true'' it is true because some component is trivial. So
For all I s.t. 1<I<0, I is both even and odd
is trivially true, because the interval [1..0] is trivial (empty). There is no I in that interval, so everything I can say about all such I is true.
Getting back to our particular problem:
- For the empty array, we have N=0, so the interval [1..N] is the empty interval, which we often call ``trivial.'' There is only one possible value of B such that 0<=B<=N. For that value of B, the problem statement is true. In this case, the invariant comes out true for all values of L and U, but there is one particular choice of values that is natural and uniform with the less trivial cases.
- When T<X[1], then B must be 0. That value of B makes the problem statement true, and no other value does. The first half of the problem statement
X[I]<=T for all I s.t. 1<=I<=B
is ``trivially'' true, because there is no I in the interval [1..B]. A correctly written loop will home in on B=0 keeping the invariant true at every step.
- Similarly, when T>X[N], then the unique value for B that makes the problem statement true is B=N, and the second half of the problem statement is ``trivially'' true in this case. A correct loop homes in on that value, keeping the invariant true at every step.
If the idea of a formula being true because it says something about all numbers in an empty interval bothers you, compare it to the weird but true sentence
Whenever you find a creature that does not exist, that creature will tell you a number that is both odd and even.
Mike O'D.
|