src/HOL/Library/Array.thy
author haftmann
Thu Feb 28 16:50:52 2008 +0100 (2008-02-28 ago)
changeset 26182 8262ec0e8782
parent 26170 66e6b967ccf1
child 26719 a259d259c797
permissions -rw-r--r--
added code generator setup
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@26182
     9
imports Heap_Monad Code_Index
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
-- {* FIXME adjustion for List theory *}
haftmann@26170
    36
no_syntax
haftmann@26170
    37
  nth  :: "'a list \<Rightarrow> nat \<Rightarrow> 'a" (infixl "!" 100)
haftmann@26170
    38
haftmann@26170
    39
abbreviation
haftmann@26170
    40
  nth_list :: "'a list \<Rightarrow> nat \<Rightarrow> 'a" (infixl "!" 100)
haftmann@26170
    41
where
haftmann@26170
    42
  "nth_list \<equiv> List.nth"
haftmann@26170
    43
haftmann@26170
    44
definition
haftmann@26170
    45
  upd :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a\<Colon>heap array Heap"
haftmann@26170
    46
where
haftmann@26170
    47
  [code del]: "upd i x a = (do len \<leftarrow> length a;
haftmann@26170
    48
                      (if i < len
haftmann@26170
    49
                           then Heap_Monad.heap (\<lambda>h. ((), Heap.upd a i x h))
haftmann@26170
    50
                           else raise (''array update: index out of range''));
haftmann@26170
    51
                      return a
haftmann@26170
    52
                   done)" 
haftmann@26170
    53
haftmann@26170
    54
lemma upd_return:
haftmann@26170
    55
  "upd i x a \<guillemotright> return a = upd i x a"
haftmann@26170
    56
  unfolding upd_def by (simp add: monad_simp)
haftmann@26170
    57
haftmann@26170
    58
haftmann@26170
    59
subsection {* Derivates *}
haftmann@26170
    60
haftmann@26170
    61
definition
haftmann@26170
    62
  map_entry :: "nat \<Rightarrow> ('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a array \<Rightarrow> 'a array Heap"
haftmann@26170
    63
where
haftmann@26170
    64
  "map_entry i f a = (do
haftmann@26170
    65
     x \<leftarrow> nth a i;
haftmann@26170
    66
     upd i (f x) a
haftmann@26170
    67
   done)"
haftmann@26170
    68
haftmann@26170
    69
definition
haftmann@26170
    70
  swap :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a Heap"
haftmann@26170
    71
where
haftmann@26170
    72
  "swap i x a = (do
haftmann@26170
    73
     y \<leftarrow> nth a i;
haftmann@26170
    74
     upd i x a;
haftmann@26170
    75
     return x
haftmann@26170
    76
   done)"
haftmann@26170
    77
haftmann@26170
    78
definition
haftmann@26170
    79
  make :: "nat \<Rightarrow> (nat \<Rightarrow> 'a\<Colon>heap) \<Rightarrow> 'a array Heap"
haftmann@26170
    80
where
haftmann@26170
    81
  "make n f = of_list (map f [0 ..< n])"
haftmann@26170
    82
haftmann@26170
    83
definition
haftmann@26170
    84
  freeze :: "'a\<Colon>heap array \<Rightarrow> 'a list Heap"
haftmann@26170
    85
where
haftmann@26170
    86
  "freeze a = (do
haftmann@26170
    87
     n \<leftarrow> length a;
haftmann@26170
    88
     mapM (nth a) [0..<n]
haftmann@26170
    89
   done)"
haftmann@26170
    90
haftmann@26170
    91
definition
haftmann@26170
    92
  map :: "('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a array \<Rightarrow> 'a array Heap"
haftmann@26170
    93
where
haftmann@26170
    94
  "map f a = (do
haftmann@26170
    95
     n \<leftarrow> length a;
haftmann@26170
    96
     foldM (\<lambda>n. map_entry n f) [0..<n] a
haftmann@26170
    97
   done)"
haftmann@26170
    98
haftmann@26170
    99
hide (open) const new map -- {* avoid clashed with some popular names *}
haftmann@26170
   100
haftmann@26170
   101
haftmann@26170
   102
subsection {* Properties *}
haftmann@26170
   103
haftmann@26170
   104
lemma array_make [code func]:
haftmann@26170
   105
  "Array.new n x = make n (\<lambda>_. x)"
haftmann@26170
   106
  by (induct n) (simp_all add: make_def new_def Heap_Monad.heap_def
haftmann@26170
   107
    monad_simp array_of_list_replicate [symmetric]
haftmann@26170
   108
    map_replicate_trivial replicate_append_same
haftmann@26170
   109
    of_list_def)
haftmann@26170
   110
haftmann@26170
   111
lemma array_of_list_make [code func]:
haftmann@26170
   112
  "of_list xs = make (List.length xs) (\<lambda>n. xs ! n)"
haftmann@26170
   113
  unfolding make_def map_nth ..
haftmann@26170
   114
haftmann@26182
   115
haftmann@26182
   116
subsection {* Code generator setup *}
haftmann@26182
   117
haftmann@26182
   118
subsubsection {* Logical intermediate layer *}
haftmann@26182
   119
haftmann@26182
   120
definition new' where
haftmann@26182
   121
  [code del]: "new' = Array.new o nat_of_index"
haftmann@26182
   122
