Coq 如何显示如果平方相等,那么操作数也相等?
我在完成这个引理的证明时遇到了一个问题:Coq 如何显示如果平方相等,那么操作数也相等?,coq,Coq,我在完成这个引理的证明时遇到了一个问题: Lemma l1 : forall m n : nat, m * m = n * n -> m = n. 任何暗示都会很有帮助 我是这样开始证明的: Require Import Arith Omega Nat. Lemma l1 : forall m n : nat, m * m = n * n -> m = n. Proof. intros. destruct (Nat.eq_dec m n ). trivial. inductio
Lemma l1 : forall m n : nat, m * m = n * n -> m = n.
任何暗示都会很有帮助
我是这样开始证明的:
Require Import Arith Omega Nat.
Lemma l1 : forall m n : nat, m * m = n * n -> m = n.
Proof.
intros.
destruct (Nat.eq_dec m n ).
trivial.
induction n.
induction m.
auto.
simpl in H;congruence.
提示:取假设两边的平方根,然后立即得出结论。对于平方根函数,使用Arith模块中的Nat.sqrt
第一个解决方案:
第二种解决方案:
还可以使用nia策略证明引理,nia策略是整数非线性算法的不完整证明程序:
Require Import Psatz.
Lemma l1 m n : m * m = n * n -> m = n.
Proof. nia. Qed.
第三种解决方案:
让我们使用标准Nat.square\u le\u Siml\u nonneg引理的两倍:
第四种解决办法:
这是一个经典的证明,基于以下等式
m * m - n * n = (m + n) * (m - n)
首先,我们需要一个helper引理来证明上述等式,令人惊讶的是,标准库似乎缺少这个引理:
Require Import Coq.Arith.Arith.
(* can be proved using `nia` tactic *)
Lemma sqr_diff (m n : nat) :
m * m - n * n = (m + n) * (m - n).
Proof with (auto with arith).
destruct (Nat.lt_trichotomy m n) as [H | [H | H]].
- pose proof H as H'. (* copy hypothesis *)
apply Nat.square_lt_mono_nonneg in H...
repeat match goal with
h : _ < _ |- _ => apply Nat.lt_le_incl, Nat.sub_0_le in h
end.
rewrite H, H'...
- now rewrite H, !Nat.sub_diag.
- rewrite Nat.mul_add_distr_r, !Nat.mul_sub_distr_l.
rewrite Nat.add_sub_assoc...
replace (n * m) with (m * n) by apply Nat.mul_comm.
rewrite Nat.sub_add...
Qed.
现在我们可以证明主要引理:
Lemma l1 (m n : nat) :
m * m = n * n -> m = n.
Proof.
intros H.
pose proof (Nat.eq_le_incl _ _ H) as Hle;
pose proof (Nat.eq_le_incl _ _ (eq_sym H)) as Hge; clear H.
rewrite <- Nat.sub_0_le in *.
rewrite sqr_diff in *.
destruct (mult_is_O _ _ Hle) as [H | H].
now destruct (plus_is_O _ _ H); subst.
destruct (mult_is_O _ _ Hge) as [H' | H'].
now destruct (plus_is_O _ _ H'); subst.
rewrite Nat.sub_0_le in *.
apply Nat.le_antisymm; assumption.
Qed.
你确定这是一个关于Coq的问题吗?你真的试过用笔和纸来证明这个引理吗?如果您有,请与我们分享您的证明的主要步骤,我们可以帮助您将其正式化。记住:这并不是因为Coq告诉你第一步是好的,你必须朝着正确的方向前进。
m * m - n * n = (m + n) * (m - n)
Require Import Coq.Arith.Arith.
(* can be proved using `nia` tactic *)
Lemma sqr_diff (m n : nat) :
m * m - n * n = (m + n) * (m - n).
Proof with (auto with arith).
destruct (Nat.lt_trichotomy m n) as [H | [H | H]].
- pose proof H as H'. (* copy hypothesis *)
apply Nat.square_lt_mono_nonneg in H...
repeat match goal with
h : _ < _ |- _ => apply Nat.lt_le_incl, Nat.sub_0_le in h
end.
rewrite H, H'...
- now rewrite H, !Nat.sub_diag.
- rewrite Nat.mul_add_distr_r, !Nat.mul_sub_distr_l.
rewrite Nat.add_sub_assoc...
replace (n * m) with (m * n) by apply Nat.mul_comm.
rewrite Nat.sub_add...
Qed.
Lemma l1 (m n : nat) :
m * m = n * n -> m = n.
Proof.
intros H.
pose proof (Nat.eq_le_incl _ _ H) as Hle;
pose proof (Nat.eq_le_incl _ _ (eq_sym H)) as Hge; clear H.
rewrite <- Nat.sub_0_le in *.
rewrite sqr_diff in *.
destruct (mult_is_O _ _ Hle) as [H | H].
now destruct (plus_is_O _ _ H); subst.
destruct (mult_is_O _ _ Hge) as [H' | H'].
now destruct (plus_is_O _ _ H'); subst.
rewrite Nat.sub_0_le in *.
apply Nat.le_antisymm; assumption.
Qed.