src/HOL/Library/Heap.thy
author haftmann
Mon Jul 07 08:47:17 2008 +0200 (2008-07-07)
changeset 27487 c8a6ce181805
parent 27368 9f90ac19e32b
child 28042 1471f2974eb1
permissions -rw-r--r--
absolute imports of HOL/*.thy theories
haftmann@26170
     1
(*  Title:      HOL/Library/Heap.thy
haftmann@26170
     2
    ID:         $Id$
haftmann@26170
     3
    Author:     John Matthews, Galois Connections; Alexander Krauss, TU Muenchen
haftmann@26170
     4
*)
haftmann@26170
     5
haftmann@26170
     6
header {* A polymorphic heap based on cantor encodings *}
haftmann@26170
     7
haftmann@26170
     8
theory Heap
haftmann@27487
     9
imports Plain "~~/src/HOL/List" Countable RType
haftmann@26170
    10
begin
haftmann@26170
    11
haftmann@26170
    12
subsection {* Representable types *}
haftmann@26170
    13
haftmann@26170
    14
text {* The type class of representable types *}
haftmann@26170
    15
haftmann@26170
    16
class heap = rtype + countable
haftmann@26170
    17
haftmann@26170
    18
text {* Instances for common HOL types *}
haftmann@26170
    19
haftmann@26170
    20
instance nat :: heap ..
haftmann@26170
    21
haftmann@26170
    22
instance "*" :: (heap, heap) heap ..
haftmann@26170
    23
haftmann@26170
    24
instance "+" :: (heap, heap) heap ..
haftmann@26170
    25
haftmann@26170
    26
instance list :: (heap) heap ..
haftmann@26170
    27
haftmann@26170
    28
instance option :: (heap) heap ..
haftmann@26170
    29
haftmann@26170
    30
instance int :: heap ..
haftmann@26170
    31
haftmann@26170
    32
instance message_string :: countable
haftmann@26170
    33
  by (rule countable_classI [of "message_string_case to_nat"])
haftmann@26170
    34
   (auto split: message_string.splits)
haftmann@26170
    35
haftmann@26170
    36
instance message_string :: heap ..
haftmann@26170
    37
haftmann@26170
    38
text {* Reflected types themselves are heap-representable *}
haftmann@26170
    39
haftmann@26170
    40
instantiation rtype :: countable
haftmann@26170
    41
begin
haftmann@26170
    42
haftmann@26170
    43
lemma list_size_size_append:
haftmann@26170
    44
  "list_size size (xs @ ys) = list_size size xs + list_size size ys"
haftmann@26170
    45
  by (induct xs, auto)
haftmann@26170
    46
haftmann@26170
    47
lemma rtype_size: "t = RType.RType c ts \<Longrightarrow> t' \<in> set ts \<Longrightarrow> size t' < size t"
haftmann@26170
    48
  by (frule split_list) (auto simp add: list_size_size_append)
haftmann@26170
    49
haftmann@26170
    50
function to_nat_rtype :: "rtype \<Rightarrow> nat" where
haftmann@26170
    51
  "to_nat_rtype (RType.RType c ts) = to_nat (to_nat c, to_nat (map to_nat_rtype ts))"
haftmann@26170
    52
by pat_completeness auto
haftmann@26170
    53
haftmann@26170
    54
termination by (relation "measure (\<lambda>x. size x)")
haftmann@26170
    55
  (simp, simp only: in_measure rtype_size)
haftmann@26170
    56
wenzelm@26932
    57
instance
wenzelm@26932
    58
proof (rule countable_classI)
wenzelm@26932
    59
  fix t t' :: rtype and ts
haftmann@26170
    60
  have "(\<forall>t'. to_nat_rtype t = to_nat_rtype t' \<longrightarrow> t = t')
haftmann@26170
    61
    \<and> (\<forall>ts'. map to_nat_rtype ts = map to_nat_rtype ts' \<longrightarrow> ts = ts')"
haftmann@26170
    62
  proof (induct rule: rtype.induct)
haftmann@26170
    63
    case (RType c ts) show ?case
haftmann@26170
    64
    proof (rule allI, rule impI)
haftmann@26170
    65
      fix t'
haftmann@26170
    66
      assume hyp: "to_nat_rtype (rtype.RType c ts) = to_nat_rtype t'"
haftmann@26170
    67
      then obtain c' ts' where t': "t' = (rtype.RType c' ts')"
haftmann@26170
    68
        by (cases t') auto
haftmann@26170
    69
      with RType hyp have "c = c'" and "ts = ts'" by simp_all
haftmann@26170
    70
      with t' show "rtype.RType c ts = t'" by simp
haftmann@26170
    71
    qed
haftmann@26170
    72
  next
haftmann@26170
    73
    case Nil_rtype then show ?case by simp
haftmann@26170
    74
  next
haftmann@26170
    75
    case (Cons_rtype t ts) then show ?case by auto
haftmann@26170
    76
  qed
haftmann@26170
    77
  then have "to_nat_rtype t = to_nat_rtype t' \<Longrightarrow> t = t'" by auto
haftmann@26170
    78
  moreover assume "to_nat_rtype t = to_nat_rtype t'"
haftmann@26170
    79
  ultimately show "t = t'" by simp
haftmann@26170
    80
qed
haftmann@26170
    81
haftmann@26170
    82
end
haftmann@26170
    83
haftmann@26170
    84
instance rtype :: heap ..
haftmann@26170
    85
haftmann@26170
    86
haftmann@26170
    87
subsection {* A polymorphic heap with dynamic arrays and references *}
haftmann@26170
    88
haftmann@26170
    89
types addr = nat -- "untyped heap references"
haftmann@26170
    90
haftmann@26170
    91
datatype 'a array = Array addr
haftmann@26170
    92
datatype 'a ref = Ref addr -- "note the phantom type 'a "
haftmann@26170
    93
haftmann@26170
    94
primrec addr_of_array :: "'a array \<Rightarrow> addr" where
haftmann@26170
    95
  "addr_of_array (Array x) = x"
haftmann@26170
    96
haftmann@26170
    97
primrec addr_of_ref :: "'a ref \<Rightarrow> addr" where
haftmann@26170
    98
  "addr_of_ref (Ref x) = x"
haftmann@26170
    99
haftmann@26170
   100
lemma addr_of_array_inj [simp]:
haftmann@26170
   101
  "addr_of_array a = addr_of_array a' \<longleftrightarrow> a = a'"
haftmann@26170
   102
  by (cases a, cases a') simp_all
haftmann@26170
   103
haftmann@26170
   104
lemma addr_of_ref_inj [simp]:
haftmann@26170
   105
  "addr_of_ref r = addr_of_ref r' \<longleftrightarrow> r = r'"
haftmann@26170
   106
  by (cases r, cases r') simp_all
haftmann@26170
   107
haftmann@26170
   108
instance array :: (type) countable
haftmann@26170
   109
  by (rule countable_classI [of addr_of_array]) simp
haftmann@26170
   110
haftmann@26170
   111
instance ref :: (type) countable
haftmann@26170
   112
  by (rule countable_classI [of addr_of_ref]) simp
haftmann@26170
   113
haftmann@26170
   114
setup {*
haftmann@26170
   115
  Sign.add_const_constraint (@{const_name Array}, SOME @{typ "nat \<Rightarrow> 'a\<Colon>heap array"})
haftmann@26170
   116
  #> Sign.add_const_constraint (@{const_name Ref}, SOME @{typ "nat \<Rightarrow> 'a\<Colon>heap ref"})
haftmann@26170
   117
  #> Sign.add_const_constraint (@{const_name addr_of_array}, SOME @{typ "'a\<Colon>heap array \<Rightarrow> nat"})
haftmann@26170
   118
  #> Sign.add_const_constraint (@{const_name addr_of_ref}, SOME @{typ "'a\<Colon>heap ref \<Rightarrow> nat"})
haftmann@26170
   119
*}
haftmann@26170
   120
haftmann@26170
   121
types heap_rep = nat -- "representable values"
haftmann@26170
   122
haftmann@26170
   123
record heap =
haftmann@26170
   124
  arrays :: "rtype \<Rightarrow> addr \<Rightarrow> heap_rep list"
haftmann@26170
   125
  refs :: "rtype \<Rightarrow> addr \<Rightarrow> heap_rep"
haftmann@26170
   126
  lim  :: addr
haftmann@26170
   127
haftmann@26170
   128
definition empty :: heap where
haftmann@26170
   129
  "empty = \<lparr>arrays = (\<lambda>_. arbitrary), refs = (\<lambda>_. arbitrary), lim = 0\<rparr>" -- "why arbitrary?"
haftmann@26170
   130
haftmann@26170
   131
haftmann@26170
   132
subsection {* Imperative references and arrays *}
haftmann@26170
   133
haftmann@26170
   134
text {*
haftmann@26170
   135
  References and arrays are developed in parallel,
huffman@26586
   136
  but keeping them separate makes some later proofs simpler.
haftmann@26170
   137
*}
haftmann@26170
   138
haftmann@26170
   139
subsubsection {* Primitive operations *}
haftmann@26170
   140
haftmann@26170
   141
definition
haftmann@26170
   142
  new_ref :: "heap \<Rightarrow> ('a\<Colon>heap) ref \<times> heap" where
haftmann@26170
   143
  "new_ref h = (let l = lim h in (Ref l, h\<lparr>lim := l + 1\<rparr>))"
haftmann@26170
   144
haftmann@26170
   145
definition
haftmann@26170
   146
  new_array :: "heap \<Rightarrow> ('a\<Colon>heap) array \<times> heap" where
haftmann@26170
   147
  "new_array h = (let l = lim h in (Array l, h\<lparr>lim := l + 1\<rparr>))"
haftmann@26170
   148
haftmann@26170
   149
definition
haftmann@26170
   150
  ref_present :: "'a\<Colon>heap ref \<Rightarrow> heap \<Rightarrow> bool" where
haftmann@26170
   151
  "ref_present r h \<longleftrightarrow> addr_of_ref r < lim h"
haftmann@26170
   152
haftmann@26170
   153
definition 
haftmann@26170
   154
  array_present :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> bool" where
haftmann@26170
   155
  "array_present a h \<longleftrightarrow> addr_of_array a < lim h"
haftmann@26170
   156
haftmann@26170
   157
definition
haftmann@26170
   158
  get_ref :: "'a\<Colon>heap ref \<Rightarrow> heap \<Rightarrow> 'a" where
haftmann@26170
   159
  "get_ref r h = from_nat (refs h (RTYPE('a)) (addr_of_ref r))"
haftmann@26170
   160
haftmann@26170
   161
definition
haftmann@26170
   162
  get_array :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> 'a list" where
haftmann@26170
   163
  "get_array a h = map from_nat (arrays h (RTYPE('a)) (addr_of_array a))"
haftmann@26170
   164
haftmann@26170
   165
definition
haftmann@26170
   166
  set_ref :: "'a\<Colon>heap ref \<Rightarrow> 'a \<Rightarrow> heap \<Rightarrow> heap" where
haftmann@26170
   167
  "set_ref r x = 
haftmann@26170
   168
  refs_update (\<lambda>h. h( RTYPE('a) := ((h (RTYPE('a))) (addr_of_ref r:=to_nat x))))"
haftmann@26170
   169
haftmann@26170
   170
definition
haftmann@26170
   171
  set_array :: "'a\<Colon>heap array \<Rightarrow> 'a list \<Rightarrow> heap \<Rightarrow> heap" where
haftmann@26170
   172
  "set_array a x = 
haftmann@26170
   173
  arrays_update (\<lambda>h. h( RTYPE('a) := ((h (RTYPE('a))) (addr_of_array a:=map to_nat x))))"
haftmann@26170
   174
haftmann@26170
   175
subsubsection {* Interface operations *}
haftmann@26170
   176
haftmann@26170
   177
definition
haftmann@26170
   178
  ref :: "'a \<Rightarrow> heap \<Rightarrow> 'a\<Colon>heap ref \<times> heap" where
haftmann@26170
   179
  "ref x h = (let (r, h') = new_ref h;
haftmann@26170
   180
                   h''    = set_ref r x h'
haftmann@26170
   181
         in (r, h''))"
haftmann@26170
   182
haftmann@26170
   183
definition
haftmann@26170
   184
  array :: "nat \<Rightarrow> 'a \<Rightarrow> heap \<Rightarrow> 'a\<Colon>heap array \<times> heap" where
haftmann@26170
   185
  "array n x h = (let (r, h') = new_array h;
haftmann@26170
   186
                       h'' = set_array r (replicate n x) h'
haftmann@26170
   187
        in (r, h''))"
haftmann@26170
   188
haftmann@26170
   189
definition
haftmann@26170
   190
  array_of_list :: "'a list \<Rightarrow> heap \<Rightarrow> 'a\<Colon>heap array \<times> heap" where
haftmann@26170
   191
  "array_of_list xs h = (let (r, h') = new_array h;
haftmann@26170
   192
           h'' = set_array r xs h'
haftmann@26170
   193
        in (r, h''))"  
haftmann@26170
   194
haftmann@26170
   195
definition
haftmann@26170
   196
  upd :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> heap \<Rightarrow> heap" where
haftmann@26170
   197
  "upd a i x h = set_array a ((get_array a h)[i:=x]) h"
haftmann@26170
   198
haftmann@26170
   199
definition
haftmann@26170
   200
  length :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> nat" where
haftmann@26170
   201
  "length a h = size (get_array a h)"
haftmann@26170
   202
haftmann@26170
   203
definition
haftmann@26170
   204
  array_ran :: "('a\<Colon>heap) option array \<Rightarrow> heap \<Rightarrow> 'a set" where
haftmann@26170
   205
  "array_ran a h = {e. Some e \<in> set (get_array a h)}"
haftmann@26170
   206
    -- {*FIXME*}
haftmann@26170
   207
haftmann@26170
   208
haftmann@26170
   209
subsubsection {* Reference equality *}
haftmann@26170
   210
haftmann@26170
   211
text {* 
haftmann@26170
   212
  The following relations are useful for comparing arrays and references.
haftmann@26170
   213
*}
haftmann@26170
   214
haftmann@26170
   215
definition
haftmann@26170
   216
  noteq_refs :: "('a\<Colon>heap) ref \<Rightarrow> ('b\<Colon>heap) ref \<Rightarrow> bool" (infix "=!=" 70)
haftmann@26170
   217
where
haftmann@26170
   218
  "r =!= s \<longleftrightarrow> RTYPE('a) \<noteq> RTYPE('b) \<or> addr_of_ref r \<noteq> addr_of_ref s"
haftmann@26170
   219
haftmann@26170
   220
definition
haftmann@26170
   221
  noteq_arrs :: "('a\<Colon>heap) array \<Rightarrow> ('b\<Colon>heap) array \<Rightarrow> bool" (infix "=!!=" 70)
haftmann@26170
   222
where
haftmann@26170
   223
  "r =!!= s \<longleftrightarrow> RTYPE('a) \<noteq> RTYPE('b) \<or> addr_of_array r \<noteq> addr_of_array s"
haftmann@26170
   224
haftmann@26170
   225
lemma noteq_refs_sym: "r =!= s \<Longrightarrow> s =!= r"
haftmann@26170
   226
  and noteq_arrs_sym: "a =!!= b \<Longrightarrow> b =!!= a"
haftmann@26170
   227
  and unequal_refs [simp]: "r \<noteq> r' \<longleftrightarrow> r =!= r'" -- "same types!"
haftmann@26170
   228
  and unequal_arrs [simp]: "a \<noteq> a' \<longleftrightarrow> a =!!= a'"
haftmann@26170
   229
unfolding noteq_refs_def noteq_arrs_def by auto
haftmann@26170
   230
haftmann@26170
   231
lemma present_new_ref: "ref_present r h \<Longrightarrow> r =!= fst (ref v h)"
haftmann@26170
   232
  by (simp add: ref_present_def new_ref_def ref_def Let_def noteq_refs_def)
haftmann@26170
   233
haftmann@26170
   234
lemma present_new_arr: "array_present a h \<Longrightarrow> a =!!= fst (array v x h)"
haftmann@26170
   235
  by (simp add: array_present_def noteq_arrs_def new_array_def array_def Let_def)
haftmann@26170
   236
haftmann@26170
   237
haftmann@26170
   238
subsubsection {* Properties of heap containers *}
haftmann@26170
   239
haftmann@26170
   240
text {* Properties of imperative arrays *}
haftmann@26170
   241
haftmann@26170
   242
text {* FIXME: Does there exist a "canonical" array axiomatisation in
haftmann@26170
   243
the literature?  *}
haftmann@26170
   244
haftmann@26170
   245
lemma array_get_set_eq [simp]: "get_array r (set_array r x h) = x"
haftmann@26170
   246
  by (simp add: get_array_def set_array_def)
haftmann@26170
   247
haftmann@26170
   248
lemma array_get_set_neq [simp]: "r =!!= s \<Longrightarrow> get_array r (set_array s x h) = get_array r h"
haftmann@26170
   249
  by (simp add: noteq_arrs_def get_array_def set_array_def)
haftmann@26170
   250
haftmann@26170
   251
lemma set_array_same [simp]:
haftmann@26170
   252
  "set_array r x (set_array r y h) = set_array r x h"
haftmann@26170
   253
  by (simp add: set_array_def)
haftmann@26170
   254
haftmann@26170
   255
lemma array_set_set_swap:
haftmann@26170
   256
  "r =!!= r' \<Longrightarrow> set_array r x (set_array r' x' h) = set_array r' x' (set_array r x h)"
haftmann@26170
   257
  by (simp add: Let_def expand_fun_eq noteq_arrs_def set_array_def)
haftmann@26170
   258
haftmann@26170
   259
lemma array_ref_set_set_swap:
haftmann@26170
   260
  "set_array r x (set_ref r' x' h) = set_ref r' x' (set_array r x h)"
haftmann@26170
   261
  by (simp add: Let_def expand_fun_eq set_array_def set_ref_def)
haftmann@26170
   262
haftmann@26170
   263
lemma get_array_upd_eq [simp]:
haftmann@26170
   264
  "get_array a (upd a i v h) = (get_array a h) [i := v]"
haftmann@26170
   265
  by (simp add: upd_def)
haftmann@26170
   266
haftmann@26170
   267
lemma nth_upd_array_neq_array [simp]:
haftmann@26170
   268
  "a =!!= b \<Longrightarrow> get_array a (upd b j v h) ! i = get_array a h ! i"
haftmann@26170
   269
  by (simp add: upd_def noteq_arrs_def)
haftmann@26170
   270
haftmann@26170
   271
lemma get_arry_array_upd_elem_neqIndex [simp]:
haftmann@26170
   272
  "i \<noteq> j \<Longrightarrow> get_array a (upd a j v h) ! i = get_array a h ! i"
haftmann@26170
   273
  by simp
haftmann@26170
   274
haftmann@26170
   275
lemma length_upd_eq [simp]: 
haftmann@26170
   276
  "length a (upd a i v h) = length a h" 
haftmann@26170
   277
  by (simp add: length_def upd_def)
haftmann@26170
   278
haftmann@26170
   279
lemma length_upd_neq [simp]: 
haftmann@26170
   280
  "length a (upd b i v h) = length a h"
haftmann@26170
   281
  by (simp add: upd_def length_def set_array_def get_array_def)
haftmann@26170
   282
haftmann@26170
   283
lemma upd_swap_neqArray:
haftmann@26170
   284
  "a =!!= a' \<Longrightarrow> 
haftmann@26170
   285
  upd a i v (upd a' i' v' h) 
haftmann@26170
   286
  = upd a' i' v' (upd a i v h)"
haftmann@26170
   287
apply (unfold upd_def)
haftmann@26170
   288
apply simp
haftmann@26170
   289
apply (subst array_set_set_swap, assumption)
haftmann@26170
   290
apply (subst array_get_set_neq)
haftmann@26170
   291
apply (erule noteq_arrs_sym)
haftmann@26170
   292
apply (simp)
haftmann@26170
   293
done
haftmann@26170
   294
haftmann@26170
   295
lemma upd_swap_neqIndex:
haftmann@26170
   296
  "\<lbrakk> i \<noteq> i' \<rbrakk> \<Longrightarrow> upd a i v (upd a i' v' h) = upd a i' v' (upd a i v h)"
haftmann@26170
   297
by (auto simp add: upd_def array_set_set_swap list_update_swap)
haftmann@26170
   298
haftmann@26170
   299
lemma get_array_init_array_list:
haftmann@26170
   300
  "get_array (fst (array_of_list ls h)) (snd (array_of_list ls' h)) = ls'"
haftmann@26170
   301
  by (simp add: Let_def split_def array_of_list_def)
haftmann@26170
   302
haftmann@26170
   303
lemma set_array:
haftmann@26170
   304
  "set_array (fst (array_of_list ls h))
haftmann@26170
   305
     new_ls (snd (array_of_list ls h))
haftmann@26170
   306
       = snd (array_of_list new_ls h)"
haftmann@26170
   307
  by (simp add: Let_def split_def array_of_list_def)
haftmann@26170
   308
haftmann@26170
   309
lemma array_present_upd [simp]: 
haftmann@26170
   310
  "array_present a (upd b i v h) = array_present a h"
haftmann@26170
   311
  by (simp add: upd_def array_present_def set_array_def get_array_def)
haftmann@26170
   312
haftmann@26170
   313
lemma array_of_list_replicate:
haftmann@26170
   314
  "array_of_list (replicate n x) = array n x"
haftmann@26170
   315
  by (simp add: expand_fun_eq array_of_list_def array_def)
haftmann@26170
   316
haftmann@26170
   317
haftmann@26170
   318
text {* Properties of imperative references *}
haftmann@26170
   319
haftmann@26170
   320
lemma next_ref_fresh [simp]:
haftmann@26170
   321
  assumes "(r, h') = new_ref h"
haftmann@26170
   322
  shows "\<not> ref_present r h"
haftmann@26170
   323
  using assms by (cases h) (auto simp add: new_ref_def ref_present_def Let_def)
haftmann@26170
   324
haftmann@26170
   325
lemma next_ref_present [simp]:
haftmann@26170
   326
  assumes "(r, h') = new_ref h"
haftmann@26170
   327
  shows "ref_present r h'"
haftmann@26170
   328
  using assms by (cases h) (auto simp add: new_ref_def ref_present_def Let_def)
haftmann@26170
   329
haftmann@26170
   330
lemma ref_get_set_eq [simp]: "get_ref r (set_ref r x h) = x"
haftmann@26170
   331
  by (simp add: get_ref_def set_ref_def)
haftmann@26170
   332
haftmann@26170
   333
lemma ref_get_set_neq [simp]: "r =!= s \<Longrightarrow> get_ref r (set_ref s x h) = get_ref r h"
haftmann@26170
   334
  by (simp add: noteq_refs_def get_ref_def set_ref_def)
haftmann@26170
   335
haftmann@26170
   336
(* FIXME: We need some infrastructure to infer that locally generated
haftmann@26170
   337
  new refs (by new_ref(_no_init), new_array(')) are distinct
haftmann@26170
   338
  from all existing refs.
haftmann@26170
   339
*)
haftmann@26170
   340
haftmann@26170
   341
lemma ref_set_get: "set_ref r (get_ref r h) h = h"
haftmann@26170
   342
apply (simp add: set_ref_def get_ref_def)
haftmann@26170
   343
oops
haftmann@26170
   344
haftmann@26170
   345
lemma set_ref_same[simp]:
haftmann@26170
   346
  "set_ref r x (set_ref r y h) = set_ref r x h"
haftmann@26170
   347
  by (simp add: set_ref_def)
haftmann@26170
   348
haftmann@26170
   349
lemma ref_set_set_swap:
haftmann@26170
   350
  "r =!= r' \<Longrightarrow> set_ref r x (set_ref r' x' h) = set_ref r' x' (set_ref r x h)"
haftmann@26170
   351
  by (simp add: Let_def expand_fun_eq noteq_refs_def set_ref_def)
haftmann@26170
   352
haftmann@26170
   353
lemma ref_new_set: "fst (ref v (set_ref r v' h)) = fst (ref v h)"
haftmann@26170
   354
  by (simp add: ref_def new_ref_def set_ref_def Let_def)
haftmann@26170
   355
haftmann@26170
   356
lemma ref_get_new [simp]:
haftmann@26170
   357
  "get_ref (fst (ref v h)) (snd (ref v' h)) = v'"
haftmann@26170
   358
  by (simp add: ref_def Let_def split_def)
haftmann@26170
   359
haftmann@26170
   360
lemma ref_set_new [simp]:
haftmann@26170
   361
  "set_ref (fst (ref v h)) new_v (snd (ref v h)) = snd (ref new_v h)"
haftmann@26170
   362
  by (simp add: ref_def Let_def split_def)
haftmann@26170
   363
haftmann@26170
   364
lemma ref_get_new_neq: "r =!= (fst (ref v h)) \<Longrightarrow> 
haftmann@26170
   365
  get_ref r (snd (ref v h)) = get_ref r h"
haftmann@26170
   366
  by (simp add: get_ref_def set_ref_def ref_def Let_def new_ref_def noteq_refs_def)
haftmann@26170
   367
haftmann@26170
   368
lemma lim_set_ref [simp]:
haftmann@26170
   369
  "lim (set_ref r v h) = lim h"
haftmann@26170
   370
  by (simp add: set_ref_def)
haftmann@26170
   371
haftmann@26170
   372
lemma ref_present_new_ref [simp]: 
haftmann@26170
   373
  "ref_present r h \<Longrightarrow> ref_present r (snd (ref v h))"
haftmann@26170
   374
  by (simp add: new_ref_def ref_present_def ref_def Let_def)
haftmann@26170
   375
haftmann@26170
   376
lemma ref_present_set_ref [simp]:
haftmann@26170
   377
  "ref_present r (set_ref r' v h) = ref_present r h"
haftmann@26170
   378
  by (simp add: set_ref_def ref_present_def)
haftmann@26170
   379
haftmann@26170
   380
lemma array_ranI: "\<lbrakk> Some b = get_array a h ! i; i < Heap.length a h \<rbrakk> \<Longrightarrow> b \<in> array_ran a h"
haftmann@26170
   381
unfolding array_ran_def Heap.length_def by simp
haftmann@26170
   382
haftmann@26170
   383
lemma array_ran_upd_array_Some:
haftmann@26170
   384
  assumes "cl \<in> array_ran a (Heap.upd a i (Some b) h)"
haftmann@26170
   385
  shows "cl \<in> array_ran a h \<or> cl = b"
haftmann@26170
   386
proof -
haftmann@26170
   387
  have "set (get_array a h[i := Some b]) \<subseteq> insert (Some b) (set (get_array a h))" by (rule set_update_subset_insert)
haftmann@26170
   388
  with assms show ?thesis 
haftmann@26170
   389
    unfolding array_ran_def Heap.upd_def by fastsimp
haftmann@26170
   390
qed
haftmann@26170
   391
haftmann@26170
   392
lemma array_ran_upd_array_None:
haftmann@26170
   393
  assumes "cl \<in> array_ran a (Heap.upd a i None h)"
haftmann@26170
   394
  shows "cl \<in> array_ran a h"
haftmann@26170
   395
proof -
haftmann@26170
   396
  have "set (get_array a h[i := None]) \<subseteq> insert None (set (get_array a h))" by (rule set_update_subset_insert)
haftmann@26170
   397
  with assms show ?thesis
haftmann@26170
   398
    unfolding array_ran_def Heap.upd_def by auto
haftmann@26170
   399
qed
haftmann@26170
   400
haftmann@26170
   401
haftmann@26170
   402
text {* Non-interaction between imperative array and imperative references *}
haftmann@26170
   403
haftmann@26170
   404
lemma get_array_set_ref [simp]: "get_array a (set_ref r v h) = get_array a h"
haftmann@26170
   405
  by (simp add: get_array_def set_ref_def)
haftmann@26170
   406
haftmann@26170
   407
lemma nth_set_ref [simp]: "get_array a (set_ref r v h) ! i = get_array a h ! i"
haftmann@26170
   408
  by simp
haftmann@26170
   409
haftmann@26170
   410
lemma get_ref_upd [simp]: "get_ref r (upd a i v h) = get_ref r h"
haftmann@26170
   411
  by (simp add: get_ref_def set_array_def upd_def)
haftmann@26170
   412
haftmann@26170
   413
lemma new_ref_upd: "fst (ref v (upd a i v' h)) = fst (ref v h)"
haftmann@26170
   414
  by (simp add: set_array_def get_array_def Let_def ref_new_set upd_def ref_def  new_ref_def)
haftmann@26170
   415
wenzelm@26300
   416
text {*not actually true ???*}
haftmann@26170
   417
lemma upd_set_ref_swap: "upd a i v (set_ref r v' h) = set_ref r v' (upd a i v h)"
haftmann@26170
   418
apply (case_tac a)
haftmann@26170
   419
apply (simp add: Let_def upd_def)
haftmann@26170
   420
apply auto
wenzelm@26300
   421
oops
haftmann@26170
   422
haftmann@26170
   423
lemma length_new_ref[simp]: 
haftmann@26170
   424
  "length a (snd (ref v h)) = length a h"
haftmann@26170
   425
  by (simp add: get_array_def set_ref_def length_def new_ref_def ref_def Let_def)
haftmann@26170
   426
haftmann@26170
   427
lemma get_array_new_ref [simp]: 
haftmann@26170
   428
  "get_array a (snd (ref v h)) = get_array a h"
haftmann@26170
   429
  by (simp add: new_ref_def ref_def set_ref_def get_array_def Let_def)
haftmann@26170
   430
haftmann@26170
   431
lemma ref_present_upd [simp]: 
haftmann@26170
   432
  "ref_present r (upd a i v h) = ref_present r h"
haftmann@26170
   433
  by (simp add: upd_def ref_present_def set_array_def get_array_def)
haftmann@26170
   434
haftmann@26170
   435
lemma array_present_set_ref [simp]:
haftmann@26170
   436
  "array_present a (set_ref r v h) = array_present a h"
haftmann@26170
   437
  by (simp add: array_present_def set_ref_def)
haftmann@26170
   438
haftmann@26170
   439
lemma array_present_new_ref [simp]:
haftmann@26170
   440
  "array_present a h \<Longrightarrow> array_present a (snd (ref v h))"
haftmann@26170
   441
  by (simp add: array_present_def new_ref_def ref_def Let_def)
haftmann@26170
   442
haftmann@26170
   443
hide (open) const empty array array_of_list upd length ref
haftmann@26170
   444
haftmann@26170
   445
end