CSE 331 Software Design and Implementation

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

In this episode of The Verification Corner, Rustan Leino talks about Loop Invariants. He gives a brief summary of the theoretical foundations and shows.
Reasoning About Code; Hoare Logic, continued
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Program Proving Notes Ellen L. Walker.
CSE 331 Software Design & Implementation Dan Grossman Winter 2014 Lecture 2 – Reasoning About Code With Logic 1CSE 331 Winter 2014.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Describing Syntax and Semantics
Reading and Writing Mathematical Proofs
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
Reasoning about programs March CSE 403, Winter 2011, Brun.
© Paul Ammann, 2008 Design by Contract Paul Ammann CS/SWE 332.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Lecture 3 – Reasoning About Loops 1CSE 331 Fall 2014.
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 26.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 2 Formal Reasoning.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Week 7 - Friday CS221.
Correctness issues and Loop invariants
Python: Control Structures
CSE 331 Software Design and Implementation
CSE 374 Programming Concepts & Tools
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
CSE 331 Software Design & Implementation
Formal Methods in Software Engineering 1
Lesson 9: "if-else-if" and Conditional Logic
Hoare Logic LN chapter 5, 6 but without 6. 8, 6. 12, 6
CSE 331 Software Design and Implementation
Announcements Final Exam on August 17th Wednesday at 16:00.
CSE 341: Programming Languages Section 1
Problem Solving: Brute Force Approaches
Building Java Programs
CSE 331 Software Design and Implementation
Instructor: Lilian de Greef Quarter: Summer 2017
CSE 331 Software Design and Implementation
Reasoning About Code; Hoare Logic
Lecture 5 Floyd-Hoare Style Verification
Axiomatic semantics Points to discuss: The assignment statement
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Instructor: Lilian de Greef Quarter: Summer 2017
Announcements Final Exam on August 19th Saturday at 16:00.
Semantics In Text: Chapter 3.
CSE 331 Software Design & Implementation
CSE 303 Concepts and Tools for Software Development
Chapter 6: Repetition Statements
Section 1: Code Reasoning
Axiomatic Verification I
Asymptotic complexity Searching/sorting
Predicate Transformers
Output Variables {true} S {i = j} i := j; or j := i;
Axiomatic Verification I
Binary Search and Loop invariants
Asymptotic complexity
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Searching and Sorting Hint at Asymptotic Complexity
CSE Winter 2008 Introduction to Program Verification
Developing loops from invariants
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
CS100J Nov 04, 2003 Arrays. Reading: 8
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Tutorial Number 8 - Daniel Razavi
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
Distributed Snapshots
Presentation transcript:

CSE 331 Software Design and Implementation Lecture 3 Loop Reasoning Leah Perlmutter / Summer 2018

Announcements

Announcements Casual Friday Congrats on making it through HW0 and Quiz1 Thank you TAs for making section go! Thoughts or ideas for section? Share with your sec TAs! Sorry for access issues with message board / gradescope / quiz1 TAs are working hard to straighten it all out Post on discussion board if you still have issues HW1 due Monday June 25 at 10pm Reporting collaborators You will get credit if you write something on the “Collaborators” line, can be “none” or a list of names. No credit if you leave it blank Quiz1 accidental question about future lecture will be invalidated

Follow up Answer question on Combining Rule: Conditional Finish slides from Wednesday I also got the feedback to go faster, so I practiced my lecture timing more for today

Loop Reasoning

Reasoning about loops So far, two things made all our examples much easier: When running the code, each statement executed 0 or 1 times (Therefore,) trivially the code always terminates Neither of these hold once we have loops (or recursion) Will consider the key ideas with while-loops Introduces the essential and much more general concept of an invariant Will mostly ignore prove-it-terminates; brief discussion at end

Loop-Related Questions Is this a valid Hoare Triple? {P} while(B) S; {Q} {x = y ∧ x < 10} while(x != 10) x = x+1; {x > y} Write code that does blah and prove that it is correct {R} LotsOfCode; {Q} Suppose LotsOfCode includes a loop. Proof may resemble this: {R} init; {P} while(B) S; {Q} {R}init;{P} {I} while(B) {I∧B} S {I} {I∧!B} {Q} R Somecode Q is the basic way to write a code proof. Take the code, enclose it in a Hoare Triple, and prove that the Hoare Triple is valid. Usually SomeCode won’t be trivial to prove, so we’ll need intermediate steps to write a convincing proof.

Whiteboarding! (See lecture notes) :) Lecture will skip the rest of the slides in this section and use the whiteboard instead, following an example from the lec-03-loop-reasoning-notes doc.

