CSE115/ENGR160 Discrete Mathematics 04/12/11 Ming-Hsuan Yang UC Merced 1.

Slides:



Advertisements
Similar presentations
In this episode of The Verification Corner, Rustan Leino talks about Loop Invariants. He gives a brief summary of the theoretical foundations and shows.
Advertisements

Functional Verification III Prepared by Stephen M. Thebaut, Ph.D. University of Florida Software Testing and Verification Lecture Notes 23.
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.
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
1/22 Programs : Semantics and Verification Charngki PSWLAB Programs: Semantics and Verification Mordechai Ben-Ari Mathematical Logic for Computer.
CSE115/ENGR160 Discrete Mathematics 04/24/12 Ming-Hsuan Yang UC Merced 1.
CSE115/ENGR160 Discrete Mathematics 04/05/11 Ming-Hsuan Yang UC Merced 1.
CSE115/ENGR160 Discrete Mathematics 02/07/12
CSE115/ENGR160 Discrete Mathematics 01/31/12 Ming-Hsuan Yang UC Merced 1.
CSE115/ENGR160 Discrete Mathematics 02/01/11
Discrete Structures Chapter 2 Part B Mathematical Induction
CSE115/ENGR160 Discrete Mathematics 03/22/12 Ming-Hsuan Yang UC Merced 1.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
CSE115/ENGR160 Discrete Mathematics 03/10/11
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,
Floyd Hoare Logic. Semantics A programming language specification consists of a syntactic description and a semantic description. Syntactic description:symbols.
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.
Induction and recursion
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 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.
1 Sections 1.5 & 3.1 Methods of Proof / Proof Strategy.
Program Correctness. 2 Program Verification An object is a finite state machine: –Its attribute values are its state. –Its methods optionally: Transition.
Recursive Algorithms &
Program Analysis and Verification Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics I Roman Manevich Ben-Gurion University.
Copyright © Cengage Learning. All rights reserved.
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen.
More on Correctness. Prime Factorization Problem: Write a program that computes all the prime factors of a given number Solution (Idea): Factors are less.
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.
Discrete Mathematics ? Transparency No. 2-0 Chapter 3 Induction and Recursion Transparency No. 3-1 formal logic mathematical preliminaries.
Static Techniques for V&V. Hierarchy of V&V techniques Static Analysis V&V Dynamic Techniques Model Checking Simulation Symbolic Execution Testing Informal.
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 26.
Program Correctness. The designer of a distributed system has the responsibility of certifying the correctness of the system before users start using.
1 Computer Algorithms Tutorial 2 Mathematical Induction Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
CSE15 Discrete Mathematics 04/12/17
Math/CSE 1019C: Discrete Mathematics for Computer Science Fall 2012
Formal Methods in Software Engineering 1
Mathematical Induction Recursion
Mathematical Structures for Computer Science Chapter 1
Copyright © Cengage Learning. All rights reserved.
Axiomatic semantics Points to discuss: The assignment statement
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Applied Discrete Mathematics Week 9: Integer Properties
Axiomatic Verification I
Predicate Transformers
Formal Methods in software development
Axiomatic Semantics Will consider axiomatic semantics (A.S.) of IMP:
Output Variables {true} S {i = j} i := j; or j := i;
Axiomatic Verification I
Mathematical Induction
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
Program Correctness an introduction.
Presentation transcript:

CSE115/ENGR160 Discrete Mathematics 04/12/11 Ming-Hsuan Yang UC Merced 1

4.5 Program correctness Suppose we have designed an algorithm to solve a problem with a program After debugging, how can we be sure that the program always get the correct answer? Need a proof to show that the program always gives the correct answer Program verification: uses the rules of inference and proof techniques 2

Program verification One form of formal verification It can be carried out using a computer However, only limited progress has been made Some mathematicians and theoretical computer scientists argue that it will never be realistic to mechanize the proof of correctness of complex programs 3

Program verification A program is said to be correct if it produces the correct output for every possible input A proof that a program is correct consists of two parts – Partial correctness: Correct answer is obtained if the program terminates – Shows the program always terminates 4

Partially correct To specify a program produces the correct output – Initial assertion: the properties that the input values must have – Final assertion: the properties that output of the program should have, if the program did what was intended A program or program segment, S is said to be partially correct with respect to the initial assertion p and the final assertion q if whenever p is true for the input values of S and S terminates, then q is true for the output values of S. The notation p{S}q indicates that the program, or program segment, S is partially correct with respect to the initial assertion p and the final assertion q 5

Hoare triple The notation p{S}q is known as one Hoare triple Show that the program segment y:=2 z:=x+y is correct w.r.t. the initial assertion p: x=1 and the final assertion q: z=3 Suppose that p is true, so that x=1. Then y is assigned the value 2, and z is assigned the sum of the values of x and y, which is 3. Hence, S is correct w.r.t the initial assertion p and the final assertion q. Thus p{S}q is true 6

