• No results found

From a type constraint to a TypeFocus

In document Decrypting Local Type Inference (Page 100-106)

3.7 On understanding the type variable instantiation

3.7.1 From a type constraint to a TypeFocus

The type variables instantiations are inferred from the collected type constraints (as explained in Section 2.1.5). The type constraints lead to a-constraint sets that define lower and upper type bounds for each of the type variables in a. The a-constraint set does not carry informa- tion about the origin of type bounds, nor is it suitable for locating them due to the implicit approximation of type constraints. The key idea that links type constraints to our thesis is that individual type bounds can be represented as type selections on types that participate in the subtyping derivation.

We make use of the fact that all type constraints that are added to the a-constraint set are of the form{A<:B}, where A and B are just part of the same subtyping check S <:T and either A∈ a or B ∈ a. Therefore a TypeFocus, say Θ, that represents some type constraint from the subtype derivation extracts eitherΘ(S) =inlA ∧ Θ(T ) =inlB orΘ(S) =inlB ∧ Θ(T ) =

inlA.

The{A<:B}form of the collected type constraint implies that only a lower or upper type

3.7. On understanding the type variable instantiation

bound of a single type variable can be represented by a single TypeFocus instance at the same time. To distinguish between them, we will use the variance informationψ±, defined as

ψ± ::= +|- (variance information)

We define a TypeFocus-generation judgment of the form

a, ψ±g en S<:T  Θ (TypeFocus-generation)

to infer a sequence of TypeFocus instances from the subtyping derivation between two types S and T , where either fv(S)=  orfv(T )= . We use a Θ notation that is equivalent to Θ =Θ1, ...,Θnfor n≥ 0. The Θ sequence represents individual lower (if ψ

±=+), or upper (ifψ±=-) type bounds for some type variable a. Such definition means thatΘ, where Θ ∈ Θ, is equivalent to a single type constraint for some type variable a such that the type of the constraints is eitherΘ(S)tpeorΘ(T )tpe.

Example: Representation of simple type constraints

We consider an example of an { a }-constraint set, C1, generated from the type of the argu-

ment ((A→ B) → A) and the type of the parameter ((A → a) → A) of some function applica- tion:

{ a }(A→ B) → A <: (A → a) → A ⇒ C1, where{<:a <:B}∈ C1

The sequence of TypeFocus instances inferred for the identical subtyping derivation would therefore result in:

a,+g en (A→ B) → A<:(A→ a) → A   a,-g en (A→ B) → A<:(A→ a) → A 



[φfun-param,φfun-res] 

The type extracted using the generated sequence agrees with the corresponding upper type bound of the type variable a from the C1 constraint set, since ([φfun-param,φfun-res])((A→

B )→ A) =inlB . At the same time the empty sequence of TypeFocus instances inferred for the lower type bound of the type variable a corresponds to the implicitly added type⊥. The judgment is correct in a sense that it does not generate a TypeFocus instance for implicitly added⊥ and  type bounds.

The sequence of TypeFocus instances resulting from the judgment is oblivious to any approx- imations that take place in the regular constraint generation process. As we show in the next

Chapter 3. Guided-analysis for type derivation trees

example, the approximated type bounds from the inferred constraint sets can always be re- covered thanks to the variance information.

Example: Representation of approximated type constraints

We let C2represent the { a, b }-constraint set generated from the type of the argument and the

type of the parameter of some function application:

{ a,b }{x : a→ b, y : a → Int} <: {x : (Int → Int) → Int, y : (⊥ → ⊥) → Int} ⇒ C2 ,

where{⊥ → Int<:a<:, ⊥<:b<:I nt}⊆ C2

The sequence of TypeFocus instances inferred for the identical subtyping derivation for each of the type variables would therefore result in:

a, +g en {x : a→ b, y : a → Int}<:{x : (I nt→ Int) → Int, y : (⊥ → ⊥) → Int} 

[φselx,φfun-param], [φsely,φfun-param]



a, -g en {x : a→ b, y : a → Int}<:{x : (I nt→ Int) → Int, y : (⊥ → ⊥) → Int}   b,+g en {x : a→ b, y : a → Int}<:{x : (I nt→ Int) → Int, y : (⊥ → ⊥) → Int}   b,-g en {x : a→ b, y : a → Int}<:{x : (I nt→ Int) → Int, y : (⊥ → ⊥) → Int} 



[φselx,φfun-res]



The translation is not one-to-one equivalent, as in the previous example, because for the lower type bound of the type variable a it infers two TypeFocus instances, corresponding to the two individual lower type bounds; we can always manually calculate the least upper bound of the extracted types to reflect the approximated type bounds of the inferred con- straint sets:

[φselx,φfun-param]({x : (I nt→ Int) → Int, y : (⊥ → ⊥) → Int})tpe

[φsely,φfun-param]({x : (I nt→ Int) → Int, y : (⊥ → ⊥) → Int})tpe= I nt→ Int ∨ ⊥ → ⊥ = ⊥ → Int

The semantics of the TypeFocus translation - formally

