Definition ltopt (x y : option key) :=
  match x, y with
  | Some x', Some y' => K.lt x' y'
  | _, _ => True
  end.Inductive searchtree: option key -> option key -> tree -> Prop :=
 | STE: forall lo hi, ltopt lo hi -> searchtree lo hi EE
 | STT: forall c tl k tr lo hi,
                searchtree lo (Some k)  tl ->
                searchtree (Some k) hi tr ->
                searchtree lo hi (T c tl k tr) .Inductive searchtree: option key -> option key -> tree -> Prop :=
 | STE: forall lo hi, ltopt lo hi -> searchtree lo hi EE
 | STT: forall c tl k tr lo hi,
                searchtree lo (Some k)  tl ->
                searchtree (Some k) hi tr ->
                searchtree lo hi (T c tl k tr) .Inductive searchtree: option key -> option key -> tree -> Prop :=
 | STE: forall lo hi, ltopt lo hi -> searchtree lo hi EE
 | STT: forall c tl k tr lo hi,
                searchtree lo (Some k)  tl ->
                searchtree (Some k) hi tr ->
                searchtree lo hi (T c tl k tr) .Ltac do_searchtree :=
  assumption ||
  constructor ||
  match goal with
  |  |- searchtree _ _ (match ?C with Red => _ | Black => _ end) => destruct C
  |  |- searchtree _ _ (match ?C with EE => _ | T _ _ _ _ => _ end) => destruct C
  | H: searchtree _ _ EE |- _ => inv H
  | H: searchtree _ _ (T _ _ _ _) |- _ => inv H
  | |- ltopt _ _ => unfold ltopt in *; auto
  | |- match ?A with Some _ => _ | None => _ end => destruct A
  | H: K.lt ?A ?B  |- K.lt ?A ?C  =>
           try solve [apply lt_trans with B; assumption]; clear H
  end.

Lemma searchtree_balance: forall c s1 t s2 lo hi, ltopt lo (Some t) -> ltopt (Some t) hi -> searchtree lo (Some t) s1-> searchtree (Some t) hi s2 -> searchtree lo hi (balance c s1 t s2).