Principal Typing in Elementary Affine Logic

7 downloads 0 Views 210KB Size Report
Turing machines. ... will check its typability, assuring, in case of positive result, its complexity ... Each abstract EA-term represents an infinite set of EA-terms, in such a way ... in the sense that we show two algorithms, the first one building, for every .... M has no subterm of the form [M1]M2=x,y where (M2)∗ is not a variable,. 2.
Principal Typing in Elementary Affine Logic⋆ Paolo Coppola1 and Simona Ronchi della Rocca2 1

Dipartimento di Matematica e Informatica - Universit` a di Udine via delle Scienze 206, I-33100 Udine, Italy [email protected] 2 Dipartimento di Informatica - Universit` a di Torino Corso Svizzera 185, I-10149 Torino, Italy [email protected]

Abstract. Elementary Affine Logic (EAL) is a variant of the Linear Logic characterizing the computational power of the elementary bounded Turing machines. The EAL Type Inference problem is the problem of automatically assigning to terms of λ-calculus EAL formulas as types. This problem is proved to be decidable, and an algorithm is showed, building, for every λ-term, either a negative answer or a finite set of type schemata, from which all and only its typings can be derived, through suitable operations.

Introduction Starting from a seminal idea of Girard, who designed the first variant of Linear Logic normalizable in polynomial time, the Light Linear Logic [10], some logical systems have been proposed in the literature, characterizing different complexity classes. In this paper we deal with Elementary Affine Logic (EAL), which characterizes the computational power of the elementary bounded Turing machines: this result has been proved in [9] for its second order version and moreover it has been extended to the propositional fragment in [1]. In [4] a language has been designed, ΛEA , starting from the implicational fragment of Elementary Affine Logic. The interest of such a language deals not only in its computational bound, but in the fact that EAL-typeable terms can be reduced with the abstract subset of Lamping’s optimal reduction algorithm [11] obtaining excellent performances. ΛEA is untyped and it can be assigned types which are formulas of EAL through a type assignment system in natural deduction style (NEAL), proving statements (typings) of the shape Γ ⊢NEAL M : A, where the context Γ assigns types to variables. However the language, as almost all designed in this way, is quite complex, both as syntax and as reduction rules. Indeed its syntax reflects the inference rules, and its reduction rules reflect all the normalization steps, which are quite involved. So it cannot be proposed as paradigm for a real programming language. Then it would be interesting to have a decidable procedure for assigning EAL formulas as types to terms of the standard λ-calculus: so ⋆

Paper partially supported by MIUR-cofin-2000 “Linear Logic and Beyond”,EC-TMR project “Linear Logic”,IST-2001-33477 DART Project,MIUR-cofin-2001 COMETA

the user can write a program in the usual style, and the compiler automatically will check its typability, assuring, in case of positive result, its complexity bound and enabling it to be reduced with the abstract Lamping’s algorithm. In this paper we define such a procedure. The problem is not easy, since there is not a type inference system for the λ- calculus. But it is easy to define a correspondence between ΛEA and the λcalculus, which preserves the operational behavior of terms, just by performing the substitutions that are implicit in terms of ΛEA . Then the typability of λterms can be defined in the following way: a term M ∈ Λ can have a typing Γ ⊢NEAL M : A if and only if there is a term M ′ of ΛEA such that it corresponds to M , and Γ ⊢NEAL M ′ : A. But in order to reach our aim, which is to check typability in order to reduce λ-terms with the Lamping’s abstract algorithm, we need to restrict the previous definition, by taking into account only simple terms of ΛEA , i.e., terms whose proofnets correspond to those obtained as initial translation of λ-terms. But it is important to point out that the result of this paper would remain true even without this restriction. The number of terms of ΛEA corresponding to a λ-term is in general infinite (thanks to the possibility of adding boxes just replacing variables by variables), and so at a first look the problem of typability for the λ-calculus seems at most semi-decidable. In [4] a procedure has been designed for assigning to a λ-term, that can be typed in the Curry’s type assignment system [5–7], a type of EAL: the procedure takes as input a pair of a term and a Curry’s type that can be assigned to it, and produces as output either a negative result or a typing, assigning to the term itself an EAL type related to the Curry type in input, in the sense that it expresses the same functionality behavior. In this paper we give a stronger solution of the problem, by designing a modular type inference procedure for λ-calculus. The procedure is given in three steps. First we introduce the language of abstract EA-terms, terms in a meta-language, to which EAL types can be assigned through a suitable type assignment system. Each abstract EA-term represents an infinite set of EA-terms, in such a way that it can be assigned all and only the typings of the EA-terms in the set. We prove that all typing information of the infinite set of EA-term corresponding to a given λ-term can be codified in a finite set of canonical form, i.e., abstract EA-terms in normal form with respect to a suitable reduction relation preserving typings. Canonical forms give information on the box structure of the typing. Such structure was only implicit in [4]. Moreover we prove that typing for abstract EA-terms is decidable, in the sense that every abstract EA-term has a principal typing scheme, from which all and only the typings for it can be derived, through suitable operations. So we can deduce that a λ-term has all and only the typings of the canonical forms corresponding to it, and, being this set finite, it has a finite number of principal typings. All the proofs are constructive, in the sense that we show two algorithms, the first one building, for every λ-term, all and only the canonical forms corresponding to it, and the second one giving, for every abstract EA-term, either a negative answer or its principal typing. The notion of principal typing we found is not standard. Indeed, the operations that

