Coq - 证明已经定义的东西?

时间:2018-05-04 11:22:21

标签: coq proof

以非常简单的证明"如果其中一个是偶数而另一个是奇数,则两个自然的总和是奇数:

Require Import Arith.
Require Import Coq.omega.Omega.

Definition even (n: nat) := exists k, n = 2 * k.
Definition odd  (n: nat) := exists k, n = 2 * k + 1.

Lemma sum_odd_even : forall n m, odd (n + m) -> odd n /\ even m \/ even n /\ odd m.
Proof.
  intros n. intros m. left.
  destruct H. firstorder.

此代码块末尾的状态为:

2 subgoals
n, m, x : nat
H : n + m = 2 * x + 1
______________________________________(1/2)
odd n
______________________________________(2/2)
even m

根据我的理解,它告诉我,我需要证明我在假设中有一个奇数n和一个偶数m?虽然我已经说过,但是n是奇数而m是偶数?我该如何从这里开始?

更新:

经过一番烦躁(根据评论),我想我必须做这样的事情?

Lemma even_or_odd: forall (n: nat), even n \/ odd n.
Proof.
  induction n as [|n IHn].
  (* Base Case *)
  left. unfold even. exists 0. firstorder.
  (* step case *)
  destruct IHn as [IHeven | IHodd].
  right. unfold even in IHeven. destruct IHeven as [k Heq].
  unfold odd. exists k. firstorder.
  left. unfold odd in IHodd. destruct IHodd as [k Heq].
  unfold even. exists (k + 1). firstorder.
Qed.

现在意味着:

Lemma sum_odd : forall n m, odd (n + m) -> odd n /\ even m \/ even n /\ odd m.
Proof.
  intros n. intros m. left. destruct H. firstorder.
  pose proof (even_or_odd n). pose proof (even_or_odd m).

结果:

    2 subgoals
n, m, x : nat
H : n + m = 2 * x + 1
H0 : even n \/ odd n
H1 : even m \/ odd m
______________________________________(1/2)
odd n
______________________________________(2/2)
even m

直观地说,我所做的就是说每个数字都是偶数或奇数。现在我必须告诉coq我的奇数和偶数确实是奇数和偶数(我猜?)。

更新2:

顺便说一句,这个问题只能用firstorder解决:

Lemma sum_odd : forall n m, odd (n + m) -> odd n /\ even m \/ even n /\ odd m.
Proof.
  intros n. intros m. firstorder.
  pose proof (even_or_odd n). pose proof (even_or_odd m).
  destruct H0 as [Even_n | Odd_n]. destruct H1 as [Even_m | Odd_m].
  exfalso. firstorder.
  right. auto.
  destruct H1. left. auto.
  exfalso. firstorder.
Qed.

1 个答案:

答案 0 :(得分:5)

您对left的使用仍然不正确,使您无法完成证明。您将其应用于以下目标:

odd (n + m) -> odd n /\ even m \/ even n /\ odd m

它给出了:

H : odd (n + m)
______________________________________(1/1)
odd n /\ even m

您承诺证明如果n + m为奇数,则n为奇数且m为偶数。但事实并非如此:{{ 1}}可能是奇数,n可能是偶数。只有在上下文中有足够的信息后才能应用mleft,以确定您要证明哪一个。

所以让我们在没有right的情况下重启:

left

此时我们在:

Lemma sum_odd : forall n m, odd (n + m) -> odd n /\ even m \/ even n /\ odd m.
Proof.
  intros n. intros m. firstorder.
  pose proof (even_or_odd n). pose proof (even_or_odd m).

现在你想从分离中证明一些东西。为了在Coq的构造逻辑中证明H : n + m = 2 * x + 1 H0 : even n \/ odd n H1 : even m \/ odd m ______________________________________(1/1) odd n /\ even m \/ even n /\ odd m 形式的某些内容,您必须证明 A \/ B -> CA -> C。您可以通过B -> C(使用A \/ B或其他策略)的案例分析来完成此操作。在这种情况下,我们有两个分解的分离:

destruct

这给出了四个案例。我将向您展示前两个,最后两个是对称的。

拳头案例:

  destruct H0 as [Even_n | Odd_n], H1 as [Even_m | Odd_m].

假设是矛盾的:如果H : n + m = 2 * x + 1 Even_n : even n Even_m : even m ______________________________________(1/1) odd n /\ even m \/ even n /\ odd m n都是偶数,则m无法成立。我们可以证明如下:

H

(通过这一步来了解会发生什么!) - exfalso. destruct Even_n, Even_m. omega. 并不是真的必要,但它是一个很好的文档,我们通过证明假设相矛盾来做证明。

第二种情况:

exfalso

现在,了解适用于此案例的假设,我们可以提交正确分离。这就是你H : n + m = 2 * x + 1 Even_n : even n Odd_m : odd m ______________________________________(1/1) odd n /\ even m \/ even n /\ odd m 阻止你取得进步的原因!

left

还有待证明的是:

  - right.

Even_n : even n Odd_m : odd m ______________________________________(1/1) even n /\ odd m 可以解决这个问题。