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