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

« back to all changes in this revision

Viewing changes to refman/Cases.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
 
\achapter{Extended pattern-matching}\defaultheaders
2
 
\aauthor{Cristina Cornes and Hugo Herbelin}
3
 
 
4
 
\label{Mult-match-full}
5
 
\ttindex{Cases}
6
 
\index{ML-like patterns}
7
 
 
8
 
This section describes the full form of pattern-matching in {\Coq} terms.
9
 
 
10
 
\asection{Patterns}\label{implementation} The full syntax of {\tt
11
 
match} is presented in figures~\ref{term-syntax}
12
 
and~\ref{term-syntax-aux}.  Identifiers in patterns are either
13
 
constructor names or variables. Any identifier that is not the
14
 
constructor of an inductive or coinductive type is considered to be a
15
 
variable. A variable name cannot occur more than once in a given
16
 
pattern. It is recommended to start variable names by a lowercase
17
 
letter.
18
 
 
19
 
If a pattern has the form $(c~\vec{x})$ where $c$ is a constructor
20
 
symbol and $\vec{x}$ is a linear vector of (distinct) variables, it is
21
 
called {\em simple}: it is the kind of pattern recognized by the basic
22
 
version of {\tt match}. On the opposite, if it is a variable $x$ or
23
 
has the form $(c~\vec{p})$ with $p$ not only made of variables, the
24
 
pattern is called {\em nested}.
25
 
 
26
 
A variable pattern matches any value, and the identifier is bound to
27
 
that value. The pattern ``\texttt{\_}'' (called ``don't care'' or
28
 
``wildcard'' symbol) also matches any value, but does not bind
29
 
anything. It may occur an arbitrary number of times in a
30
 
pattern. Alias patterns written \texttt{(}{\sl pattern} \texttt{as}
31
 
{\sl identifier}\texttt{)} are also accepted. This pattern matches the
32
 
same values as {\sl pattern} does and {\sl identifier} is bound to the
33
 
matched value.  
34
 
A pattern of the form {\pattern}{\tt |}{\pattern} is called
35
 
disjunctive. A list of patterns separated with commas is also
36
 
considered as a pattern and is called {\em multiple pattern}. However
37
 
multiple patterns can only occur at the root of pattern-matching
38
 
equations. Disjunctions of {\em multiple pattern} are allowed though.
39
 
 
40
 
Since extended {\tt match} expressions are compiled into the primitive
41
 
ones, the expressiveness of the theory remains the same. Once the
42
 
stage of parsing has finished only simple patterns remain.  Re-nesting
43
 
of pattern is performed at printing time. An easy way to see the
44
 
result of the expansion is to toggle off the nesting performed at
45
 
printing (use here {\tt Set Printing Matching}), then by printing the term
46
 
with \texttt{Print} if the term is a constant, or using the command
47
 
\texttt{Check}.
48
 
 
49
 
The extended \texttt{match} still accepts an optional {\em elimination
50
 
predicate} given after the keyword \texttt{return}.  Given a pattern
51
 
matching expression, if all the right-hand-sides of \texttt{=>} ({\em
52
 
rhs} in short) have the same type, then this type can be sometimes
53
 
synthesized, and so we can omit the \texttt{return} part. Otherwise 
54
 
the predicate after \texttt{return} has to be provided, like for the basic
55
 
\texttt{match}.
56
 
 
57
 
Let us illustrate through examples the different aspects of extended
58
 
pattern matching. Consider for example the function that computes the
59
 
maximum of two natural numbers. We can write it in primitive syntax
60
 
by:
61
 
 
62
 
\begin{coq_example}
63
 
Fixpoint max (n m:nat) {struct m} : nat :=
64
 
  match n with
65
 
  | O => m
66
 
  | S n' => match m with
67
 
            | O => S n'
68
 
            | S m' => S (max n' m')
69
 
            end
70
 
  end.
71
 
\end{coq_example}
72
 
 
73
 
\paragraph{Multiple patterns}
74
 
 
75
 
Using multiple patterns in the definition of {\tt max} allows to write:
76
 
 
77
 
\begin{coq_example}
78
 
Reset max.
79
 
