src/HOL/Library/IArray.thy
author haftmann
Wed Feb 13 13:38:52 2013 +0100 (2013-02-13)
changeset 51094 84b03c49c223
parent 50138 ca989d793b34
child 51143 0a2371e7ced3
permissions -rw-r--r--
IArray ignorant of particular representation of nat
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@51094
    49
primrec tabulate :: "code_numeral \<times> (code_numeral \<Rightarrow> 'a) \<Rightarrow> 'a iarray" where
haftmann@51094
    50
"tabulate (n, f) = IArray (map (f \<circ> Code_Numeral.of_nat) [0..<Code_Numeral.nat_of n])"
haftmann@51094
    51
hide_const (open) tabulate
haftmann@51094
    52
haftmann@51094
    53
lemma [code]:
haftmann@51094
    54
"IArray.of_fun f n = IArray.tabulate (Code_Numeral.of_nat n, f \<circ> Code_Numeral.nat_of)"
haftmann@51094
    55
by simp 
haftmann@51094
    56
haftmann@51094
    57
code_const IArray.tabulate
haftmann@51094
    58
  (SML "Vector.tabulate")
haftmann@51094
    59
haftmann@51094
    60
primrec sub' :: "'a iarray \<times> code_numeral \<Rightarrow> 'a" where
haftmann@51094
    61
"sub' (as, n) = IArray.list_of as ! Code_Numeral.nat_of n"
haftmann@51094
    62
hide_const (open) sub'
haftmann@51094
    63
haftmann@51094
    64
lemma [code]:
haftmann@51094
    65
"as !! n = IArray.sub' (as, Code_Numeral.of_nat n)"
haftmann@51094
    66
by simp
haftmann@51094
    67
haftmann@51094
    68
code_const IArray.sub'
haftmann@51094
    69
  (SML "Vector.sub")
haftmann@51094
    70
haftmann@51094
    71
definition length' :: "'a iarray \<Rightarrow> code_numeral" where
haftmann@51094
    72
[simp]: "length' as = Code_Numeral.of_nat (List.length (IArray.list_of as))"
haftmann@51094
    73
hide_const (open) length'
haftmann@51094
    74
haftmann@51094
    75
lemma [code]:
haftmann@51094
    76
"IArray.length as = Code_Numeral.nat_of (IArray.length' as)"
haftmann@51094
    77
by simp
haftmann@51094
    78
haftmann@51094
    79
code_const IArray.length'
nipkow@50138
    80
  (SML "Vector.length")
nipkow@50138
    81
nipkow@50138
    82
end
haftmann@51094
    83