Section UniqueKey.
Variables (A : Type) (B : Type).
(* An association list has unique keys if the keys appear only once *)
Inductive unique_key : list (A * B) -> Prop :=
| unique_key_nil : unique_key nil
| unique_key_cons :
forall (a : A) (b : B) l,
(forall b : B, ~ In (a, b) l) ->
unique_key l -> unique_key ((a, b) :: l).
Hint Constructors unique_key : core.
Proof using.
intros a l H; inversion H; auto.
Qed.
Proof using.
intros a b1 b2 l H; inversion H; auto.
Qed.
Proof using.
intros a l1 l2 l H; generalize a l1 l2; elim H; simpl in |- *; auto;
clear H a l1 l2 l.
intros a l1 l2 H; case H.
intros a b l H H0 H1 a0 l1 l2 [H2| H2] [H3| H3].
injection H2; injection H3; intros; apply trans_equal with b; auto.
case (H l2); injection H2; intros H4 H5; rewrite H5; auto.
case (H l1); injection H3; intros H4 H5; rewrite H5; auto.
apply H1 with (1 := H2) (2 := H3); auto.
Qed.
Proof using.
intros l1 l2 H; elim H; auto.
intros (a1, b1) L1 L2 H0 H1 H2; apply unique_key_cons.
intros b; red in |- *; intros H3; case (unique_key_in _ _ b _ H2).
apply permutation_in with (2 := H3); auto.
apply permutation_sym; auto.
apply H1; apply unique_key_inv with (1 := H2); auto.
intros (a1, b1) (a2, b2) L H0; apply unique_key_cons.
intros b; red in |- *; simpl in |- *; intros [H1| H1].
case (unique_key_in _ _ b2 _ H0); auto.
injection H1; intros H2 H3; rewrite H3; simpl in |- *; auto.
case (unique_key_in _ _ b _ (unique_key_inv _ _ H0)); auto.
apply unique_key_cons.
intros b; red in |- *; simpl in |- *; intros H1;
case (unique_key_in _ _ b _ H0); simpl in |- *; auto.
apply unique_key_inv with (a := (a2, b2));
apply unique_key_inv with (1 := H0).
Qed.
Proof using.
intros l1; elim l1; simpl in |- *; auto.
intros (a1, ll1) l H l2 H0 H1 H2; apply unique_key_cons; auto.
intros b; red in |- *; intros H3.
case in_app_or with (1 := H3).
change (~ In (a1, b) l) in |- *; apply unique_key_in with (1 := H0).
intros H4; apply (H2 a1 ll1 b); auto.
apply H; auto.
apply unique_key_inv with (1 := H0); auto.
intros a b c H3 H4; apply (H2 a b c); auto.
Qed.
Proof using.
intros l; elim l; simpl in |- *; auto.
intros a l0 H H0; apply ulist_cons.
inversion H0.
red in |- *; intros H5; case in_map_inv with (1 := H5).
intros (b2, l2); simpl in |- *; intros (Hb1, Hb2); case (H3 l2); auto.
rewrite Hb2; auto.
apply H; apply unique_key_inv with (1 := H0); auto.
Qed.
Proof using.
intros l; elim l; simpl in |- *; auto.
intros a; case a.
intros a0 b l0 H H0; apply unique_key_cons; auto.
intros b0; red in |- *; intros H1; absurd (In a0 (map (fst (B:=_)) l0)); auto.
inversion H0; auto.
change (In (fst (a0, b0)) (map (fst (B:=_)) l0)) in |- *; auto with datatypes.
apply in_map; auto.
apply H; apply ulist_inv with (1 := H0); auto.
Qed.
End UniqueKey.
Hint Constructors unique_key : core.
Arguments unique_key [A B].

(* Uniqueness is compatible with map for injective functions *)
Theorem unique_key_map :
forall (A B C D : Type) l (f : A * B -> C * D),
unique_key l ->
(forall a b, fst (f a) = fst (f b) -> fst a = fst b) -> unique_key (map f l).