Loop Invariants CSC 171 FALL 2001 LECTURE 6.

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?
Copyright W. Howden1 Programming by Contract CSE 111 6/4/2014.
Reasoning About Code; Hoare Logic, continued
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
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.
Proof Points Key ideas when proving mathematical ideas.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
CSE115/ENGR160 Discrete Mathematics 04/12/11 Ming-Hsuan Yang UC Merced 1.
Iteration & Branching CSC 171 FALL 2001 LECTURE 5.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
General Announcements Project Due Friday, 1/30 Labs start Wednesday & Thursday – Java review – Weiss 1.19, 1.20 – You may show up & hand in Workshops.
ITERATION CSC 171 FALL 2004 LECTURE 10. Simple Branching If (test){ A;} start end A; test.
CS 1431 DB HISTORY. CS 1432 Sumerian Clay Tokens.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
BOOLEAN LOGIC CSC 171 FALL 2004 LECTURE 7. ASSIGNMENT Review Quiz # 2 Start reading Chapter 5.
Describing Syntax and Semantics
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.
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.
Halting Problem. Background - Halting Problem Common error: Program goes into an infinite loop. Wouldn’t it be nice to have a tool that would warn us.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Mathematics throughout the CS Curriculum Support by NSF #
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Computer Science School of Computing Clemson University Discrete Math and Reasoning about Software Correctness Murali Sitaraman
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
CSC201 Analysis and Design of Algorithms Asst.Proof.Dr.Surasak Mungsing Oct-151 Lecture 2: Definition of algorithm and Mathematical.
Section 3.1: Proof Strategy Now that we have a fair amount of experience with proofs, we will start to prove more difficult theorems. Our experience so.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
BY Lecturer: Aisha Dawood.  an algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces.
Program Correctness. 2 Program Verification An object is a finite state machine: –Its attribute values are its state. –Its methods optionally: Transition.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
1 Put your name if you want your attendance credit. Please put your as well. CSC 320 is done at 11:30 so I did NOT fill in the box for 11:30 for.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
CS101 Computer Programming I Chapter 4 Extra Examples.
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
Chapter 5: Sequences, Mathematical Induction, and Recursion 5.5 Application: Correctness of Algorithms 1 [P]rogramming reliability – must be an activity.
Recursive Algorithms &
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Feb 18, 2005.
Iterative Statements: while, for, do-while Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.
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.
How to Test Methods Computer Science 3 Gerb Objective: Test methods properly.
CSCI1600: Embedded and Real Time Software Lecture 28: Verification I Steven Reiss, Fall 2015.
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.
Program Correctness. The designer of a distributed system has the responsibility of certifying the correctness of the system before users start using.
CSE 311: Foundations of Computing Fall 2014 Lecture 27: Cardinality.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Excerpt from I Believe: Common Myths about Learning Mathematics Rita Barger, Ph.D. University of Missouri-Kansas City
Let’s try it one more time!. Allan Technique Programming Recursively 1.Decide that the problem needs a recursive solution. 2.Decide specifically what.
Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.
Math/CSE 1019C: Discrete Mathematics for Computer Science Fall 2012
History of Computers Abacus – 1100 BC
CSE 311: Foundations of Computing
Copyright © Cengage Learning. All rights reserved.
Lecture 5 Floyd-Hoare Style Verification
Programming Languages 2nd edition Tucker and Noonan
Logic for Computer Security Protocols
Chapter 6: Repetition Statements
Halting Problem.
CSE 1020:Software Development
I believe the reason students have difficulty learning algebra and that most people have trouble doing math word problems is that although children are.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
CSS161: Fundamentals of Computing
Programming Languages 2nd edition Tucker and Noonan
Programming Languages 2nd edition Tucker and Noonan
Useful GCD Fact If a and b are positive integers, then gcd(a,b) = gcd(b, a mod b) Proof: By definition of mod, a = qb+ (a mod b) for.
Program Correctness an introduction.
Presentation transcript:

Loop Invariants CSC 171 FALL 2001 LECTURE 6

History: Herman Hollerith Herman is said to have been a bright and able child at school, but had an inability to learn spelling easily. His determined teacher made his life miserable to the extent that he used to avoid school whenever possible and run away when his teacher showed renewed effort to improve his spelling.

History: Herman Tabulating 1890 - Herman Hollerith won the competition for the processing equipment to assist in the 1890 US Census The Hollerith Tabulating Company, eventually became the Calculating-Tabulating-Recording (C-T-R) company in 1914, and eventually was renamed IBM in 1924.

Loop invariants In order to verify loops we often establish an assertion (boolean expression) that is true each time we reach a specific point in the loop. We call this assertion, a loop invariant

Assertions When ever the program reaches the top of the while loop, the assertion is true INIT BODY INVARIANT TEST

Example Write a method to compute an public static int power(int a , int n) You have 5 minutes

Possible solution public static double power(int a, int n) { double r = 1; double b = a; int i = n ; while (i>0){ if (i%2 == 0) { b = b * b; i = i / 2;} else { r = r * b; i--; } return r; }

Does it work? b i r a 100 1 a2 50 a4 25 24 a8 12 a16 6 a32 3 2 a36 a64 a100 Does it work? SURE! TRUST ME! Well, look at a100 if you don’t believe me! Note, less loops! Can you “prove” that it works?

What is the loop invariant? At the top of the while loop, it is true that r*bi = an It is? Well, at the top of the first loop r==1 b==a i==n

So, if it’s true at the start Even case rnew= rold bnew == (bold)2 inew==(iold)/2 Therefore, rnew * (bnew)i-new == rold * ((bold)2)i-old/2 == rold * ((bold)2)i-old == an

So, if it’s true at the start II Odd case rnew= rold*bold bnew == bold inew==iold-1 Therefore, rnew * (bnew)i-new == rold *bold* (bold)i-old-1 == rold * (bold)i-old == an

r*bi = an So, If it’s true at the start And every time in the loop, it remains true Then, it is true at the end r*bi = an And, i == 0 ( the loop ended) What do we know?

Correctness Proofs Proof are more valuable than testing Tests demonstrate limited correctness Proofs demonstrate correctness for all inputs For some time, people hoped that all formal logic would replace programming The naïve idea that “programming is a form of math” proved to be an oversimplification

Correctness Proofs Unfortunately, in practice, these methods never worked very well. Instead of buggy programs, people wrote buggy logic Nonetheless, the approach is useful for program analysis

The take away message? In the end, engineering and (process) management are at least as important as mathematics and logic for the successful completion of large software projects