# Calculus of Inductive Constructions¶

Coq の基礎となる形式言語は *Calculus of Inductive Constructions* (Cic) と呼ばれる。この章では、 Cic の推論規則について説明する。The history of this formalism as well as pointers to related work are provided in a separate chapter; see *Credits*.

## The terms¶

The expressions of the Cic are *terms* and all terms 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, universal quantification is relative
to a type and takes the form "*for all x of type T, P* ". The expression
“x of type T” is written `x:T`

. Informally, `x:T`

can be thought as
“x belongs to T”.

The types of types are *sorts*. Types and sorts are themselves terms
so that terms, types and sorts are all components of a common
syntactic language of terms which is described in Section Terms but,
first, we describe sorts.

### Sorts¶

All sorts have a type and 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 [Coq86], the language of Cic 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 \(\Sort\) the set of sorts which is defined by:

Their properties, such as: \(\Prop:\Type(1)\), \(\Set:\Type(1)\), and \(\Type(i):\Type(i+1)\), are defined in Section Subtyping rules.

The user does not have to 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 indices.

**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(u 1 ,...,u n )\)), or the base universe (the expression
\(0\)) which corresponds, in the arity of template polymorphic inductive
types (see Section
Well-formed inductive definitions),
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.

参考

Section ユニバースを表示する.

### Terms¶

Terms are built from sorts, variables, constants, abstractions,
applications, local definitions, and products. From a syntactic point
of view, types cannot be distinguished from terms, except that they
cannot start by an abstraction or a constructor. More precisely the
language of the *Calculus of Inductive Constructions* is built from
the following rules.

the sorts \(\Set\), \(\Prop\), \(\Type(i)\) are terms.

variables, hereafter ranged over by letters \(x\), \(y\), etc., are terms

constants, hereafter ranged over by letters \(c\), \(d\), etc., are terms.

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\) does not occur in \(U\) then \(∀ x:T,U\) reads as “if \(T\) then \(U\)”. A*non dependent product*can be written: \(T \rightarrow U\).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 [Bar81]. The term \(λ x:T . u\) is a function which maps elements of \(T\) to the expression \(u\).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”.if

`x`

is a variable, and \(t\), \(T\) and \(u\) are terms then`let x:=t:T in u`

is a term which denotes the term \(u\) where the variable \(x\) is locally bound to \(t\) of type \(T\). This stands for the common “let-in” construction of functional programs such as ML or Scheme.

**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.

**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
\(\subst{u}{x}{t}\).

**The logical vs programming readings.**
The constructions of the Cic can be used to express both logical and
programming notions, accordingly to the Curry-Howard correspondence
between proofs and programs, and between propositions and types
[CFC58][How80][dB72].

For instance, let us assume that \(\nat\) is the type of natural numbers
with zero element written \(0\) and that `True`

is the always true
proposition. Then \(→\) is used both to denote \(\nat→\nat\) which is the type
of functions from \(\nat\) to \(\nat\), to denote True→True which is an
implicative proposition, to denote \(\nat →\Prop\) which is the type of
unary predicates over the natural numbers, etc.

Let us assume that `mult`

is a function of type \(\nat→\nat→\nat\) and `eqnat`

a
predicate of type nat→nat→ Prop. The λ-abstraction can serve to build
“ordinary” functions as in \(λ x:\nat.(\kw{mult}~x~x)\) (i.e.
`fun x:nat => mult x x`

in Coq notation) but may build also predicates over the natural
numbers. For instance \(λ x:\nat.(\kw{eqnat}~x~0)\)
(i.e. `fun x:nat => eqnat x 0`

in Coq notation) will represent the predicate of one variable \(x\) which
asserts the equality of \(x\) with \(0\). This predicate has type
\(\nat → \Prop\)
and it can be applied to any expression of type \(\nat\), say \(t\), to give an
object \(P~t\) of type \(\Prop\), namely 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 the type of proofs of the formula “\(∀ x. P(x\))”.

## Typing rules¶

As objects of type theory, terms are subjected to *type discipline*.
The well typing of a term depends on a global environment and a local
context.

**Local context.**
A *local context* is an ordered list of *local declarations* of names
which we call *variables*. The declaration of some variable \(x\) is
either a *local assumption*, written \(x:T\) (\(T\) is a type) or a *local
definition*, written \(x:=t:T\). We use brackets to write local contexts.
A typical example is \([x:T;y:=u:U;z:V]\). Notice that the variables
declared in a local context must be distinct. If \(Γ\) is a local context
that 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) ∈ Γ\).
For the rest of the chapter, \(Γ::(y:T)\) denotes the local context \(Γ\)
enriched with the local assumption \(y:T\). Similarly, \(Γ::(y:=t:T)\) denotes
the local context \(Γ\) enriched with the local definition \((y:=t:T)\). The
notation \([]\) denotes the empty local context. By \(Γ_1 ; Γ_2\) we mean
concatenation of the local context \(Γ_1\) and the local context \(Γ_2\) .

**Global environment.**
A *global environment* is an ordered list of *global declarations*.
Global declarations are either *global assumptions* or *global
definitions*, but also declarations of inductive objects. Inductive
objects themselves declare both inductive or coinductive types and
constructors (see Section Inductive Definitions).

