Type Inference with Bounded Quantification

Dilip Sequeira

Doctor of Philosophy University of Edinburgh

For my Parents

Abstract

In this thesis we study some of the problems which occur when type inference is used in a type system with subtyping. An underlying poset of atomic types is used as a basis for our subtyping systems. We argue that the class of Helly posets is of significant interest, as it includes lattices and trees, and is closed under type formation not only with structural constructors such as function space and list, but also records, tagged variants, Abadi-Cardelli object constructors, ⊤, and ⊥. We develop a general theory relating consistency, solvability, and solution of sets of constraints between regular types built over Helly posets with these constructors, and introduce semantic notions of simplification and entailment for sets of constraints over Helly posets of base types. We extend Helly posets with inequalities of the form a ⩽ τ, where τ is not necessarily atomic, and show how this enables us to deal with bounded quantification.

Using bounded quantification we define a subtyping system which combines structural subtype polymorphism and predicative parametric polymorphism, and use this to extend with subtyping the type system of Laufer and Odersky for ML with type annotations. We define a complete algorithm which infers minimal types for our extension, using factorisations, solutions of subtyping problems analogous to principal unifiers for unification problems. We give some examples of typings computed by a prototype implementation.

Acknowledgements

I am grateful for the help of many people without whom I would not have been able to wrestle this thesis into submission.

Benjamin Pierce first engaged my interest in subtyping. I gained many useful insights from the comments and questions of Adriana Compagnoni, David Turner, and various members of the ML club of the Edinburgh LFCS. Professor Michael Fourman, whose considerable patience was fortunately not inexhaustible, provided useful advice in the construction of the dissertation. The examiners made valuable comments on an earlier version.

My parents were a source of constant understanding and boundless support. And Sophia, who drove me to many things, made all the difference in the world.

Declaration

I declare that this thesis was composed by myself and that the work contained therein is my own, except where explicitly stated otherwise in the text.

(Dilip Sequeira)

Table of Contents

Introduction 3

Chapter 1 Preliminaries 13 1.1 Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.2 Subtyping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.3 Regular Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.4 Type Systems and Inference . . . . . . . . . . . . . . . . . . . . . 19

Chapter 2 Helly Posets 23 2.1 Fences and Distances . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.2 Helly Posets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.3 Distance Inequalities . . . . . . . . . . . . . . . . . . . . . . . . . 32 2.4 Solving Constraint Sets . . . . . . . . . . . . . . . . . . . . . . . . 35

Chapter 3 Simplification and Entailment 44 3.1 Entailment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 3.2 Simplification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 3.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 3.4 Tautness over Smaller Classes . . . . . . . . . . . . . . . . . . . . 56 3.5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

Chapter 4 Solvability and Solution 62 4.1 Decomposition and Simulation . . . . . . . . . . . . . . . . . . . . 62 4.2 Solvability and Solution . . . . . . . . . . . . . . . . . . . . . . . 69 4.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 4.4 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

Chapter 5 Subtyping and Unification 88 5.1 First Class Type Schemes . . . . . . . . . . . . . . . . . . . . . . 89 5.2 Leaves and Essential Occurrences . . . . . . . . . . . . . . . . . . 98 5.3 Quantified Subtype Problems . . . . . . . . . . . . . . . . . . . . 101 5.4 Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 5.5 Soundness and Completeness . . . . . . . . . . . . . . . . . . . . . 104 5.6 Termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 5.7 Transitions and Proofs . . . . . . . . . . . . . . . . . . . . . . . . 109 5.8 An Algorithm for Subtyping . . . . . . . . . . . . . . . . . . . . . 111 5.9 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

Chapter 6 Subtyping and Annotation 114 6.1 Extensions to the Type System . . . . . . . . . . . . . . . . . . . 116 6.2 The Type System ML_{∀⩽} . . . . . . . . . . . . . . . . . . . . . . . 118 6.3 Feasibility and Factorisation . . . . . . . . . . . . . . . . . . . . . 119 6.4 Type Inference . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 6.5 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 6.6 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134

Conclusion 136

Bibliography 139

Introduction

Subtyping

The study of Subtyping in the λ-calculus first arose in the work of Reynolds [45] and of Coppo and Dezani [10]. One type is a subtype of another if any element of the first may be used in any context where an element of the second is required; this is the principle of subsumption. We will write τ_{1} ⩽ τ_{2} to mean that τ_{1} is a subtype of τ_{2}. Operationally this may denote either inclusion between types, or that an implicit coercion is to be inserted to transform an element of τ_{1} to one of τ_{2}. Principal motivations for subtyping as a language construct are that it is

Natural to express semantic inclusion: for example in simple record type systems we expect a record with more fields to be usable wherever one with fewer fields is. And ℕ ⊆ ℝ, so we might intuitively expect Nat ⩽ Real, even if at the machine level coercions may be required to implement this.

Powerful enough to encode the constructs of object-oriented languages within functional frameworks — see for example [37, 1], or [16, 14] for encodings in first-order systems of the kind we consider in this thesis.

Type Inference

Realistic programming languages with complex type systems benefit greatly from type inference, the process of reconstructing type information omitted by the programmer. It is not without its drawbacks: the requirement that type inference be decidable places limits on the power of the type system, and interpreting the errors that arise during type inference generally requires more understanding of the type system by the programmer. But usually type inference speeds and simplifies coding tasks, and results in more elegant code.

Although many notions of inference exist, our interest will be in versions of Hindley-Milner type inference [13] for λ-calculus extended with letpolymorphism, which is the basis for type inference in ML [27] and similar functional languages. Type inference for λ-calculus in the presence of subtyping was first considered by Mitchell [28]. It is significantly more complicated than without subtypes. Consider the following simple ML function:

fun max x y = if x < y then y else x

where < is a comparison of type Real→Real→Bool. Under the standard Core-ML type system, this program has type Real→Real→Real. However, suppose we have a subtype relation where Real has subtypes. Then if we assign the type variable α to x and β to y, type inference tells us that the term is typable for any α and β such that α and β are each subtypes of Real (from the <) and have a common supertype (so that the results of the two arms of the conditional can be combined.) The constraint graph is thus:

                            Real  γ α  β

It is not unreasonable in general to want distinct values for α, β, and γ, all of which are different from Real. In the ML type system, all the inequalities are viewed as equalities and all points of this graph are identified. But in the presence of subtyping, identifying any pair may lead to a loss of solutions: suppose the underlying order is the (rather unrealistic) poset:

                          Real   GaussInt EvenInt   OddInt

(Such a poset is called a 2-crown.) We can use max to produce a GaussInt from an EvenInt and an OddInt, but it is easy to check that if we identify any two points in the constraint set for max, we will no longer be able to use the function in this way. Thus in general a principal type scheme will be a quantified type subject to constraints. We will write the principal type here as

∀{α, β, γ}\{α⩽Real, β⩽Real, α⩽γ, β⩽γ} α→β→γ

For such a simple expression this type is somewhat unwieldy, and it is shown in [22] that in general for any sound definition of an instance relation on constrained types there is a term of size k whose principal type scheme requires at least k constraints. More practically, the usual way of inferring types in the presence of subtyping yields a constraint for every application subterm, so that in typed λ-calculus the inferred constraint set grows at least linearly with the term; letpolymorphism can result in a much faster blowup. Thus we can expect a raw type inference algorithm to yield principal types with constraint sets which are too large to be understood by humans (e.g. quicksort produces approximately 300 constraints) and for inference to be practically scalable. Faced with constrained types, several issues arise immediately:

Satisfiability Is there a solution to the constraints? If there isn’t, then we should generate a type error. In this particular instance it is easy to check by hand; in larger programs all but impossible.

Entailment Suppose we know what type we intended for this expression. Is it equivalent to, or at least less general than, the inferred type? This is the obvious question for signature matching, but it can also help during development: we can annotate program terms for documentary reasons, or, if a type error is encountered, to force the typechecker to check that it is producing the results intended at intermediate points. Entailment is a difficult problem, and not well-understood for complex type systems.

Simplification Is there a more succint representation for the constrained type? Generating small representations is important both for the typechecking algorithm, for reasons of scalability, and for the programmer, for comprehensibility. The requirements of the type inference engine and the programmer in regard to simplification may be different: if the programmer is forced to look at types, it may be worth considerable effort to produce an optimally simplified solution. For purposes of efficient inference, however, a quickly generated but less than optimal simplification system may be more practical. In general, simplification strategies depend on the notion of entailment chosen, since this determines the classes of equivalent typings.

Solution Given a solvable constraint set, can we find a solution? Suppose we have a top-level function for which we infer type ∀\overline{α}\C. Int→Int. None of the variables in C can have any effect outside the function, so the type scheme is unnecessarily polymorphic, and the actual instantiation of the variables \overline{α} is of no interest, save that the constraints in C are satisfied. Picking a particular solution enables us to compile the function monomorphically, which may represent a significant optimisation.

Types And Orderings

The question of what forms of subtyping can be feasibly implemented must be addressed in the light of how well we can deal with the above issues. In order to describe the relevant problems, we shall use some informal definitions here which will be made precise in chapter 1.

Subtype orders are typically constructed by asserting some ordering properties of the base types (Nat, Int, etc), and defining properties of the type constructors in the language (products, function types, lists, records etc) which propagate the subtyping relation on the base types. For example, we expect Int→Nat ⩽ Nat→Int, since the function space constructor is contravariant in its first argument, and covariant in its second. Constructors such as →, ×, and List are called structural: in an ordering built with such constructors from atomic types, any two elements which are in the subtype relation must have the same “shape”, that is, lists of integers are only subtypes of lists of types of which integers are a subtype, etc. The record constructor is an example of a constructor that does not behave this way. Assume that we have simple records, in which our only operations are record formation and field extraction. Since all we can do to a record is extract a field, in any context where we need a record with a certain set of fields, a record with additional fields is also usable. This is known as “widthwise” subtyping: for example we have {a:Int, b:Nat} ⩽ {b:Int}. This obviously doesn’t preserve the same-shape property, and consequently such constructors are termed non-structural.

Whilst purely structural subtyping is easier to handle, in practice records and variants are important language features, as are the object constructors of [2], which have similar width-wise subtyping behaviour. One of the major issues of this thesis will be the question of how to combine existing techniques for structural subtyping to deal with non-structural constructors.

The question of whether the subtyping order may be allowed to vary is also an important one. It seems natural that if the language permits definition of new types, then it should be possible to create a place for them in the subtype ordering. This could either be by inclusion (a natural circumstance for subrange types), or by declaring an implicit coercion (for a continued fraction datatype to be coercible to a Real, say). We might also wish to define new types as subtypes of constructor types. For example, if we wished to implement complex numbers as (r, θ) pairs with −π ⩽ θ < π, we might expect Complex < Real × Real. More generally, an abstract type represented by a concrete type subject to some invariance condition may be seen as a subtype of the concrete representation.

Another reason for introducing such types is to allow bounded quantification [7], a language construct which allows universal quantifiers to quantify over only the subtypes of a given type, for example the ∀δ⩽Real. δ→δ→δ typing for max. It is less expressive than full-blown constrained quantification, in that while all terms can be assigned principal constrained type schemes, not all terms have principal bounded type schemes. However, the question of when a constrained type scheme entails a bounded type scheme turns out to be quite straightforward to determine (whereas testing entailment between two constrained type schemes is not a well-understood problem).

In order to justify our approach to these issues, we will outline some of the results to date in the areas of satisfiability, entailment, and simplification.

Satisfiability

It is shown by Hoang and Mitchell in [22] that for any language which is based on the λ-calculus and incorporates subtyping, typability is PTIME-equivalent to satisfiability of constraint sets over the subtype ordering. Thus it is important to choose orderings for which satisfiability is tractable.

In [50], Tiuryn distinguishes between two forms of the satisfiability problem. The more general, UNIFORM-SSI, takes a constraint set and a poset as input, and checks satisfiability. The problem Q-SSI specialises UNIFORM-SSI to a particular poset. Tiuryn shows that there are posets (e.g. crowns, of which the 2-crown is the simplest) for which Q-SSI is PSPACE-hard, and thus so is UNIFORM-SSI (indeed it is PSPACE-complete [17]), but that for structural subtyping over unions of lattices it is in PTIME. This result was extended in [5] to Helly posets (a class which we will discuss further), and in [41] to a class of posets called transitively feasible. While it is not difficult to see how crowns might arise in order hierarchies (for example, those generated by multiple inheritance), in practice it is not feasible to incorporate them into subtyping systems.

Satisfiability is usually checked in polynomial time using some notion of consistency on the constraint set, rather than by constructing an explicit solution. Usually dynamic transitive closure of constraints is used: we close the constraint set under transitivity, and propagation of constraints of the form τ_{1}→τ_{2} ⩽ τ′_{1}→τ′_{2} to τ′_{1} ⩽ τ_{1} and τ_{2} ⩽ τ′_{2}. If this closure process terminates without generating inequalities between mismatching constructors, or constructors and atomic types, the constraint set is consistent and thus solvable. This technique requires the underlying poset to be a union of lattices. If subtyping is structural, then we can ensure this by choosing lattices for the posets of base types, since structural constructors always build lattices from lattices. But this is quite a strong restriction, since the natural orderings on types (for example numeric types) that we might wish to incorporate into a subtype hierarchy sometimes lack a natural choice of ⊤ type, and even more often a natural choice of ⊥.

The method fails for trees even with structural subtyping: {Nat→Nat ⩽ α, Long→Long ⩽ α} is unsolvable over the tree order Long < Int, Nat < Int, because any solution for α would have to be of the form a_{1}→a_{2} with the property that a_{1}⩽Long and a_{1}⩽Int. Yet closure of the constraint set generates no inconsistencies.

Further, the set of records over a lattice (or even a discrete poset) does not form a lattice: for example, the types {l:a} and {l:{l:a}} possess an upper bound {} but no lower bound. So constraint consistency is again inadequate: the constraint set {{l:a} ⩾ α, {l:b} ⩾ α} is unsolvable even through constraint closure generates no inconsistencies. However, a modification of this method can be used with object types where field subtyping is invariant [32, 20]: in this case we need also to propagate common-lower-bound conditions to equalities between subterms: the corresponding constraint set {[l:a] ⩾ α, [l:b] ⩾ α} using objects rather than records, yields the constraint a = b, demonstrating unsatisfiability.

In cases where records are required, the technique of [16] is to add global ⊤ and ⊥ elements to the model of subtyping, making the ordering into one big lattice. Unfortunately, this can create undesirable side-effects: for example, consider the term

let f = fun(r) r.X + if r.X then 1 else 2;

The “one big lattice” technique accepts this term as well-typed: in fact it has the minimal type {X:⊥}→Nat, since the X field of r is used at types Bool and Int. Unfortunately the domain of this type is uninhabited, so the function cannot be applied, which is unlikely to be what the programmer expected. If we solve constraint sets rather than just checking consistency, the presence of ⊤ or ⊥ in an inferred type could be enough to alert the programmer to the fact that something is wrong, but the constraint may be propagated a long way through the program before it is detected. Or consider the term true = 3. If we have polymorphic equality, this term is typable, and in a PER model where all elements are equal at type ⊤, will always evaluate to true. This expression could quietly form part of a well-typed larger program, whereas a type error probably reflects the programmer’s intuition more accurately.

These difficulties might be a fair price to pay in circumstances where ⊤ and ⊥ are themselves useful, examples cited in [48] are for heterogeneous lists, or persistent data: weaker type systems that admit more judgements can also be seen as more flexible type systems. On the other hand, it is quite reasonable to want to add records, say, to a language, without the extra baggage resulting from ⊤ and ⊥ So we are faced with the question: if the model is not then a lattice, what is it? In fact, the model containing just those types that are syntactically expressible turns out to have the structure of a Helly poset.

Once consistency has been checked, it may be possible (as in e.g. [16]) to demonstrate soundness of typing directly from the closure condition, but the usual method ([50, 5] for finite types; [34, 35] for regular infinite types) is to show that the consistency condition can be exploited to produce a solution.

Entailment

Semantically, one type scheme entails another if all solutions to the first are solutions to the second (e.g. [40, 52, 3]). For complex type systems no complete algorithm for testing entailment is known. In the context of subtyping over a lattice the entailment relation between a constraint set and a single constraint has been studied by Henglein and Rehof [21]. Our approach to entailment will be similar to theirs, but over a Helly poset rather than a lattice.

Simplification

Simplification techniques operate on the constraint set by using a sequence of transformations, each of which reduces its size. In general, the validity of a simplification transformation depends on the notion of equivalence between constrained types. For example, we have seen that in the inferred type of max, if we require that the set of solutions over a 2-crown be the same in any equivalent type, then no simplification is possible. On the other hand, if our underlying poset of types is a lattice, then in the case of max, α and β have a least upper bound δ, and in any solution this upper bound must be less than γ and Real. So any inputs of type α and β can be coerced to type δ, compared by coercing δ to Real, and an output of type δ can then be coerced to type γ. This process of “exporting” coercions leads us to an equivalent representation of the type of max as ∀δ⩽Real. δ→δ→δ.

There are two main identifiable streams of work in the literature. The simplest operates on constraints in atomic form. Introduced by Fuh and Mishra [18], its complexity and completeness properties have been more recently studied by Rehof [43]. The approach is essentially syntactic: a simplification being justified if it results in a provably equivalent constraint set.

The other operates on systems of constraints over regular types, often involving non-structural subtyping. The notion of equivalence here is semantic rather than syntactic, based on equality of solution sets. More simplifications are possible than in the syntactic approach, since information about the underlying poset (such as lattice structure) can be exploited. Early examples, such as [12, 46] were collections of transformations applied essentially heuristically. The most successful approach by Pottier [40] uses a powerful entailment algorithm as a foundation for the transformations, so that as well as transformations which known to be valid, the algorithm can speculatively transform the constraint set then test for equivalence, resulting in a very powerful simplification system. The incompleteness and high computational cost of the entailment algorithm are properties the simplification algorithm thus inherits. Simplification of constraint sets over regular types in a lattice of regular types over a discrete order extended with ⊤ and ⊥ has been studied in [3], where a procedure is given which minimises the number of variables in a constraint set.

We will use our entailment relation over Helly posets to derive a simplification operation which allows us to exploit the Helly structure. For example, while in a Helly poset it is not generally the case that any pair of types possess a least upper bound, it is the case for any pair of types which have at least some upper bound. This suffices to demonstrate the existence of the type δ in the max example, and so justifies the same simplification as if the underlying subtype order were a lattice.

Type Inference and Annotation

The type system in which the subtype ordering is embedded is also an important consideration. Although for the most part we will consider the type system of ML extended with subtyping, called ML_{⩽}, we shall also consider the system of Laufer and Odersky, which we shall call ML_{∀}. In core ML, all function arguments must have monomorphic types. Whilst this restriction is not impossible to lift [53], to do so without extending the term syntax results in the loss of principal typing. The most attractive approach, from [31], is to use annotations, whereby an abstraction is assumed to take a monomorphically typed argument unless the variable bound by the abstraction is annotated with a type scheme.

This Work

Helly Posets

We will concentrate on Helly posets, which we shall formally define in chapter 2. Helly posets possess attractive algebraic structure, and the relevance of this to efficient satisfiability was first demonstrated by Benke in [5]. The class of Helly posets is interesting for several reasons:

• It includes many classes of interesting posets, such as trees and lattices. Another example of a Helly poset is the Haskell class hierarchy [36].

• It is closed under orderings generated by many interesting type constructors.

• Satisfiability over Helly posets built with non-structural subtyping constructors is a smooth generalisation of unification and of transitive constraint closure.

• Helly posets can be extended with new elements in a natural fashion.

We will study entailment over Helly posets of atomic types, and indicate how it may be extended to the structural subtyping case, and we will introduce a simplification technique with a useful completeness property. We will also extend Benke’s result to non-structural and non-finite cases, which will enable us to work with models of subtyping that do not have the problems which accompany ⊤ and ⊥.

Non-atomic Constants

We will consider the impact of choosing a (not necessarily atomic) type τ in a Helly poset and adjoining a new type constant a with the inequality a ⩽ τ. This will enable us to introduce bounded quantification, and to determine whether a constrained type scheme entails a bounded type scheme with relatively low computational cost, by framing the entailment problem as a satisfiability problem. We will proceed to combine subtyping and annotations in a type system for which we will demonstrate an inference algorithm with a form of principal type.

Outline of the Thesis

The first part of the thesis will focus on the study of subtyping over Helly posets, the second on subtyping and annotation.

Preliminaries

Chapter 1 contains necessary background to the work.

Helly Posets

In chapter 2 we define Helly posets, and develop the theory which we shall apply in chapters 3 and 4.

Simplification and Entailment

In chapter 3 we define a simple entailment relation for atomic constraint sets over Helly posets, and show how it forms the foundation for a simplification transformation.

Satisfiability and Solution

In chapter 4 we show how constraint sets can be tested for satisfiability, and solved, over partial orders consisting of regular types built from Helly posets.

Subtyping and Annotations

In chapter 5 we investigate the relationship between bounded quantification and mixed-prefix unification. We define quantified subtyping problems, and show how such problems may be solved. In chapter 6 we show that partially solved problems have a factorisation property that is analogous to the principal solution property of first-order unification. We use this to incorporate subtyping in the extension of ML by annotations [31], and show that the resulting system has principal types (although not that it has principal typings).

Chapter 1

Preliminaries

In this chapter we will define some terms and formally state some result from the literature which we shall call on in later chapters.

1.1 Types

We will begin with a few definitions. The syntax of our types will be as follows:

τ ::= α | a | T τ_{1} … τ_{n}

where T is a type constructor such as →, List or a record type constructor, α ranges over type variables, and a ranges over base types. A ground type is a type containing no type variables. Each ground type is a base type if it is of the form a, and a constructor type otherwise. Base types can also be considered as constructors of arity zero. Our types will be built over some poset Q of atoms, whose elements cannot be coerced to elements of constructor types. Atoms are base types, but later we will allow assertions of the form a ⩽ τ_{1}→τ_{2} (for example), so that not all base types will necessarily be atoms. We will write τ(A) for the set of types built over some set A of base types.

1.2 Subtyping

We shall define the ordering over a poset Q of base types by a set of rules. Since it is an ordering, we naturally have

\overline{τ ⩽ τ}  (Taut)
\frac{τ_{1} ⩽ τ_{2} τ_{2} ⩽ τ_{3}}{τ_{1} ⩽ τ_{3}}  (Trans)

Additionally we need • a finite description of the subtyping behaviour of the base types

\frac{a⩽b ∈ Q}{a ⩽ b}  (QAx)

• a set of rules describing how subtyping between two constructor types is determined by the subtyping relationship between the constructor arguments, for example:

\frac{τ′_{1} ⩽ τ_{1} τ_{2} ⩽ τ′_{2}}{τ_{1}→τ_{2} ⩽ τ′_{1}→τ′_{2}}  (Arrow)

It will sometimes be convenient to consider the axioms for Q as constructor rules for zero-arity constructors.

Type Constructors

There are two classes of subtype orderings: structural and non-structural. Suppose we have an underlying poset Q of atomic types. There is a natural equivalence relation on Q, such that two elements are equivalent if they are in the same connected component (we shall use the term “component” to mean “connected component”). We will choose a representative for each equivalence class, and write ⌜a⌝ for the representative of the class containing a. Then we define the shape of a type as follows:

\begin{aligned}S(a) & = & ⌜a⌝ \\ S(T τ_{1} … τ_{n}) & = & T S(τ_{1}) … S(τ_{n}) \end{aligned}

A subtype ordering is structural if the components of its graph are exactly the sets of types of identical shape. A constructor will be called structural if extending a subtyping system with its constructor rule always preserves the structural subtyping property. For example, → is a structural constructor, while a global ⊤ type (such that τ ⩽ ⊤ for every τ) is not, because it merges all the components of the ordering into one.

Since non-structural constructors have rather distinctive behaviours, we will deal with each on an individual basis. Structural constructors, however, will be dealt with uniformly. We use T to range over such constructors. We will use the generic term labels to indicate the set of indices used to order constructor arguments, so that → has labels 1 and 2. We use the function L() to refer to the label set of a constructor, so that e.g. L(List) = {1}, L(→) = {1, 2}, L(a) = ∅, and we extend this to types, so that L(τ) is the set of labels of the outermost constructor of τ.

Structural Constructors

Structural constructors include ×, +, List, and →. Each T has a fixed arity n, labels {1, … n}, and an associated n-tuple of polarities, each of which is pos, neg, or mix, describing the subtyping behaviour of the constructor with respect to each argument. pos will mean that the constructor type increases as that argument does, neg that it decreases, and mix that changing that argument yields an incomparable type. For example the polarities of → would be (neg, pos), that of ref, (mix). We will write p_{T}^{i} for the polarity of the constructor in its ith argument, and ⋄_{i}^{T} for whichever one of {⩽, ⩾, =} is required by the polarity on τ_{i} ⩽ τ′_{i} for Tτ_{1} … τ_{1} ⩽ Tτ′_{1} … τ′_{n} to hold. Then for each structural constructor T we will have a rule:

\frac{τ_{i} ⋄_{i}^{T} τ′_{i} for 1 ⩽ i ⩽ n}{Tτ_{1} … τ_{n} ⩽ Tτ′_{1} … τ′_{n}}  (Cons-T)

Given a type τ with outermost constructor T of arity n, we use the term τ(i) to denote the i^{th} constructor argument of T.

Records and Variants

Record subtyping is fundamental to encodings of objects in type systems such as that of [16]. The subtyping behaviour of the Abadi-Cardelli objects ([2]) is similar, and tagged variant types are dual to records in their subtyping behaviour and can be dealt with using the same techniques. The subtyping rule for records is:

\frac{τ_{i} ⩽ τ′_{i} for all i ∈ I  I ⊆ J}{\{l_{j}:τ_{j}\}_{j∈J} ⩽ \{l_{i}:τ′_{i}\}_{i∈I}}  (Record)

The record constructor can be considered of variable arity. For each finite set of labels I, we define a constructor {}_{I} of arity |I|, which constructs a record with labels exactly the elements of I. Then p_{l}^{{}_{I}} = pos for any l ∈ I, and L({}_{I}) = I. If τ is a record type, we use the term τ(l) to refer to the type of the l field.

⊤ and ⊥

⊤ and ⊥ are the other non-structural constructors usually encountered. Their subtyping rules are simply:

\overline{τ ⩽ ⊤}  (Top)

\overline{⊥ ⩽ τ}  (Bot)

Obviously L(⊤) = L(⊥) = ∅.

Nonatomic Base Types

We can extend the ordering over the underlying poset Q by a bounded signature. A bounded signature is a list of subtype assertions either of the form a where a does not occur previously in the signature or in Q, or a⩽τ, where a is a name not previously occurring in the signature or in Q, and τ is a type constructed using elements of Q and atoms previously bound in the signature.

Σ ::= •  The empty signature
Σ, a
Σ, a ⩽ τ where τ ∈ τ(Q ∪ atom(Σ))

If a is bound in Σ, we will call it a Σ-atom (by contrast to a Q-atom), and for the set of such Σ-atoms we will write atom(Σ). It is straightforward that any extension of Q by a forest may be topologically sorted into a representation by a bounded signature. We have the rule Var^{↑}:

\frac{Σ^{↑}(a) = τ}{Q, Σ ⊢ a ⩽ τ}  (Var^{↑})

where we write Σ^{↑}(a) for the upper bound of the Σ-atom a. We will write Σ(a)↓ to mean that a is a Σ-atom with an upper bound, and write Σ^{⇑}(a) for the minimal bound of Σ which is not a base type, if such exists.

It is easy to show that with just constructors and Q-atoms, transitivity is redundant: all occurrences of Trans can be eliminated from any proof. However this is not the case once the rule Var^{↑} has been introduced. A standard technique [11, 38, 8] is to replace this rule with

\frac{Σ^{↑}(a) ⩽ τ}{a ⩽ τ}  (Prom^{↑})

whence transitivity is again redundant.

Constraint Sets

A constraint set will be a set of assertions of the form τ_{1} ⩽ τ_{2} where τ_{1} and τ_{2} are types possibly containing variables. If C is a constraint set, a solution to C will be a ground substitution θ on the variables such that for every τ_{1}⩽τ_{2} ∈ C, θτ_{1} ⩽ θτ_{2}.

The set O(τ) of types occurring in τ is defined as follows:

\begin{aligned}O(α) & = & α \\ O(a) & = & a \\ O(cτ_{1} … τ_{n}) & = & \{cτ_{1} … τ_{n}\} ∪ O(τ_{1}) ∪ … ∪ O(τ_{2}) \end{aligned}

If C is a constraint set, the set O(C) of types occurring in C is

⋃_{τ_{1}⩽τ_{2}∈C} O(τ_{1}) ∪ O(τ_{2})

1.3 Regular Types

Object systems which use subtyping typically require infinite types, and they are also a natural way to express types such as lists and trees. There are a number of ways in which to represent regular types, such as transition graphs ([25]), recursive types ([4, 6]), and trees ([4]), each of which has its advantages. We shall choose rooted transition graphs as our basic representation. A graph corresponding to a regular type is a finite directed graph with labelled vertices and edges, rooted at some particular vertex. The vertices are labelled with type constructors (via a labelling function ν), and a vertex labelled with the constructor c has exactly one out-edge for each l ∈ L(c), and no others. We shall use (G, r) to range over such graphs, where r is the root vertex. For any vertex n in a transition graph with out-edge l, we shall write n(l) for the vertex reached by an l-transition fron n. We shall use ν(τ) to mean the outermost constructor of τ, which is just ν(r), where (G, r) is a graph of τ.

Any finite rooted graph can be unfolded from the root vertex into a regular tree (that is, a tree with finitely many distinct subtrees). We say two graphs (G_{1}, r_{1}) and (G_{2}, r_{2}) are isomorphic and write (G_{1}, r_{1}) ≅ (G_{2}, r_{2}) if they unfold to the same tree: that is, if every path in either graph exists in the other, and the path traverses the same sequence of constructors in each graph. For any given tree, there is an infinite isomorphism class of rooted graphs which unfold to it.

Shallow Approximation

It is convenient to extend the ordering on atoms (viewed as zero-arity constructors) to all constructors. To this end, we define the relation ⊑ as follows:

a ⊑ b if a⩽b ∈ Q
→  ⊑ →
{}_{l∈L} ⊑ {}_{m∈M} if M ⊆ L
c  ⊑ ⊤  for any constructor c
⊥  ⊑ c  for any constructor c
a  ⊑ c  if Σ^{↑}(a) ⊑ c, for non-atomic a

⊑ on types is a “shallow” approximation of ⩽: ν(τ_{1}) ⊑ ν(τ_{2}) is a necessary condition for τ_{1} ⩽ τ_{2}, although clearly it is not sufficient.

1.3.1 Remark: Note that for all the constructors we consider, if c_{1} ⊑ c_{2}, then for any l ∈ L(c_{1}) ∩ L(c_{2}), p_{l}^{c_{1}} = p_{l}^{c_{2}}.

Simulations

1.3.2 Definition: A simulation between types built with the → constructor over some poset of base types is a relation ≲ on types such that if (G_{1}, r_{1}) ≲ (G_{2}, r_{2})

• ν(r_{1}) ⊑ ν(r_{2})

• if ν(r_{1}) = ν(r_{2}) = →, then (G_{2}, r_{2}(1)) ≲ (G_{1}, r_{1}(1)) and (G_{1}, r_{1}(2)) ≲ (G_{2}, r_{2}(2))

It is easy to show that simulations are closed under unions, and it is well known (see e.g. [25]) that the ⩽ relation is the largest simulation. Thus in order to demonstrate that τ_{1} ⩽ τ_{2}, it suffices to find a simulation between them.

In fact, this result extends easily to other structural constructors, to records, ⊤, and ⊥. We shall use a more general definition encompassing these constructors, although it can in fact be widened further, to deal with constructors for which remark(1.3.1) does not hold, e.g. updatable records [44]. (G_{1}, r_{1}) ≲ (G_{2}, r_{2}) is a simulation if

• ν(r_{1}) ⊑ ν(r_{2})

• if l ∈ L(ν(r_{1})) ∩ L(ν(r_{2})),

– if p_{l}^{ν(r_{1})} = pos, then (G_{1}, r_{1}(l)) ≲ (G_{2}, r_{2}(l))

– if p_{l}^{ν(r_{1})} = neg, then (G_{2}, r_{2}(l)) ≲ (G_{1}, r_{1}(l))

– if p_{l}^{ν(r_{1})} = mix, then (G_{1}, r_{1}(l)) ≲ (G_{2}, r_{2}(l)) and (G_{2}, r_{2}(l)) ≲ (G_{1}, r_{1}(l))

Notice that (G_{1}, r_{1}) ≅ (G_{2}, r_{2}) iff both (G_{1}, r_{1}) ⊑ (G_{2}, r_{2}) and (G_{2}, r_{2}) ⊑ (G_{1}, r_{1}). In fact, it is also straightforward to extend this approach to non-atomic base types also. For any a occurring in Σ such that Σ(a)↓ let (G_{a}, r_{a}) be the graph of Σ^{↑}(a). If τ ≠ a, then a ⩽ τ iff Σ^{⇑}(a) ⩽ τ, so it is sufficient to add to the above definition the condition

• if ν(r_{1}) = a such that Σ(a)↓, and ν(r_{2}) ≠ a, then (G_{a}, r_{a}) ≲ (G_{2}, r_{2}).

1.4 Type Systems and Inference

In this section we will outline some of the relatives of the ML typing system which we will discuss in later chapters. Terms are

e ::= x | λx.e | e_{1}e_{2} | let x = e_{1} in e_{2}

Types τ are as before, and type schemes κ have the form ∀\overline{α}\C.τ, where C is a constraint set and \overline{α} is a set of type variables.

ML_{⩽}

In order to define ML_{⩽}, we alter our subtyping rules to have premises and conclusions of the form C ⊢ τ_{1} ⩽ τ_{2}. To the rules:

\overline{C ⊢ τ ⩽ τ}  (Taut)
\frac{C ⊢ τ_{1} ⩽ τ_{2} C ⊢ τ_{2} ⩽ τ_{3}}{C ⊢ τ_{1} ⩽ τ_{3}}  (Trans)
\frac{a ⩽ b ∈ Q}{C ⊢ a ⩽ b}  (QAx)
\frac{C ⊢ b_{1} ⩽ a_{1} a_{2} ⩽ b_{2}}{C ⊢ a_{1}→a_{2} ⩽ b_{1}→b_{2}}  (Arrow)

we add the rule

\frac{τ_{1} ⩽ τ_{2} ∈ C}{C ⊢ τ_{1} ⩽ τ_{2}}  (Hyp)

and write C_{1} ⊢ C_{2} to mean that C_{1} ⊢ τ_{1} ⩽ τ_{2} for every τ_{1} ⩽ τ_{2} ∈ C_{2}. Note that transitivity can no longer be eliminated. We define the type system as follows:

\frac{Γ(x) = κ}{Γ; C ⊢ x : κ}  (Var)
\frac{Γ, x:τ_{1}; C ⊢ e : τ_{2}}{Γ; C ⊢ λx.e : τ_{1}→τ_{2}}  (Abs)
\frac{Γ; C ⊢ e_{1} : τ_{1}→τ_{2} Γ; C ⊢ e_{2} : τ_{1}}{Γ; C ⊢ e_{1}e_{2} : τ_{2}}  (App)
\frac{Γ; C ⊢ e_{1} : κ_{1} Γ, x:κ_{1}; C ⊢ e_{2} : κ_{2}}{Γ; C ⊢ let x = e_{1} in e_{2} : κ_{2}}  (Let)

\frac{Γ; C ⊢ e : ∀\overline{α}\C′.τ  C ⊢ [\overline{τ}/\overline{α}]C′}{Γ; C ⊢ e : [\overline{τ}/\overline{α}]τ}  (Inst)
\frac{Γ; C ∪ C_{1} ⊢ e : τ  \overline{α} ∩ (fv(Γ) ∪ fv(C)) = ∅}{Γ; C ⊢ ∀\overline{α}\C_{1}.τ}  (Gen)
\frac{Γ; C ⊢ e : τ_{1} C ⊢ τ_{1} ⩽ τ_{2}}{Γ; C ⊢ e : τ_{2}}  (Sub)

Type Inference

Type Inference in type systems with subtyping has two phases: extracting of a principal type and checking solvability of the resulting constraint set. As usual, we incorporate Inst and Gen into the other rules; Sub will be subsumed into the property of minimal typing that the algorithm possesses.

\frac{Γ(x) = ∀\overline{α}\C.τ  \overline{β} fresh}{Γ; [\overline{β}/\overline{α}]C ⊢_{A} x : [\overline{β}/\overline{α}]τ}  (A:Var)
\frac{Γ, x:τ_{1}; C ⊢_{A} e : τ_{2}}{Γ; C ⊢_{A} λx.e : τ_{1}→τ_{2}}  (A:Abs)
\frac{Γ; C_{1} ⊢_{A} e_{1} : τ_{1}→τ_{2} Γ; C_{2} ⊢_{A} e_{2} : τ′_{1}}{Γ; C_{1} ∪ C_{2} ∪ \{τ′_{1} ⩽ τ_{1}\} ⊢_{A} e_{1}e_{2} : τ_{2}}  (A:App)
\frac{Γ; C_{1} ⊢_{A} e_{1} : τ  Γ, x:close(Γ, τ, C_{1}); C_{2} ⊢ e_{2} : τ_{2}}{Γ; C_{1} ∪ C_{2} ⊢_{A} let x = e_{1} in e_{2} : τ_{2}}  (A:Let)

where close(Γ, τ, C) is ∀\overline{α}\C.τ where \overline{α} is the set of variables occurring free in C and τ but not in Γ. It is easy to see that by inserting appropriate Inst and Gen rules we can rewrite an algorithmic derivation in the standard proof system in a canonical fashion. The constraint set obtained by this algorithm can then be tested for solvability as described earlier. These type inference rules have a principal typing property, for which we shall have to define an instance relation. The relation we use is Henglein’s halbstark relation [19].

1.4.1 Definition: D_{1} ≡ Γ_{1}; C_{1} ⊢ e : τ_{1} has as an instance D_{2} ≡ Γ_{2}; C_{2} ⊢ e : τ_{2} (we write D_{1} ≪ D_{2}) if there is a substitution θ such that

1. C_{2} ⊢ θ(C) ∪ {θτ_{1} ⩽ τ_{2}}

2. Γ_{2} = θ(Γ_{1})

We extend this notion to type schemes as follows: Let \overline{α}_{1} not be free in C_{1} and \overline{α}_{2} not free in C_{2}. Then Γ_{1}; C_{1} ⊢ e : ∀\overline{α}_{1}\D_{1}. τ_{1} has as an instance Γ_{2}; C_{2} ⊢ e : ∀\overline{α}_{2}\D_{2}. τ_{2} if

Γ; C_{1} ∪ D_{1} ⊢ e : τ_{1} ≪ Γ; C_{2} ∪ D_{2} ⊢ e : τ_{2}

Then we have the following (again from [19]):

1.4.2 Proposition: If Γ; C ⊢ e : κ, then Γ; C_{A} ⊢_{A} e : τ_{A}, and

Γ; C_{A} ⊢_{A} e : τ_{A} ≪ Γ; C ⊢ e : κ_{1}

Thus every instance of a typing judgement is a halbstark instance of the type returned by the inference algorithm, and thus the inference algorithm provides a minimal type. In fact, we can also obtain all possible typing judgements for the term from the inferred typing using the rules Inst, Gen, Sub, and weakening on constraint sets, so the inferred typing is also principal.

ML_{∀}

Another idea for extending ML from [31] is designed to allow passing of polymorphic functions as function arguments. This is done by means of annotations: λ-bound variables are assumed to have monomorphic type unless they are annotated with type schemes. Polymorphic type schemes are still impredicative: they may only be instantiated with monomorphic types. A subtype ordering is defined as follows:

\overline{σ ⩽ σ}  (refl)
\frac{σ_{1} ⩽ σ_{2} σ_{2} ⩽ σ_{3}}{σ_{1} ⩽ σ_{3}}  (Trans)
\frac{σ′_{1} ⩽ σ_{1} σ_{2} ⩽ σ′_{2}}{σ_{1}→σ_{2} ⩽ σ′_{1}→σ′_{2}}  (Arrow)
\frac{[τ/α]σ_{1} ⩽ σ_{2}}{∀α.σ_{1} ⩽ σ_{2}}  (∀-left)
\frac{σ_{1} ⩽ σ_{2} α does not occur free in σ_{1}}{σ_{1} ⩽ ∀α.σ_{2}}  (∀-right)

Again there is a type inference algorithm with a minimal (and equivalently in this case principal) type property: if Γ ⊢_{A} e : σ then for any σ′ such that Γ ⊢ e : σ′, we have σ ⩽ σ′. In the inference algorithm, when a function accepting a parameter of type σ is applied to an argument with type σ′, it is necessary to derive a principal substitution for σ′ as an instance of σ; this is computed using mixed prefix unification [26]

Helly Posets

The study of partial orders underlies any theory of subtyping. Since our interest will be mainly in subtype orderings which possess the Helly property, in this chapter we will present some concepts and results which we shall apply to problems in subtyping in the rest of the thesis. The notion of distance in section(2.1) is from [42], the results on partial orders in section(2.2) are adapted from [30], and the results in section(2.3) are adapted from [5].

2.1 Fences and Distances

A up-fence of length n between two elements a_{1} and a_{2} of a poset is a sequence a_{1} = b_{0} ⩽ b_{1} ⩾ b_{2} … b_{n} = a_{2}. A down-fence is defined dually.

       [figure: a_{1}; a_{2}; a_{1}; a_{2}]

An up-fence of length 3  A down-fence of length 2

By convention, the fences from a_{1} to itself have length 1. The distance from one element to another is the pair ⟨u, d⟩ consisting of the lengths of the minimal upand down-fences.

Notice that if we have an up-fence from a_{1} = b_{0} ⩽ b_{1} ⩾ … b_{n} = a_{2}, we have a down-fence a_{1} = a_{1} ⩾ b_{0} ⩽ b_{1} ⩾ … b_{n} = a_{2} of length n + 1, thus in any distance ⟨u, d⟩, u and d differ by at most 1. Thus we can represent e.g. ⟨3, 4⟩ graphically by just an up-fence of length 3, and e.g. ⟨3, 2⟩ by just a down-fence of length 2; to represent ⟨3, 3⟩ however, we need both fences.

         a_{1}  a_{2}  ^{a_{1}  a_{2}}

d(a_{1}, a_{2}) = ⟨3, 3⟩  d(a_{1}, a_{2}) = ⟨3, 2⟩

We will use e to range over distances, and write d_{Q}(a, b) for the distance from a to b in the poset Q; when Q is evident from context, we shall write simply d(a, b). The diameter δ_{Q} of a poset Q is the least e such that d_{Q}(a, b) ⩽ e for all a, b in the same component of Q.

Operations on distances

We will make extensive use of operations on distances. Here we outline the properties of these operations, and motivate the definitions of the less obvious ones.

Max and Min

With pointwise ordering, the distances form a lattice:

⟨1, 1⟩ ⟨1, 2⟩ ⟨2, 3⟩ ⟨3, 4⟩  ⟨2, 2⟩ ⟨3, 3⟩⟨2, 1⟩ ⟨3, 2⟩ ⟨4, 3⟩ ⟨4, 4⟩
L = {⟨m, n⟩ ∈ ℤ^{+} × ℤ^{+} | |m − n| ⩽ 1} ∪ {∞}

We will use ∨ and ∧ for maximum and minimum respectively.

Reversal

If d(a_{1}, a_{2}) = ⟨u, d⟩, then the reverse ⟨u, d⟩^{−1} is d(a_{2}, a_{1}). Reversal flips fences horizontally, so the effect on L and on a particular distance can be visualised thus:

[figure: ⟨1, 1⟩; ⟨1, 2⟩; ⟨2, 3⟩; ⟨3, 4⟩; ⟨2, 2⟩; ⟨3, 3⟩; ⟨2, 1⟩; ⟨3, 2⟩; ⟨4, 3⟩; ⟨4, 4⟩; e ↦ e^{−1}; a_{1}; a_{2}; ⇒; a_{2}; a_{1}; ⟨3, 4⟩^{−1} = ⟨4, 3⟩]

Reversal is monotone, and

\begin{aligned}(e^{−1})^{−1} & = & e \\ (e_{1} ∧ e_{2})^{−1} & = & (e_{1}^{−1} ∧ e_{2}^{−1}) \end{aligned}

Conjugation

The conjugate of ⟨u, d⟩ is ⟨u, d⟩^{∗} = ⟨d, u⟩. Suppose we are dealing with structural subtyping over some poset of atomic base types. If we have a distance inequality d(τ_{1}→τ_{2}, τ′_{1}→τ′_{2}) ⩽ ⟨3, 4⟩, then we know there are α_{1}→α_{2} and β_{1}→β_{2} such that τ_{1}→τ_{2} ⩽ α_{1}→α_{2} ⩾ β_{1}→β_{2} ⩽ τ′_{1}→τ′_{2}. By contravariance therefore, we have τ_{1} ⩾ α_{1} ⩽ β_{1} ⩾ τ′_{1}. In other words, contravariance causes distances to be flipped vertically: up-fences are mapped to down-fences and vice versa. Conjugation represents this, and we can visualise its effect thus:

[figure: ⟨1, 1⟩; ⟨1, 2⟩; ⟨2, 3⟩; ⟨3, 4⟩; ⟨2, 2⟩; ⟨3, 3⟩; ⟨2, 1⟩; ⟨3, 2⟩; ⟨4, 3⟩; ⟨4, 4⟩; e ↦ e^{∗}; τ_{1}; τ′_{1}; ⇒; τ_{1}→τ_{2}; τ′_{1}→τ′_{2}; ⟨4, 3⟩^{∗} = ⟨3, 4⟩]

Conjugation is monotone, and

\begin{aligned}(e^{∗})^{∗} & = & e \\ (e^{−1})^{∗} & = & (e^{∗})^{−1} \\ (e_{1} ∧ e_{2})^{∗} & = & e_{1}^{∗} ∧ e_{2}^{∗} \end{aligned}

Observe that if e_{1} ⩽̸ e_{2}, although we do not have e_{2} ⩽ e_{1}, we do have e_{2} ⩽ e_{1}^{∗}. Addition We define e_{1} + e_{2} as the minimum distance given by the concatenation of the fences forming e_{1} and e_{2}.

[figure: ⟨2, 3⟩ + ⟨2, 3⟩ = ⟨4, 5⟩; ⟨2, 2⟩ + ⟨2, 3⟩ = ⟨4, 3⟩]

Numerically, ⟨u_{1}, d_{1}⟩ + ⟨u_{2}, d_{2}⟩ =

⟨u_{1} + d_{2} − 1, d_{1} + u_{2} − 1⟩ if u_{1} is even and d_{1} is even
⟨u_{1} + u_{2} − 1, d_{1} + u_{2} − 1⟩ if u_{1} is odd and d_{1} is even
⟨u_{1} + d_{2} − 1, d_{1} + d_{2} − 1⟩ if u_{1} is even and d_{1} is odd
⟨u_{1} + u_{2} − 1, d_{1} + d_{2} − 1⟩ if u_{1} is odd and d_{1} is odd

Addition is associative and monotone, and

\begin{aligned}(e_{1} + e_{2})^{∗} & = & e_{1}^{∗} + e_{2}^{∗} \\ (e_{1} + e_{2})^{−1} & = & e_{2}^{−1} + e_{1}^{−1} \\ (e_{1} ∧ e_{2}) + (e_{3} ∧ e_{4}) & = & (e_{1} + e_{3}) ∧ (e_{2} + e_{3}) ∧ (e_{1} + e_{4}) ∧ (e_{2} + e_{4}) \end{aligned}

Raising and Lowering

Suppose we know that the minimal up-fence from a to b is shorter than the minimal down-fence, for example a ≠ b and there is no element c < a. If we know also that d(a, b) ⩽ ⟨u, d⟩, we may deduce that d(a, b) ⩽ ⟨max(d − 1, 1), d⟩. Thus we define

raising ⟨u, d⟩^{△} = ⟨max(d − 1, 1), d⟩
lowering ⟨u, d⟩^{▽} = ⟨u, max(u − 1, 1)⟩

Raising and lowering are evidently monotone, and we additionally have:

\begin{aligned}(e_{1} + e_{2})^{△} & = & e_{1}^{△} + e_{2} \\ (e_{1} + e_{2})^{▽} & = & e_{1}^{▽} + e_{2} \end{aligned}

Dually, we define

\begin{aligned}e^{▲} & = & ((e^{−1})^{△})^{−1} \\ e^{▼} & = & ((e^{−1})^{▽})^{−1} \end{aligned}

so that if a is as above, and we know d(b, a) ⩽ e, then we can deduce d(b, a) ⩽ d(b, a)^{▲}. We have

\begin{aligned}(e_{1} + e_{2})^{▲} & = & e_{1} + e_{2}^{▲} \\ (e_{1} + e_{2})^{▼} & = & e_{1} + e_{2}^{▼} \end{aligned}

2.2 Helly Posets

A disc [a, e] in a poset Q is the set of elements {b ∈ Q | d(a, b) ⩽ e}. A set of discs D = {[a_{i}, e_{i}]}_{i∈I} constitute a hole if D has empty intersection, and no proper subset of D has empty intersection. If P ⊇ Q is a poset, the hole D is separated in P if the set of discs D is also a hole in P — that is, there is no element of P − Q contained in all the discs {[a_{i}, e_{i}]}_{i∈I}

If P is a poset such that Q ⊆ P, a retraction from P to Q is an order-preserving map f : P → Q such that f(q) = q for every q ∈ Q. If a retraction exists, we write P ⊳ Q.

2.2.1 Proposition: If Q ⊆ P and P ⊳ Q, every hole in Q must be separated in P.

Proof: If f : P → Q is a retraction, it is straightforward by induction on the length of the fences that d_{Q}(fa, fb) ⩽ d_{P} (a, b). So suppose D is a hole in Q, and there is some element of p ∈ P such that p ∈ ⋂ D. Then if f : P →Q were a retraction, for each disc [a, e] ∈ D, p ∈ [fa, e], a contradiction to D being a hole in Q. □

Posets Q for which the separation of holes in P ⊇ Q is sufficient as well as necessary for retractability are called absolute retracts.

2.2.2 Definition [Helly Posets]: A Helly poset Q is one in which the only holes are of size 2, i.e. for any set of discs D_{i} in Q, if

D_{i} ∩ D_{j} ≠ ∅ for all i and j

then

⋂_{i} D_{i} ≠ ∅

We have (from [30])

2.2.3 Proposition: Any Helly poset is an absolute retract.

That all holes have size two has the following consequence:

2.2.4 Lemma: Suppose d_{Q}(a_{1}, a_{2}) ⩽ e. Then for every e_{1}, e_{2} such that e_{1} +e_{2} ⩾ e, there is some b such that d(a_{1}, b) ⩽ e_{1} and d_{Q}(b, a_{2}) ⩽ e_{2}.

Proof: If e_{1} ⩾ e or e_{2} ⩾ e, the result is trivial, so we assume otherwise and proceed by cases on the form of e.

• Suppose e = ⟨n, n+1⟩.Then there is an up-fence F in Q of length n from a_{1} to a_{2}. Let b be the most distant point from a_{1} on F such that d_{F} (a_{1}, b) ⩽ e_{1}. We must have d_{F} (a_{1}, b) = e_{1}^{△}, since this is the distance along the maximal up-fence shorter than e. Now e_{1} + e_{2} ⩾ e, so (e_{1} + e_{2})^{△} ⩾ e^{△}, and since e^{△} = e, (e_{1} + e_{2})^{△} ⩾ e, and so e_{1}^{△} + e_{2} ⩾ e. Thus the distance e_{2} is long enough to extend from b at least as far as a_{2}. Thus we must have d_{Q}(b, a_{2}) ⩽ e_{2}. And since d_{F} (a_{1}, b) ⩽ e_{1}, d_{Q}(a_{1}, b) ⩽ e_{1}.

• If e = ⟨n + 1, n⟩, a similar argument applies.

• Now suppose e = ⟨n, n⟩. If e_{1} + e_{2} > e, then either e_{1} + e_{2} ⩾ ⟨n, n + 1⟩ or e_{1} + e_{2} ⩾ ⟨n + 1, n⟩, and we can use one of the methods above to find b. Otherwise we must have e_{1} = ⟨m_{1}, m_{1}⟩, e_{2} = ⟨m_{2}, m_{2}⟩, since only such a pair can sum to a distance of ⟨n, n⟩. Now by the argument above, the discs [a_{1}, ⟨m_{1}, m_{1} + 1⟩] and [a_{2}, ⟨m_{2}, m_{2}⟩] must intersect, as must the discs [a_{1}, ⟨m_{1} + 1, m_{1}⟩] and [a_{2}, ⟨m_{2}, m_{2}⟩]. Since the discs [a_{1}, ⟨m_{1}, m_{1} + 1⟩] and [a_{2}, ⟨m_{1} + 1, m_{1}⟩] intersect trivially, all three discs must have a common intersection by the Helly property, and any point in this intersection suffices as a choice for b. □

An immediate consequence of the above lemma is that two discs [a_{1}, e_{1}] and [a_{2}, e_{2}] intersect precisely if d(a_{1}, a_{2}) ⩽ e_{1} + e_{2}^{−1}, i.e. if the distance between their centres is less than or equal to the sum of their radii

2.2.5 Proposition: If Q is a Helly poset and Q ⊆ P, then P ⊳ Q iff for every a_{1}, a_{2} ∈ Q, d_{Q}(a_{1}, a_{2}) ⩽ d_{P} (a_{1}, a_{2}).

Proof: Suppose P ⊳ Q, then every fence in P retracts to a fence in Q, so d_{Q}(a_{1}, a_{2}) ⩽ d_{P} (a_{1}, a_{2}). Conversely, suppose P ⋫Q. Then there is some hole {[a_{1}, e_{1}], [a_{2}, e_{2}]} in Q which is not separated in P. So there is some element b ∈ P such that d_{P} (a_{1}, b) ⩽ e_{1} and d_{P} (a_{2}, b) ⩽ e_{2}, so we have

d_{P} (a_{1}, a_{2}) ⩽ e_{1} + e_{2}^{−1}

but we cannot have

d_{Q}(a_{1}, a_{2}) ⩽ e_{1} + e_{2}^{−1}

since if we did, by lemma(2.2.4) there would be an element c such that d_{Q}(a_{1}, c) ⩽ e_{1} and d_{Q}(c, a_{2}) ⩽ e_{2}, which would be a contradiction to the pair of discs forming a hole in Q □

Thus if Q is Helly, and P ⊇ Q, P ⊳ Q if all the holes of Q of size 2 are separated in P. If such a P is also Helly, we will say P is a Q-model.

2.2.6 Proposition: Suppose Q is Helly. P is a Q-model iff for every pair a_{1}, a_{2} ∈ Q, d_{Q}(a_{1}, a_{2}) = d_{P} (a_{1}, a_{2}).

Proof: Suppose P is a Q-model. Then every fence in Q also exists in P, so d_{P} (a_{1}, a_{2}) ⩽ d_{Q}(a_{1}, a_{2}). And since P ⊳ Q, d_{Q}(a_{1}, a_{2}) ⩽ d_{P} (a_{1}, a_{2}). Conversely, if d_{Q}(a_{1}, a_{2}) = d_{P} (a_{1}, a_{2}), P ⊳ Q by the Helly property for Q. □

Helly posets may also be characterised in terms of irreducible elements. An upper cover for an element a in a poset is an element b such that a < b, and there is no element c such that a < c < b (lower cover being defined dually). An element is doubly irreducible if it has at most one upper cover and one lower cover. A decomposition by doubly irreducibles of a finite poset Q is a sequence q_{1}, q_{2} … containing all the elements of Q such that if Q_{1} = Q, Q_{i+1} = Q_{i}\q_{i}, then each q_{i} is doubly irreducible in Q_{i}. Again from [30] we have:

2.2.7 Proposition: If Q is finite, Q is a Helly poset iff it is decomposable by doubly irreducibles.

An easy method of demonstrating that a finite poset is Helly is to adduce such a decomposition, which is immediate in the case of fences and trees, for example. It was stated in the introduction that Helly posets have the following property:

2.2.8 Proposition: In a Helly poset, any set of elements with an upper bound possess a least upper bound, and any set of elements with a lower bound possess a greatest lower bound.

Proof: We will do the first, the second is dual. Suppose {a_{i}} have a common upper bound: then the intersection of the discs {[a_{i}, ⟨1, 2⟩]} is non-empty: suppose it is the set {b_{j}}. Then for each j, [b_{j}, ⟨2, 1⟩] intersects each disc [a_{i}, ⟨1, 2⟩] (since each disc contains both points), and for j_{1} and j_{2}, the discs [b_{j_{1}}, ⟨2, 1⟩] and [b_{j_{2}}, ⟨2, 1⟩] intersect at each a_{i}. Thus each pair in the set {[a_{i}, ⟨1, 2⟩]}∪{[b_{j}, ⟨2, 1⟩]} intersects, and thus the whole set of discs have non-empty intersection. If b is contained in all of the discs, it must be an upper bound of {a_{i}} and since it is less than each b_{i}, it must be the least such. □

Examples

Although not the largest class for which constraint solution can be tested in polynomial time, the class of Helly posets is attractive because its algebraic character enables us to decompose conditions involving a set of types and type variables into a set of easily checked conditions on the set of pairs of types. It has some simple closure properties: it is closed under products, retractions (so in fact it is a variety) and also disjoint unions and inversion. As remarked in the introduction, it is also closed under record type formation, we will show the case for finite types.

2.2.9 Lemma: Let Q^{∗} be the poset of types formed over a Helly poset Q using structural and record type constructors. Any set of types τ_{i} in Q^{∗} with pairwise upper bounds have a least upper bound, and any set of types τ_{i} with pairwise lower bounds possess a greater lower bound.

Proof: We will proceed by induction on the maximal depth of constructor nesting of any type in the set {τ_{i}}. If the level of constructor nesting is 0, all the τ_{i} are base types, and the result is just proposition(2.2.8). Otherwise if one of the τ_{i} is a constructor type, say →, all must be →-types, and it is straightforward that

⋁_{i} τ_{i}→τ′_{i} = ( ⋀_{i} τ_{i}) → ( ⋁_{i} τ′_{i})

and dually for greatest lower bound. Other constructors are similar.

Similarly if one of the τ_{i} is a record type, all must be. Suppose not all the τ_{i} have an l-field, then no upper bound can have an l-field. And if all the τ_{i} have an l-field, there are two possible cases: either the set {τ_{i}(l)} has an upper bound, in which case by the induction hypothesis it has a least upper bound τ_{l}, so for any upper bound τ such that τ(l) is defined, we must have τ(l) ⩾ τ_{i}(l) for each l, and so τ(l) ⩾ τ_{l}. Or the set has no upper bound, in which case no upper bound for the {τ_{i}} can have an l-field. Thus the least upper bound of the {τ_{i}} is

⋁_{i} τ_{i} = {l : ⋁_{i} τ_{i}(l)}_{l∈J}

where J is the set of labels l occurring in every τ_{i} such that ⋁_{i} τ_{i}(l) is defined. And suppose {τ_{i}} has a lower bound τ, then τ must have all the fields occurring in any τ_{i}, and τ(l) ⩽ τ_{i}(l) for any i such that l ∈ L(τ_{i}). So τ(l) is a lower bound for the set {τ_{i}(l) | l ∈ L(τ_{i})}, and so by the induction hypothesis, {τ_{i}(l) | l ∈ L(τ_{i})} has greatest lower bound. So the greatest lower bound of the {τ_{i}} is

⋀_{i} τ_{i} = {l : ⋀_{l∈L(τ_{i})} τ_{i}(l)}_{l∈J}

where J = ⋃_{i} L(τ_{i}). □

2.2.10 Proposition: The poset of types formed over a Helly poset using structural and record type constructors is a Helly poset

Proof: We will demonstrate that any set of discs D = [τ_{i}, e_{i}]_{i∈I} in the poset which have pairwise intersection contain a common point. Again we will proceed by induction on the maximal depth of constructor nesting in any type on which a disc is centred.

From the subtyping rules, the τ_{i} must have the same outermost constructor. If this is anything other than a record constructor, the result follows easily from the induction hypothesis. So suppose the τ_{i} are all records. We may assume that no disc has radius ⟨1, 1⟩ for then the result is trivial. We define

ρ = ⋀{τ | [τ, ⟨2, 1⟩] ∈ D}

which exists, because the discs {[τ, ⟨1, 2⟩]} all intersect pairwise, so the types τ all have pairwise lower bounds, and thus by lemma(2.2.9) they have a common lower bound. It suffices to show that each disc in D contains ρ.

Since any set of records has the empty record {} as an upper bound, any disc [τ, ⟨u, d⟩] with u > 1 and d > 2 is the entire set of record types, and thus must contain ρ. Notice also that any disc [τ, ⟨3, 2⟩] contains precisely the same elements as the disc [τ, ⟨2, 2⟩], since there is an up-fence of length 2 from any record to any other (via the empty record). Thus we need consider only the discs of radius ⟨3, 2⟩ and ⟨1, 2⟩.

Suppose [τ, ⟨1, 2⟩] ∈ D. This disc intersects each [τ′, ⟨2, 1⟩] ∈ D, so

d(τ, τ′) ⩽ ⟨1, 2⟩ + ⟨1, 2⟩ = ⟨1, 2⟩

Thus τ is a lower bound for all such τ′, and thus τ ⩽ ρ, and so ρ ∈ [τ, ⟨1, 2⟩]. And each [τ, ⟨3, 2⟩] ∈ D intersects each [τ′, ⟨2, 1⟩] ∈ D so we have

d(τ, τ′) ⩽ ⟨3, 2⟩ + ⟨1, 2⟩ = ⟨3, 2⟩

Thus τ has a pairwise lower bound with each τ′, and since each pair of τ′ also have a pairwise lower bound, by lemma(2.2.9) the set {τ′} and the element τ have some greatest lower bound τ′′. But we have τ′′ ⩽ ρ, since ρ is the greatest lower bound of just the τ′. Thus

d(τ, ρ) ⩽ d(τ, τ′′) + d(τ′′, ρ)
⩽ ⟨2, 1⟩ + ⟨1, 2⟩
⩽ ⟨3, 2⟩

Thus ρ ∈ [τ, ⟨3, 2⟩]. □

A similar result is that if a subtyping system with the Helly property is augmented with either or both of the constructors ⊤ and ⊥, the Helly property is preserved.

2.3 Distance Inequalities

A distance inequality is an assertion of the form d(τ_{1}, τ_{2}) ⩽ e. Distance inequalities generalise simple inequalities: any constraint τ_{1} ⩽ τ_{2} can be expressed by the distance inequality d(τ_{1}, τ_{2}) ⩽ ⟨1, 2⟩, and equally any distance inequality d(τ_{1}, τ_{2}) ⩽ e can be expressed as a set of constraints with fresh variables forming the intermediate points in the up- and down-fences.

Given a set of distance inequalities I, we can deduce other inequalities using reversal and addition, and the fact that L has a lattice structure. It fact, we shall be interested in the minimal distances which can be deduced from a set of distance inequalities. It is easy to see that the following rules are sound:

\overline{d(τ, τ) ⩽ ⟨1, 1⟩}  (Refl)
\frac{d(τ_{1}, τ_{2}) ⩽ e ∈ I}{I ⊢ d(τ_{1}, τ_{2}) ⩽ e}  (Ax)
\frac{I ⊢ d(τ_{1}, τ_{2}) ⩽ e}{I ⊢ d(τ_{2}, τ_{1}) ⩽ e^{−1}}  (Dual)
\frac{I ⊢ d(τ_{1}, τ_{2}) ⩽ e_{1} I ⊢ d(τ_{2}, τ_{3}) ⩽ e_{2}}{I ⊢ d(τ_{1}, τ_{3}) ⩽ e_{1} + e_{2}}  (Join)
\frac{I ⊢ d(τ_{1}, τ_{2}) ⩽ e_{1} I ⊢ d(τ_{1}, τ_{2}) ⩽ e_{2}}{I ⊢ d(τ_{1}, τ_{2}) ⩽ e_{1} ∧ e_{2}}  (Min)

Since we are only interested in minimal distance between distinct elements of Q, use of Refl is unnecessary. If I ⊢ d(τ_{1}, τ_{2}) ⩽ e for some distance e, we will write d_{I}(τ_{1}, τ_{2}) for the least such distance; in the presence of Min this is well-defined.

2.3.1 Definition [Distance Consistency]: We say that a set of distance inequalities I is distance consistent over Q if for any two elements a and b of Q, d_{Q}(a, b) ⩽ d_{I}(a, b).

A set of distance inequalities I is satisfiable if there is a ground substitution such that d_{Q}(θτ_{1}, θτ_{2})⩽e for every d(τ_{1}, τ_{2})⩽e in I. θ is then a satisfying substitution, or, analogously with constraint sets, a solution.

2.3.2 Proposition: Let I be a set of inequalities over a Helly poset Q containing inequalities only between variables and elements of Q. Then if I is distance consistent, I is satisfiable.

Proof: Let C be a set of constraints (possibly containing fresh variables) which make up the fences for the distances in I, and ≡_{C} be defined by

τ_{1} ≡_{C} τ_{2} if d_{I}(τ_{1}, τ_{2}) = ⟨1, 1⟩

We can define a poset Q′ by quotienting C by this equivalence relation, so that distances in Q′ are the same as those in C (and those in I). Then d_{Q}(a, b) ⩽ d_{Q′}(a, b), so Q′ ⊳ Q. □

2.3.3 Definition: A chain in a set of distance inequalities I from τ to τ′ of length e is a set of distinct elements {τ_{1}, … τ_{n}} such that τ = τ_{1}, τ′ = τ_{n}, and for each i, either d(τ_{i}, τ_{i+1})⩽e_{i} ∈ I or d(τ_{i+1}, τ_{i})⩽e^{−1} ∈ I. and ∑_{1}^{n−1} e_{i} = e. The length of the chain is e, the size of the chain is n. If Q is a Helly poset, and there is a chain of length e in I between a and b such that d_{Q}(a, b) ⩽̸ e, we say that the chain witnesses the inconsistency of I over Q.

2.3.4 Proposition: d_{I}(τ_{1}, τ_{2}) is the minimum of the lengths of all chains in I from τ_{1} to τ_{2}.

Proof: d_{I}(τ_{1}, τ_{2}) is built from the distances in I using minimisation, addition and reversal. Using the identities

\begin{aligned}(e_{1} + e_{2})^{−1} & = & e_{1}^{−1} + e_{2}^{−1} \\ (e_{1} ∧ e_{2})^{−1} & = & e_{1}^{−1} ∧ e_{2}^{−1} \\ e_{1} + (e_{2} ∧ e_{2}) & = & (e_{1} + e_{2}) ∧ (e_{1} + e_{3}) \\ (e_{1} + e_{2}) ∧ e_{3} & = & (e_{1} + e_{3}) ∧ (e_{2} + e_{3}) \end{aligned}

we can permute uses of minimisation outwards over addition and reversal, and addition outwards over reversal, so that the minimal distance can always be expressed as the minimum of the lengths of a set of chains. □

2.3.5 Proposition: Suppose I is not distance consistent over Q. Then there is some chain a = τ_{1}, … τ_{n} = b witnessing the inconsistency of I such that τ_{2}, … τ_{n−1} are all variables.

Proof: Let {e_{i}} be the lengths of all the chains from a to b in I. If d_{Q}(a, b) ⩽ e_{i} for all i, then d_{Q}(a, b) ⩽ ⋀ e_{i}, so d_{Q}(a, b) ⩽ d_{I}(a, b). Thus if there is no chain witnessing the inconsistency of I over Q, I must be consistent over Q.

So suppose I is not distance consistent over Q, so that there must be some witness chain. Let X be a witness chain of minimal size, and suppose X starts at a, ends at b, and is of length e. If X contains no base types other than a and b, the proposition holds.

Otherwise X contains some base type c. Let X_{1} be the chain from a to c, and X_{2} from c to b, with lengths e_{1} and e_{2} respectively. If we have d_{Q}(a, c) ⩽ e_{1} and d(c, b) ⩽ e_{2}, then d_{Q}(a, b) ⩽ e_{1} + e_{2} = e, which cannot be the case since X witnesses the inconsistency of I. Thus either X_{1} or X_{2} must also be a witness to the inconsistency of I, contradicting the minimality of X. □

Thus if we have a set I of distance inequalities between variables and elements of Q, it suffices for distance consistency that the I contains no chain having variables other than at its endpoints which witnesses its inconsistency.

2.4 Solving Constraint Sets

Over a lattice, solving sets of distance inequalities is quite straightforward.

2.4.1 Definition: Let I be a set of distance inequalities over a lattice Q. We define

U(α) = ⋀ {a ∈ Q | d_{I}(α, a) ⩽ ⟨1, 2⟩}

and

D(α) = ⋁ {a ∈ Q | d_{I}(α, a) ⩽ ⟨2, 1⟩}

2.4.2 Lemma: Let Q be a lattice, and I be a set of distance inequalities between variables and elements of Q which contains Q. If I is solvable, then θ(α) = U(α) is a solution of I.

Proof: Suppose not, so there are e, τ_{1} and τ_{2} such that d_{I}(τ_{1}, τ_{2}) ⩽ e but d_{Q}(θτ_{1}, θτ_{2}) ⩽̸ e. If e = ⟨1, 1⟩, the result is immediate. If e = ⟨1, 2⟩, then for any a ⩾ τ_{2} we must have a ⩾ τ_{1}, and therefore U(τ_{2}) ⩾ U(τ_{1}). The ⟨2, 1⟩ case is similar. And since Q is a lattice, any two elements are separated by a distance of at most ⟨2, 2⟩. □

Dually, θ(α) = D(α) is also a solution. Thus we have functions which extract a solution at each variable using only the minimal distances between that variable and the base types in Q. We may obtain a similar result for trees:

2.4.3 Lemma: Let Q be a tree, and I a set of distance inequalities between variables and elements of Q which contains Q. If I is solvable over Q, then θ(α) = U(α) is a solution of I.

Proof: Suppose not, so that there are τ_{1} and τ_{2} such that d_{I}(τ_{1}, τ_{2}) ⩽ e but d_{Q}(θτ_{1}, θτ_{2}) ⩽̸ e. We will proceed by cases on e.

⟨1, 1⟩ U(τ_{1}) = U(τ_{2}) by deductive closure, so we must have θ(τ_{2}) = θ(τ_{1}).

⟨1, 2⟩ (i.e. τ_{1} ⩽ τ_{2}) Suppose d_{I}(θτ_{1}, θτ_{2}) > ⟨1, 2⟩. There are two possibilities: either θτ_{1} > θτ_{2}, in which case there is a such that θτ_{1} > a ⩾ θτ_{2}, but then we must have I ⊢ τ_{2} < a and I ⊬ τ_{1} ⩽ a, which cannot be the case since I ⊢ τ_{1} ⩽ τ_{2}. The other possibility is that θτ_{1} and θτ_{2} are incomparable, but then again there is some a such that τ_{2} ⩽ a and τ_{1} ⩽̸ a, which again contradicts the assumption that I ⊢ τ_{1} ⩽ τ_{2}.

⟨2, 1⟩ (i.e. τ_{2} ⩽ τ_{1}) is identical to the above case

⟨2, 2⟩ If d_{I}(θτ_{1}, θτ_{2}) ⩽̸ ⟨2, 2⟩, we must have d_{I}(θτ_{1}, θτ_{2}) = ⟨2, 3⟩, since this is the maximum distance between any two elements of Q. Since θτ_{1} and θτ_{2} are not comparable, there must be a ⩾ τ_{1} and b ⩾ τ_{2} such that a and b have no lower bound. But then τ_{1} and τ_{2} cannot possess a lower bound, contradicting the assumption that d_{I}(τ_{1}, τ_{2}) ⩽ ⟨2, 2⟩. □

2.4.4 Definition [Helly Solution Functions]: A Q-state S is a function Q ⇀ L. A Q-state is Q-consistent if it is either everywhere undefined (this state we shall call S_{⊥}) or it is defined on exactly one component of Q, and for all a and b in this component, d_{Q}(a, b) ⩽ S(a)^{−1} + S(b). If S and T are Q-states, We say d_{Q}(S, T) ⩽ e iff dom(S) = dom(T), and for every a ∈ dom(S), S(a) ⩽ e + T (a) and T (a) ⩽ e^{−1} + S(a)

A function F_{Q} from Q-consistent states to Q is a Helly Solution Function (HSF) for Q if

• d_{Q}(F_{Q}(S), a) ⩽ S(a) for any a such that S(a)↓

• for any S and T such that d_{Q}(S, T) ⩽ e, d_{Q}(F_{Q}(S), F_{Q}(T)) ⩽ e

It is easy to see that the solutions for lattices and trees are examples of Helly solution functions: for example in the case of lattices we define S_{α}(a) = d_{I}(α, a), and

F_{Q}(S_{α}) = U(α) = ⋀{a | S_{α}(a) ⩽ ⟨1, 2⟩}

2.4.5 Proposition: Suppose I is solvable over Q and contains Q, and F_{Q} is a HSF for Q. If we define S_{α}(a) = d_{I}(α, a), then θ(α) = F_{Q}(S_{α}) is a solution of I.

Proof: Since I contains Q and is distance consistent over Q, each S_{α} is either S_{⊥} or defined on exactly one component of Q: moreover since I is distance consistent over Q, it cannot be the case for any a and b that d_{Q}(a, b) ⩽̸ d_{I}(a, α) + d_{I}(α, b), so d_{Q}(a, b) ⩽ S_{α}(a)^{−1} + S_{α}(b). Thus S_{α} is a Q-consistent Q-state. Suppose there is some inequality d(α, τ) ⩽ e in I. There are two possible cases:

• if τ is a base type a, then S(a) ⩽ e and so it is immediate from the definition of HSF that d(θα, a) ⩽ e.

• if τ is a variable β, by Join we have S_{α}(a) ⩽ e+S_{β}(a) for any a ∈ dom(S_{α}), and by Dual and Join, S_{β}(a) ⩽ e^{−1} + S_{α}(a). So d(S_{α}, S_{β}) ⩽ e, and again from the definition of HSF, d(θ(α), θ(β)) ⩽ e □

In fact, we can show that all Helly posets have HSFs. In order to do so, we need a few preliminary results.

2.4.6 Lemma: Suppose U is an up-fence of length m, and D a down-fence of length m. We define

T = {(u, d) ∈ U × D | |u − d| ⩽ 1}

(u_{1}, d_{1})  (u_{2}, d_{1}) (u_{1}, d_{2}) (u_{2}, d_{2})  (u_{2}, d_{3}) (u_{3}, d_{2}) (u_{3}, d_{3})  (u_{4}, d_{3}) (u_{3}, d_{4}) (u_{4}, d_{4})

Then U × D ⊳ T.

Proof: It is straightforward to check that the map

(u_{i}, d_{j}) ↦ (u_{i}, d_{i−1}) if j ⩽ i − 1
(u_{i}, d_{j}) ↦ (u_{i}, d_{i+1}) if j ⩾ i + 1
(u_{i}, d_{j}) ↦ (u_{i}, d_{j}) otherwise

is a retraction. □

2.4.7 Lemma: If we define f : L → U × D by

f(⟨p, q⟩) = (u_{p}, d_{q})

(f(⟨1, 1⟩) f(⟨1, 2⟩) f(⟨2, 1⟩ f(⟨2, 2⟩)  f(⟨3, 2⟩) f(⟨2, 3⟩) f(⟨3, 3⟩)  f(⟨3, 4⟩) f(⟨4, 3⟩) f(⟨4, 4⟩)

Suppose e_{1}, e_{2}, e_{3} ⩽ ⟨m, m⟩, then (a) if e_{1} = ⟨n, n⟩ for some n ⩽ m, then d(f(e_{1}), f(e_{1} + e_{2})) = e_{2}

(b) if e_{1} ⩽ e_{2} ⩽ e_{3}, then d_{U×D}(f(e_{1}), f(e_{2})) ⩽ d_{U×D}(f(e_{1}), f(e_{3}))

(c) d_{U×D}(f(e_{1}), f(e_{1} + e_{2})) ⩽ e_{2}

(d) If e_{1} + e_{2} ⩾ e_{3} and e_{3} + e_{2}^{−1} ⩾ e_{1}, then d_{U×D}(f(e_{1}), f(e_{3})) ⩽ e_{2}

Proof: By lemma(2.4.6) U × D is a T -model, so d_{U×D}(a, b) = d_{T} (a, b) by proposition(2.2.6). Thus it suffices to demonstrate to result for distances in T.

(a) Let e_{1} = ⟨n, n⟩, e_{2} = ⟨u_{2}, d_{2}⟩. We proceed by induction on max(u_{2}, d_{2}). If max(u_{2}, d_{2}) ⩽ 2, the results for the cases n odd and n even can easily be observed from the diagram. Otherwise suppose the result holds for all smaller values of max(u_{2}, d_{2}) Since max(u_{2}, d_{2}) > 2, every fence from f(e_{1}) to f(e_{1} + e_{2}) must pass through f(⟨n + 1, n + 1⟩), and d_{T} (e_{1}, e_{1} + e_{2}) is then

d_{T} (f(e_{1}), f(⟨n + 1, n + 1⟩)) + d_{T} (f(⟨n + 1, n + 1⟩), f(e_{1} + e_{2}))

= ⟨2, 2⟩ + d_{T} (f(e_{1} + ⟨2, 2⟩), f(e_{1} + ⟨2, 2⟩ + ⟨d_{2} − 1, u_{2} − 1⟩))

since ⟨u, d⟩ = ⟨2, 2⟩ + ⟨d_{2} − 1, u_{2} − 1⟩

= ⟨2, 2⟩ + ⟨d_{2} − 1, u_{2} − 1⟩

by the induction hypothesis

= ⟨u_{2}, d_{2}⟩

(b) Suppose e_{1} ⩽ e_{2} ⩽ e_{3}. Let a_{1} = f(e_{1}), a_{2} = f(e_{2}), a_{3} = f(e_{3}). We will show first that the shortest up-fence F from a_{1} to a_{3} is no longer than the shortest up-fence from a_{1} to a_{2}. Since a_{3} is strictly to the right of a_{2} in our picture of T, F must pass through either f(e_{2}) or a′_{2} = f(e_{2}^{∗}). If F passes through a_{2}, the result is immediate. So suppose not, then a_{2} ≠ a′_{2}, so e_{2} ≠ e_{2}^{∗}, and moreover we must have e_{3} ⩾ e_{2} ∨ e_{2}^{∗}, so F must pass through b = f(e_{2} ∨ e_{2}^{∗}) But a fence from a_{1} to a_{3} via a′_{2} and b is at least as long as the fence from a_{1} to a_{2} via a′_{2} and b. The same reasoning holds for the shortest down-fence from a_{1} to a_{3}, so combining these, we must have d_{T} (a_{1}, a_{2}) ⩽ d_{T} (a_{1}, a_{3})

(c) if e_{1} = ⟨n, n⟩, then the result is immediate from (a). Otherwise, let e_{1} = ⟨u_{1}, d_{1}⟩. Suppose u_{1} is even and d_{1} odd, so that f(e_{1}) is on the “bottom” of T. Then from the definition of addition we have

\begin{aligned}⟨u_{1}, d_{1}⟩ + ⟨u_{2}, d_{2}⟩ & = & ⟨u_{1} + d_{2} − 1, d_{1} + d_{2} − 1⟩ \\ & = & ⟨u_{1}, d_{1}⟩ + ⟨u_{2}, d_{2}⟩^{△} \end{aligned}

Thus if e_{2} = ⟨1, 1⟩ or ⟨2, 1⟩, e_{1} + e_{2} = e_{1} and the result is trivial. So suppose d_{2} > 1, and let e = ⟨max(u_{1}, d_{1}), max(u_{1}, d_{1})⟩. Observe from the diagram that d_{T} (f(e_{1}), f(e)) = ⟨1, 2⟩. Now for any e′ ⩾ ⟨2, 1⟩, every fence from f(e_{1}) to f(e_{1} + e′) must pass through f(e), so

d_{T} (f(e_{1}), f(e_{1} + e_{2})) = d_{T} (f(e_{1}), f(e_{1} + e_{2}^{△}))
⩽ d_{T} (f(e_{1}), f(e)) + d_{T} (f(e), f(e + e_{2}^{△}))
⩽ ⟨1, 2⟩ + e_{2}^{△}  (by part (a))
= e_{2}^{△}

The final equality holds because since d_{2} > 1, e_{2}^{△} = ⟨d_{2} − 1, d_{2}⟩, and ⟨1, 2⟩ + ⟨d_{2} − 1, d_{2}⟩ = ⟨d_{2} − 1, d_{2}⟩. The result follows dually if u_{1} is odd and d_{1} even.

(d) Suppose e_{1} +e_{2} ⩾ e_{3}, e_{3} +e_{2}^{−1} ⩾ e_{1}. If e_{3} ⩾ e_{1}, then since e_{1} +e_{2} ⩾ e_{3} we have d(f(e_{1}), f(e_{3})) ⩽ d(f(e_{1}), f(e_{1} + e_{2})) (by (b)) and thus d(f(e_{1}), f(e_{3})) ⩽ e_{2} (by (c) and transitivity). Similarly if e_{1} ⩾ e_{3}, then since e_{3} + e_{2}^{−1} ⩾ e_{1}, we have we have d(f(e_{3}), f(e_{1})) ⩽ d(f(e_{3}), f(e_{3} + e_{2}^{−1})) ⩽ e_{2}^{−1}, and thus d(f(e_{1}), f(e_{3})) ⩽ e_{2}. Finally if e_{1} and e_{3} are incomparable, e_{1} = e_{3}^{∗}. If e_{1} = ⟨u, d⟩ with u even and d odd, then observe from the diagram that we must have e_{2} ⩾ ⟨1, 2⟩, and the result follows immediately. The case where u is odd and d even is dual. □

2.4.8 Proposition: Suppose I includes Q, and is solvable over Q. Then there is a Helly poset Q′ such that Q′ ⊳ Q, and a solution θ of I over Q′ such that the inequalities in I are satisfied exactly.

Proof: We will construct Q′ one component at a time. Suppose I is connected. Let ⟨m, m⟩ be longer than any distance in I, U and D be as in lemma(2.4.6), and the types occurring in I be τ_{1} … τ_{k}. We define Q′ = (U × D)^{k}, with

θ(τ_{i}) = (f(d_{I}(τ_{1}, τ_{i})), f(d_{I}(τ_{2}, τ_{i})), …, f(d_{I}(τ_{k}, τ_{i}))

so that

d_{Q′}(θτ_{1}, θτ_{2}) = ⋁_{i⩽k}(d_{U×D}(f(d(τ_{i}, τ_{1})), f(d(τ_{i}, τ_{2})))

θ is obviously injective, so it suffices to show that it is distance preserving (for then it is obviously order-preserving). So suppose d_{I}(τ_{i}, τ_{j}) = e. Then θ(τ_{i}) has f(⟨1, 1⟩) in the ith position, and θ(τ_{j}) has f(e), so d_{Q′}(θ(τ_{i}), θ(τ_{j})) is at least e by lemma(2.4.7(a))

On the other hand, since I is closed under Join and Min, for any τ_{h} we have d_{I}(τ_{h}, τ_{i})+e ⩾ d_{I}(τ_{h}, τ_{j}) and d_{I}(τ_{h}, τ_{j})+e^{−1} ⩾ d_{I}(τ_{h}, τ_{i}), thus by lemma(2.4.7(d))

d_{U×D}(f(d_{I}(τ_{h}, τ_{i})), f(d_{I}(τ_{h}, τ_{j}))) ⩽ e

So in each position h in the k-tuples θ(τ_{1}) and θ(τ_{j}), we have some e′ ⩽ e. Thus we obtain exactly d_{Q′}(θ(τ_{i}), θ(τ_{j})) = e. Finally, since I contains Q, and the distances between elements of Q in Q′ are the same as those in Q, θ is an embedding of Q in Q′ and Q′ is distance consistent over Q, so we have Q′ ⊳ Q.

If I is not connected, we define Q′ to be the direct sum of the products for each component. The distance inequalities in I are met exactly, and Q′ ⊳ Q. □

2.4.9 Definition: A variety V is generated by a subset U ⊆ V if every element of V can be constructed from U using products and retractions.

As a simple corollary of proposition(2.4.8), we have the following, which is also proven in [30]

2.4.10 Proposition: The variety of finite connected Helly posets is generated by the set of fences.

Proof: Just take I in proposition(2.4.8) to be the closure of Q under Dual, Min, and Join. Then Q′ is a product of fences such that Q′ ⊳ Q, and since every fence is Helly and Helly posets are closed under products, the result is immediate.

□

We can now show that all Helly posets have HSFs

2.4.11 Proposition: If Q is a fence, Q has a HSF.

Proof: Suppose Q is an up-fence a_{1} ⩽ a_{2} ⩾ a_{3} . . .. If S is a Q-consistent Q-state, the set of inequalities

{d(α, a) ⩽ S(a) | S(a)↓}

is distance consistent, and thus solvable over Q. So we define

j(S) = min{i | ∀b ∈ Q. d_{Q}(a_{i}, b) ⩽ S(b)}

and set F_{Q}(S) = a_{j(S)}.

Now suppose F_{Q} is not a HSF for Q, so that we have Q-states S and T and e ∈ L such that d_{Q}(S, T) ⩽ e but d_{Q}(F_{Q}(S), F_{Q}(T) ⩽̸ e. Obviously we cannot have S = T = S_{⊥}, and we cannot have F_{Q}(S) = F_{Q}(T), so j(S) ≠ (T). We may assume j(S) < j(T).

Let b be such that d_{Q}(a_{j(T)}, b) ⩽ T (b), but d_{Q}(a_{j(T)−1}, b) ⩽̸ T (b), in other words b is an obstacle to j(T) being any smaller. Suppose that j(S) and j(T) are both odd, so that d(F_{Q}(S), F_{Q}(T)) = ⟨2n, 2n + 1⟩, so d(F_{Q}(T), b) = ⟨m, m + 1⟩, and d_{Q}(F_{Q}(S), b) = ⟨2n + m, 2n + m + 1⟩.

F_{Q}(S)  F_{Q}(T)  b

Since b is an obstacle to j(T) being any smaller, ⟨m + 2, m + 1⟩ ⩽̸ T (b), and so T (b) ⩽ ⟨m + 1, m + 2⟩. And since d_{Q}(F_{Q}(S), F_{Q}(T)) ⩽̸ e, ⟨2n, 2n + 1⟩ ⩽̸ e, so e ⩽ ⟨2n + 1, 2n⟩. Thus by monotonicity of addition, S(b) ⩽ ⟨2n + 1, 2n⟩ + ⟨m + 1, m + 2⟩ = ⟨2n + m + 1, 2n + m⟩.

But by the choice of F_{Q}(S), d_{Q}(F_{Q}(S), b) = ⟨2n + m, 2n + m + 1⟩ ⩽ S(b), a contradiction. The cases where j(S) and j(T) are not both odd result in similar contradictions. □

2.4.12 Proposition: If Q_{1} and Q_{2} are connected and have HSFs, Q = Q_{1} × Q_{2} has a HSF.

Proof: We define

π_{1}(S)(a_{1}) = ⋀_{a_{2}∈Q_{2}}{S(a_{1}, a_{2}))}
π_{2}(S)(a_{2}) = ⋀_{a_{1}∈Q_{1}}{S(a_{1}, a_{2}))}

and we claim that

F_{Q}(S) = (F_{Q_{1}}(π_{1}(S)), F_{Q_{2}}(π_{2}(S)))

is a HSF for Q. Let S and T be Q-states. If S = T = S_{⊥}, then the result is trivial. Otherwise, let S_{1} = π_{1}(S), T_{1} = π_{1}(T). Since dom(S) = dom(T) = Q dom(S_{1}) and dom(T_{1}) = Q_{1}, and for any a = (a_{1}, a_{2}), b = (b_{1}, b_{2}) ∈ Q, we have d_{Q}(a, b) = d_{Q_{1}}(a_{1}, b_{1}) ∨ d_{Q_{2}}(a_{2}, b_{2}), so for any a_{2} and b_{2} we have

d_{Q_{1}}(a_{1}, b_{1}) ⩽ S((a_{1}, a_{2}))^{−1} + S((b_{1}, b_{2}))

so

d_{Q_{1}}(a_{1}, b_{1}) ⩽ ⋀_{a_{2}∈Q_{2}} S((a_{1}, a_{2}))^{−1} + ⋀_{b_{2}∈Q_{2}} S((b_{1}, b_{2}))

thus

d_{Q_{1}}(a_{1}, b_{1}) ⩽ ( ⋀_{a_{2}∈Q_{2}} S(a_{1}, a_{2}))^{−1} + ⋀_{b_{2}∈Q_{2}} S((b_{1}, b_{2}))

so d_{Q_{1}}(a_{1}, b_{1}) ⩽ S_{1}(a_{1})^{−1} + S_{1}(b_{1}), and thus S_{1} is Q_{1}-consistent; similarly for T_{1}. Moreover, if we have e such that S((a_{1}, a_{2})) ⩽ e + T ((a_{1}, a_{2})) and T ((a_{1}, a_{2})) ⩽ e^{−1} + S((a_{1}, a_{2})), then for any a_{1} ∈ Q_{1} we have

S_{1}(a_{1}) = ⋀_{a_{2}∈Q_{2}}{S(a_{1}, a_{2}))}
⩽ ⋀_{a_{2}∈Q_{2}}{e + T (a_{1}, a_{2}))}
⩽ e + ⋀_{a_{2}∈Q_{2}}{T (a_{1}, a_{2}))}
⩽ e + T_{1}(a_{1})

and similarly T_{1}(a_{1}) ⩽ e^{−1} + S_{1}(a_{1}). So d_{Q_{1}}(F_{Q_{1}}(S_{1}), F_{Q_{1}}(T_{1})) ⩽ e. By a similar argument it is straightforward that d_{Q_{2}}(F_{Q_{2}}(π_{2}(S)), F_{Q_{2}}(π_{2}(T))) ⩽ e, and thus d_{Q}(F_{Q}(S), F_{Q}(T)) ⩽ e. □

2.4.13 Proposition: If Q_{1} and Q_{2} are connected and Helly, r : Q_{1} ⊳ Q_{2}, and Q_{1} has a HSF, Q_{2} has a HSF

Proof: For S a Q_{2}-state, we define

i(S)(a) = ⋀_{b∈Q_{2}} S(b) + d_{Q_{1}}(b, a)

and

F_{Q_{2}}(S) = r(F_{Q_{1}}(i(S)))

and will show that F_{Q_{2}} is a HSF for Q_{2}. Let S and T be Q_{2}-consistent Q_{2}-states such that dom(S) = dom(T). If S = T = S_{⊥}, the result is immediate. Otherwise it is straightforward that dom(i(S)) = dom(i(T)) = Q_{1}. Now

i(S)(a)^{−1} + i(S)(b) = ( ⋀_{c∈Q_{2}} S(c) + d_{Q_{1}}(c, a))^{−1} + ⋀_{d∈Q_{2}} S(d) + d_{Q_{1}}(d, b)
= ⋀_{c,d∈Q_{2}} d_{Q_{1}}(a, c)) + S(c)^{−1} + S(d) + d_{Q_{1}}(d, b))

But since Q_{1} ⊳ Q_{2},

d_{Q_{1}}(a, b) ⩽ ⋀_{c,d∈Q_{2}} d_{Q_{1}}(a, c) + d_{Q_{1}}(c, d) + d_{Q_{1}}(d, b)
= ⋀_{c,d∈Q_{2}} d_{Q_{1}}(a, c) + d_{Q_{2}}(c, d) + d_{Q_{1}}(d, b)
⩽ ⋀_{c,d∈Q_{2}} d_{Q_{1}}(a, c) + S(c)^{−1} + S(d) + d_{Q_{1}}(d, b)

So i(S) and similarly i(T) are Q_{1}-consistent Q_{1}-states. Now if we have e such that S(a) ⩽ e + T (a) and T (a) ⩽ e^{−1} + S(a) for all a ∈ Q_{2}, then for all a ∈ Q_{1}, we have

i(S)(a) = ⋀_{b∈Q_{2}} S(b) + d_{Q_{1}}(b, a)
⩽ ⋀_{b∈Q_{2}} e + T (b) + d_{Q_{1}}(b, a)
⩽ e + (⋀_{b∈Q_{2}} T (b) + d_{Q_{1}}(b, a))
⩽ e + i(T)(a)

and similarly i(T)(a) ⩽ e^{−1} + i(S)(a). Thus d_{Q_{1}}(F_{Q_{1}}(S), F_{Q_{1}}(T)) ⩽ e, and since Q_{1} ⊳ Q_{2}, by proposition(2.2.5), d_{Q_{2}}(r(F_{Q_{1}}(S)), r(F_{Q_{1}}(T))) ⩽ e, i.e. d_{Q_{2}}(F_{Q_{2}}(S), F_{Q_{2}}(T)) ⩽ e. □

2.4.14 Proposition: Every Helly poset Q has a HSF.

Proof: By proposition(2.4.10), using propositions (2.4.11), (2.4.12), and (2.4.13), each connected component Q_{i} of Q has a HSF. We choose some element v ∈ Q and define F_{Q}(S_{⊥}) = v. For any other Q-state S, dom(S) = Q_{i} for some i, and since S is Q-consistent, it is Q_{i}-consistent, and we may apply the HSF for Q_{i}. □

In chapter 4 we will extend this result to consider solvability and solution for constraint sets over types built with type constructors.

Simplification and Entailment

In this chapter we will deal with the question of what it means for a constraint set C_{1} to entail a constraint set C_{2}, and how the relation can be tested. We will only work with atomic constraint sets: if the subtyping system involves only structural constructors over a poset of atomic types, it is straightforward using substitution (although potentially expensive) to reduce any ML_{⩽} typing judgement to atomic form. It is easy to demonstrate that the reduction yields a halbstark instance of the original typing judgement, and that all ground instances of the original judgement are instances of its atomic reduction.

There are several possible ways in which we might characterise entailment: however a property we would certainly intuitively expect can be stated informally as follows: every valid interpretation (i.e. solution) of C_{1} is a solution of C_{2}.

Consider the term λfλx. f(fx). Our type inference algorithm yields the type α→β→ϵ with the constraint set C = {α⩽β→γ, α⩽δ→ϵ, γ⩽δ}. By the substitution [α_{1}→α_{2}/α] we can reduce this to atomic form:

C′ = {β⩽α_{1}, α_{2}⩽γ, δ⩽α_{1}, α_{2}⩽ϵ, γ⩽δ}

By principality, the types at which we can use this expression are precisely those for which the constraints are solvable. But in such solutions, we only care about the values of α, β, and ϵ (or in the atomic form of the judgement, α_{1}, α_{2}, γ and ϵ). Given particular values for these variables, as long as there are some values for γ and δ which satisfy the constraints, we don’t really care what they are. We can think of the variables α, β, and ϵ as externally observable, in the sense that in any solution their exact values are reflected in the type of the expression, whereas the values of γ and δ are internally hidden.

Each constraint set with which we deal will have some set of variables which we consider internal; the rest will be external. In a judgement Γ; C_{1} ⊢ e : ∀\overline{α}\C_{2}.τ, the internal variables will be those occurring in \overline{α} but not in τ; the externals will be the others: in our type inference algorithm they will be those occurring in τ and Γ, since these are the variables which can be “observed” by the operation of the inference algorithm. We will refer to the set of base types and external variables together as the observables.

A further consideration for entailment is the class of posets over which instantiation can occur. The requirement that all solutions of C_{1} have solutions of C_{2} which are equal on externals over a class Q of posets becomes stronger as Q becomes larger. For example, if we know we will only work over discrete posets, then all constraints will be instantiated as equalities and the entailment relation will be relatively weak (i.e. it relates a lot of constraint sets); on the other hand if entailment over all posets is required, then the relation is much stronger (i.e. relates fewer constraint sets). The merit of a weak entailment relation is that we obtain more equivalences (equivalence just being entailment in both directions) and thus more effective ways of finding small representations of constraint sets, i.e. we get better simplification. For example we have seen that simplifications can be made to the type of max if we only consider instantiations over a Helly poset; if we must also consider the 2-crown, no simplification is possible. And the constraint sets {α⩽a} and {α⩽a, a⩽α} have the same solutions for α over a tree Q in which if a is a leaf. But if we need to consider possible future extensions of the tree where there is some type b < a, the two constraint sets are clearly not equivalent: [b/α] is a solution of one but not the other.

We will work with Helly posets, and since we are interested in languages with an extensible subtyping relation, we will consider entailment relations which are valid over various classes of Q-models for some Helly poset Q which represents the current ordering. We will thus always assume that Q is included in the constraint set, since by proposition(2.2.6) this doesn’t change the set of solutions over any Q-model.

We will start with a study of entailment and equivalence of constraint sets, and then proceed to define our simplification technique.

3.1 Entailment

Suppose we have two constraint sets C_{1} and C_{2} which share the same set \overline{α} of externals. We may regard the internal variables as implicitly existentially quantified: If C is a constraint set and Q is a poset, we will say Q ⊧ C if there is a ground substitution θ : fv(C)→Q such that all the constraints in θC are satisfied over Q. Then if \overline{α} is the set of externals in C, we will say θ:\overline{α}→Q is a (Q, \overline{α})-solution of C for \overline{α} over Q if Q ⊧ θC.

Our relation will be parameterised by a set of externals \overline{α} and a class of posets Q. Given constraint sets C_{1} and C_{2}, we say C_{1} entails C_{2} for \overline{α} over Q and write

C_{1} ⊧Q ; \overline{α} C_{2} iff

∀Q∈Q ∀θ:\overline{α}→Q (Q ⊧ θC_{1} ⇒ Q ⊧ θC_{2})

Then C_{1} and C_{2} are equivalent (for \overline{α} over Q) if C_{1} ⊧Q ; \overline{α} C_{2} and C_{2} ⊧Q ; \overline{α} C_{1}.

As might be expected, since we are considering Helly posets, we can characterise the set of (Q, \overline{α})-solutions of a constraint set C in terms of the minimal distances between the observables. In fact, we have a stronger result: that we can also characterise the (Q′, \overline{α})-solutions of C over any Q′ ⊳ Q in terms of these distances.

3.1.1 Proposition: Suppose I is a set of distance inequalities which is distance consistent over Q, Q′ is a Q-model, and \overline{α} is the set of externals in I. Then for any substitution θ : \overline{α} → Q′, θI is distance consistent (and hence solvable) iff for every pair τ_{1}, τ_{2} ∈ Q ∪ \overline{α}, d_{Q′}(θτ_{1}, θτ_{2}) ⩽ d_{I}(τ_{1}, τ_{2}).

Proof: If θI is distance consistent over Q′, then for any pair τ_{1}, τ_{2} ∈ Q ∪ \overline{α}, d_{Q′}(θτ_{1}, θτ_{2}) ⩽ d_{θI}(θτ_{1}, θτ_{2}). Any proof I ⊢ d(τ_{1}, τ_{2}) ⩽ e translates to a proof of θI ⊢ d(θτ_{1}, θτ_{2}) ⩽ e, so d_{θI}(θτ_{1}, θτ_{2}) ⩽ d_{I}(τ_{1}, τ_{2}), and thus d_{Q′}(θτ_{1}, θτ_{2}) ⩽ d_{I}(τ_{1}, τ_{2}).

Conversely, suppose for every pair τ_{1}, τ_{2} ∈ Q ∪ \overline{α}, d_{Q′}(θτ_{1}, θτ_{2}) ⩽ d_{I}(τ_{1}, τ_{2}). If θI is not solvable, then by proposition(2.3.5) there must be some chain in θI between θτ_{1} and θτ_{2} of length e containing only variables other than at its endpoints, such that d_{Q′}(θτ_{1}, θτ_{2}) ⩽̸ e. But these variables are exactly the internals in I, so the chain exists in I, and thus d_{I}(τ_{1}, τ_{2}) ⩽ e, a contradiction to the assumption d_{Q′}(θτ_{1}, θτ_{2}) ⩽ d_{I}(τ_{1}, τ_{2}). □

Tautness

In order to characterise the solutions of constraint set, we will use a notion of closure:

3.1.2 Definition [tautness]: A set I of distance inequalities is taut for some set of variables \overline{α} over some class Q of posets if for any inequality d(τ_{1}, τ_{2}) ⩽ e in I and any e′ < e, there is a poset Q ∈ Q and a (Q, \overline{α})-solution of I which is not a (Q, \overline{α})-solution to I ∪ \{d(τ_{1}, τ_{2}) ⩽ e′\}. J is a tautening of I on \overline{α} over Q if it is taut, and equivalent to I for \overline{α} over Q.

If I and J are both taut for some set of variables \overline{α} over Q, then there is no scope for decreasing any of the distances between observables without losing some solutions. So they are equivalent over Q iff the minimum distances between their observables are the same. Moreover, the set of solutions of I will be a strict subset of the solution set of J if some of the distances in I are shorter than those of J and none are longer. Thus the notions of entailment and equivalence can be tested by computing tautenings.

Over a poset Q, the weakest (in the sense of giving fewest entailment judgements) equivalence that we shall study using this notion is equivalence over the whole class of Helly posets which retract to Q. We have the following easy result:

3.1.3 Proposition: Suppose I is a set of distance inequalities containing Q and solvable over Q, and closed under Dual, Min, and Join, and \overline{α} is some set of external variables in I. Then I is taut for \overline{α} over the class of Helly posets which retract to Q.

Proof: By proposition(2.4.8) there is a Helly poset Q′⊳Q in which the distances in I between the observables \overline{α} ∪ Q are satisfied exactly, and by decreasing any of these distances this solution is lost. □

3.2 Simplification

Suppose we are interested in simplifications which preserve all solutions over a particular class of posets. Returning to the example of max, defining C = {α⩽Real, β⩽Real, α⩽γ, β⩽γ}, we have

∅; C ⊢ max : α→β→γ ≪ ∅; δ ⩽ Real ⊢ max : δ→δ→δ

with the substitution θ(α) = θ(β) = θ(γ) = δ. But it is easy to see that there is no substitution θ′ such that

{α ⩽ Real, β ⩽ Real, α ⩽ γ, β ⩽ γ} ⊢ {θ′(δ) ⩽ Real, θ′(δ→δ→δ) ⩽ α→β→γ}

so

∅; δ ⩽ Real ⊢ max : δ→δ→δ ≪̸ ∅; C ⊢ max : α→β→γ

in other words, although we know that the two typings for max are equivalent over the class of Helly posets, the halbstark relation is too strong to validate the equivalence. Of course we expect this: the ⊢ relation between constraint sets uses syntactic entailment and so is independent of the choice of underlying order, but as we have seen, the simplification is not valid over a 2-crown. Thus we introduce a weaker instance relation on typing judgements, which exploits our notion of semantic entailment.

3.2.1 Definition: D_{1} ≡ Γ_{1}; C_{1} ⊢ e : τ_{1} has as an instance D_{2} ≡ Γ_{2}; C_{2} ⊢ e : τ_{2} (we write D_{1} ≪ D_{2}) if there is a substitution θ such that

1. C_{2} ⊧Q ; \overline{α} θ(C_{1}) ∪ \{θτ_{1} ⩽ τ_{2}\}

2. Γ_{2} = θ(Γ_{1})

where \overline{α} is the set of variables occurring in τ_{2} and Γ_{2}.

It is hard to see how this instance relation can be shown to be sound in the syntactic sense of [22]: that if we have a typing derivation of D_{1} and D_{1} ≪ D_{2} then we have a typing derivation of D_{2}; rather we would need some semantic notion of the soundness of an instance relation. There are, however, good reasons to adhere to syntactic soundness: we can expect compilation to be defined on elaborated terms produced by type inference, so simplification on constraint sets had better have an operational interpretation on the corresponding elaborated terms. For example, the derivation

∅; C ⊢ max : α→β→γ

can be interpreted as building, in the presence of the coercions c_{1}:α→Real, c_{2}:β→Real, c_{3}:α→γ, c_{4}:β→γ, the elaborated term

λx:α.λy:β.if c_{1}(x) < c_{2}(y) then c_{3}(x) else c_{4}(y)

The statement

∅; C ⊢ max : α→β→γ ≪ ∅; δ⩽Real ⊢ max : δ→δ→δ

then constructs, via the substitution [δ/α] ∘ [δ/β] ∘ [δ/γ] the coercions c_{5}:δ→Real and c_{6}:δ→δ, and the term

λx:δ.λy:δ.if c_{5}(x) < c_{5}(y) then c_{6}(x) else c_{6}(y)

This observation extends to simplification: if we decrease the size of the coercion set for a term, it is necessary to be able to build a term elaborated with appropriate coercions chosen from the reduced constraint set. If the simplified constrained type is a halbstark instance of the original (and we have the substitution and proof terms to justify this) then we can always do so, and in this sense simplification of a judgement to one of its halbstark instances is constructive simplification. It is harder to see how we might operationally interpret simplification to a semantic instance. However, we will demonstrate a practical, constructive approach to simplification based on our semantic entailment relation. When simplifying D_{1} to D_{2} we shall ensure always that

1. D_{1} ≪ D_{2}, and we shall be able to derive the necessary substitution and proofs which justify entailment

2. D_{2} ≪ D_{1}

Syntactic soundness will ensure that simplification has the required operational nature; semantic completeness that no possible solution will be lost in any possible context where the constraints might be instantiated. In this setting we can simplify max, because with the substitution [δ/γ] ∘ [δ/β] ∘ [δ/γ] we obtain

δ⩽Real ⊢ δ⩽δ, δ⩽δ, δ⩽Real, δ ⩽ Real, δ→δ→δ⩽δ→δ→δ

and using the empty substitution for semantic entailment,

α⩽γ, β⩽γ, α⩽Real, β⩽Real, ⊧_{{α,β,γ}}^{Q} δ⩽Real, δ→δ→δ⩽α→β→γ

over any class of Helly posets Q, since in any solution θ for the LHS, we can set δ to be the least upper bound of θα and θβ, which exists in any Helly poset by prop(2.2.8).

Our strategy for simplification will operate on the set of distance inequalities which are the closure of the set of constraints. We will define a simplifying transformation, H-contraction on sets of distance inequalities, which will reduce distances whilst preserving semantic equivalence, and from a set of distance inequalities thus contracted we will extract a simplifying substitution and additional constraints which generate a halbstark instance of the original constraint set. There is a slight difference in the notion of completeness when dealing with constrained type schemes rather than just constraint sets: in the latter case we must preserve all solutions, whereas in the former we need only preserve those ground instances of the type scheme which are minimal (amongst ground instances); then for any type τ at which we intended to use the original type scheme we are guaranteed to have an instance of the simplified type which can be coerced to τ.

Our simplification algorithm is based on an entailment relation, and we will choose the simplest: entailment over the class of Helly posets. So tautness is simply closure under Dual, Join, and Min. We will also assume that our set of distance inequalities is connected, since if it is not, we simplify it component-wise.

Contraction

Suppose that I is C’s closure under Dual, Symm, and Join. For variables constrained by C is semantically the same as to be constrained by I, so we will consider Γ; I ⊢ e : τ to have the obvious meaning, and to be semantically equivalent to Γ; C ⊢ e : τ.

Our simplification algorithm operates by reducing the distances in I. Suppose we have a derivation Γ; I ⊢ e : τ, and we wish to add d(τ_{1}, τ_{2}) ⩽ e to I to obtain I′ such that Γ; I′ ⊢ e : τ is semantically equivalent. Let θ = [\overline{β}/\overline{α}] be a renaming substitution on the internal variables in I′ using fresh variables \overline{β}; then the conditions we require are that

θI′ ⊧ I ∪ {τ ⩽ θτ}

and

I ⊧ θI′ ∪ {θτ ⩽ τ}

for the variables in Γ and τ over the class of Helly posets which retract to the underlying base poset. Since every solution of I′ is a solution of I, the first is immediate. So writing J = θI′ ∪ {θτ ⩽ τ}, our condition is equivalent to requiring the distances in I between observables to be less than (or equal to) the distances in J. In fact, for observables α_{1} and α_{2}, we must have

d_{J} (α_{1}, α_{2}) = d_{J} (α_{1}, θα_{1}) + d_{J} (θα_{1}, θα_{2}) + d_{J} (α_{2}, θα_{2})

since for any observable α, either α occurs in τ, in which case the only path from an α to any other atom in J is via θ(α), or α = θα, so d(α, θα) = ⟨1, 1⟩. And since θ is just a renaming on I,

d_{J} (θα_{1}, θα_{2}) = d_{θI}(θα_{1}, θα_{2}) = d_{I}(α_{1}, α_{2})

Now since for any variable α occurring in τ the only inequalities relating α and θα are derived from the decomposition of the inequality d(θτ, τ) ⩽ ⟨1, 2⟩, by structural induction on the type τ

• d_{J} (α, θα) = ⟨2, 1⟩ if α occurs only positively in τ

• d_{J} (α, θα) = ⟨1, 2⟩ if α occurs only negatively in τ

• d_{J} (α, θα) = ⟨1, 1⟩ otherwise.

So choosing ⟨k, k⟩ larger than any distance in J, we define the polarity function ζ on the atoms occurring in I:

ζ(α) = ⟨2, 1⟩ if α occurs only positively in τ
ζ(α) = ⟨1, 2⟩ if α occurs only negatively in τ
ζ(α) = ⟨k, k⟩ if α is an internal
ζ(α) = ⟨1, 1⟩ otherwise

so that ζ(α) = d_{J} (α, θα) for each external α, and our condition is equivalent to requiring for every pair of atoms α_{1} and α_{2} occurring in I that

d_{I}(α_{1}, α_{2}) ⩽ ζ(α_{1}) + d_{J} (θα_{1}, θα_{2}) + ζ(α_{2})^{−1}

We can now define a transformation on sets of distance inequalities:

3.2.2 Definition [Contraction]: Suppose I is a set of distance inequalities over atomic types with a polarity function ζ, and τ_{1} and τ_{2} are types occurring in I. d(τ_{1}, τ_{2}) ⩽ e is a contraction of I if

• d_{I}(τ_{1}, τ_{2}) ⩽̸ e

• for any α_{1}, α_{2} occurring in I

d_{I}(α_{1}, α_{2}) ⩽ ζ(α_{1}) + d_{I}(α_{1}, τ_{1}) + e + d_{I}(τ_{2}, α_{2}) + ζ(α_{2})^{−1}

Although it is convenient to use the condition d_{I}(τ_{1}, τ_{2}) ⩽ e, obviously the contraction decreases the distance from τ_{1} to τ_{2} to e ∧ d_{I}(τ_{1}, τ_{2}). A contraction is thus exactly any reduction of a single distance inequality in I which preserves semantic equivalence.

Verifying that an inequality is a contraction requires time quadratic in the size of the constraint set. Thus we introduce a transformation which equally effective but easier to validate.

3.2.3 Definition [H-contraction]: Suppose I is a set of distance inequalities over atomic types with a polarity function ζ, and τ_{1} and τ_{2} are types occurring in I. d(τ_{1}, τ_{2}) ⩽ e is a H-contraction of I if

• d_{I}(τ_{1}, τ_{2}) ⩽̸ e

• for every τ, d_{I}(τ, τ_{2}) ⩽ ζ(τ) + d_{I}(τ, τ_{1}) + e

3.2.4 Proposition: If d(τ_{1}, τ_{2}) ⩽ e is an H-contraction of I, it is a contraction of I.

Proof: Let α_{1}, α_{2} be atoms occurring in I, and J = I ∪ {d(τ_{1}, τ_{2}) ⩽ e. We will show that for any pair of atoms α_{1}, α_{2} occurring in I,

d_{I}(α_{1}, α_{2}) ⩽ ζ(α_{1}) + d_{J} (α_{1}, α_{2}) + ζ(α_{2})^{−1}

which is equivalent to showing that for any chain X of minimal length from α_{1} to α_{2},

d_{I}(α_{1}, α_{2}) ⩽ ζ(α_{1}) + e_{X} + ζ(α_{2})^{−1}

where e_{X} is the length of X. So suppose X is a chain of minimal length. If X does not contain a link from τ_{1} to τ_{2} or vice versa, then the result follows immediately from the fact that X is a chain in I. Otherwise, since X is of minimal length, we may assume the link occurs at most once in the chain. So suppose X contains a link from τ_{1} to τ_{2}, then the length of X is at least

d_{I}(α_{1}, τ_{1}) + e + d_{I}(τ_{2}, α_{2})

So

ζ(α_{1}) + e_{X} + ζ(α_{2})^{−1} ⩾ ζ(α_{1}) + d_{I}(α_{1}, τ_{1}) + e + d_{I}(τ_{2}, α_{2}) + ζ(α_{2})^{−1}
⩾ d_{I}(α_{1}, τ_{2}) + d_{I}(τ_{2}, α_{2}) + ζ(α_{2})^{−1}
since d(τ_{1}, τ_{2}) ⩽ e is a H-contraction
⩾ d_{I}(α_{1}, α_{2})

The case where X has a link is from τ_{2} to τ_{1} is dual. □

Although not every contraction is a H-contraction, we can use H-contractions to obtain exactly the same distance reductions as can be performed with contractions.

3.2.5 Proposition: Suppose no H-contraction is possible on I. Then no contraction is possible.

Proof: Suppose no H-contraction is possible, and we have d(τ_{1}, τ_{2}) ⩽ e with d_{I}(τ_{1}, τ_{2}) ⩽̸ e. Since d(τ_{1}, τ_{2}) ⩽ e is not a H-contraction, there is some α_{1} such that

d_{I}(α_{1}, τ_{2}) ⩽̸ ζ(α_{1}) + d_{I}(α_{1}, τ_{1}) + e

Thus

d_{I}(τ_{2}, α_{1}) ⩽̸ e^{−1} + d_{I}(τ_{1}, α_{1}) + ζ(α_{1})^{−1}

But d_{I}(τ_{2}, α_{1}) ⩽ e^{−1} + d_{I}(τ_{1}, α_{1}) + ζ(α_{1})^{−1} is not a H-contraction, so there must be some α_{2} such that

d_{I}(α_{2}, α_{1}) ⩽̸ ζ(α_{2}) + d_{I}(α_{2}, τ_{2}) + e^{−1} + d_{I}(τ_{1}, α_{1}) + ζ(α_{1})^{−1}

But then

d_{I}(α_{1}, α_{2}) ⩽̸ ζ(α_{1}) + d_{I}(α_{1}, τ_{1}) + e + d_{I}(τ_{2}, α_{2}) + ζ(α_{2})^{−1}

so d(τ_{1}, τ_{2}) ⩽ e is not a contraction. □

From Distances to Constraints

Having completely H-contracted a set of distance inequalities, it remains to simplify the constraint set from the set of minimal distances. Our strategy will be as follows: If we obtain a minimal distance of ⟨1, 1⟩ between two types, then they must have the same value in any solution, and we can identify them in the constraint set by substitution. If we obtain ⟨1, 2⟩ or ⟨2, 1⟩, we can choose to add this to the constraint set if its transitive reduction is thereby decreased. Since adding these distance inequalities to I preserves the possible minimal typings of ∀\overline{α}\I. τ, adding them to C will do the same. Thus we have:

3.2.6 Proposition: Suppose we have a derivation D_{1} ≡ Γ ⊢ e : ∀\overline{α}\C. τ, and using H-contraction we obtain the substitution θ and additional constraint set C′. Let D_{2} ≡ θΓ ⊢ e : ∀\overline{α}\θC ∪ C′. θτ. Then D_{1} ≪ D_{2}, and D_{2} ≪ D_{1}.

Proof: It is easy to see that substitution and weakening of constraint sets results in a halbstark instance.

From the discussion of H-contraction, it should be clear that for any ground instance θ_{g} of the D_{1}, θ_{g} is a solution of θC ∪ C′, and we have (θ_{g} ∘ θ)τ ⩽ θτ. Thus D_{2} ≪ D_{1}. □

3.2.7 Remark: If I cannot be contracted (or equivalently, cannot be Hcontracted), then any reduction of any distance inequality results in set in which some solution is lost. Thus if a type scheme has a constraint-free equivalent representation, i.e. one in which all distances have been contracted to ⟨1, 1⟩, it will be found by repeated H-contraction.

Comparison with other approaches

While we have only considered this approach in the context of atomic types, in principle simplification techniques over Helly posets have wider applicability than model-based approaches which operate over lattices only. A fairly direct comparison is possible with the system of Fuh and Mishra [18], also based on atomic constraint sets, which uses syntactic rather than semantic criteria for simplification. They define two kinds of transformation on constraint sets: Ssimplification and G-simplification.

3.2.8 Definition: Let ↑_{C} (α) be the set of atoms α′ such that α ⩽ α′ is in the transitive closure of C, and ↓_{C} (α) the set of atoms α′ such that α′ ⩽ α correspondingly.

A G-simplification on a constraint set C is a substitution [α_{2}/α_{1}] where α_{1} is an internal variable, and ↑_{C} (α_{1})\{α} ⊆ ↑_{C} (α_{2}) and ↓_{C} (α_{1})\{α} ⊆ ↓_{C} (α_{2}). G-minimisation is similar, but uses multi-point substitutions which generate provably equivalent constraint sets.

An S-simplification is a substitution [α_{2}/α_{1}] where either

• α_{1} is an external occurring only positively such that α_{2} ∈ ↑_{C} (α_{1}), and ↑_{C} (α_{1})\{α_{1}} ⊆ ↑_{C} (α_{2})

• α_{1} is an external occurring only negatively such that α_{2} ∈ ↓_{C} (α_{1}), and ↓_{C} (α_{1})\{α_{1}} ⊆ ↓_{C} (α_{2})

In fact, this version of S-simplification is weaker than Fuh and Mishra’s in that it operates on external variables in τ but not in Γ. Fuh and Mishra use a slightly different form of instance relation which allows subtyping on contexts, for example, a term variable bound in Γ with type Nat can be replaced with a binding to Int. Whilst weakening the hypotheses in Γ in this way results in a stronger judgement, on partial derivations (where later we may for example apply f:Nat→Int to x, it is not sound.

Substitution of α_{1} for α_{2} is equivalent to asserting that d(α_{1}, α_{2}) = ⟨1, 1⟩, and it is not hard to see that the form of S-simplification defined here is thus a special cases of H-contraction. In fact G-minimisation can be expressed as a sequence of H-contractions: If θ is a multipoint substitution on C which constitutes a Gminimisation, since θC is provably equivalent to C, it has the same solution set. And since the combined substitution does not reduce the solution set, neither does any of the single point substitutions of which it consists: thus they are all H-contractions.

However, since it operates over a narrower class of posets, we would expect H-contraction to be capable of transformations which cannot be derived by Ssimplification and G-minimisation, and we will demonstrate that this is indeed the case.

3.3 Examples

Simplifying constraint sets is the special case of H-contraction where ζ(α) = ⟨1, 1⟩ for every observable α. Under these circumstances, a H-contraction is just a contraction which does not change the minimal distances between pairs of observables. And if the contraction is to a distance of ⟨1, 1⟩, it is equivalent to a substitution. The following two examples of problem constraint sets are taken from [43].

α  γ  β δ_{3} δ_{2} δ_{1}  α  γ  β

Here α, β, and γ are external and δ_{1}, δ_{2}, and δ_{3} internal. While G-minimisation simplifies the constraint set on the left immediately to that on the right by the multi-point substitution [γ/δ_{1}]∘[γ/δ_{2}]∘[γ/δ_{3}], there is no single-point substitution which satisfies the criteria for being a G-simplification. On the other hand none of the substitutions (applied in any order) [γ/δ_{1}], [γ/δ_{2}], [γ/δ_{3}] change any of the minimal distances between α, β, and γ, so we obtain the simplification as a sequence of H-contractions. Notice that by a different sequence of H-contractions we could obtain (for example) the simplifying substitution [α/δ_{1}] ∘ [α/δ_{2}] ∘ [γ/δ_{3}].

0 α γ β  1 1  γ1

Here the constraint set on the left has external γ, and internals α and β, and base types 0 < 1. It is shown in [43] that this type cannot be simplified by G-minimisation and S-simplification, but neither substitution in the sequence [0/α] ∘ [1/β] changes any of the minimal distances, so both are H-contractions.

Our third example, this time of a constrained type, is max:

                            Real  γ α  β

Here we have

\begin{aligned}ζ(α) & = & ⟨1, 2⟩ \\ ζ(β) & = & ⟨1, 2⟩ \\ ζ(γ) & = & ⟨2, 1⟩ \\ ζ(Real) & = & ⟨1, 1⟩ \end{aligned}

We can verify the requirement for H-contracting d(α, β) from ⟨2, 3⟩ to ⟨2, 1⟩, that for each τ, d(τ, β) ⩽ ζ(τ) + d_{I}(τ, α) + ⟨2, 1⟩.

\begin{matrix}d(α, β) & ⩽ & ⟨1, 2⟩ & + & ⟨1, 1⟩ & + & ⟨2, 1⟩ \\ d(β, β) & ⩽ & ⟨1, 1⟩ & + & ⟨2, 3⟩ & + & ⟨2, 1⟩ \\ d(γ, β) & ⩽ & ⟨1, 2⟩ & + & ⟨2, 1⟩ & + & ⟨2, 1⟩ \\ d(Real, β) & ⩽ & ⟨1, 1⟩ & + & ⟨2, 1⟩ & + & ⟨2, 1⟩ \end{matrix}

So this is a valid H-contaction. An informal explanation of this transformation is that since any solution θ is over a Helly poset, θα and θβ have an upper bound which is a lower bound for γ and Real. If we were to insert a fresh variable ϵ into the constraint set with this property, then we would obtain the substitution [ϵ/α] as an S-simplification. In any case, the remaining simplifying substitutions [α/γ] and [α/β] are just H-contractions equivalent to S-simplifications.

It should be mentioned that it is not always the case that H-contraction can be usefully exploited to simplify types. For example, in the following case, with all variables external and ζ(γ) = ⟨1, 2⟩, ζ(α) = ζ(β) = ζ(δ) = ζ(ϵ) = ⟨1, 1⟩ the constraint set on the left, where γ is internal, can be H-contracted to that on the right, but the number of constraints does not decrease.

α  β γ  δ ϵ  α  β γ  δ ϵ

3.4 Tautness over Smaller Classes

Tautness over Lattices

We will show that over lattices, to obtain tautness, we need only apply the obvious rules:

\frac{I ⊢ d(τ_{1}, τ_{2}) ⩽ e}{I ⊢ d(τ_{1}, τ_{2}) ⩽ ⟨2, 2⟩}  (Truncate)

\frac{I ⊢ d(α, a) ⩽ ⟨1, 2⟩ I ⊢ d(α, b) ⩽ ⟨1, 2⟩}{I ⊢ d(α, a ∧ b) ⩽ ⟨1, 2⟩}  (Inf)
\frac{I ⊢ d(α, a) ⩽ ⟨2, 1⟩ I ⊢ d(α, b) ⩽ ⟨2, 1⟩}{I ⊢ d(α, a ∨ b) ⩽ ⟨2, 1⟩}  (Sup)

3.4.1 Proposition [Soundness & Completeness of Deductive Closure]: If J is the closure of I by Dual, Min, Join, Inf, Sup, and Truncate. then for any lattice Q′ which is a Q-model, θ is a solution of I iff θ is a solution of J.

Proof: Straightforward. □

3.4.2 Proposition [Tautness of Deductive Closure]: Let Q be a lattice, and I be a solvable set of distance inequalities between variables and elements of Q. If I is closed under Dual, Min, Join, Inf, Sup, and Truncate then I is taut over the class of lattices which retract to Q.

Proof: We will proceed by demonstrating that if d_{I}(τ_{1}, τ_{2}) = e, then there is a Q-model Q′ and a solution θ such that d_{Q′}(θτ_{1}, θτ_{2}) = e. We will ensure the equality holds by adding sufficient inequalities to I and demonstrating that the resulting set of inequalities is still distance consistent. We proceed by cases on e:

⟨1, 1⟩ is immediate

⟨1, 2⟩ We must have U(τ_{1}) ⩽ U(τ_{2}), since for every a ⩾ τ_{1} we also have a ⩾ τ_{2}. If U(τ_{1}) < U(τ_{2}) then the distance equality holds exactly in the maximal solution. If U(τ_{1}) = U(τ_{2}), we must have D(τ_{1}) < U(τ_{1}), since otherwise we have τ_{1} ⩾ D(τ_{1}) = U(τ_{1}) ⩾ τ_{2}, contradicting the assumption that d_{I}(τ_{1}, τ_{2}) = ⟨1, 2⟩. Thus we can choose some a such that D(τ_{1}) ⩽ a < U(τ_{1}), and set I′ = I ∪ {τ_{1} ⩽ a}. Now if I′ is not consistent, there must be some chain not in I which is not in I, and thus must include d(τ, a) ⩽ ⟨1, 2⟩ as a link; indeed the shortest such chain must start or end with such a link. Suppose the latter. Then we must have some b ⩽ τ_{1} such that b ⩽̸ a, which cannot occur since D(τ_{1}) ⩽ a. Now the required inequality holds in the maximal solution of I′.

⟨2, 1⟩ is dual to the above case

⟨2, 2⟩ Here we cannot have U(τ_{1}) ⩽ D(τ_{2}) or U(τ_{2}) ⩽ D(τ_{1}) by the assumption that d_{I}(τ_{1}, τ_{2}) = ⟨2, 2⟩. We set Q′ = Q ∪ {c_{1}, c_{2}} with the inequalities

D(τ_{i}) < c_{i} < U(τ_{i})

It is straightforward that this is a lattice and a Q-model via the retraction r(c_{i}) = U(τ_{i}), and that d_{Q′}(c_{1}, c_{2}) = ⟨2, 2⟩. By a similar argument to the previous case

I ∪ {d(τ_{1}, c_{1}) = ⟨1, 1⟩, d(τ_{2}, c_{2}) = ⟨1, 1⟩}

is distance consistent over Q′, and so there is a solution θ such that d_{Q′}(θτ_{1}, θτ_{2}) = ⟨2, 2⟩

Tautness over Trees

Trees have two interesting features from the point of view of computing tautenings: firstly, every tree contains a top element, hence the minimal distance between two elements is at most ⟨2, 3⟩. Secondly, any two elements in a tree which share a lower bound are comparable: i.e. if d(α, β) ⩽ ⟨3, 2⟩, then for any solution θ either θα ⩽ θβ or θα ⩾ θβ. So if we have the following situation:

                         a              b c α

where a and b have no common lower bound, we may deduce that since α is comparable with both a and b, we must have α ⩾ c.

It turns out that these two observations are sufficient to characterise trees. We introduce two rules which formalise these observations.

\frac{I ⊢ d(τ_{1}, τ_{2}) ⩽ e}{I ⊢ d(τ_{1}, τ_{2}) ⩽ ⟨2, 3⟩}  (Truncate)

\frac{I ⊢ d(τ, a) ⩽ ⟨3, 2⟩ I ⊢ d(τ, b) ⩽ ⟨3, 2⟩ a, b have no common lower bound}{I ⊢ d(τ, a ∨ b) ⩽ ⟨2, 1⟩}  (NoInf)

3.4.3 Proposition [Soundness & Completeness of Deductive Closure]: Let Q be a tree, and I some set of distance inequalities over variables and elements of Q. Suppose I′ is the deductive closure of I under Dual, Join, Min, Truncate, and NoInf. Then for any tree Q′ which is a Q-model, I′ and I′ are equivalent over Q′.

Proof: For every pair of variables or base types, the minimal distance from one to the other will be at least as short in I′ as in I. So if a substitution satisfies I′, it also satisfies I.

Conversely let θ be a solution of I over Q′. It is straightforward that the solution satisfies those distance inequalities generated by Truncate and NoInf.

□

3.4.4 Proposition [Tautness of Deductive Closure]: Suppose I is a set of distance inequalities between variables and atomic types over a tree Q. If I is deductively closed under Dual, Join, Min, Truncate, and NoInf, then I is taut over the class of trees Q′ which are Q-models.

Proof: Suppose I is closed under these rules, τ_{1} and τ_{2} are types occurring in I, and d_{I}(τ_{1}, τ_{2}) ⩽ e. We will demonstrate tautness by adding suitable inequalities to I and choosing a suitable extension of Q to ensure either than this distance holds exactly, or in the case where e is ⟨2, 2⟩, that both the distances ⟨1, 2⟩ and ⟨2, 1⟩ can be made to hold exactly. We will proceed by cases on e: note that over a tree the minimal distance between two elements is at most ⟨2, 3⟩.

⟨1, 1⟩ Any substitution θ which solves I must have θτ_{1} = θτ_{2}.

⟨1, 2⟩ It suffices to find a solution θ such that θτ_{1} < θτ_{2}. Let b_{0} = U(τ_{2}). Note that I ⊬ τ_{1} ⩾ b_{0}, since otherwise we would have I ⊢ d(τ_{1}, τ_{2}) = ⟨1, 1⟩ by Min. There is at most one child b_{1} of b_{0} such that d(τ_{1}, b_{1}) ⩽ ⟨3, 2⟩, since if there were more than one we would have d(τ_{1}, b_{0}) ⩽ ⟨2, 1⟩ by NoInf. If such a b_{1} exists, we set Q′ = Q, else Q′ = Q ∪ {b_{1} ⩽ b_{0}} for b_{1} fresh, and in either case set I′ = I ∪ {τ_{1} ⩽ b_{1}}. We need to show I′ is solvable over Q′, which we will derive from the distance consistency of I. Suppose we have d_{I′}(c, d) < d_{I}(c, d). By induction on a minimal chain in I′ from c to d we must have a chain from b_{1} to either c (we will suppose c) or d which is shorter in I′ than in I, whose first link is from b_{1} to τ_{1} (and thus of length ⟨2, 1⟩), and whose other links are all in I. Thus it will suffice to show that for any c, d_{I′}(b_{1}, c) = d_{I}(b_{1}, c). We will proceed by cases:

• if c ⩾ b_{0} we have d_{I}(τ_{1}, c) = d_{I}(b_{1}, c) = ⟨1, 2⟩. And ⟨2, 1⟩ + ⟨1, 2⟩ ⩾

⟨1, 2⟩.

• if c ⩽ b_{1}, then d_{I}(b_{1}, c) is either ⟨1, 1⟩ or ⟨2, 1⟩, and in either case

⟨2, 1⟩ + d_{I}(τ_{1}, c) ⩾ d_{I}(b_{1}, c).

• if c is incomparable with b_{0}, then we cannot have d_{I}(τ_{1}, c) ⩽ ⟨2, 3⟩, since then, as we have I ⊢ d(τ_{1}, b_{0}) ⩽ ⟨3, 2⟩, we would have I ⊢ τ_{1} > b_{0} ∧ c by NoInf. Thus the minimal distance in I′ from b_{1} to c via τ_{1} is at least ⟨2, 3⟩.

• if c < b_{0} but c ≮ b_{1} and d_{I}(τ_{1}, c) ⩽ ⟨3, 2⟩ we have I ⊢ τ_{1} > b_{0} by NoInf. Thus again the minimal distance in I′ from b_{1} to c via τ_{1} is at least ⟨2, 3⟩.

In all cases we have d_{I′}(b_{1}, c) = d_{I}(b_{1}, c).

⟨2, 1⟩ If d(τ_{1}, τ_{2}) = ⟨2, 1⟩, then d(τ_{1}, τ_{2}) = ⟨1, 2⟩, so the construction is as above.

⟨2, 2⟩ Notice it is impossible to produce a solution in which d_{Q}(θτ_{1}, θτ_{2}) = ⟨2, 2⟩. Instead we demonstrate the existence of solutions in which d_{Q}(θτ_{1}, θτ_{2}) = ⟨1, 2⟩ and d_{Q}(θτ_{1}, θτ_{2}) = ⟨2, 1⟩. Again we define b_{0} = U(τ_{2}), and again we cannot have I ⊢ τ_{1} ⩾ b_{0}, since otherwise we would have I ⊢ d(τ_{1}, τ_{2}) ⩽ ⟨2, 1⟩ by Join. Now if I′ is as before, its distance consistency follows by exactly the same argument as the ⟨1, 2⟩ case.

⟨2, 3⟩ (i.e. θτ_{1}, θτ_{2} are incomparable). We define

b_{0} = ∧{b | d_{I}(b, τ_{1}) ⩽ ⟨3, 2⟩ and d_{I}(b, τ_{2}) ⩽ ⟨3, 2⟩}

This set is non-empty since ⊤ is in the set, and down-closed: if there are incomparable c_{1} and c_{2}, then for each of d_{I}(c_{1}, τ_{1}) ⩽ ⟨3, 2⟩ and d_{I}(c_{2}, τ_{1}) ⩽ ⟨3, 2⟩ so τ_{1} ⩾ c_{1} ∧ c_{2} by NoInf, which together with τ_{1} ⩽ c_{1} and τ_{1} ⩽ c_{2} implies c_{1} = c_{2}. so the two cannot be incomparable. Again we cannot have I ⊢ τ_{1} ⩾ b_{0} or I ⊢ τ_{2} ⩾ b_{0}. Now there is at most one child b_{1} of b_{0} satisfying d_{I}(b_{1}, τ_{1}) ⩽ ⟨2, 3⟩, and one satisfying d_{I}(b_{2}, τ_{2}) ⩽ ⟨2, 3⟩, if either of these children do not exist we set Q′ = Q ∪ {b_{1}, b_{2}} (as required) and I′ = I ∪ {τ_{1} ⩽ b_{1}, τ_{2} ⩽ b_{2}}. Note that b_{1} and b_{2} are guaranteed distinct. Again we can show the distance consistency of I′ from that of I: if I is not distance consistent there must be some down-fence from b_{1} (or b_{2}) via τ_{1} (or τ_{2}) which is shorter in I′ than I and whose other links are all in I. Again the reasoning is essentially the same as the ⟨1, 2⟩ case for each chain from b_{1} via τ_{1} or b_{2} via τ_{2}. □

3.4.5 Remark: In fact, the proof of tautness only requires the ability to extend trees at their leaf nodes. Since tautness over any class of posets implies tautness over any superclass, the result is equally applicable to tautness over the class of trees generated by adding new children to existing nodes, for example single inheritance hierarchies.

3.5 Conclusion

The technique presented here can be seen as specialising techniques for simplification over the class of all partial orders such as those of [18], to Helly posets. We have seen that our system is more powerful, and the restriction on possible orderings seems a reasonable tradeoff for better simplification. It would be interesting to investigate further properties of contraction, particularly in respect to completeness and confluence.

Since we have a theory about entailment relations over particular classes of posets such as lattices and trees, it should be possible to design more powerful simplification algorithms which exploit the fact that these relations verify more equivalences.

Although we envisage simplification occurring only prior to let-bindings, an additional refinement might be to simplify previous let-bound types as more information about distances between common λ-bound variables is discovered.

Solvability and Solution

Although we have seen how systems of constraints can be tested for solvability and solved over atomic Helly posets, in practice this is not a realistic approach to constraint sets obtained by type inference, which may well contain constraints of the form e.g. α ⩽ τ_{1}→τ_{2}. Although if we are dealing with finite types and structural subtyping it is possible (subject to a simple unification-like check for circularity) to reduce the constraint set to atomic form with substitutions of the form e.g. [α_{1}→α_{2}/τ], this results in a potentially exponential increase in the number of terms in the constraint set, and moreover when working with nonstructural subtyping the reduction is less straightforward, and in the case of regular types, is not available at all.

In this chapter we will consider the issues of working with posets of regular types built with type constructors from some base Helly poset. We will generalise the notion of simulation to allow us to test the distance relation in such orderings, and show how sets of constraints over the orderings may be efficiently tested for solvability and solved. We will treat base types as constructors of arity 0 throughout.

4.1 Decomposition and Simulation

In chapter 1 it was shown how the subtype relation on regular types can be characterised as a maximal simulation: that is, the maximal relation consistent with ⊑ and closed under certain propagation rules. Thus to test the subtype relation between two types, it suffices to find a simulation between them. The simulation which justifies the ⩽ relation has two components: a shallow approximation relation between type constructors, and rules for propagating the simulation to the constructor arguments. The propagated inequalities must be necessary for the original inequality to hold, and, together with the shallow approximation, sufficient. We can demonstrate similar properties for the distance relation.

It is straightforward to construct from our shallow approximation ⊑ on constructors a shallow approximation of the distance relation:

d_{Q}(c, c)  = ⟨1, 1⟩ for any constructor c
d_{Q}(a, b)  = d_{Q}(a, b) for all a, b ∈ Q
d_{Q}({}_{I}, {}_{J}) = ⟨1, 2⟩ if I ⊃ J
d_{Q}({}_{I}, {}_{J}) = ⟨2, 1⟩ if I ⊂ J
d_{Q}({}_{I}, {}_{J}) = ⟨2, 2⟩ otherwise
d_{Q}(c, ⊤)  = ⟨1, 2⟩ for any c ≠ ⊤
d_{Q}(⊥, c)  = ⟨1, 2⟩ for any c ≠ ⊥

and in order to propagate the constructor inequalities to their arguments, we introduce the following notion:

4.1.1 Definition: An ordering is decomposable if for any τ = c_{1}τ_{1} … τ_{m} and ρ = c_{2}ρ_{1} … ρ_{n} (perhaps containing free variables) and any e ∈ L such that d(c_{1}, c_{2}) ⩽ e, there is a decomposition set D(ρ, τ, e) where every element of D is of the form (ρ′, τ′, e′) and such that

• every ρ′ is either one of the ρ_{i} or a ground type, and every τ′ is either one of the τ_{i} or a ground type

• each τ_{i} and ρ_{i} occurs at most once, and if both occur, then they occur in the same tuple

• for any substitution θ on the variables in ρ and τ, d_{Q}(θρ, θτ) ⩽ e iff d_{Q}(θρ′, θτ′) ⩽ e′ for every (ρ′, τ′, e′) ∈ D(ρ, τ, e).

If τ_{1} and τ_{2} are both ground types, then any substitution is just the identity on τ_{1} and τ_{2}. So then

• if d(τ_{1}, τ_{2}) ⩽ e, each of the distance inequalities in D(τ_{1}, τ_{2}, e) holds

• if d(ν(τ_{1}), ν(τ_{2})) ⩽ e then d(τ_{1}, τ_{2}) ⩽ e if each of the distance inequalities in D(τ_{1}, τ_{2}, e) holds.

4.1.2 Definition: A set S of tuples (τ_{1}, τ_{2}, e) is closed under decomposition if for any (τ_{1}, τ_{2}, e) ∈ Z, d(ν(τ_{1}), ν(τ_{2})) ⩽ e and D(τ_{1}, τ_{2}, e) ⊆ Z. For e_{0} ∈ L a e_{0}-simulation Z(τ_{1}, τ_{2}, e_{0}) between two regular types τ_{1} and τ_{2} is a finite set of tuples containing (τ_{1}, τ_{2}, e_{0}) which is closed under decomposition.

We will use graph representations of types to build simulations, and read the existence of a vertex ((G_{1}, r_{1}), (G_{2}, r_{2}), e) in the simulation as a requirement that d_{Q}((G_{1}, m), (G_{2}, n)) ⩽ e. It is easy to see that this notion of simulation corresponds with the definition for the ⩽ relation in the case e = ⟨1, 2⟩, where ((G_{1}, m), (G_{2}, n), ⟨1, 2⟩) corresponds with (G_{1}, m) ⊑ (G_{2}, n) and ((G_{1}, m), (G_{2}, n), ⟨2, 1⟩) with (G_{2}, n) ⊑ (G_{1}, m)

Structural Constructors

Let Q_{0} be Helly a poset of base types, and Q built from Q_{0} with structural constructors. Suppose we have a distance inequality d(Tτ_{1} … τ_{n}, Tτ′_{1} … τ′_{n}) ⩽ e. Since constructor types are only comparable with other types headed by the same constructor, the points making up the fences which generate the distance e must also be of the form Tρ_{1} … ρ_{n}. So there is an up-fence in Q from Tτ_{1} … τ_{n} to Tτ′_{1} … τ′_{n} of length r exactly if

• for each i such that p_{i}^{T} = pos, there is an up-fence in Q from τ_{i} to τ′_{i} of length n.

• for each i such that p_{i}^{T} = neg, there is a down-fence in Q from τ_{i} to τ′_{i} of length n

• for each i such that p_{i}^{T} = mix, τ_{i} = τ′_{i}.

Thus

D(Tτ_{1} … τ_{k}, Tτ′_{1} … τ′_{k}, e) = {(τ_{i}, τ′_{i}, e) | p_{T}^{i} = pos}
∪ {(τ_{i}, τ′_{i}, e^{∗}) | p_{T}^{i} = neg}
∪ {(τ_{i}, τ′_{i}, ⟨1, 1⟩) | p_{T}^{i} = mix}

4.1.3 Proposition: Let Q be the order defined by the base types in Q_{0} and the →-constructor rule. There is a simulation Z(τ_{1}, τ_{2}, e_{0}) iff d_{Q}(τ_{1}, τ_{2}) ⩽ e_{0}.

Proof: Suppose d_{Q}(τ_{1}, τ_{2}) ⩽ e_{0}, and (G_{1}, r_{1}) and (G_{2}, r_{2}) are graphs of τ_{1} and τ_{2}. Let Z be the minimal set containing ((G_{1}, r_{1}), (G_{2}, r_{2}), e_{0}), and for every ((G_{1}, m), (G_{2}, n), e) ∈ Z with ν(m) = ν(n) = → ((G_{1}, m(1)), (G_{2}, m(1)), e^{∗}) ∈ Z, and ((G_{1}, m(2)), (G_{2}, m(2)), e) ∈ Z, i.e. the minimal closure of {((G_{1}, r_{1}), (G_{2}, r_{2}), e_{0})} under decomposition. It is straightforward to demonstrate that Z is a simulation.

Conversely, suppose there is a simulation Z. In order to prove that for every ((G_{1}, m), (G_{2}, n), e) ∈ Z, d((G_{1}, m), (G_{2}, n)) ⩽ e, we will construct types on the up-fences and down-fences which make up this distance. For every vertex z = ((G_{1}, m), (G_{2}, n), ⟨u, d⟩) of Z we define an up-fence U(z) and a down-fence D(z) of length u and d respectively, labelled with type constructors as follows:

• if ν(m) = ν(n) = →, then ν(U(z)(i)) = ν(D(z)(i)) = → for all i

• if ν(m) = a, ν(n) = b, so that d_{Q}(a, b) ⩽ ⟨u, d⟩ and there is an up-fence of length u and a down fence of length d from a to b, then ν(U(z)(i)) is the ith point on the up-fence, and ν(D(z)(i)) the ith point on the down-fence.

So for any z ∈ Z, if i is even, then ν(U(x)(i)) ⊑ ν(U(x)(i + 1)) for any x and ν(D(x)(i+1)) ⊑ ν(D(x)(i)), and similarly if i is odd, ν(U(x)(i+1)) ⊑ ν(U(x)(i)) and ν(D(x)(i)) ⊑ ν(D(x)(i + 1)). We add transitions

\begin{matrix}U(m, n, e)(i) & →^{1} & D(m(1), n(1), e^{∗})(i) \\ U(m, n, e)(i) & →^{2} & U(m(2), n(2), e)(i) \\ D(m, n, e)(i) & →^{1} & U(m(1), n(1), e^{∗})(i) \\ D(m, n, e)(i) & →^{2} & D(m(1), n(1), e)(i) \end{matrix}

to form a transition graph G_{Z}. Now we can define the fences which make up the distance e_{0} from τ_{1} to τ_{2}. Let z_{0} = ((G_{1}, r_{1}), r_{2}, e_{0}). Let u_{i} = U(z_{0})(i), and d_{i} = D(z_{0})(i). Then it is easy to show by induction that if i is even, there is a simulation between the graphs (G_{Z}, u_{i}) and (G_{Z}, u_{i+1}), and if odd, between (G_{Z}, u_{i+1}) and (G_{Z}, u_{i}), and dually for the points d_{i}. Notice that

\begin{aligned}(G_{Z}, U(z_{0})(0)) & = & (G_{Z}, D(z_{0})(0)) & ≅ & (G_{1}, r_{1}) \\ (G_{Z}, U(z_{0})(u)) & = & (G_{Z}, D(z_{0})(d)) & ≅ & (G_{2}, r_{2}) \end{aligned}

and so the graphs rooted at (G_{Z}, u_{i}) and (G_{Z}, d_{i}) form the required up- and downfences to generate the distance e_{0}. □

4.1.4 Example: In order to illustrate this construction, we will take a simple case the underlying order Q given by

                        a       b d c

and the inequality d(b→a, c→d) ⩽ ⟨2, 2⟩. Defining

x = (b→a, c→d, ⟨2, 2⟩)
y = (b, c, ⟨2, 2⟩)
z = (a, d, ⟨2, 2⟩)

the simulation is simply {x, y, z} and we take

U(x) = →, →, →
D(x) = →, →, →
U(y) = b, d, c  an up-fence from b to c
D(y) = b, a, c  a down-fence from b to c
U(z) = a, d, d  an up-fence from a to d
D(z) = a, a, d  a down-fence from a to d

d → a b → a                                  c → d a → d D(y)  a  D(z) a U(y)  d  U(z) d

(solid lines are inequalities, dashed lines are inferred fences, dotted lines indicate the source of the derived types). Observe that since → is contravariant in its first argument, the first argument of the type of U(x)(1) comes from D(y)(1), and the first argument of D(x)(1) is drawn from U(y)(1). Then the point u_{1} on the up-fence from b→a to c→d is D(y)(1) → U(z)(1) = a→d, and the point d_{1} on the down-fence is U(y)(1) → D(z)(1) = d→a.

Record Types

If we add a record constructor, the situation becomes more interesting. When e < ⟨2, 2⟩, for any d({}_{I}.{}_{J}) ⩽ e we inherit from the rules for the ⩽-constructor,

D({l_{i}:τ_{i}}_{i∈I}, {l_{j}:τ′_{j}}_{j∈J}, e) = {(τ_{j}, τ′_{j}, e) | j ∈ I ∩ J}

For longer distances, notice that any two records have an upper bound, so that the decomposition set for e ⩾ ⟨2, 3⟩ is empty. This leaves the cases e = ⟨2, 2⟩ and e = ⟨3, 2⟩. If two record types have a common lower bound, then the subterms for any common label must also have a lower bound, e.g suppose we have the distance inequality d({l:τ_{1}, m:τ_{2}}, {l:τ′_{1}, n:τ′_{2}}) ⩽ ⟨2, 2⟩, then the two records have a lower bound. Now any lower bound must have fields labelled l, m, and n, and the type of the l field must be a common lower bound for τ_{1} and τ′_{1}, so we require d(τ_{1}, τ′_{1}) ⩽ ⟨3, 2⟩. Observe that no such deductions can be made about τ_{2} and τ′_{2}. Thus if ⟨2, 2⟩ ⩽ e ⩽ ⟨3, 2⟩,

D({l_{i}:τ_{i}}_{i∈I}, {l_{j}:τ′_{j}}_{j∈J}, e) = {(τ_{i}, τ′_{i}, ⟨3, 2⟩) | i ∈ I ∩ J}

4.1.5 Proposition: Let Q be the order defined by the base types in Q_{0} and the → and {} constructor rules. If there is a simulation Z(τ_{1}, τ_{2}, e_{0}), then d_{Q}(τ_{1}, τ_{2}) ⩽ e_{0}.

Proof: The proof extends the proof for → in a fairly straightforward fashion. We define U and D for vertices (m, n, e) where ν(m) = {}_{I} and ν(n) = {}_{J} and e = ⟨u, d⟩ as follows:

u = 1 U(m, n, e) = ({}_{I}, {}_{J})
u ⩾ 2 U(m, n, e) = ({}_{I}, {}, {}, … {}, {}_{J})  u even
({}_{I}, {}, {}, … {}, {}_{J}, {}_{J}) u odd
d = 1 D(m, n, e) = ({}_{I}, {}_{J})
d = 2 D(m, n, e) = ({}_{I}, {}_{I∪J}, {}_{J})
d ⩾ 3 D(m, n, e) = ({}_{I}, {}_{I}, {}, … {}, {}_{J}, {}_{J}) d even
({}_{I}, {}, {}, … {}, {}_{J})  d odd

observe that U(m, n, ⟨u, d⟩) is an up-fence of constructors of length u, and D(m, n, ⟨u, d⟩) is an up-fence of constructors of length d. There is also a further necessary extension of the construction: suppose we have d((G_{1}, r_{1}), (G_{2}, r_{2})) ⩽ ⟨3, 4⟩, where r_{1} = {}_{I}, and r_{2} = {}_{J}. Then the simulation is just {(G_{1}, r_{1}), (G_{2}, r_{2}), ⟨3, 4⟩}. D(z_{0}) is then

{}_{I} {}^{I} {} {}^{J} {}_{J}

when we build the down fence between (G_{1}, r_{1}) and (G_{2}, r_{2}), the constructors {}_{I} and {}_{J} must be given out-edges, which unlike the case of → cannot be constructed from the fences between constructor arguments, because the constructor arguments do not occur in the simulation. In fact, the distance inequality will hold irrespective of the arguments we use, so the only constraints upon their construction are the boundary conditions, that

\begin{aligned}(G_{Z}, U(z_{0})(0)) & = & (G_{Z}, D(z_{0})(0)) & ≅ & (G_{1}, r_{1}) \\ (G_{Z}, U(z_{0})(u)) & = & (G_{Z}, D(z_{0})(d)) & ≅ & (G_{2}, r_{2}) \end{aligned}

The obvious solution is simply to augment the construction with the argument vertices in the graphs G_{1} and G_{2} themselves. In this case, for each i ∈ I, we will have D(z_{0})(0) →^{i} (r_{1}(i)) D(z_{0})(1) →^{i} (r_{1}(i)) and for each j ∈ J, D(z_{0})(3) →^{j} (r_{2}(j)) and D(z_{0})(4) →^{j} (r_{2}(j)), and the constructed types now form the required down-fence.

It remains to define the transitions which generate the simulations. Suppose we have a vertex (m, n, ⟨u, d⟩) ∈ Z

u = 1 U(m, n, e)(0)  →^{l} U(m(l), n(l), e)(0) for each l ∈ M
U(m, n, e)(1)  →^{l} U(m(l), n(l), e)(1) for each l ∈ N
u ⩾ 2 U(m, n, e)(0)  →^{l} m(l)  for each l ∈ M
U(m, n, e)(u − 1) →^{l} n(l)  for each l ∈ N, if u odd
U(m, n, e)(u)  →^{l} n(l)  for each l ∈ N
d = 1 D(m, n, e)(0)  →^{l} D(m(l), n(l), e)(0) for each l ∈ M
D(m, n, e)(1)  →^{l} D(m(l), n(l), e)(1) for each l ∈ N
d = 2 D(m, n, e)(0)  →^{l} D(m(l), n(l), e)(0) for each l ∈ M
D(m, n, e)(1)  →^{l} D(m(l), n(l), e)(0) for each l ∈ M − N
→^{l} D(m(l), n(l), e)(1) for each l ∈ M ∩ N
→^{l} D(m(l), n(l), e)(2) for each l ∈ N − M
D(m, n, e)(2)  →^{l} D(m(l), n(l), e)(2) for each l ∈ N
d ⩾ 3 D(m, n, e)(0)  →^{l} m(l)  for each l ∈ M
D(m, n, e)(1)  →^{l} m(l)  for each l ∈ M
D(m, n, e)(d − 1) →^{l} n(l)  for each l ∈ N, if d even
D(m, n, e)(d)  →^{l} n(l)  for each l ∈ N

The rest of the proof goes through as before. □

⊤ and ⊥

Introducing ⊤ and ⊥ into the subtyping order has a more wide-ranging effect. Now any two types are an upper and a lower bound (⊤ and ⊥ respectively) and so the decomposition set for any inequality of distance ⟨2, 2⟩ or greater is empty. For smaller distances the decomposition sets are unchanged. It is quite straightforward to extend propositions(4.1.3) and (4.1.5) to deal with these constructors.

Nonatomic base types

We can also use this technique to deal with nonatomic base types. We will consider the case of → constructors only. Suppose we have an inequality of the form d(a, τ_{1}→τ_{2}) ⩽ e, where Σ^{⇑}(a) is an →-type, and suppose the minimal distance from a to τ_{1}→τ_{2} is ⟨u, d⟩. Since any fence from a to τ_{1}→τ_{2} must pass through Σ^{⇑}(a), the shortest down-fence of length d must have the form a ⩾ a′ ⩽ τ′_{1}→τ′_{2} ⩾ … for some a′ ⩽ a, where Σ^{⇑}(a) ⩽ τ′_{1}→τ′_{2}. So there will be an up-fence Σ^{⇑}(a) ⩽ τ′_{1}→τ′_{2} ⩾ … from Σ^{⇑}(a) to τ_{1}→τ_{2} of length d − 1. Thus we know that d(a, τ_{1}→τ_{2}) ⩽ e iff d(Σ^{⇑}(a), τ_{1}→τ_{2}) ⩽ e^{△}, and so D(a, τ, e) = D(Σ^{⇑}(a), τ, e^{△}).

So if a is a non-atomic base type and c is not a base type, our shallow approximation extends as follows:

\begin{aligned}d(a, c) & = & d(ν(Σ^{⇑}(a)), c)^{△} \\ d(c, a) & = & d(c, ν(Σ^{⇑}(a)))^{▲} \end{aligned}

And our decomposition sets, if Σ^{⇑}(a) = ρ_{1}→ρ_{2} are

\begin{aligned}D(a, τ_{1}→τ_{2}, e) & = & D(Σ^{⇑}(a), τ_{1}→τ_{2}, e^{△}) & = & \{(ρ_{1}, τ_{1}, (e^{△})^{∗}), (ρ_{2}, τ_{2}, e^{△})\} \\ D(τ_{1}→τ_{2}, a, e) & = & D(τ_{1}→τ_{2}, Σ^{⇑}(a), e^{▲}) & = & \{(τ_{1}, ρ_{1}, (e^{▲})^{∗}), (τ_{2}, ρ_{2}, e^{▲})\} \end{aligned}

4.2 Solvability and Solution

Closure and Consistency

Let Q be a decomposable ordering. A distance inequality d(τ_{1}, τ_{2}) ⩽ e possibly containing type variables is Q-consistent if at least one of τ_{1} or τ_{2} is a variable, or d(ν(c_{1}), ν(c_{2})) ⩽ e. A set of distance inequalities is Q-consistent if every member is Q-consistent.

4.2.1 Definition [Closure]: If Q is decomposable ordering, I is Q-closed if I is closed under Dual, Min, and Join, the rule Truncate

\frac{d(ρ, τ) ⩽ e}{d(ρ, τ) ⩽ e ∧ δ_{Q}}  (Truncate)

and the rule Decomp

\frac{d(ρ, τ) ⩽ e d_{Q}(ν(ρ), ν(τ)) ⩽ e (ρ′, τ′, e′) ∈ D(ρ, τ, e)}{d(ρ′, τ′) ⩽ e′} (Decomp)

The Q-closure of I close_{Q}(I), is the minimal Q-closed superset of I.

We include the rule Truncate because it ensures that the closure is finite, since it can only contain types occurring in I and distances at most δ_{Q}.

4.2.2 Proposition: Let Q be a decomposable partial order of diameter δ_{Q}, I a set of distance inequalities, and J be the Q-closure of I. Then any solution of I is a solution of J and vice versa.

Proof: Let θ be a solution of J. Since for any τ_{1} and τ_{2}, d_{J} (τ_{1}, τ_{2}) ⩽ d_{I}(τ_{2}, τ_{2}), θ is a solution of I. Suppose conversely that θ is a solution of I. We proceed by induction on the height of the derivation an inequality d_{Q}(θτ_{1}, θτ_{2}) ⩽ e in J. The last rule in deriving this from the distance inequalities in I is either Min, Dual, Join, Truncate, or Decomp. For each rule if θ satisfies the premises, then θ also satisfies the conclusion. Thus since θ is a solution of I over Q, it is a solution of J. □

Clearly if the Q-closure of I is inconsistent, then I is unsolvable. Our fundamental result will be that under certain quite general conditions, a set of constraints is solvable iff its Q-closure is Q-consistent. Before we prove this result, a few examples will demonstrate the use of distance closure, particularly its advantage over the usual notion of constraint closure.

With structural subtyping over the tree order Long < Int, Nat < Int, the constraint set {Nat→Nat ⩽ α, Long→Long ⩽ α} is unsolvable even though transitive and subterm closure of constraints yields no inconsistencies. Distance closure enables us to deduce d(Long, Nat) ⩽ ⟨3, 2⟩:

d(Nat→Nat, α) ⩽ ⟨1, 2⟩  d(Long→Long, α) ⩽ ⟨1, 2⟩ d(α, Long→Long) ⩽ ⟨2, 1⟩

                   d(Nat→Nat, Long→Long) ⩽ ⟨2, 3⟩

d(Nat, Long) ⩽ ⟨3, 2⟩

Note that although d(Long, Nat) ⩽ ⟨3, 2⟩ is enough to demonstrate inconsistency, we can deduce the stronger inequality d(Long, Nat) ⩽ ⟨2, 2⟩. Similarly from {{l:a} ⩾ α, {l:b} ⩾ α} we can deduce d(a, b) ⩽ ⟨3, 2⟩. The reason that constraint closure is sufficient over a lattice is that in a lattice the only distances of any interest are ⟨1, 1⟩, ⟨1, 2⟩, and ⟨2, 1⟩. All elements in a component are at most ⟨2, 2⟩ apart, so beyond limiting a type to a particular component, a distance inequality of length ⟨2, 2⟩ or greater provides no information. Thus if the ordering is just one big lattice, or if shape matching can be used to decompose the constraint set into separate components, all remaining information can be expressed as constraints.

Constructing a Solution

In this section we will demonstrate a quite general method for constructing solutions of constraint sets over subtype orderings of regular types. Our technique can be seen as an extension of that of [33], using distance inequalities instead of constraints.

We will consider first a simple example: an ordering formed by → over the poset Q = {Long⩽Int, Nat⩽Int}. Consider how we might go about constructing a solution to the constraint set

C = {α⩽γ→Nat, α⩽β, Long→Long⩽β, γ⩽Nat}

I, the closure of C has two components, one of which is

                       Long→Long β α γ→Nat

It’s easy to see that α and β must be → types (clearly they cannot be atomic types), say α_{1}→α_{2} and β_{1}→β_{2}. Propagating the above constraints to the constructor arguments we obtain

         γ α_{1} β^{1} Long               ^{Nat α^{2} β_{2} Long}

Combining these with the inequalities in Q_{0} and the inequality γ⩽Nat (and with a little cosmetic reshaping of the diagrams) we obtain:

Long  β^{1} α_{1} Int γ  Nat                  ^{Int Long β_{2} α^{2}  Nat}

Applying the HSF f(α) = ⋀{a | a ⩾ α} of proposition(2.4.3), we obtain the solution

{α_{1} = Int, α_{2} = Nat, β_{1} = Long, β_{2} = Int, γ = Nat}

Notice however, that we could have solved for each of α, and β, independently using distance inequalities: the points labelled with other variables become “anonymous”. For example, we can obtain

d(α, Long→Long) ⩽ ⟨2, 3⟩
d(α, γ→Nat)  ⩽ ⟨1, 2⟩

by distance closure from the original constraint set, then having determined the substitution [α_{1}→α_{2}/α], we obtain

\begin{matrix}d(α_{1}, γ) & ⩽ & ⟨2, 1⟩ \\ d(α_{1}, Long) & ⩽ & ⟨3, 2⟩ \end{matrix}

then by closure with the original constraint set and Q_{0} we obtain

d(α_{1}, Nat) = ⟨3, 2⟩
d(α_{1}, Long) = ⟨3, 2⟩
d(α_{1}, Int) = ⟨3, 2⟩

whence we can apply the HSF. We can consider the process of solving for a variable α to have three steps:

choice decide on a constructor c for a variable (e.g. α) using the minimal distances from α to all the constructor types.

decomposition generate a set of inequalities on each argument to c, using the decomposition rules

I-expansion close this set of inequalities with those in I to obtain the full set of conditions on each argument

If we are going to solve for α, β, and γ independently, we need to find a function g for choosing constructors which ensure that the types we build do indeed form a solution. In particular, if we have two variables α and β such that e.g. d(α, β) ⩽ ⟨1, 2⟩, we require that g(α) ⊑ g(β), and that we generate sufficient conditions on the arguments of g(α) and g(β) to ensure that we will obtain a simulation, e.g. if g(α) = g(β) = →, we shall want to ensure that d(α_{1}, β_{1}) ⩽ ⟨2, 1⟩, and d(α_{2}, β_{2}) ⩽ ⟨1, 2⟩.

We identify three important concepts in deriving a solution for some set of distance inequalities I

1. A Q-state S expresses the minimal distance between some unknown and a set of types in I.

2. A choice function g defines the constructor for a particular state.

3. Given a state S and g(S), for each label l ∈ L(g(S)) the l-successor state for S at l is obtained as the I-expansion of the inequalities derived using the decomposition rules.

4.2.3 Definition [States]: An state is a partial function

S : τ → {e ∈ L | e ⩽ δ_{Q}}

If P is any set of pairs {(τ, e)}, the Q-reduction of P is the state

red(P)(τ) = δ_{Q} ∧ ⋀{e′ | (τ, e′) ∈ P } if (τ, e′) ∈ P for some e′

Similarly the Q-reduction of a state S is the state

red(S)(τ) = δ_{Q} ∧ S(τ) if S(τ)↓

The binary closure B(S) of a state S is the set of inequalities

{d(τ_{1}, τ_{2}) ⩽ S(τ_{1})^{−1} + S(τ_{2}) | τ_{1}, τ_{2} ∈ dom(S)}

S is Q-consistent if B(S) is Q-consistent. We define a relation ≲ on states: S_{1} ≲ S_{2} if dom(S_{1}) = dom(S_{2}), and

• S_{1}(τ) ⩽ δ_{Q} ∧ (⟨1, 2⟩ + S_{2}(τ))

• S_{2}(τ) ⩽ δ_{Q} ∧ (⟨2, 1⟩ + S_{1}(τ))

4.2.4 Example: Returning to our earlier example, if S_{α} and S_{β} are the initial states for α and β, we have

S_{α}(γ→Nat)  = ⟨1, 2⟩  S_{β}(γ→Nat)  = ⟨3, 2⟩
S_{α}(α)  = ⟨1, 1⟩  S_{β}(α)  = ⟨2, 1⟩
S_{α}(β)  = ⟨1, 2⟩  S_{β}(β)  = ⟨1, 1⟩
S_{α}(Long→Long) = ⟨2, 3⟩  S_{β}(Long→Long) = ⟨2, 1⟩

and from the definition we can see that S_{α} ≲ S_{β}. Indeed we also have S_{α} ≲ S_{γ→Long}, and S_{Nat→Int} ≲ S_{β}.

4.2.5 Remark: Suppose Q is a Helly poset of base types with HSF F_{Q}. Our definition of Q-consistency here coincides with that of definition(2.4.4). Moreover if S and T are Q-consistent states defined only on base types in Q and variables and S ≲ T, then d(S|_{Q}, T |_{Q}) ⩽ ⟨1, 2⟩, and so F_{Q}(S_{1}|_{Q}) ⩽ F_{Q}(S_{2}|_{Q}).

4.2.6 Definition [I-expansion]: Let I be a set of distance inequalities. The I-expansion of a state S is

exp_{I}(S)(τ) = δ_{Q} ∧ ⋀{S(τ′) + d_{I}(τ′, τ)} if there is τ′ such that S(τ′)↓ and
d_{I}(τ′, τ) < e for some e
undefined  otherwise

S is a I-state if for every τ_{1}, τ_{2} ∈ dom(S), d_{I}(τ_{1}, τ_{2}) ⩽ d_{B(S)}(τ_{1}, τ_{2}), so if I is Q-consistent, then so is any I-state.

4.2.7 Proposition [Properties of I-expansion]: Suppose Q is a partial order of diameter δ_{Q}. Let I be a Q-closed set of distance inequalities.

1. if S is an I-state, then so is exp_{I}(S).

2. if S_{1} ≲ S_{2}, then exp_{I}(S_{1}) ≲ exp_{I}(S_{2})

Proof:

1. Let T = red(exp_{I}(S)), and suppose T (τ_{1}) = e_{1}, T (τ_{2}) = e_{2}, so that d_{B(T)}(τ_{1}, τ_{2}) = e_{1}^{−1} + e_{2}.

\begin{aligned}e_{1}^{−1} + e_{2} = & (δ_{Q} ∧ ⋀\{S(τ′_{1}) + d_{I}(τ′_{1}, τ_{1}) | S(τ′_{1})↓\})^{−1}+ \\ & (δ_{Q} ∧ ⋀\{S(τ′_{2}) + d_{I}(τ′_{2}, τ_{2}) | S(τ′_{2})↓\}) \end{aligned}

which is at least

δ_{Q} ∧ ⋀{d_{I}(τ_{1}, τ′_{1}) + d_{B(S)}(τ′_{1}, τ′_{2}) + d_{I}(τ′_{2}, τ_{2}) | S(τ′_{1})↓, S(τ′_{2})↓}

Now since S is a I-state, we have d_{I}(τ′_{1}, τ′_{2}) ⩽ d_{B(S)}(τ′_{1}, τ′_{2}), so

d_{I}(τ_{1}, τ_{2}) ⩽ d_{I}(τ_{1}, τ′_{1}) + d_{B(S)}(τ′_{1}, τ′_{2}) + d_{I}(τ′_{2}, τ_{2})

for any τ′_{1} and τ′_{2}, so

d_{I}(τ_{1}, τ_{2}) ⩽ ⋀{d_{I}(τ_{1}, τ′_{1}) + d_{B(S)}(τ′_{1}, τ′_{2}) + d_{I}(τ′_{2}, τ_{2}) | S(τ′_{1})↓, S(τ′_{2})↓}

And so, since I is closed under Truncate, we have

d_{I}(τ_{1}, τ_{2}) ⩽ δ_{Q} ∧ ⋀{d_{I}(τ_{1}, τ′_{1}) + d_{B(S)}(τ′_{1}, τ′_{2}) + d_{I}(τ′_{2}, τ_{2}) | S(τ′_{1})↓, S(τ′_{2})↓}

2. Suppose S_{1} ≲ S_{2}. Let T_{1} = exp_{I}(S_{1}) and T_{2} = exp_{I}(S_{2}). If T_{2}(τ_{2}) = e_{2}. Then we must have

e_{2} = δ_{Q} ∧ ⋀{e′_{2} + e | S_{2}(τ) = e′_{2} and d_{I}(τ, τ_{2}) ⩽ e}

But for any τ such that S_{2}(τ) = e′_{2}, we have S_{1}(τ) ⩽ δ_{Q} ∧(⟨1, 2⟩+e′_{2}). Thus τ ∈ dom(T_{1}), and moreover

T_{1}(τ_{1}) = δ_{Q} ∧ ⋀{e′_{1} + e | S_{1}(τ) = e′_{1} and d_{I}(τ, τ_{1}) ⩽ e}
⩽ δ_{Q} ∧ ⋀{⟨1, 2⟩ + e′_{2} + e | S_{2}(τ) = e′_{2} and d_{I}(τ, τ_{1}) ⩽ e}
= δ_{Q} ∧ (⟨1, 2⟩ + ⋀{e′_{2} + e | S_{2}(τ) = e′_{2} and d_{I}(τ, τ_{1}) ⩽ e})
= δ_{Q} ∧ (⟨1, 2⟩ + e_{2})

The reasoning for the other condition is dual. □

4.2.8 Definition [Descent Functions]: Suppose Q is a decomposable ordering. Let c be a constructor, τ and e ∈ L be such that such that d_{Q}(c, ν(τ)) ⩽ e. Let ρ = cα_{l_{1}} … α_{l_{n}} We define the descent function for Q as follows:

P_{c}^{l}(τ, e) = (τ_{l}, e′) if (ρ′, τ_{l}, e′) ∈ D(ρ, τ, e) for some ρ′

We extend this to states: if c is a constructor such that d(c, ν(τ) ⩽ e for every constructor type τ such that S(τ) = e

P_{c}^{l}(S)(τ) = red{P_{c}^{l}(τ, e) | S(τ) = e and P_{c}^{l}(τ, e)↓}

4.2.9 Example: If we take S_{α} and S_{β} as before, it is clear that we have to choose → as the constructor for each state.

P\underrightarrow{1}(S_{α})(γ) = ⟨2, 1⟩  P\underrightarrow{1}(S_{α})(Long) = ⟨3, 2⟩
P\underrightarrow{1}(S_{β})(γ) = ⟨2, 3⟩  P\underrightarrow{1}(S_{β})(Long) = ⟨1, 2⟩
P\underrightarrow{2}(S_{α})(Nat) = ⟨1, 2⟩  P\underrightarrow{2}(S_{α})(Long) = ⟨2, 3⟩
P\underrightarrow{2}(S_{β})(Nat) = ⟨3, 2⟩  P\underrightarrow{2}(S_{β})(Long) = ⟨2, 1⟩

Notice that P\underrightarrow{1}(S_{β}) ≲ P\underrightarrow{1}(S_{α}), and P\underrightarrow{2}(S_{α}) ≲ P\underrightarrow{2}(S_{β})

4.2.10 Definition [Choice Function]: Let S be a state, g be a total function from Q-consistent states to constructors. If I is a set of distance inequalities, l ∈ L(g(S)) we define the l-successor of S

K_{I}^{l}(S) = exp_{I}(P_{g(S)}^{l}(S))

Then g is a choice function for Q if for any Q-closed I and any Q-consistent I-state S

G1 d_{Q}(g(S), ν(τ)) ⩽ e for every constructor type τ such that S(τ) = e (so K_{I}^{l}(S) exists).

G2 for any l ∈ L(g(S)), the successor state K_{I}^{l}(S) is an I-state. and for any pair S_{1}, S_{2} of consistent I-states such that S_{1} ≲ S_{2},

G3 g(S_{1}) ⊑ g(S_{2})

G4 For each l ∈ L(g(S_{1})) ∩ L(g(S_{2})))

• if p_{g(S_{1})}^{l} = pos, K_{I}^{l}(S_{1}) ≲ K_{I}^{l}(S_{2})
• if p_{g(S_{1})}^{l} = neg, K_{I}^{l}(S_{2}) ≲ K_{I}^{l}(S_{1})
• if p_{g(S_{1})}^{l} = mix, K_{I}^{l}(S_{2}) ≲ K_{I}^{l}(S_{1}) and K_{I}^{l}(S_{1}) ≲ K_{I}^{l}(S_{2})
(note that by G3 and remark (1.3.1), p_{g(S_{1})}^{l} = p_{g(S_{2})}^{l})

4.2.11 Theorem: Let Q be a decomposable ordering and g a choice function for Q. Suppose C is a constraint set whose Q-closure I is Q-consistent. Then C is solvable.

Proof: For each τ occurring in I we define the start state for τ to be the I-state

start_{τ} = exp_{I}({τ, ⟨1, 1⟩})

Let S_{C}, the set of C-states be the smallest set such that

• start_{τ} ∈ S_{C} for every τ ∈ O(C)

• K_{I}^{l}(S) ∈ S_{C} for every S ∈ S_{C}.

By induction using property G2, every state in S_{C} is a I-state, and since I is Q-consistent, every state in S_{C} is Q-consistent. We define the state graph H_{C} to be the transition graph whose vertex set is S_{C} and whose transition function is:

S →^{l} K_{I}^{l}(S)

Then we define ν(S) = g(S), and for each τ ∈ O(C) we take θ(τ) = (H_{C}, start_{τ}). Since S_{C} is finite, θ(τ) is a regular type.

To show that θ is a substitution, it suffices to demonstrate that for any constructor type τ, g(start_{τ}) = ν(τ) and that for any l ∈ L(τ)

start_{τ} →^{l} start_{τ(l)}

The first follows immediately from G1, since start_{τ}(τ) = ⟨1, 1⟩, For the second, suppose S = K_{I}^{l}(start_{τ}). Then we will show that S = start_{τ(l)}. S(τ(l)) = ⟨1, 1⟩, so by I-expansion, for any τ′ such hat d_{I}(τ(l), τ′) ⩽ e for some e,

\begin{aligned}S(τ′) & ⩽ & d_{I}(τ(l), τ′) \\ & = & start_{τ(l)}(τ′) \end{aligned}

but since by G2 S is an I-state, we have

start_{τ(l)}(τ′) = d_{I}(τ(l), τ′)
⩽ d_{B(S)}(τ(l), τ′)
= ⟨1, 1⟩ + S(τ′) = S(τ′)

Thus S = start_{τ(l)}. To see that θ is a solution, observe that if τ_{1}⩽τ_{2} ∈ C then start_{τ_{1}} ≲ start_{τ_{2}}, and by a simple induction using G3 and G4, for any τ_{1}⩽τ_{2} ∈ C, there is a simulation θ(τ_{1}) ≲ θ(τ_{2}), and thus θ(τ_{1}) ⩽ θ(τ_{2}) □

4.3 Examples

Atomic Subtyping and →

The case of structural constructors over some base poset Q_{0} is easy. We will work with just →, although others are equally straightforward. Notice that the diameter δ_{Q} of Q is δ_{Q_{0}} ∧ δ_{Q_{0}}^{∗}. Our descent function is

\begin{aligned}P\underrightarrow{1}(τ_{1}→τ_{2}, e) & = & (τ_{1}, e^{∗}) \\ P\underrightarrow{2}(τ_{1}→τ_{2}, e) & = & (τ_{2}, e) \end{aligned}

As a choice function, we take

g(S) = →  if S(τ_{1}→τ_{2})↓ for some τ_{1}→τ_{2}
F_{Q}(S|_{Q}) if S(a)↓ for some a ∈ Q
c  otherwise

For consistent S, dom(S) cannot contain both an atom and a constructor type, and can contain at most one component of Q, so this is well-defined. It remains to verify that it satisfies conditions G1–G4.

G1 immediate using remark(4.2.5) for the atomic case

G2 We will prove that P\underrightarrow{l}(S) is a I-state for l = 1, 2, for then by proposition(4.2.7) K_{I}^{l}(S) = exp_{I}(P\underrightarrow{l}(S)) is also. We will do the l = 1 case, l = 2 is similar. We need to show that for any τ_{1}, τ_{2} such that P\underrightarrow{1}(S)(τ_{1}) = e, P\underrightarrow{1}(S)(τ_{2}) = e′, d_{I}(τ_{1}τ_{2}) ⩽ e^{−1} + e′. It suffices to demonstrate that for every τ′_{1}, τ′_{2} such that S(τ_{1}→τ′_{1}) = e_{1} and S(τ_{2}→τ′_{2}) = e_{2}, that d_{I}(τ_{1}, τ_{2}) ⩽ δ_{Q} ∧ ((e_{1}^{∗})^{−1} + e_{2}^{∗}), for then

d_{I}(τ_{1}, τ_{2}) ⩽ ⋀_{τ′_{1},τ′_{2}}{δ_{Q} ∧ ((e_{1}^{∗})^{−1} + e_{2}^{∗}) | S(τ_{1}→τ′_{1}) = e_{1}, S(τ_{2}→τ′_{2}) = e_{2}}
= δ_{Q} ∧ ( ⋀_{τ′_{1},τ′_{2}}{(e_{1}^{∗})^{−1} + e_{2}^{∗} | S(τ_{1}→τ′_{1}) = e_{1}, S(τ_{2}→τ′_{2}) = e_{2}})
= δ_{Q} ∧ ( ⋀_{τ′_{1}}(S(τ_{1}→τ′_{1})^{∗})^{−1} + ⋀_{τ′_{2}} S(τ_{2}→τ′_{2})^{∗})

by distributivity of + over ∧

= ( ⋀_{τ′_{1}} S(τ_{1}→τ′_{1})^{−1})^{∗} + ( ⋀_{τ′_{2}} S(τ_{2}→τ′_{2}))^{∗}
= ( ⋀_{τ′_{1}} S(τ_{1}→τ′_{1})^{∗})^{−1} + ( ⋀_{τ′_{2}} S(τ_{2}→τ′_{2}))^{∗}
= (P\underrightarrow{1}(S)(τ_{1}))^{−1} + P\underrightarrow{1}(S)(τ_{2})

But if S_{1}(τ_{1}→τ′_{1}) = e_{1} and S_{2}(τ_{2}→τ′_{2}) = e_{2}, then d_{B(S)}(τ_{1}→τ′_{1}, τ_{2}→τ′_{2}) ⩽ e_{1}^{−1}+e_{2}, so since S is a I-state, d_{I}(τ_{1}→τ′_{1}, τ_{2}→τ′_{2}) ⩽ e_{1}^{−1}+e_{2}, so by Decomp, d_{I}(τ_{1}, τ_{2}) ⩽ (e_{1}^{−1} + e_{2})^{∗} = (e_{1}^{∗})^{−1} + e_{2}^{∗}.

G3 Suppose S ≲ T. Then dom(S) = dom(T). If the set contains →-types, or only variables, the result is immediate, and if dom(S) and dom(T) both contain base types, by remark(4.2.5) we have F_{Q}(S|_{Q}) ⩽ F_{Q}(T |_{Q}).

G4 Suppose S ≲ T, we will take l = 1, although the l = 2 case is similar. We define S_{1} = P\underrightarrow{1}(S), T_{1} = P\underrightarrow{1}(T). By proposition (4.2.7), it suffices to show that T_{1} ≲ S_{1}. Suppose T_{1}(τ) = e, then we must have

S_{1}(τ) = δ_{Q} ∧ ( ⋀_{τ′} S(τ→τ′))^{∗}
over all τ′ such that S(τ→τ′)↓
⩽ δ_{Q} ∧ ( ⋀_{τ′} δ_{Q} ∧ (⟨1, 2⟩ + T (τ→τ′)))^{∗}
⩽ δ_{Q} ∧ (⟨2, 1⟩ + ( ⋀_{τ′} T (τ→τ′))^{∗})
⩽ δ_{Q} ∧ (⟨2, 1⟩ + T_{1}(τ))

Similarly T_{1}(τ) ⩽ δ_{Q} ∧ (⟨1, 2⟩ + S_{1}(τ)), so T_{1} ≲ S_{1}

Atomic Subtyping, →, and {}

The argument for records is very similar. Our descent function is

P\underrightarrow{1}(τ_{1}→τ_{2}, e) = (τ_{1}, e^{∗})
P\underrightarrow{2}(τ_{1}→τ_{2}, e) = (τ_{2}, e)
P_{{}_{I}}^{l}({l_{j}:τ_{j}}, e) = (τ_{l}, e)  e < ⟨2, 2⟩ and l ∈ I ∩ J
P_{{}_{I}}^{l}({l_{j}:τ_{j}}, e) = (τ_{l}, ⟨3, 2⟩) ⟨2, 2⟩ ⩽ e ⩽ ⟨3, 2⟩ and l ∈ I ∩ J

and our choice function

g(S) = →  if S(τ_{1}→τ_{2})↓ for some τ_{1}→τ_{2}
F_{Q}(S|_{Q}) if S(a)↓ for some a ∈ Q
{}_{L(S)} if S({l_{i}:τ_{i}})↓ for some {l_{i}:τ_{i}}
where L(S) = ⋃{L(τ) | S(τ) ⩽ ⟨1, 2⟩}
c  otherwise

Again notice that exactly one of the above cases can hold by consistency of S.

G1 immediate

G2 The → case follows as in example(4.3) So suppose g(S) = {}_{L}. Again it suffices to show that P_{{}_{L}}^{l}(S) is an I-state for each l ∈ L, then the result follows by proposition(4.2.7). So suppose S_{l} = P_{{}_{L}}^{l}, and S_{l}(τ_{1}) = e_{1}, S_{l}(τ_{2}) = e_{2}. There are many possible cases to consider, where each of e_{1}, e_{2} is ⟨1, 1⟩, ⟨1, 2⟩, ⟨2, 1⟩, ⟨3, 2⟩. We will do the case where e_{1} = e_{2} = ⟨3, 2⟩, the others are similar.

l  l  l τ_{1}  ρ  τ_{2} S τ′_{1}  ρ′  τ′_{2}

Since l ∈ L(g(S)), there must be ρ′ such that S(ρ′) ⩽ ⟨1, 2⟩, and l ∈ L(ρ′). By inspection of the descent function, we must have τ′_{1} and τ′_{2} such that τ′_{1}(l) = τ_{1}, τ′_{2}(l) = τ_{2}, and ⟨2, 2⟩ ⩽ S(τ′_{1}) ⩽ ⟨3, 2⟩, and ⟨2, 2⟩ ⩽ S(τ′_{2}) ⩽ ⟨3, 2⟩. If ρ′(l) = ρ, we thus have {d_{I}(τ′_{1}, ρ) ⩽ ⟨3, 2⟩, d_{I}(τ′_{2}, ρ) ⩽ ⟨3, 2⟩} ⊆ B(S), so since S is a I-state, d_{I}(τ′_{1}, ρ) ⩽ ⟨3, 2⟩ and d_{I}(τ′_{2}, ρ) ⩽ ⟨3, 2⟩. Thus by Decomp, we have d_{I}(τ_{1}, ρ) ⩽ ⟨3, 2⟩ and d_{I}(τ_{2}, ρ) ⩽ ⟨3, 2⟩, and so by Dual and Join, we have d_{I}(τ_{1}, τ_{2}) ⩽ ⟨5, 4⟩ as required.

G3 Suppose S ≲ T. If dom(S) = dom(T) contains no record types, the result follows in the manner of example (4.3). Otherwise for any τ = {l_{i}:τ_{i}} such that T (τ) ⩽ ⟨1, 2⟩, S(τ) ⩽ ⟨1, 2⟩, so L(S) ⊇ L(T), and thus g(S) ⊑ g(T).

G4 The → case is as for example(4.3). Suppose S ≲ T, and g(S) = {}_{L_{S}}, and g(T) = {}_{L_{T}}, where L_{S} ⊆ L_{T}. We define S_{l} = K_{I}^{l}(S), T_{l} = K_{I}^{l}(T). Suppose T_{l}(τ_{l}) = e, we need to show that S_{l}(τ_{l}) ⩽ ⟨1, 2⟩ + e. The cases where e ⩽ ⟨1, 2⟩ are as in example(4.3); the interesting cases are ⟨2, 1⟩ ⩽ e ⩽ ⟨3, 2⟩. As all these cases are essentially similar, we will do the case where T_{l}(τ_{l}) = ⟨2, 1⟩, and show that S_{l}(τ_{l}) ⩽ ⟨2, 3⟩. There are two possibilities:

• there is some τ such that P_{{}_{T}}^{l} (T)(τ) = ⟨1, 1⟩ and d_{I}(τ, τ_{l}) = ⟨2, 1⟩, in which by reasoning similar to example(4.3) we have P_{{}_{S}}^{l}(S)(τ) ⩽ ⟨1, 2⟩, and so S_{l}(τ_{l}) ⩽ ⟨1, 2⟩ + ⟨2, 1⟩ = ⟨2, 3⟩.

• there is some τ such that P_{{}_{T}}^{l} (T)(τ) = ⟨2, 1⟩ and d_{I}(τ, τ_{l}) = ⟨2, 1⟩. In this case, we must have some τ′ such that T (τ′) = ⟨2, 1⟩, with τ′(l) = τ. But since l ∈ L(g(T)), there must also be ρ′ such that T (ρ′) = ⟨1, 2⟩ and l ∈ L(ρ). In this case, since T is an I-state, we must have

d_{I}(ρ′, τ′) ⩽ d_{B(T)}(ρ′, τ′)
= T (ρ′)^{−1} + T (τ′)
= ⟨2, 1⟩ + ⟨2, 1⟩
= ⟨2, 1⟩

and thus if ρ′(l) = ρ, d_{I}(ρ, τ_{l}) ⩽ ⟨2, 1⟩. But since S(ρ′) ⩽ ⟨1, 2⟩, we have P_{{}_{S}}^{l}(S)(ρ) ⩽ ⟨1, 2⟩ and thus S_{l}(τ_{l}) ⩽ ⟨2, 3⟩ as required. The reasoning for the other condition is dual.

Atomic Subtyping, →, ⊤ and ⊥

Observe that in this case we have δ_{Q} = ⟨2, 2⟩ and Q has only one component. Our descent function is

\begin{aligned}P\underrightarrow{1}(τ_{1}→τ_{2}, e) & = & (τ_{1}, e^{∗}) & e < ⟨2, 2⟩ \\ P\underrightarrow{2}(τ_{1}→τ_{2}, e) & = & (τ_{2}, e) & e < ⟨2, 2⟩ \end{aligned}

and our choice function

g(s) = ⊤  if there is no S(a) ⩽ ⟨1, 2⟩ or S(τ_{1}→τ_{2}) ⩽ ⟨1, 2⟩
⊥  otherwise, if there is no S(a) ⩽ ⟨2, 1⟩ or S(τ_{1}→τ_{2}) ⩽ ⟨2, 1⟩
→  otherwise if there is some S(τ_{1}→τ_{2}) < ⟨2, 2⟩
F_{Q′}(S|_{Q′}) otherwise, if there is some S(a) < ⟨2, 2⟩

where Q′ = Q ∪ {⊤, ⊥}, which by proposition(2.2.8) is a lattice. Unlike the previous examples, S is defined on all types occurring in I. However, at most one of the last two cases is possible for any consistent S: if g(S) ≠ ⊤ and g(S) ≠ ⊥, then we must have τ_{1} and τ_{2} such that S(τ_{1}) ⩽ ⟨1, 2⟩ and S(τ_{2}) ⩽ ⟨2, 1⟩, and such that each of τ_{1} and τ_{2} is either an →-type or a base type. It cannot be the case that one is an →-types and one a base type, for then we would have d(τ_{1}, τ_{2}) ⩽ ⟨2, 1⟩ ∈ B(S), and B(S) would not be consistent.

G1 immediate

G2 The only case where L(g(S)) is non-empty is when g(S) = →, whence the result follows in a similar manner to example(4.3)

G3 Suppose S ≲ T. if g(T) = ⊤ then the result is immediate. If g(T) = ⊥, then we must have T (τ) = ⟨1, 2⟩ for some →-type or atom τ, so S(τ) = ⟨1, 2⟩, and furthermore there can be no →-type or atom τ′ such that S(τ′) ⩽ ⟨2, 1⟩, since then we would have T (τ′) ⩽ ⟨2, 1⟩, contradicting g(T) = ⊥. Thus g(S) = ⊥. If g(T) = →, then we must have some T (τ_{1}→τ_{2}) ⩽ ⟨1, 2⟩, and thus S(τ_{1}→τ_{2}) ⩽ ⟨1, 2⟩, so g(S) = → or ⊥. Finally, if g(T) = a, by similar reasoning either g(S) = ⊥, or g(S) = b where b ⩽ a by the fact that F_{Q′} is a HSF.

G4 Suppose l ∈ L(g(S)) ∩ L(g(T)). We must have g(S) = g(T) = →. We will do the l = 2 case. Since K_{I}^{2}(S)(τ) ⩽ ⟨2, 2⟩ for every τ ∈ O(S), it suffices to demonstrate that for every τ such that K_{I}^{2}(T) ⩽ ⟨1, 2⟩, K_{I}^{2}(S)(τ) ⩽ ⟨1, 2⟩, and for every τ′ such that K_{I}^{2}(S)(τ) ⩽ ⟨2, 1⟩, K_{I}^{2}(T)(τ) ⩽ ⟨2, 1⟩. Since these are dual, we will do just the first: suppose we have K_{I}^{2}(T)(τ) ⩽ ⟨1, 2⟩, then we must have τ′ such that P\underrightarrow{l}(T)(τ′) ⩽ ⟨1, 2⟩, and d_{I}(τ′, τ) ⩽ ⟨1, 2⟩. But then we must have T (τ′′→τ′) ⩽ ⟨1, 2⟩ for some τ′′, and thus S(τ′′→τ′) ⩽ ⟨1, 2⟩. Hence P\underrightarrow{l}(S)(τ′) ⩽ ⟨1, 2⟩, and thus K_{I}^{2}(S)(τ) ⩽ ⟨1, 2⟩.

Atomic Subtyping, →, and Nonatomic Base Types

In order to guarantee regular solutions, we require the underlying poset of types to have finite diameter. In general, bounded quantification can result in posets of arbitrarily large diameter, but a finite signature always results in a poset of finite diameter.

4.3.1 Proposition: Suppose Q_{Σ} is the set of regular types built using the → constructor over a Helly poset Q_{0} of finite diameter extended with a bounded signature Σ. Then Q_{Σ} has finite diameter.

Proof: We proceed by induction over the signature. It is easy to show that if Q_{0} has diameter δ_{Q_{0}}, the poset of regular types over Q has diameter δ_{Q_{0}} ∧ δ_{Q_{0}}^{∗}.

So suppose the poset of regular types over Q extended by Σ has diameter δ_{(Q,Σ)}, and Σ′ = Σ, a⩽τ.

For any two types τ_{1}, τ_{2} containing a, let τ′_{1} = [τ/a]τ_{1}, and τ′_{2} = [τ/a]τ_{2}. Then d(τ_{1}, τ′_{1}) ⩽ ⟨2, 2⟩, since we obtain an up-fence of length 2 from τ to τ′ by first promoting all occurrences of a at positive parity paths, then all occurrences at negative parity , and by doing the reverse, we obtain a down-fence of length 2 from τ_{1} to τ′_{1}; similarly d(τ_{2}, τ′_{2}) ⩽ ⟨2, 2⟩. Now since τ′_{1} and τ′_{2} contain no occurrences of a, we must have d(τ′_{1}, τ′_{2}) ⩽ δ_{(Q,Σ)}, and thus d(τ′_{1}, τ′_{2}) ⩽ ⟨2, 2⟩ + δ_{(Q,Σ)} + ⟨2, 2⟩, which is an upper bound for the diameter of δ_{(Q,Σ′)}. □

Unfortunately, even if Q is a low-diameter poset such as a tree, so that the component of types of shape a→a has diameter ⟨3, 3⟩, adjoining a bounded constant results in a component of diameter ⟨3, 4⟩. Then the component of shape (a→a)→(a→a) has diameter ⟨4, 4⟩, and by extending the signature the diameter of the poset can become arbitrarily high. A base type in Σ is atomic if either Σ(a)↑, or Σ^{↑}(a) is an atomic base type (in Q or Σ.)

4.3.2 Proposition: Let Q′ be the extension of Q by all the atomic base types in Σ. Q′ is a Helly poset.

Proof: By induction on the number of bindings for atomic base types Σ. If Σ(a)↑, {a} ∪ Q′ is obviously Helly, otherwise the result follows from proposition(2.2.7). □

Since Q′ is Helly, it has a HSF. Our descent function P_{c}^{l} is:

P\underrightarrow{1}(τ_{1}→τ_{2}, e) = (τ_{1}, e^{∗})
P\underrightarrow{2}(τ_{1}→τ_{2}, e) = (τ_{2}, e)
P\underrightarrow{1}(a, e)  = (τ_{1}, (e^{▲})^{∗}) where Σ^{⇑}(a) = τ_{1}→τ_{2}
P\underrightarrow{2}(a, e)  = (τ_{2}, e^{▲}) where Σ^{⇑}(a) = τ_{1}→τ_{2}
P_{a}^{1}(τ_{1}→τ_{2}, e) = (τ_{1}, (e^{△})^{∗})
P_{a}^{2}(τ_{1}→τ_{2}, e) = (τ_{1}, e^{△})

and our choice function

→  if S(τ_{1}→τ_{2})↓ for some τ_{1}→τ_{2}
⋀{a | S(a) ⩽ ⟨1, 2⟩} if S(a) ⩽ ⟨1, 2⟩ for some non-atomic base type a
F_{Q′}(S|_{Q′})  if dom(S) contains an atomic base type
c  otherwise

Now we need to demonstrate that G1–G4 are met.

G1 straightforward G2 It suffices to prove that P_{g(S)}^{l}(S) is an I-state for then by proposition(4.2.7)

so is K_{I}^{l}(S). Suppose g(S) =→, l = 2. If S(a)↑ for any a, the case is as in example(4.3). So suppose we have S(a) = f such that e_{1} = f^{▲}, Σ^{⇑}(a) = τ′_{1}→τ_{1}, and S(τ′_{2}→τ_{2}) = e_{2}. Then d_{B(S)}(τ′_{1}→τ_{1}, τ′_{2}→τ_{2}) ⩽ f^{−1} + e_{2}, so since S is an I-state, d_{I}(τ′_{1}→τ_{1}, τ′_{2}→τ_{2}) ⩽ f^{−1} + e_{2}. Then

d_{I}(τ_{1}, τ_{2}) ⩽ (f^{−1} + e_{2})^{△} by Descent
⩽ (f^{−1})^{△} + e_{2}
= (f^{▲})^{−1} + e_{2}
= e_{1}^{−1} + e_{2}

The other possibilities are similar.

G3 Suppose S ≲ T.

• If g(S) = g(T) = →, the result is immediate.

• If g(S) = a and g(T) = b, there are two cases: either a, b ∈ Q′, whence by remark(4.2.5) we have F_{Q′}(S|_{Q′}) ⩽ F_{Q′}(T |_{Q′}). Or a and b are nonatomic base types, in which case for every c such that T (c) ⩽ ⟨1, 2⟩, we must have S(c) ⩽ ⟨1, 2⟩, so by the definition of g, a ⩽ b.

• If g(T) = a, we must have T (a) ⩽ ⟨1, 2⟩, so S(a) ⩽ ⟨1, 2⟩, and so it cannot be the case that g(S) = →.

• If g(S) = a and g(T) = →, by consistency Σ^{⇑}(a) must be an arrow type and so a ⊑ →

G4 Suppose S ≲ T and g(S) = g(T) = →, again we will do the l = 2 case. If we have T_{2}(τ) = e, and T (τ′→τ) = e, then the result follows as in example(4.3). Otherwise suppose we have T (a) = f, with Σ^{⇑}(a) = τ′→τ, and f^{▲} = e. Then we have S(a) ⩽ δ_{Q} ∧ (⟨1, 2⟩ + f), and thus S_{2}(τ) ⩽ δ_{Q} ∧ (⟨1, 2⟩ + f)^{▲}, so S_{2}(τ) ⩽ δ_{Q} ∧ (⟨1, 2⟩ + f^{▲}). If g(S) or g(T) is a base type, the argument is similar. If g(S) = a and g(T) = b then a ⩽ b and so for any τ such that S_{2}(τ) = e and e = f^{▲}, for any S(τ′→τ) = f we have T (τ′→τ) ⩽ ⟨2, 1⟩ + f, and thus

T_{2}(τ) ⩽ δ_{Q} ∧ (⟨2, 1⟩ + f)^{▲}
= δ_{Q} ∧ (⟨2, 1⟩ + f^{▲})
= δ_{Q} ∧ (⟨2, 1⟩ + e)

and similarly S_{2}(τ) ⩽ δ_{Q} ∧ (⟨1, 2⟩ + T_{2}(τ)) so S_{2} ≲ T_{2}.

One application of this result is to rephrase an entailment problem as a solvability problem. We introduce bounded quantifiers, which quantify only over a single variable and a single constraint which bounds that variable above; and unbounded quantifiers, which quantify only over a single variable without constraint.

4.3.3 Definition [Types and Type Schemes]:

Types  τ ::= a | α | Tτ_{1} … τ_{n}
Bounded Type Schemes  σ ::= τ | ∀α.σ | ∀α⩽τ.σ
Constrained Type Schemes κ ::= ∀\overline{α}\C.τ

where C is a set of inequalities of the form τ ⩽ τ′.

Bounded type schemes are relatively easy to read, and thus provide a programmerfriendly way to print out those constrained types which may be rendered in such a form.

4.3.4 Definition [Instances and Entailment]: We say that τ is a (Q, Σ)- instance of ∀\overline{α}\C.τ′ if there is a substitution θ : \overline{α}→τ(Q ∪ atom(Σ)) such that Q, Σ ⊢ θc for each c ∈ C, and θτ′ = τ.

We define Q, Σ ⊢ κ ≼ σ if for any extension Σ′ of Σ and Q-model Q′, for any (Q′, Σ′)-instance τ_{σ} of σ, there is a (Q′, Σ′)-instance τ_{κ} of κ such that Σ′ ⊢ τ_{κ} ⩽ τ_{σ}

Like other entailment relations in the literature, and like the semantic notion of entailment of chapter 3, this definition is based on the existence of an instance of τ_{κ} which is a subtype of any notion of τ_{σ} under any possible extension of the underlying order. The relation given here is much weaker than that of [52] or [40], where the underlying order may be extended by arbitrary constraints, or even recursive constraints in which a type variable is allowed to occur on both sides of an inequality. However, we gain the ability to test the entailment relation efficiently by checking solvability of an appropriate constraint set.

4.3.5 Proposition: Suppose σ = ∀α_{1}(⩽τ_{1})∀α_{2}(⩽τ_{2}) … ∀α_{n}(⩽τ_{n}).τ_{σ} and κ = ∀\overline{β}\C.τ_{κ}. Then Q, Σ ⊢ κ ≼ σ iff for

Σ′ = Σ, a_{1}(⩽τ_{1}), a_{2}(⩽[a_{1}/α_{1}]τ_{2}) …, a_{n}(⩽[a_{1}/α_{1}, …, a_{n−1}/α_{n−1}]τ_{n})

there is some substitution θ from \overline{β} to τ(Q ∪ Σ′) such that Q, Σ′ ⊢ θ(c) for every c ∈ C ∪ {θτ_{κ} ⩽ [a_{i}/α_{i}]τ_{σ}}.

Proof: Suppose Q, Σ ⊢ κ ⩽ σ. Then by the definition of the relation ⩽, we know that for any (Q, Σ′)-instance of σ, there is a (Q, Σ′)-instance of κ which is a subtype of it. Now [a_{i}/α_{i}]τ_{σ} is an instance of σ, so we must have some substitution θ on the variables \overline{β} quantified over in κ, such that Q, Σ′ ⊢ θ(c) for every c ∈ C, and moreover Q, Σ′ ⊢ θτ_{κ} ⩽ [a_{i}/α_{i}]τ_{σ} as required.

Now suppose we have θ such that Q, Σ′ ⊢ θ(c) for every c ∈ C ∪ {θτ_{κ} ⩽ [a_{i}/α_{i}]τ_{σ}}, and suppose Σ_{1} is an extension of Σ and Q_{1} ⊳ Q. Without loss of generality we may assume that the new base types in Σ_{1} and Q_{1} are distinct from the a_{i}. Let θ_{1} be a substitution from the α_{i} to τ(Q_{1} ∪ atom(Σ_{1})) such that Q_{1}, Σ_{1} ⊢ θ_{1}α_{i} ⩽ θ_{1}τ_{i}, so that θ_{1}τ_{σ} is a (Q_{1}, Σ_{1})-instance of σ. Let θ′ = θ_{1} ∘ [α/a] ∘ θ. We claim that Q_{1}, Σ_{1} ⊢ θ′τ_{κ} ⩽ θ_{1}τ_{σ}, and that Q_{1}, Σ_{1} ⊢ θ′C.

These follow from the fact that if Q, Σ′ ⊢ τ_{1} ⩽ τ_{2}, then Q, Σ_{1} ⊢ (θ_{1} ∘ [α_{i}/a_{i}])τ_{1} ⩽ (θ_{1} ∘ [α_{i}/a_{i}])τ_{2}, which we will show by induction on the structure of the proof of Q, Σ′ ⊢ τ_{1} ⩽ τ_{2}.

Suppose the result holds for the premises of a rule. If the rule is Refl or QTax, the result is immediate, and if it is Cons, then the result is follows from the induction hypothesis. The remaining case is Prom^{↑}: If the variable being promoted is not one of the a_{i}, then the result follows from the induction hypothesis. Otherwise, the conclusion of the rule is Q, Σ′ ⊢ a_{m} ⩽ τ_{2}, and it has as a premise Q, Σ′ ⊢ Σ(a_{m}) ⩽ τ_{2}. So by the induction hypothesis, we have Q_{1}, Σ_{1} ⊢ (θ_{1} ∘ [α_{i}/a_{i}])(Σ′^{↑}(a_{m})) ⩽ (θ_{1} ∘ [α_{i}/a_{i}])τ_{2}. But (θ_{1} ∘ [α_{i}/a_{i}])(Σ′^{↑}(a_{m})) is just θ_{1}τ_{m}, and we have Q_{1}, Σ_{1} ⊢ θ_{1}α_{i} ⩽ θ_{1}τ_{i}, so by transitivity we obtain the required result Q_{1}, Σ_{1} ⊢ (θ_{1} ∘ [α_{i}/a_{i}])(a_{m}) ⩽ (θ_{1} ∘ [α_{i}/a_{i}])τ_{2} □

This presents us with an alternative formulation of the subtyping relation which is not parameterised over all possible extensions of Σ, and which will be the basis for checking the relation: We simply extend the signature as relevant and check for solvability.

4.4 Conclusion

We have demonstrated a single framework which encompasses both atomic subtyping over Helly posets, and the various possibilities for structural and nonstructural subtyping. In particular, our techniques allow us to deal with the expressions given in the introduction, illustrating the problems that can result from the inclusion of ⊤ and ⊥ in a type system. We now have the ability to check the subtype relation without them, but retaining records. In the context of record and function types only, we return to the term:

let f = fun(r) r.X + if r.X then 1 else 2;

The “One big lattice” technique accepts this term as well-typed: in fact it has the minimal type {X:⊥}→Nat, since the X field of r is used at types Bool and Int. However, using the results of this chapter we have an effective technique which enables us to denounce this term as containing a type error. Similarly, in the presence of polymorphic equality, we reject the term true = 3.

Our main theorem for solvability are also applicable to other type systems

• AC-object constructors [2] whose inference problem is studied in [32, 20],

• systems with ⊤ and not ⊥ (or conversely), introduced in [48], whose inference problem is studied in [24, 33]

• Systems of records with covariant and invariant field subtyping [44]

Our main extension to the studies of type inference in these systems is the incorporation of atomic subtyping, and the development of a unifying framework in which to consider combinations of the various distinctive features of such constructors.

Polynomial Time Solution

It is not had to see that for the orderings we have considered, the Q-closure of a constraint set can be accomplished by dynamic transitive distance closure in polynomial time, thus providing a test for solvability. However, in [49], Tiuryn shows that even when solutions are represented as directed acyclic graphs, the size of a minimal solution grows exponentially with respect to the size of the constraint set, and so we cannot expect to construct solutions in polynomial time. For finite structural subtyping over a lattice, Tiuryn gives a method for constructing an augmented DAG for each variables in a solvable constraint set: a graph of polynomial size from which the solution at any particular node in the solution tree can be recovered in polynomial time.

Instead of constructing distinct graphs for each variable, we can use a single graph (the Helly Closure Graph for the constraint set C) whose vertices are O(C), with edges from τ_{1} to τ_{2} of length d_{I}(τ_{1}, τ_{2}) where I is the Q-closure of C. Using such a graph, it is easy to perform I-expansion, choice, and descent in polynomial time, and thus the process of computing the label at ant path π in the solution for a type τ in the constraint set is linear in the length of π, and polynomial in the size of the constraint set. In the case where the diameter of the poset is ⟨1, 1⟩, i.e. we have no subtyping, an obvious optimisation of Helly graph closure is to contract all ⟨1, 1⟩ edges, and we obtain as our solvability criterion that the constraints are unifiable as equalities, and as our closure graph, the usual unification DAG. This observation can also be made for the technique of [33]: our extension of that method allows incorporation of atomic subtyping over Helly posets and of record subtyping without need for ⊤ and ⊥.

Simplification

It would be interesting to extend the techniques of chapter 3 to deal with constructor types.

In the finite structural case, we can naively compute the shape of each variable α (as in e.g. [18]), and reduce the constraint set to atomic form by substitution. However it is easy to show that in a non-atomic constraint set, substituting an internal variable in this way does not alter the distances between observables which are obtained using the Decomp rules in addition to Dual, Join and Min. Since the algorithms for computing tautenings operate only on those distances, in order to test entailment on non-atomic constraint sets it is sufficient to substitute only for the externals. Whilst this can still result in an exponential increase in the size of the constraint set, such would correspond to extremely large observable types, which are rare in practice. This observation is similar to that of [21] where a proof is given that the problem of determining whether a set of constraints entails a single constraint is co-NP complete, even over lattices.

Reduction of a constraint set to atomic form may be relatively expensive, so in practice it is more sensible to use a hybrid approach: to first apply general, easily-validated simplifications (such as moving a negative observable to a least upper bound), and when the constraint set is thus simplified, to reduce to atomic form and H-contract.

However, in order for simplification over Helly posets to be competitive with the approach for lattices of [39], it will be necessary for to extend the techniques to cases where constraints cannot necessarily be reduced to an equivalent atomic form, such as when using regular types, or non-structural subtyping.

Subtyping and Unification

The polymorphic instance relation can be seen as a kind of subtyping, and one that has received considerable discussion in the literature (recent work includes [51, 31]) since defined by Mitchell in [29]. If we consider the type inference algorithm of ML in a subtyping perspective, substitutions can be viewed as coercions between type scheme, principal types are then minimal types, and principal substitutions are the coercions which must exist to generate a minimal type from the two minimal types combined in an application term.

Suppose we have inferred an argument type scheme σ_{1} for a function and we attempt to apply the function to a term with inferred type scheme σ_{2}. The application will be well-typed exactly if σ_{2} is an instance of σ_{1}. Thus we need to seek a substitution on the free variables of σ_{1} and σ_{2} and a suitable instantiation of their bound variables, which makes this the case. However, we cannot use just any such substitution: in order to obtain a principal type property, we need to use a principal substitution: one which is more general than any other possible substitution we might use.

Although type systems like F_{⩽}[11] combine subtype and parametric polymorphism, it is hard to see how one might incorporate a decidable instance relation of the sort necessary for ML style type inference, since even in System F the containment relation is undecidable [51]. Instead, we shall start with the predicative fragment of [31] as outlined in chapter 1, which we shall refer to as ML_{∀}, and show how subtyping may be incorporated into that framework.

In this chapter we will define a subtyping system using bounded quantification which combines both kinds of polymorphism, and whose subtyping relation provides a notion of instance suitable for type inference. We will show how the relation can be decided using a method similar to that of the last chapter but with scoping restrictions on possible substitutions involved in removing essential occurrences. The technique bears a considerable similarity to mixed-prefix unification [26] which is used in [31] to generate the principal substitutions.

In chapter 6, we will extend the subtyping system to deal with constrained type schemes, and demonstrate the existence of factorisations, analogous to principal substitutions. We shall use these to define a type system with annotations and a type inference algorithm for it which is complete.

In order to simplify the treatment, we will for the moment dispense with the poset of base types Q, so that all our base types, and their subtyping relation, will be introduced in a bounded signature Σ. We will describe in section 5.5 how our results may be generalised to a base poset of atomic types.

5.1 First Class Type Schemes

We will delay the discussion of type inference until chapter 6, and thus for the moment we will not consider constrained quantification. We will deal with the → constructor specially, as it is the only constructor which we shall allow to operate on type schemes as well as types. Other constructors, e.g. pairing, can be dealt with using similar techniques, but as we shall see in chapter 6, our type inference system will require us to deal with constructor arguments of polarity pos differently to those of polarity neg, in that the latter may require annotations on the term constructors which introduce them.

5.1.1 Definition [Types and Type Schemes]:

Types  τ ::= a | α | τ_{1}→τ_{2} | Tτ_{1} … τ_{n}
Type Schemes σ ::= τ | σ_{1}→σ_{2} | ∀α.σ | ∀α ⩽ τ.σ

Our rules are a combination of two natural notions of subtyping: that given by instantiation, so that ∀α.σ is a subtype of [τ/α]σ for any τ, and that given by the consequences of subtype polymorphism. They bear some similarity to the subtyping relation of F_{⩽}, although our ∀ rules are more in the ML-style, and also to ML_{∀}, although naturally we have to take the subtyping behaviour of arrow types and constructors into consideration, and we have bounded quantifiers, where it’s necessary to check that any instantiation respects the bound. Note that σ is well-formed with respect to Σ if Σ contains all the base types in σ.

Rules for Subtyping

\frac{Σ^{↑}(a) = τ}{Σ ⊢ a ⩽ τ}  (Var)

\frac{Σ ⊢ σ_{1} ⩽ σ_{2} Σ ⊢ σ_{2} ⩽ σ_{3}}{Σ ⊢ σ_{1} ⩽ σ_{3}}  (Trans)
\overline{Σ ⊢ σ ⩽ σ}  (Taut)
\frac{Σ ⊢ τ_{i} ⋄^{T}_{i} τ′_{i}}{Σ ⊢ Tτ_{1} … τ_{n} ⩽ Tτ′_{1} … τ′_{n}}  (Cons)
\frac{Σ ⊢ σ′_{1} ⩽ σ_{1} Σ ⊢ σ_{2} ⩽ σ′_{2}}{Σ ⊢ σ_{1}→σ_{2} ⩽ σ′_{1}→σ′_{2}}  (Arrow)
\overline{Σ ⊢ ∀α.σ ⩽ [τ/α]σ}  (∀-left)
\frac{Σ, a ⊢ σ_{1} ⩽ [a/α]σ_{2} a does not occur in σ_{1} or σ_{2}}{Σ ⊢ σ_{1} ⩽ ∀α.σ_{2}} (∀-right)
\frac{Σ ⊢ τ_{1} ⩽ τ}{Σ ⊢ ∀α⩽τ.σ ⩽ [τ_{1}/α]σ}  (∀_{⩽}-left)
\frac{Σ, a⩽τ ⊢ σ_{1} ⩽ [a/α]σ_{2} a does not occur in σ_{1} or σ_{2}}{Σ ⊢ σ_{1} ⩽ ∀α⩽τ.σ_{2}} (∀_{⩽}-right)

Reflexivity and Transitivity

Algorithms for testing the subtyping relation (e.g. [11]) typically involve building a derivation of the sequent in question. Usually this involves building an algorithmic set of rules that is equivalent to the original set, and which is syntax directed, so that we always know which was the last algorithmic rule applied to build a sequent. This involves some notion of canonical derivation, in which Trans is redundant, and use of Taut restricted

To develop an algorithm we will construct an equivalent system of rules for which it is the case that all proofs will, if not completely directed, will be directed in the sense that the conclusion of the judgement will determine the last rule applied. Our first step will be to greatly restrict the use of the Taut rule:

5.1.2 Proposition: Suppose we replace the Taut rule in the above proof system with

\overline{Σ ⊢ a ⩽ a}  (Refl)

Then for any σ which is well-formed with respect to Σ, Σ ⊢ σ ⩽ σ Proof: We will proceed by structural induction on the type σ. If σ is a variable, or the outermost constructor of σ is T or →, the result follows from the induction hypothesis. The remaining cases are when the outer constructor is a bounded or unbounded universal quantifier; since the two cases are very similar, we will just do the bounded case. Suppose σ = ∀α⩽τ.σ′, then Σ, a⩽τ ⊢ [a/α]σ′ ⩽ [a/α]σ′ by the induction hypothesis. So

{Σ, a⩽τ ⊢ a ⩽ τ \atop Σ ⊢ a⩽τ ⊢ ∀α⩽τ.σ ⩽ [a/α]σ′} ; Σ ⊢ ∀α⩽τ.σ ⩽ ∀α⩽τ.σ

□

In order to generate an algorithmic proof system, it remains to eliminate Trans. We replace the Var rule by

\frac{Σ ⊢ Σ^{↑}(a) ⩽ σ}{Σ ⊢ a ⩽ σ}  (Prom^{↑})

and ∀-left and ∀_{⩽}-left by

\frac{Σ ⊢ [τ/α]σ_{1} ⩽ σ_{2}}{Σ ⊢ ∀α.σ_{1} ⩽ σ_{2}}  (∀-left′)
\frac{Σ ⊢ [τ_{1}/α]σ_{1} ⩽ σ_{2} Σ ⊢ τ_{1} ⩽ τ}{Σ ⊢ ∀α⩽τ.σ_{1} ⩽ σ_{2}}  (∀_{⩽}-left′)

since in the presence of Refl and Trans these rules are equivalent. Further, we shall show that in the presence of these rules, Trans can be completely eliminated.

5.1.3 Definition: We shall call the proof system containing the rules Refl, Prom^{↑}, Arrow, Cons, ∀-left′, ∀-right, ∀_{⩽}-left′, ∀_{⩽}-right, and Trans the canonical proof system.

For the rest of this chapter, ⊢ will denote provability in the canonical proof system. We will begin with a simple structural lemma, then a limited form of transitivity elimination. We use this to derive a result relating elimination and substitution, and thence full transitivity elimination.

5.1.4 Lemma [Signature permutation]: Suppose that

Σ = Σ_{1}, a_{1}(⩽τ_{1}), a_{2}(⩽τ_{2}), Σ_{2}

and further that

Σ′ = Σ_{1}, a_{2}(⩽τ_{2}), a_{1}(⩽τ_{1}), Σ_{2}

is well-formed. If Σ ⊢ σ_{1} ⩽ σ_{2}, then Σ′ ⊢ σ_{1} ⩽ σ_{2}

Proof: By induction on the structure of the proof of Σ ⊢ σ_{1} ⩽ σ_{2}. □

5.1.5 Lemma [Renaming]: Suppose Σ ⊢ σ_{1} ⩽ σ_{2}, a is a Σ-atom, and b is an atom that occurs nowhere in the derivation. Then [b/a]Σ ⊢ [b/a]σ_{1} ⩽ [b/a]σ_{2}.

Proof: By induction on the structure of the proof of Σ ⊢ σ_{1} ⩽ σ_{2}. □

5.1.6 Lemma [Weakening]: If Σ ⊢ σ_{1} ⩽ σ_{2} and a ∉ dom(Σ), then there are derivations with the same structure of Σ, a ⊢ σ_{1} ⩽ σ_{2}, and of Σ, a⩽τ ⊢ σ_{1} ⩽ σ_{2} for any τ ∈ τ(Σ).

Proof: By induction on the structure of the proof of Σ ⊢ σ_{1} ⩽ σ_{2}, using signature permutation for the case where the last rule in the derivation is a ∀-right rule, and renaming if the situation occurs in a ∀-left rule that the new variable appended to Σ is a. □

5.1.7 Lemma [Partial Transitivity Elimination]: If there are Trans-free derivations D_{i} ≡ Σ ⊢ σ_{1} ⩽ τ and D_{2} ≡ Σ ⊢ τ ⩽ σ_{2}, then there is a Trans-free derivation of Σ ⊢ σ_{1} ⩽ σ_{2}.

Proof: We proceed by induction on the size (i.e. the number of rule applications) in the derivation. Suppose we have a derivation of size n, and every derivation of size less than n can be rewritten as a Trans-free derivation. If the last rule in the derivation is not Trans, the result is immediate. Otherwise we proceed by case analysis on the rules immediately above Trans. Some combinations are impossible, either because they result in syntactically inconsistent types for τ, or require τ to be a type scheme.

Refl Prom^{↑} Cons Arrow ∀-l ∀_{⩽}-l ∀-r ∀_{⩽}-r
Refl  × ×
Prom^{↑}  × ×
Cons  ×  ×  × ×
Arrow  ×  ×  × ×
∀-left′  × ×
∀_{⩽}-left′  × ×
∀-right ×  ×  ×  ×  × × × ×
∀_{⩽}-right ×  ×  ×  ×  × × × ×

The cases where one of the derivations is an instance of Refl are trivial, and allowable combinations of Arrow, Cons, and Prom^{↑} are straightforward using the induction hypothesis. The remaining cases are when ∀-left′ ends D_{1}, or ∀-right ends D_{2}. With ∀-left on the left we have

{{Σ ⊢ [τ_{2}/α]σ ⩽ τ Σ ⊢ τ_{2} ⩽ τ_{1} \atop Σ ⊢ ∀α⩽τ_{1}.σ ⩽ τ} Σ ⊢ τ ⩽ σ_{1} \atop Σ ⊢ ∀α⩽τ_{1}.σ ⩽ σ_{1}}

becomes

{{Σ ⊢ [τ_{2}/α]σ ⩽ τ Σ ⊢ τ ⩽ σ_{1} \atop Σ ⊢ [τ_{2}/α]σ ⩽ σ_{1}} Σ ⊢ τ_{2} ⩽ τ_{1} \atop Σ ⊢ ∀α⩽τ_{1}.σ ⩽ σ_{1}}

and with of ∀_{⩽}-right on the right

Σ ⊢ σ ⩽ τ Σ, a⩽τ_{1} ⊢ τ ⩽ [a/α]σ_{1} (a does not occur in τ or σ_{1}) Σ ⊢ τ ⩽ ∀α⩽τ_{1}.σ_{1}
Σ ⊢ σ ⩽ ∀α⩽τ_{1}.σ_{1}

which becomes

Σ, a⩽τ_{1} ⊢ σ ⩽ τ Σ, a⩽τ_{1} ⊢ τ ⩽ [a/α]σ_{1}

Σ, a ⩽ τ_{1} ⊢ σ ⩽ [a/α]σ_{1} a does not occur in σ or σ_{1}

Σ ⊢ σ ⩽ ∀α⩽τ_{1}.σ_{1}

the use of weakening and the alteration of the side-condition from a not occurring in τ to a not occurring in σ being justified by the fact that since a does not occur in Σ, nor can it in σ.

In either case, we have a use of transitivity at the base of a derivation of smaller size, so we can use the induction hypothesis. (The cases for ∀-left on the left and ∀-right on the right are similar.) □

5.1.8 Definition: Suppose Σ = Σ_{1}, a(⩽τ_{1}), Σ_{2}, and we have τ ∈ τ(Σ_{1}) such that if Σ^{↑}(a)↓ then Σ_{1} ⊢ τ ⩽ τ_{1} by a Trans-free derivation. We define ⟨τ/a⟩Σ to be Σ_{1}, [τ/α]Σ_{2}.

5.1.9 Lemma: Suppose there exists a Trans-free derivation of Σ ⊢ σ_{1} ⩽ σ_{2}. Then for any τ such that ⟨τ/a⟩Σ is defined, there is a Trans-free derivation of ⟨τ/a⟩Σ ⊢ [τ/a]σ_{1} ⩽ [τ/a]σ_{2}.

Proof: Consider a derivation for some Σ ⊢ σ_{1} ⩽ σ_{2}, such that the result holds for all derivations of lesser height. We proceed by cases on the last rule in the derivation.

Refl Straightforward

Arrow by the induction hypothesis.

Cons by the induction hypothesis.

Prom^{↑} If the base type σ_{1} is other than a, then it follows from the induction hypothesis that there is a Trans-free proof of ⟨τ/a⟩Σ ⊢ [τ/a](Σ^{↑}(b)) ⩽ [τ/a]σ_{2}, and thus since (⟨τ/a⟩Σ)(b) = [τ/a](Σ^{↑}(b), there is one of ⟨τ/a⟩Σ ⊢ b ⩽ [τ/a]σ_{2}. So suppose σ_{1} = a, and that Σ^{↑}(a) = τ′. Then the rule contains the premise Σ ⊢ τ′ ⩽ σ_{2}, and so by the induction hypothesis (and since a does not occur in τ′) we have a proof of ⟨τ/a⟩Σ ⊢ τ′ ⩽ [τ/a]σ_{2}. We can apply lemma(5.1.7) to this together with the proof of Σ_{1} ⊢ τ ⩽ τ′ to obtain a Trans-free proof of the required judgement.

∀_{⩽}-left Suppose the last rule is

\frac{Σ ⊢ [τ_{2}/β]σ_{1} ⩽ σ_{2} Σ ⊢ τ_{2} ⩽ τ_{1}}{Σ ⊢ ∀β⩽τ_{1}.σ_{1} ⩽ σ_{2}}

By the induction hypothesis, we have proofs of ⟨τ/a⟩Σ ⊢ [τ/a][τ_{2}/β]σ_{1} ⩽ [τ/a]σ_{2} and ⟨τ/a⟩Σ ⊢ [τ/a]τ_{1} ⩽ [τ/a]τ_{1}. But since [τ/α][τ_{2}/β]σ_{1} = [([τ/α]τ_{2})/β][τ/α]σ_{1}, we may apply ∀_{⩽}-left to obtain the required proof.

∀-left Similarly.

∀_{⩽}-right Suppose the last rule is

\frac{Σ, b⩽τ_{2} ⊢ σ_{1} ⩽ [b/β]σ_{2} b does not occur in σ_{1}}{Σ ⊢ σ_{1} ⩽ ∀β⩽τ_{2}.σ_{2}}

since a occurs in Σ, b ≠ a. Then by the induction hypothesis we have a proof of ⟨τ/a⟩Σ, b⩽[τ/a]τ_{2} ⊢ [τ/a]σ_{1} ⩽ [τ/a][b/β]σ_{2}. But since β does not occur in τ, [τ/a][b/β]σ_{2} = [b/β][τ/a]σ_{2}, so an application of ∀_{⩽}-right suffices.

∀-right Similarly. □

5.1.10 Lemma [Full transitivity]: if there is a derivation of Σ ⊢ σ_{1} ⩽ σ_{2}, then there is a Trans-free derivation.

Proof: Suppose we have a derivation of Σ ⊢ σ_{1} ⩽ σ_{2} where the final rule is Trans with mediating type σ_{3}, and the subderivations are Trans-free. We define an ordering on such derivations to be (q, s) ordered lexicographically, where q is the depth of quantifier nesting of σ_{3} and s is the size of the derivation. Suppose all such derivations of measure less than (q, s) can be rewritten to be transitivity-free.

Consider the possibilities for the last rule in the subderivations: almost all cases are straightforward using the induction hypothesis in the manner of lemma(5.1.7). The interesting combinations are when we have ∀_{⩽}-right on the left and ∀_{⩽}-left on the right, or ∀-right on the left and ∀-left on the right, the two cases being essentially similar. Suppose then the rule is

{Σ, a⩽τ_{3} ⊢ σ_{1} ⩽ [a/α]σ_{3} \atop Σ ⊢ σ_{1} ⩽ ∀α⩽τ_{3}.σ_{3}} \begin{matrix}Σ ⊢ [τ/α]σ_{3} ⩽ σ_{2} & Σ ⊢ τ ⩽ τ_{3} \\ Σ ⊢ ∀α⩽τ_{3}.σ_{3} ⩽ σ_{2} \end{matrix}
Σ ⊢ σ_{1} ⩽ σ_{2}

where a does not occur in σ_{1} or σ_{3}

Now ⟨τ/a⟩Σ = Σ, so we may apply lemma(5.1.9) to obtain a Trans-free derivation of Σ ⊢ [τ/a]σ_{1} ⩽ [τ/a][a/α]σ_{3}. But since a does not occur in σ_{1}, this is just Σ ⊢ σ_{1} ⩽ [τ/α]σ_{3}. Now we may rewrite the derivation above as

Σ ⊢ σ_{1} ⩽ [τ/α_{3}]σ_{3} Σ ⊢ [τ/α]σ_{3} ⩽ σ_{2} Σ ⊢ σ_{1} ⩽ σ_{2}

where the subderivations to the Trans are Trans-free, and the size of the rule is (q − 1, n′) since the depth of quantifier nesting in [τ/α]σ_{3} is less than that of ∀α⩽τ_{3}.σ_{3}. The result follows from the induction hypothesis. □

5.1.11 Definition [Canonical Derivations]: A derivation in the canonical proof system is canonical if it contains no instance of Trans, and no instance of ∀-right or ∀_{⩽}-right directly above an instance of Prom^{↑}, ∀-left′, or ∀_{⩽}-left′.

5.1.12 Theorem: Suppose there is a derivation of Σ ⊢ σ_{1} ⩽ σ_{2}. Then there is a canonical derivation.

Proof: By transitivity elimination, then simple permutation of the rules. □

The notable property of a canonical derivation is that the choice of the last rule in a canonical derivation of Σ ⊢ σ_{1} ⩽ σ_{2} is exactly determined by σ_{1} and σ_{2}. However, this does not enable us to derive the premises of the rule in all cases; in particular, in the algorithmic equivalent of the ∀-left rules, we are unable to derive the substitution term. A similar phenomenon is exhibited, for example, in the typed λ-calculus, where the type of the bound variable in an application term is lost.

Algorithmic Subtyping

In order to perform type inference in the ML style, we need to be able to address the question of how we might decide the subtype relation between two terms containing free variables. Suppose we have Σ = Nat, Int ⩽ Nat, the assertion

Σ ⊢ ∀α.γ→α ⩽ ∀β⩽Nat.β→Int→β

with γ a free variable which can be instantiated with any type in τ(Σ). Now if there is a substitution τ_{γ} for γ in τ(Σ) such that the above holds, using our knowledge about canonical derivations, the last rule in such a derivation must be ∀_{⩽}-right, whose premise must be

Σ, b⩽Nat ⊢ ∀α.τ_{γ}→α ⩽ b→Int→b

and the one preceding that must be ∀-left′. Suppose the substitution used in that rule for α is τ_{α} ∈ τ(Σ, b⩽Nat), then the premise is

Σ, b⩽Nat ⊢ τ_{γ}→τ_{α} ⩽ b→Int→b

Now the preceding rule must be Arrow, so we have two sequents.

Σ, b⩽Nat ⊢ b ⩽ τ_{γ} and Σ, b⩽Nat ⊢ τ_{α} ⩽ Int→b

Let us consider the first of these. Since τ_{γ} cannot be b, (by scoping restrictions) the last rule in the derivation must be a Prom^{↑} rule. So we must have Σ, b⩽Nat ⊢ Nat ⩽ τ_{γ}. Such a derivation cannot involve b, so it must be the case that Σ ⊢ Nat ⩽ τ_{γ}.

Now we consider the second. By syntactic examination of the rules, the last one can only be Prom^{↑} (if τ_{α} is a Σ-atom) or Arrow (if τ_{α} is an arrow type). Now suppose τ_{α} is a Σ-atom, then after a sequence of promotions, we must reach an arrow type, say τ_{1}→τ_{2}, with the property that Σ, b⩽Nat ⊢ τ_{2} ⩽ b, So τ_{2} must be b. Therefore, we must have in Σ an atom whose bound is of the form τ_{1}→b, which is impossible by well-formedness of Σ, b⩽Nat, so τ_{α} must be an arrow type. Substituting τ_{1}→τ_{2} for α, and decomposing using an arrow rule, we have

Σ, b⩽Nat ⊢ Int ⩽ τ_{1} and Σ, b⩽Nat ⊢ τ_{2} ⩽ b

Evidently the only possibility for τ_{2} is b. And for any value of τ_{1} such that Σ, b⩽Nat ⊢ Int ⩽ τ_{1}, we have Σ ⊢ Int ⩽ τ_{1}. So the inequality holds iff we can find τ_{γ}, τ_{1} with the property that Σ ⊢ Nat ⩽ τ_{γ} and Σ ⊢ Int ⩽ τ_{1}.

In order to regularise this kind of reasoning, we need a way of dealing with the variables which appear in several sequents, and thus a way of reasoning with many sequents at once. To proceed more formally, we introduce quantified subtype problems, similar to unification problems. The above inequality is represented by the QSP

             ∀Int∀Nat⩽Int∃γ∀b⩽Nat∃α. γ→α⩽b→Int→b

In order to deal with sequents that contain variables, we must modify slightly our notion of what constitutes a well-formed sequent: we will now allow types to contain free variables (i.e. variables not occurring in the domain of the signature), but in order to ensure that substitution instances of such sequents are well-formed, we annotate free variables with an initial (non-empty) segment of the signature. If such a variable occurs in Σ, it may only do so after the segment with which it is annotated.

Given a sequent Σ ⊢ τ_{1} ⩽ τ_{2}, a well-scoped atomic substitution [τ/α_{Σ′}] will be such that any variable in τ is either in dom(Σ′), or β_{Σ′′} for some Σ′′ a nonempty initial segment of Σ′. A well-scoped substitution will be one composed of well-scoped atomic substitutions.

We will also define substitutions on base types: for a sequent D ≡ Σ ⊢ τ_{1} ⩽ τ_{2}, if ⟨τ/a⟩ is defined, we define [τ/a]D = ⟨τ/a⟩Σ ⊢ [τ/a]τ_{1} ⩽ [τ/a]τ_{2}.

The scoping issues here are more complicated than mixed-prefix unification. In that case, a scope check occurs exactly if some existentially bound variable is bound to a term containing a universally quantified variable such that the existential quantifier is not within the scope of the universal. In the current situation, it may be possible to resolve such an occurrence by promotion (such as in the b ⩽ τ_{γ} case) and thus a scope check will not occur.

5.2 Leaves and Essential Occurrences

Before proceeding, we shall investigate further properties of the subtyping system which generalise and strengthen the ad-hoc deductions used above. Our first action will be to decompose an inequality between type schemes into inequalities between types, creating new variables to correspond to substituted values in ∀-left′ rules. Once we have done this, our attention naturally focuses on the quantification-free fragment of the proof system.

5.2.1 Remark: Suppose we have a derivation of Σ ⊢ τ_{1} ⩽ τ_{2}, then we have a Trans-free derivation. A simple induction suffices to show that this derivation uses only the rules Refl, Cons, Prom^{↑}, Trans, and the Arrow

Our strategy proceeded by removing the binding b⩽Nat from the signature, by solving for those variables which had to contain b, and promoting b to Nat in other constraints. We were able to deduce from Σ, b⩽Nat ⊢ τ_{2} ⩽ b that τ_{2} could only take the value b. This was possible because b was guaranteed not to occur as the bound of another Σ-atom. We will call a Σ-atom with this property a leaf. We also used the result that if τ ⩽ τ′, and τ′ is a constructor type in which b occurs positively, then τ must also be a constructor type in which b occurs positively. We will generalise this latter result to occurrences of b which we shall call essential.

5.2.2 Definition: a ∈ atom(Σ) is a leaf if it does not occur in the range of Σ. We write Σ\_{a} for Σ with the binding for a removed; it is easy to see that if Σ is a valid signature, so is Σ\_{a}.

The following are straightforward:

5.2.3 Proposition: Suppose a is a leaf in Σ

Weakening If Σ\_{a} ⊢ τ_{1} ⩽ τ_{2}, then Σ ⊢ τ_{1} ⩽ τ_{2}.

Strengthening If Σ ⊢ τ_{1} ⩽ τ_{2}, and a does not occur in τ_{1} or τ_{2}, then Σ\_{a} ⊢ τ_{1} ⩽ τ_{2}.

5.2.4 Definition [Type Shape and Size]: We need to extend the notion of type shape to deal with non-atomic base types.

S(a)  = a  if a ∈ atom(Σ) and Σ^{↑}(a)↑
S(a)  = S(Σ^{↑}(a))  if Σ^{↑}(a) = τ
S(Tτ_{1} … τ_{n}) = T S(τ_{1}) … S(τ_{n})

In order to show this definition terminates, we will use a size measure on types, an argument similar to that of [8]. Since we will use this definition later, it is given in a slightly more general form than is required here. Let Σ be a bounded signature.

size_{Σ}(α)  = 1
size_{Σ}(a)  = 1  if Σ^{↑}(a)↑ or a ∈ Q
size_{Σ}(a)  = size_{Σ}(τ)  if Σ^{↑}(a) = τ
size_{Σ}(Tτ_{1} … τ_{n}) = 1 + size_{Σ}(τ_{1}) + … + size_{Σ}(τ_{n})

5.2.5 Proposition: the computation of S_{C}(τ) terminates.

Proof: We define the index of i(a) in Σ to be the number of bindings prior to a in Σ. We define m(τ) to be (i(τ), size_{Σ}(τ)), with the usual lexicographic ordering on ℕ × ℕ. Each step in the computation of S(τ) decreases m(τ). □

5.2.6 Remark: If Σ ⊢ τ_{1} ⩽ τ_{2}, then S(τ_{1}) = S(τ_{2}).

We will assume the constraint set is shape consistent – that is, that there is a substitution θ_{S} : fv(c)→τ(atom(Σ)) such that S(θ_{S}τ_{1}) = S(θ_{S}τ_{2}) for any τ_{1} ⩽ τ_{2} ∈ C. The existence of such a substitution is clearly a necessary condition for solvability.

5.2.7 Definition [Essential Occurrence]: Suppose a is a leaf in Σ. We define a to occur essentially in τ_{1} ⩽ τ_{2} if

• Σ^{↑}(a)↓ and a occurs in τ_{1} other than positively (i.e. with positive or mixed polarity), or a occurs in τ_{2} other than negatively.

• Σ^{↑}(a)↑ and a occurs in τ_{1} or τ_{2}.

5.2.8 Lemma:

1. If Σ ⊢ a ⩽ τ and a occurs in τ, then the proof is an instance of Refl.

2. If Σ ⊢ τ_{1} ⩽ τ_{2}, and a occurs essentially in τ_{1} ⩽ τ_{2}, Then either the last rule in the derivation (and thus the whole derivation) is an instance of Refl, or a occurs essentially in one of the rule’s premises.

3. Suppose Σ ⊢ τ_{1} ⩽ τ_{2}, and a occurs essentially in τ_{1} ⩽ τ_{2}. Then a occurs in each of τ_{1} and τ_{2}.

Proof:

1. τ cannot be of the form T\overline{τ}, since it contains a, and we must have S(a) = S(τ). So τ must be a base type, and since a occurs in τ, τ = a. Then obviously the last rule in the derivation cannot be Cons. Now suppose the last rule in the proof is Prom^{↑}, then the premise of this rule cannot be of the form Σ ⊢ T\overline{τ} ⩽ a, since no rule has a consequence of this form, so it must be of the form Σ ⊢ b ⩽ a. And similarly, if the previous rule is an instance of Prom^{↑}, then its premise must be of the form Σ ⊢ c ⩽ a. So by induction the proof must consist of a sequence of zero or more instances of Prom^{↑} below an instance of Refl, and for each Prom^{↑} rule, the variable on the LHS of its premise must occur earlier in Σ than the variable on the LHS of its consequent. But the variable immediately below the instance of Refl must have a as its bound, so must occur later in Σ than a. So if there are any instances of Prom^{↑} we have an immediate contradiction.

2. If the last rule is Cons, and τ_{1} = T\overline{τ}, the result is straightforward by case analysis on the polarity of T in each of its arguments. So suppose the last rule is Prom^{↑}. If τ_{1} = a then we must have Σ^{↑}(a)↓, so an essential occurrence is exactly an occurrence in τ_{2} with positive or mixed polarity, but by part 1, a cannot occur in τ_{2} unless the derivation is an instance of Refl. So τ_{1} must be some other variable b, in which case, a must occur positively or with mixed polarity in τ_{2}, and thus it occurs essentially in Σ^{↑}(b) ⩽ τ_{2}.

3. We proceed by induction on the proof of Σ ⊢ τ_{1} ⩽ τ_{2}. Suppose the proposition is true for each of the premises in the last rule. We proceed by cases

Refl Trivially true. Prom^{↑} We will show that the last rule cannot be an instance of Prom^{↑}:

suppose it were, then τ_{1} = b, and a ≠ b by part 1. By the induction hypothesis, if a occurs essentially in Σ^{↑}(b) ⩽ τ_{2} it must occur in Σ^{↑}(b). But this cannot be the case since a is a leaf.

Cons If a occurs essentially in T\overline{τ}_{1} ⩽ T\overline{τ}_{2}, then by part 2, there must be some premise τ_{1} ⩽ τ_{2} in which it occurs essentially. Thus by the induction hypothesis it occurs in each of τ_{1} and τ_{2}, and thus in each of T\overline{τ}_{1} and T\overline{τ}_{2} □

5.2.9 Corollary: Suppose Σ ⊢ τ_{1} ⩽ τ_{2}, and a occurs essentially in τ_{1} ⩽ τ_{2}. Then 1. if one of τ_{1} and τ_{2} is a, so is the other.

2. if one of τ_{1} and τ_{2} has the form Tτ′_{1} … τ′_{n}, then the other has the form Tτ′′_{1} … τ′′_{n}.

Proof: Suppose a occurs essentially in τ_{1} ⩽ τ_{2}. Then from lemma(5.2.8(3)) we know that a must occur in each of τ_{1} and τ_{2}. Now

1. is immediate since the derivation must be an instance of Refl.

2. Note that the only other syntactically allowable possibility from the form of the rules is that τ_{1} is a base type and τ_{2} not, with the last rule in the derivation being Prom^{↑}. But since a occurs in τ_{1}, we must have τ_{1} = a, with Σ^{↑}(a)↓, and so by lemma(5.2.8(1)) a does not occur in τ_{2}. So, since Σ^{↑}(a)↓ and a occurs only positively in τ_{1} and not at all in τ_{2}, it does not occur essentially in τ_{1} ⩽ τ_{2}, contradicting the hypothesis. □

5.3 Quantified Subtype Problems

Quantified subtype problems (QSPs) bear a strong similarity to first order mixed prefix unification problems. The essential differences are that we have inequality instead of equality, that we incorporate a bounded universal quantifier, and that a quantifier prefix is replaced by a more general form of quantifier nesting. The last difference is not strictly necessary, however: we simply adopt the more flexible format in order to avoid generating additional solutions to inequalities such as e.g. β ⩽ β which might be created by moving quantifiers outwards.

We shall assume all the problems we work with are closed, i.e. every variable is in the scope of either an existential or universal quantifier which binds it, and that they begin with an unbounded universal quantifier. The latter restriction can easily be relaxed, but we shall ignore here the question of an underlying order on atomic types, and consider only the order defined by Σ. QSPs have the following syntax:

Π ::= ⊤ | τ_{1} ⩽ τ_{2} | Π ∧ Π … ∧ Π | ∀a.Π | ∀a⩽τ.Π | ∃α.Π

where ∧ represents conjunction, and ⊤ the solved problem.

It will be appropriate to view a QSP as a notation for a set of sequents for inequalities between types. For example

∀Int ∀Nat⩽Int ∃α ∀a⩽Nat ∃β .β⩽α ∧ Int⩽β ∧ β⩽a

can be viewed (writing Σ_{1} = Int, Nat⩽Int and Σ_{2} = Σ_{1}, a⩽Nat) as representing the sequents

Σ_{2} ⊢ β_{Σ_{2}} ⩽ α_{Σ_{1}}
Σ_{2} ⊢ Int ⩽ β_{Σ_{2}}
Σ_{2} ⊢ β_{Σ_{2}} ⩽ a

We define the set of sequents S(Π) corresponding to a subtyping problem to be S(Π)_{•}, where S(Π)_{Σ} is as follows:

S(⊤)_{Σ}  = ∅
S(τ_{1}⩽τ_{2})_{Σ}  = Σ ⊢ τ_{1} ⩽ τ_{2}
S(Π_{1} ∧ … ∧ Π_{n})_{Σ} = S(Π_{1})_{Σ} ∪ … ∪ S(Π_{n})_{Σ}
S(∀a.Π)_{Σ}  = S(Π)_{Σ,a}
S(∀a⩽τ.Π)_{Σ}  = S(Π)_{Σ,a⩽τ}
S(∃α.Π)_{Σ}  = S([α_{Σ}/α]Π)_{Σ}

When we wish to refer to a subproblem Π′ occurring in a context P within Π, we will write Π = P {Π′}. We will write Σ_{P} for the environment built up from the universal quantifiers in P in whose scope is the “hole” in which Π occurs, and Σ_{Π} to denote the environment corresponding to a the context in which Π occurs – (the two notions differ in the case of e.g. P {∀α.Π})

5.3.1 Definition [Substitution]: For α existentially quantified in Π, we define the atomic substitution [τ/α]Π as follows: let \overline{β} be the set of variables in τ not bound in Π. Then [τ/α]Π is the problem obtained by substituting τ for α in Π and replacing the existential quantifier for α by existential quantifiers for \overline{β}. Such an operations is well-scoped if every occurrence of β is within the scope of every variable in τ which occurs in Π.

For example, [γ→a/β] is well-scoped for the problem

∀a∃β.β ⩽ a→a

and substitution yields the problem

∀a.∃γ.γ→a ⩽ a→a

For a universally quantified, if Π = P {∀a(⩽ τ).Π_{1}}, we define [τ′/a]Π to be P {Π′_{1}}, where Π′_{1} is obtained by replacing all occurrences of α by τ. We will only allow substitutions such that [τ/a]Π is well-formed. A substitution θ on Π is a composition of well-scoped atomic substitutions.

5.3.2 Proposition: Suppose θ is a substitution on Π. Then θ is well-formed on the sequents in S(Π), and (S(θΠ)) = θ(S(Π)).

5.3.3 Definition [Solution of a QSP]: A solution θ of a QSP Π is a ground substitution such that all the sequents in S(θΠ) are provable.

For Example, the QSP

              ∀Int∀Nat⩽Int∃γ∀a⩽Nat∃α.γ→α⩽a→Int→a

has solutions [Int→a/α] ∘ [Int/γ] and [Int→a/α] ∘ [Nat/γ].

5.4 Transitions

We will solve QSPs by using transitions: each transition will preserve wellformedness of the QSP. There are three kinds of transitions: flattening, binding, and projection. Flattening is the process of applying all the possible decompositions between types whose applicability is immediately obvious from the form of the types in question. Binding uses corollary(5.2.9) to justify variable substitutions which simplify the problem, and binding removes redundant quantifiers from the problem.

Flattening We define Π^{♭} = Π_{•}^{♭}.

(∀a.Π)_{Σ}^{♭}  = ∀a.(Π)_{Σ,a}^{♭}
(∀a⩽τ.Π)_{Σ}^{♭}  = ∀a⩽τ.(Π)_{Σ,a⩽τ}^{♭}
(∃α.Π)_{Σ}^{♭}  = ∃α.(Π)_{Σ}^{♭}
(Π_{1} ∧ … ∧ Π_{n})_{Σ}^{♭}  = (Π_{1})_{Σ}^{♭} ∧ … ∧ (Π_{n})_{Σ}^{♭}
(τ ⩽ τ)_{Σ}^{♭}  = ⊤
(Tτ_{1} … τ_{n} ⩽ Tτ′_{1} … τ′_{n})_{Σ}^{♭} = (τ_{1} ⋄_{1}^{T} τ′_{1})_{Σ}^{♭} ∧ … ∧ (τ_{n} ⋄_{n}^{T} τ′_{n})_{Σ}^{♭}
(a ⩽ τ)_{Σ}^{♭}  = (Σ^{↑}(a) ⩽ τ)_{Σ}^{♭}  Σ^{↑}(a)↓, τ ≠ a
(α ⩽ τ)_{Σ}^{♭}  = α ⩽ τ
(τ ⩽ α)_{Σ}^{♭}  = τ ⩽ α
(τ_{1} ⩽ τ_{2})_{Σ}^{♭}  = Fail  other τ_{1}, τ_{2}

We will call Π flat if Π^{♭} = Π.

5.4.1 Proposition: (τ_{1} ⩽ τ_{2})^{♭} terminates.

Proof: It suffices to demonstrate termination of (τ_{1} ⩽ τ_{2})^{♭}, which we shall do by well-foundedness of the sum of size_{Σ}(τ_{1}) + size_{Σ}(τ_{2}). If τ_{1} or τ_{2} is a variable or τ_{1} = τ_{2}, the result is immediate. The case where both are constructor types results in a decomposition into inequalities of strictly smaller size. Finally suppose we are flattening a ⩽ τ where τ is not a variable. If τ is b by a finite sequence of promotions either flattening fails or the constraint disappears. Otherwise, τ is of the form Tτ_{1} … τ_{n}, and a must eventually either promote to Tτ′_{1} … τ′_{n} whence we proceed as in the constructor-constructor case, or promote to a base type with no upper bound, or constructor type with a constructor other than T, in which case flattening fails. □

Binding Let Π = P {∀a(⩽τ)Π′}, where Π′ is flat and contains some constraint π in which a occurs essentially. Since Π′ is flat, there are only four possibilities for the form of π, and each of them gives rise to a substitution: If the substitution variable is existentially bound outside Π′, there is no solution to the problem.

π  substitution
β ⩽ Tτ_{1} … τ_{n} [Tβ_{1} … β_{n}/β]
Tτ_{1} … τ_{n} ⩽ β [Tβ_{1} … β_{n}/β]
β ⩽ a  [a/β]
a ⩽ β  [a/β]

where all the β_{i} are fresh.

Projection Let Π = P {∀α(⩽τ)Π′}, where Π′ is flat and contains no constraint

π in which a occurs essentially. The transition is as follows:

Old  New
P {∀a.Π′}  P {Π′}
P {∀a⩽τ.Π′} {[τ/a]Π′}

We demonstrate the use of these transitions on the problem seen earlier:

                    ∀Int ∀Nat⩽Int ∃γ ∀a⩽Nat ∃α. γ→α ⩽ a→Int→a

flatten  ⇒ ∀Int ∀Nat⩽Int ∃γ ∀a⩽Nat ∃α. a ⩽ γ ∧ α ⩽ Int→a
bind [α_{1}→α_{2}/α] ⇒ ∀Int ∀Nat⩽Int ∃γ ∀a⩽Nat ∃α_{1}, α_{2}. a⩽γ
∧ α_{1}→α_{2} ⩽ Int→a
flatten  ⇒ ∀Int ∀Nat⩽Int ∃γ ∀a⩽Nat ∃α_{1}, α_{2}. a⩽γ
∧ Int ⩽ α_{1} ∧ α_{2} ⩽ a
bind [a/α_{2}]  ⇒ ∀Int ∀Nat⩽Int ∃γ ∀a⩽Nat ∃α_{1}. a⩽γ
∧ Int ⩽ α_{1} ∧ a ⩽ a
flatten  ⇒ ∀Int ∀Nat⩽Int ∃γ ∀a⩽Nat ∃α_{1}. a ⩽ γ ∧ Int ⩽ α_{1}
project a  ⇒ ∀Int ∀Nat⩽Int ∃γ, α_{1}. Nat ⩽ γ ∧ Int ⩽ α_{1}
project Nat  ⇒ ∀Int ∃γ, α_{1}. Int ⩽ γ ∧ Int ⩽ α_{1}

5.5 Soundness and Completeness

In order to demonstrate that we can decide this subtyping relation, we will demonstrate that the transitions form a sound and complete system for solving QSPs.

5.5.1 Definition: A QSP is trivial if there are no constrained quantifiers

If all subtyping is generated from Σ, then all trivial QSPs can be solved directly by unification. We can generalise this condition to the case, for example, where Σ extends some underlying Helly poset of atomic types; once the QSP is reduced to triviality, we can use the usual mechanisms for solvability and solution over Helly posets.

Soundness and completeness mean that given a problem Π, a finite sequence of transitions will completely reduce the QSP to a trivial QSP if and only if the problem has a solution. The definitions and results given here will enable us to prove a stronger property of factorisation in the next chapter.

The property that we need for this is: given a problem P {Π_{1}} which is transformed to P {Π_{2}} by some sequence of transitions, for any solution θ_{1} of P {Π_{1}} there will be a corresponding solution θ_{2} of P {Π_{2}} which agrees with θ_{1} on the variables existentially quantified in P outside of Π_{1}.

We will demonstrate this strong form of completeness by considering each of the transitions in turn.

Flattening

5.5.2 Lemma: Suppose Π is a subtyping problem

1. if Π has a solution, then flattening succeeds on Π.

2. θ is a solution of Π^{♭} iff it is a solution of Π.

Proof:

1. The cases for τ_{1} ⩽ τ_{2} in which flattening can fail are exactly those where neither of τ_{1} and τ_{2} is an existentially quantified variable, and either they have different shapes, or they are both Σ-atoms such that τ_{2} cannot be obtained by a sequence of promotions from τ_{1}. Thus there can be no solution to Π.

2. Proofs of the sequents in S(θΠ) can be obtained from those S(θ(Π^{♭})) just by use of the inference rules, and similarly if θ is a solution to Π, a canonical proof of a sequent in S(Π) must (by consideration of shape) end with the inference rule used in flattening that sequent. □

Binding

5.5.3 Lemma: Suppose P {Π_{1}} is transformed to P {Π_{2}} by a binding transition using a substitution [τ/β]. Let V be the set of variables other than β which are existentially bound in P {Π_{1}}.

1. if θ_{1} is a solution of P {Π_{1}}, then there is some solution θ_{2} of P {Π_{2}} such that θ_{1}|_{V} = θ_{2}|_{V}

2. if θ_{2} is a solution of P {Π_{2}}, there is a solution θ_{1} of P {Π_{1}} such that θ_{1}|_{V} = θ_{2}|_{V}

Proof:

1. Suppose the binding transition arises from an essential occurrence of a in a constraint of the form β ⩽ Tτ_{1} … τ_{n}, and θ_{1} is a solution of P {Π_{1}}. In this case Σ, a(⩽τ_{0}) ⊢ θ_{1}β ⩽ T(θ_{1}τ_{1}) … (θ_{1}τ_{n}), and since a occurs essentially in T(θ_{1}τ_{1})→(θ_{1}τ_{n}) we know from lemma(5.2.9(2)) that θ_{1}β must be Tτ′_{1} … τ′_{n}. Now τ is of the form Tβ_{1} … β_{n} for fresh variables β_{i}. So θ_{1}|_{V} ∘ [τ′_{i}/β_{i}] is a solution of P {[τ/β]Π_{1}}.

2. By similar reasoning, if θ_{2} is a solution of P {[Tβ_{1} … β_{n}/β]Π_{1}}, and the substitution comes from a binding transition, then θ_{2}|_{V} ∘ [θ_{2}(Tβ_{1} … β_{n})/β] is a solution to P {Π_{1}}.

The cases for the form of constraint β ⩽ a are straightforward. □

Projection

We need the following preliminary lemma:

5.5.4 Lemma: Suppose Σ is well-formed. Let a be a leaf in Σ, Σ^{↑}(a) = τ′

1. if a occurs only negatively in τ, Q, Σ ⊢ τ ⩽ [τ′/a]τ

2. if a occurs only positively in τ, Q, Σ ⊢ [τ′/a]τ ⩽ τ.

Proof: We proceed by simultaneous induction on the structure of τ. Suppose τ is a type constant, b. If a ≠ b, the result is obvious. And if a = b, we must be in case (1), and a proof of Σ, a⩽τ′ ⊢ a ⩽ τ′ is immediate by use of Prom^{↑}. Otherwise τ is a constructor type, and thus so is [τ′/a]τ; the result follows by decomposition by Cons and the induction hypothesis. □

5.5.5 Lemma: Suppose P {Π_{1}} is transformed to P {Π_{2}} by a projection transition.

1. Let V be the set of variables existentially bound in the context P. If θ_{1} is a solution of P {Π_{1}}, then there is some solution θ_{2} of P {Π_{2}} such that θ_{1}|_{V} = θ_{2}|_{V}

2. if θ_{2} is a solution of P {Π_{2}}, there θ_{2} is a solution of P {Π_{1}}.

Proof:

1. Suppose θ_{1} is a solution to P {∀a.Π}, it suffices to show that [τ/a] ∘ θ_{1} is a solution to P {Π} for any τ ∈ τ(Σ_{P}). This follows by induction on the derivation of any sequent in S(θ_{1}(P {∀a.Π})): every rule containing a is either Cons, Prom^{↑} on a base type other than a, or Refl on a. In the bounded case, where θ_{1} is a solution to P {∀a⩽τ.Π} and a does not occur essentially in Π, [τ/a]∘θ_{1} is again a solution to P {[τ/a]Π}: again this follows by induction on the derivation. In either case, the new substitution must agree with θ_{1} on the existentially quantified variables in P, since these can contain no occurrences of a.

2. The unbounded case is simple: Suppose θ_{2} is a solution to P {Π}. Then by weakening, it’s also a solution to P {∀a.Π}. The bounded case is slightly more difficult: suppose θ_{2} is a solution to P {[τ/a]Π}, and we have a sequent θ_{2}Σ_{P} ⊢ θ_{2}([τ/a]τ_{1}) ⩽ θ_{2}([τ/a]τ_{2}) corresponding to some inequality τ_{1} ⩽ τ_{2} in Π. We know that

θ_{2}Σ_{P} , a⩽θ_{2}τ ⊢ θ_{2}([τ/a]τ_{1}) ⩽ θ_{2}([τ/a]τ_{2})

Now since θ_{2}(α) does not contain a for any α, θ_{2} ∘ [τ/a] = [θ_{2}τ/a] ∘ θ_{2}, so by rearranging substitutions we have:

θ_{2}Σ_{P} , a⩽θ_{2}τ ⊢ [θ_{2}τ/a](θ_{2}τ_{1}) ⩽ [θ_{2}τ/a](θ_{2}τ_{2})

Now from lemma(5.5.4) we get

θ_{2}Σ_{P} , a⩽θ_{2}τ ⊢ θ_{2}τ_{1} ⩽ [θ_{2}τ/a](θ_{2}τ_{1})

and

θ_{2}Σ_{P} , a⩽θ_{2}τ ⊢ [θ_{2}τ/a](θ_{2}τ_{2}) ⩽ θ_{2}τ_{2}

Two uses of Trans yield the required proof. □

5.5.6 Theorem: Suppose P {Π_{1}} is transformed to P {Π_{2}} by a sequence of transitions. Let V be the set of variables existentially bound in P.

1. if θ_{1} is a solution of P {Π_{1}}, there is a solution θ_{2} of P {Π_{2}} such that θ_{1}|_{V} = θ_{2}|_{V}.

2. if θ_{2} is a solution of P {Π_{2}}, there is a solution θ_{1} of P {Π_{1}} such that θ_{1}|_{V} = θ_{2}|_{V}.

Proof: By induction on the length of the transition sequence, by cases on the last transition in the sequence. Suppose P {Π_{1}} is transformed to P {Π_{2}}, such that the results hold for P {Π_{2}}, and P {Π_{2}} undergoes a transition to P {Π_{3}}. We will proceed by cases on the type of transition.

flattening θ_{2} is a solution to P {Π_{3}} iff it is a solution to P {Π_{2}}.

binding Suppose the binding is a substitution θ′.

1. Note that the variable being substituted cannot be in V. Thus by lemma(5.5.3(1)), there’s a solution θ_{3} of P {Π_{3}} such that θ_{3}|_{V} = θ_{2}|_{V}. The result follows from the induction hypothesis.

2. Similarly, using lemma(5.5.3(2))

projection Similarly, using lemma(5.5.5). □

5.6 Termination

So we see that transition sequences are sound and complete: it remains to demonstrate how a sequence of transitions may be applied to solve a QSP. Our strategy will be based on a step we will call elimination (because after the transitions which make up each such step, a universal quantifier will have been eliminated from the problem). To eliminate an innermost universal quantifier from P {∀a(⩽τ).Π} we flatten Π, eliminate all essential occurrences of a using alternating binding and flattening transitions, then remove the binder with a projection transition.

It suffices to show that no infinite sequence of flattening and binding transitions can occur. We will say Π is shape consistent if the corresponding mixedprefix unification problem on shapes has some solution θ_{S}. We will demonstrate that if Π is shape consistent, no such circularities will occur. Obviously if Π is not shape consistent, then it is not solvable.

5.6.1 Proposition: If C is shape consistent, there can be no infinite sequence of substitutions and flattenings.

Proof: If C is a set of inequalities, we define size_{Σ}(C) to be the sum of sizes of the types occurring in the inequalities in C. Let θ_{S} be as above, and θ be a binding substitution.

If θ = [a/α], we must have S(θ_{S}(a)) = S(α) for any θ_{S}, and we define θ′_{S} = θ_{S}. Otherwise θ = [Tβ_{1} … β_{n}/α], and so we must have S(θ_{S}(Tβ_{1} … β_{n})) = S(α), thus we have an extension θ′_{S} of θ_{S} such that θ′_{S}(Tβ_{1} … β_{n}) = T(θβ_{1}) … (θβ_{n}), and θ′_{S} is also a a solution of the shape unification problem.

Now size_{Σ}(θ_{S}C) = size_{Σ}(θ′_{S}(θC)) > size_{Σ}(θ_{S}(θ(C)^{♭})), since flattening always either removes a constraint a ⩽ a or decomposes a constraint with Cons.

Thus each sequence of substitutions θ^{i} gives rise to a sequence of substitutions θ_{S}^{i} such that

size_{Σ}(θ_{S}^{i+1}((θ^{i+1}C)^{♭})) < size_{Σ}(θ_{S}^{i}((θ^{i}C)^{♭}))

The result follows by well-foundedness. □

5.6.2 Proposition: An elimination step terminates, and if Π has a solution, no elimination step on Π will fail.

Proof: Since there can be no infinite sequence of binding and flattening transitions, the elimination step must terminate. By theorem(5.5.6), each transition in the elimination process preserves solvability. The only transition which can fail is a flattening transition, but by lemma(5.5.2) flattening cannot fail on a solvable QSP. □

To solve a problem, we apply elimination steps until the problem is reduced to a normal first-order unification problem, then apply standard techniques. Notice the similarity with mixed-prefix unification. There are three possible ways in which unification may fail: constructors may mismatch, we may attempt to bind an existentially quantified variable to a type containing a universally quantified variable which is not in its scope, or we may discover circularity in an inequality (or collection thereof) of the form β ⩽ τ with β occurring non-trivially in τ. The first of these corresponds to failure of a flattening transition, the second failure of a binding transition, and the third to an infinite sequence of binding and flattening transitions.

5.7 Transitions and Proofs

As discussed earlier, a QSP can be seen as a set of sequents, and the solution process as a decomposition of the sequents in this set using proof rules and substitutions. It is perhaps worth making explicit the connection between the solution of a QSPs and the generation of a proof.

5.7.1 Definition: Given sets of sequents S_{1} and S_{2}, we say S_{1} builds S_{2} and write S_{1} ⊩ S_{2} if, given proofs of the sequents in S_{1}, we can construct proofs of the sequents in S_{2} using the inference rules between types and the admissible rules Weaken and Weaken_{⩽}.

\frac{Σ ⊢ σ_{1} ⩽ σ_{2} a does not occur in σ_{1} or σ_{2}}{Σ, a ⊢ σ_{1} ⩽ σ_{2}}  (Weaken)

\frac{Σ ⊢ σ_{1} ⩽ σ_{2} a does not occur in σ_{1} or σ_{2} all base types in τ occur in Σ}{Σ, a⩽τ ⊢ σ_{1} ⩽ σ_{2}}  (Weaken_{⩽})

5.7.2 Remark: ⊩ is transitive.

5.7.3 Proposition: Suppose S_{1} ⊩ S_{2}, and θ is a substitution such that all the sequents in θS_{2} are well-formed. Then all the sequents in θS_{1} are well-formed, and θS_{1} ⊩ θS_{2}.

Proof: By induction on the proof rules. □

5.7.4 Lemma: Suppose P {Π_{1}} is transformed to P {Π_{2}} by a sequence of transitions. Then there is a substitution θ on the existential variables of Π_{1} such that S(P {Π_{1}}) ⊩ S(P (θΠ_{1})).

Proof: The proof is by induction on the transition sequence. Suppose we have a substitution θ such that S(Π_{2}) ⊩ S(θΠ_{1}), and Π_{2} is transformed to Π_{3} by a transition. We proceed by cases:

flattening S(Π_{3}) ⊩ S(Π_{2}), so the result is obvious.

binding Suppose the binding transition is the substitution θ′. Then S(Π_{3}) ⊩ S(θ′Π_{2}), and thus S(Π_{3}) ⊩ S((θ′ ∘ θ)Π_{1}).

projection The unbounded case is a straightforward: uses of weakening suffice to prove that S_{3} ⊩ S_{2}. In the bounded case, for any sequent in S_{2} Σ, a⩽τ ⊢ τ_{1} ⩽ τ_{2} we can construct a proof from Σ ⊢ [τ/a]τ_{1} ⩽ [τ/a]τ_{2} in a similar fashion to lemma (5.5.5)(ii)

In the latter two cases, only variables inside Π_{1} are affected by the substitution, so θ is always restricted to the variables in Π_{1}. □

5.8 An Algorithm for Subtyping

Having developed the theory of quantified subtyping problems, it remains to show how a subtyping query may be transformed into one. We will define two transformations, one for the signature and one for the problem, and compose the results to obtain the QSP. The prefix C(Σ) corresponding to a signature Σ is

C(•)  = •  (The empty prefix)
C(Σ, a)  = C(Σ)∀a
C(Σ, a⩽τ) = C(Σ)∀a⩽τ

The problem I(σ_{1} ⩽ σ_{2}) corresponding to the inequality σ_{1} ⩽ σ_{2} is

I(σ_{1} ⩽ ∀α.σ_{2})  = ∀a.I(σ_{1} ⩽ [a/α]σ_{2})  a fresh
I(σ_{1} ⩽ ∀α⩽τ.σ_{2})  = ∀a⩽τ.I(σ_{1} ⩽ [a/α]σ_{2})  a fresh
I(∀α.σ_{1} ⩽ σ_{2})  = ∃α.I(σ_{1} ⩽ σ_{2})  α ∉ fv(σ_{2}), σ_{2} not

outermost quantified

I(∀α⩽τ.σ_{1} ⩽ σ_{2}) = ∃α.α ⩽ τ ∧ I(σ_{1}, σ_{2}) α ∉ fv(σ_{2}), σ_{2} not

outermost quantified

I(σ_{1}→σ_{2} ⩽ σ′_{1}→σ′_{2}) = I(σ′_{1} ⩽ σ_{1}) ∧ I(σ_{2} ⩽ σ′_{2}) if one of σ_{1}→σ_{2}, σ′_{1}→σ′_{2}

is not a type

I(α ⩽ σ_{1}→σ_{2}) = ∃α_{1}, α_{2}.α ⩽ α_{1}→α_{2} if σ_{1}→σ_{2} is not a type
∧ I(σ_{1} ⩽ α_{1}) ∧ I(α_{2} ⩽ σ_{2})
I(σ_{1}→σ_{2} ⩽ α) = ∃α_{1}, α_{2}.α_{1}→α_{2} ⩽ α σ_{1}→σ_{2} is not a type
∧ I(σ_{1} ⩽ α_{1}) ∧ I(α_{2} ⩽ σ_{2})
I(τ_{1} ⩽ τ_{2})  = τ_{1} ⩽ τ_{2}
I(σ_{1} ⩽ σ_{2})  = Fail  otherwise

The problem P(Σ, σ_{1}, σ_{2}) is C(Σ)I(σ_{1} ⩽ σ_{2}).

5.8.1 Remark: P(Σ, σ_{1} ⩽ σ_{2}) is a well-formed, closed quantified subtype problem

5.8.2 Lemma:

1. Suppose Σ ⊢ σ_{1} ⩽ σ_{2}. Then P(Σ, σ_{1}, σ_{2}) exists, and has a solution θ.

2. Suppose θ is a solution of P(Σ, σ_{1}, σ_{2}). Then θ(S(P(Σ, σ_{1}, σ_{2}))) ⊩ Σ ⊢ σ_{1} ⩽ σ_{2}.

Proof:

1. We use the following induction hypothesis: suppose there is a substitution θ such that θΣ ⊢ θσ_{1} ⩽ θσ_{2}. Then P(θΣ, θσ_{1}, θσ_{2}) exists, and has a solution θ′ which is an extension of θ. We proceed by induction on the cases for I(σ_{1}, σ_{2}): the first five cases are straightforward, corresponding to the rule applications of a canonical derivation of θΣ ⊢ θσ_{1} ⩽ θσ_{2}. For the next, it suffices to note that if we have an instance of θα ⩽ θσ_{1} ⩽ θσ_{2} then the last rule in the derivation must be either Arrow or Prom^{↑}. If it is the former, then we have θ(α) = τ_{1}→τ_{2}, so we set θ′α_{i} ⩽ τ_{i}. Otherwise there must be some series of Prom^{↑} rules below an arrow rule, in which case we still have τ_{1}→τ_{2} such that θα ⩽ τ_{1}→τ_{2}. Finally, if we have an instance of θΣ ⊢ θσ_{1}→θσ_{2} ⩽ θ(α) and σ_{1}→σ_{2} is not a type, then the last rule must be Arrow, so θ(α) = τ_{1}→τ_{2}, and so if θ′[τ_{1}/β_{1}] ∘ [τ_{2}/β_{2}] ∘ θ, we have θ′Σ ⊢ θ′(β_{1}→β_{2}) ⩽ θ′σ_{2}.

2. Similarly, if we have a solution θ to I(Σ, σ_{1}, σ_{2}), we can construct a proof of θΣ ⊢ θσ_{1} ⩽ θσ_{2} □

5.9 Conclusion

In this chapter we have shown how bounded subtyping can be incorporated into a unification-style framework in order to decided a subtype relation which marries subtype and parametric polymorphism. In fact, QSPs are very similar to mixedprefix unification problems, and we will exploit this in the next chapter to generate constraint sets which play an analogous role to principal solutions.

We have been unable to extend this approach to deal with non-structural subtyping, for example with records. The problems lies in the binding transition: if we have an inequality of the form α ⩽ {l_{i}:τ_{i}}_{i∈I} where some base type in the record type occurs essentially, it is not possible in general to determine a shape for α such that the substitution will preserve all possible solutions and introduce no new ones. Although we could use the techniques of chapter 4 to find a set of shapes which we could use to solve this problem, such a solution only works in the context of a closed problem: While this still has applications (for example, signature matching), it will not be useful for applications such as the next chapter, where QSPs need to be partially solved before the shape of all the type variables they contain can be fully determined.

A further problem with the approach here is that the technique used for solution falls into the class of “string-based” rather than “graph-based” unification strategies. In theory such approaches can cause exponential increase in the size of constraint sets; but typically, bounded signatures are small and constraint sets contain few essential occurrences of the types to be eliminated, and the problem has not occurred in any reasonable example. The situation corresponds to the similar case in typed λ-calculus where the printed form of an inferred type is huge, and such cases appear not to arise in practice.

Subtyping and Annotation

Like ML, ML_{⩽} lacks the ability to pass function parameters with polymorphic types. For example, consider the function

fun pair_app f (a,b) (c,d) = (f a c, f b d)

if we have max : ∀α⩽Int. α→α→α, then we cannot type the expression

pair_app max (3, -7) (4,-24)

and obtain the value (4,-7) of type Nat × Int since it requires max to be used with types Nat→Nat→Nat and Int→Int→Int, which have no common subtype.

In an analogous fashion to the lifting of this restriction for ML in [31], here we lift that restriction for ML_{⩽}, by requiring annotations on λ-bound variables whose arguments are intended to be of polymorphic type. All unannotated variables are assumed to be of monomorphic type, as they would be in ML. For example, the above term becomes

fun pair_app (f:All('a<'b) 'a -> 'a -> 'a) (a,b) (c,d) =
                  (f a c, f b d)

for which a type is inferred which has as an instance the type scheme

∀β (∀α⩽β.α→α→α) → ∀γ⩽β ∀δ⩽β. (γ × δ)→(γ × δ)→(γ × δ)

(As normal, the scope of universal quantifiers extend as far to the right as possible, while → is right-associative). We will use our theory of subtyping to define ML_{∀⩽}, a type system incorporating both subtyping and parametric polymorphism, and the ability to pass polymorphic functions such as max as parameters. Although we have defined a subtype relation incorporating first-order type schemes and subtyping, there are still issues which need to be tackled before it can be incorporated into a type inference system.

Free Variables

As in ML_{⩽}, any type inference system will need to be able to check inclusions between types which contain variables not bound in the type. We will modify our subtyping algorithm to deal with these.

Completeness

The system of types as presented cannot be incorporated into an ML-like system which has complete type inference. For example, suppose we have a term f of type

∀α.(α→α→Int)→α→α→Int, and K is λxλy.x. Consider the term

(λz.Kz(fz))(λxλy.3)

The term λxλy.3 can be given any type α→β→Int, and the term λz.Kz(fz) any type γ→γ such that γ ⩽ δ→δ→Int for some δ. So combining these, the whole expression can be given any type α→β→Int which has the property that α→β→Int ⩽ δ→δ→Int for some δ, i.e. such that there is some δ for which δ ⩽ α and δ ⩽ β. However, our type system does not let us express a type scheme which can be instantiated with any two types possessing a common lower bound. Thus if we do not widen our notion of type scheme, we lose some of the possible instantiations of inferred types.

Types and Environments

We will prove a weak version of principality for our type inference system, which we shall term minimality. In order to obtain minimal typing, we extend the definition of type schemes once more with constrained type schemes, which can be seen as a generalisation of the constrained type-schemes for ML_{⩽}.

κ ::= α | σ | σ→κ | ∀α.κ | ∀α⩽τ.κ | ∀\overline{α}\C.κ

Notice that the domain type of a function must be a bounded type scheme σ. And instead of having a separate signature for subtyping information, we will have a single environment incorporating term and type bindings.

•  The empty context
Γ, a
Γ, a ⩽ τ where Γ contains all base types in τ
Γ, x : κ where Γ contains all base types in κ.

A scoping restriction will be enforced on free variables by only allowing substitutions θ such that θΓ is well-formed. We will call such substitutions Γsubstitutions.

Although we shall define the notion more formally later, intuitively a type scheme κ_{1} is minimal for an expression if for any other derived type κ_{2} and any σ such that κ_{2} ⩽ σ, we obtain κ_{1} ⩽ σ. This is a very weak form of principal type in the sense of [23]: although we cannot derive all possible type schemes from a minimal type, we can derive all bounded type schemes. We restrict contravariant occurrences of type schemes to be bounded type schemes, and this will ensure that the modularity requirement is met that if we type a term with its minimal type, the term will be usable in any context where any different type that we might assign it would suffice. Thus our type inference algorithm will be complete.

6.1 Extensions to the Type System

In order to avoid the complexities which occur when dealing with type assertions of the form κ_{1} ⩽ κ_{2}, we will permit constrained types to appear only in assertions of the form κ ⩽ σ. We will ensure this by placing a syntactic restriction on programs: constrained type schemes are not legal in term syntax. Then the following subtyping rule for constrained types suffices:

\frac{Γ ⊢ [\overline{τ}/\overline{α}]C  Γ ⊢ [\overline{τ}/\overline{α}]κ ⩽ σ}{Γ ⊢ ∀\overline{α}\C.κ ⩽ σ}  (∀_{C⩽}-left)

This is the only subtyping rule in which constrained types are permitted, although of course ∀-left′ and ∀_{⩽}-left′ are just specialisations of ∀_{C}-left. In fact we do not even have reflexivity for constrained type schemes in the subtyping system.

Since we are now not dealing with closed types, we also extend the Refl rule to include free variables, which are simply considered to be rigid.

6.1.1 Proposition: When the rule ∀_{C⩽}-left is added to the system, we retain transitivity elimination and thus the notion of canonical derivations.

Constrained Judgements

In line with ML_{⩽}, we return to a constrained form of judgement: the judgements in the type system will be of the form

Γ; C ⊢ e : κ

where C is a set of inequalities τ_{1} ⩽ τ_{2} between types. Thus we modify our subtyping judgements rules to be of the form:

Γ; C ⊢ κ ⩽ σ

modifying ∀_{⩽}-right and ∀-right so that all occurrences are captured by quantification, e.g.:

\frac{Γ, a; C ⊢ σ_{1} ⩽ [a/α]σ_{2} a does not occur in σ_{1} or C}{Γ; C ⊢ σ_{1} ⩽ ∀α.σ_{2}} (∀-right)

and add the rule

\frac{τ_{1} ⩽ τ_{2} ∈ C}{Γ; C ⊢ τ_{1} ⩽ τ_{2}}  (Hyp)

to our subtyping system. In the presence of Hyp, transitivity can no longer be eliminated. However, we have the following:

6.1.2 Proposition: Suppose Γ; C ⊢ κ ⩽ σ and for some Γ-substitution θ, Γ ⊢ θC. Then Γ ⊢ θκ ⩽ θσ.

Proof: By induction on the derivation of Γ; C ⊢ κ ⩽ σ, each instance of Hyp being replaced by the proof of the constraint. □

6.1.3 Definition: A type scheme κ is Γ-feasible if there is a type scheme σ such that Γ ⊢ κ ⩽ σ. An environment Γ is feasible if for every x bound in Γ there is some σ such that Γ ⊢ Γ(x) ⩽ σ.

A sequent Γ; C ⊢ e : κ is feasible if there is a Γ-substitution θ such that θΓ is feasible, Γ ⊢ θC and θκ is Γ-feasible.

Like ML_{⩽}, the type system is quite capable of producing an infeasible derivation, but unlike ML_{⩽}, some of the constraints in the derivation may be contained within the derived type scheme at arbitrary depth. A check to ensure that some appropriate substitution for constrained variables exists will require constraints to be extracted from the type scheme and combined with those at top-level, thus we introduce the following:

6.1.4 Definition [Stripping]: We use the following auxiliary rules to define the notion of stripping.

\overline{strip τ = (∅, τ)}

\frac{strip κ = (C, σ)}{strip σ′→κ = (C, σ′→σ)}

\frac{strip [γ/α]κ = (C, σ)}{strip ∀α.κ = (C, σ)}

\frac{strip [γ/α]κ = (C, σ)}{strip ∀α⩽τ.κ = (\{γ⩽τ\} ∪ C, σ)\}}

\frac{strip [\overline{γ}/\overline{α}]κ = (C, σ)}{strip ∀α\C′.κ = ([\overline{γ}/\overline{α}]C′ ∪ C, σ)}

where the γ are fresh. 6.1.5 Proposition: If strip κ = (C, σ), then Γ; C ⊢ κ ⩽ σ. Proof: By induction on the derivation of strip κ. □ If C is unsolvable, then κ cannot be feasible, thus we can test the feasibility of Γ; C′ ⊢ e : κ by stripping all the types and checking solvability of the accumulated constraint set over Γ.

6.2 The Type System ML_{∀⩽}

Now we are ready to define the ML_{∀⩽} type system.

\frac{Γ(x) = κ}{Γ ⊢ x : κ}  (Taut)
\frac{Γ, x:τ; C ⊢ e : κ}{Γ; C ⊢ λx.e : τ→κ}  (Abs)
\frac{Γ, A, x:[a_{i}/a_{i}]σ; C ⊢ [a_{i}/a_{i}]e : κ Γ, A; C ⊢ κ ⩽ σ′ for some σ′ no a_{i} occurs in C  α_{i} fresh}{Γ; C ⊢ λx:σ.e : [α_{i}/a_{i}]σ→[α_{i}/a_{i}]κ}  (TypedAbs)
where fv(σ) = a_{1} … a_{n} and A = a_{1} … a_{n}.
\frac{Γ; C ⊢ e_{1} : σ→κ Γ; C ⊢ e_{2} : σ}{Γ; C ⊢ e_{1}e_{2} : κ}  (App)

\frac{Γ; C_{1} ⊢ e_{1} : κ_{1} Γ, x:κ_{1}; C_{1} ⊢ e_{2} : κ_{2} strip κ_{1} = (C_{2}, σ_{1})}{Γ; C_{1} ∪ C_{2} ⊢ let x = e_{1} in e_{2} : κ_{2}} (Let)

\frac{Γ; C ⊢ e_{1} : σ σ ∈ σ(Γ)}{Γ; C ⊢ (e_{1}:σ) : σ}  (Coerce)
\frac{Γ; C ⊢ e : κ Γ; C_{2} ⊢ κ ⩽ σ}{Γ; C ⊢ e : σ}  (Sub)
\frac{Γ; C_{1} ∪ C_{2} ⊢ e_{1} : κ \overline{α} ∩ (fv(Γ) ∪ fv_{Γ}(C_{2})) = ∅}{Γ; C_{1} ⊢ e_{1} : ∀\overline{α}\C_{2}.κ}  (Gen)
\frac{Γ; C_{1} ⊢ e : ∀\overline{α}\C_{2}.κ}{Γ; C_{1} ∪ C_{2} ⊢ e : κ}  (Inst)

We take the view that if a variable occurs free in a type annotation, then that variable is implicitly universally quantified, and the term should be typable for any instantiation of it – for example the term λx:a. x + 3 is not typable. Thus we impose the restriction on TypedAbs that the term is typable with a held rigid. Once this requirement is met, we substitute free variables for the a_{i}.

In the Let rule, stripping the type of the bound variable is necessary to ensure that constraints are not lost by being bound with a Gen rule into a type scheme for an expression used in a let construction in which the bound variable does not occur in the body. Since with strict semantics the term will be evaluated even though never used, it would be unsound to let the constraints vanish in this way. This observation was first made in [46], and is considered more fully in [47]. Our approach is the one taken in [15].

6.3 Feasibility and Factorisation

In this section we will develop a notion of minimal types for ML_{∀⩽}. In ML, the foundation of the principal type property is the notion of a principal unifier, one which is more general than any other unifier. Unfortunately, QSPs do not in general have principal solutions: for example the problem mentioned earlier

              ∀Int∀Nat⩽Int∃γ∀a⩽Nat∃α.γ→α⩽a→Int→a

has solutions [Int→a/α] ∘ [Int/γ] and [Int→a/α] ∘ [Nat/γ]. There is no value we can assign to γ to define a principal solution θ, but if we leave γ completely flexible, then [Nat→Nat/γ] ∘ θ would need to be a solution (which it isn’t).

Working with subtyping hypothesis involving constraints offers us the possibility of deriving principal constraint sets instead of principal substitutions. We shall call these constraint sets factorisations.

Let us consider the reasons why principal solutions do not exist. In unification problems a variable is only ever instantiated to a value to which it is known to be necessarily equal, and thus in the instantiation no solution is lost. However, in solving QSPs, while this holds for binding transitions, and flattening does not affect solutions, a projection transition replaces a base type with its bound, which preserves solvability (in the context in which it is applied) but may lose solutions.

For example, in applying the transitions to solve the QSP, we lose a possible solution: on page 104 when we projected Nat, the solution [Nat/γ]∘[Int→a/α] of the original problem was no longer a solution of the new one. However, a solution was preserved which is identical to this one on the variables quantified outside Nat (a set which is in this case empty).

Quantified subtype problems generated during type inference will contain two types of variables, which we may call internal and external, with much the same connotations as in chapter 3. We will derive factorisations by eliminating exactly those universally bound variables generated during the reduction of κ ⩽ σ to a QSP. Thus only internal variables will ever be instantiated by transitions, and if two solutions coincide on their external variables, we will retain at least one. We need a couple of technical results:

6.3.1 Proposition: If Γ; C ⊢ κ ⩽ σ, a does not appear in this sequent, and τ ∈ τ(Γ), then Γ, a(⩽τ); C ⊢ κ ⩽ σ.

Proof: By the standard method for weakening. An auxiliary renaming lemma is required, similar to that of lemma(5.1.6), whose proof is straightforward. □

6.3.2 Proposition: Suppose we have S and Γ such that any sequent in S is of the form Γ ⊢ τ_{1} ⩽ τ_{2}, and S ⊩ {Γ ⊢ κ ⩽ σ}. Then Γ; C ⊢ κ ⩽ σ, where C is the set of constraints given by the types τ_{1} ⩽ τ_{2} in S.

Proof: By induction on the construction of the proof of Γ ⊢ κ ⩽ σ from the sequents in S. □

Feasibility

Before considering the question of factorisations, we will deal with the simpler issue of feasibility; that is, the question of when a type or judgement “makes sense”. This is equivalent to the question of whether a constraint set C obtained by stripping is solvable. Evidently this is very similar to the application at the end of the last chapter; however it may be the case that the environment Γ has term bindings which contains variables occurring in C, and we need to respect the scoping restrictions this places on possible solutions. The prefix C(Γ) corresponding to Γ is

C(•)  = •  (The empty prefix)
C(Γ, a)  = C(Γ)∀a
C(Γ, a⩽τ) = C(Γ)∀a⩽τ
C(Γ, x:τ) = C(Γ)∃\overline{β}  where \overline{β} is the set of variables in τ

not quantified in C(Γ)

6.3.3 Proposition: Let \overline{β} be the set of variables occurring in C but not in Γ. Then there is a Γ-substitution θ such that Γ ⊢ θC iff there is a solution to the QSP

C(Γ)∃\overline{β}. ⋀ C

Proof: By the definition of solution for QSPs. □

Factorisation

The problem I(κ ⩽ σ) corresponding to the inequality κ ⩽ σ is

I(κ ⩽ ∀α.σ)  = ∀a.I(κ ⩽ [a/α]σ)  a fresh
I(κ ⩽ ∀α⩽τ.σ)  = ∀a⩽τ.I(κ ⩽ [a/α]σ)  a fresh
I(∀α.κ ⩽ ρ)  = ∃α.I(κ ⩽ σ)  ρ not quantified, α ∉ fv(ρ)
I(∀α⩽τ.κ ⩽ ρ)  = ∃α.α ⩽ τ ∧ I(κ, ρ)  ρ not quantified, α ∉ fv(ρ)
I(∀\overline{α}\C.κ ⩽ ρ)  = ∃\overline{α}.C ∧ I(κ, ρ)  ρ not quantified, α ∉ fv(ρ)
I(σ→κ ⩽ σ_{1}→σ_{2}) = I(σ_{1} ⩽ σ) ∧ I(κ ⩽ σ_{2})  if one of σ, κ, σ_{1}, σ_{2} is

not a type

I(α ⩽ σ_{1}→σ_{2}) = ∃α_{1}, α_{2}.α ⩽ α_{1}→α_{2} if one of σ_{1}, σ_{2} is
∧ I(σ_{1} ⩽ α_{1}) ∧ I(α_{2} ⩽ σ_{2}) not a type
I(σ→κ ⩽ α) = ∃α_{1}, α_{2}.α_{1}→α_{2} ⩽ α if one of σ, κ is not a type
∧ I(α_{1} ⩽ σ) ∧ I(κ ⩽ α_{2})
I(τ_{1} ⩽ τ_{2})  = τ_{1} ⩽ τ_{2}
I(κ ⩽ σ)  = Fail  otherwise

Let \overline{α} be the set of free variables in I(κ ⩽ σ) not existentially quantified in C(Γ). Then the problem P(Γ, κ, σ) is C(Γ)∃\overline{α}.I(κ ⩽ σ),

6.3.4 Remark: P(Γ, κ, σ) is a well-formed, closed QSP.

6.3.5 Definition [Factorisation]: A factorisation for Γ, κ, σ is a constraint set C such that • if Γ; C ⊢ κ ⩽ σ.

• if Γ ⊢ θκ ⩽ θσ then there is an extension θ′ of θ such that Γ ⊢ θ′C.

Thus a factorisation for Γ, κ, and σ characterises all possible Γ-substitutions θ such that Γ ⊢ θκ ⩽ θσ. Note that by proposition(6.1.2) for any solution θ of C we have Γ ⊢ θκ ⩽ θσ.

The procedure factor(Γ, κ, σ) is as follows: we apply elimination steps to P(Γ, κ, σ) until we reach a QSP of the form

C(Γ)∃\overline{α}′.C

where C is of the form τ_{1}⩽τ′_{1} ∧ … ∧ τ_{n} ⩽ τ′_{n}. Then we returns C.

Properties of Factorisation

6.3.6 Lemma:

1. Suppose there is a Γ-substitution θ such that Γ ⊢ θκ ⩽ θσ. Then P(Γ, κ, σ) exists, and there is θ′, an extension of θ on the variables existentially quantified in I(κ, σ) such that θ′ is a solution of I(Γ, κ, σ).

2. Suppose θ is a solution of P(Γ, κ, σ). Then S(θP(Γ, κ, σ)) ⊩ {Γ ⊢ κ ⩽ σ}.

Proof: Essentially the same as lemma(5.8.2) □

6.3.7 Proposition: Suppose Γ ⊢ θ_{s}κ ⩽ θ_{s}σ for some θ well-scoped on Γ.Then

1. factor(Γ, κ, σ) succeeds, with C, say.

2. Γ; C ⊢ κ ⩽ σ (so for any solution θ of C we have Γ ⊢ θκ ⩽ θσ)

3. There is an extension θ′_{s} of θ_{s} by the new free variables contained in C such that Γ ⊢ θ′_{s}C

Proof:

1. From lemma 6.3.6(i), we know that P(Γ, κ, σ) exists, and has a solution. So by proposition(5.6.2), a sequence of elimination steps on I(Γ, κ, σ) succeeds.

2. Suppose Π = P(Γ, κ, σ), and a sequence of eliminations in factor yields Π′. We know by lemma(5.7.4) that there is a substitution on the variables existentially quantified in I(κ, σ) such that S(Π′) ⊩ S(θΠ). And S(θΠ) ⊩ {Γ ⊢ κ ⩽ σ} by proposition(5.6.2). Thus we have S(Π′) ⊩ {Γ ⊢ κ ⩽ σ}, and the result follows by lemma(6.3.2).

3. By lemma(6.3.6) we have a solution θ of P(Γ, κ, σ) which agrees with θ_{s} on the variables quantified outside I(κ, σ) (which includes all the variables in C); so by theorem(5.5.6) there is a solution of C(Γ)∃\overline{α}′.C which agrees with θ_{s} on these variables. □

Thus factor(Γ, κ, σ) yields a factorisation for Γ, κ, and σ if one exists.

6.4 Type Inference

Our approach to type inference will concentrate on obtaining minimal types rather than principal typings. Thus we concentrate on the solutions of constraint sets and instances of constrained types rather than on syntactic entailment between them. The advantage of this approach is that we obtain a relatively straightforward proof of minimality which suffices to demonstrate that inference is compositional and thus complete. The disadvantage is that we do not thereby obtain a notion of principal typing, nor a notion of equivalence on judgements which would be required in order to formalise a notion of simplification within the type system.

To obtain a type inference algorithm we adopt the usual practice of absorbing the Gen, Inst, and Sub rules into the others to get a syntax-directed algorithm.

6.4.1 Definition [Opening and Closing]: Given an environment Γ, a set of constraints C, and a type scheme ξ, we define close(Γ, C, ξ) to be ∀\overline{α}\C.ξ, where \overline{α} is the set of variables occurring in C or ξ but not in Γ.

In a similar vein to stripping, we recursively define the operation open on a type scheme as follows.

\frac{open [β/α]κ = (\overline{γ}, C_{1}, κ_{1})}{open ∀α.κ = (\{β\} ∪ \overline{γ}, C_{1}, κ_{1})}

\frac{open [β/α]κ = (\overline{γ}, C_{1}, κ_{1})}{∀α⩽τ.κ = (\{β\} ∪ \overline{γ}, \{β ⩽ τ\} ∪ C_{1}, κ_{1})}

\frac{open [\overline{β}/\overline{α}]κ = (\overline{γ}, C_{1}, κ_{1})}{∀\overline{α}\C.κ = (\overline{β} ∪ \overline{γ}, [\overline{β}/\overline{α}]C ∪ C_{1}, κ_{1})}

\frac{κ is not quantified at outermost level}{open κ = (∅, ∅, κ)} Informally, opening a type scheme κ extracts the minimal type scheme which is an instance of κ and which is not quantified at outermost level, analogous to instantiating a type scheme to a type in ML. The following proposition expresses this intuition.

6.4.2 Proposition: If Γ ⊢ θκ ⩽ σ, σ is not quantified at outermost level, and open κ = (\overline{α}, C, κ′), then there is an extension θ′ of θ on the variables \overline{α} such that Γ ⊢ θ′κ′ ⩽ σ.

Proof: By induction on the calculation of open κ. Note that each rule in the derivation corresponds to some ∀-left′, ∀_{⩽}-left′, or ∀_{C}-left rule in the canonical derivation of Γ ⊢ θκ ⩽ σ, from which we can build the necessary extension of θ. □

An Algorithmic Type Inference System for ML_{∀⩽}

As with ML_{⩽}, we incorporate the Sub, Gen, and Inst rules into the others to leave a single rule for each syntactic term construct.

\frac{Γ(x) = ξ}{Γ ⊢_{A} x : ξ}  (A:Taut)
\frac{Γ, x:α ⊢_{A} e : ξ}{Γ ⊢_{A} λx.e : ∀α.α→ξ}  (A:Abs)
\frac{Γ, A, x:[a_{i}/a_{i}]σ ⊢_{A} e : ξ  ξ is Γ, A-feasible}{Γ ⊢_{A} λx:σ.e : ∀α_{i}.[α_{i}/a_{i}]σ→[α_{i}/a_{i}]ξ} (A:TypedAbs)
where fv(σ) = a_{1} … a_{n} and A = a_{1} … a_{n}.

\frac{Γ ⊢_{A} e_{1} : ξ_{1} Γ ⊢_{A} e_{2} : ξ_{2} open ξ_{1} = (\overline{α}, C_{1}, σ→ξ′_{1}) factor(Γ, ξ_{2}, σ) = C_{2}}{Γ ⊢_{A} e_{1}e_{2} : close(Γ, C_{1} ∪ C_{2}, ξ′_{1})}  (A:AppArrow)

\frac{Γ ⊢_{A} e_{1} : ξ_{1} Γ ⊢_{A} e_{2} : ξ_{2} open ξ_{1} = (\overline{β}, C_{1}, α) factor(Γ, ξ_{2}, α_{1}) = C_{2} α_{1}, α_{2} fresh}{Γ ⊢_{A} e_{1}e_{2} : close(Γ, C_{1} ∪ C_{2} ∪ \{α ⩽ α_{1}→α_{2}\}, α_{2})} (A:AppVar)
\frac{Γ ⊢_{A} e_{1} : ξ_{1} Γ, x : ξ_{1} ⊢_{A} e_{2} : ξ_{2} strip ξ_{1} = (C, σ_{1})}{Γ ⊢_{A} let x = e_{1} in e_{2} : close(Γ, C, ξ_{2})} (A:Let)

\frac{Γ ⊢_{A} e_{1}:ξ  factor(Γ, ξ, σ) = C  σ ∈ σ(Γ)}{Γ ⊢_{A} (e_{1}:σ) : close(Γ, C, σ)} (A:Coerce)

6.4.3 Remark: If Γ ⊢_{A} e : ξ, then all variables occurring free in ξ occur in Γ.

Both the algorithmic and the original typing rules are capable of producing derivations resulting in unsolvable types. Whilst it would be possible to restrict the constraints introduced in a rule to be solvable, such local conditions will not suffice to ensure that a final derived type is solvable, since the union of two solvable constraint sets is not necessarily solvable. Thus the intent is to derive the type, then perform a global solvability check.

There are also implications for completeness: there is more freedom to work with unsolvable constraint sets in the definitive system than in the algorithmic one – but such derivations aren’t interesting in practice: it suffices that the algorithmic rules are complete with respect to the definitive rules in the case where the type derived using the latter is solvable.

Soundness of Type Inference

6.4.4 Proposition: If Γ ⊢_{A} e : ξ, then Γ ⊢ e : ξ.

Proof: Notice that weakening on constraint sets is admissible in the definitional type system, so long as the TypedAbs constraint on appearances of base types is not violated. So we can proceed by induction on the derivation of Γ ⊢_{A} e : ξ: each rule in the algorithmic derivation can be translated into one or more rules from the definitional system, typically the equivalent rule for the syntactic form together with an instance of Gen.

A:Taut An instance of Taut

A:Coerce An instance of Coerce followed by an instance of Gen, the former being justified by the result from proposition(6.3.7) that if factor(Γ, κ, σ) = C, then Γ, C ⊢ κ ⩽ σ.

A:Abs An instance of Abs together with an instance of Gen.

A:TypedAbs An instance of TypedAbs, together with an instance of Gen.

A:AppArrow If Γ ⊢_{A} e : ξ_{1} and open ξ_{1} = (\overline{α}, C_{1}, σ→ξ′_{1}), then by a sequence of Inst rules we obtain Γ; C_{1} ⊢ e : σ→ξ′_{1}; And if Γ ⊢_{A} e_{2} : ξ, and factor(Γ, ξ_{2}, σ) = C_{2}, Γ; C_{2} ⊢ ξ_{2} ⩽ σ. An application of App and one of Gen suffices.

A:AppVar Similarly, by a use of Inst, App, and Gen

A:Let By an instance of Let and one of Gen □

Minimality of Type Inference

In order to obtain a minimal typing property, we need an ordering on complex type schemes, and we shall choose the obvious one:

6.4.5 Definition: We define the relation Γ ⊢ ξ ≼ κ to be true iff for any σ such that Γ ⊢ κ ⩽ σ, Γ ⊢ ξ ⩽ σ. It is easy to see that ≼ is transitive, and that Γ ⊢ κ ≼ σ iff Γ ⊢ κ ⩽ σ. We extend ≼ to environments:

•  ≼ •  The empty environment
Γ_{1}, a(⩽ τ) ≼ Γ_{2}, a(⩽ τ) if Γ_{1} ≼ Γ_{2}
Γ_{1}, x:ξ  ≼ Γ_{2}, x : κ if Γ_{1} ≼ Γ_{2} and Γ_{1} ⊢ ξ ≼ κ

6.4.6 Remark: Note that the second clause in the final case is equivalent to Γ_{2} ⊢ ξ ≼ κ, since in a subtyping context only the subtype bindings are relevant, and those are the same in each environment,

6.4.7 Lemma [Weakening for typing derivations]: Suppose Γ = Γ_{1}, Γ_{2} is such that Γ′ = Γ_{1}, a(⩽τ), Γ_{2} is well-formed.

• If Γ; C ⊢ e : κ and a does not occur in Γ, κ, or C, Then Γ′; C ⊢ e : κ in a derivation of the same shape.

• If Γ ⊢_{A} e : ξ and a does not occur in Γ or ξ, then Γ′ ⊢ e : ξ in a derivation of the same shape.

Proof: Straightforward by induction on the derivation, using proposition(6.3.1) and an auxiliary renaming lemma in the style of lemma(5.1.6). □

6.4.8 Proposition: Suppose Γ; C ⊢ e : κ and there is a Γ-substitution θ_{s} such that Γ ⊢ θ_{s}C. Suppose further that we have some Γ_{a} and Γ_{a}-substitution θ_{a} such that (θ_{s} ∘ θ_{a})Γ_{a} ≼ θ_{s}Γ and Γ_{a} ⊢_{A} e : ξ. Then (θ_{s} ∘ θ_{a})ξ ≼ θ_{s}κ.

Proof: Suppose we have σ_{s} such that Γ ⊢ θ_{s}κ ⩽ σ_{s}. We will demonstrate that Γ ⊢ (θ_{s} ∘ θ_{a})ξ ⩽ σ_{s}. Note that it suffices to demonstrate the proposition only for those ρ not quantified at outermost level, for we can then deduce the result for any σ_{s}. For suppose the result holds for all such ρ, and we have σ_{s} = ∀β_{1}(⩽τ_{1}) … β_{n}(⩽τ_{n}).σ′_{s}, where σ′_{s} is not quantified at outermost level, and we write ρ_{s} for [b_{i}/β_{i}]σ′_{s} and

Γ, B = Γ, b_{1}(⩽τ_{1}), b_{2}(⩽[b_{1}/β_{1}]τ_{2}), … b_{n}(⩽[b_{1}/β_{1}, … b_{n−1}/β_{n−1}]τ_{n})

By lemma(6.4.7) we have a derivation of Γ, B; C ⊢ e : κ which is of the same shape as that of Γ; C ⊢ e : κ, and one of Γ_{a}, B ⊢ e : ξ. And since we have Γ ⊢ θ_{s}κ ⩽ σ_{s}, a canonical derivation must include Γ, B ⊢ θ_{s}κ ⩽ ρ_{s}. Thus we can deduce Γ, B ⊢ e : (θ_{s} ∘ θ_{a})ξ ⩽ ρ_{s}.

But ξ contains no occurrences of atoms in B, and since θ_{s} and θ_{a} are Γ-well formed, none are introduced by the substitution. So none of the B occur in (θ_{s} ∘ θ_{a})ξ, and thus we have Γ ⊢ (θ_{s} ∘ θ_{a})ξ ⩽ σ_{s} as required.

So suppose we have Γ ⊢ θ_{s}κ ⩽ ρ_{s}. We proceed by induction on the height of the derivation of Γ; C ⊢ e : κ by consideration of the last rule.

Taut

\frac{Γ(x) = κ}{Γ; C ⊢ x : κ}

The last rule in the algorithmic derivation must be A:Taut. Since (θ_{s} ∘ θ_{a})Γ_{a} ≼ θ_{s}Γ, if Γ_{a}(α) = ξ, we have Γ ⊢ (θ_{s} ∘ θ_{a})(ξ) ≼ θ_{s}κ.

Abs The last rule in the algorithmic derivation must be A:Abs

\frac{Γ, x:τ; C ⊢ e : κ}{Γ; C ⊢ λx.e : τ→κ}  \frac{Γ_{a}, x:α ⊢_{A} e : ξ}{Γ_{a} ⊢_{A} λx.e : ∀α.α→ξ}

If we have a derivation of Γ ⊢ θ_{s}(τ→κ) ⩽ ρ_{s}, then ρ_{s} must be of the form ρ_{1}→ρ_{2} (the only rule applicable in a canonical derivation being Arrow) and we must have Γ ⊢ θ_{s}κ ⩽ ρ_{2}. Thus writing θ′_{a} = [τ/α] ∘ θ_{a}, we have (θ_{s} ∘ θ′_{a})(Γ_{a}, x:α) ≼ θ_{s}(Γ, x:τ), and by the induction hypothesis Γ ⊢ (θ_{s} ∘ θ′_{a})ξ ⩽ ρ_{2}. We also have Γ ⊢ ρ_{1} ⩽ θ_{s}τ, and as τ = θ′_{a}α, we have Γ ⊢ ρ_{1} ⩽ (θ_{s} ∘ θ′_{a})α. Thus we can deduce

Γ ⊢ (θ_{s} ∘ θ′_{a})(α→ξ) ⩽ ρ_{1}→ρ_{2}

and hence

Γ ⊢ (θ_{s} ∘ θ_{a})(∀α.α→ξ) ⩽ ρ_{1}→ρ_{2}

TypedAbs

\frac{Γ, A, x:[a_{i}/a_{i}]σ; C ⊢ [a_{i}/a_{i}]e : κ Γ, A; C ⊢ κ ⩽ σ′ for some σ′ no a_{i} occurs in C α_{i} fresh}{Γ; C ⊢ λx:σ.e : [α_{i}/a_{i}]σ→[α_{i}/a_{i}]κ}

The last rule in the algorithmic derivation must be A:TypedAbs

\frac{Γ_{a}, A, x:[a_{i}/a_{i}]σ ⊢_{A} [a_{i}/a_{i}]e : ξ Γ_{a}, A ⊢_{A} ξ feasible}{Γ_{a} ⊢_{A} λx:σ.e : ∀α_{i}.[α_{i}/a_{i}]σ→[α_{i}/a_{i}]ξ}

where fv(σ) = a_{1} … a_{n}. and A is a_{1} … a_{n}. Suppose we have Γ; C ⊢ [α_{i}/a_{i}]σ→[α_{i}/a_{i}]κ ⩽ ρ, then ρ must be of the form ρ_{1}→ρ_{2}, and by induction on the proof it is easy to show that

Γ, A; C ⊢ [a_{i}/a_{i}]σ→κ ⩽ [a_{i}/α_{i}](ρ_{1}→ρ_{2})

since no α_{i} occurs in C. Then by the induction hypothesis, we have

Γ, A; ⊢ [a_{i}/a_{i}]σ→ξ ⩽ [a_{i}/α_{i}](ρ_{1}→ρ_{2})

and thus

Γ, A; ⊢ ∀α_{i}.[α_{i}/a_{i}]σ→[α_{1}/a_{i}]ξ ⩽ [a_{i}/α_{i}](ρ_{1}→ρ_{2})

(using ∀-left′), and then by ∀-right, instantiation, and transitivity, we have:

Γ ⊢ ∀α_{i}.[α_{i}/a_{i}]σ→[α_{1}/a_{i}]ξ ⩽ ρ_{1}→ρ_{2}

App

\frac{Γ; C ⊢ e_{1} : σ→κ Γ; C ⊢ e_{2} : σ}{Γ; C ⊢ e_{1}e_{2} : κ}

In any derivation of Γ_{a} ⊢_{A} e_{1} : ξ_{1}, the final rule must be either A:AppVar or A:AppArrow depending on the syntactic form of ξ_{1}. Suppose the former:

\frac{Γ_{a} ⊢_{A} e_{1} : ξ_{1} Γ_{a} ⊢_{A} e_{2} : ξ_{2} open ξ_{1} = (δ_{1}, D_{1}, α) factor(Γ_{a}, ξ_{2}, α_{1}) = D_{2} α_{1}, α_{2} fresh}{Γ_{a} ⊢_{A} e_{1}e_{2} : close(Γ_{a}, D_{1} ∪ D_{2} ∪ \{α⩽α_{1}→α_{2}\}, α_{2})}

Let \overline{δ}_{1} be fv(D_{1}) − fv(Γ_{a}) and \overline{δ}_{2} = fv(D_{2}) − fv(Γ_{a}). These are disjoint since the only free variables ξ_{1} and ξ_{2} share are those occurring in Γ_{a}. Since Γ ⊢ θ_{s}κ ⩽ ρ_{s}, we have Γ ⊢ θ_{s}(σ→κ) ⩽ θ_{s}σ→ρ_{s}, so from the induction hypothesis

Γ ⊢ (θ_{s} ∘ θ_{a})ξ_{1} ⩽ θ_{s}σ→ρ_{s}

Now by proposition)6.4.2 there is a substitution θ, extending (θ_{s} ∘ θ_{a}) by the variables in \overline{δ}_{1} such that Γ ⊢ θD_{1}, and Γ ⊢ θα ⩽ θ_{s}σ→ρ_{s}.

So θα must either be an arrow type τ_{1}→τ_{2} or a base type which promotes to an arrow type τ_{1}→τ_{2}. In either case, setting θ′ = [τ_{i}/α_{i}] ∘ θ, we have Γ ⊢ θ_{s}σ ⩽ θ′α_{1}, and from the induction hypothesis we have Γ ⊢ θ′ξ_{2} ⩽ θ_{s}σ, so by transitivity, Γ ⊢ θ′ξ_{2} ⩽ θ′α_{1}. So by proposition (6.3.7) there is an extension θ′′ of θ′ by the variables in \overline{δ}_{2} such that Γ ⊢ θ′′D_{2}. Thus we have θ′′ an extension of (θ_{s} ∘ θ_{a}) by the variables \{α_{1}, α_{2}\} ∪ \overline{δ}_{1} ∪ \overline{δ}_{2} such that Γ ⊢ θ′′(D_{1} ∪ D_{2} ∪ \{α ⩽ α_{1}→α_{2}\}), and Γ ⊢ θ′′α_{2} ⩽ ρ_{s}. The result follows immediately. The A:AppArrow case is similar.

Let

\frac{Γ; C_{1} ⊢ e_{1} : κ_{1} Γ, x:κ_{1}; C_{1} ⊢ e_{2} : κ_{2} strip κ_{1} = (C_{2}, σ)}{Γ; C_{1} ∪ C_{2} ⊢ let x = e_{1} in e_{2} : κ_{2}}

The last rule in the algorithmic derivation must be A:Let.

\frac{Γ_{a} ⊢_{A} e_{1} : ξ_{1} Γ_{a}, x:ξ_{1} ⊢_{A} e_{2} : ξ_{2} strip ξ_{1} = (D, σ′)}{Γ_{a} ⊢_{A} let x = e_{1} in e_{2} : close(Γ_{a}, D, ξ_{2})}

Since Γ ⊢ θ_{s}C_{1}, from the induction hypothesis we have Γ ⊢ (θ_{s} ∘ θ_{a})ξ_{1} ≼ θ_{s}κ_{1}, and thus Γ ⊢ (θ_{s} ∘ θ_{a})(Γ_{a}, x:ξ_{1}) ≼ θ_{s}(Γ, x:κ_{1}). Now since Γ ⊢ θ_{s}C_{2}, θ_{s}κ_{1} must be feasible, and thus there is some σ′ not outermost quantified such that Γ ⊢ θ_{κ} ⩽ σ′. Using the induction hypothesis we obtain Γ ⊢ (θ_{s} ∘ θ_{a})ξ_{1} ⩽ σ′, so D = strip ξ_{1} is solvable. Thus we obtain Γ ⊢ close(Γ_{a}, D, ξ_{2}) ⩽ ρ_{s} by ∀_{C}-left′.

Coerce The last rule in the algorithmic derivation must be A:Coerce

\frac{\begin{matrix}Γ; C ⊢ e_{1} : σ \\ σ ∈ σ(Γ) \end{matrix}}{Γ; C ⊢ (e_{1} : σ) : σ}  \frac{Γ_{a} ⊢_{A} e_{1}:ξ factor(Γ_{a}, ξ, σ) = C σ ∈ σ(Γ_{a})}{Γ_{a} ⊢_{A} (e_{1}:σ) : close(Γ, C, σ)}

If \overline{α} is fv(C)−fv(Γ), by proposition(6.3.7) there is an extension θ of (θ_{s} ∘θ_{a}) by the variables in \overline{α} such that Γ_{a} ⊢_{A} θC. Thus Γ ⊢ (θ_{s} ∘ θ_{a})(∀α\C.σ) ⩽ θ_{s}σ. The result follows by transitivity.

Sub

\frac{Γ; C ⊢ e : κ Γ; C ⊢ κ ⩽ σ}{Γ; C ⊢ e : σ}

Suppose Γ_{a} ⊢_{A} e : ξ. Since Γ ⊢ θ_{s}C, we have Γ ⊢ θ_{s}κ ⩽ θ_{s}σ, and by assumption we have θ_{s}σ ⩽ ρ_{s}, so by transitivity we have Γ ⊢ θ_{s}κ ⩽ ρ_{s}. The result follows from the induction hypothesis.

Gen

\frac{Γ; C_{1} ∪ C_{2} ⊢ e : κ \overline{α} ∩ (fv(Γ) ∪ fv(C_{1})) = ∅}{Γ; C_{1} ⊢ e : ∀\overline{α}\C_{2}.κ}

Suppose Γ_{a} ⊢_{A} e : ξ. Since we have Γ ⊢ θ_{s}(∀\overline{α}\C_{2}.κ) ⩽ ρ_{s}, the last rule in the canonical derivation must be ∀_{C⩽}-left′, so there must be an extension θ′_{s} of θ_{s} by the variables in \overline{α} such that Γ ⊢ θ′_{s}C_{2} and Γ ⊢ θ′_{s}κ ⩽ ρ_{s}. Since Γ ⊢ θ_{s}C_{1} also, the result follows from the induction hypothesis.

Inst

\frac{Γ; C_{1} ⊢ e : ∀\overline{α}\C_{2}.κ}{Γ; C_{1} ∪ C_{2} ⊢ e : κ}  (Inst)

Suppose we have Γ ⊢_{A} e : ξ and Γ ⊢ θ_{s}κ ⩽ ρ_{s}. Then by ∀_{C}-left′ we have Γ ⊢ θ_{s}(∀\overline{α}\C_{2}.κ) ⩽ ρ_{s}). The result follows from the induction hypothesis.

□

6.4.9 Corollary [Minimality of Type Inference]: If Γ ⊢ e : κ, and Γ ⊢_{A} e : ξ, then Γ ⊢ ξ ≼ κ.

Proof: Take Γ_{a} = Γ, θ_{a} and θ_{s} to be identity substitutions in proposition(6.4.8).

□

Completeness of Type Inference

6.4.10 Proposition: Suppose Γ; C ⊢ e : κ is feasible, so we have Γ ⊢ θ_{s}C and Γ ⊢ θ_{s}κ ⩽ ρ where ρ is not outermost quantified. Then for any feasible Γ_{a} and Γ_{a}-substitution θ_{a} such that (θ_{s} ∘ θ_{a})Γ_{a} ≼ θ_{s}Γ, there is some ξ such that Γ_{a} ⊢_{A} e : ξ.

Proof: We will proceed by induction on the height of the derivation of Γ ⊢ e : κ by cases on the last rule.

Taut

\frac{Γ(x) = κ}{Γ; C ⊢ x : κ}

The algorithmic derivation is simply an instance of A:Taut.

Abs

\frac{Γ, x:τ; C ⊢ e : κ}{Γ; C ⊢ λx.e : τ→κ}

Let α be fresh. We define θ′_{a} = [τ/α] ∘ θ_{a}, so that (θ_{s} ∘ θ′_{a})(Γ_{a}, x:α) ≼ θ_{s}(Γ, x:τ). By the induction hypothesis, we have a derivation of Γ_{a}, x:α ⊢ e : ξ. So we may apply A:Abs to obtain Γ_{a} ⊢_{A} λx.e : ∀α.α→ξ.

TypedAbs

\frac{Γ, A, x:[a_{i}/a_{i}]σ; C ⊢ [a_{i}/a_{i}]e : κ Γ, A; C ⊢ κ ⩽ σ′ for some σ′ no a_{i} occurs in C α_{i} fresh}{Γ; C ⊢ λx:σ.e : [α_{i}/a_{i}]σ→[α_{i}/a_{i}]κ}

where fv(σ) = a_{1} … a_{n}. and A is a_{1} … a_{n}. Let θ be a (Γ, A)-substitution such that Γ, A ⊢ θC. Then Γ, A ⊢ θC and Γ, A ⊢ θκ ⩽ θσ′. Thus by using proposition(6.4.8), we must have Γ, A, x:[a_{i}/a_{i}]σ ⊢ [a_{1}/a_{i}]e : ξ such that Γ, A ⊢ (θ_{s} ∘ θ)ξ ⩽ θσ_{1}. Now since Γ and Γ_{a} are the same with respect to the subtyping assertions they prove, we must have Γ_{a}, A ⊢ ξ feasible. So we may apply A:TypedAbs.

App

\frac{Γ; C ⊢ e_{1} : σ→κ Γ; C ⊢ e_{2} : σ}{Γ; C ⊢ e_{1}e_{2} : κ}

Depending on the syntactic form of open ξ_{1}, the algorithmic rule will either be A:AppVar or A:AppArrow, so suppose open ξ_{1} = (D_{1}, α) and it is the latter case. By the induction hypothesis, we have a derivation of Γ_{a} ⊢_{A} e_{1} : ξ_{1} and from proposition(6.4.8) we have Γ ⊢ θ_{a}ξ_{1} ≼ σ→κ. Since the canonical derivation of Γ ⊢ θ_{a}ξ_{1} ≼ σ→κ must finish with an Arrow rule followed by some sequence of ∀-left′ rules, there must be an extension θ′_{a} of θ_{a} such that Γ ⊢ σ ⩽ θ′_{a}α_{1}. Now since Γ ⊢ θ_{a}ξ_{2} ⩽ σ_{1}, we have Γ ⊢ θ′_{a}ξ_{2} ⩽ θ′_{a}α_{1} by transitivity, and thus factor(Γ_{a}, ξ_{2}, α_{1}) succeeds and we may apply A:AppVar. The AppArrow case is similar.

Let

\frac{Γ; C_{1} ⊢ e_{1} : κ_{1} Γ, x:κ_{1}; C_{1} ⊢ e_{2} : κ_{2} strip κ_{2} = (C_{2}, σ)}{Γ; C_{1} ∪ C_{2} ⊢ let x = e_{1} in e_{2} : κ_{2}}

From the induction hypothesis, we have Γ_{a} ⊢_{A} e_{1} : ξ_{1} such that Γ ⊢ (θ_{s} ∘ θ_{a})ξ_{1} ≼ θ_{s}κ_{1}. Thus we have (θ_{s} ∘ θ_{a})(Γ_{a}, x:ξ_{1}) ≼ θ_{s}(Γ, x:κ_{1}). And using the induction hypothesis again we have Γ_{a}, x:ξ_{1} ⊢_{A} e : ξ_{2}, and we may apply A:Let.

Coerce

\frac{Γ; C ⊢ e_{1} : σ σ ∈ σ(Γ)}{Γ; C ⊢ (e_{1} : :σ) : σ}

By the induction hypothesis we have Γ_{a} ⊢_{A} ξ with Γ ⊢ θ_{a}ξ ⩽ σ. Thus since σ ∈ σ(Γ_{a}), factor(Γ_{a}, ξ, σ) succeeds and we may apply A:Coerce.

Sub

\frac{Γ; C ⊢ e : κ Γ; C ⊢ κ ⩽ σ}{Γ; C ⊢ e : σ}

Since Γ ⊢ θ_{s}C, we have Γ ⊢ θ_{s}κ ⩽ θ_{s}σ and Γ ⊢ θ_{s}σ ⩽ ρ_{s}. Thus by transitivity we have Γ ⊢ θ_{s}κ ⩽ ρ_{s}, and the result follows from the induction hypothesis.

Gen

\frac{Γ; C_{1} ∪ C_{2} ⊢ e : κ \overline{α} ∩ (fv(Γ) ∪ fv(C_{1})) = ∅}{Γ; C_{1} ⊢ e : ∀\overline{α}\C_{2}.κ}

By consideration of the canonical derivation of Γ ⊢ ∀\overline{α}\C_{2}.κ, we have an extension θ′_{s} of θ_{s} by \overline{α} such that Γ ⊢ θ′_{s}(C_{1} ∪ C_{2}). The result follows from the induction hypothesis.

Inst

\frac{Γ; C_{1} ⊢ e : ∀\overline{α}\C_{2}.κ}{Γ; C_{1} ∪ C_{2} ⊢ e : κ}

We have Γ ⊢ θ_{s}κ ⩽ ρ, so by ∀_{C}-left′, we have Γ ⊢ θ_{s}(∀\overline{α}\C_{2}.κ) ⩽ ρ. The result follows from the induction hypothesis. □

6.4.11 Corollary [Completeness of Type Inference]: If Γ; C ⊢ e : κ is feasible. Then Γ ⊢_{A} e : ξ for some Γ_{a}-feasible ξ.

Proof: Taking Γ_{a} = Γ and θ_{a} the identity, the result is immediate, apart from the Γ_{a}-feasibility of ξ. But if Γ ⊢ e : κ is feasible, then there is some θ and σ such that Γ ⊢ θC and Γ ⊢ θκ ⩽ σ. By proposition(6.4.8) Γ ⊢ ξ ≼ κ, and thus Γ ⊢ θξ ⩽ σ, so ξ is Γ-feasible. □

6.5 Examples

The type inference algorithm as given, like the inference algorithm for ML_{⩽} given in the introduction, is “raw”, in the sense that it produces unsimplified typings. And similarly to ML_{⩽}, this is the principal barrier to presenting comprehensible examples. Consider:

let x = fun (f: All(A) All(B<A) B -> B -> B)
   fun(a) fun (b) fun (c) fun (d)
     (f a c, f b d);

produces (reformatted for legibility) the expected typing:

x : All(A)(All(B<A) B->B->B)->
     All('j) 'j->
       All('k) 'k->
         All('l) 'l->
           All('m) 'm->
             All('w,'v,'u,'x;
                 'u < 'w, 'u < A, 'j < 'u, 'l < 'u,
                 'x < 'v, 'x < A, 'k < 'x, 'm < 'x)  Pair 'w 'v

and the application

let x = pairApp max 10 -7  20 -3;

typechecks successfully. Obviously, like ML_{⩽}, the system is unusable without constraint simplification. In cases such as the above, where a sequence of abstractions is followed by an abstraction-free function body, it can be observed that constraints accumulate at the outermost type inside the abstractions; in such cases we may reasonably expect scoping to have little impact on simplification; however, when the value returned by the function is another function computed at some depth inside the function body, for example

let x = fun (h)
  let z = fun(g) g h
  in z
  end
;

assigned the principal type is

x : All('b) 'b->
      All('g,'i,'h; 'g < 'i->'h, 'b < 'i)
         All('d) 'd->
            All('e,'f; 'd < 'e->'f, 'b < 'e) 'f

It is possible to combine the two adjacent universal quantifiers, but this does not rid us of the constrained quantifier nesting.

6.6 Conclusion

It is clear from even the small examples that simplification is required to make ML_{∀⩽} a practical type system. While we have not investigated the issue in detail, it appears that scoping significantly complicates the more powerful simplifications available. There is little difficulty with identifying two types if simplification indicates equality, but the opportunity arises less often than in ML_{⩽}, since for example it is only complete to identify a variable α with its least upper bound τ if every possible value for τ is a possible value for α, in other words, if for every free variable β of τ, every base type which can occur in a well-scoped substitution for β can also occur in a well-scoped substitution for α. Developing a theory of simplification would require a instance relation to formalise equivalence on judgements, but a simple definition remains elusive. Another way to make the approach here scalable is to annotate top-level function definitions with their intended type, against which their inferred type can be checked.

Laufer and Odersky prove their type system equivalent to system F, in the sense that any type derivation in system F can be encoded in their system extended by type operator definitions of the form

newtype Tv_{1} … v_{n} = σ

with corresponding injection and projection terms. The encoding (we omit the details, which are not particularly enlightening) uses a type operator to encapsulate each instance of universal quantification, and the operators must be defined with respect to the F_{⩽} derivation rather than just the term. Since we are restricted to structural subtyping and distinguish between bounded and unbounded quantification, it is difficult to see how we might embed F_{⩽}; however an embedding is possible for an F_{⩽} variant which uses the subtyping rules

\frac{Δ, X ⊢_{F} S_{1} ⩽ S_{2}}{Δ ⊢_{F} ∀(X)S_{1} ⩽ ∀(X)S_{2}}  (∀)
\frac{Δ ⊢_{F} S_{2} ⩽ S_{1} Δ, X ⊢ S′_{1} ⩽ S′_{2}}{Δ ⊢_{F} ∀(X⩽S_{1})S′_{1} ⩽ ∀(X⩽S_{2})S′_{2}}  (∀_{⩽})

with a corresponding separation at the term level between bounded and unbounded type abstraction.

This version of F_{⩽} is similar to the system F_{⩽}^{⊤} ([8, 9]) whose flaw, from which ML_{∀⩽} does not suffer, is that it lacks a minimal type property. Thus while it is not possible to uniformly encode F_{⩽} programs as ML_{∀⩽} programs, we might perhaps justifiably claim that much of what can be done in raw F_{⩽}^{⊤} can be done in ML_{∀⩽}.

Conclusion

Summary of Results

Subtyping provides a convenient framework in which to express relations of subsumption and implicit coercion. Here we summarise our results and consider their relevance to subtyping systems in general.

⊤-less Models

Much of this thesis has been devoted to the study of the properties of subtyping over Helly posets. Up to now, approaches using complex type systems (such as those of Smith [16] or Pottier [40]) have been based on the “one big lattice” approach, extending the ordering given by the subtyping constructors with ⊤ and ⊥. The merit of these types in themselves is an open issue. ⊤ constitutes a convenient implementation mechanism for typing features such heterogeneous lists, and so allows restricted sacrifice of type safety, but in a strong typing context it is hard to see the intrinsic value of knowing that a term inhabits, requires an argument, or produces a result which is in the type containing all elements, much less the type containing no elements. Perhaps something of the utility of ⊤ and ⊥ in the systems of Smith and Pottier can be inferred from that fact that their approaches incorporate these elements in the models of their subtype systems, but not the syntax.

Our results allow us to construct a good case for Helly posets as a feasible point in the design space for subtype orderings. We have shown that as well as encompassing many classes of natural orderings on atomic types, Helly posets are closed under type formation using many constructors of interest, so they are versatile enough to capture many interesting orderings and some of the ways in which we might wish to extend them. We have demonstrated how solvability may be determined for both finite and regular types with both structural and non-structural constructors. And we have shown in a simple context that Helly posets enable the use of semantic information to provide a powerful simplification system.

The choice of ordering in a subtyping system is made on grounds of tractability, expressive power, and strength. Operations on Helly posets are typically analogous to operations on lattices: distance closure over the former corresponds with constraint closure over the latter. So it seems reasonable to expect lowdiameter Helly posets to be approximately as efficient as lattices in most cases. In terms of expressive power, there seems very little to choose between the two. And in terms of preventing type errors, they lack the weakness that comes from including ⊤ and ⊥, and so better capture the intuitions behind complex type systems.

Bounded Quantification

We have also studied the issue of bounded quantification, a natural outgrowth of the extensibility properties of Helly posets. As well as the natural application in signature-checking and top-level coercion to keep constraint sets manageable, bounded type schemes have a principality property that can be seen as analogous to that of mixed-prefix unification, which we exploited to implement a type system with annotations containing subtypes. Whilst the question of how principal constraint sets can be generated for subtyping constrained type schemes is a difficult one, our results in this area constitute an answer to the question of how subtyping and annotation can be made to interact. However, the practical utility of such a complex type system is an open issue, particularly until the question of simplification is resolved.

Directions for Future Work

There are several issues which we have chosen not to cover in this thesis, which would constitute natural and interesting extensions of the work.

Simplification

After solvability, simplification is the most fundamental operation necessary to successful use of subtyping. Although we have given a method for simplification in the atomic case, the approach needs to be extended to deal with non-structural constructors in the style of [40].

Complexity

We have paid little attention to complexity issues, but the analogy between distance closure over a Helly poset and constraint closure over a lattice would seem to indicate that similar complexity arguments (such as [21]) should apply, in the same way as Benke’s result [5] generalises that of Tiuryn [50] for solvability in the finite structural case.

Bibliography

[1] M. Abadi and L. Cardelli. On subtyping and matching. In Walter Olthoff, editor, Proceedings of the 9th European Conference on Object-Oriented Programming (ECOOP’95), volume 952 of LNCS, pages 145–167, Berlin, GER, August 1995. Springer.

[2] M. Abadi and L. Cardelli. A Theory of Objects. Springer-Verlag, New York, 1996.

[3] A. Aiken, E. Wimmers, and J.Palsberg. Optimal representation of polymorphic types with subtyping. Technical Report UCB/CSD-96-909, University of California, Berkeley, July 1996.

[4] R. Amadio and L. Cardelli. Subtyping recursive types. In Proceedings of the 18th ACM Symposium on Principles of Programming Languages, pages 104–118, Orlando, Florida, january 1991. ACM Press.

[5] M. Benke. Efficient type reconstruction in the presence of inheritance. In Mathematical Foundations of Computer Science, pages 272–80. Springer-Verlag, 1993.

[6] M. Brandt and F. Henglein. Coinductive axiomatization of recursive type equality and subtyping. In Roger Hindley, editor, Proc. 3d Int’l Conf. on Typed Lambda Calculi and Applications (TLCA), Nancy, France, April 2-4, 1997, Lecture Notes in Computer Science (LNCS). Springer-Verlag, January 1997. To appear.

[7] L. Cardelli and P. Wegner. On understanding types, data abstraction, and polymorphism. ACM Computing Surveys, 17(4):471–522, December 1985.

[8] G. Castagna and B. Pierce. Decidable bounded quantification. In Proceedings of the Twenty-First ACM Symposium on Principles of Programming Languages (POPL), Portland, Oregon. ACM Press, January 1994.

[9] G. Castagna and B. Pierce. Corrigendum: Decidable bounded quantification. In Proceedings of the Twenty-Second ACM Symposium on Principles of Programming Languages (POPL), Portland, Oregon. ACM Press, January 1995.

[10] M. Coppo and M. Dezani-Ciancaglini. An extension of the basic functionality theory for the λ-calculus. Notre-Dame Journal of Formal Logic, 21(4):685– 693, October 1980.

[11] P.-L. Curien and G. Ghelli. Coherence of subsumption. In CAAP: Colloquium on Trees in Algebra and Programming. LNCS, Springer-Verlag, 1990.

[12] P. Curtis. Constrained quantification in polymorphic type analysis. Technical Report CSL-90-1, Xerox Palo Alto Research Center, February 1990.

[13] L. Damas and R. Milner. Principal type schemes for functional programs. In Proceedings of the 9th ACM Symposium on Principles of Programming Languages, pages 207–212, 1982.

[14] D. Duggan. Polymorphic methods with self types for ML-like languages. Technical Report CS-95-03, University of Waterloo, 1995.

[15] J. Eifrig, S. Smith, and V. Trifonov. Sound polymorphic type inference for objects. In OOPSLA, 1995.

[16] J. Eifrig, S. Smith, and V. Trifonov. Type inference for recursively constrained types and its applications to OOP. In Conference on Mathematical Foundations of Programming Languages, 1995.

[17] A. Frey. Satisfying systems of subtype inequalities in polynomial space. Proc. 4th International Static Analysis Symposium 1997.

[18] Y. Fuh and P. Mishra. Polymorphic subtype inference: Closing the theorypractice gap. In Proceedings of the 22nd International Conference on Theory and Practice of Software Development, pages 167–183, Barcelona, Spain, March 1989. Springer-Verlag.

[19] F. Henglein. Syntactic properties of polymorphic subtyping. TOPPS Technical Report (D-report series) D-293, DIKU, University of Copenhagen, May 1996.

[20] F. Henglein. Breaking through the n^{3} barrier: Faster object type inference. In Benjamin Pierce, editor, Proc. 4th Int’l Workshop on Foundations of Object-Oriented Languages (FOOL), Paris, France, January 1997. Published electronically at URL http://www.cs.williams.edu/˜ kim/FOOL/.

[21] F. Henglein and J. Rehof. The complexity of subtype entailment for simple types. TOPPS D-report D-319, DIKU, University of Copenhagen, Universitetsparken 1, DK-2100 Copenhagen, Denmark, January 1997.

[22] M. Hoang and J. Mitchell. Lower bounds on type inference with subtypes. In Conference Record of the 22nd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL’95), pages 176–185, San Francisco, California, January 22–25, 1995. ACM Press.

[23] T. Jim. What are principal typings and what are they good for? In Conference Record of POPL ’96: The 23^{rd} ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, pages 42–53, St. Petersburg Beach, Florida, 21–24 January 1996.

[24] D. Kozen, J. Palsberg, and M. Schwartzbach. Efficient inference of partial types. Journal of Computer and System Sciences, 49(2):306–324, October 1994.

[25] D. Kozen, J. Palsberg, and M. Schwarzbach. Efficient recursive subtyping. In Proc 20th Annual ACM SIGPLAN-SIGACT symposium on Principles of Programming Languages, pages 419–428. ACM Press, January 1993.

[26] D. Miller. Unification under a mixed prefix. Journal of Symbolic Computation, 14:321–358, 1992.

[27] R. Milner, M. Tofte, and R. Harper. The Definition of Standard ML. MIT Press, Cambridge, MA, 1991.

[28] J. Mitchell. Coercion and type inference (summary). In Proceedings of the Eleventh ACM Symposium on Principles of Programming Languages, pages 275–185. ACM Press, 1984.

[29] J. Mitchell. Polymorphic type inference and containment. Information and Computation, 76(2/3):211–249, February/March 1988. An extended version of the paper that appeared in the 1984 Semantics of Data Types Symposium, LNCS 173, pages 257–278.

[30] P. Nevermann and I. Rival. Holes in ordered sets. Graphs and Combinatorics, pages 339–350, 1985.

[31] M. Odersky and K. Läufer. Putting type annotations to work. In Conference Record of POPL ’96: The 23rd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, St. Petersberg Beach, Florida, pages 54–67, New York, N.Y., January 1996. ACM.

[32] J. Palsberg. Efficient type inference for object types. In 9th Annual IEEE Symposium on Logic in Computer Science, pages 186–195, Paris, France, July 1994. IEEE Computer Society Press.

[33] J. Palsberg and T. Jim. Type inference in systems of recursive types with subtyping. Manuscript, 1997.

[34] J. Palsberg and S. Smith. Constrained types and their expressiveness. ACM Transactions on Programming Languages and Systems, 18(5):519–527, September 1996.

[35] J. Palsberg, M. Wand, and P. O’Keefe. Type inference with non-structural subtyping. Formal Aspects of Computing, 9:49–67, 1997.

[36] J. Peterson, K. Hammond, et al. Report on the programming language Haskell, a non-strict purely-functional programming language, version 1.3. Technical report, Yale University, May 1996.

[37] B. Pierce and D. Turner. Simple type-theoretic foundations for objectoriented programming. Journal of Functional Programming, 4(2):207–247, April 1994. A preliminary version appeared in Principles of Programming Languages, 1993, and as University of Edinburgh technical report ECS-LFCS-92-225, under the title “Object-Oriented Programming Without Recursive Types”.

[38] Benjamin C. Pierce. Programming with Intersection Types and Bounded Polymorphism. PhD thesis, Carnegie Mellon University, December 1991. Available as School of Computer Science technical report CMU-CS-91-205.

[39] F. Pottier. Type inference and simplification for recursively constrained types. In Actes du GDR Programmation 1995 (journée du pôle Programmation Fonctionnelle), November 1995.

[40] F. Pottier. Simplifying subtype constraints. In Proceedings of the International Conference on Functional Programming, pages 122–133, Paris, France, May 1996. ACM Press.

[41] V. Pratt and J. Tiuryn. Satisfiability of inequalities in a poset. Fundamenta Infomaticae, 28(1,2):165–182, 1996.

[42] A. Quilliot. An application of the helly property to the partially ordered sets. J. Combinatorial Theory (A), 35:185–198, 1983.

[43] J. Rehof. Minimal typings in atomic subtyping. In Proceedings POPL ’97, 24th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, Paris, France. ACM Press, January 1997.

[44] D. Rémy. A case study of typechecking with constrained types: Typing record concatenation. Presented at the workshop on Advances in types for computer science at the Newton Institute, Cambridge, UK, August 1995.

[45] John Reynolds. Using category theory to design implicit conversions and generic operators. In N. D. Jones, editor, Proceedings of the Aarhus Workshop on Semantics-Directed Compiler Generation, number 94 in Lecture Notes in Computer Science. Springer-Verlag, January 1980.

[46] G. Smith. Polymorphic type inference with overloading and subtyping. In TAPSOFT93, pages 671–685, 1993.

[47] M. Sulzmann, M. Odersky, and M. Wehr. Type inference with constrained types. In Benjamin Pierce, editor, Proc. 4th Int’l Workshop on Foundations of Object-Oriented Languages (FOOL), Paris, France, January 1997. Published electronically at URL http://www.cs.williams.edu/˜ kim/FOOL/.

[48] S. Thatte. Type inference with partial types. In Timo Lepistö and Arto Salomaa, editors, Proceedings of the 15th International Colloquium on Automata, Languages and Programming, volume 317 of LNCS, pages 615–629, Berlin, July 1988. Springer.

[49] J. Tiuryn. Subtyping over a lattice. Gödel colloquium talk, Vienna 1997.

[50] J. Tiuryn. Subtype inequalities. In Proceedings of the 7th Annual Symposium on Logic in Computer Science, pages 308–315, Santa Cruz, California, June 1992. IEEE Computer Society Press.

[51] J. Tiuryn and P. Urzyczyn. The subtyping problem for second-order types is undecidable. In Proceedings, 11th Annual IEEE Symposium on Logic in Computer Science, pages 74–85, New Brunswick, New Jersey, 27–30 July 1996. IEEE Computer Society Press.

[52] V. Trifonov and S. Smith. Subtyping constrained types. In Proceedings of the Static Analysis Symposium, pages 349–365, Aachen, Germany, 1996. Springer-Verlag.

[53] J.B. Wells. A direct algorithm for type inference in the rank-2 fragment of the second-order λ-calculus. In ACM symposium on Lisp and Functional Programming, pages 196–207, 1994.