forked from mvmramos/pumping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trees.v
4531 lines (4415 loc) · 102 KB
/
trees.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* ---------------------------------------------------------------------
This file contains definitions and proof scripts related to
(i) closure operations for context-free grammars,
(ii) context-free grammars simplification
(iii) context-free grammar Chomsky normalization and
(iv) pumping lemma for context-free languages.
More information can be found in the paper "Formalization of the
pumping lemma for context-free languages", submitted to
LATA 2016.
Marcus Vinícius Midena Ramos
--------------------------------------------------------------------- *)
Require Import misc_arith.
Require Import misc_list.
Require Import misc_logic.
Require Import cfg.
Require Import chomsky.
Require Import List.
Require Import Omega.
Require Import Ring.
Require Import NPeano.
Require Import Even.
Require Import NZPow.
Import ListNotations.
Open Scope list_scope.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
(* --------------------------------------------------------------------- *)
(* BINARY TREES *)
(* --------------------------------------------------------------------- *)
Section Binary_Trees.
Variable non_terminal: Type.
Variable terminal: Type.
Notation symbol:= (non_terminal + terminal)%type.
Notation sf:= (list symbol).
Notation term_lift:= ((terminal_lift non_terminal) terminal).
Notation sentence:= (list terminal).
Inductive btree: Type:=
| bnode_1: non_terminal -> terminal -> btree
| bnode_2: non_terminal -> btree -> btree -> btree.
Definition broot (t: btree): non_terminal:=
match t with
| bnode_1 n t => n
| bnode_2 n t1 t2 => n
end.
Inductive bpath (bt: btree): sf -> Prop:=
| bp_1: forall n: non_terminal,
forall t: terminal,
bt = (bnode_1 n t) -> bpath bt [inl n; inr t]
| bp_l: forall n: non_terminal,
forall bt1 bt2: btree,
forall p1: sf,
bt = bnode_2 n bt1 bt2 -> bpath bt1 p1 -> bpath bt ((inl n) :: p1)
| bp_r: forall n: non_terminal,
forall bt1 bt2: btree,
forall p2: sf,
bt = bnode_2 n bt1 bt2 -> bpath bt2 p2 -> bpath bt ((inl n) :: p2).
Inductive bnts (bt: btree) (ntl: list non_terminal): Prop:=
| bn_1: forall n: non_terminal,
forall t: terminal,
bt = (bnode_1 n t) -> In n ntl -> bnts bt ntl
| bn_2: forall n: non_terminal,
forall bt1 bt2: btree,
bt = bnode_2 n bt1 bt2 ->
In n ntl ->
bnts bt1 ntl ->
bnts bt2 ntl ->
bnts bt ntl.
Inductive subtree (t: btree): btree -> Prop:=
| sub_br: forall tl tr: btree,
forall n: non_terminal,
t = bnode_2 n tl tr ->
subtree t tr
| sub_bl: forall tl tr: btree,
forall n: non_terminal,
t = bnode_2 n tl tr ->
subtree t tl
| sub_ir: forall tl tr t': btree,
forall n: non_terminal,
subtree tr t' ->
t = bnode_2 n tl tr ->
subtree t t'
| sub_il: forall tl tr t': btree,
forall n: non_terminal,
subtree tl t' ->
t = bnode_2 n tl tr ->
subtree t t'.
Inductive bcode (bt: btree): list bool -> Prop:=
| bcode_0: forall n: non_terminal,
forall t: terminal,
bt = (bnode_1 n t) -> bcode bt []
| bcode_1: forall n: non_terminal,
forall bt1 bt2: btree,
forall c1: list bool,
bt = bnode_2 n bt1 bt2 -> bcode bt1 c1 -> bcode bt (false :: c1)
| bcode_2: forall n: non_terminal,
forall bt1 bt2: btree,
forall c2: list bool,
bt = bnode_2 n bt1 bt2 -> bcode bt2 c2 -> bcode bt (true :: c2).
Inductive bpath_bcode (bt: btree): sf -> (list bool) -> Prop:=
| bb_0: forall n: non_terminal,
forall t: terminal,
bt = (bnode_1 n t) -> bpath_bcode bt [inl n; inr t] []
| bb_1: forall n: non_terminal,
forall bt1 bt2: btree,
forall c1: list bool,
forall p1: sf,
bt = (bnode_2 n bt1 bt2) ->
bpath bt1 p1 ->
bpath_bcode bt1 p1 c1 ->
bpath_bcode bt ((inl n) :: p1) (false :: c1)
| bb_2: forall n: non_terminal,
forall bt1 bt2: btree,
forall c2: list bool,
forall p2: sf,
bt = (bnode_2 n bt1 bt2) ->
bpath bt2 p2 ->
bpath_bcode bt2 p2 c2 ->
bpath_bcode bt ((inl n) :: p2) (true :: c2).
Inductive subtree_bcode (t1 t2: btree): list bool -> Prop:=
| sb_br: forall tl: btree,
forall n: non_terminal,
t1 = bnode_2 n tl t2 ->
subtree_bcode t1 t2 [true]
| sb_bl: forall tr: btree,
forall n: non_terminal,
t1 = bnode_2 n t2 tr ->
subtree_bcode t1 t2 [false]
| sb_ir: forall tl tr: btree,
forall n: non_terminal,
forall c: list bool,
subtree_bcode tr t2 c ->
t1 = bnode_2 n tl tr ->
subtree_bcode t1 t2 (true :: c)
| sb_il: forall tl tr: btree,
forall n: non_terminal,
forall c: list bool,
subtree_bcode tl t2 c ->
t1 = bnode_2 n tl tr ->
subtree_bcode t1 t2 (false :: c).
Lemma bnts_app_l:
forall t: btree,
forall l l': list non_terminal,
bnts t l -> bnts t (l' ++ l).
Proof.
induction t.
- intros l l' H.
inversion H.
+ inversion H0.
subst.
apply bn_1 with (n:= n0) (t:= t0).
* reflexivity.
* apply in_or_app.
right.
exact H1.
+ discriminate.
- intros l l' H.
apply bn_2 with (n:= n) (bt1:= t1) (bt2:= t2).
+ reflexivity.
+ inversion H.
* discriminate.
* inversion H0.
subst.
apply in_or_app.
right.
exact H1.
+ inversion H.
* discriminate.
* inversion H0.
subst.
specialize (IHt1 l l' H2).
exact IHt1.
+ inversion H.
* discriminate.
* inversion H0.
subst.
specialize (IHt2 l l' H3).
exact IHt2.
Qed.
Lemma bnts_app_r:
forall t: btree,
forall l l': list non_terminal,
bnts t l -> bnts t (l ++ l').
Proof.
induction t.
- intros l l' H.
inversion H.
+ inversion H0.
subst.
apply bn_1 with (n:= n0) (t:= t0).
* reflexivity.
* apply in_or_app.
left.
exact H1.
+ discriminate.
- intros l l' H.
apply bn_2 with (n:= n) (bt1:= t1) (bt2:= t2).
+ reflexivity.
+ inversion H.
* discriminate.
* inversion H0.
subst.
apply in_or_app.
left.
exact H1.
+ inversion H.
* discriminate.
* inversion H0.
subst.
specialize (IHt1 l l' H2).
exact IHt1.
+ inversion H.
* discriminate.
* inversion H0.
subst.
specialize (IHt2 l l' H3).
exact IHt2.
Qed.
Lemma bnts_app:
forall t: btree,
forall l l' l'': list non_terminal,
bnts t l -> bnts t (l' ++ l ++ l'').
Proof.
intros t l l' l'' H.
apply bnts_app_l.
apply bnts_app_r.
exact H.
Qed.
Fixpoint bfrontier (t: btree): sentence:=
match t with
| bnode_1 n t => [t]
| bnode_2 n t1 t2 => bfrontier t1 ++ bfrontier t2
end.
Lemma bfrontier_ge_1:
forall t: btree,
length (bfrontier t) >= 1.
Proof.
induction t.
- simpl.
omega.
- simpl.
rewrite app_length.
omega.
Qed.
Fixpoint bheight (t: btree): nat:=
match t with
| bnode_1 n t => 1
| bnode_2 n t1 t2 => S (max (bheight t1) (bheight t2))
end.
Lemma not_bheight_0:
forall t: btree,
~ bheight t = 0.
Proof.
intros t H.
destruct t.
- simpl in H.
omega.
- simpl in H.
inversion H.
Qed.
Lemma bheight_ge_1:
forall t: btree,
bheight t >= 1.
Proof.
intros t.
destruct t.
- simpl.
omega.
- simpl.
omega.
Qed.
Lemma bheight_ge_or:
forall n: non_terminal,
forall bt1 bt2: btree,
forall k: nat,
bheight (bnode_2 n bt1 bt2) >= k ->
(bheight bt1 >= bheight bt2 /\ bheight bt1 >= k - 1)
\/
(bheight bt2 >= bheight bt1 /\ bheight bt2 >= k - 1).
Proof.
intros n bt1 bt2 k H2.
simpl in H2.
assert (H3: (bheight bt1) >= (bheight bt2) \/ (bheight bt1) <= (bheight bt2)) by omega.
destruct H3 as [H3 | H3].
- apply max_l in H3.
rewrite H3 in H2.
left.
split.
+ apply Nat.max_l_iff in H3.
exact H3.
+ omega.
- apply max_r in H3.
rewrite H3 in H2.
right.
split.
+ apply Nat.max_r_iff in H3.
exact H3.
+ omega.
Qed.
Lemma bheight_gt_or:
forall n: non_terminal,
forall bt1 bt2: btree,
forall k: nat,
bheight (bnode_2 n bt1 bt2) > k ->
(bheight bt1 >= bheight bt2 /\ bheight bt1 > k - 1)
\/
(bheight bt2 >= bheight bt1 /\ bheight bt2 > k - 1).
Proof.
intros n bt1 bt2 k H2.
simpl in H2.
assert (H3: (bheight bt1) >= (bheight bt2) \/ (bheight bt1) <= (bheight bt2)) by omega.
destruct H3 as [H3 | H3].
- apply max_l in H3.
rewrite H3 in H2.
left.
split.
+ apply Nat.max_l_iff in H3.
exact H3.
+ apply gt_S_n.
destruct bt1.
* simpl.
simpl in H2.
omega.
* simpl.
simpl in H2.
omega.
- apply max_r in H3.
rewrite H3 in H2.
right.
split.
+ apply Nat.max_r_iff in H3.
exact H3.
+ apply gt_S_n.
destruct bt2.
* simpl.
simpl in H2.
omega.
* simpl.
simpl in H2.
omega.
Qed.
Lemma bheight_left:
forall n: non_terminal,
forall bt1 bt2: btree,
forall i: nat,
bheight (bnode_2 n bt1 bt2) = S i ->
bheight bt1 >= bheight bt2 ->
bheight bt1 = i.
Proof.
intros n bt1 bt2 i H1 H2.
simpl in H1.
apply max_l in H2.
rewrite H2 in H1.
omega.
Qed.
Lemma bheight_right:
forall n: non_terminal,
forall bt1 bt2: btree,
forall i: nat,
bheight (bnode_2 n bt1 bt2) = S i ->
bheight bt1 <= bheight bt2 ->
bheight bt2 = i.
Proof.
intros n bt1 bt2 i H1 H2.
simpl in H1.
apply max_r in H2.
rewrite H2 in H1.
omega.
Qed.
Fixpoint bnodes (t: btree): nat:=
match t with
| bnode_1 n t => 2
| bnode_2 n t1 t2 => S ((bnodes t1) + (bnodes t2))
end.
Lemma bfrontier_min:
forall t: btree,
length (bfrontier t) >= bheight t.
Proof.
induction t.
- simpl.
omega.
- simpl.
rewrite app_length.
assert (H: bheight t1 >= bheight t2 \/ bheight t1 <= bheight t2) by omega.
destruct H as [H | H].
+ apply max_l in H.
rewrite H.
assert (H2: length (bfrontier t2) >= 1).
{
apply bfrontier_ge_1.
}
omega.
+ apply max_r in H.
rewrite H.
assert (H2: length (bfrontier t1) >= 1).
{
apply bfrontier_ge_1.
}
omega.
Qed.
Lemma bfrontier_max:
forall t: btree,
length (bfrontier t) <= 2 ^ ((bheight t) - 1).
Proof.
induction t.
- simpl.
omega.
- simpl.
rewrite app_length.
rewrite <- minus_n_O.
assert (H: (bheight t1) <= (bheight t2) \/
(bheight t1) >= (bheight t2)) by omega.
destruct H as [H | H].
+ assert (H':= H).
apply max_r in H.
rewrite H.
assert (H1: 2 ^ (bheight t1 - 1) <= 2 ^ (bheight t2 - 1)).
{
apply pow_le.
- omega.
- omega.
}
assert (H2: length (bfrontier t1) <= 2 ^ (bheight t2 - 1)) by omega.
assert (H3: length (bfrontier t1) + length (bfrontier t2) <= 2 ^ (bheight t2 - 1) + 2 ^ (bheight t2 - 1)) by omega.
assert (H4: (2 ^ ((bheight t2) - 1)) + (2 ^ ((bheight t2) - 1)) = 2 * (2 ^ ((bheight t2) - 1))).
{
apply sum_double.
}
rewrite H4 in H3.
assert (H5: 2 * (2 ^ (bheight t2 - 1)) = 2 ^ bheight t2).
{
apply add_exp.
apply bheight_ge_1.
}
rewrite H5 in H3.
exact H3.
+ assert (H':= H).
apply max_l in H.
rewrite H.
assert (H1: 2 ^ (bheight t2 - 1) <= 2 ^ (bheight t1 - 1)).
{
apply pow_le.
- omega.
- omega.
}
assert (H2: length (bfrontier t2) <= 2 ^ (bheight t1 - 1)) by omega.
assert (H3: length (bfrontier t1) + length (bfrontier t2) <= 2 ^ (bheight t1 - 1) + 2 ^ (bheight t1 - 1)) by omega.
assert (H4: (2 ^ ((bheight t1) - 1)) + (2 ^ ((bheight t1) - 1)) = 2 * (2 ^ ((bheight t1) - 1))).
{
apply sum_double.
}
rewrite H4 in H3.
assert (H5: 2 * (2 ^ (bheight t1 - 1)) = 2 ^ bheight t1).
{
apply add_exp.
apply bheight_ge_1.
}
rewrite H5 in H3.
exact H3.
Qed.
Lemma bfrontier_min_max:
forall t: btree,
length (bfrontier t) >= bheight t /\
length (bfrontier t) <= 2 ^((bheight t) - 1).
Proof.
intros t.
split.
- apply bfrontier_min.
- apply bfrontier_max.
Qed.
Lemma bheight_eq:
forall t: btree,
forall i: nat,
bheight t = i -> length (bfrontier t) <= 2 ^ (i - 1).
Proof.
intros t i H.
rewrite <- H.
apply bfrontier_max.
Qed.
Lemma bheight_le:
forall t: btree,
forall i: nat,
bheight t <= i -> length (bfrontier t) <= 2 ^ (i - 1).
Proof.
intros t i H.
assert (H2: length (bfrontier t) <= 2 ^ ((bheight t) - 1)).
{
apply bfrontier_max.
}
assert (H3: exists x: nat, i = (bheight t) + x).
{
exists (i - bheight t).
rewrite le_plus_minus_r.
- reflexivity.
- exact H.
}
destruct H3 as [x H3].
rewrite H3.
assert (H4: 2 ^ (bheight t - 1) <= 2 ^ (bheight t + x - 1)).
{
induction x.
- simpl.
rewrite <- plus_n_O.
omega.
- apply pow_le.
+ omega.
+ omega.
}
omega.
Qed.
Lemma bheight_lt:
forall t: btree,
forall i: nat,
bheight t < i -> length (bfrontier t) < 2 ^ (i - 1).
Proof.
intros t i H.
assert (H2: length (bfrontier t) <= 2 ^ ((bheight t) - 1)).
{
apply bfrontier_max.
}
assert (H3: exists x: nat, i = (bheight t) + x).
{
exists (i - bheight t).
rewrite le_plus_minus_r.
- reflexivity.
- omega.
}
destruct H3 as [x H3].
rewrite H3.
assert (H4: 2 ^ (bheight t - 1) < 2 ^ (bheight t + x - 1)).
{
induction x.
- simpl.
rewrite <- plus_n_O.
omega.
- apply pow_lt.
+ omega.
+ assert (H4: bheight t >= 1).
{
apply bheight_ge_1.
}
omega.
}
omega.
Qed.
Lemma length_bfrontier_ge:
forall t: btree,
forall i: nat,
length (bfrontier t) >= 2 ^ (i - 1) ->
bheight t >= i.
Proof.
intros t i H.
assert (H2: bheight t < i -> length (bfrontier t) < 2 ^ (i - 1)).
{
apply bheight_lt.
}
apply contrap in H2.
- assert (H3: bheight t < i \/ bheight t > i \/ bheight t = i) by omega.
destruct H3 as [H3 | [H3 | H3]].
+ omega.
+ omega.
+ omega.
- intros H3.
omega.
Qed.
Lemma length_bfrontier_gt:
forall t: btree,
forall i: nat,
length (bfrontier t) > 2 ^ (i - 1) ->
bheight t > i.
Proof.
intros t i H.
assert (H2: bheight t <= i -> length (bfrontier t) <= 2 ^ (i - 1)).
{
apply bheight_le.
}
apply contrap in H2.
- assert (H3: bheight t < i \/ bheight t > i \/ bheight t = i) by omega.
destruct H3 as [H3 | [H3 | H3]].
+ omega.
+ exact H3.
+ omega.
- intros H3.
omega.
Qed.
Lemma length_ge:
forall t: btree,
forall s: sentence,
forall i: nat,
bfrontier t = s ->
length s >= 2 ^ i ->
bheight t >= (i + 1).
Proof.
intros t s i H1 H2.
rewrite <- H1 in H2.
apply length_bfrontier_ge.
replace (i + 1 - 1) with i.
- exact H2.
- omega.
Qed.
Lemma length_ge_v2:
forall t: btree,
forall s: list terminal,
forall i: nat,
i >= 1 ->
bfrontier t = s ->
length s >= 2 ^ (i - 1) + 1 ->
bheight t >= (i + 1).
Proof.
intros t s i H1 H2.
assert (H3: bheight t <= i -> length (bfrontier t) <= 2 ^ (i - 1)).
{
apply bheight_le.
}
intros H4.
apply contrap in H3.
- omega.
- intros H5.
rewrite <- H2 in H4.
omega.
Qed.
Lemma length_gt:
forall t: btree,
forall s: sentence,
forall i: nat,
bfrontier t = s ->
length s > 2 ^ i ->
bheight t > (i + 1).
Proof.
intros t s i H1 H2.
rewrite <- H1 in H2.
apply length_bfrontier_gt.
replace (i + 1 - 1) with i.
- exact H2.
- omega.
Qed.
Lemma bpath_length_gt_0:
forall t: btree,
forall p: sf,
bpath t p -> length p > 0.
Proof.
intros t p H.
destruct t.
- inversion H.
+ simpl.
omega.
+ simpl.
omega.
+ simpl.
omega.
- inversion H.
+ simpl.
omega.
+ simpl.
omega.
+ simpl.
omega.
Qed.
Lemma bpath_bheight_exists:
forall t: btree,
exists p: sf,
bpath t p /\
length p = bheight t + 1.
Proof.
induction t.
- simpl.
exists [inl n; inr t].
split.
+ apply bp_1.
reflexivity.
+ simpl.
reflexivity.
- destruct IHt1 as [p1 [H1]].
destruct IHt2 as [p2 [H2]].
assert (H3: bheight t1 >= bheight t2 \/ bheight t1 <= bheight t2) by omega.
destruct H3 as [H3 | H3].
+ exists ((inl n) :: p1).
split.
* {
apply bp_l with (bt1:= t1) (bt2:= t2).
- reflexivity.
- exact H1.
}
* simpl.
apply max_l in H3.
rewrite H3.
rewrite H.
reflexivity.
+ exists ((inl n) :: p2).
split.
* {
apply bp_r with (bt1:= t1) (bt2:= t2).
- reflexivity.
- exact H2.
}
* simpl.
apply max_r in H3.
rewrite H3.
rewrite H0.
reflexivity.
Qed.
Lemma bpath_last_terminal:
forall t: btree,
forall p: sf,
bpath t p ->
exists p': sf,
exists t: terminal,
p = p' ++ [inr t].
Proof.
induction t.
- intros p H.
inversion H.
+ subst.
exists [inl n0].
exists t0.
reflexivity.
+ discriminate.
+ discriminate.
- intros p H.
inversion H.
+ discriminate.
+ inversion H0.
subst.
specialize (IHt1 p1 H1).
destruct IHt1 as [p' [t IHt1]].
rewrite IHt1.
exists (inl n0 :: p').
exists t.
reflexivity.
+ inversion H0.
subst.
specialize (IHt2 p2 H1).
destruct IHt2 as [p' [t IHt2]].
rewrite IHt2.
exists (inl n0 :: p').
exists t.
reflexivity.
Qed.
Lemma bpath_in_ntl:
forall bt: btree,
forall ntl: list non_terminal,
forall p: sf,
forall t: terminal,
bnts bt ntl ->
bpath bt (p ++ [inr t]) ->
(forall s: symbol, In s p -> In s (map inl ntl)).
Proof.
induction bt.
- intros ntl p t0 H1 H2 s H3.
apply in_split in H3.
destruct H3 as [l1 [l2 H3]].
rewrite H3 in H2.
clear H3.
inversion H2.
+ destruct l1.
* inversion H.
{
destruct s.
- inversion H4.
subst.
inversion H0.
subst.
inversion H1.
+ inversion H3.
subst.
apply in_map.
exact H6.
+ discriminate.
- discriminate.
}
* inversion H.
{
destruct l1.
- inversion H5.
destruct l2.
+ inversion H7.
+ inversion H7.
- inversion H5.
destruct l1.
+ inversion H7.
+ inversion H7.
}
+ discriminate.
+ discriminate.
- intros ntl p t H1 H2 s H3.
apply in_split in H3.
destruct H3 as [l1 [l2 H3]].
rewrite H3 in H2.
clear H3.
inversion H2.
+ discriminate.
+ clear H2.
inversion H0.
subst.
clear H0.
destruct l1.
* inversion H.
{
inversion H1.
- discriminate.
- inversion H0.
subst.
apply in_map.
exact H5.
}
* inversion H.
rewrite H4 in H3.
{
inversion H1.
- discriminate.
- inversion H0.
subst.
specialize (IHbt1 ntl (l1 ++ s :: l2) t H6 H3).
apply IHbt1.
apply in_or_app.
right.
simpl.
left.
reflexivity.
}
+ clear H2.
inversion H0.
subst.
clear H0.
destruct l1.
* inversion H.
{
inversion H1.
- discriminate.
- inversion H0.
subst.
apply in_map.
exact H5.
}
* inversion H.
rewrite H4 in H3.
{
inversion H1.
- discriminate.
- inversion H0.
subst.
specialize (IHbt2 ntl (l1 ++ s :: l2) t H7 H3).
apply IHbt2.
apply in_or_app.
right.
simpl.
left.
reflexivity.
}
Qed.
Lemma btree_exists_bpath:
forall bt: btree,
forall ntl: list non_terminal,
bheight bt >= length ntl + 1 ->
bnts bt ntl ->
exists z: sf,
bpath bt z /\
length z = bheight bt + 1 /\
exists u r: sf,
exists t: terminal,
z = u ++ r ++ [inr t] /\
length u >= 0 /\
length r = length ntl + 1 /\
(forall s: symbol, In s (u ++ r) -> In s (map inl ntl)).
Proof.
intros bt ntl H1 H2.
assert (H3: exists p: sf, bpath bt p /\ length p = bheight bt + 1).
{
apply bpath_bheight_exists.
}
destruct H3 as [p [H3 H4]].
exists p.
split.
- exact H3.
- split.
+ exact H4.
+ assert (H3':= H3).
apply bpath_last_terminal in H3.
destruct H3 as [p' [t H3]].
apply ge_exists in H1.
destruct H1 as [z H1].
rewrite H1 in H4.
clear H1.
replace (z + length ntl + 1 + 1) with (z + (length ntl + 1) + 1) in H4.
* rewrite H3 in H4.
rewrite app_length in H4.
simpl in H4.
assert (H5: length p' = z + (length ntl + 1)) by omega.
clear H4.
apply list_split in H5.
destruct H5 as [l1 [l2 [H5 [H6 H7]]]].
exists l1, l2, t.
{
split.
- rewrite H5 in H3.
rewrite <- app_assoc in H3.
exact H3.
- split.
+ apply length_ge_0.
+ split.
* exact H7.
* rewrite H5 in H3.
rewrite H3 in H3'.
clear H3 H5 H6 H7.
{
apply bpath_in_ntl with (bt:= bt) (t:= t).
- exact H2.
- exact H3'.
}
}
* rewrite plus_assoc.
reflexivity.
Qed.
Lemma bpath_bheight_ge:
forall t: btree,
forall p: sf,
bpath t p ->
bheight t >= length p - 1.
Proof.
induction t.
- intros p H.
inversion H.
+ simpl.
omega.
+ discriminate.
+ discriminate.
- intros p H.
inversion H.
+ discriminate.
+ inversion H0.
subst.
simpl.
specialize (IHt1 p1 H1).
assert (H2: bheight bt1 >= bheight bt2 \/ bheight bt1 <= bheight bt2) by omega.
destruct H2 as [H2 | H2].
* apply max_l in H2.
rewrite H2.
omega.
* apply max_r in H2.
rewrite H2.
apply Nat.max_r_iff in H2.
omega.
+ inversion H0.
subst.
simpl.
specialize (IHt2 p2 H1).
assert (H2: bheight bt1 >= bheight bt2 \/ bheight bt1 <= bheight bt2) by omega.
destruct H2 as [H2 | H2].
* apply max_l in H2.
rewrite H2.