topical media & game development

object-oriented programming

# Self-reference

Recursive types are compound types in which the type itself occurs as the type of one of its components. Self-reference in objects clearly involves recursive types since the expression self denotes the object itself, and hence has the type of the object. In $F_\left\{%m\right\}$, our extension of $F_\left\{<=\right\}$ taken from  [CoHC90], recursive types are written as $%m %a. %t\left[%a\right]$, where $%m$ is the recursion abstractor and $%a$ a type variable. The dependence of $%t$ on $%a$ is made explicit by writing $%t\left[%a\right]$. We will use the type expressions $%m %a.%t\left[%a\right]$ to type object specifications of the form $%l\left(self\right).\left\{ a_1 = e_1, ..., a_n = e_n \right\}$ as indicated by the type assignment rule below. Object specifications may be regarded as class descriptions in C++ or Eiffel.

#### Self-reference -- recursive types

$F_\left\{ %m \right\}$

• $%t ::= ... | %m %a. %t \left[%a\right]$
• $e ::= ... | %l\left( self \right).\left\{ a1 = e_1, ..., a_n = e_n \right\}$

#### Type assignment

• \ffrac{ $%G |- e_i : %t_i$ \hspace{1cm} $\left(i = 1..n\right)$ }{ $%G |- %l\left( self \right).\left\{ a1 = %t_1,.., a_n = %t_n \right\} \e %m%a.\left\{ a1:%t_1,.., a_n : %t_n \right\}\left[%a\right]$ }

#### Refinement

• \ffrac{ $%G, %a <= %b |- %s <= %t$ }{ $%G |- %m %a.%s\left[%a\right] <= %m %b.%t \left[%b\right]$ }

slide: A calculus for recursive types

The subtype refinement rule for recursive types states that $%m %a. %s \left[%a\right] <= %m %b . %t\left[%b\right]$ if we can prove that $%s <= %t$ assuming that $%a <= %b$. An object specification $%l\left(self\right).\left\{ ... \right\}$ is a function with the type of the actual object as its domain and (naturally) also as its range. For convenience we will write an object specification as $%l\left(self\right).F$, where F denotes the object record, and the type of an object specification as $%m %a.F\left[%a\right]$, where $F\left[%a\right]$ denotes the (abstract) type of the record F. To obtain from an object specification $%l\left(self\right).F$ the object that it specifies, we need to find some type $%s$ that types the record specification F as being of type $%s$ precisely when we assign the expression self in F the type $%s$. Technically, this means that the object of type $%s$ is a fixed point of the object specification $%l\left(self\right).F\left(self\right)$ which is of type $%s -> %s$. We write this as $\Y\left( %l\left(self\right).F\left(self\right) \right) : %s$, which says that the object corresponding to the object specification is of type $%s$. See slide 9-recursion.

#### Object semantics -- fixed point $%s = F[%s]$

• $\Y \left( %l\left( self \right).F\left( self \right) \right) : %s$

#### Unrolling -- unraveling a type

• $%m %a.F\left[%a\right] = F\left[ %m %a.F\left[%a\right] \right]$

#### Example


$T =$\%m \%a.{ a:int, c:\%a, b:\%a->\%a }
$T_1 = \left\{ a:int, c:T, b:T->T, d:bool \right\}$
$T_2 =$\%m \%a.{ a:int, c:\%a, b:T->T, d:bool }
$T_3 =$\%m \%a.{ a:int, c:\%a, b:\%a->\%a, d:bool }


$T_1, T_2 <= T$, $T_3 \not<= T$ \zline{(contravariance)}

slide: Recursive types -- examples

