我如何编写像“破坏......一样”的策略?

时间:2015-05-09 00:38:12

标签: coq ltac

在coq中,destruct策略有一个变体接受“联合析取引入模式”,允许用户为引入的变量分配名称,即使在解包复杂的归纳类型时也是如此。

coq中的Ltac语言允许用户编写自定义策略。在将控制权移交给destruct之前,我想写(实际上,维护)一种做某事的策略。

我希望我的自定义策略允许(或要求,更容易)用户提供我的策略可以传递给destruct的介绍模式。

Ltac语法实现了什么?

1 个答案:

答案 0 :(得分:6)

您可以使用reference manual中描述的战术符号。例如,

Tactic Notation "foo" simple_intropattern(bar) :=
  match goal with
  | H : ?A /\ ?B |- _ =>
    destruct H as bar
  end.

Goal True /\ True /\ True -> True.
intros. foo (HA & HB & HC).

simple_intropattern指令指示Coq将bar解释为前奏模式。因此,对foo的调用会导致调用destruct H as (HA & HB & HC)

这是一个较长的例子,显示了更复杂的引入模式。

Tactic Notation "my_destruct" hyp(H) "as" simple_intropattern(pattern) :=
  destruct H as pattern.

Inductive wondrous : nat -> Prop :=
  | one         : wondrous 1
  | half        : forall n k : nat,         n = 2 * k -> wondrous k -> wondrous n
  | triple_one  : forall n k : nat, 3 * n + 1 = k     -> wondrous k -> wondrous n.

Lemma oneness : forall n : nat, n = 0 \/ wondrous n.
Proof.
  intro n.
  induction n as [ | n IH_n ].

  (* n = 0 *)
  left. reflexivity.

  (* n <> 0 *)
  right. my_destruct IH_n as
    [ H_n_zero
    | [
      | n' k H_half       H_wondrous_k
      | n' k H_triple_one H_wondrous_k ] ].

Admitted.

我们可以检查其中一个生成的目标,以查看名称的使用方式。

oneness < Show 4.
subgoal 4 is:

  n : nat
  n' : nat
  k : nat
  H_triple_one : 3 * n' + 1 = k
  H_wondrous_k : wondrous k
  ============================
   wondrous (S n')
相关问题