HoareAsLogic作为一种逻辑的霍尔逻辑
Set Warnings "-deprecated-hint-without-locality,-deprecated-hint-without-locality,-parsing".
From PLF Require Import Maps.
From PLF Require Import Hoare.
Hint Constructors ceval : core.
From PLF Require Import Maps.
From PLF Require Import Hoare.
Hint Constructors ceval : core.
霍尔逻辑与模型论
Pre: X = 0
Com: X := X + 1
Post: X = 1 是一个霍尔三元组,下面这个也是:
Pre: X = 0
Com: skip
Post: X = 1 两者有一个重要区别。前者表达了关于 Imp 程序如何执行的事实; 后者没有。
Definition valid (P : Assertion) (c : com) (Q : Assertion) : Prop :=
∀ st st',
st =[ c ]=> st' →
P st →
Q st'.
∀ st st',
st =[ c ]=> st' →
P st →
Q st'.
这种【成立】概念基于 Imp 程序如何执行的底层模型。
该模型本身基于状态。因此,
Pre: X = 0
Com: X := X + 1
Post: X = 1 是【成立】的,因为从任何 X 为 0 的状态出发执行 X := X + 1, 我们都保证到达一个 X 为 1 的状态。但是,
Pre: X = 0
Com: skip
Post: X = 1 是【不成立】的,因为从任何 X 为 0 的状态出发, 我们都保证不会改变状态,所以 X 不可能是 1。
到目前为止,我们在霍尔三元组的语法(写作 {{P}} c {{Q}})
与它的成立性(由 valid 表达)之间做了双关。本质上,我们说
该语法的语义含义就是 valid 返回的命题。这种赋予语法对象
语义含义的方式,属于数学逻辑中称为【模型论】的分支。
我们通过模型论研究霍尔逻辑。这条路径引导我们:
用同一套基于状态的模型陈述证明规则,并在其中证明程序正确性。
不过还有另一种方式。它在霍尔逻辑中可以说更为常见。
接下来我们转向它。
不用状态和求值作为推理的基础,我们转而以 Hoare
中的证明规则为基础。那些证明规则给出了一组公理与推理规则,
它们本身就构成一种逻辑。我们在此重述如下:
---------------- (hoare_skip)
{{P}} skip {{P}}
----------------------------- (hoare_asgn)
{{Q [X ⊢> a]}} X := a {{Q}}
{{P}} c1 {{Q}}
{{Q}} c2 {{R}}
------------------ (hoare_seq)
{{P}} c1; c2 {{R}}
{{P ∧ b}} c1 {{Q}}
{{P ∧ ¬ b}} c2 {{Q}}
------------------------------------ (hoare_if)
{{P}} if b then c1 else c2 end {{Q}}
{{P ∧ b}} c {{P}}
----------------------------- (hoare_while)
{{P} while b do c end {{P ∧ ¬b}}
{{P'}} c {{Q'}}
P ->> P'
Q' ->> Q
----------------------------- (hoare_consequence)
{{P}} c {{Q}}
把那些规则中的霍尔三元组,读作除了规则所赋予的含义之外
别无意义的东西。忘掉状态和求值吧。它们只是语法;
规则告诉我们如何以合法的方式操纵它们。
透过这个新视角,三元组 {{X = 0}} X := X + 1 {{X = 1}}
是【可导出】的,因为我们可以用这些规则构造出一棵证明树:
--------------------------- (hoare_asgn)
X=0 ->> X+1=1 {{X+1=1}} X := X+1 {{X=1}}
------------------------------------------------------- (hoare_consequence)
{{X=0}} X := X+1 {{X=1}}
每一步要么用了一条规则,要么诉诸关于【断言|Assertion】
的推理。后者不涉及霍尔三元组。(注意我们略去了上面
hoare_consequence 的平凡部分,即 X=1 ->> X=1。这纯粹是
因为横向空间限制:一行塞不下那么多字符,页面又得保持可读。
如果你愿意,可以把它想成改用了 hoare_consequence_pre。)
另一方面,{{X = 0}} skip {{X = 1}} 【不可】导出,
因为没有任何办法应用这些规则,构造出以该三元组为根的
证明树。
这种方式不基于模型。它基于三元组如何用于构造证明树,
由此赋予三元组含义。这是赋予语法对象语义含义的另一种方式,
属于数学逻辑中称为【证明论|Proof Theory】的分支。
本章余下部分的目标:用证明论形式化霍尔逻辑,
然后证明模型论与证明论两种形式化彼此一致。
为了形式化霍尔三元组的【可导出性|Derivability】,我们引入
归纳类型 derivable,它描述用霍尔规则构成的合法证明树。
Pre: X = 0
Com: X := X + 1
Post: X = 1 是【成立】的,因为从任何 X 为 0 的状态出发执行 X := X + 1, 我们都保证到达一个 X 为 1 的状态。但是,
Pre: X = 0
Com: skip
Post: X = 1 是【不成立】的,因为从任何 X 为 0 的状态出发, 我们都保证不会改变状态,所以 X 不可能是 1。
霍尔逻辑与证明论
---------------- (hoare_skip)
{{P}} skip {{P}}
----------------------------- (hoare_asgn)
{{Q [X ⊢> a]}} X := a {{Q}}
{{P}} c1 {{Q}}
{{Q}} c2 {{R}}
------------------ (hoare_seq)
{{P}} c1; c2 {{R}}
{{P ∧ b}} c1 {{Q}}
{{P ∧ ¬ b}} c2 {{Q}}
------------------------------------ (hoare_if)
{{P}} if b then c1 else c2 end {{Q}}
{{P ∧ b}} c {{P}}
----------------------------- (hoare_while)
{{P} while b do c end {{P ∧ ¬b}}
{{P'}} c {{Q'}}
P ->> P'
Q' ->> Q
----------------------------- (hoare_consequence)
{{P}} c {{Q}}
--------------------------- (hoare_asgn)
X=0 ->> X+1=1 {{X+1=1}} X := X+1 {{X=1}}
------------------------------------------------------- (hoare_consequence)
{{X=0}} X := X+1 {{X=1}}
可导出性
Inductive derivable : Assertion → com → Assertion → Type :=
| H_Skip : ∀ P,
derivable P <{skip}> P
| H_Asgn : ∀ Q V a,
derivable ({{Q [V ⊢> a]}}) <{V := a}> Q
| H_Seq : ∀ P c Q d R,
derivable Q d R → derivable P c Q → derivable P <{c;d}> R
| H_If : ∀ P Q b c1 c2,
derivable (fun st ⇒ P st ∧ bassertion b st) c1 Q →
derivable (fun st ⇒ P st ∧ ~(bassertion b st)) c2 Q →
derivable P <{if b then c1 else c2 end}> Q
| H_While : ∀ P b c,
derivable (fun st ⇒ P st ∧ bassertion b st) c P →
derivable P <{while b do c end}> (fun st ⇒ P st ∧ ¬ (bassertion b st))
| H_Consequence : ∀ (P Q P' Q' : Assertion) c,
derivable P' c Q' →
(∀ st, P st → P' st) →
(∀ st, Q' st → Q st) →
derivable P c Q.
| H_Skip : ∀ P,
derivable P <{skip}> P
| H_Asgn : ∀ Q V a,
derivable ({{Q [V ⊢> a]}}) <{V := a}> Q
| H_Seq : ∀ P c Q d R,
derivable Q d R → derivable P c Q → derivable P <{c;d}> R
| H_If : ∀ P Q b c1 c2,
derivable (fun st ⇒ P st ∧ bassertion b st) c1 Q →
derivable (fun st ⇒ P st ∧ ~(bassertion b st)) c2 Q →
derivable P <{if b then c1 else c2 end}> Q
| H_While : ∀ P b c,
derivable (fun st ⇒ P st ∧ bassertion b st) c P →
derivable P <{while b do c end}> (fun st ⇒ P st ∧ ¬ (bassertion b st))
| H_Consequence : ∀ (P Q P' Q' : Assertion) c,
derivable P' c Q' →
(∀ st, P st → P' st) →
(∀ st, Q' st → Q st) →
derivable P c Q.
我们不必包含对应 hoare_consequence_pre 或
hoare_consequence_post 的公理,因为它们可以很容易地
由 H_Consequence 证明。
Lemma H_Consequence_pre : ∀ (P Q P': Assertion) c,
derivable P' c Q →
(∀ st, P st → P' st) →
derivable P c Q.
Lemma H_Consequence_post : ∀ (P Q Q' : Assertion) c,
derivable P c Q' →
(∀ st, Q' st → Q st) →
derivable P c Q.
derivable P' c Q →
(∀ st, P st → P' st) →
derivable P c Q.
Lemma H_Consequence_post : ∀ (P Q Q' : Assertion) c,
derivable P c Q' →
(∀ st, Q' st → Q st) →
derivable P c Q.
Example sample_proof :
derivable
({{ $(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.
- apply H_Asgn.
Qed.
derivable
({{ $(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.
- apply H_Asgn.
Qed.
你可以看到证明脚本的结构如何反映证明树的结构:
根部用的是序列规则;叶子处用的是赋值规则。
表明任何后置条件为 True 的霍尔三元组都是可导出的。
对 c 施行归纳。
练习: 3 stars,标准 (provable_true_post)
可靠性与完备性
- 模型论方式用 valid 刻画霍尔三元组何时在一个基于状态的
模型中成立。
- 证明论方式用 derivable 刻画霍尔三元组何时作为证明树的 末端是可导出的。
- 一种逻辑是【可靠】的,如果一切可导出的都成立。
- 一种逻辑是【完备】的,如果一切成立的都可导出。
练习: 3 stars,标准 (hoare_sound)
Definition wp (c:com) (Q:Assertion) : Assertion :=
fun s ⇒ ∀ s', s =[ c ]=> s' → Q s'.
Hint Unfold wp : core.
fun s ⇒ ∀ s', s =[ c ]=> s' → Q s'.
Hint Unfold wp : core.
下面两个定理表明,关于 wp 的两种想法是一回事。
Theorem wp_is_precondition : ∀ c Q,
{{$(wp c Q)}} c {{Q}}.
Proof. auto. Qed.
Theorem wp_is_weakest : ∀ c Q P',
{{P'}} c {{Q}} →
P' ->> (wp c Q).
Proof. eauto. Qed.
{{$(wp c Q)}} c {{Q}}.
Proof. auto. Qed.
Theorem wp_is_weakest : ∀ c Q P',
{{P'}} c {{Q}} →
P' ->> (wp c Q).
Proof. eauto. Qed.
最弱前置条件很有用。它让我们能识别出否则需要巧妙心思
才能得到的断言。接下来的两个引理展示了这一点。
如果我们有一个序列 c1; c2,却没有 c1 与 c2 之间
应当成立什么的中间断言,怎么办?没问题。证明 wp c2 Q
足以充当这样的断言。
练习: 1 star,标准 (wp_seq)
Lemma wp_seq : ∀ P Q c1 c2,
derivable P c1 (wp c2 Q) → derivable (wp c2 Q) c2 Q → derivable P <{c1; c2}> Q.
Proof.
(* 在这里填写 *) Admitted.
☐
derivable P c1 (wp c2 Q) → derivable (wp c2 Q) c2 Q → derivable P <{c1; c2}> Q.
Proof.
(* 在这里填写 *) Admitted.
☐
练习: 2 stars,标准 (wp_invariant)
Lemma wp_invariant : ∀ b c Q,
valid ({{$(wp <{while b do c end}> Q) ∧ b}}) c (wp <{while b do c end}> Q).
Proof.
(* 在这里填写 *) Admitted.
☐
valid ({{$(wp <{while b do c end}> Q) ∧ b}}) c (wp <{while b do c end}> Q).
Proof.
(* 在这里填写 *) Admitted.
☐
练习: 4 stars,标准 (hoare_complete)
Theorem hoare_complete: ∀ P c Q,
valid P c Q → derivable P c Q.
Proof.
unfold valid. intros P c. generalize dependent P.
induction c; intros P Q HT.
(* 在这里填写 *) Admitted.
☐
valid P c Q → derivable P c Q.
Proof.
unfold valid. intros P c. generalize dependent P.
induction c; intros P Q HT.
(* 在这里填写 *) Admitted.
☐
后记:可判定性
(* 2026-11-26 15:55 *)
