-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tactics.v
1372 lines (1097 loc) · 46.3 KB
/
Tactics.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
(** * Tactics: More Basic Tactics *)
(** This chapter introduces several additional proof strategies
and tactics that allow us to begin proving more interesting
properties of functional programs.
We will see:
- how to use auxiliary lemmas in both "forward-" and
"backward-style" proofs;
- how to reason about data constructors -- in particular, how to
use the fact that they are injective and disjoint;
- how to strengthen an induction hypothesis, and when such
strengthening is required; and
- more details on how to reason by case analysis. *)
Set Warnings "-notation-overridden,-parsing,-deprecated-hint-without-locality".
From LF Require Export Lists.
From LF Require Export Poly.
(* ################################################################# *)
(** * The [apply] Tactic *)
(** We often encounter situations where the goal to be proved is
_exactly_ the same as some hypothesis in the context or some
previously proved lemma. *)
Theorem silly1 : forall (n m : nat),
n = m ->
n = m.
Proof.
intros n m eq.
(** Here, we could finish with "[rewrite -> eq. reflexivity.]" as we
have done several times before. Or we can finish in a single step
by using [apply]: *)
apply eq. Qed.
(** The [apply] tactic also works with _conditional_ hypotheses
and lemmas: if the statement being applied is an implication, then
the premises of this implication will be added to the list of
subgoals needing to be proved.
[apply] also works with _conditional_ hypotheses: *)
Theorem silly2 : forall (n m o p : nat),
n = m ->
(n = m -> [n;o] = [m;p]) ->
[n;o] = [m;p].
Proof.
intros n m o p eq1 eq2.
apply eq2. apply eq1. Qed.
(** Typically, when we use [apply H], the statement [H] will
begin with a [forall] that introduces some _universally quantified
variables_.
When Coq matches the current goal against the conclusion of [H],
it will try to find appropriate values for these variables. For
example, when we do [apply eq2] in the following proof, the
universal variable [q] in [eq2] gets instantiated with [n], and
[r] gets instantiated with [m]. *)
Theorem silly2a : forall (n m : nat),
(n,n) = (m,m) ->
(forall (q r : nat), (q,q) = (r,r) -> [q] = [r]) ->
[n] = [m].
Proof.
intros n m eq1 eq2.
apply eq2. apply eq1. Qed.
(** **** Exercise: 2 stars, standard, optional (silly_ex)
Complete the following proof using only [intros] and [apply]. *)
Theorem silly_ex : forall p,
(forall n, even n = true -> even (S n) = false) ->
(forall n, even n = false -> odd n = true) ->
even p = true ->
odd (S p) = true.
Proof.
intros p H1 H2. intro H. apply H2. apply H1. apply H. Qed.
(** [] *)
(** To use the [apply] tactic, the (conclusion of the) fact
being applied must match the goal exactly (perhaps after
simplification) -- for example, [apply] will not work if the left
and right sides of the equality are swapped. *)
Theorem silly3 : forall (n m : nat),
n = m ->
m = n.
Proof.
intros n m H.
(** Here we cannot use [apply] directly... *)
Fail apply H.
(** but we can use the [symmetry] tactic, which switches the left
and right sides of an equality in the goal. *)
symmetry. apply H. Qed.
(** **** Exercise: 2 stars, standard (apply_exercise1)
You can use [apply] with previously defined theorems, not
just hypotheses in the context. Use [Search] to find a
previously-defined theorem about [rev] from [Lists]. Use
that theorem as part of your (relatively short) solution to this
exercise. You do not need [induction]. *)
(* Since I can't invoke it because list nat != natlist, I rewrote the theorem here *)
Lemma rev_injective : forall (l1 l2 : list nat),
rev l1 = rev l2 -> l1 = l2.
Proof.
intros l1 l2.
intro H.
rewrite <- rev_involutive.
rewrite <- H.
rewrite rev_involutive.
reflexivity.
Qed.
Theorem rev_exercise1 : forall (l l' : list nat),
l = rev l' ->
l' = rev l.
Proof.
intros l l'.
intros H.
apply rev_injective.
rewrite rev_involutive.
symmetry. apply H.
Qed.
(** [] *)
(** **** Exercise: 1 star, standard, optional (apply_rewrite)
Briefly explain the difference between the tactics [apply] and
[rewrite]. What are the situations where both can usefully be
applied? *)
(*
Rewrite deal with equalities, but apply deals with conditionals. If we want to prove a=b,
and our current goal is b=a, and we have the hypothesis a=b and if a=b->b=a,
then rewrite gets b=b, whereas applying the hypothesis we get an empty goal.
[] *)
(* ################################################################# *)
(** * The [apply with] Tactic *)
(** The following silly example uses two rewrites in a row to
get from [[a;b]] to [[e;f]]. *)
Example trans_eq_example : forall (a b c d e f : nat),
[a;b] = [c;d] ->
[c;d] = [e;f] ->
[a;b] = [e;f].
Proof.
intros a b c d e f eq1 eq2.
rewrite -> eq1. rewrite -> eq2. reflexivity. Qed.
(** Since this is a common pattern, we might like to pull it out as a
lemma that records, once and for all, the fact that equality is
transitive. *)
Theorem trans_eq : forall (X:Type) (n m o : X),
n = m -> m = o -> n = o.
Proof.
intros X n m o eq1 eq2. rewrite -> eq1. rewrite -> eq2.
reflexivity. Qed.
(** Now, we should be able to use [trans_eq] to prove the above
example. However, to do this we need a slight refinement of the
[apply] tactic. *)
Example trans_eq_example' : forall (a b c d e f : nat),
[a;b] = [c;d] ->
[c;d] = [e;f] ->
[a;b] = [e;f].
Proof.
intros a b c d e f eq1 eq2.
(** If we simply tell Coq [apply trans_eq] at this point, it can
tell (by matching the goal against the conclusion of the lemma)
that it should instantiate [X] with [[nat]], [n] with [[a,b]], and
[o] with [[e,f]]. However, the matching process doesn't determine
an instantiation for [m]: we have to supply one explicitly by
adding "[with (m:=[c,d])]" to the invocation of [apply]. *)
apply trans_eq with (m:=[c;d]).
apply eq1. apply eq2. Qed.
(** Actually, the name [m] in the [with] clause is not required,
since Coq is often smart enough to figure out which variable we
are instantiating. We could instead simply write [apply trans_eq
with [c;d]]. *)
(** Coq also has a built-in tactic [transitivity] that
accomplishes the same purpose as applying [trans_eq]. The tactic
requires us to state the instantiation we want, just like [apply
with] does. *)
Example trans_eq_example'' : forall (a b c d e f : nat),
[a;b] = [c;d] ->
[c;d] = [e;f] ->
[a;b] = [e;f].
Proof.
intros a b c d e f eq1 eq2.
transitivity [c;d].
apply eq1. apply eq2. Qed.
(** **** Exercise: 3 stars, standard, optional (trans_eq_exercise) *)
Example trans_eq_exercise : forall (n m o p : nat),
m = (minustwo o) ->
(n + p) = m ->
(n + p) = (minustwo o).
Proof. intros n m o p H1 H2. transitivity m. apply H2. apply H1. Qed.
(** [] *)
(* ################################################################# *)
(** * The [injection] and [discriminate] Tactics *)
(** Recall the definition of natural numbers:
Inductive nat : Type :=
| O
| S (n : nat).
It is obvious from this definition that every number has one of
two forms: either it is the constructor [O] or it is built by
applying the constructor [S] to another number. But there is more
here than meets the eye: implicit in the definition are two
additional facts:
- The constructor [S] is _injective_ (or _one-to-one_). That is,
if [S n = S m], it must also be that [n = m].
- The constructors [O] and [S] are _disjoint_. That is, [O] is not
equal to [S n] for any [n]. *)
(** Similar principles apply to every inductively defined type:
all constructors are injective, and the values built from distinct
constructors are never equal. For lists, the [cons] constructor
is injective and the empty list [nil] is different from every
non-empty list. For booleans, [true] and [false] are different.
(Since [true] and [false] take no arguments, their injectivity is
neither here nor there.) And so on. *)
(** We can _prove_ the injectivity of [S] by using the [pred] function
defined in [Basics.v]. *)
Theorem S_injective : forall (n m : nat),
S n = S m ->
n = m.
Proof.
intros n m H1.
assert (H2: n = pred (S n)). { reflexivity. }
rewrite H2. rewrite H1. simpl. reflexivity.
Qed.
(** This technique can be generalized to any constructor by
writing the equivalent of [pred] -- i.e., writing a function that
"undoes" one application of the constructor.
As a more convenient alternative, Coq provides a tactic called
[injection] that allows us to exploit the injectivity of any
constructor. Here is an alternate proof of the above theorem
using [injection]: *)
Theorem S_injective' : forall (n m : nat),
S n = S m ->
n = m.
Proof.
intros n m H.
(** By writing [injection H as Hmn] at this point, we are asking Coq
to generate all equations that it can infer from [H] using the
injectivity of constructors (in the present example, the equation
[n = m]). Each such equation is added as a hypothesis (called
[Hmn] in this case) into the context. *)
injection H as Hnm. apply Hnm.
Qed.
(** Here's a more interesting example that shows how [injection] can
derive multiple equations at once. *)
Theorem injection_ex1 : forall (n m o : nat),
[n;m] = [o;o] ->
n = m.
Proof.
intros n m o H.
(* WORKED IN CLASS *)
injection H as H1 H2.
rewrite H1. rewrite H2. reflexivity.
Qed.
(** **** Exercise: 3 stars, standard (injection_ex3) *)
Example injection_ex3 : forall (X : Type) (x y z : X) (l j : list X),
x :: y :: l = z :: j ->
j = z :: l ->
x = y.
Proof.
intros X x y z l j. intros H1 H2.
injection H1 as H H'. rewrite <- H' in H2. injection H2 as H3.
transitivity z. apply H. symmetry. apply H3.
Qed.
(** [] *)
(** So much for injectivity of constructors. What about disjointness? *)
(** The principle of disjointness says that two terms beginning
with different constructors (like [O] and [S], or [true] and [false])
can never be equal. This means that, any time we find ourselves
in a context where we've _assumed_ that two such terms are equal,
we are justified in concluding anything we want, since the
assumption is nonsensical. *)
(** The [discriminate] tactic embodies this principle: It is used
on a hypothesis involving an equality between different
constructors (e.g., [false = true]), and it solves the current
goal immediately. Some examples: *)
Theorem discriminate_ex1 : forall (n m : nat),
false = true ->
n = m.
Proof.
intros n m contra. discriminate contra. Qed.
Theorem discriminate_ex2 : forall (n : nat),
S n = O ->
2 + 2 = 5.
Proof.
intros n contra. discriminate contra. Qed.
(** These examples are instances of a logical principle known as the
_principle of explosion_, which asserts that a contradictory
hypothesis entails anything (even manifestly false things!). *)
(** If you find the principle of explosion confusing, remember
that these proofs are _not_ showing that the conclusion of the
statement holds. Rather, they are showing that, _if_ the
nonsensical situation described by the premise did somehow hold,
_then_ the nonsensical conclusion would also follow, because we'd
be living in an inconsistent universe where every statement is
true.
We'll explore the principle of explosion in more detail in the
next chapter. *)
(** **** Exercise: 1 star, standard (discriminate_ex3) *)
Example discriminate_ex3 :
forall (X : Type) (x y z : X) (l j : list X),
x :: y :: l = [] ->
x = z.
Proof. intros X x y z l j H. discriminate H. Qed.
(** [] *)
(** For a more useful example, we can use [discriminate] to make a
connection between the two different notions of equality ([=] and
[=?]) that we have seen for natural numbers. *)
Theorem eqb_0_l : forall n,
0 =? n = true -> n = 0.
Proof.
intros n.
(** We can proceed by case analysis on [n]. The first case is
trivial. *)
destruct n as [| n'] eqn:E.
- (* n = 0 *)
intros H. reflexivity.
(** However, the second one doesn't look so simple: assuming [0
=? (S n') = true], we must show [S n' = 0]! The way forward is to
observe that the assumption itself is nonsensical: *)
- (* n = S n' *)
simpl.
(** If we use [discriminate] on this hypothesis, Coq confirms
that the subgoal we are working on is impossible and removes it
from further consideration. *)
intros H. discriminate H.
Qed.
(** The injectivity of constructors allows us to reason that
[forall (n m : nat), S n = S m -> n = m]. The converse of this
implication is an instance of a more general fact about both
constructors and functions, which we will find convenient
below: *)
Theorem f_equal : forall (A B : Type) (f: A -> B) (x y: A),
x = y -> f x = f y.
Proof. intros A B f x y eq. rewrite eq. reflexivity. Qed.
Theorem eq_implies_succ_equal : forall (n m : nat),
n = m -> S n = S m.
Proof. intros n m H. apply f_equal. apply H. Qed.
(** Indeed, there is also a tactic named `f_equal` that can
prove such theorems directly. Given a goal of the form [f a1
... an = g b1 ... bn], the tactic [f_equal] will produce subgoals
of the form [f = g], [a1 = b1], ..., [an = bn]. At the same time,
any of these subgoals that are simple enough (e.g., immediately
provable by [reflexivity]) will be automatically discharged by
[f_equal]. *)
Theorem eq_implies_succ_equal' : forall (n m : nat),
n = m -> S n = S m.
Proof. intros n m H. f_equal. apply H. Qed.
(* ################################################################# *)
(** * Using Tactics on Hypotheses *)
(** By default, most tactics work on the goal formula and leave
the context unchanged. However, most tactics also have a variant
that performs a similar operation on a statement in the context.
For example, the tactic "[simpl in H]" performs simplification on
the hypothesis [H] in the context. *)
Theorem S_inj : forall (n m : nat) (b : bool),
((S n) =? (S m)) = b ->
(n =? m) = b.
Proof.
intros n m b H. simpl in H. apply H. Qed.
(** Similarly, [apply L in H] matches some conditional statement
[L] (of the form [X -> Y], say) against a hypothesis [H] in the
context. However, unlike ordinary [apply] (which rewrites a goal
matching [Y] into a subgoal [X]), [apply L in H] matches [H]
against [X] and, if successful, replaces it with [Y].
In other words, [apply L in H] gives us a form of "forward
reasoning": given [X -> Y] and a hypothesis matching [X], it
produces a hypothesis matching [Y].
By contrast, [apply L] is "backward reasoning": it says that if we
know [X -> Y] and we are trying to prove [Y], it suffices to prove
[X].
Here is a variant of a proof from above, using forward reasoning
throughout instead of backward reasoning. *)
Theorem silly4 : forall (n m p q : nat),
(n = m -> p = q) ->
m = n ->
q = p.
Proof.
intros n m p q EQ H.
symmetry in H. apply EQ in H. symmetry in H.
apply H. Qed.
(** Forward reasoning starts from what is _given_ (premises,
previously proven theorems) and iteratively draws conclusions from
them until the goal is reached. Backward reasoning starts from
the _goal_ and iteratively reasons about what would imply the
goal, until premises or previously proven theorems are reached.
The informal proofs seen in math or computer science classes tend
to use forward reasoning. By contrast, idiomatic use of Coq
generally favors backward reasoning, though in some situations the
forward style can be easier to think about. *)
(* ################################################################# *)
(** * Specializing Hypotheses *)
(** Another handy tactic for fiddling with hypotheses is [specialize].
It is essentially just a combination of [assert] and [apply], but
it often provides a pleasingly smooth way to nail down overly
general assumptions. It works like this:
If [H] is a quantified hypothesis in the current context -- i.e.,
[H : forall (x:T), P] -- then [specialize H with (x := e)] will
change [H] so that it looks like [[x:=e]P], that is, [P] with [x]
replaced by [e].
For example: *)
Theorem specialize_example: forall n,
(forall m, m*n = 0)
-> n = 0.
Proof.
intros n H.
specialize H with (m := 1).
simpl in H.
rewrite add_comm in H.
simpl in H.
apply H. Qed.
(** Using [specialize] before [apply] gives us yet another way to
control where [apply] does its work. *)
Example trans_eq_example''' : forall (a b c d e f : nat),
[a;b] = [c;d] ->
[c;d] = [e;f] ->
[a;b] = [e;f].
Proof.
intros a b c d e f eq1 eq2.
specialize trans_eq with (m:=[c;d]) as H.
apply H.
apply eq1.
apply eq2. Qed.
(** Note:
- We can [specialize] facts in the global context, not just
local hypotheses.
- The [as...] clause at the end tells [specialize] how to name
the new hypothesis in this case. *)
(* ################################################################# *)
(** * Varying the Induction Hypothesis *)
(** Sometimes it is important to control the exact form of the
induction hypothesis when carrying out inductive proofs in Coq.
In particular, we may need to be careful about which of the
assumptions we move (using [intros]) from the goal to the context
before invoking the [induction] tactic.
For example, suppose we want to show that [double] is injective --
i.e., that it maps different arguments to different results:
Theorem double_injective: forall n m,
double n = double m ->
n = m.
The way we start this proof is a bit delicate: if we begin it with
intros n. induction n.
then all is well. But if we begin it with introducing both
variables
intros n m. induction n.
we get stuck in the middle of the inductive case... *)
Theorem double_injective_FAILED : forall n m,
double n = double m ->
n = m.
Proof.
intros n m. induction n as [| n' IHn'].
- (* n = O *) simpl. intros eq. destruct m as [| m'] eqn:E.
+ (* m = O *) reflexivity.
+ (* m = S m' *) discriminate eq.
- (* n = S n' *) intros eq. destruct m as [| m'] eqn:E.
+ (* m = O *) discriminate eq.
+ (* m = S m' *) f_equal.
(** At this point, the induction hypothesis ([IHn']) does _not_ give us
[n' = m'] -- there is an extra [S] in the way -- so the goal is
not provable. *)
Abort.
(** What went wrong? *)
(** The problem is that, at the point where we invoke the
induction hypothesis, we have already introduced [m] into the
context -- intuitively, we have told Coq, "Let's consider some
particular [n] and [m]..." and we now have to prove that, if
[double n = double m] for _these particular_ [n] and [m], then
[n = m].
The next tactic, [induction n] says to Coq: We are going to show
the goal by induction on [n]. That is, we are going to prove, for
_all_ [n], that the proposition
- [P n] = "if [double n = double m], then [n = m]"
holds, by showing
- [P O]
(i.e., "if [double O = double m] then [O = m]") and
- [P n -> P (S n)]
(i.e., "if [double n = double m] then [n = m]" implies "if
[double (S n) = double m] then [S n = m]").
If we look closely at the second statement, it is saying something
rather strange: that, for a _particular_ [m], if we know
- "if [double n = double m] then [n = m]"
then we can prove
- "if [double (S n) = double m] then [S n = m]".
To see why this is strange, let's think of a particular [m] --
say, [5]. The statement is then saying that, if we know
- [Q] = "if [double n = 10] then [n = 5]"
then we can prove
- [R] = "if [double (S n) = 10] then [S n = 5]".
But knowing [Q] doesn't give us any help at all with proving [R]!
If we tried to prove [R] from [Q], we would start with something
like "Suppose [double (S n) = 10]..." but then we'd be stuck:
knowing that [double (S n)] is [10] tells us nothing helpful about
whether [double n] is [10] (indeed, it strongly suggests that
[double n] is _not_ [10]!!), so [Q] is useless. *)
(** Trying to carry out this proof by induction on [n] when [m] is
already in the context doesn't work because we are then trying to
prove a statement involving _every_ [n] but just a _particular_
[m]. *)
(** A successful proof of [double_injective] leaves [m] universally
quantified in the goal statement at the point where the
[induction] tactic is invoked on [n]: *)
Theorem double_injective : forall n m,
double n = double m ->
n = m.
Proof.
intros n. induction n as [| n' IHn'].
- (* n = O *) simpl. intros m eq. destruct m as [| m'] eqn:E.
+ (* m = O *) reflexivity.
+ (* m = S m' *) discriminate eq.
- (* n = S n' *)
(** Notice that both the goal and the induction hypothesis are
different this time: the goal asks us to prove something more
general (i.e., we must prove the statement for _every_ [m]), but
the IH is correspondingly more flexible, allowing us to choose any
[m] we like when we apply the IH. *)
intros m eq.
(** Now we've chosen a particular [m] and introduced the assumption
that [double n = double m]. Since we are doing a case analysis on
[n], we also need a case analysis on [m] to keep the two "in sync." *)
destruct m as [| m'] eqn:E.
+ (* m = O *)
(** The 0 case is trivial: *)
discriminate eq.
+ (* m = S m' *)
f_equal.
(** Since we are now in the second branch of the [destruct m], the
[m'] mentioned in the context is the predecessor of the [m] we
started out talking about. Since we are also in the [S] branch of
the induction, this is perfect: if we instantiate the generic [m]
in the IH with the current [m'] (this instantiation is performed
automatically by the [apply] in the next step), then [IHn'] gives
us exactly what we need to finish the proof. *)
apply IHn'. simpl in eq. injection eq as goal. apply goal. Qed.
(** The thing to take away from all this is that you need to be
careful, when using induction, that you are not trying to prove
something too specific: When proving a property quantified over
variables [n] and [m] by induction on [n], it is sometimes crucial
to leave [m] generic. *)
(** The following exercise, which further strengthens the link between
[=?] and [=], follows the same pattern. *)
(** **** Exercise: 2 stars, standard (eqb_true) *)
Theorem eqb_true : forall n m,
n =? m = true -> n = m.
Proof.
induction n as [|n' IH].
- destruct m as [|m'].
+ reflexivity.
+ simpl. discriminate.
- destruct m as [|m'].
+ simpl. discriminate.
+ intro H. f_equal. apply IH. simpl in H. apply H.
Qed.
(** [] *)
(** **** Exercise: 2 stars, advanced (eqb_true_informal)
Give a careful informal proof of [eqb_true], stating the induction
hypothesis explicitly and being as explicit as possible about
quantifiers, everywhere. *)
(* We induct on n.
If n = 0, then we prove "forall m, 0 =? m = true -> 0 = m" by cases:
If m = 0, then 0 =? 0 and 0 = 0
If m = S m' for some m', then it is not the case that 0 =? S m', so the conditional is true
If n = S n' for some n', then the inductive hypothesis is "forall k, n' =? k = true -> n' = k".
We split m into two cases:
If m = 0, then it is not the case that S n' =? 0, so the conditional is trivially true
If m = S m', then we want to prove S n' =? S m' = true -> S n' = S m'
Assume S n' =? S m' = true.
By injectivity of S, S n' = S m' if n' = m'.
To prove that, we try to prove n' =? m' = true (from the IH where k = m').
If we simplify our asumption,S n' =? S m' = true just in case that n' =? m' = true.
So we have proved that n' =? m' = true. *)
(* Do not modify the following line: *)
Definition manual_grade_for_informal_proof : option (nat*string) := None.
(** [] *)
(** **** Exercise: 3 stars, standard, especially useful (plus_n_n_injective)
In addition to being careful about how you use [intros], practice
using "in" variants in this proof. (Hint: use [plus_n_Sm].) *)
Theorem plus_n_n_injective : forall n m,
n + n = m + m ->
n = m.
Proof.
induction n as [|n' IH].
- simpl. destruct m as [|m'].
+ reflexivity.
+ simpl. discriminate.
- simpl. destruct m as [|m'].
+ simpl. discriminate.
+ intro H. simpl in H.
rewrite <- plus_n_Sm in H. rewrite <- plus_n_Sm in H.
f_equal. apply IH. injection H as H. rewrite H. reflexivity.
Qed.
(** [] *)
(** The strategy of doing fewer [intros] before an [induction] to
obtain a more general IH doesn't always work; sometimes some
_rearrangement_ of quantified variables is needed. Suppose, for
example, that we wanted to prove [double_injective] by induction
on [m] instead of [n]. *)
Theorem double_injective_take2_FAILED : forall n m,
double n = double m ->
n = m.
Proof.
intros n m. induction m as [| m' IHm'].
- (* m = O *) simpl. intros eq. destruct n as [| n'] eqn:E.
+ (* n = O *) reflexivity.
+ (* n = S n' *) discriminate eq.
- (* m = S m' *) intros eq. destruct n as [| n'] eqn:E.
+ (* n = O *) discriminate eq.
+ (* n = S n' *) f_equal.
(* We are stuck here, just like before. *)
Abort.
(** The problem is that, to do induction on [m], we must first
introduce [n]. (If we simply say [induction m] without
introducing anything first, Coq will automatically introduce [n]
for us!) *)
(** What can we do about this? One possibility is to rewrite the
statement of the lemma so that [m] is quantified before [n]. This
works, but it's not nice: We don't want to have to twist the
statements of lemmas to fit the needs of a particular strategy for
proving them! Rather we want to state them in the clearest and
most natural way. *)
(** What we can do instead is to first introduce all the quantified
variables and then _re-generalize_ one or more of them,
selectively taking variables out of the context and putting them
back at the beginning of the goal. The [generalize dependent]
tactic does this. *)
Theorem double_injective_take2 : forall n m,
double n = double m ->
n = m.
Proof.
intros n m.
(* [n] and [m] are both in the context *)
generalize dependent n.
(* Now [n] is back in the goal and we can do induction on
[m] and get a sufficiently general IH. *)
induction m as [| m' IHm'].
- (* m = O *) simpl. intros n eq. destruct n as [| n'] eqn:E.
+ (* n = O *) reflexivity.
+ (* n = S n' *) discriminate eq.
- (* m = S m' *) intros n eq. destruct n as [| n'] eqn:E.
+ (* n = O *) discriminate eq.
+ (* n = S n' *) f_equal.
apply IHm'. injection eq as goal. apply goal. Qed.
(** Let's look at an informal proof of this theorem. Note that
the proposition we prove by induction leaves [n] quantified,
corresponding to the use of generalize dependent in our formal
proof.
_Theorem_: For any nats [n] and [m], if [double n = double m], then
[n = m].
_Proof_: Let [m] be a [nat]. We prove by induction on [m] that, for
any [n], if [double n = double m] then [n = m].
- First, suppose [m = 0], and suppose [n] is a number such
that [double n = double m]. We must show that [n = 0].
Since [m = 0], by the definition of [double] we have [double n =
0]. There are two cases to consider for [n]. If [n = 0] we are
done, since [m = 0 = n], as required. Otherwise, if [n = S n']
for some [n'], we derive a contradiction: by the definition of
[double], we can calculate [double n = S (S (double n'))], but
this contradicts the assumption that [double n = 0].
- Second, suppose [m = S m'] and that [n] is again a number such
that [double n = double m]. We must show that [n = S m'], with
the induction hypothesis that for every number [s], if [double s =
double m'] then [s = m'].
By the fact that [m = S m'] and the definition of [double], we
have [double n = S (S (double m'))]. There are two cases to
consider for [n].
If [n = 0], then by definition [double n = 0], a contradiction.
Thus, we may assume that [n = S n'] for some [n'], and again by
the definition of [double] we have [S (S (double n')) =
S (S (double m'))], which implies by injectivity that [double n' =
double m']. Instantiating the induction hypothesis with [n'] thus
allows us to conclude that [n' = m'], and it follows immediately
that [S n' = S m']. Since [S n' = n] and [S m' = m], this is just
what we wanted to show. [] *)
(** **** Exercise: 3 stars, standard, especially useful (gen_dep_practice)
Prove this by induction on [l]. *)
Theorem nth_error_after_last: forall (n : nat) (X : Type) (l : list X),
length l = n ->
nth_error l n = None.
Proof.
intros n X l. generalize dependent n.
induction l as [|h t IH].
- reflexivity.
- destruct n as [|n'].
+ simpl. discriminate.
+ simpl. intro H. apply IH. injection H as H. apply H.
Qed.
(** [] *)
(* ################################################################# *)
(** * Unfolding Definitions *)
(** It sometimes happens that we need to manually unfold a name that
has been introduced by a [Definition] so that we can manipulate
the expression it stands for.
For example, if we define... *)
Definition square n := n * n.
(** ...and try to prove a simple fact about [square]... *)
Lemma square_mult : forall n m, square (n * m) = square n * square m.
Proof.
intros n m.
simpl.
(** ...we appear to be stuck: [simpl] doesn't simplify anything, and
since we haven't proved any other facts about [square], there is
nothing we can [apply] or [rewrite] with. *)
(** To make progress, we can manually [unfold] the definition of
[square]: *)
unfold square.
(** Now we have plenty to work with: both sides of the equality are
expressions involving multiplication, and we have lots of facts
about multiplication at our disposal. In particular, we know that
it is commutative and associative, and from these it is not hard
to finish the proof. *)
rewrite mult_assoc.
assert (H : n * m * n = n * n * m).
{ rewrite mul_comm. apply mult_assoc. }
rewrite H. rewrite mult_assoc. reflexivity.
Qed.
(** At this point, a bit deeper discussion of unfolding and
simplification is in order.
We already have observed that tactics like [simpl], [reflexivity],
and [apply] will often unfold the definitions of functions
automatically when this allows them to make progress. For
example, if we define [foo m] to be the constant [5]... *)
Definition foo (x: nat) := 5.
(** .... then the [simpl] in the following proof (or the
[reflexivity], if we omit the [simpl]) will unfold [foo m] to
[(fun x => 5) m] and further simplify this expression to just
[5]. *)
Fact silly_fact_1 : forall m, foo m + 1 = foo (m + 1) + 1.
Proof.
intros m.
simpl.
reflexivity.
Qed.
(** But this automatic unfolding is somewhat conservative. For
example, if we define a slightly more complicated function
involving a pattern match... *)
Definition bar x :=
match x with
| O => 5
| S _ => 5
end.
(** ...then the analogous proof will get stuck: *)
Fact silly_fact_2_FAILED : forall m, bar m + 1 = bar (m + 1) + 1.
Proof.
intros m.
simpl. (* Does nothing! *)
Abort.
(** The reason that [simpl] doesn't make progress here is that it
notices that, after tentatively unfolding [bar m], it is left with
a match whose scrutinee, [m], is a variable, so the [match] cannot
be simplified further. It is not smart enough to notice that the
two branches of the [match] are identical, so it gives up on
unfolding [bar m] and leaves it alone.
Similarly, tentatively unfolding [bar (m+1)] leaves a [match]
whose scrutinee is a function application (that cannot itself be
simplified, even after unfolding the definition of [+]), so
[simpl] leaves it alone. *)
(** At this point, there are two ways to make progress. One is to use
[destruct m] to break the proof into two cases, each focusing on a
more concrete choice of [m] ([O] vs [S _]). In each case, the
[match] inside of [bar] can now make progress, and the proof is
easy to complete. *)
Fact silly_fact_2 : forall m, bar m + 1 = bar (m + 1) + 1.
Proof.
intros m.
destruct m eqn:E.
- simpl. reflexivity.
- simpl. reflexivity.
Qed.
(** This approach works, but it depends on our recognizing that the
[match] hidden inside [bar] is what was preventing us from making
progress. *)
(** A more straightforward way forward is to explicitly tell Coq to
unfold [bar]. *)
Fact silly_fact_2' : forall m, bar m + 1 = bar (m + 1) + 1.
Proof.
intros m.
unfold bar.
(** Now it is apparent that we are stuck on the [match] expressions on
both sides of the [=], and we can use [destruct] to finish the
proof without thinking so hard. *)
destruct m eqn:E.
- reflexivity.
- reflexivity.
Qed.
(* ################################################################# *)
(** * Using [destruct] on Compound Expressions *)
(** We have seen many examples where [destruct] is used to
perform case analysis of the value of some variable. Sometimes we
need to reason by cases on the result of some _expression_. We
can also do this with [destruct].
Here are some examples: *)
Definition sillyfun (n : nat) : bool :=
if n =? 3 then false
else if n =? 5 then false
else false.
Theorem sillyfun_false : forall (n : nat),
sillyfun n = false.
Proof.
intros n. unfold sillyfun.
destruct (n =? 3) eqn:E1.
- (* n =? 3 = true *) reflexivity.
- (* n =? 3 = false *) destruct (n =? 5) eqn:E2.
+ (* n =? 5 = true *) reflexivity.
+ (* n =? 5 = false *) reflexivity. Qed.
(** After unfolding [sillyfun] in the above proof, we find that
we are stuck on [if (n =? 3) then ... else ...]. But either
[n] is equal to [3] or it isn't, so we can use [destruct (eqb
n 3)] to let us reason about the two cases.
In general, the [destruct] tactic can be used to perform case
analysis of the results of arbitrary computations. If [e] is an
expression whose type is some inductively defined type [T], then,
for each constructor [c] of [T], [destruct e] generates a subgoal
in which all occurrences of [e] (in the goal and in the context)
are replaced by [c]. *)
(** **** Exercise: 3 stars, standard (combine_split)