hide (open) const new'
haftmann@26182
   123
lemma [code func]:
haftmann@26182
   124
  "Array.new = Array.new' o index_of_nat"
haftmann@26182
   125
  by (simp add: new'_def o_def)
haftmann@26182
   126
haftmann@26182
   127
definition of_list' where
haftmann@26182
   128
  [code del]: "of_list' i xs = Array.of_list (take (nat_of_index i) xs)"
haftmann@26182
   129
hide (open) const of_list'
haftmann@26182
   130
lemma [code func]:
haftmann@26182
   131
  "Array.of_list xs = Array.of_list' (index_of_nat (List.length xs)) xs"
haftmann@26182
   132
  by (simp add: of_list'_def)
haftmann@26182
   133
haftmann@26182
   134
definition make' where
haftmann@26182
   135
  [code del]: "make' i f = Array.make (nat_of_index i) (f o index_of_nat)"
haftmann@26182
   136
hide (open) const make'
haftmann@26182
   137
lemma [code func]:
haftmann@26182
   138
  "Array.make n f = Array.make' (index_of_nat n) (f o nat_of_index)"
haftmann@26182
   139
  by (simp add: make'_def o_def)
haftmann@26182
   140
haftmann@26182
   141
definition length' where
haftmann@26182
   142
  [code del]: "length' = Array.length \<guillemotright>== liftM index_of_nat"
haftmann@26182
   143
hide (open) const length'
haftmann@26182
   144
lemma [code func]:
haftmann@26182
   145
  "Array.length = Array.length' \<guillemotright>== liftM nat_of_index"
haftmann@26182
   146
  by (simp add: length'_def monad_simp',
haftmann@26182
   147
    simp add: liftM_def comp_def monad_simp,
haftmann@26182
   148
    simp add: monad_simp')
haftmann@26182
   149
haftmann@26182
   150
definition nth' where
haftmann@26182
   151
  [code del]: "nth' a = Array.nth a o nat_of_index"
haftmann@26182
   152
hide (open) const nth'
haftmann@26182
   153
lemma [code func]:
haftmann@26182
   154
  "Array.nth a n = Array.nth' a (index_of_nat n)"
haftmann@26182
   155
  by (simp add: nth'_def)
haftmann@26182
   156
haftmann@26182
   157
definition upd' where
haftmann@26182
   158
  [code del]: "upd' a i x = Array.upd (nat_of_index i) x a \<guillemotright> return ()"
haftmann@26182
   159
hide (open) const upd'
haftmann@26182
   160
lemma [code func]:
haftmann@26182
   161
  "Array.upd i x a = Array.upd' a (index_of_nat i) x \<guillemotright> return a"
haftmann@26182
   162
  by (simp add: upd'_def monad_simp upd_return)
haftmann@26182
   163
haftmann@26182
   164
haftmann@26182
   165
subsubsection {* SML *}
haftmann@26182
   166
haftmann@26182
   167
code_type array (SML "_/ array")
haftmann@26182
   168
code_const Array (SML "raise/ (Fail/ \"bare Array\")")
haftmann@26182
   169
code_const Array.new' (SML "Array.array ((_), (_))")
haftmann@26182
   170
code_const Array.of_list (SML "Array.fromList")
haftmann@26182
   171
code_const Array.make' (SML "Array.tabulate ((_), (_))")
haftmann@26182
   172
code_const Array.length' (SML "Array.length")
haftmann@26182
   173
code_const Array.nth' (SML "Array.sub ((_), (_))")
haftmann@26182
   174
code_const Array.upd' (SML "Array.update ((_), (_), (_))")
haftmann@26182
   175
haftmann@26182
   176
code_reserved SML Array
haftmann@26182
   177
haftmann@26182
   178
haftmann@26182
   179
subsubsection {* OCaml *}
haftmann@26182
   180
haftmann@26182
   181
code_type array (OCaml "_/ array")
haftmann@26182
   182
code_const Array (OCaml "failwith/ \"bare Array\"")
haftmann@26182
   183
code_const Array.new' (OCaml "Array.make")
haftmann@26182
   184
code_const Array.of_list (OCaml "Array.of_list")
haftmann@26182
   185
code_const Array.make' (OCaml "Array.init")
haftmann@26182
   186
code_const Array.length' (OCaml "Array.length")
haftmann@26182
   187
code_const Array.nth' (OCaml "Array.get")
haftmann@26182
   188
code_const Array.upd' (OCaml "Array.set")
haftmann@26182
   189
haftmann@26182
   190
code_reserved OCaml Array
haftmann@26182
   191
haftmann@26182
   192
haftmann@26182
   193
subsubsection {* Haskell *}
haftmann@26182
   194
haftmann@26182
   195
code_type array (Haskell "STArray '_s _")
haftmann@26182
   196
code_const Array (Haskell "error/ \"bare Array\"")
haftmann@26182
   197
code_const Array.new' (Haskell "newArray/ (0,/ _)")
haftmann@26182
   198
code_const Array.of_list' (Haskell "newListArray/ (0,/ _)")
haftmann@26182
   199
code_const Array.length' (Haskell "length")
haftmann@26182
   200
code_const Array.nth' (Haskell "readArray")
haftmann@26182
   201
code_const Array.upd' (Haskell "writeArray")
haftmann@26182
   202
haftmann@26182
   203
haftmann@26170
   204
end