Section Ptree.
Context {A : Type} {sA : Serializer A}.
Fixpoint tree_of_ptree (t : PositiveMap.t A) : tree (option A) :=
match t with
| PositiveMap.Leaf _ => atom None
| PositiveMap.Node t1 v t2 =>
node [tree_of_ptree t1; atom v; tree_of_ptree t2]
end.
Fixpoint ptree_of_tree (t : tree (option A)) : option (PositiveMap.t A) :=
match t with
| atom None => Some (PositiveMap.Leaf A)
| node [t1; atom v; t2] =>
match ptree_of_tree t1, ptree_of_tree t2 with
| Some pt1, Some pt2 =>
Some (PositiveMap.Node pt1 v pt2)
| _, _ => None
end
| _ => None
end.
Proof using.
induction t using PositiveMap.tree_ind; auto.
simpl.
rewrite IHt1.
rewrite IHt2.
reflexivity.
Qed.
Definition ptree_serialize (t : PositiveMap.t A) : IOStreamWriter.t :=
serialize (tree_of_ptree t).
Definition ptree_deserialize : ByteListReader.t (PositiveMap.t A) :=
t <- deserialize;;
match ptree_of_tree t with
| Some pt => ByteListReader.ret pt
| None => ByteListReader.error
end.

Lemma ptree_serialize_deserialize_id :
serialize_deserialize_id_spec ptree_serialize ptree_deserialize.