Program Correctness an introduction.

Slides:



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

In this episode of The Verification Corner, Rustan Leino talks about Loop Invariants. He gives a brief summary of the theoretical foundations and shows.
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.
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.
Predicate Transformers
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.
TR1413: INTRO TO DISCRETE MATHEMATICS LECTURE 2: MATHEMATICAL INDUCTION.
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.
Chapter Mathematical Induction
Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Lecture 4 Discrete Mathematics Harper Langston. Algorithms Algorithm is step-by-step method for performing some action Cost of statements execution –Simple.
Proving Program Correctness The Axiomatic Approach.
DAST 2005 Tirgul 6 Heaps Induction. DAST 2005 Heaps A binary heap is a nearly complete binary tree stored in an array object In a max heap, the value.
Proving Program Correctness The Axiomatic Approach.
Reading and Writing Mathematical Proofs
Mathematical Induction. F(1) = 1; F(n+1) = F(n) + (2n+1) for n≥ F(n) n F(n) =n 2 for all n ≥ 1 Prove it!
1 Program Correctness CIS 375 Bruce R. Maxim UM-Dearborn.
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.
4.3 Recursive Definitions and Structural Induction The PMI, 2PMI, and WOP are all valuable tools in proving the correctness of loops and of recursively.
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.
MS 101: Algorithms Instructor Neelima Gupta
Program Correctness. 2 Program Verification An object is a finite state machine: –Its attribute values are its state. –Its methods optionally: Transition.
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
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.
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.
Static Techniques for V&V. Hierarchy of V&V techniques Static Analysis V&V Dynamic Techniques Model Checking Simulation Symbolic Execution Testing Informal.
Slide Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Cs7100(Prasad)L18-9WP1 Axiomatic Semantics Predicate Transformers.
Chapter 5 1. Chapter Summary  Mathematical Induction  Strong Induction  Recursive Definitions  Structural Induction  Recursive Algorithms.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
CSE15 Discrete Mathematics 04/12/17
Math/CSE 1019C: Discrete Mathematics for Computer Science Fall 2012
Control Structures II Chapter 3
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
Formal Methods in Software Engineering 1
Axiomatic semantics Points to discuss: The assignment statement
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Induction and recursion
Follow me for a walk through...
Logic for Computer Security Protocols
Axiomatic Verification II
Axiomatic Verification I
Predicate Transformers
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Formal Methods in software development
Proofs of Correctness: An Introduction to Axiomatic Verification
Output Variables {true} S {i = j} i := j; or j := i;
Axiomatic Verification I
Program correctness Axiomatic semantics
CIS 720 Lecture 3.
CIS 720 Lecture 3.
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
Presentation transcript:

Program Correctness an introduction

Program Correctness How do you do that? How can we be sure that a program/algorithm always produces the correct result? Test it on sample input Test boundary conditions Test it on all possible inputs Prove it correct can we automate this? Use rules of inference, mathematical induction

Program Correctness Correct, what does that mean? A program is correct if it produces correct output for all possible inputs this is called partial correctness it terminates An initial assertion gives the properties of the input A final assertion gives the properties of the output The initial and final assertions must be given otherwise we cannot check correctness

Program Correctness Partially Correct? A program, or program segment, S is partially correct if with respect to initial assertion p and final assertion q whenever p is true for the input and S terminates then q is true for the output. p{S}q indicates program, or program segment S is partially correct p{S}q is called a Hoare triple Note: partial correctness only states that the program produces the correct results if it terminates. It does not prove that the program terminates

Program Correctness Tony Hoare

Program Correctness A very simple example Program segment S is as follows y:=2; z := x + y; Initial assertion p: x = 1 Final assertion q: z = 3 Prove p{S}q assume p x initially has the value 1 y is assigned the value 2 z is then assigned the value x + y that is equal to 1 + 2 which is 3 Therefore S is correct with respect to p and q

Program Correctness Decompose your program We can split our program into parts (subprograms) and prove that each of these parts (subprograms) is correct Split S into subprograms S1 and S2 S is then S1 followed by S2 S = S1;S2 Assume p is the initial assertion of S1, q is the final assertion of S1 q is the initial assertion of S2 r is the final assertion of S2 Further assume we have established p{S1}q and q{S2}r It follows that if p is true and S1 executes and terminates then q is true if q is true and S2 executes and terminates then r is true Therefore if p is true and S executes and terminates r is true