allow to obtain types from schemata are substitutions under some constraints, represented by linear equations. Another example of type derivation for the λ-calculus, in a bounded complexity logic, is that one of Baillot [2] and [3], assigning types in Light Affine Logic to λ-terms. Here the main problem is to deal with two different modal operators. Related works are in [12, 8], in the field of linear decorations of intuitionistic derivations. The paper is organized as follows. In Section 1 the λEA -calculus and the type assignment system ⊢NEAL are recalled. Section 2 presents the abstract EAterms, their canonical forms, and their relation with Λ. In Section 3 the principal typing property for abstract EA-terms is proved. In Section 4 the principal typing property for the λ-calculus is stated. The appendix contains, in its section A.1, the algorithm building the set of canonical forms corresponding to a given λterm, and in the section A.2 the principal typing algorithm.

1

The λEA-calculus

In this section we will briefly recall the λEA -calculus, as defined in [4], its typing rules, its relation with the λ-calculus, and the problem of typability of λ-calculus. EA Definition 1. i) The set of terms of the λEA -calculus ) is generated by M (Λ M the grammar: M ::= x | λx.M | (M M ) | ! (M ) /x, . . . , /x | [M ]M=x,y where all variables occur at most once. ΛEA is ranged over by M, N, P, Q. ii) EA-types are formulas of Elementary Affine Logic, and they are generated by the grammar A ::= a | A ⊸ A |!A where a belongs to a countable set of basic type constants. EA-types will be ranged over by A, B, C. iii) The type assignment system ⊢NEAL assigns EA-types to EA-terms, starting from a context, assigning EA-types to variables. The rules of the system are given in Table 1.

Γ, x : A ⊢NEAL x : A

ax

Γ, x : A ⊢NEAL M : B (⊸ I) Γ ⊢NEAL λx.M : A ⊸ B ∆1 ⊢NEAL M1 :!A1

Γ ⊢NEAL M :!A ∆, x :!A, y :!A ⊢NEAL N : B contr Γ, ∆ ⊢NEAL [N ]M =x,y : B Γ ⊢NEAL M : A ⊸ B ∆ ⊢NEAL N : A (⊸ E) Γ, ∆ ⊢NEAL (M N ) : B

∆n ⊢NEAL Mn :!An x1 : A1 , . . . , xn : An ⊢NEAL N : B ˆ ˜ ! Γ, ∆1 , . . . , ∆n ⊢NEAL ! (N ) M1 /x1 , . . . , Mn /xn :!B ···

Table 1. Type assignment system for EA-terms.

The following definition relates terms of ΛEA with the terms of the classical λ-calculus.

Definition 2. i) The set of terms of the λ-calculus (Λ) are defined by the grammar M ::= x | M M | λx.M . By abuse of notation, Λ will be ranged over by M, N, P, Q, as ΛEA , being the different meaning clear from the context. ii) The translation function ()∗ : ΛEA → Λ is defined by induction on the structure of the EA-term as follows: (x)∗ = x

(λx.M )∗ = λx.M ∗

((M1 M2 ))∗ = (M1∗ M2∗ )

(!(M )[M1 /x1 , · · · , Mn /xn ])∗ = M ∗ {M1∗ /x1 , . . . , Mn∗ /xn } ([M ]N =x1 ,x2 )∗ = M ∗ {N ∗ /x1 , N ∗ /x2 } In order to extend to Λ the notion of type assignment with EA-types, let us introduce a particular class of EA-terms, the simple ones, which correspond, if translated into proofnets, to contracting at most variables. We discard EA-terms contracting non trivial subterms otherwise we loose the possibility of reducing lambda terms with the abstract Lamping’s algorithm. Definition 3. i) The length L(M ) of an EA-term M is defined inductively as follows: L(x) = 0; L(λx.M ) = 1 + L(M ); L((M  Pn N )) = 1 + L(M ) + L(N ); L(! (M ) M1 /x1 , . . . , Mn /xn ) = L(M ) + i=1 L(Mi ); L([M ]N =x,y ) = L(M ) + L(N ). ii) An EA-term M is simple if and only if 1. M has no subterm of the form [M1 ]M2 =x,y where (M2 )∗ is not a variable, 2. L(M ) = L((M )∗ ) Condition ii)2. is not redundant. Indeed M ≡!([(x1 (x2 y))]x=x1 ,x2 )[(z does not violate condition ii)1. but L(M ) = L((M )∗ ) − 1.

w)

/x, k /y]

Definition 4. Let M ∈ Λ and let EAM = {N | N ∈ ΛEA , N is simple and (N )∗ = M }. Then Γ ⊢NEAL M : A if and only if Γ ⊢NEAL P : A, for some P ∈ EAM . EAM is the set of EA-terms corresponding to M . Remark 1. It can be easily checked that the condition to be simple is not preserved under reduction. Consequently, the notion of typability we give for the λ-calculus does not enjoy the subject reduction property. But this lack does not invalidate our result. As a matter of fact, we want to use the notion of typability just as an initial check: in fact, if a term can be typed, then it can be evaluated with the abstract Lamping’s algorithm. Since the algorithm reduces untyped terms, subject reduction is not needed. Finally notice that if we relax the constraint about simple terms it is no more assured that the term can be reduced with the abstract Lamping’s algorithm. The cardinality of EAM is always infinite, for all M ∈ Λ, thanks to the possibility of replacing variables by variables in (!) subterms. In the following example, an infinite set of terms of ΛEA , all corresponding to the λ-term λxy.(x(xy)), is showed. Note that the replacement of variables by variables in (!) subterms cannot be avoided, since it is necessary for writing EA-terms corresponding to non linear λ-terms.

Example 1. For every n ≥ 0, the EA-term 2n , defined as: λx.[λy. !(!(...!(x3 (x4 y1 ))[x5 /x3 , x6 /x4 , y2 /y1 ]...)[x1 /x2n−1 , x2 /x2n , y /yn ]]x=x1 ,x2 | {z } n+1

belongs to EAλxy.(x(xy)) .

So, at a first look, the problem of EA-typability for the λ-calculus seems to be semi-decidable. But we will see in the following Section that for every M , EAM has a finite representation.

2

Abstract EA-terms and Canonical Forms

In this section the notion of abstract EA-terms, and a type assignment for assigning EA-types to them are introduced. The notion of abstract EA-term allows for a finite representation of the set EAM , for every M ∈ Λ. In fact, an abstract EA-term M describes an infinite set of EA-terms, which can differ from each other in the number of nested occurrences of both the ! constructor and the contraction constructor. Moreover a reduction relation on abstract EA-terms is defined, which allows to choose a representative of each equivalence class of abstract EA-terms, in such a way that the typings of the terms belonging to the set EAM , for every M , are all and only the typings of a finite set of abstract EA-terms in normal forms (called canonical forms). Definition 5. The set AbsEA of abstract EA-terms is generated by the following grammar: N1 /x1 , · · · , Nk /xn ] M ::= x | λx.M | (M M ) | [M ]N1 →(c x1 ),...,Nk →(c xk ) | ∇(M )[

with the condition that every variable occurs at most once in a term. The type assignment system assigning formulas of EAL to terms of AbsEA is shown in Table 2. Note that the main differences between this system and that one for ΛEA are that the rule (!m ) is parametric, in the sense that it can assign a type having any number m > 0 of modalities, and the rule (contr) allows for contracting simultaneously any set of different variables. The relation between abstract EA-terms and EA-terms is expressed by the two functions emb : ΛEA → AbsEA and bme : AbsEA → P(ΛEA ) defined in the following. emb(x) = x emb(λx.M ) = λx.emb(M ) emb((M1 M2 ) = (emb(M1 ) emb(M2 )) emb([M ]N =x,y ) = [emb(M )]emb(N )→(x,y) N1

emb(!(M )[

/x1 , · · · , Nk /xk ]) = ∇(emb(M ))[emb(N1 ) /x1 , · · · , emb(Nk ) /xk ]

Γ, x : A ⊢abs x : A

ax

Γ, x : A ⊢abs M : B Γ ⊢abs M : A ⊸ B ∆ ⊢abs N : A (⊸ I) (⊸ E) Γ ⊢abs λx.M : A ⊸ B Γ, ∆ ⊢abs M N : B Γ1 ⊢abs N1 :!A1 .. . Γk ⊢abs Nk :!Ak

x11 :!A1 , . . . , x1n1 :!A1 , .. . ∆, xk1 :!Ak , . . . , xknk :!Ak ⊢abs M : B

Γ1 , . . . , Γk , ∆ ⊢abs [M ]N1 →(x1 ,··· ,x1n 1

1

k ),...,Nk →(xk 1 ,··· ,xn )

contr

:B

k

m

∆1 ⊢abs .. .

z}|{ N1 : !...! A1 m

∆k ⊢abs

z}|{ Nk : !...! Ak

x1 : A1 , . . . , xk : Ak ⊢abs M : B N1

Γ, ∆1 , . . . , ∆k ⊢abs ∇(M )[

/x1 , · · · ,

Nk

m>0

/xn ] : |{z} !...! B

!m

m

Table 2. Type assignment system for AbsEA -terms.

bme(x) = {x} bme(λx.M ) = {λx.M ′ |M ′ ∈ bme(M )} bme((M1 M2 ) = {(M1′ M2′ )|M1′ ∈ bme(M1 ) ∧ M2′ ∈ bme(M2 )} n−1

n

N1

bme ∇(M )[

/x1 , · · ·

Nk

z }| { ′ z }| {  /xk ] = {!(· · ·!( M ′ )[z11 /x1 , . . . , zk1 /xk ] ) · · · )[N1 /z1n , . . . ′

. . . , Nk /zkn ] |M ′ ∈ bme(M ) ∧ Ni′ ∈ bme(Ni ) ∧ n > 0} h  i bme([M ]N →(x11 ,x21 ,...,xn1 1 ),··· ) = bme [M ]y→(x11 ,x21 ) n1 3 N →(y,x1 ,...,x1 ),···

y fresh variable h i bme([M ]N1 →(x11 ,x21 ),N2 →(c [M ]N1 →(x11 ,x21 ) x2 ),··· ) = bme

N2 →(c x2 ),···



bme([M ]N →(x1 ,x2 ) ) = {[M ]N ′ =x1 ,x2 |M ∈ bme(M ) ∧ N ′ ∈ bme(N )} ′



bme([M ]N →(x) ) = bme(M {N /x}). The following lemma holds: Lemma 1. i) ∀M ∈ ΛEA ii) ∀M ∈ AbsEA

