Computer Science School of Computing Clemson University Discrete Math and Reasoning about Software Correctness Joseph E. Hollingsworth

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Advertisements

Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
This research is funded in part the U. S. National Science Foundation grant CCR DEET for Component-Based Software Murali Sitaraman, Durga P. Gandi.
Computer Science School of Computing Clemson University Introduction to Mathematical Reasoning Jason Hallstrom and Murali Sitaraman Clemson University.
Addressing the Challenges of Current Software. Questions to Address Why? What? Where? How?
Reasoning About Code; Hoare Logic, continued
ISBN Chapter 3 Describing Syntax and Semantics.
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
CS 355 – Programming Languages
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
Self-Reference - Induction Cmput Lecture 7 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Describing Syntax and Semantics
Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Mathematics throughout the CS Curriculum Support by NSF #
Discrete Maths Objective to show the close connection between recursive definitions and recursive functions , Semester 2, Recursion.
Computer Science School of Computing Clemson University Discrete Math and Reasoning about Software Correctness Murali Sitaraman
Computer Science School of Computing Clemson University Specification and Reasoning in SE Projects Using a Web IDE Charles T. Cook (Clemson) Svetlana V.
Computer Science School of Computing Clemson University Mathematical Reasoning across the Curriculum Software Development Foundations and Software Engineering.
Lecture 16 March 22, 2011 Formal Methods CS 315 Spring Adapted from slides provided by Jason Hallstrom and Murali Sitaraman (Clemson)
Reading and Writing Mathematical Proofs
CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page 1 Lecture 18 CS 1813 – Discrete Mathematics Loops Without Invariants Are Like.
1 CE 221 Data Structures & Algorithms Chapter 1 Introduction Text: Read Weiss, §1.1 – 1.3 Izmir University of Economics.
Week #7 – 7/9/11 October 2002 Prof. Marie desJardins
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.
Lecture 17 March 24, 2011 Formal Methods 2 CS 315 Spring Adapted from slides provided by Jason Hallstrom and Murali Sitaraman (Clemson)
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
1 Formal Semantics of Programming Languages “Program testing can be used to show the presence of bugs, but never to show their absence!” --Dijkstra.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Recursive Algorithms &
Reasoning about programs March CSE 403, Winter 2011, Brun.
Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Specifying Languages Our aim is to be able to specify languages for use in the computer. The sketch of an FSA is easy for us to understand, but difficult.
October 3, 2001CSE 373, Autumn Mathematical Background Exponents X A X B = X A+B X A / X B = X A-B (X A ) B = X AB X N +X N = 2X N 2 N +2 N = 2 N+1.
Static Techniques for V&V. Hierarchy of V&V techniques Static Analysis V&V Dynamic Techniques Model Checking Simulation Symbolic Execution Testing Informal.
Computer Science School of Computing Clemson University Mathematical Reasoning with Objects.
Discrete Maths: Invariant/2 1 Discrete Maths Objectives – –to show the use of induction for proving properties of code involving loops use induction.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
Integrating Math Units and Proof Checking for Specification and Verification SAVCBS Workshop 2008 SIGSOFT 2008 / FSE 16 November 9th, 2008 Hampton Smith.
Lecture 18 March 29, 2011 Formal Methods 3 CS 315 Spring Adapted from slides provided by Jason Hallstrom and Murali Sitaraman (Clemson)
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
1 Proving Properties of Recursive List Functions CS 270 Math Foundations of CS Jeremy Johnson.
Computer Science School of Computing Clemson University Reasoning with Queues and Web Interface Demo.
Fall 2002CMSC Discrete Structures1 Chapter 3 Sequences Mathematical Induction Recursion Recursion.
Introduction to Automata Theory
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
Modular Alternatives to Testing
Formal Specification of Java Interfaces
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
Disjunctive Normal Form
©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Mathematical Induction Recursion
Proving Properties of Recursive List Functions
Introduction to Components and Specifications Using RESOLVE
Programming Languages 2nd edition Tucker and Noonan
Formal Specification of Interfaces
Functional Verification I
Applied Discrete Mathematics Week 9: Integer Properties
Introduction to Components and Specifications Using RESOLVE
Mathematical Reasoning
More Mathematical Reasoning (Conditional Statements)
Mathematical Reasoning with Data Abstractions
Recursive Thinking.
Programming Languages 2nd edition Tucker and Noonan
Formal Methods Lecture 16 March 22, 2011 CS 315 Spring 2011
Presentation transcript:

