Section BOOLEAN_EQUALITY.
Variable A: Type.
Variable beqA: A -> A -> bool.
Fixpoint bempty (m: t A) : bool :=
match m with
| Leaf => true
| Node l None r => bempty l && bempty r
| Node l (Some _) r => false
end.
Fixpoint beq (m1 m2: t A) {struct m1} : bool :=
match m1, m2 with
| Leaf, _ => bempty m2
| _, Leaf => bempty m1
| Node l1 o1 r1, Node l2 o2 r2 =>
match o1, o2 with
| None, None => true
| Some y1, Some y2 => beqA y1 y2
| _, _ => false
end
&& beq l1 l2 && beq r1 r2
end.
End BOOLEAN_EQUALITY.
Fixpoint prev_append (i j: positive) {struct i} : positive :=
match i with
| xH => j
| xI i' => prev_append i' (xI j)
| xO i' => prev_append i' (xO j)
end.
Definition prev (i: positive) : positive :=
prev_append i xH.
Proof (prev_append_prev i xH).
Fixpoint xmap (A B : Type) (f : positive -> A -> B) (m : t A) (i : positive)
{struct m} : t B :=
match m with
| Leaf => Leaf
| Node l o r => Node (xmap f l (xO i))
(match o with None => None | Some x => Some (f (prev i) x) end)
(xmap f r (xI i))
end.
Definition map (A B : Type) (f : positive -> A -> B) m := xmap f m xH.

Lemma xgmap:
forall (A B: Type) (f: positive -> A -> B) (i j : positive) (m: t A),
get i (xmap f m j) = option_map (f (prev (prev_append i j))) (get i m).