Γ ⊢NEAL M : A ⇒ Γ ⊢abs emb(M ) : A.

Γ ⊢abs M : A ⇒ ∃M ′ ∈ bme(M ) Γ ⊢NEAL M ′ : A.

Example 2. bme(λx.[λy.(∇(x3 (x4 y1 )))[x1 /x3 , x2 /x4 , y/y1 ]]x→x1 ,x2 ) = {2n | n ≥ 1}, where 2n is defined in Example 1. The reduction relation →Can , defined in Figure 1, is the operational counterpart of some commuting conversions on proofs in NEAL. In fact, let us consider the two abstract EA-terms: X ≡ λxy.[[(yx1 x2 x3 )]z→(x1 ,x2 ) ]x→(z,x3 ) and Y ≡ λxy.[(yx1 x2 x3 )]x→(x1 ,x2 ,x3 ) . X and Y represent the same set of EA-terms, and they have the same types. Indeed, type derivations for X and Y differ just in the fact that the contraction of x1 , x2 , x3 into x is made in two steps for X and in one step for Y . So we choose Y as representative of both terms, and we put X →Can Y . Moreover, let consider the two terms: X ′ ≡ ∇(∇(M )[y1 /x1 , ..., yk /xk ])[N1 /y1 , ..., Nk /yk ] and Y ′ ≡ ∇(M )[N1 /x1 , ..., Nk /xk ]. bme(X ′ ) ⊆ bme(Y ′ ), and for every type derivation for X ′ there is a type derivation for Y ′ , both deriving a type of the shape |{z} !...! A, for some A. The two m

derivations differ just in the fact that, while the m modalities are introduced for Y in one application of the rule !m , the derivation for X uses two applications of the same rule. So we put X ′ →Can Y ′ . The other rules composing →Can are motivated by similar reasonings. It is easy to prove that →Can is confluent. Lemma 2 (Subject reduction). Let M ∈ AbsEA and M →∗Can N , then Γ ⊢abs M : A ⇒ Γ ⊢abs N : A. Proof. By induction on the length of the reduction. Let us call canonical forms the EA-terms in normal forms with respect to the reduction →Can . Notice that the normal form of a simple EA-term is simple too. Lemma 3. For every M ∈ Λ, let C(M ) = {N | N is canonic and simple and (N )∗ = M }. Then B ⊢NEAL M : σ if and only if B ⊢abs P : σ, for some P ∈ C(M ). Proof. (only if) B ⊢NEAL M : σ implies, by definition, B ⊢NEAL N : σ, for some N ∈ EAM , which in its turn implies, by Lemma 1.i), B ⊢abs emb(N ) : σ. So, if P is the canonical form of emb(N ), B ⊢abs P : σ, by Lemma 2. By definition, P ∈ C(M ). (if) B ⊢abs P : σ, for some P ∈ C(M ), implies, by Lemma 1.ii), there is N ∈ bme(P ) such that B ⊢NEAL N : σ. Note that N is simple by definition of C(M ). So B ⊢NEAL (N )∗ : σ, and, since M = (N )∗ , the proof is given. The following lemma proves the key property for the decidability of EAtyping for λ-terms.

Lemma 4. For every M ∈ Λ, C(M ) is finite. The proof is constructive, since we will show an algorithm C such that, for every M ∈ Λ, C(M ) build C(M ), and we prove that C is correct and complete. All this stuff is showed in the Appendix A.1.

3

Principal Typing for ⊢abs

In this section we will prove that ⊢abs enjoys the principal typing property, i.e., every typing can be derived from a particular typing schemata by means of suitable operations. First of all, let us introduce the notion of type scheme. Definition 6. i) Type schemata are defined by the following grammar: σ ::= α | σ ⊸ σ |!p (σ) where the exponential p is defined by the following grammar: p ::= n | p + p where α belongs to a countable set of scheme variables, ranged over by α, β, γ, and n belongs to a countable set of literals; type schemata are ranged over by σ, τ, ρ and exponentials are ranged over by p, q, r. Let T denote the set of type schemata. ii) A scheme substitution is a function from type schemata to types, denoted by a pair of substitutions < S, X >, where S replaces scheme variables by types and X replaces literals by natural numbers. The application of < S, X > to a type scheme is defined inductively as follows: < S, X > (α) = S(α); < S, X > (σ ⊸ τ ) =< S, X > (σ) ⊸< S, X > (τ ); !...! < S, X > (σ), < S, X > (!n1 +...+ni σ) = |{z} q

where q = X(n1 ) + ... + X(ni ).

≡ denotes the syntactical identity between both types and type schemes. In order to define the principal typing, we need a unification algorithm for type schemes. But first some technical definitions are necessary. First we introduce a function F collapsing all consecutive !p in a type scheme: F (α) = α; F (σ ⊸ τ ) = F (σ) ⊸ F (τ ); F (!p (σ)) =!p F (σ) if F (σ) is not of the form !q τ. F (!p (σ)) =!p+q (τ ) if F (σ) =!q τ.

