Verif_stack用链表实现的栈抽象数据类型

这里是一个小程序 stack.c
    #include <stddef.h>

    extern void * malloc (size_t n);
    extern void free (void *p);
    extern void exit(int n);

    struct cons {
      int value;
      struct cons *next;
    };

    struct stack {
      struct cons *top;
    };

    struct stack *newstack(void) {
      struct stack *p;
      p = (struct stack * ) malloc (sizeof (struct stack));
      if (!p) exit(1);
      p->top = NULL;
      return p;
    }

    void push (struct stack *p, int i) {
      struct cons *q;
      q = (struct cons * ) malloc (sizeof (struct cons));
      if (!q) exit(1);
      q->value = i;
      q->next = p->top;
      p->top = q;
    }

    int pop (struct stack *p) {
      struct cons *q;
      int i;
      q = p->top;
      p->top = q->next;
      i = q->value;
      free(q);
      return i;
    }
该程序实现了一个栈【抽象数据类型|Abstract Data Type】(ADT)。
  • 创建新栈:st = newstack();
  • 将整数 i 压入栈:push(st,i);
  • 从栈弹出:i=pop(st);
该栈实现为一个头节点(struct stack), 指向由 cons 单元组成的链表(struct cons)。

我们来验证吧!

Require VC.Preface. (* 检查 VST 的正确版本 *)
Require Import VST.floyd.proofauto.
Require Import VST.floyd.library.
Require Import VC.stack.
#[export] Instance CompSpecs : compspecs. make_compspecs prog. Defined.
Definition Vprog : varspecs. mk_varspecs prog. Defined.
Require Import VC.hints. (* 为本教程导入特殊提示。 *)

Malloc 与 free

当你使用 C 的 malloc/free 库时,你写 p=malloc(n); 以获得指向 n 字节块的指针 p;用完该块之后, 你调用 free(p) 释放它。 free 函数怎么知道要释放多少字节?
答案是:malloc/free 库在地址 p 之前 放了一个额外的「头」字段。你实际得到的是:
      +-----------+
      | header    |
      +-----------+
  p-->|  zero     |
      +-----------+
      |  one      |
      +-----------+
      |  two      |
      +-----------+
本例中 header=3。
在【分离逻辑|Separation Logic】中,我们可以把它描述为
  • malloc_token Ews p × data_at Ews (Tstruct _mystruct noattr) (zero,one,two) p
其中 malloc_token Ews p 描述这幅图:
      +-----------+
      | header    |
      +-----------+
  p-->
当然,malloc/free 库可以有别的方式「记住」p 所指向的大小。所以它对 malloc_token 的表示 【不一定】 是偏移 -1 处的一个字。 因此,malloc/free 库的【客户|Client】把 malloc_token 当作一个【抽象谓词|Abstract Predicate】。 下面是 malloc 与 free 的 【函数规约|Function Specification】,大致如下:
Definition malloc_spec_example :=
 DECLARE _malloc
 WITH t:type, gv: globals
 PRE [ tuint ]
    PROP (0 ≤ sizeof t ≤ Int.max_unsigned;
          complete_legal_cosu_type t = true;
          natural_aligned natural_alignment t = true)
    PARAMS (Vint (Int.repr (sizeof t)))
    SEP (mem_mgr gv)
 POST [ tptr tvoid ] EX p:_,
    PROP ()
    RETURN (p)
    SEP (mem_mgr gv;
            if eq_dec p nullval then emp
            else (malloc_token Ews t p × data_at_ Ews t p)).

Definition free_spec_example :=
 DECLARE _free
 WITH t: type, p:val, gv: globals
 PRE [ tptr tvoid ]
     PROP ()
     PARAMS (p)
     SEP (mem_mgr gv; malloc_token Ews t p; data_at_ Ews t p)
 POST [ Tvoid ]
     PROP () RETURN () SEP (mem_mgr gv).
如果你的源程序写 malloc(sizeof(t)),你的 forward_call 应当(作为 WITH-见证)提供 C 类型 t。 Malloc 可以选择返回 NULL,此时后置条件的 SEP 部分 是 emp。它也可能返回一个指针,此时你得到 data_at_ Ews t p,还白得一个 malloc_token Ews t p。 但别把那个 malloc_token 弄丢! 稍后释放该对象时,你要把它交给 free 函数。
SEP 谓词 data_at_ Ews t p 是类型 t 的一个 【未初始化】 结构体。它等价于 data_at Ews t (default_val t) p。 default_val 基本上是充满 Vundef 值的结构体或数组。

链表的规约

