src/HOL/Imperative_HOL/Array.thy
changeset 37752 d0a384c84d69
parent 37719 271ecd4fb9f9
child 37756 59caa6180fff
     1.1 --- a/src/HOL/Imperative_HOL/Array.thy	Fri Jul 09 08:11:10 2010 +0200
     1.2 +++ b/src/HOL/Imperative_HOL/Array.thy	Fri Jul 09 09:48:52 2010 +0200
     1.3 @@ -8,42 +8,81 @@
     1.4  imports Heap_Monad
     1.5  begin
     1.6  
     1.7 -subsection {* Primitive layer *}
     1.8 +subsection {* Primitives *}
     1.9  
    1.10 -definition 
    1.11 +definition (*FIXME present :: "heap \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> bool" where*)
    1.12    array_present :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> bool" where
    1.13    "array_present a h \<longleftrightarrow> addr_of_array a < lim h"
    1.14  
    1.15 -definition
    1.16 +definition (*FIXME get :: "heap \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a list" where*)
    1.17    get_array :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> 'a list" where
    1.18    "get_array a h = map from_nat (arrays h (TYPEREP('a)) (addr_of_array a))"
    1.19  
    1.20 -definition
    1.21 +definition (*FIXME set*)
    1.22    set_array :: "'a\<Colon>heap array \<Rightarrow> 'a list \<Rightarrow> heap \<Rightarrow> heap" where
    1.23    "set_array a x = 
    1.24    arrays_update (\<lambda>h. h(TYPEREP('a) := ((h(TYPEREP('a))) (addr_of_array a:=map to_nat x))))"
    1.25  
    1.26 -definition array :: "'a list \<Rightarrow> heap \<Rightarrow> 'a\<Colon>heap array \<times> heap" where
    1.27 +definition (*FIXME alloc*)
    1.28 +  array :: "'a list \<Rightarrow> heap \<Rightarrow> 'a\<Colon>heap array \<times> heap" where
    1.29    "array xs h = (let
    1.30       l = lim h;
    1.31       r = Array l;
    1.32       h'' = set_array r xs (h\<lparr>lim := l + 1\<rparr>)
    1.33     in (r, h''))"
    1.34  
    1.35 -definition length :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> nat" where
    1.36 +definition (*FIXME length :: "heap \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> nat" where*)
    1.37 +  length :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> nat" where
    1.38    "length a h = List.length (get_array a h)"
    1.39    
    1.40 -definition change :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> heap \<Rightarrow> heap" where
    1.41 +definition (*FIXME update*)
    1.42 +  change :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> heap \<Rightarrow> heap" where
    1.43    "change a i x h = set_array a ((get_array a h)[i:=x]) h"
    1.44  
    1.45 -text {* Properties of imperative arrays *}
    1.46 +definition (*FIXME noteq*)
    1.47 +  noteq_arrs :: "'a\<Colon>heap array \<Rightarrow> 'b\<Colon>heap array \<Rightarrow> bool" (infix "=!!=" 70) where
    1.48 +  "r =!!= s \<longleftrightarrow> TYPEREP('a) \<noteq> TYPEREP('b) \<or> addr_of_array r \<noteq> addr_of_array s"
    1.49 +
    1.50 +
    1.51 +subsection {* Monad operations *}
    1.52 +
    1.53 +definition new :: "nat \<Rightarrow> 'a\<Colon>heap \<Rightarrow> 'a array Heap" where
    1.54 +  [code del]: "new n x = Heap_Monad.heap (array (replicate n x))"
    1.55 +
    1.56 +definition of_list :: "'a\<Colon>heap list \<Rightarrow> 'a array Heap" where
    1.57 +  [code del]: "of_list xs = Heap_Monad.heap (array xs)"
    1.58 +
    1.59 +definition make :: "nat \<Rightarrow> (nat \<Rightarrow> 'a\<Colon>heap) \<Rightarrow> 'a array Heap" where
    1.60 +  [code del]: "make n f = Heap_Monad.heap (array (map f [0 ..< n]))"
    1.61 +
    1.62 +definition len :: "'a\<Colon>heap array \<Rightarrow> nat Heap" where
    1.63 +  [code del]: "len a = Heap_Monad.heap (\<lambda>h. (length a h, h))"
    1.64 +
    1.65 +definition nth :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a Heap" where
    1.66 +  [code del]: "nth a i = Heap_Monad.guard (\<lambda>h. i < length a h)
    1.67 +    (\<lambda>h. (get_array a h ! i, h))"
    1.68 +
    1.69 +definition upd :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a\<Colon>heap array Heap" where
    1.70 +  [code del]: "upd i x a = Heap_Monad.guard (\<lambda>h. i < length a h)
    1.71 +    (\<lambda>h. (a, change a i x h))"
    1.72 +
    1.73 +definition map_entry :: "nat \<Rightarrow> ('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a array \<Rightarrow> 'a array Heap" where
    1.74 +  [code del]: "map_entry i f a = Heap_Monad.guard (\<lambda>h. i < length a h)
    1.75 +    (\<lambda>h. (a, change a i (f (get_array a h ! i)) h))"
    1.76 +
    1.77 +definition swap :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a Heap" where
    1.78 +  [code del]: "swap i x a = Heap_Monad.guard (\<lambda>h. i < length a h)
    1.79 +    (\<lambda>h. (get_array a h ! i, change a i x h))"
    1.80 +
    1.81 +definition freeze :: "'a\<Colon>heap array \<Rightarrow> 'a list Heap" where
    1.82 +  [code del]: "freeze a = Heap_Monad.heap (\<lambda>h. (get_array a h, h))"
    1.83 +
    1.84 +
    1.85 +subsection {* Properties *}
    1.86  
    1.87  text {* FIXME: Does there exist a "canonical" array axiomatisation in
    1.88  the literature?  *}
    1.89  
    1.90 -definition noteq_arrs :: "('a\<Colon>heap) array \<Rightarrow> ('b\<Colon>heap) array \<Rightarrow> bool" (infix "=!!=" 70) where
    1.91 -  "r =!!= s \<longleftrightarrow> TYPEREP('a) \<noteq> TYPEREP('b) \<or> addr_of_array r \<noteq> addr_of_array s"
    1.92 -
    1.93  lemma noteq_arrs_sym: "a =!!= b \<Longrightarrow> b =!!= a"
    1.94    and unequal_arrs [simp]: "a \<noteq> a' \<longleftrightarrow> a =!!= a'"
    1.95    unfolding noteq_arrs_def by auto
    1.96 @@ -114,97 +153,65 @@
    1.97    "array_present a (change b i v h) = array_present a h"
    1.98    by (simp add: change_def array_present_def set_array_def get_array_def)
    1.99  
   1.100 -
   1.101 -
   1.102 -subsection {* Primitives *}
   1.103 +lemma execute_new [simp]:
   1.104 +  "Heap_Monad.execute (new n x) h = Some (array (replicate n x) h)"
   1.105 +  by (simp add: new_def)
   1.106  
   1.107 -definition
   1.108 -  new :: "nat \<Rightarrow> 'a\<Colon>heap \<Rightarrow> 'a array Heap" where
   1.109 -  [code del]: "new n x = Heap_Monad.heap (Array.array (replicate n x))"
   1.110 +lemma execute_of_list [simp]:
   1.111 +  "Heap_Monad.execute (of_list xs) h = Some (array xs h)"
   1.112 +  by (simp add: of_list_def)
   1.113  
   1.114 -definition
   1.115 -  of_list :: "'a\<Colon>heap list \<Rightarrow> 'a array Heap" where
   1.116 -  [code del]: "of_list xs = Heap_Monad.heap (Array.array xs)"
   1.117 +lemma execute_make [simp]:
   1.118 +  "Heap_Monad.execute (make n f) h = Some (array (map f [0 ..< n]) h)"
   1.119 +  by (simp add: make_def)
   1.120  
   1.121 -definition
   1.122 -  len :: "'a\<Colon>heap array \<Rightarrow> nat Heap" where
   1.123 -  [code del]: "len arr = Heap_Monad.heap (\<lambda>h. (Array.length arr h, h))"
   1.124 +lemma execute_len [simp]:
   1.125 +  "Heap_Monad.execute (len a) h = Some (length a h, h)"
   1.126 +  by (simp add: len_def)
   1.127 +
   1.128 +lemma execute_nth [simp]:
   1.129 +  "i < length a h \<Longrightarrow>
   1.130 +    Heap_Monad.execute (nth a i) h = Some (get_array a h ! i, h)"
   1.131 +  "i \<ge> length a h \<Longrightarrow> Heap_Monad.execute (nth a i) h = None"
   1.132 +  by (simp_all add: nth_def)
   1.133  
   1.134 -definition
   1.135 -  nth :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a Heap"
   1.136 -where
   1.137 -  [code del]: "nth a i = (do len \<leftarrow> len a;
   1.138 -                 (if i < len
   1.139 -                     then Heap_Monad.heap (\<lambda>h. (get_array a h ! i, h))
   1.140 -                     else raise ''array lookup: index out of range'')
   1.141 -              done)"
   1.142 +lemma execute_upd [simp]:
   1.143 +  "i < length a h \<Longrightarrow>
   1.144 +    Heap_Monad.execute (upd i x a) h = Some (a, change a i x h)"
   1.145 +  "i \<ge> length a h \<Longrightarrow> Heap_Monad.execute (nth a i) h = None"
   1.146 +  by (simp_all add: upd_def)
   1.147  
   1.148 -definition
   1.149 -  upd :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a\<Colon>heap array Heap"
   1.150 -where
   1.151 -  [code del]: "upd i x a = (do len \<leftarrow> len a;
   1.152 -                      (if i < len
   1.153 -                           then Heap_Monad.heap (\<lambda>h. (a, change a i x h))
   1.154 -                           else raise ''array update: index out of range'')
   1.155 -                   done)" 
   1.156 +lemma execute_map_entry [simp]:
   1.157 +  "i < length a h \<Longrightarrow>
   1.158 +   Heap_Monad.execute (map_entry i f a) h =
   1.159 +      Some (a, change a i (f (get_array a h ! i)) h)"
   1.160 +  "i \<ge> length a h \<Longrightarrow> Heap_Monad.execute (nth a i) h = None"
   1.161 +  by (simp_all add: map_entry_def)
   1.162 +
   1.163 +lemma execute_swap [simp]:
   1.164 +  "i < length a h \<Longrightarrow>
   1.165 +   Heap_Monad.execute (swap i x a) h =
   1.166 +      Some (get_array a h ! i, change a i x h)"
   1.167 +  "i \<ge> length a h \<Longrightarrow> Heap_Monad.execute (nth a i) h = None"
   1.168 +  by (simp_all add: swap_def)
   1.169 +
   1.170 +lemma execute_freeze [simp]:
   1.171 +  "Heap_Monad.execute (freeze a) h = Some (get_array a h, h)"
   1.172 +  by (simp add: freeze_def)
   1.173  
   1.174  lemma upd_return:
   1.175    "upd i x a \<guillemotright> return a = upd i x a"
   1.176 -  by (rule Heap_eqI) (simp add: upd_def bindM_def split: option.split) 
   1.177 -
   1.178 -
   1.179 -subsection {* Derivates *}
   1.180 -
   1.181 -definition
   1.182 -  map_entry :: "nat \<Rightarrow> ('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a array \<Rightarrow> 'a array Heap"
   1.183 -where
   1.184 -  "map_entry i f a = (do
   1.185 -     x \<leftarrow> nth a i;
   1.186 -     upd i (f x) a
   1.187 -   done)"
   1.188 +  by (rule Heap_eqI) (simp add: bindM_def guard_def upd_def)
   1.189  
   1.190 -definition
   1.191 -  swap :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a Heap"
   1.192 -where
   1.193 -  "swap i x a = (do
   1.194 -     y \<leftarrow> nth a i;
   1.195 -     upd i x a;
   1.196 -     return y
   1.197 -   done)"
   1.198 -
   1.199 -definition
   1.200 -  make :: "nat \<Rightarrow> (nat \<Rightarrow> 'a\<Colon>heap) \<Rightarrow> 'a array Heap"
   1.201 -where
   1.202 -  "make n f = of_list (map f [0 ..< n])"
   1.203 +lemma array_make:
   1.204 +  "new n x = make n (\<lambda>_. x)"
   1.205 +  by (rule Heap_eqI) (simp add: map_replicate_trivial)
   1.206  
   1.207 -definition
   1.208 -  freeze :: "'a\<Colon>heap array \<Rightarrow> 'a list Heap"
   1.209 -where
   1.210 -  "freeze a = (do
   1.211 -     n \<leftarrow> len a;
   1.212 -     mapM (nth a) [0..<n]
   1.213 -   done)"
   1.214 +lemma array_of_list_make:
   1.215 +  "of_list xs = make (List.length xs) (\<lambda>n. xs ! n)"
   1.216 +  by (rule Heap_eqI) (simp add: map_nth)
   1.217  
   1.218 -definition
   1.219 -   map :: "('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a array \<Rightarrow> 'a array Heap"
   1.220 -where
   1.221 -  "map f a = (do
   1.222 -     n \<leftarrow> len a;
   1.223 -     mapM (\<lambda>n. map_entry n f a) [0..<n];
   1.224 -     return a
   1.225 -   done)"
   1.226 -
   1.227 -
   1.228 -
   1.229 -subsection {* Properties *}
   1.230 -
   1.231 -lemma array_make [code]:
   1.232 -  "Array.new n x = make n (\<lambda>_. x)"
   1.233 -  by (rule Heap_eqI) (simp add: make_def new_def map_replicate_trivial of_list_def)
   1.234 -
   1.235 -lemma array_of_list_make [code]:
   1.236 -  "of_list xs = make (List.length xs) (\<lambda>n. xs ! n)"
   1.237 -  by (rule Heap_eqI) (simp add: make_def map_nth)
   1.238 +hide_const (open) new map
   1.239  
   1.240  
   1.241  subsection {* Code generator setup *}
   1.242 @@ -213,48 +220,95 @@
   1.243  
   1.244  definition new' where
   1.245    [code del]: "new' = Array.new o Code_Numeral.nat_of"
   1.246 -hide_const (open) new'
   1.247 +
   1.248  lemma [code]:
   1.249 -  "Array.new = Array.new' o Code_Numeral.of_nat"
   1.250 +  "Array.new = new' o Code_Numeral.of_nat"
   1.251    by (simp add: new'_def o_def)
   1.252  
   1.253  definition of_list' where
   1.254    [code del]: "of_list' i xs = Array.of_list (take (Code_Numeral.nat_of i) xs)"
   1.255 -hide_const (open) of_list'
   1.256 +
   1.257  lemma [code]:
   1.258 -  "Array.of_list xs = Array.of_list' (Code_Numeral.of_nat (List.length xs)) xs"
   1.259 +  "Array.of_list xs = of_list' (Code_Numeral.of_nat (List.length xs)) xs"
   1.260    by (simp add: of_list'_def)
   1.261  
   1.262  definition make' where
   1.263    [code del]: "make' i f = Array.make (Code_Numeral.nat_of i) (f o Code_Numeral.of_nat)"
   1.264 -hide_const (open) make'
   1.265 +
   1.266  lemma [code]:
   1.267 -  "Array.make n f = Array.make' (Code_Numeral.of_nat n) (f o Code_Numeral.nat_of)"
   1.268 +  "Array.make n f = make' (Code_Numeral.of_nat n) (f o Code_Numeral.nat_of)"
   1.269    by (simp add: make'_def o_def)
   1.270  
   1.271  definition len' where
   1.272    [code del]: "len' a = Array.len a \<guillemotright>= (\<lambda>n. return (Code_Numeral.of_nat n))"
   1.273 -hide_const (open) len'
   1.274 +
   1.275  lemma [code]:
   1.276 -  "Array.len a = Array.len' a \<guillemotright>= (\<lambda>i. return (Code_Numeral.nat_of i))"
   1.277 +  "Array.len a = len' a \<guillemotright>= (\<lambda>i. return (Code_Numeral.nat_of i))"
   1.278    by (simp add: len'_def)
   1.279  
   1.280  definition nth' where
   1.281    [code del]: "nth' a = Array.nth a o Code_Numeral.nat_of"
   1.282 -hide_const (open) nth'
   1.283 +
   1.284  lemma [code]:
   1.285 -  "Array.nth a n = Array.nth' a (Code_Numeral.of_nat n)"
   1.286 +  "Array.nth a n = nth' a (Code_Numeral.of_nat n)"
   1.287    by (simp add: nth'_def)
   1.288  
   1.289  definition upd' where
   1.290    [code del]: "upd' a i x = Array.upd (Code_Numeral.nat_of i) x a \<guillemotright> return ()"
   1.291 -hide_const (open) upd'
   1.292 +
   1.293  lemma [code]:
   1.294 -  "Array.upd i x a = Array.upd' a (Code_Numeral.of_nat i) x \<guillemotright> return a"
   1.295 +  "Array.upd i x a = upd' a (Code_Numeral.of_nat i) x \<guillemotright> return a"
   1.296    by (simp add: upd'_def upd_return)
   1.297  
   1.298 +lemma [code]:
   1.299 +  "map_entry i f a = (do
   1.300 +     x \<leftarrow> nth a i;
   1.301 +     upd i (f x) a
   1.302 +   done)"
   1.303 +  by (rule Heap_eqI) (simp add: bindM_def guard_def map_entry_def)
   1.304  
   1.305 -subsubsection {* SML *}
   1.306 +lemma [code]:
   1.307 +  "swap i x a = (do
   1.308 +     y \<leftarrow> nth a i;
   1.309 +     upd i x a;
   1.310 +     return y
   1.311 +   done)"
   1.312 +  by (rule Heap_eqI) (simp add: bindM_def guard_def swap_def)
   1.313 +
   1.314 +lemma [code]:
   1.315 +  "freeze a = (do
   1.316 +     n \<leftarrow> len a;
   1.317 +     mapM (\<lambda>i. nth a i) [0..<n]
   1.318 +   done)"
   1.319 +proof (rule Heap_eqI)
   1.320 +  fix h
   1.321 +  have *: "List.map
   1.322 +     (\<lambda>x. fst (the (if x < length a h
   1.323 +                    then Some (get_array a h ! x, h) else None)))
   1.324 +     [0..<length a h] =
   1.325 +       List.map (List.nth (get_array a h)) [0..<length a h]"
   1.326 +    by simp
   1.327 +  have "Heap_Monad.execute (mapM (Array.nth a) [0..<length a h]) h =
   1.328 +    Some (get_array a h, h)"
   1.329 +    apply (subst execute_mapM_unchanged_heap)
   1.330 +    apply (simp_all add: nth_def guard_def *)
   1.331 +    apply (simp add: length_def map_nth)
   1.332 +    done
   1.333 +  then have "Heap_Monad.execute (do
   1.334 +      n \<leftarrow> len a;
   1.335 +      mapM (Array.nth a) [0..<n]
   1.336 +    done) h = Some (get_array a h, h)"
   1.337 +    by (auto intro: execute_eq_SomeI)
   1.338 +  then show "Heap_Monad.execute (freeze a) h = Heap_Monad.execute (do
   1.339 +      n \<leftarrow> len a;
   1.340 +      mapM (Array.nth a) [0..<n]
   1.341 +    done) h" by simp
   1.342 +qed
   1.343 +
   1.344 +hide_const (open) new' of_list' make' len' nth' upd'
   1.345 +
   1.346 +
   1.347 +text {* SML *}
   1.348  
   1.349  code_type array (SML "_/ array")
   1.350  code_const Array (SML "raise/ (Fail/ \"bare Array\")")
   1.351 @@ -268,7 +322,7 @@
   1.352  code_reserved SML Array
   1.353  
   1.354  
   1.355 -subsubsection {* OCaml *}
   1.356 +text {* OCaml *}
   1.357  
   1.358  code_type array (OCaml "_/ array")
   1.359  code_const Array (OCaml "failwith/ \"bare Array\"")
   1.360 @@ -281,7 +335,7 @@
   1.361  code_reserved OCaml Array
   1.362  
   1.363  
   1.364 -subsubsection {* Haskell *}
   1.365 +text {* Haskell *}
   1.366  
   1.367  code_type array (Haskell "Heap.STArray/ Heap.RealWorld/ _")
   1.368  code_const Array (Haskell "error/ \"bare Array\"")
   1.369 @@ -291,6 +345,4 @@
   1.370  code_const Array.nth' (Haskell "Heap.readArray")
   1.371  code_const Array.upd' (Haskell "Heap.writeArray")
   1.372  
   1.373 -hide_const (open) new map -- {* avoid clashed with some popular names *}
   1.374 -
   1.375  end