DynBindModel

Not Ready for Review: This Page is Under Development!

Author: Matthias Konrad

Related articles:

Contents

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:

\gamma : \left(\mbox{TYPE} \times \mbox{NAME}\right) \rightarrow \left(\mbox{TYPE} \rightarrow \mbox{FEATURE}\right)

Function \gamma\, 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:

\gamma \left( \mbox{ANY} \mapsto \mbox{out}\right)

Namely an evaluation of \gamma\, 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 \gamma\, 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 \ll\, (This will be changed as soon as possible). The function operator \rightarrow is used for partial functions.

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

identity

\mbox{id}\left( \left\{ e_1, \ldots, e_n \right\} \right)\ = \left\{ e_1 \mapsto e_1, \ldots, e_n \mapsto e_n \right\}

Operator: inverse

{\left\{ l_1 \mapsto r_1, \ldots, l_n \mapsto r_n \right\}}^{-1}       =     \left\{ r_1 \mapsto l_1, \ldots, r_n \mapsto l_n \right\}

Operator: domain

\mbox{dom}\left(\left\{ l_1 \mapsto r_1, \ldots, l_n \mapsto r_n \right\}\right) = \left\{ l_1, \ldots, l_n \right\}

Operator: range

\mbox{ran}\left(\left\{ l_1 \mapsto r_1, \ldots, l_n \mapsto r_n \right\}\right) = \left\{ r_1, \ldots, r_n \right\}

Operator: range restriction

E \mapsto F \in r \triangleright T is equivalent to: E \mapsto F \in r \,\land\, F \in T

Example:

\left\{ 1 \mapsto 2, 2 \mapsto 3, 3 \mapsto 4 \right\} \,\triangleright\, \left\{ 3 \right\}  =  \left\{ 2 \mapsto 3 \right\}

Operator: override

E \mapsto F \in p \ll q is equivalent to: E \mapsto F \in q \,\lor\, \left( E \mapsto F \in p \,\land\, E \not\in \mbox{dom}\left( q \right) \right)

Example:

\left\{ 1 \mapsto 2, 2 \mapsto 3 \right\} \,\ll\, \left\{ 1 \mapsto 4, 3 \mapsto 6 \right\}    = \left\{ 1 \mapsto 4, 2 \mapsto 3, 3 \mapsto 6 \right\}

Operator: union

E \in \mbox{UNION} \ x \cdot \left(x \in S \ |\ T \right) is equivalent to: \exists x \cdot \left(x \in S \ \land\ E \in T \right)

Example:

\mbox{UNION} \ x \cdot \left(x \in \left\{ 1, 2, 3\right\} \ |\ \left\{ x, x^2 \right\} \right) is equivalent to: \left\{ 1, 2, 3, 4, 9\right\}

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 \,\gamma and make it considerably more complex. Its structure would become something like:

\gamma^{\prime} : \left(\mbox{TYPE} \times \mbox{NAME} \times \mbox{CONTEXT}\right) \rightarrow \left(\mbox{TYPE} \rightarrow \mbox{FEATURE}\right)

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:

Image:DynBindingABD.png

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

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

\tau : \mbox{TYPE} \rightarrow \left( \mbox{NAME} \rightarrow \mbox{FEATURE} \right)

For the example system the function is:

\tau =  \left\{    A \mapsto \left\{ f_1 \mapsto \underline{A.f_1}, g_1 \mapsto \underline{A.g_1} \right\},     B \mapsto \left\{ f_2 \mapsto \underline{B.f_2}, g_2 \mapsto \underline{B.g_2} \right\},    D \mapsto \left\{ f_1 \mapsto \underline{D.f_1}, g_1 \mapsto \underline{D.g_1} \right\} \right\}

The direct inheritance function

Eiffel classes can inherit from each other. This is described by the function \alpha\,:

\alpha : \mbox{TYPE} \rightarrow \mathbb{P} \left( \mbox{TYPE} \right) For the example system:

\alpha = \left\{    A \mapsto \emptyset,    B \mapsto \left\{ A \right\},    D \mapsto \left\{ A, B \right\} \right\}

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: \alpha \left( D \right)

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:

