简体   繁体   English

Coq初学者-证明基本引理

[英]Coq beginner - Prove a basic lemma

I'm a beginner with Coq so maybe my question will seems to be a dumb question, but here is my problem : 我是Coq的初学者,所以也许我的问题似乎是一个愚蠢的问题,但这是我的问题:

I defined a simple module in which I defined a type T and a function "my_custom_equal" : 我定义了一个简单的模块,其中定义了类型T和函数“ my_custom_equal”:

  Definition T := nat.

  Fixpoint my_custom_equal (x y : T) :=
    match x, y with
      | O, O => true
      | O, S _ => false
      | S _, O => false
      | S sub_x, S sub_y => my_custom_equal sub_x sub_y
    end.

  Lemma my_custom_reflex : forall x : T, my_custom_equal x x = true.
  Proof.
    intros.
    induction x.
    simpl.
    reflexivity.
    simpl.
    rewrite IHx.
    reflexivity.
  Qed.

  Lemma my_custom_unicite : forall x y : T, my_custom_equal x y = true -> x = y.
  Proof.
    intros.
    induction x.
    induction y.
    reflexivity.
    discriminate.

  Qed.

As you can see, it is not really complicated but I still got stuck on the my_custom_unicite proof, I always reach the point where I need to prove that "S x = y" and my hypothesis are only : 如您所见,它并不是很复杂,但是我仍然受my_custom_unicite证明的困扰,总是达到需要证明“ S x = y”且我的假设仅是:

y : nat
H : my_custom_equal 0 (S y) = true
IHy : my_custom_equal 0 y = true -> 0 = y
______________________________________(1/1)
S x = y

I don't understand how to achieve this proof, could you help me ? 我不知道该如何获得证明,您能帮我吗?

Thanks! 谢谢!

This is a typical trap for beginners. 这是初学者的典型陷阱。 The problem is that you performed induction on x when y was already introduced in your context. 问题是当上下文中已经引入y时,您对x进行了归纳。 Because of that, the induction hypothesis that you obtain is not sufficiently general: what you really want is to have something like 因此,您获得的归纳假设还不够笼统:您真正想要的是拥有类似

forall y, my_custom_equal x y = true -> x = y

Notice the extra forall . 注意额外的forall The solution is to put y back into your goal: 解决方案是将y重新投入您的目标:

Lemma my_custom_unicite : forall x y, my_custom_equal x y = true -> x = y.
Proof.
intros x y. revert y.
induction x as [|x IH].
- intros []; easy.
- intros [|y]; try easy.
  simpl.
  intros H.
  rewrite (IH y H).
  reflexivity.
Qed.

Try running this proof interactively and check how the induction hypothesis changes when you reach the second case. 尝试以交互方式运行此证明,并检查到达第二种情况时的归纳假设如何变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM