© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.

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?
Ceg860(Prasad)L10DC1 Design by Contract Invariants Pre-post conditions : Rights and Obligations Exceptions : Contract Violations.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Karlstad University Computer Science Design Contracts and Error Management Design Contracts and Errors A Software Development Strategy Eivind J. Nordby.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 3: Abstract Data Types.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Data Abstraction II SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic 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.
Design by Contract ™. 2 Design by Contract A discipline of analysis, design, implementation, management.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
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.
Jan 2005 Ron McFadyen1 Contracts Used to help understand requirements more completely (and so may not always be necessary) based on assertions;
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.
1 Specifying Object Interfaces. 2 Major tasks in this stage: --are there any missing attributes or operations? --how can we reduce coupling, make interface.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 10: Project Presentation Ilinca.
Chair of Software Engineering ATOT - Lecture 12, 12 May Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 26, 30 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 12: Design by Contract™
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 6 7 October 2008.
Chair of Software Engineering OOSC - Lecture 4 1 Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 4: The Interface of a Class.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 9: Contracts and Inheritance (based on work with.
Chair of Software Engineering 1 Introduction to Programming Exercise Session Week 9 M. Piccioni 17/18 November 2008.
Chair of Software Engineering ATOT - Lecture 11, 7 May Advanced Topics in Object Technology Bertrand Meyer.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Design by Contract David Talby. Software Correctness When is a class correct? – It ’ s a relative concept; what is required? – But it ’ s the correct.
Software Verification Bertrand Meyer Chair of Software Engineering Lecture 2: Axiomatic semantics.
Eiffel Language and Design by Contract Contract –An agreement between the client and the supplier Characteristics –Expects some benefits and is prepared.
Software Engineering Design by Contract 1 Design by Contract ™
Ranga Rodrigo. Class is central to object oriented programming.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract,
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
OOSCCh11r1:DbC RJL Slide #1 OOSC: Ch11 vs. Predecessors [Meyer:] Equipped with the basic concepts of class, object and genericity, you can by now.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
© Paul Ammann, 2008 Design by Contract Paul Ammann CS/SWE 332.
Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, Mitchell, R., and McKim, Design by Contract,
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
L13: Design by Contract Definition Reliability Correctness Pre- and post-condition Asserts and Exceptions Weak & Strong Conditions Class invariants Conditions.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
Understanding ADTs CSE 331 University of Washington.
Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 1 COSC3311 – Software Design Loop invariant/variant.
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.
Defensive Programming CNS 3370 Copyright 2003, Fresh Sources, Inc.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
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.
Object Design More Design Patterns Object Constraint Language Object Design Specifying Interfaces Review Exam 2 CEN 4010 Class 18 – 11/03.
Design by Contract. The Goal Ensure the correctness of our software (correctness) Recover when it is not correct anyway (robustness) Correctness: Assertions.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Chapter 3 of Programming Languages by Ravi Sethi
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by contract Object-Oriented Software Construction by Bertrand Meyer, Prentice Hall The presence of a precondition or postcondition in a routine.
SWE 619 Software Construction Last Modified, Fall 2015 Paul Ammann
Design by contract Object-Oriented Software Construction by Bertrand Meyer, Prentice Hall The presence of a precondition or postcondition in a routine.
Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract, Computer,
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Presentation transcript:

© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice Hall). Included here by permission of ISE, for the benefit of IDC students. Other uses, duplication or distribution require permission from ISE. For more material see

© Bertrand Meyer and Yishai Feldman Correctness Formulas The meaning of the correctness formula {P} A {Q} is that any execution of the program A, starting in a state where P holds, will terminate in a state where Q holds. Example: {x  9} x := x + 5 {x  13}

© Bertrand Meyer and Yishai Feldman The Perfect Jobs u { False } A { … } u { … } A { True } Definition: A predicate P is stronger than a predicate Q if whenever P is true, Q is also true. That is, if P  Q holds.

© Bertrand Meyer and Yishai Feldman Stack with Assertions (1) class STACK1 [G] feature -- Access count: INTEGER -- Number of stack elements item: G is -- Top element require not empty do  end

