13.07.2015 Views

A Graph-Based Generic Type System for Object-Oriented Programs

A Graph-Based Generic Type System for Object-Oriented Programs

A Graph-Based Generic Type System for Object-Oriented Programs

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Type</strong> <strong>System</strong> 39Proof. The theorem is proven by replacing all the type nodes with their corresponding instantiationnodes in the type-checking rules <strong>for</strong> expressions and commands, and then applying Lemma 4,5, 6, 8, 9 and 10. □5.10 <strong>Type</strong>-Checking of <strong>Programs</strong><strong>Based</strong> on the type-checking of commands, we are able to check the well-typedness of a program.A program is well-typed if each method, including the Main method, defined in the program iswell-typed, and a method definition is well-typed if its body command is well-typed accordingto its <strong>for</strong>mal parameters.In the type context of the checking of a method body command, the self reference points tothe class defining the method. If the class is generic, self must point to an instantiation of thegeneric class over an identity type substitution, i.e., a shallow structure of the generic class. Wedefine a function self (Γ, t) to return the shallow structure when needed. Formally,{shlo(Γ, t) if t-var(Γ, t) ≠ ∅,self (Γ, t) ̂=(Γ, t) otherwise.We start by creating the empty type context with only a root node,∆ ∅ = 〈{r}, ∅, r〉, where r ∈ O.For a method definition, we push the method frame onto ∆ ∅ , so that the result type context ∆ ′ ∅contains all the parameters and their types. We check whether the body command is well-typedunder ∆ ′ ∅. The following lemma ensures that if a parameter can be found in the method framepushed onto the empty type context, it can also be found in the method frame pushed on anytype context. Thus a well-typed method can always be invoked.Lemma 11 Given a type graph Γ, a type context ∆ and a command c, let∆ ′ ∅ = push(∆ ∅, x, n) and ∆ ′ = push(∆, x, n),if Γ, ∆ ′ ∅ ⊢ c, then Γ, ∆′ ⊢ c and <strong>for</strong> all variables y occurred in c and all ∆ 1 → ∆ 2 occurred in thededuction of Γ, ∆ ′ ⊢ c such that search(∆ 1 , y) /∈ ∆.Proof. We first show that if a variable is well-typed in ∆ ′ ∅ , it is also well-typed in ∆′ and thevariable cannot be in ∆. Then, we prove that the lemma holds <strong>for</strong> a general command byinduction on the structure of commands and expressions.If a variable y in c is well-typed, by (T-Var), y is found by search, hence, y is either introducedby c above the stack of ∆ ′ ∅ , or in ∆′ ∅ . If y is in ∆′ ∅ , it must be in x, because ∆ ∅ contains no edge.So the edge <strong>for</strong> y is either in ∆ ′ or introduced in command c, but not in ∆.□Report No. 448, June 2011UNU-IIST, P.O. Box 3058, Macao

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

Saved successfully!

Ooh no, something went wrong!