src/HOL/Imperative_HOL/Array.thy
author haftmann
Mon Nov 22 09:37:39 2010 +0100 (2010-11-22)
changeset 40671 5e46057ba8e0
parent 40360 1a73b5b90a3c
child 48073 1b609a7837ef
permissions -rw-r--r--
renamed slightly ambivalent crel to effect
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@37804
    13
definition present :: "heap \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> bool" where
haftmann@37804
    14
  "present h a \<longleftrightarrow> addr_of_array a < lim h"
haftmann@37719
    15
haftmann@37806
    16
definition get :: "heap \<Rightarrow> 'a\<Colon>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
haftmann@37804
    19
definition set :: "'a\<Colon>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
haftmann@37804
    22
definition alloc :: "'a list \<Rightarrow> heap \<Rightarrow> 'a\<Colon>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
haftmann@37802
    29
definition length :: "heap \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> nat" where
haftmann@37806
    30
  "length h a = List.length (get h a)"
haftmann@37719
    31
  
haftmann@37798
    32
definition update :: "'a\<Colon>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
haftmann@37804
    35
definition noteq :: "'a\<Colon>heap array \<Rightarrow> 'b\<Colon>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
haftmann@37752
    39
subsection {* Monad operations *}
haftmann@37752
    40
haftmann@37752
    41
definition new :: "nat \<Rightarrow> 'a\<Colon>heap \<Rightarrow> 'a array Heap" where
haftmann@37804
    42
  [code del]: "new n x = Heap_Monad.heap (alloc (replicate n x))"
haftmann@37752
    43
haftmann@37752
    44
definition of_list :: "'a\<Colon>heap list \<Rightarrow> 'a array Heap" where
haftmann@37804
    45
  [code del]: "of_list xs = Heap_Monad.heap (alloc xs)"
haftmann@37752
    46
haftmann@37752
    47
definition make :: "nat \<Rightarrow> (nat \<Rightarrow> 'a\<Colon>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
haftmann@37752
    50
definition len :: "'a\<Colon>heap array \<Rightarrow> nat Heap" where
haftmann@37802
    51
  [code del]: "len a = Heap_Monad.tap (\<lambda>h. length h a)"
haftmann@37752
    52
haftmann@37752
    53
definition nth :: "'a\<Colon>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
haftmann@37752
    57
definition upd :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a\<Colon>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
haftmann@37752
    61
definition map_entry :: "nat \<Rightarrow> ('a\<Colon>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
haftmann@37752
    65
definition swap :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>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
haftmann@37752
    69
definition freeze :: "'a\<Colon>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
haftmann@37752
    73
subsection {* Properties *}
haftmann@37719
    74
haftmann@37719
    75
text {* FIXME: Does there exist a "canonical" array axiomatisation in
haftmann@37719
    76
the literature?  *}
haftmann@37719
    77
haftmann@37758
    78
text {* Primitives *}
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
haftmann@37758
   163
text {* Monad operations *}
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@40671
   258
  using assms by (rule effectE)
haftmann@37802
   259
    (erule successE, cases "i < length h a", simp_all add: execute_simps)
haftmann@37771
   260
haftmann@37758
   261
lemma execute_upd [execute_simps]:
haftmann@37802
   262
  "i < length h a \<Longrightarrow>
haftmann@37798
   263
    execute (upd i x a) h = Some (a, update a i x h)"
haftmann@37802
   264
  "i \<ge> length h a \<Longrightarrow> execute (upd i x a) h = None"
haftmann@37758
   265
  by (simp_all add: upd_def execute_simps)
haftmann@26170
   266
haftmann@37758
   267
lemma success_updI [success_intros]:
haftmann@37802
   268
  "i < length h a \<Longrightarrow> success (upd i x a) h"
haftmann@37758
   269
  by (auto intro: success_intros simp add: upd_def)
haftmann@37758
   270
haftmann@40671
   271
lemma effect_updI [effect_intros]:
haftmann@37802
   272
  assumes "i < length h a" "h' = update a i v h"
haftmann@40671
   273
  shows "effect (upd i v a) h h' a"
haftmann@40671
   274
  by (rule effectI) (insert assms, simp add: execute_simps)
haftmann@37771
   275
haftmann@40671
   276
