• Non ci sono risultati.

RELATED WORK AND CONCLUSION

Nel documento Definitions and Conditional Expressions (pagine 36-51)

PP(D,matche0with{p1⇒ e1,. . . , pn⇒ en})

For every expression e and environment D, the set PP(D, e) is an equivalence class of pairs modulo renaming of the type variables in a pair.

LEMMA 9.12. For every expression e and environment D, ifhA, vi ∈ PP(D, e), then

(1) Dom( A)= (FV(e) − Dom(D)) ∪ FVR(D|FV(e)), and

(2) hA0, v0i ∈ PP(D, e) if and only if there is a bijection s : Tv → Tv such that s( A)= A0and s(v)= v0.

PROOF. By induction on Definition 9.11.

Indeed Definition 9.11 specifies an inference algorithm: to perform type infer-ence on an expression e with respect to the environment D simply follow the def-inition of PP(D, e), choosing fresh type variables and using the2-satisfaction, Lub2-satisfaction, and PAT algorithms as necessary.

THEOREM 9.13. (Soundness and completeness of PP for`Loc,Rec,Con

2 ). For ev-ery expression e and environment D:

(Soundness). IfhA, vi ∈ PP(D, e), then D; A `Loc,Rec,Con

2 e : v.

(Completeness). If D; A0 `Loc,Rec,Con

2 e : v0, then there are a pair hA, vi ∈ PP(D, e) and a substitution s such that A01s( A) and s(v)2v0.

PROOF. See Appendix A.3.

10. RELATED WORK AND CONCLUSION

The literature related to the present work has been partially quoted through the paper. We add here a brief survey on inference algorithms for systems involving intersection types.

The system of intersection types [Coppo and Dezani-Ciancaglini 1980; Coppo 1980; Coppo et al. 1981; Barendregt et al. 1983] is undecidable. A first approach for designing a (semi-)algorithm for finding a principal pair is that of splitting the problem in two parts: first find a normal form for the term being typed (this problem is undecidable) and then build a principal pair for the normal form (this problem is decidable). To show that the approach is sound it is necessary to prove that both the normal form and the original term have the same typings.

This approach has been followed, for instance, by Coppo et al. [1980], Ronchi della Rocca and Venneri [1984], and van Bakel [1993].

The first unification-based approach to intersection type inference was pro-posed by Ronchi della Rocca [1988] where, in particular, a decidable restriction which bounds the height of types is presented. More recently, Kfoury and Wells [1999] presented a simpler unification-based approach using a novel form of unification, calledβ-unification.

In spite of the fact that System F [Girard 1972; Reynolds 1974] and its finite rank restrictions above 3 have neither principal typings nor decidable type in-ference [Wells 1994; Kfoury and Wells 1994], combining intersection types and universal quantification results in a system with principal typings [Margaria and Zacchi 1995]. Moreover, decidable restrictions of the combined system have been developed (e.g. [Damiani and Giannini 1994; Coppo and Giannini 1995;

Jim 2000]).

Due to the expressive power of the corresponding type systems, the inference algorithms mentioned above are inherently quite expensive. More efficient in-ference algorithms can be developed by considering the rank 2 restriction of intersection types [Leivant 1983; van Bakel 1993; Yokouchi 1995; Jim 1996].

Following this line of research, in the present paper we have proposed a sound and complete principal pair inference algorithm for a rank 2 intersection type system with new typing rules for assigning rank 2 types to:

(1) local definitions, by using the principal pair property of the system to asso-ciate a different instance of the principal typing to each occurrence of the locally defined identifier;

(2) mutually recursive definitions, by allowing a restricted form of polymorphic recursion (as explained in Section 6, this rule is a slight improvement of a rule proposed by Jim [1996, 1995]);

(3) conditional expressions, by restricting the use of intersection in the types assigned to the branches in order to preserve the principal pair property.

Both the techniques for local definitions and recursive definitions do not depend on the particulars of rank 2 intersection typing, but apply to any system with principal pairs. The technique for conditional expressions, instead, is tailored to rank 2 intersection types. However, the strategy of introducing metrics on types to limit the use of the intersection type constructor in the types assigned to the branches of the conditionals expressions might also be useful for other type systems.

APPENDIX

A.1 An Extension of Jim’s Subtype Satisfaction Algorithm (Theorem 9.2)

We say that two≤∀2,1-satisfaction problems are equivalent if they have the same solutions.

A unification problem is a∀2,1-satisfaction problem that involves only equalities.40

40A unification problem can be solved by Robinsons’s algorithm (see, for instance, Chapter 3 of Hindley [1997]), which can decide whether it is solvable, and, if so, return a most general solution.

The following lemma implies Theorem 9.2.

LEMMA A.1. Every∀2,1-satisfaction problem is equivalent to a unification problem. In particular, there is an algorithm that, given a∀2,1-satisfaction prob-lem, either proves that it cannot be satisfied or transforms it into an equivalent unification problem.

PROOF. Following Jim [1996, 1995], we prove the lemma by providing the transformation algorithm. We first introduce a set of transformation rules of the form

t≤ ui ⇒ ∃−α .P (where t ∈ T2∪ T∀2).

Each rule transforms an inequality in a≤∀2,1-satisfaction problem. The trans-formation rules are in Figure 13. Note that, for each transtrans-formation rule t ≤ ui ⇒ ∃−α .P, we have that

FTV(t)∪ FTV(ui) = FTV(∃−α .P). (4) In particular, the type variables −→α are either fresh type variables introduced by the transformation rule (i.e., they do not appear in the left-hand side) or are the bound variables of t (when t∈ T∀2).

The transformation algorithm is specified as a rewrite relation on problems,

=⇒, defined by the rule:

(=⇒) vs≤ ui ⇒ ∃−α .P

∃−→β .(P0] {vs ≤ ui}) =⇒ ∃−β ] −α .(P0∪ P)

where the operator “]” denotes disjoint union (this implies that the variables

−→α must be fresh).

To see that the rewriting relation=⇒ describes an algorithm which proves the lemma, observe that:

(1) every rewriting step (corresponding to the application of one of the rules in Figure 13) transforms a ≤∀2,1-satisfaction problem into another ≤∀2,1 -satisfaction problem,

(2) every rewriting step preserves the set of solutions (note that the condition (4) above is necessary to ensure this preservation),

(3) every inequality matches the left-hand side of at most one rule, and (4) repeated application of these rules must terminate.41 This is proved by

defining a metric| · | on problems for which each rewriting step is strictly decreasing. The size of inequalities will be defined by structural induction.

By point (3), it is sufficient to consider the cases given by the left-hand sides of the rules in Figure 13. For the base case we set |u0 ≤ u| = 1.

Every other case is simply defined so that the size of the left-hand side of a rule is greater than the sum of the of the inequalities appearing on the corresponding right-hand side. For example, define

|(ui → v) ≤ α| = |α1≤ ui| + |v ≤ α2| + 1,

|(ui → v) ≤ (u1→ u2)| = |u1≤ ui| + |v ≤ u2| + 1,

41In the original termination proof given in Jim [1996, 1995] there is an error. Here we are following the termination proof given in Jim [2000].

Fig. 13. Transformation rules for∀2,1-satisfaction.

|t ≤ (u1∧ · · · ∧ un)| = |t ≤ u1| + · · · + |t ≤ un| + 1, and

—|(∀−→α .v) ≤ u| = |v ≤ u| + 1.

The function|·| is defined by structural induction (since types on right-hand sides either appear as syntactic subtypes on left-hand sides, or are type variables) and, by construction, gives a decreasing metric on problems.

Normal forms are either unification problems (i.e. do not contain inequalities) or contain at least an inequality of the form ui→ v ≤ u0where u0is a simple type which is neither a type variable nor an arrow type (such an inequality is clearly not satisfiable).

Remark A.2. (Comparison with Jim’s subtype satisfaction algorithm).

Lemma A.1 and Theorem 9.2 are a slight extension of results presented by Jim [1996, 1995]. The main differences are in the transformation rules. The transformation rules in Figure 13 either produce a unification problem or a satisfaction problem which is clearly not satisfiable. The transformation rules in Jim [1996, 1995], instead, always produce a unification problem. This dif-ference is due to the richer set of types handled by our rules42: if we restrict ourselves to the set of types considered in Jim [1996, 1995] the rules in Figure 13 always produce a unification problem.

A.2 A Lub Satisfaction Algorithm (Theorem 9.7)

We say that a Lub2-satisfaction problem and a≤∀2,1-satisfaction problem are equivalent if they have the same solutions.

A Lub2-satisfaction problemh j, {v1,. . . , vn}i is binary if n = 2. The following lemma implies Theorem 9.7 restricted to binary Lub2-satisfaction problems.

LEMMA A.3. Every binary Lub2-satisfaction problem is equivalent to a

∀2,1-satisfaction problem. In particular, there is an algorithm that, given a Lub2-satisfaction problem transforms it into an equivalent∀2,1-problem.

42The rules in Jim [1996, 1995] consider only type variables, arrow types, and top-level conjunctions.

We also consider parametric datatypes.

Fig. 14. Transformation algorithm for binary Lub2-satisfaction.

PROOF. We prove the lemma by providing the transformation algorithm. The transformation algorithm is specified as a recursive function TA defined by the clauses in Figure 14.

To show that the recursive function TA describes an algorithm that proves the lemma, for every binary Lub2-satisfaction problem h j, {v1, v2}i we will prove that:

(1) TA( j, v1, v2) is terminating and returns a≤∀2,1-satisfaction problem, (2) FTV(TA( j, v1, v2))⊆ FTV({v1, v2}), and

(3) the transformation preserves the set of solutions (i.e. h j, {v1, v2}i and TA( j, v1, v2) are equivalent).

Points (1) and (2) are straightforward, so we consider only point (3). The proof is by induction on j ∈ {0, . . . , Max{Ind(v1), Ind(v2)}}.

j = 0. From the definition of TA (see Figure 14) we have TA(0, v1, v2)= ∃α.P, whereα is fresh and P = {v1≤ α, v2≤ α}.

— If s is a solution of h0, {v1, v2}i then Ind(Lub2(s(v1), s(v2))) = 0, which implies s(v1)= s(v2)∈ T0. So s is a solution of∃α.P.

— If s is a solution of ∃α.P then s(v1) = s(v2) ∈ T0, which implies Ind(Lub2(s(v1), s(v2)))= 0. So s is a solution of h0, {v1, v2}i.

j > 0. By definition of Lub2-satisfaction problem we have jMax{Ind(v1), Ind(v2)}}, so at least one of v1and v2is an arrow type.

— If they are both arrow types, say v1 = ui1 → v10 and v1 = ui2 → v20, then TA( j, ui1 → v01, ui2 → v02) = TA( j − 1, v01, v02). By definition of Lub2 we have that, for every substitution s, Lub2(s(v1), s(v2))= s(ui1)∧ s(ui2)→ Lub2(s(v01), s(v20)). Soh j, {v1, v2}i and h j − 1, {v10, v20}i are equivalent, and the result follows by induction.

— If v1is a type variableα and v2= ui → v, for some v = ui1→ · · · → uim→ u (m≥ 0) with u ∈ T0such that u is not an arrow type andα 6∈ FTV(u) (the symmetric case is similar), then TA( j,α, ui → v) = ∃α1α2] −→β .({α = α1

Fig. 15. Transformation algorithm for n-ary Lub2-satisfaction.

α2}∪ P), where α1,α2are fresh type variables, and∃−→β .P = TA( j −1, [α :=

α1 → α2]v,α2). We now prove thath j, {α, ui → v}i and ∃α1α2] −→β .({α = α1→ α2} ∪ P) are equivalent.

— By definition of Lub2 we have that every solution s ofh j, {α, ui → v}i is such that s(α) = u1 → u2 and Ind(Lub2(s(α), s(ui → v))) ≤ j . So s = s0◦[α := α1 → α2] for some fresh type variables α1,α2 and substitution s0 such that s0(γ ) = s(γ ) for every γ 6∈ {α1,α2} and s0 is a solution of h j − 1, {[α := α1 → α2]v,α2}i. By induction s0 is a solution to∃−→β .P = TA( j − 1, [α := α1→ α2]v,α2), so s is a solution to

∃α1α2] −→β .({α = α1→ α2} ∪ P).

— If s is a solution to∃α1α2] −→β .({α = α1 → α2} ∪ P), then s = s0◦[α :=

α1→ α2], for some substitution s0such that s0(γ ) = s(γ ) for every γ 6∈

1,α2} and s0is a solution of∃−→β .P = TA( j − 1, [α := α1→ α2]v,α2).

By induction s0 is a solution toh j − 1, {[α := α1→ α2]v,α2}i, so s is a solution toh j, {α, ui → v}i.

— Otherwise h j, {v1, v2}i has no solutions and is, therefore, equivalent to

∃².{int=bool}.

The following lemma generalizes LemmaA.3 to n-ary Lub2-satisfaction problems, and so implies Theorem 9.7.

LEMMA A.4. Every Lub2-satisfaction problem is equivalent to a∀2,1 -satisfaction problem. In particular, there is an algorithm that, given a Lub2 -satisfaction problem transforms it into an equivalent∀2,1-problem.

PROOF. We prove the lemma by providing the transformation algorithm.

The transformation algorithm is specified as a recursive function TAndefined by the clauses in Figure 15, that generalizes the function TA in Figure 14 to an arbitrary number of arguments.

To show that the recursive function TAndescribes an algorithm that proves the lemma, for every n-ary (n≥ 1) Lub2-satisfaction problemh j, {v1,. . . , vn}i

we will prove that, if j ≤ Ind(v1) then43

(1) TAn( j, v1,. . . , vn) is terminating and returns a≤∀2,1-satisfaction problem, (2) FTV(TAn( j, v1,. . . , vn))⊆ FTV({v1,. . . , vn}), and

(3) the transformation preserves the set of solutions (i.e. h j, {v1,. . . , vn}i and TAn( j, v1,. . . , vn) are equivalent).

Points (1) and (2) are straightforward, so we consider only point (3). The proof is by induction on n.

n= 1. Every substitution s is a solution to h j, {v1}i. Therefore, h j, {v1}i and ∃².∅

are equivalent.

n> 1. By induction h j, {v1,. . . , vn−1}i and TAn−1( j, v1,. . . , vn−1) are equivalent.

Therefore, if TAn−1( j, v1,. . . , vn−1) has no solutions, thenh j, {v1,. . . , vn}i and

∃².{int = bool} are equivalent (they both have no solutions). Assume that TAn−1( j, v1,. . . , vn−1) = ∃−→α .P has solutions. For all s ∈ MGS(∃−α .P) we have that, by Lemma A.3,

h j, Lubn−12 (s(v1),. . . , s(vn−1)), s(vn)i and TA( j, Lubn−12 (s(v1),. . . , s(vn−1)), s(vn)) are equivalent. Let TA( j, Lubn−1

2 (s(v1),. . . , s(vn−1)), s(vn)) = ∃−→β .P0 and −→γ = FTVR(s) − (−α ] −β ). We now prove that h j, {v1,. . . , vn}i and

∃−→α ] −β ] −γ .(P ∪ P0) are equivalent.

— If s0 is a solution to h j, {v1,. . . , vn}i then it is a solution of h j, {v1,. . . , vn−1}i. So there is s ∈ MGS(∃−α .P) such that s ≤ s0 and FTV(vn) ∩ FTVR(s) = ∅. Since s is idempotent, s0 is a solution to TA( j, Lubn−12 (s(v1),. . . , s(vn−1)), s(vn))= ∃−→β .P0. Therefore, s0is also a so-lution to∃−→α ] −β ] −γ .(P ∪ P0).

— If s0 is a solution to∃−→α ] −β ] −γ .(P ∪ P0), then s0 is a solution to both h j, {v1,. . . , vn−1}i and h j, {Lubn−12 (s(v1),. . . , s(vn−1)), s(vn)}i, for some s ∈ MGS(∃−→α .P) such that FTV(vn)∩ FTVR(s) = ∅. Therefore, since s ≤ s0 and s is idempotent, Ind(Lubn2(s0(v1),. . . , s0(vn)))≤ j . s0is a solution to h j, {v1,. . . , vn}i.

A.3 Soundness and Completeness of the Inference Algorithm for`Loc, Rec, Con

2 (Theorem 9.13)

Let `sd2 be the syntax directed restriction of`Loc,Rec,Con

2 obtained by removing rule (SUB) and by replacing the rules (IFI) and (MATCHI) by the following rules:

(IFISD)

D; A` e0:bool D; A1` e1: v1 D; A2` e2: v2 v= Lub2(v1, v2)

Ind(v)≤ Max{ Min{Ind(v01)| D; A01` e1: v10}, Min{Ind(v02)| D; A02` e2: v20} } D; A+ A1+ A2`ife0thene1elsee2: v

43By definition of Lub2-satisfaction problem we have 0≤ j ≤ Max{Ind(v1),. . . , Ind(vn)}. The condition j ≤ Ind(v1) is necessary to ensure that, for every k ∈ {1, . . . , n}, h j, {v1,. . . , vk}i is a Lub2-satisfaction problem (i.e. 0≤ j ≤ Max{Ind(v1),. . . , Ind(vk)}).

(MATCHISD) Indeed, system`sd2is as powerful as system`Loc,Rec,Con

2 , as shown by the follow-ing lemma.

LEMMA A.5. (System`sd2is as powerful as system`Loc,Rec,Con

2 ). For every ex-pression e and environment D, if D; A`Loc,Rec,Con

2 e : v, then D; A0 `sd2 e : v0for

The result can be proved by showing that (the proof is by structural induction on`Loc,Rec,Con

2 derivations), for every expression e, environment D, and environ-ment D0, such that D02 D, if D; A`Loc,Rec,Con

2 e : v, then D0; A0 `sd2 e : v0 for some A0and v0such that A1 A0and v02v.

By Lemma A.5, we have that Theorem 9.13 (Soundness and completeness of PP for`Loc,Rec,Con

2 ) is implied by the following result.

LEMMA A.6. (Soundness and completeness of PP for`sd2). For every expres-sion e and environment D:

(Soundness). IfhA, vi ∈ PP(D, e), then D; A `sd2 e : v.

(Completeness). If D; A0`sd2 e : v0, then there are a pairhA, vi ∈ PP(D, e) and a substitution s such that A0= s(A) and s(v) = v0.

PROOF. Both soundness and completeness by structural induction on e.

44Observe that

— v12v2implies FTV(v1)⊆ FTV(v2), and

— A21A1implies FTV( A2)⊇ FTV(A1).

e= x.

(Soundness). We have to consider three cases.

— If x : ∀−→α .hA0, v0i ∈ D, then hs(A0), s(v0)i ∈ PP(D, x), for some fresh renaming s of −α . We have D; s(A0)`sd2 x : s(v0) by rule (IDlocal).

— If x :∀−→α .v ∈ D, use rule (IDglobal).

— If x6∈ Dom(D), use rule (IDundefined).

(Completeness). Again, we have to consider three cases.

— If x : ∀−→α .hA0, v0i ∈ D, then the last (and only) rule applied must be (IDlocal).

— If x :∀−→α .v ∈ D, the last (and only) rule applied must be (IDglobal).

— If x6∈ Dom(D), the last (and only) rule applied must be (IDundefined).

In all the three cases the proof is immediate.

e= c.

(Soundness). Use rule (CONST).

(Completeness). Immediate, since the last (and only) rule applied must be (CONST).

e= λx.e0.

(Soundness). We have to consider two cases.

— If x∈ FV(e0), thenhA, x0: ui, v0i ∈ PP(D, e0[x := x0]) andhA, ui → v0i ∈ PP(D,λ x.e0). By induction, D; A, x0: ui`sd2 e0[x := x0] : v0, and by rule (ABS) we get:

D; A`sd2 λx.e0: ui→ v0.

— If x 6∈ FV(e0), thenhA, v0i ∈ PP(D, e0) andhA, α → v0i ∈ PP(D, λ x.e0) for some fresh type variableα. By induction, D; A `sd2 e0: v0, and by rule (ABSVAC) we get:

D; A`sd2 λx.e0:α → v0. (Completeness). Again, we have to consider two cases.

— If x ∈ FV(e0), then the last rule applied must be (ABS). We have v0 = ui0→ v00 and D; A0, x0: ui0`sd2 e0[x := x0] : v00. By induction there exist a fresh pairh(A, x0: ui), v0i ∈ PP(D, e0[x := x0]) and a substitution s such that

A0, x0: ui0= s(A, x0: ui) and s(v0)= v00.

SohA, ui → v0i ∈ PP(D, λ x.e0) and s are the desired pair and substitu-tion.

— If x 6∈ FV(e0), then the last rule applied must be (ABSVAC). We have v0= u → v00 and D; A0 `sd2 e0: v00. By induction there exist a fresh pair hA, v0i ∈ PP(D, e0) and a substitution s such that

A0= s(A) and s(v0)= v00.

SohA, α → v0i ∈ PP(D, λ x.e0), where α is a fresh type variable, and [α := u] ◦ s are the desired pair and substitution.

e= e0e1.

(Soundness). We havehA0, v0i ∈ PP(D, e0) and, by induction, D; A0`sd2 e0: v0.

— If v0 is a type variable α, then we must have a fresh pair hA1, v1i ∈ PP(D, e1), A= s(A0+ A1) and v= s(α2), where s∈ MGS(∃².{v1≤ α1, α = α1→ α2} for fresh type variables α1andα2. By induction, D; A1`sd2 e1: v1. By substitutivity

D; s( A0)`sd2 e0: s(α), and D; s( A1)`sd2 e1: s(v1).

Then, by rule (APP), we have:

D; s( A0+ A1)`sd2 e0e1: s(α2).

— If v0 = u1 ∧ · · · ∧ un → v0, then we must have fresh pairs hAi, vii ∈ PP(D, e1) (for all i ∈ {1, . . . , n}), s ∈ MGS(∃².{vi ≤ ui | i ∈ {1, . . . , n}}, A= s(A0+ A1+ · · · + An), and v= s(v0). By induction and substitutivity

D; s( A0)`sd2 e0: s(u1∧ · · · ∧ un→ v0), and D; s( A1)`sd2 ei : s(vi) (for all i∈ {1, . . . , n}).

Then, by rule (APP), we have:

D; s( A0+ A1+ · · · + An)`sd2 e0e1: s(v0).

(Completeness). The last rule applied must be (APP). We have D; A00 `sd2 e0 : u01∧ · · · ∧ u0n→ v0, D; A0i `sd2 e1 : u0i (∀i ∈ {1, . . . , n}), and A0 = A00+ A01+ · · · + A0n. By induction both PP(D, e0) and PP(D, e1) are not empty and, by Lemma 9.12.(2), it is enough to consider the following two cases on the structure of the pairshA0, v0i ∈ PP(D, e0).

— v0= α (a type variable). By induction there exists a substitution s0such that

A00= s0( A0) and s0(α) = u01∧ · · · ∧ u0n→ v0.

By definition of substitution, s0(α) ∈ T0, so we have that n= 1 and v0= u for some u∈ T0.

By induction and Lemma 9.12.(2) there is a fresh pair hA1, v1i ∈ PP(D, e1) and a substitution s1such that

A01= s1( A1) and s1(v1)= u01.

Let P = {v1≤ α1, α = α1→ α2}, where α1,α2are fresh. The substitution s0 = s0◦s1◦{α1 := u01,α2 := u} is a solution of the satisfaction problem

∃².P and is such that

A0= s0( A0)+ s1( A1)= s0( A0+ A1), and s0(α2)= u = v0.

So there is s∈ MGS(∃².P) such that

hs(A0+ A1), s(α2)i ∈ PP(D, e0e1) and s≤ s0.

— v0= u1∧ · · · ∧ um → v. By induction there exists a substitution s0such that

A00= s0( A0) and s0(v)= u01∧ · · · ∧ u0m→ v0.

By induction and Lemma 9.12.(2), for all j ∈ {1, . . . , m}, there are fresh pairshAj, vji ∈ PP(D, e1) and substitutions sj such that

A0j = sj( Aj) and sj(vj)= u0j.

Let P = {vj ≤ uj | j ∈ {1, . . . , m}}. The substitution s0= s0◦s1◦ · · · ◦smis a solution of the satisfaction problem∃².P and is such that A0= s0( A0)+ s1( A1)+ · · · + sm( Am)= s0( A0+ A1+ · · · + Am), and s0(v)= v0. So there is s∈ MGS(∃².P) such that

hs(A0+ A1+ · · · + Am), s(v)i ∈ PP(D, e0e1) and s≤ s0.

e=letx = e0ine1.

(Soundness). We have fresh pairs hA0, v0i ∈ PP(D, e0) and hA1, vi ∈ PP((D, x0 : ∀−→α .hA0, v0i), e1[x := x0]), where −→α = FTV(A0)∪ FTV(v0). By induction, D; A0`sd2 e0: v0and D, x0:∀−→α .hA0, v0i; A1`sd2 e1[x := x0] : v1.

— If x∈ FV(e1), then A= A1. So by rule (LETPSI) we have:45 D; A`sd2 letx = e0ine1: v.

— If x6∈ FV(e1), then A= A1+ A0. So by rule (LETPSI) we have:

D; A`sd2 letx = e0ine1: v.

(Completeness). The last rule applied must be (LETPSI). We have D; A00`sd2 e0 : v00, Ind(v00) = Min{Ind(v000) | D; A000 `sd2 e0 : v000}, D, x0 : ∀−→α .hA00, v00i;

A01`sd2 e1[x := x0] : v0, and A0=

(A01, if x∈ FV(e1) A01+ A00, otherwise

Let x∈ FV(e1) (the other case is similar). By induction there are fresh pairs hA0, v0i ∈ PP(D, e0), hA1, v1i ∈ PP((D, x0 : ∀−→α .hA00, v00i), e1[x := x0]), and substitutions s0, s1, such that:

0. A00= s0( A0) and s0(v0)= v00, 1. A01= s1( A1) and s1(v1)= v0.

By point (0), there are a fresh pairhA2, vi ∈ PP((D, x0:∀−→α .hA0, v0i), e1[x := x0]) and a substitution s2such that:

2. A01= s2( A2) and s2(v)= v0.

SohA2, vi ∈ PP(D,letx = e0ine1) and s2are the desired pair and substitu-tion.

e=reci0{x1= e1,. . . , xn= en} (i0∈ {1, . . . , n}).

(Soundness). We have fresh pairshAi, vii ∈ PP(D, ei[x1:= x10]· · · [xn := xn0]) (for all i ∈ {1, . . . , n}), and s ∈ MGS(∃².{Gen(A1+ · · · + An, vj) ≤ uij | j ∈ { j1,. . . , jm}}), with {x0j1,. . . , x0jm} = {x10,. . . , xn0} ∩ (Dom(A1+ · · · + An)) and

45The rule can be applied, since:

Ind(v0)= Min{Ind(v00)| D; A00`sd2e0: v00} (by induction and Proposition 7.9).

