src/HOL/Imperative_HOL/Array.thy
changeset 37805 0f797d586ce5
parent 37804 0145e59c1f6c
child 37806 a7679be14442
     1.1 --- a/src/HOL/Imperative_HOL/Array.thy	Tue Jul 13 16:00:56 2010 +0200
     1.2 +++ b/src/HOL/Imperative_HOL/Array.thy	Tue Jul 13 16:12:40 2010 +0200
     1.3 @@ -13,9 +13,9 @@
     1.4  definition present :: "heap \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> bool" where
     1.5    "present h a \<longleftrightarrow> addr_of_array a < lim h"
     1.6  
     1.7 -definition (*FIXME get :: "heap \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a list" where*)
     1.8 -  get_array :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> 'a list" where
     1.9 -  "get_array a h = map from_nat (arrays h (TYPEREP('a)) (addr_of_array a))"
    1.10 +definition (*FIXME get *)
    1.11 +  get_array :: "heap \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a list" where
    1.12 +  "get_array h a = map from_nat (arrays h (TYPEREP('a)) (addr_of_array a))"
    1.13  
    1.14  definition set :: "'a\<Colon>heap array \<Rightarrow> 'a list \<Rightarrow> heap \<Rightarrow> heap" where
    1.15    "set a x = arrays_update (\<lambda>h. h(TYPEREP('a) := ((h(TYPEREP('a))) (addr_of_array a:=map to_nat x))))"
    1.16 @@ -28,10 +28,10 @@
    1.17     in (r, h''))"
    1.18  
    1.19  definition length :: "heap \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> nat" where
    1.20 -  "length h a = List.length (get_array a h)"
    1.21 +  "length h a = List.length (get_array h a)"
    1.22    
    1.23  definition update :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> heap \<Rightarrow> heap" where
    1.24 -  "update a i x h = set a ((get_array a h)[i:=x]) h"
    1.25 +  "update a i x h = set a ((get_array h a)[i:=x]) h"
    1.26  
    1.27  definition noteq :: "'a\<Colon>heap array \<Rightarrow> 'b\<Colon>heap array \<Rightarrow> bool" (infix "=!!=" 70) where
    1.28    "r =!!= s \<longleftrightarrow> TYPEREP('a) \<noteq> TYPEREP('b) \<or> addr_of_array r \<noteq> addr_of_array s"
    1.29 @@ -53,7 +53,7 @@
    1.30  
    1.31  definition nth :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a Heap" where
    1.32    [code del]: "nth a i = Heap_Monad.guard (\<lambda>h. i < length h a)
    1.33 -    (\<lambda>h. (get_array a h ! i, h))"
    1.34 +    (\<lambda>h. (get_array h a ! i, h))"
    1.35  
    1.36  definition upd :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a\<Colon>heap array Heap" where
    1.37    [code del]: "upd i x a = Heap_Monad.guard (\<lambda>h. i < length h a)
    1.38 @@ -61,14 +61,14 @@
    1.39  
    1.40  definition map_entry :: "nat \<Rightarrow> ('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a array \<Rightarrow> 'a array Heap" where
    1.41    [code del]: "map_entry i f a = Heap_Monad.guard (\<lambda>h. i < length h a)
    1.42 -    (\<lambda>h. (a, update a i (f (get_array a h ! i)) h))"
    1.43 +    (\<lambda>h. (a, update a i (f (get_array h a ! i)) h))"
    1.44  
    1.45  definition swap :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a Heap" where
    1.46    [code del]: "swap i x a = Heap_Monad.guard (\<lambda>h. i < length h a)
    1.47 -    (\<lambda>h. (get_array a h ! i, update a i x h))"
    1.48 +    (\<lambda>h. (get_array h a ! i, update a i x h))"
    1.49  
    1.50  definition freeze :: "'a\<Colon>heap array \<Rightarrow> 'a list Heap" where
    1.51 -  [code del]: "freeze a = Heap_Monad.tap (\<lambda>h. get_array a h)"
    1.52 +  [code del]: "freeze a = Heap_Monad.tap (\<lambda>h. get_array h a)"
    1.53  
    1.54  
    1.55  subsection {* Properties *}
    1.56 @@ -88,10 +88,10 @@
    1.57  lemma present_new_arr: "present h a \<Longrightarrow> a =!!= fst (alloc xs h)"
    1.58    by (simp add: present_def noteq_def alloc_def Let_def)
    1.59  
    1.60 -lemma array_get_set_eq [simp]: "get_array r (set r x h) = x"
    1.61 +lemma array_get_set_eq [simp]: "get_array (set r x h) r = x"
    1.62    by (simp add: get_array_def set_def o_def)
    1.63  
    1.64 -lemma array_get_set_neq [simp]: "r =!!= s \<Longrightarrow> get_array r (set s x h) = get_array r h"
    1.65 +lemma array_get_set_neq [simp]: "r =!!= s \<Longrightarrow> get_array (set s x h) r = get_array h r"
    1.66    by (simp add: noteq_def get_array_def set_def)
    1.67  
    1.68  lemma set_array_same [simp]:
    1.69 @@ -103,15 +103,15 @@
    1.70    by (simp add: Let_def expand_fun_eq noteq_def set_def)
    1.71  
    1.72  lemma get_array_update_eq [simp]:
    1.73 -  "get_array a (update a i v h) = (get_array a h) [i := v]"
    1.74 +  "get_array (update a i v h) a = (get_array h a) [i := v]"
    1.75    by (simp add: update_def)
    1.76  
    1.77  lemma nth_update_array_neq_array [simp]:
    1.78 -  "a =!!= b \<Longrightarrow> get_array a (update b j v h) ! i = get_array a h ! i"
    1.79 +  "a =!!= b \<Longrightarrow> get_array (update b j v h) a ! i = get_array h a ! i"
    1.80    by (simp add: update_def noteq_def)
    1.81  
    1.82  lemma get_arry_array_update_elem_neqIndex [simp]:
    1.83 -  "i \<noteq> j \<Longrightarrow> get_array a (update a j v h) ! i = get_array a h ! i"
    1.84 +  "i \<noteq> j \<Longrightarrow> get_array (update a j v h) a ! i = get_array h a ! i"
    1.85    by simp
    1.86  
    1.87  lemma length_update [simp]: 
    1.88 @@ -135,7 +135,7 @@
    1.89    by (auto simp add: update_def array_set_set_swap list_update_swap)
    1.90  
    1.91  lemma get_array_init_array_list:
    1.92 -  "get_array (fst (alloc ls h)) (snd (alloc ls' h)) = ls'"
    1.93 +  "get_array (snd (alloc ls' h)) (fst (alloc ls h)) = ls'"
    1.94    by (simp add: Let_def split_def alloc_def)
    1.95  
    1.96  lemma set_array:
    1.97 @@ -175,7 +175,7 @@
    1.98  lemma crel_newE [crel_elims]:
    1.99    assumes "crel (new n x) h h' r"
   1.100    obtains "r = fst (alloc (replicate n x) h)" "h' = snd (alloc (replicate n x) h)" 
   1.101 -    "get_array r h' = replicate n x" "present h' r" "\<not> present h r"
   1.102 +    "get_array h' r = replicate n x" "present h' r" "\<not> present h r"
   1.103    using assms by (rule crelE) (simp add: get_array_init_array_list execute_simps)
   1.104  
   1.105  lemma execute_of_list [execute_simps]:
   1.106 @@ -194,7 +194,7 @@
   1.107  lemma crel_of_listE [crel_elims]:
   1.108    assumes "crel (of_list xs) h h' r"
   1.109    obtains "r = fst (alloc xs h)" "h' = snd (alloc xs h)" 
   1.110 -    "get_array r h' = xs" "present h' r" "\<not> present h r"
   1.111 +    "get_array h' r = xs" "present h' r" "\<not> present h r"
   1.112    using assms by (rule crelE) (simp add: get_array_init_array_list execute_simps)
   1.113  
   1.114  lemma execute_make [execute_simps]:
   1.115 @@ -213,7 +213,7 @@
   1.116  lemma crel_makeE [crel_elims]:
   1.117    assumes "crel (make n f) h h' r"
   1.118    obtains "r = fst (alloc (map f [0 ..< n]) h)" "h' = snd (alloc (map f [0 ..< n]) h)" 
   1.119 -    "get_array r h' = map f [0 ..< n]" "present h' r" "\<not> present h r"
   1.120 +    "get_array h' r = map f [0 ..< n]" "present h' r" "\<not> present h r"
   1.121    using assms by (rule crelE) (simp add: get_array_init_array_list execute_simps)
   1.122  
   1.123  lemma execute_len [execute_simps]:
   1.124 @@ -236,7 +236,7 @@
   1.125  
   1.126  lemma execute_nth [execute_simps]:
   1.127    "i < length h a \<Longrightarrow>
   1.128 -    execute (nth a i) h = Some (get_array a h ! i, h)"
   1.129 +    execute (nth a i) h = Some (get_array h a ! i, h)"
   1.130    "i \<ge> length h a \<Longrightarrow> execute (nth a i) h = None"
   1.131    by (simp_all add: nth_def execute_simps)
   1.132  
   1.133 @@ -245,13 +245,13 @@
   1.134    by (auto intro: success_intros simp add: nth_def)
   1.135  
   1.136  lemma crel_nthI [crel_intros]:
   1.137 -  assumes "i < length h a" "h' = h" "r = get_array a h ! i"
   1.138 +  assumes "i < length h a" "h' = h" "r = get_array h a ! i"
   1.139    shows "crel (nth a i) h h' r"
   1.140    by (rule crelI) (insert assms, simp add: execute_simps)
   1.141  
   1.142  lemma crel_nthE [crel_elims]:
   1.143    assumes "crel (nth a i) h h' r"
   1.144 -  obtains "i < length h a" "r = get_array a h ! i" "h' = h"
   1.145 +  obtains "i < length h a" "r = get_array h a ! i" "h' = h"
   1.146    using assms by (rule crelE)
   1.147      (erule successE, cases "i < length h a", simp_all add: execute_simps)
   1.148  
   1.149 @@ -279,7 +279,7 @@
   1.150  lemma execute_map_entry [execute_simps]:
   1.151    "i < length h a \<Longrightarrow>
   1.152     execute (map_entry i f a) h =
   1.153 -      Some (a, update a i (f (get_array a h ! i)) h)"
   1.154 +      Some (a, update a i (f (get_array h a ! i)) h)"
   1.155    "i \<ge> length h a \<Longrightarrow> execute (map_entry i f a) h = None"
   1.156    by (simp_all add: map_entry_def execute_simps)
   1.157  
   1.158 @@ -288,20 +288,20 @@
   1.159    by (auto intro: success_intros simp add: map_entry_def)
   1.160  
   1.161  lemma crel_map_entryI [crel_intros]:
   1.162 -  assumes "i < length h a" "h' = update a i (f (get_array a h ! i)) h" "r = a"
   1.163 +  assumes "i < length h a" "h' = update a i (f (get_array h a ! i)) h" "r = a"
   1.164    shows "crel (map_entry i f a) h h' r"
   1.165    by (rule crelI) (insert assms, simp add: execute_simps)
   1.166  
   1.167  lemma crel_map_entryE [crel_elims]:
   1.168    assumes "crel (map_entry i f a) h h' r"
   1.169 -  obtains "r = a" "h' = update a i (f (get_array a h ! i)) h" "i < length h a"
   1.170 +  obtains "r = a" "h' = update a i (f (get_array h a ! i)) h" "i < length h a"
   1.171    using assms by (rule crelE)
   1.172      (erule successE, cases "i < length h a", simp_all add: execute_simps)
   1.173  
   1.174  lemma execute_swap [execute_simps]:
   1.175    "i < length h a \<Longrightarrow>
   1.176     execute (swap i x a) h =
   1.177 -      Some (get_array a h ! i, update a i x h)"
   1.178 +      Some (get_array h a ! i, update a i x h)"
   1.179    "i \<ge> length h a \<Longrightarrow> execute (swap i x a) h = None"
   1.180    by (simp_all add: swap_def execute_simps)
   1.181  
   1.182 @@ -310,18 +310,18 @@
   1.183    by (auto intro: success_intros simp add: swap_def)
   1.184  
   1.185  lemma crel_swapI [crel_intros]:
   1.186 -  assumes "i < length h a" "h' = update a i x h" "r = get_array a h ! i"
   1.187 +  assumes "i < length h a" "h' = update a i x h" "r = get_array h a ! i"
   1.188    shows "crel (swap i x a) h h' r"
   1.189    by (rule crelI) (insert assms, simp add: execute_simps)
   1.190  
   1.191  lemma crel_swapE [crel_elims]:
   1.192    assumes "crel (swap i x a) h h' r"
   1.193 -  obtains "r = get_array a h ! i" "h' = update a i x h" "i < length h a"
   1.194 +  obtains "r = get_array h a ! i" "h' = update a i x h" "i < length h a"
   1.195    using assms by (rule crelE)
   1.196      (erule successE, cases "i < length h a", simp_all add: execute_simps)
   1.197  
   1.198  lemma execute_freeze [execute_simps]:
   1.199 -  "execute (freeze a) h = Some (get_array a h, h)"
   1.200 +  "execute (freeze a) h = Some (get_array h a, h)"
   1.201    by (simp add: freeze_def execute_simps)
   1.202  
   1.203  lemma success_freezeI [success_intros]:
   1.204 @@ -329,13 +329,13 @@
   1.205    by (auto intro: success_intros simp add: freeze_def)
   1.206  
   1.207  lemma crel_freezeI [crel_intros]:
   1.208 -  assumes "h' = h" "r = get_array a h"
   1.209 +  assumes "h' = h" "r = get_array h a"
   1.210    shows "crel (freeze a) h h' r"
   1.211    by (rule crelI) (insert assms, simp add: execute_simps)
   1.212  
   1.213  lemma crel_freezeE [crel_elims]:
   1.214    assumes "crel (freeze a) h h' r"
   1.215 -  obtains "h' = h" "r = get_array a h"
   1.216 +  obtains "h' = h" "r = get_array h a"
   1.217    using assms by (rule crelE) (simp add: execute_simps)
   1.218  
   1.219  lemma upd_return:
   1.220 @@ -423,12 +423,12 @@
   1.221    fix h
   1.222    have *: "List.map
   1.223       (\<lambda>x. fst (the (if x < Array.length h a
   1.224 -                    then Some (get_array a h ! x, h) else None)))
   1.225 +                    then Some (get_array h a ! x, h) else None)))
   1.226       [0..<Array.length h a] =
   1.227 -       List.map (List.nth (get_array a h)) [0..<Array.length h a]"
   1.228 +       List.map (List.nth (get_array h a)) [0..<Array.length h a]"
   1.229      by simp
   1.230    have "execute (Heap_Monad.fold_map (Array.nth a) [0..<Array.length h a]) h =
   1.231 -    Some (get_array a h, h)"
   1.232 +    Some (get_array h a, h)"
   1.233      apply (subst execute_fold_map_unchanged_heap)
   1.234      apply (simp_all add: nth_def guard_def *)
   1.235      apply (simp add: length_def map_nth)
   1.236 @@ -436,7 +436,7 @@
   1.237    then have "execute (do {
   1.238        n \<leftarrow> Array.len a;
   1.239        Heap_Monad.fold_map (Array.nth a) [0..<n]
   1.240 -    }) h = Some (get_array a h, h)"
   1.241 +    }) h = Some (get_array h a, h)"
   1.242      by (auto intro: execute_bind_eq_SomeI simp add: execute_simps)
   1.243    then show "execute (Array.freeze a) h = execute (do {
   1.244        n \<leftarrow> Array.len a;