{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lec10_Find_Inv - Illinois Institute of Technology Notes for...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Writing Correct Programs; Finding Invariants CS 536 Notes, Lecture 10, Mon Nov 8, 2010 A. Why It is easier to write good programs and check them for defects than to write bad programs and then fi x them. The hardest part of programming is fi nding good loop invariants. There are heuristics for fi nding them but no algorithms that work in all cases. B. Outcomes After this lecture, you should Know how to generate possible invariants using the techniques “replace a constant by a variable” and “deleting a conjunct”. C. Writing Correct Programs It is easier to write good programs and check them for defects than to write bad programs and then fi x them. Know what speci fi cations we need to meet. Use the Hoare logic rules and wp to help write correct programs in a goal-oriented way (backwards from postconditions). Write statements only when we know how they’ll help us. For while loops, take the loop { P } S ; { inv P } { bd t } while B do S od { R }. There are fi ve things we need to know to verify that a loop does what it’s supposed to: 1. { P } S { P } Initialization establishes the loop invariant. 2. { P B } S { P } The loop body preserves the loop invariant. 3. P ¬ B R Upon exit, the loop achieves the postcondition. 4. P t 0 The bound function t has a lower bound. 5. { P B t = z } S { t < z } Executing the loop body makes progress towards termination. So to write a loop, we need to fi nd an invariant, loop test, initialization, loop body, and bound function. We need to fi nd an invariant and loop test that establishes the desired postcondition. { inv P } while B do ??? od { P ¬ B } { R } The invariant should be easy to establish with some easy initialization code S . { P } S { P } The body of the loop needs to make progress toward termination by executing some code S b to decrease the bound function. We may need some other code S to take the state from P B to wp ( S b , P ). Illinois Institute of Technology Notes for Lecture 10 CS 536: Science of Programming - 1 of 10 - © James Sasaki, 2010
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
• ... while do { P B } { wp ( S ; S b , P )} S ; { wp ( S b , P )} S b { P } od ... E.g., take this version of the summation loop: { inv P }{ bd n-i } while i n do { P i n } { P } s := s+i+1; { P } i := i+1 { P } od , where P 0 i n s = sum(0, i) , P 0 i+1 n s = sum(0, i +1) , and P   0 i+1 n s+i+1 = sum(0, i+1) . Note ( P i n R ), so we know the loop is correct. •There are other ways to write the loop body, of course. If P B doesn’t imply wp ( S b , P ), we can strengthen it by using an if B where P B B wp ( S b , P ). •... do { P B } if B then { P B B } { wp ( S b , P )} S b { P } else ... od ..., or •... do { P B } if B then { P B B } { wp ( S ; S b , P )} S ; S b { P } else ... od (Of course, now we have to worry about what to do when B is false.) D. Finding Invariants The hardest part of writing correct programs involves fi nding invariants for our loops.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern