-
Notifications
You must be signed in to change notification settings - Fork 0
/
chomsky.v
2333 lines (2249 loc) · 58.8 KB
/
chomsky.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 is part of a repository containing the definitions and
proof scripts related to the formalization of context-free language
theory in Coq. Specifically, the following results were obtained:
(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 thesis "Formalization of
Context-Free Language Theory", submitted to the Informatics
Center of the Pernambuco Federal University (CIn/UFPE) in
Brazil.
The file README.md descbrides the contents of each file and
provides instructions to compile them.
Marcus Vinícius Midena Ramos
--------------------------------------------------------------------- *)
(* --------------------------------------------------------------------- *)
(* CHOMSKY NORMAL FORM *)
(* --------------------------------------------------------------------- *)
Require Import List.
Require Import Ring.
Require Import Omega.
Require Import misc_arith.
Require Import misc_list.
Require Import cfg.
Require Import inaccessible.
Require Import useless.
Require Import unitrules.
Require Import emptyrules.
Require Import simplification.
Require Import allrules.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Import ListNotations.
Open Scope list_scope.
(* --------------------------------------------------------------------- *)
(* DEFINITIONS *)
(* --------------------------------------------------------------------- *)
Section Chomsky_Definitions_1.
Variables non_terminal terminal: Type.
Notation sf:= (list (non_terminal + terminal)).
Definition is_cnf_rule (left: non_terminal) (right: sf): Prop:=
(exists s1 s2: non_terminal, right = [inl s1; inl s2]) \/
(exists t: terminal, right = [inr t]).
Definition is_cnf (g: cfg non_terminal terminal): Prop:=
forall left: non_terminal,
forall right: sf,
rules g left right -> is_cnf_rule left right.
Definition is_cnf_with_empty_rule (g: cfg non_terminal terminal): Prop:=
forall left: non_terminal,
forall right: sf,
rules g left right ->
(left = (start_symbol g) /\ right = []) \/
is_cnf_rule left right.
End Chomsky_Definitions_1.
Section Chomsky_Definitions_2.
Variables non_terminal terminal: Type.
Notation sentence := (list terminal).
Notation sf := (list (non_terminal + terminal)).
Notation ntlist:= (list non_terminal).
Notation term_lift:= ((terminal_lift non_terminal) terminal).
Notation symbol:= (non_terminal + terminal)%type.
Notation slist:= (list sf).
Notation vlist:= (list symbol).
Inductive non_terminal': Type:=
| Lift_r: sf -> non_terminal'.
Notation sf':= (list (non_terminal' + terminal)).
Notation ntlist':= (list non_terminal').
Notation tlist:= (list terminal).
Definition non_terminal_lift (n: non_terminal): (non_terminal + terminal):= inl n.
Definition non_terminal'_lift (n: non_terminal'): (non_terminal' + terminal):= inl n.
Definition terminal_lift' (t: terminal): (non_terminal' + terminal):= inr t.
Definition symbol_lift (s: non_terminal + terminal): non_terminal' + terminal:=
match s with
| inr t => inr t
| inl n => inl (Lift_r [inl n])
end.
Inductive g_cnf_rules (g: cfg non_terminal terminal): non_terminal' -> sf' -> Prop:=
| Lift_cnf_t: forall t: terminal,
forall left: non_terminal,
forall s1 s2: sf,
rules g left (s1++[inr t]++s2) ->
g_cnf_rules g (Lift_r [inr t]) [inr t]
| Lift_cnf_1: forall left: non_terminal,
forall t: terminal,
rules g left [inr t] ->
g_cnf_rules g (Lift_r [inl left]) [inr t]
| Lift_cnf_2: forall left: non_terminal,
forall s1 s2: symbol,
forall beta: sf,
rules g left (s1 :: s2 :: beta) ->
g_cnf_rules g (Lift_r [inl left]) [inl (Lift_r [s1]); inl (Lift_r (s2 :: beta))]
| Lift_cnf_3: forall left: sf,
forall s1 s2 s3: symbol,
forall beta: sf,
g_cnf_rules g (Lift_r left) [inl (Lift_r [s1]); inl (Lift_r (s2 :: s3 :: beta))] ->
g_cnf_rules g (Lift_r (s2 :: s3 :: beta)) [inl (Lift_r [s2]); inl (Lift_r (s3 :: beta))].
(* --------------------------------------------------------------------- *)
(* ALL NTS (non-terminals) *)
(* --------------------------------------------------------------------- *)
Fixpoint all_nts (l: slist): ntlist':=
match l with
| [] => []
| s :: l' => Lift_r s :: all_nts l'
end.
Lemma all_nts_app:
forall l1 l2: slist,
all_nts (l1 ++ l2) = all_nts l1 ++ all_nts l2.
Proof.
induction l1.
- simpl.
reflexivity.
- intros l2.
simpl.
rewrite (IHl1 l2).
reflexivity.
Qed.
Lemma all_nts_single:
forall s: symbol,
forall l1 l2: vlist,
forall n: nat,
n >= 1 ->
In (Lift_r [s]) (all_nts (all_sf_up_to n (l1 ++ s :: l2))).
Proof.
intros s l1 l2 n.
revert l2.
revert l1.
revert s.
induction n.
- intros s l1 l2 H1.
omega.
- intros s l1 l2 H1.
assert (H2: n = 0 \/ n >= 1) by omega.
destruct H2 as [H2 | H2].
+ subst.
simpl.
rewrite all_nts_app.
apply in_or_app.
left.
rewrite concat_list_app_left.
rewrite all_nts_app.
apply in_or_app.
right.
simpl.
left.
reflexivity.
+ simpl.
rewrite all_nts_app.
apply in_or_app.
right.
apply IHn.
exact H2.
Qed.
Fixpoint all_nts_up_to (n: nat) (v: vlist): ntlist':=
match n with
| 0 => [Lift_r []]
| S n' => all_nts (all_sf_with n v) ++ all_nts_up_to n' v
end.
Lemma all_nts_up_to_correct_aux:
forall n: nat,
forall s: symbol,
forall v: vlist,
forall s0: sf,
In s v ->
In s0 (all_sf_with n v) ->
In (Lift_r (s :: s0)) (all_nts (concat_list v (all_sf_with n v))).
Proof.
induction n.
- intros s v s0 H1 H2.
simpl in H2.
destruct H2 as [H2 | H2].
+ subst.
simpl.
apply in_split in H1.
destruct H1 as [l1 [l2 H1]].
subst.
assert (H: In [s] (concat_list (l1 ++ s :: l2) [[]])).
{
apply concat_list_empty.
}
apply in_split in H.
destruct H as [l0 [l3 H]].
rewrite H.
rewrite all_nts_app.
apply in_or_app.
right.
simpl.
left.
reflexivity.
+ contradiction.
- intros s v s0 H1 H2.
simpl.
simpl in H2.
apply in_split in H2.
destruct H2 as [l1 [l2 H2]].
rewrite H2.
apply in_split in H1.
destruct H1 as [l1' [l2' H1]].
rewrite H1.
assert (H3: In (s :: s0) (concat_list (l1' ++ s :: l2') (l1 ++ s0 :: l2))).
{
apply concat_list_non_empty.
}
apply in_split in H3.
destruct H3 as [l0 [l3 H3]].
rewrite H3.
rewrite all_nts_app.
apply in_or_app.
right.
simpl.
left.
reflexivity.
Qed.
Lemma all_nts_up_to_correct:
forall n: nat,
forall s: sf,
forall v: vlist,
(forall e: symbol, In e s -> In e v) ->
length s <= n ->
In (Lift_r s) (all_nts_up_to n v).
Proof.
induction n.
- intros s v H1 H2.
assert (length s = 0) by omega.
apply length_zero in H.
subst.
simpl.
left.
reflexivity.
- intros s v H1 H2.
simpl.
apply in_or_app.
assert (H3: length s = S n \/ length s <= n) by omega.
clear H2.
destruct H3 as [H3 | H3].
+ left.
destruct s.
* simpl in H3.
omega.
* {
apply all_nts_up_to_correct_aux.
- apply H1.
simpl.
left.
reflexivity.
- simpl in H3.
apply eq_add_S in H3.
apply all_sf_with_correct.
+ intros e H4.
apply H1.
simpl.
right.
exact H4.
+ exact H3.
}
+ right.
apply IHn.
* exact H1.
* exact H3.
Qed.
Lemma rules_g_cnf_to_rules_g_right:
forall g: cfg _ _,
forall right2: sf,
forall n1 n2: non_terminal',
forall s1 s2: symbol,
g_cnf_rules g n1 [inl n2; inl (Lift_r (s1 :: s2 :: right2))] ->
exists left: non_terminal,
exists right1: sf,
rules g left (right1 ++ s1 :: s2 :: right2).
Proof.
intros g right2 n1 n2 s1 s2 H.
remember [inl n2; inl (Lift_r (s1 :: s2 :: right2))] as w.
generalize n2 s1 s2 right2 Heqw.
clear n2 s1 s2 right2 Heqw.
induction H.
- intros n2 s1' s2' right2 H'.
inversion H'.
- intros n2 s1' s2' right2 H2'.
inversion H2'.
- intros n2 s0 s3 right2 H2.
inversion H2.
destruct n2.
inversion H1.
destruct beta.
+ inversion H4.
+ inversion H4.
subst.
exists left.
exists [s1].
exact H.
- intros n2 s0 s4 right2 H1.
inversion H1.
destruct n2.
destruct beta.
+ inversion H4.
+ inversion H4.
subst.
inversion H1.
subst.
clear H H1 H2 H4.
specialize (IHg_cnf_rules (Lift_r [s1]) s2 s0 (s4 :: right2)).
specialize (IHg_cnf_rules (eq_refl ([inl (Lift_r [s1]); inl (Lift_r (s2 :: s0 :: s4 :: right2))]))).
destruct IHg_cnf_rules as [left0 [right1 H]].
exists left0.
exists (right1 ++ [s2]).
rewrite <- app_assoc.
exact H.
Qed.
Lemma rules_g_cnf_to_rules_g_left:
forall g: cfg _ _,
forall s1: sf,
forall s2: sf',
g_cnf_rules g (Lift_r s1) s2 ->
(exists t: terminal, s1 = [inr t]
/\ exists left s1 s2, rules g left (s1++[inr t]++s2)
) \/
(exists n: non_terminal, s1 = [inl n]
/\ exists right, rules g n right
) \/
(exists left: non_terminal,
exists right s3 s4: sf,
rules g left right /\
right = s3 ++ s1 ++ s4).
Proof.
intros g s1 s2 H.
inversion H.
- subst.
left.
exists t.
split; [reflexivity|].
eauto.
- subst.
right.
left.
exists left.
split; [reflexivity|eauto].
- subst.
right.
left.
exists left.
split; [reflexivity|eauto].
- subst.
right.
right.
apply rules_g_cnf_to_rules_g_right in H1.
destruct H1 as [left0 [right1 H1]].
exists left0.
exists (right1 ++ s3 :: s4 :: beta).
exists right1.
exists (@nil (non_terminal + terminal)).
split.
+ exact H1.
+ rewrite app_nil_r.
reflexivity.
Qed.
Lemma g_cnf_right_formats:
forall g: cfg _ _,
forall left: non_terminal',
forall right: sf',
g_cnf_rules g left right ->
(exists t: terminal, right = [inr t]) \/
(exists s1: symbol,
exists s: sf,
right = [inl (Lift_r [s1]); inl (Lift_r s)]).
Proof.
intros g left right H.
inversion H.
- subst.
left.
exists t.
reflexivity.
- subst.
left.
exists t.
reflexivity.
- subst.
right.
destruct s1.
+ exists (inl terminal n).
exists (s2 :: beta).
simpl.
reflexivity.
+ exists (inr non_terminal t).
exists (s2 :: beta).
simpl.
reflexivity.
- subst.
right.
destruct s2.
+ exists (inl terminal n).
exists (s3 :: beta).
simpl.
reflexivity.
+ exists (inr non_terminal t).
exists (s3 :: beta).
simpl.
reflexivity.
Qed.
Lemma is_cnf_right_formats_v1:
forall g: cfg non_terminal' terminal,
forall left: non_terminal',
forall right: sf',
is_cnf g ->
rules g left right ->
(exists t: terminal, right = [inr t]) \/
(exists s1 s2: sf,
right = [inl (Lift_r s1); inl (Lift_r s2)]).
Proof.
intros g left right H1 H2.
specialize (H1 left right H2).
destruct H1 as [H1 | H1].
- destruct H1 as [s1 [s2 H1]].
right.
destruct s1, s2.
exists l.
exists l0.
exact H1.
- destruct H1 as [t H1].
left.
exists t.
exact H1.
Qed.
Lemma cnf_rules_not_empty:
forall g: cfg _ _,
forall n: non_terminal,
forall s: sf,
is_cnf g \/ is_cnf_with_empty_rule g ->
n <> start_symbol g ->
rules g n s ->
s <> [].
Proof.
intros g n s H1 H2 H3.
destruct H1 as [H1 | H1].
- specialize (H1 n s H3).
destruct H1 as [H1 | H1].
+ destruct H1 as [s1 [s2 H1]].
rewrite H1.
apply not_eq_sym.
apply nil_cons.
+ destruct H1 as [t H1].
rewrite H1.
apply not_eq_sym.
apply nil_cons.
- specialize (H1 n s H3).
destruct H1 as [H1 | H1].
+ destruct H1 as [H1 _].
contradiction.
+ destruct H1 as [H1 | H1].
* destruct H1 as [s1 [s2 H1]].
rewrite H1.
apply not_eq_sym.
apply nil_cons.
* destruct H1 as [t H1].
rewrite H1.
apply not_eq_sym.
apply nil_cons.
Qed.
Lemma cnf_derives_not_empty:
forall g: cfg _ _,
forall s1 s2: sf,
is_cnf g \/ is_cnf_with_empty_rule g ->
start_symbol_not_in_rhs g ->
~ In (inl (start_symbol g)) s1 ->
s1 <> [] ->
derives g s1 s2 ->
s2 <> [].
Proof.
intros g s1 s2 H1 H2 H3 H4 H5.
apply derives_equiv_derives6 in H5.
destruct H5 as [n0 H5].
revert s1 s2 H3 H4 H5.
induction n0.
- intros n s H3 H4 H5.
inversion H5.
subst.
exact H4.
- intros s1 s2 H3 H4 H5.
inversion H5.
subst.
clear H4 H5.
specialize (IHn0 (s0 ++ right ++ s3) s2).
assert (H7: ~ In (inl (start_symbol g)) (s0 ++ right ++ s3)).
{
intros H4.
apply H3.
apply in_app_or in H4.
destruct H4 as [H4 | H4].
- apply in_or_app.
left.
exact H4.
- apply in_app_or in H4.
destruct H4 as [H4 | H4].
+ specialize (H2 left right H0).
contradiction.
+ apply in_or_app.
right.
apply in_or_app.
right.
exact H4.
}
assert (H8: right <> []).
{
destruct H1 as [H1 | H1].
- specialize (H1 left right H0).
destruct H1 as [H1 | H1].
+ destruct H1 as [s1' [s2' H1]].
rewrite H1.
apply not_eq_sym.
apply nil_cons.
+ destruct H1 as [t H1].
rewrite H1.
apply not_eq_sym.
apply nil_cons.
- specialize (H1 left right H0).
destruct H1 as [H1 | H1].
+ destruct H1 as [H1a H1b].
subst.
intros _.
apply H3.
apply in_or_app.
right.
simpl.
left.
reflexivity.
+ destruct H1 as [H1 | H1].
* destruct H1 as [s1' [s2' H1]].
rewrite H1.
apply not_eq_sym.
apply nil_cons.
* destruct H1 as [t H1].
rewrite H1.
apply not_eq_sym.
apply nil_cons.
}
assert (H9: s0 ++ right ++ s3 <> []).
{
apply app_not_nil_inv.
right.
apply app_not_nil_inv.
left.
exact H8.
}
specialize (IHn0 H7 H9 H6).
exact IHn0.
Qed.
Lemma is_cnf_right_formats_v2:
forall g: cfg non_terminal' terminal,
forall left: non_terminal',
forall right: sf',
is_cnf g \/ is_cnf_with_empty_rule g ->
rules g left right ->
right <> [] ->
(exists t: terminal, right = [inr t]) \/
(exists s1 s2: sf,
right = [inl (Lift_r s1); inl (Lift_r s2)]).
Proof.
intros g left right H1 H2 H3.
destruct H1 as [H1 | H1].
- specialize (H1 left right H2).
destruct H1 as [H1 | H1].
+ destruct H1 as [s1 [s2 H1]].
right.
destruct s1, s2.
exists l.
exists l0.
exact H1.
+ destruct H1 as [t H1].
left.
exists t.
exact H1.
- specialize (H1 left right H2).
destruct H1 as [H1 | H1].
+ destruct H1 as [H1 H4].
contradiction.
+ destruct H1 as [H1 | H1].
* destruct H1 as [s1 [s2 H1]].
right.
destruct s1, s2.
exists l, l0.
exact H1.
* destruct H1 as [t H1].
left.
exists t.
exact H1.
Qed.
Lemma g_cnf_finite:
forall g: cfg non_terminal terminal,
exists n: nat,
exists ntl: ntlist',
exists tl: tlist,
In (Lift_r [inl (start_symbol g)]) ntl /\
forall left: non_terminal',
forall right: sf',
g_cnf_rules g left right ->
(length right <= n) /\
(In left ntl) /\
(forall s: non_terminal', In (inl s) right -> In s ntl) /\
(forall s: terminal, In (inr s) right -> In s tl).
Proof.
intros g.
destruct (rules_finite g) as [n [ntl [tl H1]]].
exists 2.
exists (all_nts_up_to (S n) (map non_terminal_lift ntl ++ map term_lift tl)).
exists tl.
split.
- (* start_symbol is in the list *)
destruct H1 as [H1 _].
apply all_nts_up_to_correct.
+ intros e H2.
simpl in H2.
destruct H2 as [H2 | H2].
* subst.
apply in_or_app.
left.
apply in_map.
exact H1.
* contradiction.
+ simpl.
omega.
- split.
+ (* length rhs <= 2 *)
inversion H.
* simpl.
omega.
* simpl.
omega.
* simpl.
omega.
* simpl.
omega.
+ split.
* (* non-terminals in the lhs are in the list *)
destruct left.
{
apply all_nts_up_to_correct.
- intros e H2.
apply rules_g_cnf_to_rules_g_left in H.
destruct H as [H | [H | H]].
+ destruct H as [t [H [left [s1 [s2 Hg]]]]].
subst.
simpl in H2.
destruct H2 as [H2 | H2].
* subst.
apply in_or_app; right.
apply in_map.
destruct H1 as [_ H1].
specialize (H1 left (s1 ++ [inr t] ++ s2) Hg).
apply H1.
apply in_or_app.
right.
simpl.
left.
reflexivity.
* contradiction.
+ destruct H as [n0 [H [r Hright]]].
subst.
simpl in H2.
destruct H2 as [H2 | H2].
* subst.
unfold rules_finite_def in H1.
apply in_or_app; left.
apply in_map.
destruct H1 as [_ H1].
specialize (H1 n0 r Hright).
destruct H1 as [_ [H1 _]].
exact H1.
* contradiction.
+ destruct H as [left [right0 [s3 [s4 [H3 H4]]]]].
apply in_split in H2.
destruct H2 as [l1 [l2 H2]].
subst.
rewrite <- app_assoc in H3.
unfold rules_finite_def in H1.
destruct H1 as [_ H1].
specialize (H1 left (s3 ++ l1 ++ (e :: l2) ++ s4) H3).
destruct H1 as [_ [_ [H1 H2]]].
destruct e.
* apply in_or_app.
left.
apply in_map.
apply H1.
apply in_or_app.
right.
apply in_or_app.
right.
simpl.
left.
reflexivity.
* apply in_or_app.
right.
apply in_map.
apply H2.
apply in_or_app.
right.
apply in_or_app.
right.
simpl.
left.
reflexivity.
- apply rules_g_cnf_to_rules_g_left in H.
destruct H as [H | [H | H]].
+ destruct H as [t [H _]].
subst.
simpl.
omega.
+ destruct H as [n0 [H _]].
subst.
simpl.
omega.
+ destruct H as [left [right0 [s3 [s4 [H H2]]]]].
subst.
unfold rules_finite_def in H1.
destruct H1 as [_ H1].
specialize (H1 left (s3 ++ l ++ s4) H).
destruct H1 as [H1 _].
repeat rewrite app_length in H1.
omega.
}
* {
split.
- (* non-terminals in the rhs are in the list *)
destruct H1 as [_ H1].
inversion H.
+ subst.
intros s H2.
simpl in H2.
destruct H2 as [H2 | H2].
* inversion H2.
* contradiction.
+ subst.
intros s H2.
simpl in H2.
destruct H2 as [H2 | H2].
* inversion H2.
* contradiction.
+ subst.
intros s H2.
simpl in H2.
destruct H2 as [H2 | [H2 | H2]].
* inversion H2.
{
apply all_nts_up_to_correct.
- intros e H5.
destruct e, s1.
+ simpl in H5.
destruct H5 as [H5 | H5].
* inversion H5.
subst.
apply in_or_app.
left.
apply in_map.
specialize (H1 left0 (inl n0 :: s2 :: beta) H0).
destruct H1 as [_ [_ [H1 _]]].
apply H1.
simpl.
left.
reflexivity.
* contradiction.
+ simpl in H5.
destruct H5 as [H5 | H5].
* inversion H5.
* contradiction.
+ simpl in H5.
destruct H5 as [H5 | H5].
* inversion H5.
* contradiction.
+ simpl in H5.
destruct H5 as [H5 | H5].
* inversion H5.
subst.
apply in_or_app.
right.
apply in_map.
specialize (H1 left0 (inr t :: s2 :: beta) H0).
destruct H1 as [_ [_ [_ H1]]].
apply H1.
simpl.
left.
reflexivity.
* contradiction.
- simpl.
omega.
}
* inversion H2.
{
apply all_nts_up_to_correct.
- intros e H3.
simpl in H3.
destruct H3 as [H3 | H3].
+ subst.
destruct e.
* apply in_or_app.
left.
apply in_map.
specialize (H1 left0 (s1 :: inl n0 :: beta) H0).
destruct H1 as [_ [_ [H1 _]]].
apply H1.
simpl.
right.
left.
reflexivity.
* apply in_or_app.
right.
apply in_map.
specialize (H1 left0 (s1 :: inr t :: beta) H0).
destruct H1 as [_ [_ [_ H1]]].
apply H1.
simpl.
right.
left.
reflexivity.
+ apply in_split in H3.
destruct H3 as [l1 [l2 H3]].
subst.
apply in_or_app.
destruct e.
* left.
apply in_map.
specialize (H1 left0 (s1 :: s2 :: l1 ++ inl n0 :: l2) H0).
destruct H1 as [_ [_ [H1 _]]].
apply H1.
simpl.
right.
right.
apply in_or_app.
right.
simpl.
left.
reflexivity.
* right.
apply in_map.
specialize (H1 left0 (s1 :: s2 :: l1 ++ inr t :: l2) H0).
destruct H1 as [_ [_ [_ H1]]].
apply H1.
simpl.
right.
right.
apply in_or_app.
right.
simpl.
left.
reflexivity.
- specialize (H1 left0 (s1 :: s2 :: beta) H0).
destruct H1 as [H1 _].
simpl in H1.
simpl.
omega.
}
* contradiction.
+ apply rules_g_cnf_to_rules_g_right in H0.
destruct H0 as [left1 [right1 H0]].
intros s H4.
simpl in H4.
destruct H4 as [H4 | [H4 | H4]].
* inversion H4.
{
apply all_nts_up_to_correct.
- intros e H7.
destruct e, s2.
+ apply in_or_app.
left.
apply in_map.
simpl in H7.
destruct H7 as [H7 | H7].
* inversion H7.
subst.
specialize (H1 left1 (right1 ++ inl n0 :: s3 :: beta) H0).
destruct H1 as [_ [_ [H1 _]]].
apply H1.
apply in_or_app.
right.
simpl.
left.
reflexivity.
* contradiction.
+ simpl in H7.
destruct H7 as [H7 | H7].
* inversion H7.
* contradiction.
+ simpl in H7.
destruct H7 as [H7 | H7].
* inversion H7.
* contradiction.
+ simpl in H7.
destruct H7 as [H7 | H7].
* inversion H7.
subst.
apply in_or_app.
right.
specialize (H1 left1 (right1 ++ inr t :: s3 :: beta) H0).
destruct H1 as [_ [_ [_ H1]]].
apply in_map.
apply H1.
apply in_or_app.
right.
simpl.
left.
reflexivity.
* contradiction.
- simpl.
omega.
}
* inversion H4.
{
apply all_nts_up_to_correct.
- intros e H7.
destruct e.
+ apply in_or_app.
left.
apply in_map.
simpl in H7.
destruct H7 as [H7 | H7].
* {
destruct s3.
- inversion H7.
subst.
specialize (H1 left1 (right1 ++ s2 :: inl n0 :: beta) H0).
destruct H1 as [_ [_ [H1 _]]].
apply H1.
apply in_or_app.
right.
simpl.
right.
left.
reflexivity.
- inversion H7.
}
* apply in_split in H7.
destruct H7 as [l1 [l2 H7]].
subst.
specialize (H1 left1 (right1 ++ s2 :: s3 :: l1 ++ inl n0 :: l2) H0).
destruct H1 as [_ [_ [H1 _]]].
apply H1.
apply in_or_app.
right.
simpl.
right.
right.
apply in_or_app.
right.
simpl.
left.
reflexivity.
+ apply in_or_app.
right.
apply in_map.
simpl in H7.
destruct H7 as [H7 | H7].
* {
destruct s3.
- inversion H7.
- inversion H7.
subst.
specialize (H1 left1 (right1 ++ s2 :: inr t :: beta) H0).
destruct H1 as [_ [_ [_ H1]]].
apply H1.
apply in_or_app.
right.