Section Frequency.
Variable A : Type.
Variable eqA_dec : forall a b : A, {a = b} + {a <> b}.
(* Create a list of a given length with a given element  *)
Fixpoint id_list (a : A) (n : nat) {struct n} : list A :=
match n with
| O => nil
| S n1 => a :: id_list a n1
end.
(* An element in a frequency list *)
Fixpoint add_frequency_list (a : A) (l : list (A * nat)) {struct l} :
list (A * nat) :=
match l with
| nil => (a, 1) :: nil
| (b, n) :: l1 =>
match eqA_dec a b with
| left _ => (a, S n) :: l1
| right _ => (b, n) :: add_frequency_list a l1
end
end.
Proof using.
intros a l; generalize a; elim l; simpl in |- *; clear a l; auto.
intros (a, n) l H b.
case (eqA_dec b a); auto.
intros e; simpl in |- *; rewrite e; auto.
simpl in |- *.
intros e;
apply
permutation_trans
with
(id_list a n ++
(b :: nil) ++ flat_map (fun p => id_list (fst p) (snd p)) l);
[ idtac | simpl in |- *; auto ].
change
(permutation
((b :: nil) ++
id_list a n ++ flat_map (fun p => id_list (fst p) (snd p)) l)
(id_list a n ++
(b :: nil) ++ flat_map (fun p => id_list (fst p) (snd p)) l))
in |- *.
repeat rewrite <- app_ass; auto.
Qed.
Proof using.
intros a1 a2 b1 l; elim l; simpl in |- *; auto.
intros [H1| H1]; auto; injection H1; auto.
intros (a3, b3) l1 Rec; simpl in |- *; auto.
case (eqA_dec a2 a3); simpl in |- *; auto.
intros e H; cut (In (a1, b1) ((a2, S b3) :: l1)); auto.
simpl in |- *; intros [H1| H1]; auto.
injection H1; auto.
intuition.
Qed.
Proof using.
intros a l; elim l; simpl in |- *; auto.
intros (a1, n1) l1 Rec H; case (eqA_dec a a1).
intros e; apply unique_key_perm with (l1 := (a, S n1) :: l1); auto.
apply unique_key_cons; auto.
intros b; red in |- *; intros H0; case (unique_key_in _ _ _ _ b _ H); auto.
rewrite <- e; auto.
apply unique_key_inv with (1 := H); auto.
intros n; apply unique_key_cons; auto.
intros b; red in |- *; intros H0;
case add_frequency_list_in_inv with (1 := H0); auto.
intros H2; case (unique_key_in _ _ _ _ b _ H); auto.
apply Rec; apply unique_key_inv with (1 := H); auto.
Qed.
Proof using.
intros a l; generalize a; elim l; clear a l; simpl in |- *; auto.
intros (a1, l1) l0 H a H0.
case (eqA_dec a a1); auto.
intros H1; case (H0 l1); left;
apply f_equal2 with (f := pair (A:=A) (B:=nat)); auto.
intros n; apply in_cons; auto; apply H; auto.
intros ca; red in |- *; intros H1; case (H0 ca); auto.
Qed.
Proof using.
intros m; elim m; clear m; simpl in |- *; auto.
intros (a1, l1) l Rec a n H H1; case (eqA_dec a a1); simpl in |- *; auto.
intros H2; case H1; auto.
intros H3; left; apply f_equal2 with (f := pair (A:=A) (B:=nat));
injection H3; auto.
rewrite H2; intros H3; case unique_key_in with (1 := H) (b2 := n); auto.
intros n0; right; apply Rec.
apply unique_key_inv with (1 := H); auto.
case H1; auto.
intros H0; case n0; injection H0; auto.
Qed.
Proof using.
intros m; elim m; clear m; simpl in |- *; auto.
intros (a1, l1) l H a0 b n H0 [H1| H1]; case (eqA_dec b a1); simpl in |- *;
auto.
intros H2; case H0; injection H1; auto.
intros; apply trans_equal with (2 := sym_equal H2); auto.
Qed.
(* Create a frequency list from a message *)
Fixpoint frequency_list (l : list A) : list (A * nat) :=
match l with
| nil => nil
| a :: l1 => add_frequency_list a (frequency_list l1)
end.
Proof using.
intros a n m; generalize n; elim m; clear m n; simpl in |- *; auto.
intros a0 l H n H0; case add_frequency_list_in_inv with (1 := H0); auto.
intros H1; right; apply (H n); auto.
Qed.
Proof using.
intros l; elim l; simpl in |- *; auto.
intros a l0 H.
apply
permutation_trans with (2 := add_frequency_list_perm a (frequency_list l0));
auto.
Qed.
Proof using.
intros l; elim l; simpl in |- *; auto.
intros a l0 H; apply add_frequency_list_unique_key; auto.
Qed.
Hint Resolve frequency_list_unique : core.
Proof using.
intros l; elim l; simpl in |- *; auto.
intros a l0 H a0 [H0| H0]; auto.
rewrite H0; elim (frequency_list l0); simpl in |- *; auto.
intros (a1, l1) l2; simpl in |- *; auto.
case (eqA_dec a0 a1); simpl in |- *; auto.
cut (In a0 (map (fst (A:=A) (B:=nat)) (frequency_list l0))); auto.
elim (frequency_list l0); simpl in |- *; auto.
intros (a1, l1) l2; simpl in |- *; auto.
case (eqA_dec a a1); simpl in |- *; auto.
intros e H1 [H2| H2]; auto; left; rewrite <- H2; auto.
intros e H1 [H2| H2]; auto.
Qed.
Hint Resolve in_frequency_map : core.
Proof using.
intros l a H; case in_map_inv with (1 := H); auto.
intros (a1, l1) (Hl1, Hl2); simpl in |- *.
rewrite Hl2; apply frequency_list_in with (1 := Hl1).
Qed.
(* Compute the number of occurrences of an element in a message *)
Fixpoint number_of_occurrences (a : A) (l : list A) {struct l} : nat :=
match l with
| nil => 0
| b :: l1 =>
match eqA_dec a b with
| left _ => S (number_of_occurrences a l1)
| right _ => number_of_occurrences a l1
end
end.
Proof using.
intros a l; elim l; simpl in |- *; auto.
intros a0 l0 H H0; case (eqA_dec a a0); auto.
intros H1; case H0; auto.
Qed.
Proof using.
intros m; elim m; simpl in |- *; auto.
intros a; exists (nil (A:=A)); split; auto with datatypes.
intros a l H a0.
case (eqA_dec a0 a); simpl in |- *; intros H1.
case (H a0); intros m1 (H2, H3).
exists m1; split; auto.
pattern a0 at 1 in |- *; rewrite H1; auto.
case (H a0); intros m1 (H2, H3).
exists (a :: m1); split; auto.
apply
permutation_trans
with ((a :: m1) ++ id_list a0 (number_of_occurrences a0 l));
auto.
simpl in |- *; apply permutation_skip; auto.
apply permutation_trans with (1 := H2); auto.
simpl in |- *; Contradict H3; case H3; intros H4; auto; case H1; auto.
Qed.
Proof using.
intros l1; elim l1; simpl in |- *; auto.
intros a l H l2 a0; case (eqA_dec a0 a); intros H1; simpl in |- *; auto.
Qed.

(* Permutation preserves the number of occurrences *)
Theorem number_of_occurrences_permutation :
forall l1 l2 a,
permutation l1 l2 -> number_of_occurrences a l1 = number_of_occurrences a l2.