AltAuto精简的自动化处理
- 【泛策略|Tacticals】,允许把策略组合起来;
- 让处理假设名不那么繁琐、也更可维护的新策略;
- 无需人工协助即可证明有限类别定理的【自动求解器|Automatic Solver】;
- 用 auto 策略做的【证明搜索|Proof Search】;以及
- 用于编写策略的【Ltac】语言。
Set Warnings "-notation-overridden,-notation-incompatible-prefix".
From Stdlib Require Import Arith List.
From LF Require Import IndProp.
From Stdlib Require Import Arith List.
From LF Require Import IndProp.
作为自动化好处的一个简单示例,考虑IndProp中
形式化的正则表达式上的另一个问题。同一个字符串集合可由
许多不同的正则表达式表示。例如 App EmptyString re
恰好匹配与 re 相同的字符串。我们可以写一个函数,
把这个事实贯穿到整个正则表达式中,从而把任意正则表达式
「优化」成可能更简单的形式。(为简单起见,该函数不优化
作为其他优化结果出现的表达式。)
Fixpoint re_opt_e {T:Type} (re: reg_exp T) : reg_exp T :=
match re with
| App EmptyStr re2 ⇒ re_opt_e re2
| App re1 re2 ⇒ App (re_opt_e re1) (re_opt_e re2)
| Union re1 re2 ⇒ Union (re_opt_e re1) (re_opt_e re2)
| Star re ⇒ Star (re_opt_e re)
| _ ⇒ re
end.
match re with
| App EmptyStr re2 ⇒ re_opt_e re2
| App re1 re2 ⇒ App (re_opt_e re1) (re_opt_e re2)
| Union re1 re2 ⇒ Union (re_opt_e re1) (re_opt_e re2)
| Star re ⇒ Star (re_opt_e re)
| _ ⇒ re
end.
我们要证明正则表达式与其「优化」形式等价。
该等价的一个方向如下(另一个方向类似)。
Lemma re_opt_e_match : ∀ T (re: reg_exp T) s,
s =~ re → s =~ re_opt_e 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.
+ apply MApp.
× apply IH1.
× apply IH2.
+ inversion Hmatch1. simpl. apply IH2.
+ 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. apply MUnionL. apply IH.
- (* MUnionR *) simpl. apply MUnionR. apply IH.
- (* MStar0 *) simpl. apply MStar0.
- (* MStarApp *) simpl. apply MStarApp.
× apply IH1.
× apply IH2.
Qed.
s =~ re → s =~ re_opt_e 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.
+ apply MApp.
× apply IH1.
× apply IH2.
+ inversion Hmatch1. simpl. apply IH2.
+ 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. apply MUnionL. apply IH.
- (* MUnionR *) simpl. apply MUnionR. apply IH.
- (* MStar0 *) simpl. apply MStar0.
- (* MStarApp *) simpl. apply MStarApp.
× apply IH1.
× apply IH2.
Qed.
那个证明里的重复很烦人。若想扩展优化函数、
处理其他类似的改写机会,这会成为真问题。
我们可以用【泛策略】理顺该证明,接下来就讲。
Theorem silly1 : ∀ n, 1 + n = S n.
Proof. try reflexivity. (* 这就只是做了 reflexivity *) Qed.
Theorem silly2 : ∀ (P : Prop), P → P.
Proof.
intros P HP.
Fail reflexivity.
try reflexivity. (* 证明状态未变 *)
apply HP.
Qed.
Proof. try reflexivity. (* 这就只是做了 reflexivity *) Qed.
Theorem silly2 : ∀ (P : Prop), P → P.
Proof.
intros P HP.
Fail reflexivity.
try reflexivity. (* 证明状态未变 *)
apply HP.
Qed.
在这类完全手工的证明中没有多少理由用 try,但当与
下面要讲的 ; 泛策略一起做自动化证明时,它很有用。
Lemma simple_semi : ∀ n, (n + 1 =? 0) = false.
Proof.
intros n.
destruct n eqn:E.
(* 留下两个子目标,它们的处理方式相同…… *)
- (* n=0 *) simpl. reflexivity.
- (* n=Sn' *) simpl. reflexivity.
Qed.
Proof.
intros n.
destruct n eqn:E.
(* 留下两个子目标,它们的处理方式相同…… *)
- (* n=0 *) simpl. reflexivity.
- (* n=Sn' *) simpl. reflexivity.
Qed.
我们可以用 ; 泛策略简化这个证明:
Lemma simple_semi' : ∀ n, (n + 1 =? 0) = false.
Proof.
intros n.
(* 对当前目标做 destruct *)
destruct n;
(* 然后对每个得到的子目标做 simpl *)
simpl;
(* 并对每个得到的子目标做 reflexivity *)
reflexivity.
Qed.
Proof.
intros n.
(* 对当前目标做 destruct *)
destruct n;
(* 然后对每个得到的子目标做 simpl *)
simpl;
(* 并对每个得到的子目标做 reflexivity *)
reflexivity.
Qed.
或者更简洁地,destruct 可以做 intro,而 simpl 可以省略:
练习: 3 stars,标准 (try_sequence)
Theorem andb_eq_orb :
∀ (b c : bool),
(andb b c = orb b c) →
b = c.
Proof. (* 在这里填写 *) Admitted.
Theorem add_assoc : ∀ n m p : nat,
n + (m + p) = (n + m) + p.
Proof. (* 在这里填写 *) Admitted.
Fixpoint nonzeros (lst : list nat) :=
match lst with
| [] ⇒ []
| 0 :: t ⇒ nonzeros t
| h :: t ⇒ h :: nonzeros t
end.
Lemma nonzeros_app : ∀ lst1 lst2 : list nat,
nonzeros (lst1 ++ lst2) = (nonzeros lst1) ++ (nonzeros lst2).
Proof. (* 在这里填写 *) Admitted.
☐
∀ (b c : bool),
(andb b c = orb b c) →
b = c.
Proof. (* 在这里填写 *) Admitted.
Theorem add_assoc : ∀ n m p : nat,
n + (m + p) = (n + m) + p.
Proof. (* 在这里填写 *) Admitted.
Fixpoint nonzeros (lst : list nat) :=
match lst with
| [] ⇒ []
| 0 :: t ⇒ nonzeros t
| h :: t ⇒ h :: nonzeros t
end.
Lemma nonzeros_app : ∀ lst1 lst2 : list nat,
nonzeros (lst1 ++ lst2) = (nonzeros lst1) ++ (nonzeros lst2).
Proof. (* 在这里填写 *) Admitted.
☐
Lemma re_opt_e_match' : ∀ T (re: reg_exp T) s,
s =~ re → s =~ re_opt_e 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];
(* 在此对每种情况做 simpl: *)
simpl.
- (* MEmpty *) apply MEmpty.
- (* MChar *) apply MChar.
- (* MApp *)
destruct re1;
(* 多数情况由同一公式得到。注意 apply MApp 给出两个子目标:
try apply IH1 在【两者】上运行,在第一个上成功而第二个上
不成功;随后 apply IH2 在剩余目标上运行。 *)
try (apply MApp; try apply IH1; apply IH2).
(* 有趣的情形是 try... 什么也不做,即 re1 = EmptyStr。
此时必须诉诸事实:re1 只匹配空串。 *)
inversion Hmatch1. simpl. apply IH2.
- (* MUnionL *) apply MUnionL. apply IH.
- (* MUnionR *) apply MUnionR. apply IH.
- (* MStar0 *) apply MStar0.
- (* MStarApp *) apply MStarApp. apply IH1. apply IH2.
Qed.
s =~ re → s =~ re_opt_e 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];
(* 在此对每种情况做 simpl: *)
simpl.
- (* MEmpty *) apply MEmpty.
- (* MChar *) apply MChar.
- (* MApp *)
destruct re1;
(* 多数情况由同一公式得到。注意 apply MApp 给出两个子目标:
try apply IH1 在【两者】上运行,在第一个上成功而第二个上
不成功;随后 apply IH2 在剩余目标上运行。 *)
try (apply MApp; try apply IH1; apply IH2).
(* 有趣的情形是 try... 什么也不做,即 re1 = EmptyStr。
此时必须诉诸事实:re1 只匹配空串。 *)
inversion Hmatch1. simpl. apply IH2.
- (* MUnionL *) apply MUnionL. apply IH.
- (* MUnionR *) apply MUnionR. apply IH.
- (* MStar0 *) apply MStar0.
- (* MStarApp *) apply MStarApp. apply IH1. apply IH2.
Qed.
序列泛策略 ;(局部形式)
T; [T' | T' | ... | T']
Theorem app_length : ∀ (X : Type) (lst1 lst2 : list X),
length (lst1 ++ lst2) = (length lst1) + (length lst2).
Proof.
intros; induction lst1;
[reflexivity | simpl; rewrite IHlst1; reflexivity].
Qed.
length (lst1 ++ lst2) = (length lst1) + (length lst2).
Proof.
intros; induction lst1;
[reflexivity | simpl; rewrite IHlst1; reflexivity].
Qed.
恒等策略 idtac 总是成功且不改变证明状态。我们可以用它把
前一证明中的 reflexivity 提取出来。
Theorem app_length' : ∀ (X : Type) (lst1 lst2 : list X),
length (lst1 ++ lst2) = (length lst1) + (length lst2).
Proof.
intros; induction lst1;
[idtac | simpl; rewrite IHlst1];
reflexivity.
Qed.
length (lst1 ++ lst2) = (length lst1) + (length lst2).
Proof.
intros; induction lst1;
[idtac | simpl; rewrite IHlst1];
reflexivity.
Qed.
Lemma re_opt_e_match'' : ∀ T (re: reg_exp T) s,
s =~ re → s =~ re_opt_e 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];
(* 在此对每种情况做 simpl: *)
simpl.
- (* MEmpty *) apply MEmpty.
- (* MChar *) apply MChar.
- (* MApp *)
destruct re1;
try (apply MApp; [apply IH1 | apply IH2]). (* <=== *)
inversion Hmatch1. simpl. apply IH2.
- (* MUnionL *) apply MUnionL. apply IH.
- (* MUnionR *) apply MUnionR. apply IH.
- (* MStar0 *) apply MStar0.
- (* MStarApp *) apply MStarApp; [apply IH1 | apply IH2]. (* <=== *)
Qed.
s =~ re → s =~ re_opt_e 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];
(* 在此对每种情况做 simpl: *)
simpl.
- (* MEmpty *) apply MEmpty.
- (* MChar *) apply MChar.
- (* MApp *)
destruct re1;
try (apply MApp; [apply IH1 | apply IH2]). (* <=== *)
inversion Hmatch1. simpl. apply IH2.
- (* MUnionL *) apply MUnionL. apply IH.
- (* MUnionR *) apply MUnionR. apply IH.
- (* MStar0 *) apply MStar0.
- (* MStarApp *) apply MStarApp; [apply IH1 | apply IH2]. (* <=== *)
Qed.
局部形式也可在策略列表的中间使用 ..:
[ T; [T1 .. | Tn] ]
这样 T1 在第一个子目标上执行,Tn 在最后一个上执行,
T1 还在其余所有子目标上执行。
注意 T1 与 .. 之间没有竖线。不过,竖线也可以补上:
[ T; [T1 | .. | Tn] ]
此时没有任何策略执行于第一个与最后一个之间的子目标。
其他策略可以出现在列表中 .. 的前面或后面,策略也可以省略:
当然,列表中只能有一个 ..,否则策略该重复多少次就不明确。
作为例子,下面是我们在 IndProp 一章中证明过的一个引理:
- T; [T1 | T2 .. | Tn' | Tn]
- T; [T1 ..]
- T; [.. | Tn]
Lemma star_app': ∀ T (s1 s2 : list T) (re : reg_exp T),
s1 =~ Star re →
s2 =~ Star re →
s1 ++ s2 =~ Star re.
Proof.
intros T s1 s2 re H1.
remember (Star re) as re' eqn:Eq.
induction H1
as [|x'|s1 re1 s2' re2 Hmatch1 IH1 Hmatch2 IH2
|s1 re1 re2 Hmatch IH|re1 s2' re2 Hmatch IH
|re''|s1 s2' re'' Hmatch1 IH1 Hmatch2 IH2].
- (* MEmpty *) discriminate.
- (* MChar *) discriminate.
- (* MApp *) discriminate.
- (* MUnionL *) discriminate.
- (* MUnionR *) discriminate.
- (* MStar0 *) intros H. apply H.
- (* MStarApp *)
intros H1. rewrite <- app_assoc.
apply MStarApp.
+ apply Hmatch1.
+ apply IH2.
× apply Eq.
× apply H1.
Qed.
s1 =~ Star re →
s2 =~ Star re →
s1 ++ s2 =~ Star re.
Proof.
intros T s1 s2 re H1.
remember (Star re) as re' eqn:Eq.
induction H1
as [|x'|s1 re1 s2' re2 Hmatch1 IH1 Hmatch2 IH2
|s1 re1 re2 Hmatch IH|re1 s2' re2 Hmatch IH
|re''|s1 s2' re'' Hmatch1 IH1 Hmatch2 IH2].
- (* MEmpty *) discriminate.
- (* MChar *) discriminate.
- (* MApp *) discriminate.
- (* MUnionL *) discriminate.
- (* MUnionR *) discriminate.
- (* MStar0 *) intros H. apply H.
- (* MStarApp *)
intros H1. rewrite <- app_assoc.
apply MStarApp.
+ apply Hmatch1.
+ apply IH2.
× apply Eq.
× apply H1.
Qed.
使用 ..,我们可以把它缩减如下:
Lemma star_app'': ∀ T (s1 s2 : list T) (re : reg_exp T),
s1 =~ Star re →
s2 =~ Star re →
s1 ++ s2 =~ Star re.
Proof.
intros T s1 s2 re H1.
remember (Star re) as re' eqn:Eq.
induction H1
as [|x'|s1 re1 s2' re2 Hmatch1 IH1 Hmatch2 IH2
|s1 re1 re2 Hmatch IH|re1 s2' re2 Hmatch IH
|re''|s1 s2' re'' Hmatch1 IH1 Hmatch2 IH2];
[discriminate .. | intros H; apply H | idtac]. (* <=== *)
(* MStarApp *)
intros H1. rewrite <- app_assoc.
apply MStarApp.
+ apply Hmatch1.
+ apply IH2.
× apply Eq.
× apply H1.
Qed.
s1 =~ Star re →
s2 =~ Star re →
s1 ++ s2 =~ Star re.
Proof.
intros T s1 s2 re H1.
remember (Star re) as re' eqn:Eq.
induction H1
as [|x'|s1 re1 s2' re2 Hmatch1 IH1 Hmatch2 IH2
|s1 re1 re2 Hmatch IH|re1 s2' re2 Hmatch IH
|re''|s1 s2' re'' Hmatch1 IH1 Hmatch2 IH2];
[discriminate .. | intros H; apply H | idtac]. (* <=== *)
(* MStarApp *)
intros H1. rewrite <- app_assoc.
apply MStarApp.
+ apply Hmatch1.
+ apply IH2.
× apply Eq.
× apply H1.
Qed.
策略 repeat T 永不失败:若 T 不能作用于原目标,
repeat 仍成功且不改变原目标(即重复零次)。
Theorem In10' : In 10 [1;2;3;4;5;6;7;8;9;10].
Proof.
repeat (left; reflexivity).
repeat (right; try (left; reflexivity)).
Qed.
Proof.
repeat (left; reflexivity).
repeat (right; try (left; reflexivity)).
Qed.
策略 repeat T 对应用 T 的次数没有上界。若 T 总成功,
repeat T 会永远循环(例如 repeat simpl,因为 simpl
总成功)。Rocq 项语言 Gallina 的求值保证终止,策略求值则
不然。这不影响 Rocq 的逻辑一致性:repeat 等策略只是
引导 Rocq 构造证明。若构造过程发散,只说明我们没能构造
出证明,而非构造了错误的证明。
练习: 1 star,标准 (ev100)
证明 100 是偶数。你的证明脚本应相当短。
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.
(* 用到目前为止描述的泛策略缩短证明。上面的证明约 200 行。
把它减到 50 行或更少,行密度与原证明相当。
七个顶层子弹点各用一击证明解决。
提示:用自底向上方法。先把下面整个证明复制下来。
先自动化最内层子弹点,再向外推进。
不时检查整个证明是否仍能编译。若不能,撤销最近的改动,
直到回到能编译的证明。 *)
Lemma re_opt_match' : ∀ T (re: reg_exp T) s,
s =~ re → s =~ re_opt re.
Proof.
(* 在这里填写 *) Admitted.
(* 不要修改下面这行: *)
Definition manual_grade_for_re_opt : option (nat×string) := None.
☐
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.
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.
(* 用到目前为止描述的泛策略缩短证明。上面的证明约 200 行。
把它减到 50 行或更少,行密度与原证明相当。
七个顶层子弹点各用一击证明解决。
提示:用自底向上方法。先把下面整个证明复制下来。
先自动化最内层子弹点,再向外推进。
不时检查整个证明是否仍能编译。若不能,撤销最近的改动,
直到回到能编译的证明。 *)
Lemma re_opt_match' : ∀ T (re: reg_exp T) s,
s =~ re → s =~ re_opt re.
Proof.
(* 在这里填写 *) Admitted.
(* 不要修改下面这行: *)
Definition manual_grade_for_re_opt : option (nat×string) := None.
☐
我们费心为 HP 起了名字,之后又必须记住它。
若后来在一处改名,就得处处改。同样,若给定理增加新参数,
就得调整 intros 列表。这让维护大型证明变得困难。
因此 Rocq 提供若干策略,让证明脚本不必硬编码名字。
有人可能反对说,假设名能改善证明脚本的自我文档化。
有时或许如此。但对上面两个证明,第一个提到不必要的细节,
第二个则可简单转述为「结论由诸假设推出」。
无论如何,非形式化的(好的)数学证明能启发读者,
Rocq 证明脚本通常不然。担心假设要有丰富、自我文档化
的名字,可能放错了地方。
Theorem false_assumed : False → 0 = 1.
Proof.
intros H. destruct H.
Qed.
Theorem false_assumed' : False → 0 = 1.
Proof.
intros. contradiction.
Qed.
Theorem contras : ∀ (P : Prop), P → ¬P → 0 = 1.
Proof.
intros P HP HNP. exfalso. apply HNP. apply HP.
Qed.
Theorem contras' : ∀ (P : Prop), P → ¬P → 0 = 1.
Proof.
intros. contradiction.
Qed.
Proof.
intros H. destruct H.
Qed.
Theorem false_assumed' : False → 0 = 1.
Proof.
intros. contradiction.
Qed.
Theorem contras : ∀ (P : Prop), P → ¬P → 0 = 1.
Proof.
intros P HP HNP. exfalso. apply HNP. apply HP.
Qed.
Theorem contras' : ∀ (P : Prop), P → ¬P → 0 = 1.
Proof.
intros. contradiction.
Qed.
Theorem many_eq : ∀ (n m o p : nat),
n = m →
o = p →
[n; o] = [m; p].
Proof.
intros n m o p Hnm Hop. rewrite Hnm. rewrite Hop. reflexivity.
Qed.
Theorem many_eq' : ∀ (n m o p : nat),
n = m →
o = p →
[n; o] = [m; p].
Proof.
intros. subst. reflexivity.
Qed.
n = m →
o = p →
[n; o] = [m; p].
Proof.
intros n m o p Hnm Hop. rewrite Hnm. rewrite Hop. reflexivity.
Qed.
Theorem many_eq' : ∀ (n m o p : nat),
n = m →
o = p →
[n; o] = [m; p].
Proof.
intros. subst. reflexivity.
Qed.
实际上该策略有两种形式。
- subst x 在语境中找假设 x = e 或 e = x,
在语境与当前目标中处处把 x 换成 e,并从语境去掉该假设。
- subst 替换掉【所有】形如 x = e 或 e = x 的假设。
Check ev_0 : ev 0.
Check ev_SS : ∀ n : nat, ev n → ev (S (S n)).
Example constructor_example: ∀ (n:nat),
ev (n + n).
Proof.
induction n; simpl.
- constructor. (* 应用 ev_0 *)
- rewrite add_comm. simpl. constructor. (* 应用 ev_SS *)
assumption.
Qed.
Check ev_SS : ∀ n : nat, ev n → ev (S (S n)).
Example constructor_example: ∀ (n:nat),
ev (n + n).
Proof.
induction n; simpl.
- constructor. (* 应用 ev_0 *)
- rewrite add_comm. simpl. constructor. (* 应用 ev_SS *)
assumption.
Qed.
警告:若有多个构造子可用,constructor 按 Inductive
定义中的顺序选第一个。那可能不是你想要的。
自动求解器
线性整数算术:lia 策略
- 类型为 nat、Z 等整数类型的变量与常量;
- 算术运算符 +、-、× 与 ^;
- 相等 = 与序 <、>、≤、≥;以及
- 逻辑联结词 ∧、∨、¬、→、↔,以及常量 True 与 False。
- 对线性目标,lia 要么解决目标,要么失败,意味着目标实际无效。
- 对非线性目标,lia 要么解决要么失败。但此时失败不一定 意味着目标无效,只是因非线性而超出 lia 的证明能力。
From Stdlib Require Import Lia.
Theorem lia_succeed1 : ∀ (n : nat),
n > 0 → n × 2 > n.
Proof. lia. Qed.
Theorem lia_succeed2 : ∀ (n m : nat),
n × m = m × n.
Proof.
lia. (* 虽非线性但可解 *)
Qed.
Theorem lia_fail1 : 0 = 1.
Proof.
Fail lia. (* 目标无效 *)
Abort.
Theorem lia_fail2 : ∀ (n : nat),
n ≥ 1 → 2 ^ n = 2 × 2 ^ (n - 1).
Proof.
Fail lia. (* 目标非线性、合法,但 lia 不可解 *)
Abort.
Theorem lia_succeed1 : ∀ (n : nat),
n > 0 → n × 2 > n.
Proof. lia. Qed.
Theorem lia_succeed2 : ∀ (n m : nat),
n × m = m × n.
Proof.
lia. (* 虽非线性但可解 *)
Qed.
Theorem lia_fail1 : 0 = 1.
Proof.
Fail lia. (* 目标无效 *)
Abort.
Theorem lia_fail2 : ∀ (n : nat),
n ≥ 1 → 2 ^ n = 2 × 2 ^ (n - 1).
Proof.
Fail lia. (* 目标非线性、合法,但 lia 不可解 *)
Abort.
还有别的策略能解算术目标。例如 ring 与 field 能解
【环】与【域】这些代数结构上的方程,策略名由此而来。
这些策略不做 intros。
From Stdlib Require Import Ring.
Theorem mult_comm : ∀ (n m : nat),
n × m = m × n.
Proof.
intros n m. ring.
Qed.
Theorem mult_comm : ∀ (n m : nat),
n × m = m × n.
Proof.
intros n m. ring.
Qed.
相等: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.
intros. rewrite H. reflexivity.
Qed.
∀ (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.
相等的本质性质是:
上面证明中 rewrite 用的正是这一合同性质:
若 a = b 则 f a = f b。(ProofObjects一章以
「莱布尼茨相等」之名进一步探讨此思想。)
congruence 策略是带未解释函数与其他符号的相等的判定过程。
- 自反
- 对称
- 传递
- 【合同|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.
∀ (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 = m →
o = p →
(n, o) = (m, p).
Proof.
congruence.
Qed.
Theorem eq_example3 : ∀ (X : Type) (h : X) (t : list X),
[] ≠ h :: t.
Proof.
congruence.
Qed.
n = m →
o = p →
(n, o) = (m, p).
Proof.
congruence.
Qed.
Theorem eq_example3 : ∀ (X : Type) (h : X) (t : list X),
[] ≠ h :: t.
Proof.
congruence.
Qed.
命题:intuition 策略
Theorem intuition_succeed1 : ∀ (P : Prop),
P → P.
Proof. intuition. Qed.
Theorem intuition_succeed2 : ∀ (P Q : Prop),
¬ (P ∨ Q) → ¬P ∧ ¬Q.
Proof. intuition. Qed.
Theorem intuition_simplify1 : ∀ (P : Prop),
~~P → P.
Proof.
intuition. (* 不是构造性合法的公式 *)
Abort.
Theorem intuition_simplify2 : ∀ (x y : nat) (P Q : nat → Prop),
x = y ∧ (P x → Q x) ∧ P x → Q y.
Proof.
Fail congruence. (* 命题把它难住 *)
intuition. (* = 把它难住,但它简化了命题 *)
congruence.
Qed.
P → P.
Proof. intuition. Qed.
Theorem intuition_succeed2 : ∀ (P Q : Prop),
¬ (P ∨ Q) → ¬P ∧ ¬Q.
Proof. intuition. Qed.
Theorem intuition_simplify1 : ∀ (P : Prop),
~~P → P.
Proof.
intuition. (* 不是构造性合法的公式 *)
Abort.
Theorem intuition_simplify2 : ∀ (x y : nat) (P Q : nat → Prop),
x = y ∧ (P x → Q x) ∧ P x → Q y.
Proof.
Fail congruence. (* 命题把它难住 *)
intuition. (* = 把它难住,但它简化了命题 *)
congruence.
Qed.
在前例中,congruence 或 intuition 单独都不能解决
目标。但 intuition 简化目标中涉及的命题之后,congruence
可成功。对此类情形,intuition 接受可选参数:一个策略,
作用于 intuition 生成的所有未解目标。据此可给出更短的证明:
Theorem intuition_simplify2' : ∀ (x y : nat) (P Q : nat → Prop),
x = y ∧ (P x → Q x) ∧ P x → Q y.
Proof.
intuition congruence.
Qed.
x = y ∧ (P x → Q x) ∧ P x → Q y.
Proof.
intuition congruence.
Qed.
Theorem plus_id_exercise_from_basics : ∀ n m o : nat,
n = m → m = o → n + m = m + o.
Proof. (* 在这里填写 *) Admitted.
Theorem add_assoc_from_induction : ∀ n m p : nat,
n + (m + p) = (n + m) + p.
Proof. (* 在这里填写 *) Admitted.
Theorem S_injective_from_tactics : ∀ (n m : nat),
S n = S m →
n = m.
Proof. (* 在这里填写 *) Admitted.
Theorem or_distributes_over_and_from_logic : ∀ P Q R : Prop,
P ∨ (Q ∧ R) ↔ (P ∨ Q) ∧ (P ∨ R).
Proof. (* 在这里填写 *) Admitted.
☐
n = m → m = o → n + m = m + o.
Proof. (* 在这里填写 *) Admitted.
Theorem add_assoc_from_induction : ∀ n m p : nat,
n + (m + p) = (n + m) + p.
Proof. (* 在这里填写 *) Admitted.
Theorem S_injective_from_tactics : ∀ (n m : nat),
S n = S m →
n = m.
Proof. (* 在这里填写 *) Admitted.
Theorem or_distributes_over_and_from_logic : ∀ P Q R : Prop,
P ∨ (Q ∧ R) ↔ (P ∨ Q) ∧ (P ∨ R).
Proof. (* 在这里填写 *) Admitted.
☐
搜索策略
Example auto_example_1 : ∀ (P Q R: Prop),
(P → Q) → (Q → R) → P → R.
Proof.
intros P Q R H1 H2 H3.
apply H2. apply H1. apply H3.
Qed.
(P → Q) → (Q → R) → P → R.
Proof.
intros P Q R H1 H2 H3.
apply H2. apply H1. apply H3.
Qed.
auto 策略通过【搜索】能证明目标的应用序列,把我们从苦差中
解放出来:
auto 策略能解决任何可由下列组合解决的目标:
使用 auto 总是「安全」的:它永不失败,也永不改变
证明状态。要么完全解决当前目标,要么什么也不做。
下面是更有趣的示例,展示 auto 的威力:
- intros 与
- apply(默认用局部语境中的假设)。
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.
(P → Q) →
(P → R) →
(T → R) →
(S → T → U) →
((P → Q) → (P → S)) →
T →
P →
U.
Proof. auto. Qed.
原则上,证明搜索可能耗时任意久,因此 auto 默认搜索深度有
上限。
Example auto_example_3 : ∀ (P Q R S T U: Prop),
(P → Q) →
(Q → R) →
(R → S) →
(S → T) →
(T → U) →
P →
U.
Proof.
(* 无法解决目标时,auto 什么也不做 *)
auto.
(* 可选参数说明搜索多深(默认为 5) *)
auto 6.
Qed.
(P → Q) →
(Q → R) →
(R → S) →
(S → T) →
(T → U) →
P →
U.
Proof.
(* 无法解决目标时,auto 什么也不做 *)
auto.
(* 可选参数说明搜索多深(默认为 5) *)
auto 6.
Qed.
auto 策略同时考虑当前语境中的假设,以及一个包含其他引理与
构造子的【提示数据库|Hint Database】。关于相等与逻辑运算符的
一些常见事实默认已装入该数据库。
若想看 auto 用了哪些事实,可改用 info_auto。
Example auto_example_5 : 2 = 2.
Proof.
(* auto 包含 reflexivity,因为 eq_refl 在提示数据库中。 *)
info_auto.
Qed.
Proof.
(* auto 包含 reflexivity,因为 eq_refl 在提示数据库中。 *)
info_auto.
Qed.
我们可以只在某一次 auto 调用期间用 auto using t
扩展提示数据库。
Lemma le_antisym : ∀ n m: nat, (n ≤ m ∧ m ≤ n) → n = m.
Proof. intros. lia. Qed.
Example auto_example_6 : ∀ n m p : nat,
(n ≤ p → (n ≤ m ∧ m ≤ n)) →
n ≤ p →
n = m.
Proof.
auto using le_antisym.
Qed.
Proof. intros. lia. Qed.
Example auto_example_6 : ∀ n m p : nat,
(n ≤ p → (n ≤ m ∧ m ≤ n)) →
n ≤ p →
n = m.
Proof.
auto using le_antisym.
Qed.
当然,在任一给定开发中,很可能有些构造子与引理在证明里
用得很多。我们可写
Create HintDb db. 创建名为 db 的数据库,再写
Hint Resolve T : db. 把 T 加入数据库,其中 T 是顶层定理,或归纳定义命题的构造子 (即任何类型为蕴含的东西)。我们写 auto with db 告诉 auto 使用该数据库。严格说创建数据库是可选的;Rocq 会在首次使用 Hint 时自动创建。
Create HintDb db. 创建名为 db 的数据库,再写
Hint Resolve T : db. 把 T 加入数据库,其中 T 是顶层定理,或归纳定义命题的构造子 (即任何类型为蕴含的东西)。我们写 auto with db 告诉 auto 使用该数据库。严格说创建数据库是可选的;Rocq 会在首次使用 Hint 时自动创建。
Create HintDb le_db.
Hint Resolve le_antisym : le_db.
Example auto_example_6' : ∀ n m p : nat,
(n ≤ p → (n ≤ m ∧ m ≤ n)) →
n ≤ p →
n = m.
Proof.
auto with le_db.
Qed.
Hint Resolve le_antisym : le_db.
Example auto_example_6' : ∀ n m p : nat,
(n ≤ p → (n ≤ m ∧ m ≤ n)) →
n ≤ p →
n = m.
Proof.
auto with le_db.
Qed.
作为简写,可写
Hint Constructors c : db. 告诉 Rocq 对 c 归纳定义的【所有】构造子都做 Hint Resolve。
有时还需要加入
Hint Unfold d : db. 其中 d 是已定义符号,好让 auto 知道何时展开 d, 从而多一些应用已知引理的机会。
Hint Constructors c : db. 告诉 Rocq 对 c 归纳定义的【所有】构造子都做 Hint Resolve。
Hint Unfold d : db. 其中 d 是已定义符号,好让 auto 知道何时展开 d, 从而多一些应用已知引理的机会。
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 : le_db.
Example auto_example_7' : ∀ x,
(x ≤ 42 ∧ 42 ≤ x) → is_fortytwo x.
Proof. info_auto with le_db. Qed.
Example auto_example_7: ∀ x,
(x ≤ 42 ∧ 42 ≤ x) → is_fortytwo x.
Proof.
auto. (* 什么也不做 *)
Abort.
Hint Unfold is_fortytwo : le_db.
Example auto_example_7' : ∀ x,
(x ≤ 42 ∧ 42 ≤ x) → is_fortytwo x.
Proof. info_auto with le_db. Qed.
auto 总是使用的「全局」数据库名为 core。
你可以往里加自己的提示,但 Rocq 手册不鼓励这样做,
更倾向于为特定开发建专用数据库。许多重要库有自己的提示数据库,
你可以挂上:arith、bool、datatypes(含列表)等。
Example auto_example_8 : ∀ (n m : nat),
n + m = m + n.
Proof.
auto. (* 无进展 *)
info_auto with arith. (* 使用 Nat.add_comm *)
Qed.
n + m = m + n.
Proof.
auto. (* 无进展 *)
info_auto with arith. (* 使用 Nat.add_comm *)
Qed.
练习: 3 stars,标准 (re_opt_match_auto)
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.
☐
s =~ re → s =~ re_opt re.
Proof.
(* 在这里填写 *) Admitted.
(* 不要修改下面这行: *)
Definition manual_grade_for_re_opt_match'' : option (nat×string) := None.
☐
Import Pumping.
Lemma weak_pumping : ∀ T (re : reg_exp T) s,
s =~ re →
pumping_constant re ≤ length s →
∃ s1 s2 s3,
s = s1 ++ s2 ++ s3 ∧
s2 ≠ [] ∧
∀ m, s1 ++ napp m s2 ++ s3 =~ re.
Proof.
(* 在这里填写 *) Admitted.
(* 不要修改下面这行: *)
Definition manual_grade_for_pumping_redux : option (nat×string) := None.
☐
Lemma weak_pumping : ∀ T (re : reg_exp T) s,
s =~ re →
pumping_constant re ≤ length s →
∃ s1 s2 s3,
s = s1 ++ s2 ++ s3 ∧
s2 ≠ [] ∧
∀ m, s1 ++ napp m s2 ++ s3 =~ re.
Proof.
(* 在这里填写 *) Admitted.
(* 不要修改下面这行: *)
Definition manual_grade_for_pumping_redux : option (nat×string) := None.
☐
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.
☐
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.
☐
Example trans_example1: ∀ a b c d,
a ≤ b + b × c →
(1 + c) × b ≤ d →
a ≤ d.
Proof.
intros a b c d H1 H2.
apply Nat.le_trans with (b + b × c).
(* ^ 我们必须给出中间值 *)
- apply H1.
- simpl in H2. rewrite mul_comm. apply H2.
Qed.
a ≤ b + b × c →
(1 + c) × b ≤ d →
a ≤ d.
Proof.
intros a b c d H1 H2.
apply Nat.le_trans with (b + b × c).
(* ^ 我们必须给出中间值 *)
- apply H1.
- simpl in H2. rewrite mul_comm. apply H2.
Qed.
证明的第一步必须显式给出一个偏长的表达式,
帮 Rocq 为 le_trans 实例化「隐藏」参数。原因是
le_trans 的定义……
le_trans : ∀ m n o : nat, m ≤ n → n ≤ o → m ≤ o 对不在结论中出现的变量 n 做了量化。因此把结论与目标 统一,并不能帮 Rocq 找到该变量的取值。若省略 with, 此步会失败("Error: Unable to find an instance for the variable n")。
我们已知一种避免显式 with 子句的办法:把 H1 作为
le_trans 的(第一个)显式参数给出。还有另一种办法:
用 eapply 策略:
le_trans : ∀ m n o : nat, m ≤ n → n ≤ o → m ≤ o 对不在结论中出现的变量 n 做了量化。因此把结论与目标 统一,并不能帮 Rocq 找到该变量的取值。若省略 with, 此步会失败("Error: Unable to find an instance for the variable n")。
Example trans_example1': ∀ a b c d,
a ≤ b + b × c →
(1 + c) × b ≤ d →
a ≤ d.
Proof.
intros a b c d H1 H2.
eapply Nat.le_trans. (* 1 *)
- apply H1. (* 2 *)
- simpl in H2. rewrite mul_comm. apply H2.
Qed.
a ≤ b + b × c →
(1 + c) × b ≤ d →
a ≤ d.
Proof.
intros a b c d H1 H2.
eapply Nat.le_trans. (* 1 *)
- apply H1. (* 2 *)
- simpl in H2. rewrite mul_comm. apply H2.
Qed.
eapply H 与 apply H 行为相同,只有一点不同:
把目标与 H 的结论统一后,它不检查统一过程中引入的
变量是否都已得到具体取值。
若单步走完上面的证明,位置 1 的目标在两个子目标中
都提到【存在性变量|Existential Variable】?n。
下一步(位置 2)把 ?n 换成具体值。
开始处理第二个子目标(位置 3)时,其中的 ?n
已换成第一个子目标中得到的取值。
迄今见过的若干策略,包括 ∃、constructor 与
auto,都有 e... 变体。例如,下面用 eauto 的证明:
Example trans_example2: ∀ a b c d,
a ≤ b + b × c →
b + b × c ≤ d →
a ≤ d.
Proof.
intros a b c d H1 H2.
info_eauto using Nat.le_trans.
Qed.
a ≤ b + b × c →
b + b × c ≤ d →
a ≤ d.
Proof.
intros a b c d H1 H2.
info_eauto using Nat.le_trans.
Qed.
eauto 策略与 auto 相同,只是它用 eapply 而非
apply。
专业提示:有人可能以为,既然 eapply 与 eauto 比 apply
与 auto 更强,就该一直用它们。不幸的是,它们也明显更慢,
尤其是 eauto。Rocq 专家多数时候仍用 apply 与 auto,
只在普通变体不够用时才切换到带 e 的变体。
Ltac:策略语言
Ltac 函数
Ltac simpl_and_try tac := simpl; try tac.
这定义了名为 simpl_and_try 的新策略,它以一个策略 tac
为参数,定义为等价于 simpl; try tac。现在在证明中写
"simpl_and_try reflexivity." 等同于写
"simpl; try reflexivity."
Example sat_ex1 : 1 + 1 = 2.
Proof. simpl_and_try reflexivity. Qed.
Example sat_ex2 : ∀ (n : nat), 1 - 1 + n + 1 = 1 + n.
Proof. simpl_and_try reflexivity. lia. Qed.
Proof. simpl_and_try reflexivity. Qed.
Example sat_ex2 : ∀ (n : nat), 1 - 1 + n + 1 = 1 + n.
Proof. simpl_and_try reflexivity. lia. Qed.
当然,那个小策略没那么有用。但它表明:我们可以参数化
Ltac 定义的策略,且它们的体本身是在证明语境中运行的策略。
因此 Ltac 可用来创建作用于策略的函数。
为得到更有用的策略,考虑Basics中的三个证明,
以及它们在结构上何等相似:
Theorem plus_1_neq_0 : ∀ n : nat,
(n + 1) =? 0 = false.
Proof.
intros n. destruct n.
- reflexivity.
- reflexivity.
Qed.
Theorem negb_involutive : ∀ b : bool,
negb (negb b) = b.
Proof.
intros b. destruct b.
- reflexivity.
- reflexivity.
Qed.
Theorem andb_commutative : ∀ b c, andb b c = andb c b.
Proof.
intros b c. destruct b.
- destruct c.
+ reflexivity.
+ reflexivity.
- destruct c.
+ reflexivity.
+ reflexivity.
Qed.
(n + 1) =? 0 = false.
Proof.
intros n. destruct n.
- reflexivity.
- reflexivity.
Qed.
Theorem negb_involutive : ∀ b : bool,
negb (negb b) = b.
Proof.
intros b. destruct b.
- reflexivity.
- reflexivity.
Qed.
Theorem andb_commutative : ∀ b c, andb b c = andb c b.
Proof.
intros b c. destruct b.
- destruct c.
+ reflexivity.
+ reflexivity.
- destruct c.
+ reflexivity.
+ reflexivity.
Qed.
我们可以提取公共结构:
- 做一次 destruct。
- 对每个分支,若可能则用 reflexivity 收尾。
Ltac destructpf x :=
destruct x; try reflexivity.
Theorem plus_1_neq_0' : ∀ n : nat,
(n + 1) =? 0 = false.
Proof. intros n; destructpf n. Qed.
Theorem negb_involutive' : ∀ b : bool,
negb (negb b) = b.
Proof. intros b; destructpf b. Qed.
Theorem andb_commutative' : ∀ b c, andb b c = andb c b.
Proof.
intros b c; destructpf b; destructpf c.
Qed.
destruct x; try reflexivity.
Theorem plus_1_neq_0' : ∀ n : nat,
(n + 1) =? 0 = false.
Proof. intros n; destructpf n. Qed.
Theorem negb_involutive' : ∀ b : bool,
negb (negb b) = b.
Proof. intros b; destructpf b. Qed.
Theorem andb_commutative' : ∀ b c, andb b c = andb c b.
Proof.
intros b c; destructpf b; destructpf c.
Qed.
Theorem andb3_exchange :
∀ b c d, andb (andb b c) d = andb (andb b d) c.
Proof. (* 在这里填写 *) Admitted.
☐
∀ b c d, andb (andb b c) d = andb (andb b d) c.
Proof. (* 在这里填写 *) Admitted.
☐
Theorem andb_true_elim2 : ∀ b c : bool,
andb b c = true → c = true.
Proof.
intros b c. destruct b eqn:Eb.
- simpl. intros H. rewrite H. reflexivity.
- simpl. intros H. destruct c eqn:Ec.
+ reflexivity.
+ rewrite H. reflexivity.
Qed.
andb b c = true → c = true.
Proof.
intros b c. destruct b eqn:Eb.
- simpl. intros H. rewrite H. reflexivity.
- simpl. intros H. destruct c eqn:Ec.
+ reflexivity.
+ rewrite H. reflexivity.
Qed.
取消下面 destructpf' 定义的注释,并定义你自己改进版的
destructpf。用它证明该定理。
(*
Ltac destructpf' x := ...
*)
Ltac destructpf' x := ...
*)
你的一击证明应只需 intros 与 destructpf'。
再检查 intros 与新的 destructpf' 是否仍足以证明
更早的那个定理。也就是说,改进后的策略是否仍足够一般、
能一击证明它:
Theorem andb3_exchange' :
∀ b c d, andb (andb b c) d = andb (andb b d) c.
Proof. (* 在这里填写 *) Admitted.
☐
∀ b c d, andb (andb b c) d = andb (andb b d) c.
Proof. (* 在这里填写 *) Admitted.
☐
Theorem app_nil_r : ∀ (X : Type) (lst : list X),
lst ++ [] = lst.
Proof.
intros X lst. induction lst as [ | h t IHt].
- reflexivity.
- simpl. rewrite IHt. reflexivity.
Qed.
lst ++ [] = lst.
Proof.
intros X lst. induction lst as [ | h t IHt].
- reflexivity.
- simpl. rewrite IHt. reflexivity.
Qed.
在 rewrite 处我们不能替换掉 t:它出现在归纳假设
IHt : t ++ [] = t 中等式的两边。如何在 Ltac 中挑出
该改写哪个假设?
为解决这类问题,Ltac 含模式匹配策略 match goal。
它允许我们对【证明状态】匹配,而非对程序匹配。
语法类似 Gallina(Rocq 项语言)中的 match,
但有一些新特点:
因此,上面的单个分支匹配这样的目标:结论为 True,
假设可以是任何东西。匹配成功时运行 apply I。
可能有多个分支,按顺序尝试。
- 这里的 goal 是关键字,而非被匹配的表达式。它表示对证明状态
匹配,而非对程序项匹配。
- 模式周围的方括号常可省略,但它们确实便于视觉区分代码中
哪部分是模式。
- 推演符号 ⊢ 把假设模式(若有的)与结论模式分开。
它表示 IDE 在证明状态中显示的大横线:假设在其左,结论在其右。
- 模式中的假设不必完全描述证明状态中的全部假设。
证明状态中另有不匹配任何模式的假设也没关系。
要点是让 match goal 挑出感兴趣的特定假设,
而非完全指定证明状态。
- 分支右侧是要运行的策略,而非程序项。
Theorem match_ex2 : True ∧ True.
Proof.
match goal with
| [ ⊢ True ] ⇒ apply I
| [ ⊢ True ∧ True ] ⇒ split; apply I
end.
Qed.
Proof.
match goal with
| [ ⊢ True ] ⇒ apply I
| [ ⊢ True ∧ True ] ⇒ split; apply I
end.
Qed.
为查看正在尝试哪些分支,可插入恒等策略 idtac 的调用。
它可选接受一个参数,打印为调试信息。
Theorem match_ex2' : True ∧ True.
Proof.
match goal with
| [ ⊢ True ] ⇒ idtac "branch 1"; apply I
| [ ⊢ True ∧ True ] ⇒ idtac "branch 2"; split; apply I
end.
Qed.
Proof.
match goal with
| [ ⊢ True ] ⇒ idtac "branch 1"; apply I
| [ ⊢ True ∧ True ] ⇒ idtac "branch 2"; split; apply I
end.
Qed.
只尝试了第二个分支。第一个未匹配目标。
策略 match goal 的语义与项 match 的语义很不同。
对后者,选中第一个匹配模式后,其后的分支永不再考虑。
若其后分支已知冗余,还会报错。
但对 match goal,若某分支的策略失败,
模式匹配会继续尝试下一分支,直到某分支成功,
或所有分支都失败。
Theorem match_ex2'' : True ∧ True.
Proof.
match goal with
| [ ⊢ _ ] ⇒ idtac "branch 1"; apply I
| [ ⊢ True ∧ True ] ⇒ idtac "branch 2"; split; apply I
end.
Qed.
Proof.
match goal with
| [ ⊢ _ ] ⇒ idtac "branch 1"; apply I
| [ ⊢ True ∧ True ] ⇒ idtac "branch 2"; split; apply I
end.
Qed.
第一个分支被尝试但失败,随后第二个分支被尝试并成功。
若所有分支都失败,则 match goal 失败。
Theorem match_ex2''' : True ∧ True.
Proof.
Fail match goal with
| [ ⊢ _ ] ⇒ idtac "branch 1"; apply I
| [ ⊢ _ ] ⇒ idtac "branch 2"; apply I
end.
Abort.
Proof.
Fail match goal with
| [ ⊢ _ ] ⇒ idtac "branch 1"; apply I
| [ ⊢ _ ] ⇒ idtac "branch 2"; apply I
end.
Abort.
接下来试着对假设匹配。我们可以绑定假设名,
如下面的 H,并在分支右侧使用该名字。
Theorem match_ex3 : ∀ (P : Prop), P → P.
Proof.
intros P HP.
match goal with
| [ H : _ ⊢ _ ] ⇒ apply H
end.
Qed.
Proof.
intros P HP.
match goal with
| [ H : _ ⊢ _ ] ⇒ apply H
end.
Qed.
假设的实际名字当然是 HP,但模式把它绑定为 H。
用 idtac 甚至能观察实际名字:单步走下面的证明会打印 "HP"。
Theorem match_ex3' : ∀ (P : Prop), P → P.
Proof.
intros P HP.
match goal with
| [ H : _ ⊢ _ ] ⇒ idtac H; apply H
end.
Qed.
Proof.
intros P HP.
match goal with
| [ H : _ ⊢ _ ] ⇒ idtac H; apply H
end.
Qed.
我们会继续用一阵 idtac 以观察 match goal 的行为,
但注意:它对下列各例的成功证明并非必需。
若有多个匹配的假设,Ltac 选哪一个?这与对项的普通 match
有很大不同:Ltac 会尝试所有可能的匹配,直到某个成功
(或全部失败)。
Theorem match_ex4 : ∀ (P Q : Prop), P → Q → P.
Proof.
intros P Q HP HQ.
match goal with
| [ H : _ ⊢ _ ] ⇒ idtac H; apply H
end.
Qed.
Proof.
intros P Q HP HQ.
match goal with
| [ H : _ ⊢ _ ] ⇒ idtac H; apply H
end.
Qed.
该例先打印 "HQ" 再打印 "HP"。Ltac 先匹配最近引入的
假设 HQ 并尝试应用。那没有解决目标。于是 Ltac【回溯】,
尝试次近的匹配假设 HP。应用它成功了。
但若没有成功的假设,整个匹配会失败:
Theorem match_ex5 : ∀ (P Q R : Prop), P → Q → R.
Proof.
intros P Q R HP HQ.
Fail match goal with
| [ H : _ ⊢ _ ] ⇒ idtac H; apply H
end.
Abort.
Proof.
intros P Q R HP HQ.
Fail match goal with
| [ H : _ ⊢ _ ] ⇒ idtac H; apply H
end.
Abort.
到目前为止我们对如何匹配假设要求不高。
用过的【通配符】(又称【百搭】)模式匹配一切。
可以用【元变量】更具体一些:
Theorem match_ex5 : ∀ (P Q : Prop), P → Q → P.
Proof.
intros P Q HP HQ.
match goal with
| [ H : ?X ⊢ ?X ] ⇒ idtac H; apply H
end.
Qed.
Proof.
intros P Q HP HQ.
match goal with
| [ H : ?X ⊢ ?X ] ⇒ idtac H; apply H
end.
Qed.
注意这次 idtac 打印的唯一假设是 HP。
HQ 被排除,因为它不具有 ?X ⊢ ?X 形式。
该模式中 ?X 的出现作为【元变量】,代表同一项:
它既是假设 H 的类型,也是目标的结论。
(match goal 的语法用 ? 区分元变量与作用域中可能
出现的其他标识符。? 只用在模式中;分支右侧必须去掉
?。)
现在又看到 match goal 与对项的普通 match
的另一处不同:match goal 允许元变量在模式中多次出现,
每次都代表同一项。普通 match 不允许:
Fail Definition dup_first_two_elts (lst : list nat) :=
match lst with
| x :: x :: _ ⇒ true
| _ ⇒ false
end.
match lst with
| x :: x :: _ ⇒ true
| _ ⇒ false
end.
术语是【线性】:普通 match 要求模式变量是【线性】的,
即只使用一次。策略 match goal 允许【非线性】元变量,
即可在模式中多次使用,且每次必须绑定同一项。
既然已了解一点 match goal,回到一些简单归纳的证明模式:
Theorem app_nil_r' : ∀ (X : Type) (lst : list X),
lst ++ [] = lst.
Proof.
intros X lst. induction lst as [ | h t IHt].
- reflexivity.
- simpl. rewrite IHt. reflexivity.
Qed.
lst ++ [] = lst.
Proof.
intros X lst. induction lst as [ | h t IHt].
- reflexivity.
- simpl. rewrite IHt. reflexivity.
Qed.
用 match goal,可以自动化该证明模式:
Ltac simple_induction t :=
induction t; simpl;
try match goal with
| [H : _ = _ ⊢ _] ⇒ rewrite H
end;
reflexivity.
Theorem app_nil_r'' : ∀ (X : Type) (lst : list X),
lst ++ [] = lst.
Proof.
intros X lst. simple_induction lst.
Qed.
induction t; simpl;
try match goal with
| [H : _ = _ ⊢ _] ⇒ rewrite H
end;
reflexivity.
Theorem app_nil_r'' : ∀ (X : Type) (lst : list X),
lst ++ [] = lst.
Proof.
intros X lst. simple_induction lst.
Qed.
效果很好!下面是遵循同一模式的另外两个证明。
Theorem add_assoc'' : ∀ n m p : nat,
n + (m + p) = (n + m) + p.
Proof.
intros n m p. induction n.
- reflexivity.
- simpl. rewrite IHn. reflexivity.
Qed.
Theorem add_assoc''' : ∀ n m p : nat,
n + (m + p) = (n + m) + p.
Proof.
intros n m p. simple_induction n.
Qed.
Theorem plus_n_Sm : ∀ n m : nat,
S (n + m) = n + (S m).
Proof.
intros n m. induction n.
- reflexivity.
- simpl. rewrite IHn. reflexivity.
Qed.
Theorem plus_n_Sm' : ∀ n m : nat,
S (n + m) = n + (S m).
Proof.
intros n m. simple_induction n.
Qed.
n + (m + p) = (n + m) + p.
Proof.
intros n m p. induction n.
- reflexivity.
- simpl. rewrite IHn. reflexivity.
Qed.
Theorem add_assoc''' : ∀ n m p : nat,
n + (m + p) = (n + m) + p.
Proof.
intros n m p. simple_induction n.
Qed.
Theorem plus_n_Sm : ∀ n m : nat,
S (n + m) = n + (S m).
Proof.
intros n m. induction n.
- reflexivity.
- simpl. rewrite IHn. reflexivity.
Qed.
Theorem plus_n_Sm' : ∀ n m : nat,
S (n + m) = n + (S m).
Proof.
intros n m. simple_induction n.
Qed.
用 match goal 证明重言式
Ltac imp_intuition :=
repeat match goal with
| [ H : ?P ⊢ ?P ] ⇒ apply H
| [ ⊢ ∀ _, _ ] ⇒ intro
| [ H1 : ?P → ?Q, H2 : ?P ⊢ _ ] ⇒ apply H1 in H2
end.
repeat match goal with
| [ H : ?P ⊢ ?P ] ⇒ apply H
| [ ⊢ ∀ _, _ ] ⇒ intro
| [ H1 : ?P → ?Q, H2 : ?P ⊢ _ ] ⇒ apply H1 in H2
end.
该策略反复对目标匹配,直到匹配不再取得进展。每一步中,
match goal 做三件事之一:
我们已能用该策略证明许多定理:
- 发现结论已在假设中,此时目标完成。
- 发现结论是量化句,此时引入它。由于蕴含 P → Q 本身
是量化句 ∀ (_ : P), Q,该情形也处理蕴含的引入。
- 发现假设中有形如 ?P → ?Q 与 ?P 的两个公式。 这是我们第一次同时对两个假设做匹配。 注意元变量 ?P 再次是非线性的:同一公式必须出现在 两个不同假设中。此时策略用正向推理把假设 H2 变成 ?Q。
Example imp1 : ∀ (P : Prop), P → P.
Proof. imp_intuition. Qed.
Example imp2 : ∀ (P Q : Prop), P → (P → Q) → Q.
Proof. imp_intuition. Qed.
Example imp3 : ∀ (P Q R : Prop), (P → Q → R) → (Q → P → R).
Proof. imp_intuition. Qed.
Proof. imp_intuition. Qed.
Example imp2 : ∀ (P Q : Prop), P → (P → Q) → Q.
Proof. imp_intuition. Qed.
Example imp3 : ∀ (P Q R : Prop), (P → Q → R) → (Q → P → R).
Proof. imp_intuition. Qed.
假设要加一个新的逻辑联结词:nor,即「非或」联结词。
Inductive nor (P Q : Prop) :=
| stroke : ¬P → ¬Q → nor P Q.
Theorem nor_not_or : ∀ (P Q : Prop),
nor P Q → ¬ (P ∨ Q).
Proof.
intros. destruct H. unfold not. intros. destruct H. auto. auto.
Qed.
| stroke : ¬P → ¬Q → nor P Q.
Theorem nor_not_or : ∀ (P Q : Prop),
nor P Q → ¬ (P ∨ Q).
Proof.
intros. destruct H. unfold not. intros. destruct H. auto. auto.
Qed.
不过关于 nor 的其他常见定理仍可证。
Theorem nor_comm : ∀ (P Q : Prop),
nor P Q ↔ nor Q P.
Proof.
intros P Q. split.
- intros H. destruct H. apply stroke; assumption.
- intros H. destruct H. apply stroke; assumption.
Qed.
Theorem nor_not : ∀ (P : Prop),
nor P P ↔ ¬P.
Proof.
intros P. split.
- intros H. destruct H. assumption.
- intros H. apply stroke; assumption.
Qed.
nor P Q ↔ nor Q P.
Proof.
intros P Q. split.
- intros H. destruct H. apply stroke; assumption.
- intros H. destruct H. apply stroke; assumption.
Qed.
Theorem nor_not : ∀ (P : Prop),
nor P P ↔ ¬P.
Proof.
intros P. split.
- intros H. destruct H. assumption.
- intros H. apply stroke; assumption.
Qed.
练习: 4 stars,高级 (nor_intuition)
(* Ltac nor_intuition := ... *)
下面三个定理,以及涉及这些逻辑联结词的许多其他定理,
应只需 Proof. nor_intuition. Qed. 即可证明。
Theorem nor_comm' : ∀ (P Q : Prop),
nor P Q ↔ nor Q P.
Proof. (* 在这里填写 *) Admitted.
Theorem nor_not' : ∀ (P : Prop),
nor P P ↔ ¬P.
Proof. (* 在这里填写 *) Admitted.
Theorem nor_not_and' : ∀ (P Q : Prop),
nor P Q → ¬ (P ∧ Q).
Proof. (* 在这里填写 *) Admitted.
(* 不要修改下面这行: *)
Definition manual_grade_for_nor_intuition : option (nat×string) := None.
☐
nor P Q ↔ nor Q P.
Proof. (* 在这里填写 *) Admitted.
Theorem nor_not' : ∀ (P : Prop),
nor P P ↔ ¬P.
Proof. (* 在这里填写 *) Admitted.
Theorem nor_not_and' : ∀ (P Q : Prop),
nor P Q → ¬ (P ∧ Q).
Proof. (* 在这里填写 *) Admitted.
(* 不要修改下面这行: *)
Definition manual_grade_for_nor_intuition : option (nat×string) := None.
☐
复习
- try、;、repeat
- assumption、contradiction、subst、constructor
- lia、congruence、intuition
- auto、eauto、eapply
- Ltac 函数与 match goal
(* 2026-11-26 15:54 *)
