-
Notifications
You must be signed in to change notification settings - Fork 1
/
MoreLogic.html
787 lines (628 loc) · 59.7 KB
/
MoreLogic.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link href="coqdoc.css" rel="stylesheet" type="text/css"/>
<title>MoreLogic</title>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<div id="page">
<div id="header">
</div>
<div id="main">
<h1 class="libtitle">MoreLogic</h1>
<div class="code code-tight">
</div>
<div class="doc">
<a name="lab288"></a><h1 class="section">More Logic</h1>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Export</span> "Prop".<br/>
<br/>
</div>
<div class="doc">
<a name="lab289"></a><h1 class="section">Existential Quantification</h1>
<div class="paragraph"> </div>
Another critical logical connective is <i>existential
quantification</i>. We can express it with the following
definition:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">ex</span> (<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) (<span class="id" type="var">P</span> : <span class="id" type="var">X</span><span style="font-family: arial;">→</span><span class="id" type="keyword">Prop</span>) : <span class="id" type="keyword">Prop</span> :=<br/>
<span class="id" type="var">ex_intro</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">witness</span>:<span class="id" type="var">X</span>), <span class="id" type="var">P</span> <span class="id" type="var">witness</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ex</span> <span class="id" type="var">X</span> <span class="id" type="var">P</span>.<br/>
<br/>
</div>
<div class="doc">
That is, <span class="inlinecode"><span class="id" type="var">ex</span></span> is a family of propositions indexed by a type <span class="inlinecode"><span class="id" type="var">X</span></span>
and a property <span class="inlinecode"><span class="id" type="var">P</span></span> over <span class="inlinecode"><span class="id" type="var">X</span></span>. In order to give evidence for the
assertion "there exists an <span class="inlinecode"><span class="id" type="var">x</span></span> for which the property <span class="inlinecode"><span class="id" type="var">P</span></span> holds"
we must actually name a <i>witness</i> — a specific value <span class="inlinecode"><span class="id" type="var">x</span></span> — and
then give evidence for <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span>, i.e., evidence that <span class="inlinecode"><span class="id" type="var">x</span></span> has the
property <span class="inlinecode"><span class="id" type="var">P</span></span>.
<div class="paragraph"> </div>
<div class="paragraph"> </div>
<a name="lab290"></a><h3 class="section"> </h3>
Coq's <span class="inlinecode"><span class="id" type="keyword">Notation</span></span> facility can be used to introduce more
familiar notation for writing existentially quantified
propositions, exactly parallel to the built-in syntax for
universally quantified propositions. Instead of writing <span class="inlinecode"><span class="id" type="var">ex</span></span> <span class="inlinecode"><span class="id" type="var">nat</span></span>
<span class="inlinecode"><span class="id" type="var">ev</span></span> to express the proposition that there exists some number that
is even, for example, we can write <span class="inlinecode"><span style="font-family: arial;">∃</span></span> <span class="inlinecode"><span class="id" type="var">x</span>:<span class="id" type="var">nat</span>,</span> <span class="inlinecode"><span class="id" type="var">ev</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span>. (It is
not necessary to understand exactly how the <span class="inlinecode"><span class="id" type="keyword">Notation</span></span> definition
works.)
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Notation</span> "'exists' x , p" := (<span class="id" type="var">ex</span> <span class="id" type="var">_</span> (<span class="id" type="keyword">fun</span> <span class="id" type="var">x</span> ⇒ <span class="id" type="var">p</span>))<br/>
(<span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 200, <span class="id" type="var">x</span> <span class="id" type="var">ident</span>, <span class="id" type="var">right</span> <span class="id" type="var">associativity</span>) : <span class="id" type="var">type_scope</span>.<br/>
<span class="id" type="keyword">Notation</span> "'exists' x : X , p" := (<span class="id" type="var">ex</span> <span class="id" type="var">_</span> (<span class="id" type="keyword">fun</span> <span class="id" type="var">x</span>:<span class="id" type="var">X</span> ⇒ <span class="id" type="var">p</span>))<br/>
(<span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 200, <span class="id" type="var">x</span> <span class="id" type="var">ident</span>, <span class="id" type="var">right</span> <span class="id" type="var">associativity</span>) : <span class="id" type="var">type_scope</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab291"></a><h3 class="section"> </h3>
We can use the usual set of tactics for
manipulating existentials. For example, to prove an
existential, we can <span class="inlinecode"><span class="id" type="tactic">apply</span></span> the constructor <span class="inlinecode"><span class="id" type="var">ex_intro</span></span>. Since the
premise of <span class="inlinecode"><span class="id" type="var">ex_intro</span></span> involves a variable (<span class="inlinecode"><span class="id" type="var">witness</span></span>) that does
not appear in its conclusion, we need to explicitly give its value
when we use <span class="inlinecode"><span class="id" type="tactic">apply</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">exists_example_1</span> : <span style="font-family: arial;">∃</span><span class="id" type="var">n</span>, <span class="id" type="var">n</span> + (<span class="id" type="var">n</span> × <span class="id" type="var">n</span>) = 6.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">ex_intro</span> <span class="id" type="keyword">with</span> (<span class="id" type="var">witness</span>:=2).<br/>
<span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Note that we have to explicitly give the witness.
<div class="paragraph"> </div>
<a name="lab292"></a><h3 class="section"> </h3>
Or, instead of writing <span class="inlinecode"><span class="id" type="tactic">apply</span></span> <span class="inlinecode"><span class="id" type="var">ex_intro</span></span> <span class="inlinecode"><span class="id" type="keyword">with</span></span> <span class="inlinecode">(<span class="id" type="var">witness</span>:=<span class="id" type="var">e</span>)</span> all the
time, we can use the convenient shorthand <span class="inlinecode"><span style="font-family: arial;">∃</span></span> <span class="inlinecode"><span class="id" type="var">e</span></span>, which means
the same thing.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">exists_example_1'</span> : <span style="font-family: arial;">∃</span><span class="id" type="var">n</span>, <span class="id" type="var">n</span> + (<span class="id" type="var">n</span> × <span class="id" type="var">n</span>) = 6.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span style="font-family: arial;">∃</span>2.<br/>
<span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab293"></a><h3 class="section"> </h3>
Conversely, if we have an existential hypothesis in the
context, we can eliminate it with <span class="inlinecode"><span class="id" type="tactic">inversion</span></span>. Note the use
of the <span class="inlinecode"><span class="id" type="keyword">as</span>...</span> pattern to name the variable that Coq
introduces to name the witness value and get evidence that
the hypothesis holds for the witness. (If we don't
explicitly choose one, Coq will just call it <span class="inlinecode"><span class="id" type="var">witness</span></span>, which
makes proofs confusing.)
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">exists_example_2</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>,<br/>
(<span style="font-family: arial;">∃</span><span class="id" type="var">m</span>, <span class="id" type="var">n</span> = 4 + <span class="id" type="var">m</span>) <span style="font-family: arial;">→</span><br/>
(<span style="font-family: arial;">∃</span><span class="id" type="var">o</span>, <span class="id" type="var">n</span> = 2 + <span class="id" type="var">o</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">m</span> <span class="id" type="var">Hm</span>].<br/>
<span style="font-family: arial;">∃</span>(2 + <span class="id" type="var">m</span>).<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">Hm</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Here is another example of how to work with existentials.
</div>
<div class="code code-tight">
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">exists_example_3</span> : <br/>
<span style="font-family: arial;">∃</span>(<span class="id" type="var">n</span>:<span class="id" type="var">nat</span>), <span class="id" type="var">even</span> <span class="id" type="var">n</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">beautiful</span> <span class="id" type="var">n</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* WORKED IN CLASS *)</span><br/>
<span style="font-family: arial;">∃</span>8.<br/>
<span class="id" type="tactic">split</span>.<br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">even</span>. <span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">b_sum</span> <span class="id" type="keyword">with</span> (<span class="id" type="var">n</span>:=3) (<span class="id" type="var">m</span>:=5).<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">b_3</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">b_5</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab294"></a><h4 class="section">Exercise: 1 star, optional (english_exists)</h4>
In English, what does the proposition
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">ex</span> <span class="id" type="var">nat</span> (<span class="id" type="keyword">fun</span> <span class="id" type="var">n</span> ⇒ <span class="id" type="var">beautiful</span> (<span class="id" type="var">S</span> <span class="id" type="var">n</span>))
<div class="paragraph"> </div>
</div>
mean?
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
<br/>
<span class="comment">(*<br/>
*)</span><br/>
</div>
<div class="doc">
<a name="lab295"></a><h4 class="section">Exercise: 1 star (dist_not_exists)</h4>
Prove that "<span class="inlinecode"><span class="id" type="var">P</span></span> holds for all <span class="inlinecode"><span class="id" type="var">x</span></span>" implies "there is no <span class="inlinecode"><span class="id" type="var">x</span></span> for
which <span class="inlinecode"><span class="id" type="var">P</span></span> does not hold."
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">dist_not_exists</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) (<span class="id" type="var">P</span> : <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span>),<br/>
(<span style="font-family: arial;">∀</span><span class="id" type="var">x</span>, <span class="id" type="var">P</span> <span class="id" type="var">x</span>) <span style="font-family: arial;">→</span> ¬ (<span style="font-family: arial;">∃</span><span class="id" type="var">x</span>, ¬ <span class="id" type="var">P</span> <span class="id" type="var">x</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab296"></a><h4 class="section">Exercise: 3 stars, optional (not_exists_dist)</h4>
(The other direction of this theorem requires the classical "law
of the excluded middle".)
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">not_exists_dist</span> :<br/>
<span class="id" type="var">excluded_middle</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">∀</span>(<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) (<span class="id" type="var">P</span> : <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span>),<br/>
¬ (<span style="font-family: arial;">∃</span><span class="id" type="var">x</span>, ¬ <span class="id" type="var">P</span> <span class="id" type="var">x</span>) <span style="font-family: arial;">→</span> (<span style="font-family: arial;">∀</span><span class="id" type="var">x</span>, <span class="id" type="var">P</span> <span class="id" type="var">x</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab297"></a><h4 class="section">Exercise: 2 stars (dist_exists_or)</h4>
Prove that existential quantification distributes over
disjunction.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">dist_exists_or</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) (<span class="id" type="var">P</span> <span class="id" type="var">Q</span> : <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span>),<br/>
(<span style="font-family: arial;">∃</span><span class="id" type="var">x</span>, <span class="id" type="var">P</span> <span class="id" type="var">x</span> <span style="font-family: arial;">∨</span> <span class="id" type="var">Q</span> <span class="id" type="var">x</span>) <span style="font-family: arial;">↔</span> (<span style="font-family: arial;">∃</span><span class="id" type="var">x</span>, <span class="id" type="var">P</span> <span class="id" type="var">x</span>) <span style="font-family: arial;">∨</span> (<span style="font-family: arial;">∃</span><span class="id" type="var">x</span>, <span class="id" type="var">Q</span> <span class="id" type="var">x</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab298"></a><h1 class="section">Evidence-carrying booleans.</h1>
<div class="paragraph"> </div>
So far we've seen two different forms of equality predicates:
<span class="inlinecode"><span class="id" type="var">eq</span></span>, which produces a <span class="inlinecode"><span class="id" type="keyword">Prop</span></span>, and
the type-specific forms, like <span class="inlinecode"><span class="id" type="var">beq_nat</span></span>, that produce <span class="inlinecode"><span class="id" type="var">boolean</span></span>
values. The former are more convenient to reason about, but
we've relied on the latter to let us use equality tests
in <i>computations</i>. While it is straightforward to write lemmas
(e.g. <span class="inlinecode"><span class="id" type="var">beq_nat_true</span></span> and <span class="inlinecode"><span class="id" type="var">beq_nat_false</span></span>) that connect the two forms,
using these lemmas quickly gets tedious.
<div class="paragraph"> </div>
<a name="lab299"></a><h3 class="section"> </h3>
<div class="paragraph"> </div>
It turns out that we can get the benefits of both forms at once
by using a construct called <span class="inlinecode"><span class="id" type="var">sumbool</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">sumbool</span> (<span class="id" type="var">A</span> <span class="id" type="var">B</span> : <span class="id" type="keyword">Prop</span>) : <span class="id" type="keyword">Set</span> :=<br/>
| <span class="id" type="var">left</span> : <span class="id" type="var">A</span> <span style="font-family: arial;">→</span> <span class="id" type="var">sumbool</span> <span class="id" type="var">A</span> <span class="id" type="var">B</span> <br/>
| <span class="id" type="var">right</span> : <span class="id" type="var">B</span> <span style="font-family: arial;">→</span> <span class="id" type="var">sumbool</span> <span class="id" type="var">A</span> <span class="id" type="var">B</span>.<br/>
<br/>
<span class="id" type="keyword">Notation</span> "{ A } + { B }" := (<span class="id" type="var">sumbool</span> <span class="id" type="var">A</span> <span class="id" type="var">B</span>) : <span class="id" type="var">type_scope</span>.<br/>
<br/>
</div>
<div class="doc">
Think of <span class="inlinecode"><span class="id" type="var">sumbool</span></span> as being like the <span class="inlinecode"><span class="id" type="var">boolean</span></span> type, but instead
of its values being just <span class="inlinecode"><span class="id" type="var">true</span></span> and <span class="inlinecode"><span class="id" type="var">false</span></span>, they carry <i>evidence</i>
of truth or falsity. This means that when we <span class="inlinecode"><span class="id" type="tactic">destruct</span></span> them, we
are left with the relevant evidence as a hypothesis — just as with <span class="inlinecode"><span class="id" type="var">or</span></span>.
(In fact, the definition of <span class="inlinecode"><span class="id" type="var">sumbool</span></span> is almost the same as for <span class="inlinecode"><span class="id" type="var">or</span></span>.
The only difference is that values of <span class="inlinecode"><span class="id" type="var">sumbool</span></span> are declared to be in
<span class="inlinecode"><span class="id" type="keyword">Set</span></span> rather than in <span class="inlinecode"><span class="id" type="keyword">Prop</span></span>; this is a technical distinction
that allows us to compute with them.)
<div class="paragraph"> </div>
<a name="lab300"></a><h3 class="section"> </h3>
<div class="paragraph"> </div>
Here's how we can define a <span class="inlinecode"><span class="id" type="var">sumbool</span></span> for equality on <span class="inlinecode"><span class="id" type="var">nat</span></span>s
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">eq_nat_dec</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span> <span class="id" type="var">m</span> : <span class="id" type="var">nat</span>, {<span class="id" type="var">n</span> = <span class="id" type="var">m</span>} + {<span class="id" type="var">n</span> ≠ <span class="id" type="var">m</span>}.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* WORKED IN CLASS *)</span><br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">n</span>.<br/>
<span class="id" type="tactic">induction</span> <span class="id" type="var">n</span> <span class="id" type="keyword">as</span> [|<span class="id" type="var">n'</span>].<br/>
<span class="id" type="var">Case</span> "n = 0".<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">m</span>.<br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">m</span> <span class="id" type="keyword">as</span> [|<span class="id" type="var">m'</span>].<br/>
<span class="id" type="var">SCase</span> "m = 0".<br/>
<span class="id" type="var">left</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">SCase</span> "m = S m'".<br/>
<span class="id" type="var">right</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">contra</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">contra</span>.<br/>
<span class="id" type="var">Case</span> "n = S n'".<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">m</span>.<br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">m</span> <span class="id" type="keyword">as</span> [|<span class="id" type="var">m'</span>].<br/>
<span class="id" type="var">SCase</span> "m = 0".<br/>
<span class="id" type="var">right</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">contra</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">contra</span>.<br/>
<span class="id" type="var">SCase</span> "m = S m'".<br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">IHn'</span> <span class="id" type="keyword">with</span> (<span class="id" type="var">m</span> := <span class="id" type="var">m'</span>) <span class="id" type="keyword">as</span> [<span class="id" type="var">eq</span> | <span class="id" type="var">neq</span>].<br/>
<span class="id" type="var">left</span>. <span class="id" type="tactic">apply</span> <span class="id" type="tactic">f_equal</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">eq</span>.<br/>
<span class="id" type="var">right</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">Heq</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">Heq</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">Heq'</span>]. <span class="id" type="tactic">apply</span> <span class="id" type="var">neq</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">Heq'</span>.<br/>
<span class="id" type="keyword">Defined</span>.<br/>
<br/>
</div>
<div class="doc">
Read as a theorem, this says that equality on <span class="inlinecode"><span class="id" type="var">nat</span></span>s is decidable:
that is, given two <span class="inlinecode"><span class="id" type="var">nat</span></span> values, we can always produce either
evidence that they are equal or evidence that they are not.
Read computationally, <span class="inlinecode"><span class="id" type="var">eq_nat_dec</span></span> takes two <span class="inlinecode"><span class="id" type="var">nat</span></span> values and returns
a <span class="inlinecode"><span class="id" type="var">sumbool</span></span> constructed with <span class="inlinecode"><span class="id" type="var">left</span></span> if they are equal and <span class="inlinecode"><span class="id" type="var">right</span></span>
if they are not; this result can be tested with a <span class="inlinecode"><span class="id" type="keyword">match</span></span> or, better,
with an <span class="inlinecode"><span class="id" type="keyword">if</span>-<span class="id" type="keyword">then</span>-<span class="id" type="keyword">else</span></span>, just like a regular <span class="inlinecode"><span class="id" type="var">boolean</span></span>.
(Notice that we ended this proof with <span class="inlinecode"><span class="id" type="keyword">Defined</span></span> rather than <span class="inlinecode"><span class="id" type="keyword">Qed</span></span>.
The only difference this makes is that the proof becomes <i>transparent</i>,
meaning that its definition is available when Coq tries to do reductions,
which is important for the computational interpretation.)
<div class="paragraph"> </div>
<a name="lab301"></a><h3 class="section"> </h3>
<div class="paragraph"> </div>
Here's a simple example illustrating the advantages of the <span class="inlinecode"><span class="id" type="var">sumbool</span></span> form.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">override'</span> {<span class="id" type="var">X</span>: <span class="id" type="keyword">Type</span>} (<span class="id" type="var">f</span>: <span class="id" type="var">nat</span><span style="font-family: arial;">→</span><span class="id" type="var">X</span>) (<span class="id" type="var">k</span>:<span class="id" type="var">nat</span>) (<span class="id" type="var">x</span>:<span class="id" type="var">X</span>) : <span class="id" type="var">nat</span><span style="font-family: arial;">→</span><span class="id" type="var">X</span>:=<br/>
<span class="id" type="keyword">fun</span> (<span class="id" type="var">k'</span>:<span class="id" type="var">nat</span>) ⇒ <span class="id" type="keyword">if</span> <span class="id" type="var">eq_nat_dec</span> <span class="id" type="var">k</span> <span class="id" type="var">k'</span> <span class="id" type="keyword">then</span> <span class="id" type="var">x</span> <span class="id" type="keyword">else</span> <span class="id" type="var">f</span> <span class="id" type="var">k'</span>.<br/>
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">override_same'</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) <span class="id" type="var">x1</span> <span class="id" type="var">k1</span> <span class="id" type="var">k2</span> (<span class="id" type="var">f</span> : <span class="id" type="var">nat</span><span style="font-family: arial;">→</span><span class="id" type="var">X</span>),<br/>
<span class="id" type="var">f</span> <span class="id" type="var">k1</span> = <span class="id" type="var">x1</span> <span style="font-family: arial;">→</span> <br/>
(<span class="id" type="var">override'</span> <span class="id" type="var">f</span> <span class="id" type="var">k1</span> <span class="id" type="var">x1</span>) <span class="id" type="var">k2</span> = <span class="id" type="var">f</span> <span class="id" type="var">k2</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">X</span> <span class="id" type="var">x1</span> <span class="id" type="var">k1</span> <span class="id" type="var">k2</span> <span class="id" type="var">f</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">Hx1</span>.<br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">override'</span>.<br/>
<span class="id" type="tactic">destruct</span> (<span class="id" type="var">eq_nat_dec</span> <span class="id" type="var">k1</span> <span class="id" type="var">k2</span>). <span class="comment">(* observe what appears as a hypothesis *)</span><br/>
<span class="id" type="var">Case</span> "k1 = k2".<br/>
<span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">←</span> <span class="id" type="var">e</span>.<br/>
<span class="id" type="tactic">symmetry</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">Hx1</span>.<br/>
<span class="id" type="var">Case</span> "k1 ≠ k2".<br/>
<span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Compare this to the more laborious proof (in MoreCoq.v) for the
version of <span class="inlinecode"><span class="id" type="var">override</span></span> defined using <span class="inlinecode"><span class="id" type="var">beq_nat</span></span>, where we had to
use the auxiliary lemma <span class="inlinecode"><span class="id" type="var">beq_nat_true</span></span> to convert a fact about booleans
to a Prop.
<div class="paragraph"> </div>
<a name="lab302"></a><h4 class="section">Exercise: 1 star (override_shadow')</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">override_shadow'</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) <span class="id" type="var">x1</span> <span class="id" type="var">x2</span> <span class="id" type="var">k1</span> <span class="id" type="var">k2</span> (<span class="id" type="var">f</span> : <span class="id" type="var">nat</span><span style="font-family: arial;">→</span><span class="id" type="var">X</span>),<br/>
(<span class="id" type="var">override'</span> (<span class="id" type="var">override'</span> <span class="id" type="var">f</span> <span class="id" type="var">k1</span> <span class="id" type="var">x2</span>) <span class="id" type="var">k1</span> <span class="id" type="var">x1</span>) <span class="id" type="var">k2</span> = (<span class="id" type="var">override'</span> <span class="id" type="var">f</span> <span class="id" type="var">k1</span> <span class="id" type="var">x1</span>) <span class="id" type="var">k2</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab303"></a><h1 class="section">Additional Exercises</h1>
<div class="paragraph"> </div>
<a name="lab304"></a><h4 class="section">Exercise: 3 stars (all_forallb)</h4>
Inductively define a property <span class="inlinecode"><span class="id" type="var">all</span></span> of lists, parameterized by a
type <span class="inlinecode"><span class="id" type="var">X</span></span> and a property <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode">:</span> <span class="inlinecode"><span class="id" type="var">X</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="keyword">Prop</span></span>, such that <span class="inlinecode"><span class="id" type="var">all</span></span> <span class="inlinecode"><span class="id" type="var">X</span></span> <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">l</span></span>
asserts that <span class="inlinecode"><span class="id" type="var">P</span></span> is true for every element of the list <span class="inlinecode"><span class="id" type="var">l</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">all</span> (<span class="id" type="var">X</span> : <span class="id" type="keyword">Type</span>) (<span class="id" type="var">P</span> : <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span>) : <span class="id" type="var">list</span> <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
.<br/>
<br/>
</div>
<div class="doc">
Recall the function <span class="inlinecode"><span class="id" type="var">forallb</span></span>, from the exercise
<span class="inlinecode"><span class="id" type="var">forall_exists_challenge</span></span> in chapter <span class="inlinecode"><span class="id" type="var">Poly</span></span>:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">forallb</span> {<span class="id" type="var">X</span> : <span class="id" type="keyword">Type</span>} (<span class="id" type="var">test</span> : <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="var">bool</span>) (<span class="id" type="var">l</span> : <span class="id" type="var">list</span> <span class="id" type="var">X</span>) : <span class="id" type="var">bool</span> :=<br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">l</span> <span class="id" type="keyword">with</span><br/>
| [] ⇒ <span class="id" type="var">true</span><br/>
| <span class="id" type="var">x</span> :: <span class="id" type="var">l'</span> ⇒ <span class="id" type="var">andb</span> (<span class="id" type="var">test</span> <span class="id" type="var">x</span>) (<span class="id" type="var">forallb</span> <span class="id" type="var">test</span> <span class="id" type="var">l'</span>)<br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
</div>
<div class="doc">
Using the property <span class="inlinecode"><span class="id" type="var">all</span></span>, write down a specification for <span class="inlinecode"><span class="id" type="var">forallb</span></span>,
and prove that it satisfies the specification. Try to make your
specification as precise as possible.
<div class="paragraph"> </div>
Are there any important properties of the function <span class="inlinecode"><span class="id" type="var">forallb</span></span> which
are not captured by your specification?
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab305"></a><h4 class="section">Exercise: 4 stars, advanced (filter_challenge)</h4>
One of the main purposes of Coq is to prove that programs match
their specifications. To this end, let's prove that our
definition of <span class="inlinecode"><span class="id" type="var">filter</span></span> matches a specification. Here is the
specification, written out informally in English.
<div class="paragraph"> </div>
Suppose we have a set <span class="inlinecode"><span class="id" type="var">X</span></span>, a function <span class="inlinecode"><span class="id" type="var">test</span>:</span> <span class="inlinecode"><span class="id" type="var">X</span><span style="font-family: arial;">→</span><span class="id" type="var">bool</span></span>, and a list
<span class="inlinecode"><span class="id" type="var">l</span></span> of type <span class="inlinecode"><span class="id" type="var">list</span></span> <span class="inlinecode"><span class="id" type="var">X</span></span>. Suppose further that <span class="inlinecode"><span class="id" type="var">l</span></span> is an "in-order
merge" of two lists, <span class="inlinecode"><span class="id" type="var">l1</span></span> and <span class="inlinecode"><span class="id" type="var">l2</span></span>, such that every item in <span class="inlinecode"><span class="id" type="var">l1</span></span>
satisfies <span class="inlinecode"><span class="id" type="var">test</span></span> and no item in <span class="inlinecode"><span class="id" type="var">l2</span></span> satisfies test. Then <span class="inlinecode"><span class="id" type="var">filter</span></span>
<span class="inlinecode"><span class="id" type="var">test</span></span> <span class="inlinecode"><span class="id" type="var">l</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">l1</span></span>.
<div class="paragraph"> </div>
A list <span class="inlinecode"><span class="id" type="var">l</span></span> is an "in-order merge" of <span class="inlinecode"><span class="id" type="var">l1</span></span> and <span class="inlinecode"><span class="id" type="var">l2</span></span> if it contains
all the same elements as <span class="inlinecode"><span class="id" type="var">l1</span></span> and <span class="inlinecode"><span class="id" type="var">l2</span></span>, in the same order as <span class="inlinecode"><span class="id" type="var">l1</span></span>
and <span class="inlinecode"><span class="id" type="var">l2</span></span>, but possibly interleaved. For example,
<div class="paragraph"> </div>
<div class="code code-tight">
[1,4,6,2,3]
<div class="paragraph"> </div>
</div>
is an in-order merge of
<div class="paragraph"> </div>
<div class="code code-tight">
[1,6,2]
<div class="paragraph"> </div>
</div>
and
<div class="paragraph"> </div>
<div class="code code-tight">
[4,3].
<div class="paragraph"> </div>
</div>
Your job is to translate this specification into a Coq theorem and
prove it. (Hint: You'll need to begin by defining what it means
for one list to be a merge of two others. Do this with an
inductive relation, not a <span class="inlinecode"><span class="id" type="keyword">Fixpoint</span></span>.)
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab306"></a><h4 class="section">Exercise: 5 stars, advanced, optional (filter_challenge_2)</h4>
A different way to formally characterize the behavior of <span class="inlinecode"><span class="id" type="var">filter</span></span>
goes like this: Among all subsequences of <span class="inlinecode"><span class="id" type="var">l</span></span> with the property
that <span class="inlinecode"><span class="id" type="var">test</span></span> evaluates to <span class="inlinecode"><span class="id" type="var">true</span></span> on all their members, <span class="inlinecode"><span class="id" type="var">filter</span></span> <span class="inlinecode"><span class="id" type="var">test</span></span>
<span class="inlinecode"><span class="id" type="var">l</span></span> is the longest. Express this claim formally and prove it.
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab307"></a><h4 class="section">Exercise: 4 stars, advanced (no_repeats)</h4>
The following inductively defined proposition...
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">appears_in</span> {<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>} (<span class="id" type="var">a</span>:<span class="id" type="var">X</span>) : <span class="id" type="var">list</span> <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
| <span class="id" type="var">ai_here</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">l</span>, <span class="id" type="var">appears_in</span> <span class="id" type="var">a</span> (<span class="id" type="var">a</span>::<span class="id" type="var">l</span>)<br/>
| <span class="id" type="var">ai_later</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">b</span> <span class="id" type="var">l</span>, <span class="id" type="var">appears_in</span> <span class="id" type="var">a</span> <span class="id" type="var">l</span> <span style="font-family: arial;">→</span> <span class="id" type="var">appears_in</span> <span class="id" type="var">a</span> (<span class="id" type="var">b</span>::<span class="id" type="var">l</span>).<br/>
<br/>
</div>
<div class="doc">
...gives us a precise way of saying that a value <span class="inlinecode"><span class="id" type="var">a</span></span> appears at
least once as a member of a list <span class="inlinecode"><span class="id" type="var">l</span></span>.
<div class="paragraph"> </div>
Here's a pair of warm-ups about <span class="inlinecode"><span class="id" type="var">appears_in</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">appears_in_app</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) (<span class="id" type="var">xs</span> <span class="id" type="var">ys</span> : <span class="id" type="var">list</span> <span class="id" type="var">X</span>) (<span class="id" type="var">x</span>:<span class="id" type="var">X</span>), <br/>
<span class="id" type="var">appears_in</span> <span class="id" type="var">x</span> (<span class="id" type="var">xs</span> ++ <span class="id" type="var">ys</span>) <span style="font-family: arial;">→</span> <span class="id" type="var">appears_in</span> <span class="id" type="var">x</span> <span class="id" type="var">xs</span> <span style="font-family: arial;">∨</span> <span class="id" type="var">appears_in</span> <span class="id" type="var">x</span> <span class="id" type="var">ys</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">app_appears_in</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) (<span class="id" type="var">xs</span> <span class="id" type="var">ys</span> : <span class="id" type="var">list</span> <span class="id" type="var">X</span>) (<span class="id" type="var">x</span>:<span class="id" type="var">X</span>), <br/>
<span class="id" type="var">appears_in</span> <span class="id" type="var">x</span> <span class="id" type="var">xs</span> <span style="font-family: arial;">∨</span> <span class="id" type="var">appears_in</span> <span class="id" type="var">x</span> <span class="id" type="var">ys</span> <span style="font-family: arial;">→</span> <span class="id" type="var">appears_in</span> <span class="id" type="var">x</span> (<span class="id" type="var">xs</span> ++ <span class="id" type="var">ys</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
</div>
<div class="doc">
Now use <span class="inlinecode"><span class="id" type="var">appears_in</span></span> to define a proposition <span class="inlinecode"><span class="id" type="var">disjoint</span></span> <span class="inlinecode"><span class="id" type="var">X</span></span> <span class="inlinecode"><span class="id" type="var">l1</span></span> <span class="inlinecode"><span class="id" type="var">l2</span></span>,
which should be provable exactly when <span class="inlinecode"><span class="id" type="var">l1</span></span> and <span class="inlinecode"><span class="id" type="var">l2</span></span> are
lists (with elements of type X) that have no elements in common.
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
<br/>
</div>
<div class="doc">
Next, use <span class="inlinecode"><span class="id" type="var">appears_in</span></span> to define an inductive proposition
<span class="inlinecode"><span class="id" type="var">no_repeats</span></span> <span class="inlinecode"><span class="id" type="var">X</span></span> <span class="inlinecode"><span class="id" type="var">l</span></span>, which should be provable exactly when <span class="inlinecode"><span class="id" type="var">l</span></span> is a
list (with elements of type <span class="inlinecode"><span class="id" type="var">X</span></span>) where every member is different
from every other. For example, <span class="inlinecode"><span class="id" type="var">no_repeats</span></span> <span class="inlinecode"><span class="id" type="var">nat</span></span> <span class="inlinecode">[1,2,3,4]</span> and
<span class="inlinecode"><span class="id" type="var">no_repeats</span></span> <span class="inlinecode"><span class="id" type="var">bool</span></span> <span class="inlinecode">[]</span> should be provable, while <span class="inlinecode"><span class="id" type="var">no_repeats</span></span> <span class="inlinecode"><span class="id" type="var">nat</span></span>
<span class="inlinecode">[1,2,1]</span> and <span class="inlinecode"><span class="id" type="var">no_repeats</span></span> <span class="inlinecode"><span class="id" type="var">bool</span></span> <span class="inlinecode">[<span class="id" type="var">true</span>,<span class="id" type="var">true</span>]</span> should not be.
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
<br/>
</div>
<div class="doc">
Finally, state and prove one or more interesting theorems relating
<span class="inlinecode"><span class="id" type="var">disjoint</span></span>, <span class="inlinecode"><span class="id" type="var">no_repeats</span></span> and <span class="inlinecode">++</span> (list append).
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab308"></a><h4 class="section">Exercise: 3 stars (nostutter)</h4>
Formulating inductive definitions of predicates is an important
skill you'll need in this course. Try to solve this exercise
without any help at all (except from your study group partner, if
you have one).
<div class="paragraph"> </div>
We say that a list of numbers "stutters" if it repeats the same
number consecutively. The predicate "<span class="inlinecode"><span class="id" type="var">nostutter</span></span> <span class="inlinecode"><span class="id" type="var">mylist</span></span>" means
that <span class="inlinecode"><span class="id" type="var">mylist</span></span> does not stutter. Formulate an inductive definition
for <span class="inlinecode"><span class="id" type="var">nostutter</span></span>. (This is different from the <span class="inlinecode"><span class="id" type="var">no_repeats</span></span>
predicate in the exercise above; the sequence <span class="inlinecode">1,4,1</span> repeats but
does not stutter.)
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">nostutter</span>: <span class="id" type="var">list</span> <span class="id" type="var">nat</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
.<br/>
<br/>
</div>
<div class="doc">
Make sure each of these tests succeeds, but you are free
to change the proof if the given one doesn't work for you.
Your definition might be different from mine and still correct,
in which case the examples might need a different proof.
<div class="paragraph"> </div>
The suggested proofs for the examples (in comments) use a number
of tactics we haven't talked about, to try to make them robust
with respect to different possible ways of defining <span class="inlinecode"><span class="id" type="var">nostutter</span></span>.
You should be able to just uncomment and use them as-is, but if
you prefer you can also prove each example with more basic
tactics.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_nostutter_1</span>: <span class="id" type="var">nostutter</span> [3;1;4;1;5;6].<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<span class="comment">(* <br/>
Proof. repeat constructor; apply beq_nat_false; auto. Qed.<br/>
*)</span><br/>
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_nostutter_2</span>: <span class="id" type="var">nostutter</span> [].<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<span class="comment">(* <br/>
Proof. repeat constructor; apply beq_nat_false; auto. Qed.<br/>
*)</span><br/>
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_nostutter_3</span>: <span class="id" type="var">nostutter</span> [5].<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<span class="comment">(* <br/>
Proof. repeat constructor; apply beq_nat_false; auto. Qed.<br/>
*)</span><br/>
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_nostutter_4</span>: <span class="id" type="var">not</span> (<span class="id" type="var">nostutter</span> [3;1;1;4]).<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<span class="comment">(* <br/>
Proof. intro.<br/>
repeat match goal with <br/>
h: nostutter _ |- _ => inversion h; clear h; subst <br/>
end.<br/>
contradiction H1; auto. Qed.<br/>
*)</span><br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab309"></a><h4 class="section">Exercise: 4 stars, advanced (pigeonhole principle)</h4>
The "pigeonhole principle" states a basic fact about counting:
if you distribute more than <span class="inlinecode"><span class="id" type="var">n</span></span> items into <span class="inlinecode"><span class="id" type="var">n</span></span> pigeonholes, some
pigeonhole must contain at least two items. As is often the case,
this apparently trivial fact about numbers requires non-trivial
machinery to prove, but we now have enough...
<div class="paragraph"> </div>
First a pair of useful lemmas (we already proved these for lists
of naturals, but not for arbitrary lists).
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">app_length</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) (<span class="id" type="var">l1</span> <span class="id" type="var">l2</span> : <span class="id" type="var">list</span> <span class="id" type="var">X</span>),<br/>
<span class="id" type="var">length</span> (<span class="id" type="var">l1</span> ++ <span class="id" type="var">l2</span>) = <span class="id" type="var">length</span> <span class="id" type="var">l1</span> + <span class="id" type="var">length</span> <span class="id" type="var">l2</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">appears_in_app_split</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) (<span class="id" type="var">x</span>:<span class="id" type="var">X</span>) (<span class="id" type="var">l</span>:<span class="id" type="var">list</span> <span class="id" type="var">X</span>),<br/>
<span class="id" type="var">appears_in</span> <span class="id" type="var">x</span> <span class="id" type="var">l</span> <span style="font-family: arial;">→</span> <br/>
<span style="font-family: arial;">∃</span><span class="id" type="var">l1</span>, <span style="font-family: arial;">∃</span><span class="id" type="var">l2</span>, <span class="id" type="var">l</span> = <span class="id" type="var">l1</span> ++ (<span class="id" type="var">x</span>::<span class="id" type="var">l2</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
</div>
<div class="doc">
Now define a predicate <span class="inlinecode"><span class="id" type="var">repeats</span></span> (analogous to <span class="inlinecode"><span class="id" type="var">no_repeats</span></span> in the
exercise above), such that <span class="inlinecode"><span class="id" type="var">repeats</span></span> <span class="inlinecode"><span class="id" type="var">X</span></span> <span class="inlinecode"><span class="id" type="var">l</span></span> asserts that <span class="inlinecode"><span class="id" type="var">l</span></span> contains
at least one repeated element (of type <span class="inlinecode"><span class="id" type="var">X</span></span>).
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">repeats</span> {<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>} : <span class="id" type="var">list</span> <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
.<br/>
<br/>
</div>
<div class="doc">
Now here's a way to formalize the pigeonhole principle. List <span class="inlinecode"><span class="id" type="var">l2</span></span>
represents a list of pigeonhole labels, and list <span class="inlinecode"><span class="id" type="var">l1</span></span> represents
the labels assigned to a list of items: if there are more items
than labels, at least two items must have the same label. This
proof is much easier if you use the <span class="inlinecode"><span class="id" type="var">excluded_middle</span></span> hypothesis
to show that <span class="inlinecode"><span class="id" type="var">appears_in</span></span> is decidable, i.e. <span class="inlinecode"><span style="font-family: arial;">∀</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span>
<span class="inlinecode"><span class="id" type="var">l</span>,</span> <span class="inlinecode">(<span class="id" type="var">appears_in</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="var">l</span>)</span> <span class="inlinecode"><span style="font-family: arial;">∨</span></span> <span class="inlinecode">¬</span> <span class="inlinecode">(<span class="id" type="var">appears_in</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="var">l</span>)</span>. However, it is also
possible to make the proof go through <i>without</i> assuming that
<span class="inlinecode"><span class="id" type="var">appears_in</span></span> is decidable; if you can manage to do this, you will
not need the <span class="inlinecode"><span class="id" type="var">excluded_middle</span></span> hypothesis.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">pigeonhole_principle</span>: <span style="font-family: arial;">∀</span>(<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) (<span class="id" type="var">l1</span> <span class="id" type="var">l2</span>:<span class="id" type="var">list</span> <span class="id" type="var">X</span>), <br/>
<span class="id" type="var">excluded_middle</span> <span style="font-family: arial;">→</span> <br/>
(<span style="font-family: arial;">∀</span><span class="id" type="var">x</span>, <span class="id" type="var">appears_in</span> <span class="id" type="var">x</span> <span class="id" type="var">l1</span> <span style="font-family: arial;">→</span> <span class="id" type="var">appears_in</span> <span class="id" type="var">x</span> <span class="id" type="var">l2</span>) <span style="font-family: arial;">→</span> <br/>
<span class="id" type="var">length</span> <span class="id" type="var">l2</span> < <span class="id" type="var">length</span> <span class="id" type="var">l1</span> <span style="font-family: arial;">→</span> <br/>
<span class="id" type="var">repeats</span> <span class="id" type="var">l1</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">X</span> <span class="id" type="var">l1</span>. <span class="id" type="tactic">induction</span> <span class="id" type="var">l1</span> <span class="id" type="keyword">as</span> [|<span class="id" type="var">x</span> <span class="id" type="var">l1'</span>].<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
<br/>
<span class="comment">(* $Date: 2014-02-22 09:43:41 -0500 (Sat, 22 Feb 2014) $ *)</span><br/>
</div>
</div>
<div id="footer">
<hr/><a href="coqindex.html">Index</a><hr/>This page has been generated by <a href="http://www.lix.polytechnique.fr/coq/">coqdoc</a>
</div>
</div>
</body>
</html>