Prove a match statement

让人想犯罪 __ 提交于 2021-01-27 07:24:08

问题


Trying to solve an exercise, I have the following definition that represents the integers :

Inductive bin : Type :=
| Zero : bin
| Twice : bin -> bin
| TwiceOne : bin -> bin.

The idea is that :

  1. Twice x is 2*x.
  2. TwiceOne x is 2*x +1.

However, this representation has a problem: there are several representations of the number 0.

Therefore, I need to implement a function that normalize a number writing in bin.

To do this I have declared the following function :

Fixpoint normalize_bin (b:bin) : bin :=
  match b with
    | Zero => Zero
    | TwiceOne x => TwiceOne (normalize_bin x)
    | Twice x => match normalize_bin x with
                   | Zero => Zero
                   | x => Twice x
                 end
  end.

And now, I want to show that if a take a b of type bin, I translate b in nat and then I come back to bin I get the b' that is the normalized of b. This is the following theorem :

Theorem bin_to_nat_to_bin : forall (b:bin),
                              nat_to_bin (bin_to_nat b) = normalize_bin2 b.

To prove this theorem I do an induction on b. But I am getting stuck in the indu

ctive case since I need to show that:

   nat_to_bin (bin_to_nat Tb + bin_to_nat Tb) =
   match normalize_bin2 Tb with
   | Zero => Zero
   | Twice b => Twice (Twice b)
   | TwiceOne b => Twice (TwiceOne b)
   end

After using the tactic simpl.

However, I have no idea how to deal with this match in the goal. What can I do from here ?

Since this exercise come from a book, I don't have to use some advanced things of Coq. I just know some tactics like reflexivity, simpl, rewrite, destruct, assert.

A second normalization possible is just to declare this :

Definition normalize_bin (b:bin) : bin :=
  nat_to_bin (bin_to_nat b).

But in this case it is trivial and I don't think that is the answer expected.


回答1:


Since this is an exercise from Software Foundations, I won't give the full answer, but a hint.

This proof is indeed quite tricky. When you get in the middle of a complicated proof like this, it's usually a good idea to try to factor it into smaller lemmas. This in turn might require reorganizing your definitions so that the statements of these lemmas become simpler.

In your case, your definition of normalize_bin has an inner match that looks a bit too complicated, which becomes apparent once you get into that point of your proof. Can you factor that inner match in a separate definition so that you can state lemmas about that definition and use those lemmas later in your main proof?



来源:https://stackoverflow.com/questions/28504453/prove-a-match-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!