DynBindModel: Difference between revisions

From EiffelStudio: an EiffelSoftware project
Jump to navigation Jump to search
Konradm (talk | contribs)
Konradm (talk | contribs)
Line 7: Line 7:




==Motivation:==
===Motivation:===


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

Revision as of 12:25, 26 January 2007

Author: Matthias Konrad

Related articles:


Motivation:

Eiffels 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 decribed 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.

Some operators

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

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

We introduce the operator > as the "ancestor of" and < as "descendant of" operator.

Without loss of generality we can say that any Eiffel system S is a set of classes {C1,C2…Cn} such that Cq<Cp if q<p. In practice class C1 will allways be class ANY but this is not of importance here.

The function:

  • τ:TYPE→(NAME→FEATURE)

Yields the features declared in a given class.

  • η:TYPE→(TYPE→(NAME→NAME))

Yields the renamings of the different parent parts by a class:

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

  • α:TYPE→ℙ(TYPE)

Gives the classes from which a given class inherits:

α(A) Gives the base classes of the of class A.

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

Gives all the selected feature names of an inherited class of a class.

Furthermore we give the following shortcuts:

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

An example

For the following System:

class
   B
feature
   f do ... end
end
class
   C
inherit
   B
      rename f as f1 redefine f1 end
feature
   f1 do ... end
end
class
   D
inherit
   C
   B 
      rename f as f2 redefine f2 select f2 end
   B
      rename f as f3 redefine f3 end
feature
   f2 do ... end
   f3 do ... end
end

We get:

  • S={B,C,D}
  • τ={B↦{f↦B.f_},C↦{f1↦C.f1_},D↦{f2↦D.f2_,f3↦D.f3_}}
  • η={C↦{B↦{f↦f1}},D↦{B↦{f↦f2,f↦f3}}}
  • α={C↦{B},D↦{B,C}}
  • σ={D↦{B↦{f2}}}

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