Fixpoint rgopher (s: tree) : tree :=
match s with
| EE => EE
| T _ sl k sr => match rgopher sr with
| T _ srl j srr => T Red  (T Red sl k srl) j srr
| EE => s
end
end.

(* Specifications: *)
Lemma shape_rgopher: forall a,
match rgopher a with
| T _ _ _ (T _ _ _ _) => False
| _ => True
end.
Lemma size_rgopher: forall s, size (rgopher s) = size s.
Lemma searchtree_rgopher: forall a lo hi,
searchtree lo hi a <-> searchtree lo hi (rgopher a).
Lemma interp_rgopher: forall s k, interp (rgopher s) k <-> interp s k.
Function compare' (s t : tree) {measure size s} : comparison :=
match gopher s, gopher t with
| EE , EE => Eq
| T _ EE j sr , T _ EE j' tr =>
match K.compare j j' with
| Gt => Gt | Lt => Lt | Eq => compare' sr tr
end
| T _ _ _ _, T _ _ _ _ => Eq (* impossible *)
| T _ _ _ _, EE => Gt
|  EE, T _ _ _ _ => Lt
end.
Lemma compare'_interp_aux:
forall a lo hi b lo' hi',
searchtree lo hi a ->
searchtree lo' hi' b ->
(forall k, interp a k <-> interp b k) ->
compare' a b = Eq.