Parameter d : nat.(* could use bin_search here instead *)

  Inductive find_result := Found (i : nat) | NotFound (i : nat).(* could use bin_search here instead *)

  Inductive find_result := Found (i : nat) | NotFound (i : nat).(* could use bin_search here instead *)

  Inductive find_result := Found (i : nat) | NotFound (i : nat).Definition search tgt t := match find_path tgt t with Some _ => true | None => false end.Definition replace {A} i (x : A) l := firstn i l ++ x :: skipn (S i) l.Definition split parent i :=
    match parent with BNode keys children =>
      match nth_error children i with
      | Some (BNode keys' children') =>
        let mid := (length keys' / 2)%nat in
        BNode (insert i (nth mid keys' 0) keys)
              (insert i (BNode (firstn mid keys') (firstn (S mid) children'))
              (replace i (BNode (skipn (S mid) keys') (skipn (S mid) children')) children))
      | None => parent
      end
    end.Definition remove_at {A} i (l : list A) := firstn i l ++ skipn (S i) l.Definition delete tgt t := option_map (delete_at t) (find_path tgt t).

(* Specifications: *)
Lemma empty_wf : wf_btree b_empty.
Lemma insert_length : forall {A} i (x : A) l, length (insert i x l) = S (length l).
Lemma replace_length : forall {A} i (x : A) l, (i < length l)%nat -> length (replace i x l) = length l.
Lemma nth_error_lt : forall {A} n l (x : A), nth_error l n = Some x -> (n < length l)%nat.
Lemma replace_In : forall {A} l i (x y : A), In y (replace i x l) -> In y l \/ y = x.
Lemma Forall_replace : forall {A} (P : A -> Prop) l i x, Forall P l -> P x -> Forall P (replace i x l).
Lemma replace_In' : forall {A} l i (x y : A), In y (replace i x l) -> In y (remove_at i l) \/ y = x.
Lemma Forall_replace' : forall {A} (P : A -> Prop) l i x, Forall P (remove_at i l) -> P x -> Forall P (replace i x l).
Lemma nth_error_replace : forall {A} l i (x : A), (i <= length l)%nat -> nth_error (replace i x l) i = Some x.
Lemma insert_In : forall {A} i l (x y : A), In y (insert i x l) -> In y l \/ y = x.
Lemma Forall_insert : forall {A} (P : A -> Prop) l i x, Forall P l -> P x -> Forall P (insert i x l).
Lemma odd_div : forall n, (S (2 * n) / 2)%nat = n.
Lemma Forall_skipn : forall {A} P (l : list A) n, Forall P l -> Forall P (skipn n l).
Lemma Forall_firstn : forall {A} P (l : list A) n, Forall P l -> Forall P (firstn n l).
Lemma split_wf : forall keys children i c (Hc : nth_error children i = Some c) (Hover : wf_btree_over c) (Hrest : Forall wf_btree_aux (remove_at i children)) (Hkeys : (d <= length keys <= 2 * d)%nat) (Hchildren : length children = S (length keys)), wf_btree_aux (split (BNode keys children) i) \/ wf_btree_over (split (BNode keys children) i).
Lemma split_wf' : forall keys children i c (Hc : nth_error children i = Some c) (Hover : wf_btree_over c) (Hrest : Forall wf_btree_aux (remove_at i children)) (Hkeys : (length keys <= 2 * d)%nat) (Hchildren : length children = S (length keys)), wf_btree (split (BNode keys children) i) \/ wf_btree_over (split (BNode keys children) i).
Lemma split_contents : forall t i, contents (split t i) = contents t.
Lemma remove_replace : forall {A} i (x : A) l, (i <= length l)%nat -> remove_at i (replace i x l) = remove_at i l.
Lemma remove_In : forall {A} (x : A) i l, In x (remove_at i l) -> In x l.
Lemma Forall_remove : forall {A} P i (l : list A), Forall P l -> Forall P (remove_at i l).
Lemma insert_at_wf : forall v p t t' (Hinsert : insert_at v t p = Some t') (Hwf : wf_btree_aux t) (Hp : leaf_path t (removelast p)), wf_btree_aux t' \/ wf_btree_over t'.