src/HOL/Library/Array.thy
author haftmann
Wed Feb 27 21:41:08 2008 +0100 (2008-02-27 ago)
changeset 26170 66e6b967ccf1
child 26182 8262ec0e8782
permissions -rw-r--r--
added theories for imperative HOL
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@26170
     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
-- {* 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 {* Converting arrays to lists *}
haftmann@26170
   103
haftmann@26170
   104
primrec list_of_aux :: "nat \<Rightarrow> ('a\<Colon>heap) array \<Rightarrow> 'a list \<Rightarrow> 'a list Heap" where
haftmann@26170
   105
  "list_of_aux 0 a xs = return xs"
haftmann@26170
   106
  | "list_of_aux (Suc n) a xs = (do
haftmann@26170
   107
        x \<leftarrow> Array.nth a n;
haftmann@26170
   108
        list_of_aux n a (x#xs)
haftmann@26170
   109
     done)"
haftmann@26170
   110
haftmann@26170
   111
definition list_of :: "('a\<Colon>heap) array \<Rightarrow> 'a list Heap" where
haftmann@26170
   112
  "list_of a = (do n \<leftarrow> Array.length a;
haftmann@26170
   113
                   list_of_aux n a []
haftmann@26170
   114
                done)"
haftmann@26170
   115
haftmann@26170
   116
haftmann@26170
   117
subsection {* Properties *}
haftmann@26170
   118
haftmann@26170
   119
lemma array_make [code func]:
haftmann@26170
   120
  "Array.new n x = make n (\<lambda>_. x)"
haftmann@26170
   121
  by (induct n) (simp_all add: make_def new_def Heap_Monad.heap_def
haftmann@26170
   122
    monad_simp array_of_list_replicate [symmetric]
haftmann@26170
   123
    map_replicate_trivial replicate_append_same
haftmann@26170
   124
    of_list_def)
haftmann@26170
   125
haftmann@26170
   126
lemma array_of_list_make [code func]:
haftmann@26170
   127
  "of_list xs = make (List.length xs) (\<lambda>n. xs ! n)"
haftmann@26170
   128
  unfolding make_def map_nth ..
haftmann@26170
   129
haftmann@26170
   130
end