Tutorial Number 8 - Daniel Razavi

Slides:



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

Reasoning About Code; Hoare Logic, continued
Program Proving Notes Ellen L. Walker.
Introduction to Data Structures and Algorithms Chapter 7 Quick Sort.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
1 Advanced Material The following slides contain advanced material and are optional.
Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Proving Program Correctness The Axiomatic Approach.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
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)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
CSI 3125, Axiomatic Semantics, page 1 Axiomatic semantics The assignment statement Statement composition The "if-then-else" statement The "while" statement.
Recursion – means to recur or to repeat – A different way to get a robot to repeat an action A programming language that allows recursive definitions (and.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
Recursive Algorithms &
Reasoning about programs March CSE 403, Winter 2011, Brun.
Week 6.  Lab 1 and 2 results  Common mistakes in Style  Lab 1 common mistakes in Design  Lab 2 common mistakes in Design  Tips on PE preparation.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Feb 18, 2005.
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.
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.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
1 Section 8.2 Program Correctness (for imperative programs) A theory of program correctness needs wffs, axioms, and inference rules. Wffs (called Hoare.
Discrete Maths: Invariant/2 1 Discrete Maths Objectives – –to show the use of induction for proving properties of code involving loops use induction.
Aids to Formulating and Answering Key Questions NameWhen to use u Construction Methodproving “there exists” u Choose Method proving “for every” u Math.
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 27.
CompSci Today’s Topics Computer Science Noncomputability Upcoming Special Topic: Enabled by Computer -- Decoding the Human Genome Reading Great.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
DAY 4. MAKING SOMETHING ‘JUMP’ Simple! Move it a certain amount ‘up’ Glide back to your original spot.
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
DAST Tirgul 7.
Methods of Proof.
Math/CSE 1019C: Discrete Mathematics for Computer Science Fall 2012
The physics of falling In this section you will
Advanced Algorithms Analysis and Design
UNIT 15 Suggestions 提議.
CS2210:0001Discrete Structures Induction and Recursion
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
Proving Loops Testing debugging and verification
If, else, elif.
Algorithm Analysis CSE 2011 Winter September 2018.
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
2008/09/24: Lecture 6b CMSC 104, Section 0101 John Y. Park
CS21b: Structure and Interpretation
Iterations Programming Condition Controlled Loops (WHILE Loop)
Indirect Argument: Contradiction and Contraposition
Interesting Integers!.
Axiomatic semantics Points to discuss: The assignment statement
Programming Languages and Compilers (CS 421)
CSC 143 Error Handling Kinds of errors: invalid input vs programming bugs How to handle: Bugs: use assert to trap during testing Bad data: should never.
GCD and LCM relationship
Midterm Discussion.
Big-O, Ω, ϴ Trevor Brown CSC263 Tutorial 1 Big-O, Ω, ϴ Trevor Brown
The physics of falling In this section you will
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
CSE 373 Optional Section Led by Yuanwei, Luyi Apr
More Mathematical Reasoning (Conditional Statements)
CSE Winter 2008 Introduction to Program Verification
Programming Languages and Compilers (CS 421)
Tutorial Number 6 - Daniel Razavi
Tutorial 2 - Daniel Razavi
How to allow the program to know when to stop a loop.
Program Correctness an introduction.
Data Structures & Programming
Presentation transcript:

Tutorial Number 8 - Daniel Razavi CSC 236 Tutorial Number 8 - Daniel Razavi

How was the midterm?

Today’s Problem Set

1. No: “a” doesn't decrease in the 'if' branch, “a” increases there.

2. No again: a+b doesn't decrease in the 'if' branch. a+b stays equal after the swap.

3. In the 'else' branch, “a” decreases, so “a” should probably be part of this. In the 'if' branch, we know that the larger value is “b”, and that “b” decreases after the swap. The variant should therefore include something that gives more "weight" to “b”, so that this increased weight drags the invariant down after the swap. Here's one: 2b+a why?

2b+a Here is why! First, we need to prove that the variant decreases on each loop iteration There are two cases to consider here. The “else” case is simpler, so let's do that one first. In that branch, a decreases by 1, so 2b+a decreases by 1. Notes.pdf page 59

2b+a Here is why! Notes.pdf page 59 Now for the “if” case, where “a” and “b” are swapped. Before the loop iteration, we have values a_0, b_0, and m_0 = 2b_0+a_0; also, by the if-statement, we have a_0 < b_0. After the iteration, we have a_1 = b_0, b_1 = a_0, and m_1 = 2b_1+a_1 = 2a_0+b_0. We must show that 2a_0+b_0 < 2b_0+a_0. This is equivalent to proving that a_0 < b_0 which is exactly what we know to be true by virtue of the if-condition being true. Therefore, our variant is valid for the loop, and we conclude that the loop terminates.

2b+a Here is why! Secondly, we need to prove that the variant is part of the natural number. By the loop-condition, we have that a >= 0 and b >= 0. Therefore, 2b+a >= 0. Notes.pdf page 59

gcd1: this is still correct. When a == 1, the precondition forces b to be 1, so it's unnecessary to check that a == 1 here too. (One other thing to consider: what happens if we remove the a >= b precondition? Is the code still correct? It is! Even without the a == 1 in there.)

gcd2: this one might "return the correct answer", but a correctness proof will fail because we can make a recursive call that violates the precondition; e.g. gcd2(8, 4) calls gcd2(4, 0), and we don't know what happens when a value of 0 is supplied for b. To give a proof of correctness, we would have to change the precondition to allow a and b to be integers >= 0.

Tutorial Number 8 - Daniel Razavi CSC 236 Tutorial Number 8 - Daniel Razavi