A *global assumption* will be represented in the global environment as
\((c:T)\) which assumes the name \(c\) to be of some type \(T\). A *global
definition* will be represented in the global environment as \(c:=t:T\)
which defines the name \(c\) to have value \(t\) and type \(T\). We shall call
such names *constants*. For the rest of the chapter, the \(E;c:T\) denotes
the global environment \(E\) enriched with the global assumption \(c:T\).
Similarly, \(E;c:=t:T\) denotes the global environment \(E\) enriched with the
global definition \((c:=t:T)\).

The rules for inductive definitions (see Section Inductive Definitions) 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 define simultaneously two judgments. The first
one \(\WTEG{t}{T}\) means the term \(t\) is well-typed and has type \(T\) in the
global environment \(E\) and local context \(Γ\). The second judgment \(\WFE{Γ}\)
means that the global environment \(E\) is well-formed and the local
context \(Γ\) is a valid local context in this global environment.

A term \(t\) is well typed in a global environment \(E\) iff there exists a local context \(\Gamma\) and a term \(T\) such that the judgment \(\WTEG{t}{T}\) can be derived from the following rules.

- W-Empty
- \[\frac{% % }{% \WF{[]}{}% }\]

- W-Local-Assum
- \[\frac{% \WTEG{T}{s}% \hspace{3em}% s \in \Sort% \hspace{3em}% x \not\in \Gamma % \cup E% }{% \WFE{\Gamma::(x:T)}% }\]

- W-Local-Def
- \[\frac{% \WTEG{t}{T}% \hspace{3em}% x \not\in \Gamma % \cup E% }{% \WFE{\Gamma::(x:=t:T)}% }\]

- W-Global-Assum
- \[\frac{% \WTE{}{T}{s}% \hspace{3em}% s \in \Sort% \hspace{3em}% c \notin E% }{% \WF{E;c:T}{}% }\]

- W-Global-Def
- \[\frac{% \WTE{}{t}{T}% \hspace{3em}% c \notin E% }{% \WF{E;c:=t:T}{}% }\]

- Ax-Prop
- \[\frac{% \WFE{\Gamma}% }{% \WTEG{\Prop}{\Type(1)}% }\]

- Ax-Set
- \[\frac{% \WFE{\Gamma}% }{% \WTEG{\Set}{\Type(1)}% }\]

- Ax-Type
- \[\frac{% \WFE{\Gamma}% }{% \WTEG{\Type(i)}{\Type(i+1)}% }\]

- Var
- \[\frac{% \WFE{\Gamma}% \hspace{3em}% (x:T) \in \Gamma~~\mbox{or}~~(x:=t:T) \in \Gamma~\mbox{for some $t$}% }{% \WTEG{x}{T}% }\]

- Const
- \[\frac{% \WFE{\Gamma}% \hspace{3em}% (c:T) \in E~~\mbox{or}~~(c:=t:T) \in E~\mbox{for some $t$}% }{% \WTEG{c}{T}% }\]

- Prod-Prop
- \[\frac{% \WTEG{T}{s}% \hspace{3em}% s \in {\Sort}% \hspace{3em}% \WTE{\Gamma::(x:T)}{U}{\Prop}% }{% \WTEG{\forall~x:T,U}{\Prop}% }\]

- Prod-Set
- \[\frac{% \WTEG{T}{s}% \hspace{3em}% s \in \{\Prop, \Set\}% \hspace{3em}% \WTE{\Gamma::(x:T)}{U}{\Set}% }{% \WTEG{\forall~x:T,U}{\Set}% }\]

- Prod-Type
- \[\frac{% \WTEG{T}{\Type(i)}% \hspace{3em}% \WTE{\Gamma::(x:T)}{U}{\Type(i)}% }{% \WTEG{\forall~x:T,U}{\Type(i)}% }\]

- Lam
- \[\frac{% \WTEG{\forall~x:T,U}{s}% \hspace{3em}% \WTE{\Gamma::(x:T)}{t}{U}% }{% \WTEG{\lb x:T\mto t}{\forall x:T, U}% }\]

- App
- \[\frac{% \WTEG{t}{\forall~x:U,T}% \hspace{3em}% \WTEG{u}{U}% }{% \WTEG{(t\ u)}{\subst{T}{x}{u}}% }\]

- Let
- \[\frac{% \WTEG{t}{T}% \hspace{3em}% \WTE{\Gamma::(x:=t:T)}{u}{U}% }{% \WTEG{\letin{x}{t:T}{u}}{\subst{U}{x}{t}}% }\]

注釈

**Prod-Prop** and **Prod-Set** typing-rules make sense if we consider the
semantic difference between \(\Prop\) and \(\Set\):

All values of a type that has a sort \(\Set\) are extractable.

No values of a type that has a sort \(\Prop\) are extractable.

注釈

