繁体   English   中英

Coq:如何证明max ab <= a + b?

[英]Coq: How to prove max a b <= a+b?

我无法使用coq的策略证明简单的逻辑max ab <= a+b 我应该如何解决呢? 以下是我到目前为止一直在使用的代码。 s_le_n已被证明,但为简单起见此处未提及。

Theorem s_le_n: forall (a b: nat),  a <= b -> S a <= S b.
Proof. Admitted.

Theorem max_sum: forall (a b: nat), max a b <= a + b.
Proof. 
intros.
induction a.
- simpl. reflexivity.
- rewrite plus_Sn_m. induction b.
  + simpl. rewrite <- plus_n_O. reflexivity.
  + rewrite <- plus_Sn_m. simpl. apply s_le_n. rewrite IHa.

考虑到@ re3el的评论,我们从其“笔和纸证明”开始:

if a>b max a b = a, a < a+b; else max a b = b, b < a+b

现在让我们将其翻译成Coq! 实际上,我们要做的第一件事是确定<的可判定性,这是使用le_lt_dec ab引理完成的。 其余的是常规的:

Require Import Arith.

Theorem max_sum (a b: nat) : max a b <= a + b.
Proof.
case (le_lt_dec a b).
+ now rewrite <- Nat.max_r_iff; intros ->; apply le_plus_r.
+ intros ha; apply Nat.lt_le_incl, Nat.max_l_iff in ha.
  now rewrite ha; apply le_plus_l.
Qed.

但是,我们可以大大改善这一证明。 有各种各样的候选人,使用stdlib的一个不错的候选人是:

Theorem max_sum_1 (a b: nat) : max a b <= a + b.
Proof.
now rewrite Nat.max_lub_iff; split; [apply le_plus_l | apply le_plus_r].
Qed.

使用我选择的库[math-comp],可以链接重写以获得更紧凑的证明:

From mathcomp Require Import all_ssreflect.

Theorem max_sum_2 (a b: nat) : maxn a b <= a + b.
Proof. by rewrite geq_max leq_addl leq_addr. Qed.

实际上,从简短的证明来看,也许甚至根本不需要原始引理。

编辑:@Jason Gross提到了另一种更经验丰富的证明方式:

Proof. apply Max.max_case_strong; omega. Qed.

但是,该证明涉及使用重量级自动化策略omega 我强烈建议所有初学者暂时避免使用这种策略,并学习如何更“手动”地进行证明。 实际上,使用任何启用SMT的策略,只需调用SMT即可简单地解决原始目标。

暂无
暂无

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

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