如何在Coq中使用自定义归纳原则?

如何在Coq中使用自定义归纳原则?,coq,induction,coq-tactic,Coq,Induction,Coq Tactic,我读到一个类型的归纳原理只是一个关于命题p的定理。因此,我基于右(或反向)列表构造函数为列表构建了一个归纳原则 Definition rcons {X:Type} (l:list X) (x:X) : list X := l ++ x::nil. 归纳原则本身是: Definition true_for_nil {X:Type}(P:list X -> Prop) : Prop := P nil. Definition true_for_list {X:Type} (P:li

我读到一个类型的归纳原理只是一个关于命题
p
的定理。因此,我基于右(或反向)列表构造函数为
列表构建了一个归纳原则

Definition rcons {X:Type} (l:list X) (x:X) : list X := 
  l ++ x::nil.
归纳原则本身是:

Definition true_for_nil {X:Type}(P:list X -> Prop) : Prop :=
  P nil.

Definition true_for_list {X:Type} (P:list X -> Prop) : Prop :=
  forall xs, P xs.

Definition preserved_by_rcons {X:Type} (P: list X -> Prop): Prop :=
  forall xs' x, P xs' -> P (rcons xs' x).

Theorem list_ind_rcons: 
  forall {X:Type} (P:list X -> Prop),
    true_for_nil P ->
    preserved_by_rcons P ->
    true_for_list P.
Proof. Admitted.
但是现在,我在使用这个定理时遇到了困难。我不知道如何调用它来实现与归纳法相同的策略

例如,我尝试:

Theorem rev_app_dist: forall {X} (l1 l2:list X), rev (l1 ++ l2) = rev l2 ++ rev l1.
Proof. intros X l1 l2. 
  induction l2 using list_ind_rcons.
但在最后一行,我得到:

Error: Cannot recognize an induction scheme.
定义和应用自定义归纳原则(如
list\u ind\r cons
)的正确步骤是什么


谢谢

你所做的基本上是正确的。问题是,由于中间的定义,Coq在认识到您所写的是归纳原则方面有一些困难。例如,这一点很好:

Theorem list_ind_rcons:
  forall {X:Type} (P:list X -> Prop),
    P nil ->
    (forall x l, P l -> P (rcons l x)) ->
    forall l, P l.
Proof. Admitted.

Theorem rev_app_dist: forall {X} (l1 l2:list X), rev (l1 ++ l2) = rev l2 ++ rev l1.
Proof. intros X l1 l2.
  induction l2 using @list_ind_rcons.

我不知道Coq不能自动展开中间定义是否应该被视为缺陷,但至少有一个解决办法。

如果想要保留中间定义,那么可以使用
部分
机制,如下所示:

Require Import Coq.Lists.List. Import ListNotations.

Definition rcons {X:Type} (l:list X) (x:X) : list X := 
  l ++ [x].

Section custom_induction_principle.    
  Variable X : Type.
  Variable P : list X -> Prop.

  Hypothesis true_for_nil : P nil.
  Hypothesis true_for_list : forall xs, P xs.
  Hypothesis preserved_by_rcons : forall xs' x, P xs' -> P (rcons xs' x).

  Fixpoint list_ind_rcons (xs : list X) : P xs. Admitted.
End custom_induction_principle.
Coq替换了定义,
列表索引
具有所需的类型和
归纳。。。使用…
可以:

Theorem rev_app_dist: forall {X} (l1 l2:list X),
  rev (l1 ++ l2) = rev l2 ++ rev l1.
Proof. intros X l1 l2. 
  induction l2 using list_ind_rcons.
Abort.
顺便说一句,标准库(模块)中有此归纳原则:

CoqProp),
P[]->
(对于所有(x:A)(l:list A),PL->P(l++[x]))->
对于所有l:列表A,P l

我想你的意思是
(对于所有的xl,pl->P(l++(x::nil))->
@gallais确实,谢谢你发现了它。只是修正了它。在Coq中,在战术的实施中长期存在着对定义平等的不尊重。虽然这在意识形态上是有问题的,但在实践中对此采取任何行动都为时已晚。
Coq < Check rev_ind.
rev_ind
     : forall (A : Type) (P : list A -> Prop),
       P [] ->
       (forall (x : A) (l : list A), P l -> P (l ++ [x])) ->
       forall l : list A, P l