Checking a Loop Invariant Before the First Step

Initialization establishes that a loop invariant holds even when the body has run zero times.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Trace an empty prefix
  2. Exercise no iterations

Trace an empty prefix

A total initialized to zero equals the sum of the first zero observed entries. If initialization used one, the same claim would already be false. See loop accumulators.

Exercise no iterations

For an empty input, the invariant and exit rule may already determine the result. Include the case in tests. Worked loop invariants illustrate the full proof shape.[1]

References

  1. Benjamin C. Pierce et al., Software Foundations ↩