Predicative programming
Predicative programming is the original name of a formal method for program specification and refinement, more recently called a Practical Theory of Programming, invented by Eric Hehner. The central idea is that each specification is a binary (boolean) expression that is true of acceptable computer behaviors and false of unacceptable behaviors. It follows that refinement is just implication. This is the simplest formal method, and the most general, applying to sequential, parallel, stand-alone, communicating, terminating, nonterminating, natural-time, real-time, deterministic, and probabilistic programs, and includes time and space bounds.
Commands in a programming language are considered to be a special case of specification—those specifications that are compilable. For example, if the program variables are [math]\displaystyle{ x }[/math], [math]\displaystyle{ y }[/math], and [math]\displaystyle{ z }[/math], the command [math]\displaystyle{ x }[/math]:= [math]\displaystyle{ y }[/math]+1 is equivalent to the specification (binary expression) [math]\displaystyle{ x' }[/math]=[math]\displaystyle{ y }[/math]+1 ∧ [math]\displaystyle{ y' }[/math]=[math]\displaystyle{ y }[/math] ∧ [math]\displaystyle{ z' }[/math]=[math]\displaystyle{ z }[/math] in which [math]\displaystyle{ x }[/math], [math]\displaystyle{ y }[/math], and [math]\displaystyle{ z }[/math] represent the values of the program variables before the assignment, and [math]\displaystyle{ x' }[/math], [math]\displaystyle{ y' }[/math], and [math]\displaystyle{ z' }[/math] represent the values of the program variables after the assignment. If the specification is [math]\displaystyle{ x' }[/math]>[math]\displaystyle{ y }[/math], we easily prove ([math]\displaystyle{ x }[/math]:= [math]\displaystyle{ y }[/math]+1) ⇒ ([math]\displaystyle{ x' }[/math]>[math]\displaystyle{ y }[/math]), which says that [math]\displaystyle{ x }[/math]:= [math]\displaystyle{ y }[/math]+1 implies, or refines, or implements [math]\displaystyle{ x' }[/math]>[math]\displaystyle{ y }[/math].
Loop proofs are greatly simplified. For example, if [math]\displaystyle{ x }[/math] is an integer variable, to prove that
while [math]\displaystyle{ x }[/math]>0 do [math]\displaystyle{ x }[/math]:= [math]\displaystyle{ x }[/math]–1 od
refines, or implements the specification [math]\displaystyle{ x }[/math]≥0 ⇒ [math]\displaystyle{ x' }[/math]=0, prove
if [math]\displaystyle{ x }[/math]>0 then [math]\displaystyle{ x }[/math]:= [math]\displaystyle{ x }[/math]–1; ([math]\displaystyle{ x }[/math]≥0 ⇒ [math]\displaystyle{ x' }[/math]=0) else [math]\displaystyle{ ok }[/math] fi ⇒ ([math]\displaystyle{ x }[/math]≥0 ⇒ [math]\displaystyle{ x' }[/math]=0)
where [math]\displaystyle{ ok }[/math] = ([math]\displaystyle{ x' }[/math]=[math]\displaystyle{ x }[/math]) is the empty, or do-nothing command. There is no need for a loop invariant or least fixed point. Loops with multiple intermediate shallow and deep exits work the same way. This simplified form of proof is possible because program commands and specifications can be mixed together meaningfully.
Execution time (upper bounds, lower bounds, exact time) can be proven the same way, just by introducing a time variable. To prove termination, prove the execution time is finite. To prove nontermination, prove the execution time is infinite. For example, if the time variable is [math]\displaystyle{ t }[/math], and time is measured by counting iterations, then to prove that execution of the previous while-loop takes time [math]\displaystyle{ x }[/math] when [math]\displaystyle{ x }[/math] is initially nonnegative, and takes forever when [math]\displaystyle{ x }[/math] is initially negative, prove
if [math]\displaystyle{ x }[/math]>0 then [math]\displaystyle{ x }[/math]:= [math]\displaystyle{ x }[/math]–1; [math]\displaystyle{ t }[/math]:= [math]\displaystyle{ t }[/math]+1; ([math]\displaystyle{ x }[/math]≥0 ⇒ [math]\displaystyle{ t' }[/math]=[math]\displaystyle{ t }[/math]+[math]\displaystyle{ x }[/math]) ∧ ([math]\displaystyle{ x }[/math]<0 ⇒ [math]\displaystyle{ t' }[/math]=∞) else [math]\displaystyle{ ok }[/math] fi ⇒ ([math]\displaystyle{ x }[/math]≥0 ⇒ [math]\displaystyle{ t' }[/math]=[math]\displaystyle{ t }[/math]+[math]\displaystyle{ x }[/math]) ∧ ([math]\displaystyle{ x }[/math]<0 ⇒ [math]\displaystyle{ t' }[/math]=∞)
where [math]\displaystyle{ ok }[/math] = ([math]\displaystyle{ x' }[/math]=[math]\displaystyle{ x }[/math] ∧ [math]\displaystyle{ t' }[/math]=[math]\displaystyle{ t }[/math]).
Bibliography
- E.C.R. Hehner, a Practical Theory of Programming, Springer-Verlag 1993. Most recent edition online at a Practical Theory of Programming.
External links
Original source: https://en.wikipedia.org/wiki/Predicative programming.
Read more |