A1+ · · · + An = A, x0j1 : uij1,. . . , x0jm : uijm. By induction and substitutivity D; s( Ai)`sd2 ei[x1:= x10]· · · [xn := xn0] : s(vi) (for all i∈ {1, . . . , n}). By Defini-tion 9.1, for all j ∈ { j1,. . . , jm}, s(Gen(A1+ · · · + An, vj))≤∀2,1s(uij) and, by Lemma 3.8, Gen(s( A1+ · · · + An), s(vj))≤∀2,1s(uij). Then, by rule (REC2), we have:

D; s( A)`sd2 reci0{x1= e1,. . . , xn= en} : s(vi0).

(Completeness). The last rule applied must be (REC2). We have D; A0i `sd2 ei[x1:= x10]· · · [xn:= xn0] : vi0(for all i∈ {1, . . . , n}), Gen(A01+ · · · + A0n, v0j)≤∀2,1 ui0j (for all j ∈ { j1,. . . , jm}), with {x0j1,. . . , x0jm} = {x10,. . . , xn0} ∩ Dom(A01+

· · · + A0n) and A01+ · · · + A0n= A0, x0j1 : ui0j1,. . . , x0jm : ui0jm.

By induction, for all i ∈ {1, . . . , n}, there are fresh pairs hAi, vii ∈ PP(D, ei[x1:= x10]· · · [xn:= xn0]) and substitutions si such that

A0i = si( Ai) and si(vi)= v0i.

Let A1+· · ·+An= A, x0j1 : uij1,. . . , x0jm: uijmand P = {Gen(A1+· · ·+An, vj)≤ uij | j ∈ { j1,. . . , jm}}. The substitution s0= s1◦ · · · ◦snis such that

A0= s0( A) and s0(vi0)= v0i0

and is a solution of the satisfaction problem∃².P. So there is s ∈ MGS(∃².P) such that

hs(A), s(vi0)i ∈ PP(D,reci0{x1= e1,. . . , xn= en}) and s≤ s0.

e=ife0thene1elsee2.

(Soundness). We have fresh pairshA0, v0i ∈ PP(D, e0),hA1, v1i ∈ PP(D, e1), hA2, v2i ∈ PP(D, e2), and substitutions s0 ∈ MGS(∃².{v0 ≤ bool}), s ∈ MGS(h j, {v1, v2}i), where j = Max(Ind(v1), Ind(v2)). By induction and substitutivity

D; s0( A0)`sd2 e0: s0(v0), and

