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.

(reduce1)<br />

Eq ∪ { C ⋖ D }<br />

Eq ∪ { θ π( 1 )<br />

. = θ<br />

′<br />

1 , . . . , θ π( n )<br />

. = θ<br />

′<br />

n }<br />

where<br />

• C ≤ ∗ D<br />

• { a 1, . . . , a n } ⊆ T V<br />

• π is a permutati<strong>on</strong><br />

(reduce2)<br />

(erase)<br />

(swap)<br />

Eq ∪ { C . = C }<br />

Eq ∪ { θ 1<br />

. = θ<br />

′<br />

1 , . . . , θ n<br />

. = θ<br />

′<br />

n }<br />

Eq ∪ { θ . = θ ′ }<br />

Eq<br />

Eq ∪ { θ . = a }<br />

Eq ∪ { a . = θ }<br />

θ = θ ′<br />

θ ∉ T V, a ∈ T V<br />

(adapt)<br />

Eq ∪ { D ⋖ D ′ }<br />

Eq ∪ { D ′ [a i ↦→ θ i | 1in] ⋖ D ′ }<br />

where there are θ ′ 1, . . . , θ ′ m with<br />

• (D ≤ ∗ D ′ ) ∈ FC( ≤ )<br />

(subst)<br />

Eq ∪ { a . = θ }<br />

Eq[a ↦→ θ] ∪ { a . = θ }<br />

where<br />

• a occurs in Eq but not in θ<br />

Figure 2: Java 5.0 type unificati<strong>on</strong><br />

The following type term pairs are elements of the subtyping<br />

relati<strong>on</strong> ≤ ∗ :<br />

Vector ≤ ∗ Vector,<br />

Vector ≤ ∗ AbstractList,<br />

Matrix ≤ ∗ Vector,<br />

Matrix ≤ ∗ AbstractList.<br />

But Vector ≰ ∗ Vector which<br />

follows from the soundness of the Java 5.0 type system.<br />

The finite closure FC( ≤ ) is given as the reflexive <strong>and</strong><br />

transitive closure of { Matrix ≤ ∗ Vector<br />

≤ ∗ AbstractList ≤ ∗ List }.<br />

We complete the Java 5.0 type system with the following<br />

definiti<strong>on</strong>.<br />

Definiti<strong>on</strong> 4. Let SType Θ( BT V ) be a set of simple types<br />

of given Java 5.0 classes. The respective set of Java 5.0 types<br />

Type( SType Θ( BT V ) ) is the smallest set with the following<br />

properties:<br />

1. For the c<strong>on</strong>sidered base types holds<br />

{ Integer, Boolean, Char } ⊆ Type( SType Θ( BT V ) ).<br />

2. SType Θ( BT V ) ⊆ Type( SType Θ( BT V ) )<br />

(simple type).<br />

3. If for 0in: θ i ∈ (SType Θ( BT V ) ∪ basetype) then<br />

θ 1 × . . . × θ n → θ 0 ∈ Type( SType Θ( BT V ) ) (functi<strong>on</strong><br />

type).<br />

4. If ty 1, ty 2 ∈ Type( SType Θ( BT V ) ), then ty 1&ty 2 ∈<br />

Type( SType Θ( BT V ) ) (intersecti<strong>on</strong> type).<br />

The base types <strong>and</strong> the simple types describe types of<br />

fields, types of methods’ parameters, return types of methods,<br />

<strong>and</strong> types of local variables. Finally the types of the<br />

methods are given as intersecti<strong>on</strong>s of functi<strong>on</strong> types. The<br />

intersecti<strong>on</strong>s are necessary to describe the principle type of<br />

a method. If a method has an intersecti<strong>on</strong> type, this means<br />

that more than <strong>on</strong>e type is inferable for the code.<br />

We do not c<strong>on</strong>sider raw types as they are <strong>on</strong>ly necessary<br />

to use older Java programs (Versi<strong>on</strong> ≤ 1.4). The behavior<br />

of raw types can be simulated by using the corresp<strong>on</strong>ding<br />

parameterized types, where all arguments are instantiated<br />

by Object.<br />

3. TYPE UNIFICATION<br />

The basis of the type inference algorithm is the type unificati<strong>on</strong>.<br />

The type unificati<strong>on</strong> problem is given as: For two<br />

type terms θ 1, θ 2 a substituti<strong>on</strong> is dem<strong>and</strong>ed, such that<br />

σ( θ 1 ) ≤ ∗ σ( θ 2 ).<br />

σ is called a unifier of θ 1 <strong>and</strong> θ 2. In the following we denote<br />

θ ⋖ θ ′ for two type terms, which should be type unified.<br />

Our type unificati<strong>on</strong> algorithm is based <strong>on</strong> the unificati<strong>on</strong><br />

algorithm of Martelli <strong>and</strong> M<strong>on</strong>tanari [13]. The main<br />

difference is, that in the original unificati<strong>on</strong> a unifier is dem<strong>and</strong>ed,<br />

such that σ( θ 1 ) = σ( θ 2 ). This means that a pair<br />

a = . θ determines that the unifier substitutes a by the term<br />

θ. In c<strong>on</strong>trast a pair a ⋖ θ respectively θ ⋖ a leads to multiple<br />

correct substituti<strong>on</strong>s. All type terms smaller than θ <strong>and</strong><br />

greater than θ respectively are correct substituti<strong>on</strong>s for a.<br />

This means that there are multiple unifiers.<br />

Now, we give the type unificati<strong>on</strong> algorithm. We restrict<br />

the type terms to terms without bounded type variables<br />

<strong>and</strong> without wildcards. We denoted this subset of<br />

SType Θ( BT V ) in definiti<strong>on</strong> 1 by T Θ( T V ).<br />

The algorithm itself is given in seven steps:<br />

1. For each pair a⋖θ a set of pairs is built, which c<strong>on</strong>tains<br />

for all substypes θ of θ the pair a . = θ.<br />

2. For each pair θ⋖a a set of pairs is built, which c<strong>on</strong>tains<br />

for all supertypes θ ′ of θ the pair a . = θ ′ .<br />

3. The cartesian product of the sets from step 1 <strong>and</strong> 2 is<br />

built.<br />

4. Repeated applicati<strong>on</strong> of the rules reduce1, reduce2, erase,<br />

swap, <strong>and</strong> adapt (fig. 2) to each set of type term pairs.<br />

5. Applicati<strong>on</strong> of the rule subst (fig. 2) to each set of type<br />

term pairs.<br />

6. For all changed sets of type terms start again with step<br />

1.<br />

7. Summerize all results<br />

For more details about the Java 5.0 type unificati<strong>on</strong> see<br />

in [17].<br />

Example 2. Let the subtyping relati<strong>on</strong> <strong>and</strong> the finite closure<br />

be given as in example 1.<br />

177

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

Saved successfully!

Ooh no, something went wrong!