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