这与 Verif_reverse 中的链表非常类似。
Fixpoint listrep (il: list Z) (p: val) : mpred :=
 match il with
 | i::il' ⇒ EX y: val,
        malloc_token Ews (Tstruct _cons noattr) p ×
        data_at Ews (Tstruct _cons noattr) (Vint (Int.repr i),y) p ×
        listrep il' y
 | nil ⇒ !! (p = nullval) && emp
 end.
对用户定义的分离谓词,若像下面这样禁用自动简化, 证明自动化会更有效:
Arguments listrep il p : simpl never.
照例,我们应当填充提示数据库 saturate_local 与 valid_pointer

练习: 1 star,标准 (stack_listrep_properties)

Lemma listrep_local_prop: ∀ il p, listrep il p |--
        !! (is_pointer_or_null p ∧ (p=nullval ↔ il=nil)).
看看还记不记得怎么证明;也可以再翻翻 Verif_reverse,看它是怎么做的。
(* 在这里填写 *) Admitted.
#[export] Hint Resolve listrep_local_prop : saturate_local.

Lemma listrep_valid_pointer:
  ∀ il p,
   listrep il p |-- valid_pointer p.
看看还记不记得怎么证明;也可以再翻翻 Verif_reverse,看它是怎么做的。
(* 在这里填写 *) Admitted.
#[export] Hint Resolve listrep_valid_pointer : valid_pointer.
☐

栈数据结构的规约

我们的栈数据结构如下所示:
      +-----------+
      | token     |
      +-----------+       +---------
  p-->|  top------+---q-->| linked list...
      +-----------+       +---------
栈对象 p 指向一个头节点,该节点有一个字段 top (外加一个 malloc 令牌);top 字段的 【内容】 是指向某条链表的指针 q。
Definition stack (il: list Z) (p: val) :=
 EX q: val,
  malloc_token Ews (Tstruct _stack noattr) p ×
  data_at Ews (Tstruct _stack noattr) q p ×
  listrep il q.

Arguments stack il p : simpl never.

练习: 1 star,标准 (stack_properties)

Lemma stack_local_prop: ∀ il p, stack il p |-- !! (isptr p).
(* 在这里填写 *) Admitted.
#[export] Hint Resolve stack_local_prop : saturate_local.

Lemma stack_valid_pointer:
  ∀ il p,
   stack il p |-- valid_pointer p.
(* 在这里填写 *) Admitted.
#[export] Hint Resolve stack_valid_pointer : valid_pointer.
☐

栈操作的函数规约

Definition newstack_spec : ident × funspec :=
 DECLARE _newstack
 WITH gv: globals
 PRE [ ]
    PROP () PARAMS() GLOBALS(gv) SEP (mem_mgr gv)
 POST [ tptr (Tstruct _stack noattr) ]
    EX p: val, PROP ( ) RETURN (p) SEP (stack nil p; mem_mgr 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 (stack il p; mem_mgr gv)
 POST [ tvoid ]
    PROP ( ) RETURN () SEP (stack (i::il) p; mem_mgr 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 (stack (i::il) p; mem_mgr gv)
 POST [ tint ]
    PROP ( ) RETURN (Vint (Int.repr i)) SEP (stack il p; mem_mgr gv).
把所有函数规约放在一起:
Definition Gprog : funspecs :=
        ltac:(with_library prog [
                   newstack_spec; push_spec; pop_spec
 ]).

函数体的证明

一种【抽象数据类型】(ADT)是带有【表示|Representation】 和一组【操作|Operation】的类型。 ADT 的客户永远看不到表示,只调用操作。 操作的实现则要直接操纵表示。
本例中 stack 就是我们的 ADT。 操作是 newstack、push 与 pop。 客户只看到 stack il p, 其中 il 是客户压入栈的值的列表, p 是客户的「句柄」,即栈表示的地址。 客户并不知道抽象列表 il 在 C 数据结构中用单链表、双链表、数组 还是其他数据结构表示。 客户 【绝不展开】 定义 stack。
操作 newstack, push, pop 用 C 实现, (本例中)直接操纵一条单链表。 证明它们的正确性需要知道表示。因此,
提示:在 body_pop、body_push 与 body_newstack 的 开头,你首先应当做的是 unfold stack in ×。

练习: 2 stars,标准 (body_pop)

Lemma body_pop: semax_body Vprog Gprog f_pop pop_spec.
Proof.
start_function.
(* 在这里填写 *) Admitted.
☐

练习: 2 stars,标准 (body_push)

Lemma body_push: semax_body Vprog Gprog f_push push_spec.
Proof.
start_function.
forward_call (Tstruct _cons noattr, gv).
(* 在这里填写 *) Admitted.
☐

练习: 2 stars,标准 (body_newstack)

Lemma body_newstack: semax_body Vprog Gprog f_newstack newstack_spec.
Proof.
start_function.
(* 在这里填写 *) Admitted.
☐
(* 2026-11-26 16:01 *)