-
Notifications
You must be signed in to change notification settings - Fork 8
/
IndProp.v
2599 lines (2188 loc) · 85.5 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 LF Require Export Logic.
Require Coq.omega.Omega.
(* ################################################################# *)
(** * Inductively Defined Propositions *)
(** In the [Logic] chapter, we looked at several ways of writing
propositions, including conjunction, disjunction, and quantifiers.
In this chapter, we bring a new tool into the mix: _inductive
definitions_. *)
(** Recall that 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 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: *)
(**
------------ (ev_0)
ev 0
ev n
-------------- (ev_SS)
ev (S (S n))
*)
(** Each of the textual rules above 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 below. *)
(** 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 : forall n : nat, ev n -> ev (S (S n)).
(** This definition is different in one crucial respect from
previous uses of [Inductive]: its result is not a [Type], but
rather a function from [nat] to [Prop] -- that is, a property of
numbers. Note that we've already seen other inductive definitions
that result in functions, such as [list], whose type is [Type ->
Type]. What is new here is that, because the [nat] argument of
[ev] appears _unnamed_, to the _right_ of the colon, 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].
In contrast, the definition of [list] names the [X] parameter
_globally_, to the _left_ of the colon, forcing the result of
[nil] and [cons] to be the same ([list X]). Had we tried to bring
[nat] to the left 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 : forall n, wrong_ev n -> wrong_ev (S (S n)).
(* ===> Error: A parameter of an inductive type n is not
allowed to be used as a bound variable in the type
of its constructor. *)
(** ("Parameter" here is Coq jargon for an argument on the left of the
colon in an [Inductive] definition; "index" is used to refer to
arguments on the right of the colon.) *)
(** We can think of the definition of [ev] as defining a Coq property
[ev : nat -> Prop], together with primitive theorems [ev_0 : ev 0] and
[ev_SS : forall n, ev n -> ev (S (S n))]. *)
(** Such "constructor theorems" 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.
induction n as [|n' IHn].
- apply ev_0.
- simpl. apply ev_SS. apply IHn.
Qed.
(** [] *)
(* ################################################################# *)
(** * Using Evidence in Proofs *)
(** Besides _constructing_ evidence that numbers are even, we can also
_reason about_ such evidence.
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 even, but also that these two
constructors are the _only_ ways to build evidence that numbers
are even (in the sense of [ev]). *)
(** In other words, if someone gives us evidence [E] for the assertion
[ev n], then we know that [E] must have one of two shapes:
- [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 the [inversion] tactic, 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_.
By the definition of [ev], there are two cases to consider:
- If the evidence is of the form [ev_0], we know that [n = 0].
- Otherwise, the evidence must have the form [ev_SS n' E'], where
[n = S (S n')] and [E'] is evidence for [ev n']. *)
(** We can perform this kind of reasoning in Coq, again using
the [inversion] tactic. Besides allowing us to reason about
equalities involving constructors, [inversion] provides a
case-analysis principle for inductively defined propositions.
When used in this way, its syntax is similar to [destruct]: We
pass it a list of identifiers separated by [|] characters to name
the arguments to each of the possible constructors. *)
Theorem ev_minus2 : forall n,
ev n -> ev (pred (pred n)).
Proof.
intros n E.
inversion E as [| n' E'].
- (* E = ev_0 *) simpl. apply ev_0.
- (* E = ev_SS n' E' *) simpl. apply E'. Qed.
(** In words, here is how the inversion reasoning works in this proof:
- If the evidence is of the form [ev_0], we know that [n = 0].
Therefore, it suffices to show that [ev (pred (pred 0))] holds.
By the definition of [pred], this is equivalent to showing that
[ev 0] holds, which directly follows from [ev_0].
- Otherwise, the evidence must have the form [ev_SS n' E'], where
[n = S (S n')] and [E'] is evidence for [ev n']. We must then
show that [ev (pred (pred (S (S n'))))] holds, which, after
simplification, follows directly from [E']. *)
(** This particular proof also works if we replace [inversion] by
[destruct]: *)
Theorem ev_minus2' : forall n,
ev n -> ev (pred (pred n)).
Proof.
intros n E.
destruct E as [| n' E'].
- (* E = ev_0 *) simpl. apply ev_0.
- (* E = ev_SS n' E' *) simpl. apply E'. Qed.
(** The difference between the two forms is that [inversion] is more
convenient when used on a hypothesis that consists of an inductive
property applied to a complex expression (as opposed to a single
variable). Here's is a concrete example. Suppose that we wanted
to prove the following variation of [ev_minus2]: *)
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'].
- (* 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. *)
(** The [inversion] tactic, on the other hand, can detect (1) that the
first case does not apply, and (2) that the [n'] that appears on
the [ev_SS] case must be the same as [n]. This allows us to
complete the proof: *)
Theorem evSS_ev : forall n,
ev (S (S n)) -> ev n.
Proof.
intros n E.
inversion E as [| n' E'].
(* We are in the [E = ev_SS n' E'] case now. *)
apply E'.
Qed.
(** By using [inversion], we can also apply the principle of explosion
to "obviously contradictory" hypotheses involving inductive
properties. For example: *)
Theorem one_not_even : ~ ev 1.
Proof.
intros H. inversion H. Qed.
(** **** Exercise: 1 star (SSSSev__even) *)
(** Prove the following result using [inversion]. *)
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 (even5_nonsense) *)
(** Prove the following result using [inversion]. *)
Theorem even5_nonsense :
ev 5 -> 2 + 2 = 9.
Proof.
intros H.
inversion H.
inversion H1.
inversion H3.
Qed.
(** [] *)
(** The way we've used [inversion] here may seem a bit
mysterious at first. Until now, we've only used [inversion] on
equality propositions, to utilize injectivity of constructors or
to discriminate between different constructors. But we see here
that [inversion] can also be applied to analyzing evidence for
inductively defined propositions.
Here's how [inversion] works in general. Suppose the name [I]
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 I] generates a subgoal in which
[I] 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 -> exists k, n = double k.
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, as in the previous section. Thus, it seems
better to first try inversion on the evidence for [ev]. Indeed,
the first case can be solved trivially. *)
intros n E. inversion E as [| n' E'].
- (* 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 an similar
one that involves a _different_ piece of evidence for [ev]: [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. *)
assert (I : (exists k', n' = double k') ->
(exists k, S (S n') = double k)).
{ intros [k' Hk']. rewrite Hk'. exists (S k'). reflexivity. }
apply I. (* reduce the original goal to the new one *)
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 hypotheses for each recursive occurrence of
the property in question. *)
(** Let's try our current lemma again: *)
Lemma ev_even : forall n,
ev n -> exists k, n = double k.
Proof.
intros n E.
induction E as [|n' E' IH].
- (* E = ev_0 *)
exists 0. reflexivity.
- (* E = ev_SS n' E'
with IH : exists k', n' = double k' *)
destruct IH as [k' Hk'].
rewrite Hk'. exists (S k'). 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 <-> exists k, n = double k.
Proof.
intros n. split.
- (* -> *) apply ev_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 (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' IHen].
- (* ev n = ev_0 *) apply EM.
- (* ev n = ev_SS n' EN' *)
simpl. apply ev_SS. apply IHen.
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 : forall n m, ev' n -> ev' m -> ev' (n + m).
(** Prove that this definition is logically equivalent to the old
one. (You may want to look at the previous theorem when you get
to the induction step.) *)
Theorem ev'_ev : forall n, ev' n <-> ev n.
Proof.
split.
- (* -> *) intro EN.
induction EN.
+ (* ev 0 *) apply ev_0.
+ (* ev 2 *) apply ev_SS. apply ev_0.
+ (* ev (n + m) *) apply ev_sum.
* (* ev n *) apply IHEN1.
* (* ev m *) apply IHEN2.
- (* -> *) intro EN.
induction EN as [|n' EN' IHen].
+ (* ev' 0 *) apply ev'_0.
+ (* ev' (S (S n')) *) assert (H : (S (S n') = 2 + n')).
{ reflexivity. }
rewrite -> H. apply ev'_sum.
* (* ev' 2 *)apply ev'_2.
* (* ev' n *) apply IHen.
Qed.
(** [] *)
(** **** Exercise: 3 stars, advanced, recommended (ev_ev__ev) *)
(** Finding the appropriate thing to do induction on is a
bit tricky here: *)
Theorem ev_ev__ev : forall n m,
ev (n+m) -> ev n -> ev m.
Proof.
intros n m Enm En.
generalize dependent Enm.
induction En.
- intro H. apply H.
- simpl. intro H. apply ev_SS in IHEn.
+ inversion IHEn. apply H1.
+ inversion H. apply H1.
Qed.
(** [] *)
(** **** Exercise: 3 stars, optional (ev_plus_plus) *)
(** This exercise just requires applying existing lemmas. No
induction or even case analysis is needed, though some of the
rewriting may be tedious. *)
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 (HH : (ev (n + m) -> ev ((n + p) + (m + p)))).
{
intro H.
Check plus_assoc.
(*
plus_assoc : forall n m p : nat, n + (m + p) = (n + m) + p
*)
Check plus_comm.
(*
plus_comm : forall n m : nat, n + m = m + n
*)
(* TARGET: n + m + (p + p) || (p + p) + (n + m) *)
Check plus_comm.
(* n + p + (m + p) == (n + p) + (m + p) *)
(* assoc: n + (p + (m + p)) *)
rewrite <- (plus_assoc n p (m + p)).
(* assoc: n + ((p + m) + p) *)
rewrite (plus_assoc p m p).
(* comm: n + ((p + m) + p) *)
rewrite (plus_comm p m).
(* assoc: (n + (m + p) + p *)
rewrite plus_assoc.
(* assoc: ((n + m) + p) + p *)
rewrite plus_assoc.
(* assoc: (n + m) + (p + p) *)
rewrite <- (plus_assoc (n + m) p).
apply (ev_sum (n + m) (p + p)).
- apply H.
- rewrite <- double_plus. apply ev_double.
}
apply HH in Hnm. apply (ev_ev__ev (n + p)).
- apply Hnm.
- apply Hnp.
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.
(** 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 : forall n, le n n
| le_S : forall n m, (le n m) -> (le n (S m)).
Notation "m <= n" := (le m n).
(** 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]. *)
End Playground.
Definition lt (n m:nat) := le (S n) m.
Notation "m < n" := (lt m n).
(** Here are a few more simple relations on numbers: *)
Inductive square_of : nat -> nat -> Prop :=
| sq : forall n:nat, square_of n (n * n).
Inductive next_nat : nat -> nat -> Prop :=
| nn : forall n:nat, next_nat n (S n).
Inductive next_even : nat -> nat -> Prop :=
| ne_1 : forall n, ev (S n) -> next_even n (S n)
| ne_2 : forall n, ev (S (S n)) -> next_even n (S (S n)).
(** **** Exercise: 2 stars, optional (total_relation) *)
(** Define an inductive binary relation [total_relation] that holds
between every pair of natural numbers. *)
Inductive total_relation : nat -> nat -> Prop :=
| tr : forall n m, total_relation n m.
(** [] *)
(** **** Exercise: 2 stars, optional (empty_relation) *)
(** Define an inductive binary relation [empty_relation] (on numbers)
that never holds. *)
Inductive empty_relation : nat -> nat -> Prop :=
| er : forall n m, False -> empty_relation n m.
(** [] *)
(** **** 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 H H0.
inversion H.
- apply H0.
- rewrite <- H0. apply H.
Qed.
Theorem O_le_n : forall n,
0 <= n.
Proof.
induction n as [|n' IHn].
- apply le_n.
- apply le_S. apply IHn.
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.
- generalize dependent H1.
apply le_trans.
apply le_S. apply le_n.
Qed.
Theorem le_plus_l : forall a b,
a <= a + b.
Proof.
intros a b.
induction a.
- apply O_le_n.
- apply n_le_m__Sn_le_Sm in IHa.
apply IHa.
Qed.
Theorem plus_lt : forall n1 n2 m,
n1 + n2 < m ->
n1 < m /\ n2 < m.
Proof.
unfold lt.
intros n1 n2 m.
split.
- apply (le_trans (S n1) (S (n1 + n2))).
+ apply n_le_m__Sn_le_Sm. apply le_plus_l.
+ apply H.
- apply (le_trans (S n2) (S (n1 + n2))).
+ apply n_le_m__Sn_le_Sm. rewrite -> plus_comm.
apply le_plus_l.
+ apply H.
Qed.
Theorem lt_S : forall n m,
n < m ->
n < S m.
Proof.
unfold lt.
intros n m H.
apply le_S.
apply H.
Qed.
Theorem leb_complete : forall n m,
leb n m = true -> n <= m.
Proof.
intros n m.
generalize dependent n.
induction m as [|m' IHm].
- intros n H.
destruct n as [|n'].
+ apply le_n.
+ inversion H.
- intros n H.
destruct n as [|n'].
+ apply O_le_n.
+ simpl in H.
apply IHm in H.
apply n_le_m__Sn_le_Sm.
apply H.
Qed.
(** Hint: The next one may be easiest to prove by induction on [m]. *)
Theorem leb_correct : forall n m,
n <= m ->
leb n m = true.
Proof.
intros n m.
generalize dependent n.
induction m as [|m' IHm].
- intros n H. inversion H. reflexivity.
- intros n H. destruct n.
+ reflexivity.
+ apply IHm.
generalize dependent H.
apply Sn_le_Sm__n_le_m .
Qed.
(** Hint: This theorem can easily be proved without using [induction]. *)
Theorem leb_true_trans : forall n m o,
leb n m = true -> leb m o = true -> leb n o = true.
Proof.
intros n m o H H0.
apply leb_complete in H.
apply leb_complete in H0.
apply leb_correct.
generalize dependent H0.
generalize dependent H.
apply le_trans.
Qed.
(** [] *)
(** **** Exercise: 2 stars, optional (leb_iff) *)
Theorem leb_iff : forall n m,
leb n m = true <-> n <= m.
Proof.
split.
- (* -> *) apply leb_complete.
- (* <- *) apply leb_correct.
Qed.
(** [] *)
Module R.
(** **** Exercise: 3 stars, recommended (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 : forall m n o, R m n o -> R (S m) n (S o)
| c3 : forall m n o, R m n o -> R m (S n) (S o)
| c4 : forall m n o, R (S m) (S n) (S (S o)) -> R m n o
| c5 : forall m n o, R m n o -> R n m o.
(** - Which of the following propositions are provable?
- [R 1 1 2]
- [R 2 2 6]
- If we dropped constructor [c5] from the definition of [R],
would the set of provable propositions change? Briefly (1
sentence) explain your answer.
- If we dropped constructor [c4] from the definition of [R],
would the set of provable propositions change? Briefly (1
sentence) explain your answer.
(* FILL IN HERE *)
*)
(* Do not modify the following line: *)
Definition manual_grade_for_R_provability : option (prod nat string) := None.
(** [] *)
(** **** Exercise: 3 stars, 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! *)
Inductive subseq : list nat -> list nat -> Prop :=
| s1 : forall l, subseq [] l
| s2 : forall n l1 l2, subseq l1 l2 -> subseq (n :: l1) (n :: l2)
| s3 : forall n l1 l2, subseq l1 l2 -> subseq l1 (n :: l2).
Theorem subseq_refl : forall (l : list nat), subseq l l.
Proof.
induction l as [|n l' IHl].
- (* l = nil *) apply s1.
- apply s2. apply IHl.
Qed.
Theorem subseq_app : forall (l1 l2 l3 : list nat),
subseq l1 l2 -> subseq l1 (l2 ++ l3).
Proof.
induction l1 as [| n l1' IHl1].
- destruct l2.
+ simpl. intros l3 H. apply s1.
+ simpl. intros l3 H. apply s1.
- induction l2 as [| m l2' IHl2].
+ intros l3 H. inversion H.
+ intros l3 H. simpl. inversion H.
* apply s2. apply IHl1. apply H1.
* apply s3. apply IHl2. apply H2.
Qed.
Theorem subseq_trans : forall (l1 l2 l3 : list nat),
subseq l1 l2 -> subseq l2 l3 -> subseq l1 l3.
Proof.
intros l1 l2 l3.
generalize dependent l2.
generalize dependent l1.
induction l3 as [| x3 xs3 IHl3].
(* [] *)
intros l1 l2 H12 H23.
inversion H23 as [ys | x ys xs Hst | x ys xs Hsa ].
rewrite <- H in H12.
inversion H12.
apply s1.
(* x::xs *)
intros l1 l2 H12 H23.
inversion H23 as [ys | x ys xs H23st | x ys xs H23sa ].
(* 0 *)
rewrite <- H in H12.
rewrite <- H in H23.
inversion H12. apply s1.
(* tail *)
rewrite <- H1 in IHl3.
rewrite <- H in H23.
rewrite <- H in H12.
rewrite <- H0.
rewrite <- H0 in H23.
rewrite <- H1 in H23.
rewrite <- H1 in H23st.
rewrite <- H1.
inversion H12 as [ys1 | x1 ys1 xs1 H12st | x1 ys1 xs1 H12sa].
(* 0 *)
apply s1.
(* tail *)
apply s2.
apply (IHl3 ys1 ys).
apply H12st.
apply H23st.
(* all *)
rewrite <- H2 in H12.
rewrite <- H2 in H12sa.
rewrite <- H2.
apply s3.
apply (IHl3 ys1 ys).
apply H12sa.
apply H23st.
(* all *)
rewrite <- H in H12.
rewrite <- H in H23.
rewrite <- H in H23sa.
rewrite <- H0 in H23.
rewrite <- H0.
rewrite <- H1 in IHl3.
rewrite <- H1 in H23.
rewrite <- H1 in H23sa.
rewrite <- H1.
inversion H12 as [ys1 | x1 ys1 xs1 H12st | x1 ys1 xs1 H12sa].
(* 0 *) apply s1.
(* tail *)
rewrite -> H0.
apply s3.
rewrite -> H2.
apply (IHl3 l1 ys).
apply H12.
apply H23sa.
(* all *)
apply s3.
apply (IHl3 l1 ys).
apply H12.
apply H23sa.
Qed.
(* Do not modify the following line: *)
Definition manual_grade_for_subsequence : option (prod nat string) := None.
(** [] *)
(** **** Exercise: 2 stars, optional (R_provability2) *)
(** Suppose we give Coq the following definition:
Inductive R : nat -> list nat -> Prop :=
| c1 : R 0 []
| c2 : forall n l, R n l -> R (S n) (n :: l)
| c3 : forall n l, R (S n) l -> R n l.
Which of the following propositions are provable?
- [R 2 [1;0]]
- [R 1 [1;2;1;0]]
- [R 6 [3;2;1;0]] *)
(* FILL IN HERE *)
(** [] *)
(* ################################################################# *)
(** * Case Study: Regular Expressions *)
(** The [ev] property provides a simple example for illustrating
inductive definitions and the basic techniques for reasoning about
them, but it is not terribly exciting -- after all, it is
equivalent to the two non-inductive definitions of evenness that
we had already seen, and does not seem to offer any concrete
benefit over them. To give a better sense of the power of
inductive definitions, we now show how to use them to model a
classic concept in computer science: _regular expressions_. *)
(** Regular expressions are a simple language for describing strings,
defined as follows: *)
Inductive reg_exp {T : Type} : Type :=
| EmptySet : reg_exp
| EmptyStr : reg_exp
| Char : T -> reg_exp
| App : reg_exp -> reg_exp -> reg_exp
| Union : reg_exp -> reg_exp -> reg_exp
| Star : reg_exp -> reg_exp.
(** Note that this definition is _polymorphic_: Regular
expressions in [reg_exp T] describe strings with characters drawn
from [T] -- that is, lists of elements of [T].
(We depart slightly from standard practice in that we do not
require the type [T] to be finite. This results in a somewhat
different theory of regular expressions, but the difference is not
significant for our purposes.) *)