Difference between revisions of "Transposition"
m (→Covariance and the missing part of the ECMA standard) |
m (→Covariance and the missing part of the ECMA standard) |
||
Line 27: | Line 27: | ||
Eiffel allows covariant redefinitions. We reuse the previous example system and add two new classe X and Y: | Eiffel allows covariant redefinitions. We reuse the previous example system and add two new classe X and Y: | ||
− | [[Image:Example.jpg|450px]] | + | {|border="0" cellpadding="2" cellspacing="0" align="center" |
+ | |-valign="top" -halign="center" | ||
+ | |[[Image:Example.jpg|450px]] | ||
+ | |} | ||
Class Y covariantly redefines feature a to the more specific type B. We are interested in the semantics of feature g when executed on an object of class Y. For this we need to know wether the static type of field a is A or B. Of course the static type from the perspective of feature g is still A, f is not even a valid feature name on a target of type B. But lets now have a look at the flat-short representation of Y(Features of ANY omitted): | Class Y covariantly redefines feature a to the more specific type B. We are interested in the semantics of feature g when executed on an object of class Y. For this we need to know wether the static type of field a is A or B. Of course the static type from the perspective of feature g is still A, f is not even a valid feature name on a target of type B. But lets now have a look at the flat-short representation of Y(Features of ANY omitted): | ||
+ | |||
+ | {|border="0" cellpadding="2" cellspacing="0" align="center" | ||
+ | |-valign="top" -halign="center" | ||
+ | | | ||
<code>[Eiffel,n] | <code>[Eiffel,n] | ||
class Y | class Y | ||
feature | feature | ||
a: B | a: B | ||
− | g | + | g is |
do | do | ||
a.f1 | a.f1 | ||
Line 40: | Line 47: | ||
end | end | ||
</code> | </code> | ||
+ | |} | ||
These semantics have two consequences: | These semantics have two consequences: | ||
* The standard flat-short view of the class Y becomes missleading | * The standard flat-short view of the class Y becomes missleading |
Revision as of 08:45, 25 October 2006
Contents
The new dynamic binding semantics
With the ECMA Eiffel Standard, the dynamic binding semantics of the Eiffel language are almost clearly defined. This new or clarified semantics have some interesting consequences. The following system shows the difference between how the dynamic semantics were (and still are) implemented and how they are specified in the ECMA standard:
local a: A b: B do create {C}a create {C}b a.f --Line 3 b.f1 --Line 4 end |
The semantics for line 3 have always been clear, feature f2 is called. For line 4 the ECMA standard says, that feature f1 is called, whereas the current ISE compiler choses feature f2. So the ECMA standard restrains the power of select, they only have an impact if there are two ore more inheritance path from the static type to the dynamic type. This is indeed the case for line 3 but not for line 4 of the above example.
So the lession learned is:
- The exact static type of an entity has an important influence on the dynamic binding. A more specific static type may resolve a potential select conflict.
Covariance and the missing part of the ECMA standard
Eiffel allows covariant redefinitions. We reuse the previous example system and add two new classe X and Y:
Class Y covariantly redefines feature a to the more specific type B. We are interested in the semantics of feature g when executed on an object of class Y. For this we need to know wether the static type of field a is A or B. Of course the static type from the perspective of feature g is still A, f is not even a valid feature name on a target of type B. But lets now have a look at the flat-short representation of Y(Features of ANY omitted):
class Y feature a: B g is do a.f1 end end |
These semantics have two consequences:
- The standard flat-short view of the class Y becomes missleading
The interesting questions What happens now, when feature g is executed on an object of class Y For the current ISE compiler this redefinition has no semantic influence to the system (of course it may change the validity). For the ECMA standard the semantics are not d
Resolving of select conflicts
Transposition
We speak of the transposition of a feature, when we copy an inherited feature to a descendant class and adapt its content according to the inheritance path. When all the inherited features of a class are transposed, we get the flat short form of the class. Transposition is very interesting, since it seems to be the solution to some ambiguities in the language, namely repeated inheritance and replication. In the following system:
class B feature f do g end g do end end |
class D inherit B rename f as f1, g as g1 redefine f1 select f1, g1 end rename f as f2, g as g2 end feature f1 do ... end end |
class D has the transposed form (we omit the features from ANY):
So the transposed form of class D redefines all the features of its parent. Some rather complex rules of the standard become obsolete, when it is just stated, that every inherited feature needs to be transposed (8.16.2, 8.16.3, 8.16.4, 8.16.5). During the transposition there might be conflicts. It is possible that two transposed features have the same name. It remains to be specified how such cases are handled. One solution is to say, that they are valid iif their (transposed) body is equivalent.
Optimization possibilities for transposition
Apart from its power to describe the semantics of the language, transposition is very (maybe too) expensive. It is certainly not acceptable to really transpose every feature from a compiler designer point of view. So we need to find criteria to only transpose when really needed. The following system shows that this is not that easy:
What happens, when an object of class Y with its field a set to an object of class C has its feature g executed. Only the transposition of g to Y gives the answer:
g do a.f1 end
The covariant redefinition of a in Y resolved the potential repeated inheritance conflict. Nevertheless, if g wasn't transposed, feature f2 of class C would have been executed. So the transposition was reallly needed here. We may state:
- Every feature that uses a target of a covariant type needs to be transposed (Unqualified feature calls don't have a target).
This rule is actualy to restrictive. If our system wouldn't have contained the class C there wouldn't have been any need to transpose f. But such checks would be very expensive.
- If a feature is not transposition equal ....
Feature g of class X assumes We try to find out, wether it is necessary to really transpose feature g of class Y. The following code snippet gives the answer:
local y: Y c: C do create y create c
Transposition was never necessary in Eiffel compilers but it is now
For the following discussion we use this system of five classes: