包含Coq中N个元素的所有函数的类型

时间:2016-04-03 17:38:40

标签: coq theorem-proving

我正在学习Coq,作为练习,我想定义一个类型FnArity (N:nat)来编码 N个参数的所有函数。那就是:

Check FnArity 3 : (forall A B C : Set, A -> B -> C).

应该可以工作但是

Check FnArity 2 : (forall A B C D : Set, A -> B -> C -> D).

不应该工作。

这是出于教学目的,所以欢迎任何相关资源。

编辑:从答案到目前为止,我意识到我可能正在接近这个错误,所以这是我试图证明的命题: 组合N个组合运算符等同于组合fg的组合运算符,其中g期望N个参数。在haskell-ish术语中:

(.).(.) ... N times ... (.).(.) f g = \a1, .. aN -> f (g (a1, .. , aN))

EDIT2:在coq术语中:

Definition compose { A B C : Type } (F : C -> B) (G : A -> C ) : A -> B :=
  fun x => F ( G (x) ).

Definition compose2 {A1 A2 B C : Type} (F : C -> B) (G : A1 -> A2 -> C)
: A1 -> A2 -> B := fun x y => F ( G x y ).

Definition compose3 {A1 A2 A3 B C : Type} (F : C -> B) (G : A1 -> A2 -> A3 -> C)
: A1 -> A2 -> A3 -> B := fun x y z => F ( G x y z ).

(* The simplest case *)
Theorem dual_compose : forall {A B C D : Type} (f: D -> C) (g : A -> B -> D) ,
                         (compose compose compose) f g = compose2 f g.
Proof. reflexivity. Qed.

Theorem triple_compose : forall {A1 A2 A3 B C : Type} (f: C -> B) (g : A1 -> A2 -> A3 -> C) ,
                         (compose (compose (compose) compose) compose) f g =
                         compose3 f g.

我想要的是定义composeN的广义定理。

2 个答案:

答案 0 :(得分:3)

您记下的类型并不能完全代表您在问题中所说的内容:forall A B C, A -> B -> C不是三个参数的所有函数的类型,而是两个参数的某些多态函数的类型。您可能想要编写类似{ A & { B & { C & A -> B -> C }}}的内容,其中ABC 存在量化。你可能也想说Compute (FnArity 3)而不是使用Check命令,因为后者是评估一个术语的命令(并且正如jbapple指出的那样,没有一个术语可以具有你原来的类型写)。

我认为这是一段代码可以满足您的需求。我们首先编写一个函数FnArityAux1 : list Type -> Type -> Type,它使用列表中给出的参数计算函数类型:

Fixpoint FnArityAux1 (args : list Type) (res : Type) : Type :=
  match args with
  | [] => res
  | T :: args' => T -> FnArityAux1 args' res
  end.

例如,FnArityAux1 [nat; bool] bool评估为nat -> bool -> bool。然后我们可以使用此函数定义FnArity,如下所示:

Fixpoint FnArityAux2 (args : list Type) (n : nat) : Type :=
  match n with
  | 0 => { T : Type & FnArityAux1 args T }
  | S n' => { T : Type & FnArityAux2 (args ++ [T]) n' }
  end.

Definition FnArity n := FnArityAux2 [] n.

在这个定义中,我们使用另一个具有参数FnArityAux2的辅助函数args,其目的是携带到目前为止生成的所有存在量化类型。对于每个“迭代步骤”,它量化另一种类型T,将该类型添加到参数列表中,并进行递归。当递归结束时,我们使用FnArityAux1将所有累积类型组合成单个函数类型。然后,我们可以简单地通过使用空列表启动流程来定义FnArity - 也就是说,根本没有量化类型。

答案 1 :(得分:1)

不,这是不可能的,因为json无人居住。

(forall A B C : Set, A -> B -> C)

因此,Goal (forall A B C : Set, A -> B -> C) -> False. intros f. specialize (f True True False). apply f; trivial. Qed. 永远无法运作。