Rules of inference Suppose the program S is split into subprograms S 1 and S 2, denote it by S=S 1 ;S 2 Suppose the correctness of S 1 w.r.t. the initial assertion p and final assertion q, the correctness of S 2 w.r.t. the initial assertion q and the final assertion r It follows if p is true and S 1 is executed and terminates, then q is true; and if q is true and S 2 executes and terminates, then r is true 7

Rules of inference This, if p is true and S=S 1 ;S 2 is executed and terminates, then r is true This rule of inference, called the composition rule can be stated as 8

Conditional statements Consider a program statement if condition then S To verify this segment is correct w.r.t. initial p and final assertion q, two things must be done – First, when p is true and condition is true, then q is true after S terminates – Second, when p is true and condition is false, then q is true (as S is not executed) 9

Conditional statements The rule of inference 10

Condition statement Verify the program segment if x > y then y:=x is correct w.r.t the initial assertion T and the final assertion y≥x When the initial assertion is true and x>y, then the assignment y:=x is carrier out. Thus, the final assertion is true When the initial assertion is true and x>y is false, so x≤y, and the final assertion is true Hence, using the rule of inference for conditional statements, this program is correct w.r.t. the given initial and final assertions 11

Conditional statements if condition then S 1 else S 2 To verify this program segment is correct w.r.t. the initial assertion p and the final assertions q First, show that when p is true and condition is true, then q is true after S 1 terminates Second, show that when p is true and condition is false, then q is true after S 2 terminates 12

Conditional statements The rule of inference 13

Conditional statements Verify the program segment if x < 0 then abs:=-x else abs:=x is correct w.r.t. the initial assertion T and the final assertion abs=|x| Two things must be demonstrated. First, it must be shown that if the initial condition is true and x<0, then abs=|x|. This is correct as when x<0, the assignment statement abs:=-x sets abs=-x, which is |x| by definition when x<0 14

Conditional statements Second, it must be shown that if the initial assertion is true and x<0 is false, so that x≥0, then abs=|x|. This is correct as in this case the program uses the assignment abs:=x, and x is |x| by definition when x≥0, so that abs:=x Hence, using the rule of inference for program segments of this type, this segment is correct w.r.t. the given initial and final assertions 15

Loop invariants Proof of correctness of while loops while condition S Note that S is repeatedly executed until condition becomes false An assertion that remains true each time S is executed must be chosen Such an assertion is called a loop invariant That is, p is a loop invariant if (p ∧condition){S}p is true 16

Loop invariants Suppose that p is a loop invariant, it follows that is p is true before the program segment is executed, p and ¬condition are true after terminates, if it occurs 17

Example A loop invariant is needed to verify the segment i:=1 factorial:=1 while i<n begin i:=i+1 factorial:=factorial ∙i end terminates with factorial=n! when n is a positive integer 18

Example Let p be the assertion “factorial=i! and i≤n”. We first prove that p is a loop invariant Suppose that at the beginning of one execution of the while loop, p is true and the condition holds, i.e., assume that factorial=i! that i<n The new values i new and factorial new of i and factorial are i new =i+1≤n and factorial new =factorial ∙(i+1) = (i+1)! = i new ! Because i<n, we also have i new =i+1 ≤n Thus p is true at the end of the execution of the loop This shows that p is a loop invariant 19

Example Just before entering the loop, i=1≤n and factorial=1=1!=i! both hold, so p is true As p is a loop invariant, the rule of inference implies that if the while loop terminates, it terminates with p true and with i<n false In this case, at the end, factorial=i! and i≤n are true, but i<n is false; in other words, i=n and factorial=i!=n!, as desired Finally, need to check that while loop actually terminates At the beginning of the program i is assigned the value 1, so that after n-1 traversals of the loop, the new value of i will be n, and the loop terminates at that point 20

Example Verify the correctness of the program S procedure multiply(m, n: integers) 21

Example Example S=S 1 ;S 2 ;S 3 ;S 4 Want to show p{S}t is true using p{S 1 }q, q{S 2 }r, r{S 3 }s, s{S 4 }t p{S 1 }q:Let p be the initial assertion “m and n are integers” then it can be shown that p{S 1 }q is true when q is the proposition p ∧(a=|n|) q{S 2 }r: Next, let r be q ∧(k=0)∧(x=0), it is easily to verify q{ S 2 }r is true 22

Example r{S 3 }s: It can be shown that “x=mk and k≤a” is an invariant for the loop in S 3 Furthermore, it is easy to see that the loop terminates after a iterations, with k=a, so x=ma at this point As r implies that x=m ⋅0 and 0 ≤a, the loop invariant is true before the loop is entered As the loop terminates with k=a, it follows that r{S 3 }s is true where s is the proposition “x=ma and a=|n|” s{S 4 }t: Finally, it can be shown that S 4 is correct w.r.t. the initial assertion s and final assertion t, where t is the proposition “product=mn” As p{S 1 }q, q{S 2 }r, r{S 3 }s, s{S 4 }t are true, by the rule of composition, p{S}t is true 23