Spec_stackStack 模块的 VSU 规约
- 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 函数。
/* stack2.h */
#include <stddef.h>
struct stack;
struct stack *newstack(void);
void push (struct stack *p, int i);
int pop (struct stack *p);
来验证吧!
抽象谓词声明(APD)
正如你在 Verif_stack 中所见, 栈模块实现了一个【抽象数据类型|Abstract Data Type】。 这是一种具有私有表示与公共操作的数据结构。 在 C 程序中,该类型的名字是 struct stack。 上面的头文件写 struct stack; 而非 struct stack {...fields...};,以此强调表示是私有的。
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: 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 的类型,……
我们发现:给定一个【实例】 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.
#[export] Hint Resolve stackrep_valid_pointer: valid_pointer.
抽象规约接口(ASI)
现在函数规约与 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).
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 抽象规约接口」
只是导出函数规约的列表
还要记住,StackASI 被 Section 的 Variable 参数化:
下一章:Spec_triang
(* 2026-11-26 16:01 *)
