Rel关系的性质

本章(较短且可选)介绍 Rocq 中二元关系的一些基本定义与定理。 关键定义会在用到之处再次出现,即 Programming Language Foundations 的 Smallstep 一章。已经熟悉这些内容的读者可以略过本章。 不过,关系也是练习 Rocq 基本推理的好题材。读完 IndProp 后再看 本章,或许会有收获。
Set Warnings "-notation-overridden".
From LF Require Export IndProp.

关系

集合 X 上的二元【关系|Relation】是一族由 X 的两个元素参数化的 命题,也就是关于 X 中元素对的命题。
Definition relation (X: Type) := X → X → Prop.
有点令人困惑的是,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 : relation nat... 开头呢?因为我们想把第一个 nat 放在 : 的左侧,这能让 Rocq 为关于 ≤ 的推理生成一条更漂亮的归纳法则。)

基本性质

上过本科离散数学课的人都知道,关于关系可说的很多。比如 关系的分类方式(如自反、传递等);对某类关系普遍成立的 定理;从一种关系构造另一种关系的方式;等等。 例如……

偏函数

若对每个 x,至多存在一个 y 使得 R x y 成立,则集合 X 上的关系 R 是一个【偏函数|Partial Function】:R x y1 与 R x y2 一起蕴含 y1 = y2。
Definition partial_function {X: Type} (R: relation X) :=
  ∀ x y1 y2 : X, R x y1 → R x y2 → y1 = y2.
例如,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.
Proof.
  unfold partial_function.
  intros x y1 y2 H1 H2.
  inversion H1. inversion H2.
  reflexivity. Qed.
然而,数上的 ≤ 关系并不是偏函数。(假设 ≤ 是偏函数。 由 0 ≤ 0 与 0 ≤ 1 可推出 0 = 1,矛盾。)
Theorem le_not_a_partial_function :
  ¬ (partial_function le).
Proof.
  unfold not. unfold partial_function. intros Hc.
  assert (0 = 1) as Nonsense. {
    apply Hc with (x := 0).
    - apply le_n.
    - apply le_S. apply le_n. }
  discriminate Nonsense. Qed.

练习: 2 stars,标准,可选 (total_relation_not_partial_function)

请表明 IndProp(的练习)中定义的 total_relation 不是偏函数。
请把 total_relation 的定义从你的 IndProp 复制到这里, 以便本文件可以单独评分。
Inductive total_relation : nat → nat → Prop :=
  (* 在这里填写 *)
.

Theorem total_relation_not_partial_function :
  ¬ (partial_function total_relation).
Proof.
  (* 在这里填写 *) Admitted.
☐

练习: 2 stars,标准,可选 (empty_relation_partial_function)

请表明 IndProp(的练习)中定义的 empty_relation 是偏函数。
请把 empty_relation 的定义从你的 IndProp 复制到这里, 以便本文件可以单独评分。
Inductive empty_relation : nat → nat → Prop :=
  (* 在这里填写 *)
.

Theorem empty_relation_partial_function :
  partial_function empty_relation.
Proof.
  (* 在这里填写 *) Admitted.
☐

自反关系

集合 X 上的【自反关系|Reflexive Relation】是 X 的每个元素 都与自身相关的那种关系。
Definition reflexive {X: Type} (R: relation X) :=
  ∀ a : X, R a a.

Theorem le_reflexive :
  reflexive le.
Proof.
  unfold reflexive. intros n. apply le_n. Qed.

传递关系

若每当 R a b 与 R b c 成立时 R a c 也成立, 则关系 R 是【传递关系|Transitive Relation】。
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.
Proof.
  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.
Proof.
  unfold lt. unfold transitive.
  intros n m o Hnm Hmo.
  apply le_S in Hnm.
  apply le_trans with (a := (S n)) (b := (S m)) (c := o).
  apply Hnm.
  apply Hmo. Qed.

练习: 2 stars,标准,可选 (le_trans_hard_way)

我们也可以不用 le_trans,通过归纳更费力地证明 lt_trans。 请这样做。
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.
☐

练习: 2 stars,标准,可选 (lt_trans'')

再对 o 做归纳来证明同一件事。
Theorem lt_trans'' :
  transitive lt.
