Copyright W. Howden1 Lecture 13: Programming by Contract.

Slides:



Advertisements
Similar presentations
Design by Contract.
Advertisements

Copyright W. Howden1 Programming by Contract CSE 111 6/4/2014.
Copyright , Doron Peled and Cesare Tinelli. These notes are based on a set of lecture notes originally developed by Doron Peled at the University.
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
1 Lecture 2: Processes, Requirements, and Use Cases.
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.
Copyright © 2006 Addison-Wesley. All rights reserved. 3.5 Dynamic Semantics Meanings of expressions, statements, and program units Static semantics – type.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Predicate Transformers
1/22 Programs : Semantics and Verification Charngki PSWLAB Programs: Semantics and Verification Mordechai Ben-Ari Mathematical Logic for Computer.
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
Copyright W. Howden1 Lecture 7: Functional and OO Design Descriptions.
CSE115/ENGR160 Discrete Mathematics 04/12/11 Ming-Hsuan Yang UC Merced 1.
Copyright W. Howden1 Lecture 6: Design Evaluation and Intro to OO Design Patterns.
Copyright W. Howden1 Lecture 8: O/O Programming. Copyright W. Howden2 Topics OO Programming Languages Developing programs from Designs –Class and method.
Copyright W. Howden1 Lecture 11: UML Terminology and Additional Models and Notation.
1 Specifying Object Interfaces. 2 Major tasks in this stage: --are there any missing attributes or operations? --how can we reduce coupling, make interface.
Copyright W. Howden1 Lecture 4: Sequence Interaction Diagrams.
Copyright W. Howden1 Lecture 5: Collaboration Diagrams.
Lecture 5b: Basic Design Patterns CSE 111. Basic Design Patterns We know our subsystem interface classes and (some of) their methods We need to create.
System Architecture Lecture 3 CSE 111 Spring /22/20151Copyright William E. Howden.
1 Lecture 2: Elaboration Tasks and Domain Modeling.
OOP #10: Correctness Fritz Henglein. Wrap-up: Types A type is a collection of objects with common behavior (operations and properties). (Abstract) types.
Copyright W. Howden1 Lecture 15: Generalization, Polymorphism and States.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535.
1 Lecture 1: Processes, Requirements, and Use Cases.
Copyright W. Howden1 Lecture 6: Collaboration Diagrams.
Chapter 1 Principles of Programming and Software Engineering.
Copyright W. Howden1 Lecture 2: Elaboration Tasks and Domain Modeling.
Copyright W. Howden1 Lecture 4: Elaboration Tasks and Domain Modeling.
Lecture a: Additional UML Models: Package, Activity, Deployment Lecture b: Generalization, Aggregation and Additional Domain Model Notation Copyright W.
Describing Syntax and Semantics
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
Copyright W. Howden1 State Models and the State Pattern.
Lecture 7: UML Class Diagrams CSE 111 7/15/20151Copyright W. Howden.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
CS 261 – Data Structures Preconditions, Postconditions & Assert.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
CS 363 Comparative Programming Languages Semantics.
Program Correctness. 2 Program Verification An object is a finite state machine: –Its attribute values are its state. –Its methods optionally: Transition.
Chapter 5: Sequences, Mathematical Induction, and Recursion 5.5 Application: Correctness of Algorithms 1 [P]rogramming reliability – must be an activity.
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen.
COMP 6471 Software Design Methodologies Winter 2006 Dr Greg Butler
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, Mitchell, R., and McKim, Design by Contract,
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.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
Cs7100(Prasad)L18-9WP1 Axiomatic Semantics Predicate Transformers.
C HAPTER 3 Describing Syntax and Semantics. D YNAMIC S EMANTICS Describing syntax is relatively simple There is no single widely acceptable notation or.
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.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Principles of Programming & Software Engineering
Lecture 4: Elaboration Tasks and Domain Modeling
Abstract Data Type.
Principles of Programming and Software Engineering
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
Specifying Object Interfaces
Mathematical Structures for Computer Science Chapter 1
Programming Languages and Compilers (CS 421)
Lecture 4: Sequence Interaction Diagrams
Design by contract Object-Oriented Software Construction by Bertrand Meyer, Prentice Hall The presence of a precondition or postcondition in a routine.
Semantics In Text: Chapter 3.
Design by contract Object-Oriented Software Construction by Bertrand Meyer, Prentice Hall The presence of a precondition or postcondition in a routine.
Predicate Transformers
CSE 1020:Software Development
Presentation transcript:

Copyright W. Howden1 Lecture 13: Programming by Contract

Copyright W. Howden2 PreConditions and PostConditions PreCondition –what you assume to be true before an operation or process PostCondition –what you assert to be true after an operation or process Contract: If Preconditions hold before, then Postconditions will hold afterwards

Copyright W. Howden3 PreConditions and Data Validation Precondition client’s responsibility Input validation is not required Documented so it does not slip through the cracks Clarifies when and where data needs to be checked Redundant checks lead to –decreased performance –less clarity –increased opportunity for errors

Copyright W. Howden4 Kinds of Postconditions Low level –Conditions and relationships on variable values –Return value properties Higher level –Objects created or deleted –Associations formed or deleted

Copyright W. Howden5 Applications Low level –Algorithms –Classes class and method properties Higher level –Use case assumptions and results –System and subsystem operations identified during requirements analysis and elaboration

Copyright W. Howden6 Algorithms and Pre/PostConditions Formal specifications Proofs of correctness of algorithms –verification, formal verification

Copyright W. Howden7 Assertions Assertion: Condition that should be true at an associated location in the algorithm PreCondition= input assertion PostCondition = output assertion Intermediate assertion – intermediate state of system/program Loop invariant = assertion about state at some location in a loop

Copyright W. Howden8 Algorithms and Verification Suppose that A 1 is the precondition for an algorithm A and A n is the postcondition. Correctness: A is correct if it is partially correct and it always terminates Partial correctness of A –if A 1 is true at the beginning of A and if A is executed and terminates, then A n will be true at the end of the execution

Copyright W. Howden9 Proof Technique Add intermediate assertions A i to the algorithm. Make sure that each loop has at least one intermediate assertion For each pair of assertions (X,Y) where there is a subpath p from X to Y, which has no other assertions on it, prove that if Control is at the location of X and X is true, and if control flows along p to Y, then Y will be true

Copyright W. Howden10 Proof Method Validity Assume that we prove validity for each assertion pair (X,Y) joined by a subpath p For any execution of the algorithm, a path P will be followed from the precondition to the postcondition The path P can be broken up into subpaths p between assertion pairs (X,Y) Proofs for the subpaths p implies correctness for whole path P

Copyright W. Howden11 Sample Algorithm Multiplies two numbers x and y by adding up y x times Input (precondition) assertion Pre, output (postcondition) assertion Post, intermediate loop invariant assertion IA

Copyright W. Howden12

Copyright W. Howden13 Verification Conditions for Sample Algorithm Prove that –if Pre is true then IA will be true –if IA is true and Count = 0 then Post will be true –If IA is true and Count  0 then IA will be true

Copyright W. Howden14 Termination Proof? Termination proof –Count initialized to x >=0 –Loop terminates if Count == 0 –For each iteration Count is decremented by 1 so loop must terminate Note: if Precondition x>=0 is removed and input is negative, algorithm will not always terminate but is still partially correct

Copyright W. Howden15 Classes and Pre/Post Conditions Method Pre and Post conditions –algorithm input/output conditions –state changes for object Class invariants –true after constructor, and true before and after each method application –could also document as pre and post conditions for each method to ensure compliance

Copyright W. Howden16 Sample Class Invariants e.g. LogOn object created by DomainLogic when someone logs on to DS Has a class variable called name which is never null-valued after LogOn is constructed. i.e. it is initialized in the constructor class invariant: {this.logOn  null}

Copyright W. Howden17 Sample Class Stack Class (parameterized) Written in a language with class invariant statement precondition and postcondition statements for methods

Copyright W. Howden18

Copyright W. Howden19 Use Cases and Pre/Post Conditions Use case is a story that may contain a number of system operations Use case pre/postconditions –for whole story

Copyright W. Howden20 DS Example - Precondition Use Case: user changing his/her data in the DS data base Assume that the use case does not include the log on subtask, which will always be performed before this use case and which will have confirmed that the user was a member before this use case began PreCondition: User is a member of the Dating System and has a member record in the Data Base

Copyright W. Howden21 DS Example – Postcondition Use Case: user changing his/her data in the DS data base Suppose that m is the current user record for U in the data base, and c specifies changes to the data. Let m’ = change(m,c), the result of making changes c to m. Postcondition: User record for U == m’

Copyright W. Howden22 Systems/subsystems and Pre/Postconditions Operations determined during requirements analysis and elaboration Systems and subsystem events correspond to actor actions or messages received Events are responded to by system/subsystem operators Pre and post conditions describe required operator functionality

Copyright W. Howden23 Operator Pre and Postconditions High level, more informal than algorithm and method conditions More object oriented e.g. –Objects created and deleted –Associations between instances of classes created and deleted –Attributes of objects altered

Copyright W. Howden24 DS Examples – 1 Subsystem: GUI Operation: Create(DomainLogic Dl) Preconditions: Domain Logic object is created Postconditions: GUI object is created

Copyright W. Howden25 DS Examples - 2 Subsystem: GUI Operation: name entered during Logon Precondition: Logon is visible Postconditions: –LogOn is not visible –Appropriate user options dialog (member, admin, unauthorized) created and visible

Copyright W. Howden26 DS Examples – 3 Subsystem: GUI Operation: EnterMemberData option is chosen from member options dialog Preconditions: member options dialog is visible Postconditions: EnterMemberData Dialog is visible

Copyright W. Howden27 DS Examples – 4 Subsystem: DL Operation: logOn Preconditions: DataBase is associated with DL Postconditions: –LogOn object created and associated with DL –LogOn userName and userType attributes are set

Copyright W. Howden28 DS Examples – 5 SubSystem: DL Operation: addMember(String name) Preconditions: DataBase is associated with DL Postcondition: (none for DL, i.e. no changes for DL) /* Recall: postcondition will only refer to object/subsystem under consideration */

Copyright W. Howden29 DS Examples – 6 Subsystem: DL Operation: deleteMember(String name) PreCondition: DataBase is associated with DL PostCondition: /* in the design and implementation a value is returned by the method for this operation which would be in a more detailed postcondition */

Copyright W. Howden30 DS Examples – 7 Subsystem: DataBase Operation: Create (File file) Precondition: file exists and contains member records in expected format PostCondition: the MemberData[] object associated with DataBase will be filled with the member records from the file

Copyright W. Howden31 DS Examples – 8 Subsystem: DataBase Operation: getMemberData(String name) Precondition: Postcondition /* no changes to DataBase object. Obviously there is a return value from the operation */

Copyright W. Howden32 DS Examples – 9 Subsystem: Data Base Operation: addMember(String name) Precondition: DataBase has a MemberData[] collection object associated with it Postcondition: the MemberData[] object is altered to include a new MemberData instance with this name