Ltac:可选的变量名称

时间:2018-03-29 14:24:31

标签: coq coq-tactic ltac

我想用可选的变量名写一个策略。最初的战术看起来像这样:

Require Import Classical.

Ltac save := 
  let H := fresh in
  apply NNPP;
  intro H;
  apply H.

我想让用户有机会选择他想要的名称并使用它,例如:save a

我使用this solution编写了一个变体:

Require Import Classical.

Inductive ltac_No_arg : Set :=
  | ltac_no_arg : ltac_No_arg.

Ltac savetactic h := 
  match type of h with
    | ltac_No_arg => let H := fresh in
      apply NNPP;
      intro H;
      apply H
    | _ => apply NNPP;
      intro h;
      apply h
  end.

Tactic Notation "save" := savetactic ltac_no_arg.
Tactic Notation "save" ident(x) := savetactic x.

然而,这个证据在save h上失败了:

Lemma te (A B : Prop) : A \/ ~A.
Proof.
save h.
right.
intro a.
apply h.
left.
exact a.
Qed.

错误消息:

In nested Ltac calls to "save (ident)", "savetactic" and "h", last term evaluation failed.
In nested Ltac calls to "save (ident)", "savetactic" and "h", last term evaluation failed.
Variable h should be bound to a term but is bound to a ident.

我想我必须确保h是新鲜的,但我不确定如何做到这一点。

1 个答案:

答案 0 :(得分:7)

问题在于该解决方案涉及一个术语(constr),而您有一个名称(ident ifier)。但是,您可以使用提供新标识符的策略符号更简单地解决您的问题。

Require Import Classical_Prop.

Ltac savetactic h := 
     apply NNPP;
      intro h;
      apply h.

Tactic Notation "save" := let H := fresh in savetactic H.
Tactic Notation "save" ident(x) := savetactic x.

Lemma te (A B : Prop) : A \/ ~A.
Proof.
  save h.
  right.
  intro a.
  apply h.
  left.
  exact a.
Qed.

此解决方案的一个问题是它在运行fresh之前调用savetactic,如果您希望在执行其他工作后h变得新鲜,则无效在savetactic内。不过,我认为唯一的区别在于自动生成的名称。

相关问题