Section Store.
Variable A:Type.
Notation Poption := (Poption A).
Notation Tree := (Tree A).
Fixpoint Tget (p:positive) (T:Tree) {struct p} : Poption :=
match T with
Tempty => PNone
| Branch0 T1 T2 =>
match p with
xI pp => Tget pp T2
| xO pp => Tget pp T1
| xH => PNone
end
| Branch1 a T1 T2 =>
match p with
xI pp => Tget pp T2
| xO pp => Tget pp T1
| xH => PSome a
end
end.
Fixpoint Tadd (p:positive) (a:A) (T:Tree) {struct p}: Tree :=
match T with
| Tempty =>
match p with
| xI pp => Branch0 Tempty (Tadd pp a Tempty)
| xO pp => Branch0 (Tadd pp a Tempty) Tempty
| xH => Branch1 a Tempty Tempty
end
| Branch0 T1 T2 =>
match p with
| xI pp => Branch0 T1 (Tadd pp a T2)
| xO pp => Branch0 (Tadd pp a T1) T2
| xH => Branch1 a T1 T2
end
| Branch1 b T1 T2 =>
match p with
| xI pp => Branch1 b T1 (Tadd pp a T2)
| xO pp => Branch1 b (Tadd pp a T1) T2
| xH => Branch1 a T1 T2
end
end.
Definition mkBranch0 (T1 T2:Tree) :=
match T1,T2 with
Tempty ,Tempty => Tempty
| _,_ => Branch0 T1 T2
end.
Fixpoint Tremove (p:positive) (T:Tree) {struct p}: Tree :=
match T with
| Tempty => Tempty
| Branch0 T1 T2 =>
match p with
| xI pp => mkBranch0 T1 (Tremove pp T2)
| xO pp => mkBranch0 (Tremove pp T1) T2
| xH => T
end
| Branch1 b T1 T2 =>
match p with
| xI pp => Branch1 b T1 (Tremove pp T2)
| xO pp => Branch1 b (Tremove pp T1) T2
| xH => mkBranch0 T1 T2
end
end.
destruct p;reflexivity.
Qed.

Theorem Tget_Tadd: forall i j a T,
Tget i (Tadd j a T) =
match (i ?= j) with
Eq => PSome a
| Lt => Tget i T
| Gt => Tget i T
end.