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