lemma effect_updE [effect_elims]:
haftmann@40671
   277
  assumes "effect (upd i v a) h h' r"
haftmann@37802
   278
  obtains "r = a" "h' = update a i v h" "i < length h a"
haftmann@40671
   279
  using assms by (rule effectE)
haftmann@37802
   280
    (erule successE, cases "i < length h a", simp_all add: execute_simps)
haftmann@37771
   281
haftmann@37758
   282
lemma execute_map_entry [execute_simps]:
haftmann@37802
   283
  "i < length h a \<Longrightarrow>
haftmann@37758
   284
   execute (map_entry i f a) h =
haftmann@37806
   285
      Some (a, update a i (f (get h a ! i)) h)"
haftmann@37802
   286
  "i \<ge> length h a \<Longrightarrow> execute (map_entry i f a) h = None"
haftmann@37758
   287
  by (simp_all add: map_entry_def execute_simps)
haftmann@37752
   288
haftmann@37758
   289
lemma success_map_entryI [success_intros]:
haftmann@37802
   290
  "i < length h a \<Longrightarrow> success (map_entry i f a) h"
haftmann@37758
   291
  by (auto intro: success_intros simp add: map_entry_def)
haftmann@37758
   292
haftmann@40671
   293
lemma effect_map_entryI [effect_intros]:
haftmann@37806
   294
  assumes "i < length h a" "h' = update a i (f (get h a ! i)) h" "r = a"
haftmann@40671
   295
  shows "effect (map_entry i f a) h h' r"
haftmann@40671
   296
  by (rule effectI) (insert assms, simp add: execute_simps)
haftmann@37771
   297
haftmann@40671
   298
lemma effect_map_entryE [effect_elims]:
haftmann@40671
   299
  assumes "effect (map_entry i f a) h h' r"
haftmann@37806
   300
  obtains "r = a" "h' = update a i (f (get h a ! i)) h" "i < length h a"
haftmann@40671
   301
  using assms by (rule effectE)
haftmann@37802
   302
    (erule successE, cases "i < length h a", simp_all add: execute_simps)
haftmann@37771
   303
haftmann@37758
   304
lemma execute_swap [execute_simps]:
haftmann@37802
   305
  "i < length h a \<Longrightarrow>
haftmann@37758
   306
   execute (swap i x a) h =
haftmann@37806
   307
      Some (get h a ! i, update a i x h)"
haftmann@37802
   308
  "i \<ge> length h a \<Longrightarrow> execute (swap i x a) h = None"
haftmann@37758
   309
  by (simp_all add: swap_def execute_simps)
haftmann@37758
   310
haftmann@37758
   311
lemma success_swapI [success_intros]:
haftmann@37802
   312
  "i < length h a \<Longrightarrow> success (swap i x a) h"
haftmann@37758
   313
  by (auto intro: success_intros simp add: swap_def)
haftmann@37752
   314
haftmann@40671
   315
lemma effect_swapI [effect_intros]:
haftmann@37806
   316
  assumes "i < length h a" "h' = update a i x h" "r = get h a ! i"
haftmann@40671
   317
  shows "effect (swap i x a) h h' r"
haftmann@40671
   318
  by (rule effectI) (insert assms, simp add: execute_simps)
haftmann@37771
   319
haftmann@40671
   320
lemma effect_swapE [effect_elims]:
haftmann@40671
   321
  assumes "effect (swap i x a) h h' r"
haftmann@37806
   322
  obtains "r = get h a ! i" "h' = update a i x h" "i < length h a"
haftmann@40671
   323
  using assms by (rule effectE)
haftmann@37802
   324
    (erule successE, cases "i < length h a", simp_all add: execute_simps)
haftmann@37771
   325
haftmann@37787
   326
lemma execute_freeze [execute_simps]:
haftmann@37806
   327
  "execute (freeze a) h = Some (get h a, h)"
haftmann@37787
   328
  by (simp add: freeze_def execute_simps)
haftmann@37758
   329
haftmann@37787
   330
lemma success_freezeI [success_intros]:
haftmann@37758
   331
  "success (freeze a) h"
haftmann@37787
   332
  by (auto intro: success_intros simp add: freeze_def)
haftmann@26170
   333
haftmann@40671
   334
