src/HOL/Imperative_HOL/Array.thy
author haftmann
Mon Jul 05 16:46:23 2010 +0200 (2010-07-05 ago)
changeset 37719 271ecd4fb9f9
parent 37716 24bb91462892
child 37752 d0a384c84d69
permissions -rw-r--r--
moved "open" operations from Heap.thy to Array.thy and Ref.thy
     1 (*  Title:      HOL/Imperative_HOL/Array.thy
     2     Author:     John Matthews, Galois Connections; Alexander Krauss, Lukas Bulwahn & Florian Haftmann, TU Muenchen
     3 *)
     4 
     5 header {* Monadic arrays *}
     6 
     7 theory Array
     8 imports Heap_Monad
     9 begin
    10 
    11 subsection {* Primitive layer *}
    12 
    13 definition 
    14   array_present :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> bool" where
    15   "array_present a h \<longleftrightarrow> addr_of_array a < lim h"
    16 
    17 definition
    18   get_array :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> 'a list" where
    19   "get_array a h = map from_nat (arrays h (TYPEREP('a)) (addr_of_array a))"
    20 
    21 definition
    22   set_array :: "'a\<Colon>heap array \<Rightarrow> 'a list \<Rightarrow> heap \<Rightarrow> heap" where
    23   "set_array a x = 
    24   arrays_update (\<lambda>h. h(TYPEREP('a) := ((h(TYPEREP('a))) (addr_of_array a:=map to_nat x))))"
    25 
    26 definition array :: "'a list \<Rightarrow> heap \<Rightarrow> 'a\<Colon>heap array \<times> heap" where
    27   "array xs h = (let
    28      l = lim h;
    29      r = Array l;
    30      h'' = set_array r xs (h\<lparr>lim := l + 1\<rparr>)
    31    in (r, h''))"
    32 
    33 definition length :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> nat" where
    34   "length a h = List.length (get_array a h)"
    35   
    36 definition change :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> heap \<Rightarrow> heap" where
    37   "change a i x h = set_array a ((get_array a h)[i:=x]) h"
    38 
    39 text {* Properties of imperative arrays *}
    40 
    41 text {* FIXME: Does there exist a "canonical" array axiomatisation in
    42 the literature?  *}
    43 
    44 definition noteq_arrs :: "('a\<Colon>heap) array \<Rightarrow> ('b\<Colon>heap) array \<Rightarrow> bool" (infix "=!!=" 70) where
    45   "r =!!= s \<longleftrightarrow> TYPEREP('a) \<noteq> TYPEREP('b) \<or> addr_of_array r \<noteq> addr_of_array s"
    46 
    47 lemma noteq_arrs_sym: "a =!!= b \<Longrightarrow> b =!!= a"
    48   and unequal_arrs [simp]: "a \<noteq> a' \<longleftrightarrow> a =!!= a'"
    49   unfolding noteq_arrs_def by auto
    50 
    51 lemma noteq_arrs_irrefl: "r =!!= r \<Longrightarrow> False"
    52   unfolding noteq_arrs_def by auto
    53 
    54 lemma present_new_arr: "array_present a h \<Longrightarrow> a =!!= fst (array xs h)"
    55   by (simp add: array_present_def noteq_arrs_def array_def Let_def)
    56 
    57 lemma array_get_set_eq [simp]: "get_array r (set_array r x h) = x"
    58   by (simp add: get_array_def set_array_def o_def)
    59 
    60 lemma array_get_set_neq [simp]: "r =!!= s \<Longrightarrow> get_array r (set_array s x h) = get_array r h"
    61   by (simp add: noteq_arrs_def get_array_def set_array_def)
    62 
    63 lemma set_array_same [simp]:
    64   "set_array r x (set_array r y h) = set_array r x h"
    65   by (simp add: set_array_def)
    66 
    67 lemma array_set_set_swap:
    68   "r =!!= r' \<Longrightarrow> set_array r x (set_array r' x' h) = set_array r' x' (set_array r x h)"
    69   by (simp add: Let_def expand_fun_eq noteq_arrs_def set_array_def)
    70 
    71 lemma get_array_change_eq [simp]:
    72   "get_array a (change a i v h) = (get_array a h) [i := v]"
    73   by (simp add: change_def)
    74 
    75 lemma nth_change_array_neq_array [simp]:
    76   "a =!!= b \<Longrightarrow> get_array a (change b j v h) ! i = get_array a h ! i"
    77   by (simp add: change_def noteq_arrs_def)
    78 
    79 lemma get_arry_array_change_elem_neqIndex [simp]:
    80   "i \<noteq> j \<Longrightarrow> get_array a (change a j v h) ! i = get_array a h ! i"
    81   by simp
    82 
    83 lemma length_change [simp]: 
    84   "length a (change b i v h) = length a h"
    85   by (simp add: change_def length_def set_array_def get_array_def)
    86 
    87 lemma change_swap_neqArray:
    88   "a =!!= a' \<Longrightarrow> 
    89   change a i v (change a' i' v' h) 
    90   = change a' i' v' (change a i v h)"
    91 apply (unfold change_def)
    92 apply simp
    93 apply (subst array_set_set_swap, assumption)
    94 apply (subst array_get_set_neq)
    95 apply (erule noteq_arrs_sym)
    96 apply (simp)
    97 done
    98 
    99 lemma change_swap_neqIndex:
   100   "\<lbrakk> i \<noteq> i' \<rbrakk> \<Longrightarrow> change a i v (change a i' v' h) = change a i' v' (change a i v h)"
   101   by (auto simp add: change_def array_set_set_swap list_update_swap)
   102 
   103 lemma get_array_init_array_list:
   104   "get_array (fst (array ls h)) (snd (array ls' h)) = ls'"
   105   by (simp add: Let_def split_def array_def)
   106 
   107 lemma set_array:
   108   "set_array (fst (array ls h))
   109      new_ls (snd (array ls h))
   110        = snd (array new_ls h)"
   111   by (simp add: Let_def split_def array_def)
   112 
   113 lemma array_present_change [simp]: 
   114   "array_present a (change b i v h) = array_present a h"
   115   by (simp add: change_def array_present_def set_array_def get_array_def)
   116 
   117 
   118 
   119 subsection {* Primitives *}
   120 
   121 definition
   122   new :: "nat \<Rightarrow> 'a\<Colon>heap \<Rightarrow> 'a array Heap" where
   123   [code del]: "new n x = Heap_Monad.heap (Array.array (replicate n x))"
   124 
   125 definition
   126   of_list :: "'a\<Colon>heap list \<Rightarrow> 'a array Heap" where
   127   [code del]: "of_list xs = Heap_Monad.heap (Array.array xs)"
   128 
   129 definition
   130   len :: "'a\<Colon>heap array \<Rightarrow> nat Heap" where
   131   [code del]: "len arr = Heap_Monad.heap (\<lambda>h. (Array.length arr h, h))"
   132 
   133 definition
   134   nth :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a Heap"
   135 where
   136   [code del]: "nth a i = (do len \<leftarrow> len a;
   137                  (if i < len
   138                      then Heap_Monad.heap (\<lambda>h. (get_array a h ! i, h))
   139                      else raise ''array lookup: index out of range'')
   140               done)"
   141 
   142 definition
   143   upd :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a\<Colon>heap array Heap"
   144 where
   145   [code del]: "upd i x a = (do len \<leftarrow> len a;
   146                       (if i < len
   147                            then Heap_Monad.heap (\<lambda>h. (a, change a i x h))
   148                            else raise ''array update: index out of range'')
   149                    done)" 
   150 
   151 lemma upd_return:
   152   "upd i x a \<guillemotright> return a = upd i x a"
   153   by (rule Heap_eqI) (simp add: upd_def bindM_def split: option.split) 
   154 
   155 
   156 subsection {* Derivates *}
   157 
   158 definition
   159   map_entry :: "nat \<Rightarrow> ('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a array \<Rightarrow> 'a array Heap"
   160 where
   161   "map_entry i f a = (do
   162      x \<leftarrow> nth a i;
   163      upd i (f x) a
   164    done)"
   165 
   166 definition
   167   swap :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a Heap"
   168 where
   169   "swap i x a = (do
   170      y \<leftarrow> nth a i;
   171      upd i x a;
   172      return y
   173    done)"
   174 
   175 definition
   176   make :: "nat \<Rightarrow> (nat \<Rightarrow> 'a\<Colon>heap) \<Rightarrow> 'a array Heap"
   177 where
   178   "make n f = of_list (map f [0 ..< n])"
   179 
   180 definition
   181   freeze :: "'a\<Colon>heap array \<Rightarrow> 'a list Heap"
   182 where
   183   "freeze a = (do
   184      n \<leftarrow> len a;
   185      mapM (nth a) [0..<n]
   186    done)"
   187 
   188 definition
   189    map :: "('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a array \<Rightarrow> 'a array Heap"
   190 where
   191   "map f a = (do
   192      n \<leftarrow> len a;
   193      mapM (\<lambda>n. map_entry n f a) [0..<n];
   194      return a
   195    done)"
   196 
   197 
   198 
   199 subsection {* Properties *}
   200 
   201 lemma array_make [code]:
   202   "Array.new n x = make n (\<lambda>_. x)"
   203   by (rule Heap_eqI) (simp add: make_def new_def map_replicate_trivial of_list_def)
   204 
   205 lemma array_of_list_make [code]:
   206   "of_list xs = make (List.length xs) (\<lambda>n. xs ! n)"
   207   by (rule Heap_eqI) (simp add: make_def map_nth)
   208 
   209 
   210 subsection {* Code generator setup *}
   211 
   212 subsubsection {* Logical intermediate layer *}
   213 
   214 definition new' where
   215   [code del]: "new' = Array.new o Code_Numeral.nat_of"
   216 hide_const (open) new'
   217 lemma [code]:
   218   "Array.new = Array.new' o Code_Numeral.of_nat"
   219   by (simp add: new'_def o_def)
   220 
   221 definition of_list' where
   222   [code del]: "of_list' i xs = Array.of_list (take (Code_Numeral.nat_of i) xs)"
   223 hide_const (open) of_list'
   224 lemma [code]:
   225   "Array.of_list xs = Array.of_list' (Code_Numeral.of_nat (List.length xs)) xs"
   226   by (simp add: of_list'_def)
   227 
   228 definition make' where
   229   [code del]: "make' i f = Array.make (Code_Numeral.nat_of i) (f o Code_Numeral.of_nat)"
   230 hide_const (open) make'
   231 lemma [code]:
   232   "Array.make n f = Array.make' (Code_Numeral.of_nat n) (f o Code_Numeral.nat_of)"
   233   by (simp add: make'_def o_def)
   234 
   235 definition len' where
   236   [code del]: "len' a = Array.len a \<guillemotright>= (\<lambda>n. return (Code_Numeral.of_nat n))"
   237 hide_const (open) len'
   238 lemma [code]:
   239   "Array.len a = Array.len' a \<guillemotright>= (\<lambda>i. return (Code_Numeral.nat_of i))"
   240   by (simp add: len'_def)
   241 
   242 definition nth' where
   243   [code del]: "nth' a = Array.nth a o Code_Numeral.nat_of"
   244 hide_const (open) nth'
   245 lemma [code]:
   246   "Array.nth a n = Array.nth' a (Code_Numeral.of_nat n)"
   247   by (simp add: nth'_def)
   248 
   249 definition upd' where
   250   [code del]: "upd' a i x = Array.upd (Code_Numeral.nat_of i) x a \<guillemotright> return ()"
   251 hide_const (open) upd'
   252 lemma [code]:
   253   "Array.upd i x a = Array.upd' a (Code_Numeral.of_nat i) x \<guillemotright> return a"
   254   by (simp add: upd'_def upd_return)
   255 
   256 
   257 subsubsection {* SML *}
   258 
   259 code_type array (SML "_/ array")
   260 code_const Array (SML "raise/ (Fail/ \"bare Array\")")
   261 code_const Array.new' (SML "(fn/ ()/ =>/ Array.array/ ((_),/ (_)))")
   262 code_const Array.of_list' (SML "(fn/ ()/ =>/ Array.fromList/ _)")
   263 code_const Array.make' (SML "(fn/ ()/ =>/ Array.tabulate/ ((_),/ (_)))")
   264 code_const Array.len' (SML "(fn/ ()/ =>/ Array.length/ _)")
   265 code_const Array.nth' (SML "(fn/ ()/ =>/ Array.sub/ ((_),/ (_)))")
   266 code_const Array.upd' (SML "(fn/ ()/ =>/ Array.update/ ((_),/ (_),/ (_)))")
   267 
   268 code_reserved SML Array
   269 
   270 
   271 subsubsection {* OCaml *}
   272 
   273 code_type array (OCaml "_/ array")
   274 code_const Array (OCaml "failwith/ \"bare Array\"")
   275 code_const Array.new' (OCaml "(fun/ ()/ ->/ Array.make/ (Big'_int.int'_of'_big'_int/ _)/ _)")
   276 code_const Array.of_list' (OCaml "(fun/ ()/ ->/ Array.of'_list/ _)")
   277 code_const Array.len' (OCaml "(fun/ ()/ ->/ Big'_int.big'_int'_of'_int/ (Array.length/ _))")
   278 code_const Array.nth' (OCaml "(fun/ ()/ ->/ Array.get/ _/ (Big'_int.int'_of'_big'_int/ _))")
   279 code_const Array.upd' (OCaml "(fun/ ()/ ->/ Array.set/ _/ (Big'_int.int'_of'_big'_int/ _)/ _)")
   280 
   281 code_reserved OCaml Array
   282 
   283 
   284 subsubsection {* Haskell *}
   285 
   286 code_type array (Haskell "Heap.STArray/ Heap.RealWorld/ _")
   287 code_const Array (Haskell "error/ \"bare Array\"")
   288 code_const Array.new' (Haskell "Heap.newArray/ (0,/ _)")
   289 code_const Array.of_list' (Haskell "Heap.newListArray/ (0,/ _)")
   290 code_const Array.len' (Haskell "Heap.lengthArray")
   291 code_const Array.nth' (Haskell "Heap.readArray")
   292 code_const Array.upd' (Haskell "Heap.writeArray")
   293 
   294 hide_const (open) new map -- {* avoid clashed with some popular names *}
   295 
   296 end