Type Inference with Constrained Types
Total Page:16
File Type:pdf, Size:1020Kb
See discussions, stats, and author profiles for this publication at: https://www.researchgate.net/publication/220346751 Type Inference with Constrained Types Article in Theory and Practice of Object Systems · January 1999 DOI: 10.1002/(SICI)1096-9942(199901/03)5:1<35::AID-TAPO4>3.0.CO;2-4 · Source: DBLP CITATIONS READS 199 40 3 authors, including: Martin Sulzmann Karlsruhe University of Applied Sciences 112 PUBLICATIONS 1,341 CITATIONS SEE PROFILE All in-text references underlined in blue are linked to publications on ResearchGate, Available from: Martin Sulzmann letting you access and read them immediately. Retrieved on: 16 August 2016 Typ e Inference with Constrained Typ es Martin Sulzmann Martin Odersky Yale University University of South Australia Department of Computer Science Scho ol of Computer and Information Science New Haven CT The Levels South Australia sulzmanncsyaleedu oderskycisunisaeduau Martin Wehr University of Edinburgh Lab oratory for Foundations of Computer Science wehrdcsedacuk Research Rep ort YALEUDCSRR April typ es can b e found in Jon We discuss Jones ap Abstract proach in more detail in the section ab out related work In this pap er we present a general framework HMX Remy Rem extends the HindleyMilner typ e system for HindleyMilner style typ e systems with constraints with a sorted equational theory His approach is very HMX stays in the tradition of the HindleyMilner close in spirit to ours But he restricts his attention typ e system Typ e systems in HMX are sound un to equality constraints whereas our approach gives a der a standard untyp ed comp ositional semantics that foundation for more general constraint systems Ex guarantees the slogan welltyp ed programs can not go tensions of HindleyMilner with constraints are also in wrong Furthermore we can give a generic typ e infer creasingly p opular in program analysis DHM TJ ence algorithm for HMX Under sucient conditions Palsb erg Pal gave an ecient inference algorithm for on X typ e inference will always compute the principal a calculus of ob jects The main feature of his system typ e of a term An instance of our framework which is that he do es not use the HindleyMilner approach to deals with p olymorphic records is discussed Also we typ e inference It remains to b e seen how his approach give a subtyping extension SHMX of our HMX sys is related to ours tem In particular the typ e inference algorithm for Even though these typ e systems use dierent con subtyp es computes principal typ es Simplication of straint domains they are largely alike in their typ e the constraints inferred by the typ e inference algorithm theoretic asp ects In this pap er we present a general is discussed for HMX and SHMX framework HMX for HindleyMilner style typ e sys tems with constraints analogous to the CLPX frame work in constraint logic programming JM Partic Intro duction ular typ e systems can b e obtained by instantiating the parameter X to a sp ecic constraint system The Hind We study an extension of the HindleyMilner Mil leyMilner system itself is obtained by instantiating X system with constraints CardelliWegner CW gave to the trivial constraint system an early survey ab out general research directions Rey By and large the treatment of constraints in typ e nolds Rey and Mitchell Mit are foundational pa systems has b een syntactic constraints were regarded p ers that develop basic concepts of constraints and sub as sets of formulas often of a sp ecic form On the typing There are examples for extensions of the Hind other hand constraint programming now generally uses leyMilner system with records Oho Rem over a semantic denition of constraint systems taking a loading Jon Kae VHJW NP CHO OWW constraint system as a cylindric algebra with some ad and subtyping CCH BSvG AW ESTb A ditional prop erties HMT Sar Cylindric algebras general extension of the HindleyMilner with qualied dene a pro jection op erator that binds some sub Supp orted by a Yale University Fellowship set of variables in the constraint In the usual case always yields a b est solution if there is a solution at where constraints are b o olean algebras pro jection cor all For those constraint systems meeting this condi resp onds to existential quantication tion we present a generic typ e inference algorithm that Following the lead of constraint programming we will always yield the principal typ e of a term treat a constraint system as a cylindric algebra with a Our work generalizes Milners results to systems with pro jection op erator Pro jection is very useful for our nontrivial constraints and makes it thus p ossible to purp oses for two reasons First pro jection allows us to exp eriment with new constraint domains without hav formulate a logically pleasing and pragmatically useful ing to invent yet another typ e inference algorithm and rule Intro for quantier intro duction without having to rep eat the often tedious pro ofs of soundness and completeness of typ e inference C D e fv C fv We discuss an instance of our HMX framework Intro that deals with p olymorphic records Namely we show C D e D how to enco de the record calculus of Ohori Oho in terms of our system where C and D are constraints over the typ e variables Furthermore we give an extension SHMX of our in the typ e context and the typ e scheme HMX framework that deals with subtyp es The con Second pro jection is an imp ortant source of op cept of subtyping is of great imp ortance in many record p ortunities for simplifying constraints Jon Pot and ob ject calculi We enrich our constraint system ESTa In our framework simplifying means chang with a subtyp e predicate b etween typ es and de ing the syntactic representation of a constraint without noted by In SHMX we additionally have changing its denotation For example the subtyping the subsumption rule We provide an extension of our constraint generic typ e inference algorithm that computes prin cipal typ es for SHMX typ e systems Previous ap can safely b e simplied to proaches towards typ e inference with subtyp es AW ESTb lack this prop erty In case of subtyping simplifying constraints Jon Pot ESTa b ecomes an imp ortant issue Typ e in since the denotation is the same for b oth constraints ference simply accumulates all constraint problems and Without the pro jection op erator the two constraints ends up in a large constraint set One reason is that would b e dierent since one restricts the variable unicationbased approaches to typ e inference with sub while the other do es not typ es do not seem to apply We discuss simplication Two of the main strengths of the HindleyMilner for HMX and SHMX system are a typ e soundness result and the existence of a The rest of this pap er is structured as follows We typ e inference algorithm that computes principal typ es give a characterization of constraint systems in Sec HMX stays in the tradition of the HindleyMilner tion A general framework HMX for HindleyMil typ e system Typ e systems in HMX are sound under a ner style typ e systems with constraints is presented in standard untyp ed comp ositional semantics provided the Section Section establishes conditions on the con underlying constraint system X is sound This result straint system so that typ e inference is feasible and can b e summarized in the slogan welltyp ed programs a principal typ es theorem holds An instance of our can not go wrong One of the key ideas of our pa framework with p olymorphic records is considered in p er is to present sucient conditions on the constraint Section In Section we discuss a general condition domain X so that the principal typ es prop erty carries under which we get instances of our HMX framework over to HMX The conditions are fairly simple and for free We discuss an extension SHMX of HMX natural For those constraint systems meeting the con with subtyp es in section Simplication is discussed ditions we present a generic typ e inference algorithm in Section The relationship to previous approaches is that will always yield the principal typ e of a term considered in Section Section concludes To this purp ose we represent the typing problem it self as a constraint Generally the constraint system X needs to b e rich system to express all constraint prob Constraint systems lems On the left hand side of the turnstyle and in typ e schemes we only admit a subset of constraints in X that We present a characterization of constraint systems along are in socalled solved form The task of typ e inference the lines of Henkin HMT and Saraswat Sar We is then to split a typing problem into a substitution and restate the denitions of a simple and a cylindric con a residual constraint in solved form This we call con straint system Furthermore we intro duce the new no straint normalization We require that normalization tion of a term constraint system The next lemma states that the pro jection op erator do es not inuence the satisability of a constraint Denition Simple Constraint System A simple e Lemma Let C be a constraint Then C is satisable constraint system is a structure where is a i C is satisable nonempty set of tokens or primitive constraints We also refer to such constraints as predicates The relation We now intro duce a much more expressive constraint e p is a decidable entailment relation where p system We want to deal with typ es and substitutions is the set of nite subsets of We cal l C p a con Denition Typ es A typ e is a member of straint set or simply a constraint e T Term where Term