CurryHowardCurry-Howard 对应的形式化开发

在 ProofObjects 中,我们了解到 Rocq 的策略会构造 【证明对象|Proof Object】:即用 Rocq 的编程语言 Gallina 编写、用于操纵证据的程序。每个策略步骤都对应于构建程序的一部分,而完成的证明则是一个完全构造好的项, 其类型表达了所要证明的陈述。从这个意义上说,证明一个定理不只是逻辑推理的问题,也是编写程序的行为。
这一视角揭示了更深层的联系。一个命题的证明可以被理解为这样一个程序:它把其假设的证据转换为其结论的证据。同样,函数把输入转换为输出。从这个观点来看, 程序可以被视为证明,而程序的类型可以被视为命题。程序与证明之间、类型与命题之间的这种关系,就是 Curry–Howard 【对应|Curry-Howard Correspondence】的本质。
在本章中,我们将使这一对应关系变得精确。我们不依赖 Rocq 内建的逻辑和【类型系统|Type System】,而是在 Rocq 内部定义我们自己的逻辑【证明系统|Proof System】以及程序的类型系统。然后我们证明:只要一个程序是【良类型|Well-Typed】的, 它的类型就可以被翻译成一个在相应证明系统中【可证|Provable】的命题。换句话说,【类型判定|Typing】的【推导|Derivation】 可以被系统地转换为逻辑证明。反过来,我们还将证明:只要一个命题是可证的,就存在一个良类型程序,其类型对应于该命题。因此, 程序与证明在两个方向上都是对应的。程序本身就充当证明对象,正如在 Rocq 中那样,只不过现在这种联系被显式而形式化地建立起来了。
为什么这种对应如此令人兴奋?它表明逻辑与编程不仅仅是彼此类似;相反,它们从根本上是同一类结构,只是从两个不同的视角来看。【逻辑联结词|Connective】 对应于程序构造,证明对应于可执行的程序。因此,编写一个正确的程序就成为证明定理的一种形式,而证明定理也就成为构造程序的一种方式。 这种统一有助于解释为什么像 Rocq 这样的系统会以它们的方式工作,并且它构成了编程语言和形式化验证中现代思想的基础。
历史注记:Haskell B. Curry 在 1930 年代关于组合子逻辑的研究中发现了这一对应关系。William A. Howard 后来在 1969 年将其调整到另一个逻辑系统,即【相继演算|Sequent Calculus】,并扩展了这一类比,表明计算对应于证明的化简。 基于这一联系,Per Martin-Löf 在 1970 年代发展了【构造性类型理论|Constructive Type Theory】 (Rocq 本身正是建立在其之上)。本章的表述沿用 [Sørensen and Urzyczyn 2006]。
From Stdlib Require Import List String.
Import ListNotations.
我们首先在一个以【蕴含|Implication】为重点的设定中展开这一【对应|Correspondence】。随后,我们将加入【合取|Conjunction】 与【析取|Disjunction】。
Module Implicational.

逻辑

到目前为止,我们一直在用 Rocq 做逻辑,即使用它对【联结词|Connective】 (如【合取|Conjunction】和【析取|Disjunction】)的定义,以及它关于如何构造证明的规则。 但为了使【对应|Correspondence】更加精确,我们需要做嵌入在 Rocq 之中的逻辑, 即用归纳类型定义逻辑【命题|Proposition】的语法,并用归纳定义的命题来定义证明规则。
这里形式化的逻辑被称为命题构造逻辑。它是命题逻辑,因为它不含量词。 它是构造性的,理由与我们在 Logic 中所学的相同: 它不包含排中律(或与之等价的公理)。

命题

我们首先对命题进行建模。
  • 【原子|Atom】是一个原子命题,很像 P : Prop 中的 P,正如我们习惯于 Rocq 中的那样。原子命题使用 Rocq 的 string 来命名。
  • 蕴含是通常的 P → Q。
  • 矛盾命题被命名为 false。
Inductive proposition :=
  | atom (id : string)
  | implies (p q : proposition)
  | false.
从该起点出发,我们可以用其他命题来定义否定与真值。
Definition not (p : proposition) : proposition :=
  implies p false.

Definition true : proposition :=
  implies false false.
下面是一些【命题|Proposition】的例子。
Definition ex_P :=
  (* P -> P *)
  implies (atom "P") (atom "P").

Definition ex_PQ :=
  (* P -> Q -> P *)
  implies (atom "P") (implies (atom "Q") (atom "P")).

Definition ex_PQR :=
  (* (P -> Q -> R) -> (P -> Q) -> P -> R *)
  implies
    (implies (atom "P") (implies (atom "Q") (atom "R")))
    (implies (implies (atom "P") (atom "Q")) (implies (atom "P") (atom "R"))).
证明
什么是证明?什么使得一个证明正确或错误?在形式逻辑的研究中,【证明系统|Proof System】是一组公理和推理规则, 用以描述如何构成正确的证明。这里我们介绍一个称为【自然演绎|Natural Deduction】的证明系统。它基于一个【可证性|Provability】 关系,该关系刻画了一个命题在何种假设集合下是可证的。
可证关系写作 A ⊢ p,其中 p 是一个命题,A 是一组命题(即假设),而符号 ⊢ 读作「证明」。 该关系由以下推理规则定义,其中我们写作 A, p 表示 A 与 {p} 的并集:
   (公理)  

A, p ⊢ p
A, p ⊢ q (蕴含引入)  

A ⊢ p -> q
A ⊢ p -> q   A ⊢ p (蕴含消去)  

A ⊢ q
A ⊢ false (假消去)  

