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