Section ELEMENTS_BETWEEN.
(** [elements_between above below s] returns the set of elements of [s] that belong to the interval [above,below]. *)
Variable above_low_bound: elt -> bool.
Variable below_high_bound: elt -> bool.
Fixpoint raw_elements_between (m: Raw.tree) : Raw.tree :=
match m with
| Raw.Leaf => Raw.Leaf
| Raw.Node _ l x r =>
if above_low_bound x then
if below_high_bound x then
Raw.join (raw_elements_between l) x (raw_elements_between r)
else
raw_elements_between l
else
raw_elements_between r
end.
Remark In_raw_elements_between_1:
forall x m,
Raw.In x (raw_elements_between m) -> Raw.In x m.
Definition elements_between (s: t) : t :=
@MSet.Mkt (raw_elements_between s.(MSet.this)) (raw_elements_between_ok s.(MSet.this) s.(MSet.is_ok)).
Hypothesis above_monotone:
forall x1 x2, X.eq x1 x2 \/ X.lt x1 x2 -> above_low_bound x1 = true -> above_low_bound x2 = true.
Hypothesis below_monotone:
forall x1 x2, X.eq x1 x2 \/ X.lt x2 x1 -> below_high_bound x1 = true -> below_high_bound x2 = true.
Remark In_raw_elements_between_2:
forall x m,
Raw.In x (raw_elements_between m) -> above_low_bound x = true /\ below_high_bound x = true.
Remark In_raw_elements_between_3:
forall x m,
Raw.bst m ->
Raw.In x m -> above_low_bound x = true -> below_high_bound x = true ->
Raw.In x (raw_elements_between m).

Theorem elements_between_iff:
forall x s,
In x (elements_between s) <-> In x s /\ above_low_bound x = true /\ below_high_bound x = true.