D; s( Ai)`sd2 ei: s(vi) (for all i∈ {1, 2}).

Then by rule (IFISD) we have46

D; s0( A0)+ s(A1)+ s(A2)`sd2 ife0thene1elsee2: Lub2(s(v1), s(v2)). (Completeness). The last rule applied must be (IFISD). We have D; A00`sd2 e0: bool, D; A01`sd2 e1: v10, D; A02`sd2 e2: v02, A0= A00+ A01+ A02, v0= Lub2(v01, v20), and

Ind(v0)≤ Max{ Min{Ind(v100)| D; A001`sd2 e1: v001}, Min{Ind(v200)| D; A002`sd2 e2: v002} }

46The rule can be applied, since

Ind(Lub2(s(v1), s(v2)))≤ j (since s∈ MGS(h j, {v1, v2}i)), and

j = Max(Ind(v1), Ind(v2))

= Max{ Min{Ind(v01)| D; A01`sd2e1: v01}, Min{Ind(v2)| D; A02`sd2e2: v20} } (by induction and Proposition 7.9).

By induction there are fresh pairshA0, v0i ∈ PP(D, e0),hA1, v1i ∈ PP(D, e1), hA2, v2i ∈ PP(D, e2), and substitutions s0, s1, s2, such that:

0. A00= s0( A0) and s0(v0)=bool, 1. A01= s1( A1) and s1(v1)= v01, 2. A02= s2( A2) and s2(v2)= v02.

Let P= {v0≤bool}. The substitution s00= s0◦s1◦s2

— is such that

A00+ A01+ A02= s00( A0+ A1+ A2)), and Lub2(s00(v1), s00(v2))= v0,

— is a solution of the≤∀2,1-satisfaction problem∃².P, and

— is a solution to the Lub2-satisfaction problem h j, {v1, v2}i, where j = Max(Ind(v1), Ind(v2)).47

So there are

— s3∈ MGS(∃².P), and

— s∈ MGS(h j, {v1, v2}i), such that

hs3( A0)+ s(A1)+ s(A2), Lub2(s(v1), s(v2))i ∈ PP(D,ife0thene1elsee2), s00= s03◦s3and s00= s000◦s, for some substitutions s03, s000 such that Dom(s03)∩ Dom(s000)= ∅, FTVR(s03)∩Dom(s000)= ∅, Dom(s000)∩FTV(s03(s3( A0)))= ∅, and Dom(s03)∩(FTV(s(A1))∪FTV(s(A2))∪FTV(s(v1))∪s(FTV(v2)))= ∅. Therefore, s0= s000◦s03is such that

A00+ A01+ A02= s0(s3( A0)+ s(A1)+ s(A2)), and s0(Lub2(s(v1), s(v2)))= v0.

e=matche0with{p1⇒ e1,. . . , pn⇒ en}.

(Soundness). We have fresh pairshA0, v0i ∈ PP(D, e0),hUl, uli ∈ PAT(p0l) andhAl, vli ∈ PP(D, el0) (for all l ∈ {1, . . . , n}), and substitutions

s∈ MGS(∃α.{ v0≤ α} ∪ (S

1≤l≤n{ul ≤ α, uil ,1≤ ul ,1,. . . , uil ,hl ≤ ul ,hl})), s0 ∈ MGS(h j, {s(v1),. . . , s(vn)}i), with { yl ,1,. . . , yl ,hl} = Dom(Ul)∩ Dom(Al), Ul = Ul0,{ yl ,1 : ul ,1,. . . , yl ,hl : ul ,hl}, Al = Al0,{ yl ,1 : uil ,1,. . . , yl ,hl : uil ,hl}, α fresh type variable, and j = Max(Ind(s(v1)),. . . , Ind(s(vn))). By induc-tion and substitutivity

D; s0(s( A0))`sd2 e0: s0(s(v0)),

s0(s(Ul))Bp0l : s0(s(ul)) and D; s0(s( Al))`sd2 e0l : s0(s(vl)) (for all l ∈ {1, . . . , n}).

Then by rule (MATCHISD) we have48

D; s0(s( A0+ A01+ · · · + A0n)`sd2 matche0with{p1⇒ e1,. . . , pn⇒ en} : Lubn2(s0(s(v1)),. . . , s0(s(vn))).

47In fact

Ind(Lub2(s00(v1), s00(v2)))= Ind(v0)= Ind(Lub2(v10, v02))

≤ Max{ Min{Ind(v001)| D; A001`sd2e1: v100}, Min{Ind(v200)| D; A002`sd2e2: v002} }

= Max(Ind(v1), Ind(v2))

= j

(by induction and Proposition 7.9).

48The rule can be applied, since

Ind(Lubn2(s0(s(v1)),. . . , s0(s(vn))))≤ j

(Completeness). The last rule applied must be (MATCHISD). We have

— is a solution of the≤∀2,1-satisfaction problem∃α.P, and

— is a solution to the Lub2-satisfaction problem h j, {s(v1),. . . , s(vn)}i,

ACKNOWLEDGMENTS

I thank Mario Coppo, Mariangiola Dezani, Paola Giannini, Ines Margaria, Maddalena Zacchi, and the referees of earlier versions of this paper, for valu-able comments and suggestions. I also thank Emiliano Leporati for much use-ful feedback during the preparation of his Master thesis [Leporati 2000]. I’m particularly grateful to the TOPLAS referees for insightful and constructive comments, which greatly improved the submitted version.

REFERENCES

ADITYA, S.ANDNIKHIL, R. 1991. Incremental polymorphism. In Functional Programming Lan-guages and Computer Architecture. LNCS 523. Springer, 379–405.

BARENDREGT, H. P., COPPO, M.,ANDDEZANI-CIANCAGLINI, M. 1983. A filter lambda model and the completeness of type assignment. J. Symb. Logic 48, 931–940.

COPPO, M. 1980. An extended polymorphic type system. In Mathematical Foundations of Com-puter Science. LNCS 88. Springer, 194–204.

COPPO, M.ANDDEZANI-CIANCAGLINI, M. 1980. An extension of basic functional theory for lambda-calculus. Notre Dame J. Formal Logic 21, 4, 685–693.

COPPO, M., DEZANI-CIANCAGLINI, M.,ANDVENNERI, B. 1980. Principal Type Schemes and Lambda-calculus Semantics. In To H. B. Curry. Essays on Combinatory Logic, Lambda-Lambda-calculus and Formalism, R. Hindley and J. Seldin, Eds. Accademic Press, London, 480–490.

COPPO, M., DEZANI-CIANCAGLINI, M., ANDVENNERI, B. 1981. Functional Characters of Solvable Terms. Zeith. Math. Logik Und Grund. Math. 27, 45–58.

COPPO, M.ANDGIANNINI, P. 1995. Principal Types and Unification for Simple Intersection Types Systems. Information and Computation 122, 1, 70–96.

DAMAS, L. M. M. 1984. Type Assignment in Programming Languages. Ph.D. thesis, University of Edinburgh.

DAMAS, L. M. M.AND MILNER, R. 1982. Principal type schemas for functional programs. In POPL’82. ACM, 207–212.

DAMIANI, F. 2000. Typing local definitions and conditional expressions with rank 2 intersection.

In FOSSACS’00 (part of ETAPS’00). LNCS 1784. Springer, 82–97.

DAMIANI, F.ANDGIANNINI, P. 1994. A Decidable Intersection Type System based on Relevance. In TACS’94. LNCS 789. Springer, 707–725.

GIRARD, J. Y. 1972. Interpretation fonctionelle et elimination des coupures dans l’aritmetique d’ordre superieur. Ph.D. thesis, Universit´e Paris VII.

HENGLEIN, F. 1993. Type inference with polymorphic recursion. ACM Trans. Prog. Lang.

Syst. 15, 2, 253–289.

HINDLEY, R. 1997. Basic Simple Type Theory. Number 42 in Cambridge Tracts in Theoretical Computer Science. Cambridge University Press, London.

JIM, T. 1995. Rank 2 type systems and recursive definitions. Tech. Rep. MIT/LCS/TM-531, LCS, Massachusetts Institute of Technology.

JIM, T. 1996. What are principal typings and what are they good for? In POPL’96. ACM, 42–53.

JIM, T. 2000. A polar type system. In ICALP Workshops. Proceedings in Informatics, vol. 8.

Carleton-Scientific, 323–338.

KFOURY, A. J., TIURYN, J.,ANDURZYCZYN, P. 1993. Type reconstruction in the presence of polymor-phic recursion. ACM Trans. Prog. Lang. Syst. 15, 2, 290–311.

KFOURY, A. J.ANDWELLS, J. B. 1994. A direct algorithm for type inference in the rank-2 fragment of the second-order lambda -calculus. In LISP and Functional Programming ’94. ACM.

KFOURY, A. J.ANDWELLS, J. B. 1999. Principality and Decidable Type Inference for Finite-Rank Intersection Types. In POPL’99. ACM, 161–174.

LEIVANT, D. 1983. Polymorphic Type Inference. In POPL’83. ACM, 88–98.

LEPORATI, E. 2000. Intersezione al rank 2 per MiniOcaml. M.S. thesis, Dipartimento di Informat-ica, Universit `a di Torino.

MARGARIA, I.ANDZACCHI, M. 1995. Principal typing in a∀∧ discipline. J. Logic Comp. 5, 367–381.

MEERTENS, L. 1983. Incremental polymorphic type checking in B. In POPL’83. ACM, 265–275.

MILNER, R., TOFTE, M., HARPER, R.,ANDMACQUEEN, D. 1997. The Definition of Standard ML -Revised. MIT Press.

MILNER, R., TOFTE, M., HARPER, R.,ANDMACQUEEN, D. 1997. The Definition of Standard ML -Revised. MIT Press.

Nel documento Definitions and Conditional Expressions (pagine 36-51)

Documenti correlati