src/HOL/Imperative_HOL/Array.thy
author haftmann
Tue Jun 30 14:53:56 2009 +0200 (2009-06-30 ago)
changeset 31870 5274d3d0a6f2
parent 31205 98370b26c2ce
child 32580 5b88ae4307ff
permissions -rw-r--r--
dropped id
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@26170
    11
subsection {* Primitives *}
haftmann@26170
    12
haftmann@26170
    13
definition
haftmann@26170
    14
  new :: "nat \<Rightarrow> 'a\<Colon>heap \<Rightarrow> 'a array Heap" where
haftmann@26170
    15
  [code del]: "new n x = Heap_Monad.heap (Heap.array n x)"
haftmann@26170
    16
haftmann@26170
    17
definition
haftmann@26170
    18
  of_list :: "'a\<Colon>heap list \<Rightarrow> 'a array Heap" where
haftmann@26170
    19
  [code del]: "of_list xs = Heap_Monad.heap (Heap.array_of_list xs)"
haftmann@26170
    20
haftmann@26170
    21
definition
haftmann@26170
    22
  length :: "'a\<Colon>heap array \<Rightarrow> nat Heap" where
haftmann@26170
    23
  [code del]: "length arr = Heap_Monad.heap (\<lambda>h. (Heap.length arr h, h))"
haftmann@26170
    24
haftmann@26170
    25
definition
haftmann@26170
    26
  nth :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a Heap"
haftmann@26170
    27
where
haftmann@26170
    28
  [code del]: "nth a i = (do len \<leftarrow> length a;
haftmann@26170
    29
                 (if i < len
haftmann@26170
    30
                     then Heap_Monad.heap (\<lambda>h. (get_array a h ! i, h))
haftmann@26170
    31
                     else raise (''array lookup: index out of range''))
haftmann@26170
    32
              done)"
haftmann@26170
    33
haftmann@26170
    34
definition
haftmann@26170
    35
  upd :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a\<Colon>heap array Heap"
haftmann@26170
    36
where
haftmann@26170
    37
  [code del]: "upd i x a = (do len \<leftarrow> length a;
haftmann@26170
    38
                      (if i < len
haftmann@26719
    39
                           then Heap_Monad.heap (\<lambda>h. (a, Heap.upd a i x h))
haftmann@26719
    40
                           else raise (''array update: index out of range''))
haftmann@26170
    41
                   done)" 
haftmann@26170
    42
haftmann@26170
    43
lemma upd_return:
haftmann@26170
    44
  "upd i x a \<guillemotright> return a = upd i x a"
haftmann@26719
    45
proof (rule Heap_eqI)
haftmann@26719
    46
  fix h
haftmann@26719
    47
  obtain len h' where "Heap_Monad.execute (Array.length a) h = (len, h')"
haftmann@26719
    48
    by (cases "Heap_Monad.execute (Array.length a) h")
haftmann@26719
    49
  then show "Heap_Monad.execute (upd i x a \<guillemotright> return a) h = Heap_Monad.execute (upd i x a) h"
haftmann@28145
    50
    by (auto simp add: upd_def bindM_def split: sum.split)
haftmann@26719
    51
qed
haftmann@26170
    52
haftmann@26170
    53
haftmann@26170
    54
subsection {* Derivates *}
haftmann@26170
    55
haftmann@26170
    56
definition
haftmann@26170
    57
  map_entry :: "nat \<Rightarrow> ('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a array \<Rightarrow> 'a array Heap"
haftmann@26170
    58
where
haftmann@26170
    59
  "map_entry i f a = (do
haftmann@26170
    60
     x \<leftarrow> nth a i;
haftmann@26170
    61
     upd i (f x) a
haftmann@26170
    62
   done)"
haftmann@26170
    63
haftmann@26170
    64
definition
haftmann@26170
    65
  swap :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a Heap"
haftmann@26170
    66
where
haftmann@26170
    67
  "swap i x a = (do
haftmann@26170
    68
     y \<leftarrow> nth a i;
haftmann@26170
    69
     upd i x a;
haftmann@27596
    70
     return y
haftmann@26170
    71
   done)"
haftmann@26170
    72
haftmann@26170
    73
definition
haftmann@26170
    74
  make :: "nat \<Rightarrow> (nat \<Rightarrow> 'a\<Colon>heap) \<Rightarrow> 'a array Heap"