Finding the fixed point of a specification involves technically a procedure known as unrolling, which allows us to rewrite the type $%m %a.F\left[%a\right]$ as $F\left[%m %a.F\left[%a\right] \right]$. Notice that unrolling is valid, precisely because of the fixed point property. Namely, the object type $%s$ is equal to $%m %a.F\left[%a\right]$, due to the type assignment rule, and we have that $%s = F\left[%s\right]$. See slide 9-ex-recursive. Unrolling allows us to reason on the level of types and to determine the subtyping relation between recursive subtypes. Consider, for example, the type declarations T and $T_i$ $\left(i = 1..3\right)$ above. Based on the refinement rules for object records, functions and recursive types, we may establish that $T_1 <= T$, $T_2 <= T$ but $T_3 \not<= T$. To see that $T_1 <= T$, it suffices to substitute T for $%a$ in F, where $F = \left\{ a:Int, c:%a, b:%a->%a \right\}$. Since $F\left[T\right]= \left\{ a:Int, c:T, b:T->T \right\}$ we immediately see that $T_1$ only extends T with the field $d:Bool$, hence $T_1 <= T$. A similar line of reasoning is involved to determine that $T_2 <= T$, only we need to unroll $T_2$ as well. We must then establish that $c:T_2 <= c:T$, which follows from an application of the refinement rule. To show that $T_3 \not<= T$, let $G\left[%b\right] = \left\{ a:Int, c:%b, b:%b->%b, d:Bool \right\}$ and $T_3 = %m %b.G\left[%b\right]$. Then, by unrolling, $T_3 = G\left[T_3\right] = \left\{ a:Int, c:T_3, b:T_3 -> T_3, d:Bool \right\}$. Now, suppose that $T_3 <= T$, then $G\left[T_3\right] <= F\left[T_3\right]$ and consequently $b:T_3 -> T_3$ must refine $b:T -> T$. But from the latter requirement it follows that $T_3 <= T$ and that $T <= T_3$ (by the contravariance rule for function subtyping). However, this leads to a contradiction since T is clearly not equal to $T_3$ because $T_3$ contains a field $d:Bool$ that does not occur in T. Although analyses of this kind are to some extent satisfactory in themselves, the reader may wonder where this all leads to. In the following we will apply these techniques to show the necessity of dynamic binding and to illustrate that inheritance may easily violate the subtyping requirements.

#### Inheritance

In section flavors we have characterized inheritance as an incremental modification mechanism, which involves a dynamic interpretation of the expression self. In the recursive type calculus $F_\left\{%m\right\}$ we may characterize this more precisely, by regarding a derived object specification C as the result of applying the modifier M to the object specification P. We employ the notation $C = %l\left(self\right).P\left(self\right) \with \left\{ a_1\text{'} = e_1\text{'},...,a_k\text{'} = e_k\text{'} \right\}$ to characterize derivation by inheritance, and we assume the modifier M corresponding with $\left\{ a_1\text{'} = e_1\text{'}, ..., a_k\text{'} = e_k\text{'} \right\}$ to extend the record associated with P in the usual sense. See slide 9-self-inheritance.

#### Inheritance -- C = P + M

• $P = %l\left( self \right).\left\{ a1 = e1,...,a_n = e_n \right\}$
• $C = %l\left( self \right).P\left( self \right)$ $\with$ \ifsli{\n}{} $\left\{ a1\text{'} = e1\text{'},...,a_k\text{'} = e_k\text{'} \right\}$

#### Semantics -- $\Y(C) = \Y(%l( self ).M( self )(P( self )))$

• $P : %s -> %s$ $=>$ $\Y\left(P\right):%s$
• $C = %l\left(s\right).M\left(s\right)\left(P\left(s\right)\right) : %t -> %t$ $=>$ $\Y\left(C\right):%t$

slide: Inheritance semantics -- self-reference

The meaning of an object specification C is again a fixed point $\Y\left(C\right)$, that is $\Y\left(%l\left(self\right).M\left(self\right)\left(P\left(self\right)\right)\right)$. Now when we assume that the object specification is of type $%t -> %t$ (and hence $\Y\left(P\right)$ of type $%t$), and that C is of type $%s -> %s$ (and hence $\Y\left(C\right)$ of type $%s$), then we must require that $%s <= %t$ to obtain a properly typed derivation. We write $C <= P$ whenever $%s <= %t$. A first question that arises when we characterize inheritance as incremental modification is how we obtain the meaning of the composition of two object specifications.

#### Object inheritance -- dynamic binding

$P = %l\left( self \right).\left\{ i = 5, id = self \right\}$
$C = %l\left( self \right).P\left( self \right) \with \left\{ b = true \right\}$
$\Y\left(P\right):%t$ where $%t = %m %a.\left\{ i:int, id:%a \right\}$ and $P:%t->%t$

