src/HOL/Imperative_HOL/Array.thy
author haftmann
Mon Jul 05 15:25:42 2010 +0200 (2010-07-05 ago)
changeset 37716 24bb91462892
parent 37709 70fafefbcc98
child 37719 271ecd4fb9f9
permissions -rw-r--r--
remove primitive operation Heap.array in favour of Heap.array_of_list
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@37716
    15
  [code del]: "new n x = Heap_Monad.heap (Heap.array (replicate n x))"
haftmann@26170
    16
haftmann@26170
    17
definition
haftmann@26170
    18
  of_list :: "'a\<Colon>heap list \<Rightarrow> 'a array Heap" where
haftmann@37716
    19
  [code del]: "of_list xs = Heap_Monad.heap (Heap.array 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@37709
    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@37709
    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@37709
    45
  by (rule Heap_eqI) (simp add: upd_def bindM_def split: option.split) 
haftmann@26170
    46
haftmann@26170
    47
haftmann@26170
    48
subsection {* Derivates *}
haftmann@26170
    49
haftmann@26170
    50
definition
haftmann@26170
    51
  map_entry :: "nat \<Rightarrow> ('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a array \<Rightarrow> 'a array Heap"
haftmann@26170
    52
where
haftmann@26170
    53
  "map_entry i f a = (do
haftmann@26170
    54
     x \<leftarrow> nth a i;
haftmann@26170
    55
     upd i (f x) a
haftmann@26170
    56
   done)"
haftmann@26170
    57
haftmann@26170
    58
definition
haftmann@26170
    59
  swap :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a Heap"
haftmann@26170
    60
where
haftmann@26170
    61
  "swap i x a = (do
haftmann@26170
    62
     y \<leftarrow> nth a i;
haftmann@26170
    63
     upd i x a;
haftmann@27596
    64
     return y
haftmann@26170
    65
   done)"
haftmann@26170
    66
haftmann@26170
    67
definition
haftmann@26170
    68
  make :: "nat \<Rightarrow> (nat \<Rightarrow> 'a\<Colon>heap) \<Rightarrow> 'a array Heap"
haftmann@26170
    69
where
haftmann@26170
    70
  "make n f = of_list (map f [0 ..< n])"
haftmann@26170
    71
haftmann@26170
    72
definition
haftmann@26170
    73
  freeze :: "'a\<Colon>heap array \<Rightarrow> 'a list Heap"
haftmann@26170
    74
where
haftmann@26170
    75
  "freeze a = (do
haftmann@26170
    76
     n \<leftarrow> length a;
haftmann@26170
    77
     mapM (nth a) [0..<n]
haftmann@26170
    78
   done)"
haftmann@26170
    79
bulwahn@27656
    80
definition
bulwahn@27656
    81
   map :: "('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a array \<Rightarrow> 'a array Heap"
bulwahn@27656
    82
where
bulwahn@27656
    83
  "map f a = (do
bulwahn@27656
    84
     n \<leftarrow> length a;
bulwahn@27656
    85
     mapM (\<lambda>n. map_entry n f a) [0..<n];
bulwahn@27656
    86
     return a
bulwahn@27656
    87
   done)"
bulwahn@27656
    88
wenzelm@36176
    89
hide_const (open) new map -- {* avoid clashed with some popular names *}
haftmann@26170
    90
haftmann@26170
    91
haftmann@26170
    92
subsection {* Properties *}
haftmann@26170
    93
haftmann@28562
    94
lemma array_make [code]:
haftmann@26170
    95
  "Array.new n x = make n (\<lambda>_. x)"
haftmann@37716
    96
  by (rule Heap_eqI) (simp add: make_def new_def map_replicate_trivial of_list_def)
haftmann@26170
    97
haftmann@28562
    98
lemma array_of_list_make [code]:
haftmann@26170
    99
  "of_list xs = make (List.length xs) (\<lambda>n. xs ! n)"
haftmann@37709
   100
  by (rule Heap_eqI) (simp add: make_def map_nth)
haftmann@26170
   101
haftmann@26182
   102
haftmann@26182
   103
subsection {* Code generator setup *}
haftmann@26182
   104
haftmann@26182
   105
subsubsection {* Logical intermediate layer *}
haftmann@26182
   106
haftmann@26182
   107
definition new' where
haftmann@31205
   108
  [code del]: "new' = Array.new o Code_Numeral.nat_of"
wenzelm@36176
   109
hide_const (open) new'
haftmann@28562
   110
lemma [code]:
haftmann@31205
   111
  "Array.new = Array.new' o Code_Numeral.of_nat"
