15.01.2015 Views

4th International Conference on Principles and Practices ... - MADOC

4th International Conference on Principles and Practices ... - MADOC

4th International Conference on Principles and Practices ... - MADOC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The idea of Java 5.0 type inference is to omit these type<br />

annotati<strong>on</strong>s. The system automatically calculates the principle<br />

intersecti<strong>on</strong> type of the methods.<br />

The type inference discipline arose in functi<strong>on</strong>al programming<br />

languages (e.g. Haskell [10] or SML [14]). A basic paper<br />

for type inference including an algorithm <strong>and</strong> the definiti<strong>on</strong><br />

of principle types is given by [3]. A method’s principle type<br />

is a type, where all correct types of the method are derivable<br />

from. In secti<strong>on</strong> 4.3 we give a detailed definiti<strong>on</strong>.<br />

Many papers <strong>on</strong> type inference have been published. Some<br />

papers c<strong>on</strong>sider type inference in object–oriented languages.<br />

In [15, 11] type inference systems for a language oriented<br />

at Smalltalk [8] are given. In this approach the idea is to<br />

collect type c<strong>on</strong>straints, build a uniform set, <strong>and</strong> solve them<br />

by a fixed point derivati<strong>on</strong>. In [5] similar as in [1] types are<br />

defined as a pair of a c<strong>on</strong>venti<strong>on</strong>al type <strong>and</strong> a set of type<br />

c<strong>on</strong>straints. Finally, the c<strong>on</strong>straints are satisfied, if possible.<br />

These type systems differ from the Java 5.0 type system.<br />

In [4] <strong>and</strong> [6] a refactoring is presented that replaces raw<br />

references to generic library classes with parametrized references.<br />

For this the type parameters of the raw type annotati<strong>on</strong>s<br />

must be inferred. The main difference to our approach<br />

is, that in our approach no type annotati<strong>on</strong>, must be given<br />

<strong>and</strong> the algorithm infers the whole type.<br />

The type system of polymorphically order-sorted types,<br />

which is c<strong>on</strong>sidered for a logical language [18] <strong>and</strong> for a functi<strong>on</strong>al<br />

object–oriented language OBJ–P [16], is very similar<br />

to the Java 5.0 type system. Therefore our approach is oriented<br />

at polymorphically order-sorted types.<br />

The paper is organized as follows. In the sec<strong>on</strong>d secti<strong>on</strong><br />

we formally describe the Java 5.0 type system. In the third<br />

secti<strong>on</strong>, we give a unificati<strong>on</strong> algorithm for a subset of the<br />

Java 5.0 types. The unificati<strong>on</strong> algorithm is the base of the<br />

type inference algorithm. In the fourth secti<strong>on</strong> we present<br />

the type inference algorithm itself, illustrate the algorithm<br />

by an example, <strong>and</strong> c<strong>on</strong>sider the properties of the algorithm.<br />

Finally in the sixth secti<strong>on</strong> we present the implementati<strong>on</strong>.<br />

We have d<strong>on</strong>e the implementati<strong>on</strong> in Java using the observer<br />

design pattern. We close the paper with a c<strong>on</strong>clusi<strong>on</strong> <strong>and</strong><br />

an outlook.<br />

2. JAVA 5.0 TYPE SYSTEM<br />

As a base for the type inference algorithm we have to<br />

give a formal definiti<strong>on</strong> of the Java 5.0 type system. First<br />

we introduce the simple types. Fields, method parameters,<br />

return types of methods, <strong>and</strong> local variables are annotated<br />

by simple types. Then, from the extends relati<strong>on</strong> we derive<br />

the subtyping relati<strong>on</strong>. Furthermore, we define a relati<strong>on</strong> finite<br />

closure, which is necessary for the unificati<strong>on</strong> algorithm<br />

(secti<strong>on</strong> 3). Finally, we introduce functi<strong>on</strong> types, which represent<br />

the types of methods.<br />

The simple types are built as a set of terms over a finite<br />

rank alphabet of the class/interface names, a set of<br />

unbounded respectively bounded type variables, <strong>and</strong> unbounded<br />

respectively bounded wildcards.<br />

Definiti<strong>on</strong> 1. Let JC be a set of declared Java 5.0 classes<br />

<strong>and</strong> Θ = (Θ (n) ) n∈N the finite rank alphabet of class/interface<br />

names indexed by its respective number of parameters. Let<br />

T V be a set of unbounded type variables. Furthermore, let<br />

T Θ( T V ) be the set of (type) terms over Θ <strong>and</strong> T V .<br />

Then, the set of simple types SType Θ( BT V ) is the smallest<br />

set satisfying the following c<strong>on</strong>diti<strong>on</strong>s:<br />

• T Θ( T V ) ⊆ SType Θ( BT V )<br />

• For each intersecti<strong>on</strong> of simple types ty = ty 1& . . . &ty n<br />

BT V (ty) ⊆ SType Θ( BT V )<br />

where BT V (ty) c<strong>on</strong>tains all type variables, bounded<br />

by ty. A bounded type variable is denoted by a| ty.<br />

• For θ i ∈ SType Θ( BT V )<br />

∪ { }<br />

∪ { extends τ | τ ∈ SType Θ( BT V ) }<br />

∪ { super τ | τ ∈ SType Θ( BT V ) }<br />

<strong>and</strong> C ∈ Θ (n) : C ∈ SType Θ( BT V ).<br />

The inheritance hierarchy c<strong>on</strong>sists of two different relati<strong>on</strong>s:<br />

The “extends relati<strong>on</strong>” (in sign

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!