Simple typing -- $\Y\left(C\right):%s = \left\{ i:int, id:%t, b:bool \right\}$
Delayed -- $\Y\left(C\right):%s\text{'} = %m %a.\left\{ i:int, id:%a, b:bool \right\}$
We have $%s\text{'} <= %s$ \zline{(more information)}

slide: Object inheritance -- dynamic binding

Let (parent) P and (child) C be defined as above. Now, if we know that the type of $\Y\left(P\right)$ is $%t$ then we may simply characterize $\Y\left(C\right)$ as being of type $%s = \left\{ i:Bool, id:%t, b:Bool \right\}$. However, when we delay the typing of the P component (by first composing the record specifications before abstracting from self) then we may obtain $%s\text{'} = %m %a.\left\{ i:Int, id:%a, b:Bool \right\}$ as the type of $\Y\left(C\right)$. By employing the refinement rule and unrolling we can show that $%s\text{'} <= %s$. Hence, delayed typing clearly provides more information and must be considered as the best choice. Note, however, that both $%s\text{'} <= %t$ and $%s <= %t$ hold. See slide 9-self-dynamic. A second, important question that emerges with respect to inheritance is how self-reference affects the subtyping relation between object specifications related by inheritance. Consider the object specifications P and C given in slide 9-self-contravariance. In the (derived) specification C, the method eq is redefined to include an equality test for the b component. However, when we determine the object types corresponding to the specifications P and C we observe that $C \not<= P$.

#### Contravariance

• $P = %l\left( self \right).\left\{ i = 5, eq = %l\left(o\right).\left(o.i = self.i\right) \right\}$


$C =$\%l( self ).P( self )  \with { b = true,
$eq =$\%l(o).(o.i = self.i    and
$o.b = self.b\right)$
$\right\}$


$\Y\left(P\right) : %t$ where $%t = %m %a.\left\{ i:int, eq:%a -> bool \right\}$
$\Y\left(C\right):%s$ where $%s = %m %a.\left\{ i:int, id:%a -> bool, b: bool \right\}$
However $%s \not<= %t$ \zline{(subtyping error)}

slide: Object inheritance -- contravariance

The reasoning is as follows. For $\Y\left(P\right) : %t$ and $\Y\left(C\right) : %s$, we have that $%s = %m %b.\left\{ i:Int, id:%b -> Bool, b:Bool \right\}$ which is (by unrolling) equal to $\left\{ i:Int, id: %s-> Bool, b:Bool \right\}$. Now suppose that $%s <= %t$, then we have that $\left\{ i:Int, eq:%s->Bool, b:Bool \right\}$ is a subtype of $\left\{ i:Int, eq:%t->Bool \right\}$ which is true when $eq:%s->Bool <= eq:%t->Bool$ and hence (by contravariance) when $%s <= %t$. Clearly, this is impossible. Hence $%s \not<= %t$. We have a problem here, since the fact that $C \not<= P$ means that the type checker will not be able to accept the derivation of C from P, although C is clearly dependent on P. The solution to our problem lies in making the type dependency involved in deriving C from P explicit. Notice, in this respect, that in the example above we have omitted the type of the abstraction variable in the definition of eq, which would have to be written as $%l x:\Y\left(P\right).x.i = self.i$ (and in a similar way for C ) to do it properly.

#### Type dependency

The expression self is essentially of a polymorphic nature. To make the dependency of object specification on self explicit, we will employ an explicit type variable similar as in $F_\left\{<=\right\}$. Let $F\left[%a\right]$ stand for $\left\{ a_1 : %t_1,...,a_n:%t \right\}$ as before. We may regard $F\left[%a\right]$ as a type function, in the sense that for some type $%t$ the expression $F\left[%t\right]$ results in a type. To determine the type of an object specification we must find a type $%s$ that satisfies both $%s <= F\left[%s\right]$ and $F\left[%s\right] <= %s$.

#### Type dependency -- is polymorphic

• Let $F\left[%a\right] = \left\{ m_1:%s_1,...,m_j:%s_j \right\}$ \zline{(type function)}
• $P:\A %a <= F\left[%a\right].t -> F\left[%a\right]$
• $P = %L %a <= F\left[%a\right].%l\left( self:%a \right).\left\{ m_1:e1,...,m_j:e_j \right\}$

F-bounded constraint $%a <= F\left[%a\right]$\n Object instantiation: $\Y\left(P\left[%s\right]\right)$ for $%s = %m t.F\left[t\right]$\n We have $P\left[%s\right]:%s -> F\left[%s\right]$ because $F\left[%s\right] = %s$

slide: Bounded type constraints

We may write an object specification as $%L %a <= F\left[%a\right].%l\left(self:%a\right).\left\{ a_1 = e_1,...,a_n$ $= e_n \right\}$, which is typed as $\A %a <= F\left[%a\right]. %a -> F\left[%a\right]$. The constraint that $%a <= F\left[%a\right]$, which is called an F-bounded constraint, requires that the subtype substituted for $%a$ is a (structural) refinement of the record type $F\left[%a\right]$. As before, we have that $\Y\left(P\left[%s\right]\right) = %s$ with $%s = %m %a.F\left[%a\right]$, which differs from our previous definition only by making the type dependency in P explicit. See slide 9-dependency. Now, when applying this extended notion of object specification to the characterization of inheritance, we may relax our requirement that $\Y\left(C\right)$ must be a subtype of $\Y\left(P\right)$ into the requirement that $G\left[%a\right] <= F\left[%a\right]$ for any $%a$, where F is the record specification of P and G the record specification of C.

#### Inheritance


$P =$\%L \%a <= F[\%a].\%l( self: \%a).{ ... }
$C =$\%L \%a <= G[\%a].\%l( self:\%a).P[\%a]( self ) \with { ... }


with recursive types
$F\left[%a\right] = \left\{ i:int, id:%a -> bool \right\}$
$G\left[%a\right] = \left\{ i:int, id:%a -> bool, b : bool \right\}$
Valid, because $G\left[%a\right] <= F\left[%a\right]$
However $\Y\left(C\left[%s\right]\right) \not<=_\left\{subtype\right\} \Y\left(P\left[%t\right]\right)$

slide: Inheritance and constraints

For example, when we declare $F\left[%a\right]$ and $G\left[%a\right]$ as in slide 9-self-constraint, we have that $G\left[%a\right] <= F\left[%a\right]$ for every value for $%a$. However, when we find types $%s$ and $%t$ such that $\Y\left(C\left[%s\right]\right) : %s$ and $\Y\left(P\left[%t\right]\right) : %t$ we (still) have that $%s \not<= %t$. Conclusion, inheritance allows more than subtyping. In other words, our type checker may guard the structural application of inheritance, yet will not guarantee that the resulting object types behaviorally satisfy the subtype relation.

#### Discussion -- Eiffel is not type consistent

We have limited our exploration of the recursive structure of objects to (polymorphic) object variables. Self-reference, however, may also occur to class variables. The interested reader is referred to  [CoHC90]. The question that interests us more at this particular point is what benefits we may have from the techniques employed here and what lessons we may draw from applying them. One lesson, which should not come as a surprise, is that a language may allow us to write programs that are accepted by the compiler yet are behaviorally incorrect. However, if we can determine syntactically that the subtyping relations between classes is violated we may at least expect a warning from the compiler. So one benefit, possibly, is that we may improve our compilers on the basis of the type theory presented in this chapter. Another potential benefit is that we may better understand the trade-offs between the particular forms of polymorphism offered by our language of choice. The analysis given in  [CoHC90] indeed leads to a rather surprising result. Contrary to the claims made by its developer,  [CoHC90] demonstrate that Eiffel is not type consistent. The argument runs as follows. Suppose we define a class C with a method eq that takes an argument of a type similar to the type of the object itself (which may be written in Eiffel as like Current). We further assume that the class P is defined in a similar way, but with an integer field i and a method eq that tests only on i. See slide 9-self-eiffel.

#### Inheritance != subtyping

Eiffel


class C inherit P redefine eq
feature
b : Boolean is true;
eq( other : like Current ) : Boolean is
begin
Result := (other.i = Current.i) and
(other.b = Current.b)
end
end C


slide: Inheritance and subtyping in Eiffel

We may then declare variables v and p of type P. Now suppose that we have an object c of type C, then we may assign c to v and invoke the method eq for v, asking whether p is equal to v, as in


p,v:P, c:C

v:=c;
v.eq(p);   // error p has no b


slide: Example

Since v is associated with an instance of C, but syntactically declared as being of type P, the compiler accepts the call. Nevertheless, when p is associated with an instance of P trouble will arise, since (due to dynamic binding) the method eq defined for C will be invoked while p not necessarily has a field b. When we compare the definition of C in Eiffel with how we may define C in C++, then we are immediately confronted with the restriction that we do not have such a dynamic typing mechanism as like Current in C++. Instead, we may use overloading, as shown in slide 9-self-cc.


class C : public P {    C++
int b;
public:
C() { ... }
bool eq(C& other) { return other.i == i && other.b == b; }
bool eq(P& other) { return other.i == i; }
};


slide: Inheritance and subtyping in C++

When we would have omitted the P variant of eq, the compiler complains about hiding a virtual function. However, the same problem arises when we define eq to be virtual in P, unless we take care to explicitly cast p into either a C or P reference. (Overloading is also used in  [Liskov93] to solve a similar problem.) In the case we choose for a non-virtual definition of eq, it is determined statically which variant is chosen and (obviously) no problem occurs. Considering that determining equality between two objects is somehow orthogonal to the functionality of the object proper, we may perhaps better employ externally defined overloaded functions to express relations between objects. This observation could be an argument to have overloaded functions apart from objects, not as a means to support a hybrid approach but as a means to characterize relations between objects in a type consistent (polymorphic) fashion.

(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.