-
Notifications
You must be signed in to change notification settings - Fork 0
/
Poly.html
1913 lines (1499 loc) · 173 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>
<div class="code code-tight">
<br/>
<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Export</span> <a class="idref" href="Lists.html#"><span class="id" type="library">Lists</span></a>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab93"></a><h1 class="section">Polymorphism</h1>
<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). We begin with polymorphism.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab94"></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> <a name="boollist"><span class="id" type="inductive">boollist</span></a> : <span class="id" type="keyword">Type</span> :=<br/>
| <a name="bool_nil"><span class="id" type="constructor">bool_nil</span></a> : <a class="idref" href="Poly.html#boollist"><span class="id" type="inductive">boollist</span></a><br/>
| <a name="bool_cons"><span class="id" type="constructor">bool_cons</span></a> : <a class="idref" href="Basics.html#bool"><span class="id" type="inductive">bool</span></a> <span style="font-family: arial;">→</span> <a class="idref" href="Poly.html#boollist"><span class="id" type="inductive">boollist</span></a> <span style="font-family: arial;">→</span> <a class="idref" href="Poly.html#boollist"><span class="id" type="inductive">boollist</span></a>.<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>
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> <a name="list"><span class="id" type="inductive">list</span></a> (<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) : <span class="id" type="keyword">Type</span> :=<br/>
| <a name="nil"><span class="id" type="constructor">nil</span></a> : <a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <span class="id" type="var">X</span><br/>
| <a name="cons"><span class="id" type="constructor">cons</span></a> : <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <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> <a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a>.<br/>
<span class="comment">(* ===> nil : forall X : Type, list X *)</span><br/>
<span class="id" type="keyword">Check</span> <a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a>.<br/>
<span class="comment">(* ===> cons : forall X : Type, X -> list X -> list X *)</span><br/>
<br/>
</div>
<div class="doc">
(Side note on notation: In .v files, the "forall" quantifier is
spelled out in letters. In the generated HTML files, <span class="inlinecode"><span style="font-family: arial;">∀</span></span> is
usually typeset as the usual mathematical "upside down A," but
you'll see the spelled-out "forall" in a few places, as in the
above comments. This is just a quirk of typesetting: there is no
difference in meaning.)
<div class="paragraph"> </div>
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> (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#nat"><span class="id" type="inductive">nat</span></a> 2 (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#nat"><span class="id" type="inductive">nat</span></a> 1 (<a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a> <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#nat"><span class="id" type="inductive">nat</span></a>))).<br/>
<br/>
</div>
<div class="doc">
(We've written <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 versions of all the
list-processing functions that we wrote before. Here is <span class="inlinecode"><span class="id" type="tactic">repeat</span></span>,
for example:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <a name="repeat"><span class="id" type="definition">repeat</span></a> (<span class="id" type="var">X</span> : <span class="id" type="keyword">Type</span>) (<span class="id" type="var">x</span> : <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a>) (<span class="id" type="var">count</span> : <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#nat"><span class="id" type="inductive">nat</span></a>) : <a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a> :=<br/>
<span class="id" type="keyword">match</span> <a class="idref" href="Poly.html#count"><span class="id" type="variable">count</span></a> <span class="id" type="keyword">with</span><br/>
| 0 ⇒ <a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a><br/>
| <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#S"><span class="id" type="constructor">S</span></a> <span class="id" type="var">count'</span> ⇒ <a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a> <a class="idref" href="Poly.html#x"><span class="id" type="variable">x</span></a> (<a class="idref" href="Poly.html#repeat"><span class="id" type="definition">repeat</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a> <a class="idref" href="Poly.html#x"><span class="id" type="variable">x</span></a> <span class="id" type="var">count'</span>)<br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
</div>
<div class="doc">
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="tactic">repeat</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> <a name="test_repeat1"><span class="id" type="definition">test_repeat1</span></a> :<br/>
<a class="idref" href="Poly.html#repeat"><span class="id" type="definition">repeat</span></a> <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#nat"><span class="id" type="inductive">nat</span></a> 4 2 = <a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#nat"><span class="id" type="inductive">nat</span></a> 4 (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#nat"><span class="id" type="inductive">nat</span></a> 4 (<a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a> <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#nat"><span class="id" type="inductive">nat</span></a>)).<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 <span class="inlinecode"><span class="id" type="tactic">repeat</span></span> to build 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> <a name="test_repeat2"><span class="id" type="definition">test_repeat2</span></a> :<br/>
<a class="idref" href="Poly.html#repeat"><span class="id" type="definition">repeat</span></a> <a class="idref" href="Basics.html#bool"><span class="id" type="inductive">bool</span></a> <a class="idref" href="Basics.html#false"><span class="id" type="constructor">false</span></a> 1 = <a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <a class="idref" href="Basics.html#bool"><span class="id" type="inductive">bool</span></a> <a class="idref" href="Basics.html#false"><span class="id" type="constructor">false</span></a> (<a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a> <a class="idref" href="Basics.html#bool"><span class="id" type="inductive">bool</span></a>).<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> <a name="MumbleGrumble"><span class="id" type="module">MumbleGrumble</span></a>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab95"></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> <a name="MumbleGrumble.mumble"><span class="id" type="inductive">mumble</span></a> : <span class="id" type="keyword">Type</span> :=<br/>
| <a name="MumbleGrumble.a"><span class="id" type="constructor">a</span></a> : <a class="idref" href="Poly.html#mumble"><span class="id" type="inductive">mumble</span></a><br/>
| <a name="MumbleGrumble.b"><span class="id" type="constructor">b</span></a> : <a class="idref" href="Poly.html#mumble"><span class="id" type="inductive">mumble</span></a> <span style="font-family: arial;">→</span> <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#nat"><span class="id" type="inductive">nat</span></a> <span style="font-family: arial;">→</span> <a class="idref" href="Poly.html#mumble"><span class="id" type="inductive">mumble</span></a><br/>
| <a name="MumbleGrumble.c"><span class="id" type="constructor">c</span></a> : <a class="idref" href="Poly.html#mumble"><span class="id" type="inductive">mumble</span></a>.<br/>
<br/>
<span class="id" type="keyword">Inductive</span> <a name="MumbleGrumble.grumble"><span class="id" type="inductive">grumble</span></a> (<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) : <span class="id" type="keyword">Type</span> :=<br/>
| <a name="MumbleGrumble.d"><span class="id" type="constructor">d</span></a> : <a class="idref" href="Poly.html#MumbleGrumble.mumble"><span class="id" type="inductive">mumble</span></a> <span style="font-family: arial;">→</span> <a class="idref" href="Poly.html#grumble"><span class="id" type="inductive">grumble</span></a> <span class="id" type="var">X</span><br/>
| <a name="MumbleGrumble.e"><span class="id" type="constructor">e</span></a> : <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <a class="idref" href="Poly.html#grumble"><span class="id" type="inductive">grumble</span></a> <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 color="rgb(80%,80%,100%)">☐</font>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">End</span> <a class="idref" href="Poly.html#MumbleGrumble"><span class="id" type="module">MumbleGrumble</span></a>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab96"></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="tactic">repeat</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> <a name="repeat'"><span class="id" type="definition">repeat'</span></a> <span class="id" type="var">X</span> <span class="id" type="var">x</span> <span class="id" type="var">count</span> : <a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a> :=<br/>
<span class="id" type="keyword">match</span> <a class="idref" href="Poly.html#count"><span class="id" type="variable">count</span></a> <span class="id" type="keyword">with</span><br/>
| 0 ⇒ <a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a><br/>
| <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#S"><span class="id" type="constructor">S</span></a> <span class="id" type="var">count'</span> ⇒ <a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a> <a class="idref" href="Poly.html#x"><span class="id" type="variable">x</span></a> (<a class="idref" href="Poly.html#repeat'"><span class="id" type="definition">repeat'</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a> <a class="idref" href="Poly.html#x"><span class="id" type="variable">x</span></a> <span class="id" type="var">count'</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">repeat'</span></span>:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Check</span> <a class="idref" href="Poly.html#repeat'"><span class="id" type="definition">repeat'</span></a>.<br/>
<span class="comment">(* ===> forall X : Type, X -> nat -> list X *)</span><br/>
<span class="id" type="keyword">Check</span> <a class="idref" href="Poly.html#repeat"><span class="id" type="definition">repeat</span></a>.<br/>
<span class="comment">(* ===> forall X : Type, X -> nat -> list X *)</span><br/>
<br/>
</div>
<div class="doc">
It has exactly the same type type as <span class="inlinecode"><span class="id" type="tactic">repeat</span></span>. Coq was able
to use <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">x</span></span>, and
<span class="inlinecode"><span class="id" type="var">count</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">count</span></span>
with <span class="inlinecode">0</span> and <span class="inlinecode"><span class="id" type="var">S</span></span> means it must be a <span class="inlinecode"><span class="id" type="var">nat</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, so we will continue to use them most of the time. You
should try to find a balance in your own code between too many
type annotations (which can 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="lab97"></a><h3 class="section">Type Argument Synthesis</h3>
<div class="paragraph"> </div>
To 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="tactic">repeat</span></span> function above must
pass along the type <span class="inlinecode"><span class="id" type="var">X</span></span>. But since the second argument to
<span class="inlinecode"><span class="id" type="tactic">repeat</span></span> is an element of <span class="inlinecode"><span class="id" type="var">X</span></span>, 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 try to figure out for yourself
what 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 — 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">repeat'</span> <span class="id" type="var">X</span> <span class="id" type="var">x</span> <span class="id" type="var">count</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>
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">repeat'</span> (<span class="id" type="var">X</span> : <span class="id" type="var">_</span>) (<span class="id" type="var">x</span> : <span class="id" type="var">_</span>) (<span class="id" type="var">count</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>
to tell Coq to attempt to infer the missing information.
<div class="paragraph"> </div>
Using implicit arguments, the <span class="inlinecode"><span class="id" type="var">count</span></span> function can be written
like this:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <a name="repeat''"><span class="id" type="definition">repeat''</span></a> <span class="id" type="var">X</span> <span class="id" type="var">x</span> <span class="id" type="var">count</span> : <a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a> :=<br/>
<span class="id" type="keyword">match</span> <a class="idref" href="Poly.html#count"><span class="id" type="variable">count</span></a> <span class="id" type="keyword">with</span><br/>
| 0 ⇒ <a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a> <span class="id" type="var">_</span><br/>
| <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#S"><span class="id" type="constructor">S</span></a> <span class="id" type="var">count'</span> ⇒ <a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <span class="id" type="var">_</span> <a class="idref" href="Poly.html#x"><span class="id" type="variable">x</span></a> (<a class="idref" href="Poly.html#repeat''"><span class="id" type="definition">repeat''</span></a> <span class="id" type="var">_</span> <a class="idref" href="Poly.html#x"><span class="id" type="variable">x</span></a> <span class="id" type="var">count'</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 in both keystrokes and
readability is nontrivial. 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> <a name="list123"><span class="id" type="definition">list123</span></a> :=<br/>
<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#nat"><span class="id" type="inductive">nat</span></a> 1 (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#nat"><span class="id" type="inductive">nat</span></a> 2 (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#nat"><span class="id" type="inductive">nat</span></a> 3 (<a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a> <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#nat"><span class="id" type="inductive">nat</span></a>))).<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> <a name="list123'"><span class="id" type="definition">list123'</span></a> :=<br/>
<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <span class="id" type="var">_</span> 1 (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <span class="id" type="var">_</span> 2 (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <span class="id" type="var">_</span> 3 (<a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a> <span class="id" type="var">_</span>))).<br/>
<br/>
</div>
<div class="doc">
<a name="lab98"></a><h3 class="section">Implicit Arguments</h3>
<div class="paragraph"> </div>
We can go further and even avoid writing <span class="inlinecode"><span class="id" type="var">_</span></span>'s in most cases by
telling 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. (If
some arguments of a definition don't have a name, as is often the
case for constructors, they can be marked with a wildcard pattern
<span class="inlinecode"><span class="id" type="var">_</span></span>.)
</div>
<div class="code code-tight">
<br/>
<span class="id" type="var">Arguments</span> <a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a> {<span class="id" type="var">X</span>}.<br/>
<span class="id" type="var">Arguments</span> <a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> {<span class="id" type="var">X</span>} <span class="id" type="var">_</span> <span class="id" type="var">_</span>.<br/>
<span class="id" type="var">Arguments</span> <a class="idref" href="Poly.html#repeat"><span class="id" type="definition">repeat</span></a> {<span class="id" type="var">X</span>} <span class="id" type="var">x</span> <span class="id" type="var">count</span>.<br/>
<br/>
</div>
<div class="doc">
Now, we don't have to supply type arguments at all:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <a name="list123''"><span class="id" type="definition">list123''</span></a> := <a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> 1 (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> 2 (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> 3 <a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a>)).<br/>
<br/>
</div>
<div class="doc">
Alternatively, we can declare an argument to be implicit
when defining the function itself, by surrounding it in curly
braces. For example:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <a name="repeat'''"><span class="id" type="definition">repeat'''</span></a> {<span class="id" type="var">X</span> : <span class="id" type="keyword">Type</span>} (<span class="id" type="var">x</span> : <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a>) (<span class="id" type="var">count</span> : <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#nat"><span class="id" type="inductive">nat</span></a>) : <a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a> :=<br/>
<span class="id" type="keyword">match</span> <a class="idref" href="Poly.html#count"><span class="id" type="variable">count</span></a> <span class="id" type="keyword">with</span><br/>
| 0 ⇒ <a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a><br/>
| <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#S"><span class="id" type="constructor">S</span></a> <span class="id" type="var">count'</span> ⇒ <a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <a class="idref" href="Poly.html#x"><span class="id" type="variable">x</span></a> (<a class="idref" href="Poly.html#repeat'''"><span class="id" type="definition">repeat'''</span></a> <a class="idref" href="Poly.html#x"><span class="id" type="variable">x</span></a> <span class="id" type="var">count'</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">repeat'''</span></span>; indeed, it would be invalid to
provide one!)
<div class="paragraph"> </div>
We will use the latter style whenever possible, but 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. The reason for this is that marking the
parameter of an inductive type as implicit causes it to become
implicit for the type itself, not just for its constructors. For
instance, consider the following alternative definition of the
<span class="inlinecode"><span class="id" type="var">list</span></span> type:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <a name="list'"><span class="id" type="inductive">list'</span></a> {<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>} : <span class="id" type="keyword">Type</span> :=<br/>
| <a name="nil'"><span class="id" type="constructor">nil'</span></a> : <a class="idref" href="Poly.html#list'"><span class="id" type="inductive">list'</span></a><br/>
| <a name="cons'"><span class="id" type="constructor">cons'</span></a> : <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <a class="idref" href="Poly.html#list'"><span class="id" type="inductive">list'</span></a> <span style="font-family: arial;">→</span> <a class="idref" href="Poly.html#list'"><span class="id" type="inductive">list'</span></a>.<br/>
<br/>
</div>
<div class="doc">
Because <span class="inlinecode"><span class="id" type="var">X</span></span> is declared as implicit for the <i>entire</i> inductive
definition including <span class="inlinecode"><span class="id" type="var">list'</span></span> itself, we now have to write just
<span class="inlinecode"><span class="id" type="var">list'</span></span> whether we are talking about lists of numbers or booleans
or anything else, rather than <span class="inlinecode"><span class="id" type="var">list'</span></span> <span class="inlinecode"><span class="id" type="var">nat</span></span> or <span class="inlinecode"><span class="id" type="var">list'</span></span> <span class="inlinecode"><span class="id" type="var">bool</span></span> or
whatever; this is a step too far.
<div class="paragraph"> </div>
Let's finish 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> <a name="app"><span class="id" type="definition">app</span></a> {<span class="id" type="var">X</span> : <span class="id" type="keyword">Type</span>} (<span class="id" type="var">l<sub>1</sub></span> <span class="id" type="var">l<sub>2</sub></span> : <a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a>)<br/>
: (<a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a>) :=<br/>
<span class="id" type="keyword">match</span> <a class="idref" href="Poly.html#l<sub>1</sub>"><span class="id" type="variable">l<sub>1</sub></span></a> <span class="id" type="keyword">with</span><br/>
| <a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a> ⇒ <a class="idref" href="Poly.html#l<sub>2</sub>"><span class="id" type="variable">l<sub>2</sub></span></a><br/>
| <a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <span class="id" type="var">h</span> <span class="id" type="var">t</span> ⇒ <a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <span class="id" type="var">h</span> (<a class="idref" href="Poly.html#app"><span class="id" type="definition">app</span></a> <span class="id" type="var">t</span> <a class="idref" href="Poly.html#l<sub>2</sub>"><span class="id" type="variable">l<sub>2</sub></span></a>)<br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
<span class="id" type="keyword">Fixpoint</span> <a name="rev"><span class="id" type="definition">rev</span></a> {<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>} (<span class="id" type="var">l</span>:<a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a>) : <a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a> :=<br/>
<span class="id" type="keyword">match</span> <a class="idref" href="Poly.html#l"><span class="id" type="variable">l</span></a> <span class="id" type="keyword">with</span><br/>
| <a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a> ⇒ <a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a><br/>
| <a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <span class="id" type="var">h</span> <span class="id" type="var">t</span> ⇒ <a class="idref" href="Poly.html#app"><span class="id" type="definition">app</span></a> (<a class="idref" href="Poly.html#rev"><span class="id" type="definition">rev</span></a> <span class="id" type="var">t</span>) (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <span class="id" type="var">h</span> <a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a>)<br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
<span class="id" type="keyword">Fixpoint</span> <a name="length"><span class="id" type="definition">length</span></a> {<span class="id" type="var">X</span> : <span class="id" type="keyword">Type</span>} (<span class="id" type="var">l</span> : <a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a>) : <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#nat"><span class="id" type="inductive">nat</span></a> :=<br/>
<span class="id" type="keyword">match</span> <a class="idref" href="Poly.html#l"><span class="id" type="variable">l</span></a> <span class="id" type="keyword">with</span><br/>
| <a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a> ⇒ 0<br/>
| <a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <span class="id" type="var">_</span> <span class="id" type="var">l'</span> ⇒ <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#S"><span class="id" type="constructor">S</span></a> (<a class="idref" href="Poly.html#length"><span class="id" type="definition">length</span></a> <span class="id" type="var">l'</span>)<br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
<span class="id" type="keyword">Example</span> <a name="test_rev1"><span class="id" type="definition">test_rev1</span></a> :<br/>
<a class="idref" href="Poly.html#rev"><span class="id" type="definition">rev</span></a> (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> 1 (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> 2 <a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a>)) = (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> 2 (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> 1 <a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a>)).<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> <a name="test_rev2"><span class="id" type="definition">test_rev2</span></a>:<br/>
<a class="idref" href="Poly.html#rev"><span class="id" type="definition">rev</span></a> (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <a class="idref" href="Basics.html#true"><span class="id" type="constructor">true</span></a> <a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a>) = <a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <a class="idref" href="Basics.html#true"><span class="id" type="constructor">true</span></a> <a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a>.<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> <a name="test_length1"><span class="id" type="definition">test_length1</span></a>: <a class="idref" href="Poly.html#length"><span class="id" type="definition">length</span></a> (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> 1 (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> 2 (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> 3 <a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a>))) = 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">
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 just this time. For
example, suppose we write this:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="var">Fail</span> <span class="id" type="keyword">Definition</span> <a name="mynil"><span class="id" type="definition">mynil</span></a> := <a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a>.<br/>
<br/>
</div>
<div class="doc">
(The <span class="inlinecode"><span class="id" type="var">Fail</span></span> qualifier that appears before <span class="inlinecode"><span class="id" type="keyword">Definition</span></span> can be
used with <i>any</i> command, and is used to ensure that that command
indeed fails when executed. If the command does fail, Coq prints
the corresponding error message, but continues processing the rest
of the file.)
<div class="paragraph"> </div>
Here, Coq gives 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> <a name="mynil"><span class="id" type="definition">mynil</span></a> : <a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#nat"><span class="id" type="inductive">nat</span></a> := <a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a>.<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> @<a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a>.<br/>
<br/>
<span class="id" type="keyword">Definition</span> <a name="mynil'"><span class="id" type="definition">mynil'</span></a> := @<a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a> <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#nat"><span class="id" type="inductive">nat</span></a>.<br/>
<br/>
</div>
<div class="doc">
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" := (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <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> "[ ]" := <a class="idref" href="Poly.html#nil"><span class="id" type="constructor">nil</span></a>.<br/>
<span class="id" type="keyword">Notation</span> "[ x ; .. ; y ]" := (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <span class="id" type="var">x</span> .. (<a class="idref" href="Poly.html#cons"><span class="id" type="constructor">cons</span></a> <span class="id" type="var">y</span> []) ..).<br/>
<span class="id" type="keyword">Notation</span> "x ++ y" := (<a class="idref" href="Poly.html#app"><span class="id" type="definition">app</span></a> <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> <a name="list123'''"><span class="id" type="definition">list123'''</span></a> := [1; 2; 3].<br/>
<br/>
</div>
<div class="doc">
<a name="lab99"></a><h3 class="section">Exercises</h3>
<div class="paragraph"> </div>
<a name="lab100"></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. Complete the proofs below.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <a name="app_nil_r"><span class="id" type="lemma">app_nil_r</span></a> : <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>:<a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a>,<br/>
<a class="idref" href="Poly.html#l"><span class="id" type="variable">l</span></a> ++ [] = <a class="idref" href="Poly.html#l"><span class="id" type="variable">l</span></a>.<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> <a name="app_assoc"><span class="id" type="lemma">app_assoc</span></a> : <span style="font-family: arial;">∀</span><span class="id" type="var">A</span> (<span class="id" type="var">l</span> <span class="id" type="var">m</span> <span class="id" type="var">n</span>:<a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="Poly.html#A"><span class="id" type="variable">A</span></a>),<br/>
<a class="idref" href="Poly.html#l"><span class="id" type="variable">l</span></a> ++ <a class="idref" href="Poly.html#m"><span class="id" type="variable">m</span></a> ++ <a class="idref" href="Poly.html#n"><span class="id" type="variable">n</span></a> = (<a class="idref" href="Poly.html#l"><span class="id" type="variable">l</span></a> ++ <a class="idref" href="Poly.html#m"><span class="id" type="variable">m</span></a>) ++ <a class="idref" href="Poly.html#n"><span class="id" type="variable">n</span></a>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
<span class="id" type="keyword">Lemma</span> <a name="app_length"><span class="id" type="lemma">app_length</span></a> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) (<span class="id" type="var">l<sub>1</sub></span> <span class="id" type="var">l<sub>2</sub></span> : <a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a>),<br/>
<a class="idref" href="Poly.html#length"><span class="id" type="definition">length</span></a> (<a class="idref" href="Poly.html#l<sub>1</sub>"><span class="id" type="variable">l<sub>1</sub></span></a> ++ <a class="idref" href="Poly.html#l<sub>2</sub>"><span class="id" type="variable">l<sub>2</sub></span></a>) = <a class="idref" href="Poly.html#length"><span class="id" type="definition">length</span></a> <a class="idref" href="Poly.html#l<sub>1</sub>"><span class="id" type="variable">l<sub>1</sub></span></a> + <a class="idref" href="Poly.html#length"><span class="id" type="definition">length</span></a> <a class="idref" href="Poly.html#l<sub>2</sub>"><span class="id" type="variable">l<sub>2</sub></span></a>.<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 color="rgb(80%,80%,100%)">☐</font>
<div class="paragraph"> </div>
<a name="lab101"></a><h4 class="section">Exercise: 2 stars, optional (more_poly_exercises)</h4>
Here are some slightly more interesting ones...
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <a name="rev_app_distr"><span class="id" type="lemma">rev_app_distr</span></a>: <span style="font-family: arial;">∀</span><span class="id" type="var">X</span> (<span class="id" type="var">l<sub>1</sub></span> <span class="id" type="var">l<sub>2</sub></span> : <a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a>),<br/>
<a class="idref" href="Poly.html#rev"><span class="id" type="definition">rev</span></a> (<a class="idref" href="Poly.html#l<sub>1</sub>"><span class="id" type="variable">l<sub>1</sub></span></a> ++ <a class="idref" href="Poly.html#l<sub>2</sub>"><span class="id" type="variable">l<sub>2</sub></span></a>) = <a class="idref" href="Poly.html#rev"><span class="id" type="definition">rev</span></a> <a class="idref" href="Poly.html#l<sub>2</sub>"><span class="id" type="variable">l<sub>2</sub></span></a> ++ <a class="idref" href="Poly.html#rev"><span class="id" type="definition">rev</span></a> <a class="idref" href="Poly.html#l<sub>1</sub>"><span class="id" type="variable">l<sub>1</sub></span></a>.<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> <a name="rev_involutive"><span class="id" type="lemma">rev_involutive</span></a> : <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> : <a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a>,<br/>
<a class="idref" href="Poly.html#rev"><span class="id" type="definition">rev</span></a> (<a class="idref" href="Poly.html#rev"><span class="id" type="definition">rev</span></a> <a class="idref" href="Poly.html#l"><span class="id" type="variable">l</span></a>) = <a class="idref" href="Poly.html#l"><span class="id" type="variable">l</span></a>.<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 color="rgb(80%,80%,100%)">☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab102"></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>, often called <i>products</i>:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <a name="prod"><span class="id" type="inductive">prod</span></a> (<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/>
| <a name="pair"><span class="id" type="constructor">pair</span></a> : <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> <a class="idref" href="Poly.html#prod"><span class="id" type="inductive">prod</span></a> <span class="id" type="var">X</span> <span class="id" type="var">Y</span>.<br/>
<br/>
<span class="id" type="var">Arguments</span> <a class="idref" href="Poly.html#pair"><span class="id" type="constructor">pair</span></a> {<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 )" := (<a class="idref" href="Poly.html#pair"><span class="id" type="constructor">pair</span></a> <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 product <i>types</i>:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Notation</span> "X * Y" := (<a class="idref" href="Poly.html#prod"><span class="id" type="inductive">prod</span></a> <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 only be used when parsing types. This avoids a clash with
the multiplication symbol.)
<div class="paragraph"> </div>
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,
while <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> <a name="fst"><span class="id" type="definition">fst</span></a> {<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> : <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a> * <a class="idref" href="Poly.html#Y"><span class="id" type="variable">Y</span></a>) : <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a> :=<br/>
<span class="id" type="keyword">match</span> <a class="idref" href="Poly.html#p"><span class="id" type="variable">p</span></a> <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/>
<br/>
<span class="id" type="keyword">Definition</span> <a name="snd"><span class="id" type="definition">snd</span></a> {<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> : <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a> * <a class="idref" href="Poly.html#Y"><span class="id" type="variable">Y</span></a>) : <a class="idref" href="Poly.html#Y"><span class="id" type="variable">Y</span></a> :=<br/>
<span class="id" type="keyword">match</span> <a class="idref" href="Poly.html#p"><span class="id" type="variable">p</span></a> <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/>
</div>
<div class="doc">
The following function takes two lists and combines them
into a list of pairs. In other functional languages, it is often
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.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <a name="combine"><span class="id" type="definition">combine</span></a> {<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> : <a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a>) (<span class="id" type="var">ly</span> : <a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="Poly.html#Y"><span class="id" type="variable">Y</span></a>)<br/>
: <a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> (<a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a>*<a class="idref" href="Poly.html#Y"><span class="id" type="variable">Y</span></a>) :=<br/>
<span class="id" type="keyword">match</span> <a class="idref" href="Poly.html#lx"><span class="id" type="variable">lx</span></a>, <a class="idref" href="Poly.html#ly"><span class="id" type="variable">ly</span></a> <span class="id" type="keyword">with</span><br/>
| [], <span class="id" type="var">_</span> ⇒ []<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>) :: (<a class="idref" href="Poly.html#combine"><span class="id" type="definition">combine</span></a> <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="lab103"></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="var">Compute</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 color="rgb(80%,80%,100%)">☐</font>
</li>
</ul>
<div class="paragraph"> </div>
<a name="lab104"></a><h4 class="section">Exercise: 2 stars, recommended (split)</h4>
The function <span class="inlinecode"><span class="id" type="tactic">split</span></span> is the right inverse of <span class="inlinecode"><span class="id" type="var">combine</span></span>: it takes a
list of pairs and returns a pair of lists. In many functional
languages, it 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 test.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <a name="split"><span class="id" type="definition">split</span></a> {<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> : <a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> (<a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a>*<a class="idref" href="Poly.html#Y"><span class="id" type="variable">Y</span></a>))<br/>
: (<a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a>) * (<a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="Poly.html#Y"><span class="id" type="variable">Y</span></a>) <br/>
<span class="comment">(* REPLACE THIS LINE WITH := _your_definition_ . *)</span> . <span class="id" type="var">Admitted</span>.<br/>
<br/>
<span class="id" type="keyword">Example</span> <a name="test_split"><span class="id" type="definition">test_split</span></a>:<br/>
<a class="idref" href="Poly.html#split"><span class="id" type="axiom">split</span></a> [(1,<a class="idref" href="Basics.html#false"><span class="id" type="constructor">false</span></a>);(2,<a class="idref" href="Basics.html#false"><span class="id" type="constructor">false</span></a>)] = ([1;2],[<a class="idref" href="Basics.html#false"><span class="id" type="constructor">false</span></a>;<a class="idref" href="Basics.html#false"><span class="id" type="constructor">false</span></a>]).<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 color="rgb(80%,80%,100%)">☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab105"></a><h2 class="section">Polymorphic Options</h2>
<div class="paragraph"> </div>
One last polymorphic type for now: <i>polymorphic options</i>,
which generalize <span class="inlinecode"><span class="id" type="var">natoption</span></span> from the previous chapter:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <a name="option"><span class="id" type="inductive">option</span></a> (<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) : <span class="id" type="keyword">Type</span> :=<br/>
| <a name="Some"><span class="id" type="constructor">Some</span></a> : <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <a class="idref" href="Poly.html#option"><span class="id" type="inductive">option</span></a> <span class="id" type="var">X</span><br/>
| <a name="None"><span class="id" type="constructor">None</span></a> : <a class="idref" href="Poly.html#option"><span class="id" type="inductive">option</span></a> <span class="id" type="var">X</span>.<br/>
<br/>
<span class="id" type="var">Arguments</span> <a class="idref" href="Poly.html#Some"><span class="id" type="constructor">Some</span></a> {<span class="id" type="var">X</span>} <span class="id" type="var">_</span>.<br/>
<span class="id" type="var">Arguments</span> <a class="idref" href="Poly.html#None"><span class="id" type="constructor">None</span></a> {<span class="id" type="var">X</span>}.<br/>
<br/>
</div>
<div class="doc">
We can now rewrite the <span class="inlinecode"><span class="id" type="var">nth_error</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> <a name="nth_error"><span class="id" type="definition">nth_error</span></a> {<span class="id" type="var">X</span> : <span class="id" type="keyword">Type</span>} (<span class="id" type="var">l</span> : <a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a>) (<span class="id" type="var">n</span> : <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#nat"><span class="id" type="inductive">nat</span></a>)<br/>
: <a class="idref" href="Poly.html#option"><span class="id" type="inductive">option</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a> :=<br/>
<span class="id" type="keyword">match</span> <a class="idref" href="Poly.html#l"><span class="id" type="variable">l</span></a> <span class="id" type="keyword">with</span><br/>
| [] ⇒ <a class="idref" href="Poly.html#None"><span class="id" type="constructor">None</span></a><br/>
| <span class="id" type="var">a</span> :: <span class="id" type="var">l'</span> ⇒ <span class="id" type="keyword">if</span> <a class="idref" href="Basics.html#beq_nat"><span class="id" type="definition">beq_nat</span></a> <a class="idref" href="Poly.html#n"><span class="id" type="variable">n</span></a> <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#O"><span class="id" type="constructor">O</span></a> <span class="id" type="keyword">then</span> <a class="idref" href="Poly.html#Some"><span class="id" type="constructor">Some</span></a> <span class="id" type="var">a</span> <span class="id" type="keyword">else</span> <a class="idref" href="Poly.html#nth_error"><span class="id" type="definition">nth_error</span></a> <span class="id" type="var">l'</span> (<a class="idref" href="http://coq.inria.fr/library/Coq.Init.Peano.html#pred"><span class="id" type="abbreviation">pred</span></a> <a class="idref" href="Poly.html#n"><span class="id" type="variable">n</span></a>)<br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
<span class="id" type="keyword">Example</span> <a name="test_nth_error1"><span class="id" type="definition">test_nth_error1</span></a> : <a class="idref" href="Poly.html#nth_error"><span class="id" type="definition">nth_error</span></a> [4;5;6;7] 0 = <a class="idref" href="Poly.html#Some"><span class="id" type="constructor">Some</span></a> 4.<br/>
<div class="togglescript" id="proofcontrol1" onclick="toggleDisplay('proof1');toggleDisplay('proofcontrol1')"><span class="show"></span></div>
<div class="proofscript" id="proof1" onclick="toggleDisplay('proof1');toggleDisplay('proofcontrol1')">
<span class="id" type="keyword">Proof</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<span class="id" type="keyword">Example</span> <a name="test_nth_error2"><span class="id" type="definition">test_nth_error2</span></a> : <a class="idref" href="Poly.html#nth_error"><span class="id" type="definition">nth_error</span></a> [[1];[2]] 1 = <a class="idref" href="Poly.html#Some"><span class="id" type="constructor">Some</span></a> [2].<br/>
<div class="togglescript" id="proofcontrol2" onclick="toggleDisplay('proof2');toggleDisplay('proofcontrol2')"><span class="show"></span></div>
<div class="proofscript" id="proof2" onclick="toggleDisplay('proof2');toggleDisplay('proofcontrol2')">
<span class="id" type="keyword">Proof</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<span class="id" type="keyword">Example</span> <a name="test_nth_error3"><span class="id" type="definition">test_nth_error3</span></a> : <a class="idref" href="Poly.html#nth_error"><span class="id" type="definition">nth_error</span></a> [<a class="idref" href="Basics.html#true"><span class="id" type="constructor">true</span></a>] 2 = <a class="idref" href="Poly.html#None"><span class="id" type="constructor">None</span></a>.<br/>
<div class="togglescript" id="proofcontrol3" onclick="toggleDisplay('proof3');toggleDisplay('proofcontrol3')"><span class="show"></span></div>
<div class="proofscript" id="proof3" onclick="toggleDisplay('proof3');toggleDisplay('proofcontrol3')">
<span class="id" type="keyword">Proof</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
<a name="lab106"></a><h4 class="section">Exercise: 1 star, optional (hd_error_poly)</h4>
Complete the definition of a polymorphic version of the
<span class="inlinecode"><span class="id" type="var">hd_error</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> <a name="hd_error"><span class="id" type="definition">hd_error</span></a> {<span class="id" type="var">X</span> : <span class="id" type="keyword">Type</span>} (<span class="id" type="var">l</span> : <a class="idref" href="Poly.html#list"><span class="id" type="inductive">list</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a>) : <a class="idref" href="Poly.html#option"><span class="id" type="inductive">option</span></a> <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a> <br/>
<span class="comment">(* REPLACE THIS LINE WITH := _your_definition_ . *)</span> . <span class="id" type="var">Admitted</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> @<a class="idref" href="Poly.html#hd_error"><span class="id" type="axiom">hd_error</span></a>.<br/>
<br/>
<span class="id" type="keyword">Example</span> <a name="test_hd_error1"><span class="id" type="definition">test_hd_error1</span></a> : <a class="idref" href="Poly.html#hd_error"><span class="id" type="axiom">hd_error</span></a> [1;2] = <a class="idref" href="Poly.html#Some"><span class="id" type="constructor">Some</span></a> 1.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<span class="id" type="keyword">Example</span> <a name="test_hd_error2"><span class="id" type="definition">test_hd_error2</span></a> : <a class="idref" href="Poly.html#hd_error"><span class="id" type="axiom">hd_error</span></a> [[1];[2]] = <a class="idref" href="Poly.html#Some"><span class="id" type="constructor">Some</span></a> [1].<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2 color="rgb(80%,80%,100%)">☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab107"></a><h1 class="section">Functions as Data</h1>
<div class="paragraph"> </div>
Like many other modern programming languages — including
all functional languages (ML, Haskell, Scheme, Scala, Clojure,
etc.) — Coq treats functions as first-class citizens, allowing
them to be passed as arguments to other functions, returned as
results, stored in data structures, etc.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab108"></a><h2 class="section">Higher-Order Functions</h2>
<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> <a name="doit3times"><span class="id" type="definition">doit3times</span></a> {<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>} (<span class="id" type="var">f</span>:<a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a><span style="font-family: arial;">→</span><a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a>) (<span class="id" type="var">n</span>:<a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a>) : <a class="idref" href="Poly.html#X"><span class="id" type="variable">X</span></a> :=<br/>
<a class="idref" href="Poly.html#f"><span class="id" type="variable">f</span></a> (<a class="idref" href="Poly.html#f"><span class="id" type="variable">f</span></a> (<a class="idref" href="Poly.html#f"><span class="id" type="variable">f</span></a> <a class="idref" href="Poly.html#n"><span class="id" type="variable">n</span></a>)).<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> @<a class="idref" href="Poly.html#doit3times"><span class="id" type="definition">doit3times</span></a>.<br/>
<span class="comment">(* ===> doit3times : forall X : Type, (X -> X) -> X -> X *)</span><br/>
<br/>
<span class="id" type="keyword">Example</span> <a name="test_doit3times"><span class="id" type="definition">test_doit3times</span></a>: <a class="idref" href="Poly.html#doit3times"><span class="id" type="definition">doit3times</span></a> <a class="idref" href="Basics.html#minustwo"><span class="id" type="definition">minustwo</span></a> 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> <a name="test_doit3times'"><span class="id" type="definition">test_doit3times'</span></a>: <a class="idref" href="Poly.html#doit3times"><span class="id" type="definition">doit3times</span></a> <a class="idref" href="Basics.html#negb"><span class="id" type="definition">negb</span></a> <a class="idref" href="Basics.html#true"><span class="id" type="constructor">true</span></a> = <a class="idref" href="Basics.html#false"><span class="id" type="constructor">false</span></a>.<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>