src/HOL/Imperative_HOL/Array.thy
author haftmann
Tue Jul 13 15:37:31 2010 +0200 (2010-07-13 ago)
changeset 37803 582d0fbd201e
parent 37802 f2e9c104cebd
child 37804 0145e59c1f6c
permissions -rw-r--r--
canonical argument order for present
haftmann@31870
     1
(*  Title:      HOL/Imperative_HOL/Array.thy
haftmann@26170
     2
    Author:     John Matthews, Galois Connections; Alexander Krauss, Lukas Bulwahn & Florian Haftmann, TU Muenchen
haftmann@26170
     3
*)
haftmann@26170
     4
haftmann@26170
     5
header {* Monadic arrays *}
haftmann@26170
     6
haftmann@26170
     7
theory Array
haftmann@31203
     8
imports Heap_Monad
haftmann@26170
     9
begin
haftmann@26170
    10
haftmann@37752
    11
subsection {* Primitives *}
haftmann@37719
    12
haftmann@37803
    13
definition (*FIXME present *)
haftmann@37803
    14
  array_present :: "heap \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> bool" where
haftmann@37803
    15
  "array_present h a \<longleftrightarrow> addr_of_array a < lim h"
haftmann@37719
    16
haftmann@37752
    17
definition (*FIXME get :: "heap \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a list" where*)
haftmann@37719
    18
  get_array :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> 'a list" where
haftmann@37719
    19
  "get_array a h = map from_nat (arrays h (TYPEREP('a)) (addr_of_array a))"
haftmann@37719
    20
haftmann@37752
    21
definition (*FIXME set*)
haftmann@37719
    22
  set_array :: "'a\<Colon>heap array \<Rightarrow> 'a list \<Rightarrow> heap \<Rightarrow> heap" where
haftmann@37719
    23
  "set_array a x = 
haftmann@37719
    24
  arrays_update (\<lambda>h. h(TYPEREP('a) := ((h(TYPEREP('a))) (addr_of_array a:=map to_nat x))))"
haftmann@37719
    25
haftmann@37752
    26
definition (*FIXME alloc*)
haftmann@37752
    27
  array :: "'a list \<Rightarrow> heap \<Rightarrow> 'a\<Colon>heap array \<times> heap" where
haftmann@37719
    28
  "array xs h = (let
haftmann@37719
    29
     l = lim h;
haftmann@37719
    30
     r = Array l;
haftmann@37719
    31
     h'' = set_array r xs (h\<lparr>lim := l + 1\<rparr>)
haftmann@37719
    32
   in (r, h''))"
haftmann@37719
    33
haftmann@37802
    34
definition length :: "heap \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> nat" where
haftmann@37802
    35
  "length h a = List.length (get_array a h)"
haftmann@37719
    36
  
haftmann@37798
    37
definition update :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> heap \<Rightarrow> heap" where
haftmann@37798
    38
  "update a i x h = set_array a ((get_array a h)[i:=x]) h"
haftmann@37719
    39
haftmann@37752
    40
definition (*FIXME noteq*)
haftmann@37752
    41
  noteq_arrs :: "'a\<Colon>heap array \<Rightarrow> 'b\<Colon>heap array \<Rightarrow> bool" (infix "=!!=" 70) where
haftmann@37752
    42
  "r =!!= s \<longleftrightarrow> TYPEREP('a) \<noteq> TYPEREP('b) \<or> addr_of_array r \<noteq> addr_of_array s"
haftmann@37752
    43
haftmann@37752
    44
haftmann@37752
    45
subsection {* Monad operations *}
haftmann@37752
    46
haftmann@37752
    47
definition new :: "nat \<Rightarrow> 'a\<Colon>heap \<Rightarrow> 'a array Heap" where
haftmann@37752
    48
  [code del]: "new n x = Heap_Monad.heap (array (replicate n x))"
haftmann@37752
    49
haftmann@37752
    50
definition of_list :: "'a\<Colon>heap list \<Rightarrow> 'a array Heap" where
haftmann@37752
    51
  [code del]: "of_list xs = Heap_Monad.heap (array xs)"
haftmann@37752
    52
haftmann@37752
    53
definition make :: "nat \<Rightarrow> (nat \<Rightarrow> 'a\<Colon>heap) \<Rightarrow> 'a array Heap" where
haftmann@37752
    54
  [code del]: "make n f = Heap_Monad.heap (array (map f [0 ..< n]))"
haftmann@37752
    55
haftmann@37752
    56
