Fixpoint xelements (A : Type) (m : t A) (i : positive)
(k: list (positive * A)) {struct m}
: list (positive * A) :=
match m with
| Leaf => k
| Node l None r =>
xelements l (xO i) (xelements r (xI i) k)
| Node l (Some x) r =>
xelements l (xO i)
((prev i, x) :: xelements r (xI i) k)
end.
Definition elements (A: Type) (m : t A) := xelements m xH nil.
Remark xelements_append:
forall A (m: t A) i k1 k2,
xelements m i (k1 ++ k2) = xelements m i k1 ++ k2.
Remark xelements_leaf:
forall A i, xelements (@Leaf A) i nil = nil.
Remark xelements_node:
forall A (m1: t A) o (m2: t A) i,
xelements (Node m1 o m2) i nil =
xelements m1 (xO i) nil
++ match o with None => nil | Some v => (prev i, v) :: nil end
++ xelements m2 (xI i) nil.

(* Specifications: *)
Lemma xelements_incl:
forall (A: Type) (m: t A) (i : positive) k x,
In x k -> In x (xelements m i k).
Lemma xelements_correct:
forall (A: Type) (m: t A) (i j : positive) (v: A) k,
get i m = Some v -> In (prev (prev_append i j), v) (xelements m j k).
Theorem elements_correct:
forall (A: Type) (m: t A) (i: positive) (v: A),
get i m = Some v -> In (i, v) (elements m).
Lemma in_xelements:
forall (A: Type) (m: t A) (i k: positive) (v: A) ,
In (k, v) (xelements m i nil) ->
exists j, k = prev (prev_append j i) /\ get j m = Some v.
Theorem elements_complete:
forall (A: Type) (m: t A) (i: positive) (v: A),
In (i, v) (elements m) -> get i m = Some v.