I am a green hand in studying Coq with the reference book softwarefoundation-induction
In the last part of this phrase, there is an exercise about proving that
change a binary to a nature number and change it back to binary is equivalence to itself.
which is the last theorem bin_nat_bin
Inductive bin : Type :=
| Z
| B0 (n : bin)
| B1 (n : bin).
Fixpoint bin_to_nat (m:bin) : nat :=
match m with
| Z => 0
| B0 (m') => 2 * bin_to_nat(m')
| B1 (m') => S (2 * bin_to_nat(m'))
end.
Fixpoint nat_to_bin (n:nat) : bin :=
match n with
| 0 => Z
| S n' => incr (nat_to_bin n')
end.
Fixpoint normalize (b:bin) : bin :=
match b with
| Z => Z
| B0 b' =>
match (normalize b') with
| Z => Z
| b'' => B0 b''
end
| B1 b' => B1 (normalize b')
end.
Theorem bin_nat_bin : forall b : bin, nat_to_bin (bin_to_nat b) = normalize b.
Proof.
(* FILL IN HERE *) Admitted.
At first, I tried to induction b as follows.
Theorem bin_nat_bin : forall b, nat_to_bin (bin_to_nat b) = normalize b.
Proof.
intros b. induction b as [|b'|b''].
- simpl. reflexivity.
- simpl. rewrite add_0_r. simpl.
And get these
1 goal
b' : bin
IHb' : nat_to_bin (bin_to_nat b') = normalize b'
______________________________________(1/1)
nat_to_bin (bin_to_nat b' + bin_to_nat b') =
match normalize b' with
| Z => Z
| B0 n => B0 (B0 n)
| B1 n => B0 (B1 n)
end
I want to know that to proof this subgoal,
if I need to change the '+' between bin_to_nat b' into a construct function double_bin
Definition double_bin (b:bin) : bin :=
match b with
| Z => Z
| m => B0 (m)
end.
and bridge the gap between them (I don't know clearly how to do this. Please give me some tips)
or to do as the hint put it
Hint: Start by trying to prove the main statement, see where you get stuck, and see if you can find a lemma -- perhaps requiring its own inductive proof -- that will allow the main proof to make progress. We have one lemma for the B0 case (which also makes use of double_incr_bin) and another for the B1 case.
Could you please offer me some more detailed or practical measures or hints?
Thank you.
It's better to define
normalizewithdouble_binandincr. We can then argue the property is hold while converting from nature number to binary, likeforall m , nat_to_bin (m + m) = double_bin (nat_to_bin m).