如何在Coq列表的长度上进行归纳?

时间:2017-08-25 00:23:02

标签: coq induction

在纸上推理时,我经常通过归纳法对某些列表的长度使用参数。我想在Coq中形式化这些参数,但似乎没有任何内置的方法可以在列表的长度上进行归纳。

我应该如何进行这样的归纳?

更具体地说,我试图证明this theorem。在纸面上,我通过归纳PHPExcel_Calculation_Exception Formula Error: An unexpected error occured的长度证明了这一点。我的目标是在Coq。

中形式化这个证明

3 个答案:

答案 0 :(得分:7)

有许多一般的感应模式,如此可以涵盖 由现有的图书馆完善的归纳法。在这种情况下,你可以证明 通过使用Package 'Microsoft.VisualStudio.Xamarin.Preparation,version=15.0.26621.2' failed to install. Search URL https://aka.ms/VSSetupErrorReports?q=PackageId=Microsoft.VisualStudio.Xamarin.Preparation;PackageAction=Install;ReturnCode=1 Details Command executed: "c:\windows\syswow64\\cmd.exe" /C DEL /F /S /Q """C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Xamarin""" Return code: 1 Return code details: Incorrect function. Log C:\Users\santiaj_Adm\AppData\Local\Temp\dd_setup_20170825092313_007_Microsoft.VisualStudio.Xamarin.Preparation.log Impacted workloads Mobile development with .NET (Microsoft.VisualStudio.Workload.NetCrossPlat,version=15.0.26606.0) Impacted components Xamarin (Component.Xamarin,version=15.0.26711.1) Xamarin Workbooks (Component.Xamarin.Inspector,version=15.0.26606.0) well_founded_inductionwf_inverse_image对列表长度进行归纳的任何属性P,如以下命令所示:

PeanoNat.Nat.lt_wf_0

如果您正在使用类型induction l using (well_founded_induction (wf_inverse_image _ nat _ (@length _) PeanoNat.Nat.lt_wf_0)). 的列表并证明目标T,则会生成 形式的假设

P l

这将适用于任何其他数据类型(例如树),只要您可以使用该数据类型中的任何 size 函数将其他数据类型映射到H : forall y : list T, length y < length l -> P y 到{{1}而不是nat

请注意,您需要在开发的前端添加nat才能实现此目的。

答案 1 :(得分:6)

以下是如何证明一般列表长度归纳原理。

Require Import List Omega.

Section list_length_ind.  
  Variable A : Type.
  Variable P : list A -> Prop.

  Hypothesis H : forall xs, (forall l, length l < length xs -> P l) -> P xs.

  Theorem list_length_ind : forall xs, P xs.
  Proof.
    assert (forall xs l : list A, length l <= length xs -> P l) as H_ind.
    { induction xs; intros l Hlen; apply H; intros l0 H0.
      - inversion Hlen. omega.
      - apply IHxs. simpl in Hlen. omega.
    }
    intros xs.
    apply H_ind with (xs := xs).
    omega.
  Qed.
End list_length_ind.

您可以像这样使用

Theorem foo : forall l : list nat, ...
Proof. 
    induction l using list_length_ind.
    ...

那就是说,您的具体示例示例不一定需要长度感应。你只需要一个足够一般的归纳假设。

Import ListNotations.

(* ... some definitions elided here ... *)    

Definition flip_state (s : state) :=
  match s with
  | A => B
  | B => A
  end.

Definition delta (s : state) (n : input) : state :=
  match n with
  | zero => s
  | one => flip_state s
  end.

(* ...some more definitions elided here ...*)

Theorem automata221: forall (w : list input),
    extend_delta A w = B <-> Nat.odd (one_num w) = true.
Proof.
  assert (forall w s, extend_delta s w = if Nat.odd (one_num w) then flip_state s else s).
  { induction w as [|i w]; intros s; simpl.
    - reflexivity.
    - rewrite IHw.
      destruct i; simpl.
      + reflexivity.
      + rewrite <- Nat.negb_even, Nat.odd_succ.
        destruct (Nat.even (one_num w)), s; reflexivity.
  }

  intros w.
  rewrite H; simpl.
  destruct (Nat.odd (one_num w)); intuition congruence.
Qed.

答案 2 :(得分:4)

如果是这样的话,直接概括你的引理通常会更快:

From mathcomp Require Import all_ssreflect.

Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.

Section SO.

Variable T : Type.
Implicit Types (s : seq T) (P : seq T -> Prop).

Lemma test P s : P s.
Proof.
move: {2}(size _) (leqnn (size s)) => ss; elim: ss s => [|ss ihss] s hs.

只需为列表的大小引入一个新的nat,定期归纳就可以了。