lemma effect_freezeI [effect_intros]:
haftmann@37806
   335
  assumes "h' = h" "r = get h a"
haftmann@40671
   336
  shows "effect (freeze a) h h' r"
haftmann@40671
   337
  by (rule effectI) (insert assms, simp add: execute_simps)
haftmann@37771
   338
haftmann@40671
   339
lemma effect_freezeE [effect_elims]:
haftmann@40671
   340
  assumes "effect (freeze a) h h' r"
haftmann@37806
   341
  obtains "h' = h" "r = get h a"
haftmann@40671
   342
  using assms by (rule effectE) (simp add: execute_simps)
haftmann@37771
   343
haftmann@26170
   344
lemma upd_return:
haftmann@26170
   345
  "upd i x a \<guillemotright> return a = upd i x a"
haftmann@37787
   346
  by (rule Heap_eqI) (simp add: bind_def guard_def upd_def execute_simps)
haftmann@26170
   347
haftmann@37752
   348
lemma array_make:
haftmann@37752
   349
  "new n x = make n (\<lambda>_. x)"
haftmann@37787
   350
  by (rule Heap_eqI) (simp add: map_replicate_trivial execute_simps)
haftmann@26170
   351
haftmann@37845
   352
lemma array_of_list_make [code]:
haftmann@37752
   353
  "of_list xs = make (List.length xs) (\<lambda>n. xs ! n)"
haftmann@37787
   354
  by (rule Heap_eqI) (simp add: map_nth execute_simps)
haftmann@26170
   355
haftmann@37806
   356
hide_const (open) present get set alloc length update noteq new of_list make len nth upd map_entry swap freeze
haftmann@26170
   357
haftmann@26182
   358
haftmann@26182
   359
subsection {* Code generator setup *}
haftmann@26182
   360
haftmann@26182
   361
subsubsection {* Logical intermediate layer *}
haftmann@26182
   362
haftmann@26182
   363
definition new' where
haftmann@31205
   364
  [code del]: "new' = Array.new o Code_Numeral.nat_of"
haftmann@37752
   365
haftmann@28562
   366
lemma [code]:
haftmann@37752
   367
  "Array.new = new' o Code_Numeral.of_nat"
