Inferring Intersection Typings that are Equivalent to Call-by-Name and Call-by-Value Evaluations

We present a procedure to infer a typing for an arbitrary -term M in an intersection-type system that translates into exactly the call-by-name (resp., call-by-value) evaluation of M. Our framework is the recently developed System E which augments intersection types with expansion variables. The inferred typing for M is obtained by setting up a uni cation problem involving both type variables and expansion variables, which we solve with a con uent rewrite system. The inference procedure is compositional in the sense that typings for di erent program components can be inferred in any order, and without knowledge of the de nition of other program components. Using expansion variables lets us achieve a compositional inference procedure easily. Termination of the procedure is generally undecidable. The procedure terminates and returns a typing i the input M is normalizing according to call-by-name (resp., call-by-value). The inferred typing is exact in the sense that the exact call-by-name (resp., call-by-value) behaviour of M can be obtained by a (polynomial) transformation of the typing. The inferred typing is also principal in the sense that any other typing that translates the callby-name (resp., call-by-value) evaluation of M can be obtained from the inferred typing for M using a substitution-based transformation. 1 Background and Motivation 1.1 Exact Intersection Typings This paper is about a method for typing inference centering around the following relationships: program result typing evaluation analysis abstraction ? Work partly funded by NSF grant CCR-0113193 Implementing Modular Program Analysis via Intersection and Union Types. 3 Type inference, let alone typing inference (which is a stronger notion), in Standard ML is not compositional: for a program let x = M in N, the type-checker rst infers a type for M, based on which it infers a type for N, then a type for the whole program. Parallel separate analysis, i.e., typing inference, for M and N is impossible.ion ? Work partly funded by NSF grant CCR-0113193 Implementing Modular Program Analysis via Intersection and Union Types. 3 Type inference, let alone typing inference (which is a stronger notion), in Standard ML is not compositional: for a program let x = M in N, the type-checker rst infers a type for M, based on which it infers a type for N, then a type for the whole program. Parallel separate analysis, i.e., typing inference, for M and N is impossible. A typing combines a result type with a type environment to specify logical restrictions on the input variables, output and free variables of the program. Typings viewed as program abstractions have many uses in program validation and compilation. For example, \non-standard" type systems derive typings that support compiler optimisations such as dead-code analysis (by detecting parts that will de nitely not be evaluated) and as strictness analysis (by detecting parts that will de nitely be evaluated). The accuracy of these typing analyses, and hence the accuracy of the validation or eAEcacy of the optimisation, depends on the formalism of types and the capabilities of its inference procedures. Another important factor is the ability of a type system to model the behaviour of the program evaluation strategy. Intersection type systems (since [10, 4]) o er polymorphism by listing all the usage types of subterms. (Rather than abstracting over di erent types as in quanti er-based type systems). This can be interpreted to give precisely the sort of usage information sought by the aforementioned analyses. They can also assign types to programs beyond the reach of the most powerful quanti er-based systems such as System F! Systems of quanti er-based polymorphic types (e.g. Hindley-Milner and System F) by contrast lose the information needed to make ne distinctions between the di erent ways subprograms are used. Many existing intersection type inference techniques only support analysis to a modest (albeit useful) degree of accuracy. A typical restriction is the rank-2 types, e.g., [12] for strictness and [11] for deadcode (the rank hierarchy strati es types according to the depth of nesting of the intersection constructor). Higher rank types indicate \higher order sharing", so features of programs with a more sophisticated structure are undetectable to many analyses. It is the sheer diAEculty of de ning simple and highly accurate procedures for intersection typing analysis has held back the use of intersection typings in practical systems. We present a typing inference procedure that provides a perfectly accurate (\exact") analysis of evaluation behaviour | the abstraction arrow in our program analysis triangle is really a \lossless" translation. Naturally, program behaviour depends on the evaluation strategy; here, programs are terms of the calculus and we infer typings for their behaviour under the call-by-name (CBN hereafter) and call-by-value (CBV hereafter) evaluation strategies. Our typings are expressed in a system of intersection types, augmented with expansion variables. We claim that our approach is straightforward, and show that it enjoys many useful properties. 1.2 Running Example To illustrate the bene ts of intersection typings, the di erences between exact analyses for di erent strategies, and compositional analysis, we consider the following -terms (the visible application constructor @ aids clarity and graphical presentation). Ma = ( x: x@ x) @Mb Mb = ( y: z:Vc) @ Vd The two unspeci ed subterms Vc and Vd could be any values not containing y or z as free variables. Neither CBN nor CBV evaluate subterms inside Vc