forked from mvmramos/pumping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
union.v
1145 lines (1104 loc) · 31.6 KB
/
union.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
(* ---------------------------------------------------------------------
This file contains definitions and proof scripts related to
(i) closure operations for context-free grammars,
(ii) context-free grammars simplification
(iii) context-free grammar Chomsky normalization and
(iv) pumping lemma for context-free languages.
More information can be found in the paper "Formalization of the
pumping lemma for context-free languages", submitted to
LATA 2016.
Marcus Vinícius Midena Ramos
--------------------------------------------------------------------- *)
Require Import List.
Require Import misc_list.
Require Import cfg.
Require Import cfl.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Import ListNotations.
(* --------------------------------------------------------------------- *)
(* UNION - DEFINITIONS *)
(* --------------------------------------------------------------------- *)
Section Union.
Variables non_terminal_1 non_terminal_2 terminal: Type.
Inductive g_uni_nt: Type:=
| Start_uni
| Transf1_uni_nt: non_terminal_1 -> g_uni_nt
| Transf2_uni_nt: non_terminal_2 -> g_uni_nt.
Lemma nt_eqdec':
(forall x y:non_terminal_1, {x=y}+{x<>y}) ->
(forall x y:non_terminal_2, {x=y}+{x<>y}) ->
(forall x y:g_uni_nt, {x=y}+{x<>y}).
Proof. decide equality. Qed.
Notation sf1:= (list (non_terminal_1 + terminal)).
Notation sf2:= (list (non_terminal_2 + terminal)).
Notation sfu:= (list (g_uni_nt + terminal)).
Notation nlist:= (list g_uni_nt).
Notation tlist:= (list terminal).
Definition g_uni_sf_lift1 (c: non_terminal_1 + terminal): g_uni_nt + terminal:=
match c with
| inl nt => inl (Transf1_uni_nt nt)
| inr t => inr t
end.
Definition g_uni_sf_lift2 (c: non_terminal_2 + terminal): g_uni_nt + terminal:=
match c with
| inl nt => inl (Transf2_uni_nt nt)
| inr t => inr t
end.
Inductive g_uni_rules (g1: cfg non_terminal_1 terminal) (g2: cfg non_terminal_2 terminal): g_uni_nt -> sfu -> Prop :=
| Start1_uni: g_uni_rules g1 g2 Start_uni [inl (Transf1_uni_nt (start_symbol g1))]
| Start2_uni: g_uni_rules g1 g2 Start_uni [inl (Transf2_uni_nt (start_symbol g2))]
| Lift1_uni: forall nt: non_terminal_1,
forall s: list (non_terminal_1 + terminal),
rules g1 nt s ->
g_uni_rules g1 g2 (Transf1_uni_nt nt) (map g_uni_sf_lift1 s)
| Lift2_uni: forall nt: non_terminal_2,
forall s: list (non_terminal_2 + terminal),
rules g2 nt s ->
g_uni_rules g1 g2 (Transf2_uni_nt nt) (map g_uni_sf_lift2 s).
Lemma g_uni_finite:
forall g1: cfg non_terminal_1 terminal,
forall g2: cfg non_terminal_2 terminal,
exists n: nat,
exists ntl: list g_uni_nt,
exists tl: tlist,
In Start_uni ntl /\
forall left: g_uni_nt,
forall right: sfu,
g_uni_rules g1 g2 left right ->
(length right <= n) /\
(In left ntl) /\
(forall s: g_uni_nt, In (inl s) right -> In s ntl) /\
(forall s: terminal, In (inr s) right -> In s tl).
Proof.
intros g1 g2.
destruct (rules_finite g1) as [n1 [ntl1 [tl1 H1]]].
destruct (rules_finite g2) as [n2 [ntl2 [tl2 H2]]].
exists (S (max n1 n2)), (Start_uni :: (map Transf1_uni_nt ntl1) ++ (map Transf2_uni_nt ntl2)), (tl1 ++ tl2).
split.
- simpl.
left.
reflexivity.
- split.
+ inversion H.
* simpl.
omega.
* simpl.
omega.
* destruct H1 as [_ H1].
subst.
specialize (H1 nt s H0).
destruct H1 as [H1 _].
rewrite map_length.
assert (H3: n1 >= n2 \/ n1 <= n2) by omega.
{
destruct H3 as [H3 | H3].
- apply max_l in H3.
rewrite H3.
omega.
- assert (H3':= H3).
apply max_r in H3.
rewrite H3.
omega.
}
* destruct H2 as [_ H2].
subst.
specialize (H2 nt s H0).
destruct H2 as [H2 _].
rewrite map_length.
assert (H3: n1 >= n2 \/ n1 <= n2) by omega.
{
destruct H3 as [H3 | H3].
- assert (H3':= H3).
apply max_l in H3.
rewrite H3.
omega.
- apply max_r in H3.
rewrite H3.
omega.
}
+ split.
* {
inversion H.
- simpl.
left.
reflexivity.
- simpl.
left.
reflexivity.
- simpl.
right.
apply in_or_app.
left.
destruct H1 as [_ H1].
specialize (H1 nt s H0).
destruct H1 as [_ [H5 [_ _]]].
apply in_split in H5.
destruct H5 as [l1 [l2 H5]].
rewrite H5.
rewrite map_app.
apply in_or_app.
right.
simpl.
left.
reflexivity.
- simpl.
right.
apply in_or_app.
right.
destruct H2 as [_ H2].
specialize (H2 nt s H0).
destruct H2 as [_ [H5 [_ _]]].
apply in_split in H5.
destruct H5 as [l1 [l2 H5]].
rewrite H5.
rewrite map_app.
apply in_or_app.
right.
simpl.
left.
reflexivity.
}
* {
split.
- inversion H.
+ subst.
intros s H4.
simpl in H4.
destruct H4 as [H4 | H4].
* inversion H4.
simpl.
right.
apply in_or_app.
left.
destruct H1 as [H1 _].
apply in_split in H1.
destruct H1 as [l1 [l2 H1]].
rewrite H1.
rewrite map_app.
apply in_or_app.
right.
simpl.
left.
reflexivity.
* contradiction.
+ subst.
intros s H4.
simpl in H4.
destruct H4 as [H4 | H4].
* inversion H4.
simpl.
right.
apply in_or_app.
right.
destruct H2 as [H2 _].
apply in_split in H2.
destruct H2 as [l1 [l2 H2]].
rewrite H2.
rewrite map_app.
apply in_or_app.
right.
simpl.
left.
reflexivity.
* contradiction.
+ subst.
intros s0 H4.
destruct H1 as [_ H1].
specialize (H1 nt s H0).
destruct H1 as [_ [_ [H5 _]]].
simpl.
right.
apply in_or_app.
left.
destruct s0.
* apply in_split in H4.
destruct H4 as [l1 [l2 H4]].
symmetry in H4.
apply map_expand in H4.
destruct H4 as [s1' [s2' [H4' [H4'' H4''']]]].
change (inl Start_uni :: l2) with ([inl Start_uni] ++ l2) in H4'''.
symmetry in H4'''.
apply map_expand in H4'''.
destruct H4''' as [s1'0 [s2'0 [H5' [H5'' H5''']]]].
{
destruct s1'0.
- inversion H5''.
- simpl in H5''.
inversion H5''.
destruct s0.
+ simpl in H3.
inversion H3.
+ simpl in H3.
inversion H3.
}
* assert (H6: In (inl n) s).
{
apply in_split in H4.
destruct H4 as [l1 [l2 H4]].
symmetry in H4.
apply map_expand in H4.
destruct H4 as [s1' [s2' [H4' [H4'' H4''']]]].
change (inl (Transf1_uni_nt n) :: l2) with ([inl (Transf1_uni_nt n)] ++ l2) in H4'''.
symmetry in H4'''.
apply map_expand in H4'''.
destruct H4''' as [s1'0 [s2'0 [H5' [H5'' H5''']]]].
destruct s1'0.
- inversion H5''.
- inversion H5''.
destruct s0.
+ simpl in H3.
inversion H3.
subst.
apply in_or_app.
right.
simpl.
left.
reflexivity.
+ simpl in H3.
inversion H3.
}
specialize (H5 n H6).
apply in_map.
exact H5.
* apply in_split in H4.
destruct H4 as [l1 [l2 H4]].
symmetry in H4.
apply map_expand in H4.
destruct H4 as [s1' [s2' [H4' [H4'' H4''']]]].
change (inl (Transf2_uni_nt n) :: l2) with ([inl (Transf2_uni_nt n)] ++ l2) in H4'''.
symmetry in H4'''.
apply map_expand in H4'''.
destruct H4''' as [s1'0 [s2'0 [H5' [H5'' H5''']]]].
{
destruct s1'0.
- inversion H5''.
- simpl in H5''.
inversion H5''.
destruct s0.
+ simpl in H3.
inversion H3.
+ simpl in H3.
inversion H3.
}
+ subst.
intros s0 H4.
destruct H2 as [_ H2].
specialize (H2 nt s H0).
destruct H2 as [_ [_ [H5 _]]].
simpl.
right.
apply in_or_app.
right.
destruct s0.
* apply in_split in H4.
destruct H4 as [l1 [l2 H4]].
symmetry in H4.
apply map_expand in H4.
destruct H4 as [s1' [s2' [H4' [H4'' H4''']]]].
change (inl Start_uni :: l2) with ([inl Start_uni] ++ l2) in H4'''.
symmetry in H4'''.
apply map_expand in H4'''.
destruct H4''' as [s1'0 [s2'0 [H5' [H5'' H5''']]]].
{
destruct s1'0.
- inversion H5''.
- simpl in H5''.
inversion H5''.
destruct s0.
+ simpl in H3.
inversion H3.
+ simpl in H3.
inversion H3.
}
* apply in_split in H4.
destruct H4 as [l1 [l2 H4]].
symmetry in H4.
apply map_expand in H4.
destruct H4 as [s1' [s2' [H4' [H4'' H4''']]]].
change (inl (Transf1_uni_nt n) :: l2) with ([inl (Transf1_uni_nt n)] ++ l2) in H4'''.
symmetry in H4'''.
apply map_expand in H4'''.
destruct H4''' as [s1'0 [s2'0 [H5' [H5'' H5''']]]].
{
destruct s1'0.
- inversion H5''.
- simpl in H5''.
inversion H5''.
destruct s0.
+ simpl in H3.
inversion H3.
+ simpl in H3.
inversion H3.
}
* assert (H6: In (inl n) s).
{
apply in_split in H4.
destruct H4 as [l1 [l2 H4]].
symmetry in H4.
apply map_expand in H4.
destruct H4 as [s1' [s2' [H4' [H4'' H4''']]]].
change (inl (Transf2_uni_nt n) :: l2) with ([inl (Transf2_uni_nt n)] ++ l2) in H4'''.
symmetry in H4'''.
apply map_expand in H4'''.
destruct H4''' as [s1'0 [s2'0 [H5' [H5'' H5''']]]].
destruct s1'0.
- inversion H5''.
- inversion H5''.
destruct s0.
+ simpl in H3.
inversion H3.
subst.
apply in_or_app.
right.
simpl.
left.
reflexivity.
+ simpl in H3.
inversion H3.
}
specialize (H5 n H6).
apply in_map.
exact H5.
- inversion H.
+ subst.
intros s H4.
simpl in H4.
destruct H4 as [H4 | H4].
* inversion H4.
* contradiction.
+ subst.
intros s H4.
simpl in H4.
destruct H4 as [H4 | H4].
* inversion H4.
* contradiction.
+ subst.
intros s0 H4.
destruct H1 as [_ H1].
specialize (H1 nt s H0).
destruct H1 as [_ [_ [_ H5]]].
assert (H6: In (inr s0) s).
{
apply in_split in H4.
destruct H4 as [l1 [l2 H4]].
symmetry in H4.
apply map_expand in H4.
destruct H4 as [s1' [s2' [H4' [H4'' H4''']]]].
change (inr s0 :: l2) with ([inr s0] ++ l2) in H4'''.
symmetry in H4'''.
apply map_expand in H4'''.
destruct H4''' as [s1'0 [s2'0 [H5' [H5'' H5''']]]].
destruct s1'0.
- inversion H5''.
- simpl in H5''.
inversion H5''.
destruct s1.
+ simpl in H3.
inversion H3.
+ simpl in H3.
inversion H3.
subst.
apply in_or_app.
right.
simpl.
left.
reflexivity.
}
specialize (H5 s0 H6).
apply in_or_app.
left.
exact H5.
+ subst.
intros s0 H4.
destruct H2 as [_ H2].
specialize (H2 nt s H0).
destruct H2 as [_ [_ [_ H5]]].
assert (H6: In (inr s0) s).
{
apply in_split in H4.
destruct H4 as [l1 [l2 H4]].
symmetry in H4.
apply map_expand in H4.
destruct H4 as [s1' [s2' [H4' [H4'' H4''']]]].
change (inr s0 :: l2) with ([inr s0] ++ l2) in H4'''.
symmetry in H4'''.
apply map_expand in H4'''.
destruct H4''' as [s1'0 [s2'0 [H5' [H5'' H5''']]]].
destruct s1'0.
- inversion H5''.
- simpl in H5''.
inversion H5''.
destruct s1.
+ simpl in H3.
inversion H3.
+ simpl in H3.
inversion H3.
subst.
apply in_or_app.
right.
simpl.
left.
reflexivity.
}
specialize (H5 s0 H6).
apply in_or_app.
right.
exact H5.
}
Qed.
Definition g_uni (g1: cfg non_terminal_1 terminal) (g2: cfg non_terminal_2 terminal): (cfg g_uni_nt terminal):= {|
start_symbol:= Start_uni;
rules:= g_uni_rules g1 g2;
t_eqdec:= t_eqdec g1;
nt_eqdec:= nt_eqdec' (nt_eqdec g1) (nt_eqdec g2);
rules_finite:= g_uni_finite g1 g2
|}.
(* --------------------------------------------------------------------- *)
(* UNION - LEMMAS *)
(* --------------------------------------------------------------------- *)
Lemma derives_add_uni_left:
forall g1: cfg non_terminal_1 terminal,
forall g2: cfg non_terminal_2 terminal,
forall s s': sf1,
derives g1 s s' ->
derives (g_uni g1 g2)
(map g_uni_sf_lift1 s)
(map g_uni_sf_lift1 s').
Proof.
intros g1 g2 s s' H.
induction H as [|x y z left right H1 H2 H3].
- apply derives_refl.
- rewrite map_app.
rewrite map_app.
rewrite map_app in H2.
simpl in H2.
apply derives_step with (left:=(Transf1_uni_nt left)).
exact H2.
simpl.
apply Lift1_uni.
exact H3.
Qed.
Lemma derives_add_uni_right :
forall g1: cfg non_terminal_1 terminal,
forall g2: cfg non_terminal_2 terminal,
forall s s': sf2,
derives g2 s s' ->
derives (g_uni g1 g2)
(map g_uni_sf_lift2 s)
(map g_uni_sf_lift2 s').
Proof.
intros g1 g2 s s' H.
induction H as [|x y z left right H1 H2 H3].
- apply derives_refl.
- rewrite map_app.
rewrite map_app.
rewrite map_app in H2.
simpl in H2.
apply derives_step with (left:=(Transf2_uni_nt left)).
exact H2.
simpl.
apply Lift2_uni.
exact H3.
Qed.
Theorem g_uni_correct_left:
forall g1: cfg non_terminal_1 terminal,
forall g2: cfg non_terminal_2 terminal,
forall s: sf1,
generates g1 s ->
generates (g_uni g1 g2) (map g_uni_sf_lift1 s).
Proof.
unfold generates.
intros g1 g2 s H.
apply derives_trans with (s2:= map g_uni_sf_lift1 [(inl (start_symbol g1))]).
- simpl.
match goal with
| |- derives _ ?s1 ?s2 =>
change s1 with ([] ++ s1)%list;
change s2 with ([] ++ s2 ++ [])%list
end.
apply derives_step with (left:= Start_uni).
+ apply derives_refl.
+ simpl.
apply Start1_uni.
- apply derives_add_uni_left.
exact H.
Qed.
Theorem g_uni_correct_right:
forall g1: cfg non_terminal_1 terminal,
forall g2: cfg non_terminal_2 terminal,
forall s: sf2,
generates g2 s -> generates (g_uni g1 g2) (map g_uni_sf_lift2 s).
Proof.
unfold generates.
intros g1 g2 s H.
apply derives_trans with (s2:= map g_uni_sf_lift2 [(inl (start_symbol g2))]).
- simpl.
match goal with
| |- derives _ ?s1 ?s2 =>
change s1 with ([] ++ s1)%list;
change s2 with ([] ++ s2 ++ [])%list
end.
apply derives_step with (left:= Start_uni).
+ apply derives_refl.
+ simpl.
apply Start2_uni.
- apply derives_add_uni_right.
exact H.
Qed.
Theorem g_uni_correct:
forall g1: cfg non_terminal_1 terminal,
forall g2: cfg non_terminal_2 terminal,
forall s1: sf1,
forall s2: sf2,
(generates g1 s1 -> generates (g_uni g1 g2) (map g_uni_sf_lift1 s1)) /\
(generates g2 s2 -> generates (g_uni g1 g2) (map g_uni_sf_lift2 s2)).
Proof.
split.
- apply g_uni_correct_left.
- apply g_uni_correct_right.
Qed.
Theorem g_uni_correct_inv:
forall g1: cfg non_terminal_1 terminal,
forall g2: cfg non_terminal_2 terminal,
forall s: sfu,
generates (g_uni g1 g2) s ->
(s=[inl (start_symbol (g_uni g1 g2))]) \/
(exists s1: sf1,
(s=(map g_uni_sf_lift1 s1) /\ generates g1 s1)) \/
(exists s2: sf2,
(s=(map g_uni_sf_lift2 s2) /\ generates g2 s2)).
Proof.
unfold generates.
intros g1 g2 s.
remember [inl (start_symbol (g_uni g1 g2))] as init.
intro H.
induction H.
(* Base case *)
- left. reflexivity.
- (* Induction hypothesis *)
subst.
specialize (IHderives eq_refl).
destruct IHderives.
+ (* IHderives, first case *)
destruct s2.
* (* First case, left = Start_uni *)
simpl in H1. inversion H1. subst. {
inversion H0. subst. clear H1.
- (* First case, right = start_symbol g1 *)
simpl in *.
right.
left.
exists [inl (start_symbol g1)].
split.
+ simpl. reflexivity.
+ apply derives_refl.
- (* Second case, right = start_symbol g2 *)
simpl in *.
right.
right.
exists [inl (start_symbol g2)].
split.
+ simpl. reflexivity.
+ apply derives_refl. }
* (* Second case, left = non_terminal (g_uni g1 g2 *)
simpl in H1.
inversion H1. {
destruct s2.
- simpl in H4. inversion H4.
- inversion H4. }
+ (* IHderives, second case *)
destruct H1.
* (* H1, first case: comes from g1 *)
destruct H1 as [x [H1 H2]].
{
inversion H0.
- (* First rule: Start1_uni *)
(* Contradiction, x cannot contain Start_uni *)
simpl in *. subst.
destruct s2.
+ simpl in H1.
destruct x.
* inversion H1.
* simpl in H1.
inversion H1. {
destruct s.
- inversion H4.
- inversion H4. }
+ simpl in *.
assert (IN: In (inl Start_uni) (map g_uni_sf_lift1 x)).
{
rewrite <- H1.
simpl.
right.
apply in_app_iff.
right.
simpl.
left.
reflexivity.
}
rewrite in_map_iff in IN.
destruct IN.
destruct H3.
destruct x0.
* simpl in H3. inversion H3.
* simpl in H3. inversion H3.
- (* Second rule: Start2_uni *)
(* Contradiction, x cannot contain Start_uni *)
simpl in *. subst.
destruct s2.
+ simpl in H1.
destruct x.
* inversion H1.
* simpl in H1. inversion H1. {
destruct s.
- inversion H4.
- inversion H4. }
+ simpl in *.
assert (IN: In (inl Start_uni) (map g_uni_sf_lift1 x)).
{
rewrite <- H1.
simpl.
right.
apply in_app_iff.
right.
simpl.
left.
reflexivity.
}
rewrite in_map_iff in IN.
destruct IN.
destruct H3.
destruct x0.
* simpl in H3. inversion H3.
* simpl in H3. inversion H3.
- (* Third rule: Lift1_uni *)
(* Should be true *)
right.
left.
apply map_expand in H1.
destruct H1 as [s1' [s2'[H6 [H7 H8]]]].
replace (inl left::s3) with ([inl left]++s3) in H8.
+ symmetry in H8.
apply map_expand in H8.
destruct H8 as [m [n [H9 [H10 H11]]]].
exists (s1'++s++n).
split.
* subst.
repeat rewrite map_app.
reflexivity.
* subst.
{
destruct m.
- inversion H10.
- simpl in H10.
inversion H10.
destruct s0.
+ inversion H4.
subst.
apply map_eq_nil in H5.
subst.
apply derives_step with (right:=s) in H2.
exact H2.
exact H3.
+ inversion H4.
}
+ simpl.
reflexivity.
- (* Forth rule: Lift2_uni *)
(* Should be false *)
simpl in *.
subst.
assert (IN: In (inl (Transf2_uni_nt nt)) (map g_uni_sf_lift1 x)).
{
rewrite <- H1.
rewrite in_app_iff.
right.
simpl.
left.
reflexivity.
}
rewrite in_map_iff in IN.
destruct IN as [x0 [H4 H5]].
destruct x0.
+ inversion H4.
+ inversion H4.
}
* (* H1, second case: comes from g2 *)
destruct H1 as [x [H1 H2]].
{
inversion H0.
- (* First rule: Start1_uni *)
(* Contradiction, x cannot contain Start_uni *)
simpl in *. subst.
destruct s2.
+ simpl in H1.
destruct x.
* inversion H1.
* simpl in H1.
inversion H1.
{
destruct s.
- inversion H4.
- inversion H4.
}
+ simpl in *.
assert (IN: In (inl Start_uni) (map g_uni_sf_lift2 x)).
{
rewrite <- H1.
simpl.
right.
apply in_app_iff.
right. simpl.
left.
reflexivity.
}
rewrite in_map_iff in IN.
destruct IN.
destruct H3.
destruct x0.
* simpl in H3. inversion H3.
* simpl in H3. inversion H3.
- (* Second rule: Start2_uni *)
(* Contradiction, x cannot contain Start_uni *)
simpl in *.
subst.
destruct s2.
+ simpl in H1.
destruct x.
* inversion H1.
* simpl in H1.
inversion H1.
{
destruct s.
- inversion H4.
- inversion H4.
}
+ simpl in *.
assert (IN: In (inl Start_uni) (map g_uni_sf_lift2 x)).
{
rewrite <- H1.
simpl.
right.
apply in_app_iff.
right.
simpl.
left.
reflexivity.
}
rewrite in_map_iff in IN.
destruct IN.
destruct H3.
destruct x0.
* simpl in H3.
inversion H3.
* simpl in H3.
inversion H3.
- (* Third rule: Lift1_uni *)
(* Should be false *)
simpl in *.
subst.
assert (IN: In (inl (Transf1_uni_nt nt)) (map g_uni_sf_lift2 x)).
{
rewrite <- H1.
rewrite in_app_iff.
right.
simpl.
left.
reflexivity.
}
rewrite in_map_iff in IN.
destruct IN as [x0 [H4 H5]].
destruct x0.
+ inversion H4.
+ inversion H4.
- (* Forth rule: Lift2_uni *)
(* Should be true *)
right.
right.
apply map_expand in H1.
destruct H1 as [s1' [s2'[H6 [H7 H8]]]].
replace (inl left::s3) with ([inl left]++s3) in H8.
+ symmetry in H8.
apply map_expand in H8.
destruct H8 as [m [n [H9 [H10 H11]]]].
exists (s1'++s++n).
split.
* subst.
repeat rewrite map_app.
reflexivity.
* subst.
{
destruct m.
- inversion H10.
- simpl in H10.
inversion H10.
destruct s0.
+ inversion H4.
subst.
apply map_eq_nil in H5.
subst.
apply derives_step with (right:=s) in H2.
exact H2.
exact H3.
+ inversion H4.
}
+ simpl.
reflexivity.
}
Qed.
End Union.
(* --------------------------------------------------------------------- *)
(* AS LANGUAGES *)
(* --------------------------------------------------------------------- *)
Section Union_2.
Variable non_terminal_1 non_terminal_2 terminal: Type.
Notation sentence:= (list terminal).
Lemma map_uni_1:
forall s: sentence,
map (@g_uni_sf_lift1 non_terminal_1 non_terminal_2 terminal) (map (@terminal_lift non_terminal_1 terminal) s) =
map (@terminal_lift (@g_uni_nt non_terminal_1 non_terminal_2) terminal) s.
Proof.
induction s.
- simpl.
reflexivity.
- simpl.
change (inr a) with (terminal_lift (g_uni_nt non_terminal_1 non_terminal_2) a).
rewrite IHs.
reflexivity.
Qed.
Lemma map_uni_2:
forall s: sentence,
map (@g_uni_sf_lift2 non_terminal_1 non_terminal_2 terminal) (map (@terminal_lift non_terminal_2 terminal) s) =
map (@terminal_lift (@g_uni_nt non_terminal_1 non_terminal_2) terminal) s.
Proof.
induction s.
- simpl.
reflexivity.
- simpl.
change (inr a) with (terminal_lift (g_uni_nt non_terminal_1 non_terminal_2) a).
rewrite IHs.
reflexivity.
Qed.
Lemma map_uni_3:
forall s: sentence,
forall l: list (non_terminal_1 + terminal),
map (@terminal_lift (g_uni_nt non_terminal_1 non_terminal_2) terminal) s =
map (@g_uni_sf_lift1 non_terminal_1 non_terminal_2 terminal) l ->
l = map (@terminal_lift non_terminal_1 terminal) s.
Proof.
induction s.
- intros l H.
simpl in H.
symmetry in H.
apply map_eq_nil in H.
subst.
simpl.
reflexivity.
- intros l H.
remember (terminal_lift (g_uni_nt non_terminal_1 non_terminal_2) (terminal:=terminal)) as m1.
remember (g_uni_sf_lift1 non_terminal_2 (terminal:=terminal)) as m2.
remember (terminal_lift non_terminal_1 (terminal:=terminal)) as m3.
simpl in H.
destruct l.
+ simpl in H.
inversion H.
+ simpl in H.
inversion H.
specialize (IHs l H2).
rewrite IHs.
simpl.
destruct s0.
* {
replace (m2 (inl n)) with (inl terminal (Transf1_uni_nt non_terminal_2 n)) in H1.
- replace (m1 a) with (inr (g_uni_nt non_terminal_1 non_terminal_2) a) in H1.
+ inversion H1.
+ rewrite Heqm1.
change (inr a) with (terminal_lift (g_uni_nt non_terminal_1 non_terminal_2) a).
reflexivity.
- rewrite Heqm2.
simpl.
reflexivity.
}
* {
replace (m2 (inr t)) with (inr (g_uni_nt non_terminal_1 non_terminal_2) t) in H1.
- replace (m1 a) with (inr (g_uni_nt non_terminal_1 non_terminal_2) a) in H1.
+ inversion H1.
replace (m3 t) with (inr non_terminal_1 t).
* reflexivity.
* rewrite Heqm3.
change (inr t) with (terminal_lift non_terminal_1 t).
reflexivity.
+ rewrite Heqm1.
change (inr a) with (terminal_lift (g_uni_nt non_terminal_1 non_terminal_2) a).
reflexivity.
- rewrite Heqm2.
simpl.
reflexivity.
}
Qed.
Lemma map_uni_4:
forall s: sentence,
forall l: list (non_terminal_2 + terminal),
map (@terminal_lift (@g_uni_nt non_terminal_1 non_terminal_2) terminal) s =
map (@g_uni_sf_lift2 non_terminal_1 non_terminal_2 terminal) l ->
l = map (@terminal_lift non_terminal_2 terminal) s.
Proof.
induction s.
- intros l H.
simpl in H.
symmetry in H.
apply map_eq_nil in H.
subst.
simpl.
reflexivity.
- intros l H.
remember (terminal_lift (g_uni_nt non_terminal_1 non_terminal_2) (terminal:=terminal)) as m1.
remember (g_uni_sf_lift2 non_terminal_1 (terminal:=terminal)) as m2.
remember (terminal_lift non_terminal_2 (terminal:=terminal)) as m3.
simpl in H.
destruct l.
+ simpl in H.
inversion H.
+ simpl in H.
inversion H.
specialize (IHs l H2).