Definition Equal' s s' := forall a, interp s a <-> interp s' a.Definition Subset' s s' := forall a, interp s a -> interp s' a.Definition Empty' s := forall a, ~ interp s a.Definition For_all' (P : key -> Prop) s := forall x, interp s x -> P x.Definition Exists' (P : key -> Prop) s := exists x, interp s x /\ P x.(* Now, some definitions to match the Module Type *)

Definition t : Type := sig valid.Definition elt := key.Definition empty : t := exist valid EE empty_valid.Definition is_empty (x: t) : bool :=
   match proj1_sig x with EE => true | _ => false end.Definition mem (k: elt) (x: t) : bool := member k (proj1_sig x).Definition add (k: elt) (x: t) : t :=
      exist valid (insert k (proj1_sig x)) (insert_valid _ _ (proj2_sig x)).Definition singleton (k: elt) : t := exist valid _ (singleton_valid k).Definition remove (k: elt) (x: t) : t :=
  exist valid _ (remove_valid k _ (proj2_sig x)).Definition fold (A: Type) (f: elt -> A -> A) (x: t) (base: A) : A :=
  fold' A f (proj1_sig x) base.Definition for_all (f: elt -> bool) (x: t) : bool :=
   fold bool (fun k y => andb (f k) y) x true.Definition exists_ (f: elt -> bool) (x: t) : bool :=
   fold bool (fun k y => orb (f k) y) x false.Definition cardinal (x: t) := fold nat (fun _ => S) x O.Definition filter (f: elt -> bool) (s: t) : t := exist valid _ (filter_valid f _ (proj2_sig s)).Definition partition (f: elt -> bool) (s: t) : t*t :=
   let s' := partition' f (proj1_sig s)
    in (exist valid _ (partition_valid1 f _ (proj2_sig s)),
         exist valid _ (partition_valid2 f _ (proj2_sig s))).Definition union (t1 t2: t) : t := exist valid _ (union_valid _ _ (proj2_sig t1) (proj2_sig t2)).Definition diff (t1 t2: t) : t := exist valid _ (diff_valid _ _ (proj2_sig t1) (proj2_sig t2)).Definition inter (t1 t2: t) : t := exist valid _ (inter_valid _ _ (proj2_sig t1) (proj2_sig t2)).Definition elements (x: t) : list elt := elements' (proj1_sig x) nil.Definition choose (x: t) : option elt := choose' (proj1_sig x).Definition max_elt (x: t) : option elt := max_elt' (proj1_sig x).Definition equal' (s1 s2 : tree) : bool :=
  match compare' s1 s2 with Eq => true | _ => false end.Definition equal (t1 t2 : t) : bool := equal' (proj1_sig t1) (proj1_sig t2).Definition subset (t1 t2: t) : bool := is_empty (diff t1 t2).Definition In (k: key) (x: t) := interp (proj1_sig x) k.

(* Specifications: *)
Lemma In_compat : Proper (E.eq==>eq==>iff) In.