forked from djvelleman/HTPIwL
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathChap6.qmd
1996 lines (1673 loc) · 83.8 KB
/
Chap6.qmd
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
# Mathematical Induction
## 6.1. Proof by Mathematical Induction
Section 6.1 of *HTPI* introduces a new proof technique called *mathematical induction*. It is used for proving statements of the form `∀ (n : Nat), P n`. Here is how it works (*HTPI* p. 273):
#### To prove a goal of the form `∀ (n : Nat), P n`:
::: {.ind}
First prove `P 0`, and then prove `∀ (n : Nat), P n → P (n + 1)`. The first of these proofs is sometimes called the *base case* and the second the *induction step*.
:::
For an explanation of why this strategy works to establish the truth of `∀ (n : Nat), P n`, see *HTPI*. Here we focus on using mathematical induction in Lean.
To use mathematical induction in a Lean proof, we will use the tactic `by_induc`. If the goal has the form `∀ (n : Nat), P n`, then the `by_induc` tactic leaves the list of givens unchanged, but it replaces the goal with the goals for the base case and induction step. Thus, the effect of the tactic can be summarized as follows:
::: {.lftrt}
::: {.bef}
```state
>> ⋮
⊢ ∀ (n : Nat), P n
```
:::
::: {.aft}
```state
case Base_Case
>> ⋮
⊢ P 0
case Induction_Step
>> ⋮
⊢ ∀ (n : Nat), P n → P (n + 1)
```
:::
:::
To illustrate proof by mathematical induction in Lean, we turn first to Example 6.1.2 in *HTPI*, which gives a proof of the statement $\forall n \in \mathbb{N} (3 \mid (n^3 - n))$. For reasons that we will explain a little later, we will prove a slightly different theorem: $\forall n \in \mathbb{N} (3 \mid (n^3 + 2n))$. Here is a proof of the theorem, modeled on the proof in Example 6.1.2 of *HTPI* (*HTPI* pp. 276--277).
::: {.thm}
For every natural number $n$, $3 \mid (n^3 + 2n)$.
:::
::: {.proof}
We use mathematical induction.
Base Case: If $n = 0$, then $n^3 + 2n = 0 = 3 \cdot 0$, so $3 \mid (n^3 + 2n)$.
Induction Step: Let $n$ be an arbitrary natural number and suppose $3 \mid (n^3 + 2n)$. Then we can choose an integer $k$ such that $3k = n^3 + 2n$. Thus,
\begin{align*}
(n+1)^3 + 2(n+1) &= n^3 + 3n^2 + 3n + 1 + 2n + 2\\
&= (n^3 + 2n) + 3n^2 + 3n + 3\\
&= 3k + 3n^2 + 3n + 3\\
&= 3(k + n^2 + n + 1).
\end{align*}
Therefore $3 \mid ((n+1)^3 + 2(n+1))$, as required. [ □]{.excl}\qedhere
:::
Now let's try writing the same proof in Lean. We start, of course, with the `by_induc` tactic.
::: {.inout}
::: {.inpt}
```lean
theorem Like_Example_6_1_2 :
∀ (n : Nat), 3 ∣ n ^ 3 + 2 * n := by
by_induc
**done::
```
:::
::: {.outpt}
```state
case Base_Case
⊢ 3 ∣ 0 ^ 3 + 2 * 0
case Induction_Step
⊢ ∀ (n : ℕ),
>> 3 ∣ n ^ 3 + 2 * n →
>> 3 ∣ (n + 1) ^ 3 +
>> 2 * (n + 1)
```
:::
:::
The base case is easy: The `define` tactic tells us that the goal means `∃ (c : Nat), 0 ^ 3 + 2 * 0 = 3 * c`, and then `apply Exists.intro 0` changes the goal to `0 ^ 3 + 2 * 0 = 3 * 0`. Both sides are definitionally equal to `0`, so `rfl` finishes off the base case. For the induction step, we begin, as in the *HTPI* proof, by introducing an arbitrary natural number `n` and assuming `3 ∣ n ^ 3 + 2 * n`. This assumption is called the *inductive hypothesis*, so in the Lean proof we give it the identifier `ih`. Our goal now is to prove `3 ∣ (n + 1) ^ 3 + 2 * (n + 1)`.
::: {.inout}
::: {.inpt}
```lean
theorem Like_Example_6_1_2 :
∀ (n : Nat), 3 ∣ n ^ 3 + 2 * n := by
by_induc
· -- Base Case
define
apply Exists.intro 0
rfl
done
· -- Induction Step
fix n : Nat
assume ih : 3 ∣ n ^ 3 + 2 * n
**done::
done
```
:::
::: {.outpt}
```state
case Induction_Step
n : ℕ
ih : 3 ∣ n ^ 3 + 2 * n
⊢ 3 ∣ (n + 1) ^ 3 +
>> 2 * (n + 1)
```
:::
:::
The rest of the Lean proof follows the model of the *HTPI* proof: we use the inductive hypothesis to introduce a `k` such that `n ^ 3 + 2 * n = 3 * k`, and then we use a calculational proof to show that `(n + 1) ^ 3 + 2 * (n + 1) = 3 * (k + n ^ 2 + n + 1)`.
```lean
theorem Like_Example_6_1_2 :
∀ (n : Nat), 3 ∣ n ^ 3 + 2 * n := by
by_induc
· -- Base Case
define --Goal : ∃ (c : Nat), 0 ^ 3 + 2 * 0 = 3 * c
apply Exists.intro 0
rfl
done
· -- Induction Step
fix n : Nat
assume ih : 3 ∣ n ^ 3 + 2 * n
define at ih --ih : ∃ (c : Nat), n ^ 3 + 2 * n = 3 * c
obtain (k : Nat) (h1 : n ^ 3 + 2 * n = 3 * k) from ih
define --Goal : ∃ (c : Nat), (n + 1) ^ 3 + 2 * (n + 1) = 3 * c
apply Exists.intro (k + n ^ 2 + n + 1)
show (n + 1) ^ 3 + 2 * (n + 1) = 3 * (k + n ^ 2 + n + 1) from
calc (n + 1) ^ 3 + 2 * (n + 1)
_ = n ^ 3 + 2 * n + 3 * n ^ 2 + 3 * n + 3 := by ring
_ = 3 * k + 3 * n ^ 2 + 3 * n + 3 := by rw [h1]
_ = 3 * (k + n ^ 2 + n + 1) := by ring
done
done
```
Next we'll look at Example 6.1.1 in *HTPI*, which proves that for every natural number $n$, $2^0 + 2^1 + \cdots + 2^n = 2^{n+1} - 1$. Once again, we will change the theorem slightly before proving it in Lean. What we will prove is that for every $n$, $(2^0 + 2^1 + \cdots + 2^n) + 1 = 2^{n+1}$. To understand this theorem you must be able to recognize what the "$\cdots$" stands for. A human reader will probably realize that the numbers being added up here are the numbers of the form $2^i$, where $i$ runs through all of the natural numbers from 0 to $n$. But Lean can't be expected to figure out this pattern, so we must be more explicit.
Section 6.3 of *HTPI* introduces the explicit notation that mathematicians usually use for such sums. If $f$ is a function whose domain is the natural numbers, then
$$
\sum_{i=0}^n f(i) = f(0) + f(1) + \cdots + f(n).
$$
More generally, if $k \le n$ then
$$
\sum_{i=k}^n f(i) = f(k) + f(k+1) + \cdots + f(n).
$$
The notation we will use in Lean for this sum is `Sum i from k to n, f i`. Thus, a mathematician would state our theorem like this:
::: {.thm}
For every natural number $n$,
$$
\left(\sum_{i=0}^n 2^i\right) + 1 = 2^{n+1}.
$$
:::
And to state the same theorem in Lean, we will write:
```lean
theorem Like_Example_6_1_1 :
∀ (n : Nat), (Sum i from 0 to n, 2 ^ i) + 1 = 2 ^ (n + 1)
```
We will have more to say later about how the notation `Sum i from k to n, f i` is defined. But to use the notation in a proof, we will just need to know a few theorems. The `#check` command will tell us the meanings of the theorems `sum_base`, `sum_step`, and `sum_from_zero_step`:
::: {.ind}
```
@sum_base : ∀ {A : Type} [inst : AddZeroClass A] {k : ℕ} {f : ℕ → A},
Sum i from k to k, f i = f k
@sum_step : ∀ {A : Type} [inst : AddZeroClass A] {k n : ℕ} {f : ℕ → A},
k ≤ n → Sum i from k to n + 1, f i =
(Sum i from k to n, f i) + f (n + 1)
@sum_from_zero_step :
∀ {A : Type} [inst : AddZeroClass A] {n : ℕ} {f : ℕ → A},
Sum i from 0 to n + 1, f i =
(Sum i from 0 to n, f i) + f (n + 1)
```
:::
As usual, we don't need to pay too much attention to the implicit arguments in the first line of each statement. What is important is that `sum_base` can be used to prove any statement of the form
::: {.ind}
```
Sum i from k to k, f i = f k
```
:::
and `sum_step` proves any statement of the form
::: {.ind}
```
k ≤ n → Sum i from k to n + 1, f i = (Sum i from k to n, f i) + f (n + 1).
```
:::
In the case `k = 0`, we have the simpler theorem `sum_from_zero_step`, which proves
::: {.ind}
```
Sum i from 0 to n + 1, f i = (Sum i from 0 to n, f i) + f (n + 1).
```
:::
With that preparation, we can start on the proof. Once again we begin with the `by_induc` tactic. Our goal for the base case is `(Sum i from 0 to 0, 2 ^ i) + 1 = 2 ^ (0 + 1)`. To deal with the term `Sum i from 0 to 0, 2 ^ i`, we use that fact that `sum_base` proves `Sum i from 0 to 0, 2 ^ i = 2 ^ 0`. It follows that the tactic `rewrite [sum_base]` will change the goal to `2 ^ 0 + 1 = 2 ^ (0 + 1)`. Of course, this means `2 = 2`, so `rfl` finishes the base case. For the induction step, we start by introducing an arbitrary natural number `n` and assuming the inductive hypothesis.
```lean
theorem Like_Example_6_1_1 :
∀ (n : Nat), (Sum i from 0 to n, 2 ^ i) + 1 = 2 ^ (n + 1) := by
by_induc
· -- Base Case
rewrite [sum_base]
rfl
done
· -- Induction Step
fix n : Nat
assume ih : (Sum i from 0 to n, 2 ^ i) + 1 = 2 ^ (n + 1)
**done::
done
```
Our goal is now `(Sum i from 0 to n + 1, 2 ^ i) + 1 = 2 ^ (n + 1 + 1)`, and we use a calculational proof to prove this. Often the key to the proof of the induction step is to find a relationship between the inductive hypothesis and the goal. In this case, that means finding a relationship between `Sum i from 0 to n, 2 ^ i` and `Sum i from 0 to n + 1, 2 ^ i`. The relationship we need is given by the theorem `sum_from_zero_step`. The tactic `rewrite [sum_from_zero_step]` will replace `Sum i from 0 to n + 1, 2 ^ i` with `(Sum i from 0 to n, 2 ^ i) + 2 ^ (n + 1)`. The rest of the calculation proof involves straightforward algebra, handled by the `ring` tactic, together with an application of the inductive hypothesis.
```lean
theorem Like_Example_6_1_1 :
∀ (n : Nat), (Sum i from 0 to n, 2 ^ i) + 1 = 2 ^ (n + 1) := by
by_induc
· -- Base Case
rewrite [sum_base]
rfl
done
· -- Induction Step
fix n : Nat
assume ih : (Sum i from 0 to n, 2 ^ i) + 1 = 2 ^ (n + 1)
show (Sum i from 0 to n + 1, 2 ^ i) + 1 = 2 ^ (n + 1 + 1) from
calc (Sum i from 0 to n + 1, 2 ^ i) + 1
_ = (Sum i from 0 to n, 2 ^ i) + 2 ^ (n + 1) + 1 := by
rw [sum_from_zero_step]
_ = (Sum i from 0 to n, 2 ^ i) + 1 + 2 ^ (n + 1) := by ring
_ = 2 ^ (n + 1) + 2 ^ (n + 1) := by rw [ih]
_ = 2 ^ (n + 1 + 1) := by ring
done
done
```
The last example in Section 6.1 of *HTPI* gives a proof of the statement $\forall n \ge 5 (2^n > n^2)$. The proof is by mathematical induction, but since we are only interested in natural numbers greater than or equal to 5, it uses 5 in the base case instead of 0. Here are the theorem and proof from *HTPI* (*HTPI* p. 278):
::: {.thm}
For every natural number $n \ge 5$, $2^n > n^2$.
:::
::: {.proof}
By mathematical induction.
Base case: When $n = 5$ we have $2^n = 32 > 25 = n^2$.
Induction step: Let $n \ge 5$ be arbitrary, and suppose that $2^n > n^2$. Then
\begin{align*}
2^{n+1} &= 2 \cdot 2^n\\
&> 2n^2 &&\text{(inductive hypothesis)}\\
&= n^2 + n^2\\
&\ge n^2 + 5n &&\text{(since $n \ge 5$)}\\
&= n^2 + 2n + 3n\\
&> n^2 + 2n + 1 = (n+1)^2. &&\Box
\end{align*}
\renewcommand{\qed}{}
:::
Notice that the sequence of calculations at the end of the proof mixes $=$, $>$, and $\ge$ in a way that establishes the final conclusion $2^{n+1} > (n+1)^2$. As we'll see, such a mixture is allowed in calculational proofs in Lean as well.
To write this proof in Lean, there is no need to specify that the base case should be `n = 5`; the `by_induc` tactic is smart enough to figure that out on its own, as you can see in the tactic state below. (Notice that, as in *HTPI*, in Lean we can write `∀ n ≥ k, P n` as an abbreviation for `∀ (n : Nat), n ≥ k → P n`.)
::: {.inout}
::: {.inpt}
```lean
theorem Example_6_1_3 :
∀ n ≥ 5, 2 ^ n > n ^ 2 := by
by_induc
**done::
```
:::
::: {.outpt}
```state
case Base_Case
⊢ 2 ^ 5 > 5 ^ 2
case Induction_Step
⊢ ∀ n ≥ 5,
>> 2 ^ n > n ^ 2 →
>> 2 ^ (n + 1) >
>> (n + 1) ^ 2
```
:::
:::
To complete this proof we'll use two tactics we haven't used before: `decide` and `linarith`. The truth or falsity of the inequality in the base case can be decided by simply doing the necessary arithmetic. The tactic `decide` can do such calculations, and it proves the base case.
For the induction step, we introduce an arbitrary natural number `n`, assume `n ≥ 5`, and assume the inductive hypothesis, `2 ^ n > n ^ 2`. Then we use a calculational proof to imitate the reasoning at the end of the *HTPI* proof. The tactic `linarith` makes inferences that involve combining linear equations and inequalities. It is able to prove almost all of the inequalities in the calculational proof. The exception is `n * n ≥ 5 * n` (which is not linear because of the term `n * n`). So we prove that inequality separately, using a theorem from Lean's library, `Nat.mul_le_mul_right`. The command `#check @Nat.mul_le_mul_right` tells us the meaning of that theorem:
::: {.ind}
```
@Nat.mul_le_mul_right : ∀ {n m : ℕ} (k : ℕ), n ≤ m → n * k ≤ m * k
```
:::
Thus, `Nat.mul_le_mul_right n` can be used to prove the statement `5 ≤ n → 5 * n ≤ n * n`. Lean recognizes `x ≥ y` as meaning the same thing as `y ≤ x`, so we can apply this statement to our assumption `n ≥ 5` to prove that `n * n ≥ 5 * n`. Once we have proven that inequality, the `linarith` tactic can use it to complete the required inequality reasoning.
```lean
theorem Example_6_1_3 : ∀ n ≥ 5, 2 ^ n > n ^ 2 := by
by_induc
· -- Base Case
decide
done
· -- Induction Step
fix n : Nat
assume h1 : n ≥ 5
assume ih : 2 ^ n > n ^ 2
have h2 : n * n ≥ 5 * n := Nat.mul_le_mul_right n h1
show 2 ^ (n + 1) > (n + 1) ^ 2 from
calc 2 ^ (n + 1)
_ = 2 * 2 ^ n := by ring
_ > 2 * n ^ 2 := by linarith
_ ≥ n ^ 2 + 5 * n := by linarith
_ > n ^ 2 + 2 * n + 1 := by linarith
_ = (n + 1) ^ 2 := by ring
done
done
```
Finally, we turn to the question of why we made small changes in two of the examples from *HTPI*. Perhaps you have guessed by now that we were trying to avoid the use of subtraction. All of the numbers in the examples in this section were natural numbers, and subtraction of natural numbers is problematic. In the natural numbers, `3 - 2` is equal to `1`, but what is `2 - 3`? Lean's answer is `0`.
```lean
++#eval:: 2 - 3 --Answer: 0
```
In Lean, if `a` and `b` are natural numbers and `a < b`, then `a - b` is defined to be `0`. As a result, the algebraic laws of natural number subtraction are complicated. For example, `2 - 3 + 1 = 0 + 1 = 1`, but `2 + 1 - 3 = 3 - 3 = 0`, so it is not true that for all natural numbers `a`, `b`, and `c`, `a - b + c = a + c - b`.
If you thought that the answer to the subtraction problem `2 - 3` was `-1`, then you automatically switched from the natural numbers to the integers. (Recall that the natural numbers are the numbers 0, 1, 2, ..., while the integers are the numbers ..., –3, –2, –1, 0, 1, 2, 3, ....) To a human mathematician, this is a perfectly natural thing to do: the natural numbers are a subset of the integers, so `2` and `3` are not only natural numbers but also integers, and we can compute `2 - 3` in the integers.
However, that's not how things work in Lean. In Lean, different types are completely separate. In particular, `Nat` and `Int` are separate types, and therefore the natural numbers are not a subset of the integers. Of course, there is an integer 2, but it is different from the natural number 2. By default, Lean assumes that `2` denotes the natural number 2, but you can specify that you want the integer 2 by writing `(2 : Int)`. Subtraction of integers in Lean is the subtraction you are familiar with, and it has all the algebraic properties you would expect. If we want to use subtraction in the theorems in this section, we are better off using familiar integer subtraction rather than funky natural number subtraction.
To prove the theorem in Example 6.1.1 as it appears in *HTPI*, we could state the theorem like this:
```lean
theorem Example_6_1_1 :
∀ (n : Nat), Sum i from 0 to n, (2 : Int) ^ i =
(2 : Int) ^ (n + 1) - (1 : Int)
```
The expression `Sum i from 0 to n, (2 : Int) ^ i` denotes a sum of integers, so it is an integer. Similarly, the right side of the equation is an integer, and the equation asserts the equality of two integers. The subtraction on the right side of the equation is integer subtraction, so we can use the usual algebraic laws to reason about it. In fact, the proof of the theorem in this form is not hard:
```lean
theorem Example_6_1_1 :
∀ (n : Nat), Sum i from 0 to n, (2 : Int) ^ i =
(2 : Int) ^ (n + 1) - (1 : Int) := by
by_induc
· -- Base Case
rewrite [sum_base]
rfl
done
· -- Induction Step
fix n : Nat
assume ih : Sum i from 0 to n, (2 : Int) ^ i =
(2 : Int) ^ (n + 1) - (1 : Int)
show Sum i from 0 to n + 1, (2 : Int) ^ i =
(2 : Int) ^ (n + 1 + 1) - (1 : Int) from
calc Sum i from 0 to n + 1, (2 : Int) ^ i
_ = (Sum i from 0 to n, (2 : Int) ^ i)
+ (2 : Int) ^ (n + 1) := by rw [sum_from_zero_step]
_ = (2 : Int) ^ (n + 1) - (1 : Int)
+ (2 : Int) ^ (n + 1) := by rw [ih]
_ = (2 : Int) ^ (n + 1 + 1) - (1 : Int) := by ring
done
done
```
If you change `(2 : Int)` and `(1 : Int)` to `2` and `1`, then the right side of the equation will be a difference of two natural numbers, and Lean will interpret the subtraction as natural number subtraction. The proof won't work because the `ring` tactic is not able to deal with the peculiar algebraic properties of natural number subtraction. (The theorem is still true, but the proof is harder.)
### Exercises
::: {.numex arguments="1"}
```lean
theorem Like_Exercise_6_1_1 :
∀ (n : Nat), 2 * Sum i from 0 to n, i = n * (n + 1) := sorry
```
:::
::: {.numex arguments="2"}
```lean
theorem Like_Exercise_6_1_4 :
∀ (n : Nat), Sum i from 0 to n, 2 * i + 1 = (n + 1) ^ 2 := sorry
```
:::
::: {.numex arguments="3"}
```lean
theorem Exercise_6_1_9a : ∀ (n : Nat), 2 ∣ n ^ 2 + n := sorry
```
:::
::: {.numex arguments="4"}
```lean
theorem Exercise_6_1_13 :
∀ (a b : Int) (n : Nat), (a - b) ∣ (a ^ n - b ^ n) := sorry
```
:::
::: {.numex arguments="5"}
```lean
theorem Exercise_6_1_15 : ∀ n ≥ 10, 2 ^ n > n ^ 3 := sorry
```
:::
::: {.numex arguments="6"}
```lean
lemma nonzero_is successor :
∀ (n : Nat), n ≠ 0 → ∃ (m : Nat), n = m + 1 := sorry
```
:::
For the next two exercises you will need the following definitions:
```lean
def nat_even (n : Nat) : Prop := ∃ (k : Nat), n = 2 * k
def nat_odd (n : Nat) : Prop := ∃ (k : Nat), n = 2 * k + 1
```
::: {.numex arguments="7"}
```lean
theorem Exercise_6_1_16a1 :
∀ (n : Nat), nat_even n ∨ nat_odd n := sorry
```
:::
::: {.numex arguments="8"}
```lean
--Hint: You may find the lemma nonzero_is_successor
--from a previous exercise useful, as well as Nat.add_right_cancel.
theorem Exercise_6_1_16a2 :
∀ (n : Nat), ¬(nat_even n ∧ nat_odd n) := sorry
```
:::
## 6.2. More Examples
We saw in the last section that mathematical induction can be used to prove theorems about calculations involving natural numbers. But mathematical induction has a much wider range of uses. Section 6.2 of *HTPI* illustrates this by proving two theorems about finite sets.
How can mathematical induction be used to prove a statement about finite sets? To say that a set is finite means that it has $n$ elements, for some natural number $n$. Thus, to say that all finite sets have some property, we can say that for every natural number $n$, every set with $n$ elements has the property. Since this statement starts with "for every natural number $n$," we can use mathematical induction to try to prove it.
What does it mean to say that a set "has $n$ elements"? Section 6.2 of *HTPI* says that for the proofs in that section, "an intuitive understanding of this concept will suffice." Unfortunately, intuition is not Lean's strong suit! So we'll need to be more explicit about how to talk about finite sets in Lean.
In Chapter 8, we'll define `numElts A n` to be a proposition saying that the set `A` has `n` elements, and we'll prove several theorems involving that proposition. Those theorems make precise and explicit the intuitive ideas that we'll need in this section. We'll state those theorems here and use them in our proofs, but you'll have to wait until Section 8.1½ to see how they are proven. Here are the theorems we'll need:
```lean
theorem zero_elts_iff_empty {U : Type} (A : Set U) :
numElts A 0 ↔ empty A
theorem one_elt_iff_singleton {U : Type} (A : Set U) :
numElts A 1 ↔ ∃ (x : U), A = {x}
theorem nonempty_of_pos_numElts {U : Type} {A : Set U} {n : Nat}
(h1 : numElts A n) (h2 : n > 0) : ∃ (x : U), x ∈ A
theorem remove_one_numElts {U : Type} {A : Set U} {n : Nat} {a : U}
(h1 : numElts A (n + 1)) (h2 : a ∈ A) : numElts (A \ {a}) n
```
These theorems should make intuitive sense. The first says that a set has zero elements if and only if it is empty, and the second says that a set has one element if and only if it is a singleton set. The third theorem says that if a set has a positive number of elements, then there is something in the set. And the fourth says that if a set has $n + 1$ elements and you remove one element, then the resulting set has $n$ elements. You can probably guess that we'll be using the last theorem in the induction steps of our proofs.
Our first theorem about finite sets says that if $R$ is a partial order on $A$, then every finite, nonempty subset of $A$ has an $R$-minimal element. (This is not true in general for infinite subsets of $A$. Can you think of an example of an infinite subset of a partially ordered set that has no minimal element?) To say that a set is finite and nonempty we can say that it has $n$ elements for some $n \ge 1$. So here's how we state our theorem in Lean:
```lean
theorem Example_6_2_1 {A : Type} (R : BinRel A) (h : partial_order R) :
∀ n ≥ 1, ∀ (B : Set A), numElts B n →
∃ (x : A), minimalElt R x B
```
When we use mathematical induction to prove this theorem, the base case will be `n = 1`. To write the proof for the base case, we start by assuming `B` is a set with one element. We can then use the theorem `one_elt_iff_singleton` to conclude that `B = {b}`, for some `b` of type `A`. We need to prove that `B` has a minimal element, and the only possibility for the minimal element is `b`. Verifying that `minimalElt R b B` is straightforward. Here is the proof of the base case:
```lean
theorem Example_6_2_1 {A : Type} (R : BinRel A) (h : partial_order R) :
∀ n ≥ 1, ∀ (B : Set A), numElts B n →
∃ (x : A), minimalElt R x B := by
by_induc
· -- Base Case
fix B : Set A
assume h2 : numElts B 1
rewrite [one_elt_iff_singleton] at h2
obtain (b : A) (h3 : B = {b}) from h2
apply Exists.intro b
define --Goal : b ∈ B ∧ ¬∃ x ∈ B, R x b ∧ x ≠ b
apply And.intro
· -- Proof that b ∈ B
rewrite [h3] --Goal : b ∈ {b}
define --Goal : b = b
rfl
done
· -- Proof that nothing in B is smaller than b
by_contra h4
obtain (x : A) (h5 : x ∈ B ∧ R x b ∧ x ≠ b) from h4
have h6 : x ∈ B := h5.left
rewrite [h3] at h6 --h6 : x ∈ {b}
define at h6 --h6 : x = b
show False from h5.right.right h6
done
done
· -- Induction Step
**done::
done
```
Notice that since the definition of `minimalElt R b B` involves a negative statement, we found it convenient to use proof by contradiction to prove it.
For the induction step, we assume that `n ≥ 1` and that every set with `n` elements has an `R`-minimal element. We must prove that every set with `n + 1` elements has a minimal element, so we let `B` be an arbitrary set with `n + 1` elements. To apply the inductive hypothesis, we need a set with `n` elements. So we pick some `b ∈ B` (using the theorem `nonempty_of_pos_numElts`) and then remove it from `B` to get the set `B' = B \ {b}`. The theorem `remove_one_numElts` tells us that `B'` has `n` elements, so by the inductive hypothesis, we can then let `c` be a minimal element of `B'`. We now know about two elements of `B`: `b` and `c`. Which will be a minimal element of `B`? As explained in *HTPI*, it depends on whether or not `R b c`. We'll prove that if `R b c`, then `b` is a minimal element of `B`, and if not, then `c` is a minimal element. It will be convenient to prove these last two facts separately as lemmas. The first lemma says that in the situation at this point in the proof, if `R b c`, then `b` is an `R`-minimal element of `B`. Here is the proof.
```lean
lemma Lemma_6_2_1_1 {A : Type} {R : BinRel A} {B : Set A} {b c : A}
(h1 : partial_order R) (h2 : b ∈ B) (h3 : minimalElt R c (B \ {b}))
(h4 : R b c) : minimalElt R b B := by
define at h3
--h3 : c ∈ B \ {b} ∧ ¬∃ x ∈ B \ {b}, R x c ∧ x ≠ c
define --Goal : b ∈ B ∧ ¬∃ x ∈ B, R x b ∧ x ≠ b
apply And.intro h2 --Goal : ¬∃ x ∈ B, R x b ∧ x ≠ b
contradict h3.right with h5
obtain (x : A) (h6 : x ∈ B ∧ R x b ∧ x ≠ b) from h5
apply Exists.intro x --Goal : x ∈ B \ {b} ∧ R x c ∧ x ≠ c
apply And.intro
· -- Proof that x ∈ B \ {b}
show x ∈ B \ {b} from And.intro h6.left h6.right.right
done
· -- Proof that R x c ∧ x ≠ c
have Rtrans : transitive R := h1.right.left
have h7 : R x c := Rtrans x b c h6.right.left h4
apply And.intro h7
by_contra h8
rewrite [h8] at h6 --h6 : c ∈ B ∧ R c b ∧ c ≠ b
have Rantisymm : antisymmetric R := h1.right.right
have h9 : c = b := Rantisymm c b h6.right.left h4
show False from h6.right.right h9
done
done
```
The second lemma says that if `¬R b c`, then `c` is an `R`-minimal element of `B`. We'll leave the proof as an exercise for you:
```lean
lemma Lemma_6_2_1_2 {A : Type} {R : BinRel A} {B : Set A} {b c : A}
(h1 : partial_order R) (h2 : b ∈ B) (h3 : minimalElt R c (B \ {b}))
(h4 : ¬R b c) : minimalElt R c B := sorry
```
With this preparation, we are finally ready to give the proof of the induction step of `Example_6_2_1`:
```lean
theorem Example_6_2_1 {A : Type} (R : BinRel A) (h : partial_order R) :
∀ n ≥ 1, ∀ (B : Set A), numElts B n →
∃ (x : A), minimalElt R x B := by
by_induc
· -- Base Case
...
· -- Induction Step
fix n : Nat
assume h2 : n ≥ 1
assume ih : ∀ (B : Set A), numElts B n → ∃ (x : A), minimalElt R x B
fix B : Set A
assume h3 : numElts B (n + 1)
have h4 : n + 1 > 0 := by linarith
obtain (b : A) (h5 : b ∈ B) from nonempty_of_pos_numElts h3 h4
set B' : Set A := B \ {b}
have h6 : numElts B' n := remove_one_numElts h3 h5
obtain (c : A) (h7 : minimalElt R c B') from ih B' h6
by_cases h8 : R b c
· -- Case 1. h8 : R b c
have h9 : minimalElt R b B := Lemma_6_2_1_1 h h5 h7 h8
show ∃ (x : A), minimalElt R x B from Exists.intro b h9
done
· -- Case 2. h8 : ¬R b c
have h9 : minimalElt R c B := Lemma_6_2_1_2 h h5 h7 h8
show ∃ (x : A), minimalElt R x B from Exists.intro c h9
done
done
done
```
We'll consider one more theorem from Section 6.2 of *HTPI*. Example 6.2.2 proves that a partial order on a finite set can always be extended to a total order. Rather than give that proof, we are going to prove the more general theorem that is stated in Exercise 2 in Section 6.2 of *HTPI*. To explain the theorem in that exercise, it will be helpful to introduce a bit of terminology. Suppose `R` is a partial order on `A` and `b` has type `A`. We will say that `b` is *`R`-comparable to everything* if `∀ (x : A), R b x ∨ R x b`. If `B` is a set of objects of type `A`, we say that `B` is *`R`-comparable to everything* if every element of `B` is `R`-comparable to everything; that is, if `∀ b ∈ B, ∀ (x : A), R b x ∨ R x b`. Finally, we say that another binary relation `T` *extends* `R` if `∀ (x y : A), R x y → T x y`. We are going to prove that if `R` is a partial order on `A` and `B` is a finite set of objects of type `A`, then there is a partial order `T` that extends `R` such that `B` is `T`-comparable to everything. In other words, we are going to prove the following theorem:
```lean
theorem Exercise_6_2_2 {A : Type} (R : BinRel A) (h : partial_order R) :
∀ (n : Nat) (B : Set A), numElts B n → ∃ (T : BinRel A),
partial_order T ∧ (∀ (x y : A), R x y → T x y) ∧
∀ x ∈ B, ∀ (y : A), T x y ∨ T y x
```
In the exercises, we will ask you to show that this implies the theorem in Example 6.2.2.
It will be helpful to begin with a warm-up exercise. We'll show that a partial order can always be extended to make a single object comparable to everything. In other words, we'll show that if `R` is a partial order on `A` and `b` has type `A`, then we can define a partial order `T` extending `R` such that `b` is `T`-comparable to everything. To define `T`, we will need to make sure that for every `x` of type `A`, either `T b x` or `T x b`. If `R x b`, then since `T` must extend `R`, we must have `T x b`. If `¬R x b`, then we will define `T` so that `T b x`. But notice that if we follow this plan, then for any `x` and `y`, if we have `R x b` and `¬R y b`, then we will have `T x b` and `T b y`, and since `T` must be transitive, we must then have `T x y`. Summing up, if we have `R x y` then we must have `T x y`, and if we have `R x b` and `¬R y b` then we will also need to have `T x y`. So let's try defining `T x y` to mean `R x y ∨ (R x b ∧ ¬R y b)`.
It will be useful to have a name for this relation `T`. Since it is an extension of `R` determined by the element `b`, we will give it the name `extendPO R b`. Here is the definition of this relation:
```lean
def extendPO {A : Type} (R : BinRel A) (b : A) (x y : A) : Prop :=
R x y ∨ (R x b ∧ ¬R y b)
```
We need to prove a number of things about `extendPO R b`. First of all, we need to prove that it is a partial order. We'll leave most of the details as exercises for you:
```lean
lemma extendPO_is_ref {A : Type} (R : BinRel A) (b : A)
(h : partial_order R) : reflexive (extendPO R b) := sorry
lemma extendPO_is_trans {A : Type} (R : BinRel A) (b : A)
(h : partial_order R) : transitive (extendPO R b) := sorry
lemma extendPO_is_antisymm {A : Type} (R : BinRel A) (b : A)
(h : partial_order R) : antisymmetric (extendPO R b) := sorry
lemma extendPO_is_po {A : Type} (R : BinRel A) (b : A)
(h : partial_order R) : partial_order (extendPO R b) :=
And.intro (extendPO_is_ref R b h)
(And.intro (extendPO_is_trans R b h) (extendPO_is_antisymm R b h))
```
It is easy to prove that `extendPO R b` extends `R`:
```lean
lemma extendPO_extends {A : Type} (R : BinRel A) (b : A) (x y : A) :
R x y → extendPO R b x y := by
assume h1 : R x y
define
show R x y ∨ R x b ∧ ¬R y b from Or.inl h1
done
```
Finally, we verify that `extendPO R b` does what it was supposed to do: it makes `b` comparable with everything:
```lean
lemma extendPO_all_comp {A : Type} (R : BinRel A) (b : A)
(h : partial_order R) :
∀ (x : A), extendPO R b b x ∨ extendPO R b x b := by
have Rref : reflexive R := h.left
fix x : A
or_left with h1
define at h1 --h1 : ¬(R x b ∨ R x b ∧ ¬R b b)
demorgan at h1 --h1 : ¬R x b ∧ ¬(R x b ∧ ¬R b b)
define --Goal : R b x ∨ R b b ∧ ¬R x b
apply Or.inr
show R b b ∧ ¬R x b from And.intro (Rref b) h1.left
done
```
With this preparation, we can finally return to our theorem `Exercise_6_2_2`. We will prove it by mathematical induction. In the base case we must show that if `B` has 0 elements then we can extend `R` to make everything in `B` comparable to everything. Of course, no extension is necessary, since it is vacuously true that all elements of `B` are `R`-comparable to everything. For the induction step, after assuming the inductive hypothesis, we must prove that if `B` has `n + 1` elements then we can extend `R` to make all elements of `B` comparable to everything. As before, we choose `b ∈ B` and let `B' = B \ {b}`. By inductive hypothesis, we can find an extension `T'` of `R` that makes all elements of `B'` comparable to everything, so we just have to extend `T'` further to make `b` comparable to everything. But as we have just seen, we can do this with `extendPO T' b`.
```lean
theorem Exercise_6_2_2 {A : Type} (R : BinRel A) (h : partial_order R) :
∀ (n : Nat) (B : Set A), numElts B n → ∃ (T : BinRel A),
partial_order T ∧ (∀ (x y : A), R x y → T x y) ∧
∀ x ∈ B, ∀ (y : A), T x y ∨ T y x := by
by_induc
· -- Base Case
fix B : Set A
assume h2 : numElts B 0
rewrite [zero_elts_iff_empty] at h2
define at h2 --h2 : ¬∃ (x : A), x ∈ B
apply Exists.intro R
apply And.intro h
apply And.intro
· -- Proof that R extends R
fix x : A; fix y : A
assume h3 : R x y
show R x y from h3
done
· -- Proof that everything in B comparable to everything under R
fix x : A
assume h3 : x ∈ B
contradict h2
show ∃ (x : A), x ∈ B from Exists.intro x h3
done
done
· -- Induction Step
fix n : Nat
assume ih : ∀ (B : Set A), numElts B n → ∃ (T : BinRel A),
partial_order T ∧ (∀ (x y : A), R x y → T x y) ∧
∀ (x : A), x ∈ B → ∀ (y : A), T x y ∨ T y x
fix B : Set A
assume h2 : numElts B (n + 1)
have h3 : n + 1 > 0 := by linarith
obtain (b : A) (h4 : b ∈ B) from nonempty_of_pos_numElts h2 h3
set B' : Set A := B \ {b}
have h5 : numElts B' n := remove_one_numElts h2 h4
have h6 : ∃ (T : BinRel A), partial_order T ∧
(∀ (x y : A), R x y → T x y) ∧
∀ (x : A), x ∈ B' → ∀ (y : A), T x y ∨ T y x := ih B' h5
obtain (T' : BinRel A)
(h7 : partial_order T' ∧ (∀ (x y : A), R x y → T' x y) ∧
∀ (x : A), x ∈ B' → ∀ (y : A), T' x y ∨ T' y x) from h6
have T'po : partial_order T' := h7.left
have T'extR : ∀ (x y : A), R x y → T' x y := h7.right.left
have T'compB' : ∀ (x : A), x ∈ B' →
∀ (y : A), T' x y ∨ T' y x := h7.right.right
set T : BinRel A := extendPO T' b
apply Exists.intro T
apply And.intro (extendPO_is_po T' b T'po)
apply And.intro
· -- Proof that T extends R
fix x : A; fix y : A
assume h8 : R x y
have h9 : T' x y := T'extR x y h8
show T x y from (extendPO_extends T' b x y h9)
done
· -- Proof that everything in B comparable to everything under T
fix x : A
assume h8 : x ∈ B
by_cases h9 : x = b
· -- Case 1. h9 : x = b
rewrite [h9]
show ∀ (y : A), T b y ∨ T y b from extendPO_all_comp T' b T'po
done
· -- Case 2. h9 : x ≠ b
have h10 : x ∈ B' := And.intro h8 h9
fix y : A
have h11 : T' x y ∨ T' y x := T'compB' x h10 y
by_cases on h11
· -- Case 2.1. h11 : T' x y
show T x y ∨ T y x from
Or.inl (extendPO_extends T' b x y h11)
done
· -- Case 2.2. h11 : T' y x
show T x y ∨ T y x from
Or.inr (extendPO_extends T' b y x h11)
done
done
done
done
done
```
### Exercises
::: {.numex arguments="1"}
```lean
lemma Lemma_6_2_1_2 {A : Type} {R : BinRel A} {B : Set A} {b c : A}
(h1 : partial_order R) (h2 : b ∈ B) (h3 : minimalElt R c (B \ {b}))
(h4 : ¬R b c) : minimalElt R c B := sorry
```
:::
::: {.numex arguments="2"}
```lean
lemma extendPO_is_ref {A : Type} (R : BinRel A) (b : A)
(h : partial_order R) : reflexive (extendPO R b) := sorry
```
:::
::: {.numex arguments="3"}
```lean
lemma extendPO_is_trans {A : Type} (R : BinRel A) (b : A)
(h : partial_order R) : transitive (extendPO R b) := sorry
```
:::
::: {.numex arguments="4"}
```lean
lemma extendPO_is_antisymm {A : Type} (R : BinRel A) (b : A)
(h : partial_order R) : antisymmetric (extendPO R b) := sorry
```
:::
::: {.numex arguments="5"}
```lean
theorem Exercise_6_2_3 (A : Type) (R : BinRel A)
(h : total_order R) : ∀ n ≥ 1, ∀ (B : Set A),
numElts B n → ∃ (b : A), smallestElt R b B := sorry
```
:::
::: {.numex arguments="6"}
```lean
--Hint: First prove that R is reflexive.
theorem Exercise_6_2_4a {A : Type} (R : BinRel A)
(h : ∀ (x y : A), R x y ∨ R y x) : ∀ n ≥ 1, ∀ (B : Set A),
numElts B n → ∃ x ∈ B, ∀ y ∈ B, ∃ (z : A), R x z ∧ R z y := sorry
```
:::
::: {.numex arguments="7"}
```lean
theorem Like_Exercise_6_2_16 {A : Type} (f : A → A)
(h : one_to_one f) : ∀ (n : Nat) (B : Set A), numElts B n →
closed f B → ∀ y ∈ B, ∃ x ∈ B, f x = y := sorry
```
:::
::: {.numex arguments="8"}
```lean
--Hint: Use Exercise_6_2_2.
theorem Example_6_2_2 {A : Type} (R : BinRel A)
(h1 : ∃ (n : Nat), numElts {x : A | x = x} n)
(h2 : partial_order R) : ∃ (T : BinRel A),
total_order T ∧ ∀ (x y : A), R x y → T x y := sorry
```
:::
## 6.3. Recursion
In the last two sections, we saw that we can prove that all natural numbers have some property by proving that 0 has the property, and also that for every natural number $n$, if $n$ has the property then so does $n + 1$. In this section we will see that a similar idea can be used to define a function whose domain is the natural numbers. We can define a function $f$ with domain $\mathbb{N}$ by specifying the value of $f(0)$, and also saying how to compute $f(n+1)$ if you already know the value of $f(n)$.
For example, we can define a function $f : \mathbb{N} \to \mathbb{N}$ as follows:
::: {.quote}
$f(0) = 1$;<br>\newline
for every $n \in \mathbb{N}$, $f(n+1) = (n+1) \cdot f(n)$.
:::
Here is the same definition written in Lean. (For reasons that will become clear shortly, we have given the function the name `fact`.)
```lean
def fact (k : Nat) : Nat :=
match k with
| 0 => 1
| n + 1 => (n + 1) * fact n
```
Lean can use this definition to compute `fact k` for any natural number `k`. The `match` statement tells Lean to try to match the input `k` with one of the two patterns `0` and `n + 1`, and then to use the corresponding formula after `=>` to compute `fact k`. For example, if we ask Lean for `fact 4`, it first checks if `4` matches `0`. Since it doesn't, it goes on to the next line and determines that `4` matches the pattern `n + 1`, with `n = 3`, so it uses the formula `fact 4 = 4 * fact 3`. Of course, now it must compute `fact 3`, which it does in the same way: `3` matches `n + 1` with `n = 2`, so `fact 3 = 3 * fact 2`. Continuing in this way, Lean determines that
::: {.ind}
```
fact 4 = 4 * fact 3 = 4 * (3 * fact 2) = 4 * (3 * (2 * fact 1))
= 4 * (3 * (2 * (1 * fact 0))) = 4 * (3 * (2 * (1 * 1))) = 24.
```
:::
You can confirm this with the `#eval` command:
```lean
#eval fact 4 --Answer: 24
```
Of course, by now you have probably guessed why we used the name `fact` for his function: `fact k` is `k` factorial---the product of all the numbers from 1 to `k`.
This style of definition is called a *recursive* definition. If a function is defined by a recursive definition, then theorems about that function are often most easily proven by induction. For example, here is a theorem about the factorial function. It is Example 6.3.1 in *HTPI*, and we begin the Lean proof by imitating the proof in *HTPI*.
```lean
theorem ??Example_6_3_1:: : ∀ n ≥ 4, fact n > 2 ^ n := by
by_induc
· -- Base Case
decide
done
· -- Induction Step
fix n : Nat
assume h1 : n ≥ 4
assume ih : fact n > 2 ^ n
show fact (n + 1) > 2 ^ (n + 1) from
calc fact (n + 1)
_ = (n + 1) * fact n := by rfl
_ > (n + 1) * 2 ^ n := sorry
_ > 2 * 2 ^ n := sorry
_ = 2 ^ (n + 1) := by ring
done
done
```
There are two steps in the calculational proof at the end that require justification. The first says that `(n + 1) * fact n > (n + 1) * 2 ^ n`, which should follow from the inductive hypothesis `ih : fact n > 2 ^ n` by multiplying both sides by `n + 1`. Is there a theorem that would justify this inference?
This may remind you of a step in `Example_6_1_3` where we used the theorem `Nat.mul_le_mul_right`, which says `∀ {n m : ℕ} (k : ℕ), n ≤ m → n * k ≤ m * k`. Our situation in this example is similar, but it involves a strict inequality (`>` rather than `≥`) and it involves multiplying on the left rather than the right. Many theorems about inequalities in Lean's library contain either `le` (for "less than or equal to") or `lt` (for "less than") in their names, but they can also be used to prove statements involving `≥` or `>`. Perhaps the theorem we need is named something like `Nat.mul_lt_mul_left`. If you type `#check @Nat.mul_lt_mul_` into VS Code, a pop-up window will appear listing several theorems that begin with `Nat.mul_lt_mul_`. There is no `Nat.mul_lt_mul_left`, but there is a theorem called `Nat.mul_lt_mul_of_pos_left`, and its meaning is
::: {.ind}
```
@Nat.mul_lt_mul_of_pos_left : ∀ {n m k : ℕ},
n < m → k > 0 → k * n < k * m
```
:::
Lean has correctly reminded us that, to multiply both sides of a strict inequality by a number `k`, we need to know that `k > 0`. So in our case, we'll need to prove that `n + 1 > 0`. Once we have that, we can use the theorem `Nat.mul_lt_mul_of_pos_left` to eliminate the first `sorry`.
The second `sorry` is similar: `(n + 1) * 2 ^ n > 2 * 2 ^ n` should follow from `n + 1 > 2` and `2 ^ n > 0`, and you can verify that the theorem that will justify this inference is `Nat.mul_lt_mul_of_pos_right`.
So we have three inequalities that we need to prove before we can justify the steps of the calculational proof: `n + 1 > 0`, `n + 1 > 2`, and `2 ^ n > 0`. We'll insert `have` steps before the calculational proof to assert these three inequalities. If you try it, you'll find that `linarith` can prove the first two, but not the third.
How can we prove `2 ^ n > 0`? It is often helpful to think about whether there is a general principle that is behind a statement we are trying to prove. In our case, the inequality `2 ^ n > 0` is an instance of the general fact that if `m` and `n` are any natural numbers with `m > 0`, then `m ^ n > 0`. Maybe that fact is in Lean's library:
```lean
example (m n : Nat) (h : m > 0) : m ^ n > 0 := by ++apply?::
```
The `apply?` tactic comes up with `exact Nat.pos_pow_of_pos n h`, and `#check @Nat.pos_pow_of_pos` gives the result
::: {.ind}
```
@Nat.pos_pow_of_pos : ∀ {n : ℕ} (m : ℕ), 0 < n → 0 < n ^ m
```
:::
That means that we can use `Nat.pos_pow_of_pos` to prove `2 ^ n > 0`, but first we'll need to prove that `2 > 0`. We now have all the pieces we need; putting them together leads to this proof:
```lean
theorem Example_6_3_1 : ∀ n ≥ 4, fact n > 2 ^ n := by
by_induc
· -- Base Case
decide
done
· -- Induction Step
fix n : Nat
assume h1 : n ≥ 4
assume ih : fact n > 2 ^ n
have h2 : n + 1 > 0 := by linarith
have h3 : n + 1 > 2 := by linarith
have h4 : 2 > 0 := by linarith
have h5 : 2 ^ n > 0 := Nat.pos_pow_of_pos n h4
show fact (n + 1) > 2 ^ (n + 1) from
calc fact (n + 1)
_ = (n + 1) * fact n := by rfl
_ > (n + 1) * 2 ^ n := Nat.mul_lt_mul_of_pos_left ih h2
_ > 2 * 2 ^ n := Nat.mul_lt_mul_of_pos_right h3 h5
_ = 2 ^ (n + 1) := by ring
done
done
```
But there is an easier way. Look at the two ">" steps in the calculational proof at the end of `Example_6_3_1`. In both cases, we took a known relationship between two quantities and did something to both sides that preserved the relationship. In the first case, the known relationship was `ih : fact n > 2 ^ n`, and we multiplied both sides by `n + 1` on the left; in the second, the known relationship was `h3 : n + 1 > 2`, and we multiplied both sides by `2 ^ n` on the right. To justify these steps, we had to find the right theorems in Lean's library, and we ended up needing auxiliary positivity facts: `h2 : n + 1 > 0` in the first case and `h5 : 2 ^ n > 0` in the second. There is a tactic that can simplify these steps: if `h` is a proof of a statement asserting a relationship between two quantities, then the tactic `rel [h]` will attempt to prove any statement obtained from that relationship by applying the same operation to both sides. The tactic will try to find a theorem in Lean's library that says that the operation preserves the relationship, and if the theorem requires auxiliary positivity facts, it will try to prove those facts as well. The `rel` tactic doesn't always succeed, but when it does, it saves you the trouble of searching through the library for the necessary theorems. In this case, the tactic allows us to give a much simpler proof of `Example_6_3_1`:
```lean
theorem Example_6_3_1 : ∀ n ≥ 4, fact n > 2 ^ n := by
by_induc
· -- Base Case
decide
done
· -- Induction Step
fix n : Nat
assume h1 : n ≥ 4
assume ih : fact n > 2 ^ n
have h2 : n + 1 > 2 := by linarith
show fact (n + 1) > 2 ^ (n + 1) from
calc fact (n + 1)
_ = (n + 1) * fact n := by rfl
_ > (n + 1) * 2 ^ n := by rel [ih]
_ > 2 * 2 ^ n := by rel [h2]
_ = 2 ^ (n + 1) := by ring
done
done
```
The next example in *HTPI* is a proof of one of the laws of exponents: `a ^ (m + n) = a ^ m * a ^ n`. Lean's definition of exponentiation with natural number exponents is recursive. The definitions Lean uses are essentially as follows:
```lean
--For natural numbers b and k, b ^ k = nat_pow b k:
def nat_pow (b k : Nat) : Nat :=
match k with
| 0 => 1
| n + 1 => (nat_pow b n) * b
--For a real number b and a natural number k, b ^ k = real_pow b k:
def real_pow (b : Real) (k : Nat) : Real :=
match k with
| 0 => 1
| n + 1 => (real_pow b n) * b
```
Let's prove the addition law for exponents:
```lean
theorem Example_6_3_2_cheating : ∀ (a : Real) (m n : Nat),
a ^ (m + n) = a ^ m * a ^ n := by
fix a : Real; fix m : Nat; fix n : Nat
ring
done
```
Well, that wasn't really fair. The `ring` tactic knows the laws of exponents, so it has no trouble proving this theorem. But we want to know *why* the law holds, so let's see if we can prove it without using `ring`. The following proof is essentially the same as the proof in *HTPI*:
```lean
theorem Example_6_3_2 : ∀ (a : Real) (m n : Nat),
a ^ (m + n) = a ^ m * a ^ n := by
fix a : Real; fix m : Nat
--Goal : ∀ (n : Nat), a ^ (m + n) = a ^ m * a ^ n
by_induc
· -- Base Case
show a ^ (m + 0) = a ^ m * a ^ 0 from
calc a ^ (m + 0)
_ = a ^ m := by rfl
_ = a ^ m * 1 := (mul_one (a ^ m)).symm
_ = a ^ m * a ^ 0 := by rfl
done
· -- Induction Step
fix n : Nat
assume ih : a ^ (m + n) = a ^ m * a ^ n
show a ^ (m + (n + 1)) = a ^ m * a ^ (n + 1) from
calc a ^ (m + (n + 1))
_ = a ^ ((m + n) + 1) := by rw [add_assoc]
_ = a ^ (m + n) * a := by rfl
_ = (a ^ m * a ^ n) * a := by rw [ih]
_ = a ^ m * (a ^ n * a) := by rw [mul_assoc]
_ = a ^ m * (a ^ (n + 1)) := by rfl