-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasics.html
2705 lines (2225 loc) · 311 KB
/
Basics.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="common/css/sf.css" rel="stylesheet" type="text/css" />
<title>Basics: Functional Programming in Coq</title>
<link href="common/jquery-ui/jquery-ui.css" rel="stylesheet">
<script src="common/jquery-ui/external/jquery/jquery.js"></script>
<script src="common/jquery-ui/jquery-ui.js"></script>
<script src="common/toggleproofs.js"></script>
<link href="common/css/lf.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="page">
<div id="header">
<div id='logoinheader'><a href='https://softwarefoundations.cis.upenn.edu'>
<img src='common/media/image/sf_logo_sm.png' alt='Software Foundations Logo'></a></div>
<div class='booktitleinheader'><a href='index.html'>Volume 1: Logical Foundations</a></div>
<ul id='menu'>
<li class='section_name'><a href='toc.html'>Table of Contents</a></li>
<li class='section_name'><a href='coqindex.html'>Index</a></li>
<li class='section_name'><a href='deps.html'>Roadmap</a></li>
</ul>
</div>
<div id="main">
<h1 class="libtitle">Basics<span class="subtitle">Functional Programming in Coq</span></h1>
<div class="doc">
<a id="lab20"></a><h1 class="section">Introduction</h1>
<div class="paragraph"> </div>
The functional style of programming is founded on simple, everyday
mathematical intuitions: If a procedure or method has no side
effects, then (ignoring efficiency) all we need to understand
about it is how it maps inputs to outputs -- that is, we can think
of it as just a concrete method for computing a mathematical
function. This is one sense of the word "functional" in
"functional programming." The direct connection between programs
and simple mathematical objects supports both formal correctness
proofs and sound informal reasoning about program behavior.
<div class="paragraph"> </div>
The other sense in which functional programming is "functional" is
that it emphasizes the use of functions as <i>first-class</i> values --
i.e., values that can be passed as arguments to other functions,
returned as results, included in data structures, etc. The
recognition that functions can be treated as data gives rise to a
host of useful and powerful programming idioms.
<div class="paragraph"> </div>
Other common features of functional languages include <i>algebraic
data types</i> and <i>pattern matching</i>, which make it easy to
construct and manipulate rich data structures, and <i>polymorphic
type systems</i> supporting abstraction and code reuse. Coq offers
all of these features.
<div class="paragraph"> </div>
The first half of this chapter introduces the most essential
elements of Coq's native functional programming language,
<i>Gallina</i>. The second half introduces some basic <i>tactics</i> that
can be used to prove properties of Gallina programs.
</div>
<div class="doc">
<a id="lab21"></a><h1 class="section">Data and Functions</h1>
<div class="paragraph"> </div>
<a id="lab22"></a><h2 class="section">Enumerated Types</h2>
<div class="paragraph"> </div>
One notable thing about Coq is that its set of built-in
features is <i>extremely</i> small. For example, instead of providing
the usual palette of atomic data types (booleans, integers,
strings, etc.), Coq offers a powerful mechanism for defining new
data types from scratch, with all these familiar types as
instances.
<div class="paragraph"> </div>
Naturally, the Coq distribution comes with an extensive standard
library providing definitions of booleans, numbers, and many
common data structures like lists and hash tables. But there is
nothing magic or primitive about these library definitions. To
illustrate this, in this course we will explicitly recapitulate
(almost) all the definitions we need, rather than getting them
from the standard library.
</div>
<div class="doc">
<a id="lab23"></a><h2 class="section">Days of the Week</h2>
<div class="paragraph"> </div>
To see how this definition mechanism works, let's start with
a very simple example. The following declaration tells Coq that
we are defining a set of data values -- a <i>type</i>.
</div>
<div class="code">
<span class="id" title="keyword">Inductive</span> <a id="day" class="idref" href="#day"><span class="id" title="inductive">day</span></a> : <span class="id" title="keyword">Type</span> :=<br/>
| <a id="monday" class="idref" href="#monday"><span class="id" title="constructor">monday</span></a><br/>
| <a id="tuesday" class="idref" href="#tuesday"><span class="id" title="constructor">tuesday</span></a><br/>
| <a id="wednesday" class="idref" href="#wednesday"><span class="id" title="constructor">wednesday</span></a><br/>
| <a id="thursday" class="idref" href="#thursday"><span class="id" title="constructor">thursday</span></a><br/>
| <a id="friday" class="idref" href="#friday"><span class="id" title="constructor">friday</span></a><br/>
| <a id="saturday" class="idref" href="#saturday"><span class="id" title="constructor">saturday</span></a><br/>
| <a id="sunday" class="idref" href="#sunday"><span class="id" title="constructor">sunday</span></a>.<br/>
</div>
<div class="doc">
The new type is called <span class="inlinecode"><span class="id" title="var">day</span></span>, and its members are <span class="inlinecode"><span class="id" title="var">monday</span></span>,
<span class="inlinecode"><span class="id" title="var">tuesday</span></span>, etc.
<div class="paragraph"> </div>
Having defined <span class="inlinecode"><span class="id" title="var">day</span></span>, we can write functions that operate on
days.
</div>
<div class="code">
<span class="id" title="keyword">Definition</span> <a id="next_weekday" class="idref" href="#next_weekday"><span class="id" title="definition">next_weekday</span></a> (<a id="d:3" class="idref" href="#d:3"><span class="id" title="binder">d</span></a>:<a class="idref" href="Basics.html#day"><span class="id" title="inductive">day</span></a>) : <a class="idref" href="Basics.html#day"><span class="id" title="inductive">day</span></a> :=<br/>
<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#d:3"><span class="id" title="variable">d</span></a> <span class="id" title="keyword">with</span><br/>
| <a class="idref" href="Basics.html#monday"><span class="id" title="constructor">monday</span></a> ⇒ <a class="idref" href="Basics.html#tuesday"><span class="id" title="constructor">tuesday</span></a><br/>
| <a class="idref" href="Basics.html#tuesday"><span class="id" title="constructor">tuesday</span></a> ⇒ <a class="idref" href="Basics.html#wednesday"><span class="id" title="constructor">wednesday</span></a><br/>
| <a class="idref" href="Basics.html#wednesday"><span class="id" title="constructor">wednesday</span></a> ⇒ <a class="idref" href="Basics.html#thursday"><span class="id" title="constructor">thursday</span></a><br/>
| <a class="idref" href="Basics.html#thursday"><span class="id" title="constructor">thursday</span></a> ⇒ <a class="idref" href="Basics.html#friday"><span class="id" title="constructor">friday</span></a><br/>
| <a class="idref" href="Basics.html#friday"><span class="id" title="constructor">friday</span></a> ⇒ <a class="idref" href="Basics.html#monday"><span class="id" title="constructor">monday</span></a><br/>
| <a class="idref" href="Basics.html#saturday"><span class="id" title="constructor">saturday</span></a> ⇒ <a class="idref" href="Basics.html#monday"><span class="id" title="constructor">monday</span></a><br/>
| <a class="idref" href="Basics.html#sunday"><span class="id" title="constructor">sunday</span></a> ⇒ <a class="idref" href="Basics.html#monday"><span class="id" title="constructor">monday</span></a><br/>
<span class="id" title="keyword">end</span>.<br/>
</div>
<div class="doc">
Note that the argument and return types of this function are
explicitly declared here. Like most functional programming
languages, Coq can often figure out these types for itself when
they are not given explicitly -- i.e., it can do <i>type inference</i>
-- but we'll generally include them to make reading easier.
<div class="paragraph"> </div>
Having defined a function, we can check that it works on
some examples. There are actually three different ways to do
examples in Coq. First, we can use the command <span class="inlinecode"><span class="id" title="keyword">Compute</span></span> to
evaluate a compound expression involving <span class="inlinecode"><span class="id" title="var">next_weekday</span></span>.
</div>
<div class="code">
<span class="id" title="keyword">Compute</span> (<a class="idref" href="Basics.html#next_weekday"><span class="id" title="definition">next_weekday</span></a> <a class="idref" href="Basics.html#friday"><span class="id" title="constructor">friday</span></a>).<br/>
<span class="comment">(* ==> monday : day *)</span><br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Compute</span> (<a class="idref" href="Basics.html#next_weekday"><span class="id" title="definition">next_weekday</span></a> (<a class="idref" href="Basics.html#next_weekday"><span class="id" title="definition">next_weekday</span></a> <a class="idref" href="Basics.html#saturday"><span class="id" title="constructor">saturday</span></a>)).<br/>
<span class="comment">(* ==> tuesday : day *)</span><br/>
</div>
<div class="doc">
(We show Coq's responses in comments; if you have a computer
handy, this would be an excellent moment to fire up the Coq
interpreter under your favorite IDE (see the <a href="Preface.html"><span class="inlineref">Preface</span></a> for
installation instructions) and try it for yourself. Load this
file, <span class="inlinecode"><span class="id" title="var">Basics.v</span></span>, from the book's Coq sources, find the above
example, submit it to Coq, and observe the result.)
<div class="paragraph"> </div>
Second, we can record what we <i>expect</i> the result to be in the
form of a Coq example:
</div>
<div class="code">
<span class="id" title="keyword">Example</span> <a id="test_next_weekday" class="idref" href="#test_next_weekday"><span class="id" title="definition">test_next_weekday</span></a>:<br/>
<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#next_weekday"><span class="id" title="definition">next_weekday</span></a> (<a class="idref" href="Basics.html#next_weekday"><span class="id" title="definition">next_weekday</span></a> <a class="idref" href="Basics.html#saturday"><span class="id" title="constructor">saturday</span></a>)<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#tuesday"><span class="id" title="constructor">tuesday</span></a>.<br/>
</div>
<div class="doc">
This declaration does two things: it makes an assertion
(that the second weekday after <span class="inlinecode"><span class="id" title="var">saturday</span></span> is <span class="inlinecode"><span class="id" title="var">tuesday</span></span>), and it
gives the assertion a name that can be used to refer to it later.
Having made the assertion, we can also ask Coq to verify it like
this:
</div>
<div class="code">
<span class="id" title="keyword">Proof</span>. <span class="id" title="tactic">simpl</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
</div>
<div class="doc">
The details are not important just now, but essentially this
little script can be read as "The assertion we've just made can be
proved by observing that both sides of the equality evaluate to
the same thing."
<div class="paragraph"> </div>
Third, we can ask Coq to <i>extract</i>, from our <span class="inlinecode"><span class="id" title="keyword">Definition</span></span>, a
program in a more conventional programming language (OCaml,
Scheme, or Haskell) with a high-performance compiler. This
facility is very useful, since it gives us a path from
proved-correct algorithms written in Gallina to efficient machine
code.
<div class="paragraph"> </div>
(Of course, we are trusting the correctness of the
OCaml/Haskell/Scheme compiler, and of Coq's extraction facility
itself, but this is still a big step forward from the way most
software is developed today!)
<div class="paragraph"> </div>
Indeed, this is one of the main uses for which Coq was developed.
We'll come back to this topic in later chapters.
</div>
<div class="doc">
<a id="lab24"></a><h2 class="section">Homework Submission Guidelines</h2>
<div class="paragraph"> </div>
If you are using <i>Software Foundations</i> in a course, your
instructor may use automatic scripts to help grade your homework
assignments. In order for these scripts to work correctly (and
ensure that you get full credit for your work!), please be
careful to follow these rules:
<div class="paragraph"> </div>
<ul class="doclist">
<li> Do not change the names of exercises. Otherwise the grading
scripts will be unable to find your solution.
</li>
<li> Do not delete exercises. If you skip an exercise (e.g.,
because it is marked "optional," or because you can't solve it),
it is OK to leave a partial proof in your <span class="inlinecode">.<span class="id" title="var">v</span></span> file; in
this case, please make sure it ends with the keyword <span class="inlinecode"><span class="id" title="var">Admitted</span></span>
(not, for example <span class="inlinecode"><span class="id" title="keyword">Abort</span></span>).
</li>
<li> It is fine to use additional definitions (of helper functions,
useful lemmas, etc.) in your solutions. You can put these
before the theorem you are asked to prove.
</li>
<li> If you introduce a helper lemma that you end up being unable
to prove, hence end it with <span class="inlinecode"><span class="id" title="var">Admitted</span></span>, then make sure to also
end the main theorem in which you use it with <span class="inlinecode"><span class="id" title="var">Admitted</span></span>, not
<span class="inlinecode"><span class="id" title="keyword">Qed</span></span>. This will help you get partial credit, in case you
use that main theorem to solve a later exercise.
</li>
</ul>
<div class="paragraph"> </div>
You will also notice that each chapter (like <span class="inlinecode"><span class="id" title="var">Basics.v</span></span>) is
accompanied by a <i>test script</i> (<span class="inlinecode"><span class="id" title="var">BasicsTest.v</span></span>) that automatically
calculates points for the finished homework problems in the
chapter. These scripts are mostly for the auto-grading
tools, but you may also want to use them to double-check
that your file is well formatted before handing it in. In a
terminal window, either type "<span class="inlinecode"><span class="id" title="var">make</span></span> <span class="inlinecode"><span class="id" title="var">BasicsTest.vo</span></span>" or do the
following:
<br/>
<span class="inlinecode"> <span class="id" title="var">coqc</span> -<span class="id" title="var">Q</span> . <span class="id" title="var">LF</span> <span class="id" title="var">Basics.v</span><br/>
<span class="id" title="var">coqc</span> -<span class="id" title="var">Q</span> . <span class="id" title="var">LF</span> <span class="id" title="var">BasicsTest.v</span>
</span> See the end of this chapter for more information about how to interpret
the output of test scripts.
<div class="paragraph"> </div>
There is no need to hand in <span class="inlinecode"><span class="id" title="var">BasicsTest.v</span></span> itself (or <span class="inlinecode"><span class="id" title="var">Preface.v</span></span>).
<div class="paragraph"> </div>
If your class is using the Canvas system to hand in assignments...
<ul class="doclist">
<li> If you submit multiple versions of the assignment, you may
notice that they are given different names. This is fine: The
most recent submission is the one that will be graded.
</li>
<li> If you want to hand in multiple files at the same time (if more
than one chapter is assigned in the same week), you need to make a
single submission with all the files at once using the
"Add another file" button just above the comment box.
</li>
</ul>
<div class="paragraph"> </div>
The <span class="inlinecode"><span class="id" title="keyword">Require</span></span> <span class="inlinecode"><span class="id" title="keyword">Export</span></span> statement on the next line tells Coq to use
the <span class="inlinecode"><span class="id" title="var">String</span></span> module from the standard library. We'll use strings
for various things in later chapters, but we need to <span class="inlinecode"><span class="id" title="keyword">Require</span></span> it here so
that the grading scripts can use it for internal purposes.
</div>
<div class="code">
<span class="id" title="keyword">From</span> <span class="id" title="var">Coq</span> <span class="id" title="keyword">Require</span> <span class="id" title="keyword">Export</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Strings.String.html#"><span class="id" title="library">String</span></a>.<br/>
</div>
<div class="doc">
<a id="lab25"></a><h2 class="section">Booleans</h2>
<div class="paragraph"> </div>
Following the pattern of the days of the week above, we can
define the standard type <span class="inlinecode"><span class="id" title="var">bool</span></span> of booleans, with members <span class="inlinecode"><span class="id" title="var">true</span></span>
and <span class="inlinecode"><span class="id" title="var">false</span></span>.
</div>
<div class="code">
<span class="id" title="keyword">Inductive</span> <a id="bool" class="idref" href="#bool"><span class="id" title="inductive">bool</span></a> : <span class="id" title="keyword">Type</span> :=<br/>
| <a id="true" class="idref" href="#true"><span class="id" title="constructor">true</span></a><br/>
| <a id="false" class="idref" href="#false"><span class="id" title="constructor">false</span></a>.<br/>
</div>
<div class="doc">
Functions over booleans can be defined in the same way as
above:
</div>
<div class="code">
<span class="id" title="keyword">Definition</span> <a id="negb" class="idref" href="#negb"><span class="id" title="definition">negb</span></a> (<a id="b:7" class="idref" href="#b:7"><span class="id" title="binder">b</span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#b:7"><span class="id" title="variable">b</span></a> <span class="id" title="keyword">with</span><br/>
| <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> ⇒ <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
| <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> ⇒ <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><br/>
<span class="id" title="keyword">end</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Definition</span> <a id="andb" class="idref" href="#andb"><span class="id" title="definition">andb</span></a> (<a id="b<sub>1</sub>:9" class="idref" href="#b<sub>1</sub>:9"><span class="id" title="binder">b<sub>1</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) (<a id="b<sub>2</sub>:10" class="idref" href="#b<sub>2</sub>:10"><span class="id" title="binder">b<sub>2</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#b<sub>1</sub>:9"><span class="id" title="variable">b<sub>1</sub></span></a> <span class="id" title="keyword">with</span><br/>
| <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> ⇒ <a class="idref" href="Basics.html#b<sub>2</sub>:10"><span class="id" title="variable">b<sub>2</sub></span></a><br/>
| <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> ⇒ <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
<span class="id" title="keyword">end</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Definition</span> <a id="orb" class="idref" href="#orb"><span class="id" title="definition">orb</span></a> (<a id="b<sub>1</sub>:12" class="idref" href="#b<sub>1</sub>:12"><span class="id" title="binder">b<sub>1</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) (<a id="b<sub>2</sub>:13" class="idref" href="#b<sub>2</sub>:13"><span class="id" title="binder">b<sub>2</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#b<sub>1</sub>:12"><span class="id" title="variable">b<sub>1</sub></span></a> <span class="id" title="keyword">with</span><br/>
| <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> ⇒ <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><br/>
| <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> ⇒ <a class="idref" href="Basics.html#b<sub>2</sub>:13"><span class="id" title="variable">b<sub>2</sub></span></a><br/>
<span class="id" title="keyword">end</span>.<br/>
</div>
<div class="doc">
(Although we are rolling our own booleans here for the sake
of building up everything from scratch, Coq does, of course,
provide a default implementation of the booleans, together with a
multitude of useful functions and lemmas. Whereever possible,
we've named our own definitions and theorems to match the ones in
the standard library.)
<div class="paragraph"> </div>
The last two of these illustrate Coq's syntax for
multi-argument function definitions. The corresponding
multi-argument <i>application</i> syntax is illustrated by the
following "unit tests," which constitute a complete specification
a truth table -- for the <span class="inlinecode"><span class="id" title="var">orb</span></span> function:
</div>
<div class="code">
<span class="id" title="keyword">Example</span> <a id="test_orb1" class="idref" href="#test_orb1"><span class="id" title="definition">test_orb1</span></a>: <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#orb"><span class="id" title="definition">orb</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="id" title="keyword">Proof</span>. <span class="id" title="tactic">simpl</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_orb2" class="idref" href="#test_orb2"><span class="id" title="definition">test_orb2</span></a>: <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#orb"><span class="id" title="definition">orb</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/>
<span class="id" title="keyword">Proof</span>. <span class="id" title="tactic">simpl</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_orb3" class="idref" href="#test_orb3"><span class="id" title="definition">test_orb3</span></a>: <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#orb"><span class="id" title="definition">orb</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="id" title="keyword">Proof</span>. <span class="id" title="tactic">simpl</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_orb4" class="idref" href="#test_orb4"><span class="id" title="definition">test_orb4</span></a>: <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#orb"><span class="id" title="definition">orb</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="id" title="keyword">Proof</span>. <span class="id" title="tactic">simpl</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
</div>
<div class="doc">
We can also introduce some familiar infix syntax for the
boolean operations we have just defined. The <span class="inlinecode"><span class="id" title="keyword">Notation</span></span> command
defines a new symbolic notation for an existing definition.
</div>
<div class="code">
<span class="id" title="keyword">Notation</span> <a id=":::x_'&&'_x" class="idref" href="#:::x_'&&'_x"><span class="id" title="notation">"</span></a>x && y" := (<a class="idref" href="Basics.html#andb"><span class="id" title="definition">andb</span></a> <span class="id" title="var">x</span> <span class="id" title="var">y</span>).<br/>
<span class="id" title="keyword">Notation</span> <a id="34428c8531fd724748c0ebf19a35c764" class="idref" href="#34428c8531fd724748c0ebf19a35c764"><span class="id" title="notation">"</span></a>x || y" := (<a class="idref" href="Basics.html#orb"><span class="id" title="definition">orb</span></a> <span class="id" title="var">x</span> <span class="id" title="var">y</span>).<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Example</span> <a id="test_orb5" class="idref" href="#test_orb5"><span class="id" title="definition">test_orb5</span></a>: <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> <a class="idref" href="Basics.html#34428c8531fd724748c0ebf19a35c764"><span class="id" title="notation">||</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> <a class="idref" href="Basics.html#34428c8531fd724748c0ebf19a35c764"><span class="id" title="notation">||</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="id" title="keyword">Proof</span>. <span class="id" title="tactic">simpl</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
</div>
<div class="doc">
<i>A note on notation</i>: In <span class="inlinecode">.<span class="id" title="var">v</span></span> files, we use square brackets
to delimit fragments of Coq code within comments; this convention,
also used by the <span class="inlinecode"><span class="id" title="var">coqdoc</span></span> documentation tool, keeps them visually
separate from the surrounding text. In the HTML version of the
files, these pieces of text appear in a different font.
<div class="paragraph"> </div>
These examples are also an opportunity to introduce one more small
feature of Coq's programming language: conditional expressions...
</div>
<div class="code">
<span class="id" title="keyword">Definition</span> <a id="negb'" class="idref" href="#negb'"><span class="id" title="definition">negb'</span></a> (<a id="b:15" class="idref" href="#b:15"><span class="id" title="binder">b</span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
<span class="id" title="keyword">if</span> <a class="idref" href="Basics.html#b:15"><span class="id" title="variable">b</span></a> <span class="id" title="keyword">then</span> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
<span class="id" title="keyword">else</span> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Definition</span> <a id="andb'" class="idref" href="#andb'"><span class="id" title="definition">andb'</span></a> (<a id="b<sub>1</sub>:16" class="idref" href="#b<sub>1</sub>:16"><span class="id" title="binder">b<sub>1</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) (<a id="b<sub>2</sub>:17" class="idref" href="#b<sub>2</sub>:17"><span class="id" title="binder">b<sub>2</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
<span class="id" title="keyword">if</span> <a class="idref" href="Basics.html#b<sub>1</sub>:16"><span class="id" title="variable">b<sub>1</sub></span></a> <span class="id" title="keyword">then</span> <a class="idref" href="Basics.html#b<sub>2</sub>:17"><span class="id" title="variable">b<sub>2</sub></span></a><br/>
<span class="id" title="keyword">else</span> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Definition</span> <a id="orb'" class="idref" href="#orb'"><span class="id" title="definition">orb'</span></a> (<a id="b<sub>1</sub>:18" class="idref" href="#b<sub>1</sub>:18"><span class="id" title="binder">b<sub>1</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) (<a id="b<sub>2</sub>:19" class="idref" href="#b<sub>2</sub>:19"><span class="id" title="binder">b<sub>2</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
<span class="id" title="keyword">if</span> <a class="idref" href="Basics.html#b<sub>1</sub>:18"><span class="id" title="variable">b<sub>1</sub></span></a> <span class="id" title="keyword">then</span> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><br/>
<span class="id" title="keyword">else</span> <a class="idref" href="Basics.html#b<sub>2</sub>:19"><span class="id" title="variable">b<sub>2</sub></span></a>.<br/>
</div>
<div class="doc">
Coq's conditionals are exactly like those found in any other
language, with one small generalization:
<div class="paragraph"> </div>
Since the <span class="inlinecode"><span class="id" title="var">bool</span></span> type is not built in, Coq actually supports
conditional expressions over <i>any</i> inductively defined type with
exactly two clauses in its definition. The guard is considered
true if it evaluates to the "constructor" of the first clause of
the <span class="inlinecode"><span class="id" title="keyword">Inductive</span></span> definition (which just happens to be called <span class="inlinecode"><span class="id" title="var">true</span></span>
in this case) and false if it evaluates to the second.
<div class="paragraph"> </div>
<a id="lab26"></a><h4 class="section">Exercise: 1 star, standard (nandb)</h4>
The <span class="inlinecode"><span class="id" title="var">Admitted</span></span> command can be used as a placeholder for an
incomplete proof. We use it in exercises to indicate the parts
that we're leaving for you -- i.e., your job is to replace
<span class="inlinecode"><span class="id" title="var">Admitted</span></span>s with real proofs.
<div class="paragraph"> </div>
Remove "<span class="inlinecode"><span class="id" title="var">Admitted</span>.</span>" and complete the definition of the following
function; then make sure that the <span class="inlinecode"><span class="id" title="keyword">Example</span></span> assertions below can
each be verified by Coq. (I.e., fill in each proof, following the
model of the <span class="inlinecode"><span class="id" title="var">orb</span></span> tests above, and make sure Coq accepts it.) The
function should return <span class="inlinecode"><span class="id" title="var">true</span></span> if either or both of its inputs are
<span class="inlinecode"><span class="id" title="var">false</span></span>.
<div class="paragraph"> </div>
Hint: if <span class="inlinecode"><span class="id" title="tactic">simpl</span></span> will not simplify the goal in your proof, it's
probably because you defined <span class="inlinecode"><span class="id" title="var">nandb</span></span> without using a <span class="inlinecode"><span class="id" title="keyword">match</span></span>
expression. Try a different definition of <span class="inlinecode"><span class="id" title="var">nandb</span></span>, or just
skip over <span class="inlinecode"><span class="id" title="tactic">simpl</span></span> and go directly to <span class="inlinecode"><span class="id" title="tactic">reflexivity</span></span>. We'll
explain this phenomenon later in the chapter.
</div>
<div class="code">
<span class="id" title="keyword">Definition</span> <a id="nandb" class="idref" href="#nandb"><span class="id" title="definition">nandb</span></a> (<a id="b<sub>1</sub>:20" class="idref" href="#b<sub>1</sub>:20"><span class="id" title="binder">b<sub>1</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) (<a id="b<sub>2</sub>:21" class="idref" href="#b<sub>2</sub>:21"><span class="id" title="binder">b<sub>2</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a><br/>
<span class="comment">(* REPLACE THIS LINE WITH ":= _your_definition_ ." *)</span>. <span class="id" title="var">Admitted</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Example</span> <a id="test_nandb1" class="idref" href="#test_nandb1"><span class="id" title="definition">test_nandb1</span></a>: <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#nandb"><span class="id" title="axiom">nandb</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" title="var">Admitted</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_nandb2" class="idref" href="#test_nandb2"><span class="id" title="definition">test_nandb2</span></a>: <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#nandb"><span class="id" title="axiom">nandb</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" title="var">Admitted</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_nandb3" class="idref" href="#test_nandb3"><span class="id" title="definition">test_nandb3</span></a>: <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#nandb"><span class="id" title="axiom">nandb</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" title="var">Admitted</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_nandb4" class="idref" href="#test_nandb4"><span class="id" title="definition">test_nandb4</span></a>: <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#nandb"><span class="id" title="axiom">nandb</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" title="var">Admitted</span>.<br/>
<font size=-2>☐</font>
</div>
<div class="doc">
<div class="paragraph"> </div>
<a id="lab27"></a><h4 class="section">Exercise: 1 star, standard (andb3)</h4>
Do the same for the <span class="inlinecode"><span class="id" title="var">andb3</span></span> function below. This function should
return <span class="inlinecode"><span class="id" title="var">true</span></span> when all of its inputs are <span class="inlinecode"><span class="id" title="var">true</span></span>, and <span class="inlinecode"><span class="id" title="var">false</span></span>
otherwise.
</div>
<div class="code">
<span class="id" title="keyword">Definition</span> <a id="andb3" class="idref" href="#andb3"><span class="id" title="definition">andb3</span></a> (<a id="b<sub>1</sub>:22" class="idref" href="#b<sub>1</sub>:22"><span class="id" title="binder">b<sub>1</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) (<a id="b<sub>2</sub>:23" class="idref" href="#b<sub>2</sub>:23"><span class="id" title="binder">b<sub>2</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) (<a id="b<sub>3</sub>:24" class="idref" href="#b<sub>3</sub>:24"><span class="id" title="binder">b<sub>3</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a><br/>
<span class="comment">(* REPLACE THIS LINE WITH ":= _your_definition_ ." *)</span>. <span class="id" title="var">Admitted</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Example</span> <a id="test_andb31" class="idref" href="#test_andb31"><span class="id" title="definition">test_andb31</span></a>: <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#andb3"><span class="id" title="axiom">andb3</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" title="var">Admitted</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_andb32" class="idref" href="#test_andb32"><span class="id" title="definition">test_andb32</span></a>: <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#andb3"><span class="id" title="axiom">andb3</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" title="var">Admitted</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_andb33" class="idref" href="#test_andb33"><span class="id" title="definition">test_andb33</span></a>: <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#andb3"><span class="id" title="axiom">andb3</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" title="var">Admitted</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_andb34" class="idref" href="#test_andb34"><span class="id" title="definition">test_andb34</span></a>: <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#andb3"><span class="id" title="axiom">andb3</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" title="var">Admitted</span>.<br/>
<font size=-2>☐</font>
</div>
<div class="doc">
<a id="lab28"></a><h2 class="section">Types</h2>
<div class="paragraph"> </div>
Every expression in Coq has a type describing what sort of
thing it computes. The <span class="inlinecode"><span class="id" title="keyword">Check</span></span> command asks Coq to print the type
of an expression.
</div>
<div class="code">
<span class="id" title="keyword">Check</span> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="comment">(* ===> true : bool *)</span><br/>
</div>
<div class="doc">
If the thing after <span class="inlinecode"><span class="id" title="keyword">Check</span></span> is followed by a colon and a type
declaration, Coq will verify that the type of the expression
matches the given type and halt with an error if not.
</div>
<div class="code">
<span class="id" title="keyword">Check</span> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><br/>
: <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>.<br/>
<span class="id" title="keyword">Check</span> (<a class="idref" href="Basics.html#negb"><span class="id" title="definition">negb</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>)<br/>
: <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>.<br/>
</div>
<div class="doc">
Functions like <span class="inlinecode"><span class="id" title="var">negb</span></span> itself are also data values, just like
<span class="inlinecode"><span class="id" title="var">true</span></span> and <span class="inlinecode"><span class="id" title="var">false</span></span>. Their types are called <i>function types</i>, and
they are written with arrows.
</div>
<div class="code">
<span class="id" title="keyword">Check</span> <a class="idref" href="Basics.html#negb"><span class="id" title="definition">negb</span></a><br/>
: <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">→</span></a> <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>.<br/>
</div>
<div class="doc">
The type of <span class="inlinecode"><span class="id" title="var">negb</span></span>, written <span class="inlinecode"><span class="id" title="var">bool</span></span> <span class="inlinecode">→</span> <span class="inlinecode"><span class="id" title="var">bool</span></span> and pronounced
"<span class="inlinecode"><span class="id" title="var">bool</span></span> arrow <span class="inlinecode"><span class="id" title="var">bool</span></span>," can be read, "Given an input of type
<span class="inlinecode"><span class="id" title="var">bool</span></span>, this function produces an output of type <span class="inlinecode"><span class="id" title="var">bool</span></span>."
Similarly, the type of <span class="inlinecode"><span class="id" title="var">andb</span></span>, written <span class="inlinecode"><span class="id" title="var">bool</span></span> <span class="inlinecode">→</span> <span class="inlinecode"><span class="id" title="var">bool</span></span> <span class="inlinecode">→</span> <span class="inlinecode"><span class="id" title="var">bool</span></span>, can
be read, "Given two inputs, each of type <span class="inlinecode"><span class="id" title="var">bool</span></span>, this function
produces an output of type <span class="inlinecode"><span class="id" title="var">bool</span></span>."
</div>
<div class="doc">
<a id="lab29"></a><h2 class="section">New Types from Old</h2>
<div class="paragraph"> </div>
The types we have defined so far are examples of "enumerated
types": their definitions explicitly enumerate a finite set of
elements, called <i>constructors</i>. Here is a more interesting type
definition, where one of the constructors takes an argument:
</div>
<div class="code">
<span class="id" title="keyword">Inductive</span> <a id="rgb" class="idref" href="#rgb"><span class="id" title="inductive">rgb</span></a> : <span class="id" title="keyword">Type</span> :=<br/>
| <a id="red" class="idref" href="#red"><span class="id" title="constructor">red</span></a><br/>
| <a id="green" class="idref" href="#green"><span class="id" title="constructor">green</span></a><br/>
| <a id="blue" class="idref" href="#blue"><span class="id" title="constructor">blue</span></a>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Inductive</span> <a id="color" class="idref" href="#color"><span class="id" title="inductive">color</span></a> : <span class="id" title="keyword">Type</span> :=<br/>
| <a id="black" class="idref" href="#black"><span class="id" title="constructor">black</span></a><br/>
| <a id="white" class="idref" href="#white"><span class="id" title="constructor">white</span></a><br/>
| <a id="primary" class="idref" href="#primary"><span class="id" title="constructor">primary</span></a> (<a id="p:29" class="idref" href="#p:29"><span class="id" title="binder">p</span></a> : <a class="idref" href="Basics.html#rgb"><span class="id" title="inductive">rgb</span></a>).<br/>
</div>
<div class="doc">
Let's look at this in a little more detail.
<div class="paragraph"> </div>
An <span class="inlinecode"><span class="id" title="keyword">Inductive</span></span> definition does two things:
<div class="paragraph"> </div>
<ul class="doclist">
<li> It defines a set of new <i>constructors</i>. E.g., <span class="inlinecode"><span class="id" title="tactic">red</span></span>,
<span class="inlinecode"><span class="id" title="var">primary</span></span>, <span class="inlinecode"><span class="id" title="var">true</span></span>, <span class="inlinecode"><span class="id" title="var">false</span></span>, <span class="inlinecode"><span class="id" title="var">monday</span></span>, etc. are constructors.
<div class="paragraph"> </div>
</li>
<li> It groups them into a new named type, like <span class="inlinecode"><span class="id" title="var">bool</span></span>, <span class="inlinecode"><span class="id" title="var">rgb</span></span>, or
<span class="inlinecode"><span class="id" title="var">color</span></span>.
</li>
</ul>
<div class="paragraph"> </div>
<i>Constructor expressions</i> are formed by applying a constructor
to zero or more other constructors or constructor expressions,
obeying the declared number and types of the constructor arguments.
E.g., these are valid constructor expressions...
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" title="tactic">red</span></span>
</li>
<li> <span class="inlinecode"><span class="id" title="var">true</span></span>
</li>
<li> <span class="inlinecode"><span class="id" title="var">primary</span></span> <span class="inlinecode"><span class="id" title="tactic">red</span></span>
</li>
<li> etc.
</li>
</ul>
...but these are not:
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" title="tactic">red</span></span> <span class="inlinecode"><span class="id" title="var">primary</span></span>
</li>
<li> <span class="inlinecode"><span class="id" title="var">true</span></span> <span class="inlinecode"><span class="id" title="tactic">red</span></span>
</li>
<li> <span class="inlinecode"><span class="id" title="var">primary</span></span> <span class="inlinecode">(<span class="id" title="var">primary</span></span> <span class="inlinecode"><span class="id" title="tactic">red</span>)</span>
</li>
<li> etc.
</li>
</ul>
<div class="paragraph"> </div>
In particular, the definitions of <span class="inlinecode"><span class="id" title="var">rgb</span></span> and <span class="inlinecode"><span class="id" title="var">color</span></span> say
which constructor expressions belong to the sets <span class="inlinecode"><span class="id" title="var">rgb</span></span> and
<span class="inlinecode"><span class="id" title="var">color</span></span>:
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" title="tactic">red</span></span>, <span class="inlinecode"><span class="id" title="var">green</span></span>, and <span class="inlinecode"><span class="id" title="var">blue</span></span> belong to the set <span class="inlinecode"><span class="id" title="var">rgb</span></span>;
</li>
<li> <span class="inlinecode"><span class="id" title="var">black</span></span> and <span class="inlinecode"><span class="id" title="var">white</span></span> belong to the set <span class="inlinecode"><span class="id" title="var">color</span></span>;
</li>
<li> if <span class="inlinecode"><span class="id" title="var">p</span></span> is a constructor expression belonging to the set <span class="inlinecode"><span class="id" title="var">rgb</span></span>,
then <span class="inlinecode"><span class="id" title="var">primary</span></span> <span class="inlinecode"><span class="id" title="var">p</span></span> ("the constructor <span class="inlinecode"><span class="id" title="var">primary</span></span> applied to the
argument <span class="inlinecode"><span class="id" title="var">p</span></span>") is a constructor expression belonging to the set
<span class="inlinecode"><span class="id" title="var">color</span></span>; and
</li>
<li> constructor expressions formed in these ways are the <i>only</i> ones
belonging to the sets <span class="inlinecode"><span class="id" title="var">rgb</span></span> and <span class="inlinecode"><span class="id" title="var">color</span></span>.
</li>
</ul>
<div class="paragraph"> </div>
We can define functions on colors using pattern matching just as
we did for <span class="inlinecode"><span class="id" title="var">day</span></span> and <span class="inlinecode"><span class="id" title="var">bool</span></span>.
</div>
<div class="code">
<span class="id" title="keyword">Definition</span> <a id="monochrome" class="idref" href="#monochrome"><span class="id" title="definition">monochrome</span></a> (<a id="c:30" class="idref" href="#c:30"><span class="id" title="binder">c</span></a> : <a class="idref" href="Basics.html#color"><span class="id" title="inductive">color</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#c:30"><span class="id" title="variable">c</span></a> <span class="id" title="keyword">with</span><br/>
| <a class="idref" href="Basics.html#black"><span class="id" title="constructor">black</span></a> ⇒ <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><br/>
| <a class="idref" href="Basics.html#white"><span class="id" title="constructor">white</span></a> ⇒ <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><br/>
| <a class="idref" href="Basics.html#primary"><span class="id" title="constructor">primary</span></a> <span class="id" title="var">p</span> ⇒ <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
<span class="id" title="keyword">end</span>.<br/>
</div>
<div class="doc">
Since the <span class="inlinecode"><span class="id" title="var">primary</span></span> constructor takes an argument, a pattern
matching <span class="inlinecode"><span class="id" title="var">primary</span></span> should include either a variable, as we just
did (note that we can choose its name freely), or a constant of
appropriate type (as below).
</div>
<div class="code">
<span class="id" title="keyword">Definition</span> <a id="isred" class="idref" href="#isred"><span class="id" title="definition">isred</span></a> (<a id="c:32" class="idref" href="#c:32"><span class="id" title="binder">c</span></a> : <a class="idref" href="Basics.html#color"><span class="id" title="inductive">color</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#c:32"><span class="id" title="variable">c</span></a> <span class="id" title="keyword">with</span><br/>
| <a class="idref" href="Basics.html#black"><span class="id" title="constructor">black</span></a> ⇒ <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
| <a class="idref" href="Basics.html#white"><span class="id" title="constructor">white</span></a> ⇒ <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
| <a class="idref" href="Basics.html#primary"><span class="id" title="constructor">primary</span></a> <a class="idref" href="Basics.html#red"><span class="id" title="constructor">red</span></a> ⇒ <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><br/>
| <a class="idref" href="Basics.html#primary"><span class="id" title="constructor">primary</span></a> <span class="id" title="var">_</span> ⇒ <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
<span class="id" title="keyword">end</span>.<br/>
</div>
<div class="doc">
The pattern "<span class="inlinecode"><span class="id" title="var">primary</span></span> <span class="inlinecode"><span class="id" title="var">_</span></span>" here is shorthand for "the constructor
<span class="inlinecode"><span class="id" title="var">primary</span></span> applied to any <span class="inlinecode"><span class="id" title="var">rgb</span></span> constructor except <span class="inlinecode"><span class="id" title="tactic">red</span></span>."
<div class="paragraph"> </div>
(The wildcard pattern <span class="inlinecode"><span class="id" title="var">_</span></span> has the same effect as the dummy
pattern variable <span class="inlinecode"><span class="id" title="var">p</span></span> in the definition of <span class="inlinecode"><span class="id" title="var">monochrome</span></span>.)
</div>
<div class="doc">
<a id="lab30"></a><h2 class="section">Modules</h2>
<div class="paragraph"> </div>
Coq provides a <i>module system</i> to aid in organizing large
developments. We won't need most of its features, but one is
useful here: If we enclose a collection of declarations between
<span class="inlinecode"><span class="id" title="keyword">Module</span></span> <span class="inlinecode"><span class="id" title="var">X</span></span> and <span class="inlinecode"><span class="id" title="keyword">End</span></span> <span class="inlinecode"><span class="id" title="var">X</span></span> markers, then, in the remainder of the file
after the <span class="inlinecode"><span class="id" title="keyword">End</span></span>, these definitions are referred to by names like
<span class="inlinecode"><span class="id" title="var">X.foo</span></span> instead of just <span class="inlinecode"><span class="id" title="var">foo</span></span>. We will use this feature to limit
the scope of definitions, so that we are free to reuse names.
</div>
<div class="code">
<span class="id" title="keyword">Module</span> <a id="Playground" class="idref" href="#Playground"><span class="id" title="module">Playground</span></a>.<br/>
<span class="id" title="keyword">Definition</span> <a id="Playground.foo" class="idref" href="#Playground.foo"><span class="id" title="definition">foo</span></a> : <a class="idref" href="Basics.html#rgb"><span class="id" title="inductive">rgb</span></a> := <a class="idref" href="Basics.html#blue"><span class="id" title="constructor">blue</span></a>.<br/>
<span class="id" title="keyword">End</span> <a class="idref" href="Basics.html#Playground"><span class="id" title="module">Playground</span></a>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Definition</span> <a id="foo" class="idref" href="#foo"><span class="id" title="definition">foo</span></a> : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> := <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Check</span> <a class="idref" href="Basics.html#foo"><span class="id" title="definition">Playground.foo</span></a> : <a class="idref" href="Basics.html#rgb"><span class="id" title="inductive">rgb</span></a>.<br/>
<span class="id" title="keyword">Check</span> <a class="idref" href="Basics.html#foo"><span class="id" title="definition">foo</span></a> : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>.<br/>
</div>
<div class="doc">
<a id="lab31"></a><h2 class="section">Tuples</h2>
</div>
<div class="code">
<span class="id" title="keyword">Module</span> <a id="TuplePlayground" class="idref" href="#TuplePlayground"><span class="id" title="module">TuplePlayground</span></a>.<br/>
</div>
<div class="doc">
A single constructor with multiple parameters can be used
to create a tuple type. As an example, consider representing
the four bits in a nybble (half a byte). We first define
a datatype <span class="inlinecode"><span class="id" title="var">bit</span></span> that resembles <span class="inlinecode"><span class="id" title="var">bool</span></span> (using the
constructors <span class="inlinecode"><span class="id" title="var">B<sub>0</sub></span></span> and <span class="inlinecode"><span class="id" title="var">B<sub>1</sub></span></span> for the two possible bit values)
and then define the datatype <span class="inlinecode"><span class="id" title="var">nybble</span></span>, which is essentially
a tuple of four bits.
</div>
<div class="code">
<span class="id" title="keyword">Inductive</span> <a id="TuplePlayground.bit" class="idref" href="#TuplePlayground.bit"><span class="id" title="inductive">bit</span></a> : <span class="id" title="keyword">Type</span> :=<br/>
| <a id="TuplePlayground.B1" class="idref" href="#TuplePlayground.B1"><span class="id" title="constructor">B<sub>1</sub></span></a><br/>
| <a id="TuplePlayground.B0" class="idref" href="#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Inductive</span> <a id="TuplePlayground.nybble" class="idref" href="#TuplePlayground.nybble"><span class="id" title="inductive">nybble</span></a> : <span class="id" title="keyword">Type</span> :=<br/>
| <a id="TuplePlayground.bits" class="idref" href="#TuplePlayground.bits"><span class="id" title="constructor">bits</span></a> (<a id="b<sub>0</sub>:38" class="idref" href="#b<sub>0</sub>:38"><span class="id" title="binder">b<sub>0</sub></span></a> <a id="b<sub>1</sub>:39" class="idref" href="#b<sub>1</sub>:39"><span class="id" title="binder">b<sub>1</sub></span></a> <a id="b<sub>2</sub>:40" class="idref" href="#b<sub>2</sub>:40"><span class="id" title="binder">b<sub>2</sub></span></a> <a id="b<sub>3</sub>:41" class="idref" href="#b<sub>3</sub>:41"><span class="id" title="binder">b<sub>3</sub></span></a> : <a class="idref" href="Basics.html#TuplePlayground.bit"><span class="id" title="inductive">bit</span></a>).<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Check</span> (<a class="idref" href="Basics.html#TuplePlayground.bits"><span class="id" title="constructor">bits</span></a> <a class="idref" href="Basics.html#TuplePlayground.B1"><span class="id" title="constructor">B<sub>1</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B1"><span class="id" title="constructor">B<sub>1</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a>)<br/>
: <a class="idref" href="Basics.html#TuplePlayground.nybble"><span class="id" title="inductive">nybble</span></a>.<br/>
</div>
<div class="doc">
The <span class="inlinecode"><span class="id" title="var">bits</span></span> constructor acts as a wrapper for its contents.
Unwrapping can be done by pattern-matching, as in the <span class="inlinecode"><span class="id" title="var">all_zero</span></span>
function below, which tests a nybble to see if all its bits are
<span class="inlinecode"><span class="id" title="var">B<sub>0</sub></span></span>.
<div class="paragraph"> </div>
We use underscore (_) as a <i>wildcard pattern</i> to avoid inventing
variable names that will not be used.
</div>
<div class="code">
<span class="id" title="keyword">Definition</span> <a id="TuplePlayground.all_zero" class="idref" href="#TuplePlayground.all_zero"><span class="id" title="definition">all_zero</span></a> (<a id="nb:42" class="idref" href="#nb:42"><span class="id" title="binder">nb</span></a> : <a class="idref" href="Basics.html#TuplePlayground.nybble"><span class="id" title="inductive">nybble</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#nb:42"><span class="id" title="variable">nb</span></a> <span class="id" title="keyword">with</span><br/>
| (<a class="idref" href="Basics.html#TuplePlayground.bits"><span class="id" title="constructor">bits</span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a>) ⇒ <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><br/>
| (<a class="idref" href="Basics.html#TuplePlayground.bits"><span class="id" title="constructor">bits</span></a> <span class="id" title="var">_</span> <span class="id" title="var">_</span> <span class="id" title="var">_</span> <span class="id" title="var">_</span>) ⇒ <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
<span class="id" title="keyword">end</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Compute</span> (<a class="idref" href="Basics.html#TuplePlayground.all_zero"><span class="id" title="definition">all_zero</span></a> (<a class="idref" href="Basics.html#TuplePlayground.bits"><span class="id" title="constructor">bits</span></a> <a class="idref" href="Basics.html#TuplePlayground.B1"><span class="id" title="constructor">B<sub>1</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B1"><span class="id" title="constructor">B<sub>1</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a>)).<br/>
<span class="comment">(* ===> false : bool *)</span><br/>
<span class="id" title="keyword">Compute</span> (<a class="idref" href="Basics.html#TuplePlayground.all_zero"><span class="id" title="definition">all_zero</span></a> (<a class="idref" href="Basics.html#TuplePlayground.bits"><span class="id" title="constructor">bits</span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a>)).<br/>
<span class="comment">(* ===> true : bool *)</span><br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">End</span> <a class="idref" href="Basics.html#TuplePlayground"><span class="id" title="module">TuplePlayground</span></a>.<br/>
</div>
<div class="doc">
<a id="lab32"></a><h2 class="section">Numbers</h2>
<div class="paragraph"> </div>
We put this section in a module so that our own definition of
natural numbers does not interfere with the one from the
standard library. In the rest of the book, we'll want to use
the standard library's.
</div>
<div class="code">
<span class="id" title="keyword">Module</span> <a id="NatPlayground" class="idref" href="#NatPlayground"><span class="id" title="module">NatPlayground</span></a>.<br/>
</div>
<div class="doc">
All the types we have defined so far -- both "enumerated
types" such as <span class="inlinecode"><span class="id" title="var">day</span></span>, <span class="inlinecode"><span class="id" title="var">bool</span></span>, and <span class="inlinecode"><span class="id" title="var">bit</span></span> and tuple types such as
<span class="inlinecode"><span class="id" title="var">nybble</span></span> built from them -- are finite. The natural numbers, on
the other hand, are an infinite set, so we'll need to use a
slightly richer form of type declaration to represent them.
<div class="paragraph"> </div>
There are many representations of numbers to choose from. You are
almost certainly most familiar with decimal notation (base 10),
using the digits 0 through 9, for example, to form the number 123.
You may very likely also have encountered hexadecimal
notation (base 16), in which the same number is represented as 7B,
or octal (base 8), where it is 173, or binary (base 2), where it
is 1111011. Using an enumerated type to represent digits, we could
use any of these as our representation natural numbers. Indeed,
there are circumstances where each of these choices would be
useful.
<div class="paragraph"> </div>
The binary representation is valuable in computer hardware because
the digits can be represented with just two distinct voltage
levels, resulting in simple circuitry. Analogously, we wish here
to choose a representation that makes <i>proofs</i> simpler.
<div class="paragraph"> </div>
In fact, there is a representation of numbers that is even simpler
than binary, namely unary (base 1), in which only a single digit
is used (as our forebears might have done to count days by making
scratches on the walls of their caves). To represent unary numbers
with a Coq datatype, we use two constructors. The capital-letter
<span class="inlinecode"><span class="id" title="var">O</span></span> constructor represents zero. The <span class="inlinecode"><span class="id" title="var">S</span></span> constructor can be
applied to the representation of the natural number n, yieldimng
the representation of n+1, where <span class="inlinecode"><span class="id" title="var">S</span></span> stands for "successor" (or
"scratch"). Here is the complete datatype definition:
</div>
<div class="code">
<span class="id" title="keyword">Inductive</span> <a id="NatPlayground.nat" class="idref" href="#NatPlayground.nat"><span class="id" title="inductive">nat</span></a> : <span class="id" title="keyword">Type</span> :=<br/>
| <a id="NatPlayground.O" class="idref" href="#NatPlayground.O"><span class="id" title="constructor">O</span></a><br/>
| <a id="NatPlayground.S" class="idref" href="#NatPlayground.S"><span class="id" title="constructor">S</span></a> (<a id="n:46" class="idref" href="#n:46"><span class="id" title="binder">n</span></a> : <a class="idref" href="Basics.html#nat:44"><span class="id" title="inductive">nat</span></a>).<br/>
</div>
<div class="doc">
With this definition, 0 is represented by <span class="inlinecode"><span class="id" title="var">O</span></span>, 1 by <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">O</span></span>,
2 by <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">O</span>)</span>, and so on.
<div class="paragraph"> </div>
Informally, the clauses of the definition can be read:
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" title="var">O</span></span> is a natural number (remember this is the letter "<span class="inlinecode"><span class="id" title="var">O</span></span>,"
not the numeral "<span class="inlinecode">0</span>").
</li>
<li> <span class="inlinecode"><span class="id" title="var">S</span></span> can be put in front of a natural number to yield another
one -- i.e., if <span class="inlinecode"><span class="id" title="var">n</span></span> is a natural number, then <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">n</span></span> is too.
</li>
</ul>
<div class="paragraph"> </div>
Again, let's look at this a bit more closely. The definition
of <span class="inlinecode"><span class="id" title="var">nat</span></span> says how expressions in the set <span class="inlinecode"><span class="id" title="var">nat</span></span> can be built:
<div class="paragraph"> </div>
<ul class="doclist">
<li> the constructor expression <span class="inlinecode"><span class="id" title="var">O</span></span> belongs to the set <span class="inlinecode"><span class="id" title="var">nat</span></span>;
</li>
<li> if <span class="inlinecode"><span class="id" title="var">n</span></span> is a constructor expression belonging to the set <span class="inlinecode"><span class="id" title="var">nat</span></span>,
then <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">n</span></span> is also a constructor expression belonging to the set
<span class="inlinecode"><span class="id" title="var">nat</span></span>; and
</li>
<li> constructor expressions formed in these two ways are the only
ones belonging to the set <span class="inlinecode"><span class="id" title="var">nat</span></span>.
</li>
</ul>
<div class="paragraph"> </div>
These conditions are the precise force of the <span class="inlinecode"><span class="id" title="keyword">Inductive</span></span>
declaration that we gave to Coq. They imply that the constructor
expression <span class="inlinecode"><span class="id" title="var">O</span></span>, the constructor expression <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">O</span></span>, the constructor
expression <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">O</span>)</span>, the constructor expression <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">O</span>))</span>,
and so on all belong to the set <span class="inlinecode"><span class="id" title="var">nat</span></span>, while other constructor
expressions like <span class="inlinecode"><span class="id" title="var">true</span></span>, <span class="inlinecode"><span class="id" title="var">andb</span></span> <span class="inlinecode"><span class="id" title="var">true</span></span> <span class="inlinecode"><span class="id" title="var">false</span></span>, <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">false</span>)</span>, and
<span class="inlinecode"><span class="id" title="var">O</span></span> <span class="inlinecode">(<span class="id" title="var">O</span></span> <span class="inlinecode">(<span class="id" title="var">O</span></span> <span class="inlinecode"><span class="id" title="var">S</span>))</span> do not.
<div class="paragraph"> </div>
A critical point here is that what we've done so far is just to
define a <i>representation</i> of numbers: a way of writing them down.
The names <span class="inlinecode"><span class="id" title="var">O</span></span> and <span class="inlinecode"><span class="id" title="var">S</span></span> are arbitrary, and at this point they have
no special meaning -- they are just two different marks that we
can use to write down numbers, together with a rule that says any
<span class="inlinecode"><span class="id" title="var">nat</span></span> will be written as some string of <span class="inlinecode"><span class="id" title="var">S</span></span> marks followed by an
<span class="inlinecode"><span class="id" title="var">O</span></span>. If we like, we can write essentially the same definition
this way:
</div>
<div class="code">
<span class="id" title="keyword">Inductive</span> <a id="NatPlayground.otherNat" class="idref" href="#NatPlayground.otherNat"><span class="id" title="inductive">otherNat</span></a> : <span class="id" title="keyword">Type</span> :=<br/>
| <a id="NatPlayground.stop" class="idref" href="#NatPlayground.stop"><span class="id" title="constructor">stop</span></a><br/>
| <a id="NatPlayground.tick" class="idref" href="#NatPlayground.tick"><span class="id" title="constructor">tick</span></a> (<a id="foo:49" class="idref" href="#foo:49"><span class="id" title="binder">foo</span></a> : <a class="idref" href="Basics.html#otherNat:47"><span class="id" title="inductive">otherNat</span></a>).<br/>
</div>
<div class="doc">
The <i>interpretation</i> of these marks arises from how we use them to
compute.
<div class="paragraph"> </div>
We can do this by writing functions that pattern match on
representations of natural numbers just as we did above with
booleans and days -- for example, here is the predecessor
function:
</div>
<div class="code">
<span class="id" title="keyword">Definition</span> <a id="NatPlayground.pred" class="idref" href="#NatPlayground.pred"><span class="id" title="definition">pred</span></a> (<a id="n:50" class="idref" href="#n:50"><span class="id" title="binder">n</span></a> : <a class="idref" href="Basics.html#NatPlayground.nat"><span class="id" title="inductive">nat</span></a>) : <a class="idref" href="Basics.html#NatPlayground.nat"><span class="id" title="inductive">nat</span></a> :=<br/>
<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#n:50"><span class="id" title="variable">n</span></a> <span class="id" title="keyword">with</span><br/>
| <a class="idref" href="Basics.html#NatPlayground.O"><span class="id" title="constructor">O</span></a> ⇒ <a class="idref" href="Basics.html#NatPlayground.O"><span class="id" title="constructor">O</span></a><br/>
| <a class="idref" href="Basics.html#NatPlayground.S"><span class="id" title="constructor">S</span></a> <span class="id" title="var">n'</span> ⇒ <span class="id" title="var">n'</span><br/>
<span class="id" title="keyword">end</span>.<br/>
</div>
<div class="doc">
The second branch can be read: "if <span class="inlinecode"><span class="id" title="var">n</span></span> has the form <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">n'</span></span>
for some <span class="inlinecode"><span class="id" title="var">n'</span></span>, then return <span class="inlinecode"><span class="id" title="var">n'</span></span>."
<div class="paragraph"> </div>
The following <span class="inlinecode"><span class="id" title="keyword">End</span></span> command closes the current module, so
<span class="inlinecode"><span class="id" title="var">nat</span></span> will refer back to the type from the standard library.
</div>
<div class="code">
<span class="id" title="keyword">End</span> <a class="idref" href="Basics.html#NatPlayground"><span class="id" title="module">NatPlayground</span></a>.<br/>
</div>
<div class="doc">
Because natural numbers are such a pervasive kind of data,
Coq does provide a tiny bit of built-in magic for parsing and
printing them: ordinary decimal numerals can be used as an
alternative to the "unary" notation defined by the constructors
<span class="inlinecode"><span class="id" title="var">S</span></span> and <span class="inlinecode"><span class="id" title="var">O</span></span>. Coq prints numbers in decimal form by default:
</div>
<div class="code">
<span class="id" title="keyword">Check</span> (<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> (<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> (<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> (<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a>)))).<br/>
<span class="comment">(* ===> 4 : nat *)</span><br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Definition</span> <a id="minustwo" class="idref" href="#minustwo"><span class="id" title="definition">minustwo</span></a> (<a id="n:52" class="idref" href="#n:52"><span class="id" title="binder">n</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>) : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a> :=<br/>
<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#n:52"><span class="id" title="variable">n</span></a> <span class="id" title="keyword">with</span><br/>
| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a> ⇒ <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a><br/>
| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a> ⇒ <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a><br/>
| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> (<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <span class="id" title="var">n'</span>) ⇒ <span class="id" title="var">n'</span><br/>
<span class="id" title="keyword">end</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Compute</span> (<a class="idref" href="Basics.html#minustwo"><span class="id" title="definition">minustwo</span></a> 4).<br/>
<span class="comment">(* ===> 2 : nat *)</span><br/>
</div>
<div class="doc">
The constructor <span class="inlinecode"><span class="id" title="var">S</span></span> has the type <span class="inlinecode"><span class="id" title="var">nat</span></span> <span class="inlinecode">→</span> <span class="inlinecode"><span class="id" title="var">nat</span></span>, just like functions
such as <span class="inlinecode"><span class="id" title="var">pred</span></span> and <span class="inlinecode"><span class="id" title="var">minustwo</span></span>:
</div>
<div class="code">
<span class="id" title="keyword">Check</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">→</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>.<br/>
<span class="id" title="keyword">Check</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#pred"><span class="id" title="abbreviation">pred</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">→</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>.<br/>
<span class="id" title="keyword">Check</span> <a class="idref" href="Basics.html#minustwo"><span class="id" title="definition">minustwo</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">→</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>.<br/>
</div>
<div class="doc">
These are all things that can be applied to a number to yield a
number. However, there is a fundamental difference between <span class="inlinecode"><span class="id" title="var">S</span></span>
and the other two: functions like <span class="inlinecode"><span class="id" title="var">pred</span></span> and <span class="inlinecode"><span class="id" title="var">minustwo</span></span> are
defined by giving <i>computation rules</i> -- e.g., the definition of
<span class="inlinecode"><span class="id" title="var">pred</span></span> says that <span class="inlinecode"><span class="id" title="var">pred</span></span> <span class="inlinecode">2</span> can be simplified to <span class="inlinecode">1</span> -- while the
definition of <span class="inlinecode"><span class="id" title="var">S</span></span> has no such behavior attached. Although it is
<i>like</i> a function in the sense that it can be applied to an
argument, it does not <i>do</i> anything at all! It is just a way of
writing down numbers.
<div class="paragraph"> </div>
Think about standard decimal numerals: the numeral <span class="inlinecode">1</span> is not a
computation; it's a piece of data. When we write <span class="inlinecode">111</span> to mean
the number one hundred and eleven, we are using <span class="inlinecode">1</span>, three times,
to write down a concrete representation of a number.
<div class="paragraph"> </div>
Let's go on and define some more functions over numbers.
<div class="paragraph"> </div>
For most interesting computations involving numbers, simple
pattern matching is not enough: we also need recursion. For
example, to check that a number <span class="inlinecode"><span class="id" title="var">n</span></span> is even, we may need to
recursively check whether <span class="inlinecode"><span class="id" title="var">n</span>-2</span> is even. Such functions are
introduced with the keyword <span class="inlinecode"><span class="id" title="keyword">Fixpoint</span></span> instead of <span class="inlinecode"><span class="id" title="keyword">Definition</span></span>.
</div>
<div class="code">
<span class="id" title="keyword">Fixpoint</span> <a id="even" class="idref" href="#even"><span class="id" title="definition">even</span></a> (<a id="n:54" class="idref" href="#n:54"><span class="id" title="binder">n</span></a>:<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#n:54"><span class="id" title="variable">n</span></a> <span class="id" title="keyword">with</span><br/>
| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a> ⇒ <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><br/>
| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a> ⇒ <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> (<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <span class="id" title="var">n'</span>) ⇒ <a class="idref" href="Basics.html#even:55"><span class="id" title="definition">even</span></a> <span class="id" title="var">n'</span><br/>
<span class="id" title="keyword">end</span>.<br/>
</div>
<div class="doc">
We could define <span class="inlinecode"><span class="id" title="var">odd</span></span> by a similar <span class="inlinecode"><span class="id" title="keyword">Fixpoint</span></span> declaration, but
here is a simpler way:
</div>
<div class="code">
<span class="id" title="keyword">Definition</span> <a id="odd" class="idref" href="#odd"><span class="id" title="definition">odd</span></a> (<a id="n:57" class="idref" href="#n:57"><span class="id" title="binder">n</span></a>:<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
<a class="idref" href="Basics.html#negb"><span class="id" title="definition">negb</span></a> (<a class="idref" href="Basics.html#even"><span class="id" title="definition">even</span></a> <a class="idref" href="Basics.html#n:57"><span class="id" title="variable">n</span></a>).<br/><hr class='doublespaceincode'/>