1 / 17

Program Correctness

Program Correctness. By Justin Reschke 10-5-04. Overview. Levels of Correctness Deadly Sins of Programming Importance of Correctness Proof By Induction Conclusion. Levels of Correction. 1) No syntax errors 2) No errors that can be detected at compile or run time

huela
Télécharger la présentation

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. Program Correctness By Justin Reschke 10-5-04

  2. Overview • Levels of Correctness • Deadly Sins of Programming • Importance of Correctness • Proof By Induction • Conclusion

  3. Levels of Correction • 1) No syntax errors • 2) No errors that can be detected at compile or run time • 3) There exists a set of test data for which the program will yield proper results • 4) For a typical set of test data the program will yield proper results • 5) For deliberately difficult sets of test data the program will yield proper results

  4. Levels of Correctness • 6) For all possible sets of test data that are valid according to the problem specification, the program will yields the correct answer. • 7) For all possible sets of valid test data, and for all likely conditions of erroneous input, the program gives a correct (or at least reasonable) answer • 8) For all possible input, the program gives correct or reasonable answers

  5. Levels of Correctness • Most programmers never mature beyond level 3 • Level of correctness is in the hands of the programmer • Ideally, the user would like level 8

  6. Deadly Sins of Programming • Debugging the program instead of the algorithm • Insufficient documentation • Using an inappropriate language • Relying on default conditions • Insufficient verification assertions at critical points • Not paying attention to input operations

  7. Importance of Correctness • Saves Time • Time = Money • Critical Applications • Could mean life or death.

  8. Square function: sq(n) = n^2 sq(n) { S <- 0 i <- 0 while(i < n) { S <- S + n i <- i + 1 } return S } In general, n is added n times producing n^2 To prove, we must show that after going through the loop k times, S = k*n and i = k must hold. Proving By Induction

  9. Basis Step: k = 0 This means the loop is never entered. S = 0 and i = 0 S = k*n and i = k hold true. Square function: sq(n) = n^2 sq(n) { S <- 0 i <- 0 while(i < n) { S <- S + n i <- i + 1 } return S } Proving By Induction

  10. Induction Hypothesis: For an arbitrary value m of k, S = m * n and i = m hold after going through the loop m times Inductive Step: When the loop is entered the m+1-th time, currently, S = m * n and i = m (The Induction Hypothesis). After the next loop: S = m*n+n = (m+1)*n, i = m+1 Thus S = k*n and i = k hold for any natural number k. Square function: sq(n) = n^2 sq(n) { S <- 0 i <- 0 while(i < n) { S <- S + n i <- i + 1 } return S } Proving By Induction

  11. One more thing We must prove the algorithm will terminate for any input. Easy, i begins at 0 and increases by 1 each iteration of the loop and n is a natural number. Thus i will eventually be equal to n in a finite number of steps. Square function: sq(n) = n^2 sq(n) { S <- 0 i <- 0 while(i < n) { S <- S + n i <- i + 1 } return S } Proving By Induction

  12. Factorial: fact(n)=n! i <- 1 F <- 1 while i <= n { F <- F * i i <- i + 1 } return F Calculates factorial starting from 1. 1 * 2 * 3 * 4…. * n = n! To prove, we must show that after going through the loop k times, F = k! and i = k + 1 must hold. Proof By Induction

  13. Basis Step: k = 1 The loop is entered once. F = 1 * 1=1 andi = 1+1=2 F = k! and i = k hold true. Factorial: fact(n)=n! i <- 1 F <- 1 while i <= n { F <- F * i i <- i + 1 } return F Proof By Induction

  14. Factorial: fact(n)=n! i <- 1 F <- 1 while i <= n { F <- F * i i <- i + 1 } return F Induction Hypothesis: For an arbitrary value m of k,F = m! and i = m + 1 hold after going through the loop m times. Inductive Step:When the loop is entered (m + 1)-st time, F = m! and i = (m+1) at the beginning of the loop. After the next loop:F = m! * (m+1) = F = (m+1)!i = (m+1)+1 Proof By Induction

  15. Factorial: fact(n)=n! i <- 1 F <- 1 while i <= n { F <- F * i i <- i + 1 } return F Don’t forget. We must prove the algorithm terminates after a finite number of steps. Easy again, i increases by one starting from 1 until it is 1 greater than positive integer n. Proof By Induction

  16. Conclusion • Its good to be more correct. • Program errors can mean death • Induction is a useful way to prove correctness. • An algorithm is only as good as the programmer.

  17. References • Mathematical theory of program correctness, J. W. de Bakker • The New Turing Omnibus, A.K. Dewdney • Program Correctness using Induction, http://www.cs.odu.edu/~toida/nerzic/content/induction/use_of_induction_loop.html • Program Correctness, http://courses.cs.vt.edu/~cs2704/sum02/notes/A01.ProgCorrectness.pdf

More Related