(************************************************************************)
(*         *      The Rocq Prover / The Rocq Development Team           *)
(*  v      *         Copyright INRIA, CNRS and contributors             *)
(* <O___,, * (see version control and CREDITS file for authors & dates) *)
(*   \VV/  **************************************************************)
(*    //   *    This file is distributed under the terms of the         *)
(*         *     GNU Lesser General Public License Version 2.1          *)
(*         *     (see LICENSE file for the text of the license)         *)
(************************************************************************)
(* Finite map library.  *)
(** * FMapFullAVL  This file contains some complements to [FMapAVL].  - Functor [AvlProofs] proves that trees of [FMapAVL] are not only binary search trees, but moreover well-balanced ones. This is done by proving that all operations preserve the balancing.  - We then pack the previous elements in a [IntMake] functor similar to the one of [FMapAVL], but richer.  - In final [IntMake_ord] functor, the [compare] function is different from the one in [FMapAVL]: this non-structural version is closer to the original Ocaml code.  *)
Require Program.
Set Implicit Arguments.
Unset Strict Implicit.
Module AvlProofs (Import I:Int)(X: OrderedType).
Module Import Raw := Raw I X.
Module Import II:=MoreInt(I).
Local Open Scope pair_scope.
Local Open Scope Int_scope.
Ltac omega_max := i2z_refl; lia.
Section Elt.
Variable elt : Type.
Implicit Types m r : t elt.
(** * AVL trees *)
(** [avl s] : [s] is a properly balanced AVL tree, i.e. for any node the heights of the two children differ by at most 2 *)
Inductive avl : t elt -> Prop :=
| RBLeaf : avl (Leaf _)
| RBNode : forall x e l r h,
avl l ->
avl r ->
-(2) <= height l - height r <= 2 ->
h = max (height l) (height r) + 1 ->
avl (Node l x e r h).
(** * Automation and dedicated tactics about [avl]. *)
#[local]
Hint Constructors avl : core.

Lemma height_non_negative : forall (s : t elt), avl s ->
height s >= 0.