Verif_reverse可验证 C 中的链表

本章演示可验证 C 的更多特性。 本章没有习题。

运行示例

这里有一个小 C 程序 reverse.c:
    #include <stddef.h>

    struct list {unsigned head; struct list *tail;};

    struct list *reverse (struct list *p) {
      struct list *w, *t, *v;
      w = NULL;
      v = p;
      while (v) {
        t = v->tail;
        v->tail = w;
        w = v;
        v = t;
      }
      return w;
    }
该程序通过更新所有 tail【指针|Pointer】而不碰任何 head 值, 来反转【链表|Linked List】p。你可以翻阅随本章分发的 Reverse-Slides.pdf 幻灯片来「看它运行」。让我们证明这个程序正确!
另见 VC.pdf 第 50 章(Proof of the reverse program)
与往常一样,我们导入可验证 C 系统 VST.floyd.proofauto, 然后导入待验证的程序,本例中是 reverse。 接着我们给出 CompSpecs 与 Vprog 的标准样板定义。
Require VC.Preface. (* 检查 VST 的版本是否正确 *)
Require Import VST.floyd.proofauto.
Require Import VC.reverse.
#[export] Instance CompSpecs : compspecs. make_compspecs prog. Defined.
Definition Vprog : varspecs. mk_varspecs prog. Defined.

链表的归纳定义

Tstruct _list noattr 是 C 语言类型 struct list 的 AST(【抽象语法树|Abstract Syntax Tree】)描述。我们会经常用到它, 所以给它起个缩写 t_list:
Definition t_list := Tstruct _list noattr.
我们将定义一个【分离逻辑|Separation Logic】谓词 listrep sigma p, 来描述「内存中地址 p 是一条表示数学序列 sigma 的链表」。 这里 sigma 是 val 的列表。 val 是 C 的「值」类型:整数、指针、浮点数等。
Fixpoint listrep (sigma: list val) (p: val) : mpred :=
 match sigma with
 | h::hs ⇒
    EX y:val, data_at Tsh t_list (h,y) p × listrep hs y
 | nil ⇒
    !! (p = nullval) && emp
 end.
这是说,如果 sigma 的表头是 h、表尾是 hs, 那么地址 p 处有一个分量为 (h,y) 的 cons 单元。 这个 cons 单元由 data_at Tsh t_list (h,y) p 描述。 与它分离地,在地址 y 处有列表其余部分的表示, 即 listrep hs y。listrep (h::hs) p 的【内存足迹|Memory Footprint】 包含地址 p 处的第一个 cons 单元, 以及从地址 y 开始的其余 cons 单元。
但如果 sigma 是 nil,那么 p 就是【空指针|Null Pointer】, 内存足迹为空(emp)。事实 p=nullval 是一个纯命题 (Rocq Prop);我们用 !! 算子把它注入【断言|Assertion】语言 (Rocq mpred)。
因为 !!P(对命题 P)不指定任何足迹 (无论空与否),我们不用分离合取 × 把它与 emp 结合。 !!P 没有可供分离的【空间】【规约|Specification】。 相反,我们使用普通合取 &&。
现在,我们想阻止 simpl 策略自动展开 listrep。 这是一种设计选择,你可能会做出不同的选择; 若如此,请略去 Arguments 命令。
Arguments listrep sigma p : simpl never.

空间算子的提示数据库

每当你定义一个新的空间算子,即一个 mpred 类型的定义 (如 listrep),填充两个【提示数据库|Hint Database】是很有用的。
  • saturate_local【提示|Hint】是一条引理,它从空间事实中 提取纯命题事实。
  • valid_pointer 提示是一条引理,它从空间引理中 提取【有效指针|Valid Pointer】事实。
    考虑这个证明目标:
Lemma data_at_isptr_example1:
  ∀ (h y p : val) ,
   data_at Tsh t_list (h,y) p |-- !! isptr p.
