Section Fold.
Variables A B : Type.
Variable f : key -> A -> B -> B.
Fixpoint xfoldi (m : t A) (v : B) (i : key) :=
match m with
| Leaf _ => v
| Node l (Some x) r =>
xfoldi r (f i x (xfoldi l v (append i 2))) (append i 3)
| Node l None r =>
xfoldi r (xfoldi l v (append i 2)) (append i 3)
end.
Definition fold m i := xfoldi m i 1.
End Fold.

Lemma fold_1 :
forall (A:Type)(m:t A)(B:Type)(i : B) (f : key -> A -> B -> B),
fold f m i = fold_left (fun a p => f (fst p) (snd p) a) (elements m) i.