1
\chapter{Calculus of Inductive Constructions}
3
\index{Cic@\textsc{CIC}}
4
\index{pCic@p\textsc{CIC}}
5
\index{Calculus of (Co)Inductive Constructions}
7
The underlying formal language of {\Coq} is a {\em Calculus of
8
Constructions} with {\em Inductive Definitions}. It is presented in
10
For {\Coq} version V7, this Calculus was known as the
11
{\em Calculus of (Co)Inductive Constructions}\index{Calculus of
12
(Co)Inductive Constructions} (\iCIC\ in short).
13
The underlying calculus of {\Coq} version V8.0 and up is a weaker
14
calculus where the sort \Set{} satisfies predicative rules.
15
We call this calculus the
16
{\em Predicative Calculus of (Co)Inductive
17
Constructions}\index{Predicative Calculus of
18
(Co)Inductive Constructions} (\pCIC\ in short).
19
In section~\ref{impredicativity} we give the extra-rules for \iCIC. A
20
compiling option of \Coq{} allows to type-check theories in this
23
In \CIC\, all objects have a {\em type}. There are types for functions (or
24
programs), there are atomic types (especially datatypes)... but also
25
types for proofs and types for the types themselves.
26
Especially, any object handled in the formalism must belong to a
27
type. For instance, the statement {\it ``for all x, P''} is not
28
allowed in type theory; you must say instead: {\it ``for all x
29
belonging to T, P''}. The expression {\it ``x belonging to T''} is
30
written {\it ``x:T''}. One also says: {\it ``x has type T''}.
31
The terms of {\CIC} are detailed in section \ref{Terms}.
33
In \CIC\, there is an internal reduction mechanism. In particular, it
34
allows to decide if two programs are {\em intentionally} equal (one
35
says {\em convertible}). Convertibility is presented in section
38
The remaining sections are concerned with the type-checking of terms.
39
The beginner can skip them.
41
The reader seeking a background on the Calculus of Inductive
42
Constructions may read several papers. Gim�nez and Cast�ran~\cite{GimCas05}
44
an introduction to inductive and coinductive definitions in Coq. In
45
their book~\cite{CoqArt}, Bertot and Cast�ran give a precise
46
description of the \CIC{} based on numerous practical examples.
47
Barras~\cite{Bar99}, Werner~\cite{Wer94} and
48
Paulin-Mohring~\cite{Moh97} are the most recent theses dealing with
49
Inductive Definitions. Coquand-Huet~\cite{CoHu85a,CoHu85b,CoHu86}
50
introduces the Calculus of Constructions. Coquand-Paulin~\cite{CoPa89}
51
extended this calculus to inductive definitions. The {\CIC} is a
52
formulation of type theory including the possibility of inductive
53
constructions, Barendregt~\cite{Bar91} studies the modern form of type
56
\section{The terms}\label{Terms}
58
In most type theories, one usually makes a syntactic distinction
59
between types and terms. This is not the case for \CIC\ which defines
60
both types and terms in the same syntactical structure. This is
61
because the type-theory itself forces terms and types to be defined in
62
a mutual recursive way and also because similar constructions can be
63
applied to both terms and types and consequently can share the same
66
Consider for instance the $\ra$ constructor and assume \nat\ is the
67
type of natural numbers. Then $\ra$ is used both to denote
68
$\nat\ra\nat$ which is the type of functions from \nat\ to \nat, and
69
to denote $\nat \ra \Prop$ which is the type of unary predicates over
70
the natural numbers. Consider abstraction which builds functions. It
71
serves to build ``ordinary'' functions as $\kw{fun}~x:\nat \Ra ({\tt mult} ~x~x)$ (assuming {\tt mult} is already defined) but may build also
72
predicates over the natural numbers. For instance $\kw{fun}~x:\nat \Ra
74
represent a predicate $P$, informally written in mathematics
75
$P(x)\equiv x=x$. If $P$ has type $\nat \ra \Prop$, $(P~x)$ is a
76
proposition, furthermore $\kw{forall}~x:\nat,(P~x)$ will represent the type of
77
functions which associate to each natural number $n$ an object of
78
type $(P~n)$ and consequently represent proofs of the formula
81
\subsection{Sorts}\label{Sorts}
83
Types are seen as terms of the language and then should belong to
84
another type. The type of a type is always a constant of the language
87
The two basic sorts in the language of \CIC\ are \Set\ and \Prop.
89
The sort \Prop\ intends to be the type of logical propositions. If
90
$M$ is a logical proposition then it denotes a class, namely the class
91
of terms representing proofs of $M$. An object $m$ belonging to $M$
92
witnesses the fact that $M$ is true. An object of type \Prop\ is
93
called a {\em proposition}.
95
The sort \Set\ intends to be the type of specifications. This includes
96
programs and the usual sets such as booleans, naturals, lists
99
These sorts themselves can be manipulated as ordinary terms.
100
Consequently sorts also should be given a type. Because assuming
101
simply that \Set\ has type \Set\ leads to an inconsistent theory, we
102
have infinitely many sorts in the language of \CIC. These are, in
103
addition to \Set\ and \Prop\, a hierarchy of universes \Type$(i)$
104
for any integer $i$. We call \Sort\ the set of sorts
106
\[\Sort \equiv \{\Prop,\Set,\Type(i)| i \in \NN\} \]
110
The sorts enjoy the following properties: {\Prop:\Type(0)}, {\Set:\Type(0)} and
111
{\Type$(i)$:\Type$(i+1)$}.
113
The user will never mention explicitly the index $i$ when referring to
114
the universe \Type$(i)$. One only writes \Type. The
115
system itself generates for each instance of \Type\ a new
116
index for the universe and checks that the constraints between these
117
indexes can be solved. From the user point of view we consequently
118
have {\sf Type :Type}.
120
We shall make precise in the typing rules the constraints between the
123
\paragraph{Implementation issues}
124
In practice, the {\Type} hierarchy is implemented using algebraic
125
universes. An algebraic universe $u$ is either a variable (a qualified
126
identifier with a number) or a successor of an algebraic universe (an
127
expression $u+1$), or an upper bound of algebraic universes (an
128
expression $max(u_1,...,u_n)$), or the base universe (the expression
129
$0$) which corresponds, in the arity of sort-polymorphic inductive
130
types, to the predicative sort {\Set}. A graph of constraints between
131
the universe variables is maintained globally. To ensure the existence
132
of a mapping of the universes to the positive integers, the graph of
133
constraints must remain acyclic. Typing expressions that violate the
134
acyclicity of the graph of constraints results in a \errindex{Universe
135
inconsistency} error (see also section~\ref{PrintingUniverses}).
137
\subsection{Constants}
138
Besides the sorts, the language also contains constants denoting
139
objects in the environment. These constants may denote previously
140
defined objects but also objects related to inductive definitions
141
(either the type itself or one of its constructors or destructors).
143
\medskip\noindent {\bf Remark. } In other presentations of \CIC,
144
the inductive objects are not seen as
145
external declarations but as first-class terms. Usually the
146
definitions are also completely ignored. This is a nice theoretical
147
point of view but not so practical. An inductive definition is
148
specified by a possibly huge set of declarations, clearly we want to
149
share this specification among the various inductive objects and not
150
to duplicate it. So the specification should exist somewhere and the
151
various objects should refer to it. We choose one more level of
152
indirection where the objects are just represented as constants and
153
the environment gives the information on the kind of object the
157
Our inductive objects will be manipulated as constants declared in the
158
environment. This roughly corresponds to the way they are actually
159
implemented in the \Coq\ system. It is simple to map this presentation
160
in a theory where inductive objects are represented by terms.
164
Terms are built from variables, global names, constructors,
165
abstraction, application, local declarations bindings (``let-in''
166
expressions) and product.
168
From a syntactic point of view, types cannot be distinguished from terms,
169
except that they cannot start by an abstraction, and that if a term is
170
a sort or a product, it should be a type.
172
More precisely the language of the {\em Calculus of Inductive
173
Constructions} is built from the following rules:
176
\item the sorts {\sf Set, Prop, Type} are terms.
177
\item names for global constants of the environment are terms.
178
\item variables are terms.
179
\item if $x$ is a variable and $T$, $U$ are terms then $\forall~x:T,U$
180
($\kw{forall}~x:T,U$ in \Coq{} concrete syntax) is a term. If $x$
181
occurs in $U$, $\forall~x:T,U$ reads as {\it ``for all x of type T,
182
U''}. As $U$ depends on $x$, one says that $\forall~x:T,U$ is a
183
{\em dependent product}. If $x$ doesn't occurs in $U$ then
184
$\forall~x:T,U$ reads as {\it ``if T then U''}. A non dependent
185
product can be written: $T \rightarrow U$.
186
\item if $x$ is a variable and $T$, $U$ are terms then $\lb~x:T \mto U$
187
($\kw{fun}~x:T\Ra U$ in \Coq{} concrete syntax) is a term. This is a
188
notation for the $\lambda$-abstraction of
189
$\lambda$-calculus\index{lambda-calculus@$\lambda$-calculus}
190
\cite{Bar81}. The term $\lb~x:T \mto U$ is a function which maps
191
elements of $T$ to $U$.
192
\item if $T$ and $U$ are terms then $(T\ U)$ is a term
193
($T~U$ in \Coq{} concrete syntax). The term $(T\
194
U)$ reads as {\it ``T applied to U''}.
195
\item if $x$ is a variable, and $T$, $U$ are terms then
196
$\kw{let}~x:=T~\kw{in}~U$ is a
197
term which denotes the term $U$ where the variable $x$ is locally
198
bound to $T$. This stands for the common ``let-in'' construction of
199
functional programs such as ML or Scheme.
202
\paragraph{Notations.} Application associates to the left such that
203
$(t~t_1\ldots t_n)$ represents $(\ldots (t~t_1)\ldots t_n)$. The
204
products and arrows associate to the right such that $\forall~x:A,B\ra C\ra
205
D$ represents $\forall~x:A,(B\ra (C\ra D))$. One uses sometimes
207
$\lb~x~y:A\mto B$ to denote the abstraction or product of several variables
208
of the same type. The equivalent formulation is $\forall~x:A, \forall y:A,B$ or
209
$\lb~x:A \mto \lb y:A \mto B$
211
\paragraph{Free variables.}
212
The notion of free variables is defined as usual. In the expressions
213
$\lb~x:T\mto U$ and $\forall x:T, U$ the occurrences of $x$ in $U$
214
are bound. They are represented by de Bruijn indexes in the internal
217
\paragraph{Substitution.} \index{Substitution}
218
The notion of substituting a term $t$ to free occurrences of a
219
variable $x$ in a term $u$ is defined as usual. The resulting term
220
is written $\subst{u}{x}{t}$.
223
\section{Typed terms}\label{Typed-terms}
225
As objects of type theory, terms are subjected to {\em type
226
discipline}. The well typing of a term depends on an environment which
227
consists in a global environment (see below) and a local context.
229
\paragraph{Local context.}
230
A {\em local context} (or shortly context) is an ordered list of
231
declarations of variables. The declaration of some variable $x$ is
232
either an assumption, written $x:T$ ($T$ is a type) or a definition,
233
written $x:=t:T$. We use brackets to write contexts. A
234
typical example is $[x:T;y:=u:U;z:V]$. Notice that the variables
235
declared in a context must be distinct. If $\Gamma$ declares some $x$,
236
we write $x \in \Gamma$. By writing $(x:T) \in \Gamma$ we mean that
237
either $x:T$ is an assumption in $\Gamma$ or that there exists some $t$ such
238
that $x:=t:T$ is a definition in $\Gamma$. If $\Gamma$ defines some
239
$x:=t:T$, we also write $(x:=t:T) \in \Gamma$. Contexts must be
240
themselves {\em well formed}. For the rest of the chapter, the
241
notation $\Gamma::(y:T)$ (resp. $\Gamma::(y:=t:T)$) denotes the context
242
$\Gamma$ enriched with the declaration $y:T$ (resp. $y:=t:T$). The
243
notation $[]$ denotes the empty context. \index{Context}
245
% Does not seem to be used further...
246
% Si dans l'explication WF(E)[Gamma] concernant les constantes
247
% definies ds un contexte
249
We define the inclusion of two contexts $\Gamma$ and $\Delta$ (written
250
as $\Gamma \subset \Delta$) as the property, for all variable $x$,
251
type $T$ and term $t$, if $(x:T) \in \Gamma$ then $(x:T) \in \Delta$
252
and if $(x:=t:T) \in \Gamma$ then $(x:=t:T) \in \Delta$.
254
% $|\Delta|$ for the length of the context $\Delta$, that is for the number
255
% of declarations (assumptions or definitions) in $\Delta$.
257
A variable $x$ is said to be free in $\Gamma$ if $\Gamma$ contains a
258
declaration $y:T$ such that $x$ is free in $T$.
260
\paragraph{Environment.}\index{Environment}
261
Because we are manipulating global declarations (constants and global
262
assumptions), we also need to consider a global environment $E$.
264
An environment is an ordered list of declarations of global
265
names. Declarations are either assumptions or ``standard''
266
definitions, that is abbreviations for well-formed terms
267
but also definitions of inductive objects. In the latter
268
case, an object in the environment will define one or more constants
269
(that is types and constructors, see section \ref{Cic-inductive-definitions}).
271
An assumption will be represented in the environment as
272
\Assum{\Gamma}{c}{T} which means that $c$ is assumed of some type $T$
273
well-defined in some context $\Gamma$. An (ordinary) definition will
274
be represented in the environment as \Def{\Gamma}{c}{t}{T} which means
275
that $c$ is a constant which is valid in some context $\Gamma$ whose
276
value is $t$ and type is $T$.
278
The rules for inductive definitions (see section
279
\ref{Cic-inductive-definitions}) have to be considered as assumption
280
rules to which the following definitions apply: if the name $c$ is
281
declared in $E$, we write $c \in E$ and if $c:T$ or $c:=t:T$ is
282
declared in $E$, we write $(c : T) \in E$.
284
\paragraph{Typing rules.}\label{Typing-rules}\index{Typing rules}
285
In the following, we assume $E$ is a valid environment wrt to
286
inductive definitions. We define simultaneously two
287
judgments. The first one \WTEG{t}{T} means the term $t$ is well-typed
288
and has type $T$ in the environment $E$ and context $\Gamma$. The
289
second judgment \WFE{\Gamma} means that the environment $E$ is
290
well-formed and the context $\Gamma$ is a valid context in this
291
environment. It also means a third property which makes sure that any
292
constant in $E$ was defined in an environment which is included in
294
\footnote{This requirement could be relaxed if we instead introduced
295
an explicit mechanism for instantiating constants. At the external
296
level, the Coq engine works accordingly to this view that all the
297
definitions in the environment were built in a sub-context of the
300
A term $t$ is well typed in an environment $E$ iff there exists a
301
context $\Gamma$ and a term $T$ such that the judgment \WTEG{t}{T} can
302
be derived from the following rules.
304
\item[W-E] \inference{\WF{[]}{[]}}
305
\item[W-S] % Ce n'est pas vrai : x peut apparaitre plusieurs fois dans Gamma
306
\inference{\frac{\WTEG{T}{s}~~~~s \in \Sort~~~~x \not\in
309
{\WFE{\Gamma::(x:T)}}~~~~~
310
\frac{\WTEG{t}{T}~~~~x \not\in
312
}{\WFE{\Gamma::(x:=t:T)}}}
313
\item[Def] \inference{\frac{\WTEG{t}{T}~~~c \notin E \cup \Gamma}
314
{\WF{E;\Def{\Gamma}{c}{t}{T}}{\Gamma}}}
315
\item[Assum] \inference{\frac{\WTEG{T}{s}~~~~s \in \Sort~~~~c \notin E \cup \Gamma}
316
{\WF{E;\Assum{\Gamma}{c}{T}}{\Gamma}}}
317
\item[Ax] \index{Typing rules!Ax}
318
\inference{\frac{\WFE{\Gamma}}{\WTEG{\Prop}{\Type(p)}}~~~~~
319
\frac{\WFE{\Gamma}}{\WTEG{\Set}{\Type(q)}}}
320
\inference{\frac{\WFE{\Gamma}~~~~i<j}{\WTEG{\Type(i)}{\Type(j)}}}
321
\item[Var]\index{Typing rules!Var}
322
\inference{\frac{ \WFE{\Gamma}~~~~~(x:T) \in \Gamma~~\mbox{or}~~(x:=t:T) \in \Gamma~\mbox{for some $t$}}{\WTEG{x}{T}}}
323
\item[Const] \index{Typing rules!Const}
324
\inference{\frac{\WFE{\Gamma}~~~~(c:T) \in E~~\mbox{or}~~(c:=t:T) \in E~\mbox{for some $t$} }{\WTEG{c}{T}}}
325
\item[Prod] \index{Typing rules!Prod}
326
\inference{\frac{\WTEG{T}{s}~~~~s \in \Sort~~~
327
\WTE{\Gamma::(x:T)}{U}{\Prop}}
328
{ \WTEG{\forall~x:T,U}{\Prop}}}
329
\inference{\frac{\WTEG{T}{s}~~~~s \in\{\Prop, \Set\}~~~~~~
330
\WTE{\Gamma::(x:T)}{U}{\Set}}
331
{ \WTEG{\forall~x:T,U}{\Set}}}
332
\inference{\frac{\WTEG{T}{\Type(i)}~~~~i\leq k~~~
333
\WTE{\Gamma::(x:T)}{U}{\Type(j)}~~~j \leq k}
334
{\WTEG{\forall~x:T,U}{\Type(k)}}}
335
\item[Lam]\index{Typing rules!Lam}
336
\inference{\frac{\WTEG{\forall~x:T,U}{s}~~~~ \WTE{\Gamma::(x:T)}{t}{U}}
337
{\WTEG{\lb~x:T\mto t}{\forall x:T, U}}}
338
\item[App]\index{Typing rules!App}
339
\inference{\frac{\WTEG{t}{\forall~x:U,T}~~~~\WTEG{u}{U}}
340
{\WTEG{(t\ u)}{\subst{T}{x}{u}}}}
341
\item[Let]\index{Typing rules!Let}
342
\inference{\frac{\WTEG{t}{T}~~~~ \WTE{\Gamma::(x:=t:T)}{u}{U}}
343
{\WTEG{\kw{let}~x:=t~\kw{in}~u}{\subst{U}{x}{t}}}}
346
\Rem We may have $\kw{let}~x:=t~\kw{in}~u$
347
well-typed without having $((\lb~x:T\mto u)~t)$ well-typed (where
348
$T$ is a type of $t$). This is because the value $t$ associated to $x$
349
may be used in a conversion rule (see section \ref{conv-rules}).
351
\section{Conversion rules}
352
\index{Conversion rules}
354
\paragraph{$\beta$-reduction.}
355
\label{beta}\index{beta-reduction@$\beta$-reduction}
357
We want to be able to identify some terms as we can identify the
358
application of a function to a given argument with its result. For
359
instance the identity function over a given type $T$ can be written
360
$\lb~x:T\mto x$. In any environment $E$ and context $\Gamma$, we want to identify any object $a$ (of type $T$) with the
361
application $((\lb~x:T\mto x)~a)$. We define for this a {\em reduction} (or a
362
{\em conversion}) rule we call $\beta$:
363
\[ \WTEGRED{((\lb~x:T\mto
364
t)~u)}{\triangleright_{\beta}}{\subst{t}{x}{u}} \]
365
We say that $\subst{t}{x}{u}$ is the {\em $\beta$-contraction} of
366
$((\lb~x:T\mto t)~u)$ and, conversely, that $((\lb~x:T\mto t)~u)$
367
is the {\em $\beta$-expansion} of $\subst{t}{x}{u}$.
369
According to $\beta$-reduction, terms of the {\em Calculus of
370
Inductive Constructions} enjoy some fundamental properties such as
371
confluence, strong normalization, subject reduction. These results are
372
theoretically of great importance but we will not detail them here and
373
refer the interested reader to \cite{Coq85}.
375
\paragraph{$\iota$-reduction.}
376
\label{iota}\index{iota-reduction@$\iota$-reduction}
377
A specific conversion rule is associated to the inductive objects in
378
the environment. We shall give later on (section \ref{iotared}) the
379
precise rules but it just says that a destructor applied to an object
380
built from a constructor behaves as expected. This reduction is
381
called $\iota$-reduction and is more precisely studied in
385
\paragraph{$\delta$-reduction.}
386
\label{delta}\index{delta-reduction@$\delta$-reduction}
388
We may have defined variables in contexts or constants in the global
389
environment. It is legal to identify such a reference with its value,
390
that is to expand (or unfold) it into its value. This
391
reduction is called $\delta$-reduction and shows as follows.
393
$$\WTEGRED{x}{\triangleright_{\delta}}{t}~~~~~\mbox{if $(x:=t:T) \in \Gamma$}~~~~~~~~~\WTEGRED{c}{\triangleright_{\delta}}{t}~~~~~\mbox{if $(c:=t:T) \in E$}$$
396
\paragraph{$\zeta$-reduction.}
397
\label{zeta}\index{zeta-reduction@$\zeta$-reduction}
399
Coq allows also to remove local definitions occurring in terms by
400
replacing the defined variable by its value. The declaration being
401
destroyed, this reduction differs from $\delta$-reduction. It is
402
called $\zeta$-reduction and shows as follows.
404
$$\WTEGRED{\kw{let}~x:=u~\kw{in}~t}{\triangleright_{\zeta}}{\subst{t}{x}{u}}$$
406
\paragraph{Convertibility.}
407
\label{convertibility}
408
\index{beta-reduction@$\beta$-reduction}\index{iota-reduction@$\iota$-reduction}\index{delta-reduction@$\delta$-reduction}\index{zeta-reduction@$\zeta$-reduction}
410
Let us write $\WTEGRED{t}{\triangleright}{u}$ for the contextual closure of the relation $t$ reduces to $u$ in the environment $E$ and context $\Gamma$ with one of the previous reduction $\beta$, $\iota$, $\delta$ or $\zeta$.
412
We say that two terms $t_1$ and $t_2$ are {\em convertible} (or {\em
413
equivalent)} in the environment $E$ and context $\Gamma$ iff there exists a term $u$ such that $\WTEGRED{t_1}{\triangleright \ldots \triangleright}{u}$
414
and $\WTEGRED{t_2}{\triangleright \ldots \triangleright}{u}$.
415
We then write $\WTEGCONV{t_1}{t_2}$.
417
The convertibility relation allows to introduce a new typing rule
418
which says that two convertible well-formed types have the same
421
At the moment, we did not take into account one rule between universes
422
which says that any term in a universe of index $i$ is also a term in
423
the universe of index $i+1$. This property is included into the
424
conversion rule by extending the equivalence relation of
425
convertibility into an order inductively defined by:
427
\item if $\WTEGCONV{t}{u}$ then $\WTEGLECONV{t}{u}$,
428
\item if $i \leq j$ then $\WTEGLECONV{\Type(i)}{\Type(j)}$,
429
\item for any $i$, $\WTEGLECONV{\Prop}{\Type(i)}$,
430
\item for any $i$, $\WTEGLECONV{\Set}{\Type(i)}$,
431
\item if $\WTEGCONV{T}{U}$ and $\WTELECONV{\Gamma::(x:T)}{T'}{U'}$ then $\WTEGLECONV{\forall~x:T,T'}{\forall~x:U,U'}$.
434
The conversion rule is now exactly:
436
\begin{description}\label{Conv}
437
\item[Conv]\index{Typing rules!Conv}
439
\frac{\WTEG{U}{s}~~~~\WTEG{t}{T}~~~~\WTEGLECONV{T}{U}}{\WTEG{t}{U}}}
443
\paragraph{$\eta$-conversion.
445
\index{eta-conversion@$\eta$-conversion}
446
\index{eta-reduction@$\eta$-reduction}}
448
An other important rule is the $\eta$-conversion. It is to identify
449
terms over a dummy abstraction of a variable followed by an
450
application of this variable. Let $T$ be a type, $t$ be a term in
451
which the variable $x$ doesn't occurs free. We have
452
\[ \WTEGRED{\lb~x:T\mto (t\ x)}{\triangleright}{t} \]
453
Indeed, as $x$ doesn't occur free in $t$, for any $u$ one
454
applies to $\lb~x:T\mto (t\ x)$, it $\beta$-reduces to $(t\ u)$. So
455
$\lb~x:T\mto (t\ x)$ and $t$ can be identified.
457
\Rem The $\eta$-reduction is not taken into account in the
458
convertibility rule of \Coq.
460
\paragraph{Normal form.}\index{Normal form}\label{Normal-form}\label{Head-normal-form}\index{Head normal form}
461
A term which cannot be any more reduced is said to be in {\em normal
462
form}. There are several ways (or strategies) to apply the reduction
463
rule. Among them, we have to mention the {\em head reduction} which
464
will play an important role (see chapter \ref{Tactics}). Any term can
465
be written as $\lb~x_1:T_1\mto \ldots \lb x_k:T_k \mto
466
(t_0\ t_1\ldots t_n)$ where
467
$t_0$ is not an application. We say then that $t_0$ is the {\em head
468
of $t$}. If we assume that $t_0$ is $\lb~x:T\mto u_0$ then one step of
469
$\beta$-head reduction of $t$ is:
470
\[\lb~x_1:T_1\mto \ldots \lb x_k:T_k\mto (\lb~x:T\mto u_0\ t_1\ldots t_n)
471
~\triangleright ~ \lb~(x_1:T_1)\ldots(x_k:T_k)\mto
472
(\subst{u_0}{x}{t_1}\ t_2 \ldots t_n)\]
473
Iterating the process of head reduction until the head of the reduced
474
term is no more an abstraction leads to the {\em $\beta$-head normal
476
\[ t \triangleright \ldots \triangleright
477
\lb~x_1:T_1\mto \ldots\lb x_k:T_k\mto (v\ u_1
479
where $v$ is not an abstraction (nor an application). Note that the
480
head normal form must not be confused with the normal form since some
481
$u_i$ can be reducible.
483
Similar notions of head-normal forms involving $\delta$, $\iota$ and $\zeta$
484
reductions or any combination of those can also be defined.
486
\section{Derived rules for environments}
488
From the original rules of the type system, one can derive new rules
489
which change the context of definition of objects in the environment.
490
Because these rules correspond to elementary operations in the \Coq\
491
engine used in the discharge mechanism at the end of a section, we
492
state them explicitly.
494
\paragraph{Mechanism of substitution.}
496
One rule which can be proved valid, is to replace a term $c$ by its
497
value in the environment. As we defined the substitution of a term for
498
a variable in a term, one can define the substitution of a term for a
499
constant. One easily extends this substitution to contexts and
502
\paragraph{Substitution Property:}
503
\inference{\frac{\WF{E;\Def{\Gamma}{c}{t}{T}; F}{\Delta}}
504
{\WF{E; \subst{F}{c}{t}}{\subst{\Delta}{c}{t}}}}
507
\paragraph{Abstraction.}
509
One can modify the context of definition of a constant $c$ by
510
abstracting a constant with respect to the last variable $x$ of its
511
defining context. For doing that, we need to check that the constants
512
appearing in the body of the declaration do not depend on $x$, we need
513
also to modify the reference to the constant $c$ in the environment
514
and context by explicitly applying this constant to the variable $x$.
515
Because of the rules for building environments and terms we know the
516
variable $x$ is available at each stage where $c$ is mentioned.
518
\paragraph{Abstracting property:}
519
\inference{\frac{\WF{E; \Def{\Gamma::(x:U)}{c}{t}{T};
520
F}{\Delta}~~~~\WFE{\Gamma}}
521
{\WF{E;\Def{\Gamma}{c}{\lb~x:U\mto t}{\forall~x:U,T};
522
\subst{F}{c}{(c~x)}}{\subst{\Delta}{c}{(c~x)}}}}
524
\paragraph{Pruning the context.}
525
We said the judgment \WFE{\Gamma} means that the defining contexts of
526
constants in $E$ are included in $\Gamma$. If one abstracts or
527
substitutes the constants with the above rules then it may happen
528
that the context $\Gamma$ is now bigger than the one needed for
529
defining the constants in $E$. Because defining contexts are growing
530
in $E$, the minimum context needed for defining the constants in $E$
531
is the same as the one for the last constant. One can consequently
532
derive the following property.
534
\paragraph{Pruning property:}
535
\inference{\frac{\WF{E; \Def{\Delta}{c}{t}{T}}{\Gamma}}
536
{\WF{E;\Def{\Delta}{c}{t}{T}}{\Delta}}}
539
\section{Inductive Definitions}\label{Cic-inductive-definitions}
541
A (possibly mutual) inductive definition is specified by giving the
542
names and the type of the inductive sets or families to be
543
defined and the names and types of the constructors of the inductive
544
predicates. An inductive declaration in the environment can
545
consequently be represented with two contexts (one for inductive
546
definitions, one for constructors).
548
Stating the rules for inductive definitions in their general form
549
needs quite tedious definitions. We shall try to give a concrete
550
understanding of the rules by precising them on running examples. We
551
take as examples the type of natural numbers, the type of
552
parameterized lists over a type $A$, the relation which states that
553
a list has some given length and the mutual inductive definition of trees and
556
\subsection{Representing an inductive definition}
557
\subsubsection{Inductive definitions without parameters}
558
As for constants, inductive definitions can be defined in a non-empty
560
We write \NInd{\Gamma}{\Gamma_I}{\Gamma_C} an inductive
561
definition valid in a context $\Gamma$, a
562
context of definitions $\Gamma_I$ and a context of constructors
564
\paragraph{Examples.}
565
The inductive declaration for the type of natural numbers will be:
566
\[\NInd{}{\nat:\Set}{\nO:\nat,\nS:\nat\ra\nat}\]
567
In a context with a variable $A:\Set$, the lists of elements in $A$ is
569
\[\NInd{A:\Set}{\List:\Set}{\Nil:\List,\cons : A \ra \List \ra
572
$\Gamma_I$ is $[I_1:A_1;\ldots;I_k:A_k]$, and $\Gamma_C$ is
573
$[c_1:C_1;\ldots;c_n:C_n]$, the general typing rules are,
574
for $1\leq j\leq k$ and $1\leq i\leq n$:
577
\inference{\frac{\NInd{\Gamma}{\Gamma_I}{\Gamma_C} \in E}{(I_j:A_j) \in E}}
579
\inference{\frac{\NInd{\Gamma}{\Gamma_I}{\Gamma_C} \in E}{(c_i:C_i) \in E}}
581
\subsubsection{Inductive definitions with parameters}
583
We have to slightly complicate the representation above in order to handle
584
the delicate problem of parameters.
585
Let us explain that on the example of \List. As they were defined
586
above, the type \List\ can only be used in an environment where we
587
have a variable $A:\Set$. Generally one want to consider lists of
588
elements in different types. For constants this is easily done by abstracting
589
the value over the parameter. In the case of inductive definitions we
590
have to handle the abstraction over several objects.
592
One possible way to do that would be to define the type \List\
593
inductively as being an inductive family of type $\Set\ra\Set$:
594
\[\NInd{}{\List:\Set\ra\Set}{\Nil:(A:\Set)(\List~A),\cons : (A:\Set)A
595
\ra (\List~A) \ra (\List~A)}\]
596
There are drawbacks to this point of view. The
597
information which says that for any $A$, $(\List~A)$ is an inductively defined
599
So we introduce two important definitions.
601
\paragraph{Inductive parameters, real arguments.}
602
An inductive definition $\NInd{\Gamma}{\Gamma_I}{\Gamma_C}$ admits
603
$r$ inductive parameters if each type of constructors $(c:C)$ in
604
$\Gamma_C$ is such that
606
p_1:P_1,\ldots,\forall p_r:P_r,\forall a_1:A_1, \ldots \forall a_n:A_n,
607
(I~p_1~\ldots p_r~t_1\ldots t_q)\]
608
with $I$ one of the inductive definitions in $\Gamma_I$.
609
We say that $n$ is the number of real arguments of the constructor
611
\paragraph{Context of parameters.}
612
If an inductive definition $\NInd{\Gamma}{\Gamma_I}{\Gamma_C}$ admits
613
$r$ inductive parameters, then there exists a context $\Gamma_P$ of
614
size $r$, such that $\Gamma_P=p_1:P_1;\ldots;p_r:P_r$ and
615
if $(t:A) \in \Gamma_I,\Gamma_C$ then $A$ can be written as
616
$\forall p_1:P_1,\ldots \forall p_r:P_r,A'$.
617
We call $\Gamma_P$ the context of parameters of the inductive
618
definition and use the notation $\forall \Gamma_P,A'$ for the term $A$.
620
If we have a term $t$ in an instance of an
621
inductive definition $I$ which starts with a constructor $c$, then the
622
$r$ first arguments of $c$ (the parameters) can be deduced from the
623
type $T$ of $t$: these are exactly the $r$ first arguments of $I$ in
624
the head normal form of $T$.
625
\paragraph{Examples.}
626
The \List{} definition has $1$ parameter:
627
\[\NInd{}{\List:\Set\ra\Set}{\Nil:(A:\Set)(\List~A),\cons : (A:\Set)A
628
\ra (\List~A) \ra (\List~A)}\]
629
This is also the case for this more complex definition where there is
630
a recursive argument on a different instance of \List:
631
\[\NInd{}{\List:\Set\ra\Set}{\Nil:(A:\Set)(\List~A),\cons : (A:\Set)A
632
\ra (\List~A\ra A) \ra (\List~A)}\]
633
But the following definition has $0$ parameters:
634
\[\NInd{}{\List:\Set\ra\Set}{\Nil:(A:\Set)(\List~A),\cons : (A:\Set)A
635
\ra (\List~A) \ra (\List~A*A)}\]
638
%The interested reader may compare the above definition with the two
639
%following ones which have very different logical meaning:\\
640
%$\NInd{}{\List:\Set}{\Nil:\List,\cons : (A:\Set)A
641
% \ra \List \ra \List}$ \\
642
%$\NInd{}{\List:\Set\ra\Set}{\Nil:(A:\Set)(\List~A),\cons : (A:\Set)A
643
% \ra (\List~A\ra A) \ra (\List~A)}$.}
644
\paragraph{Concrete syntax.}
645
In the Coq system, the context of parameters is given explicitly
646
after the name of the inductive definitions and is shared between the
647
arities and the type of constructors.
648
% The vernacular declaration of polymorphic trees and forests will be:\\
649
% \begin{coq_example*}
650
% Inductive Tree (A:Set) : Set :=
651
% Node : A -> Forest A -> Tree A
652
% with Forest (A : Set) : Set :=
654
% | Cons : Tree A -> Forest A -> Forest A
656
% will correspond in our formalism to:
657
% \[\NInd{}{{\tt Tree}:\Set\ra\Set;{\tt Forest}:\Set\ra \Set}
658
% {{\tt Node} : \forall A:\Set, A \ra {\tt Forest}~A \ra {\tt Tree}~A,
659
% {\tt Empty} : \forall A:\Set, {\tt Forest}~A,
660
% {\tt Cons} : \forall A:\Set, {\tt Tree}~A \ra {\tt Forest}~A \ra
662
We keep track in the syntax of the number of
665
Formally the representation of an inductive declaration
667
\Ind{\Gamma}{p}{\Gamma_I}{\Gamma_C} for an inductive
668
definition valid in a context $\Gamma$ with $p$ parameters, a
669
context of definitions $\Gamma_I$ and a context of constructors
672
The definition \Ind{\Gamma}{p}{\Gamma_I}{\Gamma_C} will be
673
well-formed exactly when \NInd{\Gamma}{\Gamma_I}{\Gamma_C} is and
674
when $p$ is (less or equal than) the number of parameters in
675
\NInd{\Gamma}{\Gamma_I}{\Gamma_C}.
678
The declaration for parameterized lists is:
679
\[\Ind{}{1}{\List:\Set\ra\Set}{\Nil:\forall A:\Set,\List~A,\cons : \forall
680
A:\Set, A \ra \List~A \ra \List~A}\]
682
The declaration for the length of lists is:
683
\[\Ind{}{1}{\Length:\forall A:\Set, (\List~A)\ra \nat\ra\Prop}
684
{\LNil:\forall A:\Set, \Length~A~(\Nil~A)~\nO,\\
685
\LCons :\forall A:\Set,\forall a:A, \forall l:(\List~A),\forall n:\nat, (\Length~A~l~n)\ra (\Length~A~(\cons~A~a~l)~(\nS~n))}\]
687
The declaration for a mutual inductive definition of forests and trees is:
688
\[\NInd{}{\tree:\Set,\forest:\Set}
689
{\\~~\node:\forest \ra \tree,
690
\emptyf:\forest,\consf:\tree \ra \forest \ra \forest\-}\]
692
These representations are the ones obtained as the result of the \Coq\
695
Inductive nat : Set :=
698
Inductive list (A:Set) : Set :=
700
| cons : A -> list A -> list A.
703
Inductive Length (A:Set) : list A -> nat -> Prop :=
704
| Lnil : Length A (nil A) O
706
forall (a:A) (l:list A) (n:nat),
707
Length A l n -> Length A (cons A a l) (S n).
708
Inductive tree : Set :=
709
node : forest -> tree
712
| consf : tree -> forest -> forest.
714
% The inductive declaration in \Coq\ is slightly different from the one
715
% we described theoretically. The difference is that in the type of
716
% constructors the inductive definition is explicitly applied to the
717
% parameters variables.
718
The \Coq\ type-checker verifies that all
719
parameters are applied in the correct manner in the conclusion of the
720
type of each constructors~:
722
In particular, the following definition will not be accepted because
723
there is an occurrence of \List\ which is not applied to the parameter
724
variable in the conclusion of the type of {\tt cons'}:
726
Set Printing Depth 50.
727
(********** The following is not correct and should produce **********)
728
(********* Error: The 1st argument of list' must be A in ... *********)
731
Inductive list' (A:Set) : Set :=
733
| cons' : A -> list' A -> list' (A*A).
735
Since \Coq{} version 8.1, there is no restriction about parameters in
736
the types of arguments of constructors. The following definition is
739
Inductive list' (A:Set) : Set :=
741
| cons' : A -> list' (A->A) -> list' A.
745
\subsection{Types of inductive objects}
746
We have to give the type of constants in an environment $E$ which
747
contains an inductive declaration.
750
\item[Ind-Const] Assuming
751
$\Gamma_I$ is $[I_1:A_1;\ldots;I_k:A_k]$, and $\Gamma_C$ is
752
$[c_1:C_1;\ldots;c_n:C_n]$,
754
\inference{\frac{\Ind{\Gamma}{p}{\Gamma_I}{\Gamma_C} \in E
755
~~j=1\ldots k}{(I_j:A_j) \in E}}
757
\inference{\frac{\Ind{\Gamma}{p}{\Gamma_I}{\Gamma_C} \in E
763
We have $(\List:\Set \ra \Set), (\cons:\forall~A:\Set,A\ra(\List~A)\ra
765
$(\Length:\forall~A:\Set, (\List~A)\ra\nat\ra\Prop)$, $\tree:\Set$ and $\forest:\Set$.
767
From now on, we write $\ListA$ instead of $(\List~A)$ and $\LengthA$
770
%\paragraph{Parameters.}
771
%%The parameters introduce a distortion between the inside specification
772
%%of the inductive declaration where parameters are supposed to be
773
%%instantiated (this representation is appropriate for checking the
774
%%correctness or deriving the destructor principle) and the outside
775
%%typing rules where the inductive objects are seen as objects
776
%%abstracted with respect to the parameters.
778
%In the definition of \List\ or \Length\, $A$ is a parameter because
779
%what is effectively inductively defined is $\ListA$ or $\LengthA$ for
780
%a given $A$ which is constant in the type of constructors. But when
781
%we define $(\LengthA~l~n)$, $l$ and $n$ are not parameters because the
782
%constructors manipulate different instances of this family.
784
\subsection{Well-formed inductive definitions}
785
We cannot accept any inductive declaration because some of them lead
786
to inconsistent systems. We restrict ourselves to definitions which
787
satisfy a syntactic criterion of positivity. Before giving the formal
788
rules, we need a few definitions:
790
\paragraph{Definitions}\index{Positivity}\label{Positivity}
792
A type $T$ is an {\em arity of sort $s$}\index{Arity} if it converts
793
to the sort $s$ or to a product $\forall~x:T,U$ with $U$ an arity
794
of sort $s$. (For instance $A\ra \Set$ or $\forall~A:\Prop,A\ra
795
\Prop$ are arities of sort respectively \Set\ and \Prop). A {\em type
796
of constructor of $I$}\index{Type of constructor} is either a term
797
$(I~t_1\ldots ~t_n)$ or $\fa x:T,C$ with $C$ a {\em type of constructor
802
The type of constructor $T$ will be said to {\em satisfy the positivity
803
condition} for a constant $X$ in the following cases:
806
\item $T=(X~t_1\ldots ~t_n)$ and $X$ does not occur free in
808
\item $T=\forall~x:U,V$ and $X$ occurs only strictly positively in $U$ and
809
the type $V$ satisfies the positivity condition for $X$
812
The constant $X$ {\em occurs strictly positively} in $T$ in the
816
\item $X$ does not occur in $T$
817
\item $T$ converts to $(X~t_1 \ldots ~t_n)$ and $X$ does not occur in
819
\item $T$ converts to $\forall~x:U,V$ and $X$ does not occur in
820
type $U$ but occurs strictly positively in type $V$
821
\item $T$ converts to $(I~a_1 \ldots ~a_m ~ t_1 \ldots ~t_p)$ where
822
$I$ is the name of an inductive declaration of the form
823
$\Ind{\Gamma}{m}{I:A}{c_1:\forall p_1:P_1,\ldots \forall
824
p_m:P_m,C_1;\ldots;c_n:\forall p_1:P_1,\ldots \forall
826
(in particular, it is not mutually defined and it has $m$
827
parameters) and $X$ does not occur in any of the $t_i$, and the
828
(instantiated) types of constructor $C_i\{p_j/a_j\}_{j=1\ldots m}$
830
the nested positivity condition for $X$
831
%\item more generally, when $T$ is not a type, $X$ occurs strictly
832
%positively in $T[x:U]u$ if $X$ does not occur in $U$ but occurs
833
%strictly positively in $u$
836
The type of constructor $T$ of $I$ {\em satisfies the nested
837
positivity condition} for a constant $X$ in the following
841
\item $T=(I~b_1\ldots b_m~u_1\ldots ~u_{p})$, $I$ is an inductive
842
definition with $m$ parameters and $X$ does not occur in
844
\item $T=\forall~x:U,V$ and $X$ occurs only strictly positively in $U$ and
845
the type $V$ satisfies the nested positivity condition for $X$
850
$X$ occurs strictly positively in $A\ra X$ or $X*A$ or $({\tt list}~
851
X)$ but not in $X \ra A$ or $(X \ra A)\ra A$ nor $({\tt neg}~A)$
852
assuming the notion of product and lists were already defined and {\tt
853
neg} is an inductive definition with declaration \Ind{}{A:\Set}{{\tt
854
neg}:\Set}{{\tt neg}:(A\ra{\tt False}) \ra {\tt neg}}. Assuming
855
$X$ has arity ${\tt nat \ra Prop}$ and {\tt ex} is the inductively
856
defined existential quantifier, the occurrence of $X$ in ${\tt (ex~
857
nat~ \lb~n:nat\mto (X~ n))}$ is also strictly positive.
859
\paragraph{Correctness rules.}
860
We shall now describe the rules allowing the introduction of a new
861
inductive definition.
864
\item[W-Ind] Let $E$ be an environment and
865
$\Gamma,\Gamma_P,\Gamma_I,\Gamma_C$ are contexts such that
866
$\Gamma_I$ is $[I_1:\forall \Gamma_P,A_1;\ldots;I_k:\forall
867
\Gamma_P,A_k]$ and $\Gamma_C$ is
868
$[c_1:\forall \Gamma_P,C_1;\ldots;c_n:\forall \Gamma_P,C_n]$.
871
(\WTE{\Gamma;\Gamma_P}{A_j}{s'_j})_{j=1\ldots k}
872
~~ (\WTE{\Gamma;\Gamma_I;\Gamma_P}{C_i}{s_{p_i}})_{i=1\ldots n}
874
{\WF{E;\Ind{\Gamma}{p}{\Gamma_I}{\Gamma_C}}{\Gamma}}}
875
provided that the following side conditions hold:
877
\item $k>0$, $I_j$, $c_i$ are different names for $j=1\ldots k$ and $i=1\ldots n$,
878
\item $p$ is the number of parameters of \NInd{\Gamma}{\Gamma_I}{\Gamma_C}
879
and $\Gamma_P$ is the context of parameters,
880
\item for $j=1\ldots k$ we have $A_j$ is an arity of sort $s_j$ and $I_j
881
\notin \Gamma \cup E$,
882
\item for $i=1\ldots n$ we have $C_i$ is a type of constructor of
883
$I_{p_i}$ which satisfies the positivity condition for $I_1 \ldots I_k$
884
and $c_i \notin \Gamma \cup E$.
887
One can remark that there is a constraint between the sort of the
888
arity of the inductive type and the sort of the type of its
889
constructors which will always be satisfied for the impredicative sort
890
(\Prop) but may fail to define inductive definition
891
on sort \Set{} and generate constraints between universes for
892
inductive definitions in the {\Type} hierarchy.
894
\paragraph{Examples.}
895
It is well known that existential quantifier can be encoded as an
896
inductive definition.
897
The following declaration introduces the second-order existential
898
quantifier $\exists X.P(X)$.
900
Inductive exProp (P:Prop->Prop) : Prop
901
:= exP_intro : forall X:Prop, P X -> exProp P.
903
The same definition on \Set{} is not allowed and fails~:
905
(********** The following is not correct and should produce **********)
906
(*** Error: Large non-propositional inductive types must be in Type***)
909
Inductive exSet (P:Set->Prop) : Set
910
:= exS_intro : forall X:Set, P X -> exSet P.
912
It is possible to declare the same inductive definition in the
914
The \texttt{exType} inductive definition has type $(\Type_i \ra\Prop)\ra
915
\Type_j$ with the constraint that the parameter \texttt{X} of \texttt{exT\_intro} has type $\Type_k$ with $k<j$ and $k\leq i$.
917
Inductive exType (P:Type->Prop) : Type
918
:= exT_intro : forall X:Type, P X -> exType P.
920
%We shall assume for the following definitions that, if necessary, we
921
%annotated the type of constructors such that we know if the argument
922
%is recursive or not. We shall write the type $(x:_R T)C$ if it is
923
%a recursive argument and $(x:_P T)C$ if the argument is not recursive.
925
\paragraph{Sort-polymorphism of inductive families.}
926
\index{Sort-polymorphism of inductive families}
928
From {\Coq} version 8.1, inductive families declared in {\Type} are
929
polymorphic over their arguments in {\Type}.
931
If $A$ is an arity and $s$ a sort, we write $A_{/s}$ for the arity
932
obtained from $A$ by replacing its sort with $s$. Especially, if $A$
933
is well-typed in some environment and context, then $A_{/s}$ is typable
934
by typability of all products in the Calculus of Inductive Constructions.
935
The following typing rule is added to the theory.
938
\item[Ind-Family] Let $\Gamma_P$ be a context of parameters
939
$[p_1:P_1;\ldots;p_{m'}:P_{m'}]$ and $m\leq m'$ be the length of the
940
initial prefix of parameters that occur unchanged in the recursive
941
occurrences of the constructor types. Assume that $\Gamma_I$ is
942
$[I_1:\forall \Gamma_P,A_1;\ldots;I_k:\forall \Gamma_P,A_k]$ and
943
$\Gamma_C$ is $[c_1:\forall \Gamma_P,C_1;\ldots;c_n:\forall
946
Let $q_1$, \ldots, $q_r$, with $0\leq r\leq m$, be a possibly partial
947
instantiation of the parameters in $\Gamma_P$. We have:
950
{\left\{\begin{array}{l}
951
\Ind{\Gamma}{p}{\Gamma_I}{\Gamma_C} \in E\\
952
(E[\Gamma] \vdash q_s : P'_s)_{s=1\ldots r}\\
953
(E[\Gamma] \vdash \WTEGLECONV{P'_s}{\subst{P_s}{x_u}{q_u}_{u=1\ldots s-1}})_{s=1\ldots r}\\
957
{(I_j\,q_1\,\ldots\,q_r:\forall \Gamma^{r+1}_p, (A_j)_{/s})}
960
provided that the following side conditions hold:
963
\item $\Gamma_{P'}$ is the context obtained from $\Gamma_P$ by
964
replacing, each $P_s$ that is an arity with the
965
sort of $P'_s$, as soon as $1\leq s \leq r$ (notice that
966
$P_s$ arity implies $P'_s$ arity since $E[\Gamma]
967
\vdash \WTEGLECONV{P'_s}{ \subst{P_s}{x_u}{q_u}_{u=1\ldots s-1}}$);
968
\item there are sorts $s_i$, for $1 \leq i \leq k$ such that, for
969
$\Gamma_{I'}$ obtained from $\Gamma_I$ by changing each $A_i$ by $(A_i)_{/s_i}$,
970
we have $(\WTE{\Gamma;\Gamma_{I'};\Gamma_{P'}}{C_i}{s_{p_i}})_{i=1\ldots n}$;
971
\item the sorts are such that all elimination are allowed (see
972
section~\ref{elimdep}).
976
Notice that if $I_j\,q_1\,\ldots\,q_r$ is typable using the rules {\bf
977
Ind-Const} and {\bf App}, then it is typable using the rule {\bf
978
Ind-Family}. Conversely, the extended theory is not stronger than the
979
theory without {\bf Ind-Family}. We get an equiconsistency result by
980
mapping each $\Ind{\Gamma}{p}{\Gamma_I}{\Gamma_C}$ occurring into a
981
given derivation into as many fresh inductive types and constructors
982
as the number of different (partial) replacements of sorts, needed for
983
this derivation, in the parameters that are arities. That is, the
984
changes in the types of each partial instance $q_1\,\ldots\,q_r$ can
985
be characterized by the ordered sets of arity sorts among the types of
986
parameters, and to each signature is associated a new inductive
987
definition with fresh names. Conversion is preserved as any (partial)
988
instance $I_j\,q_1\,\ldots\,q_r$ or $C_i\,q_1\,\ldots\,q_r$ is mapped
989
to the names chosen in the specific instance of
990
$\Ind{\Gamma}{p}{\Gamma_I}{\Gamma_C}$.
992
\newcommand{\Single}{\mbox{\textsf{Set}}}
994
In practice, the rule is used by {\Coq} only with in case the
995
inductive type is declared with an arity of a sort in the $\Type$
996
hierarchy, and, then, the polymorphism is over the parameters whose
997
type is an arity in the {\Type} hierarchy. The sort $s_j$ are then
998
chosen canonically so that each $s_j$ is minimal with respect to the
999
hierarchy ${\Prop_u}\subset{\Set_p}\subset\Type$ where $\Set_p$ is
1000
predicative {\Set}, and ${\Prop_u}$ is the sort of small singleton
1001
inductive types (i.e. of inductive types with one single constructor
1002
and that contains either proofs or inhabitants of singleton types
1003
only). More precisely, a small singleton inductive family is set in
1004
{\Prop}, a small non singleton inductive family is set in {\Set} (even
1005
in case {\Set} is impredicative -- see section~\ref{impredicativity}),
1006
and otherwise in the {\Type} hierarchy.
1007
% TODO: clarify the case of a partial application ??
1009
Note that the side-condition about allowed elimination sorts in the
1010
rule~{\bf Ind-Family} is just to avoid to recompute the allowed
1011
elimination sorts at each instance of a pattern-matching (see
1012
section~\ref{elimdep}).
1014
As an example, let us consider the following definition:
1015
\begin{coq_example*}
1016
Inductive option (A:Type) : Type :=
1018
| Some : A -> option A.
1021
As the definition is set in the {\Type} hierarchy, it is used
1022
polymorphically over its parameters whose types are arities of a sort
1023
in the {\Type} hierarchy. Here, the parameter $A$ has this property,
1024
hence, if \texttt{option} is applied to a type in {\Set}, the result is
1025
in {\Set}. Note that if \texttt{option} is applied to a type in {\Prop},
1026
then, the result is not set in \texttt{Prop} but in \texttt{Set}
1027
still. This is because \texttt{option} is not a singleton type (see
1028
section~\ref{singleton}) and it would loose the elimination to {\Set} and
1029
{\Type} if set in {\Prop}.
1032
Check (fun A:Set => option A).
1033
Check (fun A:Prop => option A).
1036
Here is another example.
1038
\begin{coq_example*}
1039
Inductive prod (A B:Type) : Type := pair : A -> B -> prod A B.
1042
As \texttt{prod} is a singleton type, it will be in {\Prop} if applied
1043
twice to propositions, in {\Set} if applied twice to at least one type
1044
in {\Set} and none in {\Type}, and in {\Type} otherwise. In all cases,
1045
the three kind of eliminations schemes are allowed.
1048
Check (fun A:Set => prod A).
1049
Check (fun A:Prop => prod A A).
1050
Check (fun (A:Prop) (B:Set) => prod A B).
1051
Check (fun (A:Type) (B:Prop) => prod A B).
1054
\subsection{Destructors}
1055
The specification of inductive definitions with arities and
1056
constructors is quite natural. But we still have to say how to use an
1057
object in an inductive type.
1059
This problem is rather delicate. There are actually several different
1060
ways to do that. Some of them are logically equivalent but not always
1061
equivalent from the computational point of view or from the user point
1064
From the computational point of view, we want to be able to define a
1065
function whose domain is an inductively defined type by using a
1066
combination of case analysis over the possible constructors of the
1067
object and recursion.
1069
Because we need to keep a consistent theory and also we prefer to keep
1070
a strongly normalizing reduction, we cannot accept any sort of
1071
recursion (even terminating). So the basic idea is to restrict
1072
ourselves to primitive recursive functions and functionals.
1074
For instance, assuming a parameter $A:\Set$ exists in the context, we
1075
want to build a function \length\ of type $\ListA\ra \nat$ which
1076
computes the length of the list, so such that $(\length~(\Nil~A)) = \nO$
1077
and $(\length~(\cons~A~a~l)) = (\nS~(\length~l))$. We want these
1078
equalities to be recognized implicitly and taken into account in the
1081
From the logical point of view, we have built a type family by giving
1082
a set of constructors. We want to capture the fact that we do not
1083
have any other way to build an object in this type. So when trying to
1084
prove a property $(P~m)$ for $m$ in an inductive definition it is
1085
enough to enumerate all the cases where $m$ starts with a different
1088
In case the inductive definition is effectively a recursive one, we
1089
want to capture the extra property that we have built the smallest
1090
fixed point of this recursive equation. This says that we are only
1091
manipulating finite objects. This analysis provides induction
1094
For instance, in order to prove $\forall l:\ListA,(\LengthA~l~(\length~l))$
1095
it is enough to prove:
1097
\noindent $(\LengthA~(\Nil~A)~(\length~(\Nil~A)))$ and
1100
$\forall a:A, \forall l:\ListA, (\LengthA~l~(\length~l)) \ra
1101
(\LengthA~(\cons~A~a~l)~(\length~(\cons~A~a~l)))$.
1104
\noindent which given the conversion equalities satisfied by \length\ is the
1106
$(\LengthA~(\Nil~A)~\nO)$ and $\forall a:A, \forall l:\ListA,
1107
(\LengthA~l~(\length~l)) \ra
1108
(\LengthA~(\cons~A~a~l)~(\nS~(\length~l)))$.
1110
One conceptually simple way to do that, following the basic scheme
1111
proposed by Martin-L\"of in his Intuitionistic Type Theory, is to
1112
introduce for each inductive definition an elimination operator. At
1113
the logical level it is a proof of the usual induction principle and
1114
at the computational level it implements a generic operator for doing
1115
primitive recursion over the structure.
1117
But this operator is rather tedious to implement and use. We choose in
1118
this version of Coq to factorize the operator for primitive recursion
1119
into two more primitive operations as was first suggested by Th. Coquand
1120
in~\cite{Coq92}. One is the definition by pattern-matching. The second one is a definition by guarded fixpoints.
1122
\subsubsection{The {\tt match\ldots with \ldots end} construction.}
1124
\index{match@{\tt match\ldots with\ldots end}}
1126
The basic idea of this destructor operation is that we have an object
1127
$m$ in an inductive type $I$ and we want to prove a property $(P~m)$
1128
which in general depends on $m$. For this, it is enough to prove the
1129
property for $m = (c_i~u_1\ldots u_{p_i})$ for each constructor of $I$.
1131
The \Coq{} term for this proof will be written~:
1132
\[\kw{match}~m~\kw{with}~ (c_1~x_{11}~...~x_{1p_1}) \Ra f_1 ~|~\ldots~|~
1133
(c_n~x_{n1}...x_{np_n}) \Ra f_n~ \kw{end}\]
1134
In this expression, if
1135
$m$ is a term built from a constructor $(c_i~u_1\ldots u_{p_i})$ then
1136
the expression will behave as it is specified with $i$-th branch and
1137
will reduce to $f_i$ where the $x_{i1}$\ldots $x_{ip_i}$ are replaced
1138
by the $u_1\ldots u_p$ according to the $\iota$-reduction.
1140
Actually, for type-checking a \kw{match\ldots with\ldots end}
1141
expression we also need to know the predicate $P$ to be proved by case
1142
analysis. In the general case where $I$ is an inductively defined
1143
$n$-ary relation, $P$ is a $n+1$-ary relation: the $n$ first arguments
1144
correspond to the arguments of $I$ (parameters excluded), and the last
1145
one corresponds to object $m$. \Coq{} can sometimes infer this
1146
predicate but sometimes not. The concrete syntax for describing this
1147
predicate uses the \kw{as\ldots in\ldots return} construction. For
1148
instance, let us assume that $I$ is an unary predicate with one
1149
parameter. The predicate is made explicit using the syntax~:
1150
\[\kw{match}~m~\kw{as}~ x~ \kw{in}~ I~\verb!_!~a~ \kw{return}~ (P~ x)
1151
~\kw{with}~ (c_1~x_{11}~...~x_{1p_1}) \Ra f_1 ~|~\ldots~|~
1152
(c_n~x_{n1}...x_{np_n}) \Ra f_n \kw{end}\]
1153
The \kw{as} part can be omitted if either the result type does not
1154
depend on $m$ (non-dependent elimination) or $m$ is a variable (in
1155
this case, the result type can depend on $m$). The \kw{in} part can be
1156
omitted if the result type does not depend on the arguments of
1157
$I$. Note that the arguments of $I$ corresponding to parameters
1158
\emph{must} be \verb!_!, because the result type is not generalized to
1159
all possible values of the parameters. The expression after \kw{in}
1160
must be seen as an \emph{inductive type pattern}. As a final remark,
1161
expansion of implicit arguments and notations apply to this pattern.
1163
For the purpose of presenting the inference rules, we use a more
1165
\[ \Case{(\lb a x \mto P)}{m}{ \lb x_{11}~...~x_{1p_1} \mto f_1 ~|~\ldots~|~
1166
\lb x_{n1}...x_{np_n} \mto f_n}\]
1168
%% CP 06/06 Obsolete avec la nouvelle syntaxe et incompatible avec la
1169
%% presentation theorique qui suit
1170
% \paragraph{Non-dependent elimination.}
1172
% When defining a function of codomain $C$ by case analysis over an
1173
% object in an inductive type $I$, we build an object of type $I
1174
% \ra C$. The minimality principle on an inductively defined logical
1175
% predicate $I$ of type $A \ra \Prop$ is often used to prove a property
1176
% $\forall x:A,(I~x)\ra (C~x)$. These are particular cases of the dependent
1177
% principle that we stated before with a predicate which does not depend
1178
% explicitly on the object in the inductive definition.
1180
% For instance, a function testing whether a list is empty
1183
% \[\kw{fun} l:\ListA \Ra \kw{match}~l~\kw{with}~ \Nil \Ra \true~
1184
% |~(\cons~a~m) \Ra \false \kw{end}\]
1186
% \[\lb~l:\ListA \mto\Case{\bool}{l}{\true~ |~ \lb a~m,~\false}\]
1187
%\noindent {\bf Remark. }
1189
% In the system \Coq\ the expression above, can be
1190
% written without mentioning
1191
% the dummy abstraction:
1192
% \Case{\bool}{l}{\Nil~ \mbox{\tt =>}~\true~ |~ (\cons~a~m)~
1193
% \mbox{\tt =>}~ \false}
1195
\paragraph{Allowed elimination sorts.}
1197
\index{Elimination sorts}
1199
An important question for building the typing rule for \kw{match} is
1200
what can be the type of $P$ with respect to the type of the inductive
1203
We define now a relation \compat{I:A}{B} between an inductive
1204
definition $I$ of type $A$ and an arity $B$. This relation states that
1205
an object in the inductive definition $I$ can be eliminated for
1206
proving a property $P$ of type $B$.
1208
The case of inductive definitions in sorts \Set\ or \Type{} is simple.
1209
There is no restriction on the sort of the predicate to be
1212
\paragraph{Notations.}
1213
The \compat{I:A}{B} is defined as the smallest relation satisfying the
1215
We write \compat{I}{B} for \compat{I:A}{B} where $A$ is the type of
1219
\item[Prod] \inference{\frac{\compat{(I~x):A'}{B'}}
1220
{\compat{I:(x:A)A'}{(x:A)B'}}}
1221
\item[\Set \& \Type] \inference{\frac{
1222
s_1 \in \{\Set,\Type(j)\},
1223
s_2 \in \Sort}{\compat{I:s_1}{I\ra s_2}}}
1226
The case of Inductive definitions of sort \Prop{} is a bit more
1227
complicated, because of our interpretation of this sort. The only
1228
harmless allowed elimination, is the one when predicate $P$ is also of
1231
\item[\Prop] \inference{\compat{I:\Prop}{I\ra\Prop}}
1233
\Prop{} is the type of logical propositions, the proofs of properties
1234
$P$ in \Prop{} could not be used for computation and are consequently
1235
ignored by the extraction mechanism.
1236
Assume $A$ and $B$ are two propositions, and the logical disjunction
1237
$A\vee B$ is defined inductively by~:
1238
\begin{coq_example*}
1239
Inductive or (A B:Prop) : Prop :=
1240
lintro : A -> or A B | rintro : B -> or A B.
1242
The following definition which computes a boolean value by case over
1243
the proof of \texttt{or A B} is not accepted~:
1245
(***************************************************************)
1246
(*** This example should fail with ``Incorrect elimination'' ***)
1249
Definition choice (A B: Prop) (x:or A B) :=
1250
match x with lintro a => true | rintro b => false end.
1252
From the computational point of view, the structure of the proof of
1253
\texttt{(or A B)} in this term is needed for computing the boolean
1256
In general, if $I$ has type \Prop\ then $P$ cannot have type $I\ra
1257
\Set$, because it will mean to build an informative proof of type
1258
$(P~m)$ doing a case analysis over a non-computational object that
1259
will disappear in the extracted program. But the other way is safe
1260
with respect to our interpretation we can have $I$ a computational
1261
object and $P$ a non-computational one, it just corresponds to proving
1262
a logical property of a computational object.
1264
% Also if $I$ is in one of the sorts \{\Prop, \Set\}, one cannot in
1265
% general allow an elimination over a bigger sort such as \Type. But
1266
% this operation is safe whenever $I$ is a {\em small inductive} type,
1267
% which means that all the types of constructors of
1268
% $I$ are small with the following definition:\\
1269
% $(I~t_1\ldots t_s)$ is a {\em small type of constructor} and
1270
% $\forall~x:T,C$ is a small type of constructor if $C$ is and if $T$
1271
% has type \Prop\ or \Set. \index{Small inductive type}
1273
% We call this particular elimination which gives the possibility to
1274
% compute a type by induction on the structure of a term, a {\em strong
1275
% elimination}\index{Strong elimination}.
1277
In the same spirit, elimination on $P$ of type $I\ra
1278
\Type$ cannot be allowed because it trivially implies the elimination
1279
on $P$ of type $I\ra \Set$ by cumulativity. It also implies that there
1280
is two proofs of the same property which are provably different,
1281
contradicting the proof-irrelevance property which is sometimes a
1284
Axiom proof_irrelevance : forall (P : Prop) (x y : P), x=y.
1287
Reset proof_irrelevance.
1289
The elimination of an inductive definition of type \Prop\ on a
1290
predicate $P$ of type $I\ra \Type$ leads to a paradox when applied to
1291
impredicative inductive definition like the second-order existential
1292
quantifier \texttt{exProp} defined above, because it give access to
1293
the two projections on this type.
1295
%\paragraph{Warning: strong elimination}
1296
%\index{Elimination!Strong elimination}
1297
%In previous versions of Coq, for a small inductive definition, only the
1298
%non-informative strong elimination on \Type\ was allowed, because
1299
%strong elimination on \Typeset\ was not compatible with the current
1300
%extraction procedure. In this version, strong elimination on \Typeset\
1301
%is accepted but a dummy element is extracted from it and may generate
1302
%problems if extracted terms are explicitly used such as in the
1303
%{\tt Program} tactic or when extracting ML programs.
1305
\paragraph{Empty and singleton elimination}
1307
\index{Elimination!Singleton elimination}
1308
\index{Elimination!Empty elimination}
1310
There are special inductive definitions in \Prop\ for which more
1311
eliminations are allowed.
1313
\item[\Prop-extended]
1315
\frac{I \mbox{~is an empty or singleton
1316
definition}~~~s \in \Sort}{\compat{I:\Prop}{I\ra s}}
1320
% A {\em singleton definition} has always an informative content,
1321
% even if it is a proposition.
1324
definition} has only one constructor and all the arguments of this
1325
constructor have type \Prop. In that case, there is a canonical
1326
way to interpret the informative extraction on an object in that type,
1327
such that the elimination on any sort $s$ is legal. Typical examples are
1328
the conjunction of non-informative propositions and the equality.
1329
If there is an hypothesis $h:a=b$ in the context, it can be used for
1330
rewriting not only in logical propositions but also in any type.
1331
% In that case, the term \verb!eq_rec! which was defined as an axiom, is
1332
% now a term of the calculus.
1337
An empty definition has no constructors, in that case also,
1338
elimination on any sort is allowed.
1340
\paragraph{Type of branches.}
1341
Let $c$ be a term of type $C$, we assume $C$ is a type of constructor
1342
for an inductive definition $I$. Let $P$ be a term that represents the
1343
property to be proved.
1344
We assume $r$ is the number of parameters.
1346
We define a new type \CI{c:C}{P} which represents the type of the
1347
branch corresponding to the $c:C$ constructor.
1350
\CI{c:(I_i~p_1\ldots p_r\ t_1 \ldots t_p)}{P} &\equiv (P~t_1\ldots ~t_p~c) \\[2mm]
1351
\CI{c:\forall~x:T,C}{P} &\equiv \forall~x:T,\CI{(c~x):C}{P}
1354
We write \CI{c}{P} for \CI{c:C}{P} with $C$ the type of $c$.
1356
\paragraph{Examples.}
1357
For $\ListA$ the type of $P$ will be $\ListA\ra s$ for $s \in \Sort$. \\
1358
$ \CI{(\cons~A)}{P} \equiv
1359
\forall a:A, \forall l:\ListA,(P~(\cons~A~a~l))$.
1361
For $\LengthA$, the type of $P$ will be
1362
$\forall l:\ListA,\forall n:\nat, (\LengthA~l~n)\ra \Prop$ and the expression
1363
\CI{(\LCons~A)}{P} is defined as:\\
1364
$\forall a:A, \forall l:\ListA, \forall n:\nat, \forall
1365
h:(\LengthA~l~n), (P~(\cons~A~a~l)~(\nS~n)~(\LCons~A~a~l~n~l))$.\\
1366
If $P$ does not depend on its third argument, we find the more natural
1368
$\forall a:A, \forall l:\ListA, \forall n:\nat,
1369
(\LengthA~l~n)\ra(P~(\cons~A~a~l)~(\nS~n))$.
1371
\paragraph{Typing rule.}
1373
Our very general destructor for inductive definition enjoys the
1374
following typing rule
1377
% \Case{P}{c}{[x_{11}:T_{11}]\ldots[x_{1p_1}:T_{1p_1}]g_1\ldots
1378
% [x_{n1}:T_{n1}]\ldots[x_{np_n}:T_{np_n}]g_n}
1382
% \Case{P}{c}{(c_1~x_{11}~...~x_{1p_1}) \Ra g_1 ~|~\ldots~|~
1383
% (c_n~x_{n1}...x_{np_n}) \Ra g_n }
1387
\item[match] \label{elimdep} \index{Typing rules!match}
1389
\frac{\WTEG{c}{(I~q_1\ldots q_r~t_1\ldots t_s)}~~
1390
\WTEG{P}{B}~~\compat{(I~q_1\ldots q_r)}{B}
1392
(\WTEG{f_i}{\CI{(c_{p_i}~q_1\ldots q_r)}{P}})_{i=1\ldots l}}
1393
{\WTEG{\Case{P}{c}{f_1|\ldots |f_l}}{(P\ t_1\ldots t_s\ c)}}}%\\[3mm]
1395
provided $I$ is an inductive type in a declaration
1396
\Ind{\Delta}{r}{\Gamma_I}{\Gamma_C} with
1397
$\Gamma_C = [c_1:C_1;\ldots;c_n:C_n]$ and $c_{p_1}\ldots c_{p_l}$ are the
1398
only constructors of $I$.
1401
\paragraph{Example.}
1402
For \List\ and \Length\ the typing rules for the {\tt match} expression
1403
are (writing just $t:M$ instead of \WTEG{t}{M}, the environment and
1404
context being the same in all the judgments).
1406
\[\frac{l:\ListA~~P:\ListA\ra s~~~f_1:(P~(\Nil~A))~~
1407
f_2:\forall a:A, \forall l:\ListA, (P~(\cons~A~a~l))}
1408
{\Case{P}{l}{f_1~|~f_2}:(P~l)}\]
1411
\begin{array}[b]{@{}c@{}}
1412
H:(\LengthA~L~N) \\ P:\forall l:\ListA, \forall n:\nat, (\LengthA~l~n)\ra
1414
f_1:(P~(\Nil~A)~\nO~\LNil) \\
1415
f_2:\forall a:A, \forall l:\ListA, \forall n:\nat, \forall
1416
h:(\LengthA~l~n), (P~(\cons~A~a~n)~(\nS~n)~(\LCons~A~a~l~n~h))
1418
{\Case{P}{H}{f_1~|~f_2}:(P~L~N~H)}\]
1420
\paragraph{Definition of $\iota$-reduction.}\label{iotared}
1421
\index{iota-reduction@$\iota$-reduction}
1422
We still have to define the $\iota$-reduction in the general case.
1424
A $\iota$-redex is a term of the following form:
1425
\[\Case{P}{(c_{p_i}~q_1\ldots q_r~a_1\ldots a_m)}{f_1|\ldots |
1427
with $c_{p_i}$ the $i$-th constructor of the inductive type $I$ with $r$
1430
The $\iota$-contraction of this term is $(f_i~a_1\ldots a_m)$ leading
1431
to the general reduction rule:
1432
\[ \Case{P}{(c_{p_i}~q_1\ldots q_r~a_1\ldots a_m)}{f_1|\ldots |
1433
f_n} \triangleright_{\iota} (f_i~a_1\ldots a_m) \]
1435
\subsection{Fixpoint definitions}
1436
\label{Fix-term} \index{Fix@{\tt Fix}}
1437
The second operator for elimination is fixpoint definition.
1438
This fixpoint may involve several mutually recursive definitions.
1439
The basic concrete syntax for a recursive set of mutually recursive
1440
declarations is (with $\Gamma_i$ contexts)~:
1441
\[\kw{fix}~f_1 (\Gamma_1) :A_1:=t_1~\kw{with} \ldots \kw{with}~ f_n
1442
(\Gamma_n) :A_n:=t_n\]
1443
The terms are obtained by projections from this set of declarations
1445
\[\kw{fix}~f_1 (\Gamma_1) :A_1:=t_1~\kw{with} \ldots \kw{with}~ f_n
1446
(\Gamma_n) :A_n:=t_n~\kw{for}~f_i\]
1447
In the inference rules, we represent such a
1449
\[\Fix{f_i}{f_1:A_1':=t_1' \ldots f_n:A_n':=t_n'}\]
1450
with $t_i'$ (resp. $A_i'$) representing the term $t_i$ abstracted
1451
(resp. generalized) with
1452
respect to the bindings in the context $\Gamma_i$, namely
1453
$t_i'=\lb \Gamma_i \mto t_i$ and $A_i'=\forall \Gamma_i, A_i$.
1455
\subsubsection{Typing rule}
1456
The typing rule is the expected one for a fixpoint.
1459
\item[Fix] \index{Typing rules!Fix}
1460
\inference{\frac{(\WTEG{A_i}{s_i})_{i=1\ldots n}~~~~
1461
(\WTE{\Gamma,f_1:A_1,\ldots,f_n:A_n}{t_i}{A_i})_{i=1\ldots n}}
1462
{\WTEG{\Fix{f_i}{f_1:A_1:=t_1 \ldots f_n:A_n:=t_n}}{A_i}}}
1465
Any fixpoint definition cannot be accepted because non-normalizing terms
1466
will lead to proofs of absurdity.
1468
The basic scheme of recursion that should be allowed is the one needed for
1470
recursive functionals. In that case the fixpoint enjoys a special
1471
syntactic restriction, namely one of the arguments belongs to an
1472
inductive type, the function starts with a case analysis and recursive
1473
calls are done on variables coming from patterns and representing subterms.
1475
For instance in the case of natural numbers, a proof of the induction
1477
\[\forall P:\nat\ra\Prop, (P~\nO)\ra(\forall n:\nat, (P~n)\ra(P~(\nS~n)))\ra
1478
\forall n:\nat, (P~n)\]
1479
can be represented by the term:
1481
\lb P:\nat\ra\Prop\mto\lb f:(P~\nO)\mto \lb g:(\forall n:\nat,
1482
(P~n)\ra(P~(\nS~n))) \mto\\
1483
\Fix{h}{h:\forall n:\nat, (P~n):=\lb n:\nat\mto \Case{P}{n}{f~|~\lb
1484
p:\nat\mto (g~p~(h~p))}}
1488
Before accepting a fixpoint definition as being correctly typed, we
1489
check that the definition is ``guarded''. A precise analysis of this
1490
notion can be found in~\cite{Gim94}.
1492
The first stage is to precise on which argument the fixpoint will be
1493
decreasing. The type of this argument should be an inductive
1496
For doing this the syntax of fixpoints is extended and becomes
1497
\[\Fix{f_i}{f_1/k_1:A_1:=t_1 \ldots f_n/k_n:A_n:=t_n}\]
1498
where $k_i$ are positive integers.
1499
Each $A_i$ should be a type (reducible to a term) starting with at least
1500
$k_i$ products $\forall y_1:B_1,\ldots \forall y_{k_i}:B_{k_i}, A'_i$
1502
being an instance of an inductive definition.
1504
Now in the definition $t_i$, if $f_j$ occurs then it should be applied
1505
to at least $k_j$ arguments and the $k_j$-th argument should be
1506
syntactically recognized as structurally smaller than $y_{k_i}$
1509
The definition of being structurally smaller is a bit technical.
1510
One needs first to define the notion of
1511
{\em recursive arguments of a constructor}\index{Recursive arguments}.
1512
For an inductive definition \Ind{\Gamma}{r}{\Gamma_I}{\Gamma_C},
1513
the type of a constructor $c$ has the form
1514
$\forall p_1:P_1,\ldots \forall p_r:P_r,
1515
\forall x_1:T_1, \ldots \forall x_r:T_r, (I_j~p_1\ldots
1516
p_r~t_1\ldots t_s)$ the recursive arguments will correspond to $T_i$ in
1517
which one of the $I_l$ occurs.
1520
The main rules for being structurally smaller are the following:\\
1521
Given a variable $y$ of type an inductive
1522
definition in a declaration
1523
\Ind{\Gamma}{r}{\Gamma_I}{\Gamma_C}
1524
where $\Gamma_I$ is $[I_1:A_1;\ldots;I_k:A_k]$, and $\Gamma_C$ is
1525
$[c_1:C_1;\ldots;c_n:C_n]$.
1526
The terms structurally smaller than $y$ are:
1528
\item $(t~u), \lb x:u \mto t$ when $t$ is structurally smaller than $y$ .
1529
\item \Case{P}{c}{f_1\ldots f_n} when each $f_i$ is structurally
1530
smaller than $y$. \\
1531
If $c$ is $y$ or is structurally smaller than $y$, its type is an inductive
1532
definition $I_p$ part of the inductive
1533
declaration corresponding to $y$.
1534
Each $f_i$ corresponds to a type of constructor $C_q \equiv
1535
\forall p_1:P_1,\ldots,\forall p_r:P_r, \forall y_1:B_1, \ldots \forall y_k:B_k, (I~a_1\ldots a_k)$
1536
and can consequently be
1537
written $\lb y_1:B'_1\mto \ldots \lb y_k:B'_k\mto g_i$.
1538
($B'_i$ is obtained from $B_i$ by substituting parameters variables)
1539
the variables $y_j$ occurring
1540
in $g_i$ corresponding to recursive arguments $B_i$ (the ones in
1541
which one of the $I_l$ occurs) are structurally smaller than $y$.
1543
The following definitions are correct, we enter them using the
1544
{\tt Fixpoint} command as described in section~\ref{Fixpoint} and show
1545
the internal representation.
1547
Fixpoint plus (n m:nat) {struct n} : nat :=
1550
| S p => S (plus p m)
1553
Fixpoint lgth (A:Set) (l:list A) {struct l} : nat :=
1556
| cons a l' => S (lgth A l')
1559
Fixpoint sizet (t:tree) : nat := let (f) := t in S (sizef f)
1560
with sizef (f:forest) : nat :=
1563
| consf t f => plus (sizet t) (sizef f)
1569
\subsubsection{Reduction rule}
1570
\index{iota-reduction@$\iota$-reduction}
1571
Let $F$ be the set of declarations: $f_1/k_1:A_1:=t_1 \ldots
1573
The reduction for fixpoints is:
1574
\[ (\Fix{f_i}{F}~a_1\ldots
1575
a_{k_i}) \triangleright_{\iota} \substs{t_i}{f_k}{\Fix{f_k}{F}}{k=1\ldots n}\]
1576
when $a_{k_i}$ starts with a constructor.
1577
This last restriction is needed in order to keep strong normalization
1578
and corresponds to the reduction for primitive recursive operators.
1580
We can illustrate this behavior on examples.
1582
Goal forall n m:nat, plus (S n) m = S (plus n m).
1585
Goal forall f:forest, sizet (node f) = S (sizef f).
1589
But assuming the definition of a son function from \tree\ to \forest:
1591
Definition sont (t:tree) : forest
1592
:= let (f) := t in f.
1594
The following is not a conversion but can be proved after a case analysis.
1596
(******************************************************************)
1597
(** Error: Impossible to unify .... **)
1600
Goal forall t:tree, sizet t = S (sizef (sont t)).
1601
reflexivity. (** this one fails **)
1609
% La disparition de Program devrait rendre la construction Match obsolete
1610
% \subsubsection{The {\tt Match \ldots with \ldots end} expression}
1612
% %\paragraph{A unary {\tt Match\ldots with \ldots end}.}
1613
% \index{Match...with...end@{\tt Match \ldots with \ldots end}}
1614
% The {\tt Match} operator which was a primitive notion in older
1615
% presentations of the Calculus of Inductive Constructions is now just a
1616
% macro definition which generates the good combination of {\tt Case}
1617
% and {\tt Fix} operators in order to generate an operator for primitive
1618
% recursive definitions. It always considers an inductive definition as
1619
% a single inductive definition.
1621
% The following examples illustrates this feature.
1622
% \begin{coq_example}
1623
% Definition nat_pr : (C:Set)C->(nat->C->C)->nat->C
1624
% :=[C,x,g,n]Match n with x g end.
1627
% \begin{coq_example}
1628
% Definition forest_pr
1629
% : (C:Set)C->(tree->forest->C->C)->forest->C
1630
% := [C,x,g,n]Match n with x g end.
1633
% Cet exemple faisait error (HH le 12/12/96), j'ai change pour une
1634
% version plus simple
1635
%\begin{coq_example}
1636
%Definition forest_pr
1637
% : (P:forest->Set)(P emptyf)->((t:tree)(f:forest)(P f)->(P (consf t f)))
1639
% := [C,x,g,n]Match n with x g end.
1642
\subsubsection{Mutual induction}
1644
The principles of mutual induction can be automatically generated
1645
using the {\tt Scheme} command described in section~\ref{Scheme}.
1647
\section{Coinductive types}
1648
The implementation contains also coinductive definitions, which are
1649
types inhabited by infinite objects.
1650
More information on coinductive definitions can be found
1651
in~\cite{Gimenez95b,Gim98,GimCas05}.
1652
%They are described in chapter~\ref{Coinductives}.
1654
\section{\iCIC : the Calculus of Inductive Construction with
1655
impredicative \Set}\label{impredicativity}
1657
\Coq{} can be used as a type-checker for \iCIC{}, the original
1658
Calculus of Inductive Constructions with an impredicative sort \Set{}
1659
by using the compiler option \texttt{-impredicative-set}.
1661
For example, using the ordinary \texttt{coqtop} command, the following
1664
(** This example should fail *******************************
1665
Error: The term forall X:Set, X -> X has type Type
1666
while it is expected to have type Set
1670
Definition id: Set := forall X:Set,X->X.
1672
while it will type-check, if one use instead the \texttt{coqtop
1673
-impredicative-set} command.
1675
The major change in the theory concerns the rule for product formation
1676
in the sort \Set, which is extended to a domain in any sort~:
1678
\item [Prod] \index{Typing rules!Prod (impredicative Set)}
1679
\inference{\frac{\WTEG{T}{s}~~~~s \in \Sort~~~~~~
1680
\WTE{\Gamma::(x:T)}{U}{\Set}}
1681
{ \WTEG{\forall~x:T,U}{\Set}}}
1683
This extension has consequences on the inductive definitions which are
1685
In the impredicative system, one can build so-called {\em large inductive
1686
definitions} like the example of second-order existential
1687
quantifier (\texttt{exSet}).
1689
There should be restrictions on the eliminations which can be
1690
performed on such definitions. The eliminations rules in the
1691
impredicative system for sort \Set{} become~:
1693
\item[\Set] \inference{\frac{s \in
1694
\{\Prop, \Set\}}{\compat{I:\Set}{I\ra s}}
1695
~~~~\frac{I \mbox{~is a small inductive definition}~~~~s \in
1697
{\compat{I:\Set}{I\ra s}}}
1702
% $Id: RefMan-cic.tex 9306 2006-10-28 18:28:19Z herbelin $
1704
%%% Local Variables:
1706
%%% TeX-master: "Reference-Manual"