\eta : \mbox{TYPE} \rightarrow \left( \mbox{TYPE} \rightarrow \left( \mbox{NAME} \leftrightarrow \mbox{NAME} \right) \right)

For the example system it is:

\eta =  \left\{    A \mapsto \emptyset,    B \mapsto \left\{        A \mapsto \left\{ f_1 \mapsto f_2, g_1 \mapsto g_2 \right\}     \right\},    D \mapsto \left\{        A \mapsto \emptyset, B \mapsto \emptyset     \right\} \right\}

So \eta \left( D \right) \left( B \right) 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 to refer to 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:

\sigma : \mbox{TYPE} \rightarrow \left( \mbox{TYPE} \rightarrow \mathbb{P} \left( \mbox{NAME} \right) \right)

For the example system:

  • \sigma = \left\{    A \mapsto \emptyset,     B \mapsto \left\{ A \mapsto \emptyset \right\},     D \mapsto \left\{ A \mapsto \left\{ g_1 \right\},                      B \mapsto \left\{ f_2 \right\} \right\} \right\}

No undefine or redefine

Undefine and redefine are not represented in this model. The latter is generally not relevant for the semantics.

This article assumes that there is an unfolded form for undefines which is needed for the join semantics of precondition.

The order

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

For every system there is at least one topological order:

C: \mathbb{N} \rightarrow \mbox{TYPE}

Such that

\forall x, y\ \cdot \left( x > y \Rightarrow \lnot \left( C \left(x\right) < C \left(y\right) \right) \right)

For the example system there is only one such order:

C = \left\{ 1 \mapsto A, 2 \mapsto B, 3 \mapsto D \right\}

In practice class C\left(1\right) 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:

  • C_{n}\, for C\left( n \right)
  • \tau_{n}\, for \tau\left( C_n \right)
  • \eta_{n, b}\, for \eta \left( C_n \right) \left( b \right)
  • \alpha_{n}\, for \alpha \left( C_n \right)
  • \sigma_{n, b}\, for \sigma \left( C_n \right) \left( b \right)
  • S_m\, for \left\{ C_1 \ldots C_m \right\}

The full example

The full model of the example is thus:

S=\left\{ A, B, D\right\}

\tau =  \left\{    A \mapsto \left\{ f1 \mapsto \underline{A.f1}, g1 \mapsto \underline{A.g1} \right\},     B \mapsto \left\{ f2 \mapsto \underline{B.f2}, g2 \mapsto \underline{B.g2} \right\},    D \mapsto \left\{ f1 \mapsto \underline{D.f1}, g1 \mapsto \underline{D.g1} \right\} \right\}

\alpha = \left\{    A \mapsto \emptyset,    B \mapsto \left\{ A \right\},    D \mapsto \left\{ A, B \right\} \right\}

\eta =  \left\{    A \mapsto \emptyset,    B \mapsto \left\{        A \mapsto \left\{ f_1 \mapsto f_2, g_1 \mapsto g_2 \right\}     \right\},    D \mapsto \left\{        A \mapsto \emptyset, B \mapsto \emptyset     \right\} \right\}

\sigma = \left\{    A \mapsto \emptyset,     B \mapsto \left\{ A \mapsto \emptyset \right\},     D \mapsto \left\{ A \mapsto \left\{ g_1 \right\},                      B \mapsto \left\{ f_2 \right\} \right\} \right\}

C = \left\{ 1 \mapsto A, 2 \mapsto B, 3 \mapsto D \right\}

The naming function

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

\beta_S: \left( \mbox{TYPE} \times \mbox{TYPE} \right) \rightarrow\left( \mbox{NAME} \rightarrow\mbox{NAME} \right)

When one looks at a feature with name n in a class X and is interested in the name of the selected version of this feature in descendant class Y the naming function tells the answer:

\beta\left(X\right)\left(Y\right)\left(n\right)

This function simplifies the construction of the dynamic binding function.

For the example system:

\beta_{\left\{ A, B, D \right\}} = \{

\left( A \mapsto A \right) \mapsto \left\{f_1 \mapsto f_1, g_1 \mapsto g_1 \right\},

\left( A \mapsto B \right) \mapsto \left\{ f_1 \mapsto f_2, g_1 \mapsto g_2 \right\},

\left( A \mapsto D \right) \mapsto \left\{ f_1 \mapsto f_2, g_1 \mapsto g_1 \right\},

\left( B \mapsto B \right) \mapsto \left\{ f_2 \mapsto f_2, g_2 \mapsto g_2 \right\},

\left( B \mapsto D \right) \mapsto \left\{ f_2 \mapsto f_2, g_2 \mapsto g_2 \right\},

\left( D \mapsto D \right) \mapsto \left\{ f_1 \mapsto f_1, f_2 \mapsto f_2, g_1 \mapsto g_1, g_2 \mapsto g_2 \right\} \}

Definition of the naming function

The naming function \beta_S\, is defined recursively. Meaning, that \beta_{S_m } is defined based on \beta_{ S_{m-1}}.

This is sound since: \beta_{ \left\{ C_1 \right\} } \subseteq \beta_{ \left\{ C_1, C_2 \right\} } \subseteq \ldots  \subseteq \beta_S

The details of the functions are covered in the succeeding sections, its definition is shown here:

\beta_{ S_m } \left( C_q \mapsto C_p \right) =  \begin{cases}  \emptyset              &  \mbox{if } q > p \\  \beta_{ S_{m-1} } \left( C_q \mapsto C_p \right)             &  \mbox{if } q <= p < m \\  \mbox{id} \left(     \mbox{dom} \left( \tau_m \right)    \, \cup \, \mbox {UNION} \ b \cdot \left(     b \in \alpha_m       \ |\ \mbox{ran}\left( \beta_{ S_{m-1} } \left( b \mapsto b \right) \ll \eta_{m, b} \right)     \right)     \right) &  \mbox{if } q = p = m \\  \mathcal{A} \ll \mathcal{B}  & \mbox{otherwise } \end{cases}

Where

\mathcal{A}    =  \mbox {UNION} \ b \cdot \left(    b \in \alpha_m    \  | \     \phi_m \left( C_q \mapsto b \right) \right)

And:

\mathcal{B}    =  \mbox {UNION} \ b \cdot \left(    b \in \alpha_m       \ |\     \phi_m \left( C_q \mapsto b \right) \triangleright \sigma_{m,b} \right)

And:

\phi_m\left( C_q \mapsto b \right)  = \beta_{S_{m-1}}     \left(C_q\mapsto b\right) ; \left(    \beta_{S_{m-1}}\left(b \mapsto b\right)\ll\eta_{m,b} \right)

The trivial cases

There are two trivial cases:

  1. When q > p \, then according to the definition of the order C\,, C_q\, is not an ancestor of C_p\,. And thus \beta_{ S_{m-1} } \left( C_q \mapsto C_p \right) evaluates to the empty set in this case.
  2. When both p\, and q\, are smaller than m\,, it is defined by \beta_{ S_{m-1} } \left( C_q \mapsto C_p \right). Here the recursive structure of the definition can be seen.

The identity function

\beta_{ S_m } \left( C_m \mapsto C_m \right) is the identity function over all the feature names of the class C_m\,. It is an identity function since a given feature name in a class has no other name in that same class. The set of feature names of a class C_m\,consists of the names of:

  1. The features declared in C_m\,
  2. The inherited features (after renaming) of class C_m\,.

The first set is trivial, it is exactly the domain of \tau_m\,.

Every base class b\, of class C_m\, contributes the set \mbox{ran}\left( \beta_{ S_{m-1} } \left( b \mapsto b \right) \ll \eta_{m, b} \right) to the set of inherited feature names.

The whole identity function is thus:

\beta_{ S_m } \left( C_m \mapsto C_m \right)    = \mbox{id} \left(     \mbox{dom} \left( \tau_m \right)    \, \cup \, \mbox {UNION} \ b \cdot \left(     b \in \alpha_m       \ |\ \mbox{ran}\left( \beta_{ S_{m-1} } \left( b \mapsto b \right) \ll \eta_{m, b} \right)     \right)     \right)

The helper function

The definition of \beta_{ S_m } \left( C_q \mapsto C_m \right) for q<m\, is a little harder to construct. Helper function \phi\, shown here should simplify things.

There are |\alpha_m|\, paths from class C_q\, to class C_m\, that need to be considered. They are shown in the following picture:

Image:DynBindingCqCm.png

