Auto更多自动化

Set Warnings "-notation-overridden,-notation-incompatible-prefix".
From Stdlib Require Import Arith List.
From Stdlib Require Import Lia.
From Stdlib Require Import Strings.String.
From LF Require Import Maps.
From LF Require Import Imp.
Set Default Goal Selector "!".
到目前为止,我们用的是 Rocq 策略设施中偏手动的部分。 本章学习 Rocq 更强大的若干自动化特性:经由 auto 策略的 【证明搜索|Proof Search】、经由 Ltac 假设匹配的自动 【正向推理|Forward Reasoning】,以及用 eapply 与 eauto 推迟【存在性变量|Existential Variable】的实例化。 把这些特性与 Ltac 的脚本设施一起用,能让某些证明短得惊人! 用得恰当时,它们还能让证明更可维护,也更能经受底层定义的改动。 对 auto 与 eauto 的更深入处理,见《编程语言基础》中的 UseAuto 一章。
(还有一大类自动化我们尚未多谈,即针对特定问题的内建 【判定过程|Decision Procedure】:lia 是一例,本章末尾 还会再介绍 congruence。)
我们从 auto 策略开始。引入示例是下面这个证明,它与 Imp 中的证明只差几处小改动。我们将分几个阶段简化它。
Theorem ceval_deterministic: ∀ c st st1 st2,
  st =[ c ]=> st1 →
  st =[ c ]=> st2 →
  st1 = st2.
