forked from mvmramos/pumping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
concatenation.v
1147 lines (1107 loc) · 32.4 KB
/
concatenation.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.
(* --------------------------------------------------------------------- *)
(* CONCATENATION - DEFINITIONS *)
(* --------------------------------------------------------------------- *)
Section Concatenation.
Variables non_terminal_1 non_terminal_2 terminal: Type.
Inductive g_cat_nt: Type:=
| Start_cat
| Transf1_cat_nt: non_terminal_1 -> g_cat_nt
| Transf2_cat_nt: non_terminal_2 -> g_cat_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_cat_nt, {x=y}+{x<>y}).
Proof. decide equality. Qed.
Notation sf1:= (list (non_terminal_1 + terminal)).
Notation sf2:= (list (non_terminal_2 + terminal)).
Notation sfc:= (list (g_cat_nt + terminal)).
Notation nlist:= (list g_cat_nt).
Notation tlist:= (list terminal).
Definition g_cat_sf_lift1 (c: non_terminal_1 + terminal): g_cat_nt + terminal:=
match c with
| inl nt => inl (Transf1_cat_nt nt)
| inr t => inr t
end.
Definition g_cat_sf_lift2 (c: non_terminal_2 + terminal): g_cat_nt + terminal:=
match c with
| inl nt => inl (Transf2_cat_nt nt)
| inr t => inr t
end.
Inductive g_cat_rules (g1: cfg non_terminal_1 terminal) (g2: cfg non_terminal_2 terminal): g_cat_nt -> sfc -> Prop :=
| New_cat: g_cat_rules g1 g2 Start_cat ([inl (Transf1_cat_nt (start_symbol g1))]++[inl (Transf2_cat_nt (start_symbol g2))])
| Lift1_cat: forall nt s,
rules g1 nt s ->
g_cat_rules g1 g2 (Transf1_cat_nt nt) (map g_cat_sf_lift1 s)
| Lift2_cat: forall nt s,
rules g2 nt s ->
g_cat_rules g1 g2 (Transf2_cat_nt nt) (map g_cat_sf_lift2 s).
Lemma g_cat_finite:
forall g1: cfg non_terminal_1 terminal,
forall g2: cfg non_terminal_2 terminal,
exists n: nat,
exists ntl: list g_cat_nt,
exists tl: tlist,
In Start_cat ntl /\
forall left: g_cat_nt,
forall right: sfc,
g_cat_rules g1 g2 left right ->
(length right <= n) /\
(In left ntl) /\
(forall s: g_cat_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 (S (max n1 n2))), (Start_cat :: (map Transf1_cat_nt ntl1) ++ (map Transf2_cat_nt ntl2)), (tl1 ++ tl2).
split.
- simpl.
left.
reflexivity.
- split.
+ inversion H.
* 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.
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.
* {
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_cat :: l2) with ([inl Start_cat] ++ 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_cat_nt n) :: l2) with ([inl (Transf1_cat_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_cat_nt n) :: l2) with ([inl (Transf2_cat_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_cat :: l2) with ([inl Start_cat] ++ 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_cat_nt n) :: l2) with ([inl (Transf1_cat_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_cat_nt n) :: l2) with ([inl (Transf2_cat_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.
* {
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_cat (g1: cfg non_terminal_1 terminal) (g2: cfg non_terminal_2 terminal): (cfg g_cat_nt terminal):= {|
start_symbol:= Start_cat;
rules:= g_cat_rules g1 g2;
t_eqdec:= t_eqdec g1;
nt_eqdec:= nt_eqdec' (nt_eqdec g1) (nt_eqdec g2);
rules_finite:= g_cat_finite g1 g2
|}.
(* --------------------------------------------------------------------- *)
(* CONCATENATION - LEMMAS *)
(* --------------------------------------------------------------------- *)
Lemma derives_add_cat_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_cat g1 g2) (map g_cat_sf_lift1 s) (map g_cat_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_cat_nt left)).
exact H2.
simpl.
apply Lift1_cat.
apply H3.
Qed.
Lemma derives_add_cat_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_cat g1 g2) (map g_cat_sf_lift2 s) (map g_cat_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_cat_nt left)).
exact H2.
simpl.
apply Lift2_cat.
apply H3.
Qed.
Lemma g_cat_correct_aux:
forall g1: cfg non_terminal_1 terminal,
forall g2: cfg non_terminal_2 terminal,
forall s11 s12: sf1,
forall s21 s22: sf2,
derives g1 s11 s12 /\ derives g2 s21 s22 ->
derives (g_cat g1 g2)
((map g_cat_sf_lift1 s11)++(map g_cat_sf_lift2 s21))
((map g_cat_sf_lift1 s12)++(map g_cat_sf_lift2 s22)).
Proof.
intros g1 g2 s11 s12 s21 s22 [H1 H2].
induction H1, H2.
- apply derives_refl.
- rewrite map_app.
rewrite map_app.
apply derives_trans with (s2:=
(map g_cat_sf_lift1 s ++
map g_cat_sf_lift2 (s2 ++ [inl left] ++ s3))).
apply derives_context_free_add_left.
apply derives_add_cat_right.
simpl.
exact H2.
apply derives_context_free_add_left.
rewrite map_app.
rewrite map_app.
simpl.
apply derives_step with (left:= Transf2_cat_nt left).
+ apply derives_refl.
+ simpl.
apply Lift2_cat.
exact H.
- apply derives_context_free_add_right.
apply derives_add_cat_left.
apply derives_step with (left:=left).
+ exact H1.
+ exact H.
- rewrite map_app.
rewrite map_app.
rewrite <- app_assoc.
rewrite <- app_assoc.
rewrite map_app.
rewrite map_app.
rewrite map_app in IHderives.
rewrite map_app in IHderives.
rewrite <- app_assoc in IHderives.
simpl in IHderives.
rewrite map_app in IHderives.
remember ((map g_cat_sf_lift1 s1 ++
map g_cat_sf_lift2 s0)) as w1.
remember (map g_cat_sf_lift1 s3 ++
map g_cat_sf_lift2 s4 ++
map g_cat_sf_lift2 right0 ++
map g_cat_sf_lift2 s5) as w2.
apply derives_step with (left:= Transf1_cat_nt left).
exact IHderives.
simpl.
apply Lift1_cat.
exact H.
Qed.
Theorem g_cat_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 g2 s2 ->
generates (g_cat g1 g2)
((map g_cat_sf_lift1 s1)++(map g_cat_sf_lift2 s2)).
Proof.
unfold generates.
intros g1 g2 s1 s2 H.
destruct H as [H1 H2].
apply derives_trans with (s2:= (map g_cat_sf_lift1 [(inl (start_symbol g1))] ++ map g_cat_sf_lift2 [(inl (start_symbol g2))])).
- match goal with
| |- derives _ ?s1 ?s2 =>
change s1 with ([] ++ s1);
change s2 with ([] ++ s2 ++ [])
end.
apply derives_step with (left:= (start_symbol (g_cat g1 g2))).
+ apply derives_refl.
+ simpl.
apply New_cat.
- apply g_cat_correct_aux.
split.
+ exact H1.
+ exact H2.
Qed.
Theorem g_cat_correct_inv:
forall g1: cfg non_terminal_1 terminal,
forall g2: cfg non_terminal_2 terminal,
forall s: sfc,
generates (g_cat g1 g2) s ->
s = [inl (start_symbol (g_cat g1 g2))] \/
exists s1: sf1,
exists s2: sf2,
s =(map g_cat_sf_lift1 s1)++(map g_cat_sf_lift2 s2) /\
generates g1 s1 /\
generates g2 s2.
Proof.
unfold generates.
intros g1 g2 s.
remember ([inl (start_symbol (g_cat g1 g2))]) as init.
intros H.
induction H.
- left. reflexivity.
- subst.
specialize (IHderives eq_refl).
destruct IHderives.
+ (* IHderives, first case *)
(* A primeira cadeia gerada é a raiz da gramática *)
destruct s2.
* (* Se s2 é vazio, a primeira regra está sendo aplicada *)
simpl in H1. inversion H1. subst. clear H1.
simpl in H0. inversion H0. clear H0 H2.
simpl in *.
right.
exists [inl (start_symbol g1)], [inl (start_symbol g2)].
{
split.
- simpl. reflexivity.
- split. apply derives_refl. apply derives_refl.
}
* (* Se s2 não é vazio, temos uma contradição *)
simpl in H1.
{
destruct s2.
- simpl in *. inversion H1.
- simpl in *. inversion H1.
}
+ (* IHderives, second case *)
(* A primeira cadeia gerada é a concatenação de map x com map x0 *)
destruct H1 as (? & ? & ? & ? & ?).
(* Análise da regra usada *)
inversion H0.
subst.
clear H0.
* (* Case 1: rule New_cat *)
(* Contradiction: H1 cannot contain Start_cat *)
assert (IN : In (inl Start_cat) (map g_cat_sf_lift1 x ++ map g_cat_sf_lift2 x0)).
{
rewrite <- H1.
rewrite in_app_iff.
right.
simpl.
left.
reflexivity.
}
rewrite in_app_iff in IN.
(* Start_cat is in x or x0 *)
{
destruct IN.
- (* Start_cat is in x, contradiction *)
rewrite in_map_iff in H0.
destruct H0 as (? & ? & ?).
(* But Start_cat = lift x1 and x cannot contain x1 *)
destruct x1.
+ simpl in H0.
inversion H0.
+ simpl in H0.
inversion H0.
- (* Start_cat is in x0, contradiction *)
rewrite in_map_iff in H0.
destruct H0 as (? & ? & ?).
destruct x1.
+ simpl in H0.
inversion H0.
+ simpl in H0.
inversion H0.
}
* (* Case 2: rule Lift1_cat *)
(* A rule of g1 has been used *)
subst.
(* Consider both situations in H1 *)
assert (IN : In (inl (Transf1_cat_nt nt)) (map g_cat_sf_lift1 x ++ map g_cat_sf_lift2 x0)).
{
rewrite <- H1.
rewrite in_app_iff.
right.
simpl.
left.
reflexivity.
}
rewrite in_app_iff in IN.
(* Transf1_cat_nt is in x or x0 *)
{
destruct IN.
- (* Transf1_cat_nt is in x, ok *)
apply equal_app in H1.
destruct H1.
+ destruct H1 as [l [H1 H6]].
right.
destruct l.
* {
destruct x0.
- simpl in H6.
inversion H6.
- simpl in H6.
inversion H6.
destruct s0.
+ inversion H8.
+ inversion H8.
}
* inversion H6.
subst.
clear H6.
{
apply derives_step with (right:=(map g_cat_sf_lift1 s)) in H.
- symmetry in H1.
apply map_expand in H1.
destruct H1 as [s1' [s2' [H6 [H7 H8]]]].
replace (inl (Transf1_cat_nt nt) :: l) with ([inl (Transf1_cat_nt nt)]++l) in H8.
+ symmetry in H8.
apply map_expand in H8.
destruct H8 as [s1'' [s2'' [H9 [H10 H11]]]].
exists (s1'++s++s2''), x0.
split.
* repeat rewrite map_app.
subst.
repeat rewrite <- app_assoc.
reflexivity.
* {
split.
- subst.
destruct s1''.
+ simpl in H10.
inversion H10.
+ inversion H10.
destruct s0.
* inversion H6.
apply map_eq_nil in H7.
subst.
{
apply derives_step with (right:= s) in H2.
- exact H2.
- exact H4.
}
* inversion H6.
- exact H3.
}
+ simpl.
reflexivity.
- exact H0.
}
+ destruct H1 as [l [H1 H6]].
assert (IN: In (inl (Transf1_cat_nt nt)) (map g_cat_sf_lift2 x0)).
{
rewrite H6.
apply in_app_iff.
right.
simpl.
left.
reflexivity.
}
apply in_map_iff in IN.
destruct IN.
destruct H7.
destruct x1.
inversion H7.
inversion H7.
- (* Transf1_cat_nt is in x0, contradiction *)
rewrite in_map_iff in H5.
destruct H5.
destruct H5.
destruct x1.
+ inversion H5.
+ inversion H5.
}
* (* Case 3: rule Lift2_cat *)
(* A rule of g2 has been used *)
subst.
(* Consider both situations in H1 *)
assert (IN : In (inl (Transf2_cat_nt nt)) (map g_cat_sf_lift1 x ++ map g_cat_sf_lift2 x0)).
{
rewrite <- H1.
rewrite in_app_iff.
right.
simpl.
left.
reflexivity.
}
rewrite in_app_iff in IN.
(* Transf2_cat_nt is in x or x0 *)
{
destruct IN.
- (* Transf2_cat_nt is in x, contradiction *)
rewrite in_map_iff in H5.
destruct H5.
destruct H5.
destruct x1.
+ inversion H5.
+ inversion H5.
- (* Transf2_cat_nt is in x0, ok *)
apply equal_app in H1.
destruct H1.
+ destruct H1 as [l [H1 H6]].
right.
destruct l.
* {
destruct x0.
- simpl in H6.
inversion H6.
- simpl in H6.
inversion H6.
rewrite app_nil_r in H1.
subst.
exists x.
exists (s++x0).
split.
rewrite map_app.
reflexivity.
split.
exact H2.
inversion H6.
destruct s0.
+ simpl in H7.
inversion H7.
subst.
rewrite <- app_nil_l in H3.
apply derives_step with (right:=s) in H3.
* exact H3.
* exact H4.
+ inversion H7.
}
* inversion H6.
subst.
clear H6.
assert (IN : In (inl (Transf2_cat_nt nt)) (map g_cat_sf_lift1 x)).
{
rewrite H1.
rewrite in_app_iff.
right.
simpl.
left.
reflexivity.
}
rewrite in_map_iff in IN.
destruct IN as [x0' [H6 H7]].
{
destruct x0'.
- simpl in H6.
inversion H6.
- inversion H6.
}
+ destruct H1 as [l [H7 H8]].
subst.
symmetry in H8.
apply map_expand in H8.
destruct H8 as [ s1' [s2' [H10 [H11 H12]]]].
subst.
destruct s2'.
* inversion H12.
* inversion H12.
subst.
right.
exists x.
exists (s1'++s++s2').
split.
rewrite <- app_assoc.
repeat rewrite map_app.
reflexivity.
split.
exact H2.
{
destruct s0.
- inversion H6.
subst.
apply derives_step with (right:=s) in H3.
exact H3.
exact H4.
- inversion H6.
}
}
Qed.
End Concatenation.
(* --------------------------------------------------------------------- *)
(* AS LANGUAGES *)
(* --------------------------------------------------------------------- *)
Section Concatenation_2.
Variable non_terminal_1 non_terminal_2 terminal: Type.
Notation sentence:= (list terminal).
Inductive l_cat (l1 l2: lang terminal): lang terminal:=
| l_cat_app: forall s1 s2: sentence, l1 s1 -> l2 s2 -> l_cat l1 l2 (s1 ++ s2).
(*
Definition l_cat (l1 l2: lang terminal): lang terminal:=
fun s: sentence => exists s1 s2: sentence, s = s1 ++ s2 /\ l1 s1 /\ l2 s2.
*)
Lemma map_cat_1:
forall s: sentence,
map (@g_cat_sf_lift1 non_terminal_1 non_terminal_2 terminal) (map (@terminal_lift non_terminal_1 terminal) s) =
map (@terminal_lift (@g_cat_nt non_terminal_1 non_terminal_2) terminal) s.
Proof.
induction s.
- simpl.
reflexivity.
- simpl.
change (inr a) with (terminal_lift (g_cat_nt non_terminal_1 non_terminal_2) a).
rewrite IHs.
reflexivity.
Qed.
Lemma map_cat_2:
forall s: sentence,
map (@g_cat_sf_lift2 non_terminal_1 non_terminal_2 terminal) (map (@terminal_lift non_terminal_2 terminal) s) =
map (@terminal_lift (@g_cat_nt non_terminal_1 non_terminal_2) terminal) s.
Proof.
induction s.
- simpl.
reflexivity.
- simpl.
change (inr a) with (terminal_lift (g_cat_nt non_terminal_1 non_terminal_2) a).
rewrite IHs.
reflexivity.
Qed.
Lemma map_cat_3:
forall s: sentence,
forall l: list (non_terminal_1 + terminal),
map (@terminal_lift (@g_cat_nt non_terminal_1 non_terminal_2) terminal) s =
map (@g_cat_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_cat_nt non_terminal_1 non_terminal_2) (terminal:=terminal)) as m1.
remember (g_cat_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_cat_nt non_terminal_2 n)) in H1.
- replace (m1 a) with (inr (g_cat_nt non_terminal_1 non_terminal_2) a) in H1.
+ inversion H1.
+ rewrite Heqm1.
change (inr a) with (terminal_lift (g_cat_nt non_terminal_1 non_terminal_2) a).
reflexivity.
- rewrite Heqm2.
simpl.
reflexivity.
}
* {
replace (m2 (inr t)) with (inr (g_cat_nt non_terminal_1 non_terminal_2) t) in H1.
- replace (m1 a) with (inr (g_cat_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_cat_nt non_terminal_1 non_terminal_2) a).
reflexivity.
- rewrite Heqm2.
simpl.
reflexivity.
}
Qed.
Lemma map_cat_4:
forall s: sentence,
forall l: list (non_terminal_2 + terminal),
map (@terminal_lift (@g_cat_nt non_terminal_1 non_terminal_2) terminal) s =
map (@g_cat_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.