Fixpoint max (n m:nat) {struct m} : nat :=
80
 
  match n, m with
81
 
  | O, _ => m
82
 
  | S n', O => S n'
83
 
  | S n', S m' => S (max n' m')
84
 
  end.
85
 
\end{coq_example}
86
 
 
87
 
which will be compiled into the previous form.
88
 
 
89
 
The pattern-matching compilation strategy examines patterns from left
90
 
to right. A \texttt{match} expression is generated {\bf only} when
91
 
there is at least one constructor in the column of patterns. E.g. the
92
 
following example does not build a \texttt{match} expression.
93
 
 
94
 
\begin{coq_example}
95
 
Check (fun x:nat => match x return nat with
96
 
                    | y => y
97
 
                    end).
98
 
\end{coq_example}
99
 
 
100
 
\paragraph{Aliasing subpatterns}
101
 
 
102
 
We can also use ``\texttt{as} {\ident}'' to associate a name to a
103
 
sub-pattern:
104
 
 
105
 
\begin{coq_example}
106
 
Reset max.
107
 
Fixpoint max (n m:nat) {struct n} : nat :=
108
 
  match n, m with
109
 
  | O, _ => m
110
 
  | S n' as p, O => p
111
 
  | S n', S m' => S (max n' m')
112
 
  end.
113
 
\end{coq_example}
114
 
 
115
 
\paragraph{Nested patterns}
116
 
 
117
 
Here is now an example of nested patterns:
118
 
 
119
 
\begin{coq_example}
120
 
Fixpoint even (n:nat) : bool :=
121
 
  match n with
122
 
  | O => true
123
 
  | S O => false
