這部分內容寫了快兩個星期了,期末考試越來越近,緊張緊張
Proof By Simplification
之前的代碼中出現過
Proof. simpl. reflexivity. Qed.
simpl是將方程的兩邊簡化,reflexivity使用自反性檢查兩邊是否包含相同的值
例如:
Theorem plus_0_n:forall n:nat,0+n=n.
Proof.
intros n. simpl. reflexivity. Qed.
實際上自反性可以在檢查兩邊式子的時候自動實現一些簡化,我們可以省略simpl的過程(simpl相當於將中間過程展示了出來)
Theorem plus_O_n' : ∀n : nat, 0 + n = n.
Proof.
intros n. reflexivity. Qed.
以上的證明過程使用了Theorem引出需要證明的結論(實際上和exmaple差不多)
intros n. simpl. reflexivity.
以上是一個tatics的例子,tatics後續會詳細介紹,這裏我們只需要知道intro n.是將forall一般化
Proof By Rewriting
Theorem plus_id:forall n m:nat,n=m->n+n=m+m.
上述定理描述只有在n=m時,後續的定理才成立,並且需要對m和n一般化,intro. 可以實現上述三個目標
由於n和m是任意數,我們不能用simpl直接簡化,需要利用條件n=m,將所有的n用m替換,再證明式子,這個過程用rewrite實現(是不是很像命題邏輯中的替換和代入規則)
Theorem plus_id:forall n m:nat,n=m->n+n=m+m.
Proof.
intros n m.
intros H. (*將前提引入到證明環境中*)
rewrite->H.
reflexivity.
Qed.
rewrite->H.代表將前提從左到右重寫,n->m說明將n全部用m代替,也可以寫成rewrite<-H,表示n<-m將m全部替換爲n
Proof by Case Analysis
然而在一些未知條件過多的證明中,僅僅使用上述策略無法實現證明
Theorem plus_1_neq_0_firsttry : ∀n : nat,
(n + 1) =? 0 = false.
Proof.
intros n.
simpl. (* does nothing! *)
Abort.
原因是n+1=0無法再進行簡化
那樣我們就需要考慮所有n的情況,利用枚舉法證明結論
destruct
我們提到了關於表示出n所有可能性的想法,coq中用destruct函數來實現列舉可能的情況
例1
Theorem plus_1_neq_0 : ∀n : nat,
eqb (n + 1) 0 = false.
Proof.
intros n.
destruct n as [| n']
- reflexivity.
- reflexivity. Qed.
ps:我沒找到coq裏面關於eqb函數的notation,這裏直接用函數表示了
- destruct函數爲我們生成兩個子目標:n=O,n=S n’,我們需要分別證明其正確性
destruct n as [| n']
表示對每種情況需要引入的變量名,在這個證明中coq不會自動分類,需要根據引入的變量名修改子目標,[ ]表示一個參數列表,參數之間用“|”分割- 我們使用“-”來使代碼的分類證明更加有序
例2
我們也可以利用析構函數的策略證明相關的布爾表達式
Theorem negb_involutive:forall b:bool,negb (negb b)=b.
Proof.
intros b.
destruct b.
- reflexivity.
- reflexivity. Qed.
多重析構
有時我們需要對子目標繼續進行析構,這時我們需要用不同的項目符號標記目標
Theorem andb_communtive:forall b c,(andb b c)=(andb c b).
(*定律描述*)
Proof.
intros b c.
destruct b.
- destruct c.
+ reflexivity.
+ reflexivity.
- destruct c.
+ reflexivity.
+ reflexivity.
Qed.
遇到三重以上證明時,可以用*作爲第三層析構
我們也可以用括號包含析構內容,使代碼更清晰
Theorem andb_commutative' : ∀b c, andb b c = andb c b.
Proof.
intros b c. destruct b eqn:Eb.
{ destruct c eqn:Ec.
{ reflexivity. }
{ reflexivity. } }
{ destruct c eqn:Ec.
{ reflexivity. }
{ reflexivity. } }
Qed.
參考書目練習答案
basic部分
- EX5:standard (plus_id_exercise)
Theorem plus_id_exercise : forall n m o : nat,
n = m -> m = o -> n + m = m + o.
Proof.
intros n m o.
intros H1 H2.
rewrite->H1.
rewrite<-H2.
reflexivity. Qed.
Theorem mult_0_plus : forall n m : nat,
(0 + n) * m = n * m.
Proof.
intros n m.
rewrite -> plus_O_n.
reflexivity. Qed.
//其中第二個證明展示了用已知結論重寫待證明的式子
- EX6:standard (mult_S_1)
Theorem mult_S_1 : forall n m : nat,
m = S n -> m * (1 + n) = m * m.
Proof.
intros n m.
intros H.
rewrite->H.
reflexivity.
Qed.
注意這裏不能用<-,因爲無法找到需要被代替的S n
- EX7:standard (andb_true_elim2)
Theorem andb_true_elim2 : forall b c : bool,
andb b c = true -> c = true.
Proof.
intros b c.
destruct b.
-destruct c.
+ simpl.
intros H.
reflexivity.
+ simpl.
intros H.
rewrite->H.
reflexivity.
-destruct c.
+ simpl. intros H.
reflexivity.
+ simpl.
intros H.
rewrite->H.
reflexivity.
Qed.
有幾步不太好想,關鍵是把明確什麼時候引入前提,什麼時候結論爲真不需要證明,什麼時候結論爲假需要說明前提也爲假(利用替換)
- EX8:standard (zero_nbeq_plus_1)
Fixpoint eqb (n m:nat):bool:=
match n with
|O=>match m with
|O=>true
|S m'=>false
end
|S n'=>match m with
|O=>false
|S m'=>(eqb n' m')
end
end.
Theorem zero_nbeq_plus_1 : forall n : nat,
eqb 0 (n + 1) = false.
Proof.
intros n.
destruct n as [|n'].
- reflexivity.
- reflexivity.
Qed.
- More exercise答案
1 indentity_fn_applied_twice
Theorem identity_fn_applied_twice :
forall(f : bool -> bool),(forall(x : bool), f x = x) ->
forall(b : bool), f (f b) = b.
Proof.
intros f.
destruct b.(*如果不將x進行析構和引入,x代表任意數,可以直接代換*)
- rewrite H. rewrite H. reflexivity.
- rewrite H. rewrite H. reflexivity. Qed.
2 (negation_fn_applied_twice)
Theorem negation_fn_applied_twice:
forall (f: bool->bool),(forall x:bool,f x=negb x)->forall b,f(f b)=b.
Proof.
intros f.
destruct b.
- rewrite H. rewrite H. reflexivity.
- rewrite H. rewrite H. reflexivity.
Qed.
3 andb_eq_orb
Theorem andb_eq_orb :
forall (b c : bool),
(andb b c = orb b c) ->
b = c.
Proof.
intros b.
destruct b.
-simpl. intros c. intros H. rewrite->H. reflexivity.
-simpl. intros c. intros H. rewrite->H. reflexivity.
Qed.
這裏沒有選擇將可能的結果全部析構,而是析構一個變量後直接將式子化簡(不要小看simpl的化簡能力)