\phi_m\left( C_q \mapsto b \right) describes the naming on the path that goes through b\,:

\phi_m\left( C_q \mapsto b \right)  = \beta_{S_{m-1}}     \left(C_q\mapsto b\right) ; \left(    \beta_{S_{m-1}}\left(b \mapsto b\right)\ll\eta_{m,b} \right)

The complex case

The union of the helper function over all the base classes b \in \alpha_m for given C_q\, and C_m\,:

\mathcal{A}    =  \mbox {UNION} \ b \cdot \left(    b \in \alpha_m    \  | \     \phi_m \left( C_q \mapsto b \right) \right)

yields a relation but not a function. The selected features names are not yet taken into account. The explicitly selected features names are:

\mathcal{B}    =  \mbox {UNION} \ b \cdot \left(    b \in \alpha_m       \ |\     \phi_m \left( C_q \mapsto b \right) \triangleright \sigma_{m,b} \right)

So if \mathcal{A}\, is overridden with \mathcal{B}\, the result is again a function:

\beta_{S_m} \left( C_q \mapsto C_m \right)  = \mathcal{A} \ll \mathcal{B}

The dynamic binding function

The initial goal was the definition of the dynamic binding function:

\gamma : \left(\mbox{TYPE} \times \mbox{NAME}\right) \rightarrow \left(\mbox{TYPE} \rightarrow \mbox{FEATURE}\right)

For the example system it is defined as:

\gamma = \{\

\left( A \mapsto f_1 \right)        \mapsto     \left\{ A \mapsto \underline{A.f_1}, B \mapsto \underline{B.f_2}, D \mapsto \underline{B.f_2} \right\},

\left( A \mapsto g_1 \right)        \mapsto     \left\{ A \mapsto \underline{A.g_1}, B \mapsto \underline{B.g_2}, D \mapsto \underline{D.g_1} \right\},

\left( B \mapsto f_2 \right)        \mapsto     \left\{ B \mapsto \underline{B.f_2}, D \mapsto \underline{B.f_2} \right\},

\left( B \mapsto g_2 \right)        \mapsto     \left\{ B \mapsto \underline{B.g_2}, D \mapsto \underline{B.g_2} \right\},

\left( D \mapsto f_1 \right)        \mapsto     \left\{ D \mapsto \underline{D.f_1} \right\},

\left( D \mapsto g_1 \right)        \mapsto     \left\{ D \mapsto \underline{D.g_1} \right\},

\left( D \mapsto f_2 \right)        \mapsto     \left\{ D \mapsto \underline{B.f_2} \right\},

\left( D \mapsto g_2 \right)        \mapsto     \left\{ D \mapsto \underline{B.g_2} \right\}\ \}


The definition

With the naming function defined it is surprisingly simple to give the dynamic binding function. To make it even easier the function:

\delta : \left(\mbox{TYPE} \times \mbox{TYPE}\right) \rightarrow \left(\mbox{NAME} \rightarrow \mbox{FEATURE}\right)

is defined. From this function, \gamma\, can be derived:

\gamma \left( ST \mapsto f_n \right) = \left\{ DT \mapsto \underline{f}\ |\ \delta \left( ST \mapsto DT \right) \left( f_n \right) = \underline{f} \right\}

The helper function

What remains is to define \delta\,. As with the naming function it is done recursively. \delta_{ S_m } can be defined based on \delta_{ S_{m-1} } since the following holds:

\delta_{ S_1 } \subseteq \delta_{ S_2 } \subseteq \ldots \delta_{ S }

The definition:

\delta_{ S_m } \left( C_q \mapsto C_p \right) =  \begin{cases}  \emptyset              &  \mbox{if } q > p \\  \delta_{ S_{m-1} } \left( C_q \mapsto C_p \right)             &  \mbox{if } q <= p < m \\  \beta_S \left( C_q \mapsto C_m \right) ; \left( \left( \mbox {UNION} \, b \cdot \left(     b \in \alpha_m       \, |\,      \left( \beta_S \left( b \mapsto b \right) ; \eta_{m,b} \right)^{-1}       ;     \delta_{S_{m-1}}\left( b \mapsto b \right)\right)  \right) \ll \tau_{m} \right) &  \mbox{otherwise} \end{cases}