A Type System for Tom

Extending a given language with new dedicated features is a general and quite used approach to make the programming language more adapted to problems. Being closer to the application, this leads to less programming flaws and easier maintenance. But o…

Authors: Claude Kirchner, Pierre-Etienne Moreau, Claudia Tavares

I. Mackie and A. Martins Moreira (Eds.): T enth International W orksho p on Rule-Based Programming (RULE 2 009) EPTCS 21, 2010, pp. 51–63, doi:10.4204 /EPTCS .21.5 A T ype Syste m f or T om Claude Kirchner INRIA Centre de Recherche INRIA Bordeaux - Sud-Ouest 351, cours de la Libération, 33405 T alence Cede x France Claude.Kir chner@inri a.fr Pierre-Etienne Moreau Cláudia T a var es ∗ INRIA & LORIA 615 rue du Jardin Botanique, CS 20101 54603 V illers-l ès-Nancy Cedex France Pierre-Eti enne.Morea u@loria.fr Claudia.Ta vares@lori a.fr Extendin g a given language with new ded icated features is a general and qu ite used approach to m ake the progr amming language more adap ted to problem s. Being closer to the applica tion, this leads to less programmin g flaws and easier maintenance. But of course one would still like to perfor m progr am analysis on these kinds of exten ded languages, in p articular type ch ecking and inf erence. In this case one has to make the ty ping of the extended features compatible with the ones in the startin g languag e. The T om pr ogramm ing language is a typical examp le of such a s ituation as it consists of an extension of Java that adds pattern match ing, m ore particularly associativ e pattern matchin g, and r eduction strategies. This paper presents a type system with subtyp ing for T om , that is com patible with Java ’ s type system, and that perfor ms both type checking and type inference. W e pro pose an algorithm that checks if all patterns of a T om program are well-typed. In ad dition, we p ropo se an algorithm based on equa lity and sub typing co nstraints tha t infers ty pes of variables occurrin g in a pattern . Both algorithms are exemplified and the proposed type system is showed to be sound and complete. 1 Introd uction of the pr oblem: static typing in T om W e consider here the T om language, which is an ex tensio n of Java th at provi des rule base d construc ts. In particul ar , any Java pr ogram is a T om p rogram. W e call this kind of extens ion formal island s [4, 3] where the ocean consists of Java code and the island of algebra ic patter ns. For simplicity , we consider here only two ne w T om construc ts: a %match constr uct and a ‘ (backquot e) construc t. The semantics of %match is close to the match that exis ts in functio nal programmin g langua ges, b ut in an imperati ve conte xt. A %match is parameterized by a list of subjects ( i.e. expressi ons ev aluated to ground terms) and contains a list of rules. The left-ha nd side of the rules are pattern s built upon constr uctors and fres h v ariabl es, without an y linearity restrict ion. The right-ha nd side is not a t erm, b ut a Java statement that is execu ted when the pattern m atches the subject. Howe ver , thank s to the backquo te constr uct ( ‘ ) a term can be easily built and retur ned. In a similar way to the standa rd switch/case constr uct, patterns are ev aluated from top to bottom. In contrast to the functional match , se veral actions ( i.e. right-h and sides) may be fi red for a gi ven subject as long as no return or break instruction is ex ecuted. T o implement a simple reduction step for each rule, it suffices to encode the left-hand side with a pattern and conside r the Java state ment that returns the right-hand side. For example, gi ven the sort Nat and the function symbols suc and zero , ad dition and comparison of Peano inte gers may be encoded as follo w s: ∗ This work w as partial ly suppo rted by The Capes Foundation, Ministry of Education of Brazil. Cx. postal 365 , Brasília DF 70359-97 0, Brazil. 52 A T ype System for T om public Nat plus(Nat t1, Nat t2) { %match(t1, t2) { x,zero() -> { return ‘x; } x,suc(y) -> { return ‘suc(plus(x,y )); } } } public boolean greaterTh an(Nat t1, Nat t2) { %match(t1, t2) { x,x -> { return false; } suc(x),zer o() -> { return true; } zero(),suc (y) -> { return false; } suc(x),suc (y) -> { return ‘greater Than(x,y); } } } In this combination of an oc ean langua ge (in our cas e Java ) an d island fea tures (in our cas e abstra ct data types and matching) , it is still an open question to perform type checking and type inference. Since we want to allo w for type inclusion at the pattern le vel, the first purpos e of this paper is to presen t an exten sion of the signatu re definiti on mechani sm allowing for subtyp es. In this conte xt we define Java -lik e types and signat ures. Therefore the set of types is the union of Java types and abst ract data types (i.e. T om types ) where m ultiple inheritance and ov erload ing are forbidden . For exa mple, giv en the sorts Int + , Int − , Int and Zero , the type system accepts the declaratio n Int + < : Int ∧ Int − < : Int b ut refuses the declaration Zero < : Int + ∧ Zero < : Int − . Moreov er , a functi on symbol suc cannot be ov erloaded on both sort s Int + and Int − . In orde r to handle th ose is sues, we propose an algorith m based on unificat ion of equali ty constrain ts [14 ] and simplification of subtype constraint s [8 , 1 , 16]. It infers the types of the v ariables that occur in a pattern ( x and y in the prev ious exa mple). Moreov er , we also propo se an algo rithm that checks that the patterns occurring in a T om program are correctly typed. Of course typing systems for algebra ic terms and for rewritin g has a long history . It includes the seminal works done on OBJ, order -sorted algebras [10, 9] and Maude [6]; the works done on feature algebr as [2] or on membership cons traints [11, 7]; and the works on typing rewriti ng in higher -order setting s like [17] or [5]. Lar gely inspired from these works, our contri b ution here focusses on the approp riate type system for pattern-match ing, possibly modulo associati vity , in a Java en vironment. 2 T ype check ing Giv en a signature Σ v , the (simplified ) abstract syntax of a T om program is as follo ws: rul e :: = cond − → ac t ion cond :: = t er m 1 ≺ ≺ [ s ] t erm 2 | cond 1 ∧ cond 2 t erm :: = x | f ( t er m 1 , . . . , t erm n ) ac t ion :: = ( t erm 1 , . . . , t erm n ) The left -hand side of a rul e is a conju nction of matc hing condi tions t er m 1 ≺ ≺ [ s ] t erm 2 consis ting of a pair of terms and where s denot es a sort. W e introd uce the set F of free symbols. T erms are many-s orted terms composed o f vari ables x ∈ X and function symbols f ∈ F . T he set of t erms is written T ( F , X ) . In general, an action is a Java statement, bu t for our purpose it is enough to consider an abstraction consis ting of terms e 1 , . . . , e n ∈ T ( F , X ) whose instantiat ions are described by the conditio ns, and used in the Java statemen t. Example 2.1. T he last rule of the greaterThan function given abo ve can be r epr esented by the follo wing rul e exp r ession: suc ( x ) ≺ ≺ [ N ] t 1 ∧ suc ( y ) ≺ ≺ [ N ] t 2 − → ( x , y ) In a first step, we define S as a set of sorts and we consider that a conte xt Γ is composed of a set of pairs (v ariable ,sort), and (function symbol,rank): Γ :: = ∅ | Γ 1 ∪ Γ 2 | x : s | f : s 1 , . . . , s n → s Claude Kirchner , Pierre-Etienne Moreau, Cláudia T a vares 53 and c onte xt access is defined by the function sortOf ( Γ , e ) : Γ × T ( F , X ) → S which retu rns the sort of term e in the conte xt Γ : sortO f ( Γ , x ) = s , if x : s ∈ Γ sortOf ( Γ , f ( e 1 , . . . , e n )) = s , if f : s 1 , . . . , s n → s ∈ Γ where x ∈ X and f ∈ F . W e denote by Γ ( x : s ) the fact that x : s belongs to Γ . Similarly , Γ ( f : s 1 , . . . , s n → s ) means that f : s 1 , . . . , s n → s belongs to Γ . In F ig. 1 we giv e a classical type checki ng system defined by a set of inferen ce rules . Starting from a contex t Γ and a rule expre ssion π , we say that π is w ell-typ ed if π : w t can b e der i ved by apply ing the inference rules . wt is a specia l sort t hat c orresp onds to the well-typed ness of a r ul e or a co ndition cond . Γ ( x : s ) ⊢ x : s T - V A R Γ ⊢ e 1 : s 1 . . . Γ ⊢ e n : s n Γ ( f : s 1 , . . . , s n → s ) ⊢ f ( e 1 , . . . , e n ) : s T - F U N Γ ⊢ e 1 : s Γ ⊢ e 2 : s Γ ⊢ ( e 1 ≺ ≺ [ s ] e 2 ) : w t T - M AT C H Γ ⊢ ( cond 1 ) : w t . . . Γ ⊢ ( cond n ) : wt Γ ⊢ ( cond 1 ∧ . . . ∧ cond n ) : w t T - C O N J Γ ⊢ ( cond ) : wt Γ ⊢ e 1 : s 1 . . . Γ ⊢ e n : s n Γ ⊢ ( cond − → ( e 1 , . . . , e n )) : wt T - R U L E if sortOf ( Γ , e i ) = s i , for i ∈ [ 1 , n ] Figure 1: Simple type che cking system. 2.1 Subtypes and associative-matching In order to introduce subtype s in T om , w e refine S as the set of sorts, equipped with a partial order < : , called subtyping . It is a binary relation on S that satisfies reflexi vity , transiti vity and antisymmetry . Moreo ver , since we allo w for some symbols to be associati ve, w e introduc e the set F v of varia dic sym- bols to de note them. Now , th e set of terms is written T ( F ∪ F v , X ) and terms a re many-sor ted v ariadic terms compo sed of v ariables x ∈ X a nd functio n symbols f ∈ F ∪ F v . In th e follo wing, we often write ℓ a v ariadic operator and call it a list . W e extend m atchin g ov er lists to be associat i ve. Therefore a pattern matches a subject cons idering equali ty relation modulo flattening. Lists can be denoted by function symbols ℓ ∈ F v or by v ariabl es x ∈ X annotate d by ∗ . Such va riables , w hich we write x ∗ , are called star variables . So we consider in the f ollo wing many-so rted v ariadic terms composed of var iables x ∈ X , star variab les x ∗ (where x ∈ X ) and functi on symbol s f ∈ F ∪ F v . Moreov er , we define that function symbols ℓ ∈ F v with variab le domain (since they hav e a va riable arity) of sort s 1 and codomai n s are written ℓ : s 1 ∗ → s while star v ariabl es x ∗ are also sort ed and written x ∗ : s . Since terms b uilt from syntactic and variad ic operato rs can ha ve the same codomain, we cannot distin guish one from t he ot her only by theirs sorts. Howe ver , this is necessary to k no w which typing ru le applie s. Moreov er , an inserti on of a term can be treated by two ways: giv en terms ℓ ( e 1 ) , ℓ ( e 2 ) , ℓ 1 ( e 1 ) ∈ T ( F ∪ F v , X ) w here ℓ, ℓ 1 ∈ F v , we hav e: 1) an insertio n of a list ℓ ( e 1 ) into a list ℓ ( e 2 ) correspon ds 54 A T ype System for T om to a concatenat ion of these both lists resulting in ℓ ( e 1 , e 2 ) ; 2) an insertion of a list ℓ 1 ( e 1 ) into a list ℓ ( e 2 ) results in ℓ ( ℓ 1 ( e 1 ) , e 2 ) . For that reason, it is important to distingu ish the list from the inserted term by its function symbol in order to define which typing rule conce rned for list must be applied . For this purpo se, we introduce a notion of sorts decorated with function symbols, called types , to classify terms. The specia l symbol ? is used as decoration when it is not useful to kno w what the func tion symbol is, i.e. when the expecte d type is kno wn b ut not the expe cted functi on symbol. T his leads to a ne w set of decora ted sorts D which is equipp ed with a partial order < : s . It is a binary re lation o n D w here s g 1 1 < : s s g 2 2 is equi valen t to s 1 < : s 2 ∧ ( g 1 = g 2 ∨ g 2 = ? ) . As pointed out in the intr oducti on, we assume in all that paper that the signatur es consid er ed do not have multiple inheri tance and that we do not allow function symbol overlo ading . Giv en these notions , we refine the notion of conte xt Γ as a set of subtyping declaration s (type,ty pe) and pairs (v ariable ,type), and (function symbol,rank). This is ex presse d by the follo wing grammar: Γ :: = ∅ | Γ 1 ∪ Γ 2 | s ? 1 < : ∗ s s ? 2 | x : s g | x ∗ : s ℓ | f : s ? 1 , . . . , s ? n → s f | ℓ : ( s ? 1 ) ∗ → s ℓ where < : ∗ s corres ponds to the reflexi ve transiti ve closure of < : s and conte xt access is refined by the func- tion sortOf ( Γ , e ) : Γ × T ( F ∪ F v , X ) → D which retur ns the type of term e in the conte xt Γ : sortOf ( Γ , x ) = s g , if x : s g ∈ Γ sortOf ( Γ , f ( e 1 , . . . , e n )) = s f , if f : s ? 1 , . . . , s ? n → s f ∈ Γ sortOf ( Γ , x ∗ ) = s ℓ , if x ∗ : s ℓ ∈ Γ sortOf ( Γ , ℓ ( e 1 , . . . , e n , e )) = s ℓ , if ℓ : ( s ? 1 ) ∗ → s ℓ ∈ Γ where x ∈ X , f ∈ F , ℓ ∈ F v , g ∈ F ∪ F V ∪ { ? } and s ? , s f , s g , s ℓ ∈ D . The conte xt has at most one dec laratio n of type or signature per term since o ver loadin g is forbi dden. This means that for e ∈ T ( F ∪ F v , X ) and s g 1 1 , s g 2 2 (where g 1 , g 2 ∈ F ∪ F v ∪ { ? } and s g 1 1 , s g 2 2 ∈ D ) if e : s g 1 1 ∈ Γ and e : s g 2 2 ∈ Γ then s g 1 1 = s g 2 2 . W e deno te by Γ ( s g 1 1 < : s s g 2 2 ) the fact that s g 1 1 < : s s g 2 2 belong s to Γ . 2.2 T ype checking algorithm In Fig. 2 we giv e a type check ing system to man y-sorte d v ariadi c terms apply ing associati ve matching . The rules are standard excep t for the use of decorate d types. The most interesting rules are those that apply to lists. They are three: [ T - E M P T Y ] c hecks if a empty list has the same type declared in Γ ; [ T - E L E M ] is similar to [ T - F U N ] but is applied to lists; and [ T- M E R G E ] is applied to a concat enatio n of two lists of type s ℓ in Γ , resultin g in a new lis t with same type s ℓ . The type checkin g algorithm reads deriv ations bottom-up. Since the rule [ S U B ] can be applied to any kind of term, we consider a strate gy w here it is appli ed iff no other typing rule can be applied . In practic e, [ S U B ] will be combined w ith [ T - V A R ], [ T- F U N ] and [ T - E L E M ] and the type s ? 1 which appears in the premise will be defined acco rding to the result of function sortOf ( Γ , e ) . The algorithm stops if it reache s the [ T - V A R ] or [ T - S V A R ] cases, ensuring that the original expr ession is well-typed, or if none of the type check ing rules can be applied, raising an error . Example 2.2. Let Γ = { ℓ : ( Z ? ) ∗ → Z ℓ , one : → N one , x ∗ : Z ℓ , z ∗ : Z ℓ , y : Z ? , N ? < : s Z ? } . Then the expr es- sion ℓ ( x ∗ , y , z ∗ ) ≺ ≺ [ Z ? ] ℓ ( one ()) − → ( y ) is well-typ ed and its deduction tree is given in F ig. 3 3 T ype infer ence The type sy stem presente d in Section 2 needs rule s to cont rol its use in orde r to find the e xpected deduc- tion tree of an exp ressio n. W ithou t these rules it is possible to fi nd more than one deduction tree for the Claude Kirchner , Pierre-Etienne Moreau, Cláudia T a vares 55 Γ ( x : s g ) ⊢ x : s g T - V A R Γ ( x ∗ : s ℓ ) ⊢ x ∗ : s ℓ T - S V A R where g ∈ F ∪ F v ∪ { ? } Γ ⊢ e 1 : s ? 1 . . . Γ ⊢ e n : s ? n Γ ( f : s ? 1 , . . . , s ? n → s f ) ⊢ f ( e 1 , . . . , e n ) : s f T - F U N Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ ) ⊢ ℓ () : s ℓ T - E M P T Y Γ ⊢ ℓ ( e 1 , . . . , e n ) : s ℓ Γ ⊢ e : s ? 1 Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ ) ⊢ ℓ ( e 1 , . . . , e n , e ) : s ℓ T - E L E M Γ ⊢ ℓ ( e 1 , . . . , e n ) : s ℓ Γ ⊢ e : s ℓ Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ ) ⊢ ℓ ( e 1 , . . . , e n , e ) : s ℓ T - M E R G E if sort Of ( Γ , e ) 6 = s ℓ and e 6 = x ∗ if sort Of ( Γ , e ) = s ℓ Γ ⊢ e : s g 1 1 Γ ( s g 1 1 < : s s g ) ⊢ e : s g S U B Γ ⊢ e : s h Γ ⊢ e : s ? G E N where g , g 1 ∈ F ∪ F v ∪ { ? } if sort Of ( Γ , e ) = s h , where h ∈ F ∪ F v Γ ⊢ e 1 : s ? Γ ⊢ e 2 : s ? Γ ⊢ ( e 1 ≺ ≺ [ s ? ] e 2 ) : w t T - M AT C H Γ ⊢ ( cond 1 ) : w t . . . Γ ⊢ ( cond n ) : w t Γ ⊢ ( cond 1 ∧ . . . ∧ cond n ) : w t T - C O N J Γ ⊢ ( cond ) : w t Γ ⊢ e 1 : s g 1 1 . . . Γ ⊢ e n : s g n n Γ ⊢ ( cond − → ( e 1 , . . . , e n )) : w t T - R U L E if sort Of ( Γ , e i ) = s g i i , where g i ∈ F ∪ F v ∪ { ? } fo r i ∈ [ 1 , n ] Figure 2: T ype check ing rules. same expre ssion. For instance, in Example 2.2, the rule [ S U B ] can be applied to the lea ves resulti ng of applic ation of rule [ T - V A R ]. The result ing tree will still be a valid deducti on tree since the v ariables in the lea ves will ha ve typ e N ? instea d of type Z ? declar ed in the cont ext a nd N ? < : s Z ? . For that re ason, we are interes ted in defining another type system able to infer the most general types of terms. W e add type v ariabl es in the set of types (defined up to here as a set of decorated sorts) to describe a possibly infinite set of decorated sorts. The set of types T y pe ( D ∪ { w t } , V ) is gi ven by a set of deco rated sorts D , a set of type va riables V an d a special sort wt : τ :: = α | s g | wt where τ ∈ T y pe ( D ∪ { wt } , V ) , α ∈ V , g ∈ F ∪ F v ∪ { ? } and s g ∈ D . In order to b uild the subtyping rule into the rules, we use a const rain t set C to store all equali ty and subtyp ing constrain ts. These const raints limit types that terms can hav e. The languag e C is built from the set of types T y pe ( D ∪ { wt } , V ) and the operators “ = s ” (equal ity) and “ < : s ” (ex tension to T y pe ( D ∪ { w t } , V ) of the parti al order defined in Subsection 2.1): c :: = τ 1 = s τ 2 | τ 1 < : s τ 2 where c ∈ C , τ 1 , τ 2 ∈ T y pe ( D ∪ { wt } , V ) . A substitu tion σ is said to satisfy an equatio n τ 1 = s τ 2 if σ τ 1 = σ τ 2 . Moreo ver , σ is said to satisfy a subtyp e relatio n τ 1 < : s τ 2 if σ τ 1 < : s σ τ 2 . Thus, σ is a solution for C if it satisfies all constrain ts in C . This is written σ | = C . The set V ( C ) denote s the set of type v ariables in C . 56 A T ype System for T om Γ ⊢ ℓ () : Z ℓ T- E M P T Y Γ ⊢ x ∗ : Z ℓ T- S V A R Γ ⊢ ℓ ( x ∗ ) : Z ℓ T- M E R G E Γ ⊢ y : Z ? T- V A R Γ ⊢ ℓ ( x ∗ , y ) : Z ℓ T- E L E M Γ ⊢ z ∗ : Z ℓ T- S V A R Γ ⊢ ℓ ( x ∗ , y , z ∗ ) : Z ℓ T- M E R G E Γ ⊢ ℓ ( x ∗ , y , z ∗ ) : Z ? T- G EN · · · · · · · · · · · · · · · · · · · · Γ ⊢ ℓ () : Z ℓ T- E M P T Y Γ ⊢ one () : N one T- F UN Γ ⊢ one () : N ? G E N Γ ⊢ one () : Z ? S U B Γ ⊢ ℓ ( one ()) : Z ℓ T- E L E M Γ ⊢ ℓ ( one ()) : Z ? T- G EN Γ ⊢ ( ℓ ( x ∗ , y , z ∗ ) ≺ ≺ [ Z ? ] ℓ ( one ())) : w t T- M AT C H Γ ⊢ y : Z ? T- V A R Γ ⊢ ( ℓ ( x ∗ , y , z ∗ ) ≺ ≺ [ Z ? ] ℓ ( one ()) − → ( y )) : w t T- R U L E Figure 3: T ype checking exa mple. Constrai nts are calculated accordin g to the applic ation of rules of type inference system (see Fig. 4) where we can read the judgment Γ ⊢ c t e : τ • C as “the term e has type τ under assumptio ns Γ w hene ver the constra ints C are satisfied”. More formally , this judgmen t states that ∀ σ  ( σ | = C → σ Γ ⊢ e : σ τ ) . 3.1 T ype inferen ce alg orithm In F ig. 4 we gi ve a type inferen ce syste m w ith constraints . In order to infer the type of a gi ven expres- sion π , the conte xt Γ is initialized to: 1) subtyp e declarati ons of the form s ? 1 < : s s ? 2 where s ? 1 and s ? 2 ∈ D ; 2) a pa ir of the for m ( f : s ? 1 , . . . , s ? n → s f ) for ea ch syntactic operat or f occurrin g in π where s ? i , s f ∈ D for i ∈ [ 1 , n ] ; 3) a p air of the form ( ℓ : s ? 1 ∗ → s ℓ ) for each v ariadic operator ℓ occurrin g in π where s ? 1 , s ℓ ∈ D ; 4) a pair of the form ( x : α ) for each var iable x occurring in π where α ∈ V is a fresh type v ariable ; 5) a pair of the form ( x ∗ : α ) for each star va riable x ∗ occurr ing in π where α ∈ V is a fresh type v ariable. Moreo ver , each type vari able introduce d in a sub-deri vati on is a fresh type variab le and the fresh type v ariabl es in diff erent sub-d eri vation s are distinct . As in Section 2.2, w e explai n the rules concernin g lists: [ C T - E M P T Y ] infers for an empty list ℓ () a type vari able α with the constraint α = s ℓ , s ℓ gi ven by the rank of ℓ ; [ C T- E L E M ] treats applicat ions of lists to elements w hich are neither lists with the same functi on symbol nor star v ariable s; [ C T - M E R G E ] is applied to concate nate two lists of same type s ℓ ; and [ C T - S TA R ] is applied to concatenate a list and a star var iable of the same type s ℓ . Example 3.1. Let Γ = { ℓ : ( Z ? ) ∗ → Z ℓ , one : → N one , x ∗ : α 1 , y : α 2 , z ∗ : α 3 , N ? < : s Z ? } . Then the expr es- sion ℓ ( x ∗ , y , z ∗ ) ≺ ≺ [ α 4 ] ℓ ( one ()) − → ( y ) is well-typed and the deduction tr ee is given in Fi g. 5. 3.2 Constraint resolution In Fig. 6 w e propose an algorithm to decide whether a gi ven constraint set C has a solution, w here g 1 , g 2 ∈ F ∪ F v ∪ { ? } . W e denote by s g 1 < : s s ′ g 2 ∈ Γ the fact that there exists s 1 , . . . , s n such that s ? < : s ? 1 ∈ Γ , s ? 1 < : s ? 2 ∈ Γ , . . . , s ? n < : s ′ ? ∈ Γ an d ( g 1 = g 2 or g 2 = ? ) . If the a lgorith m stops without failure then C is s aid to be in solved form . While solving a constr aint set C we wish to make sure, after each applicatio n o f a constrain t resolution rule, that the constraint set at hand is satisfiable, so as to detect errors as soon as possibl e. Therefo re w e Claude Kirchner , Pierre-Etienne Moreau, Cláudia T a vares 57 Γ ( x : τ ) ⊢ c t x : α • { α = s τ } C T - V A R Γ ( x ∗ : α 1 ) ⊢ c t x ∗ : α • { α 1 = s α } C T - S V A R Γ ⊢ c t e 1 : α 1 • C 1 . . . Γ ⊢ c t e n : α n • C n Γ ( f : s ? 1 , . . . , s ? n → s f ) ⊢ c t f ( e 1 , . . . , e n ) : α • { α = s s f } n S i = 1 C i ∪ { α i < : s s ? i } C T - F U N Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ ) ⊢ c t ℓ () : α • { α = s s ℓ } C T - E M P T Y Γ ⊢ c t ℓ ( e 1 , . . . , e n ) : α • C 1 Γ ⊢ c t e : α 1 • C 2 Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ ) ⊢ c t ℓ ( e 1 , . . . , e n , e ) : α • { α = s s ℓ , α 1 < : s s ? 1 } ∪ C 1 ∪ C 2 C T - E L E M if sortOf ( Γ , e ) 6 = s ℓ and e 6 = x ∗ Γ ⊢ c t ℓ ( e 1 , . . . , e n ) : α • C 1 Γ ⊢ c t e : α • C 2 Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ ) ⊢ c t ℓ ( e 1 , . . . , e n , e ) : α • { α = s s ℓ } ∪ C 1 ∪ C 2 C T - M E R G E if sortOf ( Γ , e ) = s ℓ Γ ⊢ c t ℓ ( e 1 , . . . , e n ) : α • C 1 Γ ⊢ c t x ∗ : α • C 2 Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ ) ⊢ c t ℓ ( e 1 , . . . , e n , x ∗ ) : α • { α = s s ℓ } ∪ C 1 ∪ C 2 C T - S TA R Γ ⊢ c t e 1 : α 1 • C 1 Γ ⊢ c t e 2 : α 2 • C 2 Γ ⊢ c t ( e 1 ≺ ≺ [ τ ] e 2 ) : w t • { α 1 < : s τ , α 2 = s τ } ∪ C 1 ∪ C 2 C T - M A T C H Γ ⊢ c t ( cond 1 ) : w t • C 1 . . . Γ ⊢ c t ( cond n ) : w t • C n Γ ⊢ c t ( cond 1 ∧ . . . ∧ cond n ) : w t • n S i = 1 C i C T - C O N J Γ ⊢ c t ( cond ) : w t • C cond Γ ⊢ c t e 1 : τ 1 • C 1 . . . Γ ⊢ c t e n : τ n • C n Γ ⊢ c t ( cond − → ( e 1 , . . . , e n )) : w t • C cond n S i = 1 C i C T - R U L E if sortOf ( Γ , e i ) = τ i , for i ∈ [ 1 , n ] where τ i ∈ T y pe ( D ∪ { wt } , V ) Figure 4: T ype infere nce rules. must combine the rules for error detection and constra int resolution in order to keep C in solv ed form. The rules for the constraint resolution algorithm are provide d in Fig. 7, w here g , g 1 , g 2 ∈ F ∪ F v ∪ { ? } . The rules (1)-(14) are recurs i vel y applie d ove r C . M ore precisely , rules (1)-(3) work as a garbage collector remov ing constrain ts that are no more usef ul. Rules (4) and (5) generate σ . Rules (6) and (7) gene rate more simplified constraints . R ules (8)-(12) generate σ and simplified constraints by antisymmetric and transit i ve subty pe closu re. Rules (13) and (14) are applie d when none of pre vious rules can be applied genera ting a new σ from a const raint ove r a type var iable that has no other constraints. T he algorith m 58 A T ype System for T om Γ ⊢ ct ℓ () : α 5 • C 3 = { α 5 = s Z ℓ } C T- E M P T Y Γ ⊢ ct x ∗ : α 5 • C 4 = { α 5 = s α 1 } C T- S V A R Γ ⊢ ct ℓ ( x ∗ ) : α 5 • C 2 = { α 5 = s Z ℓ } ∪ C 3 ∪ C 4 C T- S TA R Γ ⊢ ct y : α 8 • C 5 = { α 8 = s α 2 } C T- V A R Γ ⊢ ct ℓ ( x ∗ , y ) : α 5 • C 1 = { α 5 = s Z ℓ , α 8 < : s Z ? } ∪ C 2 ∪ C 5 C T- E L E M · · · · · · · · Γ ⊢ ct z ∗ : α 5 • C 6 = { α 5 = s α 3 } C T- S V A R Γ ⊢ ct ℓ ( x ∗ , y , z ∗ ) : α 5 • C p = { α 5 = s Z ℓ } ∪ C 1 ∪ C 6 C T- S TA R (1) Γ ⊢ ct ℓ () : α 6 • C 7 = { α 6 = s Z ℓ } C T- E M P T Y Γ ⊢ ct one () : α 7 • C 8 { α 7 = s N one } C T- F U N Γ ⊢ ct ℓ ( one ()) : α 6 • C s = { α 6 = s Z ℓ , α 7 < : s Z ? } ∪ C 7 ∪ C 8 C T- E L E M (2) (1) (2) Γ ⊢ ct ( ℓ ( x ∗ , y , z ∗ ) ≺ ≺ [ α 4 ] ℓ ( one ())) : w t • C cond = { α 5 < : s α 4 , α 6 = s α 4 } C T- M A T C H Γ ⊢ ct y : α 9 • C 10 = { α 9 = s α 2 } C T- V A R Γ ⊢ ct ( ℓ ( x ∗ , y , z ∗ ) ≺ ≺ [ α 4 ] ℓ ( one ()) − → ( y )) : w t • C r = { α 2 = s α 2 } ∪ C cond ∪ C 10 C T- R U L E Figure 5: T ype inference exa mple. (1) { s g 1 1 < : s α , α < : s s g 2 2 } ⊎ C ′ = ⇒ f ail if s g 1 1 < : s s g 2 2 / ∈ Γ (2) { s g 1 1 < : s α , s g 2 2 < : s α } ⊎ C ′ = ⇒ f ail if 6 ∃ s  ( s g 1 1 < : s s ? ∈ Γ ∧ s g 2 2 < : s s ? ∈ Γ ) (3) { α < : s s g 1 1 , α < : s s g 2 2 } ⊎ C ′ = ⇒ f ail if ( s g 1 1 < : s s g 2 2 / ∈ Γ ∧ s g 2 2 < : s s g 1 1 / ∈ Γ ) (4) { s g 1 1 < : s s g 2 2 } ⊎ C ′ = ⇒ f ail if s g 1 1 < : s s g 2 2 / ∈ Γ (5) { s g 1 1 = s g 2 2 } ⊎ C ′ = ⇒ f ail if s 1 6 = s 2 ∨ g 1 6 = g 2 Figure 6: Rules for detecti on of errors in a constraint set C . stops if: a rule returns C = ∅ , then the algorithm returns the solution σ ; if C reaches a non-solv ed form, then the algorit hm for detectio n of errors retur ns f ail ; or if C reaches a normal form dif ferent from the empty set, then the algor ithm returns an e rror . W e say that the a lgorith m is failing if it returns eithe r fails or an error . Example 3.2. Let Γ = { ℓ : ( Z ? ) ∗ → Z ℓ , one : → N one , x ∗ : α 1 , y : α 2 , z ∗ : α 3 , N ? < : s Z ? } and C cond = { α 5 = s Z ℓ , α 10 = s α 1 , α 5 = s Z ℓ , α 10 = s Z ℓ , α 9 = s α 2 , α 5 = s Z ℓ , α 9 < : s Z ? , α 8 = s α 3 , α 5 = s Z ℓ , α 8 = s Z ℓ , α 6 = s Z ℓ , α 7 = s N one , α 6 = s Z ℓ , α 7 < : s Z ? , α 5 < : s α 4 , α 6 = s α 4 , α 2 = s α 2 } fr om the Example 3.1. Let σ = ∅ and C = C cond . The const rai nt r esolution algorithm starts by: 1. Application of sequ ence of rules (4), (1) an d (5) g ener ating { α 2 < : s Z ? , N one < : s Z ? , Z ℓ < : s Z ℓ } ∪ C and { α 5 7→ Z ℓ , α 10 7→ α 1 , α 1 7→ Z ℓ , α 9 7→ α 2 , α 8 7→ α 3 , α 3 7→ Z ℓ , α 6 7→ Z ℓ , α 7 7→ N one , α 4 7→ Z ℓ } ∪ σ 2. Application of rules (1), (2) and (3) gen era ting { α 2 < : s Z ? } and σ ; Claude Kirchner , Pierre-Etienne Moreau, Cláudia T a vares 59 (1) { τ = s τ } ⊎ C ′ , σ = ⇒ C ′ , σ (2) { τ < : s τ } ⊎ C ′ , σ = ⇒ C ′ , σ (3) { s g 1 1 < : s s g 2 2 } ⊎ C ′ , σ = ⇒ C ′ , σ if s g 1 1 < : s s g 2 2 ∈ Γ (4) { α = s τ } ⊎ C ′ , σ = ⇒ [ α 7→ τ ] C ′ , { α 7→ τ } ∪ σ (5) { τ = s α } ⊎ C ′ , σ = ⇒ [ α 7→ τ ] C ′ , { α 7→ τ } ∪ σ (6) { s g 1 1 < : s α , s g 2 2 < : s α } ⊎ C ′ , σ = ⇒ { s ? < : s α } ∪ C ′ , σ if ∃ s  ( s g 1 1 < : s s ? ∈ Γ ∧ s g 2 2 < : s s ? ∈ Γ ) (7a) { α < : s s g 1 1 , α < : s s g 2 2 } ⊎ C ′ , σ = ⇒ { α < : s s g 1 1 } ∪ C ′ , σ if ( s g 1 1 < : s s g 2 2 ∈ Γ ) (7b) { α < : s s g 1 1 , α < : s s g 2 2 } ⊎ C ′ , σ = ⇒ { α < : s s g 2 2 } ∪ C ′ , σ if ( s g 2 2 < : s s g 1 1 ∈ Γ ) (8) { τ 1 < : s τ 2 , τ 2 < : s τ 1 } ⊎ C ′ , σ = ⇒ { τ 1 = s τ 2 } ∪ C ′ , σ (9) { α 1 < : s α , α < : s α 2 } ⊎ C ′ , σ = ⇒ { α 1 < : s α 2 } ∪ [ α 7→ α 2 ] C ′ , { α 7→ α 2 } ∪ σ (10) { s g < : s α , α < : s α 1 } ⊎ C ′ , σ = ⇒ { s g < : s α 1 } ∪ [ α 7→ α 1 ] C ′ , { α 7→ α 1 } ∪ σ (11) { α 1 < : s α , α < : s s g } ⊎ C ′ , σ = ⇒ { α 1 < : s s g } ∪ [ α 7→ α 1 ] C ′ , { α 7→ α 1 } ∪ σ (12) { s g 1 1 < : s α , α < : s s g 2 2 } ⊎ C ′ , σ = ⇒ [ α 7→ s g 2 2 ] C ′ , { α 7→ s g 2 2 } ∪ σ if s g 1 1 < : s s g 2 2 ∈ Γ (13) { α < : s τ } ⊎ C ′ , σ = ⇒ C ′ , { α 7→ τ } ∪ σ if α / ∈ V ( C ′ ) (14) { τ < : s α } ⊎ C ′ , σ = ⇒ C ′ , { α 7→ τ } ∪ σ if α / ∈ V ( C ′ ) Figure 7: Constra int resolution rules in context Γ . 3. Application of ru le (13) g ener ating ∅ and { α 2 7→ Z ? } ∪ σ , th e algori thm then stops and r eturns σ pr ovi ding a substitut ion f or all type variables in the deducti on tre e of ℓ ( x ∗ , y , z ∗ ) ≺ ≺ [ α 4 ] ℓ ( one ()) − → ( y ) . 4 Pr operties Since our type checking system and our type inference system addres s the same issue, we must chec k two proper ties. First, we show that ev ery typing judgment that can be deri ved from the inference rules also follo ws from the checking rules (Theorem 4.2), in particu lar the soundness . Then we sho w that a solution gi ve n by the check ing rules can be ex tended to a solution propose d by the inference rules (Theorem 4.4). Definition 4.1 (Solution ) . Let Γ be a co nte xt and e a term. • A solutio n for ( Γ , e ) is a pair ( σ , T 1 ) such that σ Γ ⊢ σ e : T 1 , wher e T 1 ∈ D ∪ { wt } . • Assuming a well-formed sequent Γ ⊢ e : τ • C , a solutio n for ( Γ , e , τ , C ) is a pair ( σ , T 2 ) such that σ satisfi es C and σ τ < : s T 2 , wher e T 2 ∈ D ∪ { wt } and τ ∈ T y pe ( D ∪ { wt } , V ) . Theor em 4.2 (Soundn ess of constraint typing) . Supp ose that Γ ⊢ c t e : τ • C is a valid sequent. If ( σ , s g ) is a solutio n for ( Γ , e , τ , C ) , then it is also a solution for ( Γ , e ) (i.e. e is well-ty ped in Γ ). Pr oof. By induction on the gi ven constraint typing deri v ation for Γ ⊢ c t e : τ • C . W e just detail the most note worthy cases of this proof. 60 A T ype System for T om Case C T - E L E M : e = ℓ ( a 1 , . . . , a n , a ) τ = α Γ ⊢ c t ℓ ( a 1 , . . . , a n ) : α • C 1 Γ ⊢ c t a : α 1 • C 2 C = C 1 ∪ C 2 ∪ { α = s s ℓ 2 , α 1 < : s s ? 1 } W e are g i ven that ( σ , s g ) is a solution for ( Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ 2 ) , e , α , C ) , that is, σ satisfies C and σ α < : s s g . Since ( σ , s g ) satisfies C 1 and C 2 , ( σ , σ α ) and ( σ , σ α 1 ) are solution s for ( Γ , ℓ ( a 1 , . . . , a n ) , α , C 1 ) and ( Γ , a , α 1 , C 2 ) , respect i vely . By the indu ction hypothesi s, we ha ve σ Γ ⊢ σ ( ℓ ( a 1 , . . . , a n )) : σ α and σ Γ ⊢ σ a : σ α 1 . Since σ α 1 < : s s ? 1 , by S U B we obtai n σ Γ ⊢ σ a : s ? 1 . Since σ α = s ℓ 2 , by T - E L E M w e obtain σ ( Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ 2 )) ⊢ σ ( ℓ ( a 1 , . . . , a n , a )) : s ℓ 2 . By S U B we obtain σ ( Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ 2 )) ⊢ σ ( ℓ ( a 1 , . . . , a n , a )) : s g , as required . Case C T - M E R G E : e = ℓ ( a 1 , . . . , a n , a ) τ = α Γ ⊢ c t ℓ ( a 1 , . . . , a n ) : α • C 1 Γ ⊢ c t a 1 : α • C 2 C = C 1 ∪ C 2 ∪ { α = s s ℓ 2 } W e are g i ven that ( σ , s g ) is a solution for ( Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ 2 ) , e , α , C ) , that is, σ satisfies C and σ α < : s s g . Since ( σ , s g ) satisfies C 1 and C 2 , ( σ , σ α ) and ( σ , σ α 1 ) are solution s for ( Γ , ℓ ( a 1 , . . . , a n ) , α , C 1 ) and ( Γ , a , α , C 2 ) . By the induct ion hypot hesis, we hav e σ Γ ⊢ σ ( ℓ ( a 1 , . . . , a n )) : σ α and σ Γ ⊢ σ a : σ α 1 . Since σ α = s ℓ 2 , by T - M E R G E we obt ain σ ( Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ 2 )) ⊢ σ ( ℓ ( a 1 , . . . , a n , a )) : s ℓ 2 . By S U B we o btain σ ( Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ 2 )) ⊢ σ ( ℓ ( a 1 , . . . , a n , a )) : s g , as requ ired. Case C T - M AT C H : e = a 1 ≺ ≺ [ τ 1 ] a 2 τ = wt Γ ⊢ c t a 1 : α 1 • C 1 Γ ⊢ c t a 2 : α 2 • C 2 C = C 1 ∪ C 2 ∪ { α 1 < : s τ 1 , α 2 = s τ 1 } W e are giv en that ( σ , wt ) is a solutio n for ( Γ , e , w t , C ) , that is, σ satisfies C and σ wt < : s wt . Since ( σ , wt ) satisfies C 1 and C 2 , ( σ , σ α 1 ) and ( σ , σ α 2 ) are solutions for ( Γ , a 1 , α 1 , C 1 ) and ( Γ , a 2 , α 2 , C 2 ) , respec ti vel y . By the indu ction hypothesis , we ha ve σ Γ ⊢ σ a 1 : σ α 1 and σ Γ ⊢ σ a 2 : σ α 2 . Since σ α 1 < : s σ τ 1 , by S U B we obtain σ Γ ⊢ σ a 1 : σ τ 1 . Since σ α 2 = σ τ 1 , by T - M AT C H we obtain σ Γ ⊢ σ ( a 1 ≺ ≺ [ τ 1 ] a 2 ) : wt , as required. Definition 4.3 (Normal form of typing deri v ation) . A typing deri vation is in normal form if it does not have successi ve application s of rule [ S U B ] . Theor em 4.4 (Completen ess of constraint typing) . Suppose that π = Γ ⊢ c t e : τ • C . Write V ( π ) for the set o f all type varia bles mention ed in t he la st rule used to derive π and write σ \ V ( π ) for t he su bstitu tion that is undefine d for all the variables in V ( π ) and otherwise behaves lik e σ . If ( σ , s g ) is a solution for ( Γ , e ) and d om ( σ ) ∩ V ( π ) = ∅ , then ther e is some solutio n ( σ ′ , s g ) for ( Γ , e , τ , C ) such that σ ′ \ V ( π ) = σ . Pr oof. By i nducti on on the g i ven constra int typi ng de ri vation i n normal form, b ut we must take care with fresh names of v ariabl es. W e just detail the most note worthy cases of this proof. Case C T - E L E M : e = ℓ ( a 1 , . . . , a n , a ) τ = α π 1 = Γ ⊢ c t ℓ ( a 1 , . . . , a n ) : α • C 1 π 2 = Γ ⊢ c t a : α 1 • C 2 C = C 1 ∪ C 2 ∪ { α = s s ℓ 2 , α 1 < : s s ? 1 } V ( π ) = { α , α 1 } sortOf ( Γ , a ) 6 = s ℓ 2 Claude Kirchner , Pierre-Etienne Moreau, Cláudia T a vares 61 From the assumption that ( σ , s g ) is a solution for ( Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ 2 ) , ℓ ( a 1 , . . . , a n , a )) and d om ( σ ) ∩ V ( π ) = ∅ , we hav e σ ( Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ 2 )) ⊢ σ ( ℓ ( a 1 , . . . , a n , a )) : s g . This can be deri ved from: 1) T - M E R G E , 2) T- E L E M or 3) S U B . In all those cases, w e must exhib it a substitutio n σ ′ such that: (a) σ ′ \ V ( π ) agrees with σ ; (b) σ ′ α < : s s g ; (c) σ ′ satisfies C 1 and C 2 ; and (d) σ ′ satisfies { α = s s ℓ 2 , α 1 < : s s ? 1 } . W e reas on by cases as follo ws: 1. By T - M E R G E we ass ume that s g = s ℓ 2 and we know that σ Γ ⊢ σ ( ℓ ( a 1 , . . . , a n )) : s ℓ 2 and σ Γ ⊢ σ a : s ℓ 2 . But since we cannot fi nd a type s ℓ 3 such that s ℓ 3 < : s s ℓ 2 , σ Γ ⊢ σ a : s ℓ 2 canno t be deriv ed ev en from S U B . Thus T - M E R G E is not a relev ant case. 2. By T- E L E M we assume that s g = s ℓ 2 and we kno w that σ Γ ⊢ σ ( ℓ ( a 1 , . . . , a n )) : s ℓ 2 and σ Γ ⊢ σ a : s ? 1 . By the induct ion hypothesis, there are solutions ( σ 1 , s ℓ 2 ) for ( Γ , ℓ ( a 1 , . . . , a n ) , α , C 1 ) and ( σ 2 , s ? 1 ) for ( Γ , a , α 1 , C 2 ) , and d om ( σ 1 ) \ V ( π 1 ) = ∅ = d om ( σ 2 ) \ V ( π 2 ) . Define σ ′ = { α 7→ s ℓ 2 , α 1 7→ s ? 1 } ∪ σ ∪ σ 1 ∪ σ 2 . Condition s (a), (b), (c) and (d) are obviousl y satisfied. Thus, we see that ( σ ′ , s g ) is a soluti on for ( Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ 2 ) , ℓ ( a 1 , . . . , a n , a ) , α , C ) . 3. By S U B we a ssume that s ℓ 2 < : s s g ∈ Γ and we know th at σ ( Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ 2 )) ⊢ σ ( ℓ ( a 1 , . . . , a n , a )) : s ℓ 2 . This must be deri ved from T - E L E M , similar to case (2). Case C T - M E R G E : e = ℓ ( a 1 , . . . , a n , a ) τ = α π 1 = Γ ⊢ c t ℓ ( a 1 , . . . , a n ) : α • C 1 π 2 = Γ ⊢ c t a : α • C 2 C = C 1 ∪ C 2 ∪ { α = s s ℓ 2 } V ( π ) = { α } sortOf ( Γ , a ) = s ℓ 2 From the assumption that ( σ , s g ) is a solution for ( Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ 2 ) , ℓ ( a 1 , . . . , a n , a )) and d om ( σ ) ∩ V ( π ) = ∅ , we hav e σ ( Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ 2 )) ⊢ σ ( ℓ ( a 1 , . . . , a n , a )) : s g . This can be deri ved from: 1) T - M E R G E , 2) T- E L E M or 3) S U B . In all those cases, w e must exhib it a substitutio n σ ′ such that: (a) σ ′ \ V ( π ) agrees with σ ; (b) σ ′ α < : s s g ; (c) σ ′ satisfies C 1 and C 2 ; and (d) σ ′ satisfies { α = s s ℓ 2 } . W e reason by cases as follo ws: 1. By T - M E R G E we ass ume that s g = s ℓ 2 and we know that σ Γ ⊢ σ ( ℓ ( a 1 , . . . , a n )) : s ℓ 2 and σ Γ ⊢ σ a : s ℓ 2 . By the induction hy pothes is, there are sol utions ( σ 1 , s ℓ 2 ) for ( Γ , ℓ ( a 1 , . . . , a n ) , α , C 1 ) and ( σ 2 , s ℓ 2 ) for ( Γ , a , α , C 2 ) , and d om ( σ 1 ) \ V ( π 1 ) = ∅ = d om ( σ 2 ) \ V ( π 2 ) . Define σ ′ = { α 7→ s ℓ 2 } ∪ σ ∪ σ 1 ∪ σ 2 . Conditio ns (a), (b), (c) and (d) are obvi ously satisfied. Thus, we see that ( σ ′ , s g ) is a solution for ( Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ 2 ) , ℓ ( a 1 , . . . , a n , a ) , α , C ) . 2. By T- E L E M we assume that s g = s ℓ 2 and we kno w that σ Γ ⊢ σ ( ℓ ( a 1 , . . . , a n )) : s ℓ 2 and σ Γ ⊢ σ a : s ? 1 . But, becaus e of the applica tion condition of T- E L E M , w e cannot find a type s ℓ 1 for σ a such that s ℓ 1 < : s s ? 1 , σ Γ ⊢ σ a : s ? 1 canno t be deri ved from G E N . L ike wise, since we canno t find a type s ℓ 3 for σ a such that s ℓ 3 < : s s ? 1 , σ Γ ⊢ σ a : s ? 1 canno t be deri ved e ven from S U B . Thus T - E L E M is not a rele vant case. 3. By S U B we a ssume that s ℓ 2 < : s s g ∈ Γ and we know th at σ ( Γ ( ℓ : ( s ? 1 ) ∗ → s ℓ 2 )) ⊢ σ ( ℓ ( a 1 , . . . , a n , a )) : s ℓ 2 . This must be deri ved from T - M E R G E , similar to case (1). Case C T - M AT C H : e = a 1 ≺ ≺ [ τ 1 ] a 2 τ = wt π 1 = Γ ⊢ c t a 1 : α 1 • C 1 π 2 = Γ ⊢ c t a 2 : α 2 • C 2 C = C 1 ∪ C 2 ∪ { α 1 < : s τ 1 , α 2 = s τ 1 } V ( π ) = { α 1 , α 2 , τ 1 } if τ 1 ∈ V V ( π ) = { α 1 , α 2 } if τ 1 / ∈ V 62 A T ype System for T om From the assumptio n that ( σ , wt ) is a solution for ( Γ , a 1 ≺ ≺ [ τ 1 ] a 2 ) and d om ( σ ) ∩ V ( π ) = ∅ , we ha ve σ Γ ⊢ σ ( a 1 ≺ ≺ [ τ 1 ] a 2 ) : wt . This must be deri ved from T - M A T C H , we know that σ Γ ⊢ σ a 1 : σ τ 1 and σ Γ ⊢ σ a 2 : σ τ 1 . By the inductio n hypothe sis, there are solution s ( σ 1 , σ τ 1 ) for ( Γ , a 1 , α 1 , C 1 ) and ( σ 2 , σ τ 1 ) for ( Γ , a 2 , α 2 , C 2 ) . W e must e xhibit a substitutio n σ ′ such that: (a) σ ′ \ V ( π ) agrees with σ ; (b) σ ′ wt < : s wt ; (c) σ ′ satisfies C 1 and C 2 ; and (d) σ ′ satisfies { α 1 < : s τ 1 , α 2 = s τ 1 } . Define σ ′′ = { α 1 7→ s g , α 2 7→ s g } ∪ σ ∪ σ 1 ∪ σ 2 , where s g ∈ D . Moreo ver , define σ ′ = σ ′′ ∪ { τ 1 7→ s g } if τ 1 ∈ V an d σ ′ = σ ′′ otherwis e. Conditions (a), (b), (c) and (d ) are ob viously satisfied. Thus, we see th at ( σ ′ , wt ) is a sol ution for ( Γ , ( a 1 ≺ ≺ [ τ 1 ] a 2 ) , wt , C ) . The constr aint resolu tion algorithm always terminates . More formally: Theor em 4.5 (T erminat ion of algorithm) . 1. the algori thm halts, either by faili ng or by ret urning a substitu tion, for all C ; 2. if the algorith m retu rns σ , then σ is a solu tion for C ; W e can already sketc h a proof of Theorem 4.5 followin g P ierce [15]. Pr oof. For part 1, define the de gr ee of a constrain t set C to be the pair ( m , n ) , w here m is the number of constr aints in C and n i s t he number o f subtyping c onstra ints in C . The algorithm ter minates immediately (with success in the case of an empty constraint set or failu re for an equation in vol ving two diff erent decora ted s orts) or makes recursi ve calls to itself with a constrai nt set of lexic ograph ically s maller degr ee. For par t 2, by induction on the number of recursi ve calls in the computation of the algorithm. 5 Conclusion In this paper w e ha ve prese nted a type system for the patte rn matchin g constr ucts of T om . The system is composed of type checking and type inferen ce algorithms with subtyping ov er sorts. Since T om also implements associati ve pattern matchi ng ov er va riadic opera tors, we were interested in defining both a way to disti nguish these from syntactic operators and checking and inferring their types. W e ha ve obtaine d the follo wing: our type inferen ce system is soun d and complete w .r .t. checking , sho wed by Theorems 4.4 a nd 4.2. This is the fi rst ste p to wards an ef fecti ve implemen tation, thus lead ing to a safer T om . Howe ver , we still need to in vestiga te type unicity that we belie ve to hold under our assumpti ons of non-o verload ing and non-multip le inheri tance. As we ha ve considered a subset of the T om language, future work will focus on extend ing the type system to handle the other construct ions of the languag e such as anti-patt erns [12, 13]. A s a slight ly more prospec ti ve researc h area, we also want paramet ric polymorp hism ov er types for T om : our type system will therefor e hav e to be able to handle that as well. Ackno wledgements W e would like to ackno wledge the numerou s discussions we had in the P rotheo and Pareo teams, espe- cially with Paul Brauner , on these topics durin g these last years as w ell as the construc ti ve and useful comments done by the anon ymous refereees. Claude Kirchner , Pierre-Etienne Moreau, Cláudia T a vares 63 Refer ences [1] Alexander Aiken and Edward L. W immers. Solving systems of s et con straints (extended abstra ct). In Se venth Annua l IEEE Symposium on Logic in Compu ter Scien ce , pag es 32 9–34 0. IEEE Comp uter Society Press, 1992. [2] H. Ait-Ka ci, A. Po delski, and G. Smolk a. A feature co nstraint system f or logic pro grammin g with entailmen t. Theor etical Computer Science , 122(1-2) :263–2 83, 1994. [3] Emilie B alland. Conceptio n d’u n lan gage déd ié à l’analyse et la transformation de pr ogrammes . Ph D thesis, Université Henr i Poincaré, 2009. [4] Emilie Balland, Claude Kirchner, an d Pierre-Etienn e Morea u. Form al Islan ds. In Michael John son and Varmo Vene, editors, 11th Interna tional Co nfer en ce on Algebraic Methodo logy and Software Technology , volume 4019 of LNC S , pages 51– 65. Springer , 2006. [5] Gilles Barthe, Horatiu Cirstea, Claude Kirc hner, an d Luigi Liq uori. Pure Patterns T yp e Systems. In Princi- ples of Pr ogramming Languages - POPL2003 , New Orleans, USA . A CM, January 2003. [6] Manuel Clavel, Fran cisco Durán, Steven Eker, Patrick Lincoln, Narciso Ma rtí-Oliet, José M eseguer , and Carolyn L. T alcott, editors. All About Maude , v o lume 4350 of Lectur e Notes in Computer Science . Sprin ger, 2007. [7] Hubert Com on. Com pletion of rewrite systems with membersh ip c onstraints. pa rt i: De duction rules. Journal of Symbolic Computation , 25(4 ):397– 419, 199 8. [8] Duggan Dominic. Finite subtype inferen ce with explicit poly morph ism. Sci. Comp ut. Pr ogram. , 39(1 ):57–9 2, 2001. [9] K. Futatsugi, J. A. G oguen , J.-P . Jouan naud, and J. Meseguer . Principles of OBJ-2. In B. Reid, editor, Pr oceedin gs 12th A CM Symp . on Principles of Pr ogramming Langu ages , pag es 52–66. A CM, 19 85. [10] J. A. Goguen, Claude Kirchner, Hélè ne Kirchner, A. Mégrelis, J. Meseguer , and T . W ink ler . An intr oduction to OBJ-3. In J.-P . Jouann aud and S. Kaplan, editors, Pr oceeding s 1 st International W orkshop on Co nditiona l T erm Rewriting Systems, Or say (F rance) , volume 30 8 o f Lectu r e Notes in Computer S cience , pages 2 58–2 63. Springer-V erlag, July 19 87. [11] Claus Hintermeier, Claud e Kirch ner, and Hélène Kir chner . Dynamically -typed compu tations for order-sorted equationa l presentations. Journal of Symbolic Computatio n , 25(4):455–5 26, 19[7]98. [12] Claude Kirc hner, Radu Kopetz, and Pierre-E tienne Mo reau. Anti-pattern match ing. In 16th Eur opean Symp o- sium on Pr ogramming , v olu me 4 421 o f Lectur e Notes in Computer S cience , pages 110 –124, Braga, Portugal, 2007. Springe r . [13] Radu Kopetz. Contraintes d’anti-filtrage et pr ogrammation par réécritur e . PhD th esis, Institut N ational Polytechniq ue de Lorraine, 2008. [14] Robin M ilner . A theo ry of typ e poly morph ism in prog ramming . Journal of Compu ter and System Sciences , 17:348 –375 , 1978 . [15] Benjamin C. Pierce. T yp es and Pr ogramming L angu ages . MIT Press, 200 2. Chapter 22. [16] Franço is Pottier . Simplifying subtypin g constraints: a theory . Inf. Comput. , 170(2) :153–1 83, 2001 . [17] Steffen van Bakel and Marib el Fern ández. No rmalization results for ty peable rewrite systems. Informatio n and Computation , 133(2) :73–1 16, 1997 .

Original Paper

Loading high-quality paper...

Comments & Academic Discussion

Loading comments...

Leave a Comment