Formal Methods in Software Engineering 1

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: flowchart programs
Program Verification Using Hoares Logic Book: Chapter 7.
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.
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.
This Week Finish relational semantics Hoare logic Interlude on one-point rule Building formulas from programs.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 19: Minding Ps & Qs: Axiomatic.
Axiomatic Verification I Prepared by Stephen M. Thebaut, Ph.D. University of Florida Software Testing and Verification Lecture 17.
Partial correctness © Marcelo d’Amorim 2010.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
Dynamic semantics Precisely specify the meanings of programs. Why? –programmers need to understand the meanings of programs they read –programmers need.
Predicate Transformers
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
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.
CSE115/ENGR160 Discrete Mathematics 04/12/11 Ming-Hsuan Yang UC Merced 1.
Hoare-style program verification K. Rustan M. Leino Guest lecturer Rob DeLine’s CSE 503, Software Engineering University of Washington 26 Apr 2004.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
4/17/2017 Section 3.6 Program Correctness ch3.6.
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.
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)
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.
1 Formal Semantics of Programming Languages “Program testing can be used to show the presence of bugs, but never to show their absence!” --Dijkstra.
Program Correctness. 2 Program Verification An object is a finite state machine: –Its attribute values are its state. –Its methods optionally: Transition.
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.
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.
Program Analysis and Verification Spring 2015 Program Analysis and Verification Lecture 4: Axiomatic Semantics I Roman Manevich Ben-Gurion University.
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.
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.
Hoare-style program verification
Mathematical Structures for Computer Science Chapter 1
Reasoning About Code; Hoare Logic
Lecture 5 Floyd-Hoare Style Verification
Axiomatic semantics Points to discuss: The assignment statement
Reasoning about Loops, Conclusion
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Semantics In Text: Chapter 3.
Axiomatic Verification I
Formal Methods in software development
Predicate Transformers
Formal Methods in software development
Axiomatic Semantics Will consider axiomatic semantics (A.S.) of IMP:
Proofs of Correctness: An Introduction to Axiomatic Verification
Axiomatic Verification I
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
Program Correctness an introduction.
Presentation transcript:

Formal Methods in Software Engineering 1 Agenda TA: Wenhua Wang, 10:00 – 11:30am Mon & Wed, NH 239, wenhua.wang@mavs.uta.edu Quick Review Finish Logic Review Program Proof Formal Methods in Software Engineering 1

Formal Methods in Software Engineering 2 Quick Review What are the syntactic objects we can use in first-order logic? Formal Methods in Software Engineering 2

Deductive Verification Introduction Hoare’s Logic An Example Proof Summary Formal Methods in Software Engineering 3

Deductive Verification ... is to formally verify the correctness of a program using a proof system. Given a program P and an initial condition I, this approach proves that the expected final condition is the logical consequence of executing P from I. Formal Methods in Software Engineering 4

Formal Methods in Software Engineering 5 Program Correctness Partial correctness: If the initial condition holds, and if the program terminates, then the input-output claim holds. Termination: If the initial condition holds, then the program terminates Total correctness: If the initial condition holds, then the program terminates, and the input-output claim holds. Formal Methods in Software Engineering 5

Formal Methods in Software Engineering 6 Benefits Establishes or enhances the correctness of a program Finding errors in the code and correct them A better understanding of the verified algorithm Identifies invariants that can be used to check code consistency during the different development stages. A proof system can be used as a formalism to formally define the semantics of a language. Formal Methods in Software Engineering 6

Formal Methods in Software Engineering 7 Limitations Can’t be completely automated often requires a great deal of domain expertise Highly time-consuming only performed on key algorithms Errors may crop into the proof who will verify the verifier? Has limited scalability difficult to verify large programs Formal Methods in Software Engineering 7

Deductive Verification Introduction Hoare Logic An Example Proof Summary Formal Methods in Software Engineering 8

Formal Methods in Software Engineering 9 While Program A while program consists of three types of statements: S ::= v:= e | S ; S | if p then S else S fi | while p do S Formal Methods in Software Engineering 9

