forked from mvmramos/pumping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pumping.v
1046 lines (1032 loc) · 35.4 KB
/
pumping.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 Ring.
Require Import Omega.
Require Import NPeano.
Require Import misc_arith.
Require Import misc_list.
Require Import cfg.
Require Import cfl.
Require Import inaccessible.
Require Import useless.
Require Import unitrules.
Require Import emptyrules.
Require Import simplification.
Require Import trees.
Require Import allrules.
Require Import chomsky.
Require Import pigeon.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Import ListNotations.
Open Scope list_scope.
(* --------------------------------------------------------------------- *)
(* PUMPING LEMMA *)
(* --------------------------------------------------------------------- *)
Section Pumping.
Variable non_terminal terminal: Type.
Notation sentence:= (list terminal).
Lemma pumping_aux:
forall g: cfg _ _,
forall t1 t2: btree (non_terminal' non_terminal terminal) _,
forall n: _,
forall c1 c2: list bool,
forall v x: sentence,
btree_decompose t1 c1 = Some (v, t2, x) ->
btree_cnf g t1 ->
broot t1 = n ->
bcode t1 (c1 ++ c2) ->
c1 <> [] ->
broot t2 = n ->
bcode t2 c2 ->
(forall i: nat,
exists t': btree _ _,
btree_cnf g t' /\
broot t' = n /\
btree_decompose t' (iter c1 i) = Some (iter v i, t2, iter x i) /\
bcode t' (iter c1 i ++ c2) /\
get_nt_btree (iter c1 i) t' = Some n).
Proof.
induction i.
- exists t2.
simpl.
split.
+ apply btree_cnf_subtree with (t2:= t2) in H0.
* exact H0.
* {
apply btree_decompose_subtree_bcode in H.
- apply subtree_bcode_subtree in H.
exact H.
- exact H3.
}
+ split.
* exact H4.
* {
split.
- rewrite btree_decompose_empty.
reflexivity.
- split.
+ exact H5.
+ destruct t2.
* simpl in H4.
subst.
reflexivity.
* simpl in H4.
subst.
reflexivity.
}
- destruct IHi as [t' [H10 [H11 [H12 [H13 H14]]]]].
assert (H15: exists t'': btree _ _, btree_subst t' t1 (iter c1 i) = Some t'').
{
apply bcode_btree_subst with (c2:= c2).
exact H13.
}
destruct H15 as [t'' H15].
exists t''.
split.
+ apply btree_subst_preserves_cnf with (g:= g) (c1':= c2) in H15.
* exact H15.
* exact H10.
* exact H13.
* exact H0.
* congruence.
+ split.
* {
apply btree_subst_preserves_broot_v1 in H15.
- congruence.
- congruence.
}
* {
split.
- apply btree_subst_decompose with (t2:= t2) (x:= iter v i) (y:= iter x i) in H15.
+ simpl.
repeat rewrite iter_comm.
replace (iter x i ++ x) with (x ++ iter x i).
* {
apply btree_decompose_combine with (t1:= t1).
- exact H15.
- exact H.
}
* rewrite iter_comm.
reflexivity.
+ exact H12.
- split.
+ apply btree_subst_bcode with (c1':= c2) (c2:= c1 ++ c2) in H15.
* simpl.
rewrite iter_comm.
rewrite <- app_assoc.
exact H15.
* exact H13.
* exact H2.
+ simpl.
rewrite iter_comm.
{
apply btree_subst_get_nt with (c1':= c2) (c2:= c1) (c2':= c2) (n:= n) in H15.
- exact H15.
- exact H13.
- exact H2.
- apply btree_decompose_get_nt in H.
congruence.
}
}
Qed.
End Pumping.
Variable terminal: Type.
Notation sentence:= (list terminal).
Theorem pumping_lemma:
forall l: lang terminal,
(contains_empty l \/ ~ contains_empty l) /\ (contains_non_empty l \/ ~ contains_non_empty l) ->
cfl l ->
exists n: nat,
forall s: sentence,
l s ->
length s >= n ->
exists u v w x y: sentence,
s = u ++ v ++ w ++ x ++ y /\
length (v ++ x) >= 1 /\
length (u ++ y) >= 1 /\
length (v ++ w ++ x) <= n /\
forall i: nat, l (u ++ (iter v i) ++ w ++ (iter x i) ++ y).
Proof.
intros l H1 H2.
inversion H2 as [non_terminal H3].
clear H2.
destruct H3 as [g H3].
(* Find g' in CNF *)
assert (H2: exists g': cfg (chomsky.non_terminal' (emptyrules.non_terminal' non_terminal) terminal) terminal, (g_equiv g' g) /\ (is_cnf g' \/ is_cnf_with_empty_rule g') /\ start_symbol_not_in_rhs g').
{
apply g_cnf_exists.
destruct H1 as [H1 H2].
split.
- destruct H1 as [H1 | H1].
+ left.
specialize (H3 []).
destruct H3 as [H3 _].
apply H3.
exact H1.
+ right.
intros H4.
apply H1.
specialize (H3 []).
destruct H3 as [_ H3].
apply H3.
exact H4.
- destruct H2 as [H2 | H2].
+ left.
destruct H2 as [w [H2 H4]].
specialize (H3 w).
destruct H3 as [H3 _].
exists w.
split.
* apply H3.
exact H2.
* exact H4.
+ right.
intros H4.
apply H2.
destruct H4 as [w [H4 H5]].
specialize (H3 w).
destruct H3 as [_ H3].
exists w.
split.
* apply H3.
exact H4.
* exact H5.
}
destruct H2 as [g' [H2 H4]].
(* Change g for g' *)
apply lang_eq_change_g with (non_terminal':= chomsky.non_terminal' (emptyrules.non_terminal' non_terminal) terminal) (g':= g') in H3.
- assert (H3':= H3).
assert (H3copy:= H3).
destruct (rules_finite g') as [n' [ntl' [tl' H5]]].
assert (H5':= H5).
(* exists n *)
exists (2 ^ (length ntl')).
intros s H6 H7.
specialize (H3 s).
destruct H3 as [H3 _].
specialize (H3 H6).
assert (H6':= H6).
unfold lang_of_g in H3.
destruct H4 as [H4 H4'].
assert (H4copy:= H4).
unfold produces, generates in H3.
(* Find btree *)
apply derives_g_cnf_equiv_btree_v4 with (n:= start_symbol g') (s:= s) in H4.
+ destruct H4 as [t [H4 [H8 H9]]].
clear g H1 H2 H3 H5 H6.
apply length_ge with (t:= t) in H7.
assert (HHH:= H7).
* (* Find bpath *)
{
apply btree_exists_bpath with (ntl:= ntl') in H7.
- destruct H7 as [z [H20 [H21 [m [r [t0 [H22 [H23 [H24 H25]]]]]]]]].
assert (H26: forall s, In s r -> In s (map inl ntl')).
{
intros s0 H27.
specialize (H25 s0).
apply H25.
apply in_or_app.
right.
exact H27.
}
clear H25.
assert (H27: exists r': list (non_terminal' (emptyrules.non_terminal' non_terminal) terminal), r = map inl r').
{
exists (get_As _ _ r).
apply get_As_correct.
intros e H28.
specialize (H26 e H28).
apply in_split in H26.
destruct H26 as [l1 [l2 H26]].
symmetry in H26.
apply map_expand in H26.
destruct H26 as [s1' [s2' [_ [_ H26]]]].
symmetry in H26.
change (e :: l2) with ([e] ++ l2) in H26.
apply map_expand in H26.
destruct H26 as [s1'0 [s2'0 [_ [H26 _]]]].
destruct s1'0.
- simpl in H26.
inversion H26.
- simpl in H26.
inversion H26.
exists n.
reflexivity.
}
destruct H27 as [r' H27].
assert (H28: length r' = length r).
{
rewrite H27.
rewrite map_length.
reflexivity.
}
assert (H24':= H24).
rewrite <- H28 in H24.
rewrite H27 in H26.
assert (H29: forall n: non_terminal' (emptyrules.non_terminal' non_terminal) terminal, In n r' -> In n ntl').
{
intros n H99.
assert (H29: In (inl n) (map (@inl _ terminal) r')).
{
apply in_map.
exact H99.
}
specialize (H26 (inl n) H29).
apply in_split in H26.
destruct H26 as [l1 [l2 H26]].
symmetry in H26.
apply map_expand in H26.
destruct H26 as [s1' [s2' [H40 [H41 H42]]]].
symmetry in H42.
change (inl n :: l2) with ([inl n] ++ l2) in H42.
apply map_expand in H42.
destruct H42 as [s1'0 [s2'0 [H42 [H43 H44]]]].
destruct s1'0.
- simpl in H43.
inversion H43.
- simpl in H43.
inversion H43.
subst.
apply in_or_app.
right.
simpl.
left.
reflexivity.
}
apply pigeon in H29.
+ destruct H29 as [n [r1' [r2' [r3' H29]]]].
rewrite H29 in H27.
repeat rewrite map_app in H27.
(* Prepare path *)
assert (Hpath:= H22).
rewrite H27 in Hpath.
repeat rewrite <- app_assoc in Hpath.
assert (H20copy:= H20).
rewrite Hpath in H20copy.
apply bpath_insert_head in H20copy.
* destruct H20copy as [p12' H20copy].
rewrite app_assoc in Hpath.
rewrite <- H20copy in Hpath.
rewrite <- app_assoc in Hpath.
(* Find subtrees and u, v, w, x, y *)
apply bpath_exists_bcode in H20.
destruct H20 as [c [H100 H101]].
rewrite Hpath in H101.
rewrite app_assoc in H101.
assert (H101copy:= H101).
{
apply bcode_split in H101.
- destruct H101 as [c1 [c2 [H101 [H102 [t1 [u [y [H104 [H105 H106]]]]]]]]].
assert (H105copy:= H105).
assert (Ht:= H105).
apply btree_decompose_bfrontier in H105.
destruct H105 as [Ht_a Ht_b].
assert (Hc1: c1 <> []).
{
simpl in H102.
assert (length c1 > 0) by omega.
apply length_not_zero in H.
apply not_eq_sym.
exact H.
}
specialize (Ht_b Hc1).
assert (Ht_c: subtree_bcode t t1 c1).
{
apply btree_decompose_subtree_bcode in H105copy.
- exact H105copy.
- exact Hc1.
}
rewrite app_assoc in H104.
assert (H104copy:= H104).
apply bcode_split in H104.
+ destruct H104 as [c3 [c4 [H104 [H107 [t2 [v [x [H109 [H110 H111]]]]]]]]].
assert (H110copy:= H110).
assert (Ht1:= H110).
apply btree_decompose_bfrontier in H110.
destruct H110 as [Ht1_a Ht1_b].
assert (Hc3: c3 <> []).
{
simpl in H107.
assert (length c3 > 0) by omega.
apply length_not_zero in H.
apply not_eq_sym.
exact H.
}
specialize (Ht1_b Hc3).
assert (Ht1_c: subtree_bcode t1 t2 c3).
{
apply btree_decompose_subtree_bcode in H110copy.
- exact H110copy.
- exact Hc3.
}
remember (bfrontier t2) as w.
(* Find roots *)
assert (Hroot_t1: broot t1 = n).
{
apply bpath_bcode_split in H104copy.
destruct H104copy as [H104copy _].
apply bpath_broot with (d:= inl (broot t1)) in H104copy.
simpl in H104copy.
inversion H104copy.
reflexivity.
}
assert (Hroot_t2: broot t2 = n).
{
apply bpath_bcode_split in H109.
destruct H109 as [H109 _].
apply bpath_broot with (d:= inl (broot t2)) in H109.
simpl in H109.
inversion H109.
reflexivity.
}
(* Find heights *)
remember (length ntl') as k.
assert (H114: bheight t1 <= k + 1 /\ bheight t1 >= 2).
{
split.
- rewrite H106.
rewrite H27 in H24'.
repeat rewrite app_length in H24'.
repeat rewrite app_length.
simpl in H24'.
simpl.
omega.
- rewrite H106.
rewrite H27 in H24'.
repeat rewrite app_length in H24'.
repeat rewrite app_length.
simpl in H24'.
simpl.
omega.
}
assert (H115: bheight t2 <= k /\ bheight t2 >= 1).
{
split.
- rewrite H111.
rewrite H27 in H24'.
repeat rewrite app_length in H24'.
repeat rewrite app_length.
simpl in H24'.
simpl.
omega.
- rewrite H111.
rewrite H27 in H24'.
repeat rewrite app_length in H24'.
repeat rewrite app_length.
simpl in H24'.
simpl.
omega.
}
(* Exists u, v, w, x, y *)
exists u, v, w, x, y.
split.
* (* s = u ++ v ++ w ++ x ++ y *)
rewrite <- H9.
rewrite Ht_a.
rewrite Ht1_a.
repeat rewrite <- app_assoc.
reflexivity.
* {
split.
- (* length (v ++ x) >= 1 *)
apply length_not_zero_inv in Ht1_b.
omega.
- split.
+ (* length (u ++ y) >= 1 *)
apply length_not_zero_inv in Ht_b.
omega.
+ split.
* (* length (v ++ w ++ x) <= 2 ^ k *)
destruct H114 as [H114 H116].
apply bheight_le in H114.
rewrite Ht1_a in H114.
{
replace (k + 1 - 1) with k in H114.
- exact H114.
- omega.
}
* (* uv^iwx^iy *)
intros i.
assert (Ht': exists t': btree _ _,
btree_cnf g' t' /\
broot t' = n /\
btree_decompose t' (iter c3 i) = Some (iter v i, t2, iter x i) /\
bcode t' (iter c3 i ++ c4) /\
get_nt_btree (iter c3 i) t' = Some n).
{
apply pumping_aux with (g:= g') (t1:= t1) (t2:= t2) (n:= n) (c1:= c3) (c2:= c4) (v:= v) (x:= x) (i:= i).
- exact Ht1.
- apply subtree_bcode_subtree in Ht_c.
apply btree_cnf_subtree with (g:= g') in Ht_c.
+ exact Ht_c.
+ exact H4.
- exact Hroot_t1.
- rewrite <- H104.
apply bpath_bcode_split in H104copy.
apply H104copy.
- exact Hc3.
- congruence.
- apply bpath_bcode_split in H109.
apply H109.
}
destruct Ht' as [t' [Ht'_1 [Ht'_2 [Ht'_3 Ht'_4]]]].
assert (Ht'': exists t'': btree _ _, btree_subst t t' c1 = Some t'').
{
rewrite H101 in H100.
apply bcode_btree_subst with (t2:= t') in H100.
destruct H100 as [t'' H100].
exists t''.
exact H100.
}
destruct Ht'' as [t'' Ht''].
assert (Ht''_1: broot t'' = start_symbol g').
{
apply btree_subst_preserves_broot_v2 in Ht''.
- congruence.
- exact Hc1.
}
assert (Ht''_2: bfrontier t'' = u ++ iter v i ++ w ++ iter x i ++ y).
{
apply btree_subst_bfrontier with (t2:= t1) (x:= u) (y:= y) in Ht''.
- apply btree_decompose_bfrontier in Ht'_3.
destruct Ht'_3 as [Ht'_3 _].
rewrite Ht'_3 in Ht''.
rewrite Heqw.
repeat rewrite <- app_assoc in Ht''.
exact Ht''.
- exact Ht_c.
- exact Ht.
}
assert (Ht''_3: btree_cnf g' t'').
{
apply btree_subst_preserves_cnf with (g:= g') (c1':= c3 ++ c4) in Ht''.
- exact Ht''.
- exact H4.
- rewrite <- H104.
rewrite <- H101.
exact H100.
- exact Ht'_1.
- apply btree_decompose_get_nt in Ht.
congruence.
}
{
apply btree_equiv_produces_g_cnf in Ht''_3.
- unfold lang_eq in H3'.
specialize (H3' (u ++ iter v i ++ w ++ iter x i ++ y)).
destruct H3' as [_ H3'].
unfold lang_of_g in H3'.
apply H3'.
rewrite <- Ht''_2.
exact Ht''_3.
- congruence.
}
}
+ rewrite app_length.
simpl.
omega.
+ repeat rewrite app_length.
simpl.
omega.
+ rewrite H106.
repeat rewrite app_length.
reflexivity.
- rewrite app_length.
simpl.
omega.
- repeat rewrite app_length.
simpl.
omega.
- rewrite Hpath in H21.
repeat rewrite app_length in H21.
repeat rewrite app_length.
omega.
}
* rewrite H8.
{
apply start_symbol_only_once with (g:= g') (t:= t) (p1:= m ++ map inl r1') (p2:= map inl r2') (p3:= map inl r3' ++ [inr t0]).
- exact H4'.
- exact H4.
- repeat rewrite <- app_assoc.
exact H20copy.
}
+ apply (nt_eqdec g').
+ rewrite H28.
exact H24'.
- apply cnf_bnts with (g:= g') (n:= n') (tl:= tl').
+ exact H5'.
+ exact H4.
}
* exact H9.
+ assert (Hntl': 2 ^ length ntl' > 0).
{
apply pow_2_gt_0.
}
assert (Hs: length s > 0) by omega.
apply length_not_zero in Hs.
apply not_eq_sym.
exact Hs.
+ exact H4'.
+ exact H3.
- exact H2.
Qed.
Theorem pumping_lemma_v2:
forall l: lang terminal,
(contains_empty l \/ ~ contains_empty l) /\ (contains_non_empty l \/ ~ contains_non_empty l) ->
cfl l ->
exists n: nat,
forall s: sentence,
l s ->
length s >= n ->
exists u v w x y: sentence,
s = u ++ v ++ w ++ x ++ y /\
length (v ++ x) >= 1 /\
length (v ++ w ++ x) <= (n - 1) * 2 /\
forall i: nat, l (u ++ (iter v i) ++ w ++ (iter x i) ++ y).
Proof.
intros l H1 H2.
inversion H2 as [non_terminal H3].
clear H2.
destruct H3 as [g H3].
(* Find g' in CNF *)
assert (H2: exists g': cfg (chomsky.non_terminal' (emptyrules.non_terminal' non_terminal) terminal) terminal, (g_equiv g' g) /\ (is_cnf g' \/ is_cnf_with_empty_rule g') /\ start_symbol_not_in_rhs g').
{
apply g_cnf_exists.
destruct H1 as [H1 H2].
split.
- destruct H1 as [H1 | H1].
+ left.
specialize (H3 []).
destruct H3 as [H3 _].
apply H3.
exact H1.
+ right.
intros H4.
apply H1.
specialize (H3 []).
destruct H3 as [_ H3].
apply H3.
exact H4.
- destruct H2 as [H2 | H2].
+ left.
destruct H2 as [w [H2 H4]].
specialize (H3 w).
destruct H3 as [H3 _].
exists w.
split.
* apply H3.
exact H2.
* exact H4.
+ right.
intros H4.
apply H2.
destruct H4 as [w [H4 H5]].
specialize (H3 w).
destruct H3 as [_ H3].
exists w.
split.
* apply H3.
exact H4.
* exact H5.
}
destruct H2 as [g' [H2 H4]].
(* Change g for g' *)
apply lang_eq_change_g with (non_terminal':= chomsky.non_terminal' (emptyrules.non_terminal' non_terminal) terminal) (g':= g') in H3.
- assert (H3':= H3).
assert (H3copy:= H3).
destruct (rules_finite g') as [n' [ntl' [tl' H5]]].
assert (H5':= H5).
assert (H5'':= H5).
(* exists n *)
exists (2 ^ ((length ntl') - 1) + 1).
intros s H6 H7.
specialize (H3 s).
destruct H3 as [H3 _].
specialize (H3 H6).
assert (H6':= H6).
unfold lang_of_g in H3.
destruct H4 as [H4 H4'].
assert (H4copy:= H4).
unfold produces, generates in H3.
(* Find btree *)
apply derives_g_cnf_equiv_btree_v4 with (n:= start_symbol g') (s:= s) in H4.
+ destruct H4 as [t [H4 [H8 H9]]].
clear g H1 H2 H3 H5 H6.
apply length_ge_v2 with (t:= t) in H7.
assert (HHH:= H7).
* (* Find bpath *)
{
apply btree_exists_bpath with (ntl:= ntl') in H7.
- destruct H7 as [z [H20 [H21 [m [r [t0 [H22 [H23 [H24 H25]]]]]]]]].
assert (H26: forall s, In s r -> In s (map inl ntl')).
{
intros s0 H27.
specialize (H25 s0).
apply H25.
apply in_or_app.
right.
exact H27.
}
clear H25.
assert (H27: exists r': list (non_terminal' (emptyrules.non_terminal' non_terminal) terminal), r = map inl r').
{
exists (get_As _ _ r).
apply get_As_correct.
intros e H28.
specialize (H26 e H28).
apply in_split in H26.
destruct H26 as [l1 [l2 H26]].
symmetry in H26.
apply map_expand in H26.
destruct H26 as [s1' [s2' [_ [_ H26]]]].
symmetry in H26.
change (e :: l2) with ([e] ++ l2) in H26.
apply map_expand in H26.
destruct H26 as [s1'0 [s2'0 [_ [H26 _]]]].
destruct s1'0.
- simpl in H26.
inversion H26.
- simpl in H26.
inversion H26.
exists n.
reflexivity.
}
destruct H27 as [r' H27].
assert (H28: length r' = length r).
{
rewrite H27.
rewrite map_length.
reflexivity.
}
assert (H24':= H24).
rewrite <- H28 in H24.
rewrite H27 in H26.
assert (H29: forall n: non_terminal' (emptyrules.non_terminal' non_terminal) terminal, In n r' -> In n ntl').
{
intros n H99.
assert (H29: In (inl n) (map (@inl _ terminal) r')).
{
apply in_map.
exact H99.
}
specialize (H26 (inl n) H29).
apply in_split in H26.
destruct H26 as [l1 [l2 H26]].
symmetry in H26.
apply map_expand in H26.
destruct H26 as [s1' [s2' [H40 [H41 H42]]]].
symmetry in H42.
change (inl n :: l2) with ([inl n] ++ l2) in H42.
apply map_expand in H42.
destruct H42 as [s1'0 [s2'0 [H42 [H43 H44]]]].
destruct s1'0.
- simpl in H43.
inversion H43.
- simpl in H43.
inversion H43.
subst.
apply in_or_app.
right.
simpl.
left.
reflexivity.
}
apply pigeon in H29.
+ destruct H29 as [n [r1' [r2' [r3' H29]]]].
rewrite H29 in H27.
repeat rewrite map_app in H27.
(* Prepare path *)
assert (Hpath:= H22).
rewrite H27 in Hpath.
repeat rewrite <- app_assoc in Hpath.
assert (H20copy:= H20).
rewrite Hpath in H20copy.
apply bpath_insert_head in H20copy.
* destruct H20copy as [p12' H20copy].
rewrite app_assoc in Hpath.
rewrite <- H20copy in Hpath.
rewrite <- app_assoc in Hpath.
(* Find subtrees and u, v, w, x, y *)
apply bpath_exists_bcode in H20.
destruct H20 as [c [H100 H101]].
rewrite Hpath in H101.
rewrite app_assoc in H101.
assert (H101copy:= H101).
{
apply bcode_split in H101.
- destruct H101 as [c1 [c2 [H101 [H102 [t1 [u [y [H104 [H105 H106]]]]]]]]].
assert (H105copy:= H105).
assert (Ht:= H105).
apply btree_decompose_bfrontier in H105.
destruct H105 as [Ht_a Ht_b].
assert (Hc1: c1 <> []).
{
simpl in H102.
assert (length c1 > 0) by omega.
apply length_not_zero in H.
apply not_eq_sym.
exact H.
}
specialize (Ht_b Hc1).
assert (Ht_c: subtree_bcode t t1 c1).
{
apply btree_decompose_subtree_bcode in H105copy.
- exact H105copy.
- exact Hc1.
}
rewrite app_assoc in H104.
assert (H104copy:= H104).
apply bcode_split in H104.
+ destruct H104 as [c3 [c4 [H104 [H107 [t2 [v [x [H109 [H110 H111]]]]]]]]].
assert (H110copy:= H110).
assert (Ht1:= H110).
apply btree_decompose_bfrontier in H110.
destruct H110 as [Ht1_a Ht1_b].
assert (Hc3: c3 <> []).
{
simpl in H107.
assert (length c3 > 0) by omega.
apply length_not_zero in H.
apply not_eq_sym.
exact H.
}
specialize (Ht1_b Hc3).
assert (Ht1_c: subtree_bcode t1 t2 c3).
{
apply btree_decompose_subtree_bcode in H110copy.
- exact H110copy.
- exact Hc3.
}
remember (bfrontier t2) as w.
(* Find roots *)
assert (Hroot_t1: broot t1 = n).
{
apply bpath_bcode_split in H104copy.
destruct H104copy as [H104copy _].
apply bpath_broot with (d:= inl (broot t1)) in H104copy.
simpl in H104copy.
inversion H104copy.
reflexivity.
}
assert (Hroot_t2: broot t2 = n).
{
apply bpath_bcode_split in H109.
destruct H109 as [H109 _].
apply bpath_broot with (d:= inl (broot t2)) in H109.
simpl in H109.
inversion H109.
reflexivity.
}
(* Find heights *)
remember (length ntl') as k.
assert (H114: bheight t1 <= k + 1 /\ bheight t1 >= 2).
{
split.
- rewrite H106.
rewrite H27 in H24'.
repeat rewrite app_length in H24'.
repeat rewrite app_length.
simpl in H24'.
simpl.
omega.
- rewrite H106.
rewrite H27 in H24'.
repeat rewrite app_length in H24'.
repeat rewrite app_length.
simpl in H24'.
simpl.
omega.
}
assert (H115: bheight t2 <= k /\ bheight t2 >= 1).
{
split.
- rewrite H111.
rewrite H27 in H24'.
repeat rewrite app_length in H24'.
repeat rewrite app_length.
simpl in H24'.
simpl.
omega.
- rewrite H111.
rewrite H27 in H24'.
repeat rewrite app_length in H24'.
repeat rewrite app_length.
simpl in H24'.
simpl.
omega.
}
(* Exists u, v, w, x, y *)
exists u, v, w, x, y.
split.
* (* s = u ++ v ++ w ++ x ++ y *)
rewrite <- H9.
rewrite Ht_a.
rewrite Ht1_a.
repeat rewrite <- app_assoc.
reflexivity.
* {
split.
- (* length (v ++ x) >= 1 *)
apply length_not_zero_inv in Ht1_b.
omega.
- split.
+ (* length (v ++ w ++ x) <= 2 ^ k *)
destruct H114 as [H114 H116].
apply bheight_le in H114.
rewrite Ht1_a in H114.
replace (k + 1 - 1) with k in H114.
* {
replace ((2 ^ (k - 1) + 1 - 1) * 2) with (2 ^ k).
- exact H114.
- replace (2 ^ (k - 1) + 1 - 1) with (2 ^ (k - 1)).
+ replace k with (k - 1 + 1) at 1.
* rewrite Nat.pow_add_r.
simpl.
reflexivity.
* omega.
+ omega.
}
* omega.
+ (* uv^iwx^iy *)
intros i.
assert (Ht': exists t': btree _ _,
btree_cnf g' t' /\
broot t' = n /\
btree_decompose t' (iter c3 i) = Some (iter v i, t2, iter x i) /\
bcode t' (iter c3 i ++ c4) /\
get_nt_btree (iter c3 i) t' = Some n).
{
apply pumping_aux with (g:= g') (t1:= t1) (t2:= t2) (n:= n) (c1:= c3) (c2:= c4) (v:= v) (x:= x) (i:= i).
- exact Ht1.
- apply subtree_bcode_subtree in Ht_c.
apply btree_cnf_subtree with (g:= g') in Ht_c.
+ exact Ht_c.
+ exact H4.
- exact Hroot_t1.
- rewrite <- H104.
apply bpath_bcode_split in H104copy.
apply H104copy.
- exact Hc3.
- congruence.
- apply bpath_bcode_split in H109.
apply H109.
}
destruct Ht' as [t' [Ht'_1 [Ht'_2 [Ht'_3 Ht'_4]]]].
assert (Ht'': exists t'': btree _ _, btree_subst t t' c1 = Some t'').
{
rewrite H101 in H100.
apply bcode_btree_subst with (t2:= t') in H100.
destruct H100 as [t'' H100].
exists t''.
exact H100.
}
destruct Ht'' as [t'' Ht''].
assert (Ht''_1: broot t'' = start_symbol g').
{
apply btree_subst_preserves_broot_v2 in Ht''.
- congruence.
- exact Hc1.
}
assert (Ht''_2: bfrontier t'' = u ++ iter v i ++ w ++ iter x i ++ y).
{
apply btree_subst_bfrontier with (t2:= t1) (x:= u) (y:= y) in Ht''.
- apply btree_decompose_bfrontier in Ht'_3.
destruct Ht'_3 as [Ht'_3 _].
rewrite Ht'_3 in Ht''.
rewrite Heqw.
repeat rewrite <- app_assoc in Ht''.
exact Ht''.
- exact Ht_c.
- exact Ht.
}
assert (Ht''_3: btree_cnf g' t'').
{
apply btree_subst_preserves_cnf with (g:= g') (c1':= c3 ++ c4) in Ht''.
- exact Ht''.
- exact H4.
- rewrite <- H104.
rewrite <- H101.
exact H100.
- exact Ht'_1.
- apply btree_decompose_get_nt in Ht.
congruence.
}
apply btree_equiv_produces_g_cnf in Ht''_3.
* unfold lang_eq in H3'.
specialize (H3' (u ++ iter v i ++ w ++ iter x i ++ y)).
destruct H3' as [_ H3'].
unfold lang_of_g in H3'.
apply H3'.
rewrite <- Ht''_2.
exact Ht''_3.
* congruence.
}
+ rewrite app_length.
simpl.
omega.
+ repeat rewrite app_length.
simpl.
omega.
+ rewrite H106.
repeat rewrite app_length.
reflexivity.
- rewrite app_length.
simpl.