-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndProp.v
3324 lines (2814 loc) · 112 KB
/
IndProp.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
(** * IndProp: Inductively Defined Propositions *)
Set Warnings "-notation-overridden,-parsing,-deprecated-hint-without-locality".
From LF Require Export Logic.
(* ################################################################# *)
(** * Inductively Defined Propositions *)
(** In the [Logic] chapter, we looked at several ways of writing
propositions, including conjunction, disjunction, and existential
quantification.
In this chapter, we bring yet another new tool into the mix:
_inductively defined propositions_.
To begin, some examples... *)
(* ================================================================= *)
(** ** Example: The Collatz Conjecture *)
(** The _Collatz Conjecture_ is a famous open problem in number
theory.
Its statement is quite simple. First, we define a function [f]
on numbers, as follows: *)
Fixpoint div2 (n : nat) :=
match n with
0 => 0
| 1 => 0
| S (S n) => S (div2 n)
end.
Definition f (n : nat) :=
if even n then div2 n
else (3 * n) + 1.
(** Next, we look at what happens when we repeatedly apply [f] to
some given starting number. For example, [f 12] is [6], and [f
6] is [3], so by repeatedly applying [f] we get the sequence
[12, 6, 3, 10, 5, 16, 8, 4, 2, 1].
Similarly, if we start with [19], we get the longer sequence
[19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5,
16, 8, 4, 2, 1].
Both of these sequences eventually reach [1]. The question
posed by Collatz was: Is the sequence starting from _any_
natural number guaranteed to reach [1] eventually? *)
(** To formalize this question in Coq, we might try to define a
recursive _function_ that calculates the total number of steps
that it takes for such a sequence to reach [1]. *)
Fail Fixpoint reaches_1_in (n : nat) :=
if n =? 1 then true
else 1 + reaches_1_in (f n).
(** This definition is rejected by Coq's termination checker, since
the argument to the recursive call, [f n], is not "obviously
smaller" than [n].
Indeed, this isn't just a pointless limitation: functions in Coq
are required to be total, to ensure logical consistency.
Moreover, we can't fix it by devising a more clever termination
checker: deciding whether this particular function is total
would be equivalent to settling the Collatz conjecture! *)
(** Fortunately, there is another way to do it: We can express the
concept "reaches [1] eventually" as an _inductively defined
property_ of numbers: *)
Inductive Collatz_holds_for : nat -> Prop :=
| Chf_done : Collatz_holds_for 1
| Chf_more (n : nat) : Collatz_holds_for (f n) -> Collatz_holds_for n.
(** What we've done here is to use Coq's [Inductive] definition
mechanism to characterize the property "Collatz holds for..." by
stating two different ways in which it can hold: (1) Collatz holds
for [1] and (2) if Collatz holds for [f n] then it holds for
[n]. *)
(** For particular numbers, we can now argue that the Collatz sequence
reaches [1] like this (again, we'll go through the details of how
it works a bit later in the chapter): *)
Example Collatz_holds_for_12 : Collatz_holds_for 12.
Proof.
apply Chf_more. unfold f. simpl.
apply Chf_more. unfold f. simpl.
apply Chf_more. unfold f. simpl.
apply Chf_more. unfold f. simpl.
apply Chf_more. unfold f. simpl.
apply Chf_more. unfold f. simpl.
apply Chf_more. unfold f. simpl.
apply Chf_more. unfold f. simpl.
apply Chf_more. unfold f. simpl.
apply Chf_done. Qed.
(** The Collatz conjecture then states that the sequence beginning
from _any_ number reaches [1]: *)
Conjecture collatz : forall n, Collatz_holds_for n.
(** If you succeed in proving this conjecture, you've got a bright
future as a number theorist! But don't spend too long on it --
it's been open since 1937. *)
(* ================================================================= *)
(** ** Example: Ordering *)
(** A binary _relation_ on a set [X] is a family of propositions
parameterized by two elements of [X] -- i.e., a proposition
about pairs of elements of [X]. *)
(** For example, one familiar binary relation on [nat] is [le], the
less-than-or-equal-to relation. We've already seen how to define
it as a boolean computation. Here is a "direct" propositional
definition. *)
Module LePlayground.
(** The following definition says that there are two ways to
show that one number is less than or equal to another: either
observe that they are the same number, or, if the second has the
form [S m], give evidence that the first is less than or equal to
[m]. *)
Inductive le : nat -> nat -> Prop :=
| le_n (n : nat) : le n n
| le_S (n m : nat) : le n m -> le n (S m).
Notation "n <= m" := (le n m) (at level 70).
Example le_3_5 : 3 <= 5.
Proof.
apply le_S. apply le_S. apply le_n. Qed.
End LePlayground.
Module LePlayground1.
(** (By "reserving" the notation before defining the [Inductive], we
can use it in the definition.) *)
Reserved Notation "n <= m" (at level 70).
Inductive le : nat -> nat -> Prop :=
| le_n (n : nat) : n <= n
| le_S (n m : nat) : n <= m -> n <= (S m)
where "n <= m" := (le n m).
End LePlayground1.
(* ================================================================= *)
(** ** Example: Transitive Closure *)
(** As another example, the _transitive closure_ of a relation [R]
is the smallest relation that contains [R] and that is
transitive. *)
Inductive clos_trans {X: Type} (R: X->X->Prop) : X->X->Prop :=
| t_step (x y : X) :
R x y ->
clos_trans R x y
| t_trans (x y z : X) :
clos_trans R x y ->
clos_trans R y z ->
clos_trans R x z.
(** For example, suppose we define a "parent of" relation on a group
of people... *)
Inductive Person : Type := Sage | Cleo | Ridley | Moss.
Inductive parent_of : Person -> Person -> Prop :=
po_SC : parent_of Sage Cleo
| po_SR : parent_of Sage Ridley
| po_CM : parent_of Cleo Moss.
(** Then we can define "ancestor of" as its transitive closure: *)
Definition ancestor_of : Person -> Person -> Prop :=
clos_trans parent_of.
Example ancestor_of1 : ancestor_of Sage Moss.
Proof.
unfold ancestor_of. apply t_trans with Cleo.
- apply t_step. apply po_SC.
- apply t_step. apply po_CM. Qed.
(** **** Exercise: 1 star, standard, optional (close_refl_trans)
How would you modify this definition so that it defines _reflexive
and_ transitive closure? How about reflexive, symmetric, and
transitive closure? *)
Inductive close_refl_symm_trans {X:Type} (R: X->X->Prop) : X->X->Prop :=
| rts_step (x y : X) :
R x y ->
close_refl_symm_trans R x y
| rts_refl (x : X) :
close_refl_symm_trans R x x
| rts_symm (x y : X) :
close_refl_symm_trans R x y ->
close_refl_symm_trans R y x
| rts_trans (x y z : X) :
close_refl_symm_trans R x y ->
close_refl_symm_trans R y z ->
close_refl_symm_trans R x z.
(* [] *)
(* ================================================================= *)
(** ** Example: Permutations *)
(** The familiar mathematical concept of _permutation_ also has an
elegant formulation as an inductive relation. For simplicity,
let's focus on permutations of lists with exactly three
elements. *)
Inductive Perm3 {X : Type} : list X -> list X -> Prop :=
| perm3_swap12 (a b c : X) :
Perm3 [a;b;c] [b;a;c]
| perm3_swap23 (a b c : X) :
Perm3 [a;b;c] [a;c;b]
| perm3_trans (l1 l2 l3 : list X) :
Perm3 l1 l2 -> Perm3 l2 l3 -> Perm3 l1 l3.
(** This definition says:
- If [l2] can be obtained from [l1] by swapping the first and
second elements, then [l2] is a permutation of [l1].
- If [l2] can be obtained from [l1] by swapping the second and
third elements, then [l2] is a permutation of [l1].
- If [l2] is a permutation of [l1] and [l3] is a permutation of
[l2], then [l3] is a permutation of [l1]. *)
(** **** Exercise: 1 star, standard, optional (perm)
According to this definition, is [[1;2;3]] a permutation of
[[3;2;1]]? Is [[1;2;3]] a permutation of itself? *)
(* Yes, [1;2;3] => [1;3;2] => [3;1;2] => [3;2;1]
[1;2;3] => [2;1;3] => [1;2;3]
[] *)
Example Perm3_example1 : Perm3 [1;2;3] [2;3;1].
Proof.
apply perm3_trans with [2;1;3].
- apply perm3_swap12.
- apply perm3_swap23. Qed.
(* ================================================================= *)
(** ** Example: Evenness (yet again) *)
(** We've already seen two ways of stating a proposition that a number
[n] is even: We can say
(1) [even n = true], or
(2) [exists k, n = double k].
A third possibility, which we'll use as a running example for the
rest of this chapter, is to say that [n] is even if we can
_establish_ its evenness from the following rules:
- The number [0] is even.
- If [n] is even, then [S (S n)] is even. *)
(** (Defining evenness in this way may seem a bit confusing,
since we have already seen another perfectly good way of doing
it -- "[n] is even if it is equal to the result of doubling some
number". It makes a convenient running example because it is
simple and compact, but we will see more compelling examples in
future chapters.) *)
(** To illustrate how this new definition of evenness works,
let's imagine using it to show that [4] is even. First, we give
the rules names for easy reference:
- Rule [ev_0]: The number [0] is even.
- Rule [ev_SS]: If [n] is even, then [S (S n)] is even.
Now, by rule [ev_SS], it suffices to show that [2] is even. This,
in turn, is again guaranteed by rule [ev_SS], as long as we can
show that [0] is even. But this last fact follows directly from
the [ev_0] rule. *)
(** We can translate the informal definition of evenness from above
into a formal [Inductive] declaration, where each "way that a
number can be even" corresponds to a separate constructor: *)
(* Inductive ev (n:nat): Prop :=
| ev_0 : n = 0 -> ev n
| ev_SS : ev n -> ev (S (S n)). *)
Inductive ev : nat -> Prop :=
| ev_0 : ev 0
| ev_SS (n : nat) (H : ev n) : ev (S (S n)).
(** This definition is interestingly different from previous uses of
[Inductive]. For one thing, we are defining not a [Type] (like
[nat]) or a function yielding a [Type] (like [list]), but rather a
function from [nat] to [Prop] -- that is, a property of numbers.
But what is really new is that, because the [nat] argument of [ev]
appears to the _right_ of the colon on the first line, it is
allowed to take _different_ values in the types of different
constructors: [0] in the type of [ev_0] and [S (S n)] in the type
of [ev_SS]. Accordingly, the type of each constructor must be
specified explicitly (after a colon), and each constructor's type
must have the form [ev n] for some natural number [n].
In contrast, recall the definition of [list]:
Inductive list (X:Type) : Type :=
| nil
| cons (x : X) (l : list X).
or equivalently:
Inductive list (X:Type) : Type :=
| nil : list X
| cons (x : X) (l : list X) : list X.
This definition introduces the [X] parameter _globally_, to the
_left_ of the colon, forcing the result of [nil] and [cons] to be
the same type (i.e., [list X]). But if we had tried to bring [nat]
to the left of the colon in defining [ev], we would have seen an
error: *)
Fail Inductive wrong_ev (n : nat) : Prop :=
| wrong_ev_0 : wrong_ev 0
| wrong_ev_SS (H: wrong_ev n) : wrong_ev (S (S n)).
(* ===> Error: Last occurrence of "[wrong_ev]" must have "[n]" as 1st
argument in "[wrong_ev 0]". *)
(** In an [Inductive] definition, an argument to the type constructor
on the left of the colon is called a "parameter", whereas an
argument on the right is called an "index" or "annotation."
For example, in [Inductive list (X : Type) := ...], the [X] is a
parameter, while in [Inductive ev : nat -> Prop := ...], the
unnamed [nat] argument is an index. *)
(** We can think of this as defining a Coq property [ev : nat ->
Prop], together with "evidence constructors" [ev_0 : ev 0] and
[ev_SS : forall n, ev n -> ev (S (S n))]. *)
(** These evidence constructors can be thought of as "primitive
evidence of evenness", and they can be used just like proven
theorems. In particular, we can use Coq's [apply] tactic with the
constructor names to obtain evidence for [ev] of particular
numbers... *)
Theorem ev_4 : ev 4.
Proof. apply ev_SS. apply ev_SS. apply ev_0. Qed.
(** ... or we can use function application syntax to combine several
constructors: *)
Theorem ev_4' : ev 4.
Proof. apply (ev_SS 2 (ev_SS 0 ev_0)). Qed.
(** In this way, we can also prove theorems that have hypotheses
involving [ev]. *)
Theorem ev_plus4 : forall n, ev n -> ev (4 + n).
Proof.
intros n. simpl. intros Hn. apply ev_SS. apply ev_SS. apply Hn.
Qed.
(** **** Exercise: 1 star, standard (ev_double) *)
Theorem ev_double : forall n,
ev (double n).
Proof.
intros n.
induction n as [|n IH].
- apply ev_0.
- simpl. apply ev_SS. apply IH.
Qed.
(** [] *)
(* ################################################################# *)
(** * Using Evidence in Proofs *)
(** Besides _constructing_ evidence that numbers are even, we can also
_destruct_ such evidence, reasoning about how it could have been
built.
Defining [ev] with an [Inductive] declaration tells Coq not
only that the constructors [ev_0] and [ev_SS] are valid ways to
build evidence that some number is [ev], but also that these two
constructors are the _only_ ways to build evidence that numbers
are [ev]. *)
(** In other words, if someone gives us evidence [E] for the assertion
[ev n], then we know that [E] must be one of two things:
- [E] is [ev_0] (and [n] is [O]), or
- [E] is [ev_SS n' E'] (and [n] is [S (S n')], where [E'] is
evidence for [ev n']). *)
(** This suggests that it should be possible to analyze a
hypothesis of the form [ev n] much as we do inductively defined
data structures; in particular, it should be possible to argue by
_case analysis_ or by _induction_ on such evidence. Let's look at a
few examples to see what this means in practice. *)
(* ================================================================= *)
(** ** Inversion on Evidence *)
(** Suppose we are proving some fact involving a number [n], and
we are given [ev n] as a hypothesis. We already know how to
perform case analysis on [n] using [destruct] or [induction],
generating separate subgoals for the case where [n = O] and the
case where [n = S n'] for some [n']. But for some proofs we may
instead want to analyze the evidence for [ev n] _directly_.
As a tool for such proofs, we can formalize the intuitive
characterization that we gave above for evidence of [ev n], using
[destruct]. *)
Theorem ev_inversion : forall (n : nat),
ev n ->
(n = 0) \/ (exists n', n = S (S n') /\ ev n').
Proof.
intros n E. destruct E as [ | n' E'] eqn:EE.
- (* E = ev_0 : ev 0 *)
left. reflexivity.
- (* E = ev_SS n' E' : ev (S (S n')) *)
right. exists n'. split. reflexivity. apply E'.
Qed.
(** Facts like this are often called "inversion lemmas" because they
allow us to "invert" some given information to reason about all
the different ways it could have been derived.
Here, there are two ways to prove [ev n], and the inversion lemma
makes this explicit. *)
(** We can use the inversion lemma that we proved above to help
structure proofs: *)
Theorem evSS_ev : forall n, ev (S (S n)) -> ev n.
Proof.
intros n H. apply ev_inversion in H. destruct H as [H0|H1].
- discriminate H0.
- destruct H1 as [n' [Hnm Hev]]. injection Hnm as Heq.
rewrite Heq. apply Hev.
Qed.
(** Note how the inversion lemma produces two subgoals, which
correspond to the two ways of proving [ev]. The first subgoal is
a contradiction that is discharged with [discriminate]. The
second subgoal makes use of [injection] and [rewrite].
Coq provides a handy tactic called [inversion] that factors out
this common pattern, saving us the trouble of explicitly stating
and proving an inversion lemma for every [Inductive] definition we
make.
Here, the [inversion] tactic can detect (1) that the first case,
where [n = 0], does not apply and (2) that the [n'] that appears
in the [ev_SS] case must be the same as [n]. It includes an
"[as]" annotation similar to [destruct], allowing us to assign
names rather than have Coq choose them. *)
Theorem evSS_ev' : forall n,
ev (S (S n)) -> ev n.
Proof.
intros n E. inversion E as [| n' E' Heq].
(* We are in the [E = ev_SS n' E'] case now. *)
apply E'.
Qed.
(** The [inversion] tactic can apply the principle of explosion to
"obviously contradictory" hypotheses involving inductively defined
properties, something that takes a bit more work using our
inversion lemma. Compare: *)
Theorem one_not_even : ~ ev 1.
Proof.
intros H. apply ev_inversion in H. destruct H as [ | [m [Hm _]]].
- discriminate H.
- discriminate Hm.
Qed.
Theorem one_not_even' : ~ ev 1.
Proof.
intros H. inversion H. Qed.
(** **** Exercise: 1 star, standard (inversion_practice)
Prove the following result using [inversion]. (For extra
practice, you can also prove it using the inversion lemma.) *)
Theorem SSSSev__even : forall n,
ev (S (S (S (S n)))) -> ev n.
Proof.
intros n H. inversion H. inversion H1. apply H3.
Qed.
(** [] *)
(** **** Exercise: 1 star, standard (ev5_nonsense)
Prove the following result using [inversion]. *)
Theorem ev5_nonsense :
ev 5 -> 2 + 2 = 9.
Proof.
intros H. inversion H. inversion H1. inversion H3. Qed.
(** [] *)
(** The [inversion] tactic does quite a bit of work. For
example, when applied to an equality assumption, it does the work
of both [discriminate] and [injection]. In addition, it carries
out the [intros] and [rewrite]s that are typically necessary in
the case of [injection]. It can also be applied to analyze
evidence for arbitrary inductively defined propositions, not just
equality. As examples, we'll use it to re-prove some theorems
from chapter [Tactics]. (Here we are being a bit lazy by
omitting the [as] clause from [inversion], thereby asking Coq to
choose names for the variables and hypotheses that it introduces.) *)
Theorem inversion_ex1 : forall (n m o : nat),
[n; m] = [o; o] -> [n] = [m].
Proof.
intros n m o H. inversion H. reflexivity. Qed.
Theorem inversion_ex2 : forall (n : nat),
S n = O -> 2 + 2 = 5.
Proof.
intros n contra. inversion contra. Qed.
(** Here's how [inversion] works in general.
- Suppose the name [H] refers to an assumption [P] in the
current context, where [P] has been defined by an [Inductive]
declaration.
- Then, for each of the constructors of [P], [inversion H]
generates a subgoal in which [H] has been replaced by the
specific conditions under which this constructor could have
been used to prove [P].
- Some of these subgoals will be self-contradictory; [inversion]
throws these away.
- The ones that are left represent the cases that must be proved
to establish the original goal. For those, [inversion] adds
to the proof context all equations that must hold of the
arguments given to [P] -- e.g., [n' = n] in the proof of
[evSS_ev]). *)
(** The [ev_double] exercise above shows that our new notion of
evenness is implied by the two earlier ones (since, by
[even_bool_prop] in chapter [Logic], we already know that
those are equivalent to each other). To show that all three
coincide, we just need the following lemma. *)
Lemma ev_Even_firsttry : forall n,
ev n -> Even n.
Proof.
(* WORKED IN CLASS *) unfold Even.
(** We could try to proceed by case analysis or induction on [n]. But
since [ev] is mentioned in a premise, this strategy seems
unpromising, because (as we've noted before) the induction
hypothesis will talk about [n-1] (which is _not_ even!). Thus, it
seems better to first try [inversion] on the evidence for [ev].
Indeed, the first case can be solved trivially. And we can
seemingly make progress on the second case with a helper lemma. *)
intros n E. inversion E as [EQ' | n' E' EQ'].
- (* E = ev_0 *) exists 0. reflexivity.
- (* E = ev_SS n' E'
Unfortunately, the second case is harder. We need to show [exists
n0, S (S n') = double n0], but the only available assumption is
[E'], which states that [ev n'] holds. Since this isn't directly
useful, it seems that we are stuck and that performing case
analysis on [E] was a waste of time.
If we look more closely at our second goal, however, we can see
that something interesting happened: By performing case analysis
on [E], we were able to reduce the original result to a similar
one that involves a _different_ piece of evidence for [ev]: namely
[E']. More formally, we could finish our proof if we could show
that
exists k', n' = double k',
which is the same as the original statement, but with [n'] instead
of [n]. Indeed, it is not difficult to convince Coq that this
intermediate result would suffice. *)
assert (H: (exists k', n' = double k')
-> (exists n0, S (S n') = double n0)).
{ intros [k' EQ'']. exists (S k'). simpl.
rewrite <- EQ''. reflexivity. }
apply H.
(** Unfortunately, now we are stuck. To see this clearly, let's
move [E'] back into the goal from the hypotheses. *)
generalize dependent E'.
(** Now it is obvious that we are trying to prove another instance
of the same theorem we set out to prove -- only here we are
talking about [n'] instead of [n]. *)
Abort.
(* ================================================================= *)
(** ** Induction on Evidence *)
(** If this story feels familiar, it is no coincidence: We
encountered similar problems in the [Induction] chapter, when
trying to use case analysis to prove results that required
induction. And once again the solution is... induction! *)
(** The behavior of [induction] on evidence is the same as its
behavior on data: It causes Coq to generate one subgoal for each
constructor that could have used to build that evidence, while
providing an induction hypothesis for each recursive occurrence of
the property in question.
To prove that a property of [n] holds for all even numbers (i.e.,
those for which [ev n] holds), we can use induction on [ev
n]. This requires us to prove two things, corresponding to the two
ways in which [ev n] could have been constructed. If it was
constructed by [ev_0], then [n=0] and the property must hold of
[0]. If it was constructed by [ev_SS], then the evidence of [ev n]
is of the form [ev_SS n' E'], where [n = S (S n')] and [E'] is
evidence for [ev n']. In this case, the inductive hypothesis says
that the property we are trying to prove holds for [n']. *)
(** Let's try proving that lemma again: *)
Lemma ev_Even : forall n,
ev n -> Even n.
Proof.
intros n E.
induction E as [|n' E' IH].
- (* E = ev_0 *)
unfold Even. exists 0. reflexivity.
- (* E = ev_SS n' E'
with IH : Even n' *)
unfold Even in IH.
destruct IH as [k Hk].
rewrite Hk.
unfold Even. exists (S k). simpl. reflexivity.
Qed.
(** Here, we can see that Coq produced an [IH] that corresponds
to [E'], the single recursive occurrence of [ev] in its own
definition. Since [E'] mentions [n'], the induction hypothesis
talks about [n'], as opposed to [n] or some other number. *)
(** The equivalence between the second and third definitions of
evenness now follows. *)
Theorem ev_Even_iff : forall n,
ev n <-> Even n.
Proof.
intros n. split.
- (* -> *) apply ev_Even.
- (* <- *) unfold Even. intros [k Hk]. rewrite Hk. apply ev_double.
Qed.
(** As we will see in later chapters, induction on evidence is a
recurring technique across many areas -- in particular for
formalizing the semantics of programming languages. *)
(** The following exercises provide simple examples of this
technique, to help you familiarize yourself with it. *)
(** **** Exercise: 2 stars, standard (ev_sum) *)
Theorem ev_sum : forall n m, ev n -> ev m -> ev (n + m).
Proof.
intros n m H.
generalize dependent m.
induction H as [|n E IH].
- intros m H. apply H.
- intros m H. simpl. apply IH in H.
apply ev_SS. apply H.
Qed.
(** [] *)
(** **** Exercise: 4 stars, advanced, optional (ev'_ev)
In general, there may be multiple ways of defining a
property inductively. For example, here's a (slightly contrived)
alternative definition for [ev]: *)
Inductive ev' : nat -> Prop :=
| ev'_0 : ev' 0
| ev'_2 : ev' 2
| ev'_sum n m (Hn : ev' n) (Hm : ev' m) : ev' (n + m).
(** Prove that this definition is logically equivalent to the old one.
To streamline the proof, use the technique (from the [Logic]
chapter) of applying theorems to arguments, and note that the same
technique works with constructors of inductively defined
propositions. *)
Theorem ev'_ev : forall n, ev' n <-> ev n.
Proof.
intros n. split.
- intros H. induction H as [| |n m Hn Hm IH1 IH2].
* apply ev_0.
* apply ev_SS. apply ev_0.
* apply ev_sum.
+ apply Hm.
+ apply IH2.
- intros H. induction H as [|n H IH].
* apply ev'_0.
* assert (A: (S (S n)) = n + 2).
{ rewrite add_comm. reflexivity. }
rewrite A. apply ev'_sum.
+ apply IH.
+ apply ev'_2.
Qed.
(** [] *)
(** **** Exercise: 3 stars, advanced, especially useful (ev_ev__ev) *)
Theorem ev_ev__ev : forall n m,
ev (n+m) -> ev n -> ev m.
(* Hint: There are two pieces of evidence you could attempt to induct upon
here. If one doesn't work, try the other. *)
Proof.
intros n m H1 H2.
generalize dependent m.
induction H2.
- intros m H. simpl in H. apply H.
- intros m H. rewrite PeanoNat.Nat.add_succ_comm in H.
rewrite PeanoNat.Nat.add_succ_comm in H.
apply IHev in H. apply evSS_ev. apply H.
Qed.
(** [] *)
(** **** Exercise: 3 stars, standard, optional (ev_plus_plus)
This exercise can be completed without induction or case analysis.
But, you will need a clever assertion and some tedious rewriting.
Hint: Is [(n+m) + (n+p)] even? *)
Theorem ev_plus_plus : forall n m p,
ev (n+m) -> ev (n+p) -> ev (m+p).
Proof.
intros n m p H1 H2.
assert (H : ev ((n+n)+(m+p))).
assert (H': (n+m)+(n+p) = (n+n)+(m+p)).
{ rewrite add_shuffle3.
rewrite add_assoc.
rewrite add_assoc.
rewrite add_assoc.
reflexivity. }
{ rewrite <- H'.
apply ev_sum with (m:=n+p) in H1.
- rewrite H' in H1. rewrite H'. apply H1.
- apply H2. }
apply ev_ev__ev with (n:=n+n) in H.
- apply H.
- apply ev_Even_iff. unfold Even. exists n.
rewrite double_plus. reflexivity.
Qed.
(** [] *)
(* ################################################################# *)
(** * Inductive Relations *)
(** A proposition parameterized by a number (such as [ev])
can be thought of as a _property_ -- i.e., it defines
a subset of [nat], namely those numbers for which the proposition
is provable. In the same way, a two-argument proposition can be
thought of as a _relation_ -- i.e., it defines a set of pairs for
which the proposition is provable. *)
Module Playground.
(** Just like properties, relations can be defined inductively. One
useful example is the "less than or equal to" relation on numbers
that we briefly saw above. *)
Inductive le : nat -> nat -> Prop :=
| le_n (n : nat) : le n n
| le_S (n m : nat) (H : le n m) : le n (S m).
Notation "n <= m" := (le n m).
(** (We've written the definition a bit differently this time,
giving explicit names to the arguments to the constructors and
moving them to the left of the colons.) *)
(** Proofs of facts about [<=] using the constructors [le_n] and
[le_S] follow the same patterns as proofs about properties, like
[ev] above. We can [apply] the constructors to prove [<=]
goals (e.g., to show that [3<=3] or [3<=6]), and we can use
tactics like [inversion] to extract information from [<=]
hypotheses in the context (e.g., to prove that [(2 <= 1) ->
2+2=5].) *)
(** Here are some sanity checks on the definition. (Notice that,
although these are the same kind of simple "unit tests" as we gave
for the testing functions we wrote in the first few lectures, we
must construct their proofs explicitly -- [simpl] and
[reflexivity] don't do the job, because the proofs aren't just a
matter of simplifying computations.) *)
Theorem test_le1 :
3 <= 3.
Proof.
(* WORKED IN CLASS *)
apply le_n. Qed.
Theorem test_le2 :
3 <= 6.
Proof.
(* WORKED IN CLASS *)
apply le_S. apply le_S. apply le_S. apply le_n. Qed.
Theorem test_le3 :
(2 <= 1) -> 2 + 2 = 5.
Proof.
(* WORKED IN CLASS *)
intros H. inversion H. inversion H2. Qed.
(** The "strictly less than" relation [n < m] can now be defined
in terms of [le]. *)
Definition lt (n m : nat) := le (S n) m.
Notation "n < m" := (lt n m).
End Playground.
(** **** Exercise: 2 stars, standard, optional (total_relation)
Define an inductive binary relation [total_relation] that holds
between every pair of natural numbers. *)
Inductive total_relation : nat -> nat -> Prop :=
| total_rel (n m : nat) : total_relation n m.
Theorem total_relation_is_total : forall n m, total_relation n m.
Proof.
intros n m. apply total_rel.
Qed.
(** [] *)
(** **** Exercise: 2 stars, standard, optional (empty_relation)
Define an inductive binary relation [empty_relation] (on numbers)
that never holds. *)
Inductive empty_relation : nat -> nat -> Prop :=.
Theorem empty_relation_is_empty : forall n m, ~ empty_relation n m.
Proof.
intros n m. unfold not. intro H.
destruct H.
Qed.
(** [] *)
(** From the definition of [le], we can sketch the behaviors of
[destruct], [inversion], and [induction] on a hypothesis [H]
providing evidence of the form [le e1 e2]. Doing [destruct H]
will generate two cases. In the first case, [e1 = e2], and it
will replace instances of [e2] with [e1] in the goal and context.
In the second case, [e2 = S n'] for some [n'] for which [le e1 n']
holds, and it will replace instances of [e2] with [S n'].
Doing [inversion H] will remove impossible cases and add generated
equalities to the context for further use. Doing [induction H]
will, in the second case, add the induction hypothesis that the
goal holds when [e2] is replaced with [n']. *)
(** Here are a number of facts about the [<=] and [<] relations that
we are going to need later in the course. The proofs make good
practice exercises. *)
(** **** Exercise: 5 stars, standard, optional (le_and_lt_facts) *)
Lemma le_trans : forall m n o, m <= n -> n <= o -> m <= o.
Proof.
intros m n o H1 H2.
induction H2 as [|n' m' IH].
- apply H1.
- apply le_S. apply IH.
Qed.
Theorem O_le_n : forall n,
0 <= n.
Proof.
induction n as [|n' IH].
- apply le_n.
- apply le_S. apply IH.
Qed.
Theorem n_le_m__Sn_le_Sm : forall n m,
n <= m -> S n <= S m.
Proof.
intros n m H.
induction H as [|n' m' IH].
- apply le_n.
- apply le_S. apply IH.
Qed.
Theorem Sn_le_Sm__n_le_m : forall n m,
S n <= S m -> n <= m.
Proof.
intros n m H.
(* generalize dependent m. *)
induction m.
- inversion H.
+ apply le_n.
+ inversion H1.
- inversion H.
+ apply le_n.
+ apply IHm in H1. apply le_S. apply H1.
Qed.
Theorem lt_ge_cases : forall n m,
n < m \/ n >= m.
Proof.
intros n m.
induction m.
- right. destruct n.
+ apply le_n.
+ apply le_S. apply O_le_n.
- destruct IHm as [H1|H2].
+ left. apply le_S. apply H1.
+ destruct H2.
* left. unfold lt. apply le_n.
* right. apply n_le_m__Sn_le_Sm. apply H2.
Qed.
Theorem le_plus_l : forall a b,
a <= a + b.
Proof.
intros a b.
induction b as [|b' IH].
- rewrite add_0_r. apply le_n.
- rewrite <- plus_n_Sm. apply le_S. apply IH.
Qed.
Theorem plus_le : forall n1 n2 m,
n1 + n2 <= m ->
n1 <= m /\ n2 <= m.
Proof.
intros n1 n2 m H.
induction H.
- split.
+ induction n2.
* rewrite add_0_r. apply le_n.
* rewrite <- plus_n_Sm. apply le_S. apply IHn2.
+ induction n1.
* rewrite add_comm. rewrite add_0_r. apply le_n.
* rewrite add_comm. rewrite <- plus_n_Sm. apply le_S.
rewrite add_comm. apply IHn1.
- destruct IHle. split.
+ apply le_S. apply H0.
+ apply le_S. apply H1.
Qed.
Lemma lt_n_m : forall n m p,
(n+m) <= p -> n <= p.
Proof.
induction n.
- intros. apply O_le_n.
- intros. inversion H.
+ simpl. apply n_le_m__Sn_le_Sm. apply le_plus_l.
+ apply n_le_m__Sn_le_Sm. simpl in H0. rewrite plus_n_Sm in H0.
apply IHn in H0. apply H0.
Qed.
Theorem add_le_cases : forall n m p q,
n + m <= p + q -> n <= p \/ m <= q.
(** Hint: May be easiest to prove by induction on [n]. *)
Proof.
intros n. induction n.
- left. apply O_le_n.
- intros. destruct p.
+ replace (0+q) with (q) in H. right.
rewrite add_comm in H. apply lt_n_m in H. apply H.
{ reflexivity. }
+ simpl in H. apply Sn_le_Sm__n_le_m in H.
apply IHn in H. destruct H.
* left. apply n_le_m__Sn_le_Sm. apply H.
* right. apply H.
Qed.
Theorem plus_le_compat_l : forall n m p,
n <= m ->
p + n <= p + m.
Proof.
intros n m p H.
induction H.
- apply le_n.
- rewrite <- plus_n_Sm. apply le_S. apply IHle.
Qed.
Theorem plus_le_compat_r : forall n m p,
n <= m ->
n + p <= m + p.
Proof.
intros n m p H.
induction H.