124
 
  | S (S n') => even n'
125
 
  end.
126
 
\end{coq_example}
127
 
 
128
 
This is compiled into:
129
 
 
130
 
\begin{coq_example}
131
 
Print even.
132
 
\end{coq_example}
133
 
 
134
 
In the previous examples patterns do not conflict with, but
135
 
sometimes it is comfortable to write patterns that admit a non
136
 
trivial superposition. Consider
137
 
the boolean function \texttt{lef} that given two natural numbers
138
 
yields \texttt{true} if the first one is less or equal than the second
139
 
one and \texttt{false} otherwise. We can write it as follows:
140
 
 
141
 
\begin{coq_example}
142
 
Fixpoint lef (n m:nat) {struct m} : bool :=
143
 
  match n, m with
144
 
  | O, x => true
145
 
  | x, O => false
146
 
  | S n, S m => lef n m
147
 
  end.
148
 
\end{coq_example}
149
 
 
150
 
Note that the first and the second multiple pattern superpose because
151
 
the couple of values \texttt{O O} matches both. Thus, what is the result
152
 
of the function on those values?  To eliminate ambiguity we use the
153
 
{\em textual priority rule}: we consider patterns ordered from top to
154
 
bottom, then a value is matched by the pattern at the $ith$ row if and
155
 
only if it is not matched by some pattern of a previous row. Thus in the
156
 
example,
157
 
\texttt{O O} is matched by the first pattern, and so \texttt{(lef O O)}
158
 
yields \texttt{true}.
159
 
 
160
 
Another way to write  this function is:
161
 
 
162
 
\begin{coq_example}
163
 
Reset lef.
164
 
Fixpoint lef (n m:nat) {struct m} : bool :=
165
 
  match n, m with
166
 
  | O, x => true
167
 
  | S n, S m => lef n m
168
 
  | _, _ => false
169
 
  end.
170
 
\end{coq_example}
171
 
 
172
 
Here the last pattern superposes with the first two. Because
173
 
of the priority rule, the last pattern 
174
 
will be used only for values that do not match neither the  first nor
175
 
the second one.  
176
 
 
177
 
Terms with useless patterns are not accepted by the
178
 
system. Here is an example:
179
 
% Test failure
180
 
\begin{coq_eval}
181
 
Set Printing Depth 50.
182
 
  (********** The following is not correct and should produce **********)
183
 
  (**************** Error: This clause is redundant ********************)
184
 
\end{coq_eval}
185
 
\begin{coq_example}
186
 
Check (fun x:nat =>
187
 
         match x with
188
 
         | O => true
189
 
         | S _ => false
190
 
         | x => true
191
 
         end).
192
 
\end{coq_example}
193
 
 
194
 
\paragraph{Disjunctive patterns}
195
 
 
196
 
Multiple patterns that share the same right-hand-side can be
197
 
factorized using the notation \nelist{\multpattern}{\tt |}. For instance,
198
 
{\tt max} can be rewritten as follows:
199
 
 
200
 
\begin{coq_eval}
201
 
Reset max.
202
 
\end{coq_eval}
203
 
\begin{coq_example}
204
 
Fixpoint max (n m:nat) {struct m} : nat :=
205
 
  match n, m with
206
 
  | S n', S m' => S (max n' m')
207
 
  | 0, p | p, 0 => p
208
 
  end.
209
 
\end{coq_example}
210
 
 
211
 
Similarly, factorization of (non necessary multiple) patterns
212
 
that share the same variables is possible by using the notation
213
 
\nelist{\pattern}{\tt |}. Here is an example:
214
 
 
215
 
\begin{coq_example}
216
 
Definition filter_2_4 (n:nat) : nat :=
217
 
  match n with
218
 
  | 2 as m | 4 as m => m
219
 
  | _ => 0
220
 
  end.
221
 
\end{coq_example}
222
 
 
223
 
Here is another example using disjunctive subpatterns.
224
 
 
225
 
\begin{coq_example}
226
 
Definition filter_some_square_corners (p:nat*nat) : nat*nat :=
227
 
  match p with
228
 
  | ((2 as m | 4 as m), (3 as n | 5 as n)) => (m,n)
229
 
  | _ => (0,0)
230
 
  end.
231
 
\end{coq_example}
232
 
 
233
 
\asection{About patterns of parametric types}
234
 
When matching objects of a parametric type, constructors in patterns
235
 
{\em do not expect} the parameter arguments. Their value is deduced
236
 
during expansion.
237
 
Consider for example the type of polymorphic lists:
238
 
 
239
 
\begin{coq_example}
240
 
Inductive List (A:Set) : Set :=
241
 
  | nil : List A
242
 
  | cons : A -> List A -> List A.
243
 
\end{coq_example}
244
 
 
245
 
We can check the function {\em tail}:
246
 
 
247
 
\begin{coq_example}
248
 
Check
249
 
  (fun l:List nat =>
250
 
     match l with
251
 
     | nil => nil nat
252
 
     | cons _ l' => l'
253
 
     end).
254
 
\end{coq_example}
255
 
 
256
 
 
257
 
When we use parameters in patterns there is an error message:
258
 
% Test failure
259
 
\begin{coq_eval}
260
 
Set Printing Depth 50.
261
 
(********** The following is not correct and should produce **********)
262
 
(******** Error: The constructor cons expects 2 arguments ************)
263
 
\end{coq_eval}
264
 
\begin{coq_example}
265
 
Check
266
 
  (fun l:List nat =>
267
 
     match l with
268
 
     | nil A => nil nat
269
 
     | cons A _ l' => l'
270
 
     end).
271
 
\end{coq_example}
272
 
 
273
 
 
274
 
 
275
 
\asection{Matching objects of dependent types}
276
 
The previous examples illustrate pattern matching on objects of
277
 
non-dependent types, but we can also 
278
 
use the expansion strategy to destructure objects of dependent type.
279
 
Consider the type \texttt{listn} of lists of a certain length:
280
 
\label{listn}
281
 
 
282
 
\begin{coq_example}
283
 
Inductive listn : nat -> Set :=
284
 
  | niln : listn 0
285
 
  | consn : forall n:nat, nat -> listn n -> listn (S n).
286
 
\end{coq_example}
287
 
 
288
 
\asubsection{Understanding dependencies in patterns}
289
 
We can define the function \texttt{length} over \texttt{listn} by:
290
 
 
291
 
\begin{coq_example}
292
 
Definition length (n:nat) (l:listn n) := n.
293
 
\end{coq_example}
294
 
 
295
 
Just for illustrating pattern matching, 
296
 
we can define it by case analysis:
297
 
 
298
 
\begin{coq_example}
299
 
Reset length.
300
 
Definition length (n:nat) (l:listn n) :=
301
 
  match l with
302
 
  | niln => 0
303
 
  | consn n _ _ => S n
304
 
  end.
305
 
\end{coq_example}
306
 
 
307
 
We can understand the meaning of this definition using the
308
 
same notions of usual pattern matching.
309
 
 
310
 
%
311
 
% Constraining of dependencies is not longer valid in V7
312
 
%
313
 
\iffalse
314
 
Now suppose we split the second pattern  of \texttt{length} into two 
315
 
cases so to give an
316
 
alternative definition using nested patterns:
317
 
\begin{coq_example}
318
 
Definition length1 (n:nat) (l:listn n) :=
319
 
  match l with
320
 
  | niln => 0
321
 
  | consn n _ niln => S n
322
 
  | consn n _ (consn _ _ _) => S n
323
 
  end.
324
 
\end{coq_example}
325
 
 
326
 
It is obvious that \texttt{length1} is  another version of
327
 
\texttt{length}. We can also give the following definition:
328
 
\begin{coq_example}
329
 
Definition length2 (n:nat) (l:listn n) :=
330
 
  match l with
331
 
  | niln => 0
332
 
  | consn n _ niln => 1
333
 
  | consn n _ (consn m _ _) => S (S m)
334
 
  end.
335
 
\end{coq_example}
336
 
 
337
 
If we forget that \texttt{listn} is a dependent type and we read these
338
 
definitions using the usual semantics of pattern matching,  we can conclude
339
 
that \texttt{length1}
340
 
and \texttt{length2} are different functions.
341
 
In fact, they are equivalent
342
 
because the pattern \texttt{niln} implies that \texttt{n} can only match
343
 
the value $0$ and analogously the pattern \texttt{consn} determines that \texttt{n} can
344
 
only match  values of the form  $(S~v)$ where $v$ is the value matched by
345
 
\texttt{m}. 
346
 
 
347
 
The converse is also true. If
348
 
we destructure the  length  value with the pattern \texttt{O} then the list
349
 
value should be $niln$. 
350
 
Thus, the following term \texttt{length3} corresponds to the function
351
 
\texttt{length} but this time defined by case analysis on the dependencies instead of on the list:
352
 
 
353
 
\begin{coq_example}
354
 
Definition length3 (n:nat) (l:listn n) :=
355
 
  match l with
356
 
  | niln => 0
357
 
  | consn O _ _ => 1
358
 
  | consn (S n) _ _ => S (S n)
359
 
  end.
360
 
\end{coq_example}
361
 
 
362
 
When we have nested patterns of dependent types, the semantics of
363
 
pattern matching becomes a little more difficult because
364
 
the set of values that are matched by a sub-pattern may be conditioned by the
365
 
values matched by another sub-pattern. Dependent nested patterns are
366
 
somehow constrained patterns. 
367
 
In the examples, the expansion of
368
 
\texttt{length1} and \texttt{length2} yields exactly the same term
369
 
 but the
370
 
expansion of \texttt{length3} is completely different. \texttt{length1} and
371
 
\texttt{length2} are expanded into two nested case analysis on
372
 
\texttt{listn} while \texttt{length3} is expanded into a case analysis on
373
 
\texttt{listn} containing a case analysis on natural numbers inside.
374
 
 
375
 
 
376
 
In practice the user can think about the patterns as independent and
377
 
it is the expansion algorithm that cares to relate them. \\
378
 
\fi
379
 
%
380
 
%
381
 
%
382
 
 
383
 
\asubsection{When the elimination predicate must be provided}
384
 
The examples  given so far do not need an explicit elimination predicate
385
 
 because all the rhs have the same type and the
386
 
strategy succeeds to synthesize it.
387
 
Unfortunately when dealing with dependent patterns it often happens
388
 
that we need to write cases where the type of the rhs are 
389
 
different  instances of the elimination  predicate.
390
 
The function  \texttt{concat} for \texttt{listn}
391
 
is an example where the branches have different type
392
 
and we need to provide the elimination predicate:
393
 
 
394
 
\begin{coq_example}
395
 
Fixpoint concat (n:nat) (l:listn n) (m:nat) (l':listn m) {struct l} :
396
 
 listn (n + m) :=
397
 
  match l in listn n return listn (n + m) with
398
 
  | niln => l'
399
 
  | consn n' a y => consn (n' + m) a (concat n' y m l')
400
 
  end.
401
 
\end{coq_example}
402
 
The elimination predicate is {\tt fun (n:nat) (l:listn n) => listn~(n+m)}.
403
 
In general if $m$ has type $(I~q_1\ldots q_r~t_1\ldots t_s)$ where 
404
 
$q_1\ldots q_r$ are parameters, the elimination predicate should be of
405
 
the form~:
406
 
{\tt fun $y_1$\ldots $y_s$ $x$:($I$~$q_1$\ldots $q_r$~$y_1$\ldots
407
 
  $y_s$) => P}. 
408
 
 
409
 
In the concrete syntax, it should be written~:
410
 
\[ \kw{match}~m~\kw{as}~x~\kw{in}~(I~\_\ldots \_~y_1\ldots y_s)~\kw{return}~Q~\kw{with}~\ldots~\kw{end}\]
411
 
 
412
 
The variables which appear in the \kw{in} and \kw{as} clause are new
413
 
and bounded in the property $Q$ in the \kw{return} clause. The
414
 
parameters of the inductive definitions should not be mentioned and
415
 
are replaced by \kw{\_}.
416
 
 
417
 
Recall that a list of patterns is also a pattern. So, when
418
 
we destructure several terms at the same time and the branches have
419
 
different type  we need to provide
420
 
the elimination predicate for this multiple pattern. 
421
 
It is done using the same scheme, each term may be associated to an
422
 
\kw{as} and \kw{in} clause in order to introduce a dependent product.
423
 
 
424
 
For example, an equivalent definition for \texttt{concat} (even though the matching on the second term is trivial) would have
425
 
been:
426
 
 
427
 
\begin{coq_example}
428
 
Reset concat.
429
 
Fixpoint concat (n:nat) (l:listn n) (m:nat) (l':listn m) {struct l} :
430
 
 listn (n + m) :=
431
 
  match l in listn n, l' return listn (n + m) with
432
 
  | niln, x => x
433
 
  | consn n' a y, x => consn (n' + m) a (concat n' y m x)
434
 
  end.
435
 
\end{coq_example}
436
 
 
437
 
% Notice that this time, the predicate \texttt{[n,\_:nat](listn (plus n
438
 
%   m))}  is binary because we
439
 
% destructure both \texttt{l} and \texttt{l'} whose types have arity one.
440
 
% In general, if we destructure the terms $e_1\ldots e_n$
441
 
% the predicate will be of arity $m$ where $m$ is the sum of the
442
 
% number of dependencies of the type of $e_1, e_2,\ldots e_n$ 
443
 
% (the $\lambda$-abstractions
444
 
% should correspond from left to right to each dependent argument of the
445
 
% type of $e_1\ldots e_n$).
446
 
When the arity of the predicate (i.e. number of abstractions) is not
447
 
correct Coq raises an error message. For example:
448
 
 
449
 
% Test failure
450
 
\begin{coq_eval}
451
 
Reset concat.
452
 
Set Printing Depth 50.
453
 
(********** The following is not correct and should produce ***********)
454
 
(** Error: the term l' has type listn m while it is expected to have **)
455
 
(** type listn (?31 + ?32)                                           **)
456
 
\end{coq_eval}
457
 
\begin{coq_example}
458
 
Fixpoint concat
459
 
 (n:nat) (l:listn n) (m:nat)
460
 
 (l':listn m) {struct l} : listn (n + m) :=
461
 
  match l, l' with
462
 
  | niln, x => x
463
 
  | consn n' a y, x => consn (n' + m) a (concat n' y m x)
464
 
  end.
465
 
\end{coq_example}
466
 
 
467
 
\asection{Using pattern matching to write proofs}
468
 
In all the previous examples the elimination predicate does not depend
469
 
on the object(s) matched. But it may depend and the typical case 
470
 
is when we write a proof by induction or a function that yields an
471
 
object of dependent type. An example of proof using \texttt{match} in
472
 
given in section \ref{refine-example}
473
 
 
474
 
For example, we can write 
475
 
the function \texttt{buildlist} that given a natural number
476
 
$n$ builds a list of length $n$ containing zeros as follows:
477
 
 
478
 
\begin{coq_example}
479
 
Fixpoint buildlist (n:nat) : listn n :=
480
 
  match n return listn n with
481
 
  | O => niln
482
 
  | S n => consn n 0 (buildlist n)
483
 
  end.
484
 
\end{coq_example}
485
 
 
486
 
We can also use multiple patterns. 
487
 
Consider the following definition of the predicate less-equal
488
 
\texttt{Le}:
489
 
 
490
 
\begin{coq_example}
491
 
Inductive LE : nat -> nat -> Prop :=
492
 
  | LEO : forall n:nat, LE 0 n
493
 
  | LES : forall n m:nat, LE n m -> LE (S n) (S m).
494
 
\end{coq_example}
495
 
 
496
 
We can use multiple patterns to write  the proof of the lemma
497
 
 \texttt{forall (n m:nat), (LE n m)}\verb=\/=\texttt{(LE m n)}:
498
 
 
499
 
\begin{coq_example}
500
 
Fixpoint dec (n m:nat) {struct n} : LE n m \/ LE m n :=
501
 
  match n, m return LE n m \/ LE m n with
502
 
  | O, x => or_introl (LE x 0) (LEO x)
503
 
  | x, O => or_intror (LE x 0) (LEO x)
504
 
  | S n as n', S m as m' =>
505
 
      match dec n m with
506
 
      | or_introl h => or_introl (LE m' n') (LES n m h)
507
 
      | or_intror h => or_intror (LE n' m') (LES m n h)
508
 
      end
509
 
  end.
510
 
\end{coq_example}
511
 
In the example of \texttt{dec},
512
 
the first \texttt{match} is dependent while 
513
 
the second is not.
514
 
 
515
 
% In general, consider the terms $e_1\ldots e_n$,
516
 
% where  the type of $e_i$ is an instance of a family type
517
 
% $\lb (\vec{d_i}:\vec{D_i}) \mto T_i$  ($1\leq i
518
 
% \leq n$). Then, in expression \texttt{match}  $e_1,\ldots,
519
 
% e_n$ \texttt{of} \ldots \texttt{end}, the 
520
 
% elimination predicate ${\cal P}$ should be of the form:
521
 
% $[\vec{d_1}:\vec{D_1}][x_1:T_1]\ldots [\vec{d_n}:\vec{D_n}][x_n:T_n]Q.$
522
 
 
523
 
The user can also use \texttt{match} in combination with the tactic
524
 
\texttt{refine} (see section \ref{refine}) to build incomplete proofs
525
 
beginning with a \texttt{match} construction.
526
 
 
527
 
\asection{Pattern-matching on inductive objects involving local
528
 
definitions}
529
 
 
530
 
If local definitions occur in the type of a constructor, then there
531
 
are two ways to match on this constructor. Either the local
532
 
definitions are skipped and matching is done only on the true arguments
533
 
of the constructors, or the bindings for local definitions can also
534
 
be caught in the matching.
535
 
 
536
 
Example.
537
 
 
538
 
\begin{coq_eval}
539
 
Reset Initial.
540
 
Require Import Arith.
541
 
\end{coq_eval}
542
 
 
543
 
\begin{coq_example*}
544
 
Inductive list : nat -> Set :=
545
 
  | nil : list 0
546
 
  | cons : forall n:nat, let m := (2 * n) in list m -> list (S (S m)).
547
 
\end{coq_example*}
548
 
 
549
 
In the next example, the local definition is not caught.
550
 
 
551
 
\begin{coq_example}
552
 
Fixpoint length n (l:list n) {struct l} : nat :=
553
 
  match l with
554
 
  | nil => 0
555
 
  | cons n l0 => S (length (2 * n) l0)
556
 
  end.
557
 
\end{coq_example}
558
 
 
559
 
But in this example, it is.
560
 
 
561
 
\begin{coq_example}
562
 
Fixpoint length' n (l:list n) {struct l} : nat :=
563
 
  match l with
564
 
  | nil => 0
565
 
  | cons _ m l0 => S (length' m l0)
566
 
  end.
567
 
\end{coq_example}
568
 
 
569
 
\Rem for a given matching clause, either none of the local
570
 
definitions or all of them can be caught.
571
 
 
572
 
\asection{Pattern-matching and coercions}
573
 
 
574
 
If a mismatch occurs between the expected type of a pattern and its
575
 
actual type, a coercion made from constructors is sought. If such a
576
 
coercion can be found, it is automatically inserted around the
577
 
pattern.
578
 
 
579
 
Example:
580
 
 
581
 
\begin{coq_example}
582
 
Inductive I : Set :=
583
 
  | C1 : nat -> I
584
 
  | C2 : I -> I.
585
 
Coercion C1 : nat >-> I.
586
 
Check (fun x => match x with
587
 
                | C2 O => 0
588
 
                | _ => 0
589
 
                end).
590
 
\end{coq_example}
591
 
 
592
 
 
593
 
\asection{When does the expansion strategy fail ?}\label{limitations}
594
 
The strategy works very like in ML languages when treating
595
 
patterns of non-dependent type.  
596
 
But there are new cases of failure that are due to the presence of 
597
 
dependencies. 
598
 
 
599
 
The error messages of the current implementation may be sometimes
600
 
confusing.  When the tactic fails because patterns are somehow
601
 
incorrect then error messages refer to the initial expression. But the
602
 
strategy may succeed to build an expression whose sub-expressions are
603
 
well typed when the whole expression is not. In this situation the
604
 
message makes reference to the expanded expression.  We encourage
605
 
users, when they have patterns with the same outer constructor in
606
 
different equations, to name the variable patterns in the same
607
 
positions with the same name.  
608
 
E.g. to write {\small\texttt{(cons n O x) => e1}} 
609
 
and {\small\texttt{(cons n \_ x) => e2}} instead of
610
 
{\small\texttt{(cons n O x) => e1}} and 
611
 
{\small\texttt{(cons n' \_ x') => e2}}. 
612
 
This helps to maintain certain name correspondence between the
613
 
generated expression and the original.
614
 
 
615
 
Here is a summary of the error messages corresponding to each situation:
616
 
 
617
 
\begin{ErrMsgs}
618
 
\item \sverb{The constructor } {\sl
619
 
    ident} \sverb{expects } {\sl num} \sverb{arguments}
620
 
  
621
 
 \sverb{The variable } {\sl ident} \sverb{is bound several times
622
 
    in pattern } {\sl term}
623
 
  
624
 
 \sverb{Found a constructor of inductive type} {\term}
625
 
 \sverb{while a constructor of} {\term} \sverb{is expected}
626
 
 
627
 
 Patterns are incorrect (because constructors are not applied to
628
 
  the correct number of the arguments, because they are not linear or
629
 
  they are wrongly typed)
630
 
 
631
 
\item \errindex{Non exhaustive pattern-matching}
632
 
 
633
 
the pattern matching is not exhaustive
634
 
 
635
 
\item \sverb{The elimination predicate } {\sl term} \sverb{should be
636
 
    of arity } {\sl num} \sverb{(for non dependent case) or } {\sl
637
 
    num} \sverb{(for dependent case)}
638
 
 
639
 
The elimination predicate provided to \texttt{match} has not the
640
 
  expected arity
641
 
 
642
 
 
643
 
%\item the whole expression is wrongly typed
644
 
 
645
 
% CADUC ?
646
 
% , or the synthesis of
647
 
%   implicit arguments fails (for example to find the elimination
648
 
%   predicate or to resolve implicit arguments in the rhs).
649
 
 
650
 
%   There are {\em nested patterns of dependent type}, the elimination
651
 
%   predicate corresponds to non-dependent case and has the form
652
 
%   $[x_1:T_1]...[x_n:T_n]T$ and {\bf some} $x_i$ occurs {\bf free} in
653
 
%   $T$.  Then, the strategy may fail to find out a correct elimination
654
 
%   predicate during some step of compilation.  In this situation we
655
 
%   recommend the user to rewrite the nested dependent patterns into
656
 
%   several \texttt{match} with {\em simple patterns}.
657
 
  
658
 
\item {\tt Unable to infer a match predicate\\
659
 
    Either there is a type incompatiblity or the problem involves\\
660
 
    dependencies}
661
 
 
662
 
  There is a type mismatch between the different branches
663
 
 
664
 
  Then the user should provide an elimination predicate.
665
 
 
666
 
% Obsolete ?  
667
 
% \item because of nested patterns, it may happen that even though all
668
 
%   the rhs have the same type, the strategy needs dependent elimination
669
 
%   and so an elimination predicate must be provided. The system warns
670
 
%   about this situation, trying to compile anyway with the
671
 
%   non-dependent strategy. The risen message is:
672
 
 
673
 
% \begin{itemize}
674
 
% \item {\tt Warning: This pattern matching may need dependent
675
 
%     elimination to be compiled.  I will try, but if fails try again
676
 
%     giving dependent elimination predicate.}
677
 
% \end{itemize}
678
 
 
679
 
 
680
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
681
 
% % LA PROPAGATION DES CONTRAINTES ARRIERE N'EST PAS FAITE DANS LA V7
682
 
% TODO
683
 
% \item there are {\em nested patterns of dependent type} and the
684
 
%   strategy builds a term that is well typed but recursive calls in fix
685
 
%   point are reported as illegal:
686
 
% \begin{itemize}
687
 
% \item {\tt Error: Recursive call applied to an illegal term ...}
688
 
% \end{itemize}
689
 
 
690
 
% This is because the strategy generates a term that is correct w.r.t.
691
 
% the initial term but which does not pass the guard condition.  In
692
 
% this situation we recommend the user to transform the nested dependent
693
 
% patterns into {\em several \texttt{match} of simple patterns}.  Let us
694
 
% explain this with an example.  Consider the following definition of a
695
 
% function that yields the last element of a list and \texttt{O} if it is
696
 
% empty:
697
 
 
698
 
% \begin{coq_example}
699
 
%   Fixpoint last [n:nat; l:(listn n)] : nat :=
700
 
%    match l of 
701
 
%      (consn _ a niln) => a
702
 
%    | (consn m _ x) => (last m x) | niln => O
703
 
%    end.
704
 
% \end{coq_example}
705
 
 
706
 
% It fails because of the priority between patterns, we know that this
707
 
% definition is equivalent to the following more explicit one (which
708
 
% fails too):
709
 
 
710
 
% \begin{coq_example*}
711
 
%   Fixpoint last [n:nat; l:(listn n)] : nat :=
712
 
%    match l of
713
 
%      (consn _ a niln) => a
714
 
%    | (consn n _ (consn m b x)) => (last n (consn m b x))
715
 
%    | niln => O
716
 
%    end.
717
 
% \end{coq_example*}
718
 
 
719
 
% Note that the recursive call {\tt (last n (consn m b x))} is not
720
 
% guarded. When treating with patterns of dependent types the strategy
721
 
% interprets the first definition of \texttt{last} as the second
722
 
% one\footnote{In languages of the ML family the first definition would
723
 
%   be translated into a term where the variable \texttt{x} is shared in
724
 
%   the expression.  When patterns are of non-dependent types, Coq
725
 
%   compiles as in ML languages using sharing. When patterns are of
726
 
%   dependent types the compilation reconstructs the term as in the
727
 
%   second definition of \texttt{last} so to ensure the result of
728
 
%   expansion is well typed.}.  Thus it generates a term where the
729
 
% recursive call is rejected by the guard condition.
730
 
 
731
 
% You can get rid of this problem by writing the definition with
732
 
% \emph{simple patterns}:
733
 
 
734
 
% \begin{coq_example}
735
 
%   Fixpoint last [n:nat; l:(listn n)] : nat :=
736
 
%   <[_:nat]nat>match l of
737
 
%     (consn m a x) => Cases x of niln => a | _ => (last m x) end
738
 
%   | niln => O
739
 
%   end.
740
 
% \end{coq_example}
741
 
 
742
 
\end{ErrMsgs}
743
 
 
744
 
 
745
 
%%% Local Variables: 
746
 
%%% mode: latex
747
 
%%% TeX-master: "Reference-Manual"
748
 
%%% End: