In the previous section we provided syntax-directed rules for type checking RAJA programs with explicit types that are equivalent to the declarative and non-deterministic typing rules from Chapter 3. These rules will be useful for proving the correctness of the constraint generation rules for type inference that we shall describe in Chapter 5.
Moreover, we have described a deterministic type-checking algorithm that can be implemented for checking correctness of RAJA programs with explicit types, provided that the range of the mapInstis finite and that the set of viewsV is finite and the views are regular.
Definition 4.5.1 (Finite RAJA programs with explicit types) LetR+= (C,main,V,M,Inst,Abody)be a RAJA program with explicit types. We say that R+ is finite if the following conditions are satisfied:
1. V is a finite set of regular views.
4.5.1 Decidability of subtyping
For checking that RAJA programs with explicit types are well-typed, we need to check subtyping between RAJA types and between sets of mono- morphic RAJA method types, both of which can be reduced to checking sub- typing between views, i.e. checkings1 vs2whens1 ands2 are views, which
is defined coinductively. Hence we can implement subtyping between views using an algorithm for computing membership in greatest fixpoints. Con- cretely, we use an algorithm defined and proved correct in [Pie02, Ch. 21], which works for coinductive definitions that fall into a specific scheme, i.e. a goal issupported by a set of sub-goals in a deterministic way. In our case, a goals1 vs2 is supported by the set of sub-goalsAget(Cs1, a)vAget(Cs2, a)
andAset(Cs2, a)vAset(Cs1, a) for each classC ∈C and a∈A(C).
The idea of the algorithm is to maintain a list of assumptions. Every goal is kept in this list, unless some condition is not fulfilled. The condition in our case is ♦(Cs1) ≥ ♦(Cs2) for each class C. Then, the algorithm is
called recursively with all the sub-goals. If a given sub-goal is an element of the list of assumptions (which means it has been a goal before) then we conclude that the sub-goal is in the coinductive defined relation.
As described in [Pie02, Ch. 21], the algorithm terminates if the set of reachable states from a given goal (a pair of views) is finite. Since views are infinite trees, the set of reachable states can be infinite. Hence, we must ensure that we check subtyping only between views for which the set of reachable states is finite and this is the case for regular views.
This is the reason why we require the views that appear in a finite RAJA program to be regular. However, in the syntax-directed typing rules from Section 4.4.1, we use not only views from the given setV, but also computed views like s1⊕s2 or nC. Fortunately, these computed views are regular as
well, as the following fact shows.
Fact 4.5.2 Lets, s1, s2 ∈VC andn∈DandC∈C and let∗ ∈ {⊕,,∧,∨}. Then:
1. nC andn−C are regular. 2. (s− n)
C is regular, if sis regular.
3. s1∗s2 is regular, if s1 ands2 are regular.
4.5.2 Efficiency of typing
In the following we prove that given a finite RAJA program with explicit typesR+, it can be decided in polynomial time whetherR+ is well-typed.
Lemma 4.5.3 (Efficiency of syntax-directed typing )
Γ nn0 e◦⇔Cr is decidable in polynomial time.
Proof sketch. The backwards application of the syntax-directed typing rules produces a linear number of subtyping constraints. Furthermore, the algorithmic view expressions occurring in these constraints are themselves of linear size. It then suffices to restrict attention to the views that occur as sub-expressions of the ones appearing in the constraints. Their number is therefore polynomial in the size of the program. A complete table of the subtyping judgements for this relevant subset can then be computed iteratively in polynomial time. In practice, a goal-directed implementation
performs even better. 2
Theorem 4.5.4 (Efficiency of typing finite RAJA programs) Given a finite RAJA programR+= (C,main,V,M,Inst,Abody)with explicit types, its well-typedness is decidable in polynomial time.
Proof. Let C ∈ C and m ∈ Meth(C). Then `a m : Inst(C, m) ok is
decidable in polynomial time because
this:Cs0, x
1:E1s1, . . . , xn:Ensn u
u0 Mbody(C, m)◦ ⇔Hrn+1
is decidable in polynomial time by Lemma 4.5.3. Let moreover S(C) = D. ThenInst(C, m)<:Inst(D, m) is decidable in polynomial time, as discussed
in Section 4.5.1. 2
In [HR09] we provided a similar type checking algorithm for RAJA pro- grams and we also gave an implementation. The main difference between that system and the system presented here is that here we require more annotations and the rules are considerably simpler. The reason for this is that, when we described the system in [HR09], we had not developed the type inference algorithm yet, and thus we wanted to reduce the amount of annotations that a programmer had to write to a minimum. Here we have more annotations because we also have a type inference algorithm that will provide them, that we shall describe in the next chapter.
Chapter 5
Type Inference for RAJA
5.1
Overview
In this chapter we present an algorithm that, when given an FJEU program P = (C,main), generates a RAJA program based onPby giving polymorphic RAJA method types to the methods of P. Moreover, we give an algorithm for generating a monomorphic RAJA method type for the main method of P, by solving the constraints of main’s polymorphic type. The heap-space consumption of P follows from the potential given to main’s arguments by that type, as discussed in Chapter 3.
We are only able to analyse a subset of the programs that are typeable in the RAJA system. We have the following restrictions:
First, we do not analyse programs with polymorphic recursion; we anal- yse programs that are typeable in a subset of RAJA that allows only mono- morphic recursion, called RAJAm.
Second, the algorithm for solving subtyping constraints that we describe in Chapter 6, is capable of solving only a subset of the constraints that arise during the analysis, i.e. a subset of the constraints that correspond to programs whose heap-space consumption can be described as a linear function. Finding an algorithm for solving all the subtyping constraints remains an open problem for future work.
When we are able to obtain a well-typed RAJA program for P, we can also extend it to a finite RAJA program with explicit types, that can be type checked with the efficient algorithm described in Chapter 4.
This chapter is organised as follows. In Section 5.2 we describe the sys- tem RAJAm. Then, in Section 5.3 we present an algorithm for generating RAJAm programs. Section 5.4 discusses the algorithm for constraint solv- ing, which is described in Chapter 6. Finally, Section 5.5 describes how we can build a finite RAJA program with explicit types.
Notation. In this chapter we use the notation C.m for referring to the method m of class C, when we wish to distinguish it from the method of the same name in another class.