通过平等的coq归纳

时间:2017-10-03 05:36:17

标签: coq induction

我有一个已知值的列表,想要导入它,跟踪原始列表是什么,并按元素引用它。也就是说,我需要通过l [i]引用它来改变i而不是只有(a :: l)。

我试图制定归纳原则让我这样做。这是一个使用简化示例将所有不必要的定理替换为Admitted的程序。目标是使用countDown_nth证明allLE_countDown,并以方便的形式使用list_nth_rect。 (这个定理很容易直接证明,没有任何这些定理。)

Require Import Arith.
Require Import List.

Definition countDown1 := fix f a i := match i with
| 0 => nil
| S i0 => (a + i0) :: f a i0
end.

(* countDown from a number to another, excluding greatest. *)
Definition countDown a b := countDown1 b (a - b).

Theorem countDown_nth a b i d (boundi : i < length (countDown a b))
    : nth i (countDown a b) d = a - i - 1.
Admitted.

Definition allLE := fix f l m := match l with
| nil => true
| a :: l0 => if Nat.leb a m then f l0 m else false
end.

Definition drop {A} := fix f (l : list A) n := match n with
| 0 => l
| S a => match l with
  | nil => nil
  | _ :: l2 => f l2 a
  end
end.

Theorem list_nth_rect_aux {A : Type} (P : list A -> list A -> nat -> Type)
    (Pnil : forall l, P l nil (length l))
    (Pcons : forall i s l d (boundi : i < length l), P l s (S i) -> P l ((nth i l d) :: s) i)
    l s i (size : length l = i + length s) (sub : s = drop l i) : P l s i.
Admitted.

Theorem list_nth_rect {A : Type} (P : list A -> list A -> nat -> Type)
    (Pnil : forall l, P l nil (length l))
    (Pcons : forall i s l d (boundi : i < length l), P l s (S i) -> P l ((nth i l d) :: s) i)
    l s (leqs : l = s): P l s 0.
Admitted.

Theorem allLE_countDown a b : allLE (countDown a b) a = true.
  remember (countDown a b) as l.
  refine (list_nth_rect (fun l s _ => l = countDown a b -> allLE s a = true) _ _ l l eq_refl Heql);
    intros; subst; [ apply eq_refl | ].
  rewrite countDown_nth; [ | apply boundi ].
  pose proof (Nat.le_sub_l a (i + 1)).
  rewrite Nat.sub_add_distr in H0.
  apply leb_correct in H0.
  simpl; rewrite H0; clear H0.
  apply (H eq_refl).
Qed.

所以,我有list_nth_rect,并且能够根据需要通过引用第n个元素来使用refine来证明定理。但是,我必须自己构建命题P.通常,您希望使用归纳法。

这需要区分哪些元素是原始列表l与导入的子列表s。所以,我可以使用记住。

Theorem allLE_countDown a b : allLE (countDown a b) a = true.
  remember (countDown a b) as s.
  remember s as l.
  rewrite Heql.

这让我知道

  a, b : nat
  s, l : list nat
  Heql : l = s
  Heqs : l = countDown a b
  ============================
  allLE s a = true

但是,我似乎无法像上面那样传递平等。当我尝试

  induction l, s, Heql using list_nth_rect.

我收到错误

Error: Abstracting over the terms "l", "s" and "0" leads to a term
fun (l0 : list ?X133@{__:=a; __:=b; __:=s; __:=l; __:=Heql; __:=Heqs})
  (s0 : list ?X133@{__:=a; __:=b; __:=s; __:=l0; __:=Heql; __:=Heqs})
  (_ : nat) =>
(fun (l1 l2 : list nat) (_ : l1 = l2) =>
 l1 = countDown a b -> allLE l2 a = true) l0 s0 Heql
which is ill-typed.
Reason is: Illegal application: 
The term
 "fun (l l0 : list nat) (_ : l = l0) =>
  l = countDown a b -> allLE l0 a = true" of type
 "forall l l0 : list nat, l = l0 -> Prop"
cannot be applied to the terms
 "l0" : "list nat"
 "s0" : "list nat"
 "Heql" : "l = s"
The 3rd term has type "l = s" which should be coercible to 
"l0 = s0".

那么,我该如何改变归纳原理 这样它与感应策略一起工作? 它看起来好像很困惑 外部变量和内部变量 功能。但是,我没有办法说话 关于不在范围内的内部变量。 这非常奇怪,因为用它来调用它 精炼工作没有问题。 我知道匹配,有条款,但是 我无法弄清楚如何在这里申请。 或者,有没有办法使list_nth_rect使用 P l l 0并且仍然指出哪些变量对应于l和s?

3 个答案:

答案 0 :(得分:3)

首先,您可以通过重复使用更基本的结果来更轻松地证明这一结果。这是基于ssreflect库定义的版本:

From mathcomp
Require Import ssreflect ssrfun ssrbool ssrnat eqtype seq.

Definition countDown n m := rev (iota m (n - m)).

Lemma allLE_countDown n m : all (fun k => k <= n) (countDown n m).
Proof.
rewrite /countDown all_rev; apply/allP=> k; rewrite mem_iota.
have [mn|/ltnW] := leqP m n.
  by rewrite subnKC //; case/andP => _; apply/leqW.
by rewrite -subn_eq0 => /eqP ->; rewrite addn0 ltnNge andbN.
Qed.

此处,iota n m是从m开始计算的n元素列表,而allallLE的通用版本。标准库中存在类似的功能和结果。

回到原来的问题,有时我们需要在记住我们开始的整个列表的同时导入列表。我不知道是否有办法通过标准的感应策略获得你想要的东西;我甚至不知道它有一个多参数变体。当我想使用此策略证明P l时,我通常按以下步骤进行:

  1. 查找谓词Q : nat -> PropQ (length l)隐含P l。通常情况下,Q n的格式为n <= length l -> R (take n l) (drop n l),其中R : list A -> list A -> Prop

  2. 通过归纳证明Q n所有n

答案 1 :(得分:3)

我不知道这是否能回答您的问题,但induction似乎接受with条款。因此,您可以编写以下内容。

Theorem allLE_countDown a b : allLE (countDown a b) a = true.
  remember (countDown a b) as s.
  remember s as l.
  rewrite Heql.
  induction l, s, Heql using list_nth_rect
    with (P:=fun l s _ => l = countDown a b -> allLE s a = true).

但是,这个好处是相当有限的w.r.t. refine版本,因为您需要手动指定谓词。

现在,我将使用标准库中的对象证明这样的结果。

Require Import List. Import ListNotations.
Require Import Omega.

Definition countDown1 := fix f a i := match i with
| 0 => nil
| S i0 => (a + i0) :: f a i0
end.

(* countDown from a number to another, excluding greatest. *)
Definition countDown a b := countDown1 b (a - b).

Theorem countDown1_nth a i k d (boundi : k < i) :
  nth k (countDown1 a i) d = a + i -k - 1.
Proof.
  revert k boundi.
  induction i; intros.
  - inversion boundi.
  - simpl. destruct k.
    + omega.
    + rewrite IHi; omega.
Qed.

Lemma countDown1_length a i : length (countDown1 a i) = i.
Proof.
  induction i.
  - reflexivity.
  - simpl. rewrite IHi. reflexivity.
Qed.

Theorem countDown_nth a b i d (boundi : i < length (countDown a b))
    : nth i (countDown a b) d = a - i - 1.
Proof.
  unfold countDown in *.
  rewrite countDown1_length in boundi.
  rewrite countDown1_nth.
  replace (b+(a-b)) with a by omega. reflexivity. assumption.
Qed.

Theorem allLE_countDown a b : Forall (ge a) (countDown a b).
Proof.
  apply Forall_forall. intros.
  apply In_nth with (d:=0) in H.
  destruct H as (n & H & H0).
  rewrite countDown_nth in H0 by assumption. omega.
Qed.

编辑: 你可以说一个辅助引理来做一个更简洁的证明。

Lemma Forall_nth : forall {A} (P:A->Prop) l,
    (forall d i, i < length l -> P (nth i l d)) ->
    Forall P l.
  Proof.
    intros. apply Forall_forall.
    intros. apply In_nth with (d:=x) in H0.
    destruct H0 as (n & H0 & H1).
    rewrite <- H1. apply H. assumption.
  Qed.

Theorem allLE_countDown a b : Forall (ge a) (countDown a b).
Proof.
  apply Forall_nth.
  intros. rewrite countDown_nth. omega. assumption.
Qed.

答案 2 :(得分:2)

问题是,无论好坏,induction似乎都认为它的论点是独立的。然后,解决方案是让inductionl自动推断sHeql

Theorem list_nth_rect {A : Type} {l s : list A} (P : list A -> list A -> nat -> Type)
        (Pnil : P l nil (length l))
        (Pcons : forall i s d (boundi : i < length l), P l s (S i) -> P l ((nth i l d) :: s) i)
        (leqs : l = s): P l s 0.
Admitted.

Theorem allLE_countDown a b : allLE (countDown a b) a = true.
  remember (countDown a b) as s.
  remember s as l.
  rewrite Heql.
  induction Heql using list_nth_rect;
    intros; subst; [ apply eq_refl | ].
  rewrite countDown_nth; [ | apply boundi ].
  pose proof (Nat.le_sub_l a (i + 1)).
  rewrite Nat.sub_add_distr in H.
  apply leb_correct in H.
  simpl; rewrite H; clear H.
  assumption.
Qed.

我不得不改变list_nth_rect的类型;我希望我没有做错。