~ubuntu-branches/ubuntu/wily/coq-doc/wily

« back to all changes in this revision

Viewing changes to doc/refman/RefMan-cic.tex

  • Committer: Bazaar Package Importer
  • Author(s): Stéphane Glondu, Stéphane Glondu, Samuel Mimram
  • Date: 2010-01-07 22:50:39 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100107225039-n3cq82589u0qt0s2
Tags: 8.2pl1-1
[ Stéphane Glondu ]
* New upstream release (Closes: #563669)
  - remove patches
* Packaging overhaul:
  - use git, advertise it in Vcs-* fields of debian/control
  - use debhelper 7 and dh with override
  - use source format 3.0 (quilt)
* debian/control:
  - set Maintainer to d-o-m, set Uploaders to Sam and myself
  - add Homepage field
  - bump Standards-Version to 3.8.3
* Register PDF documentation into doc-base
* Add debian/watch
* Update debian/copyright

[ Samuel Mimram ]
* Change coq-doc's description to mention that it provides documentation in
  pdf format, not postscript, closes: #543545.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
\chapter[Calculus of Inductive Constructions]{Calculus of Inductive Constructions
 
2
\label{Cic}
 
3
\index{Cic@\textsc{CIC}}
 
4
\index{pCic@p\textsc{CIC}}
 
5
\index{Calculus of (Co)Inductive Constructions}}
 
6
 
 
7
The underlying formal language of {\Coq} is a {\em Calculus of
 
8
  Constructions} with {\em Inductive Definitions}.  It is presented in
 
9
this chapter.  
 
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
 
21
  extended system.
 
22
 
 
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}.
 
32
 
 
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 
 
36
\ref{convertibility}.
 
37
 
 
38
The remaining sections are concerned with the type-checking of terms.
 
39
The beginner can skip them.
 
40
 
 
41
The reader seeking a background on the Calculus of Inductive
 
42
Constructions may read several papers. Gim�nez and Cast�ran~\cite{GimCas05} 
 
43
provide
 
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
 
54
theory.
 
55
 
 
56
\section[The terms]{The terms\label{Terms}}
 
57
 
 
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
 
64
syntactic structure.
 
65
 
 
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
 
73
(x=x)$ will
 
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
 
79
``$\forall x.P(x)$''.
 
80
 
 
81
\subsection[Sorts]{Sorts\label{Sorts}
 
82
\index{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
 
85
called a {\em sort}.
 
86
 
 
87
The two basic sorts in the language of \CIC\ are \Set\ and \Prop.
 
88
 
 
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}.
 
94
 
 
95
The sort \Set\ intends to be the type of specifications. This includes
 
96
programs and the usual sets such as booleans, naturals, lists
 
97
etc.
 
98
 
 
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
 
105
which is defined by:
 
106
\[\Sort \equiv \{\Prop,\Set,\Type(i)| i \in \NN\} \]
 
107
\index{Type@{\Type}}
 
108
\index{Prop@{\Prop}}
 
109
\index{Set@{\Set}}
 
110
The sorts enjoy the following properties: {\Prop:\Type(0)}, {\Set:\Type(0)} and
 
111
  {\Type$(i)$:\Type$(i+1)$}.
 
112
 
 
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}.
 
119
 
 
120
We shall make precise in the typing rules the constraints between the
 
121
indexes. 
 
122
 
 
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}).
 
136
 
 
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).
 
142
 
 
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
 
154
constant refers to.
 
155
 
 
156
\medskip
 
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.
 
161
 
 
162
\subsection{Terms}
 
163
 
 
164
Terms are built from variables, global names, constructors,
 
165
abstraction, application, local declarations bindings (``let-in''
 
166
expressions) and product.
 
167
 
 
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.
 
171
 
 
172
More precisely the language of the {\em Calculus of Inductive
 
173
  Constructions} is built from the following rules:
 
174
 
 
175
\begin{enumerate}
 
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.
 
200
\end{enumerate}
 
201
 
 
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
 
206
$\forall~x~y:A,B$ or
 
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$
 
210
 
 
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
 
215
structure of terms.
 
216
 
 
217
\paragraph[Substitution.]{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}$.
 
221
 
 
222
 
 
223
\section[Typed terms]{Typed terms\label{Typed-terms}}
 
224
 
 
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.
 
228
 
 
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}
 
244
 
 
245
% Does not seem to be used further...
 
246
% Si dans l'explication WF(E)[Gamma] concernant les constantes
 
247
% definies ds un contexte
 
248
 
 
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$.  
 
253
%We write
 
254
% $|\Delta|$ for the length of the context $\Delta$, that is for the number
 
255
% of declarations (assumptions or definitions) in $\Delta$.
 
256
 
 
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$.
 
259
 
 
260
\paragraph[Environment.]{Environment.\index{Environment}}
 
261
Because we are manipulating global declarations (constants and global
 
262
assumptions), we also need to consider a global environment $E$.
 
263
 
 
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}).
 
270
 
 
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$.
 
277
 
 
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$.
 
283
 
 
284
\paragraph[Typing rules.]{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
 
293
$\Gamma$
 
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
 
298
  current context.}.
 
299
 
 
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.
 
303
\begin{description}
 
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
 
307
      \Gamma           % \cup E
 
308
      }
 
309
      {\WFE{\Gamma::(x:T)}}~~~~~
 
310
    \frac{\WTEG{t}{T}~~~~x \not\in
 
311
      \Gamma           % \cup E
 
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}}}}
 
344
\end{description}
 
345
    
 
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}).
 
350
 
 
351
\section[Conversion rules]{Conversion rules\index{Conversion rules}
 
352
\label{conv-rules}}
 
353
\paragraph[$\beta$-reduction.]{$\beta$-reduction.\label{beta}\index{beta-reduction@$\beta$-reduction}}
 
354
 
 
355
We want to be able to identify some terms as we can identify the
 
356
application of a function to a given argument with its result. For
 
357
instance the identity function over a given type $T$ can be written
 
358
$\lb~x:T\mto x$. In any environment $E$ and context $\Gamma$, we want to identify any object $a$ (of type $T$) with the
 
359
application $((\lb~x:T\mto x)~a)$. We define for this a {\em reduction} (or a
 
360
{\em conversion}) rule we call $\beta$:
 
361
\[ \WTEGRED{((\lb~x:T\mto
 
362
  t)~u)}{\triangleright_{\beta}}{\subst{t}{x}{u}} \] 
 
363
We say that $\subst{t}{x}{u}$ is the {\em $\beta$-contraction} of
 
364
$((\lb~x:T\mto t)~u)$ and, conversely, that $((\lb~x:T\mto t)~u)$
 
365
is the {\em $\beta$-expansion} of $\subst{t}{x}{u}$.
 
366
 
 
367
According to $\beta$-reduction, terms of the {\em Calculus of
 
368
  Inductive Constructions} enjoy some fundamental properties such as
 
369
confluence, strong normalization, subject reduction. These results are
 
370
theoretically of great importance but we will not detail them here and
 
371
refer the interested reader to \cite{Coq85}.
 
372
 
 
373
\paragraph[$\iota$-reduction.]{$\iota$-reduction.\label{iota}\index{iota-reduction@$\iota$-reduction}}
 
374
A specific conversion rule is associated to the inductive objects in
 
375
the environment.  We shall give later on (see Section~\ref{iotared}) the
 
376
precise rules but it just says that a destructor applied to an object
 
377
built from a constructor behaves as expected.  This reduction is
 
378
called $\iota$-reduction and is more precisely studied in
 
379
\cite{Moh93,Wer94}.
 
380
 
 
381
 
 
382
\paragraph[$\delta$-reduction.]{$\delta$-reduction.\label{delta}\index{delta-reduction@$\delta$-reduction}}
 
383
 
 
384
We may have defined variables in contexts or constants in the global
 
385
environment. It is legal to identify such a reference with its value,
 
386
that is to expand (or unfold) it into its value. This
 
387
reduction is called $\delta$-reduction and shows as follows.
 
388
 
 
389
$$\WTEGRED{x}{\triangleright_{\delta}}{t}~~~~~\mbox{if $(x:=t:T) \in \Gamma$}~~~~~~~~~\WTEGRED{c}{\triangleright_{\delta}}{t}~~~~~\mbox{if $(c:=t:T) \in E$}$$
 
390
 
 
391
 
 
392
\paragraph[$\zeta$-reduction.]{$\zeta$-reduction.\label{zeta}\index{zeta-reduction@$\zeta$-reduction}}
 
393
 
 
394
Coq allows also to remove local definitions occurring in terms by
 
395
replacing the defined variable by its value. The declaration being
 
396
destroyed, this reduction differs from $\delta$-reduction. It is
 
397
called $\zeta$-reduction and shows as follows.
 
398
 
 
399
$$\WTEGRED{\kw{let}~x:=u~\kw{in}~t}{\triangleright_{\zeta}}{\subst{t}{x}{u}}$$
 
400
 
 
401
\paragraph[Convertibility.]{Convertibility.\label{convertibility}
 
402
\index{beta-reduction@$\beta$-reduction}\index{iota-reduction@$\iota$-reduction}\index{delta-reduction@$\delta$-reduction}\index{zeta-reduction@$\zeta$-reduction}}
 
403
 
 
404
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$.
 
405
 
 
406
We say that two terms $t_1$ and $t_2$ are {\em convertible} (or {\em
 
407
  equivalent)} in the environment $E$ and context $\Gamma$ iff there exists a term $u$ such that $\WTEGRED{t_1}{\triangleright \ldots \triangleright}{u}$
 
408
and $\WTEGRED{t_2}{\triangleright \ldots \triangleright}{u}$.
 
409
We then write $\WTEGCONV{t_1}{t_2}$.
 
410
 
 
411
The convertibility relation allows to introduce a new typing rule
 
412
which says that two convertible well-formed types have the same
 
413
inhabitants.
 
414
 
 
415
At the moment, we did not take into account one rule between universes
 
416
which says that any term in a universe of index $i$ is also a term in
 
417
the universe of index $i+1$. This property is included into the
 
418
conversion rule by extending the equivalence relation of
 
419
convertibility into an order inductively defined by:
 
420
\begin{enumerate}
 
421
\item if $\WTEGCONV{t}{u}$ then $\WTEGLECONV{t}{u}$,
 
422
\item if $i \leq j$ then $\WTEGLECONV{\Type(i)}{\Type(j)}$,
 
423
\item for any $i$, $\WTEGLECONV{\Prop}{\Type(i)}$,
 
424
\item for any $i$, $\WTEGLECONV{\Set}{\Type(i)}$,
 
425
\item if $\WTEGCONV{T}{U}$ and $\WTELECONV{\Gamma::(x:T)}{T'}{U'}$ then $\WTEGLECONV{\forall~x:T,T'}{\forall~x:U,U'}$.
 
426
\end{enumerate}
 
427
 
 
428
The conversion rule is now exactly:
 
429
 
 
430
\begin{description}\label{Conv}
 