Proof.
intros.
isptr p 意味着 p 是非空指针,而非 NULL、Vundef 或浮点数:
Print isptr.
  (* = fun v : val => match v with Vptr _ _ => True | _ => False end *)
该目标用 entailer! 自动解决
entailer!.
Qed.

Lemma data_at_isptr_example2:
  ∀ (h y p : val) ,
   data_at Tsh t_list (h,y) p |-- !! isptr p.
Proof.
intros.
让我们更仔细地看 entailer! 如何解决这个目标。 首先,它找出左侧 mpred 合取项中能推导出的所有 纯命题 Prop,并把它们放在线上方。
saturate_local.
saturate_local 策略使用一个提示数据库(也叫 saturate_local)来查找左侧的各个合取项。 这个【蕴含式|Entailment】只有一个合取项。
Print HintDb saturate_local.
在本例中,线上方的新命题标为 H 与 H0。 接下来,如果证明目标右侧只是一个命题 !!P, entailer! 就丢弃左侧并尝试证明 P。 (这相当激进,有时会丢失信息。) 也就是说,有时 entailer! 会把可证明的目标 变成不可证明的目标。
apply prop_right.
恰好 field_compatible _ _ p 蕴含 isptr p,
Check field_compatible_isptr.
   (* : forall (t : type) (path : list gfield) (p : val),
       field_compatible t path p -> isptr p *)

因此 field_compatible_isptr 解决了该目标。
eapply field_compatible_isptr; eauto.
现在你对 entailer! 的工作原理有所了解了。
Qed.
但当你定义一个新的空间谓词 mpred(如 listrep)时, saturate_local 策略不知道如何从 listrep 合取项 推导 Prop 事实:
Lemma listrep_facts_example:
 ∀ sigma p,
   listrep sigma p |-- !! (isptr p ∨ p=nullval).
Proof.
intros.
entailer!.
这里 entailer! 丢弃了左侧,留下一个不可证明的目标。 让我们看看为什么。
Abort.

Lemma listrep_facts_example:
 ∀ sigma p,
   listrep sigma p |-- !! (isptr p ∨ p=nullval).
Proof.
intros.
首先 entailer! 会用 saturate_local 查看(从提示数据库中) 能从 listrep sigma p 推导出什么。
saturate_local.
但 saturate_local 没有在线上方添加任何东西。 那是因为 listrep 在提示数据库中没有提示。 因此我们必须添加一条。这种引理的惯用名字是 f_local_facts,如果你的新谓词叫 f 的话。
Abort.

Lemma listrep_local_facts:
  ∀ sigma p,
   listrep sigma p |--
   !! (is_pointer_or_null p ∧ (p=nullval ↔ sigma=nil)).
对每个空间谓词 Definition f(_): mpred, 都应当有【一条】「局部事实」,即形如 f(_) |-- !! _ 的引理。右侧放上你能从 f(_) 导出的所有命题。在本例中,我们知道:
  • p 要么是指针要么是空值(它绝不会是 Vundef、 Vfloat 或非零 Vint)。
  • p 为空,当且仅当 sigma 为 nil。
Proof.
intros.
我们将通过对 sigma 归纳来证明这个蕴含式
revert p; induction sigma; intros p.
-
在基本情况中,sigma 是 nil。我们可以展开 listrep 的定义看看那是什么意思。
 unfold listrep.
现在我们有一个左侧带命题 p=nullval 的蕴含式。 为了把该命题移到线上方,我们可以用 Intros。 但直接调用 entailer! 看它如何简化(并可能部分解决) 这个蕴含式目标更容易:
  entailer!.
 (* entailer! 留下一个普通命题,它很容易解决。 *)
  split; auto.
-
在归纳情况中,我们可以再次展开 listrep (a::sigma) 的定义;不过随后最好把 listrep sigma 折叠回去。 把下一行的分号 ; 换成句点,看看为什么。
 unfold listrep; fold listrep.