definition len :: "'a\<Colon>heap array \<Rightarrow> nat Heap" where
haftmann@37802
    57
  [code del]: "len a = Heap_Monad.tap (\<lambda>h. length h a)"
haftmann@37752
    58
haftmann@37752
    59
definition nth :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a Heap" where
haftmann@37802
    60
  [code del]: "nth a i = Heap_Monad.guard (\<lambda>h. i < length h a)
haftmann@37752
    61
    (\<lambda>h. (get_array a h ! i, h))"
haftmann@37752
    62
haftmann@37752
    63
definition upd :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a\<Colon>heap array Heap" where
haftmann@37802
    64
  [code del]: "upd i x a = Heap_Monad.guard (\<lambda>h. i < length h a)
haftmann@37798
    65
    (\<lambda>h. (a, update a i x h))"
haftmann@37752
    66
haftmann@37752
    67
definition map_entry :: "nat \<Rightarrow> ('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a array \<Rightarrow> 'a array Heap" where
haftmann@37802
    68
  [code del]: "map_entry i f a = Heap_Monad.guard (\<lambda>h. i < length h a)
haftmann@37798
    69
    (\<lambda>h. (a, update a i (f (get_array a h ! i)) h))"
haftmann@37752
    70
haftmann@37752
    71
definition swap :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a Heap" where
haftmann@37802
    72
  [code del]: "swap i x a = Heap_Monad.guard (\<lambda>h. i < length h a)
haftmann@37798
    73
    (\<lambda>h. (get_array a h ! i, update a i x h))"
haftmann@37752
    74
haftmann@37752
    75
definition freeze :: "'a\<Colon>heap array \<Rightarrow> 'a list Heap" where
haftmann@37758
    76
  [code del]: "freeze a = Heap_Monad.tap (\<lambda>h. get_array a h)"
haftmann@37752
    77
haftmann@37752
    78
haftmann@37752
    79
subsection {* Properties *}
haftmann@37719
    80
haftmann@37719
    81
text {* FIXME: Does there exist a "canonical" array axiomatisation in
haftmann@37719
    82
the literature?  *}
haftmann@37719
    83
haftmann@37758
    84
text {* Primitives *}
haftmann@37758
    85
haftmann@37719
    86
lemma noteq_arrs_sym: "a =!!= b \<Longrightarrow> b =!!= a"
haftmann@37719
    87
  and unequal_arrs [simp]: "a \<noteq> a' \<longleftrightarrow> a =!!= a'"
haftmann@37719
    88
  unfolding noteq_arrs_def by auto
haftmann@37719
    89
haftmann@37719
    90
lemma noteq_arrs_irrefl: "r =!!= r \<Longrightarrow> False"
haftmann@37719
    91
  unfolding noteq_arrs_def by auto
haftmann@37719
    92
haftmann@37803
    93
lemma present_new_arr: "array_present h a \<Longrightarrow> a =!!= fst (array xs h)"
haftmann@37719
    94
  by (simp add: array_present_def noteq_arrs_def array_def Let_def)
haftmann@37719
    95
haftmann@37719
    96
lemma array_get_set_eq [simp]: "get_array r (set_array r x h) = x"
haftmann@37719
    97
  by (simp add: get_array_def set_array_def o_def)
haftmann@37719
    98
haftmann@37719
    99
lemma array_get_set_neq [simp]: "r =!!= s \<Longrightarrow> get_array r (set_array s x h) = get_array r h"
haftmann@37719
   100
  by (simp add: noteq_arrs_def get_array_def set_array_def)
haftmann@37719
   101
haftmann@37719
   102
lemma set_array_same [simp]:
haftmann@37719
   103
  "set_array r x (set_array r y h) = set_array r x h"
haftmann@37719
   104
  by (simp add: set_array_def)
haftmann@37719
   105
haftmann@37719
   106
lemma array_set_set_swap:
haftmann@37719
   107
  "r =!!= r' \<Longrightarrow> set_array r x (set_array r' x' h) = set_array r' x' (set_array r x h)"
haftmann@37719
   108
  by (simp add: Let_def expand_fun_eq noteq_arrs_def set_array_def)
haftmann@37719
   109
haftmann@37798
   110
lemma get_array_update_eq [simp]:
haftmann@37798
   111
  "get_array a (update a i v h) = (get_array a h) [i := v]"
haftmann@37798
   112
  by (simp add: update_def)
haftmann@37719
   113
haftmann@37798
   114
