# Coq Reference Manual

# Chapter 4 Calculus of Inductive Constructions

- 4.1 The terms
- 4.2 Typed terms
- 4.3 Conversion rules
- 4.4 Derived rules for environments
- 4.5 Inductive Definitions
- 4.6 Co-inductive types
- 4.7 Cic: the Calculus of Inductive Construction with impredicative Set

The underlying formal language of Coq is a Calculus of Constructions with Inductive Definitions. It is presented in this chapter. For Coq version V7, this Calculus was known as the Calculus of (Co)Inductive Constructions (Cic in short). The underlying calculus of Coq version V8.0 and up is a weaker calculus where the sort Set satisfies predicative rules. We call this calculus the Predicative Calculus of (Co)Inductive Constructions (pCic in short). In Section 4.7 we give the extra-rules for Cic. A compiling option of Coq allows to type-check theories in this extended system.

In pCic all objects have a type. There are types for functions (or programs), there are atomic types (especially datatypes)... but also types for proofs and types for the types themselves. Especially, any object handled in the formalism must belong to a type. For instance, the statement “for all x, P” is not allowed in type theory; you must say instead: “for all x belonging to T, P”. The expression “x belonging to T” is written “x:T”. One also says: “x has type T”. The terms of pCic are detailed in Section 4.1.

In pCic there is an internal reduction mechanism. In particular, it allows to decide if two programs are intentionally equal (one says convertible). Convertibility is presented in section 4.3.

The remaining sections are concerned with the type-checking of terms. The beginner can skip them.

The reader seeking a background on the Calculus of Inductive Constructions may read several papers. Giménez and Castéran [68] provide an introduction to inductive and co-inductive definitions in Coq. In their book [14], Bertot and Castéran give a precise description of the pCic based on numerous practical examples. Barras [9], Werner [137] and Paulin-Mohring [119] are the most recent theses dealing with Inductive Definitions. Coquand-Huet [29, 30, 31] introduces the Calculus of Constructions. Coquand-Paulin [32] extended this calculus to inductive definitions. The pCic is a formulation of type theory including the possibility of inductive constructions, Barendregt [6] studies the modern form of type theory.

## 4.1 The terms

In most type theories, one usually makes a syntactic distinction between types and terms. This is not the case for pCic which defines both types and terms in the same syntactical structure. This is because the type-theory itself forces terms and types to be defined in a mutual recursive way and also because similar constructions can be applied to both terms and types and consequently can share the same syntactic structure.

Consider for instance the → constructor and assume nat is the type of natural numbers. Then → is used both to denote nat→nat which is the type of functions from nat to nat, and to denote nat → Prop which is the type of unary predicates over the natural numbers. Consider abstraction which builds functions. It serves to build “ordinary” functions as fun x:nat ⇒ (mult x x) (assuming mult is already defined) but may build also predicates over the natural numbers. For instance fun x:nat ⇒ (x=x) will represent a predicate P, informally written in mathematics P(x)≡ x=x. If P has type nat → Prop, (P x) is a proposition, furthermore forall x:nat,(P x) will represent the type of functions which associate to each natural number n an object of type (P n) and consequently represent proofs of the formula “∀ x.P(x)”.

### 4.1.1 Sorts

When manipulated as terms, types have themselves a type which is called a sort.

There is an infinite well-founded typing hierarchy of sorts whose base sorts are Prop and Set.

The sort Prop intends to be the type of logical propositions. If M is a logical proposition then it denotes the class of terms representing proofs of M. An object m belonging to M witnesses the fact that M is provable. An object of type Prop is called a proposition.

The sort Set intends to be the type of small sets. This includes data types such as booleans and naturals, but also products, subsets, and function types over these data types.

Prop and Set themselves can be manipulated as ordinary terms. Consequently they also have a type. Because assuming simply that Set has type Set leads to an inconsistent theory, the language of pCic has infinitely many sorts. There are, in addition to Set and Prop a hierarchy of universes Type(i) for any integer i.

Like Set, all of the sorts Type(i) contain small sets such as booleans, natural numbers, as well as products, subsets and function types over small sets. But, unlike Set, they also contain large sets, namely the sorts Set and Type(j) for j<i, and all products, subsets and function types over these sorts.

Formally, we call S the set of sorts which is defined by:

S ≡ {Prop,Set,Type(i)| i ∈ ℕ}

The sorts enjoy the following properties1: Prop:Type(1), Set:Type(1) and Type(i):Type(i+1).

The user will never mention explicitly the index i when referring to the universe Type(i). One only writes Type. The system itself generates for each instance of Type a new index for the universe and checks that the constraints between these indexes can be solved. From the user point of view we consequently have Type :Type.

We shall make precise in the typing rules the constraints between the indexes.

##### Implementation issues

In practice, the Type hierarchy is implemented using algebraic universes. An algebraic universe u is either a variable (a qualified identifier with a number) or a successor of an algebraic universe (an expression u+1), or an upper bound of algebraic universes (an expression max(u1,...,un)), or the base universe (the expression 0) which corresponds, in the arity of sort-polymorphic inductive types, to the predicative sort Set. A graph of constraints between the universe variables is maintained globally. To ensure the existence of a mapping of the universes to the positive integers, the graph of constraints must remain acyclic. Typing expressions that violate the acyclicity of the graph of constraints results in a Universe inconsistency error (see also Section 2.10).

### 4.1.2 Constants

Besides the sorts, the language also contains constants denoting objects in the environment. These constants may denote previously defined objects but also objects related to inductive definitions (either the type itself or one of its constructors or destructors).

Remark. In other presentations of pCic, the inductive objects are not seen as external declarations but as first-class terms. Usually the definitions are also completely ignored. This is a nice theoretical point of view but not so practical. An inductive definition is specified by a possibly huge set of declarations, clearly we want to share this specification among the various inductive objects and not to duplicate it. So the specification should exist somewhere and the various objects should refer to it. We choose one more level of indirection where the objects are just represented as constants and the environment gives the information on the kind of object the constant refers to.

Our inductive objects will be manipulated as constants declared in the environment. This roughly corresponds to the way they are actually implemented in the Coq system. It is simple to map this presentation in a theory where inductive objects are represented by terms.

### 4.1.3 Terms

Terms are built from variables, global names, constructors, abstraction, application, local declarations bindings (“let-in” expressions) and product.

From a syntactic point of view, types cannot be distinguished from terms, except that they cannot start by an abstraction, and that if a term is a sort or a product, it should be a type.

More precisely the language of the Calculus of Inductive Constructions is built from the following rules:

1. the sorts Set, Prop, Type are terms.
2. names for global constants of the environment are terms.
3. variables are terms.
4. if x is a variable and T, U are terms then ∀ x:T,U (forall x:T,U in Coq concrete syntax) is a term. If x occurs in U, ∀ x:T,U reads as “for all x of type T, U”. As U depends on x, one says that ∀ x:T,U is a dependent product. If x doesn’t occurs in U then ∀ x:T,U reads as “if T then U”. A non dependent product can be written: T → U.
5. if x is a variable and T, U are terms then λ x:T . U (fun x:T⇒ U in Coq concrete syntax) is a term. This is a notation for the λ-abstraction of λ-calculus [8]. The term λ x:T . U is a function which maps elements of T to U.
6. if T and U are terms then (T U) is a term (T U in Coq concrete syntax). The term (T U) reads as “T applied to U”.
7. if x is a variable, and T, U are terms then let x:=T in U is a term which denotes the term U where the variable x is locally bound to T. This stands for the common “let-in” construction of functional programs such as ML or Scheme.