A ⊢ p
这些规则可以如下解读:
  • 公理:从包含 p 的一组假设出发,可以推出 p 是【可证|Provable】的, 也就是说 p 「因假设而成立」。
  • 蕴含引入:如果能在假设 A, p 下证明 q,那么就能推出 p → q 在假设 A 下是可证的。也就是说,p → q 的证明就是一种把对 p 的假设转化为 q 的证明的方法。规则的名称表明蕴含【联结词|Connective】→ 是在结论中 被 【引入】 的;它在前提中并不存在。
  • 蕴含消去:如果能在 A 下既证明 p → q 又能证明 p,那么就能推出 q 在 A 下是可证的。也就是说,既然存在一种把对 p 的假设转化为 q 的 证明的方法,而 p 本身也有证明,那么就可以把两者结合起来构造出 q 的 证明。规则的名称表明 → 是在结论中被 【消去】 的;它在前提中是存在的。 这条规则通常称为「肯定前件式」(modus ponens)。
  • 假消去:如果能在 A 下证明 false,那么这组假设就是不一致的,因此 可以推出在 A 下能证明任何命题,包括任意【命题|Proposition】p。 这就是「从假可推出任意」(ex falso quodlibet)原则。 注意,对于假引入并不存在推理规则,因为我们永远不应该能从一组一致的 假设出发证明 false。
将【自然演绎|Natural Deduction】的推理规则形式化到 Rocq 中是直截了当的。 将【假设|Assumption】集合表示为列表很方便, 因为我们对这种类型已经有了大量经验。
Inductive provable : list proposition → proposition → Prop :=
  | axiom : ∀ A p,
      In p A →
      provable A p
  | implies_intro : ∀ A p q,
      provable (p :: A) q →
      provable A (implies p q)
  | implies_elim : ∀ A p q,
      provable A (implies p q) →
      provable A p →
      provable A q
  | false_elim : ∀ A p,
      provable A false →
      provable A p.
这里是使用证明系统的一些示例。
Example ex_P_provable : provable [] ex_P.
(*
         -------------------- (axiom)
                p ⊢ p
         -------------------- (implies intro)
              ⊢ p -> p
*)

Proof.
  unfold ex_P. apply implies_intro.
  apply axiom. simpl. left. reflexivity.
Qed.

Example ex_PQ_provable : provable [] ex_PQ.
(*
      ------------------- (axiom)
           P, Q ⊢ P
      ------------------- (implies intro)
          P ⊢ Q -> P
      ------------------- (implies intro)
        ⊢ P -> Q -> P
*)

Proof.
  (* 已在课上完成 *)
  unfold ex_PQ. apply implies_intro. apply implies_intro.
  apply axiom. simpl. right. left. reflexivity.
Qed.

Example ex_mp : ∀ (P Q : string),
  provable [implies (atom P) (atom Q); (atom P)] (atom Q).
(*
    ------------------- (axiom)     -------------- (axiom)
    P -> Q, P ⊢ P -> Q             P -> Q, P ⊢ P
    ---------------------------------------------- (implies elim)
                   P -> Q, P ⊢ Q
*)

Proof.
  (* 已在课上完成 *)
  intros P Q. apply implies_elim with (atom P).
  - apply axiom. simpl. left. reflexivity.
  - apply axiom. simpl. right. left. reflexivity.
Qed.
使用 axiom 规则后再对 In 进行推理相当繁琐。 我们可以用下面这个小型自定义策略【策略|Tactic】来自动完成这一过程。 该策略中用到的技术将在 Auto (或者 AltAuto) 中讲解。
Ltac by_assumption :=
  constructor;
  repeat match goal with
  | ⊢ In ?p (?h :: ?t) ⇒ simpl
  | ⊢ (?x = ?y) ∨ ?p ⇒ first [left; reflexivity | right]
  | ⊢ False ⇒ idtac "by_assumption failed: not in assumptions"; fail
  end.

Example in_assumptions : provable [atom "P"] (atom "P").
Proof.
  by_assumption.
Qed.

Example not_in_assumptions : provable [atom "Q"] (atom "P").
Proof.
  by_assumption.
Abort.

Example ex_P_provable' : provable [] ex_P.
Proof.
  unfold ex_P. apply implies_intro.
  by_assumption.
Qed.

练习: 2 stars,标准 (ex_PQR_provable)

证明我们最初给出的第三个示例【命题|Proposition】是【可证|Provable】的。 首先,作为热身,在 Rocq 中证明与它等价的一个命题。
Theorem ex_PQR_provable_rocq : ∀ (P Q R : Prop),
  (P → Q → R) → (P → Q) → P → R.
Proof.
  (* 在这里填写 *) Admitted.
其次,使用自然演绎来证明它。
提示:先试着在纸上用推理规则草拟出推导过程。 难点在于每次都要找到正确的命题来实例化 implies_elim。
Theorem ex_PQR_provable_nd : provable [] ex_PQR.
Proof.
  (* 在这里填写 *) Admitted.
☐

练习: 3 stars,标准 (more_provable_propositions)

证明下列每一个【命题|Proposition】。
Theorem implies_trans : ∀ (P Q R : string),
  provable []
  (implies (implies (atom P) (atom Q))
           (implies (implies (atom Q) (atom R))
                    (implies (atom P) (atom R)))).
Proof.
  (* 在这里填写 *) Admitted.

Theorem exfalso_quodlibet : ∀ (P : string),
  provable [] (implies false (atom P)).
Proof.
  (* 在这里填写 *) Admitted.

Theorem contrapositive : ∀ (P Q : string),
  provable []
  (implies (implies (atom P) (atom Q))
           (implies (not (atom Q)) (not (atom P)))).
