Hoare logic proves properties of programs through Hoare triples . A precondition constrains initial states and a postcondition constrains returned states. The basic rules establish partial correctness; termination requires a further argument.[ref]
For a program expression whose evaluation is defined and has no side effects, the assignment rule isThe substitution replaces the free occurrences of in the desired postcondition. For example, obtaining after
x := x+1 requires beforehand, so suffices. Integer overflow must be excluded or handled by the stated arithmetic model.Sequential composition joins a first postcondition to the next precondition. For a while loop, proving establishesHere is the loop invariant. For
while x > 0 do x := x-1, starting with an exact nonnegative integer, is preserved and the false exit guard yields . The decreasing nonnegative integer is also a loop variant, establishing total correctness for this example.[ref] New to topics? Read the docs here!