警告!有时 entailer! 太过激进。如果我们在这里 用它,它会丢弃左侧,因为它不知道如何看进一个 存在量词内部。感叹号 ! 就是警告:entailer! 可能把可证明的目标变成不可证明的目标。 取消下一行的注释看看会发生什么。然后把注释标记加回去。
 (* entailer!. *)
处理蕴含式左侧 EX y:t 的首选方式是用 Intros y. 取消其注释试试,然后把注释标记加回去。
 (* Intros y. *)
一个不那么激进的蕴含式化简器是不带感叹号的 entailer。 它绝不会把可证明的目标变成不可证明的目标。 这里它会把 EX 绑定的变量 y Intro 进来。
 entailer.
在普通证明中该用 entailer! 还是 entailer? 通常 entailer! 最好:它更快,而且替你做更多工作。 只有当你发现 entailer! 走进了死胡同时, 才应改用 entailer。
这里用 entailer! 是安全的
 entailer!.
注意 entailer! 在线上方放了几条事实: field_compatible t_list [] p 与 value_fits t_list (a,y) 来自 saturate_local 提示数据库,来自 data_at 合取项; 而 is_pointer_or_null y 与 y=nullval ↔ sigma=[] 来自 listrep 合取项,用到了归纳假设 IHsigma。
现在,让我们分裂目标,分别处理两种情况。
 split; intro.
 +
  clear - H H2.
  subst p.
恰好 field_compatible _ _ p 蕴含 isptr p,
Check field_compatible_isptr.
   (* : forall (t : type) (path : list gfield) (p : val),
       field_compatible t path p -> isptr p *)

谓词 isptr 排除空指针,
Print isptr.
  (* = fun v : val => match v with Vptr _ _ => True | _ => False end *)
Print nullval.
  (* = if Archi.ptr64 then Vlong Int64.zero else Vint Int.zero *)
因此 H 是一个矛盾。我们可以继续,
Check field_compatible_nullval.
  (* = forall (CS : compspecs) (t : type) (f : list gfield) (P : Type),
       field_compatible t f nullval -> P *)

  eapply field_compatible_nullval; eauto.
 + (* 情况 a::sigma=[] → p=nullval 很容易,用反演: *)
   inversion H2.
Qed.
现在我们把这条引理添加到名为 saturate_local 的提示数据库
#[export] Hint Resolve listrep_local_facts : saturate_local.

有效指针与 valid_pointer 提示数据库

在 C 语言中,只有当 p 是【有效指针】时, 你才能做 p!=NULL 或 p==q 这样的指针比较。 也就是说,p 要么是 NULL,要么确实指向某个已分配对象之内。 证明 p 有效的一种方式是,例如, data_at Tsh t_list (h,y) p,这意味着 p 指向一个列表单元。有一个提示数据库 valid_pointer, 谓词 valid_pointer p 可以从它被自动证明。 例如:
Lemma struct_list_valid_pointer_example:
  ∀ h y p,
   data_at Tsh t_list (h,y) p |-- valid_pointer p.
Proof.
  intros.
  auto with valid_pointer.
Qed.
然而,提示数据库不认识用户定义的分离逻辑谓词 (mpred),如 listrep;例如:
Lemma listrep_valid_pointer_example:
 ∀ sigma p,
   listrep sigma p |-- valid_pointer p.
Proof.
 intros.
 auto with valid_pointer.
注意 auto with... 没有解决该证明目标
Abort.
因此,我们应当证明合适的引理,并把它添加到 提示数据库中。
Lemma listrep_valid_pointer:
  ∀ sigma p,
   listrep sigma p |-- valid_pointer p.
Proof.
 intros.
要点是展开 listrep。
 unfold listrep.
现在我们可以对 sigma 做【分类讨论|Case Analysis】来证明它; 甚至不需要归纳。
 destruct sigma; simpl.
-
nil 情况很容易:
  hint.
  entailer!.
-
cons 情况
  Intros y.
现在这可以用提示数据库 valid_pointer 解决, 因为左侧的 data_at Tsh t_list (v,y) p 足以证明该目标。
  auto with valid_pointer.
