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 *}

Slides:



Advertisements
Similar presentations
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
Advertisements

AE1APS Algorithmic Problem Solving John Drake
PROOF BY CONTRADICTION
If R = {(x,y)| y = 3x + 2}, then R -1 = (1) x = 3y + 2 (2) y = (x – 2)/3 (3) {(x,y)| y = 3x + 2} (4) {(x,y)| y = (x – 2)/3} (5) {(x,y)| y – 2 = 3x} (6)
Reasoning About Code; Hoare Logic, continued
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
MA/CSSE 473/474 How (not) to do an induction proof.
The Engineering Design of Systems: Models and Methods
>In We need a rule that will let us prove a conditional. What should the rule be?
Axiomatic Semantics The meaning of a program is defined by a formal system that allows one to deduce true properties of that program. No specific meaning.
Program Proving Notes Ellen L. Walker.
1/22 Programs : Semantics and Verification Charngki PSWLAB Programs: Semantics and Verification Mordechai Ben-Ari Mathematical Logic for Computer.
Program Analysis and Verification Spring 2014 Program Analysis and Verification Lecture 5: Axiomatic Semantics II Roman Manevich Ben-Gurion University.
Copyright © Cengage Learning. All rights reserved.
Sorting. Input: A sequence of n numbers a 1, …, a n Output: A reordering a 1 ’, …, a n ’, such that a 1 ’ < … < a n ’
Hoare-style program verification K. Rustan M. Leino Guest lecturer Rob DeLine’s CSE 503, Software Engineering University of Washington 28 Apr 2004.
1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.
Proving correctness. Proof based on loop invariants  an assertion which is satisfied before each iteration of a loop  At termination the loop invariant.
Floyd Hoare Logic. Semantics A programming language specification consists of a syntactic description and a semantic description. Syntactic description:symbols.
Proving Program Correctness The Axiomatic Approach.
Index FAQ Limits of Sequences of Real Numbers Sequences of Real Numbers Limits through Rigorous Definitions The Squeeze Theorem Using the Squeeze Theorem.
Reading and Writing Mathematical Proofs
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
CSE 311 Foundations of Computing I Lecture 8 Proofs and Set Theory Spring
Slide 1 Propositional Definite Clause Logic: Syntax, Semantics and Bottom-up Proofs Jim Little UBC CS 322 – CSP October 20, 2014.
Equations Reducible to Quadratic
TABLES AND VALUES Section 1.5. Open Sentence Equation.
September 17, 2001 Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Copyright © Cengage Learning. All rights reserved. CHAPTER 8 RELATIONS.
Recursive Algorithms &
CSE 311 Foundations of Computing I Lecture 9 Proofs and Set Theory Autumn 2012 CSE
Formal Proofs and Boolean Logic Chapter 6 Language, Proof and Logic.
1 Software Correctness Proofs CIS 376 Bruce R. Maxim UM-Dearborn.
CSE 311: Foundations of Computing Fall 2013 Lecture 8: Proofs and Set theory.
Section 1.4. Propositional Functions Propositional functions become propositions (and have truth values) when their variables are each replaced by a value.
What have you learned in this lecture until now? Circuit theory Undefined quantities ( , ) Axioms ( , ) Two.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Hoare Logic LN chapter 5, 6 but without 6.8, 6.12, 6.13 (to be discussed later) Hoare Logic is used to reason about the correctness of programs. In the.
11/22/2016IT 3271 A formal system:Axioms and Rules, for inferring valid specification x := m; y := n; while ¬(x=y) do if x>y then x := x-y else y := y-x.
Kirchhoff’s Laws.
Further with Hoare Logic Sections 6.12, 6.10, 6.13
Reasoning About Code.
Revisiting Predicate Logic LN chapters 3,4
Proving Loops Testing debugging and verification
Hoare Logic LN chapter 5, 6 but without 6. 8, 6. 12, 6
Axiomatic semantics Points to discuss: The assignment statement
Proving correctness.
Divide and Conquer (Merge Sort)
Axiomatic Verification II
Ampere’s Law Just for kicks, let’s evaluate the line integral along the direction of B over a closed circular path around a current-carrying wire. I B.
Hoare Logic LN chapter 5, 6 Hoare Logic is used to reason about the correctness of programs. In the end, it reduces a program and its specification to.
Some heuristics for deriving invariant LN sections 6.7 , 6.8
Example of a Worked-out Proof
Axiomatic Verification II
Output Variables {true} S {i = j} i := j; or j := i;
More Mathematical Reasoning (Conditional Statements)
Binary Search and Loop invariants
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
Program Verification with Hoare Logic
CSE Winter 2008 Introduction to Program Verification
How does the point (2, 4) change as a result of the transformation
Equations Reducible to Quadratic
Use the Chain Rule to find {image} where t = 5, u = 3, v = 0. {image}
Quicksort Quick sort Correctness of partition - loop invariant
Tutorial Number 8 - Daniel Razavi
Solving Quadratics EQ: How do you solve quadratic inequalities algebraically? M2 Unit 1C: Day 7.
Hoare Logic LN chapter 5, 6, 9.3 Hoare Logic is used to reason about the correctness of programs. In the end, it reduces a program and its specification.
Algorithms and Data Structures Lecture II
Program Correctness an introduction.
Presentation transcript:

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 *} i := 0 ; r := true ; while i<n do { r := r /\ (a[i]=0) ; i++ } {* ?? *} 2

Proof rule for loop (total correctness) {* g /\ I *} S {* I *} // invariance I /\  g  Q // exit cond {* I /\ g *} C:=m; S {* m 0 // m bounded below {* I *} while g do S {* Q *} 3