In the following we assume that type schemes are already transformed by F , i.e. it is not the case that a subterm !p (!q (σ)) occurs in a type scheme. Moreover, let =e be the relation between type schemes defined as follows: α =e α; σ =e µ and τ =e ν imply σ ⊸ τ =e µ ⊸ ν; σ =e τ implies !p σ =e !q τ . Roughly speaking, two type schemes are =e if and only if they are identical modulo the exponentials. The unification algorithm, which we will present in SOS style, is a function U from T × T to pairs of the shape < C, s >, where C (the modality set) is a set of natural linear constraints, in the form p = q, where p and q are exponentials, and s is a substitution, replacing scheme variables by type schemes. A set C of linear constraints is solvable if there is a substitution X from literals to natural numbers such that, for every constraint n1 + ... + ni = m1 + ... + mj in C, X(n1 ) + ... + X(ni ) = X(m1 ) + ... + X(mj ). Clearly the solvability of a set of linear constraints is a decidable problem. U (α, α) =< ∅, [] > α is a scheme variable not occurring in τ U (α, τ ) =< ∅, [α 7→ τ ] > α is a scheme variable not occurring in σ U (σ, α) =< ∅, [α 7→ σ] > U (ρ, µ) =< C, s > U (! ρ, !q µ) =< C ∪ {p = q}, s > p

U (σ1 , τ1 ) =< C1 , s1 > U (s1 (σ2 ), s1 (τ2 )) =< C2 , s2 > U (σ1 → σ2 , τ1 ⊸ τ2 ) =< C1 ∪ C2 , s1 ◦ s2 > In all other cases, U is undefined: for example both U (α, α ⊸ β) and U (!p α, σ ⊸ τ ) are undefined. The following lemma proves that U is the most general unifier for type schemes, with respect to =e . Lemma 5. i) (correctness) U (σ, τ ) =< C, s > implies s(σ) =e s(τ ). ii) (completeness) s(σ) =e s(τ ) implies U (σ, τ ) =< C, s′ > and s = s′ ◦ s′′ , for some s′′ . Proof. (correctness) By induction on the rules deriving U (σ, τ ) =< c, s >. (completeness) By induction on the pair (number of symbols of σ, number of scheme variables occurring in σ). The following lemma extends the behavior of U to scheme substitutions: it is the key lemma for proving the correctness of our principal typing result. Lemma 6. Let < S, X > be a scheme substitution such that < S, X > (σ) ≡< S, X > (τ ). Then U (σ, τ ) =< C, s >, and there is < S ′ , X ′ > such that X ′ is a solution of C and < S ′ , X ′ > (s(σ)) ≡< S, X > (σ).

