HoareAsLogic证明论霍尔逻辑
Inductive hoare_proof : Assertion → com → Assertion → Type :=
| H_Skip : ∀ P,
hoare_proof P (SKIP) P
| H_Asgn : ∀ Q V a,
hoare_proof (Q [V ⊢> a]) (V ::= a) Q
| H_Seq : ∀ P c Q d R,
hoare_proof P c Q → hoare_proof Q d R → hoare_proof P (c;;d) R
| H_If : ∀ P Q b c1 c2,
hoare_proof (fun st ⇒ P st ∧ bassn b st) c1 Q →
hoare_proof (fun st ⇒ P st ∧ ~(bassn b st)) c2 Q →
hoare_proof P (TEST b THEN c1 ELSE c2 FI) Q
| H_While : ∀ P b c,
hoare_proof (fun st ⇒ P st ∧ bassn b st) c P →
hoare_proof P (WHILE b DO c END) (fun st ⇒ P st ∧ ¬ (bassn b st))
| H_Consequence : ∀ (P Q P' Q' : Assertion) c,
hoare_proof P' c Q' →
(∀ st, P st → P' st) →
(∀ st, Q' st → Q st) →
hoare_proof P c Q.
我们并不需要包含对应 hoare_consequence_pre 或 hoare_consequence_post 的公理,
因为它们可以简单地通过 H_Consequence 被证明。
Lemma H_Consequence_pre : ∀ (P Q P': Assertion) c,
hoare_proof P' c Q →
(∀ st, P st → P' st) →
hoare_proof P c Q.
Lemma H_Consequence_post : ∀ (P Q Q' : Assertion) c,
hoare_proof P c Q' →
(∀ st, Q' st → Q st) →
hoare_proof P c Q.
作为一个例子,让我们构造一个证明对象,来表示这个霍尔三元组的一个导出式
{{(X=3) [X ⊢> X + 2] [X ⊢> X + 1]}}
X::=X+1 ;; X::=X+2
{{X=3}}.
我们可以让 Coq 的策略来帮助我们构造这个证明对象。
{{(X=3) [X ⊢> X + 2] [X ⊢> X + 1]}}
X::=X+1 ;; X::=X+2
{{X=3}}.
Example sample_proof :
hoare_proof
((fun st:state ⇒ st X = 3) [X ⊢> X + 2] [X ⊢> X + 1])
(X ::= X + 1;; X ::= X + 2)
(fun st:state ⇒ st X = 3).
Proof.
eapply H_Seq; apply H_Asgn.
Qed.
Print sample_proof.
(*
====>
H_Seq
(((fun st : state => st X = 3) X ⊢> X + 2) X ⊢> X + 1)
(X ::= X + 1)
((fun st : state => st X = 3) X ⊢> X + 2)
(X ::= X + 2)
(fun st : state => st X = 3)
(H_Asgn
((fun st : state => st X = 3) X ⊢> X + 2)
X (X + 1))
(H_Asgn
(fun st : state => st X = 3)
X (X + 2))
*)
性质
练习:2 星, standard (hoare_proof_sound)
请证明由 hoare_proof 构造的推导式对应于有效的霍尔三元组。换言之,就是 hoare_proof 推导式是'可靠'的。提示:我们已经将Hoare 中所有独立的证明规则证明成了定理 hoare_skip、hoare_asgn 等等。 请发挥它们的优势。Theorem hoare_proof_sound : ∀ P c Q,
hoare_proof P c Q → {{P}} c {{Q}}.
Proof.
(* 请在此处解答 *) Admitted.
☐
Theorem H_Post_True_deriv:
∀ c P, hoare_proof P c (fun _ ⇒ True).
Proof.
intro c.
induction c; intro P.
- (* SKIP *)
eapply H_Consequence.
apply H_Skip.
intros. apply H.
(* Proof of True *)
intros. apply I.
- (* ::= *)
eapply H_Consequence_pre.
apply H_Asgn.
intros. apply I.
- (* ;; *)
eapply H_Consequence_pre.
eapply H_Seq.
apply (IHc1 (fun _ ⇒ True)).
apply IHc2.
intros. apply I.
- (* TEST *)
apply H_Consequence_pre with (fun _ ⇒ True).
apply H_If.
apply IHc1.
apply IHc2.
intros. apply I.
- (* WHILE *)
eapply H_Consequence.
eapply H_While.
eapply IHc.
intros; apply I.
intros; apply I.
Qed.
类似地,我们可以说明对于任意的 c 和 Q,{{False}} c {{Q}} 是可证明的。
Lemma False_and_P_imp: ∀ P Q,
False ∧ P → Q.
Proof.
intros P Q [CONTRA HP].
destruct CONTRA.
Qed.
Tactic Notation "pre_false_helper" constr(CONSTR) :=
eapply H_Consequence_pre;
[eapply CONSTR | intros ? CONTRA; destruct CONTRA].
Theorem H_Pre_False_deriv:
∀ c Q, hoare_proof (fun _ ⇒ False) c Q.
Proof.
intros c.
induction c; intro Q.
- (* SKIP *) pre_false_helper H_Skip.
- (* ::= *) pre_false_helper H_Asgn.
- (* ;; *) pre_false_helper H_Seq. apply IHc1. apply IHc2.
- (* TEST *)
apply H_If; eapply H_Consequence_pre.
apply IHc1. intro. eapply False_and_P_imp.
apply IHc2. intro. eapply False_and_P_imp.
- (* WHILE *)
eapply H_Consequence_post.
eapply H_While.
eapply H_Consequence_pre.
apply IHc.
intro. eapply False_and_P_imp.
intro. simpl. eapply False_and_P_imp.
Qed.
最后,我们可以说明 hoare_proof 公理集合足够用来证明关于任何(部分)正确性的
事实。更准确地说,任何我们能够证明的语义霍尔三元组,都能够通过这些公理证明。
这样的公理集合被称为'相对完备的(relatively complete)'。也就是说,这些公理
'相对于'我们可以用底层的断言语言证明的事实来说是完备的。
如果该语言的证明中有所跳跃(gap),那么我们应该责怪它而非霍尔逻辑公理。
我们的证明的灵感来自于:
https://www.ps.uni-saarland.de/courses/sem-ws11/script/Hoare.html
为了完成这个证明,我们需要使用一种被称为'最弱前置条件(Weakest Precondition)'
的技术装置来创造一些中间的断言(它也在 Hoare2 有所讨论)。
给定一个命令c和一个想要的后置条件断言 Q ,最弱前置条件 wp c Q 是一个断言
P,使得 {{P}} c {{Q}} 成立,并且对于任意其他断言 P',如果 {{P}} c {{Q}} 成立,那么
P' → P。我们可以更加直接地将其定义为:
Theorem wp_is_weakest : ∀ c Q P',
{{P'}} c {{Q}} → ∀ st, P' st → wp c Q st.
Proof. (* 请在此处解答 *) Admitted.
☐
练习:2 星, standard (wp_invariant)
Lemma wp_invariant : ∀ b c Inv Q,
Inv = wp (WHILE b DO c END) Q
→ {{ fun st ⇒ Inv st ∧ bassn b st }} c {{ Inv }}.
Proof. (* 请在此处解答 *) Admitted.
☐
Lemma bassn_eval_false : ∀ b st, ¬ bassn b st → beval st b = false.
Proof.
intros b st H. unfold bassn in H. destruct (beval st b).
exfalso. apply H. reflexivity.
reflexivity.
Qed.
Theorem hoare_proof_complete: ∀ P c Q,
{{P}} c {{Q}} → hoare_proof P c Q.
Proof.
intros P c. generalize dependent P.
induction c; intros P Q HT.
- (* SKIP *)
eapply H_Consequence.
eapply H_Skip.
intros. eassumption.
intro st. apply HT. apply E_Skip.
- (* ::= *)
eapply H_Consequence.
eapply H_Asgn.
intro st. apply HT. constructor. reflexivity.
intros; assumption.
- (* ;; *)
apply H_Seq with (wp c2 Q).
eapply IHc1.
intros st st' E1 H. unfold wp. intros st'' E2.
eapply HT. econstructor; eassumption. assumption.
eapply IHc2. intros st st' E1 H. apply H; assumption.
(* 请在此处解答 *) Admitted.
☐
(* 2022-03-14 05:28:22 (UTC+00) *)