Formal Methods in Software Engineering 10 Hoare Triple A Hoare Triple is written as {p} S {q}, where S is a program segment, and p and q are two first order formulas. The meaning of {p} S {q} is as follows: If execution of S starts with a state satisfying p, and if S terminates, then a state satisfying q is reached. Formal Methods in Software Engineering 10

Formal Methods in Software Engineering 11 Assignment Axiom {p[e/v]} v:= e {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} Formal Methods in Software Engineering 11

Formal Methods in Software Engineering 12 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} Formal Methods in Software Engineering 12

If-Then-Else Rule If the antecedents are Then the consequent is: {p  c } S1 {q}, {p  c } S2 {q} {p} if c then S1 else S2 fi {q} If the antecedents are 1: {(y1 > 0  y2 > 0  y1 != y2)  (y1 > y2)} y1 := y1 – y2 {y1 > 0  y2 > 0} 2: {(y1 > 0  y2 > 0  y1 != y2)   (y1 > y2)} y2 := y2 – y1 {y1 > 0  y2 > 0} Then the consequent is: {(y1 > 0  y2 > 0  y1 != y2)} if y1 > y2 then y1 := y1 – y2 else y2 := y2 – y1 {y1 > 0  y2 > 0} Formal Methods in Software Engineering 13

While Rule (1) The while rule uses an invariant, which needs to hold before and after each iteration. {p  c } S {p} {p} while c do S end {p   c } Formal Methods in Software Engineering 14

Formal Methods in Software Engineering 15 While Rule (2) {(y1 > 0  y2 > 0)  y1 != y2} if y1 > y2 then y1 := y1 – y2 else y2 := y2 – y1 {y1 > 0  y2 > 0} {y1 > 0  y2 > 0} while y1 != y2 do if y1 > y2 then y1 := y1 – y2 else y2 := y2 – y1 end {y1 > 0  y2 > 0}  {y1 == y2} Formal Methods in Software Engineering 15

Formal Methods in Software Engineering 16 Strengthening Rule This rule is used to strengthen a precondition: p  r, {r} S {q} {p} S {q} Note that this rule is often used together with the assignment axiom: To prove {p} v := e {q}, we show that p  q [e/v]. Formal Methods in Software Engineering 16

Formal Methods in Software Engineering 17 Weakening Rule This rule is used to weaken a postcondition: {p} S {r}, r  q {p} S {q} Formal Methods in Software Engineering 17

Deductive Verification Introduction Hoare Logic An Example Proof Summary Formal Methods in Software Engineering 18

Formal Methods in Software Engineering 19 The Program Below is an program that computes the integer division of x1 by x2. {x1 >= 0  x2 > 0} y1 := 0; y2 := x1; while y2 >= x2 do y1 := y1 + 1; y2 := y2 – x2; end {x1  y1  x2 + y2  y2 >= 0  y2 < x2} S1 S2 S3 Formal Methods in Software Engineering 19

Formal Methods in Software Engineering 20 A Proof Tree 1 Sequential Composition 2 7 while 8 3 Strengthening 5 4 Assignment 6 9 11 10 Formal Methods in Software Engineering 20

Formal Methods in Software Engineering 21 Proof Steps (1) Goal 1. Sequential Composition {x1 >= 0  x2 > 0} S1 {x1  y1  x2 + y2  y2 >= 0} {x1  y1  x2 + y2  y2 >= 0} S2 {x1  y1  x2 + y2  y2 >= 0  y2 < x2} {x1 >= 0  x2 > 0} S1 ; S2 {x1  y1  x2 + y2  y2 >= 0  y2 < x2} Goal 2. Sequential Composition {x1 >= 0  x2 > 0} y1 := 0 {x1 >= 0  x2 > 0  y1  0} {x1 >= 0  x2 > 0  y1  0} y2 := x1 {x1  y1  x2 + y2  y2 >= 0} {x1 >= 0  x2 > 0} S1 {x1  y1  x2 + y2  y2 >= 0} Formal Methods in Software Engineering 21

