- 10.3 - covariance & anchored types

23
1 - 10.3 - Covariance & anchored types

Upload: javier

Post on 23-Feb-2016

51 views

Category:

Documents


0 download

DESCRIPTION

- 10.3 - Covariance & anchored types. Covariance?. Within the type system of a programming language, a typing rule or a type conversion operator is*: covariant if it preserves the ordering, ≤, of types, which orders types from more specific to more generic: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: - 10.3 - Covariance & anchored types

1

- 10.3 -

Covariance&

anchored types

Page 2: - 10.3 - Covariance & anchored types

2

Covariance?Within the type system of a programming language, a typing rule or a type conversion operator is*:

• covariant if it preserves the ordering, ≤, of types, which orders types from more specific to more generic:

animal := dog (animal: ANIMAL, dog: DOG)list := string_list (list: LIST [ ANY ], string_list: LIST [ STRING ])

• contravariant if it reverses this ordering, which orders types from more generic to more specific

dog := animal string_list := list• novariant if neither of these apply.

* http://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)

Page 3: - 10.3 - Covariance & anchored types

3

The need for covariance

CUSTOMER

MINOR

BEVERAGE

SOFT_DRINK ALCOHOL

drink : BEVERAGEserve (b : BEVERAGE ) dodrink := b

ensure drink = b

end

drink : SOFT_DRINKserve (b : SOFT_DRINK ) do drink := b end

Client

Inherit

Page 4: - 10.3 - Covariance & anchored types

4

Defeating covariance (1)

CUSTOMER

MINOR

BEVERAGE

SOFT_DRINK ALCOHOL

Pimms : ALCOHOLc : CUSTOMERShiloh : MINOR

c.serve ( Pimms)

c := Shiloh

c.serve (b )

b := Pimms

b : BEVERAGE

drink : BEVERAGEserve (b : BEVERAGE ) do drink := b end

drink : SOFT_DRINKserve (b : SOFT_DRINK ) do drink := b end

c.serve ( Pimms)

c := Shiloh

Page 5: - 10.3 - Covariance & anchored types

5

Defeating covariance (2)

CUSTOMER

MINOR

BEVERAGE

SOFT_DRINK ALCOHOL

drink : BEVERAGEserve (b : BEVERAGE ) do drink := b end

drink : SOFT_DRINKserve (b : SOFT_DRINK ) do drink := b end

bus.item.serve ( Pimms)

bus : LIST [CUSTOMER]school_bus : LIST [MINOR]

Client

Inherit

Pimms : ALCOHOLc : CUSTOMERShiloh : MINOR

bus := school_bus

Generic conformance

Page 6: - 10.3 - Covariance & anchored types

6

Terminology

Catcall: incorrect application of a feature to an object as a result of

Incorrect argument type Information hiding violation

(CAT: Changed Availabilityor Type)

BIRD

OSTRICH

fly

??

Page 7: - 10.3 - Covariance & anchored types

7

Status

Problem known since late 80s“Covariance” term coined by Luca CardelliCriticism of initial Eiffel design by W. Cook et al., 1989*Numerous proposals since then

*A Proposal for Making Eiffel Type-Safe, ECOOP 1989

Page 8: - 10.3 - Covariance & anchored types

8

Mitigating mechanism 1: non-conforming inheritance

classC

inherit {NONE }B

feature...

end

No polymorphism permitted:b1 := c1 -- Invalid

B

C

b1 : Bc1 : C

Page 9: - 10.3 - Covariance & anchored types

9

class CUSTOMER featuredrink : BEVERAGEserve (b : like drink )

do drink := b

endend

Mitigating mechanism 2: anchored typesclass CUSTOMER feature

drink : BEVERAGEserve (b : BEVERAGE )

do drink := b

endend

class MINOR inheritCUSTOMER

redefine drink, serve endfeature

drink : SOFT_DRINK

serve (b : SOFT_DRINK )do

drink := bend

end

class MINOR inheritCUSTOMER

redefine drink endfeature

drink : SOFT_DRINKend

In practice: anchored types

Page 10: - 10.3 - Covariance & anchored types

10

Anchoring to Current

Also possible, in a class C :

x : like Current

In any descendant D of C (including C itself), x has type D

Page 11: - 10.3 - Covariance & anchored types

11

Mitigating mechanism 3: flat type checking

An assignmentx := y

may be valid in a routine r of a class B but not necessarily in a descendant C which only redefines x (covariantly)

