-
Notifications
You must be signed in to change notification settings - Fork 7
/
huffman.v
1728 lines (1584 loc) · 65.9 KB
/
huffman.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
From Equations Require Import Equations.
From Coq Require Import ssreflect ssrbool ssrfun.
From mathcomp Require Import eqtype ssrnat seq bigop ssrAC.
From favssr Require Import prelude.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Section TheImplementation.
Context {A : Type}.
(* TODO most of forest functions (fooF) here can probably be rewritten as maps/folds *)
Inductive tree A := Leaf of nat & A | Node of nat & tree A & tree A.
Definition forest A := seq (tree A).
Definition cachedWeight (t : tree A) : nat :=
match t with
| Leaf w _ => w
| Node w _ _ => w
end.
Definition uniteTrees (l r : tree A) : tree A :=
Node (cachedWeight l + cachedWeight r) l r.
Fixpoint insortTree (u : tree A) (ts : forest A) : forest A :=
if ts is t::s then
if cachedWeight u <= cachedWeight t
then u :: t :: s
else t :: insortTree u s
else [::u].
Lemma size_insortTree u ts : size (insortTree u ts) = (size ts).+1.
Proof. by elim: ts=>//=t s IH; case: ifP=>//= _; rewrite IH. Qed.
Equations? huffman (t0 : tree A) (ts : forest A) : tree A by wf (size ts) lt :=
huffman t0 [::] => t0;
huffman _ [::t] => t;
huffman t0 (t1::t2::ts) => huffman t0 (insortTree (uniteTrees t1 t2) ts).
Proof. by apply: ssrnat.ltP; rewrite size_insortTree. Qed.
End TheImplementation.
Lemma insortTree_notnil {A} (p : tree A) ts:
~~ nilp (insortTree p ts).
Proof. by case: ts=>//=t s; case: ifP. Qed.
Section EqTree.
Context {T : eqType}.
Fixpoint eqtree (t1 t2 : tree T) :=
match t1, t2 with
| Leaf w1 x1, Leaf w2 x2 => (w1 == w2) && (x1 == x2)
| Node w1 l1 r1, Node w2 l2 r2 => [&& w1 == w2, eqtree l1 l2 & eqtree r1 r2]
| _, _ => false
end.
Lemma eqtreeP : Equality.axiom eqtree.
Proof.
move; elim=>[w1 x1|w1 l1 IHl r1 IHr][w2 x2|w2 l2 r2] /=; try by constructor.
apply: (iffP andP).
- by case=>/eqP->/eqP->.
by case=>->->.
have [<-/=|neqx] := w1 =P w2; last by apply: ReflectF; case.
- apply: (iffP andP).
- by case=>/IHl->/IHr->.
by case=><-<-; split; [apply/IHl|apply/IHr].
Qed.
Canonical tree_eqMixin := EqMixin eqtreeP.
Canonical tree_eqType := Eval hnf in EqType (tree T) tree_eqMixin.
End EqTree.
Section BasicAuxiliaryFunctions.
Context {A : eqType}.
(* alphabets & intersection *)
Fixpoint alphabet (t : tree A) : pred A :=
match t with
| Leaf _ a => pred1 a
| Node _ l r => [predU (alphabet l) & (alphabet r)]
end.
Lemma exists_in_alphabet t :
exists a, a \in alphabet t.
Proof.
elim: t=>/=[_ c|_ l IHl r _].
- by exists c; exact: eq_refl.
by case: IHl=>a Ha; exists a; rewrite !inE Ha.
Qed.
Fixpoint alphabetF (ts : forest A) : pred A :=
if ts is t::s then [predU (alphabet t) & (alphabetF s)]
else pred0.
Fixpoint enum (t : tree A) : seq A :=
match t with
| Leaf _ a => [::a]
| Node _ l r => enum l ++ enum r
end.
Fixpoint enumF (ts : forest A) : seq A :=
if ts is t::s then enum t ++ enumF s
else [::].
Lemma alphabet_enum t : alphabet t =i enum t.
Proof.
elim: t=>[w a|w l IHl r IHr] /= z.
- by rewrite in_cons in_nil orbF.
by rewrite !inE mem_cat (IHl z) (IHr z).
Qed.
Lemma alphabet_enumF t : alphabetF t =i enumF t.
Proof.
elim: t=>//=t s IH z.
by rewrite !inE mem_cat alphabet_enum IH.
Qed.
Lemma alphabetF_insortTree (p : tree A) ts :
alphabetF (insortTree p ts) =i [predU (alphabet p) & (alphabetF ts)].
Proof.
elim: ts=>//=t s IH; case: ifP=>//= _ x.
by rewrite !inE IH !inE orbCA.
Qed.
Definition alpha_intersects p q := has (alphabet p) (enum q).
Definition alpha_intersectsF p qs := has (alphabet p) (enumF qs).
Lemma intersectsC p q : alpha_intersects p q = alpha_intersects q p.
Proof.
rewrite /alpha_intersects; elim: p=>/= _.
- by move=>a; rewrite has_pred1 orbF -(alphabet_enum q).
by move=>l IHl r IHr; rewrite has_predU has_cat IHl IHr.
Qed.
Lemma alpha_not p q :
~~ alpha_intersects p q <->
(forall x, x \in alphabet p -> x \notin alphabet q).
Proof.
split.
- move=>Hi x Hp; apply/negP; rewrite alphabet_enum=>Hq.
by move/hasPn: Hi=>/(_ x Hq); move: Hp; rewrite -topredE /==>->.
move=>H; rewrite intersectsC /alpha_intersects; apply/hasPn=>z; rewrite -alphabet_enum.
by move/H; rewrite -topredE /=.
Qed.
Lemma alpha_notF p q :
~~ alpha_intersectsF p q <->
(forall x, x \in alphabet p -> x \notin alphabetF q).
Proof.
split.
- move=>Hi x Hp; apply/negP; rewrite alphabet_enumF=>Hq.
by move/hasPn: Hi =>/(_ x Hq); move: Hp; rewrite -topredE /==>->.
move=>H; rewrite /alpha_intersectsF; apply/hasPn=>z; rewrite -alphabet_enumF=>Hq.
by apply/negP=>/H; rewrite Hq.
Qed.
Lemma alpha_notFC p q :
~~ alpha_intersectsF p q <->
(forall x, x \in alphabetF q -> x \notin alphabet p).
Proof.
split.
- by move=>Hi x; apply/contraL; move: x; apply/alpha_notF.
move=>H; rewrite /alpha_intersectsF; apply/hasPn=>z; rewrite -alphabet_enumF.
by apply: H.
Qed.
Lemma not_intersects p q x :
~~ alpha_intersects p q ->
[\/ (x \in alphabet p) /\ (x \notin alphabet q)
, (x \notin alphabet p) /\ (x \in alphabet q)
| (x \notin alphabet p) /\ (x \notin alphabet q)
].
Proof.
move=>Hi.
case/boolP: (x \in alphabet p)=>Hp; case/boolP: (x \in alphabet q)=>Hq /=; try by constructor.
by move/alpha_not: Hi=>/(_ _ Hp); rewrite Hq.
Qed.
Lemma alpha_intersectsF_cons (p : tree A) q ts :
alpha_intersectsF p (q::ts) = alpha_intersects p q || alpha_intersectsF p ts.
Proof. by rewrite /alpha_intersects /alpha_intersectsF /= has_cat. Qed.
Lemma alpha_intersectsF_uniteTrees (p : tree A) q ts :
alpha_intersectsF (uniteTrees p q) ts = alpha_intersectsF p ts || alpha_intersectsF q ts.
Proof. by rewrite /alpha_intersectsF /uniteTrees /= has_predU. Qed.
Lemma intersectsF_insortTree (p : tree A) q ts :
alpha_intersectsF p (insortTree q ts) = alpha_intersects p q || alpha_intersectsF p ts.
Proof.
rewrite /alpha_intersects /alpha_intersectsF; elim: ts=>/=.
- by rewrite cats0 orbF.
move=>t s IH; case: ifP=>/= _; rewrite !has_cat //.
by rewrite IH orbCA.
Qed.
(* consistency *)
Fixpoint consistent (t : tree A) : bool :=
if t is Node _ l r
then [&& ~~ alpha_intersects l r,
consistent l &
consistent r]
else true.
Fixpoint consistentF (ts : forest A) : bool :=
if ts is t::s
then [&& ~~ alpha_intersectsF t s,
consistent t &
consistentF s]
else true.
Lemma consistent_ind (P : tree A -> A -> Prop) t a:
consistent t ->
(forall w b a, P (Leaf w b) a) ->
(forall w l r a,
consistent l -> consistent r ->
~~ alpha_intersects l r ->
a \in alphabet l -> a \notin alphabet r ->
P l a -> P r a ->
P (Node w l r) a) ->
(forall w l r a,
consistent l -> consistent r ->
~~ alpha_intersects l r ->
a \notin alphabet l -> a \in alphabet r ->
P l a -> P r a ->
P (Node w l r) a) ->
(forall w l r a,
consistent l -> consistent r ->
~~ alpha_intersects l r ->
a \notin alphabet l -> a \notin alphabet r ->
P l a -> P r a ->
P (Node w l r) a) ->
P t a.
Proof.
move=>Hc H0 Hl Hr Hb; elim: t Hc=>[w b|w l IHl r IHr] /=.
- by move=>_; apply: H0.
case/and3P=>Hi Hcl Hcr.
case: (not_intersects a Hi); case=>Hil Hir.
- by apply: Hl=>//; [apply: IHl|apply: IHr].
- by apply: Hr=>//; [apply: IHl|apply: IHr].
by apply: Hb=>//; [apply: IHl|apply: IHr].
Qed.
Lemma consistentF_insortTree (p : tree A) ts :
consistentF (insortTree p ts) = consistentF (p::ts).
Proof.
elim: ts=>//=t s IH; case: ifP=>//= _.
rewrite intersectsF_insortTree IH alpha_intersectsF_cons !negb_or intersectsC.
by rewrite !andbA (ACl (1*4*5*2*3*6)%AC).
Qed.
(* tree metrics *)
Fixpoint depth (t : tree A) (a : A) : nat :=
if t is Node _ l r then
if a \in alphabet l
then depth l a + 1
else if a \in alphabet r
then depth r a + 1
else 0
else 0.
Fixpoint height (t : tree A) : nat :=
if t is Node _ l r
then maxn (height l) (height r) + 1
else 0.
Fixpoint heightF (ts : forest A) : nat :=
if ts is t::s
then maxn (height t) (heightF s)
else 0.
Lemma heightE (t : tree A) : reflect (exists w x, t = Leaf w x) (height t == 0).
Proof.
apply: (iffP idP); last by case=>w[x]->.
case: t=>[w x|w l r] /=; last by rewrite addn1.
by move=>_; exists w, x.
Qed.
Lemma depth_le_height t a :
depth t a <= height t.
Proof.
elim: t=>//=_ l IHl r IHr; case: ifP=>_.
- by rewrite leq_add2r leq_max IHl.
case: ifP=>_.
- by rewrite leq_add2r leq_max IHr orbT.
by rewrite addn1.
Qed.
Lemma exists_at_height t :
consistent t -> exists2 a, a \in alphabet t & depth t a = height t.
Proof.
elim: t=>/=[_ a|_ l IHl r IHr].
- move=>_; exists a=>//; apply: eq_refl.
case/and3P=>Hi Hl Hr.
case: (IHl Hl)=>al Hal Hdl.
case: (IHr Hr)=>ar Har Hdr.
case: (leqP (height r) (height l))=>H.
- exists al; rewrite ?inE Hal //.
by rewrite Hdl.
exists ar; rewrite ?inE Har; first by rewrite orbT.
move: Hi; rewrite intersectsC=>/alpha_not/(_ _ Har)/negbTE->.
by rewrite Hdr.
Qed.
Lemma height_gt0_alphabet_eq t u :
0 < height t -> consistent t -> alphabet t =i alphabet u ->
0 < height u.
Proof.
case: t=>//=_ l r _; case/and3P=>Hi Hl Hr.
case: (exists_in_alphabet l)=>b Hb.
case: (exists_in_alphabet r)=>c Hc.
case/boolP: (b==c).
- move/eqP=>E; rewrite -E in Hc.
by move/alpha_not: Hi=>/(_ _ Hb); rewrite Hc.
move=>Hbc; case: u=>/=[_ a|_ lu ru]; last by rewrite addn1.
move=>H; move: (H b) (H c); rewrite !inE Hb Hc /= orbT => Hba Hca.
symmetry in Hba; move/eqP: Hba=>Hba; rewrite -Hba in Hca.
by symmetry in Hca; move/eqP: Hca=>Hca; rewrite Hca eq_refl in Hbc.
Qed.
Lemma heightF_insortTree (p : tree A) ts :
heightF (insortTree p ts) = maxn (height p) (heightF ts).
Proof.
elim: ts=>//= t s IH; case: ifP=>//= _.
by rewrite IH maxnCA.
Qed.
Fixpoint freq (t : tree A) (b : A) : nat :=
match t with
| Leaf w a => if b == a then w else 0
| Node _ l r => freq l b + freq r b
end.
Fixpoint freqF (ts : forest A) (b : A) : nat :=
if ts is t::s
then freq t b + freqF s b
else 0.
Lemma notin_alphabet_freq_0 a t :
a \notin alphabet t -> freq t a = 0.
Proof.
elim: t=>[w b|_ l IHl r IHr] /=; rewrite !inE.
- by move/negbTE=>->.
by rewrite negb_or; case/andP=>/IHl->/IHr->.
Qed.
Lemma notin_alphabet_freqF_0 a ts :
a \notin alphabetF ts -> freqF ts a = 0.
Proof.
elim: ts=>//=t s IH; rewrite !inE negb_or.
by case/andP=>/notin_alphabet_freq_0->/IH->.
Qed.
Lemma heightF_0_Leaf_freqF ts a :
consistentF ts -> heightF ts = 0 -> a \in alphabetF ts ->
Leaf (freqF ts a) a \in ts.
Proof.
elim: ts=>//=t s IH; case/and3P=>Hi Hct Hcs.
move/eqP; rewrite maxn_eq0; case/andP=>/heightE [w][x] Et /= Hs.
rewrite Et /= in Hi *.
rewrite !inE; case: eqP=>/= [Ea|_].
- move/alpha_notF: Hi=>/=; rewrite -Ea.
move=>/(_ a); rewrite inE eq_refl=>/(_ erefl) Has _.
by rewrite notin_alphabet_freqF_0 // addn0 eq_refl.
rewrite add0n=>Has; apply/orP; right; apply: IH=>//.
by apply/eqP.
Qed.
Lemma freqF_insortTree (p : tree A) ts a :
freqF (insortTree p ts) a = freq p a + freqF ts a.
Proof.
elim: ts=>//=t s IH; case: ifP=>//= _.
by rewrite IH addnCA.
Qed.
Fixpoint weight (t : tree A) : nat :=
match t with
| Leaf w _ => w
| Node _ l r => weight l + weight r
end.
Lemma height_0_cachedWeight t:
height t = 0 -> cachedWeight t = weight t.
Proof. by case: t=>//=n l r; rewrite addn1. Qed.
Fixpoint cost (t : tree A) : nat :=
if t is Node _ l r
then weight l + cost l + weight r + cost r
else 0.
(* optimality *)
Definition optimum (t : tree A) : Prop :=
forall u, consistent u ->
alphabet t =i alphabet u ->
freq t =1 freq u ->
cost t <= cost u.
(* siblings *)
(* weird that equations can't see t is decreasing *)
Equations? sibling (t : tree A) (a : A) : A by wf (height t) lt :=
sibling (Leaf _ _) a => a;
sibling (Node _ (Leaf _ b) (Leaf _ c)) a =>
if a == b then c else if a == c then b else a;
sibling (Node _ l r) a =>
if a \in alphabet l then sibling l a
else if a \in alphabet r then sibling r a
else a.
Proof.
all: apply: ssrnat.ltP.
- by rewrite addn1.
- by rewrite ltn_add2r max0n addn1.
- by rewrite ltn_add2r leq_max addn1 ltnS leqnn.
by rewrite addn1 ltnS leq_max leqnn orbT.
Qed.
Lemma in_alphabet_sibling t a:
a \in alphabet t -> sibling t a \in alphabet t.
Proof.
funelim (sibling t a)=>//; simp sibling=>/=; rewrite !inE.
- case/orP=>->; first by rewrite eq_refl orbT.
by case: ifP=>_; rewrite eq_refl // orbT.
- case/or3P=>E; rewrite !E //= ?orbT.
- case: ifP; first by move=>->.
move=>_; apply/orP; right.
by move: H0=>/=; rewrite !inE; apply; rewrite E.
case: ifP; first by move=>->.
move=>_; apply/orP; right.
by move: H0=>/=; rewrite !inE; apply; rewrite E orbT.
rewrite -orbA; case/or3P=>E; rewrite E /= ?orbT.
- apply/orP; left.
by move: H=>/=; rewrite !inE; apply; rewrite E.
- apply/orP; left.
by move: H=>/=; rewrite !inE; apply; rewrite E orbT.
case: ifP.
- move=>Hlr; apply/orP; left.
by move: H=>/=; rewrite !inE; apply.
by move=>_; rewrite (H0 E) orbT.
Qed.
Lemma notin_alphabet_sibling_id a t :
a \notin alphabet t -> sibling t a = a.
Proof.
funelim (sibling t a)=>//; simp sibling=>/=; rewrite !inE !negb_or -?andbA.
- by case/andP=>/negbTE->/negbTE->.
- by case/and3P=>/negbTE->/negbTE->/negbTE->.
by case/and3P=>/negbTE->/negbTE->/negbTE->.
Qed.
Corollary sibling_ne_in_alphabet t a :
sibling t a != a -> sibling t a \in alphabet t.
Proof.
move=>H; apply: in_alphabet_sibling; move: H.
by apply: contraR=>H; apply/eqP/notin_alphabet_sibling_id.
Qed.
Lemma height_gt0_sibling w p q a :
(0 < height p) || (0 < height q) ->
sibling (Node w p q) a =
if a \in alphabet p then sibling p a else sibling q a.
Proof.
case: q=>[wq q|wq ql qr]; case: p=>[wp p|wp pl pr]; simp sibling=>/=.
- by rewrite ltnn.
- by move=>_; rewrite !inE if_same.
- move=>_; rewrite !inE; case: ifP=>//_; case: ifP=>//.
by move/negbT=>H; rewrite notin_alphabet_sibling_id.
move=>_; rewrite !inE; case: ifP=>//_; case: ifP=>//.
by move/negbT=>H; rewrite notin_alphabet_sibling_id.
Qed.
Lemma sibling_consistent_ind (P : tree A -> A -> Prop) t a :
consistent t ->
(forall w b a, P (Leaf w b) a) ->
(forall w wb b wc c a,
b != c -> P (Node w (Leaf wb b) (Leaf wc c)) a) ->
(forall w l r a,
consistent l -> consistent r ->
~~ alpha_intersects l r ->
(0 < height l) || (0 < height r) ->
a \in alphabet l -> sibling l a \in alphabet l ->
a \notin alphabet r -> sibling l a \notin alphabet r ->
P l a ->
P (Node w l r) a) ->
(forall w l r a,
consistent l -> consistent r ->
~~ alpha_intersects l r ->
(0 < height l) || (0 < height r) ->
a \notin alphabet l -> sibling r a \notin alphabet l ->
a \in alphabet r -> sibling r a \in alphabet r ->
P r a ->
P (Node w l r) a) ->
(forall w l r a,
consistent l -> consistent r ->
~~ alpha_intersects l r ->
(0 < height l) || (0 < height r) ->
a \notin alphabet l -> a \notin alphabet r ->
P (Node w l r) a) ->
P t a.
Proof.
move=>Hc H0 Hn Hl Hr Hb; elim: t Hc=>[w b|w l IHl r IHr] /=.
- by move=>_; apply: H0.
case/and3P=>Hi Hcl Hcr.
case: (posnP (height l + height r)).
- move/eqP; rewrite addn_eq0; case/andP.
case/heightE=>[wb][b] El; case/heightE=>[wc][c] Er.
move: Hi; rewrite El Er /alpha_intersects /= orbF eq_sym.
by apply: Hn.
rewrite addn_gt0=>Hh.
case: (not_intersects a Hi); case=>Hil Hir.
- have Hsl : sibling l a \in alphabet l by apply: in_alphabet_sibling.
by apply: Hl=>//; [move/alpha_not: Hi; apply |apply: IHl].
- have Hsr : sibling r a \in alphabet r by apply: in_alphabet_sibling.
apply: Hr=>//; last by apply: IHr.
by move: Hi; rewrite intersectsC=>/alpha_not; apply.
by apply: Hb.
Qed.
Lemma sibling_sibling_id t a :
consistent t -> sibling t (sibling t a) = a.
Proof.
move=>H.
apply: (sibling_consistent_ind (P:=fun t a => sibling t (sibling t a) = a))=>{a}//=.
- move=>w wb b wc c a /negbTE Hbc; simp sibling.
case/boolP: (a==b)=>[/eqP->|/negbTE Hab]; first by rewrite eq_sym Hbc eq_refl.
case/boolP: (a==c)=>[/eqP->|/negbTE Hac]; first by rewrite eq_refl.
by rewrite Hab Hac.
- move=>w l r a _ _ _ Hh Hal Hsal _ _ IH.
by rewrite height_gt0_sibling // height_gt0_sibling // Hal Hsal.
- move=>w l r a _ _ _ Hh /negbTE Hal /negbTE Hsal Har Hsar IH.
by rewrite height_gt0_sibling // height_gt0_sibling // Hal Hsal.
move=>w l r a _ _ _ Hh Hal Har.
by rewrite !notin_alphabet_sibling_id //= !inE negb_or Hal Har.
Qed.
Corollary sibling_reciprocal t a b :
consistent t -> sibling t a = b -> sibling t b = a.
Proof. by move=>H <-; apply: sibling_sibling_id. Qed.
Lemma depth_height_sibling_ne t a :
consistent t -> depth t a = height t -> 0 < height t ->
a \in alphabet t ->
sibling t a != a.
Proof.
move=>H.
apply: (sibling_consistent_ind (P:=fun t a => depth t a = height t -> 0 < height t -> a \in alphabet t -> sibling t a != a))=>{a}//=.
- move=>w wb b wc c a Hbc.
rewrite !inE maxn0 add0n; simp sibling; case: ifP=>/=.
- by move/eqP=>-> _ _ _; rewrite eq_sym.
by move=>_; case: ifP=>// /eqP->.
- move=>w l r a _ _ _ Hh Hal _ _ _.
rewrite height_gt0_sibling // Hal =>IH /eqP; rewrite eqn_add2r=>E _ _.
apply: IH=>//.
- case: (leqP (height r) (height l)) E; first by move=>_/eqP.
by move=>Hlr /eqP Hd; move: (depth_le_height l a); rewrite Hd leqNgt Hlr.
case: (posnP (height l))=>// El; rewrite El ltnn /= in Hh.
case/eqP/heightE: El=>[wl][xl]El; rewrite El /= max0n eq_sym in E.
by move: Hh; move/eqP: E=>->.
- move=>w l r a _ _ _ Hh /negbTE Hal _ Har _.
rewrite height_gt0_sibling // Hal Har =>IH /eqP; rewrite eqn_add2r=>E _ _.
apply: IH=>//.
- case: (leqP (height l) (height r)) E; first by move=>_/eqP.
by move=>Hrl /eqP Hd; move: (depth_le_height r a); rewrite Hd leqNgt Hrl.
case: (posnP (height r))=>// Er; rewrite Er ltnn /= orbF in Hh.
case/eqP/heightE: Er=>[wr][xr]Er; rewrite Er /= maxn0 eq_sym in E.
by move: Hh; move/eqP: E=>->.
by move=>w l r a _ _ _ _ /negbTE Hal /negbTE Har _ _; rewrite !inE Hal Har.
Qed.
Lemma depth_sibling t a :
consistent t -> depth t (sibling t a) = depth t a.
Proof.
move=>H.
apply: (sibling_consistent_ind (P:=fun t a => depth t (sibling t a) = depth t a))=>{a}//=.
- move=>w wb b wc c a /negbTE Hbc; simp sibling; rewrite !inE.
case/boolP: (a==b)=>[_|/negbTE Hab].
- by rewrite eq_sym Hbc eq_refl.
case/boolP: (a==c); first by rewrite eq_refl.
by rewrite Hab =>/negbTE->.
- move=>w l r a _ _ _ Hh Hal Hsal _ _ IH.
by rewrite height_gt0_sibling // Hal Hsal IH.
- move=>w l r a _ _ _ Hh /negbTE Hal /negbTE Hsal Har Hsar IH.
by rewrite height_gt0_sibling // Hal Hsal Har Hsar IH.
move=>w l r a _ _ _ Hh Hal Har.
rewrite notin_alphabet_sibling_id; last by rewrite /= !inE negb_or Hal Har.
by rewrite (negbTE Hal) (negbTE Har).
Qed.
End BasicAuxiliaryFunctions.
Arguments consistent_ind [A P].
Section OtherFunctions.
Context {A : eqType}.
(* swapping *)
Fixpoint swapLeaves (t : tree A) (wa : nat) (a : A) (wb : nat) (b : A) : tree A :=
match t with
| Leaf wc c =>
if c == a then Leaf wb b else
if c == b then Leaf wa a else
Leaf wc c
| Node w l r =>
Node w (swapLeaves l wa a wb b) (swapLeaves r wa a wb b)
end.
Lemma swapLeaves_id_notin_alphabet t w v a :
a \notin alphabet t -> swapLeaves t w a v a = t.
Proof.
elim: t=>[n b|n l IHl r IHr] /=; rewrite !inE.
- by rewrite eq_sym=>/negbTE->.
by rewrite negb_or; case/andP=>/IHl->/IHr->.
Qed.
Lemma swapLeaves_id t a :
consistent t -> swapLeaves t (freq t a) a (freq t a) a = t.
Proof.
(* TODO make nicer? *)
move=>H.
apply: (consistent_ind (P:=fun t a => swapLeaves t (freq t a) a (freq t a) a = t))=>{a}//=.
- by move=>w b a; case: eqP=>//->; rewrite eq_refl.
- move=>w l r a Hcl Hcr Hi Hal Har IHl IHr.
by rewrite (notin_alphabet_freq_0 Har) addn0 IHl swapLeaves_id_notin_alphabet.
- move=>w l r a Hcl Hcr Hi Hal Har IHl IHr.
by rewrite (notin_alphabet_freq_0 Hal) add0n IHr swapLeaves_id_notin_alphabet.
move=>w l r a Hcl Hcr Hi Hal Har IHl IHr.
by rewrite (notin_alphabet_freq_0 Hal) (notin_alphabet_freq_0 Har) addn0
swapLeaves_id_notin_alphabet // swapLeaves_id_notin_alphabet.
Qed.
Lemma alphabet_swapLeaves t wa a wb b :
alphabet (swapLeaves t wa a wb b) =i
if a \in alphabet t then
if b \in alphabet t
then alphabet t
else [predU1 b & [predD1 (alphabet t) & a]]
else
if b \in alphabet t
then [predU1 a & [predD1 (alphabet t) & b]]
else SimplPred (alphabet t).
Proof.
move=>x; elim: t=>/=[w c|w l IHl r IHr]; rewrite !inE.
- case: ifP.
- move/eqP=><-; rewrite eq_refl /= inE.
case: ifP; first by move/eqP=>->.
by move=>_; rewrite !inE andNb orbF.
rewrite eq_sym=>->; case: ifP.
- by move/eqP=>->; rewrite eq_refl /= !inE andNb orbF.
by rewrite eq_sym=>->/=; rewrite inE.
rewrite {}IHl {}IHr.
case/boolP: (a \in alphabet l)=>Hal; case/boolP: (a \in alphabet r)=>Har;
case/boolP: (b \in alphabet l)=>Hbl; case/boolP: (b \in alphabet r)=>Hbr;
rewrite /= !inE //;
case: eqP=>[->|_]; case: eqP=>[->|_] //=;
rewrite ?orbT ?orbF ?Hal ?Hbl ?Har ?Hbr ?orbT //.
- by move/negbTE: Har; rewrite -topredE.
- by move/negbTE: Hal; rewrite -topredE.
- by move/negbTE: Hbr; rewrite -topredE.
by move/negbTE: Hbl; rewrite -topredE.
Qed.
Lemma consistent_swapLeaves t wa a wb b :
consistent t -> consistent (swapLeaves t wa a wb b).
Proof.
elim: t=>[w c|w l IHl r IHr]/=.
- by move=>_; case: ifP=>//_; case: ifP.
case/and3P=>Hi Hl Hr; rewrite IHl // IHr //= andbT.
apply/alpha_not=>z; rewrite !alphabet_swapLeaves.
case: (not_intersects a Hi)=>[[ Hail /negbTE Hair]
|[/negbTE Hail Hair]
|[/negbTE Hail /negbTE Hair]];
case: (not_intersects b Hi)=>[[ Hbil /negbTE Hbir]
|[/negbTE Hbil Hbir]
|[/negbTE Hbil /negbTE Hbir]];
rewrite Hail Hair Hbil Hbir ?inE //.
- by move/alpha_not: Hi; apply.
- case: eqP=>[->|_] /=; rewrite negb_or.
- by rewrite andbT=>_; apply/eqP=>Hba; rewrite Hba Hail in Hbil.
by case: eqP=>//=_; move/alpha_not: (Hi); apply.
- case: eqP=>[->|_] /=; first by move=>_; move: Hbir; rewrite -topredE /= =>->.
by case/andP=>_; move/alpha_not: (Hi); apply.
- case: eqP=>[->|_] /=; rewrite negb_or.
- by rewrite andbT=>_; apply/eqP=>Hab; rewrite Hab Hbil in Hail.
by case: eqP=>//=_; move/alpha_not: (Hi); apply.
- by move/alpha_not: Hi; apply.
- rewrite negb_or; case: eqP=>[->|_] /=.
- by move: Hbil; rewrite -topredE /= =>->.
by case: eqP=>[->|_] //=; move/alpha_not: Hi; apply.
- case: eqP=>[->|_] /=; first by move=>_; move: Hair; rewrite -topredE /= =>->.
by case: eqP=>//=_; move/alpha_not: (Hi); apply.
- rewrite negb_or; case: eqP=>[->|_] /=.
- by move: Hail=>/[swap]; rewrite -topredE /= =>->.
by case: eqP=>[->|_] //=; move/alpha_not: Hi; apply.
by move/alpha_not: Hi; apply.
Qed.
Lemma depth_swapLeaves t wa a wb b c :
consistent t -> c != a -> c != b ->
depth (swapLeaves t wa a wb b) c = depth t c.
Proof.
move=>H.
apply: (consistent_ind (P:=fun t c => c != a -> c != b -> depth (swapLeaves t wa a wb b) c = depth t c))=>{c}//=.
- by move=>w d c _ _; case: ifP=>//=_; case: ifP.
- move=>_ l r c _ _ Hi Hcl _ IHl IHr Hca Hcb.
rewrite !alphabet_swapLeaves Hcl IHl // IHr //.
case: (not_intersects a Hi)=>[[ Hail /negbTE Hair]
|[/negbTE Hail Hair]
|[/negbTE Hail /negbTE Hair]];
case: (not_intersects b Hi)=>[[ Hbil /negbTE Hbir]
|[/negbTE Hbil Hbir]
|[/negbTE Hbil /negbTE Hbir]];
rewrite Hail Hair Hbil Hbir ?inE ?Hcl ?Hca ?Hcb /= ?orbT //.
- by move: Hcl; rewrite -topredE /==>->.
- by move: Hcl; rewrite -topredE /==>->.
- by move: Hcl; rewrite -topredE /==>->.
by move: Hcl; rewrite -topredE /==>->.
- move=>_ l r c _ _ Hi /negbTE Hcl Hcr IHl IHr Hca Hcb.
rewrite !alphabet_swapLeaves Hcl Hcr IHl // IHr //.
case: (not_intersects a Hi)=>[[ Hail /negbTE Hair]
|[/negbTE Hail Hair]
|[/negbTE Hail /negbTE Hair]];
case: (not_intersects b Hi)=>[[ Hbil /negbTE Hbir]
|[/negbTE Hbil Hbir]
|[/negbTE Hbil /negbTE Hbir]];
rewrite Hail Hair Hbil Hbir ?inE ?Hcl ?Hcr ?Hca ?Hcb /= ?orbT ?andbF ?andbT ?orbF.
- by move: Hcr; rewrite -topredE /==>->.
- by rewrite (negbTE Hcb).
- by rewrite (negbTE Hcb); move: Hcr; rewrite -topredE /==>->.
- by rewrite (negbTE Hca).
- by move: Hcl; rewrite -topredE /==>->.
- by move: Hcl; rewrite -topredE /==>->.
- by rewrite (negbTE Hca); move: Hcr; rewrite -topredE /==>->.
- by move: Hcl; rewrite -topredE /==>->.
by move: Hcl Hcr; rewrite -!topredE /==>->->.
move=>_ l r c _ _ Hi /negbTE Hcl /negbTE Hcr IHl IHr Hca Hcb.
rewrite !alphabet_swapLeaves Hcl Hcr IHl // IHr //.
case: (not_intersects a Hi)=>[[ Hail /negbTE Hair]
|[/negbTE Hail Hair]
|[/negbTE Hail /negbTE Hair]];
case: (not_intersects b Hi)=>[[ Hbil /negbTE Hbir]
|[/negbTE Hbil Hbir]
|[/negbTE Hbil /negbTE Hbir]];
rewrite Hail Hair Hbil Hbir ?inE ?Hcl ?Hcr ?Hca ?Hcb /= ?andbF ?andbT ?orbF.
- by move: Hcr; rewrite -topredE /==>->.
- by rewrite (negbTE Hcb) (negbTE Hca).
- by rewrite (negbTE Hcb); move: Hcr; rewrite -topredE /==>->.
- by rewrite (negbTE Hca) (negbTE Hcb).
- by move: Hcl; rewrite -topredE /==>->.
- by move: Hcl; rewrite (negbTE Hcb) -topredE /==>->.
- by rewrite (negbTE Hca); move: Hcr; rewrite -topredE /==>->.
- by move: Hcl; rewrite (negbTE Hca) -topredE /==>->.
by move: Hcl Hcr; rewrite -!topredE /==>->->.
Qed.
Lemma height_swapLeaves t wa a wb b :
height (swapLeaves t wa a wb b) = height t.
Proof.
elim: t=>/=[w c|_ l IHl r IHr]; first by case: ifP=>//_; case: ifP.
by rewrite IHl IHr.
Qed.
Lemma freq_swapLeaves t a b wa wb c :
consistent t -> a != b ->
freq (swapLeaves t wa a wb b) c =
if c == a
then if b \in alphabet t then wa else 0
else if c == b then
if a \in alphabet t then wb else 0
else freq t c.
Proof.
elim: t=>/=[w d|_ l IHl r IHr].
- rewrite eq_sym=>_ Hba; rewrite !inE; case: eqP=>[->|/eqP Hda] /=.
- rewrite eq_refl (negbTE Hba).
case: eqP=>[->|_]; first by rewrite (negbTE Hba).
by case: ifP=>//->.
case: eqP=>[->|/eqP Hdb] /=.
- rewrite eq_refl (eq_sym a b) (negbTE Hba).
by case: eqP=>//_; case: ifP=>//->.
move: (Hda); rewrite eq_sym=>/negbTE->.
move: (Hdb); rewrite eq_sym=>/negbTE->.
case: eqP=>[->|_]; first by rewrite (negbTE Hda) (negbTE Hdb).
by rewrite !if_same.
case/and3P=>Hi Hl Hr Hab. rewrite IHl // IHr // !inE.
case: ifP=>_.
- case: (not_intersects b Hi); case=>Hil Hir.
- by rewrite Hil (negbTE Hir) /= addn0.
- by rewrite (negbTE Hil) Hir /= add0n.
by rewrite (negbTE Hil) (negbTE Hir).
case: ifP=>_ //.
case: (not_intersects a Hi); case=>Hil Hir.
- by rewrite Hil (negbTE Hir) /= addn0.
- by rewrite (negbTE Hil) Hir /= add0n.
by rewrite (negbTE Hil) (negbTE Hir).
Qed.
Lemma weight_swapLeaves t a b wa wb :
consistent t -> a != b ->
if a \in alphabet t then
if b \in alphabet t then
weight (swapLeaves t wa a wb b) + freq t a + freq t b =
weight t + wa + wb
else
weight (swapLeaves t wa a wb b) + freq t a = weight t + wb
else
if b \in alphabet t then
weight (swapLeaves t wa a wb b) + freq t b = weight t + wa
else
weight (swapLeaves t wa a wb b) = weight t.
Proof.
move=>H; apply: (consistent_ind (P:=fun t a => a != b -> if a \in alphabet t then if _ then _ = _ else _ else _)) =>{a}//=.
- move=>w c a Hab; rewrite !inE; case: eqP; case: eqP.
- by move=><- E; rewrite E eq_refl in Hab.
- by move=>/[swap]<-; rewrite eq_sym eq_refl addnC.
- by move=><-; rewrite eq_sym eq_refl=>/eqP/negbTE->; rewrite addnC.
move/eqP/negbTE; rewrite eq_sym=>->.
by move/eqP/negbTE; rewrite eq_sym=>->.
- move=>_ l r a Hcl Hcr Hi Hal Har IHl IHr Hab; rewrite !inE.
move: (IHr Hab); move: (IHl Hab).
rewrite Hal (negbTE Har) (notin_alphabet_freq_0 Har) addn0 /=.
case: (not_intersects b Hi); case=>Hbl Hbr.
- rewrite Hbl (negbTE Hbr) (notin_alphabet_freq_0 Hbr) /= =>H0->.
by rewrite addn0 (ACl ((1*3*4)*2)%AC) /= H0 (ACl (1*4*2*3)%AC).
- rewrite (negbTE Hbl) (notin_alphabet_freq_0 Hbl) /= Hbr =>H0 H1.
by rewrite add0n (ACl ((1*3)*(2*4))%AC) /= H0 H1 addnA (ACl (1*3*4*2)%AC).
rewrite (negbTE Hbl) (negbTE Hbr) /= =>H0->.
by rewrite addnAC H0 addnAC.
- move=>_ l r a Hcl Hcr Hi Hal Har IHl IHr Hab; rewrite !inE.
move: (IHr Hab); move: (IHl Hab).
rewrite (negbTE Hal) Har (notin_alphabet_freq_0 Hal) add0n /=.
case: (not_intersects b Hi); case=>Hbl Hbr.
- rewrite Hbl (negbTE Hbr) (notin_alphabet_freq_0 Hbr) /= =>H0 H1.
by rewrite addn0 (ACl ((1*4)*(2*3))%AC) /= H0 H1 addnA (ACl (1*3*2*4)%AC).
- rewrite (negbTE Hbl) Hbr (notin_alphabet_freq_0 Hbl) /= =>->H1.
by rewrite add0n (ACl (1*(2*3*4))%AC) /= H1 !addnA.
rewrite (negbTE Hbl) (negbTE Hbr) /= =>->H1.
by rewrite -addnA H1 addnA.
move=>_ l r a Hcl Hcr Hi Hal Har IHl IHr Hab; rewrite !inE.
move: (IHr Hab); move: (IHl Hab); rewrite (negbTE Hal) (negbTE Har) /=.
case: (not_intersects b Hi); case=>Hbl Hbr.
- rewrite Hbl (negbTE Hbr) (notin_alphabet_freq_0 Hbr) /= =>H0->.
by rewrite addn0 addnAC H0 addnAC.
- rewrite (negbTE Hbl) Hbr (notin_alphabet_freq_0 Hbl) /= =>->H1.
by rewrite add0n -addnA H1 addnA.
by rewrite (negbTE Hbl) (negbTE Hbr) /= =>->->.
Qed.
Lemma cost_swapLeaves t a b wa wb :
a != b -> consistent t ->
if a \in alphabet t then
if b \in alphabet t then
cost (swapLeaves t wa a wb b) + freq t a * depth t a + freq t b * depth t b =
cost t + wa * depth t b + wb * depth t a
else
cost (swapLeaves t wa a wb b) + freq t a * depth t a =
cost t + wb * depth t a
else
if b \in alphabet t then
cost (swapLeaves t wa a wb b) + freq t b * depth t b =
cost t + wa * depth t b
else
cost (swapLeaves t wa a wb b) = cost t.
Proof.
move=>Hab; elim: t=>/=[w c|w l IHl r IHr]; rewrite !inE.
- move=>_; case: eqP; case: eqP.
- by move=><- E; rewrite E eq_refl in Hab.
- by move=>/[swap]<-; rewrite eq_sym eq_refl !muln0.
- by move=><-; rewrite eq_sym eq_refl=>/eqP/negbTE->; rewrite !muln0.
move/eqP/negbTE; rewrite eq_sym=>->.
by move/eqP/negbTE; rewrite eq_sym=>->.
case/and3P=>Hi Hcl Hcr; move: (IHr Hcr)=>{IHr}; move: (IHl Hcl)=>{IHl}.
move: (weight_swapLeaves wa wb Hcr Hab); move: (weight_swapLeaves wa wb Hcl Hab).
case: (not_intersects a Hi); case=>Hal Har;
case: (not_intersects b Hi); case=>Hbl Hbr.
- rewrite Hal (negbTE Har) Hbl (negbTE Hbr)
(notin_alphabet_freq_0 Har)
(notin_alphabet_freq_0 Hbr) /==>Wl -> Cl ->.
by rewrite !addn0 !mulnDr !muln1 !addnA (ACl ((1*6*8)*3*(2*5*7)*4)%AC) /=
Wl Cl !addnA (ACl (1*5*4*8*6*2*7*3)%AC).
- rewrite Hal (negbTE Har) (negbTE Hbl) Hbr
(notin_alphabet_freq_0 Har)
(notin_alphabet_freq_0 Hbl) /==>Wl Wr Cl Cr.
by rewrite addn0 add0n !mulnDr !muln1 !addnA (ACl ((1*6)*(3*8)*(2*5)*(4*7))%AC) /=
Wl Wr Cl Cr !addnA (ACl (1*5*3*7*8*4*6*2)%AC).
- rewrite Hal (negbTE Har) (negbTE Hbl) (negbTE Hbr)
(notin_alphabet_freq_0 Har)
(notin_alphabet_freq_0 Hbl) (notin_alphabet_freq_0 Hbr) /==>Wl -> Cl ->.
by rewrite addn0 !mulnDr !muln1 !addnA (ACl ((1*6)*(2*5)*3*4)%AC) /=
Wl Cl !addnA (ACl (1*3*5*6*4*2)%AC).
- rewrite (negbTE Hal) Har Hbl (negbTE Hbr)
(notin_alphabet_freq_0 Hal)
(notin_alphabet_freq_0 Hbr) /==>Wl Wr Cl Cr.
by rewrite addn0 add0n !mulnDr !muln1 !addnA (ACl ((1*8)*(3*6)*(2*7)*(4*5))%AC) /=
Wl Wr Cl Cr !addnA (ACl (1*5*3*7*6*2*8*4)%AC).
- rewrite (negbTE Hal) Har (negbTE Hbl) Hbr
(notin_alphabet_freq_0 Hal)
(notin_alphabet_freq_0 Hbl) /==>-> Wr -> Cr.
by rewrite !add0n !mulnDr !muln1 !addnA (ACl (1*2*(3*6*8)*(4*5*7))%AC) /=
Wr Cr !addnA (ACl (1*2*3*6*7*4*8*5)%AC).
- rewrite (negbTE Hal) Har (negbTE Hbl) (negbTE Hbr)
(notin_alphabet_freq_0 Hal)
(notin_alphabet_freq_0 Hbl) (notin_alphabet_freq_0 Hbr) /==>-> Wr -> Cr.
by rewrite add0n !mulnDr !muln1 !addnA (ACl (1*2*(3*6)*(4*5))%AC) /=
Wr Cr !addnA (ACl (1*2*3*5*6*4)%AC).
- rewrite (negbTE Hal) (negbTE Har) Hbl (negbTE Hbr)
(notin_alphabet_freq_0 Hal) (notin_alphabet_freq_0 Har)
(notin_alphabet_freq_0 Hbr) /==>Wl -> Cl ->.
by rewrite addn0 !mulnDr !muln1 !addnA (ACl ((1*6)*(2*5)*3*4)%AC) /=
Wl Cl !addnA (ACl (1*3*5*6*4*2)%AC).
- rewrite (negbTE Hal) (negbTE Har) (negbTE Hbl) Hbr
(notin_alphabet_freq_0 Hal) (notin_alphabet_freq_0 Har)
(notin_alphabet_freq_0 Hbl) /==>-> Wr -> Cr.
by rewrite add0n !mulnDr !muln1 !addnA (ACl (1*2*(3*6)*(4*5))%AC) /=
Wr Cr !addnA (ACl (1*2*3*5*6*4)%AC).
by rewrite (negbTE Hal) (negbTE Har) (negbTE Hbl) (negbTE Hbr)
(notin_alphabet_freq_0 Hal) (notin_alphabet_freq_0 Har)
(notin_alphabet_freq_0 Hbl) (notin_alphabet_freq_0 Hbr) /==>->->->->.
Qed.
Lemma sibling_swapLeaves_sibling t wa a ws b :
consistent t -> sibling t b != b -> a != b ->
sibling (swapLeaves t wa a ws (sibling t b)) a = b.
Proof.
elim: t=>/=[w c|w l IHl r IHr].
- by move=>_; simp sibling; rewrite eq_refl.
case: (posnP (height l))=>El; case: (posnP (height r))=>Er.
- case/eqP/heightE: El=>[wl][xl]->; case/eqP/heightE: Er=>[wr][xr]-> /=.
simp sibling; rewrite /alpha_intersects /= orbF andbT =>/negbTE Hrl; case: ifP=>[/eqP->|Hbxl].
- move=>_; rewrite eq_sym=>/negbTE Haxl.
rewrite Haxl eq_sym Hrl eq_refl; case: ifP; simp sibling.
- by rewrite eq_sym=>->; rewrite eq_sym Haxl.
by move=>_; rewrite eq_sym Haxl eq_refl.
case: ifP; last by rewrite eq_refl.
move/eqP=>-> _; rewrite eq_sym=>/negbTE Haxr; rewrite eq_refl Hrl Haxr.
case: ifP; simp sibling; first by rewrite eq_sym=>->.
by rewrite eq_refl.
- rewrite height_gt0_sibling; last by rewrite Er orbT.
case/eqP/heightE: El=>[wl][xl]->/=; rewrite /alpha_intersects /= inE; simp sibling.
case/andP=>Hxlr Hcr; case: ifP; first by rewrite eq_refl.
move=>Hbxl Hsrb Hab.
have Hsab : sibling r b \in alphabet r by apply: sibling_ne_in_alphabet.
have Hxsb : sibling r b != xl.
- by rewrite alphabet_enum in Hsab; move/hasPn: Hxlr=>/(_ _ Hsab).
rewrite height_gt0_sibling; last by rewrite height_swapLeaves Er orbT.
case/boolP: (xl==a)=>/=.
- rewrite inE; move/eqP=>{1}<-; rewrite eq_sym (negbTE Hxsb).
by apply: IHr.
move=>Haxl; rewrite eq_sym (negbTE Hxsb) /= inE eq_sym (negbTE Haxl).
by apply: IHr.
- rewrite height_gt0_sibling; last by rewrite El.
case/eqP/heightE: Er=>[wr][xr]->/=; rewrite /alpha_intersects /= orbF andbT; simp sibling.
case/andP=>Hlxr Hcl; case: ifP; last by rewrite eq_refl.
move=>Hbal Hslb Hab.
have Hsab : sibling l b \in alphabet l by apply: sibling_ne_in_alphabet.
rewrite height_gt0_sibling; last by rewrite height_swapLeaves El.
rewrite alphabet_swapLeaves Hsab; case/boolP: (a \in alphabet l)=>/=.
- by move=>->; apply: IHl.
by move=>_; rewrite !inE eq_refl /=; apply: IHl.
case/and3P=>Hi Hcl Hcr.
rewrite height_gt0_sibling; last by rewrite El.
case/boolP: (b \in alphabet l)=>Hbal Hsb Hab.
- have Hsab : sibling l b \in alphabet l by apply: sibling_ne_in_alphabet.
rewrite height_gt0_sibling; last by rewrite height_swapLeaves El.
rewrite alphabet_swapLeaves Hsab; case/boolP: (a \in alphabet l)=>/=.
- by move=>->; apply: IHl.
by move=>_; rewrite !inE eq_refl /=; apply: IHl.
rewrite height_gt0_sibling; last by rewrite height_swapLeaves El.
have Hsab : sibling r b \in alphabet r by apply: sibling_ne_in_alphabet.
move: Hi; rewrite intersectsC=>/alpha_not/(_ _ Hsab)=>Hsab'.
rewrite alphabet_swapLeaves (negbTE Hsab'); case/boolP: (a \in alphabet l)=>/=.
- move=>Hal; rewrite !inE eq_refl /= orbF; case: eqP=>[E|_]; last by apply IHr.
by rewrite -E Hal in Hsab'.
by rewrite inE=>/negbTE->; apply: IHr.
Qed.
Definition swapSyms (t : tree A) (a b : A) : tree A :=
swapLeaves t (freq t a) a (freq t b) b.
Corollary swapSyms_id t a :
consistent t -> swapSyms t a a = t.
Proof. by move=>H; apply: swapLeaves_id. Qed.
Lemma alphabet_swapSyms t a b :
a \in alphabet t -> b \in alphabet t -> alphabet (swapSyms t a b) =i alphabet t.
Proof.
move=>Ha Hb; rewrite /swapSyms=>x.
by rewrite alphabet_swapLeaves Ha Hb.
Qed.
Lemma consistent_swapSyms t a b :
consistent t -> consistent (swapSyms t a b).
Proof. by exact: consistent_swapLeaves. Qed.