Type Inference for Recursively Constrained Types and Its

Type Inference for Recursively Constrained Types and Its

Electronic Notes in Theoretical Computer Science URL httpwwwelsevier nlloc ateen tcsvo lume html pages Typ e Inference for Recursively Constrained Typ es and its Application to OOP Jonathan Eifrig Scott Smith Valery Trifonov Department of Computer Science The Johns Hopkins University Baltimore Maryland feifrig scott trifonovgcsjhuedu Abstract We dene a p owerful typ e inference mechanism with application to ob jectoriented programming The typ es inferred are recursively constrained typ es typ es that come with a system of constraints These typ es may b e viewed as generalizations of recur sivetyp es and Fb ounded p olymorphic typ es the forms of typ e that are necessary to prop erly enco de ob ject typings The base language we study ISo op incorp o rates state and records the two features critical to enco de ob jects in a nonob ject oriented language Soundness and completeness of the typ e inference algorithm are established by op erational means Our metho d for establishing these prop erties is somewhat novel We illustrate how the algorithm may b e fruitfully applied to infer typ es of ob jectoriented programs Intro duction This pap er addresses the problem of designing an ob jectoriented program ming language with an eectivetyp e inference mechanism Recently devel op ed programming languages including Standard ML and Haskell incorp orate typ e inference as a core comp onent of the language However typ e infer ence has yet to achieve practical application to ob jectoriented programming languages We strongly feel the core typ e features necessary to mo del ob jectoriented programming with typ e inference include a notion of subtyping and a no tion of recursively constrained p olymorphism a generalization of Fb ounded p olymorphism Recursively constrained typ es are typ es of the form n C with n reading where C is a set of typ e constraints of the form p ossibly containing free typ e variables These constraints maybe recursive in that a Partially supp orted by NSF grants CCR and CCR Partially supp orted byAFOSR grant F c Elsevier Science B V Eifrig Smith and Trifonov variable t could o ccur free in b oth and The recursive constraint set ft Nat t t t Natg expresses t t Nat so recursively constrained typ es subsume recursivetyp es We will use rctype to abbreviate recursively constrained typ e Polymorphic rc typ es are typ es t t n C where constraints n in C maycontain typ e variables t t free Polymorphic rc typ es generalize n the more wellknown b ounded typ es t in several ways First they are recursive so t could o ccur free in this is not allowed in b ounded typ es Typ es with t o ccurring free in are the socalled Fb ounded typ es Polymorphic rc typ es generalize Fb ounded typ es byallowing more than one upp er b ound on a typ e variable as well as allowing multiple lowerb ound constraints t This generalized form of p olymorphic typeisvery useful in typing ob jectoriented programs that are otherwise untypable irresp ective of the question of typ e inference An example of such a program is given in Section b elow It is not dicult to see how rc p olymorphism is useful in typing classes and ob jects for it is at least as useful as Fb ounded p olymorphism Classes mayhave socalled binary metho ds that refer to the typ e of ob jects of their own class for instance an ob ject with an equal metho d takes as parameter another ob ject of its own typ e Thus a selftyp e is needed And this self typ e needs to b e op enended since a class may b e extended wewishthetyp e of self to b e an ob ject with all the metho ds currently dened and p ossibly additional ones Polymorphic rc typ es capture this notion by constraining the p olymorphic selftyp e t to include the current metho ds for instance t nft equal t Bo ol g Binary metho ds haveproven very dicult to typ e in a general way it has even b een suggested that they b e disallowed One way to understand the usefulness of lower b ounds t in rc typ es are as generalizations of recursivetyp es It is p ossible to write an rc typ e t nf t g where lower b ound diers from upp er b ound it is a recursivetyp e if These generalized forms are useful as intermediate results pro duced during the typ e inference pro cess as partial forms of recursivetyp es During the typ e inference pro cess constraints are accumulated on typ es in a b ottomup fashion and so typ es at the leaves of typing pro ofs have small constraint sets and have fat constraint sets at the ro ot The lower b ound constrains the output of the typ e what prop erties ob jects of typ e must have if an ob ject of typ e is used ie passed to a function of typ e an additional upp erb ound constraint t will b e placed on the typ e bythetyp e inference mechanism and this could only b e contradictory if which follows by transitivitywas contradictory The upp er b ound is the dual of this constraining the input of the typ e what functions of typ e t must do The presence of multiple upp erb ound constraints or multiple lowerb ound constraints can b e understo o d as a restricted form of union and intersection typ e f t tg would b e equivalentto f tg if there were union Eifrig Smith and Trifonov typ es in the language a dual relationship exists b etween intersections and upp er b ounds We b elieve general union and intersection typ es cause to o many problems to b e worthwhile but this implicit restricted form is quite natural In this pap er wedevelop a typ e inference algorithm for the ISo op lan guage Inference Semantics of OOP ISo op is not an ob jectoriented lan guage however it has an expressive enough typ e system so that typ ed OOP may b e eectively enco ded within ISo op We take a translational approach b ecause we nd the factoring to help clarify ambiguities however there is also merit in studying languages where ob jects themselves are primitive and the concepts herein should eventually b e recast as primitiveobjecttypings ISo op s typ e system contains b oth subtyping and p olymorphic rc typ es We infer shallow p olymorphic rc typ es at letexpressions as in the HindleyMilner algorithm In addition the underlying language includes records and a notion of state for with these features it is p ossible to obtain an eective enco ding of ob jectoriented programming Records are needed so record sub typing can b e used to mo del ob ject subtyping Without state the critical stateholding prop erty of ob jects is lost Our approach to establishing the soundness of constrained typ e inference diers from other work in the literature In other approaches eg a metho d is given that either pro duces a satisfying assignmenttothe constraints and thus establishes their consistency or establishes that no such solution exists and the constraints are thus inconsistent In our approach an rc typ es constraint system is considered consistent if it do es not contain anyobvious contradictions suchas Nat Bo ol Weshow this view is sound without ever showing the consistent constraint systems have solutions In stead we directly establish a sub jectreduction prop ertyover a pro of of typing with consistent rc typ es at each no de We b elieve the standard metho d of nding solutions to the constraint sets can b e overly restrictive for it forces one to havearich enough typ e language or typ e mo del that can express the solutions as typ es or sets In our language for instance we exp ect general union and intersection typ es would b e required to express the solution of constraints as typ es but we do not wish to pay the p enaltyofhaving these typ es in our language Wealsotake a more primitive approach to establishing the completeness of typ e inference ie that all typable programs will successfully have some typ e inferred by the typ e inference algorithm We rst dene a restricted set of typing rules the inference rules for whichtyping derivations are deterministic Then these rules are shown equivalent in strength to the general form of rules without recourse to a principal typ es prop erty Related Work Anumber of typ e inference systems have b een develop ed that b ear on the typ e inference problem for OOPPap ers of Reynolds Cardelli and Mitchell are foundational pap ers in the eld that develop the basic concepts of Eifrig Smith and Trifonov constraints and subtyping Many pap ers have b een written since we fo cus on the more recentwork the most relevanttoours Kaes develops a typ e inference algorithm for a language containing p olymorphic and recursivetyp es and typ e constraints This work incorp orates subtyping constraints recursivetyp es and p olymorphism Kaes writes so called constrained typ es jC in close analogy to our rc typ es n C This approach cannot solve general recursive constraints t generates a non terminating unication problem in his system if t o ccurs free in while our approach can handle such constraints without diculty He do es allowa xing of such a constraintby replacing it with a recursivetyp e t butat the cost of an imp ortant loss of generalityKaestakes the standard approach to constraint consistencyby pro ducing a solution to the constraints He also intends to mo del overloading not record subtyping his system has no record typ es Sekiguchi and Yonezawa take an approach similar to Kaes but interpret as subtyping on record typ es making it more directly applicable to ob jectoriented programming Palsb erg Schwartzbach et al have written a numb er of pap ers concerning typ e inference for ob jects The main feature

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    22 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us