跳到正文

个人记录

如何在coq中使用两步归纳?

LWLAymh约 17 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年10月28日
篇幅
约 17 分钟

版本

  1. v1当前

众所周知,coq中的自然数构造是通过Peano公理构造的,而induction操作只能沿着Type构造的方向证明.这意味着:在coq中关于一个自然数的结论P(n)P(n),我们的归纳不能”假设<n<n的都成立,推导P(n)P(n)成立”,而只能是”假设P(n−1)P(n-1)成立,推导P(n)P(n)成立”.

这种问题在处理数列的时候更加显著,例如,我们有下面这个问题(来自于Software foundations-Logic foundations-IndProp):

coq
Inductive pal {X:Type} : list X -> Prop :=
  | empty_is_pal : pal []
  | char_is_pal (x: X) : pal [x]
  | two_app_is_pal (x:X) (l:list X) (H : pal l) : pal (x::l++[x])
.

Theorem palindrome_converse: forall {X: Type} (l: list X),
    l = rev l -> pal l.
Proof.
  Admitted.

如果你直接对着这个归纳会发现完全做不了,原因无他:l=x::l′l=x::l'的回文结构不能来自于l′l'.因此,必须在这里把整个ll的结构前后分别拆开,我们尝试引入这个引理:

coq
Theorem sep_head_tail : forall (X:Type) (l:list X),
  2<=length l ->
  (exists (x y:X) (l0:list X),l=x::l0++[y]).
