Curry 通用量化函数

Posted

技术标签:

【中文标题】Curry 通用量化函数【英文标题】:Curry universally quantified function 【发布时间】:2017-06-04 22:27:44 【问题描述】:

我正在尝试编写一种用于柯里化函数的策略,包括普遍量化的函数。

Require Import Coq.Program.Tactics.

Definition curry1 := forall A B C, (A /\ B -> C) -> (A -> B -> C).
Definition curry2 := forall A B, (forall C, A /\ B -> C) -> (forall C, A -> B -> C).
Definition curry3 := forall A, (forall B C, A /\ B -> C) -> (forall B C, A -> B -> C).
(* etc. *)

Ltac curry H :=
  let T := type of H in
  match T with
  | _ /\ _ -> _ =>
    replace_hyp H (fun H1 => fun H2 => H (conj H1 H2))
  | forall x, ?P x =>
    fail 1 "not implemented"
  | _ =>
    fail 1 "not a curried function"
  end.

Example ex1 : curry1.
Proof.
  intros A B C H.
  curry H.
  assumption.
Qed.

Example ex2 : curry2.
Proof.
  intros A B H.
  Fail curry H. (* Tactic failure: not a curried function. *)
  Fail replace_hyp H (fun H1 => let H2 := H H1 in ltac:(curry H2)). (* Cannot infer an existential variable of type "Type" in environment: [...] *)
Abort.

如何扩展我的 curry 策略来处理普遍量化的函数?

【问题讨论】:

【参考方案1】:

您基本上可以像这样对所有变体进行模式匹配:

Ltac curry H :=
  match type of H with
  | _ /\ _ -> _ =>
      replace_hyp H (fun a b => H (conj a b))
  | forall C, _ /\ _ -> _ =>
      replace_hyp H (fun C a b => H C (conj a b))
  | forall B C, _ /\ _ -> _ =>
      replace_hyp H (fun B C a b => H B C (conj a b))
  | forall A B C, _ /\ _ -> _ =>
      replace_hyp H (fun A B C a b => H A B C (conj a b))
  end.

注意C 有排序Type,而不是Prop。如果你愿意在Prop 工作,你可以使用setoid_rewrite 策略和逻辑等价。例如:

Require Import Coq.Setoids.Setoid.

Implicit Types C : Prop.

Definition and_curry_uncurry_iff A B C : (A /\ B -> C) <-> (A -> B -> C) :=
  conj (fun H a b => H (conj a b)) (and_ind (P := C)).

Ltac find_and_curry :=
  match goal with
  | H : context [_ /\ _ -> _] |- _ =>
      setoid_rewrite and_curry_uncurry_iff in H
  end.

Example ex2_prop A B : (forall C, A /\ B -> C) -> (forall C, A -> B -> C).
Proof. intros H. find_and_curry. assumption. Qed.

【讨论】:

以上是关于Curry 通用量化函数的主要内容,如果未能解决你的问题,请参考以下文章

手写curry函数,实现函数柯里化

函数工厂Curry的威力

谈谈函数式编程curry

Groovy闭包 Closure ( 闭包参数绑定 | curry 函数 | rcurry 函数 | ncurry 函数 | 代码示例 )

函数式编程之柯里化(curry)

curry化函数