##### Notations.

Application associates to the left such that (t t1… tn) represents (… (t t1)… tn). The products and arrows associate to the right such that ∀ x:A,B→ C→ D represents ∀ x:A,(B→ (C→ D)). One uses sometimes ∀ x y:A,B or λ x y:A. B to denote the abstraction or product of several variables of the same type. The equivalent formulation is ∀ x:A, ∀ y:A,B or λ x:A . λ y:A . B

##### Free variables.

The notion of free variables is defined as usual. In the expressions λ x:T. U and ∀ x:T, U the occurrences of x in U are bound. They are represented by de Bruijn indexes in the internal structure of terms.

##### Substitution.

The notion of substituting a term t to free occurrences of a variable x in a term u is defined as usual. The resulting term is written u{x/t}.

## 4.2 Typed terms

As objects of type theory, terms are subjected to type discipline. The well typing of a term depends on an environment which consists in a global environment (see below) and a local context.

##### Local context.

A local context (or shortly context) is an ordered list of declarations of variables. The declaration of some variable x is either an assumption, written x:T (T is a type) or a definition, written x:=t:T. We use brackets to write contexts. A typical example is [x:T;y:=u:U;z:V]. Notice that the variables declared in a context must be distinct. If Γ declares some x, we write x ∈ Γ. By writing (x:T) ∈ Γ we mean that either x:T is an assumption in Γ or that there exists some t such that x:=t:T is a definition in Γ. If Γ defines some x:=t:T, we also write (x:=t:T) ∈ Γ. Contexts must be themselves well formed. For the rest of the chapter, the notation Γ::(y:T) (resp. Γ::(y:=t:T)) denotes the context Γ enriched with the declaration y:T (resp. y:=t:T). The notation [] denotes the empty context.

We define the inclusion of two contexts Γ and Δ (written as Γ ⊂ Δ) as the property, for all variable x, type T and term t, if (x:T) ∈ Γ then (x:T) ∈ Δ and if (x:=t:T) ∈ Γ then (x:=t:T) ∈ Δ.

A variable x is said to be free in Γ if Γ contains a declaration y:T such that x is free in T.

##### Environment.

Because we are manipulating global declarations (constants and global assumptions), we also need to consider a global environment E.

An environment is an ordered list of declarations of global names. Declarations are either assumptions or “standard” definitions, that is abbreviations for well-formed terms but also definitions of inductive objects. In the latter case, an object in the environment will define one or more constants (that is types and constructors, see Section 4.5).

An assumption will be represented in the environment as Assum(Γ)(c:T) which means that c is assumed of some type T well-defined in some context Γ. An (ordinary) definition will be represented in the environment as Def(Γ)(c:=t:T) which means that c is a constant which is valid in some context Γ whose value is t and type is T.

The rules for inductive definitions (see section 4.5) have to be considered as assumption rules to which the following definitions apply: if the name c is declared in E, we write c ∈ E and if c:T or c:=t:T is declared in E, we write (c : T) ∈ E.

##### Typing rules.

In the following, we assume E is a valid environment w.r.t. inductive definitions. We define simultaneously two judgments. The first one E[Γ] ⊢ t : T means the term t is well-typed and has type T in the environment E and context Γ. The second judgment WF(E)[Γ] means that the environment E is well-formed and the context Γ is a valid context in this environment. It also means a third property which makes sure that any constant in E was defined in an environment which is included in Γ 2.

A term t is well typed in an environment E iff there exists a context Γ and a term T such that the judgment E[Γ] ⊢ t : T can be derived from the following rules.

W-E

  WF([])[[]]

W-S

  E[Γ] ⊢ T : s s ∈ S x ∉Γ
  WF(E)[Γ::(x:T)]

  E[Γ] ⊢ t : T x ∉Γ
  WF(E)[Γ::(x:=t:T)]

Def

  E[Γ] ⊢ t : T c ∉ E ⋃ Γ
  WF(E;Def(Γ)(c:=t:T))[Γ]

Assum

  E[Γ] ⊢ T : s s ∈ S c ∉ E ⋃ Γ
  WF(E;Assum(Γ)(c:T))[Γ]

Ax

  WF(E)[Γ]
  E[Γ] ⊢ Prop : Type(p)

  WF(E)[Γ]
  E[Γ] ⊢ Set : Type(q)

  WF(E)[Γ] i<j
  E[Γ] ⊢ Type(i) : Type(j)

Var

  WF(E)[Γ] (x:T) ∈ Γ or (x:=t:T) ∈ Γ for some t
  E[Γ] ⊢ x : T

Const

  WF(E)[Γ] (c:T) ∈ E or (c:=t:T) ∈ E for some t
  E[Γ] ⊢ c : T

Prod

  E[Γ] ⊢ T : s s ∈ S E[Γ::(x:T)] ⊢ U : Prop
  E[Γ] ⊢ ∀ x:T,U : Prop

  E[Γ] ⊢ T : s s ∈{Prop, Set} E[Γ::(x:T)] ⊢ U : Set
  E[Γ] ⊢ ∀ x:T,U : Set

  E[Γ] ⊢ T : Type(i) i≤ k E[Γ::(x:T)] ⊢ U : Type(j) j ≤ k
  E[Γ] ⊢ ∀ x:T,U : Type(k)

Lam

  E[Γ] ⊢ ∀ x:T,U : s E[Γ::(x:T)] ⊢ t : U
  E[Γ] ⊢ λ x:T. t : ∀ x:T, U

App

  E[Γ] ⊢ t : ∀ x:U,T E[Γ] ⊢ u : U
  E[Γ] ⊢ (t u) : T{x/u}

Let

  E[Γ] ⊢ t : T E[Γ::(x:=t:T)] ⊢ u : U
  E[Γ] ⊢ let x:=t in u : U{x/t}

Remark: We may have let x:=t in u well-typed without having ((λ x:T. u) t) well-typed (where T is a type of t). This is because the value t associated to x may be used in a conversion rule (see Section 4.3).

## 4.3 Conversion rules

##### β-reduction.

We want to be able to identify some terms as we can identify the application of a function to a given argument with its result. For instance the identity function over a given type T can be written λ x:T. x. In any environment E and context Γ, we want to identify any object a (of type T) with the application ((λ x:T. x) a). We define for this a reduction (or a conversion) rule we call β:

E[Γ] ⊢ ((λ x:T. t) u) ▷β t{x/u}

We say that t{x/u} is the β-contraction of ((λ x:T. t) u) and, conversely, that ((λ x:T. t) u) is the β-expansion of t{x/u}.

According to β-reduction, terms of the Calculus of Inductive Constructions enjoy some fundamental properties such as confluence, strong normalization, subject reduction. These results are theoretically of great importance but we will not detail them here and refer the interested reader to [23].

##### ι-reduction.

A specific conversion rule is associated to the inductive objects in the environment. We shall give later on (see Section 4.5.4) the precise rules but it just says that a destructor applied to an object built from a constructor behaves as expected. This reduction is called ι-reduction and is more precisely studied in [118, 137].

##### δ-reduction.

We may have defined variables in contexts or constants in the global environment. It is legal to identify such a reference with its value, that is to expand (or unfold) it into its value. This reduction is called δ-reduction and shows as follows.

