[英]Coq: Proving that the product of n and (S n) is even
鑒於程序even
,我想證明所有自然數n
even (n * (S n)) = true
。
使用感應,這是很容易看到是true
的情況下n = 0
。 然而,情況(S n) * (S (S n))
難以簡化。
我已經考慮過證even (m * n) = even m /\\ even n
的引理,但這似乎並不容易。
而且,很容易看出, even n = true
iff。 even (S n) = false
。
Fixpoint even (n: nat) : bool :=
match n with
| O => true
| 1 => false
| S (S n') => even n'
end.
有人可以暗示如何使用Coq的“初學者”子集來證明這一點嗎?
這是一種更先進的感應原理可能有用的情況。 在這個答案中簡要描述了它 。
Require Import Coq.Arith.Arith.
Require Import Coq.Bool.Bool.
Lemma pair_induction (P : nat -> Prop) :
P 0 -> P 1 -> (forall n, P n -> P (S n) -> P (S (S n))) ->
forall n, P n.
Proof.
intros ? ? ? n. enough (P n /\ P (S n)) by tauto.
induction n; intuition.
Qed.
現在,讓我們定義幾個輔助引理。 它們很明顯,可以使用pair_induction
原理和一些證明自動化輕松證明。
Lemma even_mul2 : forall n, Nat.even (2 * n) = true.
Proof.
induction n; auto.
now replace (2 * S n) with (2 + 2 * n) by ring.
Qed.
Lemma even_add_even : forall m n,
Nat.even m = true ->
Nat.even (m + n) = Nat.even n.
Proof.
now induction m using pair_induction; auto.
Qed.
Lemma even_add_mul2 : forall m n,
Nat.even (2 * m + n) = Nat.even n.
Proof.
intros; apply even_add_even, even_mul2.
Qed.
Lemma even_S : forall n,
Nat.even (S n) = negb (Nat.even n).
Proof.
induction n; auto.
simpl (Nat.even (S (S n))). (* not necessary -- just to make things clear *)
apply negb_sym. assumption.
Qed.
以下引理顯示了even
在乘法中如何“分布”。 它在證明我們的主要目標方面發揮了重要作用。 因為幾乎總是泛化有很大幫助。
Lemma even_mult : forall m n,
Nat.even (m * n) = Nat.even m || Nat.even n.
Proof.
induction m using pair_induction; simpl; auto.
intros n. replace (n + (n + m * n)) with (2 * n + m * n) by ring.
now rewrite even_add_mul2.
Qed.
現在,目標的證明是微不足道的
Goal forall n, Nat.even (n * (S n)) = true.
intros n. now rewrite even_mult, even_S, orb_negb_r.
Qed.
有人可以暗示如何使用Coq的“初學者”子集來證明這一點嗎?
您可以將此視為提示,因為它揭示了可能的證明的一般結構。 自動戰術可以用“手動”策略取代,如rewrite
, apply
, destruct
等。
我想使用mathcomp lib提供更短的證明:
From mathcomp Require Import all_ssreflect all_algebra.
Lemma P n : ~~ odd (n * n.+1).
Proof. by rewrite odd_mul andbN. Qed.
odd_mul
通過簡單歸納證明,以及odd_add
。
另一個版本,本着@ ejgallego的回答。 讓我們給出偶數謂詞的另一個定義。 這樣做的目的是通過簡單的感應簡單地進行校樣,因此不需要使用pair_induction
。 主要思想是我們將證明even2
一些屬性然后我們將使用Nat.even
和even2
在擴展上等於將even2
的屬性轉移到even2
上的Nat.even
。
Require Import Coq.Bool.Bool.
Fixpoint even2 (n : nat) : bool :=
match n with
| O => true
| S n' => negb (even2 n')
end.
讓我們看看Nat.even
和even2
在擴展上是相等的。
Lemma even_S n :
Nat.even (S n) = negb (Nat.even n).
Proof. induction n; auto. apply negb_sym; assumption. Qed.
Lemma even_equiv_even2 n :
Nat.even n = even2 n.
Proof. induction n; auto. now rewrite even_S, IHn. Qed.
even2
一些分布引理:
Lemma even2_distr_add m n :
even2 (m + n) = negb (xorb (even2 m) (even2 n)).
Proof.
induction m; simpl.
- now destruct (even2 n).
- rewrite IHm. now destruct (even2 m); destruct (even2 n).
Qed.
Lemma even2_distr_mult m n :
even2 (m * n) = even2 m || even2 n.
Proof.
induction m; auto; simpl.
rewrite even2_distr_add, IHm.
now destruct (even2 m); destruct (even2 n).
Qed.
最后,我們能夠使用Nat.even
和even2
之間的平等來證明我們的目標。
Goal forall n, Nat.even (n * (S n)) = true.
intros n.
now rewrite even_equiv_even2, even2_distr_mult, orb_negb_r.
Qed.
使用標准庫的簡短版本:
Require Import Coq.Arith.Arith.
Goal forall n, Nat.even (n * (S n)) = true.
intros n.
now rewrite Nat.even_mul, Nat.even_succ, Nat.orb_even_odd.
Qed.
對於它的價值,這是我對解決方案的看法。 基本思想是,而不是證明謂詞P n
,而是證明P n /\\ P (S n)
,它是等價的,但第二種表述允許使用簡單的歸納法。
這是完整的證據:
Require Import Nat.
Require Import Omega.
Definition claim n := even (n * (S n)) = true.
(* A technical Lemma, needed in the proof *)
Lemma tech: forall n m, even n = true -> even (n + 2*m) = true.
Proof.
intros. induction m.
* simpl. replace (n+0) with n; intuition.
* replace (n + 2 * S m) with (S (S (n+2*m))); intuition.
Qed.
(* A simple identity, that Coq needs help to prove *)
Lemma ident: forall n,
(S (S n) * S (S (S n))) = (S n * S( S n) + 2*(S (S n))).
(* (n+2)*(n+3) = (n+1)*(n+2) + 2*(n+2) *)
Proof.
intro.
replace (S (S (S n))) with ((S n) + 2) by intuition.
remember (S (S n)) as m.
replace (m * (S n + 2)) with ((S n + 2) * m) by intuition.
intuition.
Qed.
(* The claim to be proved by simple induction *)
Lemma nsn: forall n, claim n /\ claim (S n).
Proof.
intros.
unfold claim.
induction n.
* intuition.
* intuition. rewrite ident. apply tech; auto.
Qed.
(* The final result is now a simple corollary *)
Theorem thm: forall n, claim n.
Proof.
apply nsn.
Qed.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.