author | haftmann |
Tue, 23 Feb 2010 10:11:15 +0100 | |
changeset 35316 | 870dfea4f9c0 |
parent 35101 | 6ce9177d6b38 |
child 35416 | d8d7d1b785af |
permissions | -rw-r--r-- |
13771 | 1 |
(* Title: HOL/Hoare/Pointers.thy |
2 |
Author: Tobias Nipkow |
|
3 |
Copyright 2002 TUM |
|
4 |
||
5 |
This is like Pointers.thy, but instead of a type constructor 'a ref |
|
6 |
that adjoins Null to a type, Null is simply a distinguished element of |
|
7 |
the address type. This avoids the Ref constructor and thus simplifies |
|
8 |
specifications (a bit). However, the proofs don't seem to get simpler |
|
9 |
- in fact in some case they appear to get (a bit) more complicated. |
|
10 |
*) |
|
11 |
||
35316
870dfea4f9c0
dropped axclass; dropped Id; session theory Hoare.thy
haftmann
parents:
35101
diff
changeset
|
12 |
theory Pointers0 imports Hoare_Logic begin |
13771 | 13 |
|
14 |
subsection "References" |
|
15 |
||
35316
870dfea4f9c0
dropped axclass; dropped Id; session theory Hoare.thy
haftmann
parents:
35101
diff
changeset
|
16 |
class ref = |
870dfea4f9c0
dropped axclass; dropped Id; session theory Hoare.thy
haftmann
parents:
35101
diff
changeset
|
17 |
fixes Null :: 'a |
13771 | 18 |
|
19 |
subsection "Field access and update" |
|
20 |
||
21 |
syntax |
|
35101 | 22 |
"_fassign" :: "'a::ref => id => 'v => 's com" |
13771 | 23 |
("(2_^._ :=/ _)" [70,1000,65] 61) |
35101 | 24 |
"_faccess" :: "'a::ref => ('a::ref \<Rightarrow> 'v) => 'v" |
13771 | 25 |
("_^._" [65,1000] 65) |
26 |
translations |
|
35101 | 27 |
"p^.f := e" => "f := CONST fun_upd f p e" |
13771 | 28 |
"p^.f" => "f p" |
29 |
||
30 |
||
31 |
text "An example due to Suzuki:" |
|
32 |
||
33 |
lemma "VARS v n |
|
34 |
{distinct[w,x,y,z]} |
|
35 |
w^.v := (1::int); w^.n := x; |
|
36 |
x^.v := 2; x^.n := y; |
|
37 |
y^.v := 3; y^.n := z; |
|
38 |
z^.v := 4; x^.n := z |
|
39 |
{w^.n^.n^.v = 4}" |
|
40 |
by vcg_simp |
|
41 |
||
42 |
||
43 |
section "The heap" |
|
44 |
||
45 |
subsection "Paths in the heap" |
|
46 |
||
47 |
consts |
|
48 |
Path :: "('a::ref \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> 'a \<Rightarrow> bool" |
|
49 |
primrec |
|
50 |
"Path h x [] y = (x = y)" |
|
51 |
"Path h x (a#as) y = (x \<noteq> Null \<and> x = a \<and> Path h (h a) as y)" |
|
52 |
||
53 |
lemma [iff]: "Path h Null xs y = (xs = [] \<and> y = Null)" |
|
54 |
apply(case_tac xs) |
|
55 |
apply fastsimp |
|
56 |
apply fastsimp |
|
57 |
done |
|
58 |
||
59 |
lemma [simp]: "a \<noteq> Null \<Longrightarrow> Path h a as z = |
|
60 |
(as = [] \<and> z = a \<or> (\<exists>bs. as = a#bs \<and> Path h (h a) bs z))" |
|
61 |
apply(case_tac as) |
|
62 |
apply fastsimp |
|
63 |
apply fastsimp |
|
64 |
done |
|
65 |
||
66 |
lemma [simp]: "\<And>x. Path f x (as@bs) z = (\<exists>y. Path f x as y \<and> Path f y bs z)" |
|
67 |
by(induct as, simp+) |
|
68 |
||
69 |
lemma [simp]: "\<And>x. u \<notin> set as \<Longrightarrow> Path (f(u := v)) x as y = Path f x as y" |
|
70 |
by(induct as, simp, simp add:eq_sym_conv) |
|
71 |
||
72 |
subsection "Lists on the heap" |
|
73 |
||
74 |
subsubsection "Relational abstraction" |
|
75 |
||
76 |
constdefs |
|
77 |
List :: "('a::ref \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> bool" |
|
78 |
"List h x as == Path h x as Null" |
|
79 |
||
80 |
lemma [simp]: "List h x [] = (x = Null)" |
|
81 |
by(simp add:List_def) |
|
82 |
||
83 |
lemma [simp]: "List h x (a#as) = (x \<noteq> Null \<and> x = a \<and> List h (h a) as)" |
|
84 |
by(simp add:List_def) |
|
85 |
||
86 |
lemma [simp]: "List h Null as = (as = [])" |
|
87 |
by(case_tac as, simp_all) |
|
88 |
||
89 |
lemma List_Ref[simp]: |
|
90 |
"a \<noteq> Null \<Longrightarrow> List h a as = (\<exists>bs. as = a#bs \<and> List h (h a) bs)" |
|
91 |
by(case_tac as, simp_all, fast) |
|
92 |
||
93 |
theorem notin_List_update[simp]: |
|
94 |
"\<And>x. a \<notin> set as \<Longrightarrow> List (h(a := y)) x as = List h x as" |
|
95 |
apply(induct as) |
|
96 |
apply simp |
|
97 |
apply(clarsimp simp add:fun_upd_apply) |
|
98 |
done |
|
99 |
||
100 |
||
101 |
declare fun_upd_apply[simp del]fun_upd_same[simp] fun_upd_other[simp] |
|
102 |
||
103 |
lemma List_unique: "\<And>x bs. List h x as \<Longrightarrow> List h x bs \<Longrightarrow> as = bs" |
|
104 |
by(induct as, simp, clarsimp) |
|
105 |
||
106 |
lemma List_unique1: "List h p as \<Longrightarrow> \<exists>!as. List h p as" |
|
107 |
by(blast intro:List_unique) |
|
108 |
||
109 |
lemma List_app: "\<And>x. List h x (as@bs) = (\<exists>y. Path h x as y \<and> List h y bs)" |
|
110 |
by(induct as, simp, clarsimp) |
|
111 |
||
112 |
lemma List_hd_not_in_tl[simp]: "List h (h a) as \<Longrightarrow> a \<notin> set as" |
|
113 |
apply (clarsimp simp add:in_set_conv_decomp) |
|
114 |
apply(frule List_app[THEN iffD1]) |
|
115 |
apply(fastsimp dest: List_unique) |
|
116 |
done |
|
117 |
||
118 |
lemma List_distinct[simp]: "\<And>x. List h x as \<Longrightarrow> distinct as" |
|
119 |
apply(induct as, simp) |
|
120 |
apply(fastsimp dest:List_hd_not_in_tl) |
|
121 |
done |
|
122 |
||
123 |
subsection "Functional abstraction" |
|
124 |
||
125 |
constdefs |
|
126 |
islist :: "('a::ref \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> bool" |
|
127 |
"islist h p == \<exists>as. List h p as" |
|
128 |
list :: "('a::ref \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a list" |
|
129 |
"list h p == SOME as. List h p as" |
|
130 |
||
131 |
lemma List_conv_islist_list: "List h p as = (islist h p \<and> as = list h p)" |
|
132 |
apply(simp add:islist_def list_def) |
|
133 |
apply(rule iffI) |
|
134 |
apply(rule conjI) |
|
135 |
apply blast |
|
136 |
apply(subst some1_equality) |
|
137 |
apply(erule List_unique1) |
|
138 |
apply assumption |
|
139 |
apply(rule refl) |
|
140 |
apply simp |
|
141 |
apply(rule someI_ex) |
|
142 |
apply fast |
|
143 |
done |
|
144 |
||
145 |
lemma [simp]: "islist h Null" |
|
146 |
by(simp add:islist_def) |
|
147 |
||
148 |
lemma [simp]: "a \<noteq> Null \<Longrightarrow> islist h a = islist h (h a)" |
|
149 |
by(simp add:islist_def) |
|
150 |
||
151 |
lemma [simp]: "list h Null = []" |
|
152 |
by(simp add:list_def) |
|
153 |
||
154 |
lemma list_Ref_conv[simp]: |
|
155 |
"\<lbrakk> a \<noteq> Null; islist h (h a) \<rbrakk> \<Longrightarrow> list h a = a # list h (h a)" |
|
156 |
apply(insert List_Ref[of _ h]) |
|
157 |
apply(fastsimp simp:List_conv_islist_list) |
|
158 |
done |
|
159 |
||
160 |
lemma [simp]: "islist h (h a) \<Longrightarrow> a \<notin> set(list h (h a))" |
|
161 |
apply(insert List_hd_not_in_tl[of h]) |
|
162 |
apply(simp add:List_conv_islist_list) |
|
163 |
done |
|
164 |
||
165 |
lemma list_upd_conv[simp]: |
|
166 |
"islist h p \<Longrightarrow> y \<notin> set(list h p) \<Longrightarrow> list (h(y := q)) p = list h p" |
|
167 |
apply(drule notin_List_update[of _ _ h q p]) |
|
168 |
apply(simp add:List_conv_islist_list) |
|
169 |
done |
|
170 |
||
171 |
lemma islist_upd[simp]: |
|
172 |
"islist h p \<Longrightarrow> y \<notin> set(list h p) \<Longrightarrow> islist (h(y := q)) p" |
|
173 |
apply(frule notin_List_update[of _ _ h q p]) |
|
174 |
apply(simp add:List_conv_islist_list) |
|
175 |
done |
|
176 |
||
177 |
||
178 |
section "Verifications" |
|
179 |
||
180 |
subsection "List reversal" |
|
181 |
||
182 |
text "A short but unreadable proof:" |
|
183 |
||
184 |
lemma "VARS tl p q r |
|
185 |
{List tl p Ps \<and> List tl q Qs \<and> set Ps \<inter> set Qs = {}} |
|
186 |
WHILE p \<noteq> Null |
|
187 |
INV {\<exists>ps qs. List tl p ps \<and> List tl q qs \<and> set ps \<inter> set qs = {} \<and> |
|
188 |
rev ps @ qs = rev Ps @ Qs} |
|
189 |
DO r := p; p := p^.tl; r^.tl := q; q := r OD |
|
190 |
{List tl q (rev Ps @ Qs)}" |
|
191 |
apply vcg_simp |
|
192 |
apply fastsimp |
|
193 |
apply(fastsimp intro:notin_List_update[THEN iffD2]) |
|
194 |
(* explicily: |
|
195 |
apply clarify |
|
196 |
apply(rename_tac ps qs) |
|
197 |
apply clarsimp |
|
198 |
apply(rename_tac ps') |
|
199 |
apply(rule_tac x = ps' in exI) |
|
200 |
apply simp |
|
201 |
apply(rule_tac x = "p#qs" in exI) |
|
202 |
apply simp |
|
203 |
*) |
|
204 |
apply fastsimp |
|
205 |
done |
|
206 |
||
207 |
||
208 |
text "A longer readable version:" |
|
209 |
||
210 |
lemma "VARS tl p q r |
|
211 |
{List tl p Ps \<and> List tl q Qs \<and> set Ps \<inter> set Qs = {}} |
|
212 |
WHILE p \<noteq> Null |
|
213 |
INV {\<exists>ps qs. List tl p ps \<and> List tl q qs \<and> set ps \<inter> set qs = {} \<and> |
|
214 |
rev ps @ qs = rev Ps @ Qs} |
|
215 |
DO r := p; p := p^.tl; r^.tl := q; q := r OD |
|
216 |
{List tl q (rev Ps @ Qs)}" |
|
217 |
proof vcg |
|
218 |
fix tl p q r |
|
219 |
assume "List tl p Ps \<and> List tl q Qs \<and> set Ps \<inter> set Qs = {}" |
|
220 |
thus "\<exists>ps qs. List tl p ps \<and> List tl q qs \<and> set ps \<inter> set qs = {} \<and> |
|
221 |
rev ps @ qs = rev Ps @ Qs" by fastsimp |
|
222 |
next |
|
223 |
fix tl p q r |
|
224 |
assume "(\<exists>ps qs. List tl p ps \<and> List tl q qs \<and> set ps \<inter> set qs = {} \<and> |
|
225 |
rev ps @ qs = rev Ps @ Qs) \<and> p \<noteq> Null" |
|
226 |
(is "(\<exists>ps qs. ?I ps qs) \<and> _") |
|
227 |
then obtain ps qs where I: "?I ps qs \<and> p \<noteq> Null" by fast |
|
228 |
then obtain ps' where "ps = p # ps'" by fastsimp |
|
229 |
hence "List (tl(p := q)) (p^.tl) ps' \<and> |
|
230 |
List (tl(p := q)) p (p#qs) \<and> |
|
231 |
set ps' \<inter> set (p#qs) = {} \<and> |
|
232 |
rev ps' @ (p#qs) = rev Ps @ Qs" |
|
233 |
using I by fastsimp |
|
234 |
thus "\<exists>ps' qs'. List (tl(p := q)) (p^.tl) ps' \<and> |
|
235 |
List (tl(p := q)) p qs' \<and> |
|
236 |
set ps' \<inter> set qs' = {} \<and> |
|
237 |
rev ps' @ qs' = rev Ps @ Qs" by fast |
|
238 |
next |
|
239 |
fix tl p q r |
|
240 |
assume "(\<exists>ps qs. List tl p ps \<and> List tl q qs \<and> set ps \<inter> set qs = {} \<and> |
|
241 |
rev ps @ qs = rev Ps @ Qs) \<and> \<not> p \<noteq> Null" |
|
242 |
thus "List tl q (rev Ps @ Qs)" by fastsimp |
|
243 |
qed |
|
244 |
||
245 |
||
246 |
text{* Finaly, the functional version. A bit more verbose, but automatic! *} |
|
247 |
||
248 |
lemma "VARS tl p q r |
|
249 |
{islist tl p \<and> islist tl q \<and> |
|
250 |
Ps = list tl p \<and> Qs = list tl q \<and> set Ps \<inter> set Qs = {}} |
|
251 |
WHILE p \<noteq> Null |
|
252 |
INV {islist tl p \<and> islist tl q \<and> |
|
253 |
set(list tl p) \<inter> set(list tl q) = {} \<and> |
|
254 |
rev(list tl p) @ (list tl q) = rev Ps @ Qs} |
|
255 |
DO r := p; p := p^.tl; r^.tl := q; q := r OD |
|
256 |
{islist tl q \<and> list tl q = rev Ps @ Qs}" |
|
257 |
apply vcg_simp |
|
258 |
apply clarsimp |
|
259 |
apply clarsimp |
|
260 |
apply clarsimp |
|
261 |
done |
|
262 |
||
263 |
||
264 |
subsection "Searching in a list" |
|
265 |
||
266 |
text{*What follows is a sequence of successively more intelligent proofs that |
|
267 |
a simple loop finds an element in a linked list. |
|
268 |
||
269 |
We start with a proof based on the @{term List} predicate. This means it only |
|
270 |
works for acyclic lists. *} |
|
271 |
||
272 |
lemma "VARS tl p |
|
273 |
{List tl p Ps \<and> X \<in> set Ps} |
|
274 |
WHILE p \<noteq> Null \<and> p \<noteq> X |
|
275 |
INV {p \<noteq> Null \<and> (\<exists>ps. List tl p ps \<and> X \<in> set ps)} |
|
276 |
DO p := p^.tl OD |
|
277 |
{p = X}" |
|
278 |
apply vcg_simp |
|
279 |
apply(case_tac "p = Null") |
|
280 |
apply clarsimp |
|
281 |
apply fastsimp |
|
282 |
apply clarsimp |
|
283 |
apply fastsimp |
|
284 |
apply clarsimp |
|
285 |
done |
|
286 |
||
287 |
text{*Using @{term Path} instead of @{term List} generalizes the correctness |
|
288 |
statement to cyclic lists as well: *} |
|
289 |
||
290 |
lemma "VARS tl p |
|
291 |
{Path tl p Ps X} |
|
292 |
WHILE p \<noteq> Null \<and> p \<noteq> X |
|
293 |
INV {\<exists>ps. Path tl p ps X} |
|
294 |
DO p := p^.tl OD |
|
295 |
{p = X}" |
|
296 |
apply vcg_simp |
|
297 |
apply blast |
|
298 |
apply fastsimp |
|
299 |
apply clarsimp |
|
300 |
done |
|
301 |
||
302 |
text{*Now it dawns on us that we do not need the list witness at all --- it |
|
303 |
suffices to talk about reachability, i.e.\ we can use relations directly. *} |
|
304 |
||
305 |
lemma "VARS tl p |
|
306 |
{(p,X) \<in> {(x,y). y = tl x & x \<noteq> Null}^*} |
|
307 |
WHILE p \<noteq> Null \<and> p \<noteq> X |
|
308 |
INV {(p,X) \<in> {(x,y). y = tl x & x \<noteq> Null}^*} |
|
309 |
DO p := p^.tl OD |
|
310 |
{p = X}" |
|
311 |
apply vcg_simp |
|
312 |
apply clarsimp |
|
313 |
apply(erule converse_rtranclE) |
|
314 |
apply simp |
|
315 |
apply(simp) |
|
316 |
apply(fastsimp elim:converse_rtranclE) |
|
317 |
done |
|
318 |
||
319 |
||
320 |
subsection "Merging two lists" |
|
321 |
||
322 |
text"This is still a bit rough, especially the proof." |
|
323 |
||
324 |
consts merge :: "'a list * 'a list * ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list" |
|
325 |
||
326 |
recdef merge "measure(%(xs,ys,f). size xs + size ys)" |
|
327 |
"merge(x#xs,y#ys,f) = (if f x y then x # merge(xs,y#ys,f) |
|
328 |
else y # merge(x#xs,ys,f))" |
|
329 |
"merge(x#xs,[],f) = x # merge(xs,[],f)" |
|
330 |
"merge([],y#ys,f) = y # merge([],ys,f)" |
|
331 |
"merge([],[],f) = []" |
|
332 |
||
333 |
lemma imp_disjCL: "(P|Q \<longrightarrow> R) = ((P \<longrightarrow> R) \<and> (~P \<longrightarrow> Q \<longrightarrow> R))" |
|
334 |
by blast |
|
335 |
||
336 |
declare disj_not1[simp del] imp_disjL[simp del] imp_disjCL[simp] |
|
337 |
||
338 |
lemma "VARS hd tl p q r s |
|
339 |
{List tl p Ps \<and> List tl q Qs \<and> set Ps \<inter> set Qs = {} \<and> |
|
340 |
(p \<noteq> Null \<or> q \<noteq> Null)} |
|
341 |
IF if q = Null then True else p ~= Null & p^.hd \<le> q^.hd |
|
342 |
THEN r := p; p := p^.tl ELSE r := q; q := q^.tl FI; |
|
343 |
s := r; |
|
344 |
WHILE p \<noteq> Null \<or> q \<noteq> Null |
|
345 |
INV {EX rs ps qs. Path tl r rs s \<and> List tl p ps \<and> List tl q qs \<and> |
|
346 |
distinct(s # ps @ qs @ rs) \<and> s \<noteq> Null \<and> |
|
347 |
merge(Ps,Qs,\<lambda>x y. hd x \<le> hd y) = |
|
348 |
rs @ s # merge(ps,qs,\<lambda>x y. hd x \<le> hd y) \<and> |
|
349 |
(tl s = p \<or> tl s = q)} |
|
350 |
DO IF if q = Null then True else p \<noteq> Null \<and> p^.hd \<le> q^.hd |
|
351 |
THEN s^.tl := p; p := p^.tl ELSE s^.tl := q; q := q^.tl FI; |
|
352 |
s := s^.tl |
|
353 |
OD |
|
354 |
{List tl r (merge(Ps,Qs,\<lambda>x y. hd x \<le> hd y))}" |
|
355 |
apply vcg_simp |
|
356 |
||
357 |
apply (fastsimp) |
|
358 |
||
359 |
apply clarsimp |
|
360 |
apply(rule conjI) |
|
361 |
apply clarsimp |
|
362 |
apply(simp add:eq_sym_conv) |
|
363 |
apply(rule_tac x = "rs @ [s]" in exI) |
|
364 |
apply simp |
|
365 |
apply(rule_tac x = "bs" in exI) |
|
366 |
apply (fastsimp simp:eq_sym_conv) |
|
367 |
||
368 |
apply clarsimp |
|
369 |
apply(rule conjI) |
|
370 |
apply clarsimp |
|
371 |
apply(rule_tac x = "rs @ [s]" in exI) |
|
372 |
apply simp |
|
373 |
apply(rule_tac x = "bsa" in exI) |
|
374 |
apply(rule conjI) |
|
375 |
apply (simp add:eq_sym_conv) |
|
376 |
apply(rule exI) |
|
377 |
apply(rule conjI) |
|
378 |
apply(rule_tac x = bs in exI) |
|
379 |
apply(rule conjI) |
|
380 |
apply(rule refl) |
|
381 |
apply (simp add:eq_sym_conv) |
|
382 |
apply (simp add:eq_sym_conv) |
|
383 |
||
384 |
apply(rule conjI) |
|
385 |
apply clarsimp |
|
386 |
apply(rule_tac x = "rs @ [s]" in exI) |
|
387 |
apply simp |
|
388 |
apply(rule_tac x = bs in exI) |
|
389 |
apply (simp add:eq_sym_conv) |
|
390 |
apply clarsimp |
|
391 |
apply(rule_tac x = "rs @ [s]" in exI) |
|
392 |
apply (simp add:eq_sym_conv) |
|
393 |
apply(rule exI) |
|
394 |
apply(rule conjI) |
|
395 |
apply(rule_tac x = bsa in exI) |
|
396 |
apply(rule conjI) |
|
397 |
apply(rule refl) |
|
398 |
apply (simp add:eq_sym_conv) |
|
399 |
apply(rule_tac x = bs in exI) |
|
400 |
apply (simp add:eq_sym_conv) |
|
401 |
||
402 |
apply(clarsimp simp add:List_app) |
|
403 |
done |
|
404 |
||
405 |
(* TODO: merging with islist/list instead of List: an improvement? |
|
406 |
needs (is)path, which is not so easy to prove either. *) |
|
407 |
||
408 |
subsection "Storage allocation" |
|
409 |
||
410 |
constdefs new :: "'a set \<Rightarrow> 'a::ref" |
|
411 |
"new A == SOME a. a \<notin> A & a \<noteq> Null" |
|
412 |
||
413 |
||
414 |
lemma new_notin: |
|
415 |
"\<lbrakk> ~finite(UNIV::('a::ref)set); finite(A::'a set); B \<subseteq> A \<rbrakk> \<Longrightarrow> |
|
416 |
new (A) \<notin> B & new A \<noteq> Null" |
|
417 |
apply(unfold new_def) |
|
418 |
apply(rule someI2_ex) |
|
419 |
apply (fast dest:ex_new_if_finite[of "insert Null A"]) |
|
420 |
apply (fast) |
|
421 |
done |
|
422 |
||
423 |
lemma "~finite(UNIV::('a::ref)set) \<Longrightarrow> |
|
424 |
VARS xs elem next alloc p q |
|
425 |
{Xs = xs \<and> p = (Null::'a)} |
|
426 |
WHILE xs \<noteq> [] |
|
427 |
INV {islist next p \<and> set(list next p) \<subseteq> set alloc \<and> |
|
428 |
map elem (rev(list next p)) @ xs = Xs} |
|
429 |
DO q := new(set alloc); alloc := q#alloc; |
|
430 |
q^.next := p; q^.elem := hd xs; xs := tl xs; p := q |
|
431 |
OD |
|
432 |
{islist next p \<and> map elem (rev(list next p)) = Xs}" |
|
433 |
apply vcg_simp |
|
434 |
apply (clarsimp simp: subset_insert_iff neq_Nil_conv fun_upd_apply new_notin) |
|
435 |
apply fastsimp |
|
436 |
done |
|
437 |
||
438 |
||
439 |
end |