E[Γ] ⊢ x ▷δ t if (x:=t:T) ∈ Γ E[Γ] ⊢ c ▷δ t if (c:=t:T) ∈ E

##### ζ-reduction.

Coq allows also to remove local definitions occurring in terms by replacing the defined variable by its value. The declaration being destroyed, this reduction differs from δ-reduction. It is called ζ-reduction and shows as follows.

E[Γ] ⊢ let x:=u in t ▷ζ t{x/u}

##### η-conversion.

An other important concept is η-conversion. It is to identify any term t of functional type ∀ x:T, U with its so-called η-expansion λ x:T. (t x) for x an arbitrary variable name fresh in t.

The notion of η-reduction λ x:T. (t x)▷t (for x not occurring in t) is not type-sound because of subtyping (think about λ x:Type(1). (f x) of type ∀ x:Type(1), Type(1) for f of type ∀ x:Type(2), Type(1)). On the other side, η-expansion requires to know T and hence requires types. Hence, neither η-expansion nor η-reduction can be type-safely considered on terms we do not know the type. However, η can be used as a conversion rule.

##### Convertibility.

Let us write E[Γ] ⊢ t ▷ u for the contextual closure of the relation t reduces to u in the environment E and context Γ with one of the previous reduction β, ι, δ or ζ.

We say that two terms t1 and t2 are βιδζη-convertible, or simply convertible, or equivalent, in the environment E and context Γ iff there exist terms u1 and u2 such that E[Γ] ⊢ t1 ▷ … ▷ u1 and E[Γ] ⊢ t2 ▷ … ▷ u2 and either u1 and u2 are identical, or they are convertible up to η-expansion, i.e. u1 is λ x:T. u′1 and u2 x is recursively convertible to u′1, or, symmetrically, u2 is λ x:T. u′2 and u1 x is recursively convertible to u′2. We then write E[Γ] ⊢ t1 =βδιζη t2.

The convertibility relation allows to introduce a new typing rule which says that two convertible well-formed types have the same inhabitants.

At the moment, we did not take into account one rule between universes which says that any term in a universe of index i is also a term in the universe of index i+1 (this is the cumulativity rule of pCic). This property is included into the conversion rule by extending the equivalence relation of convertibility into a subtyping relation inductively defined by:

1. if E[Γ] ⊢ t =βδιζη u then E[Γ] ⊢ t ≤βδιζη u,
2. if i ≤ j then E[Γ] ⊢ Type(i) ≤βδιζη Type(j),
3. for any i, E[Γ] ⊢ Set ≤βδιζη Type(i),
4. E[Γ] ⊢ Prop ≤βδιζη Set, hence, by transitivity, E[Γ] ⊢ Prop ≤βδιζη Type(i), for any i
5. if E[Γ] ⊢ T =βδιζη U and E[Γ::(x:T)] ⊢ T′ ≤βδιζη U′ then E[Γ] ⊢ ∀ x:T, T′ ≤βδιζη ∀ x:U, U′.

The conversion rule is now exactly:

Conv

  E[Γ] ⊢ U : s E[Γ] ⊢ t : T E[Γ] ⊢ T ≤βδιζη U
  E[Γ] ⊢ t : U

##### Normal form.

A term which cannot be any more reduced is said to be in normal form. There are several ways (or strategies) to apply the reduction rule. Among them, we have to mention the head reduction which will play an important role (see Chapter 8). Any term can be written as λ x1:T1. … λ xk:Tk . (t0 t1… tn) where t0 is not an application. We say then that t0 is the head of t. If we assume that t0 is λ x:T. u0 then one step of β-head reduction of t is:

λ x1:T1. … λ xk:Tk. (λ x:T. u0 t1… tn) ▷ λ (x1:T1)…(xk:Tk). (u0{x/t1} t2 … tn)

Iterating the process of head reduction until the head of the reduced term is no more an abstraction leads to the β-head normal form of t:

t ▷ … ▷ λ x1:T1. …λ xk:Tk. (v u1 … um)

where v is not an abstraction (nor an application). Note that the head normal form must not be confused with the normal form since some ui can be reducible.

Similar notions of head-normal forms involving δ, ι and ζ reductions or any combination of those can also be defined.

## 4.4 Derived rules for environments

From the original rules of the type system, one can derive new rules which change the context of definition of objects in the environment. Because these rules correspond to elementary operations in the Coq engine used in the discharge mechanism at the end of a section, we state them explicitly.

##### Mechanism of substitution.

One rule which can be proved valid, is to replace a term c by its value in the environment. As we defined the substitution of a term for a variable in a term, one can define the substitution of a term for a constant. One easily extends this substitution to contexts and environments.

##### Substitution Property:

WF(E;Def(Γ)(c:=t:T); F)[Δ]
WF(E; F{c/t})[Δ{c/t}]

##### Abstraction.

One can modify the context of definition of a constant c by abstracting a constant with respect to the last variable x of its defining context. For doing that, we need to check that the constants appearing in the body of the declaration do not depend on x, we need also to modify the reference to the constant c in the environment and context by explicitly applying this constant to the variable x. Because of the rules for building environments and terms we know the variable x is available at each stage where c is mentioned.

##### Abstracting property:

WF(E; Def(Γ::(x:U))(c:=t:T); F)[Δ] WF(E)[Γ]
WF(E;Def(Γ)(c:=λ x:U. t:∀ x:U,T); F{c/(c x)})[Δ{c/(c x)}]

##### Pruning the context.

We said the judgment WF(E)[Γ] means that the defining contexts of constants in E are included in Γ. If one abstracts or substitutes the constants with the above rules then it may happen that the context Γ is now bigger than the one needed for defining the constants in E. Because defining contexts are growing in E, the minimum context needed for defining the constants in E is the same as the one for the last constant. One can consequently derive the following property.

##### Pruning property:

WF(E; Def(Δ)(c:=t:T))[Γ]
WF(E;Def(Δ)(c:=t:T))[Δ]

## 4.5 Inductive Definitions

A (possibly mutual) inductive definition is specified by giving the names and the type of the inductive sets or families to be defined and the names and types of the constructors of the inductive predicates. An inductive declaration in the environment can consequently be represented with two contexts (one for inductive definitions, one for constructors).

Stating the rules for inductive definitions in their general form needs quite tedious definitions. We shall try to give a concrete understanding of the rules by precising them on running examples. We take as examples the type of natural numbers, the type of parameterized lists over a type A, the relation which states that a list has some given length and the mutual inductive definition of trees and forests.

### 4.5.1 Representing an inductive definition

#### Inductive definitions without parameters

As for constants, inductive definitions can be defined in a non-empty context.
We write Ind(Γ)(ΓI:=ΓC ) an inductive definition valid in a context Γ, a context of definitions ΓI and a context of constructors ΓC.

##### Examples.

The inductive declaration for the type of natural numbers will be:

Ind()(nat:Set:=O:nat,S:nat→nat )

In a context with a variable A:Set, the lists of elements in A are represented by:

Ind(A:Set)(List:Set:=nil:List,cons : A → List → List )

Assuming ΓI is [I1:A1;…;Ik:Ak], and ΓC is [c1:C1;…;cn:Cn], the general typing rules are, for 1≤ j≤ k and 1≤ i≤ n:

Ind(Γ)(ΓI:=ΓC ) ∈ E
(Ij:Aj) ∈ E

