Inductive bin : Type :=
| Z
| B0 (n : bin)
| B1 (n : bin).
Fixpoint incr (m:bin) : bin :=
match m with
| Z => B1 Z
| B0 m' => B1 m'
| B1 m' => B0 (incr m')
end.
Fixpoint bin_to_nat (m:bin) : nat :=
match m with
| Z => O
| B0 m' => 2 * (bin_to_nat m')
| B1 m' => 1 + 2 * (bin_to_nat m')
end.
(*  Prove that the following diagram commutes: incr bin ----------------------> bin |                           | bin_to_nat |                           |  bin_to_nat |                           | v                           v nat ----------------------> nat  *)
Fixpoint nat_to_bin (n:nat) : bin :=
match n with
| O => Z
| S n' => incr (nat_to_bin n')
end.

Theorem nat_bin_nat : forall n, bin_to_nat (nat_to_bin n) = n.