Nature abhors a void Bertrand Meyer Software Engineering The basic - - PowerPoint PPT Presentation

nature abhors a void
SMART_READER_LITE
LIVE PREVIEW

Nature abhors a void Bertrand Meyer Software Engineering The basic - - PowerPoint PPT Presentation

Nature abhors a void Bertrand Meyer Software Engineering The basic O-O operation x . f ( args ) Semantics: apply the feature f , with given args if any, to the object to which x is attached and the basic issue studied here: How do we


slide-1
SLIDE 1

Software Engineering

Nature abhors a void

Bertrand Meyer

slide-2
SLIDE 2

2

Software Engineering

The basic O-O operation…

x.f (args)

… and the basic issue studied here:

How do we guarantee that x will always denote (be “attached” to) an object? (If not, call produces an exception and usually program termination.) Semantics: apply the feature f, with given args if any, to the

  • bject to which x is attached
slide-3
SLIDE 3

3

Software Engineering

Other problems solved by this work

Getting rid of “catcalls”, statically A consistent approach to type casts A better way to do “once per object” A consistent approach to object locking in concurrent programming

slide-4
SLIDE 4

4

Software Engineering

From “Eiffel: The Language” (1990)

There is one and only one kind of acceptable language extension: the one that dawns on you with the sudden self-evidence of morning mist. It must provide a complete solution to a real problem, but usually that is not enough: almost all good extensions solve several potential problems at once… It must fit perfectly within the spirit and letter of the rest of the language. It must not have any dark sides or raise any unanswerable questions.

slide-5
SLIDE 5

5

Software Engineering

The context

ECMA standard for Eiffel Approved 21 June 2005 2nd edition, June 2006 Will be ISO standard by end of 2006

slide-6
SLIDE 6

6

Software Engineering

References

Useful to describe linked data structures Can be void!

“Almaviva” name landlord loved_one O1 “Figaro” O2 “Susanna” O3

Zurich Pisa

Piombino

Biodola

item right item right item right item right

slide-7
SLIDE 7

7

Software Engineering

A success story: static type checking

We allow

x.f (args)

  • nly 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?

slide-8
SLIDE 8

8

Software Engineering

Can we extend static type checking?

Our goal (“void safety”): at compile time, allow

x.f (args)

  • nly if we can guarantee that at run time:

x is not void.

slide-9
SLIDE 9

9

Software Engineering

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
slide-10
SLIDE 10

10

Software Engineering

A slightly different approach

“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

slide-11
SLIDE 11

11

Software Engineering

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)

slide-12
SLIDE 12

12

Software Engineering

Source: Patrick Chalin, forthcoming

44.4% of Eiffel preconditions clauses are of the form x /= Void

slide-13
SLIDE 13

13

Software Engineering

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
slide-14
SLIDE 14

14

Software Engineering

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

slide-15
SLIDE 15

15

Software Engineering

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.

  • ther_instructions

(args) (Assume exp is a variable)

slide-16
SLIDE 16

16

Software Engineering

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

slide-17
SLIDE 17

17

Software Engineering

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
slide-18
SLIDE 18

18

Software Engineering

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

slide-19
SLIDE 19

19

Software Engineering

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

item right item right item right first_element

slide-20
SLIDE 20

20

Software Engineering

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

  • bviously correct

Need to be approved by committee Mathematical, machine-checked proofs desirable

slide-21
SLIDE 21

21

Software Engineering

When no CAP applies: the Object Test

Not void-safe: if exp /= Void then … Various instructions … exp.operation … Other instructions … end

slide-22
SLIDE 22

22

Software Engineering

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

slide-23
SLIDE 23

23

Software Engineering

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

slide-24
SLIDE 24

24

Software Engineering

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

slide-25
SLIDE 25

25

Software Engineering

Scope of x

Another example of Object Test scope

from … until not {x: T } exp then … Various instructions, anything OK! …x.operation_of_T … Other instructions … end

slide-26
SLIDE 26

26

Software Engineering

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
slide-27
SLIDE 27

27

Software Engineering

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

slide-28
SLIDE 28

28

Software Engineering

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

slide-29
SLIDE 29

29

Software Engineering

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

slide-30
SLIDE 30

30

Software Engineering

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
slide-31
SLIDE 31

31

Software Engineering

The remaining problem…

How to ensure that variables declared of an attached type

x : T

meet that declaration, i.e. can never become void!

slide-32
SLIDE 32

32

Software Engineering

Rule 2: Conservation of attachment

In x := y

  • r

r (y)

  • - Where formal argument is x

if type of x is attached, y must be attached. (No “traitors”, see SCOOP.)

slide-33
SLIDE 33

33

Software Engineering

If assignment & argument passing are OK…

… there remains initialization!

slide-34
SLIDE 34

34

Software Engineering

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
slide-35
SLIDE 35

35

Software Engineering

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

slide-36
SLIDE 36

36

Software Engineering

Reminder: overall inheritance structure

ANY NONE A F B C D E

Inherits from

