Hoare霍尔逻辑,第一部分
Set Warnings "-notation-overridden".
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 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.
在【《逻辑基础》】(【《软件基础》】第一卷)的最后一章中,
我们开始运用课程第一部分发展的数学工具。
我们用这些工具研究一个小型编程语言 Imp 的理论。
如果我们到此为止,就已经得到了一些有用的东西:
一套定义和讨论编程语言及语言特性的工具。
它们在数学上精确、灵活且易于使用,并已用于一组关键性质。
语言设计者、编译器作者和用户都可能关心这些性质。
其中许多性质对我们理解编程语言如此基础,
以至于我们可能不会自觉地把它们当作「定理」。
但直观上显然的性质有时可能相当微妙
(有时还会微妙地出错!)。
在本卷稍后讨论【类型|Type】与【类型可靠性|Type Soundness】时,
我们会回到整个语言的元理论性质这一主题。
不过在本章中,我们转向另一组问题。
本章的目标是发展一些工具,完成若干简单的
【程序验证|Program Verification】示例。
也就是利用 Imp 的精确定义,形式地证明特定程序满足
其行为的特定【规约|Specification】。
我们将发展一个称为【Floyd-Hoare|Floyd-Hoare Logic】的推理系统,
通常简称为【霍尔逻辑|Hoare Logic】。
Imp 的每个语法构造都配有一条通用的「证明规则」,
可用来组合地推理涉及该构造的程序之正确性。
霍尔逻辑起源于 1960 年代,并一直作为深入研究的主题延续至今。
它处于众多工具的核心,这些工具正被学术界和工业界用于
规约和验证真实的软件系统。
霍尔逻辑结合了两个美妙的想法:
一种写下程序规约的自然方式;
一种【结构化证明技术|Structured Proof Technique】,
用于证明程序相对于此类规约是正确的。
这里的「结构化」是指:证明的结构直接反映程序的结构。
- 我们为 Imp 定义了一种【抽象语法树|Abstract Syntax Tree】类型,
以及一个【求值关系|Evaluation Relation】(状态上的偏函数),
它指定了程序的【操作语义|Operational Semantics】。
我们定义的语言虽然很小,却捕捉到了 C、C++、Java 等成熟语言的 关键特性。这包括可变状态这一基本概念,以及若干常见的控制结构。
- 我们证明了许多【元理论性质|Metatheoretic Property】,
「元」的意思是它们是关于整个语言的性质,
而非关于语言中特定程序的性质。这些性质包括:
- 求值的【确定性|Determinism】
- 用不同方式写出的定义之间的【等价|Equivalence】
(例如,算术表达式求值的函数式定义与关系式定义)
- 某些类别程序的保证【停机|Termination】
- 若干有用程序变换的正确性(即保持含义的意义上)
- 程序的【行为等价|Behavioral Equivalence】 (在 Equiv 一章中)。
- 求值的【确定性|Determinism】
例如,
- fun st ⇒ st X = 3 对 X 的值为 3 的状态 st 成立,
- fun st ⇒ True 对所有状态成立,而
- fun st ⇒ False 对任何状态都不成立。
练习: 1 star,标准,可选 (assertions)
用中文(或你最喜欢的自然语言)重新表述下列断言。
Module ExAssertions.
Definition assertion1 : Assertion := fun st ⇒ st X ≤ st Y.
Definition assertion2 : Assertion :=
fun st ⇒ st X = 3 ∨ st X ≤ st Y.
Definition assertion3 : Assertion :=
fun st ⇒ st Z × st Z ≤ st X ∧
¬ (((S (st Z)) × (S (st Z))) ≤ st X).
Definition assertion4 : Assertion :=
fun st ⇒ st Z = max (st X) (st Y).
(* 在这里填写 *)
End ExAssertions.
☐
Definition assertion1 : Assertion := fun st ⇒ st X ≤ st Y.
Definition assertion2 : Assertion :=
fun st ⇒ st X = 3 ∨ st X ≤ st Y.
Definition assertion3 : Assertion :=
fun st ⇒ st Z × st Z ≤ st X ∧
¬ (((S (st Z)) × (S (st Z))) ≤ st X).
Definition assertion4 : Assertion :=
fun st ⇒ st Z = max (st X) (st Y).
(* 在这里填写 *)
End ExAssertions.
☐
断言的记号
fun st ⇒ st X = m 我们只写
{{ X = m }}.
这种方法有一个小局限。我们没有自动的方式,
把出现在断言中的函数应用强制转换为适当使用状态的形式。
当其参数应被解释为 Imp 算术表达式时尤其如此。
相反,我们引入记号 #f e1 .. en,它表示
(fun st ⇒ f (e1 st) .. (en st))。
需要时,我们可以手动把此类函数调用标记为断言的一部分。
有时我们需要「转义」一个原始的「Rocq 定义的」函数,
来表达一个特别复杂的断言。我们可以使用 $ 前缀做到这一点,
如 {{ $(raw_rocq) }}。
例如,{{ $(fun st ⇒ ∀ X, st X = 0) }} 表示一个断言,
即给定状态中每个变量 X 都映射到 0。
Module ExamplePrettyAssertions.
Definition assertion1 : Assertion := {{ X = 3 }}.
Definition assertion2 : Assertion := {{ True }}.
Definition assertion3 : Assertion := {{ False }}.
Definition assertion4 : Assertion := {{ True ∨ False }}.
Definition assertion5 : Assertion := {{ X ≤ Y }}.
Definition assertion6 : Assertion := {{ X = 3 ∨ X ≤ Y }}.
Definition assertion7 : Assertion := {{ Z = (#max X Y) }}.
Definition assertion8 : Assertion := {{ Z × Z ≤ X
∧ ¬ (((#S Z) × (#S Z)) ≤ X) }}.
Definition assertion9 : Assertion := {{ #add X Y > #max Y X }}.
End ExamplePrettyAssertions.
Definition assertion1 : Assertion := {{ X = 3 }}.
Definition assertion2 : Assertion := {{ True }}.
Definition assertion3 : Assertion := {{ False }}.
Definition assertion4 : Assertion := {{ True ∨ False }}.
Definition assertion5 : Assertion := {{ X ≤ Y }}.
Definition assertion6 : Assertion := {{ X = 3 ∨ X ≤ Y }}.
Definition assertion7 : Assertion := {{ Z = (#max X Y) }}.
Definition assertion8 : Assertion := {{ Z × Z ≤ X
∧ ¬ (((#S Z) × (#S Z)) ≤ X) }}.
Definition assertion9 : Assertion := {{ #add X Y > #max Y X }}.
End ExamplePrettyAssertions.
注意,【断言蕴含】 的记号类似于「通常的」Rocq 蕴含 →。
我们还需要断言之间蕴含的「iff」变体:
(这里的 hoare_spec_scope 注解告诉 Rocq,
这个记号不是全局的,而是打算在特定语境中使用的。)
霍尔三元组,非形式化
{P} c {Q} 意思是:
- 如果命令 c 从满足断言 P 的状态开始执行,
- 且如果 c 最终在某个终止状态停机,
- 那么该终止状态将满足断言 Q。
{{P}} c {{Q}} 例如,
- 霍尔三元组
{{X = 0}} X := X + 1 {{X = 1}} 声明命令 X := X + 1 会把 X = 0 的状态 变换为 X = 1 的状态。 - 另一方面,
∀ m, {{X = m}} X := X + 1 {{X = m + 1}} 是一个【命题】,它声明霍尔三元组 {{X = m}} X := X + 1 {{X = m + 1}} 对 m 的任何选择都成立。 注意两个断言中的 m 指的是绑定在霍尔三元组之外的 Rocq 变量 m。
练习: 1 star,标准,可选 (triples)
用中文重新表述下列内容。1) {{True}} c {{X = 5}}
2) ∀ m, {{X = m}} c {{X = m + 5)}}
3) {{X ≤ Y}} c {{Y ≤ X}}
4) {{True}} c {{False}}
5) ∀ m,
{{X = m}}
c
{{Y = real_fact m}}
6) ∀ m,
{{X = m}}
c
{{(Z × Z) ≤ m ∧ ¬ (((S Z) × (S Z)) ≤ m)}}
(* 在这里填写 *)
☐
☐
练习: 1 star,标准,可选 (valid_triples)
下列霍尔三元组哪些是【成立】的,亦即 P、c、Q 之间 声明的关系为真?1) {{True}} X := 5 {{X = 5}}
2) {{X = 2}} X := X + 1 {{X = 3}}
3) {{True}} X := 5; Y := 0 {{X = 5}}
4) {{X = 2 ∧ X = 3}} X := 5 {{X = 0}}
5) {{True}} skip {{False}}
6) {{False}} skip {{True}}
7) {{True}} while true do skip end {{False}}
8) {{X = 0}}
while X = 0 do X := X + 1 end
{{X = 1}}
9) {{X = 1}}
while X ≠ 0 do X := X + 1 end
{{X = 100}}
(* 在这里填写 *)
☐
☐
Definition valid_hoare_triple
(P : Assertion) (c : com) (Q : Assertion) : Prop :=
∀ st st',
st =[ c ]=> st' →
P st →
Q st'.
(P : Assertion) (c : com) (Q : Assertion) : Prop :=
∀ st st',
st =[ c ]=> st' →
P st →
Q st'.
霍尔三元组的记号
Notation "{{ P }} c {{ Q }}" :=
(valid_hoare_triple P c Q)
(at level 2, P custom assn at level 99, c custom com at level 99,
Q custom assn at level 99)
: hoare_spec_scope.
(valid_hoare_triple P c Q)
(at level 2, P custom assn at level 99, c custom com at level 99,
Q custom assn at level 99)
: hoare_spec_scope.
Theorem hoare_post_true : ∀ (P Q : Assertion) c,
(∀ st, Q st) →
{{P}} c {{Q}}.
Proof.
(* 在这里填写 *) Admitted.
☐
(∀ st, Q st) →
{{P}} c {{Q}}.
Proof.
(* 在这里填写 *) Admitted.
☐
Theorem hoare_pre_false : ∀ (P Q : Assertion) c,
(∀ st, ¬ (P st)) →
{{P}} c {{Q}}.
Proof.
(* 在这里填写 *) Admitted.
☐
(∀ st, ¬ (P st)) →
{{P}} c {{Q}}.
Proof.
(* 在这里填写 *) Admitted.
☐
证明规则
顺序
| {{ P }} c1 {{ Q }} | |
| {{ Q }} c2 {{ R }} | (hoare_seq) |
| {{ P }} c1;c2 {{ R }} |
注意,在形式规则 hoare_seq 中,
前提是以相反的顺序给出的(c2 在 c1 之前)。
这符合信息在许多使用场合中的自然流动。
构造霍尔逻辑证明的自然方式是从程序末尾开始。
我们带着最终的后置条件,
把后置条件逆着命令往回推,直到到达开头。
赋值
{{ ??? }} X := Y {{ X = 1 }}
{{ Y = 1 }} X := Y {{ X = 1 }} 看起来想出那个前置条件一定费了些巧思。 但我们有一种机械的方法: 取后置条件 X = 1, 在其中把 X 替换为 Y。 也就是把赋值语句的左边替换为右边, 于是得到前置条件 Y = 1。
{{ ??? }} X := X + Y {{ X = 1 }} 如果我们把 X = 1 中的 X 替换为 X + Y, 就得到 X + Y = 1。 这再次导出一个成立的霍尔三元组:
{{ X + Y = 1 }} X := X + Y {{ X = 1 }} 为什么这个技巧有效?后置条件指出了我们希望 被赋值的变量 X 满足的某个性质 P。 在本例中,P 是「等于 1」。 为了补全三元组并使其成立, 我们需要指明一个保证该性质对 X 成立的前置条件。 这样的前置条件必须确保同样的性质对_ 无论什么被赋给_ X 的东西都成立。 所以在本例中,我们需要「等于 1」对 X + Y 成立。 这正是该技巧所保证的。
{{ ??? }} X := a {{ Q }} 那么前置条件就是 Q, 只是把其中 X 的任何出现替换为 a。 我们为这种替换出现的想法引入一个记号: 定义 Q [X ⊢> a] 表示「把 a 代入 X 的位置后的 Q」。
{{ Q [X ⊢> a] }} X := a {{ Q }} 阅读这条规则的一种方式如下。 如果你希望 X := a 停机在满足 Q 的状态中, 那么从一个同样满足 Q 的状态开始就足够了。 只是其中 a 被代入 X 的每个出现之处。
{{ (X ≤ 5) [X ⊢> X + 1] }} (即 X + 1 ≤ 5)
X := X + 1
{{ X ≤ 5 }}
{{ (X = 3) [X ⊢> 3] }} (即 3 = 3)
X := 3
{{ X = 3 }}
{{ (0 ≤ X ∧ X ≤ 5) [X ⊢> 3] }}. (即 0 ≤ 3 ∧ 3 ≤ 5)
X := 3
{{ 0 ≤ X ∧ X ≤ 5 }}
Definition assertion_sub X (a:aexp) (P:Assertion) : Assertion :=
fun (st : state) ⇒ P (X !-> (aeval st a); st).
Notation "P [ X ⊢> a ]" := (assertion_sub X a P)
(in custom assn at level 10, left associativity,
P custom assn, X global, a custom com)
: assertion_scope.
fun (st : state) ⇒ P (X !-> (aeval st a); st).
Notation "P [ X ⊢> a ]" := (assertion_sub X a P)
(in custom assn at level 10, left associativity,
P custom assn, X global, a custom com)
: assertion_scope.
这个记号允许我们把这个操作写成:
P[ X ⊢> a ]
也就是说,P [X ⊢> a] 表示一个断言 P'。
它的行为与 P 完全一样,只是每当 P 在当前状态中
查找变量 X 时,P' 改用表达式 a 的值。
要更细致地看清这是如何工作的,
我们来计算几个示例。
首先,假设 P' 是 (X ≤ 5) [X ⊢> 3]。
更形式化地说,P' 是 Rocq 表达式
fun st ⇒
(fun st' ⇒ st' X ≤ 5)
(X !-> aeval st 3 ; st), 它简化为
fun st ⇒
(fun st' ⇒ st' X ≤ 5)
(X !-> 3 ; st) 并进一步简化为
fun st ⇒
((X !-> 3 ; st) X) ≤ 5 最终成为
fun st ⇒
3 ≤ 5. 也就是说,P' 是断言 3 小于等于 5(正如预期)。
更有趣的示例,假设 P' 是 (X ≤ 5) [X ⊢>
X + 1]。形式化地说,P' 是 Rocq 表达式
fun st ⇒
(fun st' ⇒ st' X ≤ 5)
(X !-> aeval st (X + 1); st), 它简化为
fun st ⇒
(X !-> aeval st (X + 1) ; st) X ≤ 5 并进一步简化为
fun st ⇒
(aeval st (X + 1)) ≤ 5. 也就是说,P' 是断言 X + 1 至多为 5。
我们可以通过证明一些示例逻辑等价,
形式化地表明我们已捕捉到「断言替换」的直觉含义:
P[ X ⊢> a ]
fun st ⇒
(fun st' ⇒ st' X ≤ 5)
(X !-> aeval st 3 ; st), 它简化为
fun st ⇒
(fun st' ⇒ st' X ≤ 5)
(X !-> 3 ; st) 并进一步简化为
fun st ⇒
((X !-> 3 ; st) X) ≤ 5 最终成为
fun st ⇒
3 ≤ 5. 也就是说,P' 是断言 3 小于等于 5(正如预期)。
fun st ⇒
(fun st' ⇒ st' X ≤ 5)
(X !-> aeval st (X + 1); st), 它简化为
fun st ⇒
(X !-> aeval st (X + 1) ; st) X ≤ 5 并进一步简化为
fun st ⇒
(aeval st (X + 1)) ≤ 5. 也就是说,P' 是断言 X + 1 至多为 5。
Example equivalent_assertion1 :
{{ (X ≤ 5) [X ⊢> 3] }} <<->> {{ 3 ≤ 5 }}.
Example equivalent_assertion2 :
{{ (X ≤ 5) [X ⊢> X + 1] }} <<->> {{ (X + 1) ≤ 5 }}.
{{ (X ≤ 5) [X ⊢> 3] }} <<->> {{ 3 ≤ 5 }}.
Example equivalent_assertion2 :
{{ (X ≤ 5) [X ⊢> X + 1] }} <<->> {{ (X + 1) ≤ 5 }}.
现在,利用我们刚定义的替换操作,
可以给出赋值的精确证明规则:
我们可以形式化地证明这条规则确实成立。
| (hoare_asgn) | |
| {{Q [X ⊢> a]}} X := a {{Q}} |
下面是用这条规则对霍尔三元组的第一次形式化证明。
Example assertion_sub_example :
{{(X < 5) [X ⊢> X + 1]}}
X := X + 1
{{X < 5}}.
Proof.
apply hoare_asgn. Qed.
{{(X < 5) [X ⊢> X + 1]}}
X := X + 1
{{X < 5}}.
Proof.
apply hoare_asgn. Qed.
当然,我们可能更愿意使用这个更简单的三元组:
{{X < 4}} X := X + 1 {{X < 5}} 我们将在下一节看到如何做到这一点。
用 ∃ 提供一个合适的前置条件来补全这些霍尔三元组,
然后用 apply hoare_asgn 证明它们。
如果你发现该策略不够用,请仔细检查你是否正确补全了三元组。
{{X < 4}} X := X + 1 {{X < 5}} 我们将在下一节看到如何做到这一点。
练习: 2 stars,标准,可选 (hoare_asgn_examples1)
Example hoare_asgn_examples2 :
∃ P,
{{ P }}
X := 3
{{ 0 ≤ X ∧ X ≤ 5 }}.
Proof. (* 在这里填写 *) Admitted.
☐
∃ P,
{{ P }}
X := 3
{{ 0 ≤ X ∧ X ≤ 5 }}.
Proof. (* 在这里填写 *) Admitted.
☐
练习: 2 stars,标准,特别有用 (hoare_asgn_wrong)
| (hoare_asgn_wrong) | |
| {{ True }} X := a {{ X = a }} |
Theorem hoare_asgn_wrong : ∃ a:aexp,
¬ {{ True }} X := a {{ X = a }}.
Proof.
(* 在这里填写 *) Admitted.
(* 在这里填写 *)
☐
¬ {{ True }} X := a {{ X = a }}.
Proof.
(* 在这里填写 *) Admitted.
(* 在这里填写 *)
☐
练习: 3 stars,高级,可选 (hoare_asgn_fwd)
通过使用一个【参数|Parameter】m(一个 Rocq 数)来记住 X 的原值, 我们可以定义一条赋值的霍尔规则, 它直观地「正向工作」而非反向。| (hoare_asgn_fwd) | |
| {{fun st => P st /\ st X = m}} | |
| X := a | |
| {{fun st => P (X !-> m ; st) /\ st X = aeval (X !-> m ; st) a }} |
Theorem hoare_asgn_fwd :
∀ (m:nat) (a:aexp) (P : Assertion),
{{P ∧ X = m}}
X := a
{{ $(fun st ⇒ (P (X !-> m ; st)
∧ st X = aeval (X !-> m ; st) a)) }}.
Proof.
(* 在这里填写 *) Admitted.
☐
∀ (m:nat) (a:aexp) (P : Assertion),
{{P ∧ X = m}}
X := a
{{ $(fun st ⇒ (P (X !-> m ; st)
∧ st X = aeval (X !-> m ; st) a)) }}.
Proof.
(* 在这里填写 *) Admitted.
☐
练习: 2 stars,高级,可选 (hoare_asgn_fwd_exists)
定义赋值正向规则的另一种方式是对被赋值变量的 前一个值做存在量化。证明它是正确的。| (hoare_asgn_fwd_exists) | |
| {{fun st => P st}} | |
| X := a | |
| {{fun st => exists m, P (X !-> m ; st) /\ | |
| st X = aeval (X !-> m ; st) a }} |
Theorem hoare_asgn_fwd_exists :
∀ a (P : Assertion),
{{ P }}
X := a
{{ $(fun st ⇒ ∃ m, P (X !-> m ; st) ∧
st X = aeval (X !-> m ; st) a) }}.
Proof.
(* 在这里填写 *) Admitted.
☐
∀ a (P : Assertion),
{{ P }}
X := a
{{ $(fun st ⇒ ∃ m, P (X !-> m ; st) ∧
st X = aeval (X !-> m ; st) a) }}.
Proof.
(* 在这里填写 *) Admitted.
☐
后果规则
{{(X = 3) [X ⊢> 3]}} X := 3 {{X = 3}}, 直接由赋值规则得出,而
{{True}} X := 3 {{X = 3}} 则不能。这个三元组是成立的,但它不是 hoare_asgn 的实例, 因为 True 和 (X = 3) [X ⊢> 3] 不是语法上相等的断言。
| {{P'}} c {{Q}} | |
| P <<->> P' | |
| {{P}} c {{Q}} |
| {{P'}} c {{Q}} | |
| P ->> P' | (hoare_consequence_pre) |
| {{P}} c {{Q}} |
| {{P}} c {{Q'}} | |
| Q' ->> Q | (hoare_consequence_post) |
| {{P}} c {{Q}} |
Theorem hoare_consequence_pre : ∀ (P P' Q : Assertion) c,
{{P'}} c {{Q}} →
P ->> P' →
{{P}} c {{Q}}.
Theorem hoare_consequence_post : ∀ (P Q Q' : Assertion) c,
{{P}} c {{Q'}} →
Q' ->> Q →
{{P}} c {{Q}}.
{{P'}} c {{Q}} →
P ->> P' →
{{P}} c {{Q}}.
Proof.
unfold valid_hoare_triple, "->>".
intros P P' Q c Hhoare Himp st st' Heval Hpre.
apply Hhoare with (st := st).
- assumption.
- apply Himp. assumption.
Qed.
unfold valid_hoare_triple, "->>".
intros P P' Q c Hhoare Himp st st' Heval Hpre.
apply Hhoare with (st := st).
- assumption.
- apply Himp. assumption.
Qed.
Theorem hoare_consequence_post : ∀ (P Q Q' : Assertion) c,
{{P}} c {{Q'}} →
Q' ->> Q →
{{P}} c {{Q}}.
Example hoare_asgn_example1 :
{{True}} X := 1 {{X = 1}}.
Proof.
(* 已在课上完成 *)
eapply hoare_consequence_pre.
- apply hoare_asgn.
- unfold "->>", assertion_sub, t_update; simpl.
intros st _. reflexivity.
Qed.
{{True}} X := 1 {{X = 1}}.
Proof.
(* 已在课上完成 *)
eapply hoare_consequence_pre.
- apply hoare_asgn.
- unfold "->>", assertion_sub, t_update; simpl.
intros st _. reflexivity.
Qed.
Example assertion_sub_example2 :
{{X < 4}}
X := X + 1
{{X < 5}}.
Proof.
(* 已在课上完成 *)
eapply hoare_consequence_pre.
- apply hoare_asgn.
- unfold "->>", assertion_sub, t_update.
intros st H. simpl in ×. lia.
Qed.
{{X < 4}}
X := X + 1
{{X < 5}}.
Proof.
(* 已在课上完成 *)
eapply hoare_consequence_pre.
- apply hoare_asgn.
- unfold "->>", assertion_sub, t_update.
intros st H. simpl in ×. lia.
Qed.
最后,这里有一条组合的后果规则,
允许我们同时改变前置条件和后置条件。
| {{P'}} c {{Q'}} | |
| P ->> P' | |
| Q' ->> Q | (hoare_consequence) |
| {{P}} c {{Q}} |
Theorem hoare_consequence : ∀ (P P' Q Q' : Assertion) c,
{{P'}} c {{Q'}} →
P ->> P' →
Q' ->> Q →
{{P}} c {{Q}}.
{{P'}} c {{Q'}} →
P ->> P' →
Q' ->> Q →
{{P}} c {{Q}}.
自动化
Hint Unfold assert_implies assertion_sub t_update : core.
Hint Unfold valid_hoare_triple : core.
Hint Unfold assert_of_Prop Aexp_of_nat Aexp_of_aexp : core.
Hint Unfold valid_hoare_triple : core.
Hint Unfold assert_of_Prop Aexp_of_nat Aexp_of_aexp : core.
还要回想 auto 会搜索涉及 intros 和 apply 的证明。
默认情况下,它将应用的定理包括任何局部假设,
以及「core」【提示数据库|Hint Database】中的定理。
下面重复的 hoare_consequence_pre 的证明
看起来像是这种自动化的合适位置,
因为它所做的只是 unfold、intros 和 apply。
(它也用了 assumption,但那只是应用一个假设。)
Theorem hoare_consequence_pre' : ∀ (P P' Q : Assertion) c,
{{P'}} c {{Q}} →
P ->> P' →
{{P}} c {{Q}}.
Proof.
unfold valid_hoare_triple, "->>".
intros P P' Q c Hhoare Himp st st' Heval Hpre.
apply Hhoare with (st := st).
- assumption.
- apply Himp. assumption.
Qed.
{{P'}} c {{Q}} →
P ->> P' →
{{P}} c {{Q}}.
Proof.
unfold valid_hoare_triple, "->>".
intros P P' Q c Hhoare Himp st st' Heval Hpre.
apply Hhoare with (st := st).
- assumption.
- apply Himp. assumption.
Qed.
然而,仅仅使用 auto 并不能完成证明。
Theorem hoare_consequence_pre'' : ∀ (P P' Q : Assertion) c,
{{P'}} c {{Q}} →
P ->> P' →
{{P}} c {{Q}}.
Proof.
auto. (* no progress *)
Abort.
{{P'}} c {{Q}} →
P ->> P' →
{{P}} c {{Q}}.
Proof.
auto. (* no progress *)
Abort.
问题在于证明的 apply Hhoare with... 部分。
没有我们的一些帮助,Rocq 无法弄清如何实例化 st。
不过回想一下,许多策略都有这样的版本:
它们会用【存在性变量|Existential Variable】来取得进展,
即使那些策略的常规版本会卡住。
这里,eapply 策略会引入一个存在性变量 ?st
作为 st 的占位符,
而 eassumption 会在假设 Heval 中发现 st 时
用 st 实例化 ?st。
通过使用 eapply,我们实质上是在告诉 Rocq:
「耐心点:缺失的部分稍后会在证明中填上。」
Theorem hoare_consequence_pre''' : ∀ (P P' Q : Assertion) c,
{{P'}} c {{Q}} →
P ->> P' →
{{P}} c {{Q}}.
Proof.
unfold valid_hoare_triple, "->>".
intros P P' Q c Hhoare Himp st st' Heval Hpre.
eapply Hhoare.
- eassumption.
- apply Himp. assumption.
Qed.
{{P'}} c {{Q}} →
P ->> P' →
{{P}} c {{Q}}.
Proof.
unfold valid_hoare_triple, "->>".
intros P P' Q c Hhoare Himp st st' Heval Hpre.
eapply Hhoare.
- eassumption.
- apply Himp. assumption.
Qed.
eauto 策略会在其证明搜索中使用 eapply。
所以,整个证明实际上只需一行就能完成。
Theorem hoare_consequence_pre'''' : ∀ (P P' Q : Assertion) c,
{{P'}} c {{Q}} →
P ->> P' →
{{P}} c {{Q}}.
Proof.
eauto.
Qed.
{{P'}} c {{Q}} →
P ->> P' →
{{P}} c {{Q}}.
Proof.
eauto.
Qed.
当然,如果没经历过 hoare_consequence_pre 的
原始证明、没看过它用了哪些策略,
就很难预料 eauto 在这里就够了。
但既然我们知道 eauto 在那里有效,
那么它很可能对 hoare_consequence_post 也有效。
Theorem hoare_consequence_post' : ∀ (P Q Q' : Assertion) c,
{{P}} c {{Q'}} →
Q' ->> Q →
{{P}} c {{Q}}.
Proof.
eauto.
Qed.
{{P}} c {{Q'}} →
Q' ->> Q →
{{P}} c {{Q}}.
Proof.
eauto.
Qed.
我们也可以用 eapply 来简化一个证明
(hoare_asgn_example1),我们之前把它作为
使用后果规则的示例做过:
Example hoare_asgn_example1' :
{{True}} X := 1 {{X = 1}}.
Proof.
eapply hoare_consequence_pre. (* no need to state an assertion *)
- apply hoare_asgn.
- unfold "->>", assertion_sub, t_update.
intros st _. simpl. reflexivity.
Qed.
{{True}} X := 1 {{X = 1}}.
Proof.
eapply hoare_consequence_pre. (* no need to state an assertion *)
- apply hoare_asgn.
- unfold "->>", assertion_sub, t_update.
intros st _. simpl. reflexivity.
Qed.
该证明的最后一个要点看起来也是自动化的候选。
Example hoare_asgn_example1'' :
{{True}} X := 1 {{X = 1}}.
Proof.
eapply hoare_consequence_pre.
- apply hoare_asgn.
- auto.
Qed.
{{True}} X := 1 {{X = 1}}.
Proof.
eapply hoare_consequence_pre.
- apply hoare_asgn.
- auto.
Qed.
现在我们有了相当不错的证明脚本:
它只需指明要用哪些霍尔规则,
其余的底层细节留给 Rocq 弄清。
到这里可能已经很明显。
如果我们把 hoare_consequence_pre 和 hoare_asgn
加入提示数据库,【整个】证明都可以自动化。
我们本章不会这样做,
以便更好地理解霍尔规则何时以及如何被使用。
在下一章 Hoare2 中,
我们将更深入地探讨自动化证明整个霍尔三元组。
我们之前做的另一个使用后果的示例 hoare_asgn_example2
需要多做一点工作才能自动化。
我们可以用 eapply 简化第一行,
但最后一个要点不能只用 auto,因为它需要 lia。
Example assertion_sub_example2' :
{{X < 4}}
X := X + 1
{{X < 5}}.
Proof.
eapply hoare_consequence_pre.
- apply hoare_asgn.
- auto. (* no progress *)
unfold "->>", assertion_sub, t_update.
intros st H. simpl in ×. lia.
Qed.
{{X < 4}}
X := X + 1
{{X < 5}}.
Proof.
eapply hoare_consequence_pre.
- apply hoare_asgn.
- auto. (* no progress *)
unfold "->>", assertion_sub, t_update.
intros st H. simpl in ×. lia.
Qed.
我们引入自己的策略来处理那个要点
以及示例 1 的要点:
Ltac assertion_auto :=
try auto; (* as in example 1, above *)
try (unfold "->>", assertion_sub, t_update;
intros; simpl in *; lia). (* as in example 2 *)
Example assertion_sub_example2'' :
{{X < 4}}
X := X + 1
{{X < 5}}.
Proof.
eapply hoare_consequence_pre.
- apply hoare_asgn.
- assertion_auto.
Qed.
Example hoare_asgn_example1''':
{{True}} X := 1 {{X = 1}}.
Proof.
eapply hoare_consequence_pre.
- apply hoare_asgn.
- assertion_auto.
Qed.
try auto; (* as in example 1, above *)
try (unfold "->>", assertion_sub, t_update;
intros; simpl in *; lia). (* as in example 2 *)
Example assertion_sub_example2'' :
{{X < 4}}
X := X + 1
{{X < 5}}.
Proof.
eapply hoare_consequence_pre.
- apply hoare_asgn.
- assertion_auto.
Qed.
Example hoare_asgn_example1''':
{{True}} X := 1 {{X = 1}}.
Proof.
eapply hoare_consequence_pre.
- apply hoare_asgn.
- assertion_auto.
Qed.
再次地,我们有了相当不错的证明脚本。
关于断言的证明中所有底层细节都被自动处理了。
当然,assertion_auto 不能证明
我们可能想知道的关于断言的一切,
这里没有魔法!但它已经相当好了。
练习: 2 stars,标准 (hoare_asgn_examples_2)
证明这些三元组。仿照上面的示例, 尽量让你的证明脚本很好地自动化。
Example assertion_sub_ex1' :
{{ X ≤ 5 }}
X := 2 × X
{{ X ≤ 10 }}.
Proof.
(* 在这里填写 *) Admitted.
Example assertion_sub_ex2' :
{{ 0 ≤ 3 ∧ 3 ≤ 5 }}
X := 3
{{ 0 ≤ X ∧ X ≤ 5 }}.
Proof.
(* 在这里填写 *) Admitted.
☐
{{ X ≤ 5 }}
X := 2 × X
{{ X ≤ 10 }}.
Proof.
(* 在这里填写 *) Admitted.
Example assertion_sub_ex2' :
{{ 0 ≤ 3 ∧ 3 ≤ 5 }}
X := 3
{{ 0 ≤ X ∧ X ≤ 5 }}.
Proof.
(* 在这里填写 *) Admitted.
☐
Example hoare_asgn_example3 : ∀ (a:aexp) (n:nat),
{{a = n}}
X := a;
skip
{{X = n}}.
Proof.
intros a n. eapply hoare_seq.
- (* right part of seq *)
apply hoare_skip.
- (* left part of seq *)
eapply hoare_consequence_pre.
+ apply hoare_asgn.
+ assertion_auto.
Qed.
{{a = n}}
X := a;
skip
{{X = n}}.
Proof.
intros a n. eapply hoare_seq.
- (* right part of seq *)
apply hoare_skip.
- (* left part of seq *)
eapply hoare_consequence_pre.
+ apply hoare_asgn.
+ assertion_auto.
Qed.
非形式化地,展示使用顺序规则的证明的一种好方式
是写成一个【装饰程序|Decorated Program】,
其中断言 Q 写在 c1 和 c2 之间:
{{ a = n }}
X := a
{{ X = n }}; <--- decoration for Q
skip
{{ X = n }} 我们将在下一章更详细地回到装饰程序的想法。
{{ True }} ->>
{{ 1 = 1 }}
X := 1
{{ X = 1 }} ->>
{{ X = 1 ∧ 2 = 2 }};
Y := 2
{{ X = 1 ∧ Y = 2 }} 注意「->>」装饰的使用,每个都标记了一次 hoare_consequence_pre 的使用。
我们已经为你开了头,提供了一次 hoare_seq 的使用,
它显式地把 X = 1 确认为中间断言。
{{ a = n }}
X := a
{{ X = n }}; <--- decoration for Q
skip
{{ X = n }} 我们将在下一章更详细地回到装饰程序的想法。
练习: 2 stars,标准,特别有用 (hoare_asgn_example4)
把这个「装饰程序」翻译成形式化证明:{{ True }} ->>
{{ 1 = 1 }}
X := 1
{{ X = 1 }} ->>
{{ X = 1 ∧ 2 = 2 }};
Y := 2
{{ X = 1 ∧ Y = 2 }} 注意「->>」装饰的使用,每个都标记了一次 hoare_consequence_pre 的使用。
Example hoare_asgn_example4 :
{{ True }}
X := 1;
Y := 2
{{ X = 1 ∧ Y = 2 }}.
Proof.
eapply hoare_seq with (Q := {{ X = 1 }}).
(* 在这里填写 *) Admitted.
☐
{{ True }}
X := 1;
Y := 2
{{ X = 1 ∧ Y = 2 }}.
Proof.
eapply hoare_seq with (Q := {{ X = 1 }}).
(* 在这里填写 *) Admitted.
☐
练习: 3 stars,标准 (swap_exercise)
写一个交换 X 和 Y 的值的 Imp 程序 c, 并表明它满足下列规约:{{X ≤ Y}} c {{Y ≤ X}} 你的证明不应需要使用 unfold valid_hoare_triple。
- 记住 Imp 命令需要用 <{...}> 括号括起来。
- 记住赋值规则在「从后往前」应用时效果最好, 即从后置条件到前置条件。 所以你的证明会想从末尾开始, 逆着往程序开头做。
- 记住 eapply 是你的好朋友。)
Definition swap_program : com
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Theorem swap_exercise :
{{X ≤ Y}}
swap_program
{{Y ≤ X}}.
Proof.
(* 在这里填写 *) Admitted.
☐
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Theorem swap_exercise :
{{X ≤ Y}}
swap_program
{{Y ≤ X}}.
Proof.
(* 在这里填写 *) Admitted.
☐
练习: 4 stars,高级 (invalid_triple)
表明{{ a = n }} X := 3; Y := a {{ Y = n }} 对某些 a 和 n 的选择不是成立的霍尔三元组。
specialize H with (a := your_a) (n := your_n) 该假设就会在 your_a 和 your_n 上被实例化。
- 用所给霍尔三元组的(假定的)成立性, 推导出一个 Y 具有某值 y1 的状态 st'
- 用求值规则(E_Seq 和 E_Asgn)表明 在同一个终止状态 st' 中 Y 具有【不同】的值 y2
- y1 和 y2 都等于 st' Y,所以它们相等。 但我们选择了它们不同,于是得到矛盾, 从而完成证明。
Theorem invalid_triple : ¬ ∀ (a : aexp) (n : nat),
{{ a = n }}
X := 3; Y := a
{{ Y = n }}.
Proof.
unfold valid_hoare_triple.
intros H.
(* 在这里填写 *) Admitted.
☐
{{ a = n }}
X := 3; Y := a
{{ Y = n }}.
Proof.
unfold valid_hoare_triple.
intros H.
(* 在这里填写 *) Admitted.
☐
条件语句
| {{P}} c1 {{Q}} | |
| {{P}} c2 {{Q}} | |
| {{P}} if b then c1 else c2 {{Q}} |
{{ True }}
if X = 0
then Y := 2
else Y := X + 1
end
{{ X ≤ Y }} 因为该规则没有告诉我们: 赋值发生在「then」和「else」分支中时,状态是什么样的。
| {{P /\ b}} c1 {{Q}} | |
| {{P /\ ~ b}} c2 {{Q}} | (hoare_if) |
| {{P}} if b then c1 else c2 end {{Q}} |
Definition bassertion b : Assertion :=
fun st ⇒ (beval st b = true).
Coercion bassertion : bexp >-> Assertion.
Arguments bassertion /.
fun st ⇒ (beval st b = true).
Coercion bassertion : bexp >-> Assertion.
Arguments bassertion /.
关于 bassertion 的一个有用事实:
Lemma bexp_eval_false : ∀ b st,
beval st b = false → ¬ ((bassertion b) st).
Hint Resolve bexp_eval_false : core.
beval st b = false → ¬ ((bassertion b) st).
Hint Resolve bexp_eval_false : core.
我们在 Auto 一章构建 find_rwd 策略时
顺带提到过 congruence 策略。
与 find_rwd 一样,congruence 能够自动发现
beval st b = false 和 beval st b = true
都被假定了,注意到矛盾,
并用 discriminate 完成证明。
现在我们可以形式化条件语句的霍尔证明规则并证明其正确。
Theorem hoare_if : ∀ P Q (b:bexp) c1 c2,
{{ P ∧ b }} c1 {{Q}} →
{{ P ∧ ¬ b}} c2 {{Q}} →
{{P}} if b then c1 else c2 end {{Q}}.
{{ P ∧ b }} c1 {{Q}} →
{{ P ∧ ¬ b}} c2 {{Q}} →
{{P}} if b then c1 else c2 end {{Q}}.
也就是说(展开记号后):
Theorem hoare_if : ∀ P Q b c1 c2,
{{fun st ⇒ P st ∧ bassertion b st}} c1 {{Q}} →
{{fun st ⇒ P st ∧ ¬ (bassertion b st)}} c2 {{Q}} →
{{P}} if b then c1 else c2 end {{Q}}.
Theorem hoare_if : ∀ P Q b c1 c2,
{{fun st ⇒ P st ∧ bassertion b st}} c1 {{Q}} →
{{fun st ⇒ P st ∧ ¬ (bassertion b st)}} c2 {{Q}} →
{{P}} if b then c1 else c2 end {{Q}}.
Example if_example :
{{True}}
if (X = 0)
then Y := 2
else Y := X + 1
end
{{X ≤ Y}}.
Proof.
apply hoare_if.
- (* Then *)
eapply hoare_consequence_pre.
+ apply hoare_asgn.
+ assertion_auto. (* no progress *)
unfold "->>", assertion_sub, t_update, bassertion.
simpl. intros st [_ H]. apply eqb_eq in H.
rewrite H. lia.
- (* Else *)
eapply hoare_consequence_pre.
+ apply hoare_asgn.
+ assertion_auto.
Qed.
{{True}}
if (X = 0)
then Y := 2
else Y := X + 1
end
{{X ≤ Y}}.
Proof.
apply hoare_if.
- (* Then *)
eapply hoare_consequence_pre.
+ apply hoare_asgn.
+ assertion_auto. (* no progress *)
unfold "->>", assertion_sub, t_update, bassertion.
simpl. intros st [_ H]. apply eqb_eq in H.
rewrite H. lia.
- (* Else *)
eapply hoare_consequence_pre.
+ apply hoare_asgn.
+ assertion_auto.
Qed.
和之前一样,能够消除所有与霍尔规则无关的底层证明脚本会很好。
不幸的是,我们写的 assertion_auto 策略还不能完全胜任。
看 if_example 的证明,我们能看出原因。
我们不得不展开一个定义(bassertion)
并使用一个之前证明中不需要的定理(eqb_eq)。
所以,让我们把这些加入我们的策略,
并顺手把它清理一下。
Ltac assertion_auto' :=
unfold "->>", assertion_sub, t_update, bassertion;
intros; simpl in *;
try rewrite → eqb_eq in *; (* for equalities *)
auto; try lia.
unfold "->>", assertion_sub, t_update, bassertion;
intros; simpl in *;
try rewrite → eqb_eq in *; (* for equalities *)
auto; try lia.
现在证明相当精简了。
Example if_example'' :
{{True}}
if X = 0
then Y := 2
else Y := X + 1
end
{{X ≤ Y}}.
Proof.
apply hoare_if.
- eapply hoare_consequence_pre.
+ apply hoare_asgn.
+ assertion_auto'.
- eapply hoare_consequence_pre.
+ apply hoare_asgn.
+ assertion_auto'.
Qed.
{{True}}
if X = 0
then Y := 2
else Y := X + 1
end
{{X ≤ Y}}.
Proof.
apply hoare_if.
- eapply hoare_consequence_pre.
+ apply hoare_asgn.
+ assertion_auto'.
- eapply hoare_consequence_pre.
+ apply hoare_asgn.
+ assertion_auto'.
Qed.
我们甚至可以再缩短一点。
Example if_example''' :
{{True}}
if X = 0
then Y := 2
else Y := X + 1
end
{{X ≤ Y}}.
Proof.
apply hoare_if; eapply hoare_consequence_pre;
try apply hoare_asgn; try assertion_auto'.
Qed.
{{True}}
if X = 0
then Y := 2
else Y := X + 1
end
{{X ≤ Y}}.
Proof.
apply hoare_if; eapply hoare_consequence_pre;
try apply hoare_asgn; try assertion_auto'.
Qed.
对后面的证明,扩展 assertion_auto' 以处理不等式也会有帮助。
Ltac assertion_auto'' :=
unfold "->>", assertion_sub, t_update, bassertion;
intros; simpl in *;
try rewrite → eqb_eq in *;
try rewrite → leb_le in *; (* for inequalities *)
auto; try lia.
unfold "->>", assertion_sub, t_update, bassertion;
intros; simpl in *;
try rewrite → eqb_eq in *;
try rewrite → leb_le in *; (* for inequalities *)
auto; try lia.
练习: 2 stars,标准 (if_minus_plus)
用 hoare_if 证明下面的定理。 不要使用 unfold valid_hoare_triple。 我们刚定义的 assertion_auto'' 策略可能有用。
Theorem if_minus_plus :
{{True}}
if (X ≤ Y)
then Z := Y - X
else Y := X + Z
end
{{Y = X + Z}}.
Proof.
(* 在这里填写 *) Admitted.
☐
{{True}}
if (X ≤ Y)
then Z := Y - X
else Y := X + Z
end
{{Y = X + Z}}.
Proof.
(* 在这里填写 *) Admitted.
☐
练习:单侧条件
Module If1.
Inductive com : Type :=
| CSkip : com
| CAsgn : string → aexp → com
| CSeq : com → com → com
| CIf : bexp → com → com → com
| CWhile : bexp → com → com
| CIf1 : bexp → com → com.
Notation "'if1' x 'then' y 'end'" :=
(CIf1 x y)
(in custom com at level 0, x custom com at level 99).
Notation "'skip'" := CSkip
(in custom com at level 0) : com_scope.
Notation "x := y" := (CAsgn x y)
(in custom com at level 0, x constr at level 0, y at level 85, no associativity,
format "x := y") : com_scope.
Notation "x ; y" := (CSeq x y)
(in custom com at level 90,
right associativity,
format "'[v' x ; '/' y ']'") : com_scope.
Notation "'if' x 'then' y 'else' z 'end'" := (CIf x y z)
(in custom com at level 89, x at level 99, y at level 99, z at level 99,
format "'[v' 'if' x 'then' '/ ' y '/' 'else' '/ ' z '/' 'end' ']'") : com_scope.
Notation "'while' x 'do' y 'end'" := (CWhile x y)
(in custom com at level 89, x at level 99, y at level 99,
format "'[v' 'while' x 'do' '/ ' y '/' 'end' ']'") : com_scope.
Inductive com : Type :=
| CSkip : com
| CAsgn : string → aexp → com
| CSeq : com → com → com
| CIf : bexp → com → com → com
| CWhile : bexp → com → com
| CIf1 : bexp → com → com.
Notation "'if1' x 'then' y 'end'" :=
(CIf1 x y)
(in custom com at level 0, x custom com at level 99).
Notation "'skip'" := CSkip
(in custom com at level 0) : com_scope.
Notation "x := y" := (CAsgn x y)
(in custom com at level 0, x constr at level 0, y at level 85, no associativity,
format "x := y") : com_scope.
Notation "x ; y" := (CSeq x y)
(in custom com at level 90,
right associativity,
format "'[v' x ; '/' y ']'") : com_scope.
Notation "'if' x 'then' y 'else' z 'end'" := (CIf x y z)
(in custom com at level 89, x at level 99, y at level 99, z at level 99,
format "'[v' 'if' x 'then' '/ ' y '/' 'else' '/ ' z '/' 'end' ']'") : com_scope.
Notation "'while' x 'do' y 'end'" := (CWhile x y)
(in custom com at level 89, x at level 99, y at level 99,
format "'[v' 'while' x 'do' '/ ' y '/' 'end' ']'") : com_scope.
Inductive ceval : com → state → state → Prop :=
| E_Skip : ∀ st,
st =[ skip ]=> st
| E_Asgn : ∀ st a1 n x,
aeval st a1 = n →
st =[ x := a1 ]=> (x !-> n ; st)
| E_Seq : ∀ c1 c2 st st' st'',
st =[ c1 ]=> st' →
st' =[ c2 ]=> st'' →
st =[ c1 ; c2 ]=> st''
| E_IfTrue : ∀ st st' b c1 c2,
beval st b = true →
st =[ c1 ]=> st' →
st =[ if b then c1 else c2 end ]=> st'
| E_IfFalse : ∀ st st' b c1 c2,
beval st b = false →
st =[ c2 ]=> st' →
st =[ if b then c1 else c2 end ]=> st'
| E_WhileFalse : ∀ b st c,
beval st b = false →
st =[ while b do c end ]=> st
| E_WhileTrue : ∀ st st' st'' b c,
beval st b = true →
st =[ c ]=> st' →
st' =[ while b do c end ]=> st'' →
st =[ while b do c end ]=> st''
(* 在这里填写 *)
where "st '=[' c ']=>' st'" := (ceval c st st').
Hint Constructors ceval : core.
如果你正确定义了 if1 的规则,
下列单元测试应该只需 eauto 就能证明。
Example if1true_test :
empty_st =[ if1 X = 0 then X := 1 end ]=> (X !-> 1).
Proof. (* 在这里填写 *) Admitted.
Example if1false_test :
(X !-> 2) =[ if1 X = 0 then X := 1 end ]=> (X !-> 2).
Proof. (* 在这里填写 *) Admitted.
☐
empty_st =[ if1 X = 0 then X := 1 end ]=> (X !-> 1).
Proof. (* 在这里填写 *) Admitted.
Example if1false_test :
(X !-> 2) =[ if1 X = 0 then X := 1 end ]=> (X !-> 2).
Proof. (* 在这里填写 *) Admitted.
☐
Definition valid_hoare_triple
(P : Assertion) (c : com) (Q : Assertion) : Prop :=
∀ st st',
st =[ c ]=> st' →
P st →
Q st'.
Hint Unfold valid_hoare_triple : core.
Notation "{{ P }} c {{ Q }}" :=
(valid_hoare_triple P c Q)
(at level 2, P custom assn at level 99, c custom com at level 99, Q custom assn at level 99)
: hoare_spec_scope.
(P : Assertion) (c : com) (Q : Assertion) : Prop :=
∀ st st',
st =[ c ]=> st' →
P st →
Q st'.
Hint Unfold valid_hoare_triple : core.
Notation "{{ P }} c {{ Q }}" :=
(valid_hoare_triple P c Q)
(at level 2, P custom assn at level 99, c custom com at level 99, Q custom assn at level 99)
: hoare_spec_scope.
练习: 2 stars,标准 (hoare_if1)
(* 在这里填写 *)
例如(hoare_if1_good),你的规则应当足够强,
能表明下列霍尔三元组成立:
{{ X + Y = Z }}
if1 Y ≠ 0 then
X := X + Y
end
{{ X = Z }}
{{ X + Y = Z }}
if1 Y ≠ 0 then
X := X + Y
end
{{ X = Z }}
Theorem hoare_consequence_pre : ∀ (P P' Q : Assertion) c,
{{P'}} c {{Q}} →
P ->> P' →
{{P}} c {{Q}}.
Proof.
eauto.
Qed.
Theorem hoare_asgn : ∀ Q X a,
{{Q [X ⊢> a]}} (X := a) {{Q}}.
Proof.
intros Q X a st st' Heval HQ.
inversion Heval; subst.
auto.
Qed.
{{P'}} c {{Q}} →
P ->> P' →
{{P}} c {{Q}}.
Proof.
eauto.
Qed.
Theorem hoare_asgn : ∀ Q X a,
{{Q [X ⊢> a]}} (X := a) {{Q}}.
Proof.
intros Q X a st st' Heval HQ.
inversion Heval; subst.
auto.
Qed.
练习: 2 stars,标准 (hoare_if1_good)
Lemma hoare_if1_good :
{{ X + Y = Z }}
if1 Y ≠ 0 then
X := X + Y
end
{{ X = Z }}.
Proof. (* 在这里填写 *) Admitted.
☐
{{ X + Y = Z }}
if1 Y ≠ 0 then
X := X + Y
end
{{ X = Z }}.
Proof. (* 在这里填写 *) Admitted.
☐
While 循环
{{P}} c {{P}} 成立。注意命令不变式在 c 执行的中途 可能会暂时变为假,但到 c 结束时它必须被恢复。
{{P}} c {{P}}
---------------------------
{{P} while b do c end {{P}}
{{P}} c {{P}}
---------------------------------
{{P} while b do c end {{P ∧ ¬b}}
{{P ∧ b}} c {{P}}
--------------------------------- (hoare_while)
{{P} while b do c end {{P ∧ ¬b}}
- 如果循环体执行零次,该规则就像 skip 一样,
前置条件存活下来成为(后置条件的)一部分。
- 像条件语句一样,我们可以假定进入子命令时 卫条件 b 成立。
Theorem hoare_while : ∀ P (b:bexp) c,
{{P ∧ b}} c {{P}} →
{{P}} while b do c end {{P ∧ ¬ b}}.
{{P ∧ b}} c {{P}} →
{{P}} while b do c end {{P ∧ ¬ b}}.
Proof.
intros P b c Hhoare st st' Heval HP.
(* 我们对 Heval 做归纳,因为在「继续循环」的情形中,
它的假设谈论的是整个循环而不仅仅是 c。
remember 用来把原始命令保留在假设中;
否则它会在 induction 中丢失。
通过使用 inversion,我们清除了除涉及 while
以外的所有情形。 *)
remember <{while b do c end}> as original_command eqn:Horig.
induction Heval;
try (inversion Horig; subst; clear Horig);
eauto.
Qed.
intros P b c Hhoare st st' Heval HP.
(* 我们对 Heval 做归纳,因为在「继续循环」的情形中,
它的假设谈论的是整个循环而不仅仅是 c。
remember 用来把原始命令保留在假设中;
否则它会在 induction 中丢失。
通过使用 inversion,我们清除了除涉及 while
以外的所有情形。 *)
remember <{while b do c end}> as original_command eqn:Horig.
induction Heval;
try (inversion Horig; subst; clear Horig);
eauto.
Qed.
我们称 P 是 while b do c end 的一个
【循环不变式|Loop Invariant】,如果
{{P ∧ b}} c {{P}} 是一个成立的霍尔三元组。
这意味着只要循环体执行,
P 在循环体结束时都将为真。
如果 P 与 b 矛盾,
由于前置条件为假,这平凡地成立。
例如,X = 0 是
while X = 2 do X := 1 end 的一个循环不变式, 因为程序永远不会进入循环。
程序
while Y > 10 do Y := Y - 1; Z := Z + 1 end 有一个有趣的循环不变式:
X = Y + Z 注意这与循环卫条件不矛盾, 但它也不是
Y := Y - 1; Z := Z + 1 的命令不变式。 如果 X = 5、Y = 0 且 Z = 5, 运行该命令会把 Y + Z 设为 6。 循环卫条件 Y > 10 保证这不会发生。 我们将在下一章看到许多这样的循环不变式。
{{P ∧ b}} c {{P}} 是一个成立的霍尔三元组。
while X = 2 do X := 1 end 的一个循环不变式, 因为程序永远不会进入循环。
while Y > 10 do Y := Y - 1; Z := Z + 1 end 有一个有趣的循环不变式:
X = Y + Z 注意这与循环卫条件不矛盾, 但它也不是
Y := Y - 1; Z := Z + 1 的命令不变式。 如果 X = 5、Y = 0 且 Z = 5, 运行该命令会把 Y + Z 设为 6。 循环卫条件 Y > 10 保证这不会发生。 我们将在下一章看到许多这样的循环不变式。
如果循环永不停机,任何后置条件都可以。
当然,如果我们记得 valid_hoare_triple 的定义:
后置条件【只】在命令停机时才必须成立,
这个结果并不令人惊讶。
如果命令不停机,
我们可以证明关于后置条件的任何我们喜欢的东西。
霍尔规则只规定命令【如果】停机会发生什么,
却不证明它们确实停机。
这样的逻辑被称为【部分】正确性逻辑。
也可以给出【完全】正确性的霍尔规则,
它额外规定命令必须停机。
完全正确性超出了本教科书的范围。
练习:REPEAT
练习: 4 stars,高级,可选 (hoare_repeat)
在这个练习中,我们将给命令语言加入一个新命令: REPEAT c until b end。 你将写出 REPEAT 的求值规则, 并为涉及它的程序在语言中加入一条新的霍尔规则。 (你可能记得求值规则在 Auto 一章的 一个示例中给出。试着自己在这里想出来,别偷看。)
Module RepeatExercise.
Inductive com : Type :=
| CSkip : com
| CAsgn : string → aexp → com
| CSeq : com → com → com
| CIf : bexp → com → com → com
| CWhile : bexp → com → com
| CRepeat : com → bexp → com.
Inductive com : Type :=
| CSkip : com
| CAsgn : string → aexp → com
| CSeq : com → com → com
| CIf : bexp → com → com → com
| CWhile : bexp → com → com
| CRepeat : com → bexp → com.
REPEAT 的行为类似 while。
区别在于:循环卫条件是在每次执行循环体_
之后_检查的。
只要卫条件保持【假】,循环就重复。
因此,循环体将总是至少执行一次。
Notation "'repeat' e1 'until' b2 'end'" :=
(CRepeat e1 b2)
(in custom com at level 0,
e1 custom com at level 99, b2 custom com at level 99).
Notation "'skip'" := CSkip
(in custom com at level 0) : com_scope.
Notation "x := y" := (CAsgn x y)
(in custom com at level 0, x constr at level 0, y at level 85, no associativity,
format "x := y") : com_scope.
Notation "x ; y" := (CSeq x y)
(in custom com at level 90,
right associativity,
format "'[v' x ; '/' y ']'") : com_scope.
Notation "'if' x 'then' y 'else' z 'end'" := (CIf x y z)
(in custom com at level 89, x at level 99, y at level 99, z at level 99,
format "'[v' 'if' x 'then' '/ ' y '/' 'else' '/ ' z '/' 'end' ']'") : com_scope.
Notation "'while' x 'do' y 'end'" := (CWhile x y)
(in custom com at level 89, x at level 99, y at level 99,
format "'[v' 'while' x 'do' '/ ' y '/' 'end' ']'") : com_scope.
(CRepeat e1 b2)
(in custom com at level 0,
e1 custom com at level 99, b2 custom com at level 99).
Notation "'skip'" := CSkip
(in custom com at level 0) : com_scope.
Notation "x := y" := (CAsgn x y)
(in custom com at level 0, x constr at level 0, y at level 85, no associativity,
format "x := y") : com_scope.
Notation "x ; y" := (CSeq x y)
(in custom com at level 90,
right associativity,
format "'[v' x ; '/' y ']'") : com_scope.
Notation "'if' x 'then' y 'else' z 'end'" := (CIf x y z)
(in custom com at level 89, x at level 99, y at level 99, z at level 99,
format "'[v' 'if' x 'then' '/ ' y '/' 'else' '/ ' z '/' 'end' ']'") : com_scope.
Notation "'while' x 'do' y 'end'" := (CWhile x y)
(in custom com at level 89, x at level 99, y at level 99,
format "'[v' 'while' x 'do' '/ ' y '/' 'end' ']'") : com_scope.
给下面的 ceval 加入 REPEAT 的新规则。
你可以用 while 的规则作指导,
但记住 REPEAT 的循环体应总是至少执行一次,
且循环在卫条件变为真时结束。
Inductive ceval : state → com → state → Prop :=
| E_Skip : ∀ st,
st =[ skip ]=> st
| E_Asgn : ∀ st a1 n x,
aeval st a1 = n →
st =[ x := a1 ]=> (x !-> n ; st)
| E_Seq : ∀ c1 c2 st st' st'',
st =[ c1 ]=> st' →
st' =[ c2 ]=> st'' →
st =[ c1 ; c2 ]=> st''
| E_IfTrue : ∀ st st' b c1 c2,
beval st b = true →
st =[ c1 ]=> st' →
st =[ if b then c1 else c2 end ]=> st'
| E_IfFalse : ∀ st st' b c1 c2,
beval st b = false →
st =[ c2 ]=> st' →
st =[ if b then c1 else c2 end ]=> st'
| E_WhileFalse : ∀ b st c,
beval st b = false →
st =[ while b do c end ]=> st
| E_WhileTrue : ∀ st st' st'' b c,
beval st b = true →
st =[ c ]=> st' →
st' =[ while b do c end ]=> st'' →
st =[ while b do c end ]=> st''
(* 在这里填写 *)
where "st '=[' c ']=>' st'" := (ceval st c st').
| E_Skip : ∀ st,
st =[ skip ]=> st
| E_Asgn : ∀ st a1 n x,
aeval st a1 = n →
st =[ x := a1 ]=> (x !-> n ; st)
| E_Seq : ∀ c1 c2 st st' st'',
st =[ c1 ]=> st' →
st' =[ c2 ]=> st'' →
st =[ c1 ; c2 ]=> st''
| E_IfTrue : ∀ st st' b c1 c2,
beval st b = true →
st =[ c1 ]=> st' →
st =[ if b then c1 else c2 end ]=> st'
| E_IfFalse : ∀ st st' b c1 c2,
beval st b = false →
st =[ c2 ]=> st' →
st =[ if b then c1 else c2 end ]=> st'
| E_WhileFalse : ∀ b st c,
beval st b = false →
st =[ while b do c end ]=> st
| E_WhileTrue : ∀ st st' st'' b c,
beval st b = true →
st =[ c ]=> st' →
st' =[ while b do c end ]=> st'' →
st =[ while b do c end ]=> st''
(* 在这里填写 *)
where "st '=[' c ']=>' st'" := (ceval st c st').
上面的一些定义抄到这里,使它们使用新的 ceval。
Definition valid_hoare_triple (P : Assertion) (c : com) (Q : Assertion)
: Prop :=
∀ st st', st =[ c ]=> st' → P st → Q st'.
Notation "{{ P }} c {{ Q }}" :=
(valid_hoare_triple P c Q)
(at level 2, P custom assn at level 99, c custom com at level 99, Q custom assn at level 99)
: hoare_spec_scope.
: Prop :=
∀ st st', st =[ c ]=> st' → P st → Q st'.
Notation "{{ P }} c {{ Q }}" :=
(valid_hoare_triple P c Q)
(at level 2, P custom assn at level 99, c custom com at level 99, Q custom assn at level 99)
: hoare_spec_scope.
为确保你正确写出了 repeat 的求值规则,
证明 ex1_repeat 正确求值。
Definition ex1_repeat :=
<{ repeat
X := 1;
Y := Y + 1
until (X = 1) end }>.
Theorem ex1_repeat_works :
empty_st =[ ex1_repeat ]=> (Y !-> 1 ; X !-> 1).
Proof.
(* 在这里填写 *) Admitted.
<{ repeat
X := 1;
Y := Y + 1
until (X = 1) end }>.
Theorem ex1_repeat_works :
empty_st =[ ex1_repeat ]=> (Y !-> 1 ; X !-> 1).
Proof.
(* 在这里填写 *) Admitted.
现在陈述并证明一个定理 hoare_repeat,
表达 repeat 命令的一条合适的证明规则。
以 hoare_while 为模型,
试着让你的规则尽可能精确。
(* 在这里填写 *)
为拿到全部分数,请确保(非形式化地)
你的规则能用来证明下列成立的霍尔三元组:
{{ X > 0 }}
repeat
Y := X;
X := X - 1
until X = 0 end
{{ X = 0 ∧ Y > 0 }}
{{ X > 0 }}
repeat
Y := X;
X := X - 1
until X = 0 end
{{ X = 0 ∧ Y > 0 }}
End RepeatExercise.
(* 不要修改下面这行: *)
Definition manual_grade_for_hoare_repeat : option (nat×string) := None.
☐
(* 不要修改下面这行: *)
Definition manual_grade_for_hoare_repeat : option (nat×string) := None.
☐
总结
| (hoare_asgn) | |
| {{Q [X ⊢> a]}} X:=a {{Q}} |
| (hoare_skip) | |
| {{ P }} skip {{ P }} |
| {{ 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}} |
附加练习
Havoc
Module Himp.
Inductive com : Type :=
| CSkip : com
| CAsgn : string → aexp → com
| CSeq : com → com → com
| CIf : bexp → com → com → com
| CWhile : bexp → com → com
| CHavoc : string → com.
Notation "'havoc' l" := (CHavoc l)
(in custom com at level 60, l constr at level 0).
Notation "'skip'" := CSkip
(in custom com at level 0) : com_scope.
Notation "x := y" := (CAsgn x y)
(in custom com at level 0, x constr at level 0, y at level 85, no associativity,
format "x := y") : com_scope.
Notation "x ; y" := (CSeq x y)
(in custom com at level 90,
right associativity,
format "'[v' x ; '/' y ']'") : com_scope.
Notation "'if' x 'then' y 'else' z 'end'" := (CIf x y z)
(in custom com at level 89, x at level 99, y at level 99, z at level 99,
format "'[v' 'if' x 'then' '/ ' y '/' 'else' '/ ' z '/' 'end' ']'") : com_scope.
Notation "'while' x 'do' y 'end'" := (CWhile x y)
(in custom com at level 89, x at level 99, y at level 99,
format "'[v' 'while' x 'do' '/ ' y '/' 'end' ']'") : com_scope.
Inductive ceval : com → state → state → Prop :=
| E_Skip : ∀ st,
st =[ skip ]=> st
| E_Asgn : ∀ st a1 n x,
aeval st a1 = n →
st =[ x := a1 ]=> (x !-> n ; st)
| E_Seq : ∀ c1 c2 st st' st'',
st =[ c1 ]=> st' →
st' =[ c2 ]=> st'' →
st =[ c1 ; c2 ]=> st''
| E_IfTrue : ∀ st st' b c1 c2,
beval st b = true →
st =[ c1 ]=> st' →
st =[ if b then c1 else c2 end ]=> st'
| E_IfFalse : ∀ st st' b c1 c2,
beval st b = false →
st =[ c2 ]=> st' →
st =[ if b then c1 else c2 end ]=> st'
| E_WhileFalse : ∀ b st c,
beval st b = false →
st =[ while b do c end ]=> st
| E_WhileTrue : ∀ st st' st'' b c,
beval st b = true →
st =[ c ]=> st' →
st' =[ while b do c end ]=> st'' →
st =[ while b do c end ]=> st''
| E_Havoc : ∀ st x n,
st =[ havoc x ]=> (x !-> n ; st)
where "st '=[' c ']=>' st'" := (ceval c st st').
Hint Constructors ceval : core.
Inductive com : Type :=
| CSkip : com
| CAsgn : string → aexp → com
| CSeq : com → com → com
| CIf : bexp → com → com → com
| CWhile : bexp → com → com
| CHavoc : string → com.
Notation "'havoc' l" := (CHavoc l)
(in custom com at level 60, l constr at level 0).
Notation "'skip'" := CSkip
(in custom com at level 0) : com_scope.
Notation "x := y" := (CAsgn x y)
(in custom com at level 0, x constr at level 0, y at level 85, no associativity,
format "x := y") : com_scope.
Notation "x ; y" := (CSeq x y)
(in custom com at level 90,
right associativity,
format "'[v' x ; '/' y ']'") : com_scope.
Notation "'if' x 'then' y 'else' z 'end'" := (CIf x y z)
(in custom com at level 89, x at level 99, y at level 99, z at level 99,
format "'[v' 'if' x 'then' '/ ' y '/' 'else' '/ ' z '/' 'end' ']'") : com_scope.
Notation "'while' x 'do' y 'end'" := (CWhile x y)
(in custom com at level 89, x at level 99, y at level 99,
format "'[v' 'while' x 'do' '/ ' y '/' 'end' ']'") : com_scope.
Inductive ceval : com → state → state → Prop :=
| E_Skip : ∀ st,
st =[ skip ]=> st
| E_Asgn : ∀ st a1 n x,
aeval st a1 = n →
st =[ x := a1 ]=> (x !-> n ; st)
| E_Seq : ∀ c1 c2 st st' st'',
st =[ c1 ]=> st' →
st' =[ c2 ]=> st'' →
st =[ c1 ; c2 ]=> st''
| E_IfTrue : ∀ st st' b c1 c2,
beval st b = true →
st =[ c1 ]=> st' →
st =[ if b then c1 else c2 end ]=> st'
| E_IfFalse : ∀ st st' b c1 c2,
beval st b = false →
st =[ c2 ]=> st' →
st =[ if b then c1 else c2 end ]=> st'
| E_WhileFalse : ∀ b st c,
beval st b = false →
st =[ while b do c end ]=> st
| E_WhileTrue : ∀ st st' st'' b c,
beval st b = true →
st =[ c ]=> st' →
st' =[ while b do c end ]=> st'' →
st =[ while b do c end ]=> st''
| E_Havoc : ∀ st x n,
st =[ havoc x ]=> (x !-> n ; st)
where "st '=[' c ']=>' st'" := (ceval c st st').
Hint Constructors ceval : core.
霍尔三元组的定义与之前完全一样。
Definition valid_hoare_triple (P:Assertion) (c:com) (Q:Assertion) : Prop :=
∀ st st', st =[ c ]=> st' → P st → Q st'.
Hint Unfold valid_hoare_triple : core.
Notation "{{ P }} c {{ Q }}" :=
(valid_hoare_triple P c Q)
(at level 2, P custom assn at level 99, c custom com at level 99, Q custom assn at level 99)
: hoare_spec_scope.
∀ st st', st =[ c ]=> st' → P st → Q st'.
Hint Unfold valid_hoare_triple : core.
Notation "{{ P }} c {{ Q }}" :=
(valid_hoare_triple P c Q)
(at level 2, P custom assn at level 99, c custom com at level 99, Q custom assn at level 99)
: hoare_spec_scope.
前置条件的后果规则与之前完全一样。
Theorem hoare_consequence_pre : ∀ (P P' Q : Assertion) c,
{{P'}} c {{Q}} →
P ->> P' →
{{P}} c {{Q}}.
Proof. eauto. Qed.
{{P'}} c {{Q}} →
P ->> P' →
{{P}} c {{Q}}.
Proof. eauto. Qed.
Definition havoc_pre (X : string) (Q : Assertion) (st : total_map nat) : Prop
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Theorem hoare_havoc : ∀ (Q : Assertion) (X : string),
{{ $(havoc_pre X Q) }} havoc X {{ Q }}.
Proof.
(* 在这里填写 *) Admitted.
☐
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Theorem hoare_havoc : ∀ (Q : Assertion) (X : string),
{{ $(havoc_pre X Q) }} havoc X {{ Q }}.
Proof.
(* 在这里填写 *) Admitted.
☐
练习: 3 stars,高级 (havoc_post)
在不改变任何所给命令的情况下完成下列证明。 如果你发现它无法完成, 你对 havoc_pre 的定义可能太强了。 找一种放松它的方法,使 havoc_post 能被证明。
Theorem havoc_post : ∀ (P : Assertion) (X : string),
{{ P }} havoc X {{ $(fun st ⇒ ∃ (n:nat), ({{P [X ⊢> n] }}) st) }}.
Proof.
intros P X. eapply hoare_consequence_pre.
- apply hoare_havoc.
- (* 在这里填写 *) Admitted.
☐
{{ P }} havoc X {{ $(fun st ⇒ ∃ (n:nat), ({{P [X ⊢> n] }}) st) }}.
Proof.
intros P X. eapply hoare_consequence_pre.
- apply hoare_havoc.
- (* 在这里填写 *) Admitted.
☐
断言与假定
练习: 4 stars,标准 (assert_vs_assume)
在这个练习中,我们将用两个命令 assert 和 assume 扩展 IMP。这两个命令都是用来指明: 只要程序执行到达这部分, 某个断言就应当成立。但它们有如下区别:- 如果 assert 语句失败,
它使程序进入错误状态并退出。
- 如果 assume 语句失败,程序根本无法求值。 换句话说,程序卡住了,没有终止状态。
Module HoareAssertAssume.
Inductive com : Type :=
| CSkip : com
| CAsgn : string → aexp → com
| CSeq : com → com → com
| CIf : bexp → com → com → com
| CWhile : bexp → com → com
| CAssert : bexp → com
| CAssume : bexp → com.
Notation "'assert' l" := (CAssert l)
(in custom com at level 8, l custom com at level 0).
Notation "'assume' l" := (CAssume l)
(in custom com at level 8, l custom com at level 0).
Notation "'skip'" := CSkip
(in custom com at level 0) : com_scope.
Notation "x := y" := (CAsgn x y)
(in custom com at level 0, x constr at level 0, y at level 85, no associativity,
format "x := y") : com_scope.
Notation "x ; y" := (CSeq x y)
(in custom com at level 90,
right associativity,
format "'[v' x ; '/' y ']'") : com_scope.
Notation "'if' x 'then' y 'else' z 'end'" := (CIf x y z)
(in custom com at level 89, x at level 99, y at level 99, z at level 99,
format "'[v' 'if' x 'then' '/ ' y '/' 'else' '/ ' z '/' 'end' ']'") : com_scope.
Notation "'while' x 'do' y 'end'" := (CWhile x y)
(in custom com at level 89, x at level 99, y at level 99,
format "'[v' 'while' x 'do' '/ ' y '/' 'end' ']'") : com_scope.
Inductive com : Type :=
| CSkip : com
| CAsgn : string → aexp → com
| CSeq : com → com → com
| CIf : bexp → com → com → com
| CWhile : bexp → com → com
| CAssert : bexp → com
| CAssume : bexp → com.
Notation "'assert' l" := (CAssert l)
(in custom com at level 8, l custom com at level 0).
Notation "'assume' l" := (CAssume l)
(in custom com at level 8, l custom com at level 0).
Notation "'skip'" := CSkip
(in custom com at level 0) : com_scope.
Notation "x := y" := (CAsgn x y)
(in custom com at level 0, x constr at level 0, y at level 85, no associativity,
format "x := y") : com_scope.
Notation "x ; y" := (CSeq x y)
(in custom com at level 90,
right associativity,
format "'[v' x ; '/' y ']'") : com_scope.
Notation "'if' x 'then' y 'else' z 'end'" := (CIf x y z)
(in custom com at level 89, x at level 99, y at level 99, z at level 99,
format "'[v' 'if' x 'then' '/ ' y '/' 'else' '/ ' z '/' 'end' ']'") : com_scope.
Notation "'while' x 'do' y 'end'" := (CWhile x y)
(in custom com at level 89, x at level 99, y at level 99,
format "'[v' 'while' x 'do' '/ ' y '/' 'end' ']'") : com_scope.
为定义 assert 和 assume 的行为,
我们需要为错误加入记号,
它指明某个断言失败了。
因此我们修改 ceval 关系,
使它把一个开始状态关联到一个终止状态或 error。
result 类型指明程序的结束值,
要么是状态要么是错误:
现在我们准备给出新语言的 ceval 关系。
Inductive ceval : com → state → result → Prop :=
(* 稍加修改的旧有规则 *)
| E_Skip : ∀ st,
st =[ skip ]=> RNormal st
| E_Asgn : ∀ st a1 n x,
aeval st a1 = n →
st =[ x := a1 ]=> RNormal (x !-> n ; st)
| E_SeqNormal : ∀ c1 c2 st st' r,
st =[ c1 ]=> RNormal st' →
st' =[ c2 ]=> r →
st =[ c1 ; c2 ]=> r
| E_SeqError : ∀ c1 c2 st,
st =[ c1 ]=> RError →
st =[ c1 ; c2 ]=> RError
| E_IfTrue : ∀ st r b c1 c2,
beval st b = true →
st =[ c1 ]=> r →
st =[ if b then c1 else c2 end ]=> r
| E_IfFalse : ∀ st r b c1 c2,
beval st b = false →
st =[ c2 ]=> r →
st =[ if b then c1 else c2 end ]=> r
| E_WhileFalse : ∀ b st c,
beval st b = false →
st =[ while b do c end ]=> RNormal st
| E_WhileTrueNormal : ∀ st st' r b c,
beval st b = true →
st =[ c ]=> RNormal st' →
st' =[ while b do c end ]=> r →
st =[ while b do c end ]=> r
| E_WhileTrueError : ∀ st b c,
beval st b = true →
st =[ c ]=> RError →
st =[ while b do c end ]=> RError
(* Assert 和 Assume 的规则 *)
| E_AssertTrue : ∀ st b,
beval st b = true →
st =[ assert b ]=> RNormal st
| E_AssertFalse : ∀ st b,
beval st b = false →
st =[ assert b ]=> RError
| E_Assume : ∀ st b,
beval st b = true →
st =[ assume b ]=> RNormal st
where "st '=[' c ']=>' r" := (ceval c st r).
(* 稍加修改的旧有规则 *)
| E_Skip : ∀ st,
st =[ skip ]=> RNormal st
| E_Asgn : ∀ st a1 n x,
aeval st a1 = n →
st =[ x := a1 ]=> RNormal (x !-> n ; st)
| E_SeqNormal : ∀ c1 c2 st st' r,
st =[ c1 ]=> RNormal st' →
st' =[ c2 ]=> r →
st =[ c1 ; c2 ]=> r
| E_SeqError : ∀ c1 c2 st,
st =[ c1 ]=> RError →
st =[ c1 ; c2 ]=> RError
| E_IfTrue : ∀ st r b c1 c2,
beval st b = true →
st =[ c1 ]=> r →
st =[ if b then c1 else c2 end ]=> r
| E_IfFalse : ∀ st r b c1 c2,
beval st b = false →
st =[ c2 ]=> r →
st =[ if b then c1 else c2 end ]=> r
| E_WhileFalse : ∀ b st c,
beval st b = false →
st =[ while b do c end ]=> RNormal st
| E_WhileTrueNormal : ∀ st st' r b c,
beval st b = true →
st =[ c ]=> RNormal st' →
st' =[ while b do c end ]=> r →
st =[ while b do c end ]=> r
| E_WhileTrueError : ∀ st b c,
beval st b = true →
st =[ c ]=> RError →
st =[ while b do c end ]=> RError
(* Assert 和 Assume 的规则 *)
| E_AssertTrue : ∀ st b,
beval st b = true →
st =[ assert b ]=> RNormal st
| E_AssertFalse : ∀ st b,
beval st b = false →
st =[ assert b ]=> RError
| E_Assume : ∀ st b,
beval st b = true →
st =[ assume b ]=> RNormal st
where "st '=[' c ']=>' r" := (ceval c st r).
我们重新定义霍尔三元组:
现在 {{P}} c {{Q}} 的意思是,
只要 c 在满足 P 的状态中启动
并以结果 r 停机,
那么 r 不是错误且 r 的状态满足 Q。
Definition valid_hoare_triple
(P : Assertion) (c : com) (Q : Assertion) : Prop :=
∀ st r,
st =[ c ]=> r → P st →
(∃ st', r = RNormal st' ∧ Q st').
Notation "{{ P }} c {{ Q }}" :=
(valid_hoare_triple P c Q)
(at level 2, P custom assn at level 99, c custom com at level 99, Q custom assn at level 99)
: hoare_spec_scope.
(P : Assertion) (c : com) (Q : Assertion) : Prop :=
∀ st r,
st =[ c ]=> r → P st →
(∃ st', r = RNormal st' ∧ Q st').
Notation "{{ P }} c {{ Q }}" :=
(valid_hoare_triple P c Q)
(at level 2, P custom assn at level 99, c custom com at level 99, Q custom assn at level 99)
: hoare_spec_scope.
为测试你对这一修改的理解,
给出一组被 assume 语句满足
却不被 assert 语句满足的前置条件和后置条件。
Theorem assert_assume_differ : ∃ (P:Assertion) b (Q:Assertion),
({{P}} assume b {{Q}})
∧ ¬ ({{P}} assert b {{Q}}).
(* 在这里填写 *) Admitted.
({{P}} assume b {{Q}})
∧ ¬ ({{P}} assert b {{Q}}).
(* 在这里填写 *) Admitted.
然后证明:任何 assert 的三元组
在把 assert 换成 assume 时也成立。
Theorem assert_implies_assume : ∀ P b Q,
({{P}} assert b {{Q}})
→ ({{P}} assume b {{Q}}).
Proof.
(* 在这里填写 *) Admitted.
({{P}} assert b {{Q}})
→ ({{P}} assume b {{Q}}).
Proof.
(* 在这里填写 *) Admitted.
接下来,下面是适配到新语义的旧霍尔规则的证明。
你不需要对这些做任何事。
Theorem hoare_asgn : ∀ Q X a,
{{Q [X ⊢> a]}} X := a {{Q}}.
Theorem hoare_consequence_pre : ∀ (P P' Q : Assertion) c,
Theorem hoare_consequence_post : ∀ (P Q Q' : Assertion) c,
{{P}} c {{Q'}} →
Q' ->> Q →
{{P}} c {{Q}}.
Theorem hoare_seq : ∀ P Q R c1 c2,
{{Q}} c2 {{R}} →
{{P}} c1 {{Q}} →
{{P}} c1;c2 {{R}}.
{{Q [X ⊢> a]}} X := a {{Q}}.
Proof.
unfold valid_hoare_triple.
intros Q X a st st' HE HQ.
inversion HE. subst.
∃ (X !-> aeval st a ; st). split; try reflexivity.
assumption. Qed.
unfold valid_hoare_triple.
intros Q X a st st' HE HQ.
inversion HE. subst.
∃ (X !-> aeval st a ; st). split; try reflexivity.
assumption. Qed.
Theorem hoare_consequence_pre : ∀ (P P' Q : Assertion) c,
{{P'}} c {{Q}} →
P ->> P' →
{{P}} c {{Q}}.
Proof.
intros P P' Q c Hhoare Himp.
intros st st' Hc HP. apply (Hhoare st st').
- assumption.
- apply Himp. assumption. Qed.
P ->> P' →
{{P}} c {{Q}}.
Proof.
intros P P' Q c Hhoare Himp.
intros st st' Hc HP. apply (Hhoare st st').
- assumption.
- apply Himp. assumption. Qed.
Theorem hoare_consequence_post : ∀ (P Q Q' : Assertion) c,
{{P}} c {{Q'}} →
Q' ->> Q →
{{P}} c {{Q}}.
Proof.
intros P Q Q' c Hhoare Himp.
intros st r Hc HP.
unfold valid_hoare_triple in Hhoare.
assert (∃ st', r = RNormal st' ∧ Q' st').
{ apply (Hhoare st); assumption. }
destruct H as [st' [Hr HQ'] ].
∃ st'. split; try assumption.
apply Himp. assumption.
Qed.
intros P Q Q' c Hhoare Himp.
intros st r Hc HP.
unfold valid_hoare_triple in Hhoare.
assert (∃ st', r = RNormal st' ∧ Q' st').
{ apply (Hhoare st); assumption. }
destruct H as [st' [Hr HQ'] ].
∃ st'. split; try assumption.
apply Himp. assumption.
Qed.
Theorem hoare_seq : ∀ P Q R c1 c2,
{{Q}} c2 {{R}} →
{{P}} c1 {{Q}} →
{{P}} c1;c2 {{R}}.
Proof.
intros P Q R c1 c2 H1 H2 st r H12 Pre.
inversion H12; subst.
- eapply H1.
+ apply H6.
+ apply H2 in H3. apply H3 in Pre.
destruct Pre as [st'0 [Heq HQ] ].
inversion Heq; subst. assumption.
- (* 找到矛盾的假设 *)
apply H2 in H5. apply H5 in Pre.
destruct Pre as [st' [C _] ].
inversion C.
Qed.
intros P Q R c1 c2 H1 H2 st r H12 Pre.
inversion H12; subst.
- eapply H1.
+ apply H6.
+ apply H2 in H3. apply H3 in Pre.
destruct Pre as [st'0 [Heq HQ] ].
inversion Heq; subst. assumption.
- (* 找到矛盾的假设 *)
apply H2 in H5. apply H5 in Pre.
destruct Pre as [st' [C _] ].
inversion C.
Qed.
下面是其他证明规则(健全性检查)
Theorem hoare_skip : ∀ P,
{{P}} skip {{P}}.
Theorem hoare_if : ∀ P Q (b:bexp) c1 c2,
{{ P ∧ b}} c1 {{Q}} →
{{ P ∧ ¬ b}} c2 {{Q}} →
{{P}} if b then c1 else c2 end {{Q}}.
Theorem hoare_while : ∀ P (b:bexp) c,
{{P ∧ b}} c {{P}} →
{{P}} while b do c end {{ P ∧ ¬b}}.
{{P}} skip {{P}}.
Proof.
intros P st st' H HP. inversion H. subst.
eexists. split.
- reflexivity.
- assumption.
Qed.
intros P st st' H HP. inversion H. subst.
eexists. split.
- reflexivity.
- assumption.
Qed.
Theorem hoare_if : ∀ P Q (b:bexp) c1 c2,
{{ P ∧ b}} c1 {{Q}} →
{{ P ∧ ¬ b}} c2 {{Q}} →
{{P}} if b then c1 else c2 end {{Q}}.
Proof.
intros P Q b c1 c2 HTrue HFalse st st' HE HP.
inversion HE; subst.
- (* b is true *)
apply (HTrue st st').
+ assumption.
+ split; assumption.
- (* b is false *)
apply (HFalse st st').
+ assumption.
+ split.
× assumption.
× apply bexp_eval_false. assumption.
Qed.
intros P Q b c1 c2 HTrue HFalse st st' HE HP.
inversion HE; subst.
- (* b is true *)
apply (HTrue st st').
+ assumption.
+ split; assumption.
- (* b is false *)
apply (HFalse st st').
+ assumption.
+ split.
× assumption.
× apply bexp_eval_false. assumption.
Qed.
Theorem hoare_while : ∀ P (b:bexp) c,
{{P ∧ b}} c {{P}} →
{{P}} while b do c end {{ P ∧ ¬b}}.
Proof.
intros P b c Hhoare st st' He HP.
remember <{while b do c end}> as wcom eqn:Heqwcom.
induction He;
try (inversion Heqwcom); subst; clear Heqwcom.
- (* E_WhileFalse *)
eexists. split.
+ reflexivity.
+ split; try assumption.
apply bexp_eval_false. assumption.
- (* E_WhileTrueNormal *)
clear IHHe1.
apply IHHe2.
+ reflexivity.
+ clear IHHe2 He2 r.
unfold valid_hoare_triple in Hhoare.
apply Hhoare in He1.
× destruct He1 as [st1 [Heq Hst1] ].
inversion Heq; subst.
assumption.
× split; assumption.
- (* E_WhileTrueError *)
exfalso. clear IHHe.
unfold valid_hoare_triple in Hhoare.
apply Hhoare in He.
+ destruct He as [st' [C _] ]. inversion C.
+ split; assumption.
Qed.
intros P b c Hhoare st st' He HP.
remember <{while b do c end}> as wcom eqn:Heqwcom.
induction He;
try (inversion Heqwcom); subst; clear Heqwcom.
- (* E_WhileFalse *)
eexists. split.
+ reflexivity.
+ split; try assumption.
apply bexp_eval_false. assumption.
- (* E_WhileTrueNormal *)
clear IHHe1.
apply IHHe2.
+ reflexivity.
+ clear IHHe2 He2 r.
unfold valid_hoare_triple in Hhoare.
apply Hhoare in He1.
× destruct He1 as [st1 [Heq Hst1] ].
inversion Heq; subst.
assumption.
× split; assumption.
- (* E_WhileTrueError *)
exfalso. clear IHHe.
unfold valid_hoare_triple in Hhoare.
apply Hhoare in He.
+ destruct He as [st' [C _] ]. inversion C.
+ split; assumption.
Qed.
最后,为 assert 和 assume 陈述霍尔规则,
并用它们证明一个简单程序正确。
把你的规则命名为 hoare_assert 和 hoare_assume。
(* 在这里填写 *)
用你的规则证明下列三元组。
Example assert_assume_example:
{{True}}
assume (X = 1);
X := X + 1;
assert (X = 2)
{{True}}.
Proof.
(* 在这里填写 *) Admitted.
End HoareAssertAssume.
☐
{{True}}
assume (X = 1);
X := X + 1;
assert (X = 2)
{{True}}.
Proof.
(* 在这里填写 *) Admitted.
End HoareAssertAssume.
☐
(* 2026-11-26 15:55 *)
