-
Notifications
You must be signed in to change notification settings - Fork 1
/
Induction.v
1185 lines (1016 loc) · 35.2 KB
/
Induction.v
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
(** * Induction: Proof by Induction *)
(** The next line imports all of our definitions from the
previous chapter. *)
Require Export Basics.
(** For it to work, you need to use [coqc] to compile [Basics.v]
into [Basics.vo]. (This is like making a .class file from a .java
file, or a .o file from a .c file.)
Here are two ways to compile your code:
- CoqIDE:
Open [Basics.v].
In the "Compile" menu, click on "Compile Buffer".
- Command line:
Run [coqc Basics.v]
*)
(* ###################################################################### *)
(** * Naming Cases *)
(** The fact that there is no explicit command for moving from
one branch of a case analysis to the next can make proof scripts
rather hard to read. In larger proofs, with nested case analyses,
it can even become hard to stay oriented when you're sitting with
Coq and stepping through the proof. (Imagine trying to remember
that the first five subgoals belong to the inner case analysis and
the remaining seven cases are what remains of the outer one...)
Disciplined use of indentation and comments can help, but a better
way is to use the [Case] tactic. *)
(** [Case] is not built into Coq: we need to define it ourselves.
There is no need to understand how it works -- you can just skip
over the definition to the example that follows. It uses some
facilities of Coq that we have not discussed -- the string
library (just for the concrete syntax of quoted strings) and the
[Ltac] command, which allows us to declare custom tactics. Kudos
to Aaron Bohannon for this nice hack! *)
Require String. Open Scope string_scope.
Ltac move_to_top x :=
match reverse goal with
| H : _ |- _ => try move x after H
end.
Tactic Notation "assert_eq" ident(x) constr(v) :=
let H := fresh in
assert (x = v) as H by reflexivity;
clear H.
Tactic Notation "Case_aux" ident(x) constr(name) :=
first [
set (x := name); move_to_top x
| assert_eq x name; move_to_top x
| fail 1 "because we are working on a different case" ].
Tactic Notation "Case" constr(name) := Case_aux Case name.
Tactic Notation "SCase" constr(name) := Case_aux SCase name.
Tactic Notation "SSCase" constr(name) := Case_aux SSCase name.
Tactic Notation "SSSCase" constr(name) := Case_aux SSSCase name.
Tactic Notation "SSSSCase" constr(name) := Case_aux SSSSCase name.
Tactic Notation "SSSSSCase" constr(name) := Case_aux SSSSSCase name.
Tactic Notation "SSSSSSCase" constr(name) := Case_aux SSSSSSCase name.
Tactic Notation "SSSSSSSCase" constr(name) := Case_aux SSSSSSSCase name.
(** Here's an example of how [Case] is used. Step through the
following proof and observe how the context changes. *)
Theorem andb_true_elim1 : forall b c : bool,
andb b c = true -> b = true.
Proof.
intros b c H.
destruct b.
Case "b = true". (* <----- here *)
reflexivity.
Case "b = false". (* <---- and here *)
rewrite <- H.
reflexivity.
Qed.
(** [Case] does something very straightforward: It simply adds a
string that we choose (tagged with the identifier "Case") to the
context for the current goal. When subgoals are generated, this
string is carried over into their contexts. When the last of
these subgoals is finally proved and the next top-level goal
becomes active, this string will no longer appear in the context
and we will be able to see that the case where we introduced it is
complete. Also, as a sanity check, if we try to execute a new
[Case] tactic while the string left by the previous one is still
in the context, we get a nice clear error message.
For nested case analyses (e.g., when we want to use a [destruct]
to solve a goal that has itself been generated by a [destruct]),
there is an [SCase] ("subcase") tactic. *)
(** **** Exercise: 2 stars (andb_true_elim2) *)
(** Prove [andb_true_elim2], marking cases (and subcases) when
you use [destruct]. *)
Theorem andb_true_elim2 : forall b c : bool,
andb b c = true -> c = true.
Proof.
intros b c.
destruct b.
Case "b is true".
simpl.
intros H.
rewrite -> H.
reflexivity.
Case "b is false".
simpl.
intros H'.
destruct c.
SCase "c is true".
reflexivity.
SCase "c is false".
rewrite -> H'.
reflexivity.
Qed.
(** [] *)
(** There are no hard and fast rules for how proofs should be
formatted in Coq -- in particular, where lines should be broken
and how sections of the proof should be indented to indicate their
nested structure. However, if the places where multiple subgoals
are generated are marked with explicit [Case] tactics placed at
the beginning of lines, then the proof will be readable almost no
matter what choices are made about other aspects of layout.
This is a good place to mention one other piece of (possibly
obvious) advice about line lengths. Beginning Coq users sometimes
tend to the extremes, either writing each tactic on its own line
or entire proofs on one line. Good style lies somewhere in the
middle. In particular, one reasonable convention is to limit
yourself to 80-character lines. Lines longer than this are hard
to read and can be inconvenient to display and print. Many
editors have features that help enforce this. *)
(* ###################################################################### *)
(** * Proof by Induction *)
(** We proved in the last chapter that [0] is a neutral element
for [+] on the left using a simple argument. The fact that it is
also a neutral element on the _right_... *)
Theorem plus_0_r_firsttry : forall n:nat,
n + 0 = n.
(** ... cannot be proved in the same simple way. Just applying
[reflexivity] doesn't work: the [n] in [n + 0] is an arbitrary
unknown number, so the [match] in the definition of [+] can't be
simplified. *)
Proof.
intros n.
simpl. (* Does nothing! *)
Abort.
(** *** *)
(** And reasoning by cases using [destruct n] doesn't get us much
further: the branch of the case analysis where we assume [n = 0]
goes through, but in the branch where [n = S n'] for some [n'] we
get stuck in exactly the same way. We could use [destruct n'] to
get one step further, but since [n] can be arbitrarily large, if we
try to keep on like this we'll never be done. *)
Theorem plus_0_r_secondtry : forall n:nat,
n + 0 = n.
Proof.
intros n. destruct n as [| n'].
Case "n = 0".
reflexivity. (* so far so good... *)
Case "n = S n'".
simpl. (* ...but here we are stuck again *)
Abort.
(** *** *)
(** To prove such facts -- indeed, to prove most interesting
facts about numbers, lists, and other inductively defined sets --
we need a more powerful reasoning principle: _induction_.
Recall (from high school) the principle of induction over natural
numbers: If [P(n)] is some proposition involving a natural number
[n] and we want to show that P holds for _all_ numbers [n], we can
reason like this:
- show that [P(O)] holds;
- show that, for any [n'], if [P(n')] holds, then so does
[P(S n')];
- conclude that [P(n)] holds for all [n].
In Coq, the steps are the same but the order is backwards: we
begin with the goal of proving [P(n)] for all [n] and break it
down (by applying the [induction] tactic) into two separate
subgoals: first showing [P(O)] and then showing [P(n') -> P(S
n')]. Here's how this works for the theorem we are trying to
prove at the moment: *)
(** *** *)
Theorem plus_0_r : forall n:nat, n + 0 = n.
Proof.
intros n. induction n as [| n'].
Case "n = 0". reflexivity.
Case "n = S n'". simpl. rewrite -> IHn'. reflexivity. Qed.
(** Like [destruct], the [induction] tactic takes an [as...]
clause that specifies the names of the variables to be introduced
in the subgoals. In the first branch, [n] is replaced by [0] and
the goal becomes [0 + 0 = 0], which follows by simplification. In
the second, [n] is replaced by [S n'] and the assumption [n' + 0 =
n'] is added to the context (with the name [IHn'], i.e., the
Induction Hypothesis for [n']). The goal in this case becomes [(S
n') + 0 = S n'], which simplifies to [S (n' + 0) = S n'], which in
turn follows from the induction hypothesis. *)
Theorem minus_diag : forall n,
minus n n = 0.
Proof.
(* WORKED IN CLASS *)
intros n. induction n as [| n'].
Case "n = 0".
simpl. reflexivity.
Case "n = S n'".
simpl. rewrite -> IHn'. reflexivity. Qed.
(** **** Exercise: 2 stars (basic_induction) *)
(** Prove the following lemmas using induction. You might need
previously proven results. *)
Theorem mult_0_r : forall n:nat,
n * 0 = 0.
Proof.
intros IHn.
induction IHn as [|n].
Case "n is 0".
reflexivity.
Case "n under IH n-1".
Check IHn.
simpl.
rewrite -> IHn.
reflexivity.
Qed.
Theorem plus_n_Sm : forall n m : nat,
S (n + m) = n + (S m).
Proof.
intros n m.
induction n as [|n'].
Case "n is 0".
reflexivity.
Case "n under IH of n-1".
simpl.
rewrite -> IHn'.
reflexivity.
Qed.
Theorem plus_comm : forall n m : nat,
n + m = m + n.
Proof.
intros n m.
induction n as [|n'].
Case "n is 0".
rewrite -> (plus_0_r m).
reflexivity.
Case "n under IH n-1".
rewrite <- (plus_n_Sm m n').
simpl.
rewrite -> IHn'.
reflexivity.
Qed.
Theorem plus_assoc : forall n m p : nat,
n + (m + p) = (n + m) + p.
Proof.
intros n m p.
induction m as [|m'].
Case "m is 0".
simpl.
rewrite -> (plus_comm n 0).
reflexivity.
Case "m under IH of m-1".
rewrite -> (plus_comm n (S m' + p)).
rewrite <- (plus_n_Sm n m').
simpl.
rewrite -> (plus_comm (m' + p) n).
rewrite -> IHm'.
reflexivity.
Qed.
(** [] *)
(** **** Exercise: 2 stars (double_plus) *)
(** Consider the following function, which doubles its argument: *)
Fixpoint double (n:nat) :=
match n with
| O => O
| S n' => S (S (double n'))
end.
(** Use induction to prove this simple fact about [double]: *)
Lemma double_plus : forall n, double n = n + n .
Proof.
induction n as [|n'].
Case "n is 0".
reflexivity.
Case "n under IH of n-1".
simpl.
rewrite -> IHn'.
rewrite <- (plus_n_Sm n' n').
reflexivity.
Qed.
(** **** Exercise: 1 star (destruct_induction) *)
(** Briefly explain the difference between the tactics
[destruct] and [induction]. *)
(**
INDUCTION
http://coq.inria.fr/distrib/current/refman/Reference-Manual010.html#hevea_tactic71
This tactic applies to any goal. The argument term must be of inductive type and the tactic induction generates subgoals, one for each possible form of term, i.e. one for each constructor of the inductive type.
If the argument is dependent in either the conclusion or some hypotheses of the goal, the argument is replaced by the appropriate constructor form in each of the resulting subgoals and induction hypotheses are added to the local context using names whose prefix is IH.
There are particular cases:
If term is an identifier ident denoting a quantified variable of the conclusion of the goal, then induction ident behaves as intros until ident; induction ident.
If term is a num, then induction num behaves as intros until num followed by induction applied to the last introduced hypothesis. Remark: For simple induction on a numeral, use syntax induction (num) (not very interesting anyway).
The argument term can also be a pattern of which holes are denoted by “_”. In this case, the tactic checks that all subterms matching the pattern in the conclusion and the hypotheses are compatible and performs induction using this subterm.
DESTRUCT
This tactic applies to any goal. The argument term must be of inductive or co-inductive type and the tactic generates subgoals, one for each possible form of term, i.e. one for each constructor of the inductive or co-inductive type. Unlike induction, no induction hypothesis is generated by destruct.
If the argument is dependent in either the conclusion or some hypotheses of the goal, the argument is replaced by the appropriate constructor form in each of the resulting subgoals, thus performing case analysis. If non-dependent, the tactic simply exposes the inductive or co-inductive structure of the argument. *)
(* ###################################################################### *)
(** * Proofs Within Proofs *)
(** In Coq, as in informal mathematics, large proofs are very
often broken into a sequence of theorems, with later proofs
referring to earlier theorems. Occasionally, however, a proof
will need some miscellaneous fact that is too trivial (and of too
little general interest) to bother giving it its own top-level
name. In such cases, it is convenient to be able to simply state
and prove the needed "sub-theorem" right at the point where it is
used. The [assert] tactic allows us to do this. For example, our
earlier proof of the [mult_0_plus] theorem referred to a previous
theorem named [plus_O_n]. We can also use [assert] to state and
prove [plus_O_n] in-line: *)
Theorem mult_0_plus' : forall n m : nat,
(0 + n) * m = n * m.
Proof.
intros n m.
assert (H: 0 + n = n). (** forall n : nat, ~~ *)
Case "Proof of assertion". reflexivity.
rewrite -> H.
reflexivity. Qed.
Theorem mult_1_plus : forall n m : nat,
(1 + n) * m = n * m + m.
Proof.
simpl.
intros n m.
rewrite -> (plus_comm m (n * m)).
reflexivity.
Qed.
Theorem mult_l_0 : forall n : nat,
n * 0 = 0.
induction n as [|n'].
Case "n = 0".
simpl.
reflexivity.
Case "n under IH of n-1".
simpl.
rewrite -> IHn'.
reflexivity.
Qed.
Theorem mult_l_1 : forall n : nat,
n * 1 = n.
induction n as [|n'].
Case "n = 0".
simpl.
reflexivity.
Case "n under IH of n-1".
simpl.
rewrite -> IHn'.
reflexivity.
Qed.
(** The [assert] tactic introduces two sub-goals. The first is
the assertion itself; by prefixing it with [H:] we name the
assertion [H]. (Note that we could also name the assertion with
[as] just as we did above with [destruct] and [induction], i.e.,
[assert (0 + n = n) as H]. Also note that we mark the proof of
this assertion with a [Case], both for readability and so that,
when using Coq interactively, we can see when we're finished
proving the assertion by observing when the ["Proof of assertion"]
string disappears from the context.) The second goal is the same
as the one at the point where we invoke [assert], except that, in
the context, we have the assumption [H] that [0 + n = n]. That
is, [assert] generates one subgoal where we must prove the
asserted fact and a second subgoal where we can use the asserted
fact to make progress on whatever we were trying to prove in the
first place. *)
(** Actually, [assert] will turn out to be handy in many sorts of
situations. For example, suppose we want to prove that [(n + m)
+ (p + q) = (m + n) + (p + q)]. The only difference between the
two sides of the [=] is that the arguments [m] and [n] to the
first inner [+] are swapped, so it seems we should be able to
use the commutativity of addition ([plus_comm]) to rewrite one
into the other. However, the [rewrite] tactic is a little stupid
about _where_ it applies the rewrite. There are three uses of
[+] here, and it turns out that doing [rewrite -> plus_comm]
will affect only the _outer_ one. *)
Theorem plus_rearrange_firsttry : forall n m p q : nat,
(n + m) + (p + q) = (m + n) + (p + q).
Proof.
intros n m p q.
(* We just need to swap (n + m) for (m + n)...
it seems like plus_comm should do the trick! *)
rewrite -> (plus_comm n m).
(* Doesn't work...Coq rewrote the wrong plus! *)
reflexivity.
Qed.
(** To get [plus_comm] to apply at the point where we want it, we can
introduce a local lemma stating that [n + m = m + n] (for
the particular [m] and [n] that we are talking about here), prove
this lemma using [plus_comm], and then use this lemma to do the
desired rewrite. *)
Theorem plus_rearrange : forall n m p q : nat,
(n + m) + (p + q) = (m + n) + (p + q).
Proof.
intros n m p q.
assert (H: n + m = m + n).
Case "Proof of assertion".
rewrite -> plus_comm. reflexivity.
rewrite -> H. reflexivity. Qed.
(** **** Exercise: 4 stars (mult_comm) *)
(** Use [assert] to help prove this theorem. You shouldn't need to
use induction. *)
Theorem plus_swap : forall n m p : nat,
n + (m + p) = m + (n + p).
Proof.
intros n m p.
rewrite -> (plus_assoc).
rewrite -> (plus_assoc).
(** rewrite -> (plus_comm n m). *)
assert (H : n + m = m + n).
Case "assertion".
rewrite -> plus_comm.
reflexivity.
rewrite -> H.
reflexivity.
Qed.
Theorem mult_plus_1 : forall n m : nat,
n * (1 + m) = n * m + n.
Proof.
simpl.
intros n m.
induction n as [|n'].
Case "0".
simpl.
reflexivity.
Case "n under IH of n-1".
simpl.
rewrite -> IHn'.
rewrite -> plus_n_Sm.
rewrite -> plus_n_Sm.
rewrite -> plus_assoc.
reflexivity.
Qed.
(** Now prove commutativity of multiplication. (You will probably
need to define and prove a separate subsidiary theorem to be used
in the proof of this one.) You may find that [plus_swap] comes in
handy. *)
Theorem mult_comm : forall m n : nat,
m * n = n * m.
Proof.
intros n m.
induction n as [|n'].
Case "n is 0".
simpl.
rewrite mult_0_r.
reflexivity.
Case "n under IH of n-1".
simpl.
rewrite -> IHn'.
rewrite -> mult_plus_1.
rewrite -> plus_comm.
reflexivity.
Qed.
(** **** Exercise: 2 stars, optional (evenb_n__oddb_Sn) *)
(** Prove the following simple fact: *)
Theorem evenb_n__oddb_Sn : forall n : nat,
evenb n = negb (evenb (S n)).
Proof.
simpl.
induction n as [|n'].
Case "n = 0".
reflexivity.
Case "n under IH of n-1".
rewrite -> IHn'.
assert(H : evenb(S n') = negb(evenb(n'))).
SCase "assertion".
simpl.
rewrite -> IHn'.
rewrite -> negb_involutive.
reflexivity.
rewrite -> H.
rewrite -> IHn'.
reflexivity.
Qed.
(* ###################################################################### *)
(** * More Exercises *)
(** **** Exercise: 3 stars, optional (more_exercises) *)
(** Take a piece of paper. For each of the following theorems, first
_think_ about whether (a) it can be proved using only
simplification and rewriting, (b) it also requires case
analysis ([destruct]), or (c) it also requires induction. Write
down your prediction. Then fill in the proof. (There is no need
to turn in your piece of paper; this is just to encourage you to
reflect before hacking!) *)
Theorem ble_nat_refl : forall n:nat,
true = ble_nat n n.
Proof.
induction n as [|n'].
Case "0".
reflexivity.
Case "n under IHn'".
simpl.
rewrite -> IHn'.
reflexivity.
Qed.
Theorem zero_nbeq_S : forall n:nat,
beq_nat 0 (S n) = false.
Proof.
simpl.
reflexivity.
Qed.
Theorem andb_false_r : forall b : bool,
andb b false = false.
Proof.
simpl.
destruct b as [|b'].
reflexivity.
reflexivity.
Qed.
Theorem plus_ble_compat_l : forall n m p : nat,
ble_nat n m = true -> ble_nat (p + n) (p + m) = true.
Proof.
induction p as [|p'].
simpl.
intros H.
rewrite -> H.
reflexivity.
simpl.
intros a.
rewrite <- IHp'.
reflexivity.
rewrite -> a.
reflexivity.
Qed.
Theorem S_nbeq_0 : forall n:nat,
beq_nat (S n) 0 = false.
Proof.
simpl.
reflexivity.
Qed.
Theorem mult_1_l : forall n:nat, 1 * n = n.
Proof.
induction n as [|n'].
reflexivity.
simpl.
rewrite -> plus_n_Sm.
rewrite -> plus_comm.
reflexivity.
Qed.
Theorem all3_spec : forall b c : bool,
orb
(andb b c)
(orb (negb b)
(negb c))
= true.
Proof.
intros a b.
destruct a as [|].
Case "a true".
simpl.
assert (H : orb b (negb b) = true).
SCase "assertion".
destruct b as [|].
reflexivity.
reflexivity.
rewrite -> H.
reflexivity.
Case "a false".
simpl.
reflexivity.
Qed.
Theorem mult_plus_distr_r : forall n m p : nat,
(n + m) * p = (n * p) + (m * p).
Proof.
intros n m p.
rewrite -> (mult_comm (n + m) p).
rewrite -> (mult_comm n p).
rewrite -> (mult_comm m p).
induction p as [|p'].
Case "p = 0".
simpl.
reflexivity.
Case "p under IHp'".
simpl.
rewrite -> IHp'.
assert(H : forall a b c d : nat, a + b + (c + d) = a + c + (b + d)).
SCase "assertion".
intros a b c d.
rewrite -> plus_assoc.
rewrite -> plus_assoc.
rewrite -> plus_comm.
rewrite -> (plus_comm (a + c + b) d).
rewrite <- (plus_assoc a b c).
rewrite <- (plus_assoc a c b).
rewrite -> (plus_comm b c).
reflexivity.
rewrite -> H.
reflexivity.
Qed.
Theorem mult_assoc : forall n m p : nat,
n * (m * p) = (n * m) * p.
Proof.
induction n as [|n'].
Case "n = 0".
simpl.
reflexivity.
Case "n under IHn'".
simpl.
intros m p.
rewrite -> IHn'.
rewrite -> mult_plus_distr_r.
reflexivity.
Qed.
(** **** Exercise: 2 stars, optional (beq_nat_refl) *)
(** Prove the following theorem. Putting [true] on the left-hand side
of the equality may seem odd, but this is how the theorem is stated in
the standard library, so we follow suit. Since rewriting
works equally well in either direction, we will have no
problem using the theorem no matter which way we state it. *)
Theorem beq_nat_refl : forall n : nat,
true = beq_nat n n.
Proof.
intros n.
induction n as [|n'].
reflexivity.
simpl.
rewrite -> IHn'.
reflexivity.
Qed.
(** **** Exercise: 2 stars, optional (plus_swap') *)
(** The [replace] tactic allows you to specify a particular subterm to
rewrite and what you want it rewritten to. More precisely,
[replace (t) with (u)] replaces (all copies of) expression [t] in
the goal by expression [u], and generates [t = u] as an additional
subgoal. This is often useful when a plain [rewrite] acts on the wrong
part of the goal.
Use the [replace] tactic to do a proof of [plus_swap'], just like
[plus_swap] but without needing [assert (n + m = m + n)].
*)
Theorem plus_swap' : forall n m p : nat,
n + (m + p) = m + (n + p).
Proof.
intros n m p.
rewrite -> (plus_assoc).
rewrite -> (plus_assoc).
(** rewrite -> (plus_comm n m). *)
replace (n+m) with (m+n).
reflexivity.
rewrite -> plus_comm.
reflexivity.
Qed.
(** **** Exercise: 3 stars (binary_commute) *)
(** Recall the [increment] and [binary-to-unary] functions that you
wrote for the [binary] exercise in the [Basics] chapter. Prove
that these functions commute -- that is, incrementing a binary
number and then converting it to unary yields the same result as
first converting it to unary and then incrementing.
(Before you start working on this exercise, please copy the
definitions from your solution to the [binary] exercise here so
that this file can be graded on its own. If you find yourself
wanting to change your original definitions to make the property
easier to prove, feel free to do so.) *)
Check bin.
Check incr.
Check to_unary.
Theorem binary_commute : forall n : bin, to_unary(incr(n)) = 1+(to_unary(n)).
Proof.
induction n as[|n0|n1].
Case "n = 0".
simpl.
reflexivity.
Case "n under IHn0, which means n = xxx0".
simpl.
rewrite -> plus_0_r.
rewrite -> plus_n_Sm.
rewrite <- (plus_assoc (to_unary n0) (to_unary n0) 1).
rewrite -> plus_comm.
rewrite <- plus_assoc.
rewrite -> (plus_1_l (to_unary n0)).
reflexivity.
Case "n under IHn1, which means n = xxx1".
simpl.
rewrite -> plus_0_r.
rewrite -> plus_0_r.
rewrite -> IHn1.
simpl.
rewrite <- plus_assoc.
rewrite -> (plus_comm (to_unary n1) 1).
rewrite -> (plus_1_l (to_unary n1)).
reflexivity.
Qed.
(** **** Exercise: 5 stars, advanced (binary_inverse) *)
(** This exercise is a continuation of the previous exercise about
binary numbers. You will need your definitions and theorems from
the previous exercise to complete this one.
(a) First, write a function to convert natural numbers to binary
numbers. Then prove that starting with any natural number,
converting to binary, then converting back yields the same
natural number you started with.
(b) You might naturally think that we should also prove the
opposite direction: that starting with a binary number,
converting to a natural, and then back to binary yields the
same number we started with. However, it is not true!
Explain what the problem is.
(c) Define a function [normalize] from binary numbers to binary
numbers such that for any binary number b, converting to a
natural and then back to binary yields [(normalize b)]. Prove
it.
Again, feel free to change your earlier definitions if this helps
here.
*)
Fixpoint to_binary (n : nat) : bin :=
match n with
| 0 => O'
| S n' => incr(to_binary(n'))
end.
Lemma conversion_plus_1 :
forall n : bin, to_unary(incr(n)) = to_unary(n) + 1.
Proof.
induction n as [|n0|n1].
Case "0".
simpl.
reflexivity.
Case "n0".
simpl.
rewrite -> plus_0_r.
reflexivity.
Case "n1".
simpl.
rewrite -> plus_0_r.
rewrite -> plus_0_r.
rewrite -> IHn1.
rewrite -> plus_assoc.
rewrite -> plus_comm.
rewrite -> (plus_comm ((to_unary n1) + (to_unary n1) + 1) 1).
rewrite <- (plus_assoc (to_unary n1) 1 (to_unary n1)).
rewrite <- (plus_assoc (to_unary n1) (to_unary n1) 1).
replace (1 + to_unary n1) with (to_unary n1 + 1).
reflexivity.
rewrite -> plus_comm.
reflexivity.
Qed.
Theorem conversion_involutive :
forall n : nat, to_unary(to_binary(n)) = n.
Proof.
induction n as [|n'].
Case "0".
simpl.
reflexivity.
Case "IHn'".
simpl.
destruct (to_binary n') as [|n'0|n'1].
SCase "0".
rewrite <- IHn'.
simpl. reflexivity.
SCase "n'0".
rewrite <- IHn'.
simpl.
rewrite -> plus_0_r.
rewrite <- (plus_1_l ((to_unary n'0) + (to_unary n'0))).
rewrite -> (plus_comm ((to_unary n'0) + (to_unary n'0)) 1).
reflexivity.
SCase "n'1".
rewrite <- IHn'.
simpl.
rewrite -> plus_0_r.
rewrite -> plus_0_r.
rewrite -> plus_n_Sm.
replace (to_unary(incr n'1)) with (to_unary(n'1) + 1).
rewrite <- (plus_1_l 1).
rewrite -> plus_assoc.
rewrite -> plus_assoc.
rewrite -> plus_comm.
rewrite -> (plus_comm ((to_unary n'1) + (to_unary n'1) + 1) 1).
rewrite <- plus_assoc.
rewrite <- plus_assoc.
rewrite -> (plus_comm 1 (to_unary n'1)).
reflexivity.
rewrite -> conversion_plus_1.
reflexivity.
Qed.
(** (b)
binary A*O is all maped to 0, not injective. *)
Fixpoint reverse (n m : bin) : bin :=
match n with
| O' => m
| A n' => (reverse n' (A m))
| B n' => ((reverse n') (B m))
end.
Eval compute in to_unary(A(B(A(O')))).
Eval compute in to_unary(B(B(B(O')))).
Eval compute in reverse (B(B(B(B(O'))))) O'.
Eval compute in reverse (B(B(B(A(O'))))) O'.
Eval compute in reverse (B(B(A(B(O'))))) O'.
Eval compute in reverse (B(A(B(B(O'))))) O'.
Eval compute in reverse (A(B(B(B(O'))))) O'.
Eval compute in reverse (B(B(A(A(O'))))) O'.
Eval compute in reverse (B(A(B(A(O'))))) O'.
Eval compute in reverse (A(B(B(A(O'))))) O'.
Eval compute in reverse (B(A(A(B(O'))))) O'.
Eval compute in reverse (A(B(A(B(O'))))) O'.
Eval compute in reverse (A(A(B(B(O'))))) O'.
Fixpoint reverse_depth (d : nat) (n m : bin) : bin :=
match d with
| 0 => O'
| S d' =>
match n with
| O' => m
| A n' => (reverse_depth (d-1) n' (A m))
| B n' => (reverse_depth (d-1) n') (B m)
end
end.
Eval compute in (reverse_depth 5 (B(B(A(A(O'))))) O').
Theorem reverse_involutive :
forall n : bin, (reverse (reverse n O') O') = n.
Proof.
intros n.
induction n as [|n0|n1].
Case "0".
simpl.
reflexivity.
Case "n0".
simpl.
Abort.
Fixpoint len (n : bin) :=
match n with
| O' => 1
| A n' => 1 + len(n')
| B n' => 1 + len(n')
end.
Eval compute in len(to_binary(8)).
Eval compute in len(to_binary(7)).
Eval compute in len(to_binary(6)).
Eval compute in len(to_binary(5)).
Eval compute in len(to_binary(4)).
Eval compute in len(to_binary(3)).
Eval compute in len(to_binary(2)).
Eval compute in len(to_binary(1)).
Eval compute in len(to_binary(0)).
Theorem reverse_depth_0 :
forall n : bin, (reverse_depth 0 n O') = O'.
Proof.
destruct n as [|n0|n1].
reflexivity.
simpl.
reflexivity.
simpl.
reflexivity.
Qed.
Theorem reverse_depth_involutive :
forall n : bin, (reverse_depth (len n) (reverse_depth (len n) n O') O') = n.
Proof.
intros n.
induction (len n) as [|l'].
simpl.
rewrite -> reverse_depth_0.
Abort.
(**
A*B[A|B]*O -> B[A|B]*O
A*O -> O
*)
(**
Fixpoint sub_normalize (n : bin) : bin :=
match n with
| O' => O'
| A n' => sub_normalize(n')
| B n' => n
end.
Fixpoint sub_normalize_2 (n : bin) : nat :=
match n with
| O' => 0
| A n' => 1 + sub_normalize_2(n')
| B n' => 1
end.
*)
Fixpoint normalize (n : bin) : bin :=
match n with
| O' => O'
| A n' =>
match normalize(n') with
| O' => O'
| A n'' => A(A(n''))
| B n'' => A(B(n''))
end