Ind(Γ)(ΓI:=ΓC ) ∈ E
(ci:Ci) ∈ E

#### Inductive definitions with parameters

We have to slightly complicate the representation above in order to handle the delicate problem of parameters. Let us explain that on the example of List. With the above definition, the type List can only be used in an environment where we have a variable A:Set. Generally one want to consider lists of elements in different types. For constants this is easily done by abstracting the value over the parameter. In the case of inductive definitions we have to handle the abstraction over several objects.

One possible way to do that would be to define the type List inductively as being an inductive family of type Set→Set:

Ind()(List:Set→Set:=nil:(∀ A:Set,List A), cons : (∀ A:Set, A → List A → List A) )

There are drawbacks to this point of view. The information which says that for any A, (List A) is an inductively defined Set has been lost. So we introduce two important definitions.

##### Inductive parameters, real arguments.

An inductive definition Ind(Γ)(ΓI:=ΓC ) admits r inductive parameters if each type of constructors (c:C) in ΓC is such that

C≡ ∀ p1:P1,…,∀ pr:Pr,∀ a1:A1, … ∀ an:An, (I p1 … pr t1… tq)

with I one of the inductive definitions in ΓI. We say that q is the number of real arguments of the constructor c.

##### Context of parameters.

If an inductive definition Ind(Γ)(ΓI:=ΓC ) admits r inductive parameters, then there exists a context ΓP of size r, such that ΓP=[p1:P1;…;pr:Pr] and if (t:A) ∈ ΓI,ΓC then A can be written as ∀ p1:P1,… ∀ pr:Pr,A′. We call ΓP the context of parameters of the inductive definition and use the notation ∀ ΓP,A′ for the term A.

##### Remark.

If we have a term t in an instance of an inductive definition I which starts with a constructor c, then the r first arguments of c (the parameters) can be deduced from the type T of t: these are exactly the r first arguments of I in the head normal form of T.

##### Examples.

The List definition has 1 parameter:

Ind()(List:Set→Set:=nil:(∀ A:Set, List A), cons : (∀ A:Set, A → List A → List A) )

This is also the case for this more complex definition where there is a recursive argument on a different instance of List:

Ind()(List:Set→Set:=nil:(∀ A:Set, List A), cons : (∀ A:Set, A → List (A → A) → List A) )

But the following definition has 0 parameters:

Ind()(List:Set→Set:=nil:(∀ A:Set, List A), cons : (∀ A:Set, A → List A → List (A*A)) )

##### Concrete syntax.

In the Coq system, the context of parameters is given explicitly after the name of the inductive definitions and is shared between the arities and the type of constructors. We keep track in the syntax of the number of parameters.

Formally the representation of an inductive declaration will be Ind(Γ)[p](ΓI:=ΓC ) for an inductive definition valid in a context Γ with p parameters, a context of definitions ΓI and a context of constructors ΓC.

The definition Ind(Γ)[p](ΓI:=ΓC ) will be well-formed exactly when Ind(Γ)(ΓI:=ΓC ) is and when p is (less or equal than) the number of parameters in Ind(Γ)(ΓI:=ΓC ).

##### Examples

The declaration for parameterized lists is:

Ind()[1](List:Set→Set:=nil:(∀ A:Set,List A),cons : (∀ A:Set, A → List A → List A) )

The declaration for the length of lists is:

Ind()[1](Length:∀ A:Set, (List A)→ nat→Prop:=Lnil:∀ A:Set, Length A (nil A) O, Lcons :∀ A:Set,∀ a:A, ∀ l:(List A),∀ n:nat, (Length A l n)→ (Length A (cons A a l) (S n)) )

The declaration for a mutual inductive definition of forests and trees is:

Ind()(tree:Set,forest:Set:= node:forest → tree, emptyf:forest,consf:tree → forest → forest )

These representations are the ones obtained as the result of the Coq declaration:

Coq < Inductive nat : Set :=
Coq < | O : nat
Coq < | S : nat -> nat.
Coq < Inductive list (A:Set) : Set :=
Coq < | nil : list A
Coq < | cons : A -> list A -> list A.
Coq < Inductive Length (A:Set) : list A -> nat -> Prop :=
Coq < | Lnil : Length A (nil A) O
Coq < | Lcons :
Coq < forall (a:A) (l:list A) (n:nat),
Coq < Length A l n -> Length A (cons A a l) (S n).
Coq < Inductive tree : Set :=
Coq < node : forest -> tree
Coq < with forest : Set :=
Coq < | emptyf : forest
Coq < | consf : tree -> forest -> forest.

The Coq type-checker verifies that all parameters are applied in the correct manner in the conclusion of the type of each constructors :

In particular, the following definition will not be accepted because there is an occurrence of List which is not applied to the parameter variable in the conclusion of the type of cons’:

Coq < Inductive list’ (A:Set) : Set :=
Coq < | nil’ : list’ A
Coq < | cons’ : A -> list’ A -> list’ (A*A).
Coq < Coq < Error: Last occurrence of "list’" must have "A" as 1st argument in
"A -> list’ A -> list’ (A * A)%type".

Since Coq version 8.1, there is no restriction about parameters in the types of arguments of constructors. The following definition is valid:

Coq < Inductive list’ (A:Set) : Set :=
Coq < | nil’ : list’ A
Coq < | cons’ : A -> list’ (A->A) -> list’ A.
list’ is defined
list’_rect is defined
list’_ind is defined
list’_rec is defined

### 4.5.2 Types of inductive objects

We have to give the type of constants in an environment E which contains an inductive declaration.

Ind-Const
  Assuming ΓI is [I1:A1;…;Ik:Ak], and ΓC is [c1:C1;…;cn:Cn],

  Ind(Γ)[p](ΓI:=ΓC ) ∈ E j=1… k
  (Ij:Aj) ∈ E

  Ind(Γ)[p](ΓI:=ΓC ) ∈ E i=1.. n
  (ci:Ci) ∈ E

##### Example.

We have (List:Set → Set), (cons:∀ A:Set,A→(List A)→ (List A)),
(Length:∀ A:Set, (List A)→nat→Prop), tree:Set and forest:Set.

From now on, we write List_A instead of (List A) and Length_A for (Length A).

### 4.5.3 Well-formed inductive definitions

We cannot accept any inductive declaration because some of them lead to inconsistent systems. We restrict ourselves to definitions which satisfy a syntactic criterion of positivity. Before giving the formal rules, we need a few definitions:

##### Definitions

A type T is an arity of sort s if it converts to the sort s or to a product ∀ x:T,U with U an arity of sort s. (For instance A→ Set or ∀ A:Prop,A→ Prop are arities of sort respectively Set and Prop). A type of constructor of I is either a term (I t1… tn) or ∀ x:T,C with C recursively a type of constructor of I.

The type of constructor T will be said to satisfy the positivity condition for a constant X in the following cases:

- T=(X t1… tn) and X does not occur free in any ti
- T=∀ x:U,V and X occurs only strictly positively in U and the type V satisfies the positivity condition for X

The constant X occurs strictly positively in T in the following cases:

- X does not occur in T
- T converts to (X t1 … tn) and X does not occur in any of ti
- T converts to ∀ x:U,V and X does not occur in type U but occurs strictly positively in type V
- T converts to (I a1 … am t1 … tp) where I is the name of an inductive declaration of the form Ind(Γ)[m](I:A:=c1:∀ p1:P1,… ∀ pm:Pm,C1;…;cn:∀ p1:P1,… ∀ pm:Pm,Cn ) (in particular, it is not mutually defined and it has m parameters) and X does not occur in any of the ti, and the (instantiated) types of constructor Ci{pj/aj}j=1… m of I satisfy the nested positivity condition for X

