首页 > 解决方案 > Coq:在 if-then-else 下重写

问题描述

我有时需要在 if-then-else 的一个分支中应用简化而不破坏被歧视者。

From Coq Require Import Setoid.

Lemma true_and :
  forall P, True /\ P <-> P.
Proof.
  firstorder.
Qed.

Goal (forall (b:bool) P Q, if b then True /\ P else Q).
  intros.
  Fail rewrite (true_and P).
Abort.

在这个例子中rewrite失败(setoid_rewritetoo),建议注册如下

为什么重写引擎要求如此之高?

标签: coqcoq-tacticrewriting

解决方案


我认为重写策略失败了,因为你想要的不是重写,而是减少(True /\ P技术上不等于P)。如果您想保持if then else声明,我会建议以下解决方案(但有点不满意):

Tactics.v文件中,添加以下引理和 ltacs :

Lemma if_then_else_rewrite:
  forall (b: bool) (T1 T2 E1 E2 : Prop),
    (T1 -> T2) ->
    (E1 -> E2) ->
    (if b then T1 else E1) ->
    (if b then T2 else E2).
Proof.
  destruct b; auto.
Qed.

Ltac ite_app1 Th :=
  match goal with
  | H:_ |- if ?b then ?T2 else ?E =>
    eapply if_then_else_rewrite with (E1 := E) (E2 := E);
    [eapply Th | eauto |]
   end.


Ltac ite_app2 Th :=
  match goal with
  | H:_ |- if ?b then ?T else ?E2 =>
    eapply if_then_else_rewrite with (T1 := T) (T2 := T);
    [eauto | eapply Th |]
   end.

然后,当您有一个顶级的目标时if then else,您可以使用 或 将定理Th应用于左侧或ite_app1 Th右侧ite_app2 Th。例如,您的目标是:

Goal (forall (b:bool) P Q, if b then (True /\ P) else Q).
  intros.
  ite_app1 true_and. (* -> if b then P else Q *)

您可能需要根据您的需要微调 ltacs(应用于上下文与目标等),但这是第一个解决方案。也许有人可以带来更多的 ltac 黑暗魔法,以更通用的方式解决这个问题。


推荐阅读