CurryHowardCurry-Howard 对应的形式化开发
我们首先在一个以【蕴含|Implication】为重点的设定中展开这一【对应|Correspondence】。随后,我们将加入【合取|Conjunction】
与【析取|Disjunction】。
逻辑
命题
- 【原子|Atom】是一个原子命题,很像 P : Prop 中的 P,正如我们习惯于
Rocq 中的那样。原子命题使用 Rocq 的 string 来命名。
- 蕴含是通常的 P → Q。
- 矛盾命题被命名为 false。
从该起点出发,我们可以用其他命题来定义否定与真值。
Definition not (p : proposition) : proposition :=
implies p false.
Definition true : proposition :=
implies false false.
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"))).
(* 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} 的并集:
这些规则可以如下解读:
将【自然演绎|Natural Deduction】的推理规则形式化到 Rocq 中是直截了当的。
将【假设|Assumption】集合表示为列表很方便,
因为我们对这种类型已经有了大量经验。
| (公理) | |
| 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。
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.
| 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)
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.
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)
Theorem ex_PQR_provable_rocq : ∀ (P Q R : Prop),
(P → Q → R) → (P → Q) → P → R.
Proof.
(* 在这里填写 *) Admitted.
(P → Q → R) → (P → Q) → P → R.
Proof.
(* 在这里填写 *) Admitted.
其次,使用自然演绎来证明它。
提示:先试着在纸上用推理规则草拟出推导过程。
难点在于每次都要找到正确的命题来实例化 implies_elim。
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.
☐
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 中定义多态列表时使用的
X : Type 中的 X。我们同样使用 Rocq 的 string 作为标识符。
- 【箭头】 表示函数类型,类似于 Rocq 中的 t1 → t2。
接下来,我们为程序【表达式】 (又称【项】) 建模。
- 与往常一样,我们有【变量】。
- 【函数|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).
| 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"))))).
(* 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 中被绑定。
类型判定关系由以下推理规则定义:
这些规则可以解释如下:
作为偏映射的一种简单表示,我们使用 【关联列表|Association List】,
即一个由若干对组成的列表,其中每对的第一个分量是 【键|Key】,
第二个分量是该键所绑定的值。为简单起见,我们省略了任何禁止重复键的检查。
| (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 的输出。
【类型系统|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.
| 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.
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.
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.
☐
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.
☐
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.
☐
(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.
☐
对应关系
箭头消去
| 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 |
蕴含引入
其次,考虑引入箭头(arrow)的规则:
再次说明,这绝非偶然。二者都表达了如何创建一个转换器的思想,无论该转换器是蕴含还是函数。类型判定规则 t-abs
只是更加明确,它提供了实现该转换的代码 fun (x : t1) ⇒ e。
| 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-var-erased 看起来与其逻辑伙伴 axiom 完全一样。
这些规则表达的是同一条基本的推理原理,即「基于假设」的推理。
| (t-var) | |
| E, x : t ⊢ x : t |
| (t-var-erased) | |
| E, t ⊢ t |
| (axiom) | |
| A, p ⊢ p |
证明对应关系
------------------------------------------------- (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)
---------------- (axiom)
X, Y ⊢ X
---------------- (implies introduction)
X ⊢ Y -> X
---------------- (implies introduction)
⊢ X -> (Y -> X)
- axiom 对应 t_var。
- implies_intro 对应 t_abs。
- implies_elim 对应 t_app。
- 【命题变量|Propositional Variable】 对应 【程序变量|Program Variable】。
- 【蕴含|Implication】 对应 【函数|Function】与【应用|Application】。
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.
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.
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.
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.
☐
In (x, y) l → In (f y) (map f (map snd l)).
Proof. (* 在这里填写 *) Admitted.
☐
练习: 3 stars,标准 (curry_howard)
Theorem curry_howard : ∀ E e t,
hastype E e t →
provable (assumptions_of_tenv E) (proposition_of_type t).
Proof. (* 在这里填写 *) Admitted.
☐
hastype E e t →
provable (assumptions_of_tenv E) (proposition_of_type t).
Proof. (* 在这里填写 *) Admitted.
☐
逻辑:加入合取与析取
现在我们将这一发展扩展为包含合取与析取的完整命题逻辑。
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).
| 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.
(* 前四条规则来自此前的开发。 *)
| 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.
Theorem false_iff_inconsistency : ∀ (p : proposition),
provable [] (iff false (and p (not p))).
Proof.
(* 在这里填写 *) Admitted.
☐
provable [] (iff false (and p (not p))).
Proof.
(* 在这里填写 *) Admitted.
☐
Theorem and_comm : ∀ (p q : proposition),
provable [] (iff (and p q) (and q p)).
Proof.
(* 在这里填写 *) Admitted.
☐
provable [] (iff (and p q) (and q p)).
Proof.
(* 在这里填写 *) Admitted.
☐
Theorem or_comm : ∀ (p q : proposition),
provable [] (iff (or p q) (or q p)).
Proof.
(* 在这里填写 *) Admitted.
☐
provable [] (iff (or p q) (or q p)).
Proof.
(* 在这里填写 *) Admitted.
☐
Theorem and_distr_or : ∀ (p q r : proposition),
provable [] (iff (and p (or q r)) (or (and p q) (and p r))).
Proof.
(* 在这里填写 *) Admitted.
☐
provable [] (iff (and p (or q r)) (or (and p q) (and p r))).
Proof.
(* 在这里填写 *) Admitted.
☐
Theorem or_distr_and : ∀ (p q r : proposition),
provable [] (iff (or p (and q r)) (and (or p q) (or p r))).
Proof.
(* 在这里填写 *) Admitted.
☐
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 标记来计算结果。非正式地,我们将其记作
这与如下的 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 sum (T1 T2 : Type) :=
| inl : T1 → sum T1 T2
| inr : T2 → sum T1 T2.
case e of (x1 : t1) => e1 | (x2 : t2) => e2 end
match e with (x1 : t1) ⇒ e1 | (x2 : t2) ⇒ e2 end
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).
| 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。也就是说,
- 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。也就是说,我们可以把一个值
- 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.
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.
Theorem swap_product :
∀ (t1 t2 : type), ∃ (e : expr),
hastype [] e (arrow (product t1 t2) (product t2 t1)).
Proof.
(* 在这里填写 *) Admitted.
☐
∀ (t1 t2 : type), ∃ (e : expr),
hastype [] e (arrow (product t1 t2) (product t2 t1)).
Proof.
(* 在这里填写 *) Admitted.
☐
Theorem swap_sum :
∀ (t1 t2 : type), ∃ (e : expr),
hastype [] e (arrow (sum t1 t2) (sum t2 t1)).
Proof.
(* 在这里填写 *) Admitted.
☐
∀ (t1 t2 : type), ∃ (e : expr),
hastype [] e (arrow (sum t1 t2) (sum t2 t1)).
Proof.
(* 在这里填写 *) Admitted.
☐
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.
☐
∀ (t1 t2 t3 : type), ∃ (e : expr),
hastype [] e (arrow (product t1 (sum t2 t3))
(sum (product t1 t2) (product t1 t3))).
Proof. (* 在这里填写 *) Admitted.
☐
证明扩展对应
我们新增的规则同样存在对应关系:
因此,逻辑【联结词|Connective】与编程语言特性之间存在扩展的【对应|Correspondence】关系:
现在我们可以为扩展后的逻辑和程序语言重新证明两条主要的 Curry-Howard 定理中的第一条。首先,我们扩展转换函数,将积转换为【合取|Conjunction】,
将和转换为【析取|Disjunction】。
- 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。
- (命题变量对应程序变量。)
- (【蕴含|Implication】对应【函数|Function】和【应用|Application】。)
- 【假|False】对应【空类型|Empty Type】。
- 【合取|Conjunction】对应【序对|Pair】。
- 【析取|Disjunction】对应【变体|Variant】。
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).
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)
Theorem curry_howard_extended : ∀ E e t,
hastype E e t →
provable (assumptions_of_tenv E) (proposition_of_type t).
Proof. (* 在这里填写 *) Admitted.
☐
hastype E e t →
provable (assumptions_of_tenv E) (proposition_of_type t).
Proof. (* 在这里填写 *) Admitted.
☐
对应的第二基本定理 (进阶)
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.
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.
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】。
每个假设都有一个名字,也就是它到字符串的转换。
Definition tenv_of_assumptions (A : list proposition) : tenv :=
map (fun p ⇒ (string_of_proposition p, type_of_proposition p)) A.
map (fun p ⇒ (string_of_proposition p, type_of_proposition p)) A.
现在我们准备好了 Curry-Howard 两条主要定理中的第二条。
证明若一个【命题|Proposition】是【可证|Provable】的,则存在一个【良类型|Well-Typed】的【程序|Program】,其类型与该命题相对应。
提示:对 p 可证的证据进行归纳。
在(众多的)归纳情形中,使用归纳假设来推导某些子表达式的存在,
然后将它们组合成一个具有所需类型的表达式。
练习: 4 stars,高级 (curry_howard_converse)
Theorem curry_howard_converse : ∀ A p,
provable A p →
∃ e, hastype (tenv_of_assumptions A) e (type_of_proposition p).
Proof. (* 在这里填写 *) Admitted.
☐
provable A p →
∃ e, hastype (tenv_of_assumptions A) e (type_of_proposition p).
Proof. (* 在这里填写 *) Admitted.
☐
进一步的扩展
- 【经典公理】,对应于【控制流运算符】(例如与异常和非局部跳转相关的机制)。
- 【一阶全称量化】,对应于【依赖类型】,其中类型可以依赖于值。
- 【一阶存在量化】,对应于【抽象类型】,它隐藏了实现细节。
- 【二阶命题量化】,对应于【多态】,其中程序可以针对类型以通用方式编写。
(* 2026-11-26 15:54 *)