Computer Science School of Computing Clemson University Discrete Math and Reasoning about Software Correctness Joseph E. Hollingsworth Murali Sitaraman This research is funded in part by NSF grants CCF , CCF ‐ , DUE , and DUE

School of Computing Clemson University Overview  Software correctness proofs  Connections between software correctness and discrete math foundations  Key example: Recursive code correctness and induction

School of Computing Clemson University Basics  Software is correct if it meets its specifications.  Is the following code segment correct? Assume all variables are Integers. Assume I = #I and J = #J; Temp = I; I = J; J = Temp; Confirm I = #J and J = #I;

School of Computing Clemson University Basics  Software is correct if it meets its specifications.  Is the following code segment correct? Assume all variables are Integers. Assume I = #I and J = #J; Temp := I; I := J; J := Temp; Confirm I = #J and J = #I;

School of Computing Clemson University Assignment Rule Assume I = #I and J = #J; Temp := I; I := J; J := Temp; Confirm I = #J and J = #I;  Since J is assigned Temp, if we can prove Temp = #I before the last assignment, then J would equal #I after that assignment (our goal)

School of Computing Clemson University Simplify Assume I = #I and J = #J; Temp := I; I := J; J := Temp; Confirm I = #J and J = #I;  becomes (change is shown in italics) Assume I = #I and J = #J; Temp := I; I := J; Confirm I = #J and Temp = #I;

School of Computing Clemson University Simplify again Assume I = #I and J = #J; Temp := I; I := J; Confirm I = #J and Temp = #I;  becomes Assume I = #I and J = #J; Temp := I; Confirm J = #J and Temp = #I;

School of Computing Clemson University Simplify one more time Assume I = #I and J = #J; Temp := I; Confirm J = #J and Temp = #I;  becomes Assume I = #I and J = #J; Confirm J = #J and I = #I;

School of Computing Clemson University Correctness to Discrete Math  Simplify one more time. Assume I = #I and J = #J; Confirm J = #J and I = #I;  The above is the same as: (I = #I and J = #J)  (J = #J and I = #I);  True, because P  P;

School of Computing Clemson University Basics  What did we just do?  Mathematically prove that a piece of code is correct using integer theory and logic.  The process is mechanical.  This is unlike testing where we can only show presence of errors, but not their absence.

School of Computing Clemson University Exercise  Is the following code segment correct? Assume all variables are Integers.  Work it out on the given sheet. Assume I = #I and J = #J; I := I + J; J := I - J; I := I - J; Confirm I = #J and J = #I;

School of Computing Clemson University Simplify Assume I = #I and J = #J; I := I + J; J := I - J; I := I - J; Confirm I = #J and J = #I;  becomes Assume I = #I and J = #J; I := I + J; J := I - J; Confirm (I – J) = #J and J = #I;

School of Computing Clemson University Simplify again Assume I = #I and J = #J; I := I + J; J := I - J; Confirm (I – J) = #J and J = #I;  becomes Assume I = #I and J = #J; I := I + J; Confirm (I – (I – J)) = #J and (I – J) = #I;

School of Computing Clemson University Simplify expression Assume I = #I and J = #J; I := I + J; Confirm (I – (I – J)) = #J and (I – J) = #I;  becomes Assume I = #I and J = #J; I := I + J; Confirm J = #J and (I – J) = #I;

School of Computing Clemson University Simplify one last time Assume I = #I and J = #J; I := I + J; Confirm J = #J and (I – J) = #I;  becomes Assume I = #I and J = #J; Confirm J = #J and ((I + J) – J) = #I;  becomes Assume I = #I and J = #J; Confirm J = #J and I = #I;

