如何在当前目标的子表达式上使用重写

时间:2012-11-30 13:13:29

标签: coq

在coq中,是否有可能将引理或假设应用于当前目标的子表达式?例如,我想在这个例子中应用加上交换的事实来交换3和4。

Require Import Coq.Arith.Plus.

Inductive foobar : nat -> Prop :=
 | foob : forall n:nat, foobar n.

Goal foob (3 + 4) = foob (4 + 3).
Proof.
apply plus_comm. (* or maybe rewrite plus_comm *)

给出:

Error: Impossible to unify "?199 + ?200 = ?200 + ?199" with
"foob (3 + 4) = foob (4 + 3)".

如何判断coq在此目标中的确切位置应用plus_comm?

2 个答案:

答案 0 :(得分:4)

使用策略simpl有效,但不要问我为什么rewrite plus_commrewrite (plus_comm 3 4)不起作用。 apply是为了暗示,而不是等式。

答案 1 :(得分:0)

在这种特殊情况下,如果您使用构造函数参数而不是索引来定义归纳类型,那么rewrite将起作用:

Inductive foobar : Type :=
    | foob (n : nat).

由于这种类型只有一个构造函数,因此我的理解(from this answer)是,使用索引不会带来任何好处,但会使Coq很难进行模式匹配。

通常,以下任何一种技术都可以达到目标rewrite的效果:

断言

assert (H: 3 + 4 = 4 + 3). { rewrite <- plus_comm. reflexivity. }

重写

(* introduce a new sub-goal where you can prove that the replacement is OK *)
replace (3 + 4) with (4 + 3).
(* Coq orders the new goal last. I think it's clearer to tackle it first *)
Focus 2.
(* do the rewrite *)
rewrite <- plus_comm. reflexivity.
相关问题