lemma nth_update_array_neq_array [simp]:
haftmann@37798
   115
  "a =!!= b \<Longrightarrow> get_array a (update b j v h) ! i = get_array a h ! i"
haftmann@37798
   116
  by (simp add: update_def noteq_arrs_def)
haftmann@37719
   117
haftmann@37798
   118
lemma get_arry_array_update_elem_neqIndex [simp]:
haftmann@37798
   119
  "i \<noteq> j \<Longrightarrow> get_array a (update a j v h) ! i = get_array a h ! i"
haftmann@37719
   120
  by simp
haftmann@37719
   121
haftmann@37798
   122
lemma length_update [simp]: 
haftmann@37802
   123
  "length (update b i v h) = length h"
haftmann@37802
   124
  by (simp add: update_def length_def set_array_def get_array_def expand_fun_eq)
haftmann@37719
   125
haftmann@37798
   126
lemma update_swap_neqArray:
haftmann@37719
   127
  "a =!!= a' \<Longrightarrow> 
haftmann@37798
   128
  update a i v (update a' i' v' h) 
haftmann@37798
   129
  = update a' i' v' (update a i v h)"
haftmann@37798
   130
apply (unfold update_def)
haftmann@37719
   131
apply simp
haftmann@37719
   132
apply (subst array_set_set_swap, assumption)
haftmann@37719
   133
apply (subst array_get_set_neq)
haftmann@37719
   134
apply (erule noteq_arrs_sym)
haftmann@37719
   135
apply (simp)
haftmann@37719
   136
done
haftmann@37719
   137
haftmann@37798
   138
lemma update_swap_neqIndex:
haftmann@37798
   139
  "\<lbrakk> i \<noteq> i' \<rbrakk> \<Longrightarrow> update a i v (update a i' v' h) = update a i' v' (update a i v h)"
haftmann@37798
   140
  by (auto simp add: update_def array_set_set_swap list_update_swap)
haftmann@37719
   141
haftmann@37719
   142
lemma get_array_init_array_list:
haftmann@37719
   143
  "get_array (fst (array ls h)) (snd (array ls' h)) = ls'"
haftmann@37719
   144
  by (simp add: Let_def split_def array_def)
haftmann@37719
   145
haftmann@37719
   146
lemma set_array:
haftmann@37719
   147
  "set_array (fst (array ls h))
haftmann@37719
   148
     new_ls (snd (array ls h))
haftmann@37719
   149
       = snd (array new_ls h)"
haftmann@37719
   150
  by (simp add: Let_def split_def array_def)
haftmann@37719
   151
haftmann@37798
   152
lemma array_present_update [simp]: 
haftmann@37803
   153
  "array_present (update b i v h) = array_present h"
haftmann@37803
   154
  by (simp add: update_def array_present_def set_array_def get_array_def expand_fun_eq)
haftmann@37719
   155
haftmann@37771
   156
lemma array_present_array [simp]:
haftmann@37803
   157
  "array_present (snd (array xs h)) (fst (array xs h))"
haftmann@37771
   158
  by (simp add: array_present_def array_def set_array_def Let_def)
haftmann@37771
   159
haftmann@37771
   160
lemma not_array_present_array [simp]:
haftmann@37803
   161
  "\<not> array_present h (fst (array xs h))"
haftmann@37771
   162
  by (simp add: array_present_def array_def Let_def)
haftmann@37771
   163
haftmann@37758
   164
haftmann@37758
   165
text {* Monad operations *}
haftmann@37758
   166
haftmann@37787
   167
lemma execute_new [execute_simps]:
haftmann@37758
   168
  "execute (new n x) h = Some (array (replicate n x) h)"
haftmann@37787
   169
  by (simp add: new_def execute_simps)
haftmann@37758
   170
haftmann@37787
   171
lemma success_newI [success_intros]:
haftmann@37758
   172
  "success (new n x) h"
haftmann@37787
   173
  by (auto intro: success_intros simp add: new_def)
haftmann@26170
   174
haftmann@37771
   175
lemma crel_newI [crel_intros]:
haftmann@37771
   176
  assumes "(a, h') = array (replicate n x) h"
haftmann@37771
   177
  shows "crel (new n x) h h' a"
haftmann@37787
   178
  by (rule crelI) (simp add: assms execute_simps)
haftmann@37771
   179
haftmann@37771
   180
lemma crel_newE [crel_elims]:
haftmann@37771
   181
  assumes "crel (new n x) h h' r"
haftmann@37771
   182
  obtains "r = fst (array (replicate n x) h)" "h' = snd (array (replicate n x) h)" 
haftmann@37803
   183
    "get_array r h' = replicate n x" "array_present h' r" "\<not> array_present h r"
haftmann@37787
   184
  using assms by (rule crelE) (simp add: get_array_init_array_list execute_simps)
haftmann@37771
   185
haftmann@37787
   186
lemma execute_of_list [execute_simps]:
haftmann@37758
   187
  "execute (of_list xs) h = Some (array xs h)"
haftmann@37787
   188
  by (simp add: of_list_def execute_simps)
haftmann@37758
   189
haftmann@37787
   190
lemma success_of_listI [success_intros]:
haftmann@37758
   191
  "success (of_list xs) h"
haftmann@37787
   192
  by (auto intro: success_intros simp add: of_list_def)
haftmann@26170
   193
haftmann@37771
   194
lemma crel_of_listI [crel_intros]:
haftmann@37771
   195
  assumes "(a, h') = array xs h"
haftmann@37771
   196
  shows "crel (of_list xs) h h' a"
haftmann@37787
   197
  by (rule crelI) (simp add: assms execute_simps)
haftmann@37771
   198
haftmann@37771
   199
lemma crel_of_listE [crel_elims]:
haftmann@37771
   200
  assumes "crel (of_list xs) h h' r"
haftmann@37771
   201
  obtains "r = fst (array xs h)" "h' = snd (array xs h)" 
haftmann@37803
   202
    "get_array r h' = xs" "array_present h' r" "\<not> array_present h r"
haftmann@37787
   203
  using assms by (rule crelE) (simp add: get_array_init_array_list execute_simps)
haftmann@37771
   204
haftmann@37787
   205
lemma execute_make [execute_simps]:
haftmann@37758
   206
  "execute (make n f) h = Some (array (map f [0 ..< n]) h)"
haftmann@37787
   207
  by (simp add: make_def execute_simps)
haftmann@26170
   208
haftmann@37787
   209
lemma success_makeI [success_intros]:
haftmann@37758
   210
  "success (make n f) h"
haftmann@37787
   211
  by (auto intro: success_intros simp add: make_def)
haftmann@37758
   212
haftmann@37771
   213
lemma crel_makeI [crel_intros]:
haftmann@37771
   214
  assumes "(a, h') = array (map f [0 ..< n]) h"
haftmann@37771
   215
  shows "crel (make n f) h h' a"
haftmann@37787
   216
  by (rule crelI) (simp add: assms execute_simps)
haftmann@37771
   217
haftmann@37771
   218
lemma crel_makeE [crel_elims]:
haftmann@37771
   219
  assumes "crel (make n f) h h' r"
haftmann@37771
   220
  obtains "r = fst (array (map f [0 ..< n]) h)" "h' = snd (array (map f [0 ..< n]) h)" 
haftmann@37803
   221
    "get_array r h' = map f [0 ..< n]" "array_present h' r" "\<not> array_present h r"
haftmann@37787
   222
  using assms by (rule crelE) (simp add: get_array_init_array_list execute_simps)
haftmann@37771
   223
haftmann@37787
   224
lemma execute_len [execute_simps]:
haftmann@37802
   225
  "execute (len a) h = Some (length h a, h)"
haftmann@37787
   226
  by (simp add: len_def execute_simps)
haftmann@37758
   227
haftmann@37787
   228
lemma success_lenI [success_intros]:
haftmann@37758
   229
  "success (len a) h"
haftmann@37787
   230
  by (auto intro: success_intros simp add: len_def)
haftmann@37752
   231
haftmann@37771
   232
lemma crel_lengthI [crel_intros]:
haftmann@37802
   233
  assumes "h' = h" "r = length h a"
haftmann@37771
   234
  shows "crel (len a) h h' r"
haftmann@37787
   235
  by (rule crelI) (simp add: assms execute_simps)
haftmann@37771
   236
haftmann@37771
   237
lemma crel_lengthE [crel_elims]:
haftmann@37771
   238
  assumes "crel (len a) h h' r"
haftmann@37802
   239
  obtains "r = length h' a" "h' = h" 
haftmann@37787
   240
  using assms by (rule crelE) (simp add: execute_simps)
haftmann@37771
   241
haftmann@37758
   242
lemma execute_nth [execute_simps]:
haftmann@37802
   243
  "i < length h a \<Longrightarrow>
haftmann@37758
   244
    execute (nth a i) h = Some (get_array a h ! i, h)"
haftmann@37802
   245
  "i \<ge> length h a \<Longrightarrow> execute (nth a i) h = None"
haftmann@37758
   246
  by (simp_all add: nth_def execute_simps)
haftmann@37758
   247
haftmann@37758
   248
lemma success_nthI [success_intros]:
haftmann@37802
   249
  "i < length h a \<Longrightarrow> success (nth a i) h"
haftmann@37758
   250
  by (auto intro: success_intros simp add: nth_def)
haftmann@26170
   251
haftmann@37771
   252
lemma crel_nthI [crel_intros]:
haftmann@37802
   253
  assumes "i < length h a" "h' = h" "r = get_array a h ! i"
haftmann@37771
   254
  shows "crel (nth a i) h h' r"
haftmann@37771
   255
  by (rule crelI) (insert assms, simp add: execute_simps)
haftmann@37771
   256
haftmann@37771
   257
lemma crel_nthE [crel_elims]:
haftmann@37771
   258
  assumes "crel (nth a i) h h' r"
haftmann@37802
   259
  obtains "i < length h a" "r = get_array a h ! i" "h' = h"
haftmann@37771
   260
  using assms by (rule crelE)
haftmann@37802
   261
    (erule successE, cases "i < length h a", simp_all add: execute_simps)
haftmann@37771
   262
haftmann@37758
   263
lemma execute_upd [execute_simps]:
haftmann@37802
   264
  "i < length h a \<Longrightarrow>
haftmann@37798
   265
    execute (upd i x a) h = Some (a, update a i x h)"
haftmann@37802
   266
  "i \<ge> length h a \<Longrightarrow> execute (upd i x a) h = None"
haftmann@37758
   267
  by (simp_all add: upd_def execute_simps)
haftmann@26170
   268
haftmann@37758
   269
lemma success_updI [success_intros]:
haftmann@37802
   270
  "i < length h a \<Longrightarrow> success (upd i x a) h"
haftmann@37758
   271
  by (auto intro: success_intros simp add: upd_def)
haftmann@37758
   272
haftmann@37771
   273
lemma crel_updI [crel_intros]:
haftmann@37802
   274
  assumes "i < length h a" "h' = update a i v h"
haftmann@37771
   275
  shows "crel (upd i v a) h h' a"
haftmann@37771
   276
  by (rule crelI) (insert assms, simp add: execute_simps)
haftmann@37771
   277
haftmann@37771
   278
lemma crel_updE [crel_elims]:
haftmann@37771
   279
  assumes "crel (upd i v a) h h' r"
haftmann@37802
   280
  obtains "r = a" "h' = update a i v h" "i < length h a"
haftmann@37771
   281
  using assms by (rule crelE)
haftmann@37802
   282
    (erule successE, cases "i < length h a", simp_all add: execute_simps)
haftmann@37771
   283
haftmann@37758
   284
lemma execute_map_entry [execute_simps]:
haftmann@37802
   285
  "i < length h a \<Longrightarrow>
haftmann@37758
   286
   execute (map_entry i f a) h =
haftmann@37798
   287
      Some (a, update a i (f (get_array a h ! i)) h)"
haftmann@37802
   288
  "i \<ge> length h a \<Longrightarrow> execute (map_entry i f a) h = None"
haftmann@37758
   289
  by (simp_all add: map_entry_def execute_simps)
haftmann@37752
   290
haftmann@37758
   291
lemma success_map_entryI [success_intros]:
haftmann@37802
   292
  "i < length h a \<Longrightarrow> success (map_entry i f a) h"
haftmann@37758
   293
  by (auto intro: success_intros simp add: map_entry_def)
haftmann@37758
   294
haftmann@37771
   295
lemma crel_map_entryI [crel_intros]:
haftmann@37802
   296
  assumes "i < length h a" "h' = update a i (f (get_array a h ! i)) h" "r = a"
haftmann@37771
   297
  shows "crel (map_entry i f a) h h' r"
haftmann@37771
   298
  by (rule crelI) (insert assms, simp add: execute_simps)
haftmann@37771
   299
haftmann@37771
   300
lemma crel_map_entryE [crel_elims]:
haftmann@37771
   301
  assumes "crel (map_entry i f a) h h' r"
haftmann@37802
   302
  obtains "r = a" "h' = update a i (f (get_array a h ! i)) h" "i < length h a"
haftmann@37771
   303
  using assms by (rule crelE)
haftmann@37802
   304
    (erule successE, cases "i < length h a", simp_all add: execute_simps)
haftmann@37771
   305
haftmann@37758
   306
lemma execute_swap [execute_simps]:
haftmann@37802
   307
  "i < length h a \<Longrightarrow>
haftmann@37758
   308
   execute (swap i x a) h =
haftmann@37798
   309
      Some (get_array a h ! i, update a i x h)"
haftmann@37802
   310
  "i \<ge> length h a \<Longrightarrow> execute (swap i x a) h = None"
haftmann@37758
   311
  by (simp_all add: swap_def execute_simps)
haftmann@37758
   312
haftmann@37758
   313
lemma success_swapI [success_intros]:
haftmann@37802
   314
  "i < length h a \<Longrightarrow> success (swap i x a) h"
haftmann@37758
   315
  by (auto intro: success_intros simp add: swap_def)
haftmann@37752
   316
haftmann@37771
   317
lemma crel_swapI [crel_intros]:
haftmann@37802
   318
  assumes "i < length h a" "h' = update a i x h" "r = get_array a h ! i"
haftmann@37771
   319
  shows "crel (swap i x a) h h' r"
haftmann@37771
   320
  by (rule crelI) (insert assms, simp add: execute_simps)
haftmann@37771
   321
haftmann@37771
   322
lemma crel_swapE [crel_elims]:
haftmann@37771
   323
  assumes "crel (swap i x a) h h' r"
haftmann@37802
   324
  obtains "r = get_array a h ! i" "h' = update a i x h" "i < length h a"
haftmann@37771
   325
  using assms by (rule crelE)
haftmann@37802
   326
    (erule successE, cases "i < length h a", simp_all add: execute_simps)
haftmann@37771
   327
haftmann@37787
   328
lemma execute_freeze [execute_simps]:
haftmann@37758
   329
  "execute (freeze a) h = Some (get_array a h, h)"
haftmann@37787
   330
  by (simp add: freeze_def execute_simps)
haftmann@37758
   331
haftmann@37787
   332
lemma success_freezeI [success_intros]:
haftmann@37758
   333
  "success (freeze a) h"
haftmann@37787
   334
  by (auto intro: success_intros simp add: freeze_def)
haftmann@26170
   335
haftmann@37771
   336
lemma crel_freezeI [crel_intros]:
haftmann@37771
   337
  assumes "h' = h" "r = get_array a h"
haftmann@37771
   338
  shows "crel (freeze a) h h' r"
haftmann@37771
   339
  by (rule crelI) (insert assms, simp add: execute_simps)
haftmann@37771
   340
haftmann@37771
   341
lemma crel_freezeE [crel_elims]:
haftmann@37771
   342
  assumes "crel (freeze a) h h' r"
haftmann@37771
   343
  obtains "h' = h" "r = get_array a h"
haftmann@37787
   344
  using assms by (rule crelE) (simp add: execute_simps)
haftmann@37771
   345
haftmann@26170
   346
lemma upd_return:
haftmann@26170
   347
  "upd i x a \<guillemotright> return a = upd i x a"
haftmann@37787
   348
  by (rule Heap_eqI) (simp add: bind_def guard_def upd_def execute_simps)
haftmann@26170
   349
haftmann@37752
   350
lemma array_make:
haftmann@37752
   351
  "new n x = make n (\<lambda>_. x)"
haftmann@37787
   352
  by (rule Heap_eqI) (simp add: map_replicate_trivial execute_simps)
haftmann@26170
   353
haftmann@37752
   354
lemma array_of_list_make:
haftmann@37752
   355
  "of_list xs = make (List.length xs) (\<lambda>n. xs ! n)"
haftmann@37787
   356
  by (rule Heap_eqI) (simp add: map_nth execute_simps)
haftmann@26170
   357
haftmann@37798
   358
hide_const (open) update new of_list make len nth upd map_entry swap freeze
haftmann@26170
   359
haftmann@26182
   360
haftmann@26182
   361
subsection {* Code generator setup *}
haftmann@26182
   362
haftmann@26182
   363
subsubsection {* Logical intermediate layer *}
haftmann@26182
   364
haftmann@26182
   365
definition new' where
haftmann@31205
   366
  [code del]: "new' = Array.new o Code_Numeral.nat_of"
haftmann@37752
   367
haftmann@28562
   368
lemma [code]:
haftmann@37752
   369
  "Array.new = new' o Code_Numeral.of_nat"
haftmann@26182
   370
  by (simp add: new'_def o_def)
haftmann@26182
   371
haftmann@26182
   372
definition of_list' where
haftmann@31205
   373
  [code del]: "of_list' i xs = Array.of_list (take (Code_Numeral.nat_of i) xs)"
haftmann@37752
   374
haftmann@28562
   375
lemma [code]:
haftmann@37752
   376
  "Array.of_list xs = of_list' (Code_Numeral.of_nat (List.length xs)) xs"
haftmann@26182
   377
  by (simp add: of_list'_def)
haftmann@26182
   378
haftmann@26182
   379
definition make' where
haftmann@31205
   380
  [code del]: "make' i f = Array.make (Code_Numeral.nat_of i) (f o Code_Numeral.of_nat)"
haftmann@37752
   381
haftmann@28562
   382
lemma [code]:
haftmann@37752
   383
  "Array.make n f = make' (Code_Numeral.of_nat n) (f o Code_Numeral.nat_of)"
haftmann@26182
   384
  by (simp add: make'_def o_def)
haftmann@26182
   385
haftmann@37719
   386
definition len' where
haftmann@37719
   387
  [code del]: "len' a = Array.len a \<guillemotright>= (\<lambda>n. return (Code_Numeral.of_nat n))"
haftmann@37752
   388
haftmann@28562
   389
lemma [code]:
haftmann@37752
   390
  "Array.len a = len' a \<guillemotright>= (\<lambda>i. return (Code_Numeral.nat_of i))"
haftmann@37719
   391
  by (simp add: len'_def)
haftmann@26182
   392
haftmann@26182
   393
definition nth' where
haftmann@31205
   394
  [code del]: "nth' a = Array.nth a o Code_Numeral.nat_of"
haftmann@37752
   395
haftmann@28562
   396
lemma [code]:
haftmann@37752
   397
  "Array.nth a n = nth' a (Code_Numeral.of_nat n)"
haftmann@26182
   398
  by (simp add: nth'_def)
haftmann@26182
   399
haftmann@26182
   400
definition upd' where
haftmann@31205
   401
  [code del]: "upd' a i x = Array.upd (Code_Numeral.nat_of i) x a \<guillemotright> return ()"
haftmann@37752
   402
haftmann@28562
   403
lemma [code]:
haftmann@37752
   404
  "Array.upd i x a = upd' a (Code_Numeral.of_nat i) x \<guillemotright> return a"
haftmann@37709
   405
  by (simp add: upd'_def upd_return)
haftmann@26182
   406
haftmann@37752
   407
lemma [code]:
haftmann@37798
   408
  "Array.map_entry i f a = do {
haftmann@37798
   409
     x \<leftarrow> Array.nth a i;
haftmann@37798
   410
     Array.upd i (f x) a
krauss@37792
   411
   }"
haftmann@37758
   412
  by (rule Heap_eqI) (simp add: bind_def guard_def map_entry_def execute_simps)
haftmann@26182
   413
haftmann@37752
   414
lemma [code]:
haftmann@37798
   415
  "Array.swap i x a = do {
haftmann@37798
   416
     y \<leftarrow> Array.nth a i;
haftmann@37798
   417
     Array.upd i x a;
haftmann@37752
   418
     return y
krauss@37792
   419
   }"
haftmann@37758
   420
  by (rule Heap_eqI) (simp add: bind_def guard_def swap_def execute_simps)
haftmann@37752
   421
haftmann@37752
   422
lemma [code]:
haftmann@37798
   423
  "Array.freeze a = do {
haftmann@37798
   424
     n \<leftarrow> Array.len a;
haftmann@37798
   425
     Heap_Monad.fold_map (\<lambda>i. Array.nth a i) [0..<n]
krauss@37792
   426
   }"
haftmann@37752
   427
proof (rule Heap_eqI)
haftmann@37752
   428
  fix h
haftmann@37752
   429
  have *: "List.map
haftmann@37802
   430
     (\<lambda>x. fst (the (if x < length h a
haftmann@37752
   431
                    then Some (get_array a h ! x, h) else None)))
haftmann@37802
   432
     [0..<length h a] =
haftmann@37802
   433
       List.map (List.nth (get_array a h)) [0..<length h a]"
haftmann@37752
   434
    by simp
haftmann@37802
   435
  have "execute (Heap_Monad.fold_map (Array.nth a) [0..<length h a]) h =
haftmann@37752
   436
    Some (get_array a h, h)"
haftmann@37756
   437
    apply (subst execute_fold_map_unchanged_heap)
haftmann@37752
   438
    apply (simp_all add: nth_def guard_def *)
haftmann@37752
   439
    apply (simp add: length_def map_nth)
haftmann@37752
   440
    done
krauss@37792
   441
  then have "execute (do {
haftmann@37798
   442
      n \<leftarrow> Array.len a;
haftmann@37756
   443
      Heap_Monad.fold_map (Array.nth a) [0..<n]
krauss@37792
   444
    }) h = Some (get_array a h, h)"
haftmann@37787
   445
    by (auto intro: execute_bind_eq_SomeI simp add: execute_simps)
haftmann@37798
   446
  then show "execute (Array.freeze a) h = execute (do {
haftmann@37798
   447
      n \<leftarrow> Array.len a;
haftmann@37756
   448
      Heap_Monad.fold_map (Array.nth a) [0..<n]
krauss@37792
   449
    }) h" by (simp add: execute_simps)