School of Computing Clemson University Discussion and Demo  Is the code correct?  Math integers vs. computational integers  Assume and confirm assertions come from requires and ensures clauses of operations  Mechanical “proof” rules exist for all programming constructs, including if statements, while statements, objects, pointers, etc.  They all have discrete math foundations.  Demo site:

School of Computing Clemson University Part II: Recursion and induction

School of Computing Clemson University Example int sum(int j, int k) // requires j >= 0 // ensures result = j + k { if (j == 0) { return k; } else { j--; int r = sum(j, k); return r + 1; }

School of Computing Clemson University Reasoning Pattern  Similar to an inductive proof  Base case (e.g., j == 0)  Reason code works for the base case  Recursive case (e.g., j != 0)  Assume that the recursive call j--; r = sum(j, k) works  Reason that the code works for the case of j  Show assumption is legit, i.e., show termination

School of Computing Clemson University Recursion: Base case int sum(int j, int k) // requires j >= 0 // ensures result = j + k { if (j == 0) { return k; // Assume: (j = 0) ^ (result = k) // Confirm ensures: result = 0 + k } else {... }

School of Computing Clemson University Recursion: Inductive Assumption int sum(int j, int k) // requires j >= 0 // ensures result = j + k { if (j == 0) {... } else { j--; int r = sum(j, k); // Assume: r = (j – 1) + k return r + 1; }

School of Computing Clemson University Recursion: Inductive Proof Step int sum(int j, int k) // requires j >= 0 // ensures result = j + k {if (j == 0) {... } else { j--; int r = sum(j, k); return r + 1; // Assume: (r = (j – 1) + k) ^ //(result = r + 1) // Confirm ensures: result = j + k }

School of Computing Clemson University Reasoning: Recursive Case  For the inductive proof to be legit, the inductive assumption must be legit  This requires showing that an argument passed to the recursive call is strictly smaller  This is the proof of termination  To prove termination automatically, programmers need to provide a progress metric ( j decreases in the example)

School of Computing Clemson University Reasoning about iterative code  Also appeals to induction  Loops need to include an “invariant” and a progress metric for termination  Invariant is established for the base case (i.e., before the loop is entered)  Invariant is assumed at the beginning of an iteration and confirmed at the beginning of the next  Also needs a progress metric and a proof of termination

School of Computing Clemson University Part III: Discrete Math and Software Modeling

School of Computing Clemson University Discrete Math and Specifications  Discrete structures, such as numbers, sets, etc., are used in mathematical modeling of software  Example using another discrete structure: Mathematical strings  Strings are useful to specify and reason about CS structures, such as stacks, queues, lists, etc.  Example: Verify recursive code that reverses a character string or a queue

School of Computing Clemson University Basic Math Strings  Unlike sets, strings have order Example: Str(Z) for String of integers  Notations Empty string (written empty_string or ) Concatenation ( alpha o beta ) Length ( |alpha| ) String containing one entry ( )

School of Computing Clemson University Theorems from string theory  Various theorems from string theory are necessary to prove software correctness  VCs in proving correctness of iterative Queue Append realization  Demo: roup/resolve/ roup/resolve/  Theorem: string concatenation is associative: , : Str(), x: , (( o ) o ) = ( o ( o ))

School of Computing Clemson University Theorems from string theory  Various theorems from string theory are necessary to prove software correctness  VCs in proving correctness of recursive Queue Flip or Text Flip realization  Demo: roup/resolve/ roup/resolve/  String reversal theorem: : Str(), x: , Reverse ( o ) = o Reverse()

School of Computing Clemson University Summary  Discrete math foundations are central for developing correct software.  Modern programming languages are being enhanced with specification language counterparts to facilitate verification of software correctness  Example spec/programming language combinations include JML (Java Modeling Language)/Java, Spec#/C#, and RESOLVE (an integrated language).