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