Proof. By induction on the pair (number of symbols of σ, number of scheme variables occurring in σ). Using U , we can now design an algorithm, P T , that, for every abstract EAterm M , either gives a negative answer or build a triple < Θ, σ, C >, where Θ is a scheme context, i.e. a finite set of scheme assignments, σ is a type scheme and C is a set of constraints. The algorithm P T is showed in the Appendix A.2. For every term M ∈ AbsEA , P T (M ), provided it exists, is the principal typing of M , in the sense that all and only the typings for M can be derived from it through scheme substitutions, as stated in the next theorem. In the statement of the theorem, we will denote by < S, X > (Θ), where Θ is a scheme context, the scheme context {x :< S, X > (σ) | x : σ ∈ Θ}. Theorem 1 (Principal typing for AbsEA ). (correctness) P T (M ) =< Θ, σ, C > implies, for all scheme substitution < S, X > such that X satisfies C, < S, X > (Θ) ⊢abs M :< S, X > (σ). (completeness) Γ ⊢abs M : A implies P T (M ) =< Θ, σ, C > and there is a scheme substitution < S, X > such that X satisfies C, Γ ⊇< S, X > (Θ) and A ≡< S, X > (σ). Proof. (correctness) By induction on M , using the fact that the derivations are syntax directed. (completeness) By induction on the derivation of Γ ⊢abs M : A, using Lemma 6. Example 3. Let M be the abstract term λx.[λy.∇(x1 (x2 t))[z1 /x1 , z2 /x2 , y/t]x→(z1 ,z2 ) . P T (M ) =< ∅, !n (α ⊸ α) ⊸!n α ⊸!n α, ∅ > . The principal typing property can be extended in an obvious way to all the terms of ΛEA , in the sense that an EA-term M has the principal typing of the abstract term emb(M ) with the additional constraints n = 1 for any exponential n.

4

Type Inference for λ-calculus

In this section we prove the main result of this paper, namely that a term of the λ-calculus is typeable in the type assignment system ⊢NEAL , if and only if it has a finite number of principal typing schemes. This result is an easy consequence of the two ones we proved in the previous sections: the principal typing property for the abstract EA-terms, and the fact the number of canonical forms associated to every λ-term is finite. So we can prove that a λ-term M has all and only the principal typings of the canonical forms belonging to the set C(M ). Theorem 2 (Principal typing for Λ in EAL). ∀M ∈ Λ, Γ ⊢NEAL M : A if and only if P T (N ) =< Θ, σ, C > and Γ ⊇< S, X > (Θ), A =< S, X > (σ), for some scheme substitution < S, X > such that X satisfies C and for some N ∈ C(M ).

Proof. The proof is an easy consequence of Lemma 3 and of Theorem 1. Let us define the following erasure function E transforming EA-types (normalized by the function F defined in the previous section) into Curry’s types: E(α) = α; E(!p σ) = E(σ); E(σ ⊸ τ ) = E(σ) → E(τ ). Assume that a term M ∈ Λ be typeable in the type assignment system ⊢NEAL , and consequently in the Curry’s type assignment system ⊢C . Since ⊢C has the principal typing property, let < Γ, σ > be its principal typing in this system, where Γ is a context and σ a Curry’s type. Then for each < Θ, τ, C >∈ P T (M ), E(Θ) = Γ and E(τ ) = σ (where E(Θ) is the distribution of E on all type schemes occurring in Θ). Example 4. Let M be the λ-term λx.λy.(x(x y)). The set C(M ) contains 24 elements and only six of them are typeable in EAL. They are: – λx.[λy.∇((x3 (x4 y1 )))[x1 /x3 , x2 /x4 , y /y1 ]]x→(x1 ,x2 ) with P T = h∅, !n (α ⊸ α) ⊸!n α ⊸!n α, ∅i; – λx.[∇(λy.(x3 (x4 y)))[x1 /x3 , x2 /x4 ]]x→(x1 ,x2 ) with P T = h∅, !n (α ⊸ α) ⊸!n (α ⊸ α), ∅}i; – λx.[∇(λy.∇((x5 (x6 y1 )))[x3 /x5 , x4 /x6 , y /y1 ])[x1 /x3 , x2 /x4 ]]x→(x1 ,x2 ) with P T = h∅, !n1 (α ⊸ α) ⊸!n2 (!n3 α ⊸!n3 α), {n1 = n2 + n3 }i; – ∇(λx.[λy.∇((x3 (x4 y1 )))[x1 /x3 , x2 /x4 , y /y1 ]]x→(x1 ,x2 ) )[ ] with P T = h∅, !m (!n (α ⊸ α) ⊸!n α ⊸!n α), ∅i; – ∇(λx.[∇(λy.(x3 (x4 y)))[x1 /x3 , x2 /x4 ]]x→(x1 ,x2 ) )[ ] with P T = h∅, !m (!n (α ⊸ α) ⊸!n (α ⊸ α)), ∅i; – ∇(λx.[∇(λy.∇((x5 (x6 y1 )))[x3 /x5 , x4 /x6 , y /y1 ])[x1 /x3 , ,x2 /x4 ]]x→(x1 ,x2 ) )[ ] with P T = h∅, !m (!n1 (α ⊸ α) ⊸!n2 (!n3 α ⊸!n3 α)), {n1 = n2 + n3 }i;

References 1. Asperti, A., Coppola, P., Martini, S.: (Optimal) duplication is not elementary recursive. In: Proceedings of the 27th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POLP-00), N.Y., ACM Press (2000) 96–107 2. Baillot, P.: Checking Polynomial Time Complexity with Types. In: Proc. of 2nd IFIP International Conference on Theoretical Computer Science (TCS 2002). Volume 223 of IFIP Conference Proceedings., Montr´eal, Qu´ebec, Canada (2002) 370– 382 3. Baillot, P.: Type inference for polynomial time complexity via constraints on words. Prepublication LIPN Universite Paris Nord (2003) 4. Coppola, P., Martini, S.: Typing Lambda Terms in Elementary Logic with Linear Constraints. In Abramsky, S., ed.: Proc. of Typed Lambda Calculi and Applications, 5th International Conference, TLCA 2001. Volume 2044 of Lecture Notes in Computer Science., Springer (2001) 76–90 5. Curry, H.B.: Functionality in combinatory logic. In: Proc. Nat. Acad. Science USA. Volume 20. (1934) 584–590

6. Curry, H.B., Feys, R.: Combinatory Logic, Volume I. Studies in Logic and the Foundations of Mathematics. North-Holland, Amsterdam (1958) 7. Curry, H.B., Hindley, J.R., Seldin, J.P.: Combinatory Logic, Volume II. Studies in Logic and the Foundations of Mathematics. North-Holland, Amsterdam (1972) 8. Danos, V., Joinet, J.B., Schellinx, H.: On the linear decoration of intuitionistic derivations. In: Archive for Mathematical Logic. Volume 33. (1995) 387–412 9. Danos, V., Joinet, J.B.: Linear Logic and Elementary Time. ICC’99, to appear in Information & Computation (1999) 10. Girard, J.Y.: Light linear logic. Information and Computation 204 (1998) 143–175 11. Lamping, J.: An algorithm for optimal lambda calculus reduction. In ACM, ed.: POPL ’90. Proceedings of the seventeenth annual ACM symposium on Principles of programming languages, January 17–19, 1990, San Francisco, CA, New York, NY, USA, ACM Press (1990) 16–30 12. Schellinx, H.: The Noble Art of Linear Decorating. PhD thesis, Institute for Logic, Language and Computation, University of Amsterdam (1994)

A A.1

Appendix The algorithm C

