1 / 8

4.5 Program Correctness

4.5 Program Correctness. Definition 1: A program, or program segment, S is said to be partially correct with respect to the initial assertion p and the final assertion q if whenever p is true for the input values of S and S terminates, then q is true for the output values of S.

willis
Download Presentation

4.5 Program Correctness

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 4.5 Program Correctness • Definition 1: A program, or program segment, S is said to be partially correct with respect tothe initial assertion p and the final assertion q if whenever p is true for the input values of S and S terminates, then q is true for the output values of S. • The notation p{S}q indicates that the program , or program segment, S is partially correct with respect to the initial assertion p and the final assertion q.

  2. Program Verification • Example 1: show that the program segment y := 2 z := x+y is correct with respect to the initial assertion p :x=1 and the final assertion q :z=3.

  3. Rules of Inference • Suppose that the program S is split into subprograms S1 and S2. Write S=S1; S2 to indicate that S is made up of S1 followed by S2. • Suppose that the correctness of S1 with respect to the initial assertion p and final assertion q, and the correctness of S2 with respect to the initial assertion q and the final assertion r, have been established. • It follows that if p is true and S1 is executed and terminates, then q is true; and if q is true, and S2 executes and terminates, then r is true. • This rule of inference, called the composition rule, can be stated as p{S1}q q{S2}r ∴ p{S1 ;S2}r

  4. Conditional Statements Ifcondition then S ,where S is a block of statements. • First , it must be shown that when p is true and condition is also true, then q is true after S terminates. • Example 2: Verify that the program segment if x > y then y := x is correct with respect to the initial assertion T and the final assertion yx.

  5. Conditional Statements • Suppose that a program has a statement of the form If condition then S1 else S2 (pcondition){S1}q (pcondition){S2}q p{if condition then S1 else S2}q • Example 3: verify that the program segment If x < 0 then abs := -x else abs := x is correct with respect to the initial assertion T and the final assertion abs =|x|.

  6. Loop Invariants While condition S • S is repeatedly executed until condition untel condition becomes false. • An assertion that remains true each time S is executed must be chosen. • Such an assertion is called a loop invariant. • In other words, p is a loop invariant if (pcondition){S}p is true. (pcondition){S}p p{while condition S}(conditionp)

  7. Loop Invariants • Example 4: A loop invariant is needed to verify that the program segment i:=1 factorial :=1 while i < n begin i :=i+1 factorial := factorial.i end Terminates with factorial = n! where n is a positive integer.

  8. Example 5: we will outline how to verify the correctness of the program S for computing the product of two integers. The goal is to prove that after S is executed, product has the value mn. Procedure multiply(m,n : integers)

More Related