Proof.
  intros c st st1 st2 E1 E2;
  generalize dependent st2;
  induction E1; intros st2 E2; inversion E2; subst.
  - (* E_Skip *) reflexivity.
  - (* E_Asgn *) reflexivity.
  - (* E_Seq *)
    rewrite (IHE1_1 st'0 H1) in ×.
    apply IHE1_2. assumption.
  (* E_IfTrue *)
  - (* b 求值为真 *)
    apply IHE1. assumption.
  - (* b 求值为假(矛盾) *)
    rewrite H in H5. discriminate.
  (* E_IfFalse *)
  - (* b 求值为真(矛盾) *)
    rewrite H in H5. discriminate.
  - (* b 求值为假 *)
    apply IHE1. assumption.
  (* E_WhileFalse *)
  - (* b 求值为假 *)
    reflexivity.
  - (* b 求值为真(矛盾) *)
    rewrite H in H2. discriminate.
  (* E_WhileTrue *)
  - (* b 求值为假(矛盾) *)
    rewrite H in H4. discriminate.
  - (* b 求值为真 *)
    rewrite (IHE1_1 st'0 H3) in ×.
    apply IHE1_2. assumption. Qed.

auto 策略

到目前为止,证明脚本大多按名字,逐个把相关假设或引理 应用于目标,逆向推进。
Example auto_example_1 : ∀ (P Q R: Prop),
  (P → Q) → (Q → R) → P → R.
Proof.
  intros P Q R HPQ HQR HP.
  apply HQR. apply HPQ. assumption.
Qed.
auto 策略试图把我们从这桩苦差中解放出来: 它搜索一串对目标的应用,最终证明该目标:
Example auto_example_1' : ∀ (P Q R: Prop),
  (P → Q) → (Q → R) → P → R.
Proof.
  auto.
Qed.
auto 策略能解决任何可由 intros 与 apply 的某种组合 解决的目标。
使用 auto 总是「安全」的:它永不失败,也永不改变 证明状态。要么它完全解决当前目标,要么它什么也不做。
下面是更大的示例,展示 auto 的威力:
Example auto_example_2 : ∀ P Q R S T U : Prop,
  (P → Q) →
  (P → R) →
  (T → R) →
  (S → T → U) →
  ((P → Q) → (P → S)) →
  T →
  P →
  U.
Proof. auto. Qed.
从直觉上看,auto 策略可以理解为递归执行下列逆向搜索步骤:
  • 若 assumption 能关闭目标,就完成了。
  • 若目标是蕴含(→)或全称量词(∀), 调用 intro(intros 的一元版本),对新目标继续。
  • 否则,应用结论与目标统一的假设,再对新的子目标递归。 若某个子目标关不掉,就回溯,改试其他与目标统一的假设。
若没有候选路径能给出完整证明,auto 什么都不做, 目标保持不变。
若 auto 没有像预期那样解决目标,可以用 debug auto 看追踪。事实上,即便 auto 【成功】,debug auto 也会打印 整个搜索追踪,所以也能用它观察 auto 如何回溯:
Example auto_algorithm : ∀ (P Q R: Prop),
  (Q → R) → (* HQR : requires Q -- dead end *)
  ((P → P) → R) → (* HPPR : requires P → P -- proved by intro
                                                    and assumption *)

  R.
Proof. intros P Q R HQR HPPR. debug auto. Qed.
原则上,证明搜索可能耗时任意久,因此 auto 的搜索深度 有上限(默认为 5)。
Example auto_example_3 : ∀ (P Q R S T U: Prop),
  (P → Q) →
  (Q → R) →
  (R → S) →
  (S → T) →
  (T → U) →
  P →
  U.
Proof.
  intros P Q R S T U HPQ HQR HRS HST HTU HP.
  (* 即便目标看起来可以解决,auto 也什么都不做 *)
  auto.

  (* 添加 debug 关键字,看看在默认的搜索深度 5 下 auto 做了什么 *)
  debug auto.

  (* 它做了这些: *)
  (* apply HTU. *)
  (* apply HST. *)
  (* apply HRS. *)
  (* apply HQR. *)
  (* apply HPQ. *)
  (* 但之后它却无法证明 P,
     即便它拥有 P 作为假设 *)


  (* 通过添加一个可选的参数,我们可以修改 auto 的最大搜索深度,
      这里我们用深度 6 来搞定它:*)

  auto 6.
Qed.
在搜索当前目标的潜在证明时,auto 会同时考虑当前 语境中的假设,以及一个包含其他引理与构造子的【提示数据库】。 关于相等与逻辑运算符的一些常见引理默认已装入此数据库。
Example auto_example_4 : ∀ P Q R : Prop,
  Q →
  (Q → R) →
  P ∨ (Q ∧ R).
Proof. auto. Qed.
若想看 auto 用了哪些事实,可改用 info_auto。 与会打印失败路径的 debug auto 不同,info_auto 只显示成功的验证路径。
Example auto_example_4' : ∀ P Q R : Prop,
  Q →
  (Q → R) →
  P ∨ (Q ∧ R).
Proof.
  info_auto.
Qed.
auto 使用默认提示数据库中的以下事实:
Check or_intror : ∀ A B : Prop, B → A ∨ B.
Check conj : ∀ A B : Prop, A → B → A ∧ B.

Example auto_example_5: 2 = 2.
Proof.
  info_auto.
Qed.
auto 包含了自反性,因为 eq_refl 是默认提示数据库中的一个事实:
Check @eq_refl : ∀ A (x:A), x = x.
我们可以只在某一次 auto 调用期间扩展提示数据库, 写法是「auto using ...」。
Lemma le_antisym : ∀ n m: nat, (n ≤ m ∧ m ≤ n) → n = m.
Proof. lia. Qed.

Example auto_example_6 : ∀ n m p q : nat,
  (p = q → (n ≤ m ∧ m ≤ n)) →
  p = q →
  n = m.
Proof.
  auto using le_antisym.
Qed.
当然,在任一开发中,很可能有些构造子与引理 在证明里用得很多。我们可在顶层写
      Hint Resolve T : core. 把它们加入全局提示数据库,其中 T 是顶层定理,或归纳 定义命题的构造子(即任何类型为蕴含的东西)。 作为简写,可写
      Hint Constructors c : core. 告诉 Rocq 对 c 归纳定义的【所有】构造子都做 Hint Resolve。
有时还需要加入
      Hint Unfold d : core. 其中 d 是已定义符号,好让 auto 知道何时展开 d, 从而多一些应用已知引理的机会。
也可以定义专用提示数据库(除 core 外),只在需要时激活; 确实,自建提示数据库而非污染 core 是好风格。
细节见 Rocq 参考手册。
Hint Resolve le_antisym : core.

Example auto_example_6' : ∀ n m p q : nat,
  (p = q → (n ≤ m ∧ m ≤ n)) →
  p = q →
  n = m.
Proof.
  auto. (* 从数据库取用提示 *)
Qed.

Definition is_fortytwo x := (x = 42).

Example auto_example_7: ∀ x,
  (x ≤ 42 ∧ 42 ≤ x) → is_fortytwo x.
Proof.
  auto. (* 什么也不做 *)
Abort.

Hint Unfold is_fortytwo : core.

Example auto_example_7' : ∀ x,
  (x ≤ 42 ∧ 42 ≤ x) → is_fortytwo x.
Proof.
  auto. (* 也可试:info_auto。 *)
Qed.
注意,示例上方的 Hint Unfold is_fortytwo 命令 是必需的。普通的 apply 会自动展开,而 auto 执行的 simple apply 步骤不做任何展开。
我们先用 auto 对 ceval_deterministic 做第一轮简化。
Theorem ceval_deterministic': ∀ c st st1 st2,
  st =[ c ]=> st1 →
  st =[ c ]=> st2 →
  st1 = st2.
Proof.
  intros c st st1 st2 E1 E2.
  generalize dependent st2.
  induction E1; intros st2 E2;
    inversion E2; subst; (* <- ends with ; *)
    auto. (* <---- 这里是 auto 的一个好地方 *)
  - (* E_Seq *)
    rewrite (IHE1_1 st'0 H1) in ×.
    auto. (* <---- 还有一处 *)
  - (* E_IfTrue *)
    rewrite H in H5. discriminate.
  - (* E_IfFalse *)
    rewrite H in H5. discriminate.
  - (* E_WhileFalse *)
    rewrite H in H2. discriminate.
  - (* E_WhileTrue,b 为假 *)
    rewrite H in H4. discriminate.
  - (* E_WhileTrue,b 为真 *)
    rewrite (IHE1_1 st'0 H3) in ×.
    auto. (* <---- 以及另一处 *)
Qed.
若证明中反复用到某个策略,可用 Proof 命令的变体, 把该策略设为证明内的默认。写 Proof with t(t 为任意 策略)后,证明中的 t1... 就是 t1;t 的简写。 作为示范,下面是前一证明的另一版本,使用 Proof with auto。
Theorem ceval_deterministic'_alt: ∀ c st st1 st2,
  st =[ c ]=> st1 →
  st =[ c ]=> st2 →
  st1 = st2.
Proof with auto.
  intros c st st1 st2 E1 E2.
  generalize dependent st2.
  induction E1; intros st2 E2;
    inversion E2; subst...
  - (* E_Seq *)
    rewrite (IHE1_1 st'0 H1) in ×...
  - (* E_IfTrue *)
    rewrite H in H5. discriminate.
  - (* E_IfFalse *)
    rewrite H in H5. discriminate.
  - (* E_WhileFalse *)
    rewrite H in H2. discriminate.
  - (* E_WhileTrue,b 为假 *)
    rewrite H in H4. discriminate.
  - (* E_WhileTrue,b 为真 *)
    rewrite (IHE1_1 st'0 H3) in ×...
Qed.

练习

From LF Require IndProp.

Module Exercises.

Import IndProp. Import Pumping.

练习: 1 star,标准 (pumping_constant_ge_1_redux)

用 auto、lia 和 ; 把这个引理重新证明在「一行」里。 IndProp 中的「官方」证明大约要花十几行。
Lemma pumping_constant_ge_1 :
  ∀ T (re : reg_exp T),
    pumping_constant re ≥ 1.
(* 在这里填写 *) Admitted.
(* 不要修改下面这行: *)
Definition manual_grade_for_pumping_constant_ge_1_redux : option (nat×string) := None.
☐

正则表达式优化练习

练习: 3 stars,标准 (re_opt_match_auto)

作为自动化好处的一个简单示例,考虑IndProp中 形式化的正则表达式上的另一个问题。同一个字符串集合可由 许多不同的正则表达式表示。例如 App EmptyString re 恰好匹配与 re 相同的字符串。我们可以写一个函数, 把这个事实贯穿到整个正则表达式中,从而把任意正则表达式 「优化」成可能更简单的形式。(为简单起见,该函数不优化 作为其他优化结果出现的表达式。)
Fixpoint re_opt {T:Type} (re: reg_exp T) : reg_exp T :=
  match re with
  | App _ EmptySet ⇒ EmptySet
  | App EmptyStr re2 ⇒ re_opt re2
  | App re1 EmptyStr ⇒ re_opt re1
  | App re1 re2 ⇒ App (re_opt re1) (re_opt re2)
  | Union EmptySet re2 ⇒ re_opt re2
  | Union re1 EmptySet ⇒ re_opt re1
  | Union re1 re2 ⇒ Union (re_opt re1) (re_opt re2)
  | Star EmptySet ⇒ EmptyStr
  | Star EmptyStr ⇒ EmptyStr
  | Star re ⇒ Star (re_opt re)
  | EmptySet ⇒ EmptySet
  | EmptyStr ⇒ EmptyStr
  | Char x ⇒ Char x
  end.
我们要证明正则表达式与其「优化」形式等价。 下面是正确性(一个方向)的手工证明,极其繁琐:
Lemma re_opt_match : ∀ T (re: reg_exp T) s,
  s =~ re → s =~ re_opt re.
Proof.
  intros T re s M.
  induction M
    as [| x'
       | s1 re1 s2 re2 Hmatch1 IH1 Hmatch2 IH2
       | s1 re1 re2 Hmatch IH | s2 re1 re2 Hmatch IH
       | re | s1 s2 re Hmatch1 IH1 Hmatch2 IH2].
  - (* MEmpty *) simpl. apply MEmpty.
  - (* MChar *) simpl. apply MChar.
  - (* MApp *) simpl.
    destruct re1.
    + inversion IH1.
    + inversion IH1. simpl. destruct re2.
      × apply IH2.
      × apply IH2.
      × apply IH2.
      × apply IH2.
      × apply IH2.
      × apply IH2.
    + destruct re2.
      × inversion IH2.
      × inversion IH2. rewrite app_nil_r. apply IH1.
      × apply MApp.
        -- apply IH1.
        -- apply IH2.
      × apply MApp.
        -- apply IH1.
        -- apply IH2.
      × apply MApp.
        -- apply IH1.
        -- apply IH2.
      × apply MApp.
        -- apply IH1.
        -- apply IH2.
    + destruct re2.
      × inversion IH2.
      × inversion IH2. rewrite app_nil_r. apply IH1.
      × apply MApp.
        -- apply IH1.
        -- apply IH2.
      × apply MApp.
        -- apply IH1.
        -- apply IH2.
      × apply MApp.
        -- apply IH1.
        -- apply IH2.
      × apply MApp.
        -- apply IH1.
        -- apply IH2.
    + destruct re2.
      × inversion IH2.
      × inversion IH2. rewrite app_nil_r. apply IH1.
      × apply MApp.
        -- apply IH1.
        -- apply IH2.
      × apply MApp.
        -- apply IH1.
        -- apply IH2.
      × apply MApp.
        -- apply IH1.
        -- apply IH2.
      × apply MApp.
        -- apply IH1.
        -- apply IH2.
    + destruct re2.
      × inversion IH2.
      × inversion IH2. rewrite app_nil_r. apply IH1.
      × apply MApp.
        -- apply IH1.
        -- apply IH2.
      × apply MApp.
        -- apply IH1.
        -- apply IH2.
      × apply MApp.
        -- apply IH1.
        -- apply IH2.
      × apply MApp.
        -- apply IH1.
        -- apply IH2.
  - (* MUnionL *) simpl.
    destruct re1.
    + inversion IH.
    + destruct re2.
      × apply IH.
      × apply MUnionL. apply IH.
      × apply MUnionL. apply IH.
      × apply MUnionL. apply IH.
      × apply MUnionL. apply IH.
      × apply MUnionL. apply IH.
    + destruct re2.
      × apply IH.
      × apply MUnionL. apply IH.
      × apply MUnionL. apply IH.
      × apply MUnionL. apply IH.
      × apply MUnionL. apply IH.
      × apply MUnionL. apply IH.
    + destruct re2.
      × apply IH.
      × apply MUnionL. apply IH.
      × apply MUnionL. apply IH.
      × apply MUnionL. apply IH.
      × apply MUnionL. apply IH.
      × apply MUnionL. apply IH.
    + destruct re2.
      × apply IH.
      × apply MUnionL. apply IH.
      × apply MUnionL. apply IH.
      × apply MUnionL. apply IH.
      × apply MUnionL. apply IH.
      × apply MUnionL. apply IH.
    + destruct re2.
      × apply IH.
      × apply MUnionL. apply IH.
      × apply MUnionL. apply IH.
      × apply MUnionL. apply IH.
      × apply MUnionL. apply IH.
      × apply MUnionL. apply IH.
  - (* MUnionR *) simpl.
    destruct re1.
    + apply IH.
    + destruct re2.
      × inversion IH.
      × apply MUnionR. apply IH.
      × apply MUnionR. apply IH.
      × apply MUnionR. apply IH.
      × apply MUnionR. apply IH.
      × apply MUnionR. apply IH.
    + destruct re2.
      × inversion IH.
      × apply MUnionR. apply IH.
      × apply MUnionR. apply IH.
      × apply MUnionR. apply IH.
      × apply MUnionR. apply IH.
      × apply MUnionR. apply IH.
    + destruct re2.
      × inversion IH.
      × apply MUnionR. apply IH.
      × apply MUnionR. apply IH.
      × apply MUnionR. apply IH.
      × apply MUnionR. apply IH.
      × apply MUnionR. apply IH.
    + destruct re2.
      × inversion IH.
      × apply MUnionR. apply IH.
      × apply MUnionR. apply IH.
      × apply MUnionR. apply IH.
      × apply MUnionR. apply IH.
      × apply MUnionR. apply IH.
    + destruct re2.
      × inversion IH.
      × apply MUnionR. apply IH.
      × apply MUnionR. apply IH.
      × apply MUnionR. apply IH.
      × apply MUnionR. apply IH.
      × apply MUnionR. apply IH.
 - (* MStar0 *) simpl.
    destruct re.
    + apply MEmpty.
    + apply MEmpty.
    + apply MStar0.
    + apply MStar0.
    + apply MStar0.
    + simpl.
      destruct re.
      × apply MStar0.
      × apply MStar0.
      × apply MStar0.
      × apply MStar0.
      × apply MStar0.
      × apply MStar0.
 - (* MStarApp *) simpl.
   destruct re.
   + inversion IH1.
   + inversion IH1. inversion IH2. apply MEmpty.
   + apply star_app.
     × apply MStar1. apply IH1.
     × apply IH2.
   + apply star_app.
     × apply MStar1. apply IH1.
     × apply IH2.
   + apply star_app.
     × apply MStar1. apply IH1.
     × apply IH2.
   + apply star_app.
     × apply MStar1. apply IH1.
     × apply IH2.
Qed.
用 auto 缩短该证明。上面的证明约 200 行。 把它减到 50 行或更少,行密度与原证明相当。 去掉所有 apply,不必再点名正则表达式特有的构造子与引理。
提示:用自底向上方法。先把下面整个证明复制下来。 先自动化最内层子弹点,再向外推进。 不时检查整个证明是否仍能编译。若不能,撤销最近的改动, 直到回到能编译的证明。
Lemma re_opt_match'' : ∀ T (re: reg_exp T) s,
  s =~ re → s =~ re_opt re.
Proof.
(* 在这里填写 *) Admitted.
(* 不要修改下面这行: *)
Definition manual_grade_for_re_opt_match'' : option (nat×string) := None.
☐

抽泵练习(可选)

练习: 3 stars,高级,可选 (pumping_redux_strong)

用 auto、lia、repeat、;、try 等缩短 IndProp 中 更强抽泵引理可选练习的「官方」证明。
Lemma pumping : ∀ T (re : reg_exp T) s,
    s =~ re →
    pumping_constant re ≤ length s →
    ∃ s1 s2 s3,
      s = s1 ++ s2 ++ s3 ∧
        s2 ≠ [] ∧
        length s1 + length s2 ≤ pumping_constant re ∧
        ∀ m, s1 ++ napp m s2 ++ s3 =~ re.
Proof.
  (* 在这里填写 *) Admitted.
(* 不要修改下面这行: *)
Definition manual_grade_for_pumping_redux_strong : option (nat×string) := None.
☐
End Exercises.

搜索假设

证明变简单了,但仍有恼人的重复。先处理矛盾情形。 每种情形都出现在同时有
      H1: beval st b = false 与
      H2: beval st b = true 作为假设的时候。矛盾一目了然,但展示出来有点麻烦: 要定位假设 H1 与 H2,先 rewrite 再 discriminate。 我们想把这个过程自动化。
(Rocq 其实有内建策略 congruence 能处理这种情形。 我们暂且不用它,先示范如何手工构造正向搜索策略。)
第一步:在 Ltac 中写个小函数,把相关脚本片段抽象出来。
Ltac rwd H1 H2 := rewrite H1 in H2; discriminate.

Theorem ceval_deterministic'': ∀ c st st1 st2,
  st =[ c ]=> st1 →
  st =[ c ]=> st2 →
  st1 = st2.
Proof.
  intros c st st1 st2 E1 E2.
  generalize dependent st2;
  induction E1; intros st2 E2; inversion E2; subst; auto.
  - (* E_Seq *)
    rewrite (IHE1_1 st'0 H1) in ×.
    auto.
  - (* E_IfTrue *)
    rwd H H5. (* <----- *)
  - (* E_IfFalse *)
    rwd H H5. (* <----- *)
  - (* E_WhileFalse *)
    rwd H H2. (* <----- *)
  - (* E_WhileTrue - b 为假 *)
    rwd H H4. (* <----- *)
  - (* EWhileTrue - b 为真 *)
    rewrite (IHE1_1 st'0 H3) in ×.
    auto. Qed.
这样好些了,但我们更希望 Rocq 替我们发现相关假设。 可用 Ltac 的 match goal 设施做到。
Ltac find_rwd :=
  match goal with
    H1: ?E = true, H2: ?E = false ⊢ _
       ⇒
    rwd H1 H2
  end.
这个 match goal 寻找两个不同的等式假设: 左侧是同一个任意表达式 E,右侧是冲突的布尔值。 若找到这样的假设,就把 H1 与 H2 绑定到它们的名字, 并对 H1 与 H2 应用 rwd 策略。
把此策略加入归纳各情形中所调用的策略,就能处理所有矛盾情形。
Theorem ceval_deterministic''': ∀ c st st1 st2,
  st =[ c ]=> st1 →
  st =[ c ]=> st2 →
  st1 = st2.
Proof.
  intros c st st1 st2 E1 E2.
  generalize dependent st2;
  induction E1; intros st2 E2; inversion E2; subst;
       try find_rwd; (* <------ *)
       auto.
  - (* E_Seq *)
    rewrite (IHE1_1 st'0 H1) in ×.
    auto.
  - (* E_WhileTrue - b 为真 *)
    rewrite (IHE1_1 st'0 H3) in ×.
    auto. Qed.
再看剩下的情形。每种情形都先给出所需条件, 再用它改写某个假设。我们可以自动化「找出该用哪些 相关假设来改写」。
Ltac find_eqn :=
  match goal with
    H1: ∀ x, ?P x → ?L = ?R,
    H2: ?P ?X
    ⊢ _
    ⇒ rewrite (H1 X H2) in ×
  end.
模式 ∀ x, ?P x → ?L = ?R 匹配任何形如 「对所有 x,【x 的某个性质】 蕴含 【某个等式】」的假设。 x 的性质绑定到模式变量 P,等式左右分别绑定到 L 与 R。 该假设的名字绑定到 H1。接着模式 ?P ?X 匹配任何提供 「P 对某个具体 X 成立」之证据的假设。若两个模式都 匹配成功,就在所有假设与目标上应用 rewrite (用 X 实例化被量化的 x,并提供 H2 作为 P X 的证据)。
Theorem ceval_deterministic'''': ∀ c st st1 st2,
  st =[ c ]=> st1 →
  st =[ c ]=> st2 →
  st1 = st2.
Proof.
  intros c st st1 st2 E1 E2.
  generalize dependent st2;
  induction E1; intros st2 E2; inversion E2; subst;
    try find_rwd;
    try find_eqn; (* <------- *)
    auto.
Qed.
此方法的最大回报是:新证明脚本更能经受语言改动。 为测试这一点,我们给语言加上 REPEAT 命令。
Module Repeat.

Inductive com : Type :=
  | CSkip
  | CAsgn (x : string) (a : aexp)
  | CSeq (c1 c2 : com)
  | CIf (b : bexp) (c1 c2 : com)
  | CWhile (b : bexp) (c : com)
  | CRepeat (c : com) (b : bexp).
REPEAT 的行为类似 while,只是循环条件在每次执行循环体 【之后】才检查,且只要条件保持为【假】就继续循环。 因此,循环体至少会执行一次。
Notation "'repeat' x 'until' y 'end'" :=
         (CRepeat x y)
            (in custom com at level 0,
             x at level 99, y at level 99).
Notation "'skip'" :=
         CSkip (in custom com at level 0).
Notation "x := y" :=
         (CAsgn x y)
            (in custom com at level 0, x constr at level 0,
             y at level 85, no associativity).
Notation "x ; y" :=
         (CSeq x y)
           (in custom com at level 90, right associativity).
Notation "'if' x 'then' y 'else' z 'end'" :=
         (CIf x y z)
           (in custom com at level 89, x at level 99,
            y at level 99, z at level 99).
Notation "'while' x 'do' y 'end'" :=
         (CWhile x y)
            (in custom com at level 89, x at level 99, y at level 99).

Reserved Notation "st '=[' c ']=>' st'"
         (at level 40, c custom com at level 99, st' constr at next level).

Inductive ceval : com → state → state → Prop :=
  | E_Skip : ∀ st,
      st =[ skip ]=> st
  | E_Asgn : ∀ st a1 n x,
      aeval st a1 = n →
      st =[ x := a1 ]=> (x !-> n ; st)
  | E_Seq : ∀ c1 c2 st st' st'',
      st =[ c1 ]=> st' →
      st' =[ c2 ]=> st'' →
      st =[ c1 ; c2 ]=> st''
  | E_IfTrue : ∀ st st' b c1 c2,
      beval st b = true →
      st =[ c1 ]=> st' →
      st =[ if b then c1 else c2 end ]=> st'
  | E_IfFalse : ∀ st st' b c1 c2,
      beval st b = false →
      st =[ c2 ]=> st' →
      st =[ if b then c1 else c2 end ]=> st'
  | E_WhileFalse : ∀ b st c,
      beval st b = false →
      st =[ while b do c end ]=> st
  | E_WhileTrue : ∀ st st' st'' b c,
      beval st b = true →
      st =[ c ]=> st' →
      st' =[ while b do c end ]=> st'' →
      st =[ while b do c end ]=> st''
  | E_RepeatEnd : ∀ st st' b c,
      st =[ c ]=> st' →
      beval st' b = true →
      st =[ repeat c until b end ]=> st'
  | E_RepeatLoop : ∀ st st' st'' b c,
      st =[ c ]=> st' →
      beval st' b = false →
      st' =[ repeat c until b end ]=> st'' →
      st =[ repeat c until b end ]=> st''

  where "st =[ c ]=> st'" := (ceval c st st').
我们对确定性证明的首次尝试不太成功: 此前的自动化没处理 E_RepeatEnd 与 E_RepeatLoop 情形。
Theorem ceval_deterministic: ∀ c st st1 st2,
  st =[ c ]=> st1 →
  st =[ c ]=> st2 →
  st1 = st2.
Proof.
  intros c st st1 st2 E1 E2.
  generalize dependent st2;
  induction E1;
    intros st2 E2; inversion E2; subst; try find_rwd; try find_eqn; auto.
  - (* E_RepeatEnd *)
    + (* b 求值为假(矛盾) *)
       find_rwd.
       (* 哎呀:find_rwd 为什么没替我们解决?
          因为我们把顺序弄错了。 *)

  - (* E_RepeatLoop *)
     + (* b 求值为真(矛盾) *)
        find_rwd.
Qed.
幸运的是,要修复它,只需交换 find_eqn 与 find_rwd 的调用顺序。
Theorem ceval_deterministic': ∀ c st st1 st2,
  st =[ c ]=> st1 →
  st =[ c ]=> st2 →
  st1 = st2.
Proof.
  intros c st st1 st2 E1 E2.
  generalize dependent st2;
  induction E1;
    intros st2 E2; inversion E2; subst; try find_eqn; try find_rwd; auto.
Qed.

End Repeat.
这些示例只是让人领略 Rocq 中「超级自动化」能走多远。 match goal 的细节有点棘手(用它的脚本调试并不愉快)。 但至少把简单用法加进证明很值得:既避免繁琐,也让证明更能 经受未来的改动。

eapply 与 eauto 策略

作为本章收尾,再看 Rocq 的一项便利特性: 推迟量词的实例化。先回忆Imp中的这个示例:
Example ceval_example1:
  empty_st =[
    X := 2;
    if (X ≤ 1)
      then Y := 3
      else Z := 4
    end
  ]=> (Z !-> 4 ; X !-> 2).
Proof.
  (* 我们给出中间状态 st'…… *)
  apply E_Seq with (st' := X !-> 2).
  - apply E_Asgn. reflexivity.
  - apply E_IfFalse.
    + reflexivity.
    + apply E_Asgn. reflexivity.
Qed.
证明的第一步必须显式给出一个偏长的表达式, 帮 Rocq 为 E_Seq 实例化「隐藏」参数。原因是 E_Seq 的定义……
          E_Seq : ∀ c1 c2 st st' st'',
            st =[ c1 ]=> st' →
            st' =[ c2 ]=> st'' →
            st =[ c1 ; c2 ]=> st''
对不在结论中出现的变量 st' 做了量化。因此把结论与目标 统一,并不能帮 Rocq 找到该变量的取值。若省略 with, 此步会失败("Error: Unable to find an instance for the variable st'")。
这个错误有点蠢:st' 的合适取值在紧接的下一步 (应用 E_Asgn 时)就很明显。若 Rocq 能等到那一步, 我们就不必显式给出该值。eapply 正是允许这样做:
Example ceval'_example1:
  empty_st =[
    X := 2;
    if (X ≤ 1)
      then Y := 3
      else Z := 4
    end
  ]=> (Z !-> 4 ; X !-> 2).
Proof.
  (* 1 *) eapply E_Seq. (* 引入 ?st' 存在性变量 *)
  - (* 2 *) apply E_Asgn. (* 用 X !-> ?n 实例化 ?st';
                       用一个全新的存在性变量 ?n *)

    (* 3 *) simpl. reflexivity. (* 用 2 实例化 ?n *)
  - (* 4 *) apply E_IfFalse.
    + reflexivity.
    + apply E_Asgn. reflexivity.
Qed.
eapply H 与 apply H 行为相同,只有一点不同: 把目标与 H 的结论统一后,它跳过一步检查:统一过程中 引入的变量是否都已得到具体取值。
若单步走完上面的证明,位置 1 的目标在两个子目标中 都提到【存在性变量】?st'。下一步(位置 2)把 ?st' 换成具体值。新值含新的存在性变量 ?n,它又由随后的 reflexivity 步(位置 3)实例化。开始处理第二个 子目标(位置 4)时,其中的 ?st' 已换成第一个 子目标中得到的取值。
迄今见过的若干策略,包括 ∃、constructor 与 auto, 都有类似 eapply 的变体。
例如,除 ∃ 外,Rocq 还提供 eexists 策略。 为证明形如 ∃ x, P x 的目标,∃ t 策略要求我们 提供具体项 t,把目标改成 P t。而 eexists 策略则代之以 引入一个全新的存在性变量 ?x,留下目标 P ?x。 ?x 的值须在随后某个证明步骤中由统一确定。
Example eexists_example : ∃ s',
  (Y !-> 1 ; X !-> 2) =[
    if (X ≤ Y)
      then Z := Y - X
      else Y := X + Z
    end
  ]=> s'.
Proof.
  eexists. eapply E_IfFalse.
  - reflexivity.
  - eapply E_Asgn. reflexivity.
Qed.
类似地,econstructor 之于 constructor,犹如 eapply 之于 apply。在 ceval'_example1 的证明中,本可以写 econstructor 代替 eapply E_Seq:Rocq 会挑出匹配的 构造子(E_Seq),并把中间状态留作存在性变量,由后续 步骤确定。下面是用 econstructor 再写一遍该证明。
Example econstructor_example :
  empty_st =[
    X := 2;
    if (X ≤ 1)
      then Y := 3
      else Z := 4
    end
  ]=> (Z !-> 4 ; X !-> 2).
Proof.
  econstructor.
  - apply E_Asgn. reflexivity.
  - apply E_IfFalse.
    + reflexivity.
    + apply E_Asgn. reflexivity.
Qed.
eauto 策略与 auto 工作方式相同,只是它用 eapply 而非 apply。在示例中使用它之前,我们先给 eauto 一些提示, 让它在证明搜索中使用 ceval 的构造子以及 state 与 total_map 的定义。
Hint Constructors ceval : core.
(* HIDE CH: I think better would be to choose another example that
   doesn't need `Hint Transparent` *)

Hint Transparent state total_map : core.

Example eauto_example : ∃ s',
  (Y !-> 1 ; X !-> 2) =[
    if (X ≤ Y)
      then Z := Y - X
      else Y := X + Z
    end
  ]=> s'.
Proof. info_eauto. Qed.
info_eauto 向我们展示 eauto 用了哪些事实。
专业提示:有人可能以为,既然 eapply 与 eauto 比 apply 与 auto 更强,就该一直用它们。不幸的是,它们也 明显更慢,尤其是 eauto。Rocq 专家多数时候仍用 apply 与 auto,只在普通变体不够用时才切换到带 e 的变体。

对存在性变量的约束

为使 Qed 成功,所有存在性变量都须在证明结束时确定。 否则 Rocq 会(正确地)拒绝接受该证明。记住:Rocq 策略构造 证明对象,而含存在性变量的证明对象并不完整。
Lemma silly1 : ∀ (P : nat → nat → Prop) (Q : nat → Prop),
  (∀ x y : nat, P x y) →
  (∀ x y : nat, P x y → Q x) →
  Q 42.
Proof.
  intros P Q HP HQ. eapply HQ. apply HP. Unshelve. exact 0.
apply HP 之后 Rocq 给出警告:"All the remaining goals are on the shelf"。意思是:所有顶层证明义务都已完成,但沿途有些 目标被搁到一旁,尚未完成。试图用 Qed 结束证明会得到错误。 (试试看!)
Abort.
另一项约束:存在性变量不能用这样的项来实例化, 其中含有在该存在性变量创建时还不存在的普通变量。 (理由是:允许这种实例化会导致 Rocq 逻辑不一致。)
Lemma silly2 :
  ∀ (P : nat → nat → Prop) (Q : nat → Prop),
  (∃ y, P 42 y) →
  (∀ x y : nat, P x y → Q x) →
  Q 42.
Proof.
  intros P Q HP HQ. eapply HQ. destruct HP as [y HP'].
  Fail apply HP'.
我们得到的错误(略去细节)是:
      cannot instantiate "?y" because "y" is not in its scope 此情形有个简单修法:在做 eapply HQ 【之前】先做 destruct HP。
Abort.

Lemma silly2_fixed :
  ∀ (P : nat → nat → Prop) (Q : nat → Prop),
  (∃ y, P 42 y) →
  (∀ x y : nat, P x y → Q x) →
  Q 42.
Proof.
  intros P Q HP HQ. destruct HP as [y HP'].
  eapply HQ. apply HP'.
Qed.
最后一步的 apply HP' 把目标中的存在性变量与变量 y 统一。
注意:assumption 在此情形下无效,因为它不能处理存在性 变量。Rocq 还提供 eassumption:若某个前提在「实例化 存在性变量之后」与目标匹配,它就解决目标。若愿意, 可用它代替 apply HP'。
Lemma silly2_eassumption : ∀ (P : nat → nat → Prop) (Q : nat → Prop),
  (∃ y, P 42 y) →
  (∀ x y : nat, P x y → Q x) →
  Q 42.
Proof.
  intros P Q HP HQ. destruct HP as [y HP']. eapply HQ. eassumption.
Qed.
eauto 会使用 eapply 与 eassumption, 让证明更顺畅。
Lemma silly2_eauto : ∀ (P : nat → nat → Prop) (Q : nat → Prop),
  (∃ y, P 42 y) →
  (∀ x y : nat, P x y → Q x) →
  Q 42.
Proof.
  intros P Q HP HQ. destruct HP as [y HP']. eauto.
Qed.

相等求解器:congruence 策略

Rocq 有若干专用策略,能依据特定数学或逻辑领域的算法, 自动解决某类目标。其中一个策略 lia 已在 Imp 中介绍过, 这里再学一个:congruence。
lia 策略利用加法与乘法的事实来证明等式。 处理这类公式更基本的方式,是把其中出现的每个函数都当作黑盒: 对函数行为一无所知。仅凭相等本身的性质,仍可证明某些公式。 例如 y = f x → g y = g (f x),即使对 f 或 g 一无所知:
Theorem eq_example1 :
  ∀ (A B C : Type) (f : A → B) (g : B → C) (x : A) (y : B),
    y = f x → g y = g (f x).
Proof.
  intros. rewrite H. reflexivity.
Qed.
相等的本质性质是:
  • 自反
  • 对称
  • 传递
  • 【合同|Congruence】:它尊重函数与谓词应用。
上面证明中 rewrite 用的正是这一合同性质: 若 a = b 则 f a = f b。(ProofObjects 一章以 「莱布尼茨相等」之名进一步探讨此思想。)
congruence 策略是带未解释函数与其他符号的相等的判定过程。
Theorem eq_example1' :
  ∀ (A B C : Type) (f : A → B) (g : B → C) (x : A) (y : B),
    y = f x → g y = g (f x).
Proof.
  congruence.
Qed.
congruence 策略能处理构造子,甚至利用它们的单射性与互异性。
Theorem eq_example2 : ∀ (n m o p : nat),
    (n, o) = (m, p) →
    n = m.
Proof.
  congruence.
Qed.

Theorem eq_example3 : ∀ (X : Type) (h : X) (t : list X),
    nil ≠ h :: t.
Proof.
  congruence.
Qed.
这给了我们另一种简化确定性证明的方式。即使不用定制策略, 证明也能相当短:
Theorem ceval_deterministic_congruence: ∀ c st st1 st2,
  st =[ c ]=> st1 →
  st =[ c ]=> st2 →
  st1 = st2.
Proof.
  intros c st st1 st2 E1 E2.
  generalize dependent st2.
  induction E1; intros st2 E2;
    inversion E2; subst;
    auto;
    try congruence. (* <---- 这解决了 4 种情形,此前用
                             rewrite 与 discriminate 证明 *)

  - (* E_Seq *)
    rewrite (IHE1_1 st'0 H1) in ×.
    auto.
  - (* E_WhileTrue, with b true *)
    rewrite (IHE1_1 st'0 H3) in ×.
    auto.
Qed.

练习: 2 stars,标准 (automatic_solvers)

下列练习可用(相对)长的手工证明脚本证明, 但每个都只需调用一次我们学过的自动求解器 (congruence 或 lia)即可解决。
Theorem cons_equal : ∀ {X} (n m: X) (l: list X),
    n = m → n :: l = m :: l.
Proof. (* 在这里填写 *) Admitted.

Theorem plus_le_cancel_r : ∀ n m p : nat,
    n + p ≤ m + p → n ≤ m.
Proof. (* 在这里填写 *) Admitted.

Theorem no_half : ∀ n,
    2 × n ≠ 1.
Proof. (* 在这里填写 *) Admitted.

Theorem pair_equal : ∀ {X} (a b c d: X) (e: X × X),
    (a, (b, c)) = (d, e) →
    e = (d, c) →
    b = d.
Proof. (* 在这里填写 *) Admitted.
☐
(* 2026-11-26 15:54 *)