DynBindModel: Difference between revisions

From EiffelStudio: an EiffelSoftware project
Jump to navigation Jump to search
Konradm (talk | contribs)
Konradm (talk | contribs)
Line 176: Line 176:
</math>
</math>


So <math>\eta \left( D \right) \left( B \right) </math> gives the renamings in class D for the inherited class B.
So <math>\eta \left( D \right) \left( B \right) </math> gives the renaming in class D for the inherited class B.


Again, the fact that the final result is not a function but just a relation is surprising. Take it as a property of CoreEiffel, that for a given base class a feature can be renamed to many different names. This is also possible in Eiffel, since it allows several parent parts reference the same class.  
Again, the fact that the final result is not a function but just a relation is surprising. Take it as a property of CoreEiffel, that for a given base class a feature can be renamed to many different names. This is also possible in Eiffel, since it allows several parent parts reference the same class.  

Revision as of 16:10, 26 January 2007

Warning: Warning: Article under development

Author: Matthias Konrad

Related articles:


Motivation:

Eiffel's dynamic binding is somewhat more complex than that of other languages. Its structure is still very simple and is described by the function:

γ:(TYPE×NAME)→(TYPE→FEATURE)

Function γ contains both the static and dynamic binding part of the language. In the following feature:

f (a: ANY)
   do
      print (a.out)
   end

The static binding part of call a.out can be described as:

γ(ANY↦out)

Namely an evaluation of γ with a tuple containing the static type of the call and the name. The result of this is a function. This function evaluated at runtime will yield the right feature according to the dynamic type of a.

In the rest of this text, the construction of the function γ is shown, based on any given valid Eiffel system.

There is a special wiki page that shows an example calculation of the dynamic binding function for a complete system: DynBindModelExamples

Notation

This wiki uses the notation introduced in these slides by Prof. Jean-Raymond Abrial (ETHZ): notation The only difference is the symbol for override which is ≪ (This will be changed as soon as possible). The function operator → is used for partial functions.

We will give some rather informal definitions for the set-theoretic operators used here.

identity

id({e1,…,en}) ={e1↦e1,…,en↦en}

Operator: inverse

{l1↦r1,…,ln↦rn}−1={r1↦l1,…,rn↦ln}

Operator: domain

dom({l1↦r1,…,ln↦rn})={l1,…,ln}

Operator: range

ran({l1↦r1,…,ln↦rn})={r1,…,rn}

Operator: range restriction

E↦F∈r▹T is equivalent to: E↦F∈r∧F∈T

Example:

{1↦2,2↦3,3↦4}▹{3}={2↦3}

Operator: override

E↦F∈p≪q is equivalent to: E↦F∈q∨(E↦F∈p∧E∉dom(p))

Example:

{1↦2,2↦3}≪{1↦4,3↦6}={1↦4,2↦3,3↦6}

Operator: union

E∈UNION x⋅(x∈S | T) is equivalent to: ∃x⋅(x∈S ∧ E∈T)

Example:

UNION x⋅(x∈{1,2,3} | {x,x2}) is equivalent to: {1,2,3,4,9}

Unfolded forms and replication

The Eiffel ECMA standard defines semantics in two ways. A small core of the language (called CoreEiffel) is described explicitly. The rest is described by giving unfolded forms. These unfolded forms map the constructs that do not belong to CoreEiffel (but to Eiffel) to constructs that do belong to CoreEiffel.

This is especially important for replication. There is an other article that gives an unfolded form for replication. This article shows (somewhat informal) how replication can be reduced away by a similar construct. As a result CoreEiffel does not need any replication anymore.

If replication would be a valid construct of CoreEiffel this would change the very structure of the dynamic binding function γ and make it considerably more complex. Its structure would become something like:

γ′:(TYPE×NAME×CONTEXT)→(TYPE→FEATURE)

A model for an Eiffel System

First of all, the model used here does not cover generics. The concepts type and class are thus considered equal.

For making things simpler the following example system is used:

An Eiffel system is basically just a set S of classes (or a set of types). For the example system, S={A,B,D}

The features declared in a class

For all these classes there is a partial function that maps the name of the features declared in the class to the actual feature (maybe feature body would have been a better name):

τ:TYPE→(NAME→FEATURE)

For the example system the function is:

τ={A↦{f1↦A.f1_,g1↦A.g1_},B↦{f2↦B.f2_,g2↦B.g2_},D↦{f1↦D.f1_,g1↦D.g1_}}

The direct inheritance function

Eiffel classes can inherit from each other. This is described by the function α: α:TYPE→ℙ(TYPE) For the example system:

α={A↦∅,B↦{A},D↦{A,B}}

It may surprise, that the functions range is a set of classes and not a list, since it is not reflected how may times a class inherits from a given base class. This is indeed a property of CoreEiffel assumed here but not stated in the standard. This property comes as a result of the unfolded form of replication.

The base classes of class D are expressed as: α(D)

The renaming function

The renaming function yields for every class of the system a second function that describes its renaming. This second function yields for every base class of the class a relation that describes the renaming for a given base class:

η:TYPE→(TYPE→(NAME↔NAME))

For the example system it:

η={A↦∅,B↦{A↦{f1↦f2,g1↦g2}},D↦{A↦∅,B↦∅}}

So η(D)(B) gives the renaming in class D for the inherited class B.

Again, the fact that the final result is not a function but just a relation is surprising. Take it as a property of CoreEiffel, that for a given base class a feature can be renamed to many different names. This is also possible in Eiffel, since it allows several parent parts reference the same class.

The select function

The select function yields for a given class the feature names of the features that are explicitly selected for a base class:

σ:TYPE→(TYPE→ℙ(NAME))

For the example system:

  • σ={A↦∅,B↦{A↦∅},D↦{A↦{g1},B↦{f2}}}

