• No results found

Inference algorithms

2.2 Type and effect systems

2.2.8 Inference algorithms

Tables 2.3 and 2.4 present the type and effect analysis as proof systems that require guessing suitable types for sub-expressions; to obtain an automatic analysis we need an algorithm fortype and effect reconstruction.

A first problem is the presence of non-structural type rules such as those for subeffecting (2.14), subtyping, generalisation (2.16) and instantiation (2.17), (2.18): these rules can occur in arbitrary points of a derivation and therefore some “canoni- cal” choice has to be made; this is usually done by establishing aproof normalisation result, i.e. that uses of non-structural rules can be restricted to specific syntax points without incurring a loss of typeability.

Let-bound polymorphism is a suitable choice for normalising the uses of generali- sation and instantiation: generalise all suitable type and effect variables of let-bound identifiers2 and instantiate all quantified variables just after the use of a variable.

Subeffecting can be normalised by allowing over-approximation of effects in all rules, i.e. by adding an arbitrary effect. . .∪ϕ0 to the conclusions of (2.7), (2.8), (2.9) and (2.12).

Type inference for type and effect systems with subeffecting but not subtyping can be implemented as an extension of the well-known algorithmW of Damas (1985). The key insight is to restrict types to the subset ofsimple types bτwhose annotations must be variables:

b

τ ::= α | int | bool | bτ1

β −→τb2

To allow expressing complex effects (i.e. non-variables) the algorithm collects sepa- ratelower-bound constraints C over effect variables:

C ::= ∅ | {β ⊇ϕ} | C1∪C2

ϕ ::= ∅ | β | {} | ϕ1∪ϕ2

The reason for restricting the algorithm to simple types is that these form a free algebra in which equality constraints can be solved by first-order unification (Robin- son 1971) just as in ordinary Damas-Milner type inference. By contrast, the algebra

U(int,int) =id U(bool,bool) =id U(bτ1 β −→τb2,bτ 0 1 β0 −→bτ20) = let θ0= [β7→β0] θ1=U(θ0bτ1, θ0τb 0 1) θ2=U(θ1θ0bτ2, θ1θ0bτ 0 2) in θ2◦θ1◦θ0 U(α,bτ) =U(τ , αb ) = (

[α7→bτ] ifαdoes not occur inτb

fails otherwise U(bτ ,bτ0) fails in all other cases

Table 2.5: Unification of simple types.

of effects is non-free (e.g.∪is associative, commutative and has a empty element∅). By segregating effects to separate constraints, it becomes possible to use the simple unification to solve type equalities and deal with the non-free algebra of effects in a separate constraint solver.

Table 2.6 presents an excerpt of the reconstruction algorithm as judgements

b

Γ`RAe: (bτ , ϕ, C, θ)

where bΓ is a set of (simple) type assumptions, eis an expression and the output is

a 4-tuple of: a simple type bτ, an effect ϕ, a set of lower-bound constraints C and a substitution θ. For simplicity, we include only the rules for constants, abstrac- tion and application; the omitted cases (conditionals and exception handling) are straightforward but tedious.

The main difference between the proof systems of Table 2.3 and Table 2.6 is that the latter does not require guessing types of sub-expressions; instead, it uses “fresh” variables for both types and effects and uses unification to impose equality constraints between (simple) types.

The unification algorithmU in Table 2.5 takes two simple typesτ ,b τb0 and yields the “smallest” substitution θ such that θbτ ≡ θbτ0 (or fails, if no such substitution exists). Note that substitutions bind both type and effect variables and therefore are applied to types, effects and constraints.

Each rule of Table 2.6 is applicable to a single expression syntax node; thus, the rules can be read as an algorithm for reconstructing the type and effect of an

b Γ`RAc: (τc,∅,∅,id) b Γ∪ {x:τb} `RAx: (bτ ,∅, ∅,id) b Γ`RAraise: (α,{},∅,id) b Γ∪ {x:α} `RAe: (τ , ϕ, C, θb ) b Γ`RAλx. e: (θα β −→τ ,b ∅,{β⊇ϕ} ∪C, θ)

α, β are fresh variables

b Γ`RAe1: (bτ1, ϕ1, C1, θ1) θ1bΓ`RAe2: (τb2, ϕ2, C2, θ2) θ3=U(bτ2 β −→α, θ2bτ1) b Γ`RA(e1e2) : (θ3α, θ3θ2ϕ1∪θ3ϕ2∪ {θ3β}, θ3θ2C1∪θ3C2, θ3◦θ2◦θ1)

Table 2.6: Algorithmic typing judgements for exception analysis (excerpt).

expression.

Extending the inference algorithm with let-bound polymorphism is straightfor- ward: quantification of variables is handled at theletand instantiation is handled at the use of variables by introducing fresh type and effect variables. The type and effect system for region inference of Talpin and Jouvelot (1992) combines polymorphism and effects (but not subtyping).

Type reconstruction algorithms for subtyping usually require extending the proof system with explicit type inequality constraints (Mitchell 1984, Fuh and Mishra 1988); this is needed to obtain syntactic completeness, i.e. an algorithm that com- putes a principal solution from which any valid typing can be derived. This approach is followed in Nielson et al. (1996a,b) although completeness of the algorithm is left as an open problem. For shape conformant subtyping typical of type and effect sys- tems it is possible to employ a simpler two-stage approach: first the underlying types are inferred and then the subtyping inequalities are translated to constraints on the annotations (Reistad and Gifford 1994); such an algorithm will not be complete, i.e. it may compute a type and effect that is not minimal.