Proving Program Correctness The Axiomatic Approach.

Slides:



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

Program Verification Using Hoares Logic Book: Chapter 7.
Semantics Static semantics Dynamic semantics attribute grammars
PZ03D Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03D - Program verification Programming Language Design.
Functional Verification III Prepared by Stephen M. Thebaut, Ph.D. University of Florida Software Testing and Verification Lecture Notes 23.
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
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
Axiomatic Verification I Prepared by Stephen M. Thebaut, Ph.D. University of Florida Software Testing and Verification Lecture 17.
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.
Dynamic semantics Precisely specify the meanings of programs. Why? –programmers need to understand the meanings of programs they read –programmers need.
Copyright © 2006 Addison-Wesley. All rights reserved. 3.5 Dynamic Semantics Meanings of expressions, statements, and program units Static semantics – type.
Predicate Transformers
Program Proving Notes Ellen L. Walker.
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.
CS 355 – Programming Languages
CSE115/ENGR160 Discrete Mathematics 04/12/11 Ming-Hsuan Yang UC Merced 1.
Axiomatic Semantics Dr. M Al-Mulhem ICS
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
PSUCS322 HM 1 Languages and Compiler Design II Formal Semantics Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
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
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.
CSE 755, part3 Axiomatic Semantics Will consider axiomatic semantics (A.S.) of IMP: ::=skip | | | | ; | | Only integer vars; no procedures/fns; vars declared.
Reading and Writing Mathematical Proofs
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
Proofs of Correctness: An Introduction to Axiomatic Verification Prepared by Stephen M. Thebaut, Ph.D. University of Florida CEN 5035 Software Engineering.
CSI 3125, Axiomatic Semantics, page 1 Axiomatic semantics The assignment statement Statement composition The "if-then-else" statement The "while" statement.
Program Correctness. 2 Program Verification An object is a finite state machine: –Its attribute values are its state. –Its methods optionally: Transition.
Recursive Algorithms &
Reasoning about programs March CSE 403, Winter 2011, Brun.
Program Analysis and Verification Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics I Roman Manevich Ben-Gurion University.
Semantics In Text: Chapter 3.
Languages and Compilers
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.
CSE 311: Foundations of Computing Fall 2013 Lecture 8: Proofs and Set theory.
1 Section 8.2 Program Correctness (for imperative programs) A theory of program correctness needs wffs, axioms, and inference rules. Wffs (called Hoare.
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
Formal Methods in Software Engineering 1
Lecture 2: Axiomatic semantics
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.
Axiomatic Verification I
Predicate Transformers
Formal Methods in software development
Axiomatic Semantics Will consider axiomatic semantics (A.S.) of IMP:
Functional Verification II
Axiomatic Verification I
PZ03D - Program verification
PZ03D - Program verification
Lecture 2: Axiomatic semantics
CIS 720 Lecture 3.
Programming Languages and Compilers (CS 421)
CIS 720 Lecture 3.
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
Program Correctness an introduction.
Presentation transcript:

Proving Program Correctness The Axiomatic Approach

What is Correctness? Correctness: –partial correctness + termination Partial correctness: –Program implements its specification

Proving Partial Correctness Goal: prove that program is partially correct Approach: model computation with predicates –Predicates are boolean functions over program state Simple example –{odd(x)} a = x {odd(a)} Generally: {P} S {Q}, where –P  precondition –Q  postcondition –S  Programming language statement

Proof System Two elements of proof system –Axioms: capture the effect of prog. lang. stmts. –Inference rules: compose axioms to build up proofs of entire program behavior Let’s start by discussing inference rules and then we’ll return to discussing axioms

Composition Rule: Consider two predicates –{odd(x+1)} x = x+1 {odd(x)} –{odd(x)} a = x {odd(a)} What is the effect of executing both stmts? –{odd(x+1)} x = x+1 ; a = x {odd(a)}

Consequence 1 Rule Ex: –{odd(x)} a = x {odd(a)} and –Postcondition  {a  4} What can we say about this program?

Consequence 2 Rule: Ex: –Precondition  {x=1} and –{odd(x)} a = x {odd(a)} What can we say about this program?

Axioms Axioms explain the effect of executing a single statement –Assignment –If –If then else –While loop Typically applied in reverse during proof –Start with postcondition and work backwards to determine what must precondition must be

Assignment Axiom Rule: Application: Replace all free occurences of x with y –e.g., {odd(x)} a = x {odd(a)}

Conditional Stmt 1 Axiom Rule: B if S {P} {P   B if }{P  B if } {Q}

Application Example: 1.if even(x) then { 2. x = x +1 3.} {odd(x)  x > 3} else part: need to show {(P   even(x))  (odd(x)  x>3)} {P  (x>3)} then part: need to show {P ^ even(x)} x=x+1 {odd(x)  x>3} {odd(x+1)  x>2} x = x+1 {odd(x)  x > 3} {(P  even(x))  (odd(x+1)  x>2)} {P  (x>2)} Need to choose a predicate P consistent with implications above P  x>2 –x > 39 works as well

Conditional Stmt 2 Axiom Rule {P} {P   B if } {Q} S2S2 S1S1 {P  B if } B if

Conditional Stmt 2 Axiom Example: 1.if x < 0 then { 2. x = -x; 3. y = x 4.} else { 5. y = x 6.} {y = |x|} Then part: need to show {P  (x<0)} x=-x;y=x {y = |x|} {x = |x|} y = x {y = |x|} {-x = |x|} x = -x {x = |x|} ( P  x <0)  -x = |x| Else part: need to show {P   (x<0)} y=x {y = |x|} {x =|x|} y=x {y=|x|} ( P  ¬(x < 0))  x = |x| P  true

While Loop Axiom Rule Infinite number of paths, so we need one predicate for that captures the effect of 0 or more loop traversals P is called an Pariant B if S {P} {P   B}

Partial Correctness Proof Example IN  {B  0} –a = A –b = B –y = 0 –while b > 0 do { –y = y + a –b = b - 1 –} OUT  {y = AB} P  y + ab = AB  b  0 B w  b > 0 Show P  ¬ B w  OUT y + ab = AB  b  0  ¬(b > 0) y + ab = AB  b = 0 y = AB So {P  ¬ B w }  OUT Establish {IN} a=A;b=B;y=0 {P} {ab = AB  b  0} y=0 { P} {aB = AB  B  0} b = B {….} {AB = AB  B  0} a = A {….} So {IN } a=A;b=B;y=0 {P}

While Loop Axiom Need to show {P  B w } y=y+a; b=b-1 {P} {y+a(b-1) = AB  b-1  0} b = b - 1 {P} {y+a+a(b-1) = AB  b-1  0} y = y+a {….} {y +ab = AB  b-1  0} loop body {P} { y + ab = AB  b  0  b > 0}  {y +ab = AB  b-1  0}, So –{IN} lines 1-3} {P}, –{P} while loop {P  ¬ B w }, and –{P  ¬ B w }  OUT Therefore –{IN} program {OUT}

Total correctness After you have shown partial correctness –Need to prove that program terminates Usually a progress argument. For previous program –Loop terminates if b  0 –b starts positive and is decremented by 1 every iteration –So loop must eventually terminate