Coq “list_rec”的第一个输入何时不是常量函数?
Coq “list_rec”的第一个输入何时不是常量函数?,coq,Coq,list\u rec功能具有以下类型: list_rec : forall (A : Type) (P : list A -> Set), P nil -> (forall (a : A) (l : list A), P l -> P (a :: l)%list) -> forall l : list A, P l 在我给出的所有示例中,p只是一个常量函数,它忽略输入列表并返回相同的类型。例如,P可能是fun\uu:list A=>nat或fun\u
list\u rec
功能具有以下类型:
list_rec
: forall (A : Type) (P : list A -> Set),
P nil ->
(forall (a : A) (l : list A), P l -> P (a :: l)%list) ->
forall l : list A, P l
在我给出的所有示例中,
p
只是一个常量函数,它忽略输入列表并返回相同的类型。例如,P
可能是fun\uu:list A=>nat
或fun\uu:list A=>list B
。使P
的输出依赖于输入的一些用例是什么?为什么P
的类型是列出A->Set
而不仅仅是Set
?尝试证明
s++[]=s
[提示:将
p
定义为fun s=>s++[]=s
。例如,我们可以使用带有非常量p
函数的list\u rec
来实现将列表转换为向量(长度索引列表)的函数
您可以将其与函数的标准定义进行比较,函数的标准定义使用显式递归,而不是将其隐藏在递归原则后面,其功能完全相同(t
在以下代码中的意思):
Fixpoint of_list {A} (l : list A) : t A (length l) :=
match l as l' return t A (length l') with
|Datatypes.nil => []
|(h :: tail)%list => (h :: (of_list tail))
end.
一个简单的测试:
Eval compute in list_to_vector [1;2;3].
Eval compute in Vector.of_list [1;2;3].
两个函数调用返回相同的结果:
= [1; 2; 3]
: Vector.t nat (length [1; 2; 3])
= [1; 2; 3]
: Vector.t nat (length [1; 2; 3])