Retractions in Intersection Types
Mario Coppo, Mariangiola Dezani-Ciancaglini, Alejandro Díaz-Caro, Ines Margaria, Maddalena Zacchi
NN. Kobayashi (Ed.): Eighth Workshopon Intersection Types and Related Systems (ITRS 2016).EPTCS 242, 2017, pp. 31–47, doi:10.4204/EPTCS.242.5
Retractions in Intersection Types ∗ Mario Coppo Mariangiola Dezani-Ciancaglini Alejandro D´ıaz-Caro , Ines Margaria Maddalena Zacchi Dipartimento di Informatica Universit`a di Torino, corso Svizzera 185, 10149 Torino, Italy CONICET & Universidad Nacional de Quilmes, Roque S´aenz Pe˜na 352, B1876BXD Bernal, Buenos Aires, Argentina
This paper deals with retraction - intended as isomorphic embedding - in intersection types buildingleft and right inverses as terms of a l -calculus with a ⊥ constant. The main result is a necessaryand sufficient condition two strict intersection types must satisfy in order to assure the existence oftwo terms showing the first type to be a retract of the second one. Moreover, the characterisation ofretraction in the standard intersection types is discussed. Isomorphism of types has been first discussed in the seminal paper [7] and then studied in various typedisciplines [6, 8–11, 14–17, 23, 24]. Two types s and t in some typed calculus are isomorphic if there aretwo terms L and R of types t → s and s → t , respectively, such that the composition L ◦ R is equal to theidentity at type s and the composition R ◦ L is equal to the identity at type t .We claim that in programming practice and theory the notion of retraction between types plays acentral role, and it is more widespread than that of type isomorphism. Type s is a retract of type t insome typed calculus if there are two terms L and R of types t → s and s → t , respectively, such that thecomposition L ◦ R is equal to the identity at type s . Clearly L is right invertible and R is left invertible .The terms R and L are injective and surjective, respectively, on their domains. They are also called the coder and the decoder of type s in type t [20,22]. In fact, the term R encodes the values in s as elementsof t , while the term L decodes back from t to s , by returning the original values.To the best of our knowledge, type retraction as defined above has been discussed for Curry typesand higher-order types [7, 13, 20–22, 25, 26], but not for intersection types. Aim of this paper is to makea first step toward the filling of this gap. We consider the l ⊥ -calculus as given in [2, Definition 14.3.1].For this calculus the terms having left and right inverses have been characterised [19]. We reformulatethis characterisation in order to simplify the study of the types which can be derived for these terms.In particular we identify a class of right inverses (the simple right inverses ) such that if a term has aright inverse it has also a right inverse belonging to this class. All results in this paper are given for the l ⊥ -calculus but they hold, as well, for the l -calculus.We choose to investigate retraction for the essential intersection type assignment system introducedin [1]. This system has the same typeability power of the standard system [3] and a less permissive typesyntax. This restriction better fits the technical development of the present paper, as discussed in theConclusion. Nevertheless, we also provide a result for standard intersection types in the case where theright inverse is assumed to be a simple right inverse. ∗ Partially supported by EU H2020-644235 Rephrase project, EU H2020-644298 HyVar project, ICT COST Actions IC1201BETTY, IC1402 ARVI, IC1405 Reversible Computation, CA1523 EUTYPES, Ateneo/CSP project RunVar and STIC-AmSudproject FoQCoSS. µ is a retract of n and n is a retract of µ , then µ and n areequivalent with respect to the usual subtyping relation of intersection types. Then we discuss retractionin standard intersection types. Finally we define semantic retraction as the natural adaptation to modelsand we show that retraction and semantic retraction coincide. This proof uses the completeness of thefilter model given in [3]. Outline
Section 2 introduces the l ⊥ -calculus and characterises terms having left and right inverses.The essential intersection type assignment system is defined in Section 3 together with the character-isation of the types derivable for terms with left or right inverses. The results on retraction in strictintersection types are the content of Section 4. In Section 5 we extend the characterisation of retractionsto standard intersection types assuming that right inverses are simple. The notion of retraction is shownequivalent to that of semantic retraction in Section 6. Related work is overviewed in Section 7. Section 8discusses our choices and future work. Following [2, Definition 14.3.1], L ⊥ is the set of terms obtained by adding a constant ⊥ to the formationrules of l -terms. The terms of L ⊥ are generated by the syntax M :: = x | ⊥ | l x . M | MM where x ranges over a denumerable set of term variables.The reduction rules of the l ⊥ -calculus include the b -rule and two rules for ⊥ prescribing that bothapplication and abstraction of ⊥ reduce to ⊥ . ( l x . M ) N −→ M { N / x } ⊥ M −→ ⊥ l x . ⊥ −→ ⊥ The equality between terms is defined as b ⊥ -conversion, i.e. M = N means that there is a term P suchthat both M and N reduce to P .Let I = l x . x and M ◦ N = B MN , where B = l xyz . x ( yz ) . We are interested in investigating the monoidof terms with the combinator I as identity element and ◦ as binary operation. This naturally leads to thefollowing definition of left/right invertibility. Definition 2.1 (Left/right invertibility) .
1. A term M is left invertible if there exists a term L such that L ◦ M = I . We say that L is a left inverse of M.2. A term M is right invertible if there exists a term R such that M ◦ R = I . We say that R is a rightinverse of M. The left/right invertibility has been studied since the seventies. In particular the sets of terms havingat least one left or one right inverse have been characterised in [5] and [19].The characterisation of left invertible terms resorts to a set of head normal forms (hnfs for short). Werecall that a hnf is a term of the shape l x . . . x n . x j M . . . M m [2, Definition 2.2.11]. We define a set X ofhnfs and we show that a term is left invertible iff it reduces to a hnf belonging to X (Theorem 2.8)..Coppo,M.Dezani-Ciancaglini, A.D´ıaz-Caro, I.Margaria &M.Zacchi 33 Definition 2.2.
Let X be the set of hnfs inductively defined as follows: • l tx . . . x n . t ∈ X for n ≥ ; • if l tx . . . x n . M i ∈ X , then l tx . . . x n . x j M . . . M i . . . M m ∈ X , where ≤ j ≤ n and ≤ i ≤ m. Example 2.3. i) The term l tx . xx ( xt ) ∈ X because l tx . xt ∈ X , and in turn l tx . xt ∈ X since l tx . t ∈ X .ii) The term M = l tx x . x ( l x . t )( x t ) ∈ X since l tx x x . t ∈ X . We can also show that M ∈ X because l tx x . x t ∈ X and in turn l tx x . x t ∈ X since l tx x . t ∈ X . Point ii) of Example 2.3 shows that there is not, in general, a unique way to derive that a termbelongs to X . In the following, when we write M ∈ X , we refer to a particular proof, chosen according toDefinition 2.2.In order to build a left inverse of a term in X we need to “reach” an occurrence of the first abstractedvariable (called t in Definition 2.2). We use some machinery inspired by the B ¨ohm-out technique [2,§10.3]. In a hnf l x . . . x n . x j M . . . M m the number of initial abstractions is n , the variable x j (bound in the j -th abstraction) is the head variable and M , . . . , M m are the m components. Following the definition of X we can associate with each term in X a list of integer triples, whose first element is the abstraction positionof the head variable, whose second element is the number of components and whose third element is theposition of the component used to show that the term belongs to X (0 if the component is missing). Moreprecisely, using ⌢ to denote concatenation: Definition 2.4.
The path p ( M ) of the hnf M ∈ X is inductively defined by: • p ( l tx . . . x n . t ) = h , , i ; • p ( l tx . . . x n . x j M . . . M m ) = h j + , m , i i ⌢ p ( l tx . . . x n . M i ) if l tx . . . x n . M i ∈ X is used to show l tx . . . x n . x j M . . . M m ∈ X . Let p range over paths. Example 2.5.
We get p ( l tx . xx ( xt )) = h , , i ⌢ h , , i ⌢ h , , i . If M is defined as in Point ii) of Ex-ample 2.3 we get either p ( M ) = h , , i ⌢ h , , i or p ( M ) = h , , i ⌢ h , , i ⌢ h , , i , according tothe proof used to show M ∈ X . The triple h j + , m , i i says that the variable bound in the j + i -thcomponent out of m components to “reach” an occurrence of t . Then the variable bound in the j + S ( m ) i = l y . . . y m . y i . We call selectors the terms of the shownshape. This replacement becomes problematic if we have in the same path two triples with the samefirst element which differ in one of the other elements. Following [19] we differentiate these occurrencesusing terms of the shape P ( m ) = l z . . . z m + . z m + z . . . z m with m ≥ permutators . Weconvene that P ( ) = ⊥ . We need two preliminary definitions and a technical lemma.By ℓ ( p ) we denote the length, i.e. the number of triples, of the path p .We define ( j + , p ) as the maximum of the second components of triples in path p whose firstcomponent is j +
1. We assume ( j + , p ) = j + p as first component. Moreformally: ( j + , h , , i ) = ( j + , h h , m , i i ⌢ p ) = ( max ( m , ( j + , p )) if h = j + , ( j + , p ) otherwise4 Retractions inIntersection Types Lemma 2.6.
Let M = l tx . . . x n . M ′ ∈ X and m j ≥ ( j + , p ( M )) for ≤ j ≤ n. Then l t . M ′ { P ( m ) / x } . . . { P ( m n ) / x n } = Q ∈ X and ℓ ( p ( Q )) = ℓ ( p ( M )) .Proof. The proof is by induction on the definition of X . If M ′ = l x n + . . . x q . t , then Q = l tx n + . . . x q . t .Let M ′ = l x n + . . . x q . x r M . . . M i . . . M m and M ∈ X since l tx . . . x q . M i ∈ X . Because m j ≥ ( j + , p ( M )) implies m j ≥ ( j + , p ( l tx . . . x q . M i )) for 1 ≤ j ≤ n , by induction we get l tx n + . . . x q . M i s = l tx n + . . . x q . Q ′ ∈ X where s is the substitution { P ( m ) / x } . . . { P ( m n ) / x n } . If r ≥ n + Q = l tx n + . . . x q . x r M s . . . M i − s Q ′ M i + s . . . M m s Otherwise M ′ s = l tx n + . . . x q . P ( m r ) M s . . . M i − s Q ′ M i + s . . . M m s = l tx n + . . . x q z m + . . . z m r + . z m r + M s . . . M i − s Q ′ M i + s . . . M m s z m + . . . z m r and we can take this last hnf as Q since l tx n + . . . x q . Q ′ ∈ X implies l tx n + . . . x q z m + . . . z m r + . Q ′ ∈ X .In all cases it is easy to verify that ℓ ( p ( Q )) = ℓ ( p ( M )) . Example 2.7.
Let M = l tx . M ′ where M ′ = xx ( xt ) . We get l t . M ′ { P ( ) / x } = l tz . z ( l z z z . z z z )( l z z . z tz ) = QWe have p ( Q ) = h , , i ⌢ h , , i ⌢ h , , i . Theorem 2.8.
A term has at least one left inverse if and only if it reduces to a hnf M in X .Proof.(If) Let M ∈ X . The proof is by induction on ℓ ( p ( M )) . Let n be the number of initial abstractions and p be the path of the term considered in the induction step. We build a left inverse of the shape l z . zL . . . L q , where q ≥ n and L l = ⊥ whenever ( l + , p ) = M = l tx . . . x n . t , then l z . z ⊥⊥ · · · ⊥ | {z } n is a left inverse of M .Let M = l tx . . . x n . x j M . . . M i . . . M m ∈ X since l tx . . . x n . M i ∈ X . We distinguish two cases. Inthe first case the construction of the left inverse using a selector is easy. In the second case wecompose M with a term N build out of permutators. The useful property is that N ◦ M = Q ∈ X and ℓ ( p ( Q )) = ℓ ( p ( M )) and Q satisfies the condition of case 1. We can then build a left inverse L of Q and L ◦ N is a left inverse of M . Case 1 : ( j + , p ( l tx . . . x n . M i )) =
0. By induction hypothesis l tx . . . x n . M i ∈ X has a left inverse l z . zL ′ . . . L ′ q and in this case L ′ j = ⊥ . Then l z . zL ′ . . . L ′ j − S ( m ) i L ′ j + . . . L ′ q is a left inverse of M . Case 2 : ( j + , p ( l tx . . . x n . M i )) = m j =
0. Let m l = ( l + , p ( M )) for 1 ≤ l ≤ j and N = l z . zP ( m ) . . . P ( m j ) By the proof of Lemma 2.6 N ◦ M = Q ∈ X , where Q is the hnf l tx j + . . . x n z m + . . . z m j + . z m j + M s . . . M m s z m + . . . z m j .Coppo,M.Dezani-Ciancaglini, A.D´ıaz-Caro, I.Margaria &M.Zacchi 35and s is the substitution { P ( m ) / x } . . . { P ( m j ) / x j } . Since z m j + does not occur in M i s , i.e. ( n − j + m j − m + , p ( l tx j + . . . x n z m + . . . z m j + . M i s )) = ℓ ( p ( Q )) = ℓ ( p ( M )) we can build a left inverse L of Q according to previous case. Then a leftinverse of M is L ◦ N . (Only if) Let us suppose, ad absurdum, that a term has a left inverse and it is unsolvable or its hnf doesn’tbelong to X . The first case is obvious. In the second case the hnf M has no path which satisfiesDefinition 2.4. Therefore, if M = l t . N , then there is no occurrence of t in N which is not appliedand such that it is always in components whose head variables are bound. The arguments of t cannot be erased by reduction and a free variable cannot be replaced in order to get t . So weconclude that M has no left inverse. Example 2.9.
Let M , Q be as in Example 2.7. The left inverse of Q built according to the lemma isL = l z . z ( l y y . y ) ⊥ ( l y y . y ) . According to the proof of previous theorem we get N = l z . zP ( ) . Then aleft inverse of M is L ◦ N = l z . zP ( ) ( l y y . y ) ⊥ ( l y y . y ) . The characterisations of terms having right inverses is easy.
Theorem 2.10.
A term has at least one right inverse if and only if its hnf is of the shape: l z . zM . . . M m .Proof.(If) A right inverse is l tx . . . x m . t . (Only if) An unsolvable term has no left inverse. Let suppose the hnf of a term be not of the shape l z . zM . . . M m . Then it must have more than one abstraction and/or the head variable must be afree variable. In the first case the initial abstractions and in the second case the head free variablecannot be eliminated using reductions. Example 2.11.
The term M of Example 2.7 is a right inverse of the term L ◦ N of Example 2.9. The rightinverse of L ◦ N built by the theorem is l tx x x . t. From the proof of Theorem 2.10 it is clear that if a term has a right inverse, then it has also a rightinverse of the shape l tx . . . x n . t , i.e. a selector S ( n + ) . We call simple right inverses the hnfs of this shape. The type system considered in this paper is a notational variant of the essential intersection assignmentintroduced in [1].The set of strict intersection types is defined by: µ : = j | w | s → µ s : = µ | s ∧ s where j ranges over type variables and w is a constant. We convene that µ , n range over strict intersectiontypes (either atomic or arrow types), while s , t , r range over intersections.Conventionally, we omit parentheses according to the precedence rule “ ∧ over → ” and we assume that → associates to the right. Intersections are considered modulo idempotence, commutativity and associa-tivity of ∧ . In this section and in the following one we use type as short for strict intersection type.A preorder relation ≤ , representing set inclusion, is assumed between types and intersections.6 Retractions inIntersection Types Definition 3.1.
Let ≤ be the minimal reflexive and transitive relation such that: s ≤ w w ≤ w → w s ∧ s ≤ s i ( i = , ) s ≤ t and s ≤ t imply s ∧ s ≤ t ∧ t s ≤ s and µ ≤ µ imply s → µ ≤ s → µ We write s ∼ t if s ≤ t and s ≥ t .A key property of this subtyping is the content of the following lemma, for a proof see [1]. Lemma 3.2. If s → µ ≤ t → n , then t ≤ s and µ ≤ n . The essential intersection type assignment system is defined by the typing rules of Table 1. Weassume that an environment associates intersections with a finite number of term variables. Let G rangeover environments. The subsumption rule uses the preorder of Definition 3.1. We write G ⊢ N : s with s = V i ∈ I µ i as short for G ⊢ N : µ i for all i ∈ I . ( Ax ) G , x : V i ∈ I µ i ⊢ x : µ j j ∈ I ( w ) G ⊢ M : w ( ≤ ) G ⊢ M : µ µ ≤ nG ⊢ M : n ( → I ) G , x : s ⊢ M : µ G ⊢ l x . M : s → µ ( → E ) G ⊢ M : s → µ G ⊢ N : sG ⊢ MN : µ Table 1: Typing RulesThe inversion lemma is as expected, for a proof see [1].
Lemma 3.3 (Inversion Lemma) .
1. If G ⊢ x : µ, then either µ ∼ w or x : s ∈ G and s ≤ µ.2. If G ⊢ ⊥ : µ, then µ ∼ w .3. If G ⊢ MN : µ, then G ⊢ M : s → µ and G ⊢ N : s .4. If G ⊢ l x . M : µ, then µ ∼ s → n and G , x : s ⊢ M : n . In this system types are preserved by b ⊥ -conversion [1]: Theorem 3.4 (Subject Conversion) . If G ⊢ M : µ and M = N, then G ⊢ N : µ. We say that a term inhabits a type if we can derive the type for the term starting from the emptyenvironment. A term inhabits a set of types if it inhabits all the types belonging to the set. Let t = V i ∈ I µ i :we say that s → t is inhabited if there exists a term which inhabits all the types s → µ i for i ∈ I .Inhabitation for intersection types has been shown undecidable in general [27], but decidable fortypes with rank less than or equal to 2 [28], when the rank of types and intersections is defined by:rank ( µ ) = ( max ( rank ( s ) + , rank ( n )) if µ = s → n and ∧ occurs in µ , ( s ∧ t ) = max ( , rank ( s ) , rank ( t )) .Coppo,M.Dezani-Ciancaglini, A.D´ıaz-Caro, I.Margaria &M.Zacchi 37In the following we characterise the types of left/right invertible terms. These characterisationsrequire inhabitation of some types and therefore they are effective only when these types are of rank atmost 2.We start defining inductively the set Q of left types which mimic the set X of hnfs, that is the con-struction of Q follows the construction of X . In the following definition s → s → . . . → s n → t ∈ Q ,where t = V i ∈ I µ i , is used as short for s → s → . . . → s n → µ i ∈ Q for all i ∈ I . Definition 3.5.
The set Q of left types is inductively defined by: • if s ≤ n , then s → s → . . . → s n → n ∈ Q ; • if s → s → . . . → s n → r i ∈ Q and s j ≤ r → . . . → r m → n for some j ≤ n and i ≤ m and s → s → . . . → s n → r l is inhabited for ≤ l ≤ m, then s → s → . . . → s n → n ∈ Q . Example 3.6. i) Let t = y ∧ ( j → j ′ ) ∧ ( y → j ′ → y ′ ) . We have j → t → y ′ ∈ Q since: – j → t → j ′ ∈ Q – t ≤ y → j ′ → y ′ and – both j → t → y and j → t → j ′ are inhabited.Moreover j → t → j ′ ∈ Q since: – j → t → j ∈ Q – t ≤ j → j ′ and – j → t → j is inhabited.The type j → t → y ′ can be derived for the term M of Example 2.7.ii) Let µ = ( j → y ) → y → y ′ and n = j → y . We have j → µ → n → y ′ ∈ Q since: – j → µ → n → y ∈ Q – µ ≤ µ and – both j → µ → n → n and j → µ → n → y are inhabited.Moreover j → µ → n → y ∈ Q since: – j → µ → n → j ∈ Q – n ≤ n and – j → µ → n → j is inhabited.The type j → µ → n → y ′ can be derived for the left invertible term l tx x . x x ( x t ) . We define the number of top arrows of a type as expected: ♭ ( j ) = ♭ ( w ) = ♭ ( s → µ ) = + ♭ ( µ ) It is useful to observe that if a type with at least n top arrows has an inhabitant, then this type has also aninhabitant with at least n initial abstraction. Lemma 3.7.
If type µ is inhabited and ♭ ( µ ) ≥ n, then there is M with at least n initial abstractions suchthat ⊢ M : µ. Proof. If µ ∼ w it is trivial. Otherwise the inhabitants of µ must have hnfs, see [1] for a proof. Let µ = s → . . . → s n → n and l x . . . x n ′ . x j M . . . M m be an inhabitant of µ with n ′ < n . It is easy to checkthat we get M by h -expansion (see [2, Definition 3.3.1]) ⊢ l x . . . x n ′ y . . . y n − n ′ . x j M . . . M m y . . . y n − n ′ : s → . . . → s n → n Lemma 3.8 (Characterisation of Types for Left Invertible Terms) .
1. Left invertible terms inhabit only types which are left types.2. Each left type is inhabited by a left invertible term.Proof.
1. By Definition 2.2 a left invertible term M is a l -abstraction, then its type is of the shape s → n by Lemma 3.3(4). It is enough to show that if the head normal form of M belongs to X , then s → n ∈ Q . The proof is by induction on X .Case l tx . . . x n . t . By the invariance of types under b ⊥ -conversion (Theorem 3.4) we get ⊢ l tx . . . x n . t : s → n which implies by repeated application of Lemma 3.3(4) n ∼ s → . . . → s n → µ and t : s , x : s , . . . , x n : s n ⊢ t : µ Then s ≤ µ by Lemma 3.3(1) and we can conclude s → n ∈ Q .Case l tx . . . x n . x j M . . . M m ∈ X with j ≤ n since l tx . . . x n . M i ∈ X with i ≤ m . As in previous casewe get n ∼ s → . . . → s n → µ and t : s , x : s , . . . , x n : s n ⊢ x j M . . . M m : µ Let G = t : s , x : s , . . . , x n : s n . By repeated application of Lemma 3.3(3) we have G ⊢ x j : r → . . . r m → µ and G ⊢ M l : r l for 1 ≤ l ≤ m .Lemma 3.3(1) implies s j ≤ r → . . . r m → µ . Moreover l tx . . . x n . M l inhabits s → s → . . . → s n → r l for 1 ≤ l ≤ m .Lastly l tx . . . x n . M i ∈ X implies by induction s → s → . . . → s n → r i ∈ Q We can then conclude s → n ∈ Q .2. The proof is by induction on Q . If s ≤ n we can derive ⊢ l tx . . . x n . t : s → s → . . . → s n → n .Otherwise by Lemma 3.7 we can assume that the inhabitants of s → s → . . . → s n → r l for1 ≤ l ≤ m have at least n + l tx . . . x n . M l be an inhabitant of s → s → . . . → s n → r l for 1 ≤ l ≤ m and s → s → . . . → s n → r i ∈ Q for some i ≤ m .By induction l tx . . . x n . M i ∈ X , then l tx . . . x n . x j M . . . M i . . . M m ∈ X . Moreover.Coppo,M.Dezani-Ciancaglini, A.D´ıaz-Caro, I.Margaria &M.Zacchi 39if s j ≤ r → . . . → r m → n , then l tx . . . x n . x j M . . . M i . . . M m inhabits s → s → . . . → s n → n .The types of right invertible terms are easy to define, as expected. Definition 3.9.
A type t → µ is a right type if t ≤ r → . . . → r m → µ and t → r i is inhabited for ≤ i ≤ m. Example 3.10.
A right type is (( y → y ) ∧ ( y → y ) → w → j ) ∧ y → j . Another right type is (( j → j → ( j → j → j ) → j ) → ( y → y → y ) → w → ( y → y → y ) → j ) → j This last type can be derived for the term L ◦ N of Example 2.9.
Lemma 3.11 (Characterisation of Types for Right Invertible Terms) .
1. Right invertible terms inhabit only types which are right types.2. Each right type is inhabited by a right invertible term.Proof.
1. By Theorem 2.10 is it enough to show that ⊢ l z . zM . . . M m : t → µ implies that t → µ is a righttype. By Lemma 3.3(4) and (3) we get: z : t ⊢ z : r → . . . → r m → µ and z : t ⊢ M i : r i for1 ≤ i ≤ m . By Lemma 3.3(1) t ≤ r → . . . → r m → µ . Moreover l z . zM i inhabits t → r i for1 ≤ i ≤ m . Therefore t → µ is a right type.2. Let M i be an inhabitant of t → r i for 1 ≤ i ≤ m and t ≤ r → . . . → r m → µ . Then we can derive ⊢ l z . zM . . . M m : t → µ .It is easy to verify that w is both a left and a right type. We can discuss now retractions, i.e. isomorphic embeddings, in strict types using terms of L ⊥ . Definition 4.1.
Type µ is a retract of type n (notation µ ⊳ n ) if there exist terms L and R such that:1. ⊢ L : n → µ;2. ⊢ R : µ → n ;3. L ◦ R = I .We say that L , R witness the retraction.
Example 4.2. L = l z . z I ( l y . yy ) z and R = l tx x x . x x t witness the retraction j ⊳ ( j → j ) ∧ (( j → j ) → j → j ) → s → w → j where s = ( j → j ) ∧ (( j → j ) → j → j ) → j → j . The same retraction is witnessed by L and R ′ = l tx x x . t, which is a simple right inverse. Notice that L cannot be typed with Curry types. It easy to prove that the retraction relation enjoys the transitivity property. In fact if L , R witness µ ⊳ µ ′ and L ′ , R ′ witness µ ′ ⊳ n , then L ◦ L ′ , R ′ ◦ R witness µ ⊳ n .Retraction can be fully characterised.0 Retractions inIntersection Types Theorem 4.3 (Characterisation of Retraction) . µ ⊳ n if and only if n ∼ r → . . . → r m → µ and n → r i is inhabited for ≤ i ≤ m. Moreover, each retraction can be witnessed by a simple right inverse.Proof.(If) Let M i be an inhabitant of n → r i for 1 ≤ i ≤ m . We can choose L = l z . z ( M z ) . . . ( M m z ) and R = l tx . . . x m . t . Notice that R is a simple right inverse. It it easy to verify that L and R satisfy theconditions of Definition 4.1. (Only if) By Theorem 2.10 L = l z . zM . . . M m . Then applying Lemma 3.3(4) to ⊢ L : n → µ we get z : n ⊢ zM . . . M m : µ . By repeated applications of Lemma 3.3(3) this implies z : n ⊢ z : s → . . . → s m → µ and z : n ⊢ M i : s i for 1 ≤ i ≤ m .From z : n ⊢ z : s → . . . → s m → µ we have n ≤ s → . . . → s m → µ by Lemma 3.3(1). We canassume n = r → . . . → r m → µ ′ , which implies s i ≤ r i for 1 ≤ i ≤ m and µ ′ ≤ µ by Lemma 3.2.Observe that L ◦ R = l x . RxM ′ . . . M ′ m where M ′ i = M i { Rx / z } for 1 ≤ i ≤ m . From ⊢ R : µ → n and z : n ⊢ M i : s i and s i ≤ r i we can derive x : µ ⊢ M ′ i : r i for 1 ≤ i ≤ m . This together with n = r → . . . → r m → µ ′ implies x : µ ⊢ RxM ′ . . . M ′ m : µ ′ . From L ◦ R = I we get RxM ′ . . . M ′ m = x .Subject Conversion derives x : µ ⊢ x : µ ′ , so by Lemma 3.3(1) µ ≤ µ ′ . We conclude µ ∼ µ ′ .As an easy consequence of this theorem if µ ⊳ n , then µ ⊳ r → n for any intersection r such that n → r is inhabited. Moreover if µ w , then neither µ ⊳ w nor w ⊳ µ can hold.Reciprocal retraction implies equivalence. Corollary 4.4.
If µ ⊳ n and n ⊳ µ, then µ ∼ n .Proof. By previous theorem n ∼ r → . . . → r m → µ and µ ∼ s → . . . → s n → n , which imply m = n = µ ∼ n .Given two terms L and R such that L ◦ R = I , they do not witness a retraction for all the pairs of theirtypes. For instance, the terms L ◦ N , M in Examples 2.9 and 2.7 do not witness a retraction using theirtypes shown in Examples 3.10 and 3.6 i) . However L ◦ N , M witness the following retraction: µ ⊳ ( µ → n ) ∧ ( w → n → n ) → n where n = w → ( µ → w → µ ) → µ and n = ( w → n → n ) → n .Notice that L ◦ R = I implies that for all types µ there is an intersection s such that L has type s → µ and R has type µ → s . The proof is easy using the inversion lemma and the invariance of types under b -expansion. This does not mean that L , R always witness a retraction between strict types, since s canbe an intersection of strict types, as shown in the following example. Example 4.5.
Let L = l u . u ⊥ ( u ⊥ ( l z . I )) and R = l tx x . x t. We derive ⊢ L : ( w → ( j → j ) → j ) ∧ ( w → ( j → j → j ) → j → j ) → j ⊢ R : j → w → ( j → j ) → j and ⊢ R : j → w → ( j → j → j ) → j → j When R is a simple right inverse instead we always get a set of retractions. Theorem 4.6.
If L ◦ R = I and R is a simple right inverse, then for each type µ we find types n such thatL , R witness the retraction µ ⊳ n .Proof. By Theorem 2.10 L = l z . zM . . . M m . By definition of simple right inverse R = l tx . . . x m . t . Wecan then choose n ∼ r → . . . → r m → µ for each r , . . . , r m such that ⊢ l z . M i : n → r i for 1 ≤ i ≤ m . Inparticular we can always take r i = w for 1 ≤ i ≤ m ..Coppo,M.Dezani-Ciancaglini, A.D´ıaz-Caro, I.Margaria &M.Zacchi 41 In this section we extend the characterisation of retraction (Theorem 4.3), which holds for strict types, tothe case of standard intersection types, by considering only simple right inverses.The set of standard intersection types (simply intersection types ) is defined by: s : = j | w | s → s | s ∧ s where, as before, j ranges over type variables and w is a constant. In this section, we convene that s , t , r range over intersection types.The preorder of Definition 3.1 is extended to intersection types by adding the rule ( s → t ) ∧ ( s → r ) ≤ s → t ∧ r The following lemma gives a crucial property of this subtyping, which is shown in [3].
Lemma 5.1. If V i ∈ I ( s i → t i ) ≤ s → t , then there is J ⊆ I such that s ≤ V i ∈ J s i and V i ∈ J t i ≤ t . The type assignment system is defined by the typing rules of Table 2, where environments are finitemappings from term variables to intersection types. ( Ax ) G , x : s ⊢ x : s ( w ) G ⊢ M : w ( ≤ ) G ⊢ M : s s ≤ tG ⊢ M : t ( → I ) G , x : s ⊢ M : tG ⊢ l x . M : s → t ( → E ) G ⊢ M : s → t G ⊢ N : sG ⊢ MN : t ( ∧ I ) G ⊢ M : s G ⊢ M : tG ⊢ M : s ∧ t Table 2: Typing Rules of Standard Intersection TypesThe inversion lemma is as expected, for a proof see [4, Theorem 12.1.13].
Lemma 5.2 (Inversion Lemma) .
1. If G ⊢ x : t , then either t ∼ w or x : s ∈ G for s ≤ t .2. If G ⊢ ⊥ : t , then t ∼ w .3. If G ⊢ MN : t , then G ⊢ M : s → t and G ⊢ N : s .4. If G ⊢ l x . M : t , then t ∼ V i ∈ I ( s i → r i ) and G , x : s i ⊢ M : r i for all i ∈ I. The notion of retraction (Definition 4.1) can be extended to standard intersection types. It is usefulhere to add the notion of simple retraction.
Definition 5.3.
1. Type s is a retract of type t (notation s ⊳ t ) if there exist terms L and R such that:(a) ⊢ L : t → s ;(b) ⊢ R : s → t ;(c) L ◦ R = I .2. Type s is a simple retract of type t (notation s ⊳ s t ) if s ⊳ t and R is a simple right inverse. We say that L , R witness the (simple) retraction.
We can now show the desired characterisation.
Theorem 5.4 (Characterisation of Simple Retractions in Standard Intersection Types) . s ⊳ s t if and only if s ∼ V i ∈ I s i and t ∼ V i ∈ I ( r ( i ) → · · · → r ( i ) m → s i ) and the types t → V i ∈ I r ( i ) k are inhabited for ≤ k ≤ m.Proof.(If) Let M k be an inhabitant of t → V i ∈ I r ( i ) k for 1 ≤ k ≤ m . We can choose L = l z . z ( M z ) . . . ( M m z ) and R = l tx . . . x m . t . It is easy to verify that L and R satisfy the conditions of Definition 5.3. (Only if) Since R is simple, we can assume R = l tx . . . x m . t . Lemma 5.2(4) applied to ⊢ R : s → t gives t ∼ V i ∈ I ( r ( i ) → · · · → r ( i ) m → s i ) and t : s ⊢ t : s i for all i ∈ I . Then s ≤ s i for all i ∈ I , whichimplies s ≤ V i ∈ I s i .By Theorem 2.10 we have L = l z . zM . . . M m . Then applying the Inversion Lemma to ⊢ L : t → s we get t ≤ r → · · · → r m → s and z : t ⊢ M k : r k for 1 ≤ k ≤ m . Then ^ i ∈ I ( r ( i ) → · · · → r ( i ) m → s i ) ≤ r → · · · → r m → s By Lemma 5.1 this implies V i ∈ J s i ≤ s and r k ≤ V i ∈ J r ( i ) k for some J ⊆ I and for all 1 ≤ k ≤ m .From s ≤ V i ∈ I s i and V i ∈ J s i ≤ s with J ⊆ I we get J = I and s ∼ V i ∈ I s i . From z : t ⊢ M k : r k and r k ≤ V i ∈ I r ( i ) k we can derive z : t ⊢ M k : V i ∈ I r ( i ) k for 1 ≤ k ≤ m . Therefore the types t → V i ∈ I r ( i ) k are inhabited for 1 ≤ k ≤ m .In [1] it is proved that each intersection type is equivalent to an intersection of strict types. Owingto this property and the idempotence of the intersection type constructor, we can show that a simpleretraction between intersection types implies a set of retractions between strict types. Corollary 5.5. If s ⊳ s t and t ∼ V i ∈ I n i , then there are strict types µ i with i ∈ I such that s ∼ V i ∈ I µ i , andµ i ⊳ n i for i ∈ I.Proof.
By Theorem 5.4 n i ∼ r ( i ) → · · · → r ( i ) m → µ i for some µ i and s ∼ V i ∈ I µ i . Then µ i ⊳ n i for i ∈ I byTheorem 4.3. Example 5.6. i) Consider s = j ∧ y and t = w → (( j → j → j ) → j ) ∧ (( y → w → y ) → y ) . We get s ⊳ s t bychoosing L = l z . z ⊥ K , where K = l xy . x and its simple right inverse R = l tx x . t. The terms L, Rwitness also the two retractions j ⊳ w → ( j → j → j ) → j and y ⊳ w → ( y → w → y ) → y between strict types.ii) The terms L = l z . z I and R = l tx . t witness the simple retraction j ⊳ s (( y → y ) → j ) ∧ ( w → j ) .The same terms show the two retractions j ⊳ ( y → y ) → j and j ⊳ w → j between strict types. .Coppo,M.Dezani-Ciancaglini, A.D´ıaz-Caro, I.Margaria &M.Zacchi 43 In this section we discuss retractions in models of l ⊥ -calculus. Since standard intersection types canbe seen as a conservative extension of strict intersection types, the results of this section hold for bothsystems.It is easy to adapt the Hindley-Longo definition of l -calculus models [18] to the l ⊥ -calculus. We use V to range over mappings from term variables to elements of the domain. Definition 6.1.
A model of the l ⊥ -calculus is a structure M = < D , · , [[ − ]] M , d > which satisfies thefollowing conditions:1. d is a distinguished element of the domain D such that d · e = d for all e ∈ D2. [[ x ]] MV = V ( x ) [[ MN ]] MV = [[ M ]] MV · [[ N ]] MV [[ l x . M ]] MV · d = [[ M ]] MV [ d / x ] V = V ′ implies [[ M ]] MV = [[ M ]] MV ′ [[ l x . M ]] MV = [[ l y . M [ y / x ]]] MV if y is not in M7. ∀ d ∈ D [[ M ]] MV [ d / x ] = [[ N ]] MV [ d / x ] implies [[ l x . M ]] MV = [[ l x . N ]] MV [[ ⊥ ]] MV = [[ l x . ⊥ ]] MV = d . As usual we interpret types as subsets of the model domain.
Definition 6.2.
The standard interpretation of types in a model M with domain D is defined by:1. [[ w ]] MW = D2. [[ j ]] MW = W ( j ) [[ s → t ]] MW = { d | ∀ e ∈ [[ s ]] MW d · e ∈ [[ t ]] MW } [[ s ∧ t ]] MW = [[ s ]] MW ∩ [[ t ]] MW where W ranges over mappings from type variables to subsets of the domain. From these definitions it easy to show the soundness of the type systems of Sections 3 and 5. We saythat the mappings V , W for a model M respect an environment G if x : s ∈ G implies [[ x ]] MV ∈ [[ s ]] MW . Theorem 6.3 (Soundness) . If G ⊢ M : s , then [[ M ]] MV ∈ [[ s ]] MW for all M and all V , W respecting G . The semantic retraction is naturally defined as follows.
Definition 6.4.
Type s is a semantic retract of type t (notation s ◭ t ) if there are two terms L and R suchthat for all models M of l ⊥ -calculus and for all mappings V , W :1. [[ R ]] MV ∈ [[ s → t ]] MW [[ L ]] MV ∈ [[ t → s ]] MW [[ L ]] MV ◦ [[ R ]] MV = [[ I ]] MV . l -calculus is the filter model F defined in [3]. The domain of this model isthe set of filters of types. We refer to that paper for the basic definitions and properties. The model F can be easily seen as a model of the l ⊥ -calculus by taking the filter generated by type w as d .In [3] the filter model is proved to be complete for l -calculus and standard intersection types. It iseasy to adapt this result to l ⊥ -calculus. The completeness theorem of [3] can then be reformulated asfollows: Theorem 6.5 (Completeness of the Filter Model) . If [[ M ]] FV ∈ [[ s ]] FW for all V , W respecting G , then G ⊢ M : s . We can show that retraction and semantic retraction coincide using the soundness and the complete-ness of the filter model.
Theorem 6.6. s ⊳ t if and only if s ◭ t .Proof.(If) By Theorem 6.5 we immediately have that • [[ L ]] FV ∈ [[ t → s ]] FW for all V , W implies ⊢ L : t → s and • [[ R ]] FV ∈ [[ s → t ]] FW for all V , W implies ⊢ R : s → t .Notice that [[ L ]] MV ◦ [[ R ]] MV = [[ B LR ]] MV . Since j → j ∈ [[ I ]] FV , the completeness of the filter modelgives ⊢ B LR : j → j . It is easy to prove that this implies B LR = I , as remarked in [3]. (Only if) The soundness of the type system (Theorem 6.3) implies that • ⊢ L : t → s gives [[ L ]] MV ∈ [[ t → s ]] FW for all M , V , W and • ⊢ R : s → t gives [[ R ]] MV ∈ [[ s → t ]] FW for all M , V , W .If L ◦ R = I , then by definition of model we get [[ L ]] MV ◦ [[ R ]] MV = [[ I ]] MV . The seminal paper [7] characterises for Curry types both isomorphism in the lbh -calculus and retractionin the lb -calculus.Isomorphism in the lbh -calculus is characterised for various type disciplines by means of equationsbetween types [16]. Product and unit types are considered in [6,23,24], universally quantified types in [7],all the above type constructors in [15]. Characterisation of isomorphism for intersection types insteadrequires a notion of type similarity [9, 14]. Analogous result holds for intersection and union types [8,10, 11]. [17] shows that isomorphism for product, arrow and sum types is not finitely axiomatizable.Retraction witnessed by affine terms of lbh -calculus for Curry types with only one atom is charac-terised in [13]. As an auxiliary result in the study of the relation between iteration and recursion, [25]gives a necessary condition for retraction considering universally quantified types and lb -calculus. Analgorithm to decide if a Curry type with a single atom is a retract of another one in the lbh -calculus isgiven in [20]. This algorithm builds the witnesses of the retraction, when they exist. The results of [13]are extended to Curry types with many atoms in [21]. Moreover [21] gives necessary conditions for re-traction witnessed by arbitrary terms of lbh -calculus dealing with both Curry and universally quantifiedtypes. The problem of retraction solved in [13] is shown to be NP-complete in [22]. [26] gives a proofsystem which leads to an exponential decision procedure to characterise retraction for Curry types in the lbh -calculus..Coppo,M.Dezani-Ciancaglini, A.D´ıaz-Caro, I.Margaria &M.Zacchi 45 This paper deals with retraction for strict and standard intersection types in the l ⊥ -calculus. Both thechoices of the calculus and of the types can be discussed.We considered the l ⊥ -calculus following [19]. Our results are easily adapted to the l -calculus takingan unsolvable term to play the role of ⊥ .By conservativity the given characterisation of retraction in strict intersection types holds in Currytypes [12]. In this way we obtain the result of [7]. We give also a characterisation of retractions instandard intersection types when the right inverses are simple. However the retraction of Example 4.5cannot be shown by a simple right inverse, since it should have both the types j → w → ( j → j ) → j and j → w → ( j → j → j ) → j → j .The definition of a necessary and sufficient condition for the existence of a retraction between stan-dard intersection types is not obvious when the right inverses are arbitrary. For example, no type can bea retract of the type w → (( w → j → j ) → j ) ∧ (( y → w → y ) → y ) essentially since ( w → j → j ) ∧ ( y → w → y ) is not inhabited. Notice that this type and the type t ofExample 5.6 i) differ only for one occurrence of w in place of one occurrence of j .We plan to investigate retractions in standard intersection types without conditions on right inverses,and in intersection and union types. The problem for the lbh -calculus is surely more difficult as shownby the papers [7, 13, 20–22, 26] and it is left for future work.As suggested by one referee, an interesting future development is to see how the results presentedhere can be adapted to programming languages with richer sets of constructs. This would allow to applyautomatic retraction inference in dealing with API of functional programs or proof-assistants so as tomaximise code reuse. Acknowledgments.
We are grateful to the anonymous reviewers for their useful suggestions, which ledto substantial improvements. This work was done during a visit of Alejandro D´ıaz-Caro at the ComputerScience Department of Torino University. This visit has been supported by the WWS2 Project, financedby “Fondazione CRT” and Torino University.
References [1] Steffen van Bakel (2011):
Strict Intersection Types for the Lambda Calculus . ACM Computing Surveys43(3), p. 20, doi: .[2] Henk Barendregt (1984):
The Lambda Calculus: its Syntax and Semantics , revised edition. North-Holland.[3] Henk Barendregt, Mario Coppo & Mariangiola Dezani-Ciancaglini (1983):
A Filter Lambda Model andthe Completeness of Type Assignment . The Journal of Symbolic Logic 48(4), pp. 931–940, doi: .[4] Henk Barendregt, Wil Dekkers & Richard Statman (2013):
Lambda Calculus with Types . Perspectives inlogic, Cambridge University Press, doi: .[5] Corrado B¨ohm & Mariangiola Dezani-Ciancaglini (1974):
Combinatorial Problems, Combinator Equationsand Normal Forms . In Jacques Loeckx, editor: ICALP’74, LNCS 14, Springer, pp. 185–199, doi: . [6] Kim Bruce, Roberto Di Cosmo & Giuseppe Longo (1992): Provable isomorphisms of types . MathematicalStructuresinComputerScience 2(2), pp. 231–247, doi: .[7] Kim Bruce & Giuseppe Longo (1985):
Provable Isomorphisms and Domain Equations in Models of TypedLanguages . In Robert Sedgewick, editor: STOC’85, ACM Press, pp. 263 – 272, doi: .[8] Mario Coppo, Mariangiola Dezani-Ciancaglini, Ines Margaria & Maddalena Zacchi (2013):
Towards Isomor-phism of Intersection and Union Types . In Stephane Graham-Lengrand & Luca Paolini, editors: ITRS’12,EPTCS 121, pp. 58 – 80, doi: .[9] Mario Coppo, Mariangiola Dezani-Ciancaglini, Ines Margaria & Maddalena Zacchi (2014):
Isomorphism of“Functional” Intersection Types . In Ralph Matthes & Aleksy Schubert, editors: Types’13, 26, LIPIcs, pp.129–149, doi: .[10] Mario Coppo, Mariangiola Dezani-Ciancaglini, Ines Margaria & Maddalena Zacchi (2015):
Isomor-phism of Intersection and Union Types . Mathematical Structures in Computer Science doi: . Published online: 07 August 2015.[11] Mario Coppo, Mariangiola Dezani-Ciancaglini, Ines Margaria & Maddalena Zacchi (2015):
On Isomorphismof “Functional” Intersection and Union Types . In Jacob Rehof, editor: ITRS’14, EPTCS 177, pp. 53–64,doi: .[12] Haskell B. Curry & Robert Feys (1958):
Combinatory Logic . Studies in Logic and the Foundations ofMathematics I, North-Holland.[13] Ugo de’Liguoro, Adolfo Piperno & Rick Statman (1992):
Retracts in Simply Typed lbh -calculus . In AndreScedrov, editor: LICS’92, IEEE Computer Society Press, pp. 461–469, doi: .[14] Mariangiola Dezani-Ciancaglini, Roberto Di Cosmo, Elio Giovannetti & Makoto Tatsuta (2010):
On Isomor-phisms of Intersection Types . ACM Transactions on Computational Logic 11(4), pp. 1–22, doi: .[15] Roberto Di Cosmo (1995):
Second Order Isomorphic Types. A Proof Theoretic Study on Second Order l -calculus with Surjective Pairing and Terminal Object . Information and Computation 119(2), pp. 176–201,doi: .[16] Roberto Di Cosmo (2005): A Short Survey of Isomorphisms of Types . MathematicalStructuresinComputerScience 15, pp. 825–838, doi: .[17] Marcelo Fiore, Roberto Di Cosmo & Vincent Balat (2006):
Remarks on Isomorphisms in Typed LambdaCalculi with Empty and Sum Types . Annalsof Pureand Applied Logic 141(1–2), pp. 35–50, doi: .[18] Roger Hindley & Giuseppe Longo (1980):
Lambda-Calculus Models and Extensionality . MathematicalLogicQuarterly 26(19-21), pp. 289–310, doi: .[19] Ines Margaria & Maddalena Zacchi (1983):
Right and Left Invertibility in Lambda-Beta-Calculus . R.A.I.R.O.TheoreticalInformatics17(1), pp. 71–88.[20] Vincent Padovani (2001):
Retracts in Simple Types . In Samson Abramsky, editor: TLCA’01, LNCS 2044,Springer, pp. 376–384, doi: .[21] Laurent Regnier & Pawel Urzyczyn (2002):
Retractions of Types with Many Atoms . CoRR cs.LO/0212005.[22] Aleksy Schubert (2008):
On the Building of Affine Retractions . MathematicalStructuresinComputerScience18(4), pp. 753–793, doi: .[23] Sergei Soloviev (1983):
The Category of Finite Sets and Cartesian Closed Categories . Journal of SovietMathematics 22(3), pp. 1387–1400, doi: . English translation of the original paper inRussian published in Zapiski Nauchnykh Seminarov LOMI, v.105, 1981.[24] Sergei Soloviev (1993):
A Complete Axiom System for Isomorphism of Types in Closed Categories . In AndreiVoronkov, editor: LPAR’93, LNCS 698, Springer, pp. 360–371, doi: .[25] Zdzislaw Splawski & Pawel Urzyczyn (1999):
Type Fixpoints: Iteration vs. Recursion . In Didier R´emi &Peter Lee, editors: ICFP’99, ACM Press, pp. 102–113, doi: . .Coppo,M.Dezani-Ciancaglini, A.D´ıaz-Caro, I.Margaria &M.Zacchi 47 [26] Colin Stirling (2013): Proof Systems for Retracts in Simply Typed Lambda Calculus . In Fedor V. Fomin,Rusins Freivalds, Marta Z. Kwiatkowska & David Peleg, editors: ICALP’13, LNCS 7966, Springer, pp.398–409, doi: .[27] Pawel Urzyczyn (1999):
The Emptiness Problem for Intersection Types . The Journal of Symbolic Logic64(3), pp. 1195–1215, doi: .[28] Pawel Urzyczyn (2009):
Inhabitation of Low-Rank Intersection Types . In Pierre-Louis Curien, editor: TLCA,LNCS 5608, Springer, pp. 356–370, doi:10.1007/978-3-642-02273-9_26