Proof.
  (* 在这里填写 *) Admitted.
☐

程序

正如我们刚刚将逻辑嵌入到 Rocq 中,我们现在需要将一种 【编程语言|Programming Language】嵌入到 Rocq 中。也就是说,我们用 【归纳类型|Inductive Type】来建模【程序|Program】的【语法|Syntax】,并用归纳定义的 【命题|Proposition】来建模【类型判定|Type Checking】。
我们形式化的语言是【简单类型纯 lambda 演算|Simply-Typed Pure Lambda Calculus】。 它是函数式编程中一个小而富有表达力的核心:程序由变量、【函数|Function】 和【函数应用|Function Application】组成。在这一细节层次上,程序与 证明之间的【对应|Correspondence】可以被清晰地陈述并精确地 证明。此外,这种演算已经捕捉到了有类型函数式语言背后的 本质结构,因此我们在此获得的结果可以很好地推广到远超这一 最小设定的范围。
语法
我们首先对程序的类型进行建模。
  • 【类型变量】 代表一个未知类型,类似于我们在 Rocq 中定义多态列表时使用的 X : Type 中的 X。我们同样使用 Rocq 的 string 作为标识符。
  • 【箭头】 表示函数类型,类似于 Rocq 中的 t1 → t2。
Inductive type :=
  | tvar (id : string)
  | arrow (t1 t2 : type).
接下来,我们为程序【表达式】 (又称【项】) 建模。
  • 与往常一样,我们有【变量】。
  • 【函数|Function】(又称【抽象】) 类似于 Rocq 中的 fun (x : t) ⇒ e。 它绑定一个变量,显式给出该变量的类型, 并以一个表达式作为其主体。
  • 【应用|Application】把函数作用于参数,类似于 Rocq 中的 e1 e2。
Inductive expr :=
  | var (id : string)
  | abs (id : string) (t : type) (e : expr)
  | app (e1 e2 : expr).
下面是一些程序的示例。
Definition e_I : expr :=
  (* fun (x:T) => x *)
  abs "x" (tvar "T") (var "x").

Definition e_K : expr :=
  (* fun (x:T) (y:U) => x *)
  abs "x" (tvar "T") (abs "y" (tvar "U") (var "x")).

Definition e_S : expr :=
  (* fun (x:T->U->V) (y:T->U) (z:T) => x z (y z) *)
  abs "x" (arrow (tvar "T") (arrow (tvar "U") (tvar "V")))
    (abs "y" (arrow (tvar "T") (tvar "U"))
      (abs "z" (tvar "T")
        (app (app (var "x") (var "z"))
             (app (var "y") (var "z"))))).
类型检查
一个类型系统是一组公理和推理规则,用于判定一个程序是否良类型。由于我们的小型编程语言在 abs 语法中显式地给出了每个变量的类型,为该语言构建类型系统相当容易。特别地,我们不需要推断任何类型。
我们类型系统的基础是一个类型判定关系,写作 E ⊢ e : t,其中 e 是一个表达式,t 是一个类型,E 是一个类型环境, 记录了各个变量的类型,而且冒号照例读作「具有类型」。
类型环境可以看作是一个将标识符绑定到类型的偏映射。我们写作 E, x:t 来表示包含 E 中所有绑定以及从 x 到 t 的绑定的映射。我们(不失一般性地,变量可以按需重命名)在这种记号中假定 x 尚未在 E 中被绑定。
类型判定关系由以下推理规则定义:
   (t-var)  

E, x : t ⊢ x : t
E, x : t1 ⊢ e : t2 (t-abs)  

E ⊢ fun (x : t1) => e : t1 -> t2
E ⊢ e1 : t1 -> t2    E ⊢ e2 : t1 (t-app)  

E ⊢ e1 e2 : t2
这些规则可以解释如下:
  • T-Var:如果类型判定环境将 x 绑定到 t,那么在该环境中 x 具有类型 t。
  • T-Abs:如果 e 在将 x 绑定到 t1 的类型判定环境中具有类型 t2, 那么通过抽象 x 得到的函数 fun (x : t1) ⇒ e 具有类型 t1 → t2。
  • T-App:如果 e1 是具有类型 t1 → t2 的函数,且 e2 具有类型 t1, 那么将 e1 应用于输入 e2 将得到类型为 t2 的输出。
作为偏映射的一种简单表示,我们使用 【关联列表|Association List】, 即一个由若干对组成的列表,其中每对的第一个分量是 【键|Key】, 第二个分量是该键所绑定的值。为简单起见,我们省略了任何禁止重复键的检查。
Definition tenv := list (string × type).
【类型系统|Type System】的【推导规则|Inference Rule】在 Rocq 中形式化是直接的。
Inductive hastype : tenv → expr → type → Prop :=
  | t_var : ∀ E x t,
      In (x, t) E →
      hastype E (var x) t
  | t_abs : ∀ E x t1 e t2,
      hastype ((x, t1) :: E) e t2 →
      hastype E (abs x t1 e) (arrow t1 t2)
  | t_app : ∀ E e1 t1 t2 e2,
      hastype E e1 (arrow t1 t2) →
      hastype E e2 t1 →
      hastype E (app e1 e2) t2.
上面给出的示例程序都是良类型的。我们的 by_assumption 策略对于关联列表同样能很好地工作。
Example e_I_typable :
  hastype [] e_I (arrow (tvar "T") (tvar "T")).
(*
        ---------------------------- (t-var)
               x : T ⊢ x : T
        ---------------------------- (t-abs)
        ⊢ fun (x : T) => x : T -> T
*)

Proof.
  unfold e_I. apply t_abs. by_assumption.
Qed.