haftmann@37752
   450
qed
haftmann@37752
   451
haftmann@37752
   452
hide_const (open) new' of_list' make' len' nth' upd'
haftmann@37752
   453
haftmann@37752
   454
haftmann@37752
   455
text {* SML *}
haftmann@26182
   456
haftmann@26182
   457
code_type array (SML "_/ array")
haftmann@26182
   458
code_const Array (SML "raise/ (Fail/ \"bare Array\")")
haftmann@26752
   459
code_const Array.new' (SML "(fn/ ()/ =>/ Array.array/ ((_),/ (_)))")
haftmann@35846
   460
code_const Array.of_list' (SML "(fn/ ()/ =>/ Array.fromList/ _)")
haftmann@26752
   461
code_const Array.make' (SML "(fn/ ()/ =>/ Array.tabulate/ ((_),/ (_)))")
haftmann@37719
   462
code_const Array.len' (SML "(fn/ ()/ =>/ Array.length/ _)")
haftmann@26752
   463
code_const Array.nth' (SML "(fn/ ()/ =>/ Array.sub/ ((_),/ (_)))")
haftmann@26752
   464
code_const Array.upd' (SML "(fn/ ()/ =>/ Array.update/ ((_),/ (_),/ (_)))")
haftmann@26182
   465
haftmann@26182
   466
