-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfg.v
4393 lines (4227 loc) · 103 KB
/
cfg.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
--------------------------------------------------------------------- *)
(* --------------------------------------------------------------------- *)
(* CONTEXT FREE GRAMMARS *)
(* --------------------------------------------------------------------- *)
Require Import List.
Require Import Ring.
Require Import Omega.
Require Import misc_arith.
Require Import misc_list.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Import ListNotations.
Open Scope list_scope.
(* --------------------------------------------------------------------- *)
(* CONTEXT-FREE GRAMMARS - DEFINITIONS *)
(* --------------------------------------------------------------------- *)
Section ContextFreeGrammars_Definitions.
Variables non_terminal terminal: Type.
Notation sf := (list (non_terminal + terminal)).
Notation sentence := (list terminal).
Notation nlist:= (list non_terminal).
Notation tlist:= (list terminal).
Notation symbol:= (non_terminal + terminal)%type.
Definition rules_finite_def (ss: non_terminal)
(rules: non_terminal -> sf -> Prop)
(n: nat)
(ntl: list non_terminal)
(tl: list terminal) :=
In ss ntl /\
(forall left: non_terminal,
forall right: list (non_terminal + terminal),
rules 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)).
Record cfg: Type:= {
start_symbol: non_terminal;
rules: non_terminal -> sf -> Prop;
rules_finite: exists n: nat,
exists ntl: nlist,
exists tl: tlist,
rules_finite_def start_symbol rules n ntl tl
}.
Inductive derives (g: cfg): sf -> sf -> Prop :=
| derives_refl: forall s: sf,
derives g s s
| derives_step: forall s1 s2 s3: sf,
forall left: non_terminal,
forall right: sf,
derives g s1 (s2 ++ inl left :: s3) ->
rules g left right ->
derives g s1 (s2 ++ right ++ s3).
Inductive derives2 (g: cfg): sf -> sf -> Prop :=
| derives2_refl: forall s: sf,
derives2 g s s
| derives2_step: forall s1 s2 s3: sf,
forall left: non_terminal,
forall right: sf,
derives2 g (s1 ++ right ++ s2) s3 ->
rules g left right ->
derives2 g (s1 ++ inl left :: s2) s3.
Inductive derives3 (g: cfg): non_terminal -> sentence -> Prop :=
| derives3_rule: forall (n: non_terminal) (lt: sentence),
rules g n (map inr lt) -> derives3 g n lt
| derives3_step: forall (n: non_terminal) (ltnt: sf) (lt: list terminal),
rules g n ltnt -> derives3_aux g ltnt lt -> derives3 g n lt
with derives3_aux (g: cfg): sf -> sentence -> Prop :=
| derives3_aux_empty: derives3_aux g [] []
| derives3_aux_t: forall (t: terminal) (ltnt: sf) (lt: sentence),
derives3_aux g ltnt lt -> derives3_aux g (inr t :: ltnt) (t :: lt)
| derives3_aux_nt: forall (n: non_terminal) (lt lt': sentence) (ltnt: sf),
derives3_aux g ltnt lt -> derives3 g n lt' -> derives3_aux g (inl n :: ltnt) (lt' ++ lt).
Scheme derives3_ind_2:= Minimality for derives3 Sort Prop
with derives3_aux_ind_2:= Minimality for derives3_aux Sort Prop.
Combined Scheme derives3_comb_ind from derives3_ind_2, derives3_aux_ind_2.
Set Elimination Schemes.
Inductive derives4 (g: cfg): sf -> sf -> Prop :=
| derives4_refl: forall s: sf,
derives4 g s s
| derives4_rule: forall left: non_terminal,
forall s1 s2 right: sf,
rules g left right ->
derives4 g (s1 ++ [inl left] ++ s2) (s1 ++ right ++ s2)
| derives4_trans: forall s1 s2 s3: sf,
derives4 g s1 s2 ->
derives4 g s2 s3 ->
derives4 g s1 s3.
Inductive derives5 (g: cfg): nat -> sf -> sf -> Prop:=
| derives5_0: forall s: sf,
derives5 g 0 s s
| derives5_1: forall left: non_terminal,
forall s1 s2 right: sf,
rules g left right ->
derives5 g 1 (s1 ++ [inl left] ++ s2) (s1 ++ right ++ s2)
| derives5_sum: forall i j: nat,
forall s1 s2 s3: sf,
derives5 g i s1 s2 ->
derives5 g j s2 s3 ->
derives5 g (i+j) s1 s3.
Inductive derives6 (g: cfg): nat -> sf -> sf -> Prop:=
| derives6_0: forall s: sf,
derives6 g 0 s s
| derives6_sum: forall left: non_terminal,
forall right: sf,
forall i: nat,
forall s1 s2 s3: sf,
rules g left right ->
derives6 g i (s1 ++ right ++ s2) s3 ->
derives6 g (S i) (s1 ++ [inl left] ++ s2) s3.
Inductive derives7 (g: cfg): nat -> sf -> sf -> Prop:=
| derives7_0: forall s: sf,
derives7 g 0 s s
| derives7_sum: forall left: non_terminal,
forall right: sf,
forall i: nat,
forall s1 s2 s3: sf,
derives7 g i s1 (s2 ++ [inl left] ++ s3) ->
rules g left right ->
derives7 g (S i) s1 (s2 ++ right ++ s3).
Definition derives_direct (g: cfg) (s1 s2: sf): Prop:=
exists s' s'': sf,
exists left: non_terminal,
exists right: sf,
s1 = s' ++ [inl left] ++ s'' /\
s2 = s' ++ right ++ s'' /\
rules g left right.
Definition generates (g: cfg) (s: sf): Prop:=
derives g [inl (start_symbol g)] s.
Definition terminal_lift (t: terminal): non_terminal + terminal:=
inr t.
Definition produces (g: cfg) (s: sentence): Prop:=
generates g (map terminal_lift s).
Definition appears (g: cfg) (s: non_terminal + terminal): Prop:=
match s with
| inl n => exists left: non_terminal,
exists right: sf,
rules g left right /\ ((n=left) \/ (In (inl n) right))
| inr t => exists left: non_terminal,
exists right: sf,
rules g left right /\ In (inr t) right
end.
Inductive sflist (g: cfg): list sf -> Prop:=
| sflist_empty: sflist g []
| sflist_start: forall s: sf,
sflist g [s]
| sflist_step: forall l: list sf,
forall s2 s3: sf,
forall left: non_terminal,
forall right: sf,
sflist g l -> last l [] = (s2 ++ inl left :: s3) ->
rules g left right ->
sflist g (l++[s2 ++ right ++ s3]).
Inductive sflist2 (g: cfg): list sf -> Prop:=
| sflist2_empty: sflist2 g []
| sflist2_start: forall s: sf,
sflist2 g [s]
| sflist2_step: forall l: list sf,
forall s1 s2: sf,
l <> [] -> sflist2 g l -> last l [] = s1 ->
derives g s1 s2 ->
sflist2 g (l ++ [s2]).
End ContextFreeGrammars_Definitions.
(* --------------------------------------------------------------------- *)
(* CONTEXT-FREE GRAMMARS - DEFINITIONS 2 *)
(* --------------------------------------------------------------------- *)
Section ContextFreeGrammars_Definitions_2.
Variables non_terminal non_terminal' terminal: Type.
Notation sentence := (list terminal).
Notation sf:= (list (non_terminal + terminal)).
Definition g_equiv (g1: cfg non_terminal terminal) (g2: cfg non_terminal' terminal): Prop:=
forall s: sentence,
produces g1 s <-> produces g2 s.
Definition g_equiv_without_empty (g1: cfg non_terminal terminal) (g2: cfg non_terminal' terminal): Prop:=
forall s: sentence,
s <> [] ->
(produces g1 s <-> produces g2 s).
Definition start_symbol_not_in_rhs (g: cfg _ _):=
forall left: non_terminal,
forall right: sf,
rules g left right -> ~ In (inl (start_symbol g)) right.
Definition empty (g: cfg _ _) (s: non_terminal + terminal): Prop:=
derives g [s] [].
Definition not_derives_empty: Prop:=
forall g: cfg non_terminal terminal,
forall n: non_terminal,
~ derives g [inl n] [].
Definition has_no_empty_rules (g: cfg non_terminal terminal): Prop:=
forall left: _,
forall right: _,
rules g left right -> right <> [].
Definition has_one_empty_rule (g: cfg _ _): Prop:=
forall left: non_terminal,
forall right: sf,
rules g left right ->
((left = start_symbol g) /\ (right = []) \/ right <> []).
Definition has_no_nullable_symbols (g: cfg _ _): Prop:=
forall s: non_terminal + terminal, ~ empty g s.
Definition generates_empty (g: cfg _ _): Prop:=
empty g (inl (start_symbol g)).
Definition produces_empty (g: cfg non_terminal terminal): Prop:=
produces g [].
Definition produces_non_empty (g: cfg non_terminal terminal): Prop:=
exists s: sentence, produces g s /\ s <> [].
End ContextFreeGrammars_Definitions_2.
(* --------------------------------------------------------------------- *)
(* CONTEXTT-FREE GRAMMARS - LEMMAS AND THEOREMS *)
(* --------------------------------------------------------------------- *)
Section ContextFreeGrammars_Lemmas.
Variables non_terminal non_terminal1 non_terminal2 terminal: Type.
Notation sf := (list (non_terminal + terminal)).
Notation sentence := (list terminal).
Notation term_lift:= ((terminal_lift non_terminal) terminal).
Notation symbol:= (non_terminal + terminal)%type.
Theorem derives_rule:
forall g: cfg _ _,
forall left: non_terminal,
forall right s1 s2: sf,
rules g left right ->
derives g (s1 ++ [inl left] ++ s2) (s1 ++ right ++ s2).
Proof.
intros g left right s1 s2 H.
apply derives_step with (left:=left).
- apply derives_refl.
- exact H.
Qed.
Theorem derives_start:
forall g: cfg _ _,
forall left: non_terminal,
forall right: sf,
rules g left right -> derives g [inl left] right.
Proof.
intros g left right H.
apply derives_rule with (s1:=[]) (s2:=[]) in H.
simpl in H.
rewrite app_nil_r in H.
exact H.
Qed.
Theorem derives_trans (g: cfg _ _) (s1 s2 s3: sf):
derives g s1 s2 ->
derives g s2 s3 ->
derives g s1 s3.
Proof.
intros H1 H2.
induction H2.
- exact H1.
- apply derives_step with (left:=left).
+ apply IHderives.
exact H1.
+ exact H.
Qed.
Theorem derives2_trans (g: cfg _ _) (s1 s2 s3: sf):
derives2 g s1 s2 ->
derives2 g s2 s3 ->
derives2 g s1 s3.
Proof.
intros H1 H2.
induction H1.
- exact H2.
- apply derives2_step with (right:=right).
+ apply IHderives2.
exact H2.
+ exact H.
Qed.
Theorem derives_equiv_derives2:
forall g: cfg _ _,
forall s1 s2: sf,
derives g s1 s2 <-> derives2 g s1 s2.
Proof.
intros g s1 s2.
split.
- intro H.
induction H.
+ apply derives2_refl.
+ inversion IHderives.
* {
apply derives2_step with (right:=right).
- apply derives2_refl.
- exact H0.
}
* {
apply derives2_step with (right:=right0).
- apply derives2_trans with (s2:=(s2 ++ inl left :: s3)).
+ exact H1.
+ apply derives2_step with (right:=right).
* apply derives2_refl.
* exact H0.
- exact H2.
}
- intro H.
induction H.
+ apply derives_refl.
+ inversion IHderives2.
* apply derives_rule.
exact H0.
* {
apply derives_trans with (s2:=s1 ++ right ++ s2).
- apply derives_rule.
exact H0.
- apply derives_step with (right:=right0) in H1.
+ exact H1.
+ exact H2.
}
Qed.
Theorem derives_context_free_add_left (g: cfg _ _) (s1 s2 s: sf):
derives g s1 s2 -> derives g (s++s1) (s++s2).
Proof.
intros H.
induction H as [| x y z left right H1 H2 H3].
apply derives_refl.
remember (s++x) as w1.
rewrite app_assoc.
rewrite app_assoc in H2.
remember (s++y) as w2.
apply derives_step with (left:=left).
exact H2.
exact H3.
Qed.
Theorem derives_context_free_add_right (g: cfg _ _) (s1 s2 s: sf):
derives g s1 s2 -> derives g (s1++s) (s2++s).
Proof.
intros H.
induction H as [| x y z left right H1 H2 H3].
apply derives_refl.
remember (x++s) as w1.
rewrite <- app_assoc.
rewrite <- app_assoc.
rewrite <- app_assoc in H2.
rewrite <- app_comm_cons in H2.
remember (z++s) as w2.
apply derives_step with (left:=left).
exact H2.
exact H3.
Qed.
Theorem derives_context_free_add (g: cfg _ _) (s1 s2 s s': sf):
derives g s1 s2 -> derives g (s++s1++s') (s++s2++s').
Proof.
intros H.
apply derives_context_free_add_left.
apply derives_context_free_add_right.
exact H.
Qed.
Theorem derives6_context_free_add_left (g: cfg _ _) (n: nat) (s1 s2 s: sf):
derives6 g n s1 s2 -> derives6 g n (s++s1) (s++s2).
Proof.
intros H.
induction H.
- apply derives6_0.
- apply derives6_sum with (i:= i) (s1:= s ++ s1) (s2:= s2) (s3:= s ++ s3) in H.
+ rewrite <- app_assoc in H.
exact H.
+ rewrite <- app_assoc.
exact IHderives6.
Qed.
Theorem derives6_context_free_add_right (g: cfg _ _) (n: nat) (s1 s2 s: sf):
derives6 g n s1 s2 -> derives6 g n (s1++s) (s2++s).
Proof.
intros H.
induction H.
- apply derives6_0.
- repeat rewrite <- app_assoc in IHderives6.
apply derives6_sum with (i:= i) (s1:= s1) (s2:= s2 ++ s) (s3:= s3 ++ s) in H.
+ repeat rewrite <- app_assoc.
exact H.
+ exact IHderives6.
Qed.
Theorem derives6_context_free_add (g: cfg _ _) (n: nat) (s1 s2 s s': sf):
derives6 g n s1 s2 -> derives6 g n (s++s1++s') (s++s2++s').
Proof.
intros H.
apply derives6_context_free_add_left.
apply derives6_context_free_add_right.
exact H.
Qed.
Lemma derives6_cat_sum:
forall g: cfg _ _,
forall n1 n2: nat,
forall s1 s2 s3 s4: sf,
derives6 g n1 s1 s2 ->
derives6 g n2 s3 s4 ->
derives6 g (n1 + n2) (s1 ++ s3) (s2 ++ s4).
Proof.
intros g n1.
induction n1.
- intros n2 s1 s2 s3 s4 H1 H2.
inversion H1.
simpl.
inversion H1.
apply derives6_context_free_add_left.
exact H2.
- intros n2 s1 s2 s3 s4 H1 H2.
inversion H1.
specialize (IHn1 n2 (s0 ++ right ++ s5) s2 s3 s4 H3 H2).
apply derives6_sum with (i:= n1 + n2) (s1:= s0) (s2:= s5 ++ s3) (s3:= s2 ++ s4) in H0.
+ repeat rewrite <- app_assoc.
exact H0.
+ repeat rewrite <- app_assoc in IHn1.
exact IHn1.
Qed.
Theorem derives_combine (g: cfg _ _) (s1 s2 s3 s4: sf):
derives g s1 s2 /\ derives g s3 s4 -> derives g (s1++s3) (s2++s4).
Proof.
intros [H1 H2].
induction H1,H2.
apply derives_refl.
apply derives_context_free_add_left.
apply derives_step with (left:= left).
exact H2.
exact H.
apply derives_context_free_add_right.
apply derives_step with (left:=left).
exact H1.
exact H.
rewrite <- app_assoc.
rewrite <- app_assoc.
rewrite <- app_assoc in IHderives.
simpl in IHderives.
remember (s0 ++ s4 ++ right0 ++ s5) as w.
apply derives_step with (left:=left).
exact IHderives.
exact H.
Qed.
Lemma derives_multiple (g: cfg _ _) (s1 s2 s3: sf) (left: non_terminal) (right1 right2: sf):
derives g s1 (s2 ++ inl left :: s3) ->
rules g left right1 ->
rules g left right2 ->
derives g s1 (s2 ++ right1 ++ s3) /\ derives g s1 (s2 ++ right2 ++ s3).
Proof.
intros H1 H2 H3.
split.
- apply derives_step with (left:= left).
exact H1.
exact H2.
- apply derives_step with (left:= left).
exact H1.
exact H3.
Qed.
Lemma derives_subs:
forall g: cfg _ _,
forall s1 s2 s3 s3' s4: sf,
derives g s1 (s2++s3++s4) ->
derives g s3 s3' ->
derives g s1 (s2++s3'++s4).
Proof.
intros g s1 s2 s3 s3' s4 H1 H2.
induction H2.
- exact H1.
- specialize (IHderives H1).
rewrite <- app_assoc in IHderives.
simpl in IHderives.
repeat rewrite <- app_assoc.
remember (s5++s4) as w2.
rewrite app_assoc.
apply derives_step with (left:=left).
subst.
rewrite <- app_assoc.
exact IHderives.
exact H.
Qed.
Lemma derives_split:
forall g: cfg _ _,
forall s1 s2 s3: sf,
derives g (s1 ++ s2) s3 ->
exists s1' s2': sf, s3 = s1' ++ s2' /\ derives g s1 s1' /\ derives g s2 s2'.
Proof.
intros g s1 s2 s3 H.
remember (s1++s2) as w.
induction H.
- exists s1, s2.
split.
+ exact Heqw.
+ split.
* apply derives_refl.
* apply derives_refl.
- specialize (IHderives Heqw).
destruct IHderives as [s1' [s2' [H10 [H11 H12]]]].
apply equal_app in H10.
destruct H10 as [H10 | H10].
+ destruct H10 as [l [H20 H21]].
destruct l.
* simpl in H21.
{
destruct s2'.
- inversion H21.
- inversion H21.
subst.
exists s3, (right ++ s2').
split.
+ reflexivity.
+ split.
* rewrite app_nil_r in H11.
exact H11.
* rewrite <- app_nil_l in H12.
{
apply derives_step with (right:=right) in H12.
- exact H12.
- exact H0.
}
}
* inversion H21.
subst.
exists (s3 ++ right ++ l), s2'.
{
split.
- repeat rewrite <- app_assoc.
reflexivity.
- split.
+ apply derives_step with (right:=right) in H11.
* exact H11.
* exact H0.
+ exact H12.
}
+ destruct H10 as [l [H20 H21]].
destruct l.
* simpl in H21.
rewrite app_nil_r in H20.
subst.
exists s1', (right ++ s4).
{
split.
- reflexivity.
- split.
exact H11.
rewrite <- app_nil_l in H12.
apply derives_step with (right:=right) in H12.
+ exact H12.
+ exact H0.
}
* {
destruct s2'.
- inversion H21.
- inversion H21.
subst.
exists s1', (s :: l ++ right ++ s4).
split.
+ repeat rewrite <- app_assoc.
reflexivity.
+ split.
* exact H11.
* {
apply derives_step with (s2:=s :: l) (right:=right) in H12.
- exact H12.
- exact H0.
}
}
Qed.
Lemma derives_app_empty:
forall g: cfg _ _,
forall s1 s2: sf,
derives g (s1 ++ s2) (map term_lift []) ->
derives g s1 (map term_lift []) /\ derives g s2 (map term_lift []).
Proof.
intros g s1 s2 H.
apply derives_split in H.
destruct H as [s1' [s2' [H1 [H2 H3]]]].
simpl in H1.
symmetry in H1.
apply app_eq_nil in H1.
destruct H1 as [H4 H5].
subst.
split.
- exact H2.
- exact H3.
Qed.
Lemma derives_nt_sentence:
forall g: cfg _ _,
forall l1 l2: sf,
forall n: non_terminal,
forall s: sentence,
derives g (l1 ++ inl n :: l2) (map term_lift s) ->
exists s': sentence,
derives g [inl n] (map term_lift s').
Proof.
intros g l1 l2 n s H.
apply derives_split in H.
destruct H as [s1' [s2' [H2 [_ H4]]]].
symmetry in H2.
apply map_expand in H2.
destruct H2 as [_ [s2'0 [_ [_ H5]]]].
rewrite <- H5 in H4.
replace (inl n::l2) with ([inl n]++l2) in H4.
- apply derives_split in H4.
destruct H4 as [s1'0 [s2'1 [H6 [H7 _]]]].
symmetry in H6.
apply map_expand in H6.
destruct H6 as [s1'1 [_ [_ [H8 _]]]].
rewrite <- H8 in H7.
exists s1'1.
exact H7.
- simpl.
reflexivity.
Qed.
Lemma derives_nt_sf:
forall g: cfg _ _,
forall s1 s2: sf,
derives g s1 s2 ->
forall n: non_terminal,
In (inl n) s1 ->
exists s1' s1'' s2' s2'' beta: sf,
s1 = s1' ++ [inl n] ++ s1'' /\
s2 = s2' ++ beta ++ s2'' /\
derives g [inl n] beta.
Proof.
intros g s1 s2 H1 n H2.
apply in_split in H2.
destruct H2 as [l1 [l2 H3]].
exists l1, l2.
rewrite H3 in H1.
apply derives_split in H1.
destruct H1 as [s1' [s2' [H4 [H5 H6]]]].
change (inl n :: l2) with ([inl n] ++ l2) in H6.
apply derives_split in H6.
destruct H6 as [s1'0 [s2'0 [H7 [H8 H9]]]].
exists s1', s2'0, s1'0.
split.
- exact H3.
- split.
+ rewrite H7 in H4.
exact H4.
+ exact H8.
Qed.
Lemma derives_nt_sf':
forall g: cfg _ _,
forall l1 l2 l3: sf,
forall n: non_terminal,
derives g (l1 ++ inl n :: l2) l3 ->
exists l': sf,
derives g [inl n] l'.
Proof.
intros g l1 l2 l3 n H.
apply derives_split in H.
destruct H as [s1' [s2' [H2 [_ H4]]]].
symmetry in H2.
replace (inl n :: l2) with ([inl n] ++ l2) in H4.
- apply derives_split in H4.
destruct H4 as [s1'0 [s2'0 [H2' [H3' _]]]].
exists s1'0.
exact H3'.
- simpl.
reflexivity.
Qed.
Lemma derives3_equiv_derives3_aux:
forall g: cfg _ _,
forall n: non_terminal,
forall s: sentence,
derives3 g n s <-> derives3_aux g [inl n] s.
Proof.
intros g n s.
split.
- intros H.
inversion H.
+ subst.
rewrite <- app_nil_r.
rewrite <- app_nil_r at 1.
apply derives3_aux_nt.
* apply derives3_aux_empty.
* exact H.
+ subst.
rewrite <- app_nil_r.
rewrite <- app_nil_r at 1.
apply derives3_aux_nt.
* apply derives3_aux_empty.
* exact H.
- intros H.
inversion H.
inversion H2.
subst.
rewrite app_nil_r.
exact H4.
Qed.
Theorem derives3_aux_combine (g: cfg _ _) (s1 s2: sf) (s3 s4: sentence):
derives3_aux g s1 s3 ->
derives3_aux g s2 s4 ->
derives3_aux g (s1++s2) (s3++s4).
Proof.
intros H.
induction H.
- intros H.
exact H.
- intros H1.
change (inr t :: ltnt) with ([inr t] ++ ltnt).
change (t :: lt) with ([t] ++ lt).
repeat rewrite <- app_assoc.
apply derives3_aux_t.
apply IHderives3_aux.
exact H1.
- intros H1.
change (inl n :: ltnt) with ([inl n] ++ ltnt).
repeat rewrite <- app_assoc.
apply derives3_aux_nt.
+ apply IHderives3_aux.
exact H1.
+ exact H0.
Qed.
Lemma derives3_aux_split:
forall g: cfg _ _,
forall s1 s2: sf,
forall s3: sentence,
derives3_aux g (s1 ++ s2) s3 ->
exists s3' s3'': sentence,
derives3_aux g s1 s3' /\ derives3_aux g s2 s3'' /\ s3 = s3' ++ s3''.
Proof.
intros g s1 s2.
induction s1 as [ | c s11 IH].
- simpl.
intros s3 H.
exists [], s3.
split.
+ apply derives3_aux_empty.
+ split.
* exact H.
* trivial.
- destruct c as [n | t].
+ simpl.
intros s3 H.
inversion H.
subst.
clear H.
specialize (IH _ H2).
destruct IH as (s21 & s22 & IH1 & IH2 & IH3).
subst.
exists (lt' ++ s21), s22.
split.
* {
apply derives3_aux_nt.
- exact IH1.
- exact H4.
}
* {
split.
- exact IH2.
- rewrite <- app_assoc.
reflexivity.
}
+ simpl.
intros s3 H.
inversion H.
subst.
clear H.
specialize (IH _ H3).
destruct IH as (s21 & s22 & IH1 & IH2 & IH3).
subst.
exists (t :: s21), s22.
split.
* apply derives3_aux_t.
exact IH1.
* {
split.
- exact IH2.
- trivial.
}
Qed.
Lemma derives_implies_derives3_aux:
forall g: cfg _ _,
forall s1: sf,
forall s2: sentence,
derives g s1 (map term_lift s2) -> derives3_aux g s1 s2.
Proof.
intros g s1 s2.
remember (map term_lift s2) as s2'.
rewrite derives_equiv_derives2.
intros H.
induction H as [s | s_1 s_2 s_3 left right H1 H2 H3].
- subst.
induction s2 as [| c s IH].
+ apply derives3_aux_empty.
+ replace (c :: s) with ([c] ++ s).
* rewrite map_app.
apply derives3_aux_t.
exact IH.
* simpl.
reflexivity.
- rewrite Heqs2' in H2.
specialize (H2 eq_refl).
apply derives3_aux_split in H2.
destruct H2 as [s3' [s3'' [H4 [H5 H6]]]].
subst s2.
apply derives3_aux_combine.
+ exact H4.
+ apply derives3_aux_split in H5.
destruct H5 as [s3'0 [s3''0 [H7 [H8 H9]]]].
subst s3''.
apply derives3_aux_nt.
* exact H8.
* {
apply derives3_step with (ltnt:=right).
- exact H3.
- exact H7.
}
Qed.
Lemma derives3_implies_derives_and_derives3_aux_implies_derives:
forall g: cfg _ _,
(forall n: non_terminal,
forall s: sentence,
derives3 g n s -> derives g [inl n] (map term_lift s))
/\
(forall s1: sf,
forall s2: sentence,
derives3_aux g s1 s2 -> derives g s1 (map term_lift s2)).
Proof.
intros g.
apply derives3_comb_ind.
- intros n lt H.
rewrite derives_equiv_derives2.
rewrite <- (app_nil_l [inl n]).
apply derives2_step with (right:=(map inr lt)).
+ rewrite app_nil_l.
rewrite app_nil_r.
apply derives2_refl.
+ exact H.
- intros n ltnt lt H1 H2 H3.
rewrite derives_equiv_derives2.
rewrite <- (app_nil_l [inl n]).
apply derives2_step with (right:=ltnt).
+ rewrite app_nil_l.
rewrite app_nil_r.
rewrite <- derives_equiv_derives2.
exact H3.
+ exact H1.
- simpl.
apply derives_refl.
- intros t ltnt lt H1 H2.
simpl.
replace (inr t :: ltnt) with ([inr t] ++ ltnt).
+ replace (term_lift t :: map term_lift lt) with ([term_lift t] ++ map term_lift lt).
* apply derives_context_free_add_left.
exact H2.
* simpl.
reflexivity.
+ simpl.
reflexivity.
- intros n lt lt' ltnt H1 H2 H3 H4.
change (inl n :: ltnt) with ([inl n] ++ ltnt).
rewrite map_app.
apply derives_combine.
split.
+ exact H4.
+ exact H2.
Qed.
Lemma derives3_implies_derives:
forall g: cfg _ _,
forall n: non_terminal,
forall s: sentence,
derives3 g n s -> derives g [inl n] (map term_lift s).
Proof.
intros g n s H.
apply derives3_implies_derives_and_derives3_aux_implies_derives in H.
exact H.
Qed.
Lemma derives3_aux_implies_derives:
forall g: cfg _ _,
forall s1: sf,
forall s2: sentence,
derives3_aux g s1 s2 -> derives g s1 (map term_lift s2).
Proof.
intros g n s H.
apply derives3_implies_derives_and_derives3_aux_implies_derives in H.
exact H.
Qed.
Lemma derives_implies_derives3:
forall g: cfg _ _,
forall n: non_terminal,
forall s: sentence,
derives g [inl n] (map term_lift s) -> derives3 g n s.
Proof.
intros g n s H.
rewrite derives3_equiv_derives3_aux.
apply derives_implies_derives3_aux.
exact H.
Qed.
Lemma derives_equiv_derives3:
forall g: cfg _ _,
forall n: non_terminal,
forall s: sentence,
derives g [inl n] (map term_lift s) <-> derives3 g n s.
Proof.
intros g n s.
split.
- intros H.
apply derives_implies_derives3.
exact H.
- intros H.
apply derives3_implies_derives.
exact H.
Qed.
Lemma derives_equiv_derives3_aux:
forall g: cfg _ _,
forall s1: sf,
forall s2: sentence,
derives g s1 (map term_lift s2) <-> derives3_aux g s1 s2.
Proof.
intros g s1 s2.
split.
- intros H.
apply derives_implies_derives3_aux.
exact H.
- intros H.
apply derives3_aux_implies_derives.