The type of constructor T of I satisfies the nested positivity condition for a constant X in the following cases:

- T=(I b1… bm u1… up), I is an inductive definition with m parameters and X does not occur in any ui
- T=∀ x:U,V and X occurs only strictly positively in U and the type V satisfies the nested positivity condition for X

##### Example

X occurs strictly positively in A→ X or X*A or (list X) but not in X → A or (X → A)→ A nor (neg X) assuming the notion of product and lists were already defined and neg is an inductive definition with declaration Ind()[A:Set](neg:Set:=neg:(A→False) → neg ). Assuming X has arity nat → Prop and ex is the inductively defined existential quantifier, the occurrence of X in (ex nat λ n:nat. (X n)) is also strictly positive.

##### Correctness rules.

We shall now describe the rules allowing the introduction of a new inductive definition.

W-Ind
  Let E be an environment and Γ,ΓP,ΓI,ΓC are contexts such that ΓI is [I1:∀ ΓP,A1;…;Ik:∀ ΓP,Ak] and ΓC is [c1:∀ ΓP,C1;…;cn:∀ ΓP,Cn].

  (E[Γ;ΓP] ⊢ Aj : s′j)j=1… k (E[Γ;ΓI;ΓP] ⊢ Ci : sqi)i=1… n
  WF(E;Ind(Γ)[p](ΓI:=ΓC ))[Γ]

  provided that the following side conditions hold:
  - k>0 and all of Ij and ci are distinct names for j=1… k and i=1… n,
  - p is the number of parameters of Ind(Γ)(ΓI:=ΓC ) and ΓP is the context of parameters,
  - for j=1… k we have that Aj is an arity of sort sj and Ij ∉ Γ ∪ E,
  - for i=1… n we have that Ci is a type of constructor of Iqi which satisfies the positivity condition for I1 … Ik and ci ∉ Γ ∪ E.

One can remark that there is a constraint between the sort of the arity of the inductive type and the sort of the type of its constructors which will always be satisfied for the impredicative sort (Prop) but may fail to define inductive definition on sort Set and generate constraints between universes for inductive definitions in the Type hierarchy.

##### Examples.

It is well known that existential quantifier can be encoded as an inductive definition. The following declaration introduces the second-order existential quantifier ∃ X.P(X).

Coq < Inductive exProp (P:Prop->Prop) : Prop
Coq < := exP_intro : forall X:Prop, P X -> exProp P.

The same definition on Set is not allowed and fails :

Coq < Inductive exSet (P:Set->Prop) : Set
Coq < := exS_intro : forall X:Set, P X -> exSet P.
Coq < Coq < Error: Large non-propositional inductive types must be in Type.

It is possible to declare the same inductive definition in the universe Type. The exType inductive definition has type (Typei →Prop)→ Typej with the constraint that the parameter X of exT_intro has type Typek with k<j and k≤ i.

Coq < Inductive exType (P:Type->Prop) : Type
Coq < := exT_intro : forall X:Type, P X -> exType P.

##### Sort-polymorphism of inductive families.

From Coq version 8.1, inductive families declared in Type are polymorphic over their arguments in Type.

If A is an arity and s a sort, we write A/s for the arity obtained from A by replacing its sort with s. Especially, if A is well-typed in some environment and context, then A/s is typable by typability of all products in the Calculus of Inductive Constructions. The following typing rule is added to the theory.

Ind-Family
  Let Ind(Γ)[p](ΓI:=ΓC ) be an inductive definition. Let ΓP = [p1:P1;…;pp:Pp] be its context of parameters, ΓI = [I1:∀ ΓP,A1;…;Ik:∀ ΓP,Ak] its context of definitions and ΓC = [c1:∀ ΓP,C1;…;cn:∀ ΓP,Cn] its context of constructors, with ci a constructor of Iqi.

  Let m ≤ p be the length of the longest prefix of parameters such that the m first arguments of all occurrences of all Ij in all Ck (even the occurrences in the hypotheses of Ck) are exactly applied to p1 … pm (m is the number of recursively uniform parameters and the p−m remaining parameters are the recursively non-uniform parameters). Let q1, …, qr, with 0≤ r≤ m, be a (possibly) partial instantiation of the recursively uniform parameters of ΓP. We have:

  ⎧
  ⎪
  ⎨
  ⎪
  ⎩

  Ind(Γ)[p](ΓI:=ΓC ) ∈ E
  (E[Γ] ⊢ ql : P′l)l=1… r
  (E[Γ] ⊢ P′l ≤βδιζη Pl{pu/qu}u=1… l−1)l=1… r
  1 ≤ j ≤ k

  E[Γ] ⊢ (Ij q1 … qr:∀ [pr+1:Pr+1;…;pp:Pp], (Aj)/sj)

  provided that the following side conditions hold:

  - ΓP′ is the context obtained from ΓP by replacing each Pl that is an arity with P′l for 1≤ l ≤ r (notice that Pl arity implies P′l arity since E[Γ] ⊢ P′l ≤βδιζη Pl{pu/qu}u=1… l−1);
  - there are sorts si, for 1 ≤ i ≤ k such that, for ΓI′ = [I1:∀ ΓP′,(A1)/s1;…;Ik:∀ ΓP′,(Ak)/sk] we have (E[Γ;ΓI′;ΓP′] ⊢ Ci : sqi)i=1… n;
  - the sorts are such that all eliminations, to Prop, Set and Type(j), are allowed (see section 4.5.4).

Notice that if Ij q1 … qr is typable using the rules Ind-Const and App, then it is typable using the rule Ind-Family. Conversely, the extended theory is not stronger than the theory without Ind-Family. We get an equiconsistency result by mapping each Ind(Γ)[p](ΓI:=ΓC ) occurring into a given derivation into as many different inductive types and constructors as the number of different (partial) replacements of sorts, needed for this derivation, in the parameters that are arities (this is possible because Ind(Γ)[p](ΓI:=ΓC ) well-formed implies that Ind(Γ)[p](ΓI′:=ΓC′ ) is well-formed and has the same allowed eliminations, where ΓI′ is defined as above and ΓC′ = [c1:∀ ΓP′,C1;…;cn:∀ ΓP′,Cn]). That is, the changes in the types of each partial instance q1 … qr can be characterized by the ordered sets of arity sorts among the types of parameters, and to each signature is associated a new inductive definition with fresh names. Conversion is preserved as any (partial) instance Ij q1 … qr or Ci q1 … qr is mapped to the names chosen in the specific instance of Ind(Γ)[p](ΓI:=ΓC ).

In practice, the rule Ind-Family is used by Coq only when all the inductive types of the inductive definition are declared with an arity whose sort is in the Type hierarchy. Then, the polymorphism is over the parameters whose type is an arity of sort in the Type hierarchy. The sort sj are chosen canonically so that each sj is minimal with respect to the hierarchy Prop⊂Setp⊂Type where Setp is predicative Set. More precisely, an empty or small singleton inductive definition (i.e. an inductive definition of which all inductive types are singleton – see paragraph 4.5.4) is set in Prop, a small non-singleton inductive family is set in Set (even in case Set is impredicative – see Section 4.7), and otherwise in the Type hierarchy.

