Program Verification Using Hoares Logic Book: Chapter 7.

Slides:



Advertisements
Similar presentations
1.1 Line Segments, Distance and Midpoint
Advertisements

3.6 PARALLEL LINES IN THE COORDINATE PLANE 1 m = GOAL
5.3 Inverse Function.
Technologies for finding errors in object-oriented software K. Rustan M. Leino Microsoft Research, Redmond, WA Lecture 1 Summer school on Formal Models.
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Calculating Slope m = y2 – y1 x2 – x1.
Program verification: flowchart programs
Program verification: flowchart programs Book: chapter 7.
Verification with Array Variables Book: Chapter 7.2.
1 Program verification: flowchart programs (Book: chapter 7)
Program verification: flowchart programs Book: chapter 7.
Numbers & Geometry Points and Distances. 6/3/2013 Numbers and Geometry 2 Distance d between numbers a and b d = Example: | = Points and Distances.
Gradient of a straight line x y 88 66 44 2 44 4 For the graph of y = 2x  4 rise run  = 8  4 = 2 8 rise = 8 4 run = 4 Gradient = y.
PZ03D Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03D - Program verification Programming Language Design.
Copyright , Doron Peled and Cesare Tinelli. These notes are based on a set of lecture notes originally developed by Doron Peled at the University.
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Reasoning About Code; Hoare Logic, continued
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
This Week Finish relational semantics Hoare logic Interlude on one-point rule Building formulas from programs.
Pre and Post Condition Rules Definition : If R and S are two assertions, then R is said to be stronger than S if R -> S (R implies S). –Example : the assertion.
Axiomatic Verification I Prepared by Stephen M. Thebaut, Ph.D. University of Florida Software Testing and Verification Lecture 17.
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.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Predicate Transformers
Announcements We are done with homeworks Second coding exam this week, in recitation –Times will be posted later today –If in doubt, show up for your regular.
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 © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535.
Software Verification Bertrand Meyer Chair of Software Engineering Lecture 2: Axiomatic semantics.
Describing Syntax and Semantics
Discrete Structures Lecture 11 Implication II 1.
Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Proving Program Correctness The Axiomatic Approach.
Proving Program Correctness The Axiomatic Approach.
Reading and Writing Mathematical Proofs
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
CSI 3125, Axiomatic Semantics, page 1 Axiomatic semantics The assignment statement Statement composition The "if-then-else" statement The "while" statement.
Program Analysis and Verification Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics I Roman Manevich Ben-Gurion University.
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen.
13 Aug 2013 Program Verification. Proofs about Programs Why make you study logic? Why make you do proofs? Because we want to prove properties of programs.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 26.
Cs7100(Prasad)L18-9WP1 Axiomatic Semantics Predicate Transformers.
1 Section 8.2 Program Correctness (for imperative programs) A theory of program correctness needs wffs, axioms, and inference rules. Wffs (called Hoare.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
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.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
Greatest Common Divisor
Greatest Common Divisor
Formal Methods in Software Engineering 1
Mathematical Structures for Computer Science Chapter 1
Axiomatic semantics Points to discuss: The assignment statement
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Semantics In Text: Chapter 3.
Formal Methods in software development
Predicate Transformers
Formal Methods in software development
Program correctness Axiomatic semantics
Program Verification with Hoare Logic
CIS 720 Lecture 3.
Programming Languages and Compilers (CS 421)
CIS 720 Lecture 3.
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
Presentation transcript:

Program Verification Using Hoares Logic Book: Chapter 7

While programs Assignments y:=t Composition S1; S2 If-then-else if e the S1 else S2 fi While while e do S od

Greatest common divisor {x1>0/\x2>0} y1:=x1; y2:=x2; while ¬(y1=y2) do if y1>y2 then y1:=y1-y2 else y2:=y2-y1 fi od {y1=gcd(x1,x2)}

Why it works? Suppose that y1,y2 are both positive integers. If y1>y2 then gcd(y1,y2)=gcd(y1-y2,y2) If y2>y1 then gcd(y1,y2)=gcd(y1,y2-y1) If y1-y2 then gcd(y1,y2)=y1=y2

Assignment axiom {p[t/y]} y:=t {p} For example: {y+5=10} y:=y+5 {y=10} {y+y<z} x:=y {x+y<z} {2*(y+5)>20} y:=2*(y+5) {y>20} Justification: write p with y instead of y, and add the conjunct y=t. Next, eliminate y by replacing y by t.

Why axiom works backwards? {p} y:=t {?} Strategy: write p and the conjunct y=t, where y replaces y in both p and t. Eliminate y. {y>5} y:=2*(y+5) {?} {p} y:=t { y (p[y/y] /\ t[y/y]=y)} y>5 /\ y=2*(y+5) y>20

Composition rule {p} S1 {r}, {r} S2 {q} {p} S1;S2 {q} For example: if the antecedents are 1. {x+1=y+2} x:=x+1 {x=y+2} 2. {x=y+2} y:=y+2 {x=y} Then the consequent is {x+1=y+2} x:=x+1; y:=y+2 {x=y}

More examples {p} S1 {r}, {r} S2 {q} {p} S1;S2 {q} {x1>0/\x2>0} y1:=x1 {gcd(x1,x2)=gcd(y1,x2)/\y1>0/\x2>0} {gcd(y1,x2)=gcd(y1,x2)/\y1>0/\x2>0} y2:=x2 {gcd(x1,x2)=gcd(y1,y2)/\y1>0/\y2>0} {x1>0/\x2>0} y1:=x1 ; y2:=x2 {gcd(x1,x2)=gcd(y1,y2)/\y1>0/\y2>0}

If-then-else rule {p/\e} S1 {q}, {p/\¬e} S2 {q} {p} if e then S1 else S2 fi {q} For example: p is gcd(y1,y2)=gcd(x1,x2) /\y1>0/\y2>0/\¬(y1=y2) e is y1>y2 S1 is y1:=y1-y2 S2 is y2:=y2-y1 q is gcd(y1,y2)=gcd(x1,x2)/\y1>0/\y2>0

While rule {p/\e} S {p} {p} while e do S od {p/\¬e} Example: p is {gcd(y1,y2)=gcd(x1,x2)/\y1>0/\y2>0} e is (y1=y2) S is if y1>y2 then y1:=y1-y2 else y2:=y2-y1 fi

Consequence rules Strengthen a precondition r p, {p} S {q} {r} S {q} Weaken a postscondition {p} S {q}, q r {p} S {r}

Use of first consequence rule Want to prove {x1>0/\x2>0} y1:=x1 {gcd(x1,x2)=gcd(y1,x2)/\y1>0/\x2>0} By assignment rule: {gcd(x1,x2)=gcd(x1,x2)/\x1>0/\x2>0} y1:=x1 {gcd(x1,x2)=gcd(y1,x2)/\y1>0/\x2>0} x1>0/\x2>0 gcd(x1,x2)=gcd(x1,x2)/\x1>0/\x2>0

Combining program {x1>0 /\ x2>0} y1:=x1; y2:=x1; {gcd(x1,x2)=gcd(y1,y2)/\y1>0/\y2>0} while S do if e then S1 else S2 fi od {gcd(x1,x2)=gcd(y1,y2)/\y1>0/\y2>0} Combine the above using concatenation rule!

Not completely finished {x1>0/\x2>0} y1:=x1; y2:=x1; while ~(y1=y2) do if e then S1 else S2 fi od {gcd(x1,x2)=gcd(y1,y2)/\y1>0/\y2>0/\y1=y2} But we wanted to prove: {x1>0/\x1>0} Prog {y1=gcd(x1,x2)}

Use of secend consequence rule {x1>0/\x2>0} Prog {gcd(x1,x2)=gcd(y1,y2)/\y1>0/\y2>0/\y1=y2} And the implication {gcd(x1,x2)=gcd(y1,y2)/\y1>0/\y2>0/\y1=y2} y1=gcd(x1,x2) Thus, {x1>0/\x2>0} Prog {y1=gcd(x1,x2)

Annotating a while program {x1>0/\x2>0} y1:=x1; {gcd(x1,x2)=gcd(y1,x2) /\y1>0/\x2>0} y2:=x2; {gcd(x1,x2)=gcd(y1,y2) /\y1>0/\y2>0} while ¬(y1=y2) do {gcd(x1,x2)=gcd(y1,y2)/\ y1>0/\y2>0/\¬(y1=y2)} if y1>y2 then y1:=y1-y2 else y2:=y2-y1 fi od {y1=gcd(x1,x2)}

Another example {x>=0 /\ y>=0} a:=0; b:=x; while b>=y do b:=b-y; a:=a+1 od. {x=a*y+b/\b>=0/\b<y} Invariant: x=a*y+b /\ b>=0

Invariant How to start the proof? Heuristics: Find invariant for each loop. For this example: x=a*y+b/\x>=0 Note: total correctness does not hold for y=0. Total correctness (with y>0) to be proved separately.

Proof (1) {x=a*y+x/\x>=0} b:=x {x=a*y+b/\b>=0} (Assignment) (2) {x=0*y+x/\x>=0} a:=0 {x=a*y+x/\x>=0} (Assignment) (3){x=0*y+x/\x>=0} a:=0;b:=x{x=a*y+b/\x>=0} (Composition (2), (1)) {p[t/y]} y:=t {p} {p}S1{r}, {r} S2{q} {p} S1;S2 {q}

Proof (cont.) (4){x=(a+1)*y+b/\b>=0} a:=a+1{x=a*y+b/\b>=0} (Assignment) (5){x=(a+1)*y+b-y/\b-y>=0} b:=b-y{x=(a+1)*y+b/\b>=0} (Assignment) (6){x=(a+1)*y+b-y/\b-y>=0} b:=b-y;a:=a+1{x=a*y+b/\b>=0} (Composition (5), (4)) {p[t/y]} y:=t {p} {p}S1{r}, {r} S2{q} {p} S1;S2 {q}

While rule {p/\e} S {p} {p} while e do S od {p/\¬e}

Consequence rules Strengthen a precondition r p, {p} S {q} {r} S {q} Weaken a postcondition {p} S {q}, q r {p} S {r}

Proof (cont.) (7) x=a*y+b/\b>=0/\b>=y x=(a+1)*y+b-y/\b-y>=0 (Logic) (8) {x=a*y+b/\b>=0/\b>=y} b:=b-y; a:=a+1 {x=a*y+b/\b>=0} (Consequence (6), (7)) (9) {x=a*y+b/\b>=0}while b>=y do b:=b-y; a:=a+1 od {x=a*y+b/\b>=0/\b<y} (while (8))

Proof (cont.) (10) {x=0*y+x/\x>=0} Prog {x=a*y+b/\b>=0/\b<y} (Composition (3), (9)) (11) x>=0/\y>=0 x=0*y+x/\x>=0 (Logic) (12) {x>=0/\y>=0} Prog {x=a*y+b/\b>=0/\b<y} (Consequence)

Soundness Hoare logic is sound in the sense that everything that can be proved is correct! This follows from the fact that each axiom and proof rule preserves soundness.

Completeness A proof system is called complete if every correct assertion can be proved. Propositional logic is complete. No deductive system for the standard arithmetic can be complete (Godel).

And for Hoares logic? Let S be a program and p its precondition. Then {p} S {false} means that S never terminates when started from p. This is undecideable. Thus, Hoares logic cannot be complete.

Weakest prendition, Strongest postcondition For an assertion p and code S, let post(p,S) be the strongest assertion such that {p}S{post(p,S)} That is, if {p}S{q} then post(p,S) q. For an assertion q and code S, let pre(S,q) be the weakest assertion such that {pre(S,q)}S{q} That is, if {p}S{q} then p pre(S,q).

Relative completeness Suppose that either post(p,S) exists for each p, S, or pre(S,q) exists for each S, q. Some oracle decides on pure implications. Then each correct Hoare triple can be proved. What does that mean? The weakness of the proof system stem from the weakness of the (FO) logic, not of Hoares proof system.

Extensions Many extensions for Hoares proof rules: Total correctness Arrays Subroutines Concurrent programs Fairness

Proof rule for total correctness {p/\e/\t=z} S {p/\t =0 {p} while e do S od {p/\¬e} where z - an int. variable, not appearing in p,t,e,S. t - an int. expression.