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
     1 (*  Title:      HOL/Imperative_HOL/Array.thy
     2     Author:     John Matthews, Galois Connections; Alexander Krauss, Lukas Bulwahn & Florian Haftmann, TU Muenchen
     3 *)
     4 
     5 header {* Monadic arrays *}
     6 
     7 theory Array
     8 imports Heap_Monad
     9 begin
    10 
    11 subsection {* Primitives *}
    12 
    13 definition
    14   new :: "nat \<Rightarrow> 'a\<Colon>heap \<Rightarrow> 'a array Heap" where
    15   [code del]: "new n x = Heap_Monad.heap (Heap.array (replicate n x))"
    16 
    17 definition
    18   of_list :: "'a\<Colon>heap list \<Rightarrow> 'a array Heap" where
    19   [code del]: "of_list xs = Heap_Monad.heap (Heap.array xs)"
    20 
    21 definition
    22   length :: "'a\<Colon>heap array \<Rightarrow> nat Heap" where
    23   [code del]: "length arr = Heap_Monad.heap (\<lambda>h. (Heap.length arr h, h))"
    24 
    25 definition
    26   nth :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a Heap"
    27 where
    28   [code del]: "nth a i = (do len \<leftarrow> length a;
    29                  (if i < len
    30                      then Heap_Monad.heap (\<lambda>h. (get_array a h ! i, h))
    31                      else raise ''array lookup: index out of range'')
    32               done)"
    33 
    34 definition
    35   upd :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a\<Colon>heap array Heap"
    36 where
    37   [code del]: "upd i x a = (do len \<leftarrow> length a;
    38                       (if i < len
    39                            then Heap_Monad.heap (\<lambda>h. (a, Heap.upd a i x h))
    40                            else raise ''array update: index out of range'')
    41                    done)" 
    42 
    43 lemma upd_return:
    44   "upd i x a \<guillemotright> return a = upd i x a"
    45   by (rule Heap_eqI) (simp add: upd_def bindM_def split: option.split) 
    46 
    47 
    48 subsection {* Derivates *}
    49 
    50 definition
    51   map_entry :: "nat \<Rightarrow> ('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a array \<Rightarrow> 'a array Heap"
    52 where
    53   "map_entry i f a = (do
    54      x \<leftarrow> nth a i;
    55      upd i (f x) a
    56    done)"
    57 
    58 definition
    59   swap :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a Heap"
    60 where
    61   "swap i x a = (do
    62      y \<leftarrow> nth a i;
    63      upd i x a;
    64      return y
    65    done)"
    66 
    67 definition
    68   make :: "nat \<Rightarrow> (nat \<Rightarrow> 'a\<Colon>heap) \<Rightarrow> 'a array Heap"
    69 where
    70   "make n f = of_list (map f [0 ..< n])"
    71 
    72 definition
    73   freeze :: "'a\<Colon>heap array \<Rightarrow> 'a list Heap"
    74 where
    75   "freeze a = (do
    76      n \<leftarrow> length a;
    77      mapM (nth a) [0..<n]
    78    done)"
    79 
    80 definition
    81    map :: "('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a array \<Rightarrow> 'a array Heap"
    82 where
    83   "map f a = (do
    84      n \<leftarrow> length a;
    85      mapM (\<lambda>n. map_entry n f a) [0..<n];
    86      return a
    87    done)"
    88 
    89 hide_const (open) new map -- {* avoid clashed with some popular names *}
    90 
    91 
    92 subsection {* Properties *}
    93 
    94 lemma array_make [code]:
    95   "Array.new n x = make n (\<lambda>_. x)"
    96   by (rule Heap_eqI) (simp add: make_def new_def map_replicate_trivial of_list_def)
    97 
    98 lemma array_of_list_make [code]:
    99   "of_list xs = make (List.length xs) (\<lambda>n. xs ! n)"
   100   by (rule Heap_eqI) (simp add: make_def map_nth)
   101 
   102 
   103 subsection {* Code generator setup *}
   104 
   105 subsubsection {* Logical intermediate layer *}
   106 
   107 definition new' where
   108   [code del]: "new' = Array.new o Code_Numeral.nat_of"
   109 hide_const (open) new'
   110 lemma [code]:
   111   "Array.new = Array.new' o Code_Numeral.of_nat"
   112   by (simp add: new'_def o_def)
   113 
   114 definition of_list' where
   115   [code del]: "of_list' i xs = Array.of_list (take (Code_Numeral.nat_of i) xs)"
   116 hide_const (open) of_list'
   117 lemma [code]:
   118   "Array.of_list xs = Array.of_list' (Code_Numeral.of_nat (List.length xs)) xs"
   119   by (simp add: of_list'_def)
   120 
   121 definition make' where
   122   [code del]: "make' i f = Array.make (Code_Numeral.nat_of i) (f o Code_Numeral.of_nat)"
   123 hide_const (open) make'
   124 lemma [code]:
   125   "Array.make n f = Array.make' (Code_Numeral.of_nat n) (f o Code_Numeral.nat_of)"
   126   by (simp add: make'_def o_def)
   127 
   128 definition length' where
   129   [code del]: "length' a = Array.length a \<guillemotright>= (\<lambda>n. return (Code_Numeral.of_nat n))"
   130 hide_const (open) length'
   131 lemma [code]:
   132   "Array.length a = Array.length' a \<guillemotright>= (\<lambda>i. return (Code_Numeral.nat_of i))"
   133   by (simp add: length'_def)
   134 
   135 definition nth' where
   136   [code del]: "nth' a = Array.nth a o Code_Numeral.nat_of"
   137 hide_const (open) nth'
   138 lemma [code]:
   139   "Array.nth a n = Array.nth' a (Code_Numeral.of_nat n)"
   140   by (simp add: nth'_def)
   141 
   142 definition upd' where
   143   [code del]: "upd' a i x = Array.upd (Code_Numeral.nat_of i) x a \<guillemotright> return ()"
   144 hide_const (open) upd'
   145 lemma [code]:
   146   "Array.upd i x a = Array.upd' a (Code_Numeral.of_nat i) x \<guillemotright> return a"
   147   by (simp add: upd'_def upd_return)
   148 
   149 
   150 subsubsection {* SML *}
   151 
   152 code_type array (SML "_/ array")
   153 code_const Array (SML "raise/ (Fail/ \"bare Array\")")
   154 code_const Array.new' (SML "(fn/ ()/ =>/ Array.array/ ((_),/ (_)))")
   155 code_const Array.of_list' (SML "(fn/ ()/ =>/ Array.fromList/ _)")
   156 code_const Array.make' (SML "(fn/ ()/ =>/ Array.tabulate/ ((_),/ (_)))")
   157 code_const Array.length' (SML "(fn/ ()/ =>/ Array.length/ _)")
   158 code_const Array.nth' (SML "(fn/ ()/ =>/ Array.sub/ ((_),/ (_)))")
   159 code_const Array.upd' (SML "(fn/ ()/ =>/ Array.update/ ((_),/ (_),/ (_)))")
   160 
   161 code_reserved SML Array
   162 
   163 
   164 subsubsection {* OCaml *}
   165 
   166 code_type array (OCaml "_/ array")
   167 code_const Array (OCaml "failwith/ \"bare Array\"")
   168 code_const Array.new' (OCaml "(fun/ ()/ ->/ Array.make/ (Big'_int.int'_of'_big'_int/ _)/ _)")
   169 code_const Array.of_list' (OCaml "(fun/ ()/ ->/ Array.of'_list/ _)")
   170 code_const Array.length' (OCaml "(fun/ ()/ ->/ Big'_int.big'_int'_of'_int/ (Array.length/ _))")
   171 code_const Array.nth' (OCaml "(fun/ ()/ ->/ Array.get/ _/ (Big'_int.int'_of'_big'_int/ _))")
   172 code_const Array.upd' (OCaml "(fun/ ()/ ->/ Array.set/ _/ (Big'_int.int'_of'_big'_int/ _)/ _)")
   173 
   174 code_reserved OCaml Array
   175 
   176 
   177 subsubsection {* Haskell *}
   178 
   179 code_type array (Haskell "Heap.STArray/ Heap.RealWorld/ _")
   180 code_const Array (Haskell "error/ \"bare Array\"")
   181 code_const Array.new' (Haskell "Heap.newArray/ (0,/ _)")
   182 code_const Array.of_list' (Haskell "Heap.newListArray/ (0,/ _)")
   183 code_const Array.length' (Haskell "Heap.lengthArray")
   184 code_const Array.nth' (Haskell "Heap.readArray")
   185 code_const Array.upd' (Haskell "Heap.writeArray")
   186 
   187 end