Qed.
现在我们把这条引理添加到提示数据库
#[export] Hint Resolve listrep_valid_pointer : valid_pointer.

reverse 函数的规约。

funspec 刻画调用该函数所需的【前置条件|Precondition】 与该函数保证的【后置条件|Postcondition】。
Definition reverse_spec : ident × funspec :=
 DECLARE _reverse
  WITH sigma : list val, p: val
  PRE [ tptr t_list ]
     PROP () PARAMS (p) SEP (listrep sigma p)
  POST [ (tptr t_list) ]
    EX q:val,
     PROP () RETURN (q) SEP (listrep(rev sigma) q).
  • WITH 子句说,存在一个值 sigma: list val 与一个值 p: val,它们在前置条件与 后置条件中都可见。
  • 前置条件说,
    • 有一个函数参数,其 C 类型是 「指向 struct list 的指针」
    • PARAMS:该参数包含 Rocq 值 p;
    • SEP:内存中地址 p 处有一条表示 sigma 的链表。
  • 后置条件说,
    • 函数返回一个值,其 C 类型是 「指向 struct list 的指针」;且
    • 存在一个值 q: val,使得
    • RETURN:函数的返回值是 q
    • SEP:内存中地址 q 处有一条表示 rev sigma 的链表。
全局函数规约刻画你的经证明正确的程序将调用的 所有函数的前置条件/后置条件。通常你会在这里 包含所有函数,但在这个教程示例中我们只包含一个。
Definition Gprog : funspecs := [ reverse_spec ].

reverse 函数的证明

对 C 程序中的每个函数定义,证明其函数体 (本例中是 f_reverse)满足它的规约 (本例中是 reverse_spec)。
Lemma body_reverse: semax_body Vprog Gprog f_reverse reverse_spec.
Proof.
start_function 策略把 semax_body 证明目标 「打开」成一个霍尔三元组。
start_function.
与往常一样,当前断言(前置条件)来自函数规约 reverse_spec 的 PRE 子句,而当前命令 w=0; ...more... 是 f_reverse 的函数体。
函数体中的第一条语句(命令)是【赋值|Assignment】语句 w=NULL;。其中 NULL 是 C 的 #define,展开为 「把 0 转换为 void 指针」,(void × )0, 这里丑陋地打印为 (tptr tvoid)(0)。 要对这条命令应用分离逻辑的赋值规则, 直接使用策略 forward:
forward. (* w = NULL; *)
新的 semax 判断针对的是命令 w=NULL 【之后】 的函数体其余部分。这个 semax 的前置条件 实际上是 w=NULL 语句的后置条件。 它很像 w=NULL 的前置条件,但包含额外的 LOCAL 事实 temp _w (Vint (Int.repr 0)),即变量 _w 包含 nullval。
我们可以把本程序的【霍尔逻辑|Hoare Logic】证明看作一种 【符号执行|Symbolic Execution】, 其中符号状态就是断言。我们再次说 forward 就能符号执行下一条命令。
forward. (* v = p; *)
检视前置条件,注意现在我们有了额外的事实 temp _v p。
我们不能用 forward 走下一步……
Fail forward.
……因为下一条命令是一个 while 循环。

循环不变式