Note that the side-condition about allowed elimination sorts in the rule Ind-Family is just to avoid to recompute the allowed elimination sorts at each instance of a pattern-matching (see section 4.5.4).

As an example, let us consider the following definition:

Coq < Inductive option (A:Type) : Type :=
Coq < | None : option A
Coq < | Some : A -> option A.

As the definition is set in the Type hierarchy, it is used polymorphically over its parameters whose types are arities of a sort in the Type hierarchy. Here, the parameter A has this property, hence, if option is applied to a type in Set, the result is in Set. Note that if option is applied to a type in Prop, then, the result is not set in Prop but in Set still. This is because option is not a singleton type (see section 4.5.4) and it would loose the elimination to Set and Type if set in Prop.

Coq < Check (fun A:Set => option A).
fun A : Set => option A
: Set -> Set
Coq < Check (fun A:Prop => option A).
fun A : Prop => option A
: Prop -> Set

Here is another example.

Coq < Inductive prod (A B:Type) : Type := pair : A -> B -> prod A B.

As prod is a singleton type, it will be in Prop if applied twice to propositions, in Set if applied twice to at least one type in Set and none in Type, and in Type otherwise. In all cases, the three kind of eliminations schemes are allowed.

Coq < Check (fun A:Set => prod A).
fun A : Set => prod A
: Set -> Type -> Type
Coq < Check (fun A:Prop => prod A A).
fun A : Prop => prod A A
: Prop -> Prop
Coq < Check (fun (A:Prop) (B:Set) => prod A B).
fun (A : Prop) (B : Set) => prod A B
: Prop -> Set -> Set
Coq < Check (fun (A:Type) (B:Prop) => prod A B).
fun (A : Type) (B : Prop) => prod A B
: Type -> Prop -> Type

### 4.5.4 Destructors

The specification of inductive definitions with arities and constructors is quite natural. But we still have to say how to use an object in an inductive type.

This problem is rather delicate. There are actually several different ways to do that. Some of them are logically equivalent but not always equivalent from the computational point of view or from the user point of view.

From the computational point of view, we want to be able to define a function whose domain is an inductively defined type by using a combination of case analysis over the possible constructors of the object and recursion.

Because we need to keep a consistent theory and also we prefer to keep a strongly normalizing reduction, we cannot accept any sort of recursion (even terminating). So the basic idea is to restrict ourselves to primitive recursive functions and functionals.

For instance, assuming a parameter A:Set exists in the context, we want to build a function length of type List_A→ nat which computes the length of the list, so such that (length (nil A)) = O and (length (cons A a l)) = (S (length l)). We want these equalities to be recognized implicitly and taken into account in the conversion rule.

From the logical point of view, we have built a type family by giving a set of constructors. We want to capture the fact that we do not have any other way to build an object in this type. So when trying to prove a property (P m) for m in an inductive definition it is enough to enumerate all the cases where m starts with a different constructor.

In case the inductive definition is effectively a recursive one, we want to capture the extra property that we have built the smallest fixed point of this recursive equation. This says that we are only manipulating finite objects. This analysis provides induction principles.

For instance, in order to prove ∀ l:List_A,(Length_A l (length l)) it is enough to prove:

(Length_A (nil A) (length (nil A))) and

∀ a:A, ∀ l:List_A, (Length_A l (length l)) → (Length_A (cons A a l) (length (cons A a l))).

which given the conversion equalities satisfied by length is the same as proving: (Length_A (nil A) O) and ∀ a:A, ∀ l:List_A, (Length_A l (length l)) → (Length_A (cons A a l) (S (length l))).

One conceptually simple way to do that, following the basic scheme proposed by Martin-Löf in his Intuitionistic Type Theory, is to introduce for each inductive definition an elimination operator. At the logical level it is a proof of the usual induction principle and at the computational level it implements a generic operator for doing primitive recursion over the structure.

But this operator is rather tedious to implement and use. We choose in this version of Coq to factorize the operator for primitive recursion into two more primitive operations as was first suggested by Th. Coquand in [27]. One is the definition by pattern-matching. The second one is a definition by guarded fixpoints.

#### The match…with …end construction.

The basic idea of this destructor operation is that we have an object m in an inductive type I and we want to prove a property (P m) which in general depends on m. For this, it is enough to prove the property for m = (ci u1… upi) for each constructor of I.

The Coq term for this proof will be written :

match m with (c1 x11 ... x1p1) ⇒ f1 | … | (cn xn1...xnpn) ⇒ fn end

In this expression, if m is a term built from a constructor (ci u1… upi) then the expression will behave as it is specified with i-th branch and will reduce to fi where the xi1…xipi are replaced by the u1… up according to the ι-reduction.

Actually, for type-checking a match…with…end expression we also need to know the predicate P to be proved by case analysis. In the general case where I is an inductively defined n-ary relation, P is a n+1-ary relation: the n first arguments correspond to the arguments of I (parameters excluded), and the last one corresponds to object m. Coq can sometimes infer this predicate but sometimes not. The concrete syntax for describing this predicate uses the as…in…return construction. For instance, let us assume that I is an unary predicate with one parameter. The predicate is made explicit using the syntax :

match m as x in I `_` a return (P x) with (c1 x11 ... x1p1) ⇒ f1 | … | (cn xn1...xnpn) ⇒ fn end

The as part can be omitted if either the result type does not depend on m (non-dependent elimination) or m is a variable (in this case, the result type can depend on m). The in part can be omitted if the result type does not depend on the arguments of I. Note that the arguments of I corresponding to parameters must be `_`, because the result type is not generalized to all possible values of the parameters. The expression after in must be seen as an inductive type pattern. As a final remark, expansion of implicit arguments and notations apply to this pattern.

For the purpose of presenting the inference rules, we use a more compact notation :

case(m,(λ a x . P), λ x11 ... x1p1 . f1 | … | λ xn1...xnpn . fn)

##### Allowed elimination sorts.

An important question for building the typing rule for match is what can be the type of P with respect to the type of the inductive definitions.

We define now a relation [I:A|B] between an inductive definition I of type A and an arity B. This relation states that an object in the inductive definition I can be eliminated for proving a property P of type B.

The case of inductive definitions in sorts Set or Type is simple. There is no restriction on the sort of the predicate to be eliminated.

##### Notations.

The [I:A|B] is defined as the smallest relation satisfying the following rules: We write [I|B] for [I:A|B] where A is the type of I.

Prod

  [(I x):A′|B′]
  [I:∀ x:A, A′|∀ x:A, B′]

Set & Type

  s1 ∈ {Set,Type(j)}, s2 ∈ S
  [I:s1|I→ s2]

The case of Inductive definitions of sort Prop is a bit more complicated, because of our interpretation of this sort. The only harmless allowed elimination, is the one when predicate P is also of sort Prop.

Prop

  [I:Prop|I→Prop]

Prop is the type of logical propositions, the proofs of properties P in Prop could not be used for computation and are consequently ignored by the extraction mechanism. Assume A and B are two propositions, and the logical disjunction A∨ B is defined inductively by :

Coq < Inductive or (A B:Prop) : Prop :=
Coq < lintro : A -> or A B | rintro : B -> or A B.

The following definition which computes a boolean value by case over the proof of or A B is not accepted :

