Section Restrict.
Variable A : Type.
Variable empty : A.
Variable eqA_dec : forall a b : A, {a = b} + {a <> b}.
Variable m : list A.
(* Restrict the code putting only codes of element in the frequency list *)
Definition restrict_code (m : list A) (c : code A) :
code A :=
map (fun x => (fst x, find_code eqA_dec (fst x) c))
(frequency_list eqA_dec m).
Proof using.
intros c; apply ulist_unique_key.
unfold restrict_code in |- *.
replace
(map (fst (B:=_))
(map (fun x : A * nat => (fst x, find_code eqA_dec (fst x) c))
(frequency_list eqA_dec m))) with
(map (fst (B:=_)) (frequency_list eqA_dec m)).
apply unique_key_ulist; auto.
elim (frequency_list eqA_dec m); simpl in |- *; auto with datatypes.
intros a l H; apply f_equal2 with (f := cons (A:=A)); auto.
Qed.
Proof using.
intros a c H.
apply sym_equal; apply find_code_correct2; auto.
apply restrict_code_unique_key.
generalize (in_frequency_map _ eqA_dec m a H).
unfold restrict_code in |- *; elim (frequency_list eqA_dec m); simpl in |- *;
auto with datatypes.
intros a0; case a0; simpl in |- *; auto with datatypes.
intros a1 n l H0 [H1| H1]; try rewrite H1; auto.
Qed.
Proof using.
intros m1 c; elim m1; simpl in |- *; auto.
intros a l H H0.
apply f_equal2 with (f := app (A:=bool)); auto with datatypes.
apply restrict_code_in; auto with datatypes.
apply H; apply incl_tran with (2 := H0); auto with datatypes.
Qed.
Proof using.
intros c; apply restrict_code_encode_incl; auto with datatypes.
Qed.
Proof using.
intros c HH HH0 (HH1, HH2); split.
intros a1 a2 lb1 lb2 H0 H1 H2; apply HH1 with (lb1 := lb1) (lb2 := lb2); auto.
unfold restrict_code in H0.
case in_map_inv with (1 := H0).
intros x; case x; simpl in |- *.
intros a0 n (HP1, HP2).
rewrite HP2.
case (HH0 a0); auto.
apply frequency_list_in with (1 := HP1).
intros x0 H; rewrite find_code_correct2 with (2 := H); auto.
unfold restrict_code in H1.
case in_map_inv with (1 := H1).
intros x; case x; simpl in |- *.
intros a0 n (HP1, HP2).
rewrite HP2.
case (HH0 a0); auto.
apply frequency_list_in with (1 := HP1).
intros x0 H; rewrite find_code_correct2 with (2 := H); auto.
unfold restrict_code in |- *.
apply unique_key_map; auto.
Qed.
Proof using.
intros c; unfold restrict_code in |- *; elim (frequency_list eqA_dec m);
simpl in |- *; auto.
intros a0 l H; apply f_equal2 with (f := cons (A:=A)); auto.
Qed.
Proof using.
case m; simpl in |- *; auto.
unfold restrict_code in |- *.
intros a0 l c H H1.
absurd
(In
((fun x : A * nat => (fst x, find_code eqA_dec (fst x) c))
(a0, number_of_occurrences eqA_dec a0 (a0 :: l))) nil);
auto with datatypes.
rewrite <- H1.
apply
in_map with (f := fun x : A * nat => (fst x, find_code eqA_dec (fst x) c)).
apply frequency_number_of_occurrences; auto with datatypes.
Qed.

(* The leaves of the build tree from the restrict code are the keys of the frequency list *)
Theorem restrict_code_pbbuild :
forall c : code A,
not_null c ->
unique_prefix c ->
in_alphabet m c ->
m <> nil ->
permutation (map fst (frequency_list eqA_dec m))
(all_pbleaves (pbbuild empty (restrict_code m c))).