-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammar.v
1911 lines (1719 loc) · 61.4 KB
/
grammar.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
Require Import
Coq.Lists.List
(* dependent induction *)
Coq.Program.Equality
(* well-foundedness of lt *)
Coq.Arith.Wf_nat
(* lia *)
Psatz.
Require Import
patterns_terms
patterns_terms_dec
lib_ext.ListExt
lib_ext.PeanoNatExt.
Import ListNotations.
Open Scope list_scope.
Module Type Grammar(pt : PatTermsSymb).
Import pt.
Definition production := prod nonterm pat.
(* useful for specification purposes *)
Definition productions := list production.
Parameter production_eq_dec : forall (p1 p2 : production), {p1 = p2} + {p1 <> p2}.
(* type with which grammars are implemented *)
Parameter grammar : Set.
(* constructor *)
Parameter new_grammar : productions -> grammar.
Parameter grammar_eq_dec : forall (g1 g2 : grammar), {g1 = g2} + {g1 <> g2}.
(* query functions *)
(* membership of a production to a given grammar *)
Parameter prod_in_g : production -> grammar -> Prop.
Parameter prod_in_g_dec : forall (p : production) (g : grammar),
{prod_in_g p g} + {not (prod_in_g p g)}.
Parameter grammar_length : grammar -> nat.
(* modifiers *)
Parameter remove_prod : forall (p : production) (g : grammar)
(proof : prod_in_g p g), grammar.
(* minimal characterization of remove_prod *)
Parameter remove_prod_length_decrease :
forall (g : grammar) prod (proof : prod_in_g prod g),
grammar_length (remove_prod prod g proof) < grammar_length g.
Parameter remove_prod_in_g :
forall (pr1 pr2 : production) (g : grammar)
(proof_in : prod_in_g pr1 g),
prod_in_g pr2 (remove_prod pr1 g proof_in) ->
prod_in_g pr2 g.
End Grammar.
(* ********************************************************* *)
(* Grammars as extensional sets of productions, using lists. *)
(* ********************************************************* *)
Module GrammarLists(pt : PatTermsSymb) <: Grammar pt.
Import pt.
(* import decidability results about pats and terms *)
Module PatTermsDec := PatTermsDec(pt).
Import PatTermsDec.
Definition production := (prod nonterm pat).
Definition productions := list (production).
Definition grammar := productions.
(* ********************************************************* *)
(* Theory. *)
(* ********************************************************* *)
Lemma production_eq_dec : forall (p1 p2 : production), {p1 = p2} + {p1 <> p2}.
Proof.
intros.
destruct p1,p2.
destruct (pat_eq_dec p p0).
+ destruct (nonterm_eq_dec n n0).
- left.
rewrite e.
rewrite e0.
reflexivity.
- right.
intro.
inversion H.
contradiction.
+ destruct (nonterm_eq_dec n n0).
- right.
intro.
inversion H.
contradiction.
- right.
intro.
inversion H.
contradiction.
Defined.
Lemma list_prod_eq_dec : forall (prods1 prods2 : productions),
{prods1 = prods2} + {prods1 <> prods2}.
Proof.
intros.
dependent induction prods1.
- induction prods2.
+ left.
reflexivity.
+ right.
intro.
inversion H.
- dependent induction prods2.
+ right.
intro.
inversion H.
+ destruct (production_eq_dec a a0).
-- assert({prods1 = prods2} + {prods1 <> prods2}).
{apply (IHprods1 prods2).
}
inversion H.
++ left.
rewrite e.
rewrite H0.
reflexivity.
++ right.
intro.
inversion H1.
contradiction.
-- right.
intro.
inversion H.
contradiction.
Defined.
Lemma grammar_eq_dec : forall (g1 g2 : grammar), {g1 = g2} + {g1 <> g2}.
Proof.
apply list_prod_eq_dec.
Defined.
(* ********************************* *)
(* Manipulation of grammars *)
(* ********************************* *)
(* ********************************* *)
(* new_gramar *)
(* ********************************* *)
(* for this simple implementation we do not need to enforce some property
over the generated grammar, e.g., no duplicated productions *)
Definition new_grammar (prods : productions) : grammar := prods.
(* ********************************* *)
(* grammar_length *)
(* ********************************* *)
Definition grammar_length (g : grammar) : nat := length g.
(* ********************************* *)
(* prod_in_g *)
(* ********************************* *)
Inductive _prod_in_g : production -> grammar -> Prop :=
| prod_in_g_ev : forall (prod : production) (g : grammar),
(In prod g) -> _prod_in_g prod g.
(* TODO: "The kernel does not recognize yet that a parameter can be
instantiated by an inductive type"
no idea about this error, but this seems to be the workaround:
https://sympa.inria.fr/sympa/arc/coq-club/2017-09/msg00042.html *)
Definition prod_in_g := _prod_in_g.
Definition prod_in_g_dec (prod : production) (g : grammar) :
{prod_in_g prod g} + {not (prod_in_g prod g)}.
refine (
match (in_dec production_eq_dec prod g) as in_prod_proof return
(in_dec production_eq_dec prod g) = in_prod_proof
-> {prod_in_g prod g} + {not (prod_in_g prod g)} with
| left proof =>
fun eqp : (in_dec production_eq_dec prod g) = left proof => _
| right proof =>
fun eqp : (in_dec production_eq_dec prod g) = right proof => _
end eq_refl).
- left.
apply (prod_in_g_ev _ _ proof).
- right.
intro.
inversion H.
contradiction.
Defined.
Lemma prod_in_g_split :
forall (g : grammar) (pr : production) (proof_in : prod_in_g pr g),
exists (g1 g2 : grammar), g = g1 ++ pr :: g2 /\ not (prod_in_g pr g1).
Proof.
intros g pr Hproof_in.
inversion Hproof_in as [prod g' Hin Heq_prof Heq_g].
apply in_split in Hin.
inversion Hin as [l1 (l2, Hin'')].
clear Hin.
clear Hproof_in.
rewrite Hin''.
clear Hin''.
(* look for the first occurrence *)
induction l1 as [| hdl1 tll1 IH].
- (* l1 = nil -> we already have the first occurrence of pr *)
exists nil.
exists l2.
split.
+ (* g = split *)
reflexivity.
+ (* not prod_in *)
intro Hprod_in.
inversion Hprod_in as [pr' g'' Hin Heq_pr Heq_g''].
inversion Hin.
- (* l1 = hdl1 :: tll1 *)
destruct (production_eq_dec hdl1 pr) as [Heq_pr | Hneq_pr].
+ (* hdl1 = pr *)
exists nil.
exists (tll1 ++ pr :: l2).
split.
* (* split eq *)
rewrite Heq_pr.
reflexivity.
* (* not prod_in *)
intro Hprod_in.
inversion Hprod_in as [pr' nilg Hin Heq_pr' Heq_nil].
inversion Hin.
+ (* hdl1 <> pr *)
(* we just use the induction hypothesis *)
inversion IH as [g1 (g2, IH')].
clear IH.
exists (hdl1 :: g1).
exists g2.
inversion IH' as [Heq_split Hnprod_in].
clear IH'.
simpl.
rewrite <- Heq_split.
split.
* (* eq split *)
reflexivity.
* (* not prod_in *)
intro Hprod_in_hd_g1.
assert(prod_in_g pr g1) as Hprod_in_g1.
{inversion Hprod_in_hd_g1 as [pr' g'' Hin Heq_prod Heq_g'].
rewrite (in_inv_iff _ hdl1 pr g1) in Hin.
inversion Hin as [Heq_pr | Hin_g1].
- (* hdl1 = pr *)
contradiction.
- (* In pr g1 *)
constructor.
exact Hin_g1.
}
contradiction.
Qed.
(* tactic useful to discard a goal with a hypothesis of the form
prod_in_g pr [] *)
Ltac prod_in_g_discard :=
match goal with
| [H : prod_in_g _ []%list |- _] =>
inversion H;
match goal with
| [H' : In ?pr [] |- _] =>
inversion H'
end
end.
(* useful for reasoning about membership *)
(* ********************************* *)
(* remove_prod *)
(* ********************************* *)
(* auxiliary functions *)
(* removes a given production from a grammar (assumes productions do not
repeat) *)
Fixpoint remove_prod_aux (g : grammar) (prod : production) : grammar :=
match g with
| nil => nil
| prod' :: tail =>
match production_eq_dec prod prod' with
| left _ => tail
| _ => prod' :: remove_prod_aux tail prod
end
end.
Definition remove_prod (p : production) (g : grammar)
(proof : prod_in_g p g) : grammar := remove_prod_aux g p.
Definition fast_remove_prod (p : production) (g : grammar) : grammar :=
remove_prod_aux g p.
Lemma remove_prod_length_decrease :
forall (g : grammar) prod (proof : prod_in_g prod g),
grammar_length (remove_prod prod g proof) < grammar_length g.
Proof.
intros.
induction g.
- inversion proof.
inversion H.
- inversion proof.
apply (in_inv_trans production a prod g) in H.
inversion H.
+ unfold remove_prod.
simpl.
destruct (production_eq_dec prod a).
-- apply suc_prop_trans.
-- simpl.
unfold remove_prod in IHg.
assert(In prod g).
{assert(a = prod \/ In prod g).
{apply in_inv_trans.
exact H.
}
inversion H3.
++ symmetry in H4.
contradiction.
++ exact H4.
}
assert(prod_in_g prod g).
{constructor.
exact H3.
}
apply IHg in H4.
apply (lt_n_S_trans (grammar_length (remove_prod_aux g prod))
(grammar_length g)
H4).
+ unfold remove_prod.
simpl.
destruct (production_eq_dec prod a).
-- apply suc_prop_trans.
-- simpl.
unfold remove_prod in IHg.
simpl.
assert(In prod g).
{assert(a = prod \/ In prod g).
{apply in_inv_trans.
exact H.
}
inversion H3.
++ symmetry in H4.
contradiction.
++ exact H4.
}
assert(prod_in_g prod g).
{constructor.
exact H3.
}
apply IHg in H4.
apply (lt_n_S_trans (grammar_length (remove_prod_aux g prod))
(grammar_length g)
H4).
Defined.
Lemma fast_remove_prod_length_decrease :
forall (g : grammar) prod (proof : prod_in_g prod g),
grammar_length (fast_remove_prod prod g) < grammar_length g.
Proof.
intros.
induction g.
- inversion proof.
inversion H.
- inversion proof.
apply (in_inv_trans production a prod g) in H.
inversion H.
+ unfold remove_prod.
simpl.
destruct (production_eq_dec prod a).
-- apply suc_prop_trans.
-- simpl.
unfold remove_prod in IHg.
assert(In prod g).
{assert(a = prod \/ In prod g).
{apply in_inv_trans.
exact H.
}
inversion H3.
++ symmetry in H4.
contradiction.
++ exact H4.
}
assert(prod_in_g prod g).
{constructor.
exact H3.
}
apply IHg in H4.
apply (lt_n_S_trans (grammar_length (remove_prod_aux g prod))
(grammar_length g)
H4).
+ unfold remove_prod.
simpl.
destruct (production_eq_dec prod a).
-- apply suc_prop_trans.
-- simpl.
unfold remove_prod in IHg.
simpl.
assert(In prod g).
{assert(a = prod \/ In prod g).
{apply in_inv_trans.
exact H.
}
inversion H3.
++ symmetry in H4.
contradiction.
++ exact H4.
}
assert(prod_in_g prod g).
{constructor.
exact H3.
}
apply IHg in H4.
apply (lt_n_S_trans (grammar_length (remove_prod_aux g prod))
(grammar_length g)
H4).
Defined.
(* remove_prod removes just the first occurrence *)
Lemma remove_prod_first_occ :
forall (pr : production) (g1 g2 : grammar)
(proof_nin : not (prod_in_g pr g1))
(proof_in : prod_in_g pr (g1 ++ pr :: g2)),
remove_prod pr (g1 ++ pr :: g2) proof_in = g1 ++ g2.
Proof.
intros pr g1 g2 Hprood_nin Hproof_in.
induction g1 as [| hdg1 tlg1 IH].
- (* g1 = nil *)
simpl.
unfold remove_prod.
unfold remove_prod_aux.
destruct (production_eq_dec pr pr) as [Heq_pr | Hneq_pr].
+ (* pr = pr *)
reflexivity.
+ (* pr <> pr *)
assert (pr = pr) as Heq_pr.
{reflexivity.
}
contradiction.
- (* g1 = hdg1 :: tlg1 *)
unfold remove_prod.
unfold remove_prod_aux.
simpl.
destruct (production_eq_dec pr hdg1) as [Heq_pr | Hneq_pr].
+ (* pr = hdg1 *)
rewrite <- Heq_pr in Hprood_nin.
assert (prod_in_g pr (pr :: tlg1)) as Hprod_in.
{unfold prod_in_g.
constructor.
apply in_eq.
}
contradiction.
+ (* pr <> hdg1 *)
fold remove_prod_aux.
(* use IH *)
assert(prod_in_g pr (tlg1 ++ pr :: g2)) as Hprod_in.
{constructor.
apply in_elt.
}
assert(remove_prod pr (tlg1 ++ pr :: g2) Hprod_in = tlg1 ++ g2)
as IHinst.
{assert (~ prod_in_g pr tlg1) as Hnprod_in_tlg1.
{intro Hprod_in_tlg1.
assert(prod_in_g pr (hdg1 :: tlg1)) as Hprod_in_hd_tlg1.
{constructor.
inversion Hprod_in_tlg1 as [pr' tlg1' Hin Heq_pr Heq_tlg1].
apply in_cons.
exact Hin.
}
contradiction.
}
apply (IH Hnprod_in_tlg1 Hprod_in).
}
unfold remove_prod in IHinst.
rewrite IHinst.
reflexivity.
Qed.
(* another way of specifying that remove_prod only removes one prod *)
Lemma remove_prod_in_g :
forall (pr1 pr2 : production) (g : grammar)
(proof_in : prod_in_g pr1 g),
prod_in_g pr2 (remove_prod pr1 g proof_in) ->
prod_in_g pr2 g.
Proof.
intros pr1 pr2 g Hproof_in Hprod_in.
assert(exists (g1 g2 : grammar), g = g1 ++ pr1 :: g2 /\
not (prod_in_g pr1 g1))
as Hg_split.
{apply prod_in_g_split.
exact Hproof_in.
}
inversion Hg_split as [g1 (g2, Hg_split')].
clear Hg_split.
inversion Hg_split' as [Hg_split Hnprod_in].
revert Hprod_in.
revert Hproof_in.
rewrite Hg_split.
intros Hproof_in Hprod_in.
assert(remove_prod pr1 (g1 ++ pr1 :: g2) Hproof_in = g1 ++ g2)
as Hremove_prod.
{apply remove_prod_first_occ.
exact Hnprod_in.
}
rewrite Hremove_prod in Hprod_in.
inversion Hprod_in as [pr2' g1_g2 Hin_g1_g2 Heq_pr2 Heq_g1_g2].
rewrite in_app_iff in Hin_g1_g2.
constructor.
rewrite in_app_iff.
inversion Hin_g1_g2 as [Hin_pr2_g1 | Hin_pr2_g2].
- (* In pr2 g1 *)
left.
exact Hin_pr2_g1.
- (* In pr2 g2 *)
right.
apply in_cons.
exact Hin_pr2_g2.
Qed.
(* more characterization lemmas for remove_prod *)
Lemma remove_prod_in_g_neq :
forall (pr1 pr2 : production) (g : grammar)
(proof_in : prod_in_g pr2 g)
(proof_neq : pr1 <> pr2),
prod_in_g pr1 g ->
prod_in_g pr1 (remove_prod pr2 g proof_in).
Proof.
intros pr1 pr2 g Hproof_in_pr2 Hneq Hproof_in_pr1.
assert(exists (g1 g2 : grammar), g = g1 ++ pr2 :: g2 /\
not (prod_in_g pr2 g1))
as [g1 (g2, (Hsplit_g_pr2, Hnot_pr2_g1))].
{generalize prod_in_g_split; intro lemma.
eauto.
}
revert Hproof_in_pr2.
rewrite Hsplit_g_pr2.
intro Hproof_in_pr2.
assert(remove_prod pr2 (g1 ++ pr2 :: g2) Hproof_in_pr2 = g1 ++ g2)
as Hremove_pr2.
{generalize remove_prod_first_occ; intro lemma.
eauto.
}
rewrite Hremove_pr2.
clear Hnot_pr2_g1 Hproof_in_pr2 Hremove_pr2.
rewrite Hsplit_g_pr2 in Hproof_in_pr1.
unfold prod_in_g in Hproof_in_pr1.
inversion Hproof_in_pr1 as [prod g' Hin_pr1_g Heq_prod Heq_g'].
rewrite in_app_iff in Hin_pr1_g.
inversion Hin_pr1_g as [Hin_pr1_g1 | Hin_pr1_tl].
- (* In pr1 g1 *)
unfold prod_in_g.
constructor.
rewrite in_app_iff.
left.
exact Hin_pr1_g1.
- (* In pr1 tl *)
inversion Hin_pr1_tl as [Heq_pr1_pr2 | Hin_pr1_g2].
+ (* pr1 = pr2 *)
apply eq_sym in Heq_pr1_pr2.
contradiction.
+ (* In pr1 g2 *)
constructor.
rewrite in_app_iff.
right.
exact Hin_pr1_g2.
Qed.
Lemma remove_prod_in_g_comm :
forall (pr1 pr2 : production) (g : grammar)
(proof_in : prod_in_g pr1 g),
prod_in_g pr2 (remove_prod pr1 g proof_in) ->
exists (proof_in_pr2 : prod_in_g pr2 g),
prod_in_g pr1 (remove_prod pr2 g proof_in_pr2).
Proof.
intros pr1 pr2 g Hproof_in_pr1 Hprod_in_pr2.
assert(prod_in_g pr2 g)
as Hprod_in_pr2'.
{apply (remove_prod_in_g pr1 pr2 g Hproof_in_pr1).
exact Hprod_in_pr2.
}
exists Hprod_in_pr2'.
(* reason about the result of remove_prod *)
assert(exists (g1 g2 : grammar), g = g1 ++ pr1 :: g2 /\ not (prod_in_g pr1 g1))
as Hsplit_g_pr1.
{apply prod_in_g_split.
exact Hproof_in_pr1.
}
inversion Hsplit_g_pr1 as [g1 (g2, (Heq_g_pr1, Hnot_prod_in_pr1))].
clear Hsplit_g_pr1.
assert(remove_prod pr1 g Hproof_in_pr1 = g1 ++ g2)
as Heq_g_remove_pr1.
{clear Hprod_in_pr2.
revert Hproof_in_pr1.
rewrite Heq_g_pr1.
intros Hproof_in_pr1.
apply (remove_prod_first_occ pr1 g1 g2 Hnot_prod_in_pr1 Hproof_in_pr1).
}
rewrite Heq_g_remove_pr1 in Hprod_in_pr2.
inversion Hprod_in_pr2 as [prod g_res Hin_pr2 Heq_prod Heq_g_res].
rewrite (in_app_iff_strong _ _ _ _ production_eq_dec) in Hin_pr2.
inversion Hin_pr2 as [Hpr2_in_g1 | Hpr2_in_g2].
- (* In pr2 g1 *)
apply prod_in_g_ev in Hpr2_in_g1.
fold prod_in_g in Hpr2_in_g1.
apply prod_in_g_split in Hpr2_in_g1.
inversion Hpr2_in_g1 as [g11 (g12, (Heq_g1, Hnot_prod_in_pr2))].
clear Hpr2_in_g1.
revert Hprod_in_pr2'.
rewrite Heq_g_pr1.
rewrite Heq_g1.
rewrite <- app_assoc.
intro Hprod_in_pr2'.
assert(remove_prod pr2 (g11 ++ (pr2 :: g12) ++ pr1 :: g2) Hprod_in_pr2'
= g11 ++ g12 ++ pr1 :: g2)
as Heq_remove_prod_pr2.
{apply remove_prod_first_occ.
exact Hnot_prod_in_pr2.
}
rewrite Heq_remove_prod_pr2.
constructor.
rewrite in_app_iff.
right.
rewrite in_app_iff.
right.
apply in_eq.
- (* In pr2 g2 *)
(* analogous to the previous case *)
inversion Hpr2_in_g2 as [Hnot_in_g1 Hpr2_in_g2'].
clear Hpr2_in_g2.
apply prod_in_g_ev in Hpr2_in_g2'.
fold prod_in_g in Hpr2_in_g2'.
apply prod_in_g_split in Hpr2_in_g2'.
inversion Hpr2_in_g2' as [g21 (g22, (Heq_g2, Hnot_prod_in_pr2))].
clear Hpr2_in_g2'.
revert Hprod_in_pr2'.
rewrite Heq_g_pr1.
rewrite Heq_g2.
intro Hprod_in_pr2'.
destruct (production_eq_dec pr1 pr2) as [Heq_pr1_pr2 | Hneq_pr1_pr2].
* (* pr1 = pr2 *)
rewrite <- Heq_pr1_pr2 in Hnot_in_g1.
assert(remove_prod pr2 (g1 ++ pr1 :: g21 ++ pr2 :: g22) Hprod_in_pr2'
= g1 ++ g21 ++ pr2 :: g22)
as Heq_remove_prod_pr2.
{revert Hprod_in_pr2'.
rewrite Heq_pr1_pr2.
intro Hprod_in_pr2'.
apply remove_prod_first_occ.
rewrite Heq_pr1_pr2 in Hnot_in_g1.
intro Hprod_in.
inversion Hprod_in as [pr2' g1' Hin_g1 Heq_g1].
contradiction.
}
rewrite Heq_remove_prod_pr2.
constructor.
rewrite in_app_iff.
right.
rewrite in_app_iff.
right.
rewrite Heq_pr1_pr2.
apply in_eq.
* (* pr1 <> pr2 *)
assert(not (In pr2 (g1 ++ pr1 :: g21)))
as Hnot_pr2_in.
{intro Hpr2_in.
rewrite in_app_iff in Hpr2_in.
inversion Hpr2_in as [Hpr2_in_g1 | Hpr2_in_tl].
- (* In pr2 g1 *)
contradiction.
- (* In pr2 (pr1 :: g21) *)
inversion Hpr2_in_tl as [Hpr2_in_hd | Hpr2_in_tl'].
+ (* pr1 = pr2 *)
contradiction.
+ (* In pr2 g21 *)
apply prod_in_g_ev in Hpr2_in_tl'.
fold prod_in_g in Hpr2_in_tl'.
contradiction.
}
assert(remove_prod pr2 (g1 ++ pr1 :: g21 ++ pr2 :: g22) Hprod_in_pr2'
= (g1 ++ pr1 :: g21) ++ g22)
as Heq_remove_prod_pr2.
{assert(~ (prod_in_g pr2 (g1 ++ pr1 :: g21)))
as Hnot_pr2_in'.
{intro Hpr2_in.
inversion Hpr2_in as [pr2' g' Hin_pr2' Heq_pr2' Heq_g'].
contradiction.
}
assert(g1 ++ pr1 :: g21 ++ pr2 :: g22
=
(g1 ++ pr1 :: g21) ++ pr2 :: g22)
as Hassoc.
{apply (app_assoc g1 (pr1 :: g21) (pr2 :: g22)).
}
revert Hprod_in_pr2'.
rewrite Hassoc.
clear Hassoc.
intro Hprod_in_pr2'.
apply (remove_prod_first_occ pr2 (g1 ++ pr1 :: g21) g22
Hnot_pr2_in' Hprod_in_pr2').
}
rewrite Heq_remove_prod_pr2.
constructor.
rewrite in_app_iff.
left.
rewrite in_app_iff.
right.
apply in_eq.
Qed.
(* useful when reasoning about soundness of our manipulation of
grammars *)
Lemma remove_prod_comm :
forall (pr1 pr2 : production)
(g : grammar)
(proof_in_pr1 : prod_in_g pr1 g)
(proof_in_pr2 : prod_in_g pr2 (remove_prod pr1 g proof_in_pr1)),
exists (proof_in_pr2' : prod_in_g pr2 g)
(proof_in_pr1' : prod_in_g pr1 (remove_prod pr2 g proof_in_pr2')),
remove_prod pr2 (remove_prod pr1 g proof_in_pr1) proof_in_pr2
= remove_prod pr1 (remove_prod pr2 g proof_in_pr2') proof_in_pr1'.
Proof.
intros pr1 pr2 g Hproof_in_pr1 Hproof_in_pr2.
assert(exists (proof_in_pr2 : prod_in_g pr2 g),
prod_in_g pr1 (remove_prod pr2 g proof_in_pr2))
as Hprod_in_g_pr1.
{apply (remove_prod_in_g_comm pr1 pr2 g Hproof_in_pr1 Hproof_in_pr2).
}
inversion Hprod_in_g_pr1 as [Hprod_in_g_pr2 Hprod_in_g_pr1'].
clear Hprod_in_g_pr1.
exists Hprod_in_g_pr2.
exists Hprod_in_g_pr1'.
(* reason about the result of remove_prod *)
assert(exists (g1 g2 : grammar), g = g1 ++ pr1 :: g2 /\ not (prod_in_g pr1 g1))
as Hsplit_g_pr1.
{apply prod_in_g_split.
exact Hproof_in_pr1.
}
inversion Hsplit_g_pr1 as [g1 (g2, (Heq_g_pr1, Hnot_prod_in_pr1))].
clear Hsplit_g_pr1.
assert(remove_prod pr1 g Hproof_in_pr1 = g1 ++ g2)
as Heq_remove_pr1.
{revert Hproof_in_pr2.
revert Hproof_in_pr1.
rewrite Heq_g_pr1.
intros Hproof_in_pr1 Hproof_in_pr2.
apply remove_prod_first_occ.
exact Hnot_prod_in_pr1.
}
revert Hproof_in_pr2.
rewrite Heq_remove_pr1.
intro Hproof_in_pr2.
inversion Hproof_in_pr2 as [prod' g' Hin_pr2_g1_g2 Heq_prod Heq_g'].
rewrite (in_app_iff_strong _ _ _ _ production_eq_dec) in Hin_pr2_g1_g2.
clear Heq_g' Heq_prod g' prod'.
inversion Hin_pr2_g1_g2 as [Hin_pr2 | (Hnin_pr2_g1, Hin_pr2_g2)].
- (* In pr2 g1 *)
apply prod_in_g_ev in Hin_pr2.
fold prod_in_g in Hin_pr2.
assert(exists (g11 g12 : grammar), g1 = g11 ++ pr2 :: g12 /\
not (prod_in_g pr2 g11))
as Hsplit_g1_pr2.
{apply (prod_in_g_split g1 pr2 Hin_pr2).
}
inversion Hsplit_g1_pr2 as [g11 (g12, (Heq_g_pr2, Hnot_prod_in_pr2))].
clear Hsplit_g1_pr2.
assert(remove_prod pr2 g Hprod_in_g_pr2 = g11 ++ g12 ++ pr1 :: g2)
as Heq_remove_pr2.
{revert Hprod_in_g_pr1'.
revert Hprod_in_g_pr2.
rewrite Heq_g_pr1.
rewrite Heq_g_pr2.
intros Hprod_in_g_pr2 Hprod_in_g_pr1'.
assert((g11 ++ pr2 :: g12) ++ pr1 :: g2 =
g11 ++ (pr2 :: g12 ++ pr1 :: g2))
as Hassoc.
{rewrite <- app_assoc.
reflexivity.
}
revert Hprod_in_g_pr1'.
revert Hprod_in_g_pr2.
rewrite Hassoc.
intros Hprod_in_g_pr2 Hprod_in_g_pr1'.
apply remove_prod_first_occ.
exact Hnot_prod_in_pr2.
}
revert Hprod_in_g_pr1'.
rewrite Heq_remove_pr2.
revert Hproof_in_pr2.
rewrite Heq_g_pr2.
intros Hproof_in_pr2 Hprod_in_g_pr1'.
clear Heq_remove_pr1 Heq_remove_pr2.
assert(remove_prod pr2 ((g11 ++ pr2 :: g12) ++ g2) Hproof_in_pr2
= g11 ++ g12 ++ g2)
as Heq_remove_pr2.
{revert Hproof_in_pr2.
rewrite <- (app_assoc g11 (pr2 :: g12) g2).
intro Hproof_in_pr2.
apply remove_prod_first_occ.
exact Hnot_prod_in_pr2.
}
rewrite Heq_remove_pr2.
clear Heq_remove_pr2.
assert(remove_prod pr1 (g11 ++ g12 ++ pr1 :: g2) Hprod_in_g_pr1'
=
(g11 ++ g12) ++ g2)
as Heq_remove_pr1.
{assert(g11 ++ g12 ++ pr1 :: g2 = (g11 ++ g12) ++ pr1 :: g2)
as Hassoc.
{rewrite app_assoc.
reflexivity.
}
revert Hprod_in_g_pr1'.
rewrite Hassoc.
intro Hprod_in_g_pr1'.
apply remove_prod_first_occ.
intro Hnot_prod_in_pr1'.
assert(prod_in_g pr1 (g11 ++ pr2 :: g12))
as Hprod_in_pr1.
{rewrite Heq_g_pr2 in Hnot_prod_in_pr1.
inversion Hnot_prod_in_pr1' as [prod g' Hin_pr1_g11_g12 Heq_prod
Heq_g'].
rewrite in_app_iff in Hin_pr1_g11_g12.
constructor.
rewrite in_app_iff.
inversion Hin_pr1_g11_g12 as [Hin_pr1_g11 | Hin_pr1_g12].
- (* In pr1 g11 *)
left.
exact Hin_pr1_g11.
- (* In pr1 g12 *)
right.
apply in_cons.
exact Hin_pr1_g12.
}
rewrite <- Heq_g_pr2 in Hprod_in_pr1.
contradiction.
}
rewrite Heq_remove_pr1.
rewrite app_assoc.
reflexivity.
- (* In pr2 g2 *)
apply prod_in_g_ev in Hin_pr2_g2.
fold prod_in_g in Hin_pr2_g2.
destruct (production_eq_dec pr1 pr2) as [Heq_pr1_pr2 | Hneq_pr1_pr2].
+ (* pr1 = pr2 *)
assert(remove_prod pr2 g Hprod_in_g_pr2 = g1 ++ g2)
as Heq_remove_pr2.
{revert Hprod_in_g_pr1'.
revert Hprod_in_g_pr2.
rewrite Heq_g_pr1.
rewrite Heq_pr1_pr2.
intros Hprod_in_g_pr2 Hprod_in_g_pr1'.
apply remove_prod_first_occ.
rewrite Heq_pr1_pr2 in Hnot_prod_in_pr1.
exact Hnot_prod_in_pr1.
}
revert Hprod_in_g_pr1'.
rewrite Heq_remove_pr2.
rewrite Heq_pr1_pr2.
intro Hprod_in_g_pr1'.
assert(exists (g21 g22 : grammar), g2 = g21 ++ pr2 :: g22 /\
not (prod_in_g pr2 g21))
as Hsplit_g1_pr2.
{apply (prod_in_g_split g2 pr2 Hin_pr2_g2).
}
inversion Hsplit_g1_pr2 as [g21 (g22, (Heq_g_pr2, Hnot_prod_in_pr2))].
clear Hsplit_g1_pr2.
revert Hproof_in_pr2.
revert Hprod_in_g_pr1'.
rewrite Heq_g_pr2.
intros Hprod_in_g_pr1' Hproof_in_pr2.
clear Hproof_in_pr1 Hprod_in_g_pr2 Heq_g_pr1 Hnot_prod_in_pr1
Heq_remove_pr1 Hin_pr2_g1_g2 Heq_remove_pr2.
assert(remove_prod pr2 (g1 ++ g21 ++ pr2 :: g22) Hproof_in_pr2
=
g1 ++ g21 ++ g22)
as Heq_remove_pr2.
{ assert(g1 ++ g21 ++ pr2 :: g22 = (g1 ++ g21) ++ pr2 :: g22)
as Hassoc.
{rewrite app_assoc.
reflexivity.
}
revert Hproof_in_pr2.
rewrite Hassoc.
clear Hassoc.
intro Hproof_in_pr2.
assert(g1 ++ g21 ++ g22 = (g1 ++ g21) ++ g22)
as Hassoc.
{rewrite app_assoc.
reflexivity.
}
rewrite Hassoc.
apply remove_prod_first_occ.
intro Hprod_in_g1_g21.
inversion Hprod_in_g1_g21 as [prod' g' Hin_pr2_g1_g21 Heq_prod Heq_g'].
rewrite in_app_iff in Hin_pr2_g1_g21.
inversion Hin_pr2_g1_g21 as [Hpr2_in_g1 | Hpr2_in_g21].
- (* In pr2 g1 *)
contradiction.
- (* In pr2 g21 *)
apply prod_in_g_ev in Hpr2_in_g21.
fold prod_in_g in Hpr2_in_g21.
contradiction.
}
rewrite Heq_remove_pr2.
clear Heq_remove_pr2.
assert(remove_prod pr2 (g1 ++ g21 ++ pr2 :: g22) Hprod_in_g_pr1'
=
g1 ++ g21 ++ g22)
as Heq_remove_pr2.
{ assert(g1 ++ g21 ++ pr2 :: g22 = (g1 ++ g21) ++ pr2 :: g22)
as Hassoc.
{rewrite app_assoc.
reflexivity.
}
revert Hproof_in_pr2.
revert Hprod_in_g_pr1'.
rewrite Hassoc.
clear Hassoc.
intros Hprod_in_g_pr1' Hproof_in_pr2.