在战术/战术符号内匹配上下文模式

时间:2019-01-07 20:13:31

标签: coq coq-tactic

我通过战术在目标内找到了模式。

这为什么失败:

        Tactic Notation "my_context_match" uconstr(g) :=
          match goal with
          | |- context[g] => idtac
          end.

          my_context_match _.

这成功了吗?

        match goal with
          | |- context[_] => idtac
        end.

有什么方法可以写my_context_match,以便我可以传递不完整的模式(上面带有_),看看我的目标内是否有与模式匹配的东西?

1 个答案:

答案 0 :(得分:2)

uconstr的支持非常零散。我刚刚报告了#9321。请注意,即使这样也会失败:

Goal True.
  let v := uconstr:(True) in
  lazymatch constr:(v) with
  | v => idtac
  end. (* Error: No matching clauses for match. *)

如@eponier在评论中所建议,您可以使用open_constr代替uconstr。但是,这将留下未解决的evar。这是一种行之有效的策略,不会留下未解决的evar:

Tactic Notation "my_context_match" uconstr(g) :=
  (* [match] does not support [uconstr], cf COQBUG(https://github.com/coq/coq/issues/9321),
     so we use [open_constr] *)
  let g := open_constr:(g) in
  (* turning [g] into an [open_constr] creates new evars, so we must
     eventually unify them with the goal *)
  let G := match goal with |- ?G => G end in
  (* We now search for [g] in the goal, and then replace the matching
     subterm with the [open_constr] [g], so that we can unify the
     result with the goal [G] to resolve the new evars we created *)
  match G with
  | context cG[g]
    => let G' := context cG[g] in
       unify G G'
  end.

Goal True /\ True.
  my_context_match _.
  my_context_match (_ /\ _).
  Fail my_context_match (_ \/ _).
  my_context_match True.
  exact (conj I I).
Qed.