haftmann@26182
   368
  by (simp add: new'_def o_def)
haftmann@26182
   369
haftmann@26182
   370
definition make' where
haftmann@31205
   371
  [code del]: "make' i f = Array.make (Code_Numeral.nat_of i) (f o Code_Numeral.of_nat)"
haftmann@37752
   372
haftmann@28562
   373
lemma [code]:
haftmann@37752
   374
  "Array.make n f = make' (Code_Numeral.of_nat n) (f o Code_Numeral.nat_of)"
haftmann@26182
   375
  by (simp add: make'_def o_def)
haftmann@26182
   376
haftmann@37719
   377
definition len' where
haftmann@37719
   378
  [code del]: "len' a = Array.len a \<guillemotright>= (\<lambda>n. return (Code_Numeral.of_nat n))"
haftmann@37752
   379
haftmann@28562
   380
lemma [code]:
haftmann@37752
   381
  "Array.len a = len' a \<guillemotright>= (\<lambda>i. return (Code_Numeral.nat_of i))"
haftmann@37719
   382
  by (simp add: len'_def)
haftmann@26182
   383
haftmann@26182
   384
definition nth' where
haftmann@31205
   385
  [code del]: "nth' a = Array.nth a o Code_Numeral.nat_of"
haftmann@37752
   386
haftmann@28562
   387
lemma [code]:
haftmann@37752
   388
  "Array.nth a n = nth' a (Code_Numeral.of_nat n)"
haftmann@26182
   389
  by (simp add: nth'_def)
haftmann@26182
   390
haftmann@26182
   391
definition upd' where
haftmann@31205
   392
  [code del]: "upd' a i x = Array.upd (Code_Numeral.nat_of i) x a \<guillemotright> return ()"
haftmann@37752
   393
haftmann@28562
   394
lemma [code]:
haftmann@37752
   395
  "Array.upd i x a = upd' a (Code_Numeral.of_nat i) x \<guillemotright> return a"
haftmann@37709
   396
  by (simp add: upd'_def upd_return)
haftmann@26182
   397
haftmann@37752
   398
lemma [code]:
haftmann@37798
   399
  "Array.map_entry i f a = do {
haftmann@37798
   400
     x \<leftarrow> Array.nth a i;
haftmann@37798
   401
     Array.upd i (f x) a
krauss@37792
   402
   }"
haftmann@37758
   403
  by (rule Heap_eqI) (simp add: bind_def guard_def map_entry_def execute_simps)
haftmann@26182
   404
haftmann@37752
   405
lemma [code]:
haftmann@37798
   406
  "Array.swap i x a = do {
haftmann@37798
   407
     y \<leftarrow> Array.nth a i;
haftmann@37798
   408
     Array.upd i x a;
haftmann@37752
   409
     return y
krauss@37792
   410
   }"
haftmann@37758
   411
  by (rule Heap_eqI) (simp add: bind_def guard_def swap_def execute_simps)
haftmann@37752
   412
haftmann@37752
   413
lemma [code]:
haftmann@37798
   414
  "Array.freeze a = do {
haftmann@37798
   415
     n \<leftarrow> Array.len a;
haftmann@37798
   416
     Heap_Monad.fold_map (\<lambda>i. Array.nth a i) [0..<n]
krauss@37792
   417
   }"
haftmann@37752
   418
proof (rule Heap_eqI)
haftmann@37752
   419
  fix h
haftmann@37752
   420
  have *: "List.map
haftmann@37804
   421
     (\<lambda>x. fst (the (if x < Array.length h a
haftmann@37806
   422
                    then Some (Array.get h a ! x, h) else None)))
haftmann@37804
   423
     [0..<Array.length h a] =
haftmann@37806
   424
       List.map (List.nth (Array.get h a)) [0..<Array.length h a]"
haftmann@37752
   425
    by simp
haftmann@37804
   426
  have "execute (Heap_Monad.fold_map (Array.nth a) [0..<Array.length h a]) h =
haftmann@37806
   427
    Some (Array.get h a, h)"
haftmann@37756
   428
    apply (subst execute_fold_map_unchanged_heap)
haftmann@37752
   429
    apply (simp_all add: nth_def guard_def *)
haftmann@37752
   430
    apply (simp add: length_def map_nth)
haftmann@37752
   431
    done
krauss@37792
   432
  then have "execute (do {
haftmann@37798
   433
      n \<leftarrow> Array.len a;
haftmann@37756
   434
      Heap_Monad.fold_map (Array.nth a) [0..<n]
haftmann@37806
   435
    }) h = Some (Array.get h a, h)"
haftmann@37787
   436
    by (auto intro: execute_bind_eq_SomeI simp add: execute_simps)
haftmann@37798
   437
  then show "execute (Array.freeze a) h = execute (do {
haftmann@37798
   438
      n \<leftarrow> Array.len a;
haftmann@37756
   439
      Heap_Monad.fold_map (Array.nth a) [0..<n]
krauss@37792
   440
    }) h" by (simp add: execute_simps)
haftmann@37752
   441
qed
haftmann@37752
   442
haftmann@37831
   443
hide_const (open) new' make' len' nth' upd'
haftmann@37752
   444
haftmann@37752
   445
haftmann@37752
   446
text {* SML *}
haftmann@26182
   447
haftmann@26182
   448
code_type array (SML "_/ array")
haftmann@26182
   449
code_const Array (SML "raise/ (Fail/ \"bare Array\")")
haftmann@26752
   450
code_const Array.new' (SML "(fn/ ()/ =>/ Array.array/ ((_),/ (_)))")
haftmann@37831
   451
code_const Array.of_list (SML "(fn/ ()/ =>/ Array.fromList/ _)")
haftmann@26752
   452
code_const Array.make' (SML "(fn/ ()/ =>/ Array.tabulate/ ((_),/ (_)))")
haftmann@37719
   453
code_const Array.len' (SML "(fn/ ()/ =>/ Array.length/ _)")
haftmann@26752
   454
code_const Array.nth' (SML "(fn/ ()/ =>/ Array.sub/ ((_),/ (_)))")
haftmann@26752
   455
code_const Array.upd' (SML "(fn/ ()/ =>/ Array.update/ ((_),/ (_),/ (_)))")
haftmann@39716
   456