The Eiffel type system now specifies that every class must be independently valid

“Flat type checking”

B

C

r do x := y end

x : Ty : T

x : U

T

U

Page 12: - 10.3 - Covariance & anchored types

12

Unrealistic approach: contravariance

Results specialized, arguments generalizedSolves the problemHard to reconcile with practice. The world does seem to be covariant.

CUSTOMER

MINOR

BEVERAGE

SOFT_DRINK ALCOHOL

drink : BEVERAGEserve (b : BEVERAGE ) do drink := b end

drink : ????serve (b :????) do drink := b end

Page 13: - 10.3 - Covariance & anchored types

13

Novariance (argument passing)C++, Java, .NET languages

Eliminates the problem (obviously)

Forces programmers to do the adaptations themselves

May result in brittle/unnecessary code

Page 14: - 10.3 - Covariance & anchored types

14

Previous solutions: genericity*

class CUSTOMER [DRINK_TYPE]

*Franz Weber: Getting Class Correctness and System Correctness Equivalent (How to get covariance right), TOOLS EUROPE 1992

Page 15: - 10.3 - Covariance & anchored types

15

Previous solutions: system-level validity*

Considering all assignments, compute dynamic type set (DTS) of any variable x. If there is an assignment x := y, or argument passing, all elements of DTS of y are also in the DTS of x .Advantages:

No attempt at control flow analysis Fixpoint algorithm Helps with optimization

Disadvantages: Pessimistic Not incremental Difficulty of giving precise diagnostics

*B. Meyer: Eiffel: The Language, Prentice Hall, 1991

Page 16: - 10.3 - Covariance & anchored types

16

Type intervals

CUSTOMER

MINOR

drink : BEVERAGEserve (b : BEVERAGE ) do drink := b end

drink : SOFT_DRINKserve (b : SOFT_DRINK ) do drink := b end

Pimms : ALCOHOL

c.serve ( Pimms)

c := Shiloh

c : CUSTOMER..MINOR

Shiloh : MINOR

-- Now invalid

Page 17: - 10.3 - Covariance & anchored types

17

Type intervals

c.serve ( Pimms)

c := Shiloh

c : CUSTOMER..MINOR

-- Now invalid

Rule: a call x.f (a), with x : T..U, must be valid when x is given any type in T..U

Abbreviations:x : T means x : T..NONELIST [T ] means LIST [T..T ]

x : CUSTOMER..CUSTOMER

x.serve ( Pimms)

x := Shiloh-- Now invalid

Page 18: - 10.3 - Covariance & anchored types

18

The retained solution: a simplified version

c.serve ( Pimms)

c := Shiloh

c : CUSTOMERx: frozen CUSTOMER

OK

Rule: a call x.f (a), with x: T not frozen, must be valid when x is given any descendant type of T

x.serve ( Pimms)

x := Shiloh Invalid

OKInvalid

Page 19: - 10.3 - Covariance & anchored types

19

Genericity rule

vbus.extend (Shiloh)

vbus := school_bus

bus : LIST [CUSTOMER ]vbus : LIST [variant CUSTOMER ]school_bus : LIST [MINOR]

OK

Rule:An assignment with a different actual generic parameter requires the “variant” mark.The variant mark precludes the use of a routine with an argument of a formal generic parameter type

Invalid

OKInvalid

bus.extend (Shiloh)

bus := school_bus

Page 20: - 10.3 - Covariance & anchored types

20

By the way!

vbus.extend (Shiloh)

vbus := school_bus

bus : LIST [CUSTOMER ]vbus : LIST [variant CUSTOMER ]school_bus : LIST [MINOR]

OK Invalid

Invalid

bus := school_busOKbus.extend (Shiloh)

Invalid since, in LIST [G],item : G

is not frozen

bus.item.serve (Pimms) ???

Page 21: - 10.3 - Covariance & anchored types

21

Anchored (“like”) declarations

New results:

“Flat type checking” guarantees that like Current declarations are safe

b : like a , with a of type T, may be considered an abbreviation not for b : T as now, but for

Then only explicit (non-anchored) covariance remains!

b : like a

b : frozen T

Page 22: - 10.3 - Covariance & anchored types

22

StatusImplemented and currently being experimented

Criterion: must not break code unless there is a real catcall risk

Need for formal proof

Proposal for a more flexible mechanism (“forget”)

Page 23: - 10.3 - Covariance & anchored types

23

Covariance: summaryReconcile modeling power with safety