在coq中,如何做和感应:nn"以一种不会弄乱归纳假设的方式?

时间:2015-06-25 11:54:23

标签: coq

使用归纳时,我希望假设n = 0n = S n'来分隔案例。

Section x.
  Variable P : nat -> Prop.
  Axiom P0: P 0.
  Axiom PSn : forall n, P n -> P (S n).

  Theorem Pn: forall n:nat, P n.
  Proof. intros n. induction n.
   - (* = 0 *) 
     apply P0. 
   - (* = S n *)
     apply PSn. assumption.
  Qed.

理论上我可以用induction n eqn: Hn做到这一点,但这似乎弄乱了归纳假设:

  Theorem Pn2: forall n:nat, P n.
  Proof. intros n. induction n eqn: Hn.
   - (* Hn : n = 0 *) 
     apply P0. 
   - (* Hn : n = S n0 *)
     (*** 1 subgoals
      P : nat -> Prop
      n : nat
      n0 : nat
      Hn : n = S n0
      IHn0 : n = n0 -> P n0
      ______________________________________(1/1)
      P (S n0)
     ****)
  Abort.
End x.

有没有一种简单的方法可以得到我想要的东西?

3 个答案:

答案 0 :(得分:1)

噢,我想我已经明白了!

应用归纳假设将你的目标从(P n)改为(P(构造函数n')),所以我认为一般来说你可以匹配目标来创建方程n =构造n'。

我认为这是一种策略:

(* like set (a:=b) except introduces a name and hypothesis *)
Tactic Notation 
    "provide_name" ident(n) "=" constr(v)
       "as" simple_intropattern(H) :=
    assert (exists n, n = v) as [n H] by (exists v; reflexivity).

Tactic Notation
  "induction_eqn" ident(n) "as" simple_intropattern(HNS)
    "eqn:" ident(Hn) :=
  let PROP := fresh in (
    pattern n;
    match goal with [ |- ?FP _ ] => set ( PROP := FP ) end;
    induction n as HNS;
    match goal with [ |- PROP ?nnn ] => provide_name n = nnn as Hn end;
    unfold PROP in *; clear PROP
  ).

它适用于我的例子:

Theorem Pn_3: forall n:nat, P n.
Proof.
  intros n.
  induction_eqn n as [|n'] eqn: Hn.
  - (* n: nat, Hn: n = 0; Goal: P 0 *)
    apply P0.
  - (* n': nat, IHn': P n';
       n: nat, Hn: n = S n'
       Goal: P (S n') *)
    apply PSn. exact IHn'.
Qed.

答案 1 :(得分:1)

马特几乎是对的,你只是忘了通过恢复记住的n来概括你的目标:

Theorem Pn2: forall n:nat, P n.
  Proof. intros n. remember n. revert n0 Heqn0.
  induction n as [ | p hi]; intros m heq.
  - (* heq : n = 0 *)  subst. apply P0. 
  - (* heq : n = S n0 *)
    (* 
  1 subgoal
  P : nat -> Prop
  p : nat
  hi : forall n0 : nat, n0 = p -> P n0
  m : nat
  heq : m = S p
  ______________________________________(1/1)
  P m
  *) subst; apply (PSn p). apply hi. reflexivity.

答案 2 :(得分:0)

我不确定这比你在第二次尝试中所做的更容易,但你可以先“记住”n

 Theorem Pn: forall n:nat, P n.
 Proof. intro n. remember n. induction n.
 - (*P : nat -> Prop
     n0 : nat
     Heqn0 : n0 = 0
     ============================
     P n0
    *)
    subst. apply P0.
 - (* P : nat -> Prop
      n : nat
      n0 : nat
      Heqn0 : n0 = S n
      IHn : n0 = n -> P n0
      ============================
      P n0
    *)