Jean-Pierre Talpin and Pierre Jouvelot
Total Page:16
File Type:pdf, Size:1020Kb
Submitted for publication in Information and Computation Academic Press The Typ e and Eect Discipline JeanPierre Talpin And Pierre Jouvelot Centre de Recherche en Informatique Ecole des Mines de Paris rue Saint Honore F Fontainebleau EMail talpinensm p fr and jouveloten smp f r Novemb er th Current Address JeanPierre Talpin Europ ean ComputerIndustry ResearchCenter ECRCGmbH Arab ella Strae D Munc hen Email jpecrcde Abstract The type and eect discipline is a new framework for reconstructing the principal typ e and the minimal eect of expressions in implicitly typ ed p olymorphic functional languages that supp ort imp erative constructs The typ e and eect discipline outp erforms other p olymorphic typ e systems Just as typ es abstract collections of concrete values eects denote imp erative op erations on regions Regions abstract sets of p ossibly aliased memory lo cations Eects are used to control typ e generalization in the presence of imp erative constructs while regions delimit observable sideeects The observable eects of an expression range over the regions that are free in its typ e environmentanditstyp e eects related to lo cal data structures can b e discarded during typ e reconstruction The typ e of an expression can b e generalized with resp ect to the typ e variables that are not free in the typ e environment or in the observable eect Intro ducing the typ e and eect discipline we dene b oth a dynamic and a static se mantics for an MLlike language and prove that they are consistently related We presenta reconstruction algorithm that computes the principal typ e and the minimal observable eect of expressions Weprove its correctness with resp ect to the static semantics Contents Intro duction Related Work A Core Language and its Semantics Syntax Store op erations Formulation of the Dynamic Semantics Semantic Ob jects Axioms and Rules of the Dynamic Semantics Dynamic Semantics of Store Op erations Static Semantics Free Variables and Substitutions Typ e Schemes and Environments Typ e Generalization Rules of the Static Semantics Static Semantics of Store Op erations Observation Criterion Formal Prop erties of the Static Semantics Consistency of Dynamic and Static Semantics The Reconstruction Algorithm Constrained Typ e Schemes Constrained Typ e Schemes of Store Op erations The Reconstruction Algorithm Constraint Resolution WellFormed Constraint Sets Unication Algorithm Correctness of the Reconstruction Algorithm Examples Comparison with the Related Work Comparative Examples Benchmarks Extensions Conclusion Intro duction Typ e inference Milner is the pro cess that automatically reconstructs the typ e of expressions in programming languages Polymorphic typ e inference in functional languages b ecomes problematic in the presence of imp erative constructs and muchinvestigations have b een devoted to this issue Tofte Leroy Weis Wright Eect systems Giord al Lucassen Lucassen Giord aim at a safe integration of imp erative programming features in functional languages Just as typ es describ e what expressions compute eects describ e how expressions compute and b oth can b e statically reconstructed Jouvelot Giord Talpin Jouvelot Sept Weintro duce the type and eect discipline a new framework for reconstructing the prin cipal typ e and the minimal eect of expressions in implicitly typ ed p olymorphic functional languages that supp ort imp erative constructs Just as typ es structurally abstract collections of concrete values regions abstract sets of p ossibly aliased memory lo cations while eects denote imp erative op erations on regions Eects control typ e generalization in the presence of imp erative constructs while regions are used to rep ort their only observable sideeects The observable eects of an expression range over the regions that are free in its typ e environment and its typ e Eects related to lo cal data structures can b e discarded during typ e reconstruction The typ e of an expression can b e generalized with resp ect to the variables that are neither free in the typ e environment nor in the observable eect In this pap er section presents the related work Then we describ e the dynamic sec tion and static sections and semantics of the language We state that the static and dynamic semantics are consistent section and that our reconstruction algorithm presented in sections and is correct with resp ect to the static semantics section We e some examples sections and that show that our approach surpasses previous giv techniques b efore suggesting some extensions section and concluding section Related Work Short of the adho c techniques used in the rst typ e inference systems the imp erativetyp e discipline Tofte is the classical way to deal with the problem of typ e generalization for p olymorphic functional languages in the presence of non referentially transparent constructs Its extension based on weak typ e variables is used in the implementation of Standard ML App el Mac Queen A dierent approach suggested in LeroyWeis consists in lab eling the typeofeach function with the set of the typ es of the value identiers that o ccur in its b o dy and then to trackthedangerous typ e variables of references All those approaches build conservative approximations of value typ es that may b e ac cessible from the global store by relating the typing of references to syntactic information A more intuitive and integrated approach is to infer a more semantically meaningful infor mation by recording and and keeping track of the typ es of values referenced in the store as so on as the store is expanded and as long as its lo cations are used The quest for sucha typ e system has for long b een the sub ject of manyinvestigations Damas OTo ole Wright Eect inference allows us to approximate the store by regions and typ es and as such can b e used to decide when to p erform typ e generalization The FX system Lucassen Giord Giord al suggests a static seman tics for p olymorphic typ e and eect checking In Jouvelot Giord the authors show that eect reconstruction can b e seen as a constraint satisfaction problem However the exact matching of eects required by the static semantics together with the use of explicit p olymorphism imply the nonexistence of syntactic principal typ es it also somewhat limits the kind of accepted programs We presentinTalpin Jouvelot Sept an algorithm that computes the maximal typ e and the minimal eect of expressions using subsumption on eects to overcome this particular problem of eect matching In the type and eect disciplinewe apply the technique of typ e region and eect inference to the problem of typing references in MLlike languages in the presence of p olymorphic let constructs We determine the principal typ e and the minimal observable eects of expres sions We use eect information to p erform typ e generalization By using eect information together with an observation criterion our typ e system is able to precisely delimit the scop e let expres of sideeecting op erations thus allowing typ e generalization to b e p erformed in sions in a more ecient and uniform way than previous systems It is shown with some simple examples section that our system improves over earlier typ e generalization p olicies for MLlike languages A Core Language and its Semantics Reasoning on the complete denition of a functional language such as Standard ML or FX would have b een complex and tedious In order to simplify the presentation and to ease the formal reasoning this section intro duces a core language It is an attempt to trade between integrating the principal features of functional and imp erative programming and b eing simple This section intro duces its syntax and its dynamic semantics together with a series of conventions and notations that are used in this pap er Syntax The expressions of the language written e p ossibly with a prime or a subscript are the ele ments of the term algebra Exp generated by the grammar describ ed b elow It uses enclosing parentheses in the reminiscence of Scheme Rees al e x j value identier op e j op eration e e j application lambda x e j abstraction let x e e lexical value binding op new j get j set op erations on references Syntax In this grammar x and f range over a countable set of identiers The form e e stands for the application of a function e to an argument e The form op e applies the primitive op eration op to the argument e The expression lambda x e is the socalled lamb da abstraction that denes the rstclass function whose parameter is x and whose result is the value of e Store op erations The arithmetic op erations over integers and the b o olean op erations and and or or even the if construct are typically represented by op erators op b ecause their meaning cannot b e explained easily by abstractions and applications Store op erations can also b e dened by op erators They op erate on reference values which are indirection cells that can b e dynamically allo cated read and written in place The op eration new e initializes a fresh reference to the value of the expression eThe op eration get e gets the value referenced by the p ointer returned by e The op eration set e e mo dies the content