Spec_stackStack 模块的 VSU 规约

回顾 Verif_stack 中描述的 Stack 模块。 stack.c 其实包含两个模块:「stack」与「triang」。 我们把这个程序拆成如下四部分:
  • stdlib.c(及 stdlib.h),刻画外部库函数 malloc、free、exit。
  • stack2.c(及 stack2.h),含 newstack、push、pop。
  • triang2.c(及 triang2.h),含 push_increasing、 pop_and_add 与 triang。
  • main2.c,含调用 triang 的 main 函数。
对 stack 模块,我们有头文件 stack2.h:
    /* stack2.h */
    #include <stddef.h>
    struct stack;
    struct stack *newstack(void);
    void push (struct stack *p, int i);
    int pop (struct stack *p);

来验证吧!

Require Import VST.floyd.proofauto.
Require Import VC.stack2.
Require Import VC.Spec_stdlib.

抽象谓词声明(APD)

正如你在 Verif_stack 中所见, 栈模块实现了一个【抽象数据类型|Abstract Data Type】。 这是一种具有私有表示与公共操作的数据结构。 在 C 程序中,该类型的名字是 struct stack。 上面的头文件写 struct stack; 而非 struct stack {...fields...};,以此强调表示是私有的。
这意味着只导入 stack.h 的 .c 文件 无法访问各个字段。 这类文件应当用 API 函数 newstack、push 与 pop 来创建与操作类型 <struct stack *> 的值。 C 程序无法精确【强制】这一点:客户端可以「作弊」, 自行声明 <struct stack {...fields...}>, 或把类型 <struct stack *> 的值强制转换为其他类型。 但在 VST 验证中,我们【可以】强制这种数据抽象, 办法是给出一个【抽象谓词声明】(APD)。
Record StackAPD := {
  stackrep: list Z → val → mpred;
  stackrep_local_facts: ∀ il p, stackrep il p |-- !! (isptr p);
  stackrep_valid_pointer: ∀ il p, stackrep il p |-- valid_pointer p
}.
这里,stackrep 与 Verif_stack 中的 stack 作用相同:它是栈的分离逻辑表示关系。 特别地,stackrep il p 是一个 mpred, 表示「地址 p 处有一个表示序列 il 的数据结构」。 区别在于,我们不说明表示【是什么】, 只给出类型签名 list Z → val → mpred。 此外,还要为该关系提供 local_facts 与 valid_pointer 引理,正如 Verif_stack 中那样。 不过这里我们把它们作为公理而非定理给出。 这些内容打包进一个 Record, 即【抽象谓词声明】(APD)。 客户端无需知道数据结构究竟如何表示, 即 stackrep 谓词的细节。
若我们检查 stackrep 的类型,……
Check stackrep. (* : StackAPD -> list Z -> val -> mpred *)
我们发现:给定一个【实例】 S:StackAPD, stackrep S 就是类型 list Z → val → mpred 的关系。 目前还没有实例。毕竟这是抽象数据类型, 它的表示将由某个模块的实现提供, 而本文件只描述接口,不描述实现。
现在,同 Verif_stack 中一样, 我们把关于 stackrep 的引理/公理 加入各自的提示数据库。
#[export] Hint Resolve stackrep_local_facts : saturate_local.
#[export] Hint Resolve stackrep_valid_pointer: valid_pointer.

抽象规约接口(ASI)

模块之间的规约接口,主要由客户端可调用的 API 函数的 【函数规约|Funspec】组成。 在抽象规约接口(ASI)中我们给出这些函数规约, 但不给出它们的证明。 这些函数规约的前置条件与后置条件中 可以使用分离逻辑谓词(mpred), 即 APD(本模块的或其他模块的)。 这些 APD 将作为该模块的参数。 函数规约也可以使用普通的 mpred, 例如 data_at。
在本例中,Stack ASI 需要引用 2 个 APD: 从 MallocFree 模块导入的 MallocFreeAPD, 以及将由本模块导出的 StackAPD。 换言之,每个函数规约都由 M (MallocFreeAPD 的实例)与 STACK(StackAPD 的实例)参数化。 我们用一个 Section 实现这一点。
Section StackASI.
Variable M: MallocFreeAPD.
Variable STACK: StackAPD.
现在函数规约与 Verif_stack 中的一样, 只是把 mem_mgr 写成 mem_mgr M, 把 stack 写成 stackrep STACK。 顺带一提,Verif_stack 中的 mem_mgr 指的是 VST.floyd.library.mem_mgr, 这里指的却是 Spec_stdlib.mem_mgr,二者不同。 Spec_stdlib 一章会解释后者。
Locate mem_mgr. (* VC.Spec_stdlib.mem_mgr 的缩写 *)
Check mem_mgr. (* MallocFreeAPD -> globals -> mpred. *)

Definition newstack_spec : ident × funspec :=
 DECLARE _newstack
 WITH gv: globals
 PRE [ ]
    PROP () PARAMS() GLOBALS(gv) SEP (mem_mgr M gv)
 POST [ tptr (Tstruct _stack noattr) ]
    EX p: val, PROP ( ) RETURN (p)
               SEP (stackrep STACK nil p; mem_mgr M gv).

Definition push_spec : ident × funspec :=
 DECLARE _push
 WITH p: val, i: Z, il: list Z, gv: globals
 PRE [ tptr (Tstruct _stack noattr), tint ]
    PROP (Int.min_signed ≤ i ≤ Int.max_signed)
    PARAMS (p; Vint (Int.repr i)) GLOBALS(gv)
    SEP (stackrep STACK il p; mem_mgr M gv)
 POST [ tvoid ]
    PROP ( ) RETURN ()
    SEP (stackrep STACK (i::il) p; mem_mgr M gv).

Definition pop_spec : ident × funspec :=
 DECLARE _pop
 WITH p: val, i: Z, il: list Z, gv: globals
 PRE [ tptr (Tstruct _stack noattr) ]
    PROP ()
    PARAMS (p) GLOBALS(gv)
    SEP (stackrep STACK (i::il) p; mem_mgr M gv)
 POST [ tint ]
    PROP ( ) RETURN (Vint (Int.repr i))
    SEP (stackrep STACK il p; mem_mgr M gv).
现在「Stack 抽象规约接口」 只是导出函数规约的列表
Definition StackASI : funspecs := [ newstack_spec; push_spec; pop_spec ].

End StackASI.
还要记住,StackASI 被 Section 的 Variable 参数化:
Check StackASI. (*   : MallocFreeAPD -> StackAPD -> funspecs *)

下一章:Spec_triang

(* 2026-11-26 16:01 *)