© Bertrand Meyer and Yishai Feldman Stack with Assertions (2) feature -- Status report empty: BOOLEAN is -- Is stack empty? do  end full: BOOLEAN is -- Is stack representation full? do  end

© Bertrand Meyer and Yishai Feldman Stack with Assertions (3) feature -- Element change put (x: G) is -- Add x on top. require not full do  ensure not empty item = x count = old count + 1 end

© Bertrand Meyer and Yishai Feldman Stack with Assertions (4) remove is -- Remove top element. require not empty do  ensure not full count = old count – 1 end

© Bertrand Meyer and Yishai Feldman The Contract The clauses "require pre" and "ensure post" in a routine r imply the following contract: ¬ The caller may only call r when pre is satisfied. ­ If r is called with pre satisfied, it must return in a state that satisfies post.

© Bertrand Meyer and Yishai Feldman Benefits and Obligations

© Bertrand Meyer and Yishai Feldman Non-Redundancy Principle Under no circumstances shall the body of a routine ever test for the routine's precondition.

© Bertrand Meyer and Yishai Feldman Input Validation require input > 0 External ObjectsInput and validation modulesProcessing modules

© Bertrand Meyer and Yishai Feldman Assertion Violation Rule  A run-time assertion violation is the manifestation of a bug in the software.  A precondition violation is the manifestation of a bug in the client.  A postcondition violation is the manifestation of a bug in the supplier.

© Bertrand Meyer and Yishai Feldman Stack Implemented by Array

© Bertrand Meyer and Yishai Feldman Stack Implementation (1) indexing description: "Stacks: Dispenser structures with a Last-In, First-Out access policy, and a fixed maximum capacity" class STACK2 [G] creation make feature -- Initialization

© Bertrand Meyer and Yishai Feldman Stack Implementation (2) make (n: INTEGER) is -- Allocate stack for a maximum of n elements require positive_capacity: n >= 0 do capacity := n !! representation. make (1, capacity) ensure capacity_set: capacity = n array_allocated: representation /= Void stack_empty: empty end

© Bertrand Meyer and Yishai Feldman Stack Implementation (3) feature -- Access capacity: INTEGER -- Maximum number of stack elements count: INTEGER -- Number of stack elements item: G is -- Top element require not_empty: not empty -- i.e. count > 0 do Result := count end

© Bertrand Meyer and Yishai Feldman Stack Implementation (4) feature -- Status report empty: BOOLEAN is -- Is stack empty? do Result := (count = 0) ensure empty_definition: Result = (count = 0) end

© Bertrand Meyer and Yishai Feldman Stack Implementation (5) full: BOOLEAN is -- Is stack full? do Result := (count = capacity) ensure full_definition: Result = (count = capacity) end

© Bertrand Meyer and Yishai Feldman Stack Implementation (6) feature -- Element change put (x: G) is -- Add x on top require not_full: not full -- i.e. count < capacity in this representation do count := count + 1 representation. put (count, x) ensure not_empty: not empty added_to_top: item = x one_more_item: count = old count + 1 in_top_array_entry: count = x end

© Bertrand Meyer and Yishai Feldman Stack Implementation (7) remove is -- Remove top element require not_empty: not empty -- i.e. count > 0 do count := count – 1 ensure not_full: not full one_fewer: count = old count – 1 end

© Bertrand Meyer and Yishai Feldman Stack Implementation (8) feature {NONE} -- Implementation representation: ARRAY [G] -- The array used to hold the stack elements invariant count_non_negative: 0 <= count count_bounded: count <= capacity consistent_with_array_size: capacity = representation. capacity empty_if_no_elements: empty = (count = 0) item_at_top: (count > 0) implies (representation. item (count) = item) end -- class STACK2

© Bertrand Meyer and Yishai Feldman Tolerant Routines remove is -- Remove top element do if empty then print ("Error: attempt to pop an empty stack") else count := count – 1 end

© Bertrand Meyer and Yishai Feldman Reasonable Precondition Principle Every routine precondition must satisfy the following requirements: The precondition appears in the official documentation distributed to authors of client modules. It is possible to justify the need for the precondition in terms of the specification only.

© Bertrand Meyer and Yishai Feldman Precondition Availability Rule Every feature appearing in the precondition of a routine must be available to every client to which the routine is available. class SNEAKY feature tricky is require accredited do  end feature {NONE} accredited: BOOLEAN is do  end end