要证明 while 循环,你必须提供一个【循环不变式|Loop Invariant】, 形如
     (EX s1 ... PROP(...)LOCAL(...)SEP(...).
forward_while
   (EX s1: list val, EX s2 : list val,
    EX w: val, EX v: val,
     PROP (sigma = rev s1 ++ s2)
     LOCAL (temp _w w; temp _v v)
     SEP (listrep s1 w; listrep s2 v)).
forward_while 策略留下四个子目标, 我们用 -(Rocq 的「项目符号」)标记它们
- (* 证明(当前)前置条件蕴含循环不变式 *)
hint.
这个蕴含式的左侧是整个 while 循环的前置条件 (我们至此已通过前向符号执行确立了它)。 右侧是我们刚交给 forward_while 策略的循环不变式。 因为右侧有四个存在量词,一个好的证明策略是 用 Exists 策略为它们选择值。
Exists (@nil val) sigma nullval p.
现在我们有一个无量词的证明目标;让我们看看 entailer! 能否解决其中一部分。
entailer!.
确实,entailer! 干得不错。剩下的是我们的 用户定义谓词 listrep 的一条性质: emp |-- listrep [] nullval。
unfold listrep.
用户定义谓词展开后,entailer! 就能解决 剩余的蕴含式。
entailer!.

- (* 证明循环不变式蕴含循环条件的类型检查 *)
hint.
forward_while 的第二个子目标是证明 循环测试条件能执行而不崩溃。例如考虑 C 语言的 while 循环 while (a[i]>0) ..., 其中 i 的值可能超出数组界。 那样就会是「缓冲区越界」。在 C 语义中, 这属于「未定义行为」(「卡住」)。 我们必须证明:给定当前前置条件(本例中是 循环不变式),循环测试不是「未定义行为」。 这个证明目标形如 当前前置条件 |-- tc_expr Delta e, 其中 e 是循环测试表达式。你可以把 tc_expr 读作「类型检查表达式」。可验证 C 的类型检查器 确保这类表达式是安全的(有时需要你证明一个子目标)。
幸运的是,在大多数情形中 entailer! 能完全 自动地解决 tc_expr 目标:
entailer!.

- (* 证明循环体保持不变式 *)
hint.
与任何霍尔逻辑(包括分离逻辑)中一样,我们需要证明 循环体保持循环不变式。更精确地说,
  • {Inv /\ Test} body {Inv}
其中 Test 是循环测试条件。这里,原始 C 代码中的 循环测试条件是 (v)。它在线上方的表现是 假设 HRE: isptr v,意味着 v 是一个(非空)指针。
循环不变式原是 EX s1:_, EX s2:_, EX w:_, EX v:_, ..., 而这里蕴含式左侧所有存在量化的变量都已移到线上方: s1, s2: val 与 w,v: val。
循环不变式的 PROP 部分是 sigma = rev s1 ++ s2, 它也已作为假设 H 移到线上方。
所以现在我们想前向符号执行穿过循环体中的 四条赋值语句。
Fail forward.
但我们不能前向执行过 t=v→tail;,因为那将需要 前置条件中有形如 data_at sh t_list (_,_) v 的 SEP 合取项。表面上没有这样的合取项。实际上, 有这样的合取项,但它藏在 listrep s2 v 里面。 也就是说,只要 s2 不是 nil,就有这样的合取项。 让我们对 s2 做分类讨论:
destruct s2 as [ | h r].
 + (* s2=nil *)

   
假设 s2=nil。如果我们展开 listrep……
   unfold listrep at 2.
那么我们得知 v=nullval。为了把这条事实(或任何命题) 从前置条件移到线上方,我们用 Intros:
   Intros.
现在线上方有 v=nullval 与 isptr v; 这是一个矛盾。
   subst. contradiction.

 + (* s2=h::r *)

   
假设 s2=h::r。我们可以展开/折叠 h::r 的 listrep 合取项。如果你不记得我们为什么做 unfold/fold, 把(fold 与 unfold 之间的)分号换成句点看看会发生什么。

   unfold listrep at 2; fold listrep.
根据 listrep 的定义,地址 v 处必须存在一个值 y 与一个包含 (h,y) 的列表单元。所以让我们把 y 移到线上方:
   Intros y.
现在我们有了合适的 SEP 合取项,可以 forward 执行过循环体了
   forward. (* t = v->tail *)
   forward. (* v->tail = w; *)
   forward. (* w = v; *)
   forward. (* v = t; *)
到了循环体末尾;我们必须重新确立循环不变式。 这个蕴含式的左侧是当前断言(循环体之后的); 右侧就是我们的循环不变式。 (遗憾的是,forward_while 策略把存在量词 「反柯里化」成了一个绑定 4 元组的单一 EX。) 这是一个看起来复杂的蕴含式。 让我们看看 entailer! 能否把它简化一点:
   entailer!.
现在,我们可以为 s1,s2,w,v 提供新值来实例化 这四个存在量词;它们分别是 h::s1,r,v,y。
   Exists (h::s1,r,v,y).
又是一个看起来复杂的蕴含式;我们让 entailer! 再化简一些。
   entailer!.
   × simpl. rewrite app_ass. auto.
   × unfold listrep at 3; fold listrep.
     Exists w. entailer!.

- (* 循环之后 *)
  
与任何霍尔逻辑(包括分离逻辑)中一样, while 循环的后置条件是 {Inv /\ not Test}, 其中 Inv 是循环不变式,Test 是循环测试。 这里,循环不变式的所有 EXistential 与 PROP 都已作为 s1,s2,w,v,HRE,H 移到线上方。
我们总是可以 forward 执行过一条 return 语句:
forward. (* return w; *)
现在我们必须证明当前断言(while 循环之后的) 蕴含函数后置条件。这个蕴含式的左侧是我们在 forward 执行过 return 之前所拥有的。 右侧是 reverse_spec 的后置条件, 在局部变量(等)被化简掉之后的形式。 我们必须给出一个指针 q(这里叫 x)满足各种条件。 这里找出 x 很容易,因为它被约束为等于 w:
Exists w; entailer!.
rewrite (proj1 H1) by auto.
unfold listrep at 2; fold listrep.
entailer!.
rewrite app_nil_r, rev_involutive.
auto.
Qed.

为什么要分离逻辑?

如果我们回顾对 reverse.c 的【功能正确性|Functional Correctness】证明, 也许看不出我们为什么根本需要分离逻辑。让我们仔细看看。
首先,我们把「分离」内建到 listrep 的定义中。 下面是我们的定义:
  Fixpoint listrep (sigma: list val) (p: val) : mpred :=
   match sigma with
   | h::hs ⇒ EX y:val, data_at Tsh t_list (h,y) p × listrep hs y
   | nil ⇒ !! (p = nullval) && emp
   end.
在非空列表情形中,表头元素由
   data_at Tsh t_list (h,y) p 描述,它与列表的其余部分
   listrep hs y. (由分离合取 * )分离。 这种分离确保没有地址能在一条链表中被使用超过一次。 例如,考虑一条长度至少为 2 的链表,
   listrep (a :: b :: l) x. 我们知道必定存在两个地址 y 与 z 使得
      data_at Tsh t_list (a,y) x ×
      data_at Tsh t_list (b,z) y ×
      listrep l z.
「分离合取」× 告诉我们 x 与 y 必须不同! 形式化地,我们可以证明以下两条引理:
Lemma listrep_len_ge2_fact: ∀ (a b x: val) (l: list val),
  listrep (a :: b :: l) x |--
  EX y: val, EX z: val,
      data_at Tsh t_list (a,y) x ×
      data_at Tsh t_list (b,z) y ×
      listrep l z.
Proof.
  intros.
  unfold listrep; fold listrep.
  Intros y z.
  Exists y z.
  cancel.
Qed.

Lemma listrep_len_ge2_address_different: ∀ (a b x y z: val) (l: list val),
  data_at Tsh t_list (a,y) x ×
  data_at Tsh t_list (b,z) y ×
  listrep l z |--
  !! (x ≠ y).
Proof.
  intros.
为证明地址不同,我们先做分类讨论。 如果 x = y,我们用下面的定理:
  Check data_at_conflict.
    (* : forall (sh : Share.t) (t : type) (v v' : reptype t) (p : val),
         sepalg.nonidentity sh -> 0 < sizeof t ->
         data_at sh t v p * data_at sh t v' p |-- FF *)

它说我们可以从 × 所定义的「分离」 导出【地址反别名|Address Anti-Aliasing】。 如果 x ≠ y,右侧已经证好了。
  destruct (Val.eq x y); [| apply prop_right; auto].
  subst x.
  sep_apply (data_at_conflict Tsh t_list (a, y)).
  + auto.
  + entailer!.
Qed.
事实上,即使性质 x≠y 也不够强!我们需要 知道 x 不与记录 y 的【任何字段】重叠。 例如(用 C 记号)x != &(y→tail) 与 &(x→tail) != y。 否则,当存入 y→tail 时,我们就无法知道 x→head 没有被改动。
没有分离逻辑,我们仍可以用额外的子句定义 listrep' 来做地址反别名。例如,一条长度为 3 的链表 listrep (a :: b :: c :: nil) x 可以是: 存在 y 与 z,使得 (a, y) 存在 x 处、 (b, z) 存在 y 处、(c, nullval) 存在 z 处, 且 x、y 与 z 彼此不同。 一般而言,那条断言会是二次方长度的(作为链表长度的函数)。 于是,为确保 x→head 与 y→tail 不在同一地址, 我们还需要更多断言。
在我们的程序正确性证明中,我们(隐式地)用到了 不同 SEP 子句描述不相交【堆块|Heaplet】这一事实。 这是 body_reverse 证明中的一个中间步骤。
(我们很少陈述这样的中间证明目标。 我们在这里做是为了说明关于分离合取的一点。)
Lemma body_reverse_step: ∀
  {Espec : OracleKind}
  (sigma : list val)
  (s1 : list val)
  (h : val)
  (r : list val)
  (w v : val)
  (HRE : isptr v)
  (H : sigma = rev s1 ++ h :: r)
  (y : val),
  semax (func_tycontext f_reverse Vprog Gprog nil)
    (PROP ( )
     LOCAL (temp _t y; temp _w w; temp _v v)
     SEP (listrep s1 w; data_at Tsh t_list (h, y) v; listrep r y))
    (Ssequence
       (Sassign
          (Efield
             (Ederef (Etempvar _v (tptr (Tstruct _list noattr)))
                     (Tstruct _list noattr))
             _tail (tptr (Tstruct _list noattr)))
          (Etempvar _w (tptr (Tstruct _list noattr))))
       (Ssequence (Sset _w (Etempvar _v (tptr (Tstruct _list noattr))))
                  (Sset _v (Etempvar _t (tptr (Tstruct _list noattr))))))
    (normal_ret_assert
       (PROP ( )
        LOCAL (temp _v y; temp _w v; temp _t y)
        SEP (listrep s1 w; data_at Tsh t_list (h, w) v; listrep r y))).
Proof.
  intros.
  abbreviate_semax.
现在,我们的证明目标是:
  semax Delta
    (PROP ( )
     LOCAL (temp _t y; temp _w w; temp _v v)
     SEP (listrep s1 w; data_at Tsh t_list (h, y) v; listrep r y))
    ((_v → _tail) = _w; MORE_COMMANDS)
    POSTCONDITION.
下一个 forward 策略将对 v→tail = w 做符号执行。
   forward. (* v->tail = w; *)
它把前置条件变成:
     PROP ( )
     LOCAL (temp _t y; temp _w w; temp _v v)
     SEP (listrep s1 w; data_at Tsh t_list (h, w) v; listrep r y).
分离合取项 data_at Tsh t_list (h, y) v 变成 data_at Tsh t_list (h, w) v 没有问题。 但为什么其他分离合取项如 listrep s1 w 没受影响?
因为它们是分离的!这种分离确保地址 v 不被 listrep s1 w 所描述的链表使用。
Abort.
当 C 程序操纵指针数据结构(或数组切片)时, 地址反别名在其正确性证明中扮演重要角色。 分离逻辑对推理这些结构的更新必不可少。 可验证 C 的 SEP 子句确保其所有合取项之间的分离。
(* 2026-11-26 16:01 *)