Hoare-style program verification K. Rustan M. Leino Guest lecturer Rob DeLine’s CSE 503, Software Engineering University of Washington 28 Apr 2004.

Slides:



Advertisements
Similar presentations
Hoare-style program verification K. Rustan M. Leino Guest lecturer Rob DeLines CSE 503, Software Engineering University of Washington 26 Apr 2004.
Advertisements

Hoare-style program verification K. Rustan M. Leino Guest lecturer Rob DeLines CSE 503, Software Engineering University of Washington 28 Apr 2004.
Technologies for finding errors in object-oriented software K. Rustan M. Leino Microsoft Research, Redmond, WA Lecture 1 Summer school on Formal Models.
Copyright W. Howden1 Programming by Contract CSE 111 6/4/2014.
Program correctness The State-transition model A global state S  s 0 x s 1 x … x s m {s k = local state of process k} S0  S1  S2  … Each state transition.
Discussion #33 Adjacency Matrices. Topics Adjacency matrix for a directed graph Reachability Algorithmic Complexity and Correctness –Big Oh –Proofs of.
CMPS 1371 Introduction to Computing for Engineers
FIT FIT1002 Computer Programming Unit 19 Testing and Debugging.
Using and Building an Automatic Program Verifier K. Rustan M. Leino Research in Software Engineering (RiSE) Microsoft Research, Redmond Lecture 2 LASER.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
Proving the Correctness of Algorithms Algorithm Design and Analysis Week 2
Tirgul 10 Solving parts of the first two theoretical exercises: –T1 Q.1(a,b) –T1 Q.4(a,e) –T2 Q.2(a) –T2 Q.3(b) –T2 Q.4(a,b)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Discrete Mathematics Lecture 4: Sequences and Mathematical Induction
Discrete Mathematics Lecture 4 Harper Langston New York University.
Using and Building an Automatic Program Verifier K. Rustan M. Leino Research in Software Engineering (RiSE) Microsoft Research, Redmond Lecture 1 LASER.
Hoare-style program verification K. Rustan M. Leino Guest lecturer Rob DeLine’s CSE 503, Software Engineering University of Washington 26 Apr 2004.
Invariant Based Programming, March 20101/ Teaching the construction of correct programs using invariant based programming Ralph-Johan Back Johannes Eriksson.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Lecture 4 Feb 5 completion of recursion (inserting into a linked list as last item) analysis of algorithms – Chapter 2.
1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.
Using and Building an Automatic Program Verifier K. Rustan M. Leino Research in Software Engineering (RiSE) Microsoft Research, Redmond Lecture 3 LASER.
Proving correctness. Proof based on loop invariants  an assertion which is satisfied before each iteration of a loop  At termination the loop invariant.
Lecture 4 Discrete Mathematics Harper Langston. Algorithms Algorithm is step-by-step method for performing some action Cost of statements execution –Simple.
Floyd Hoare Logic. Semantics A programming language specification consists of a syntactic description and a semantic description. Syntactic description:symbols.
SEEFM 07, Thessaloniki, Nov 20071/ Teaching the construction of correct programs using invariant based programming Ralph-Johan Back Johannes Eriksson Linda.
Sequences Informally, a sequence is a set of elements written in a row. – This concept is represented in CS using one- dimensional arrays The goal of mathematics.
CS 473Lecture 121 CS473-Algorithms I Lecture 12 Amortized Analysis.
Reading and Writing Mathematical Proofs
Analysis of Algorithms
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
Additional Problems.
Course Teacher: Moona Kanwal
Graph Definitions and Applications. Graphs: Very Useful Abstractions Graphs apply to many diverse areas: social sciences, linguistics, physical sciences,
Program Verification K. Rustan M. Leino Research in Software Engineering (RiSE) Microsoft Research, Redmond University of Washington CSE P January.
Discrete Mathematics Lecture 4 Alexander Bukharovich New York University.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Feb 18, 2005.
Using and Building an Automatic Program Verifier K. Rustan M. Leino Research in Software Engineering (RiSE) Microsoft Research, Redmond Lecture 0 Marktoberdorf.
Program correctness The State-transition model The set of global states = so x s1 x … x sm {sk is the set of local states of process k} S0 ---> S1 --->
Program correctness The State-transition model A global states S  s 0 x s 1 x … x s m {s k = set of local states of process k} S0  S1  S2  Each state.
September 9, Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Example of a Worked-out Proof. Consider again this program Let 0  n. This program checks if all elements of the segment a[0..n) are zero. {* 0  n *}
Introduction to Algorithms: Verification, Complexity, and Searching Andy Wang Data Structures, Algorithms, and Generic Programming.
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
CES 592 Theory of Software Systems B. Ravikumar (Ravi) Office: 124 Darwin Hall.
1 Section 8.2 Program Correctness (for imperative programs) A theory of program correctness needs wffs, axioms, and inference rules. Wffs (called Hoare.
1 Computer Algorithms Tutorial 2 Mathematical Induction Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 27.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
September 18, Algorithms and Data Structures Lecture II Simonas Šaltenis Aalborg University
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Proving the Correctness of Algorithms
Using and Building an Automatic Program Verifier
Data Structures: Segment Trees, Fenwick Trees
Hoare-style program verification
Axiomatic semantics Points to discuss: The assignment statement
Reasoning about Loops, Conclusion
Hoare-style program verification
Example of a Worked-out Proof
Sigma/Summation Notation
Program Verification with Hoare Logic
CSE 1020:Software Development
Presentation transcript:

Hoare-style program verification K. Rustan M. Leino Guest lecturer Rob DeLine’s CSE 503, Software Engineering University of Washington 28 Apr 2004

Review {P} skip {P} {P[w:=E]} w:=E {P} {P∧B} assert B {P} if {P} S {Q} and {Q} T {R}, then {P} S ; T {R} if {P∧B} S {R} and {P∧¬B} T {R}, then {P} if B then S else T end {R}

Loops To prove {P} while B do S end {Q} prove {P} {J} while B do {J ∧ B} {0 ≦ vf} {J ∧ B ∧ vf=VF} S {J ∧ vf<VF} end {J ∧¬B} {Q}

Example: Array sum {0≦N} k := 0; s := 0; while k ≠ N do s:=s+a[k] ; k:=k+ 1 end {s = (Σi | 0≦i<N ・ a[i])}

{0≦N} k := 0; s := 0; {J} while k ≠ N do {J ∧ k≠N} {0 ≦ vf} {J ∧ k≠N ∧ vf=VF} s:=s+a[k] ; k:=k+ 1 {J ∧ vf<VF} end {J ∧¬(k≠N)} {s = (Σi | 0≦i<N ・ a[i])} Example: Array sum {0≦N} k := 0; s := 0; while k ≠ N do s:=s+a[k] ; k:=k+ 1 end {s = (Σi | 0≦i<N ・ a[i])}

Example: Array sum {0≦N} k := 0; s := 0; {J} while k ≠ N do {J ∧ k≠N} {0 ≦ vf} {J ∧ k≠N ∧ vf=VF} s:=s+a[k] ; k:=k+ 1 {J ∧ vf<VF} end {J ∧¬(k≠N)} {s = (Σi | 0≦i<N ・ a[i])}

Example: Array sum {0≦N} k := 0; s := 0; {J} while k ≠ N do {J ∧ k≠N} {0 ≦ vf} {J ∧ k≠N ∧ vf=VF} s:=s+a[k] ; k:=k+ 1 {J ∧ vf<VF} end {J ∧ k=N} {s = (Σi | 0≦i<N ・ a[i])} J: s = (Σi | 0≦i<k ・ a[i]) ∧ 0 ≦ k ≦ N vf: N-k

{0≦N} {0 = (Σi | 0≦i<0 ・ a[i]) ∧ 0≦0≦N} k := 0; {0 = (Σi | 0≦i<k ・ a[i]) ∧ 0≦k≦N} s := 0; {s = (Σi | 0≦i<k ・ a[i]) ∧ 0≦k≦N} Example: Array sum — Initialization

Example: Array sum — Invariance {s = (Σi | 0≦i<k ・ a[i]) ∧ 0≦k≦N ∧ k≠N ∧ N-k=VF} {s+a[k] = (Σi | 0≦i<k ・ a[i])+a[k] ∧ 0≦k<N ∧ N-k- 1 <VF} s := s + a[k]; {s = (Σi | 0≦i<k ・ a[i])+a[k] ∧ 0≦k<N ∧ N-k- 1 <VF} {s = (Σi | 0≦i<k+ 1 ・ a[i]) ∧ 0≦k+ 1 ≦N ∧ N-(k+ 1 )<VF} k := k+ 1 ; {s = (Σi | 0≦i<k ・ a[i]) ∧ 0≦k≦N ∧ N-k<VF}

In-class exercise: computing cubes {0≦N} k := 0; r := 0; s := 1; t := 6; while k≠N do a[k] := r; r := r + s; s := s + t; t := t + 6; k := k + 1 end {(∀i | 0≦i<N ・ a[i] = i 3 )}

Computing cubes — Guessing the invariant From the postcondition (∀i | 0≦i<N ・ a[i] = i 3 ) and the negation of the guard k=N guess the invariant (∀i | 0≦i<k ・ a[i] = i 3 ) ∧ 0≦k≦N From this invariant and variant function N-k, it follows that the loop terminates

Computing cubes — Maintaining the invariant while k≠N do {(∀i | 0≦i<k ・ a[i] = i 3 ) ∧ 0≦k≦N ∧ k≠N} {(∀i | 0≦i<k ・ a[i] = i 3 ) ∧ r=k 3 ∧ 0≦k<N} a[k] := r; r := r + s; s := s + t; t := t + 6; {(∀i | 0≦i<k ・ a[i] = i 3 ) ∧ a[k]=k 3 ∧ 0≦k<N} {(∀i | 0≦i<k+ 1 ・ a[i] = i 3 ) ∧ 0≦k+ 1 ≦N} k := k + 1 {(∀i | 0≦i<k ・ a[i] = i 3 ) ∧ 0≦k≦N} end Add this to the invariant, and then try to prove that it is maintained

Computing cubes — Maintaining the invariant while k≠N do {r = k 3 ∧ …} {r + s = k 3 + 3*k 2 + 3*k + 1} a[k] := r; r := r + s; s := s + t; t := t + 6; {r = k 3 + 3*k 2 + 3*k + 1} {r = (k+ 1 ) 3 } k := k + 1 {r = k 3 } end Add s = 3*k 2 + 3*k + 1 to the invariant, and then try to prove that it is maintained

Computing cubes — Maintaining the invariant while k≠N do {s = 3*k 2 + 3*k + 1 ∧ …} {s + t = 3*k 2 + 6*k *k } a[k] := r; r := r + s; s := s + t; t := t + 6; {s = 3*k 2 + 6*k *k } {s = 3*(k+1) 2 + 3*(k+1) + 1} k := k + 1 {s = 3*k 2 + 3*k + 1} end Add t = 6*k + 6 to the invariant, and then try to prove that it is maintained

Computing cubes — Maintaining the invariant while k≠N do {t = 6*k + 6 ∧ …} {t + 6 = 6*k } a[k] := r; r := r + s; s := s + t; t := t + 6; {t = 6*k } {t = 6*(k+1) + 6} k := k + 1 {t = 6*k + 6} end

Computing cubes — Establishing the invariant {0≦N} {(∀i | 0≦i<0 ・ a[i] = i 3 ) ∧0≦0≦N ∧ 0 = 0 3 ∧ 1 = 3* *0 + 1 ∧ 6 = 6*0 + 6} k := 0; r := 0; s := 1; t := 6; {(∀i | 0≦i<k ・ a[i] = i 3 ) ∧ 0≦k≦N ∧ r = k 3 ∧ s = 3*k 2 + 3*k + 1 ∧ t = 6*k + 6}

In-class exercise: computing cubes — Answers Invariant: (∀i | 0≦i<k ・ a[i] = i 3 ) ∧ 0 ≦ k ≦ N ∧ r = k 3 ∧ s = 3*k 2 + 3*k + 1 ∧ t = 6*k + 6 Variant function: N-k

Other common invariants k is the number of nodes traversed so far the current value of n does not exceed the initial value of n all array elements with an index less than j are smaller than x the number of processes whose program counter is inside the critical section is at most one the only principals that know the key K are A and B

Belgian chocolate How many breaks do you need to make 50 individual pieces from a 10x5 Belgian chocolate bar? Note: Belgian chocolate is so thick that you can't break two pieces at once. Invariant: #pieces = 1 + #breaks

Loop proof obligations — a closer look To prove {P} while B do S end {Q} find invariant J and variant function vf such that: – invariant initially: P ⇒ J – invariant maintained: {J ∧ B} S {J} – invariant sufficient: J ∧¬B ⇒ Q – vf well-founded – vf bounded: J ∧ B ⇒ 0 ≦ vf – vf decreases: {J ∧ B ∧ vf=VF} S {vf<VF} Are all of these conditions needed?

{0≦N} k := N; s := 0; {J} while k ≠ N do {J ∧ k≠N} {0 ≦ vf} {J ∧ k≠N ∧ vf=VF} s:=s+a[k] ; k:=k+ 1 {J ∧ vf<VF} end {J ∧¬(k≠N)} {s = (Σi | 0≦i<N ・ a[i])} J: s = (Σi | 0≦i<k ・ a[i]) ∧ 0≦k≦N vf: N-k Loop proof obligations — invariant holds initially

{0≦N} k := 0; s := 0; {J} while k ≠ N do {J ∧ k≠N} {0 ≦ vf} {J ∧ k≠N ∧ vf=VF} s:=s+a[k] ; k:=k+ 2 {J ∧ vf<VF} end {J ∧¬(k≠N)} {s = (Σi | 0≦i<N ・ a[i])} J: s = (Σi | 0≦i<k ・ a[i]) ∧ 0≦k≦N vf: N-k Loop proof obligations — invariant is maintained

Loop proof obligations — invariant is sufficient {0≦N} k := 0; s := 0; {J} while k ≠ N do {J ∧ k≠N} {0 ≦ vf} {J ∧ k≠N ∧ vf=VF} k:=k+ 1 {J ∧ vf<VF} end {J ∧¬(k≠N)} {s = (Σi | 0≦i<N ・ a[i])} J: 0≦k≦N vf: N-k

Loop proof obligations — variant function is well-founded {0≦N} k := 0; s := 0; r := 1.0 ; {J} while k ≠ N do {J ∧ k≠N} {0 ≦ vf} {J ∧ k≠N ∧ vf=VF} r := r / 2.0 ; {J ∧ vf<VF} end {J ∧¬(k≠N)} {s = (Σi | 0≦i<N ・ a[i])} J: s=(Σi | 0≦i<k ・ a[i]) ∧ 0≦r vf: r

Loop proof obligations — variant function is bounded {0≦N} k := 0; s := 0; {J} while k ≠ N do {J ∧ k≠N} {0 ≦ vf} {J ∧ k≠N ∧ vf=VF} k:=k- 1 {J ∧ vf<VF} end {J ∧¬(k≠N)} {s = (Σi | 0≦i<N ・ a[i])} J: s = (Σi | 0≦i<k ・ a[i]) ∧ k≦N vf: k

Loop proof obligations — variant function decreases {0≦N} k := 0; s := 0; {J} while k ≠ N do {J ∧ k≠N} {0 ≦ vf} {J ∧ k≠N ∧ vf=VF} skip {J ∧ vf<VF} end {J ∧¬(k≠N)} {s = (Σi | 0≦i<N ・ a[i])} J: s = (Σi | 0≦i<k ・ a[i]) ∧ 0≦k≦N vf: N-k

Ranges in invariants {0≦N} k := 0; s := 0; {J} while k ≠ N do {J ∧ k≠N} {0 ≦ vf} {J ∧ k≠N ∧ vf=VF} s:=s+a[k] ; k:=k+ 1 {J ∧ vf<VF} end {J ∧¬(k≠N)} {s = (Σi | 0≦i<N ・ a[i])} J: s = (Σi | 0≦i<k ・ a[i]) ∧ 0≦k≦N vf: N-k Where are these used?

Ranges: lower bound {s = (Σi | 0≦i<k ・ a[i]) ∧ 0≦k≦N ∧ k≠N ∧ N-k=VF} {s+a[k] = (Σi | 0≦i<k ・ a[i])+a[k] ∧ 0≦k<N ∧ N-k- 1 <VF} s := s + a[k]; {s = (Σi | 0≦i<k ・ a[i])+a[k] ∧ 0≦k<N ∧ N-k- 1 <VF} {s = (Σi | 0≦i<k+ 1 ・ a[i]) ∧ 0≦k+ 1 ≦N ∧ N-(k+ 1 )<VF} k := k+ 1 ; {s = (Σi | 0≦i<k ・ a[i]) ∧ 0≦k≦N ∧ N-k<VF} This step uses 0≦k

Ranges: upper bound {0≦N} k := 0; s := 0; {J} while k < N do {J ∧ k≠N} {0 ≦ vf} {J ∧ k≠N ∧ vf=VF} s:=s+a[k] ; k:=k+ 1 {J ∧ vf<VF} end {J ∧¬(k<N)} {s = (Σi | 0≦i<N ・ a[i])} J: s = (Σi | 0≦i<k ・ a[i]) ∧ 0≦k≦N vf: N-k this step still needs k≦N Even with < instead of ≠