Example e_K_typable :
  hastype [] e_K (arrow (tvar "T") (arrow (tvar "U") (tvar "T"))).
(*
      ------------------------------------------------ (t-var)
                   x : T, y : U ⊢ x : T
      ------------------------------------------------ (t-abs)
            x : T ⊢ fun (y : U) => x : U -> T
      ------------------------------------------------ (t-abs)
      ⊢ fun (x : T) => fun (y : U) => x : T -> U -> T
*)

Proof.
  (* 已在课上完成 *)
  unfold e_K. apply t_abs. apply t_abs. by_assumption.
Qed.

练习: 2 stars,标准 (e_S_typable)

证明上面的第三个示例程序是【可类型化|Typable】的。 提示:先试着在纸上把证明画出来。
Theorem e_S_typable :
  hastype [] e_S
    (arrow (arrow (tvar "T") (arrow (tvar "U") (tvar "V")))
           (arrow (arrow (tvar "T") (tvar "U"))
                  (arrow (tvar "T") (tvar "V")))).
Proof.
  (* 在这里填写 *) Admitted.
☐

练习: 3 stars,标准 (more_typable_programs)

Definition prog_B :=
  (abs "g" (arrow (tvar "Y") (tvar "Z"))
    (abs "f" (arrow (tvar "X") (tvar "Y"))
      (abs "x" (tvar "X")
        (app (var "g") (app (var "f") (var "x")))))).

Theorem prog_B_typable : ∃ (T : type),
  hastype [] prog_B T.
Proof.
  (* 在这里填写 *) Admitted.

Definition prog_C :=
  (abs "f" (arrow (tvar "X") (arrow (tvar "Y") (tvar "Z" )))
    (abs "y" (tvar "Y")
      (abs "x" (tvar "X")
        (app (app (var "f") (var "x")) (var "y"))))).

Theorem prog_C_typable : ∃ (T : type),
  hastype [] prog_C T.
Proof.
  (* 在这里填写 *) Admitted.

Definition prog_W_type :=
  arrow (arrow (tvar "X") (arrow (tvar "X") (tvar "Y")))
        (arrow (tvar "X") (tvar "Y")).

Theorem prog_W_typable : ∃ (e : expr),
  hastype [] e prog_W_type.
Proof.
  (* 在这里填写 *) Admitted.
☐

对应关系

借助【可证性|Provability】和【可类型性|Typability】的形式化, 我们现在可以将柯里-霍华德对应(Curry-Howard Correspondence)精确化。我们不再把它当作非正式的类比, 而是将证明系统(Proof System)的推理规则与类型系统(Type System)的推理规则进行比较。
核心思想是:每条逻辑规则都有一条对应的类型判定(Typing)规则,并且类型推导(Typing Derivation)可以看作带有额外计算内容的证明推导(Proof Derivation)。为了看清这一点,我们并排考察这些规则, 观察它们如何表达相同的底层结构。

箭头消去

首先,考虑涉及消去箭头的规则,无论是函数类型箭头还是蕴含箭头:
E ⊢ e1 : t1 -> t2    E ⊢ e2 : t1 (t-app)  

E ⊢ e1 e2 : t2
A ⊢ p -> q         A ⊢ p (蕴含消去)  