Coq < Definition choice (A B: Prop) (x:or A B) :=
Coq < match x with lintro a => true | rintro b => false end.
Coq < Coq < Error:
Incorrect elimination of "x" in the inductive type "or":
the return type has sort "Set" while it should be "Prop".
Elimination of an inductive object of sort Prop
is not allowed on a predicate in sort Set
because proofs can be eliminated only to build proofs.

From the computational point of view, the structure of the proof of (or A B) in this term is needed for computing the boolean value.

In general, if I has type Prop then P cannot have type I→ Set, because it will mean to build an informative proof of type (P m) doing a case analysis over a non-computational object that will disappear in the extracted program. But the other way is safe with respect to our interpretation we can have I a computational object and P a non-computational one, it just corresponds to proving a logical property of a computational object.

In the same spirit, elimination on P of type I→ Type cannot be allowed because it trivially implies the elimination on P of type I→ Set by cumulativity. It also implies that there is two proofs of the same property which are provably different, contradicting the proof-irrelevance property which is sometimes a useful axiom :

Coq < Axiom proof_irrelevance : forall (P : Prop) (x y : P), x=y.
proof_irrelevance is assumed

The elimination of an inductive definition of type Prop on a predicate P of type I→ Type leads to a paradox when applied to impredicative inductive definition like the second-order existential quantifier exProp defined above, because it give access to the two projections on this type.

##### Empty and singleton elimination

There are special inductive definitions in Prop for which more eliminations are allowed.

Prop-extended

  I is an empty or singleton definition s ∈ S
  [I:Prop|I→ s]

A singleton definition has only one constructor and all the arguments of this constructor have type Prop. In that case, there is a canonical way to interpret the informative extraction on an object in that type, such that the elimination on any sort s is legal. Typical examples are the conjunction of non-informative propositions and the equality. If there is an hypothesis h:a=b in the context, it can be used for rewriting not only in logical propositions but also in any type.

Coq < Print eq_rec.
eq_rec =
fun (A : Type) (x : A) (P : A -> Set) => eq_rect x P
: forall (A : Type) (x : A) (P : A -> Set),
P x -> forall y : A, x = y -> P y
Argument A is implicit
Argument scopes are [type_scope _ _ _ _ _]
Coq < Extraction eq_rec.
(** val eq_rec : ’a1 -> ’a2 -> ’a1 -> ’a2 **)
let eq_rec x f y =
f

An empty definition has no constructors, in that case also, elimination on any sort is allowed.

##### Type of branches.

Let c be a term of type C, we assume C is a type of constructor for an inductive definition I. Let P be a term that represents the property to be proved. We assume r is the number of parameters.

We define a new type {c:C}P which represents the type of the branch corresponding to the c:C constructor.

{c:(Ii p1… pr t1 … tp)}P | ≡ (P t1… tp c)
{c:∀ x:T,C}P | ≡ ∀ x:T,{(c x):C}P

We write {c}P for {c:C}P with C the type of c.

##### Examples.

For List_A the type of P will be List_A→ s for s ∈ S.
{(cons A)}P ≡ ∀ a:A, ∀ l:List_A,(P (cons A a l)).

For Length_A, the type of P will be ∀ l:List_A,∀ n:nat, (Length_A l n)→ Prop and the expression {(Lcons A)}P is defined as:
∀ a:A, ∀ l:List_A, ∀ n:nat, ∀ h:(Length_A l n), (P (cons A a l) (S n) (Lcons A a l n l)).
If P does not depend on its third argument, we find the more natural expression:
∀ a:A, ∀ l:List_A, ∀ n:nat, (Length_A l n)→(P (cons A a l) (S n)).

##### Typing rule.

Our very general destructor for inductive definition enjoys the following typing rule

match

  E[Γ] ⊢ c : (I q1… qr t1… ts) E[Γ] ⊢ P : B [(I q1… qr)|B] (E[Γ] ⊢ fi : {(cpi q1… qr)}P)i=1… l
  E[Γ] ⊢ case(c,P,f1|… |fl) : (P t1… ts c)

  provided I is an inductive type in a declaration Ind(Δ)[r](ΓI:=ΓC ) with ΓC = [c1:C1;…;cn:Cn] and cp1… cpl are the only constructors of I.

##### Example.

For List and Length the typing rules for the match expression are (writing just t:M instead of E[Γ] ⊢ t : M, the environment and context being the same in all the judgments).

l:List_A P:List_A→ s f1:(P (nil A)) f2:∀ a:A, ∀ l:List_A, (P (cons A a l))
case(l,P,f1 | f2):(P l)

H:(Length_A L N)
P:∀ l:List_A, ∀ n:nat, (Length_A l n)→ Prop
f1:(P (nil A) O Lnil)
f2:∀ a:A, ∀ l:List_A, ∀ n:nat, ∀ h:(Length_A l n), (P (cons A a n) (S n) (Lcons A a l n h))

case(H,P,f1 | f2):(P L N H)

##### Definition of ι-reduction.

We still have to define the ι-reduction in the general case.

A ι-redex is a term of the following form:

case((cpi q1… qr a1… am),P,f1|… | fl)

with cpi the i-th constructor of the inductive type I with r parameters.

The ι-contraction of this term is (fi a1… am) leading to the general reduction rule:

case((cpi q1… qr a1… am),P,f1|… | fn) ▷ι (fi a1… am)

### 4.5.5 Fixpoint definitions

The second operator for elimination is fixpoint definition. This fixpoint may involve several mutually recursive definitions. The basic concrete syntax for a recursive set of mutually recursive declarations is (with Γi contexts) :

fix f1 (Γ1) :A1:=t1 with … with fn (Γn) :An:=tn

The terms are obtained by projections from this set of declarations and are written

fix f1 (Γ1) :A1:=t1 with … with fn (Γn) :An:=tn for fi

In the inference rules, we represent such a term by

Fix fi{f1:A1′:=t1′ … fn:An′:=tn′}

with ti′ (resp. Ai′) representing the term ti abstracted (resp. generalized) with respect to the bindings in the context Γi, namely ti′=λ Γi . ti and Ai′=∀ Γi, Ai.

#### Typing rule

The typing rule is the expected one for a fixpoint.

Fix

  (E[Γ] ⊢ Ai : si)i=1… n (E[Γ,f1:A1,…,fn:An] ⊢ ti : Ai)i=1… n
  E[Γ] ⊢ Fix fi{f1:A1:=t1 … fn:An:=tn} : Ai

Any fixpoint definition cannot be accepted because non-normalizing terms will lead to proofs of absurdity.

The basic scheme of recursion that should be allowed is the one needed for defining primitive recursive functionals. In that case the fixpoint enjoys a special syntactic restriction, namely one of the arguments belongs to an inductive type, the function starts with a case analysis and recursive calls are done on variables coming from patterns and representing subterms.

For instance in the case of natural numbers, a proof of the induction principle of type

∀ P:nat→Prop, (P O)→(∀ n:nat, (P n)→(P (S n)))→ ∀ n:nat, (P n)

can be represented by the term:

λ P:nat→Prop. λ f:(P O). λ g:(∀ n:nat, (P n)→(P (S n))) .
Fix h{h:∀ n:nat, (P n):=λ n:nat. case(n,P,f | λ p:nat. (g p (h p)))}

Before accepting a fixpoint definition as being correctly typed, we check that the definition is “guarded”. A precise analysis of this notion can be found in [65].

The first stage is to precise on which argument the fixpoint will be decreasing. The type of this argument should be an inductive definition.