Notation. Let L(M ) be the linearization of M with respect to all its free variables and let Lx (M ) be the set of fresh variables generated by L during the linearization of x in M . I.e. if M = (x(xyy)) then L(M ) = (x1 (x2 y1 y2 )) and Lx (M ) = {x1 , x2 }, Ly (M ) = {y1 , y2 } and Lz (M ) = ∅ for any other variable z. Moreover, if T1 and T2 are two sets of terms, then T1 @T2 is the set of all terms obtained by applying a term of T1 to a term of T2 , and, if R is any term constructor, RT1 is the set of terms obtained by applying R to every term in T1 . The algorithm uses three auxiliary functions T, F, F′ : Λ → P(AbsEA ) and is defined by the following equations: C(M ) = if ∃x1 , . . . , xk ∈i>1 FV(M )   then T(L(M )) ∪ F(L(M )) x1 →(Lx

1 (M)),...,xk →(Lxk (M))

else T(M ) ∪ F(M )

T(x) = {x}

  T(λx.M ) = if x ∈i>1 FV(M ) then λx. T(L(M )) ∪ F(L(M )) x→(L

x (M))

else λx.(T(M ) ∪ F(M ))   T((M N )) = T(M )@ T(N ) ∪ F(N ) F(x) = F′ (x) = ∅

 F(M 6= x) = F′ (M ) ∪ ∇ F′ (M {b z/FV(M )}) ∪ T(M {b z /FV(M )}) [FV(M )/b z]