The Hoare logic Consider just a while-loop (other loop forms not so different) {P} while(B) S {Q} Such a triple is valid if there exists an invariant I such that: P => I invariant must hold initially {I ∧ B}S{I} body must re-establish invariant (I ∧ !B) => Q invariant must establish Q if test-is-false The loop-test B, loop-body S, and loop-invariant I “fit together”: There is often more than one correct loop, but with possibly different invariants Note definition “makes sense” even in the zero-iterations case When we are constructing a loop and proving it, P is I. If you are given a Hoare triple and asked whether it is valid, P might be something stronger than I

Need “Little Bear” Invariants If loop invariant is too strong, it could be false! Won’t be able to prove it holds either initially or after loop-body If loop invariant is too weak, it could Leave the post-condition too weak to prove what you want And/or be impossible to re-establish after the loop body This is the essence of why there is no complete automatic procedure for conjuring a loop-invariant Requires thinking (or, sometimes, “guessing”) Often while writing the code If proof doesn’t work, invariant or code or both may need work There may be multiple invariants that “work” (neither too strong nor too weak), with some easier to reason about than others Maybe this slide is good too.

Recap: Do it backwards! Start with postcondition (from spec) Write an invariant (often a weaker form of the postcondition) Implement the loop body Backward reasoning to prove {I ∧ B} S {I} Figure out B to fulfill (I ∧ !B) => Q Initialization code to make {I} true before loop header Precondition {R} {R}init;{P} {I} while(B) {I∧B} S {I} {I∧!B} {Q}

Dutch National Flag Problem

Dutch National Flag (classic) Given an array of red, white, and blue pebbles, sort the array so the red pebbles are at the front, white are in the middle, and blue are at the end [Use only swapping contents rather than “count and assign”] Edsgar Dijkstra

Pre- and post-conditions Precondition: Any mix of red, white, and blue Postcondition: Red, then white, then blue Number of each color same as in original array Mixed colors: red, white, blue Red White Blue

Some potential invariants Any of these four choices can work, making the array more-and-more partitioned as you go: Middle two slightly better because at most one swap per iteration instead of two Red White Blue Mixed Red White Mixed Blue Red Mixed White Blue Mixed Red White Blue

More precise, and code Precondition P: arr contains r reds, w whites, and b blues Postcondition: P ∧ 0 <= i <= j <= arr.size ∧ arr[0..i-1] is red ∧ arr[i..j-1] is white ∧ arr[j..arr.size-1] is blue Invariant: P ∧ 0 <= i <= j <= k <= arr.size ∧ arr[j..k-1] is unsorted ∧ arr[k..arr.size-1] is blue Exit when unsorted segment is empty, i.e. j=k Initializing to establish the invariant: i=0; j=0; k=arr.size;

The loop test and body Red White Mixed Blue i j k while(j!=k) { if(arr[j] == White) { j = j+1; } else if (arr[j] == Blue) { swap(arr,j,k-1); k = k-1; } else { // arr[j] == Red swap(arr,i,j) i = i+1; } void swap(int[] x, int y, int z) { int tmp = x[y]; x[y] = x[z]; x[z] = tmp; }

Aside: swap Reading notes write swap(a[i],a[j]) and such This is not implementable in Java But fine pseudocode Great exercise: Write a coherent English paragraph why it is not implementable in Java (i.e., does not do what you want) You can implement swap(a,i,j) in Java So previous slide and Homework 2 do it that way

In Practice ... Many loops are so “obvious” that proofs are, in practice, overkill for(String name : friends) {…} Often the intermediate state (invariant) is unclear or edge cases are tricky – use invariants here! Can draw a picture Use logical reasoning as an intellectual debugging tool What exactly is the invariant? Is it satisfied on every iteration? Are you sure? Write code to check? Did you check all the edge cases? Are there preconditions you did not make explicit? in practice, don’t need inv for every loop often it’s a tricky loop and it helps to write something down or draw a picture as you’re working it out here are some questions to ask yourself.

Termination Two kinds of loops Those we want to always terminate (normal case) Those that may conceptually run forever (e.g., web-server) So, proving a loop correct usually also requires proving termination We haven’t been proving this: might just preserve invariant forever without test ever becoming false Our Hoare triples say if loop terminates, postcondition holds How to prove termination (variants exist): Map state to a natural number somehow (just “in the proof”) Prove the natural number goes down on every iteration Prove test is false by the time natural number gets to 0

Termination examples Dutch-national-flag: size of unsorted range (k-j) Search in a linked list: length of list not yet considered Don’t know length of list, but goes down by one each time… … unless list is cyclic in which case, termination not assured

Closing

Closing Recap of announcements HW1 due Monday June 25 at 10pm Reporting collaborators You will get credit if you write something on the “Collaborators” line. Can be “none” or a list of names, but don’t leave blank Are there any general questions on HW1? Office hours today Wei: 2-3 pm Joyce: 5-6 pm