431
\item[Conv]\index{Typing rules!Conv}
 
432
 \inference{
 
433
      \frac{\WTEG{U}{s}~~~~\WTEG{t}{T}~~~~\WTEGLECONV{T}{U}}{\WTEG{t}{U}}}
 
434
  \end{description}
 
435
 
 
436
 
 
437
\paragraph{$\eta$-conversion.
 
438
\label{eta}
 
439
\index{eta-conversion@$\eta$-conversion}
 
440
\index{eta-reduction@$\eta$-reduction}}
 
441
 
 
442
An other important rule is the $\eta$-conversion. It is to identify
 
443
terms over a dummy abstraction of a variable followed by an
 
444
application of this variable. Let $T$ be a type, $t$ be a term in
 
445
which the variable $x$ doesn't occurs free. We have
 
446
\[ \WTEGRED{\lb~x:T\mto (t\ x)}{\triangleright}{t} \]
 
447
Indeed, as $x$ doesn't occur free in $t$, for any $u$ one
 
448
applies to $\lb~x:T\mto (t\ x)$, it $\beta$-reduces to $(t\ u)$. So
 
449
$\lb~x:T\mto (t\ x)$ and $t$ can be identified.
 
450
 
 
451
\Rem The $\eta$-reduction is not taken into account in the
 
452
convertibility rule of \Coq.
 
453
 
 
454
\paragraph[Normal form.]{Normal form.\index{Normal form}\label{Normal-form}\label{Head-normal-form}\index{Head normal form}}
 
455
A term which cannot be any more reduced is said to be in {\em normal
 
456
  form}. There are several ways (or strategies) to apply the reduction
 
457
rule. Among them, we have to mention the {\em head reduction} which
 
458
will play an important role (see Chapter~\ref{Tactics}). Any term can
 
459
be written as $\lb~x_1:T_1\mto \ldots \lb x_k:T_k \mto 
 
460
(t_0\ t_1\ldots t_n)$ where
 
461
$t_0$ is not an application. We say then that $t_0$ is the {\em head
 
462
  of $t$}. If we assume that $t_0$ is $\lb~x:T\mto u_0$ then one step of
 
463
$\beta$-head reduction of $t$ is:
 
464
\[\lb~x_1:T_1\mto \ldots \lb x_k:T_k\mto (\lb~x:T\mto u_0\ t_1\ldots t_n) 
 
465
~\triangleright ~ \lb~(x_1:T_1)\ldots(x_k:T_k)\mto 
 
466
(\subst{u_0}{x}{t_1}\ t_2 \ldots t_n)\]
 
467
Iterating the process of head reduction until the head of the reduced
 
468
term is no more an abstraction leads to the {\em $\beta$-head normal
 
469
  form} of $t$:
 
470
\[ t \triangleright \ldots \triangleright
 
471
\lb~x_1:T_1\mto \ldots\lb x_k:T_k\mto (v\ u_1
 
472
\ldots u_m)\]
 
473
where $v$ is not an abstraction (nor an application).  Note that the
 
474
head normal form must not be confused with the normal form since some
 
475
$u_i$ can be reducible.
 
476
 
 
477
Similar notions of head-normal forms involving $\delta$, $\iota$ and $\zeta$
 
478
reductions or any combination of those can also be defined.
 
479
 
 
480
\section{Derived rules for environments}
 
481
 
 
482
From the original rules of the type system, one can derive new rules
 
483
which change the context of definition of objects in the environment.
 
484
Because these rules correspond to elementary operations in the \Coq\ 
 
485
engine used in the discharge mechanism at the end of a section, we
 
486
state them explicitly.
 
487
 
 
488
\paragraph{Mechanism of substitution.}
 
489
 
 
490
One rule which can be proved valid, is to replace a term $c$ by its
 
491
value in the environment. As we defined the substitution of a term for
 
492
a variable in a term, one can define the substitution of a term for a
 
493
constant. One easily extends this substitution to contexts and
 
494
environments.
 
495
 
 
496
\paragraph{Substitution Property:} 
 
497
\inference{\frac{\WF{E;\Def{\Gamma}{c}{t}{T}; F}{\Delta}}
 
498
           {\WF{E; \subst{F}{c}{t}}{\subst{\Delta}{c}{t}}}}
 
499
 
 
500
 
 
501
\paragraph{Abstraction.}
 
502
 
 
503
One can modify the context of definition of a constant $c$ by
 
504
abstracting a constant with respect to the last variable $x$ of its
 
505
defining context. For doing that, we need to check that the constants
 
506
appearing in the body of the declaration do not depend on $x$, we need
 
507
also to modify the reference to the constant $c$ in the environment
 
508
and context by explicitly applying this constant to the variable $x$.
 
509
Because of the rules for building environments and terms we know the
 
510
variable $x$ is available at each stage where $c$ is mentioned.
 
511
 
 
512
\paragraph{Abstracting property:} 
 
513
 \inference{\frac{\WF{E; \Def{\Gamma::(x:U)}{c}{t}{T};
 
514
       F}{\Delta}~~~~\WFE{\Gamma}}
 
515
           {\WF{E;\Def{\Gamma}{c}{\lb~x:U\mto t}{\forall~x:U,T};
 
516
  \subst{F}{c}{(c~x)}}{\subst{\Delta}{c}{(c~x)}}}}
 
517
 
 
518
\paragraph{Pruning the context.} 
 
519
We said the judgment \WFE{\Gamma} means that the defining contexts of
 
520
constants in $E$ are included in $\Gamma$. If one abstracts or
 
521
substitutes the constants with the above rules then it may happen
 
522
that the context $\Gamma$ is now bigger than the one needed for
 
523
defining the constants in $E$. Because defining contexts are growing
 
524
in $E$, the minimum context needed for defining the constants in $E$
 
525
is the same as the one for the last constant. One can consequently
 
526
derive the following property.
 
527
 
 
528
\paragraph{Pruning property:}
 
529
\inference{\frac{\WF{E; \Def{\Delta}{c}{t}{T}}{\Gamma}}
 
530
                {\WF{E;\Def{\Delta}{c}{t}{T}}{\Delta}}}
 
531
 
 
532
 
 
533
\section[Inductive Definitions]{Inductive Definitions\label{Cic-inductive-definitions}}
 
534
 
 
535
A (possibly mutual) inductive definition is specified by giving the
 
536
names and the type of the inductive sets or families to be
 
537
defined and the names and types of the constructors of the inductive
 
538
predicates.  An inductive declaration in the environment can
 
539
consequently be represented with two contexts (one for inductive
 
540
definitions, one for constructors).
 
541
 
 
542
Stating the rules for inductive definitions in their general form
 
543
needs quite tedious definitions. We shall try to give a concrete
 
544
understanding of the rules by precising them on running examples.  We
 
545
take as examples the type of natural numbers, the type of
 
546
parameterized lists over a type $A$, the relation which states that
 
547
a list has some given length and the mutual inductive definition of trees and
 
548
forests. 
 
549
 
 
550
\subsection{Representing an inductive definition}
 
551
\subsubsection{Inductive definitions without parameters}
 
552
As for constants, inductive definitions can be defined in a non-empty
 
553
context. \\
 
554
We write \NInd{\Gamma}{\Gamma_I}{\Gamma_C} an inductive
 
555
definition valid in a context $\Gamma$, a
 
556
context of definitions $\Gamma_I$ and a context of constructors
 
557
$\Gamma_C$.
 
558
\paragraph{Examples.}
 
559
The inductive declaration for the type of natural numbers will be:
 
560
\[\NInd{}{\nat:\Set}{\nO:\nat,\nS:\nat\ra\nat}\]
 
561
In a context with a variable $A:\Set$, the lists of elements in $A$ are
 
562
represented by:
 
563
\[\NInd{A:\Set}{\List:\Set}{\Nil:\List,\cons : A \ra \List \ra
 
564
  \List}\]
 
565
 Assuming 
 
566
  $\Gamma_I$ is $[I_1:A_1;\ldots;I_k:A_k]$, and $\Gamma_C$ is
 
567
  $[c_1:C_1;\ldots;c_n:C_n]$, the general typing rules are, 
 
568
  for $1\leq j\leq k$ and $1\leq i\leq n$:
 
569
 
 
570
\bigskip
 
571
\inference{\frac{\NInd{\Gamma}{\Gamma_I}{\Gamma_C} \in E}{(I_j:A_j) \in E}}
 
572
 
 
573
\inference{\frac{\NInd{\Gamma}{\Gamma_I}{\Gamma_C} \in E}{(c_i:C_i) \in E}}
 
574
 
 
575
\subsubsection{Inductive definitions with parameters}
 
576
 
 
577
We have to slightly complicate the representation above in order to handle
 
578
the delicate problem of parameters. 
 
579
Let us explain that on the example of \List. With the above definition,
 
580
the type \List\ can only be used in an environment where we
 
581
have a variable $A:\Set$. Generally one want to consider lists of
 
582
elements in different types. For constants this is easily done by abstracting
 
583
the value over the parameter. In the case of inductive definitions we
 
584
have to handle the abstraction over several objects.
 
585
 
 
586
One possible way to do that would be to define the type \List\
 
587
inductively as being an inductive family of type $\Set\ra\Set$:
 
588
\[\NInd{}{\List:\Set\ra\Set}{\Nil:(\forall A:\Set,\List~A),
 
589
  \cons : (\forall A:\Set, A \ra \List~A \ra \List~A)}\]
 
590
There are drawbacks to this point of view. The
 
591
information which says that for any $A$, $(\List~A)$ is an inductively defined
 
592
\Set\ has been lost.
 
593
So we introduce two important definitions.
 
594
 
 
595
\paragraph{Inductive parameters, real arguments.}
 
596
An inductive definition $\NInd{\Gamma}{\Gamma_I}{\Gamma_C}$ admits 
 
597
$r$ inductive parameters if each type of constructors $(c:C)$ in
 
598
$\Gamma_C$ is such that 
 
599
\[C\equiv \forall
 
600
p_1:P_1,\ldots,\forall p_r:P_r,\forall a_1:A_1, \ldots \forall a_n:A_n,
 
601
(I~p_1~\ldots p_r~t_1\ldots t_q)\]
 
602
with $I$ one of the inductive definitions in $\Gamma_I$. 
 
603
We say that $q$ is the number of real arguments of the constructor
 
604
$c$. 
 
605
\paragraph{Context of parameters.}
 
606
If an inductive definition $\NInd{\Gamma}{\Gamma_I}{\Gamma_C}$ admits 
 
607
$r$ inductive parameters, then there exists a context $\Gamma_P$ of
 
608
size $r$, such that $\Gamma_P=[p_1:P_1;\ldots;p_r:P_r]$ and 
 
609
if $(t:A) \in \Gamma_I,\Gamma_C$ then $A$ can be written as 
 
610
$\forall p_1:P_1,\ldots \forall p_r:P_r,A'$. 
 
611
We call $\Gamma_P$ the context of parameters of the inductive
 
612
definition and use the notation $\forall \Gamma_P,A'$ for the term $A$.
 
