-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathch04.tex
More file actions
executable file
·1612 lines (1487 loc) · 79.3 KB
/
Copy pathch04.tex
File metadata and controls
executable file
·1612 lines (1487 loc) · 79.3 KB
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
\chapter{Mathematical Induction}
\label{ch:mathematical-induction}
\section{Lists as Mathematical Objects}
\label{sec:lists-as-obj}
A \index{sequence}\index{list}sequence
is an ordered list of elements.
In fact, for our purposes, the terms ``list'' and ``sequence'' are synonyms.
Many things that computers do come down to keeping track of lists,
so lists are an important class of mathematical objects.
We will need a formal notation, including an algebra of formulas,
to discuss lists with the level of mathematical precision
required in specifications of computer hardware and software.
Informally, we will write lists as sequences of elements separated by spaces,
with square brackets marking the beginning and end of the list.
For example, \textsf{[8 3 7]} denotes a list with first element 8,
second element 3, and third element 7, and
\textsf{[9 8 3 7]} denotes a list with the same elements
plus an additional element 9 at the beginning.
\label{nil-def}\index{nil}\index{empty list (nil)}We
use the symbol \textsf{nil} for the empty list
(that is, the list with no elements).
\label{square-brackets}We use
\index{square brackets}\index{brackets!square}\index{list!square bracket notation}square brackets
rather than round ones in formulas
specifying lists, to avoid confusion with formulas that
invoke operators.\footnote{To \index{invoke (invocation)}\emph{invoke}
an operator is to apply it to its operands
to make a computation.
An \emph{invocation} is a formula that invokes an operator.}
For example, \textsf{[4 7 9]} denotes a three-element \index{list}list,
whereas \textsf{(+ 7 9)} is a numeric formula representing the value 16.
However, ACL2 does not employ this square-bracket notation.
When it displays the list \textsf{[4 7 9]},
it uses \index{parentheses}\index{brackets!round}round brackets: \textsf{(4 7 9)}.
The square-bracket notation helps keep data in computational formulas
straight in written discussion and also makes some
formulas more compact in writing, but square-bracket
notation for lists is not ACL2 notation.
One of the basic operators in the algebra of lists is the
\index{list!cons (\emph{see also} operator)}construction operator
\index{operator, by name!cons (insert at front)}\textsf{cons},
which inserts a new element at the beginning of a list.
Formulas using \textsf{cons}, like all formulas in
the mathematical notation we have been using to discuss software concepts,
are written in prefix form.
So, the formula \textsf{(cons $x$ $xs$)} denotes a list
with the same elements as the list $xs$
plus an additional element $x$ inserted at the beginning.
If $x$ stands for the number \textsf{9}
and $xs$ stands for the list \textsf{[8 3 7]},
then \textsf{(cons $x$ $xs$)} constructs the list \textsf{[9 8 3 7]}.
\begin{aside}{square-bracket-notation}{Square Bracket Notation for Lists: Paper-and-Pencil Only}
Most of the time, we will use square bracket
\index{square brackets}\index{brackets!square}\index{list!square bracket notation}notation for lists
to distinguish them from computational formulas.
However, ACL2 does not display lists with square brackets.
It uses round parentheses both for lists and for computational formulas.
\begin{center}
\begin{tabular}{cc}
\emph{paper-and-pencil only} & \emph{formal ACL2 notation}\\
\textsf{[1 2 1 5]} & \textsf{(list 1 2 1 5)}\\
\end{tabular}
\end{center}
%\caption{Square Bracket Notation for Lists: Paper-and-Pencil Only}
%\label{square-bracket-notation}
\end{aside}
\begin{figure}
\begin{center}
\textsf{[$x_1$ $x_2$ \dots $x_n$]} $=$
\textsf{(list $x_1$ $x_2$ $\dots$ $x_n$)} $=$ \textsf{(cons $x_1$ (cons $x_2$ $\dots$ (cons $x_n$ nil) $\dots$))}\\
\addtolength{\tabcolsep}{-4pt}
\begin{tabular}{lclcl}
\hline \\[-1.0em]
\textsf{{[1 2]}} &$=$ &\textsf{(list 1 2)} &$=$ &\textsf{(cons 1 (cons 2 nil))}\\
\textsf{{[16 256 4096]}} &$=$ &\textsf{(list 16 256 4096})&$=$ &\textsf{(cons 16 (cons 256 (cons 4096 nil)))}\\
\textsf{{[1 9 4 7]}} &$=$ &\textsf{(list 1 9 4 7)} &$=$ &\textsf{(cons 1 (cons 9 (cons 4 (cons 7 nil))))}\\
\end{tabular}
\addtolength{\tabcolsep}{4pt}
\end{center}
\caption{Shorthand for nested \textsf{cons}: \textsf{list}.}
\label{fig:list-nested-cons}
\end{figure}
Any list can be constructed by starting from an \index{empty list (nil)}empty list
and using the construction operator to insert the elements of the list, one by one.
The empty list, \index{nil}\textsf{nil}, which is intrinsic in ACL2, needs no construction.
Nonempty \index{list!nonempty}lists are constructed using the \textsf{cons} operator.
The formula \textsf{[8 3 7]} is paper-and-pencil shorthand
for \textsf{(cons 8 (cons 3 (cons 7 nil)))}.
ACL2 also has shorthand
for nested \textsf{cons} operations
(figure \ref{fig:list-nested-cons}): %, page \pageref{fig:list-nested-cons}):
\textsf{(list 8 3 7)} is another way to write the formula
\textsf{(cons 8 (cons 3 (cons 7 nil)))}.
\begin{aside}{three-line-equal}{Equal by Definition: $\equiv$}
The
\index{equation!defining ($\equiv$)}\index{definition!equation ($\equiv$)}\index{equivalence!by definition ($\equiv$)}\index{three-line equal ($\equiv$)}\index{equal, three-line ($\equiv$)}three-line
variation of the equals sign
indicates that the term on the left stands
for the formula on the right \emph{by definition}.
\begin{center}
\addtolength{\tabcolsep}{-2pt}
\begin{tabular}{ll}
$term$ $\equiv$ $\dots$ \emph{some formula} $\dots$ &\emph{definition of term} \\
$P(xs, y, ys)$ $\equiv$ $(xs$ $=$ \textsf{(cons $y$ $ys$)}$)$ &$P(xs, y, ys)$ \emph{means} $(xs$ $=$ \textsf{(cons $y$ $ys$)}$)$ \\
\end{tabular}
\addtolength{\tabcolsep}{2pt}
\end{center}
%\caption{Equal by Definition: $\equiv$}
%\label{three-line-equal}
%%note: this aside mostly repeats an aside in ch02 predicates section, on purpose in case they skip that section
\end{aside}
Suppose we take $P(xs, y, ys)$ as shorthand
for the equation $xs$ $=$ \textsf{(cons $y$ $ys$)}.
\begin{center}
$P(xs, y, ys)$ $\equiv$ $(xs$ $=$ \textsf{(cons $y$ $ys$)}$)$
\end{center}
Given a particular list $xs$, together with a value $y$,
we can view the equation $P(xs, y, ys)$ as a set of propositions
indexed by the variable $ys$ whose universe of discourse is the set of
lists that can be constructed by ACL2.
In this set of propositions, the one corresponding to
the list $ys$ is the equation that $P(xs, y, ys)$ stands for:
$(xs$ $=$ \textsf{(cons $y$ $ys$)}$)$.
If that equation holds, the value of the proposition $P(xs, y, ys)$ is true.
Otherwise, it's false.
For example, if $xs$ denotes the list \textsf{[1 2 3]}
and $y$ denotes the natural number \textsf{1},
then $P(xs, y, ys)$ is $P($\textsf{[1 2 3]}, \textsf{1}, $ys)$,
which stands for an equation involving the variable $ys$.
There is one such equation for each different list $ys$.
Taken all together, those equations comprise a predicate
whose universe of discourse is ACL2 lists.
The operator
\index{predicate, by name!consp (nonempty list)}\seeonlyindex{consp}{predicate}\textsf{consp}
checks for nonempty lists.
That is, the formula \textsf{(consp $xs$)} delivers true
if $xs$ is a nonempty list and false otherwise.
The \{\emph{consp}\} axiom
(figure~\ref{consp-axiom}) %, page \pageref{consp-axiom})
formally asserts that all nonempty lists
are constructed with the \textsf{cons} operator.
The formula
$(\exists ys.P($\textsf{[1 2 3]}, \textsf{1}, $ys))$ is true
because \textsf{[1 2 3]} $=$ \textsf{(cons 1 [2 3])},
so there is a value of $ys$, namely, $ys$ $=$ \textsf{[2 3]},
for which $P($\textsf{[1 2 3]}, \textsf{1}, $ys)$ is true.
If there were no list that made the equation valid,
the formula $(\exists ys.P($[1 2 3], \textsf{1}, $ys))$
would be false.
If, on the other hand, $xs$ were the list \textsf{[1 2 3]}
and $y$ were the number 2, there would be no list
$ys$ that would make the equation \textsf{[1 2 3]} $=$ \textsf{(cons $2$ $ys$)} valid
because the list on the left-hand side of the equation
starts with 1 and the list on the right-hand side starts with 2.
So, the formula $(\exists ys.P($\textsf{[1 2 3]}, \textsf{2}, $ys))$
is false.
\begin{figure}
%\begin{center}
\begin{tabular}{c}
Axiom \{\emph{consp}\} \\
\hline
\textsf{(consp $xs$)} $=$ $(\exists y.(\exists ys.(xs$ $=$ \textsf{(cons $y$ $ys$)}$)))$
\end{tabular}
%\end{center}
\index{axiom, by name!\{consp\}}\index{operator, by name!consp (\emph{see} predicate)}\seeonlyindex{consp}{predicate}\index{predicate, by name!consp}
\caption{Nonempty list predicate: \textsf{consp}.}
\label{consp-axiom}
\end{figure}
Now, let's take a step back.
We can view the formula
($\exists ys.$ ($xs$ $=$ \textsf{(cons $y$ $ys$)}))
as a set of propositions,
one for each object $y$ that ACL2 can represent.
The formula
$(\exists ys.P(xs, y, ys))$ is one way to represent that
set of propositions.
Since any set of propositions is a predicate,
we can view $(\exists ys.P(xs, y, ys))$ as a predicate indexed
by the set of ACL2 objects $y$.
\begin{figure}
%\begin{center}
\begin{tabular}{c}
Axiom \{\emph{nlst}\}\\
\hline
\textsf{[$x_{m}$ $x_{m+1}$ \dots $x_{n}$]} \emph{denotes a list with $n - m + 1$ elements} \{\emph{nlst}\} \\
\emph{Note: Denotes} \textsf{nil}\emph{, the empty list, if $m > n$}\\
\end{tabular}
%\end{center}
\index{numbered list notation}\index{list!numbered}\index{list!ellipsis}\index{axiom, by name!\{nlst\}}
\caption{Numbered list notation.}
\label{numbered-list-interpretation}
\end{figure}
We can convert the predicate $(\exists ys.P(xs, y, ys))$
into a true/false value (that is, convert it to a proposition)
by applying the $\exists$ quantifier again,
but this time with $y$ as the bound variable:
$(\exists y.(\exists ys.P(xs, y, ys)))$.
When $xs$ is a list for which this formula has the value true,
then \textsf{(consp $xs$)} is true.
That is, \textsf{consp} is the ACL2 predicate that means $(\exists y.(\exists ys.P(xs, y, ys)))$.
The universe of discourse of the predicate \textsf{consp}
is the set of all objects that ACL2 can represent.
That specification of \textsf{consp} is expressed in the \{\emph{consp}\} axiom
(figure~\ref{consp-axiom}, page \pageref{consp-axiom}).
So, \textsf{(consp $xs$)} is a way to write the formula
$(\exists y.(\exists ys.(xs$ $=$ \textsf{(cons $y$ $ys$)}$)))$ in ACL2.
When we know that a list $ys$ is nonempty,
we can cite the \{\emph{consp}\} axiom
to rewrite $ys$ in the form \textsf{(cons $x$ $xs$)}.
When we do this, we choose the symbols $x$ and $xs$ carefully
to avoid conflicts with other symbols that appear in the context of the discussion.
The \{\emph{consp}\} axiom refers to \textsf{cons},
so we will need a \{\emph{cons}\} axiom.
The \{\emph{cons}\} axiom uses numbered list notation
(figure~\ref{numbered-list-interpretation}, page \pageref{numbered-list-interpretation})
to specify that \textsf{cons} delivers a nonempty list,
\textsf{[$x_1$ $x_2$ \dots $x_{n+1}$]},
where $n$ stands for a natural number.
Because that list has $n+1$ elements and $n+1$
is at least one when $n$ is a natural number,
the list cannot be empty.
Therefore, the list can be constructed by \textsf{cons}.
The construction operator, \textsf{cons}, cannot be the whole story, of course.
To compute with lists, we need to be able to construct them,
but we also need to be able to take them apart.
There are two basic operators for taking lists apart: \textsf{first} and \textsf{rest}.
We express the relationship between these operators and
the construction operator in the form of equations,
\{\emph{fst}\} and \{\emph{rst}\}, that we take as axioms
(figure~\ref{first-rest-cons}). %, page \pageref{first-rest-cons}).
\begin{figure}
%\begin{center}
\begin{tabular}{ll}
\multicolumn{2}{c}{Axioms \{\emph{cons}\}, \{\emph{first}\}, and \{\emph{rest}\}} \\
\textsf{[$x_1$ $x_2$ \dots $x_{n+1}$]} $=$ \textsf{(cons $x_1$ [$x_2$ \dots $x_{n+1}$])} & \{\emph{cons}\} \\
\hline
\textsf{(first (cons $x$ $xs$))} $=$ $x$ & \{\emph{fst}\}\\
\textsf{(rest (cons $x$ $xs$))} $=$ $xs$ & \{\emph{rst}\} \\
\textsf{(first nil)} $=$ \textsf{nil} & \{\emph{fst0}\}\\
\textsf{(rest nil)} $=$ \textsf{nil} & \{\emph{rst0}\}\\
\end{tabular}
%\end{center}
\index{operator, by name!first (extract first element)}\index{operator, by name!rest (drop first element)}\index{axiom, by name!\{fst\}, \{fst0\}}\index{operator, by name!cons (insert at front)}\seeonlyindex{first}{operator}\seeonlyindex{rest}{operator}\seeonlyindex{cons}{operator}\index{list!cons (\emph{see also} operator)}\index{list!first (\emph{see also} operator)}\index{list!rest (\emph{see also} operator)}\index{axiom, by name!\{rst\}, \{rst0\}}\index{axiom, by name!\{cons\}}\index{equation, by name!\{fst\}, \{fst0\}}\index{equation, by name!\{rst\}, \{rst0\}}\index{equation, by name!\{snd\}}\index{equation, by name!\{cons\}}
\caption{List constructor and deconstructors: \textsf{cons}, \textsf{first}, \textsf{rest}.}
\label{first-rest-cons}
\end{figure}
The \{\emph{fst}\} axiom states formally that
the operator \textsf{first} delivers the first element from a nonempty list.
The \{\emph{rst}\} axiom states that the operator \textsf{rest} delivers
a list like its operand but without the first element.
Note that the lists to which the operators \textsf{first} and \textsf{rest}
are applied in the axioms have at least one element
because those lists are constructed by the \textsf{cons} operator.
The axioms
\{\emph{fst0}\} and \{\emph{rst0}\}
provide an interpretation of the formulas
\textsf{(first nil)} and \textsf{(rest nil)}
when the operand is a list with no elements.
We will use equations like the ones in these axioms in the
same way we used the logic equations in figure~\ref{fig-02-02}
(page \pageref{fig-02-02}) and the arithmetic equations of
figure~\ref{fig-02-01} (page \pageref{fig-02-01}).
That is, whenever we see a formula like \textsf{(first (cons $x$ $xs$))},
no matter what formulas $x$ and $xs$ stand for,
we will be able to cite equation \{\emph{fst}\} to replace
\textsf{(first (cons $x$ $xs$))} by the simpler formula $x$.
Equations go both ways, so we can also cite equation \{\emph{fst}\}
to replace any formula $x$ by the more complicated formula
\textsf{(first (cons $x$ $xs$))}, where $xs$ stands for any formula
we care to make up, as long as it is grammatically correct.
Similarly, we can cite the equation \{\emph{rst}\} to justify
replacing the formula \textsf{(rest (cons $x$ $xs$))} by $xs$
and vice versa, regardless of what formulas the symbols $x$ and $xs$ stand for.
In other words, these are ordinary algebraic equations.
The only new factors are
(1)~the kind of mathematical object they denote and
(2)~the syntactic quirk of prefix notation, instead of the more familiar infix notation.
All \index{property!of lists}\index{list!properties}properties of lists,
as mathematical objects,
derive from the \{\emph{cons}\}, \{\emph{fst}\}, and \{\emph{rst}\} axioms.
The operator \textsf{len} (page \pageref{len-op-informal})
delivers the number of elements in a list.
We can use \textsf{check-expect} to test \textsf{len} in some specific cases.
\begin{code}
\begin{verbatim}
(check-expect (len (cons 8 (cons 3 (cons 7 nil)))) 3)
(check-expect (len nil) 0)
\end{verbatim}
\end{code}
We can use the DoubleCheck facility for more general tests.
For example, we expect that the number of elements
in a list constructed by the \textsf{cons} operation is
one more than the number of elements in its second operand.
The following property tests this expectation:
\index{definition!property}\index{data, random test}\index{random data}\index{defproperty}
\begin{code}
\begin{verbatim}
(defproperty len-cons-test
(x :value (random-natural)
xs :value (random-list-of (random-natural)))
(= (len (cons x xs))
(+ 1 (len xs))))
\end{verbatim}
\end{code}
By the same token, we expect that a list always
has one more element than it would have if
its first element were removed: \textsf{(len $xs$)} $=$ $1 +$ \textsf{(len (rest $xs$))}.
However, that is true only if the list $xs$
has some elements. It would not be true if $xs$ were \textsf{nil}.
What we want to test is an implication:
\textsf{(consp $xs$)} $\rightarrow$ (\textsf{(len $xs$)} $=$ $1$ $+$ \textsf{(len (rest $xs$))}$)$.
The ACL2 name for the implication operator is \textsf{implies},
and we can use that operator to specify a test that
constrains $xs$ to nonempty lists and thereby
makes the length equation in the test true.
\index{definition!property}\index{data, random test}\index{random data}\index{defproperty}
\begin{code}
\begin{verbatim}
(defproperty len-rest-test
(xs :value (random-list-of (random-natural)))
(implies (consp xs)
(= (len xs)
(+ 1 (len (rest xs))))))
\end{verbatim}
\end{code}
The equation in the len-rest test can serve
as an axiom for the \textsf{len} operator in the case
when its operand is a nonempty list.
The axiom for the empty case is simpler.
Figure~\ref{fig:len-axioms} (page \pageref{fig:len-axioms})
states these two axioms for
the len operator. Axiom \{\emph{len1}\} applies when
the operand is nonempty and the other axiom
applies in all other circumstances.\footnote{Normally
the operand of len will be either a nonempty list or nil, the empty list.
That is a good way to think of it for now,
but the operand may not be a list at all,
and according to axiom \{\emph{len0}\}, its value is zero in that case.
So, \textsf{(len nil)} $=$ $0$ but \textsf{(len $3$)} $=$ $0$ too since
$3$ is not a list. Later, we will say more about this kind of axiom.}
\begin{figure}
%\begin{center}
\begin{tabular}{ll}
\multicolumn{2}{c}{Axioms \{\emph{len}\}} \\
\hline
\textsf{(len (cons $x$ $xs$))} $=$ \textsf{(+ 1 (len $xs$))} & \{\emph{len1}\} \\
\textsf{(len $e$)} $=$ 0 & \{\emph{len0}\} \\
\multicolumn{2}{c}{\emph{Note: Cite }\{\emph{len0}\}\emph{ only if }\{\emph{len1}\}\emph{ doesn't match.}}\\
\end{tabular}
%\end{center}
\caption{Length of list: \textsf{len}.}
\label{len-equations}\label{fig:len-axioms}
\end{figure}
We expect the \textsf{len} operator to deliver a natural number,
regardless of the value of its operand.
For the record, we state this property as a theorem.
Later, you will have a chance to derive
this theorem from the \{\emph{len}\} axioms.
The theorem refers to the \textsf{natp} operator
(page \pageref{natp-op}),
which delivers true if its operand is a natural number and false otherwise.
\begin{samepage}
\label{len-nat-thm}\index{theorem, by name!\{len-nat\}}
\begin{center}
Theorem \{\emph{len-nat}\}: $\forall xs.$\textsf{(natp (len $xs$))}
\end{center}
\end{samepage}
A related fact is that the length of a nonempty list is strictly positive.
One way to state that fact is to observe that the formula \textsf{(consp $xs$)} is true
if \textsf{($>$ (len $xs$) 0)} and vice-versa. %(\verb+>+ (len $xs$) 0). %\textit{using math mode instead of \verb}
%In the notation from Chapter~\ref{ch:Boolean-Formulas}: %\textit{never covered the equiv op}
%(consp $xs$)$\leftrightarrow$(\verb+>+ (len $xs$) 0).
This theorem too can be derived from the axioms for
\textsf{len}, \textsf{consp}, and \textsf{cons}.
For the moment, we state the theorem without proof.
\begin{samepage}
\label{consp-len-thm}
\index{theorem, by name!\{consp $= (len > 0)$\}}
\begin{center}
Theorem \{\emph{consp} $= (len > 0)$\}: $\forall xs.($\textsf{(consp $xs$)} $=$ \textsf{($>$ (len $xs$) 0)}$)$
\end{center}
\end{samepage}
\begin{aside}{acl2-single-quote}{Suppressing Computation with Single-Quote}
To specify the list \textsf{[1 2 3 4]} in an ACL2 formula
rather than in a paper-and-pencil formula,
we can, of course, use the \textsf{cons} operator to construct it:
\textsf{(cons 1 (cons 2 (cons 3 (cons 4 nil))))}.
Or, we can use the \textsf{list} operator (page \pageref{list-op-informal}) to write it more compactly:
\textsf{(list 1 2 3 4)}.
However, the single-quote trick provides a less bulky ACL2 formula for lists
whose elements are numbers (or literals denoting other ACL2 constants).
The formula
\textsf{'(1 2 3 4)} has the same meaning as \textsf{(list 1 2 3 4)}.
Normally, ACL2 interprets the first symbol after a left-parenthesis
as the name of an operator.
However, the single-quote mark suppresses that interpretation and
delivers a list made up of the elements in the parentheses.
Without the single-quote mark,
the formula would make no sense because 1 is not the name of an
operator.\index{single-quote mark}\index{quote mark, single}\index{square brackets}\index{brackets!square}\index{list!square bracket notation}
%\caption{Suppressing Computation with Single-quote}
%\label{acl2-single-quote}
\end{aside}
\begin{exercises}
\exer {\label{rst1}%
\index{theorem, by name!\{rst1\}}Prove theorem \{\emph{rst1}\}: \textsf{(rest (list $x$))} $=$ \textsf{nil}.\\
\emph{Hint}: Cite some equations from figure~\ref{first-rest-cons} (page \pageref{first-rest-cons})
and figure~\ref{fig:list-nested-cons} (page \pageref{fig:list-nested-cons}).}
\end{exercises}
\section{Mathematical Induction}
\label{sec:induction}
The \textsf{cons}, \textsf{first}, and \textsf{rest} operators
form the basis for computing with lists,
but there are lots of other operators for lists.
The operator \textsf{append}, previously discussed in terms of some \textsf{check-expect} tests
(page \pageref{append-op-informal}), concatenates two lists, as illustrated
in the following \textsf{check-expect} tests,
which use the single-quote notation (box~\ref{acl2-single-quote}, page \pageref{acl2-single-quote})
to make them more compact:
\begin{code}
\begin{verbatim}
(check-expect (append '(1 2 3 4) '(5 6 7)) '(1 2 3 4 5 6 7))
(check-expect (append '(1 2 3 4 5) nil) '(1 2 3 4 5))
\end{verbatim}
\end{code}
%'
The numbered-list notation
(figure~\ref{numbered-list-interpretation}, page \pageref{numbered-list-interpretation})
provides a way to define the \textsf{append} operator informally.
In this form, the definition implicitly reveals the number of elements in the lists
involved in the concatenation.
\label{list-schematic} In the following list schematics,
the $x$ list has $m$ elements, the $y$ list has $n$ elements,
and the concatenated list has $m+n$ elements:
\begin{samepage}
\begin{center}
\textsf{(append [$x_1$ $x_2$ $\dots$ $x_m$] [$y_1$ $y_2$ $\dots$ $y_n$])} $=$
\textsf{[$x_1$ $x_2$ $\dots$ $x_m$ $y_1$ $y_2$ $\dots$ $y_n$]}
\end{center}
\end{samepage}
Let's analyze the concatenation \textsf{(append $xs$ $ys$)}. %'
If $xs$ is the empty list, then we expect
the concatenation to deliver the list $ys$.
This is the \{\emph{app0}\} case: \textsf{(append nil $ys$)} $=$ $ys$.
If $xs$ is not empty, then we expect the concatenation
to start with the first element of $xs$,
that is, \textsf{(first $xs$)}, and to continue
with the remaining elements of $xs$, that is, \textsf{(rest $xs$)},
and then the elements of $ys$ come after that.
Put another way, when $xs$ is not empty,
the result is the concatenation of \textsf{(rest $xs$)} and $ys$,
with \textsf{(first $xs$)} inserted at the front.
This is the \{\emph{app1}\} case: \textsf{(append $xs$ $ys$)} $=$
\textsf{(cons (first $xs$) (append (rest $xs$) $ys$))}.
We would like to express our expectations formally.
To do so, we use a special ACL2 operator
called \textsf{if} (figure~\ref{fig:if-axioms}), which has three operands.
It delivers its second operand if
its first operand is true (that is, not \textsf{nil})
and selects its third operand
if its first operand is false (that is, \textsf{nil}).
So, the \textsf{if} operator provides a way to select between
the two formulas supplied as its second and third operands.
\begin{figure}
%\begin{center}
\begin{tabular}{ll}
\multicolumn{2}{c}{Axioms \{\emph{if}\}} \\
\hline
\textsf{(if $p$ $x$ $y$}) $=$ $x$, \emph{if} $p$ $\neq$ \textsf{nil} & \{\emph{if-true}\} \\
\textsf{(if $p$ $x$ $y$)} $=$ $y$, \emph{if} $p$ $=$ \textsf{nil} & \{\emph{if-false}\} \\
\end{tabular}
%\end{center}
\index{axiom!if-true, if-false}\index{axiom, by name!\{if-true\}, \{if-false\}}\index{equation, by name!\{if-true\}, \{if-false\}}\index{operator, by name!if (select formula)}\seeonlyindex{if}{operator}
\caption{Formula selector: \textsf{if}.}
\label{fig:if-axioms}
\end{figure}
With the \textsf{if} operator, we can use DoubleCheck
to test our expectations of \textsf{(append $xs$ $ys$)}
by comparing \textsf{(append $xs$ $ys$)} to the \{\emph{app1}\} formula
if $xs$ is nonempty and to the \{\emph{app0}\} formula if $xs$ is empty.
Figure~\ref{fig:append-test} (page \pageref{fig:append-test})
displays a property definition that formalizes this idea.
It uses an operator called
\textsf{equal} (box~\ref{equal}, page \pageref{equal})
to compare \textsf{(append $xs$ $ys$)}
to a formula selected by the \textsf{if} operator.
\begin{figure}
\index{definition!property}\index{data, random test}\index{random data}\index{defproperty}
\begin{code}
\begin{verbatim}
(defproperty append-test
(xs :value (random-list-of (random-natural))
ys :value (random-list-of (random-natural)))
(equal (append xs ys)
(if (consp xs)
(cons (first xs)
(append (rest xs) ys))
ys)))
\end{verbatim}
\end{code}
\caption{DoubleCheck test of \textsf{append}.}
\label{fig:append-test}
\end{figure}
\begin{aside}{equal}{``\textsf{equal}'' vs ``\textsf{=}''}
Why is it \textsf{(equal (append $xs$ $ys$) $\dots$)}?
Why not \textsf{(= (append $xs$ $ys$) $\dots$)}?
The ``\textsf{=}'' operator
is restricted to numbers, whereas the operator \textsf{equal} can check
for equality between any two values: numbers, lists, whatever.
The $=$ symbol reminds people who are
reading the formula that it compares numbers.
It reminds the computer, too, and that could
simplify mechanized reasoning or
promote efficient comparison.
Mostly, though, it's a matter of
taste.\index{equal, vs =}\seeonlyindex{equal}{predicate}\index{operator, by name!equal (\emph{see} predicate)}\index{predicate, by name!equal}\index{operator!numeric order ($<$, $<=$, $=$, $>=$, $>$)}\index{predicate!numeric order ($<$, $<=$, $=$, $>=$, $>$)}\index{order!numeric ($<$, $<=$, $=$, $>=$, $>$)}\index{number!order ($<$, $<=$, $=$, $>=$, $>$)}\index{compare!numbers ($<$, $<=$, $=$, $>=$, $>$)}\index{predicate!numeric order ($<$, $<=$, $=$, $>=$, $>$)}\index{compare!ACL2 values (equal)}
%We will be using ``='' when we can and hope it doesn't put too much of an extra burden on you.
%\caption{``\textsf{equal}'' vs ``\textsf{=}''}
%\label{equal}
\end{aside}
The append-test property might not be the first test you would think of,
but if the test failed to pass,
you would know for sure that something was wrong with the \textsf{append} operator.
The append-test property is so plainly correct that
we are going to state it in the form of equations that we accept as axioms
(figure~\ref{append-equations}). %, page \pageref{append-equations}).
As in the \{\emph{len}\} theorem, there are two \{\emph{append}\} equations,
which specify the meaning of the append operation in different situations.
One of them specifies the meaning when the first operand is a nonempty list,
the other specifies the meaning under all other circumstances.
\begin{figure}
%\begin{center}
\begin{tabular}{ll}
\multicolumn{2}{c}{Axioms \{\emph{append}\}} \\
\hline
\textsf{(append (cons $x$ $xs$) $ys$)} $=$ \textsf{(cons $x$ (append $xs$ $ys$))} & \{\emph{app1}\} \\
\textsf{(append nil $ys$)} $=$ $ys$ & \{\emph{app0}\} \\
\end{tabular}
%\end{center}
\index{axiom!append}\index{equation!append}\index{operator, by name!append}\index{append!operator}\index{concatenate}\index{equation, by name!\{app0\}, \{app1\}}\index{axiom, by name!\{app0\}, \{app1\}}
\caption{List concatenation: \textsf{append}.}
\label{append-equations}
\end{figure}
%\todo{Note 12/10/17, see also fig: Defining Concatenation
% Not sure what to do here. Had the following for app0 axiom, with note:
%(append ~$e$~ $ys$) = $ys$ & \{\emph{app0}\} \\
%~~~~\emph{Note: Cite \{\emph{app0}\} only if \{\emph{app1}\} doesn't match.}&\\
% However, the ACL2 documentation requires first operand of append to be a truelist,
% even though ACL2 with guards off allows it to violate that constraint.
% For now (and it's late in the game), going back to the constraint.
% Before reverting, the following comment stood here:
% These axioms allow non-true-lists as first operand. However, when guards are in effect,
% ACL2 chokes if first operand isn't a true list.
% When guards are disabled (append 1 (list 2 3)) = (list 2 3)
% I don't want to say anything about guards, but students may see error messages
% that talk about guards. Not sure how to handle this.
% }
These equations state simple properties of the \textsf{append} operation,
and it turns out that lots of other properties of the
\textsf{append} operation can be derived from them.
For example, we can prove that the length of
the concatenation of two lists is the sum of the lengths of the lists,
as tested by a DoubleCheck property in
chapter~\ref{ch:software-testing-prefix-notation} (page \pageref{additive-lengths-test}).
We call this theorem the \emph{additive law of concatenation}.
Let's see how a proof of this law could be carried out.
First, let's break it down into some special cases.
We will use L($n$) as shorthand for the proposition that
\textsf{(len (append [$x_1$ $x_2$ \dots $x_n$] $ys$))}
is the sum of \textsf{(len [$x_1$ $x_2$ $\dots$ $x_n$])} and \textsf{(len $ys$)}.
That makes L a predicate whose universe of discourse is
the natural numbers.
\label{additive-concat-law-predicate}
\begin{center}
% old prefix notation: \begin{tabbing}
% old prefix notation: L($n$) $\equiv$ (= \=(len (append [$x_1$ $x_2$ \dots $x_n$] $ys$)) \\
% old prefix notation: \>(+ (len [$x_1$ $x_2$ \dots $x_n$]) (len $ys$)))
% old prefix notation: \end{tabbing}
L($n$) $\equiv$ \textsf{(len (append [$x_1$ $x_2$ $\dots$ $x_n$] $ys$))} $=$
\textsf{(+ (len [$x_1$ $x_2$ $\dots$ $x_n$]) (len $ys$))}
\end{center}
For the first few values of $n$, L($n$) would stand for the following equations:\\
\hspace*{1cm}L(0) $\equiv$ \textsf{(len (append nil $ys$))} $=$ \textsf{(+ (len nil) (len $ys$))} \\
\hspace*{1cm}L(1) $\equiv$ \textsf{(len (append [$x_1$] $ys$))} $=$ \textsf{(+ (len [$x_1$]) (len $ys$))} \\
\hspace*{1cm}L(2) $\equiv$ \textsf{(len (append [$x_1$ $x_2$] $ys$))} $=$ \textsf{(+ (len [$x_1$ $x_2$]) (len $ys$))} \\
\hspace*{1cm}L(3) $\equiv$ \textsf{(len (append [$x_1$ $x_2$ $x_3$] $ys$))} $=$ \textsf{(+ (len [$x_1$ $x_2$ $x_3$]) (len $ys$))} \\
%\hspace*{1cm}L(4) $\equiv$ \textsf{(len (append [$x_1$ $x_2$ $x_3$ $x_4$] $ys$))} $=$ \textsf{(+ (len [$x_1$ $x_2$ $x_3$ $x_4$]) (len $ys$))}\\
%\todo{COMMENT ONLY, NO TODO
%in case we want to go back to the prefix =
%\begin{center}
%\begin{tabular}{llll}
%L(0) & $\equiv$ & \textsf{(=} &\textsf{(len (append nil $ys$))} \\
% & & &\textsf{(+ (len nil) (len $ys$)))} \\
%L(1) & $\equiv$ & \textsf{(=} &\textsf{(len (append [$x_1$] $ys$))} \\
% & & &\textsf{(+ (len [$x_1$]) (len $ys$))}) \\
%L(2) & $\equiv$ & \textsf{(=} &\textsf{(len (append [$x_1$ $x_2$] $ys$))}\\
% & & &\textsf{(+ (len [$x_1$ $x_2$]) (len $ys$)))} \\
%L(3) & $\equiv$ & \textsf{(=} &\textsf{(len (append [$x_1$ $x_2$ $x_3$] $ys$))} \\
% & & &\textsf{(+ (len [$x_1$ $x_2$ $x_3$]) (len $ys$)))} \\
%L(4) & $\equiv$ & \textsf{(=} &\textsf{(len (append [$x_1$ $x_2$ $x_3$ $x_4$] $ys$))} \\
% & & &\textsf{(+ (len [$x_1$ $x_2$ $x_3$ $x_4$]) (len $ys$)))}
%\end{tabular}
%\end{center}
%END OF TODO COMMENT}
We can derive L(0) from the \{\emph{append}\} and \{\emph{len}\} axioms as follows.
We start from the left-hand side of the equation that L(0) stands for and
cite some axioms about \textsf{append}, \textsf{len}, and numeric algebra,
one by one. We end up with the right-hand side of the equation L(0).
\begin{center}
\begin{tabular}{lll}
\multicolumn{3}{c}{\emph{Proof of L(0), citing axioms}}\\
\hline
& \textsf{(len (append nil $ys$))} & \\
$=$ & \textsf{(len $ys$)} & \{\emph{app0}\} (page \pageref{append-equations})\\
$=$ & \textsf{(+ (len $ys$) 0)} & \{$+$ identity\} (page \pageref{fig-02-01}) \\
$=$ & \textsf{(+ 0 (len $ys$))} & \{$+$ commutative\} (page \pageref{fig-02-01}) \\
$=$ & \textsf{(+ (len nil) (len $ys$))} & \{\emph{len0}\} (page \pageref{len-equations})
\end{tabular}
\end{center}
That takes care of L(0). How about L(1)?
\begin{center}
\begin{tabular}{lll}
\multicolumn{3}{c}{\emph{Proof of L(1), citing axioms and proven equations}}\\
\hline
& \textsf{(len (append [$x_1$] $ys$))} & \\
$=$ & \textsf{(len (append (cons $x_1$ nil) $ys$))} & \{\emph{cons}\} (page \pageref{first-rest-cons}) \\
$=$ & \textsf{(len (cons $x_1$ (append nil $ys$)))} & \{\emph{app1}\} \\
$=$ & \textsf{(+ 1 (len (append nil $ys$)))} & \{\emph{len1}\} \\
$=$ & \textsf{(+ 1 (+ (len nil) (len $ys$)))} & \{L(0)\} ~~~~\emph{Note: L(0) already proved}\\
$=$ & \textsf{(+ (+ 1 (len nil)) (len $ys$))} & \{$+$ associative\} (page \pageref{fig-02-01}) \\
$=$ & \textsf{(+ (len (cons $x_1$ nil)) (len $ys$))} & \{\emph{len1}\} \\
$=$ & \textsf{(+ (len [$x_1$]) (len $ys$))} & \{\emph{cons}\} \\
\end{tabular}
\end{center}
That was a little harder. Will proving L(2) be still harder? Let's try it.
\begin{center}
\begin{tabular}{lll}
\multicolumn{3}{c}{\emph{Proof of L(2), citing axioms and proven equations}}\\
\hline\\[-1.0em]
& \textsf{(len (append [$x_1$ $x_2$] $ys$))} & \\
$=$ & \textsf{(len (append (cons $x_1$ [$x_2$]) $ys$))} & \{\emph{cons}\} \\
$=$ & \textsf{(len (cons $x_1$ (append [$x_2$] $ys$)))} & \{\emph{app1}\} \\
$=$ & \textsf{(+ 1 (len (append [$x_2$] $ys$)))} & \{\emph{len1}\} \\
$=$ & \textsf{(+ 1 (+ (len [$x_2$]) (len $ys$)))} & \{L(1)\} ~~~~\emph{Note: L(1) already proved}\\
$=$ & \textsf{(+ (+ 1 (len [$x_2$])) (len $ys$))} & \{$+$ associative\} \\
$=$ & \textsf{(+ (len (cons $x_1$ [$x_2$])) (len $ys$))} & \{\emph{len1}\} \\
$=$ & \textsf{(+ (len [$x_1$ $x_2$]) (len $ys$))} & \{\emph{cons}\} \\
\end{tabular}
\end{center}
Proving L(2) was no harder than proving L(1).
In fact, the two proofs cite exactly the same equations all the way through
except in one place.
Where the proof of L(1) cited the equation L(0),
the proof of L(2) cited the equation L(1).
Maybe the proof of L(3) will work the same way.
\begin{center}
\begin{tabular}{lll}
\multicolumn{3}{c}{\emph{Proof of L(3), citing axioms and proven equations}}\\
\hline\\[-1.0em]
& \textsf{(len (append [$x_1$ $x_2$ $x_3$] $ys$))} & \\
$=$ & \textsf{(len (append (cons $x_1$ [$x_2$ $x_3$]) $ys$))} & \{\emph{cons}\} \\
$=$ & \textsf{(len (cons $x_1$ (append [$x_2$ $x_3$] $ys$)))} & \{\emph{app1}\} \\
$=$ & \textsf{(+ 1 (len (append [$x_2$ $x_3$] $ys$)))} & \{\emph{len1}\} \\
$=$ & \textsf{(+ 1 (+ (len [$x_2$ $x_3$]) (len $ys$)))} & \{L(2)\} ~~~~\emph{Note: L(2) already proved}\\
$=$ & \textsf{(+ (+ 1 (len [$x_2$ $x_3$])) (len $ys$))} & \{$+$ associative\} \\
$=$ & \textsf{(+ (len (cons $x_1$ [$x_2$ $x_3$])) (len $ys$))} & \{\emph{len1}\} \\
$=$ & \textsf{(+ (len [$x_1$ $x_2$ $x_3$]) (len $ys$))} & \{\emph{cons}\} \\
\end{tabular}
\end{center}
\label{induction-rationale}\index{induction!rationale}\index{induction!proof by}
By now it's easy to see how to derive L(4) from L(3),
then L(5) from L(4), and so on.
If you had the patience, you could prove L(100), L(1000), or even L(1000000)
by following the established pattern.
It would not be hard to write a program to print out the proof of L($n$)
given any natural number $n$.
Since we know how to prove L($n$) for any natural number $n$,
it seems fair to say that we know all those equations are true.
That is, we think we know that the formula ($\forall$$n$.L($n$)) is true.
However, to prove that in a formal sense,
we need a rule of inference that allows us to make conclusions
from patterns like those we observed in proving L(1), L(2), and so on.
That rule of inference is known as \emph{mathematical induction}.
\seeonlyindex{mathematical induction}{induction}\index{induction!proof by}Mathematical induction
provides a way to prove that
formulas like ($\forall$$n$.P($n$)) are true
when P is a predicate whose universe of discourse is the natural numbers.
If for each natural number $n$, P($n$) stands for a proposition,
then mathematical induction is an inference rule that may be useful
in a proof that ($\forall$$n$.P($n$)) is true.
That is not to say that such a proof can always be constructed.
It's just that mathematical induction might provide some help in the process.
The inverse is also true: mathematical induction cannot help
if the universe of discourse is not the natural numbers.\footnote{Mathematical
induction is not the only form
of proof by induction, but all the other forms
(other than transfinite induction, which is a different animal)
can be contorted into proofs by mathematical induction.
We will stick with classical mathematical induction
and leave the variations for another time.
They are easy to learn for people who understand ordinary mathematical induction.}
The rule goes as follows. Infer the truth of ($\forall$$n$.P($n$)),
citing \{induction\} as a rule of inference,
from proofs of two propositions:
P(0) and $(\forall n.($P($n$) $\rightarrow$ P($n+1$)$))$.
\begin{center}
$(($P($0$) $\wedge$ $(\forall n.($P($n$) $\rightarrow$ P($n+1$)$)))$ $\rightarrow$ $(\forall n.$P($n$)$))$ $=$ $True$
~~~\{induction\}
\end{center}
%\begin{center}
%\begin{tabular}{lll}
%\emph{Prove:} & P($0$) & \emph{base case}\\
%\emph{Prove:} & ($\forall$$n$.(P($n$)$\rightarrow$P($n+1$)))&\emph{inductive case}\\
% &---------------------------------------------& \\
%\emph{Infer:} & ($\forall$$n$.P($n$)) & \emph{citing} \{induction\}\\
%\end{tabular}
%\end{center}
It's a very good deal if you think about it.
A direct proof of ($\forall$$n$.P($n$)) would require a proof of proposition P($n$)
for each value of $n$ ($0$, $1$, $2$, \dots).
But, in a \index{proof!by induction}proof by \index{induction!proof by}induction,
the only proposition that needs to be proved on its own is P($0$).
To justify any step in the proof of proposition P($n+1$)
(that is, any proposition in the predicate P with a nonzero index:
P($1$), P($2$), P($3$), \dots),
you are allowed to cite P($n$) as if it were a known theorem.
The proof of P($0$) is known as the
\seeonlyindex{base case}{induction}\index{induction!base case}\emph{base case}
in the proof by induction,
and the proof of (P($n$) $\rightarrow$ P($n+1$)) is known as the
\seeonlyindex{inductive case}{induction}\index{induction!inductive case}\emph{inductive case}.
The reason you can assume that P($n$) is true in the proof of P($n+1$)
(that is, in the inductive case)
is because the goal, according to part two of the inference rule
for induction, is to prove that the implication
P($n$)$\rightarrow$P($n+1$) is true.
When P($n$) is false, we know already from the truth table
of the implication operator (page \pageref{implication-truth-table})
that the implication P($n$)$\rightarrow$P($n+1$) is true.
There is no need to prove that again.
Therefore, in the proof of P($n+1$), we can assume P($n$) is true.
The assumption of P($n$) is known as the
\index{hypothesis!induction}\index{induction!hypothesis}\emph{induction hypothesis}.
It gives you a leg up in the proof of P($n+1$).
Figure~\ref{induction-rule} states the \{induction\} inference rule
in the form of natural deduction, a formal method of logic discussed
in section~\ref{sec:deduction}. You don't need to have studied that
section to understand inductive proofs, but in case you did study
natural deduction, figure~\ref{induction-rule} puts proof by
induction in that context.
\begin{figure}
%\begin{center}
\begin{spacing}{0.9}
\begin{tabular}{ll}
Prove P(0) &\emph{base case}\\
- - - - - - - - - - - - - - - - - - - - - &\\
Prove ($\forall$$n$.(P($n$)$\rightarrow$P($n+1$))) &\emph{inductive case}\\
-------------------------------------\{induction\} &\\
Infer ($\forall$$n$.P($n$)) &\\
\end{tabular}
\end{spacing}
%\end{center}
\caption{Mathematical induction: a rule of inference.}
\label{fig-04-01}\label{induction-rule}\index{induction!proof by}\index{proof!by induction}\index{inference rule, by name!\{induction\}}\index{induction!inference rule}
\end{figure}
\label{induction-hyp-def}Now,
let's apply mathematical induction to prove
the additive law of concatenation.
Here, the predicate that we will apply the method to is L
(page \pageref{additive-concat-law-predicate}).
\label{len-additive-thm}We
have already proved L(0), so we have already completed one of the
two proofs required to cite the mathematical induction inference rule.
All that is left is to prove ($\forall$$n$.(L($n$)$\rightarrow$L($n+1$))).
That is, we have to derive L($n+1$) from L($n$) for an arbitrary natural number $n$.
Fortunately, we know how to do this. Just copy the derivation of,
say, L(3) from L(2), but start with an append formula in which the first operand
is a list with $n+1$ elements and cite L($n$) where we would have cited L(2).
\begin{center}
\begin{tabular}{lll}
\multicolumn{3}{c}{\emph{Proof of L(n+1), citing axioms, proven equations, and L(n): L(n) $\rightarrow$ L(n+1)}}\\
\hline\\[-1.0em]
& \textsf{(len (append [$x_1$ $x_2$ \dots $x_{n+1}$] $ys$))} &\\
$=$ & \textsf{(len (append (cons $x_1$ [$x_2$ \dots $x_{n+1}$]) $ys$))} & \{\emph{cons}\}\\
$=$ & \textsf{(len (cons $x_1$ (append [$x_2$ \dots $x_{n+1}$] $ys$)))} & \{\emph{app1}\}\\
$=$ & \textsf{(+ 1 (len (append [$x_2$ \dots $x_{n+1}$] $ys$)))} & \{\emph{len1}\}\\
$=$ & \textsf{(+ 1 (+ (len [$x_2$ \dots $x_{n+1}$]) (len $ys$)))} & \{L($n$)\} ~~\emph{Note: induction hypothesis}\\
$=$ & \textsf{(+ (+ 1 (len [$x_2$ \dots $x_{n+1}$])) (len $ys$))} & \{$+$ associative\}\\
$=$ & \textsf{(+ (len (cons $x_1$ [$x_2$ \dots $x_{n+1}$])) (len $ys$))} & \{\emph{len1}\}\\
$=$ & \textsf{(+ (len [$x_1$ $x_2$ \dots $x_{n+1}$]) (len $ys$))} & \{\emph{cons}\}\\
\end{tabular}
\end{center}
\vspace*{5mm}
This completes the mathematical induction proving the
additive law of concatenation.
\begin{center}
\index{append!additive law}\index{theorem, by name!\{additive law of concatenation\}}\index{additive law of concatenation}\index{concatenate!additive law}\label{additive-law-concatenation}
\begin{tabular}{c}
Theorem \{\emph{additive law of concatenation}\} \\
\hline
$\forall n.($\textsf{(len (append [$x_1$ $x_2$ $\dots$ $x_n$] $ys$))}
$=$ \textsf{(+ (len [$x_1$ $x_2$ \dots $x_n$]) (len $ys$))}$)$\\
\end{tabular}
\end{center}
An important point to notice in this proof is that
we could not cite the \{\emph{cons}\} equation to replace \textsf{[$x_2$ $\dots$ $x_{n+1}$]}
with \textsf{(cons $x_2$ [$x_3$ $\dots$ $x_{n+1}$])}.
The reason we could not do this is that we are trying to derive
L($n+1$) from L($n$) without making any assumptions about $n$
other than the fact that it is a natural number.
Since zero is a natural number, the list \textsf{[$x_2$ $\dots$ $x_{n+1}$]}
could be empty,\footnote{The list [$x_2$ $\dots$ $x_{0+1}$] is empty
(\{\emph{nlst}\} numbered list notation,
figure~\ref{numbered-list-interpretation}, page \pageref{numbered-list-interpretation}).}
and the \textsf{cons} operator cannot deliver an empty list as its value.
In the next section, we will prove several properties of \textsf{append}
and its relationships with other operators.
These properties, and in fact all properties of the \textsf{append} operator,
can be derived from the \{\emph{append}\} axioms
(figure~\ref{append-equations}, page \pageref{append-equations}).
Those axioms state properties of the \textsf{append} operation
in two separate cases:
(1)~\{\emph{app0}\}: the first operand is the empty list and
(2)~\{\emph{app1}\}: the first operand is a nonempty list.
When the first operand is the empty list,
the result must be the second operand, no matter what.
When the first operand is not empty, it must have a first element,
and that element must be the first element of the concatenation.
The other elements of the concatenation are the ones you would get
if you appended the second operand to the rest of the first operand.
Both of these properties are so straightforward and easy to believe
that we would probably be willing to accept them as axioms with no proof at all,
so it might seem surprising that all of the other properties
of the \textsf{append} operation can be derived from
the two simple properties \{\emph{app0}\} and \{\emph{app1}\}.
That is the power of mathematical induction.
The two equations of the \{\emph{append}\} axioms
amount to an inductive definition of the \textsf{append} operator.
An inductive definition is circular in the sense
that some of the equations in the definition refer
to the operator on both sides of the equation.
Most of the time, people think of circular definitions
as unhelpful at best,
but they can be useful in mathematics.
You will see a lot of them and
gradually learn how to recognize and create useful,
circular (that is, inductive) definitions.
\begin{figure}
\begin{center}
\begin{tabular}{lp{3.5in}}
\emph{Complete} & All possible combinations of operands are covered by at least one equation in the definition. \\
\emph{Consistent} & Combinations of operands covered by two or more equations define the same value for the operation. \\
\emph{Computational} &
\begin{enumerate}
\item \emph{Noninductive Equation}: In at least one equation,
the operator being defined appears only on the left-hand side.
\item \emph{Reduced Computation}:
\index{three C's}\index{definition!inductive (circular)}On the right-hand side of an inductive equation,
each invocation of the operator being defined has operands that
are closer to the operands on the left-hand side of a noninductive equation
than to those on the left-hand side of the inductive equation.
\end{enumerate}
\end{tabular}
\end{center}
\vspace*{-6mm}
\caption{The three C's: a guide to inductive definitions.}
\label{fig:inductive-def-keys}
\end{figure}
It turns out that all operators that can be defined in
\index{software!as equations}\index{equation!software}software
have inductive definitions in the manner of the equations
of the \{\emph{append}\} axioms.
The keys to an inductive definition of an operator are listed in
figure~\ref{fig:inductive-def-keys}. % (page \pageref{fig:inductive-def-keys}).
All of the software we will discuss will take the form of
inductive definitions of operators.
That makes it possible to use mathematical induction as
a fundamental tool in verifying, to a logical certainty,
properties of that software.
\begin{exercises}
\exer {Prove $\forall xs.$\textsf{(natp (len $xs$))}.
You may
cite\index{equation, by name!\{natp0\}, \{natp1\}}\index{axiom, by name!\{natp0\}, \{natp1\}}
\{natp0\} and \{natp1\},
defined as follows. (\emph{Note}: This definition will be adequate for this exercise but it is not the full definition of \textsf{natp}.)
\begin{center}
\begin{tabular}{ll}
\textsf{(natp 0)} & \{natp0\}\\
$\forall x.($\textsf{(natp $x$)} $\rightarrow$ \textsf{(natp (+ $x$ 1))}$)$ & \{natp1\}\\
\hline
\multicolumn{2}{c}{$\forall x$ \emph{universe of discourse: numbers}}\\
\end{tabular}
\end{center}}
\exer {Prove: $\forall n.(\forall x.($\textsf{(expt $x$ $n$)} $=$ $x^n))$~~~\{\emph{expt}\}\\
$\forall n$ universe of discourse: natural numbers;
$\forall x$ universe of discourse: non-zero numbers\\
Assume the equations \{\emph{expt0}\} and \{\emph{expt1}\} are true.
\label{expt-equations}\label{expt-thm}\index{theorem, by name!\{expt\}}\index{axiom!exponent}\index{operator, by name!expt ($x^n$)}\seeonlyindex{expt}{operator}\index{axiom, by name!\{expt0\}, \{expt1\}}\index{equation, by name!\{expt0\}, \{expt1\}}
\begin{center}
\begin{tabular}{ll}
\textsf{(expt $x$ 0)} $=$ 1 & \{\emph{expt0}\} \\
\textsf{(expt $x$ (+ $n$ 1))} $=$ \textsf{($*$ $x$ (expt $x$ $n$))} & \{\emph{expt1}\} \\
\hline
\multicolumn{2}{c}{{\textsf{($*$ $x$ $y$)} $=$ $x \times y$}}\\
\end{tabular}
\end{center}
}
\end{exercises}
\section{Defun: Defining Operators in ACL2}
\label{sec:defun}
Now, we are going to let you in on a little secret.
The axioms we wrote for the \textsf{append} operator are very
close to an ACL2 definition of \textsf{append}.
Operators are defined in ACL2 with a \textsf{defun} command,
which has four \index{defun}\index{definition!defun}\index{definition!operator}\index{operator!defun}parts.
\vspace{2mm}
\begin{tabular}{ll}
\multicolumn{2}{c}{\textsf{(defun $f$ ($x_1$ $x_2$ $\dots$ $x_n$) $formula$)}}\\
\hline
\textsf{defun} & keyword\\
$f$ & name for the operator being defined\\
\textsf{($x_1$ $x_2$ $\dots$ $x_n$)} & names designating operands, surrounded by parentheses\\