src/HOL/Library/IArray.thy
author haftmann
Tue Feb 19 19:44:10 2013 +0100 (2013-02-19)
changeset 51188 9b5bf1a9a710
parent 51161 6ed12ae3b3e1
child 52435 6646bb548c6b
permissions -rw-r--r--
dropped spurious left-over from 0a2371e7ced3
nipkow@50138
     1
header "Immutable Arrays with Code Generation"
nipkow@50138
     2
nipkow@50138
     3
theory IArray
haftmann@51094
     4
imports Main
nipkow@50138
     5
begin
nipkow@50138
     6
nipkow@50138
     7
text{* Immutable arrays are lists wrapped up in an additional constructor.
nipkow@50138
     8
There are no update operations. Hence code generation can safely implement
nipkow@50138
     9
this type by efficient target language arrays.  Currently only SML is
nipkow@50138
    10
provided. Should be extended to other target languages and more operations.
nipkow@50138
    11
nipkow@50138
    12
Note that arrays cannot be printed directly but only by turning them into
nipkow@50138
    13
lists first. Arrays could be converted back into lists for printing if they
nipkow@50138
    14
were wrapped up in an additional constructor. *}
nipkow@50138
    15
nipkow@50138
    16
datatype 'a iarray = IArray "'a list"
nipkow@50138
    17
haftmann@51094
    18
primrec list_of :: "'a iarray \<Rightarrow> 'a list" where
nipkow@50138
    19
"list_of (IArray xs) = xs"
nipkow@50138
    20
hide_const (open) list_of
nipkow@50138
    21
haftmann@51094
    22
definition of_fun :: "(nat \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a iarray" where
haftmann@51094
    23
[simp]: "of_fun f n = IArray (map f [0..<n])"
haftmann@51094
    24
hide_const (open) of_fun
haftmann@51094
    25
haftmann@51094
    26
definition sub :: "'a iarray \<Rightarrow> nat \<Rightarrow> 'a" (infixl "!!" 100) where
haftmann@51094
    27
[simp]: "as !! n = IArray.list_of as ! n"
haftmann@51094
    28
hide_const (open) sub
haftmann@51094
    29
haftmann@51094
    30
definition length :: "'a iarray \<Rightarrow> nat" where
haftmann@51094
    31
[simp]: "length as = List.length (IArray.list_of as)"
haftmann@51094
    32
hide_const (open) length
haftmann@51094
    33
haftmann@51094
    34
lemma list_of_code [code]:
haftmann@51094
    35
"IArray.list_of as = map (\<lambda>n. as !! n) [0 ..< IArray.length as]"
haftmann@51094
    36
by (cases as) (simp add: map_nth)
haftmann@51094
    37
nipkow@50138
    38
nipkow@50138
    39
subsection "Code Generation"
nipkow@50138
    40
haftmann@51094
    41
code_reserved SML Vector
haftmann@51094
    42
nipkow@50138
    43
code_type iarray
nipkow@50138
    44
  (SML "_ Vector.vector")
nipkow@50138
    45
nipkow@50138
    46
code_const IArray
nipkow@50138
    47
  (SML "Vector.fromList")
haftmann@51094
    48
haftmann@51161
    49
lemma [code]:
haftmann@51161
    50
"size (as :: 'a iarray) = 0"
haftmann@51161
    51
by (cases as) simp
haftmann@51161
    52
haftmann@51161
    53
lemma [code]:
haftmann@51161
    54
"iarray_size f as = Suc (list_size f (IArray.list_of as))"
haftmann@51161
    55
by (cases as) simp
haftmann@51161
    56
haftmann@51161
    57
lemma [code]:
haftmann@51161
    58
"iarray_rec f as = f (IArray.list_of as)"
haftmann@51161
    59
by (cases as) simp
haftmann@51161
    60
haftmann@51161
    61
lemma [code]:
haftmann@51161
    62
"iarray_case f as = f (IArray.list_of as)"
haftmann@51161
    63
by (cases as) simp
haftmann@51161
    64
haftmann@51161
    65
lemma [code]:
haftmann@51161
    66
"HOL.equal as bs \<longleftrightarrow> HOL.equal (IArray.list_of as) (IArray.list_of bs)"
haftmann@51161
    67
by (cases as, cases bs) (simp add: equal)
haftmann@51161
    68
haftmann@51143
    69
primrec tabulate :: "integer \<times> (integer \<Rightarrow> 'a) \<Rightarrow> 'a iarray" where
haftmann@51143
    70
"tabulate (n, f) = IArray (map (f \<circ> integer_of_nat) [0..<nat_of_integer n])"
haftmann@51094
    71
hide_const (open) tabulate
haftmann@51094
    72
haftmann@51094
    73
lemma [code]:
haftmann@51143
    74
"IArray.of_fun f n = IArray.tabulate (integer_of_nat n, f \<circ> nat_of_integer)"
haftmann@51094
    75
by simp 
haftmann@51094
    76
haftmann@51094
    77
code_const IArray.tabulate
haftmann@51094
    78
  (SML "Vector.tabulate")
haftmann@51094
    79
haftmann@51143
    80
primrec sub' :: "'a iarray \<times> integer \<Rightarrow> 'a" where
haftmann@51143
    81
"sub' (as, n) = IArray.list_of as ! nat_of_integer n"
haftmann@51094
    82
hide_const (open) sub'
haftmann@51094
    83
haftmann@51094
    84
lemma [code]:
haftmann@51143
    85
"as !! n = IArray.sub' (as, integer_of_nat n)"
haftmann@51094
    86
by simp
haftmann@51094
    87
haftmann@51094
    88
code_const IArray.sub'
haftmann@51094
    89
  (SML "Vector.sub")
haftmann@51094
    90
haftmann@51143
    91
definition length' :: "'a iarray \<Rightarrow> integer" where
haftmann@51143
    92
[simp]: "length' as = integer_of_nat (List.length (IArray.list_of as))"
haftmann@51094
    93
hide_const (open) length'
haftmann@51094
    94
haftmann@51094
    95
lemma [code]:
haftmann@51143
    96
"IArray.length as = nat_of_integer (IArray.length' as)"
haftmann@51094
    97
by simp
haftmann@51094
    98
haftmann@51094
    99
code_const IArray.length'
nipkow@50138
   100
  (SML "Vector.length")
nipkow@50138
   101
nipkow@50138
   102
end
haftmann@51094
   103