Section Trec.
Fixpoint nat_of_pos p0 :=
match p0 with
| xO p => (nat_of_pos p).*2
| xI p => (nat_of_pos p).*2.+1
| xH   => 1
end.
End Trec.
Local Coercion nat_of_pos : positive >-> nat.
Coercion nat_of_bin b := if b is Npos p then p : nat else 0.
Fixpoint pos_of_nat n0 m0 :=
match n0, m0 with
| n.+1, m.+2 => pos_of_nat n m
| n.+1,    1 => xO (pos_of_nat n n)
| n.+1,    0 => xI (pos_of_nat n n)
|    0,    _ => xH
end.
Definition bin_of_nat n0 := if n0 is n.+1 then Npos (pos_of_nat n n) else 0%num.

Lemma nat_of_binK : cancel nat_of_bin bin_of_nat.