注射策略可以修改最终目标,还是添加无关紧要的假设?

时间:2018-10-14 14:09:30

标签: coq ltac

考虑以下开发过程,它是Adam Chlipala's simplHyp的独立部分:

(** Fail if H is in context *)
Ltac notInCtx H := assert H; [ assumption | fail 1 ] || idtac.

Ltac injectionInCtx :=
  match goal with
  (* Is matching on G strictly necessary? *)
  | [ H : ?F ?X = ?F ?Y |- ?G ] =>
    (* fail early if it wouldn't progress *)
    notInCtx (X = Y); 
    injection H;
    match goal with
      (* G is used here *)
      | [ |- X = Y -> G ] =>
        try clear H; intros; try subst
    end
  end.

Goal forall (x y : nat), S x = S y -> x = y.
  intros x y H.
  injectionInCtx.
  exact eq_refl.
Qed.

查看内联注释-G一开始就已匹配,最终用于验证最终目标是否保持不变。这是为了排除injection H可能修改目标或添加无关紧要的假设的可能性吗?

1 个答案:

答案 0 :(得分:1)

我认为您无法修改G,但是您可以提出一个假设,injection会产生一个以上的相等性。

我们定义的injectionInCtx2injectionInCtx相同,只是它不使用G

Ltac injectionInCtx2 :=
  match goal with
  | [ H : ?F ?X = ?F ?Y |- _ ] =>
    (* fail early if it wouldn't progress *)
    notInCtx (X = Y);
    injection H;
    match goal with
    | [ |- X = Y -> _ ] =>
      try clear H; intros; try subst
    end
  end.

Definition make_pair {A} (n:A) := (n, n).

Goal forall (x y : nat), make_pair x = make_pair y -> x = y.
Proof.
  intros x y H.
  (* [injection H] gives [x = y -> x = y -> x = y] *)
  Fail injectionInCtx.
  injectionInCtx2.
  reflexivity.
Qed.