Verif_object如何验证面向对象程序
Require VC.Preface. (* 检查 VST 的正确版本 *)
Require Import VST.floyd.proofauto.
Require Import VST.floyd.library.
Require Import VC.nonexpansive. (* VST 2.17 之后无需导入此项 *)
Require Import VC.object.
#[export] Instance CompSpecs : compspecs. make_compspecs prog. Defined.
Definition Vprog : varspecs. mk_varspecs prog. Defined.
Require Import VST.floyd.proofauto.
Require Import VST.floyd.library.
Require Import VC.nonexpansive. (* VST 2.17 之后无需导入此项 *)
Require Import VC.object.
#[export] Instance CompSpecs : compspecs. make_compspecs prog. Defined.
Definition Vprog : varspecs. mk_varspecs prog. Defined.
一个面向对象的 C 程序
这是 C 中一个非常简单的【面向对象|Object-Oriented】编程模式。C 【程序|Program】
object.c 实现了【类|Class】「小整数集合」,以及该类的两个【实例|Instance】
(一个基于链表的实例,一个基于数组的实例)和一个客户程序。
在此演示中,我们将所有这些组件放在同一个 .c 文件中;
更真实的例子会使用 VSU 系统(除了此处展示的面向对象技术之外)
来验证一个被分成多个文件的实现
(本列表中的每一项对应一个文件/模块):
【类|Class】接口:类结构 struct set、抽象表示【类型|Type】struct setrep,
以及客户端可用于进行动态方法查找与调用的便利【函数|Function】。
客户端【函数|Function】示例。给定一个已创建的【实例|Instance】p,
依次插入元素 3、然后 5、再插入 3(幂等地),
接着查找 1(返回 0)和 3(返回 1);然后返回它们的和(0+1=1)。
一个使用链表来表示集合的类实例。
一个使用数组来表示集合的类实例。
主函数:创建两个实例,在每个实例上运行客户端,并(因为客户端应给出相同答案)返回 0。
辅助定义
malloc 的函数规约是 Verif_stack 章节中所述规约的简化版本
- 类接口:类结构和抽象表示【类型|Type】, 以及调用方法 set_insert、set_lookup 的便捷【函数|Function】。
- 客户函数 set_client,以指向该类中任意对象的指针为参数
- 类的实例 list,使用链表实现面向对象接口
- 类的实例 array,使用数组实现面向对象接口
- 主函数,创建两个实例(每种一个),并对每个实例调用客户函数。
/* object.c: Demonstration of object-oriented programming in C */
#include <stddef.h>
extern void * malloc (size_t n);
struct setrep; /* abstract struct for instance-specific representation */
/* Every set has a data-pointer and two method-pointers */
struct set {
struct setrep *objdata;
void ( *insert)(struct set *s, int k); /* Insert k into s, where 0 <= k < 100 */
int ( *lookup)(struct set *s, int k); /* Test membership, where 0 <= k < 100 */
};
/* Convenience functions to call the methods */
void set_insert(struct set *p, int i) {
( *(p->insert))(p, i);
}
int set_lookup(struct set *p, int i) {
return ( *(p->lookup))(p, i);
}
/* Example client function */
int set_client(struct set *p) {
set_insert(p,3);
set_insert(p,5);
set_insert(p,3);
return set_lookup(p,1) + set_lookup(p,3);
}
struct setrep_list {int key; struct setrep_list *next;};
void set_insert_list(struct set *p, int k) {
struct setrep_list *head = (struct setrep_list * )(p->objdata);
struct setrep_list *new = (struct setrep_list * )malloc(sizeof *new);
new->key=k;
new->next=head;
p->objdata = (struct setrep* )new;
}
int set_lookup_list(struct set *p, int k) {
struct setrep_list *head = (struct setrep_list * )(p->objdata);
while (head!=NULL) {
if (head->key==k) return 1;
head=head->next;
}
return 0;
}
struct set *set_create_list(void) {
struct set *p = (struct set * )malloc(sizeof (struct set));
p->objdata=NULL;
p->insert= &set_insert_list;
p->lookup= &set_lookup_list;
return p;
}
#define N 100
struct setrep_array {
unsigned char bytes[N];
};
void set_insert_array(struct set *p, int k) {
struct setrep_array *a = (struct setrep_array * )(p->objdata);
a->bytes[k]=1;
}
int set_lookup_array(struct set *p, int k) {
struct setrep_array *a = (struct setrep_array * )(p->objdata);
return a->bytes[k];
}
struct set *set_create_array(void) {
struct set *p = (struct set * )malloc(sizeof (struct set));
struct setrep_array *a = (struct setrep_array * )malloc(sizeof *a);
int i;
for (i=0; i<N; i++) a->bytes[i]=0;
p->objdata = (struct setrep* )a;
p->insert= &set_insert_array;
p->lookup= &set_lookup_array;
return p;
}
/* main function */
int main(void) {
struct set *s1 = set_create_list();
struct set *s2 = set_create_array();
int n1 = set_client(s1);
int n2 = set_client(s2);
return n1-n2;
}
Definition simple_malloc_spec :=
DECLARE _malloc
WITH t:type, gv: globals
PRE [ size_t ]
PROP (0 ≤ sizeof t ≤ Ptrofs.max_unsigned;
complete_legal_cosu_type t = true;
natural_aligned natural_alignment t = true)
PARAMS (Vptrofs (Ptrofs.repr (sizeof t)))
SEP (mem_mgr gv)
POST [ tptr tvoid ] EX p:_,
PROP ()
RETURN (p)
SEP (mem_mgr gv; data_at_ Ews t p).
DECLARE _malloc
WITH t:type, gv: globals
PRE [ size_t ]
PROP (0 ≤ sizeof t ≤ Ptrofs.max_unsigned;
complete_legal_cosu_type t = true;
natural_aligned natural_alignment t = true)
PARAMS (Vptrofs (Ptrofs.repr (sizeof t)))
SEP (mem_mgr gv)
POST [ tptr tvoid ] EX p:_,
PROP ()
RETURN (p)
SEP (mem_mgr gv; data_at_ Ews t p).
我们从一些便捷的定义开始:
Definition set_t := Tstruct _set noattr.
Definition setrep_t := Tstruct _setrep noattr.
Definition setrep_list_t := Tstruct _setrep_list noattr.
Definition setrep_array_t := Tstruct _setrep_array noattr.
Lemma data_at_tptr_whatever: ∀ sh t1 t2 q p,
is_pointer_or_null q →
data_at sh (tptr t1) q p = data_at sh (tptr t2) q p.
Proof.
intros.
destruct q; try contradiction.
red in H.
destruct Archi.ptr64; try contradiction. subst.
rewrite <- !data_at_nullptr; auto.
-
rewrite <- !data_at_int_or_ptr_ptr; auto.
Qed.
Definition setrep_t := Tstruct _setrep noattr.
Definition setrep_list_t := Tstruct _setrep_list noattr.
Definition setrep_array_t := Tstruct _setrep_array noattr.
Lemma data_at_tptr_whatever: ∀ sh t1 t2 q p,
is_pointer_or_null q →
data_at sh (tptr t1) q p = data_at sh (tptr t2) q p.
Proof.
intros.
destruct q; try contradiction.
red in H.
destruct Archi.ptr64; try contradiction. subst.
rewrite <- !data_at_nullptr; auto.
-
rewrite <- !data_at_int_or_ptr_ptr; auto.
Qed.
规约
不透明常量
本程序管理小整数的集合,即 0 ≤ k < 100。 我们将 N=100 定义为「不透明常量」;参见参考手册 VC.pdf [Appel 2015a] 的 不透明常量章节,或参见 Hashfun 章节。
Definition N : Z := proj1_sig (opaque_constant 100).
Definition N_eq : N=100 := proj2_sig (opaque_constant _).
Hint Rewrite N_eq : rep_lia.
Definition N_eq : N=100 := proj2_sig (opaque_constant _).
Hint Rewrite N_eq : rep_lia.
为什么不直接写 Definition N := 100. ?
因为我们的验证经常会用到 Zrepeat Vundef N 这样的项,
对它施加 simpl 会把它展开成一长串 Vundef,这会极大地拖慢速度。
所以「不透明常量」的技巧正是为了避免不想要的展开,
同时又允许 rep_lia 策略访问到该值。
"array" 实例用一个 100 字节的数组来表示小整数集合,其中第 i 个字节非零
当且仅当 i 属于该集合。
由于 data_at 的数组标准功能模型是一个 vals 的列表,我们定义 eval_array
来关联 Z→bool 与 list val。
数组表示的功能模型
该 C 程序包含两个【实例|Instance】 "set" 【类|Class】,它们都关联于同一个 功能模型,即标准集合论的 Z→bool。
Fixpoint eval_array (i: Z) (n: nat) (f: Z → bool) : list val :=
match n with O ⇒ nil | (S n') ⇒ Val.of_bool (f i) :: eval_array (i+1) n' f end.
match n with O ⇒ nil | (S n') ⇒ Val.of_bool (f i) :: eval_array (i+1) n' f end.
Lemma update_eval_array: ∀ n f k,
0 ≤ k < Z.of_nat n →
upd_Znth k (eval_array 0 n f) (Val.of_bool true) =
eval_array 0 n (fun k' ⇒ orb (k' =? k) (f k')).
0 ≤ k < Z.of_nat n →
upd_Znth k (eval_array 0 n f) (Val.of_bool true) =
eval_array 0 n (fun k' ⇒ orb (k' =? k) (f k')).
这个例子虽然并不完全平凡,但却是直接明了的。
(* 在这里填写 *) Admitted.
☐
☐
Lemma Znth_eval_array: ∀ n f k,
0 ≤ k < Z.of_nat n → Znth k (eval_array 0 n f) = Val.of_bool (f k).
(* 在这里填写 *) Admitted.
☐
0 ≤ k < Z.of_nat n → Znth k (eval_array 0 n f) = Val.of_bool (f k).
(* 在这里填写 *) Admitted.
☐
Lemma eval_array_emptyset: ∀ n,
0 ≤ n →
eval_array 0 (Z.to_nat n) (fun _ : Z ⇒ false) = Zrepeat Vfalse n.
(* 在这里填写 *) Admitted.
☐
0 ≤ n →
eval_array 0 (Z.to_nat n) (fun _ : Z ⇒ false) = Zrepeat Vfalse n.
(* 在这里填写 *) Admitted.
☐
Definition array_data_rep (v: Z→bool) (p: val) : mpred :=
data_at Ews setrep_array_t (eval_array 0 (Z.to_nat N) v) p.
data_at Ews setrep_array_t (eval_array 0 (Z.to_nat N) v) p.
Fixpoint listrep (il: list Z) (p: val) : mpred :=
match il with
| i::il' ⇒ EX y: val,
data_at Ews setrep_list_t (Vint (Int.repr i),y) p ×
listrep il' y
| nil ⇒ !! (p = nullval) && emp
end.
match il with
| i::il' ⇒ EX y: val,
data_at Ews setrep_list_t (Vint (Int.repr i),y) p ×
listrep il' y
| nil ⇒ !! (p = nullval) && emp
end.
与往常一样,如果你禁用自动简化,用户自定义的分离谓词的证明自动化效果会更好,如下所示:
与往常一样,我们应该为 Hint 数据库
saturate_local 和 valid_pointer 填充内容。
练习: 1 star,标准 (set_listrep_properties)
想一想你是否还记得如何证明这些,或者重新看看 Verif_reverse 了解具体做法。
Lemma listrep_local_prop: ∀ il p, listrep il p |--
!! (is_pointer_or_null p ∧ (p=nullval ↔ il=nil)).
(* 在这里填写 *) Admitted.
#[export] Hint Resolve listrep_local_prop : saturate_local.
Lemma listrep_valid_pointer:
∀ il p,
listrep il p |-- valid_pointer p.
(* 在这里填写 *) Admitted.
#[export] Hint Resolve listrep_valid_pointer : valid_pointer.
☐
!! (is_pointer_or_null p ∧ (p=nullval ↔ il=nil)).
(* 在这里填写 *) Admitted.
#[export] Hint Resolve listrep_local_prop : saturate_local.
Lemma listrep_valid_pointer:
∀ il p,
listrep il p |-- valid_pointer p.
(* 在这里填写 *) Admitted.
#[export] Hint Resolve listrep_valid_pointer : valid_pointer.
☐
Definition list_data_rep (f: Z → bool) (p: val) :=
EX al: list Z,
!! (Forall (fun i ⇒ 0 ≤ i < N) al ∧ ∀ i, f i = true ↔ In i al) &&
listrep al p.
EX al: list Z,
!! (Forall (fun i ⇒ 0 ≤ i < N) al ∧ ∀ i, f i = true ↔ In i al) &&
listrep al p.
array_data_rep 与 list_data_rep 具有相同的 Rocq 类型,这是有意为之
Remark data_rep_types:
ltac:(match type of list_data_rep with ?t ⇒ exact t end) =
ltac:(match type of array_data_rep with ?t ⇒ exact t end).
Proof. reflexivity. Qed.
(* ** 函数规约 *)
Definition set_insert_array_spec : ident × funspec :=
DECLARE _set_insert_array
WITH obj: val, f: Z → bool, k: Z, p: val, gv: globals
PRE [ tptr (tptr setrep_t) , tint ]
PROP(0 ≤ k < N)
PARAMS (obj; Vint (Int.repr k)) GLOBALS(gv)
SEP(data_at Ews (tptr setrep_t) p obj;
array_data_rep f p;
mem_mgr gv)
POST [ tvoid ]
EX p',
PROP(is_pointer_or_null p')
RETURN ()
SEP (data_at Ews (tptr setrep_t) p' obj;
array_data_rep (fun k' ⇒ orb (Z.eqb k' k) (f k')) p';
mem_mgr gv).
Definition set_lookup_array_spec : ident × funspec :=
DECLARE _set_lookup_array
WITH obj: val, f: Z → bool, k: Z, p: val
PRE [ tptr (tptr setrep_t) , tint ]
PROP(0 ≤ k < N)
PARAMS (obj; Vint (Int.repr k))
SEP(data_at Ews (tptr setrep_t) p obj;
array_data_rep f p)
POST [ tint ]
PROP()
RETURN ( Val.of_bool (f k) )
SEP (data_at Ews (tptr setrep_t) p obj;
array_data_rep f p).
Definition set_insert_list_spec : ident × funspec :=
DECLARE _set_insert_list
WITH obj: val, f: Z → bool, k: Z, p: val, gv: globals
PRE [ tptr (tptr setrep_t) , tint ]
PROP(0 ≤ k < N)
PARAMS (obj; Vint (Int.repr k)) GLOBALS(gv)
SEP(data_at Ews (tptr setrep_t) p obj;
list_data_rep f p;
mem_mgr gv)
POST [ tvoid ]
EX p',
PROP(is_pointer_or_null p')
RETURN ()
SEP (data_at Ews (tptr setrep_t) p' obj;
list_data_rep (fun k' ⇒ orb (Z.eqb k' k) (f k')) p';
mem_mgr gv).
Definition set_lookup_list_spec : ident × funspec :=
DECLARE _set_lookup_list
WITH obj: val, f: Z → bool, k: Z, p: val
PRE [ tptr (tptr setrep_t) , tint ]
PROP(0 ≤ k < N)
PARAMS (obj; Vint (Int.repr k))
SEP(data_at Ews (tptr setrep_t) p obj;
list_data_rep f p)
POST [ tint ]
PROP()
RETURN ( Val.of_bool (f k) )
SEP (data_at Ews (tptr setrep_t) p obj;
list_data_rep f p).
ltac:(match type of list_data_rep with ?t ⇒ exact t end) =
ltac:(match type of array_data_rep with ?t ⇒ exact t end).
Proof. reflexivity. Qed.
(* ** 函数规约 *)
Definition set_insert_array_spec : ident × funspec :=
DECLARE _set_insert_array
WITH obj: val, f: Z → bool, k: Z, p: val, gv: globals
PRE [ tptr (tptr setrep_t) , tint ]
PROP(0 ≤ k < N)
PARAMS (obj; Vint (Int.repr k)) GLOBALS(gv)
SEP(data_at Ews (tptr setrep_t) p obj;
array_data_rep f p;
mem_mgr gv)
POST [ tvoid ]
EX p',
PROP(is_pointer_or_null p')
RETURN ()
SEP (data_at Ews (tptr setrep_t) p' obj;
array_data_rep (fun k' ⇒ orb (Z.eqb k' k) (f k')) p';
mem_mgr gv).
Definition set_lookup_array_spec : ident × funspec :=
DECLARE _set_lookup_array
WITH obj: val, f: Z → bool, k: Z, p: val
PRE [ tptr (tptr setrep_t) , tint ]
PROP(0 ≤ k < N)
PARAMS (obj; Vint (Int.repr k))
SEP(data_at Ews (tptr setrep_t) p obj;
array_data_rep f p)
POST [ tint ]
PROP()
RETURN ( Val.of_bool (f k) )
SEP (data_at Ews (tptr setrep_t) p obj;
array_data_rep f p).
Definition set_insert_list_spec : ident × funspec :=
DECLARE _set_insert_list
WITH obj: val, f: Z → bool, k: Z, p: val, gv: globals
PRE [ tptr (tptr setrep_t) , tint ]
PROP(0 ≤ k < N)
PARAMS (obj; Vint (Int.repr k)) GLOBALS(gv)
SEP(data_at Ews (tptr setrep_t) p obj;
list_data_rep f p;
mem_mgr gv)
POST [ tvoid ]
EX p',
PROP(is_pointer_or_null p')
RETURN ()
SEP (data_at Ews (tptr setrep_t) p' obj;
list_data_rep (fun k' ⇒ orb (Z.eqb k' k) (f k')) p';
mem_mgr gv).
Definition set_lookup_list_spec : ident × funspec :=
DECLARE _set_lookup_list
WITH obj: val, f: Z → bool, k: Z, p: val
PRE [ tptr (tptr setrep_t) , tint ]
PROP(0 ≤ k < N)
PARAMS (obj; Vint (Int.repr k))
SEP(data_at Ews (tptr setrep_t) p obj;
list_data_rep f p)
POST [ tint ]
PROP()
RETURN ( Val.of_bool (f k) )
SEP (data_at Ews (tptr setrep_t) p obj;
list_data_rep f p).
看看数组与列表的「insert」【函数规约|Funspec】是多么相似:
若 array_data_rep 与 list_data_rep 相等 (其实并非如此!)
它们就会完全相同。
Remark insert_specs_similar: array_data_rep = list_data_rep →
snd set_insert_array_spec = snd set_insert_list_spec.
Proof.
intros; simpl; rewrite H; reflexivity.
Qed.
Remark lookup_specs_similar: array_data_rep = list_data_rep →
snd set_lookup_array_spec = snd set_lookup_list_spec.
Proof.
intros; simpl; rewrite H; reflexivity.
Qed.
snd set_insert_array_spec = snd set_insert_list_spec.
Proof.
intros; simpl; rewrite H; reflexivity.
Qed.
Remark lookup_specs_similar: array_data_rep = list_data_rep →
snd set_lookup_array_spec = snd set_lookup_list_spec.
Proof.
intros; simpl; rewrite H; reflexivity.
Qed.
你可能已经注意到 C 函数 set_insert_list 使用了 malloc,
因此在函数规约(funspec)set_insert_list_spec 中我们需要 mem_mgr gv 和 GLOBALS(gv)。
由于 set_insert_array 不使用 malloc,人们可能以为它的函数规约不需要 mem_mgr gv 和 GLOBALS(gv),
但我们需要确保两个函数规约保持相似,所以我们在 set_insert_array_spec 中人为地加入了那些依赖。
练习: 1 star,标准 (set_insert_array_body)
Lemma set_insert_array_body: ∀ Gprog, semax_body Vprog Gprog f_set_insert_array set_insert_array_spec.
Proof.
intros.
start_function.
unfold array_data_rep.
assert_PROP (isptr p) by entailer!.
rewrite (data_at_tptr_whatever Ews setrep_t setrep_array_t) by auto.
(* 在这里填写 *) Admitted.
☐
Proof.
intros.
start_function.
unfold array_data_rep.
assert_PROP (isptr p) by entailer!.
rewrite (data_at_tptr_whatever Ews setrep_t setrep_array_t) by auto.
(* 在这里填写 *) Admitted.
☐
Lemma set_lookup_array_body: ∀ Gprog, semax_body Vprog Gprog f_set_lookup_array set_lookup_array_spec.
(* 在这里填写 *) Admitted.
☐
(* 在这里填写 *) Admitted.
☐
Lemma set_insert_list_body: semax_body Vprog [simple_malloc_spec] f_set_insert_list set_insert_list_spec.
Proof.
intros.
start_function.
unfold list_data_rep.
Intros al.
assert_PROP (is_pointer_or_null p) by entailer!.
rewrite (data_at_tptr_whatever Ews setrep_t setrep_list_t) by auto.
(* 在这里填写 *) Admitted.
☐
Proof.
intros.
start_function.
unfold list_data_rep.
Intros al.
assert_PROP (is_pointer_or_null p) by entailer!.
rewrite (data_at_tptr_whatever Ews setrep_t setrep_list_t) by auto.
(* 在这里填写 *) Admitted.
☐
Lemma set_lookup_list_body: ∀ Gprog, semax_body Vprog Gprog f_set_lookup_list set_lookup_list_spec.
(* 在这里填写 *) Admitted.
☐
(* 在这里填写 *) Admitted.
☐
作为一个热身练习,在完全一般地证明面向对象版本之前,
我们先针对数组表示这一特例,用函数指针来证明它。
类 set 的一个实例由一个 3 词的结构体表示,包含一个数据指针和
两个方法指针。如果只允许存在一个实例,即数组实例,
我们可以这样写:
Definition set_obj (v: Z→bool) (obj: val) :=
EX p: val, EX insert: val, EX lookup: val,
!! is_pointer_or_null p &&
func_ptr' (snd set_insert_array_spec) insert ×
func_ptr' (snd set_lookup_array_spec) lookup ×
data_at Ews set_t (p,(insert,lookup)) obj ×
array_data_rep v p.
Definition set_create_array_spec : ident × funspec :=
DECLARE _set_create_array
WITH gv : globals
PRE [ ]
PROP ()
PARAMS () GLOBALS(gv)
SEP (mem_mgr gv)
POST [ tptr set_t ]
EX obj: val,
PROP ()
RETURN (obj)
SEP (set_obj (fun _ ⇒ false) obj; mem_mgr gv).
Definition set_insert_spec :=
DECLARE _set_insert
WITH obj: val, f: Z → bool, k: Z, gv: globals
PRE [ tptr set_t , tint ]
PROP (0 ≤ k < N)
PARAMS (obj; Vint (Int.repr k)) GLOBALS(gv)
SEP (set_obj f obj; mem_mgr gv)
POST [ tvoid ]
PROP() RETURN()
SEP (set_obj (fun k' ⇒ orb (Z.eqb k' k) (f k')) obj; mem_mgr gv).
Definition set_lookup_spec :=
DECLARE _set_lookup
WITH obj: val, f: Z → bool, k: Z
PRE [ tptr set_t , tint ]
PROP (0 ≤ k < N)
PARAMS (obj; Vint (Int.repr k))
SEP (set_obj f obj)
POST [ tint ]
PROP() RETURN(Val.of_bool (f k))
SEP (set_obj f obj).
Definition set_client_spec :=
DECLARE _set_client
WITH obj: val, gv: globals
PRE [ tptr set_t ]
PROP ()
PARAMS (obj) GLOBALS(gv)
SEP (set_obj (fun _ ⇒ false) obj; mem_mgr gv)
POST [ tint ]
PROP() RETURN (Vint (Int.repr 1)) SEP(mem_mgr gv; TT).
Definition Gprog := [simple_malloc_spec;
set_insert_array_spec; set_lookup_array_spec; set_create_array_spec;
set_insert_list_spec; set_lookup_list_spec;
set_insert_spec; set_lookup_spec; set_client_spec].
EX p: val, EX insert: val, EX lookup: val,
!! is_pointer_or_null p &&
func_ptr' (snd set_insert_array_spec) insert ×
func_ptr' (snd set_lookup_array_spec) lookup ×
data_at Ews set_t (p,(insert,lookup)) obj ×
array_data_rep v p.
Definition set_create_array_spec : ident × funspec :=
DECLARE _set_create_array
WITH gv : globals
PRE [ ]
PROP ()
PARAMS () GLOBALS(gv)
SEP (mem_mgr gv)
POST [ tptr set_t ]
EX obj: val,
PROP ()
RETURN (obj)
SEP (set_obj (fun _ ⇒ false) obj; mem_mgr gv).
Definition set_insert_spec :=
DECLARE _set_insert
WITH obj: val, f: Z → bool, k: Z, gv: globals
PRE [ tptr set_t , tint ]
PROP (0 ≤ k < N)
PARAMS (obj; Vint (Int.repr k)) GLOBALS(gv)
SEP (set_obj f obj; mem_mgr gv)
POST [ tvoid ]
PROP() RETURN()
SEP (set_obj (fun k' ⇒ orb (Z.eqb k' k) (f k')) obj; mem_mgr gv).
Definition set_lookup_spec :=
DECLARE _set_lookup
WITH obj: val, f: Z → bool, k: Z
PRE [ tptr set_t , tint ]
PROP (0 ≤ k < N)
PARAMS (obj; Vint (Int.repr k))
SEP (set_obj f obj)
POST [ tint ]
PROP() RETURN(Val.of_bool (f k))
SEP (set_obj f obj).
Definition set_client_spec :=
DECLARE _set_client
WITH obj: val, gv: globals
PRE [ tptr set_t ]
PROP ()
PARAMS (obj) GLOBALS(gv)
SEP (set_obj (fun _ ⇒ false) obj; mem_mgr gv)
POST [ tint ]
PROP() RETURN (Vint (Int.repr 1)) SEP(mem_mgr gv; TT).
Definition Gprog := [simple_malloc_spec;
set_insert_array_spec; set_lookup_array_spec; set_create_array_spec;
set_insert_list_spec; set_lookup_list_spec;
set_insert_spec; set_lookup_spec; set_client_spec].
Lemma set_create_array_body: semax_body Vprog Gprog f_set_create_array set_create_array_spec.
Proof.
start_function.
forward_call (set_t, gv).
Intros p.
forward_call (setrep_array_t, gv).
Intros a.
pose (allfalse := Zrepeat (Val.of_bool false) N : reptype setrep_array_t).
apply semax_seq' with (PROP ( )
LOCAL (temp _i (Vint (Int.repr N)); temp _a a; temp _p p; gvars gv)
SEP (mem_mgr gv; data_at_ Ews set_t p;
data_at Ews setrep_array_t allfalse a))%assert.
-
(* 在这里填写 *) admit.
-
subst MORE_COMMANDS; unfold abbreviate.
forward.
make_func_ptr _set_insert_array.
make_func_ptr _set_lookup_array.
change (func_ptr' _ (gv _set_lookup_array))
with (func_ptr' (snd set_lookup_array_spec) (gv _set_lookup_array)).
change (func_ptr' _ (gv _set_insert_array))
with (func_ptr' (snd set_insert_array_spec) (gv _set_insert_array)).
forward.
forward.
forward.
Exists p.
unfold set_obj, array_data_rep.
Exists a (gv _set_insert_array) (gv _set_lookup_array).
entailer!!.
rewrite eval_array_emptyset by rep_lia.
apply derives_refl.
(* 在这里填写 *) Admitted.
☐
Proof.
start_function.
forward_call (set_t, gv).
Intros p.
forward_call (setrep_array_t, gv).
Intros a.
pose (allfalse := Zrepeat (Val.of_bool false) N : reptype setrep_array_t).
apply semax_seq' with (PROP ( )
LOCAL (temp _i (Vint (Int.repr N)); temp _a a; temp _p p; gvars gv)
SEP (mem_mgr gv; data_at_ Ews set_t p;
data_at Ews setrep_array_t allfalse a))%assert.
-
(* 在这里填写 *) admit.
-
subst MORE_COMMANDS; unfold abbreviate.
forward.
make_func_ptr _set_insert_array.
make_func_ptr _set_lookup_array.
change (func_ptr' _ (gv _set_lookup_array))
with (func_ptr' (snd set_lookup_array_spec) (gv _set_lookup_array)).
change (func_ptr' _ (gv _set_insert_array))
with (func_ptr' (snd set_insert_array_spec) (gv _set_insert_array)).
forward.
forward.
forward.
Exists p.
unfold set_obj, array_data_rep.
Exists a (gv _set_insert_array) (gv _set_lookup_array).
entailer!!.
rewrite eval_array_emptyset by rep_lia.
apply derives_refl.
(* 在这里填写 *) Admitted.
☐
Lemma set_insert_body: semax_body Vprog Gprog f_set_insert set_insert_spec.
Proof.
start_function.
unfold set_obj.
Intros p insert lookup.
assert_PROP (field_compatible set_t (DOT _objdata) obj) as Hobj by entailer!.
forward.
forward_call (obj, f, k, p, gv).
unfold_data_at (data_at Ews set_t _ _).
rewrite field_at_data_at'.
simpl nested_field_type. simpl offset_val.
rewrite isptr_offset_val_zero; auto with field_compatible.
Intros.
fold setrep_t.
cancel.
Intros p'.
assert_PROP (isptr p') as Hp' by (unfold array_data_rep; entailer!).
entailer!!.
unfold set_obj.
Exists p' insert lookup.
entailer!!.
unfold_data_at (data_at Ews set_t _ obj).
cancel.
rewrite field_at_data_at'.
simpl.
entailer!!.
Qed.
Proof.
start_function.
unfold set_obj.
Intros p insert lookup.
assert_PROP (field_compatible set_t (DOT _objdata) obj) as Hobj by entailer!.
forward.
forward_call (obj, f, k, p, gv).
unfold_data_at (data_at Ews set_t _ _).
rewrite field_at_data_at'.
simpl nested_field_type. simpl offset_val.
rewrite isptr_offset_val_zero; auto with field_compatible.
Intros.
fold setrep_t.
cancel.
Intros p'.
assert_PROP (isptr p') as Hp' by (unfold array_data_rep; entailer!).
entailer!!.
unfold set_obj.
Exists p' insert lookup.
entailer!!.
unfold_data_at (data_at Ews set_t _ obj).
cancel.
rewrite field_at_data_at'.
simpl.
entailer!!.
Qed.
Lemma set_client_body: semax_body Vprog Gprog f_set_client set_client_spec.
Proof.
start_function.
forward_call.
forward_call.
forward_call.
forward_call.
forward_call.
forward.
cancel.
Qed.
Proof.
start_function.
forward_call.
forward_call.
forward_call.
forward_call.
forward_call.
forward.
cancel.
Qed.
现在我们遇到了问题。
正如前面所承诺的,这种方法不够通用:set_obj 的定义不允许除 array_data_rep 之外的任何表示方式。例如, 如果我们尝试证明 set_create_list,事情将无法进行。)
Definition set_create_list_spec : ident × funspec :=
DECLARE _set_create_list
WITH gv : globals
PRE [ ]
PROP ()
PARAMS () GLOBALS(gv)
SEP (mem_mgr gv)
POST [ tptr set_t ]
EX obj: val,
PROP ()
RETURN (obj)
SEP (set_obj (fun _ ⇒ false) obj; mem_mgr gv).
Lemma set_create_list_body: semax_body Vprog Gprog f_set_create_list set_create_list_spec.
Proof.
start_function.
make_func_ptr _set_insert_list.
make_func_ptr _set_lookup_list.
change (func_ptr' _ (gv _set_lookup_list))
with (func_ptr' (snd set_lookup_list_spec) (gv _set_lookup_list)).
change (func_ptr' _ (gv _set_insert_list))
with (func_ptr' (snd set_insert_list_spec) (gv _set_insert_list)).
forward_call (set_t, gv).
Intros p.
forward.
forward.
forward.
forward.
Exists p.
entailer!!.
unfold set_obj.
Exists nullval.
Exists (gv _set_insert_list) (gv _set_lookup_list).
assert (emp |-- list_data_rep (fun _ ⇒ false) nullval).
unfold list_data_rep. Exists (@nil Z). unfold listrep; entailer!!. intros; split; intro. discriminate. contradiction.
sep_apply H1.
entailer!!.
pull_right (list_data_rep (fun _ : Z ⇒ false) nullval).
pull_left (func_ptr' (snd set_insert_list_spec) (gv _set_insert_list)).
repeat apply sepcon_derives.
DECLARE _set_create_list
WITH gv : globals
PRE [ ]
PROP ()
PARAMS () GLOBALS(gv)
SEP (mem_mgr gv)
POST [ tptr set_t ]
EX obj: val,
PROP ()
RETURN (obj)
SEP (set_obj (fun _ ⇒ false) obj; mem_mgr gv).
Lemma set_create_list_body: semax_body Vprog Gprog f_set_create_list set_create_list_spec.
Proof.
start_function.
make_func_ptr _set_insert_list.
make_func_ptr _set_lookup_list.
change (func_ptr' _ (gv _set_lookup_list))
with (func_ptr' (snd set_lookup_list_spec) (gv _set_lookup_list)).
change (func_ptr' _ (gv _set_insert_list))
with (func_ptr' (snd set_insert_list_spec) (gv _set_insert_list)).
forward_call (set_t, gv).
Intros p.
forward.
forward.
forward.
forward.
Exists p.
entailer!!.
unfold set_obj.
Exists nullval.
Exists (gv _set_insert_list) (gv _set_lookup_list).
assert (emp |-- list_data_rep (fun _ ⇒ false) nullval).
unfold list_data_rep. Exists (@nil Z). unfold listrep; entailer!!. intros; split; intro. discriminate. contradiction.
sep_apply H1.
entailer!!.
pull_right (list_data_rep (fun _ : Z ⇒ false) nullval).
pull_left (func_ptr' (snd set_insert_list_spec) (gv _set_insert_list)).
repeat apply sepcon_derives.
到这里我们走进了死胡同。
- 函数规约不匹配,即 snd set_insert_list_spec ≠ snd set_insert_array_spec
- 数据表示不匹配,即 list_data_rep ≠ array_data_rep
用抽象的 data_rep 谓词对 obj_rep 进行参数化
本问题的解决方案是将 set_obj 谓词参数化为一个关于内部(实例特定)数据表示的谓词。我们将把这类数据表示谓词的类型定义为,
Definition set_data_rep : Type := (Z → bool) → val → mpred.
Check array_data_rep : set_data_rep.
Check list_data_rep : set_data_rep.
Check array_data_rep : set_data_rep.
Check list_data_rep : set_data_rep.
现在我们取之前对 set_insert_array_spec 和 set_insert_list_spec 的定义,
并抽取出集合的数据表示:
Definition set_insert_method_spec (setrep: set_data_rep): funspec :=
WITH obj: val, f: Z → bool, k: Z, p: val, gv: globals
PRE [ tptr (tptr setrep_t) , tint ]
PROP(0 ≤ k < N)
PARAMS (obj; Vint (Int.repr k)) GLOBALS(gv)
SEP(data_at Ews (tptr setrep_t) p obj;
setrep f p;
mem_mgr gv)
POST [ tvoid ]
EX p',
PROP(is_pointer_or_null p')
RETURN ()
SEP (data_at Ews (tptr setrep_t) p' obj;
setrep (fun k' ⇒ orb (Z.eqb k' k) (f k')) p';
mem_mgr gv).
WITH obj: val, f: Z → bool, k: Z, p: val, gv: globals
PRE [ tptr (tptr setrep_t) , tint ]
PROP(0 ≤ k < N)
PARAMS (obj; Vint (Int.repr k)) GLOBALS(gv)
SEP(data_at Ews (tptr setrep_t) p obj;
setrep f p;
mem_mgr gv)
POST [ tvoid ]
EX p',
PROP(is_pointer_or_null p')
RETURN ()
SEP (data_at Ews (tptr setrep_t) p' obj;
setrep (fun k' ⇒ orb (Z.eqb k' k) (f k')) p';
mem_mgr gv).
对于 lookup 方法同样如此:
Definition set_lookup_method_spec (setrep: set_data_rep) : funspec :=
WITH obj: val, f: Z → bool, k: Z, p: val
PRE [ tptr (tptr setrep_t) , tint ]
PROP(0 ≤ k < N)
PARAMS (obj; Vint (Int.repr k))
SEP(data_at Ews (tptr setrep_t) p obj;
setrep f p)
POST [ tint ]
PROP()
RETURN ( Val.of_bool (f k) )
SEP (data_at Ews (tptr setrep_t) p obj;
setrep f p).
WITH obj: val, f: Z → bool, k: Z, p: val
PRE [ tptr (tptr setrep_t) , tint ]
PROP(0 ≤ k < N)
PARAMS (obj; Vint (Int.repr k))
SEP(data_at Ews (tptr setrep_t) p obj;
setrep f p)
POST [ tint ]
PROP()
RETURN ( Val.of_bool (f k) )
SEP (data_at Ews (tptr setrep_t) p obj;
setrep f p).
现在我们可以给出一个比上面的注记 insert_specs_similar 更有用的陈述,它毕竟具有 False → ... 的形式,因而是空洞的。
Remark set_insert_list_type_remark:
snd set_insert_list_spec = set_insert_method_spec list_data_rep.
Proof. reflexivity. Qed.
Remark set_lookup_list_type_remark:
snd set_lookup_list_spec = set_lookup_method_spec list_data_rep.
Proof. reflexivity. Qed.
Remark set_insert_array_type_remark:
snd set_insert_array_spec = set_insert_method_spec array_data_rep.
Proof. reflexivity. Qed.
Remark set_lookup_array_type_remark:
snd set_lookup_array_spec = set_lookup_method_spec array_data_rep.
Proof. reflexivity. Qed.
snd set_insert_list_spec = set_insert_method_spec list_data_rep.
Proof. reflexivity. Qed.
Remark set_lookup_list_type_remark:
snd set_lookup_list_spec = set_lookup_method_spec list_data_rep.
Proof. reflexivity. Qed.
Remark set_insert_array_type_remark:
snd set_insert_array_spec = set_insert_method_spec array_data_rep.
Proof. reflexivity. Qed.
Remark set_lookup_array_type_remark:
snd set_lookup_array_spec = set_lookup_method_spec array_data_rep.
Proof. reflexivity. Qed.
基于这些观察,我们注意到,其实没有必要把 set_insert_array_spec 定义得这么冗长;我们本可以这样写:
Definition set_insert_array_spec' : ident × funspec :=
DECLARE _set_insert_array
(set_insert_method_spec array_data_rep).
DECLARE _set_insert_array
(set_insert_method_spec array_data_rep).
……set_lookup_array_spec, set_insert_list_spec, set_lookup_list_spec 亦同。
现在我们以一种由数据表示参数化的方式来定义 set_obj:
Definition set_obj (instance: set_data_rep) (v: Z→bool) (obj: val) :=
EX p: val, EX insert: val, EX lookup: val,
!! is_pointer_or_null p &&
func_ptr' (set_insert_method_spec instance) insert ×
func_ptr' (set_lookup_method_spec instance) lookup ×
data_at Ews set_t (p,(insert,lookup)) obj ×
instance v p.
EX p: val, EX insert: val, EX lookup: val,
!! is_pointer_or_null p &&
func_ptr' (set_insert_method_spec instance) insert ×
func_ptr' (set_lookup_method_spec instance) lookup ×
data_at Ews set_t (p,(insert,lookup)) obj ×
instance v p.
Create 构造子的函数规约
Definition create_spec (setrep: set_data_rep) : funspec :=
WITH gv : globals
PRE [ ]
PROP ()
PARAMS () GLOBALS(gv)
SEP (mem_mgr gv)
POST [ tptr set_t ]
EX obj: val,
PROP ()
RETURN (obj)
SEP (set_obj setrep (fun _ ⇒ false) obj; mem_mgr gv).
Definition set_create_array_spec : ident × funspec :=
DECLARE _set_create_array
(create_spec array_data_rep).
Definition set_create_list_spec : ident × funspec :=
DECLARE _set_create_list
(create_spec list_data_rep).
WITH gv : globals
PRE [ ]
PROP ()
PARAMS () GLOBALS(gv)
SEP (mem_mgr gv)
POST [ tptr set_t ]
EX obj: val,
PROP ()
RETURN (obj)
SEP (set_obj setrep (fun _ ⇒ false) obj; mem_mgr gv).
Definition set_create_array_spec : ident × funspec :=
DECLARE _set_create_array
(create_spec array_data_rep).
Definition set_create_list_spec : ident × funspec :=
DECLARE _set_create_list
(create_spec list_data_rep).
依赖函数规约,用于指定包装函数
现在我们可以为「便捷包装函数」set_insert 制作一个参数化的【函数规约|Funspec】,它执行动态方法查找,以调用对象
insert 字段中的【函数指针|Function Pointer】。
但出了点问题……
Fail
Definition set_insert_spec :=
DECLARE _set_insert
WITH obj: val, f: Z → bool, k: Z, gv: globals, setrep: set_data_rep
PRE [ tptr set_t , tint ]
PROP (0 ≤ k < N)
PARAMS (obj; Vint (Int.repr k)) GLOBALS(gv)
SEP (set_obj setrep f obj; mem_mgr gv)
POST [ tvoid ]
PROP() RETURN()
SEP (set_obj setrep (fun k' ⇒ orb (Z.eqb k' k) (f k')) obj; mem_mgr gv).
(* 这确实会产生宇宙不一致消息:
项 "(val * (Z -> bool) * Z * globals * set_data_rep)" 的类型是
"Type@{max(Set,block.u0,set_data_rep.u0)}"
而期望类型是 "Type@{NDmk_funspec.u0}"(宇宙不一致:无法强制
set_data_rep.u0 <= NDmk_funspec.u0,因为
NDmk_funspec.u0 <= rmaps.TypeTree.u0 < Maps.TREE.elements_correct.u0 <= set_data_rep.u0)。
*)
Definition set_insert_spec :=
DECLARE _set_insert
WITH obj: val, f: Z → bool, k: Z, gv: globals, setrep: set_data_rep
PRE [ tptr set_t , tint ]
PROP (0 ≤ k < N)
PARAMS (obj; Vint (Int.repr k)) GLOBALS(gv)
SEP (set_obj setrep f obj; mem_mgr gv)
POST [ tvoid ]
PROP() RETURN()
SEP (set_obj setrep (fun k' ⇒ orb (Z.eqb k' k) (f k')) obj; mem_mgr gv).
(* 这确实会产生宇宙不一致消息:
项 "(val * (Z -> bool) * Z * globals * set_data_rep)" 的类型是
"Type@{max(Set,block.u0,set_data_rep.u0)}"
而期望类型是 "Type@{NDmk_funspec.u0}"(宇宙不一致:无法强制
set_data_rep.u0 <= NDmk_funspec.u0,因为
NDmk_funspec.u0 <= rmaps.TypeTree.u0 < Maps.TREE.elements_correct.u0 <= set_data_rep.u0)。
*)
我们在这里尝试的做法是在 WITH 子句中加入一个 setrep 参数,使得同一个 set_insert
函数调用能够作用于该类中的任意对象,尽管不同对象的内部数据表示可能有所不同。那么,为什么这会导致宇宙不一致(universe
inconsistency)呢?
为了探讨这个问题,我们先从 Rocq 中一个简单的东西开始:
Fail
Inductive t := A: (t → nat) → t.
(* 这确实会产生如下消息:
"t" 在 "(t -> nat) -> t" 中的出现不是严格正的。 *)
Fail
Inductive t := A: ∀ (arg: t → nat), t.
Inductive t := A: (t → nat) → t.
(* 这确实会产生如下消息:
"t" 在 "(t -> nat) -> t" 中的出现不是严格正的。 *)
Fail
Inductive t := A: ∀ (arg: t → nat), t.
这会因同样的非严格正性消息而失败,因为它只是同一定义的脱糖形式。
在【归纳构造演算|Calculus of Inductive Constructions】中,无法进行这种形式的逆变量化。
在类型理论中,这被称为「逆变」(contravariant),因为如果类型 t2 包含的值比类型 t1 多,
那么类型 t2→nat 包含的函数就比类型 t1→nat 少。例如,如果 x 属于 t2 但不属于 t1,
而函数 f 的定义域包含整个 t1 但不包含 x,那么 f: t1→nat 但 NOT f: t2→nat。
允许逆变归纳类型会产生悖论,导致逻辑的不一致性。
错误信息 "Non strictly positive occurrence" 正是为了防范这类悖论。
Rocq 的宇宙(Universe)系统则防范另一类悖论:「包含自身的类型」。
让我们考察上面尝试的 set_insert_spec。WITH 子句的行为类似于一个
全称量化器。它对 obj: val, f: Z → bool, k: Z, gv: globals 进行量化,这些都是没问题的,
但它还对 setrep: set_data_rep 进行量化,其中 set_data_rep = (Z→bool)->val→mpred。
也就是说,set_insert_spec 试图构造一个对 mpred 进行量化的 mpred。
这是「非直谓量化」(impredicative quantification)的一个例子,如果在 Rocq 中允许,
就会导致悖论,因此被宇宙检查器排除。
然而,为了推理面向对象(object-oriented)程序(无论是用 C、C++、Java 还是其他语言),
我们常常需要某种非直谓量化。在函数式编程语言中推理也是如此
(即使它们并非官方的「面向对象」语言),当涉及使用「把抽象类型当作存在类型」的函数闭包时。
最后,面向对象方法的「this」或「self」参数就是一种逆变量化。
(不过本章展示的简单面向对象模式并没有演示「self」参数。)
为了支持所有这些推理模式,VST 的程序逻辑支持一种非直谓性。
(Iris 程序逻辑同样如此,它的灵感正来自 VST 关于非直谓性的语义模型。)
要使用 VST 的这一高级特性来构造 WITH 子句中包含 mpred 的 funspec,
我们必须构造一个「依赖 funspec」(dependent funspec)。
你已经见过形如 DECLARE _ WITH _ ... 的 funspec;
这些是「非依赖」funspec,比较简单。
要构造依赖 funspec,我们按如下方式进行:
新增的部分是:TYPE 以及【非扩张性|Nonexpansiveness】的证明。
TYPE 是 WITH 子句中各类型的一个【重化|Reified】描述,
其组织方式使得 VST 能够找到任何 mpred 藏在何处。
非扩张性是一项技术性质,在 Program Logics for Certified Compilers [Appel 2014]
第 15 章中有解释。
但我们无需在此解释它,因为(当一切顺利时)它
会由 nonexpansive_prover 策略完全自动地证明。
让我们看一个例子:
Program Definition my_function_spec : ident * funspec := DECLARE _my_function TYPE ... WITH ... PRE ... POST. Next Obligation. . . . proof of nonexpansiveness of precondition. Qed. Next Obligation. . . . proof of nonexpansiveness of postcondition. Qed.
Import rmaps.
Import compcert_rmaps.RML.R.
Import compcert_rmaps.RML.R.
set_insert_spec 的 WITH 子句将如下所示,
为了构造一个依赖的【函数规约|Funspec】,我们需要借助 TypeTree 构造子,
对该类型给出一个半深嵌入的描述,如下所示:
- WITH obj: val, f: Z → bool, k: Z, gv: globals, setrep: set_data_rep
- val × (Z→bool) × Z × globals × set_data_rep
Definition set_insert_withtype : TypeTree :=
ProdType (ConstType (val × (Z→bool) × Z × globals))
(ArrowType (ConstType (Z→bool))
(ArrowType (ConstType val)
Mpred)).
(* 为了表明这确实是对所需类型的重化,我们可以把它「反映」出来: *)
Eval cbv [dependent_type_function_rec set_insert_withtype] in dependent_type_function_rec nil mpred
set_insert_withtype.
(* = (val * (Z -> bool) * Z * (positive -> val) * ((Z -> bool) -> val -> mpred))*)
ProdType (ConstType (val × (Z→bool) × Z × globals))
(ArrowType (ConstType (Z→bool))
(ArrowType (ConstType val)
Mpred)).
(* 为了表明这确实是对所需类型的重化,我们可以把它「反映」出来: *)
Eval cbv [dependent_type_function_rec set_insert_withtype] in dependent_type_function_rec nil mpred
set_insert_withtype.
(* = (val * (Z -> bool) * Z * (positive -> val) * ((Z -> bool) -> val -> mpred))*)
我们也可以改写为,
Definition set_insert_withtype_alt : TypeTree :=
ProdType (
ProdType (
ProdType (
ProdType (ConstType val)
(ArrowType (ConstType Z) (ConstType bool)))
(ConstType Z))
(ConstType globals))
(ArrowType (ConstType (Z→bool))
(ArrowType (ConstType val)
Mpred)).
Eval cbv [dependent_type_function_rec set_insert_withtype_alt] in dependent_type_function_rec nil mpred
set_insert_withtype_alt.
ProdType (
ProdType (
ProdType (
ProdType (ConstType val)
(ArrowType (ConstType Z) (ConstType bool)))
(ConstType Z))
(ConstType globals))
(ArrowType (ConstType (Z→bool))
(ArrowType (ConstType val)
Mpred)).
Eval cbv [dependent_type_function_rec set_insert_withtype_alt] in dependent_type_function_rec nil mpred
set_insert_withtype_alt.
然而,对于任何不含 mpreds 的组件,无需对其内部细节进行具体化,
而且写成 ConstType (val × (Z→bool) × Z × globals) 更为简便。
我们现在在一个依赖【函数规约|Funspec】的 TYPE 子句中使用这个重化类型:
Program Definition set_insert_spec :=
DECLARE _set_insert
TYPE set_insert_withtype
WITH obj: val, f: Z → bool, k: Z, gv: globals, setrep: set_data_rep
PRE [ tptr set_t , tint ]
PROP (0 ≤ k < N)
PARAMS (obj; Vint (Int.repr k)) GLOBALS(gv)
SEP (set_obj setrep f obj; mem_mgr gv)
POST [ tvoid ]
PROP() RETURN()
SEP (set_obj setrep (fun k' ⇒ orb (Z.eqb k' k) (f k')) obj; mem_mgr gv).
DECLARE _set_insert
TYPE set_insert_withtype
WITH obj: val, f: Z → bool, k: Z, gv: globals, setrep: set_data_rep
PRE [ tptr set_t , tint ]
PROP (0 ≤ k < N)
PARAMS (obj; Vint (Int.repr k)) GLOBALS(gv)
SEP (set_obj setrep f obj; mem_mgr gv)
POST [ tvoid ]
PROP() RETURN()
SEP (set_obj setrep (fun k' ⇒ orb (Z.eqb k' k) (f k')) obj; mem_mgr gv).
剩下的是两个证明义务,分别证明【前置条件|Precondition】和【后置条件|Postcondition】的【非扩张性|Nonexpansiveness】
性质。这可以使用 nonexpansive_prover 来完成:
Next Obligation.
nonexpansive_prover.
unfold set_obj.
nonexpansive_prover.
Qed.
Next Obligation.
nonexpansive_prover.
unfold set_obj.
nonexpansive_prover.
Qed.
nonexpansive_prover.
unfold set_obj.
nonexpansive_prover.
Qed.
Next Obligation.
nonexpansive_prover.
unfold set_obj.
nonexpansive_prover.
Qed.
客户端程序的函数规约
int set_client(struct set *p) {
set_insert(p,3);
set_insert(p,5);
set_insert(p,3);
return set_lookup(p,1) + set_lookup(p,3);
}
其函数规约如下:
Program Definition set_client_spec :=
DECLARE _set_client
TYPE ProdType (ConstType (val × globals))
(ArrowType (ConstType (Z→bool))
(ArrowType (ConstType val)
Mpred))
WITH obj: val, gv: globals, setrep: set_data_rep
PRE [ tptr set_t ]
PROP ()
PARAMS (obj) GLOBALS(gv)
SEP (set_obj setrep (fun _ ⇒ false) obj; mem_mgr gv)
POST [ tint ]
PROP() RETURN (Vint (Int.repr 1)) SEP(mem_mgr gv; TT).
Next Obligation.
nonexpansive_prover.
unfold set_obj.
nonexpansive_prover.
Qed.
Next Obligation.
nonexpansive_prover.
Qed.
DECLARE _set_client
TYPE ProdType (ConstType (val × globals))
(ArrowType (ConstType (Z→bool))
(ArrowType (ConstType val)
Mpred))
WITH obj: val, gv: globals, setrep: set_data_rep
PRE [ tptr set_t ]
PROP ()
PARAMS (obj) GLOBALS(gv)
SEP (set_obj setrep (fun _ ⇒ false) obj; mem_mgr gv)
POST [ tint ]
PROP() RETURN (Vint (Int.repr 1)) SEP(mem_mgr gv; TT).
Next Obligation.
nonexpansive_prover.
unfold set_obj.
nonexpansive_prover.
Qed.
Next Obligation.
nonexpansive_prover.
Qed.
这里再次给出 main 客户端函数:
下面这个关于 main 的规约看起来微不足道,其实并非如此!
它声称操作数组集的客户端返回的值,与操作列表规约的客户端返回的值相同。
/* main function */
int main(void) {
struct set *s1 = set_create_list();
struct set *s2 = set_create_array();
int n1 = set_client(s1);
int n2 = set_client(s2);
return n1-n2;
}
Definition main_spec :=
DECLARE _main
WITH gv: globals
PRE [ ]
PROP () PARAMS () GLOBALS (gv) SEP(mem_mgr gv)
POST [ tint ]
PROP () RETURN(Vint Int.zero) SEP(TT).
DECLARE _main
WITH gv: globals
PRE [ ]
PROP () PARAMS () GLOBALS (gv) SEP(mem_mgr gv)
POST [ tint ]
PROP () RETURN(Vint Int.zero) SEP(TT).
函数体的证明
与往常一样,我们首先创建一个 Gprog 函数规约(funspec)列表。
Definition Gprog := [simple_malloc_spec;
set_insert_array_spec; set_lookup_array_spec; set_create_array_spec;
set_insert_list_spec; set_lookup_list_spec; set_create_list_spec;
set_insert_spec; set_lookup_spec; set_client_spec;
main_spec].
set_insert_array_spec; set_lookup_array_spec; set_create_array_spec;
set_insert_list_spec; set_lookup_list_spec; set_create_list_spec;
set_insert_spec; set_lookup_spec; set_client_spec;
main_spec].
该证明与上面的 set_create_array_body_warmup 类似
(* 在这里填写 *) Admitted.
☐
☐
Lemma set_insert_body: semax_body Vprog Gprog f_set_insert set_insert_spec.
Proof.
start_function.
unfold set_obj.
Intros p insert lookup.
forward.
assert_PROP (field_compatible set_t (DOT _objdata) obj) as Hobj by entailer!.
Proof.
start_function.
unfold set_obj.
Intros p insert lookup.
forward.
assert_PROP (field_compatible set_t (DOT _objdata) obj) as Hobj by entailer!.
在这里,我们调用的是一个函数指针(Function Pointer)。也就是说,
- LOCAL 子句断言临时变量 _t'1 中包含的值是 insert,
- SEP 子句断言 insert 是一个函数指针(Function Pointer),其函数规约(funspec) 为 set_insert_method_spec setrep,
- 其中 setrep 是一个抽象谓词(见上方那一行!)。
展开并改写,把前置条件资源与框架分开:
unfold_data_at (data_at Ews set_t _ _).
rewrite field_at_data_at'.
simpl nested_field_type. simpl offset_val.
rewrite isptr_offset_val_zero; auto with field_compatible.
Intros.
fold setrep_t.
cancel.
}
rewrite field_at_data_at'.
simpl nested_field_type. simpl offset_val.
rewrite isptr_offset_val_zero; auto with field_compatible.
Intros.
fold setrep_t.
cancel.
}
调用之后
Intros p'.
entailer!!.
unfold set_obj.
Exists p' insert lookup.
entailer!!.
unfold_data_at (data_at Ews set_t _ obj).
cancel.
rewrite field_at_data_at'.
simpl.
entailer!!.
Qed.
entailer!!.
unfold set_obj.
Exists p' insert lookup.
entailer!!.
unfold_data_at (data_at Ews set_t _ obj).
cancel.
rewrite field_at_data_at'.
simpl.
entailer!!.
Qed.
提示:与 set_insert_body 非常类似。
(* 在这里填写 *) Admitted.
☐
☐
提示:在你定义了 set_client_spec 之后,这个证明如下所示:
Proof. start_function. fastforward. cancel. Qed.
(* 在这里填写 *) Admitted.
☐
☐
提示:使用 fastforward
(* 在这里填写 *) Admitted.
- 抽象表示谓词,允许不同实例具有不同的表示
- 统一的规约接口,使得可以针对抽象规约来验证客户端程序
- 创建函数指针并将其存入对象
- 取出函数指针并对它们进行调用
- 使用依赖型函数规约来处理在一个 mpred(隐藏的表示不变式)内部引用另一个 mpred(函数规约)所导致的非直谓量化
(* 2026-11-26 16:01 *)
