Finding Loop Invariants from Templates
Use pre-state and post-state equations to identify a useful affine invariant, then prove its base and step.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 2
- Reading time
- 1 min
In this article2 chapters
Start with what one iteration preserves
Suppose a loop starts with \(i=n\) and \(s=0\), then subtracts one from \(i\) and adds two to \(s\). Seek an invariant of the form \(ai+bs=c\). Its one-step change is \(-a+2b\), so select \(a=2,b=1\). The initial value sets \(c=2n\). Thus \(2i+s=2n\) holds at the start and survives every update.
An equality may need companion bounds
If the loop runs while \(i>0\) and \(n\ge0\), the bound \(i\ge0\) is also invariant. At exit it gives \(i=0\), so the affine equality yields \(s=2n\). An invariant template restricts the search to forms a solver can handle. The selected form may miss relations involving products or histories. Verification conditions check the candidate independently of how it was guessed. Program synthesis describes a general search over candidates with counterexample feedback.[1][2]