Type inference with algebraic universes in the Calculus of Inductive Constructions

Abstract

We describe an algebraic system of universes and a typechecking algorithm for universe constraints in a version of the extended calculus of constructions with inductive types. The use of algebraic universes ensures that the graph of constraints only contains universes already present in the term to type. This algorithm, used in the typechecker of the Coq proof assistant, refines Huet and Harper-Pollack algorithms for typical ambiguity. The Extended Calculus of Constructions (ECC) [11] is a type theory extending the Calculus of Constructions (CC) [3, 5] with a stratified cumulative hierarchy of type universes, and Σ-types. We call CCω the extension of CC with the hierarchy of ECC but without Σ-types. CCω can be seen as a Pure Type System (PTS) [1, 13] extended with subtyping. The signature of CCω as a PTS contains the impredicative sort of propositions Prop and a stratified hierarchy of predicative types Typei, for i ≥ 1. The sort of propositions itself is in Type1 and the stratification is expressed by the axioms Typei : Typei+1. The subtyping is generated from the inclusion Prop ⊂ Type1 and Typei ⊂ Typei+1, by extension to products, covariantly on the codomain. CCω differs from the “Generalised” Calculus of Constructions (GCC) [4] which extends the Calculus of Constructions with a slightly weaker cumulative hierarchy of type universes (it does not have Prop ⊂ Type1 and it is not compatible with products). Thanks to its compatibility with products, the hierarchy of CCω (comparatively called fully cumulative in Luo [11]) is easier to work with. Along the formulae-as-type paradigm, types in Prop denotes propositions and terms typed of these types are proofs of these propositions. Thanks to the normalisation of ECC [11], hence of CCω, type inference and type checking in CCω are decidable (terms of CCω are written à la Church with explicitly typed abstractions). Type checkers for CCω (or GCC) have been implemented on computer from 1985 [8, 12] (with further extensions leading to the Coq [2] and LEGO proof assistants). To abstract over the use of type universe levels, Huet [10] proposed to introduce some form of typical ambiguity in the type checker of CCω: each time a reference to a type universe is given, its level is left anonymous (i.e. the user writes Type without mentioning the level) and the type checker handles a graph of constraints between anonymous universes and relies on a decidable graph algorithm of non-circularity check to ensure the mapping of anonymous universes to actual numerical levels. Similarly, Pollack introduced typical ambiguity in the type-checker of GCC, but still retaining the possibility of using explicit level of universes. The theory of anonymous universes in GCC has been carefully studied (among other related concepts) by Harper and Pollack [9]. Both algorithms use universe variables that are associated to every occurrence of Type occurring in a derivation of some judgement Γ ` t : T . The derivation enforces constraints on the universe variables and the derivation is sound iff the graph of constraints is acyclic (i.e. if the universe variables are mappable to positive integers so that the constraints are true). The current paper refines Huet’s algorithm for Coq by reducing the size of the graph of constraints. This is achieved by using algebraic universe levels in a free algebra generated by the numerical levels, a successor operator suc and a maximum-of-two-integers operator max. As a result, the graph of constraints for a type inference problem Γ ` t : ? only contains nodes for the occurrences of Type occurring in Γ and t. All other occurrences, the ones occurring inside the derivation and the ones occurring in the type of t (if typable) are algebraic expressions built on the formers. We call CC ωd the extension of CCω with algebraic universes to which definitions also are added. In the first section, we give a type-directed presentation of CC ωd. In the second section, we explore the form of the algebraic constraints that appear in derivations of CC ωd. Especially, we show that under special reasonable conditions, constraints between algebraic universes are reducible without time complexity penalty to simple constraints between universe variables only. The third section addresses the issue of the Calculus of Inductive Constructions (CCI) as implemented in the Coq proof assistant [2]. Especially, the previous analysis is extended to the subset of CCI that is relevant for the question of type universes. We conclude with a few remarks. 1 CC ωd: type-checking CCω with algebraic universes We present an extension of CC with Luo’s full cumulative hierarchy of universes, and definitions. Our presentation is syntax-directed, in the same vein as HarperPollack’s presentation of GCC (e.g. Tables 4 or 9 in [9]) or, implicitly, as Luo’s type inference algorithm (Definition 6.2.2 in [11], see also Table 5-3 in [12]). Since it is syntax-directed, a derivation Γ ` t : T can directly be interpreted as a type inference algorithm taking an inference problem Γ ` t : ? as input and returning, if successful, a type T of t. More generally, our system does not only include type universes at given numerical levels. It also has universes at floating levels represented by universe variables so that the a type inference problem Γ ` t : ? actually returns a judgement Γ C ` t : T where T is a type for t and C is a graph of constraints on the universe variables to be satisfied to ensure the typability of t. In case all universe variables are distinct in Γ and t, this gives a system similar to the system with anonymous universes (where each anonymous universe is associated to a fresh universe variable) in Harper-Pollack [9] (Table 8, or Table 12 for a system with definitions). However, in our system, the inferred type T , if any, is not a type scheme built on fresh universe variables, but an algebraic expression built on the universe variables occurring in Γ and t. And similarly for the set of constraints C. 1.1 Syntax Let i ranges over numerical universes (i ∈ N∗). Let α ranges over an infinite set of universe variables. An atomic universe is either a numerical universe or a universe variable. An algebraic universe is an expression built from atomic universes by means of a formal successor function suc and a formal maximum function max. l ::= i | α e ::= l | suc(e) | max(e, e) A sort in CC ωd is either a type universe or the propositional sort Prop. s ::= Type(e) | Prop To define terms, we assume the existence of an infinite set of variable names whose inhabitants are written x, y, z, ... The terms are defined by the following grammar. t, u, v, T, U, V ::= x | ∀x : t.t | λx : t.t | (t t) | s We consider terms up to α-conversion. By t{u/x} we denote the captureavoiding substitution of x by u in t. Typing contexts, or simply contexts, are lists of declarations, of the form x : T , or definitions, of the form x := t : T . Formally, a context is defined by the grammar Γ ::= | Γ, x : t | Γ, x := t : T where denotes an empty context (left implicit, unless it causes an ambiguity). We write (x : T ) ∈ Γ (respectively (x := t : T ) ∈ Γ to say that the corresponding declaration (respectively definition) is part of Γ . 1.2 Conversion, subtyping and constraints Typing in CC ωd is up to conversion. Since the system has definitions the convertibility is not only based on β-reduction but also on δ-reduction whose effect is to replace variables defined in the context by their value. Thanks to the strong normalisation of ECC and the confluence of β-reduction (which obviously still holds in presence of δ-reduction), convertibility of typed terms can be decided by reduction to weak head normal form. For this, it is useful to characterise the set of weak head normal forms. We first define evaluation contexts E as follows.

Cite this paper

@inproceedings{Herbelin2005TypeIW, title={Type inference with algebraic universes in the Calculus of Inductive Constructions}, author={Hugo Herbelin}, year={2005} }