613
\paragraph{Remark.}
 
614
If we have a term $t$ in an instance of an
 
615
inductive definition $I$ which starts with a constructor $c$, then the
 
616
$r$ first arguments of $c$ (the parameters) can be deduced from the
 
617
type $T$ of $t$: these are exactly the $r$ first arguments of $I$ in
 
618
the head normal form of $T$.
 
619
\paragraph{Examples.}
 
620
The \List{} definition has $1$ parameter:
 
621
\[\NInd{}{\List:\Set\ra\Set}{\Nil:(\forall A:\Set, \List~A),
 
622
  \cons : (\forall A:\Set, A \ra \List~A \ra \List~A)}\]
 
623
This is also the case for this more complex definition where there is
 
624
a recursive argument on a different instance of \List: 
 
625
\[\NInd{}{\List:\Set\ra\Set}{\Nil:(\forall A:\Set, \List~A),
 
626
  \cons : (\forall A:\Set, A \ra \List~(A \ra A) \ra \List~A)}\]
 
627
But the following definition has $0$ parameters:
 
628
\[\NInd{}{\List:\Set\ra\Set}{\Nil:(\forall A:\Set, \List~A),
 
629
  \cons : (\forall A:\Set, A \ra \List~A \ra \List~(A*A))}\]
 
630
 
 
631
%\footnote{
 
632
%The interested reader may compare the above definition with the two 
 
633
%following ones which have very different logical meaning:\\
 
634
%$\NInd{}{\List:\Set}{\Nil:\List,\cons : (A:\Set)A
 
635
%  \ra \List \ra \List}$ \\
 
636
%$\NInd{}{\List:\Set\ra\Set}{\Nil:(A:\Set)(\List~A),\cons : (A:\Set)A
 
637
%  \ra (\List~A\ra A) \ra (\List~A)}$.}
 
638
\paragraph{Concrete syntax.}
 
639
In the Coq system, the context of parameters is given explicitly
 
640
after the name of the inductive definitions and is shared between the
 
641
arities and the type of constructors.
 
642
% The vernacular declaration of polymorphic trees and forests will be:\\
 
643
% \begin{coq_example*}
 
644
% Inductive Tree (A:Set) : Set := 
 
645
%    Node : A -> Forest A -> Tree A
 
646
% with Forest (A : Set) : Set := 
 
647
%    Empty : Forest A
 
648
%  | Cons  : Tree A -> Forest A -> Forest A
 
649
% \end{coq_example*}
 
650
% will correspond in our formalism to:
 
651
% \[\NInd{}{{\tt Tree}:\Set\ra\Set;{\tt Forest}:\Set\ra \Set}
 
652
%   {{\tt Node} : \forall A:\Set, A \ra {\tt Forest}~A \ra {\tt Tree}~A,
 
653
%    {\tt Empty} : \forall A:\Set, {\tt Forest}~A,
 
654
%    {\tt Cons} : \forall A:\Set, {\tt Tree}~A \ra {\tt Forest}~A \ra
 
655
%    {\tt Forest}~A}\]
 
656
We keep track in the syntax of the number of
 
657
parameters. 
 
658
 
 
659
Formally the representation of an inductive declaration
 
660
will be 
 
661
\Ind{\Gamma}{p}{\Gamma_I}{\Gamma_C} for an inductive
 
662
definition valid in a context $\Gamma$ with $p$ parameters, a
 
663
context of definitions $\Gamma_I$ and a context of constructors
 
664
$\Gamma_C$.
 
665
 
 
666
The definition \Ind{\Gamma}{p}{\Gamma_I}{\Gamma_C} will be
 
667
well-formed exactly when \NInd{\Gamma}{\Gamma_I}{\Gamma_C} is and 
 
668
when $p$ is (less or equal than) the number of parameters in
 
669
\NInd{\Gamma}{\Gamma_I}{\Gamma_C}. 
 
670
 
 
671
\paragraph{Examples}
 
672
The declaration for parameterized lists is:
 
673
\[\Ind{}{1}{\List:\Set\ra\Set}{\Nil:(\forall A:\Set,\List~A),\cons :
 
674
  (\forall A:\Set, A \ra \List~A \ra   \List~A)}\]
 
675
 
 
676
The declaration for the length of lists is:
 
677
\[\Ind{}{1}{\Length:\forall A:\Set, (\List~A)\ra \nat\ra\Prop}
 
678
      {\LNil:\forall A:\Set, \Length~A~(\Nil~A)~\nO,\\ 
 
679
     \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))}\]
 
680
 
 
681
The declaration for a mutual inductive definition of forests and trees is:
 
682
\[\NInd{}{\tree:\Set,\forest:\Set}
 
683
      {\\~~\node:\forest \ra \tree,
 
684
       \emptyf:\forest,\consf:\tree \ra \forest \ra \forest\-}\]
 
685
  
 
686
These representations are the ones obtained as the result of the \Coq\ 
 
687
declaration:
 
688
\begin{coq_example*}
 
689
Inductive nat : Set :=
 
690
  | O : nat
 
691
  | S : nat -> nat.
 
692
Inductive list (A:Set) : Set :=
 
693
  | nil : list A
 
694
  | cons : A -> list A -> list A.
 
695
\end{coq_example*}
 
696
\begin{coq_example*}
 
697
Inductive Length (A:Set) : list A -> nat -> Prop :=
 
698
  | Lnil : Length A (nil A) O
 
699
  | Lcons :
 
700
      forall (a:A) (l:list A) (n:nat),
 
701
        Length A l n -> Length A (cons A a l) (S n).
 
702
Inductive tree : Set :=
 
703
    node : forest -> tree
 
704
with forest : Set :=
 
705
  | emptyf : forest
 
706
  | consf : tree -> forest -> forest.
 
707
\end{coq_example*}
 
708
% The inductive declaration in \Coq\ is slightly different from the one
 
709
% we described theoretically. The difference is that in the type of
 
710
% constructors the inductive definition is explicitly applied to the
 
711
% parameters variables.
 
712
The \Coq\ type-checker verifies that all
 
713
parameters are applied in the correct manner in the conclusion of the
 
714
type of each constructors~:
 
715
 
 
716
In particular, the following definition will not be accepted because 
 
717
there is an occurrence of \List\ which is not applied to the parameter
 
