-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lists.html
1770 lines (1372 loc) · 117 KB
/
Lists.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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!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>Lists: Working with Structured Data</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">Lists<span class="subtitle">Working with Structured Data</span></h1>
<div class="code code-tight">
</div>
<div class="doc">
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Export</span> <span class="id" type="keyword">Induction</span>.<br/>
<br/>
<span class="id" type="keyword">Module</span> <span class="id" type="var">NatList</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab59"></a><h1 class="section">Pairs of Numbers</h1>
<div class="paragraph"> </div>
In an <span class="inlinecode"><span class="id" type="keyword">Inductive</span></span> type definition, each constructor can take
any number of arguments — none (as with <span class="inlinecode"><span class="id" type="var">true</span></span> and <span class="inlinecode"><span class="id" type="var">O</span></span>), one (as
with <span class="inlinecode"><span class="id" type="var">S</span></span>), or more than one, as in this definition:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">natprod</span> : <span class="id" type="keyword">Type</span> :=<br/>
<span class="id" type="var">pair</span> : <span class="id" type="var">nat</span> <span style="font-family: arial;">→</span> <span class="id" type="var">nat</span> <span style="font-family: arial;">→</span> <span class="id" type="var">natprod</span>.<br/>
<br/>
</div>
<div class="doc">
This declaration can be read: "There is just one way to
construct a pair of numbers: by applying the constructor <span class="inlinecode"><span class="id" type="var">pair</span></span> to
two arguments of type <span class="inlinecode"><span class="id" type="var">nat</span></span>."
<div class="paragraph"> </div>
We can construct an element of <span class="inlinecode"><span class="id" type="var">natprod</span></span> like this:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Check</span> (<span class="id" type="var">pair</span> 3 5).<br/>
<br/>
</div>
<div class="doc">
<a name="lab60"></a><h3 class="section"> </h3>
<div class="paragraph"> </div>
Here are two simple function definitions for extracting the
first and second components of a pair. (The definitions also
illustrate how to do pattern matching on two-argument
constructors.)
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">fst</span> (<span class="id" type="var">p</span> : <span class="id" type="var">natprod</span>) : <span class="id" type="var">nat</span> := <br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">p</span> <span class="id" type="keyword">with</span><br/>
| <span class="id" type="var">pair</span> <span class="id" type="var">x</span> <span class="id" type="var">y</span> ⇒ <span class="id" type="var">x</span><br/>
<span class="id" type="keyword">end</span>.<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">snd</span> (<span class="id" type="var">p</span> : <span class="id" type="var">natprod</span>) : <span class="id" type="var">nat</span> := <br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">p</span> <span class="id" type="keyword">with</span><br/>
| <span class="id" type="var">pair</span> <span class="id" type="var">x</span> <span class="id" type="var">y</span> ⇒ <span class="id" type="var">y</span><br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
<span class="id" type="keyword">Eval</span> <span class="id" type="tactic">compute</span> <span class="id" type="keyword">in</span> (<span class="id" type="var">fst</span> (<span class="id" type="var">pair</span> 3 5)).<br/>
<span class="comment">(* ===> 3 *)</span><br/>
<br/>
</div>
<div class="doc">
<a name="lab61"></a><h3 class="section"> </h3>
<div class="paragraph"> </div>
Since pairs are used quite a bit, it is nice to be able to
write them with the standard mathematical notation <span class="inlinecode">(<span class="id" type="var">x</span>,<span class="id" type="var">y</span>)</span> instead
of <span class="inlinecode"><span class="id" type="var">pair</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="var">y</span></span>. We can tell Coq to allow this with a <span class="inlinecode"><span class="id" type="keyword">Notation</span></span>
declaration.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Notation</span> "( x , y )" := (<span class="id" type="var">pair</span> <span class="id" type="var">x</span> <span class="id" type="var">y</span>).<br/>
<br/>
</div>
<div class="doc">
The new notation can be used both in expressions and in
pattern matches (indeed, we've seen it already in the previous
chapter — this notation is provided as part of the standard
library):
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Eval</span> <span class="id" type="tactic">compute</span> <span class="id" type="keyword">in</span> (<span class="id" type="var">fst</span> (3,5)).<br/>
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">fst'</span> (<span class="id" type="var">p</span> : <span class="id" type="var">natprod</span>) : <span class="id" type="var">nat</span> := <br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">p</span> <span class="id" type="keyword">with</span><br/>
| (<span class="id" type="var">x</span>,<span class="id" type="var">y</span>) ⇒ <span class="id" type="var">x</span><br/>
<span class="id" type="keyword">end</span>.<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">snd'</span> (<span class="id" type="var">p</span> : <span class="id" type="var">natprod</span>) : <span class="id" type="var">nat</span> := <br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">p</span> <span class="id" type="keyword">with</span><br/>
| (<span class="id" type="var">x</span>,<span class="id" type="var">y</span>) ⇒ <span class="id" type="var">y</span><br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">swap_pair</span> (<span class="id" type="var">p</span> : <span class="id" type="var">natprod</span>) : <span class="id" type="var">natprod</span> := <br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">p</span> <span class="id" type="keyword">with</span><br/>
| (<span class="id" type="var">x</span>,<span class="id" type="var">y</span>) ⇒ (<span class="id" type="var">y</span>,<span class="id" type="var">x</span>)<br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab62"></a><h3 class="section"> </h3>
<div class="paragraph"> </div>
Let's try and prove a few simple facts about pairs. If we
state the lemmas in a particular (and slightly peculiar) way, we
can prove them with just reflexivity (and its built-in
simplification):
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">surjective_pairing'</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>),<br/>
(<span class="id" type="var">n</span>,<span class="id" type="var">m</span>) = (<span class="id" type="var">fst</span> (<span class="id" type="var">n</span>,<span class="id" type="var">m</span>), <span class="id" type="var">snd</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="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Note that <span class="inlinecode"><span class="id" type="tactic">reflexivity</span></span> is not enough if we state the lemma in a
more natural way:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">surjective_pairing_stuck</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">p</span> : <span class="id" type="var">natprod</span>),<br/>
<span class="id" type="var">p</span> = (<span class="id" type="var">fst</span> <span class="id" type="var">p</span>, <span class="id" type="var">snd</span> <span class="id" type="var">p</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">simpl</span>. <span class="comment">(* Doesn't reduce anything! *)</span><br/>
<span class="id" type="keyword">Abort</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab63"></a><h3 class="section"> </h3>
We have to expose the structure of <span class="inlinecode"><span class="id" type="var">p</span></span> so that <span class="inlinecode"><span class="id" type="tactic">simpl</span></span> can
perform the pattern match in <span class="inlinecode"><span class="id" type="var">fst</span></span> and <span class="inlinecode"><span class="id" type="var">snd</span></span>. We can do this with
<span class="inlinecode"><span class="id" type="tactic">destruct</span></span>.
<div class="paragraph"> </div>
Notice that, unlike for <span class="inlinecode"><span class="id" type="var">nat</span></span>s, <span class="inlinecode"><span class="id" type="tactic">destruct</span></span> doesn't generate an
extra subgoal here. That's because <span class="inlinecode"><span class="id" type="var">natprod</span></span>s can only be
constructed in one way.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">surjective_pairing</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">p</span> : <span class="id" type="var">natprod</span>),<br/>
<span class="id" type="var">p</span> = (<span class="id" type="var">fst</span> <span class="id" type="var">p</span>, <span class="id" type="var">snd</span> <span class="id" type="var">p</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">p</span>. <span class="id" type="tactic">destruct</span> <span class="id" type="var">p</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">n</span> <span class="id" type="var">m</span>]. <span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab64"></a><h4 class="section">Exercise: 1 star (snd_fst_is_swap)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">snd_fst_is_swap</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">p</span> : <span class="id" type="var">natprod</span>),<br/>
(<span class="id" type="var">snd</span> <span class="id" type="var">p</span>, <span class="id" type="var">fst</span> <span class="id" type="var">p</span>) = <span class="id" type="var">swap_pair</span> <span class="id" type="var">p</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="lab65"></a><h4 class="section">Exercise: 1 star, optional (fst_swap_is_snd)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">fst_swap_is_snd</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">p</span> : <span class="id" type="var">natprod</span>),<br/>
<span class="id" type="var">fst</span> (<span class="id" type="var">swap_pair</span> <span class="id" type="var">p</span>) = <span class="id" type="var">snd</span> <span class="id" type="var">p</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="lab66"></a><h1 class="section">Lists of Numbers</h1>
<div class="paragraph"> </div>
Generalizing the definition of pairs a little, we can
describe the type of <i>lists</i> of numbers like this: "A list is
either the empty list or else a pair of a number and another
list."
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">natlist</span> : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">nil</span> : <span class="id" type="var">natlist</span><br/>
| <span class="id" type="var">cons</span> : <span class="id" type="var">nat</span> <span style="font-family: arial;">→</span> <span class="id" type="var">natlist</span> <span style="font-family: arial;">→</span> <span class="id" type="var">natlist</span>.<br/>
<br/>
</div>
<div class="doc">
For example, here is a three-element list:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">mylist</span> := <span class="id" type="var">cons</span> 1 (<span class="id" type="var">cons</span> 2 (<span class="id" type="var">cons</span> 3 <span class="id" type="var">nil</span>)).<br/>
<br/>
</div>
<div class="doc">
<a name="lab67"></a><h3 class="section"> </h3>
As with pairs, it is more convenient to write lists in
familiar programming notation. The following two declarations
allow us to use <span class="inlinecode">::</span> as an infix <span class="inlinecode"><span class="id" type="var">cons</span></span> operator and square
brackets as an "outfix" notation for constructing lists.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Notation</span> "x :: l" := (<span class="id" type="var">cons</span> <span class="id" type="var">x</span> <span class="id" type="var">l</span>) (<span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 60, <span class="id" type="var">right</span> <span class="id" type="var">associativity</span>).<br/>
<span class="id" type="keyword">Notation</span> "[ ]" := <span class="id" type="var">nil</span>.<br/>
<span class="id" type="keyword">Notation</span> "[ x ; .. ; y ]" := (<span class="id" type="var">cons</span> <span class="id" type="var">x</span> .. (<span class="id" type="var">cons</span> <span class="id" type="var">y</span> <span class="id" type="var">nil</span>) ..).<br/>
<br/>
</div>
<div class="doc">
It is not necessary to fully understand these declarations,
but in case you are interested, here is roughly what's going on.
<div class="paragraph"> </div>
The <span class="inlinecode"><span class="id" type="var">right</span></span> <span class="inlinecode"><span class="id" type="var">associativity</span></span> annotation tells Coq how to parenthesize
expressions involving several uses of <span class="inlinecode">::</span> so that, for example,
the next three declarations mean exactly the same thing:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">mylist1</span> := 1 :: (2 :: (3 :: <span class="id" type="var">nil</span>)).<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">mylist2</span> := 1 :: 2 :: 3 :: <span class="id" type="var">nil</span>.<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">mylist3</span> := [1;2;3].<br/>
<br/>
</div>
<div class="doc">
The <span class="inlinecode"><span class="id" type="tactic">at</span></span> <span class="inlinecode"><span class="id" type="var">level</span></span> <span class="inlinecode">60</span> part tells Coq how to parenthesize
expressions that involve both <span class="inlinecode">::</span> and some other infix operator.
For example, since we defined <span class="inlinecode">+</span> as infix notation for the <span class="inlinecode"><span class="id" type="var">plus</span></span>
function at level 50,
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="keyword">Notation</span> "x + y" := (<span class="id" type="var">plus</span> <span class="id" type="var">x</span> <span class="id" type="var">y</span>) <br/>
(<span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 50, <span class="id" type="var">left</span> <span class="id" type="var">associativity</span>).
<div class="paragraph"> </div>
</div>
The <span class="inlinecode">+</span> operator will bind tighter than <span class="inlinecode">::</span>, so <span class="inlinecode">1</span> <span class="inlinecode">+</span> <span class="inlinecode">2</span> <span class="inlinecode">::</span> <span class="inlinecode">[3]</span>
will be parsed, as we'd expect, as <span class="inlinecode">(1</span> <span class="inlinecode">+</span> <span class="inlinecode">2)</span> <span class="inlinecode">::</span> <span class="inlinecode">[3]</span> rather than <span class="inlinecode">1</span>
<span class="inlinecode">+</span> <span class="inlinecode">(2</span> <span class="inlinecode">::</span> <span class="inlinecode">[3])</span>.
<div class="paragraph"> </div>
(By the way, it's worth noting in passing that expressions like "<span class="inlinecode">1</span>
<span class="inlinecode">+</span> <span class="inlinecode">2</span> <span class="inlinecode">::</span> <span class="inlinecode">[3]</span>" can be a little confusing when you read them in a .v
file. The inner brackets, around 3, indicate a list, but the outer
brackets, which are invisible in the HTML rendering, are there to
instruct the "coqdoc" tool that the bracketed part should be
displayed as Coq code rather than running text.)
<div class="paragraph"> </div>
The second and third <span class="inlinecode"><span class="id" type="keyword">Notation</span></span> declarations above introduce the
standard square-bracket notation for lists; the right-hand side of
the third one illustrates Coq's syntax for declaring n-ary
notations and translating them to nested sequences of binary
constructors.
<div class="paragraph"> </div>
<a name="lab68"></a><h3 class="section">Repeat</h3>
A number of functions are useful for manipulating lists.
For example, the <span class="inlinecode"><span class="id" type="tactic">repeat</span></span> function takes a number <span class="inlinecode"><span class="id" type="var">n</span></span> and a
<span class="inlinecode"><span class="id" type="var">count</span></span> and returns a list of length <span class="inlinecode"><span class="id" type="var">count</span></span> where every element
is <span class="inlinecode"><span class="id" type="var">n</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="tactic">repeat</span> (<span class="id" type="var">n</span> <span class="id" type="var">count</span> : <span class="id" type="var">nat</span>) : <span class="id" type="var">natlist</span> := <br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">count</span> <span class="id" type="keyword">with</span><br/>
| <span class="id" type="var">O</span> ⇒ <span class="id" type="var">nil</span><br/>
| <span class="id" type="var">S</span> <span class="id" type="var">count'</span> ⇒ <span class="id" type="var">n</span> :: (<span class="id" type="tactic">repeat</span> <span class="id" type="var">n</span> <span class="id" type="var">count'</span>)<br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab69"></a><h3 class="section">Length</h3>
The <span class="inlinecode"><span class="id" type="var">length</span></span> function calculates the length of a list.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">length</span> (<span class="id" type="var">l</span>:<span class="id" type="var">natlist</span>) : <span class="id" type="var">nat</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">nil</span> ⇒ <span class="id" type="var">O</span><br/>
| <span class="id" type="var">h</span> :: <span class="id" type="var">t</span> ⇒ <span class="id" type="var">S</span> (<span class="id" type="var">length</span> <span class="id" type="var">t</span>)<br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab70"></a><h3 class="section">Append</h3>
The <span class="inlinecode"><span class="id" type="var">app</span></span> ("append") function concatenates two lists.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">app</span> (<span class="id" type="var">l1</span> <span class="id" type="var">l2</span> : <span class="id" type="var">natlist</span>) : <span class="id" type="var">natlist</span> := <br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">l1</span> <span class="id" type="keyword">with</span><br/>
| <span class="id" type="var">nil</span> ⇒ <span class="id" type="var">l2</span><br/>
| <span class="id" type="var">h</span> :: <span class="id" type="var">t</span> ⇒ <span class="id" type="var">h</span> :: (<span class="id" type="var">app</span> <span class="id" type="var">t</span> <span class="id" type="var">l2</span>)<br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
</div>
<div class="doc">
Actually, <span class="inlinecode"><span class="id" type="var">app</span></span> will be used a lot in some parts of what
follows, so it is convenient to have an infix operator for it.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Notation</span> "x ++ y" := (<span class="id" type="var">app</span> <span class="id" type="var">x</span> <span class="id" type="var">y</span>) <br/>
(<span class="id" type="var">right</span> <span class="id" type="var">associativity</span>, <span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 60).<br/>
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_app1</span>: [1;2;3] ++ [4;5] = [1;2;3;4;5].<br/>
<span class="id" type="keyword">Proof</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_app2</span>: <span class="id" type="var">nil</span> ++ [4;5] = [4;5].<br/>
<span class="id" type="keyword">Proof</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_app3</span>: [1;2;3] ++ <span class="id" type="var">nil</span> = [1;2;3].<br/>
<span class="id" type="keyword">Proof</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Here are two smaller examples of programming with lists.
The <span class="inlinecode"><span class="id" type="var">hd</span></span> function returns the first element (the "head") of the
list, while <span class="inlinecode"><span class="id" type="var">tl</span></span> returns everything but the first
element (the "tail").
Of course, the empty list has no first element, so we
must pass a default value to be returned in that case.
<div class="paragraph"> </div>
<a name="lab71"></a><h3 class="section">Head (with default) and Tail</h3>
</div>
<div class="code code-space">
<span class="id" type="keyword">Definition</span> <span class="id" type="var">hd</span> (<span class="id" type="var">default</span>:<span class="id" type="var">nat</span>) (<span class="id" type="var">l</span>:<span class="id" type="var">natlist</span>) : <span class="id" type="var">nat</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">nil</span> ⇒ <span class="id" type="var">default</span><br/>
| <span class="id" type="var">h</span> :: <span class="id" type="var">t</span> ⇒ <span class="id" type="var">h</span><br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">tl</span> (<span class="id" type="var">l</span>:<span class="id" type="var">natlist</span>) : <span class="id" type="var">natlist</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">nil</span> ⇒ <span class="id" type="var">nil</span> <br/>
| <span class="id" type="var">h</span> :: <span class="id" type="var">t</span> ⇒ <span class="id" type="var">t</span><br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_hd1</span>: <span class="id" type="var">hd</span> 0 [1;2;3] = 1.<br/>
<span class="id" type="keyword">Proof</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_hd2</span>: <span class="id" type="var">hd</span> 0 [] = 0.<br/>
<span class="id" type="keyword">Proof</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_tl</span>: <span class="id" type="var">tl</span> [1;2;3] = [2;3].<br/>
<span class="id" type="keyword">Proof</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab72"></a><h4 class="section">Exercise: 2 stars (list_funs)</h4>
Complete the definitions of <span class="inlinecode"><span class="id" type="var">nonzeros</span></span>, <span class="inlinecode"><span class="id" type="var">oddmembers</span></span> and
<span class="inlinecode"><span class="id" type="var">countoddmembers</span></span> below. Have a look at the tests to understand
what these functions should do.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">nonzeros</span> (<span class="id" type="var">l</span>:<span class="id" type="var">natlist</span>) : <span class="id" type="var">natlist</span> :=<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">admit</span>.<br/>
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_nonzeros</span>: <span class="id" type="var">nonzeros</span> [0;1;0;2;3;0;0] = [1;2;3].<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">oddmembers</span> (<span class="id" type="var">l</span>:<span class="id" type="var">natlist</span>) : <span class="id" type="var">natlist</span> :=<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">admit</span>.<br/>
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_oddmembers</span>: <span class="id" type="var">oddmembers</span> [0;1;0;2;3;0;0] = [1;3].<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">countoddmembers</span> (<span class="id" type="var">l</span>:<span class="id" type="var">natlist</span>) : <span class="id" type="var">nat</span> :=<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">admit</span>.<br/>
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_countoddmembers1</span>: <span class="id" type="var">countoddmembers</span> [1;0;3;1;4;5] = 4.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_countoddmembers2</span>: <span class="id" type="var">countoddmembers</span> [0;2;4] = 0.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_countoddmembers3</span>: <span class="id" type="var">countoddmembers</span> <span class="id" type="var">nil</span> = 0.<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="lab73"></a><h4 class="section">Exercise: 3 stars, advanced (alternate)</h4>
Complete the definition of <span class="inlinecode"><span class="id" type="var">alternate</span></span>, which "zips up" two lists
into one, alternating between elements taken from the first list
and elements from the second. See the tests below for more
specific examples.
<div class="paragraph"> </div>
Note: one natural and elegant way of writing <span class="inlinecode"><span class="id" type="var">alternate</span></span> will fail
to satisfy Coq's requirement that all <span class="inlinecode"><span class="id" type="keyword">Fixpoint</span></span> definitions be
"obviously terminating." If you find yourself in this rut, look
for a slightly more verbose solution that considers elements of
both lists at the same time. (One possible solution requires
defining a new kind of pairs, but this is not the only way.)
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">alternate</span> (<span class="id" type="var">l1</span> <span class="id" type="var">l2</span> : <span class="id" type="var">natlist</span>) : <span class="id" type="var">natlist</span> :=<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">admit</span>.<br/>
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_alternate1</span>: <span class="id" type="var">alternate</span> [1;2;3] [4;5;6] = [1;4;2;5;3;6].<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_alternate2</span>: <span class="id" type="var">alternate</span> [1] [4;5;6] = [1;4;5;6].<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_alternate3</span>: <span class="id" type="var">alternate</span> [1;2;3] [4] = [1;4;2;3].<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_alternate4</span>: <span class="id" type="var">alternate</span> [] [20;30] = [20;30].<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="lab74"></a><h2 class="section">Bags via Lists</h2>
<div class="paragraph"> </div>
A <span class="inlinecode"><span class="id" type="var">bag</span></span> (or <span class="inlinecode"><span class="id" type="var">multiset</span></span>) is like a set, but each element can appear
multiple times instead of just once. One reasonable
implementation of bags is to represent a bag of numbers as a
list.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">bag</span> := <span class="id" type="var">natlist</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab75"></a><h4 class="section">Exercise: 3 stars (bag_functions)</h4>
Complete the following definitions for the functions
<span class="inlinecode"><span class="id" type="var">count</span></span>, <span class="inlinecode"><span class="id" type="var">sum</span></span>, <span class="inlinecode"><span class="id" type="var">add</span></span>, and <span class="inlinecode"><span class="id" type="var">member</span></span> for bags.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">count</span> (<span class="id" type="var">v</span>:<span class="id" type="var">nat</span>) (<span class="id" type="var">s</span>:<span class="id" type="var">bag</span>) : <span class="id" type="var">nat</span> := <br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">admit</span>.<br/>
<br/>
</div>
<div class="doc">
All these proofs can be done just by <span class="inlinecode"><span class="id" type="tactic">reflexivity</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_count1</span>: <span class="id" type="var">count</span> 1 [1;2;3;1;4;1] = 3.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_count2</span>: <span class="id" type="var">count</span> 6 [1;2;3;1;4;1] = 0.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
</div>
<div class="doc">
Multiset <span class="inlinecode"><span class="id" type="var">sum</span></span> is similar to set <span class="inlinecode"><span class="id" type="var">union</span></span>: <span class="inlinecode"><span class="id" type="var">sum</span></span> <span class="inlinecode"><span class="id" type="var">a</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> contains
all the elements of <span class="inlinecode"><span class="id" type="var">a</span></span> and of <span class="inlinecode"><span class="id" type="var">b</span></span>. (Mathematicians usually
define <span class="inlinecode"><span class="id" type="var">union</span></span> on multisets a little bit differently, which
is why we don't use that name for this operation.)
For <span class="inlinecode"><span class="id" type="var">sum</span></span> we're giving you a header that does not give explicit
names to the arguments. Moreover, it uses the keyword
<span class="inlinecode"><span class="id" type="keyword">Definition</span></span> instead of <span class="inlinecode"><span class="id" type="keyword">Fixpoint</span></span>, so even if you had names for
the arguments, you wouldn't be able to process them recursively.
The point of stating the question this way is to encourage you to
think about whether <span class="inlinecode"><span class="id" type="var">sum</span></span> can be implemented in another way —
perhaps by using functions that have already been defined.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">sum</span> : <span class="id" type="var">bag</span> <span style="font-family: arial;">→</span> <span class="id" type="var">bag</span> <span style="font-family: arial;">→</span> <span class="id" type="var">bag</span> := <br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">admit</span>.<br/>
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_sum1</span>: <span class="id" type="var">count</span> 1 (<span class="id" type="var">sum</span> [1;2;3] [1;4;1]) = 3.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">add</span> (<span class="id" type="var">v</span>:<span class="id" type="var">nat</span>) (<span class="id" type="var">s</span>:<span class="id" type="var">bag</span>) : <span class="id" type="var">bag</span> := <br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">admit</span>.<br/>
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_add1</span>: <span class="id" type="var">count</span> 1 (<span class="id" type="var">add</span> 1 [1;4;1]) = 3.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_add2</span>: <span class="id" type="var">count</span> 5 (<span class="id" type="var">add</span> 1 [1;4;1]) = 0.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">member</span> (<span class="id" type="var">v</span>:<span class="id" type="var">nat</span>) (<span class="id" type="var">s</span>:<span class="id" type="var">bag</span>) : <span class="id" type="var">bool</span> := <br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">admit</span>.<br/>
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_member1</span>: <span class="id" type="var">member</span> 1 [1;4;1] = <span class="id" type="var">true</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_member2</span>: <span class="id" type="var">member</span> 2 [1;4;1] = <span class="id" type="var">false</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="lab76"></a><h4 class="section">Exercise: 3 stars, optional (bag_more_functions)</h4>
Here are some more bag functions for you to practice with.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">remove_one</span> (<span class="id" type="var">v</span>:<span class="id" type="var">nat</span>) (<span class="id" type="var">s</span>:<span class="id" type="var">bag</span>) : <span class="id" type="var">bag</span> :=<br/>
<span class="comment">(* When remove_one is applied to a bag without the number to remove,<br/>
it should return the same bag unchanged. *)</span><br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">admit</span>.<br/>
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_remove_one1</span>: <span class="id" type="var">count</span> 5 (<span class="id" type="var">remove_one</span> 5 [2;1;5;4;1]) = 0.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_remove_one2</span>: <span class="id" type="var">count</span> 5 (<span class="id" type="var">remove_one</span> 5 [2;1;4;1]) = 0.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_remove_one3</span>: <span class="id" type="var">count</span> 4 (<span class="id" type="var">remove_one</span> 5 [2;1;4;5;1;4]) = 2.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_remove_one4</span>: <span class="id" type="var">count</span> 5 (<span class="id" type="var">remove_one</span> 5 [2;1;5;4;5;1;4]) = 1.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">remove_all</span> (<span class="id" type="var">v</span>:<span class="id" type="var">nat</span>) (<span class="id" type="var">s</span>:<span class="id" type="var">bag</span>) : <span class="id" type="var">bag</span> :=<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">admit</span>.<br/>
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_remove_all1</span>: <span class="id" type="var">count</span> 5 (<span class="id" type="var">remove_all</span> 5 [2;1;5;4;1]) = 0.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_remove_all2</span>: <span class="id" type="var">count</span> 5 (<span class="id" type="var">remove_all</span> 5 [2;1;4;1]) = 0.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_remove_all3</span>: <span class="id" type="var">count</span> 4 (<span class="id" type="var">remove_all</span> 5 [2;1;4;5;1;4]) = 2.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_remove_all4</span>: <span class="id" type="var">count</span> 5 (<span class="id" type="var">remove_all</span> 5 [2;1;5;4;5;1;4;5;1;4]) = 0.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">subset</span> (<span class="id" type="var">s<sub>1</sub></span>:<span class="id" type="var">bag</span>) (<span class="id" type="var">s<sub>2</sub></span>:<span class="id" type="var">bag</span>) : <span class="id" type="var">bool</span> :=<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">admit</span>.<br/>
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_subset1</span>: <span class="id" type="var">subset</span> [1;2] [2;1;4;1] = <span class="id" type="var">true</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_subset2</span>: <span class="id" type="var">subset</span> [1;2;2] [2;1;4;1] = <span class="id" type="var">false</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="lab77"></a><h4 class="section">Exercise: 3 stars (bag_theorem)</h4>
Write down an interesting theorem <span class="inlinecode"><span class="id" type="var">bag_theorem</span></span> about bags involving
the functions <span class="inlinecode"><span class="id" type="var">count</span></span> and <span class="inlinecode"><span class="id" type="var">add</span></span>, and prove it. Note that, since this
problem is somewhat open-ended, it's possible that you may come up
with a theorem which is true, but whose proof requires techniques
you haven't learned yet. Feel free to ask for help if you get
stuck!
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
</div>
<div class="doc">
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab78"></a><h1 class="section">Reasoning About Lists</h1>
<div class="paragraph"> </div>
Just as with numbers, simple facts about list-processing
functions can sometimes be proved entirely by simplification. For
example, the simplification performed by <span class="inlinecode"><span class="id" type="tactic">reflexivity</span></span> is enough
for this theorem...
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">nil_app</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">l</span>:<span class="id" type="var">natlist</span>,<br/>
[] ++ <span class="id" type="var">l</span> = <span class="id" type="var">l</span>.<br/>
<span class="id" type="keyword">Proof</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
... because the <span class="inlinecode">[]</span> is substituted into the match position
in the definition of <span class="inlinecode"><span class="id" type="var">app</span></span>, allowing the match itself to be
simplified.
<div class="paragraph"> </div>
Also, as with numbers, it is sometimes helpful to perform case
analysis on the possible shapes (empty or non-empty) of an unknown
list.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">tl_length_pred</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">l</span>:<span class="id" type="var">natlist</span>,<br/>
<span class="id" type="var">pred</span> (<span class="id" type="var">length</span> <span class="id" type="var">l</span>) = <span class="id" type="var">length</span> (<span class="id" type="var">tl</span> <span class="id" type="var">l</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">l</span>. <span class="id" type="tactic">destruct</span> <span class="id" type="var">l</span> <span class="id" type="keyword">as</span> [| <span class="id" type="var">n</span> <span class="id" type="var">l'</span>].<br/>
<span class="id" type="var">Case</span> "l = nil".<br/>
<span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">Case</span> "l = cons n l'".<br/>
<span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Here, the <span class="inlinecode"><span class="id" type="var">nil</span></span> case works because we've chosen to define
<span class="inlinecode"><span class="id" type="var">tl</span></span> <span class="inlinecode"><span class="id" type="var">nil</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">nil</span></span>. Notice that the <span class="inlinecode"><span class="id" type="keyword">as</span></span> annotation on the <span class="inlinecode"><span class="id" type="tactic">destruct</span></span>
tactic here introduces two names, <span class="inlinecode"><span class="id" type="var">n</span></span> and <span class="inlinecode"><span class="id" type="var">l'</span></span>, corresponding to
the fact that the <span class="inlinecode"><span class="id" type="var">cons</span></span> constructor for lists takes two
arguments (the head and tail of the list it is constructing).
<div class="paragraph"> </div>
Usually, though, interesting theorems about lists require
induction for their proofs.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab79"></a><h2 class="section">Micro-Sermon</h2>
<div class="paragraph"> </div>
Simply reading example proof scripts will not get you very far!
It is very important to work through the details of each one,
using Coq and thinking about what each step achieves. Otherwise
it is more or less guaranteed that the exercises will make no
sense...
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab80"></a><h2 class="section">Induction on Lists</h2>
<div class="paragraph"> </div>
Proofs by induction over datatypes like <span class="inlinecode"><span class="id" type="var">natlist</span></span> are
perhaps a little less familiar than standard natural number
induction, but the basic idea is equally simple. Each <span class="inlinecode"><span class="id" type="keyword">Inductive</span></span>
declaration defines a set of data values that can be built up from
the declared constructors: a boolean can be either <span class="inlinecode"><span class="id" type="var">true</span></span> or
<span class="inlinecode"><span class="id" type="var">false</span></span>; a number can be either <span class="inlinecode"><span class="id" type="var">O</span></span> or <span class="inlinecode"><span class="id" type="var">S</span></span> applied to a number; a
list can be either <span class="inlinecode"><span class="id" type="var">nil</span></span> or <span class="inlinecode"><span class="id" type="var">cons</span></span> applied to a number and a list.
<div class="paragraph"> </div>
Moreover, applications of the declared constructors to one another
are the <i>only</i> possible shapes that elements of an inductively
defined set can have, and this fact directly gives rise to a way
of reasoning about inductively defined sets: a number is either
<span class="inlinecode"><span class="id" type="var">O</span></span> or else it is <span class="inlinecode"><span class="id" type="var">S</span></span> applied to some <i>smaller</i> number; a list is
either <span class="inlinecode"><span class="id" type="var">nil</span></span> or else it is <span class="inlinecode"><span class="id" type="var">cons</span></span> applied to some number and some
<i>smaller</i> list; etc. So, if we have in mind some proposition <span class="inlinecode"><span class="id" type="var">P</span></span>
that mentions a list <span class="inlinecode"><span class="id" type="var">l</span></span> and we want to argue that <span class="inlinecode"><span class="id" type="var">P</span></span> holds for
<i>all</i> lists, we can reason as follows:
<div class="paragraph"> </div>
<ul class="doclist">
<li> First, show that <span class="inlinecode"><span class="id" type="var">P</span></span> is true of <span class="inlinecode"><span class="id" type="var">l</span></span> when <span class="inlinecode"><span class="id" type="var">l</span></span> is <span class="inlinecode"><span class="id" type="var">nil</span></span>.
<div class="paragraph"> </div>
</li>
<li> Then show that <span class="inlinecode"><span class="id" type="var">P</span></span> is true of <span class="inlinecode"><span class="id" type="var">l</span></span> when <span class="inlinecode"><span class="id" type="var">l</span></span> is <span class="inlinecode"><span class="id" type="var">cons</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> <span class="inlinecode"><span class="id" type="var">l'</span></span> for
some number <span class="inlinecode"><span class="id" type="var">n</span></span> and some smaller list <span class="inlinecode"><span class="id" type="var">l'</span></span>, assuming that <span class="inlinecode"><span class="id" type="var">P</span></span>
is true for <span class="inlinecode"><span class="id" type="var">l'</span></span>.
</li>
</ul>
<div class="paragraph"> </div>
Since larger lists can only be built up from smaller ones,
eventually reaching <span class="inlinecode"><span class="id" type="var">nil</span></span>, these two things together establish the
truth of <span class="inlinecode"><span class="id" type="var">P</span></span> for all lists <span class="inlinecode"><span class="id" type="var">l</span></span>. Here's a concrete example:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">app_assoc</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">l1</span> <span class="id" type="var">l2</span> <span class="id" type="var">l3</span> : <span class="id" type="var">natlist</span>, <br/>
(<span class="id" type="var">l1</span> ++ <span class="id" type="var">l2</span>) ++ <span class="id" type="var">l3</span> = <span class="id" type="var">l1</span> ++ (<span class="id" type="var">l2</span> ++ <span class="id" type="var">l3</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">l1</span> <span class="id" type="var">l2</span> <span class="id" type="var">l3</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">n</span> <span class="id" type="var">l1'</span>].<br/>
<span class="id" type="var">Case</span> "l1 = nil".<br/>
<span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">Case</span> "l1 = cons n l1'".<br/>
<span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">→</span> <span class="id" type="var">IHl1'</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Again, this Coq proof is not especially illuminating as a
static written document — it is easy to see what's going on if
you are reading the proof in an interactive Coq session and you
can see the current goal and context at each point, but this state
is not visible in the written-down parts of the Coq proof. So a
natural-language proof — one written for human readers — will
need to include more explicit signposts; in particular, it will
help the reader stay oriented if we remind them exactly what the
induction hypothesis is in the second case.
<div class="paragraph"> </div>
<a name="lab81"></a><h3 class="section">Informal version</h3>
<div class="paragraph"> </div>
<i>Theorem</i>: For all lists <span class="inlinecode"><span class="id" type="var">l1</span></span>, <span class="inlinecode"><span class="id" type="var">l2</span></span>, and <span class="inlinecode"><span class="id" type="var">l3</span></span>,
<span class="inlinecode">(<span class="id" type="var">l1</span></span> <span class="inlinecode">++</span> <span class="inlinecode"><span class="id" type="var">l2</span>)</span> <span class="inlinecode">++</span> <span class="inlinecode"><span class="id" type="var">l3</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">l1</span></span> <span class="inlinecode">++</span> <span class="inlinecode">(<span class="id" type="var">l2</span></span> <span class="inlinecode">++</span> <span class="inlinecode"><span class="id" type="var">l3</span>)</span>.
<div class="paragraph"> </div>
<i>Proof</i>: By induction on <span class="inlinecode"><span class="id" type="var">l1</span></span>.
<div class="paragraph"> </div>
<ul class="doclist">
<li> First, suppose <span class="inlinecode"><span class="id" type="var">l1</span></span> <span class="inlinecode">=</span> <span class="inlinecode">[]</span>. We must show
<div class="paragraph"> </div>
<div class="code code-tight">
([] ++ <span class="id" type="var">l2</span>) ++ <span class="id" type="var">l3</span> = [] ++ (<span class="id" type="var">l2</span> ++ <span class="id" type="var">l3</span>),
<div class="paragraph"> </div>
</div>
which follows directly from the definition of <span class="inlinecode">++</span>.
<div class="paragraph"> </div>
</li>
<li> Next, suppose <span class="inlinecode"><span class="id" type="var">l1</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">n</span>::<span class="id" type="var">l1'</span></span>, with
<div class="paragraph"> </div>
<div class="code code-tight">
(<span class="id" type="var">l1'</span> ++ <span class="id" type="var">l2</span>) ++ <span class="id" type="var">l3</span> = <span class="id" type="var">l1'</span> ++ (<span class="id" type="var">l2</span> ++ <span class="id" type="var">l3</span>)
<div class="paragraph"> </div>
</div>
(the induction hypothesis). We must show
<div class="paragraph"> </div>
<div class="code code-tight">
((<span class="id" type="var">n</span> :: <span class="id" type="var">l1'</span>) ++ <span class="id" type="var">l2</span>) ++ <span class="id" type="var">l3</span> = (<span class="id" type="var">n</span> :: <span class="id" type="var">l1'</span>) ++ (<span class="id" type="var">l2</span> ++ <span class="id" type="var">l3</span>).
<div class="paragraph"> </div>
</div>
By the definition of <span class="inlinecode">++</span>, this follows from
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">n</span> :: ((<span class="id" type="var">l1'</span> ++ <span class="id" type="var">l2</span>) ++ <span class="id" type="var">l3</span>) = <span class="id" type="var">n</span> :: (<span class="id" type="var">l1'</span> ++ (<span class="id" type="var">l2</span> ++ <span class="id" type="var">l3</span>)),
<div class="paragraph"> </div>
</div>
which is immediate from the induction hypothesis. <font size=-2>☐</font>
</li>
</ul>
<div class="paragraph"> </div>
<a name="lab82"></a><h3 class="section">Another example</h3>
<div class="paragraph"> </div>
Here is a similar example to be worked together in class:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">app_length</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">l1</span> <span class="id" type="var">l2</span> : <span class="id" type="var">natlist</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">(* WORKED IN CLASS *)</span><br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">l1</span> <span class="id" type="var">l2</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">n</span> <span class="id" type="var">l1'</span>].<br/>
<span class="id" type="var">Case</span> "l1 = nil".<br/>
<span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">Case</span> "l1 = cons".<br/>
<span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">→</span> <span class="id" type="var">IHl1'</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab83"></a><h3 class="section">Reversing a list</h3>
For a slightly more involved example of an inductive proof
over lists, suppose we define a "cons on the right" function
<span class="inlinecode"><span class="id" type="var">snoc</span></span> like this...
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">snoc</span> (<span class="id" type="var">l</span>:<span class="id" type="var">natlist</span>) (<span class="id" type="var">v</span>:<span class="id" type="var">nat</span>) : <span class="id" type="var">natlist</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">nil</span> ⇒ [<span class="id" type="var">v</span>]<br/>
| <span class="id" type="var">h</span> :: <span class="id" type="var">t</span> ⇒ <span class="id" type="var">h</span> :: (<span class="id" type="var">snoc</span> <span class="id" type="var">t</span> <span class="id" type="var">v</span>)<br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
</div>
<div class="doc">
... and use it to define a list-reversing function <span class="inlinecode"><span class="id" type="var">rev</span></span>
like this:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">rev</span> (<span class="id" type="var">l</span>:<span class="id" type="var">natlist</span>) : <span class="id" type="var">natlist</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">nil</span> ⇒ <span class="id" type="var">nil</span><br/>
| <span class="id" type="var">h</span> :: <span class="id" type="var">t</span> ⇒ <span class="id" type="var">snoc</span> (<span class="id" type="var">rev</span> <span class="id" type="var">t</span>) <span class="id" type="var">h</span><br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_rev1</span>: <span class="id" type="var">rev</span> [1;2;3] = [3;2;1].<br/>
<span class="id" type="keyword">Proof</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_rev2</span>: <span class="id" type="var">rev</span> <span class="id" type="var">nil</span> = <span class="id" type="var">nil</span>.<br/>
<span class="id" type="keyword">Proof</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab84"></a><h3 class="section">Proofs about reverse</h3>
Now let's prove some more list theorems using our newly
defined <span class="inlinecode"><span class="id" type="var">snoc</span></span> and <span class="inlinecode"><span class="id" type="var">rev</span></span>. For something a little more challenging
than the inductive proofs we've seen so far, let's prove that
reversing a list does not change its length. Our first attempt at
this proof gets stuck in the successor case...
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">rev_length_firsttry</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">l</span> : <span class="id" type="var">natlist</span>,<br/>
<span class="id" type="var">length</span> (<span class="id" type="var">rev</span> <span class="id" type="var">l</span>) = <span class="id" type="var">length</span> <span class="id" type="var">l</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">l</span>. <span class="id" type="tactic">induction</span> <span class="id" type="var">l</span> <span class="id" type="keyword">as</span> [| <span class="id" type="var">n</span> <span class="id" type="var">l'</span>].<br/>
<span class="id" type="var">Case</span> "l = []".<br/>
<span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">Case</span> "l = n :: l'".<br/>
<span class="comment">(* This is the tricky case. Let's begin as usual <br/>
by simplifying. *)</span><br/>
<span class="id" type="tactic">simpl</span>.<br/>
<span class="comment">(* Now we seem to be stuck: the goal is an equality <br/>