为什么我不能在 Coq 中将 `app` 与 `fold_right` 一起使用?
Why can't I use `app` together with `fold_right` in Coq?
我在以下程序的最后一行遇到类型错误:
Require Import List.
Import ListNotations.
(* This computes to 10 *)
Compute (fold_right plus 0 [1;2;3;4]).
(* I want this to compute to [5;6;7;8] but it gives a type error instead *)
Compute (fold_right app [] [[5;6]; [7;8]]).
这是我得到的错误:
Error:
The term "app" has type "forall A : Type, list A -> list A -> list A" while it is expected to have type
"Type -> ?A -> ?A" (cannot instantiate "?A" because "A" is not in its scope).
我不太明白为什么会出现此错误。这里的 app
和 plus
有什么不同?它是否与 app
是多态的而 plus
是单态的 nat -> nat -> nat
函数有关?
以防万一,我的 Coq 版本是 8.5。
您猜对了:它确实与 app
是多态性有关。问题在于,Coq 允许根据相应项是否应用于参数来不同地推断隐式参数。更准确地说,non-maximal implicits 仅在术语应用于某物时插入,但如果术语单独使用则不会插入,例如您的 app
。有两种补救方法:
1- 强制 Coq 为那个实例推断出一些东西,如 fold_right (@app _) [] [[5; 6]; [7; 8]]
.
2- 使用全局声明,使类型参数最大化插入:Arguments app {_} _ _.
。有关此操作的更多详细信息,请查看 reference manual
我在以下程序的最后一行遇到类型错误:
Require Import List.
Import ListNotations.
(* This computes to 10 *)
Compute (fold_right plus 0 [1;2;3;4]).
(* I want this to compute to [5;6;7;8] but it gives a type error instead *)
Compute (fold_right app [] [[5;6]; [7;8]]).
这是我得到的错误:
Error:
The term "app" has type "forall A : Type, list A -> list A -> list A" while it is expected to have type
"Type -> ?A -> ?A" (cannot instantiate "?A" because "A" is not in its scope).
我不太明白为什么会出现此错误。这里的 app
和 plus
有什么不同?它是否与 app
是多态的而 plus
是单态的 nat -> nat -> nat
函数有关?
以防万一,我的 Coq 版本是 8.5。
您猜对了:它确实与 app
是多态性有关。问题在于,Coq 允许根据相应项是否应用于参数来不同地推断隐式参数。更准确地说,non-maximal implicits 仅在术语应用于某物时插入,但如果术语单独使用则不会插入,例如您的 app
。有两种补救方法:
1- 强制 Coq 为那个实例推断出一些东西,如 fold_right (@app _) [] [[5; 6]; [7; 8]]
.
2- 使用全局声明,使类型参数最大化插入:Arguments app {_} _ _.
。有关此操作的更多详细信息,请查看 reference manual