Formal spec, inv, and term. metric {* 0  n *} i := 0 ; r := true ; while i<n do { r := r /\ (a[i]=0) ; i++ } {* r = (  k : 0  k<n : a[k]=0) *} Chosen invariant & termination metric: I : (r = (  k : 0  k<i : a[k]=0)) /\ 0  i  n m : n - i 4 I1I1 I2I2

It comes down to proving these (Exit Condition) I /\  g  Q (Initialization Condition) {* given-pre-cond *} i := 0 ; r := true {* I *}, Equivalently : given-pre-cond  wp (i := 0 ; r := true) I (Invariance) I /\ g  wp body I (Termination Condition) I /\ g  wp (C:=m ; body) (m<C) (Termination Condition) I /\ g  m>0 5

Proof of the exit condition PROOF PEC [A1] r = (  k : 0  k<i : a[k]=0) // I1 [A2] 0  i  n // I2 [A3] i  n //  g [G] r = (  k : 0  k<n : a[k]=0) 1. { A2 and A3 } i = n 2. { rewrite A1 with 1 } r = (  k : 0  k<n : a[k]=0) END 6

Proof of the initialization condition Calculate the wp first : wp (i := 0 ; r := true ) I =..... PROOF Init [A1] 0  n [ G ] (true = (  k : 0  k<0 : a[k]=0)) /\ 0  0  n // calculated wp 1. { 0  0, conjunction with A1 } 0  0  n 2. { see the eq. sub proof below } true = (  k : 0  k<0 : a[k]=0) EQUATIONAL PROOF (  k : 0  k<0 : a[k]=0) = { the domain is empty } (  k : false : a[k]=0) = {  over empty domain } true END 3. { conjunction of 1 and 2 } G 7

Proof of the invariance condition We have to prove: I /\ g  wp body I Calculate the wp first: wp (r := r /\ (a[i]=0) ; i++ ) I =.... Proof structure: PROOF PIC [A1] r = (  k : 0  k<i : a[k]=0) // I1 [A2] 0  i  n // I2 [A3] i<n // g [G].... here the wp you calculated above 8

Proof of the invariance condition PROOF PIC [A1] r = (  k : 0  k<i : a[k]=0) [A2] 0  i  n [A3] i<n [G1] r/\(a[i]=0) = (  k : 0  k<i+1 : a[k]=0) [G2] 0  i+1  n 1. { follows from 0  i in A2 } 0  i+1 2. { follows from A3 } i+1  n 3. { see eq. subproof below } G1 EQUATIONAL PROOF (  k : 0  k<i+1 : a[k]=0) = { dom. merge, PIC.A2 } (  k : 0  k<i \/ k=i : a[k]=0) = {  domain-split } (  k : 0  k<i: a[k]=0) /\ (  k : k=i : a[k]=0) = { PIC.A1 } r /\ (  k : k=i : a[k]=0) = { quant. over singleton } r /\ (a[i]=0) END 4. { conjunction of 1,2,3} G END 9

Proof that m decreases You have to prove: I /\ g  wp (C:=m ; body) (m<C) Calculate this first: wp (C:=n-i ; r := r /\ (a[i]=0) ; i++ ) (n-i<C) =.... PROOF PTC1 [A1] r = (  k : 0  k<i : a[k]=0) // I1 [A2] 0  i  n // I2 [A3] i<n // g [G] n-(i+1) < n-i 1. { x-1 < x } n – i - 1 < n-i 2. { rewriting 1 } n – (i+1) < n-1 END 10

Proof that m has a lower bound You have to prove: I /\ g  m  0 PROOF PTC2 [A1] r = (  k : 0  k complete this yourself. 11

Let’s take a look at the version with a break {* 0  n *} i := 0 ; r := true ; while i<n /\ r do { r := r /\ (a[i]=0) ; i++ } {* r = (  k : 0  k<n : a[k]=0) *} Let’s just use the same invariant & termination metric: I : (r = (  k : 0  k<i : a[k]=0)) /\ 0  i  n m : n - i 12 let’s call this g /\ h

Ok.. so what do we have to prove ? (Exit Condition) I /\  (g /\ h)  Q (Initialization Condition) given-pre-cond  wp (i := 0 ; r := true) I (Invariance) I /\ g /\ h  wp body I (Termination Condition) I /\ g /\ h  wp (C:=m ; body) (m<C) (Termination Condition) I /\ g /\ h  m>0 13

Patched proof of the exit condition PROOF PEC [A1] r = (  k : 0  k<i : a[k]=0) // I1 [A2] 0  i  n // I2 [A3] i  n \/  r //  g [G] r = (  k : 0  k<n : a[k]=0) 1. { see subproof1} i  n  G 2. { see subproof2}  r  G 3. { case-split on A3, using 1 and 2 } G END 14

The Subproofs of PEC PROOF sub2 [A1]  r [G] r = (  k : 0  k<n : a[k]=0) 1. { rewrite A1 with PEC.A1}  (  k : 0  k<i : a[k]=0) 2. { neg of  on 1 } (  k : 0  k<i : a[k]  0) 3. {  elim on 2} [SOME k] 0  k<i /\ a[k]  0 4. {PEC.A2 says i  n, so 3 implies this: } 0  k<n /\ a[k]  0 5. {  intro on 4 } (  k : 0  k<n : a[k]  0) 6. { neg of  on 5 }  (  k : 0  k<n : a[k]=0) 7. {A1 and 6 }  r =  (  k : 0  k<n : a[k]=0) 8. { follows from 7 } r = (  k : 0  k<n : a[k]=0) END 15 P, Q P = Q