forked from mvmramos/pumping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emptyrules.v
3261 lines (3118 loc) · 72 KB
/
emptyrules.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
--------------------------------------------------------------------- *)
(* --------------------------------------------------------------------- *)
(* SIMPLIFICATION - EMPTY RULES *)
(* --------------------------------------------------------------------- *)
Require Import List.
Require Import Ring.
Require Import Omega.
Require Import Decidable.
Require Import misc_arith.
Require Import misc_list.
Require Import cfg.
Require Import useless.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Import ListNotations.
Open Scope list_scope.
(* --------------------------------------------------------------------- *)
(* SIMPLIFICATION - EMPTY RULES - DEFINITIONS *)
(* --------------------------------------------------------------------- *)
Section EmptyRules_1_Definitions.
Variables non_terminal terminal: Type.
Inductive non_terminal': Type:=
| Lift_nt: non_terminal -> non_terminal'
| New_ss.
Lemma nt_eqdec' :
(forall (x y: non_terminal), {x=y}+{x<>y}) ->
forall (x y: non_terminal'), {x=y}+{x<>y}.
Proof. decide equality. Qed.
Notation symbol:= (non_terminal + terminal)%type.
Notation symbol':= (non_terminal' + terminal)%type.
Notation nlist:= (list non_terminal).
Notation nlist':= (list non_terminal').
Notation tlist:= (list terminal).
Notation sentence := (list terminal).
Notation sf := (list (non_terminal + terminal)).
Notation sf' := (list (non_terminal' + terminal)).
Notation term_lift:= ((terminal_lift non_terminal) terminal).
Definition symbol_lift (s: symbol): symbol':=
match s with
| inl n => inl (Lift_nt n)
| inr t => inr t
end.
Lemma symbol_lift_inj:
injective _ _ symbol_lift.
Proof.
unfold injective.
intros e1 e2 H.
destruct e1, e2.
- simpl in H.
inversion H.
reflexivity.
- simpl in H.
inversion H.
- simpl in H.
inversion H.
- simpl in H.
inversion H.
reflexivity.
Qed.
Lemma symbol_lift_equiv_terminal_lift:
forall s: sentence,
(map symbol_lift (map term_lift s)) = (map (@terminal_lift _ _) s).
Proof.
induction s.
- simpl.
reflexivity.
- simpl.
rewrite IHs.
change (terminal_lift non_terminal' a) with (inr non_terminal' a).
reflexivity.
Qed.
Definition sf_lift (s: sf): sf':=
map symbol_lift s.
Definition sf_list_lift (l: list sf): list sf':=
map sf_lift l.
Lemma symbol_lift_exists:
forall a': symbol',
a' <> (inl New_ss) ->
exists a: symbol,
a' = symbol_lift a.
Proof.
intros a'.
destruct a'.
- destruct n.
+ exists (inl n).
simpl.
reflexivity.
+ intros H.
destruct H.
reflexivity.
- exists (inr t).
simpl.
reflexivity.
Qed.
Lemma sf_lift_exists:
forall s': sf',
~ In (inl New_ss) s' ->
exists s: sf,
s' = sf_lift s.
Proof.
intros s' H.
induction s'.
- exists [].
simpl.
reflexivity.
- assert (H1: ~ In (inl New_ss) s').
{
intros H1.
apply H.
apply in_cons.
exact H1.
}
specialize (IHs' H1).
destruct IHs' as [s H2].
assert (H3: a <> (inl New_ss) -> exists b: symbol, a = symbol_lift b).
{
intros H3.
apply symbol_lift_exists.
exact H3.
}
simpl in H.
apply not_or in H.
destruct H as [H _].
specialize (H3 H).
destruct H3 as [b H3].
exists (b :: s).
simpl.
rewrite <- H3.
apply app_eq.
exact H2.
Qed.
Inductive g_emp_rules (g: cfg _ _): non_terminal' -> sf' -> Prop :=
| Lift_direct :
forall left: non_terminal,
forall right: sf,
right <> [] -> rules g left right ->
g_emp_rules g (Lift_nt left) (map symbol_lift right)
| Lift_indirect:
forall left: non_terminal,
forall right: sf,
g_emp_rules g (Lift_nt left) (map symbol_lift right)->
forall s1 s2: sf,
forall s: non_terminal,
right = s1 ++ (inl s) :: s2 ->
empty g (inl s) ->
s1 ++ s2 <> [] ->
g_emp_rules g (Lift_nt left) (map symbol_lift (s1 ++ s2))
| Lift_start_emp:
g_emp_rules g New_ss [inl (Lift_nt (start_symbol g))].
Lemma g_emp_finite:
forall g: cfg _ _,
exists n: nat,
exists ntl: nlist',
exists tl: tlist,
In New_ss ntl /\
forall left: non_terminal',
forall right: sf',
g_emp_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 (S n), (New_ss :: map Lift_nt ntl), tl.
split.
- destruct H1 as [H1 _].
simpl.
left.
reflexivity.
- intros left right H2.
destruct H1 as [H1' H1].
induction H2.
+ specialize (H1 left right H0).
destruct H1 as [H4 [H5 H6]].
split.
* apply length_map_le.
omega.
* {
split.
- simpl.
right.
apply in_map.
exact H5.
- split.
+ intros s HH.
destruct s.
* simpl.
right.
apply in_map.
apply H6.
apply in_map_iff in HH.
destruct HH as [x [HH1 HH2]].
{
destruct x.
- simpl in HH1.
inversion HH1.
subst.
exact HH2.
- simpl in HH1.
inversion HH1.
}
* simpl.
left.
reflexivity.
+ intros s HH.
apply H6.
apply in_map_iff in HH.
destruct HH as [x [HH1 HH2]].
destruct x.
* simpl in HH1.
inversion HH1.
* simpl in HH1.
inversion HH1.
subst.
exact HH2.
}
+ subst.
destruct IHg_emp_rules as [H4 [H5 H6]].
split.
* apply length_map_le.
rewrite map_app in H4.
simpl in H4.
apply length_cons_le in H4.
{
replace (map symbol_lift s1 ++ map symbol_lift s2) with (map symbol_lift (s1 ++ s2)) in H4.
- apply length_map_le_inv in H4.
exact H4.
- apply map_app.
}
* {
split.
- exact H5.
- split.
+ intros s0 H7.
apply H6.
rewrite map_app in H7.
apply in_app_or in H7.
rewrite map_app.
apply in_or_app.
destruct H7 as [H7 | H7].
* left.
exact H7.
* right.
simpl.
right.
exact H7.
+ destruct H6 as [_ H6].
intros s0 H7.
apply H6.
rewrite map_app in H7.
apply in_app_or in H7.
rewrite map_app.
apply in_or_app.
destruct H7 as [H7 | H7].
* left.
exact H7.
* right.
simpl.
right.
exact H7.
}
+ split.
* simpl.
omega.
* {
split.
- simpl.
left.
reflexivity.
- split.
+ intros s H2.
simpl in H2.
destruct H2 as [H2 | H2].
* inversion H2.
simpl.
right.
apply in_map.
exact H1'.
* contradiction.
+ intros s H.
simpl in H.
destruct H as [H | H].
* inversion H.
* contradiction.
}
Qed.
Definition g_emp (g: cfg non_terminal terminal): cfg non_terminal' terminal := {|
start_symbol:= New_ss;
rules:= g_emp_rules g;
t_eqdec:= t_eqdec g;
nt_eqdec:= nt_eqdec' (nt_eqdec g);
rules_finite:= g_emp_finite g
|}.
End EmptyRules_1_Definitions.
(* --------------------------------------------------------------------- *)
(* SIMPLIFICATION - EMPTY RULES - LEMMAS AND THEOREMS *)
(* --------------------------------------------------------------------- *)
Section EmptyRules_1_Lemmas.
Variables non_terminal non_terminal1 non_terminal2 terminal: Type.
Notation symbol:= (non_terminal + terminal)%type.
Notation symbol':= (non_terminal' + terminal)%type.
Notation nlist:= (list non_terminal).
Notation nlist':= (list non_terminal' _).
Notation tlist:= (list terminal).
Notation sentence := (list terminal).
Notation sf := (list (non_terminal + terminal)).
Notation sf' := (list ((non_terminal' non_terminal) + terminal)).
Notation term_lift:= ((terminal_lift non_terminal) terminal).
Lemma produces_non_empty_equiv_non_empty:
forall g: cfg non_terminal terminal,
produces_non_empty g -> non_empty g.
Proof.
intros g H.
unfold non_empty.
unfold useful.
unfold produces_non_empty in H.
destruct H as [s [H1 H2]].
exists s.
exact H1.
Qed.
Lemma non_empty_equiv_or:
forall g: cfg non_terminal terminal,
non_empty g -> (produces_empty g \/ produces_non_empty g).
Proof.
intros g H.
unfold non_empty in H.
unfold useful in H.
destruct H as [s H1].
assert (H2: s = [] \/ s <> []).
{
apply nil_not_nil.
}
destruct H2 as [H2 | H2].
- subst.
left.
exact H1.
- right.
exists s.
exact (conj H1 H2).
Qed.
Lemma start_symbol_not_in_rhs_g_emp:
forall g: cfg non_terminal terminal,
start_symbol_not_in_rhs (g_emp g).
Proof.
intros g.
unfold start_symbol_not_in_rhs.
intros left right H1 H2.
inversion H1.
- subst.
simpl in H2.
apply in_split in H2.
destruct H2 as [l1 [l2 H2]].
symmetry in H2.
apply map_expand in H2.
destruct H2 as [s1' [s2' [H3 [H4 H5]]]].
destruct s2'.
+ inversion H5.
+ simpl in H5.
inversion H5.
destruct s.
* simpl in H6.
inversion H6.
* simpl in H6.
inversion H6.
- subst.
simpl in H2.
apply in_split in H2.
destruct H2 as [l1 [l2 H2]].
symmetry in H2.
apply map_expand in H2.
destruct H2 as [s1' [s2' [H5 [H6 H7]]]].
destruct s2'.
+ inversion H7.
+ simpl in H7.
inversion H7.
destruct s0.
* simpl in H2.
inversion H2.
* simpl in H2.
inversion H2.
- rewrite <- H0 in H2.
simpl in H2.
destruct H2 as [H2 | H2].
+ inversion H2.
+ contradiction.
Qed.
Lemma g_emp_not_derives_empty:
forall g: cfg non_terminal terminal,
forall n: (non_terminal' _),
~ derives (g_emp g) [inl n] [].
Proof.
intros g n H.
inversion H.
clear H.
subst.
simpl in H3.
inversion H3.
- subst.
apply app_eq_nil in H0.
destruct H0 as [_ H0].
apply app_eq_nil in H0.
destruct H0 as [H0 _].
apply map_eq_nil in H0.
contradiction.
- subst.
apply app_eq_nil in H0.
destruct H0 as [_ H0].
apply app_eq_nil in H0.
destruct H0 as [H0 _].
rewrite H0 in H3.
inversion H3.
+ subst.
apply map_eq_nil in H6.
contradiction.
+ apply map_eq_nil in H6.
contradiction.
- subst.
destruct s2.
+ inversion H0.
+ inversion H0.
Qed.
Lemma g_emp_has_no_empty_rules:
forall g: cfg non_terminal terminal,
has_no_empty_rules (g_emp g).
Proof.
intros g.
unfold has_no_empty_rules.
intros left right H.
inversion H.
- apply map_not_nil_inv.
exact H0.
- apply map_not_nil_inv.
exact H3.
- apply not_eq_sym.
apply nil_cons.
Qed.
Lemma in_left_not_empty:
forall g: cfg non_terminal terminal,
forall x: non_terminal' _,
forall right: sf',
rules (g_emp g) x right -> ~ empty (g_emp g) (inl x).
Proof.
intros g x right H1 H2.
simpl in H1.
inversion H1.
- clear H1.
subst.
apply g_emp_not_derives_empty in H2.
contradiction.
- clear H1.
subst.
apply g_emp_not_derives_empty in H2.
contradiction.
- subst.
apply g_emp_not_derives_empty in H2.
contradiction.
Qed.
Lemma in_right_not_empty:
forall g: cfg non_terminal terminal,
forall x n: non_terminal' _,
forall right: sf',
rules (g_emp g) x right -> In (inl n) right -> ~ empty (g_emp g) (inl n).
Proof.
intros g x n right H1 H2 H3.
simpl in H1.
inversion H1.
- clear H1.
subst.
apply g_emp_not_derives_empty in H3.
contradiction.
- clear H1.
subst.
apply g_emp_not_derives_empty in H3.
contradiction.
- subst.
apply g_emp_not_derives_empty in H3.
contradiction.
Qed.
Lemma g_emp_has_no_nullable_symbols:
forall g: cfg non_terminal terminal,
has_no_nullable_symbols (g_emp g).
Proof.
intros g.
unfold has_no_nullable_symbols.
intros s H1.
destruct s as [nt | t].
- apply g_emp_not_derives_empty in H1.
contradiction.
- unfold empty in H1.
inversion H1.
apply app_eq_nil in H.
destruct H as [_ H].
apply app_eq_nil in H.
destruct H as [H _].
subst.
apply g_emp_has_no_empty_rules in H3.
destruct H3.
reflexivity.
Qed.
Lemma rules_g_emp_g:
forall g: cfg non_terminal terminal,
forall left: non_terminal,
forall right: sf,
rules (g_emp g) (Lift_nt left) (map (@symbol_lift _ _) right) ->
rules g left right \/ derives g [inl left] right.
Proof.
intros g left right H.
simpl in H.
remember (Lift_nt left) as w1.
remember (map (symbol_lift (terminal:=terminal)) right) as w2.
(*
move left after H.
move right after H.
*)
generalize left right Heqw1 Heqw2.
clear left right Heqw1 Heqw2.
induction H.
- intros left0 right0 Heqw1 Heqw2.
inversion Heqw1.
apply map_eq in Heqw2.
+ subst.
left.
exact H0.
+ apply symbol_lift_inj.
- intros left0 right0 Heqw1 Heqw2.
inversion Heqw1.
apply map_eq in Heqw2.
+ subst.
specialize (IHg_emp_rules left0 (s1 ++ inl s :: s2)).
specialize (IHg_emp_rules (eq_refl (Lift_nt left0))).
specialize (IHg_emp_rules (eq_refl (map (symbol_lift (terminal:=terminal)) (s1 ++ inl s :: s2)))).
destruct IHg_emp_rules as [HH | HH].
* right.
{
replace (s1 ++ s2) with (s1 ++ [] ++ s2).
- apply derives_subs with (s3:=[inl s]).
+ apply derives_start.
exact HH.
+ exact H1.
- simpl.
reflexivity.
}
* right.
{
replace (s1 ++ s2) with (s1 ++ [] ++ s2).
- apply derives_subs with (s3:=[inl s]).
+ exact HH.
+ exact H1.
- simpl.
reflexivity.
}
+ apply symbol_lift_inj.
- intros left right H1 H2.
inversion H1.
Qed.
Lemma derives_g_emp_g:
forall g: cfg _ _,
forall n: non_terminal,
forall s: sf,
derives (g_emp g) [inl (Lift_nt n)] (map (@symbol_lift _ _) s) -> derives g [inl n] s.
Proof.
intros g n s H.
remember [inl (Lift_nt n)] as w1.
remember (map (symbol_lift (terminal:=terminal)) s) as w2.
generalize n s Heqw1 Heqw2.
clear s n Heqw1 Heqw2.
induction H.
- intros n0 s1 Heqw1 Heqw2.
subst.
destruct s1.
+ simpl in Heqw2.
inversion Heqw2.
+ destruct s.
* simpl in Heqw2.
inversion Heqw2.
symmetry in H1.
apply map_eq_nil in H1.
subst.
apply derives_refl.
* simpl in Heqw2.
inversion Heqw2.
- intros n s Heqw1 Heqw2.
destruct left.
+ assert (H1: exists r: sf, right = sf_lift r).
{
apply sf_lift_exists.
intros H7.
apply in_split in H7.
destruct H7 as [l1 [l2 H7]].
subst.
apply map_expand in Heqw2.
destruct Heqw2 as [s1' [s2' [_ [_ H1]]]].
symmetry in H1.
apply map_expand in H1.
destruct H1 as [s1'0 [s2'0 [H1 [H2 H3]]]].
symmetry in H2.
apply map_expand in H2.
destruct H2 as [s1'1 [s2'1 [H4 [H5 H6]]]].
destruct s2'1.
- inversion H6.
- destruct s0.
+ inversion H6.
+ inversion H6.
}
destruct H1 as [r H1].
subst.
apply map_expand in Heqw2.
destruct Heqw2 as [s1' [s2' [H1 [H2 H3]]]].
symmetry in H3.
apply map_expand in H3.
destruct H3 as [s1'0 [s2'0 [H4 [H5 H6]]]].
rewrite H1.
rewrite H4.
apply rules_g_emp_g in H0.
destruct H0 as [H0 | H0].
* specialize (IHderives n).
unfold sf_lift in H5.
{
apply map_eq in H5.
- subst.
specialize (IHderives (s1' ++ (inl n0) :: s2'0)).
apply derives_step with (left:= n0).
+ apply IHderives.
* reflexivity.
* change (s1' ++ inl n0 :: s2'0) with (s1' ++ [inl n0] ++ s2'0).
repeat rewrite map_app.
reflexivity.
+ exact H0.
- apply symbol_lift_inj.
}
* specialize (IHderives n).
unfold sf_lift in H5.
{
apply map_eq in H5.
- subst.
specialize (IHderives (s1' ++ (inl n0) :: s2'0)).
apply derives_subs with (s3:= [inl n0]).
+ apply IHderives.
* reflexivity.
* change (s1' ++ inl n0 :: s2'0) with (s1' ++ [inl n0] ++ s2'0).
repeat rewrite map_app.
reflexivity.
+ exact H0.
- apply symbol_lift_inj.
}
+ rewrite Heqw1 in H.
apply exists_rule' in H.
destruct H as [H | H].
* destruct H as [H _].
inversion H.
* destruct H as [left [right0 [H H1]]].
apply start_symbol_not_in_rhs_g_emp in H.
simpl in H.
contradiction.
Qed.
Lemma rules_g_g_emp:
forall g: cfg _ _,
forall left: non_terminal,
forall right: sf,
right <> [] ->
rules g left right ->
rules (g_emp g) (Lift_nt left) (map (@symbol_lift _ _) right).
Proof.
intros g left right H.
simpl.
apply Lift_direct.
exact H.
Qed.
Inductive sfmatch g: sf -> list sf -> Prop :=
| sfmatch_nil:
sfmatch g [] []
| sfmatch_term:
forall t xs xxs,
sfmatch g xs xxs -> sfmatch g (inr t :: xs) ([inr t] :: xxs)
| sfmatch_nonterm:
forall nt xs xxs p,
(p = [] -> empty g (inl nt)) ->
(p <> [] -> derives (g_emp g) [inl (Lift_nt nt)] (map (@symbol_lift _ _) p)) ->
sfmatch g xs xxs -> sfmatch g (inl nt :: xs) (p :: xxs).
Fixpoint flatten (l: list sf): sf :=
match l with
| [] => []
| x :: xs => x ++ flatten xs
end.
Fixpoint elim_emp (l: sf) (ll: list sf): sf :=
match l with
| [] => []
| (x :: xs) => match ll with
| [] => l
| [] :: ll' => elim_emp xs ll'
| p :: ll' => x :: elim_emp xs ll'
end
end.
Lemma sfmatch_left_nil:
forall g: cfg _ _,
forall l: list sf,
sfmatch g [] l ->
l = [].
Proof.
intros g l H.
inversion H.
reflexivity.
Qed.
Lemma elim_emp_not_nil:
forall right: sf,
forall split: list sf,
elim_emp right split <> [] ->
right <> [].
Proof.
intros right split H.
destruct right.
- simpl in H.
destruct H.
reflexivity.
- apply not_eq_sym.
apply nil_cons.
Qed.
Lemma flatten_map:
forall x: list sf,
forall y: sentence,
flatten x = map term_lift y ->
y <> [] ->
x <> [].
Proof.
intros x y H1 H2.
destruct x.
- simpl in H1.
symmetry in H1.
apply map_eq_nil in H1.
subst.
destruct H2.
reflexivity.
- apply not_eq_sym.
apply nil_cons.
Qed.
Lemma flatten_not_nil:
forall x: sf,
flatten [x] <> [] ->
x <> [].
intros x H.
destruct x.
- simpl in H.
destruct H.
reflexivity.
- apply not_eq_sym.
apply nil_cons.
Qed.
Lemma flatten_not_nil_exists:
forall x: list sf,
flatten x <> [] ->
exists x1 x3: list sf,
exists x2: sf,
x = x1 ++ [x2] ++ x3 /\
x2 <> [].
Proof.
induction x.
- intro H.
simpl in H.
destruct H.
reflexivity.
- intro H.
simpl in H.
apply app_not_nil in H.
destruct H as [H | H].
+ exists [], x, a.
split.
* simpl.
reflexivity.
* exact H.
+ specialize (IHx H).
destruct IHx as [x1 [x3 [x2 [H1 H2]]]].
subst.
exists (a :: x1), x3, x2.
split.
* simpl.
reflexivity.
* exact H2.
Qed.
Lemma sfmatch_derives:
forall g: cfg _ _,
forall l1 l2: sf,
forall l: list sf,
forall x: non_terminal + terminal,
sfmatch g (l1 ++ [x] ++ l2) l ->
exists p: sf,
exists l3 l4: list sf,
l = l3 ++ [p] ++ l4 /\
sfmatch g l1 l3 /\
derives g [x] p /\
sfmatch g l2 l4.
Proof.
intros g l1.
induction l1.
- intros l2 l x H.
simpl in H.
inversion H.
+ (* terminal *)
subst.
exists [inr t], [], xxs.
split.
* simpl.
reflexivity.
* {
split.
- constructor.
- split.
+ constructor.
+ exact H3.
}
+ (* non-terminal *)
subst.
exists p, [], xxs.
split.
* simpl.
reflexivity.
* {
split.
- constructor.
- split.
+ assert (H6: p = [] \/ p <> []).
{
apply nil_not_nil.
}
destruct H6 as [H6 | H6].
* subst.
specialize (H2 (eq_refl [])).
exact H2.
* apply derives_g_emp_g.
apply H3.
exact H6.
+ exact H5.
}
- intros l2 l x H.
inversion H.
clear H.
+ (* terminal *)
subst.
specialize (IHl1 l2 xxs x H3).
destruct IHl1 as [p [l3 [l4 [H11 [H12 [H13 H14]]]]]].
exists p, ([inr t] :: l3), l4.
split.
* simpl.
rewrite H11.
reflexivity.
* {
split.
- apply sfmatch_term.
exact H12.
- split.
+ exact H13.
+ exact H14.
}
+ (* non-terminal *)
subst.
specialize (IHl1 l2 xxs x H5).
destruct IHl1 as [p' [l3 [l4 [H11 [H12 [H13 H14]]]]]].
exists p', ([p] ++ l3), l4.
split.
* simpl.
rewrite H11.
reflexivity.
* {
split.
- apply sfmatch_nonterm.
+ exact H2.
+ exact H3.
+ exact H12.
- split.
+ exact H13.
+ exact H14.
}
Qed.
Lemma sfmatch_derives_inv:
forall g: cfg _ _,
forall l: sf,
forall l3 l4: list sf,
forall p: sf,
sfmatch g l (l3 ++ [p] ++ l4) ->
exists x: non_terminal + terminal,
exists l1 l2: sf,
l = l1 ++ [x] ++ l2 /\
sfmatch g l1 l3 /\
sfmatch g [x] [p] /\
sfmatch g l2 l4.
Proof.
intros g l l3 l4 p H.
remember (l3 ++ [p] ++ l4) as w.
generalize dependent l4.
generalize dependent l3.
generalize dependent p.
induction H.
- (* empty *)
intros p l3 l4 H.
destruct l3.
+ inversion H.
+ inversion H.
- (* terminal *)
intros p l3 l4 H1.
destruct l3.
+ simpl in H1.
inversion H1.
exists (inr t), [], xs.
split.
* simpl.
reflexivity.
* {
split.
- constructor.
- split.
+ constructor.
constructor.
+ rewrite <- H3.
exact H.
}
+ inversion H1.
clear H1.
subst.
specialize (IHsfmatch p l3 l4 (eq_refl (l3 ++ p :: l4))).
destruct IHsfmatch as [x [l1 [l2 [H1 [H2 [H3 H4]]]]]].
rewrite H1.
exists x, (inr t :: l1), l2.
split.
* simpl.
reflexivity.
* {
split.
- apply sfmatch_term.
exact H2.
- split.
+ exact H3.
+ exact H4.
}
- (* non-terminal *)
intros p0 l3 l4 H2.
destruct l3.
inversion H2.