如何表明如果正方形相等,那么操作数也相等?

时间:2016-07-30 15:12:35

标签: 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.
induction n.
induction m.
auto.
simpl in H;congruence.

1 个答案:

答案 0 :(得分:3)

提示:取你假设两边的平方根,然后立即得出结论。对于平方根函数,请使用Nat.sqrt模块中的Arith

第一个解决方案

Require Import Coq.Arith.Arith.

Lemma l1 : forall m n : nat, m * m = n * n -> m = n.
Proof.
  intros m n H. apply (f_equal Nat.sqrt) in H.
  now repeat rewrite Nat.sqrt_square in H.
Qed.

第二个解决方案:

引理也可以使用nia策略证明,这是一个整数非线性算法的不完整证明程序:

Require Import Psatz.

Lemma l1 m n : m * m = n * n -> m = n.
Proof. nia. Qed.

第三个解决方案:

让我们使用标准Nat.square_le_simpl_nonneg引理的几倍:

forall n m : nat, 0 <= m -> n * n <= m * m -> n <= m

我们走了:

Require Import Coq.Arith.Arith.

Lemma l1 (m n : nat) :
  m * m = n * n -> m = n.
Proof with (auto with arith).
  intros H.
  pose proof (Nat.eq_le_incl _ _ H) as Hle.
  pose proof (Nat.eq_le_incl _ _ (eq_sym H)) as Hge.
  apply Nat.square_le_simpl_nonneg in Hle...
  apply Nat.square_le_simpl_nonneg in Hge...
Qed.

第四个解决方案:

这是一个基于以下相等的经典证明

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.