-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndProp.v
1479 lines (1189 loc) · 49 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".
From COC Require Export Logic.
From Coq Require Export Lia.
(* ################################################################# *)
(** * 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: _inductive definitions_.
_Note_: For the sake of simplicity, most of this chapter uses an
inductive definition of "evenness" as a running example. You may
find this confusing, since we already have a perfectly good way of
defining evenness as a proposition ([n] is even if it is equal to
the result of doubling some number); if so, rest assured that we
will see many more compelling examples of inductively defined
propositions toward the end of this chapter and in future
chapters. *)
(** In past chapters, we have seen two ways of stating that a number
[n] is even: We can say
(1) [evenb n = true], or
(2) [exists k, n = double k].
Yet another possibility is to say that [n] is even if we can
establish its evenness from the following rules:
- Rule [ev_0]: The number [0] is even.
- Rule [ev_SS]: If [n] is even, then [S (S n)] is even. *)
(** To illustrate how this new definition of evenness works,
let's imagine using it to show that [4] is even. 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 will see many definitions like this one during the rest
of the course. For purposes of informal discussions, it is
helpful to have a lightweight notation that makes them easy to
read and write. _Inference rules_ are one such notation. (We'll
use [ev] for the name of this property, since [even] is already
used.)
------------ (ev_0)
ev 0
ev n
---------------- (ev_SS)
ev (S (S n))
*)
(** Each of the textual rules that we started with is
reformatted here as an inference rule; the intended reading is
that, if the _premises_ above the line all hold, then the
_conclusion_ below the line follows. For example, the rule
[ev_SS] says that, if [n] satisfies [ev], then [S (S n)] also
does. If a rule has no premises above the line, then its
conclusion holds unconditionally.
We can represent a proof using these rules by combining rule
applications into a _proof tree_. Here's how we might transcribe
the above proof that [4] is even:
-------- (ev_0)
ev 0
-------- (ev_SS)
ev 2
-------- (ev_SS)
ev 4
*)
(** (Why call this a "tree", rather than a "stack", for example?
Because, in general, inference rules can have multiple premises.
We will see examples of this shortly.) *)
(* ================================================================= *)
(** ** Inductive Definition of Evenness *)
(** Putting all of this together, we can translate the definition of
evenness into a formal Coq definition using an [Inductive]
declaration, where each constructor corresponds to an inference
rule: *)
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).
This definition introduces the [X] parameter _globally_, to the
_left_ of the colon, forcing the result of [nil] and [cons] to be
the same (i.e., [list X]). Had we 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; in [Inductive ev : nat -> Prop := ...], the unnamed
[nat] argument is an index. *)
(** We can think of the definition of [ev] 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))]. *)
(** Such "evidence constructors" have the same status as proven
theorems. In particular, we can use Coq's [apply] tactic with the
rule names to prove [ev] for 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: *)
Theorem ev_4' : ev 4.
Proof. apply (ev_SS 2 (ev_SS 0 ev_0)). Qed.
(** 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.
(** More generally, we can show that any number multiplied by 2 is even: *)
(** **** Exercise: 1 star (ev_double) *)
Theorem ev_double : forall n,
ev (double n).
Proof.
intros n. rewrite double_plus. induction n as [| n'].
- (* n = O *) simpl. apply ev_0.
- (* n = S n' *)simpl. rewrite <- plus_n_Sm. apply ev_SS. apply IHn'.
Qed.
(* ################################################################# *)
(** * Using Evidence in Proofs *)
(** Besides _constructing_ evidence that numbers are even, we can also
_destruct_ such evidence, which amounts to reasoning about how it
could have been built.
Introducing [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
_induction_ and _case analysis_ 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 that [ev n] _directly_. As
a tool, we can prove our characterization of evidence for
[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.
(** The following theorem can easily be proved using [destruct] on
evidence. *)
Theorem ev_minus2 : forall n,
ev n -> ev (pred (pred n)).
Proof.
intros n E.
destruct E as [| n' E'] eqn:EE.
- (* E = ev_0 *) simpl. apply ev_0.
- (* E = ev_SS n' E' *) simpl. apply E'.
Qed.
(** However, this variation cannot easily be handled with just
[destruct]. *)
Theorem evSS_ev : forall n,
ev (S (S n)) -> ev n.
(** Intuitively, we know that evidence for the hypothesis cannot
consist just of the [ev_0] constructor, since [O] and [S] are
different constructors of the type [nat]; hence, [ev_SS] is the
only case that applies. Unfortunately, [destruct] is not smart
enough to realize this, and it still generates two subgoals. Even
worse, in doing so, it keeps the final goal unchanged, failing to
provide any useful information for completing the proof. *)
Proof.
intros n E.
destruct E as [| n' E'] eqn:EE.
- (* E = ev_0. *)
(* We must prove that [n] is even from no assumptions! *)
Abort.
(** What happened, exactly? Calling [destruct] has the effect of
replacing all occurrences of the property argument by the values
that correspond to each constructor. This is enough in the case
of [ev_minus2] because that argument [n] is mentioned directly
in the final goal. However, it doesn't help in the case of
[evSS_ev] since the term that gets replaced ([S (S n)]) is not
mentioned anywhere. *)
(** If we [remember] that term [S (S n)], the proof goes
through. (We'll discuss [remember] in more detail below.) *)
Theorem evSS_ev_remember : forall n,
ev (S (S n)) -> ev n.
Proof.
intros n E. remember (S (S n)) as k eqn:Hk. destruct E as [|n' E'] eqn:EE.
- (* E = ev_0 *)
(* Now we do have an assumption, in which [k = S (S n)] has been
rewritten as [0 = S (S n)] by [destruct]. That assumption
gives us a contradiction. *)
discriminate Hk.
- (* E = ev_S n' E' *)
(* This time [k = S (S n)] has been rewritten as [S (S n') = S (S n)]. *)
injection Hk as Heq. rewrite <- Heq. apply E'.
Qed.
(** Alternatively, the proof is straightforward using our inversion
lemma. *)
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].
- (* The first subgoal is a contradiction that is discharged with discriminate *)
discriminate H0.
- (* The second subgoal makes use of injection and rewrite *)
destruct H1 as [n' [Hnm Hev]]. injection Hnm as Heq.
rewrite -> Heq. apply Hev.
Qed.
(** Note how both proofs produce 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 that common
pattern.
The [inversion] tactic can detect (1) that the first case ([n =
0]) does not apply and (2) that the [n'] that appears in the
[ev_SS] case must be the same as [n]. It has an "[as]" variant
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. For example: *)
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.
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 E. inversion E as [|n' E' H'].
inversion E' as [|n'' E'' H'']. apply E''.
Qed.
(** **** Exercise: 1 star, standard (ev5_nonsense)
Prove the following result using [inversion]. *)
Theorem ev5_nonsense :
ev 5 -> 2 + 2 = 9.
Proof.
intros E. inversion E as [|n' E' H']. inversion E' as [|n'' E'' H'']. inversion E''.
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, more generally,
to analyze evidence for inductively defined propositions. As
examples, we'll use it to reprove 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 exact, 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 all equations into the proof context that must hold of the
arguments given to [P] (e.g., [S (S 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 *)
(** We could try to proceed by case analysis or induction on [n]. But
since [ev] is mentioned in a premise, this strategy would
probably lead to a dead end, 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' *) simpl.
(** Unfortunately, the second case is harder. We need to show [exists
k, S (S n') = double k], 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 can finish our proof by showing
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 suffices. *)
(** Unforunately, now we are stuck. To make that apparent, let's move
[E'] back into the goal from the hypotheses. *)
generalize dependent E'.
(** Now it is clear we are trying to prove another instance of the
same theorem we set out to prove. This instance is with [n'],
instead of [n], where [n'] is a smaller natural number than [n]. *)
Abort.
(* ================================================================= *)
(** ** Induction on Evidence *)
(** If this looks familiar, it is no coincidence: We've 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 a property of [n] holds for all numbers 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 our current lemma again: *)
Lemma ev_even : forall n,
ev n -> even n.
Proof.
intros n E.
induction E as [|n' E' IH].
- (* E = ev_0 *)
exists 0. reflexivity.
- (* E = ev_SS n' E'
with IH : even E' *)
unfold even in IH.
destruct IH as [k Hk].
rewrite Hk. 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, and in particular when
formalizing the semantics of programming languages, where many
properties of interest are defined inductively. *)
(** 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 En Em. induction En as [|n' En'].
- simpl. assumption.
- simpl. apply ev_SS. assumption.
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 [Logic]) 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 E. induction E.
+ apply ev_0.
+ apply ev_SS. apply ev_0.
+ apply ev_sum. assumption. assumption.
- (* <- *) intros E. induction E.
+ apply ev'_0.
+ assert (S (S n) = 2 + n) as H.
{ induction n.
- reflexivity.
- reflexivity. }
rewrite H. apply ev'_sum.
{ apply ev'_2. }
{ assumption. }
Qed.
(** **** Exercise: 3 stars, advanced, especially useful (ev_ev__ev)
There are two pieces of evidence you could attempt to induct upon
here. If one doesn't work, try the other. *)
Theorem ev_ev__ev : forall n m,
ev (n+m) -> ev n -> ev m.
Proof.
intros n m H0 H1. induction H1.
- assumption.
- apply IHev.
assert (S (S n) + m = S (S (n + m))) as H.
{ reflexivity. }
rewrite H in H0. apply evSS_ev in H0. assumption.
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 Hnm Hnp.
assert (ev (n+m) -> ev ((n+p)+(m+p))) as H.
{ intros H. rewrite (plus_comm n p).
rewrite (plus_comm m p). rewrite plus_swap.
rewrite plus_assoc. rewrite plus_assoc.
rewrite <- double_plus.
rewrite <- (plus_assoc (double p) n m).
apply ev_sum. apply ev_double. apply Hnm.
}
apply H in Hnm. apply (ev_ev__ev (n+p)). assumption. assumption.
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. *)
(** The following definition should be fairly intuitive. It
says that there are two ways to give evidence that one number is
less than or equal to another: either observe that they are the
same number, or give evidence that the first is less than or equal
to the predecessor of the second. *)
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).
(** 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 "m < n" := (lt m n).
End Playground.
(** Here are a few more simple relations on numbers: *)
Inductive square_of : nat -> nat -> Prop :=
| sq n : square_of n (n * n).
Inductive next_nat : nat -> nat -> Prop :=
| nn n : next_nat n (S n).
Inductive next_ev : nat -> nat -> Prop :=
| ne_1 n (H: ev (S n)) : next_ev n (S n)
| ne_2 n (H: ev (S (S n))) : next_ev n (S (S n)).
(** **** Exercise: 2 stars, recommended (total_relation) *)
(** Define an inductive binary relation [total_relation] that holds
between every pair of natural numbers. *)
Inductive total_relation : nat -> nat -> Prop :=
tr_nm : forall (n m : nat), total_relation n m.
Lemma tr_test : forall n m, total_relation n m.
Proof.
intros n m. apply tr_nm.
Qed.
(** **** Exercise: 2 stars (empty_relation) *)
(** Define an inductive binary relation [empty_relation] (on numbers)
that never holds. *)
Inductive empty_relation : nat -> nat -> Prop :=
er_nm : forall (n m : nat), n = m /\ n <> m -> empty_relation n m.
Lemma er_test : forall n m, ~ empty_relation n m.
Proof.
intros n m. unfold not. intros H.
inversion H. inversion H0. rewrite H3 in H4. unfold not in H4. apply H4. reflexivity.
Qed.
(** **** Exercise: 3 stars, optional (le_exercises) *)
(** 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. *)
Lemma le_trans : forall m n o,
m <= n -> n <= o -> m <= o.
Proof.
intros m n o Hmn Hno. induction Hno as [|n' o'].
- apply Hmn.
- apply le_S. apply IHo'.
Qed.
Theorem O_le_n : forall n,
0 <= n.
Proof.
intros n. induction n.
- apply le_n.
- apply le_S. assumption.
Qed.
Theorem n_le_m__Sn_le_Sm : forall n m,
n <= m -> S n <= S m.
Proof.
intros n m H. induction H.
- apply le_n.
- apply le_S. apply IHle.
Qed.
Theorem Sn_le_Sm__n_le_m : forall n m,
S n <= S m -> n <= m.
Proof.
intros n m H. inversion H.
- apply le_n.
- apply (le_trans n (S n)).
+ apply le_S. apply le_n.
+ apply H1.
Qed.
Theorem le_plus_l : forall a b,
a <= a + b.
Proof.
intros a b. induction b.
- rewrite <- plus_n_O. apply le_n.
- rewrite <- plus_n_Sm. apply le_S. assumption.
Qed.
Theorem plus_lt : forall n1 n2 m,
n1 + n2 < m ->
n1 < m /\ n2 < m.
Proof.
unfold lt. intros n1 n2 m H.
split.
- apply (le_trans (S n1) (S (n1 + n2))).
+ apply n_le_m__Sn_le_Sm. apply le_plus_l.
+ assumption.
- apply (le_trans (S n2) (S (n1 + n2))).
+ apply n_le_m__Sn_le_Sm. rewrite plus_comm. apply le_plus_l.
+ assumption.
Qed.
(** Hint: the next one may be easiest to prove by induction on [n]. *)
Theorem add_le_cases : forall n m p q,
n + m <= p + q -> n <= p \/ m <= q.
Proof. Admitted.
Theorem lt_S : forall n m,
n < m ->
n < S m.
Proof.
unfold lt. intros n m H.
apply le_S. assumption.
Qed.
Theorem Sn_leb_Sm__n_leb_m : forall n m,
leb (S n) (S m) = true -> leb n m = true.
Proof.
intros n m H. inversion H. reflexivity.
Qed.
Theorem leb_complete : forall n m,
n <=? m = true -> n <= m.
Proof.
intros n m H. generalize dependent n. induction m.
- (* m = O *) intros n H. induction n.
+ (* n = O *) apply le_n.
+ (* n = S n' *) inversion H.
- (* m = S m' *) intros n H. induction n.
+ (* n = O *) apply O_le_n.
+ (* n = S n' *) apply Sn_leb_Sm__n_leb_m in H. apply IHm in H. apply n_le_m__Sn_le_Sm. assumption.
Qed.
(** Hint: The next one may be easiest to prove by induction on [m]. *)
Lemma n_leb_m__Sn_leb_Sm : forall n m,
n <=? m = true -> (S n) <=? (S m) = true.
Proof.
intros n m H. inversion H. reflexivity.
Qed.
Lemma n_leb_m__n_leb_Sm :
forall n m : nat, n <=? m = true -> n <=? (S m) = true.
Proof.
intros n m H. generalize dependent m. induction n.
- intros m H. simpl. reflexivity.
- intros m H. induction m.
+ inversion H.
+ apply n_leb_m__Sn_leb_Sm. apply IHn. apply Sn_leb_Sm__n_leb_m in H. assumption.
Qed.
Theorem leb_correct : forall n m,
n <= m ->
n <=? m = true.
Proof.
intros n m H. generalize dependent n. induction m.
- intros n H. inversion H. rewrite <- leb_refl. reflexivity.
- intros n H. inversion H.
+ rewrite <- leb_refl. reflexivity.
+ apply IHm in H1. apply n_leb_m__n_leb_Sm. assumption.
Qed.
(** Hint: The next one can easily be proved without using [induction]. *)
Theorem leb_true_trans : forall n m o,
n <=? m = true -> m <=? o = true -> n <=? o = true.
Proof.
intros n m o Hnm Hmo.
apply leb_correct. apply leb_complete in Hnm. apply leb_complete in Hmo.
apply (le_trans n m). assumption. assumption.
Qed.
Module R.
(** **** Exercise: 3 stars, standard, especially useful (R_provability)
We can define three-place relations, four-place relations,
etc., in just the same way as binary relations. For example,
consider the following three-place relation on numbers: *)
Inductive R : nat -> nat -> nat -> Prop :=
| c1 : R 0 0 0
| c2 m n o (H : R m n o) : R (S m) n (S o)
| c3 m n o (H : R m n o) : R m (S n) (S o)
| c4 m n o (H : R (S m) (S n) (S (S o))) : R m n o
| c5 m n o (H : R m n o) : R n m o.
(** - Which of the following propositions are provable?
- [R 1 1 2]
- [R 2 2 6] **)
(* [R 1 1 2] -> provable *)
Theorem R_1: R 1 1 2.
Proof. apply c2. apply c3. apply c1. Qed.
(* [R 2 2 6] -> non provable *)
Theorem R_2: R 2 2 6.
Proof. apply c3. apply c2. apply c2. apply c3. Abort.
(** - If we dropped constructor [c5] from the definition of [R],
would the set of provable propositions change? Briefly (1
sentence) explain your answer.
The set of provable propositions wouldn't change since
applying the rule constructed by c5 doesn't change the goal
in any form.**)
(** - If we dropped constructor [c4] from the definition of [R],
would the set of provable propositions change? Briefly (1
sentence) explain your answer.
The set of provable propositions would change since
when applied, the rule constructed by c4, the goal is changed **)
(** **** Exercise: 3 stars, standard, optional (R_fact)
The relation [R] above actually encodes a familiar function.
Figure out which function; then state and prove this equivalence
in Coq? *)
Definition fR : nat -> nat -> nat
(* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.
Theorem R_equiv_fR : forall m n o, R m n o <-> fR m n = o.
Proof.
(* FILL IN HERE *) Admitted.
(** [] *)
End R.
(** **** Exercise: 4 stars, advanced (subsequence)
A list is a _subsequence_ of another list if all of the elements
in the first list occur in the same order in the second list,
possibly with some extra elements in between. For example,
[1;2;3]
is a subsequence of each of the lists
[1;2;3]
[1;1;1;2;2;3]
[1;2;7;3]
[5;6;1;9;9;2;7;3;8]
but it is _not_ a subsequence of any of the lists
[1;2]
[1;3]
[5;6;2;1;7;3;8].
- Define an inductive proposition [subseq] on [list nat] that
captures what it means to be a subsequence. (Hint: You'll need
three cases.)
- Prove [subseq_refl] that subsequence is reflexive, that is,
any list is a subsequence of itself.
- Prove [subseq_app] that for any lists [l1], [l2], and [l3],
if [l1] is a subsequence of [l2], then [l1] is also a subsequence
of [l2 ++ l3].
- (Optional, harder) Prove [subseq_trans] that subsequence is
transitive -- that is, if [l1] is a subsequence of [l2] and [l2]
is a subsequence of [l3], then [l1] is a subsequence of [l3].
Hint: choose your induction carefully! *)
Notation "x :: l" := (cons x l) (at level 60, right associativity).
Notation "[ ]" := nil.
Notation "[ x ; .. ; y ]" := (cons x .. (cons y nil) ..).
Notation "x ++ y" := (app x y).
Inductive subseq {X: Type}: list X -> list X -> Prop :=
| empty l : subseq [] l
| removeone x l1 l2 : subseq l1 l2 -> subseq l1 (x :: l2)
| removetwo x l1 l2 : subseq l1 l2 -> subseq (x :: l1) (x :: l2).
(* First use of the tactic [repeat] *)
Example subseq_ex1: subseq [1;2;3] [1;2;3].
Proof.
repeat apply removetwo. apply empty.
Qed.
Example subseq_ex2: subseq [1;2;3] [1;1;1;2;2;3].
Proof.
apply removeone. apply removeone. repeat apply removetwo. apply removeone. apply removetwo. apply empty.
Qed.
Example subseq_ex3: subseq [1;2;3] [1;2;7;3].
Proof.
repeat apply removetwo. apply removeone. apply removetwo. apply empty.
Qed.
Example subseq_ex4: subseq [1;2;3] [5;6;1;9;9;2;7;3;8].
Proof.
apply removeone.
apply removeone.
apply removetwo.
apply removeone.
apply removeone.
apply removetwo.
apply removeone.
apply removetwo.
apply empty.
Qed.
Example subseq_ex5: ~ subseq [1;2;3] [1;2].
Proof.
intro H. repeat match goal with [H: subseq _ _ |- _] => inversion_clear H end.
Qed.
Example subseq_ex6: ~ subseq [1;2;3] [1;3].
Proof.
intro H; repeat match goal with [H: subseq _ _ |- _] => inversion_clear H end.
Qed.
Example subseq_ex7: ~ subseq [1;2;3] [5;6;2;1;7;3;8].
Proof.
intro H; repeat match goal with [H: subseq _ _ |- _] => inversion_clear H end.
Qed.
Theorem subseq_refl : forall (l : list nat), subseq l l.
Proof.
intros. induction l as [| n l' IHl'].
- (* l = nil *) apply empty.
- (* l = cons n l' *) apply removetwo. apply IHl'.
Qed.
Lemma subseq_app: forall X (l1 l2 l3: list X)
(SUB: subseq l1 l2),
subseq l1 (l2++l3).
Proof.
intros X l1 l2 l3 SUB.
induction SUB.
- apply empty.
- assert ((x :: l2) ++ l3 = x :: (l2 ++ l3)) as H. { reflexivity. }
rewrite -> H. apply removeone. apply IHSUB.
- assert ((x :: l2) ++ l3 = x :: (l2 ++ l3)) as H. { reflexivity. }
rewrite -> H. apply removetwo. apply IHSUB.
Qed.
Theorem subseq_trans : forall (l1 l2 l3 : list nat),
subseq l1 l2 ->
subseq l2 l3 ->
subseq l1 l3.
Proof.
(* FILL IN HERE *) Admitted.
(** **** Exercise: 2 stars, standard, optional (R_provability2)
Suppose we give Coq the following definition: