13903
|
1 |
(* Title: HOL/Hoare/Separation.thy
|
|
2 |
Author: Tobias Nipkow
|
|
3 |
Copyright 2003 TUM
|
|
4 |
|
|
5 |
A first attempt at a nice syntactic embedding of separation logic.
|
14074
|
6 |
Already builds on the theory for list abstractions.
|
|
7 |
|
|
8 |
If we suppress the H parameter for "List", we have to hardwired this
|
|
9 |
into parser and pretty printer, which is not very modular.
|
|
10 |
Alternative: some syntax like <P> which stands for P H. No more
|
|
11 |
compact, but avoids the funny H.
|
|
12 |
|
13903
|
13 |
*)
|
|
14 |
|
16417
|
15 |
theory Separation imports HoareAbort SepLogHeap begin
|
13857
|
16 |
|
|
17 |
text{* The semantic definition of a few connectives: *}
|
|
18 |
|
|
19 |
constdefs
|
13875
|
20 |
ortho:: "heap \<Rightarrow> heap \<Rightarrow> bool" (infix "\<bottom>" 55)
|
|
21 |
"h1 \<bottom> h2 == dom h1 \<inter> dom h2 = {}"
|
13857
|
22 |
|
13875
|
23 |
is_empty :: "heap \<Rightarrow> bool"
|
|
24 |
"is_empty h == h = empty"
|
13857
|
25 |
|
|
26 |
singl:: "heap \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool"
|
|
27 |
"singl h x y == dom h = {x} & h x = Some y"
|
|
28 |
|
13875
|
29 |
star:: "(heap \<Rightarrow> bool) \<Rightarrow> (heap \<Rightarrow> bool) \<Rightarrow> (heap \<Rightarrow> bool)"
|
|
30 |
"star P Q == \<lambda>h. \<exists>h1 h2. h = h1++h2 \<and> h1 \<bottom> h2 \<and> P h1 \<and> Q h2"
|
|
31 |
|
|
32 |
wand:: "(heap \<Rightarrow> bool) \<Rightarrow> (heap \<Rightarrow> bool) \<Rightarrow> (heap \<Rightarrow> bool)"
|
|
33 |
"wand P Q == \<lambda>h. \<forall>h'. h' \<bottom> h \<and> P h' \<longrightarrow> Q(h++h')"
|
|
34 |
|
13903
|
35 |
text{*This is what assertions look like without any syntactic sugar: *}
|
|
36 |
|
13857
|
37 |
lemma "VARS x y z w h
|
|
38 |
{star (%h. singl h x y) (%h. singl h z w) h}
|
|
39 |
SKIP
|
|
40 |
{x \<noteq> z}"
|
|
41 |
apply vcg
|
13875
|
42 |
apply(auto simp:star_def ortho_def singl_def)
|
13857
|
43 |
done
|
|
44 |
|
13903
|
45 |
text{* Now we add nice input syntax. To suppress the heap parameter
|
|
46 |
of the connectives, we assume it is always called H and add/remove it
|
|
47 |
upon parsing/printing. Thus every pointer program needs to have a
|
|
48 |
program variable H, and assertions should not contain any locally
|
|
49 |
bound Hs - otherwise they may bind the implicit H. *}
|
13857
|
50 |
|
|
51 |
syntax
|
35101
|
52 |
"_emp" :: "bool" ("emp")
|
|
53 |
"_singl" :: "nat \<Rightarrow> nat \<Rightarrow> bool" ("[_ \<mapsto> _]")
|
|
54 |
"_star" :: "bool \<Rightarrow> bool \<Rightarrow> bool" (infixl "**" 60)
|
|
55 |
"_wand" :: "bool \<Rightarrow> bool \<Rightarrow> bool" (infixl "-*" 60)
|
13857
|
56 |
|
17781
|
57 |
(* FIXME does not handle "_idtdummy" *)
|
13857
|
58 |
ML{*
|
13875
|
59 |
(* free_tr takes care of free vars in the scope of sep. logic connectives:
|
|
60 |
they are implicitly applied to the heap *)
|
|
61 |
fun free_tr(t as Free _) = t $ Syntax.free "H"
|
14074
|
62 |
(*
|
|
63 |
| free_tr((list as Free("List",_))$ p $ ps) = list $ Syntax.free "H" $ p $ ps
|
|
64 |
*)
|
13875
|
65 |
| free_tr t = t
|
|
66 |
|
|
67 |
fun emp_tr [] = Syntax.const "is_empty" $ Syntax.free "H"
|
|
68 |
| emp_tr ts = raise TERM ("emp_tr", ts);
|
13857
|
69 |
fun singl_tr [p,q] = Syntax.const "singl" $ Syntax.free "H" $ p $ q
|
|
70 |
| singl_tr ts = raise TERM ("singl_tr", ts);
|
|
71 |
fun star_tr [P,Q] = Syntax.const "star" $
|
13875
|
72 |
absfree("H",dummyT,free_tr P) $ absfree("H",dummyT,free_tr Q) $
|
|
73 |
Syntax.free "H"
|
|
74 |
| star_tr ts = raise TERM ("star_tr", ts);
|
|
75 |
fun wand_tr [P,Q] = Syntax.const "wand" $
|
13857
|
76 |
absfree("H",dummyT,P) $ absfree("H",dummyT,Q) $ Syntax.free "H"
|
13875
|
77 |
| wand_tr ts = raise TERM ("wand_tr", ts);
|
13857
|
78 |
*}
|
|
79 |
|
13875
|
80 |
parse_translation
|
35101
|
81 |
{* [("_emp", emp_tr), ("_singl", singl_tr),
|
|
82 |
("_star", star_tr), ("_wand", wand_tr)] *}
|
13857
|
83 |
|
13903
|
84 |
text{* Now it looks much better: *}
|
|
85 |
|
13857
|
86 |
lemma "VARS H x y z w
|
|
87 |
{[x\<mapsto>y] ** [z\<mapsto>w]}
|
|
88 |
SKIP
|
|
89 |
{x \<noteq> z}"
|
|
90 |
apply vcg
|
13875
|
91 |
apply(auto simp:star_def ortho_def singl_def)
|
|
92 |
done
|
|
93 |
|
|
94 |
lemma "VARS H x y z w
|
|
95 |
{emp ** emp}
|
|
96 |
SKIP
|
|
97 |
{emp}"
|
|
98 |
apply vcg
|
|
99 |
apply(auto simp:star_def ortho_def is_empty_def)
|
13857
|
100 |
done
|
|
101 |
|
13903
|
102 |
text{* But the output is still unreadable. Thus we also strip the heap
|
|
103 |
parameters upon output: *}
|
|
104 |
|
|
105 |
(* debugging code:
|
|
106 |
fun sot(Free(s,_)) = s
|
|
107 |
| sot(Var((s,i),_)) = "?" ^ s ^ string_of_int i
|
|
108 |
| sot(Const(s,_)) = s
|
|
109 |
| sot(Bound i) = "B." ^ string_of_int i
|
|
110 |
| sot(s $ t) = "(" ^ sot s ^ " " ^ sot t ^ ")"
|
|
111 |
| sot(Abs(_,_,t)) = "(% " ^ sot t ^ ")";
|
|
112 |
*)
|
13857
|
113 |
|
|
114 |
ML{*
|
13875
|
115 |
local
|
13903
|
116 |
fun strip (Abs(_,_,(t as Const("_free",_) $ Free _) $ Bound 0)) = t
|
|
117 |
| strip (Abs(_,_,(t as Free _) $ Bound 0)) = t
|
14074
|
118 |
(*
|
|
119 |
| strip (Abs(_,_,((list as Const("List",_))$ Bound 0 $ p $ ps))) = list$p$ps
|
|
120 |
*)
|
13903
|
121 |
| strip (Abs(_,_,(t as Const("_var",_) $ Var _) $ Bound 0)) = t
|
13875
|
122 |
| strip (Abs(_,_,P)) = P
|
35101
|
123 |
| strip (Const("is_empty",_)) = Syntax.const "_emp"
|
13875
|
124 |
| strip t = t;
|
|
125 |
in
|
35101
|
126 |
fun is_empty_tr' [_] = Syntax.const "_emp"
|
|
127 |
fun singl_tr' [_,p,q] = Syntax.const "_singl" $ p $ q
|
|
128 |
fun star_tr' [P,Q,_] = Syntax.const "_star" $ strip P $ strip Q
|
|
129 |
fun wand_tr' [P,Q,_] = Syntax.const "_wand" $ strip P $ strip Q
|
13875
|
130 |
end
|
13857
|
131 |
*}
|
|
132 |
|
13875
|
133 |
print_translation
|
13903
|
134 |
{* [("is_empty", is_empty_tr'),("singl", singl_tr'),
|
|
135 |
("star", star_tr'),("wand", wand_tr')] *}
|
|
136 |
|
|
137 |
text{* Now the intermediate proof states are also readable: *}
|
13857
|
138 |
|
|
139 |
lemma "VARS H x y z w
|
|
140 |
{[x\<mapsto>y] ** [z\<mapsto>w]}
|
13867
|
141 |
y := w
|
13857
|
142 |
{x \<noteq> z}"
|
|
143 |
apply vcg
|
13875
|
144 |
apply(auto simp:star_def ortho_def singl_def)
|
|
145 |
done
|
|
146 |
|
|
147 |
lemma "VARS H x y z w
|
|
148 |
{emp ** emp}
|
|
149 |
SKIP
|
|
150 |
{emp}"
|
|
151 |
apply vcg
|
|
152 |
apply(auto simp:star_def ortho_def is_empty_def)
|
|
153 |
done
|
|
154 |
|
13903
|
155 |
text{* So far we have unfolded the separation logic connectives in
|
|
156 |
proofs. Here comes a simple example of a program proof that uses a law
|
|
157 |
of separation logic instead. *}
|
|
158 |
|
13875
|
159 |
(* a law of separation logic *)
|
|
160 |
lemma star_comm: "P ** Q = Q ** P"
|
18447
|
161 |
by(auto simp add:star_def ortho_def dest: map_add_comm)
|
13875
|
162 |
|
|
163 |
lemma "VARS H x y z w
|
|
164 |
{P ** Q}
|
|
165 |
SKIP
|
|
166 |
{Q ** P}"
|
|
167 |
apply vcg
|
|
168 |
apply(simp add: star_comm)
|
|
169 |
done
|
|
170 |
|
14074
|
171 |
|
|
172 |
lemma "VARS H
|
|
173 |
{p\<noteq>0 \<and> [p \<mapsto> x] ** List H q qs}
|
|
174 |
H := H(p \<mapsto> q)
|
|
175 |
{List H p (p#qs)}"
|
|
176 |
apply vcg
|
|
177 |
apply(simp add: star_def ortho_def singl_def)
|
|
178 |
apply clarify
|
|
179 |
apply(subgoal_tac "p \<notin> set qs")
|
|
180 |
prefer 2
|
|
181 |
apply(blast dest:list_in_heap)
|
|
182 |
apply simp
|
|
183 |
done
|
|
184 |
|
|
185 |
lemma "VARS H p q r
|
|
186 |
{List H p Ps ** List H q Qs}
|
|
187 |
WHILE p \<noteq> 0
|
|
188 |
INV {\<exists>ps qs. (List H p ps ** List H q qs) \<and> rev ps @ qs = rev Ps @ Qs}
|
|
189 |
DO r := p; p := the(H p); H := H(r \<mapsto> q); q := r OD
|
|
190 |
{List H q (rev Ps @ Qs)}"
|
|
191 |
apply vcg
|
|
192 |
apply(simp_all add: star_def ortho_def singl_def)
|
|
193 |
|
|
194 |
apply fastsimp
|
|
195 |
|
|
196 |
apply (clarsimp simp add:List_non_null)
|
|
197 |
apply(rename_tac ps')
|
|
198 |
apply(rule_tac x = ps' in exI)
|
|
199 |
apply(rule_tac x = "p#qs" in exI)
|
|
200 |
apply simp
|
|
201 |
apply(rule_tac x = "h1(p:=None)" in exI)
|
|
202 |
apply(rule_tac x = "h2(p\<mapsto>q)" in exI)
|
|
203 |
apply simp
|
|
204 |
apply(rule conjI)
|
|
205 |
apply(rule ext)
|
|
206 |
apply(simp add:map_add_def split:option.split)
|
|
207 |
apply(rule conjI)
|
|
208 |
apply blast
|
|
209 |
apply(simp add:map_add_def split:option.split)
|
|
210 |
apply(rule conjI)
|
|
211 |
apply(subgoal_tac "p \<notin> set qs")
|
|
212 |
prefer 2
|
|
213 |
apply(blast dest:list_in_heap)
|
|
214 |
apply(simp)
|
|
215 |
apply fast
|
|
216 |
|
|
217 |
apply(fastsimp)
|
|
218 |
done
|
|
219 |
|
13875
|
220 |
end
|