Verif_stack用链表实现的栈抽象数据类型
#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;
}
- 创建新栈:st = newstack();
- 将整数 i 压入栈:push(st,i);
- 从栈弹出:i=pop(st);
我们来验证吧!
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. (* 为本教程导入特殊提示。 *)
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
+-----------+
| header |
+-----------+
p-->| zero |
+-----------+
| one |
+-----------+
| two |
+-----------+
本例中 header=3。
- malloc_token Ews p × data_at Ews (Tstruct _mystruct noattr) (zero,one,two) 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).
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.
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.
对用户定义的分离谓词,若像下面这样禁用自动简化,
证明自动化会更有效:
Lemma listrep_local_prop: ∀ il p, listrep il p |--
!! (is_pointer_or_null p ∧ (p=nullval ↔ il=nil)).
!! (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.
#[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.
☐
#[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.
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.
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.
☐
(* 在这里填写 *) 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).
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).
把所有函数规约放在一起:
函数体的证明
练习: 2 stars,标准 (body_pop)
Lemma body_pop: semax_body Vprog Gprog f_pop pop_spec.
Proof.
start_function.
(* 在这里填写 *) Admitted.
☐
Proof.
start_function.
(* 在这里填写 *) Admitted.
☐
Lemma body_push: semax_body Vprog Gprog f_push push_spec.
Proof.
start_function.
forward_call (Tstruct _cons noattr, gv).
(* 在这里填写 *) Admitted.
☐
Proof.
start_function.
forward_call (Tstruct _cons noattr, gv).
(* 在这里填写 *) Admitted.
☐
Lemma body_newstack: semax_body Vprog Gprog f_newstack newstack_spec.
Proof.
start_function.
(* 在这里填写 *) Admitted.
☐
Proof.
start_function.
(* 在这里填写 *) Admitted.
☐
(* 2026-11-26 16:01 *)
