1Loop Invariants
1. Invariant means that the relationship given remains true every time control hits
the location. Not that nothing changes. E.g., x > y + 2 can be an invariant, even
though both x and y change.
2. Every loop has an assertion placed within the loop. This assertion is expected to
be true every time control reaches it. It is called a loop invariant.
3. Does such an assertion always exist? Yes. Meta-proof.
4. For while-loops, the traditional location for the loop invariant is just-left-of the
Boolean expression.
5. ./assertions-in-the-small.html .has several examples with loop invariants. Focus
on the (i) Mark Error Positions, (ii) Next Permutation problem.
6. ../Examples/Mark-Error-Pos/index.html ."Thos broplem is peenuds." by Niklaus
Wirth, 1984 Turing Award Winner.
2Class Invariants
1. OOP Classes and Abstract Data Types are strongly coupled. This coupling is
best exhibited through class invariants.
2. Every class has an assertion that describes the relationships among its data
members and public methods. This called the class invariant.
2.1Public Methods
1. Focus on public methods. Ignore, for now, Private and protected.
2. Each of these public methods M has a pre P and a post Q. Together this is
a.contract.
3. We can split P up as P1 and P2; similarly, Q is split up as Q1 and Q2.
4. A split exists so that P1 = Q1.
5. This P1 = Q1 = CI is the class invariant. We prefer this to be the strongest
possible invariant.
6. Note that A and B is stronger than just A.
2.2Constructors
1. Every constructor must have as its post the class invariant.
2. The pre of a constructor may not obey the class invariant.