haftmann@26170
    75
where
haftmann@26170
    76
  "make n f = of_list (map f [0 ..< n])"
haftmann@26170
    77
haftmann@26170
    78
definition
haftmann@26170
    79
  freeze :: "'a\<Colon>heap array \<Rightarrow> 'a list Heap"
haftmann@26170
    80
where
haftmann@26170
    81
  "freeze a = (do
haftmann@26170
    82
     n \<leftarrow> length a;
haftmann@26170
    83
     mapM (nth a) [0..<n]
haftmann@26170
    84
   done)"
haftmann@26170
    85
bulwahn@27656
    86
definition
bulwahn@27656
    87
   map :: "('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a array \<Rightarrow> 'a array Heap"
bulwahn@27656
    88
where
bulwahn@27656
    89
  "map f a = (do
bulwahn@27656
    90
     n \<leftarrow> length a;
bulwahn@27656
    91
     mapM (\<lambda>n. map_entry n f a) [0..<n];
bulwahn@27656
    92
     return a
bulwahn@27656
    93
   done)"
bulwahn@27656
    94
bulwahn@27656
    95
hide (open) const new map -- {* avoid clashed with some popular names *}
haftmann@26170
    96
haftmann@26170
    97
haftmann@26170
    98
subsection {* Properties *}
haftmann@26170
    99
haftmann@28562
   100
lemma array_make [code]:
haftmann@26170
   101
  "Array.new n x = make n (\<lambda>_. x)"
haftmann@26170
   102
  by (induct n) (simp_all add: make_def new_def Heap_Monad.heap_def
haftmann@26170
   103
    monad_simp array_of_list_replicate [symmetric]
haftmann@26170
   104
    map_replicate_trivial replicate_append_same
haftmann@26170
   105
    of_list_def)
haftmann@26170
   106
haftmann@28562
   107
lemma array_of_list_make [code]:
haftmann@26170
   108
  "of_list xs = make (List.length xs) (\<lambda>n. xs ! n)"
haftmann@26170
   109
  unfolding make_def map_nth ..
haftmann@26170
   110
haftmann@26182
   111
haftmann@26182
   112
subsection {* Code generator setup *}
haftmann@26182
   113
haftmann@26182
   114
subsubsection {* Logical intermediate layer *}
haftmann@26182
   115
haftmann@26182
   116
definition new' where
haftmann@31205
   117
  [code del]: "new' = Array.new o Code_Numeral.nat_of"
haftmann@26182
   118
hide (open) const new'
haftmann@28562
   119
lemma [code]:
haftmann@31205
   120
  "Array.new = Array.new' o Code_Numeral.of_nat"
