Hoare2霍尔逻辑,第二部分
Set Warnings "-notation-overridden".
Ltac intuition_solver ::= auto.
From Stdlib Require Import Strings.String.
From PLF Require Import Maps.
From Stdlib Require Import Bool.
From Stdlib Require Import Arith.
From Stdlib Require Import EqNat.
From Stdlib Require Import PeanoNat. Import Nat.
From Stdlib Require Import Lia.
From PLF Require Export Imp.
From PLF Require Import Hoare.
Definition FILL_IN_HERE := <{True}>.
Ltac intuition_solver ::= auto.
From Stdlib Require Import Strings.String.
From PLF Require Import Maps.
From Stdlib Require Import Bool.
From Stdlib Require Import Arith.
From Stdlib Require Import EqNat.
From Stdlib Require Import PeanoNat. Import Nat.
From Stdlib Require Import Lia.
From PLF Require Export Imp.
From PLF Require Import Hoare.
Definition FILL_IN_HERE := <{True}>.
装饰程序
X := m;
Z := p;
while X ≠ 0 do
Z := Z - 1;
X := X - 1
end 下面是这个程序的一种可能规约,写成霍尔三元组的形式:
{{ True }}
X := m;
Z := p;
while X ≠ 0 do
Z := Z - 1;
X := X - 1
end
{{ Z = p - m }} (注意【参数|Parameter】m 和 p,它们代表固定但任意的数。 形式化地说,它们不过是类型为 nat 的 Rocq 变量。)
{{ True }} ->>
{{ m = m }}
X := m
{{ X = m }} ->>
{{ X = m ∧ p = p }};
Z := p;
{{ X = m ∧ Z = p }} ->>
{{ Z - X = p - m }}
while X ≠ 0 do
{{ Z - X = p - m ∧ X ≠ 0 }} ->>
{{ (Z - 1) - (X - 1) = p - m }}
Z := Z - 1
{{ Z - (X - 1) = p - m }};
X := X - 1
{{ Z - X = p - m }}
end
{{ Z - X = p - m ∧ ¬ (X ≠ 0) }} ->>
{{ Z = p - m }}
示例:交换
X := X + Y;
Y := X - Y;
X := X - Y 我们可以给出一个装饰形式的证明,表明这个程序是正确的, 即它确实交换了 X 和 Y,如下所示。
(1) {{ X = m ∧ Y = n }} ->>
(2) {{ (X + Y) - ((X + Y) - Y) = n ∧ (X + Y) - Y = m }}
X := X + Y
(3) {{ X - (X - Y) = n ∧ X - Y = m }};
Y := X - Y
(4) {{ X - Y = n ∧ Y = m }};
X := X - Y
(5) {{ X = n ∧ Y = m }}
- 我们从无装饰的程序(未编号的行)开始。
- 我们加上规约,即最外层的【前置条件|Precondition】(1)
和【后置条件|Postcondition】(5)。在前置条件中,我们用参数
m 和 n 记住变量 X 和 Y 的初始值,
以便在后置条件 (5) 中引用它们。
- 我们从 (5) 出发机械地倒推,一直做到 (2)。
每一步中,我们通过把被赋值的变量替换成赋值语句的
右边部分,由赋值的后置条件得到其前置条件。
例如,我们在 (5) 中把 X 替换为 X - Y 得到 (4),
再在 (4) 中把 Y 替换为 X - Y 得到 (3)。
- 最后,我们做一点初等代数,验证 (1) 在逻辑上蕴含 (2), 即从 (1) 到 (2) 是【后果规则|Rule of Consequence】 的一次合法使用。
示例:简单条件
(1) {{ True }}
if X ≤ Y then
(2) {{ True ∧ X ≤ Y }} ->>
(3) {{ (Y - X) + X = Y ∨ (Y - X) + Y = X }}
Z := Y - X
(4) {{ Z + X = Y ∨ Z + Y = X }}
else
(5) {{ True ∧ ~(X ≤ Y) }} ->>
(6) {{ (X - Y) + X = Y ∨ (X - Y) + Y = X }}
Z := X - Y
(7) {{ Z + X = Y ∨ Z + Y = X }}
end
(8) {{ Z + X = Y ∨ Z + Y = X }} 这些装饰可以按如下方式构造:
- 我们从最外层的前置条件 (1) 和后置条件 (8) 开始。
- 按照 hoare_if 规则规定的格式,我们把后置条件 (8)
复制到 (4) 和 (7)。我们把前置条件 (1) 与条件的【守卫|Guard】合取
得到 (2);把 (1) 与条件守卫的否定合取得到 (5)。
- 为了使用【赋值规则|Assignment Rule】得到 (3),我们在 (4) 中把 Z
替换为 Y - X;为了得到 (6),我们在 (7) 中把 Z
替换为 X - Y。
- 最后,我们验证 (2) 蕴含 (3) 以及 (5) 蕴含 (6)。 这两条蕴含都关键性地依赖于守卫给出的 X 与 Y 的大小关系。 例如,由 X ≤ Y 可知,从 Y 中减去 X 再加回 X 得到 Y。这正是 (3) 的第一个析取支所要求的。 类似地,由 ¬ (X ≤ Y) 可知,从 X 中减去 Y 再加回 Y 得到 X。这正是 (6) 的第二个析取支所需要的。 注意 n - m + m = n 并【不】对任意自然数 n 和 m 成立 (例如 3 - 5 + 5 = 5)。
练习: 2 stars,标准,可选 (if_minus_plus_reloaded)
注意:虽然这个练习标为可选,但它是下面(非可选的) if_minus_plus_correct 练习的绝佳热身!
(*
{{ True }}
if X <= Y then
{{ }} ->>
{{ }}
Z := Y - X
{{ }}
else
{{ }} ->>
{{ }}
Y := X + Z
{{ }}
end
{{ Y = X + Z }}
*)
{{ True }}
if X <= Y then
{{ }} ->>
{{ }}
Z := Y - X
{{ }}
else
{{ }} ->>
{{ }}
Y := X + Z
{{ }}
end
{{ Y = X + Z }}
*)
简要说明每次 ->> 的使用。
☐
示例:递减至零
(1) {{ True }}
while X ≠ 0 do
(2) {{ True ∧ X ≠ 0 }} ->>
(3) {{ True }}
X := X - 1
(4) {{ True }}
end
(5) {{ True ∧ ~(X ≠ 0) }} ->>
(6) {{ X = 0 }} 装饰可以按如下方式构造:
- 从最外层的前置条件 (1) 和后置条件 (6) 开始。
- 按照 hoare_while 规则规定的格式,我们把 (1)
复制到 (4)。我们把 (1) 与守卫合取得到 (2),
也把 (1) 与守卫的否定合取得到 (5)。
- 由于最终的后置条件 (6) 与 (5) 在语法上不匹配,
我们在它们之间加上一条蕴含。
- 对断言 (4) 使用赋值规则,平凡地替换即可得到断言 (3)。
- 我们在 (2) 和 (3) 之间加上这条蕴含。
示例:除法
X := m;
Y := 0;
while n ≤ X do
X := X - n;
Y := Y + 1
end; 如果我们把 m 和 n 换成具体的数并执行程序, 它将终止,此时变量 X 是 m 除以 n 的余数, Y 是商。
(1) {{ True }} ->>
(2) {{ n × 0 + m = m }}
X := m;
(3) {{ n × 0 + X = m }}
Y := 0;
(4) {{ n × Y + X = m }}
while n ≤ X do
(5) {{ n × Y + X = m ∧ n ≤ X }} ->>
(6) {{ n × (Y + 1) + (X - n) = m }}
X := X - n;
(7) {{ n × (Y + 1) + X = m }}
Y := Y + 1
(8) {{ n × Y + X = m }}
end
(9) {{ n × Y + X = m ∧ ¬ (n ≤ X) }} ->>
(10) {{ n × Y + X = m ∧ X < n }} 断言 (4)、(5)、(8) 和 (9) 由循环不变式和循环的守卫机械地导出。 断言 (8)、(7) 和 (6) 是从 (8) 到 (6) 倒着使用赋值规则导出的。 断言 (4)、(3) 和 (2) 又是赋值规则的反向应用。
- (1) ->> (2):由代数,平凡。
- (5) ->> (6):由于 n ≤ X,我们有保证 (6) 中的减法 不会被截断为零。因此可以把 (6) 改写为 n × Y + n + X - n 并消去 n,结果正是 (5) 的左合取支。
- (9) ->> (10):若 ¬ (n ≤ X) 则 X < n。 由初等代数立得。
从装饰程序到形式化证明
Definition reduce_to_zero : com :=
<{ while X ≠ 0 do
X := X - 1
end }>.
Theorem reduce_to_zero_correct' :
{{True}}
reduce_to_zero
{{X = 0}}.
Proof.
unfold reduce_to_zero.
(* 首先需要变换后置条件,
以便 hoare_while 能够适用。 *)
eapply hoare_consequence_post.
- apply hoare_while.
+ (* 循环体保持循环不变式 *)
(* 调整前置条件使 hoare_asgn 适用 *)
eapply hoare_consequence_pre.
× apply hoare_asgn.
× (* 证明平凡的蕴含 (2) ->> (3) *)
unfold assertion_sub, "->>". simpl. intros.
exact I.
- (* 循环不变式与守卫的否定共同蕴含后置条件 *)
intros st [Inv GuardFalse].
simpl in ×.
destruct (eqb_spec (st X) 0); lia.
Qed.
<{ while X ≠ 0 do
X := X - 1
end }>.
Theorem reduce_to_zero_correct' :
{{True}}
reduce_to_zero
{{X = 0}}.
Proof.
unfold reduce_to_zero.
(* 首先需要变换后置条件,
以便 hoare_while 能够适用。 *)
eapply hoare_consequence_post.
- apply hoare_while.
+ (* 循环体保持循环不变式 *)
(* 调整前置条件使 hoare_asgn 适用 *)
eapply hoare_consequence_pre.
× apply hoare_asgn.
× (* 证明平凡的蕴含 (2) ->> (3) *)
unfold assertion_sub, "->>". simpl. intros.
exact I.
- (* 循环不变式与守卫的否定共同蕴含后置条件 *)
intros st [Inv GuardFalse].
simpl in ×.
destruct (eqb_spec (st X) 0); lia.
Qed.
在 Hoare 中,我们引入了一系列名为
assertion_auto 的策略来自动化涉及断言的证明。
下面的声明引入了一个更精细的策略,
它将在本章余下部分帮助我们证明断言。
你无需理解其细节。简要地说:它反复用 split
把所有合取拆成单独的子目标。
然后尝试使用若干关于布尔值和(不)等式的定理,
再用 eauto 和 lia 尽可能多地完成子目标。
verify_assertion 处理完之后剩下的,
应当只是证明中「有意思的部分」
(运气好的话,可能什么也不剩!)。
Ltac verify_assertion :=
repeat split;
simpl;
unfold assert_implies;
unfold bassertion in *; unfold beval in *; unfold aeval in *;
unfold assertion_sub; intros;
repeat (simpl in *;
rewrite t_update_eq ||
(try rewrite t_update_neq;
[| (intro X; inversion X; fail)]));
simpl in *;
repeat match goal with [H : _ ∧ _ ⊢ _] ⇒
destruct H end;
repeat match goal with
| ⊢ context [ ?X =? ?Y] ⇒ destruct (eqb_spec X Y)
| ⊢ context [ ?X <? ?Y] ⇒ destruct (ltb_spec0 X Y)
| ⊢ context [ ?X <=? ?Y] ⇒ destruct (leb_spec0 X Y)
| H: context [ ?X =? ?Y] ⊢ _ ⇒ destruct (eqb_spec X Y)
| H: context [ ?X <? ?Y] ⊢ _ ⇒ destruct (ltb_spec0 X Y)
| H: context [ ?X <=? ?Y] ⊢ _ ⇒ destruct (leb_spec0 X Y)
end;
subst;
try eauto;
try lia.
repeat split;
simpl;
unfold assert_implies;
unfold bassertion in *; unfold beval in *; unfold aeval in *;
unfold assertion_sub; intros;
repeat (simpl in *;
rewrite t_update_eq ||
(try rewrite t_update_neq;
[| (intro X; inversion X; fail)]));
simpl in *;
repeat match goal with [H : _ ∧ _ ⊢ _] ⇒
destruct H end;
repeat match goal with
| ⊢ context [ ?X =? ?Y] ⇒ destruct (eqb_spec X Y)
| ⊢ context [ ?X <? ?Y] ⇒ destruct (ltb_spec0 X Y)
| ⊢ context [ ?X <=? ?Y] ⇒ destruct (leb_spec0 X Y)
| H: context [ ?X =? ?Y] ⊢ _ ⇒ destruct (eqb_spec X Y)
| H: context [ ?X <? ?Y] ⊢ _ ⇒ destruct (ltb_spec0 X Y)
| H: context [ ?X <=? ?Y] ⊢ _ ⇒ destruct (leb_spec0 X Y)
end;
subst;
try eauto;
try lia.
这让验证 reduce_to_zero 变得相当容易:
Theorem reduce_to_zero_correct''' :
{{True}}
reduce_to_zero
{{X = 0}}.
Proof.
unfold reduce_to_zero.
eapply hoare_consequence_post.
- apply hoare_while.
+ eapply hoare_consequence_pre.
× apply hoare_asgn.
× verify_assertion.
- verify_assertion.
Qed.
{{True}}
reduce_to_zero
{{X = 0}}.
Proof.
unfold reduce_to_zero.
eapply hoare_consequence_post.
- apply hoare_while.
+ eapply hoare_consequence_pre.
× apply hoare_asgn.
× verify_assertion.
- verify_assertion.
Qed.
这个示例表明,从装饰程序读出形式化证明的主要成分
在概念上是直截了当的。
事实上,这个过程如此直截了当,以至于可以自动化,
我们接下来就会看到。
形式化装饰程序
语法
Module DComFirstTry.
Inductive dcom : Type :=
| DCSkip (P : Assertion)
(* {{ P }} skip {{ P }} *)
| DCSeq (P : Assertion) (d1 : dcom) (Q : Assertion)
(d2 : dcom) (R : Assertion)
(* {{ P }} d1 {{Q}}; d2 {{ R }} *)
| DCAsgn (X : string) (a : aexp) (Q : Assertion)
(* etc. *)
| DCIf (P : Assertion) (b : bexp) (P1 : Assertion) (d1 : dcom)
(P2 : Assertion) (d2 : dcom) (Q : Assertion)
| DCWhile (P : Assertion) (b : bexp)
(P1 : Assertion) (d : dcom) (P2 : Assertion)
(Q : Assertion)
| DCPre (P : Assertion) (d : dcom)
| DCPost (d : dcom) (Q : Assertion).
End DComFirstTry.
Inductive dcom : Type :=
| DCSkip (P : Assertion)
(* {{ P }} skip {{ P }} *)
| DCSeq (P : Assertion) (d1 : dcom) (Q : Assertion)
(d2 : dcom) (R : Assertion)
(* {{ P }} d1 {{Q}}; d2 {{ R }} *)
| DCAsgn (X : string) (a : aexp) (Q : Assertion)
(* etc. *)
| DCIf (P : Assertion) (b : bexp) (P1 : Assertion) (d1 : dcom)
(P2 : Assertion) (d2 : dcom) (Q : Assertion)
| DCWhile (P : Assertion) (b : bexp)
(P1 : Assertion) (d : dcom) (P2 : Assertion)
(Q : Assertion)
| DCPre (P : Assertion) (d : dcom)
| DCPost (d : dcom) (Q : Assertion).
End DComFirstTry.
但这样得到的装饰程序将【非常】冗长,
有大量重复的标注。像 skip;skip 这样的简单程序
会被装饰成这样,
{{P}} ({{P}} skip {{P}}) ; ({{P}} skip {{P}}) {{P}} 每个 skip 前后都有前置和后置条件, 分号前后还有完全相同的前置和后置条件!
换句话说,我们不希望每条命令都既有前置条件又有后置条件。
在两条命令的序列中,这会产生冗余的装饰:
第一条的后置条件很可能与第二条的前置条件相同。
相反,我们的形式化装饰命令语法将尽可能省略前置条件,
只内嵌后置条件。
把这些合在一起,就得到装饰命令的形式化语法:
{{P}} ({{P}} skip {{P}}) ; ({{P}} skip {{P}}) {{P}} 每个 skip 前后都有前置和后置条件, 分号前后还有完全相同的前置和后置条件!
- 例如,skip 命令只用它的后置条件装饰
skip {{ Q }} 前提是前置条件将由别人提供。我们把同样的假设贯穿到其他语法形式中。 每个装饰命令都被认为在自身内部带有自己的后置条件, 而前置条件则从它所处的使用语境中获得。
- 序列 d1 ; d2 不需要额外的装饰。
为什么?因为 d2 内部会有一个后置条件, 它同时也充当 d1;d2 的后置条件。类似地,d1 内部也会有一个后置条件, 它还充当 d2 的【前置条件】。
- 条件 if b then d1 else d2 带有整个语句的后置条件,
以及每个分支的前置条件:
if b then {{ P1 }} d1 else {{ P2 }} d2 end {{ Q }}
- 蕴含 ->> 可以作为装饰加上,或为前置条件……
->> {{ P }} d ……或为后置条件:
d ->> {{ Q }} 前者在等待语境提供的另一个前置条件; 后者则依赖于已经内嵌在 d 中的后置条件。
Inductive dcom : Type :=
| DCSkip (Q : Assertion)
(* skip {{ Q }} *)
| DCSeq (d1 d2 : dcom)
(* d1 ; d2 *)
| DCAsgn (X : string) (a : aexp) (Q : Assertion)
(* X := a {{ Q }} *)
| DCIf (b : bexp) (P1 : Assertion) (d1 : dcom)
(P2 : Assertion) (d2 : dcom) (Q : Assertion)
(* if b then {{ P1 }} d1 else {{ P2 }} d2 end {{ Q }} *)
| DCWhile (b : bexp) (P : Assertion) (d : dcom)
(Q : Assertion)
(* while b do {{ P }} d end {{ Q }} *)
| DCPre (P : Assertion) (d : dcom)
(* ->> {{ P }} d *)
| DCPost (d : dcom) (Q : Assertion)
(* d ->> {{ Q }} *).
| DCSkip (Q : Assertion)
(* skip {{ Q }} *)
| DCSeq (d1 d2 : dcom)
(* d1 ; d2 *)
| DCAsgn (X : string) (a : aexp) (Q : Assertion)
(* X := a {{ Q }} *)
| DCIf (b : bexp) (P1 : Assertion) (d1 : dcom)
(P2 : Assertion) (d2 : dcom) (Q : Assertion)
(* if b then {{ P1 }} d1 else {{ P2 }} d2 end {{ Q }} *)
| DCWhile (b : bexp) (P : Assertion) (d : dcom)
(Q : Assertion)
(* while b do {{ P }} d end {{ Q }} *)
| DCPre (P : Assertion) (d : dcom)
(* ->> {{ P }} d *)
| DCPost (d : dcom) (Q : Assertion)
(* d ->> {{ Q }} *).
为了提供位于装饰程序最顶端的初始前置条件,
我们引入一个新类型 decorated:
一个把 X 递减到 0 的 decorated 程序示例:
Example dec_while : decorated :=
<{
{{ True }}
while X ≠ 0
do
{{ True ∧ (X ≠ 0) }}
X := X - 1
{{ True }}
end
{{ True ∧ X = 0}} ->>
{{ X = 0 }} }>.
<{
{{ True }}
while X ≠ 0
do
{{ True ∧ (X ≠ 0) }}
X := X - 1
{{ True }}
end
{{ True ∧ X = 0}} ->>
{{ X = 0 }} }>.
通过抹掉所有标注,从 dcom 得到 com 是很容易的。
Fixpoint erase (d : dcom) : com :=
match d with
| DCSkip _ ⇒ CSkip
| DCSeq d1 d2 ⇒ CSeq (erase d1) (erase d2)
| DCAsgn X a _ ⇒ CAsgn X a
| DCIf b _ d1 _ d2 _ ⇒ CIf b (erase d1) (erase d2)
| DCWhile b _ d _ ⇒ CWhile b (erase d)
| DCPre _ d ⇒ erase d
| DCPost d _ ⇒ erase d
end.
Definition erase_d (dec : decorated) : com :=
match dec with
| Decorated P d ⇒ erase d
end.
Example erase_while_ex :
erase_d dec_while
= <{while X ≠ 0 do X := X - 1 end}>.
Proof.
unfold dec_while.
reflexivity.
Qed.
match d with
| DCSkip _ ⇒ CSkip
| DCSeq d1 d2 ⇒ CSeq (erase d1) (erase d2)
| DCAsgn X a _ ⇒ CAsgn X a
| DCIf b _ d1 _ d2 _ ⇒ CIf b (erase d1) (erase d2)
| DCWhile b _ d _ ⇒ CWhile b (erase d)
| DCPre _ d ⇒ erase d
| DCPost d _ ⇒ erase d
end.
Definition erase_d (dec : decorated) : com :=
match dec with
| Decorated P d ⇒ erase d
end.
Example erase_while_ex :
erase_d dec_while
= <{while X ≠ 0 do X := X - 1 end}>.
Proof.
unfold dec_while.
reflexivity.
Qed.
从装饰程序中提取前置条件和后置条件也很直接。
Definition precondition_from (dec : decorated) : Assertion :=
match dec with
| Decorated P d ⇒ P
end.
Fixpoint post (d : dcom) : Assertion :=
match d with
| DCSkip P ⇒ P
| DCSeq _ d2 ⇒ post d2
| DCAsgn _ _ Q ⇒ Q
| DCIf _ _ _ _ _ Q ⇒ Q
| DCWhile _ _ _ Q ⇒ Q
| DCPre _ d ⇒ post d
| DCPost _ Q ⇒ Q
end.
Definition postcondition_from (dec : decorated) : Assertion :=
match dec with
| Decorated P d ⇒ post d
end.
Example precondition_from_while : precondition_from dec_while = True.
Proof. reflexivity. Qed.
Example postcondition_from_while : postcondition_from dec_while = {{ X = 0 }}.
Proof. reflexivity. Qed.
match dec with
| Decorated P d ⇒ P
end.
Fixpoint post (d : dcom) : Assertion :=
match d with
| DCSkip P ⇒ P
| DCSeq _ d2 ⇒ post d2
| DCAsgn _ _ Q ⇒ Q
| DCIf _ _ _ _ _ Q ⇒ Q
| DCWhile _ _ _ Q ⇒ Q
| DCPre _ d ⇒ post d
| DCPost _ Q ⇒ Q
end.
Definition postcondition_from (dec : decorated) : Assertion :=
match dec with
| Decorated P d ⇒ post d
end.
Example precondition_from_while : precondition_from dec_while = True.
Proof. reflexivity. Qed.
Example postcondition_from_while : postcondition_from dec_while = {{ X = 0 }}.
Proof. reflexivity. Qed.
于是我们可以把装饰程序的正确性含义表述如下:
Definition outer_triple_valid (dec : decorated) :=
{{$(precondition_from dec)}} erase_d dec {{$(postcondition_from dec)}}.
{{$(precondition_from dec)}} erase_d dec {{$(postcondition_from dec)}}.
例如:
Example dec_while_triple_correct :
outer_triple_valid dec_while
=
{{ True }}
while X ≠ 0 do X := X - 1 end
{{ X = 0 }}.
outer_triple_valid dec_while
=
{{ True }}
while X ≠ 0 do X := X - 1 end
{{ X = 0 }}.
装饰程序的最外层霍尔三元组只是一个 Prop;
因此,要表明它【成立】,我们需要给出这个命题的一个证明。
我们将通过从散布在整个程序中的装饰里
提取【证明义务|Proof Obligation】来做到这一点。
这些义务通常称为【验证条件|Verification Condition】。
它们是必须验证的事实,
用以表明这些装饰在局部是一致的。
由此构成最外层三元组成立性的一个证明。
函数 verification_conditions 接受一个装饰命令 d
和一个前置条件 P,返回一个【命题】。
若能证明该命题,则蕴含三元组
{{P}} erase d {{post d}} 是成立的。
它的做法是遍历 d 并生成一个大合取,其中包含
局部一致性定义如下……
考虑到这一切,我们可以写一个
【验证条件生成器|Verification Condition Generator】。
它接受一个装饰命令并读出一个命题,
表明它的所有装饰都是局部一致的。
形式化地说,由于装饰命令「在等待它的前置条件」,
主 VC 生成器以一个 dcom 加上给定的前置条件作为参数。
提取验证条件
{{P}} erase d {{post d}} 是成立的。
- 每种命令形式的【局部一致性|Local Consistency】检查,以及
- 用 ->> 来弥合装饰命令内部发现的断言 与外部前置条件所施加的断言之间的间隙。 这些使用对应于后果规则的应用。
- d1 与 d2 的顺序组合关于 P 是局部一致的, 若 d1 关于 P 是局部一致的, 且 d2 关于 d1 的后置条件是局部一致的。
- 条件
if b then {{P1}} d1 else {{P2}} d2 end {{Q}} 关于前置条件 P 是局部一致的,若(1) P ∧ b ->> P1(2) P ∧ ¬b ->> P2(3) d1 关于 P1 是局部一致的(4) d2 关于 P2 是局部一致的(5) post d1 ->> Q(6) post d2 ->> Q
- 循环
while b do {{Q}} d end {{R}} 关于前置条件 P 是局部一致的,若:(1) P ->> post d(2) post d ∧ b ->> Q(3) post d ∧ ¬b ->> R(4) d 关于 Q 是局部一致的
Fixpoint verification_conditions (P : Assertion) (d : dcom) : Prop :=
match d with
| DCSkip Q ⇒
(P ->> Q)
| DCSeq d1 d2 ⇒
verification_conditions P d1
∧ verification_conditions (post d1) d2
| DCAsgn X a Q ⇒
P ->> {{ Q [X ⊢> a] }}
| DCIf b P1 d1 P2 d2 Q ⇒
{{ P ∧ b }} ->> P1
∧ {{ P ∧ ¬ b }} ->> P2
∧ (post d1 ->> Q) ∧ (post d2 ->> Q)
∧ verification_conditions P1 d1
∧ verification_conditions P2 d2
| DCWhile b Q d R ⇒
(* (post d) 同时是循环不变式和初始前置条件 *)
(P ->> post d)
∧ {{ $(post d) ∧ b }} ->> Q
∧ {{ $(post d) ∧ ¬ b }} ->> R
∧ verification_conditions Q d
| DCPre P' d ⇒
(P ->> P')
∧ verification_conditions P' d
| DCPost d Q ⇒
verification_conditions P d
∧ (post d ->> Q)
end.
match d with
| DCSkip Q ⇒
(P ->> Q)
| DCSeq d1 d2 ⇒
verification_conditions P d1
∧ verification_conditions (post d1) d2
| DCAsgn X a Q ⇒
P ->> {{ Q [X ⊢> a] }}
| DCIf b P1 d1 P2 d2 Q ⇒
{{ P ∧ b }} ->> P1
∧ {{ P ∧ ¬ b }} ->> P2
∧ (post d1 ->> Q) ∧ (post d2 ->> Q)
∧ verification_conditions P1 d1
∧ verification_conditions P2 d2
| DCWhile b Q d R ⇒
(* (post d) 同时是循环不变式和初始前置条件 *)
(P ->> post d)
∧ {{ $(post d) ∧ b }} ->> Q
∧ {{ $(post d) ∧ ¬ b }} ->> R
∧ verification_conditions Q d
| DCPre P' d ⇒
(P ->> P')
∧ verification_conditions P' d
| DCPost d Q ⇒
verification_conditions P d
∧ (post d ->> Q)
end.
下面的关键定理表明 verification_conditions
正确地完成了它的工作。毫不意外,
霍尔逻辑的每条规则都在证明的某个地方起着关键作用。
Theorem verification_correct : ∀ d P,
verification_conditions P d → {{P}} erase d {{ $(post d) }}.
verification_conditions P d → {{P}} erase d {{ $(post d) }}.
Proof.
induction d; intros; simpl in ×.
- (* Skip *)
eapply hoare_consequence_pre.
+ apply hoare_skip.
+ assumption.
- (* Seq *)
destruct H as [H1 H2].
eapply hoare_seq.
+ apply IHd2. apply H2.
+ apply IHd1. apply H1.
- (* Asgn *)
eapply hoare_consequence_pre.
+ apply hoare_asgn.
+ assumption.
- (* If *)
destruct H as [HPre1 [HPre2 [Hd1 [Hd2 [HThen HElse] ] ] ] ].
apply IHd1 in HThen. clear IHd1.
apply IHd2 in HElse. clear IHd2.
apply hoare_if.
+ eapply hoare_consequence; eauto.
+ eapply hoare_consequence; eauto.
- (* While *)
destruct H as [Hpre [Hbody1 [Hpost1 Hd] ] ].
eapply hoare_consequence; eauto.
apply hoare_while.
eapply hoare_consequence_pre; eauto.
- (* Pre *)
destruct H as [HP Hd].
eapply hoare_consequence_pre; eauto.
- (* Post *)
destruct H as [Hd HQ].
eapply hoare_consequence_post; eauto.
Qed.
induction d; intros; simpl in ×.
- (* Skip *)
eapply hoare_consequence_pre.
+ apply hoare_skip.
+ assumption.
- (* Seq *)
destruct H as [H1 H2].
eapply hoare_seq.
+ apply IHd2. apply H2.
+ apply IHd1. apply H1.
- (* Asgn *)
eapply hoare_consequence_pre.
+ apply hoare_asgn.
+ assumption.
- (* If *)
destruct H as [HPre1 [HPre2 [Hd1 [Hd2 [HThen HElse] ] ] ] ].
apply IHd1 in HThen. clear IHd1.
apply IHd2 in HElse. clear IHd2.
apply hoare_if.
+ eapply hoare_consequence; eauto.
+ eapply hoare_consequence; eauto.
- (* While *)
destruct H as [Hpre [Hbody1 [Hpost1 Hd] ] ].
eapply hoare_consequence; eauto.
apply hoare_while.
eapply hoare_consequence_pre; eauto.
- (* Pre *)
destruct H as [HP Hd].
eapply hoare_consequence_pre; eauto.
- (* Post *)
destruct H as [Hd HQ].
eapply hoare_consequence_post; eauto.
Qed.
现在所有部件都已就位,我们可以定义
验证整个程序意味着什么。
Definition verification_conditions_from
(dec : decorated) : Prop :=
match dec with
| Decorated P d ⇒ verification_conditions P d
end.
(dec : decorated) : Prop :=
match dec with
| Decorated P d ⇒ verification_conditions P d
end.
这就引出了本节的主定理:
Corollary verification_conditions_correct : ∀ dec,
verification_conditions_from dec →
outer_triple_valid dec.
verification_conditions_from dec →
outer_triple_valid dec.
Eval simpl in verification_conditions_from dec_while.
(* ==>
((fun _ : state => True) ->>
(fun _ : state => True)) /\
((fun st : state => True /\ negb (st X =? 0) = true) ->>
(fun st : state => True /\ st X <> 0)) /\
((fun st : state => True /\ negb (st X =? 0) <> true) ->>
(fun st : state => True /\ st X = 0)) /\
(fun st : state => True /\ st X <> 0) ->>
(fun _ : state => True) X ⊢> X - 1) /\
(fun st : state => True /\ st X = 0) ->>
(fun st : state => st X = 0)
: Prop
*)
(* ==>
((fun _ : state => True) ->>
(fun _ : state => True)) /\
((fun st : state => True /\ negb (st X =? 0) = true) ->>
(fun st : state => True /\ st X <> 0)) /\
((fun st : state => True /\ negb (st X =? 0) <> true) ->>
(fun st : state => True /\ st X = 0)) /\
(fun st : state => True /\ st X <> 0) ->>
(fun _ : state => True) X ⊢> X - 1) /\
(fun st : state => True /\ st X = 0) ->>
(fun st : state => st X = 0)
: Prop
*)
幸运的是,我们的 verify_assertion 策略
通常能处理其中的大多数(有时是全部)。
为了自动化整体的验证过程,我们可以用
verification_correct 提取验证条件。
用 verify_assertion 尽可能多地验证它们,
最后手工整理剩下的零星部分。
这是 dec_while 正确的最终形式化证明。
类似地,下面是之前见过的「用加减法交换」示例
的形式化装饰程序。
Definition swap_dec (m n:nat) : decorated :=
<{
{{ X = m ∧ Y = n}} ->>
{{ (X + Y) - ((X + Y) - Y) = n ∧ (X + Y) - Y = m }}
X := X + Y
{{ X - (X - Y) = n ∧ X - Y = m }};
Y := X - Y
{{ X - Y = n ∧ Y = m }};
X := X - Y
{{ X = n ∧ Y = m}}
}>.
Theorem swap_correct : ∀ m n,
outer_triple_valid (swap_dec m n).
Proof. verify. Qed.
<{
{{ X = m ∧ Y = n}} ->>
{{ (X + Y) - ((X + Y) - Y) = n ∧ (X + Y) - Y = m }}
X := X + Y
{{ X - (X - Y) = n ∧ X - Y = m }};
Y := X - Y
{{ X - Y = n ∧ Y = m }};
X := X - Y
{{ X = n ∧ Y = m}}
}>.
Theorem swap_correct : ∀ m n,
outer_triple_valid (swap_dec m n).
Proof. verify. Qed.
下面是之前「正差」程序的形式化装饰版本:
Definition positive_difference_dec :=
<{
{{True}}
if X ≤ Y then
{{True ∧ X ≤ Y}} ->>
{{(Y - X) + X = Y ∨ (Y - X) + Y = X}}
Z := Y - X
{{Z + X = Y ∨ Z + Y = X}}
else
{{True ∧ ¬(X ≤ Y)}} ->>
{{(X - Y) + X = Y ∨ (X - Y) + Y = X}}
Z := X - Y
{{Z + X = Y ∨ Z + Y = X}}
end
{{Z + X = Y ∨ Z + Y = X}}
}>.
Theorem positive_difference_correct :
outer_triple_valid positive_difference_dec.
Proof. verify. Qed.
<{
{{True}}
if X ≤ Y then
{{True ∧ X ≤ Y}} ->>
{{(Y - X) + X = Y ∨ (Y - X) + Y = X}}
Z := Y - X
{{Z + X = Y ∨ Z + Y = X}}
else
{{True ∧ ¬(X ≤ Y)}} ->>
{{(X - Y) + X = Y ∨ (X - Y) + Y = X}}
Z := X - Y
{{Z + X = Y ∨ Z + Y = X}}
end
{{Z + X = Y ∨ Z + Y = X}}
}>.
Theorem positive_difference_correct :
outer_triple_valid positive_difference_dec.
Proof. verify. Qed.
练习: 2 stars,标准,特别有用 (if_minus_plus_correct)
下面是之前见过的 if_minus_plus 程序 形式化装饰版本的【骨架|Skeleton】。 把所有 FILL_IN_HERE 出现的地方替换成适当的断言, 并补全证明(它应当与上面的示例一样直截了当)。
Definition if_minus_plus_dec :=
<{
{{True}}
if (X ≤ Y) then
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
Z := Y - X
{{ FILL_IN_HERE }}
else
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
Y := X + Z
{{ FILL_IN_HERE }}
end
{{ Y = X + Z}} }>.
Theorem if_minus_plus_correct :
outer_triple_valid if_minus_plus_dec.
Proof.
(* 在这里填写 *) Admitted.
☐
<{
{{True}}
if (X ≤ Y) then
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
Z := Y - X
{{ FILL_IN_HERE }}
else
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
Y := X + Z
{{ FILL_IN_HERE }}
end
{{ Y = X + Z}} }>.
Theorem if_minus_plus_correct :
outer_triple_valid if_minus_plus_dec.
Proof.
(* 在这里填写 *) Admitted.
☐
Definition div_mod_dec (a b : nat) : decorated :=
<{
{{ True }} ->>
{{ FILL_IN_HERE }}
X := a
{{ FILL_IN_HERE }};
Y := 0
{{ FILL_IN_HERE }};
while b ≤ X do
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
X := X - b
{{ FILL_IN_HERE }};
Y := Y + 1
{{ FILL_IN_HERE }}
end
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }} }>.
Theorem div_mod_outer_triple_valid : ∀ a b,
outer_triple_valid (div_mod_dec a b).
Proof.
(* 在这里填写 *) Admitted.
☐
<{
{{ True }} ->>
{{ FILL_IN_HERE }}
X := a
{{ FILL_IN_HERE }};
Y := 0
{{ FILL_IN_HERE }};
while b ≤ X do
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
X := X - b
{{ FILL_IN_HERE }};
Y := Y + 1
{{ FILL_IN_HERE }}
end
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }} }>.
Theorem div_mod_outer_triple_valid : ∀ a b,
outer_triple_valid (div_mod_dec a b).
Proof.
(* 在这里填写 *) Admitted.
☐
寻找循环不变式
- 加强【循环不变式】意味着在试图确立循环体的后置条件时
你有更强的假设可用。但这也意味着循环体的后置条件
更难证明。
- 类似地,加强【归纳假设】意味着在试图完成证明的归纳步骤时 你有更强的假设可用。但这也意味着被归纳证明的语句 更难证明。
示例:慢速减法
{{ X = m ∧ Y = n }}
while X ≠ 0 do
Y := Y - 1;
X := X - 1
end
{{ Y = n - m }}
(1) {{ X = m ∧ Y = n }} ->> (a)
(2) {{ Inv }}
while X ≠ 0 do
(3) {{ Inv ∧ X ≠ 0 }} ->> (c)
(4) {{ Inv [X ⊢> X-1] [Y ⊢> Y-1] }}
Y := Y - 1;
(5) {{ Inv [X ⊢> X-1] }}
X := X - 1
(6) {{ Inv }}
end
(7) {{ Inv ∧ ¬ (X ≠ 0) }} ->> (b)
(8) {{ Y = n - m }} 检查这个骨架,我们可以看到任何合法的 Inv 都必须满足三个条件:
- (a) 它必须足够【弱】,能被循环的前置条件蕴含, 即 (1) 必须蕴含 (2);
- (b) 它必须足够【强】,能蕴含程序的后置条件, 即 (7) 必须蕴含 (8);
- (c) 在循环守卫也为真的假设下, 它必须被循环的一次迭代所【保持】, 即 (3) 必须蕴含 (4)。
(1) {{ X = m ∧ Y = n }} ->> (a - OK)
(2) {{ True }}
while X ≠ 0 do
(3) {{ True ∧ X ≠ 0 }} ->> (c - OK)
(4) {{ True }}
Y := Y - 1;
(5) {{ True }}
X := X - 1
(6) {{ True }}
end
(7) {{ True ∧ ~(X ≠ 0) }} ->> (b - WRONG!)
(8) {{ Y = n - m }} 条件 (a) 和 (c) 平凡地满足,而 (b) 是错的。 True ∧ X = 0(7)并不蕴含 Y = n - m(8)。 事实上这两个断言毫不相关, 所以很容易为这条蕴含找到反例 (比如 Y = X = m = 0 且 n = 1)。
(1) {{ X = m ∧ Y = n }} ->> (a - WRONG!)
(2) {{ Y = n - m }}
while X ≠ 0 do
(3) {{ Y = n - m ∧ X ≠ 0 }} ->> (c - WRONG!)
(4) {{ Y - 1 = n - m }}
Y := Y - 1;
(5) {{ Y = n - m }}
X := X - 1
(6) {{ Y = n - m }}
end
(7) {{ Y = n - m ∧ ~(X ≠ 0) }} ->> (b - OK)
(8) {{ Y = n - m }} 这次条件 (b) 平凡地成立,但 (a) 和 (c) 坏掉了。 条件 (a) 要求 (1) X = m ∧ Y = n 蕴含 (2) Y = n - m。 若把 Y 替换为 n,我们就得对任意 m 和 n 证明 n = n - m,这并不成立(例如 m = n = 1 时)。 条件 (c) 要求 n - m - 1 = n - m, 例如对 n = 1 和 m = 0 就失败了。 所以,尽管 Y = n - m 在循环结束时成立, 它从一开始就并不成立,每次迭代也不成立; 它不是正确的循环不变式。
(1) {{ X = m ∧ Y = n }} ->> (a - OK)
(2) {{ Y - X = n - m }}
while X ≠ 0 do
(3) {{ Y - X = n - m ∧ X ≠ 0 }} ->> (c - OK)
(4) {{ (Y - 1) - (X - 1) = n - m }}
Y := Y - 1;
(5) {{ Y - (X - 1) = n - m }}
X := X - 1
(6) {{ Y - X = n - m }}
end
(7) {{ Y - X = n - m ∧ ~(X ≠ 0) }} ->> (b - OK)
(8) {{ Y = n - m }} 成功!条件 (a)、(b) 和 (c) 现在全都成立。 (验证 (c) 时,我们需要检查:在 X ≠ 0 的假设下 有 Y - X = (Y - 1) - (X - 1);这对所有自然数 X 和 Y 都成立。)
Example subtract_slowly_dec (m : nat) (n : nat) : decorated :=
<{
{{ X = m ∧ Y = n }} ->>
{{ Y - X = n - m }}
while X ≠ 0 do
{{ Y - X = n - m ∧ X ≠ 0 }} ->>
{{ (Y - 1) - (X - 1) = n - m }}
Y := Y - 1
{{ Y - (X - 1) = n - m }} ;
X := X - 1
{{ Y - X = n - m }}
end
{{ Y - X = n - m ∧ X = 0 }} ->>
{{ Y = n - m }} }>.
Theorem subtract_slowly_outer_triple_valid : ∀ m n,
outer_triple_valid (subtract_slowly_dec m n).
Proof.
verify. (* 这里要磨一阵子! *)
Qed.
<{
{{ X = m ∧ Y = n }} ->>
{{ Y - X = n - m }}
while X ≠ 0 do
{{ Y - X = n - m ∧ X ≠ 0 }} ->>
{{ (Y - 1) - (X - 1) = n - m }}
Y := Y - 1
{{ Y - (X - 1) = n - m }} ;
X := X - 1
{{ Y - X = n - m }}
end
{{ Y - X = n - m ∧ X = 0 }} ->>
{{ Y = n - m }} }>.
Theorem subtract_slowly_outer_triple_valid : ∀ m n,
outer_triple_valid (subtract_slowly_dec m n).
Proof.
verify. (* 这里要磨一阵子! *)
Qed.
练习:慢速赋值
练习: 2 stars,标准 (slow_assignment)
把当前存放在 X 中的数赋给变量 Y 的一种迂回方式是: 先把 Y 置为 0,然后把 X 递减到 0, 每步把 Y 加 1。下面是一个实现这个想法的程序。 填入装饰并证明装饰程序正确。(证明应当非常简单。)
Example slow_assignment_dec (m : nat) : decorated :=
<{
{{ X = m }}
Y := 0
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }} ;
while X ≠ 0 do
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
X := X - 1
{{ FILL_IN_HERE }} ;
Y := Y + 1
{{ FILL_IN_HERE }}
end
{{ FILL_IN_HERE }} ->>
{{ Y = m }}
}>.
Theorem slow_assignment : ∀ m,
outer_triple_valid (slow_assignment_dec m).
Proof. (* 在这里填写 *) Admitted.
☐
<{
{{ X = m }}
Y := 0
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }} ;
while X ≠ 0 do
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
X := X - 1
{{ FILL_IN_HERE }} ;
Y := Y + 1
{{ FILL_IN_HERE }}
end
{{ FILL_IN_HERE }} ->>
{{ Y = m }}
}>.
Theorem slow_assignment : ∀ m,
outer_triple_valid (slow_assignment_dec m).
Proof. (* 在这里填写 *) Admitted.
☐
示例:奇偶性
{{ X = m }}
while 2 ≤ X do
X := X - 2
end
{{ X = parity m }} 规约中使用的 parity 函数在 Rocq 中定义如下:
后置条件在循环开始时并不成立,
因为 m = parity m 对任意 m 并不成立,
所以我们不能指望把它用作循环不变式。
为了找到可行的循环不变式,
让我们稍微想想这个循环做什么。
每次迭代它把 X 减 2,这保持 X 的奇偶性。
所以 X 的奇偶性不变,即它是不变的。
X 的初始值是 m,所以 X 的奇偶性总等于 m 的奇偶性。
用 parity X = parity m 作为不变式,
我们得到如下装饰程序:
{{ X = m }} ->> (a - OK)
{{ parity X = parity m }}
while 2 ≤ X do
{{ parity X = parity m ∧ 2 ≤ X }} ->> (c - OK)
{{ parity (X-2) = parity m }}
X := X - 2
{{ parity X = parity m }}
end
{{ parity X = parity m ∧ ~(2 ≤ X) }} ->> (b - OK)
{{ X = parity m }} 有了这个循环不变式,条件 (a)、(b) 和 (c) 全都满足。 验证 (b) 时我们注意到,当 X < 2 时有 parity X = X (从 parity 的定义容易看出)。 验证 (c) 时我们注意到,当 2 ≤ X 时 有 parity X = parity (X-2)。
提示:实际上有好几个都能导出好证明的循环不变式;
其中一个导出特别简单证明的是 parity X = parity m,
或者更形式化地说,用 # 语法把 parity 函数的应用
提升到断言语法中,即 {{ #parity X = #parity m }}。
{{ X = m }} ->> (a - OK)
{{ parity X = parity m }}
while 2 ≤ X do
{{ parity X = parity m ∧ 2 ≤ X }} ->> (c - OK)
{{ parity (X-2) = parity m }}
X := X - 2
{{ parity X = parity m }}
end
{{ parity X = parity m ∧ ~(2 ≤ X) }} ->> (b - OK)
{{ X = parity m }} 有了这个循环不变式,条件 (a)、(b) 和 (c) 全都满足。 验证 (b) 时我们注意到,当 X < 2 时有 parity X = X (从 parity 的定义容易看出)。 验证 (c) 时我们注意到,当 2 ≤ X 时 有 parity X = parity (X-2)。
练习: 3 stars,标准,可选 (parity)
把上面的非形式化装饰程序翻译成形式化的, 并证明它正确。
Definition parity_dec (m:nat) : decorated :=
<{
{{ X = m }} ->>
{{ FILL_IN_HERE }}
while 2 ≤ X do
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
X := X - 2
{{ FILL_IN_HERE }}
end
{{ FILL_IN_HERE }} ->>
{{ X = #parity m }} }>.
<{
{{ X = m }} ->>
{{ FILL_IN_HERE }}
while 2 ≤ X do
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
X := X - 2
{{ FILL_IN_HERE }}
end
{{ FILL_IN_HERE }} ->>
{{ X = #parity m }} }>.
若你使用建议的循环不变式,
下面这些引理(以及 leb_complete 和 leb_correct)
可能会派上用场。
Lemma parity_ge_2 : ∀ x,
2 ≤ x →
parity (x - 2) = parity x.
Lemma parity_lt_2 : ∀ x,
¬ 2 ≤ x →
parity x = x.
Theorem parity_outer_triple_valid : ∀ m,
outer_triple_valid (parity_dec m).
Proof.
(* 在这里填写 *) Admitted.
☐
2 ≤ x →
parity (x - 2) = parity x.
Proof.
destruct x; intros; simpl.
- reflexivity.
- destruct x; simpl.
+ lia.
+ rewrite sub_0_r. reflexivity.
Qed.
destruct x; intros; simpl.
- reflexivity.
- destruct x; simpl.
+ lia.
+ rewrite sub_0_r. reflexivity.
Qed.
Lemma parity_lt_2 : ∀ x,
¬ 2 ≤ x →
parity x = x.
Theorem parity_outer_triple_valid : ∀ m,
outer_triple_valid (parity_dec m).
Proof.
(* 在这里填写 *) Admitted.
☐
示例:求平方根
{{ X=m }}
Z := 0;
while (Z+1)*(Z+1) ≤ X do
Z := Z+1
end
{{ Z×Z≤m ∧ m<(Z+1)*(Z+1) }}
(1) {{ X=m }} ->> (a - second conjunct of (2) WRONG!)
(2) {{ 0*0 ≤ m ∧ m<(0+1)*(0+1) }}
Z := 0
(3) {{ Z×Z ≤ m ∧ m<(Z+1)*(Z+1) }};
while (Z+1)*(Z+1) ≤ X do
(4) {{ Z×Z≤m ∧ m<(Z+1)*(Z+1)
∧ (Z+1)*(Z+1)<=X }} ->> (c - WRONG!)
(5) {{ (Z+1)*(Z+1)<=m ∧ m<((Z+1)+1)*((Z+1)+1) }}
Z := Z+1
(6) {{ Z×Z≤m ∧ m<(Z+1)*(Z+1) }}
end
(7) {{ Z×Z≤m ∧ m<(Z+1)*(Z+1) ∧ ~((Z+1)*(Z+1)<=X) }} ->> (b - OK)
(8) {{ Z×Z≤m ∧ m<(Z+1)*(Z+1) }} 这效果不好:条件 (a) 和 (c) 都失败了。 看条件 (c)。(4) 的第二个合取支与 (5) 的第一个合取支 几乎相同,只是 (4) 提到 X 而 (5) 提到 m。 但注意这个程序从不给 X 赋值, 所以我们始终应有 X=m。 我们没有把这个信息从 (1) 传播进循环不变式,但我们可以!
{{ X=m }} ->> (a - OK)
{{ X=m ∧ 0*0 ≤ m }}
Z := 0
{{ X=m ∧ Z×Z ≤ m }};
while (Z+1)*(Z+1) ≤ X do
{{ X=m ∧ Z×Z≤m ∧ (Z+1)*(Z+1)<=X }} ->> (c - OK)
{{ X=m ∧ (Z+1)*(Z+1)<=m }}
Z := Z + 1
{{ X=m ∧ Z×Z≤m }}
end
{{ X=m ∧ Z×Z≤m ∧ ~((Z+1)*(Z+1)<=X) }} ->> (b - OK)
{{ Z×Z≤m ∧ m<(Z+1)*(Z+1) }} 这行得通,因为条件 (a)、(b) 和 (c) 现在都相当平凡地满足。
练习: 3 stars,标准,可选 (sqrt)
把上面的非形式化装饰程序翻译成形式化的, 并证明它正确。
Definition sqrt_dec (m:nat) : decorated :=
<{
{{ X = m }} ->>
{{ FILL_IN_HERE }}
Z := 0
{{ FILL_IN_HERE }};
while ((Z+1)×(Z+1) ≤ X) do
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
Z := Z + 1
{{ FILL_IN_HERE }}
end
{{ FILL_IN_HERE }} ->>
{{ Z×Z≤m ∧ m<(Z+1)×(Z+1) }}
}>.
Theorem sqrt_correct : ∀ m,
outer_triple_valid (sqrt_dec m).
Proof. (* 在这里填写 *) Admitted.
<{
{{ X = m }} ->>
{{ FILL_IN_HERE }}
Z := 0
{{ FILL_IN_HERE }};
while ((Z+1)×(Z+1) ≤ X) do
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
Z := Z + 1
{{ FILL_IN_HERE }}
end
{{ FILL_IN_HERE }} ->>
{{ Z×Z≤m ∧ m<(Z+1)×(Z+1) }}
}>.
Theorem sqrt_correct : ∀ m,
outer_triple_valid (sqrt_dec m).
Proof. (* 在这里填写 *) Admitted.
示例:平方
{{ X = m }}
Y := 0;
Z := 0;
while Y ≠ X do
Z := Z + X;
Y := Y + 1
end
{{ Z = m×m }}
{{ X = m }} ->> (a - WRONG)
{{ 0 = m×m ∧ X = m }}
Y := 0
{{ 0 = m×m ∧ X = m }};
Z := 0
{{ Z = m×m ∧ X = m }};
while Y ≠ X do
{{ Z = m×m ∧ X = m ∧ Y ≠ X }} ->> (c - WRONG)
{{ Z+X = m×m ∧ X = m }}
Z := Z + X
{{ Z = m×m ∧ X = m }};
Y := Y + 1
{{ Z = m×m ∧ X = m }}
end
{{ Z = m×m ∧ X = m ∧ ~(Y ≠ X) }} ->> (b - OK)
{{ Z = m×m }}
{{ X = m }} ->> (a - OK)
{{ 0 = 0*m ∧ X = m }}
Y := 0
{{ 0 = Y×m ∧ X = m }};
Z := 0
{{ Z = Y×m ∧ X = m }};
while Y ≠ X do
{{ Z = Y×m ∧ X = m ∧ Y ≠ X }} ->> (c - OK)
{{ Z+X = (Y+1)*m ∧ X = m }}
Z := Z + X
{{ Z = (Y+1)*m ∧ X = m }};
Y := Y + 1
{{ Z = Y×m ∧ X = m }}
end
{{ Z = Y×m ∧ X = m ∧ ~(Y ≠ X) }} ->> (b - OK)
{{ Z = m×m }}
练习:阶乘
练习: 4 stars,高级 (factorial_correct)
回想一下 n! 表示 n 的【阶乘|Factorial】 (即 n! = 1*2*...*n)。 形式化地说,阶乘函数在 Rocq 标准库中递归定义, 等价于以下定义:Fixpoint fact (n : nat) : nat :=
match n with
| O ⇒ 1
| S n' ⇒ n × (fact n')
end.
首先,写出 Imp 程序 factorial,
计算最初存放在变量 X 中的数的阶乘,
并把结果放进变量 Y。
以你的定义 factorial 和 slow_assignment_dec 为指南,
写出实现阶乘函数的形式化装饰程序 factorial_dec。
提示:回想断言中用 # 把函数作用于 Imp 变量的用法。
填空并完成正确性证明。记住我们处理的是自然数,
对它们而言除法和减法的行为可能与实数不同。
把这两种运算都排除在你的循环不变式之外是明智的!
然后陈述一条名为 factorial_correct 的定理,
表明 factorial_dec 正确,并证明该定理。
一切顺利的话,verify 只会给你留下两个子目标,
每个都需要确立 fact 的某个性质,
而非关于你的程序证明任何东西。
提示:若那两个子目标证明起来很繁琐,
可以想想如何重述你的断言,
让其中的数学运算在 Rocq 中更容易摆弄。
例如,回想 1 + ... 比 ... + 1 更容易处理。
Example factorial_dec (m:nat) : decorated
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
(* 在这里填写 *)
Theorem factorial_correct: ∀ m,
outer_triple_valid (factorial_dec m).
Proof. (* 在这里填写 *) Admitted.
☐
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
(* 在这里填写 *)
Theorem factorial_correct: ∀ m,
outer_triple_valid (factorial_dec m).
Proof. (* 在这里填写 *) Admitted.
☐
练习:最小值
练习: 3 stars,高级 (minimum_correct)
为下列程序填入装饰并证明它们正确。 与 factorial 一样,小心涉及自然数的数学推理, 尤其是减法。
Definition minimum_dec (a b : nat) : decorated :=
<{
{{ True }} ->>
{{ FILL_IN_HERE }}
X := a
{{ FILL_IN_HERE }};
Y := b
{{ FILL_IN_HERE }};
Z := 0
{{ FILL_IN_HERE }};
while X ≠ 0 && Y ≠ 0 do
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
X := X - 1
{{ FILL_IN_HERE }};
Y := Y - 1
{{ FILL_IN_HERE }};
Z := Z + 1
{{ FILL_IN_HERE }}
end
{{ FILL_IN_HERE }} ->>
{{ Z = #min a b }}
}>.
Theorem minimum_correct : ∀ a b,
outer_triple_valid (minimum_dec a b).
Proof. (* 在这里填写 *) Admitted.
☐
<{
{{ True }} ->>
{{ FILL_IN_HERE }}
X := a
{{ FILL_IN_HERE }};
Y := b
{{ FILL_IN_HERE }};
Z := 0
{{ FILL_IN_HERE }};
while X ≠ 0 && Y ≠ 0 do
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
X := X - 1
{{ FILL_IN_HERE }};
Y := Y - 1
{{ FILL_IN_HERE }};
Z := Z + 1
{{ FILL_IN_HERE }}
end
{{ FILL_IN_HERE }} ->>
{{ Z = #min a b }}
}>.
Theorem minimum_correct : ∀ a b,
outer_triple_valid (minimum_dec a b).
Proof. (* 在这里填写 *) Admitted.
☐
练习:双循环
练习: 3 stars,标准 (two_loops)
下面是把 3 个数相加的一种相当低效的方式:X := 0;
Y := 0;
Z := c;
while X ≠ a do
X := X + 1;
Z := Z + 1
end;
while Y ≠ b do
Y := Y + 1;
Z := Z + 1
end 补全下面的装饰程序,表明它确实做了该做的事。
Definition two_loops_dec (a b c : nat) : decorated :=
<{
{{ True }} ->>
{{ FILL_IN_HERE }}
X := 0
{{ FILL_IN_HERE }};
Y := 0
{{ FILL_IN_HERE }};
Z := c
{{ FILL_IN_HERE }};
while X ≠ a do
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
X := X + 1
{{ FILL_IN_HERE }};
Z := Z + 1
{{ FILL_IN_HERE }}
end
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }};
while Y ≠ b do
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
Y := Y + 1
{{ FILL_IN_HERE }};
Z := Z + 1
{{ FILL_IN_HERE }}
end
{{ FILL_IN_HERE }} ->>
{{ Z = a + b + c }}
}>.
Theorem two_loops : ∀ a b c,
outer_triple_valid (two_loops_dec a b c).
Proof.
(* 在这里填写 *) Admitted.
☐
<{
{{ True }} ->>
{{ FILL_IN_HERE }}
X := 0
{{ FILL_IN_HERE }};
Y := 0
{{ FILL_IN_HERE }};
Z := c
{{ FILL_IN_HERE }};
while X ≠ a do
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
X := X + 1
{{ FILL_IN_HERE }};
Z := Z + 1
{{ FILL_IN_HERE }}
end
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }};
while Y ≠ b do
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
Y := Y + 1
{{ FILL_IN_HERE }};
Z := Z + 1
{{ FILL_IN_HERE }}
end
{{ FILL_IN_HERE }} ->>
{{ Z = a + b + c }}
}>.
Theorem two_loops : ∀ a b c,
outer_triple_valid (two_loops_dec a b c).
Proof.
(* 在这里填写 *) Admitted.
☐
练习:幂级数
练习: 4 stars,标准,可选 (dpow2)
下面是一个计算级数 1 + 2 + 2^2 + ... + 2^m = 2^(m+1) - 1 的程序:X := 0;
Y := 1;
Z := 1;
while X ≠ m do
Z := 2 × Z;
Y := Y + Z;
X := X + 1
end 把它变成装饰程序并证明其正确。
Fixpoint pow2 n :=
match n with
| 0 ⇒ 1
| S n' ⇒ 2 × (pow2 n')
end.
Definition dpow2_dec (n : nat) :=
<{
{{ True }} ->>
{{ FILL_IN_HERE }}
X := 0
{{ FILL_IN_HERE }};
Y := 1
{{ FILL_IN_HERE }};
Z := 1
{{ FILL_IN_HERE }};
while X ≠ n do
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
Z := 2 × Z
{{ FILL_IN_HERE }};
Y := Y + Z
{{ FILL_IN_HERE }};
X := X + 1
{{ FILL_IN_HERE }}
end
{{ FILL_IN_HERE }} ->>
{{ Y = #pow2 (n+1) - 1 }}
}>.
match n with
| 0 ⇒ 1
| S n' ⇒ 2 × (pow2 n')
end.
Definition dpow2_dec (n : nat) :=
<{
{{ True }} ->>
{{ FILL_IN_HERE }}
X := 0
{{ FILL_IN_HERE }};
Y := 1
{{ FILL_IN_HERE }};
Z := 1
{{ FILL_IN_HERE }};
while X ≠ n do
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
Z := 2 × Z
{{ FILL_IN_HERE }};
Y := Y + Z
{{ FILL_IN_HERE }};
X := X + 1
{{ FILL_IN_HERE }}
end
{{ FILL_IN_HERE }} ->>
{{ Y = #pow2 (n+1) - 1 }}
}>.
一些你可能觉得有用的引理……
Lemma pow2_plus_1 : ∀ n,
pow2 (n+1) = pow2 n + pow2 n.
Proof.
induction n; simpl.
- reflexivity.
- lia.
Qed.
Lemma pow2_le_1 : ∀ n, pow2 n ≥ 1.
Proof.
induction n; simpl; [constructor | lia].
Qed.
pow2 (n+1) = pow2 n + pow2 n.
Proof.
induction n; simpl.
- reflexivity.
- lia.
Qed.
Lemma pow2_le_1 : ∀ n, pow2 n ≥ 1.
Proof.
induction n; simpl; [constructor | lia].
Qed.
主正确性定理:
练习: 2 stars,高级,可选 (fib_eqn)
斐波那契函数通常写成这样:Fixpoint fib n :=
match n with
| 0 ⇒ 1
| 1 ⇒ 1
| _ ⇒ fib (pred n) + fib (pred (pred n))
end. 这通不过 Rocq 的终止检查器, 但下面这个稍显笨拙的定义可以通过:
Fixpoint fib n :=
match n with
| 0 ⇒ 1
| S n' ⇒ match n' with
| 0 ⇒ 1
| S n'' ⇒ fib n' + fib n''
end
end.
match n with
| 0 ⇒ 1
| S n' ⇒ match n' with
| 0 ⇒ 1
| S n'' ⇒ fib n' + fib n''
end
end.
证明 fib 满足下面的等式。
你会在下一个练习中需要它作为引理。
练习: 4 stars,高级,可选 (fib)
下面的 Imp 程序终止时, 把 fib n 的值留在变量 Y 中:X := 1;
Y := 1;
Z := 1;
while X ≠ 1 + n do
T := Z;
Z := Z + Y;
Y := T;
X := 1 + X
end 补全下面 dfib 的定义,并证明它满足这条规约:
{{ True }} dfib {{ Y = fib n }} 你的断言中会需要多次使用 ap。 一切顺利的话,你的证明会非常简短。
Definition T : string := "T".
Definition dfib (n : nat) : decorated :=
<{
{{ True }} ->>
{{ FILL_IN_HERE }}
X := 1
{{ FILL_IN_HERE }} ;
Y := 1
{{ FILL_IN_HERE }} ;
Z := 1
{{ FILL_IN_HERE }} ;
while X ≠ 1 + n do
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
T := Z
{{ FILL_IN_HERE }};
Z := Z + Y
{{ FILL_IN_HERE }};
Y := T
{{ FILL_IN_HERE }};
X := 1 + X
{{ FILL_IN_HERE }}
end
{{ FILL_IN_HERE }} ->>
{{ Y = #fib n }}
}>.
Theorem dfib_correct : ∀ n,
outer_triple_valid (dfib n).
Proof.
(* 在这里填写 *) Admitted.
☐
Definition dfib (n : nat) : decorated :=
<{
{{ True }} ->>
{{ FILL_IN_HERE }}
X := 1
{{ FILL_IN_HERE }} ;
Y := 1
{{ FILL_IN_HERE }} ;
Z := 1
{{ FILL_IN_HERE }} ;
while X ≠ 1 + n do
{{ FILL_IN_HERE }} ->>
{{ FILL_IN_HERE }}
T := Z
{{ FILL_IN_HERE }};
Z := Z + Y
{{ FILL_IN_HERE }};
Y := T
{{ FILL_IN_HERE }};
X := 1 + X
{{ FILL_IN_HERE }}
end
{{ FILL_IN_HERE }} ->>
{{ Y = #fib n }}
}>.
Theorem dfib_correct : ∀ n,
outer_triple_valid (dfib n).
Proof.
(* 在这里填写 *) Admitted.
☐
练习: 5 stars,高级,可选 (improve_dcom)
上面定义的形式化装饰程序, 本意是尽可能与前面定义的非形式化装饰程序看起来相似。 若放弃这一要求,我们几乎可以消除所有标注, 只要求显式提供最终后置条件和循环不变式。 请做到这一点,即定义一个标注尽可能少的 dcom 新版本, 并调整通向 verification_correct 定理的 其余形式化开发。
(* 在这里填写 *)
☐
☐
最弱前置条件(可选)
{{ False }} X := Y + 1 {{ X ≤ 5 }} 【不】很有意思:虽然它完全成立, 却没有告诉我们任何有用的东西。 由于没有任何状态满足该前置条件, 它没有描述任何我们能用命令 X := Y + 1 实现后置条件 X ≤ 5 的情形。
{{ Y ≤ 4 ∧ Z = 0 }} X := Y + 1 {{ X ≤ 5 }} 有一个有用的前置条件。它告诉我们: 若能以某种方式创造出我们知道 Y ≤ 4 ∧ Z = 0 的情形,那么运行这条命令将产生满足后置条件的状态。 不过,这个前置条件还不像它本可以的那样有用。 其中的 Z = 0 子句 实际上与后置条件 X ≤ 5 毫无关系。
{{ Y ≤ 4 }} X := Y + 1 {{ X ≤ 5 }} 断言 Y ≤ 4 称为 X := Y + 1 关于后置条件 X ≤ 5 的【最弱前置条件|Weakest Precondition】。
- P 是前置条件,即 {{P}} c {{Q}};且
- P 至少与所有其他前置条件一样弱, 即若 {{P'}} c {{Q}} 则 P' ->> P。
练习: 1 star,标准,可选 (wp)
下列命令关于下列后置条件的最弱前置条件是什么?1) {{ ? }} skip {{ X = 5 }}
2) {{ ? }} X := Y + Z {{ X = 5 }}
3) {{ ? }} X := Y {{ X = Y }}
4) {{ ? }}
if X = 0 then Y := Z + 1 else Y := W + 2 end
{{ Y = 5 }}
5) {{ ? }}
X := 5
{{ X = 0 }}
6) {{ ? }}
while true do X := 0 end
{{ X = 0 }}
(* 在这里填写 *)
☐
☐
练习: 3 stars,高级,可选 (is_wp)
用 valid_hoare_triple 的定义形式化地证明: Y ≤ 4 确实是 X := Y + 1 关于后置条件 X ≤ 5 的一个最弱前置条件。
Theorem is_wp_example :
is_wp ({{ Y ≤ 4 }}) (<{X := Y + 1}>) ({{ X ≤ 5 }}).
Proof.
(* 在这里填写 *) Admitted.
☐
is_wp ({{ Y ≤ 4 }}) (<{X := Y + 1}>) ({{ X ≤ 5 }}).
Proof.
(* 在这里填写 *) Admitted.
☐
Theorem hoare_asgn_weakest : ∀ Q X a,
is_wp ({{ Q [X ⊢> a] }}) <{ X := a }> Q.
Proof.
(* 在这里填写 *) Admitted.
☐
is_wp ({{ Q [X ⊢> a] }}) <{ X := a }> Q.
Proof.
(* 在这里填写 *) Admitted.
☐
Module Himp2.
Import Himp.
Lemma hoare_havoc_weakest : ∀ (P Q : Assertion) (X : string),
{{ P }} havoc X {{ Q }} →
P ->> havoc_pre X Q.
Proof.
(* 在这里填写 *) Admitted.
End Himp2.
☐
Import Himp.
Lemma hoare_havoc_weakest : ∀ (P Q : Assertion) (X : string),
{{ P }} havoc X {{ Q }} →
P ->> havoc_pre X Q.
Proof.
(* 在这里填写 *) Admitted.
End Himp2.
☐
(* 2026-11-26 15:55 *)
