-
Notifications
You must be signed in to change notification settings - Fork 1
/
Poly.html
1860 lines (1450 loc) · 129 KB
/
Poly.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>Poly: Polymorphism and Higher-Order Functions</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">Poly<span class="subtitle">Polymorphism and Higher-Order Functions</span></h1>
<div class="code code-tight">
</div>
<div class="doc">
<div class="paragraph"> </div>
In this chapter we continue our development of basic
concepts of functional programming. The critical new ideas are
<i>polymorphism</i> (abstracting functions over the types of the data
they manipulate) and <i>higher-order functions</i> (treating functions
as data).
</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="var">Lists</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab104"></a><h1 class="section">Polymorphism</h1>
</div>
<div class="code code-space">
</div>
<div class="doc">
<a name="lab105"></a><h2 class="section">Polymorphic Lists</h2>
<div class="paragraph"> </div>
For the last couple of chapters, we've been working just
with lists of numbers. Obviously, interesting programs also need
to be able to manipulate lists with elements from other types —
lists of strings, lists of booleans, lists of lists, etc. We
<i>could</i> just define a new inductive datatype for each of these,
for example...
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">boollist</span> : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">bool_nil</span> : <span class="id" type="var">boollist</span><br/>
| <span class="id" type="var">bool_cons</span> : <span class="id" type="var">bool</span> <span style="font-family: arial;">→</span> <span class="id" type="var">boollist</span> <span style="font-family: arial;">→</span> <span class="id" type="var">boollist</span>.<br/>
<br/>
</div>
<div class="doc">
... but this would quickly become tedious, partly because we
have to make up different constructor names for each datatype, but
mostly because we would also need to define new versions of all
our list manipulating functions (<span class="inlinecode"><span class="id" type="var">length</span></span>, <span class="inlinecode"><span class="id" type="var">rev</span></span>, etc.) for each
new datatype definition.
<div class="paragraph"> </div>
<a name="lab106"></a><h3 class="section"> </h3>
<div class="paragraph"> </div>
To avoid all this repetition, Coq supports <i>polymorphic</i>
inductive type definitions. For example, here is a <i>polymorphic
list</i> datatype.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">list</span> (<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">nil</span> : <span class="id" type="var">list</span> <span class="id" type="var">X</span><br/>
| <span class="id" type="var">cons</span> : <span class="id" type="var">X</span> <span style="font-family: arial;">→</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="var">list</span> <span class="id" type="var">X</span>.<br/>
<br/>
</div>
<div class="doc">
This is exactly like the definition of <span class="inlinecode"><span class="id" type="var">natlist</span></span> from the
previous chapter, except that the <span class="inlinecode"><span class="id" type="var">nat</span></span> argument to the <span class="inlinecode"><span class="id" type="var">cons</span></span>
constructor has been replaced by an arbitrary type <span class="inlinecode"><span class="id" type="var">X</span></span>, a binding
for <span class="inlinecode"><span class="id" type="var">X</span></span> has been added to the header, and the occurrences of
<span class="inlinecode"><span class="id" type="var">natlist</span></span> in the types of the constructors have been replaced by
<span class="inlinecode"><span class="id" type="var">list</span></span> <span class="inlinecode"><span class="id" type="var">X</span></span>. (We can re-use the constructor names <span class="inlinecode"><span class="id" type="var">nil</span></span> and <span class="inlinecode"><span class="id" type="var">cons</span></span>
because the earlier definition of <span class="inlinecode"><span class="id" type="var">natlist</span></span> was inside of a
<span class="inlinecode"><span class="id" type="keyword">Module</span></span> definition that is now out of scope.)
<div class="paragraph"> </div>
What sort of thing is <span class="inlinecode"><span class="id" type="var">list</span></span> itself? One good way to think
about it is that <span class="inlinecode"><span class="id" type="var">list</span></span> is a <i>function</i> from <span class="inlinecode"><span class="id" type="keyword">Type</span></span>s to
<span class="inlinecode"><span class="id" type="keyword">Inductive</span></span> definitions; or, to put it another way, <span class="inlinecode"><span class="id" type="var">list</span></span> is a
function from <span class="inlinecode"><span class="id" type="keyword">Type</span></span>s to <span class="inlinecode"><span class="id" type="keyword">Type</span></span>s. For any particular type <span class="inlinecode"><span class="id" type="var">X</span></span>,
the type <span class="inlinecode"><span class="id" type="var">list</span></span> <span class="inlinecode"><span class="id" type="var">X</span></span> is an <span class="inlinecode"><span class="id" type="keyword">Inductive</span></span>ly defined set of lists whose
elements are things of type <span class="inlinecode"><span class="id" type="var">X</span></span>.
<div class="paragraph"> </div>
With this definition, when we use the constructors <span class="inlinecode"><span class="id" type="var">nil</span></span> and
<span class="inlinecode"><span class="id" type="var">cons</span></span> to build lists, we need to tell Coq the type of the
elements in the lists we are building — that is, <span class="inlinecode"><span class="id" type="var">nil</span></span> and <span class="inlinecode"><span class="id" type="var">cons</span></span>
are now <i>polymorphic constructors</i>. Observe the types of these
constructors:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Check</span> <span class="id" type="var">nil</span>.<br/>
<span class="comment">(* ===> nil : forall X : Type, list X *)</span><br/>
<span class="id" type="keyword">Check</span> <span class="id" type="var">cons</span>.<br/>
<span class="comment">(* ===> cons : forall X : Type, X -> list X -> list X *)</span><br/>
<br/>
</div>
<div class="doc">
The "<span class="inlinecode"><span style="font-family: arial;">∀</span></span> <span class="inlinecode"><span class="id" type="var">X</span></span>" in these types can be read as an additional
argument to the constructors that determines the expected types of
the arguments that follow. When <span class="inlinecode"><span class="id" type="var">nil</span></span> and <span class="inlinecode"><span class="id" type="var">cons</span></span> are used, these
arguments are supplied in the same way as the others. For
example, the list containing <span class="inlinecode">2</span> and <span class="inlinecode">1</span> is written like this:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Check</span> (<span class="id" type="var">cons</span> <span class="id" type="var">nat</span> 2 (<span class="id" type="var">cons</span> <span class="id" type="var">nat</span> 1 (<span class="id" type="var">nil</span> <span class="id" type="var">nat</span>))).<br/>
<br/>
</div>
<div class="doc">
(We've gone back to writing <span class="inlinecode"><span class="id" type="var">nil</span></span> and <span class="inlinecode"><span class="id" type="var">cons</span></span> explicitly here
because we haven't yet defined the <span class="inlinecode"></span> <span class="inlinecode">[]</span> <span class="inlinecode"></span> and <span class="inlinecode">::</span> notations for
the new version of lists. We'll do that in a bit.)
<div class="paragraph"> </div>
We can now go back and make polymorphic (or "generic")
versions of all the list-processing functions that we wrote
before. Here is <span class="inlinecode"><span class="id" type="var">length</span></span>, for example:
<div class="paragraph"> </div>
<a name="lab107"></a><h3 class="section"> </h3>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">length</span> (<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</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">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> ⇒ 0<br/>
| <span class="id" type="var">cons</span> <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">X</span> <span class="id" type="var">t</span>)<br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
</div>
<div class="doc">
Note that the uses of <span class="inlinecode"><span class="id" type="var">nil</span></span> and <span class="inlinecode"><span class="id" type="var">cons</span></span> in <span class="inlinecode"><span class="id" type="keyword">match</span></span> patterns
do not require any type annotations: Coq already knows that the list
<span class="inlinecode"><span class="id" type="var">l</span></span> contains elements of type <span class="inlinecode"><span class="id" type="var">X</span></span>, so there's no reason to include
<span class="inlinecode"><span class="id" type="var">X</span></span> in the pattern. (More precisely, the type <span class="inlinecode"><span class="id" type="var">X</span></span> is a parameter
of the whole definition of <span class="inlinecode"><span class="id" type="var">list</span></span>, not of the individual
constructors. We'll come back to this point later.)
<div class="paragraph"> </div>
As with <span class="inlinecode"><span class="id" type="var">nil</span></span> and <span class="inlinecode"><span class="id" type="var">cons</span></span>, we can use <span class="inlinecode"><span class="id" type="var">length</span></span> by applying it first
to a type and then to its list argument:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_length1</span> :<br/>
<span class="id" type="var">length</span> <span class="id" type="var">nat</span> (<span class="id" type="var">cons</span> <span class="id" type="var">nat</span> 1 (<span class="id" type="var">cons</span> <span class="id" type="var">nat</span> 2 (<span class="id" type="var">nil</span> <span class="id" type="var">nat</span>))) = 2.<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">
To use our length with other kinds of lists, we simply
instantiate it with an appropriate type parameter:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_length2</span> :<br/>
<span class="id" type="var">length</span> <span class="id" type="var">bool</span> (<span class="id" type="var">cons</span> <span class="id" type="var">bool</span> <span class="id" type="var">true</span> (<span class="id" type="var">nil</span> <span class="id" type="var">bool</span>)) = 1.<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="lab108"></a><h3 class="section"> </h3>
Let's close this subsection by re-implementing a few other
standard list functions on our new polymorphic 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">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">list</span> <span class="id" type="var">X</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">cons</span> <span class="id" type="var">h</span> <span class="id" type="var">t</span> ⇒ <span class="id" type="var">cons</span> <span class="id" type="var">X</span> <span class="id" type="var">h</span> (<span class="id" type="var">app</span> <span class="id" type="var">X</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/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">snoc</span> (<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</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">v</span>:<span class="id" type="var">X</span>) : (<span class="id" type="var">list</span> <span class="id" type="var">X</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">cons</span> <span class="id" type="var">X</span> <span class="id" type="var">v</span> (<span class="id" type="var">nil</span> <span class="id" type="var">X</span>)<br/>
| <span class="id" type="var">cons</span> <span class="id" type="var">h</span> <span class="id" type="var">t</span> ⇒ <span class="id" type="var">cons</span> <span class="id" type="var">X</span> <span class="id" type="var">h</span> (<span class="id" type="var">snoc</span> <span class="id" type="var">X</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/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">rev</span> (<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</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">list</span> <span class="id" type="var">X</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> <span class="id" type="var">X</span><br/>
| <span class="id" type="var">cons</span> <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">X</span> (<span class="id" type="var">rev</span> <span class="id" type="var">X</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> :<br/>
<span class="id" type="var">rev</span> <span class="id" type="var">nat</span> (<span class="id" type="var">cons</span> <span class="id" type="var">nat</span> 1 (<span class="id" type="var">cons</span> <span class="id" type="var">nat</span> 2 (<span class="id" type="var">nil</span> <span class="id" type="var">nat</span>)))<br/>
= (<span class="id" type="var">cons</span> <span class="id" type="var">nat</span> 2 (<span class="id" type="var">cons</span> <span class="id" type="var">nat</span> 1 (<span class="id" type="var">nil</span> <span class="id" type="var">nat</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/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_rev2</span>:<br/>
<span class="id" type="var">rev</span> <span class="id" type="var">bool</span> (<span class="id" type="var">nil</span> <span class="id" type="var">bool</span>) = <span class="id" type="var">nil</span> <span class="id" type="var">bool</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/>
<span class="id" type="keyword">Module</span> <span class="id" type="var">MumbleBaz</span>.<br/>
</div>
<div class="doc">
<a name="lab109"></a><h4 class="section">Exercise: 2 stars (mumble_grumble)</h4>
Consider the following two inductively defined types.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">mumble</span> : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">a</span> : <span class="id" type="var">mumble</span><br/>
| <span class="id" type="var">b</span> : <span class="id" type="var">mumble</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">mumble</span><br/>
| <span class="id" type="var">c</span> : <span class="id" type="var">mumble</span>.<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">grumble</span> (<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">d</span> : <span class="id" type="var">mumble</span> <span style="font-family: arial;">→</span> <span class="id" type="var">grumble</span> <span class="id" type="var">X</span><br/>
| <span class="id" type="var">e</span> : <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="var">grumble</span> <span class="id" type="var">X</span>.<br/>
<br/>
</div>
<div class="doc">
Which of the following are well-typed elements of <span class="inlinecode"><span class="id" type="var">grumble</span></span> <span class="inlinecode"><span class="id" type="var">X</span></span> for
some type <span class="inlinecode"><span class="id" type="var">X</span></span>?
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" type="var">d</span></span> <span class="inlinecode">(<span class="id" type="var">b</span></span> <span class="inlinecode"><span class="id" type="var">a</span></span> <span class="inlinecode">5)</span>
</li>
<li> <span class="inlinecode"><span class="id" type="var">d</span></span> <span class="inlinecode"><span class="id" type="var">mumble</span></span> <span class="inlinecode">(<span class="id" type="var">b</span></span> <span class="inlinecode"><span class="id" type="var">a</span></span> <span class="inlinecode">5)</span>
</li>
<li> <span class="inlinecode"><span class="id" type="var">d</span></span> <span class="inlinecode"><span class="id" type="var">bool</span></span> <span class="inlinecode">(<span class="id" type="var">b</span></span> <span class="inlinecode"><span class="id" type="var">a</span></span> <span class="inlinecode">5)</span>
</li>
<li> <span class="inlinecode"><span class="id" type="var">e</span></span> <span class="inlinecode"><span class="id" type="var">bool</span></span> <span class="inlinecode"><span class="id" type="var">true</span></span>
</li>
<li> <span class="inlinecode"><span class="id" type="var">e</span></span> <span class="inlinecode"><span class="id" type="var">mumble</span></span> <span class="inlinecode">(<span class="id" type="var">b</span></span> <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode">0)</span>
</li>
<li> <span class="inlinecode"><span class="id" type="var">e</span></span> <span class="inlinecode"><span class="id" type="var">bool</span></span> <span class="inlinecode">(<span class="id" type="var">b</span></span> <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode">0)</span>
</li>
<li> <span class="inlinecode"><span class="id" type="var">c</span></span>
</li>
</ul>
<span class="comment">(* FILL IN HERE *)</span><br/>
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab110"></a><h4 class="section">Exercise: 2 stars (baz_num_elts)</h4>
Consider the following inductive definition:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">baz</span> : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">x</span> : <span class="id" type="var">baz</span> <span style="font-family: arial;">→</span> <span class="id" type="var">baz</span><br/>
| <span class="id" type="var">y</span> : <span class="id" type="var">baz</span> <span style="font-family: arial;">→</span> <span class="id" type="var">bool</span> <span style="font-family: arial;">→</span> <span class="id" type="var">baz</span>.<br/>
<br/>
</div>
<div class="doc">
How <i>many</i> elements does the type <span class="inlinecode"><span class="id" type="var">baz</span></span> have?
<span class="comment">(* FILL IN HERE *)</span><br/>
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">End</span> <span class="id" type="var">MumbleBaz</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab111"></a><h3 class="section">Type Annotation Inference</h3>
<div class="paragraph"> </div>
Let's write the definition of <span class="inlinecode"><span class="id" type="var">app</span></span> again, but this time we won't
specify the types of any of the arguments. Will Coq still accept
it?
</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">X</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="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">cons</span> <span class="id" type="var">h</span> <span class="id" type="var">t</span> ⇒ <span class="id" type="var">cons</span> <span class="id" type="var">X</span> <span class="id" type="var">h</span> (<span class="id" type="var">app'</span> <span class="id" type="var">X</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">
Indeed it will. Let's see what type Coq has assigned to <span class="inlinecode"><span class="id" type="var">app'</span></span>:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Check</span> <span class="id" type="var">app'</span>.<br/>
<span class="comment">(* ===> forall X : Type, list X -> list X -> list X *)</span><br/>
<span class="id" type="keyword">Check</span> <span class="id" type="var">app</span>.<br/>
<span class="comment">(* ===> forall X : Type, list X -> list X -> list X *)</span><br/>
<br/>
</div>
<div class="doc">
It has exactly the same type type as <span class="inlinecode"><span class="id" type="var">app</span></span>. Coq was able to
use a process called <i>type inference</i> to deduce what the types of
<span class="inlinecode"><span class="id" type="var">X</span></span>, <span class="inlinecode"><span class="id" type="var">l1</span></span>, and <span class="inlinecode"><span class="id" type="var">l2</span></span> must be, based on how they are used. For
example, since <span class="inlinecode"><span class="id" type="var">X</span></span> is used as an argument to <span class="inlinecode"><span class="id" type="var">cons</span></span>, it must be a
<span class="inlinecode"><span class="id" type="keyword">Type</span></span>, since <span class="inlinecode"><span class="id" type="var">cons</span></span> expects a <span class="inlinecode"><span class="id" type="keyword">Type</span></span> as its first argument;
matching <span class="inlinecode"><span class="id" type="var">l1</span></span> with <span class="inlinecode"><span class="id" type="var">nil</span></span> and <span class="inlinecode"><span class="id" type="var">cons</span></span> means it must be a <span class="inlinecode"><span class="id" type="var">list</span></span>; and
so on.
<div class="paragraph"> </div>
This powerful facility means we don't always have to write
explicit type annotations everywhere, although explicit type
annotations are still quite useful as documentation and sanity
checks. You should try to find a balance in your own code between
too many type annotations (so many that they clutter and distract)
and too few (which forces readers to perform type inference in
their heads in order to understand your code).
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab112"></a><h3 class="section">Type Argument Synthesis</h3>
<div class="paragraph"> </div>
Whenever we use a polymorphic function, we need to pass it
one or more types in addition to its other arguments. For
example, the recursive call in the body of the <span class="inlinecode"><span class="id" type="var">length</span></span> function
above must pass along the type <span class="inlinecode"><span class="id" type="var">X</span></span>. But just like providing
explicit type annotations everywhere, this is heavy and verbose.
Since the second argument to <span class="inlinecode"><span class="id" type="var">length</span></span> is a list of <span class="inlinecode"><span class="id" type="var">X</span></span>s, it seems
entirely obvious that the first argument can only be <span class="inlinecode"><span class="id" type="var">X</span></span> — why
should we have to write it explicitly?
<div class="paragraph"> </div>
Fortunately, Coq permits us to avoid this kind of redundancy. In
place of any type argument we can write the "implicit argument"
<span class="inlinecode"><span class="id" type="var">_</span></span>, which can be read as "Please figure out for yourself what
type belongs here." More precisely, when Coq encounters a <span class="inlinecode"><span class="id" type="var">_</span></span>, it
will attempt to <i>unify</i> all locally available information — the
type of the function being applied, the types of the other
arguments, and the type expected by the context in which the
application appears — to determine what concrete type should
replace the <span class="inlinecode"><span class="id" type="var">_</span></span>.
<div class="paragraph"> </div>
This may sound similar to type annotation inference — and,
indeed, the two procedures rely on the same underlying mechanisms.
Instead of simply omitting the types of some arguments to a
function, like
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">app'</span> <span class="id" type="var">X</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> :=
<div class="paragraph"> </div>
</div>
we can also replace the types with <span class="inlinecode"><span class="id" type="var">_</span></span>, like
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">app'</span> (<span class="id" type="var">X</span> : <span class="id" type="var">_</span>) (<span class="id" type="var">l1</span> <span class="id" type="var">l2</span> : <span class="id" type="var">_</span>) : <span class="id" type="var">list</span> <span class="id" type="var">X</span> :=
<div class="paragraph"> </div>
</div>
which tells Coq to attempt to infer the missing information, just
as with argument synthesis.
<div class="paragraph"> </div>
Using implicit arguments, the <span class="inlinecode"><span class="id" type="var">length</span></span> function can be written
like this:
</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">X</span>:<span class="id" type="keyword">Type</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">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> ⇒ 0<br/>
| <span class="id" type="var">cons</span> <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">_</span> <span class="id" type="var">t</span>)<br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
</div>
<div class="doc">
In this instance, we don't save much by writing <span class="inlinecode"><span class="id" type="var">_</span></span> instead of
<span class="inlinecode"><span class="id" type="var">X</span></span>. But in many cases the difference can be significant. For
example, suppose we want to write down a list containing the
numbers <span class="inlinecode">1</span>, <span class="inlinecode">2</span>, and <span class="inlinecode">3</span>. Instead of writing this...
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">list123</span> :=<br/>
<span class="id" type="var">cons</span> <span class="id" type="var">nat</span> 1 (<span class="id" type="var">cons</span> <span class="id" type="var">nat</span> 2 (<span class="id" type="var">cons</span> <span class="id" type="var">nat</span> 3 (<span class="id" type="var">nil</span> <span class="id" type="var">nat</span>))).<br/>
<br/>
</div>
<div class="doc">
...we can use argument synthesis to write this:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">list123'</span> := <span class="id" type="var">cons</span> <span class="id" type="var">_</span> 1 (<span class="id" type="var">cons</span> <span class="id" type="var">_</span> 2 (<span class="id" type="var">cons</span> <span class="id" type="var">_</span> 3 (<span class="id" type="var">nil</span> <span class="id" type="var">_</span>))).<br/>
<br/>
</div>
<div class="doc">
<a name="lab113"></a><h3 class="section">Implicit Arguments</h3>
<div class="paragraph"> </div>
If fact, we can go further. To avoid having to sprinkle <span class="inlinecode"><span class="id" type="var">_</span></span>'s
throughout our programs, we can tell Coq <i>always</i> to infer the
type argument(s) of a given function. The <span class="inlinecode"><span class="id" type="var">Arguments</span></span> directive
specifies the name of the function or constructor, and then lists
its argument names, with curly braces around any arguments to be
treated as implicit.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="var">Arguments</span> <span class="id" type="var">nil</span> {<span class="id" type="var">X</span>}.<br/>
<span class="id" type="var">Arguments</span> <span class="id" type="var">cons</span> {<span class="id" type="var">X</span>} <span class="id" type="var">_</span> <span class="id" type="var">_</span>. <span class="comment">(* use underscore for argument position that has no name *)</span><br/>
<span class="id" type="var">Arguments</span> <span class="id" type="var">length</span> {<span class="id" type="var">X</span>} <span class="id" type="var">l</span>.<br/>
<span class="id" type="var">Arguments</span> <span class="id" type="var">app</span> {<span class="id" type="var">X</span>} <span class="id" type="var">l1</span> <span class="id" type="var">l2</span>.<br/>
<span class="id" type="var">Arguments</span> <span class="id" type="var">rev</span> {<span class="id" type="var">X</span>} <span class="id" type="var">l</span>.<br/>
<span class="id" type="var">Arguments</span> <span class="id" type="var">snoc</span> {<span class="id" type="var">X</span>} <span class="id" type="var">l</span> <span class="id" type="var">v</span>.<br/>
<br/>
<span class="comment">(* note: no _ arguments required... *)</span><br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">list123''</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/>
<span class="id" type="keyword">Check</span> (<span class="id" type="var">length</span> <span class="id" type="var">list123''</span>).<br/>
<br/>
</div>
<div class="doc">
<a name="lab114"></a><h3 class="section"> </h3>
<div class="paragraph"> </div>
Alternatively, we can declare an argument to be implicit while
defining the function itself, by surrounding the argument in curly
braces. For example:
</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">X</span>:<span class="id" type="keyword">Type</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">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> ⇒ 0<br/>
| <span class="id" type="var">cons</span> <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">
(Note that we didn't even have to provide a type argument to
the recursive call to <span class="inlinecode"><span class="id" type="var">length''</span></span>; indeed, it is invalid to provide
one.) We will use this style whenever possible, although we will
continue to use use explicit <span class="inlinecode"><span class="id" type="var">Argument</span></span> declarations for
<span class="inlinecode"><span class="id" type="keyword">Inductive</span></span> constructors.
<div class="paragraph"> </div>
<a name="lab115"></a><h3 class="section"> </h3>
<div class="paragraph"> </div>
One small problem with declaring arguments <span class="inlinecode"><span class="id" type="keyword">Implicit</span></span> is
that, occasionally, Coq does not have enough local information to
determine a type argument; in such cases, we need to tell Coq that
we want to give the argument explicitly this time, even though
we've globally declared it to be <span class="inlinecode"><span class="id" type="keyword">Implicit</span></span>. For example, suppose we
write this:
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* Definition mynil := nil. *)</span><br/>
<br/>
</div>
<div class="doc">
If we uncomment this definition, Coq will give us an error,
because it doesn't know what type argument to supply to <span class="inlinecode"><span class="id" type="var">nil</span></span>. We
can help it by providing an explicit type declaration (so that Coq
has more information available when it gets to the "application"
of <span class="inlinecode"><span class="id" type="var">nil</span></span>):
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">mynil</span> : <span class="id" type="var">list</span> <span class="id" type="var">nat</span> := <span class="id" type="var">nil</span>.<br/>
<br/>
</div>
<div class="doc">
Alternatively, we can force the implicit arguments to be explicit by
prefixing the function name with <span class="inlinecode">@</span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Check</span> @<span class="id" type="var">nil</span>.<br/>
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">mynil'</span> := @<span class="id" type="var">nil</span> <span class="id" type="var">nat</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab116"></a><h3 class="section"> </h3>
Using argument synthesis and implicit arguments, we can
define convenient notation for lists, as before. Since we have
made the constructor type arguments implicit, Coq will know to
automatically infer these when we use the notations.
</div>
<div class="code code-tight">
<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">y</span>)<br/>
(<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> []) ..).<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="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/>
<br/>
</div>
<div class="doc">
Now lists can be written just the way we'd hope:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">list123'''</span> := [1; 2; 3].<br/>
<br/>
<span class="id" type="keyword">Check</span> ([3 + 4] ++ <span class="id" type="var">nil</span>).<br/>
<br/>
</div>
<div class="doc">
<a name="lab117"></a><h3 class="section">Exercises: Polymorphic Lists</h3>
<div class="paragraph"> </div>
<a name="lab118"></a><h4 class="section">Exercise: 2 stars, optional (poly_exercises)</h4>
Here are a few simple exercises, just like ones in the <span class="inlinecode"><span class="id" type="var">Lists</span></span>
chapter, for practice with polymorphism. Fill in the definitions
and complete the proofs below.
</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">X</span> : <span class="id" type="keyword">Type</span>} (<span class="id" type="var">n</span> : <span class="id" type="var">X</span>) (<span class="id" type="var">count</span> : <span class="id" type="var">nat</span>) : <span class="id" type="var">list</span> <span class="id" type="var">X</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_repeat1</span>:<br/>
<span class="id" type="tactic">repeat</span> <span class="id" type="var">true</span> 2 = <span class="id" type="var">cons</span> <span class="id" type="var">true</span> (<span class="id" type="var">cons</span> <span class="id" type="var">true</span> <span class="id" type="var">nil</span>).<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<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">X</span>:<span class="id" type="keyword">Type</span>, <span style="font-family: arial;">∀</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">app</span> [] <span class="id" type="var">l</span> = <span class="id" type="var">l</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">Theorem</span> <span class="id" type="var">rev_snoc</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">X</span> : <span class="id" type="keyword">Type</span>,<br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">v</span> : <span class="id" type="var">X</span>,<br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">s</span> : <span class="id" type="var">list</span> <span class="id" type="var">X</span>,<br/>
<span class="id" type="var">rev</span> (<span class="id" type="var">snoc</span> <span class="id" type="var">s</span> <span class="id" type="var">v</span>) = <span class="id" type="var">v</span> :: (<span class="id" type="var">rev</span> <span class="id" type="var">s</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">Theorem</span> <span class="id" type="var">rev_involutive</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">X</span> : <span class="id" type="keyword">Type</span>, <span style="font-family: arial;">∀</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">rev</span> (<span class="id" type="var">rev</span> <span class="id" type="var">l</span>) = <span class="id" type="var">l</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">Theorem</span> <span class="id" type="var">snoc_with_append</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">X</span> : <span class="id" type="keyword">Type</span>,<br/>
<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">list</span> <span class="id" type="var">X</span>,<br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">v</span> : <span class="id" type="var">X</span>,<br/>
<span class="id" type="var">snoc</span> (<span class="id" type="var">l1</span> ++ <span class="id" type="var">l2</span>) <span class="id" type="var">v</span> = <span class="id" type="var">l1</span> ++ (<span class="id" type="var">snoc</span> <span class="id" type="var">l2</span> <span class="id" type="var">v</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="lab119"></a><h2 class="section">Polymorphic Pairs</h2>
<div class="paragraph"> </div>
Following the same pattern, the type definition we gave in
the last chapter for pairs of numbers can be generalized to
<i>polymorphic pairs</i> (or <i>products</i>):
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">prod</span> (<span class="id" type="var">X</span> <span class="id" type="var">Y</span> : <span class="id" type="keyword">Type</span>) : <span class="id" type="keyword">Type</span> :=<br/>
<span class="id" type="var">pair</span> : <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Y</span> <span style="font-family: arial;">→</span> <span class="id" type="var">prod</span> <span class="id" type="var">X</span> <span class="id" type="var">Y</span>.<br/>
<br/>
<span class="id" type="var">Arguments</span> <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">_</span> <span class="id" type="var">_</span>.<br/>
<br/>
</div>
<div class="doc">
As with lists, we make the type arguments implicit and define the
familiar concrete notation.
</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">
We can also use the <span class="inlinecode"><span class="id" type="keyword">Notation</span></span> mechanism to define the standard
notation for pair <i>types</i>:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Notation</span> "X × Y" := (<span class="id" type="var">prod</span> <span class="id" type="var">X</span> <span class="id" type="var">Y</span>) : <span class="id" type="var">type_scope</span>.<br/>
<br/>
</div>
<div class="doc">
(The annotation <span class="inlinecode">:</span> <span class="inlinecode"><span class="id" type="var">type_scope</span></span> tells Coq that this abbreviation
should be used when parsing types. This avoids a clash with the
multiplication symbol.)
<div class="paragraph"> </div>
<a name="lab120"></a><h3 class="section"> </h3>
A note of caution: it is easy at first to get <span class="inlinecode">(<span class="id" type="var">x</span>,<span class="id" type="var">y</span>)</span> and
<span class="inlinecode"><span class="id" type="var">X</span>×<span class="id" type="var">Y</span></span> confused. Remember that <span class="inlinecode">(<span class="id" type="var">x</span>,<span class="id" type="var">y</span>)</span> is a <i>value</i> built from two
other values; <span class="inlinecode"><span class="id" type="var">X</span>×<span class="id" type="var">Y</span></span> is a <i>type</i> built from two other types. If
<span class="inlinecode"><span class="id" type="var">x</span></span> has type <span class="inlinecode"><span class="id" type="var">X</span></span> and <span class="inlinecode"><span class="id" type="var">y</span></span> has type <span class="inlinecode"><span class="id" type="var">Y</span></span>, then <span class="inlinecode">(<span class="id" type="var">x</span>,<span class="id" type="var">y</span>)</span> has type
<span class="inlinecode"><span class="id" type="var">X</span>×<span class="id" type="var">Y</span></span>.
<div class="paragraph"> </div>
The first and second projection functions now look pretty
much as they would in any functional programming language.
</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">X</span> <span class="id" type="var">Y</span> : <span class="id" type="keyword">Type</span>} (<span class="id" type="var">p</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">match</span> <span class="id" type="var">p</span> <span class="id" type="keyword">with</span> (<span class="id" type="var">x</span>,<span class="id" type="var">y</span>) ⇒ <span class="id" type="var">x</span> <span class="id" type="keyword">end</span>.<br/>
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">snd</span> {<span class="id" type="var">X</span> <span class="id" type="var">Y</span> : <span class="id" type="keyword">Type</span>} (<span class="id" type="var">p</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">match</span> <span class="id" type="var">p</span> <span class="id" type="keyword">with</span> (<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="keyword">end</span>.<br/>
<br/>
</div>
<div class="doc">
The following function takes two lists and combines them
into a list of pairs. In many functional programming languages,
it is called <span class="inlinecode"><span class="id" type="var">zip</span></span>. We call it <span class="inlinecode"><span class="id" type="var">combine</span></span> for consistency with
Coq's standard library. Note that the pair notation can be used both in expressions and in
patterns...
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">combine</span> {<span class="id" type="var">X</span> <span class="id" type="var">Y</span> : <span class="id" type="keyword">Type</span>} (<span class="id" type="var">lx</span> : <span class="id" type="var">list</span> <span class="id" type="var">X</span>) (<span class="id" type="var">ly</span> : <span class="id" type="var">list</span> <span class="id" type="var">Y</span>)<br/>
: <span class="id" type="var">list</span> (<span class="id" type="var">X</span>×<span class="id" type="var">Y</span>) :=<br/>
<span class="id" type="keyword">match</span> (<span class="id" type="var">lx</span>,<span class="id" type="var">ly</span>) <span class="id" type="keyword">with</span><br/>
| ([],_) ⇒ []<br/>
| (<span class="id" type="var">_</span>,[]) ⇒ []<br/>
| (<span class="id" type="var">x</span>::<span class="id" type="var">tx</span>, <span class="id" type="var">y</span>::<span class="id" type="var">ty</span>) ⇒ (<span class="id" type="var">x</span>,<span class="id" type="var">y</span>) :: (<span class="id" type="var">combine</span> <span class="id" type="var">tx</span> <span class="id" type="var">ty</span>)<br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab121"></a><h4 class="section">Exercise: 1 star, optional (combine_checks)</h4>
Try answering the following questions on paper and
checking your answers in coq:
<div class="paragraph"> </div>
<ul class="doclist">
<li> What is the type of <span class="inlinecode"><span class="id" type="var">combine</span></span> (i.e., what does <span class="inlinecode"><span class="id" type="keyword">Check</span></span>
<span class="inlinecode">@<span class="id" type="var">combine</span></span> print?)
</li>
<li> What does
<div class="paragraph"> </div>
<div class="code code-tight">
<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">combine</span> [1;2] [<span class="id" type="var">false</span>;<span class="id" type="var">false</span>;<span class="id" type="var">true</span>;<span class="id" type="var">true</span>]).
<div class="paragraph"> </div>
</div>
print? <font size=-2>☐</font>
</li>
</ul>
<div class="paragraph"> </div>
<a name="lab122"></a><h4 class="section">Exercise: 2 stars (split)</h4>
The function <span class="inlinecode"><span class="id" type="tactic">split</span></span> is the right inverse of combine: it takes a
list of pairs and returns a pair of lists. In many functional
programing languages, this function is called <span class="inlinecode"><span class="id" type="var">unzip</span></span>.
<div class="paragraph"> </div>
Uncomment the material below and fill in the definition of
<span class="inlinecode"><span class="id" type="tactic">split</span></span>. Make sure it passes the given unit tests.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="tactic">split</span><br/>
{<span class="id" type="var">X</span> <span class="id" type="var">Y</span> : <span class="id" type="keyword">Type</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">Y</span>))<br/>
: (<span class="id" type="var">list</span> <span class="id" type="var">X</span>) × (<span class="id" type="var">list</span> <span class="id" type="var">Y</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_split</span>:<br/>
<span class="id" type="tactic">split</span> [(1,<span class="id" type="var">false</span>);(2,<span class="id" type="var">false</span>)] = ([1;2],[<span class="id" type="var">false</span>;<span class="id" type="var">false</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="lab123"></a><h2 class="section">Polymorphic Options</h2>
<div class="paragraph"> </div>
One last polymorphic type for now: <i>polymorphic options</i>.
The type declaration generalizes the one for <span class="inlinecode"><span class="id" type="var">natoption</span></span> in the
previous chapter:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">option</span> (<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">Some</span> : <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="var">option</span> <span class="id" type="var">X</span><br/>
| <span class="id" type="var">None</span> : <span class="id" type="var">option</span> <span class="id" type="var">X</span>.<br/>
<br/>
<span class="id" type="var">Arguments</span> <span class="id" type="var">Some</span> {<span class="id" type="var">X</span>} <span class="id" type="var">_</span>.<br/>
<span class="id" type="var">Arguments</span> <span class="id" type="var">None</span> {<span class="id" type="var">X</span>}.<br/>
<br/>
</div>
<div class="doc">
<a name="lab124"></a><h3 class="section"> </h3>
We can now rewrite the <span class="inlinecode"><span class="id" type="var">index</span></span> function so that it works
with any type of lists.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">index</span> {<span class="id" type="var">X</span> : <span class="id" type="keyword">Type</span>} (<span class="id" type="var">n</span> : <span class="id" type="var">nat</span>)<br/>
(<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">option</span> <span class="id" type="var">X</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">None</span><br/>
| <span class="id" type="var">a</span> :: <span class="id" type="var">l'</span> ⇒ <span class="id" type="keyword">if</span> <span class="id" type="var">beq_nat</span> <span class="id" type="var">n</span> <span class="id" type="var">O</span> <span class="id" type="keyword">then</span> <span class="id" type="var">Some</span> <span class="id" type="var">a</span> <span class="id" type="keyword">else</span> <span class="id" type="var">index</span> (<span class="id" type="var">pred</span> <span class="id" type="var">n</span>) <span class="id" type="var">l'</span><br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_index1</span> : <span class="id" type="var">index</span> 0 [4;5;6;7] = <span class="id" type="var">Some</span> 4.<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_index2</span> : <span class="id" type="var">index</span> 1 [[1];[2]] = <span class="id" type="var">Some</span> [2].<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_index3</span> : <span class="id" type="var">index</span> 2 [<span class="id" type="var">true</span>] = <span class="id" type="var">None</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="lab125"></a><h4 class="section">Exercise: 1 star, optional (hd_opt_poly)</h4>
Complete the definition of a polymorphic version of the
<span class="inlinecode"><span class="id" type="var">hd_opt</span></span> function from the last chapter. Be sure that it
passes the unit tests below.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">hd_opt</span> {<span class="id" type="var">X</span> : <span class="id" type="keyword">Type</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">option</span> <span class="id" type="var">X</span> :=<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">admit</span>.<br/>
<br/>
</div>
<div class="doc">
Once again, to force the implicit arguments to be explicit,
we can use <span class="inlinecode">@</span> before the name of the function.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Check</span> @<span class="id" type="var">hd_opt</span>.<br/>
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_hd_opt1</span> : <span class="id" type="var">hd_opt</span> [1;2] = <span class="id" type="var">Some</span> 1.<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_hd_opt2</span> : <span class="id" type="var">hd_opt</span> [[1];[2]] = <span class="id" type="var">Some</span> [1].<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="lab126"></a><h1 class="section">Functions as Data</h1>
</div>
<div class="code code-space">
</div>
<div class="doc">
<a name="lab127"></a><h2 class="section">Higher-Order Functions</h2>
<div class="paragraph"> </div>
Like many other modern programming languages — including
all <i>functional languages</i> (ML, Haskell, Scheme, etc.) — Coq
treats functions as first-class citizens, allowing functions to be
passed as arguments to other functions, returned as results,
stored in data structures, etc.
<div class="paragraph"> </div>
Functions that manipulate other functions are often called
<i>higher-order</i> functions. Here's a simple one:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">doit3times</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">X</span><span style="font-family: arial;">→</span><span class="id" type="var">X</span>) (<span class="id" type="var">n</span>:<span class="id" type="var">X</span>) : <span class="id" type="var">X</span> :=<br/>
<span class="id" type="var">f</span> (<span class="id" type="var">f</span> (<span class="id" type="var">f</span> <span class="id" type="var">n</span>)).<br/>
<br/>
</div>
<div class="doc">
The argument <span class="inlinecode"><span class="id" type="var">f</span></span> here is itself a function (from <span class="inlinecode"><span class="id" type="var">X</span></span> to
<span class="inlinecode"><span class="id" type="var">X</span></span>); the body of <span class="inlinecode"><span class="id" type="var">doit3times</span></span> applies <span class="inlinecode"><span class="id" type="var">f</span></span> three times to some
value <span class="inlinecode"><span class="id" type="var">n</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Check</span> @<span class="id" type="var">doit3times</span>.<br/>
<span class="comment">(* ===> doit3times : forall X : Type, (X -> X) -> X -> X *)</span><br/>
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_doit3times</span>: <span class="id" type="var">doit3times</span> <span class="id" type="var">minustwo</span> 9 = 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/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_doit3times'</span>: <span class="id" type="var">doit3times</span> <span class="id" type="var">negb</span> <span class="id" type="var">true</span> = <span class="id" type="var">false</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="lab128"></a><h2 class="section">Partial Application</h2>
<div class="paragraph"> </div>
In fact, the multiple-argument functions we have already
seen are also examples of passing functions as data. To see why,
recall the type of <span class="inlinecode"><span class="id" type="var">plus</span></span>.
</div>
<div class="code code-tight">
<br/>