Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 9: Contracts and Inheritance (based on work with.

Slides:



Advertisements
Similar presentations
SCOOP: Simple Concurrent Object-Oriented Programming Extend the pure, strongly typed, object-oriented language Eiffel with a general and powerful concurrency.
Advertisements

© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 10: Advanced Object-Oriented Mechanisms (based on.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Exercise Session 1: Eiffel Introduction.
2 nd Microsoft Rotor Workshop, Pisa, April 23-25, SCOOPLI for.NET: a library for concurrent object-oriented programming Volkan Arslan, Piotr Nienaltowski.
SCOOP on.NET Volkan Arslan Chair of Software Engineering ETH Zurich, Switzerland
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 7: SCOOP Type System (based on work with Piotr Nienaltowski)
Chair of Software Engineering PPoPP 2003, , San Diego SCOOP it up! Piotr Nienaltowski Chair of Software Engineering, ETH Zurich, Switzerland.
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.
Inheritance. class RECTANGLE inherit POLYGON RECTANGLE includes features of POLYGON r: RECTANGLE; p: POLYGON Polymorphism p := r -- legal assignment Dynamic.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC Lecture 20 - Concurrency Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Piotr Nienaltowski, SCOOP Simple Concurrent Object-Oriented Programming.
Chair of Software Engineering SCOOP: an introduction Bertrand Meyer Chair of Software Engineering, ETH Zurich, Switzerland
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.
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 Piotr Nienaltowski, , Concurrent Object-Oriented Programming Bertrand Meyer, Piotr Nienaltowski.
Chair of Software Engineering Object-Oriented Software Construction Bertrand Meyer Lesson 21 Last update: 25 May 2004 Type issues, covariance and catcalls.
Chair of Software Engineering Piotr Nienaltowski, , Concurrent Object-Oriented Programming Bertrand Meyer, Piotr Nienaltowski.
Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 10: Project Presentation Ilinca.
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.
Principles of Object-Oriented Software Development The language Eiffel.
Chair of Software Engineering SCOOP for ROTOR Bertrand Meyer Capstone (ROTOR final workshop), 2005 © Bertrand Meyer, 2005.
Chair of Software Engineering ATOT - Lecture 26, 30 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 8, 28 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 6 7 October 2008.
SCOOP: Simple Concurrent Object-Oriented Programming Piotr Nienaltowski, Volkan Arslan, Bertrand Meyer presented by: Mark Schall.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Lecture 21 1 Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 25, 30 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Avoid a void Bertrand Meyer ©Bertrand Meyer, 2008.
Chair of Software Engineering 1 Introduction to Programming Exercise Session Week 9 M. Piccioni 17/18 November 2008.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 6: Genericity.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Chair of Software Engineering OOSC - Summer Semester Bertrand Meyer Object-Oriented Software Construction Lecture 7: Inheritance.
Describing Syntax and Semantics
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 6:Computational Model (based on work with Piotr Nienaltowski)
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 2: an overview of SCOOP.
Chair of Software EngineeringPiotr Nienaltowski, 2 nd SCOOP workshop, SCOOP update and proposed extensions Piotr Nienaltowski.
Chair of Software Engineering ATOT - Lecture 7, 23 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software EngineeringOOSC – Summer Semester 2005 Object-Oriented Software Construction Bertrand Meyer Lecture 19: SCOOP Simple Concurrent Object-Oriented.
Chair of Software Engineering ATOT - Lecture 22, 18 June Advanced Topics in Object Technology Bertrand Meyer.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
ABSTRACT The real world is concurrent. Several things may happen at the same time. Computer systems must increasingly contend with concurrent applications.
Contracts for Concurrency - Contracts & Inheritance Aryabrata Basu University of Georgia.
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.
Ceg860 (Prasad)L17IT1 Inheritance Techniques Subcontracting Anchored Types.
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.
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.
© 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.
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
SCOOPLI for .NET: a library for concurrent object-oriented programming
Simple Concurrent Object-Oriented Programming
Design by contract Object-Oriented Software Construction by Bertrand Meyer, Prentice Hall The presence of a precondition or postcondition in a routine.
Design by contract Object-Oriented Software Construction by Bertrand Meyer, Prentice Hall The presence of a precondition or postcondition in a routine.
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Presentation transcript:

Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 9: Contracts and Inheritance (based on work with Piotr Nienaltowski)

2 Preconditions In sequential context: precondition is correctness condition In concurrent context: feature call and feature application do not usually coincide A supplier cannot assume that a property satisfied at the call time still holds at the execution time.

3 Preconditions store (buffer: separate BUFFER [INTEGER]; i: INTEGER) -- Store i in buffer. require not buffer is_full i > 0 do buffer put (i) end my_buffer: separate BUFFER [INTEGER] ns_buffer: BUFFER [INTEGER] … store (my_buffer, 24) store (ns_buffer, 24) my_buffer := ns_buffer store (my_buffer, 79)

4 Preconditions A precondition expresses the necessary requirements for a correct feature application. Precondition viewed as synchronization mechanism: A called feature cannot be executed unless the preconditions holds A violated precondition delays the feature’s execution The guarantee given to the supplier is exactly the same as with the traditional semantics.

5 Postconditions A postcondition describes the result of a feature’s application. Postconditions are evaluated asynchronously; wait by necessity does not apply. Postcondition clauses that do not involve calls on objects handled by the same processors are evaluated independently.

6 Postconditions spawn_two (l1, l2: separate LOCATION) do l1 do_job l2 do_job ensure post_1: l1 is_ready post_2: l2 is_ready end tokyo, zurich: separate LOCATION r (l: separate LOCATION) do spawn_two (l, tokyo) do_local_stuff get_result (l) do_local_stuff get_result (tokyo) end … r (zurich)

7 Invariants Standard Eiffel semantics applies to class invariants in SCOOP because all the calls appearing in invariants must be non-separate.

8 Loop Invariants, Loop Variants and Check Asynchronous semantics is applied to loop variants, loop invariants, and check instruction.

9 Loop Invariants and Loop Variants remove_n (list: separate LIST [G]; n: INTEGER) -- Remove n elements. require list count >= n local initial, removed: INTEGER do from initial := list count removed := 0 until removed = n invariant list count + removed = initial variant list count - initial + n-- Same as n - removed loop list remove removed := removed + 1 end ensure list count = old list count + 1 end

10 Reasoning about Objects: Sequential Only n proofs if n exported routines! {INV  Pre r } body r {INV  Post r } __________________________ {Pre r } x. r (a) {Post r }

11 Reasoning about Objects: Concurrent Hoare-style sequential reasoning Controlled expressions (known statically as part of the type system) are both: Attached (statically known to be non-void) Handled by processor locked in current context {INV  Pre r } body r {INV  Post r } __________________________ {Pre r cont } x. r (a) {Post r cont }

12 Inheritance Can we use inheritance as in the sequential world? Is multiple inheritance allowed? Does SCOOP suffer from inheritance anomalies?

13 Example: Dining Philosophers class PHILOSOPHER inherit GENERAL_PHILOSOPHER PROCESS rename setup as getup undefine getup end feature step -- Perform a philosopher’s tasks. do think ; eat (left, right) end eat (l, r: separate FORK) -- Eat, having grabbed l and r. do… end end

14 Dining Philosophers deferred class PROCESS feature over: BOOLEAN -- Should execution terminate now? deferred end setup -- Prepare to execute process operations. deferred end step -- Execute basic process operations. deferred end wrapup -- Execute termination operations (default: nothing). do end live -- Perform process lifecycle. do from setup until over loop step end wrapup end

15 Dining Philosophers class GENERAL_PHILOSOPHER create make feature -- Initialization make (l, r: separate FORK) -- Define l as left and r -- as right forks. do left := l right := r end class FORK end feature {NONE} -- Implementation left: separate FORK right: separate FORK getup -- Take initialization actions. do end think -- Philosopher’s act. do end end

16 Inheritance Full support for inheritance (including multiple inheritance) Most inheritance anomalies eliminated thanks to the proper use of OO mechanisms What’s about Active objects?

17 Inheritance and Contracts Preconditions may be kept or weakened. Less waiting Postconditions may me kept or strengthened. More guarantees to the client Invariants may be kept or strengthened More consistency conditions See “Contracts for concurrency” paper on the website.

18 Inheritance: Result redeclaration class C feature r (x: X) do … end s (y: separate Y) do … end end class A feature x: X y: separate Y end class B inherit A redefine x, y end feature x: separate X y: Y end --Would lead to a traitor: c: C a: A create {B} a c. r (a x) -- This one is OK: c: C a: A create {B} a c s (a y) Covariant result redeclaration causes no problems. The client waits less.

19 Inheritance: formal argument redeclaration class A feature r (x: separate X) do … end s (x: X) do … end end class B inherit A redefine r, s end feature r (x: X) do … end s (x: separate X) do … end end -- x could be a traitor: a: A x: separate X create {B} a a r (x) -- OK Formal argument types may be redefined contravariantly w.r.t processor tag. The client may only use non-separate actual arguments in polymorphic calls, so no additional blocking will happen.

20 Inheritance: formal argument redeclaration class A feature r (x: detachable separate X) do … end s (x: separate X) do … end end class B inherit A redefine r, s end feature r (x: separate X) do … end s (x: detachable separate X) do … end end Additional locking for client: not acceptable Less locking for client: acceptable Formal argument types may be redefined contravariantly w.r.t detachable tags. The client waits less.

21 Impact on contracts Inherited precondition and postcondition clauses that involve calls on the redefined arguments may become invalid. class A feature s (x: separate X) require x some_precondition do … ensure x some_postcondition end class B inherit A redefine s end feature s (x: detachable separate X) do … end The call on x in the inherited pre- and postcondition is invalid.

22 Impact on Contracts The redeclaring feature cannot rely on the precondition clauses involving the formal argument. Inherited precondition clauses involving calls on detachable formal arguments hold vacuously (trivially true). Postconditions involving detachable formal arguments cannot be allowed.

23 Feature Redeclaration The redefinition rules for the individual components of a type are now clarified Class types may be redefined covariantly both in result types and argument types but the redefinition of a formal argument forces it to become detachable. For example, assuming that Y conforms to X, an argument x: X may be redefined into x: detachable Y but not x: Y. An attribute may be redefined from my_x: X into my_x: Y. Detachable tags may be redefined from ? to ! in result types. They may be changed from ! to ? in argument types, provided that no call on the redefined argument occurs in the original postcondition. Processor tags may be redefined from T to something more specific in result types, and from more specific to T in argument types.

24 Problems with Precursor Calls Use of Precursor is not always possible. class A feature r (x: separate X; y: separate Y) require some_precondition do x.f y.f end class B inherit A redefine r end feature r (x: separate X; y: detachable separate Y) require else new_precondition do if new_precondition then -- do something here else Precursor (x, y) end here some_precondition holds invalid

25 Solution for Precursor Calls Need object test (downcast): class B inherit A redefine r end feature r (x: separate X; y: detachable separate Y) require else new_precondition do if new_precondition then … elseif attached y as ly: separate Y then Precursor (x, ly) end Valid