-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndProp.v
1688 lines (1477 loc) · 45.4 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.
Inductive even : nat -> Prop :=
| ev_0 : even 0
| ev_SS (n : nat) (H : even n) : even (S (S n)).
Fail Inductive wrong_ev (n : nat) : Prop :=
| wrong_ev_0 : wrong_ev 0
| wrong_ev_SS : wrong_ev n -> wrong_ev (S (S n)).
Theorem ev_4 : even 4.
Proof. apply ev_SS. apply ev_SS. apply ev_0. Qed.
(** ... or we can use function application syntax: *)
Theorem ev_4' : even 4.
Proof. apply (ev_SS 2 (ev_SS 0 ev_0)). Qed.
(** We can also prove theorems that have hypotheses involving [even]. *)
Theorem ev_plus4 : forall n, even n -> even (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,
even (double n).
Proof.
intros n.
induction n.
-simpl. apply ev_0.
-simpl. apply ev_SS. apply IHn.
Qed.
(** [] *)
Theorem ev_inversion :
forall (n : nat), even n ->
(n = 0) \/ (exists n', n = S (S n') /\ even n').
Proof.
intros n E.
destruct E as [ | n' E'].
- (* E = ev_0 : even 0 *)
left. reflexivity.
- (* E = ev_SS n' E' : even (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,
even n -> even (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.
(** However, this variation cannot easily be handled with [destruct]. *)
Theorem evSS_ev : forall n, even (S (S n)) -> even n.
Proof. intros n H. apply ev_inversion in H. destruct H.
- discriminate H.
- destruct H as [n' [Hnm Hev]]. injection Hnm.
intros Heq. rewrite Heq. apply Hev.
Qed.
Theorem evSS_ev' : forall n,
even (S (S n)) -> even n.
Proof.
intros n E.
inversion E as [| n' E'].
(* 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 inductive
properties, something that takes a bit more work using our
inversion lemma. For example: *)
Theorem one_not_even : ~ even 1.
Proof.
intros H. apply ev_inversion in H.
destruct H as [ | [m [Hm _]]].
- discriminate H.
- discriminate Hm.
Qed.
Theorem one_not_even' : ~ even 1.
intros H. inversion H. Qed.
(** **** Exercise: 1 star, standard (inversion_practice)
Prove the following result using [inversion]. For extra practice,
prove it using the inversion lemma. *)
Theorem SSSSev__even : forall n,
even (S (S (S (S n)))) -> even n.
Proof.
intros n H. apply ev_inversion in H.
destruct H as[ | [m [Hnm Hev]]].
-discriminate H.
-apply S_injective in Hnm. apply S_injective in Hnm.
rewrite<- Hnm in Hev. apply evSS_ev'.
apply Hev.
Qed.
(** [] *)
(** **** Exercise: 1 star, standard (even5_nonsense)
Prove the following result using [inversion]. *)
Theorem even5_nonsense :
even 5 -> 2 + 2 = 9.
Proof.
intros H.
inversion H as [ | n' E'].
inversion E' as [ | n'' E''].
inversion E'' as [ | n''' E'''].
Qed.
(** [] *)
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.
Lemma ev_even : forall n,
even 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.
Theorem ev_even_iff : forall n,
even n <-> exists k, n = double k.
Proof.
intros n. split.
- (* -> *) apply ev_even.
- (* <- *) intros [k Hk]. rewrite Hk. apply ev_double.
Qed.
Theorem ev_sum : forall n m, even n -> even m -> even (n + m).
Proof.
intros n m E1 E2.
induction E1 as [|n' E1' IH].
-simpl. apply E2.
-simpl. apply ev_SS. apply IH.
Qed.
(** [] *)
(** **** Exercise: 4 stars, advanced, optional (even'_ev)
In general, there may be multiple ways of defining a
property inductively. For example, here's a (slightly contrived)
alternative definition for [even]: *)
Inductive even' : nat -> Prop :=
| even'_0 : even' 0
| even'_2 : even' 2
| even'_sum n m (Hn : even' n) (Hm : even' m) : even' (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 even'_ev : forall n, even' n <-> even n.
Proof.
intros n. split.
-intros H. induction H.
+apply ev_0.
+apply ev_SS. apply ev_0.
+apply ev_sum. apply IHeven'1. apply IHeven'2.
-intros H. induction H.
+apply even'_0.
+destruct (n=?0) eqn: H1.
*apply eqb_eq in H1. rewrite H1. apply even'_2.
*assert(H2: S(S n)=2+n). { reflexivity. }
rewrite H2. apply even'_sum. apply even'_2. apply IHeven.
Qed.
(** [] *)
(** **** Exercise: 3 stars, advanced, recommended (ev_ev__ev)
Finding the appropriate thing to do induction on is a
bit tricky here: *)
Lemma ev_2: forall n m,
even (n+n+m)-> even m.
Proof.
intros n m H1.
induction n.
-simpl in H1. apply H1.
-simpl in H1.
assert(H2:n+S n = S n+n). {rewrite <- plus_comm. reflexivity. }
rewrite H2 in H1. simpl in H1.
inversion H1. apply IHn.
apply H0.
Qed.
Theorem ev_ev__ev : forall n m,
even (n+m) -> even n -> even m.
Proof.
intros n m H1 H2.
assert(H3: even (n+(n+m))). {apply ev_sum. apply H2. apply H1. }
rewrite plus_assoc in H3.
apply ev_2 in H3.
apply H3.
Qed.
(** [] *)
(** **** Exercise: 3 stars, standard, 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,
even (n+m) -> even (n+p) -> even (m+p).
Proof.
intros n m p H1 H2.
assert(H: even((n+m)+(n+p))). {apply ev_sum. apply H1. apply H2. }
rewrite plus_assoc in H.
assert(H3: n+m=m+n). {apply plus_comm. }
assert(H4: m+n+n=n+n+m). {rewrite<-plus_assoc. apply plus_comm. }
rewrite H3 in H. rewrite H4 in H.
assert(H5: n+n+m+p=n+n+(m+p)). {rewrite plus_assoc. reflexivity. }
rewrite H5 in H.
apply ev_2 in H.
apply H.
Qed.
(** [] *)
(* ################################################################# *)
(** * Inductive Relations *)
(** A proposition parameterized by a number (such as [even])
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.
Inductive le : nat -> nat -> Prop :=
| le_n n : le n n
| le_S n m (H : le n m) : le n (S m).
Notation "m <= n" := (le m n).
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 n : square_of n (n * n).
Inductive next_nat : nat -> nat -> Prop :=
| nn n : next_nat n (S n).
Inductive next_even : nat -> nat -> Prop :=
| ne_1 n : even (S n) -> next_even n (S n)
| ne_2 n (H : even (S (S n))) : next_even n (S (S n)).
(** **** Exercise: 2 stars, standard, optional (total_relation)
Define an inductive binary relation [total_relation] that holds
between every pair of natural numbers. *)
(* FILL IN HERE
[] *)
Inductive total_relation:nat->nat -> Prop:=
|total_0_0: total_relation 0 0
|total_n_m n m(H:total_relation n m): total_relation n (S m)
|total_m_n n m(H:total_relation n m): total_relation (S n) m.
(** **** Exercise: 2 stars, standard, optional (empty_relation)
Define an inductive binary relation [empty_relation] (on numbers)
that never holds. *)
(* FILL IN HERE
[] *)
Inductive empty_relation:nat->nat->Prop:= .
(** 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']. *)
(** **** Exercise: 3 stars, standard, 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.
induction H0.
-apply H.
-apply le_S in IHle. apply IHle.
Qed.
Theorem O_le_n : forall n,
0 <= n.
Proof.
intros n.
induction n.
-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.
-reflexivity.
-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_comm. simpl. apply le_S. rewrite->plus_comm.
apply IHb.
Qed.
Theorem plus_le : forall n1 n2 m,
n1 + n2 <= m ->
n1<= m /\ n2<=m.
Proof.
intros n1 n2 m H.
induction H.
-split.
+apply le_plus_l.
+rewrite plus_comm. apply le_plus_l.
-destruct IHle. split.
+apply le_S in H0. apply H0.
+apply le_S in H1. apply H1.
Qed.
Theorem plus_lt : forall n1 n2 m,
n1 + n2 < m ->
n1 < m /\ n2 < m.
Proof.
unfold lt.
intros n1 n2 m H1.
rewrite plus_n_Sm in H1.
split.
-rewrite plus_comm in H1. simpl in H1. rewrite plus_comm in H1.
apply (le_trans (S n1) (S(n1+n2))).
+apply n_le_m__Sn_le_Sm. apply le_plus_l.
+apply H1.
-apply plus_le in H1. destruct H1.
apply H0.
Qed.
Theorem lt_S : forall n m,
n < m ->
n < S m.
Proof.
unfold lt.
intros n m H.
apply (le_trans (S n) m).
-apply H.
-apply le_S. apply le_n.
Qed.
Theorem leb_complete : forall n m,
n <=? m = true -> n <= m.
Proof.
intros n m.
generalize dependent m.
induction n.
-intros m H. apply O_le_n.
-intros m H. destruct m eqn:eq1.
+discriminate H.
+apply IHn 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 ->
n <=? m = true.
Proof.
intros n m.
generalize dependent n.
induction m.
-intros. inversion H.
+unfold leb. reflexivity.
-intros. inversion H.
+simpl. rewrite <-leb_refl. reflexivity.
+destruct n.
*unfold leb. reflexivity.
*apply Sn_le_Sm__n_le_m in H. simpl. apply IHm in H.
apply H.
Qed.
(** Hint: This 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 H1 H2.
apply leb_complete in H1.
apply leb_complete in H2.
apply leb_correct.
apply (le_trans n m).
-apply H1.
-apply H2.
Qed.
(** [] *)
(** **** Exercise: 2 stars, standard, optional (leb_iff) *)
Theorem leb_iff : forall n m,
n <=? m = true <-> n <= m.
Proof.
intros n m.
split.
-apply leb_complete.
-apply leb_correct.
Qed.
(** [] *)
Module R.
(** **** Exercise: 3 stars, standard, 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 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.
Example R_try1:R 1 1 2.
Proof. apply c2. apply c3. apply c1. Qed.
(* Do not modify the following line: *)
Definition manual_grade_for_R_provability : option (nat*string) :=None.
Definition fR : nat -> nat -> nat:= plus.
Theorem R_equiv_fR : forall m n o, R m n o <-> fR m n = o.
Proof.
intros m n o.
split.
-intros H. induction H.
+simpl. reflexivity.
+simpl. apply injective_S. apply IHR.
+unfold fR. rewrite plus_comm. simpl. apply injective_S.
unfold fR in IHR. rewrite plus_comm in IHR. apply IHR.
+simpl in IHR. apply S_injective in IHR. unfold fR in IHR.
rewrite plus_comm in IHR. simpl in IHR. apply S_injective in IHR.
unfold fR. rewrite plus_comm. apply IHR.
+unfold fR. rewrite plus_comm. unfold fR in IHR. apply IHR.
-generalize dependent n.
generalize dependent o.
induction m.
+intros n. induction n.
*intros. unfold fR in H. simpl in H. rewrite->H. apply c1.
*intros. simpl in H. simpl in IHn. destruct n0.
{ discriminate H. }
{ apply S_injective in H. apply c3. apply IHn in H. apply H. }
+intros n. induction n.
*intros. simpl in H. discriminate H.
*intros. apply c2. simpl in H. apply S_injective in H. apply IHm in H.
apply H.
Qed.
(** [] *)
End R.
(** **** Exercise: 2 stars, advanced (subsequence)**)
Inductive subseq {X:Type}: list X -> list X -> Prop :=
|emp_l l : subseq [] l
|ll1 x l1 l2(H:subseq l1 l2): subseq l1 (x::l2)
|ll2 x l1 l2(H:subseq l1 l2): subseq (x::l1) (x::l2).
Theorem subseq_refl : forall (l : list nat), subseq l l.
Proof.
intros l. induction l.
-apply emp_l.
-apply ll2. apply IHl.
Qed.
Theorem subseq_app : forall (l1 l2 l3 : list nat),
subseq l1 l2 ->
subseq l1 (l2 ++ l3).
Proof.
intros.
induction H.
-apply emp_l.
-simpl. apply ll1. apply IHsubseq.
-simpl. apply ll2. apply IHsubseq.
Qed.
Theorem subseq_trans : forall (l1 l2 l3 : list nat),
subseq l1 l2 ->
subseq l2 l3 ->
subseq l1 l3.
Proof.
intros.
generalize dependent l1.
induction H0.
-intros. inversion H;subst. apply emp_l.
-intros. apply ll1. apply (IHsubseq l0). apply H.
-intros. inversion H.
+apply emp_l.
+subst. apply ll1. apply (IHsubseq l0). apply H3.
+subst. apply ll2. apply (IHsubseq l3). apply H3.
Qed.
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.
Example R_try1: R 2 [1;0].
Proof.
apply c2. apply c2. apply c1.
Qed.
Example R_try2: R 1 [1;2;1;0].
Proof.
apply c3. apply c2. apply c3. apply c3. apply c2. apply c2. apply c2. apply c1.
Qed.
(** * Case Study: Regular Expressions *)
Inductive reg_exp {T : Type} : Type :=
| EmptySet
| EmptyStr
| Char (t : T)
| App (r1 r2 : reg_exp)
| Union (r1 r2 : reg_exp)
| Star (r : reg_exp).
Arguments EmptySet{T}.
Arguments EmptyStr{T}.
Arguments Char{T} _.
Arguments App {T} _ _.
Arguments Union {T} _ _.
Arguments Star {T} _.
(** We can easily translate this informal definition into an
[Inductive] one as follows: *)
Inductive exp_match {T} : list T -> reg_exp -> Prop :=
| MEmpty : exp_match [] EmptyStr
| MChar x : exp_match [x] (Char x)
| MApp s1 re1 s2 re2
(H1 : exp_match s1 re1)
(H2 : exp_match s2 re2) :
exp_match (s1 ++ s2) (App re1 re2)
| MUnionL s1 re1 re2
(H1 : exp_match s1 re1) :
exp_match s1 (Union re1 re2)
| MUnionR re1 s2 re2
(H2 : exp_match s2 re2) :
exp_match s2 (Union re1 re2)
| MStar0 re : exp_match [] (Star re)
| MStarApp s1 s2 re
(H1 : exp_match s1 re)
(H2 : exp_match s2 (Star re)) :
exp_match (s1 ++ s2) (Star re).
(** Again, for readability, we can also display this definition using
inference-rule notation. At the same time, let's introduce a more
readable infix notation. *)
Notation "s =~ re" := (exp_match s re) (at level 80).
Lemma quiz: forall T(s:list T), ~(s=~EmptySet).
Proof. intros T s Hc. inversion Hc. Qed.
Example reg_exp_ex1 : [1] =~ Char 1.
Proof.
apply MChar.
Qed.
Example reg_exp_ex2 : [1; 2] =~ App (Char 1) (Char 2).
Proof.
apply (MApp [1] _ [2]).
- apply MChar.
- apply MChar.
Qed.
(** (Notice how the last example applies [MApp] to the strings
[[1]] and [[2]] directly. Since the goal mentions [[1; 2]]
instead of [[1] ++ [2]], Coq wouldn't be able to figure out how to
split the string on its own.)
Using [inversion], we can also show that certain strings do _not_
match a regular expression: *)
Example reg_exp_ex3 : ~ ([1; 2] =~ Char 1).
Proof.
intros H. inversion H.
Qed.
(** We can define helper functions for writing down regular
expressions. The [reg_exp_of_list] function constructs a regular
expression that matches exactly the list that it receives as an
argument: *)
Fixpoint reg_exp_of_list {T} (l : list T) :=
match l with
| [] => EmptyStr
| x :: l' => App (Char x) (reg_exp_of_list l')
end.
Example reg_exp_ex4 : [1; 2; 3] =~ reg_exp_of_list [1; 2; 3].
Proof.
simpl. apply (MApp [1]).
{ apply MChar. }
apply (MApp [2]).
{ apply MChar. }
apply (MApp [3]).
{ apply MChar. }
apply MEmpty.
Qed.
Lemma MStar1 :
forall T s (re : @reg_exp T) ,
s =~ re ->
s =~ Star re.
Proof.
intros T s re H.
rewrite <- (app_nil_r _ s).
apply (MStarApp s [] re).
- apply H.
- apply MStar0.
Qed.
(** **** Exercise: 3 stars, standard (exp_match_ex1)**)
Lemma empty_is_empty : forall T (s : list T),
~ (s =~ EmptySet).
Proof.
intros T s H. inversion H.
Qed.
Lemma MUnion' : forall T (s : list T) (re1 re2 : @reg_exp T),
s =~ re1 \/ s =~ re2 ->
s =~ Union re1 re2.
Proof.
intros T s re1 re2 H.
destruct H as [H1 | H2].
-apply (MUnionL s re1 re2). apply H1.
-apply (MUnionR re1 s re2). apply H2.
Qed.
Lemma MStar' : forall T (ss : list (list T)) (re : reg_exp),
(forall s, In s ss -> s =~ re) ->
fold app ss [] =~ Star re.
Proof.
intros T ss re H.
induction ss.
-unfold fold. apply MStar0.
-simpl. apply MStarApp.
+ apply H. simpl. left. reflexivity.
+ apply IHss. simpl in H. intros. apply H. right. apply H0.
Qed.
(** **** Exercise: 4 stars, standard, optional (reg_exp_of_list_spec)**)
Lemma reg_exp_of_list_spec : forall T (s1 s2 : list T),
s1 =~ reg_exp_of_list s2 <-> s1 = s2.
Proof.
intros T s1 s2.
split.
-generalize dependent s1.
induction s2.
+Abort.
Fixpoint re_chars {T} (re : reg_exp) : list T :=
match re with
| EmptySet => []
| EmptyStr => []
| Char x => [x]
| App re1 re2 => re_chars re1 ++ re_chars re2
| Union re1 re2 => re_chars re1 ++ re_chars re2
| Star re => re_chars re
end.
(** We can then phrase our theorem as follows: *)
Theorem in_re_match : forall T (s : list T) (re : reg_exp) (x : T),
s =~ re ->
In x s ->
In x (re_chars re).
Proof.
intros T s re x Hmatch Hin.
induction Hmatch
as [| x'
| s1 re1 s2 re2 Hmatch1 IH1 Hmatch2 IH2
| s1 re1 re2 Hmatch IH | re1 s2 re2 Hmatch IH
| re | s1 s2 re Hmatch1 IH1 Hmatch2 IH2].
- simpl. simpl in Hin. apply Hin.
- apply Hin.
- simpl. rewrite In_app_iff in *.
destruct Hin as [Hin | Hin].
+ left. apply (IH1 Hin).
+ right. apply (IH2 Hin).
- simpl. rewrite In_app_iff.
left. apply (IH Hin).
- simpl. rewrite In_app_iff.
right. apply (IH Hin).
- destruct Hin.
- simpl. rewrite In_app_iff in Hin.
destruct Hin as [Hin | Hin].
+ apply (IH1 Hin).
+ apply (IH2 Hin).
Qed.
(** **** Exercise: 4 stars, standard (re_not_empty) **)
Fixpoint re_not_empty {T : Type} (re : @reg_exp T) : bool:=
match re with
| EmptySet => false
| EmptyStr => true
| Char x => true
| App re1 re2 => (re_not_empty re1)&&(re_not_empty re2)
| Union re1 re2 => (re_not_empty re1)||(re_not_empty re2)
| Star re => true
end.
Lemma re_not_empty_correct : forall T (re : @reg_exp T),
(exists s, s =~ re) <-> re_not_empty re = true.
Proof.
intros T re.
split.
-intros H. induction re.
+destruct H as [s H1]. inversion H1.
+simpl. reflexivity.
+reflexivity.
+simpl. apply andb_true_iff. split.
* apply IHre1. destruct H as [s H1].
inversion H1;subst. exists s1. apply H3.
* apply IHre2. destruct H as [s H1].
inversion H1;subst. exists s2. apply H4.
+simpl. apply orb_true_iff.
destruct H as [s H1]. inversion H1;subst.
*left. apply IHre1. exists s. apply H2.
*right. apply IHre2. exists s. apply H0.
+reflexivity.
-intros H. induction re.
+inversion H.
+exists []. apply MEmpty.
+exists [t]. apply MChar.
+inversion H. apply andb_true_iff in H1.
destruct H1 as [H2 H3].
apply IHre1 in H2. apply IHre2 in H3.
destruct H2 as [s1 H2']. destruct H3 as [s2 H3'].
exists (s1++s2).
apply MApp.
*apply H2'.
*apply H3'.
+inversion H. apply orb_true_iff in H1.
destruct H1 as [H2 | H3].
*apply IHre1 in H2. destruct H2 as [s' H2'].
exists s'. apply MUnionL. apply H2'.
*apply IHre2 in H3. destruct H3 as [s' H3'].
exists s'. apply MUnionR. apply H3'.
+exists []. apply MStar0.
Qed.
(** [] *)
(* ================================================================= *)
(** ** The [remember] Tactic *)
Lemma star_app: forall T (s1 s2 : list T) (re : reg_exp),
s1 =~ Star re ->
s2 =~ Star re ->
s1 ++ s2 =~ Star re.
Proof.
intros T s1 s2 re H1.
remember (Star re) as re'.
generalize dependent s2.
induction H1
as [|x'|s1 re1 s2' re2 Hmatch1 IH1 Hmatch2 IH2
|s1 re1 re2 Hmatch IH|re1 s2' re2 Hmatch IH
|re''|s1 s2' re'' Hmatch1 IH1 Hmatch2 IH2].
(** The [Heqre'] is contradictory in most cases, allowing us to
conclude immediately. *)
- (* MEmpty *) discriminate.
- (* MChar *) discriminate.
- (* MApp *) discriminate.
- (* MUnionL *) discriminate.
- (* MUnionR *) discriminate.
(** The interesting cases are those that correspond to [Star]. Note
that the induction hypothesis [IH2] on the [MStarApp] case
mentions an additional premise [Star re'' = Star re'], which
results from the equality generated by [remember]. *)
- (* MStar0 *)
injection Heqre'. intros Heqre'' s H. apply H.
- (* MStarApp *)
injection Heqre'. intros H0.
intros s2 H1. rewrite <- app_assoc.
apply MStarApp.
+ apply Hmatch1.
+ apply IH2.
* rewrite H0. reflexivity.
* apply H1.
Qed.
(** **** Exercise: 4 stars, standard, optional (exp_match_ex2) *)
(** The [MStar''] lemma below (combined with its converse, the
[MStar'] exercise above), shows that our definition of [exp_match]
for [Star] is equivalent to the informal one given previously. *)
(**Que1**)
Lemma MStar'' : forall T (s : list T) (re : reg_exp),
s =~ Star re ->
exists ss : list (list T),
s = fold app ss []
/\ forall s', In s' ss -> s' =~ re.
Proof.
intros T s re.
intros H.
remember (Star re) as re'.
revert re Heqre'.
induction H; intros.
- discriminate.
- discriminate.
- discriminate.
- discriminate.
- discriminate.
- exists []. split.
+reflexivity.
+intros. inversion H.
- apply IHexp_match2 in Heqre'. destruct Heqre' as [ss [H1 H2]].
exists (s1::ss).
Abort.
(** [] *)
(** **** Exercise: 5 stars, advanced (pumping)
One of the first really interesting theorems in the theory of
regular expressions is the so-called _pumping lemma_, which
states, informally, that any sufficiently long string [s] matching
a regular expression [re] can be "pumped" by repeating some middle
section of [s] an arbitrary number of times to produce a new
string also matching [re].
To begin, we need to define "sufficiently long." Since we are
working in a constructive logic, we actually need to be able to
calculate, for each regular expression [re], the minimum length
for strings [s] to guarantee "pumpability." *)
Module Pumping.
(**(Que2')**)
Fixpoint pumping_constant {T} (re : @reg_exp T) : nat :=
match re with
| EmptySet => 0
| EmptyStr => 1
| Char _ => 2
| App re1 re2 =>
pumping_constant re1 + pumping_constant re2
| Union re1 re2 =>
pumping_constant re1 + pumping_constant re2
| Star _ => 1
end.
(** Next, it is useful to define an auxiliary function that repeats a
string (appends it to itself) some number of times. *)
Fixpoint napp {T} (n : nat) (l : list T) : list T :=
match n with
| 0 => []
| S n' => l ++ napp n' l
end.
Lemma napp_plus: forall T (n m : nat) (l : list T),
napp (n + m) l = napp n l ++ napp m l.
Proof.
intros T n m l.
induction n as [|n IHn].
- reflexivity.
- simpl. rewrite IHn, app_assoc. reflexivity.
Qed.
Lemma napp_star: forall T (m:nat)(s1 s2 : list T) (re : @reg_exp T),
s1=~re->
s2=~Star re ->
napp m s1++s2=~ Star re.
Proof.
intros T m s1 s2 re Hs1 Hs2.
induction m.
-simpl. apply Hs2.
-simpl. rewrite <- app_assoc.
apply MStarApp.
+apply Hs1.
+apply IHm.
Qed.
Import Coq.omega.Omega.
Lemma pumping : forall T (re : @reg_exp T) s,
s =~ re ->
pumping_constant re <= length s ->
exists s1 s2 s3,
s = s1 ++ s2 ++ s3 /\
s2 <> [] /\
forall m, s1 ++ napp m s2 ++ s3 =~ re.
Proof.
intros T re s Hmatch.
induction Hmatch
as [ | x | s1 re1 s2 re2 Hmatch1 IH1 Hmatch2 IH2
| s1 re1 re2 Hmatch IH | re1 s2 re2 Hmatch IH
| re | s1 s2 re Hmatch1 IH1 Hmatch2 IH2 ].
- (* MEmpty *)
simpl. omega.
- simpl. omega.
- simpl. intros. rewrite app_length in H.
apply Nat.add_le_cases in H.
destruct H as [H1 | H2].
+apply IH1 in H1. destruct H1 as [ s3[s4 [s5 H1']]].
destruct H1' as [H3 [H4 H5]].
exists s3,s4,(s5++s2). split.
* rewrite H3. rewrite app_assoc. rewrite app_assoc. rewrite app_assoc. reflexivity.
*split.
{apply H4. }
{intros m. rewrite app_assoc. rewrite app_assoc.