Proof.
  unfold lt. unfold transitive.
  intros n m o Hnm Hmo.
  induction o as [| o'].
  (* 在这里填写 *) Admitted.
☐
反过来,le 的传递性也可用于证明一些以后会有用的事实 (例如下面反对称性的证明)……
Theorem le_Sn_le : ∀ n m, S n ≤ m → n ≤ m.
Proof.
  intros n m H. apply le_trans with (S n).
  - apply le_S. apply le_n.
  - apply H.
Qed.

练习: 1 star,标准,可选 (le_S_n)

Theorem le_S_n : ∀ n m,
  (S n ≤ S m) → (n ≤ m).
Proof.
  (* 在这里填写 *) Admitted.
☐

练习: 2 stars,标准,可选 (le_Sn_n_inf)

请给出下列定理的非形式化证明:
定理:对每个 n,¬ (S n ≤ n)
它的形式化证明是下面的可选练习。请先不借助形式化证明, 尝试写出非形式化证明。
证明:
    (* 在这里填写 *)
☐

练习: 1 star,标准,可选 (le_Sn_n)

Theorem le_Sn_n : ∀ n,
  ¬ (S n ≤ n).
Proof.
  (* 在这里填写 *) Admitted.
☐
自反性与传递性是后面章节的主要概念。不过,为了再练练在 Rocq 中处理关系,我们再看几个其他常见概念……

对称关系与反对称关系

若 R a b 蕴含 R b a,则关系 R 是【对称关系|Symmetric Relation】。
Definition symmetric {X: Type} (R: relation X) :=
  ∀ a b : X, (R a b) → (R b a).

练习: 2 stars,标准,可选 (le_not_symmetric)

Theorem le_not_symmetric :
  ¬ (symmetric le).
Proof.
  (* 在这里填写 *) Admitted.
☐
若 R a b 与 R b a 一起蕴含 a = b,则关系 R 是 【反对称关系|Antisymmetric Relation】:R 中唯一的 「环」都是平凡的。
Definition antisymmetric {X: Type} (R: relation X) :=
  ∀ a b : X, (R a b) → (R b a) → a = b.

练习: 2 stars,标准,可选 (le_antisymmetric)

Theorem le_antisymmetric :
  antisymmetric le.
Proof.
  (* 在这里填写 *) Admitted.
☐

练习: 2 stars,标准,可选 (le_step)

Theorem le_step : ∀ n m p,
  n < m →
  m ≤ S p →
  n ≤ p.
Proof.
  (* 在这里填写 *) Admitted.
☐

等价关系

若一个关系是自反、对称且传递的,则它是【等价关系|Equivalence Relation】。

偏序关系与预序关系

若一个关系是自反、【反】对称且传递的,则它是【偏序关系|Partial Order】。 在 Rocq 标准库中,它就叫 order。
Definition order {X:Type} (R: relation X) :=
  (reflexive R) ∧ (antisymmetric R) ∧ (transitive R).
预序几乎就像偏序,但不必是对称的。
Definition preorder {X:Type} (R: relation X) :=
  (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.

自反传递闭包

关系 R 的【自反传递闭包|Reflexive Transitive Closure】是 包含 R 且既自反又传递的最小关系。形式化地,它在 Rocq 标准库的 Relations 模块中这样定义:
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.
例如,next_nat 关系的自反传递闭包与 le 关系重合。
Theorem next_nat_closure_is_le : ∀ 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.
上面的自反传递闭包定义很自然。它明确地说,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.
我们的新自反传递闭包定义把 rt_step 与 rt_trans 规则 「打包」进单条 step 规则。这条规则的左端前提只用了一次 R,于是归纳法则简单得多。
在继续之前,我们应当检查这两个定义确实定义了同一个关系……
首先,我们证明两条引理,表明 clos_refl_trans_1n 模仿了 clos_refl_trans 中那两个「缺失」构造子的行为。
Lemma rsc_R : ∀ (X:Type) (R:relation X) (x y : X),
  R x y → clos_refl_trans_1n R x y.
Proof.
  intros X R x y H.
  apply rt1n_trans with y. apply H. apply rt1n_refl. Qed.

练习: 2 stars,标准,可选 (rsc_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.
☐
然后我们用这些事实来证明:自反传递闭包的两个定义确实 定义了同一个关系。

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

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.
☐
(* 2026-11-26 15:54 *)