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.

(* Adding an element does not change the unique key property *)
Theorem add_frequency_list_unique_key :
forall (a : A) l, unique_key l -> unique_key (add_frequency_list a l).