F′ (M 6= x) = ∀A1 , . . . , An , P, n > 0 s.t. M =α P {A1 /y1 , · · · , An /yn } {y1 , . . . , yn+k } = FV(P ) P 6= x ∀1 ≤ i ≤ n Ai = (Ai1 Ai2 )   [ n+k c n+k n+k ′ ck [ k ∇ T(P {c z1k /y[ n+1 }) ∪ (F (P {z1 /yn+1 }) T(A1 )/y1 , · · · , T(An )/yn , yn+1 /z1

`

´

∇ ∇(M )[y1 /x1 ,··· ,yn /xn ] [M1 /y1 ,··· ,Mn /yn ]→Can ∇(M )[M1 /x1 ,...,Mn /xn ]

ˆ

[M ]

˜

→Can j j ni ),... ...,N →(z j ,...,z j ...,xi →(yd ,x ,z ,...,zm ),... j 1 k−1 i k+1 j

ˆ

ˆ

[M ]

[M ]...

˜

˜

j j j ni ,z j ...,Nj →(z ,...,z ,yd ,...,zm ),... 1 k−1 k+1 j

c ni ),... Slist →Can N1 →(x1 ),...,Ni →(xd

ˆ

[M ]

˜

d n d d c N1 →(x1 ),...,Ni−1 →(xi−1 ),Ni+1 →(xi+1 ),... Slist,Ni →(x i )

if FV(Ni ) ∩ CV(Slist) = ∅. ∇(M )[M1 /x1 ,··· ,

[

∇(N ) P1 /y1 ,...,Pm /ym

] /xi ,··· ,Mn /xn ]→Can ∇(M {N/xi })[M1 /x1 ,··· ,P1 /y1 ,··· ,Pm /ym ,···Mn /xn ]

∇(M )[··· ,

[Mi ]

d c N1 →(y1 ),...,Nk →(yk ) /x

i ,··· ]→Can » – c c 1 c 1 k d k ∇(M )[··· ,Mi {z /y ,...,z /y } /xi ,··· ]

ˆ

∇([M ]...,x→(b y/b x,··· ] x),... )[··· ,y/x,··· ]→Can ∇([M ]... )[··· ,b [M ]

...,[N ]

→Can ci d →(x ),... c P1 →(y1 ),...,Pk →(yk ) »

([M ]

d c M1 →(x1 ),...,Mk →(xk )

[M ]

˜

c c N1 →(z 1 ),...,Nk →(z k )

y→(y) b



c d c c c ...,N {z 1 /y1 ,··· ,z k /yk }→(xi ),... P →(zc 1 ),...,P →(zc k) 1 k

h i c c c c 1 /x 1 ,...,y k /x k } N) N)→Can (M {y h

d c M1 →(y1 ),...,Mk →(yk )

i c c 1 c 1 k c k (M [N] d )→Can (M N{y /x ,...,y /x }) d c c N1 →(x1 ),...,Nk →(xk ) N1 →(y1 ),...,Nk →(yk )

λx.[M ]Slist →Can [λy.M ′ ]Slist′ where x ∈ / SFV(Slist) ∪ SSV(Slist) and [M ′ ]Slist′ = [M ]Slist {y/x} ∇(x)[M /x]→Can M

∇(M )[··· ,N /x,··· ]→Can ∇(M )[··· ] where x∈FV(M / ) [x]...,N →(...,x,...),... →Can N [M ]...,N →(...,x,...),... →Can [M ]...,N →(...),... where x∈FV(M / ) [M ]...,N →(x),... →Can [M {N/x}]...

Fig. 1. Reduction relation →Can for AbsEA terms.

n+k where zb and c z1k are fresh variables. c z1k /y[ n+1 stands for z1 /yn+1 , · · · , zk /yn+k , zb/FV(M ) stands for the complete renaming of free variables of M with fresh ones, and FV(M )/b z stands for the inverse substitution.

Fact 1. ∀C ∈ C(M )

(C)∗ = M

The proof of soundness and completeness of the algorithm C is based on the fact that the simple canonical forms can be generated by the grammar given in the next definition. The sets of free variables FV, banged variables BV and contracted variables CV used by the grammar, are defined in Figure 2. Definition 7 (CC EA ). The set of simple canonical EAL-terms CC EA is generated by the following grammar (CC is the starting symbol): CC ::= [K]Clist where CV(Clist) ⊆ FV(K) | K Clist ::= y → (b x) | y → (b x), Clist where |b x| ≥ 2 K ::= ∇(B)[b x/b y ] where BV(b x/b y) = FV(B) | B | x B ::= ∇ (B) [L] where BV(L) = FV(B) | R L ::= A/x | y/x, L | A/x, L R ::= λx.[K]x→(x1 ,...,xn ) where {x1 , . . . , xn } ⊆ FV(K) | λx.K | A A ::= (R K) | (x K) where all variables are linear, x b stands for x1 , . . . , xn and n > 0.

Notice that side condition |b x| ≥ 2 in the production of Clist implies [x]Clist is not a possible term in CC EA by side condition CV(Clist) ⊆ FV(K) in production of CC and by {x1 , . . . , xn } ⊆ FV(K) in production of R.

FV(x)

={x}

FV(λx.M )=FV(M )\{x} FV((M1 M2 ))=FV(M1 )∪FV(M2 ) FV([M ]Clist )=(FV(M )\CV(Clist))∪FV(Clist)

S FV(∇(M )[N1 /x1 ,··· ,Nk /xk ])= k i=1 FV(Ni )

FV(x→(x1 ,...,xn ),Clist)={x}∪FV(Clist) CV(x→(x1 ,...,xn ),Clist)={x1 ,...,xn }∪CV(Clist) BV(M/x,Blist)={x}∪BV(Blist)

Fig. 2. Free, Contracted and Banged Variables for EA-terms.

It is easy, but boring, to check that the grammar of definition 7 is correct and complete, in the sense that it generates all and only the simple canonical forms. Lemma 7. i) ∀M ∈ Λ C(M ) ⊆ CC EA . ii) N ∈ CC EA and (N )∗ = M implies N ∈ C((M )).

Theorem 3 (Soundness and Completeness of C). ∀M ∈ Λ 1. C(M ) ⊆ C(M ); 2. N ∈ C(M ) implies N ∈ C(M ). Proof. 1. By Lemma 7.i) for any C ∈ C(M ) we have C ∈ CC EA . Moreover (C)∗ = M by Fact 1. Then C is in C(M ). 2. By Lemma 7.ii). A.2

The algorithm P T

Let Let U (σ1 , σ2 , . . . , σn ) = let U (σ1 , σ2 ) =< C1 , s1 > and let U (s1 ◦ · · · ◦ si (σi+1 ), s1 ◦ · · · ◦ si (σi+2 )) =< Ci+1 , si+1 > in < C1 ∪ · · · ∪ Cn , s1 ◦ · · · ◦ sn > . The function P T is defined as follows. – P T (x) =< {x : α}, α, ∅ >, where α is fresh; – P T (λx.M ) = let P T (M ) =< B, σ, C > in if B = B ′ ∪ {x : τ } then < B ′ , τ ⊸ σ, C > else < B, α ⊸ σ, C > where α is fresh; – P T (M N ) = let P T (M ) =< B1 , σ1 , C1 > and P T (N ) =< B2 , σ2 , C2 > and let they be disjoint in let U (σ1 , σ2 ⊸ α) =< C, s > (α fresh) in < s(B1 ∪ B2 ), s(α), C ∪ C1 ∪ C2 >; – P T ([M ]N1 →(c x1 ),...,Nk →(c xk ) ) = let P T (M ) =< B, σ, C > and P T (Ni ) =< Bi , σi , Ci > (all disjoint and disjoint from P T (M )) and let U (B(x11 ), . . . , B(x1n1 ), σ1 , !m1 α1 ) =< C1′ , s1 > i+1 and U (s1 ◦ · · · ◦ si (B(xi+1 1 )), . . . , s1 ◦ · · · ◦ si (B(xni+1 )), ′ mi+1 αi+1 ) =< Ci+1 , si+1 > s1 ◦ · · · ◦ si (σi+1 ),! and s = s1 ◦ · · · ◦ sk S S S in< s(B/c x1 , ..., x ck ∪ 1≤j≤k Bj ), s(σ), C ∪ 1≤j≤k Cj ∪ 1≤j≤k Cj′ > . (where αi and mi are fresh and B/c x1 , ..., x ck denotes the context obtained from B by deleting the assignments to variables in xbi (1 ≤ i ≤ k) ) – P T (∇(M )[N1 /x1 , . . . , Nm /xm ]) = let P T (M ) =< B, σ, C > and P T (Ni ) =< Bi , σi , Ci > (all disjoint and disjoint from P T (M )) and let U (!n B(x1 ), σ1 ) =< C1′ , s1 > ′ and U (!n (s1 ◦ · · · ◦ si (B(xi+1 ))), s1 ◦ · · · ◦si (σi+1 )) =< Ci+1 , si+1 > and s =Ss1 ◦ · · · ◦ sm S S in (n fresh). where xij denotes the j-th component of xbi .

Note that P T is defined modulo names of type variables.

Suggest Documents