code_const "HOL.equal :: 'a array \<Rightarrow> 'a array \<Rightarrow> bool" (SML infixl 6 "=")
haftmann@26182
   457
haftmann@26182
   458
code_reserved SML Array
haftmann@26182
   459
haftmann@26182
   460
haftmann@37752
   461
text {* OCaml *}
haftmann@26182
   462
haftmann@26182
   463
code_type array (OCaml "_/ array")
haftmann@26182
   464
code_const Array (OCaml "failwith/ \"bare Array\"")
haftmann@32580
   465
code_const Array.new' (OCaml "(fun/ ()/ ->/ Array.make/ (Big'_int.int'_of'_big'_int/ _)/ _)")
haftmann@37831
   466
code_const Array.of_list (OCaml "(fun/ ()/ ->/ Array.of'_list/ _)")
haftmann@37831
   467
code_const Array.make' (OCaml "(fun/ ()/ ->/ Array.init/ (Big'_int.int'_of'_big'_int/ _)/
haftmann@37831
   468
  (fun k'_ ->/ _/ (Big'_int.big'_int'_of'_int/ k'_)))")
haftmann@37719
   469
code_const Array.len' (OCaml "(fun/ ()/ ->/ Big'_int.big'_int'_of'_int/ (Array.length/ _))")
haftmann@32580
   470
code_const Array.nth' (OCaml "(fun/ ()/ ->/ Array.get/ _/ (Big'_int.int'_of'_big'_int/ _))")
haftmann@32580
   471
code_const Array.upd' (OCaml "(fun/ ()/ ->/ Array.set/ _/ (Big'_int.int'_of'_big'_int/ _)/ _)")
haftmann@39716
   472
code_const "HOL.equal :: 'a array \<Rightarrow> 'a array \<Rightarrow> bool" (OCaml infixl 4 "=")
haftmann@26182
   473
haftmann@26182
   474
code_reserved OCaml Array
haftmann@26182
   475
haftmann@26182
   476
haftmann@37752
   477
text {* Haskell *}
haftmann@26182
   478
haftmann@29793
   479
code_type array (Haskell "Heap.STArray/ Heap.RealWorld/ _")
haftmann@26182
   480
code_const Array (Haskell "error/ \"bare Array\"")
haftmann@37831
   481
code_const Array.new' (Haskell "Heap.newArray")
haftmann@37831
   482
code_const Array.of_list (Haskell "Heap.newListArray")
haftmann@37831
   483
code_const Array.make' (Haskell "Heap.newFunArray")
haftmann@37719
   484
code_const Array.len' (Haskell "Heap.lengthArray")
haftmann@29793
   485
code_const Array.nth' (Haskell "Heap.readArray")
haftmann@29793
   486
code_const Array.upd' (Haskell "Heap.writeArray")
haftmann@39716
   487
code_const "HOL.equal :: 'a array \<Rightarrow> 'a array \<Rightarrow> bool" (Haskell infix 4 "==")
haftmann@39716
   488
code_instance array :: HOL.equal (Haskell -)
haftmann@26182
   489
haftmann@37842
   490
haftmann@37842
   491
text {* Scala *}
haftmann@37842
   492
haftmann@37845
   493
code_type array (Scala "!collection.mutable.ArraySeq[_]")
haftmann@37842
   494
code_const Array (Scala "!error(\"bare Array\")")
haftmann@38968
   495
code_const Array.new' (Scala "('_: Unit)/ => / Array.alloc((_))((_))")
haftmann@38968
   496
code_const Array.make' (Scala "('_: Unit)/ =>/ Array.make((_))((_))")
haftmann@38968
   497
code_const Array.len' (Scala "('_: Unit)/ =>/ Array.len((_))")
haftmann@38968
   498
code_const Array.nth' (Scala "('_: Unit)/ =>/ Array.nth((_), (_))")
haftmann@38968
   499
code_const Array.upd' (Scala "('_: Unit)/ =>/ Array.upd((_), (_), (_))")
haftmann@38968
   500
code_const Array.freeze (Scala "('_: Unit)/ =>/ Array.freeze((_))")
haftmann@39716
   501
code_const "HOL.equal :: 'a array \<Rightarrow> 'a array \<Rightarrow> bool" (Scala infixl 5 "==")
haftmann@37842
   502
haftmann@26170
   503
end