Department of Computer Science, York University Object Oriented Software Construction 22/01/2016 11:58 AM 1 COSC3311 – Software Design Loop invariant/variant.

Slides:



Advertisements
Similar presentations
Chapter 4: Control Structures I (Selection)
Advertisements

Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Control Structures Ranga Rodrigo. Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Reasoning About Code; Hoare Logic, continued
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
Chair of Software Engineering Software Architecture Prof. Dr. Bertrand Meyer Lecture 6: Exception Handling.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
Design by Contract ™. 2 Design by Contract A discipline of analysis, design, implementation, management.
Chair of Software Engineering ATOT - Lecture 10, 5 May Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 25, 30 June Advanced Topics in Object Technology Bertrand Meyer.
Design by Contract. Specifications Correctness formula (Hoare triple) {P} A {Q} – A is some operation (for example, a routine body) – P and Q are predicates.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 15: Exception handling.
-5- Exception handling What is an exception? “An abnormal event” Not a very precise definition Informally: something that you don’t want to happen.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 12, 12 May Advanced Topics in Object Technology Bertrand Meyer.
Chapter 2: Algorithm Discovery and Design
IMSE Week 18 White Box or Structural Testing Reading:Sommerville (4th edition) ch 22 orPressman (4th edition) ch 16.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 9: Contracts and Inheritance (based on work with.
Software Verification Bertrand Meyer Chair of Software Engineering Lecture 2: Axiomatic semantics.
Describing Syntax and Semantics
Software Engineering Design by Contract 1 Design by Contract ™
Chapter 2: Algorithm Discovery and Design
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.
Ranga Rodrigo. Class is central to object oriented programming.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
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.
CSI 3125, Axiomatic Semantics, page 1 Axiomatic semantics The assignment statement Statement composition The "if-then-else" statement The "while" statement.
Department of Computer Science, York University Slides based on Object Oriented Software Construction 16/10/2015 4:08 PM 0 COSC3311 – Software Design Gotos,
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 5: Sequences, Mathematical Induction, and Recursion 5.5 Application: Correctness of Algorithms 1 [P]rogramming reliability – must be an activity.
Reasoning about programs March CSE 403, Winter 2011, Brun.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 8: Control Structures I.
CPS120 Introduction to Computer Science Iteration (Looping)
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
1 Assertions. 2 A boolean expression or predicate that evaluates to true or false in every state In a program they express constraints on the state that.
Static Techniques for V&V. Hierarchy of V&V techniques Static Analysis V&V Dynamic Techniques Model Checking Simulation Symbolic Execution Testing Informal.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
1 Exceptions When the Contract is Broken. 2 Definitions A routine call succeeds if it terminates its execution in a state satisfying its contract A routine.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Design by Contract. The Goal Ensure the correctness of our software (correctness) Recover when it is not correct anyway (robustness) Correctness: Assertions.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
Chapter 3 of Programming Languages by Ravi Sethi
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Correctness issues and Loop invariants
Reasoning About Code.
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Chapter 6: Repetition Statements
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Design by Contract – Exceptions
Presentation transcript:

Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 1 COSC3311 – Software Design Loop invariant/variant

Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 2 Review: Concept of State Each state is a combination of run-time instance values for all the class attributes Expressed as a tuple of size corresponding to the number of class attributes a, b: BOOLEAN ,,,

Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 3 Review: Correctness in software Basic notation: (P, Q: assertions, i.e. properties of the state of the computation. A: instructions). {P} A {Q}  “Hoare triple”  What this means (total correctness): oAny execution of A started in a state satisfying P will terminate in a state satisfying Q.

Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 4 Review: The correctness of a class  For every creation procedure cp: {pre cp } do cp {post cp and INV}  For every exported routine r: {INV and pre r } do r {post r and INV}  The worst possible erroneous run-time situation in object-oriented software development: oProducing an object that does not satisfy the invariant of its own class. a.f (…) a.g (…) a.f (…) create a.make (…) S1 S2 S3 S4

Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 5 Loop trouble  Loops are needed, powerful  But very hard to get right: o“off-by-one” oInfinite loops oImproper handling of borderline cases  For example: binary search feature

Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 6 OOSC2 page 381

Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 7 The answer: controlled loops/assertions  Use of loop variants and invariants.  A loop is a way to compute a certain result by successive approximations.  (e.g. computing the maximum value of an array of integers)

Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 8 Computing the max of an array  Approach by successive slices: max_of_array (t: ARRAY [INTEGER]): INTEGER -- Maximum value of array t local i: INTEGER do from i := t.lower Result := lower until i = t.upper loop i := i + 1 Result := Result.max i) end

Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 9 Loop variants and invariants  Syntax: from init invariant inv-- Correctness property until exit loop body variant var-- Ensure loop termination. end

Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 10 Maximum of an array (cont’d) max_of_array (t: ARRAY [INTEGER]): INTEGER -- Maximum value of array t local i: INTEGER do from i := t.lower Result := t.lower invariant -- Result is the max of the elements of t at indices -- t.lower to i until i = t.upper loop i := i + 1 Result := Result.max i) variant t.upper – i end

Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 11 Maximum of an array (cont’d) max_of_array (t: ARRAY [INTEGER]): INTEGER -- Maximum value of array t local i: INTEGER do from i := t.lower Result := t.lower invariant until i = t.upper loop i := i + 1 Result := Result.max i) variant t.upper – i end

Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 12 Loop Invariant Invariant: t.lower <= i and i <= t.upper and t.subarray (t.lower, i).for_all (agent max(Result, ?)) max (r, g: INTEGER) : BOOLEAN do Result := r >= g end

Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 13 Contract View class interface ARRAY_MAX feature max (r, g: INTEGER): BOOLEAN max_of_array (t: ARRAY [INTEGER]): INTEGER -- maxium value of array `t' require P: t /= Void ensure Q: t.for_all (agent max (Result, ?)) end

Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 14 Terminology routine require P do from initial invariant I until B do loop-body variant V end ensure Q end

Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 15 Successive approximations

Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 16 Verifying a routine with a loop  Partial correctness oShow that invariant I is true before execution of the loop begins, i.e. show: {P} initial {I} oShow that: {I and not B} loop-body {I} oShow that: (I and B) implies (Q and V = 0)  Termination oShow that: (I and not B) implies V > 0 oShow that: {I and not B} loop-body {V < old V}

Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 17 Infinite Loop

Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 18 What’s wrong with `break’ | | Subject: AT& T Bug | | Date: Fri Jan 19 12: 18: | | | This is the bug that caused the AT& T breakdown the other day: | | In the switching software (written in C), | | there was a long do... while construct, | | which contained a switch statement, | | which contained an if clause, | | which contained a break, | | which was intended for the if clause, | | but instead broke from the switch.  A missing “break” statement brought down the entire long- distance telephone network in the North Eastern US, resulting in millions of dollars of damage!

Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 19 Why not use Breaks and Go-Tos  Thirty five years ago, Edsger Dijkstra, followed by others, made a powerful case against using control structures other than the one-entry, one-exit blocks that came to be known as "the control structures of structured programming". They are, with a few complements, the control structures of Eiffel: sequence (implicit most of the time since you may omit the semicolon), if-then-else, inspect, loop, routine.  They are all one-entry, one-exit, meaning you can read the code sequentially and don't have to turn yourself into a computer and apply operational reasoning of the style "if when executing this instruction I'm coming from here it will satisfy this property, but if I am coming from there it will satisfy that other property, so in both cases... it will be true that... maybe... wait a minute... etc.  Some people view "break" and "continue" as milder forms of the goto that do not raise the same problems. But in fact these forms are just as bad. Note in particular that what makes it possible really to understand a loop is the notion of loop *invariant*: a property that is ensured by the initialization, and maintained by every iteration. The invariant is like an approximation of the loop's goal (e.g. if we want to compute the maximum of a structure -- the final goal -- the invariant states that Result is the maximum *of the elements seen so far*). Combined with the loop exit condition (in the example, "we've now seen all elements") it allows the program reader to check, through a simple inspection of the loop text, that the loop is doing its job. The loop *variant* plays a complementary role by telling us which decreasing quantity guarantees that the loop will terminate.  These notions are simple to understand and use thanks to the one-entry, one-exit form of loops and other control structures. Introduce `break', `continue' and other goto instructions (in sheep's clothing or not) and they disappear, or at least are replaced with much more complicated rules.  The key point is to be able to reason about a program statically, i.e. by associating properties with its text as given, rather than operationally, i.e. by trying to mimic its execution within your head. Computers are there to execute programs; humans can't really do it effectively for any non-trivial examples. The one-entry, one-exit structure helps us do something at which we are much better: static reasoning based on the simple rules of logic.  In practice it is not surprising that if you have been used to goto-prone languages you will find it a bit hard at first to stick to the one-entry, one-exit discipline. I am sure that if you try earnestly to overcome what initially seems like a hurdle you will very quickly find that your programming style improves, that the control structure of your program becomes simpler, and that your programs become more readable and maintainable. See