Theg en judgment is realized through a set of algorithmicΘGrules, defined in Figure 3.22.

TheΘGrules mimic the constraint generationCGrules defined in Pierce and Turner [2000] which in turn realize the (a S <: T ⇒ C) constraint generation judgment. The ΘG rules

recursively construct TypeFocus instances based on the shape of the subtyping derivation and the kind of type bounds considered. For clarity, the definition uses aΘX::Θnotation to abbreviateΘX::Θ1, ... ,ΘX::Θn, whereΘi∈ Θ and 1 ≤ i ≤ n. If Θ = , thenΘX::Θ is equivalent to an empty sequence.

TheΘG algorithm defines four base rules: ΘG(-, <),ΘG(+, <),ΘG(-, >) andΘG(+, >). We recall

that theCGconstraint generation algorithm defines only two rules that generate the base type

3.7. On understanding the type variable instantiation ΘG(+, <) a,+,Wg en a<:T   ΘG(-, <) a,-,Wg en a<:T  { [ ] } ΘG(+, >) a,+,Wg en T <:a  { [ ] } ΘG(-, >) a,-,Wg en T <:a   ΘG(TOP) S ∈ { a,⊥ } a,ψ±,Wg en S<:   Θ G(BOT) S ∈ { a, } a,ψ±,Wg en <:S   ΘG()a, ψa ∈ (fv(S)∪fv(T )) ±,Wg en S<:T   ΘG(FUN)

a ∈ b a ∈ (fv(∀a.R → S) ∪fv(∀a.T → U))

a,ψ±, W∪ b g en T<:R  Θ a, ψ±, W∪ b g en S<:U  Θ a,ψ±, Wg en ∀b.R → S<:∀b.T → U   φfun-param::Θ  φfun-res::Θ  ΘG(REC) a∈ (fv(S1)∪ ... ∪fv(Sn)fv(T1)∪ ... ∪fv(Tn)) a,ψ±, Wg en S1<:T1  Θ 1 ... a,ψ±, Wg en Sm<:Tm  Θ m a,ψ±, Wg en {x1: S1, ..., xm: Sm, ..., x n: Sn}<:{x1: T1, ..., xm: Tm}  φselx1 ::Θ 1 ∪ ... ∪φselxm ::Θ m

Figure 3.22: Algorithmic rulesΘGthat define the (a,ψ±,Wg en S<:T  Θ) judgment.

The rules mimic the corresponding constraint generation algorithmCGdefined in Pierce and Turner [2000]. The implicit W variable set keeps track of bounded out-of-scope type variables.

constraints: (CG-Lower) and (CG-Upper).

The difference stems from our choice to ignore or accept type constraint information based on whether we seek to represent a lower or upper type bound of the type variable. There is no need no perform variable-elimination promotion (⇑) and demotion (⇓) directly within the ΘG rules because we do not perform any approximation. For example, rather than always inferring a TypeFocus instance for a subtyping derivation such as a <:I nt → Int, we will only do so, if we seek to represent the upper bounds of the type variable a.

Rules ΘG(TOP) and ΘG(BOT) directly correspond to their constraint generation counterparts

(CG-(top))and(CG-(bot)), respectively, where the top type and the bottom type is a su- pertype and a subtype of any type, respectively, and do not lead to type constraints. The additional premises in the rules, along with theΘG() rule ensure that the definition is al-

gorithmic. We notice that the implicitly and explicitly added⊥ and  type bounds are not distinguishable in the a-constraint set; initially every a-constraint set is{<:ai<:}for all ai∈ a. Our TypeFocus translation faithfully represents every type constraint, including those

Chapter 3. Guided-analysis for type derivation trees

TheΘG(FUN)andΘG(REC)rules define the inference of TypeFocus for arrow and record type con-

structors, respectively. Whenever the subtyping derivation between their type elements re- turns a non-empty sequence, we simply compose it with an appropriate TypeFocus instance; the composition ensures that the type selection is well-formed with respect to the type appli- cation involving the given type constructor.

The algorithmic rulesΘGare well-behaved, meaning that every type selection extracts a left tagged value from the types of the subtyping derivation. The statement is formally specified in Lemma 3.9.

Lemma 3.9 Well-formedness of theΘ sequence generated from the subtyping deriva-

tion.

Let any S and T , a set of type variables a, a type variable ai such that ai∈ a, and vari-

ance informationψ±, such that eitherfv(T )∩ a = , orfv(S)∩ a = . If (ai,ψ±g en S<:T  Θ) then

∀Θ.∃T.∃S.Θ ∈ Θ ∧ Θ(T ) =inlT ∧ Θ(S) =inlS∧ (T= a

i∨ S= ai)

Proof.

By induction on the lastΘGrule used.

A complete proof is available in Appendix E.1.

The generated sequences of TypeFocus instances are sound with respect to the type con- straints that are generated by the original constraint generation judgment of Local Type Infer- ence. The soundness property, formally stated in Lemma 3.10, states that for any type variable ai the types that are extracted by theΘ instances approximate (with least upper bound ap-

proximation) to the same type as the lower bound inferred from the constraint generation judgment. Similarly for the upper bound types of the type variable, except that the approxi- mation means greatest lower bound approximation of types.