The order

The operator > is the "ancestor of" and < the "descendant of" operator.

For every system there is at least one topological order:

C:ℕ→TYPE

Such that

∀x,y ⋅(x>y⇒¬(C(x)<C(y)))

For the example system there is only one such order:

C={1↦A,2↦B,3↦D}

In practice class C(1) is always class ANY. For simplification, in this article classes are not implicitly inherited by ANY, this simplifies the examples.

Some shortcuts

To simplify the discussion, some shortcuts are introduced:

  • Cn for C(n)
  • τn for τ(Cn)
  • ηn,b for η(Cn)(b)
  • αn for α(Cn)
  • σn,b for σ(Cn)(b)

The full example

The full model of the example is thus:

S={A,B,D}

τ={A↦{f1↦A.f1_,g1↦A.g1_},B↦{f2↦B.f2_,g2↦B.g2_},D↦{f1↦D.f1_,g1↦D.g1_}}

α={A↦∅,B↦{A},D↦{A,B}}

η={A↦∅,B↦{A↦{f1↦f2,g1↦g2}},D↦{A↦∅,B↦∅}}

σ={A↦∅,B↦{A↦∅},D↦{A↦{g1},B↦{f2}}}

C={1↦A,2↦B,3↦D}

The naming function

The naming function shows for two classes how their feature names are related to each other:

βS:(TYPE×TYPE)→(NAME→NAME)

In the previous example class C renames feature f to f1. β{B,C,D}(B↦C)(f) thus evaluates to f1. β{B,C,D}(B↦D)(f) evaluates to f2, the selected feature in class D. The range of βS is a function, this means that every name is mapped to just one name.

As an example the complete function β{B,C,D} is shown:

β{B,C,D}={

(B↦B)↦{f↦f},(B↦C)↦{f↦f1},(B↦D)↦{f↦f2},

(C↦C)↦{f1↦f1},(C↦D)↦{f1↦f1},

(D↦D)↦{f1↦f1,f2↦f2,f3↦f3}}

Definition of the naming function

The shortcut Sm denotes the system {C1…Cm}.

A recursive definition of βS follows. βSm is defined based on βSm−1.

This is sound since: β{C1}⊆β{C1,C2}⊆…βS

The trivial function S0 is the empty set. Sm for m>0 is defined by by showing what βSm(Cq↦Cp) evaluates to for q≤m and p≤m. There are two trivial cases. When q>p it evaluates to the empty set and when both p and q are smaller than m, it is defined by βSm−1(Cq↦Cp).

βSm(Cm↦Cm) is an identity function over the set of feature names of Cm. The set of feature names consists of the names of

  • the inherited features (after renaming).
  • the features declared in Cm.

The features declared in Cm are given by the domain of τm. Every base class b∈αm contributes the set: ran(βSm−1(b↦b)≪ηm,b) to the set of inherited feature names. We can now define:

βSm(Cm↦Cm)=id(dom(τm)∪UNION b⋅(b∈αm | ran(βSm−1(b↦b)≪ηm,b)))

The definition of βSm(Cq↦Cm) for q<m depends on two helper functions. Function βS′ ignores whether a features is selected or not, apart from that it is similar to the naming function βS. Its structure is:

βS′:(TYPE×TYPE)→(NAME↔NAME)

The image β{B,C,D}′(B↦C)[f] from the previous example is {f1,f2,f3}. Its definition is:

βSm′(Cq↦Cm)=UNION b ⋅ (b∈αm | βSm−1(Cq↦b);(id(ran(βSm−1(Cq↦b)))≪ηm,b))

The second helper function θS is again similar to the naming function but it only includes selected feature names. Its structure is:

θS:(TYPE×TYPE)→(NAME→NAME)

θ{B,C,D}(B↦D) from the previous example is {f↦f2}, whereas θ{B,C,D}(C↦D) is the empty set. Its definition is:

θSm(Cq↦Cm)=UNION b ⋅ (b∈αm | βSm−1(Cq↦b);(id(ran(βSm−1(Cq↦b)))≪ηm,b)▹σm,b)

The definition of βSm(Cq↦Cm) for q<m is then:

βSm(Cq↦Cm)=βSm′(Cq↦Cm)≪θSm(Cq↦Cm)

With this we have defined the whole naming function. As a summary we present the whole definition:

βSm(Cq↦Cp)={∅if q>pβSm−1(Cq↦Cp)if q<=p<mid(dom(τm)∪UNION b⋅(b∈αm | ran(βSm−1(b↦b)≪ηm,b)))if q=p=mβSm′(Cq↦Cm)≪θSm(Cq↦Cm)otherwise 

Where

βSm′(Cq↦Cm)=UNION b ⋅ (b∈αm | βSm−1(Cq↦b);(id(ran(βSm−1(Cq↦b)))≪ηm,b))

And:

θSm(Cq↦Cm)=UNION b ⋅ (b∈αm | βSm−1(Cq↦b);(id(ran(βSm−1(Cq↦b)))≪ηm,b)▹σm,b)

The dynamic binding function

With the naming function defined it is surprisingly simple to give the dynamic binding function. To make it even easier we define the function: δ:(TYPE×TYPE)→(NAME→FEATURE)

With this function defined we can derive γ:

γ(S↦fn)={D↦f | δ(S↦D)(fn)=f}

What remains is to define δ. As with the naming function we do it recursively. We define δSm based on δSm−1 since the following holds:

δS1⊆δS2⊆…δS

The definition:

δSm(Cq↦Cp)={∅if q>pδSm−1(Cq↦Cp)if q<=p<mβS(Cq↦Cm);((UNIONb⋅(b∈αm|(βS(b↦b);ηm,b)−1;δSm−1(b↦b)))≪τm)otherwise