(** ** Comparison *)
(** Relations [eq] and [lt] over trees *)
Module L := MSetInterface.MakeListOrdering X.
Definition eq := Equal.
#[global]
Instance eq_equiv : Equivalence eq.
Definition lt (s1 s2 : tree) : Prop :=
exists s1' s2', Ok s1' /\ Ok s2' /\ eq s1 s1' /\ eq s2 s2'
/\ L.lt (elements s1') (elements s2').
Declare Equivalent Keys L.eq equivlistA.
#[global]
Instance lt_strorder : StrictOrder lt.
#[global]
Instance lt_compat : Proper (eq==>eq==>iff) lt.
(** Proof of the comparison algorithm *)
(** [flatten_e e] returns the list of elements of [e] i.e. the list of elements actually compared *)
Fixpoint flatten_e (e : enumeration) : list elt := match e with
| End => nil
| More x t r => x :: elements t ++ flatten_e r
end.

(* Specifications: *)
Lemma flatten_e_elements :
forall l x r c e,
elements l ++ flatten_e (More x r e) = elements (Node c l x r) ++ flatten_e e.
Lemma cons_1 : forall s e,
flatten_e (cons s e) = elements s ++ flatten_e e.