Verif_append2魔杖与部分数据结构
分离蕴含
Require VC.Preface. (* 检查 VST 的正确版本 *)
Require Import VST.floyd.proofauto.
Locate "−∗".
(* Notation "P '−∗' Q" := wand P Q : logic (default interpretation) *)
Check wand. (* : mpred -> mpred -> mpred *)
Require Import VST.floyd.proofauto.
Locate "−∗".
(* Notation "P '−∗' Q" := wand P Q : logic (default interpretation) *)
Check wand. (* : mpred -> mpred -> mpred *)
在分离逻辑中,【堆片|Heaplet】(一片内存)m 满足 P −∗ Q
当且仅当:对任意可能的堆片 n,若 n 与 m 不相交,
且 n 满足 P,则 n 与 m 的组合将满足 Q。
分离蕴含最重要的证明规则是【伴随性质|Adjoint Property】。
它说,P×Q 蕴含 R 当且仅当 P 蕴含 Q−∗R。
这条规则在可验证 C 中称为 wand_sepcon_adjoint。
Check wand_sepcon_adjoint.
(* : forall P Q R : mpred, P * Q |-- R <-> P |-- Q −∗ R *)
(* : forall P Q R : mpred, P * Q |-- R <-> P |-- Q −∗ R *)
由于这一性质,我们也把 −∗ 叫作 × 的【右伴随|Right Adjoint】。
在命题逻辑中,蕴含 → 是合取 ∧ 的右伴随。
这一内在相似性给了 −∗ 「分离蕴含」这个名字。
下面是 −∗ 的另外两条重要性质;我们可以轻易找到
命题逻辑「蕴含」的对应物。
分离蕴含的证明规则:
Check wand_derives.
(* : forall P P' Q Q' : mpred,
P' |-- P -> Q |-- Q' -> P −∗ Q |-- P' −∗ Q' *)
Check modus_ponens_wand.
(* : forall P Q : mpred, P * (P −∗ Q) |-- Q *)
(* : forall P P' Q Q' : mpred,
P' |-- P -> Q |-- Q' -> P −∗ Q |-- P' −∗ Q' *)
Check modus_ponens_wand.
(* : forall P Q : mpred, P * (P −∗ Q) |-- Q *)
现在,我们学习用伴随性质来证明关于 −∗ 的
其他分离逻辑规则。先从一条简单的开始。
Lemma wand_trivial: ∀ P Q: mpred, P |-- Q −∗ (P × Q).
Proof.
intros.
rewrite <- wand_sepcon_adjoint.
apply derives_refl.
Qed.
Proof.
intros.
rewrite <- wand_sepcon_adjoint.
apply derives_refl.
Qed.
接着,我们将从伴随性质重新证明 −∗ 与 × 的
假言推理规则。
Lemma modus_ponens_wand_from_adjoint: ∀ P Q : mpred, P × (P −∗ Q) |-- Q.
Proof.
intros.
rewrite sepcon_comm.
rewrite → wand_sepcon_adjoint.
apply derives_refl.
Qed.
Proof.
intros.
rewrite sepcon_comm.
rewrite → wand_sepcon_adjoint.
apply derives_refl.
Qed.
现在用 wand_sepcon_adjoint 与 modus_ponens_wand
证明 wand_derives。你可以使用关于 × 的其他证明规则,
如 sepcon_derives。另外,策略 sep_apply 可能有用。
练习: 2 stars,标准: (wand_derives)
Lemma wand_derives_from_adjoint_and_modus_ponens:
∀ P P' Q Q' : mpred,
P' |-- P → Q |-- Q' → P −∗ Q |-- P' −∗ Q'.
Proof.
(* 在这里填写 *) Admitted.
☐
∀ P P' Q Q' : mpred,
P' |-- P → Q |-- Q' → P −∗ Q |-- P' −∗ Q'.
Proof.
(* 在这里填写 *) Admitted.
☐
Check wand_frame_ver.
(* : forall P Q R : mpred, (P −∗ Q) * (Q −∗ R) |-- P −∗ R *)
(* : forall P Q R : mpred, (P −∗ Q) * (Q −∗ R) |-- P −∗ R *)
Lemma wand_frame_ver_from_adjoint_and_modus_ponens:
∀ P Q R : mpred, (P −∗ Q) × (Q −∗ R) |-- P −∗ R.
Proof.
(* 在这里填写 *) Admitted.
☐
∀ P Q R : mpred, (P −∗ Q) × (Q −∗ R) |-- P −∗ R.
Proof.
(* 在这里填写 *) Admitted.
☐
Lemma emp_wand_emp_right: emp |-- emp −∗ emp.
Proof.
(* 在这里填写 *) Admitted.
Lemma emp_wand_emp_left: emp −∗ emp |-- emp.
Proof.
(* 在这里填写 *) Admitted.
☐
Proof.
(* 在这里填写 *) Admitted.
Lemma emp_wand_emp_left: emp −∗ emp |-- emp.
Proof.
(* 在这里填写 *) Admitted.
☐
在 Verif_append1 中,我们递归定义了一条新的
分离逻辑谓词:【列表段|List Segment】。该谓词描述
一个包含部分链表的堆片。
本章学习描述部分数据结构的另一种方式:
我们把魔杖与量词结合使用。
这是一个自然的想法。以链表为例,把一条链表
接到部分链表(或列表段)的尾部,会得到一条
从头起完整的链表。因此,部分链表可以用
「所添加的列表 −∗ 完整列表」来描述。形式化地:
Definition wlseg (contents: list val) (x y: val) : mpred :=
ALL tail: list val, listrep tail y −∗ listrep (contents ++ tail) x.
ALL tail: list val, listrep tail y −∗ listrep (contents ++ tail) x.
这里 wlseg 中的 w 代表「魔杖」(wand)。
这个定义与 lseg 非常不同,却简洁而优美。
它还能很好地推广到树等其他数据结构。
让我们证明 wlseg 的一些基本性质。下列引理
表明如何引入魔杖表达式(emp_wlseg 与
singleton_wlseg)、如何消去魔杖表达式(wlseg_list)
以及如何合并两条魔杖表达式(wlseg_wlseg)。
这个定义中有两个逻辑运算符:−∗ 与
【全称量词|Universal Quantifier】。此前我们已经学过
如何证明关于 × 与 −∗ 的性质。要证明关于全称量词的
性质,我们将使用 allp_left 与 allp_right。
Check allp_left.
(* : forall (P : ?B -> mpred) (x : ?B) (Q : mpred),
P x |-- Q -> ALL x : _ , P x |-- Q *)
Check allp_right.
(* : forall (P : mpred) (Q : ?B -> mpred),
(forall v : ?B, P |-- Q v) -> P |-- ALL x : _ , Q x *)
(* : forall (P : ?B -> mpred) (x : ?B) (Q : mpred),
P x |-- Q -> ALL x : _ , P x |-- Q *)
Check allp_right.
(* : forall (P : mpred) (Q : ?B -> mpred),
(forall v : ?B, P |-- Q v) -> P |-- ALL x : _ , Q x *)
wlseg 的第一条性质是我们可以从 emp
引入 wlseg。
Lemma emp_wlseg: ∀ (x: val),
emp |-- wlseg [] x x.
Proof.
intros.
unfold wlseg.
apply allp_right; intro tail.
rewrite <- wand_sepcon_adjoint.
rewrite emp_sepcon.
simpl app.
apply derives_refl.
Qed.
emp |-- wlseg [] x x.
Proof.
intros.
unfold wlseg.
apply allp_right; intro tail.
rewrite <- wand_sepcon_adjoint.
rewrite emp_sepcon.
simpl app.
apply derives_refl.
Qed.
接下来,我们表明两条 wlseg 谓词可以合并为一条。
Lemma wlseg_wlseg: ∀ (s1 s2: list val) (x y z: val),
wlseg s2 y z × wlseg s1 x y |-- wlseg (s1 ++ s2) x z.
Proof.
intros.
unfold wlseg.
wlseg s2 y z × wlseg s1 x y |-- wlseg (s1 ++ s2) x z.
Proof.
intros.
unfold wlseg.
首先,提取右侧全称量化的变量 tail。
apply allp_right; intro tail.
接着,用 tail 实例化左侧第一个量化的 tail0。
rewrite → wand_sepcon_adjoint.
apply (allp_left _ tail).
rewrite <- wand_sepcon_adjoint.
apply (allp_left _ tail).
rewrite <- wand_sepcon_adjoint.
然后,用 s2 ++ tail 实例化左侧另一个量化的 tail0。
rewrite sepcon_comm, → wand_sepcon_adjoint.
apply (allp_left _ (s2 ++ tail)).
rewrite <- wand_sepcon_adjoint, sepcon_comm.
apply (allp_left _ (s2 ++ tail)).
rewrite <- wand_sepcon_adjoint, sepcon_comm.
最后,用 wand_frame_ver 完成证明。
定理 wlseg_wlseg 与 lseg_lseg 形式相同。
事实上,关于 lseg 与 wlseg 的性质非常相似。
下列练习证明 singleton_lseg 与 lseg_list
的对应物。
练习: 2 stars,标准: (singleton_wlseg)
Lemma singleton_wlseg: ∀ (a: val) (x y: val),
data_at Tsh t_list (a, y) x |-- wlseg [a] x y.
Proof.
(* 在这里填写 *) Admitted.
☐
data_at Tsh t_list (a, y) x |-- wlseg [a] x y.
Proof.
(* 在这里填写 *) Admitted.
☐
Lemma wlseg_list: ∀ (s1 s2: list val) (x y: val),
wlseg s1 x y × listrep s2 y |-- listrep (s1 ++ s2) x.
Proof.
(* 在这里填写 *) Admitted.
☐
wlseg s1 x y × listrep s2 y |-- listrep (s1 ++ s2) x.
Proof.
(* 在这里填写 *) Admitted.
☐
用 wlseg 证明 append 函数
练习: 3 stars,标准: (body_append_alter2)
Lemma body_append_alter2: semax_body Vprog Gprog f_append append_spec.
Proof.
start_function.
forward_if. (* if (x == NULL) *)
- (* then 分支 *)
rewrite (listrep_null _ x) by auto.
(* 在这里填写 *) admit.
- (* else 分支 *)
rewrite (listrep_nonnull _ x) by auto.
Intros h r u.
forward. (* t = x; *)
forward. (* u = t -> tail; *)
Proof.
start_function.
forward_if. (* if (x == NULL) *)
- (* then 分支 *)
rewrite (listrep_null _ x) by auto.
(* 在这里填写 *) admit.
- (* else 分支 *)
rewrite (listrep_nonnull _ x) by auto.
Intros h r u.
forward. (* t = x; *)
forward. (* u = t -> tail; *)
这里我们用 wlseg 表示列表段。
forward_while
(EX s1a: list val, EX b: val, EX s1c: list val, EX t: val, EX u: val,
PROP (s1 = s1a ++ b :: s1c)
LOCAL (temp _x x; temp _t t; temp _u u; temp _y y)
SEP (wlseg s1a x t;
data_at Tsh t_list (b, u) t;
listrep s1c u;
listrep s2 y))%assert.
+ (* 当前断言蕴含循环不变式 *)
(EX s1a: list val, EX b: val, EX s1c: list val, EX t: val, EX u: val,
PROP (s1 = s1a ++ b :: s1c)
LOCAL (temp _x x; temp _t t; temp _u u; temp _y y)
SEP (wlseg s1a x t;
data_at Tsh t_list (b, u) t;
listrep s1c u;
listrep s2 y))%assert.
+ (* 当前断言蕴含循环不变式 *)
要从当前断言导出循环不变式,关键在于
引入 wlseg。这里 emp_wlseg 可能对你有帮助。
(* 在这里填写 *) admit.
+ (* 循环测试可安全执行 *)
entailer!.
+ (* 循环体保持不变式 *)
+ (* 循环测试可安全执行 *)
entailer!.
+ (* 循环体保持不变式 *)
沿循环体逐步 forward;在此过程中你需要对当前断言
做其他变换,以揭示继续 forward 的机会。
在循环体末尾,你需要证明 s1a 的列表段与 b 的
单元素单元构成一条更长的列表段,其内容为
s1a ++ b :: nil。这里 singleton_wlseg 与
wlseg_wlseg 可能有用。
(* 在这里填写 *) admit.
+ (* 循环之后 *)
+ (* 循环之后 *)
对 return 命令做符号执行之后,你需要把三样东西
拼成一条完整的链表:
s1a 的列表段、b 的单元素单元,以及 s2 的另一条链表。
新链表的内容是 s1a ++ b :: s2。
这里 singleton_wlseg 与 wlseg_list 可能有用。
(* 在这里填写 *) admit.
(* 在这里填写 *) Admitted.
☐
(* 在这里填写 *) Admitted.
☐
一般思想:魔杖即框架
Lemma wandQ_frame_elim_mpred: ∀ {A: Type} (P Q: A → mpred) (a: A),
(ALL x : A, P x −∗ Q x) × P a |-- Q a.
Proof.
intros.
rewrite → wand_sepcon_adjoint.
apply (allp_left _ a).
apply derives_refl.
Qed.
(ALL x : A, P x −∗ Q x) × P a |-- Q a.
Proof.
intros.
rewrite → wand_sepcon_adjoint.
apply (allp_left _ a).
apply derives_refl.
Qed.
下一引理名字中的 ver 代表魔杖框架的「纵向组合」。
一个魔杖框架嵌套在另一个之内。
Lemma wandQ_frame_ver_mpred: ∀ {A: Type} (P Q R: A → mpred),
(ALL x : A, P x −∗ Q x) × (ALL x: A, Q x −∗ R x) |-- ALL x: A, P x −∗ R x.
Proof.
intros.
apply allp_right; intro a.
rewrite → wand_sepcon_adjoint.
apply (allp_left _ a).
rewrite <- wand_sepcon_adjoint.
rewrite sepcon_comm, → wand_sepcon_adjoint.
apply (allp_left _ a).
rewrite <- wand_sepcon_adjoint, sepcon_comm.
apply wand_frame_ver.
Qed.
(ALL x : A, P x −∗ Q x) × (ALL x: A, Q x −∗ R x) |-- ALL x: A, P x −∗ R x.
Proof.
intros.
apply allp_right; intro a.
rewrite → wand_sepcon_adjoint.
apply (allp_left _ a).
rewrite <- wand_sepcon_adjoint.
rewrite sepcon_comm, → wand_sepcon_adjoint.
apply (allp_left _ a).
rewrite <- wand_sepcon_adjoint, sepcon_comm.
apply wand_frame_ver.
Qed.
案例研究:链表盒的列表段
struct list * append2 (struct list * x, struct list * y) {
struct list **retp, **curp;
retp = & x;
curp = & x;
while ( *curp != NULL ) {
curp = & (( *curp ) -> tail);
}
*curp = y;
return *retp;
}
struct list *append (struct list *x, struct list *y) {
struct list *t, *u;
if (x==NULL)
return y;
else {
t = x;
u = t->tail;
while (u!=NULL) {
t = u;
u = t->tail;
}
t->tail = y;
return x;
}
}
Definition t_list_box := tptr t_list.
Definition listboxrep (contents: list val) (x: val) :=
EX y: val, data_at Tsh t_list_box y x × listrep contents x.
Definition lbseg (contents: list val) (x y: val) :=
ALL tail: list val, listboxrep tail y −∗ listboxrep (contents ++ tail) x.
Definition listboxrep (contents: list val) (x: val) :=
EX y: val, data_at Tsh t_list_box y x × listrep contents x.
Definition lbseg (contents: list val) (x y: val) :=
ALL tail: list val, listboxrep tail y −∗ listboxrep (contents ++ tail) x.
此前我们已经表明,通过证明 emp_wlseg、singleton_wlseg、
wlseg_list 与 wlseg_wlseg,我们可以引入、消去与合并
魔杖表达式。现在,你的任务是证明 lbseg 的性质。
提示:证明 wlseg 的性质与证明 lbseg 的性质
应当非常相似。
练习: 1 star,标准: (emp_lbseg)
从 emp 引入魔杖表达式 lbseg。
Lemma lbseg_lbseg: ∀ (s1 s2: list val) (x y z: val),
lbseg s2 y z × lbseg s1 x y |-- lbseg (s1 ++ s2) x z.
Proof.
(* 在这里填写 *) Admitted.
☐
lbseg s2 y z × lbseg s1 x y |-- lbseg (s1 ++ s2) x z.
Proof.
(* 在这里填写 *) Admitted.
☐
Lemma listbox_lbseg: ∀ (s1 s2: list val) (x y: val),
lbseg s1 x y × listboxrep s2 y |-- listboxrep (s1 ++ s2) x.
Proof.
(* 在这里填写 *) Admitted.
☐
lbseg s1 x y × listboxrep s2 y |-- listboxrep (s1 ++ s2) x.
Proof.
(* 在这里填写 *) Admitted.
☐
比较与联系:lseg 与 wlseg
Lemma lseg2wlseg: ∀ s x y, lseg s x y |-- wlseg s x y.
Proof.
intros.
unfold wlseg.
apply allp_right; intros tail.
rewrite <- wand_sepcon_adjoint.
sep_apply (lseg_list s tail x y).
apply derives_refl.
Qed.
Proof.
intros.
unfold wlseg.
apply allp_right; intros tail.
rewrite <- wand_sepcon_adjoint.
sep_apply (lseg_list s tail x y).
apply derives_refl.
Qed.
在某些特殊情形下,wlseg 也蕴含 lseg。
Lemma wlseg2lseg_nullval: ∀ s x, wlseg s x nullval |-- lseg s x nullval.
Proof.
intros.
unfold wlseg.
apply allp_left with (@nil val).
unfold listrep at 1.
rewrite prop_true_andp by auto.
entailer!.
rewrite app_nil_r.
Proof.
intros.
unfold wlseg.
apply allp_left with (@nil val).
unfold listrep at 1.
rewrite prop_true_andp by auto.
entailer!.
rewrite app_nil_r.
此时证明目标的形式是:一条魔杖表达式蕴含某个
不含魔杖的断言。通常这是艰巨的任务,因为左侧
没有好办法消去魔杖。但这个证明目标很特殊。
我们可以在左侧添加一条额外的分离合取项 emp,
并用 modus_ponens_wand 消去魔杖。
然后,就容易了!
把上面两条引理合起来,我们知道
到空的 wlseg 等于 lseg。
Lemma wlseg_nullval: ∀ s x, wlseg s x nullval = lseg s x nullval.
Proof.
intros.
apply pred_ext.
+ apply wlseg2lseg_nullval.
+ apply lseg2wlseg.
Qed.
Corollary wlseg_listrep_equiv: ∀ s x, wlseg s x nullval = listrep s x.
Proof.
intros.
rewrite wlseg_nullval, lseg_listrep_equiv.
reflexivity.
Qed.
Proof.
intros.
apply pred_ext.
+ apply wlseg2lseg_nullval.
+ apply lseg2wlseg.
Qed.
Corollary wlseg_listrep_equiv: ∀ s x, wlseg s x nullval = listrep s x.
Proof.
intros.
rewrite wlseg_nullval, lseg_listrep_equiv.
reflexivity.
Qed.
然而,wlseg 一般并不蕴含 lseg。如上所述,
在左侧消去魔杖很困难。当 y ≠ nullval 时,
我们无法实例化 (wlseg s x y) 中全称量化的变量
tail 以得到 emp −∗ _ 的形式。
下面是从 wlseg 到 lseg 的一般蕴含关系的一个反例。
一方面,我们有
data_at_ Tsh t_list y ⊢/- lseg [a] x y。
另一方面,data_at_ Tsh t_list y |-- wlseg [a] x y。
见下列定理:
Lemma wlseg_weird: ∀ a x y,
data_at_ Tsh t_list y |-- wlseg [a] x y.
Proof.
intros.
unfold wlseg.
apply allp_right; intros s.
rewrite <- wand_sepcon_adjoint.
destruct s.
+ unfold listrep at 1.
entailer!.
destruct H as [H _].
contradiction.
+ unfold listrep at 1; fold listrep.
Intros u.
sep_apply (data_at_conflict Tsh t_list (default_val t_list) (v, u) y); auto.
entailer!.
Qed.
(* 2026-11-26 16:01 *)
data_at_ Tsh t_list y |-- wlseg [a] x y.
Proof.
intros.
unfold wlseg.
apply allp_right; intros s.
rewrite <- wand_sepcon_adjoint.
destruct s.
+ unfold listrep at 1.
entailer!.
destruct H as [H _].
contradiction.
+ unfold listrep at 1; fold listrep.
Intros u.
sep_apply (data_at_conflict Tsh t_list (default_val t_list) (v, u) y); auto.
entailer!.
Qed.
(* 2026-11-26 16:01 *)
