Chair of Software Engineering Avoid a void Bertrand Meyer ©Bertrand Meyer, 2008.

Slides:



Advertisements
Similar presentations
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Advertisements

SCOOP: Simple Concurrent Object-Oriented Programming Extend the pure, strongly typed, object-oriented language Eiffel with a general and powerful concurrency.
Eiffel: Analysis, Design and Programming Bertrand Meyer (Nadia Polikarpova) Chair of Software Engineering.
Control Structures Ranga Rodrigo. Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end.
Semantics Static semantics Dynamic semantics attribute grammars
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 10: Advanced Object-Oriented Mechanisms (based on.
- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Exercise Session 1: Eiffel Introduction.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
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 The alias calculus Bertrand Meyer ITMO Software Engineering Seminar June 2011.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
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 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
Chair of Software Engineering Constants, once routines, and helper functions these slides contain advanced material and are optional.
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.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Piotr Nienaltowski, SCOOP Simple Concurrent Object-Oriented Programming.
Java Generics.
Chair of Software Engineering ATOT - Lecture 25, 30 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Avoid a void Bertrand Meyer With major contributions by Emmanuel Stapf & Alexander Kogtenkov (Eiffel Software)
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 17: Topological Sort Algorithm.
Chair of Software Engineering Piotr Nienaltowski, , Concurrent Object-Oriented Programming Bertrand Meyer, Piotr Nienaltowski.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 Advanced Material The following slides contain advanced material and are optional.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.
1 Advanced Material The following slides contain advanced material and are optional.
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.
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 Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 9: Contracts and Inheritance (based on work with.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 6: Genericity.
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.
Software Verification Bertrand Meyer Chair of Software Engineering Lecture 2: Axiomatic semantics.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Describing Syntax and Semantics
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 7.
Chair of Software Engineering ATOT - Lecture 7, 23 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering ATOT - Lecture 22, 18 June Advanced Topics in Object Technology Bertrand Meyer.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
10 Conversion. Let’s start with conformance Conformance determines when a type may be used in lieu of another. Conformance relies on inheritance. The.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
Contracts for Concurrency - Contracts & Inheritance Aryabrata Basu University of Georgia.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
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.
Chair of Software Engineering Void safety these slides contain advanced material and are optional.
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
Presentation transcript:

Chair of Software Engineering Avoid a void Bertrand Meyer ©Bertrand Meyer, 2008

2 Basic O-O operation… x. f (args) … and basic issue studied here: (If not, call produces an exception and usually termination) Semantics: apply the feature f, with given args if any, to the object to which x is attached How do we guarantee that x will always be “attached” to an object?

3 Other problems solved by this work A consistent approach to type casts A better way to do “once per object” A consistent approach to object locking in concurrent programming

4 The context ECMA standard for Eiffel Approved June 2005, 2 nd edition, June 2006 ISO standard, November 2006 Mechanism described here is in EiffelStudio 6.1, Nov 2007

5 “If and only if” language specification

6 References Useful to describe linked data structures Can be void! “Almaviva”name landlord loved_one O1 “Figaro” O2 “Susanna” O3 Zurich Pisa Piombino Biodola itemright itemright itemrightitemright

7 References are a conceptual modeling tool Difference between  Each car has an engine  Each car has a manufacturer (e.g. Fiat, Renault)

8 A success story: static type checking We allow x. f (args) only if we can guarantee that at run time: The object attached to x, if it exists, has a feature for f, able to handle the args. Basic ideas:  Accept it only if type of x has a feature f  Assignment x := y requires conformance (based on inheritance) What if x is void?

9 Can we extend static type checking? Our goal (“void safety”): at compile time, allow x. f (args) only if we can guarantee that at run time: x is not void.

10 Requirements on an acceptable solution  Statically, completely void safe: no exceptions  Handles genericity  Simple for programmer, no mysterious rules  Reasonably simple for compiler  Compatibility or minimum change for existing code (Plus for me: 1 st semester teachability)

11 Key influence “Spec# stipulates the inference of non- [voidness] for local variables. This inference is performed as a dataflow analysis by the Spec# compiler.” (Barnett, Leino, Schulte, Spec# paper) x /= Void

12 Source: Patrice Chalin 44.4% of Eiffel preconditions clauses are of the form x /= Void

13 Components of the solution 1. Some patterns guaranteed void-safe (“Certified Attachment Patterns” or CAPS) 2. Void value permitted only for types declared as “detachable”. By default types are “attached” 3. Initialization rules ensure that any variable of an attached type has a non-void initialization value 4. Rule for generic parameters

14 Rule 1: Target Validity Rule x. f (args) is permitted only if x is attached “Attached” is a static property. x is attached if either:  Its type is attached  Its type is not attached, but x itself is guaranteed attached in a specific context

15 An interesting pattern Consider a variable or expression exp Can this be guaranteed void-safe? if exp /= Void then exp. operation end Answer: only if exp is a local variable! (or formal argument) Not for an attribute, or a general expression. other_instructions (args) (Assume exp is a variable) not assigning to exp

16 Attached entity: Case #1 x is attached if used as part of a Certified Attachment Pattern (CAP). This is a CAP for x Example CAP for a local variable or formal argument x: if x /= Void then … Any instructions not assigning to x … … (except possibly last instruction) … end

17 Rule 1: Target Validity Rule x. f (args) is permitted only if x is attached Ways to ensure that x is attached: 1.Use it in a Certified Attachment Pattern

18 A loop CAP from... until x = Void loop... Any instructions not assigning to x... … (except possibly last instruction) … end x must again be a local variable or a formal argument! A CAP for x

19 A typical loop, now safe from x := first_element until x = Void or else Result loop Result := (x. item = sought) x := x. right end Zurich Piombino Biodola itemrightitemrightitemright first_element

20 The CAP catalog About 6 CAPs specified in the ECMA standard. Above two are the most important. Another example: x in x /= Void implies x. some_property Criterion: simple; easy to understand; provably and obviously correct Need to be approved by committee Mathematical, machine-checked proofs desirable

21 When no CAP applies: the Object Test Not void-safe: if exp /= Void then … Various instructions … exp. operation … Other instructions … end

22 When no CAP applies: the Object Test Previous scheme made void-safe! if {x : T } exp then-- T is the type of exp … Various instructions, anything OK! … x. operation … Other instructions … end Scope of x Karine Arnout Éric Bezault Bezault

23 The Object Test {x: T } exp exp is an arbitrary expression x is a fresh variable, read-only (like formal argument) It’s a boolean expression: true if value of exp is attached to object of type T or conforming Binds x to that value over scope of expression

24 Object Test example if {x : T } exp then-- T is the type of exp … Various instructions, anything OK! … x. operation … Other instructions … end Scope of x

25 Scope of x Another example of Object Test scope from … until not {x: T } exp loop … Various instructions, anything OK! …x. operation_of_T … Other instructions … end

26 Rule 1: Target Validity Rule x. f (args) is permitted only if x is attached Ways to ensure that x is attached: 1.Use it in a Certified Attachment Pattern 2.Use it in the scope of an Object Test

27 Another example of Object Test if {e: EMPLOYEE } database. retrieved then … Various instructions, anything OK! … e. print_paycheck … Other instructions … end Replaces “assignment attempt” of current Eiffel, and various “type narrowing”, “Run-Time Type Identification”, “downcasting” mechanisms

28 The remaining goal… Minimize use of Object Test! General but impractical solution: protect every qualified feature call by an Object Test! Instead of exp. operation write if {x : T } exp then x. operation end

29 Attached and detachable types For any type T, a variable declared as just x : T cannot be void! Type T is attached To allow void values, declare x : ?T Type ?T is detachable

30 Rule 1: Target Validity Rule x. f (args) is permitted only if x is attached Ways to ensure that x is attached: 1.Use it in a Certified Attachment Pattern 2.Use it in the scope of an Object Test 3.Give it an attached type

31 The remaining problem… How to ensure that variables declared of an attached type x : T meet that declaration, i.e. can never become void!

32 Rule 2: Conservation of attachment In x := y or r (y) -- Where formal argument is x if type of x is attached, y must be attached. (No “traitors”, see SCOOP.)

33 If assignment & argument passing are OK… … there remains initialization!

34 The initialization problem In previous Eiffel:  Basic types initialized to standard values (zero, False, null character)  Other expanded types must have default_create from ANY  References initialized to Void

35 Initializing variables  An attribute is attached if every creation procedure sets it.  A local variable is initialized if the beginning of the routine sets it. Scheme 1: CAP

36 The initialization problem In previous Eiffel:  Basic types initialized to standard values (zero, False, null character)  Other expanded types must have default_create from ANY  References initialized to Void

37 Initializing variables Scheme 1: CAP Scheme 2: Properly set variables

38 Properly set variables At any place where it is accessed, a variable must be properly set

39 Properly set variables ep is not in a creation- reachable routine, and x is

40 Initializing variables What if an attribute is not set by creation procedures? Scheme 1: CAP Scheme 2: Properly set variables Scheme 3: Self-initializing attributes

41 Digression: attributes (fields/data members) bounding_rectangle: RECTANGLE -- Smallest rectangle including whole of current figure

42 Digression: attributes with contracts! bounding_rectangle: RECTANGLE -- Smallest rectangle including whole of current figure require bounded attribute ensure Result. height = height Result. width = width Result. lower_left = lower_left Result. contains (Current) end

43 Self-initializing attributes bounding_rectangle: FIGURE -- Smallest rectangle including whole of current figure -- (Computed only if needed) require bounded attribute create Result.set (lower_left, width, height) ensure Result. height = height Result. width = width Result. lower_left = lower_left Result. contains (Current) end … As before …

44 Another example: once per object class STOCKS feature db: DATABASE history (ts: TICKER_SYMBOL): HISTORY -- List of previous valuations of ts attribute if {h: HISTORY} db.retrieved (ts) then Result := h else create Result -- Produces empty list end

45 Initializing variables In class C [G ], what about the initialization of x : G ? Scheme 1: CAP Scheme 2: Properly set variables Scheme 3: Self-initializing attributes Scheme 4: Variables of formal generic type Example generic derivations: C [INTEGER ] C [EMPLOYEE ]

46 Genericity: the issue class ARRAY [G] feature item alias “[]” (i : INTEGER): G put (x : G ; i : INTEGER) … end a: ARRAY [INTEGER] x := a [i ] -- Means a.item (i) a [ j ] := y -- Means a.put (y, j ) a: ARRAY [SOME_TYPE] x := a.item (i) a.put (y, j ) i j ? How can class ARRAY initialize the entries properly without knowing what G is? a (y)(y)

47 Genericity In a class C [G], if the implementation accesses an attribute x : G that is not guaranteeably attached (through a CAP, or self- initialization), then the class must be declared as C [? G] -- For example: ARRAY [? G] Then any actual parameter (T in C [T]) must be detachable

48 Initializing variables Scheme 1: CAP Scheme 2: Properly set variables Scheme 3: Self-initializing attributes Scheme 4: Variables of formal generic type

49 Requirements on an acceptable solution  Statically, completely void safe: no exceptions  Handles genericity  Simple for programmer, no mysterious rules  Reasonably simple for compiler  Compatibility or minimum change for existing code (+ for me: the “Week 6 of Einführung in die Programmierung” criterion)