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