src/HOL/Imperative_HOL/Array.thy
author haftmann
Thu Aug 26 10:16:22 2010 +0200 (2010-08-26)
changeset 38771 f9cd27cbe8a4
parent 37964 0a1ae22df1f1
child 38968 e55deaa22fff
permissions -rw-r--r--
code_include Scala: qualify module nmae
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)"
haftmann@37804
   102
  by (simp add: Let_def expand_fun_eq 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"
haftmann@37806
   118
  by (simp add: update_def length_def set_def get_def expand_fun_eq)
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@37806
   137
  "get (snd (alloc ls' h)) (fst (alloc ls h)) = ls'"
haftmann@37804
   138
  by (simp add: Let_def split_def alloc_def)
haftmann@37719
   139
haftmann@37807
   140
lemma set:
haftmann@37804
   141
  "set (fst (alloc ls h))
haftmann@37804
   142
     new_ls (snd (alloc ls h))
haftmann@37804
   143
       = snd (alloc new_ls h)"
haftmann@37804
   144
  by (simp add: Let_def split_def alloc_def)
haftmann@37719
   145
haftmann@37807
   146
lemma present_update [simp]: 
haftmann@37804
   147
  "present (update b i v h) = present h"
haftmann@37806
   148
  by (simp add: update_def present_def set_def get_def expand_fun_eq)
haftmann@37719
   149
haftmann@37807
   150
lemma present_alloc [simp]:
haftmann@37804
   151
  "present (snd (alloc xs h)) (fst (alloc xs h))"
haftmann@37804
   152
  by (simp add: present_def alloc_def set_def Let_def)
haftmann@37771
   153
haftmann@37807
   154
lemma not_present_alloc [simp]:
haftmann@37804
   155
  "\<not> present h (fst (alloc xs h))"
haftmann@37804
   156
  by (simp add: present_def alloc_def Let_def)
haftmann@37771
   157
haftmann@37758
   158
haftmann@37758
   159
text {* Monad operations *}
haftmann@37758
   160
haftmann@37787
   161
lemma execute_new [execute_simps]:
haftmann@37804
   162
  "execute (new n x) h = Some (alloc (replicate n x) h)"
haftmann@37787
   163
  by (simp add: new_def execute_simps)
haftmann@37758
   164
haftmann@37787
   165
lemma success_newI [success_intros]:
haftmann@37758
   166
  "success (new n x) h"
haftmann@37787
   167
  by (auto intro: success_intros simp add: new_def)
haftmann@26170
   168
haftmann@37771
   169
lemma crel_newI [crel_intros]:
haftmann@37804
   170
  assumes "(a, h') = alloc (replicate n x) h"
haftmann@37771
   171
  shows "crel (new n x) h h' a"
haftmann@37787
   172
  by (rule crelI) (simp add: assms execute_simps)
haftmann@37771
   173
haftmann@37771
   174
lemma crel_newE [crel_elims]:
haftmann@37771
   175
  assumes "crel (new n x) h h' r"
haftmann@37804
   176
  obtains "r = fst (alloc (replicate n x) h)" "h' = snd (alloc (replicate n x) h)" 
haftmann@37806
   177
    "get h' r = replicate n x" "present h' r" "\<not> present h r"
haftmann@37807
   178
  using assms by (rule crelE) (simp add: get_alloc execute_simps)
haftmann@37771
   179
haftmann@37787
   180
lemma execute_of_list [execute_simps]:
haftmann@37804
   181
  "execute (of_list xs) h = Some (alloc xs h)"
haftmann@37787
   182
  by (simp add: of_list_def execute_simps)
haftmann@37758
   183
haftmann@37787
   184
lemma success_of_listI [success_intros]:
haftmann@37758
   185
  "success (of_list xs) h"
haftmann@37787
   186
  by (auto intro: success_intros simp add: of_list_def)
haftmann@26170
   187
haftmann@37771
   188
lemma crel_of_listI [crel_intros]:
haftmann@37804
   189
  assumes "(a, h') = alloc xs h"
haftmann@37771
   190
  shows "crel (of_list xs) h h' a"
haftmann@37787
   191
  by (rule crelI) (simp add: assms execute_simps)
haftmann@37771
   192
haftmann@37771
   193
lemma crel_of_listE [crel_elims]:
haftmann@37771
   194
  assumes "crel (of_list xs) h h' r"
haftmann@37804
   195
  obtains "r = fst (alloc xs h)" "h' = snd (alloc xs h)" 
haftmann@37806
   196
    "get h' r = xs" "present h' r" "\<not> present h r"
haftmann@37807
   197
  using assms by (rule crelE) (simp add: get_alloc execute_simps)
haftmann@37771
   198
haftmann@37787
   199
lemma execute_make [execute_simps]:
haftmann@37804
   200
  "execute (make n f) h = Some (alloc (map f [0 ..< n]) h)"
haftmann@37787
   201
  by (simp add: make_def execute_simps)
haftmann@26170
   202
haftmann@37787
   203
lemma success_makeI [success_intros]:
haftmann@37758
   204
  "success (make n f) h"
haftmann@37787
   205
  by (auto intro: success_intros simp add: make_def)
haftmann@37758
   206
haftmann@37771
   207
lemma crel_makeI [crel_intros]:
haftmann@37804
   208
  assumes "(a, h') = alloc (map f [0 ..< n]) h"
haftmann@37771
   209
  shows "crel (make n f) h h' a"
haftmann@37787
   210
  by (rule crelI) (simp add: assms execute_simps)
haftmann@37771
   211
haftmann@37771
   212
lemma crel_makeE [crel_elims]:
haftmann@37771
   213
  assumes "crel (make n f) h h' r"
haftmann@37804
   214
  obtains "r = fst (alloc (map f [0 ..< n]) h)" "h' = snd (alloc (map f [0 ..< n]) h)" 
haftmann@37806
   215
    "get h' r = map f [0 ..< n]" "present h' r" "\<not> present h r"
haftmann@37807
   216
  using assms by (rule crelE) (simp add: get_alloc execute_simps)
haftmann@37771
   217
haftmann@37787
   218
lemma execute_len [execute_simps]:
haftmann@37802
   219
  "execute (len a) h = Some (length h a, h)"
haftmann@37787
   220
  by (simp add: len_def execute_simps)
haftmann@37758
   221
haftmann@37787
   222
lemma success_lenI [success_intros]:
haftmann@37758
   223
  "success (len a) h"
haftmann@37787
   224
  by (auto intro: success_intros simp add: len_def)
haftmann@37752
   225
haftmann@37771
   226
lemma crel_lengthI [crel_intros]:
haftmann@37802
   227
  assumes "h' = h" "r = length h a"
haftmann@37771
   228
  shows "crel (len a) h h' r"
haftmann@37787
   229
  by (rule crelI) (simp add: assms execute_simps)
haftmann@37771
   230
haftmann@37771
   231
lemma crel_lengthE [crel_elims]:
haftmann@37771
   232
  assumes "crel (len a) h h' r"
haftmann@37802
   233
  obtains "r = length h' a" "h' = h" 
haftmann@37787
   234
  using assms by (rule crelE) (simp add: execute_simps)
haftmann@37771
   235
haftmann@37758
   236
lemma execute_nth [execute_simps]:
haftmann@37802
   237
  "i < length h a \<Longrightarrow>
haftmann@37806
   238
    execute (nth a i) h = Some (get h a ! i, h)"
haftmann@37802
   239
  "i \<ge> length h a \<Longrightarrow> execute (nth a i) h = None"
haftmann@37758
   240
  by (simp_all add: nth_def execute_simps)
haftmann@37758
   241
haftmann@37758
   242
lemma success_nthI [success_intros]:
haftmann@37802
   243
  "i < length h a \<Longrightarrow> success (nth a i) h"
haftmann@37758
   244
  by (auto intro: success_intros simp add: nth_def)
haftmann@26170
   245
haftmann@37771
   246
lemma crel_nthI [crel_intros]:
haftmann@37806
   247
  assumes "i < length h a" "h' = h" "r = get h a ! i"
haftmann@37771
   248
  shows "crel (nth a i) h h' r"
haftmann@37771
   249
  by (rule crelI) (insert assms, simp add: execute_simps)
haftmann@37771
   250
haftmann@37771
   251
lemma crel_nthE [crel_elims]:
haftmann@37771
   252
  assumes "crel (nth a i) h h' r"
haftmann@37806
   253
  obtains "i < length h a" "r = get h a ! i" "h' = h"
haftmann@37771
   254
  using assms by (rule crelE)
haftmann@37802
   255
    (erule successE, cases "i < length h a", simp_all add: execute_simps)
haftmann@37771
   256
haftmann@37758
   257
lemma execute_upd [execute_simps]:
haftmann@37802
   258
  "i < length h a \<Longrightarrow>
haftmann@37798
   259
    execute (upd i x a) h = Some (a, update a i x h)"
haftmann@37802
   260
  "i \<ge> length h a \<Longrightarrow> execute (upd i x a) h = None"
haftmann@37758
   261
  by (simp_all add: upd_def execute_simps)
haftmann@26170
   262
haftmann@37758
   263
lemma success_updI [success_intros]:
haftmann@37802
   264
  "i < length h a \<Longrightarrow> success (upd i x a) h"
haftmann@37758
   265
  by (auto intro: success_intros simp add: upd_def)
haftmann@37758
   266
haftmann@37771
   267
lemma crel_updI [crel_intros]:
haftmann@37802
   268
  assumes "i < length h a" "h' = update a i v h"
haftmann@37771
   269
  shows "crel (upd i v a) h h' a"
haftmann@37771
   270
  by (rule crelI) (insert assms, simp add: execute_simps)
haftmann@37771
   271
haftmann@37771
   272
lemma crel_updE [crel_elims]:
haftmann@37771
   273
  assumes "crel (upd i v a) h h' r"
haftmann@37802
   274
  obtains "r = a" "h' = update a i v h" "i < length h a"
haftmann@37771
   275
  using assms by (rule crelE)
haftmann@37802
   276
    (erule successE, cases "i < length h a", simp_all add: execute_simps)
haftmann@37771
   277
haftmann@37758
   278
lemma execute_map_entry [execute_simps]:
haftmann@37802
   279
  "i < length h a \<Longrightarrow>
haftmann@37758
   280
   execute (map_entry i f a) h =
haftmann@37806
   281
      Some (a, update a i (f (get h a ! i)) h)"
haftmann@37802
   282
  "i \<ge> length h a \<Longrightarrow> execute (map_entry i f a) h = None"
haftmann@37758
   283
  by (simp_all add: map_entry_def execute_simps)
haftmann@37752
   284
haftmann@37758
   285
lemma success_map_entryI [success_intros]:
haftmann@37802
   286
  "i < length h a \<Longrightarrow> success (map_entry i f a) h"
haftmann@37758
   287
  by (auto intro: success_intros simp add: map_entry_def)
haftmann@37758
   288
haftmann@37771
   289
lemma crel_map_entryI [crel_intros]:
haftmann@37806
   290
  assumes "i < length h a" "h' = update a i (f (get h a ! i)) h" "r = a"
haftmann@37771
   291
  shows "crel (map_entry i f a) h h' r"
haftmann@37771
   292
  by (rule crelI) (insert assms, simp add: execute_simps)
haftmann@37771
   293
haftmann@37771
   294
lemma crel_map_entryE [crel_elims]:
haftmann@37771
   295
  assumes "crel (map_entry i f a) h h' r"
haftmann@37806
   296
  obtains "r = a" "h' = update a i (f (get h a ! i)) h" "i < length h a"
haftmann@37771
   297
  using assms by (rule crelE)
haftmann@37802
   298
    (erule successE, cases "i < length h a", simp_all add: execute_simps)
haftmann@37771
   299
haftmann@37758
   300
lemma execute_swap [execute_simps]:
haftmann@37802
   301
  "i < length h a \<Longrightarrow>
haftmann@37758
   302
   execute (swap i x a) h =
haftmann@37806
   303
      Some (get h a ! i, update a i x h)"
haftmann@37802
   304
  "i \<ge> length h a \<Longrightarrow> execute (swap i x a) h = None"
haftmann@37758
   305
  by (simp_all add: swap_def execute_simps)
haftmann@37758
   306
haftmann@37758
   307
lemma success_swapI [success_intros]:
haftmann@37802
   308
  "i < length h a \<Longrightarrow> success (swap i x a) h"
haftmann@37758
   309
  by (auto intro: success_intros simp add: swap_def)
haftmann@37752
   310
haftmann@37771
   311
lemma crel_swapI [crel_intros]:
haftmann@37806
   312
  assumes "i < length h a" "h' = update a i x h" "r = get h a ! i"
haftmann@37771
   313
  shows "crel (swap i x a) h h' r"
haftmann@37771
   314
  by (rule crelI) (insert assms, simp add: execute_simps)
haftmann@37771
   315
haftmann@37771
   316
lemma crel_swapE [crel_elims]:
haftmann@37771
   317
  assumes "crel (swap i x a) h h' r"
haftmann@37806
   318
  obtains "r = get h a ! i" "h' = update a i x h" "i < length h a"
haftmann@37771
   319
  using assms by (rule crelE)
haftmann@37802
   320
    (erule successE, cases "i < length h a", simp_all add: execute_simps)
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@37771
   330
lemma crel_freezeI [crel_intros]:
haftmann@37806
   331
  assumes "h' = h" "r = get h a"
haftmann@37771
   332
  shows "crel (freeze a) h h' r"
haftmann@37771
   333
  by (rule crelI) (insert assms, simp add: execute_simps)
haftmann@37771
   334
haftmann@37771
   335
lemma crel_freezeE [crel_elims]:
haftmann@37771
   336
  assumes "crel (freeze a) h h' r"
haftmann@37806
   337
  obtains "h' = h" "r = get h a"
haftmann@37787
   338
  using assms by (rule crelE) (simp add: execute_simps)
haftmann@37771
   339
haftmann@26170
   340
lemma upd_return:
haftmann@26170
   341
  "upd i x a \<guillemotright> 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
haftmann@26182
   355
subsection {* Code generator setup *}
haftmann@26182
   356
haftmann@26182
   357
subsubsection {* Logical intermediate layer *}
haftmann@26182
   358
haftmann@26182
   359
definition new' where
haftmann@31205
   360
  [code del]: "new' = Array.new o Code_Numeral.nat_of"
haftmann@37752
   361
haftmann@28562
   362
lemma [code]:
haftmann@37752
   363
  "Array.new = new' o Code_Numeral.of_nat"
haftmann@26182
   364
  by (simp add: new'_def o_def)
haftmann@26182
   365
haftmann@26182
   366
definition make' where
haftmann@31205
   367
  [code del]: "make' i f = Array.make (Code_Numeral.nat_of i) (f o Code_Numeral.of_nat)"
haftmann@37752
   368
haftmann@28562
   369
lemma [code]:
haftmann@37752
   370
  "Array.make n f = make' (Code_Numeral.of_nat n) (f o Code_Numeral.nat_of)"
haftmann@26182
   371
  by (simp add: make'_def o_def)
haftmann@26182
   372
haftmann@37719
   373
definition len' where
haftmann@37719
   374
  [code del]: "len' a = Array.len a \<guillemotright>= (\<lambda>n. return (Code_Numeral.of_nat n))"
haftmann@37752
   375
haftmann@28562
   376
lemma [code]:
haftmann@37752
   377
  "Array.len a = len' a \<guillemotright>= (\<lambda>i. return (Code_Numeral.nat_of i))"
haftmann@37719
   378
  by (simp add: len'_def)
haftmann@26182
   379
haftmann@26182
   380
definition nth' where
haftmann@31205
   381
  [code del]: "nth' a = Array.nth a o Code_Numeral.nat_of"
haftmann@37752
   382
haftmann@28562
   383
lemma [code]:
haftmann@37752
   384
  "Array.nth a n = nth' a (Code_Numeral.of_nat n)"
haftmann@26182
   385
  by (simp add: nth'_def)
haftmann@26182
   386
haftmann@26182
   387
definition upd' where
haftmann@31205
   388
  [code del]: "upd' a i x = Array.upd (Code_Numeral.nat_of i) x a \<guillemotright> return ()"
haftmann@37752
   389
haftmann@28562
   390
lemma [code]:
haftmann@37752
   391
  "Array.upd i x a = upd' a (Code_Numeral.of_nat i) x \<guillemotright> 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
haftmann@37752
   442
text {* SML *}
haftmann@26182
   443
haftmann@26182
   444
code_type array (SML "_/ array")
haftmann@26182
   445
code_const Array (SML "raise/ (Fail/ \"bare Array\")")
haftmann@26752
   446
code_const Array.new' (SML "(fn/ ()/ =>/ Array.array/ ((_),/ (_)))")
haftmann@37831
   447
code_const Array.of_list (SML "(fn/ ()/ =>/ Array.fromList/ _)")
haftmann@26752
   448
code_const Array.make' (SML "(fn/ ()/ =>/ Array.tabulate/ ((_),/ (_)))")
haftmann@37719
   449
code_const Array.len' (SML "(fn/ ()/ =>/ Array.length/ _)")
haftmann@26752
   450
code_const Array.nth' (SML "(fn/ ()/ =>/ Array.sub/ ((_),/ (_)))")
haftmann@26752
   451
code_const Array.upd' (SML "(fn/ ()/ =>/ Array.update/ ((_),/ (_),/ (_)))")
haftmann@26182
   452
haftmann@26182
   453
code_reserved SML Array
haftmann@26182
   454
haftmann@26182
   455
haftmann@37752
   456
text {* OCaml *}
haftmann@26182
   457
haftmann@26182
   458
code_type array (OCaml "_/ array")
haftmann@26182
   459
code_const Array (OCaml "failwith/ \"bare Array\"")
haftmann@32580
   460
code_const Array.new' (OCaml "(fun/ ()/ ->/ Array.make/ (Big'_int.int'_of'_big'_int/ _)/ _)")
haftmann@37831
   461
code_const Array.of_list (OCaml "(fun/ ()/ ->/ Array.of'_list/ _)")
haftmann@37831
   462
code_const Array.make' (OCaml "(fun/ ()/ ->/ Array.init/ (Big'_int.int'_of'_big'_int/ _)/
haftmann@37831
   463
  (fun k'_ ->/ _/ (Big'_int.big'_int'_of'_int/ k'_)))")
haftmann@37719
   464
code_const Array.len' (OCaml "(fun/ ()/ ->/ Big'_int.big'_int'_of'_int/ (Array.length/ _))")
haftmann@32580
   465
code_const Array.nth' (OCaml "(fun/ ()/ ->/ Array.get/ _/ (Big'_int.int'_of'_big'_int/ _))")
haftmann@32580
   466
code_const Array.upd' (OCaml "(fun/ ()/ ->/ Array.set/ _/ (Big'_int.int'_of'_big'_int/ _)/ _)")
haftmann@26182
   467
haftmann@26182
   468
code_reserved OCaml Array
haftmann@26182
   469
haftmann@26182
   470
haftmann@37752
   471
text {* Haskell *}
haftmann@26182
   472
haftmann@29793
   473
code_type array (Haskell "Heap.STArray/ Heap.RealWorld/ _")
haftmann@26182
   474
code_const Array (Haskell "error/ \"bare Array\"")
haftmann@37831
   475
code_const Array.new' (Haskell "Heap.newArray")
haftmann@37831
   476
code_const Array.of_list (Haskell "Heap.newListArray")
haftmann@37831
   477
code_const Array.make' (Haskell "Heap.newFunArray")
haftmann@37719
   478
code_const Array.len' (Haskell "Heap.lengthArray")
haftmann@29793
   479
code_const Array.nth' (Haskell "Heap.readArray")
haftmann@29793
   480
code_const Array.upd' (Haskell "Heap.writeArray")
haftmann@26182
   481
haftmann@37842
   482
haftmann@37842
   483
text {* Scala *}
haftmann@37842
   484
haftmann@37845
   485
code_type array (Scala "!collection.mutable.ArraySeq[_]")
haftmann@37842
   486
code_const Array (Scala "!error(\"bare Array\")")
haftmann@38771
   487
code_const Array.new' (Scala "('_: Unit)/ => / Heap.Array.alloc((_))((_))")
haftmann@38771
   488
code_const Array.make' (Scala "('_: Unit)/ =>/ Heap.Array.make((_))((_))")
haftmann@38771
   489
code_const Array.len' (Scala "('_: Unit)/ =>/ Heap.Array.len((_))")
haftmann@38771
   490
code_const Array.nth' (Scala "('_: Unit)/ =>/ Heap.Array.nth((_), (_))")
haftmann@38771
   491
code_const Array.upd' (Scala "('_: Unit)/ =>/ Heap.Array.upd((_), (_), (_))")
haftmann@38771
   492
code_const Array.freeze (Scala "('_: Unit)/ =>/ Heap.Array.freeze((_))")
haftmann@37842
   493
haftmann@26170
   494
end