© Bertrand Meyer and Yishai Feldman Invariant Rule An assertion I is a correct class invariant for a class C if and only if it meets the following two conditions: E1 Every creation procedure of C, when applied to arguments satisfying its precondition in a state where the attributes have their default values, yields a state satisfying I. E2 Every exported routine of the class, when applied to arguments and a state satisfying both I and the routine’s precondition, yields a state satisfying I.

© Bertrand Meyer and Yishai Feldman Class Correctness A class C is correct with respect to its assertions if and only if: C1 For any valid set of arguments x p to a creation procedure p: {Default C and pre p (x p )} Body p {post p (x p ) and INV} C2 For every exported routine r and any set of valid arguments x r : {pre r (x r ) and INV} Body r {post r (x r ) and INV}

© Bertrand Meyer and Yishai Feldman Reminder: ADT Specification of Stacks TYPES STACK [G] FUNCTIONS put: STACK [G]  G  STACK [G] remove: STACK [G]  STACK [G] item: STACK [G]  G empty: STACK [G]  BOOLEAN new: STACK [G] AXIOMS For any x: G, s: STACK [G] A1 item (put (s, x)) = x A2 remove (put (s, x)) = s A3 empty (new) A4 not empty (put (s, x)) PRECONDITIONS remove (s: STACK [G]) require not empty (s) item (s: STACK [G]) require not empty (s)

© Bertrand Meyer and Yishai Feldman The Abstraction Function

© Bertrand Meyer and Yishai Feldman Correctness of the Implementation CONC_2 ABST_1ABST_2 CONC_1 cf af aa Concrete objects (instances of the class C) Abstract objects (instances of the ADT A) af a = a cf

© Bertrand Meyer and Yishai Feldman Assertion Instructions x := a ^2 + b^2  Other instructions  check x >= 0 -- Because x was computed above as a sum of squares. end y := sqrt (x)

© Bertrand Meyer and Yishai Feldman Buggy Binary Search (1) from i := 1; j := n until i = j loop m := (i + j) // 2 if m <= x then i := m else j := m end Result := (x = j)

© Bertrand Meyer and Yishai Feldman Buggy Binary Search (2) from i := 1; j := n; found := false until i = j or found loop m := (i + j) // 2 if m < x then i := m + 1 elseif m = x then found := true else j := m – 1 end Result := found

© Bertrand Meyer and Yishai Feldman Buggy Binary Search (3) from i := 0; j := n until i = j loop m := (i + j + 1) // 2 if m <= x then i := m + 1 else j := m end if i >= 1 and i <= n then Result := (x = i) else Result := false end

© Bertrand Meyer and Yishai Feldman Correct Binary Search from i := 1; j := n invariant -- x in t [1..n] implies i <= x <= j 1 <= i <= j <= n variant j – i until i = j loop m := (i + j) // 2 check i <= m < j end if m < x then i := m + 1 else j := m end Result := (x = i)

© Bertrand Meyer and Yishai Feldman Breaking the Loop: Generating the Verification Conditions Start Init Exit? Body End YES NO “Precondition” Invariant “Postcondition” (1) (2) (3)

© Bertrand Meyer and Yishai Feldman Proving Loop Correctness Step 1: The invariant follows from the loop precondition and the initialization. {Loop-precond} Init {Invariant} Step 2: The invariant is maintained by one pass through the loop. {Invariant   Exit} Body {Invariant} Step 3: The loop postcondition follows from the invariant. Invariant  Exit  Loop-postcond

© Bertrand Meyer and Yishai Feldman Proving Loop Termination Step 4: The variant is always non-negative (should follow from the invariant). Invariant  Variant  0 Step 5: The variant decreases by at least 1 in every pass through the loop. Invariant  Variant < Previous-Variant

© Bertrand Meyer and Yishai Feldman Proving Loop Correctness with Verification Conditions u Verification conditions refer to loop- free code. u The proofs of Steps 1–5 are not inductive! u If you feel the need to refer to what goes on throughout the loop computation, you are missing an invariant!

© Bertrand Meyer and Yishai Feldman Loop Computations