Recursion 如何获得嵌套修复的归纳原理

Recursion 如何获得嵌套修复的归纳原理,recursion,coq,induction,totality,Recursion,Coq,Induction,Totality,我正在使用一个函数来搜索一系列值 Require Import List. (* Implementation of ListTest omitted. *) Definition ListTest (l : list nat) := false. Definition SearchCountList n := (fix f i l := match i with | 0 => ListTest (rev l) | S i1 => (fix g j l1 :=

我正在使用一个函数来搜索一系列值

Require Import List.

(* Implementation of ListTest omitted. *)
Definition ListTest (l : list nat) := false.

Definition SearchCountList n :=
  (fix f i l := match i with
  | 0 => ListTest (rev l)
  | S i1 =>
    (fix g j l1 := match j with
    | 0 => false
    | S j1 =>
      if f i1 (j :: l1)
      then true
      else g j1 l1
    end) (n + n) (i :: l)
  end) n nil
.
我想能够对这个函数进行推理

然而,我似乎无法让coq的内置感应原理设备工作

Functional Scheme SearchCountList := Induction for SearchCountList Sort Prop.

Error: GRec not handled
看起来coq是为处理相互递归而设置的,而不是嵌套递归。在本例中,我有两个嵌套for循环

但是,转换为相互递归也不是那么容易:

Definition SearchCountList_Loop :=
  fix outer n i l {struct i} :=
    match i with
    | 0 => ListTest (rev l)
    | S i1 => inner n i1 (n + n) (i :: l)
    end
  with inner n i j l {struct j} :=
    match j with
    | 0 => false
    | S j1 =>
      if outer n i (j :: l)
      then true
      else inner n i j1 l
    end
  for outer
.
但这会产生错误

对内部的递归调用的主参数等于 “
n+n
”而不是“
i1

所以,看起来我需要使用measure来让它直接接受定义。有时我会重置j,这让人感到困惑。但是,在嵌套设置中,这是有意义的,因为我已经减少了,并且我是外部循环


那么,是否有一种标准的方法来处理嵌套递归,而不是相互递归?有没有更简单的方法来对这些案例进行推理,而不涉及单独的归纳定理?由于我还没有找到一种自动生成它的方法,我想我只能直接编写归纳原理。

在这种情况下,有一个技巧可以避免相互递归:你可以在
f
内部计算
f
并将结果传递给
g

Fixpoint g (f_n_i1 : list nat -> bool) (j : nat) (l1 : list nat) : bool :=
  match j with
  | 0 => false
  | S j1 => if f_n_i1 (j :: l1) then true else g f_n_i1 j1 l1
  end.

Fixpoint f (n i : nat) (l : list nat) : bool :=
  match i with
  | 0 => ListTest (rev l)
  | S i1 => g (f n i1) (n + n) (i :: l)
  end.

Definition SearchCountList (n : nat) : bool := f n n nil.

你确定简单的归纳法在原始代码中还不够吗?有充分依据的入职培训怎么样?

谢谢。这项建设应该是可行的。通过将捕获的变量转换为curry参数来翻转它是一个好主意。现在,我有了单独的函数,可能只需要简单的归纳就可以了。理想情况下,我想说的是:归纳法n,(SearchCountList n),并具有所有额外的前提和循环不变量,等等。但是,有了这个提示,我应该能够更容易、更直接地到达那里。