Lemma 3.10 Soundness of the TypeFocus translation with respect to the inferred a-

constraint set.

For any types S and T , a set of type variables a and a set of out-of-scope bounded vari- ables W :

If (ai,+g en S<:T  Θ) and ai∈ a and (W aS<: T ⇒ C) then

{A<:ai <:B}∈ C and

(fv(S)∩ a =  =⇒ WΘ(T ) = A) and (fv(T )∩ a =  =⇒ WΘ(S) = A).

If (ai,-g en S<:T  Θ) and ai∈ a and (W aS<: T ⇒ C) then

3.7. On understanding the type variable instantiation

{A<:ai<:B}∈ C and

(fv(S)=  =⇒WΘ(T ) = B) and (fv(T )=  =⇒WΘ(S) = B).

Proof.

By induction on the last rule used in the (ai,ψ±g en S<:T  Θ) judgment.

A complete proof is available in Appendix E.2.

We use aWΘ(T ) notation to abbreviate a calculation of the least upper bound approxima-

tion from the type selections, i.e., WΘ(T ) is equivalent to

Θ1(T )

tpe ⇑W ∨ ... ∨ Θn(T )tpe ⇑W (forΘi∈ Θ where 1 ≤ i ≤ n). SimilarlyWΘ(T ) ab-

breviates a calculation of the greatest lower bound approximation from the type selections, i.e., VΘ(T ) is equivalent to Θ1(T )tpe ⇓W ∧ ... ∧ Θn(T )tpe ⇓W (forΘi∈ Θ where 1 ≤ i ≤ n). IfΘ =  thenWΘ(T ) = ⊥ andWΘ(T ) = . The approximations take into account the po-

tential variable-elimination promotion (⇑) and demotion (⇓) with respect to the type variable set W , as carried out in the(CG-Lower)and(CG-Upper)rules, respectively. For presentation reasons, when the W set is omitted, the promotion and demotion is performed with respect to an empty set of bounded type variables.

The TypeFocus instances representing the type constraints of some type variable are also com- plete with respect to the a-constraint set inferred by the corresponding constraint generation judgment. The completeness property, formally stated in Lemma 3.11, ensures that the Type- Focus instances inferred from the generation judgment reflect all the possible constraints corresponding to the lower and upper type bound, respectively. The completeness property is divided into two parts, one for each of the possible type bounds. Apart from the check for the inclusion of a type constraint (i.e., Θ’(T )=inla

iimpliesΘ’∈ Θ

+

) we also have to verify that the type constraint belongs to the appropriate type bound. The TypeFocus-sequences themselves do not carry information about the kind of the type bound they represent there- fore both definitions rely on the fact that knowing which of the types of the subtyping deriva- tion is type variable-free links the position of the type variable in the type with the kind of lower or upper type bound it can produce. For example, the a<:  subtyping check defines a type constraint that is valid as an upper type bound type constraint but not as the lower type bound type constraint.

The definition uses the implicit variance position functionposaof type T→ ψ. The function

returns information about the type T being constant, covariant, contravariant, or invariant in the given type variable a. The complete variance information is defined symbolically as

Chapter 3. Guided-analysis for type derivation trees

For simplicity, we assume now that ifposa(T ) returns± then both statements ± =+and± =-

are correct, and ifposa(T ) returns 0 then statement 0=+is valid, and defer the explanation of such semantics until Section 3.7.2.

Lemma 3.11 Completeness of the TypeFocus translation with respect to lower and upper type bounds of the a-constraint sets .

We let a represent a set of free type variables,

let types S and T such that eitherfv(S)∩ a =  orfv(T )∩ a = . If (ai,+g en S<:T  Θ

+ ) then (fv(S)∩ a = ) =⇒

(∀Θ. (Θ(T )=inla

i) and (Θ’, WFS) and (posai(T )=+) =⇒ Θ

∈ Θ+) and

(fv(T )∩ a = ) =⇒ (∀Θ. (Θ(S)=inla

i) and (Θ’, WFT ) and (posai(S)=-) =⇒ Θ∈ Θ

+ ) If (ai,-g en S<:T  Θ - ) then (fv(S)∩ a = ) =⇒ (∀Θ. (Θ(T )=inla

i) and (Θ’, WFS) and (posai(T )=-) =⇒ Θ∈ Θ

- ) and

(fv(T )∩ a = ) =⇒ (∀Θ. (Θ(S)=inla

i) and (Θ’, WFT ) and (posai(S)=+) =⇒ Θ

∈ Θ-)

Proof.

Prove by induction on the structure of S and T , the two types that participate in the subtyping derivation.

The complete proof is available in Appendix E.3.

Together, the soundness and completeness properties ensure that the generated sequence of TypeFocus instances faithfully represent all type constraints that are used to infer lower or upper type bounds for the involved type variables, irrespective of any least upper bound or greatest lower bound approximations.

In document Decrypting Local Type Inference (Page 100-106)