Proof.
  intros X l.
  induction l as [|z l' IHl].
  - intros contra. simpl in contra. inversion contra.
  - destruct l' as [|w [|a l'']].
    + intros contra. simpl in contra. inversion contra. inversion H0.
    + intros _. exists z. exists w. exists []. simpl. reflexivity.
    + intros _. assert(IHl_to : exists (x y : X) (l0 : list X), w :: a :: l'' = x :: l0 ++ [y]).
      {apply IHl. simpl. apply n_le_m__Sn_le_Sm. apply n_le_m__Sn_le_Sm. apply O_le_n. }
      destruct IHl_to as [x[y[l0 H]]].
      exists z. exists y. exists (x::l0).
      rewrite H. simpl. reflexivity.
Qed.

这样,我们就实现了将一个list拆出前后的做法.此时,我们的目标应该对ll的这种拆分做递归,但这有点困难,因此我们退而求其次,对它的长度length(l)\mathrm{length}(l)做递归,引入以下命题:

coq
Definition Pn{X:Type} : nat->Prop := (fun n => forall (l:list X),length l = n->l = rev l -> pal l).

这就必须引入对自然数的两步归纳.可以将所有自然数分成奇数和偶数并分别归纳:

coq
Lemma nat_ind2_even:
  forall (P : nat -> Prop),
    P 0 ->
    (forall n, P n -> P (S (S n)))->
    (forall n, P (n+n)).
Proof.
  intros P H0.
  intros ind.
  intros n.
  induction n as [|n' IHn].
  - apply H0.
  - simpl. rewrite <-(plus_n_Sm n' n'). apply ind. apply IHn.
Qed.

Lemma nat_ind2_odd:
  forall (P : nat -> Prop),
    P 1 ->
    (forall n, P n -> P (S (S n)))->
    (forall n, P (1+n+n)).
Proof.
  intros P H1.
  intros ind.
  intros n.
  induction n as [|n' IHn].
  - apply H1.
  - simpl. rewrite <-(plus_n_Sm n' n'). apply ind. apply IHn.
Qed.

Lemma nat_is_even_or_odd :
  forall (n:nat),
  exists p,
  n = p+p\/ n = 1+p+p.
Proof.
  intros n.
  induction n as [|n' IHn].
  - exists 0. left. reflexivity.
  - destruct IHn as [p [Hpeven|Hpodd]].
    + exists p. right. rewrite Hpeven. simpl. reflexivity.
    + exists (p+1). left. rewrite (add_comm p 1). simpl. f_equal.
      rewrite <-plus_n_Sm. apply Hpodd.
Qed.

Lemma nat_ind2:
  forall (P : nat -> Prop),
    P 0 ->
    P 1 ->
    (forall n, P n -> P (S (S n))) ->
    forall n, P n.
Proof.
  intros P H0 H1 ind.
  intros n.
  assert(exists p,n=p+p\/ n= 1+p+p). {apply nat_is_even_or_odd. }
  destruct H as [p [Heven|Hodd]].
  - rewrite Heven. apply nat_ind2_even. apply H0. apply ind.
  - rewrite Hodd . apply nat_ind2_odd . apply H1. apply ind.
Qed.

从而解决了我们当前的问题.完整代码如下:

coq
Inductive pal {X:Type} : list X -> Prop :=
  | empty_is_pal : pal []
  | char_is_pal (x: X) : pal [x]
  | two_app_is_pal (x:X) (l:list X) (H : pal l) : pal (x::l++[x])
.

Theorem pal_app_rev : forall (X:Type) (l : list X),
  pal (l ++ (rev l)).
Proof.
  intros X l.
  induction l as [|x l' IHl].
  - simpl. apply empty_is_pal.
  - simpl. rewrite app_assoc. apply two_app_is_pal. apply IHl.
Qed.

Theorem pal_rev : forall (X:Type) (l: list X) , pal l -> l = rev l.
Proof.
  intros X l.
  intros pall.
  induction pall.
  - reflexivity.
  - reflexivity.
  - simpl. rewrite rev_app_distr. simpl. f_equal. f_equal. apply IHpall.
Qed.

Lemma two_same_char_is_pal : forall {X:Type} (x:X),
  pal [x;x].
Proof.
  intros X x.
  assert([x;x]=x::[]++[x]). {reflexivity. }
  rewrite H. apply two_app_is_pal.
  apply empty_is_pal.
Qed.

Lemma nat_ind2_even:
  forall (P : nat -> Prop),
    P 0 ->
    (forall n, P n -> P (S (S n)))->
    (forall n, P (n+n)).
Proof.
  intros P H0.
  intros ind.
  intros n.
  induction n as [|n' IHn].
  - apply H0.
  - simpl. rewrite <-(plus_n_Sm n' n'). apply ind. apply IHn.
Qed.

Lemma nat_ind2_odd:
  forall (P : nat -> Prop),
    P 1 ->
    (forall n, P n -> P (S (S n)))->
    (forall n, P (1+n+n)).
Proof.
  intros P H1.
  intros ind.
  intros n.
  induction n as [|n' IHn].
  - apply H1.
  - simpl. rewrite <-(plus_n_Sm n' n'). apply ind. apply IHn.
Qed.

Lemma nat_is_even_or_odd :
  forall (n:nat),
  exists p,
  n = p+p\/ n = 1+p+p.
Proof.
  intros n.
  induction n as [|n' IHn].
  - exists 0. left. reflexivity.
  - destruct IHn as [p [Hpeven|Hpodd]].
    + exists p. right. rewrite Hpeven. simpl. reflexivity.
    + exists (p+1). left. rewrite (add_comm p 1). simpl. f_equal.
      rewrite <-plus_n_Sm. apply Hpodd.
Qed.

Lemma nat_ind2:
  forall (P : nat -> Prop),
    P 0 ->
    P 1 ->
    (forall n, P n -> P (S (S n))) ->
    forall n, P n.
Proof.
  intros P H0 H1 ind.
  intros n.
  assert(exists p,n=p+p\/ n= 1+p+p). {apply nat_is_even_or_odd. }
  destruct H as [p [Heven|Hodd]].
  - rewrite Heven. apply nat_ind2_even. apply H0. apply ind.
  - rewrite Hodd . apply nat_ind2_odd . apply H1. apply ind.
Qed.

Definition Pn{X:Type} : nat->Prop := (fun n => forall (l:list X),length l = n->l = rev l -> pal l).

Theorem sep_head_tail : forall (X:Type) (l:list X),
  2<=length l ->
  (exists (x y:X) (l0:list X),l=x::l0++[y]).
Proof.
  intros X l.
  induction l as [|z l' IHl].
  - intros contra. simpl in contra. inversion contra.
  - destruct l' as [|w [|a l'']].
    + intros contra. simpl in contra. inversion contra. inversion H0.
    + intros _. exists z. exists w. exists []. simpl. reflexivity.
    + intros _. assert(IHl_to : exists (x y : X) (l0 : list X), w :: a :: l'' = x :: l0 ++ [y]).
      {apply IHl. simpl. apply n_le_m__Sn_le_Sm. apply n_le_m__Sn_le_Sm. apply O_le_n. }
      destruct IHl_to as [x[y[l0 H]]].
      exists z. exists y. exists (x::l0).
      rewrite H. simpl. reflexivity.
Qed.

Theorem cons_r_is_inj : forall (X:Type) (x:X)(l1 l2:list X) ,
  (l1++[x])=(l2++[x]) ->
  l1 = l2.
Proof.
  intros X x l1.
  induction l1 as [|y l1' IHl1].
  - intros l2. destruct l2 as [|z l2']. reflexivity. intros contra.
    simpl in contra. injection contra as contra. destruct l2' as [|w l2''].
    + simpl in H. discriminate H.
    + discriminate H.
  - intros l2 H. destruct l2 as [|z l2'].
    + simpl in H. injection H as H. destruct l1' as [|w l1'']. discriminate H0. discriminate H0.
    + simpl in H. injection H as H. apply IHl1 in H0. rewrite H0. rewrite H. reflexivity.
Qed.

Theorem app_r_is_inj : forall (X:Type) (l l1 l2:list X) ,
  (l1++l)=(l2++l) ->
  l1 = l2.
Proof.
  intros X l.
  induction l as [|x l' IHl].
  - intros l1 l2. intros H. rewrite app_nil_r in H. rewrite app_nil_r in H. apply H.
  - intros l1 l2. rewrite cons_to_app. intros H.
    rewrite app_assoc in H. rewrite app_assoc in H. apply IHl in H. apply cons_r_is_inj in H. apply H.
Qed.

Theorem palindrome_converse_n: forall (X: Type)
    (n:nat), (@Pn X n).
Proof.
  intros X.
  apply (nat_ind2 Pn).
  - unfold Pn. intros l. destruct l as [|x l'].
    + intros _ _. apply empty_is_pal.
    + intros contra. simpl in contra. discriminate contra.
  - unfold Pn. intros l. destruct l as [|x l'].
    + intros contra. simpl in contra. discriminate contra.
    + intros H. simpl in H. injection H as H. destruct l' as [| y l''].
      * intros _. apply char_is_pal.
      * simpl in H. discriminate H.
  - unfold Pn. intros n. intros H. intros l. intros lenSS. intros Hr.
    assert(H2:2<=length l ). {rewrite lenSS. apply n_le_m__Sn_le_Sm. apply n_le_m__Sn_le_Sm. apply O_le_n. }
    apply sep_head_tail in H2. destruct H2 as [x[y[l0 Hl]]].
    rewrite Hl in Hr. simpl in Hr. rewrite rev_app_distr in Hr. simpl in Hr. injection Hr as Exy.
    rewrite Exy in Hl.
    rewrite Hl. apply two_app_is_pal. apply H. rewrite Hl in lenSS. simpl in lenSS.
    rewrite app_length in lenSS. simpl in lenSS. rewrite <-plus_n_Sm in lenSS. rewrite add_0_r in lenSS. injection lenSS as Hlen.
    apply Hlen.
    rewrite Exy in H0.
    apply app_r_is_inj in H0. apply H0.
Qed.

Theorem palindrome_converse: forall {X: Type} (l: list X),
    l = rev l -> pal l.
Proof.
  intros X l.
  remember (length l) as n.
  assert(@Pn X n). {apply palindrome_converse_n. }
  unfold Pn in H.
  apply H. rewrite Heqn. reflexivity.
Qed.
原文