haftmann@26182
   121
  by (simp add: new'_def o_def)
haftmann@26182
   122
haftmann@26182
   123
definition of_list' where
haftmann@31205
   124
  [code del]: "of_list' i xs = Array.of_list (take (Code_Numeral.nat_of i) xs)"
haftmann@26182
   125
hide (open) const of_list'
haftmann@28562
   126
lemma [code]:
haftmann@31205
   127
  "Array.of_list xs = Array.of_list' (Code_Numeral.of_nat (List.length xs)) xs"
haftmann@26182
   128
  by (simp add: of_list'_def)
haftmann@26182
   129
haftmann@26182
   130
definition make' where
haftmann@31205
   131
  [code del]: "make' i f = Array.make (Code_Numeral.nat_of i) (f o Code_Numeral.of_nat)"
haftmann@26182
   132
hide (open) const make'
haftmann@28562
   133
lemma [code]:
haftmann@31205
   134
  "Array.make n f = Array.make' (Code_Numeral.of_nat n) (f o Code_Numeral.nat_of)"
haftmann@26182
   135
  by (simp add: make'_def o_def)
haftmann@26182
   136
haftmann@26182
   137
definition length' where
haftmann@31205
   138
  [code del]: "length' = Array.length \<guillemotright>== liftM Code_Numeral.of_nat"
haftmann@26182
   139
hide (open) const length'
haftmann@28562
   140
lemma [code]:
haftmann@31205
   141
  "Array.length = Array.length' \<guillemotright>== liftM Code_Numeral.nat_of"
haftmann@26182
   142
  by (simp add: length'_def monad_simp',
haftmann@26182
   143
    simp add: liftM_def comp_def monad_simp,
haftmann@26182
   144
    simp add: monad_simp')
haftmann@26182
   145
haftmann@26182
   146
definition nth' where
haftmann@31205
   147
  [code del]: "nth' a = Array.nth a o Code_Numeral.nat_of"
haftmann@26182
   148
hide (open) const nth'
haftmann@28562
   149
lemma [code]:
haftmann@31205
   150
  "Array.nth a n = Array.nth' a (Code_Numeral.of_nat n)"
haftmann@26182
   151
  by (simp add: nth'_def)
haftmann@26182
   152
haftmann@26182
   153
definition upd' where
haftmann@31205
   154
  [code del]: "upd' a i x = Array.upd (Code_Numeral.nat_of i) x a \<guillemotright> return ()"
haftmann@26182
   155
hide (open) const upd'
haftmann@28562
   156
lemma [code]:
haftmann@31205
   157
  "Array.upd i x a = Array.upd' a (Code_Numeral.of_nat i) x \<guillemotright> return a"
haftmann@26743
   158
  by (simp add: upd'_def monad_simp upd_return)
haftmann@26182
   159
haftmann@26182
   160
haftmann@26182
   161
subsubsection {* SML *}
haftmann@26182
   162
haftmann@26182
   163
code_type array (SML "_/ array")
haftmann@26182
   164
code_const Array (SML "raise/ (Fail/ \"bare Array\")")
haftmann@26752
   165
code_const Array.new' (SML "(fn/ ()/ =>/ Array.array/ ((_),/ (_)))")
haftmann@26752
   166
code_const Array.of_list (SML "(fn/ ()/ =>/ Array.fromList/ _)")
haftmann@26752
   167
code_const Array.make' (SML "(fn/ ()/ =>/ Array.tabulate/ ((_),/ (_)))")
haftmann@26752
   168
code_const Array.length' (SML "(fn/ ()/ =>/ Array.length/ _)")
haftmann@26752
   169
code_const Array.nth' (SML "(fn/ ()/ =>/ Array.sub/ ((_),/ (_)))")
haftmann@26752
   170
code_const Array.upd' (SML "(fn/ ()/ =>/ Array.update/ ((_),/ (_),/ (_)))")
haftmann@26182
   171
haftmann@26182
   172
code_reserved SML Array
haftmann@26182
   173
haftmann@26182
   174
haftmann@26182
   175
subsubsection {* OCaml *}
haftmann@26182
   176
haftmann@26182
   177
code_type array (OCaml "_/ array")
haftmann@26182
   178
code_const Array (OCaml "failwith/ \"bare Array\"")
haftmann@27673
   179
code_const Array.new' (OCaml "(fun/ ()/ ->/ Array.make/ _/ _)")
haftmann@27673
   180
code_const Array.of_list (OCaml "(fun/ ()/ ->/ Array.of'_list/ _)")
haftmann@27673
   181
code_const Array.make' (OCaml "(fun/ ()/ ->/ Array.init/ _/ _)")
haftmann@27673
   182
code_const Array.length' (OCaml "(fun/ ()/ ->/ Array.length/ _)")
haftmann@27673
   183
code_const Array.nth' (OCaml "(fun/ ()/ ->/ Array.get/ _/ _)")
haftmann@27673
   184
code_const Array.upd' (OCaml "(fun/ ()/ ->/ Array.set/ _/ _/ _)")
haftmann@26182
   185
haftmann@26182
   186
code_reserved OCaml Array
haftmann@26182
   187
haftmann@26182
   188
haftmann@26182
   189
subsubsection {* Haskell *}
haftmann@26182
   190
haftmann@29793
   191
code_type array (Haskell "Heap.STArray/ Heap.RealWorld/ _")
haftmann@26182
   192
code_const Array (Haskell "error/ \"bare Array\"")
haftmann@29793
   193
code_const Array.new' (Haskell "Heap.newArray/ (0,/ _)")
haftmann@29793
   194
code_const Array.of_list' (Haskell "Heap.newListArray/ (0,/ _)")
haftmann@29793
   195
code_const Array.length' (Haskell "Heap.lengthArray")
haftmann@29793
   196
code_const Array.nth' (Haskell "Heap.readArray")
haftmann@29793
   197
code_const Array.upd' (Haskell "Heap.writeArray")
haftmann@26182
   198
haftmann@26170
   199
end