code_reserved SML Array
haftmann@26182
   467
haftmann@26182
   468
haftmann@37752
   469
text {* OCaml *}
haftmann@26182
   470
haftmann@26182
   471
code_type array (OCaml "_/ array")
haftmann@26182
   472
code_const Array (OCaml "failwith/ \"bare Array\"")
haftmann@32580
   473
code_const Array.new' (OCaml "(fun/ ()/ ->/ Array.make/ (Big'_int.int'_of'_big'_int/ _)/ _)")
haftmann@35846
   474
code_const Array.of_list' (OCaml "(fun/ ()/ ->/ Array.of'_list/ _)")
haftmann@37719
   475
code_const Array.len' (OCaml "(fun/ ()/ ->/ Big'_int.big'_int'_of'_int/ (Array.length/ _))")
haftmann@32580
   476
code_const Array.nth' (OCaml "(fun/ ()/ ->/ Array.get/ _/ (Big'_int.int'_of'_big'_int/ _))")
haftmann@32580
   477
code_const Array.upd' (OCaml "(fun/ ()/ ->/ Array.set/ _/ (Big'_int.int'_of'_big'_int/ _)/ _)")
haftmann@26182
   478
haftmann@26182
   479
code_reserved OCaml Array
haftmann@26182
   480
haftmann@26182
   481
haftmann@37752
   482
text {* Haskell *}
haftmann@26182
   483
haftmann@29793
   484
code_type array (Haskell "Heap.STArray/ Heap.RealWorld/ _")
haftmann@26182
   485
code_const Array (Haskell "error/ \"bare Array\"")
haftmann@29793
   486
code_const Array.new' (Haskell "Heap.newArray/ (0,/ _)")
haftmann@29793
   487
code_const Array.of_list' (Haskell "Heap.newListArray/ (0,/ _)")
haftmann@37719
   488
code_const Array.len' (Haskell "Heap.lengthArray")
haftmann@29793
   489
code_const Array.nth' (Haskell "Heap.readArray")
haftmann@29793
   490
code_const Array.upd' (Haskell "Heap.writeArray")
haftmann@26182
   491
haftmann@26170
   492
end