Formal Methods in Software Engineering 22 Proof Steps (2) Goal 3. Strengthening {x1 >= 0  x2 > 0}  {x1 >= 0  x2 > 0  0  0} {x1 >= 0  x2 > 0  0  0} y1 := 0 {x1 >= 0  x2 > 0  y1  0} {x1 >= 0  x2 > 0} y1 := 0 {x1 >= 0  x2 > 0  y1  0} Goal 4. Assignment {x1 >= 0  x2 > 0  0  0} y1 := 0 {x1 >= 0  x2 > 0  y1  0} Formal Methods in Software Engineering 22

Formal Methods in Software Engineering 23 Proof Steps (3) Goal 5. Strengthening {x1 >= 0  x2 > 0  y1  0}  {x1  y1  x2 + x1  x1 >= 0} {x1  y1  x2 + x1  x1 >= 0} y2 := x1 {x1  y1  x2 + y2  y2 >= 0} {x1 >= 0  x2 > 0  y1  0} y2 := x1 {x1  y1  x2 + y2  y2 >= 0} Goal 6. Assignment {x1  y1  x2 + x1  x1 >= 0} y2 := x1 {x1  y1  x2 + y2  y2 >= 0} Formal Methods in Software Engineering 23

Formal Methods in Software Engineering 24 Proof Steps (4) Goal 7. While {x1  y1  x2 + y2  y2 >= 0  y2 >= x2} S3 {x1  y1  x2 + y2  y2 >= 0} {x1  y1  x2 + y2  y2 >= 0} S2 {x1  y1  x2 + y2  y2 >= 0  y2 < x2} Goal 8. Sequential Composition {x1  y1  x2 + y2  y2 >= x2} y1 := y1 + 1 {x1  y1  x2 + y2 – x2  y2 – x2 >= 0} {x1  y1  x2 + y2  y2 >= x2} S3 {x1  y1  x2 + y2  y2 >= 0} {x1  y1  x2 + y2 – x2  y2 – x2 >= 0} y2 := y2 – x2 {x1  y1  x2 + y2  y2 >= 0} Formal Methods in Software Engineering 24

Formal Methods in Software Engineering 25 Proof Steps (5) Goal 9. Strengthening {x1  y1  x2 + y2  y2 >= x2} -> {x1  (y1 + 1)  x2 + y2 – x2  y2 – x2 >= 0} {x1  y1  x2 + y2  y2 >= x2} y1 := y1 + 1 {x1  y1  x2 + y2 – x2  y2 – x2 >= 0} {x1  (y1 + 1)  x2 + y2 – x2  y2 – x2 >= 0} y1 := y1 + 1 {x1  y1  x2 + y2 – x2  y2 – x2 >= 0} Goal 10. Assignment {x1  (y1 + 1)  x2 + y2 – x2  y2 – x2 >= 0} y1 := y1 + 1 {x1  y1  x2 + y2 – x2  y2 – x2 >= 0} Goal 11. Assignment {x1  y1  x2 + y2 – x2  y2 – x2 >= 0} y2 := y2 – x2 {x1  y1  x2 + y2 – x2  y2 >= 0} Formal Methods in Software Engineering 25

Formal Methods in Software Engineering 26 Annotation { x1 >= 0  x2 > 0 } y1 := 0; {x1 >= 0  x2 > 0  y1  0} y2 : = x1; {x1  y1  x2 + y2  y2 >= 0} while y2 >= x2 do {x1  y1  x2 + y2  y2 >= x2} y1 := y1 + 1; {x1  y1  x2 + y2 – x2  y2 – x2 >= 0} y2 := y2 – x2; end {x1  y1  x2 + y2  y2 >= 0  y2 < x2} Formal Methods in Software Engineering 26

Deductive Verification Introduction Hoare Logic An Example Proof Summary Formal Methods in Software Engineering 27

Formal Methods in Software Engineering 28 Summary Deductive verification can establish and/or enhance the correctness of a program. The verification process is highly time-consuming, and can’t be completely automated. The key to apply Hoare’s logic is to find the right “loop invariants”. The proof process is usually conducted using backward reasoning. Formal Methods in Software Engineering 28