(* 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_leaf_leaf : forall n : Z, RMAX (Z_bnode n Z_leaf Z_leaf) Z_leaf n.