718
variable in the conclusion of the type of {\tt cons'}:
 
719
\begin{coq_eval}
 
720
Set Printing Depth 50.
 
721
(********** The following is not correct and should produce **********)
 
722
(********* Error: The 1st argument of list' must be A in ... *********)
 
723
\end{coq_eval}
 
724
\begin{coq_example}
 
725
Inductive list' (A:Set) : Set :=
 
726
  | nil' : list' A
 
727
  | cons' : A -> list' A -> list' (A*A).
 
728
\end{coq_example}
 
729
Since \Coq{} version 8.1, there is no restriction about parameters in
 
730
the types of arguments of constructors. The following definition is
 
731
valid:
 
732
\begin{coq_example}
 
733
Inductive list' (A:Set) : Set :=
 
734
  | nil' : list' A
 
735
  | cons' : A -> list' (A->A) -> list' A.
 
736
\end{coq_example}
 
737
 
 
738
 
 
739
\subsection{Types of inductive objects}
 
740
We have to give the type of constants in an environment $E$ which
 
741
contains an inductive declaration.
 
742
 
 
743
\begin{description}
 
744
\item[Ind-Const] Assuming 
 
745
  $\Gamma_I$ is $[I_1:A_1;\ldots;I_k:A_k]$, and $\Gamma_C$ is
 
746
  $[c_1:C_1;\ldots;c_n:C_n]$,
 
747
   
 
748
\inference{\frac{\Ind{\Gamma}{p}{\Gamma_I}{\Gamma_C} \in E
 
749
    ~~j=1\ldots k}{(I_j:A_j) \in E}}
 
750
 
 
751
\inference{\frac{\Ind{\Gamma}{p}{\Gamma_I}{\Gamma_C} \in E
 
752
    ~~~~i=1.. n}
 
753
   {(c_i:C_i) \in E}}
 
754
\end{description}
 
755
 
 
756
\paragraph{Example.}
 
757
We have $(\List:\Set \ra \Set), (\cons:\forall~A:\Set,A\ra(\List~A)\ra
 
758
(\List~A))$, \\ 
 
759
$(\Length:\forall~A:\Set, (\List~A)\ra\nat\ra\Prop)$, $\tree:\Set$ and $\forest:\Set$.
 
760
 
 
761
From now on, we write $\ListA$ instead of $(\List~A)$ and $\LengthA$
 
762
for $(\Length~A)$.
 
763
 
 
764
%\paragraph{Parameters.}
 
765
%%The parameters introduce a distortion between the inside specification
 
766
%%of the inductive declaration where parameters are supposed to be
 
767
%%instantiated (this representation is appropriate for checking the
 
768
%%correctness or deriving the destructor principle) and the outside
 
769
%%typing rules where the inductive objects are seen as objects
 
770
%%abstracted with respect to the parameters.
 
771
 
 
772
%In the definition of \List\ or \Length\, $A$ is a parameter because
 
773
%what is effectively inductively defined is $\ListA$ or $\LengthA$ for
 
774
%a given $A$ which is constant in the type of constructors.  But when
 
775
%we define $(\LengthA~l~n)$, $l$ and $n$ are not parameters because the
 
776
%constructors manipulate different instances of this family.
 
777
 
 
778
\subsection{Well-formed inductive definitions}
 
779
We cannot accept any inductive declaration because some of them lead
 
780
to inconsistent systems. We restrict ourselves to definitions which
 
781
satisfy a syntactic criterion of positivity. Before giving the formal
 
782
rules, we need a few definitions:
 
783
 
 
784
\paragraph[Definitions]{Definitions\index{Positivity}\label{Positivity}}
 
785
 
 
786
A type $T$ is an {\em arity of sort $s$}\index{Arity} if it converts
 
787
to the sort $s$ or to a product $\forall~x:T,U$ with $U$ an arity
 
788
of sort $s$. (For instance $A\ra \Set$ or $\forall~A:\Prop,A\ra
 
789
\Prop$ are arities of sort respectively \Set\ and \Prop).  A {\em type
 
790
  of constructor of $I$}\index{Type of constructor} is either a term
 
791
$(I~t_1\ldots ~t_n)$ or $\fa x:T,C$ with $C$ recursively 
 
792
a {\em type of constructor of $I$}.
 
793
 
 
794
\smallskip
 
795
 
 
796
The type of constructor $T$ will be said to {\em satisfy the positivity
 
797
condition} for a constant $X$ in the following cases:
 
798
 
 
799
\begin{itemize}
 
800
\item $T=(X~t_1\ldots ~t_n)$ and $X$ does not occur free in
 
801
any $t_i$
 
802
\item $T=\forall~x:U,V$ and $X$ occurs only strictly positively in $U$ and
 
803
the type $V$ satisfies the positivity condition for $X$
 
804
\end{itemize}
 
805
 
 
806
The constant $X$ {\em occurs strictly positively} in $T$ in the
 
807
following cases:
 
808
 
 
809
\begin{itemize}
 
810
\item $X$ does not occur in $T$
 
811
\item $T$ converts to $(X~t_1 \ldots ~t_n)$ and $X$ does not occur in
 
812
  any of $t_i$
 
813
\item $T$ converts to $\forall~x:U,V$ and $X$ does not occur in
 
814
  type $U$ but occurs strictly positively in type $V$
 
815
\item $T$ converts to $(I~a_1 \ldots ~a_m ~ t_1 \ldots ~t_p)$ where
 
816
  $I$ is the name of an inductive declaration of the form
 
817
  $\Ind{\Gamma}{m}{I:A}{c_1:\forall p_1:P_1,\ldots \forall
 
818
    p_m:P_m,C_1;\ldots;c_n:\forall p_1:P_1,\ldots \forall
 
819
    p_m:P_m,C_n}$ 
 
820
  (in particular, it is not mutually defined and it has $m$
 
821
  parameters) and $X$ does not occur in any of the $t_i$, and the
 
822
  (instantiated) types of constructor $C_i\{p_j/a_j\}_{j=1\ldots m}$
 
823
  of $I$ satisfy 
 
824
  the nested positivity condition for $X$
 
825
%\item more generally, when $T$ is not a type, $X$ occurs strictly
 
826
%positively in $T[x:U]u$ if $X$ does not occur in $U$ but occurs
 
827
%strictly positively in $u$
 
828
\end{itemize}
 
829
 
 
830
The type of constructor $T$ of $I$ {\em satisfies the nested
 
831
positivity condition} for a constant $X$ in the following
 
832
cases:
 
833
 
 
834
\begin{itemize}
 
835
\item $T=(I~b_1\ldots b_m~u_1\ldots ~u_{p})$, $I$ is an inductive
 
836
  definition with $m$ parameters and $X$ does not occur in
 
837
any $u_i$
 
838
\item $T=\forall~x:U,V$ and $X$ occurs only strictly positively in $U$ and
 
839
the type $V$ satisfies the nested positivity condition for $X$
 
840
\end{itemize}
 
841
 
 
842
\paragraph{Example}
 
843
 
 
844
$X$ occurs strictly positively in $A\ra X$ or $X*A$ or $({\tt list}~
 
845
X)$ but not in $X \ra A$ or $(X \ra A)\ra A$ nor $({\tt neg}~A)$
 
846
assuming the notion of product and lists were already defined and {\tt
 
847
  neg} is an inductive definition with declaration \Ind{}{A:\Set}{{\tt
 
848
    neg}:\Set}{{\tt neg}:(A\ra{\tt False}) \ra {\tt neg}}.  Assuming
 
849
$X$ has arity ${\tt nat \ra Prop}$ and {\tt ex} is the inductively
 
850
defined existential quantifier, the occurrence of $X$ in ${\tt (ex~
 
851
  nat~ \lb~n:nat\mto (X~ n))}$ is also strictly positive.
 
852
 
 
853
\paragraph{Correctness rules.}
 
854
We shall now describe the rules allowing the introduction of a new
 
855
inductive definition.
 
856
 
 
857
\begin{description}
 
858
\item[W-Ind] Let $E$ be an environment and
 
859
  $\Gamma,\Gamma_P,\Gamma_I,\Gamma_C$ are contexts such that
 
860
  $\Gamma_I$ is $[I_1:\forall \Gamma_P,A_1;\ldots;I_k:\forall
 
861
  \Gamma_P,A_k]$ and $\Gamma_C$ is 
 
862
  $[c_1:\forall \Gamma_P,C_1;\ldots;c_n:\forall \Gamma_P,C_n]$. 
 
863
\inference{
 
864
  \frac{
 
865
  (\WTE{\Gamma;\Gamma_P}{A_j}{s'_j})_{j=1\ldots  k}
 
866
  ~~ (\WTE{\Gamma;\Gamma_I;\Gamma_P}{C_i}{s_{q_i}})_{i=1\ldots  n}
 
867
}
 
868
  {\WF{E;\Ind{\Gamma}{p}{\Gamma_I}{\Gamma_C}}{\Gamma}}}
 
869
provided that the following side conditions hold:
 
870
\begin{itemize}
 
871
\item $k>0$ and all of $I_j$ and $c_i$ are distinct names for $j=1\ldots  k$ and $i=1\ldots  n$,
 
872
\item $p$ is the number of parameters of \NInd{\Gamma}{\Gamma_I}{\Gamma_C}
 
873
  and $\Gamma_P$ is the context of parameters, 
 
874
\item for $j=1\ldots  k$ we have that $A_j$ is an arity of sort $s_j$ and $I_j
 
875
  \notin \Gamma \cup E$,
 
876
\item for $i=1\ldots  n$ we have that $C_i$ is a type of constructor of
 
877
  $I_{q_i}$ which satisfies the positivity condition for $I_1 \ldots  I_k$
 
878
  and $c_i \notin \Gamma \cup E$.
 
879
\end{itemize}
 
880
\end{description}
 
881
One can remark that there is a constraint between the sort of the
 
882
arity of the inductive type and the sort of the type of its
 
883
constructors which will always be satisfied for the impredicative sort
 
884
(\Prop) but may fail to define inductive definition 
 
885
on sort \Set{} and generate constraints between universes for
 
886
inductive definitions in the {\Type} hierarchy.
 
887
 
 
888
\paragraph{Examples.}
 
889
It is well known that existential quantifier can be encoded as an
 
890
inductive definition.
 
891
The following declaration introduces the second-order existential
 
892
quantifier $\exists X.P(X)$.
 
893
\begin{coq_example*}
 
894
Inductive exProp (P:Prop->Prop) : Prop 
 
895
  := exP_intro : forall X:Prop, P X -> exProp P.
 
896
\end{coq_example*}
 
897
The same definition on \Set{} is not allowed and fails~:
 
898
\begin{coq_eval}
 
899
(********** The following is not correct and should produce **********)
 
900
(*** Error: Large non-propositional inductive types must be in Type***)
 
901
\end{coq_eval}
 
902
\begin{coq_example}
 
903
Inductive exSet (P:Set->Prop) : Set 
 
904
  := exS_intro : forall X:Set, P X -> exSet P.
 
905
\end{coq_example}
 
906
It is possible to declare the same inductive definition in the
 
907
universe \Type. 
 
908
The \texttt{exType} inductive definition has type  $(\Type_i \ra\Prop)\ra
 
909
\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$.
 
910
\begin{coq_example*}
 
911
Inductive exType (P:Type->Prop) : Type
 
912
  := exT_intro : forall X:Type, P X -> exType P.
 
913
\end{coq_example*}
 
914
%We shall assume for the following definitions that, if necessary, we
 
915
%annotated the type of constructors such that we know if the argument
 
916
%is recursive or not.  We shall write the type $(x:_R T)C$ if it is 
 
917
%a recursive argument and $(x:_P T)C$ if the argument is not recursive.
 
918
 
 
919
\paragraph[Sort-polymorphism of inductive families.]{Sort-polymorphism of inductive families.\index{Sort-polymorphism of inductive families}}
 
920
 
 
921
From {\Coq} version 8.1, inductive families declared in {\Type} are
 
922
polymorphic over their arguments in {\Type}.
 
923
 
 
924
If $A$ is an arity and $s$ a sort, we write $A_{/s}$ for the arity
 
925
obtained from $A$ by replacing its sort with $s$. Especially, if $A$
 
926
is well-typed in some environment and context, then $A_{/s}$ is typable
 
927
by typability of all products in the Calculus of Inductive Constructions.
 
928
The following typing rule is added to the theory.
 
929
 
 
930
\begin{description}
 
931
\item[Ind-Family] Let $\Ind{\Gamma}{p}{\Gamma_I}{\Gamma_C}$ be an
 
932
  inductive definition. Let $\Gamma_P = [p_1:P_1;\ldots;p_{p}:P_{p}]$
 
933
  be its context of parameters, $\Gamma_I = [I_1:\forall
 
934
    \Gamma_P,A_1;\ldots;I_k:\forall \Gamma_P,A_k]$ its context of
 
935
  definitions and $\Gamma_C = [c_1:\forall
 
936
    \Gamma_P,C_1;\ldots;c_n:\forall \Gamma_P,C_n]$ its context of
 
937
  constructors, with $c_i$ a constructor of $I_{q_i}$.
 
938
 
 
939
  Let $m \leq p$ be the length of the longest prefix of parameters
 
940
  such that the $m$ first arguments of all occurrences of all $I_j$ in
 
941
  all $C_k$ (even the occurrences in the hypotheses of $C_k$) are
 
942
  exactly applied to $p_1~\ldots~p_m$ ($m$ is the number of {\em
 
943
    recursively uniform parameters} and the $p-m$ remaining parameters
 
944
  are the {\em recursively non-uniform parameters}). Let $q_1$,
 
945
  \ldots, $q_r$, with $0\leq r\leq m$, be a (possibly) partial
 
946
  instantiation of the recursively uniform parameters of
 
947
  $\Gamma_P$. We have:
 
948
 
 
949
\inference{\frac
 
950
{\left\{\begin{array}{l}
 
951
\Ind{\Gamma}{p}{\Gamma_I}{\Gamma_C} \in E\\
 
952
(E[\Gamma] \vdash q_l : P'_l)_{l=1\ldots r}\\
 
953
(\WTEGLECONV{P'_l}{\subst{P_l}{p_u}{q_u}_{u=1\ldots l-1}})_{l=1\ldots r}\\
 
954
1 \leq j \leq k
 
955
\end{array}
 
956
\right.}
 
957
{E[\Gamma] \vdash (I_j\,q_1\,\ldots\,q_r:\forall [p_{r+1}:P_{r+1};\ldots;p_{p}:P_{p}], (A_j)_{/s_j})}
 
958
}
 
959
 
 
960
provided that the following side conditions hold:
 
961
 
 
962
\begin{itemize}
 
963
\item $\Gamma_{P'}$ is the context obtained from $\Gamma_P$ by
 
964
replacing each $P_l$ that is an arity with $P'_l$ for $1\leq l \leq r$ (notice that
 
965
$P_l$ arity implies $P'_l$ arity since $\WTEGLECONV{P'_l}{ \subst{P_l}{p_u}{q_u}_{u=1\ldots l-1}}$);
 
966
\item there are sorts $s_i$, for $1 \leq i \leq k$ such that, for
 
967
 $\Gamma_{I'} = [I_1:\forall
 
968
    \Gamma_{P'},(A_1)_{/s_1};\ldots;I_k:\forall \Gamma_{P'},(A_k)_{/s_k}]$
 
969
we have $(\WTE{\Gamma;\Gamma_{I'};\Gamma_{P'}}{C_i}{s_{q_i}})_{i=1\ldots  n}$;
 
970
\item the sorts are such that all eliminations, to {\Prop}, {\Set} and
 
971
  $\Type(j)$, are allowed (see section~\ref{elimdep}).
 
972
\end{itemize}
 
973
\end{description}
 
974
 
 
975
Notice that if $I_j\,q_1\,\ldots\,q_r$ is typable using the rules {\bf
 
976
Ind-Const} and {\bf App}, then it is typable using the rule {\bf
 
977
Ind-Family}. Conversely, the extended theory is not stronger than the
 
978
theory without {\bf Ind-Family}. We get an equiconsistency result by
 
979
mapping each $\Ind{\Gamma}{p}{\Gamma_I}{\Gamma_C}$ occurring into a
 
980
given derivation into as many different inductive types and constructors
 
981
as the number of different (partial) replacements of sorts, needed for
 
982
this derivation, in the parameters that are arities (this is possible
 
983
because $\Ind{\Gamma}{p}{\Gamma_I}{\Gamma_C}$ well-formed implies
 
984
that $\Ind{\Gamma}{p}{\Gamma_{I'}}{\Gamma_{C'}}$ is well-formed and
 
985
has the same allowed eliminations, where
 
986
$\Gamma_{I'}$ is defined as above and $\Gamma_{C'} = [c_1:\forall
 
987
\Gamma_{P'},C_1;\ldots;c_n:\forall \Gamma_{P'},C_n]$). That is,
 
988
the changes in the types of each partial instance
 
989
$q_1\,\ldots\,q_r$ can be characterized by the ordered sets of arity
 
990
sorts among the types of parameters, and to each signature is
 
991
associated a new inductive definition with fresh names. Conversion is
 
992
preserved as any (partial) instance $I_j\,q_1\,\ldots\,q_r$ or
 
993
$C_i\,q_1\,\ldots\,q_r$ is mapped to the names chosen in the specific
 
994
instance of $\Ind{\Gamma}{p}{\Gamma_I}{\Gamma_C}$.
 
995
 
 
996
\newcommand{\Single}{\mbox{\textsf{Set}}}
 
997
 
 
998
In practice, the rule {\bf Ind-Family} is used by {\Coq} only when all the
 
999
inductive types of the inductive definition are declared with an arity whose 
 
1000
sort is in the $\Type$
 
1001
hierarchy. Then, the polymorphism is over the parameters whose
 
1002
type is an arity of sort in the {\Type} hierarchy. 
 
1003
The sort $s_j$ are
 
1004
chosen canonically so that each $s_j$ is minimal with respect to the
 
1005
hierarchy ${\Prop}\subset{\Set_p}\subset\Type$ where $\Set_p$ is
 
1006
predicative {\Set}.
 
1007
%and ${\Prop_u}$ is the sort of small singleton
 
1008
%inductive types (i.e. of inductive types with one single constructor
 
1009
%and that contains either proofs or inhabitants of singleton types
 
1010
%only). 
 
1011
More precisely, an empty or small singleton inductive definition
 
1012
(i.e. an inductive definition of which all inductive types are
 
1013
singleton -- see paragraph~\ref{singleton}) is set in
 
1014
{\Prop}, a small non-singleton inductive family is set in {\Set} (even
 
1015
in case {\Set} is impredicative -- see Section~\ref{impredicativity}),
 
1016
and otherwise in the {\Type} hierarchy.
 
1017
% TODO: clarify the case of a partial application ??
 
1018
 
 
1019
Note that the side-condition about allowed elimination sorts in the
 
1020
rule~{\bf Ind-Family} is just to avoid to recompute the allowed
 
1021
elimination sorts at each instance of a pattern-matching (see
 
1022
section~\ref{elimdep}).
 
1023
 
 
1024
As an example, let us consider the following definition:
 
1025
\begin{coq_example*}
 
1026
Inductive option (A:Type) : Type := 
 
1027
| None : option A 
 
1028
| Some : A -> option A.
 
1029
\end{coq_example*}
 
1030
 
 
1031
As the definition is set in the {\Type} hierarchy, it is used
 
1032
polymorphically over its parameters whose types are arities of a sort
 
1033
in the {\Type} hierarchy. Here, the parameter $A$ has this property,
 
1034
hence, if \texttt{option} is applied to a type in {\Set}, the result is
 
1035
in {\Set}. Note that if \texttt{option} is applied to a type in {\Prop},
 
1036
then, the result is not set in \texttt{Prop} but in \texttt{Set}
 
1037
still. This is because \texttt{option} is not a singleton type (see
 
1038
section~\ref{singleton}) and it would loose the elimination to {\Set} and
 
1039
{\Type} if set in {\Prop}.
 
1040
 
 
1041
\begin{coq_example}
 
1042
Check (fun A:Set => option A).
 
1043
Check (fun A:Prop => option A).
 
1044
\end{coq_example}
 
1045
 
 
1046
Here is another example.
 
1047
 
 
1048
\begin{coq_example*}
 
1049
Inductive prod (A B:Type) : Type := pair : A -> B -> prod A B.
 
1050
\end{coq_example*}
 
1051
 
 
1052
As \texttt{prod} is a singleton type, it will be in {\Prop} if applied
 
1053
twice to propositions, in {\Set} if applied twice to at least one type
 
1054
in {\Set} and none in {\Type}, and in {\Type} otherwise. In all cases,
 
1055
the three kind of eliminations schemes are allowed.
 
1056
 
 
1057
\begin{coq_example}
 
1058
Check (fun A:Set => prod A).
 
1059
Check (fun A:Prop => prod A A).
 
1060
Check (fun (A:Prop) (B:Set) => prod A B).
 
1061
Check (fun (A:Type) (B:Prop) => prod A B).
 
1062
\end{coq_example}
 
1063
 
 
1064
\subsection{Destructors}
 
1065
The specification of inductive definitions with arities and
 
1066
constructors is quite natural.  But we still have to say how to use an
 
1067
object in an inductive type.
 
1068
 
 
1069
This problem is rather delicate. There are actually several different
 
1070
ways to do that. Some of them are logically equivalent but not always
 
1071
equivalent from the computational point of view or from the user point
 
1072
of view.
 
1073
 
 
1074
From the computational point of view, we want to be able to define a
 
1075
function whose domain is an inductively defined type by using a
 
1076
combination of case analysis over the possible constructors of the
 
1077
object and recursion.
 
1078
 
 
1079
Because we need to keep a consistent theory and also we prefer to keep
 
1080
a strongly normalizing reduction, we cannot accept any sort of
 
1081
recursion (even terminating). So the basic idea is to restrict
 
1082
ourselves to primitive recursive functions and functionals.
 
1083
 
 
1084
For instance, assuming a parameter $A:\Set$ exists in the context, we
 
1085
want to build a function \length\ of type $\ListA\ra \nat$ which
 
1086
computes the length of the list, so such that $(\length~(\Nil~A)) = \nO$
 
1087
and $(\length~(\cons~A~a~l)) = (\nS~(\length~l))$.  We want these
 
1088
equalities to be recognized implicitly and taken into account in the
 
1089
conversion rule.
 
1090
 
 
1091
From the logical point of view, we have built a type family by giving
 
1092
a set of constructors.  We want to capture the fact that we do not
 
1093
have any other way to build an object in this type. So when trying to
 
1094
prove a property $(P~m)$ for $m$ in an inductive definition it is
 
1095
enough to enumerate all the cases where $m$ starts with a different
 
1096
constructor.
 
1097
 
 
1098
In case the inductive definition is effectively a recursive one, we
 
1099
want to capture the extra property that we have built the smallest
 
1100
fixed point of this recursive equation.  This says that we are only
 
1101
manipulating finite objects. This analysis provides induction
 
1102
principles.
 
1103
 
 
1104
For instance, in order to prove $\forall l:\ListA,(\LengthA~l~(\length~l))$
 
1105
it is enough to prove:
 
1106
 
 
1107
\noindent $(\LengthA~(\Nil~A)~(\length~(\Nil~A)))$ and
 
1108
 
 
1109
\smallskip
 
1110
$\forall a:A, \forall l:\ListA, (\LengthA~l~(\length~l)) \ra
 
1111
(\LengthA~(\cons~A~a~l)~(\length~(\cons~A~a~l)))$.
 
1112
\smallskip
 
1113
 
 
1114
\noindent which given the conversion equalities satisfied by \length\ is the
 
1115
same as proving:
 
1116
$(\LengthA~(\Nil~A)~\nO)$ and $\forall a:A, \forall l:\ListA, 
 
1117
(\LengthA~l~(\length~l)) \ra
 
1118
(\LengthA~(\cons~A~a~l)~(\nS~(\length~l)))$.
 
1119
 
 
1120
One conceptually simple way to do that, following the basic scheme
 
1121
proposed by Martin-L\"of in his Intuitionistic Type Theory, is to
 
1122
introduce for each inductive definition an elimination operator. At
 
1123
the logical level it is a proof of the usual induction principle and
 
1124
at the computational level it implements a generic operator for doing
 
1125
primitive recursion over the structure.
 
1126
 
 
1127
But this operator is rather tedious to implement and use. We choose in
 
1128
this version of Coq to factorize the operator for primitive recursion
 
1129
into two more primitive operations as was first suggested by Th. Coquand
 
1130
in~\cite{Coq92}.  One is the definition by pattern-matching. The second one is a definition by guarded fixpoints. 
 
1131
 
 
1132
\subsubsection[The {\tt match\ldots with \ldots end} construction.]{The {\tt match\ldots with \ldots end} construction.\label{Caseexpr}
 
1133
\index{match@{\tt match\ldots with\ldots end}}}
 
1134
 
 
1135
The basic idea of this destructor operation is that we have an object
 
1136
$m$ in an inductive type $I$ and we want to prove a property $(P~m)$
 
1137
which in general depends on $m$. For this, it is enough to prove the
 
1138
property for $m = (c_i~u_1\ldots  u_{p_i})$ for each constructor of $I$.
 
1139
 
 
1140
The \Coq{} term for this proof will be written~:
 
1141
\[\kw{match}~m~\kw{with}~ (c_1~x_{11}~...~x_{1p_1}) \Ra f_1 ~|~\ldots~|~
 
1142
  (c_n~x_{n1}...x_{np_n}) \Ra f_n~ \kw{end}\]
 
1143
In this expression, if
 
1144
$m$ is a term built from a constructor $(c_i~u_1\ldots u_{p_i})$ then
 
1145
the expression will behave as it is specified with $i$-th branch and
 
1146
will reduce to $f_i$ where the $x_{i1}$\ldots $x_{ip_i}$ are replaced
 
1147
by the $u_1\ldots u_p$ according to the $\iota$-reduction.
 
1148
 
 
1149
Actually, for type-checking a \kw{match\ldots with\ldots end}
 
1150
expression we also need to know the predicate $P$ to be proved by case
 
1151
analysis. In the general case where $I$ is an inductively defined
 
1152
$n$-ary relation, $P$ is a $n+1$-ary relation: the $n$ first arguments
 
1153
correspond to the arguments of $I$ (parameters excluded), and the last
 
1154
one corresponds to object $m$. \Coq{} can sometimes infer this
 
1155
predicate but sometimes not. The concrete syntax for describing this
 
1156
predicate uses the \kw{as\ldots in\ldots return} construction. For
 
1157
instance, let us assume that $I$ is an unary predicate with one
 
1158
parameter. The predicate is made explicit using the syntax~:
 
1159
\[\kw{match}~m~\kw{as}~ x~ \kw{in}~ I~\verb!_!~a~ \kw{return}~ (P~ x)
 
1160
 ~\kw{with}~ (c_1~x_{11}~...~x_{1p_1}) \Ra f_1 ~|~\ldots~|~
 
1161
  (c_n~x_{n1}...x_{np_n}) \Ra f_n \kw{end}\]
 
1162
The \kw{as} part can be omitted if either the result type does not
 
1163
depend on $m$ (non-dependent elimination) or $m$ is a variable (in
 
1164
this case, the result type can depend on $m$). The \kw{in} part can be
 
1165
omitted if the result type does not depend on the arguments of
 
1166
$I$. Note that the arguments of $I$ corresponding to parameters
 
1167
\emph{must} be \verb!_!, because the result type is not generalized to
 
1168
all possible values of the parameters. The expression after \kw{in}
 
1169
must be seen as an \emph{inductive type pattern}. As a final remark,
 
1170
expansion of implicit arguments and notations apply to this pattern.
 
1171
 
 
1172
For the purpose of presenting the inference rules, we use a more
 
1173
compact notation~:
 
1174
\[ \Case{(\lb a x \mto P)}{m}{ \lb x_{11}~...~x_{1p_1} \mto f_1 ~|~\ldots~|~
 
1175
  \lb x_{n1}...x_{np_n} \mto f_n}\]
 
1176
 
 
1177
%% CP 06/06 Obsolete avec la nouvelle syntaxe et incompatible avec la
 
1178
%% presentation theorique qui suit
 
1179
% \paragraph{Non-dependent elimination.}
 
1180
%
 
1181
% When defining a function of codomain $C$ by case analysis over an
 
1182
% object in an inductive type $I$, we build an object of type $I
 
1183
% \ra C$. The minimality principle on an inductively defined logical
 
1184
% predicate $I$ of type $A \ra \Prop$ is often used to prove a property
 
1185
% $\forall x:A,(I~x)\ra (C~x)$.  These are particular cases of the dependent
 
1186
% principle that we stated before with a predicate which does not depend
 
1187
% explicitly on the object in the inductive definition.
 
1188
 
 
1189
% For instance, a function testing whether a list is empty 
 
1190
% can be
 
1191
% defined as:
 
1192
% \[\kw{fun} l:\ListA \Ra \kw{match}~l~\kw{with}~ \Nil \Ra \true~
 
1193
% |~(\cons~a~m) \Ra \false \kw{end}\]
 
1194
% represented by
 
1195
% \[\lb~l:\ListA \mto\Case{\bool}{l}{\true~ |~ \lb a~m,~\false}\]
 
1196
%\noindent {\bf Remark. } 
 
1197
 
 
1198
% In the system \Coq\ the expression above, can be
 
1199
% written without mentioning
 
1200
% the dummy abstraction:
 
1201
% \Case{\bool}{l}{\Nil~ \mbox{\tt =>}~\true~ |~ (\cons~a~m)~
 
1202
%  \mbox{\tt =>}~ \false}
 
1203
 
 
1204
\paragraph[Allowed elimination sorts.]{Allowed elimination sorts.\index{Elimination sorts}}
 
1205
 
 
1206
An important question for building the typing rule for \kw{match} is
 
1207
what can be the type of $P$ with respect to the type of the inductive
 
1208
definitions.
 
1209
 
 
1210
We define now a relation \compat{I:A}{B} between an inductive
 
1211
definition $I$ of type $A$ and an arity $B$. This relation states that
 
1212
an object in the inductive definition $I$ can be eliminated for
 
1213
proving a property $P$ of type $B$.
 
1214
 
 
1215
The case of inductive definitions in sorts \Set\ or \Type{} is simple.
 
1216
There is no restriction on the sort of the predicate to be
 
1217
eliminated. 
 
1218
 
 
1219
\paragraph{Notations.}
 
1220
The \compat{I:A}{B} is defined as the smallest relation satisfying the
 
1221
following rules:
 
1222
We write \compat{I}{B} for \compat{I:A}{B} where $A$ is the type of
 
1223
$I$.
 
1224
 
 
1225
\begin{description}
 
1226
\item[Prod] \inference{\frac{\compat{(I~x):A'}{B'}}
 
1227
                      {\compat{I:(x:A)A'}{(x:A)B'}}}
 
1228
\item[\Set \& \Type] \inference{\frac{
 
1229
    s_1 \in \{\Set,\Type(j)\}, 
 
1230
    s_2 \in \Sort}{\compat{I:s_1}{I\ra s_2}}}
 
1231
\end{description}
 
1232
 
 
1233
The case of Inductive definitions of sort \Prop{} is a bit more
 
1234
complicated, because of our interpretation of this sort. The only
 
1235
harmless allowed elimination, is the one when predicate $P$ is also of
 
1236
sort \Prop.
 
1237
\begin{description}
 
1238
\item[\Prop] \inference{\compat{I:\Prop}{I\ra\Prop}}
 
1239
\end{description}
 
1240
\Prop{} is the type of logical propositions, the proofs of properties
 
1241
$P$ in \Prop{} could not be used for computation and are consequently
 
1242
ignored by the extraction mechanism.
 
1243
Assume $A$ and $B$ are two propositions, and the logical disjunction
 
1244
$A\vee B$ is defined inductively by~:
 
1245
\begin{coq_example*}
 
1246
Inductive or (A B:Prop) : Prop :=
 
1247
  lintro : A -> or A B | rintro : B -> or A B.
 
1248
\end{coq_example*}
 
1249
The following definition which computes a boolean value by case over
 
1250
the proof of \texttt{or A B} is not accepted~:
 
1251
\begin{coq_eval}
 
1252
(***************************************************************)
 
1253
(*** This example should fail with ``Incorrect elimination'' ***)
 
1254
\end{coq_eval}
 
1255
\begin{coq_example}
 
1256
Definition choice (A B: Prop) (x:or A B) := 
 
1257
   match x with lintro a => true | rintro b => false end.
 
1258
\end{coq_example}
 
1259
From the computational point of view, the structure of the proof of
 
1260
\texttt{(or A B)} in this term is needed for computing the boolean
 
1261
value.
 
1262
 
 
1263
In general, if $I$ has type \Prop\ then $P$ cannot have type $I\ra
 
1264
\Set$, because it will mean to build an informative proof of type
 
1265
$(P~m)$ doing a case analysis over a non-computational object that
 
1266
will disappear in the extracted program.  But the other way is safe
 
1267
with respect to our interpretation we can have $I$ a computational
 
1268
object and $P$ a non-computational one, it just corresponds to proving
 
1269
a logical property of a computational object.
 
1270
 
 
1271
% Also if $I$ is in one of the sorts \{\Prop, \Set\}, one cannot in
 
1272
% general allow an elimination over a bigger sort such as \Type.  But
 
1273
% this operation is safe whenever $I$ is a {\em small inductive} type,
 
1274
% which means that all the types of constructors of
 
1275
% $I$ are small with the following definition:\\
 
1276
% $(I~t_1\ldots t_s)$ is a {\em small type of constructor} and
 
1277
% $\forall~x:T,C$ is a small type of constructor if $C$ is and if $T$
 
1278
% has type \Prop\ or \Set.  \index{Small inductive type}
 
1279
 
 
1280
% We call this particular elimination which gives the possibility to
 
1281
% compute a type by induction on the structure of a term, a {\em strong
 
1282
%   elimination}\index{Strong elimination}.
 
1283
 
 
1284
In the same spirit, elimination on $P$ of type $I\ra
 
1285
\Type$ cannot be allowed because it trivially implies the elimination
 
1286
on $P$ of type $I\ra \Set$ by cumulativity. It also implies that there
 
1287
is two proofs of the same property which are provably different,
 
1288
contradicting the proof-irrelevance property which is sometimes a
 
1289
useful axiom~:
 
1290
\begin{coq_example}
 
1291
Axiom proof_irrelevance : forall (P : Prop) (x y : P), x=y.
 
1292
\end{coq_example}
 
1293
\begin{coq_eval}
 
1294
Reset proof_irrelevance.
 
1295
\end{coq_eval}
 
1296
The elimination of an inductive definition of type \Prop\ on a
 
1297
predicate $P$ of type $I\ra \Type$ leads to a paradox when applied to 
 
1298
impredicative inductive definition like the second-order existential
 
1299
quantifier \texttt{exProp} defined above, because it give access to
 
1300
the two projections on this type.
 
1301
 
 
1302
%\paragraph{Warning: strong elimination}
 
1303
%\index{Elimination!Strong elimination}
 
1304
%In previous versions of Coq, for a small inductive definition, only the
 
1305
%non-informative strong elimination on \Type\ was allowed, because
 
1306
%strong elimination on \Typeset\ was not compatible with the current
 
1307
%extraction procedure. In this version, strong elimination on \Typeset\
 
1308
%is accepted but a dummy element is extracted from it and may generate
 
1309
%problems if extracted terms are explicitly used such as in the 
 
1310
%{\tt Program} tactic or when extracting ML programs.
 
1311
 
 
1312
\paragraph[Empty and singleton elimination]{Empty and singleton elimination\label{singleton}
 
1313
\index{Elimination!Singleton elimination}
 
1314
\index{Elimination!Empty elimination}}
 
1315
 
 
1316
There are special inductive definitions in \Prop\ for which more
 
1317
eliminations are allowed. 
 
1318
\begin{description}
 
1319
\item[\Prop-extended] 
 
1320
\inference{
 
1321
   \frac{I \mbox{~is an empty or singleton
 
1322
       definition}~~~s \in \Sort}{\compat{I:\Prop}{I\ra s}}
 
1323
}
 
1324
\end{description}
 
1325
 
 
1326
% A {\em singleton definition} has always an informative content,
 
1327
% even if it is a proposition.
 
1328
 
 
1329
A {\em singleton
 
1330
definition} has only one constructor and all the arguments of this
 
1331
constructor have type \Prop. In that case, there is a canonical
 
1332
way to interpret the informative extraction on an object in that type,
 
1333
such that the elimination on any sort $s$ is legal.  Typical examples are
 
1334
the conjunction of non-informative propositions and the equality. 
 
1335
If there is an hypothesis $h:a=b$ in the context, it can be used for
 
1336
rewriting not only in logical propositions but also in any type.
 
1337
% In that case, the term \verb!eq_rec! which was defined as an axiom, is
 
1338
% now a term of the calculus.
 
1339
\begin{coq_example}
 
1340
Print eq_rec.
 
1341
Extraction eq_rec.
 
1342
\end{coq_example}
 
1343
An empty definition has no constructors, in that case also,
 
1344
elimination on any sort is allowed.
 
1345
 
 
1346
\paragraph{Type of branches.}
 
1347
Let $c$ be a term of type $C$, we assume $C$ is a type of constructor
 
1348
for an inductive definition $I$. Let $P$ be a term that represents the
 
1349
property to be proved.
 
1350
We assume $r$ is the number of parameters.
 
1351
 
 
1352
We define a new type \CI{c:C}{P} which represents the type of the
 
1353
branch corresponding to the $c:C$ constructor.
 
1354
\[
 
1355
\begin{array}{ll}
 
1356
\CI{c:(I_i~p_1\ldots p_r\ t_1 \ldots t_p)}{P} &\equiv (P~t_1\ldots ~t_p~c) \\[2mm]
 
1357
\CI{c:\forall~x:T,C}{P} &\equiv \forall~x:T,\CI{(c~x):C}{P} 
 
1358
\end{array}
 
1359
\]
 
1360
We write \CI{c}{P} for \CI{c:C}{P} with $C$ the type of $c$.
 
1361
 
 
1362
\paragraph{Examples.}
 
1363
For $\ListA$ the type of $P$ will be $\ListA\ra s$ for $s \in \Sort$. \\ 
 
1364
$ \CI{(\cons~A)}{P} \equiv
 
1365
\forall a:A, \forall l:\ListA,(P~(\cons~A~a~l))$.
 
1366
 
 
1367
For $\LengthA$, the type of $P$ will be
 
1368
$\forall l:\ListA,\forall n:\nat, (\LengthA~l~n)\ra \Prop$ and the expression
 
1369
\CI{(\LCons~A)}{P} is defined as:\\ 
 
1370
$\forall a:A, \forall l:\ListA, \forall n:\nat, \forall
 
1371
h:(\LengthA~l~n), (P~(\cons~A~a~l)~(\nS~n)~(\LCons~A~a~l~n~l))$.\\ 
 
1372
If $P$ does not depend on its third argument, we find the more natural
 
1373
expression:\\ 
 
1374
$\forall a:A, \forall l:\ListA, \forall n:\nat,
 
1375
(\LengthA~l~n)\ra(P~(\cons~A~a~l)~(\nS~n))$.
 
1376
 
 
1377
\paragraph{Typing rule.}
 
1378
 
 
1379
Our very general destructor for inductive definition enjoys the
 
1380
following typing rule
 
1381
% , where we write 
 
1382
% \[
 
1383
% \Case{P}{c}{[x_{11}:T_{11}]\ldots[x_{1p_1}:T_{1p_1}]g_1\ldots
 
1384
%   [x_{n1}:T_{n1}]\ldots[x_{np_n}:T_{np_n}]g_n}
 
1385
% \]
 
1386
% for 
 
1387
% \[
 
1388
% \Case{P}{c}{(c_1~x_{11}~...~x_{1p_1}) \Ra g_1 ~|~\ldots~|~
 
1389
% (c_n~x_{n1}...x_{np_n}) \Ra g_n }
 
1390
% \]
 
1391
 
 
1392
\begin{description}
 
1393
\item[match] \label{elimdep} \index{Typing rules!match}
 
1394
\inference{
 
1395
\frac{\WTEG{c}{(I~q_1\ldots  q_r~t_1\ldots  t_s)}~~
 
1396
  \WTEG{P}{B}~~\compat{(I~q_1\ldots  q_r)}{B}
 
1397
 ~~
 
1398
(\WTEG{f_i}{\CI{(c_{p_i}~q_1\ldots  q_r)}{P}})_{i=1\ldots  l}}
 
1399
{\WTEG{\Case{P}{c}{f_1|\ldots  |f_l}}{(P\ t_1\ldots  t_s\ c)}}}%\\[3mm]
 
1400
 
 
1401
provided $I$ is an inductive type in a declaration
 
1402
\Ind{\Delta}{r}{\Gamma_I}{\Gamma_C} with 
 
1403
$\Gamma_C = [c_1:C_1;\ldots;c_n:C_n]$ and $c_{p_1}\ldots  c_{p_l}$ are the
 
1404
only constructors of $I$.
 
1405
\end{description}
 
1406
 
 
1407
\paragraph{Example.}
 
1408
For \List\ and \Length\ the typing rules for the {\tt match} expression
 
1409
are (writing just $t:M$ instead of \WTEG{t}{M}, the environment and
 
1410
context being the same in all the judgments).
 
1411
 
 
1412
\[\frac{l:\ListA~~P:\ListA\ra s~~~f_1:(P~(\Nil~A))~~
 
1413
      f_2:\forall a:A, \forall l:\ListA, (P~(\cons~A~a~l))}
 
1414
    {\Case{P}{l}{f_1~|~f_2}:(P~l)}\]
 
1415
 
 
1416
\[\frac{
 
1417
     \begin{array}[b]{@{}c@{}} 
 
1418
H:(\LengthA~L~N) \\ P:\forall l:\ListA, \forall n:\nat, (\LengthA~l~n)\ra
 
1419
  \Prop\\
 
1420
 f_1:(P~(\Nil~A)~\nO~\LNil) \\
 
1421
   f_2:\forall a:A, \forall l:\ListA, \forall n:\nat, \forall
 
1422
  h:(\LengthA~l~n), (P~(\cons~A~a~n)~(\nS~n)~(\LCons~A~a~l~n~h)) 
 
1423
      \end{array}}
 
1424
    {\Case{P}{H}{f_1~|~f_2}:(P~L~N~H)}\]
 
1425
 
 
1426
\paragraph[Definition of $\iota$-reduction.]{Definition of $\iota$-reduction.\label{iotared}
 
1427
\index{iota-reduction@$\iota$-reduction}}
 
1428
We still have to define the $\iota$-reduction in the general case.
 
1429
 
 
1430
A $\iota$-redex is a term of the following form:
 
1431
\[\Case{P}{(c_{p_i}~q_1\ldots  q_r~a_1\ldots  a_m)}{f_1|\ldots |
 
1432
    f_l}\]
 
1433
with $c_{p_i}$ the $i$-th constructor of the inductive type $I$ with $r$
 
1434
parameters.
 
1435
 
 
1436
The $\iota$-contraction of this term is $(f_i~a_1\ldots a_m)$ leading
 
1437
to the general reduction rule:
 
1438
\[ \Case{P}{(c_{p_i}~q_1\ldots  q_r~a_1\ldots  a_m)}{f_1|\ldots |
 
1439
    f_n} \triangleright_{\iota} (f_i~a_1\ldots a_m) \]
 
1440
 
 
1441
\subsection[Fixpoint definitions]{Fixpoint definitions\label{Fix-term} \index{Fix@{\tt Fix}}}
 
1442
The second operator for elimination is fixpoint definition. 
 
1443
This fixpoint may involve several mutually recursive definitions.
 
1444
The basic concrete syntax for a recursive set of mutually recursive 
 
1445
declarations is (with $\Gamma_i$ contexts)~: 
 
1446
\[\kw{fix}~f_1 (\Gamma_1) :A_1:=t_1~\kw{with} \ldots \kw{with}~ f_n
 
1447
(\Gamma_n) :A_n:=t_n\]
 
1448
The terms are obtained by projections from this set of declarations
 
1449
and are written 
 
1450
\[\kw{fix}~f_1 (\Gamma_1) :A_1:=t_1~\kw{with} \ldots \kw{with}~ f_n
 
1451
(\Gamma_n) :A_n:=t_n~\kw{for}~f_i\]
 
1452
In the inference rules, we represent such a
 
1453
term by 
 
1454
\[\Fix{f_i}{f_1:A_1':=t_1' \ldots f_n:A_n':=t_n'}\]
 
1455
with $t_i'$ (resp. $A_i'$) representing the term $t_i$ abstracted
 
1456
(resp. generalized) with
 
1457
respect to the bindings in the context $\Gamma_i$, namely
 
1458
$t_i'=\lb \Gamma_i \mto t_i$ and $A_i'=\forall \Gamma_i, A_i$.
 
1459
 
 
1460
\subsubsection{Typing rule}
 
1461
The typing rule is the expected one for a fixpoint.
 
1462
 
 
1463
\begin{description}
 
1464
\item[Fix] \index{Typing rules!Fix}
 
1465
\inference{\frac{(\WTEG{A_i}{s_i})_{i=1\ldots n}~~~~
 
1466
            (\WTE{\Gamma,f_1:A_1,\ldots,f_n:A_n}{t_i}{A_i})_{i=1\ldots n}}
 
1467
         {\WTEG{\Fix{f_i}{f_1:A_1:=t_1 \ldots f_n:A_n:=t_n}}{A_i}}}
 
1468
\end{description}
 
1469
 
 
1470
Any fixpoint definition cannot be accepted because non-normalizing terms
 
1471
will lead to proofs of absurdity.
 
1472
 
 
1473
The basic scheme of recursion that should be allowed is the one needed for 
 
1474
defining primitive
 
1475
recursive functionals. In that case the fixpoint enjoys a special
 
1476
syntactic restriction, namely one of the arguments belongs to an
 
1477
inductive type, the function starts with a case analysis and recursive
 
1478
calls are done on variables coming from patterns and representing subterms.
 
1479
 
 
1480
For instance in the case of natural numbers, a proof of the induction
 
1481
principle of type 
 
1482
\[\forall P:\nat\ra\Prop, (P~\nO)\ra(\forall n:\nat, (P~n)\ra(P~(\nS~n)))\ra
 
1483
\forall n:\nat, (P~n)\]
 
1484
can be represented by the term:
 
1485
\[\begin{array}{l}
 
1486
\lb P:\nat\ra\Prop\mto\lb f:(P~\nO)\mto \lb g:(\forall n:\nat,
 
1487
(P~n)\ra(P~(\nS~n))) \mto\\
 
1488
\Fix{h}{h:\forall n:\nat, (P~n):=\lb n:\nat\mto \Case{P}{n}{f~|~\lb
 
1489
    p:\nat\mto (g~p~(h~p))}}
 
1490
\end{array}
 
1491
\]
 
1492
 
 
1493
Before accepting a fixpoint definition as being correctly typed, we
 
1494
check that the definition is ``guarded''. A precise analysis of this
 
1495
notion can be found in~\cite{Gim94}.
 
1496
 
 
1497
The first stage is to precise on which argument the fixpoint will be
 
1498
decreasing. The type of this argument should be an inductive
 
1499
definition.
 
1500
 
 
1501
For doing this the syntax of fixpoints is extended and becomes 
 
1502
 \[\Fix{f_i}{f_1/k_1:A_1:=t_1 \ldots f_n/k_n:A_n:=t_n}\]
 
1503
where $k_i$ are positive integers.
 
1504
Each $A_i$ should be a type (reducible to a term) starting with at least
 
1505
$k_i$ products $\forall y_1:B_1,\ldots \forall y_{k_i}:B_{k_i}, A'_i$ 
 
1506
and $B_{k_i}$
 
1507
being an instance of an inductive definition.
 
1508
 
 
1509
Now in the definition $t_i$, if $f_j$ occurs then it should be applied
 
1510
to at least $k_j$ arguments and the $k_j$-th argument should be
 
1511
syntactically recognized as structurally smaller than $y_{k_i}$
 
1512
 
 
1513
 
 
1514
The definition of being structurally smaller is a bit technical.
 
1515
One needs first to  define the notion of 
 
1516
{\em recursive arguments of a constructor}\index{Recursive arguments}.
 
1517
For an inductive definition \Ind{\Gamma}{r}{\Gamma_I}{\Gamma_C},
 
1518
the type of a constructor $c$ has the form
 
1519
$\forall p_1:P_1,\ldots \forall p_r:P_r, 
 
1520
\forall x_1:T_1, \ldots \forall x_r:T_r, (I_j~p_1\ldots 
 
1521
p_r~t_1\ldots  t_s)$ the recursive arguments will correspond to $T_i$ in
 
1522
which one of the $I_l$ occurs.
 
1523
 
 
1524
 
 
1525
The main rules for being structurally smaller are the following:\\
 
1526
Given a variable $y$ of type an inductive
 
1527
definition in a declaration 
 
1528
\Ind{\Gamma}{r}{\Gamma_I}{\Gamma_C}
 
1529
where $\Gamma_I$ is $[I_1:A_1;\ldots;I_k:A_k]$, and $\Gamma_C$ is
 
1530
  $[c_1:C_1;\ldots;c_n:C_n]$.
 
1531
The terms structurally smaller than $y$ are:
 
1532
\begin{itemize}
 
1533
\item $(t~u), \lb x:u \mto t$ when $t$ is structurally smaller than $y$ .
 
1534
\item \Case{P}{c}{f_1\ldots f_n} when each $f_i$ is structurally
 
1535
  smaller than $y$. \\
 
1536
  If $c$ is $y$ or is structurally smaller than $y$, its type is an inductive
 
1537
  definition $I_p$ part of the inductive
 
1538
  declaration corresponding to $y$. 
 
1539
  Each $f_i$ corresponds to a type of constructor $C_q \equiv
 
1540
  \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)$ 
 
1541
  and can consequently be
 
1542
  written $\lb y_1:B'_1\mto \ldots \lb y_k:B'_k\mto g_i$.
 
1543
  ($B'_i$ is obtained from $B_i$ by substituting parameters variables)
 
1544
  the variables $y_j$ occurring
 
1545
  in $g_i$ corresponding to recursive arguments $B_i$ (the ones in
 
1546
  which one of the $I_l$ occurs) are structurally smaller than $y$.
 
1547
\end{itemize}
 
1548
The following definitions are correct, we enter them using the
 
1549
{\tt Fixpoint} command as described in Section~\ref{Fixpoint} and show
 
1550
the internal representation.
 
1551
\begin{coq_example}
 
1552
Fixpoint plus (n m:nat) {struct n} : nat :=
 
1553
  match n with
 
1554
  | O => m
 
1555
  | S p => S (plus p m)
 
1556
  end.
 
1557
Print plus.
 
1558
Fixpoint lgth (A:Set) (l:list A) {struct l} : nat :=
 
1559
  match l with
 
1560
  | nil => O
 
1561
  | cons a l' => S (lgth A l')
 
1562
  end.
 
1563
Print lgth.
 
1564
Fixpoint sizet (t:tree) : nat := let (f) := t in S (sizef f)
 
1565
 with sizef (f:forest) : nat :=
 
1566
  match f with
 
1567
  | emptyf => O
 
1568
  | consf t f => plus (sizet t) (sizef f)
 
1569
  end.
 
1570
Print sizet.
 
1571
\end{coq_example}
 
1572
 
 
1573
 
 
1574
\subsubsection[Reduction rule]{Reduction rule\index{iota-reduction@$\iota$-reduction}}
 
1575
Let $F$ be the set of declarations: $f_1/k_1:A_1:=t_1 \ldots
 
1576
f_n/k_n:A_n:=t_n$.
 
1577
The reduction for fixpoints is:
 
1578
\[ (\Fix{f_i}{F}~a_1\ldots
 
1579
a_{k_i}) \triangleright_{\iota} \substs{t_i}{f_k}{\Fix{f_k}{F}}{k=1\ldots n}
 
1580
~a_1\ldots a_{k_i}\]
 
1581
when $a_{k_i}$ starts with a constructor.
 
1582
This last restriction is needed in order to keep strong normalization
 
1583
and corresponds to the reduction for primitive recursive operators.
 
1584
 
 
1585
We can illustrate this behavior on examples.
 
1586
\begin{coq_example}
 
1587
Goal forall n m:nat, plus (S n) m = S (plus n m).
 
1588
reflexivity.
 
1589
Abort.
 
1590
Goal forall f:forest, sizet (node f) = S (sizef f).
 
1591
reflexivity.
 
1592
Abort.
 
1593
\end{coq_example}
 
1594
But assuming the definition of a son function from \tree\ to \forest:
 
1595
\begin{coq_example}
 
1596
Definition sont (t:tree) : forest 
 
1597
   := let (f) := t in f.
 
1598
\end{coq_example}
 
1599
The following is not a conversion but can be proved after a case analysis.
 
1600
\begin{coq_eval}
 
1601
(******************************************************************)
 
1602
(** Error: Impossible to unify ....                              **)
 
1603
\end{coq_eval}
 
1604
\begin{coq_example}
 
1605
Goal forall t:tree, sizet t = S (sizef (sont t)).
 
1606
reflexivity. (** this one fails **)
 
1607
destruct t.
 
1608
reflexivity.
 
1609
\end{coq_example}
 
1610
\begin{coq_eval}
 
1611
Abort.
 
1612
\end{coq_eval}
 
1613
 
 
1614
% La disparition de Program devrait rendre la construction Match obsolete
 
1615
% \subsubsection{The {\tt Match \ldots with \ldots end} expression}
 
1616
% \label{Matchexpr}
 
1617
% %\paragraph{A unary {\tt Match\ldots with \ldots end}.}
 
1618
% \index{Match...with...end@{\tt Match \ldots with \ldots end}}
 
1619
% The {\tt Match} operator which was a primitive notion in older
 
1620
% presentations of the Calculus of Inductive Constructions is now just a
 
1621
% macro definition which generates the good combination of {\tt Case}
 
1622
% and {\tt Fix} operators in order to generate an operator for primitive
 
1623
% recursive definitions. It always considers an inductive definition as 
 
1624
% a single inductive definition.
 
1625
 
 
1626
% The following examples illustrates this feature.
 
1627
% \begin{coq_example}
 
1628
% Definition nat_pr : (C:Set)C->(nat->C->C)->nat->C 
 
1629
%   :=[C,x,g,n]Match n with x g end.
 
1630
% Print nat_pr.
 
1631
% \end{coq_example}
 
1632
% \begin{coq_example}
 
1633
% Definition forest_pr 
 
1634
%   : (C:Set)C->(tree->forest->C->C)->forest->C
 
1635
%   := [C,x,g,n]Match n with x g end.
 
1636
% \end{coq_example}
 
1637
 
 
1638
% Cet exemple faisait error (HH le 12/12/96), j'ai change pour une
 
1639
% version plus simple
 
1640
%\begin{coq_example}
 
1641
%Definition forest_pr 
 
1642
%  : (P:forest->Set)(P emptyf)->((t:tree)(f:forest)(P f)->(P (consf t f)))
 
1643
%    ->(f:forest)(P f)
 
1644
%  := [C,x,g,n]Match n with x g end.
 
1645
%\end{coq_example}
 
1646
 
 
1647
\subsubsection{Mutual induction}
 
1648
 
 
1649
The principles of mutual induction can be automatically generated 
 
1650
using the {\tt Scheme} command described in Section~\ref{Scheme}.
 
1651
 
 
1652
\section{Coinductive types}
 
1653
The implementation contains also coinductive definitions, which are
 
1654
types inhabited by infinite objects. 
 
1655
More information on coinductive definitions can be found
 
1656
in~\cite{Gimenez95b,Gim98,GimCas05}.
 
1657
%They are described in Chapter~\ref{Coinductives}.
 
1658
 
 
1659
\section[\iCIC : the Calculus of Inductive Construction with
 
1660
  impredicative \Set]{\iCIC : the Calculus of Inductive Construction with
 
1661
  impredicative \Set\label{impredicativity}}
 
1662
 
 
1663
\Coq{} can be used as a type-checker for \iCIC{}, the original 
 
1664
Calculus of Inductive Constructions with an impredicative sort \Set{}
 
1665
by using the compiler option \texttt{-impredicative-set}.
 
1666
 
 
1667
For example, using the ordinary \texttt{coqtop} command, the following
 
1668
is rejected.
 
1669
\begin{coq_eval}
 
1670
(** This example should fail *******************************
 
1671
    Error: The term forall X:Set, X -> X has type Type
 
1672
    while it is expected to have type Set
 
1673
***)
 
1674
\end{coq_eval}
 
1675
\begin{coq_example}
 
1676
Definition id: Set := forall X:Set,X->X.
 
1677
\end{coq_example}
 
1678
while it will type-check, if one use instead the \texttt{coqtop
 
1679
  -impredicative-set} command.
 
1680
 
 
1681
The major change in the theory concerns the rule for product formation
 
1682
in the sort \Set, which is extended to a domain in any sort~:
 
1683
\begin{description}
 
1684
\item [Prod]  \index{Typing rules!Prod (impredicative Set)}
 
1685
\inference{\frac{\WTEG{T}{s}~~~~s \in \Sort~~~~~~
 
1686
    \WTE{\Gamma::(x:T)}{U}{\Set}}
 
1687
      { \WTEG{\forall~x:T,U}{\Set}}} 
 
1688
\end{description}
 
1689
This extension has consequences on the inductive definitions which are
 
1690
allowed. 
 
1691
In the impredicative system, one can build so-called {\em large inductive
 
1692
  definitions} like the example of second-order existential
 
1693
quantifier (\texttt{exSet}).
 
1694
 
 
1695
There should be restrictions on the eliminations which can be
 
1696
performed on such definitions. The eliminations rules in the
 
1697
impredicative system for sort \Set{} become~:
 
1698
\begin{description}
 
1699
\item[\Set] \inference{\frac{s \in
 
1700
      \{\Prop, \Set\}}{\compat{I:\Set}{I\ra s}}
 
1701
~~~~\frac{I \mbox{~is a small inductive definition}~~~~s \in
 
1702
      \{\Type(i)\}}
 
1703
         {\compat{I:\Set}{I\ra s}}}
 
1704
\end{description}
 
1705
     
 
1706
 
 
1707
 
 
1708
% $Id: RefMan-cic.tex 11643 2008-11-28 18:12:34Z letouzey $ 
 
1709
 
 
1710
%%% Local Variables: 
 
1711
%%% mode: latex
 
1712
%%% TeX-master: "Reference-Manual"
 
1713
%%% End: 
 
1714
 
 
1715