src/HOL/Imperative_HOL/Array.thy
 author huffman Sun Apr 01 16:09:58 2012 +0200 (2012-04-01) changeset 47255 30a1692557b0 parent 40671 5e46057ba8e0 child 48073 1b609a7837ef permissions -rw-r--r--
removed Nat_Numeral.thy, moving all theorems elsewhere
```     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 {* Primitives *}
```
```    12
```
```    13 definition present :: "heap \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> bool" where
```
```    14   "present h a \<longleftrightarrow> addr_of_array a < lim h"
```
```    15
```
```    16 definition get :: "heap \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a list" where
```
```    17   "get h a = map from_nat (arrays h (TYPEREP('a)) (addr_of_array a))"
```
```    18
```
```    19 definition set :: "'a\<Colon>heap array \<Rightarrow> 'a list \<Rightarrow> heap \<Rightarrow> heap" where
```
```    20   "set a x = arrays_update (\<lambda>h. h(TYPEREP('a) := ((h(TYPEREP('a))) (addr_of_array a:=map to_nat x))))"
```
```    21
```
```    22 definition alloc :: "'a list \<Rightarrow> heap \<Rightarrow> 'a\<Colon>heap array \<times> heap" where
```
```    23   "alloc xs h = (let
```
```    24      l = lim h;
```
```    25      r = Array l;
```
```    26      h'' = set r xs (h\<lparr>lim := l + 1\<rparr>)
```
```    27    in (r, h''))"
```
```    28
```
```    29 definition length :: "heap \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> nat" where
```
```    30   "length h a = List.length (get h a)"
```
```    31
```
```    32 definition update :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> heap \<Rightarrow> heap" where
```
```    33   "update a i x h = set a ((get h a)[i:=x]) h"
```
```    34
```
```    35 definition noteq :: "'a\<Colon>heap array \<Rightarrow> 'b\<Colon>heap array \<Rightarrow> bool" (infix "=!!=" 70) where
```
```    36   "r =!!= s \<longleftrightarrow> TYPEREP('a) \<noteq> TYPEREP('b) \<or> addr_of_array r \<noteq> addr_of_array s"
```
```    37
```
```    38
```
```    39 subsection {* Monad operations *}
```
```    40
```
```    41 definition new :: "nat \<Rightarrow> 'a\<Colon>heap \<Rightarrow> 'a array Heap" where
```
```    42   [code del]: "new n x = Heap_Monad.heap (alloc (replicate n x))"
```
```    43
```
```    44 definition of_list :: "'a\<Colon>heap list \<Rightarrow> 'a array Heap" where
```
```    45   [code del]: "of_list xs = Heap_Monad.heap (alloc xs)"
```
```    46
```
```    47 definition make :: "nat \<Rightarrow> (nat \<Rightarrow> 'a\<Colon>heap) \<Rightarrow> 'a array Heap" where
```
```    48   [code del]: "make n f = Heap_Monad.heap (alloc (map f [0 ..< n]))"
```
```    49
```
```    50 definition len :: "'a\<Colon>heap array \<Rightarrow> nat Heap" where
```
```    51   [code del]: "len a = Heap_Monad.tap (\<lambda>h. length h a)"
```
```    52
```
```    53 definition nth :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a Heap" where
```
```    54   [code del]: "nth a i = Heap_Monad.guard (\<lambda>h. i < length h a)
```
```    55     (\<lambda>h. (get h a ! i, h))"
```
```    56
```
```    57 definition upd :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a\<Colon>heap array Heap" where
```
```    58   [code del]: "upd i x a = Heap_Monad.guard (\<lambda>h. i < length h a)
```
```    59     (\<lambda>h. (a, update a i x h))"
```
```    60
```
```    61 definition map_entry :: "nat \<Rightarrow> ('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a array \<Rightarrow> 'a array Heap" where
```
```    62   [code del]: "map_entry i f a = Heap_Monad.guard (\<lambda>h. i < length h a)
```
```    63     (\<lambda>h. (a, update a i (f (get h a ! i)) h))"
```
```    64
```
```    65 definition swap :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a Heap" where
```
```    66   [code del]: "swap i x a = Heap_Monad.guard (\<lambda>h. i < length h a)
```
```    67     (\<lambda>h. (get h a ! i, update a i x h))"
```
```    68
```
```    69 definition freeze :: "'a\<Colon>heap array \<Rightarrow> 'a list Heap" where
```
```    70   [code del]: "freeze a = Heap_Monad.tap (\<lambda>h. get h a)"
```
```    71
```
```    72
```
```    73 subsection {* Properties *}
```
```    74
```
```    75 text {* FIXME: Does there exist a "canonical" array axiomatisation in
```
```    76 the literature?  *}
```
```    77
```
```    78 text {* Primitives *}
```
```    79
```
```    80 lemma noteq_sym: "a =!!= b \<Longrightarrow> b =!!= a"
```
```    81   and unequal [simp]: "a \<noteq> a' \<longleftrightarrow> a =!!= a'"
```
```    82   unfolding noteq_def by auto
```
```    83
```
```    84 lemma noteq_irrefl: "r =!!= r \<Longrightarrow> False"
```
```    85   unfolding noteq_def by auto
```
```    86
```
```    87 lemma present_alloc_noteq: "present h a \<Longrightarrow> a =!!= fst (alloc xs h)"
```
```    88   by (simp add: present_def noteq_def alloc_def Let_def)
```
```    89
```
```    90 lemma get_set_eq [simp]: "get (set r x h) r = x"
```
```    91   by (simp add: get_def set_def o_def)
```
```    92
```
```    93 lemma get_set_neq [simp]: "r =!!= s \<Longrightarrow> get (set s x h) r = get h r"
```
```    94   by (simp add: noteq_def get_def set_def)
```
```    95
```
```    96 lemma set_same [simp]:
```
```    97   "set r x (set r y h) = set r x h"
```
```    98   by (simp add: set_def)
```
```    99
```
```   100 lemma set_set_swap:
```
```   101   "r =!!= r' \<Longrightarrow> set r x (set r' x' h) = set r' x' (set r x h)"
```
```   102   by (simp add: Let_def fun_eq_iff noteq_def set_def)
```
```   103
```
```   104 lemma get_update_eq [simp]:
```
```   105   "get (update a i v h) a = (get h a) [i := v]"
```
```   106   by (simp add: update_def)
```
```   107
```
```   108 lemma nth_update_neq [simp]:
```
```   109   "a =!!= b \<Longrightarrow> get (update b j v h) a ! i = get h a ! i"
```
```   110   by (simp add: update_def noteq_def)
```
```   111
```
```   112 lemma get_update_elem_neqIndex [simp]:
```
```   113   "i \<noteq> j \<Longrightarrow> get (update a j v h) a ! i = get h a ! i"
```
```   114   by simp
```
```   115
```
```   116 lemma length_update [simp]:
```
```   117   "length (update b i v h) = length h"
```
```   118   by (simp add: update_def length_def set_def get_def fun_eq_iff)
```
```   119
```
```   120 lemma update_swap_neq:
```
```   121   "a =!!= a' \<Longrightarrow>
```
```   122   update a i v (update a' i' v' h)
```
```   123   = update a' i' v' (update a i v h)"
```
```   124 apply (unfold update_def)
```
```   125 apply simp
```
```   126 apply (subst set_set_swap, assumption)
```
```   127 apply (subst get_set_neq)
```
```   128 apply (erule noteq_sym)
```
```   129 apply simp
```
```   130 done
```
```   131
```
```   132 lemma update_swap_neqIndex:
```
```   133   "\<lbrakk> i \<noteq> i' \<rbrakk> \<Longrightarrow> update a i v (update a i' v' h) = update a i' v' (update a i v h)"
```
```   134   by (auto simp add: update_def set_set_swap list_update_swap)
```
```   135
```
```   136 lemma get_alloc:
```
```   137   "get (snd (alloc xs h)) (fst (alloc ys h)) = xs"
```
```   138   by (simp add: Let_def split_def alloc_def)
```
```   139
```
```   140 lemma length_alloc:
```
```   141   "length (snd (alloc (xs :: 'a::heap list) h)) (fst (alloc (ys :: 'a list) h)) = List.length xs"
```
```   142   by (simp add: Array.length_def get_alloc)
```
```   143
```
```   144 lemma set:
```
```   145   "set (fst (alloc ls h))
```
```   146      new_ls (snd (alloc ls h))
```
```   147        = snd (alloc new_ls h)"
```
```   148   by (simp add: Let_def split_def alloc_def)
```
```   149
```
```   150 lemma present_update [simp]:
```
```   151   "present (update b i v h) = present h"
```
```   152   by (simp add: update_def present_def set_def get_def fun_eq_iff)
```
```   153
```
```   154 lemma present_alloc [simp]:
```
```   155   "present (snd (alloc xs h)) (fst (alloc xs h))"
```
```   156   by (simp add: present_def alloc_def set_def Let_def)
```
```   157
```
```   158 lemma not_present_alloc [simp]:
```
```   159   "\<not> present h (fst (alloc xs h))"
```
```   160   by (simp add: present_def alloc_def Let_def)
```
```   161
```
```   162
```
```   163 text {* Monad operations *}
```
```   164
```
```   165 lemma execute_new [execute_simps]:
```
```   166   "execute (new n x) h = Some (alloc (replicate n x) h)"
```
```   167   by (simp add: new_def execute_simps)
```
```   168
```
```   169 lemma success_newI [success_intros]:
```
```   170   "success (new n x) h"
```
```   171   by (auto intro: success_intros simp add: new_def)
```
```   172
```
```   173 lemma effect_newI [effect_intros]:
```
```   174   assumes "(a, h') = alloc (replicate n x) h"
```
```   175   shows "effect (new n x) h h' a"
```
```   176   by (rule effectI) (simp add: assms execute_simps)
```
```   177
```
```   178 lemma effect_newE [effect_elims]:
```
```   179   assumes "effect (new n x) h h' r"
```
```   180   obtains "r = fst (alloc (replicate n x) h)" "h' = snd (alloc (replicate n x) h)"
```
```   181     "get h' r = replicate n x" "present h' r" "\<not> present h r"
```
```   182   using assms by (rule effectE) (simp add: get_alloc execute_simps)
```
```   183
```
```   184 lemma execute_of_list [execute_simps]:
```
```   185   "execute (of_list xs) h = Some (alloc xs h)"
```
```   186   by (simp add: of_list_def execute_simps)
```
```   187
```
```   188 lemma success_of_listI [success_intros]:
```
```   189   "success (of_list xs) h"
```
```   190   by (auto intro: success_intros simp add: of_list_def)
```
```   191
```
```   192 lemma effect_of_listI [effect_intros]:
```
```   193   assumes "(a, h') = alloc xs h"
```
```   194   shows "effect (of_list xs) h h' a"
```
```   195   by (rule effectI) (simp add: assms execute_simps)
```
```   196
```
```   197 lemma effect_of_listE [effect_elims]:
```
```   198   assumes "effect (of_list xs) h h' r"
```
```   199   obtains "r = fst (alloc xs h)" "h' = snd (alloc xs h)"
```
```   200     "get h' r = xs" "present h' r" "\<not> present h r"
```
```   201   using assms by (rule effectE) (simp add: get_alloc execute_simps)
```
```   202
```
```   203 lemma execute_make [execute_simps]:
```
```   204   "execute (make n f) h = Some (alloc (map f [0 ..< n]) h)"
```
```   205   by (simp add: make_def execute_simps)
```
```   206
```
```   207 lemma success_makeI [success_intros]:
```
```   208   "success (make n f) h"
```
```   209   by (auto intro: success_intros simp add: make_def)
```
```   210
```
```   211 lemma effect_makeI [effect_intros]:
```
```   212   assumes "(a, h') = alloc (map f [0 ..< n]) h"
```
```   213   shows "effect (make n f) h h' a"
```
```   214   by (rule effectI) (simp add: assms execute_simps)
```
```   215
```
```   216 lemma effect_makeE [effect_elims]:
```
```   217   assumes "effect (make n f) h h' r"
```
```   218   obtains "r = fst (alloc (map f [0 ..< n]) h)" "h' = snd (alloc (map f [0 ..< n]) h)"
```
```   219     "get h' r = map f [0 ..< n]" "present h' r" "\<not> present h r"
```
```   220   using assms by (rule effectE) (simp add: get_alloc execute_simps)
```
```   221
```
```   222 lemma execute_len [execute_simps]:
```
```   223   "execute (len a) h = Some (length h a, h)"
```
```   224   by (simp add: len_def execute_simps)
```
```   225
```
```   226 lemma success_lenI [success_intros]:
```
```   227   "success (len a) h"
```
```   228   by (auto intro: success_intros simp add: len_def)
```
```   229
```
```   230 lemma effect_lengthI [effect_intros]:
```
```   231   assumes "h' = h" "r = length h a"
```
```   232   shows "effect (len a) h h' r"
```
```   233   by (rule effectI) (simp add: assms execute_simps)
```
```   234
```
```   235 lemma effect_lengthE [effect_elims]:
```
```   236   assumes "effect (len a) h h' r"
```
```   237   obtains "r = length h' a" "h' = h"
```
```   238   using assms by (rule effectE) (simp add: execute_simps)
```
```   239
```
```   240 lemma execute_nth [execute_simps]:
```
```   241   "i < length h a \<Longrightarrow>
```
```   242     execute (nth a i) h = Some (get h a ! i, h)"
```
```   243   "i \<ge> length h a \<Longrightarrow> execute (nth a i) h = None"
```
```   244   by (simp_all add: nth_def execute_simps)
```
```   245
```
```   246 lemma success_nthI [success_intros]:
```
```   247   "i < length h a \<Longrightarrow> success (nth a i) h"
```
```   248   by (auto intro: success_intros simp add: nth_def)
```
```   249
```
```   250 lemma effect_nthI [effect_intros]:
```
```   251   assumes "i < length h a" "h' = h" "r = get h a ! i"
```
```   252   shows "effect (nth a i) h h' r"
```
```   253   by (rule effectI) (insert assms, simp add: execute_simps)
```
```   254
```
```   255 lemma effect_nthE [effect_elims]:
```
```   256   assumes "effect (nth a i) h h' r"
```
```   257   obtains "i < length h a" "r = get h a ! i" "h' = h"
```
```   258   using assms by (rule effectE)
```
```   259     (erule successE, cases "i < length h a", simp_all add: execute_simps)
```
```   260
```
```   261 lemma execute_upd [execute_simps]:
```
```   262   "i < length h a \<Longrightarrow>
```
```   263     execute (upd i x a) h = Some (a, update a i x h)"
```
```   264   "i \<ge> length h a \<Longrightarrow> execute (upd i x a) h = None"
```
```   265   by (simp_all add: upd_def execute_simps)
```
```   266
```
```   267 lemma success_updI [success_intros]:
```
```   268   "i < length h a \<Longrightarrow> success (upd i x a) h"
```
```   269   by (auto intro: success_intros simp add: upd_def)
```
```   270
```
```   271 lemma effect_updI [effect_intros]:
```
```   272   assumes "i < length h a" "h' = update a i v h"
```
```   273   shows "effect (upd i v a) h h' a"
```
```   274   by (rule effectI) (insert assms, simp add: execute_simps)
```
```   275
```
```   276 lemma effect_updE [effect_elims]:
```
```   277   assumes "effect (upd i v a) h h' r"
```
```   278   obtains "r = a" "h' = update a i v h" "i < length h a"
```
```   279   using assms by (rule effectE)
```
```   280     (erule successE, cases "i < length h a", simp_all add: execute_simps)
```
```   281
```
```   282 lemma execute_map_entry [execute_simps]:
```
```   283   "i < length h a \<Longrightarrow>
```
```   284    execute (map_entry i f a) h =
```
```   285       Some (a, update a i (f (get h a ! i)) h)"
```
```   286   "i \<ge> length h a \<Longrightarrow> execute (map_entry i f a) h = None"
```
```   287   by (simp_all add: map_entry_def execute_simps)
```
```   288
```
```   289 lemma success_map_entryI [success_intros]:
```
```   290   "i < length h a \<Longrightarrow> success (map_entry i f a) h"
```
```   291   by (auto intro: success_intros simp add: map_entry_def)
```
```   292
```
```   293 lemma effect_map_entryI [effect_intros]:
```
```   294   assumes "i < length h a" "h' = update a i (f (get h a ! i)) h" "r = a"
```
```   295   shows "effect (map_entry i f a) h h' r"
```
```   296   by (rule effectI) (insert assms, simp add: execute_simps)
```
```   297
```
```   298 lemma effect_map_entryE [effect_elims]:
```
```   299   assumes "effect (map_entry i f a) h h' r"
```
```   300   obtains "r = a" "h' = update a i (f (get h a ! i)) h" "i < length h a"
```
```   301   using assms by (rule effectE)
```
```   302     (erule successE, cases "i < length h a", simp_all add: execute_simps)
```
```   303
```
```   304 lemma execute_swap [execute_simps]:
```
```   305   "i < length h a \<Longrightarrow>
```
```   306    execute (swap i x a) h =
```
```   307       Some (get h a ! i, update a i x h)"
```
```   308   "i \<ge> length h a \<Longrightarrow> execute (swap i x a) h = None"
```
```   309   by (simp_all add: swap_def execute_simps)
```
```   310
```
```   311 lemma success_swapI [success_intros]:
```
```   312   "i < length h a \<Longrightarrow> success (swap i x a) h"
```
```   313   by (auto intro: success_intros simp add: swap_def)
```
```   314
```
```   315 lemma effect_swapI [effect_intros]:
```
```   316   assumes "i < length h a" "h' = update a i x h" "r = get h a ! i"
```
```   317   shows "effect (swap i x a) h h' r"
```
```   318   by (rule effectI) (insert assms, simp add: execute_simps)
```
```   319
```
```   320 lemma effect_swapE [effect_elims]:
```
```   321   assumes "effect (swap i x a) h h' r"
```
```   322   obtains "r = get h a ! i" "h' = update a i x h" "i < length h a"
```
```   323   using assms by (rule effectE)
```
```   324     (erule successE, cases "i < length h a", simp_all add: execute_simps)
```
```   325
```
```   326 lemma execute_freeze [execute_simps]:
```
```   327   "execute (freeze a) h = Some (get h a, h)"
```
```   328   by (simp add: freeze_def execute_simps)
```
```   329
```
```   330 lemma success_freezeI [success_intros]:
```
```   331   "success (freeze a) h"
```
```   332   by (auto intro: success_intros simp add: freeze_def)
```
```   333
```
```   334 lemma effect_freezeI [effect_intros]:
```
```   335   assumes "h' = h" "r = get h a"
```
```   336   shows "effect (freeze a) h h' r"
```
```   337   by (rule effectI) (insert assms, simp add: execute_simps)
```
```   338
```
```   339 lemma effect_freezeE [effect_elims]:
```
```   340   assumes "effect (freeze a) h h' r"
```
```   341   obtains "h' = h" "r = get h a"
```
```   342   using assms by (rule effectE) (simp add: execute_simps)
```
```   343
```
```   344 lemma upd_return:
```
```   345   "upd i x a \<guillemotright> return a = upd i x a"
```
```   346   by (rule Heap_eqI) (simp add: bind_def guard_def upd_def execute_simps)
```
```   347
```
```   348 lemma array_make:
```
```   349   "new n x = make n (\<lambda>_. x)"
```
```   350   by (rule Heap_eqI) (simp add: map_replicate_trivial execute_simps)
```
```   351
```
```   352 lemma array_of_list_make [code]:
```
```   353   "of_list xs = make (List.length xs) (\<lambda>n. xs ! n)"
```
```   354   by (rule Heap_eqI) (simp add: map_nth execute_simps)
```
```   355
```
```   356 hide_const (open) present get set alloc length update noteq new of_list make len nth upd map_entry swap freeze
```
```   357
```
```   358
```
```   359 subsection {* Code generator setup *}
```
```   360
```
```   361 subsubsection {* Logical intermediate layer *}
```
```   362
```
```   363 definition new' where
```
```   364   [code del]: "new' = Array.new o Code_Numeral.nat_of"
```
```   365
```
```   366 lemma [code]:
```
```   367   "Array.new = new' o Code_Numeral.of_nat"
```
```   368   by (simp add: new'_def o_def)
```
```   369
```
```   370 definition make' where
```
```   371   [code del]: "make' i f = Array.make (Code_Numeral.nat_of i) (f o Code_Numeral.of_nat)"
```
```   372
```
```   373 lemma [code]:
```
```   374   "Array.make n f = make' (Code_Numeral.of_nat n) (f o Code_Numeral.nat_of)"
```
```   375   by (simp add: make'_def o_def)
```
```   376
```
```   377 definition len' where
```
```   378   [code del]: "len' a = Array.len a \<guillemotright>= (\<lambda>n. return (Code_Numeral.of_nat n))"
```
```   379
```
```   380 lemma [code]:
```
```   381   "Array.len a = len' a \<guillemotright>= (\<lambda>i. return (Code_Numeral.nat_of i))"
```
```   382   by (simp add: len'_def)
```
```   383
```
```   384 definition nth' where
```
```   385   [code del]: "nth' a = Array.nth a o Code_Numeral.nat_of"
```
```   386
```
```   387 lemma [code]:
```
```   388   "Array.nth a n = nth' a (Code_Numeral.of_nat n)"
```
```   389   by (simp add: nth'_def)
```
```   390
```
```   391 definition upd' where
```
```   392   [code del]: "upd' a i x = Array.upd (Code_Numeral.nat_of i) x a \<guillemotright> return ()"
```
```   393
```
```   394 lemma [code]:
```
```   395   "Array.upd i x a = upd' a (Code_Numeral.of_nat i) x \<guillemotright> return a"
```
```   396   by (simp add: upd'_def upd_return)
```
```   397
```
```   398 lemma [code]:
```
```   399   "Array.map_entry i f a = do {
```
```   400      x \<leftarrow> Array.nth a i;
```
```   401      Array.upd i (f x) a
```
```   402    }"
```
```   403   by (rule Heap_eqI) (simp add: bind_def guard_def map_entry_def execute_simps)
```
```   404
```
```   405 lemma [code]:
```
```   406   "Array.swap i x a = do {
```
```   407      y \<leftarrow> Array.nth a i;
```
```   408      Array.upd i x a;
```
```   409      return y
```
```   410    }"
```
```   411   by (rule Heap_eqI) (simp add: bind_def guard_def swap_def execute_simps)
```
```   412
```
```   413 lemma [code]:
```
```   414   "Array.freeze a = do {
```
```   415      n \<leftarrow> Array.len a;
```
```   416      Heap_Monad.fold_map (\<lambda>i. Array.nth a i) [0..<n]
```
```   417    }"
```
```   418 proof (rule Heap_eqI)
```
```   419   fix h
```
```   420   have *: "List.map
```
```   421      (\<lambda>x. fst (the (if x < Array.length h a
```
```   422                     then Some (Array.get h a ! x, h) else None)))
```
```   423      [0..<Array.length h a] =
```
```   424        List.map (List.nth (Array.get h a)) [0..<Array.length h a]"
```
```   425     by simp
```
```   426   have "execute (Heap_Monad.fold_map (Array.nth a) [0..<Array.length h a]) h =
```
```   427     Some (Array.get h a, h)"
```
```   428     apply (subst execute_fold_map_unchanged_heap)
```
```   429     apply (simp_all add: nth_def guard_def *)
```
```   430     apply (simp add: length_def map_nth)
```
```   431     done
```
```   432   then have "execute (do {
```
```   433       n \<leftarrow> Array.len a;
```
```   434       Heap_Monad.fold_map (Array.nth a) [0..<n]
```
```   435     }) h = Some (Array.get h a, h)"
```
```   436     by (auto intro: execute_bind_eq_SomeI simp add: execute_simps)
```
```   437   then show "execute (Array.freeze a) h = execute (do {
```
```   438       n \<leftarrow> Array.len a;
```
```   439       Heap_Monad.fold_map (Array.nth a) [0..<n]
```
```   440     }) h" by (simp add: execute_simps)
```
```   441 qed
```
```   442
```
```   443 hide_const (open) new' make' len' nth' upd'
```
```   444
```
```   445
```
```   446 text {* SML *}
```
```   447
```
```   448 code_type array (SML "_/ array")
```
```   449 code_const Array (SML "raise/ (Fail/ \"bare Array\")")
```
```   450 code_const Array.new' (SML "(fn/ ()/ =>/ Array.array/ ((_),/ (_)))")
```
```   451 code_const Array.of_list (SML "(fn/ ()/ =>/ Array.fromList/ _)")
```
```   452 code_const Array.make' (SML "(fn/ ()/ =>/ Array.tabulate/ ((_),/ (_)))")
```
```   453 code_const Array.len' (SML "(fn/ ()/ =>/ Array.length/ _)")
```
```   454 code_const Array.nth' (SML "(fn/ ()/ =>/ Array.sub/ ((_),/ (_)))")
```
```   455 code_const Array.upd' (SML "(fn/ ()/ =>/ Array.update/ ((_),/ (_),/ (_)))")
```
```   456 code_const "HOL.equal :: 'a array \<Rightarrow> 'a array \<Rightarrow> bool" (SML infixl 6 "=")
```
```   457
```
```   458 code_reserved SML Array
```
```   459
```
```   460
```
```   461 text {* OCaml *}
```
```   462
```
```   463 code_type array (OCaml "_/ array")
```
```   464 code_const Array (OCaml "failwith/ \"bare Array\"")
```
```   465 code_const Array.new' (OCaml "(fun/ ()/ ->/ Array.make/ (Big'_int.int'_of'_big'_int/ _)/ _)")
```
```   466 code_const Array.of_list (OCaml "(fun/ ()/ ->/ Array.of'_list/ _)")
```
```   467 code_const Array.make' (OCaml "(fun/ ()/ ->/ Array.init/ (Big'_int.int'_of'_big'_int/ _)/
```
```   468   (fun k'_ ->/ _/ (Big'_int.big'_int'_of'_int/ k'_)))")
```
```   469 code_const Array.len' (OCaml "(fun/ ()/ ->/ Big'_int.big'_int'_of'_int/ (Array.length/ _))")
```
```   470 code_const Array.nth' (OCaml "(fun/ ()/ ->/ Array.get/ _/ (Big'_int.int'_of'_big'_int/ _))")
```
```   471 code_const Array.upd' (OCaml "(fun/ ()/ ->/ Array.set/ _/ (Big'_int.int'_of'_big'_int/ _)/ _)")
```
```   472 code_const "HOL.equal :: 'a array \<Rightarrow> 'a array \<Rightarrow> bool" (OCaml infixl 4 "=")
```
```   473
```
```   474 code_reserved OCaml Array
```
```   475
```
```   476
```
```   477 text {* Haskell *}
```
```   478
```
```   479 code_type array (Haskell "Heap.STArray/ Heap.RealWorld/ _")
```
```   480 code_const Array (Haskell "error/ \"bare Array\"")
```
```   481 code_const Array.new' (Haskell "Heap.newArray")
```
```   482 code_const Array.of_list (Haskell "Heap.newListArray")
```
```   483 code_const Array.make' (Haskell "Heap.newFunArray")
```
```   484 code_const Array.len' (Haskell "Heap.lengthArray")
```
```   485 code_const Array.nth' (Haskell "Heap.readArray")
```
```   486 code_const Array.upd' (Haskell "Heap.writeArray")
```
```   487 code_const "HOL.equal :: 'a array \<Rightarrow> 'a array \<Rightarrow> bool" (Haskell infix 4 "==")
```
```   488 code_instance array :: HOL.equal (Haskell -)
```
```   489
```
```   490
```
```   491 text {* Scala *}
```
```   492
```
```   493 code_type array (Scala "!collection.mutable.ArraySeq[_]")
```
```   494 code_const Array (Scala "!error(\"bare Array\")")
```
```   495 code_const Array.new' (Scala "('_: Unit)/ => / Array.alloc((_))((_))")
```
```   496 code_const Array.make' (Scala "('_: Unit)/ =>/ Array.make((_))((_))")
```
```   497 code_const Array.len' (Scala "('_: Unit)/ =>/ Array.len((_))")
```
```   498 code_const Array.nth' (Scala "('_: Unit)/ =>/ Array.nth((_), (_))")
```
```   499 code_const Array.upd' (Scala "('_: Unit)/ =>/ Array.upd((_), (_), (_))")
```
```   500 code_const Array.freeze (Scala "('_: Unit)/ =>/ Array.freeze((_))")
```
```   501 code_const "HOL.equal :: 'a array \<Rightarrow> 'a array \<Rightarrow> bool" (Scala infixl 5 "==")
```
```   502
```
```   503 end
```