Rel关系的性质
有点令人困惑的是,Rocq 标准库把通用术语「relation」挪用给了
这一特定实例。为与标准库保持一致,我们也照做。因此从现在起,
Rocq 标识符 relation 总是指某个集合【上】的二元关系,
也就是该集合与自身之间的关系。而在普通数学英语中,
「relation」既可指这个特定概念,也可指更一般的关系。
后者指任意多个(可能不同的)集合之间的关系。讨论时的语境
应当让人清楚指的是哪一种。
nat 上的一个关系示例是 le,即小于或等于关系,
我们通常写作 n1 ≤ n2。
Print le.
(* ====> Inductive le (n : nat) : nat -> Prop :=
le_n : n <= n
| le_S : forall m : nat, n <= m -> n <= S m *)
Check le : nat → nat → Prop.
Check le : relation nat.
(* ====> Inductive le (n : nat) : nat -> Prop :=
le_n : n <= n
| le_S : forall m : nat, n <= m -> n <= S m *)
Check le : nat → nat → Prop.
Check le : relation nat.
(为什么我们这样写,而非以 Inductive le : relation nat...
开头呢?因为我们想把第一个 nat 放在 : 的左侧,这能让 Rocq
为关于 ≤ 的推理生成一条更漂亮的归纳法则。)
基本性质
偏函数
例如,next_nat 关系就是一个偏函数。
Inductive next_nat : nat → nat → Prop :=
| nn n : next_nat n (S n).
Check next_nat : relation nat.
Theorem next_nat_partial_function :
partial_function next_nat.
| nn n : next_nat n (S n).
Check next_nat : relation nat.
Theorem next_nat_partial_function :
partial_function next_nat.
然而,数上的 ≤ 关系并不是偏函数。(假设 ≤ 是偏函数。
由 0 ≤ 0 与 0 ≤ 1 可推出 0 = 1,矛盾。)
练习: 2 stars,标准,可选 (total_relation_not_partial_function)
请表明 IndProp(的练习)中定义的 total_relation 不是偏函数。
Inductive total_relation : nat → nat → Prop :=
(* 在这里填写 *)
.
Theorem total_relation_not_partial_function :
¬ (partial_function total_relation).
Proof.
(* 在这里填写 *) Admitted.
☐
(* 在这里填写 *)
.
Theorem total_relation_not_partial_function :
¬ (partial_function total_relation).
Proof.
(* 在这里填写 *) Admitted.
☐
练习: 2 stars,标准,可选 (empty_relation_partial_function)
请表明 IndProp(的练习)中定义的 empty_relation 是偏函数。
Inductive empty_relation : nat → nat → Prop :=
(* 在这里填写 *)
.
Theorem empty_relation_partial_function :
partial_function empty_relation.
Proof.
(* 在这里填写 *) Admitted.
☐
(* 在这里填写 *)
.
Theorem empty_relation_partial_function :
partial_function empty_relation.
Proof.
(* 在这里填写 *) Admitted.
☐
Definition reflexive {X: Type} (R: relation X) :=
∀ a : X, R a a.
Theorem le_reflexive :
reflexive le.
∀ a : X, R a a.
Theorem le_reflexive :
reflexive le.
Definition transitive {X: Type} (R: relation X) :=
∀ a b c : X, (R a b) → (R b c) → (R a c).
Theorem le_trans :
transitive le.
Theorem lt_trans:
transitive lt.
∀ a b c : X, (R a b) → (R b c) → (R a c).
Theorem le_trans :
transitive le.
Proof.
intros n m o Hnm Hmo.
induction Hmo.
- (* le_n *) apply Hnm.
- (* le_S *) apply le_S. apply IHHmo. Qed.
intros n m o Hnm Hmo.
induction Hmo.
- (* le_n *) apply Hnm.
- (* le_S *) apply le_S. apply IHHmo. Qed.
Theorem lt_trans:
transitive lt.
Theorem lt_trans' :
transitive lt.
Proof.
(* 通过对 m 小于 o 的证据做归纳来证明它。 *)
unfold lt. unfold transitive.
intros n m o Hnm Hmo.
induction Hmo as [| m' Hm'o].
(* 在这里填写 *) Admitted.
☐
transitive lt.
Proof.
(* 通过对 m 小于 o 的证据做归纳来证明它。 *)
unfold lt. unfold transitive.
intros n m o Hnm Hmo.
induction Hmo as [| m' Hm'o].
(* 在这里填写 *) Admitted.
☐
Theorem lt_trans'' :
transitive lt.
transitive lt.
Proof.
unfold lt. unfold transitive.
intros n m o Hnm Hmo.
induction o as [| o'].
(* 在这里填写 *) Admitted.
unfold lt. unfold transitive.
intros n m o Hnm Hmo.
induction o as [| o'].
(* 在这里填写 *) Admitted.
☐
反过来,le 的传递性也可用于证明一些以后会有用的事实
(例如下面反对称性的证明)……
练习: 2 stars,标准,可选 (le_Sn_n_inf)
请给出下列定理的非形式化证明:
(* 在这里填写 *)
☐
☐
对称关系与反对称关系
预序几乎就像偏序,但不必是对称的。
Definition preorder {X:Type} (R: relation X) :=
(reflexive R) ∧ (transitive R).
Theorem le_order :
order le.
(reflexive R) ∧ (transitive R).
Theorem le_order :
order le.
Proof.
unfold order. split.
- (* refl *) apply le_reflexive.
- split.
+ (* antisym *) apply le_antisymmetric.
+ (* transitive. *) apply le_trans. Qed.
unfold order. split.
- (* refl *) apply le_reflexive.
- split.
+ (* antisym *) apply le_antisymmetric.
+ (* transitive. *) apply le_trans. Qed.
自反传递闭包
Inductive clos_refl_trans {A: Type} (R: relation A) : relation A :=
| rt_step x y (H : R x y) : clos_refl_trans R x y
| rt_refl x : clos_refl_trans R x x
| rt_trans x y z
(Hxy : clos_refl_trans R x y)
(Hyz : clos_refl_trans R y z) :
clos_refl_trans R x z.
| rt_step x y (H : R x y) : clos_refl_trans R x y
| rt_refl x : clos_refl_trans R x x
| rt_trans x y z
(Hxy : clos_refl_trans R x y)
(Hyz : clos_refl_trans R y z) :
clos_refl_trans R x z.
例如,next_nat 关系的自反传递闭包与 le 关系重合。
Theorem next_nat_closure_is_le : ∀ n m,
(n ≤ m) ↔ ((clos_refl_trans next_nat) n m).
(n ≤ m) ↔ ((clos_refl_trans next_nat) n m).
Proof.
intros n m. split.
- (* -> *)
intro H. induction H.
+ (* le_n *) apply rt_refl.
+ (* le_S *)
apply rt_trans with m. apply IHle. apply rt_step.
apply nn.
- (* <- *)
intro H. induction H.
+ (* rt_step *) inversion H. apply le_S. apply le_n.
+ (* rt_refl *) apply le_n.
+ (* rt_trans *)
apply le_trans with y.
apply IHclos_refl_trans1.
apply IHclos_refl_trans2. Qed.
intros n m. split.
- (* -> *)
intro H. induction H.
+ (* le_n *) apply rt_refl.
+ (* le_S *)
apply rt_trans with m. apply IHle. apply rt_step.
apply nn.
- (* <- *)
intro H. induction H.
+ (* rt_step *) inversion H. apply le_S. apply le_n.
+ (* rt_refl *) apply le_n.
+ (* rt_trans *)
apply le_trans with y.
apply IHclos_refl_trans1.
apply IHclos_refl_trans2. Qed.
上面的自反传递闭包定义很自然。它明确地说,R 的自反传递
闭包是包含 R 且对自反与传递规则封闭的最小关系。
但这个定义做起证明来并不方便。rt_trans 规则的
「非确定性」有时会导致棘手的归纳。下面这个定义更有用:
Inductive clos_refl_trans_1n {A : Type}
(R : relation A) (x : A)
: A → Prop :=
| rt1n_refl : clos_refl_trans_1n R x x
| rt1n_trans (y z : A)
(Hxy : R x y) (Hrest : clos_refl_trans_1n R y z) :
clos_refl_trans_1n R x z.
(R : relation A) (x : A)
: A → Prop :=
| rt1n_refl : clos_refl_trans_1n R x x
| rt1n_trans (y z : A)
(Hxy : R x y) (Hrest : clos_refl_trans_1n R y z) :
clos_refl_trans_1n R x z.
我们的新自反传递闭包定义把 rt_step 与 rt_trans 规则
「打包」进单条 step 规则。这条规则的左端前提只用了一次
R,于是归纳法则简单得多。
在继续之前,我们应当检查这两个定义确实定义了同一个关系……
首先,我们证明两条引理,表明 clos_refl_trans_1n 模仿了
clos_refl_trans 中那两个「缺失」构造子的行为。
Lemma rsc_trans :
∀ (X:Type) (R: relation X) (x y z : X),
clos_refl_trans_1n R x y →
clos_refl_trans_1n R y z →
clos_refl_trans_1n R x z.
Proof.
(* 在这里填写 *) Admitted.
☐
∀ (X:Type) (R: relation X) (x y z : X),
clos_refl_trans_1n R x y →
clos_refl_trans_1n R y z →
clos_refl_trans_1n R x z.
Proof.
(* 在这里填写 *) Admitted.
☐
Theorem rtc_rsc_coincide :
∀ (X:Type) (R: relation X) (x y : X),
clos_refl_trans R x y ↔ clos_refl_trans_1n R x y.
Proof.
(* 在这里填写 *) Admitted.
☐
∀ (X:Type) (R: relation X) (x y : X),
clos_refl_trans R x y ↔ clos_refl_trans_1n R x y.
Proof.
(* 在这里填写 *) Admitted.
☐
(* 2026-11-26 15:54 *)