haftmann@26182
   112
  by (simp add: new'_def o_def)
haftmann@26182
   113
haftmann@26182
   114
definition of_list' where
haftmann@31205
   115
  [code del]: "of_list' i xs = Array.of_list (take (Code_Numeral.nat_of i) xs)"
wenzelm@36176
   116
hide_const (open) of_list'
haftmann@28562
   117
lemma [code]:
haftmann@31205
   118
  "Array.of_list xs = Array.of_list' (Code_Numeral.of_nat (List.length xs)) xs"
haftmann@26182
   119
  by (simp add: of_list'_def)
haftmann@26182
   120
haftmann@26182
   121
definition make' where
haftmann@31205
   122
  [code del]: "make' i f = Array.make (Code_Numeral.nat_of i) (f o Code_Numeral.of_nat)"
wenzelm@36176
   123
hide_const (open) make'
haftmann@28562
   124
lemma [code]:
haftmann@31205
   125
  "Array.make n f = Array.make' (Code_Numeral.of_nat n) (f o Code_Numeral.nat_of)"
haftmann@26182
   126
  by (simp add: make'_def o_def)
haftmann@26182
   127
haftmann@26182
   128
definition length' where
haftmann@37709
   129
  [code del]: "length' a = Array.length a \<guillemotright>= (\<lambda>n. return (Code_Numeral.of_nat n))"
wenzelm@36176
   130
hide_const (open) length'
haftmann@28562
   131
lemma [code]:
haftmann@37709
   132
  "Array.length a = Array.length' a \<guillemotright>= (\<lambda>i. return (Code_Numeral.nat_of i))"
haftmann@37709
   133
  by (simp add: length'_def)
haftmann@26182
   134
haftmann@26182
   135
definition nth' where
haftmann@31205
   136
  [code del]: "nth' a = Array.nth a o Code_Numeral.nat_of"
wenzelm@36176
   137
hide_const (open) nth'
haftmann@28562
   138
lemma [code]:
haftmann@31205
   139
  "Array.nth a n = Array.nth' a (Code_Numeral.of_nat n)"
haftmann@26182
   140
  by (simp add: nth'_def)
haftmann@26182
   141
haftmann@26182
   142
definition upd' where
haftmann@31205
   143
  [code del]: "upd' a i x = Array.upd (Code_Numeral.nat_of i) x a \<guillemotright> return ()"
wenzelm@36176
   144
hide_const (open) upd'
haftmann@28562
   145
lemma [code]:
haftmann@31205
   146
  "Array.upd i x a = Array.upd' a (Code_Numeral.of_nat i) x \<guillemotright> return a"
haftmann@37709
   147
  by (simp add: upd'_def upd_return)
haftmann@26182
   148
haftmann@26182
   149
haftmann@26182
   150
subsubsection {* SML *}
haftmann@26182
   151
haftmann@26182
   152
code_type array (SML "_/ array")
haftmann@26182
   153
code_const Array (SML "raise/ (Fail/ \"bare Array\")")
haftmann@26752
   154
code_const Array.new' (SML "(fn/ ()/ =>/ Array.array/ ((_),/ (_)))")
haftmann@35846
   155
code_const Array.of_list' (SML "(fn/ ()/ =>/ Array.fromList/ _)")
haftmann@26752
   156
code_const Array.make' (SML "(fn/ ()/ =>/ Array.tabulate/ ((_),/ (_)))")
haftmann@26752
   157
code_const Array.length' (SML "(fn/ ()/ =>/ Array.length/ _)")
haftmann@26752
   158
code_const Array.nth' (SML "(fn/ ()/ =>/ Array.sub/ ((_),/ (_)))")
haftmann@26752
   159
code_const Array.upd' (SML "(fn/ ()/ =>/ Array.update/ ((_),/ (_),/ (_)))")
haftmann@26182
   160
haftmann@26182
   161
code_reserved SML Array
haftmann@26182
   162
haftmann@26182
   163
haftmann@26182
   164
subsubsection {* OCaml *}
haftmann@26182
   165
haftmann@26182
   166
code_type array (OCaml "_/ array")
haftmann@26182
   167
code_const Array (OCaml "failwith/ \"bare Array\"")
haftmann@32580
   168
code_const Array.new' (OCaml "(fun/ ()/ ->/ Array.make/ (Big'_int.int'_of'_big'_int/ _)/ _)")
haftmann@35846
   169
code_const Array.of_list' (OCaml "(fun/ ()/ ->/ Array.of'_list/ _)")
haftmann@32580
   170
code_const Array.length' (OCaml "(fun/ ()/ ->/ Big'_int.big'_int'_of'_int/ (Array.length/ _))")
haftmann@32580
   171
code_const Array.nth' (OCaml "(fun/ ()/ ->/ Array.get/ _/ (Big'_int.int'_of'_big'_int/ _))")
haftmann@32580
   172
code_const Array.upd' (OCaml "(fun/ ()/ ->/ Array.set/ _/ (Big'_int.int'_of'_big'_int/ _)/ _)")
haftmann@26182
   173
haftmann@26182
   174
code_reserved OCaml Array
haftmann@26182
   175
haftmann@26182
   176
haftmann@26182
   177
subsubsection {* Haskell *}
haftmann@26182
   178
haftmann@29793
   179
code_type array (Haskell "Heap.STArray/ Heap.RealWorld/ _")
haftmann@26182
   180
code_const Array (Haskell "error/ \"bare Array\"")
haftmann@29793
   181
code_const Array.new' (Haskell "Heap.newArray/ (0,/ _)")
haftmann@29793
   182
code_const Array.of_list' (Haskell "Heap.newListArray/ (0,/ _)")
haftmann@29793
   183
code_const Array.length' (Haskell "Heap.lengthArray")
haftmann@29793
   184
code_const Array.nth' (Haskell "Heap.readArray")
haftmann@29793
   185
code_const Array.upd' (Haskell "Heap.writeArray")
haftmann@26182
   186
haftmann@26170
   187
end