Difference between revisions of "Transposition"

m (Optimization possibilities for transposition)
m (Transposition, repeated inheritance and replication)
 
(94 intermediate revisions by 2 users not shown)
Line 1: Line 1:
With the ECMA Eiffel Standard, the dynamic binding semantics of the Eiffel language are slightly changed. This puts some additional burdens on the compiler implementor. We will discuss two of them:
+
[[Category:ECMA]]
* The need for copying inherited features in descendants.
+
Author: Matthias Konrad
* More complex dynamic binding.
+
==Description==
 
+
====The new dynamic binding semantics====
==Transposition==
+
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:
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:
+
  
 
{|border="0" cellpadding="2" cellspacing="0" align="center"
 
{|border="0" cellpadding="2" cellspacing="0" align="center"
 
|-valign="top" -halign="center"
 
|-valign="top" -halign="center"
|<code>[eiffel, N]
+
|[[Image:SC_ABC.jpg|280px]]
class
+
|
   B
+
<code>[eiffel,N]
feature
+
local
   f do g end
+
   a: A
   g do end
+
  b: B
 +
do
 +
   create {C}a
 +
  create {C}b
 +
  a.f         --Line 3
 +
   b.f1        --Line 4
 
end
 
end
 
</code>
 
</code>
 +
|}
 +
 +
The semantics for line 3 have always been clear, feature ''f2'' should be called. For line 4 the ECMA standard says, that feature ''f1'' is called, whereas the current ISE compiler chooses feature ''f2''. So the ECMA standard restrains the power of select; it only has an impact if there are two or more inheritance paths 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.
 +
 +
The lesson learned is:
 +
*The exact static type of an entity has an important influence on the dynamic binding.
 +
 +
====Covariance and the missing part of the ECMA standard====
 +
Eiffel allows covariant redefinitions. We reuse the previous example system and add two new classes ''X'' and ''Y'':
 +
 +
{|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 whether 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 the flat-short representation of ''Y'' tells us something different (Features of ''ANY'' omitted):
 +
 +
{|border="0" cellpadding="2" cellspacing="0" align="center"
 +
|-valign="top" -halign="center"
 
|
 
|
<code>[eiffel, N]
+
<code>[eiffel,N]
class
+
class  
   D
+
   Y
inherit
+
  B
+
      rename f as f1, g as g1 redefine f1 select f1, g1 end
+
      rename f as f2, g as g2 end
+
 
feature
 
feature
   f1 do ... end
+
   a: B
 +
  g
 +
      do
 +
        a.f1
 +
      end
 
end
 
end
 
</code>
 
</code>
 
|}
 
|}
  
class D has the transposed form (we omit the features from ANY):
+
So according to the flat-short representation, when ''g'' is executed on an object of class ''Y'', the static type of ''a'' is ''B''. The flat-short thus implies an other dynamic-binding semantics than the one currently described in ECMA.
 +
 
 +
====Transposition====
 +
In the flat-short representation all the inherited features are transposed to the class. It is this transposition that causes the problem. Transposition is also used for the new join semantics (ECMA-3). Furthermore it should be used in the definition of some unfolded forms (for example for the unfolded form of an assertion (8.10.2) or replication). We may say, that if the transposition of a specimen is semantically contradicting then this is a huge problem not only but also because the ECMA standard heavily relies on unfolded forms.
 +
 
 +
Or we state, that the unfolded form of a class has all its inherited features transposed. The unfolded form of class ''Y'' would be: (we again omit the ''ANY'' stuff)
  
 
{|border="0" cellpadding="2" cellspacing="0" align="center"
 
{|border="0" cellpadding="2" cellspacing="0" align="center"
|-valign="top" -halign="center"|
+
|-valign="top" -halign="center"
<code>[eiffel, N]
+
|
 +
<code>[eiffel,N]
 
class
 
class
   D
+
   Y
 
inherit
 
inherit
   B
+
   X redefine a, b end
      rename f as f1, g as g1 redefine f1, g1 select f1, g1 end
+
      rename f as f2, g as g2 redefine f2, g2 end
+
 
feature
 
feature
   f1 do ... end
+
   a: B
   g1 do end 
+
   g
  f2 do g2 end
+
      do
  g2 do end  
+
        a.f1
 +
      end
 
end
 
end
 
</code>
 
</code>
 
|}
 
|}
  
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.
+
Apart from the inheritance clause, this unfolded form is similar to the flat-short view of the class. In this unfolded form the inheritance relationship is degraded to a pure subtyping, no feature of ''X'' is ever executed for an object of type ''Y''. This is very convenient, for every class we get both its definitive features and its subtype relation. One interesting property of this is, that every unqualified call is statically bound.
  
====Optimization possibilities for transposition====
+
Conclusions:
Apart from its power to describe the semantics of the language, transposition is very (mabe 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:
+
*With the old dynamic-binding semantics it does not matter whether or not something is transposed.
 +
*With the new semantics we need to either transpose always or never. Since transposition is indeed needed every inherited feature needs to be transposed.
  
[[Image:Example.jpg|450px]]
+
====Incremental transposition====
 +
To construct the transposed form of a class, the transposed forms of its direct base classes are needed. To illustrate that we show class ''Z'' and its transposition:
  
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:
+
{|border="0" cellpadding="2" cellspacing="0" align="center"
 
+
|-valign="top" -halign="center"
<code>[Eiffel,n]
+
|
g
+
<code>[eiffel,N]
   do
+
class
      a.f1
+
   Z
   end
+
inherit
 +
  Y redefine a end
 +
feature
 +
   a: C
 +
end
 
</code>
 
</code>
 
+
|
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:
+
<code>[eiffel,N]
 
+
class
* Every feature that uses a target of a covariant type needs to be transposed (Unqualified feature calls don't have a target).
+
  Z
 
+
inherit
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.
+
   Y redefine a,g end
 
+
feature
* If a feature is not transposition equal ....
+
   a: C
 
+
  g
 
+
      do
 
+
        a.f1
 
+
      end
 
+
end
 
+
 
+
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:
+
 
+
<code>[Eiffel,n]
+
local
+
   y: Y
+
   c: C
+
do
+
  create y
+
  create c
+
 
</code>
 
</code>
 +
|}
 +
Would we have constructed the unfolded form of ''Z'' based on ''X'', then feature ''g'' would call feature ''f2''.
  
 +
====Transposition, repeated inheritance and replication====
 +
We show now, that transposition simplifies the discussion of repeated inheritance and replication. Lets forget for a moment everything we know about replication (ECMA rules 8.16.2, 8.16.3, 8.16.4, 8.16.5), we only need to specify what happens if two transposed features happen to have the same name as in the following two systems ''S1'' and ''S2'':
  
 +
{|border="0" cellpadding="2" cellspacing="0" align="center"
 +
|-valign="top" -halign="center"
 +
|[[Image:TransRep.jpg]]
 +
|}
 +
 +
The unfolded form of class ''B'' has two features with name ''f'', since both of them are the same they can just be joined. The unfolded form of class ''D'' also has two features with name ''g''. One of this features calls ''f1'' and the other one calls ''f2''. This is clearly a conflict. Two transposed features should be allowed to have the same name if and only if they are identical.
 +
This is all there is to say about replication. Again, the fact, that we have a definite set of features for every class makes things simpler.
  
 +
====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. So we need to find criteria to only transpose when really needed. Interestingly this is quite difficult but it is maybe better to have a simple language and complex compilers than the opposite.
  
  
 +
==Summary==
 +
* Transposition clarifies semantics of feature calls in the ECMA standard.
 +
* The new dynamic binding semantics forces every class to perform a transposition of all its inherited features (with little space for optimization, see [[Transposition#Optimization_possibilities_for_transposition|above]])
  
 
+
Given the above, one could ask whether the new dynamic binding rules make sense:
Transposition was never necessary in Eiffel compilers but it is now
+
# If we use the semantics of the Eiffel Software compiler, transposition only needs to be done for replicated features, thus simplifying the work for compiler writers.
For the following discussion we use this system of five classes:
+
# The semantics get clearer since by just looking at the text of a class you know which feature will be called (no need for you to perform a mental incremental transposition).
 +
# In real life, there are few occurrences of repeated inheritance that need a select.

Latest revision as of 14:57, 13 November 2006

Author: Matthias Konrad

Description

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:

SC ABC.jpg
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 should be called. For line 4 the ECMA standard says, that feature f1 is called, whereas the current ISE compiler chooses feature f2. So the ECMA standard restrains the power of select; it only has an impact if there are two or more inheritance paths 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.

The lesson learned is:

  • The exact static type of an entity has an important influence on the dynamic binding.

Covariance and the missing part of the ECMA standard

Eiffel allows covariant redefinitions. We reuse the previous example system and add two new classes X and Y:

Example.jpg

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 whether 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 the flat-short representation of Y tells us something different (Features of ANY omitted):

class 
   Y
feature
   a: B
   g 
      do
         a.f1
      end
end

So according to the flat-short representation, when g is executed on an object of class Y, the static type of a is B. The flat-short thus implies an other dynamic-binding semantics than the one currently described in ECMA.

Transposition

In the flat-short representation all the inherited features are transposed to the class. It is this transposition that causes the problem. Transposition is also used for the new join semantics (ECMA-3). Furthermore it should be used in the definition of some unfolded forms (for example for the unfolded form of an assertion (8.10.2) or replication). We may say, that if the transposition of a specimen is semantically contradicting then this is a huge problem not only but also because the ECMA standard heavily relies on unfolded forms.

Or we state, that the unfolded form of a class has all its inherited features transposed. The unfolded form of class Y would be: (we again omit the ANY stuff)

class
   Y
inherit
   X redefine a, b end
feature
   a: B
   g
      do
         a.f1
      end
end

Apart from the inheritance clause, this unfolded form is similar to the flat-short view of the class. In this unfolded form the inheritance relationship is degraded to a pure subtyping, no feature of X is ever executed for an object of type Y. This is very convenient, for every class we get both its definitive features and its subtype relation. One interesting property of this is, that every unqualified call is statically bound.

Conclusions:

  • With the old dynamic-binding semantics it does not matter whether or not something is transposed.
  • With the new semantics we need to either transpose always or never. Since transposition is indeed needed every inherited feature needs to be transposed.

Incremental transposition

To construct the transposed form of a class, the transposed forms of its direct base classes are needed. To illustrate that we show class Z and its transposition:

class 
   Z
inherit
   Y redefine a end
feature
   a: C
end
class 
   Z
inherit
   Y redefine a,g end
feature
   a: C
   g
      do
         a.f1 
      end
end

Would we have constructed the unfolded form of Z based on X, then feature g would call feature f2.

Transposition, repeated inheritance and replication

We show now, that transposition simplifies the discussion of repeated inheritance and replication. Lets forget for a moment everything we know about replication (ECMA rules 8.16.2, 8.16.3, 8.16.4, 8.16.5), we only need to specify what happens if two transposed features happen to have the same name as in the following two systems S1 and S2:

TransRep.jpg

The unfolded form of class B has two features with name f, since both of them are the same they can just be joined. The unfolded form of class D also has two features with name g. One of this features calls f1 and the other one calls f2. This is clearly a conflict. Two transposed features should be allowed to have the same name if and only if they are identical. This is all there is to say about replication. Again, the fact, that we have a definite set of features for every class makes things simpler.

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. So we need to find criteria to only transpose when really needed. Interestingly this is quite difficult but it is maybe better to have a simple language and complex compilers than the opposite.


Summary

  • Transposition clarifies semantics of feature calls in the ECMA standard.
  • The new dynamic binding semantics forces every class to perform a transposition of all its inherited features (with little space for optimization, see above)

Given the above, one could ask whether the new dynamic binding rules make sense:

  1. If we use the semantics of the Eiffel Software compiler, transposition only needs to be done for replicated features, thus simplifying the work for compiler writers.
  2. The semantics get clearer since by just looking at the text of a class you know which feature will be called (no need for you to perform a mental incremental transposition).
  3. In real life, there are few occurrences of repeated inheritance that need a select.