slide-37
SLIDE 37

37

Software Engineering

Reminder: creation in Eiffel (1)

Basic creation instruction create x.cp (…) where cp is one of the procedures of the class, marked as creation procedure

slide-38
SLIDE 38

38

Software Engineering

Reminder: creation in Eiffel (2)

The form without an explicit creation procedure create x

  • - With x of type T

is an abbreviation for create x.default_create and is valid if and only if T specifies its version of default_create as a creation procedure.

slide-39
SLIDE 39

39

Software Engineering

Reminder: overall inheritance structure

ANY NONE A F B C D E

Inherits from

default_create

slide-40
SLIDE 40

40

Software Engineering

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
slide-41
SLIDE 41

41

Software Engineering

Initializing variables

A type T is self-initializing if it specifies default_create as creation procedure Then initialization can be lazy:

  • If execution accesses a variable x of type T

uninitialized, x will initialize itself with default_create . Scheme 1: CAP Scheme 2: Self-initializing types

slide-42
SLIDE 42

42

Software Engineering

Initializing variables

What if an attribute is not of a self-initializing type, and is not set by creation procedures? Scheme 1: CAP Scheme 2: Self-initializing types Scheme 3: Self-initializing attributes

slide-43
SLIDE 43

43

Software Engineering

Digression: attributes (fields/data members)

bounding_rectangle: RECTANGLE

  • - Smallest rectangle including whole of current figure
slide-44
SLIDE 44

44

Software Engineering

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

slide-45
SLIDE 45

45

Software Engineering

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 …

slide-46
SLIDE 46

46

Software Engineering

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 end end

slide-47
SLIDE 47

47

Software Engineering

Initializing variables

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

slide-48
SLIDE 48

48

Software Engineering

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)

slide-49
SLIDE 49

49

Software Engineering

Genericity: the solution

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 either:

  • Self-initializing
  • Detachable!
slide-50
SLIDE 50

50

Software Engineering

Initializing variables

Scheme 1: CAP Scheme 2: Self-initializing types Scheme 3: Self-initializing attributes Scheme 4: Variables of formal generic type

slide-51
SLIDE 51

51

Software Engineering

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)

slide-52
SLIDE 52

52

Software Engineering

Other problems solved by attached types

Getting rid of “catcalls”, statically A better way to do “once per object” A consistent approach to object locking in concurrent programming (Through the Object Test) Consistent approach to type casts

slide-53
SLIDE 53

53

Software Engineering

The concurrency issue

f (x: separate A) do x.operation

  • - [1]

end Lock x? y := x -- [2] ?

slide-54
SLIDE 54

54

Software Engineering

From “Eiffel: The Language” (1990)

There is one and only one kind of acceptable language extension: the one that dawns on you with the sudden self-evidence of morning mist. It must provide a complete solution to a real problem, but usually that is not enough: almost all good extensions solve several potential problems at once… It must fit perfectly within the spirit and letter of the rest of the language. It must not have any dark sides or raise any unanswerable questions.

slide-55
SLIDE 55

55

Software Engineering

Eiffel today

Small but extremely successful user community Large mission-critical projects: Chicago Board of Trade, Boeing, Lockheed, AxaRosenberg, EMC Thriving developments: ECMA and others Extensive research work class proofs, testing, persistence, concurrency Powerful libraries (graphics, multimedia, networking…) Extensive use in teaching google “meyer touch class”

slide-56
SLIDE 56

56

Software Engineering

Kristen Nygaard

“In many teams a new idea is cherished, cuddled and shielded so it may grow. Grow up to become a weak attenuated result or finding, being alive only through the fierce protection of its parents. New ideas should be confronted by the executioner, with cruel attacks, with subtle attempts to prove them faulty or

  • useless. Ideas surviving such a fight are worth building upon”
slide-57
SLIDE 57

57

Software Engineering

Acknowledgments

Mechanism design: Mark Howard (AxaRosenberg) Emmanuel Stapf (Eiffel Software) Eric Bezault (AxaRosenberg) Karine Arnout (ETH) Alexander Kogtenkov (Eiffel Software) Concurrency applications: Piotr Nienaltowski (ETH) Special thanks to: Erik Meijer, Rustan Leino (Microsoft) Peter Mueller (ETH) In fond memory of: Kristen Nygaard For an inexhaustible supply of language design counter-examples: C++ Java For providing the best university environment on the planet: ETH Zurich

slide-58
SLIDE 58

58

Software Engineering

Acknowledgments

Mechanism design: Mark Howard (AxaRosenberg) Emmanuel Stapf (Eiffel Software) Eric Bezault (AxaRosenberg) Karine Arnout (ETH) Alexander Kogtenkov (Eiffel Software) Concurrency applications: Piotr Nienaltowski (ETH) Special thanks to: Erik Meijer, Rustan Leino (Microsoft) Peter Mueller (ETH) In fond memory of: Kristen Nygaard For an inexhaustible supply of language design counter-examples: C++ Java For providing the best university environment on the planet: ETH Zurich