For doing this the syntax of fixpoints is extended and becomes

Fix fi{f1/k1:A1:=t1 … fn/kn:An:=tn}

where ki are positive integers. Each Ai should be a type (reducible to a term) starting with at least ki products ∀ y1:B1,… ∀ yki:Bki, A′i and Bki being an instance of an inductive definition.

Now in the definition ti, if fj occurs then it should be applied to at least kj arguments and the kj-th argument should be syntactically recognized as structurally smaller than yki

The definition of being structurally smaller is a bit technical. One needs first to define the notion of recursive arguments of a constructor. For an inductive definition Ind(Γ)[r](ΓI:=ΓC ), the type of a constructor c has the form ∀ p1:P1,… ∀ pr:Pr, ∀ x1:T1, … ∀ xr:Tr, (Ij p1… pr t1… ts) the recursive arguments will correspond to Ti in which one of the Il occurs.

The main rules for being structurally smaller are the following:
Given a variable y of type an inductive definition in a declaration Ind(Γ)[r](ΓI:=ΓC ) where ΓI is [I1:A1;…;Ik:Ak], and ΓC is [c1:C1;…;cn:Cn]. The terms structurally smaller than y are:

- (t u), λ x:u . t when t is structurally smaller than y .
- case(c,P,f1… fn) when each fi is structurally smaller than y.
  If c is y or is structurally smaller than y, its type is an inductive definition Ip part of the inductive declaration corresponding to y. Each fi corresponds to a type of constructor Cq ≡ ∀ p1:P1,…,∀ pr:Pr, ∀ y1:B1, … ∀ yk:Bk, (I a1… ak) and can consequently be written λ y1:B′1. … λ yk:B′k. gi. (B′i is obtained from Bi by substituting parameters variables) the variables yj occurring in gi corresponding to recursive arguments Bi (the ones in which one of the Il occurs) are structurally smaller than y.

The following definitions are correct, we enter them using the Fixpoint command as described in Section 1.3.4 and show the internal representation.

Coq < Fixpoint plus (n m:nat) {struct n} : nat :=
Coq < match n with
Coq < | O => m
Coq < | S p => S (plus p m)
Coq < end.
plus is recursively defined (decreasing on 1st argument)
Coq < Print plus.
plus =
fix plus (n m : nat) {struct n} : nat :=
match n with
| O => m
| S p => S (plus p m)
end
: nat -> nat -> nat
Coq < Fixpoint lgth (A:Set) (l:list A) {struct l} : nat :=
Coq < match l with
Coq < | nil => O
Coq < | cons a l’ => S (lgth A l’)
Coq < end.
lgth is recursively defined (decreasing on 2nd argument)
Coq < Print lgth.
lgth =
fix lgth (A : Set) (l : list A) {struct l} : nat :=
match l with
| nil => O
| cons _ l’ => S (lgth A l’)
end
: forall A : Set, list A -> nat
Argument scopes are [type_scope _]
Coq < Fixpoint sizet (t:tree) : nat := let (f) := t in S (sizef f)
Coq < with sizef (f:forest) : nat :=
Coq < match f with
Coq < | emptyf => O
Coq < | consf t f => plus (sizet t) (sizef f)
Coq < end.
sizet, sizef are recursively defined (decreasing respectively on 1st,
1st arguments)
Coq < Print sizet.
sizet =
fix sizet (t : tree) : nat :=
let (f) := t in S (sizef f)
with sizef (f : forest) : nat :=
match f with
| emptyf => O
| consf t f0 => plus (sizet t) (sizef f0)
end
for sizet
: tree -> nat

#### Reduction rule

Let F be the set of declarations: f1/k1:A1:=t1 … fn/kn:An:=tn. The reduction for fixpoints is:

(Fix fi{F} a1… aki) ▷ι ti{(fk/Fix fk{F})k=1… n} a1… aki

when aki starts with a constructor. This last restriction is needed in order to keep strong normalization and corresponds to the reduction for primitive recursive operators.

We can illustrate this behavior on examples.

Coq < Goal forall n m:nat, plus (S n) m = S (plus n m).
1 subgoal
============================
forall n m : nat, plus (S n) m = S (plus n m)
Coq < reflexivity.
No more subgoals.
Coq < Abort.
Current goal aborted
Coq < Goal forall f:forest, sizet (node f) = S (sizef f).
1 subgoal
============================
forall f : forest, sizet (node f) = S (sizef f)
Coq < reflexivity.
No more subgoals.
Coq < Abort.
Current goal aborted

But assuming the definition of a son function from tree to forest:

Coq < Definition sont (t:tree) : forest
Coq < := let (f) := t in f.
sont is defined

The following is not a conversion but can be proved after a case analysis.

Coq < Goal forall t:tree, sizet t = S (sizef (sont t)).
Coq < Coq < 1 subgoal
============================
forall t : tree, sizet t = S (sizef (sont t))
Coq < reflexivity. (** this one fails **)
Toplevel input, characters 0-11:
> reflexivity.
> ^^^^^^^^^^^
Error: Impossible to unify "S (sizef (sont t))" with "sizet t".
Coq < destruct t.
1 subgoal
f : forest
============================
sizet (node f) = S (sizef (sont (node f)))
Coq < reflexivity.
No more subgoals.

#### Mutual induction

The principles of mutual induction can be automatically generated using the Scheme command described in Section 13.1.

## 4.6 Co-inductive types

The implementation contains also co-inductive definitions, which are types inhabited by infinite objects. More information on co-inductive definitions can be found in [66, 67, 68].

## 4.7 Cic: the Calculus of Inductive Construction with impredicative Set

Coq can be used as a type-checker for Cic, the original Calculus of Inductive Constructions with an impredicative sort Set by using the compiler option -impredicative-set.

For example, using the ordinary coqtop command, the following is rejected.

Coq < Definition id: Set := forall X:Set,X->X.
Coq < Coq < Coq < Coq < Toplevel input, characters 185-202:
> Definition id: Set := forall X:Set,X->X.
> ^^^^^^^^^^^^^^^^^
Error: The term "forall X : Set, X -> X" has type "Type"
while it is expected to have type "Set".

while it will type-check, if one use instead the coqtop -impredicative-set command.

The major change in the theory concerns the rule for product formation in the sort Set, which is extended to a domain in any sort :

Prod

  E[Γ] ⊢ T : s s ∈ S E[Γ::(x:T)] ⊢ U : Set
  E[Γ] ⊢ ∀ x:T,U : Set

This extension has consequences on the inductive definitions which are allowed. In the impredicative system, one can build so-called large inductive definitions like the example of second-order existential quantifier (exSet).

There should be restrictions on the eliminations which can be performed on such definitions. The eliminations rules in the impredicative system for sort Set become :

Set

  s ∈ {Prop, Set}
  [I:Set|I→ s]

  I is a small inductive definition s ∈ {Type(i)}
  [I:Set|I→ s]

1
  In the Reference Manual of versions of Coq prior to 8.4, the level of Type typing Prop and Set was numbered 0. From Coq 8.4, it started to be numbered 1 so as to be able to leave room for re-interpreting Set in the hierarchy as Type(0). This change also put the reference manual in accordance with the internal conventions adopted in the implementation.
2
  This requirement could be relaxed if we instead introduced an explicit mechanism for instantiating constants. At the external level, the Coq engine works accordingly to this view that all the definitions in the environment were built in a sub-context of the current context.