We may have \(\letin{x}{t:T}{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 Conversion rules).

## Conversion rules¶

In Cic, there is an internal reduction mechanism. In particular, it
can decide if two programs are *intentionally* equal (one says
*convertible*). Convertibility is described in this section.

### β-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 global environment \(E\) and local 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 \(β\):

We say that \(\subst{t}{x}{u}\) is the *β-contraction* of
\(((λx:T. t) u)\) and, conversely, that \(((λ x:T. t) u)\) is the
*β-expansion* of \(\subst{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 [Coq85].

### ι-reduction¶

A specific conversion rule is associated to the inductive objects in the global environment. We shall give later on (see Section Well-formed inductive definitions) 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 [PM93][Wer94].

### δ-reduction¶

We may have variables defined in local contexts or constants defined 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.

- Delta-Local
- \[\frac{% \WFE{\Gamma}% \hspace{3em}% (x:=t:T) ∈ Γ% }{% E[Γ] ⊢ x~\triangleright_Δ~t% }\]

- Delta-Global
- \[\frac{% \WFE{\Gamma}% \hspace{3em}% (c:=t:T) ∈ E% }{% E[Γ] ⊢ c~\triangleright_δ~t% }\]

### ζ-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.

- Zeta
- \[\frac{% \WFE{\Gamma}% \hspace{3em}% \WTEG{u}{U}% \hspace{3em}% \WTE{\Gamma::(x:=u:U)}{t}{T}% }{% E[Γ] ⊢ \letin{x}{u}{t}~\triangleright_ζ~\subst{t}{x}{u}% }\]

### η-expansion¶

Another important concept is η-expansion. It is legal to identify any term \(t\) of functional type \(∀ x:T, U\) with its so-called η-expansion

for \(x\) an arbitrary variable name fresh in \(t\).

注釈

We deliberately do not define η-reduction:

This is because, in general, the type of \(t\) need not to be convertible to the type of \(λ x:T. (t~x)\). E.g., if we take \(f\) such that:

then

We could not allow

because the type of the reduced term \(∀ x:\Type(2),\Type(1)\) would not be convertible to the type of the original term \(∀ x:\Type(1),\Type(1).\)

### Convertibility¶

Let us write \(E[Γ] ⊢ t \triangleright u\) for the contextual closure of the relation \(t\) reduces to \(u\) in the global environment \(E\) and local context \(Γ\) with one of the previous reductions β, ι, δ or ζ.

We say that two terms \(t_1\) and \(t_2\) are
*βιδζη-convertible*, or simply *convertible*, or *equivalent*, in the
global environment \(E\) and local context \(Γ\) iff there
exist terms \(u_1\) and \(u_2\) such that \(E[Γ] ⊢ t_1 \triangleright
… \triangleright u_1\) and \(E[Γ] ⊢ t_2 \triangleright … \triangleright u_2\) and either \(u_1\) and
\(u_2\) are identical, or they are convertible up to η-expansion,
i.e. \(u_1\) is \(λ x:T. u_1'\) and \(u_2 x\) is
recursively convertible to \(u_1'\) , or, symmetrically,
\(u_2\) is \(λx:T. u_2'\)
and \(u_1 x\) is recursively convertible to u_2′ . We then write
\(E[Γ] ⊢ t_1 =_{βδιζη} t_2\) .

Apart from this we consider two instances of polymorphic and cumulative (see Chapter Polymorphic Universes) inductive types (see below) convertible

if we have subtypings (see below) in both directions, i.e.,

and

Furthermore, we consider

convertible if

and we have that \(c\) and \(c'\) are the same constructors of different instances of the same inductive types (differing only in universe levels) such that

and

and we have

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

## Subtyping rules¶

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 Cic).
This property extends the equivalence relation of convertibility into
a *subtyping* relation inductively defined by:

if \(E[Γ] ⊢ t =_{βδιζη} u\) then \(E[Γ] ⊢ t ≤_{βδιζη} u\),

if \(i ≤ j\) then \(E[Γ] ⊢ \Type(i) ≤_{βδιζη} \Type(j)\),

for any \(i\), \(E[Γ] ⊢ \Set ≤_{βδιζη} \Type(i)\),

\(E[Γ] ⊢ \Prop ≤_{βδιζη} \Set\), hence, by transitivity, \(E[Γ] ⊢ \Prop ≤_{βδιζη} \Type(i)\), for any \(i\)

if \(E[Γ] ⊢ T =_{βδιζη} U\) and \(E[Γ::(x:T)] ⊢ T' ≤_{βδιζη} U'\) then \(E[Γ] ⊢ ∀x:T, T′ ≤_{βδιζη} ∀ x:U, U′\).

if \(\ind{p}{Γ_I}{Γ_C}\) is a universe polymorphic and cumulative (see Chapter Polymorphic Universes) inductive type (see below) and \((t : ∀Γ_P ,∀Γ_{\mathit{Arr}(t)}, \Sort)∈Γ_I\) and \((t' : ∀Γ_P' ,∀Γ_{\mathit{Arr}(t)}', \Sort')∈Γ_I\) are two different instances of

*the same*inductive type (differing only in universe levels) with constructors\[[c_1 : ∀Γ_P ,∀ T_{1,1} … T_{1,n_1} , t~v_{1,1} … v_{1,m} ;…; c_k : ∀Γ_P ,∀ T_{k,1} … T_{k,n_k} ,t~v_{n,1} … v_{n,m} ]\]and

\[[c_1 : ∀Γ_P' ,∀ T_{1,1}' … T_{1,n_1}' , t'~v_{1,1}' … v_{1,m}' ;…; c_k : ∀Γ_P' ,∀ T_{k,1}' … T_{k,n_k}' ,t'~v_{n,1}' … v_{n,m}' ]\]respectively then

\[E[Γ] ⊢ t~w_1 … w_m ≤_{βδιζη} t'~w_1' … w_m'\](notice that \(t\) and \(t'\) are both fully applied, i.e., they have a sort as a type) if

\[E[Γ] ⊢ w_i =_{βδιζη} w_i'\]for \(1 ≤ i ≤ m\) and we have

\[E[Γ] ⊢ T_{i,j} ≤_{βδιζη} T_{i,j}'\]and

\[E[Γ] ⊢ A_i ≤_{βδιζη} A_i'\]where \(Γ_{\mathit{Arr}(t)} = [a_1 : A_1 ; … ; a_l : A_l ]\) and \(Γ_{\mathit{Arr}(t)}' = [a_1 : A_1'; … ; a_l : A_l']\).

The conversion rule up to subtyping is now exactly:

- Conv
- \[\frac{% E[Γ] ⊢ U : s% \hspace{3em}% E[Γ] ⊢ t : T% \hspace{3em}% 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
rules. Among them, we have to mention the *head reduction* which will
play an important role (see Chapter Tactics). Any term \(t\) can be written as
\(λ x_1 :T_1 . … λ x_k :T_k . (t_0~t_1 … t_n )\) where \(t_0\) is not an
application. We say then that \(t~0\) is the *head of* \(t\). If we assume
that \(t_0\) is \(λ x:T. u_0\) then one step of β-head reduction of \(t\) is:

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\):

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 \(u_i\) can be reducible. Similar notions of head-normal forms involving δ, ι and ζ reductions or any combination of those can also be defined.

## Inductive Definitions¶

Formally, we can represent any *inductive definition* as
\(\ind{p}{Γ_I}{Γ_C}\) where:

\(Γ_I\) determines the names and types of inductive types;

\(Γ_C\) determines the names and types of constructors of these inductive types;

\(p\) determines the number of parameters of these inductive types.

These inductive definitions, together with global assumptions and
global definitions, then form the global environment. Additionally,
for any \(p\) there always exists \(Γ_P =[a_1 :A_1 ;…;a_p :A_p ]\) such that
each \(T\) in \((t:T)∈Γ_I \cup Γ_C\) can be written as: \(∀Γ_P , T'\) where \(Γ_P\) is
called the *context of parameters*. Furthermore, we must have that
each \(T\) in \((t:T)∈Γ_I\) can be written as: \(∀Γ_P,∀Γ_{\mathit{Arr}(t)}, S\) where
\(Γ_{\mathit{Arr}(t)}\) is called the *Arity* of the inductive type t and \(S\) is called
the sort of the inductive type t (not to be confused with \(\Sort\) which is the set of sorts).

Example

The declaration for parameterized lists is:

which corresponds to the result of the Coq declaration:

- Inductive list (A:Set) : Set := | nil : list A | cons : A -> list A -> list A.
- list is defined list_rect is defined list_ind is defined list_rec is defined

Example

The declaration for a mutual inductive definition of tree and forest is:

which corresponds to the result of the Coq declaration:

- Inductive tree : Set := | node : forest -> tree with forest : Set := | emptyf : forest | consf : tree -> forest -> forest.
- tree, forest are defined tree_rect is defined tree_ind is defined tree_rec is defined forest_rect is defined forest_ind is defined forest_rec is defined

Example

The declaration for a mutual inductive definition of even and odd is:

which corresponds to the result of the Coq declaration:

- Inductive even : nat -> Prop := | even_O : even 0 | even_S : forall n, odd n -> even (S n) with odd : nat -> prop := | odd_S : forall n, even n -> odd (S n).
- Toplevel input, characters 109-113: > Inductive even : nat -> Prop := | even_O : even 0 | even_S : forall n, odd n -> even (S n) with odd : nat -> prop := | odd_S : forall n, even n -> odd (S n). > ^^^^ Error: The reference prop was not found in the current environment.

### Types of inductive objects¶

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

- Ind
- \[\frac{% \WFE{Γ}% \hspace{3em}% \ind{p}{Γ_I}{Γ_C} ∈ E% \hspace{3em}% (a:A)∈Γ_I% }{% E[Γ] ⊢ a : A% }\]

- Constr
- \[\frac{% \WFE{Γ}% \hspace{3em}% \ind{p}{Γ_I}{Γ_C} ∈ E% \hspace{3em}% (c:C)∈Γ_C% }{% E[Γ] ⊢ c : C% }\]

Example

Provided that our environment \(E\) contains inductive definitions we showed before, these two inference rules above enable us to conclude that:

### 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:

#### Arity of a given sort¶

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\).

Example

\(A→\Set\) is an arity of sort \(\Set\). \(∀ A:\Prop,A→ \Prop\) is an arity of sort \(\Prop\).

#### Arity¶

A type \(T\) is an *arity* if there is a \(s∈ \Sort\) such that \(T\) is an arity of
sort \(s\).

Example

\(A→ Set\) and \(∀ A:\Prop,A→ \Prop\) are arities.

#### Type constructor¶

We say that T is a *type of constructor of I* in one of the following
two cases:

\(T\) is \((I~t_1 … t_n )\)

\(T\) is \(∀ x:U,T'\) where \(T'\) is also a type of constructor of \(I\)

Example

\(\nat\) and \(\nat→\nat\) are types of constructor of \(\nat\). \(∀ A:Type,\List~A\) and \(∀ A:Type,A→\List~A→\List~A\) are types of constructor of \(\List\).

#### Positivity Condition¶

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

\(T=(X~t_1 … t_n )\) and \(X\) does not occur free in any \(t_i\)

\(T=∀ x:U,V\) and \(X\) occurs only strictly positively in \(U\) and the type \(V\) satisfies the positivity condition for \(X\).

#### Strict positivity¶

The constant \(X\) *occurs strictly positively* in \(T\) in the following
cases:

\(X\) does not occur in \(T\)

\(T\) converts to \((X~t_1 … t_n )\) and \(X\) does not occur in any of \(t_i\)

\(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~a_1 … a_m~t_1 … t_p )\) where \(I\) is the name of an inductive declaration of the form

\[\ind{m}{I:A}{c_1 :∀ p_1 :P_1 ,… ∀p_m :P_m ,C_1 ;…;c_n :∀ p_1 :P_1 ,… ∀p_m :P_m ,C_n}\](in particular, it is not mutually defined and it has \(m\) parameters) and \(X\) does not occur in any of the \(t_i\), and the (instantiated) types of constructor \(\subst{C_i}{p_j}{a_j}_{j=1… m}\) of \(I\) satisfy the nested positivity condition for \(X\)

#### Nested Positivity¶

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

\(T=(I~b_1 … b_m~u_1 … u_p)\), \(I\) is an inductive definition with \(m\) parameters and \(X\) does not occur in any \(u_i\)

\(T=∀ x:U,V\) and \(X\) occurs only strictly positively in \(U\) and the type \(V\) satisfies the nested positivity condition for \(X\)

Example

For instance, if one considers the following variant of a tree type branching over the natural numbers:

- Inductive nattree (A:Type) : Type := | leaf : nattree A | node : A -> (nat -> nattree A) -> nattree A.
- Toplevel input, characters 0-102: > Inductive nattree (A:Type) : Type := | leaf : nattree A | node : A -> (nat -> nattree A) -> nattree A. > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Error: node already exists.
- End TreeExample.
- Toplevel input, characters 0-16: > End TreeExample. > ^^^^^^^^^^^^^^^^ Error: There is nothing to end.

Then every instantiated constructor of `nattree A`

satisfies the nested positivity
condition for `nattree`

:

Type

`nattree A`

of constructor`leaf`

satisfies the positivity condition for`nattree`

because`nattree`

does not appear in any (real) arguments of the type of that constructor (primarily because`nattree`

does not have any (real) arguments) ... (bullet 1)Type

`A → (nat → nattree A) → nattree A`

of constructor`node`

satisfies the positivity condition for`nattree`

because:`nattree`

occurs only strictly positively in`A`

... (bullet 3)`nattree`

occurs only strictly positively in`nat → nattree A`

... (bullet 3 + 2)`nattree`

satisfies the positivity condition for`nattree A`

... (bullet 1)

#### Correctness rules¶

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

Let \(E\) be a global environment and \(Γ_P\), \(Γ_I\), \(Γ_C\) be contexts such that \(Γ_I\) is \([I_1 :∀ Γ_P ,A_1 ;…;I_k :∀ Γ_P ,A_k]\), and \(Γ_C\) is \([c_1:∀ Γ_P ,C_1 ;…;c_n :∀ Γ_P ,C_n ]\). Then

- W-Ind
- \[\frac{% \WFE{Γ_P}% \hspace{3em}% (E[Γ_P ] ⊢ A_j : s_j )_{j=1… k}% \hspace{3em}% (E[Γ_I ;Γ_P ] ⊢ C_i : s_{q_i} )_{i=1… n}% }{% \WF{E;\ind{p}{Γ_I}{Γ_C}}{Γ}% }\]

provided that the following side conditions hold:

\(k>0\) and all of \(I_j\) and \(c_i\) are distinct names for \(j=1… k\) and \(i=1… n\),

\(p\) is the number of parameters of \(\ind{p}{Γ_I}{Γ_C}\) and \(Γ_P\) is the context of parameters,

for \(j=1… k\) we have that \(A_j\) is an arity of sort \(s_j\) and \(I_j ∉ E\),

for \(i=1… n\) we have that \(C_i\) is a type of constructor of \(I_{q_i}\) which satisfies the positivity condition for \(I_1 … I_k\) and \(c_i ∉ Γ ∪ 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.

Example

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

- Inductive exProp (P:Prop->Prop) : Prop := | exP_intro : forall X:Prop, P X -> exProp P.
- exProp is defined exProp_ind is defined

The same definition on \(\Set\) is not allowed and fails:

- Fail Inductive exSet (P:Set->Prop) : Set := exS_intro : forall X:Set, P X -> exSet P.
- The command has indeed failed with message: 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
\((\Type(i)→\Prop)→\Type(j)\) with the constraint that the parameter \(X\) of \(\kw{exT}_{\kw{intro}}\)
has type \(\Type(k)\) with \(k<j\) and \(k≤ i\).

- Inductive exType (P:Type->Prop) : Type := exT_intro : forall X:Type, P X -> exType P.
- exType is defined exType_rect is defined exType_ind is defined exType_rec is defined

#### Template polymorphism¶

Inductive types declared in \(\Type\) are polymorphic over their arguments in \(\Type\). If \(A\) is an arity of some sort and \(s\) is 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 global environment and local 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.

Let \(\ind{p}{Γ_I}{Γ_C}\) be an inductive definition. Let
\(Γ_P = [p_1 :P_1 ;…;p_p :P_p ]\) be its context of parameters,
\(Γ_I = [I_1:∀ Γ_P ,A_1 ;…;I_k :∀ Γ_P ,A_k ]\) its context of definitions and
\(Γ_C = [c_1 :∀ Γ_P ,C_1 ;…;c_n :∀ Γ_P ,C_n]\) its context of constructors,
with \(c_i\) a constructor of \(I_{q_i}\). Let \(m ≤ p\) be the length of the
longest prefix of parameters such that the \(m\) first arguments of all
occurrences of all \(I_j\) in all \(C_k\) (even the occurrences in the
hypotheses of \(C_k\)) are exactly applied to \(p_1 … p_m\) (\(m\) is the number
of *recursively uniform parameters* and the \(p−m\) remaining parameters
are the *recursively non-uniform parameters*). Let \(q_1 , …, q_r\) , with
\(0≤ r≤ m\), be a (possibly) partial instantiation of the recursively
uniform parameters of \(Γ_P\) . We have:

- Ind-Family
- \[\begin{split}\frac{% \left\{\begin{array}{l}% \hspace{3em}% \ind{p}{Γ_I}{Γ_C} \in E\\% \hspace{3em}% (E[] ⊢ q_l : P'_l)_{l=1\ldots r}\\% \hspace{3em}% (E[] ⊢ P'_l ≤_{βδιζη} \subst{P_l}{p_u}{q_u}_{u=1\ldots l-1})_{l=1\ldots r}\\% \hspace{3em}% 1 \leq j \leq k% \hspace{3em}% \end{array}% \hspace{3em}% \right.% }{% E[] ⊢ I_j~q_1 … q_r :∀ [p_{r+1} :P_{r+1} ;…;p_p :P_p], (A_j)_{/s_j}% }\end{split}\]

provided that the following side conditions hold:

\(Γ_{P′}\) is the context obtained from \(Γ_P\) by replacing each \(P_l\) that is an arity with \(P_l'\) for \(1≤ l ≤ r\) (notice that \(P_l\) arity implies \(P_l'\) arity since \((E[] ⊢ P_l' ≤_{βδιζη} \subst{P_l}{p_u}{q_u}_{u=1\ldots l-1} )\);

there are sorts \(s_i\) , for \(1 ≤ i ≤ k\) such that, for \(Γ_{I'} = [I_1 :∀ Γ_{P'} ,(A_1)_{/s_1} ;…;I_k :∀ Γ_{P'} ,(A_k)_{/s_k}]\) we have \((E[Γ_{I′} ;Γ_{P′}] ⊢ C_i : s_{q_i})_{i=1… n}\) ;

the sorts \(s_i\) are such that all eliminations, to \(\Prop\), \(\Set\) and \(\Type(j)\), are allowed (see Section Destructors).

Notice that if \(I_j~q_1 … q_r\) 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′} = [c_1 :∀ Γ_{P′} ,C_1 ;…;c_n :∀ Γ_{P′} ,C_n ]\)). That is, the changes in the
types of each partial instance \(q_1 … q_r\) 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 \(I_j~q_1 … q_r\) or
\(C_i~q_1 … q_r\) 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 sorts \(s_j\) are chosen canonically so that each \(s_j\) is minimal with
respect to the hierarchy \(\Prop ⊂ \Set_p ⊂ \Type\) where \(\Set_p\) 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 Section Destructors) is set in \(\Prop\), a small non-singleton
inductive type is set in \(\Set\) (even in case \(\Set\) is impredicative – see
Section The-Calculus-of-Inductive-Construction-with-impredicative-Set),
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 Destructors). As
an example, let us consider the following definition:

Example

- Inductive option (A:Type) : Type := | None : option A | Some : A -> option A.
- option is defined option_rect is defined option_ind is defined option_rec is defined

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 Destructors) and it would lose the elimination to \(\Set\) and \(\Type\)
if set in \(\Prop\).

Example

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

Here is another example.

Example

- Inductive prod (A B:Type) : Type := pair : A -> B -> prod A B.
- prod is defined prod_rect is defined prod_ind is defined prod_rec is defined

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.

Example

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

注釈

Template polymorphism used to be called “sort-polymorphism of inductive types” before universe polymorphism (see Chapter Polymorphic Universes) was introduced.

### 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 local context,
we want to build a function length of type `list A -> nat`

which computes
the length of the list, 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 about an object \(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,(has_length A l
(length l))
```

it is enough to prove:

`(has_length A (nil A) (length (nil A)))`

`∀ a:A, ∀ l:list A, (has_length A l (length l)) →`

`(has_length A (cons A a l) (length (cons A a l)))`

which given the conversion equalities satisfied by length is the same as proving:

`(has_length A (nil A) O)`

`∀ a:A, ∀ l:list A, (has_length A l (length l)) →`

`(has_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 [Coq92]. 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 operator is that we have an object \(m\) in an inductive type \(I\) and we want to prove a property which possibly depends on \(m\). For this, it is enough to prove the property for \(m = (c_i~u_1 … u_{p_i} )\) for each constructor of \(I\). The Coq term for this proof will be written:

In this expression, if \(m\) eventually happens to evaluate to \((c_i~u_1 … u_{p_i})\) then the expression will behave as specified in its \(i\)-th branch and it will reduce to \(f_i\) where the \(x_{i1} …x_{ip_i}\) are replaced by the \(u_1 … u_{p_i}\) according to the ι-reduction.

Actually, for type checking a \(\Match…\with…\kwend\) 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 predicate over \(n+1\) arguments: the \(n\) first ones 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 and one argument. The predicate is made explicit using the syntax:

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, \(m\)
can occur in \(P\) where it is considered a bound variable). 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 other arguments of \(I\) (sometimes called
indices in the literature) have to be variables (\(a\) above) and these
variables can occur in \(P\). The expression after \(\In\) must be seen as an
*inductive type pattern*. Notice that expansion of implicit arguments
and notations apply to this pattern. For the purpose of presenting the
inference rules, we use a more compact notation:

**Allowed elimination sorts.** An important question for building the typing rule for match is what
can be the type of \(λ a x . P\) with respect to the type of \(m\). If \(m:I\)
and \(I:A\) and \(λ a x . P : B\) then by \([I:A|B]\) we mean that one can use
\(λ a x . P\) with \(m\) in the above match-construct.

**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\).

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.

- Prod
- \[\frac{% [(I~x):A′|B′]% }{% [I:∀ x:A, A′|∀ x:A, B′]% }\]

- Set & Type
- \[\frac{% s_1 ∈ \{\Set,\Type(j)\}% \hspace{3em}% s_2 ∈ \Sort% }{% [I:s_1 |I→ s_2 ]% }\]

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
- \[\frac{% ~% }{% [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:

Example

- Inductive or (A B:Prop) : Prop := or_introl : A -> or A B | or_intror : B -> or A B.
- or is defined or_ind is defined

The following definition which computes a boolean value by case over
the proof of `or A B`

is not accepted:

Example

- Fail Definition choice (A B: Prop) (x:or A B) := match x with or_introl _ _ a => true | or_intror _ _ b => false end.
- The command has indeed failed with message: 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 are two proofs of the same property which are provably different, contradicting the proof- irrelevance property which is sometimes a useful axiom:

Example

- Axiom proof_irrelevance : forall (P : Prop) (x y : P), x=y.
- proof_irrelevance is declared

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 gives 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
- \[\frac{% I~\kw{is an empty or singleton definition}% \hspace{3em}% s ∈ \Sort% }{% [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 a hypothesis \(h:a=b\) in the local context, it can
be used for rewriting not only in logical propositions but also in any
type.

Example

- 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 _ function_scope _ _ _]
- Require Extraction.
- [Loading ML file extraction_plugin.cmxs ... done]
- Extraction eq_rec.
- (** val eq_rec : 'a1 -> 'a2 -> 'a1 -> 'a2 **) let eq_rec _ f _ = 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 type \(I\). Let \(P\) be a term that represents the property to be
proved. We assume \(r\) is the number of parameters and \(p\) is the number of
arguments.

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

We write \(\{c\}^P\) for \(\{c:C\}^P\) with \(C\) the type of \(c\).

Example

The following term in concrete syntax:

```
match t as l return P' with
| nil _ => t1
| cons _ hd tl => t2
end
```

can be represented in abstract syntax as

where

According to the definition:

Given some \(P\) then \(\{(\kw{nil}~\nat)\}^P\) represents the expected type of \(f_1\) , and \(\{(\kw{cons}~\nat)\}^P\) represents the expected type of \(f_2\).

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

- match
- \[\begin{split}\frac{% \begin{array}{l}% \hspace{3em}% E[Γ] ⊢ c : (I~q_1 … q_r~t_1 … t_s ) \\% \hspace{3em}% E[Γ] ⊢ P : B \\% \hspace{3em}% [(I~q_1 … q_r)|B] \\% \hspace{3em}% (E[Γ] ⊢ f_i : \{(c_{p_i}~q_1 … q_r)\}^P)_{i=1… l}% \hspace{3em}% \end{array}% }{% E[Γ] ⊢ \case(c,P,f_1 |… |f_l ) : (P~t_1 … t_s~c)% }\end{split}\]

provided \(I\) is an inductive type in a definition \(\ind{r}{Γ_I}{Γ_C}\) with \(Γ_C = [c_1 :C_1 ;…;c_n :C_n ]\) and \(c_{p_1} … c_{p_l}\) are the only constructors of \(I\).

Example

Below is a typing rule for the term shown in the previous example:

- list example
- \[\begin{split}\frac{% \begin{array}{l}% \hspace{3em}% E[Γ] ⊢ t : (\List ~\nat) \\% \hspace{3em}% E[Γ] ⊢ P : B \\% \hspace{3em}% [(\List ~\nat)|B] \\% \hspace{3em}% E[Γ] ⊢ f_1 : {(\kw{nil} ~\nat)}^P \\% \hspace{3em}% E[Γ] ⊢ f_2 : {(\kw{cons} ~\nat)}^P% \hspace{3em}% \end{array}% }{% E[Γ] ⊢ \case(t,P,f_1 |f_2 ) : (P~t)% }\end{split}\]

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

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

with \(c_{p_i}\) the \(i\)-th constructor of the inductive type \(I\) with \(r\) parameters.

The ι-contraction of this term is \((f_i~a_1 … a_m )\) leading to the general reduction rule:

### 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):

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

In the inference rules, we represent such a term by

with \(t_i'\) (resp. \(A_i'\)) representing the term \(t_i\) abstracted (resp. generalized) with respect to the bindings in the context Γ_i , namely \(t_i'=λ Γ_i . t_i\) and \(A_i'=∀ Γ_i , A_i\).

#### Typing rule¶

The typing rule is the expected one for a fixpoint.

- Fix
- \[\frac{% (E[Γ] ⊢ A_i : s_i )_{i=1… n}% \hspace{3em}% (E[Γ,f_1 :A_1 ,…,f_n :A_n ] ⊢ t_i : A_i )_{i=1… n}% }{% E[Γ] ⊢ \Fix~f_i\{f_1 :A_1 :=t_1 … f_n :A_n :=t_n \} : A_i% }\]

Any fixpoint definition cannot be accepted because non-normalizing terms allow 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

can be represented by the term:

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 [Gimenez94]. 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

where \(k_i\) are positive integers. Each \(k_i\) represents the index of parameter of \(f_i\) , on which \(f_i\) is decreasing. Each \(A_i\) should be a type (reducible to a term) starting with at least \(k_i\) products \(∀ y_1 :B_1 ,… ∀ y_{k_i} :B_{k_i} , A_i'\) and \(B_{k_i}\) an inductive type.

Now in the definition \(t_i\), if \(f_j\) occurs then it should be applied to at least \(k_j\) arguments and the \(k_j\)-th argument should be syntactically recognized as structurally smaller than \(y_{k_i}\).

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}\), if the
type of a constructor \(c\) has the form
\(∀ p_1 :P_1 ,… ∀ p_r :P_r, ∀ x_1:T_1, … ∀ x_r :T_r, (I_j~p_1 … p_r~t_1 … t_s )\),
then the recursive
arguments will correspond to \(T_i\) in which one of the \(I_l\) occurs.

The main rules for being structurally smaller are the following. Given a variable \(y\) of an inductively defined type in a declaration \(\ind{r}{Γ_I}{Γ_C}\) where \(Γ_I\) is \([I_1 :A_1 ;…;I_k :A_k]\), and \(Γ_C\) is \([c_1 :C_1 ;…;c_n :C_n ]\), the terms structurally smaller than \(y\) are:

\((t~u)\) and \(λ x:u . t\) when \(t\) is structurally smaller than \(y\).

\(\case(c,P,f_1 … f_n)\) when each \(f_i\) is structurally smaller than \(y\). If \(c\) is \(y\) or is structurally smaller than \(y\), its type is an inductive definition \(I_p\) part of the inductive declaration corresponding to \(y\). Each \(f_i\) corresponds to a type of constructor \(C_q ≡ ∀ p_1 :P_1 ,…,∀ p_r :P_r , ∀ y_1 :B_1 , … ∀ y_k :B_k , (I~a_1 … a_k )\) and can consequently be written \(λ y_1 :B_1' . … λ y_k :B_k'. g_i\). (\(B_i'\) is obtained from \(B_i\) by substituting parameters for variables) the variables \(y_j\) occurring in \(g_i\) corresponding to recursive arguments \(B_i\) (the ones in which one of the \(I_l\) occurs) are structurally smaller than y.

The following definitions are correct, we enter them using the `Fixpoint`

command and show the internal representation.

Example

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

#### Reduction rule¶

Let \(F\) be the set of declarations: \(f_1 /k_1 :A_1 :=t_1 …f_n /k_n :A_n:=t_n\). The reduction for fixpoints is:

when \(a_{k_i}\) starts with a constructor. This last restriction is needed in order to keep strong normalization and corresponds to the reduction for primitive recursive operators. The following reductions are now possible:

**Mutual induction**

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

## Admissible rules for global environments¶

From the original rules of the type system, one can show the admissibility of rules which change the local context of definition of objects in the global environment. We show here the admissible rules that are used in the discharge mechanism at the end of a section.

**Abstraction.**
One can modify a global declaration by generalizing it over a
previously assumed constant \(c\). For doing that, we need to modify the
reference to the global declaration in the subsequent global
environment and local context by explicitly applying this constant to
the constant \(c'\).

Below, if \(Γ\) is a context of the form \([y_1 :A_1 ;…;y_n :A_n]\), we write \(∀x:U,\subst{Γ}{c}{x}\) to mean \([y_1 :∀ x:U,\subst{A_1}{c}{x};…;y_n :∀ x:U,\subst{A_n}{c}{x}]\) and \(\subst{E}{|Γ|}{|Γ|c}\) to mean the parallel substitution \(E\{y_1 /(y_1~c)\}…\{y_n/(y_n~c)\}\).

**First abstracting property:**

One can similarly modify a global declaration by generalizing it over a previously defined constant \(c′\). Below, if \(Γ\) is a context of the form \([y_1 :A_1 ;…;y_n :A_n]\), we write \(\subst{Γ}{c}{u}\) to mean \([y_1 :\subst{A_1} {c}{u};…;y_n:\subst{A_n} {c}{u}]\).

**Second abstracting property:**

**Pruning the local context.**
If one abstracts or substitutes constants with the above rules then it
may happen that some declared or defined constant does not occur any
more in the subsequent global environment and in the local context.
One can consequently derive the following property.

- First pruning property:
- \[\frac{% \WF{E;c:U;E′}{Γ}% \hspace{3em}% c~\kw{does not occur in}~E′~\kw{and}~Γ% }{% \WF{E;E′}{Γ}% }\]

- Second pruning property:
- \[\frac{% \WF{E;c:=u:U;E′}{Γ}% \hspace{3em}% c~\kw{does not occur in}~E′~\kw{and}~Γ% }{% \WF{E;E′}{Γ}% }\]

## 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 [Gimenez95][Gimenez98][GimenezCasteran05].

## The Calculus of Inductive Constructions with impredicative Set¶

Coq can be used as a type checker for the 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,

Example

- Fail Definition id: Set := forall X:Set,X->X.
- The command has indeed failed with message: The term "forall X : Set, X -> X" has type "Type" while it is expected to have type "Set" (universe inconsistency).

while it will type check, if one uses instead the coqtop
`-impredicative-set`

option..

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:

- ProdImp
- \[\frac{% E[Γ] ⊢ T : s% \hspace{3em}% s ∈ {\Sort}% \hspace{3em}% 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 elimination rules in the impredicative system for sort \(\Set\) become:

- Set1
- \[\frac{% s ∈ \{Prop, Set\}% }{% [I:Set|I→ s]% }\]

- Set2
- \[\frac{% I~\kw{is a small inductive definition}% \hspace{3em}% s ∈ \{\Type(i)\}% }{% [I:Set|I→ s]% }\]