Program Correctness A new rule of inference: The Composition Rule

Program Correctness Simple Conditional Statement Assume program segment is as follows if cond then S S is executed if cond is true S is not executed if cond is false To verify that the segment above is true with respect to initial assertion p final assertion q Show that when p is true, and cond is true and S executes, q is true when p is true and cond is false and S does not execute, q is true

Program Correctness The simple condition rule of inference

Program Correctness An example of a simple conditional Program segment S is as follows if x > y then x := y Initial assertion p: is True Final assertion q: y  x (y is greater than or equal to x) Consider cond = true (x > y) and cond = false (x  y) (1) p and x > y the assignment x := y is made consequently y  x therefore q holds (2) p and x  y no assignment is made y  x Therefore S is correct with respect to p and q

Program Correctness Conditional Statement Assume program segment is as follows if cond then S1 else S2 S1 is executed if cond is true S2 is executed if cond is false To verify that the segment above is true with respect to initial assertion p final assertion q Show that when p is true, and cond is true and S1 executes, q is true when p is true, and cond is false and S2 executes, q is true

Program Correctness The condition rule of inference

Program Correctness An example of a conditional Program segment S is as follows if x < 0 then abs := -x else abs := x Initial assertion p: is True Final assertion q: abs = |x| Consider the cases when cond = true and when cond = false (1) p and x < 0 the assignment abs := -x is made consequently abs = |x| therefore q holds (2) p and x  0 consequently abs := x, and again abs is |x| Therefore S is correct with respect to p and q

Program Correctness While Loop (loop invariants) Assume program segment is as follows while cond do S S is repeatedly executed while cond is true S is repeatedly executed until cond is false An assertion that remains true each time S is executed is required this is the loop invariant p is a loop invariant if (p and cond){S}p is true To verify that the segment above is true with respect to loop invariant p Show that p is true before S is executed p is true and cond is false on termination of the loop if it terminates

Program Correctness The loop invariant rule of inference

An example of a loop invariant Program Correctness An example of a loop invariant i := 1; fact := 1; while i < n do begin i := i + 1; fact := fact * i; end Prove segment terminates with fact = n! a loop invariant is required let p be proposition p: fact = i! and i <= n let S be the segment: i := i+1; fact := fact * i; Prove that p is a loop invariant, using mathematical induction Basis Step: initially i = fact = 1 = i! and 1 <= n Inductive Step assume p is true and 1 < i < n and fact = i! after executing loop i was incremented by 1, i.e. i + 1 therefore i  n fact := i!(i + 1) therefore fact = (i+1)! … and i has been incremented Therefore p is a loop invariant

An example of a loop invariant Program Correctness An example of a loop invariant i := 1; fact := 1; while i < n do begin i := i + 1; fact := fact * i; end Therefore p is a loop invariant Therefore the assumption [p and (i < n)]{S}p is true Therefore it follows that p{while i<n do S}[i >= n and p] is true The while loop terminates i starts at 1, assuming n  0 i is incremented inside loop eventually i will equal n Therefore the program segment is correct

Program Correctness An example, min(x,y) Program segment S is as follows if x < y then min := x else min := y Initial assertion p: is True Final assertion q: (x  y and min = x) or (x > y and min = y) Consider three cases (1) p and x < y min is set to x (x  y and min = x) (2) p and x = y min is set to y, which equals x (x  y and min = x) (3) p and x > y min is set to y (x > y and min = y) Therefore S is correct with respect to p and q Question 4

Program Correctness An example, ? Initial assertion p: is True Final assertion q: x  y Consider two cases (1) p and x  y S is not executed q is true (2) p and x > y x := x + y y := x - y = (x + y) - y = x x := x - y = (x + y) - x = y x and y are now swapped, so y is now greater than x Therefore S is correct with respect to p and q if x > y then begin x := x + y; y := x - y; x := x - y; end;

Program Correctness So? For each program segment S we need an initial assertion p a final assertion q If it is a loop we need to establish a loop invariant p We need to apply the appropriate rules of inference Generally we need to decompose program It takes time, it aint easy Could we automate the process? For partial correctness For correctness What do we do in an industrial setting