A ⊢ q
]]] *)
(** 观察如果从 [t-app] 中删除每个竖线符号 [⊢](不含)与冒号(含)之间的所有内容,会发生什么: *)
(**
[[[
E ⊢ t1 -> t2       E ⊢ t1 (t-app-erased)  

E ⊢ t2
A ⊢ p -> q         A ⊢ p (implies elim)  

A ⊢ q
所得规则(即使它不再是一个良类型的类型判定规则) 看起来与 implies elim 【完全】相同。这绝非偶然!
回想一下,我们可以把蕴含【Implication|Implication】看作一种变换:它取 p 的证明, 并将其变换为 q 的证明。同样地,箭头类型也是一种变换:它取类型为 t1 的 输入,并将其变换为类型为 t2 的输出。
因此,类型判定规则【Typing Rule】t-app 表达的想法与可证性规则 implies elimination 是相同的,但它还附加了实现该变换的代码 e1。
蕴含引入
其次,考虑引入箭头(arrow)的规则:
E, x : t1 ⊢ e : t2 (t-abs)  

E ⊢ fun (x : t1) => e : t1 -> t2
A, p ⊢ q (implies intro)  

A ⊢ p -> q
]]] *)
(** 与之前一样,如果我们从 [t-abs] 中删去程序,就得到一条看起来与 [implies introduction] 完全相同的规则: *)
(**
[[[
E, t1 ⊢ t2 (t-abs-erased)  

E ⊢ t1 -> t2
A, p ⊢ q (【蕴含引入|Implies Intro】)  

A ⊢ p -> q
再次说明,这绝非偶然。二者都表达了如何创建一个转换器的思想,无论该转换器是蕴含还是函数。类型判定规则 t-abs 只是更加明确,它提供了实现该转换的代码 fun (x : t1) ⇒ e。
假设
第三,也是最后,考虑涉及假设的规则,以及类型判定规则的擦除:
   (t-var)  

E, x : t ⊢ x : t
   (t-var-erased)  

E, t ⊢ t
   (axiom)  

A, p ⊢ p
再次地,t-var-erased 看起来与其逻辑伙伴 axiom 完全一样。 这些规则表达的是同一条基本的推理原理,即「基于假设」的推理。

证明对应关系

上面我们看到,将程序从【类型判定|Typing】规则中擦除后,基本上只剩下【可证性|Provability】规则。因此,任何【程序|Program】 的【类型|Type】的推导也同时建立了对应【命题|Proposition】可证性的推导。
例如,下面是程序类型 X → (Y → X) 的一个推导:
        ------------------------------------------------- (t-var)
                         x:X, y:Y ⊢ x : X
        ------------------------------------------------- (t-abs)
                  x:X ⊢ fun (y:Y) => x : Y -> X
        ------------------------------------------------- (t-abs)
        ⊢ fun (x:X) => (fun (y:Y) => x)) : X -> (Y -> X)
而把该推导中的代码抹去后,我们得到的是 X → (Y → X) 可证性的一个推导:
        ---------------- (axiom)
            X, Y ⊢ X
        ---------------- (implies introduction)
          X ⊢ Y -> X
        ---------------- (implies introduction)
        ⊢ X -> (Y -> X)
因此,我们的逻辑【证明系统|Proof System】规则与【类型系统|Type System】规则之间存在【对应|Correspondence】关系:
  • axiom 对应 t_var。
  • implies_intro 对应 t_abs。
  • implies_elim 对应 t_app。
因此,逻辑【联结词|Connective】与编程语言特性之间也存在对应关系:
  • 【命题变量|Propositional Variable】 对应 【程序变量|Program Variable】。
  • 【蕴含|Implication】 对应 【函数|Function】与【应用|Application】。
然而,我们的【类型|Type】与【命题|Proposition】数据结构之间存在一点不匹配。 我们可以用下面这个函数把类型转换为命题:
Fixpoint proposition_of_type (t : type) : proposition :=
  match t with
  | tvar x ⇒ atom x
  | arrow t1 t2 ⇒
    implies (proposition_of_type t1) (proposition_of_type t2)
  end.

Example pot_XY :
  proposition_of_type
    (arrow (tvar "X") (arrow (tvar "Y") (tvar "X")))
  =
    (implies (atom "X") (implies (atom "Y") (atom "X"))).
Proof. reflexivity. Qed.
借助该函数,我们可以把【类型环境|Type Environment】转换为【假设|Assumption】列表:
Definition assumptions_of_tenv (E : tenv) : list proposition :=
  map proposition_of_type (map snd E).

Example aot_XY :
  assumptions_of_tenv
    [("x"%string, tvar "X"); ("y"%string, tvar "Y")]
  =
    [atom "X"; atom "Y"].
Proof. reflexivity. Qed.

练习: 2 stars,标准 (in_snd)

下面的引理在下面的主定理中应当会派上用场。提示:对 l 进行归纳。
Lemma in_snd : ∀ (X Y Z : Type) (x : X) (y : Y) (f : Y → Z) (l : list (X×Y)),
  In (x, y) l → In (f y) (map f (map snd l)).
Proof. (* 在这里填写 *) Admitted.
☐

练习: 3 stars,标准 (curry_howard)

终于我们可以陈述【柯里-霍华德对应|Curry-Howard Correspondence】两条主要定理中的第一条:在对 E 和 t 施加适当的转换函数之后,由 E ⊢ e : t 可得 E ⊢ t。也就是说,一个程序的【类型判定|Typing】 推导也给出一个命题的【可证性|Provability】【推导|Derivation】。 (本章末尾我们再回过头来讨论这两条主要定理中的第二条。)
提示:对 hastype 的证据进行归纳。
Theorem curry_howard : ∀ E e t,
  hastype E e t →
  provable (assumptions_of_tenv E) (proposition_of_type t).
Proof. (* 在这里填写 *) Admitted.
☐

将对应关系扩展到更大的系统

Module Propositional.
逻辑:加入合取与析取
现在我们将这一发展扩展为包含合取与析取的完整命题逻辑。
Inductive proposition :=
  | atom (id : string)
  | implies (p q : proposition)
  | and (p q : proposition)
  | or (p q : proposition)
  | false.

Definition not (f : proposition) : proposition :=
  implies f false.

Definition true : proposition :=
  implies false false.

Definition iff (p q : proposition) :=
  and (implies p q) (implies q p).
新的证明系统规则如下:
A ⊢ p   A ⊢ q (合取引入)  

A ⊢ p /\ q
A ⊢ p /\ q (合取消去左)  

A ⊢ p
A ⊢ p /\ q (合取消去右)  

A ⊢ q
A ⊢ p (析取引入左)  

A ⊢ p \/ q
A ⊢ q (析取引入右)  

A ⊢ p \/ q
A, p ⊢ r    A, q ⊢ r    A ⊢ p \/ q (析取消去)  

A ⊢ r
新的规则可以解释如下:
  • And 引入(合取引入):如果可以从 A 证明 p 和 q,那么就可以得出 p ∧ q 是可从 A 证明的。也就是说,一个合取的证明由其每个组成部分的 证明构成。
  • And 消去(左):如果可以从 A 证明 p ∧ q,那么就可以得出 p 是 可从 A 证明的。也就是说,从一个合取的证明中,我们可以提取出它的 第一个组成部分。
  • And 消去(右):如果可以从 A 证明 p ∧ q,那么就可以得出 q 是 可从 A 证明的。
  • Or 引入(析取引入,左):如果可以从 A 证明 p,那么就可以得出 p ∨ q 是可从 A 证明的。也就是说,要证明一个析取,只需证明它的 某一个组成部分,并指明我们证明的是哪一个组成部分(通过选择左规则或 右规则)。
  • Or 引入(析取引入,右):如果可以从 A 证明 q,那么就可以得出 p ∨ q 是可从 A 证明的。
  • Or 消去(析取消去):如果从 A, p 可以证明 r,从 A, q 也可以 证明 r,并且我们还有 p ∨ q 从 A 的证明,那么就可以得出 r 是 可从 A 证明的。也就是说,要使用一个析取的证明,我们必须考虑两种 情况,即假设 p 和假设 q,并证明在每种情况下都能得出相同的结论 r。
我们将它们形式化如下。
Inductive provable : list proposition → proposition → Prop :=
  (* 前四条规则来自此前的开发。 *)
  | axiom : ∀ A p,
      In p A →
      provable A p
  | implies_intro : ∀ A p q,
      provable (p :: A) q →
      provable A (implies p q)
  | implies_elim : ∀ A p q,
      provable A (implies p q) →
      provable A p →
      provable A q
  | false_elim : ∀ A p,
      provable A false →
      provable A p
  (* 接下来的六条规则是新的。 *)
  | and_intro : ∀ A p q,
      provable A p →
      provable A q →
      provable A (and p q)
  | and_elim_L : ∀ A p q,
      provable A (and p q) →
      provable A p
  | and_elim_R : ∀ A p q,
      provable A (and p q) →
      provable A q
  | or_intro_L : ∀ A p q,
      provable A p →
      provable A (or p q)
  | or_intro_R : ∀ A p q,
      provable A q →
      provable A (or p q)
  | or_elim : ∀ A p q r,
      provable (p :: A) r →
      provable (q :: A) r →
      provable A (or p q) →
      provable A r.

Ltac by_assumption :=
  constructor;
  repeat match goal with
  | ⊢ In ?p (?h :: ?t) ⇒ simpl
  | ⊢ (?x = ?y) ∨ ?p ⇒ first [left; reflexivity | right]
  | ⊢ False ⇒ idtac "by_assumption failed: not in assumptions"; fail
  end.

练习: 2 stars,标准 (false_iff_inconsistency)

证明 false 等价于【不一致性|Inconsistency】。
Theorem false_iff_inconsistency : ∀ (p : proposition),
  provable [] (iff false (and p (not p))).
Proof.
  (* 在这里填写 *) Admitted.
☐

练习: 2 stars,标准 (and_comm)

证明 and 满足交换性。
Theorem and_comm : ∀ (p q : proposition),
  provable [] (iff (and p q) (and q p)).
Proof.
  (* 在这里填写 *) Admitted.
☐

练习: 2 stars,标准 (or_comm)

证明 or 是可交换的。
Theorem or_comm : ∀ (p q : proposition),
  provable [] (iff (or p q) (or q p)).
Proof.
  (* 在这里填写 *) Admitted.
☐

练习: 3 stars,标准 (and_distr_or)

证明 and 对 or 具有分配律。
Theorem and_distr_or : ∀ (p q r : proposition),
  provable [] (iff (and p (or q r)) (or (and p q) (and p r))).
Proof.
  (* 在这里填写 *) Admitted.
☐

练习: 3 stars,标准,可选 (or_distr_and)

证明 or 对 and 满足分配律。
Theorem or_distr_and : ∀ (p q r : proposition),
  provable [] (iff (or p (and q r)) (and (or p q) (or p r))).
Proof.
  (* 在这里填写 *) Admitted.
☐
程序:加入积、和与空类型
接下来我们将编程语言扩展,使其支持【序对|Pair】和【变体|Variant】。
【序对】就像我们在 Rocq 中先前使用的两元素对,例如 (1, true)。 序对的每个分量可能具有不同的类型。序对的类型是【积类型|Product Type】。 非正式地,我们用 t1 × t2 表示积类型。我们引入【投影函数|Projection】fst 和 snd 来返回序对的第一个和第二个分量。
【变体】类似于如下的 Rocq 类型:

        Inductive sum (T1 T2 : Type) :=
        | inl : T1 → sum T1 T2
        | inr : T2 → sum T1 T2.
也就是说,它有两个名为 inl 和 inr 的【构造子|Constructor】。这些就是所谓的【注入函数|Injection】, 它们接受单个值并将其注入到 sum 类型中。l 和 r 代表「left」和「right」,即类型是积在左侧的 T1 还是右侧的 T2。因此,和类型的值要么是类型 T1 的值,要么是类型 T2 的值,并附带一个标记(即构造子)来说明它来自哪个类型。非正式地,我们用 T1 + T2 表示和类型。
为了使用 sum,我们添加了 case 表达式,它说明了如何根据被检查的值是用 inl 还是 inr 标记来计算结果。非正式地,我们将其记作
        case e of (x1 : t1) => e1 | (x2 : t2) => e2 end
这与如下的 Rocq 表达式非常相似:

        match e with (x1 : t1) ⇒ e1 | (x2 : t2) ⇒ e2 end
事实上我们很乐意使用「match」作为它的名称,但这个词已被 Rocq 保留。我们的 case 表达式需要类型标注,这样我们就不必担心类型推断或类型重建的问题。
我们还借此机会引入了一个新的类型和表达式,它们将在很久之后用于完成主要的 Curry-Howard 定理中的第二条。我们引入了【空类型|Empty Type】, 它没有任何值。它对应于 false 命题。
此外,我们引入了 magic 程序,其中 magic t e 「神奇地」成为类型 t 的一个表达式,只要 e : empty, 这当然是不可能的,因为 empty 类型没有值。magic 程序在某种程度上类似于类型转换, 只不过某些语言中的类型转换允许在没有依据的情况下进行任意转换,而 magic 则由一个不可能的前提(空类型的一个值)来保证其合理性。
Inductive type :=
  | tvar (id : string)
  | arrow (t1 t2 : type)
  | product (t1 t2 : type)
  | sum (t1 t2 : type)
  | empty.

Inductive expr :=
  | var (id : string)
  | abs (id : string) (t : type) (e : expr)
  | app (e1 e2 : expr)
  | magic (t : type) (e : expr)
  | pair (e1 e2 : expr)
  | fst (e : expr)
  | snd (e : expr)
  | inl (e1 : expr)
  | inr (e2 : expr)
  | case (e : expr) (id1 : string) (t1 : type) (e1 : expr)
                    (id2 : string) (t2 : type) (e2 : expr).
新的【类型系统|Type System】规则如下:
E ⊢ e : empty (t-magic)  

E ⊢ magic t e : t
E ⊢ e1 : t1    E ⊢ e2 : t2 (t-pair)  

E ⊢ pair e1 e2 : t1 * t2
E ⊢ e : t1 * t2 (t-fst)  

E ⊢ fst e : t1
E ⊢ e : t1 * t2 (t-snd)  

E ⊢ snd e : t2
E ⊢ e : t1 (t-inl)  

E ⊢ inl e : t1 + t2
E ⊢ e : t2 (t-inr)  

E ⊢ inr e : t1 + t2
E ⊢ e : t1 + t2   E, x1:t1 ⊢ e1 : t   E, x2:t2 ⊢ e2 : t (t-case)  

E ⊢ case e of (x1 : t1) => e1 | (x2 : t2) => e2 end : t
新规则可以解释如下:
  • T-Magic: 若 e 的【类型|Type】为 empty,则对任意 t,magic t e 的类型为 t。也就是说,
从一个不可能的值 (类型为 empty),我们可以产生任意类型的值。
  • T-Pair: 若 e1 的类型为 t1,e2 的类型为 t2,则【序对|Pair】pair e1 e2 的类型为 t1 × t2。也就是说,序对将两个值打包在一起,每种类型各一个。
  • T-Fst: 若 e 的类型为 t1 × t2,则 fst e 的类型为 t1。也就是说,从序对中我们可以提取它的第一个分量。
  • T-Snd: 若 e 的类型为 t1 × t2,则 snd e 的类型为 t2。也就是说,从序对中我们可以提取它的第二个分量。
  • T-Inl: 若 e 的类型为 t1,则对任意类型 t2,inl e 的类型为 t1 + t2。也就是说,我们可以把一个值
【注入|Injection】到【和类型|Sum Type】的左侧。
  • T-Inr: 若 e 的类型为 t2,则对任意类型 t1,inr e 的类型为 t1 + t2。也就是说,我们可以把一个值注入到和类型的右侧。
  • T-Case: 若 e 的类型为 t1 + t2,并且若假设 x1 : t1 我们能证明 e1 的类型为 t,假设 x2 : t2 我们能证明 e2 的类型为 t,则 case 表达式的类型为 t。也就是说,要使用和类型的值,我们必须考虑两种可能性,
每种注入对应一种,并确保两个分支产生相同类型的结果。
我们将其形式化如下。
Definition tenv := list (string × type).

Inductive hastype : tenv → expr → type → Prop :=
  (* 前三条规则来自此前的开发。 *)
  | t_var : ∀ E x t,
      In (x, t) E →
      hastype E (var x) t
  | t_abs : ∀ E x t1 e t2,
      hastype ((x, t1) :: E) e t2 →
      hastype E (abs x t1 e) (arrow t1 t2)
  | t_app : ∀ E e1 t1 t2 e2,
      hastype E e1 (arrow t1 t2) →
      hastype E e2 t1 →
      hastype E (app e1 e2) t2
  (* 接下来的七条规则是新的。 *)
  | t_magic : ∀ E e t,
      hastype E e empty →
      hastype E (magic t e) t
  | t_pair : ∀ E e1 e2 t1 t2,
      hastype E e1 t1 →
      hastype E e2 t2 →
      hastype E (pair e1 e2) (product t1 t2)
  | t_fst : ∀ E e t1 t2,
      hastype E e (product t1 t2) →
      hastype E (fst e) t1
  | t_snd : ∀ E e t1 t2,
      hastype E e (product t1 t2) →
      hastype E (snd e) t2
  | t_inl : ∀ E e t1 t2,
      hastype E e t1 →
      hastype E (inl e) (sum t1 t2)
  | t_inr : ∀ E e t1 t2,
      hastype E e t2 →
      hastype E (inr e) (sum t1 t2)
  | t_case : ∀ E e x1 t1 e1 x2 t2 e2 t,
      hastype E e (sum t1 t2) →
      hastype ((x1, t1) :: E) e1 t →
      hastype ((x2, t2) :: E) e2 t →
      hastype E (case e x1 t1 e1 x2 t2 e2) t.

练习: 2 stars,标准 (swap_product)

证明存在一个能够交换乘积的程序。
Theorem swap_product :
  ∀ (t1 t2 : type), ∃ (e : expr),
    hastype [] e (arrow (product t1 t2) (product t2 t1)).
Proof.
  (* 在这里填写 *) Admitted.
☐

练习: 2 stars,标准 (swap_sum)

证明存在一个能够交换 sum 的程序。
Theorem swap_sum :
  ∀ (t1 t2 : type), ∃ (e : expr),
    hastype [] e (arrow (sum t1 t2) (sum t2 t1)).
Proof.
  (* 在这里填写 *) Admitted.
☐

练习: 3 stars,标准 (product_of_sum__sum_of_product)

证明存在一个能够对和式分配乘积的程序。
Theorem product_of_sum__sum_of_product :
  ∀ (t1 t2 t3 : type), ∃ (e : expr),
    hastype [] e (arrow (product t1 (sum t2 t3))
                        (sum (product t1 t2) (product t1 t3))).
Proof. (* 在这里填写 *) Admitted.
☐
证明扩展对应
我们新增的规则同样存在对应关系:
  • false_elim 对应 t_magic。
  • and_intro 对应 t_pair。
  • _and_elim_L 对应 t_fst。
  • and_elim_R 对应 t_snd。
  • or_intro_L 对应 t_inl。
  • or_intro_R 对应 t_inr。
  • or_elim 对应 t_case。
因此,逻辑【联结词|Connective】与编程语言特性之间存在扩展的【对应|Correspondence】关系:
  • (命题变量对应程序变量。)
  • (【蕴含|Implication】对应【函数|Function】和【应用|Application】。)
  • 【假|False】对应【空类型|Empty Type】。
  • 【合取|Conjunction】对应【序对|Pair】。
  • 【析取|Disjunction】对应【变体|Variant】。
现在我们可以为扩展后的逻辑和程序语言重新证明两条主要的 Curry-Howard 定理中的第一条。首先,我们扩展转换函数,将积转换为【合取|Conjunction】, 将和转换为【析取|Disjunction】。
Fixpoint proposition_of_type (t : type) : proposition :=
  match t with
  | tvar x ⇒ atom x
  | arrow t1 t2 ⇒
    implies (proposition_of_type t1) (proposition_of_type t2)
  | product t1 t2 ⇒
    and (proposition_of_type t1) (proposition_of_type t2)
  | sum t1 t2 ⇒
    or (proposition_of_type t1) (proposition_of_type t2)
  | empty ⇒ false
  end.

Definition assumptions_of_tenv (E : tenv) : list proposition :=
  map proposition_of_type (map Datatypes.snd E).

练习: 3 stars,标准 (curry_howard_extended)

对一个程序的【可类型性|Typability】推导仍然给出该命题的【可证性|Provability】推导。
提示:证明的前三个情形应当只需对你的上一版本做最小改动即可通过。新增情形的难度应与之前的情形相近。
Theorem curry_howard_extended : ∀ E e t,
  hastype E e t →
  provable (assumptions_of_tenv E) (proposition_of_type t).
Proof. (* 在这里填写 *) Admitted.
☐

对应的第二基本定理 (进阶)

到目前为止,我们已经证明,对 E 和 t 应用适当的转换函数后, 如果 E ⊢ e : t,则 E ⊢ t。也就是说, 【程序|Program】的可类型性【推导|Derivation】也给出 【命题|Proposition】的【可证性|Provability】推导。
反方向呢?能否从可证性推导得到可类型性推导? 答案是肯定的,但这需要更多的工作。原因在于, 在原来的方向上,我们可以从推导中擦除信息(程序)来完成转换。 而在新的方向上,我们必须【合成】缺失的程序。
首先,我们定义一个函数,将命题转换为字符串表示。
Fixpoint string_of_proposition (p : proposition) : string :=
  match p with
  | atom id ⇒ "(" ++ id ++ ")"
  | implies p q ⇒
    "(implies " ++ (string_of_proposition p) ++ " "
                ++ (string_of_proposition q) ++ ")"
  | and p q ⇒
    "(and " ++ (string_of_proposition p) ++ " "
            ++ (string_of_proposition q) ++ ")"
  | or p q ⇒
    "(or " ++ (string_of_proposition p) ++ " "
           ++ (string_of_proposition q) ++ ")"
  | false ⇒ "(false)"
  end.
其次,我们定义一个函数,将命题转换为与其等价的类型。
Fixpoint type_of_proposition (p : proposition) : type :=
  match p with
  | atom id ⇒ tvar id
  | implies p q ⇒ arrow (type_of_proposition p) (type_of_proposition q)
  | and p q ⇒ product (type_of_proposition p) (type_of_proposition q)
  | or p q ⇒ sum (type_of_proposition p) (type_of_proposition q)
  | false ⇒ empty
  end.
第三,我们定义一个函数,把【假设|Assumption】转换为【类型环境|Type Environment】。 每个假设都有一个名字,也就是它到字符串的转换。
现在我们准备好了 Curry-Howard 两条主要定理中的第二条。

练习: 4 stars,高级 (curry_howard_converse)

证明若一个【命题|Proposition】是【可证|Provable】的,则存在一个【良类型|Well-Typed】的【程序|Program】,其类型与该命题相对应。
提示:对 p 可证的证据进行归纳。 在(众多的)归纳情形中,使用归纳假设来推导某些子表达式的存在, 然后将它们组合成一个具有所需类型的表达式。
Theorem curry_howard_converse : ∀ A p,
  provable A p →
  ∃ e, hastype (tenv_of_assumptions A) e (type_of_proposition p).
Proof. (* 在这里填写 *) Admitted.
☐

进一步的扩展

柯里-霍华德对应(Curry–Howard correspondence)远远超出了我们在本章中研究过的系统。我们在这里看到的, 只是逻辑与编程之间更丰富联系的开端。
还有许多其他逻辑构造对应于程序语言特性,包括:
  • 【经典公理】,对应于【控制流运算符】(例如与异常和非局部跳转相关的机制)。
  • 【一阶全称量化】,对应于【依赖类型】,其中类型可以依赖于值。
  • 【一阶存在量化】,对应于【抽象类型】,它隐藏了实现细节。
  • 【二阶命题量化】,对应于【多态】,其中程序可以针对类型以通用方式编写。
这些对应关系不只是有趣的巧合:它们构成了现代类型系统、证明助手以及构建可靠软件技术的基础。你在这里看到的这些思想,重新出现在程序语言的设计中、 关键系统的验证中,以及逻辑与计算交界处的持续研究中。
深入的探讨请参见 [Sørensen and Urzyczyn 2006]。
(* 2026-11-26 15:54 *)