(* Construction of a binary search tree containing the elements of
  a list of integers *)


Definition list2trees_spec (l : list Z) :=
  {t : Z_btree | search t /\ (forall p : Z, In p l <-> occ p t)}.Definition list2trees_aux_spec (l : list Z) (t : Z_btree) :=
  search t ->
  {t' : Z_btree |
  search t' /\ (forall p : Z, In p l \/ occ p t <-> occ p t')}.Definition list2trees_aux :
  forall (l : list Z) (t : Z_btree), list2trees_aux_spec l t.Definition list2trees : forall l : list Z, list2trees_spec l.(*
let list2trees l =
  list2trees_aux l Z_leaf prop
*)



Inductive RMAX (t t' : Z_btree) (n : Z) : Prop :=
    rmax_intro :
      occ n t ->
      (forall p : Z, occ p t -> (p <= n)%Z) ->
      (forall q : Z, occ q t' -> occ q t) ->
      (forall q : Z, occ q t -> occ q t' \/ n = q) ->
      ~ occ n t' -> search t' -> RMAX t t' n.(*
let list2trees l =
  list2trees_aux l Z_leaf prop
*)



Inductive RMAX (t t' : Z_btree) (n : Z) : Prop :=
    rmax_intro :
      occ n t ->
      (forall p : Z, occ p t -> (p <= n)%Z) ->
      (forall q : Z, occ q t' -> occ q t) ->
      (forall q : Z, occ q t -> occ q t' \/ n = q) ->
      ~ occ n t' -> search t' -> RMAX t t' n.

Lemma rmax_t_Z_leaf : forall (t : Z_btree) (n : Z), search (Z_bnode n t Z_leaf) -> RMAX (Z_bnode n t Z_leaf) t n.